org.el 953 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783177841778517786177871778817789177901779117792177931779417795177961779717798177991780017801178021780317804178051780617807178081780917810178111781217813178141781517816178171781817819178201782117822178231782417825178261782717828178291783017831178321783317834178351783617837178381783917840178411784217843178441784517846178471784817849178501785117852178531785417855178561785717858178591786017861178621786317864178651786617867178681786917870178711787217873178741787517876178771787817879178801788117882178831788417885178861788717888178891789017891178921789317894178951789617897178981789917900179011790217903179041790517906179071790817909179101791117912179131791417915179161791717918179191792017921179221792317924179251792617927179281792917930179311793217933179341793517936179371793817939179401794117942179431794417945179461794717948179491795017951179521795317954179551795617957179581795917960179611796217963179641796517966179671796817969179701797117972179731797417975179761797717978179791798017981179821798317984179851798617987179881798917990179911799217993179941799517996179971799817999180001800118002180031800418005180061800718008180091801018011180121801318014180151801618017180181801918020180211802218023180241802518026180271802818029180301803118032180331803418035180361803718038180391804018041180421804318044180451804618047180481804918050180511805218053180541805518056180571805818059180601806118062180631806418065180661806718068180691807018071180721807318074180751807618077180781807918080180811808218083180841808518086180871808818089180901809118092180931809418095180961809718098180991810018101181021810318104181051810618107181081810918110181111811218113181141811518116181171811818119181201812118122181231812418125181261812718128181291813018131181321813318134181351813618137181381813918140181411814218143181441814518146181471814818149181501815118152181531815418155181561815718158181591816018161181621816318164181651816618167181681816918170181711817218173181741817518176181771817818179181801818118182181831818418185181861818718188181891819018191181921819318194181951819618197181981819918200182011820218203182041820518206182071820818209182101821118212182131821418215182161821718218182191822018221182221822318224182251822618227182281822918230182311823218233182341823518236182371823818239182401824118242182431824418245182461824718248182491825018251182521825318254182551825618257182581825918260182611826218263182641826518266182671826818269182701827118272182731827418275182761827718278182791828018281182821828318284182851828618287182881828918290182911829218293182941829518296182971829818299183001830118302183031830418305183061830718308183091831018311183121831318314183151831618317183181831918320183211832218323183241832518326183271832818329183301833118332183331833418335183361833718338183391834018341183421834318344183451834618347183481834918350183511835218353183541835518356183571835818359183601836118362183631836418365183661836718368183691837018371183721837318374183751837618377183781837918380183811838218383183841838518386183871838818389183901839118392183931839418395183961839718398183991840018401184021840318404184051840618407184081840918410184111841218413184141841518416184171841818419184201842118422184231842418425184261842718428184291843018431184321843318434184351843618437184381843918440184411844218443184441844518446184471844818449184501845118452184531845418455184561845718458184591846018461184621846318464184651846618467184681846918470184711847218473184741847518476184771847818479184801848118482184831848418485184861848718488184891849018491184921849318494184951849618497184981849918500185011850218503185041850518506185071850818509185101851118512185131851418515185161851718518185191852018521185221852318524185251852618527185281852918530185311853218533185341853518536185371853818539185401854118542185431854418545185461854718548185491855018551185521855318554185551855618557185581855918560185611856218563185641856518566185671856818569185701857118572185731857418575185761857718578185791858018581185821858318584185851858618587185881858918590185911859218593185941859518596185971859818599186001860118602186031860418605186061860718608186091861018611186121861318614186151861618617186181861918620186211862218623186241862518626186271862818629186301863118632186331863418635186361863718638186391864018641186421864318644186451864618647186481864918650186511865218653186541865518656186571865818659186601866118662186631866418665186661866718668186691867018671186721867318674186751867618677186781867918680186811868218683186841868518686186871868818689186901869118692186931869418695186961869718698186991870018701187021870318704187051870618707187081870918710187111871218713187141871518716187171871818719187201872118722187231872418725187261872718728187291873018731187321873318734187351873618737187381873918740187411874218743187441874518746187471874818749187501875118752187531875418755187561875718758187591876018761187621876318764187651876618767187681876918770187711877218773187741877518776187771877818779187801878118782187831878418785187861878718788187891879018791187921879318794187951879618797187981879918800188011880218803188041880518806188071880818809188101881118812188131881418815188161881718818188191882018821188221882318824188251882618827188281882918830188311883218833188341883518836188371883818839188401884118842188431884418845188461884718848188491885018851188521885318854188551885618857188581885918860188611886218863188641886518866188671886818869188701887118872188731887418875188761887718878188791888018881188821888318884188851888618887188881888918890188911889218893188941889518896188971889818899189001890118902189031890418905189061890718908189091891018911189121891318914189151891618917189181891918920189211892218923189241892518926189271892818929189301893118932189331893418935189361893718938189391894018941189421894318944189451894618947189481894918950189511895218953189541895518956189571895818959189601896118962189631896418965189661896718968189691897018971189721897318974189751897618977189781897918980189811898218983189841898518986189871898818989189901899118992189931899418995189961899718998189991900019001190021900319004190051900619007190081900919010190111901219013190141901519016190171901819019190201902119022190231902419025190261902719028190291903019031190321903319034190351903619037190381903919040190411904219043190441904519046190471904819049190501905119052190531905419055190561905719058190591906019061190621906319064190651906619067190681906919070190711907219073190741907519076190771907819079190801908119082190831908419085190861908719088190891909019091190921909319094190951909619097190981909919100191011910219103191041910519106191071910819109191101911119112191131911419115191161911719118191191912019121191221912319124191251912619127191281912919130191311913219133191341913519136191371913819139191401914119142191431914419145191461914719148191491915019151191521915319154191551915619157191581915919160191611916219163191641916519166191671916819169191701917119172191731917419175191761917719178191791918019181191821918319184191851918619187191881918919190191911919219193191941919519196191971919819199192001920119202192031920419205192061920719208192091921019211192121921319214192151921619217192181921919220192211922219223192241922519226192271922819229192301923119232192331923419235192361923719238192391924019241192421924319244192451924619247192481924919250192511925219253192541925519256192571925819259192601926119262192631926419265192661926719268192691927019271192721927319274192751927619277192781927919280192811928219283192841928519286192871928819289192901929119292192931929419295192961929719298192991930019301193021930319304193051930619307193081930919310193111931219313193141931519316193171931819319193201932119322193231932419325193261932719328193291933019331193321933319334193351933619337193381933919340193411934219343193441934519346193471934819349193501935119352193531935419355193561935719358193591936019361193621936319364193651936619367193681936919370193711937219373193741937519376193771937819379193801938119382193831938419385193861938719388193891939019391193921939319394193951939619397193981939919400194011940219403194041940519406194071940819409194101941119412194131941419415194161941719418194191942019421194221942319424194251942619427194281942919430194311943219433194341943519436194371943819439194401944119442194431944419445194461944719448194491945019451194521945319454194551945619457194581945919460194611946219463194641946519466194671946819469194701947119472194731947419475194761947719478194791948019481194821948319484194851948619487194881948919490194911949219493194941949519496194971949819499195001950119502195031950419505195061950719508195091951019511195121951319514195151951619517195181951919520195211952219523195241952519526195271952819529195301953119532195331953419535195361953719538195391954019541195421954319544195451954619547195481954919550195511955219553195541955519556195571955819559195601956119562195631956419565195661956719568195691957019571195721957319574195751957619577195781957919580195811958219583195841958519586195871958819589195901959119592195931959419595195961959719598195991960019601196021960319604196051960619607196081960919610196111961219613196141961519616196171961819619196201962119622196231962419625196261962719628196291963019631196321963319634196351963619637196381963919640196411964219643196441964519646196471964819649196501965119652196531965419655196561965719658196591966019661196621966319664196651966619667196681966919670196711967219673196741967519676196771967819679196801968119682196831968419685196861968719688196891969019691196921969319694196951969619697196981969919700197011970219703197041970519706197071970819709197101971119712197131971419715197161971719718197191972019721197221972319724197251972619727197281972919730197311973219733197341973519736197371973819739197401974119742197431974419745197461974719748197491975019751197521975319754197551975619757197581975919760197611976219763197641976519766197671976819769197701977119772197731977419775197761977719778197791978019781197821978319784197851978619787197881978919790197911979219793197941979519796197971979819799198001980119802198031980419805198061980719808198091981019811198121981319814198151981619817198181981919820198211982219823198241982519826198271982819829198301983119832198331983419835198361983719838198391984019841198421984319844198451984619847198481984919850198511985219853198541985519856198571985819859198601986119862198631986419865198661986719868198691987019871198721987319874198751987619877198781987919880198811988219883198841988519886198871988819889198901989119892198931989419895198961989719898198991990019901199021990319904199051990619907199081990919910199111991219913199141991519916199171991819919199201992119922199231992419925199261992719928199291993019931199321993319934199351993619937199381993919940199411994219943199441994519946199471994819949199501995119952199531995419955199561995719958199591996019961199621996319964199651996619967199681996919970199711997219973199741997519976199771997819979199801998119982199831998419985199861998719988199891999019991199921999319994199951999619997199981999920000200012000220003200042000520006200072000820009200102001120012200132001420015200162001720018200192002020021200222002320024200252002620027200282002920030200312003220033200342003520036200372003820039200402004120042200432004420045200462004720048200492005020051200522005320054200552005620057200582005920060200612006220063200642006520066200672006820069200702007120072200732007420075200762007720078200792008020081200822008320084200852008620087200882008920090200912009220093200942009520096200972009820099201002010120102201032010420105201062010720108201092011020111201122011320114201152011620117201182011920120201212012220123201242012520126201272012820129201302013120132201332013420135201362013720138201392014020141201422014320144201452014620147201482014920150201512015220153201542015520156201572015820159201602016120162201632016420165201662016720168201692017020171201722017320174201752017620177201782017920180201812018220183201842018520186201872018820189201902019120192201932019420195201962019720198201992020020201202022020320204202052020620207202082020920210202112021220213202142021520216202172021820219202202022120222202232022420225202262022720228202292023020231202322023320234202352023620237202382023920240202412024220243202442024520246202472024820249202502025120252202532025420255202562025720258202592026020261202622026320264202652026620267202682026920270202712027220273202742027520276202772027820279202802028120282202832028420285202862028720288202892029020291202922029320294202952029620297202982029920300203012030220303203042030520306203072030820309203102031120312203132031420315203162031720318203192032020321203222032320324203252032620327203282032920330203312033220333203342033520336203372033820339203402034120342203432034420345203462034720348203492035020351203522035320354203552035620357203582035920360203612036220363203642036520366203672036820369203702037120372203732037420375203762037720378203792038020381203822038320384203852038620387203882038920390203912039220393203942039520396203972039820399204002040120402204032040420405204062040720408204092041020411204122041320414204152041620417204182041920420204212042220423204242042520426204272042820429204302043120432204332043420435204362043720438204392044020441204422044320444204452044620447204482044920450204512045220453204542045520456204572045820459204602046120462204632046420465204662046720468204692047020471204722047320474204752047620477204782047920480204812048220483204842048520486204872048820489204902049120492204932049420495204962049720498204992050020501205022050320504205052050620507205082050920510205112051220513205142051520516205172051820519205202052120522205232052420525205262052720528205292053020531205322053320534205352053620537205382053920540205412054220543205442054520546205472054820549205502055120552205532055420555205562055720558205592056020561205622056320564205652056620567205682056920570205712057220573205742057520576205772057820579205802058120582205832058420585205862058720588205892059020591205922059320594205952059620597205982059920600206012060220603206042060520606206072060820609206102061120612206132061420615206162061720618206192062020621206222062320624206252062620627206282062920630206312063220633206342063520636206372063820639206402064120642206432064420645206462064720648206492065020651206522065320654206552065620657206582065920660206612066220663206642066520666206672066820669206702067120672206732067420675206762067720678206792068020681206822068320684206852068620687206882068920690206912069220693206942069520696206972069820699207002070120702207032070420705207062070720708207092071020711207122071320714207152071620717207182071920720207212072220723207242072520726207272072820729207302073120732207332073420735207362073720738207392074020741207422074320744207452074620747207482074920750207512075220753207542075520756207572075820759207602076120762207632076420765207662076720768207692077020771207722077320774207752077620777207782077920780207812078220783207842078520786207872078820789207902079120792207932079420795207962079720798207992080020801208022080320804208052080620807208082080920810208112081220813208142081520816208172081820819208202082120822208232082420825208262082720828208292083020831208322083320834208352083620837208382083920840208412084220843208442084520846208472084820849208502085120852208532085420855208562085720858208592086020861208622086320864208652086620867208682086920870208712087220873208742087520876208772087820879208802088120882208832088420885208862088720888208892089020891208922089320894208952089620897208982089920900209012090220903209042090520906209072090820909209102091120912209132091420915209162091720918209192092020921209222092320924209252092620927209282092920930209312093220933209342093520936209372093820939209402094120942209432094420945209462094720948209492095020951209522095320954209552095620957209582095920960209612096220963209642096520966209672096820969209702097120972209732097420975209762097720978209792098020981209822098320984209852098620987209882098920990209912099220993209942099520996209972099820999210002100121002210032100421005210062100721008210092101021011210122101321014210152101621017210182101921020210212102221023210242102521026210272102821029210302103121032210332103421035210362103721038210392104021041210422104321044210452104621047210482104921050210512105221053210542105521056210572105821059210602106121062210632106421065210662106721068210692107021071210722107321074210752107621077210782107921080210812108221083210842108521086210872108821089210902109121092210932109421095210962109721098210992110021101211022110321104211052110621107211082110921110211112111221113211142111521116211172111821119211202112121122211232112421125211262112721128211292113021131211322113321134211352113621137211382113921140211412114221143211442114521146211472114821149211502115121152211532115421155211562115721158211592116021161211622116321164211652116621167211682116921170211712117221173211742117521176211772117821179211802118121182211832118421185211862118721188211892119021191211922119321194211952119621197211982119921200212012120221203212042120521206212072120821209212102121121212212132121421215212162121721218212192122021221212222122321224212252122621227212282122921230212312123221233212342123521236212372123821239212402124121242212432124421245212462124721248212492125021251212522125321254212552125621257212582125921260212612126221263212642126521266212672126821269212702127121272212732127421275212762127721278212792128021281212822128321284212852128621287212882128921290212912129221293212942129521296212972129821299213002130121302213032130421305213062130721308213092131021311213122131321314213152131621317213182131921320213212132221323213242132521326213272132821329213302133121332213332133421335213362133721338213392134021341213422134321344213452134621347213482134921350213512135221353213542135521356213572135821359213602136121362213632136421365213662136721368213692137021371213722137321374213752137621377213782137921380213812138221383213842138521386213872138821389213902139121392213932139421395213962139721398213992140021401214022140321404214052140621407214082140921410214112141221413214142141521416214172141821419214202142121422214232142421425214262142721428214292143021431214322143321434214352143621437214382143921440214412144221443214442144521446214472144821449214502145121452214532145421455214562145721458214592146021461214622146321464214652146621467214682146921470214712147221473214742147521476214772147821479214802148121482214832148421485214862148721488214892149021491214922149321494214952149621497214982149921500215012150221503215042150521506215072150821509215102151121512215132151421515215162151721518215192152021521215222152321524215252152621527215282152921530215312153221533215342153521536215372153821539215402154121542215432154421545215462154721548215492155021551215522155321554215552155621557215582155921560215612156221563215642156521566215672156821569215702157121572215732157421575215762157721578215792158021581215822158321584215852158621587215882158921590215912159221593215942159521596215972159821599216002160121602216032160421605216062160721608216092161021611216122161321614216152161621617216182161921620216212162221623216242162521626216272162821629216302163121632216332163421635216362163721638216392164021641216422164321644216452164621647216482164921650216512165221653216542165521656216572165821659216602166121662216632166421665216662166721668216692167021671216722167321674216752167621677216782167921680216812168221683216842168521686216872168821689216902169121692216932169421695216962169721698216992170021701217022170321704217052170621707217082170921710217112171221713217142171521716217172171821719217202172121722217232172421725217262172721728217292173021731217322173321734217352173621737217382173921740217412174221743217442174521746217472174821749217502175121752217532175421755217562175721758217592176021761217622176321764217652176621767217682176921770217712177221773217742177521776217772177821779217802178121782217832178421785217862178721788217892179021791217922179321794217952179621797217982179921800218012180221803218042180521806218072180821809218102181121812218132181421815218162181721818218192182021821218222182321824218252182621827218282182921830218312183221833218342183521836218372183821839218402184121842218432184421845218462184721848218492185021851218522185321854218552185621857218582185921860218612186221863218642186521866218672186821869218702187121872218732187421875218762187721878218792188021881218822188321884218852188621887218882188921890218912189221893218942189521896218972189821899219002190121902219032190421905219062190721908219092191021911219122191321914219152191621917219182191921920219212192221923219242192521926219272192821929219302193121932219332193421935219362193721938219392194021941219422194321944219452194621947219482194921950219512195221953219542195521956219572195821959219602196121962219632196421965219662196721968219692197021971219722197321974219752197621977219782197921980219812198221983219842198521986219872198821989219902199121992219932199421995219962199721998219992200022001220022200322004220052200622007220082200922010220112201222013220142201522016220172201822019220202202122022220232202422025220262202722028220292203022031220322203322034220352203622037220382203922040220412204222043220442204522046220472204822049220502205122052220532205422055220562205722058220592206022061220622206322064220652206622067220682206922070220712207222073220742207522076220772207822079220802208122082220832208422085220862208722088220892209022091220922209322094220952209622097220982209922100221012210222103221042210522106221072210822109221102211122112221132211422115221162211722118221192212022121221222212322124221252212622127221282212922130221312213222133221342213522136221372213822139221402214122142221432214422145221462214722148221492215022151221522215322154221552215622157221582215922160221612216222163221642216522166221672216822169221702217122172221732217422175221762217722178221792218022181221822218322184221852218622187221882218922190221912219222193221942219522196221972219822199222002220122202222032220422205222062220722208222092221022211222122221322214222152221622217222182221922220222212222222223222242222522226222272222822229222302223122232222332223422235222362223722238222392224022241222422224322244222452224622247222482224922250222512225222253222542225522256222572225822259222602226122262222632226422265222662226722268222692227022271222722227322274222752227622277222782227922280222812228222283222842228522286222872228822289222902229122292222932229422295222962229722298222992230022301223022230322304223052230622307223082230922310223112231222313223142231522316223172231822319223202232122322223232232422325223262232722328223292233022331223322233322334223352233622337223382233922340223412234222343223442234522346223472234822349223502235122352223532235422355223562235722358223592236022361223622236322364223652236622367223682236922370223712237222373223742237522376223772237822379223802238122382223832238422385223862238722388223892239022391223922239322394223952239622397223982239922400224012240222403224042240522406224072240822409224102241122412224132241422415224162241722418224192242022421224222242322424224252242622427224282242922430224312243222433224342243522436224372243822439224402244122442224432244422445224462244722448224492245022451224522245322454224552245622457224582245922460224612246222463224642246522466224672246822469224702247122472224732247422475224762247722478224792248022481224822248322484224852248622487224882248922490224912249222493224942249522496224972249822499225002250122502225032250422505225062250722508225092251022511225122251322514225152251622517225182251922520225212252222523225242252522526225272252822529225302253122532225332253422535225362253722538225392254022541225422254322544225452254622547225482254922550225512255222553225542255522556225572255822559225602256122562225632256422565225662256722568225692257022571225722257322574225752257622577225782257922580225812258222583225842258522586225872258822589225902259122592225932259422595225962259722598225992260022601226022260322604226052260622607226082260922610226112261222613226142261522616226172261822619226202262122622226232262422625226262262722628226292263022631226322263322634226352263622637226382263922640226412264222643226442264522646226472264822649226502265122652226532265422655226562265722658226592266022661226622266322664226652266622667226682266922670226712267222673226742267522676226772267822679226802268122682226832268422685226862268722688226892269022691226922269322694226952269622697226982269922700227012270222703227042270522706227072270822709227102271122712227132271422715227162271722718227192272022721227222272322724227252272622727227282272922730227312273222733227342273522736227372273822739227402274122742227432274422745227462274722748227492275022751227522275322754227552275622757227582275922760227612276222763227642276522766227672276822769227702277122772227732277422775227762277722778227792278022781227822278322784227852278622787227882278922790227912279222793227942279522796227972279822799228002280122802228032280422805228062280722808228092281022811228122281322814228152281622817228182281922820228212282222823228242282522826228272282822829228302283122832228332283422835228362283722838228392284022841228422284322844228452284622847228482284922850228512285222853228542285522856228572285822859228602286122862228632286422865228662286722868228692287022871228722287322874228752287622877228782287922880228812288222883228842288522886228872288822889228902289122892228932289422895228962289722898228992290022901229022290322904229052290622907229082290922910229112291222913229142291522916229172291822919229202292122922229232292422925229262292722928229292293022931229322293322934229352293622937229382293922940229412294222943229442294522946229472294822949229502295122952229532295422955229562295722958229592296022961229622296322964229652296622967229682296922970229712297222973229742297522976229772297822979229802298122982229832298422985229862298722988229892299022991229922299322994229952299622997229982299923000230012300223003230042300523006230072300823009230102301123012230132301423015230162301723018230192302023021230222302323024230252302623027230282302923030230312303223033230342303523036230372303823039230402304123042230432304423045230462304723048230492305023051230522305323054230552305623057230582305923060230612306223063230642306523066230672306823069230702307123072230732307423075230762307723078230792308023081230822308323084230852308623087230882308923090230912309223093230942309523096230972309823099231002310123102231032310423105231062310723108231092311023111231122311323114231152311623117231182311923120231212312223123231242312523126231272312823129231302313123132231332313423135231362313723138231392314023141231422314323144231452314623147231482314923150231512315223153231542315523156231572315823159231602316123162231632316423165231662316723168231692317023171231722317323174231752317623177231782317923180231812318223183231842318523186231872318823189231902319123192231932319423195231962319723198231992320023201232022320323204232052320623207232082320923210232112321223213232142321523216232172321823219232202322123222232232322423225232262322723228232292323023231232322323323234232352323623237232382323923240232412324223243232442324523246232472324823249232502325123252232532325423255232562325723258232592326023261232622326323264232652326623267232682326923270232712327223273232742327523276232772327823279232802328123282232832328423285232862328723288232892329023291232922329323294232952329623297232982329923300233012330223303233042330523306233072330823309233102331123312233132331423315233162331723318233192332023321233222332323324233252332623327233282332923330233312333223333233342333523336233372333823339233402334123342233432334423345233462334723348233492335023351233522335323354233552335623357233582335923360233612336223363233642336523366233672336823369233702337123372233732337423375233762337723378233792338023381233822338323384233852338623387233882338923390233912339223393233942339523396233972339823399234002340123402234032340423405234062340723408234092341023411234122341323414234152341623417234182341923420234212342223423234242342523426234272342823429234302343123432234332343423435234362343723438234392344023441234422344323444234452344623447234482344923450234512345223453234542345523456234572345823459234602346123462234632346423465234662346723468234692347023471234722347323474234752347623477234782347923480234812348223483234842348523486234872348823489234902349123492234932349423495234962349723498234992350023501235022350323504235052350623507235082350923510235112351223513235142351523516235172351823519235202352123522235232352423525235262352723528235292353023531235322353323534235352353623537235382353923540235412354223543235442354523546235472354823549235502355123552235532355423555235562355723558235592356023561235622356323564235652356623567235682356923570235712357223573235742357523576235772357823579235802358123582235832358423585235862358723588235892359023591235922359323594235952359623597235982359923600236012360223603236042360523606236072360823609236102361123612236132361423615236162361723618236192362023621236222362323624236252362623627236282362923630236312363223633236342363523636236372363823639236402364123642236432364423645236462364723648236492365023651236522365323654236552365623657236582365923660236612366223663236642366523666236672366823669236702367123672236732367423675236762367723678236792368023681236822368323684236852368623687236882368923690236912369223693236942369523696236972369823699237002370123702237032370423705237062370723708237092371023711237122371323714237152371623717237182371923720237212372223723237242372523726237272372823729237302373123732237332373423735237362373723738237392374023741237422374323744237452374623747237482374923750237512375223753237542375523756237572375823759237602376123762237632376423765237662376723768237692377023771237722377323774237752377623777237782377923780237812378223783237842378523786237872378823789237902379123792237932379423795237962379723798237992380023801238022380323804238052380623807238082380923810238112381223813238142381523816238172381823819238202382123822238232382423825238262382723828238292383023831238322383323834238352383623837238382383923840238412384223843238442384523846238472384823849238502385123852238532385423855238562385723858238592386023861238622386323864238652386623867238682386923870238712387223873238742387523876238772387823879238802388123882238832388423885238862388723888238892389023891238922389323894238952389623897238982389923900239012390223903239042390523906239072390823909239102391123912239132391423915239162391723918239192392023921239222392323924239252392623927239282392923930239312393223933239342393523936239372393823939239402394123942239432394423945239462394723948239492395023951239522395323954239552395623957239582395923960239612396223963239642396523966239672396823969239702397123972239732397423975239762397723978239792398023981239822398323984239852398623987239882398923990239912399223993239942399523996239972399823999240002400124002240032400424005240062400724008240092401024011240122401324014240152401624017240182401924020240212402224023240242402524026240272402824029240302403124032240332403424035240362403724038240392404024041240422404324044240452404624047240482404924050240512405224053240542405524056240572405824059240602406124062240632406424065240662406724068240692407024071240722407324074240752407624077240782407924080240812408224083240842408524086240872408824089240902409124092240932409424095240962409724098240992410024101241022410324104241052410624107241082410924110241112411224113241142411524116241172411824119241202412124122241232412424125241262412724128241292413024131241322413324134241352413624137241382413924140241412414224143241442414524146241472414824149241502415124152241532415424155241562415724158241592416024161241622416324164241652416624167241682416924170241712417224173241742417524176241772417824179241802418124182241832418424185241862418724188241892419024191241922419324194241952419624197241982419924200242012420224203242042420524206242072420824209242102421124212242132421424215242162421724218242192422024221242222422324224242252422624227242282422924230242312423224233242342423524236242372423824239242402424124242242432424424245242462424724248242492425024251242522425324254242552425624257242582425924260242612426224263242642426524266242672426824269242702427124272242732427424275242762427724278242792428024281242822428324284242852428624287242882428924290242912429224293242942429524296242972429824299243002430124302243032430424305243062430724308243092431024311243122431324314243152431624317243182431924320243212432224323243242432524326243272432824329243302433124332243332433424335243362433724338243392434024341243422434324344243452434624347243482434924350243512435224353243542435524356243572435824359243602436124362243632436424365243662436724368243692437024371243722437324374243752437624377243782437924380243812438224383243842438524386243872438824389243902439124392243932439424395243962439724398243992440024401244022440324404244052440624407244082440924410244112441224413244142441524416244172441824419244202442124422244232442424425244262442724428244292443024431244322443324434244352443624437244382443924440244412444224443244442444524446244472444824449244502445124452244532445424455244562445724458244592446024461244622446324464244652446624467244682446924470244712447224473244742447524476244772447824479244802448124482244832448424485244862448724488244892449024491244922449324494244952449624497244982449924500245012450224503245042450524506245072450824509245102451124512245132451424515245162451724518245192452024521245222452324524245252452624527245282452924530245312453224533245342453524536245372453824539245402454124542245432454424545245462454724548245492455024551245522455324554245552455624557245582455924560245612456224563245642456524566245672456824569245702457124572245732457424575245762457724578245792458024581245822458324584245852458624587245882458924590245912459224593245942459524596245972459824599246002460124602246032460424605246062460724608246092461024611246122461324614246152461624617246182461924620246212462224623246242462524626246272462824629246302463124632246332463424635246362463724638246392464024641246422464324644246452464624647246482464924650246512465224653246542465524656246572465824659246602466124662246632466424665246662466724668246692467024671246722467324674246752467624677246782467924680246812468224683246842468524686246872468824689246902469124692246932469424695246962469724698246992470024701247022470324704247052470624707247082470924710247112471224713247142471524716247172471824719247202472124722247232472424725247262472724728247292473024731247322473324734247352473624737247382473924740247412474224743247442474524746247472474824749247502475124752247532475424755247562475724758247592476024761247622476324764247652476624767247682476924770247712477224773247742477524776247772477824779247802478124782247832478424785247862478724788247892479024791247922479324794247952479624797247982479924800248012480224803248042480524806248072480824809248102481124812248132481424815248162481724818248192482024821248222482324824248252482624827248282482924830248312483224833248342483524836248372483824839248402484124842248432484424845248462484724848248492485024851248522485324854248552485624857248582485924860248612486224863248642486524866248672486824869248702487124872248732487424875248762487724878248792488024881248822488324884248852488624887248882488924890248912489224893248942489524896248972489824899249002490124902249032490424905249062490724908249092491024911249122491324914249152491624917249182491924920249212492224923249242492524926249272492824929249302493124932249332493424935249362493724938249392494024941249422494324944249452494624947249482494924950249512495224953249542495524956249572495824959249602496124962249632496424965249662496724968249692497024971249722497324974249752497624977249782497924980249812498224983249842498524986249872498824989249902499124992249932499424995249962499724998249992500025001250022500325004250052500625007250082500925010250112501225013250142501525016250172501825019250202502125022250232502425025250262502725028250292503025031250322503325034250352503625037250382503925040250412504225043250442504525046250472504825049250502505125052250532505425055250562505725058250592506025061250622506325064250652506625067250682506925070250712507225073250742507525076250772507825079250802508125082250832508425085250862508725088250892509025091250922509325094250952509625097250982509925100251012510225103251042510525106251072510825109251102511125112251132511425115251162511725118251192512025121251222512325124251252512625127251282512925130251312513225133251342513525136251372513825139251402514125142251432514425145251462514725148251492515025151251522515325154251552515625157251582515925160251612516225163251642516525166251672516825169
  1. ;;; org.el --- Outline-based notes management and organizer -*- lexical-binding: t; -*-
  2. ;; Carstens outline-mode for keeping track of everything.
  3. ;; Copyright (C) 2004-2016 Free Software Foundation, Inc.
  4. ;;
  5. ;; Author: Carsten Dominik <carsten at orgmode dot org>
  6. ;; Maintainer: Carsten Dominik <carsten at orgmode dot org>
  7. ;; Keywords: outlines, hypermedia, calendar, wp
  8. ;; Homepage: http://orgmode.org
  9. ;;
  10. ;; This file is part of GNU Emacs.
  11. ;;
  12. ;; GNU Emacs is free software: you can redistribute it and/or modify
  13. ;; it under the terms of the GNU General Public License as published by
  14. ;; the Free Software Foundation, either version 3 of the License, or
  15. ;; (at your option) any later version.
  16. ;; GNU Emacs is distributed in the hope that it will be useful,
  17. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. ;; GNU General Public License for more details.
  20. ;; You should have received a copy of the GNU General Public License
  21. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  22. ;;
  23. ;;; Commentary:
  24. ;;
  25. ;; Org-mode is a mode for keeping notes, maintaining ToDo lists, and doing
  26. ;; project planning with a fast and effective plain-text system.
  27. ;;
  28. ;; Org-mode develops organizational tasks around NOTES files that contain
  29. ;; information about projects as plain text. Org-mode is implemented on
  30. ;; top of outline-mode, which makes it possible to keep the content of
  31. ;; large files well structured. Visibility cycling and structure editing
  32. ;; help to work with the tree. Tables are easily created with a built-in
  33. ;; table editor. Org-mode supports ToDo items, deadlines, time stamps,
  34. ;; and scheduling. It dynamically compiles entries into an agenda that
  35. ;; utilizes and smoothly integrates much of the Emacs calendar and diary.
  36. ;; Plain text URL-like links connect to websites, emails, Usenet
  37. ;; messages, BBDB entries, and any files related to the projects. For
  38. ;; printing and sharing of notes, an Org-mode file can be exported as a
  39. ;; structured ASCII file, as HTML, or (todo and agenda items only) as an
  40. ;; iCalendar file. It can also serve as a publishing tool for a set of
  41. ;; linked webpages.
  42. ;;
  43. ;; Installation and Activation
  44. ;; ---------------------------
  45. ;; See the corresponding sections in the manual at
  46. ;;
  47. ;; http://orgmode.org/org.html#Installation
  48. ;;
  49. ;; Documentation
  50. ;; -------------
  51. ;; The documentation of Org-mode can be found in the TeXInfo file. The
  52. ;; distribution also contains a PDF version of it. At the homepage of
  53. ;; Org-mode, you can read the same text online as HTML. There is also an
  54. ;; excellent reference card made by Philip Rooke. This card can be found
  55. ;; in the etc/ directory of Emacs 22.
  56. ;;
  57. ;; A list of recent changes can be found at
  58. ;; http://orgmode.org/Changes.html
  59. ;;
  60. ;;; Code:
  61. (defvar org-inhibit-highlight-removal nil) ; dynamically scoped param
  62. (defvar-local org-table-formula-constants-local nil
  63. "Local version of `org-table-formula-constants'.")
  64. ;;;; Require other packages
  65. (require 'cl-lib)
  66. (eval-when-compile (require 'gnus-sum))
  67. (require 'calendar)
  68. (require 'find-func)
  69. (require 'format-spec)
  70. (or (equal this-command 'eval-buffer)
  71. (condition-case nil
  72. (load (concat (file-name-directory load-file-name)
  73. "org-loaddefs.el")
  74. nil t t t)
  75. (error
  76. (message "WARNING: No org-loaddefs.el file could be found from where org.el is loaded.")
  77. (sit-for 3)
  78. (message "You need to run \"make\" or \"make autoloads\" from Org lisp directory")
  79. (sit-for 3))))
  80. (require 'org-macs)
  81. (require 'org-compat)
  82. ;; `org-outline-regexp' ought to be a defconst but is let-bound in
  83. ;; some places -- e.g. see the macro `org-with-limited-levels'.
  84. ;;
  85. ;; In Org buffers, the value of `outline-regexp' is that of
  86. ;; `org-outline-regexp'. The only function still directly relying on
  87. ;; `outline-regexp' is `org-overview' so that `org-cycle' can do its
  88. ;; job when `orgstruct-mode' is active.
  89. (defvar org-outline-regexp "\\*+ "
  90. "Regexp to match Org headlines.")
  91. (defvar org-outline-regexp-bol "^\\*+ "
  92. "Regexp to match Org headlines.
  93. This is similar to `org-outline-regexp' but additionally makes
  94. sure that we are at the beginning of the line.")
  95. (defvar org-heading-regexp "^\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  96. "Matches a headline, putting stars and text into groups.
  97. Stars are put in group 1 and the trimmed body in group 2.")
  98. (declare-function calendar-check-holidays "holidays" (&optional date))
  99. (declare-function cdlatex-environment "ext:cdlatex" (environment item))
  100. (declare-function org-add-archive-files "org-archive" (files))
  101. (declare-function org-agenda-entry-get-agenda-timestamp "org-agenda" (pom))
  102. (declare-function org-agenda-list "org-agenda" (&optional arg start-day span))
  103. (declare-function org-agenda-redo "org-agenda" (&optional all))
  104. (declare-function org-babel-do-in-edit-buffer "ob-core" (&rest body))
  105. (declare-function org-babel-tangle-file "ob-tangle" (file &optional target-file lang))
  106. (declare-function org-beamer-mode "ox-beamer" ())
  107. (declare-function org-clock-get-last-clock-out-time "org-clock" ())
  108. (declare-function org-clock-out "org-clock" (&optional switch-to-state fail-quietly at-time))
  109. (declare-function org-clock-remove-overlays "org-clock" (&optional beg end noremove))
  110. (declare-function org-clock-sum "org-clock" (&optional tstart tend headline-filter propname))
  111. (declare-function org-clock-sum-current-item "org-clock" (&optional tstart))
  112. (declare-function org-clock-timestamps-down "org-clock" (&optional n))
  113. (declare-function org-clock-timestamps-up "org-clock" (&optional n))
  114. (declare-function org-clock-update-time-maybe "org-clock" ())
  115. (declare-function org-clocktable-shift "org-clock" (dir n))
  116. (declare-function org-element-at-point "org-element" ())
  117. (declare-function org-element-cache-refresh "org-element" (pos))
  118. (declare-function org-element-cache-reset "org-element" (&optional all))
  119. (declare-function org-element-contents "org-element" (element))
  120. (declare-function org-element-context "org-element" (&optional element))
  121. (declare-function org-element-copy "org-element" (datum))
  122. (declare-function org-element-interpret-data "org-element" (data &optional parent))
  123. (declare-function org-element-lineage "org-element" (blob &optional types with-self))
  124. (declare-function org-element-nested-p "org-element" (elem-a elem-b))
  125. (declare-function org-element-parse-buffer "org-element" (&optional granularity visible-only))
  126. (declare-function org-element-property "org-element" (property element))
  127. (declare-function org-element-put-property "org-element" (element property value))
  128. (declare-function org-element-swap-A-B "org-element" (elem-a elem-b))
  129. (declare-function org-element-type "org-element" (element))
  130. (declare-function org-element-update-syntax "org-element" ())
  131. (declare-function org-id-find-id-file "org-id" (id))
  132. (declare-function org-id-get-create "org-id" (&optional force))
  133. (declare-function org-inlinetask-at-task-p "org-inlinetask" ())
  134. (declare-function org-inlinetask-outline-regexp "org-inlinetask" ())
  135. (declare-function org-inlinetask-toggle-visibility "org-inlinetask" ())
  136. (declare-function org-plot/gnuplot "org-plot" (&optional params))
  137. (declare-function org-pop-to-buffer-same-window "org-compat" (&optional buffer-or-name norecord label))
  138. (declare-function org-table-align "org-table" ())
  139. (declare-function org-table-begin "org-table" (&optional table-type))
  140. (declare-function org-table-beginning-of-field "org-table" (&optional n))
  141. (declare-function org-table-blank-field "org-table" ())
  142. (declare-function org-table-calc-current-TBLFM "org-table" (&optional arg))
  143. (declare-function org-table-edit-field "org-table" (arg))
  144. (declare-function org-table-end "org-table" (&optional table-type))
  145. (declare-function org-table-end-of-field "org-table" (&optional n))
  146. (declare-function org-table-insert-row "org-table" (&optional arg))
  147. (declare-function org-table-justify-field-maybe "org-table" (&optional new))
  148. (declare-function org-table-maybe-eval-formula "org-table" ())
  149. (declare-function org-table-maybe-recalculate-line "org-table" ())
  150. (declare-function org-table-next-row "org-table" ())
  151. (declare-function org-table-paste-rectangle "org-table" ())
  152. (declare-function org-table-wrap-region "org-table" (arg))
  153. (declare-function org-tags-view "org-agenda" (&optional todo-only match))
  154. (declare-function orgtbl-ascii-plot "org-table" (&optional ask))
  155. (declare-function orgtbl-mode "org-table" (&optional arg))
  156. (defsubst org-uniquify (list)
  157. "Non-destructively remove duplicate elements from LIST."
  158. (let ((res (copy-sequence list))) (delete-dups res)))
  159. (defsubst org-get-at-bol (property)
  160. "Get text property PROPERTY at the beginning of line."
  161. (get-text-property (point-at-bol) property))
  162. (defsubst org-trim (s)
  163. "Remove whitespace at the beginning and the end of string S."
  164. (replace-regexp-in-string
  165. "\\`[ \t\n\r]+" ""
  166. (replace-regexp-in-string "[ \t\n\r]+\\'" "" s)))
  167. ;; load languages based on value of `org-babel-load-languages'
  168. (defvar org-babel-load-languages)
  169. ;;;###autoload
  170. (defun org-babel-do-load-languages (sym value)
  171. "Load the languages defined in `org-babel-load-languages'."
  172. (set-default sym value)
  173. (dolist (pair org-babel-load-languages)
  174. (let ((active (cdr pair)) (lang (symbol-name (car pair))))
  175. (if active
  176. (require (intern (concat "ob-" lang)))
  177. (funcall 'fmakunbound
  178. (intern (concat "org-babel-execute:" lang)))
  179. (funcall 'fmakunbound
  180. (intern (concat "org-babel-expand-body:" lang)))))))
  181. (declare-function org-babel-tangle-file "ob-tangle" (file &optional target-file lang))
  182. ;;;###autoload
  183. (defun org-babel-load-file (file &optional compile)
  184. "Load Emacs Lisp source code blocks in the Org-mode FILE.
  185. This function exports the source code using `org-babel-tangle'
  186. and then loads the resulting file using `load-file'. With prefix
  187. arg (noninteractively: 2nd arg) COMPILE the tangled Emacs Lisp
  188. file to byte-code before it is loaded."
  189. (interactive "fFile to load: \nP")
  190. (let* ((age (lambda (file)
  191. (float-time
  192. (time-subtract (current-time)
  193. (nth 5 (or (file-attributes (file-truename file))
  194. (file-attributes file)))))))
  195. (base-name (file-name-sans-extension file))
  196. (exported-file (concat base-name ".el")))
  197. ;; tangle if the org-mode file is newer than the elisp file
  198. (unless (and (file-exists-p exported-file)
  199. (> (funcall age file) (funcall age exported-file)))
  200. ;; Tangle-file traversal returns reversed list of tangled files
  201. ;; and we want to evaluate the first target.
  202. (setq exported-file
  203. (car (last (org-babel-tangle-file file exported-file "emacs-lisp")))))
  204. (message "%s %s"
  205. (if compile
  206. (progn (byte-compile-file exported-file 'load)
  207. "Compiled and loaded")
  208. (progn (load-file exported-file) "Loaded"))
  209. exported-file)))
  210. (defcustom org-babel-load-languages '((emacs-lisp . t))
  211. "Languages which can be evaluated in Org-mode buffers.
  212. This list can be used to load support for any of the languages
  213. below, note that each language will depend on a different set of
  214. system executables and/or Emacs modes. When a language is
  215. \"loaded\", then code blocks in that language can be evaluated
  216. with `org-babel-execute-src-block' bound by default to C-c
  217. C-c (note the `org-babel-no-eval-on-ctrl-c-ctrl-c' variable can
  218. be set to remove code block evaluation from the C-c C-c
  219. keybinding. By default only Emacs Lisp (which has no
  220. requirements) is loaded."
  221. :group 'org-babel
  222. :set 'org-babel-do-load-languages
  223. :version "24.1"
  224. :type '(alist :tag "Babel Languages"
  225. :key-type
  226. (choice
  227. (const :tag "Awk" awk)
  228. (const :tag "C" C)
  229. (const :tag "R" R)
  230. (const :tag "Asymptote" asymptote)
  231. (const :tag "Calc" calc)
  232. (const :tag "Clojure" clojure)
  233. (const :tag "CSS" css)
  234. (const :tag "Ditaa" ditaa)
  235. (const :tag "Dot" dot)
  236. (const :tag "Emacs Lisp" emacs-lisp)
  237. (const :tag "Forth" forth)
  238. (const :tag "Fortran" fortran)
  239. (const :tag "Gnuplot" gnuplot)
  240. (const :tag "Haskell" haskell)
  241. (const :tag "IO" io)
  242. (const :tag "J" J)
  243. (const :tag "Java" java)
  244. (const :tag "Javascript" js)
  245. (const :tag "LaTeX" latex)
  246. (const :tag "Ledger" ledger)
  247. (const :tag "Lilypond" lilypond)
  248. (const :tag "Lisp" lisp)
  249. (const :tag "Makefile" makefile)
  250. (const :tag "Maxima" maxima)
  251. (const :tag "Matlab" matlab)
  252. (const :tag "Mscgen" mscgen)
  253. (const :tag "Ocaml" ocaml)
  254. (const :tag "Octave" octave)
  255. (const :tag "Org" org)
  256. (const :tag "Perl" perl)
  257. (const :tag "Pico Lisp" picolisp)
  258. (const :tag "PlantUML" plantuml)
  259. (const :tag "Python" python)
  260. (const :tag "Ruby" ruby)
  261. (const :tag "Sass" sass)
  262. (const :tag "Scala" scala)
  263. (const :tag "Scheme" scheme)
  264. (const :tag "Screen" screen)
  265. (const :tag "Shell Script" shell)
  266. (const :tag "Shen" shen)
  267. (const :tag "Sql" sql)
  268. (const :tag "Sqlite" sqlite)
  269. (const :tag "Stan" stan)
  270. (const :tag "ebnf2ps" ebnf2ps))
  271. :value-type (boolean :tag "Activate" :value t)))
  272. ;;;; Customization variables
  273. (defcustom org-clone-delete-id nil
  274. "Remove ID property of clones of a subtree.
  275. When non-nil, clones of a subtree don't inherit the ID property.
  276. Otherwise they inherit the ID property with a new unique
  277. identifier."
  278. :type 'boolean
  279. :version "24.1"
  280. :group 'org-id)
  281. ;;; Version
  282. (org-check-version)
  283. ;;;###autoload
  284. (defun org-version (&optional here full message)
  285. "Show the org-mode version.
  286. Interactively, or when MESSAGE is non-nil, show it in echo area.
  287. With prefix argument, or when HERE is non-nil, insert it at point.
  288. In non-interactive uses, a reduced version string is output unless
  289. FULL is given."
  290. (interactive (list current-prefix-arg t (not current-prefix-arg)))
  291. (let ((org-dir (ignore-errors (org-find-library-dir "org")))
  292. (save-load-suffixes (when (boundp 'load-suffixes) load-suffixes))
  293. (load-suffixes (list ".el"))
  294. (org-install-dir
  295. (ignore-errors (org-find-library-dir "org-loaddefs"))))
  296. (unless (and (fboundp 'org-release) (fboundp 'org-git-version))
  297. (org-load-noerror-mustsuffix (concat org-dir "org-version")))
  298. (let* ((load-suffixes save-load-suffixes)
  299. (release (org-release))
  300. (git-version (org-git-version))
  301. (version (format "Org-mode version %s (%s @ %s)"
  302. release
  303. git-version
  304. (if org-install-dir
  305. (if (string= org-dir org-install-dir)
  306. org-install-dir
  307. (concat "mixed installation! "
  308. org-install-dir
  309. " and "
  310. org-dir))
  311. "org-loaddefs.el can not be found!")))
  312. (version1 (if full version release)))
  313. (when here (insert version1))
  314. (when message (message "%s" version1))
  315. version1)))
  316. (defconst org-version (org-version))
  317. ;;; Syntax Constants
  318. ;;;; Block
  319. (defconst org-block-regexp
  320. "^[ \t]*#\\+begin_?\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_?\\1[ \t]*$"
  321. "Regular expression for hiding blocks.")
  322. (defconst org-dblock-start-re
  323. "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  324. "Matches the start line of a dynamic block, with parameters.")
  325. (defconst org-dblock-end-re "^[ \t]*#\\+\\(?:END\\|end\\)\\([: \t\r\n]\\|$\\)"
  326. "Matches the end of a dynamic block.")
  327. ;;;; Clock and Planning
  328. (defconst org-clock-string "CLOCK:"
  329. "String used as prefix for timestamps clocking work hours on an item.")
  330. (defvar org-closed-string "CLOSED:"
  331. "String used as the prefix for timestamps logging closing a TODO entry.")
  332. (defvar org-deadline-string "DEADLINE:"
  333. "String to mark deadline entries.
  334. A deadline is this string, followed by a time stamp. Should be a word,
  335. terminated by a colon. You can insert a schedule keyword and
  336. a timestamp with \\[org-deadline].")
  337. (defvar org-scheduled-string "SCHEDULED:"
  338. "String to mark scheduled TODO entries.
  339. A schedule is this string, followed by a time stamp. Should be a word,
  340. terminated by a colon. You can insert a schedule keyword and
  341. a timestamp with \\[org-schedule].")
  342. (defconst org-ds-keyword-length
  343. (+ 2
  344. (apply #'max
  345. (mapcar #'length
  346. (list org-deadline-string org-scheduled-string
  347. org-clock-string org-closed-string))))
  348. "Maximum length of the DEADLINE and SCHEDULED keywords.")
  349. (defconst org-planning-line-re
  350. (concat "^[ \t]*"
  351. (regexp-opt
  352. (list org-closed-string org-deadline-string org-scheduled-string)
  353. t))
  354. "Matches a line with planning info.
  355. Matched keyword is in group 1.")
  356. (defconst org-clock-line-re
  357. (concat "^[ \t]*" org-clock-string)
  358. "Matches a line with clock info.")
  359. (defconst org-deadline-regexp (concat "\\<" org-deadline-string)
  360. "Matches the DEADLINE keyword.")
  361. (defconst org-deadline-time-regexp
  362. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  363. "Matches the DEADLINE keyword together with a time stamp.")
  364. (defconst org-deadline-time-hour-regexp
  365. (concat "\\<" org-deadline-string
  366. " *<\\([^>]+[0-9]\\{1,2\\}:[0-9]\\{2\\}[0-9-+:hdwmy \t.]*\\)>")
  367. "Matches the DEADLINE keyword together with a time-and-hour stamp.")
  368. (defconst org-deadline-line-regexp
  369. (concat "\\<\\(" org-deadline-string "\\).*")
  370. "Matches the DEADLINE keyword and the rest of the line.")
  371. (defconst org-scheduled-regexp (concat "\\<" org-scheduled-string)
  372. "Matches the SCHEDULED keyword.")
  373. (defconst org-scheduled-time-regexp
  374. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  375. "Matches the SCHEDULED keyword together with a time stamp.")
  376. (defconst org-scheduled-time-hour-regexp
  377. (concat "\\<" org-scheduled-string
  378. " *<\\([^>]+[0-9]\\{1,2\\}:[0-9]\\{2\\}[0-9-+:hdwmy \t.]*\\)>")
  379. "Matches the SCHEDULED keyword together with a time-and-hour stamp.")
  380. (defconst org-closed-time-regexp
  381. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  382. "Matches the CLOSED keyword together with a time stamp.")
  383. (defconst org-keyword-time-regexp
  384. (concat "\\<"
  385. (regexp-opt
  386. (list org-scheduled-string org-deadline-string org-closed-string
  387. org-clock-string)
  388. t)
  389. " *[[<]\\([^]>]+\\)[]>]")
  390. "Matches any of the 4 keywords, together with the time stamp.")
  391. (defconst org-keyword-time-not-clock-regexp
  392. (concat
  393. "\\<"
  394. (regexp-opt
  395. (list org-scheduled-string org-deadline-string org-closed-string) t)
  396. " *[[<]\\([^]>]+\\)[]>]")
  397. "Matches any of the 3 keywords, together with the time stamp.")
  398. (defconst org-maybe-keyword-time-regexp
  399. (concat "\\(\\<"
  400. (regexp-opt
  401. (list org-scheduled-string org-deadline-string org-closed-string
  402. org-clock-string)
  403. t)
  404. "\\)?"
  405. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?[]>]"
  406. "\\|"
  407. "<%%([^\r\n>]*>\\)")
  408. "Matches a timestamp, possibly preceded by a keyword.")
  409. (defconst org-all-time-keywords
  410. (mapcar (lambda (w) (substring w 0 -1))
  411. (list org-scheduled-string org-deadline-string
  412. org-clock-string org-closed-string))
  413. "List of time keywords.")
  414. ;;;; Drawer
  415. (defconst org-drawer-regexp "^[ \t]*:\\(\\(?:\\w\\|[-_]\\)+\\):[ \t]*$"
  416. "Matches first or last line of a hidden block.
  417. Group 1 contains drawer's name or \"END\".")
  418. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  419. "Regular expression matching the first line of a property drawer.")
  420. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  421. "Regular expression matching the last line of a property drawer.")
  422. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  423. "Regular expression matching the first line of a clock drawer.")
  424. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  425. "Regular expression matching the last line of a clock drawer.")
  426. (defconst org-property-drawer-re
  427. (concat "^[ \t]*:PROPERTIES:[ \t]*\n"
  428. "\\(?:[ \t]*:\\S-+:\\(?: .*\\)?[ \t]*\n\\)*?"
  429. "[ \t]*:END:[ \t]*$")
  430. "Matches an entire property drawer.")
  431. (defconst org-clock-drawer-re
  432. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*?\\("
  433. org-clock-drawer-end-re "\\)\n?")
  434. "Matches an entire clock drawer.")
  435. ;;;; Headline
  436. (defconst org-heading-keyword-regexp-format
  437. "^\\(\\*+\\)\\(?: +%s\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  438. "Printf format for a regexp matching a headline with some keyword.
  439. This regexp will match the headline of any node which has the
  440. exact keyword that is put into the format. The keyword isn't in
  441. any group by default, but the stars and the body are.")
  442. (defconst org-heading-keyword-maybe-regexp-format
  443. "^\\(\\*+\\)\\(?: +%s\\)?\\(?: +\\(.*?\\)\\)?[ \t]*$"
  444. "Printf format for a regexp matching a headline, possibly with some keyword.
  445. This regexp can match any headline with the specified keyword, or
  446. without a keyword. The keyword isn't in any group by default,
  447. but the stars and the body are.")
  448. (defconst org-archive-tag "ARCHIVE"
  449. "The tag that marks a subtree as archived.
  450. An archived subtree does not open during visibility cycling, and does
  451. not contribute to the agenda listings.")
  452. (defconst org-comment-string "COMMENT"
  453. "Entries starting with this keyword will never be exported.
  454. An entry can be toggled between COMMENT and normal with
  455. \\[org-toggle-comment].")
  456. ;;;; LaTeX Environments and Fragments
  457. (defconst org-latex-regexps
  458. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  459. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  460. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  461. ("$1" "\\([^$]\\|^\\)\\(\\$[^ \r\n,;.$]\\$\\)\\(\\s.\\|\\s-\\|\\s(\\|\\s)\\|\\s\"\\|\000\\|$\\)" 2 nil)
  462. ("$" "\\([^$]\\|^\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\(\\s.\\|\\s-\\|\\s(\\|\\s)\\|\\s\"\\|\000\\|$\\)" 2 nil)
  463. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  464. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
  465. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
  466. "Regular expressions for matching embedded LaTeX.")
  467. ;;;; Node Property
  468. (defconst org-effort-property "Effort"
  469. "The property that is being used to keep track of effort estimates.
  470. Effort estimates given in this property need to have the format H:MM.")
  471. ;;;; Table
  472. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  473. "Detect an org-type or table-type table.")
  474. (defconst org-table-line-regexp "^[ \t]*|"
  475. "Detect an org-type table line.")
  476. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  477. "Detect an org-type table line.")
  478. (defconst org-table-hline-regexp "^[ \t]*|-"
  479. "Detect an org-type table hline.")
  480. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  481. "Detect a table-type table hline.")
  482. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  483. "Detect the first line outside a table when searching from within it.
  484. This works for both table types.")
  485. (defconst org-TBLFM-regexp "^[ \t]*#\\+TBLFM: "
  486. "Detect a #+TBLFM line.")
  487. ;;;; Timestamp
  488. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^\r\n>]*?\\)>"
  489. "Regular expression for fast time stamp matching.")
  490. (defconst org-ts-regexp-inactive
  491. "\\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^\r\n>]*?\\)\\]"
  492. "Regular expression for fast inactive time stamp matching.")
  493. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?\\)[]>]"
  494. "Regular expression for fast time stamp matching.")
  495. (defconst org-ts-regexp0
  496. "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\)\\( +[^]+0-9>\r\n -]+\\)?\\( +\\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  497. "Regular expression matching time strings for analysis.
  498. This one does not require the space after the date, so it can be used
  499. on a string that terminates immediately after the date.")
  500. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]+0-9>\r\n -]*\\)\\( \\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  501. "Regular expression matching time strings for analysis.")
  502. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  503. "Regular expression matching time stamps, with groups.")
  504. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  505. "Regular expression matching time stamps (also [..]), with groups.")
  506. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  507. "Regular expression matching a time stamp range.")
  508. (defconst org-tr-regexp-both
  509. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  510. "Regular expression matching a time stamp range.")
  511. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  512. org-ts-regexp "\\)?")
  513. "Regular expression matching a time stamp or time stamp range.")
  514. (defconst org-tsr-regexp-both
  515. (concat org-ts-regexp-both "\\(--?-?"
  516. org-ts-regexp-both "\\)?")
  517. "Regular expression matching a time stamp or time stamp range.
  518. The time stamps may be either active or inactive.")
  519. (defconst org-repeat-re
  520. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*?\\([.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)"
  521. "Regular expression for specifying repeated events.
  522. After a match, group 1 contains the repeat expression.")
  523. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  524. "Formats for `format-time-string' which are used for time stamps.")
  525. ;;; The custom variables
  526. (defgroup org nil
  527. "Outline-based notes management and organizer."
  528. :tag "Org"
  529. :group 'outlines
  530. :group 'calendar)
  531. (defcustom org-mode-hook nil
  532. "Mode hook for Org-mode, run after the mode was turned on."
  533. :group 'org
  534. :type 'hook)
  535. (defcustom org-load-hook nil
  536. "Hook that is run after org.el has been loaded."
  537. :group 'org
  538. :type 'hook)
  539. (defcustom org-log-buffer-setup-hook nil
  540. "Hook that is run after an Org log buffer is created."
  541. :group 'org
  542. :version "24.1"
  543. :type 'hook)
  544. (defvar org-modules) ; defined below
  545. (defvar org-modules-loaded nil
  546. "Have the modules been loaded already?")
  547. (defun org-load-modules-maybe (&optional force)
  548. "Load all extensions listed in `org-modules'."
  549. (when (or force (not org-modules-loaded))
  550. (dolist (ext org-modules)
  551. (condition-case nil (require ext)
  552. (error (message "Problems while trying to load feature `%s'" ext))))
  553. (setq org-modules-loaded t)))
  554. (defun org-set-modules (var value)
  555. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  556. (set var value)
  557. (when (featurep 'org)
  558. (org-load-modules-maybe 'force)
  559. (org-element-cache-reset 'all)))
  560. (defcustom org-modules '(org-w3m org-bbdb org-bibtex org-docview org-gnus org-info org-irc org-mhe org-rmail)
  561. "Modules that should always be loaded together with org.el.
  562. If a description starts with <C>, the file is not part of Emacs
  563. and loading it will require that you have downloaded and properly
  564. installed the Org mode distribution.
  565. You can also use this system to load external packages (i.e. neither Org
  566. core modules, nor modules from the CONTRIB directory). Just add symbols
  567. to the end of the list. If the package is called org-xyz.el, then you need
  568. to add the symbol `xyz', and the package must have a call to:
  569. (provide \\='org-xyz)
  570. For export specific modules, see also `org-export-backends'."
  571. :group 'org
  572. :set 'org-set-modules
  573. :version "24.4"
  574. :package-version '(Org . "8.0")
  575. :type
  576. '(set :greedy t
  577. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  578. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  579. (const :tag " crypt: Encryption of subtrees" org-crypt)
  580. (const :tag " ctags: Access to Emacs tags with links" org-ctags)
  581. (const :tag " docview: Links to doc-view buffers" org-docview)
  582. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  583. (const :tag " habit: Track your consistency with habits" org-habit)
  584. (const :tag " id: Global IDs for identifying entries" org-id)
  585. (const :tag " info: Links to Info nodes" org-info)
  586. (const :tag " inlinetask: Tasks independent of outline hierarchy" org-inlinetask)
  587. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  588. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  589. (const :tag " mouse: Additional mouse support" org-mouse)
  590. (const :tag " protocol: Intercept calls from emacsclient" org-protocol)
  591. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  592. (const :tag " w3m: Special cut/paste from w3m to Org-mode." org-w3m)
  593. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  594. (const :tag "C bookmark: Org-mode links to bookmarks" org-bookmark)
  595. (const :tag "C bullets: Add overlays to headlines stars" org-bullets)
  596. (const :tag "C checklist: Extra functions for checklists in repeated tasks" org-checklist)
  597. (const :tag "C choose: Use TODO keywords to mark decisions states" org-choose)
  598. (const :tag "C collector: Collect properties into tables" org-collector)
  599. (const :tag "C depend: TODO dependencies for Org-mode\n\t\t\t(PARTIALLY OBSOLETE, see built-in dependency support))" org-depend)
  600. (const :tag "C drill: Flashcards and spaced repetition for Org-mode" org-drill)
  601. (const :tag "C elisp-symbol: Org-mode links to emacs-lisp symbols" org-elisp-symbol)
  602. (const :tag "C eshell Support for links to working directories in eshell" org-eshell)
  603. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  604. (const :tag "C eval: Include command output as text" org-eval)
  605. (const :tag "C eww: Store link to url of eww" org-eww)
  606. (const :tag "C expiry: Expiry mechanism for Org-mode entries" org-expiry)
  607. (const :tag "C favtable: Lookup table of favorite references and links" org-favtable)
  608. (const :tag "C git-link: Provide org links to specific file version" org-git-link)
  609. (const :tag "C interactive-query: Interactive modification of tags query\n\t\t\t(PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query)
  610. (const :tag "C invoice: Help manage client invoices in Org-mode" org-invoice)
  611. (const :tag "C learn: SuperMemo's incremental learning algorithm" org-learn)
  612. (const :tag "C mac-iCal Imports events from iCal.app to the Emacs diary" org-mac-iCal)
  613. (const :tag "C mac-link: Grab links and url from various mac Applications" org-mac-link)
  614. (const :tag "C mairix: Hook mairix search into Org-mode for different MUAs" org-mairix)
  615. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  616. (const :tag "C mew: Links to Mew folders/messages" org-mew)
  617. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  618. (const :tag "C notmuch: Provide org links to notmuch searches or messages" org-notmuch)
  619. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  620. (const :tag "C registry: A registry for Org-mode links" org-registry)
  621. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  622. (const :tag "C secretary: Team management with org-mode" org-secretary)
  623. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  624. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  625. (const :tag "C track: Keep up with Org-mode development" org-track)
  626. (const :tag "C velocity Something like Notational Velocity for Org" org-velocity)
  627. (const :tag "C vm: Links to VM folders/messages" org-vm)
  628. (const :tag "C wikinodes: CamelCase wiki-like links" org-wikinodes)
  629. (const :tag "C wl: Links to Wanderlust folders/messages" org-wl)
  630. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  631. (defvar org-export-registered-backends) ; From ox.el.
  632. (declare-function org-export-derived-backend-p "ox" (backend &rest backends))
  633. (declare-function org-export-backend-name "ox" (backend))
  634. (defcustom org-export-backends '(ascii html icalendar latex)
  635. "List of export back-ends that should be always available.
  636. If a description starts with <C>, the file is not part of Emacs
  637. and loading it will require that you have downloaded and properly
  638. installed the Org mode distribution.
  639. Unlike to `org-modules', libraries in this list will not be
  640. loaded along with Org, but only once the export framework is
  641. needed.
  642. This variable needs to be set before org.el is loaded. If you
  643. need to make a change while Emacs is running, use the customize
  644. interface or run the following code, where VAL stands for the new
  645. value of the variable, after updating it:
  646. (progn
  647. (setq org-export-registered-backends
  648. (cl-remove-if-not
  649. (lambda (backend)
  650. (let ((name (org-export-backend-name backend)))
  651. (or (memq name val)
  652. (catch \\='parentp
  653. (dolist (b val)
  654. (and (org-export-derived-backend-p b name)
  655. (throw \\='parentp t)))))))
  656. org-export-registered-backends))
  657. (let ((new-list (mapcar #\\='org-export-backend-name
  658. org-export-registered-backends)))
  659. (dolist (backend val)
  660. (cond
  661. ((not (load (format \"ox-%s\" backend) t t))
  662. (message \"Problems while trying to load export back-end \\=`%s\\='\"
  663. backend))
  664. ((not (memq backend new-list)) (push backend new-list))))
  665. (set-default \\='org-export-backends new-list)))
  666. Adding a back-end to this list will also pull the back-end it
  667. depends on, if any."
  668. :group 'org
  669. :group 'org-export
  670. :version "24.4"
  671. :package-version '(Org . "8.0")
  672. :initialize 'custom-initialize-set
  673. :set (lambda (var val)
  674. (if (not (featurep 'ox)) (set-default var val)
  675. ;; Any back-end not required anymore (not present in VAL and not
  676. ;; a parent of any back-end in the new value) is removed from the
  677. ;; list of registered back-ends.
  678. (setq org-export-registered-backends
  679. (cl-remove-if-not
  680. (lambda (backend)
  681. (let ((name (org-export-backend-name backend)))
  682. (or (memq name val)
  683. (catch 'parentp
  684. (dolist (b val)
  685. (and (org-export-derived-backend-p b name)
  686. (throw 'parentp t)))))))
  687. org-export-registered-backends))
  688. ;; Now build NEW-LIST of both new back-ends and required
  689. ;; parents.
  690. (let ((new-list (mapcar #'org-export-backend-name
  691. org-export-registered-backends)))
  692. (dolist (backend val)
  693. (cond
  694. ((not (load (format "ox-%s" backend) t t))
  695. (message "Problems while trying to load export back-end `%s'"
  696. backend))
  697. ((not (memq backend new-list)) (push backend new-list))))
  698. ;; Set VAR to that list with fixed dependencies.
  699. (set-default var new-list))))
  700. :type '(set :greedy t
  701. (const :tag " ascii Export buffer to ASCII format" ascii)
  702. (const :tag " beamer Export buffer to Beamer presentation" beamer)
  703. (const :tag " html Export buffer to HTML format" html)
  704. (const :tag " icalendar Export buffer to iCalendar format" icalendar)
  705. (const :tag " latex Export buffer to LaTeX format" latex)
  706. (const :tag " man Export buffer to MAN format" man)
  707. (const :tag " md Export buffer to Markdown format" md)
  708. (const :tag " odt Export buffer to ODT format" odt)
  709. (const :tag " org Export buffer to Org format" org)
  710. (const :tag " texinfo Export buffer to Texinfo format" texinfo)
  711. (const :tag "C confluence Export buffer to Confluence Wiki format" confluence)
  712. (const :tag "C deck Export buffer to deck.js presentations" deck)
  713. (const :tag "C freemind Export buffer to Freemind mindmap format" freemind)
  714. (const :tag "C groff Export buffer to Groff format" groff)
  715. (const :tag "C koma-letter Export buffer to KOMA Scrlttrl2 format" koma-letter)
  716. (const :tag "C RSS 2.0 Export buffer to RSS 2.0 format" rss)
  717. (const :tag "C s5 Export buffer to s5 presentations" s5)
  718. (const :tag "C taskjuggler Export buffer to TaskJuggler format" taskjuggler)))
  719. (eval-after-load 'ox
  720. '(dolist (backend org-export-backends)
  721. (condition-case nil (require (intern (format "ox-%s" backend)))
  722. (error (message "Problems while trying to load export back-end `%s'"
  723. backend)))))
  724. (defcustom org-support-shift-select nil
  725. "Non-nil means make shift-cursor commands select text when possible.
  726. \\<org-mode-map>\
  727. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys
  728. start selecting a region, or enlarge regions started in this way.
  729. In Org-mode, in special contexts, these same keys are used for
  730. other purposes, important enough to compete with shift selection.
  731. Org tries to balance these needs by supporting `shift-select-mode'
  732. outside these special contexts, under control of this variable.
  733. The default of this variable is nil, to avoid confusing behavior. Shifted
  734. cursor keys will then execute Org commands in the following contexts:
  735. - on a headline, changing TODO state (left/right) and priority (up/down)
  736. - on a time stamp, changing the time
  737. - in a plain list item, changing the bullet type
  738. - in a property definition line, switching between allowed values
  739. - in the BEGIN line of a clock table (changing the time block).
  740. Outside these contexts, the commands will throw an error.
  741. When this variable is t and the cursor is not in a special
  742. context, Org-mode will support shift-selection for making and
  743. enlarging regions. To make this more effective, the bullet
  744. cycling will no longer happen anywhere in an item line, but only
  745. if the cursor is exactly on the bullet.
  746. If you set this variable to the symbol `always', then the keys
  747. will not be special in headlines, property lines, and item lines,
  748. to make shift selection work there as well. If this is what you
  749. want, you can use the following alternative commands:
  750. `\\[org-todo]' and `\\[org-priority]' \
  751. to change TODO state and priority,
  752. `\\[universal-argument] \\[universal-argument] \\[org-todo]' \
  753. can be used to switch TODO sets,
  754. `\\[org-ctrl-c-minus]' to cycle item bullet types,
  755. and properties can be edited by hand or in column view.
  756. However, when the cursor is on a timestamp, shift-cursor commands
  757. will still edit the time stamp - this is just too good to give up.
  758. XEmacs user should have this variable set to nil, because
  759. `shift-select-mode' is in Emacs 23 or later only."
  760. :group 'org
  761. :type '(choice
  762. (const :tag "Never" nil)
  763. (const :tag "When outside special context" t)
  764. (const :tag "Everywhere except timestamps" always)))
  765. (defcustom org-loop-over-headlines-in-active-region nil
  766. "Shall some commands act upon headlines in the active region?
  767. When set to t, some commands will be performed in all headlines
  768. within the active region.
  769. When set to `start-level', some commands will be performed in all
  770. headlines within the active region, provided that these headlines
  771. are of the same level than the first one.
  772. When set to a string, those commands will be performed on the
  773. matching headlines within the active region. Such string must be
  774. a tags/property/todo match as it is used in the agenda tags view.
  775. The list of commands is: `org-schedule', `org-deadline',
  776. `org-todo', `org-archive-subtree', `org-archive-set-tag' and
  777. `org-archive-to-archive-sibling'. The archiving commands skip
  778. already archived entries."
  779. :type '(choice (const :tag "Don't loop" nil)
  780. (const :tag "All headlines in active region" t)
  781. (const :tag "In active region, headlines at the same level than the first one" start-level)
  782. (string :tag "Tags/Property/Todo matcher"))
  783. :version "24.1"
  784. :group 'org-todo
  785. :group 'org-archive)
  786. (defgroup org-startup nil
  787. "Options concerning startup of Org-mode."
  788. :tag "Org Startup"
  789. :group 'org)
  790. (defcustom org-startup-folded t
  791. "Non-nil means entering Org-mode will switch to OVERVIEW.
  792. This can also be configured on a per-file basis by adding one of
  793. the following lines anywhere in the buffer:
  794. #+STARTUP: fold (or `overview', this is equivalent)
  795. #+STARTUP: nofold (or `showall', this is equivalent)
  796. #+STARTUP: content
  797. #+STARTUP: showeverything
  798. By default, this option is ignored when Org opens agenda files
  799. for the first time. If you want the agenda to honor the startup
  800. option, set `org-agenda-inhibit-startup' to nil."
  801. :group 'org-startup
  802. :type '(choice
  803. (const :tag "nofold: show all" nil)
  804. (const :tag "fold: overview" t)
  805. (const :tag "content: all headlines" content)
  806. (const :tag "show everything, even drawers" showeverything)))
  807. (defcustom org-startup-truncated t
  808. "Non-nil means entering Org-mode will set `truncate-lines'.
  809. This is useful since some lines containing links can be very long and
  810. uninteresting. Also tables look terrible when wrapped."
  811. :group 'org-startup
  812. :type 'boolean)
  813. (defcustom org-startup-indented nil
  814. "Non-nil means turn on `org-indent-mode' on startup.
  815. This can also be configured on a per-file basis by adding one of
  816. the following lines anywhere in the buffer:
  817. #+STARTUP: indent
  818. #+STARTUP: noindent"
  819. :group 'org-structure
  820. :type '(choice
  821. (const :tag "Not" nil)
  822. (const :tag "Globally (slow on startup in large files)" t)))
  823. (defcustom org-use-sub-superscripts t
  824. "Non-nil means interpret \"_\" and \"^\" for display.
  825. If you want to control how Org exports those characters, see
  826. `org-export-with-sub-superscripts'. `org-use-sub-superscripts'
  827. used to be an alias for `org-export-with-sub-superscripts' in
  828. Org <8.0, it is not anymore.
  829. When this option is turned on, you can use TeX-like syntax for
  830. sub- and superscripts within the buffer. Several characters after
  831. \"_\" or \"^\" will be considered as a single item - so grouping
  832. with {} is normally not needed. For example, the following things
  833. will be parsed as single sub- or superscripts:
  834. 10^24 or 10^tau several digits will be considered 1 item.
  835. 10^-12 or 10^-tau a leading sign with digits or a word
  836. x^2-y^3 will be read as x^2 - y^3, because items are
  837. terminated by almost any nonword/nondigit char.
  838. x_{i^2} or x^(2-i) braces or parenthesis do grouping.
  839. Still, ambiguity is possible. So when in doubt, use {} to enclose
  840. the sub/superscript. If you set this variable to the symbol `{}',
  841. the braces are *required* in order to trigger interpretations as
  842. sub/superscript. This can be helpful in documents that need \"_\"
  843. frequently in plain text."
  844. :group 'org-startup
  845. :version "24.4"
  846. :package-version '(Org . "8.0")
  847. :type '(choice
  848. (const :tag "Always interpret" t)
  849. (const :tag "Only with braces" {})
  850. (const :tag "Never interpret" nil)))
  851. (defcustom org-startup-with-beamer-mode nil
  852. "Non-nil means turn on `org-beamer-mode' on startup.
  853. This can also be configured on a per-file basis by adding one of
  854. the following lines anywhere in the buffer:
  855. #+STARTUP: beamer"
  856. :group 'org-startup
  857. :version "24.1"
  858. :type 'boolean)
  859. (defcustom org-startup-align-all-tables nil
  860. "Non-nil means align all tables when visiting a file.
  861. This is useful when the column width in tables is forced with <N> cookies
  862. in table fields. Such tables will look correct only after the first re-align.
  863. This can also be configured on a per-file basis by adding one of
  864. the following lines anywhere in the buffer:
  865. #+STARTUP: align
  866. #+STARTUP: noalign"
  867. :group 'org-startup
  868. :type 'boolean)
  869. (defcustom org-startup-with-inline-images nil
  870. "Non-nil means show inline images when loading a new Org file.
  871. This can also be configured on a per-file basis by adding one of
  872. the following lines anywhere in the buffer:
  873. #+STARTUP: inlineimages
  874. #+STARTUP: noinlineimages"
  875. :group 'org-startup
  876. :version "24.1"
  877. :type 'boolean)
  878. (defcustom org-startup-with-latex-preview nil
  879. "Non-nil means preview LaTeX fragments when loading a new Org file.
  880. This can also be configured on a per-file basis by adding one of
  881. the following lines anywhere in the buffer:
  882. #+STARTUP: latexpreview
  883. #+STARTUP: nolatexpreview"
  884. :group 'org-startup
  885. :version "24.4"
  886. :package-version '(Org . "8.0")
  887. :type 'boolean)
  888. (defcustom org-insert-mode-line-in-empty-file nil
  889. "Non-nil means insert the first line setting Org-mode in empty files.
  890. When the function `org-mode' is called interactively in an empty file, this
  891. normally means that the file name does not automatically trigger Org-mode.
  892. To ensure that the file will always be in Org-mode in the future, a
  893. line enforcing Org-mode will be inserted into the buffer, if this option
  894. has been set."
  895. :group 'org-startup
  896. :type 'boolean)
  897. (defcustom org-replace-disputed-keys nil
  898. "Non-nil means use alternative key bindings for some keys.
  899. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  900. These keys are also used by other packages like shift-selection-mode'
  901. \(built into Emacs 23), `CUA-mode' or `windmove.el'.
  902. If you want to use Org-mode together with one of these other modes,
  903. or more generally if you would like to move some Org-mode commands to
  904. other keys, set this variable and configure the keys with the variable
  905. `org-disputed-keys'.
  906. This option is only relevant at load-time of Org-mode, and must be set
  907. *before* org.el is loaded. Changing it requires a restart of Emacs to
  908. become effective."
  909. :group 'org-startup
  910. :type 'boolean)
  911. (defcustom org-use-extra-keys nil
  912. "Non-nil means use extra key sequence definitions for certain commands.
  913. This happens automatically if you run XEmacs or if `window-system'
  914. is nil. This variable lets you do the same manually. You must
  915. set it before loading org.
  916. Example: on Carbon Emacs 22 running graphically, with an external
  917. keyboard on a Powerbook, the default way of setting M-left might
  918. not work for either Alt or ESC. Setting this variable will make
  919. it work for ESC."
  920. :group 'org-startup
  921. :type 'boolean)
  922. (org-defvaralias 'org-CUA-compatible 'org-replace-disputed-keys)
  923. (defcustom org-disputed-keys
  924. '(([(shift up)] . [(meta p)])
  925. ([(shift down)] . [(meta n)])
  926. ([(shift left)] . [(meta -)])
  927. ([(shift right)] . [(meta +)])
  928. ([(control shift right)] . [(meta shift +)])
  929. ([(control shift left)] . [(meta shift -)]))
  930. "Keys for which Org-mode and other modes compete.
  931. This is an alist, cars are the default keys, second element specifies
  932. the alternative to use when `org-replace-disputed-keys' is t.
  933. Keys can be specified in any syntax supported by `define-key'.
  934. The value of this option takes effect only at Org-mode's startup,
  935. therefore you'll have to restart Emacs to apply it after changing."
  936. :group 'org-startup
  937. :type 'alist)
  938. (defun org-key (key)
  939. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  940. Or return the original if not disputed.
  941. Also apply the translations defined in `org-xemacs-key-equivalents'."
  942. (when org-replace-disputed-keys
  943. (let* ((nkey (key-description key))
  944. (x (org-find-if (lambda (x)
  945. (equal (key-description (car x)) nkey))
  946. org-disputed-keys)))
  947. (setq key (if x (cdr x) key))))
  948. (when (featurep 'xemacs)
  949. (setq key (or (cdr (assoc key org-xemacs-key-equivalents)) key)))
  950. key)
  951. (defun org-find-if (predicate seq)
  952. (catch 'exit
  953. (while seq
  954. (if (funcall predicate (car seq))
  955. (throw 'exit (car seq))
  956. (pop seq)))))
  957. (defun org-defkey (keymap key def)
  958. "Define a key, possibly translated, as returned by `org-key'."
  959. (define-key keymap (org-key key) def))
  960. (defcustom org-ellipsis nil
  961. "The ellipsis to use in the Org-mode outline.
  962. When nil, just use the standard three dots.
  963. When a string, use that string instead.
  964. When a face, use the standard 3 dots, but with the specified face.
  965. The change affects only Org-mode (which will then use its own display table).
  966. Changing this requires executing \\[org-mode] in a buffer to become
  967. effective."
  968. :group 'org-startup
  969. :type '(choice (const :tag "Default" nil)
  970. (face :tag "Face" :value org-warning)
  971. (string :tag "String" :value "...#")))
  972. (defvar org-display-table nil
  973. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  974. (defgroup org-keywords nil
  975. "Keywords in Org-mode."
  976. :tag "Org Keywords"
  977. :group 'org)
  978. (defcustom org-closed-keep-when-no-todo nil
  979. "Remove CLOSED: time-stamp when switching back to a non-todo state?"
  980. :group 'org-todo
  981. :group 'org-keywords
  982. :version "24.4"
  983. :package-version '(Org . "8.0")
  984. :type 'boolean)
  985. (defgroup org-structure nil
  986. "Options concerning the general structure of Org-mode files."
  987. :tag "Org Structure"
  988. :group 'org)
  989. (defgroup org-reveal-location nil
  990. "Options about how to make context of a location visible."
  991. :tag "Org Reveal Location"
  992. :group 'org-structure)
  993. (defcustom org-show-context-detail '((isearch . lineage)
  994. (bookmark-jump . lineage)
  995. (default . ancestors))
  996. "Alist between context and visibility span when revealing a location.
  997. \\<org-mode-map>Some actions may move point into invisible
  998. locations. As a consequence, Org always expose a neighborhood
  999. around point. How much is shown depends on the initial action,
  1000. or context. Valid contexts are
  1001. agenda when exposing an entry from the agenda
  1002. org-goto when using the command `org-goto' (\\[org-goto])
  1003. occur-tree when using the command `org-occur' (\\[org-sparse-tree] /)
  1004. tags-tree when constructing a sparse tree based on tags matches
  1005. link-search when exposing search matches associated with a link
  1006. mark-goto when exposing the jump goal of a mark
  1007. bookmark-jump when exposing a bookmark location
  1008. isearch when exiting from an incremental search
  1009. default default for all contexts not set explicitly
  1010. Allowed visibility spans are
  1011. minimal show current headline; if point is not on headline,
  1012. also show entry
  1013. local show current headline, entry and next headline
  1014. ancestors show current headline and its direct ancestors; if
  1015. point is not on headline, also show entry
  1016. lineage show current headline, its direct ancestors and all
  1017. their children; if point is not on headline, also show
  1018. entry and first child
  1019. tree show current headline, its direct ancestors and all
  1020. their children; if point is not on headline, also show
  1021. entry and all children
  1022. canonical show current headline, its direct ancestors along with
  1023. their entries and children; if point is not located on
  1024. the headline, also show current entry and all children
  1025. As special cases, a nil or t value means show all contexts in
  1026. `minimal' or `canonical' view, respectively.
  1027. Some views can make displayed information very compact, but also
  1028. make it harder to edit the location of the match. In such
  1029. a case, use the command `org-reveal' (\\[org-reveal]) to show
  1030. more context."
  1031. :group 'org-reveal-location
  1032. :version "25.1"
  1033. :package-version '(Org . "8.3")
  1034. :type '(choice
  1035. (const :tag "Canonical" t)
  1036. (const :tag "Minimal" nil)
  1037. (repeat :greedy t :tag "Individual contexts"
  1038. (cons
  1039. (choice :tag "Context"
  1040. (const agenda)
  1041. (const org-goto)
  1042. (const occur-tree)
  1043. (const tags-tree)
  1044. (const link-search)
  1045. (const mark-goto)
  1046. (const bookmark-jump)
  1047. (const isearch)
  1048. (const default))
  1049. (choice :tag "Detail level"
  1050. (const minimal)
  1051. (const local)
  1052. (const ancestors)
  1053. (const lineage)
  1054. (const tree)
  1055. (const canonical))))))
  1056. (defcustom org-indirect-buffer-display 'other-window
  1057. "How should indirect tree buffers be displayed?
  1058. This applies to indirect buffers created with the commands
  1059. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  1060. Valid values are:
  1061. current-window Display in the current window
  1062. other-window Just display in another window.
  1063. dedicated-frame Create one new frame, and re-use it each time.
  1064. new-frame Make a new frame each time. Note that in this case
  1065. previously-made indirect buffers are kept, and you need to
  1066. kill these buffers yourself."
  1067. :group 'org-structure
  1068. :group 'org-agenda-windows
  1069. :type '(choice
  1070. (const :tag "In current window" current-window)
  1071. (const :tag "In current frame, other window" other-window)
  1072. (const :tag "Each time a new frame" new-frame)
  1073. (const :tag "One dedicated frame" dedicated-frame)))
  1074. (defcustom org-use-speed-commands nil
  1075. "Non-nil means activate single letter commands at beginning of a headline.
  1076. This may also be a function to test for appropriate locations where speed
  1077. commands should be active.
  1078. For example, to activate speed commands when the point is on any
  1079. star at the beginning of the headline, you can do this:
  1080. (setq org-use-speed-commands
  1081. (lambda () (and (looking-at org-outline-regexp) (looking-back \"^\\**\"))))"
  1082. :group 'org-structure
  1083. :type '(choice
  1084. (const :tag "Never" nil)
  1085. (const :tag "At beginning of headline stars" t)
  1086. (function)))
  1087. (defcustom org-speed-commands-user nil
  1088. "Alist of additional speed commands.
  1089. This list will be checked before `org-speed-commands-default'
  1090. when the variable `org-use-speed-commands' is non-nil
  1091. and when the cursor is at the beginning of a headline.
  1092. The car if each entry is a string with a single letter, which must
  1093. be assigned to `self-insert-command' in the global map.
  1094. The cdr is either a command to be called interactively, a function
  1095. to be called, or a form to be evaluated.
  1096. An entry that is just a list with a single string will be interpreted
  1097. as a descriptive headline that will be added when listing the speed
  1098. commands in the Help buffer using the `?' speed command."
  1099. :group 'org-structure
  1100. :type '(repeat :value ("k" . ignore)
  1101. (choice :value ("k" . ignore)
  1102. (list :tag "Descriptive Headline" (string :tag "Headline"))
  1103. (cons :tag "Letter and Command"
  1104. (string :tag "Command letter")
  1105. (choice
  1106. (function)
  1107. (sexp))))))
  1108. (defcustom org-bookmark-names-plist
  1109. '(:last-capture "org-capture-last-stored"
  1110. :last-refile "org-refile-last-stored"
  1111. :last-capture-marker "org-capture-last-stored-marker")
  1112. "Names for bookmarks automatically set by some Org commands.
  1113. This can provide strings as names for a number of bookmarks Org sets
  1114. automatically. The following keys are currently implemented:
  1115. :last-capture
  1116. :last-capture-marker
  1117. :last-refile
  1118. When a key does not show up in the property list, the corresponding bookmark
  1119. is not set."
  1120. :group 'org-structure
  1121. :type 'plist)
  1122. (defgroup org-cycle nil
  1123. "Options concerning visibility cycling in Org-mode."
  1124. :tag "Org Cycle"
  1125. :group 'org-structure)
  1126. (defcustom org-cycle-skip-children-state-if-no-children t
  1127. "Non-nil means skip CHILDREN state in entries that don't have any."
  1128. :group 'org-cycle
  1129. :type 'boolean)
  1130. (defcustom org-cycle-max-level nil
  1131. "Maximum level which should still be subject to visibility cycling.
  1132. Levels higher than this will, for cycling, be treated as text, not a headline.
  1133. When `org-odd-levels-only' is set, a value of N in this variable actually
  1134. means 2N-1 stars as the limiting headline.
  1135. When nil, cycle all levels.
  1136. Note that the limiting level of cycling is also influenced by
  1137. `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
  1138. `org-inlinetask-min-level' is, cycling will be limited to levels one less
  1139. than its value."
  1140. :group 'org-cycle
  1141. :type '(choice
  1142. (const :tag "No limit" nil)
  1143. (integer :tag "Maximum level")))
  1144. (defcustom org-hide-block-startup nil
  1145. "Non-nil means entering Org-mode will fold all blocks.
  1146. This can also be set in on a per-file basis with
  1147. #+STARTUP: hideblocks
  1148. #+STARTUP: showblocks"
  1149. :group 'org-startup
  1150. :group 'org-cycle
  1151. :type 'boolean)
  1152. (defcustom org-cycle-global-at-bob nil
  1153. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  1154. This makes it possible to do global cycling without having to use S-TAB or
  1155. \\[universal-argument] TAB. For this special case to work, the first line
  1156. of the buffer must not be a headline -- it may be empty or some other text.
  1157. When used in this way, `org-cycle-hook' is disabled temporarily to make
  1158. sure the cursor stays at the beginning of the buffer. When this option is
  1159. nil, don't do anything special at the beginning of the buffer."
  1160. :group 'org-cycle
  1161. :type 'boolean)
  1162. (defcustom org-cycle-level-after-item/entry-creation t
  1163. "Non-nil means cycle entry level or item indentation in new empty entries.
  1164. When the cursor is at the end of an empty headline, i.e., with only stars
  1165. and maybe a TODO keyword, TAB will then switch the entry to become a child,
  1166. and then all possible ancestor states, before returning to the original state.
  1167. This makes data entry extremely fast: M-RET to create a new headline,
  1168. on TAB to make it a child, two or more tabs to make it a (grand-)uncle.
  1169. When the cursor is at the end of an empty plain list item, one TAB will
  1170. make it a subitem, two or more tabs will back up to make this an item
  1171. higher up in the item hierarchy."
  1172. :group 'org-cycle
  1173. :type 'boolean)
  1174. (defcustom org-cycle-emulate-tab t
  1175. "Where should `org-cycle' emulate TAB.
  1176. nil Never
  1177. white Only in completely white lines
  1178. whitestart Only at the beginning of lines, before the first non-white char
  1179. t Everywhere except in headlines
  1180. exc-hl-bol Everywhere except at the start of a headline
  1181. If TAB is used in a place where it does not emulate TAB, the current subtree
  1182. visibility is cycled."
  1183. :group 'org-cycle
  1184. :type '(choice (const :tag "Never" nil)
  1185. (const :tag "Only in completely white lines" white)
  1186. (const :tag "Before first char in a line" whitestart)
  1187. (const :tag "Everywhere except in headlines" t)
  1188. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)))
  1189. (defcustom org-cycle-separator-lines 2
  1190. "Number of empty lines needed to keep an empty line between collapsed trees.
  1191. If you leave an empty line between the end of a subtree and the following
  1192. headline, this empty line is hidden when the subtree is folded.
  1193. Org-mode will leave (exactly) one empty line visible if the number of
  1194. empty lines is equal or larger to the number given in this variable.
  1195. So the default 2 means at least 2 empty lines after the end of a subtree
  1196. are needed to produce free space between a collapsed subtree and the
  1197. following headline.
  1198. If the number is negative, and the number of empty lines is at least -N,
  1199. all empty lines are shown.
  1200. Special case: when 0, never leave empty lines in collapsed view."
  1201. :group 'org-cycle
  1202. :type 'integer)
  1203. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  1204. (defcustom org-pre-cycle-hook nil
  1205. "Hook that is run before visibility cycling is happening.
  1206. The function(s) in this hook must accept a single argument which indicates
  1207. the new state that will be set right after running this hook. The
  1208. argument is a symbol. Before a global state change, it can have the values
  1209. `overview', `content', or `all'. Before a local state change, it can have
  1210. the values `folded', `children', or `subtree'."
  1211. :group 'org-cycle
  1212. :type 'hook)
  1213. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  1214. org-cycle-hide-drawers
  1215. org-cycle-show-empty-lines
  1216. org-optimize-window-after-visibility-change)
  1217. "Hook that is run after `org-cycle' has changed the buffer visibility.
  1218. The function(s) in this hook must accept a single argument which indicates
  1219. the new state that was set by the most recent `org-cycle' command. The
  1220. argument is a symbol. After a global state change, it can have the values
  1221. `overview', `contents', or `all'. After a local state change, it can have
  1222. the values `folded', `children', or `subtree'."
  1223. :group 'org-cycle
  1224. :type 'hook
  1225. :version "25.1"
  1226. :package-version '(Org . "8.3"))
  1227. (defgroup org-edit-structure nil
  1228. "Options concerning structure editing in Org-mode."
  1229. :tag "Org Edit Structure"
  1230. :group 'org-structure)
  1231. (defcustom org-odd-levels-only nil
  1232. "Non-nil means skip even levels and only use odd levels for the outline.
  1233. This has the effect that two stars are being added/taken away in
  1234. promotion/demotion commands. It also influences how levels are
  1235. handled by the exporters.
  1236. Changing it requires restart of `font-lock-mode' to become effective
  1237. for fontification also in regions already fontified.
  1238. You may also set this on a per-file basis by adding one of the following
  1239. lines to the buffer:
  1240. #+STARTUP: odd
  1241. #+STARTUP: oddeven"
  1242. :group 'org-edit-structure
  1243. :group 'org-appearance
  1244. :type 'boolean)
  1245. (defcustom org-adapt-indentation t
  1246. "Non-nil means adapt indentation to outline node level.
  1247. When this variable is set, Org assumes that you write outlines by
  1248. indenting text in each node to align with the headline (after the
  1249. stars). The following issues are influenced by this variable:
  1250. - The indentation is increased by one space in a demotion
  1251. command, and decreased by one in a promotion command. However,
  1252. in the latter case, if shifting some line in the entry body
  1253. would alter document structure (e.g., insert a new headline),
  1254. indentation is not changed at all.
  1255. - Property drawers and planning information is inserted indented
  1256. when this variable is set. When nil, they will not be indented.
  1257. - TAB indents a line relative to current level. The lines below
  1258. a headline will be indented when this variable is set.
  1259. Note that this is all about true indentation, by adding and
  1260. removing space characters. See also `org-indent.el' which does
  1261. level-dependent indentation in a virtual way, i.e. at display
  1262. time in Emacs."
  1263. :group 'org-edit-structure
  1264. :type 'boolean)
  1265. (defcustom org-special-ctrl-a/e nil
  1266. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  1267. When t, `C-a' will bring back the cursor to the beginning of the
  1268. headline text, i.e. after the stars and after a possible TODO
  1269. keyword. In an item, this will be the position after bullet and
  1270. check-box, if any. When the cursor is already at that position,
  1271. another `C-a' will bring it to the beginning of the line.
  1272. `C-e' will jump to the end of the headline, ignoring the presence
  1273. of tags in the headline. A second `C-e' will then jump to the
  1274. true end of the line, after any tags. This also means that, when
  1275. this variable is non-nil, `C-e' also will never jump beyond the
  1276. end of the heading of a folded section, i.e. not after the
  1277. ellipses.
  1278. When set to the symbol `reversed', the first `C-a' or `C-e' works
  1279. normally, going to the true line boundary first. Only a directly
  1280. following, identical keypress will bring the cursor to the
  1281. special positions.
  1282. This may also be a cons cell where the behavior for `C-a' and
  1283. `C-e' is set separately."
  1284. :group 'org-edit-structure
  1285. :type '(choice
  1286. (const :tag "off" nil)
  1287. (const :tag "on: after stars/bullet and before tags first" t)
  1288. (const :tag "reversed: true line boundary first" reversed)
  1289. (cons :tag "Set C-a and C-e separately"
  1290. (choice :tag "Special C-a"
  1291. (const :tag "off" nil)
  1292. (const :tag "on: after stars/bullet first" t)
  1293. (const :tag "reversed: before stars/bullet first" reversed))
  1294. (choice :tag "Special C-e"
  1295. (const :tag "off" nil)
  1296. (const :tag "on: before tags first" t)
  1297. (const :tag "reversed: after tags first" reversed)))))
  1298. (org-defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e)
  1299. (defcustom org-special-ctrl-k nil
  1300. "Non-nil means `C-k' will behave specially in headlines.
  1301. When nil, `C-k' will call the default `kill-line' command.
  1302. When t, the following will happen while the cursor is in the headline:
  1303. - When the cursor is at the beginning of a headline, kill the entire
  1304. line and possible the folded subtree below the line.
  1305. - When in the middle of the headline text, kill the headline up to the tags.
  1306. - When after the headline text, kill the tags."
  1307. :group 'org-edit-structure
  1308. :type 'boolean)
  1309. (defcustom org-ctrl-k-protect-subtree nil
  1310. "Non-nil means, do not delete a hidden subtree with C-k.
  1311. When set to the symbol `error', simply throw an error when C-k is
  1312. used to kill (part-of) a headline that has hidden text behind it.
  1313. Any other non-nil value will result in a query to the user, if it is
  1314. OK to kill that hidden subtree. When nil, kill without remorse."
  1315. :group 'org-edit-structure
  1316. :version "24.1"
  1317. :type '(choice
  1318. (const :tag "Do not protect hidden subtrees" nil)
  1319. (const :tag "Protect hidden subtrees with a security query" t)
  1320. (const :tag "Never kill a hidden subtree with C-k" error)))
  1321. (defcustom org-special-ctrl-o t
  1322. "Non-nil means, make `C-o' insert a row in tables."
  1323. :group 'org-edit-structure
  1324. :type 'boolean)
  1325. (defcustom org-catch-invisible-edits nil
  1326. "Check if in invisible region before inserting or deleting a character.
  1327. Valid values are:
  1328. nil Do not check, so just do invisible edits.
  1329. error Throw an error and do nothing.
  1330. show Make point visible, and do the requested edit.
  1331. show-and-error Make point visible, then throw an error and abort the edit.
  1332. smart Make point visible, and do insertion/deletion if it is
  1333. adjacent to visible text and the change feels predictable.
  1334. Never delete a previously invisible character or add in the
  1335. middle or right after an invisible region. Basically, this
  1336. allows insertion and backward-delete right before ellipses.
  1337. FIXME: maybe in this case we should not even show?"
  1338. :group 'org-edit-structure
  1339. :version "24.1"
  1340. :type '(choice
  1341. (const :tag "Do not check" nil)
  1342. (const :tag "Throw error when trying to edit" error)
  1343. (const :tag "Unhide, but do not do the edit" show-and-error)
  1344. (const :tag "Show invisible part and do the edit" show)
  1345. (const :tag "Be smart and do the right thing" smart)))
  1346. (defcustom org-yank-folded-subtrees t
  1347. "Non-nil means when yanking subtrees, fold them.
  1348. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  1349. it starts with a heading and all other headings in it are either children
  1350. or siblings, then fold all the subtrees. However, do this only if no
  1351. text after the yank would be swallowed into a folded tree by this action."
  1352. :group 'org-edit-structure
  1353. :type 'boolean)
  1354. (defcustom org-yank-adjusted-subtrees nil
  1355. "Non-nil means when yanking subtrees, adjust the level.
  1356. With this setting, `org-paste-subtree' is used to insert the subtree, see
  1357. this function for details."
  1358. :group 'org-edit-structure
  1359. :type 'boolean)
  1360. (defcustom org-M-RET-may-split-line '((default . t))
  1361. "Non-nil means M-RET will split the line at the cursor position.
  1362. When nil, it will go to the end of the line before making a
  1363. new line.
  1364. You may also set this option in a different way for different
  1365. contexts. Valid contexts are:
  1366. headline when creating a new headline
  1367. item when creating a new item
  1368. table in a table field
  1369. default the value to be used for all contexts not explicitly
  1370. customized"
  1371. :group 'org-structure
  1372. :group 'org-table
  1373. :type '(choice
  1374. (const :tag "Always" t)
  1375. (const :tag "Never" nil)
  1376. (repeat :greedy t :tag "Individual contexts"
  1377. (cons
  1378. (choice :tag "Context"
  1379. (const headline)
  1380. (const item)
  1381. (const table)
  1382. (const default))
  1383. (boolean)))))
  1384. (defcustom org-insert-heading-respect-content nil
  1385. "Non-nil means insert new headings after the current subtree.
  1386. When nil, the new heading is created directly after the current line.
  1387. The commands \\[org-insert-heading-respect-content] and \\[org-insert-todo-heading-respect-content] turn
  1388. this variable on for the duration of the command."
  1389. :group 'org-structure
  1390. :type 'boolean)
  1391. (defcustom org-blank-before-new-entry '((heading . auto)
  1392. (plain-list-item . auto))
  1393. "Should `org-insert-heading' leave a blank line before new heading/item?
  1394. The value is an alist, with `heading' and `plain-list-item' as CAR,
  1395. and a boolean flag as CDR. The cdr may also be the symbol `auto', in
  1396. which case Org will look at the surrounding headings/items and try to
  1397. make an intelligent decision whether to insert a blank line or not."
  1398. :group 'org-edit-structure
  1399. :type '(list
  1400. (cons (const heading)
  1401. (choice (const :tag "Never" nil)
  1402. (const :tag "Always" t)
  1403. (const :tag "Auto" auto)))
  1404. (cons (const plain-list-item)
  1405. (choice (const :tag "Never" nil)
  1406. (const :tag "Always" t)
  1407. (const :tag "Auto" auto)))))
  1408. (defcustom org-insert-heading-hook nil
  1409. "Hook being run after inserting a new heading."
  1410. :group 'org-edit-structure
  1411. :type 'hook)
  1412. (defcustom org-enable-fixed-width-editor t
  1413. "Non-nil means lines starting with \":\" are treated as fixed-width.
  1414. This currently only means they are never auto-wrapped.
  1415. When nil, such lines will be treated like ordinary lines."
  1416. :group 'org-edit-structure
  1417. :type 'boolean)
  1418. (defcustom org-goto-auto-isearch t
  1419. "Non-nil means typing characters in `org-goto' starts incremental search.
  1420. When nil, you can use these keybindings to navigate the buffer:
  1421. q Quit the org-goto interface
  1422. n Go to the next visible heading
  1423. p Go to the previous visible heading
  1424. f Go one heading forward on same level
  1425. b Go one heading backward on same level
  1426. u Go one heading up"
  1427. :group 'org-edit-structure
  1428. :type 'boolean)
  1429. (defgroup org-sparse-trees nil
  1430. "Options concerning sparse trees in Org-mode."
  1431. :tag "Org Sparse Trees"
  1432. :group 'org-structure)
  1433. (defcustom org-highlight-sparse-tree-matches t
  1434. "Non-nil means highlight all matches that define a sparse tree.
  1435. The highlights will automatically disappear the next time the buffer is
  1436. changed by an edit command."
  1437. :group 'org-sparse-trees
  1438. :type 'boolean)
  1439. (defcustom org-remove-highlights-with-change t
  1440. "Non-nil means any change to the buffer will remove temporary highlights.
  1441. \\<org-mode-map>\
  1442. Such highlights are created by `org-occur' and `org-clock-display'.
  1443. When nil, `\\[org-ctrl-c-ctrl-c]' needs to be used \
  1444. to get rid of the highlights.
  1445. The highlights created by `org-toggle-latex-fragment' always need
  1446. `\\[org-toggle-latex-fragment]' to be removed."
  1447. :group 'org-sparse-trees
  1448. :group 'org-time
  1449. :type 'boolean)
  1450. (defcustom org-occur-hook '(org-first-headline-recenter)
  1451. "Hook that is run after `org-occur' has constructed a sparse tree.
  1452. This can be used to recenter the window to show as much of the structure
  1453. as possible."
  1454. :group 'org-sparse-trees
  1455. :type 'hook)
  1456. (defgroup org-imenu-and-speedbar nil
  1457. "Options concerning imenu and speedbar in Org-mode."
  1458. :tag "Org Imenu and Speedbar"
  1459. :group 'org-structure)
  1460. (defcustom org-imenu-depth 2
  1461. "The maximum level for Imenu access to Org-mode headlines.
  1462. This also applied for speedbar access."
  1463. :group 'org-imenu-and-speedbar
  1464. :type 'integer)
  1465. (defgroup org-table nil
  1466. "Options concerning tables in Org-mode."
  1467. :tag "Org Table"
  1468. :group 'org)
  1469. (defcustom org-enable-table-editor 'optimized
  1470. "Non-nil means lines starting with \"|\" are handled by the table editor.
  1471. When nil, such lines will be treated like ordinary lines.
  1472. When equal to the symbol `optimized', the table editor will be optimized to
  1473. do the following:
  1474. - Automatic overwrite mode in front of whitespace in table fields.
  1475. This makes the structure of the table stay in tact as long as the edited
  1476. field does not exceed the column width.
  1477. - Minimize the number of realigns. Normally, the table is aligned each time
  1478. TAB or RET are pressed to move to another field. With optimization this
  1479. happens only if changes to a field might have changed the column width.
  1480. Optimization requires replacing the functions `self-insert-command',
  1481. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  1482. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  1483. very good at guessing when a re-align will be necessary, but you can always
  1484. force one with \\[org-ctrl-c-ctrl-c].
  1485. If you would like to use the optimized version in Org-mode, but the
  1486. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  1487. This variable can be used to turn on and off the table editor during a session,
  1488. but in order to toggle optimization, a restart is required.
  1489. See also the variable `org-table-auto-blank-field'."
  1490. :group 'org-table
  1491. :type '(choice
  1492. (const :tag "off" nil)
  1493. (const :tag "on" t)
  1494. (const :tag "on, optimized" optimized)))
  1495. (defcustom org-self-insert-cluster-for-undo (or (featurep 'xemacs)
  1496. (version<= emacs-version "24.1"))
  1497. "Non-nil means cluster self-insert commands for undo when possible.
  1498. If this is set, then, like in the Emacs command loop, 20 consecutive
  1499. characters will be undone together.
  1500. This is configurable, because there is some impact on typing performance."
  1501. :group 'org-table
  1502. :type 'boolean)
  1503. (defcustom org-table-tab-recognizes-table.el t
  1504. "Non-nil means TAB will automatically notice a table.el table.
  1505. When it sees such a table, it moves point into it and - if necessary -
  1506. calls `table-recognize-table'."
  1507. :group 'org-table-editing
  1508. :type 'boolean)
  1509. (defgroup org-link nil
  1510. "Options concerning links in Org-mode."
  1511. :tag "Org Link"
  1512. :group 'org)
  1513. (defvar-local org-link-abbrev-alist-local nil
  1514. "Buffer-local version of `org-link-abbrev-alist', which see.
  1515. The value of this is taken from the #+LINK lines.")
  1516. (defcustom org-link-abbrev-alist nil
  1517. "Alist of link abbreviations.
  1518. The car of each element is a string, to be replaced at the start of a link.
  1519. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  1520. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  1521. [[linkkey:tag][description]]
  1522. The `linkkey' must be a word word, starting with a letter, followed
  1523. by letters, numbers, `-' or `_'.
  1524. If REPLACE is a string, the tag will simply be appended to create the link.
  1525. If the string contains \"%s\", the tag will be inserted there. If the string
  1526. contains \"%h\", it will cause a url-encoded version of the tag to be inserted
  1527. at that point (see the function `url-hexify-string'). If the string contains
  1528. the specifier \"%(my-function)\", then the custom function `my-function' will
  1529. be invoked: this function takes the tag as its only argument and must return
  1530. a string.
  1531. REPLACE may also be a function that will be called with the tag as the
  1532. only argument to create the link, which should be returned as a string.
  1533. See the manual for examples."
  1534. :group 'org-link
  1535. :type '(repeat
  1536. (cons
  1537. (string :tag "Protocol")
  1538. (choice
  1539. (string :tag "Format")
  1540. (function)))))
  1541. (defcustom org-descriptive-links t
  1542. "Non-nil means Org will display descriptive links.
  1543. E.g. [[http://orgmode.org][Org website]] will be displayed as
  1544. \"Org Website\", hiding the link itself and just displaying its
  1545. description. When set to nil, Org will display the full links
  1546. literally.
  1547. You can interactively set the value of this variable by calling
  1548. `org-toggle-link-display' or from the menu Org>Hyperlinks menu."
  1549. :group 'org-link
  1550. :type 'boolean)
  1551. (defcustom org-link-file-path-type 'adaptive
  1552. "How the path name in file links should be stored.
  1553. Valid values are:
  1554. relative Relative to the current directory, i.e. the directory of the file
  1555. into which the link is being inserted.
  1556. absolute Absolute path, if possible with ~ for home directory.
  1557. noabbrev Absolute path, no abbreviation of home directory.
  1558. adaptive Use relative path for files in the current directory and sub-
  1559. directories of it. For other files, use an absolute path."
  1560. :group 'org-link
  1561. :type '(choice
  1562. (const relative)
  1563. (const absolute)
  1564. (const noabbrev)
  1565. (const adaptive)))
  1566. (defvaralias 'org-activate-links 'org-highlight-links)
  1567. (defcustom org-highlight-links '(bracket angle plain radio tag date footnote)
  1568. "Types of links that should be highlighted in Org-mode files.
  1569. This is a list of symbols, each one of them leading to the
  1570. highlighting of a certain link type.
  1571. You can still open links that are not highlighted.
  1572. In principle, it does not hurt to turn on highlighting for all
  1573. link types. There may be a small gain when turning off unused
  1574. link types. The types are:
  1575. bracket The recommended [[link][description]] or [[link]] links with hiding.
  1576. angle Links in angular brackets that may contain whitespace like
  1577. <bbdb:Carsten Dominik>.
  1578. plain Plain links in normal text, no whitespace, like http://google.com.
  1579. radio Text that is matched by a radio target, see manual for details.
  1580. tag Tag settings in a headline (link to tag search).
  1581. date Time stamps (link to calendar).
  1582. footnote Footnote labels.
  1583. If you set this variable during an Emacs session, use `org-mode-restart'
  1584. in the Org buffer so that the change takes effect."
  1585. :group 'org-link
  1586. :group 'org-appearance
  1587. :type '(set :greedy t
  1588. (const :tag "Double bracket links" bracket)
  1589. (const :tag "Angular bracket links" angle)
  1590. (const :tag "Plain text links" plain)
  1591. (const :tag "Radio target matches" radio)
  1592. (const :tag "Tags" tag)
  1593. (const :tag "Timestamps" date)
  1594. (const :tag "Footnotes" footnote)))
  1595. (defcustom org-make-link-description-function nil
  1596. "Function to use for generating link descriptions from links.
  1597. When nil, the link location will be used. This function must take
  1598. two parameters: the first one is the link, the second one is the
  1599. description generated by `org-insert-link'. The function should
  1600. return the description to use."
  1601. :group 'org-link
  1602. :type '(choice (const nil) (function)))
  1603. (defgroup org-link-store nil
  1604. "Options concerning storing links in Org-mode."
  1605. :tag "Org Store Link"
  1606. :group 'org-link)
  1607. (defcustom org-url-hexify-p t
  1608. "When non-nil, hexify URL when creating a link."
  1609. :type 'boolean
  1610. :version "24.3"
  1611. :group 'org-link-store)
  1612. (defcustom org-email-link-description-format "Email %c: %.30s"
  1613. "Format of the description part of a link to an email or usenet message.
  1614. The following %-escapes will be replaced by corresponding information:
  1615. %F full \"From\" field
  1616. %f name, taken from \"From\" field, address if no name
  1617. %T full \"To\" field
  1618. %t first name in \"To\" field, address if no name
  1619. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  1620. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  1621. %s subject
  1622. %d date
  1623. %m message-id.
  1624. You may use normal field width specification between the % and the letter.
  1625. This is for example useful to limit the length of the subject.
  1626. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  1627. :group 'org-link-store
  1628. :type 'string)
  1629. (defcustom org-from-is-user-regexp
  1630. (let (r1 r2)
  1631. (when (and user-mail-address (not (string= user-mail-address "")))
  1632. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  1633. (when (and user-full-name (not (string= user-full-name "")))
  1634. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  1635. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  1636. "Regexp matched against the \"From:\" header of an email or usenet message.
  1637. It should match if the message is from the user him/herself."
  1638. :group 'org-link-store
  1639. :type 'regexp)
  1640. (defcustom org-context-in-file-links t
  1641. "Non-nil means file links from `org-store-link' contain context.
  1642. A search string will be added to the file name with :: as separator and
  1643. used to find the context when the link is activated by the command
  1644. `org-open-at-point'. When this option is t, the entire active region
  1645. will be placed in the search string of the file link. If set to a
  1646. positive integer, only the first n lines of context will be stored.
  1647. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1648. negates this setting for the duration of the command."
  1649. :group 'org-link-store
  1650. :type '(choice boolean integer))
  1651. (defcustom org-keep-stored-link-after-insertion nil
  1652. "Non-nil means keep link in list for entire session.
  1653. The command `org-store-link' adds a link pointing to the current
  1654. location to an internal list. These links accumulate during a session.
  1655. The command `org-insert-link' can be used to insert links into any
  1656. Org-mode file (offering completion for all stored links). When this
  1657. option is nil, every link which has been inserted once using \\[org-insert-link]
  1658. will be removed from the list, to make completing the unused links
  1659. more efficient."
  1660. :group 'org-link-store
  1661. :type 'boolean)
  1662. (defgroup org-link-follow nil
  1663. "Options concerning following links in Org-mode."
  1664. :tag "Org Follow Link"
  1665. :group 'org-link)
  1666. (defcustom org-link-translation-function nil
  1667. "Function to translate links with different syntax to Org syntax.
  1668. This can be used to translate links created for example by the Planner
  1669. or emacs-wiki packages to Org syntax.
  1670. The function must accept two parameters, a TYPE containing the link
  1671. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  1672. which is everything after the link protocol. It should return a cons
  1673. with possibly modified values of type and path.
  1674. Org contains a function for this, so if you set this variable to
  1675. `org-translate-link-from-planner', you should be able follow many
  1676. links created by planner."
  1677. :group 'org-link-follow
  1678. :type '(choice (const nil) (function)))
  1679. (defcustom org-follow-link-hook nil
  1680. "Hook that is run after a link has been followed."
  1681. :group 'org-link-follow
  1682. :type 'hook)
  1683. (defcustom org-tab-follows-link nil
  1684. "Non-nil means on links TAB will follow the link.
  1685. Needs to be set before org.el is loaded.
  1686. This really should not be used, it does not make sense, and the
  1687. implementation is bad."
  1688. :group 'org-link-follow
  1689. :type 'boolean)
  1690. (defcustom org-return-follows-link nil
  1691. "Non-nil means on links RET will follow the link.
  1692. In tables, the special behavior of RET has precedence."
  1693. :group 'org-link-follow
  1694. :type 'boolean)
  1695. (defcustom org-mouse-1-follows-link
  1696. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1697. "Non-nil means mouse-1 on a link will follow the link.
  1698. A longer mouse click will still set point. Does not work on XEmacs.
  1699. Needs to be set before org.el is loaded."
  1700. :group 'org-link-follow
  1701. :version "24.4"
  1702. :package-version '(Org . "8.3")
  1703. :type '(choice
  1704. (const :tag "A double click follows the link" double)
  1705. (const :tag "Unconditionally follow the link with mouse-1" t)
  1706. (integer :tag "mouse-1 click does not follow the link if longer than N ms" 450)))
  1707. (defcustom org-mark-ring-length 4
  1708. "Number of different positions to be recorded in the ring.
  1709. Changing this requires a restart of Emacs to work correctly."
  1710. :group 'org-link-follow
  1711. :type 'integer)
  1712. (defcustom org-link-search-must-match-exact-headline 'query-to-create
  1713. "Non-nil means internal links in Org files must exactly match a headline.
  1714. When nil, the link search tries to match a phrase with all words
  1715. in the search text."
  1716. :group 'org-link-follow
  1717. :version "24.1"
  1718. :type '(choice
  1719. (const :tag "Use fuzzy text search" nil)
  1720. (const :tag "Match only exact headline" t)
  1721. (const :tag "Match exact headline or query to create it"
  1722. query-to-create)))
  1723. (defcustom org-link-frame-setup
  1724. '((vm . vm-visit-folder-other-frame)
  1725. (vm-imap . vm-visit-imap-folder-other-frame)
  1726. (gnus . org-gnus-no-new-news)
  1727. (file . find-file-other-window)
  1728. (wl . wl-other-frame))
  1729. "Setup the frame configuration for following links.
  1730. When following a link with Emacs, it may often be useful to display
  1731. this link in another window or frame. This variable can be used to
  1732. set this up for the different types of links.
  1733. For VM, use any of
  1734. `vm-visit-folder'
  1735. `vm-visit-folder-other-window'
  1736. `vm-visit-folder-other-frame'
  1737. For Gnus, use any of
  1738. `gnus'
  1739. `gnus-other-frame'
  1740. `org-gnus-no-new-news'
  1741. For FILE, use any of
  1742. `find-file'
  1743. `find-file-other-window'
  1744. `find-file-other-frame'
  1745. For Wanderlust use any of
  1746. `wl'
  1747. `wl-other-frame'
  1748. For the calendar, use the variable `calendar-setup'.
  1749. For BBDB, it is currently only possible to display the matches in
  1750. another window."
  1751. :group 'org-link-follow
  1752. :type '(list
  1753. (cons (const vm)
  1754. (choice
  1755. (const vm-visit-folder)
  1756. (const vm-visit-folder-other-window)
  1757. (const vm-visit-folder-other-frame)))
  1758. (cons (const vm-imap)
  1759. (choice
  1760. (const vm-visit-imap-folder)
  1761. (const vm-visit-imap-folder-other-window)
  1762. (const vm-visit-imap-folder-other-frame)))
  1763. (cons (const gnus)
  1764. (choice
  1765. (const gnus)
  1766. (const gnus-other-frame)
  1767. (const org-gnus-no-new-news)))
  1768. (cons (const file)
  1769. (choice
  1770. (const find-file)
  1771. (const find-file-other-window)
  1772. (const find-file-other-frame)))
  1773. (cons (const wl)
  1774. (choice
  1775. (const wl)
  1776. (const wl-other-frame)))))
  1777. (defcustom org-display-internal-link-with-indirect-buffer nil
  1778. "Non-nil means use indirect buffer to display infile links.
  1779. Activating internal links (from one location in a file to another location
  1780. in the same file) normally just jumps to the location. When the link is
  1781. activated with a \\[universal-argument] prefix (or with mouse-3), the link \
  1782. is displayed in
  1783. another window. When this option is set, the other window actually displays
  1784. an indirect buffer clone of the current buffer, to avoid any visibility
  1785. changes to the current buffer."
  1786. :group 'org-link-follow
  1787. :type 'boolean)
  1788. (defcustom org-open-non-existing-files nil
  1789. "Non-nil means `org-open-file' will open non-existing files.
  1790. When nil, an error will be generated.
  1791. This variable applies only to external applications because they
  1792. might choke on non-existing files. If the link is to a file that
  1793. will be opened in Emacs, the variable is ignored."
  1794. :group 'org-link-follow
  1795. :type 'boolean)
  1796. (defcustom org-open-directory-means-index-dot-org nil
  1797. "Non-nil means a link to a directory really means to index.org.
  1798. When nil, following a directory link will run dired or open a finder/explorer
  1799. window on that directory."
  1800. :group 'org-link-follow
  1801. :type 'boolean)
  1802. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1803. "Non-nil means ask for confirmation before executing shell links.
  1804. Shell links can be dangerous: just think about a link
  1805. [[shell:rm -rf ~/*][Google Search]]
  1806. This link would show up in your Org-mode document as \"Google Search\",
  1807. but really it would remove your entire home directory.
  1808. Therefore we advise against setting this variable to nil.
  1809. Just change it to `y-or-n-p' if you want to confirm with a
  1810. single keystroke rather than having to type \"yes\"."
  1811. :group 'org-link-follow
  1812. :type '(choice
  1813. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1814. (const :tag "with y-or-n (faster)" y-or-n-p)
  1815. (const :tag "no confirmation (dangerous)" nil)))
  1816. (put 'org-confirm-shell-link-function
  1817. 'safe-local-variable
  1818. (lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1819. (defcustom org-confirm-shell-link-not-regexp ""
  1820. "A regexp to skip confirmation for shell links."
  1821. :group 'org-link-follow
  1822. :version "24.1"
  1823. :type 'regexp)
  1824. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1825. "Non-nil means ask for confirmation before executing Emacs Lisp links.
  1826. Elisp links can be dangerous: just think about a link
  1827. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1828. This link would show up in your Org-mode document as \"Google Search\",
  1829. but really it would remove your entire home directory.
  1830. Therefore we advise against setting this variable to nil.
  1831. Just change it to `y-or-n-p' if you want to confirm with a
  1832. single keystroke rather than having to type \"yes\"."
  1833. :group 'org-link-follow
  1834. :type '(choice
  1835. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1836. (const :tag "with y-or-n (faster)" y-or-n-p)
  1837. (const :tag "no confirmation (dangerous)" nil)))
  1838. (put 'org-confirm-shell-link-function
  1839. 'safe-local-variable
  1840. (lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1841. (defcustom org-confirm-elisp-link-not-regexp ""
  1842. "A regexp to skip confirmation for Elisp links."
  1843. :group 'org-link-follow
  1844. :version "24.1"
  1845. :type 'regexp)
  1846. (defconst org-file-apps-defaults-gnu
  1847. '((remote . emacs)
  1848. (system . mailcap)
  1849. (t . mailcap))
  1850. "Default file applications on a UNIX or GNU/Linux system.
  1851. See `org-file-apps'.")
  1852. (defconst org-file-apps-defaults-macosx
  1853. '((remote . emacs)
  1854. (system . "open %s")
  1855. ("ps.gz" . "gv %s")
  1856. ("eps.gz" . "gv %s")
  1857. ("dvi" . "xdvi %s")
  1858. ("fig" . "xfig %s")
  1859. (t . "open %s"))
  1860. "Default file applications on a MacOS X system.
  1861. The system \"open\" is known as a default, but we use X11 applications
  1862. for some files for which the OS does not have a good default.
  1863. See `org-file-apps'.")
  1864. (defconst org-file-apps-defaults-windowsnt
  1865. (list '(remote . emacs)
  1866. (cons 'system (lambda (file _path)
  1867. (with-no-warnings (w32-shell-execute "open" file))))
  1868. (cons t (lambda (file _path)
  1869. (with-no-warnings (w32-shell-execute "open" file)))))
  1870. "Default file applications on a Windows NT system.
  1871. The system \"open\" is used for most files.
  1872. See `org-file-apps'.")
  1873. (defcustom org-file-apps
  1874. '((auto-mode . emacs)
  1875. ("\\.mm\\'" . default)
  1876. ("\\.x?html?\\'" . default)
  1877. ("\\.pdf\\'" . default))
  1878. "External applications for opening `file:path' items in a document.
  1879. \\<org-mode-map>\
  1880. Org mode uses system defaults for different file types, but
  1881. you can use this variable to set the application for a given file
  1882. extension. The entries in this list are cons cells where the car identifies
  1883. files and the cdr the corresponding command. Possible values for the
  1884. file identifier are
  1885. \"string\" A string as a file identifier can be interpreted in different
  1886. ways, depending on its contents:
  1887. - Alphanumeric characters only:
  1888. Match links with this file extension.
  1889. Example: (\"pdf\" . \"evince %s\")
  1890. to open PDFs with evince.
  1891. - Regular expression: Match links where the
  1892. filename matches the regexp. If you want to
  1893. use groups here, use shy groups.
  1894. Example: (\"\\.x?html\\\\='\" . \"firefox %s\")
  1895. \(\"\\(?:xhtml\\|html\\)\" . \"firefox %s\")
  1896. to open *.html and *.xhtml with firefox.
  1897. - Regular expression which contains (non-shy) groups:
  1898. Match links where the whole link, including \"::\", and
  1899. anything after that, matches the regexp.
  1900. In a custom command string, %1, %2, etc. are replaced with
  1901. the parts of the link that were matched by the groups.
  1902. For backwards compatibility, if a command string is given
  1903. that does not use any of the group matches, this case is
  1904. handled identically to the second one (i.e. match against
  1905. file name only).
  1906. In a custom function, you can access the group matches with
  1907. \(match-string n link).
  1908. Example: (\"\\.pdf::\\(\\d+\\)\\\\='\" . \"evince -p %1 %s\")
  1909. to open [[file:document.pdf::5]] with evince at page 5.
  1910. `directory' Matches a directory
  1911. `remote' Matches a remote file, accessible through tramp or efs.
  1912. Remote files most likely should be visited through Emacs
  1913. because external applications cannot handle such paths.
  1914. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1915. so all files Emacs knows how to handle. Using this with
  1916. command `emacs' will open most files in Emacs. Beware that this
  1917. will also open html files inside Emacs, unless you add
  1918. \(\"html\" . default) to the list as well.
  1919. `system' The system command to open files, like `open' on Windows
  1920. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1921. that will be selected if you call \\[org-open-at-point] with a double
  1922. \\[universal-argument] \\[universal-argument] prefix.
  1923. t Default for files not matched by any of the other options.
  1924. Possible values for the command are:
  1925. `emacs' The file will be visited by the current Emacs process.
  1926. `default' Use the default application for this file type, which is the
  1927. association for t in the list, most likely in the system-specific
  1928. part. This can be used to overrule an unwanted setting in the
  1929. system-specific variable.
  1930. `system' Use the system command for opening files, like \"open\".
  1931. This command is specified by the entry whose car is `system'.
  1932. Most likely, the system-specific version of this variable
  1933. does define this command, but you can overrule/replace it
  1934. here.
  1935. `mailcap' Use command specified in the mailcaps.
  1936. string A command to be executed by a shell; %s will be replaced
  1937. by the path to the file.
  1938. function A Lisp function, which will be called with two arguments:
  1939. the file path and the original link string, without the
  1940. \"file:\" prefix.
  1941. For more examples, see the system specific constants
  1942. `org-file-apps-defaults-macosx'
  1943. `org-file-apps-defaults-windowsnt'
  1944. `org-file-apps-defaults-gnu'."
  1945. :group 'org-link-follow
  1946. :type '(repeat
  1947. (cons (choice :value ""
  1948. (string :tag "Extension")
  1949. (const :tag "System command to open files" system)
  1950. (const :tag "Default for unrecognized files" t)
  1951. (const :tag "Remote file" remote)
  1952. (const :tag "Links to a directory" directory)
  1953. (const :tag "Any files that have Emacs modes"
  1954. auto-mode))
  1955. (choice :value ""
  1956. (const :tag "Visit with Emacs" emacs)
  1957. (const :tag "Use default" default)
  1958. (const :tag "Use the system command" system)
  1959. (string :tag "Command")
  1960. (function :tag "Function")))))
  1961. (defcustom org-doi-server-url "http://dx.doi.org/"
  1962. "The URL of the DOI server."
  1963. :type 'string
  1964. :version "24.3"
  1965. :group 'org-link-follow)
  1966. (defgroup org-refile nil
  1967. "Options concerning refiling entries in Org-mode."
  1968. :tag "Org Refile"
  1969. :group 'org)
  1970. (defcustom org-directory "~/org"
  1971. "Directory with Org files.
  1972. This is just a default location to look for Org files. There is no need
  1973. at all to put your files into this directory. It is used in the
  1974. following situations:
  1975. 1. When a capture template specifies a target file that is not an
  1976. absolute path. The path will then be interpreted relative to
  1977. `org-directory'
  1978. 2. When the value of variable `org-agenda-files' is a single file, any
  1979. relative paths in this file will be taken as relative to
  1980. `org-directory'."
  1981. :group 'org-refile
  1982. :group 'org-capture
  1983. :type 'directory)
  1984. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1985. "Default target for storing notes.
  1986. Used as a fall back file for org-capture.el, for templates that
  1987. do not specify a target file."
  1988. :group 'org-refile
  1989. :group 'org-capture
  1990. :type 'file)
  1991. (defcustom org-goto-interface 'outline
  1992. "The default interface to be used for `org-goto'.
  1993. Allowed values are:
  1994. outline The interface shows an outline of the relevant file
  1995. and the correct heading is found by moving through
  1996. the outline or by searching with incremental search.
  1997. outline-path-completion Headlines in the current buffer are offered via
  1998. completion. This is the interface also used by
  1999. the refile command."
  2000. :group 'org-refile
  2001. :type '(choice
  2002. (const :tag "Outline" outline)
  2003. (const :tag "Outline-path-completion" outline-path-completion)))
  2004. (defcustom org-goto-max-level 5
  2005. "Maximum target level when running `org-goto' with refile interface."
  2006. :group 'org-refile
  2007. :type 'integer)
  2008. (defcustom org-reverse-note-order nil
  2009. "Non-nil means store new notes at the beginning of a file or entry.
  2010. When nil, new notes will be filed to the end of a file or entry.
  2011. This can also be a list with cons cells of regular expressions that
  2012. are matched against file names, and values."
  2013. :group 'org-capture
  2014. :group 'org-refile
  2015. :type '(choice
  2016. (const :tag "Reverse always" t)
  2017. (const :tag "Reverse never" nil)
  2018. (repeat :tag "By file name regexp"
  2019. (cons regexp boolean))))
  2020. (defcustom org-log-refile nil
  2021. "Information to record when a task is refiled.
  2022. Possible values are:
  2023. nil Don't add anything
  2024. time Add a time stamp to the task
  2025. note Prompt for a note and add it with template `org-log-note-headings'
  2026. This option can also be set with on a per-file-basis with
  2027. #+STARTUP: nologrefile
  2028. #+STARTUP: logrefile
  2029. #+STARTUP: lognoterefile
  2030. You can have local logging settings for a subtree by setting the LOGGING
  2031. property to one or more of these keywords.
  2032. When bulk-refiling from the agenda, the value `note' is forbidden and
  2033. will temporarily be changed to `time'."
  2034. :group 'org-refile
  2035. :group 'org-progress
  2036. :version "24.1"
  2037. :type '(choice
  2038. (const :tag "No logging" nil)
  2039. (const :tag "Record timestamp" time)
  2040. (const :tag "Record timestamp with note." note)))
  2041. (defcustom org-refile-targets nil
  2042. "Targets for refiling entries with \\[org-refile].
  2043. This is a list of cons cells. Each cell contains:
  2044. - a specification of the files to be considered, either a list of files,
  2045. or a symbol whose function or variable value will be used to retrieve
  2046. a file name or a list of file names. If you use `org-agenda-files' for
  2047. that, all agenda files will be scanned for targets. Nil means consider
  2048. headings in the current buffer.
  2049. - A specification of how to find candidate refile targets. This may be
  2050. any of:
  2051. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  2052. This tag has to be present in all target headlines, inheritance will
  2053. not be considered.
  2054. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  2055. todo keyword.
  2056. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  2057. headlines that are refiling targets.
  2058. - a cons cell (:level . N). Any headline of level N is considered a target.
  2059. Note that, when `org-odd-levels-only' is set, level corresponds to
  2060. order in hierarchy, not to the number of stars.
  2061. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  2062. Note that, when `org-odd-levels-only' is set, level corresponds to
  2063. order in hierarchy, not to the number of stars.
  2064. Each element of this list generates a set of possible targets.
  2065. The union of these sets is presented (with completion) to
  2066. the user by `org-refile'.
  2067. You can set the variable `org-refile-target-verify-function' to a function
  2068. to verify each headline found by the simple criteria above.
  2069. When this variable is nil, all top-level headlines in the current buffer
  2070. are used, equivalent to the value `((nil . (:level . 1))'."
  2071. :group 'org-refile
  2072. :type '(repeat
  2073. (cons
  2074. (choice :value org-agenda-files
  2075. (const :tag "All agenda files" org-agenda-files)
  2076. (const :tag "Current buffer" nil)
  2077. (function) (variable) (file))
  2078. (choice :tag "Identify target headline by"
  2079. (cons :tag "Specific tag" (const :value :tag) (string))
  2080. (cons :tag "TODO keyword" (const :value :todo) (string))
  2081. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  2082. (cons :tag "Level number" (const :value :level) (integer))
  2083. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  2084. (defcustom org-refile-target-verify-function nil
  2085. "Function to verify if the headline at point should be a refile target.
  2086. The function will be called without arguments, with point at the
  2087. beginning of the headline. It should return t and leave point
  2088. where it is if the headline is a valid target for refiling.
  2089. If the target should not be selected, the function must return nil.
  2090. In addition to this, it may move point to a place from where the search
  2091. should be continued. For example, the function may decide that the entire
  2092. subtree of the current entry should be excluded and move point to the end
  2093. of the subtree."
  2094. :group 'org-refile
  2095. :type '(choice
  2096. (const nil)
  2097. (function)))
  2098. (defcustom org-refile-use-cache nil
  2099. "Non-nil means cache refile targets to speed up the process.
  2100. \\<org-mode-map>\
  2101. The cache for a particular file will be updated automatically when
  2102. the buffer has been killed, or when any of the marker used for flagging
  2103. refile targets no longer points at a live buffer.
  2104. If you have added new entries to a buffer that might themselves be targets,
  2105. you need to clear the cache manually by pressing `C-0 \\[org-refile]' or,
  2106. if you find that easier, \
  2107. `\\[universal-argument] \\[universal-argument] \\[universal-argument] \
  2108. \\[org-refile]'."
  2109. :group 'org-refile
  2110. :version "24.1"
  2111. :type 'boolean)
  2112. (defcustom org-refile-use-outline-path nil
  2113. "Non-nil means provide refile targets as paths.
  2114. So a level 3 headline will be available as level1/level2/level3.
  2115. When the value is `file', also include the file name (without directory)
  2116. into the path. In this case, you can also stop the completion after
  2117. the file name, to get entries inserted as top level in the file.
  2118. When `full-file-path', include the full file path."
  2119. :group 'org-refile
  2120. :type '(choice
  2121. (const :tag "Not" nil)
  2122. (const :tag "Yes" t)
  2123. (const :tag "Start with file name" file)
  2124. (const :tag "Start with full file path" full-file-path)))
  2125. (defcustom org-outline-path-complete-in-steps t
  2126. "Non-nil means complete the outline path in hierarchical steps.
  2127. When Org-mode uses the refile interface to select an outline path
  2128. \(see variable `org-refile-use-outline-path'), the completion of
  2129. the path can be done in a single go, or it can be done in steps down
  2130. the headline hierarchy. Going in steps is probably the best if you
  2131. do not use a special completion package like `ido' or `icicles'.
  2132. However, when using these packages, going in one step can be very
  2133. fast, while still showing the whole path to the entry."
  2134. :group 'org-refile
  2135. :type 'boolean)
  2136. (defcustom org-refile-allow-creating-parent-nodes nil
  2137. "Non-nil means allow the creation of new nodes as refile targets.
  2138. New nodes are then created by adding \"/new node name\" to the completion
  2139. of an existing node. When the value of this variable is `confirm',
  2140. new node creation must be confirmed by the user (recommended).
  2141. When nil, the completion must match an existing entry.
  2142. Note that, if the new heading is not seen by the criteria
  2143. listed in `org-refile-targets', multiple instances of the same
  2144. heading would be created by trying again to file under the new
  2145. heading."
  2146. :group 'org-refile
  2147. :type '(choice
  2148. (const :tag "Never" nil)
  2149. (const :tag "Always" t)
  2150. (const :tag "Prompt for confirmation" confirm)))
  2151. (defcustom org-refile-active-region-within-subtree nil
  2152. "Non-nil means also refile active region within a subtree.
  2153. By default `org-refile' doesn't allow refiling regions if they
  2154. don't contain a set of subtrees, but it might be convenient to
  2155. do so sometimes: in that case, the first line of the region is
  2156. converted to a headline before refiling."
  2157. :group 'org-refile
  2158. :version "24.1"
  2159. :type 'boolean)
  2160. (defgroup org-todo nil
  2161. "Options concerning TODO items in Org-mode."
  2162. :tag "Org TODO"
  2163. :group 'org)
  2164. (defgroup org-progress nil
  2165. "Options concerning Progress logging in Org-mode."
  2166. :tag "Org Progress"
  2167. :group 'org-time)
  2168. (defvar org-todo-interpretation-widgets
  2169. '((:tag "Sequence (cycling hits every state)" sequence)
  2170. (:tag "Type (cycling directly to DONE)" type))
  2171. "The available interpretation symbols for customizing `org-todo-keywords'.
  2172. Interested libraries should add to this list.")
  2173. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  2174. "List of TODO entry keyword sequences and their interpretation.
  2175. \\<org-mode-map>This is a list of sequences.
  2176. Each sequence starts with a symbol, either `sequence' or `type',
  2177. indicating if the keywords should be interpreted as a sequence of
  2178. action steps, or as different types of TODO items. The first
  2179. keywords are states requiring action - these states will select a headline
  2180. for inclusion into the global TODO list Org-mode produces. If one of
  2181. the \"keywords\" is the vertical bar, \"|\", the remaining keywords
  2182. signify that no further action is necessary. If \"|\" is not found,
  2183. the last keyword is treated as the only DONE state of the sequence.
  2184. The command \\[org-todo] cycles an entry through these states, and one
  2185. additional state where no keyword is present. For details about this
  2186. cycling, see the manual.
  2187. TODO keywords and interpretation can also be set on a per-file basis with
  2188. the special #+SEQ_TODO and #+TYP_TODO lines.
  2189. Each keyword can optionally specify a character for fast state selection
  2190. \(in combination with the variable `org-use-fast-todo-selection')
  2191. and specifiers for state change logging, using the same syntax that
  2192. is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says that
  2193. the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  2194. indicates to record a time stamp each time this state is selected.
  2195. Each keyword may also specify if a timestamp or a note should be
  2196. recorded when entering or leaving the state, by adding additional
  2197. characters in the parenthesis after the keyword. This looks like this:
  2198. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  2199. record only the time of the state change. With X and Y being either
  2200. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  2201. Y when leaving the state if and only if the *target* state does not
  2202. define X. You may omit any of the fast-selection key or X or /Y,
  2203. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  2204. For backward compatibility, this variable may also be just a list
  2205. of keywords. In this case the interpretation (sequence or type) will be
  2206. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  2207. :group 'org-todo
  2208. :group 'org-keywords
  2209. :type '(choice
  2210. (repeat :tag "Old syntax, just keywords"
  2211. (string :tag "Keyword"))
  2212. (repeat :tag "New syntax"
  2213. (cons
  2214. (choice
  2215. :tag "Interpretation"
  2216. ;;Quick and dirty way to see
  2217. ;;`org-todo-interpretations'. This takes the
  2218. ;;place of item arguments
  2219. :convert-widget
  2220. (lambda (widget)
  2221. (widget-put widget
  2222. :args (mapcar
  2223. (lambda (x)
  2224. (widget-convert
  2225. (cons 'const x)))
  2226. org-todo-interpretation-widgets))
  2227. widget))
  2228. (repeat
  2229. (string :tag "Keyword"))))))
  2230. (defvar-local org-todo-keywords-1 nil
  2231. "All TODO and DONE keywords active in a buffer.")
  2232. (defvar org-todo-keywords-for-agenda nil)
  2233. (defvar org-done-keywords-for-agenda nil)
  2234. (defvar org-todo-keyword-alist-for-agenda nil)
  2235. (defvar org-tag-alist-for-agenda nil
  2236. "Alist of all tags from all agenda files.")
  2237. (defvar org-tag-groups-alist-for-agenda nil
  2238. "Alist of all groups tags from all current agenda files.")
  2239. (defvar-local org-tag-groups-alist nil)
  2240. (defvar org-agenda-contributing-files nil)
  2241. (defvar-local org-not-done-keywords nil)
  2242. (defvar-local org-done-keywords nil)
  2243. (defvar-local org-todo-heads nil)
  2244. (defvar-local org-todo-sets nil)
  2245. (defvar-local org-todo-log-states nil)
  2246. (defvar-local org-todo-kwd-alist nil)
  2247. (defvar-local org-todo-key-alist nil)
  2248. (defvar-local org-todo-key-trigger nil)
  2249. (defcustom org-todo-interpretation 'sequence
  2250. "Controls how TODO keywords are interpreted.
  2251. This variable is in principle obsolete and is only used for
  2252. backward compatibility, if the interpretation of todo keywords is
  2253. not given already in `org-todo-keywords'. See that variable for
  2254. more information."
  2255. :group 'org-todo
  2256. :group 'org-keywords
  2257. :type '(choice (const sequence)
  2258. (const type)))
  2259. (defcustom org-use-fast-todo-selection t
  2260. "\\<org-mode-map>\
  2261. Non-nil means use the fast todo selection scheme with \\[org-todo].
  2262. This variable describes if and under what circumstances the cycling
  2263. mechanism for TODO keywords will be replaced by a single-key, direct
  2264. selection scheme.
  2265. When nil, fast selection is never used.
  2266. When the symbol `prefix', it will be used when `org-todo' is called
  2267. with a prefix argument, i.e. `\\[universal-argument] \\[org-todo]' \
  2268. in an Org-mode buffer, and
  2269. `\\[universal-argument] t' in an agenda buffer.
  2270. When t, fast selection is used by default. In this case, the prefix
  2271. argument forces cycling instead.
  2272. In all cases, the special interface is only used if access keys have
  2273. actually been assigned by the user, i.e. if keywords in the configuration
  2274. are followed by a letter in parenthesis, like TODO(t)."
  2275. :group 'org-todo
  2276. :type '(choice
  2277. (const :tag "Never" nil)
  2278. (const :tag "By default" t)
  2279. (const :tag "Only with C-u C-c C-t" prefix)))
  2280. (defcustom org-provide-todo-statistics t
  2281. "Non-nil means update todo statistics after insert and toggle.
  2282. ALL-HEADLINES means update todo statistics by including headlines
  2283. with no TODO keyword as well, counting them as not done.
  2284. A list of TODO keywords means the same, but skip keywords that are
  2285. not in this list.
  2286. When set to a list of two lists, the first list contains keywords
  2287. to consider as TODO keywords, the second list contains keywords
  2288. to consider as DONE keywords.
  2289. When this is set, todo statistics is updated in the parent of the
  2290. current entry each time a todo state is changed."
  2291. :group 'org-todo
  2292. :type '(choice
  2293. (const :tag "Yes, only for TODO entries" t)
  2294. (const :tag "Yes, including all entries" all-headlines)
  2295. (repeat :tag "Yes, for TODOs in this list"
  2296. (string :tag "TODO keyword"))
  2297. (list :tag "Yes, for TODOs and DONEs in these lists"
  2298. (repeat (string :tag "TODO keyword"))
  2299. (repeat (string :tag "DONE keyword")))
  2300. (other :tag "No TODO statistics" nil)))
  2301. (defcustom org-hierarchical-todo-statistics t
  2302. "Non-nil means TODO statistics covers just direct children.
  2303. When nil, all entries in the subtree are considered.
  2304. This has only an effect if `org-provide-todo-statistics' is set.
  2305. To set this to nil for only a single subtree, use a COOKIE_DATA
  2306. property and include the word \"recursive\" into the value."
  2307. :group 'org-todo
  2308. :type 'boolean)
  2309. (defcustom org-after-todo-state-change-hook nil
  2310. "Hook which is run after the state of a TODO item was changed.
  2311. The new state (a string with a TODO keyword, or nil) is available in the
  2312. Lisp variable `org-state'."
  2313. :group 'org-todo
  2314. :type 'hook)
  2315. (defvar org-blocker-hook nil
  2316. "Hook for functions that are allowed to block a state change.
  2317. Functions in this hook should not modify the buffer.
  2318. Each function gets as its single argument a property list,
  2319. see `org-trigger-hook' for more information about this list.
  2320. If any of the functions in this hook returns nil, the state change
  2321. is blocked.")
  2322. (defvar org-trigger-hook nil
  2323. "Hook for functions that are triggered by a state change.
  2324. Each function gets as its single argument a property list with at
  2325. least the following elements:
  2326. (:type type-of-change :position pos-at-entry-start
  2327. :from old-state :to new-state)
  2328. Depending on the type, more properties may be present.
  2329. This mechanism is currently implemented for:
  2330. TODO state changes
  2331. ------------------
  2332. :type todo-state-change
  2333. :from previous state (keyword as a string), or nil, or a symbol
  2334. `todo' or `done', to indicate the general type of state.
  2335. :to new state, like in :from")
  2336. (defcustom org-enforce-todo-dependencies nil
  2337. "Non-nil means undone TODO entries will block switching the parent to DONE.
  2338. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  2339. be blocked if any prior sibling is not yet done.
  2340. Finally, if the parent is blocked because of ordered siblings of its own,
  2341. the child will also be blocked."
  2342. :set (lambda (var val)
  2343. (set var val)
  2344. (if val
  2345. (add-hook 'org-blocker-hook
  2346. 'org-block-todo-from-children-or-siblings-or-parent)
  2347. (remove-hook 'org-blocker-hook
  2348. 'org-block-todo-from-children-or-siblings-or-parent)))
  2349. :group 'org-todo
  2350. :type 'boolean)
  2351. (defcustom org-enforce-todo-checkbox-dependencies nil
  2352. "Non-nil means unchecked boxes will block switching the parent to DONE.
  2353. When this is nil, checkboxes have no influence on switching TODO states.
  2354. When non-nil, you first need to check off all check boxes before the TODO
  2355. entry can be switched to DONE.
  2356. This variable needs to be set before org.el is loaded, and you need to
  2357. restart Emacs after a change to make the change effective. The only way
  2358. to change is while Emacs is running is through the customize interface."
  2359. :set (lambda (var val)
  2360. (set var val)
  2361. (if val
  2362. (add-hook 'org-blocker-hook
  2363. 'org-block-todo-from-checkboxes)
  2364. (remove-hook 'org-blocker-hook
  2365. 'org-block-todo-from-checkboxes)))
  2366. :group 'org-todo
  2367. :type 'boolean)
  2368. (defcustom org-treat-insert-todo-heading-as-state-change nil
  2369. "Non-nil means inserting a TODO heading is treated as state change.
  2370. So when the command \\[org-insert-todo-heading] is used, state change
  2371. logging will apply if appropriate. When nil, the new TODO item will
  2372. be inserted directly, and no logging will take place."
  2373. :group 'org-todo
  2374. :type 'boolean)
  2375. (defcustom org-treat-S-cursor-todo-selection-as-state-change t
  2376. "Non-nil means switching TODO states with S-cursor counts as state change.
  2377. This is the default behavior. However, setting this to nil allows a
  2378. convenient way to select a TODO state and bypass any logging associated
  2379. with that."
  2380. :group 'org-todo
  2381. :type 'boolean)
  2382. (defcustom org-todo-state-tags-triggers nil
  2383. "Tag changes that should be triggered by TODO state changes.
  2384. This is a list. Each entry is
  2385. (state-change (tag . flag) .......)
  2386. State-change can be a string with a state, and empty string to indicate the
  2387. state that has no TODO keyword, or it can be one of the symbols `todo'
  2388. or `done', meaning any not-done or done state, respectively."
  2389. :group 'org-todo
  2390. :group 'org-tags
  2391. :type '(repeat
  2392. (cons (choice :tag "When changing to"
  2393. (const :tag "Not-done state" todo)
  2394. (const :tag "Done state" done)
  2395. (string :tag "State"))
  2396. (repeat
  2397. (cons :tag "Tag action"
  2398. (string :tag "Tag")
  2399. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  2400. (defcustom org-log-done nil
  2401. "Information to record when a task moves to the DONE state.
  2402. Possible values are:
  2403. nil Don't add anything, just change the keyword
  2404. time Add a time stamp to the task
  2405. note Prompt for a note and add it with template `org-log-note-headings'
  2406. This option can also be set with on a per-file-basis with
  2407. #+STARTUP: nologdone
  2408. #+STARTUP: logdone
  2409. #+STARTUP: lognotedone
  2410. You can have local logging settings for a subtree by setting the LOGGING
  2411. property to one or more of these keywords."
  2412. :group 'org-todo
  2413. :group 'org-progress
  2414. :type '(choice
  2415. (const :tag "No logging" nil)
  2416. (const :tag "Record CLOSED timestamp" time)
  2417. (const :tag "Record CLOSED timestamp with note." note)))
  2418. ;; Normalize old uses of org-log-done.
  2419. (cond
  2420. ((eq org-log-done t) (setq org-log-done 'time))
  2421. ((and (listp org-log-done) (memq 'done org-log-done))
  2422. (setq org-log-done 'note)))
  2423. (defcustom org-log-reschedule nil
  2424. "Information to record when the scheduling date of a tasks is modified.
  2425. Possible values are:
  2426. nil Don't add anything, just change the date
  2427. time Add a time stamp to the task
  2428. note Prompt for a note and add it with template `org-log-note-headings'
  2429. This option can also be set with on a per-file-basis with
  2430. #+STARTUP: nologreschedule
  2431. #+STARTUP: logreschedule
  2432. #+STARTUP: lognotereschedule"
  2433. :group 'org-todo
  2434. :group 'org-progress
  2435. :type '(choice
  2436. (const :tag "No logging" nil)
  2437. (const :tag "Record timestamp" time)
  2438. (const :tag "Record timestamp with note." note)))
  2439. (defcustom org-log-redeadline nil
  2440. "Information to record when the deadline date of a tasks is modified.
  2441. Possible values are:
  2442. nil Don't add anything, just change the date
  2443. time Add a time stamp to the task
  2444. note Prompt for a note and add it with template `org-log-note-headings'
  2445. This option can also be set with on a per-file-basis with
  2446. #+STARTUP: nologredeadline
  2447. #+STARTUP: logredeadline
  2448. #+STARTUP: lognoteredeadline
  2449. You can have local logging settings for a subtree by setting the LOGGING
  2450. property to one or more of these keywords."
  2451. :group 'org-todo
  2452. :group 'org-progress
  2453. :type '(choice
  2454. (const :tag "No logging" nil)
  2455. (const :tag "Record timestamp" time)
  2456. (const :tag "Record timestamp with note." note)))
  2457. (defcustom org-log-note-clock-out nil
  2458. "Non-nil means record a note when clocking out of an item.
  2459. This can also be configured on a per-file basis by adding one of
  2460. the following lines anywhere in the buffer:
  2461. #+STARTUP: lognoteclock-out
  2462. #+STARTUP: nolognoteclock-out"
  2463. :group 'org-todo
  2464. :group 'org-progress
  2465. :type 'boolean)
  2466. (defcustom org-log-done-with-time t
  2467. "Non-nil means the CLOSED time stamp will contain date and time.
  2468. When nil, only the date will be recorded."
  2469. :group 'org-progress
  2470. :type 'boolean)
  2471. (defcustom org-log-note-headings
  2472. '((done . "CLOSING NOTE %t")
  2473. (state . "State %-12s from %-12S %t")
  2474. (note . "Note taken on %t")
  2475. (reschedule . "Rescheduled from %S on %t")
  2476. (delschedule . "Not scheduled, was %S on %t")
  2477. (redeadline . "New deadline from %S on %t")
  2478. (deldeadline . "Removed deadline, was %S on %t")
  2479. (refile . "Refiled on %t")
  2480. (clock-out . ""))
  2481. "Headings for notes added to entries.
  2482. The value is an alist, with the car being a symbol indicating the
  2483. note context, and the cdr is the heading to be used. The heading
  2484. may also be the empty string. The following placeholders can be
  2485. used:
  2486. %t a time stamp.
  2487. %T an active time stamp instead the default inactive one
  2488. %d a short-format time stamp.
  2489. %D an active short-format time stamp.
  2490. %s the new TODO state or time stamp (inactive), in double quotes.
  2491. %S the old TODO state or time stamp (inactive), in double quotes.
  2492. %u the user name.
  2493. %U full user name.
  2494. In fact, it is not a good idea to change the `state' entry,
  2495. because Agenda Log mode depends on the format of these entries."
  2496. :group 'org-todo
  2497. :group 'org-progress
  2498. :type '(list :greedy t
  2499. (cons (const :tag "Heading when closing an item" done) string)
  2500. (cons (const :tag
  2501. "Heading when changing todo state (todo sequence only)"
  2502. state) string)
  2503. (cons (const :tag "Heading when just taking a note" note) string)
  2504. (cons (const :tag "Heading when rescheduling" reschedule) string)
  2505. (cons (const :tag "Heading when an item is no longer scheduled" delschedule) string)
  2506. (cons (const :tag "Heading when changing deadline" redeadline) string)
  2507. (cons (const :tag "Heading when deleting a deadline" deldeadline) string)
  2508. (cons (const :tag "Heading when refiling" refile) string)
  2509. (cons (const :tag "Heading when clocking out" clock-out) string)))
  2510. (unless (assq 'note org-log-note-headings)
  2511. (push '(note . "%t") org-log-note-headings))
  2512. (defcustom org-log-into-drawer nil
  2513. "Non-nil means insert state change notes and time stamps into a drawer.
  2514. When nil, state changes notes will be inserted after the headline and
  2515. any scheduling and clock lines, but not inside a drawer.
  2516. The value of this variable should be the name of the drawer to use.
  2517. LOGBOOK is proposed as the default drawer for this purpose, you can
  2518. also set this to a string to define the drawer of your choice.
  2519. A value of t is also allowed, representing \"LOGBOOK\".
  2520. A value of t or nil can also be set with on a per-file-basis with
  2521. #+STARTUP: logdrawer
  2522. #+STARTUP: nologdrawer
  2523. If this variable is set, `org-log-state-notes-insert-after-drawers'
  2524. will be ignored.
  2525. You can set the property LOG_INTO_DRAWER to overrule this setting for
  2526. a subtree.
  2527. Do not check directly this variable in a Lisp program. Call
  2528. function `org-log-into-drawer' instead."
  2529. :group 'org-todo
  2530. :group 'org-progress
  2531. :type '(choice
  2532. (const :tag "Not into a drawer" nil)
  2533. (const :tag "LOGBOOK" t)
  2534. (string :tag "Other")))
  2535. (org-defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer)
  2536. (defun org-log-into-drawer ()
  2537. "Name of the log drawer, as a string, or nil.
  2538. This is the value of `org-log-into-drawer'. However, if the
  2539. current entry has or inherits a LOG_INTO_DRAWER property, it will
  2540. be used instead of the default value."
  2541. (let ((p (org-entry-get nil "LOG_INTO_DRAWER" 'inherit t)))
  2542. (cond ((equal p "nil") nil)
  2543. ((equal p "t") "LOGBOOK")
  2544. ((stringp p) p)
  2545. (p "LOGBOOK")
  2546. ((stringp org-log-into-drawer) org-log-into-drawer)
  2547. (org-log-into-drawer "LOGBOOK"))))
  2548. (defcustom org-log-state-notes-insert-after-drawers nil
  2549. "Non-nil means insert state change notes after any drawers in entry.
  2550. Only the drawers that *immediately* follow the headline and the
  2551. deadline/scheduled line are skipped.
  2552. When nil, insert notes right after the heading and perhaps the line
  2553. with deadline/scheduling if present.
  2554. This variable will have no effect if `org-log-into-drawer' is
  2555. set."
  2556. :group 'org-todo
  2557. :group 'org-progress
  2558. :type 'boolean)
  2559. (defcustom org-log-states-order-reversed t
  2560. "Non-nil means the latest state note will be directly after heading.
  2561. When nil, the state change notes will be ordered according to time.
  2562. This option can also be set with on a per-file-basis with
  2563. #+STARTUP: logstatesreversed
  2564. #+STARTUP: nologstatesreversed"
  2565. :group 'org-todo
  2566. :group 'org-progress
  2567. :type 'boolean)
  2568. (defcustom org-todo-repeat-to-state nil
  2569. "The TODO state to which a repeater should return the repeating task.
  2570. By default this is the first task in a TODO sequence, or the previous state
  2571. in a TODO_TYP set. But you can specify another task here.
  2572. alternatively, set the :REPEAT_TO_STATE: property of the entry."
  2573. :group 'org-todo
  2574. :version "24.1"
  2575. :type '(choice (const :tag "Head of sequence" nil)
  2576. (string :tag "Specific state")))
  2577. (defcustom org-log-repeat 'time
  2578. "Non-nil means record moving through the DONE state when triggering repeat.
  2579. An auto-repeating task is immediately switched back to TODO when
  2580. marked DONE. If you are not logging state changes (by adding \"@\"
  2581. or \"!\" to the TODO keyword definition), or set `org-log-done' to
  2582. record a closing note, there will be no record of the task moving
  2583. through DONE. This variable forces taking a note anyway.
  2584. nil Don't force a record
  2585. time Record a time stamp
  2586. note Prompt for a note and add it with template `org-log-note-headings'
  2587. This option can also be set with on a per-file-basis with
  2588. #+STARTUP: nologrepeat
  2589. #+STARTUP: logrepeat
  2590. #+STARTUP: lognoterepeat
  2591. You can have local logging settings for a subtree by setting the LOGGING
  2592. property to one or more of these keywords."
  2593. :group 'org-todo
  2594. :group 'org-progress
  2595. :type '(choice
  2596. (const :tag "Don't force a record" nil)
  2597. (const :tag "Force recording the DONE state" time)
  2598. (const :tag "Force recording a note with the DONE state" note)))
  2599. (defgroup org-priorities nil
  2600. "Priorities in Org-mode."
  2601. :tag "Org Priorities"
  2602. :group 'org-todo)
  2603. (defcustom org-enable-priority-commands t
  2604. "Non-nil means priority commands are active.
  2605. When nil, these commands will be disabled, so that you never accidentally
  2606. set a priority."
  2607. :group 'org-priorities
  2608. :type 'boolean)
  2609. (defcustom org-highest-priority ?A
  2610. "The highest priority of TODO items. A character like ?A, ?B etc.
  2611. Must have a smaller ASCII number than `org-lowest-priority'."
  2612. :group 'org-priorities
  2613. :type 'character)
  2614. (defcustom org-lowest-priority ?C
  2615. "The lowest priority of TODO items. A character like ?A, ?B etc.
  2616. Must have a larger ASCII number than `org-highest-priority'."
  2617. :group 'org-priorities
  2618. :type 'character)
  2619. (defcustom org-default-priority ?B
  2620. "The default priority of TODO items.
  2621. This is the priority an item gets if no explicit priority is given.
  2622. When starting to cycle on an empty priority the first step in the cycle
  2623. depends on `org-priority-start-cycle-with-default'. The resulting first
  2624. step priority must not exceed the range from `org-highest-priority' to
  2625. `org-lowest-priority' which means that `org-default-priority' has to be
  2626. in this range exclusive or inclusive the range boundaries. Else the
  2627. first step refuses to set the default and the second will fall back
  2628. to (depending on the command used) the highest or lowest priority."
  2629. :group 'org-priorities
  2630. :type 'character)
  2631. (defcustom org-priority-start-cycle-with-default t
  2632. "Non-nil means start with default priority when starting to cycle.
  2633. When this is nil, the first step in the cycle will be (depending on the
  2634. command used) one higher or lower than the default priority.
  2635. See also `org-default-priority'."
  2636. :group 'org-priorities
  2637. :type 'boolean)
  2638. (defcustom org-get-priority-function nil
  2639. "Function to extract the priority from a string.
  2640. The string is normally the headline. If this is nil Org computes the
  2641. priority from the priority cookie like [#A] in the headline. It returns
  2642. an integer, increasing by 1000 for each priority level.
  2643. The user can set a different function here, which should take a string
  2644. as an argument and return the numeric priority."
  2645. :group 'org-priorities
  2646. :version "24.1"
  2647. :type '(choice
  2648. (const nil)
  2649. (function)))
  2650. (defgroup org-time nil
  2651. "Options concerning time stamps and deadlines in Org-mode."
  2652. :tag "Org Time"
  2653. :group 'org)
  2654. (defcustom org-time-stamp-rounding-minutes '(0 5)
  2655. "Number of minutes to round time stamps to.
  2656. \\<org-mode-map>\
  2657. These are two values, the first applies when first creating a time stamp.
  2658. The second applies when changing it with the commands `S-up' and `S-down'.
  2659. When changing the time stamp, this means that it will change in steps
  2660. of N minutes, as given by the second value.
  2661. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  2662. numbers should be factors of 60, so for example 5, 10, 15.
  2663. When this is larger than 1, you can still force an exact time stamp by using
  2664. a double prefix argument to a time stamp command like \
  2665. `\\[org-time-stamp]' or `\\[org-time-stamp-inactive],
  2666. and by using a prefix arg to `S-up/down' to specify the exact number
  2667. of minutes to shift."
  2668. :group 'org-time
  2669. :get (lambda (var) ; Make sure both elements are there
  2670. (if (integerp (default-value var))
  2671. (list (default-value var) 5)
  2672. (default-value var)))
  2673. :type '(list
  2674. (integer :tag "when inserting times")
  2675. (integer :tag "when modifying times")))
  2676. ;; Normalize old customizations of this variable.
  2677. (when (integerp org-time-stamp-rounding-minutes)
  2678. (setq org-time-stamp-rounding-minutes
  2679. (list org-time-stamp-rounding-minutes
  2680. org-time-stamp-rounding-minutes)))
  2681. (defcustom org-display-custom-times nil
  2682. "Non-nil means overlay custom formats over all time stamps.
  2683. The formats are defined through the variable `org-time-stamp-custom-formats'.
  2684. To turn this on on a per-file basis, insert anywhere in the file:
  2685. #+STARTUP: customtime"
  2686. :group 'org-time
  2687. :set 'set-default
  2688. :type 'sexp)
  2689. (make-variable-buffer-local 'org-display-custom-times)
  2690. (defcustom org-time-stamp-custom-formats
  2691. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  2692. "Custom formats for time stamps. See `format-time-string' for the syntax.
  2693. These are overlaid over the default ISO format if the variable
  2694. `org-display-custom-times' is set. Time like %H:%M should be at the
  2695. end of the second format. The custom formats are also honored by export
  2696. commands, if custom time display is turned on at the time of export."
  2697. :group 'org-time
  2698. :type 'sexp)
  2699. (defun org-time-stamp-format (&optional long inactive)
  2700. "Get the right format for a time string."
  2701. (let ((f (if long (cdr org-time-stamp-formats)
  2702. (car org-time-stamp-formats))))
  2703. (if inactive
  2704. (concat "[" (substring f 1 -1) "]")
  2705. f)))
  2706. (defcustom org-time-clocksum-format
  2707. '(:days "%dd " :hours "%d" :require-hours t :minutes ":%02d" :require-minutes t)
  2708. "The format string used when creating CLOCKSUM lines.
  2709. This is also used when Org mode generates a time duration.
  2710. The value can be a single format string containing two
  2711. %-sequences, which will be filled with the number of hours and
  2712. minutes in that order.
  2713. Alternatively, the value can be a plist associating any of the
  2714. keys :years, :months, :weeks, :days, :hours or :minutes with
  2715. format strings. The time duration is formatted using only the
  2716. time components that are needed and concatenating the results.
  2717. If a time unit in absent, it falls back to the next smallest
  2718. unit.
  2719. The keys :require-years, :require-months, :require-days,
  2720. :require-weeks, :require-hours, :require-minutes are also
  2721. meaningful. A non-nil value for these keys indicates that the
  2722. corresponding time component should always be included, even if
  2723. its value is 0.
  2724. For example,
  2725. (:days \"%dd\" :hours \"%d\" :require-hours t :minutes \":%02d\"
  2726. :require-minutes t)
  2727. means durations longer than a day will be expressed in days,
  2728. hours and minutes, and durations less than a day will always be
  2729. expressed in hours and minutes (even for durations less than an
  2730. hour).
  2731. The value
  2732. (:days \"%dd\" :minutes \"%dm\")
  2733. means durations longer than a day will be expressed in days and
  2734. minutes, and durations less than a day will be expressed entirely
  2735. in minutes (even for durations longer than an hour)."
  2736. :group 'org-time
  2737. :group 'org-clock
  2738. :version "24.4"
  2739. :package-version '(Org . "8.0")
  2740. :type '(choice (string :tag "Format string")
  2741. (set :tag "Plist"
  2742. (group :inline t (const :tag "Years" :years)
  2743. (string :tag "Format string"))
  2744. (group :inline t
  2745. (const :tag "Always show years" :require-years)
  2746. (const t))
  2747. (group :inline t (const :tag "Months" :months)
  2748. (string :tag "Format string"))
  2749. (group :inline t
  2750. (const :tag "Always show months" :require-months)
  2751. (const t))
  2752. (group :inline t (const :tag "Weeks" :weeks)
  2753. (string :tag "Format string"))
  2754. (group :inline t
  2755. (const :tag "Always show weeks" :require-weeks)
  2756. (const t))
  2757. (group :inline t (const :tag "Days" :days)
  2758. (string :tag "Format string"))
  2759. (group :inline t
  2760. (const :tag "Always show days" :require-days)
  2761. (const t))
  2762. (group :inline t (const :tag "Hours" :hours)
  2763. (string :tag "Format string"))
  2764. (group :inline t
  2765. (const :tag "Always show hours" :require-hours)
  2766. (const t))
  2767. (group :inline t (const :tag "Minutes" :minutes)
  2768. (string :tag "Format string"))
  2769. (group :inline t
  2770. (const :tag "Always show minutes" :require-minutes)
  2771. (const t)))))
  2772. (defcustom org-time-clocksum-use-fractional nil
  2773. "When non-nil, \\[org-clock-display] uses fractional times.
  2774. See `org-time-clocksum-format' for more on time clock formats."
  2775. :group 'org-time
  2776. :group 'org-clock
  2777. :version "24.3"
  2778. :type 'boolean)
  2779. (defcustom org-time-clocksum-use-effort-durations nil
  2780. "When non-nil, \\[org-clock-display] uses effort durations.
  2781. E.g. by default, one day is considered to be a 8 hours effort,
  2782. so a task that has been clocked for 16 hours will be displayed
  2783. as during 2 days in the clock display or in the clocktable.
  2784. See `org-effort-durations' on how to set effort durations
  2785. and `org-time-clocksum-format' for more on time clock formats."
  2786. :group 'org-time
  2787. :group 'org-clock
  2788. :version "24.4"
  2789. :package-version '(Org . "8.0")
  2790. :type 'boolean)
  2791. (defcustom org-time-clocksum-fractional-format "%.2f"
  2792. "The format string used when creating CLOCKSUM lines,
  2793. or when Org mode generates a time duration, if
  2794. `org-time-clocksum-use-fractional' is enabled.
  2795. The value can be a single format string containing one
  2796. %-sequence, which will be filled with the number of hours as
  2797. a float.
  2798. Alternatively, the value can be a plist associating any of the
  2799. keys :years, :months, :weeks, :days, :hours or :minutes with
  2800. a format string. The time duration is formatted using the
  2801. largest time unit which gives a non-zero integer part. If all
  2802. specified formats have zero integer part, the smallest time unit
  2803. is used."
  2804. :group 'org-time
  2805. :type '(choice (string :tag "Format string")
  2806. (set (group :inline t (const :tag "Years" :years)
  2807. (string :tag "Format string"))
  2808. (group :inline t (const :tag "Months" :months)
  2809. (string :tag "Format string"))
  2810. (group :inline t (const :tag "Weeks" :weeks)
  2811. (string :tag "Format string"))
  2812. (group :inline t (const :tag "Days" :days)
  2813. (string :tag "Format string"))
  2814. (group :inline t (const :tag "Hours" :hours)
  2815. (string :tag "Format string"))
  2816. (group :inline t (const :tag "Minutes" :minutes)
  2817. (string :tag "Format string")))))
  2818. (defcustom org-deadline-warning-days 14
  2819. "Number of days before expiration during which a deadline becomes active.
  2820. This variable governs the display in sparse trees and in the agenda.
  2821. When 0 or negative, it means use this number (the absolute value of it)
  2822. even if a deadline has a different individual lead time specified.
  2823. Custom commands can set this variable in the options section."
  2824. :group 'org-time
  2825. :group 'org-agenda-daily/weekly
  2826. :type 'integer)
  2827. (defcustom org-scheduled-delay-days 0
  2828. "Number of days before a scheduled item becomes active.
  2829. This variable governs the display in sparse trees and in the agenda.
  2830. The default value (i.e. 0) means: don't delay scheduled item.
  2831. When negative, it means use this number (the absolute value of it)
  2832. even if a scheduled item has a different individual delay time
  2833. specified.
  2834. Custom commands can set this variable in the options section."
  2835. :group 'org-time
  2836. :group 'org-agenda-daily/weekly
  2837. :version "24.4"
  2838. :package-version '(Org . "8.0")
  2839. :type 'integer)
  2840. (defcustom org-read-date-prefer-future t
  2841. "Non-nil means assume future for incomplete date input from user.
  2842. This affects the following situations:
  2843. 1. The user gives a month but not a year.
  2844. For example, if it is April and you enter \"feb 2\", this will be read
  2845. as Feb 2, *next* year. \"May 5\", however, will be this year.
  2846. 2. The user gives a day, but no month.
  2847. For example, if today is the 15th, and you enter \"3\", Org-mode will
  2848. read this as the third of *next* month. However, if you enter \"17\",
  2849. it will be considered as *this* month.
  2850. If you set this variable to the symbol `time', then also the following
  2851. will work:
  2852. 3. If the user gives a time.
  2853. If the time is before now, it will be interpreted as tomorrow.
  2854. Currently none of this works for ISO week specifications.
  2855. When this option is nil, the current day, month and year will always be
  2856. used as defaults.
  2857. See also `org-agenda-jump-prefer-future'."
  2858. :group 'org-time
  2859. :type '(choice
  2860. (const :tag "Never" nil)
  2861. (const :tag "Check month and day" t)
  2862. (const :tag "Check month, day, and time" time)))
  2863. (defcustom org-agenda-jump-prefer-future 'org-read-date-prefer-future
  2864. "Should the agenda jump command prefer the future for incomplete dates?
  2865. The default is to do the same as configured in `org-read-date-prefer-future'.
  2866. But you can also set a deviating value here.
  2867. This may t or nil, or the symbol `org-read-date-prefer-future'."
  2868. :group 'org-agenda
  2869. :group 'org-time
  2870. :version "24.1"
  2871. :type '(choice
  2872. (const :tag "Use org-read-date-prefer-future"
  2873. org-read-date-prefer-future)
  2874. (const :tag "Never" nil)
  2875. (const :tag "Always" t)))
  2876. (defcustom org-read-date-force-compatible-dates t
  2877. "Should date/time prompt force dates that are guaranteed to work in Emacs?
  2878. Depending on the system Emacs is running on, certain dates cannot
  2879. be represented with the type used internally to represent time.
  2880. Dates between 1970-1-1 and 2038-1-1 can always be represented
  2881. correctly. Some systems allow for earlier dates, some for later,
  2882. some for both. One way to find out it to insert any date into an
  2883. Org buffer, putting the cursor on the year and hitting S-up and
  2884. S-down to test the range.
  2885. When this variable is set to t, the date/time prompt will not let
  2886. you specify dates outside the 1970-2037 range, so it is certain that
  2887. these dates will work in whatever version of Emacs you are
  2888. running, and also that you can move a file from one Emacs implementation
  2889. to another. WHenever Org is forcing the year for you, it will display
  2890. a message and beep.
  2891. When this variable is nil, Org will check if the date is
  2892. representable in the specific Emacs implementation you are using.
  2893. If not, it will force a year, usually the current year, and beep
  2894. to remind you. Currently this setting is not recommended because
  2895. the likelihood that you will open your Org files in an Emacs that
  2896. has limited date range is not negligible.
  2897. A workaround for this problem is to use diary sexp dates for time
  2898. stamps outside of this range."
  2899. :group 'org-time
  2900. :version "24.1"
  2901. :type 'boolean)
  2902. (defcustom org-read-date-display-live t
  2903. "Non-nil means display current interpretation of date prompt live.
  2904. This display will be in an overlay, in the minibuffer."
  2905. :group 'org-time
  2906. :type 'boolean)
  2907. (defcustom org-read-date-popup-calendar t
  2908. "Non-nil means pop up a calendar when prompting for a date.
  2909. In the calendar, the date can be selected with mouse-1. However, the
  2910. minibuffer will also be active, and you can simply enter the date as well.
  2911. When nil, only the minibuffer will be available."
  2912. :group 'org-time
  2913. :type 'boolean)
  2914. (org-defvaralias 'org-popup-calendar-for-date-prompt
  2915. 'org-read-date-popup-calendar)
  2916. (make-obsolete-variable
  2917. 'org-read-date-minibuffer-setup-hook
  2918. "Set `org-read-date-minibuffer-local-map' instead." "24.4")
  2919. (defcustom org-read-date-minibuffer-setup-hook nil
  2920. "Hook to be used to set up keys for the date/time interface.
  2921. Add key definitions to `minibuffer-local-map', which will be a
  2922. temporary copy.
  2923. WARNING: This option is obsolete, you should use
  2924. `org-read-date-minibuffer-local-map' to set up keys."
  2925. :group 'org-time
  2926. :type 'hook)
  2927. (defcustom org-extend-today-until 0
  2928. "The hour when your day really ends. Must be an integer.
  2929. This has influence for the following applications:
  2930. - When switching the agenda to \"today\". It it is still earlier than
  2931. the time given here, the day recognized as TODAY is actually yesterday.
  2932. - When a date is read from the user and it is still before the time given
  2933. here, the current date and time will be assumed to be yesterday, 23:59.
  2934. Also, timestamps inserted in capture templates follow this rule.
  2935. IMPORTANT: This is a feature whose implementation is and likely will
  2936. remain incomplete. Really, it is only here because past midnight seems to
  2937. be the favorite working time of John Wiegley :-)"
  2938. :group 'org-time
  2939. :type 'integer)
  2940. (defcustom org-use-effective-time nil
  2941. "If non-nil, consider `org-extend-today-until' when creating timestamps.
  2942. For example, if `org-extend-today-until' is 8, and it's 4am, then the
  2943. \"effective time\" of any timestamps between midnight and 8am will be
  2944. 23:59 of the previous day."
  2945. :group 'org-time
  2946. :version "24.1"
  2947. :type 'boolean)
  2948. (defcustom org-use-last-clock-out-time-as-effective-time nil
  2949. "When non-nil, use the last clock out time for `org-todo'.
  2950. Note that this option has precedence over the combined use of
  2951. `org-use-effective-time' and `org-extend-today-until'."
  2952. :group 'org-time
  2953. :version "24.4"
  2954. :package-version '(Org . "8.0")
  2955. :type 'boolean)
  2956. (defcustom org-edit-timestamp-down-means-later nil
  2957. "Non-nil means S-down will increase the time in a time stamp.
  2958. When nil, S-up will increase."
  2959. :group 'org-time
  2960. :type 'boolean)
  2961. (defcustom org-calendar-follow-timestamp-change t
  2962. "Non-nil means make the calendar window follow timestamp changes.
  2963. When a timestamp is modified and the calendar window is visible, it will be
  2964. moved to the new date."
  2965. :group 'org-time
  2966. :type 'boolean)
  2967. (defgroup org-tags nil
  2968. "Options concerning tags in Org-mode."
  2969. :tag "Org Tags"
  2970. :group 'org)
  2971. (defcustom org-tag-alist nil
  2972. "List of tags allowed in Org-mode files.
  2973. When this list is nil, Org-mode will base TAG input on what is already in the
  2974. buffer.
  2975. The value of this variable is an alist, the car of each entry must be a
  2976. keyword as a string, the cdr may be a character that is used to select
  2977. that tag through the fast-tag-selection interface.
  2978. See the manual for details."
  2979. :group 'org-tags
  2980. :type '(repeat
  2981. (choice
  2982. (cons (string :tag "Tag name")
  2983. (character :tag "Access char"))
  2984. (list :tag "Start radio group"
  2985. (const :startgroup)
  2986. (option (string :tag "Group description")))
  2987. (list :tag "Start tag group, non distinct"
  2988. (const :startgrouptag)
  2989. (option (string :tag "Group description")))
  2990. (list :tag "Group tags delimiter"
  2991. (const :grouptags))
  2992. (list :tag "End radio group"
  2993. (const :endgroup)
  2994. (option (string :tag "Group description")))
  2995. (list :tag "End tag group, non distinct"
  2996. (const :endgrouptag)
  2997. (option (string :tag "Group description")))
  2998. (const :tag "New line" (:newline)))))
  2999. (defcustom org-tag-persistent-alist nil
  3000. "List of tags that will always appear in all Org-mode files.
  3001. This is in addition to any in buffer settings or customizations
  3002. of `org-tag-alist'.
  3003. When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
  3004. The value of this variable is an alist, the car of each entry must be a
  3005. keyword as a string, the cdr may be a character that is used to select
  3006. that tag through the fast-tag-selection interface.
  3007. See the manual for details.
  3008. To disable these tags on a per-file basis, insert anywhere in the file:
  3009. #+STARTUP: noptag"
  3010. :group 'org-tags
  3011. :type '(repeat
  3012. (choice
  3013. (cons (string :tag "Tag name")
  3014. (character :tag "Access char"))
  3015. (const :tag "Start radio group" (:startgroup))
  3016. (const :tag "Group tags delimiter" (:grouptags))
  3017. (const :tag "End radio group" (:endgroup))
  3018. (const :tag "New line" (:newline)))))
  3019. (defcustom org-complete-tags-always-offer-all-agenda-tags nil
  3020. "If non-nil, always offer completion for all tags of all agenda files.
  3021. Instead of customizing this variable directly, you might want to
  3022. set it locally for capture buffers, because there no list of
  3023. tags in that file can be created dynamically (there are none).
  3024. (add-hook \\='org-capture-mode-hook
  3025. (lambda ()
  3026. (setq-local org-complete-tags-always-offer-all-agenda-tags t)))"
  3027. :group 'org-tags
  3028. :version "24.1"
  3029. :type 'boolean)
  3030. (defvar org-file-tags nil
  3031. "List of tags that can be inherited by all entries in the file.
  3032. The tags will be inherited if the variable `org-use-tag-inheritance'
  3033. says they should be.
  3034. This variable is populated from #+FILETAGS lines.")
  3035. (defcustom org-use-fast-tag-selection 'auto
  3036. "Non-nil means use fast tag selection scheme.
  3037. This is a special interface to select and deselect tags with single keys.
  3038. When nil, fast selection is never used.
  3039. When the symbol `auto', fast selection is used if and only if selection
  3040. characters for tags have been configured, either through the variable
  3041. `org-tag-alist' or through a #+TAGS line in the buffer.
  3042. When t, fast selection is always used and selection keys are assigned
  3043. automatically if necessary."
  3044. :group 'org-tags
  3045. :type '(choice
  3046. (const :tag "Always" t)
  3047. (const :tag "Never" nil)
  3048. (const :tag "When selection characters are configured" auto)))
  3049. (defcustom org-fast-tag-selection-single-key nil
  3050. "Non-nil means fast tag selection exits after first change.
  3051. When nil, you have to press RET to exit it.
  3052. During fast tag selection, you can toggle this flag with `C-c'.
  3053. This variable can also have the value `expert'. In this case, the window
  3054. displaying the tags menu is not even shown, until you press C-c again."
  3055. :group 'org-tags
  3056. :type '(choice
  3057. (const :tag "No" nil)
  3058. (const :tag "Yes" t)
  3059. (const :tag "Expert" expert)))
  3060. (defvar org-fast-tag-selection-include-todo nil
  3061. "Non-nil means fast tags selection interface will also offer TODO states.
  3062. This is an undocumented feature, you should not rely on it.")
  3063. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  3064. "The column to which tags should be indented in a headline.
  3065. If this number is positive, it specifies the column. If it is negative,
  3066. it means that the tags should be flushright to that column. For example,
  3067. -80 works well for a normal 80 character screen.
  3068. When 0, place tags directly after headline text, with only one space in
  3069. between."
  3070. :group 'org-tags
  3071. :type 'integer)
  3072. (defcustom org-auto-align-tags t
  3073. "Non-nil keeps tags aligned when modifying headlines.
  3074. Some operations (i.e. demoting) change the length of a headline and
  3075. therefore shift the tags around. With this option turned on, after
  3076. each such operation the tags are again aligned to `org-tags-column'."
  3077. :group 'org-tags
  3078. :type 'boolean)
  3079. (defcustom org-use-tag-inheritance t
  3080. "Non-nil means tags in levels apply also for sublevels.
  3081. When nil, only the tags directly given in a specific line apply there.
  3082. This may also be a list of tags that should be inherited, or a regexp that
  3083. matches tags that should be inherited. Additional control is possible
  3084. with the variable `org-tags-exclude-from-inheritance' which gives an
  3085. explicit list of tags to be excluded from inheritance, even if the value of
  3086. `org-use-tag-inheritance' would select it for inheritance.
  3087. If this option is t, a match early-on in a tree can lead to a large
  3088. number of matches in the subtree when constructing the agenda or creating
  3089. a sparse tree. If you only want to see the first match in a tree during
  3090. a search, check out the variable `org-tags-match-list-sublevels'."
  3091. :group 'org-tags
  3092. :type '(choice
  3093. (const :tag "Not" nil)
  3094. (const :tag "Always" t)
  3095. (repeat :tag "Specific tags" (string :tag "Tag"))
  3096. (regexp :tag "Tags matched by regexp")))
  3097. (defcustom org-tags-exclude-from-inheritance nil
  3098. "List of tags that should never be inherited.
  3099. This is a way to exclude a few tags from inheritance. For way to do
  3100. the opposite, to actively allow inheritance for selected tags,
  3101. see the variable `org-use-tag-inheritance'."
  3102. :group 'org-tags
  3103. :type '(repeat (string :tag "Tag")))
  3104. (defun org-tag-inherit-p (tag)
  3105. "Check if TAG is one that should be inherited."
  3106. (cond
  3107. ((member tag org-tags-exclude-from-inheritance) nil)
  3108. ((eq org-use-tag-inheritance t) t)
  3109. ((not org-use-tag-inheritance) nil)
  3110. ((stringp org-use-tag-inheritance)
  3111. (string-match org-use-tag-inheritance tag))
  3112. ((listp org-use-tag-inheritance)
  3113. (member tag org-use-tag-inheritance))
  3114. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  3115. (defcustom org-tags-match-list-sublevels t
  3116. "Non-nil means list also sublevels of headlines matching a search.
  3117. This variable applies to tags/property searches, and also to stuck
  3118. projects because this search is based on a tags match as well.
  3119. When set to the symbol `indented', sublevels are indented with
  3120. leading dots.
  3121. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  3122. the sublevels of a headline matching a tag search often also match
  3123. the same search. Listing all of them can create very long lists.
  3124. Setting this variable to nil causes subtrees of a match to be skipped.
  3125. This variable is semi-obsolete and probably should always be true. It
  3126. is better to limit inheritance to certain tags using the variables
  3127. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  3128. :group 'org-tags
  3129. :type '(choice
  3130. (const :tag "No, don't list them" nil)
  3131. (const :tag "Yes, do list them" t)
  3132. (const :tag "List them, indented with leading dots" indented)))
  3133. (defcustom org-tags-sort-function nil
  3134. "When set, tags are sorted using this function as a comparator."
  3135. :group 'org-tags
  3136. :type '(choice
  3137. (const :tag "No sorting" nil)
  3138. (const :tag "Alphabetical" string<)
  3139. (const :tag "Reverse alphabetical" string>)
  3140. (function :tag "Custom function" nil)))
  3141. (defvar org-tags-history nil
  3142. "History of minibuffer reads for tags.")
  3143. (defvar org-last-tags-completion-table nil
  3144. "The last used completion table for tags.")
  3145. (defvar org-after-tags-change-hook nil
  3146. "Hook that is run after the tags in a line have changed.")
  3147. (defgroup org-properties nil
  3148. "Options concerning properties in Org-mode."
  3149. :tag "Org Properties"
  3150. :group 'org)
  3151. (defcustom org-property-format "%-10s %s"
  3152. "How property key/value pairs should be formatted by `indent-line'.
  3153. When `indent-line' hits a property definition, it will format the line
  3154. according to this format, mainly to make sure that the values are
  3155. lined-up with respect to each other."
  3156. :group 'org-properties
  3157. :type 'string)
  3158. (defcustom org-properties-postprocess-alist nil
  3159. "Alist of properties and functions to adjust inserted values.
  3160. Elements of this alist must be of the form
  3161. ([string] [function])
  3162. where [string] must be a property name and [function] must be a
  3163. lambda expression: this lambda expression must take one argument,
  3164. the value to adjust, and return the new value as a string.
  3165. For example, this element will allow the property \"Remaining\"
  3166. to be updated wrt the relation between the \"Effort\" property
  3167. and the clock summary:
  3168. ((\"Remaining\" (lambda(value)
  3169. (let ((clocksum (org-clock-sum-current-item))
  3170. (effort (org-duration-string-to-minutes
  3171. (org-entry-get (point) \"Effort\"))))
  3172. (org-minutes-to-clocksum-string (- effort clocksum))))))"
  3173. :group 'org-properties
  3174. :version "24.1"
  3175. :type '(alist :key-type (string :tag "Property")
  3176. :value-type (function :tag "Function")))
  3177. (defcustom org-use-property-inheritance nil
  3178. "Non-nil means properties apply also for sublevels.
  3179. This setting is chiefly used during property searches. Turning it on can
  3180. cause significant overhead when doing a search, which is why it is not
  3181. on by default.
  3182. When nil, only the properties directly given in the current entry count.
  3183. When t, every property is inherited. The value may also be a list of
  3184. properties that should have inheritance, or a regular expression matching
  3185. properties that should be inherited.
  3186. However, note that some special properties use inheritance under special
  3187. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  3188. and the properties ending in \"_ALL\" when they are used as descriptor
  3189. for valid values of a property.
  3190. Note for programmers:
  3191. When querying an entry with `org-entry-get', you can control if inheritance
  3192. should be used. By default, `org-entry-get' looks only at the local
  3193. properties. You can request inheritance by setting the inherit argument
  3194. to t (to force inheritance) or to `selective' (to respect the setting
  3195. in this variable)."
  3196. :group 'org-properties
  3197. :type '(choice
  3198. (const :tag "Not" nil)
  3199. (const :tag "Always" t)
  3200. (repeat :tag "Specific properties" (string :tag "Property"))
  3201. (regexp :tag "Properties matched by regexp")))
  3202. (defun org-property-inherit-p (property)
  3203. "Check if PROPERTY is one that should be inherited."
  3204. (cond
  3205. ((eq org-use-property-inheritance t) t)
  3206. ((not org-use-property-inheritance) nil)
  3207. ((stringp org-use-property-inheritance)
  3208. (string-match org-use-property-inheritance property))
  3209. ((listp org-use-property-inheritance)
  3210. (member property org-use-property-inheritance))
  3211. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  3212. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  3213. "The default column format, if no other format has been defined.
  3214. This variable can be set on the per-file basis by inserting a line
  3215. #+COLUMNS: %25ITEM ....."
  3216. :group 'org-properties
  3217. :type 'string)
  3218. (defcustom org-columns-ellipses ".."
  3219. "The ellipses to be used when a field in column view is truncated.
  3220. When this is the empty string, as many characters as possible are shown,
  3221. but then there will be no visual indication that the field has been truncated.
  3222. When this is a string of length N, the last N characters of a truncated
  3223. field are replaced by this string. If the column is narrower than the
  3224. ellipses string, only part of the ellipses string will be shown."
  3225. :group 'org-properties
  3226. :type 'string)
  3227. (defconst org-global-properties-fixed
  3228. '(("VISIBILITY_ALL" . "folded children content all")
  3229. ("CLOCK_MODELINE_TOTAL_ALL" . "current today repeat all auto"))
  3230. "List of property/value pairs that can be inherited by any entry.
  3231. These are fixed values, for the preset properties. The user variable
  3232. that can be used to add to this list is `org-global-properties'.
  3233. The entries in this list are cons cells where the car is a property
  3234. name and cdr is a string with the value. If the value represents
  3235. multiple items like an \"_ALL\" property, separate the items by
  3236. spaces.")
  3237. (defcustom org-global-properties nil
  3238. "List of property/value pairs that can be inherited by any entry.
  3239. This list will be combined with the constant `org-global-properties-fixed'.
  3240. The entries in this list are cons cells where the car is a property
  3241. name and cdr is a string with the value.
  3242. You can set buffer-local values for the same purpose in the variable
  3243. `org-file-properties' this by adding lines like
  3244. #+PROPERTY: NAME VALUE"
  3245. :group 'org-properties
  3246. :type '(repeat
  3247. (cons (string :tag "Property")
  3248. (string :tag "Value"))))
  3249. (defvar-local org-file-properties nil
  3250. "List of property/value pairs that can be inherited by any entry.
  3251. Valid for the current buffer.
  3252. This variable is populated from #+PROPERTY lines.")
  3253. (defgroup org-agenda nil
  3254. "Options concerning agenda views in Org-mode."
  3255. :tag "Org Agenda"
  3256. :group 'org)
  3257. (defvar-local org-category nil
  3258. "Variable used by org files to set a category for agenda display.
  3259. Such files should use a file variable to set it, for example
  3260. # -*- mode: org; org-category: \"ELisp\"
  3261. or contain a special line
  3262. #+CATEGORY: ELisp
  3263. If the file does not specify a category, then file's base name
  3264. is used instead.")
  3265. (put 'org-category 'safe-local-variable (lambda (x) (or (symbolp x) (stringp x))))
  3266. (defcustom org-agenda-files nil
  3267. "The files to be used for agenda display.
  3268. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  3269. \\[org-remove-file]. You can also use customize to edit the list.
  3270. If an entry is a directory, all files in that directory that are matched by
  3271. `org-agenda-file-regexp' will be part of the file list.
  3272. If the value of the variable is not a list but a single file name, then
  3273. the list of agenda files is actually stored and maintained in that file, one
  3274. agenda file per line. In this file paths can be given relative to
  3275. `org-directory'. Tilde expansion and environment variable substitution
  3276. are also made."
  3277. :group 'org-agenda
  3278. :type '(choice
  3279. (repeat :tag "List of files and directories" file)
  3280. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  3281. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  3282. "Regular expression to match files for `org-agenda-files'.
  3283. If any element in the list in that variable contains a directory instead
  3284. of a normal file, all files in that directory that are matched by this
  3285. regular expression will be included."
  3286. :group 'org-agenda
  3287. :type 'regexp)
  3288. (defcustom org-agenda-text-search-extra-files nil
  3289. "List of extra files to be searched by text search commands.
  3290. These files will be searched in addition to the agenda files by the
  3291. commands `org-search-view' (`\\[org-agenda] s') \
  3292. and `org-occur-in-agenda-files'.
  3293. Note that these files will only be searched for text search commands,
  3294. not for the other agenda views like todo lists, tag searches or the weekly
  3295. agenda. This variable is intended to list notes and possibly archive files
  3296. that should also be searched by these two commands.
  3297. In fact, if the first element in the list is the symbol `agenda-archives',
  3298. then all archive files of all agenda files will be added to the search
  3299. scope."
  3300. :group 'org-agenda
  3301. :type '(set :greedy t
  3302. (const :tag "Agenda Archives" agenda-archives)
  3303. (repeat :inline t (file))))
  3304. (org-defvaralias 'org-agenda-multi-occur-extra-files
  3305. 'org-agenda-text-search-extra-files)
  3306. (defcustom org-agenda-skip-unavailable-files nil
  3307. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  3308. A nil value means to remove them, after a query, from the list."
  3309. :group 'org-agenda
  3310. :type 'boolean)
  3311. (defcustom org-calendar-to-agenda-key [?c]
  3312. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  3313. The command `org-calendar-goto-agenda' will be bound to this key. The
  3314. default is the character `c' because then `c' can be used to switch back and
  3315. forth between agenda and calendar."
  3316. :group 'org-agenda
  3317. :type 'sexp)
  3318. (defcustom org-calendar-insert-diary-entry-key [?i]
  3319. "The key to be installed in `calendar-mode-map' for adding diary entries.
  3320. This option is irrelevant until `org-agenda-diary-file' has been configured
  3321. to point to an Org-mode file. When that is the case, the command
  3322. `org-agenda-diary-entry' will be bound to the key given here, by default
  3323. `i'. In the calendar, `i' normally adds entries to `diary-file'. So
  3324. if you want to continue doing this, you need to change this to a different
  3325. key."
  3326. :group 'org-agenda
  3327. :type 'sexp)
  3328. (defcustom org-agenda-diary-file 'diary-file
  3329. "File to which to add new entries with the `i' key in agenda and calendar.
  3330. When this is the symbol `diary-file', the functionality in the Emacs
  3331. calendar will be used to add entries to the `diary-file'. But when this
  3332. points to a file, `org-agenda-diary-entry' will be used instead."
  3333. :group 'org-agenda
  3334. :type '(choice
  3335. (const :tag "The standard Emacs diary file" diary-file)
  3336. (file :tag "Special Org file diary entries")))
  3337. (eval-after-load "calendar"
  3338. '(progn
  3339. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  3340. 'org-calendar-goto-agenda)
  3341. (add-hook 'calendar-mode-hook
  3342. (lambda ()
  3343. (unless (eq org-agenda-diary-file 'diary-file)
  3344. (define-key calendar-mode-map
  3345. org-calendar-insert-diary-entry-key
  3346. 'org-agenda-diary-entry))))))
  3347. (defgroup org-latex nil
  3348. "Options for embedding LaTeX code into Org-mode."
  3349. :tag "Org LaTeX"
  3350. :group 'org)
  3351. (defcustom org-format-latex-options
  3352. '(:foreground default :background default :scale 1.0
  3353. :html-foreground "Black" :html-background "Transparent"
  3354. :html-scale 1.0 :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  3355. "Options for creating images from LaTeX fragments.
  3356. This is a property list with the following properties:
  3357. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  3358. `default' means use the foreground of the default face.
  3359. `auto' means use the foreground from the text face.
  3360. :background the background color, or \"Transparent\".
  3361. `default' means use the background of the default face.
  3362. `auto' means use the background from the text face.
  3363. :scale a scaling factor for the size of the images, to get more pixels
  3364. :html-foreground, :html-background, :html-scale
  3365. the same numbers for HTML export.
  3366. :matchers a list indicating which matchers should be used to
  3367. find LaTeX fragments. Valid members of this list are:
  3368. \"begin\" find environments
  3369. \"$1\" find single characters surrounded by $.$
  3370. \"$\" find math expressions surrounded by $...$
  3371. \"$$\" find math expressions surrounded by $$....$$
  3372. \"\\(\" find math expressions surrounded by \\(...\\)
  3373. \"\\=\\[\" find math expressions surrounded by \\=\\[...\\]"
  3374. :group 'org-latex
  3375. :type 'plist)
  3376. (defcustom org-format-latex-signal-error t
  3377. "Non-nil means signal an error when image creation of LaTeX snippets fails.
  3378. When nil, just push out a message."
  3379. :group 'org-latex
  3380. :version "24.1"
  3381. :type 'boolean)
  3382. (defcustom org-latex-to-mathml-jar-file nil
  3383. "Value of\"%j\" in `org-latex-to-mathml-convert-command'.
  3384. Use this to specify additional executable file say a jar file.
  3385. When using MathToWeb as the converter, specify the full-path to
  3386. your mathtoweb.jar file."
  3387. :group 'org-latex
  3388. :version "24.1"
  3389. :type '(choice
  3390. (const :tag "None" nil)
  3391. (file :tag "JAR file" :must-match t)))
  3392. (defcustom org-latex-to-mathml-convert-command nil
  3393. "Command to convert LaTeX fragments to MathML.
  3394. Replace format-specifiers in the command as noted below and use
  3395. `shell-command' to convert LaTeX to MathML.
  3396. %j: Executable file in fully expanded form as specified by
  3397. `org-latex-to-mathml-jar-file'.
  3398. %I: Input LaTeX file in fully expanded form.
  3399. %i: The latex fragment to be converted.
  3400. %o: Output MathML file.
  3401. This command is used by `org-create-math-formula'.
  3402. When using MathToWeb as the converter, set this option to
  3403. \"java -jar %j -unicode -force -df %o %I\".
  3404. When using LaTeXML set this option to
  3405. \"latexmlmath \"%i\" --presentationmathml=%o\"."
  3406. :group 'org-latex
  3407. :version "24.1"
  3408. :type '(choice
  3409. (const :tag "None" nil)
  3410. (string :tag "\nShell command")))
  3411. (defcustom org-latex-create-formula-image-program 'dvipng
  3412. "Program to convert LaTeX fragments with.
  3413. dvipng Process the LaTeX fragments to dvi file, then convert
  3414. dvi files to png files using dvipng.
  3415. This will also include processing of non-math environments.
  3416. imagemagick Convert the LaTeX fragments to pdf files and use imagemagick
  3417. to convert pdf files to png files"
  3418. :group 'org-latex
  3419. :version "24.1"
  3420. :type '(choice
  3421. (const :tag "dvipng" dvipng)
  3422. (const :tag "imagemagick" imagemagick)))
  3423. (defcustom org-latex-preview-ltxpng-directory "ltxpng/"
  3424. "Path to store latex preview images.
  3425. A relative path here creates many directories relative to the
  3426. processed org files paths. An absolute path puts all preview
  3427. images at the same place."
  3428. :group 'org-latex
  3429. :version "24.3"
  3430. :type 'string)
  3431. (defun org-format-latex-mathml-available-p ()
  3432. "Return t if `org-latex-to-mathml-convert-command' is usable."
  3433. (save-match-data
  3434. (when (and (boundp 'org-latex-to-mathml-convert-command)
  3435. org-latex-to-mathml-convert-command)
  3436. (let ((executable (car (split-string
  3437. org-latex-to-mathml-convert-command))))
  3438. (when (executable-find executable)
  3439. (if (string-match
  3440. "%j" org-latex-to-mathml-convert-command)
  3441. (file-readable-p org-latex-to-mathml-jar-file)
  3442. t))))))
  3443. (defcustom org-format-latex-header "\\documentclass{article}
  3444. \\usepackage[usenames]{color}
  3445. \[PACKAGES]
  3446. \[DEFAULT-PACKAGES]
  3447. \\pagestyle{empty} % do not remove
  3448. % The settings below are copied from fullpage.sty
  3449. \\setlength{\\textwidth}{\\paperwidth}
  3450. \\addtolength{\\textwidth}{-3cm}
  3451. \\setlength{\\oddsidemargin}{1.5cm}
  3452. \\addtolength{\\oddsidemargin}{-2.54cm}
  3453. \\setlength{\\evensidemargin}{\\oddsidemargin}
  3454. \\setlength{\\textheight}{\\paperheight}
  3455. \\addtolength{\\textheight}{-\\headheight}
  3456. \\addtolength{\\textheight}{-\\headsep}
  3457. \\addtolength{\\textheight}{-\\footskip}
  3458. \\addtolength{\\textheight}{-3cm}
  3459. \\setlength{\\topmargin}{1.5cm}
  3460. \\addtolength{\\topmargin}{-2.54cm}"
  3461. "The document header used for processing LaTeX fragments.
  3462. It is imperative that this header make sure that no page number
  3463. appears on the page. The package defined in the variables
  3464. `org-latex-default-packages-alist' and `org-latex-packages-alist'
  3465. will either replace the placeholder \"[PACKAGES]\" in this
  3466. header, or they will be appended."
  3467. :group 'org-latex
  3468. :type 'string)
  3469. (defun org-set-packages-alist (var val)
  3470. "Set the packages alist and make sure it has 3 elements per entry."
  3471. (set var (mapcar (lambda (x)
  3472. (if (and (consp x) (= (length x) 2))
  3473. (list (car x) (nth 1 x) t)
  3474. x))
  3475. val)))
  3476. (defun org-get-packages-alist (var)
  3477. "Get the packages alist and make sure it has 3 elements per entry."
  3478. (mapcar (lambda (x)
  3479. (if (and (consp x) (= (length x) 2))
  3480. (list (car x) (nth 1 x) t)
  3481. x))
  3482. (default-value var)))
  3483. (defcustom org-latex-default-packages-alist
  3484. '(("AUTO" "inputenc" t ("pdflatex"))
  3485. ("T1" "fontenc" t ("pdflatex"))
  3486. ("" "graphicx" t)
  3487. ("" "grffile" t)
  3488. ("" "longtable" nil)
  3489. ("" "wrapfig" nil)
  3490. ("" "rotating" nil)
  3491. ("normalem" "ulem" t)
  3492. ("" "amsmath" t)
  3493. ("" "textcomp" t)
  3494. ("" "amssymb" t)
  3495. ("" "capt-of" nil)
  3496. ("" "hyperref" nil))
  3497. "Alist of default packages to be inserted in the header.
  3498. Change this only if one of the packages here causes an
  3499. incompatibility with another package you are using.
  3500. The packages in this list are needed by one part or another of
  3501. Org mode to function properly:
  3502. - inputenc, fontenc: for basic font and character selection
  3503. - graphicx: for including images
  3504. - grffile: allow periods and spaces in graphics file names
  3505. - longtable: For multipage tables
  3506. - wrapfig: for figure placement
  3507. - rotating: for sideways figures and tables
  3508. - ulem: for underline and strike-through
  3509. - amsmath: for subscript and superscript and math environments
  3510. - textcomp, amssymb: for various symbols used
  3511. for interpreting the entities in `org-entities'. You can skip
  3512. some of these packages if you don't use any of their symbols.
  3513. - capt-of: for captions outside of floats
  3514. - hyperref: for cross references
  3515. Therefore you should not modify this variable unless you know
  3516. what you are doing. The one reason to change it anyway is that
  3517. you might be loading some other package that conflicts with one
  3518. of the default packages. Each element is either a cell or
  3519. a string.
  3520. A cell is of the format
  3521. (\"options\" \"package\" SNIPPET-FLAG COMPILERS)
  3522. If SNIPPET-FLAG is non-nil, the package also needs to be included
  3523. when compiling LaTeX snippets into images for inclusion into
  3524. non-LaTeX output. COMPILERS is a list of compilers that should
  3525. include the package, see `org-latex-compiler'. If the document
  3526. compiler is not in the list, and the list is non-nil, the package
  3527. will not be inserted in the final document.
  3528. A string will be inserted as-is in the header of the document."
  3529. :group 'org-latex
  3530. :group 'org-export-latex
  3531. :set 'org-set-packages-alist
  3532. :get 'org-get-packages-alist
  3533. :version "25.1"
  3534. :package-version '(Org . "8.3")
  3535. :type '(repeat
  3536. (choice
  3537. (list :tag "options/package pair"
  3538. (string :tag "options")
  3539. (string :tag "package")
  3540. (boolean :tag "Snippet"))
  3541. (string :tag "A line of LaTeX"))))
  3542. (defcustom org-latex-packages-alist nil
  3543. "Alist of packages to be inserted in every LaTeX header.
  3544. These will be inserted after `org-latex-default-packages-alist'.
  3545. Each element is either a cell or a string.
  3546. A cell is of the format:
  3547. (\"options\" \"package\" SNIPPET-FLAG)
  3548. SNIPPET-FLAG, when non-nil, indicates that this package is also
  3549. needed when turning LaTeX snippets into images for inclusion into
  3550. non-LaTeX output.
  3551. A string will be inserted as-is in the header of the document.
  3552. Make sure that you only list packages here which:
  3553. - you want in every file;
  3554. - do not conflict with the setup in `org-format-latex-header';
  3555. - do not conflict with the default packages in
  3556. `org-latex-default-packages-alist'."
  3557. :group 'org-latex
  3558. :group 'org-export-latex
  3559. :set 'org-set-packages-alist
  3560. :get 'org-get-packages-alist
  3561. :type '(repeat
  3562. (choice
  3563. (list :tag "options/package pair"
  3564. (string :tag "options")
  3565. (string :tag "package")
  3566. (boolean :tag "Snippet"))
  3567. (string :tag "A line of LaTeX"))))
  3568. (defgroup org-appearance nil
  3569. "Settings for Org-mode appearance."
  3570. :tag "Org Appearance"
  3571. :group 'org)
  3572. (defcustom org-level-color-stars-only nil
  3573. "Non-nil means fontify only the stars in each headline.
  3574. When nil, the entire headline is fontified.
  3575. Changing it requires restart of `font-lock-mode' to become effective
  3576. also in regions already fontified."
  3577. :group 'org-appearance
  3578. :type 'boolean)
  3579. (defcustom org-hide-leading-stars nil
  3580. "Non-nil means hide the first N-1 stars in a headline.
  3581. This works by using the face `org-hide' for these stars. This
  3582. face is white for a light background, and black for a dark
  3583. background. You may have to customize the face `org-hide' to
  3584. make this work.
  3585. Changing it requires restart of `font-lock-mode' to become effective
  3586. also in regions already fontified.
  3587. You may also set this on a per-file basis by adding one of the following
  3588. lines to the buffer:
  3589. #+STARTUP: hidestars
  3590. #+STARTUP: showstars"
  3591. :group 'org-appearance
  3592. :type 'boolean)
  3593. (defcustom org-hidden-keywords nil
  3594. "List of symbols corresponding to keywords to be hidden the org buffer.
  3595. For example, a value \\='(title) for this list will make the document's title
  3596. appear in the buffer without the initial #+TITLE: keyword."
  3597. :group 'org-appearance
  3598. :version "24.1"
  3599. :type '(set (const :tag "#+AUTHOR" author)
  3600. (const :tag "#+DATE" date)
  3601. (const :tag "#+EMAIL" email)
  3602. (const :tag "#+TITLE" title)))
  3603. (defcustom org-custom-properties nil
  3604. "List of properties (as strings) with a special meaning.
  3605. The default use of these custom properties is to let the user
  3606. hide them with `org-toggle-custom-properties-visibility'."
  3607. :group 'org-properties
  3608. :group 'org-appearance
  3609. :version "24.3"
  3610. :type '(repeat (string :tag "Property Name")))
  3611. (defcustom org-fontify-done-headline nil
  3612. "Non-nil means change the face of a headline if it is marked DONE.
  3613. Normally, only the TODO/DONE keyword indicates the state of a headline.
  3614. When this is non-nil, the headline after the keyword is set to the
  3615. `org-headline-done' as an additional indication."
  3616. :group 'org-appearance
  3617. :type 'boolean)
  3618. (defcustom org-fontify-emphasized-text t
  3619. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  3620. Changing this variable requires a restart of Emacs to take effect."
  3621. :group 'org-appearance
  3622. :type 'boolean)
  3623. (defcustom org-fontify-whole-heading-line nil
  3624. "Non-nil means fontify the whole line for headings.
  3625. This is useful when setting a background color for the
  3626. org-level-* faces."
  3627. :group 'org-appearance
  3628. :type 'boolean)
  3629. (defcustom org-highlight-latex-and-related nil
  3630. "Non-nil means highlight LaTeX related syntax in the buffer.
  3631. When non nil, the value should be a list containing any of the
  3632. following symbols:
  3633. `latex' Highlight LaTeX snippets and environments.
  3634. `script' Highlight subscript and superscript.
  3635. `entities' Highlight entities."
  3636. :group 'org-appearance
  3637. :version "24.4"
  3638. :package-version '(Org . "8.0")
  3639. :type '(choice
  3640. (const :tag "No highlighting" nil)
  3641. (set :greedy t :tag "Highlight"
  3642. (const :tag "LaTeX snippets and environments" latex)
  3643. (const :tag "Subscript and superscript" script)
  3644. (const :tag "Entities" entities))))
  3645. (defcustom org-hide-emphasis-markers nil
  3646. "Non-nil mean font-lock should hide the emphasis marker characters."
  3647. :group 'org-appearance
  3648. :type 'boolean)
  3649. (defcustom org-hide-macro-markers nil
  3650. "Non-nil mean font-lock should hide the brackets marking macro calls."
  3651. :group 'org-appearance
  3652. :type 'boolean)
  3653. (defcustom org-pretty-entities nil
  3654. "Non-nil means show entities as UTF8 characters.
  3655. When nil, the \\name form remains in the buffer."
  3656. :group 'org-appearance
  3657. :version "24.1"
  3658. :type 'boolean)
  3659. (defcustom org-pretty-entities-include-sub-superscripts t
  3660. "Non-nil means, pretty entity display includes formatting sub/superscripts."
  3661. :group 'org-appearance
  3662. :version "24.1"
  3663. :type 'boolean)
  3664. (defvar org-emph-re nil
  3665. "Regular expression for matching emphasis.
  3666. After a match, the match groups contain these elements:
  3667. 0 The match of the full regular expression, including the characters
  3668. before and after the proper match
  3669. 1 The character before the proper match, or empty at beginning of line
  3670. 2 The proper match, including the leading and trailing markers
  3671. 3 The leading marker like * or /, indicating the type of highlighting
  3672. 4 The text between the emphasis markers, not including the markers
  3673. 5 The character after the match, empty at the end of a line")
  3674. (defvar org-verbatim-re nil
  3675. "Regular expression for matching verbatim text.")
  3676. (defvar org-emphasis-regexp-components) ; defined just below
  3677. (defvar org-emphasis-alist) ; defined just below
  3678. (defun org-set-emph-re (var val)
  3679. "Set variable and compute the emphasis regular expression."
  3680. (set var val)
  3681. (when (and (boundp 'org-emphasis-alist)
  3682. (boundp 'org-emphasis-regexp-components)
  3683. org-emphasis-alist org-emphasis-regexp-components)
  3684. (let* ((e org-emphasis-regexp-components)
  3685. (pre (car e))
  3686. (post (nth 1 e))
  3687. (border (nth 2 e))
  3688. (body (nth 3 e))
  3689. (nl (nth 4 e))
  3690. (body1 (concat body "*?"))
  3691. (markers (mapconcat 'car org-emphasis-alist ""))
  3692. (vmarkers (mapconcat
  3693. (lambda (x) (if (eq (nth 2 x) 'verbatim) (car x) ""))
  3694. org-emphasis-alist "")))
  3695. ;; make sure special characters appear at the right position in the class
  3696. (if (string-match "\\^" markers)
  3697. (setq markers (concat (replace-match "" t t markers) "^")))
  3698. (if (string-match "-" markers)
  3699. (setq markers (concat (replace-match "" t t markers) "-")))
  3700. (if (string-match "\\^" vmarkers)
  3701. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  3702. (if (string-match "-" vmarkers)
  3703. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  3704. (if (> nl 0)
  3705. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  3706. (int-to-string nl) "\\}")))
  3707. ;; Make the regexp
  3708. (setq org-emph-re
  3709. (concat "\\([" pre "]\\|^\\)"
  3710. "\\("
  3711. "\\([" markers "]\\)"
  3712. "\\("
  3713. "[^" border "]\\|"
  3714. "[^" border "]"
  3715. body1
  3716. "[^" border "]"
  3717. "\\)"
  3718. "\\3\\)"
  3719. "\\([" post "]\\|$\\)"))
  3720. (setq org-verbatim-re
  3721. (concat "\\([" pre "]\\|^\\)"
  3722. "\\("
  3723. "\\([" vmarkers "]\\)"
  3724. "\\("
  3725. "[^" border "]\\|"
  3726. "[^" border "]"
  3727. body1
  3728. "[^" border "]"
  3729. "\\)"
  3730. "\\3\\)"
  3731. "\\([" post "]\\|$\\)")))))
  3732. ;; This used to be a defcustom (Org <8.0) but allowing the users to
  3733. ;; set this option proved cumbersome. See this message/thread:
  3734. ;; http://article.gmane.org/gmane.emacs.orgmode/68681
  3735. (defvar org-emphasis-regexp-components
  3736. '(" \t('\"{" "- \t.,:!?;'\")}\\[" " \t\r\n" "." 1)
  3737. "Components used to build the regular expression for emphasis.
  3738. This is a list with five entries. Terminology: In an emphasis string
  3739. like \" *strong word* \", we call the initial space PREMATCH, the final
  3740. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  3741. and \"trong wor\" is the body. The different components in this variable
  3742. specify what is allowed/forbidden in each part:
  3743. pre Chars allowed as prematch. Beginning of line will be allowed too.
  3744. post Chars allowed as postmatch. End of line will be allowed too.
  3745. border The chars *forbidden* as border characters.
  3746. body-regexp A regexp like \".\" to match a body character. Don't use
  3747. non-shy groups here, and don't allow newline here.
  3748. newline The maximum number of newlines allowed in an emphasis exp.
  3749. You need to reload Org or to restart Emacs after customizing this.")
  3750. (defcustom org-emphasis-alist
  3751. `(("*" bold)
  3752. ("/" italic)
  3753. ("_" underline)
  3754. ("=" org-verbatim verbatim)
  3755. ("~" org-code verbatim)
  3756. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))))
  3757. "Alist of characters and faces to emphasize text.
  3758. Text starting and ending with a special character will be emphasized,
  3759. for example *bold*, _underlined_ and /italic/. This variable sets the
  3760. marker characters and the face to be used by font-lock for highlighting
  3761. in Org-mode Emacs buffers.
  3762. You need to reload Org or to restart Emacs after customizing this."
  3763. :group 'org-appearance
  3764. :set 'org-set-emph-re
  3765. :version "24.4"
  3766. :package-version '(Org . "8.0")
  3767. :type '(repeat
  3768. (list
  3769. (string :tag "Marker character")
  3770. (choice
  3771. (face :tag "Font-lock-face")
  3772. (plist :tag "Face property list"))
  3773. (option (const verbatim)))))
  3774. (defvar org-protecting-blocks '("src" "example" "export")
  3775. "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
  3776. This is needed for font-lock setup.")
  3777. ;;; Functions and variables from their packages
  3778. ;; Declared here to avoid compiler warnings
  3779. ;; XEmacs only
  3780. (defvar outline-mode-menu-heading)
  3781. (defvar outline-mode-menu-show)
  3782. (defvar outline-mode-menu-hide)
  3783. (defvar zmacs-regions) ; XEmacs regions
  3784. ;; Emacs only
  3785. (defvar mark-active)
  3786. ;; Various packages
  3787. (declare-function calc-eval "calc" (str &optional separator &rest args))
  3788. (declare-function calendar-forward-day "cal-move" (arg))
  3789. (declare-function calendar-goto-date "cal-move" (date))
  3790. (declare-function calendar-goto-today "cal-move" ())
  3791. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  3792. (declare-function calendar-iso-to-absolute "cal-iso" (date))
  3793. (declare-function cdlatex-compute-tables "ext:cdlatex" ())
  3794. (declare-function cdlatex-tab "ext:cdlatex" ())
  3795. (declare-function dired-get-filename
  3796. "dired"
  3797. (&optional localp no-error-if-not-filep))
  3798. (declare-function iswitchb-read-buffer
  3799. "iswitchb"
  3800. (prompt &optional default require-match start matches-set))
  3801. (declare-function org-agenda-change-all-lines
  3802. "org-agenda"
  3803. (newhead hdmarker &optional fixface just-this))
  3804. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  3805. "org-agenda"
  3806. (&optional end))
  3807. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  3808. (declare-function org-agenda-format-item
  3809. "org-agenda"
  3810. (extra txt &optional level category tags dotime noprefix
  3811. remove-re habitp))
  3812. (declare-function org-agenda-maybe-redo "org-agenda" ())
  3813. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  3814. (declare-function org-agenda-save-markers-for-cut-and-paste
  3815. "org-agenda"
  3816. (beg end))
  3817. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  3818. (declare-function org-agenda-skip "org-agenda" ())
  3819. (declare-function org-attach-reveal "org-attach" (&optional if-exists))
  3820. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  3821. (declare-function org-indent-mode "org-indent" (&optional arg))
  3822. (declare-function org-inlinetask-goto-beginning "org-inlinetask" ())
  3823. (declare-function org-inlinetask-goto-end "org-inlinetask" ())
  3824. (declare-function org-inlinetask-in-task-p "org-inlinetask" ())
  3825. (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
  3826. (declare-function orgtbl-send-table "org-table" (&optional maybe))
  3827. (declare-function parse-time-string "parse-time" (string))
  3828. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  3829. (declare-function table--at-cell-p
  3830. "table"
  3831. (position &optional object at-column))
  3832. (defvar align-mode-rules-list)
  3833. (defvar calc-embedded-close-formula)
  3834. (defvar calc-embedded-open-formula)
  3835. (defvar calc-embedded-open-mode)
  3836. (defvar font-lock-unfontify-region-function)
  3837. (defvar iswitchb-temp-buflist)
  3838. (defvar org-agenda-tags-todo-honor-ignore-options)
  3839. (defvar remember-data-file)
  3840. (defvar texmathp-why)
  3841. ;;;###autoload
  3842. (defun turn-on-orgtbl ()
  3843. "Unconditionally turn on `orgtbl-mode'."
  3844. (require 'org-table)
  3845. (orgtbl-mode 1))
  3846. (defun org-at-table-p (&optional table-type)
  3847. "Non-nil if the cursor is inside an Org table.
  3848. If TABLE-TYPE is non-nil, also check for table.el-type tables.
  3849. If `org-enable-table-editor' is nil, return nil unconditionally."
  3850. (and
  3851. org-enable-table-editor
  3852. (save-excursion
  3853. (beginning-of-line)
  3854. (org-looking-at-p (if table-type "[ \t]*[|+]" "[ \t]*|")))
  3855. (or (not (derived-mode-p 'org-mode))
  3856. (let ((e (org-element-lineage (org-element-at-point) '(table) t)))
  3857. (and e (or table-type (eq (org-element-property :type e) 'org)))))))
  3858. (define-obsolete-function-alias 'org-table-p 'org-at-table-p "Org 9.0")
  3859. (defun org-at-table.el-p ()
  3860. "Non-nil when point is at a table.el table."
  3861. (and (save-excursion (beginning-of-line) (looking-at "[ \t]*[|+]"))
  3862. (let ((element (org-element-at-point)))
  3863. (and (eq (org-element-type element) 'table)
  3864. (eq (org-element-property :type element) 'table.el)))))
  3865. (defun org-table-recognize-table.el ()
  3866. "If there is a table.el table nearby, recognize it and move into it."
  3867. (when (and org-table-tab-recognizes-table.el (org-at-table.el-p))
  3868. (beginning-of-line)
  3869. (unless (or (looking-at org-table-dataline-regexp)
  3870. (not (looking-at org-table1-hline-regexp)))
  3871. (forward-line)
  3872. (when (looking-at org-table-any-border-regexp)
  3873. (forward-line -2)))
  3874. (if (re-search-forward "|" (org-table-end t) t)
  3875. (progn
  3876. (require 'table)
  3877. (if (table--at-cell-p (point)) t
  3878. (message "recognizing table.el table...")
  3879. (table-recognize-table)
  3880. (message "recognizing table.el table...done")))
  3881. (error "This should not happen"))))
  3882. ;;; This function is not used by org core since commit 6d1e3082, Feb 2010
  3883. (make-obsolete 'org-table-recognize-table.el
  3884. "please notify the org mailing list if you use this function."
  3885. "Org 9.0")
  3886. (defun org-at-table-hline-p ()
  3887. "Non-nil when point is inside a hline in a table.
  3888. Assume point is already in a table. If `org-enable-table-editor'
  3889. is nil, return nil unconditionally."
  3890. (and org-enable-table-editor
  3891. (save-excursion
  3892. (beginning-of-line)
  3893. (looking-at org-table-hline-regexp))))
  3894. (defun org-table-map-tables (function &optional quietly)
  3895. "Apply FUNCTION to the start of all tables in the buffer."
  3896. (save-excursion
  3897. (save-restriction
  3898. (widen)
  3899. (goto-char (point-min))
  3900. (while (re-search-forward org-table-any-line-regexp nil t)
  3901. (unless quietly
  3902. (message "Mapping tables: %d%%"
  3903. (floor (* 100.0 (point)) (buffer-size))))
  3904. (beginning-of-line 1)
  3905. (when (and (looking-at org-table-line-regexp)
  3906. ;; Exclude tables in src/example/verbatim/clocktable blocks
  3907. (not (org-in-block-p '("src" "example" "verbatim" "clocktable"))))
  3908. (save-excursion (funcall function))
  3909. (or (looking-at org-table-line-regexp)
  3910. (forward-char 1)))
  3911. (re-search-forward org-table-any-border-regexp nil 1))))
  3912. (unless quietly (message "Mapping tables: done")))
  3913. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock" (beg end))
  3914. (declare-function org-clock-update-mode-line "org-clock" ())
  3915. (declare-function org-resolve-clocks "org-clock"
  3916. (&optional also-non-dangling-p prompt last-valid))
  3917. (defun org-at-TBLFM-p (&optional pos)
  3918. "Non-nil when point (or POS) is in #+TBLFM line."
  3919. (save-excursion
  3920. (goto-char (or pos (point)))
  3921. (beginning-of-line)
  3922. (and (let ((case-fold-search t)) (looking-at org-TBLFM-regexp))
  3923. (eq (org-element-type (org-element-at-point)) 'table))))
  3924. (defvar org-clock-start-time)
  3925. (defvar org-clock-marker (make-marker)
  3926. "Marker recording the last clock-in.")
  3927. (defvar org-clock-hd-marker (make-marker)
  3928. "Marker recording the last clock-in, but the headline position.")
  3929. (defvar org-clock-heading ""
  3930. "The heading of the current clock entry.")
  3931. (defun org-clock-is-active ()
  3932. "Return the buffer where the clock is currently running.
  3933. Return nil if no clock is running."
  3934. (marker-buffer org-clock-marker))
  3935. (defun org-check-running-clock ()
  3936. "Check if the current buffer contains the running clock.
  3937. If yes, offer to stop it and to save the buffer with the changes."
  3938. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3939. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  3940. (buffer-name))))
  3941. (org-clock-out)
  3942. (when (y-or-n-p "Save changed buffer?")
  3943. (save-buffer))))
  3944. (defun org-clocktable-try-shift (dir n)
  3945. "Check if this line starts a clock table, if yes, shift the time block."
  3946. (when (org-match-line "^[ \t]*#\\+BEGIN:[ \t]+clocktable\\>")
  3947. (org-clocktable-shift dir n)))
  3948. ;;;###autoload
  3949. (defun org-clock-persistence-insinuate ()
  3950. "Set up hooks for clock persistence."
  3951. (require 'org-clock)
  3952. (add-hook 'org-mode-hook 'org-clock-load)
  3953. (add-hook 'kill-emacs-hook 'org-clock-save))
  3954. (defgroup org-archive nil
  3955. "Options concerning archiving in Org-mode."
  3956. :tag "Org Archive"
  3957. :group 'org-structure)
  3958. (defcustom org-archive-location "%s_archive::"
  3959. "The location where subtrees should be archived.
  3960. The value of this variable is a string, consisting of two parts,
  3961. separated by a double-colon. The first part is a filename and
  3962. the second part is a headline.
  3963. When the filename is omitted, archiving happens in the same file.
  3964. %s in the filename will be replaced by the current file
  3965. name (without the directory part). Archiving to a different file
  3966. is useful to keep archived entries from contributing to the
  3967. Org-mode Agenda.
  3968. The archived entries will be filed as subtrees of the specified
  3969. headline. When the headline is omitted, the subtrees are simply
  3970. filed away at the end of the file, as top-level entries. Also in
  3971. the heading you can use %s to represent the file name, this can be
  3972. useful when using the same archive for a number of different files.
  3973. Here are a few examples:
  3974. \"%s_archive::\"
  3975. If the current file is Projects.org, archive in file
  3976. Projects.org_archive, as top-level trees. This is the default.
  3977. \"::* Archived Tasks\"
  3978. Archive in the current file, under the top-level headline
  3979. \"* Archived Tasks\".
  3980. \"~/org/archive.org::\"
  3981. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  3982. \"~/org/archive.org::* From %s\"
  3983. Archive in file ~/org/archive.org (absolute path), under headlines
  3984. \"From FILENAME\" where file name is the current file name.
  3985. \"~/org/datetree.org::datetree/* Finished Tasks\"
  3986. The \"datetree/\" string is special, signifying to archive
  3987. items to the datetree. Items are placed in either the CLOSED
  3988. date of the item, or the current date if there is no CLOSED date.
  3989. The heading will be a subentry to the current date. There doesn't
  3990. need to be a heading, but there always needs to be a slash after
  3991. datetree. For example, to store archived items directly in the
  3992. datetree, use \"~/org/datetree.org::datetree/\".
  3993. \"basement::** Finished Tasks\"
  3994. Archive in file ./basement (relative path), as level 3 trees
  3995. below the level 2 heading \"** Finished Tasks\".
  3996. You may set this option on a per-file basis by adding to the buffer a
  3997. line like
  3998. #+ARCHIVE: basement::** Finished Tasks
  3999. You may also define it locally for a subtree by setting an ARCHIVE property
  4000. in the entry. If such a property is found in an entry, or anywhere up
  4001. the hierarchy, it will be used."
  4002. :group 'org-archive
  4003. :type 'string)
  4004. (defcustom org-agenda-skip-archived-trees t
  4005. "Non-nil means the agenda will skip any items located in archived trees.
  4006. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  4007. variable is no longer recommended, you should leave it at the value t.
  4008. Instead, use the key `v' to cycle the archives-mode in the agenda."
  4009. :group 'org-archive
  4010. :group 'org-agenda-skip
  4011. :type 'boolean)
  4012. (defcustom org-columns-skip-archived-trees t
  4013. "Non-nil means ignore archived trees when creating column view."
  4014. :group 'org-archive
  4015. :group 'org-properties
  4016. :type 'boolean)
  4017. (defcustom org-cycle-open-archived-trees nil
  4018. "Non-nil means `org-cycle' will open archived trees.
  4019. An archived tree is a tree marked with the tag ARCHIVE.
  4020. When nil, archived trees will stay folded. You can still open them with
  4021. normal outline commands like `show-all', but not with the cycling commands."
  4022. :group 'org-archive
  4023. :group 'org-cycle
  4024. :type 'boolean)
  4025. (defcustom org-sparse-tree-open-archived-trees nil
  4026. "Non-nil means sparse tree construction shows matches in archived trees.
  4027. When nil, matches in these trees are highlighted, but the trees are kept in
  4028. collapsed state."
  4029. :group 'org-archive
  4030. :group 'org-sparse-trees
  4031. :type 'boolean)
  4032. (defcustom org-sparse-tree-default-date-type nil
  4033. "The default date type when building a sparse tree.
  4034. When this is nil, a date is a scheduled or a deadline timestamp.
  4035. Otherwise, these types are allowed:
  4036. all: all timestamps
  4037. active: only active timestamps (<...>)
  4038. inactive: only inactive timestamps ([...])
  4039. scheduled: only scheduled timestamps
  4040. deadline: only deadline timestamps"
  4041. :type '(choice (const :tag "Scheduled or deadline" nil)
  4042. (const :tag "All timestamps" all)
  4043. (const :tag "Only active timestamps" active)
  4044. (const :tag "Only inactive timestamps" inactive)
  4045. (const :tag "Only scheduled timestamps" scheduled)
  4046. (const :tag "Only deadline timestamps" deadline)
  4047. (const :tag "Only closed timestamps" closed))
  4048. :version "25.1"
  4049. :package-version '(Org . "8.3")
  4050. :group 'org-sparse-trees)
  4051. (defun org-cycle-hide-archived-subtrees (state)
  4052. "Re-hide all archived subtrees after a visibility state change."
  4053. (when (and (not org-cycle-open-archived-trees)
  4054. (not (memq state '(overview folded))))
  4055. (save-excursion
  4056. (let* ((globalp (memq state '(contents all)))
  4057. (beg (if globalp (point-min) (point)))
  4058. (end (if globalp (point-max) (org-end-of-subtree t))))
  4059. (org-hide-archived-subtrees beg end)
  4060. (goto-char beg)
  4061. (when (looking-at-p (concat ".*:" org-archive-tag ":"))
  4062. (message "%s" (substitute-command-keys
  4063. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  4064. (defun org-force-cycle-archived ()
  4065. "Cycle subtree even if it is archived."
  4066. (interactive)
  4067. (setq this-command 'org-cycle)
  4068. (let ((org-cycle-open-archived-trees t))
  4069. (call-interactively 'org-cycle)))
  4070. (defun org-hide-archived-subtrees (beg end)
  4071. "Re-hide all archived subtrees after a visibility state change."
  4072. (org-with-wide-buffer
  4073. (let ((case-fold-search nil)
  4074. (re (concat org-outline-regexp-bol ".*:" org-archive-tag ":")))
  4075. (goto-char beg)
  4076. ;; Include headline point is currently on.
  4077. (beginning-of-line)
  4078. (while (and (< (point) end) (re-search-forward re end t))
  4079. (when (member org-archive-tag (org-get-tags))
  4080. (org-flag-subtree t)
  4081. (org-end-of-subtree t))))))
  4082. (declare-function outline-end-of-heading "outline" ())
  4083. (declare-function outline-flag-region "outline" (from to flag))
  4084. (defun org-flag-subtree (flag)
  4085. (save-excursion
  4086. (org-back-to-heading t)
  4087. (outline-end-of-heading)
  4088. (outline-flag-region (point)
  4089. (progn (org-end-of-subtree t) (point))
  4090. flag)))
  4091. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  4092. ;; Declare Column View Code
  4093. (declare-function org-columns-get-format-and-top-level "org-colview" ())
  4094. (declare-function org-columns-compute "org-colview" (property))
  4095. ;; Declare ID code
  4096. (declare-function org-id-store-link "org-id")
  4097. (declare-function org-id-locations-load "org-id")
  4098. (declare-function org-id-locations-save "org-id")
  4099. (defvar org-id-track-globally)
  4100. ;;; Variables for pre-computed regular expressions, all buffer local
  4101. (defvar-local org-todo-regexp nil
  4102. "Matches any of the TODO state keywords.")
  4103. (defvar-local org-not-done-regexp nil
  4104. "Matches any of the TODO state keywords except the last one.")
  4105. (defvar-local org-not-done-heading-regexp nil
  4106. "Matches a TODO headline that is not done.")
  4107. (defvar-local org-todo-line-regexp nil
  4108. "Matches a headline and puts TODO state into group 2 if present.")
  4109. (defvar-local org-complex-heading-regexp nil
  4110. "Matches a headline and puts everything into groups:
  4111. group 1: the stars
  4112. group 2: The todo keyword, maybe
  4113. group 3: Priority cookie
  4114. group 4: True headline
  4115. group 5: Tags")
  4116. (defvar-local org-complex-heading-regexp-format nil
  4117. "Printf format to make regexp to match an exact headline.
  4118. This regexp will match the headline of any node which has the
  4119. exact headline text that is put into the format, but may have any
  4120. TODO state, priority and tags.")
  4121. (defvar-local org-todo-line-tags-regexp nil
  4122. "Matches a headline and puts TODO state into group 2 if present.
  4123. Also put tags into group 4 if tags are present.")
  4124. (defconst org-plain-time-of-day-regexp
  4125. (concat
  4126. "\\(\\<[012]?[0-9]"
  4127. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4128. "\\(--?"
  4129. "\\(\\<[012]?[0-9]"
  4130. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4131. "\\)?")
  4132. "Regular expression to match a plain time or time range.
  4133. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  4134. groups carry important information:
  4135. 0 the full match
  4136. 1 the first time, range or not
  4137. 8 the second time, if it is a range.")
  4138. (defconst org-plain-time-extension-regexp
  4139. (concat
  4140. "\\(\\<[012]?[0-9]"
  4141. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4142. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  4143. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  4144. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  4145. groups carry important information:
  4146. 0 the full match
  4147. 7 hours of duration
  4148. 9 minutes of duration")
  4149. (defconst org-stamp-time-of-day-regexp
  4150. (concat
  4151. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  4152. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  4153. "\\(--?"
  4154. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  4155. "Regular expression to match a timestamp time or time range.
  4156. After a match, the following groups carry important information:
  4157. 0 the full match
  4158. 1 date plus weekday, for back referencing to make sure both times are on the same day
  4159. 2 the first time, range or not
  4160. 4 the second time, if it is a range.")
  4161. (defconst org-startup-options
  4162. '(("fold" org-startup-folded t)
  4163. ("overview" org-startup-folded t)
  4164. ("nofold" org-startup-folded nil)
  4165. ("showall" org-startup-folded nil)
  4166. ("showeverything" org-startup-folded showeverything)
  4167. ("content" org-startup-folded content)
  4168. ("indent" org-startup-indented t)
  4169. ("noindent" org-startup-indented nil)
  4170. ("hidestars" org-hide-leading-stars t)
  4171. ("showstars" org-hide-leading-stars nil)
  4172. ("odd" org-odd-levels-only t)
  4173. ("oddeven" org-odd-levels-only nil)
  4174. ("align" org-startup-align-all-tables t)
  4175. ("noalign" org-startup-align-all-tables nil)
  4176. ("inlineimages" org-startup-with-inline-images t)
  4177. ("noinlineimages" org-startup-with-inline-images nil)
  4178. ("latexpreview" org-startup-with-latex-preview t)
  4179. ("nolatexpreview" org-startup-with-latex-preview nil)
  4180. ("customtime" org-display-custom-times t)
  4181. ("logdone" org-log-done time)
  4182. ("lognotedone" org-log-done note)
  4183. ("nologdone" org-log-done nil)
  4184. ("lognoteclock-out" org-log-note-clock-out t)
  4185. ("nolognoteclock-out" org-log-note-clock-out nil)
  4186. ("logrepeat" org-log-repeat state)
  4187. ("lognoterepeat" org-log-repeat note)
  4188. ("logdrawer" org-log-into-drawer t)
  4189. ("nologdrawer" org-log-into-drawer nil)
  4190. ("logstatesreversed" org-log-states-order-reversed t)
  4191. ("nologstatesreversed" org-log-states-order-reversed nil)
  4192. ("nologrepeat" org-log-repeat nil)
  4193. ("logreschedule" org-log-reschedule time)
  4194. ("lognotereschedule" org-log-reschedule note)
  4195. ("nologreschedule" org-log-reschedule nil)
  4196. ("logredeadline" org-log-redeadline time)
  4197. ("lognoteredeadline" org-log-redeadline note)
  4198. ("nologredeadline" org-log-redeadline nil)
  4199. ("logrefile" org-log-refile time)
  4200. ("lognoterefile" org-log-refile note)
  4201. ("nologrefile" org-log-refile nil)
  4202. ("fninline" org-footnote-define-inline t)
  4203. ("nofninline" org-footnote-define-inline nil)
  4204. ("fnlocal" org-footnote-section nil)
  4205. ("fnauto" org-footnote-auto-label t)
  4206. ("fnprompt" org-footnote-auto-label nil)
  4207. ("fnconfirm" org-footnote-auto-label confirm)
  4208. ("fnplain" org-footnote-auto-label plain)
  4209. ("fnadjust" org-footnote-auto-adjust t)
  4210. ("nofnadjust" org-footnote-auto-adjust nil)
  4211. ("constcgs" constants-unit-system cgs)
  4212. ("constSI" constants-unit-system SI)
  4213. ("noptag" org-tag-persistent-alist nil)
  4214. ("hideblocks" org-hide-block-startup t)
  4215. ("nohideblocks" org-hide-block-startup nil)
  4216. ("beamer" org-startup-with-beamer-mode t)
  4217. ("entitiespretty" org-pretty-entities t)
  4218. ("entitiesplain" org-pretty-entities nil))
  4219. "Variable associated with STARTUP options for org-mode.
  4220. Each element is a list of three items: the startup options (as written
  4221. in the #+STARTUP line), the corresponding variable, and the value to set
  4222. this variable to if the option is found. An optional forth element PUSH
  4223. means to push this value onto the list in the variable.")
  4224. (defcustom org-group-tags t
  4225. "When non-nil (the default), use group tags.
  4226. This can be turned on/off through `org-toggle-tags-groups'."
  4227. :group 'org-tags
  4228. :group 'org-startup
  4229. :type 'boolean)
  4230. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  4231. (defun org-toggle-tags-groups ()
  4232. "Toggle support for group tags.
  4233. Support for group tags is controlled by the option
  4234. `org-group-tags', which is non-nil by default."
  4235. (interactive)
  4236. (setq org-group-tags (not org-group-tags))
  4237. (cond ((and (derived-mode-p 'org-agenda-mode)
  4238. org-group-tags)
  4239. (org-agenda-redo))
  4240. ((derived-mode-p 'org-mode)
  4241. (let ((org-inhibit-startup t)) (org-mode))))
  4242. (message "Groups tags support has been turned %s"
  4243. (if org-group-tags "on" "off")))
  4244. (defun org-set-regexps-and-options (&optional tags-only)
  4245. "Precompute regular expressions used in the current buffer.
  4246. When optional argument TAGS-ONLY is non-nil, only compute tags
  4247. related expressions."
  4248. (when (derived-mode-p 'org-mode)
  4249. (let ((alist (org--setup-collect-keywords
  4250. (org-make-options-regexp
  4251. (append '("FILETAGS" "TAGS" "SETUPFILE")
  4252. (and (not tags-only)
  4253. '("ARCHIVE" "CATEGORY" "COLUMNS" "CONSTANTS"
  4254. "LINK" "OPTIONS" "PRIORITIES" "PROPERTY"
  4255. "SEQ_TODO" "STARTUP" "TODO" "TYP_TODO")))))))
  4256. (org--setup-process-tags
  4257. (cdr (assq 'tags alist)) (cdr (assq 'filetags alist)))
  4258. (unless tags-only
  4259. ;; File properties.
  4260. (setq-local org-file-properties (cdr (assq 'property alist)))
  4261. ;; Archive location.
  4262. (let ((archive (cdr (assq 'archive alist))))
  4263. (when archive (setq-local org-archive-location archive)))
  4264. ;; Category.
  4265. (let ((cat (org-string-nw-p (cdr (assq 'category alist)))))
  4266. (when cat
  4267. (setq-local org-category (intern cat))
  4268. (setq-local org-file-properties
  4269. (org--update-property-plist
  4270. "CATEGORY" cat org-file-properties))))
  4271. ;; Columns.
  4272. (let ((column (cdr (assq 'columns alist))))
  4273. (when column (setq-local org-columns-default-format column)))
  4274. ;; Constants.
  4275. (setq org-table-formula-constants-local (cdr (assq 'constants alist)))
  4276. ;; Link abbreviations.
  4277. (let ((links (cdr (assq 'link alist))))
  4278. (when links (setq org-link-abbrev-alist-local (nreverse links))))
  4279. ;; Priorities.
  4280. (let ((priorities (cdr (assq 'priorities alist))))
  4281. (when priorities
  4282. (setq-local org-highest-priority (nth 0 priorities))
  4283. (setq-local org-lowest-priority (nth 1 priorities))
  4284. (setq-local org-default-priority (nth 2 priorities))))
  4285. ;; Scripts.
  4286. (let ((scripts (assq 'scripts alist)))
  4287. (when scripts
  4288. (setq-local org-use-sub-superscripts (cdr scripts))))
  4289. ;; Startup options.
  4290. (let ((startup (cdr (assq 'startup alist))))
  4291. (dolist (option startup)
  4292. (let ((entry (assoc-string option org-startup-options t)))
  4293. (when entry
  4294. (let ((var (nth 1 entry))
  4295. (val (nth 2 entry)))
  4296. (if (not (nth 3 entry)) (set (make-local-variable var) val)
  4297. (unless (listp (symbol-value var))
  4298. (set (make-local-variable var) nil))
  4299. (add-to-list var val)))))))
  4300. ;; TODO keywords.
  4301. (setq-local org-todo-kwd-alist nil)
  4302. (setq-local org-todo-key-alist nil)
  4303. (setq-local org-todo-key-trigger nil)
  4304. (setq-local org-todo-keywords-1 nil)
  4305. (setq-local org-done-keywords nil)
  4306. (setq-local org-todo-heads nil)
  4307. (setq-local org-todo-sets nil)
  4308. (setq-local org-todo-log-states nil)
  4309. (let ((todo-sequences
  4310. (or (nreverse (cdr (assq 'todo alist)))
  4311. (let ((d (default-value 'org-todo-keywords)))
  4312. (if (not (stringp (car d))) d
  4313. ;; XXX: Backward compatibility code.
  4314. (list (cons org-todo-interpretation d)))))))
  4315. (dolist (sequence todo-sequences)
  4316. (let* ((sequence (or (run-hook-with-args-until-success
  4317. 'org-todo-setup-filter-hook sequence)
  4318. sequence))
  4319. (sequence-type (car sequence))
  4320. (keywords (cdr sequence))
  4321. (sep (member "|" keywords))
  4322. names alist)
  4323. (dolist (k (remove "|" keywords))
  4324. (unless (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$"
  4325. k)
  4326. (error "Invalid TODO keyword %s" k))
  4327. (let ((name (match-string 1 k))
  4328. (key (match-string 2 k))
  4329. (log (org-extract-log-state-settings k)))
  4330. (push name names)
  4331. (push (cons name (and key (string-to-char key))) alist)
  4332. (when log (push log org-todo-log-states))))
  4333. (let* ((names (nreverse names))
  4334. (done (if sep (org-remove-keyword-keys (cdr sep))
  4335. (last names)))
  4336. (head (car names))
  4337. (tail (list sequence-type head (car done) (org-last done))))
  4338. (add-to-list 'org-todo-heads head 'append)
  4339. (push names org-todo-sets)
  4340. (setq org-done-keywords (append org-done-keywords done nil))
  4341. (setq org-todo-keywords-1 (append org-todo-keywords-1 names nil))
  4342. (setq org-todo-key-alist
  4343. (append org-todo-key-alist
  4344. (and alist
  4345. (append '((:startgroup))
  4346. (nreverse alist)
  4347. '((:endgroup))))))
  4348. (dolist (k names) (push (cons k tail) org-todo-kwd-alist))))))
  4349. (setq org-todo-sets (nreverse org-todo-sets)
  4350. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  4351. org-todo-key-trigger (delq nil (mapcar #'cdr org-todo-key-alist))
  4352. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist))
  4353. ;; Compute the regular expressions and other local variables.
  4354. ;; Using `org-outline-regexp-bol' would complicate them much,
  4355. ;; because of the fixed white space at the end of that string.
  4356. (unless org-done-keywords
  4357. (setq org-done-keywords
  4358. (and org-todo-keywords-1 (last org-todo-keywords-1))))
  4359. (setq org-not-done-keywords
  4360. (org-delete-all org-done-keywords
  4361. (copy-sequence org-todo-keywords-1))
  4362. org-todo-regexp (regexp-opt org-todo-keywords-1 t)
  4363. org-not-done-regexp (regexp-opt org-not-done-keywords t)
  4364. org-not-done-heading-regexp
  4365. (format org-heading-keyword-regexp-format org-not-done-regexp)
  4366. org-todo-line-regexp
  4367. (format org-heading-keyword-maybe-regexp-format org-todo-regexp)
  4368. org-complex-heading-regexp
  4369. (concat "^\\(\\*+\\)"
  4370. "\\(?: +" org-todo-regexp "\\)?"
  4371. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4372. "\\(?: +\\(.*?\\)\\)??"
  4373. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?")
  4374. "[ \t]*$")
  4375. org-complex-heading-regexp-format
  4376. (concat "^\\(\\*+\\)"
  4377. "\\(?: +" org-todo-regexp "\\)?"
  4378. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4379. "\\(?: +"
  4380. ;; Stats cookies can be stuck to body.
  4381. "\\(?:\\[[0-9%%/]+\\] *\\)*"
  4382. "\\(%s\\)"
  4383. "\\(?: *\\[[0-9%%/]+\\]\\)*"
  4384. "\\)"
  4385. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%%:]+:\\)\\)?")
  4386. "[ \t]*$")
  4387. org-todo-line-tags-regexp
  4388. (concat "^\\(\\*+\\)"
  4389. "\\(?: +" org-todo-regexp "\\)?"
  4390. "\\(?: +\\(.*?\\)\\)??"
  4391. (org-re "\\(?:[ \t]+\\(:[[:alnum:]:_@#%]+:\\)\\)?")
  4392. "[ \t]*$"))
  4393. (org-compute-latex-and-related-regexp)))))
  4394. (defun org--setup-collect-keywords (regexp &optional files alist)
  4395. "Return setup keywords values as an alist.
  4396. REGEXP matches a subset of setup keywords. FILES is a list of
  4397. file names already visited. It is used to avoid circular setup
  4398. files. ALIST, when non-nil, is the alist computed so far.
  4399. Return value contains the following keys: `archive', `category',
  4400. `columns', `constants', `filetags', `link', `priorities',
  4401. `property', `scripts', `startup', `tags' and `todo'."
  4402. (org-with-wide-buffer
  4403. (goto-char (point-min))
  4404. (let ((case-fold-search t))
  4405. (while (re-search-forward regexp nil t)
  4406. (let ((element (org-element-at-point)))
  4407. (when (eq (org-element-type element) 'keyword)
  4408. (let ((key (org-element-property :key element))
  4409. (value (org-element-property :value element)))
  4410. (cond
  4411. ((equal key "ARCHIVE")
  4412. (when (org-string-nw-p value)
  4413. (push (cons 'archive value) alist)))
  4414. ((equal key "CATEGORY") (push (cons 'category value) alist))
  4415. ((equal key "COLUMNS") (push (cons 'columns value) alist))
  4416. ((equal key "CONSTANTS")
  4417. (let* ((constants (assq 'constants alist))
  4418. (store (cdr constants)))
  4419. (dolist (pair (org-split-string value))
  4420. (when (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)"
  4421. pair)
  4422. (let* ((name (match-string 1 pair))
  4423. (value (match-string 2 pair))
  4424. (old (assoc name store)))
  4425. (if old (setcdr old value)
  4426. (push (cons name value) store)))))
  4427. (if constants (setcdr constants store)
  4428. (push (cons 'constants store) alist))))
  4429. ((equal key "FILETAGS")
  4430. (when (org-string-nw-p value)
  4431. (let ((old (assq 'filetags alist))
  4432. (new (apply #'nconc
  4433. (mapcar (lambda (x) (org-split-string x ":"))
  4434. (org-split-string value)))))
  4435. (if old (setcdr old (append new (cdr old)))
  4436. (push (cons 'filetags new) alist)))))
  4437. ((equal key "LINK")
  4438. (when (string-match "\\`\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  4439. (let ((links (assq 'link alist))
  4440. (pair (cons (org-match-string-no-properties 1 value)
  4441. (org-match-string-no-properties 2 value))))
  4442. (if links (push pair (cdr links))
  4443. (push (list 'link pair) alist)))))
  4444. ((equal key "OPTIONS")
  4445. (when (and (org-string-nw-p value)
  4446. (string-match "\\^:\\(t\\|nil\\|{}\\)" value))
  4447. (push (cons 'scripts (read (match-string 1 value))) alist)))
  4448. ((equal key "PRIORITIES")
  4449. (push (cons 'priorities
  4450. (let ((prio (org-split-string value)))
  4451. (if (< (length prio) 3) '(?A ?C ?B)
  4452. (mapcar #'string-to-char prio))))
  4453. alist))
  4454. ((equal key "PROPERTY")
  4455. (when (string-match "\\(\\S-+\\)[ \t]+\\(.*\\)" value)
  4456. (let* ((property (assq 'property alist))
  4457. (value (org--update-property-plist
  4458. (org-match-string-no-properties 1 value)
  4459. (org-match-string-no-properties 2 value)
  4460. (cdr property))))
  4461. (if property (setcdr property value)
  4462. (push (cons 'property value) alist)))))
  4463. ((equal key "STARTUP")
  4464. (let ((startup (assq 'startup alist)))
  4465. (if startup
  4466. (setcdr startup
  4467. (append (cdr startup) (org-split-string value)))
  4468. (push (cons 'startup (org-split-string value)) alist))))
  4469. ((equal key "TAGS")
  4470. (let ((tag-cell (assq 'tags alist)))
  4471. (if tag-cell
  4472. (setcdr tag-cell
  4473. (append (cdr tag-cell)
  4474. '("\\n")
  4475. (org-split-string value)))
  4476. (push (cons 'tags (org-split-string value)) alist))))
  4477. ((member key '("TODO" "SEQ_TODO" "TYP_TODO"))
  4478. (let ((todo (assq 'todo alist))
  4479. (value (cons (if (equal key "TYP_TODO") 'type 'sequence)
  4480. (org-split-string value))))
  4481. (if todo (push value (cdr todo))
  4482. (push (list 'todo value) alist))))
  4483. ((equal key "SETUPFILE")
  4484. (unless buffer-read-only ; Do not check in Gnus messages.
  4485. (let ((f (and (org-string-nw-p value)
  4486. (expand-file-name
  4487. (org-remove-double-quotes value)))))
  4488. (when (and f (file-readable-p f) (not (member f files)))
  4489. (with-temp-buffer
  4490. (setq default-directory (file-name-directory f))
  4491. (insert-file-contents f)
  4492. (setq alist
  4493. ;; Fake Org mode to benefit from cache
  4494. ;; without recurring needlessly.
  4495. (let ((major-mode 'org-mode))
  4496. (org--setup-collect-keywords
  4497. regexp (cons f files) alist)))))))))))))))
  4498. alist)
  4499. (defun org--setup-process-tags (tags filetags)
  4500. "Precompute variables used for tags.
  4501. TAGS is a list of tags and tag group symbols, as strings.
  4502. FILETAGS is a list of tags, as strings."
  4503. ;; Process the file tags.
  4504. (setq-local org-file-tags
  4505. (mapcar #'org-add-prop-inherited filetags))
  4506. ;; Provide default tags if no local tags are found.
  4507. (when (and (not tags) org-tag-alist)
  4508. (setq tags
  4509. (mapcar (lambda (tag)
  4510. (cl-case (car tag)
  4511. (:startgroup "{")
  4512. (:endgroup "}")
  4513. (:startgrouptag "[")
  4514. (:endgrouptag "]")
  4515. (:grouptags ":")
  4516. (:newline "\\n")
  4517. (otherwise (concat (car tag)
  4518. (and (characterp (cdr tag))
  4519. (format "(%c)" (cdr tag)))))))
  4520. org-tag-alist)))
  4521. ;; Process the tags.
  4522. (setq-local org-tag-groups-alist nil)
  4523. (setq-local org-tag-alist nil)
  4524. (let (group-flag)
  4525. (while tags
  4526. (let ((e (car tags)))
  4527. (setq tags (cdr tags))
  4528. (cond
  4529. ((equal e "{")
  4530. (push '(:startgroup) org-tag-alist)
  4531. (when (equal (nth 1 tags) ":") (setq group-flag t)))
  4532. ((equal e "}")
  4533. (push '(:endgroup) org-tag-alist)
  4534. (setq group-flag nil))
  4535. ((equal e "[")
  4536. (push '(:startgrouptag) org-tag-alist)
  4537. (when (equal (nth 1 tags) ":") (setq group-flag t)))
  4538. ((equal e "]")
  4539. (push '(:endgrouptag) org-tag-alist)
  4540. (setq group-flag nil))
  4541. ((equal e ":")
  4542. (push '(:grouptags) org-tag-alist)
  4543. (setq group-flag 'append))
  4544. ((equal e "\\n") (push '(:newline) org-tag-alist))
  4545. ((string-match
  4546. (org-re (concat "\\`\\([[:alnum:]_@#%]+"
  4547. "\\|{.+?}\\)" ; regular expression
  4548. "\\(?:(\\(.\\))\\)?\\'")) e)
  4549. (let ((tag (match-string 1 e))
  4550. (key (and (match-beginning 2)
  4551. (string-to-char (match-string 2 e)))))
  4552. (cond ((eq group-flag 'append)
  4553. (setcar org-tag-groups-alist
  4554. (append (car org-tag-groups-alist) (list tag))))
  4555. (group-flag (push (list tag) org-tag-groups-alist)))
  4556. ;; Push all tags in groups, no matter if they already exist.
  4557. (unless (and (not group-flag) (assoc tag org-tag-alist))
  4558. (push (cons tag key) org-tag-alist))))))))
  4559. (setq org-tag-alist (nreverse org-tag-alist)))
  4560. (defun org-file-contents (file &optional noerror)
  4561. "Return the contents of FILE, as a string."
  4562. (if (and file (file-readable-p file))
  4563. (with-temp-buffer
  4564. (insert-file-contents file)
  4565. (buffer-string))
  4566. (funcall (if noerror 'message 'error)
  4567. "Cannot read file \"%s\"%s"
  4568. file
  4569. (let ((from (buffer-file-name (buffer-base-buffer))))
  4570. (if from (concat " (referenced in file \"" from "\")") "")))))
  4571. (defun org-extract-log-state-settings (x)
  4572. "Extract the log state setting from a TODO keyword string.
  4573. This will extract info from a string like \"WAIT(w@/!)\"."
  4574. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  4575. (let ((kw (match-string 1 x))
  4576. (log1 (and (match-end 3) (match-string 3 x)))
  4577. (log2 (and (match-end 4) (match-string 4 x))))
  4578. (and (or log1 log2)
  4579. (list kw
  4580. (and log1 (if (equal log1 "!") 'time 'note))
  4581. (and log2 (if (equal log2 "!") 'time 'note)))))))
  4582. (defun org-remove-keyword-keys (list)
  4583. "Remove a pair of parenthesis at the end of each string in LIST."
  4584. (mapcar (lambda (x)
  4585. (if (string-match "(.*)$" x)
  4586. (substring x 0 (match-beginning 0))
  4587. x))
  4588. list))
  4589. (defun org-assign-fast-keys (alist)
  4590. "Assign fast keys to a keyword-key alist.
  4591. Respect keys that are already there."
  4592. (let (new e (alt ?0))
  4593. (while (setq e (pop alist))
  4594. (if (or (memq (car e) '(:newline :grouptags :endgroup :startgroup))
  4595. (cdr e)) ;; Key already assigned.
  4596. (push e new)
  4597. (let ((clist (string-to-list (downcase (car e))))
  4598. (used (append new alist)))
  4599. (when (= (car clist) ?@)
  4600. (pop clist))
  4601. (while (and clist (rassoc (car clist) used))
  4602. (pop clist))
  4603. (unless clist
  4604. (while (rassoc alt used)
  4605. (cl-incf alt)))
  4606. (push (cons (car e) (or (car clist) alt)) new))))
  4607. (nreverse new)))
  4608. ;;; Some variables used in various places
  4609. (defvar org-window-configuration nil
  4610. "Used in various places to store a window configuration.")
  4611. (defvar org-selected-window nil
  4612. "Used in various places to store a window configuration.")
  4613. (defvar org-finish-function nil
  4614. "Function to be called when `C-c C-c' is used.
  4615. This is for getting out of special buffers like capture.")
  4616. (defvar org-last-state)
  4617. ;; Defined somewhere in this file, but used before definition.
  4618. (defvar org-entities) ;; defined in org-entities.el
  4619. (defvar org-struct-menu)
  4620. (defvar org-org-menu)
  4621. (defvar org-tbl-menu)
  4622. ;;;; Define the Org-mode
  4623. ;; We use a before-change function to check if a table might need
  4624. ;; an update.
  4625. (defvar org-table-may-need-update t
  4626. "Indicates that a table might need an update.
  4627. This variable is set by `org-before-change-function'.
  4628. `org-table-align' sets it back to nil.")
  4629. (defun org-before-change-function (_beg _end)
  4630. "Every change indicates that a table might need an update."
  4631. (setq org-table-may-need-update t))
  4632. (defvar org-mode-map)
  4633. (defvar org-inhibit-startup-visibility-stuff nil) ; Dynamically-scoped param.
  4634. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  4635. (defvar org-inhibit-logging nil) ; Dynamically-scoped param.
  4636. (defvar org-inhibit-blocking nil) ; Dynamically-scoped param.
  4637. (defvar org-table-buffer-is-an nil)
  4638. (defvar bidi-paragraph-direction)
  4639. (defvar buffer-face-mode-face)
  4640. (require 'outline)
  4641. (when (and (not (keymapp outline-mode-map)) (featurep 'allout))
  4642. (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"))
  4643. (require 'noutline "noutline" 'noerror) ;; stock XEmacs does not have it
  4644. ;; Other stuff we need.
  4645. (require 'time-date)
  4646. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  4647. (require 'easymenu)
  4648. (autoload 'easy-menu-add "easymenu")
  4649. (require 'overlay)
  4650. ;; (require 'org-macs) moved higher up in the file before it is first used
  4651. (require 'org-entities)
  4652. ;; (require 'org-compat) moved higher up in the file before it is first used
  4653. (require 'org-faces)
  4654. (require 'org-list)
  4655. (require 'org-pcomplete)
  4656. (require 'org-src)
  4657. (require 'org-footnote)
  4658. (require 'org-macro)
  4659. ;; babel
  4660. (require 'ob)
  4661. ;;;###autoload
  4662. (define-derived-mode org-mode outline-mode "Org"
  4663. "Outline-based notes management and organizer, alias
  4664. \"Carsten's outline-mode for keeping track of everything.\"
  4665. Org-mode develops organizational tasks around a NOTES file which
  4666. contains information about projects as plain text. Org-mode is
  4667. implemented on top of outline-mode, which is ideal to keep the content
  4668. of large files well structured. It supports ToDo items, deadlines and
  4669. time stamps, which magically appear in the diary listing of the Emacs
  4670. calendar. Tables are easily created with a built-in table editor.
  4671. Plain text URL-like links connect to websites, emails (VM), Usenet
  4672. messages (Gnus), BBDB entries, and any files related to the project.
  4673. For printing and sharing of notes, an Org-mode file (or a part of it)
  4674. can be exported as a structured ASCII or HTML file.
  4675. The following commands are available:
  4676. \\{org-mode-map}"
  4677. ;; Get rid of Outline menus, they are not needed
  4678. ;; Need to do this here because define-derived-mode sets up
  4679. ;; the keymap so late. Still, it is a waste to call this each time
  4680. ;; we switch another buffer into org-mode.
  4681. (if (featurep 'xemacs)
  4682. (when (boundp 'outline-mode-menu-heading)
  4683. ;; Assume this is Greg's port, it uses easymenu
  4684. (easy-menu-remove outline-mode-menu-heading)
  4685. (easy-menu-remove outline-mode-menu-show)
  4686. (easy-menu-remove outline-mode-menu-hide))
  4687. (define-key org-mode-map [menu-bar headings] 'undefined)
  4688. (define-key org-mode-map [menu-bar hide] 'undefined)
  4689. (define-key org-mode-map [menu-bar show] 'undefined))
  4690. (org-load-modules-maybe)
  4691. (when (featurep 'xemacs)
  4692. (easy-menu-add org-org-menu)
  4693. (easy-menu-add org-tbl-menu))
  4694. (org-install-agenda-files-menu)
  4695. (when org-descriptive-links (add-to-invisibility-spec '(org-link)))
  4696. (add-to-invisibility-spec '(org-cwidth))
  4697. (add-to-invisibility-spec '(org-hide-block . t))
  4698. (when (featurep 'xemacs)
  4699. (setq-local line-move-ignore-invisible t))
  4700. (setq-local outline-regexp org-outline-regexp)
  4701. (setq-local outline-level 'org-outline-level)
  4702. (setq bidi-paragraph-direction 'left-to-right)
  4703. (when (and org-ellipsis
  4704. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  4705. (fboundp 'make-glyph-code))
  4706. (unless org-display-table
  4707. (setq org-display-table (make-display-table)))
  4708. (set-display-table-slot
  4709. org-display-table 4
  4710. (vconcat (mapcar
  4711. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  4712. org-ellipsis)))
  4713. (if (stringp org-ellipsis) org-ellipsis "..."))))
  4714. (setq buffer-display-table org-display-table))
  4715. (org-set-regexps-and-options)
  4716. (org-set-font-lock-defaults)
  4717. (when (and org-tag-faces (not org-tags-special-faces-re))
  4718. ;; tag faces set outside customize.... force initialization.
  4719. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  4720. ;; Calc embedded
  4721. (setq-local calc-embedded-open-mode "# ")
  4722. ;; Modify a few syntax entries
  4723. (modify-syntax-entry ?@ "w")
  4724. (modify-syntax-entry ?\" "\"")
  4725. (modify-syntax-entry ?\\ "_")
  4726. (modify-syntax-entry ?~ "_")
  4727. (setq-local font-lock-unfontify-region-function 'org-unfontify-region)
  4728. ;; Activate before-change-function
  4729. (setq-local org-table-may-need-update t)
  4730. (org-add-hook 'before-change-functions 'org-before-change-function nil 'local)
  4731. ;; Check for running clock before killing a buffer
  4732. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  4733. ;; Initialize macros templates.
  4734. (org-macro-initialize-templates)
  4735. ;; Initialize radio targets.
  4736. (org-update-radio-target-regexp)
  4737. ;; Indentation.
  4738. (setq-local indent-line-function 'org-indent-line)
  4739. (setq-local indent-region-function 'org-indent-region)
  4740. ;; Filling and auto-filling.
  4741. (org-setup-filling)
  4742. ;; Comments.
  4743. (org-setup-comments-handling)
  4744. ;; Initialize cache.
  4745. (org-element-cache-reset)
  4746. ;; Beginning/end of defun
  4747. (setq-local beginning-of-defun-function 'org-backward-element)
  4748. (setq-local end-of-defun-function
  4749. (lambda ()
  4750. (if (not (org-at-heading-p))
  4751. (org-forward-element)
  4752. (org-forward-element)
  4753. (forward-char -1))))
  4754. ;; Next error for sparse trees
  4755. (setq-local next-error-function 'org-occur-next-match)
  4756. ;; Make sure dependence stuff works reliably, even for users who set it
  4757. ;; too late :-(
  4758. (if org-enforce-todo-dependencies
  4759. (add-hook 'org-blocker-hook
  4760. 'org-block-todo-from-children-or-siblings-or-parent)
  4761. (remove-hook 'org-blocker-hook
  4762. 'org-block-todo-from-children-or-siblings-or-parent))
  4763. (if org-enforce-todo-checkbox-dependencies
  4764. (add-hook 'org-blocker-hook
  4765. 'org-block-todo-from-checkboxes)
  4766. (remove-hook 'org-blocker-hook
  4767. 'org-block-todo-from-checkboxes))
  4768. ;; Align options lines
  4769. (setq-local
  4770. align-mode-rules-list
  4771. '((org-in-buffer-settings
  4772. (regexp . "^[ \t]*#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  4773. (modes . '(org-mode)))))
  4774. ;; Imenu
  4775. (setq-local imenu-create-index-function 'org-imenu-get-tree)
  4776. ;; Make isearch reveal context
  4777. (setq-local outline-isearch-open-invisible-function
  4778. (lambda (&rest _) (org-show-context 'isearch)))
  4779. ;; Setup the pcomplete hooks
  4780. (setq-local pcomplete-command-completion-function 'org-pcomplete-initial)
  4781. (setq-local pcomplete-command-name-function 'org-command-at-point)
  4782. (setq-local pcomplete-default-completion-function 'ignore)
  4783. (setq-local pcomplete-parse-arguments-function 'org-parse-arguments)
  4784. (setq-local pcomplete-termination-string "")
  4785. (setq-local buffer-face-mode-face 'org-default)
  4786. ;; If empty file that did not turn on Org mode automatically, make
  4787. ;; it to.
  4788. (when (and org-insert-mode-line-in-empty-file
  4789. (org-called-interactively-p 'any)
  4790. (= (point-min) (point-max)))
  4791. (insert "# -*- mode: org -*-\n\n"))
  4792. (unless org-inhibit-startup
  4793. (org-unmodified
  4794. (when org-startup-with-beamer-mode (org-beamer-mode))
  4795. (when org-startup-align-all-tables
  4796. (org-table-map-tables #'org-table-align t))
  4797. (when org-startup-with-inline-images (org-display-inline-images))
  4798. (when org-startup-with-latex-preview (org-toggle-latex-fragment))
  4799. (unless org-inhibit-startup-visibility-stuff (org-set-startup-visibility))
  4800. (when org-startup-truncated (setq truncate-lines t))
  4801. (when org-startup-indented (require 'org-indent) (org-indent-mode 1))
  4802. (org-refresh-effort-properties)))
  4803. ;; Try to set `org-hide' face correctly.
  4804. (let ((foreground (org-find-invisible-foreground)))
  4805. (when foreground
  4806. (set-face-foreground 'org-hide foreground))))
  4807. ;; Update `customize-package-emacs-version-alist'
  4808. (add-to-list 'customize-package-emacs-version-alist
  4809. '(Org ("6.21b" . "23.1") ("6.33x" . "23.2")
  4810. ("7.8.11" . "24.1") ("7.9.4" . "24.3")
  4811. ("8.2.6" . "24.4") ("8.3" . "25.1")))
  4812. (defvar org-mode-transpose-word-syntax-table
  4813. (let ((st (make-syntax-table text-mode-syntax-table)))
  4814. (dolist (c org-emphasis-alist st)
  4815. (modify-syntax-entry (string-to-char (car c)) "w p" st))))
  4816. (when (fboundp 'abbrev-table-put)
  4817. (abbrev-table-put org-mode-abbrev-table
  4818. :parents (list text-mode-abbrev-table)))
  4819. (defun org-find-invisible-foreground ()
  4820. (let ((candidates (remove
  4821. "unspecified-bg"
  4822. (nconc
  4823. (list (face-background 'default)
  4824. (face-background 'org-default))
  4825. (mapcar
  4826. (lambda (alist)
  4827. (when (boundp alist)
  4828. (cdr (assoc 'background-color (symbol-value alist)))))
  4829. '(default-frame-alist initial-frame-alist window-system-default-frame-alist))
  4830. (list (face-foreground 'org-hide))))))
  4831. (car (remove nil candidates))))
  4832. (defun org-current-time (&optional rounding-minutes past)
  4833. "Current time, possibly rounded to ROUNDING-MINUTES.
  4834. When ROUNDING-MINUTES is not an integer, fall back on the car of
  4835. `org-time-stamp-rounding-minutes'. When PAST is non-nil, ensure
  4836. the rounding returns a past time."
  4837. (let ((r (or (and (integerp rounding-minutes) rounding-minutes)
  4838. (car org-time-stamp-rounding-minutes)))
  4839. (time (decode-time)) res)
  4840. (if (< r 1)
  4841. (current-time)
  4842. (setq res
  4843. (apply 'encode-time
  4844. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  4845. (nthcdr 2 time))))
  4846. (if (and past (< (org-float-time (time-subtract (current-time) res)) 0))
  4847. (seconds-to-time (- (org-float-time res) (* r 60)))
  4848. res))))
  4849. (defun org-today ()
  4850. "Return today date, considering `org-extend-today-until'."
  4851. (time-to-days
  4852. (time-subtract (current-time)
  4853. (list 0 (* 3600 org-extend-today-until) 0))))
  4854. ;;;; Font-Lock stuff, including the activators
  4855. (defvar org-mouse-map (make-sparse-keymap))
  4856. (org-defkey org-mouse-map [mouse-2] 'org-open-at-mouse)
  4857. (org-defkey org-mouse-map [mouse-3] 'org-find-file-at-mouse)
  4858. (when org-mouse-1-follows-link
  4859. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  4860. (when org-tab-follows-link
  4861. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  4862. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  4863. (require 'font-lock)
  4864. (defconst org-non-link-chars "]\t\n\r<>")
  4865. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "file+emacs"
  4866. "file+sys" "news" "shell" "elisp" "doi" "message"
  4867. "help"))
  4868. (defvar org-link-types-re nil
  4869. "Matches a link that has a url-like prefix like \"http:\"")
  4870. (defvar org-link-re-with-space nil
  4871. "Matches a link with spaces, optional angular brackets around it.")
  4872. (defvar org-link-re-with-space2 nil
  4873. "Matches a link with spaces, optional angular brackets around it.")
  4874. (defvar org-link-re-with-space3 nil
  4875. "Matches a link with spaces, only for internal part in bracket links.")
  4876. (defvar org-angle-link-re nil
  4877. "Matches link with angular brackets, spaces are allowed.")
  4878. (defvar org-plain-link-re nil
  4879. "Matches plain link, without spaces.")
  4880. (defvar org-bracket-link-regexp nil
  4881. "Matches a link in double brackets.")
  4882. (defvar org-bracket-link-analytic-regexp nil
  4883. "Regular expression used to analyze links.
  4884. Here is what the match groups contain after a match:
  4885. 1: http:
  4886. 2: http
  4887. 3: path
  4888. 4: [desc]
  4889. 5: desc")
  4890. (defvar org-bracket-link-analytic-regexp++ nil
  4891. "Like `org-bracket-link-analytic-regexp', but include coderef internal type.")
  4892. (defvar org-any-link-re nil
  4893. "Regular expression matching any link.")
  4894. (defconst org-match-sexp-depth 3
  4895. "Number of stacked braces for sub/superscript matching.")
  4896. (defun org-create-multibrace-regexp (left right n)
  4897. "Create a regular expression which will match a balanced sexp.
  4898. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  4899. as single character strings.
  4900. The regexp returned will match the entire expression including the
  4901. delimiters. It will also define a single group which contains the
  4902. match except for the outermost delimiters. The maximum depth of
  4903. stacked delimiters is N. Escaping delimiters is not possible."
  4904. (let* ((nothing (concat "[^" left right "]*?"))
  4905. (or "\\|")
  4906. (re nothing)
  4907. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  4908. (while (> n 1)
  4909. (setq n (1- n)
  4910. re (concat re or next)
  4911. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  4912. (concat left "\\(" re "\\)" right)))
  4913. (defconst org-match-substring-regexp
  4914. (concat
  4915. "\\(\\S-\\)\\([_^]\\)\\("
  4916. "\\(?:" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4917. "\\|"
  4918. "\\(?:" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  4919. "\\|"
  4920. "\\(?:\\*\\|[+-]?[[:alnum:].,\\]*[[:alnum:]]\\)\\)")
  4921. "The regular expression matching a sub- or superscript.")
  4922. (defconst org-match-substring-with-braces-regexp
  4923. (concat
  4924. "\\(\\S-\\)\\([_^]\\)"
  4925. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)")
  4926. "The regular expression matching a sub- or superscript, forcing braces.")
  4927. (defun org-make-link-regexps ()
  4928. "Update the link regular expressions.
  4929. This should be called after the variable `org-link-types' has changed."
  4930. (let ((types-re (regexp-opt org-link-types t)))
  4931. (setq org-link-types-re
  4932. (concat "\\`" types-re ":")
  4933. org-link-re-with-space
  4934. (concat "<?" types-re ":"
  4935. "\\([^" org-non-link-chars " ]"
  4936. "[^" org-non-link-chars "]*"
  4937. "[^" org-non-link-chars " ]\\)>?")
  4938. org-link-re-with-space2
  4939. (concat "<?" types-re ":"
  4940. "\\([^" org-non-link-chars " ]"
  4941. "[^\t\n\r]*"
  4942. "[^" org-non-link-chars " ]\\)>?")
  4943. org-link-re-with-space3
  4944. (concat "<?" types-re ":"
  4945. "\\([^" org-non-link-chars " ]"
  4946. "[^\t\n\r]*\\)")
  4947. org-angle-link-re
  4948. (format "<%s:\\([^>\n]*\\(?:\n[ \t]*[^> \t\n][^>\n]*\\)*\\)>"
  4949. types-re)
  4950. org-plain-link-re
  4951. (concat
  4952. "\\<" types-re ":"
  4953. (org-re "\\([^ \t\n()<>]+\\(?:([[:word:]0-9_]+)\\|\\([^[:punct:] \t\n]\\|/\\)\\)\\)"))
  4954. ;; "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  4955. org-bracket-link-regexp
  4956. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  4957. org-bracket-link-analytic-regexp
  4958. (concat
  4959. "\\[\\["
  4960. "\\(" types-re ":\\)?"
  4961. "\\([^]]+\\)"
  4962. "\\]"
  4963. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4964. "\\]")
  4965. org-bracket-link-analytic-regexp++
  4966. (concat
  4967. "\\[\\["
  4968. "\\(" (regexp-opt (cons "coderef" org-link-types) t) ":\\)?"
  4969. "\\([^]]+\\)"
  4970. "\\]"
  4971. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4972. "\\]")
  4973. org-any-link-re
  4974. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  4975. org-angle-link-re "\\)\\|\\("
  4976. org-plain-link-re "\\)"))))
  4977. (org-make-link-regexps)
  4978. (defvar org-emph-face nil)
  4979. (defun org-do-emphasis-faces (limit)
  4980. "Run through the buffer and emphasize strings."
  4981. (let (rtn a)
  4982. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  4983. (let* ((border (char-after (match-beginning 3)))
  4984. (bre (regexp-quote (char-to-string border))))
  4985. (when (and (not (= border (char-after (match-beginning 4))))
  4986. (not (string-match-p (concat bre ".*" bre)
  4987. (replace-regexp-in-string
  4988. "\n" " "
  4989. (substring (match-string 2) 1 -1)))))
  4990. (setq rtn t)
  4991. (setq a (assoc (match-string 3) org-emphasis-alist))
  4992. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  4993. 'face
  4994. (nth 1 a))
  4995. (and (nth 2 a)
  4996. (org-remove-flyspell-overlays-in
  4997. (match-beginning 0) (match-end 0)))
  4998. (add-text-properties (match-beginning 2) (match-end 2)
  4999. '(font-lock-multiline t org-emphasis t))
  5000. (when org-hide-emphasis-markers
  5001. (add-text-properties (match-end 4) (match-beginning 5)
  5002. '(invisible org-link))
  5003. (add-text-properties (match-beginning 3) (match-end 3)
  5004. '(invisible org-link)))))
  5005. (goto-char (1+ (match-beginning 0))))
  5006. rtn))
  5007. (defun org-emphasize (&optional char)
  5008. "Insert or change an emphasis, i.e. a font like bold or italic.
  5009. If there is an active region, change that region to a new emphasis.
  5010. If there is no region, just insert the marker characters and position
  5011. the cursor between them.
  5012. CHAR should be the marker character. If it is a space, it means to
  5013. remove the emphasis of the selected region.
  5014. If CHAR is not given (for example in an interactive call) it will be
  5015. prompted for."
  5016. (interactive)
  5017. (let ((erc org-emphasis-regexp-components)
  5018. (string "") beg end move s)
  5019. (if (org-region-active-p)
  5020. (setq beg (region-beginning)
  5021. end (region-end)
  5022. string (buffer-substring beg end))
  5023. (setq move t))
  5024. (unless char
  5025. (message "Emphasis marker or tag: [%s]"
  5026. (mapconcat #'car org-emphasis-alist ""))
  5027. (setq char (read-char-exclusive)))
  5028. (if (equal char ?\s)
  5029. (setq s ""
  5030. move nil)
  5031. (unless (assoc (char-to-string char) org-emphasis-alist)
  5032. (user-error "No such emphasis marker: \"%c\"" char))
  5033. (setq s (char-to-string char)))
  5034. (while (and (> (length string) 1)
  5035. (equal (substring string 0 1) (substring string -1))
  5036. (assoc (substring string 0 1) org-emphasis-alist))
  5037. (setq string (substring string 1 -1)))
  5038. (setq string (concat s string s))
  5039. (when beg (delete-region beg end))
  5040. (unless (or (bolp)
  5041. (string-match (concat "[" (nth 0 erc) "\n]")
  5042. (char-to-string (char-before (point)))))
  5043. (insert " "))
  5044. (unless (or (eobp)
  5045. (string-match (concat "[" (nth 1 erc) "\n]")
  5046. (char-to-string (char-after (point)))))
  5047. (insert " ") (backward-char 1))
  5048. (insert string)
  5049. (and move (backward-char 1))))
  5050. (defconst org-nonsticky-props
  5051. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text htmlize-link))
  5052. (defsubst org-rear-nonsticky-at (pos)
  5053. (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
  5054. (defun org-activate-plain-links (limit)
  5055. "Add link properties for plain links."
  5056. (when (and (re-search-forward org-plain-link-re limit t)
  5057. (not (org-in-src-block-p)))
  5058. (let ((face (get-text-property (max (1- (match-beginning 0)) (point-min))
  5059. 'face))
  5060. (link (org-match-string-no-properties 0)))
  5061. (unless (if (consp face) (memq 'org-tag face) (eq 'org-tag face))
  5062. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5063. (add-text-properties (match-beginning 0) (match-end 0)
  5064. (list 'mouse-face 'highlight
  5065. 'face 'org-link
  5066. 'htmlize-link `(:uri ,link)
  5067. 'keymap org-mouse-map))
  5068. (org-rear-nonsticky-at (match-end 0))
  5069. t))))
  5070. (defun org-activate-code (limit)
  5071. (when (re-search-forward "^[ \t]*\\(:\\(?: .*\\|$\\)\n?\\)" limit t)
  5072. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5073. (remove-text-properties (match-beginning 0) (match-end 0)
  5074. '(display t invisible t intangible t))
  5075. t))
  5076. (defcustom org-src-fontify-natively t
  5077. "When non-nil, fontify code in code blocks."
  5078. :type 'boolean
  5079. :version "24.4"
  5080. :package-version '(Org . "8.3")
  5081. :group 'org-appearance
  5082. :group 'org-babel)
  5083. (defcustom org-allow-promoting-top-level-subtree nil
  5084. "When non-nil, allow promoting a top level subtree.
  5085. The leading star of the top level headline will be replaced
  5086. by a #."
  5087. :type 'boolean
  5088. :version "24.1"
  5089. :group 'org-appearance)
  5090. (defun org-fontify-meta-lines-and-blocks (limit)
  5091. (condition-case nil
  5092. (org-fontify-meta-lines-and-blocks-1 limit)
  5093. (error (message "org-mode fontification error"))))
  5094. (defun org-fontify-meta-lines-and-blocks-1 (limit)
  5095. "Fontify #+ lines and blocks."
  5096. (let ((case-fold-search t))
  5097. (when (re-search-forward
  5098. "^\\([ \t]*#\\(\\(\\+[a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)[ \t]*\\(\\([^ \t\n]*\\)[ \t]*\\(.*\\)\\)\\)"
  5099. limit t)
  5100. (let ((beg (match-beginning 0))
  5101. (block-start (match-end 0))
  5102. (block-end nil)
  5103. (lang (match-string 7))
  5104. (beg1 (line-beginning-position 2))
  5105. (dc1 (downcase (match-string 2)))
  5106. (dc3 (downcase (match-string 3)))
  5107. end end1 quoting block-type)
  5108. (cond
  5109. ((and (match-end 4) (equal dc3 "+begin"))
  5110. ;; Truly a block
  5111. (setq block-type (downcase (match-string 5))
  5112. quoting (member block-type org-protecting-blocks))
  5113. (when (re-search-forward
  5114. (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
  5115. nil t) ;; on purpose, we look further than LIMIT
  5116. (setq end (min (point-max) (match-end 0))
  5117. end1 (min (point-max) (1- (match-beginning 0))))
  5118. (setq block-end (match-beginning 0))
  5119. (when quoting
  5120. (org-remove-flyspell-overlays-in beg1 end1)
  5121. (remove-text-properties beg end
  5122. '(display t invisible t intangible t)))
  5123. (add-text-properties
  5124. beg end '(font-lock-fontified t font-lock-multiline t))
  5125. (add-text-properties beg beg1 '(face org-meta-line))
  5126. (org-remove-flyspell-overlays-in beg beg1)
  5127. (add-text-properties ; For end_src
  5128. end1 (min (point-max) (1+ end)) '(face org-meta-line))
  5129. (org-remove-flyspell-overlays-in end1 end)
  5130. (cond
  5131. ((and lang (not (string= lang "")) org-src-fontify-natively)
  5132. (org-src-font-lock-fontify-block lang block-start block-end)
  5133. (add-text-properties beg1 block-end '(src-block t)))
  5134. (quoting
  5135. (add-text-properties beg1 (min (point-max) (1+ end1))
  5136. '(face org-block))) ; end of source block
  5137. ((not org-fontify-quote-and-verse-blocks))
  5138. ((string= block-type "quote")
  5139. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-quote)))
  5140. ((string= block-type "verse")
  5141. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-verse))))
  5142. (add-text-properties beg beg1 '(face org-block-begin-line))
  5143. (add-text-properties (min (point-max) (1+ end)) (min (point-max) (1+ end1))
  5144. '(face org-block-end-line))
  5145. t))
  5146. ((member dc1 '("+title:" "+author:" "+email:" "+date:"))
  5147. (org-remove-flyspell-overlays-in
  5148. (match-beginning 0)
  5149. (if (equal "+title:" dc1) (match-end 2) (match-end 0)))
  5150. (add-text-properties
  5151. beg (match-end 3)
  5152. (if (member (intern (substring dc1 1 -1)) org-hidden-keywords)
  5153. '(font-lock-fontified t invisible t)
  5154. '(font-lock-fontified t face org-document-info-keyword)))
  5155. (add-text-properties
  5156. (match-beginning 6) (min (point-max) (1+ (match-end 6)))
  5157. (if (string-equal dc1 "+title:")
  5158. '(font-lock-fontified t face org-document-title)
  5159. '(font-lock-fontified t face org-document-info))))
  5160. ((equal dc1 "+caption:")
  5161. (org-remove-flyspell-overlays-in (match-end 2) (match-end 0))
  5162. (remove-text-properties (match-beginning 0) (match-end 0)
  5163. '(display t invisible t intangible t))
  5164. (add-text-properties (match-beginning 1) (match-end 3)
  5165. '(font-lock-fontified t face org-meta-line))
  5166. (add-text-properties (match-beginning 6) (+ (match-end 6) 1)
  5167. '(font-lock-fontified t face org-block))
  5168. t)
  5169. ((member dc3 '(" " ""))
  5170. (org-remove-flyspell-overlays-in beg (match-end 0))
  5171. (add-text-properties
  5172. beg (match-end 0)
  5173. '(font-lock-fontified t face font-lock-comment-face)))
  5174. (t ;; just any other in-buffer setting, but not indented
  5175. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5176. (remove-text-properties (match-beginning 0) (match-end 0)
  5177. '(display t invisible t intangible t))
  5178. (add-text-properties beg (match-end 0)
  5179. '(font-lock-fontified t face org-meta-line))
  5180. t))))))
  5181. (defun org-fontify-drawers (limit)
  5182. "Fontify drawers."
  5183. (when (re-search-forward org-drawer-regexp limit t)
  5184. (add-text-properties
  5185. (match-beginning 0) (match-end 0)
  5186. '(font-lock-fontified t face org-special-keyword))
  5187. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5188. t))
  5189. (defun org-fontify-macros (limit)
  5190. "Fontify macros."
  5191. (when (re-search-forward "\\({{{\\).+?\\(}}}\\)" limit t)
  5192. (add-text-properties
  5193. (match-beginning 0) (match-end 0)
  5194. '(font-lock-fontified t face org-macro))
  5195. (when org-hide-macro-markers
  5196. (add-text-properties (match-end 2) (match-beginning 2)
  5197. '(invisible t))
  5198. (add-text-properties (match-beginning 1) (match-end 1)
  5199. '(invisible t)))
  5200. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5201. t))
  5202. (defun org-activate-angle-links (limit)
  5203. "Add text properties for angle links."
  5204. (when (and (re-search-forward org-angle-link-re limit t)
  5205. (not (org-in-src-block-p)))
  5206. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5207. (add-text-properties (match-beginning 0) (match-end 0)
  5208. (list 'mouse-face 'highlight
  5209. 'keymap org-mouse-map
  5210. 'font-lock-multiline t))
  5211. (org-rear-nonsticky-at (match-end 0))
  5212. t))
  5213. (defun org-activate-footnote-links (limit)
  5214. "Add text properties for footnotes."
  5215. (let ((fn (org-footnote-next-reference-or-definition limit)))
  5216. (when fn
  5217. (let* ((beg (nth 1 fn))
  5218. (end (nth 2 fn))
  5219. (label (car fn))
  5220. (referencep (/= (line-beginning-position) beg)))
  5221. (when (and referencep (nth 3 fn))
  5222. (save-excursion
  5223. (goto-char beg)
  5224. (search-forward (or label "fn:"))
  5225. (org-remove-flyspell-overlays-in beg (match-end 0))))
  5226. (add-text-properties beg end
  5227. (list 'mouse-face 'highlight
  5228. 'keymap org-mouse-map
  5229. 'help-echo
  5230. (if referencep "Footnote reference"
  5231. "Footnote definition")
  5232. 'font-lock-fontified t
  5233. 'font-lock-multiline t
  5234. 'face 'org-footnote))))))
  5235. (defun org-activate-bracket-links (limit)
  5236. "Add text properties for bracketed links."
  5237. (when (and (re-search-forward org-bracket-link-regexp limit t)
  5238. (not (org-in-src-block-p)))
  5239. (let* ((hl (org-match-string-no-properties 1))
  5240. (help (concat "LINK: " (save-match-data (org-link-unescape hl))))
  5241. (ip (org-maybe-intangible
  5242. (list 'invisible 'org-link
  5243. 'keymap org-mouse-map 'mouse-face 'highlight
  5244. 'font-lock-multiline t 'help-echo help
  5245. 'htmlize-link `(:uri ,hl))))
  5246. (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
  5247. 'font-lock-multiline t 'help-echo help
  5248. 'htmlize-link `(:uri ,hl))))
  5249. ;; We need to remove the invisible property here. Table narrowing
  5250. ;; may have made some of this invisible.
  5251. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5252. (remove-text-properties (match-beginning 0) (match-end 0)
  5253. '(invisible nil))
  5254. (if (match-end 3)
  5255. (progn
  5256. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  5257. (org-rear-nonsticky-at (match-beginning 3))
  5258. (add-text-properties (match-beginning 3) (match-end 3) vp)
  5259. (org-rear-nonsticky-at (match-end 3))
  5260. (add-text-properties (match-end 3) (match-end 0) ip)
  5261. (org-rear-nonsticky-at (match-end 0)))
  5262. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  5263. (org-rear-nonsticky-at (match-beginning 1))
  5264. (add-text-properties (match-beginning 1) (match-end 1) vp)
  5265. (org-rear-nonsticky-at (match-end 1))
  5266. (add-text-properties (match-end 1) (match-end 0) ip)
  5267. (org-rear-nonsticky-at (match-end 0)))
  5268. t)))
  5269. (defun org-activate-dates (limit)
  5270. "Add text properties for dates."
  5271. (when (and (re-search-forward org-tsr-regexp-both limit t)
  5272. (not (equal (char-before (match-beginning 0)) 91)))
  5273. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5274. (add-text-properties (match-beginning 0) (match-end 0)
  5275. (list 'mouse-face 'highlight
  5276. 'keymap org-mouse-map))
  5277. (org-rear-nonsticky-at (match-end 0))
  5278. (when org-display-custom-times
  5279. (if (match-end 3)
  5280. (org-display-custom-time (match-beginning 3) (match-end 3))
  5281. (org-display-custom-time (match-beginning 1) (match-end 1))))
  5282. t))
  5283. (defvar-local org-target-link-regexp nil
  5284. "Regular expression matching radio targets in plain text.")
  5285. (defconst org-target-regexp (let ((border "[^<>\n\r \t]"))
  5286. (format "<<\\(%s\\|%s[^<>\n\r]*%s\\)>>"
  5287. border border border))
  5288. "Regular expression matching a link target.")
  5289. (defconst org-radio-target-regexp (format "<%s>" org-target-regexp)
  5290. "Regular expression matching a radio target.")
  5291. (defconst org-any-target-regexp
  5292. (format "%s\\|%s" org-radio-target-regexp org-target-regexp)
  5293. "Regular expression matching any target.")
  5294. (defun org-activate-target-links (limit)
  5295. "Add text properties for target matches."
  5296. (when org-target-link-regexp
  5297. (let ((case-fold-search t))
  5298. (when (re-search-forward org-target-link-regexp limit t)
  5299. (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
  5300. (add-text-properties (match-beginning 1) (match-end 1)
  5301. (list 'mouse-face 'highlight
  5302. 'keymap org-mouse-map
  5303. 'help-echo "Radio target link"
  5304. 'org-linked-text t))
  5305. (org-rear-nonsticky-at (match-end 1))
  5306. t))))
  5307. (defun org-update-radio-target-regexp ()
  5308. "Find all radio targets in this file and update the regular expression.
  5309. Also refresh fontification if needed."
  5310. (interactive)
  5311. (let ((old-regexp org-target-link-regexp)
  5312. (before-re "\\(?:^\\|[^[:alnum:]]\\)\\(")
  5313. (after-re "\\)\\(?:$\\|[^[:alnum:]]\\)")
  5314. (targets
  5315. (org-with-wide-buffer
  5316. (goto-char (point-min))
  5317. (let (rtn)
  5318. (while (re-search-forward org-radio-target-regexp nil t)
  5319. ;; Make sure point is really within the object.
  5320. (backward-char)
  5321. (let ((obj (org-element-context)))
  5322. (when (eq (org-element-type obj) 'radio-target)
  5323. (cl-pushnew (org-element-property :value obj) rtn
  5324. :test #'equal))))
  5325. rtn))))
  5326. (setq org-target-link-regexp
  5327. (and targets
  5328. (concat before-re
  5329. (mapconcat
  5330. (lambda (x)
  5331. (replace-regexp-in-string
  5332. " +" "\\s-+" (regexp-quote x) t t))
  5333. targets
  5334. "\\|")
  5335. after-re)))
  5336. (unless (equal old-regexp org-target-link-regexp)
  5337. ;; Clean-up cache.
  5338. (let ((regexp (cond ((not old-regexp) org-target-link-regexp)
  5339. ((not org-target-link-regexp) old-regexp)
  5340. (t
  5341. (concat before-re
  5342. (mapconcat
  5343. (lambda (re)
  5344. (substring re (length before-re)
  5345. (- (length after-re))))
  5346. (list old-regexp org-target-link-regexp)
  5347. "\\|")
  5348. after-re)))))
  5349. (org-with-wide-buffer
  5350. (goto-char (point-min))
  5351. (while (re-search-forward regexp nil t)
  5352. (org-element-cache-refresh (match-beginning 1)))))
  5353. ;; Re fontify buffer.
  5354. (when (memq 'radio org-highlight-links)
  5355. (org-restart-font-lock)))))
  5356. (defun org-hide-wide-columns (limit)
  5357. (let (s e)
  5358. (setq s (text-property-any (point) (or limit (point-max))
  5359. 'org-cwidth t))
  5360. (when s
  5361. (setq e (next-single-property-change s 'org-cwidth))
  5362. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  5363. (goto-char e)
  5364. t)))
  5365. (defvar org-latex-and-related-regexp nil
  5366. "Regular expression for highlighting LaTeX, entities and sub/superscript.")
  5367. (defun org-compute-latex-and-related-regexp ()
  5368. "Compute regular expression for LaTeX, entities and sub/superscript.
  5369. Result depends on variable `org-highlight-latex-and-related'."
  5370. (setq-local
  5371. org-latex-and-related-regexp
  5372. (let* ((re-sub
  5373. (cond ((not (memq 'script org-highlight-latex-and-related)) nil)
  5374. ((eq org-use-sub-superscripts '{})
  5375. (list org-match-substring-with-braces-regexp))
  5376. (org-use-sub-superscripts (list org-match-substring-regexp))))
  5377. (re-latex
  5378. (when (memq 'latex org-highlight-latex-and-related)
  5379. (let ((matchers (plist-get org-format-latex-options :matchers)))
  5380. (delq nil
  5381. (mapcar (lambda (x)
  5382. (and (member (car x) matchers) (nth 1 x)))
  5383. org-latex-regexps)))))
  5384. (re-entities
  5385. (when (memq 'entities org-highlight-latex-and-related)
  5386. (list "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)"))))
  5387. (mapconcat 'identity (append re-latex re-entities re-sub) "\\|"))))
  5388. (defun org-do-latex-and-related (limit)
  5389. "Highlight LaTeX snippets and environments, entities and sub/superscript.
  5390. LIMIT bounds the search for syntax to highlight. Stop at first
  5391. highlighted object, if any. Return t if some highlighting was
  5392. done, nil otherwise."
  5393. (when (org-string-nw-p org-latex-and-related-regexp)
  5394. (catch 'found
  5395. (while (re-search-forward org-latex-and-related-regexp limit t)
  5396. (unless
  5397. (cl-some
  5398. (lambda (f)
  5399. (memq f '(org-code org-verbatim underline org-special-keyword)))
  5400. (save-excursion
  5401. (goto-char (1+ (match-beginning 0)))
  5402. (face-at-point nil t)))
  5403. (let ((offset (if (memq (char-after (1+ (match-beginning 0)))
  5404. '(?_ ?^))
  5405. 1
  5406. 0)))
  5407. (font-lock-prepend-text-property
  5408. (+ offset (match-beginning 0)) (match-end 0)
  5409. 'face 'org-latex-and-related)
  5410. (add-text-properties (+ offset (match-beginning 0)) (match-end 0)
  5411. '(font-lock-multiline t)))
  5412. (throw 'found t)))
  5413. nil)))
  5414. (defun org-restart-font-lock ()
  5415. "Restart `font-lock-mode', to force refontification."
  5416. (when (and (boundp 'font-lock-mode) font-lock-mode)
  5417. (font-lock-mode -1)
  5418. (font-lock-mode 1)))
  5419. (defun org-activate-tags (limit)
  5420. (when (re-search-forward
  5421. (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$") 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. (org-rear-nonsticky-at (match-end 1))
  5427. t))
  5428. (defun org-outline-level ()
  5429. "Compute the outline level of the heading at point.
  5430. If this is called at a normal headline, the level is the number
  5431. of stars. Use `org-reduced-level' to remove the effect of
  5432. `org-odd-levels'. Unlike to `org-current-level', this function
  5433. takes into consideration inlinetasks."
  5434. (org-with-wide-buffer
  5435. (end-of-line)
  5436. (if (re-search-backward org-outline-regexp-bol nil t)
  5437. (1- (- (match-end 0) (match-beginning 0)))
  5438. 0)))
  5439. (defvar org-font-lock-keywords nil)
  5440. (defsubst org-re-property (property &optional literal allow-null value)
  5441. "Return a regexp matching a PROPERTY line.
  5442. When optional argument LITERAL is non-nil, do not quote PROPERTY.
  5443. This is useful when PROPERTY is a regexp. When ALLOW-NULL is
  5444. non-nil, match properties even without a value.
  5445. Match group 3 is set to the value when it exists. If there is no
  5446. value and ALLOW-NULL is non-nil, it is set to the empty string.
  5447. With optional argument VALUE, match only property lines with
  5448. that value; in this case, ALLOW-NULL is ignored. VALUE is quoted
  5449. unless LITERAL is non-nil."
  5450. (concat
  5451. "^\\(?4:[ \t]*\\)"
  5452. (format "\\(?1::\\(?2:%s\\):\\)"
  5453. (if literal property (regexp-quote property)))
  5454. (cond (value
  5455. (format "[ \t]+\\(?3:%s\\)\\(?5:[ \t]*\\)$"
  5456. (if literal value (regexp-quote value))))
  5457. (allow-null
  5458. "\\(?:\\(?3:$\\)\\|[ \t]+\\(?3:.*?\\)\\)\\(?5:[ \t]*\\)$")
  5459. (t
  5460. "[ \t]+\\(?3:[^ \r\t\n]+.*?\\)\\(?5:[ \t]*\\)$"))))
  5461. (defconst org-property-re
  5462. (org-re-property "\\S-+" 'literal t)
  5463. "Regular expression matching a property line.
  5464. There are four matching groups:
  5465. 1: :PROPKEY: including the leading and trailing colon,
  5466. 2: PROPKEY without the leading and trailing colon,
  5467. 3: PROPVAL without leading or trailing spaces,
  5468. 4: the indentation of the current line,
  5469. 5: trailing whitespace.")
  5470. (defvar org-font-lock-hook nil
  5471. "Functions to be called for special font lock stuff.")
  5472. (defvar org-font-lock-extra-keywords nil) ;Dynamically scoped.
  5473. (defvar org-font-lock-set-keywords-hook nil
  5474. "Functions that can manipulate `org-font-lock-extra-keywords'.
  5475. This is called after `org-font-lock-extra-keywords' is defined, but before
  5476. it is installed to be used by font lock. This can be useful if something
  5477. needs to be inserted at a specific position in the font-lock sequence.")
  5478. (defun org-font-lock-hook (limit)
  5479. "Run `org-font-lock-hook' within LIMIT."
  5480. (run-hook-with-args 'org-font-lock-hook limit))
  5481. (defun org-set-font-lock-defaults ()
  5482. "Set font lock defaults for the current buffer."
  5483. (let* ((em org-fontify-emphasized-text)
  5484. (lk org-highlight-links)
  5485. (org-font-lock-extra-keywords
  5486. (list
  5487. ;; Call the hook
  5488. '(org-font-lock-hook)
  5489. ;; Headlines
  5490. `(,(if org-fontify-whole-heading-line
  5491. "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
  5492. "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
  5493. (1 (org-get-level-face 1))
  5494. (2 (org-get-level-face 2))
  5495. (3 (org-get-level-face 3)))
  5496. ;; Table lines
  5497. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  5498. (1 'org-table t))
  5499. ;; Table internals
  5500. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  5501. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  5502. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  5503. '("| *\\(<[lrc]?[0-9]*>\\)" (1 'org-formula t))
  5504. ;; Drawers
  5505. '(org-fontify-drawers)
  5506. ;; Properties
  5507. (list org-property-re
  5508. '(1 'org-special-keyword t)
  5509. '(3 'org-property-value t))
  5510. ;; Links
  5511. (when (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  5512. (when (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  5513. (when (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  5514. (when (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  5515. (when (memq 'radio lk) '(org-activate-target-links (1 'org-link t)))
  5516. (when (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  5517. (when (memq 'footnote lk) '(org-activate-footnote-links))
  5518. ;; Targets.
  5519. (list org-any-target-regexp '(0 'org-target t))
  5520. ;; Diary sexps.
  5521. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  5522. ;; Macro
  5523. '(org-fontify-macros)
  5524. '(org-hide-wide-columns (0 nil append))
  5525. ;; TODO keyword
  5526. (list (format org-heading-keyword-regexp-format
  5527. org-todo-regexp)
  5528. '(2 (org-get-todo-face 2) t))
  5529. ;; DONE
  5530. (if org-fontify-done-headline
  5531. (list (format org-heading-keyword-regexp-format
  5532. (concat
  5533. "\\(?:"
  5534. (mapconcat 'regexp-quote org-done-keywords "\\|")
  5535. "\\)"))
  5536. '(2 'org-headline-done t))
  5537. nil)
  5538. ;; Priorities
  5539. '(org-font-lock-add-priority-faces)
  5540. ;; Tags
  5541. '(org-font-lock-add-tag-faces)
  5542. ;; Tags groups
  5543. (when (and org-group-tags org-tag-groups-alist)
  5544. (list (concat org-outline-regexp-bol ".+\\(:"
  5545. (regexp-opt (mapcar 'car org-tag-groups-alist))
  5546. ":\\).*$")
  5547. '(1 'org-tag-group prepend)))
  5548. ;; Special keywords
  5549. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  5550. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  5551. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  5552. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  5553. ;; Emphasis
  5554. (when em
  5555. (if (featurep 'xemacs)
  5556. '(org-do-emphasis-faces (0 nil append))
  5557. '(org-do-emphasis-faces)))
  5558. ;; Checkboxes
  5559. '("^[ \t]*\\(?:[-+*]\\|[0-9]+[.)]\\)[ \t]+\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?\\(\\[[- X]\\]\\)"
  5560. 1 'org-checkbox prepend)
  5561. (when (cdr (assq 'checkbox org-list-automatic-rules))
  5562. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  5563. (0 (org-get-checkbox-statistics-face) t)))
  5564. ;; Description list items
  5565. '("^[ \t]*[-+*][ \t]+\\(.*?[ \t]+::\\)\\([ \t]+\\|$\\)"
  5566. 1 'org-list-dt prepend)
  5567. ;; ARCHIVEd headings
  5568. (list (concat
  5569. org-outline-regexp-bol
  5570. "\\(.*:" org-archive-tag ":.*\\)")
  5571. '(1 'org-archived prepend))
  5572. ;; Specials
  5573. '(org-do-latex-and-related)
  5574. '(org-fontify-entities)
  5575. '(org-raise-scripts)
  5576. ;; Code
  5577. '(org-activate-code (1 'org-code t))
  5578. ;; COMMENT
  5579. (list (format
  5580. "^\\*+\\(?: +%s\\)?\\(?: +\\[#[A-Z0-9]\\]\\)? +\\(?9:%s\\)\\(?: \\|$\\)"
  5581. org-todo-regexp
  5582. org-comment-string)
  5583. '(9 'org-special-keyword t))
  5584. ;; Blocks and meta lines
  5585. '(org-fontify-meta-lines-and-blocks))))
  5586. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  5587. (run-hooks 'org-font-lock-set-keywords-hook)
  5588. ;; Now set the full font-lock-keywords
  5589. (setq-local org-font-lock-keywords org-font-lock-extra-keywords)
  5590. (setq-local font-lock-defaults
  5591. '(org-font-lock-keywords t nil nil backward-paragraph))
  5592. (kill-local-variable 'font-lock-keywords) nil))
  5593. (defun org-toggle-pretty-entities ()
  5594. "Toggle the composition display of entities as UTF8 characters."
  5595. (interactive)
  5596. (setq-local org-pretty-entities (not org-pretty-entities))
  5597. (org-restart-font-lock)
  5598. (if org-pretty-entities
  5599. (message "Entities are now displayed as UTF8 characters")
  5600. (save-restriction
  5601. (widen)
  5602. (org-decompose-region (point-min) (point-max))
  5603. (message "Entities are now displayed as plain text"))))
  5604. (defvar-local org-custom-properties-overlays nil
  5605. "List of overlays used for custom properties.")
  5606. (defun org-toggle-custom-properties-visibility ()
  5607. "Display or hide properties in `org-custom-properties'."
  5608. (interactive)
  5609. (if org-custom-properties-overlays
  5610. (progn (mapc #'delete-overlay org-custom-properties-overlays)
  5611. (setq org-custom-properties-overlays nil))
  5612. (when org-custom-properties
  5613. (org-with-wide-buffer
  5614. (goto-char (point-min))
  5615. (let ((regexp (org-re-property (regexp-opt org-custom-properties) t t)))
  5616. (while (re-search-forward regexp nil t)
  5617. (let ((end (cdr (save-match-data (org-get-property-block)))))
  5618. (when (and end (< (point) end))
  5619. ;; Hide first custom property in current drawer.
  5620. (let ((o (make-overlay (match-beginning 0) (1+ (match-end 0)))))
  5621. (overlay-put o 'invisible t)
  5622. (overlay-put o 'org-custom-property t)
  5623. (push o org-custom-properties-overlays))
  5624. ;; Hide additional custom properties in the same drawer.
  5625. (while (re-search-forward regexp end t)
  5626. (let ((o (make-overlay (match-beginning 0) (1+ (match-end 0)))))
  5627. (overlay-put o 'invisible t)
  5628. (overlay-put o 'org-custom-property t)
  5629. (push o org-custom-properties-overlays)))))
  5630. ;; Each entry is limited to a single property drawer.
  5631. (outline-next-heading)))))))
  5632. (defun org-fontify-entities (limit)
  5633. "Find an entity to fontify."
  5634. (let (ee)
  5635. (when org-pretty-entities
  5636. (catch 'match
  5637. ;; "\_ "-family is left out on purpose. Only the first one,
  5638. ;; i.e., "\_ ", could be fontified anyway, and it would be
  5639. ;; confusing when adding a second white space character.
  5640. (while (re-search-forward
  5641. "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]\n]\\)"
  5642. limit t)
  5643. (when (and (not (org-at-comment-p))
  5644. (setq ee (org-entity-get (match-string 1)))
  5645. (= (length (nth 6 ee)) 1))
  5646. (let* ((end (if (equal (match-string 2) "{}")
  5647. (match-end 2)
  5648. (match-end 1))))
  5649. (add-text-properties
  5650. (match-beginning 0) end
  5651. (list 'font-lock-fontified t))
  5652. (compose-region (match-beginning 0) end
  5653. (nth 6 ee) nil)
  5654. (backward-char 1)
  5655. (throw 'match t))))
  5656. nil))))
  5657. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  5658. "Fontify string S like in Org-mode."
  5659. (with-temp-buffer
  5660. (insert s)
  5661. (let ((org-odd-levels-only odd-levels))
  5662. (org-mode)
  5663. (org-font-lock-ensure)
  5664. (buffer-string))))
  5665. (defvar org-m nil)
  5666. (defvar org-l nil)
  5667. (defvar org-f nil)
  5668. (defun org-get-level-face (n)
  5669. "Get the right face for match N in font-lock matching of headlines."
  5670. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  5671. (when org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  5672. (if org-cycle-level-faces
  5673. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  5674. (setq org-f (nth (1- (min org-l org-n-level-faces)) org-level-faces)))
  5675. (cond
  5676. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  5677. ((eq n 2) org-f)
  5678. (t (unless org-level-color-stars-only org-f))))
  5679. (defun org-face-from-face-or-color (context inherit face-or-color)
  5680. "Create a face list that inherits INHERIT, but sets the foreground color.
  5681. When FACE-OR-COLOR is not a string, just return it."
  5682. (if (stringp face-or-color)
  5683. (list :inherit inherit
  5684. (cdr (assoc context org-faces-easy-properties))
  5685. face-or-color)
  5686. face-or-color))
  5687. (defun org-get-todo-face (kwd)
  5688. "Get the right face for a TODO keyword KWD.
  5689. If KWD is a number, get the corresponding match group."
  5690. (when (numberp kwd) (setq kwd (match-string kwd)))
  5691. (or (org-face-from-face-or-color
  5692. 'todo 'org-todo (cdr (assoc kwd org-todo-keyword-faces)))
  5693. (and (member kwd org-done-keywords) 'org-done)
  5694. 'org-todo))
  5695. (defun org-get-priority-face (priority)
  5696. "Get the right face for PRIORITY.
  5697. PRIORITY is a character."
  5698. (or (org-face-from-face-or-color
  5699. 'priority 'org-priority (cdr (assq priority org-priority-faces)))
  5700. 'org-priority))
  5701. (defun org-get-tag-face (tag)
  5702. "Get the right face for TAG.
  5703. If TAG is a number, get the corresponding match group."
  5704. (let ((tag (if (wholenump tag) (match-string tag) tag)))
  5705. (or (org-face-from-face-or-color
  5706. 'tag 'org-tag (cdr (assoc tag org-tag-faces)))
  5707. 'org-tag)))
  5708. (defun org-font-lock-add-priority-faces (limit)
  5709. "Add the special priority faces."
  5710. (while (re-search-forward "^\\*+ .*?\\(\\[#\\(.\\)\\]\\)" limit t)
  5711. (add-text-properties
  5712. (match-beginning 1) (match-end 1)
  5713. (list 'face (org-get-priority-face (string-to-char (match-string 2)))
  5714. 'font-lock-fontified t))))
  5715. (defun org-font-lock-add-tag-faces (limit)
  5716. "Add the special tag faces."
  5717. (when (and org-tag-faces org-tags-special-faces-re)
  5718. (while (re-search-forward org-tags-special-faces-re limit t)
  5719. (add-text-properties (match-beginning 1) (match-end 1)
  5720. (list 'face (org-get-tag-face 1)
  5721. 'font-lock-fontified t))
  5722. (backward-char 1))))
  5723. (defun org-unfontify-region (beg end &optional _maybe_loudly)
  5724. "Remove fontification and activation overlays from links."
  5725. (font-lock-default-unfontify-region beg end)
  5726. (let* ((buffer-undo-list t)
  5727. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  5728. (inhibit-modification-hooks t)
  5729. deactivate-mark buffer-file-name buffer-file-truename)
  5730. (org-decompose-region beg end)
  5731. (remove-text-properties beg end
  5732. '(mouse-face t keymap t org-linked-text t
  5733. invisible t intangible t
  5734. org-emphasis t))
  5735. (org-remove-font-lock-display-properties beg end)))
  5736. (defconst org-script-display '(((raise -0.3) (height 0.7))
  5737. ((raise 0.3) (height 0.7))
  5738. ((raise -0.5))
  5739. ((raise 0.5)))
  5740. "Display properties for showing superscripts and subscripts.")
  5741. (defun org-remove-font-lock-display-properties (beg end)
  5742. "Remove specific display properties that have been added by font lock.
  5743. The will remove the raise properties that are used to show superscripts
  5744. and subscripts."
  5745. (let (next prop)
  5746. (while (< beg end)
  5747. (setq next (next-single-property-change beg 'display nil end)
  5748. prop (get-text-property beg 'display))
  5749. (when (member prop org-script-display)
  5750. (put-text-property beg next 'display nil))
  5751. (setq beg next))))
  5752. (defun org-raise-scripts (limit)
  5753. "Add raise properties to sub/superscripts."
  5754. (when (and org-pretty-entities org-pretty-entities-include-sub-superscripts
  5755. (re-search-forward
  5756. (if (eq org-use-sub-superscripts t)
  5757. org-match-substring-regexp
  5758. org-match-substring-with-braces-regexp)
  5759. limit t))
  5760. (let* ((pos (point)) table-p comment-p
  5761. (mpos (match-beginning 3))
  5762. (emph-p (get-text-property mpos 'org-emphasis))
  5763. (link-p (get-text-property mpos 'mouse-face))
  5764. (keyw-p (eq 'org-special-keyword (get-text-property mpos 'face))))
  5765. (goto-char (point-at-bol))
  5766. (setq table-p (org-looking-at-p org-table-dataline-regexp)
  5767. comment-p (org-looking-at-p "^[ \t]*#[ +]"))
  5768. (goto-char pos)
  5769. ;; Handle a_b^c
  5770. (when (member (char-after) '(?_ ?^)) (goto-char (1- pos)))
  5771. (unless (or comment-p emph-p link-p keyw-p)
  5772. (put-text-property (match-beginning 3) (match-end 0)
  5773. 'display
  5774. (if (equal (char-after (match-beginning 2)) ?^)
  5775. (nth (if table-p 3 1) org-script-display)
  5776. (nth (if table-p 2 0) org-script-display)))
  5777. (add-text-properties (match-beginning 2) (match-end 2)
  5778. (list 'invisible t
  5779. 'org-dwidth t 'org-dwidth-n 1))
  5780. (if (and (eq (char-after (match-beginning 3)) ?{)
  5781. (eq (char-before (match-end 3)) ?}))
  5782. (progn
  5783. (add-text-properties
  5784. (match-beginning 3) (1+ (match-beginning 3))
  5785. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))
  5786. (add-text-properties
  5787. (1- (match-end 3)) (match-end 3)
  5788. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1)))))
  5789. t)))
  5790. ;;;; Visibility cycling, including org-goto and indirect buffer
  5791. ;;; Cycling
  5792. (defvar-local org-cycle-global-status nil)
  5793. (put 'org-cycle-global-status 'org-state t)
  5794. (defvar-local org-cycle-subtree-status nil)
  5795. (put 'org-cycle-subtree-status 'org-state t)
  5796. (defvar org-inlinetask-min-level)
  5797. (defun org-unlogged-message (&rest args)
  5798. "Display a message, but avoid logging it in the *Messages* buffer."
  5799. (let ((message-log-max nil))
  5800. (apply 'message args)))
  5801. ;;;###autoload
  5802. (defun org-cycle (&optional arg)
  5803. "TAB-action and visibility cycling for Org-mode.
  5804. This is the command invoked in Org-mode by the TAB key. Its main purpose
  5805. is outline visibility cycling, but it also invokes other actions
  5806. in special contexts.
  5807. - When this function is called with a prefix argument, rotate the entire
  5808. buffer through 3 states (global cycling)
  5809. 1. OVERVIEW: Show only top-level headlines.
  5810. 2. CONTENTS: Show all headlines of all levels, but no body text.
  5811. 3. SHOW ALL: Show everything.
  5812. With a double \\[universal-argument] prefix argument, \
  5813. switch to the startup visibility,
  5814. determined by the variable `org-startup-folded', and by any VISIBILITY
  5815. properties in the buffer.
  5816. With a triple \\[universal-argument] prefix argument, \
  5817. show the entire buffer, including any drawers.
  5818. - When inside a table, re-align the table and move to the next field.
  5819. - When point is at the beginning of a headline, rotate the subtree started
  5820. by this line through 3 different states (local cycling)
  5821. 1. FOLDED: Only the main headline is shown.
  5822. 2. CHILDREN: The main headline and the direct children are shown.
  5823. From this state, you can move to one of the children
  5824. and zoom in further.
  5825. 3. SUBTREE: Show the entire subtree, including body text.
  5826. If there is no subtree, switch directly from CHILDREN to FOLDED.
  5827. - When point is at the beginning of an empty headline and the variable
  5828. `org-cycle-level-after-item/entry-creation' is set, cycle the level
  5829. of the headline by demoting and promoting it to likely levels. This
  5830. speeds up creation document structure by pressing TAB once or several
  5831. times right after creating a new headline.
  5832. - When there is a numeric prefix, go up to a heading with level ARG, do
  5833. a `show-subtree' and return to the previous cursor position. If ARG
  5834. is negative, go up that many levels.
  5835. - When point is not at the beginning of a headline, execute the global
  5836. binding for TAB, which is re-indenting the line. See the option
  5837. `org-cycle-emulate-tab' for details.
  5838. - Special case: if point is at the beginning of the buffer and there is
  5839. no headline in line 1, this function will act as if called with prefix arg
  5840. (\\[universal-argument] TAB, same as S-TAB) also when called without prefix arg.
  5841. But only if also the variable `org-cycle-global-at-bob' is t."
  5842. (interactive "P")
  5843. (org-load-modules-maybe)
  5844. (unless (or (run-hook-with-args-until-success 'org-tab-first-hook)
  5845. (and org-cycle-level-after-item/entry-creation
  5846. (or (org-cycle-level)
  5847. (org-cycle-item-indentation))))
  5848. (let* ((limit-level
  5849. (or org-cycle-max-level
  5850. (and (boundp 'org-inlinetask-min-level)
  5851. org-inlinetask-min-level
  5852. (1- org-inlinetask-min-level))))
  5853. (nstars (and limit-level
  5854. (if org-odd-levels-only
  5855. (and limit-level (1- (* limit-level 2)))
  5856. limit-level)))
  5857. (org-outline-regexp
  5858. (if (not (derived-mode-p 'org-mode))
  5859. outline-regexp
  5860. (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ "))))
  5861. (bob-special (and org-cycle-global-at-bob (not arg) (bobp)
  5862. (not (looking-at org-outline-regexp))))
  5863. (org-cycle-hook
  5864. (if bob-special
  5865. (delq 'org-optimize-window-after-visibility-change
  5866. (copy-sequence org-cycle-hook))
  5867. org-cycle-hook))
  5868. (pos (point)))
  5869. (when (or bob-special (equal arg '(4)))
  5870. ;; special case: use global cycling
  5871. (setq arg t))
  5872. (cond
  5873. ((equal arg '(16))
  5874. (setq last-command 'dummy)
  5875. (org-set-startup-visibility)
  5876. (org-unlogged-message "Startup visibility, plus VISIBILITY properties"))
  5877. ((equal arg '(64))
  5878. (outline-show-all)
  5879. (org-unlogged-message "Entire buffer visible, including drawers"))
  5880. ;; Try cdlatex TAB completion
  5881. ((org-try-cdlatex-tab))
  5882. ;; Table: enter it or move to the next field.
  5883. ((org-at-table-p 'any)
  5884. (if (org-at-table.el-p)
  5885. (message "%s" (substitute-command-keys "\\<org-mode-map>\
  5886. Use \\[org-edit-special] to edit table.el tables"))
  5887. (if arg (org-table-edit-field t)
  5888. (org-table-justify-field-maybe)
  5889. (call-interactively 'org-table-next-field))))
  5890. ((run-hook-with-args-until-success
  5891. 'org-tab-after-check-for-table-hook))
  5892. ;; Global cycling: delegate to `org-cycle-internal-global'.
  5893. ((eq arg t) (org-cycle-internal-global))
  5894. ;; Drawers: delegate to `org-flag-drawer'.
  5895. ((save-excursion
  5896. (beginning-of-line 1)
  5897. (looking-at org-drawer-regexp))
  5898. (org-flag-drawer ; toggle block visibility
  5899. (not (get-char-property (match-end 0) 'invisible))))
  5900. ;; Show-subtree, ARG levels up from here.
  5901. ((integerp arg)
  5902. (save-excursion
  5903. (org-back-to-heading)
  5904. (outline-up-heading (if (< arg 0) (- arg)
  5905. (- (funcall outline-level) arg)))
  5906. (org-show-subtree)))
  5907. ;; Inline task: delegate to `org-inlinetask-toggle-visibility'.
  5908. ((and (featurep 'org-inlinetask)
  5909. (org-inlinetask-at-task-p)
  5910. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5911. (org-inlinetask-toggle-visibility))
  5912. ;; At an item/headline: delegate to `org-cycle-internal-local'.
  5913. ((and (or (and org-cycle-include-plain-lists (org-at-item-p))
  5914. (save-excursion (move-beginning-of-line 1)
  5915. (looking-at org-outline-regexp)))
  5916. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5917. (org-cycle-internal-local))
  5918. ;; From there: TAB emulation and template completion.
  5919. (buffer-read-only (org-back-to-heading))
  5920. ((run-hook-with-args-until-success
  5921. 'org-tab-after-check-for-cycling-hook))
  5922. ((org-try-structure-completion))
  5923. ((run-hook-with-args-until-success
  5924. 'org-tab-before-tab-emulation-hook))
  5925. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  5926. (or (not (bolp))
  5927. (not (looking-at org-outline-regexp))))
  5928. (call-interactively (global-key-binding "\t")))
  5929. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  5930. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  5931. (or (and (eq org-cycle-emulate-tab 'white)
  5932. (= (match-end 0) (point-at-eol)))
  5933. (and (eq org-cycle-emulate-tab 'whitestart)
  5934. (>= (match-end 0) pos))))
  5935. t
  5936. (eq org-cycle-emulate-tab t))
  5937. (call-interactively (global-key-binding "\t")))
  5938. (t (save-excursion
  5939. (org-back-to-heading)
  5940. (org-cycle)))))))
  5941. (defun org-cycle-internal-global ()
  5942. "Do the global cycling action."
  5943. ;; Hack to avoid display of messages for .org attachments in Gnus
  5944. (let ((ga (string-match "\\*fontification" (buffer-name))))
  5945. (cond
  5946. ((and (eq last-command this-command)
  5947. (eq org-cycle-global-status 'overview))
  5948. ;; We just created the overview - now do table of contents
  5949. ;; This can be slow in very large buffers, so indicate action
  5950. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  5951. (unless ga (org-unlogged-message "CONTENTS..."))
  5952. (org-content)
  5953. (unless ga (org-unlogged-message "CONTENTS...done"))
  5954. (setq org-cycle-global-status 'contents)
  5955. (run-hook-with-args 'org-cycle-hook 'contents))
  5956. ((and (eq last-command this-command)
  5957. (eq org-cycle-global-status 'contents))
  5958. ;; We just showed the table of contents - now show everything
  5959. (run-hook-with-args 'org-pre-cycle-hook 'all)
  5960. (outline-show-all)
  5961. (unless ga (org-unlogged-message "SHOW ALL"))
  5962. (setq org-cycle-global-status 'all)
  5963. (run-hook-with-args 'org-cycle-hook 'all))
  5964. (t
  5965. ;; Default action: go to overview
  5966. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  5967. (org-overview)
  5968. (unless ga (org-unlogged-message "OVERVIEW"))
  5969. (setq org-cycle-global-status 'overview)
  5970. (run-hook-with-args 'org-cycle-hook 'overview)))))
  5971. (defvar org-called-with-limited-levels nil
  5972. "Non-nil when `org-with-limited-levels' is currently active.")
  5973. (defun org-cycle-internal-local ()
  5974. "Do the local cycling action."
  5975. (let ((goal-column 0) eoh eol eos has-children children-skipped struct)
  5976. ;; First, determine end of headline (EOH), end of subtree or item
  5977. ;; (EOS), and if item or heading has children (HAS-CHILDREN).
  5978. (save-excursion
  5979. (if (org-at-item-p)
  5980. (progn
  5981. (beginning-of-line)
  5982. (setq struct (org-list-struct))
  5983. (setq eoh (point-at-eol))
  5984. (setq eos (org-list-get-item-end-before-blank (point) struct))
  5985. (setq has-children (org-list-has-child-p (point) struct)))
  5986. (org-back-to-heading)
  5987. (setq eoh (save-excursion (outline-end-of-heading) (point)))
  5988. (setq eos (save-excursion (org-end-of-subtree t t)
  5989. (when (bolp) (backward-char)) (point)))
  5990. (setq has-children
  5991. (or (save-excursion
  5992. (let ((level (funcall outline-level)))
  5993. (outline-next-heading)
  5994. (and (org-at-heading-p t)
  5995. (> (funcall outline-level) level))))
  5996. (save-excursion
  5997. (org-list-search-forward (org-item-beginning-re) eos t)))))
  5998. ;; Determine end invisible part of buffer (EOL)
  5999. (beginning-of-line 2)
  6000. ;; XEmacs doesn't have `next-single-char-property-change'
  6001. (if (featurep 'xemacs)
  6002. (while (and (not (eobp)) ;; this is like `next-line'
  6003. (get-char-property (1- (point)) 'invisible))
  6004. (beginning-of-line 2))
  6005. (while (and (not (eobp)) ;; this is like `next-line'
  6006. (get-char-property (1- (point)) 'invisible))
  6007. (goto-char (next-single-char-property-change (point) 'invisible))
  6008. (and (eolp) (beginning-of-line 2))))
  6009. (setq eol (point)))
  6010. ;; Find out what to do next and set `this-command'
  6011. (cond
  6012. ((= eos eoh)
  6013. ;; Nothing is hidden behind this heading
  6014. (unless (org-before-first-heading-p)
  6015. (run-hook-with-args 'org-pre-cycle-hook 'empty))
  6016. (org-unlogged-message "EMPTY ENTRY")
  6017. (setq org-cycle-subtree-status nil)
  6018. (save-excursion
  6019. (goto-char eos)
  6020. (outline-next-heading)
  6021. (when (outline-invisible-p) (org-flag-heading nil))))
  6022. ((and (or (>= eol eos)
  6023. (not (string-match "\\S-" (buffer-substring eol eos))))
  6024. (or has-children
  6025. (not (setq children-skipped
  6026. org-cycle-skip-children-state-if-no-children))))
  6027. ;; Entire subtree is hidden in one line: children view
  6028. (unless (org-before-first-heading-p)
  6029. (run-hook-with-args 'org-pre-cycle-hook 'children))
  6030. (if (org-at-item-p)
  6031. (org-list-set-item-visibility (point-at-bol) struct 'children)
  6032. (org-show-entry)
  6033. (org-with-limited-levels (org-show-children))
  6034. ;; FIXME: This slows down the func way too much.
  6035. ;; How keep drawers hidden in subtree anyway?
  6036. ;; (when (memq 'org-cycle-hide-drawers org-cycle-hook)
  6037. ;; (org-cycle-hide-drawers 'subtree))
  6038. ;; Fold every list in subtree to top-level items.
  6039. (when (eq org-cycle-include-plain-lists 'integrate)
  6040. (save-excursion
  6041. (org-back-to-heading)
  6042. (while (org-list-search-forward (org-item-beginning-re) eos t)
  6043. (beginning-of-line 1)
  6044. (let* ((struct (org-list-struct))
  6045. (prevs (org-list-prevs-alist struct))
  6046. (end (org-list-get-bottom-point struct)))
  6047. (dolist (e (org-list-get-all-items (point) struct prevs))
  6048. (org-list-set-item-visibility e struct 'folded))
  6049. (goto-char (if (< end eos) end eos)))))))
  6050. (org-unlogged-message "CHILDREN")
  6051. (save-excursion
  6052. (goto-char eos)
  6053. (outline-next-heading)
  6054. (when (outline-invisible-p) (org-flag-heading nil)))
  6055. (setq org-cycle-subtree-status 'children)
  6056. (unless (org-before-first-heading-p)
  6057. (run-hook-with-args 'org-cycle-hook 'children)))
  6058. ((or children-skipped
  6059. (and (eq last-command this-command)
  6060. (eq org-cycle-subtree-status 'children)))
  6061. ;; We just showed the children, or no children are there,
  6062. ;; now show everything.
  6063. (unless (org-before-first-heading-p)
  6064. (run-hook-with-args 'org-pre-cycle-hook 'subtree))
  6065. (outline-flag-region eoh eos nil)
  6066. (org-unlogged-message
  6067. (if children-skipped "SUBTREE (NO CHILDREN)" "SUBTREE"))
  6068. (setq org-cycle-subtree-status 'subtree)
  6069. (unless (org-before-first-heading-p)
  6070. (run-hook-with-args 'org-cycle-hook 'subtree)))
  6071. (t
  6072. ;; Default action: hide the subtree.
  6073. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  6074. (outline-flag-region eoh eos t)
  6075. (org-unlogged-message "FOLDED")
  6076. (setq org-cycle-subtree-status 'folded)
  6077. (unless (org-before-first-heading-p)
  6078. (run-hook-with-args 'org-cycle-hook 'folded))))))
  6079. ;;;###autoload
  6080. (defun org-global-cycle (&optional arg)
  6081. "Cycle the global visibility. For details see `org-cycle'.
  6082. With \\[universal-argument] prefix arg, switch to startup visibility.
  6083. With a numeric prefix, show all headlines up to that level."
  6084. (interactive "P")
  6085. (let ((org-cycle-include-plain-lists
  6086. (if (derived-mode-p 'org-mode) org-cycle-include-plain-lists nil)))
  6087. (cond
  6088. ((integerp arg)
  6089. (outline-show-all)
  6090. (outline-hide-sublevels arg)
  6091. (setq org-cycle-global-status 'contents))
  6092. ((equal arg '(4))
  6093. (org-set-startup-visibility)
  6094. (org-unlogged-message "Startup visibility, plus VISIBILITY properties."))
  6095. (t
  6096. (org-cycle '(4))))))
  6097. (defun org-set-startup-visibility ()
  6098. "Set the visibility required by startup options and properties."
  6099. (cond
  6100. ((eq org-startup-folded t)
  6101. (org-overview))
  6102. ((eq org-startup-folded 'content)
  6103. (org-content))
  6104. ((or (eq org-startup-folded 'showeverything)
  6105. (eq org-startup-folded nil))
  6106. (outline-show-all)))
  6107. (unless (eq org-startup-folded 'showeverything)
  6108. (when org-hide-block-startup (org-hide-block-all))
  6109. (org-set-visibility-according-to-property 'no-cleanup)
  6110. (org-cycle-hide-archived-subtrees 'all)
  6111. (org-cycle-hide-drawers 'all)
  6112. (org-cycle-show-empty-lines t)))
  6113. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  6114. "Switch subtree visibilities according to :VISIBILITY: property."
  6115. (interactive)
  6116. (org-with-wide-buffer
  6117. (goto-char (point-min))
  6118. (while (re-search-forward "^[ \t]*:VISIBILITY:" nil t)
  6119. (if (not (org-at-property-p)) (outline-next-heading)
  6120. (let ((state (match-string 3)))
  6121. (save-excursion
  6122. (org-back-to-heading t)
  6123. (outline-hide-subtree)
  6124. (org-reveal)
  6125. (cond
  6126. ((equal state "folded")
  6127. (outline-hide-subtree))
  6128. ((equal state "children")
  6129. (org-show-hidden-entry)
  6130. (org-show-children))
  6131. ((equal state "content")
  6132. (save-excursion
  6133. (save-restriction
  6134. (org-narrow-to-subtree)
  6135. (org-content))))
  6136. ((member state '("all" "showall"))
  6137. (outline-show-subtree)))))))
  6138. (unless no-cleanup
  6139. (org-cycle-hide-archived-subtrees 'all)
  6140. (org-cycle-hide-drawers 'all)
  6141. (org-cycle-show-empty-lines 'all))))
  6142. ;; This function uses outline-regexp instead of the more fundamental
  6143. ;; org-outline-regexp so that org-cycle-global works outside of Org
  6144. ;; buffers, where outline-regexp is needed.
  6145. (defun org-overview ()
  6146. "Switch to overview mode, showing only top-level headlines.
  6147. This shows all headlines with a level equal or greater than the level
  6148. of the first headline in the buffer. This is important, because if the
  6149. first headline is not level one, then (hide-sublevels 1) gives confusing
  6150. results."
  6151. (interactive)
  6152. (save-excursion
  6153. (let ((level
  6154. (save-excursion
  6155. (goto-char (point-min))
  6156. (when (re-search-forward (concat "^" outline-regexp) nil t)
  6157. (goto-char (match-beginning 0))
  6158. (funcall outline-level)))))
  6159. (and level (outline-hide-sublevels level)))))
  6160. (defun org-content (&optional arg)
  6161. "Show all headlines in the buffer, like a table of contents.
  6162. With numerical argument N, show content up to level N."
  6163. (interactive "P")
  6164. (org-overview)
  6165. (save-excursion
  6166. ;; Visit all headings and show their offspring
  6167. (and (integerp arg) (org-overview))
  6168. (goto-char (point-max))
  6169. (catch 'exit
  6170. (while (and (progn (condition-case nil
  6171. (outline-previous-visible-heading 1)
  6172. (error (goto-char (point-min))))
  6173. t)
  6174. (looking-at org-outline-regexp))
  6175. (if (integerp arg)
  6176. (org-show-children (1- arg))
  6177. (outline-show-branches))
  6178. (when (bobp) (throw 'exit nil))))))
  6179. (defun org-optimize-window-after-visibility-change (state)
  6180. "Adjust the window after a change in outline visibility.
  6181. This function is the default value of the hook `org-cycle-hook'."
  6182. (when (get-buffer-window (current-buffer))
  6183. (cond
  6184. ((eq state 'content) nil)
  6185. ((eq state 'all) nil)
  6186. ((eq state 'folded) nil)
  6187. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  6188. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  6189. (defun org-remove-empty-overlays-at (pos)
  6190. "Remove outline overlays that do not contain non-white stuff."
  6191. (dolist (o (overlays-at pos))
  6192. (and (eq 'outline (overlay-get o 'invisible))
  6193. (not (string-match "\\S-" (buffer-substring (overlay-start o)
  6194. (overlay-end o))))
  6195. (delete-overlay o))))
  6196. (defun org-clean-visibility-after-subtree-move ()
  6197. "Fix visibility issues after moving a subtree."
  6198. ;; First, find a reasonable region to look at:
  6199. ;; Start two siblings above, end three below
  6200. (let* ((beg (save-excursion
  6201. (and (org-get-last-sibling)
  6202. (org-get-last-sibling))
  6203. (point)))
  6204. (end (save-excursion
  6205. (and (org-get-next-sibling)
  6206. (org-get-next-sibling)
  6207. (org-get-next-sibling))
  6208. (if (org-at-heading-p)
  6209. (point-at-eol)
  6210. (point))))
  6211. (level (looking-at "\\*+"))
  6212. (re (when level (concat "^" (regexp-quote (match-string 0)) " "))))
  6213. (save-excursion
  6214. (save-restriction
  6215. (narrow-to-region beg end)
  6216. (when re
  6217. ;; Properly fold already folded siblings
  6218. (goto-char (point-min))
  6219. (while (re-search-forward re nil t)
  6220. (when (and (not (outline-invisible-p))
  6221. (save-excursion
  6222. (goto-char (point-at-eol)) (outline-invisible-p)))
  6223. (outline-hide-entry))))
  6224. (org-cycle-show-empty-lines 'overview)
  6225. (org-cycle-hide-drawers 'overview)))))
  6226. (defun org-cycle-show-empty-lines (state)
  6227. "Show empty lines above all visible headlines.
  6228. The region to be covered depends on STATE when called through
  6229. `org-cycle-hook'. Lisp program can use t for STATE to get the
  6230. entire buffer covered. Note that an empty line is only shown if there
  6231. are at least `org-cycle-separator-lines' empty lines before the headline."
  6232. (when (/= org-cycle-separator-lines 0)
  6233. (save-excursion
  6234. (let* ((n (abs org-cycle-separator-lines))
  6235. (re (cond
  6236. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  6237. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  6238. (t (let ((ns (number-to-string (- n 2))))
  6239. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  6240. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  6241. beg end)
  6242. (cond
  6243. ((memq state '(overview contents t))
  6244. (setq beg (point-min) end (point-max)))
  6245. ((memq state '(children folded))
  6246. (setq beg (point)
  6247. end (progn (org-end-of-subtree t t)
  6248. (line-beginning-position 2)))))
  6249. (when beg
  6250. (goto-char beg)
  6251. (while (re-search-forward re end t)
  6252. (unless (get-char-property (match-end 1) 'invisible)
  6253. (let ((e (match-end 1))
  6254. (b (if (>= org-cycle-separator-lines 0)
  6255. (match-beginning 1)
  6256. (save-excursion
  6257. (goto-char (match-beginning 0))
  6258. (skip-chars-backward " \t\n")
  6259. (line-end-position)))))
  6260. (outline-flag-region b e nil))))))))
  6261. ;; Never hide empty lines at the end of the file.
  6262. (save-excursion
  6263. (goto-char (point-max))
  6264. (outline-previous-heading)
  6265. (outline-end-of-heading)
  6266. (when (and (looking-at "[ \t\n]+")
  6267. (= (match-end 0) (point-max)))
  6268. (outline-flag-region (point) (match-end 0) nil))))
  6269. (defun org-show-empty-lines-in-parent ()
  6270. "Move to the parent and re-show empty lines before visible headlines."
  6271. (save-excursion
  6272. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  6273. (org-cycle-show-empty-lines context))))
  6274. (defun org-files-list ()
  6275. "Return `org-agenda-files' list, plus all open org-mode files.
  6276. This is useful for operations that need to scan all of a user's
  6277. open and agenda-wise Org files."
  6278. (let ((files (mapcar 'expand-file-name (org-agenda-files))))
  6279. (dolist (buf (buffer-list))
  6280. (with-current-buffer buf
  6281. (when (and (derived-mode-p 'org-mode) (buffer-file-name))
  6282. (cl-pushnew (expand-file-name (buffer-file-name)) files))))
  6283. files))
  6284. (defsubst org-entry-beginning-position ()
  6285. "Return the beginning position of the current entry."
  6286. (save-excursion (org-back-to-heading t) (point)))
  6287. (defsubst org-entry-end-position ()
  6288. "Return the end position of the current entry."
  6289. (save-excursion (outline-next-heading) (point)))
  6290. (defun org-cycle-hide-drawers (state &optional exceptions)
  6291. "Re-hide all drawers after a visibility state change.
  6292. When non-nil, optional argument EXCEPTIONS is a list of strings
  6293. specifying which drawers should not be hidden."
  6294. (when (and (derived-mode-p 'org-mode)
  6295. (not (memq state '(overview folded contents))))
  6296. (save-excursion
  6297. (let* ((globalp (memq state '(contents all)))
  6298. (beg (if globalp (point-min) (point)))
  6299. (end (if globalp (point-max)
  6300. (if (eq state 'children)
  6301. (save-excursion (outline-next-heading) (point))
  6302. (org-end-of-subtree t)))))
  6303. (goto-char beg)
  6304. (while (re-search-forward org-drawer-regexp (max end (point)) t)
  6305. (unless (member-ignore-case (match-string 1) exceptions)
  6306. (let ((drawer (org-element-at-point)))
  6307. (when (memq (org-element-type drawer) '(drawer property-drawer))
  6308. (org-flag-drawer t drawer)
  6309. ;; Make sure to skip drawer entirely or we might flag
  6310. ;; it another time when matching its ending line with
  6311. ;; `org-drawer-regexp'.
  6312. (goto-char (org-element-property :end drawer))))))))))
  6313. (defun org-flag-drawer (flag &optional element)
  6314. "When FLAG is non-nil, hide the drawer we are at.
  6315. Otherwise make it visible. When optional argument ELEMENT is
  6316. a parsed drawer, as returned by `org-element-at-point', hide or
  6317. show that drawer instead."
  6318. (when (save-excursion
  6319. (beginning-of-line)
  6320. (org-looking-at-p org-drawer-regexp))
  6321. (let ((drawer (or element (org-element-at-point))))
  6322. (when (memq (org-element-type drawer) '(drawer property-drawer))
  6323. (let ((post (org-element-property :post-affiliated drawer)))
  6324. (save-excursion
  6325. (outline-flag-region
  6326. (progn (goto-char post) (line-end-position))
  6327. (progn (goto-char (org-element-property :end drawer))
  6328. (skip-chars-backward " \r\t\n")
  6329. (line-end-position))
  6330. flag))
  6331. ;; When the drawer is hidden away, make sure point lies in
  6332. ;; a visible part of the buffer.
  6333. (when (and flag (> (line-beginning-position) post))
  6334. (goto-char post)))))))
  6335. (defun org-subtree-end-visible-p ()
  6336. "Is the end of the current subtree visible?"
  6337. (pos-visible-in-window-p
  6338. (save-excursion (org-end-of-subtree t) (point))))
  6339. (defun org-first-headline-recenter ()
  6340. "Move cursor to the first headline and recenter the headline."
  6341. (goto-char (point-min))
  6342. (when (re-search-forward (concat "^\\(" org-outline-regexp "\\)") nil t)
  6343. (set-window-start (selected-window) (point-at-bol))))
  6344. ;;; Saving and restoring visibility
  6345. (defun org-outline-overlay-data (&optional use-markers)
  6346. "Return a list of the locations of all outline overlays.
  6347. These are overlays with the `invisible' property value `outline'.
  6348. The return value is a list of cons cells, with start and stop
  6349. positions for each overlay.
  6350. If USE-MARKERS is set, return the positions as markers."
  6351. (let (beg end)
  6352. (save-excursion
  6353. (save-restriction
  6354. (widen)
  6355. (delq nil
  6356. (mapcar (lambda (o)
  6357. (when (eq (overlay-get o 'invisible) 'outline)
  6358. (setq beg (overlay-start o)
  6359. end (overlay-end o))
  6360. (and beg end (> end beg)
  6361. (if use-markers
  6362. (cons (copy-marker beg)
  6363. (copy-marker end t))
  6364. (cons beg end)))))
  6365. (overlays-in (point-min) (point-max))))))))
  6366. (defun org-set-outline-overlay-data (data)
  6367. "Create visibility overlays for all positions in DATA.
  6368. DATA should have been made by `org-outline-overlay-data'."
  6369. (org-with-wide-buffer
  6370. (outline-show-all)
  6371. (dolist (c data) (outline-flag-region (car c) (cdr c) t))))
  6372. ;;; Folding of blocks
  6373. (defvar-local org-hide-block-overlays nil
  6374. "Overlays hiding blocks.")
  6375. (defun org-block-map (function &optional start end)
  6376. "Call FUNCTION at the head of all source blocks in the current buffer.
  6377. Optional arguments START and END can be used to limit the range."
  6378. (let ((start (or start (point-min)))
  6379. (end (or end (point-max))))
  6380. (save-excursion
  6381. (goto-char start)
  6382. (while (and (< (point) end) (re-search-forward org-block-regexp end t))
  6383. (save-excursion
  6384. (save-match-data
  6385. (goto-char (match-beginning 0))
  6386. (funcall function)))))))
  6387. (defun org-hide-block-toggle-all ()
  6388. "Toggle the visibility of all blocks in the current buffer."
  6389. (org-block-map 'org-hide-block-toggle))
  6390. (defun org-hide-block-all ()
  6391. "Fold all blocks in the current buffer."
  6392. (interactive)
  6393. (org-show-block-all)
  6394. (org-block-map 'org-hide-block-toggle-maybe))
  6395. (defun org-show-block-all ()
  6396. "Unfold all blocks in the current buffer."
  6397. (interactive)
  6398. (mapc #'delete-overlay org-hide-block-overlays)
  6399. (setq org-hide-block-overlays nil))
  6400. (defun org-hide-block-toggle-maybe ()
  6401. "Toggle visibility of block at point.
  6402. Unlike to `org-hide-block-toggle', this function does not throw
  6403. an error. Return a non-nil value when toggling is successful."
  6404. (interactive)
  6405. (ignore-errors (org-hide-block-toggle)))
  6406. (defun org-hide-block-toggle (&optional force)
  6407. "Toggle the visibility of the current block.
  6408. When optional argument FORCE is `off', make block visible. If it
  6409. is non-nil, hide it unconditionally. Throw an error when not at
  6410. a block. Return a non-nil value when toggling is successful."
  6411. (interactive)
  6412. (let ((element (org-element-at-point)))
  6413. (unless (memq (org-element-type element)
  6414. '(center-block comment-block dynamic-block example-block
  6415. export-block quote-block special-block
  6416. src-block verse-block))
  6417. (user-error "Not at a block"))
  6418. (let* ((start (save-excursion
  6419. (goto-char (org-element-property :post-affiliated element))
  6420. (line-end-position)))
  6421. (end (save-excursion
  6422. (goto-char (org-element-property :end element))
  6423. (skip-chars-backward " \r\t\n")
  6424. (line-end-position)))
  6425. (overlays (overlays-at start)))
  6426. (cond
  6427. ;; Do nothing when not before or at the block opening line or
  6428. ;; at the block closing line.
  6429. ((let ((eol (line-end-position))) (and (> eol start) (/= eol end))) nil)
  6430. ((and (not (eq force 'off))
  6431. (not (memq t (mapcar
  6432. (lambda (o)
  6433. (eq (overlay-get o 'invisible) 'org-hide-block))
  6434. overlays))))
  6435. (let ((ov (make-overlay start end)))
  6436. (overlay-put ov 'invisible 'org-hide-block)
  6437. ;; Make the block accessible to `isearch'.
  6438. (overlay-put
  6439. ov 'isearch-open-invisible
  6440. (lambda (ov)
  6441. (when (memq ov org-hide-block-overlays)
  6442. (setq org-hide-block-overlays (delq ov org-hide-block-overlays)))
  6443. (when (eq (overlay-get ov 'invisible) 'org-hide-block)
  6444. (delete-overlay ov))))
  6445. (push ov org-hide-block-overlays)
  6446. ;; When the block is hidden away, make sure point is left in
  6447. ;; a visible part of the buffer.
  6448. (when (> (line-beginning-position) start)
  6449. (goto-char start)
  6450. (beginning-of-line))
  6451. ;; Signal successful toggling.
  6452. t))
  6453. ((or (not force) (eq force 'off))
  6454. (dolist (ov overlays t)
  6455. (when (memq ov org-hide-block-overlays)
  6456. (setq org-hide-block-overlays (delq ov org-hide-block-overlays)))
  6457. (when (eq (overlay-get ov 'invisible) 'org-hide-block)
  6458. (delete-overlay ov))))))))
  6459. ;; org-tab-after-check-for-cycling-hook
  6460. (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
  6461. ;; Remove overlays when changing major mode
  6462. (add-hook 'org-mode-hook
  6463. (lambda () (org-add-hook 'change-major-mode-hook
  6464. 'org-show-block-all 'append 'local)))
  6465. ;;; Org-goto
  6466. (defvar org-goto-window-configuration nil)
  6467. (defvar org-goto-marker nil)
  6468. (defvar org-goto-map)
  6469. (defun org-goto-map ()
  6470. "Set the keymap `org-goto'."
  6471. (setq org-goto-map
  6472. (let ((map (make-sparse-keymap)))
  6473. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command
  6474. mouse-drag-region universal-argument org-occur)))
  6475. (dolist (cmd cmds)
  6476. (substitute-key-definition cmd cmd map global-map)))
  6477. (suppress-keymap map)
  6478. (org-defkey map "\C-m" 'org-goto-ret)
  6479. (org-defkey map [(return)] 'org-goto-ret)
  6480. (org-defkey map [(left)] 'org-goto-left)
  6481. (org-defkey map [(right)] 'org-goto-right)
  6482. (org-defkey map [(control ?g)] 'org-goto-quit)
  6483. (org-defkey map "\C-i" 'org-cycle)
  6484. (org-defkey map [(tab)] 'org-cycle)
  6485. (org-defkey map [(down)] 'outline-next-visible-heading)
  6486. (org-defkey map [(up)] 'outline-previous-visible-heading)
  6487. (if org-goto-auto-isearch
  6488. (if (fboundp 'define-key-after)
  6489. (define-key-after map [t] 'org-goto-local-auto-isearch)
  6490. nil)
  6491. (org-defkey map "q" 'org-goto-quit)
  6492. (org-defkey map "n" 'outline-next-visible-heading)
  6493. (org-defkey map "p" 'outline-previous-visible-heading)
  6494. (org-defkey map "f" 'outline-forward-same-level)
  6495. (org-defkey map "b" 'outline-backward-same-level)
  6496. (org-defkey map "u" 'outline-up-heading))
  6497. (org-defkey map "/" 'org-occur)
  6498. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  6499. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  6500. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  6501. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  6502. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  6503. map)))
  6504. (defconst org-goto-help
  6505. "Browse buffer copy, to find location or copy text.%s
  6506. RET=jump to location C-g=quit and return to previous location
  6507. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  6508. (defvar org-goto-start-pos) ; dynamically scoped parameter
  6509. (defun org-goto (&optional alternative-interface)
  6510. "Look up a different location in the current file, keeping current visibility.
  6511. When you want look-up or go to a different location in a
  6512. document, the fastest way is often to fold the entire buffer and
  6513. then dive into the tree. This method has the disadvantage, that
  6514. the previous location will be folded, which may not be what you
  6515. want.
  6516. This command works around this by showing a copy of the current
  6517. buffer in an indirect buffer, in overview mode. You can dive
  6518. into the tree in that copy, use org-occur and incremental search
  6519. to find a location. When pressing RET or `Q', the command
  6520. returns to the original buffer in which the visibility is still
  6521. unchanged. After RET it will also jump to the location selected
  6522. in the indirect buffer and expose the headline hierarchy above.
  6523. With a prefix argument, use the alternative interface: e.g., if
  6524. `org-goto-interface' is `outline' use `outline-path-completion'."
  6525. (interactive "P")
  6526. (org-goto-map)
  6527. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  6528. (org-refile-use-outline-path t)
  6529. (org-refile-target-verify-function nil)
  6530. (interface
  6531. (if (not alternative-interface)
  6532. org-goto-interface
  6533. (if (eq org-goto-interface 'outline)
  6534. 'outline-path-completion
  6535. 'outline)))
  6536. (org-goto-start-pos (point))
  6537. (selected-point
  6538. (if (eq interface 'outline)
  6539. (car (org-get-location (current-buffer) org-goto-help))
  6540. (let ((pa (org-refile-get-location "Goto" nil nil t)))
  6541. (org-refile-check-position pa)
  6542. (nth 3 pa)))))
  6543. (if selected-point
  6544. (progn
  6545. (org-mark-ring-push org-goto-start-pos)
  6546. (goto-char selected-point)
  6547. (when (or (outline-invisible-p) (org-invisible-p2))
  6548. (org-show-context 'org-goto)))
  6549. (message "Quit"))))
  6550. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  6551. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  6552. (defvar org-goto-local-auto-isearch-map) ; defined below
  6553. (defun org-get-location (_buf help)
  6554. "Let the user select a location in current buffer.
  6555. This function uses a recursive edit. It returns the selected position
  6556. or nil."
  6557. (org-no-popups
  6558. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  6559. (isearch-hide-immediately nil)
  6560. (isearch-search-fun-function
  6561. (lambda () 'org-goto-local-search-headings))
  6562. (org-goto-selected-point org-goto-exit-command))
  6563. (save-excursion
  6564. (save-window-excursion
  6565. (delete-other-windows)
  6566. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  6567. (org-pop-to-buffer-same-window
  6568. (condition-case nil
  6569. (make-indirect-buffer (current-buffer) "*org-goto*")
  6570. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  6571. (with-output-to-temp-buffer "*Org Help*"
  6572. (princ (format help (if org-goto-auto-isearch
  6573. " Just type for auto-isearch."
  6574. " n/p/f/b/u to navigate, q to quit."))))
  6575. (org-fit-window-to-buffer (get-buffer-window "*Org Help*"))
  6576. (setq buffer-read-only nil)
  6577. (let ((org-startup-truncated t)
  6578. (org-startup-folded nil)
  6579. (org-startup-align-all-tables nil))
  6580. (org-mode)
  6581. (org-overview))
  6582. (setq buffer-read-only t)
  6583. (if (and (boundp 'org-goto-start-pos)
  6584. (integer-or-marker-p org-goto-start-pos))
  6585. (progn (goto-char org-goto-start-pos)
  6586. (when (outline-invisible-p)
  6587. (org-show-set-visibility 'lineage)))
  6588. (goto-char (point-min)))
  6589. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  6590. (message "Select location and press RET")
  6591. (use-local-map org-goto-map)
  6592. (recursive-edit)))
  6593. (kill-buffer "*org-goto*")
  6594. (cons org-goto-selected-point org-goto-exit-command))))
  6595. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  6596. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  6597. ;; `isearch-other-control-char' was removed in Emacs 24.4.
  6598. (if (fboundp 'isearch-other-control-char)
  6599. (progn
  6600. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  6601. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char))
  6602. (define-key org-goto-local-auto-isearch-map "\C-i" nil)
  6603. (define-key org-goto-local-auto-isearch-map "\C-m" nil)
  6604. (define-key org-goto-local-auto-isearch-map [return] nil))
  6605. (defun org-goto-local-search-headings (string bound noerror)
  6606. "Search and make sure that any matches are in headlines."
  6607. (catch 'return
  6608. (while (if isearch-forward
  6609. (search-forward string bound noerror)
  6610. (search-backward string bound noerror))
  6611. (when (save-match-data
  6612. (and (save-excursion
  6613. (beginning-of-line)
  6614. (looking-at org-complex-heading-regexp))
  6615. (or (not (match-beginning 5))
  6616. (< (point) (match-beginning 5)))))
  6617. (throw 'return (point))))))
  6618. (defun org-goto-local-auto-isearch ()
  6619. "Start isearch."
  6620. (interactive)
  6621. (goto-char (point-min))
  6622. (let ((keys (this-command-keys)))
  6623. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  6624. (isearch-mode t)
  6625. (isearch-process-search-char (string-to-char keys)))))
  6626. (defun org-goto-ret (&optional _arg)
  6627. "Finish `org-goto' by going to the new location."
  6628. (interactive "P")
  6629. (setq org-goto-selected-point (point))
  6630. (setq org-goto-exit-command 'return)
  6631. (throw 'exit nil))
  6632. (defun org-goto-left ()
  6633. "Finish `org-goto' by going to the new location."
  6634. (interactive)
  6635. (if (org-at-heading-p)
  6636. (progn
  6637. (beginning-of-line 1)
  6638. (setq org-goto-selected-point (point)
  6639. org-goto-exit-command 'left)
  6640. (throw 'exit nil))
  6641. (user-error "Not on a heading")))
  6642. (defun org-goto-right ()
  6643. "Finish `org-goto' by going to the new location."
  6644. (interactive)
  6645. (if (org-at-heading-p)
  6646. (progn
  6647. (setq org-goto-selected-point (point)
  6648. org-goto-exit-command 'right)
  6649. (throw 'exit nil))
  6650. (user-error "Not on a heading")))
  6651. (defun org-goto-quit ()
  6652. "Finish `org-goto' without cursor motion."
  6653. (interactive)
  6654. (setq org-goto-selected-point nil)
  6655. (setq org-goto-exit-command 'quit)
  6656. (throw 'exit nil))
  6657. ;;; Indirect buffer display of subtrees
  6658. (defvar org-indirect-dedicated-frame nil
  6659. "This is the frame being used for indirect tree display.")
  6660. (defvar org-last-indirect-buffer nil)
  6661. (defun org-tree-to-indirect-buffer (&optional arg)
  6662. "Create indirect buffer and narrow it to current subtree.
  6663. With a numerical prefix ARG, go up to this level and then take that tree.
  6664. If ARG is negative, go up that many levels.
  6665. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  6666. indirect buffer previously made with this command, to avoid proliferation of
  6667. indirect buffers. However, when you call the command with a \
  6668. \\[universal-argument] prefix, or
  6669. when `org-indirect-buffer-display' is `new-frame', the last buffer
  6670. is kept so that you can work with several indirect buffers at the same time.
  6671. If `org-indirect-buffer-display' is `dedicated-frame', the \
  6672. \\[universal-argument] prefix also
  6673. requests that a new frame be made for the new buffer, so that the dedicated
  6674. frame is not changed."
  6675. (interactive "P")
  6676. (let ((cbuf (current-buffer))
  6677. (cwin (selected-window))
  6678. (pos (point))
  6679. beg end level heading ibuf)
  6680. (save-excursion
  6681. (org-back-to-heading t)
  6682. (when (numberp arg)
  6683. (setq level (org-outline-level))
  6684. (when (< arg 0) (setq arg (+ level arg)))
  6685. (while (> (setq level (org-outline-level)) arg)
  6686. (org-up-heading-safe)))
  6687. (setq beg (point)
  6688. heading (org-get-heading 'no-tags))
  6689. (org-end-of-subtree t t)
  6690. (when (org-at-heading-p) (backward-char 1))
  6691. (setq end (point)))
  6692. (when (and (buffer-live-p org-last-indirect-buffer)
  6693. (not (eq org-indirect-buffer-display 'new-frame))
  6694. (not arg))
  6695. (kill-buffer org-last-indirect-buffer))
  6696. (setq ibuf (org-get-indirect-buffer cbuf heading)
  6697. org-last-indirect-buffer ibuf)
  6698. (cond
  6699. ((or (eq org-indirect-buffer-display 'new-frame)
  6700. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  6701. (select-frame (make-frame))
  6702. (delete-other-windows)
  6703. (org-pop-to-buffer-same-window ibuf)
  6704. (org-set-frame-title heading))
  6705. ((eq org-indirect-buffer-display 'dedicated-frame)
  6706. (raise-frame
  6707. (select-frame (or (and org-indirect-dedicated-frame
  6708. (frame-live-p org-indirect-dedicated-frame)
  6709. org-indirect-dedicated-frame)
  6710. (setq org-indirect-dedicated-frame (make-frame)))))
  6711. (delete-other-windows)
  6712. (org-pop-to-buffer-same-window ibuf)
  6713. (org-set-frame-title (concat "Indirect: " heading)))
  6714. ((eq org-indirect-buffer-display 'current-window)
  6715. (org-pop-to-buffer-same-window ibuf))
  6716. ((eq org-indirect-buffer-display 'other-window)
  6717. (pop-to-buffer ibuf))
  6718. (t (error "Invalid value")))
  6719. (when (featurep 'xemacs)
  6720. (save-excursion (org-mode) (turn-on-font-lock)))
  6721. (narrow-to-region beg end)
  6722. (outline-show-all)
  6723. (goto-char pos)
  6724. (run-hook-with-args 'org-cycle-hook 'all)
  6725. (and (window-live-p cwin) (select-window cwin))))
  6726. (defun org-get-indirect-buffer (&optional buffer heading)
  6727. (setq buffer (or buffer (current-buffer)))
  6728. (let ((n 1) (base (buffer-name buffer)) bname)
  6729. (while (buffer-live-p
  6730. (get-buffer
  6731. (setq bname
  6732. (concat base "-"
  6733. (if heading (concat heading "-" (number-to-string n))
  6734. (number-to-string n))))))
  6735. (setq n (1+ n)))
  6736. (condition-case nil
  6737. (make-indirect-buffer buffer bname 'clone)
  6738. (error (make-indirect-buffer buffer bname)))))
  6739. (defun org-set-frame-title (title)
  6740. "Set the title of the current frame to the string TITLE."
  6741. ;; FIXME: how to name a single frame in XEmacs???
  6742. (unless (featurep 'xemacs)
  6743. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  6744. ;;;; Structure editing
  6745. ;;; Inserting headlines
  6746. (defun org--line-empty-p (n)
  6747. "Is the Nth next line empty?
  6748. Counts the current line as N = 1 and the previous line as N = 0;
  6749. see `beginning-of-line'."
  6750. (save-excursion
  6751. (and (not (bobp))
  6752. (or (beginning-of-line n) t)
  6753. (save-match-data
  6754. (looking-at "[ \t]*$")))))
  6755. (defun org-previous-line-empty-p ()
  6756. "Is the previous line a blank line?
  6757. When NEXT is non-nil, check the next line instead."
  6758. (org--line-empty-p 0))
  6759. (defun org-next-line-empty-p ()
  6760. "Is the previous line a blank line?
  6761. When NEXT is non-nil, check the next line instead."
  6762. (org--line-empty-p 2))
  6763. (defun org-insert-heading (&optional arg invisible-ok top)
  6764. "Insert a new heading or an item with the same depth at point.
  6765. If point is at the beginning of a heading or a list item, insert
  6766. a new heading or a new item above the current one. If point is
  6767. at the beginning of a normal line, turn the line into a heading.
  6768. If point is in the middle of a headline or a list item, split the
  6769. headline or the item and create a new headline/item with the text
  6770. in the current line after point \(see `org-M-RET-may-split-line'
  6771. on how to modify this behavior).
  6772. With one universal prefix argument, set the user option
  6773. `org-insert-heading-respect-content' to t for the duration of
  6774. the command. This modifies the behavior described above in this
  6775. ways: on list items and at the beginning of normal lines, force
  6776. the insertion of a heading after the current subtree.
  6777. With two universal prefix arguments, insert the heading at the
  6778. end of the grandparent subtree. For example, if point is within
  6779. a 2nd-level heading, then it will insert a 2nd-level heading at
  6780. the end of the 1st-level parent heading.
  6781. If point is at the beginning of a headline, insert a sibling
  6782. before the current headline. If point is not at the beginning,
  6783. split the line and create a new headline with the text in the
  6784. current line after point \(see `org-M-RET-may-split-line' on how
  6785. to modify this behavior).
  6786. If point is at the beginning of a normal line, turn this line
  6787. into a heading.
  6788. When INVISIBLE-OK is set, stop at invisible headlines when going
  6789. back. This is important for non-interactive uses of the
  6790. command.
  6791. When optional argument TOP is non-nil, insert a level 1 heading,
  6792. unconditionally."
  6793. (interactive "P")
  6794. (when (org-called-interactively-p 'any) (org-reveal))
  6795. (let ((itemp (and (not top) (org-in-item-p)))
  6796. (may-split (org-get-alist-option org-M-RET-may-split-line 'headline))
  6797. (respect-content (or org-insert-heading-respect-content
  6798. (equal arg '(4))))
  6799. (initial-content ""))
  6800. (cond
  6801. ((or (= (buffer-size) 0)
  6802. (and (not (save-excursion
  6803. (and (ignore-errors (org-back-to-heading invisible-ok))
  6804. (org-at-heading-p))))
  6805. (or arg (not itemp))))
  6806. ;; At beginning of buffer or so high up that only a heading
  6807. ;; makes sense.
  6808. (cond ((and (bolp) (not respect-content)) (insert "* "))
  6809. ((not respect-content)
  6810. (unless may-split (end-of-line))
  6811. (insert "\n* "))
  6812. ((re-search-forward org-outline-regexp-bol nil t)
  6813. (beginning-of-line)
  6814. (insert "* \n")
  6815. (backward-char))
  6816. (t (goto-char (point-max))
  6817. (insert "\n* ")))
  6818. (run-hooks 'org-insert-heading-hook))
  6819. ((and itemp (not (member arg '((4) (16)))) (org-insert-item)))
  6820. (t
  6821. ;; Maybe move at the end of the subtree
  6822. (when (equal arg '(16))
  6823. (org-up-heading-safe)
  6824. (org-end-of-subtree t))
  6825. ;; Insert a heading
  6826. (save-restriction
  6827. (widen)
  6828. (let* ((level nil)
  6829. (on-heading (org-at-heading-p))
  6830. (empty-line-p (if on-heading
  6831. (org-previous-line-empty-p)
  6832. ;; We will decide later
  6833. nil))
  6834. ;; Get a level string to fall back on.
  6835. (fix-level
  6836. (if (org-before-first-heading-p) "*"
  6837. (save-excursion
  6838. (org-back-to-heading t)
  6839. (when (org-previous-line-empty-p) (setq empty-line-p t))
  6840. (looking-at org-outline-regexp)
  6841. (make-string (1- (length (match-string 0))) ?*))))
  6842. (stars
  6843. (save-excursion
  6844. (condition-case nil
  6845. (if top "* "
  6846. (org-back-to-heading invisible-ok)
  6847. (when (and (not on-heading)
  6848. (featurep 'org-inlinetask)
  6849. (integerp org-inlinetask-min-level)
  6850. (>= (length (match-string 0))
  6851. org-inlinetask-min-level))
  6852. ;; Find a heading level before the inline
  6853. ;; task.
  6854. (while (and (setq level (org-up-heading-safe))
  6855. (>= level org-inlinetask-min-level)))
  6856. (if (org-at-heading-p)
  6857. (org-back-to-heading invisible-ok)
  6858. (error "This should not happen")))
  6859. (unless (and (save-excursion
  6860. (save-match-data
  6861. (org-backward-heading-same-level
  6862. 1 invisible-ok))
  6863. (= (point) (match-beginning 0)))
  6864. (not (org-next-line-empty-p)))
  6865. (setq empty-line-p (or empty-line-p
  6866. (org-previous-line-empty-p))))
  6867. (match-string 0))
  6868. (error (or fix-level "* ")))))
  6869. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  6870. (blank (if (eq blank-a 'auto) empty-line-p blank-a)))
  6871. ;; If we insert after content, move there and clean up
  6872. ;; whitespace.
  6873. (when (and respect-content
  6874. (not (org-looking-at-p org-outline-regexp-bol)))
  6875. (if (not (org-before-first-heading-p))
  6876. (org-end-of-subtree nil t)
  6877. (re-search-forward org-outline-regexp-bol)
  6878. (beginning-of-line 0))
  6879. (skip-chars-backward " \r\t\n")
  6880. (and (not (org-looking-back "^\\*+" (line-beginning-position)))
  6881. (looking-at "[ \t]+") (replace-match ""))
  6882. (unless (eobp) (forward-char 1))
  6883. (when (looking-at "^\\*")
  6884. (unless (bobp) (backward-char 1))
  6885. (insert "\n")))
  6886. ;; If we are splitting, grab the text that should be moved
  6887. ;; to the new headline.
  6888. (when may-split
  6889. (if (org-at-heading-p)
  6890. ;; This is a heading: split intelligently (keeping
  6891. ;; tags).
  6892. (let ((pos (point)))
  6893. (beginning-of-line)
  6894. (unless (looking-at org-complex-heading-regexp)
  6895. (error "This should not happen"))
  6896. (when (and (match-beginning 4)
  6897. (> pos (match-beginning 4))
  6898. (< pos (match-end 4)))
  6899. (setq initial-content (buffer-substring pos (match-end 4)))
  6900. (goto-char pos)
  6901. (delete-region (point) (match-end 4))
  6902. (if (looking-at "[ \t]*$")
  6903. (replace-match "")
  6904. (insert (make-string (length initial-content) ?\s)))
  6905. (setq initial-content (org-trim initial-content)))
  6906. (goto-char pos))
  6907. ;; A normal line.
  6908. (setq initial-content
  6909. (org-trim
  6910. (delete-and-extract-region (point) (line-end-position))))))
  6911. ;; If we are at the beginning of the line, insert before it.
  6912. ;; Otherwise, after it.
  6913. (cond
  6914. ((and (bolp) (looking-at "[ \t]*$")))
  6915. ((bolp) (save-excursion (insert "\n")))
  6916. (t (end-of-line)
  6917. (insert "\n")))
  6918. ;; Insert the new heading
  6919. (insert stars)
  6920. (just-one-space)
  6921. (insert initial-content)
  6922. (unless (and blank (org-previous-line-empty-p))
  6923. (org-N-empty-lines-before-current (if blank 1 0)))
  6924. ;; Adjust visibility, which may be messed up if we removed
  6925. ;; blank lines while previous entry was hidden.
  6926. (let ((bol (line-beginning-position)))
  6927. (dolist (o (overlays-at (1- bol)))
  6928. (when (and (eq (overlay-get o 'invisible) 'outline)
  6929. (eq (overlay-end o) bol))
  6930. (move-overlay o (overlay-start o) (1- bol)))))
  6931. (run-hooks 'org-insert-heading-hook)))))))
  6932. (defun org-N-empty-lines-before-current (N)
  6933. "Make the number of empty lines before current exactly N.
  6934. So this will delete or add empty lines."
  6935. (save-excursion
  6936. (beginning-of-line)
  6937. (let ((p (point)))
  6938. (skip-chars-backward " \r\t\n")
  6939. (unless (bolp) (forward-line))
  6940. (delete-region (point) p))
  6941. (when (> N 0) (insert (make-string N ?\n)))))
  6942. (defun org-get-heading (&optional no-tags no-todo)
  6943. "Return the heading of the current entry, without the stars.
  6944. When NO-TAGS is non-nil, don't include tags.
  6945. When NO-TODO is non-nil, don't include TODO keywords."
  6946. (save-excursion
  6947. (org-back-to-heading t)
  6948. (cond
  6949. ((and no-tags no-todo)
  6950. (looking-at org-complex-heading-regexp)
  6951. (match-string 4))
  6952. (no-tags
  6953. (looking-at (concat org-outline-regexp
  6954. "\\(.*?\\)"
  6955. "\\(?:[ \t]+:[[:alnum:]:_@#%]+:\\)?[ \t]*$"))
  6956. (match-string 1))
  6957. (no-todo
  6958. (looking-at org-todo-line-regexp)
  6959. (match-string 3))
  6960. (t (looking-at org-heading-regexp)
  6961. (match-string 2)))))
  6962. (defvar orgstruct-mode) ; defined below
  6963. (defun org-heading-components ()
  6964. "Return the components of the current heading.
  6965. This is a list with the following elements:
  6966. - the level as an integer
  6967. - the reduced level, different if `org-odd-levels-only' is set.
  6968. - the TODO keyword, or nil
  6969. - the priority character, like ?A, or nil if no priority is given
  6970. - the headline text itself, or the tags string if no headline text
  6971. - the tags string, or nil."
  6972. (save-excursion
  6973. (org-back-to-heading t)
  6974. (when (let (case-fold-search)
  6975. (looking-at
  6976. (if orgstruct-mode
  6977. org-heading-regexp
  6978. org-complex-heading-regexp)))
  6979. (if orgstruct-mode
  6980. (list (length (match-string 1))
  6981. (org-reduced-level (length (match-string 1)))
  6982. nil
  6983. nil
  6984. (match-string 2)
  6985. nil)
  6986. (list (length (match-string 1))
  6987. (org-reduced-level (length (match-string 1)))
  6988. (org-match-string-no-properties 2)
  6989. (and (match-end 3) (aref (match-string 3) 2))
  6990. (org-match-string-no-properties 4)
  6991. (org-match-string-no-properties 5))))))
  6992. (defun org-get-entry ()
  6993. "Get the entry text, after heading, entire subtree."
  6994. (save-excursion
  6995. (org-back-to-heading t)
  6996. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  6997. (defun org-edit-headline (&optional heading)
  6998. "Edit the current headline.
  6999. Set it to HEADING when provided."
  7000. (interactive)
  7001. (org-with-wide-buffer
  7002. (org-back-to-heading t)
  7003. (when (looking-at org-complex-heading-regexp)
  7004. (let* ((old (match-string-no-properties 4))
  7005. (new (org-trim (or heading (read-string "Edit: " old)))))
  7006. (unless (equal old new)
  7007. (if old (replace-match new t t nil 4)
  7008. (goto-char (or (match-end 3) (match-end 2) (match-end 1)))
  7009. (insert " " new))
  7010. (org-set-tags nil t)
  7011. (when (looking-at "[ \t]*$") (replace-match "")))))))
  7012. (defun org-insert-heading-after-current ()
  7013. "Insert a new heading with same level as current, after current subtree."
  7014. (interactive)
  7015. (org-back-to-heading)
  7016. (org-insert-heading)
  7017. (org-move-subtree-down)
  7018. (end-of-line 1))
  7019. (defun org-insert-heading-respect-content (&optional invisible-ok)
  7020. "Insert heading with `org-insert-heading-respect-content' set to t."
  7021. (interactive)
  7022. (org-insert-heading '(4) invisible-ok))
  7023. (defun org-insert-todo-heading-respect-content (&optional force-state)
  7024. "Insert TODO heading with `org-insert-heading-respect-content' set to t."
  7025. (interactive)
  7026. (org-insert-todo-heading force-state '(4)))
  7027. (defun org-insert-todo-heading (arg &optional force-heading)
  7028. "Insert a new heading with the same level and TODO state as current heading.
  7029. If the heading has no TODO state, or if the state is DONE, use the first
  7030. state (TODO by default). Also with one prefix arg, force first state. With
  7031. two prefix args, force inserting at the end of the parent subtree."
  7032. (interactive "P")
  7033. (when (or force-heading (not (org-insert-item 'checkbox)))
  7034. (org-insert-heading (or (and (equal arg '(16)) '(16))
  7035. force-heading))
  7036. (save-excursion
  7037. (org-back-to-heading)
  7038. (outline-previous-heading)
  7039. (looking-at org-todo-line-regexp))
  7040. (let*
  7041. ((new-mark-x
  7042. (if (or (equal arg '(4))
  7043. (not (match-beginning 2))
  7044. (member (match-string 2) org-done-keywords))
  7045. (car org-todo-keywords-1)
  7046. (match-string 2)))
  7047. (new-mark
  7048. (or
  7049. (run-hook-with-args-until-success
  7050. 'org-todo-get-default-hook new-mark-x nil)
  7051. new-mark-x)))
  7052. (beginning-of-line 1)
  7053. (and (looking-at org-outline-regexp) (goto-char (match-end 0))
  7054. (if org-treat-insert-todo-heading-as-state-change
  7055. (org-todo new-mark)
  7056. (insert new-mark " "))))
  7057. (when org-provide-todo-statistics
  7058. (org-update-parent-todo-statistics))))
  7059. (defun org-insert-subheading (arg)
  7060. "Insert a new subheading and demote it.
  7061. Works for outline headings and for plain lists alike."
  7062. (interactive "P")
  7063. (org-insert-heading arg)
  7064. (cond
  7065. ((org-at-heading-p) (org-do-demote))
  7066. ((org-at-item-p) (org-indent-item))))
  7067. (defun org-insert-todo-subheading (arg)
  7068. "Insert a new subheading with TODO keyword or checkbox and demote it.
  7069. Works for outline headings and for plain lists alike."
  7070. (interactive "P")
  7071. (org-insert-todo-heading arg)
  7072. (cond
  7073. ((org-at-heading-p) (org-do-demote))
  7074. ((org-at-item-p) (org-indent-item))))
  7075. ;;; Promotion and Demotion
  7076. (defvar org-after-demote-entry-hook nil
  7077. "Hook run after an entry has been demoted.
  7078. The cursor will be at the beginning of the entry.
  7079. When a subtree is being demoted, the hook will be called for each node.")
  7080. (defvar org-after-promote-entry-hook nil
  7081. "Hook run after an entry has been promoted.
  7082. The cursor will be at the beginning of the entry.
  7083. When a subtree is being promoted, the hook will be called for each node.")
  7084. (defun org-promote-subtree ()
  7085. "Promote the entire subtree.
  7086. See also `org-promote'."
  7087. (interactive)
  7088. (save-excursion
  7089. (org-with-limited-levels (org-map-tree 'org-promote)))
  7090. (org-fix-position-after-promote))
  7091. (defun org-demote-subtree ()
  7092. "Demote the entire subtree.
  7093. See `org-demote' and `org-promote'."
  7094. (interactive)
  7095. (save-excursion
  7096. (org-with-limited-levels (org-map-tree 'org-demote)))
  7097. (org-fix-position-after-promote))
  7098. (defun org-do-promote ()
  7099. "Promote the current heading higher up the tree.
  7100. If the region is active in `transient-mark-mode', promote all
  7101. headings in the region."
  7102. (interactive)
  7103. (save-excursion
  7104. (if (org-region-active-p)
  7105. (org-map-region 'org-promote (region-beginning) (region-end))
  7106. (org-promote)))
  7107. (org-fix-position-after-promote))
  7108. (defun org-do-demote ()
  7109. "Demote the current heading lower down the tree.
  7110. If the region is active in `transient-mark-mode', demote all
  7111. headings in the region."
  7112. (interactive)
  7113. (save-excursion
  7114. (if (org-region-active-p)
  7115. (org-map-region 'org-demote (region-beginning) (region-end))
  7116. (org-demote)))
  7117. (org-fix-position-after-promote))
  7118. (defun org-fix-position-after-promote ()
  7119. "Fix cursor position and indentation after demoting/promoting."
  7120. (let ((pos (point)))
  7121. (when (save-excursion
  7122. (beginning-of-line 1)
  7123. (looking-at org-todo-line-regexp)
  7124. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  7125. (cond ((eobp) (insert " "))
  7126. ((eolp) (insert " "))
  7127. ((equal (char-after) ?\ ) (forward-char 1))))))
  7128. (defun org-current-level ()
  7129. "Return the level of the current entry, or nil if before the first headline.
  7130. The level is the number of stars at the beginning of the
  7131. headline. Use `org-reduced-level' to remove the effect of
  7132. `org-odd-levels'. Unlike to `org-outline-level', this function
  7133. ignores inlinetasks."
  7134. (let ((level (org-with-limited-levels (org-outline-level))))
  7135. (and (> level 0) level)))
  7136. (defun org-get-previous-line-level ()
  7137. "Return the outline depth of the last headline before the current line.
  7138. Returns 0 for the first headline in the buffer, and nil if before the
  7139. first headline."
  7140. (and (org-current-level)
  7141. (or (and (/= (line-beginning-position) (point-min))
  7142. (save-excursion (beginning-of-line 0) (org-current-level)))
  7143. 0)))
  7144. (defun org-reduced-level (l)
  7145. "Compute the effective level of a heading.
  7146. This takes into account the setting of `org-odd-levels-only'."
  7147. (cond
  7148. ((zerop l) 0)
  7149. (org-odd-levels-only (1+ (floor (/ l 2))))
  7150. (t l)))
  7151. (defun org-level-increment ()
  7152. "Return the number of stars that will be added or removed at a
  7153. time to headlines when structure editing, based on the value of
  7154. `org-odd-levels-only'."
  7155. (if org-odd-levels-only 2 1))
  7156. (defun org-get-valid-level (level &optional change)
  7157. "Rectify a level change under the influence of `org-odd-levels-only'
  7158. LEVEL is a current level, CHANGE is by how much the level should be
  7159. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  7160. even level numbers will become the next higher odd number."
  7161. (if org-odd-levels-only
  7162. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  7163. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  7164. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  7165. (max 1 (+ level (or change 0)))))
  7166. (define-obsolete-function-alias 'org-get-legal-level 'org-get-valid-level "23.1")
  7167. (defun org-promote ()
  7168. "Promote the current heading higher up the tree."
  7169. (org-with-wide-buffer
  7170. (org-back-to-heading t)
  7171. (let* ((after-change-functions (remq 'flyspell-after-change-function
  7172. after-change-functions))
  7173. (level (save-match-data (funcall outline-level)))
  7174. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  7175. (diff (abs (- level (length up-head) -1))))
  7176. (cond
  7177. ((and (= level 1) org-allow-promoting-top-level-subtree)
  7178. (replace-match "# " nil t))
  7179. ((= level 1)
  7180. (user-error "Cannot promote to level 0. UNDO to recover if necessary"))
  7181. (t (replace-match up-head nil t)))
  7182. (unless (= level 1)
  7183. (when org-auto-align-tags (org-set-tags nil 'ignore-column))
  7184. (when org-adapt-indentation (org-fixup-indentation (- diff))))
  7185. (run-hooks 'org-after-promote-entry-hook))))
  7186. (defun org-demote ()
  7187. "Demote the current heading lower down the tree."
  7188. (org-with-wide-buffer
  7189. (org-back-to-heading t)
  7190. (let* ((after-change-functions (remq 'flyspell-after-change-function
  7191. after-change-functions))
  7192. (level (save-match-data (funcall outline-level)))
  7193. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  7194. (diff (abs (- level (length down-head) -1))))
  7195. (replace-match down-head nil t)
  7196. (when org-auto-align-tags (org-set-tags nil 'ignore-column))
  7197. (when org-adapt-indentation (org-fixup-indentation diff))
  7198. (run-hooks 'org-after-demote-entry-hook))))
  7199. (defun org-cycle-level ()
  7200. "Cycle the level of an empty headline through possible states.
  7201. This goes first to child, then to parent, level, then up the hierarchy.
  7202. After top level, it switches back to sibling level."
  7203. (interactive)
  7204. (let ((org-adapt-indentation nil))
  7205. (when (org-point-at-end-of-empty-headline)
  7206. (setq this-command 'org-cycle-level) ; Only needed for caching
  7207. (let ((cur-level (org-current-level))
  7208. (prev-level (org-get-previous-line-level)))
  7209. (cond
  7210. ;; If first headline in file, promote to top-level.
  7211. ((= prev-level 0)
  7212. (cl-loop repeat (/ (- cur-level 1) (org-level-increment))
  7213. do (org-do-promote)))
  7214. ;; If same level as prev, demote one.
  7215. ((= prev-level cur-level)
  7216. (org-do-demote))
  7217. ;; If parent is top-level, promote to top level if not already.
  7218. ((= prev-level 1)
  7219. (cl-loop repeat (/ (- cur-level 1) (org-level-increment))
  7220. do (org-do-promote)))
  7221. ;; If top-level, return to prev-level.
  7222. ((= cur-level 1)
  7223. (cl-loop repeat (/ (- prev-level 1) (org-level-increment))
  7224. do (org-do-demote)))
  7225. ;; If less than prev-level, promote one.
  7226. ((< cur-level prev-level)
  7227. (org-do-promote))
  7228. ;; If deeper than prev-level, promote until higher than
  7229. ;; prev-level.
  7230. ((> cur-level prev-level)
  7231. (cl-loop repeat (+ 1 (/ (- cur-level prev-level) (org-level-increment)))
  7232. do (org-do-promote))))
  7233. t))))
  7234. (defun org-map-tree (fun)
  7235. "Call FUN for every heading underneath the current one."
  7236. (org-back-to-heading)
  7237. (let ((level (funcall outline-level)))
  7238. (save-excursion
  7239. (funcall fun)
  7240. (while (and (progn
  7241. (outline-next-heading)
  7242. (> (funcall outline-level) level))
  7243. (not (eobp)))
  7244. (funcall fun)))))
  7245. (defun org-map-region (fun beg end)
  7246. "Call FUN for every heading between BEG and END."
  7247. (let ((org-ignore-region t))
  7248. (save-excursion
  7249. (setq end (copy-marker end))
  7250. (goto-char beg)
  7251. (when (and (re-search-forward org-outline-regexp-bol nil t)
  7252. (< (point) end))
  7253. (funcall fun))
  7254. (while (and (progn
  7255. (outline-next-heading)
  7256. (< (point) end))
  7257. (not (eobp)))
  7258. (funcall fun)))))
  7259. (defun org-fixup-indentation (diff)
  7260. "Change the indentation in the current entry by DIFF.
  7261. DIFF is an integer. Indentation is done according to the
  7262. following rules:
  7263. - Planning information and property drawers are always indented
  7264. according to the new level of the headline;
  7265. - Footnote definitions and their contents are ignored;
  7266. - Inlinetasks' boundaries are not shifted;
  7267. - Empty lines are ignored;
  7268. - Other lines' indentation are shifted by DIFF columns, unless
  7269. it would introduce a structural change in the document, in
  7270. which case no shifting is done at all.
  7271. Assume point is at a heading or an inlinetask beginning."
  7272. (org-with-wide-buffer
  7273. (narrow-to-region (line-beginning-position)
  7274. (save-excursion
  7275. (if (org-with-limited-levels (org-at-heading-p))
  7276. (org-with-limited-levels (outline-next-heading))
  7277. (org-inlinetask-goto-end))
  7278. (point)))
  7279. (forward-line)
  7280. ;; Indent properly planning info and property drawer.
  7281. (when (org-looking-at-p org-planning-line-re)
  7282. (org-indent-line)
  7283. (forward-line))
  7284. (when (looking-at org-property-drawer-re)
  7285. (goto-char (match-end 0))
  7286. (forward-line)
  7287. (save-excursion (org-indent-region (match-beginning 0) (match-end 0))))
  7288. (catch 'no-shift
  7289. (when (zerop diff) (throw 'no-shift nil))
  7290. ;; If DIFF is negative, first check if a shift is possible at all
  7291. ;; (e.g., it doesn't break structure). This can only happen if
  7292. ;; some contents are not properly indented.
  7293. (let ((case-fold-search t))
  7294. (when (< diff 0)
  7295. (let ((diff (- diff))
  7296. (forbidden-re (concat org-outline-regexp
  7297. "\\|"
  7298. (substring org-footnote-definition-re 1))))
  7299. (save-excursion
  7300. (while (not (eobp))
  7301. (cond
  7302. ((org-looking-at-p "[ \t]*$") (forward-line))
  7303. ((and (org-looking-at-p org-footnote-definition-re)
  7304. (let ((e (org-element-at-point)))
  7305. (and (eq (org-element-type e) 'footnote-definition)
  7306. (goto-char (org-element-property :end e))))))
  7307. ((org-looking-at-p org-outline-regexp) (forward-line))
  7308. ;; Give up if shifting would move before column 0 or
  7309. ;; if it would introduce a headline or a footnote
  7310. ;; definition.
  7311. (t
  7312. (skip-chars-forward " \t")
  7313. (let ((ind (current-column)))
  7314. (when (or (< ind diff)
  7315. (and (= ind diff) (org-looking-at-p forbidden-re)))
  7316. (throw 'no-shift nil)))
  7317. ;; Ignore contents of example blocks and source
  7318. ;; blocks if their indentation is meant to be
  7319. ;; preserved. Jump to block's closing line.
  7320. (beginning-of-line)
  7321. (or (and (org-looking-at-p "[ \t]*#\\+BEGIN_\\(EXAMPLE\\|SRC\\)")
  7322. (let ((e (org-element-at-point)))
  7323. (and (memq (org-element-type e)
  7324. '(example-block src-block))
  7325. (or org-src-preserve-indentation
  7326. (org-element-property :preserve-indent e))
  7327. (goto-char (org-element-property :end e))
  7328. (progn (skip-chars-backward " \r\t\n")
  7329. (beginning-of-line)
  7330. t))))
  7331. (forward-line))))))))
  7332. ;; Shift lines but footnote definitions, inlinetasks boundaries
  7333. ;; by DIFF. Also skip contents of source or example blocks
  7334. ;; when indentation is meant to be preserved.
  7335. (while (not (eobp))
  7336. (cond
  7337. ((and (org-looking-at-p org-footnote-definition-re)
  7338. (let ((e (org-element-at-point)))
  7339. (and (eq (org-element-type e) 'footnote-definition)
  7340. (goto-char (org-element-property :end e))))))
  7341. ((org-looking-at-p org-outline-regexp) (forward-line))
  7342. ((org-looking-at-p "[ \t]*$") (forward-line))
  7343. (t
  7344. (org-indent-line-to (+ (org-get-indentation) diff))
  7345. (beginning-of-line)
  7346. (or (and (org-looking-at-p "[ \t]*#\\+BEGIN_\\(EXAMPLE\\|SRC\\)")
  7347. (let ((e (org-element-at-point)))
  7348. (and (memq (org-element-type e)
  7349. '(example-block src-block))
  7350. (or org-src-preserve-indentation
  7351. (org-element-property :preserve-indent e))
  7352. (goto-char (org-element-property :end e))
  7353. (progn (skip-chars-backward " \r\t\n")
  7354. (beginning-of-line)
  7355. t))))
  7356. (forward-line)))))))))
  7357. (defun org-convert-to-odd-levels ()
  7358. "Convert an org-mode file with all levels allowed to one with odd levels.
  7359. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  7360. level 5 etc."
  7361. (interactive)
  7362. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  7363. (let ((outline-level 'org-outline-level)
  7364. (org-odd-levels-only nil) n)
  7365. (save-excursion
  7366. (goto-char (point-min))
  7367. (while (re-search-forward "^\\*\\*+ " nil t)
  7368. (setq n (- (length (match-string 0)) 2))
  7369. (while (>= (setq n (1- n)) 0)
  7370. (org-demote))
  7371. (end-of-line 1))))))
  7372. (defun org-convert-to-oddeven-levels ()
  7373. "Convert an org-mode file with only odd levels to one with odd/even levels.
  7374. This promotes level 3 to level 2, level 5 to level 3 etc. If the
  7375. file contains a section with an even level, conversion would
  7376. destroy the structure of the file. An error is signaled in this
  7377. case."
  7378. (interactive)
  7379. (goto-char (point-min))
  7380. ;; First check if there are no even levels
  7381. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  7382. (org-show-set-visibility 'canonical)
  7383. (error "Not all levels are odd in this file. Conversion not possible"))
  7384. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  7385. (let ((outline-regexp org-outline-regexp)
  7386. (outline-level 'org-outline-level)
  7387. (org-odd-levels-only nil) n)
  7388. (save-excursion
  7389. (goto-char (point-min))
  7390. (while (re-search-forward "^\\*\\*+ " nil t)
  7391. (setq n (/ (1- (length (match-string 0))) 2))
  7392. (while (>= (setq n (1- n)) 0)
  7393. (org-promote))
  7394. (end-of-line 1))))))
  7395. (defun org-tr-level (n)
  7396. "Make N odd if required."
  7397. (if org-odd-levels-only (1+ (/ n 2)) n))
  7398. ;;; Vertical tree motion, cutting and pasting of subtrees
  7399. (defun org-move-subtree-up (&optional arg)
  7400. "Move the current subtree up past ARG headlines of the same level."
  7401. (interactive "p")
  7402. (org-move-subtree-down (- (prefix-numeric-value arg))))
  7403. (defun org-move-subtree-down (&optional arg)
  7404. "Move the current subtree down past ARG headlines of the same level."
  7405. (interactive "p")
  7406. (setq arg (prefix-numeric-value arg))
  7407. (let ((movfunc (if (> arg 0) 'org-get-next-sibling
  7408. 'org-get-last-sibling))
  7409. (ins-point (make-marker))
  7410. (cnt (abs arg))
  7411. (col (current-column))
  7412. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  7413. ;; Select the tree
  7414. (org-back-to-heading)
  7415. (setq beg0 (point))
  7416. (save-excursion
  7417. (setq ne-beg (org-back-over-empty-lines))
  7418. (setq beg (point)))
  7419. (save-match-data
  7420. (save-excursion (outline-end-of-heading)
  7421. (setq folded (outline-invisible-p)))
  7422. (progn (org-end-of-subtree nil t)
  7423. (unless (eobp) (backward-char))))
  7424. (outline-next-heading)
  7425. (setq ne-end (org-back-over-empty-lines))
  7426. (setq end (point))
  7427. (goto-char beg0)
  7428. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  7429. ;; include less whitespace
  7430. (save-excursion
  7431. (goto-char beg)
  7432. (forward-line (- ne-beg ne-end))
  7433. (setq beg (point))))
  7434. ;; Find insertion point, with error handling
  7435. (while (> cnt 0)
  7436. (or (and (funcall movfunc) (looking-at org-outline-regexp))
  7437. (progn (goto-char beg0)
  7438. (user-error "Cannot move past superior level or buffer limit")))
  7439. (setq cnt (1- cnt)))
  7440. (when (> arg 0)
  7441. ;; Moving forward - still need to move over subtree
  7442. (org-end-of-subtree t t)
  7443. (save-excursion
  7444. (org-back-over-empty-lines)
  7445. (or (bolp) (newline))))
  7446. (setq ne-ins (org-back-over-empty-lines))
  7447. (move-marker ins-point (point))
  7448. (setq txt (buffer-substring beg end))
  7449. (org-save-markers-in-region beg end)
  7450. (delete-region beg end)
  7451. (org-remove-empty-overlays-at beg)
  7452. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  7453. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  7454. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  7455. (let ((bbb (point)))
  7456. (insert-before-markers txt)
  7457. (org-reinstall-markers-in-region bbb)
  7458. (move-marker ins-point bbb))
  7459. (or (bolp) (insert "\n"))
  7460. (setq ins-end (point))
  7461. (goto-char ins-point)
  7462. (org-skip-whitespace)
  7463. (when (and (< arg 0)
  7464. (org-first-sibling-p)
  7465. (> ne-ins ne-beg))
  7466. ;; Move whitespace back to beginning
  7467. (save-excursion
  7468. (goto-char ins-end)
  7469. (let ((kill-whole-line t))
  7470. (kill-line (- ne-ins ne-beg)) (point)))
  7471. (insert (make-string (- ne-ins ne-beg) ?\n)))
  7472. (move-marker ins-point nil)
  7473. (if folded
  7474. (outline-hide-subtree)
  7475. (org-show-entry)
  7476. (org-show-children)
  7477. (org-cycle-hide-drawers 'children))
  7478. (org-clean-visibility-after-subtree-move)
  7479. ;; move back to the initial column we were at
  7480. (move-to-column col)))
  7481. (defvar org-subtree-clip ""
  7482. "Clipboard for cut and paste of subtrees.
  7483. This is actually only a copy of the kill, because we use the normal kill
  7484. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  7485. (defvar org-subtree-clip-folded nil
  7486. "Was the last copied subtree folded?
  7487. This is used to fold the tree back after pasting.")
  7488. (defun org-cut-subtree (&optional n)
  7489. "Cut the current subtree into the clipboard.
  7490. With prefix arg N, cut this many sequential subtrees.
  7491. This is a short-hand for marking the subtree and then cutting it."
  7492. (interactive "p")
  7493. (org-copy-subtree n 'cut))
  7494. (defun org-copy-subtree (&optional n cut force-store-markers nosubtrees)
  7495. "Copy the current subtree it in the clipboard.
  7496. With prefix arg N, copy this many sequential subtrees.
  7497. This is a short-hand for marking the subtree and then copying it.
  7498. If CUT is non-nil, actually cut the subtree.
  7499. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  7500. of some markers in the region, even if CUT is non-nil. This is
  7501. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  7502. (interactive "p")
  7503. (let (beg end folded (beg0 (point)))
  7504. (if (org-called-interactively-p 'any)
  7505. (org-back-to-heading nil) ; take what looks like a subtree
  7506. (org-back-to-heading t)) ; take what is really there
  7507. (setq beg (point))
  7508. (skip-chars-forward " \t\r\n")
  7509. (save-match-data
  7510. (if nosubtrees
  7511. (outline-next-heading)
  7512. (save-excursion (outline-end-of-heading)
  7513. (setq folded (outline-invisible-p)))
  7514. (ignore-errors (org-forward-heading-same-level (1- n) t))
  7515. (org-end-of-subtree t t)))
  7516. ;; Include the end of an inlinetask
  7517. (when (and (featurep 'org-inlinetask)
  7518. (looking-at-p (concat (org-inlinetask-outline-regexp)
  7519. "END[ \t]*$")))
  7520. (end-of-line))
  7521. (setq end (point))
  7522. (goto-char beg0)
  7523. (when (> end beg)
  7524. (setq org-subtree-clip-folded folded)
  7525. (when (or cut force-store-markers)
  7526. (org-save-markers-in-region beg end))
  7527. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  7528. (setq org-subtree-clip (current-kill 0))
  7529. (message "%s: Subtree(s) with %d characters"
  7530. (if cut "Cut" "Copied")
  7531. (length org-subtree-clip)))))
  7532. (defun org-paste-subtree (&optional level tree for-yank remove)
  7533. "Paste the clipboard as a subtree, with modification of headline level.
  7534. The entire subtree is promoted or demoted in order to match a new headline
  7535. level.
  7536. If the cursor is at the beginning of a headline, the same level as
  7537. that headline is used to paste the tree.
  7538. If not, the new level is derived from the *visible* headings
  7539. before and after the insertion point, and taken to be the inferior headline
  7540. level of the two. So if the previous visible heading is level 3 and the
  7541. next is level 4 (or vice versa), level 4 will be used for insertion.
  7542. This makes sure that the subtree remains an independent subtree and does
  7543. not swallow low level entries.
  7544. You can also force a different level, either by using a numeric prefix
  7545. argument, or by inserting the heading marker by hand. For example, if the
  7546. cursor is after \"*****\", then the tree will be shifted to level 5.
  7547. If optional TREE is given, use this text instead of the kill ring.
  7548. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  7549. move back over whitespace before inserting, and move point to the end of
  7550. the inserted text when done.
  7551. When REMOVE is non-nil, remove the subtree from the clipboard."
  7552. (interactive "P")
  7553. (setq tree (or tree (and kill-ring (current-kill 0))))
  7554. (unless (org-kill-is-subtree-p tree)
  7555. (user-error "%s"
  7556. (substitute-command-keys
  7557. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  7558. (org-with-limited-levels
  7559. (let* ((visp (not (outline-invisible-p)))
  7560. (txt tree)
  7561. (^re_ "\\(\\*+\\)[ \t]*")
  7562. (old-level (if (string-match org-outline-regexp-bol txt)
  7563. (- (match-end 0) (match-beginning 0) 1)
  7564. -1))
  7565. (force-level (cond (level (prefix-numeric-value level))
  7566. ((and (looking-at "[ \t]*$")
  7567. (string-match
  7568. "^\\*+$" (buffer-substring
  7569. (point-at-bol) (point))))
  7570. (- (match-end 0) (match-beginning 0)))
  7571. ((and (bolp)
  7572. (looking-at org-outline-regexp))
  7573. (- (match-end 0) (point) 1))))
  7574. (previous-level (save-excursion
  7575. (condition-case nil
  7576. (progn
  7577. (outline-previous-visible-heading 1)
  7578. (if (looking-at ^re_)
  7579. (- (match-end 0) (match-beginning 0) 1)
  7580. 1))
  7581. (error 1))))
  7582. (next-level (save-excursion
  7583. (condition-case nil
  7584. (progn
  7585. (or (looking-at org-outline-regexp)
  7586. (outline-next-visible-heading 1))
  7587. (if (looking-at ^re_)
  7588. (- (match-end 0) (match-beginning 0) 1)
  7589. 1))
  7590. (error 1))))
  7591. (new-level (or force-level (max previous-level next-level)))
  7592. (shift (if (or (= old-level -1)
  7593. (= new-level -1)
  7594. (= old-level new-level))
  7595. 0
  7596. (- new-level old-level)))
  7597. (delta (if (> shift 0) -1 1))
  7598. (func (if (> shift 0) 'org-demote 'org-promote))
  7599. (org-odd-levels-only nil)
  7600. beg end newend)
  7601. ;; Remove the forced level indicator
  7602. (when force-level
  7603. (delete-region (point-at-bol) (point)))
  7604. ;; Paste
  7605. (beginning-of-line (if (bolp) 1 2))
  7606. (setq beg (point))
  7607. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  7608. (insert-before-markers txt)
  7609. (unless (string-match "\n\\'" txt) (insert "\n"))
  7610. (setq newend (point))
  7611. (org-reinstall-markers-in-region beg)
  7612. (setq end (point))
  7613. (goto-char beg)
  7614. (skip-chars-forward " \t\n\r")
  7615. (setq beg (point))
  7616. (when (and (outline-invisible-p) visp)
  7617. (save-excursion (outline-show-heading)))
  7618. ;; Shift if necessary
  7619. (unless (= shift 0)
  7620. (save-restriction
  7621. (narrow-to-region beg end)
  7622. (while (not (= shift 0))
  7623. (org-map-region func (point-min) (point-max))
  7624. (setq shift (+ delta shift)))
  7625. (goto-char (point-min))
  7626. (setq newend (point-max))))
  7627. (when (or (org-called-interactively-p 'interactive) for-yank)
  7628. (message "Clipboard pasted as level %d subtree" new-level))
  7629. (when (and (not for-yank) ; in this case, org-yank will decide about folding
  7630. kill-ring
  7631. (eq org-subtree-clip (current-kill 0))
  7632. org-subtree-clip-folded)
  7633. ;; The tree was folded before it was killed/copied
  7634. (outline-hide-subtree))
  7635. (and for-yank (goto-char newend))
  7636. (and remove (setq kill-ring (cdr kill-ring))))))
  7637. (defun org-kill-is-subtree-p (&optional txt)
  7638. "Check if the current kill is an outline subtree, or a set of trees.
  7639. Returns nil if kill does not start with a headline, or if the first
  7640. headline level is not the largest headline level in the tree.
  7641. So this will actually accept several entries of equal levels as well,
  7642. which is OK for `org-paste-subtree'.
  7643. If optional TXT is given, check this string instead of the current kill."
  7644. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  7645. (re (org-get-limited-outline-regexp))
  7646. (^re (concat "^" re))
  7647. (start-level (and kill
  7648. (string-match
  7649. (concat "\\`\\([ \t\n\r]*?\n\\)?\\(" re "\\)")
  7650. kill)
  7651. (- (match-end 2) (match-beginning 2) 1)))
  7652. (start (1+ (or (match-beginning 2) -1))))
  7653. (if (not start-level)
  7654. (progn
  7655. nil) ;; does not even start with a heading
  7656. (catch 'exit
  7657. (while (setq start (string-match ^re kill (1+ start)))
  7658. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  7659. (throw 'exit nil)))
  7660. t))))
  7661. (defvar org-markers-to-move nil
  7662. "Markers that should be moved with a cut-and-paste operation.
  7663. Those markers are stored together with their positions relative to
  7664. the start of the region.")
  7665. (defun org-save-markers-in-region (beg end)
  7666. "Check markers in region.
  7667. If these markers are between BEG and END, record their position relative
  7668. to BEG, so that after moving the block of text, we can put the markers back
  7669. into place.
  7670. This function gets called just before an entry or tree gets cut from the
  7671. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  7672. called immediately, to move the markers with the entries."
  7673. (setq org-markers-to-move nil)
  7674. (when (featurep 'org-clock)
  7675. (org-clock-save-markers-for-cut-and-paste beg end))
  7676. (when (featurep 'org-agenda)
  7677. (org-agenda-save-markers-for-cut-and-paste beg end)))
  7678. (defun org-check-and-save-marker (marker beg end)
  7679. "Check if MARKER is between BEG and END.
  7680. If yes, remember the marker and the distance to BEG."
  7681. (when (and (marker-buffer marker)
  7682. (equal (marker-buffer marker) (current-buffer))
  7683. (>= marker beg) (< marker end))
  7684. (push (cons marker (- marker beg)) org-markers-to-move)))
  7685. (defun org-reinstall-markers-in-region (beg)
  7686. "Move all remembered markers to their position relative to BEG."
  7687. (dolist (x org-markers-to-move)
  7688. (move-marker (car x) (+ beg (cdr x))))
  7689. (setq org-markers-to-move nil))
  7690. (defun org-narrow-to-subtree ()
  7691. "Narrow buffer to the current subtree."
  7692. (interactive)
  7693. (save-excursion
  7694. (save-match-data
  7695. (org-with-limited-levels
  7696. (narrow-to-region
  7697. (progn (org-back-to-heading t) (point))
  7698. (progn (org-end-of-subtree t t)
  7699. (when (and (org-at-heading-p) (not (eobp))) (backward-char 1))
  7700. (point)))))))
  7701. (defun org-narrow-to-block ()
  7702. "Narrow buffer to the current block."
  7703. (interactive)
  7704. (let* ((case-fold-search t)
  7705. (blockp (org-between-regexps-p "^[ \t]*#\\+begin_.*"
  7706. "^[ \t]*#\\+end_.*")))
  7707. (if blockp
  7708. (narrow-to-region (car blockp) (cdr blockp))
  7709. (user-error "Not in a block"))))
  7710. (defun org-clone-subtree-with-time-shift (n &optional shift)
  7711. "Clone the task (subtree) at point N times.
  7712. The clones will be inserted as siblings.
  7713. In interactive use, the user will be prompted for the number of
  7714. clones to be produced. If the entry has a timestamp, the user
  7715. will also be prompted for a time shift, which may be a repeater
  7716. as used in time stamps, for example `+3d'. To disable this,
  7717. you can call the function with a universal prefix argument.
  7718. When a valid repeater is given and the entry contains any time
  7719. stamps, the clones will become a sequence in time, with time
  7720. stamps in the subtree shifted for each clone produced. If SHIFT
  7721. is nil or the empty string, time stamps will be left alone. The
  7722. ID property of the original subtree is removed.
  7723. If the original subtree did contain time stamps with a repeater,
  7724. the following will happen:
  7725. - the repeater will be removed in each clone
  7726. - an additional clone will be produced, with the current, unshifted
  7727. date(s) in the entry.
  7728. - the original entry will be placed *after* all the clones, with
  7729. repeater intact.
  7730. - the start days in the repeater in the original entry will be shifted
  7731. to past the last clone.
  7732. In this way you can spell out a number of instances of a repeating task,
  7733. and still retain the repeater to cover future instances of the task.
  7734. As described above, N+1 clones are produced when the original
  7735. subtree has a repeater. Setting N to 0, then, can be used to
  7736. remove the repeater from a subtree and create a shifted clone
  7737. with the original repeater."
  7738. (interactive "nNumber of clones to produce: ")
  7739. (let ((shift
  7740. (or shift
  7741. (if (and (not (equal current-prefix-arg '(4)))
  7742. (save-excursion
  7743. (re-search-forward org-ts-regexp-both
  7744. (save-excursion
  7745. (org-end-of-subtree t)
  7746. (point)) t)))
  7747. (read-from-minibuffer
  7748. "Date shift per clone (e.g. +1w, empty to copy unchanged): ")
  7749. ""))) ;; No time shift
  7750. (n-no-remove -1)
  7751. (drawer-re org-drawer-regexp)
  7752. (org-clock-re (format "^[ \t]*%s.*$" org-clock-string))
  7753. beg end template task idprop
  7754. shift-n shift-what doshift nmin nmax)
  7755. (unless (wholenump n)
  7756. (user-error "Invalid number of replications %s" n))
  7757. (when (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  7758. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([hdwmy]\\)[ \t]*\\'"
  7759. shift)))
  7760. (user-error "Invalid shift specification %s" shift))
  7761. (when doshift
  7762. (setq shift-n (string-to-number (match-string 1 shift))
  7763. shift-what (cdr (assoc (match-string 2 shift)
  7764. '(("d" . day) ("w" . week)
  7765. ("m" . month) ("y" . year))))))
  7766. (when (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  7767. (setq nmin 1 nmax n)
  7768. (org-back-to-heading t)
  7769. (setq beg (point))
  7770. (setq idprop (org-entry-get nil "ID"))
  7771. (org-end-of-subtree t t)
  7772. (or (bolp) (insert "\n"))
  7773. (setq end (point))
  7774. (setq template (buffer-substring beg end))
  7775. (when (and doshift
  7776. (string-match "<[^<>\n]+ [.+]?\\+[0-9]+[hdwmy][^<>\n]*>" template))
  7777. (delete-region beg end)
  7778. (setq end beg)
  7779. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  7780. (goto-char end)
  7781. (cl-loop for n from nmin to nmax do
  7782. ;; prepare clone
  7783. (with-temp-buffer
  7784. (insert template)
  7785. (org-mode)
  7786. (goto-char (point-min))
  7787. (org-show-subtree)
  7788. (and idprop (if org-clone-delete-id
  7789. (org-entry-delete nil "ID")
  7790. (org-id-get-create t)))
  7791. (unless (= n 0)
  7792. (while (re-search-forward org-clock-re nil t)
  7793. (kill-whole-line))
  7794. (goto-char (point-min))
  7795. (while (re-search-forward drawer-re nil t)
  7796. (org-remove-empty-drawer-at (point))))
  7797. (goto-char (point-min))
  7798. (when doshift
  7799. (while (re-search-forward org-ts-regexp-both nil t)
  7800. (org-timestamp-change (* n shift-n) shift-what))
  7801. (unless (= n n-no-remove)
  7802. (goto-char (point-min))
  7803. (while (re-search-forward org-ts-regexp nil t)
  7804. (save-excursion
  7805. (goto-char (match-beginning 0))
  7806. (when (looking-at "<[^<>\n]+\\( +[.+]?\\+[0-9]+[hdwmy]\\)")
  7807. (delete-region (match-beginning 1) (match-end 1)))))))
  7808. (setq task (buffer-string)))
  7809. (insert task))
  7810. (goto-char beg)))
  7811. ;;; Outline Sorting
  7812. (defun org-sort (with-case)
  7813. "Call `org-sort-entries', `org-table-sort-lines' or `org-sort-list'.
  7814. Optional argument WITH-CASE means sort case-sensitively."
  7815. (interactive "P")
  7816. (cond
  7817. ((org-at-table-p) (org-call-with-arg 'org-table-sort-lines with-case))
  7818. ((org-at-item-p) (org-call-with-arg 'org-sort-list with-case))
  7819. (t
  7820. (org-call-with-arg 'org-sort-entries with-case))))
  7821. (defun org-sort-remove-invisible (s)
  7822. "Remove invisible links from string S."
  7823. (remove-text-properties 0 (length s) org-rm-props s)
  7824. (while (string-match org-bracket-link-regexp s)
  7825. (setq s (replace-match (if (match-end 2)
  7826. (match-string 3 s)
  7827. (match-string 1 s))
  7828. t t s)))
  7829. (let ((st (format " %s " s)))
  7830. (while (string-match org-emph-re st)
  7831. (setq st (replace-match (format " %s " (match-string 4 st)) t t st)))
  7832. (setq s (substring st 1 -1)))
  7833. s)
  7834. (defvar org-priority-regexp) ; defined later in the file
  7835. (defvar org-after-sorting-entries-or-items-hook nil
  7836. "Hook that is run after a bunch of entries or items have been sorted.
  7837. When children are sorted, the cursor is in the parent line when this
  7838. hook gets called. When a region or a plain list is sorted, the cursor
  7839. will be in the first entry of the sorted region/list.")
  7840. (defun org-sort-entries
  7841. (&optional with-case sorting-type getkey-func compare-func property)
  7842. "Sort entries on a certain level of an outline tree.
  7843. If there is an active region, the entries in the region are sorted.
  7844. Else, if the cursor is before the first entry, sort the top-level items.
  7845. Else, the children of the entry at point are sorted.
  7846. Sorting can be alphabetically, numerically, by date/time as given by
  7847. a time stamp, by a property, by priority order, or by a custom function.
  7848. The command prompts for the sorting type unless it has been given to the
  7849. function through the SORTING-TYPE argument, which needs to be a character,
  7850. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?o ?O ?r ?R ?f ?F ?k ?K). Here is
  7851. the precise meaning of each character:
  7852. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  7853. c By creation time, which is assumed to be the first inactive time stamp
  7854. at the beginning of a line.
  7855. d By deadline date/time.
  7856. k By clocking time.
  7857. n Numerically, by converting the beginning of the entry/item to a number.
  7858. o By order of TODO keywords.
  7859. p By priority according to the cookie.
  7860. r By the value of a property.
  7861. s By scheduled date/time.
  7862. t By date/time, either the first active time stamp in the entry, or, if
  7863. none exist, by the first inactive one.
  7864. Capital letters will reverse the sort order.
  7865. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  7866. called with point at the beginning of the record. It must return either
  7867. a string or a number that should serve as the sorting key for that record.
  7868. Comparing entries ignores case by default. However, with an optional argument
  7869. WITH-CASE, the sorting considers case as well.
  7870. Sorting is done against the visible part of the headlines, it ignores hidden
  7871. links.
  7872. When sorting is done, call `org-after-sorting-entries-or-items-hook'."
  7873. (interactive "P")
  7874. (let ((case-func (if with-case 'identity 'downcase))
  7875. (cmstr
  7876. ;; The clock marker is lost when using `sort-subr', let's
  7877. ;; store the clocking string.
  7878. (when (equal (marker-buffer org-clock-marker) (current-buffer))
  7879. (save-excursion
  7880. (goto-char org-clock-marker)
  7881. (buffer-substring-no-properties (line-beginning-position)
  7882. (point)))))
  7883. start beg end stars re re2
  7884. txt what tmp)
  7885. ;; Find beginning and end of region to sort
  7886. (cond
  7887. ((org-region-active-p)
  7888. ;; we will sort the region
  7889. (setq end (region-end)
  7890. what "region")
  7891. (goto-char (region-beginning))
  7892. (unless (org-at-heading-p) (outline-next-heading))
  7893. (setq start (point)))
  7894. ((or (org-at-heading-p)
  7895. (ignore-errors (progn (org-back-to-heading) t)))
  7896. ;; we will sort the children of the current headline
  7897. (org-back-to-heading)
  7898. (setq start (point)
  7899. end (progn (org-end-of-subtree t t)
  7900. (or (bolp) (insert "\n"))
  7901. (when (>= (org-back-over-empty-lines) 1)
  7902. (forward-line 1))
  7903. (point))
  7904. what "children")
  7905. (goto-char start)
  7906. (outline-show-subtree)
  7907. (outline-next-heading))
  7908. (t
  7909. ;; we will sort the top-level entries in this file
  7910. (goto-char (point-min))
  7911. (or (org-at-heading-p) (outline-next-heading))
  7912. (setq start (point))
  7913. (goto-char (point-max))
  7914. (beginning-of-line 1)
  7915. (when (looking-at ".*?\\S-")
  7916. ;; File ends in a non-white line
  7917. (end-of-line 1)
  7918. (insert "\n"))
  7919. (setq end (point-max))
  7920. (setq what "top-level")
  7921. (goto-char start)
  7922. (outline-show-all)))
  7923. (setq beg (point))
  7924. (when (>= beg end) (goto-char start) (user-error "Nothing to sort"))
  7925. (looking-at "\\(\\*+\\)")
  7926. (setq stars (match-string 1)
  7927. re (concat "^" (regexp-quote stars) " +")
  7928. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[ \t\n]")
  7929. txt (buffer-substring beg end))
  7930. (unless (equal (substring txt -1) "\n") (setq txt (concat txt "\n")))
  7931. (when (and (not (equal stars "*")) (string-match re2 txt))
  7932. (user-error "Region to sort contains a level above the first entry"))
  7933. (unless sorting-type
  7934. (message
  7935. "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
  7936. [t]ime [s]cheduled [d]eadline [c]reated cloc[k]ing
  7937. A/N/P/R/O/F/T/S/D/C/K means reversed:"
  7938. what)
  7939. (setq sorting-type (read-char-exclusive))
  7940. (unless getkey-func
  7941. (and (= (downcase sorting-type) ?f)
  7942. (setq getkey-func
  7943. (completing-read "Sort using function: "
  7944. obarray 'fboundp t nil nil))
  7945. (setq getkey-func (intern getkey-func))))
  7946. (and (= (downcase sorting-type) ?r)
  7947. (not property)
  7948. (setq property
  7949. (completing-read "Property: "
  7950. (mapcar #'list (org-buffer-property-keys t))
  7951. nil t))))
  7952. (when (member sorting-type '(?k ?K)) (org-clock-sum))
  7953. (message "Sorting entries...")
  7954. (save-restriction
  7955. (narrow-to-region start end)
  7956. (let ((dcst (downcase sorting-type))
  7957. (case-fold-search nil)
  7958. (now (current-time)))
  7959. (sort-subr
  7960. (/= dcst sorting-type)
  7961. ;; This function moves to the beginning character of the "record" to
  7962. ;; be sorted.
  7963. (lambda nil
  7964. (if (re-search-forward re nil t)
  7965. (goto-char (match-beginning 0))
  7966. (goto-char (point-max))))
  7967. ;; This function moves to the last character of the "record" being
  7968. ;; sorted.
  7969. (lambda nil
  7970. (save-match-data
  7971. (condition-case nil
  7972. (outline-forward-same-level 1)
  7973. (error
  7974. (goto-char (point-max))))))
  7975. ;; This function returns the value that gets sorted against.
  7976. (lambda nil
  7977. (cond
  7978. ((= dcst ?n)
  7979. (if (looking-at org-complex-heading-regexp)
  7980. (string-to-number (org-sort-remove-invisible (match-string 4)))
  7981. nil))
  7982. ((= dcst ?a)
  7983. (if (looking-at org-complex-heading-regexp)
  7984. (funcall case-func (org-sort-remove-invisible (match-string 4)))
  7985. nil))
  7986. ((= dcst ?k)
  7987. (or (get-text-property (point) :org-clock-minutes) 0))
  7988. ((= dcst ?t)
  7989. (let ((end (save-excursion (outline-next-heading) (point))))
  7990. (if (or (re-search-forward org-ts-regexp end t)
  7991. (re-search-forward org-ts-regexp-both end t))
  7992. (org-time-string-to-seconds (match-string 0))
  7993. (org-float-time now))))
  7994. ((= dcst ?c)
  7995. (let ((end (save-excursion (outline-next-heading) (point))))
  7996. (if (re-search-forward
  7997. (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
  7998. end t)
  7999. (org-time-string-to-seconds (match-string 0))
  8000. (org-float-time now))))
  8001. ((= dcst ?s)
  8002. (let ((end (save-excursion (outline-next-heading) (point))))
  8003. (if (re-search-forward org-scheduled-time-regexp end t)
  8004. (org-time-string-to-seconds (match-string 1))
  8005. (org-float-time now))))
  8006. ((= dcst ?d)
  8007. (let ((end (save-excursion (outline-next-heading) (point))))
  8008. (if (re-search-forward org-deadline-time-regexp end t)
  8009. (org-time-string-to-seconds (match-string 1))
  8010. (org-float-time now))))
  8011. ((= dcst ?p)
  8012. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  8013. (string-to-char (match-string 2))
  8014. org-default-priority))
  8015. ((= dcst ?r)
  8016. (or (org-entry-get nil property) ""))
  8017. ((= dcst ?o)
  8018. (when (looking-at org-complex-heading-regexp)
  8019. (let* ((m (match-string 2))
  8020. (s (if (member m org-done-keywords) '- '+)))
  8021. (- 99 (funcall s (length (member m org-todo-keywords-1)))))))
  8022. ((= dcst ?f)
  8023. (if getkey-func
  8024. (progn
  8025. (setq tmp (funcall getkey-func))
  8026. (when (stringp tmp) (setq tmp (funcall case-func tmp)))
  8027. tmp)
  8028. (error "Invalid key function `%s'" getkey-func)))
  8029. (t (error "Invalid sorting type `%c'" sorting-type))))
  8030. nil
  8031. (cond
  8032. ((= dcst ?a) 'string<)
  8033. ((= dcst ?f) compare-func)
  8034. ((member dcst '(?p ?t ?s ?d ?c ?k)) '<)))))
  8035. (run-hooks 'org-after-sorting-entries-or-items-hook)
  8036. ;; Reset the clock marker if needed
  8037. (when cmstr
  8038. (save-excursion
  8039. (goto-char start)
  8040. (search-forward cmstr nil t)
  8041. (move-marker org-clock-marker (point))))
  8042. (message "Sorting entries...done")))
  8043. ;;; The orgstruct minor mode
  8044. ;; Define a minor mode which can be used in other modes in order to
  8045. ;; integrate the org-mode structure editing commands.
  8046. ;; This is really a hack, because the org-mode structure commands use
  8047. ;; keys which normally belong to the major mode. Here is how it
  8048. ;; works: The minor mode defines all the keys necessary to operate the
  8049. ;; structure commands, but wraps the commands into a function which
  8050. ;; tests if the cursor is currently at a headline or a plain list
  8051. ;; item. If that is the case, the structure command is used,
  8052. ;; temporarily setting many Org-mode variables like regular
  8053. ;; expressions for filling etc. However, when any of those keys is
  8054. ;; used at a different location, function uses `key-binding' to look
  8055. ;; up if the key has an associated command in another currently active
  8056. ;; keymap (minor modes, major mode, global), and executes that
  8057. ;; command. There might be problems if any of the keys is otherwise
  8058. ;; used as a prefix key.
  8059. (defcustom orgstruct-heading-prefix-regexp ""
  8060. "Regexp that matches the custom prefix of Org headlines in
  8061. orgstruct(++)-mode."
  8062. :group 'org
  8063. :version "24.4"
  8064. :package-version '(Org . "8.3")
  8065. :type 'regexp)
  8066. ;;;###autoload(put 'orgstruct-heading-prefix-regexp 'safe-local-variable 'stringp)
  8067. (defcustom orgstruct-setup-hook nil
  8068. "Hook run after orgstruct-mode-map is filled."
  8069. :group 'org
  8070. :version "24.4"
  8071. :package-version '(Org . "8.0")
  8072. :type 'hook)
  8073. (defvar orgstruct-initialized nil)
  8074. (defvar org-local-vars nil
  8075. "List of local variables, for use by `orgstruct-mode'.")
  8076. ;;;###autoload
  8077. (define-minor-mode orgstruct-mode
  8078. "Toggle the minor mode `orgstruct-mode'.
  8079. This mode is for using Org-mode structure commands in other
  8080. modes. The following keys behave as if Org-mode were active, if
  8081. the cursor is on a headline, or on a plain list item (both as
  8082. defined by Org-mode)."
  8083. nil " OrgStruct" (make-sparse-keymap)
  8084. (funcall (if orgstruct-mode
  8085. 'add-to-invisibility-spec
  8086. 'remove-from-invisibility-spec)
  8087. '(outline . t))
  8088. (when orgstruct-mode
  8089. (org-load-modules-maybe)
  8090. (unless orgstruct-initialized
  8091. (orgstruct-setup)
  8092. (setq orgstruct-initialized t))))
  8093. ;;;###autoload
  8094. (defun turn-on-orgstruct ()
  8095. "Unconditionally turn on `orgstruct-mode'."
  8096. (orgstruct-mode 1))
  8097. (defvar-local orgstruct-is-++ nil
  8098. "Is `orgstruct-mode' in ++ version in the current-buffer?")
  8099. (defvar-local org-fb-vars nil)
  8100. (defun orgstruct++-mode (&optional arg)
  8101. "Toggle `orgstruct-mode', the enhanced version of it.
  8102. In addition to setting orgstruct-mode, this also exports all
  8103. indentation and autofilling variables from org-mode into the
  8104. buffer. It will also recognize item context in multiline items."
  8105. (interactive "P")
  8106. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  8107. (if (< arg 1)
  8108. (progn (orgstruct-mode -1)
  8109. (dolist (v org-fb-vars)
  8110. (set (make-local-variable (car v))
  8111. (if (eq (car-safe (cadr v)) 'quote)
  8112. (cl-cadadr v)
  8113. (nth 1 v)))))
  8114. (orgstruct-mode 1)
  8115. (setq org-fb-vars nil)
  8116. (unless org-local-vars
  8117. (setq org-local-vars (org-get-local-variables)))
  8118. (let (var val)
  8119. (dolist (x org-local-vars)
  8120. (when (string-match
  8121. "^\\(paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\
  8122. \\|fill-prefix\\|indent-\\)"
  8123. (symbol-name (car x)))
  8124. (setq var (car x) val (nth 1 x))
  8125. (push (list var `(quote ,(eval var))) org-fb-vars)
  8126. (set (make-local-variable var)
  8127. (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  8128. (setq-local orgstruct-is-++ t))))
  8129. ;;;###autoload
  8130. (defun turn-on-orgstruct++ ()
  8131. "Unconditionally turn on `orgstruct++-mode'."
  8132. (orgstruct++-mode 1))
  8133. (defun orgstruct-error ()
  8134. "Error when there is no default binding for a structure key."
  8135. (interactive)
  8136. (funcall (if (fboundp 'user-error)
  8137. 'user-error
  8138. 'error)
  8139. "This key has no function outside structure elements"))
  8140. (defun orgstruct-setup ()
  8141. "Setup orgstruct keymap."
  8142. (dolist (cell '((org-demote . t)
  8143. (org-metaleft . t)
  8144. (org-metaright . t)
  8145. (org-promote . t)
  8146. (org-shiftmetaleft . t)
  8147. (org-shiftmetaright . t)
  8148. org-backward-element
  8149. org-backward-heading-same-level
  8150. org-ctrl-c-ret
  8151. org-ctrl-c-minus
  8152. org-ctrl-c-star
  8153. org-cycle
  8154. org-force-cycle-archived
  8155. org-forward-heading-same-level
  8156. org-insert-heading
  8157. org-insert-heading-respect-content
  8158. org-kill-note-or-show-branches
  8159. org-mark-subtree
  8160. org-meta-return
  8161. org-metadown
  8162. org-metaup
  8163. org-narrow-to-subtree
  8164. org-promote-subtree
  8165. org-reveal
  8166. org-shiftdown
  8167. org-shiftleft
  8168. org-shiftmetadown
  8169. org-shiftmetaup
  8170. org-shiftright
  8171. org-shifttab
  8172. org-shifttab
  8173. org-shiftup
  8174. org-show-children
  8175. org-show-subtree
  8176. org-sort
  8177. org-up-element
  8178. outline-demote
  8179. outline-next-visible-heading
  8180. outline-previous-visible-heading
  8181. outline-promote
  8182. outline-up-heading))
  8183. (let ((f (or (car-safe cell) cell))
  8184. (disable-when-heading-prefix (cdr-safe cell)))
  8185. (when (fboundp f)
  8186. (let ((new-bindings))
  8187. (dolist (binding (nconc (where-is-internal f org-mode-map)
  8188. (where-is-internal f outline-mode-map)))
  8189. (push binding new-bindings)
  8190. ;; TODO use local-function-key-map
  8191. (dolist (rep '(("<tab>" . "TAB")
  8192. ("<return>" . "RET")
  8193. ("<escape>" . "ESC")
  8194. ("<delete>" . "DEL")))
  8195. (setq binding (read-kbd-macro
  8196. (let ((case-fold-search))
  8197. (replace-regexp-in-string
  8198. (regexp-quote (cdr rep))
  8199. (car rep)
  8200. (key-description binding)))))
  8201. (cl-pushnew binding new-bindings :test 'equal)))
  8202. (dolist (binding new-bindings)
  8203. (let ((key (lookup-key orgstruct-mode-map binding)))
  8204. (when (or (not key) (numberp key))
  8205. (ignore-errors
  8206. (org-defkey orgstruct-mode-map
  8207. binding
  8208. (orgstruct-make-binding
  8209. f binding disable-when-heading-prefix))))))))))
  8210. (run-hooks 'orgstruct-setup-hook))
  8211. (defun orgstruct-make-binding (fun key disable-when-heading-prefix)
  8212. "Create a function for binding in the structure minor mode.
  8213. FUN is the command to call inside a table. KEY is the key that
  8214. should be checked in for a command to execute outside of tables.
  8215. Non-nil `disable-when-heading-prefix' means to disable the command
  8216. if `orgstruct-heading-prefix-regexp' is not empty."
  8217. (let ((name (concat "orgstruct-hijacker-" (symbol-name fun))))
  8218. (let ((nname name)
  8219. (i 0))
  8220. (while (fboundp (intern nname))
  8221. (setq nname (format "%s-%d" name (setq i (1+ i)))))
  8222. (setq name (intern nname)))
  8223. (eval
  8224. (let ((bindings '((org-heading-regexp
  8225. (concat "^"
  8226. orgstruct-heading-prefix-regexp
  8227. "\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ ]*$"))
  8228. (org-outline-regexp
  8229. (concat orgstruct-heading-prefix-regexp "\\*+ "))
  8230. (org-outline-regexp-bol
  8231. (concat "^" org-outline-regexp))
  8232. (outline-regexp org-outline-regexp)
  8233. (outline-heading-end-regexp "\n")
  8234. (outline-level 'org-outline-level)
  8235. (outline-heading-alist))))
  8236. `(defun ,name (arg)
  8237. ,(concat "In Structure, run `" (symbol-name fun) "'.\n"
  8238. "Outside of structure, run the binding of `"
  8239. (key-description key) "'."
  8240. (when disable-when-heading-prefix
  8241. (concat
  8242. "\nIf `orgstruct-heading-prefix-regexp' is not empty, this command will always fall\n"
  8243. "back to the default binding due to limitations of Org's implementation of\n"
  8244. "`" (symbol-name fun) "'.")))
  8245. (interactive "p")
  8246. (let* ((disable
  8247. ,(and disable-when-heading-prefix
  8248. '(not (string= orgstruct-heading-prefix-regexp ""))))
  8249. (fallback
  8250. (or disable
  8251. (not
  8252. (let* ,bindings
  8253. (org-context-p 'headline 'item
  8254. ,(when (memq fun
  8255. '(org-insert-heading
  8256. org-insert-heading-respect-content
  8257. org-meta-return))
  8258. '(when orgstruct-is-++
  8259. 'item-body))))))))
  8260. (if fallback
  8261. (let* ((orgstruct-mode)
  8262. (binding
  8263. (let ((key ,key))
  8264. (catch 'exit
  8265. (dolist
  8266. (rep
  8267. '(nil
  8268. ("<\\([^>]*\\)tab>" . "\\1TAB")
  8269. ("<\\([^>]*\\)return>" . "\\1RET")
  8270. ("<\\([^>]*\\)escape>" . "\\1ESC")
  8271. ("<\\([^>]*\\)delete>" . "\\1DEL"))
  8272. nil)
  8273. (when rep
  8274. (setq key (read-kbd-macro
  8275. (let ((case-fold-search))
  8276. (replace-regexp-in-string
  8277. (car rep)
  8278. (cdr rep)
  8279. (key-description key))))))
  8280. (when (key-binding key)
  8281. (throw 'exit (key-binding key))))))))
  8282. (if (keymapp binding)
  8283. (org-set-transient-map binding)
  8284. (let ((func (or binding
  8285. (unless disable
  8286. 'orgstruct-error))))
  8287. (when func
  8288. (call-interactively func)))))
  8289. (org-run-like-in-org-mode
  8290. (lambda ()
  8291. (interactive)
  8292. (let* ,bindings
  8293. (call-interactively ',fun)))))))))
  8294. name))
  8295. (defun org-contextualize-keys (alist contexts)
  8296. "Return valid elements in ALIST depending on CONTEXTS.
  8297. `org-agenda-custom-commands' or `org-capture-templates' are the
  8298. values used for ALIST, and `org-agenda-custom-commands-contexts'
  8299. or `org-capture-templates-contexts' are the associated contexts
  8300. definitions."
  8301. (let ((contexts
  8302. ;; normalize contexts
  8303. (mapcar
  8304. (lambda(c) (cond ((listp (cadr c))
  8305. (list (car c) (car c) (nth 1 c)))
  8306. ((string= "" (cadr c))
  8307. (list (car c) (car c) (nth 2 c)))
  8308. (t c)))
  8309. contexts))
  8310. (a alist) r s)
  8311. ;; loop over all commands or templates
  8312. (dolist (c a)
  8313. (let (vrules repl)
  8314. (cond
  8315. ((not (assoc (car c) contexts))
  8316. (push c r))
  8317. ((and (assoc (car c) contexts)
  8318. (setq vrules (org-contextualize-validate-key
  8319. (car c) contexts)))
  8320. (mapc (lambda (vr)
  8321. (unless (equal (car vr) (cadr vr))
  8322. (setq repl vr)))
  8323. vrules)
  8324. (if (not repl) (push c r)
  8325. (push (cadr repl) s)
  8326. (push
  8327. (cons (car c)
  8328. (cdr (or (assoc (cadr repl) alist)
  8329. (error "Undefined key `%s' as contextual replacement for `%s'"
  8330. (cadr repl) (car c)))))
  8331. r))))))
  8332. ;; Return limited ALIST, possibly with keys modified, and deduplicated
  8333. (delq
  8334. nil
  8335. (delete-dups
  8336. (mapcar (lambda (x)
  8337. (let ((tpl (car x)))
  8338. (unless (delq
  8339. nil
  8340. (mapcar (lambda (y)
  8341. (equal y tpl))
  8342. s))
  8343. x)))
  8344. (reverse r))))))
  8345. (defun org-contextualize-validate-key (key contexts)
  8346. "Check CONTEXTS for agenda or capture KEY."
  8347. (let (res)
  8348. (dolist (r contexts)
  8349. (dolist (rr (car (last r)))
  8350. (when
  8351. (and (equal key (car r))
  8352. (if (functionp rr) (funcall rr)
  8353. (or (and (eq (car rr) 'in-file)
  8354. (buffer-file-name)
  8355. (string-match (cdr rr) (buffer-file-name)))
  8356. (and (eq (car rr) 'in-mode)
  8357. (string-match (cdr rr) (symbol-name major-mode)))
  8358. (and (eq (car rr) 'in-buffer)
  8359. (string-match (cdr rr) (buffer-name)))
  8360. (when (and (eq (car rr) 'not-in-file)
  8361. (buffer-file-name))
  8362. (not (string-match (cdr rr) (buffer-file-name))))
  8363. (when (eq (car rr) 'not-in-mode)
  8364. (not (string-match (cdr rr) (symbol-name major-mode))))
  8365. (when (eq (car rr) 'not-in-buffer)
  8366. (not (string-match (cdr rr) (buffer-name)))))))
  8367. (push r res))))
  8368. (delete-dups (delq nil res))))
  8369. (defun org-context-p (&rest contexts)
  8370. "Check if local context is any of CONTEXTS.
  8371. Possible values in the list of contexts are `table', `headline', and `item'."
  8372. (let ((pos (point)))
  8373. (goto-char (point-at-bol))
  8374. (prog1 (or (and (memq 'table contexts)
  8375. (looking-at "[ \t]*|"))
  8376. (and (memq 'headline contexts)
  8377. (looking-at org-outline-regexp))
  8378. (and (memq 'item contexts)
  8379. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  8380. (and (memq 'item-body contexts)
  8381. (org-in-item-p)))
  8382. (goto-char pos))))
  8383. (defconst org-unique-local-variables
  8384. '(org-element--cache
  8385. org-element--cache-objects
  8386. org-element--cache-sync-keys
  8387. org-element--cache-sync-requests
  8388. org-element--cache-sync-timer)
  8389. "List of local variables that cannot be transferred to another buffer.")
  8390. (defun org-get-local-variables ()
  8391. "Return a list of all local variables in an Org mode buffer."
  8392. (delq nil
  8393. (mapcar
  8394. (lambda (x)
  8395. (let* ((binding (if (symbolp x) (list x) (list (car x) (cdr x))))
  8396. (name (car binding)))
  8397. (and (not (get name 'org-state))
  8398. (not (memq name org-unique-local-variables))
  8399. (string-match-p
  8400. "\\`\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|\
  8401. auto-fill\\|normal-auto-fill\\|fill-paragraph\\|indent-\\)"
  8402. (symbol-name name))
  8403. binding)))
  8404. (with-temp-buffer
  8405. (org-mode)
  8406. (buffer-local-variables)))))
  8407. (defun org-clone-local-variables (from-buffer &optional regexp)
  8408. "Clone local variables from FROM-BUFFER.
  8409. Optional argument REGEXP selects variables to clone."
  8410. (dolist (pair (buffer-local-variables from-buffer))
  8411. (let ((name (car pair)))
  8412. (when (and (symbolp name)
  8413. (not (memq name org-unique-local-variables))
  8414. (or (null regexp) (string-match regexp (symbol-name name))))
  8415. (set (make-local-variable name) (cdr pair))))))
  8416. ;;;###autoload
  8417. (defun org-run-like-in-org-mode (cmd)
  8418. "Run a command, pretending that the current buffer is in Org-mode.
  8419. This will temporarily bind local variables that are typically bound in
  8420. Org-mode to the values they have in Org-mode, and then interactively
  8421. call CMD."
  8422. (org-load-modules-maybe)
  8423. (unless org-local-vars
  8424. (setq org-local-vars (org-get-local-variables)))
  8425. (let (binds)
  8426. (dolist (var org-local-vars)
  8427. (when (or (not (boundp (car var)))
  8428. (eq (symbol-value (car var))
  8429. (default-value (car var))))
  8430. (push (list (car var) `(quote ,(cadr var))) binds)))
  8431. (eval `(let ,binds
  8432. (call-interactively (quote ,cmd))))))
  8433. (defun org-get-category (&optional pos force-refresh)
  8434. "Get the category applying to position POS."
  8435. (save-match-data
  8436. (when force-refresh (org-refresh-category-properties))
  8437. (let ((pos (or pos (point))))
  8438. (or (get-text-property pos 'org-category)
  8439. (progn (org-refresh-category-properties)
  8440. (get-text-property pos 'org-category))))))
  8441. ;;; Refresh properties
  8442. (defun org-refresh-properties (dprop tprop)
  8443. "Refresh buffer text properties.
  8444. DPROP is the drawer property and TPROP is either the
  8445. corresponding text property to set, or an alist with each element
  8446. being a text property (as a symbol) and a function to apply to
  8447. the value of the drawer property."
  8448. (let ((case-fold-search t)
  8449. (inhibit-read-only t))
  8450. (org-with-silent-modifications
  8451. (save-excursion
  8452. (save-restriction
  8453. (widen)
  8454. (goto-char (point-min))
  8455. (while (re-search-forward (concat "^[ \t]*:" dprop ": +\\(.*\\)[ \t]*$") nil t)
  8456. (org-refresh-property tprop (org-match-string-no-properties 1))))))))
  8457. (defun org-refresh-property (tprop p)
  8458. "Refresh the buffer text property TPROP from the drawer property P.
  8459. The refresh happens only for the current tree (not subtree)."
  8460. (unless (org-before-first-heading-p)
  8461. (save-excursion
  8462. (org-back-to-heading t)
  8463. (if (symbolp tprop)
  8464. ;; TPROP is a text property symbol
  8465. (put-text-property
  8466. (point) (or (outline-next-heading) (point-max)) tprop p)
  8467. ;; TPROP is an alist with (properties . function) elements
  8468. (dolist (al tprop)
  8469. (save-excursion
  8470. (put-text-property
  8471. (line-beginning-position) (or (outline-next-heading) (point-max))
  8472. (car al)
  8473. (funcall (cdr al) p))))))))
  8474. (defun org-refresh-category-properties ()
  8475. "Refresh category text properties in the buffer."
  8476. (let ((case-fold-search t)
  8477. (inhibit-read-only t)
  8478. (default-category
  8479. (cond ((null org-category)
  8480. (if buffer-file-name
  8481. (file-name-sans-extension
  8482. (file-name-nondirectory buffer-file-name))
  8483. "???"))
  8484. ((symbolp org-category) (symbol-name org-category))
  8485. (t org-category))))
  8486. (org-with-silent-modifications
  8487. (org-with-wide-buffer
  8488. ;; Set buffer-wide category. Search last #+CATEGORY keyword.
  8489. ;; This is the default category for the buffer. If none is
  8490. ;; found, fall-back to `org-category' or buffer file name.
  8491. (put-text-property
  8492. (point-min) (point-max)
  8493. 'org-category
  8494. (catch 'buffer-category
  8495. (goto-char (point-max))
  8496. (while (re-search-backward "^[ \t]*#\\+CATEGORY:" (point-min) t)
  8497. (let ((element (org-element-at-point)))
  8498. (when (eq (org-element-type element) 'keyword)
  8499. (throw 'buffer-category
  8500. (org-element-property :value element)))))
  8501. default-category))
  8502. ;; Set sub-tree specific categories.
  8503. (goto-char (point-min))
  8504. (let ((regexp (org-re-property "CATEGORY")))
  8505. (while (re-search-forward regexp nil t)
  8506. (let ((value (org-match-string-no-properties 3)))
  8507. (when (org-at-property-p)
  8508. (put-text-property
  8509. (save-excursion (org-back-to-heading t) (point))
  8510. (save-excursion (org-end-of-subtree t t) (point))
  8511. 'org-category
  8512. value)))))))))
  8513. (defun org-refresh-stats-properties ()
  8514. "Refresh stats text properties in the buffer."
  8515. (let (stats)
  8516. (org-with-silent-modifications
  8517. (save-excursion
  8518. (save-restriction
  8519. (widen)
  8520. (goto-char (point-min))
  8521. (while (re-search-forward
  8522. (concat org-outline-regexp-bol ".*"
  8523. "\\(?:\\[\\([0-9]+\\)%\\|\\([0-9]+\\)/\\([0-9]+\\)\\]\\)")
  8524. nil t)
  8525. (setq stats (cond ((equal (match-string 3) "0") 0)
  8526. ((match-string 2)
  8527. (/ (* (string-to-number (match-string 2)) 100)
  8528. (string-to-number (match-string 3))))
  8529. (t (string-to-number (match-string 1)))))
  8530. (org-back-to-heading t)
  8531. (put-text-property (point) (progn (org-end-of-subtree t t) (point))
  8532. 'org-stats stats)))))))
  8533. (defun org-refresh-effort-properties ()
  8534. "Refresh effort properties"
  8535. (org-refresh-properties
  8536. org-effort-property
  8537. '((effort . identity)
  8538. (effort-minutes . org-duration-string-to-minutes))))
  8539. ;;;; Link Stuff
  8540. ;;; Link abbreviations
  8541. (defun org-link-expand-abbrev (link)
  8542. "Apply replacements as defined in `org-link-abbrev-alist'."
  8543. (if (string-match "^\\([^:]*\\)\\(::?\\(.*\\)\\)?$" link)
  8544. (let* ((key (match-string 1 link))
  8545. (as (or (assoc key org-link-abbrev-alist-local)
  8546. (assoc key org-link-abbrev-alist)))
  8547. (tag (and (match-end 2) (match-string 3 link)))
  8548. rpl)
  8549. (if (not as)
  8550. link
  8551. (setq rpl (cdr as))
  8552. (cond
  8553. ((symbolp rpl) (funcall rpl tag))
  8554. ((string-match "%(\\([^)]+\\))" rpl)
  8555. (replace-match
  8556. (save-match-data
  8557. (funcall (intern-soft (match-string 1 rpl)) tag)) t t rpl))
  8558. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  8559. ((string-match "%h" rpl)
  8560. (replace-match (url-hexify-string (or tag "")) t t rpl))
  8561. (t (concat rpl tag)))))
  8562. link))
  8563. ;;; Storing and inserting links
  8564. (defvar org-insert-link-history nil
  8565. "Minibuffer history for links inserted with `org-insert-link'.")
  8566. (defvar org-stored-links nil
  8567. "Contains the links stored with `org-store-link'.")
  8568. (defvar org-store-link-plist nil
  8569. "Plist with info about the most recently link created with `org-store-link'.")
  8570. (defvar org-link-protocols nil
  8571. "Link protocols added to Org-mode using `org-add-link-type'.")
  8572. (defvar org-store-link-functions nil
  8573. "List of functions that are called to create and store a link.
  8574. Each function will be called in turn until one returns a non-nil
  8575. value. Each function should check if it is responsible for creating
  8576. this link (for example by looking at the major mode).
  8577. If not, it must exit and return nil.
  8578. If yes, it should return a non-nil value after a calling
  8579. `org-store-link-props' with a list of properties and values.
  8580. Special properties are:
  8581. :type The link prefix, like \"http\". This must be given.
  8582. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  8583. This is obligatory as well.
  8584. :description Optional default description for the second pair
  8585. of brackets in an Org-mode link. The user can still change
  8586. this when inserting this link into an Org-mode buffer.
  8587. In addition to these, any additional properties can be specified
  8588. and then used in capture templates.")
  8589. (defun org-add-link-type (type &optional follow export)
  8590. "Add TYPE to the list of `org-link-types'.
  8591. Re-compute all regular expressions depending on `org-link-types'
  8592. FOLLOW and EXPORT are two functions.
  8593. FOLLOW should take the link path as the single argument and do whatever
  8594. is necessary to follow the link, for example find a file or display
  8595. a mail message.
  8596. EXPORT should format the link path for export to one of the export formats.
  8597. It should be a function accepting three arguments:
  8598. path the path of the link, the text after the prefix (like \"http:\")
  8599. desc the description of the link, if any
  8600. format the export format, a symbol like `html' or `latex' or `ascii'.
  8601. The function may use the FORMAT information to return different values
  8602. depending on the format. The return value will be put literally into
  8603. the exported file. If the return value is nil, this means Org should
  8604. do what it normally does with links which do not have EXPORT defined.
  8605. Org mode has a built-in default for exporting links. If you are happy with
  8606. this default, there is no need to define an export function for the link
  8607. type. For a simple example of an export function, see `org-bbdb.el'."
  8608. (add-to-list 'org-link-types type t)
  8609. (org-make-link-regexps)
  8610. (org-element-update-syntax)
  8611. (if (assoc type org-link-protocols)
  8612. (setcdr (assoc type org-link-protocols) (list follow export))
  8613. (push (list type follow export) org-link-protocols)))
  8614. (defvar org-agenda-buffer-name) ; Defined in org-agenda.el
  8615. (defvar org-id-link-to-org-use-id) ; Defined in org-id.el
  8616. ;;;###autoload
  8617. (defun org-store-link (arg)
  8618. "\\<org-mode-map>Store an org-link to the current location.
  8619. This link is added to `org-stored-links' and can later be inserted
  8620. into an Org buffer with \\[org-insert-link].
  8621. For some link types, a prefix ARG is interpreted.
  8622. For links to Usenet articles, ARG negates `org-gnus-prefer-web-links'.
  8623. For file links, ARG negates `org-context-in-file-links'.
  8624. A double prefix ARG force skipping storing functions that are not
  8625. part of Org's core.
  8626. A triple prefix ARG force storing a link for each line in the
  8627. active region."
  8628. (interactive "P")
  8629. (org-load-modules-maybe)
  8630. (if (and (equal arg '(64)) (org-region-active-p))
  8631. (save-excursion
  8632. (let ((end (region-end)))
  8633. (goto-char (region-beginning))
  8634. (set-mark (point))
  8635. (while (< (point-at-eol) end)
  8636. (move-end-of-line 1) (activate-mark)
  8637. (let (current-prefix-arg)
  8638. (call-interactively 'org-store-link))
  8639. (move-beginning-of-line 2)
  8640. (set-mark (point)))))
  8641. (org-with-limited-levels
  8642. (setq org-store-link-plist nil)
  8643. (let (link cpltxt desc description search
  8644. txt custom-id agenda-link sfuns sfunsn)
  8645. (cond
  8646. ;; Store a link using an external link type
  8647. ((and (not (equal arg '(16)))
  8648. (setq sfuns
  8649. (delq
  8650. nil (mapcar (lambda (f)
  8651. (let (fs) (if (funcall f) (push f fs))))
  8652. org-store-link-functions))
  8653. sfunsn (mapcar (lambda (fu) (symbol-name (car fu))) sfuns))
  8654. (or (and (cdr sfuns)
  8655. (funcall (intern
  8656. (completing-read
  8657. "Which function for creating the link? "
  8658. sfunsn nil t (car sfunsn)))))
  8659. (funcall (caar sfuns)))
  8660. (setq link (plist-get org-store-link-plist :link)
  8661. desc (or (plist-get org-store-link-plist
  8662. :description)
  8663. link))))
  8664. ;; Store a link from a source code buffer.
  8665. ((org-src-edit-buffer-p)
  8666. (cond
  8667. ((save-excursion
  8668. (beginning-of-line)
  8669. (looking-at (concat (format org-coderef-label-format "\\(.*?\\)")
  8670. "[ \t]*$")))
  8671. (setq link (format "(%s)" (org-match-string-no-properties 1))))
  8672. ((org-called-interactively-p 'any)
  8673. (let (label)
  8674. (while (or (not label)
  8675. (org-with-wide-buffer
  8676. (goto-char (point-min))
  8677. (re-search-forward
  8678. (regexp-quote (format org-coderef-label-format label))
  8679. nil t)))
  8680. (when label (message "Label exists already") (sit-for 2))
  8681. (setq label (read-string "Code line label: " label)))
  8682. (end-of-line)
  8683. (setq link (format org-coderef-label-format label))
  8684. (let ((gc (- 79 (length link))))
  8685. (if (< (current-column) gc) (org-move-to-column gc t)
  8686. (insert " ")))
  8687. (insert link)
  8688. (setq link (concat "(" label ")") desc nil)))
  8689. (t (setq link nil))))
  8690. ;; We are in the agenda, link to referenced location
  8691. ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
  8692. (let ((m (or (get-text-property (point) 'org-hd-marker)
  8693. (get-text-property (point) 'org-marker))))
  8694. (when m
  8695. (org-with-point-at m
  8696. (setq agenda-link
  8697. (if (org-called-interactively-p 'any)
  8698. (call-interactively 'org-store-link)
  8699. (org-store-link nil)))))))
  8700. ((eq major-mode 'calendar-mode)
  8701. (let ((cd (calendar-cursor-to-date)))
  8702. (setq link
  8703. (format-time-string
  8704. (car org-time-stamp-formats)
  8705. (apply 'encode-time
  8706. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  8707. nil nil nil))))
  8708. (org-store-link-props :type "calendar" :date cd)))
  8709. ((eq major-mode 'help-mode)
  8710. (setq link (concat "help:" (save-excursion
  8711. (goto-char (point-min))
  8712. (looking-at "^[^ ]+")
  8713. (match-string 0))))
  8714. (org-store-link-props :type "help"))
  8715. ((eq major-mode 'w3-mode)
  8716. (setq cpltxt (if (and (buffer-name)
  8717. (not (string-match "Untitled" (buffer-name))))
  8718. (buffer-name)
  8719. (url-view-url t))
  8720. link (url-view-url t))
  8721. (org-store-link-props :type "w3" :url (url-view-url t)))
  8722. ((eq major-mode 'image-mode)
  8723. (setq cpltxt (concat "file:"
  8724. (abbreviate-file-name buffer-file-name))
  8725. link cpltxt)
  8726. (org-store-link-props :type "image" :file buffer-file-name))
  8727. ;; In dired, store a link to the file of the current line
  8728. ((derived-mode-p 'dired-mode)
  8729. (let ((file (dired-get-filename nil t)))
  8730. (setq file (if file
  8731. (abbreviate-file-name
  8732. (expand-file-name (dired-get-filename nil t)))
  8733. ;; otherwise, no file so use current directory.
  8734. default-directory))
  8735. (setq cpltxt (concat "file:" file)
  8736. link cpltxt)))
  8737. ((setq search (run-hook-with-args-until-success
  8738. 'org-create-file-search-functions))
  8739. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  8740. "::" search))
  8741. (setq cpltxt (or description link)))
  8742. ((and (buffer-file-name (buffer-base-buffer)) (derived-mode-p 'org-mode))
  8743. (setq custom-id (org-entry-get nil "CUSTOM_ID"))
  8744. (cond
  8745. ;; Store a link using the target at point
  8746. ((org-in-regexp "[^<]<<\\([^<>]+\\)>>[^>]" 1)
  8747. (setq cpltxt
  8748. (concat "file:"
  8749. (abbreviate-file-name
  8750. (buffer-file-name (buffer-base-buffer)))
  8751. "::" (match-string 1))
  8752. link cpltxt))
  8753. ((and (featurep 'org-id)
  8754. (or (eq org-id-link-to-org-use-id t)
  8755. (and (org-called-interactively-p 'any)
  8756. (or (eq org-id-link-to-org-use-id 'create-if-interactive)
  8757. (and (eq org-id-link-to-org-use-id
  8758. 'create-if-interactive-and-no-custom-id)
  8759. (not custom-id))))
  8760. (and org-id-link-to-org-use-id (org-entry-get nil "ID"))))
  8761. ;; Store a link using the ID at point
  8762. (setq link (condition-case nil
  8763. (prog1 (org-id-store-link)
  8764. (setq desc (or (plist-get org-store-link-plist
  8765. :description)
  8766. "")))
  8767. (error
  8768. ;; Probably before first headline, link only to file
  8769. (concat "file:"
  8770. (abbreviate-file-name
  8771. (buffer-file-name (buffer-base-buffer))))))))
  8772. (t
  8773. ;; Just link to current headline
  8774. (setq cpltxt (concat "file:"
  8775. (abbreviate-file-name
  8776. (buffer-file-name (buffer-base-buffer)))))
  8777. ;; Add a context search string
  8778. (when (org-xor org-context-in-file-links arg)
  8779. (let* ((ee (org-element-at-point))
  8780. (et (org-element-type ee))
  8781. (ev (plist-get (cadr ee) :value))
  8782. (ek (plist-get (cadr ee) :key))
  8783. (eok (and (stringp ek) (string-match "name" ek))))
  8784. (setq txt (cond
  8785. ((org-at-heading-p) nil)
  8786. ((and (eq et 'keyword) eok) ev)
  8787. ((org-region-active-p)
  8788. (buffer-substring (region-beginning) (region-end)))))
  8789. (when (or (null txt) (string-match "\\S-" txt))
  8790. (setq cpltxt
  8791. (concat cpltxt "::"
  8792. (condition-case nil
  8793. (org-make-org-heading-search-string txt)
  8794. (error "")))
  8795. desc (or (and (eq et 'keyword) eok ev)
  8796. (nth 4 (ignore-errors (org-heading-components)))
  8797. "NONE")))))
  8798. (when (string-match "::\\'" cpltxt)
  8799. (setq cpltxt (substring cpltxt 0 -2)))
  8800. (setq link cpltxt))))
  8801. ((buffer-file-name (buffer-base-buffer))
  8802. ;; Just link to this file here.
  8803. (setq cpltxt (concat "file:"
  8804. (abbreviate-file-name
  8805. (buffer-file-name (buffer-base-buffer)))))
  8806. ;; Add a context string.
  8807. (when (org-xor org-context-in-file-links arg)
  8808. (setq txt (if (org-region-active-p)
  8809. (buffer-substring (region-beginning) (region-end))
  8810. (buffer-substring (point-at-bol) (point-at-eol))))
  8811. ;; Only use search option if there is some text.
  8812. (when (string-match "\\S-" txt)
  8813. (setq cpltxt
  8814. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  8815. desc "NONE")))
  8816. (setq link cpltxt))
  8817. ((org-called-interactively-p 'interactive)
  8818. (user-error "No method for storing a link from this buffer"))
  8819. (t (setq link nil)))
  8820. ;; We're done setting link and desc, clean up
  8821. (when (consp link) (setq cpltxt (car link) link (cdr link)))
  8822. (setq link (or link cpltxt)
  8823. desc (or desc cpltxt))
  8824. (cond ((equal desc "NONE") (setq desc nil))
  8825. ((and desc (string-match org-bracket-link-analytic-regexp desc))
  8826. (let ((d0 (match-string 3 desc))
  8827. (p0 (match-string 5 desc)))
  8828. (setq desc
  8829. (replace-regexp-in-string
  8830. org-bracket-link-regexp
  8831. (concat (or p0 d0)
  8832. (if (equal (length (match-string 0 desc))
  8833. (length desc)) "*" "")) desc)))))
  8834. ;; Return the link
  8835. (if (not (and (or (org-called-interactively-p 'any)
  8836. executing-kbd-macro)
  8837. link))
  8838. (or agenda-link (and link (org-make-link-string link desc)))
  8839. (push (list link desc) org-stored-links)
  8840. (message "Stored: %s" (or desc link))
  8841. (when custom-id
  8842. (setq link (concat "file:" (abbreviate-file-name
  8843. (buffer-file-name)) "::#" custom-id))
  8844. (push (list link desc) org-stored-links))
  8845. (car org-stored-links))))))
  8846. (defun org-store-link-props (&rest plist)
  8847. "Store link properties, extract names, addresses and dates."
  8848. (let ((x (plist-get plist :from)))
  8849. (when x
  8850. (let ((adr (mail-extract-address-components x)))
  8851. (setq plist (plist-put plist :fromname (car adr)))
  8852. (setq plist (plist-put plist :fromaddress (nth 1 adr))))))
  8853. (let ((x (plist-get plist :to)))
  8854. (when x
  8855. (let ((adr (mail-extract-address-components x)))
  8856. (setq plist (plist-put plist :toname (car adr)))
  8857. (setq plist (plist-put plist :toaddress (nth 1 adr))))))
  8858. (let ((x (ignore-errors (date-to-time (plist-get plist :date)))))
  8859. (when x
  8860. (setq plist (plist-put plist :date-timestamp
  8861. (format-time-string
  8862. (org-time-stamp-format t) x)))
  8863. (setq plist (plist-put plist :date-timestamp-inactive
  8864. (format-time-string
  8865. (org-time-stamp-format t t) x)))))
  8866. (let ((from (plist-get plist :from))
  8867. (to (plist-get plist :to)))
  8868. (when (and from to org-from-is-user-regexp)
  8869. (setq plist
  8870. (plist-put plist :fromto
  8871. (if (string-match org-from-is-user-regexp from)
  8872. (concat "to %t")
  8873. (concat "from %f"))))))
  8874. (setq org-store-link-plist plist))
  8875. (defun org-add-link-props (&rest plist)
  8876. "Add these properties to the link property list."
  8877. (let (key value)
  8878. (while plist
  8879. (setq key (pop plist) value (pop plist))
  8880. (setq org-store-link-plist
  8881. (plist-put org-store-link-plist key value)))))
  8882. (defun org-email-link-description (&optional fmt)
  8883. "Return the description part of an email link.
  8884. This takes information from `org-store-link-plist' and formats it
  8885. according to FMT (default from `org-email-link-description-format')."
  8886. (setq fmt (or fmt org-email-link-description-format))
  8887. (let* ((p org-store-link-plist)
  8888. (to (plist-get p :toaddress))
  8889. (from (plist-get p :fromaddress))
  8890. (table
  8891. (list
  8892. (cons "%c" (plist-get p :fromto))
  8893. (cons "%F" (plist-get p :from))
  8894. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  8895. (cons "%T" (plist-get p :to))
  8896. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  8897. (cons "%s" (plist-get p :subject))
  8898. (cons "%d" (plist-get p :date))
  8899. (cons "%m" (plist-get p :message-id)))))
  8900. (when (string-match "%c" fmt)
  8901. ;; Check if the user wrote this message
  8902. (if (and org-from-is-user-regexp from to
  8903. (save-match-data (string-match org-from-is-user-regexp from)))
  8904. (setq fmt (replace-match "to %t" t t fmt))
  8905. (setq fmt (replace-match "from %f" t t fmt))))
  8906. (org-replace-escapes fmt table)))
  8907. (defun org-make-org-heading-search-string (&optional string)
  8908. "Make search string for the current headline or STRING."
  8909. (let ((s (or string
  8910. (and (derived-mode-p 'org-mode)
  8911. (save-excursion
  8912. (org-back-to-heading t)
  8913. (org-element-property :raw-value (org-element-at-point))))))
  8914. (lines org-context-in-file-links))
  8915. (or string (setq s (concat "*" s))) ; Add * for headlines
  8916. (setq s (replace-regexp-in-string "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" "" s))
  8917. (when (and string (integerp lines) (> lines 0))
  8918. (let ((slines (org-split-string s "\n")))
  8919. (when (< lines (length slines))
  8920. (setq s (mapconcat
  8921. 'identity
  8922. (reverse (nthcdr (- (length slines) lines)
  8923. (reverse slines))) "\n")))))
  8924. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  8925. (defun org-make-link-string (link &optional description)
  8926. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  8927. (unless (org-string-nw-p link) (error "Empty link"))
  8928. (let ((uri (cond ((string-match org-link-types-re link)
  8929. (concat (match-string 1 link)
  8930. (org-link-escape (substring link (match-end 1)))))
  8931. ;; For readability, url-encode internal links only
  8932. ;; when absolutely needed (i.e, when they contain
  8933. ;; square brackets). File links however, are
  8934. ;; encoded since, e.g., spaces are significant.
  8935. ((or (file-name-absolute-p link)
  8936. (org-string-match-p "\\`\\.\\.?/\\|[][]" link))
  8937. (org-link-escape link))
  8938. (t link)))
  8939. (description
  8940. (and (org-string-nw-p description)
  8941. ;; Remove brackets from description, as they are fatal.
  8942. (replace-regexp-in-string
  8943. "[][]" (lambda (m) (if (equal "[" m) "{" "}"))
  8944. (org-trim description)))))
  8945. (format "[[%s]%s]"
  8946. uri
  8947. (if description (format "[%s]" description) ""))))
  8948. (defconst org-link-escape-chars
  8949. ;;%20 %5B %5D %25
  8950. '(?\s ?\[ ?\] ?%)
  8951. "List of characters that should be escaped in a link when stored to Org.
  8952. This is the list that is used for internal purposes.")
  8953. (defconst org-link-escape-chars-browser
  8954. ;;%20 %22
  8955. '(?\s ?\")
  8956. "List of characters to be escaped before handing over to the browser.
  8957. If you consider using this constant then you probably want to use
  8958. the function `org-link-escape-browser' instead. See there why
  8959. this constant is a candidate to be removed once Org drops support
  8960. for Emacs 24.1 and 24.2.")
  8961. (defun org-link-escape (text &optional table merge)
  8962. "Return percent escaped representation of TEXT.
  8963. TEXT is a string with the text to escape.
  8964. Optional argument TABLE is a list with characters that should be
  8965. escaped. When nil, `org-link-escape-chars' is used.
  8966. If optional argument MERGE is set, merge TABLE into
  8967. `org-link-escape-chars'."
  8968. (let ((characters-to-encode
  8969. (cond ((null table) org-link-escape-chars)
  8970. (merge (append org-link-escape-chars table))
  8971. (t table))))
  8972. (mapconcat
  8973. (lambda (c)
  8974. (if (or (memq c characters-to-encode)
  8975. (and org-url-hexify-p (or (< c 32) (> c 126))))
  8976. (mapconcat (lambda (e) (format "%%%.2X" e))
  8977. (or (encode-coding-char c 'utf-8)
  8978. (error "Unable to percent escape character: %c" c))
  8979. "")
  8980. (char-to-string c)))
  8981. text "")))
  8982. (defun org-link-escape-browser (text)
  8983. "Escape some characters before handing over to the browser.
  8984. This function is a candidate to be removed together with the
  8985. constant `org-link-escape-chars-browser' once Org drops support
  8986. for Emacs 24.1 and 24.2. All calls to this function will have to
  8987. be replaced with `url-encode-url' which is available since Emacs
  8988. 24.3.1."
  8989. ;; Example with the Org link
  8990. ;; [[http://lists.gnu.org/archive/cgi-bin/namazu.cgi?idxname=emacs-orgmode&query=%252Bsubject:"Release+8.2"]]
  8991. ;; to open the browser with +subject:"Release 8.2" filled into the
  8992. ;; query field: In this case the variable TEXT contains the
  8993. ;; unescaped [...]=%2Bsubject:"Release+8.2". Then `url-encode-url'
  8994. ;; converts correctly to [...]=%2Bsubject:%22Release+8.2%22 or
  8995. ;; `org-link-escape' with `org-link-escape-chars-browser' converts
  8996. ;; wrongly to [...]=%252Bsubject:%22Release+8.2%22.
  8997. (if (fboundp 'url-encode-url)
  8998. (url-encode-url text)
  8999. (if (org-string-match-p
  9000. (concat "[[:nonascii:]" org-link-escape-chars-browser "]")
  9001. text)
  9002. (org-link-escape text org-link-escape-chars-browser)
  9003. text)))
  9004. (defun org-link-unescape (str)
  9005. "Unhex hexified Unicode parts in string STR.
  9006. E.g. `%C3%B6' becomes the german o-Umlaut. This is the
  9007. reciprocal of `org-link-escape', which see."
  9008. (if (org-string-nw-p str)
  9009. (replace-regexp-in-string
  9010. "\\(%[0-9A-Za-z]\\{2\\}\\)+" #'org-link-unescape-compound str t t)
  9011. str))
  9012. (defun org-link-unescape-compound (hex)
  9013. "Unhexify Unicode hex-chars. E.g. `%C3%B6' is the German o-Umlaut.
  9014. Note: this function also decodes single byte encodings like
  9015. `%E1' (a-acute) if not followed by another `%[A-F0-9]{2}' group."
  9016. (save-match-data
  9017. (let* ((bytes (cdr (split-string hex "%")))
  9018. (ret "")
  9019. (eat 0)
  9020. (sum 0))
  9021. (while bytes
  9022. (let* ((val (string-to-number (pop bytes) 16))
  9023. (shift-xor
  9024. (if (= 0 eat)
  9025. (cond
  9026. ((>= val 252) (cons 6 252))
  9027. ((>= val 248) (cons 5 248))
  9028. ((>= val 240) (cons 4 240))
  9029. ((>= val 224) (cons 3 224))
  9030. ((>= val 192) (cons 2 192))
  9031. (t (cons 0 0)))
  9032. (cons 6 128))))
  9033. (when (>= val 192) (setq eat (car shift-xor)))
  9034. (setq val (logxor val (cdr shift-xor)))
  9035. (setq sum (+ (lsh sum (car shift-xor)) val))
  9036. (when (> eat 0) (setq eat (- eat 1)))
  9037. (cond
  9038. ((= 0 eat) ;multi byte
  9039. (setq ret (concat ret (org-char-to-string sum)))
  9040. (setq sum 0))
  9041. ((not bytes) ; single byte(s)
  9042. (setq ret (org-link-unescape-single-byte-sequence hex))))))
  9043. ret)))
  9044. (defun org-link-unescape-single-byte-sequence (hex)
  9045. "Unhexify hex-encoded single byte character sequences."
  9046. (mapconcat (lambda (byte)
  9047. (char-to-string (string-to-number byte 16)))
  9048. (cdr (split-string hex "%")) ""))
  9049. (defun org-xor (a b)
  9050. "Exclusive or."
  9051. (if a (not b) b))
  9052. (defun org-fixup-message-id-for-http (s)
  9053. "Replace special characters in a message id, so it can be used in an http query."
  9054. (when (string-match "%" s)
  9055. (setq s (mapconcat (lambda (c)
  9056. (if (eq c ?%)
  9057. "%25"
  9058. (char-to-string c)))
  9059. s "")))
  9060. (while (string-match "<" s)
  9061. (setq s (replace-match "%3C" t t s)))
  9062. (while (string-match ">" s)
  9063. (setq s (replace-match "%3E" t t s)))
  9064. (while (string-match "@" s)
  9065. (setq s (replace-match "%40" t t s)))
  9066. s)
  9067. (defun org-link-prettify (link)
  9068. "Return a human-readable representation of LINK.
  9069. The car of LINK must be a raw link.
  9070. The cdr of LINK must be either a link description or nil."
  9071. (let ((desc (or (cadr link) "<no description>")))
  9072. (concat (format "%-45s" (substring desc 0 (min (length desc) 40)))
  9073. "<" (car link) ">")))
  9074. ;;;###autoload
  9075. (defun org-insert-link-global ()
  9076. "Insert a link like Org-mode does.
  9077. This command can be called in any mode to insert a link in Org-mode syntax."
  9078. (interactive)
  9079. (org-load-modules-maybe)
  9080. (org-run-like-in-org-mode 'org-insert-link))
  9081. (defun org-insert-all-links (arg &optional pre post)
  9082. "Insert all links in `org-stored-links'.
  9083. When a universal prefix, do not delete the links from `org-stored-links'.
  9084. When `ARG' is a number, insert the last N link(s).
  9085. `PRE' and `POST' are optional arguments to define a string to
  9086. prepend or to append."
  9087. (interactive "P")
  9088. (let ((org-keep-stored-link-after-insertion (equal arg '(4)))
  9089. (links (copy-sequence org-stored-links))
  9090. (pr (or pre "- "))
  9091. (po (or post "\n"))
  9092. (cnt 1) l)
  9093. (if (null org-stored-links)
  9094. (message "No link to insert")
  9095. (while (and (or (listp arg) (>= arg cnt))
  9096. (setq l (if (listp arg)
  9097. (pop links)
  9098. (pop org-stored-links))))
  9099. (setq cnt (1+ cnt))
  9100. (insert pr)
  9101. (org-insert-link nil (car l) (or (cadr l) "<no description>"))
  9102. (insert po)))))
  9103. (defun org-insert-last-stored-link (arg)
  9104. "Insert the last link stored in `org-stored-links'."
  9105. (interactive "p")
  9106. (org-insert-all-links arg "" "\n"))
  9107. (defun org-link-fontify-links-to-this-file ()
  9108. "Fontify links to the current file in `org-stored-links'."
  9109. (let ((f (buffer-file-name)) a b)
  9110. (setq a (mapcar (lambda(l)
  9111. (let ((ll (car l)))
  9112. (when (and (string-match "^file:\\(.+\\)::" ll)
  9113. (equal f (expand-file-name (match-string 1 ll))))
  9114. ll)))
  9115. org-stored-links))
  9116. (when (featurep 'org-id)
  9117. (setq b (mapcar (lambda(l)
  9118. (let ((ll (car l)))
  9119. (when (and (string-match "^id:\\(.+\\)$" ll)
  9120. (equal f (expand-file-name
  9121. (or (org-id-find-id-file
  9122. (match-string 1 ll)) ""))))
  9123. ll)))
  9124. org-stored-links)))
  9125. (mapcar (lambda(l)
  9126. (put-text-property 0 (length l) 'face 'font-lock-comment-face l))
  9127. (delq nil (append a b)))))
  9128. (defvar org--links-history nil)
  9129. (defun org-insert-link (&optional complete-file link-location default-description)
  9130. "Insert a link. At the prompt, enter the link.
  9131. Completion can be used to insert any of the link protocol prefixes like
  9132. http or ftp in use.
  9133. The history can be used to select a link previously stored with
  9134. `org-store-link'. When the empty string is entered (i.e. if you just
  9135. press RET at the prompt), the link defaults to the most recently
  9136. stored link. As SPC triggers completion in the minibuffer, you need to
  9137. use M-SPC or C-q SPC to force the insertion of a space character.
  9138. You will also be prompted for a description, and if one is given, it will
  9139. be displayed in the buffer instead of the link.
  9140. If there is already a link at point, this command will allow you to edit link
  9141. and description parts.
  9142. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  9143. be selected using completion. The path to the file will be relative to the
  9144. current directory if the file is in the current directory or a subdirectory.
  9145. Otherwise, the link will be the absolute path as completed in the minibuffer
  9146. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  9147. option `org-link-file-path-type'.
  9148. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  9149. the current directory or below.
  9150. With three \\[universal-argument] prefixes, negate the meaning of
  9151. `org-keep-stored-link-after-insertion'.
  9152. If `org-make-link-description-function' is non-nil, this function will be
  9153. called with the link target, and the result will be the default
  9154. link description.
  9155. If the LINK-LOCATION parameter is non-nil, this value will be
  9156. used as the link location instead of reading one interactively.
  9157. If the DEFAULT-DESCRIPTION parameter is non-nil, this value will
  9158. be used as the default description."
  9159. (interactive "P")
  9160. (let* ((wcf (current-window-configuration))
  9161. (origbuf (current-buffer))
  9162. (region (when (org-region-active-p)
  9163. (buffer-substring (region-beginning) (region-end))))
  9164. (remove (and region (list (region-beginning) (region-end))))
  9165. (desc region)
  9166. (link link-location)
  9167. (abbrevs org-link-abbrev-alist-local)
  9168. entry all-prefixes auto-desc)
  9169. (cond
  9170. (link-location) ; specified by arg, just use it.
  9171. ((org-in-regexp org-bracket-link-regexp 1)
  9172. ;; We do have a link at point, and we are going to edit it.
  9173. (setq remove (list (match-beginning 0) (match-end 0)))
  9174. (setq desc (when (match-end 3) (org-match-string-no-properties 3)))
  9175. (setq link (read-string "Link: "
  9176. (org-link-unescape
  9177. (org-match-string-no-properties 1)))))
  9178. ((or (org-in-regexp org-angle-link-re)
  9179. (org-in-regexp org-plain-link-re))
  9180. ;; Convert to bracket link
  9181. (setq remove (list (match-beginning 0) (match-end 0))
  9182. link (read-string "Link: "
  9183. (org-remove-angle-brackets (match-string 0)))))
  9184. ((member complete-file '((4) (16)))
  9185. ;; Completing read for file names.
  9186. (setq link (org-file-complete-link complete-file)))
  9187. (t
  9188. ;; Read link, with completion for stored links.
  9189. (org-link-fontify-links-to-this-file)
  9190. (org-switch-to-buffer-other-window "*Org Links*")
  9191. (with-current-buffer "*Org Links*"
  9192. (erase-buffer)
  9193. (insert "Insert a link.
  9194. Use TAB to complete link prefixes, then RET for type-specific completion support\n")
  9195. (when org-stored-links
  9196. (insert "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  9197. (insert (mapconcat 'org-link-prettify
  9198. (reverse org-stored-links) "\n")))
  9199. (goto-char (point-min)))
  9200. (let ((cw (selected-window)))
  9201. (select-window (get-buffer-window "*Org Links*" 'visible))
  9202. (with-current-buffer "*Org Links*" (setq truncate-lines t))
  9203. (unless (pos-visible-in-window-p (point-max))
  9204. (org-fit-window-to-buffer))
  9205. (and (window-live-p cw) (select-window cw)))
  9206. (setq all-prefixes (append (mapcar 'car abbrevs)
  9207. (mapcar 'car org-link-abbrev-alist)
  9208. org-link-types))
  9209. (unwind-protect
  9210. ;; Fake a link history, containing the stored links.
  9211. (let ((org--links-history
  9212. (append (mapcar #'car org-stored-links)
  9213. org-insert-link-history)))
  9214. (setq link
  9215. (org-completing-read
  9216. "Link: "
  9217. (append
  9218. (mapcar (lambda (x) (concat x ":")) all-prefixes)
  9219. (mapcar #'car org-stored-links))
  9220. nil nil nil
  9221. 'org--links-history
  9222. (caar org-stored-links)))
  9223. (unless (org-string-nw-p link) (user-error "No link selected"))
  9224. (dolist (l org-stored-links)
  9225. (when (equal link (cadr l))
  9226. (setq link (car l))
  9227. (setq auto-desc t)))
  9228. (when (or (member link all-prefixes)
  9229. (and (equal ":" (substring link -1))
  9230. (member (substring link 0 -1) all-prefixes)
  9231. (setq link (substring link 0 -1))))
  9232. (setq link (with-current-buffer origbuf
  9233. (org-link-try-special-completion link)))))
  9234. (set-window-configuration wcf)
  9235. (kill-buffer "*Org Links*"))
  9236. (setq entry (assoc link org-stored-links))
  9237. (or entry (push link org-insert-link-history))
  9238. (setq desc (or desc (nth 1 entry)))))
  9239. (when (funcall (if (equal complete-file '(64)) 'not 'identity)
  9240. (not org-keep-stored-link-after-insertion))
  9241. (setq org-stored-links (delq (assoc link org-stored-links)
  9242. org-stored-links)))
  9243. (when (and (string-match org-plain-link-re link)
  9244. (not (string-match org-ts-regexp link)))
  9245. ;; URL-like link, normalize the use of angular brackets.
  9246. (setq link (org-remove-angle-brackets link)))
  9247. ;; Check if we are linking to the current file with a search
  9248. ;; option If yes, simplify the link by using only the search
  9249. ;; option.
  9250. (when (and buffer-file-name
  9251. (string-match "^file:\\(.+?\\)::\\(.+\\)" link))
  9252. (let* ((path (match-string 1 link))
  9253. (case-fold-search nil)
  9254. (search (match-string 2 link)))
  9255. (save-match-data
  9256. (when (equal (file-truename buffer-file-name) (file-truename path))
  9257. ;; We are linking to this same file, with a search option
  9258. (setq link search)))))
  9259. ;; Check if we can/should use a relative path. If yes, simplify the link
  9260. (when (string-match "^\\(file:\\|docview:\\)\\(.*\\)" link)
  9261. (let* ((type (match-string 1 link))
  9262. (path (match-string 2 link))
  9263. (origpath path)
  9264. (case-fold-search nil))
  9265. (cond
  9266. ((or (eq org-link-file-path-type 'absolute)
  9267. (equal complete-file '(16)))
  9268. (setq path (abbreviate-file-name (expand-file-name path))))
  9269. ((eq org-link-file-path-type 'noabbrev)
  9270. (setq path (expand-file-name path)))
  9271. ((eq org-link-file-path-type 'relative)
  9272. (setq path (file-relative-name path)))
  9273. (t
  9274. (save-match-data
  9275. (if (string-match (concat "^" (regexp-quote
  9276. (expand-file-name
  9277. (file-name-as-directory
  9278. default-directory))))
  9279. (expand-file-name path))
  9280. ;; We are linking a file with relative path name.
  9281. (setq path (substring (expand-file-name path)
  9282. (match-end 0)))
  9283. (setq path (abbreviate-file-name (expand-file-name path)))))))
  9284. (setq link (concat type path))
  9285. (when (equal desc origpath)
  9286. (setq desc path))))
  9287. (if org-make-link-description-function
  9288. (setq desc
  9289. (or (condition-case nil
  9290. (funcall org-make-link-description-function link desc)
  9291. (error (progn (message "Can't get link description from `%s'"
  9292. (symbol-name org-make-link-description-function))
  9293. (sit-for 2) nil)))
  9294. (read-string "Description: " default-description)))
  9295. (if default-description (setq desc default-description)
  9296. (setq desc (or (and auto-desc desc)
  9297. (read-string "Description: " desc)))))
  9298. (unless (string-match "\\S-" desc) (setq desc nil))
  9299. (when remove (apply 'delete-region remove))
  9300. (insert (org-make-link-string link desc))
  9301. ;; Redisplay so as the new link has proper invisible characters.
  9302. (sit-for 0)))
  9303. (defun org-link-try-special-completion (type)
  9304. "If there is completion support for link type TYPE, offer it."
  9305. (let ((fun (intern (concat "org-" type "-complete-link"))))
  9306. (if (functionp fun)
  9307. (funcall fun)
  9308. (read-string "Link (no completion support): " (concat type ":")))))
  9309. (defun org-file-complete-link (&optional arg)
  9310. "Create a file link using completion."
  9311. (let ((file (read-file-name "File: "))
  9312. (pwd (file-name-as-directory (expand-file-name ".")))
  9313. (pwd1 (file-name-as-directory (abbreviate-file-name
  9314. (expand-file-name ".")))))
  9315. (cond ((equal arg '(16))
  9316. (concat "file:"
  9317. (abbreviate-file-name (expand-file-name file))))
  9318. ((string-match
  9319. (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  9320. (concat "file:" (match-string 1 file)))
  9321. ((string-match
  9322. (concat "^" (regexp-quote pwd) "\\(.+\\)")
  9323. (expand-file-name file))
  9324. (concat "file:"
  9325. (match-string 1 (expand-file-name file))))
  9326. (t (concat "file:" file)))))
  9327. (defun org-completing-read (&rest args)
  9328. "Completing-read with SPACE being a normal character."
  9329. (let ((enable-recursive-minibuffers t)
  9330. (minibuffer-local-completion-map
  9331. (copy-keymap minibuffer-local-completion-map)))
  9332. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  9333. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  9334. (org-defkey minibuffer-local-completion-map (kbd "C-c !")
  9335. 'org-time-stamp-inactive)
  9336. (apply #'completing-read args)))
  9337. (define-obsolete-function-alias
  9338. 'org-completing-read-no-i 'completing-read "Org 9.0")
  9339. (define-obsolete-function-alias
  9340. 'org-icompleting-read 'completing-read "Org 9.0")
  9341. (define-obsolete-function-alias
  9342. 'org-iread-file-name 'read-file-name "Org 9.0")
  9343. ;;; Opening/following a link
  9344. (defvar org-link-search-failed nil)
  9345. (defvar org-open-link-functions nil
  9346. "Hook for functions finding a plain text link.
  9347. These functions must take a single argument, the link content.
  9348. They will be called for links that look like [[link text][description]]
  9349. when LINK TEXT does not have a protocol like \"http:\" and does not look
  9350. like a filename (e.g. \"./blue.png\").
  9351. These functions will be called *before* Org attempts to resolve the
  9352. link by doing text searches in the current buffer - so if you want a
  9353. link \"[[target]]\" to still find \"<<target>>\", your function should
  9354. handle this as a special case.
  9355. When the function does handle the link, it must return a non-nil value.
  9356. If it decides that it is not responsible for this link, it must return
  9357. nil to indicate that that Org-mode can continue with other options
  9358. like exact and fuzzy text search.")
  9359. (defun org-next-link (&optional search-backward)
  9360. "Move forward to the next link.
  9361. If the link is in hidden text, expose it."
  9362. (interactive "P")
  9363. (when (and org-link-search-failed (eq this-command last-command))
  9364. (goto-char (point-min))
  9365. (message "Link search wrapped back to beginning of buffer"))
  9366. (setq org-link-search-failed nil)
  9367. (let* ((pos (point))
  9368. (ct (org-context))
  9369. (a (assoc :link ct))
  9370. (srch-fun (if search-backward 're-search-backward 're-search-forward)))
  9371. (cond (a (goto-char (nth (if search-backward 1 2) a)))
  9372. ((looking-at org-any-link-re)
  9373. ;; Don't stay stuck at link without an org-link face
  9374. (forward-char (if search-backward -1 1))))
  9375. (if (funcall srch-fun org-any-link-re nil t)
  9376. (progn
  9377. (goto-char (match-beginning 0))
  9378. (when (outline-invisible-p) (org-show-context)))
  9379. (goto-char pos)
  9380. (setq org-link-search-failed t)
  9381. (message "No further link found"))))
  9382. (defun org-previous-link ()
  9383. "Move backward to the previous link.
  9384. If the link is in hidden text, expose it."
  9385. (interactive)
  9386. (funcall 'org-next-link t))
  9387. (defun org-translate-link (s)
  9388. "Translate a link string if a translation function has been defined."
  9389. (with-temp-buffer
  9390. (insert (org-trim s))
  9391. (org-trim (org-element-interpret-data (org-element-context)))))
  9392. (defun org-translate-link-from-planner (type path)
  9393. "Translate a link from Emacs Planner syntax so that Org can follow it.
  9394. This is still an experimental function, your mileage may vary."
  9395. (cond
  9396. ((member type '("http" "https" "news" "ftp"))
  9397. ;; standard Internet links are the same.
  9398. nil)
  9399. ((and (equal type "irc") (string-match "^//" path))
  9400. ;; Planner has two / at the beginning of an irc link, we have 1.
  9401. ;; We should have zero, actually....
  9402. (setq path (substring path 1)))
  9403. ((and (equal type "lisp") (string-match "^/" path))
  9404. ;; Planner has a slash, we do not.
  9405. (setq type "elisp" path (substring path 1)))
  9406. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  9407. ;; A typical message link. Planner has the id after the final slash,
  9408. ;; we separate it with a hash mark
  9409. (setq path (concat (match-string 1 path) "#"
  9410. (org-remove-angle-brackets (match-string 2 path))))))
  9411. (cons type path))
  9412. (defun org-find-file-at-mouse (ev)
  9413. "Open file link or URL at mouse."
  9414. (interactive "e")
  9415. (mouse-set-point ev)
  9416. (org-open-at-point 'in-emacs))
  9417. (defun org-open-at-mouse (ev)
  9418. "Open file link or URL at mouse.
  9419. See the docstring of `org-open-file' for details."
  9420. (interactive "e")
  9421. (mouse-set-point ev)
  9422. (when (eq major-mode 'org-agenda-mode)
  9423. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  9424. (org-open-at-point))
  9425. (defvar org-window-config-before-follow-link nil
  9426. "The window configuration before following a link.
  9427. This is saved in case the need arises to restore it.")
  9428. ;;;###autoload
  9429. (defun org-open-at-point-global ()
  9430. "Follow a link like Org-mode does.
  9431. This command can be called in any mode to follow a link that has
  9432. Org-mode syntax."
  9433. (interactive)
  9434. (org-run-like-in-org-mode 'org-open-at-point))
  9435. ;;;###autoload
  9436. (defun org-open-link-from-string (s &optional arg reference-buffer)
  9437. "Open a link in the string S, as if it was in Org-mode."
  9438. (interactive "sLink: \nP")
  9439. (let ((reference-buffer (or reference-buffer (current-buffer))))
  9440. (with-temp-buffer
  9441. (let ((org-inhibit-startup (not reference-buffer)))
  9442. (org-mode)
  9443. (insert s)
  9444. (goto-char (point-min))
  9445. (when reference-buffer
  9446. (setq org-link-abbrev-alist-local
  9447. (with-current-buffer reference-buffer
  9448. org-link-abbrev-alist-local)))
  9449. (org-open-at-point arg reference-buffer)))))
  9450. (defvar org-open-at-point-functions nil
  9451. "Hook that is run when following a link at point.
  9452. Functions in this hook must return t if they identify and follow
  9453. a link at point. If they don't find anything interesting at point,
  9454. they must return nil.")
  9455. (defvar org-link-search-inhibit-query nil) ;; dynamically scoped
  9456. (defvar clean-buffer-list-kill-buffer-names) ; Defined in midnight.el
  9457. (defun org-open-at-point (&optional arg reference-buffer)
  9458. "Open link, timestamp, footnote or tags at point.
  9459. When point is on a link, follow it. Normally, files will be
  9460. opened by an appropriate application. If the optional prefix
  9461. argument ARG is non-nil, Emacs will visit the file. With
  9462. a double prefix argument, try to open outside of Emacs, in the
  9463. application the system uses for this file type.
  9464. When point is on a timestamp, open the agenda at the day
  9465. specified.
  9466. When point is a footnote definition, move to the first reference
  9467. found. If it is on a reference, move to the associated
  9468. definition.
  9469. When point is on a headline, display a list of every link in the
  9470. entry, so it is possible to pick one, or all, of them. If point
  9471. is on a tag, call `org-tags-view' instead.
  9472. When optional argument REFERENCE-BUFFER is non-nil, it should
  9473. specify a buffer from where the link search should happen. This
  9474. is used internally by `org-open-link-from-string'.
  9475. On top of syntactically correct links, this function will open
  9476. the link at point in comments or comment blocks and the first
  9477. link in a property drawer line."
  9478. (interactive "P")
  9479. ;; On a code block, open block's results.
  9480. (unless (call-interactively 'org-babel-open-src-block-result)
  9481. (org-load-modules-maybe)
  9482. (setq org-window-config-before-follow-link (current-window-configuration))
  9483. (org-remove-occur-highlights nil nil t)
  9484. (unless (run-hook-with-args-until-success 'org-open-at-point-functions)
  9485. (let* ((context
  9486. ;; Only consider supported types, even if they are not
  9487. ;; the closest one.
  9488. (org-element-lineage
  9489. (org-element-context)
  9490. '(clock comment comment-block footnote-definition
  9491. footnote-reference headline inlinetask keyword link
  9492. node-property timestamp)
  9493. t))
  9494. (type (org-element-type context))
  9495. (value (org-element-property :value context)))
  9496. (cond
  9497. ((not context) (user-error "No link found"))
  9498. ;; Exception: open timestamps and links in properties
  9499. ;; drawers, keywords and comments.
  9500. ((memq type '(comment comment-block keyword node-property))
  9501. (cond ((org-in-regexp org-any-link-re)
  9502. (org-open-link-from-string (match-string-no-properties 0)))
  9503. ((or (org-in-regexp org-ts-regexp-both nil t)
  9504. (org-in-regexp org-tsr-regexp-both nil t))
  9505. (org-follow-timestamp-link))
  9506. (t (user-error "No link found"))))
  9507. ;; On a headline or an inlinetask, but not on a timestamp,
  9508. ;; a link, a footnote reference or on tags.
  9509. ((and (memq type '(headline inlinetask))
  9510. ;; Not on tags.
  9511. (progn (save-excursion (beginning-of-line)
  9512. (looking-at org-complex-heading-regexp))
  9513. (or (not (match-beginning 5))
  9514. (< (point) (match-beginning 5)))))
  9515. (let* ((data (org-offer-links-in-entry (current-buffer) (point) arg))
  9516. (links (car data))
  9517. (links-end (cdr data)))
  9518. (if links
  9519. (dolist (link (if (stringp links) (list links) links))
  9520. (search-forward link nil links-end)
  9521. (goto-char (match-beginning 0))
  9522. (org-open-at-point))
  9523. (require 'org-attach)
  9524. (org-attach-reveal 'if-exists))))
  9525. ;; On a clock line, make sure point is on the timestamp
  9526. ;; before opening it.
  9527. ((and (eq type 'clock)
  9528. value
  9529. (>= (point) (org-element-property :begin value))
  9530. (<= (point) (org-element-property :end value)))
  9531. (org-follow-timestamp-link))
  9532. ;; Do nothing on white spaces after an object.
  9533. ((>= (point)
  9534. (save-excursion
  9535. (goto-char (org-element-property :end context))
  9536. (skip-chars-backward " \t")
  9537. (point)))
  9538. (user-error "No link found"))
  9539. ((eq type 'timestamp) (org-follow-timestamp-link))
  9540. ;; On tags within a headline or an inlinetask.
  9541. ((and (memq type '(headline inlinetask))
  9542. (progn (save-excursion (beginning-of-line)
  9543. (looking-at org-complex-heading-regexp))
  9544. (and (match-beginning 5)
  9545. (>= (point) (match-beginning 5)))))
  9546. (org-tags-view arg (substring (match-string 5) 0 -1)))
  9547. ((eq type 'link)
  9548. ;; When link is located within the description of another
  9549. ;; link (e.g., an inline image), always open the parent
  9550. ;; link.
  9551. (let*((link (let ((up (org-element-property :parent context)))
  9552. (if (eq (org-element-type up) 'link) up context)))
  9553. (type (org-element-property :type link))
  9554. (path (org-link-unescape (org-element-property :path link))))
  9555. ;; Switch back to REFERENCE-BUFFER needed when called in
  9556. ;; a temporary buffer through `org-open-link-from-string'.
  9557. (with-current-buffer (or reference-buffer (current-buffer))
  9558. (cond
  9559. ((equal type "file")
  9560. (if (string-match "[*?{]" (file-name-nondirectory path))
  9561. (dired path)
  9562. ;; Look into `org-link-protocols' in order to find
  9563. ;; a DEDICATED-FUNCTION to open file. The function
  9564. ;; will be applied on raw link instead of parsed
  9565. ;; link due to the limitation in `org-add-link-type'
  9566. ;; ("open" function called with a single argument).
  9567. ;; If no such function is found, fallback to
  9568. ;; `org-open-file'.
  9569. ;;
  9570. ;; Note : "file+emacs" and "file+sys" types are
  9571. ;; hard-coded in order to escape the previous
  9572. ;; limitation.
  9573. (let* ((option (org-element-property :search-option link))
  9574. (app (org-element-property :application link))
  9575. (dedicated-function
  9576. (nth 1 (assoc app org-link-protocols))))
  9577. (if dedicated-function
  9578. (funcall dedicated-function
  9579. (concat path
  9580. (and option (concat "::" option))))
  9581. (apply #'org-open-file
  9582. path
  9583. (cond (arg)
  9584. ((equal app "emacs") 'emacs)
  9585. ((equal app "sys") 'system))
  9586. (cond ((not option) nil)
  9587. ((org-string-match-p "\\`[0-9]+\\'" option)
  9588. (list (string-to-number option)))
  9589. (t (list nil
  9590. (org-link-unescape option)))))))))
  9591. ((assoc type org-link-protocols)
  9592. (funcall (nth 1 (assoc type org-link-protocols)) path))
  9593. ((equal type "help")
  9594. (let ((f-or-v (intern path)))
  9595. (cond ((fboundp f-or-v) (describe-function f-or-v))
  9596. ((boundp f-or-v) (describe-variable f-or-v))
  9597. (t (error "Not a known function or variable")))))
  9598. ((member type '("http" "https" "ftp" "mailto" "news"))
  9599. (browse-url (org-link-escape-browser (concat type ":" path))))
  9600. ((equal type "doi")
  9601. (browse-url
  9602. (org-link-escape-browser (concat org-doi-server-url path))))
  9603. ((equal type "message") (browse-url (concat type ":" path)))
  9604. ((equal type "shell")
  9605. (let ((buf (generate-new-buffer "*Org Shell Output*"))
  9606. (cmd path))
  9607. (if (or (and (org-string-nw-p
  9608. org-confirm-shell-link-not-regexp)
  9609. (string-match
  9610. org-confirm-shell-link-not-regexp cmd))
  9611. (not org-confirm-shell-link-function)
  9612. (funcall org-confirm-shell-link-function
  9613. (format "Execute \"%s\" in shell? "
  9614. (org-add-props cmd nil
  9615. 'face 'org-warning))))
  9616. (progn
  9617. (message "Executing %s" cmd)
  9618. (shell-command cmd buf)
  9619. (when (featurep 'midnight)
  9620. (setq clean-buffer-list-kill-buffer-names
  9621. (cons (buffer-name buf)
  9622. clean-buffer-list-kill-buffer-names))))
  9623. (user-error "Abort"))))
  9624. ((equal type "elisp")
  9625. (let ((cmd path))
  9626. (if (or (and (org-string-nw-p
  9627. org-confirm-elisp-link-not-regexp)
  9628. (org-string-match-p
  9629. org-confirm-elisp-link-not-regexp cmd))
  9630. (not org-confirm-elisp-link-function)
  9631. (funcall org-confirm-elisp-link-function
  9632. (format "Execute \"%s\" as elisp? "
  9633. (org-add-props cmd nil
  9634. 'face 'org-warning))))
  9635. (message "%s => %s" cmd
  9636. (if (eq (string-to-char cmd) ?\()
  9637. (eval (read cmd))
  9638. (call-interactively (read cmd))))
  9639. (user-error "Abort"))))
  9640. ((equal type "id")
  9641. (require 'org-id)
  9642. (funcall (nth 1 (assoc "id" org-link-protocols)) path))
  9643. ((member type '("coderef" "custom-id" "fuzzy" "radio"))
  9644. (unless (run-hook-with-args-until-success
  9645. 'org-open-link-functions path)
  9646. (if (not arg) (org-mark-ring-push)
  9647. (switch-to-buffer-other-window
  9648. (org-get-buffer-for-internal-link (current-buffer))))
  9649. (let ((destination
  9650. (org-with-wide-buffer
  9651. (if (equal type "radio")
  9652. (org-search-radio-target
  9653. (org-element-property :path link))
  9654. (org-link-search
  9655. (if (member type '("custom-id" "coderef"))
  9656. (org-element-property :raw-link link)
  9657. path)
  9658. ;; Prevent fuzzy links from matching
  9659. ;; themselves.
  9660. (and (equal type "fuzzy")
  9661. (+ 2 (org-element-property :begin link)))))
  9662. (point))))
  9663. (unless (and (<= (point-min) destination)
  9664. (>= (point-max) destination))
  9665. (widen))
  9666. (goto-char destination))))
  9667. (t (browse-url-at-point))))))
  9668. ;; On a footnote reference or at a footnote definition's label.
  9669. ((or (eq type 'footnote-reference)
  9670. (and (eq type 'footnote-definition)
  9671. (save-excursion
  9672. ;; Do not validate action when point is on the
  9673. ;; spaces right after the footnote label, in
  9674. ;; order to be on par with behaviour on links.
  9675. (skip-chars-forward " \t")
  9676. (let ((begin
  9677. (org-element-property :contents-begin context)))
  9678. (if begin (< (point) begin)
  9679. (= (org-element-property :post-affiliated context)
  9680. (line-beginning-position)))))))
  9681. (org-footnote-action))
  9682. (t (user-error "No link found")))))
  9683. (run-hook-with-args 'org-follow-link-hook)))
  9684. (defun org-offer-links-in-entry (buffer marker &optional nth zero)
  9685. "Offer links in the current entry and return the selected link.
  9686. If there is only one link, return it.
  9687. If NTH is an integer, return the NTH link found.
  9688. If ZERO is a string, check also this string for a link, and if
  9689. there is one, return it."
  9690. (with-current-buffer buffer
  9691. (save-excursion
  9692. (save-restriction
  9693. (widen)
  9694. (goto-char marker)
  9695. (let ((cnt ?0)
  9696. have-zero end links link c)
  9697. (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
  9698. (push (match-string 0 zero) links)
  9699. (setq cnt (1- cnt) have-zero t))
  9700. (save-excursion
  9701. (org-back-to-heading t)
  9702. (setq end (save-excursion (outline-next-heading) (point)))
  9703. (while (re-search-forward org-any-link-re end t)
  9704. (push (match-string 0) links))
  9705. (setq links (org-uniquify (reverse links))))
  9706. (cond
  9707. ((null links)
  9708. (message "No links"))
  9709. ((equal (length links) 1)
  9710. (setq link (car links)))
  9711. ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
  9712. (setq link (nth (if have-zero nth (1- nth)) links)))
  9713. (t ; we have to select a link
  9714. (save-excursion
  9715. (save-window-excursion
  9716. (delete-other-windows)
  9717. (with-output-to-temp-buffer "*Select Link*"
  9718. (dolist (l links)
  9719. (cond
  9720. ((not (string-match org-bracket-link-regexp l))
  9721. (princ (format "[%c] %s\n" (cl-incf cnt)
  9722. (org-remove-angle-brackets l))))
  9723. ((match-end 3)
  9724. (princ (format "[%c] %s (%s)\n" (cl-incf cnt)
  9725. (match-string 3 l) (match-string 1 l))))
  9726. (t (princ (format "[%c] %s\n" (cl-incf cnt)
  9727. (match-string 1 l)))))))
  9728. (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
  9729. (message "Select link to open, RET to open all:")
  9730. (setq c (read-char-exclusive))
  9731. (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
  9732. (when (equal c ?q) (user-error "Abort"))
  9733. (if (equal c ?\C-m)
  9734. (setq link links)
  9735. (setq nth (- c ?0))
  9736. (when have-zero (setq nth (1+ nth)))
  9737. (unless (and (integerp nth) (>= (length links) nth))
  9738. (user-error "Invalid link selection"))
  9739. (setq link (nth (1- nth) links)))))
  9740. (cons link end))))))
  9741. ;; TODO: These functions are deprecated since `org-open-at-point'
  9742. ;; hard-codes behaviour for "file+emacs" and "file+sys" types.
  9743. (defun org-open-file-with-system (path)
  9744. "Open file at PATH using the system way of opening it."
  9745. (org-open-file path 'system))
  9746. (defun org-open-file-with-emacs (path)
  9747. "Open file at PATH in Emacs."
  9748. (org-open-file path 'emacs))
  9749. ;;; File search
  9750. (defvar org-create-file-search-functions nil
  9751. "List of functions to construct the right search string for a file link.
  9752. These functions are called in turn with point at the location to
  9753. which the link should point.
  9754. A function in the hook should first test if it would like to
  9755. handle this file type, for example by checking the `major-mode'
  9756. or the file extension. If it decides not to handle this file, it
  9757. should just return nil to give other functions a chance. If it
  9758. does handle the file, it must return the search string to be used
  9759. when following the link. The search string will be part of the
  9760. file link, given after a double colon, and `org-open-at-point'
  9761. will automatically search for it. If special measures must be
  9762. taken to make the search successful, another function should be
  9763. added to the companion hook `org-execute-file-search-functions',
  9764. which see.
  9765. A function in this hook may also use `setq' to set the variable
  9766. `description' to provide a suggestion for the descriptive text to
  9767. be used for this link when it gets inserted into an Org-mode
  9768. buffer with \\[org-insert-link].")
  9769. (defvar org-execute-file-search-functions nil
  9770. "List of functions to execute a file search triggered by a link.
  9771. Functions added to this hook must accept a single argument, the
  9772. search string that was part of the file link, the part after the
  9773. double colon. The function must first check if it would like to
  9774. handle this search, for example by checking the `major-mode' or
  9775. the file extension. If it decides not to handle this search, it
  9776. should just return nil to give other functions a chance. If it
  9777. does handle the search, it must return a non-nil value to keep
  9778. other functions from trying.
  9779. Each function can access the current prefix argument through the
  9780. variable `current-prefix-arg'. Note that a single prefix is used
  9781. to force opening a link in Emacs, so it may be good to only use a
  9782. numeric or double prefix to guide the search function.
  9783. In case this is needed, a function in this hook can also restore
  9784. the window configuration before `org-open-at-point' was called using:
  9785. \(set-window-configuration org-window-config-before-follow-link)")
  9786. (defun org-search-radio-target (target)
  9787. "Search a radio target matching TARGET in current buffer.
  9788. White spaces are not significant."
  9789. (let ((re (format "<<<%s>>>"
  9790. (mapconcat #'regexp-quote
  9791. (org-split-string target "[ \t\n]+")
  9792. "[ \t]+\\(?:\n[ \t]*\\)?")))
  9793. (origin (point)))
  9794. (goto-char (point-min))
  9795. (catch :radio-match
  9796. (while (re-search-forward re nil t)
  9797. (backward-char)
  9798. (let ((object (org-element-context)))
  9799. (when (eq (org-element-type object) 'radio-target)
  9800. (goto-char (org-element-property :begin object))
  9801. (org-show-context 'link-search)
  9802. (throw :radio-match nil))))
  9803. (goto-char origin)
  9804. (user-error "No match for radio target: %s" target))))
  9805. (defun org-link-search (s &optional avoid-pos stealth)
  9806. "Search for a search string S.
  9807. If S starts with \"#\", it triggers a custom ID search.
  9808. If S is enclosed within parenthesis, it initiates a coderef
  9809. search.
  9810. If S is surrounded by forward slashes, it is interpreted as
  9811. a regular expression. In Org mode files, this will create an
  9812. `org-occur' sparse tree. In ordinary files, `occur' will be used
  9813. to list matches. If the current buffer is in `dired-mode', grep
  9814. will be used to search in all files.
  9815. When AVOID-POS is given, ignore matches near that position.
  9816. When optional argument STEALTH is non-nil, do not modify
  9817. visibility around point, thus ignoring `org-show-context-detail'
  9818. variable.
  9819. Search is case-insensitive and ignores white spaces. Return type
  9820. of matched result, with is either `dedicated' or `fuzzy'."
  9821. (unless (org-string-nw-p s) (error "Invalid search string \"%s\"" s))
  9822. (let* ((case-fold-search t)
  9823. (origin (point))
  9824. (normalized (replace-regexp-in-string "\n[ \t]*" " " s))
  9825. (words (org-split-string s "[ \t\n]+"))
  9826. (s-multi-re (mapconcat #'regexp-quote words "[ \t]+\\(?:\n[ \t]*\\)?"))
  9827. (s-single-re (mapconcat #'regexp-quote words "[ \t]+"))
  9828. type)
  9829. (cond
  9830. ;; Check if there are any special search functions.
  9831. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  9832. ((eq (string-to-char s) ?#)
  9833. ;; Look for a custom ID S if S starts with "#".
  9834. (let* ((id (substring normalized 1))
  9835. (match (org-find-property "CUSTOM_ID" id)))
  9836. (if match (progn (goto-char match) (setf type 'dedicated))
  9837. (error "No match for custom ID: %s" id))))
  9838. ((string-match "\\`(\\(.*\\))\\'" normalized)
  9839. ;; Look for coderef targets if S is enclosed within parenthesis.
  9840. (let ((coderef (match-string-no-properties 1 normalized))
  9841. (re (substring s-single-re 1 -1)))
  9842. (goto-char (point-min))
  9843. (catch :coderef-match
  9844. (while (re-search-forward re nil t)
  9845. (let ((element (org-element-at-point)))
  9846. (when (and (memq (org-element-type element)
  9847. '(example-block src-block))
  9848. ;; Build proper regexp according to current
  9849. ;; block's label format.
  9850. (let ((label-fmt
  9851. (regexp-quote
  9852. (or (org-element-property :label-fmt element)
  9853. org-coderef-label-format))))
  9854. (save-excursion
  9855. (beginning-of-line)
  9856. (looking-at (format ".*?\\(%s\\)[ \t]*$"
  9857. (format label-fmt coderef))))))
  9858. (setq type 'dedicated)
  9859. (goto-char (match-beginning 1))
  9860. (throw :coderef-match nil))))
  9861. (goto-char origin)
  9862. (error "No match for coderef: %s" coderef))))
  9863. ((string-match "\\`/\\(.*\\)/\\'" normalized)
  9864. ;; Look for a regular expression.
  9865. (funcall (if (derived-mode-p 'org-mode) #'org-occur #'org-do-occur)
  9866. (match-string 1 s)))
  9867. ;; Fuzzy links.
  9868. (t
  9869. (let ((starred (eq (string-to-char normalized) ?*)))
  9870. (cond
  9871. ;; Look for targets, only if not in a headline search.
  9872. ((and (not starred)
  9873. (let ((target (format "<<%s>>" s-multi-re)))
  9874. (catch :target-match
  9875. (goto-char (point-min))
  9876. (while (re-search-forward target nil t)
  9877. (backward-char)
  9878. (let ((context (org-element-context)))
  9879. (when (eq (org-element-type context) 'target)
  9880. (setq type 'dedicated)
  9881. (goto-char (org-element-property :begin context))
  9882. (throw :target-match t))))
  9883. nil))))
  9884. ;; Look for elements named after S, only if not in a headline
  9885. ;; search.
  9886. ((and (not starred)
  9887. (let ((name (format "^[ \t]*#\\+NAME: +%s[ \t]*$" s-single-re)))
  9888. (catch :name-match
  9889. (goto-char (point-min))
  9890. (while (re-search-forward name nil t)
  9891. (let ((element (org-element-at-point)))
  9892. (when (equal (org-split-string
  9893. (org-element-property :name element)
  9894. "[ \t]+")
  9895. words)
  9896. (setq type 'dedicated)
  9897. (beginning-of-line)
  9898. (throw :name-match t))))
  9899. nil))))
  9900. ;; Regular text search. Prefer headlines in Org mode
  9901. ;; buffers.
  9902. ((and (derived-mode-p 'org-mode)
  9903. (let* ((wspace "[ \t]")
  9904. (wspaceopt (concat wspace "*"))
  9905. (cookie (concat "\\(?:"
  9906. wspaceopt
  9907. "\\[[0-9]*\\(?:%\\|/[0-9]*\\)\\]"
  9908. wspaceopt
  9909. "\\)"))
  9910. (sep (concat "\\(?:\\(?:" wspace "\\|" cookie "\\)+\\)"))
  9911. (re (concat
  9912. org-outline-regexp-bol
  9913. "\\(?:" org-todo-regexp "[ \t]+\\)?"
  9914. "\\(?:\\[#.\\][ \t]+\\)?"
  9915. "\\(?:" org-comment-string "[ \t]+\\)?"
  9916. sep "?"
  9917. (let ((title (mapconcat #'regexp-quote
  9918. words
  9919. sep)))
  9920. (if starred (substring title 1) title))
  9921. sep "?"
  9922. (org-re "\\(?:[ \t]+:[[:alnum:]_@#%%:]+:\\)?")
  9923. "[ \t]*$")))
  9924. (goto-char (point-min))
  9925. (re-search-forward re nil t)))
  9926. (goto-char (match-beginning 0))
  9927. (setq type 'dedicated))
  9928. ;; Offer to create non-existent headline depending on
  9929. ;; `org-link-search-must-match-exact-headline'.
  9930. ((and (derived-mode-p 'org-mode)
  9931. (not org-link-search-inhibit-query)
  9932. (eq org-link-search-must-match-exact-headline 'query-to-create)
  9933. (yes-or-no-p "No match - create this as a new heading? "))
  9934. (goto-char (point-max))
  9935. (unless (bolp) (newline))
  9936. (org-insert-heading nil t t)
  9937. (insert s "\n")
  9938. (beginning-of-line 0))
  9939. ;; Only headlines are looked after. No need to process
  9940. ;; further: throw an error.
  9941. ((and (derived-mode-p 'org-mode)
  9942. (or starred org-link-search-must-match-exact-headline))
  9943. (goto-char origin)
  9944. (error "No match for fuzzy expression: %s" normalized))
  9945. ;; Regular text search.
  9946. ((catch :fuzzy-match
  9947. (goto-char (point-min))
  9948. (while (re-search-forward s-multi-re nil t)
  9949. ;; Skip match if it contains AVOID-POS or it is included
  9950. ;; in a link with a description but outside the
  9951. ;; description.
  9952. (unless (or (and avoid-pos
  9953. (<= (match-beginning 0) avoid-pos)
  9954. (> (match-end 0) avoid-pos))
  9955. (and (save-match-data
  9956. (org-in-regexp org-bracket-link-regexp))
  9957. (match-beginning 3)
  9958. (or (> (match-beginning 3) (point))
  9959. (<= (match-end 3) (point)))
  9960. (org-element-lineage
  9961. (save-match-data (org-element-context))
  9962. '(link) t)))
  9963. (goto-char (match-beginning 0))
  9964. (setq type 'fuzzy)
  9965. (throw :fuzzy-match t)))
  9966. nil))
  9967. ;; All failed. Throw an error.
  9968. (t (goto-char origin)
  9969. (error "No match for fuzzy expression: %s" normalized))))))
  9970. ;; Disclose surroundings of match, if appropriate.
  9971. (when (and (derived-mode-p 'org-mode) (not stealth))
  9972. (org-show-context 'link-search))
  9973. type))
  9974. (defun org-get-buffer-for-internal-link (buffer)
  9975. "Return a buffer to be used for displaying the link target of internal links."
  9976. (cond
  9977. ((not org-display-internal-link-with-indirect-buffer)
  9978. buffer)
  9979. ((string-match "(Clone)$" (buffer-name buffer))
  9980. (message "Buffer is already a clone, not making another one")
  9981. ;; we also do not modify visibility in this case
  9982. buffer)
  9983. (t ; make a new indirect buffer for displaying the link
  9984. (let* ((bn (buffer-name buffer))
  9985. (ibn (concat bn "(Clone)"))
  9986. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  9987. (with-current-buffer ib (org-overview))
  9988. ib))))
  9989. (defun org-do-occur (regexp &optional cleanup)
  9990. "Call the Emacs command `occur'.
  9991. If CLEANUP is non-nil, remove the printout of the regular expression
  9992. in the *Occur* buffer. This is useful if the regex is long and not useful
  9993. to read."
  9994. (occur regexp)
  9995. (when cleanup
  9996. (let ((cwin (selected-window)) win beg end)
  9997. (when (setq win (get-buffer-window "*Occur*"))
  9998. (select-window win))
  9999. (goto-char (point-min))
  10000. (when (re-search-forward "match[a-z]+" nil t)
  10001. (setq beg (match-end 0))
  10002. (when (re-search-forward "^[ \t]*[0-9]+" nil t)
  10003. (setq end (1- (match-beginning 0)))))
  10004. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  10005. (goto-char (point-min))
  10006. (select-window cwin))))
  10007. ;;; The mark ring for links jumps
  10008. (defvar org-mark-ring nil
  10009. "Mark ring for positions before jumps in Org-mode.")
  10010. (defvar org-mark-ring-last-goto nil
  10011. "Last position in the mark ring used to go back.")
  10012. ;; Fill and close the ring
  10013. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  10014. (dotimes (_ org-mark-ring-length)
  10015. (push (make-marker) org-mark-ring))
  10016. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  10017. org-mark-ring)
  10018. (defun org-mark-ring-push (&optional pos buffer)
  10019. "Put the current position or POS into the mark ring and rotate it."
  10020. (interactive)
  10021. (setq pos (or pos (point)))
  10022. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  10023. (move-marker (car org-mark-ring)
  10024. (or pos (point))
  10025. (or buffer (current-buffer)))
  10026. (message "%s"
  10027. (substitute-command-keys
  10028. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  10029. (defun org-mark-ring-goto (&optional n)
  10030. "Jump to the previous position in the mark ring.
  10031. With prefix arg N, jump back that many stored positions. When
  10032. called several times in succession, walk through the entire ring.
  10033. Org-mode commands jumping to a different position in the current file,
  10034. or to another Org-mode file, automatically push the old position
  10035. onto the ring."
  10036. (interactive "p")
  10037. (let (p m)
  10038. (if (eq last-command this-command)
  10039. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  10040. (setq p org-mark-ring))
  10041. (setq org-mark-ring-last-goto p)
  10042. (setq m (car p))
  10043. (org-pop-to-buffer-same-window (marker-buffer m))
  10044. (goto-char m)
  10045. (when (or (outline-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  10046. ;;; TODO: Use string-remove-prefix and -suffix once we only support
  10047. ;;; Emacs 24.4+
  10048. (defun org-remove-angle-brackets (s)
  10049. (when (equal (substring s 0 1) "<") (setq s (substring s 1)))
  10050. (when (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  10051. s)
  10052. (defun org-add-angle-brackets (s)
  10053. (unless (equal (substring s 0 1) "<") (setq s (concat "<" s)))
  10054. (unless (equal (substring s -1) ">") (setq s (concat s ">")))
  10055. s)
  10056. (defun org-remove-double-quotes (s)
  10057. (when (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  10058. (when (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  10059. s)
  10060. ;;; Following specific links
  10061. (defvar org-agenda-buffer-tmp-name)
  10062. (defvar org-agenda-start-on-weekday)
  10063. (defun org-follow-timestamp-link ()
  10064. "Open an agenda view for the time-stamp date/range at point."
  10065. (cond
  10066. ((org-at-date-range-p t)
  10067. (let ((org-agenda-start-on-weekday)
  10068. (t1 (match-string 1))
  10069. (t2 (match-string 2)) tt1 tt2)
  10070. (setq tt1 (time-to-days (org-time-string-to-time t1))
  10071. tt2 (time-to-days (org-time-string-to-time t2)))
  10072. (let ((org-agenda-buffer-tmp-name
  10073. (format "*Org Agenda(a:%s)"
  10074. (concat (substring t1 0 10) "--" (substring t2 0 10)))))
  10075. (org-agenda-list nil tt1 (1+ (- tt2 tt1))))))
  10076. ((org-at-timestamp-p t)
  10077. (let ((org-agenda-buffer-tmp-name
  10078. (format "*Org Agenda(a:%s)" (substring (match-string 1) 0 10))))
  10079. (org-agenda-list nil (time-to-days (org-time-string-to-time
  10080. (substring (match-string 1) 0 10)))
  10081. 1)))
  10082. (t (error "This should not happen"))))
  10083. ;;; Following file links
  10084. (declare-function mailcap-parse-mailcaps "mailcap" (&optional path force))
  10085. (declare-function mailcap-extension-to-mime "mailcap" (extn))
  10086. (declare-function mailcap-mime-info
  10087. "mailcap" (string &optional request no-decode))
  10088. (defvar org-wait nil)
  10089. (defun org-open-file (path &optional in-emacs line search)
  10090. "Open the file at PATH.
  10091. First, this expands any special file name abbreviations. Then the
  10092. configuration variable `org-file-apps' is checked if it contains an
  10093. entry for this file type, and if yes, the corresponding command is launched.
  10094. If no application is found, Emacs simply visits the file.
  10095. With optional prefix argument IN-EMACS, Emacs will visit the file.
  10096. With a double \\[universal-argument] \\[universal-argument] \
  10097. prefix arg, Org tries to avoid opening in Emacs
  10098. and to use an external application to visit the file.
  10099. Optional LINE specifies a line to go to, optional SEARCH a string
  10100. to search for. If LINE or SEARCH is given, the file will be
  10101. opened in Emacs, unless an entry from org-file-apps that makes
  10102. use of groups in a regexp matches.
  10103. If you want to change the way frames are used when following a
  10104. link, please customize `org-link-frame-setup'.
  10105. If the file does not exist, an error is thrown."
  10106. (let* ((file (if (equal path "")
  10107. buffer-file-name
  10108. (substitute-in-file-name (expand-file-name path))))
  10109. (file-apps (append org-file-apps (org-default-apps)))
  10110. (apps (cl-remove-if
  10111. 'org-file-apps-entry-match-against-dlink-p file-apps))
  10112. (apps-dlink (cl-remove-if-not
  10113. 'org-file-apps-entry-match-against-dlink-p file-apps))
  10114. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  10115. (dirp (unless remp (file-directory-p file)))
  10116. (file (if (and dirp org-open-directory-means-index-dot-org)
  10117. (concat (file-name-as-directory file) "index.org")
  10118. file))
  10119. (a-m-a-p (assq 'auto-mode apps))
  10120. (dfile (downcase file))
  10121. ;; Reconstruct the original link from the PATH, LINE and
  10122. ;; SEARCH args.
  10123. (link (cond (line (concat file "::" (number-to-string line)))
  10124. (search (concat file "::" search))
  10125. (t file)))
  10126. (dlink (downcase link))
  10127. (old-buffer (current-buffer))
  10128. (old-pos (point))
  10129. (old-mode major-mode)
  10130. (ext
  10131. (and (string-match "\\`.*?\\.\\([a-zA-Z0-9]+\\(\\.gz\\)?\\)\\'" dfile)
  10132. (match-string 1 dfile)))
  10133. cmd link-match-data)
  10134. (cond
  10135. ((member in-emacs '((16) system))
  10136. (setq cmd (cdr (assq 'system apps))))
  10137. (in-emacs (setq cmd 'emacs))
  10138. (t
  10139. (setq cmd (or (and remp (cdr (assq 'remote apps)))
  10140. (and dirp (cdr (assq 'directory apps)))
  10141. ;; First, try matching against apps-dlink if we
  10142. ;; get a match here, store the match data for
  10143. ;; later.
  10144. (let ((match (assoc-default dlink apps-dlink
  10145. 'string-match)))
  10146. (if match
  10147. (progn (setq link-match-data (match-data))
  10148. match)
  10149. (progn (setq in-emacs (or in-emacs line search))
  10150. nil))) ; if we have no match in apps-dlink,
  10151. ; always open the file in emacs if line or search
  10152. ; is given (for backwards compatibility)
  10153. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  10154. 'string-match)
  10155. (cdr (assoc ext apps))
  10156. (cdr (assq t apps))))))
  10157. (when (eq cmd 'system)
  10158. (setq cmd (cdr (assoc 'system apps))))
  10159. (when (eq cmd 'default)
  10160. (setq cmd (cdr (assoc t apps))))
  10161. (when (eq cmd 'mailcap)
  10162. (require 'mailcap)
  10163. (mailcap-parse-mailcaps)
  10164. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  10165. (command (mailcap-mime-info mime-type)))
  10166. (if (stringp command)
  10167. (setq cmd command)
  10168. (setq cmd 'emacs))))
  10169. (when (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  10170. (not (file-exists-p file))
  10171. (not org-open-non-existing-files))
  10172. (user-error "No such file: %s" file))
  10173. (cond
  10174. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  10175. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  10176. (while (string-match "['\"]%s['\"]" cmd)
  10177. (setq cmd (replace-match "%s" t t cmd)))
  10178. (while (string-match "%s" cmd)
  10179. (setq cmd (replace-match
  10180. (save-match-data
  10181. (shell-quote-argument
  10182. (convert-standard-filename file)))
  10183. t t cmd)))
  10184. ;; Replace "%1", "%2" etc. in command with group matches from regex
  10185. (save-match-data
  10186. (let ((match-index 1)
  10187. (number-of-groups (- (/ (length link-match-data) 2) 1)))
  10188. (set-match-data link-match-data)
  10189. (while (<= match-index number-of-groups)
  10190. (let ((regex (concat "%" (number-to-string match-index)))
  10191. (replace-with (match-string match-index dlink)))
  10192. (while (string-match regex cmd)
  10193. (setq cmd (replace-match replace-with t t cmd))))
  10194. (setq match-index (+ match-index 1)))))
  10195. (save-window-excursion
  10196. (message "Running %s...done" cmd)
  10197. (start-process-shell-command cmd nil cmd)
  10198. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))))
  10199. ((or (stringp cmd)
  10200. (eq cmd 'emacs))
  10201. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  10202. (widen)
  10203. (cond (line (org-goto-line line)
  10204. (when (derived-mode-p 'org-mode) (org-reveal)))
  10205. (search (org-link-search search))))
  10206. ((functionp cmd)
  10207. (save-match-data
  10208. (set-match-data link-match-data)
  10209. (condition-case nil
  10210. (funcall cmd file link)
  10211. ;; FIXME: Remove this check when most default installations
  10212. ;; of Emacs have at least Org 9.0.
  10213. ((debug wrong-number-of-arguments wrong-type-argument
  10214. invalid-function)
  10215. (user-error "Please see Org News for version 9.0 about \
  10216. `org-file-apps'--Lisp error: %S" cmd)))))
  10217. ((consp cmd)
  10218. ;; FIXME: Remove this check when most default installations of
  10219. ;; Emacs have at least Org 9.0.
  10220. ;; Heads-up instead of silently fall back to
  10221. ;; `org-link-frame-setup' for an old usage of `org-file-apps'
  10222. ;; with sexp instead of a function for `cmd'.
  10223. (user-error "Please see Org News for version 9.0 about \
  10224. `org-file-apps'--Error: Deprecated usage of %S" cmd))
  10225. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  10226. (and (derived-mode-p 'org-mode)
  10227. (eq old-mode 'org-mode)
  10228. (or (not (eq old-buffer (current-buffer)))
  10229. (not (eq old-pos (point))))
  10230. (org-mark-ring-push old-pos old-buffer))))
  10231. (defun org-file-apps-entry-match-against-dlink-p (entry)
  10232. "This function returns non-nil if `entry' uses a regular
  10233. expression which should be matched against the whole link by
  10234. org-open-file.
  10235. It assumes that is the case when the entry uses a regular
  10236. expression which has at least one grouping construct and the
  10237. action is either a lisp form or a command string containing
  10238. `%1', i.e. using at least one subexpression match as a
  10239. parameter."
  10240. (let ((selector (car entry))
  10241. (action (cdr entry)))
  10242. (if (stringp selector)
  10243. (and (> (regexp-opt-depth selector) 0)
  10244. (or (and (stringp action)
  10245. (string-match "%[0-9]" action))
  10246. (consp action)))
  10247. nil)))
  10248. (defun org-default-apps ()
  10249. "Return the default applications for this operating system."
  10250. (cond
  10251. ((eq system-type 'darwin)
  10252. org-file-apps-defaults-macosx)
  10253. ((eq system-type 'windows-nt)
  10254. org-file-apps-defaults-windowsnt)
  10255. (t org-file-apps-defaults-gnu)))
  10256. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  10257. "Convert extensions to regular expressions in the cars of LIST.
  10258. Also, weed out any non-string entries, because the return value is used
  10259. only for regexp matching.
  10260. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  10261. point to the symbol `emacs', indicating that the file should
  10262. be opened in Emacs."
  10263. (append
  10264. (delq nil
  10265. (mapcar (lambda (x)
  10266. (unless (not (stringp (car x)))
  10267. (if (string-match "\\W" (car x))
  10268. x
  10269. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  10270. list))
  10271. (when add-auto-mode
  10272. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  10273. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  10274. (defun org-file-remote-p (file)
  10275. "Test whether FILE specifies a location on a remote system.
  10276. Return non-nil if the location is indeed remote.
  10277. For example, the filename \"/user@host:/foo\" specifies a location
  10278. on the system \"/user@host:\"."
  10279. (cond ((fboundp 'file-remote-p)
  10280. (file-remote-p file))
  10281. ((fboundp 'tramp-handle-file-remote-p)
  10282. (tramp-handle-file-remote-p file))
  10283. ((and (boundp 'ange-ftp-name-format)
  10284. (string-match (car ange-ftp-name-format) file))
  10285. t)))
  10286. ;;;; Refiling
  10287. (defun org-get-org-file ()
  10288. "Read a filename, with default directory `org-directory'."
  10289. (let ((default (or org-default-notes-file remember-data-file)))
  10290. (read-file-name (format "File name [%s]: " default)
  10291. (file-name-as-directory org-directory)
  10292. default)))
  10293. (defun org-notes-order-reversed-p ()
  10294. "Check if the current file should receive notes in reversed order."
  10295. (cond
  10296. ((not org-reverse-note-order) nil)
  10297. ((eq t org-reverse-note-order) t)
  10298. ((not (listp org-reverse-note-order)) nil)
  10299. (t (catch 'exit
  10300. (dolist (entry org-reverse-note-order)
  10301. (when (string-match (car entry) buffer-file-name)
  10302. (throw 'exit (cdr entry))))))))
  10303. (defvar org-refile-target-table nil
  10304. "The list of refile targets, created by `org-refile'.")
  10305. (defvar org-agenda-new-buffers nil
  10306. "Buffers created to visit agenda files.")
  10307. (defvar org-refile-cache nil
  10308. "Cache for refile targets.")
  10309. (defvar org-refile-markers nil
  10310. "All the markers used for caching refile locations.")
  10311. (defun org-refile-marker (pos)
  10312. "Get a new refile marker, but only if caching is in use."
  10313. (if (not org-refile-use-cache)
  10314. pos
  10315. (let ((m (make-marker)))
  10316. (move-marker m pos)
  10317. (push m org-refile-markers)
  10318. m)))
  10319. (defun org-refile-cache-clear ()
  10320. "Clear the refile cache and disable all the markers."
  10321. (dolist (m org-refile-markers) (move-marker m nil))
  10322. (setq org-refile-markers nil)
  10323. (setq org-refile-cache nil)
  10324. (message "Refile cache has been cleared"))
  10325. (defun org-refile-cache-check-set (set)
  10326. "Check if all the markers in the cache still have live buffers."
  10327. (let (marker)
  10328. (catch 'exit
  10329. (while (and set (setq marker (nth 3 (pop set))))
  10330. ;; If `org-refile-use-outline-path' is 'file, marker may be nil
  10331. (when (and marker (null (marker-buffer marker)))
  10332. (message "Please regenerate the refile cache with `C-0 C-c C-w'")
  10333. (sit-for 3)
  10334. (throw 'exit nil)))
  10335. t)))
  10336. (defun org-refile-cache-put (set &rest identifiers)
  10337. "Push the refile targets SET into the cache, under IDENTIFIERS."
  10338. (let* ((key (sha1 (prin1-to-string identifiers)))
  10339. (entry (assoc key org-refile-cache)))
  10340. (if entry
  10341. (setcdr entry set)
  10342. (push (cons key set) org-refile-cache))))
  10343. (defun org-refile-cache-get (&rest identifiers)
  10344. "Retrieve the cached value for refile targets given by IDENTIFIERS."
  10345. (cond
  10346. ((not org-refile-cache) nil)
  10347. ((not org-refile-use-cache) (org-refile-cache-clear) nil)
  10348. (t
  10349. (let ((set (cdr (assoc (sha1 (prin1-to-string identifiers))
  10350. org-refile-cache))))
  10351. (and set (org-refile-cache-check-set set) set)))))
  10352. (defvar org-outline-path-cache nil
  10353. "Alist between buffer positions and outline paths.
  10354. It value is an alist (POSITION . PATH) where POSITION is the
  10355. buffer position at the beginning of an entry and PATH is a list
  10356. of strings describing the outline path for that entry, in reverse
  10357. order.")
  10358. (defun org-refile-get-targets (&optional default-buffer excluded-entries)
  10359. "Produce a table with refile targets."
  10360. (let ((case-fold-search nil)
  10361. ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
  10362. (entries (or org-refile-targets '((nil . (:level . 1)))))
  10363. targets tgs files desc descre)
  10364. (message "Getting targets...")
  10365. (with-current-buffer (or default-buffer (current-buffer))
  10366. (dolist (entry entries)
  10367. (setq files (car entry) desc (cdr entry))
  10368. (cond
  10369. ((null files) (setq files (list (current-buffer))))
  10370. ((eq files 'org-agenda-files)
  10371. (setq files (org-agenda-files 'unrestricted)))
  10372. ((and (symbolp files) (fboundp files))
  10373. (setq files (funcall files)))
  10374. ((and (symbolp files) (boundp files))
  10375. (setq files (symbol-value files))))
  10376. (when (stringp files) (setq files (list files)))
  10377. (cond
  10378. ((eq (car desc) :tag)
  10379. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  10380. ((eq (car desc) :todo)
  10381. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  10382. ((eq (car desc) :regexp)
  10383. (setq descre (cdr desc)))
  10384. ((eq (car desc) :level)
  10385. (setq descre (concat "^\\*\\{" (number-to-string
  10386. (if org-odd-levels-only
  10387. (1- (* 2 (cdr desc)))
  10388. (cdr desc)))
  10389. "\\}[ \t]")))
  10390. ((eq (car desc) :maxlevel)
  10391. (setq descre (concat "^\\*\\{1," (number-to-string
  10392. (if org-odd-levels-only
  10393. (1- (* 2 (cdr desc)))
  10394. (cdr desc)))
  10395. "\\}[ \t]")))
  10396. (t (error "Bad refiling target description %s" desc)))
  10397. (dolist (f files)
  10398. (with-current-buffer (if (bufferp f) f (org-get-agenda-file-buffer f))
  10399. (or
  10400. (setq tgs (org-refile-cache-get (buffer-file-name) descre))
  10401. (progn
  10402. (when (bufferp f)
  10403. (setq f (buffer-file-name (buffer-base-buffer f))))
  10404. (setq f (and f (expand-file-name f)))
  10405. (when (eq org-refile-use-outline-path 'file)
  10406. (push (list (file-name-nondirectory f) f nil nil) tgs))
  10407. (org-with-wide-buffer
  10408. (goto-char (point-min))
  10409. (setq org-outline-path-cache nil)
  10410. (while (re-search-forward descre nil t)
  10411. (beginning-of-line)
  10412. (looking-at org-complex-heading-regexp)
  10413. (let ((begin (point))
  10414. (heading (org-match-string-no-properties 4)))
  10415. (unless (or (and
  10416. org-refile-target-verify-function
  10417. (not
  10418. (funcall org-refile-target-verify-function)))
  10419. (not heading)
  10420. (member heading excluded-entries))
  10421. (let ((re (format org-complex-heading-regexp-format
  10422. (regexp-quote heading)))
  10423. (target
  10424. (if (not org-refile-use-outline-path) heading
  10425. (mapconcat
  10426. #'org-protect-slash
  10427. (append
  10428. (pcase org-refile-use-outline-path
  10429. (`file (list (file-name-nondirectory
  10430. (buffer-file-name
  10431. (buffer-base-buffer)))))
  10432. (`full-file-path
  10433. (list (buffer-file-name
  10434. (buffer-base-buffer))))
  10435. (_ nil))
  10436. (org-get-outline-path t t))
  10437. "/"))))
  10438. (push (list target f re (org-refile-marker (point)))
  10439. tgs)))
  10440. (when (= (point) begin)
  10441. ;; Verification function has not moved point.
  10442. (end-of-line)))))))
  10443. (when org-refile-use-cache
  10444. (org-refile-cache-put tgs (buffer-file-name) descre))
  10445. (setq targets (append tgs targets))))))
  10446. (message "Getting targets...done")
  10447. (nreverse targets)))
  10448. (defun org-protect-slash (s)
  10449. (while (string-match "/" s)
  10450. (setq s (replace-match "\\" t t s)))
  10451. s)
  10452. (defun org--get-outline-path-1 (&optional use-cache)
  10453. "Return outline path to current headline.
  10454. Outline path is a list of strings, in reverse order. When
  10455. optional argument USE-CACHE is non-nil, make use of a cache. See
  10456. `org-get-outline-path' for details.
  10457. Assume buffer is widened and point is on a headline."
  10458. (or (and use-cache (cdr (assq (point) org-outline-path-cache)))
  10459. (let ((p (point))
  10460. (heading (progn
  10461. (looking-at org-complex-heading-regexp)
  10462. (if (not (match-end 4)) ""
  10463. ;; Remove statistics cookies.
  10464. (org-trim
  10465. (org-link-display-format
  10466. (replace-regexp-in-string
  10467. "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" ""
  10468. (org-match-string-no-properties 4))))))))
  10469. (if (org-up-heading-safe)
  10470. (let ((path (cons heading (org--get-outline-path-1 use-cache))))
  10471. (when use-cache
  10472. (push (cons p path) org-outline-path-cache))
  10473. path)
  10474. ;; This is a new root node. Since we assume we are moving
  10475. ;; forward, we can drop previous cache so as to limit number
  10476. ;; of associations there.
  10477. (let ((path (list heading)))
  10478. (when use-cache (setq org-outline-path-cache (list (cons p path))))
  10479. path)))))
  10480. (defun org-get-outline-path (&optional with-self use-cache)
  10481. "Return the outline path to the current entry.
  10482. An outline path is a list of ancestors for current headline, as
  10483. a list of strings. Statistics cookies are removed and links are
  10484. replaced with their description, if any, or their path otherwise.
  10485. When optional argument WITH-SELF is non-nil, the path also
  10486. includes the current headline.
  10487. When optional argument USE-CACHE is non-nil, cache outline paths
  10488. between calls to this function so as to avoid backtracking. This
  10489. argument is useful when planning to find more than one outline
  10490. path in the same document. In that case, there are two
  10491. conditions to satisfy:
  10492. - `org-outline-path-cache' is set to nil before starting the
  10493. process;
  10494. - outline paths are computed by increasing buffer positions."
  10495. (org-with-wide-buffer
  10496. (and (or (and with-self (org-back-to-heading t))
  10497. (org-up-heading-safe))
  10498. (reverse (org--get-outline-path-1 use-cache)))))
  10499. (defun org-format-outline-path (path &optional width prefix separator)
  10500. "Format the outline path PATH for display.
  10501. WIDTH is the maximum number of characters that is available.
  10502. PREFIX is a prefix to be included in the returned string,
  10503. such as the file name.
  10504. SEPARATOR is inserted between the different parts of the path,
  10505. the default is \"/\"."
  10506. (setq width (or width 79))
  10507. (setq path (delq nil path))
  10508. (unless (> width 0)
  10509. (user-error "Argument `width' must be positive"))
  10510. (setq separator (or separator "/"))
  10511. (let* ((org-odd-levels-only nil)
  10512. (fpath (concat
  10513. prefix (and prefix path separator)
  10514. (mapconcat
  10515. (lambda (s) (replace-regexp-in-string "[ \t]+\\'" "" s))
  10516. (cl-loop for head in path
  10517. for n from 0
  10518. collect (org-add-props
  10519. head nil 'face
  10520. (nth (% n org-n-level-faces) org-level-faces)))
  10521. separator))))
  10522. (when (> (length fpath) width)
  10523. (if (< width 7)
  10524. ;; It's unlikely that `width' will be this small, but don't
  10525. ;; waste characters by adding ".." if it is.
  10526. (setq fpath (substring fpath 0 width))
  10527. (setf (substring fpath (- width 2)) "..")))
  10528. fpath))
  10529. (defun org-display-outline-path (&optional file current separator just-return-string)
  10530. "Display the current outline path in the echo area.
  10531. If FILE is non-nil, prepend the output with the file name.
  10532. If CURRENT is non-nil, append the current heading to the output.
  10533. SEPARATOR is passed through to `org-format-outline-path'. It separates
  10534. the different parts of the path and defaults to \"/\".
  10535. If JUST-RETURN-STRING is non-nil, return a string, don't display a message."
  10536. (interactive "P")
  10537. (let* (case-fold-search
  10538. (bfn (buffer-file-name (buffer-base-buffer)))
  10539. (path (and (derived-mode-p 'org-mode) (org-get-outline-path)))
  10540. res)
  10541. (when current (setq path (append path
  10542. (save-excursion
  10543. (org-back-to-heading t)
  10544. (when (looking-at org-complex-heading-regexp)
  10545. (list (match-string 4)))))))
  10546. (setq res
  10547. (org-format-outline-path
  10548. path
  10549. (1- (frame-width))
  10550. (and file bfn (concat (file-name-nondirectory bfn) separator))
  10551. separator))
  10552. (if just-return-string
  10553. (org-no-properties res)
  10554. (org-unlogged-message "%s" res))))
  10555. (defvar org-refile-history nil
  10556. "History for refiling operations.")
  10557. (defvar org-after-refile-insert-hook nil
  10558. "Hook run after `org-refile' has inserted its stuff at the new location.
  10559. Note that this is still *before* the stuff will be removed from
  10560. the *old* location.")
  10561. (defvar org-capture-last-stored-marker)
  10562. (defvar org-refile-keep nil
  10563. "Non-nil means `org-refile' will copy instead of refile.")
  10564. (defun org-copy ()
  10565. "Like `org-refile', but copy."
  10566. (interactive)
  10567. (let ((org-refile-keep t))
  10568. (funcall 'org-refile nil nil nil "Copy")))
  10569. (defun org-refile (&optional arg default-buffer rfloc msg)
  10570. "Move the entry or entries at point to another heading.
  10571. The list of target headings is compiled using the information in
  10572. `org-refile-targets', which see.
  10573. At the target location, the entry is filed as a subitem of the
  10574. target heading. Depending on `org-reverse-note-order', the new
  10575. subitem will either be the first or the last subitem.
  10576. If there is an active region, all entries in that region will be
  10577. refiled. However, the region must fulfill the requirement that
  10578. the first heading sets the top-level of the moved text.
  10579. With prefix arg ARG, the command will only visit the target
  10580. location and not actually move anything.
  10581. With a double prefix arg \\[universal-argument] \\[universal-argument], go to the location where the last
  10582. refiling operation has put the subtree.
  10583. With a numeric prefix argument of `2', refile to the running clock.
  10584. With a numeric prefix argument of `3', emulate `org-refile-keep'
  10585. being set to t and copy to the target location, don't move it.
  10586. Beware that keeping refiled entries may result in duplicated ID
  10587. properties.
  10588. RFLOC can be a refile location obtained in a different way.
  10589. MSG is a string to replace \"Refile\" in the default prompt with
  10590. another verb. E.g. `org-copy' sets this parameter to \"Copy\".
  10591. See also `org-refile-use-outline-path'.
  10592. If you are using target caching (see `org-refile-use-cache'), you
  10593. have to clear the target cache in order to find new targets.
  10594. This can be done with a 0 prefix (`C-0 C-c C-w') or a triple
  10595. prefix argument (`C-u C-u C-u C-c C-w')."
  10596. (interactive "P")
  10597. (if (member arg '(0 (64)))
  10598. (org-refile-cache-clear)
  10599. (let* ((actionmsg (cond (msg msg)
  10600. ((equal arg 3) "Refile (and keep)")
  10601. (t "Refile")))
  10602. (regionp (org-region-active-p))
  10603. (region-start (and regionp (region-beginning)))
  10604. (region-end (and regionp (region-end)))
  10605. (org-refile-keep (if (equal arg 3) t org-refile-keep))
  10606. pos it nbuf file level reversed)
  10607. (setq last-command nil)
  10608. (when regionp
  10609. (goto-char region-start)
  10610. (or (bolp) (goto-char (point-at-bol)))
  10611. (setq region-start (point))
  10612. (unless (or (org-kill-is-subtree-p
  10613. (buffer-substring region-start region-end))
  10614. (prog1 org-refile-active-region-within-subtree
  10615. (let ((s (point-at-eol)))
  10616. (org-toggle-heading)
  10617. (setq region-end (+ (- (point-at-eol) s) region-end)))))
  10618. (user-error "The region is not a (sequence of) subtree(s)")))
  10619. (if (equal arg '(16))
  10620. (org-refile-goto-last-stored)
  10621. (when (or
  10622. (and (equal arg 2)
  10623. org-clock-hd-marker (marker-buffer org-clock-hd-marker)
  10624. (prog1
  10625. (setq it (list (or org-clock-heading "running clock")
  10626. (buffer-file-name
  10627. (marker-buffer org-clock-hd-marker))
  10628. ""
  10629. (marker-position org-clock-hd-marker)))
  10630. (setq arg nil)))
  10631. (setq it (or rfloc
  10632. (let (heading-text)
  10633. (save-excursion
  10634. (unless (and arg (listp arg))
  10635. (org-back-to-heading t)
  10636. (setq heading-text
  10637. (replace-regexp-in-string
  10638. org-bracket-link-regexp
  10639. "\\3"
  10640. (nth 4 (org-heading-components)))))
  10641. (org-refile-get-location
  10642. (cond ((and arg (listp arg)) "Goto")
  10643. (regionp (concat actionmsg " region to"))
  10644. (t (concat actionmsg " subtree \""
  10645. heading-text "\" to")))
  10646. default-buffer
  10647. (and (not (equal '(4) arg))
  10648. org-refile-allow-creating-parent-nodes)
  10649. arg))))))
  10650. (setq file (nth 1 it)
  10651. pos (nth 3 it))
  10652. (when (and (not arg)
  10653. pos
  10654. (equal (buffer-file-name) file)
  10655. (if regionp
  10656. (and (>= pos region-start)
  10657. (<= pos region-end))
  10658. (and (>= pos (point))
  10659. (< pos (save-excursion
  10660. (org-end-of-subtree t t))))))
  10661. (error "Cannot refile to position inside the tree or region"))
  10662. (setq nbuf (or (find-buffer-visiting file)
  10663. (find-file-noselect file)))
  10664. (if (and arg (not (equal arg 3)))
  10665. (progn
  10666. (org-pop-to-buffer-same-window nbuf)
  10667. (goto-char pos)
  10668. (org-show-context 'org-goto))
  10669. (if regionp
  10670. (progn
  10671. (org-kill-new (buffer-substring region-start region-end))
  10672. (org-save-markers-in-region region-start region-end))
  10673. (org-copy-subtree 1 nil t))
  10674. (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
  10675. (find-file-noselect file)))
  10676. (setq reversed (org-notes-order-reversed-p))
  10677. (save-excursion
  10678. (save-restriction
  10679. (widen)
  10680. (if pos
  10681. (progn
  10682. (goto-char pos)
  10683. (looking-at org-outline-regexp)
  10684. (setq level (org-get-valid-level (funcall outline-level) 1))
  10685. (goto-char
  10686. (if reversed
  10687. (or (outline-next-heading) (point-max))
  10688. (or (save-excursion (org-get-next-sibling))
  10689. (org-end-of-subtree t t)
  10690. (point-max)))))
  10691. (setq level 1)
  10692. (if (not reversed)
  10693. (goto-char (point-max))
  10694. (goto-char (point-min))
  10695. (or (outline-next-heading) (goto-char (point-max)))))
  10696. (unless (bolp) (newline))
  10697. (org-paste-subtree level nil nil t)
  10698. (when org-log-refile
  10699. (org-add-log-setup 'refile nil nil org-log-refile)
  10700. (unless (eq org-log-refile 'note)
  10701. (save-excursion (org-add-log-note))))
  10702. (and org-auto-align-tags
  10703. (let ((org-loop-over-headlines-in-active-region nil))
  10704. (org-set-tags nil t)))
  10705. (let ((bookmark-name (plist-get org-bookmark-names-plist
  10706. :last-refile)))
  10707. (when bookmark-name
  10708. (with-demoted-errors
  10709. (bookmark-set bookmark-name))))
  10710. ;; If we are refiling for capture, make sure that the
  10711. ;; last-capture pointers point here
  10712. (when (org-bound-and-true-p org-capture-is-refiling)
  10713. (let ((bookmark-name (plist-get org-bookmark-names-plist
  10714. :last-capture-marker)))
  10715. (when bookmark-name
  10716. (with-demoted-errors
  10717. (bookmark-set bookmark-name))))
  10718. (move-marker org-capture-last-stored-marker (point)))
  10719. (when (fboundp 'deactivate-mark) (deactivate-mark))
  10720. (run-hooks 'org-after-refile-insert-hook))))
  10721. (unless org-refile-keep
  10722. (if regionp
  10723. (delete-region (point) (+ (point) (- region-end region-start)))
  10724. (delete-region
  10725. (and (org-back-to-heading t) (point))
  10726. (min (1+ (buffer-size)) (org-end-of-subtree t t) (point)))))
  10727. (when (featurep 'org-inlinetask)
  10728. (org-inlinetask-remove-END-maybe))
  10729. (setq org-markers-to-move nil)
  10730. (message (concat actionmsg " to \"%s\" in file %s: done") (car it) file)))))))
  10731. (defun org-refile-goto-last-stored ()
  10732. "Go to the location where the last refile was stored."
  10733. (interactive)
  10734. (bookmark-jump (plist-get org-bookmark-names-plist :last-refile))
  10735. (message "This is the location of the last refile"))
  10736. (defun org-refile--get-location (refloc tbl)
  10737. "When user refile to REFLOC, find the associated target in TBL.
  10738. Also check `org-refile-target-table'."
  10739. (car (delq
  10740. nil
  10741. (mapcar
  10742. (lambda (r) (or (assoc r tbl)
  10743. (assoc r org-refile-target-table)))
  10744. (list (replace-regexp-in-string "/$" "" refloc)
  10745. (replace-regexp-in-string "\\([^/]\\)$" "\\1/" refloc))))))
  10746. (defun org-refile-get-location (&optional prompt default-buffer new-nodes
  10747. no-exclude)
  10748. "Prompt the user for a refile location, using PROMPT.
  10749. PROMPT should not be suffixed with a colon and a space, because
  10750. this function appends the default value from
  10751. `org-refile-history' automatically, if that is not empty.
  10752. When NO-EXCLUDE is set, do not exclude headlines in the current subtree,
  10753. this is used for the GOTO interface."
  10754. (let ((org-refile-targets org-refile-targets)
  10755. (org-refile-use-outline-path org-refile-use-outline-path)
  10756. excluded-entries)
  10757. (when (and (derived-mode-p 'org-mode)
  10758. (not org-refile-use-cache)
  10759. (not no-exclude))
  10760. (org-map-tree
  10761. (lambda()
  10762. (setq excluded-entries
  10763. (append excluded-entries (list (org-get-heading t t)))))))
  10764. (setq org-refile-target-table
  10765. (org-refile-get-targets default-buffer excluded-entries)))
  10766. (unless org-refile-target-table
  10767. (user-error "No refile targets"))
  10768. (let* ((cbuf (current-buffer))
  10769. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  10770. (cfunc (if (and org-refile-use-outline-path
  10771. org-outline-path-complete-in-steps)
  10772. #'org-olpath-completing-read
  10773. #'completing-read))
  10774. (extra (if org-refile-use-outline-path "/" ""))
  10775. (cbnex (concat (buffer-name) extra))
  10776. (filename (and cfn (expand-file-name cfn)))
  10777. (tbl (mapcar
  10778. (lambda (x)
  10779. (if (and (not (member org-refile-use-outline-path
  10780. '(file full-file-path)))
  10781. (not (equal filename (nth 1 x))))
  10782. (cons (concat (car x) extra " ("
  10783. (file-name-nondirectory (nth 1 x)) ")")
  10784. (cdr x))
  10785. (cons (concat (car x) extra) (cdr x))))
  10786. org-refile-target-table))
  10787. (completion-ignore-case t)
  10788. cdef
  10789. (prompt (concat prompt
  10790. (or (and (car org-refile-history)
  10791. (concat " (default " (car org-refile-history) ")"))
  10792. (and (assoc cbnex tbl) (setq cdef cbnex)
  10793. (concat " (default " cbnex ")"))) ": "))
  10794. pa answ parent-target child parent old-hist)
  10795. (setq old-hist org-refile-history)
  10796. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  10797. nil 'org-refile-history (or cdef (car org-refile-history))))
  10798. (if (setq pa (org-refile--get-location answ tbl))
  10799. (progn
  10800. (org-refile-check-position pa)
  10801. (when (or (not org-refile-history)
  10802. (not (eq old-hist org-refile-history))
  10803. (not (equal (car pa) (car org-refile-history))))
  10804. (setq org-refile-history
  10805. (cons (car pa) (if (assoc (car org-refile-history) tbl)
  10806. org-refile-history
  10807. (cdr org-refile-history))))
  10808. (when (equal (car org-refile-history) (nth 1 org-refile-history))
  10809. (pop org-refile-history)))
  10810. pa)
  10811. (if (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  10812. (progn
  10813. (setq parent (match-string 1 answ)
  10814. child (match-string 2 answ))
  10815. (setq parent-target (org-refile--get-location parent tbl))
  10816. (when (and parent-target
  10817. (or (eq new-nodes t)
  10818. (and (eq new-nodes 'confirm)
  10819. (y-or-n-p (format "Create new node \"%s\"? "
  10820. child)))))
  10821. (org-refile-new-child parent-target child)))
  10822. (user-error "Invalid target location")))))
  10823. (declare-function org-string-nw-p "org-macs" (s))
  10824. (defun org-refile-check-position (refile-pointer)
  10825. "Check if the refile pointer matches the headline to which it points."
  10826. (let* ((file (nth 1 refile-pointer))
  10827. (re (nth 2 refile-pointer))
  10828. (pos (nth 3 refile-pointer))
  10829. buffer)
  10830. (if (and (not (markerp pos)) (not file))
  10831. (user-error "Please indicate a target file in the refile path")
  10832. (when (org-string-nw-p re)
  10833. (setq buffer (if (markerp pos)
  10834. (marker-buffer pos)
  10835. (or (find-buffer-visiting file)
  10836. (find-file-noselect file))))
  10837. (with-current-buffer buffer
  10838. (save-excursion
  10839. (save-restriction
  10840. (widen)
  10841. (goto-char pos)
  10842. (beginning-of-line 1)
  10843. (unless (org-looking-at-p re)
  10844. (user-error "Invalid refile position, please clear the cache with `C-0 C-c C-w' before refiling")))))))))
  10845. (defun org-refile-new-child (parent-target child)
  10846. "Use refile target PARENT-TARGET to add new CHILD below it."
  10847. (unless parent-target
  10848. (error "Cannot find parent for new node"))
  10849. (let ((file (nth 1 parent-target))
  10850. (pos (nth 3 parent-target))
  10851. level)
  10852. (with-current-buffer (or (find-buffer-visiting file)
  10853. (find-file-noselect file))
  10854. (save-excursion
  10855. (save-restriction
  10856. (widen)
  10857. (if pos
  10858. (goto-char pos)
  10859. (goto-char (point-max))
  10860. (unless (bolp) (newline)))
  10861. (when (looking-at org-outline-regexp)
  10862. (setq level (funcall outline-level))
  10863. (org-end-of-subtree t t))
  10864. (org-back-over-empty-lines)
  10865. (insert "\n" (make-string
  10866. (if pos (org-get-valid-level level 1) 1) ?*)
  10867. " " child "\n")
  10868. (beginning-of-line 0)
  10869. (list (concat (car parent-target) "/" child) file "" (point)))))))
  10870. (defun org-olpath-completing-read (prompt collection &rest args)
  10871. "Read an outline path like a file name."
  10872. (let ((thetable collection))
  10873. (apply #'completing-read
  10874. prompt
  10875. (lambda (string predicate &optional flag)
  10876. (cond
  10877. ((eq flag nil) (try-completion string thetable))
  10878. ((eq flag t)
  10879. (let ((l (length string)))
  10880. (mapcar (lambda (x)
  10881. (let ((r (substring x l))
  10882. (f (if (string-match " ([^)]*)$" x)
  10883. (match-string 0 x)
  10884. "")))
  10885. (if (string-match "/" r)
  10886. (concat string (substring r 0 (match-end 0)) f)
  10887. x)))
  10888. (all-completions string thetable predicate))))
  10889. ;; Exact match?
  10890. ((eq flag 'lambda) (assoc string thetable))))
  10891. args)))
  10892. ;;;; Dynamic blocks
  10893. (defun org-find-dblock (name)
  10894. "Find the first dynamic block with name NAME in the buffer.
  10895. If not found, stay at current position and return nil."
  10896. (let ((case-fold-search t) pos)
  10897. (save-excursion
  10898. (goto-char (point-min))
  10899. (setq pos (and (re-search-forward
  10900. (concat "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+" name "\\>") nil t)
  10901. (match-beginning 0))))
  10902. (when pos (goto-char pos))
  10903. pos))
  10904. (defun org-create-dblock (plist)
  10905. "Create a dynamic block section, with parameters taken from PLIST.
  10906. PLIST must contain a :name entry which is used as the name of the block."
  10907. (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
  10908. (end-of-line 1)
  10909. (newline))
  10910. (let ((col (current-column))
  10911. (name (plist-get plist :name)))
  10912. (insert "#+BEGIN: " name)
  10913. (while plist
  10914. (if (eq (car plist) :name)
  10915. (setq plist (cddr plist))
  10916. (insert " " (prin1-to-string (pop plist)))))
  10917. (insert "\n\n" (make-string col ?\ ) "#+END:\n")
  10918. (beginning-of-line -2)))
  10919. (defun org-prepare-dblock ()
  10920. "Prepare dynamic block for refresh.
  10921. This empties the block, puts the cursor at the insert position and returns
  10922. the property list including an extra property :name with the block name."
  10923. (unless (looking-at org-dblock-start-re)
  10924. (user-error "Not at a dynamic block"))
  10925. (let* ((begdel (1+ (match-end 0)))
  10926. (name (org-no-properties (match-string 1)))
  10927. (params (append (list :name name)
  10928. (read (concat "(" (match-string 3) ")")))))
  10929. (save-excursion
  10930. (beginning-of-line 1)
  10931. (skip-chars-forward " \t")
  10932. (setq params (plist-put params :indentation-column (current-column))))
  10933. (unless (re-search-forward org-dblock-end-re nil t)
  10934. (error "Dynamic block not terminated"))
  10935. (setq params
  10936. (append params
  10937. (list :content (buffer-substring
  10938. begdel (match-beginning 0)))))
  10939. (delete-region begdel (match-beginning 0))
  10940. (goto-char begdel)
  10941. (open-line 1)
  10942. params))
  10943. (defun org-map-dblocks (&optional command)
  10944. "Apply COMMAND to all dynamic blocks in the current buffer.
  10945. If COMMAND is not given, use `org-update-dblock'."
  10946. (let ((cmd (or command 'org-update-dblock)))
  10947. (save-excursion
  10948. (goto-char (point-min))
  10949. (while (re-search-forward org-dblock-start-re nil t)
  10950. (goto-char (match-beginning 0))
  10951. (save-excursion
  10952. (condition-case nil
  10953. (funcall cmd)
  10954. (error (message "Error during update of dynamic block"))))
  10955. (unless (re-search-forward org-dblock-end-re nil t)
  10956. (error "Dynamic block not terminated"))))))
  10957. (defun org-dblock-update (&optional arg)
  10958. "User command for updating dynamic blocks.
  10959. Update the dynamic block at point. With prefix ARG, update all dynamic
  10960. blocks in the buffer."
  10961. (interactive "P")
  10962. (if arg
  10963. (org-update-all-dblocks)
  10964. (or (looking-at org-dblock-start-re)
  10965. (org-beginning-of-dblock))
  10966. (org-update-dblock)))
  10967. (defun org-update-dblock ()
  10968. "Update the dynamic block at point.
  10969. This means to empty the block, parse for parameters and then call
  10970. the correct writing function."
  10971. (interactive)
  10972. (save-excursion
  10973. (let* ((win (selected-window))
  10974. (pos (point))
  10975. (line (org-current-line))
  10976. (params (org-prepare-dblock))
  10977. (name (plist-get params :name))
  10978. (indent (plist-get params :indentation-column))
  10979. (cmd (intern (concat "org-dblock-write:" name))))
  10980. (message "Updating dynamic block `%s' at line %d..." name line)
  10981. (funcall cmd params)
  10982. (message "Updating dynamic block `%s' at line %d...done" name line)
  10983. (goto-char pos)
  10984. (when (and indent (> indent 0))
  10985. (setq indent (make-string indent ?\ ))
  10986. (save-excursion
  10987. (select-window win)
  10988. (org-beginning-of-dblock)
  10989. (forward-line 1)
  10990. (while (not (looking-at org-dblock-end-re))
  10991. (insert indent)
  10992. (beginning-of-line 2))
  10993. (when (looking-at org-dblock-end-re)
  10994. (and (looking-at "[ \t]+")
  10995. (replace-match ""))
  10996. (insert indent)))))))
  10997. (defun org-beginning-of-dblock ()
  10998. "Find the beginning of the dynamic block at point.
  10999. Error if there is no such block at point."
  11000. (let ((pos (point))
  11001. beg)
  11002. (end-of-line 1)
  11003. (if (and (re-search-backward org-dblock-start-re nil t)
  11004. (setq beg (match-beginning 0))
  11005. (re-search-forward org-dblock-end-re nil t)
  11006. (> (match-end 0) pos))
  11007. (goto-char beg)
  11008. (goto-char pos)
  11009. (error "Not in a dynamic block"))))
  11010. (defun org-update-all-dblocks ()
  11011. "Update all dynamic blocks in the buffer.
  11012. This function can be used in a hook."
  11013. (interactive)
  11014. (when (derived-mode-p 'org-mode)
  11015. (org-map-dblocks 'org-update-dblock)))
  11016. ;;;; Completion
  11017. (declare-function org-export-backend-name "org-export" (cl-x))
  11018. (declare-function org-export-backend-options "org-export" (cl-x))
  11019. (defun org-get-export-keywords ()
  11020. "Return a list of all currently understood export keywords.
  11021. Export keywords include options, block names, attributes and
  11022. keywords relative to each registered export back-end."
  11023. (let (keywords)
  11024. (dolist (backend
  11025. (org-bound-and-true-p org-export-registered-backends)
  11026. (delq nil keywords))
  11027. ;; Back-end name (for keywords, like #+LATEX:)
  11028. (push (upcase (symbol-name (org-export-backend-name backend))) keywords)
  11029. (dolist (option-entry (org-export-backend-options backend))
  11030. ;; Back-end options.
  11031. (push (nth 1 option-entry) keywords)))))
  11032. (defconst org-options-keywords
  11033. '("ARCHIVE:" "AUTHOR:" "BIND:" "CATEGORY:" "COLUMNS:" "CREATOR:" "DATE:"
  11034. "DESCRIPTION:" "DRAWERS:" "EMAIL:" "EXCLUDE_TAGS:" "FILETAGS:" "INCLUDE:"
  11035. "INDEX:" "KEYWORDS:" "LANGUAGE:" "MACRO:" "OPTIONS:" "PROPERTY:"
  11036. "PRIORITIES:" "SELECT_TAGS:" "SEQ_TODO:" "SETUPFILE:" "STARTUP:" "TAGS:"
  11037. "TITLE:" "TODO:" "TYP_TODO:" "SELECT_TAGS:" "EXCLUDE_TAGS:"))
  11038. (defcustom org-structure-template-alist
  11039. '(("s" "#+BEGIN_SRC ?\n\n#+END_SRC")
  11040. ("e" "#+BEGIN_EXAMPLE\n?\n#+END_EXAMPLE")
  11041. ("q" "#+BEGIN_QUOTE\n?\n#+END_QUOTE")
  11042. ("v" "#+BEGIN_VERSE\n?\n#+END_VERSE")
  11043. ("V" "#+BEGIN_VERBATIM\n?\n#+END_VERBATIM")
  11044. ("c" "#+BEGIN_CENTER\n?\n#+END_CENTER")
  11045. ("l" "#+BEGIN_EXPORT latex\n?\n#+END_EXPORT")
  11046. ("L" "#+LaTeX: ")
  11047. ("h" "#+BEGIN_EXPORT html\n?\n#+END_EXPORT")
  11048. ("H" "#+HTML: ")
  11049. ("a" "#+BEGIN_EXPORT ascii\n?\n#+END_EXPORT")
  11050. ("A" "#+ASCII: ")
  11051. ("i" "#+INDEX: ?")
  11052. ("I" "#+INCLUDE: %file ?"))
  11053. "Structure completion elements.
  11054. This is a list of abbreviation keys and values. The value gets inserted
  11055. if you type `<' followed by the key and then press the completion key,
  11056. usually `TAB'. %file will be replaced by a file name after prompting
  11057. for the file using completion. The cursor will be placed at the position
  11058. of the `?' in the template.
  11059. There are two templates for each key, the first uses the original Org syntax,
  11060. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  11061. the default when the /org-mtags.el/ module has been loaded. See also the
  11062. variable `org-mtags-prefer-muse-templates'."
  11063. :group 'org-completion
  11064. :type '(repeat
  11065. (list
  11066. (string :tag "Key")
  11067. (string :tag "Template")))
  11068. :version "25.1"
  11069. :package-version '(Org . "8.3"))
  11070. (defun org-try-structure-completion ()
  11071. "Try to complete a structure template before point.
  11072. This looks for strings like \"<e\" on an otherwise empty line and
  11073. expands them."
  11074. (let ((l (buffer-substring (point-at-bol) (point)))
  11075. a)
  11076. (when (and (looking-at "[ \t]*$")
  11077. (string-match "^[ \t]*<\\([a-zA-Z]+\\)$" l)
  11078. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  11079. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  11080. (match-beginning 1)) a)
  11081. t)))
  11082. (defun org-complete-expand-structure-template (start cell)
  11083. "Expand a structure template."
  11084. (let ((rpl (nth 1 cell))
  11085. (ind ""))
  11086. (delete-region start (point))
  11087. (when (string-match "\\`[ \t]*#\\+" rpl)
  11088. (cond
  11089. ((bolp))
  11090. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  11091. (setq ind (buffer-substring (point-at-bol) (point))))
  11092. (t (newline))))
  11093. (setq start (point))
  11094. (when (string-match "%file" rpl)
  11095. (setq rpl (replace-match
  11096. (concat
  11097. "\""
  11098. (save-match-data
  11099. (abbreviate-file-name (read-file-name "Include file: ")))
  11100. "\"")
  11101. t t rpl)))
  11102. (setq rpl (mapconcat 'identity (split-string rpl "\n")
  11103. (concat "\n" ind)))
  11104. (insert rpl)
  11105. (when (re-search-backward "\\?" start t) (delete-char 1))))
  11106. ;;;; TODO, DEADLINE, Comments
  11107. (defun org-toggle-comment ()
  11108. "Change the COMMENT state of an entry."
  11109. (interactive)
  11110. (save-excursion
  11111. (org-back-to-heading)
  11112. (looking-at org-complex-heading-regexp)
  11113. (goto-char (or (match-end 3) (match-end 2) (match-end 1)))
  11114. (skip-chars-forward " \t")
  11115. (unless (memq (char-before) '(?\s ?\t)) (insert " "))
  11116. (if (org-in-commented-heading-p t)
  11117. (delete-region (point)
  11118. (progn (search-forward " " (line-end-position) 'move)
  11119. (skip-chars-forward " \t")
  11120. (point)))
  11121. (insert org-comment-string)
  11122. (unless (eolp) (insert " ")))))
  11123. (defvar org-last-todo-state-is-todo nil
  11124. "This is non-nil when the last TODO state change led to a TODO state.
  11125. If the last change removed the TODO tag or switched to DONE, then
  11126. this is nil.")
  11127. (defvar org-setting-tags nil) ; dynamically skipped
  11128. (defvar org-todo-setup-filter-hook nil
  11129. "Hook for functions that pre-filter todo specs.
  11130. Each function takes a todo spec and returns either nil or the spec
  11131. transformed into canonical form." )
  11132. (defvar org-todo-get-default-hook nil
  11133. "Hook for functions that get a default item for todo.
  11134. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  11135. nil or a string to be used for the todo mark." )
  11136. (defvar org-agenda-headline-snapshot-before-repeat)
  11137. (defun org-current-effective-time ()
  11138. "Return current time adjusted for `org-extend-today-until' variable."
  11139. (let* ((ct (org-current-time))
  11140. (dct (decode-time ct))
  11141. (ct1
  11142. (cond
  11143. (org-use-last-clock-out-time-as-effective-time
  11144. (or (org-clock-get-last-clock-out-time) ct))
  11145. ((and org-use-effective-time (< (nth 2 dct) org-extend-today-until))
  11146. (encode-time 0 59 23 (1- (nth 3 dct)) (nth 4 dct) (nth 5 dct)))
  11147. (t ct))))
  11148. ct1))
  11149. (defun org-todo-yesterday (&optional arg)
  11150. "Like `org-todo' but the time of change will be 23:59 of yesterday."
  11151. (interactive "P")
  11152. (if (eq major-mode 'org-agenda-mode)
  11153. (apply 'org-agenda-todo-yesterday arg)
  11154. (let* ((org-use-effective-time t)
  11155. (hour (nth 2 (decode-time (org-current-time))))
  11156. (org-extend-today-until (1+ hour)))
  11157. (org-todo arg))))
  11158. (defvar org-block-entry-blocking ""
  11159. "First entry preventing the TODO state change.")
  11160. (defun org-cancel-repeater ()
  11161. "Cancel a repeater by setting its numeric value to zero."
  11162. (interactive)
  11163. (save-excursion
  11164. (org-back-to-heading t)
  11165. (let ((bound1 (point))
  11166. (bound0 (save-excursion (outline-next-heading) (point))))
  11167. (when (and (re-search-forward
  11168. (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  11169. org-deadline-time-regexp "\\)\\|\\("
  11170. org-ts-regexp "\\)")
  11171. bound0 t)
  11172. (re-search-backward "[ \t]+\\(?:[.+]\\)?\\+\\([0-9]+\\)[hdwmy]"
  11173. bound1 t))
  11174. (replace-match "0" t nil nil 1)))))
  11175. (defvar org-state) ;; dynamically scoped into this function
  11176. (defun org-todo (&optional arg)
  11177. "Change the TODO state of an item.
  11178. The state of an item is given by a keyword at the start of the heading,
  11179. like
  11180. *** TODO Write paper
  11181. *** DONE Call mom
  11182. The different keywords are specified in the variable `org-todo-keywords'.
  11183. By default the available states are \"TODO\" and \"DONE\".
  11184. So for this example: when the item starts with TODO, it is changed to DONE.
  11185. When it starts with DONE, the DONE is removed. And when neither TODO nor
  11186. DONE are present, add TODO at the beginning of the heading.
  11187. With \\[universal-argument] prefix arg, use completion to determine the new \
  11188. state.
  11189. With numeric prefix arg, switch to that state.
  11190. With a double \\[universal-argument] prefix, switch to the next set of TODO \
  11191. keywords (nextset).
  11192. With a triple \\[universal-argument] prefix, circumvent any state blocking.
  11193. With a numeric prefix arg of 0, inhibit note taking for the change.
  11194. With a numeric prefix arg of -1, cancel repeater to allow marking as DONE.
  11195. When called through ELisp, arg is also interpreted in the following way:
  11196. `none' -> empty state
  11197. \"\"(empty string) -> switch to empty state
  11198. `done' -> switch to DONE
  11199. `nextset' -> switch to the next set of keywords
  11200. `previousset' -> switch to the previous set of keywords
  11201. \"WAITING\" -> switch to the specified keyword, but only if it
  11202. really is a member of `org-todo-keywords'."
  11203. (interactive "P")
  11204. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  11205. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11206. 'region-start-level 'region))
  11207. org-loop-over-headlines-in-active-region)
  11208. (org-map-entries
  11209. `(org-todo ,arg)
  11210. org-loop-over-headlines-in-active-region
  11211. cl (when (outline-invisible-p) (org-end-of-subtree nil t))))
  11212. (when (equal arg '(16)) (setq arg 'nextset))
  11213. (when (equal arg -1) (org-cancel-repeater) (setq arg nil))
  11214. (let ((org-blocker-hook org-blocker-hook)
  11215. commentp
  11216. case-fold-search)
  11217. (when (equal arg '(64))
  11218. (setq arg nil org-blocker-hook nil))
  11219. (when (and org-blocker-hook
  11220. (or org-inhibit-blocking
  11221. (org-entry-get nil "NOBLOCKING")))
  11222. (setq org-blocker-hook nil))
  11223. (save-excursion
  11224. (catch 'exit
  11225. (org-back-to-heading t)
  11226. (when (org-in-commented-heading-p t)
  11227. (org-toggle-comment)
  11228. (setq commentp t))
  11229. (when (looking-at org-outline-regexp) (goto-char (1- (match-end 0))))
  11230. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|[ \t]*$\\)"))
  11231. (looking-at "\\(?: *\\|[ \t]*$\\)"))
  11232. (let* ((match-data (match-data))
  11233. (startpos (point-at-bol))
  11234. (logging (save-match-data (org-entry-get nil "LOGGING" t t)))
  11235. (org-log-done org-log-done)
  11236. (org-log-repeat org-log-repeat)
  11237. (org-todo-log-states org-todo-log-states)
  11238. (org-inhibit-logging
  11239. (if (equal arg 0)
  11240. (progn (setq arg nil) 'note) org-inhibit-logging))
  11241. (this (match-string 1))
  11242. (hl-pos (match-beginning 0))
  11243. (head (org-get-todo-sequence-head this))
  11244. (ass (assoc head org-todo-kwd-alist))
  11245. (interpret (nth 1 ass))
  11246. (done-word (nth 3 ass))
  11247. (final-done-word (nth 4 ass))
  11248. (org-last-state (or this ""))
  11249. (completion-ignore-case t)
  11250. (member (member this org-todo-keywords-1))
  11251. (tail (cdr member))
  11252. (org-state (cond
  11253. ((and org-todo-key-trigger
  11254. (or (and (equal arg '(4))
  11255. (eq org-use-fast-todo-selection 'prefix))
  11256. (and (not arg) org-use-fast-todo-selection
  11257. (not (eq org-use-fast-todo-selection
  11258. 'prefix)))))
  11259. ;; Use fast selection
  11260. (org-fast-todo-selection))
  11261. ((and (equal arg '(4))
  11262. (or (not org-use-fast-todo-selection)
  11263. (not org-todo-key-trigger)))
  11264. ;; Read a state with completion
  11265. (completing-read
  11266. "State: " (mapcar #'list org-todo-keywords-1)
  11267. nil t))
  11268. ((eq arg 'right)
  11269. (if this
  11270. (if tail (car tail) nil)
  11271. (car org-todo-keywords-1)))
  11272. ((eq arg 'left)
  11273. (unless (equal member org-todo-keywords-1)
  11274. (if this
  11275. (nth (- (length org-todo-keywords-1)
  11276. (length tail) 2)
  11277. org-todo-keywords-1)
  11278. (org-last org-todo-keywords-1))))
  11279. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  11280. (setq arg nil))) ; hack to fall back to cycling
  11281. (arg
  11282. ;; user or caller requests a specific state
  11283. (cond
  11284. ((equal arg "") nil)
  11285. ((eq arg 'none) nil)
  11286. ((eq arg 'done) (or done-word (car org-done-keywords)))
  11287. ((eq arg 'nextset)
  11288. (or (car (cdr (member head org-todo-heads)))
  11289. (car org-todo-heads)))
  11290. ((eq arg 'previousset)
  11291. (let ((org-todo-heads (reverse org-todo-heads)))
  11292. (or (car (cdr (member head org-todo-heads)))
  11293. (car org-todo-heads))))
  11294. ((car (member arg org-todo-keywords-1)))
  11295. ((stringp arg)
  11296. (user-error "State `%s' not valid in this file" arg))
  11297. ((nth (1- (prefix-numeric-value arg))
  11298. org-todo-keywords-1))))
  11299. ((null member) (or head (car org-todo-keywords-1)))
  11300. ((equal this final-done-word) nil) ;; -> make empty
  11301. ((null tail) nil) ;; -> first entry
  11302. ((memq interpret '(type priority))
  11303. (if (eq this-command last-command)
  11304. (car tail)
  11305. (if (> (length tail) 0)
  11306. (or done-word (car org-done-keywords))
  11307. nil)))
  11308. (t
  11309. (car tail))))
  11310. (org-state (or
  11311. (run-hook-with-args-until-success
  11312. 'org-todo-get-default-hook org-state org-last-state)
  11313. org-state))
  11314. (next (if org-state (concat " " org-state " ") " "))
  11315. (change-plist (list :type 'todo-state-change :from this :to org-state
  11316. :position startpos))
  11317. dolog now-done-p)
  11318. (when org-blocker-hook
  11319. (setq org-last-todo-state-is-todo
  11320. (not (member this org-done-keywords)))
  11321. (unless (save-excursion
  11322. (save-match-data
  11323. (org-with-wide-buffer
  11324. (run-hook-with-args-until-failure
  11325. 'org-blocker-hook change-plist))))
  11326. (if (org-called-interactively-p 'interactive)
  11327. (user-error "TODO state change from %s to %s blocked (by \"%s\")"
  11328. this org-state org-block-entry-blocking)
  11329. ;; fail silently
  11330. (message "TODO state change from %s to %s blocked (by \"%s\")"
  11331. this org-state org-block-entry-blocking)
  11332. (throw 'exit nil))))
  11333. (store-match-data match-data)
  11334. (replace-match next t t)
  11335. (cond ((equal this org-state)
  11336. (message "TODO state was already %s" (org-trim next)))
  11337. ((pos-visible-in-window-p hl-pos)
  11338. (message "TODO state changed to %s" (org-trim next))))
  11339. (unless head
  11340. (setq head (org-get-todo-sequence-head org-state)
  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. (when (memq arg '(nextset previousset))
  11346. (message "Keyword-Set %d/%d: %s"
  11347. (- (length org-todo-sets) -1
  11348. (length (memq (assoc org-state org-todo-sets) org-todo-sets)))
  11349. (length org-todo-sets)
  11350. (mapconcat 'identity (assoc org-state org-todo-sets) " ")))
  11351. (setq org-last-todo-state-is-todo
  11352. (not (member org-state org-done-keywords)))
  11353. (setq now-done-p (and (member org-state org-done-keywords)
  11354. (not (member this org-done-keywords))))
  11355. (and logging (org-local-logging logging))
  11356. (when (and (or org-todo-log-states org-log-done)
  11357. (not (eq org-inhibit-logging t))
  11358. (not (memq arg '(nextset previousset))))
  11359. ;; we need to look at recording a time and note
  11360. (setq dolog (or (nth 1 (assoc org-state org-todo-log-states))
  11361. (nth 2 (assoc this org-todo-log-states))))
  11362. (when (and (eq dolog 'note) (eq org-inhibit-logging 'note))
  11363. (setq dolog 'time))
  11364. (when (or (and (not org-state) (not org-closed-keep-when-no-todo))
  11365. (and org-state
  11366. (member org-state org-not-done-keywords)
  11367. (not (member this org-not-done-keywords))))
  11368. ;; This is now a todo state and was not one before
  11369. ;; If there was a CLOSED time stamp, get rid of it.
  11370. (org-add-planning-info nil nil 'closed))
  11371. (when (and now-done-p org-log-done)
  11372. ;; It is now done, and it was not done before
  11373. (org-add-planning-info 'closed (org-current-effective-time))
  11374. (when (and (not dolog) (eq 'note org-log-done))
  11375. (org-add-log-setup 'done org-state this 'note)))
  11376. (when (and org-state dolog)
  11377. ;; This is a non-nil state, and we need to log it
  11378. (org-add-log-setup 'state org-state this dolog)))
  11379. ;; Fixup tag positioning
  11380. (org-todo-trigger-tag-changes org-state)
  11381. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  11382. (when org-provide-todo-statistics
  11383. (org-update-parent-todo-statistics))
  11384. (run-hooks 'org-after-todo-state-change-hook)
  11385. (when (and arg (not (member org-state org-done-keywords)))
  11386. (setq head (org-get-todo-sequence-head org-state)))
  11387. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  11388. ;; Do we need to trigger a repeat?
  11389. (when now-done-p
  11390. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  11391. ;; This is for the agenda, take a snapshot of the headline.
  11392. (save-match-data
  11393. (setq org-agenda-headline-snapshot-before-repeat
  11394. (org-get-heading))))
  11395. (org-auto-repeat-maybe org-state))
  11396. ;; Fixup cursor location if close to the keyword
  11397. (when (and (outline-on-heading-p)
  11398. (not (bolp))
  11399. (save-excursion (beginning-of-line 1)
  11400. (looking-at org-todo-line-regexp))
  11401. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  11402. (goto-char (or (match-end 2) (match-end 1)))
  11403. (and (looking-at " ") (just-one-space)))
  11404. (when org-trigger-hook
  11405. (save-excursion
  11406. (run-hook-with-args 'org-trigger-hook change-plist)))
  11407. (when commentp (org-toggle-comment))))))))
  11408. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  11409. "Block turning an entry into a TODO, using the hierarchy.
  11410. This checks whether the current task should be blocked from state
  11411. changes. Such blocking occurs when:
  11412. 1. The task has children which are not all in a completed state.
  11413. 2. A task has a parent with the property :ORDERED:, and there
  11414. are siblings prior to the current task with incomplete
  11415. status.
  11416. 3. The parent of the task is blocked because it has siblings that should
  11417. be done first, or is child of a block grandparent TODO entry."
  11418. (if (not org-enforce-todo-dependencies)
  11419. t ; if locally turned off don't block
  11420. (catch 'dont-block
  11421. ;; If this is not a todo state change, or if this entry is already DONE,
  11422. ;; do not block
  11423. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  11424. (member (plist-get change-plist :from)
  11425. (cons 'done org-done-keywords))
  11426. (member (plist-get change-plist :to)
  11427. (cons 'todo org-not-done-keywords))
  11428. (not (plist-get change-plist :to)))
  11429. (throw 'dont-block t))
  11430. ;; If this task has children, and any are undone, it's blocked
  11431. (save-excursion
  11432. (org-back-to-heading t)
  11433. (let ((this-level (funcall outline-level)))
  11434. (outline-next-heading)
  11435. (let ((child-level (funcall outline-level)))
  11436. (while (and (not (eobp))
  11437. (> child-level this-level))
  11438. ;; this todo has children, check whether they are all
  11439. ;; completed
  11440. (when (and (not (org-entry-is-done-p))
  11441. (org-entry-is-todo-p))
  11442. (setq org-block-entry-blocking (org-get-heading))
  11443. (throw 'dont-block nil))
  11444. (outline-next-heading)
  11445. (setq child-level (funcall outline-level))))))
  11446. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  11447. ;; any previous siblings are undone, it's blocked
  11448. (save-excursion
  11449. (org-back-to-heading t)
  11450. (let* ((pos (point))
  11451. (parent-pos (and (org-up-heading-safe) (point))))
  11452. (unless parent-pos (throw 'dont-block t)) ; no parent
  11453. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  11454. (forward-line 1)
  11455. (re-search-forward org-not-done-heading-regexp pos t))
  11456. (setq org-block-entry-blocking (match-string 0))
  11457. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  11458. ;; Search further up the hierarchy, to see if an ancestor is blocked
  11459. (while t
  11460. (goto-char parent-pos)
  11461. (unless (looking-at org-not-done-heading-regexp)
  11462. (throw 'dont-block t)) ; do not block, parent is not a TODO
  11463. (setq pos (point))
  11464. (setq parent-pos (and (org-up-heading-safe) (point)))
  11465. (unless parent-pos (throw 'dont-block t)) ; no parent
  11466. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  11467. (forward-line 1)
  11468. (re-search-forward org-not-done-heading-regexp pos t)
  11469. (setq org-block-entry-blocking (org-get-heading)))
  11470. (throw 'dont-block nil)))))))) ; block, older sibling not done.
  11471. (defcustom org-track-ordered-property-with-tag nil
  11472. "Should the ORDERED property also be shown as a tag?
  11473. The ORDERED property decides if an entry should require subtasks to be
  11474. completed in sequence. Since a property is not very visible, setting
  11475. this option means that toggling the ORDERED property with the command
  11476. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  11477. not relevant for the behavior, but it makes things more visible.
  11478. Note that toggling the tag with tags commands will not change the property
  11479. and therefore not influence behavior!
  11480. This can be t, meaning the tag ORDERED should be used, It can also be a
  11481. string to select a different tag for this task."
  11482. :group 'org-todo
  11483. :type '(choice
  11484. (const :tag "No tracking" nil)
  11485. (const :tag "Track with ORDERED tag" t)
  11486. (string :tag "Use other tag")))
  11487. (defun org-toggle-ordered-property ()
  11488. "Toggle the ORDERED property of the current entry.
  11489. For better visibility, you can track the value of this property with a tag.
  11490. See variable `org-track-ordered-property-with-tag'."
  11491. (interactive)
  11492. (let* ((t1 org-track-ordered-property-with-tag)
  11493. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  11494. (save-excursion
  11495. (org-back-to-heading)
  11496. (if (org-entry-get nil "ORDERED")
  11497. (progn
  11498. (org-delete-property "ORDERED")
  11499. (and tag (org-toggle-tag tag 'off))
  11500. (message "Subtasks can be completed in arbitrary order"))
  11501. (org-entry-put nil "ORDERED" "t")
  11502. (and tag (org-toggle-tag tag 'on))
  11503. (message "Subtasks must be completed in sequence")))))
  11504. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  11505. (defun org-block-todo-from-checkboxes (change-plist)
  11506. "Block turning an entry into a TODO, using checkboxes.
  11507. This checks whether the current task should be blocked from state
  11508. changes because there are unchecked boxes in this entry."
  11509. (if (not org-enforce-todo-checkbox-dependencies)
  11510. t ; if locally turned off don't block
  11511. (catch 'dont-block
  11512. ;; If this is not a todo state change, or if this entry is already DONE,
  11513. ;; do not block
  11514. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  11515. (member (plist-get change-plist :from)
  11516. (cons 'done org-done-keywords))
  11517. (member (plist-get change-plist :to)
  11518. (cons 'todo org-not-done-keywords))
  11519. (not (plist-get change-plist :to)))
  11520. (throw 'dont-block t))
  11521. ;; If this task has checkboxes that are not checked, it's blocked
  11522. (save-excursion
  11523. (org-back-to-heading t)
  11524. (let ((beg (point)) end)
  11525. (outline-next-heading)
  11526. (setq end (point))
  11527. (goto-char beg)
  11528. (when (org-list-search-forward
  11529. (concat (org-item-beginning-re)
  11530. "\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?"
  11531. "\\[[- ]\\]")
  11532. end t)
  11533. (when (boundp 'org-blocked-by-checkboxes)
  11534. (setq org-blocked-by-checkboxes t))
  11535. (throw 'dont-block nil))))
  11536. t))) ; do not block
  11537. (defun org-entry-blocked-p ()
  11538. "Non-nil if entry at point is blocked."
  11539. (and (not (org-entry-get nil "NOBLOCKING"))
  11540. (member (org-entry-get nil "TODO") org-not-done-keywords)
  11541. (not (run-hook-with-args-until-failure
  11542. 'org-blocker-hook
  11543. (list :type 'todo-state-change
  11544. :position (point)
  11545. :from 'todo
  11546. :to 'done)))))
  11547. (defun org-update-statistics-cookies (all)
  11548. "Update the statistics cookie, either from TODO or from checkboxes.
  11549. This should be called with the cursor in a line with a statistics cookie."
  11550. (interactive "P")
  11551. (if all
  11552. (progn
  11553. (org-update-checkbox-count 'all)
  11554. (org-map-entries 'org-update-parent-todo-statistics))
  11555. (if (not (org-at-heading-p))
  11556. (org-update-checkbox-count)
  11557. (let ((pos (point-marker))
  11558. end l1 l2)
  11559. (ignore-errors (org-back-to-heading t))
  11560. (if (not (org-at-heading-p))
  11561. (org-update-checkbox-count)
  11562. (setq l1 (org-outline-level))
  11563. (setq end (save-excursion
  11564. (outline-next-heading)
  11565. (when (org-at-heading-p) (setq l2 (org-outline-level)))
  11566. (point)))
  11567. (if (and (save-excursion
  11568. (re-search-forward
  11569. "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) \\[[- X]\\]" end t))
  11570. (not (save-excursion (re-search-forward
  11571. ":COOKIE_DATA:.*\\<todo\\>" end t))))
  11572. (org-update-checkbox-count)
  11573. (if (and l2 (> l2 l1))
  11574. (progn
  11575. (goto-char end)
  11576. (org-update-parent-todo-statistics))
  11577. (goto-char pos)
  11578. (beginning-of-line 1)
  11579. (while (re-search-forward
  11580. "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)"
  11581. (point-at-eol) t)
  11582. (replace-match (if (match-end 2) "[100%]" "[0/0]") t t)))))
  11583. (goto-char pos)
  11584. (move-marker pos nil)))))
  11585. (defvar org-entry-property-inherited-from) ;; defined below
  11586. (defun org-update-parent-todo-statistics ()
  11587. "Update any statistics cookie in the parent of the current headline.
  11588. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  11589. the entire subtree and this will travel up the hierarchy and update
  11590. statistics everywhere."
  11591. (let* ((prop (save-excursion (org-up-heading-safe)
  11592. (org-entry-get nil "COOKIE_DATA" 'inherit)))
  11593. (recursive (or (not org-hierarchical-todo-statistics)
  11594. (and prop (string-match "\\<recursive\\>" prop))))
  11595. (lim (or (and prop (marker-position org-entry-property-inherited-from))
  11596. 0))
  11597. (first t)
  11598. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  11599. level ltoggle l1 new ndel
  11600. (cnt-all 0) (cnt-done 0) is-percent kwd
  11601. checkbox-beg ov ovs ove cookie-present)
  11602. (catch 'exit
  11603. (save-excursion
  11604. (beginning-of-line 1)
  11605. (setq ltoggle (funcall outline-level))
  11606. ;; Three situations are to consider:
  11607. ;; 1. if `org-hierarchical-todo-statistics' is nil, repeat up
  11608. ;; to the top-level ancestor on the headline;
  11609. ;; 2. If parent has "recursive" property, repeat up to the
  11610. ;; headline setting that property, taking inheritance into
  11611. ;; account;
  11612. ;; 3. Else, move up to direct parent and proceed only once.
  11613. (while (and (setq level (org-up-heading-safe))
  11614. (or recursive first)
  11615. (>= (point) lim))
  11616. (setq first nil cookie-present nil)
  11617. (unless (and level
  11618. (not (string-match
  11619. "\\<checkbox\\>"
  11620. (downcase (or (org-entry-get nil "COOKIE_DATA")
  11621. "")))))
  11622. (throw 'exit nil))
  11623. (while (re-search-forward box-re (point-at-eol) t)
  11624. (setq cnt-all 0 cnt-done 0 cookie-present t)
  11625. (setq is-percent (match-end 2) checkbox-beg (match-beginning 0))
  11626. (save-match-data
  11627. (unless (outline-next-heading) (throw 'exit nil))
  11628. (while (and (looking-at org-complex-heading-regexp)
  11629. (> (setq l1 (length (match-string 1))) level))
  11630. (setq kwd (and (or recursive (= l1 ltoggle))
  11631. (match-string 2)))
  11632. (if (or (eq org-provide-todo-statistics 'all-headlines)
  11633. (and (eq org-provide-todo-statistics t)
  11634. (or (member kwd org-done-keywords)))
  11635. (and (listp org-provide-todo-statistics)
  11636. (stringp (car org-provide-todo-statistics))
  11637. (or (member kwd org-provide-todo-statistics)
  11638. (member kwd org-done-keywords)))
  11639. (and (listp org-provide-todo-statistics)
  11640. (listp (car org-provide-todo-statistics))
  11641. (or (member kwd (car org-provide-todo-statistics))
  11642. (and (member kwd org-done-keywords)
  11643. (member kwd (cadr org-provide-todo-statistics))))))
  11644. (setq cnt-all (1+ cnt-all))
  11645. (and (eq org-provide-todo-statistics t)
  11646. kwd
  11647. (setq cnt-all (1+ cnt-all))))
  11648. (when (or (and (member org-provide-todo-statistics '(t all-headlines))
  11649. (member kwd org-done-keywords))
  11650. (and (listp org-provide-todo-statistics)
  11651. (listp (car org-provide-todo-statistics))
  11652. (member kwd org-done-keywords)
  11653. (member kwd (cadr org-provide-todo-statistics)))
  11654. (and (listp org-provide-todo-statistics)
  11655. (stringp (car org-provide-todo-statistics))
  11656. (member kwd org-done-keywords)))
  11657. (setq cnt-done (1+ cnt-done)))
  11658. (outline-next-heading)))
  11659. (setq new
  11660. (if is-percent
  11661. (format "[%d%%]" (floor (* 100.0 cnt-done)
  11662. (max 1 cnt-all)))
  11663. (format "[%d/%d]" cnt-done cnt-all))
  11664. ndel (- (match-end 0) checkbox-beg))
  11665. ;; handle overlays when updating cookie from column view
  11666. (when (setq ov (car (overlays-at checkbox-beg)))
  11667. (setq ovs (overlay-start ov) ove (overlay-end ov))
  11668. (delete-overlay ov))
  11669. (goto-char checkbox-beg)
  11670. (insert new)
  11671. (delete-region (point) (+ (point) ndel))
  11672. (when org-auto-align-tags (org-fix-tags-on-the-fly))
  11673. (when ov (move-overlay ov ovs ove)))
  11674. (when cookie-present
  11675. (run-hook-with-args 'org-after-todo-statistics-hook
  11676. cnt-done (- cnt-all cnt-done))))))
  11677. (run-hooks 'org-todo-statistics-hook)))
  11678. (defvar org-after-todo-statistics-hook nil
  11679. "Hook that is called after a TODO statistics cookie has been updated.
  11680. Each function is called with two arguments: the number of not-done entries
  11681. and the number of done entries.
  11682. For example, the following function, when added to this hook, will switch
  11683. an entry to DONE when all children are done, and back to TODO when new
  11684. entries are set to a TODO status. Note that this hook is only called
  11685. when there is a statistics cookie in the headline!
  11686. \(defun org-summary-todo (n-done n-not-done)
  11687. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  11688. \(let (org-log-done org-log-states) ; turn off logging
  11689. \(org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  11690. ")
  11691. (defvar org-todo-statistics-hook nil
  11692. "Hook that is run whenever Org thinks TODO statistics should be updated.
  11693. This hook runs even if there is no statistics cookie present, in which case
  11694. `org-after-todo-statistics-hook' would not run.")
  11695. (defun org-todo-trigger-tag-changes (state)
  11696. "Apply the changes defined in `org-todo-state-tags-triggers'."
  11697. (let ((l org-todo-state-tags-triggers)
  11698. changes)
  11699. (when (or (not state) (equal state ""))
  11700. (setq changes (append changes (cdr (assoc "" l)))))
  11701. (when (and (stringp state) (> (length state) 0))
  11702. (setq changes (append changes (cdr (assoc state l)))))
  11703. (when (member state org-not-done-keywords)
  11704. (setq changes (append changes (cdr (assoc 'todo l)))))
  11705. (when (member state org-done-keywords)
  11706. (setq changes (append changes (cdr (assoc 'done l)))))
  11707. (dolist (c changes)
  11708. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  11709. (defun org-local-logging (value)
  11710. "Get logging settings from a property VALUE."
  11711. ;; Directly set the variables, they are already local.
  11712. (setq org-log-done nil
  11713. org-log-repeat nil
  11714. org-todo-log-states nil)
  11715. (dolist (w (org-split-string value))
  11716. (let (a)
  11717. (cond
  11718. ((setq a (assoc w org-startup-options))
  11719. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  11720. (set (nth 1 a) (nth 2 a))))
  11721. ((setq a (org-extract-log-state-settings w))
  11722. (and (member (car a) org-todo-keywords-1)
  11723. (push a org-todo-log-states)))))))
  11724. (defun org-get-todo-sequence-head (kwd)
  11725. "Return the head of the TODO sequence to which KWD belongs.
  11726. If KWD is not set, check if there is a text property remembering the
  11727. right sequence."
  11728. (let (p)
  11729. (cond
  11730. ((not kwd)
  11731. (or (get-text-property (point-at-bol) 'org-todo-head)
  11732. (progn
  11733. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  11734. nil (point-at-eol)))
  11735. (get-text-property p 'org-todo-head))))
  11736. ((not (member kwd org-todo-keywords-1))
  11737. (car org-todo-keywords-1))
  11738. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  11739. (defun org-fast-todo-selection ()
  11740. "Fast TODO keyword selection with single keys.
  11741. Returns the new TODO keyword, or nil if no state change should occur."
  11742. (let* ((fulltable org-todo-key-alist)
  11743. (done-keywords org-done-keywords) ;; needed for the faces.
  11744. (maxlen (apply 'max (mapcar
  11745. (lambda (x)
  11746. (if (stringp (car x)) (string-width (car x)) 0))
  11747. fulltable)))
  11748. (expert nil)
  11749. (fwidth (+ maxlen 3 1 3))
  11750. (ncol (/ (- (window-width) 4) fwidth))
  11751. tg cnt e c tbl
  11752. groups ingroup)
  11753. (save-excursion
  11754. (save-window-excursion
  11755. (if expert
  11756. (set-buffer (get-buffer-create " *Org todo*"))
  11757. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  11758. (erase-buffer)
  11759. (setq-local org-done-keywords done-keywords)
  11760. (setq tbl fulltable cnt 0)
  11761. (while (setq e (pop tbl))
  11762. (cond
  11763. ((equal e '(:startgroup))
  11764. (push '() groups) (setq ingroup t)
  11765. (unless (= cnt 0)
  11766. (setq cnt 0)
  11767. (insert "\n"))
  11768. (insert "{ "))
  11769. ((equal e '(:endgroup))
  11770. (setq ingroup nil cnt 0)
  11771. (insert "}\n"))
  11772. ((equal e '(:newline))
  11773. (unless (= cnt 0)
  11774. (setq cnt 0)
  11775. (insert "\n")
  11776. (setq e (car tbl))
  11777. (while (equal (car tbl) '(:newline))
  11778. (insert "\n")
  11779. (setq tbl (cdr tbl)))))
  11780. (t
  11781. (setq tg (car e) c (cdr e))
  11782. (when ingroup (push tg (car groups)))
  11783. (setq tg (org-add-props tg nil 'face
  11784. (org-get-todo-face tg)))
  11785. (when (and (= cnt 0) (not ingroup)) (insert " "))
  11786. (insert "[" c "] " tg (make-string
  11787. (- fwidth 4 (length tg)) ?\ ))
  11788. (when (= (setq cnt (1+ cnt)) ncol)
  11789. (insert "\n")
  11790. (when ingroup (insert " "))
  11791. (setq cnt 0)))))
  11792. (insert "\n")
  11793. (goto-char (point-min))
  11794. (unless expert (org-fit-window-to-buffer))
  11795. (message "[a-z..]:Set [SPC]:clear")
  11796. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  11797. (cond
  11798. ((or (= c ?\C-g)
  11799. (and (= c ?q) (not (rassoc c fulltable))))
  11800. (setq quit-flag t))
  11801. ((= c ?\ ) nil)
  11802. ((setq e (rassoc c fulltable) tg (car e))
  11803. tg)
  11804. (t (setq quit-flag t)))))))
  11805. (defun org-entry-is-todo-p ()
  11806. (member (org-get-todo-state) org-not-done-keywords))
  11807. (defun org-entry-is-done-p ()
  11808. (member (org-get-todo-state) org-done-keywords))
  11809. (defun org-get-todo-state ()
  11810. "Return the TODO keyword of the current subtree."
  11811. (save-excursion
  11812. (org-back-to-heading t)
  11813. (and (looking-at org-todo-line-regexp)
  11814. (match-end 2)
  11815. (match-string 2))))
  11816. (defun org-at-date-range-p (&optional inactive-ok)
  11817. "Non-nil if point is inside a date range.
  11818. When optional argument INACTIVE-OK is non-nil, also consider
  11819. inactive time ranges.
  11820. When this function returns a non-nil value, match data is set
  11821. according to `org-tr-regexp-both' or `org-tr-regexp', depending
  11822. on INACTIVE-OK."
  11823. (interactive)
  11824. (save-excursion
  11825. (catch 'exit
  11826. (let ((pos (point)))
  11827. (skip-chars-backward "^[<\r\n")
  11828. (skip-chars-backward "<[")
  11829. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  11830. (>= (match-end 0) pos)
  11831. (throw 'exit t))
  11832. (skip-chars-backward "^<[\r\n")
  11833. (skip-chars-backward "<[")
  11834. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  11835. (>= (match-end 0) pos)
  11836. (throw 'exit t)))
  11837. nil)))
  11838. (defun org-get-repeat (&optional tagline)
  11839. "Check if there is a deadline/schedule with repeater in this entry."
  11840. (save-match-data
  11841. (save-excursion
  11842. (org-back-to-heading t)
  11843. (and (re-search-forward (if tagline
  11844. (concat tagline "\\s-*" org-repeat-re)
  11845. org-repeat-re)
  11846. (org-entry-end-position) t)
  11847. (match-string-no-properties 1)))))
  11848. (defvar org-last-changed-timestamp)
  11849. (defvar org-last-inserted-timestamp)
  11850. (defvar org-log-post-message)
  11851. (defvar org-log-note-purpose)
  11852. (defvar org-log-note-how nil)
  11853. (defvar org-log-note-extra)
  11854. (defun org-auto-repeat-maybe (done-word)
  11855. "Check if the current headline contains a repeated time-stamp.
  11856. If yes, set TODO state back to what it was and change the base date
  11857. of repeating deadline/scheduled time stamps to new date.
  11858. This function is run automatically after each state change to a DONE state."
  11859. (let* ((repeat (org-get-repeat))
  11860. (aa (assoc org-last-state org-todo-kwd-alist))
  11861. (interpret (nth 1 aa))
  11862. (head (nth 2 aa))
  11863. (whata '(("h" . hour) ("d" . day) ("m" . month) ("y" . year)))
  11864. (msg "Entry repeats: ")
  11865. (org-log-done nil)
  11866. (org-todo-log-states nil))
  11867. (when (and repeat (not (zerop (string-to-number (substring repeat 1)))))
  11868. (when (eq org-log-repeat t) (setq org-log-repeat 'state))
  11869. (let ((to-state (or (org-entry-get nil "REPEAT_TO_STATE" 'selective)
  11870. org-todo-repeat-to-state)))
  11871. (unless (and to-state (member to-state org-todo-keywords-1))
  11872. (setq to-state (if (eq interpret 'type) org-last-state head)))
  11873. (org-todo to-state))
  11874. (when (or org-log-repeat (org-entry-get nil "CLOCK"))
  11875. (org-entry-put nil "LAST_REPEAT" (format-time-string
  11876. (org-time-stamp-format t t))))
  11877. (when org-log-repeat
  11878. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  11879. (memq 'org-add-log-note post-command-hook))
  11880. ;; We are already setup for some record.
  11881. (when (eq org-log-repeat 'note)
  11882. ;; Make sure we take a note, not only a time stamp.
  11883. (setq org-log-note-how 'note))
  11884. ;; Set up for taking a record.
  11885. (org-add-log-setup 'state
  11886. (or done-word (car org-done-keywords))
  11887. org-last-state
  11888. org-log-repeat)))
  11889. (org-back-to-heading t)
  11890. (org-add-planning-info nil nil 'closed)
  11891. (let ((end (save-excursion (outline-next-heading) (point))))
  11892. (while (re-search-forward org-ts-regexp end t)
  11893. (when (save-match-data
  11894. (or (org-at-planning-p)
  11895. (org-at-property-p)
  11896. (eq (org-element-type (save-excursion
  11897. (backward-char)
  11898. (org-element-context)))
  11899. 'timestamp)))
  11900. (let ((type (cond ((match-end 1) org-scheduled-string)
  11901. ((match-end 3) org-deadline-string)
  11902. (t "Plain:")))
  11903. (ts (or (match-string 2) (match-string 4) (match-string 0))))
  11904. (cond
  11905. ((not
  11906. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts))
  11907. ;; Time-stamps without a repeater are usually skipped.
  11908. ;; However, a SCHEDULED time-stamp without one is
  11909. ;; removed, as it is considered as no longer relevant.
  11910. (when (equal type org-scheduled-string)
  11911. (org-remove-timestamp-with-keyword type)))
  11912. (t
  11913. (let ((n (string-to-number (match-string 2 ts)))
  11914. (what (match-string 3 ts)))
  11915. (when (equal what "w") (setq n (* n 7) what "d"))
  11916. (when (and (equal what "h")
  11917. (not (string-match-p "[0-9]\\{1,2\\}:[0-9]\\{2\\}"
  11918. ts)))
  11919. (user-error
  11920. "Cannot repeat in Repeat in %d hour(s) because no hour \
  11921. has been set"
  11922. n))
  11923. ;; Preparation, see if we need to modify the start
  11924. ;; date for the change.
  11925. (when (match-end 1)
  11926. (let ((time (save-match-data (org-time-string-to-time ts))))
  11927. (cond
  11928. ((equal (match-string 1 ts) ".")
  11929. ;; Shift starting date to today
  11930. (org-timestamp-change
  11931. (- (org-today) (time-to-days time))
  11932. 'day))
  11933. ((equal (match-string 1 ts) "+")
  11934. (let ((nshiftmax 10)
  11935. (nshift 0))
  11936. (while (or (= nshift 0)
  11937. (<= (time-to-days time)
  11938. (time-to-days (current-time))))
  11939. (when (= (cl-incf nshift) nshiftmax)
  11940. (or (y-or-n-p
  11941. (format "%d repeater intervals were not \
  11942. enough to shift date past today. Continue? "
  11943. nshift))
  11944. (user-error "Abort")))
  11945. (org-timestamp-change n (cdr (assoc what whata)))
  11946. (org-at-timestamp-p t)
  11947. (setq ts (match-string 1))
  11948. (setq time
  11949. (save-match-data
  11950. (org-time-string-to-time ts)))))
  11951. (org-timestamp-change (- n) (cdr (assoc what whata)))
  11952. ;; Rematch, so that we have everything in
  11953. ;; place for the real shift.
  11954. (org-at-timestamp-p t)
  11955. (setq ts (match-string 1))
  11956. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)"
  11957. ts)))))
  11958. (save-excursion
  11959. (org-timestamp-change n (cdr (assoc what whata)) nil t))
  11960. (setq msg
  11961. (concat
  11962. msg type " " org-last-changed-timestamp " ")))))))))
  11963. (setq org-log-post-message msg)
  11964. (message "%s" msg))))
  11965. (defun org-show-todo-tree (arg)
  11966. "Make a compact tree which shows all headlines marked with TODO.
  11967. The tree will show the lines where the regexp matches, and all higher
  11968. headlines above the match.
  11969. With a \\[universal-argument] prefix, prompt for a regexp to match.
  11970. With a numeric prefix N, construct a sparse tree for the Nth element
  11971. of `org-todo-keywords-1'."
  11972. (interactive "P")
  11973. (let ((case-fold-search nil)
  11974. (kwd-re
  11975. (cond ((null arg) org-not-done-regexp)
  11976. ((equal arg '(4))
  11977. (let ((kwd
  11978. (completing-read "Keyword (or KWD1|KWD2|...): "
  11979. (mapcar #'list org-todo-keywords-1))))
  11980. (concat "\\("
  11981. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  11982. "\\)\\>")))
  11983. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  11984. (regexp-quote (nth (1- (prefix-numeric-value arg))
  11985. org-todo-keywords-1)))
  11986. (t (user-error "Invalid prefix argument: %s" arg)))))
  11987. (message "%d TODO entries found"
  11988. (org-occur (concat "^" org-outline-regexp " *" kwd-re )))))
  11989. (defun org-deadline (arg &optional time)
  11990. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  11991. With one universal prefix argument, remove any deadline from the item.
  11992. With two universal prefix arguments, prompt for a warning delay.
  11993. With argument TIME, set the deadline at the corresponding date. TIME
  11994. can either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  11995. (interactive "P")
  11996. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  11997. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11998. 'region-start-level 'region))
  11999. org-loop-over-headlines-in-active-region)
  12000. (org-map-entries
  12001. `(org-deadline ',arg ,time)
  12002. org-loop-over-headlines-in-active-region
  12003. cl (when (outline-invisible-p) (org-end-of-subtree nil t))))
  12004. (let* ((old-date (org-entry-get nil "DEADLINE"))
  12005. (old-date-time (when old-date (org-time-string-to-time old-date)))
  12006. (repeater (and old-date
  12007. (string-match
  12008. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  12009. old-date)
  12010. (match-string 1 old-date))))
  12011. (cond
  12012. ((equal arg '(4))
  12013. (when (and old-date org-log-redeadline)
  12014. (org-add-log-setup 'deldeadline nil old-date org-log-redeadline))
  12015. (org-remove-timestamp-with-keyword org-deadline-string)
  12016. (message "Item no longer has a deadline."))
  12017. ((equal arg '(16))
  12018. (save-excursion
  12019. (org-back-to-heading t)
  12020. (if (re-search-forward
  12021. org-deadline-time-regexp
  12022. (save-excursion (outline-next-heading) (point)) t)
  12023. (let* ((rpl0 (match-string 1))
  12024. (rpl (replace-regexp-in-string " -[0-9]+[hdwmy]" "" rpl0)))
  12025. (replace-match
  12026. (concat org-deadline-string
  12027. " <" rpl
  12028. (format " -%dd"
  12029. (abs
  12030. (- (time-to-days
  12031. (save-match-data
  12032. (org-read-date nil t nil "Warn starting from" old-date-time)))
  12033. (time-to-days old-date-time))))
  12034. ">") t t))
  12035. (user-error "No deadline information to update"))))
  12036. (t
  12037. (org-add-planning-info 'deadline time 'closed)
  12038. (when (and old-date
  12039. org-log-redeadline
  12040. (not (equal old-date org-last-inserted-timestamp)))
  12041. (org-add-log-setup
  12042. 'redeadline org-last-inserted-timestamp old-date org-log-redeadline))
  12043. (when repeater
  12044. (save-excursion
  12045. (org-back-to-heading t)
  12046. (when (re-search-forward (concat org-deadline-string " "
  12047. org-last-inserted-timestamp)
  12048. (save-excursion
  12049. (outline-next-heading) (point)) t)
  12050. (goto-char (1- (match-end 0)))
  12051. (insert " " repeater)
  12052. (setq org-last-inserted-timestamp
  12053. (concat (substring org-last-inserted-timestamp 0 -1)
  12054. " " repeater
  12055. (substring org-last-inserted-timestamp -1))))))
  12056. (message "Deadline on %s" org-last-inserted-timestamp))))))
  12057. (defun org-schedule (arg &optional time)
  12058. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  12059. With one universal prefix argument, remove any scheduling date from the item.
  12060. With two universal prefix arguments, prompt for a delay cookie.
  12061. With argument TIME, scheduled at the corresponding date. TIME can
  12062. either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  12063. (interactive "P")
  12064. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  12065. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  12066. 'region-start-level 'region))
  12067. org-loop-over-headlines-in-active-region)
  12068. (org-map-entries
  12069. `(org-schedule ',arg ,time)
  12070. org-loop-over-headlines-in-active-region
  12071. cl (when (outline-invisible-p) (org-end-of-subtree nil t))))
  12072. (let* ((old-date (org-entry-get nil "SCHEDULED"))
  12073. (old-date-time (when old-date (org-time-string-to-time old-date)))
  12074. (repeater (and old-date
  12075. (string-match
  12076. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  12077. old-date)
  12078. (match-string 1 old-date))))
  12079. (cond
  12080. ((equal arg '(4))
  12081. (progn
  12082. (when (and old-date org-log-reschedule)
  12083. (org-add-log-setup 'delschedule nil old-date org-log-reschedule))
  12084. (org-remove-timestamp-with-keyword org-scheduled-string)
  12085. (message "Item is no longer scheduled.")))
  12086. ((equal arg '(16))
  12087. (save-excursion
  12088. (org-back-to-heading t)
  12089. (if (re-search-forward
  12090. org-scheduled-time-regexp
  12091. (save-excursion (outline-next-heading) (point)) t)
  12092. (let* ((rpl0 (match-string 1))
  12093. (rpl (replace-regexp-in-string " -[0-9]+[hdwmy]" "" rpl0)))
  12094. (replace-match
  12095. (concat org-scheduled-string
  12096. " <" rpl
  12097. (format " -%dd"
  12098. (abs
  12099. (- (time-to-days
  12100. (save-match-data
  12101. (org-read-date nil t nil "Delay until" old-date-time)))
  12102. (time-to-days old-date-time))))
  12103. ">") t t))
  12104. (user-error "No scheduled information to update"))))
  12105. (t
  12106. (org-add-planning-info 'scheduled time 'closed)
  12107. (when (and old-date
  12108. org-log-reschedule
  12109. (not (equal old-date org-last-inserted-timestamp)))
  12110. (org-add-log-setup
  12111. 'reschedule org-last-inserted-timestamp old-date org-log-reschedule))
  12112. (when repeater
  12113. (save-excursion
  12114. (org-back-to-heading t)
  12115. (when (re-search-forward (concat org-scheduled-string " "
  12116. org-last-inserted-timestamp)
  12117. (save-excursion
  12118. (outline-next-heading) (point)) t)
  12119. (goto-char (1- (match-end 0)))
  12120. (insert " " repeater)
  12121. (setq org-last-inserted-timestamp
  12122. (concat (substring org-last-inserted-timestamp 0 -1)
  12123. " " repeater
  12124. (substring org-last-inserted-timestamp -1))))))
  12125. (message "Scheduled to %s" org-last-inserted-timestamp))))))
  12126. (defun org-get-scheduled-time (pom &optional inherit)
  12127. "Get the scheduled time as a time tuple, of a format suitable
  12128. for calling org-schedule with, or if there is no scheduling,
  12129. returns nil."
  12130. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  12131. (when time
  12132. (apply 'encode-time (org-parse-time-string time)))))
  12133. (defun org-get-deadline-time (pom &optional inherit)
  12134. "Get the deadline as a time tuple, of a format suitable for
  12135. calling org-deadline with, or if there is no scheduling, returns
  12136. nil."
  12137. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  12138. (when time
  12139. (apply 'encode-time (org-parse-time-string time)))))
  12140. (defun org-remove-timestamp-with-keyword (keyword)
  12141. "Remove all time stamps with KEYWORD in the current entry."
  12142. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  12143. beg)
  12144. (save-excursion
  12145. (org-back-to-heading t)
  12146. (setq beg (point))
  12147. (outline-next-heading)
  12148. (while (re-search-backward re beg t)
  12149. (replace-match "")
  12150. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  12151. (equal (char-before) ?\ ))
  12152. (backward-delete-char 1)
  12153. (when (string-match "^[ \t]*$" (buffer-substring
  12154. (point-at-bol) (point-at-eol)))
  12155. (delete-region (point-at-bol)
  12156. (min (point-max) (1+ (point-at-eol))))))))))
  12157. (defvar org-time-was-given) ; dynamically scoped parameter
  12158. (defvar org-end-time-was-given) ; dynamically scoped parameter
  12159. (defun org-at-planning-p ()
  12160. "Non-nil when point is on a planning info line."
  12161. ;; This is as accurate and faster than `org-element-at-point' since
  12162. ;; planning info location is fixed in the section.
  12163. (org-with-wide-buffer
  12164. (beginning-of-line)
  12165. (and (org-looking-at-p org-planning-line-re)
  12166. (eq (point)
  12167. (ignore-errors
  12168. (if (and (featurep 'org-inlinetask) (org-inlinetask-in-task-p))
  12169. (org-back-to-heading t)
  12170. (org-with-limited-levels (org-back-to-heading t)))
  12171. (line-beginning-position 2))))))
  12172. (defun org-add-planning-info (what &optional time &rest remove)
  12173. "Insert new timestamp with keyword in the planning line.
  12174. WHAT indicates what kind of time stamp to add. It is a symbol
  12175. among `closed', `deadline', `scheduled' and nil. TIME indicates
  12176. the time to use. If none is given, the user is prompted for
  12177. a date. REMOVE indicates what kind of entries to remove. An old
  12178. WHAT entry will also be removed."
  12179. (let (org-time-was-given org-end-time-was-given default-time default-input)
  12180. (catch 'exit
  12181. (when (and (memq what '(scheduled deadline))
  12182. (or (not time)
  12183. (and (stringp time)
  12184. (string-match "^[-+]+[0-9]" time))))
  12185. ;; Try to get a default date/time from existing timestamp
  12186. (save-excursion
  12187. (org-back-to-heading t)
  12188. (let ((end (save-excursion (outline-next-heading) (point))) ts)
  12189. (when (re-search-forward (if (eq what 'scheduled)
  12190. org-scheduled-time-regexp
  12191. org-deadline-time-regexp)
  12192. end t)
  12193. (setq ts (match-string 1)
  12194. default-time (apply 'encode-time (org-parse-time-string ts))
  12195. default-input (and ts (org-get-compact-tod ts)))))))
  12196. (when what
  12197. (setq time
  12198. (if (stringp time)
  12199. ;; This is a string (relative or absolute), set
  12200. ;; proper date.
  12201. (apply #'encode-time
  12202. (org-read-date-analyze
  12203. time default-time (decode-time default-time)))
  12204. ;; If necessary, get the time from the user
  12205. (or time (org-read-date nil 'to-time nil nil
  12206. default-time default-input)))))
  12207. (org-with-wide-buffer
  12208. (org-back-to-heading t)
  12209. (forward-line)
  12210. (unless (bolp) (insert "\n"))
  12211. (cond ((org-looking-at-p org-planning-line-re)
  12212. ;; Move to current indentation.
  12213. (skip-chars-forward " \t")
  12214. ;; Check if we have to remove something.
  12215. (dolist (type (if what (cons what remove) remove))
  12216. (save-excursion
  12217. (when (re-search-forward
  12218. (cl-case type
  12219. (closed org-closed-time-regexp)
  12220. (deadline org-deadline-time-regexp)
  12221. (scheduled org-scheduled-time-regexp)
  12222. (otherwise
  12223. (error "Invalid planning type: %s" type)))
  12224. (line-end-position) t)
  12225. ;; Delete until next keyword or end of line.
  12226. (delete-region
  12227. (match-beginning 0)
  12228. (if (re-search-forward org-keyword-time-not-clock-regexp
  12229. (line-end-position)
  12230. t)
  12231. (match-beginning 0)
  12232. (line-end-position))))))
  12233. ;; If there is nothing more to add and no more keyword
  12234. ;; is left, remove the line completely.
  12235. (if (and (org-looking-at-p "[ \t]*$") (not what))
  12236. (delete-region (line-beginning-position)
  12237. (line-beginning-position 2))
  12238. ;; If we removed last keyword, do not leave trailing
  12239. ;; white space at the end of line.
  12240. (let ((p (point)))
  12241. (save-excursion
  12242. (end-of-line)
  12243. (unless (= (skip-chars-backward " \t" p) 0)
  12244. (delete-region (point) (line-end-position)))))))
  12245. ((not what) (throw 'exit nil)) ; Nothing to do.
  12246. (t (insert-before-markers "\n")
  12247. (backward-char 1)
  12248. (when org-adapt-indentation
  12249. (org-indent-to-column (1+ (org-outline-level))))))
  12250. (when what
  12251. ;; Insert planning keyword.
  12252. (insert (cl-case what
  12253. (closed org-closed-string)
  12254. (deadline org-deadline-string)
  12255. (scheduled org-scheduled-string)
  12256. (otherwise (error "Invalid planning type: %s" what)))
  12257. " ")
  12258. ;; Insert associated timestamp.
  12259. (let ((ts (org-insert-time-stamp
  12260. time
  12261. (or org-time-was-given
  12262. (and (eq what 'closed) org-log-done-with-time))
  12263. (eq what 'closed)
  12264. nil nil (list org-end-time-was-given))))
  12265. (unless (eolp) (insert " "))
  12266. ts))))))
  12267. (defvar org-log-note-marker (make-marker)
  12268. "Marker pointing at the entry where the note is to be inserted.")
  12269. (defvar org-log-note-purpose nil)
  12270. (defvar org-log-note-state nil)
  12271. (defvar org-log-note-previous-state nil)
  12272. (defvar org-log-note-extra nil)
  12273. (defvar org-log-note-window-configuration nil)
  12274. (defvar org-log-note-return-to (make-marker))
  12275. (defvar org-log-note-effective-time nil
  12276. "Remembered current time so that dynamically scoped
  12277. `org-extend-today-until' affects timestamps in state change log")
  12278. (defvar org-log-post-message nil
  12279. "Message to be displayed after a log note has been stored.
  12280. The auto-repeater uses this.")
  12281. (defun org-add-note ()
  12282. "Add a note to the current entry.
  12283. This is done in the same way as adding a state change note."
  12284. (interactive)
  12285. (org-add-log-setup 'note))
  12286. (defun org-log-beginning (&optional create)
  12287. "Return expected start of log notes in current entry.
  12288. When optional argument CREATE is non-nil, the function creates
  12289. a drawer to store notes, if necessary. Returned position ignores
  12290. narrowing."
  12291. (org-with-wide-buffer
  12292. (let ((drawer (org-log-into-drawer)))
  12293. (cond
  12294. (drawer
  12295. (org-end-of-meta-data)
  12296. (let ((regexp (concat "^[ \t]*:" (regexp-quote drawer) ":[ \t]*$"))
  12297. (end (if (org-at-heading-p) (point)
  12298. (save-excursion (outline-next-heading) (point))))
  12299. (case-fold-search t))
  12300. (catch 'exit
  12301. ;; Try to find existing drawer.
  12302. (while (re-search-forward regexp end t)
  12303. (let ((element (org-element-at-point)))
  12304. (when (eq (org-element-type element) 'drawer)
  12305. (let ((cend (org-element-property :contents-end element)))
  12306. (when (and (not org-log-states-order-reversed) cend)
  12307. (goto-char cend)))
  12308. (throw 'exit nil))))
  12309. ;; No drawer found. Create one, if permitted.
  12310. (when create
  12311. (unless (bolp) (insert "\n"))
  12312. (let ((beg (point)))
  12313. (insert ":" drawer ":\n:END:\n")
  12314. (org-indent-region beg (point)))
  12315. (end-of-line -1)))))
  12316. (t
  12317. (org-end-of-meta-data org-log-state-notes-insert-after-drawers)
  12318. (skip-chars-forward " \t\n")
  12319. (beginning-of-line)
  12320. (unless org-log-states-order-reversed
  12321. (org-skip-over-state-notes)
  12322. (skip-chars-backward " \t\n")
  12323. (forward-line)))))
  12324. (if (bolp) (point) (line-beginning-position 2))))
  12325. (defun org-add-log-setup (&optional purpose state prev-state how extra)
  12326. "Set up the post command hook to take a note.
  12327. If this is about to TODO state change, the new state is expected in STATE.
  12328. HOW is an indicator what kind of note should be created.
  12329. EXTRA is additional text that will be inserted into the notes buffer."
  12330. (move-marker org-log-note-marker (point))
  12331. (setq org-log-note-purpose purpose
  12332. org-log-note-state state
  12333. org-log-note-previous-state prev-state
  12334. org-log-note-how how
  12335. org-log-note-extra extra
  12336. org-log-note-effective-time (org-current-effective-time))
  12337. (add-hook 'post-command-hook 'org-add-log-note 'append))
  12338. (defun org-skip-over-state-notes ()
  12339. "Skip past the list of State notes in an entry."
  12340. (when (ignore-errors (goto-char (org-in-item-p)))
  12341. (let* ((struct (org-list-struct))
  12342. (prevs (org-list-prevs-alist struct))
  12343. (regexp
  12344. (concat "[ \t]*- +"
  12345. (replace-regexp-in-string
  12346. " +" " +"
  12347. (org-replace-escapes
  12348. (regexp-quote (cdr (assq 'state org-log-note-headings)))
  12349. `(("%d" . ,org-ts-regexp-inactive)
  12350. ("%D" . ,org-ts-regexp)
  12351. ("%s" . "\"\\S-+\"")
  12352. ("%S" . "\"\\S-+\"")
  12353. ("%t" . ,org-ts-regexp-inactive)
  12354. ("%T" . ,org-ts-regexp)
  12355. ("%u" . ".*?")
  12356. ("%U" . ".*?")))))))
  12357. (while (org-looking-at-p regexp)
  12358. (goto-char (or (org-list-get-next-item (point) struct prevs)
  12359. (org-list-get-item-end (point) struct)))))))
  12360. (defun org-add-log-note (&optional _purpose)
  12361. "Pop up a window for taking a note, and add this note later."
  12362. (remove-hook 'post-command-hook 'org-add-log-note)
  12363. (setq org-log-note-window-configuration (current-window-configuration))
  12364. (delete-other-windows)
  12365. (move-marker org-log-note-return-to (point))
  12366. (org-pop-to-buffer-same-window (marker-buffer org-log-note-marker))
  12367. (goto-char org-log-note-marker)
  12368. (org-switch-to-buffer-other-window "*Org Note*")
  12369. (erase-buffer)
  12370. (if (memq org-log-note-how '(time state))
  12371. (let (current-prefix-arg) (org-store-log-note))
  12372. (let ((org-inhibit-startup t)) (org-mode))
  12373. (insert (format "# Insert note for %s.
  12374. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  12375. (cond
  12376. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  12377. ((eq org-log-note-purpose 'done) "closed todo item")
  12378. ((eq org-log-note-purpose 'state)
  12379. (format "state change from \"%s\" to \"%s\""
  12380. (or org-log-note-previous-state "")
  12381. (or org-log-note-state "")))
  12382. ((eq org-log-note-purpose 'reschedule)
  12383. "rescheduling")
  12384. ((eq org-log-note-purpose 'delschedule)
  12385. "no longer scheduled")
  12386. ((eq org-log-note-purpose 'redeadline)
  12387. "changing deadline")
  12388. ((eq org-log-note-purpose 'deldeadline)
  12389. "removing deadline")
  12390. ((eq org-log-note-purpose 'refile)
  12391. "refiling")
  12392. ((eq org-log-note-purpose 'note)
  12393. "this entry")
  12394. (t (error "This should not happen")))))
  12395. (when org-log-note-extra (insert org-log-note-extra))
  12396. (setq-local org-finish-function 'org-store-log-note)
  12397. (run-hooks 'org-log-buffer-setup-hook)))
  12398. (defvar org-note-abort nil) ; dynamically scoped
  12399. (defun org-store-log-note ()
  12400. "Finish taking a log note, and insert it to where it belongs."
  12401. (let ((txt (prog1 (buffer-string)
  12402. (kill-buffer)))
  12403. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  12404. lines)
  12405. (while (string-match "\\`# .*\n[ \t\n]*" txt)
  12406. (setq txt (replace-match "" t t txt)))
  12407. (when (string-match "\\s-+\\'" txt)
  12408. (setq txt (replace-match "" t t txt)))
  12409. (setq lines (org-split-string txt "\n"))
  12410. (when (org-string-nw-p note)
  12411. (setq note
  12412. (org-replace-escapes
  12413. note
  12414. (list (cons "%u" (user-login-name))
  12415. (cons "%U" user-full-name)
  12416. (cons "%t" (format-time-string
  12417. (org-time-stamp-format 'long 'inactive)
  12418. org-log-note-effective-time))
  12419. (cons "%T" (format-time-string
  12420. (org-time-stamp-format 'long nil)
  12421. org-log-note-effective-time))
  12422. (cons "%d" (format-time-string
  12423. (org-time-stamp-format nil 'inactive)
  12424. org-log-note-effective-time))
  12425. (cons "%D" (format-time-string
  12426. (org-time-stamp-format nil nil)
  12427. org-log-note-effective-time))
  12428. (cons "%s" (cond
  12429. ((not org-log-note-state) "")
  12430. ((org-string-match-p org-ts-regexp
  12431. org-log-note-state)
  12432. (format "\"[%s]\""
  12433. (substring org-log-note-state 1 -1)))
  12434. (t (format "\"%s\"" org-log-note-state))))
  12435. (cons "%S"
  12436. (cond
  12437. ((not org-log-note-previous-state) "")
  12438. ((org-string-match-p org-ts-regexp
  12439. org-log-note-previous-state)
  12440. (format "\"[%s]\""
  12441. (substring
  12442. org-log-note-previous-state 1 -1)))
  12443. (t (format "\"%s\""
  12444. org-log-note-previous-state)))))))
  12445. (when lines (setq note (concat note " \\\\")))
  12446. (push note lines))
  12447. (when (and lines (not (or current-prefix-arg org-note-abort)))
  12448. (with-current-buffer (marker-buffer org-log-note-marker)
  12449. (org-with-wide-buffer
  12450. ;; Find location for the new note.
  12451. (goto-char org-log-note-marker)
  12452. (set-marker org-log-note-marker nil)
  12453. (goto-char (org-log-beginning t))
  12454. ;; Make sure point is at the beginning of an empty line.
  12455. (cond ((not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  12456. ((looking-at "[ \t]*\\S-") (save-excursion (insert "\n"))))
  12457. ;; In an existing list, add a new item at the top level.
  12458. ;; Otherwise, indent line like a regular one.
  12459. (let ((itemp (org-in-item-p)))
  12460. (if itemp
  12461. (org-indent-line-to
  12462. (let ((struct (save-excursion
  12463. (goto-char itemp) (org-list-struct))))
  12464. (org-list-get-ind (org-list-get-top-point struct) struct)))
  12465. (org-indent-line)))
  12466. (insert (org-list-bullet-string "-") (pop lines))
  12467. (let ((ind (org-list-item-body-column (line-beginning-position))))
  12468. (dolist (line lines)
  12469. (insert "\n")
  12470. (org-indent-line-to ind)
  12471. (insert line)))
  12472. (message "Note stored")
  12473. (org-back-to-heading t)
  12474. (org-cycle-hide-drawers 'children))
  12475. ;; Fix `buffer-undo-list' when `org-store-log-note' is called
  12476. ;; from within `org-add-log-note' because `buffer-undo-list'
  12477. ;; is then modified outside of `org-with-remote-undo'.
  12478. (when (eq this-command 'org-agenda-todo)
  12479. (setcdr buffer-undo-list (cddr buffer-undo-list))))))
  12480. ;; Don't add undo information when called from `org-agenda-todo'.
  12481. (let ((buffer-undo-list (eq this-command 'org-agenda-todo)))
  12482. (set-window-configuration org-log-note-window-configuration)
  12483. (with-current-buffer (marker-buffer org-log-note-return-to)
  12484. (goto-char org-log-note-return-to))
  12485. (move-marker org-log-note-return-to nil)
  12486. (when org-log-post-message (message "%s" org-log-post-message))))
  12487. (defun org-remove-empty-drawer-at (pos)
  12488. "Remove an empty drawer at position POS.
  12489. POS may also be a marker."
  12490. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  12491. (org-with-wide-buffer
  12492. (goto-char pos)
  12493. (let ((drawer (org-element-at-point)))
  12494. (when (and (memq (org-element-type drawer) '(drawer property-drawer))
  12495. (not (org-element-property :contents-begin drawer)))
  12496. (delete-region (org-element-property :begin drawer)
  12497. (progn (goto-char (org-element-property :end drawer))
  12498. (skip-chars-backward " \r\t\n")
  12499. (forward-line)
  12500. (point))))))))
  12501. (defvar org-ts-type nil)
  12502. (defun org-sparse-tree (&optional arg type)
  12503. "Create a sparse tree, prompt for the details.
  12504. This command can create sparse trees. You first need to select the type
  12505. of match used to create the tree:
  12506. t Show all TODO entries.
  12507. T Show entries with a specific TODO keyword.
  12508. m Show entries selected by a tags/property match.
  12509. p Enter a property name and its value (both with completion on existing
  12510. names/values) and show entries with that property.
  12511. r Show entries matching a regular expression (`/' can be used as well).
  12512. b Show deadlines and scheduled items before a date.
  12513. a Show deadlines and scheduled items after a date.
  12514. d Show deadlines due within `org-deadline-warning-days'.
  12515. D Show deadlines and scheduled items between a date range."
  12516. (interactive "P")
  12517. (setq type (or type org-sparse-tree-default-date-type))
  12518. (setq org-ts-type type)
  12519. (message "Sparse tree: [/]regexp [t]odo [T]odo-kwd [m]atch [p]roperty
  12520. \[d]eadlines [b]efore-date [a]fter-date [D]ates range
  12521. \[c]ycle through date types: %s"
  12522. (cl-case type
  12523. (all "all timestamps")
  12524. (scheduled "only scheduled")
  12525. (deadline "only deadline")
  12526. (active "only active timestamps")
  12527. (inactive "only inactive timestamps")
  12528. (closed "with a closed time-stamp")
  12529. (otherwise "scheduled/deadline")))
  12530. (let ((answer (read-char-exclusive)))
  12531. (cl-case answer
  12532. (?c
  12533. (org-sparse-tree
  12534. arg
  12535. (cadr
  12536. (memq type '(nil all scheduled deadline active inactive closed)))))
  12537. (?d (call-interactively 'org-check-deadlines))
  12538. (?b (call-interactively 'org-check-before-date))
  12539. (?a (call-interactively 'org-check-after-date))
  12540. (?D (call-interactively 'org-check-dates-range))
  12541. (?t (call-interactively 'org-show-todo-tree))
  12542. (?T (org-show-todo-tree '(4)))
  12543. (?m (call-interactively 'org-match-sparse-tree))
  12544. ((?p ?P)
  12545. (let* ((kwd (completing-read
  12546. "Property: " (mapcar #'list (org-buffer-property-keys))))
  12547. (value (completing-read
  12548. "Value: " (mapcar #'list (org-property-values kwd)))))
  12549. (unless (string-match "\\`{.*}\\'" value)
  12550. (setq value (concat "\"" value "\"")))
  12551. (org-match-sparse-tree arg (concat kwd "=" value))))
  12552. ((?r ?R ?/) (call-interactively 'org-occur))
  12553. (otherwise (user-error "No such sparse tree command \"%c\"" answer)))))
  12554. (defvar-local org-occur-highlights nil
  12555. "List of overlays used for occur matches.")
  12556. (defvar-local org-occur-parameters nil
  12557. "Parameters of the active org-occur calls.
  12558. This is a list, each call to org-occur pushes as cons cell,
  12559. containing the regular expression and the callback, onto the list.
  12560. The list can contain several entries if `org-occur' has been called
  12561. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  12562. will only contain one set of parameters. When the highlights are
  12563. removed (for example with `C-c C-c', or with the next edit (depending
  12564. on `org-remove-highlights-with-change'), this variable is emptied
  12565. as well.")
  12566. (defun org-occur (regexp &optional keep-previous callback)
  12567. "Make a compact tree which shows all matches of REGEXP.
  12568. The tree will show the lines where the regexp matches, and all higher
  12569. headlines above the match. It will also show the heading after the match,
  12570. to make sure editing the matching entry is easy.
  12571. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  12572. call to `org-occur' will be kept, to allow stacking of calls to this
  12573. command.
  12574. If CALLBACK is non-nil, it is a function which is called to confirm
  12575. that the match should indeed be shown."
  12576. (interactive "sRegexp: \nP")
  12577. (when (equal regexp "")
  12578. (user-error "Regexp cannot be empty"))
  12579. (unless keep-previous
  12580. (org-remove-occur-highlights nil nil t))
  12581. (push (cons regexp callback) org-occur-parameters)
  12582. (let ((cnt 0))
  12583. (save-excursion
  12584. (goto-char (point-min))
  12585. (when (or (not keep-previous) ; do not want to keep
  12586. (not org-occur-highlights)) ; no previous matches
  12587. ;; hide everything
  12588. (org-overview))
  12589. (while (re-search-forward regexp nil t)
  12590. (backward-char) ;; FIXME: Match timestamps at the end of a headline
  12591. (when (or (not callback)
  12592. (save-match-data (funcall callback)))
  12593. (setq cnt (1+ cnt))
  12594. (when org-highlight-sparse-tree-matches
  12595. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  12596. (org-show-context 'occur-tree))))
  12597. (when org-remove-highlights-with-change
  12598. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  12599. nil 'local))
  12600. (unless org-sparse-tree-open-archived-trees
  12601. (org-hide-archived-subtrees (point-min) (point-max)))
  12602. (run-hooks 'org-occur-hook)
  12603. (when (org-called-interactively-p 'interactive)
  12604. (message "%d match(es) for regexp %s" cnt regexp))
  12605. cnt))
  12606. (defun org-occur-next-match (&optional n _reset)
  12607. "Function for `next-error-function' to find sparse tree matches.
  12608. N is the number of matches to move, when negative move backwards.
  12609. This function always goes back to the starting point when no
  12610. match is found."
  12611. (let* ((limit (if (< n 0) (point-min) (point-max)))
  12612. (search-func (if (< n 0)
  12613. 'previous-single-char-property-change
  12614. 'next-single-char-property-change))
  12615. (n (abs n))
  12616. (pos (point))
  12617. p1)
  12618. (catch 'exit
  12619. (while (setq p1 (funcall search-func (point) 'org-type))
  12620. (when (equal p1 limit)
  12621. (goto-char pos)
  12622. (user-error "No more matches"))
  12623. (when (equal (get-char-property p1 'org-type) 'org-occur)
  12624. (setq n (1- n))
  12625. (when (= n 0)
  12626. (goto-char p1)
  12627. (throw 'exit (point))))
  12628. (goto-char p1))
  12629. (goto-char p1)
  12630. (user-error "No more matches"))))
  12631. (defun org-show-context (&optional key)
  12632. "Make sure point and context are visible.
  12633. Optional argument KEY, when non-nil, is a symbol. See
  12634. `org-show-context-detail' for allowed values and how much is to
  12635. be shown."
  12636. (org-show-set-visibility
  12637. (cond ((symbolp org-show-context-detail) org-show-context-detail)
  12638. ((cdr (assq key org-show-context-detail)))
  12639. (t (cdr (assq 'default org-show-context-detail))))))
  12640. (defun org-show-set-visibility (detail)
  12641. "Set visibility around point according to DETAIL.
  12642. DETAIL is either nil, `minimal', `local', `ancestors', `lineage',
  12643. `tree', `canonical' or t. See `org-show-context-detail' for more
  12644. information."
  12645. (unless (org-before-first-heading-p)
  12646. ;; Show current heading and possibly its entry, following headline
  12647. ;; or all children.
  12648. (if (and (org-at-heading-p) (not (eq detail 'local)))
  12649. (org-flag-heading nil)
  12650. (org-show-entry)
  12651. (org-with-limited-levels
  12652. (cl-case detail
  12653. ((tree canonical t) (org-show-children))
  12654. ((nil minimal ancestors))
  12655. (t (save-excursion
  12656. (outline-next-heading)
  12657. (org-flag-heading nil))))))
  12658. ;; Show all siblings.
  12659. (when (eq detail 'lineage) (org-show-siblings))
  12660. ;; Show ancestors, possibly with their children.
  12661. (when (memq detail '(ancestors lineage tree canonical t))
  12662. (save-excursion
  12663. (while (org-up-heading-safe)
  12664. (org-flag-heading nil)
  12665. (when (memq detail '(canonical t)) (org-show-entry))
  12666. (when (memq detail '(tree canonical t)) (org-show-children)))))))
  12667. (defvar org-reveal-start-hook nil
  12668. "Hook run before revealing a location.")
  12669. (defun org-reveal (&optional siblings)
  12670. "Show current entry, hierarchy above it, and the following headline.
  12671. This can be used to show a consistent set of context around
  12672. locations exposed with `org-show-context'.
  12673. With optional argument SIBLINGS, on each level of the hierarchy all
  12674. siblings are shown. This repairs the tree structure to what it would
  12675. look like when opened with hierarchical calls to `org-cycle'.
  12676. With double optional argument \\[universal-argument] \\[universal-argument], \
  12677. go to the parent and show the
  12678. entire tree."
  12679. (interactive "P")
  12680. (run-hooks 'org-reveal-start-hook)
  12681. (cond ((equal siblings '(4)) (org-show-set-visibility 'canonical))
  12682. ((equal siblings '(16))
  12683. (save-excursion
  12684. (when (org-up-heading-safe)
  12685. (org-show-subtree)
  12686. (run-hook-with-args 'org-cycle-hook 'subtree))))
  12687. (t (org-show-set-visibility 'lineage))))
  12688. (defun org-highlight-new-match (beg end)
  12689. "Highlight from BEG to END and mark the highlight is an occur headline."
  12690. (let ((ov (make-overlay beg end)))
  12691. (overlay-put ov 'face 'secondary-selection)
  12692. (overlay-put ov 'org-type 'org-occur)
  12693. (push ov org-occur-highlights)))
  12694. (defun org-remove-occur-highlights (&optional _beg _end noremove)
  12695. "Remove the occur highlights from the buffer.
  12696. BEG and END are ignored. If NOREMOVE is nil, remove this function
  12697. from the `before-change-functions' in the current buffer."
  12698. (interactive)
  12699. (unless org-inhibit-highlight-removal
  12700. (mapc #'delete-overlay org-occur-highlights)
  12701. (setq org-occur-highlights nil)
  12702. (setq org-occur-parameters nil)
  12703. (unless noremove
  12704. (remove-hook 'before-change-functions
  12705. 'org-remove-occur-highlights 'local))))
  12706. ;;;; Priorities
  12707. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  12708. "Regular expression matching the priority indicator.")
  12709. (defvar org-remove-priority-next-time nil)
  12710. (defun org-priority-up ()
  12711. "Increase the priority of the current item."
  12712. (interactive)
  12713. (org-priority 'up))
  12714. (defun org-priority-down ()
  12715. "Decrease the priority of the current item."
  12716. (interactive)
  12717. (org-priority 'down))
  12718. (defun org-priority (&optional action _show)
  12719. "Change the priority of an item.
  12720. ACTION can be `set', `up', `down', or a character."
  12721. (interactive "P")
  12722. (if (equal action '(4))
  12723. (org-show-priority)
  12724. (unless org-enable-priority-commands
  12725. (user-error "Priority commands are disabled"))
  12726. (setq action (or action 'set))
  12727. (let (current new news have remove)
  12728. (save-excursion
  12729. (org-back-to-heading t)
  12730. (when (looking-at org-priority-regexp)
  12731. (setq current (string-to-char (match-string 2))
  12732. have t))
  12733. (cond
  12734. ((eq action 'remove)
  12735. (setq remove t new ?\ ))
  12736. ((or (eq action 'set)
  12737. (if (featurep 'xemacs) (characterp action) (integerp action)))
  12738. (if (not (eq action 'set))
  12739. (setq new action)
  12740. (message "Priority %c-%c, SPC to remove: "
  12741. org-highest-priority org-lowest-priority)
  12742. (save-match-data
  12743. (setq new (read-char-exclusive))))
  12744. (when (and (= (upcase org-highest-priority) org-highest-priority)
  12745. (= (upcase org-lowest-priority) org-lowest-priority))
  12746. (setq new (upcase new)))
  12747. (cond ((equal new ?\ ) (setq remove t))
  12748. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  12749. (user-error "Priority must be between `%c' and `%c'"
  12750. org-highest-priority org-lowest-priority))))
  12751. ((eq action 'up)
  12752. (setq new (if have
  12753. (1- current) ; normal cycling
  12754. ;; last priority was empty
  12755. (if (eq last-command this-command)
  12756. org-lowest-priority ; wrap around empty to lowest
  12757. ;; default
  12758. (if org-priority-start-cycle-with-default
  12759. org-default-priority
  12760. (1- org-default-priority))))))
  12761. ((eq action 'down)
  12762. (setq new (if have
  12763. (1+ current) ; normal cycling
  12764. ;; last priority was empty
  12765. (if (eq last-command this-command)
  12766. org-highest-priority ; wrap around empty to highest
  12767. ;; default
  12768. (if org-priority-start-cycle-with-default
  12769. org-default-priority
  12770. (1+ org-default-priority))))))
  12771. (t (user-error "Invalid action")))
  12772. (when (or (< (upcase new) org-highest-priority)
  12773. (> (upcase new) org-lowest-priority))
  12774. (if (and (memq action '(up down))
  12775. (not have) (not (eq last-command this-command)))
  12776. ;; `new' is from default priority
  12777. (error
  12778. "The default can not be set, see `org-default-priority' why")
  12779. ;; normal cycling: `new' is beyond highest/lowest priority
  12780. ;; and is wrapped around to the empty priority
  12781. (setq remove t)))
  12782. (setq news (format "%c" new))
  12783. (if have
  12784. (if remove
  12785. (replace-match "" t t nil 1)
  12786. (replace-match news t t nil 2))
  12787. (if remove
  12788. (user-error "No priority cookie found in line")
  12789. (let ((case-fold-search nil))
  12790. (looking-at org-todo-line-regexp))
  12791. (if (match-end 2)
  12792. (progn
  12793. (goto-char (match-end 2))
  12794. (insert " [#" news "]"))
  12795. (goto-char (match-beginning 3))
  12796. (insert "[#" news "] "))))
  12797. (org-set-tags nil 'align))
  12798. (if remove
  12799. (message "Priority removed")
  12800. (message "Priority of current item set to %s" news)))))
  12801. (defun org-show-priority ()
  12802. "Show the priority of the current item.
  12803. This priority is composed of the main priority given with the [#A] cookies,
  12804. and by additional input from the age of a schedules or deadline entry."
  12805. (interactive)
  12806. (let ((pri (if (eq major-mode 'org-agenda-mode)
  12807. (org-get-at-bol 'priority)
  12808. (save-excursion
  12809. (save-match-data
  12810. (beginning-of-line)
  12811. (and (looking-at org-heading-regexp)
  12812. (org-get-priority (match-string 0))))))))
  12813. (message "Priority is %d" (if pri pri -1000))))
  12814. (defun org-get-priority (s)
  12815. "Find priority cookie and return priority."
  12816. (save-match-data
  12817. (if (functionp org-get-priority-function)
  12818. (funcall org-get-priority-function)
  12819. (if (not (string-match org-priority-regexp s))
  12820. (* 1000 (- org-lowest-priority org-default-priority))
  12821. (* 1000 (- org-lowest-priority
  12822. (string-to-char (match-string 2 s))))))))
  12823. ;;;; Tags
  12824. (defvar org-agenda-archives-mode)
  12825. (defvar org-map-continue-from nil
  12826. "Position from where mapping should continue.
  12827. Can be set by the action argument to `org-scan-tags' and `org-map-entries'.")
  12828. (defvar org-scanner-tags nil
  12829. "The current tag list while the tags scanner is running.")
  12830. (defvar org-trust-scanner-tags nil
  12831. "Should `org-get-tags-at' use the tags for the scanner.
  12832. This is for internal dynamical scoping only.
  12833. When this is non-nil, the function `org-get-tags-at' will return the value
  12834. of `org-scanner-tags' instead of building the list by itself. This
  12835. can lead to large speed-ups when the tags scanner is used in a file with
  12836. many entries, and when the list of tags is retrieved, for example to
  12837. obtain a list of properties. Building the tags list for each entry in such
  12838. a file becomes an N^2 operation - but with this variable set, it scales
  12839. as N.")
  12840. (defvar org--matcher-tags-todo-only nil)
  12841. (defun org-scan-tags (action matcher todo-only &optional start-level)
  12842. "Scan headline tags with inheritance and produce output ACTION.
  12843. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  12844. or `agenda' to produce an entry list for an agenda view. It can also be
  12845. a Lisp form or a function that should be called at each matched headline, in
  12846. this case the return value is a list of all return values from these calls.
  12847. MATCHER is a function accepting three arguments, returning
  12848. a non-nil value whenever a given set of tags qualifies a headline
  12849. for inclusion. See `org-make-tags-matcher' for more information.
  12850. As a special case, it can also be set to t (respectively nil) in
  12851. order to match all (respectively none) headline.
  12852. When TODO-ONLY is non-nil, only lines with a not-done TODO
  12853. keyword are included in the output.
  12854. START-LEVEL can be a string with asterisks, reducing the scope to
  12855. headlines matching this string."
  12856. (require 'org-agenda)
  12857. (let* ((re (concat "^"
  12858. (if start-level
  12859. ;; Get the correct level to match
  12860. (concat "\\*\\{" (number-to-string start-level) "\\} ")
  12861. org-outline-regexp)
  12862. " *\\(\\<\\("
  12863. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  12864. (org-re "\\)\\>\\)? *\\(.*?\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*$")))
  12865. (props (list 'face 'default
  12866. 'done-face 'org-agenda-done
  12867. 'undone-face 'default
  12868. 'mouse-face 'highlight
  12869. 'org-not-done-regexp org-not-done-regexp
  12870. 'org-todo-regexp org-todo-regexp
  12871. 'org-complex-heading-regexp org-complex-heading-regexp
  12872. 'help-echo
  12873. (format "mouse-2 or RET jump to org file %s"
  12874. (abbreviate-file-name
  12875. (or (buffer-file-name (buffer-base-buffer))
  12876. (buffer-name (buffer-base-buffer)))))))
  12877. (org-map-continue-from nil)
  12878. lspos tags tags-list
  12879. (tags-alist (list (cons 0 org-file-tags)))
  12880. (llast 0) rtn rtn1 level category i txt
  12881. todo marker entry priority
  12882. ts-date ts-date-type ts-date-pair)
  12883. (unless (or (member action '(agenda sparse-tree)) (functionp action))
  12884. (setq action (list 'lambda nil action)))
  12885. (save-excursion
  12886. (goto-char (point-min))
  12887. (when (eq action 'sparse-tree)
  12888. (org-overview)
  12889. (org-remove-occur-highlights))
  12890. (while (let (case-fold-search)
  12891. (re-search-forward re nil t))
  12892. (setq org-map-continue-from nil)
  12893. (catch :skip
  12894. (setq todo
  12895. ;; TODO: is the 1-2 difference a bug?
  12896. (when (match-end 1) (org-match-string-no-properties 2))
  12897. tags (when (match-end 4) (org-match-string-no-properties 4)))
  12898. (goto-char (setq lspos (match-beginning 0)))
  12899. (setq level (org-reduced-level (org-outline-level))
  12900. category (org-get-category))
  12901. (when (eq action 'agenda)
  12902. (setq ts-date-pair (org-agenda-entry-get-agenda-timestamp (point))
  12903. ts-date (car ts-date-pair)
  12904. ts-date-type (cdr ts-date-pair)))
  12905. (setq i llast llast level)
  12906. ;; remove tag lists from same and sublevels
  12907. (while (>= i level)
  12908. (when (setq entry (assoc i tags-alist))
  12909. (setq tags-alist (delete entry tags-alist)))
  12910. (setq i (1- i)))
  12911. ;; add the next tags
  12912. (when tags
  12913. (setq tags (org-split-string tags ":")
  12914. tags-alist
  12915. (cons (cons level tags) tags-alist)))
  12916. ;; compile tags for current headline
  12917. (setq tags-list
  12918. (if org-use-tag-inheritance
  12919. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  12920. tags)
  12921. org-scanner-tags tags-list)
  12922. (when org-use-tag-inheritance
  12923. (setcdr (car tags-alist)
  12924. (mapcar (lambda (x)
  12925. (setq x (copy-sequence x))
  12926. (org-add-prop-inherited x))
  12927. (cdar tags-alist))))
  12928. (when (and tags org-use-tag-inheritance
  12929. (or (not (eq t org-use-tag-inheritance))
  12930. org-tags-exclude-from-inheritance))
  12931. ;; Selective inheritance, remove uninherited ones.
  12932. (setcdr (car tags-alist)
  12933. (org-remove-uninherited-tags (cdar tags-alist))))
  12934. (when (and
  12935. ;; eval matcher only when the todo condition is OK
  12936. (and (or (not todo-only) (member todo org-not-done-keywords))
  12937. (if (functionp matcher)
  12938. (let ((case-fold-search t) (org-trust-scanner-tags t))
  12939. (funcall matcher todo tags-list level))
  12940. matcher))
  12941. ;; Call the skipper, but return t if it does not
  12942. ;; skip, so that the `and' form continues evaluating.
  12943. (progn
  12944. (unless (eq action 'sparse-tree) (org-agenda-skip))
  12945. t)
  12946. ;; Check if timestamps are deselecting this entry
  12947. (or (not todo-only)
  12948. (and (member todo org-not-done-keywords)
  12949. (or (not org-agenda-tags-todo-honor-ignore-options)
  12950. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item))))))
  12951. ;; select this headline
  12952. (cond
  12953. ((eq action 'sparse-tree)
  12954. (and org-highlight-sparse-tree-matches
  12955. (org-get-heading) (match-end 0)
  12956. (org-highlight-new-match
  12957. (match-beginning 1) (match-end 1)))
  12958. (org-show-context 'tags-tree))
  12959. ((eq action 'agenda)
  12960. (setq txt (org-agenda-format-item
  12961. ""
  12962. (concat
  12963. (if (eq org-tags-match-list-sublevels 'indented)
  12964. (make-string (1- level) ?.) "")
  12965. (org-get-heading))
  12966. (make-string level ?\s)
  12967. category
  12968. tags-list)
  12969. priority (org-get-priority txt))
  12970. (goto-char lspos)
  12971. (setq marker (org-agenda-new-marker))
  12972. (org-add-props txt props
  12973. 'org-marker marker 'org-hd-marker marker 'org-category category
  12974. 'todo-state todo
  12975. 'ts-date ts-date
  12976. 'priority priority
  12977. 'type (concat "tagsmatch" ts-date-type))
  12978. (push txt rtn))
  12979. ((functionp action)
  12980. (setq org-map-continue-from nil)
  12981. (save-excursion
  12982. (setq rtn1 (funcall action))
  12983. (push rtn1 rtn)))
  12984. (t (user-error "Invalid action")))
  12985. ;; if we are to skip sublevels, jump to end of subtree
  12986. (unless org-tags-match-list-sublevels
  12987. (org-end-of-subtree t)
  12988. (backward-char 1))))
  12989. ;; Get the correct position from where to continue
  12990. (if org-map-continue-from
  12991. (goto-char org-map-continue-from)
  12992. (and (= (point) lspos) (end-of-line 1)))))
  12993. (when (and (eq action 'sparse-tree)
  12994. (not org-sparse-tree-open-archived-trees))
  12995. (org-hide-archived-subtrees (point-min) (point-max)))
  12996. (nreverse rtn)))
  12997. (defun org-remove-uninherited-tags (tags)
  12998. "Remove all tags that are not inherited from the list TAGS."
  12999. (cond
  13000. ((eq org-use-tag-inheritance t)
  13001. (if org-tags-exclude-from-inheritance
  13002. (org-delete-all org-tags-exclude-from-inheritance tags)
  13003. tags))
  13004. ((not org-use-tag-inheritance) nil)
  13005. ((stringp org-use-tag-inheritance)
  13006. (delq nil (mapcar
  13007. (lambda (x)
  13008. (if (and (string-match org-use-tag-inheritance x)
  13009. (not (member x org-tags-exclude-from-inheritance)))
  13010. x nil))
  13011. tags)))
  13012. ((listp org-use-tag-inheritance)
  13013. (delq nil (mapcar
  13014. (lambda (x)
  13015. (if (member x org-use-tag-inheritance) x nil))
  13016. tags)))))
  13017. (defun org-match-sparse-tree (&optional todo-only match)
  13018. "Create a sparse tree according to tags string MATCH.
  13019. MATCH can contain positive and negative selection of tags, like
  13020. \"+WORK+URGENT-WITHBOSS\".
  13021. If optional argument TODO-ONLY is non-nil, only select lines that are
  13022. also TODO lines."
  13023. (interactive "P")
  13024. (org-agenda-prepare-buffers (list (current-buffer)))
  13025. (let ((org--matcher-tags-todo-only todo-only))
  13026. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match))
  13027. org--matcher-tags-todo-only)))
  13028. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  13029. (defvar org-cached-props nil)
  13030. (defun org-cached-entry-get (pom property)
  13031. (if (or (eq t org-use-property-inheritance)
  13032. (and (stringp org-use-property-inheritance)
  13033. (let ((case-fold-search t))
  13034. (org-string-match-p org-use-property-inheritance property)))
  13035. (and (listp org-use-property-inheritance)
  13036. (member-ignore-case property org-use-property-inheritance)))
  13037. ;; Caching is not possible, check it directly.
  13038. (org-entry-get pom property 'inherit)
  13039. ;; Get all properties, so we can do complicated checks easily.
  13040. (cdr (assoc-string property
  13041. (or org-cached-props
  13042. (setq org-cached-props (org-entry-properties pom)))
  13043. t))))
  13044. (defun org-global-tags-completion-table (&optional files)
  13045. "Return the list of all tags in all agenda buffer/files.
  13046. Optional FILES argument is a list of files which can be used
  13047. instead of the agenda files."
  13048. (save-excursion
  13049. (org-uniquify
  13050. (delq nil
  13051. (apply 'append
  13052. (mapcar
  13053. (lambda (file)
  13054. (set-buffer (find-file-noselect file))
  13055. (append (org-get-buffer-tags)
  13056. (mapcar (lambda (x) (if (stringp (car-safe x))
  13057. (list (car-safe x)) nil))
  13058. org-tag-alist)))
  13059. (if (and files (car files))
  13060. files
  13061. (org-agenda-files))))))))
  13062. (defun org-make-tags-matcher (match)
  13063. "Create the TAGS/TODO matcher form for the selection string MATCH.
  13064. Returns a cons of the selection string MATCH and a function
  13065. implementing the matcher.
  13066. The matcher is to be called at an Org entry, with point on the
  13067. headline, and returns non-nil if the entry matches the selection
  13068. string MATCH. It must be called with three arguments: the TODO
  13069. keyword at the entry (or nil if none), the list of all tags at
  13070. the entry including inherited ones and the reduced level of the
  13071. headline. Additionally, the category of the entry, if any, must
  13072. be specified as the text property `org-category' on the headline.
  13073. This function sets the variable `org--matcher-tags-todo-only' to
  13074. a non-nil value if the matcher restricts matching to TODO
  13075. entries, otherwise it is not touched.
  13076. See also `org-scan-tags'."
  13077. (unless match
  13078. ;; Get a new match request, with completion against the global
  13079. ;; tags table and the local tags in current buffer.
  13080. (let ((org-last-tags-completion-table
  13081. (org-uniquify
  13082. (delq nil (append (org-get-buffer-tags)
  13083. (org-global-tags-completion-table))))))
  13084. (setq match
  13085. (completing-read
  13086. "Match: "
  13087. 'org-tags-completion-function nil nil nil 'org-tags-history))))
  13088. (let ((match0 match)
  13089. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\(\\(?:[[:alnum:]_]+\\(?:\\\\-\\)*\\)+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@#%]+\\)"))
  13090. (start 0)
  13091. tagsmatch todomatch tagsmatcher todomatcher)
  13092. ;; Expand group tags.
  13093. (setq match (org-tags-expand match))
  13094. ;; Check if there is a TODO part of this match, which would be the
  13095. ;; part after a "/". To make sure that this slash is not part of
  13096. ;; a property value to be matched against, we also check that
  13097. ;; there is no / after that slash. First, find the last slash.
  13098. (let ((s 0))
  13099. (while (string-match "/+" match s)
  13100. (setq start (match-beginning 0))
  13101. (setq s (match-end 0))))
  13102. (if (and (string-match "/+" match start)
  13103. (not (string-match-p "\"" match start)))
  13104. ;; Match contains also a TODO-matching request.
  13105. (progn
  13106. (setq tagsmatch (substring match 0 (match-beginning 0)))
  13107. (setq todomatch (substring match (match-end 0)))
  13108. (when (string-match "\\`!" todomatch)
  13109. (setq org--matcher-tags-todo-only t)
  13110. (setq todomatch (substring todomatch 1)))
  13111. (when (string-match "\\`\\s-*\\'" todomatch)
  13112. (setq todomatch nil)))
  13113. ;; Only matching tags.
  13114. (setq tagsmatch match)
  13115. (setq todomatch nil))
  13116. ;; Make the tags matcher.
  13117. (when (org-string-nw-p tagsmatch)
  13118. (let ((orlist nil)
  13119. (orterms (org-split-string tagsmatch "|"))
  13120. term)
  13121. (while (setq term (pop orterms))
  13122. (while (and (equal (substring term -1) "\\") orterms)
  13123. (setq term (concat term "|" (pop orterms)))) ;repair bad split.
  13124. (while (string-match re term)
  13125. (let* ((rest (substring term (match-end 0)))
  13126. (minus (and (match-end 1)
  13127. (equal (match-string 1 term) "-")))
  13128. (tag (save-match-data
  13129. (replace-regexp-in-string
  13130. "\\\\-" "-" (match-string 2 term))))
  13131. (regexp (eq (string-to-char tag) ?{))
  13132. (levelp (match-end 4))
  13133. (propp (match-end 5))
  13134. (mm
  13135. (cond
  13136. (regexp `(org-match-any-p ,(substring tag 1 -1) tags-list))
  13137. (levelp
  13138. `(,(org-op-to-function (match-string 3 term))
  13139. level
  13140. ,(string-to-number (match-string 4 term))))
  13141. (propp
  13142. (let* ((gv (pcase (upcase (match-string 5 term))
  13143. ("CATEGORY"
  13144. '(get-text-property (point) 'org-category))
  13145. ("TODO" 'todo)
  13146. (p `(org-cached-entry-get nil ,p))))
  13147. (pv (match-string 7 term))
  13148. (regexp (eq (string-to-char pv) ?{))
  13149. (strp (eq (string-to-char pv) ?\"))
  13150. (timep (string-match-p "^\"[[<].*[]>]\"$" pv))
  13151. (po (org-op-to-function (match-string 6 term)
  13152. (if timep 'time strp))))
  13153. (setq pv (if (or regexp strp) (substring pv 1 -1) pv))
  13154. (when timep (setq pv (org-matcher-time pv)))
  13155. (cond ((and regexp (eq po 'org<>))
  13156. `(not (string-match ,pv (or ,gv ""))))
  13157. (regexp `(string-match ,pv (or ,gv "")))
  13158. (strp `(,po (or ,gv "") ,pv))
  13159. (t
  13160. `(,po
  13161. (string-to-number (or ,gv ""))
  13162. ,(string-to-number pv))))))
  13163. (t `(member ,tag tags-list)))))
  13164. (push (if minus `(not ,mm) mm) tagsmatcher)
  13165. (setq term rest)))
  13166. (push `(and ,@tagsmatcher) orlist)
  13167. (setq tagsmatcher nil))
  13168. (setq tagsmatcher `(progn (setq org-cached-props nil) (or ,@orlist)))))
  13169. ;; Make the TODO matcher.
  13170. (when (org-string-nw-p todomatch)
  13171. (let ((orlist nil))
  13172. (dolist (term (org-split-string todomatch "|"))
  13173. (while (string-match re term)
  13174. (let* ((minus (and (match-end 1)
  13175. (equal (match-string 1 term) "-")))
  13176. (kwd (match-string 2 term))
  13177. (regexp (eq (string-to-char kwd) ?{))
  13178. (mm (if regexp `(string-match ,(substring kwd 1 -1) todo)
  13179. `(equal todo ,kwd))))
  13180. (push (if minus `(not ,mm) mm) todomatcher))
  13181. (setq term (substring term (match-end 0))))
  13182. (push (if (> (length todomatcher) 1)
  13183. (cons 'and todomatcher)
  13184. (car todomatcher))
  13185. orlist)
  13186. (setq todomatcher nil))
  13187. (setq todomatcher (cons 'or orlist))))
  13188. ;; Return the string and function of the matcher. If no
  13189. ;; tags-specific or todo-specific matcher exists, match
  13190. ;; everything.
  13191. (let ((matcher (if (and tagsmatcher todomatcher)
  13192. `(and ,tagsmatcher ,todomatcher)
  13193. (or tagsmatcher todomatcher t))))
  13194. (when org--matcher-tags-todo-only
  13195. (setq matcher `(and (member todo org-not-done-keywords) ,matcher)))
  13196. (cons match0 `(lambda (todo tags-list level) ,matcher)))))
  13197. (defun org-tags-expand (match &optional single-as-list downcased tags-already-expanded)
  13198. "Expand group tags in MATCH.
  13199. This replaces every group tag in MATCH with a regexp tag search.
  13200. For example, a group tag \"Work\" defined as { Work : Lab Conf }
  13201. will be replaced like this:
  13202. Work => {\\<\\(?:Work\\|Lab\\|Conf\\)\\>}
  13203. +Work => +{\\<\\(?:Work\\|Lab\\|Conf\\)\\>}
  13204. -Work => -{\\<\\(?:Work\\|Lab\\|Conf\\)\\>}
  13205. Replacing by a regexp preserves the structure of the match.
  13206. E.g., this expansion
  13207. Work|Home => {\\(?:Work\\|Lab\\|Conf\\}|Home
  13208. will match anything tagged with \"Lab\" and \"Home\", or tagged
  13209. with \"Conf\" and \"Home\" or tagged with \"Work\" and \"home\".
  13210. A group tag in MATCH can contain regular expressions of its own.
  13211. For example, a group tag \"Proj\" defined as { Proj : {P@.+} }
  13212. will be replaced like this:
  13213. Proj => {\\<\\(?:Proj\\)\\>\\|P@.+}
  13214. When the optional argument SINGLE-AS-LIST is non-nil, MATCH is
  13215. assumed to be a single group tag, and the function will return
  13216. the list of tags in this group.
  13217. When DOWNCASE is non-nil, expand downcased TAGS."
  13218. (if org-group-tags
  13219. (let* ((case-fold-search t)
  13220. (stable org-mode-syntax-table)
  13221. (taggroups (or org-tag-groups-alist-for-agenda org-tag-groups-alist))
  13222. (taggroups (if downcased
  13223. (mapcar (lambda (tg) (mapcar #'downcase tg))
  13224. taggroups)
  13225. taggroups))
  13226. (taggroups-keys (mapcar #'car taggroups))
  13227. (return-match (if downcased (downcase match) match))
  13228. (count 0)
  13229. (work-already-expanded tags-already-expanded)
  13230. regexps-in-match tags-in-group regexp-in-group regexp-in-group-escaped)
  13231. ;; @ and _ are allowed as word-components in tags.
  13232. (modify-syntax-entry ?@ "w" stable)
  13233. (modify-syntax-entry ?_ "w" stable)
  13234. ;; Temporarily replace regexp-expressions in the match-expression.
  13235. (while (string-match "{.+?}" return-match)
  13236. (cl-incf count)
  13237. (push (match-string 0 return-match) regexps-in-match)
  13238. (setq return-match (replace-match (format "<%d>" count) t nil return-match)))
  13239. (while (and taggroups-keys
  13240. (with-syntax-table stable
  13241. (string-match
  13242. (concat "\\(?1:[+-]?\\)\\(?2:\\<"
  13243. (regexp-opt taggroups-keys) "\\>\\)")
  13244. return-match)))
  13245. (let* ((dir (match-string 1 return-match))
  13246. (tag (match-string 2 return-match))
  13247. (tag (if downcased (downcase tag) tag)))
  13248. (unless (or (get-text-property 0 'grouptag (match-string 2 return-match))
  13249. (member tag work-already-expanded))
  13250. (setq tags-in-group (assoc tag taggroups))
  13251. (push tag work-already-expanded)
  13252. ;; Recursively expand each tag in the group, if the tag hasn't
  13253. ;; already been expanded. Restore the match-data after all recursive calls.
  13254. (save-match-data
  13255. (let (tags-expanded)
  13256. (dolist (x (cdr tags-in-group))
  13257. (if (and (member x taggroups-keys)
  13258. (not (member x work-already-expanded)))
  13259. (setq tags-expanded
  13260. (delete-dups
  13261. (append
  13262. (org-tags-expand x t downcased
  13263. work-already-expanded)
  13264. tags-expanded)))
  13265. (setq tags-expanded
  13266. (append (list x) tags-expanded)))
  13267. (setq work-already-expanded
  13268. (delete-dups
  13269. (append tags-expanded
  13270. work-already-expanded))))
  13271. (setq tags-in-group
  13272. (delete-dups (cons (car tags-in-group)
  13273. tags-expanded)))))
  13274. ;; Filter tag-regexps from tags.
  13275. (setq regexp-in-group-escaped
  13276. (delq nil (mapcar (lambda (x)
  13277. (if (stringp x)
  13278. (and (equal "{" (substring x 0 1))
  13279. (equal "}" (substring x -1))
  13280. x)
  13281. x))
  13282. tags-in-group))
  13283. regexp-in-group
  13284. (mapcar (lambda (x)
  13285. (substring x 1 -1))
  13286. regexp-in-group-escaped)
  13287. tags-in-group
  13288. (delq nil (mapcar (lambda (x)
  13289. (if (stringp x)
  13290. (and (not (equal "{" (substring x 0 1)))
  13291. (not (equal "}" (substring x -1)))
  13292. x)
  13293. x))
  13294. tags-in-group)))
  13295. ;; If single-as-list, do no more in the while-loop.
  13296. (if (not single-as-list)
  13297. (progn
  13298. (when regexp-in-group
  13299. (setq regexp-in-group
  13300. (concat "\\|"
  13301. (mapconcat 'identity regexp-in-group
  13302. "\\|"))))
  13303. (setq tags-in-group
  13304. (concat dir
  13305. "{\\<"
  13306. (regexp-opt tags-in-group)
  13307. "\\>"
  13308. regexp-in-group
  13309. "}"))
  13310. (when (stringp tags-in-group)
  13311. (org-add-props tags-in-group '(grouptag t)))
  13312. (setq return-match
  13313. (replace-match tags-in-group t t return-match)))
  13314. (setq tags-in-group
  13315. (append regexp-in-group-escaped tags-in-group))))
  13316. (setq taggroups-keys (delete tag taggroups-keys))))
  13317. ;; Add the regular expressions back into the match-expression again.
  13318. (while regexps-in-match
  13319. (setq return-match (replace-regexp-in-string (format "<%d>" count)
  13320. (pop regexps-in-match)
  13321. return-match t t))
  13322. (cl-decf count))
  13323. (if single-as-list
  13324. (if tags-in-group tags-in-group (list return-match))
  13325. return-match))
  13326. (if single-as-list
  13327. (list (if downcased (downcase match) match))
  13328. match)))
  13329. (defun org-op-to-function (op &optional stringp)
  13330. "Turn an operator into the appropriate function."
  13331. (setq op
  13332. (cond
  13333. ((equal op "<" ) '(< string< org-time<))
  13334. ((equal op ">" ) '(> org-string> org-time>))
  13335. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  13336. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  13337. ((member op '("=" "==")) '(= string= org-time=))
  13338. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  13339. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  13340. (defun org<> (a b) (not (= a b)))
  13341. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  13342. (defun org-string>= (a b) (not (string< a b)))
  13343. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  13344. (defun org-string<> (a b) (not (string= a b)))
  13345. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  13346. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  13347. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  13348. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  13349. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  13350. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  13351. (defun org-2ft (s)
  13352. "Convert S to a floating point time.
  13353. If S is already a number, just return it. If it is a string, parse
  13354. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  13355. (cond
  13356. ((numberp s) s)
  13357. ((stringp s)
  13358. (condition-case nil
  13359. (float-time (apply 'encode-time (org-parse-time-string s)))
  13360. (error 0.)))
  13361. (t 0.)))
  13362. (defun org-time-today ()
  13363. "Time in seconds today at 0:00.
  13364. Returns the float number of seconds since the beginning of the
  13365. epoch to the beginning of today (00:00)."
  13366. (float-time (apply 'encode-time
  13367. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  13368. (defun org-matcher-time (s)
  13369. "Interpret a time comparison value."
  13370. (save-match-data
  13371. (cond
  13372. ((string= s "<now>") (float-time))
  13373. ((string= s "<today>") (org-time-today))
  13374. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  13375. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  13376. ((string-match "^<\\([-+][0-9]+\\)\\([hdwmy]\\)>$" s)
  13377. (+ (org-time-today)
  13378. (* (string-to-number (match-string 1 s))
  13379. (cdr (assoc (match-string 2 s)
  13380. '(("d" . 86400.0) ("w" . 604800.0)
  13381. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  13382. (t (org-2ft s)))))
  13383. (defun org-match-any-p (re list)
  13384. "Does re match any element of list?"
  13385. (setq list (mapcar (lambda (x) (string-match re x)) list))
  13386. (delq nil list))
  13387. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  13388. (defvar org-tags-overlay (make-overlay 1 1))
  13389. (org-detach-overlay org-tags-overlay)
  13390. (defun org-get-local-tags-at (&optional pos)
  13391. "Get a list of tags defined in the current headline."
  13392. (org-get-tags-at pos 'local))
  13393. (defun org-get-local-tags ()
  13394. "Get a list of tags defined in the current headline."
  13395. (org-get-tags-at nil 'local))
  13396. (defun org-get-tags-at (&optional pos local)
  13397. "Get a list of all headline tags applicable at POS.
  13398. POS defaults to point. If tags are inherited, the list contains
  13399. the targets in the same sequence as the headlines appear, i.e.
  13400. the tags of the current headline come last.
  13401. When LOCAL is non-nil, only return tags from the current headline,
  13402. ignore inherited ones."
  13403. (interactive)
  13404. (if (and org-trust-scanner-tags
  13405. (or (not pos) (equal pos (point)))
  13406. (not local))
  13407. org-scanner-tags
  13408. (let (tags ltags lastpos parent)
  13409. (save-excursion
  13410. (save-restriction
  13411. (widen)
  13412. (goto-char (or pos (point)))
  13413. (save-match-data
  13414. (catch 'done
  13415. (condition-case nil
  13416. (progn
  13417. (org-back-to-heading t)
  13418. (while (not (equal lastpos (point)))
  13419. (setq lastpos (point))
  13420. (when (looking-at
  13421. (org-re "[^\r\n]+?:\\([[:alnum:]_@#%:]+\\):[ \t]*$"))
  13422. (setq ltags (org-split-string
  13423. (org-match-string-no-properties 1) ":"))
  13424. (when parent
  13425. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  13426. (setq tags (append
  13427. (if parent
  13428. (org-remove-uninherited-tags ltags)
  13429. ltags)
  13430. tags)))
  13431. (or org-use-tag-inheritance (throw 'done t))
  13432. (when local (throw 'done t))
  13433. (or (org-up-heading-safe) (error nil))
  13434. (setq parent t)))
  13435. (error nil)))))
  13436. (if local
  13437. tags
  13438. (reverse (delete-dups
  13439. (reverse (append
  13440. (org-remove-uninherited-tags
  13441. org-file-tags)
  13442. tags)))))))))
  13443. (defun org-add-prop-inherited (s)
  13444. (add-text-properties 0 (length s) '(inherited t) s)
  13445. s)
  13446. (defun org-toggle-tag (tag &optional onoff)
  13447. "Toggle the tag TAG for the current line.
  13448. If ONOFF is `on' or `off', don't toggle but set to this state."
  13449. (let (res current)
  13450. (save-excursion
  13451. (org-back-to-heading t)
  13452. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t]*$")
  13453. (point-at-eol) t)
  13454. (progn
  13455. (setq current (match-string 1))
  13456. (replace-match ""))
  13457. (setq current ""))
  13458. (setq current (nreverse (org-split-string current ":")))
  13459. (cond
  13460. ((eq onoff 'on)
  13461. (setq res t)
  13462. (or (member tag current) (push tag current)))
  13463. ((eq onoff 'off)
  13464. (or (not (member tag current)) (setq current (delete tag current))))
  13465. (t (if (member tag current)
  13466. (setq current (delete tag current))
  13467. (setq res t)
  13468. (push tag current))))
  13469. (end-of-line 1)
  13470. (if current
  13471. (progn
  13472. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  13473. (org-set-tags nil t))
  13474. (delete-horizontal-space))
  13475. (run-hooks 'org-after-tags-change-hook))
  13476. res))
  13477. (defun org-align-tags-here (to-col)
  13478. ;; Assumes that this is a headline
  13479. "Align tags on the current headline to TO-COL."
  13480. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  13481. (beginning-of-line 1)
  13482. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13483. (< pos (match-beginning 2)))
  13484. (progn
  13485. (setq tags-l (- (match-end 2) (match-beginning 2)))
  13486. (goto-char (match-beginning 1))
  13487. (insert " ")
  13488. (delete-region (point) (1+ (match-beginning 2)))
  13489. (setq ncol (max (current-column)
  13490. (1+ col)
  13491. (if (> to-col 0)
  13492. to-col
  13493. (- (abs to-col) tags-l))))
  13494. (setq p (point))
  13495. (insert (make-string (- ncol (current-column)) ?\ ))
  13496. (setq ncol (current-column))
  13497. (when indent-tabs-mode (tabify p (point-at-eol)))
  13498. (org-move-to-column (min ncol col)))
  13499. (goto-char pos))))
  13500. (defun org-set-tags-command (&optional arg just-align)
  13501. "Call the set-tags command for the current entry."
  13502. (interactive "P")
  13503. (if (or (org-at-heading-p) (and arg (org-before-first-heading-p)))
  13504. (org-set-tags arg just-align)
  13505. (save-excursion
  13506. (unless (and (org-region-active-p)
  13507. org-loop-over-headlines-in-active-region)
  13508. (org-back-to-heading t))
  13509. (org-set-tags arg just-align))))
  13510. (defun org-set-tags-to (data)
  13511. "Set the tags of the current entry to DATA, replacing the current tags.
  13512. DATA may be a tags string like :aa:bb:cc:, or a list of tags.
  13513. If DATA is nil or the empty string, any tags will be removed."
  13514. (interactive "sTags: ")
  13515. (setq data
  13516. (cond
  13517. ((eq data nil) "")
  13518. ((equal data "") "")
  13519. ((stringp data)
  13520. (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
  13521. ":"))
  13522. ((listp data)
  13523. (concat ":" (mapconcat 'identity data ":") ":"))))
  13524. (when data
  13525. (save-excursion
  13526. (org-back-to-heading t)
  13527. (when (looking-at org-complex-heading-regexp)
  13528. (if (match-end 5)
  13529. (progn
  13530. (goto-char (match-beginning 5))
  13531. (insert data)
  13532. (delete-region (point) (point-at-eol))
  13533. (org-set-tags nil 'align))
  13534. (goto-char (point-at-eol))
  13535. (insert " " data)
  13536. (org-set-tags nil 'align)))
  13537. (beginning-of-line 1)
  13538. (when (looking-at ".*?\\([ \t]+\\)$")
  13539. (delete-region (match-beginning 1) (match-end 1))))))
  13540. (defun org-align-all-tags ()
  13541. "Align the tags in all headings."
  13542. (interactive)
  13543. (save-excursion
  13544. (or (ignore-errors (org-back-to-heading t))
  13545. (outline-next-heading))
  13546. (if (org-at-heading-p)
  13547. (org-set-tags t)
  13548. (message "No headings"))))
  13549. (defvar org-indent-indentation-per-level)
  13550. (defun org-set-tags (&optional arg just-align)
  13551. "Set the tags for the current headline.
  13552. With prefix ARG, realign all tags in headings in the current buffer.
  13553. When JUST-ALIGN is non-nil, only align tags."
  13554. (interactive "P")
  13555. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  13556. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  13557. 'region-start-level
  13558. 'region))
  13559. org-loop-over-headlines-in-active-region)
  13560. (org-map-entries
  13561. ;; We don't use ARG and JUST-ALIGN here because these args
  13562. ;; are not useful when looping over headlines.
  13563. #'org-set-tags
  13564. org-loop-over-headlines-in-active-region
  13565. cl
  13566. '(when (outline-invisible-p) (org-end-of-subtree nil t))))
  13567. (let ((org-setting-tags t))
  13568. (if arg
  13569. (save-excursion
  13570. (goto-char (point-min))
  13571. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  13572. (while (re-search-forward org-outline-regexp-bol nil t)
  13573. (org-set-tags nil t)
  13574. (end-of-line)))
  13575. (message "All tags realigned to column %d" org-tags-column))
  13576. (let* ((current (org-get-tags-string))
  13577. (col (current-column))
  13578. (tags
  13579. (if just-align current
  13580. ;; Get a new set of tags from the user.
  13581. (save-excursion
  13582. (let* ((table
  13583. (setq
  13584. org-last-tags-completion-table
  13585. (append
  13586. org-tag-persistent-alist
  13587. (or org-tag-alist (org-get-buffer-tags))
  13588. (and
  13589. org-complete-tags-always-offer-all-agenda-tags
  13590. (org-global-tags-completion-table
  13591. (org-agenda-files))))))
  13592. (current-tags (org-split-string current ":"))
  13593. (inherited-tags
  13594. (nreverse (nthcdr (length current-tags)
  13595. (nreverse (org-get-tags-at))))))
  13596. (replace-regexp-in-string
  13597. "\\([-+&]+\\|,\\)"
  13598. ":"
  13599. (if (or (eq t org-use-fast-tag-selection)
  13600. (and org-use-fast-tag-selection
  13601. (delq nil (mapcar #'cdr table))))
  13602. (org-fast-tag-selection
  13603. current-tags inherited-tags table
  13604. (and org-fast-tag-selection-include-todo
  13605. org-todo-key-alist))
  13606. (let ((org-add-colon-after-tag-completion
  13607. (< 1 (length table))))
  13608. (org-trim
  13609. (completing-read
  13610. "Tags: "
  13611. #'org-tags-completion-function
  13612. nil nil current 'org-tags-history))))))))))
  13613. (when org-tags-sort-function
  13614. (setq tags
  13615. (mapconcat
  13616. #'identity
  13617. (sort (org-split-string tags (org-re "[^[:alnum:]_@#%]+"))
  13618. org-tags-sort-function)
  13619. ":")))
  13620. (if (not (org-string-nw-p tags)) (setq tags "")
  13621. (unless (string-match ":\\'" tags) (setq tags (concat tags ":")))
  13622. (unless (string-match "\\`:" tags) (setq tags (concat ":" tags))))
  13623. ;; Insert new tags at the correct column
  13624. (beginning-of-line)
  13625. (let ((level (if (looking-at org-outline-regexp)
  13626. (- (match-end 0) (point) 1)
  13627. 1)))
  13628. (cond
  13629. ((and (equal current "") (equal tags "")))
  13630. ((re-search-forward
  13631. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  13632. (line-end-position)
  13633. t)
  13634. (if (equal tags "") (replace-match "" t t)
  13635. (goto-char (match-beginning 0))
  13636. (let* ((c0 (current-column))
  13637. ;; Compute offset for the case of org-indent-mode
  13638. ;; active.
  13639. (di (if (org-bound-and-true-p org-indent-mode)
  13640. (* (1- org-indent-indentation-per-level)
  13641. (1- level))
  13642. 0))
  13643. (p0 (if (eq (char-before) ?*) (1+ (point)) (point)))
  13644. (tc (+ org-tags-column
  13645. (if (> org-tags-column 0) (- di) di)))
  13646. (c1 (max (1+ c0)
  13647. (if (> tc 0) tc
  13648. (- (- tc) (string-width tags)))))
  13649. (rpl (concat (make-string (max 0 (- c1 c0)) ?\s) tags)))
  13650. (replace-match rpl t t)
  13651. (when (and (not (featurep 'xemacs)) indent-tabs-mode)
  13652. (tabify p0 (point))))))
  13653. (t (error "Tags alignment failed"))))
  13654. (org-move-to-column col))
  13655. (unless just-align (run-hooks 'org-after-tags-change-hook))))))
  13656. (defun org-change-tag-in-region (beg end tag off)
  13657. "Add or remove TAG for each entry in the region.
  13658. This works in the agenda, and also in an org-mode buffer."
  13659. (interactive
  13660. (list (region-beginning) (region-end)
  13661. (let ((org-last-tags-completion-table
  13662. (if (derived-mode-p 'org-mode)
  13663. (org-uniquify
  13664. (delq nil (append (org-get-buffer-tags)
  13665. (org-global-tags-completion-table))))
  13666. (org-global-tags-completion-table))))
  13667. (completing-read
  13668. "Tag: " 'org-tags-completion-function nil nil nil
  13669. 'org-tags-history))
  13670. (progn
  13671. (message "[s]et or [r]emove? ")
  13672. (equal (read-char-exclusive) ?r))))
  13673. (when (fboundp 'deactivate-mark) (deactivate-mark))
  13674. (let ((agendap (equal major-mode 'org-agenda-mode))
  13675. l1 l2 m buf pos newhead (cnt 0))
  13676. (goto-char end)
  13677. (setq l2 (1- (org-current-line)))
  13678. (goto-char beg)
  13679. (setq l1 (org-current-line))
  13680. (cl-loop for l from l1 to l2 do
  13681. (org-goto-line l)
  13682. (setq m (get-text-property (point) 'org-hd-marker))
  13683. (when (or (and (derived-mode-p 'org-mode) (org-at-heading-p))
  13684. (and agendap m))
  13685. (setq buf (if agendap (marker-buffer m) (current-buffer))
  13686. pos (if agendap m (point)))
  13687. (with-current-buffer buf
  13688. (save-excursion
  13689. (save-restriction
  13690. (goto-char pos)
  13691. (setq cnt (1+ cnt))
  13692. (org-toggle-tag tag (if off 'off 'on))
  13693. (setq newhead (org-get-heading)))))
  13694. (and agendap (org-agenda-change-all-lines newhead m))))
  13695. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  13696. (defun org-tags-completion-function (string _predicate &optional flag)
  13697. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  13698. (confirm (lambda (x) (stringp (car x)))))
  13699. (if (string-match "^\\(.*[-+:&,|]\\)\\([^-+:&,|]*\\)$" string)
  13700. (setq s1 (match-string 1 string)
  13701. s2 (match-string 2 string))
  13702. (setq s1 "" s2 string))
  13703. (cond
  13704. ((eq flag nil)
  13705. ;; try completion
  13706. (setq rtn (try-completion s2 ctable confirm))
  13707. (when (stringp rtn)
  13708. (setq rtn
  13709. (concat s1 s2 (substring rtn (length s2))
  13710. (if (and org-add-colon-after-tag-completion
  13711. (assoc rtn ctable))
  13712. ":" ""))))
  13713. rtn)
  13714. ((eq flag t)
  13715. ;; all-completions
  13716. (all-completions s2 ctable confirm))
  13717. ((eq flag 'lambda)
  13718. ;; exact match?
  13719. (assoc s2 ctable)))))
  13720. (defun org-fast-tag-insert (kwd tags face &optional end)
  13721. "Insert KDW, and the TAGS, the latter with face FACE.
  13722. Also insert END."
  13723. (insert (format "%-12s" (concat kwd ":"))
  13724. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  13725. (or end "")))
  13726. (defun org-fast-tag-show-exit (flag)
  13727. (save-excursion
  13728. (org-goto-line 3)
  13729. (when (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  13730. (replace-match ""))
  13731. (when flag
  13732. (end-of-line 1)
  13733. (org-move-to-column (- (window-width) 19) t)
  13734. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  13735. (defun org-set-current-tags-overlay (current prefix)
  13736. "Add an overlay to CURRENT tag with PREFIX."
  13737. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  13738. (if (featurep 'xemacs)
  13739. (org-overlay-display org-tags-overlay (concat prefix s)
  13740. 'secondary-selection)
  13741. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  13742. (org-overlay-display org-tags-overlay (concat prefix s)))))
  13743. (defvar org-last-tag-selection-key nil)
  13744. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  13745. "Fast tag selection with single keys.
  13746. CURRENT is the current list of tags in the headline, INHERITED is the
  13747. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  13748. possibly with grouping information. TODO-TABLE is a similar table with
  13749. TODO keywords, should these have keys assigned to them.
  13750. If the keys are nil, a-z are automatically assigned.
  13751. Returns the new tags string, or nil to not change the current settings."
  13752. (let* ((fulltable (append table todo-table))
  13753. (maxlen (apply 'max (mapcar
  13754. (lambda (x)
  13755. (if (stringp (car x)) (string-width (car x)) 0))
  13756. fulltable)))
  13757. (buf (current-buffer))
  13758. (expert (eq org-fast-tag-selection-single-key 'expert))
  13759. (buffer-tags nil)
  13760. (fwidth (+ maxlen 3 1 3))
  13761. (ncol (/ (- (window-width) 4) fwidth))
  13762. (i-face 'org-done)
  13763. (c-face 'org-todo)
  13764. tg cnt e c char c1 c2 ntable tbl rtn
  13765. ov-start ov-end ov-prefix
  13766. (exit-after-next org-fast-tag-selection-single-key)
  13767. (done-keywords org-done-keywords)
  13768. groups ingroup intaggroup)
  13769. (save-excursion
  13770. (beginning-of-line 1)
  13771. (if (looking-at
  13772. (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13773. (setq ov-start (match-beginning 1)
  13774. ov-end (match-end 1)
  13775. ov-prefix "")
  13776. (setq ov-start (1- (point-at-eol))
  13777. ov-end (1+ ov-start))
  13778. (skip-chars-forward "^\n\r")
  13779. (setq ov-prefix
  13780. (concat
  13781. (buffer-substring (1- (point)) (point))
  13782. (if (> (current-column) org-tags-column)
  13783. " "
  13784. (make-string (- org-tags-column (current-column)) ?\ ))))))
  13785. (move-overlay org-tags-overlay ov-start ov-end)
  13786. (save-window-excursion
  13787. (if expert
  13788. (set-buffer (get-buffer-create " *Org tags*"))
  13789. (delete-other-windows)
  13790. (set-window-buffer (split-window-vertically) (get-buffer-create " *Org tags*"))
  13791. (org-switch-to-buffer-other-window " *Org tags*"))
  13792. (erase-buffer)
  13793. (setq-local org-done-keywords done-keywords)
  13794. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  13795. (org-fast-tag-insert "Current" current c-face "\n\n")
  13796. (org-fast-tag-show-exit exit-after-next)
  13797. (org-set-current-tags-overlay current ov-prefix)
  13798. (setq tbl fulltable char ?a cnt 0)
  13799. (while (setq e (pop tbl))
  13800. (cond
  13801. ((eq (car e) :startgroup)
  13802. (push '() groups) (setq ingroup t)
  13803. (unless (zerop cnt)
  13804. (setq cnt 0)
  13805. (insert "\n"))
  13806. (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
  13807. ((eq (car e) :endgroup)
  13808. (setq ingroup nil cnt 0)
  13809. (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
  13810. ((eq (car e) :startgrouptag)
  13811. (setq intaggroup t)
  13812. (unless (zerop cnt)
  13813. (setq cnt 0)
  13814. (insert "\n"))
  13815. (insert "[ "))
  13816. ((eq (car e) :endgrouptag)
  13817. (setq intaggroup nil cnt 0)
  13818. (insert "]\n"))
  13819. ((equal e '(:newline))
  13820. (unless (zerop cnt)
  13821. (setq cnt 0)
  13822. (insert "\n")
  13823. (setq e (car tbl))
  13824. (while (equal (car tbl) '(:newline))
  13825. (insert "\n")
  13826. (setq tbl (cdr tbl)))))
  13827. ((equal e '(:grouptags)) (insert " : "))
  13828. (t
  13829. (setq tg (copy-sequence (car e)) c2 nil)
  13830. (if (cdr e)
  13831. (setq c (cdr e))
  13832. ;; automatically assign a character.
  13833. (setq c1 (string-to-char
  13834. (downcase (substring
  13835. tg (if (= (string-to-char tg) ?@) 1 0)))))
  13836. (if (or (rassoc c1 ntable) (rassoc c1 table))
  13837. (while (or (rassoc char ntable) (rassoc char table))
  13838. (setq char (1+ char)))
  13839. (setq c2 c1))
  13840. (setq c (or c2 char)))
  13841. (when ingroup (push tg (car groups)))
  13842. (setq tg (org-add-props tg nil 'face
  13843. (cond
  13844. ((not (assoc tg table))
  13845. (org-get-todo-face tg))
  13846. ((member tg current) c-face)
  13847. ((member tg inherited) i-face))))
  13848. (when (equal (caar tbl) :grouptags)
  13849. (org-add-props tg nil 'face 'org-tag-group))
  13850. (when (and (zerop cnt) (not ingroup) (not intaggroup)) (insert " "))
  13851. (insert "[" c "] " tg (make-string
  13852. (- fwidth 4 (length tg)) ?\ ))
  13853. (push (cons tg c) ntable)
  13854. (when (= (cl-incf cnt) ncol)
  13855. (insert "\n")
  13856. (when (or ingroup intaggroup) (insert " "))
  13857. (setq cnt 0)))))
  13858. (setq ntable (nreverse ntable))
  13859. (insert "\n")
  13860. (goto-char (point-min))
  13861. (unless expert (org-fit-window-to-buffer))
  13862. (setq rtn
  13863. (catch 'exit
  13864. (while t
  13865. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
  13866. (if (not groups) "no " "")
  13867. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  13868. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  13869. (setq org-last-tag-selection-key c)
  13870. (cond
  13871. ((= c ?\r) (throw 'exit t))
  13872. ((= c ?!)
  13873. (setq groups (not groups))
  13874. (goto-char (point-min))
  13875. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  13876. ((= c ?\C-c)
  13877. (if (not expert)
  13878. (org-fast-tag-show-exit
  13879. (setq exit-after-next (not exit-after-next)))
  13880. (setq expert nil)
  13881. (delete-other-windows)
  13882. (set-window-buffer (split-window-vertically) " *Org tags*")
  13883. (org-switch-to-buffer-other-window " *Org tags*")
  13884. (org-fit-window-to-buffer)))
  13885. ((or (= c ?\C-g)
  13886. (and (= c ?q) (not (rassoc c ntable))))
  13887. (org-detach-overlay org-tags-overlay)
  13888. (setq quit-flag t))
  13889. ((= c ?\ )
  13890. (setq current nil)
  13891. (when exit-after-next (setq exit-after-next 'now)))
  13892. ((= c ?\t)
  13893. (condition-case nil
  13894. (setq tg (completing-read
  13895. "Tag: "
  13896. (or buffer-tags
  13897. (with-current-buffer buf
  13898. (setq buffer-tags
  13899. (org-get-buffer-tags))))))
  13900. (quit (setq tg "")))
  13901. (when (string-match "\\S-" tg)
  13902. (cl-pushnew (list tg) buffer-tags :test #'equal)
  13903. (if (member tg current)
  13904. (setq current (delete tg current))
  13905. (push tg current)))
  13906. (when exit-after-next (setq exit-after-next 'now)))
  13907. ((setq e (rassoc c todo-table) tg (car e))
  13908. (with-current-buffer buf
  13909. (save-excursion (org-todo tg)))
  13910. (when exit-after-next (setq exit-after-next 'now)))
  13911. ((setq e (rassoc c ntable) tg (car e))
  13912. (if (member tg current)
  13913. (setq current (delete tg current))
  13914. (cl-loop for g in groups do
  13915. (when (member tg g)
  13916. (dolist (x g) (setq current (delete x current)))))
  13917. (push tg current))
  13918. (when exit-after-next (setq exit-after-next 'now))))
  13919. ;; Create a sorted list
  13920. (setq current
  13921. (sort current
  13922. (lambda (a b)
  13923. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  13924. (when (eq exit-after-next 'now) (throw 'exit t))
  13925. (goto-char (point-min))
  13926. (beginning-of-line 2)
  13927. (delete-region (point) (point-at-eol))
  13928. (org-fast-tag-insert "Current" current c-face)
  13929. (org-set-current-tags-overlay current ov-prefix)
  13930. (while (re-search-forward
  13931. (org-re "\\[.\\] \\([[:alnum:]_@#%]+\\)") nil t)
  13932. (setq tg (match-string 1))
  13933. (add-text-properties
  13934. (match-beginning 1) (match-end 1)
  13935. (list 'face
  13936. (cond
  13937. ((member tg current) c-face)
  13938. ((member tg inherited) i-face)
  13939. (t (get-text-property (match-beginning 1) 'face))))))
  13940. (goto-char (point-min)))))
  13941. (org-detach-overlay org-tags-overlay)
  13942. (if rtn
  13943. (mapconcat 'identity current ":")
  13944. nil))))
  13945. (defun org-get-tags-string ()
  13946. "Get the TAGS string in the current headline."
  13947. (unless (org-at-heading-p t)
  13948. (user-error "Not on a heading"))
  13949. (save-excursion
  13950. (beginning-of-line 1)
  13951. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13952. (org-match-string-no-properties 1)
  13953. "")))
  13954. (defun org-get-tags ()
  13955. "Get the list of tags specified in the current headline."
  13956. (org-split-string (org-get-tags-string) ":"))
  13957. (defun org-get-buffer-tags ()
  13958. "Get a table of all tags used in the buffer, for completion."
  13959. (org-with-wide-buffer
  13960. (goto-char (point-min))
  13961. (let ((tag-re (concat org-outline-regexp-bol
  13962. "\\(?:.*?[ \t]\\)?"
  13963. (org-re ":\\([[:alnum:]_@#%:]+\\):[ \t]*$")))
  13964. tags)
  13965. (while (re-search-forward tag-re nil t)
  13966. (dolist (tag (org-split-string (org-match-string-no-properties 1) ":"))
  13967. (push tag tags)))
  13968. (mapcar #'list (append org-file-tags (org-uniquify tags))))))
  13969. ;;;; The mapping API
  13970. (defvar org-agenda-skip-comment-trees)
  13971. (defvar org-agenda-skip-function)
  13972. (defun org-map-entries (func &optional match scope &rest skip)
  13973. "Call FUNC at each headline selected by MATCH in SCOPE.
  13974. FUNC is a function or a lisp form. The function will be called without
  13975. arguments, with the cursor positioned at the beginning of the headline.
  13976. The return values of all calls to the function will be collected and
  13977. returned as a list.
  13978. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  13979. does not need to preserve point. After evaluation, the cursor will be
  13980. moved to the end of the line (presumably of the headline of the
  13981. processed entry) and search continues from there. Under some
  13982. circumstances, this may not produce the wanted results. For example,
  13983. if you have removed (e.g. archived) the current (sub)tree it could
  13984. mean that the next entry will be skipped entirely. In such cases, you
  13985. can specify the position from where search should continue by making
  13986. FUNC set the variable `org-map-continue-from' to the desired buffer
  13987. position.
  13988. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  13989. Only headlines that are matched by this query will be considered during
  13990. the iteration. When MATCH is nil or t, all headlines will be
  13991. visited by the iteration.
  13992. SCOPE determines the scope of this command. It can be any of:
  13993. nil The current buffer, respecting the restriction if any
  13994. tree The subtree started with the entry at point
  13995. region The entries within the active region, if any
  13996. region-start-level
  13997. The entries within the active region, but only those at
  13998. the same level than the first one.
  13999. file The current buffer, without restriction
  14000. file-with-archives
  14001. The current buffer, and any archives associated with it
  14002. agenda All agenda files
  14003. agenda-with-archives
  14004. All agenda files with any archive files associated with them
  14005. \(file1 file2 ...)
  14006. If this is a list, all files in the list will be scanned
  14007. The remaining args are treated as settings for the skipping facilities of
  14008. the scanner. The following items can be given here:
  14009. archive skip trees with the archive tag
  14010. comment skip trees with the COMMENT keyword
  14011. function or Emacs Lisp form:
  14012. will be used as value for `org-agenda-skip-function', so
  14013. whenever the function returns a position, FUNC will not be
  14014. called for that entry and search will continue from the
  14015. position returned
  14016. If your function needs to retrieve the tags including inherited tags
  14017. at the *current* entry, you can use the value of the variable
  14018. `org-scanner-tags' which will be much faster than getting the value
  14019. with `org-get-tags-at'. If your function gets properties with
  14020. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  14021. to t around the call to `org-entry-properties' to get the same speedup.
  14022. Note that if your function moves around to retrieve tags and properties at
  14023. a *different* entry, you cannot use these techniques."
  14024. (unless (and (or (eq scope 'region) (eq scope 'region-start-level))
  14025. (not (org-region-active-p)))
  14026. (let* ((org-agenda-archives-mode nil) ; just to make sure
  14027. (org-agenda-skip-archived-trees (memq 'archive skip))
  14028. (org-agenda-skip-comment-trees (memq 'comment skip))
  14029. (org-agenda-skip-function
  14030. (car (org-delete-all '(comment archive) skip)))
  14031. (org-tags-match-list-sublevels t)
  14032. (start-level (eq scope 'region-start-level))
  14033. matcher res
  14034. org-todo-keywords-for-agenda
  14035. org-done-keywords-for-agenda
  14036. org-todo-keyword-alist-for-agenda
  14037. org-tag-alist-for-agenda
  14038. org--matcher-tags-todo-only)
  14039. (cond
  14040. ((eq match t) (setq matcher t))
  14041. ((eq match nil) (setq matcher t))
  14042. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  14043. (save-excursion
  14044. (save-restriction
  14045. (cond ((eq scope 'tree)
  14046. (org-back-to-heading t)
  14047. (org-narrow-to-subtree)
  14048. (setq scope nil))
  14049. ((and (or (eq scope 'region) (eq scope 'region-start-level))
  14050. (org-region-active-p))
  14051. ;; If needed, set start-level to a string like "2"
  14052. (when start-level
  14053. (save-excursion
  14054. (goto-char (region-beginning))
  14055. (unless (org-at-heading-p) (outline-next-heading))
  14056. (setq start-level (org-current-level))))
  14057. (narrow-to-region (region-beginning)
  14058. (save-excursion
  14059. (goto-char (region-end))
  14060. (unless (and (bolp) (org-at-heading-p))
  14061. (outline-next-heading))
  14062. (point)))
  14063. (setq scope nil)))
  14064. (if (not scope)
  14065. (progn
  14066. (org-agenda-prepare-buffers
  14067. (and buffer-file-name (list buffer-file-name)))
  14068. (setq res
  14069. (org-scan-tags
  14070. func matcher org--matcher-tags-todo-only start-level)))
  14071. ;; Get the right scope
  14072. (cond
  14073. ((and scope (listp scope) (symbolp (car scope)))
  14074. (setq scope (eval scope)))
  14075. ((eq scope 'agenda)
  14076. (setq scope (org-agenda-files t)))
  14077. ((eq scope 'agenda-with-archives)
  14078. (setq scope (org-agenda-files t))
  14079. (setq scope (org-add-archive-files scope)))
  14080. ((eq scope 'file)
  14081. (setq scope (and buffer-file-name (list buffer-file-name))))
  14082. ((eq scope 'file-with-archives)
  14083. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  14084. (org-agenda-prepare-buffers scope)
  14085. (dolist (file scope)
  14086. (with-current-buffer (org-find-base-buffer-visiting file)
  14087. (save-excursion
  14088. (save-restriction
  14089. (widen)
  14090. (goto-char (point-min))
  14091. (setq res
  14092. (append
  14093. res
  14094. (org-scan-tags
  14095. func matcher org--matcher-tags-todo-only))))))))))
  14096. res)))
  14097. ;;; Properties API
  14098. (defconst org-special-properties
  14099. '("ALLTAGS" "BLOCKED" "CLOCKSUM" "CLOCKSUM_T" "CLOSED" "DEADLINE" "FILE"
  14100. "ITEM" "PRIORITY" "SCHEDULED" "TAGS" "TIMESTAMP" "TIMESTAMP_IA" "TODO")
  14101. "The special properties valid in Org mode.
  14102. These are properties that are not defined in the property drawer,
  14103. but in some other way.")
  14104. (defconst org-default-properties
  14105. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  14106. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  14107. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  14108. "EXPORT_OPTIONS" "EXPORT_TEXT" "EXPORT_FILE_NAME"
  14109. "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE" "UNNUMBERED"
  14110. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER" "REPEAT_TO_STATE"
  14111. "CLOCK_MODELINE_TOTAL" "STYLE" "HTML_CONTAINER_CLASS")
  14112. "Some properties that are used by Org mode for various purposes.
  14113. Being in this list makes sure that they are offered for completion.")
  14114. (defun org--valid-property-p (property)
  14115. "Non nil when string PROPERTY is a valid property name."
  14116. (not
  14117. (or (equal property "")
  14118. (org-string-match-p "\\s-" property))))
  14119. (defun org--update-property-plist (key val props)
  14120. "Associate KEY to VAL in alist PROPS.
  14121. Modifications are made by side-effect. Return new alist."
  14122. (let* ((appending (string= (substring key -1) "+"))
  14123. (key (if appending (substring key 0 -1) key))
  14124. (old (assoc-string key props t)))
  14125. (if (not old) (cons (cons key val) props)
  14126. (setcdr old (if appending (concat (cdr old) " " val) val))
  14127. props)))
  14128. (defun org-get-property-block (&optional beg force)
  14129. "Return the (beg . end) range of the body of the property drawer.
  14130. BEG is the beginning of the current subtree, or of the part
  14131. before the first headline. If it is not given, it will be found.
  14132. If the drawer does not exist, create it if FORCE is non-nil, or
  14133. return nil."
  14134. (org-with-wide-buffer
  14135. (when beg (goto-char beg))
  14136. (unless (org-before-first-heading-p)
  14137. (let ((beg (cond (beg)
  14138. ((or (not (featurep 'org-inlinetask))
  14139. (org-inlinetask-in-task-p))
  14140. (org-back-to-heading t))
  14141. (t (org-with-limited-levels (org-back-to-heading t))))))
  14142. (forward-line)
  14143. (when (org-looking-at-p org-planning-line-re) (forward-line))
  14144. (cond ((looking-at org-property-drawer-re)
  14145. (forward-line)
  14146. (cons (point) (progn (goto-char (match-end 0))
  14147. (line-beginning-position))))
  14148. (force
  14149. (goto-char beg)
  14150. (org-insert-property-drawer)
  14151. (let ((pos (save-excursion (search-forward ":END:")
  14152. (line-beginning-position))))
  14153. (cons pos pos))))))))
  14154. (defun org-at-property-p ()
  14155. "Non-nil when point is inside a property drawer.
  14156. See `org-property-re' for match data, if applicable."
  14157. (save-excursion
  14158. (beginning-of-line)
  14159. (and (looking-at org-property-re)
  14160. (let ((property-drawer (save-match-data (org-get-property-block))))
  14161. (and property-drawer
  14162. (>= (point) (car property-drawer))
  14163. (< (point) (cdr property-drawer)))))))
  14164. (defun org-property-action ()
  14165. "Do an action on properties."
  14166. (interactive)
  14167. (unless (org-at-property-p) (user-error "Not at a property"))
  14168. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  14169. (let ((c (read-char-exclusive)))
  14170. (cl-case c
  14171. (?s (call-interactively #'org-set-property))
  14172. (?d (call-interactively #'org-delete-property))
  14173. (?D (call-interactively #'org-delete-property-globally))
  14174. (?c (call-interactively #'org-compute-property-at-point))
  14175. (otherwise (user-error "No such property action %c" c)))))
  14176. (defun org-inc-effort ()
  14177. "Increment the value of the effort property in the current entry."
  14178. (interactive)
  14179. (org-set-effort nil t))
  14180. (defvar org-clock-effort) ; Defined in org-clock.el.
  14181. (defvar org-clock-current-task) ; Defined in org-clock.el.
  14182. (defun org-set-effort (&optional value increment)
  14183. "Set the effort property of the current entry.
  14184. With numerical prefix arg, use the nth allowed value, 0 stands for the
  14185. 10th allowed value.
  14186. When INCREMENT is non-nil, set the property to the next allowed value."
  14187. (interactive "P")
  14188. (when (equal value 0) (setq value 10))
  14189. (let* ((completion-ignore-case t)
  14190. (prop org-effort-property)
  14191. (cur (org-entry-get nil prop))
  14192. (allowed (org-property-get-allowed-values nil prop 'table))
  14193. (existing (mapcar 'list (org-property-values prop)))
  14194. (heading (nth 4 (org-heading-components)))
  14195. rpl
  14196. (val (cond
  14197. ((stringp value) value)
  14198. ((and allowed (integerp value))
  14199. (or (car (nth (1- value) allowed))
  14200. (car (org-last allowed))))
  14201. ((and allowed increment)
  14202. (or (cl-caadr (member (list cur) allowed))
  14203. (user-error "Allowed effort values are not set")))
  14204. (allowed
  14205. (message "Select 1-9,0, [RET%s]: %s"
  14206. (if cur (concat "=" cur) "")
  14207. (mapconcat 'car allowed " "))
  14208. (setq rpl (read-char-exclusive))
  14209. (if (equal rpl ?\r)
  14210. cur
  14211. (setq rpl (- rpl ?0))
  14212. (when (equal rpl 0) (setq rpl 10))
  14213. (if (and (> rpl 0) (<= rpl (length allowed)))
  14214. (car (nth (1- rpl) allowed))
  14215. (org-completing-read "Effort: " allowed nil))))
  14216. (t
  14217. (org-completing-read
  14218. (concat "Effort " (if (and cur (string-match "\\S-" cur))
  14219. (concat "[" cur "]") "")
  14220. ": ")
  14221. existing nil nil "" nil cur)))))
  14222. (unless (equal (org-entry-get nil prop) val)
  14223. (org-entry-put nil prop val))
  14224. (org-refresh-property
  14225. '((effort . identity)
  14226. (effort-minutes . org-duration-string-to-minutes))
  14227. val)
  14228. (when (string= heading org-clock-current-task)
  14229. (setq org-clock-effort (get-text-property (point-at-bol) 'effort))
  14230. (org-clock-update-mode-line))
  14231. (message "%s is now %s" prop val)))
  14232. (defun org-entry-properties (&optional pom which)
  14233. "Get all properties of the current entry.
  14234. When POM is a buffer position, get all properties from the entry
  14235. there instead.
  14236. This includes the TODO keyword, the tags, time strings for
  14237. deadline, scheduled, and clocking, and any additional properties
  14238. defined in the entry.
  14239. If WHICH is nil or `all', get all properties. If WHICH is
  14240. `special' or `standard', only get that subclass. If WHICH is
  14241. a string, only get that property.
  14242. Return value is an alist. Keys are properties, as upcased
  14243. strings."
  14244. (org-with-point-at pom
  14245. (when (and (derived-mode-p 'org-mode)
  14246. (ignore-errors (org-back-to-heading t)))
  14247. (catch 'exit
  14248. (let* ((beg (point))
  14249. (specific (and (stringp which) (upcase which)))
  14250. (which (cond ((not specific) which)
  14251. ((member specific org-special-properties) 'special)
  14252. (t 'standard)))
  14253. props)
  14254. ;; Get the special properties, like TODO and TAGS.
  14255. (when (memq which '(nil all special))
  14256. (when (or (not specific) (string= specific "CLOCKSUM"))
  14257. (let ((clocksum (get-text-property (point) :org-clock-minutes)))
  14258. (when clocksum
  14259. (push (cons "CLOCKSUM"
  14260. (org-minutes-to-clocksum-string clocksum))
  14261. props)))
  14262. (when specific (throw 'exit props)))
  14263. (when (or (not specific) (string= specific "CLOCKSUM_T"))
  14264. (let ((clocksumt (get-text-property (point)
  14265. :org-clock-minutes-today)))
  14266. (when clocksumt
  14267. (push (cons "CLOCKSUM_T"
  14268. (org-minutes-to-clocksum-string clocksumt))
  14269. props)))
  14270. (when specific (throw 'exit props)))
  14271. (when (or (not specific) (string= specific "ITEM"))
  14272. (when (looking-at org-complex-heading-regexp)
  14273. (push (cons "ITEM"
  14274. (let ((title (match-string-no-properties 4)))
  14275. (if (org-string-nw-p title)
  14276. (org-remove-tabs title)
  14277. "")))
  14278. props))
  14279. (when specific (throw 'exit props)))
  14280. (when (or (not specific) (string= specific "TODO"))
  14281. (let ((case-fold-search nil))
  14282. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  14283. (push (cons "TODO" (org-match-string-no-properties 2)) props)))
  14284. (when specific (throw 'exit props)))
  14285. (when (or (not specific) (string= specific "PRIORITY"))
  14286. (push (cons "PRIORITY"
  14287. (if (looking-at org-priority-regexp)
  14288. (org-match-string-no-properties 2)
  14289. (char-to-string org-default-priority)))
  14290. props)
  14291. (when specific (throw 'exit props)))
  14292. (when (or (not specific) (string= specific "FILE"))
  14293. (push (cons "FILE" (buffer-file-name (buffer-base-buffer)))
  14294. props)
  14295. (when specific (throw 'exit props)))
  14296. (when (or (not specific) (string= specific "TAGS"))
  14297. (let ((value (org-string-nw-p (org-get-tags-string))))
  14298. (when value (push (cons "TAGS" value) props)))
  14299. (when specific (throw 'exit props)))
  14300. (when (or (not specific) (string= specific "ALLTAGS"))
  14301. (let ((value (org-get-tags-at)))
  14302. (when value
  14303. (push (cons "ALLTAGS"
  14304. (format ":%s:" (mapconcat #'identity value ":")))
  14305. props)))
  14306. (when specific (throw 'exit props)))
  14307. (when (or (not specific) (string= specific "BLOCKED"))
  14308. (push (cons "BLOCKED" (if (org-entry-blocked-p) "t" "")) props)
  14309. (when specific (throw 'exit props)))
  14310. (when (or (not specific)
  14311. (member specific '("CLOSED" "DEADLINE" "SCHEDULED")))
  14312. (forward-line)
  14313. (when (org-looking-at-p org-planning-line-re)
  14314. (end-of-line)
  14315. (let ((bol (line-beginning-position))
  14316. ;; Backward compatibility: time keywords used to
  14317. ;; be configurable (before 8.3). Make sure we
  14318. ;; get the correct keyword.
  14319. (key-assoc `(("CLOSED" . ,org-closed-string)
  14320. ("DEADLINE" . ,org-deadline-string)
  14321. ("SCHEDULED" . ,org-scheduled-string))))
  14322. (dolist (pair (if specific (list (assoc specific key-assoc))
  14323. key-assoc))
  14324. (save-excursion
  14325. (when (search-backward (cdr pair) bol t)
  14326. (goto-char (match-end 0))
  14327. (skip-chars-forward " \t")
  14328. (and (looking-at org-ts-regexp-both)
  14329. (push (cons (car pair)
  14330. (org-match-string-no-properties 0))
  14331. props)))))))
  14332. (when specific (throw 'exit props)))
  14333. (when (or (not specific)
  14334. (member specific '("TIMESTAMP" "TIMESTAMP_IA")))
  14335. (let ((find-ts
  14336. (lambda (end ts)
  14337. ;; Fix next time-stamp before END. TS is the
  14338. ;; list of time-stamps found so far.
  14339. (let ((ts ts)
  14340. (regexp (cond
  14341. ((string= specific "TIMESTAMP")
  14342. org-ts-regexp)
  14343. ((string= specific "TIMESTAMP_IA")
  14344. org-ts-regexp-inactive)
  14345. ((assoc "TIMESTAMP_IA" ts)
  14346. org-ts-regexp)
  14347. ((assoc "TIMESTAMP" ts)
  14348. org-ts-regexp-inactive)
  14349. (t org-ts-regexp-both))))
  14350. (catch 'next
  14351. (while (re-search-forward regexp end t)
  14352. (backward-char)
  14353. (let ((object (org-element-context)))
  14354. ;; Accept to match timestamps in node
  14355. ;; properties, too.
  14356. (when (memq (org-element-type object)
  14357. '(node-property timestamp))
  14358. (let ((type
  14359. (org-element-property :type object)))
  14360. (cond
  14361. ((and (memq type '(active active-range))
  14362. (not (equal specific "TIMESTAMP_IA")))
  14363. (unless (assoc "TIMESTAMP" ts)
  14364. (push (cons "TIMESTAMP"
  14365. (org-element-property
  14366. :raw-value object))
  14367. ts)
  14368. (when specific (throw 'exit ts))))
  14369. ((and (memq type '(inactive inactive-range))
  14370. (not (string= specific "TIMESTAMP")))
  14371. (unless (assoc "TIMESTAMP_IA" ts)
  14372. (push (cons "TIMESTAMP_IA"
  14373. (org-element-property
  14374. :raw-value object))
  14375. ts)
  14376. (when specific (throw 'exit ts))))))
  14377. ;; Both timestamp types are found,
  14378. ;; move to next part.
  14379. (when (= (length ts) 2) (throw 'next ts)))))
  14380. ts)))))
  14381. (goto-char beg)
  14382. ;; First look for timestamps within headline.
  14383. (let ((ts (funcall find-ts (line-end-position) nil)))
  14384. (if (= (length ts) 2) (setq props (nconc ts props))
  14385. (forward-line)
  14386. ;; Then find timestamps in the section, skipping
  14387. ;; planning line.
  14388. (when (org-looking-at-p org-planning-line-re)
  14389. (forward-line))
  14390. (let ((end (save-excursion (outline-next-heading))))
  14391. (setq props (nconc (funcall find-ts end ts) props))))))))
  14392. ;; Get the standard properties, like :PROP:.
  14393. (when (memq which '(nil all standard))
  14394. ;; If we are looking after a specific property, delegate
  14395. ;; to `org-entry-get', which is faster. However, make an
  14396. ;; exception for "CATEGORY", since it can be also set
  14397. ;; through keywords (i.e. #+CATEGORY).
  14398. (if (and specific (not (equal specific "CATEGORY")))
  14399. (let ((value (org-entry-get beg specific nil t)))
  14400. (throw 'exit (and value (list (cons specific value)))))
  14401. (let ((range (org-get-property-block beg)))
  14402. (when range
  14403. (let ((end (cdr range)) seen-base)
  14404. (goto-char (car range))
  14405. ;; Unlike to `org--update-property-plist', we
  14406. ;; handle the case where base values is found
  14407. ;; after its extension. We also forbid standard
  14408. ;; properties to be named as special properties.
  14409. (while (re-search-forward org-property-re end t)
  14410. (let* ((key (upcase (org-match-string-no-properties 2)))
  14411. (extendp (org-string-match-p "\\+\\'" key))
  14412. (key-base (if extendp (substring key 0 -1) key))
  14413. (value (org-match-string-no-properties 3)))
  14414. (cond
  14415. ((member-ignore-case key-base org-special-properties))
  14416. (extendp
  14417. (setq props
  14418. (org--update-property-plist key value props)))
  14419. ((member key seen-base))
  14420. (t (push key seen-base)
  14421. (let ((p (assoc-string key props t)))
  14422. (if p (setcdr p (concat value " " (cdr p)))
  14423. (push (cons key value) props))))))))))))
  14424. (unless (assoc "CATEGORY" props)
  14425. (push (cons "CATEGORY" (org-get-category beg)) props)
  14426. (when (string= specific "CATEGORY") (throw 'exit props)))
  14427. ;; Return value.
  14428. props)))))
  14429. (defun org-property--local-values (property literal-nil)
  14430. "Return value for PROPERTY in current entry.
  14431. Value is a list whose care is the base value for PROPERTY and cdr
  14432. a list of accumulated values. Return nil if neither is found in
  14433. the entry. Also return nil when PROPERTY is set to \"nil\",
  14434. unless LITERAL-NIL is non-nil."
  14435. (let ((range (org-get-property-block)))
  14436. (when range
  14437. (goto-char (car range))
  14438. (let* ((case-fold-search t)
  14439. (end (cdr range))
  14440. (value
  14441. ;; Base value.
  14442. (save-excursion
  14443. (let ((v (and (re-search-forward
  14444. (org-re-property property nil t) end t)
  14445. (org-match-string-no-properties 3))))
  14446. (list (if literal-nil v (org-not-nil v)))))))
  14447. ;; Find additional values.
  14448. (let* ((property+ (org-re-property (concat property "+") nil t)))
  14449. (while (re-search-forward property+ end t)
  14450. (push (org-match-string-no-properties 3) value)))
  14451. ;; Return final values.
  14452. (and (not (equal value '(nil))) (nreverse value))))))
  14453. (defun org-entry-get (pom property &optional inherit literal-nil)
  14454. "Get value of PROPERTY for entry or content at point-or-marker POM.
  14455. If INHERIT is non-nil and the entry does not have the property,
  14456. then also check higher levels of the hierarchy. If INHERIT is
  14457. the symbol `selective', use inheritance only if the setting in
  14458. `org-use-property-inheritance' selects PROPERTY for inheritance.
  14459. If the property is present but empty, the return value is the
  14460. empty string. If the property is not present at all, nil is
  14461. returned. In any other case, return the value as a string.
  14462. Search is case-insensitive.
  14463. If LITERAL-NIL is set, return the string value \"nil\" as
  14464. a string, do not interpret it as the list atom nil. This is used
  14465. for inheritance when a \"nil\" value can supersede a non-nil
  14466. value higher up the hierarchy."
  14467. (org-with-point-at pom
  14468. (cond
  14469. ((member-ignore-case property (cons "CATEGORY" org-special-properties))
  14470. ;; We need a special property. Use `org-entry-properties' to
  14471. ;; retrieve it, but specify the wanted property.
  14472. (cdr (assoc-string property (org-entry-properties nil property))))
  14473. ((and inherit
  14474. (or (not (eq inherit 'selective)) (org-property-inherit-p property)))
  14475. (org-entry-get-with-inheritance property literal-nil))
  14476. (t
  14477. (let* ((local (org-property--local-values property literal-nil))
  14478. (value (and local (mapconcat #'identity (delq nil local) " "))))
  14479. (if literal-nil value (org-not-nil value)))))))
  14480. (defun org-property-or-variable-value (var &optional inherit)
  14481. "Check if there is a property fixing the value of VAR.
  14482. If yes, return this value. If not, return the current value of the variable."
  14483. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  14484. (if (and prop (stringp prop) (string-match "\\S-" prop))
  14485. (read prop)
  14486. (symbol-value var))))
  14487. (defun org-entry-delete (pom property)
  14488. "Delete PROPERTY from entry at point-or-marker POM.
  14489. Accumulated properties, i.e. PROPERTY+, are also removed. Return
  14490. non-nil when a property was removed."
  14491. (unless (member property org-special-properties)
  14492. (org-with-point-at pom
  14493. (let ((range (org-get-property-block)))
  14494. (when range
  14495. (let* ((begin (car range))
  14496. (origin (cdr range))
  14497. (end (copy-marker origin))
  14498. (re (org-re-property
  14499. (concat (regexp-quote property) "\\+?") t t)))
  14500. (goto-char begin)
  14501. (while (re-search-forward re end t)
  14502. (delete-region (match-beginning 0) (line-beginning-position 2)))
  14503. ;; If drawer is empty, remove it altogether.
  14504. (when (= begin end)
  14505. (delete-region (line-beginning-position 0)
  14506. (line-beginning-position 2)))
  14507. ;; Return non-nil if some property was removed.
  14508. (prog1 (/= end origin) (set-marker end nil))))))))
  14509. ;; Multi-values properties are properties that contain multiple values
  14510. ;; These values are assumed to be single words, separated by whitespace.
  14511. (defun org-entry-add-to-multivalued-property (pom property value)
  14512. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  14513. (let* ((old (org-entry-get pom property))
  14514. (values (and old (org-split-string old "[ \t]"))))
  14515. (setq value (org-entry-protect-space value))
  14516. (unless (member value values)
  14517. (setq values (append values (list value)))
  14518. (org-entry-put pom property
  14519. (mapconcat 'identity values " ")))))
  14520. (defun org-entry-remove-from-multivalued-property (pom property value)
  14521. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  14522. (let* ((old (org-entry-get pom property))
  14523. (values (and old (org-split-string old "[ \t]"))))
  14524. (setq value (org-entry-protect-space value))
  14525. (when (member value values)
  14526. (setq values (delete value values))
  14527. (org-entry-put pom property
  14528. (mapconcat 'identity values " ")))))
  14529. (defun org-entry-member-in-multivalued-property (pom property value)
  14530. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  14531. (let* ((old (org-entry-get pom property))
  14532. (values (and old (org-split-string old "[ \t]"))))
  14533. (setq value (org-entry-protect-space value))
  14534. (member value values)))
  14535. (defun org-entry-get-multivalued-property (pom property)
  14536. "Return a list of values in a multivalued property."
  14537. (let* ((value (org-entry-get pom property))
  14538. (values (and value (org-split-string value "[ \t]"))))
  14539. (mapcar 'org-entry-restore-space values)))
  14540. (defun org-entry-put-multivalued-property (pom property &rest values)
  14541. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  14542. VALUES should be a list of strings. Spaces will be protected."
  14543. (org-entry-put pom property
  14544. (mapconcat 'org-entry-protect-space values " "))
  14545. (let* ((value (org-entry-get pom property))
  14546. (values (and value (org-split-string value "[ \t]"))))
  14547. (mapcar 'org-entry-restore-space values)))
  14548. (defun org-entry-protect-space (s)
  14549. "Protect spaces and newline in string S."
  14550. (while (string-match " " s)
  14551. (setq s (replace-match "%20" t t s)))
  14552. (while (string-match "\n" s)
  14553. (setq s (replace-match "%0A" t t s)))
  14554. s)
  14555. (defun org-entry-restore-space (s)
  14556. "Restore spaces and newline in string S."
  14557. (while (string-match "%20" s)
  14558. (setq s (replace-match " " t t s)))
  14559. (while (string-match "%0A" s)
  14560. (setq s (replace-match "\n" t t s)))
  14561. s)
  14562. (defvar org-entry-property-inherited-from (make-marker)
  14563. "Marker pointing to the entry from where a property was inherited.
  14564. Each call to `org-entry-get-with-inheritance' will set this marker to the
  14565. location of the entry where the inheritance search matched. If there was
  14566. no match, the marker will point nowhere.
  14567. Note that also `org-entry-get' calls this function, if the INHERIT flag
  14568. is set.")
  14569. (defun org-entry-get-with-inheritance (property &optional literal-nil)
  14570. "Get PROPERTY of entry or content at point, search higher levels if needed.
  14571. The search will stop at the first ancestor which has the property defined.
  14572. If the value found is \"nil\", return nil to show that the property
  14573. should be considered as undefined (this is the meaning of nil here).
  14574. However, if LITERAL-NIL is set, return the string value \"nil\" instead."
  14575. (move-marker org-entry-property-inherited-from nil)
  14576. (org-with-wide-buffer
  14577. (let (value)
  14578. (catch 'exit
  14579. (while t
  14580. (let ((v (org-property--local-values property literal-nil)))
  14581. (when v
  14582. (setq value
  14583. (concat (mapconcat #'identity (delq nil v) " ")
  14584. (and value " ")
  14585. value)))
  14586. (cond
  14587. ((car v)
  14588. (org-back-to-heading t)
  14589. (move-marker org-entry-property-inherited-from (point))
  14590. (throw 'exit nil))
  14591. ((org-up-heading-safe))
  14592. (t
  14593. (let ((global
  14594. (cdr (or (assoc-string property org-file-properties t)
  14595. (assoc-string property org-global-properties t)
  14596. (assoc-string property org-global-properties-fixed t)))))
  14597. (cond ((not global))
  14598. (value (setq value (concat global " " value)))
  14599. (t (setq value global))))
  14600. (throw 'exit nil))))))
  14601. (if literal-nil value (org-not-nil value)))))
  14602. (defvar org-property-changed-functions nil
  14603. "Hook called when the value of a property has changed.
  14604. Each hook function should accept two arguments, the name of the property
  14605. and the new value.")
  14606. (defun org-entry-put (pom property value)
  14607. "Set PROPERTY to VALUE for entry at point-or-marker POM.
  14608. If the value is nil, it is converted to the empty string. If it
  14609. is not a string, an error is raised. Also raise an error on
  14610. invalid property names.
  14611. PROPERTY can be any regular property (see
  14612. `org-special-properties'). It can also be \"TODO\",
  14613. \"PRIORITY\", \"SCHEDULED\" and \"DEADLINE\".
  14614. For the last two properties, VALUE may have any of the special
  14615. values \"earlier\" and \"later\". The function then increases or
  14616. decreases scheduled or deadline date by one day."
  14617. (cond ((null value) (setq value ""))
  14618. ((not (stringp value)) (error "Properties values should be strings"))
  14619. ((not (org--valid-property-p property))
  14620. (user-error "Invalid property name: \"%s\"" property)))
  14621. (org-with-point-at pom
  14622. (if (or (not (featurep 'org-inlinetask)) (org-inlinetask-in-task-p))
  14623. (org-back-to-heading t)
  14624. (org-with-limited-levels (org-back-to-heading t)))
  14625. (let ((beg (point)))
  14626. (cond
  14627. ((equal property "TODO")
  14628. (cond ((not (org-string-nw-p value)) (setq value 'none))
  14629. ((not (member value org-todo-keywords-1))
  14630. (user-error "\"%s\" is not a valid TODO state" value)))
  14631. (org-todo value)
  14632. (org-set-tags nil 'align))
  14633. ((equal property "PRIORITY")
  14634. (org-priority (if (org-string-nw-p value) (string-to-char value) ?\s))
  14635. (org-set-tags nil 'align))
  14636. ((equal property "SCHEDULED")
  14637. (forward-line)
  14638. (if (and (org-looking-at-p org-planning-line-re)
  14639. (re-search-forward
  14640. org-scheduled-time-regexp (line-end-position) t))
  14641. (cond ((string= value "earlier") (org-timestamp-change -1 'day))
  14642. ((string= value "later") (org-timestamp-change 1 'day))
  14643. ((string= value "") (org-schedule '(4)))
  14644. (t (org-schedule nil value)))
  14645. (if (member value '("earlier" "later" ""))
  14646. (call-interactively #'org-schedule)
  14647. (org-schedule nil value))))
  14648. ((equal property "DEADLINE")
  14649. (forward-line)
  14650. (if (and (org-looking-at-p org-planning-line-re)
  14651. (re-search-forward
  14652. org-deadline-time-regexp (line-end-position) t))
  14653. (cond ((string= value "earlier") (org-timestamp-change -1 'day))
  14654. ((string= value "later") (org-timestamp-change 1 'day))
  14655. ((string= value "") (org-deadline '(4)))
  14656. (t (org-deadline nil value)))
  14657. (if (member value '("earlier" "later" ""))
  14658. (call-interactively #'org-deadline)
  14659. (org-deadline nil value))))
  14660. ((member property org-special-properties)
  14661. (error "The %s property cannot be set with `org-entry-put'" property))
  14662. (t
  14663. (let* ((range (org-get-property-block beg 'force))
  14664. (end (cdr range))
  14665. (case-fold-search t))
  14666. (goto-char (car range))
  14667. (if (re-search-forward (org-re-property property nil t) end t)
  14668. (progn (delete-region (match-beginning 0) (match-end 0))
  14669. (goto-char (match-beginning 0)))
  14670. (goto-char end)
  14671. (insert "\n")
  14672. (backward-char))
  14673. (insert ":" property ":")
  14674. (when value (insert " " value))
  14675. (org-indent-line)))))
  14676. (run-hook-with-args 'org-property-changed-functions property value)))
  14677. (defun org-buffer-property-keys
  14678. (&optional specials defaults columns ignore-malformed)
  14679. "Get all property keys in the current buffer.
  14680. When SPECIALS is non-nil, also list the special properties that
  14681. reflect things like tags and TODO state.
  14682. When DEFAULTS is non-nil, also include properties that has
  14683. special meaning internally: ARCHIVE, CATEGORY, SUMMARY,
  14684. DESCRIPTION, LOCATION, and LOGGING and others.
  14685. When COLUMNS in non-nil, also include property names given in
  14686. COLUMN formats in the current buffer.
  14687. When IGNORE-MALFORMED is non-nil, malformed drawer repair will not be
  14688. automatically performed, such drawers will be silently ignored."
  14689. (let ((case-fold-search t)
  14690. (props (append
  14691. (and specials org-special-properties)
  14692. (and defaults (cons org-effort-property org-default-properties))
  14693. nil)))
  14694. (org-with-wide-buffer
  14695. (goto-char (point-min))
  14696. (while (re-search-forward org-property-start-re nil t)
  14697. (let ((range (org-get-property-block)))
  14698. (catch 'skip
  14699. (unless range
  14700. (when (and (not ignore-malformed)
  14701. (not (org-before-first-heading-p))
  14702. (y-or-n-p (format "Malformed drawer at %d, repair?"
  14703. (line-beginning-position))))
  14704. (org-get-property-block nil t))
  14705. (throw 'skip nil))
  14706. (goto-char (car range))
  14707. (let ((begin (car range))
  14708. (end (cdr range)))
  14709. ;; Make sure that found property block is not located
  14710. ;; before current point, as it would generate an infloop.
  14711. ;; It can happen, for example, in the following
  14712. ;; situation:
  14713. ;;
  14714. ;; * Headline
  14715. ;; :PROPERTIES:
  14716. ;; ...
  14717. ;; :END:
  14718. ;; *************** Inlinetask
  14719. ;; #+BEGIN_EXAMPLE
  14720. ;; :PROPERTIES:
  14721. ;; #+END_EXAMPLE
  14722. ;;
  14723. (if (< begin (point)) (throw 'skip nil) (goto-char begin))
  14724. (while (< (point) end)
  14725. (let ((p (progn (looking-at org-property-re)
  14726. (org-match-string-no-properties 2))))
  14727. ;; Only add true property name, not extension symbol.
  14728. (push (if (not (org-string-match-p "\\+\\'" p)) p
  14729. (substring p 0 -1))
  14730. props))
  14731. (forward-line))))
  14732. (outline-next-heading)))
  14733. (when columns
  14734. (goto-char (point-min))
  14735. (while (re-search-forward "^[ \t]*\\(?:#\\+\\|:\\)COLUMNS:" nil t)
  14736. (let ((element (org-element-at-point)))
  14737. (when (memq (org-element-type element) '(keyword node-property))
  14738. (let ((value (org-element-property :value element))
  14739. (start 0))
  14740. (while (string-match "%[0-9]*\\(\\S-+\\)" value start)
  14741. (setq start (match-end 0))
  14742. (let ((p (org-match-string-no-properties 1 value)))
  14743. (unless (member-ignore-case p org-special-properties)
  14744. (push p props))))))))))
  14745. (sort (delete-dups props) (lambda (a b) (string< (upcase a) (upcase b))))))
  14746. (defun org-property-values (key)
  14747. "List all non-nil values of property KEY in current buffer."
  14748. (org-with-wide-buffer
  14749. (goto-char (point-min))
  14750. (let ((case-fold-search t)
  14751. (re (org-re-property key))
  14752. values)
  14753. (while (re-search-forward re nil t)
  14754. (push (org-entry-get (point) key) values))
  14755. (delete-dups values))))
  14756. (defun org-insert-property-drawer ()
  14757. "Insert a property drawer into the current entry."
  14758. (org-with-wide-buffer
  14759. (if (or (not (featurep 'org-inlinetask)) (org-inlinetask-in-task-p))
  14760. (org-back-to-heading t)
  14761. (org-with-limited-levels (org-back-to-heading t)))
  14762. (forward-line)
  14763. (when (org-looking-at-p org-planning-line-re) (forward-line))
  14764. (unless (org-looking-at-p org-property-drawer-re)
  14765. ;; Make sure we start editing a line from current entry, not from
  14766. ;; next one. It prevents extending text properties or overlays
  14767. ;; belonging to the latter.
  14768. (when (bolp) (backward-char))
  14769. (let ((begin (1+ (point)))
  14770. (inhibit-read-only t))
  14771. (insert "\n:PROPERTIES:\n:END:")
  14772. (when (eobp) (insert "\n"))
  14773. (org-indent-region begin (point))))))
  14774. (defun org-insert-drawer (&optional arg drawer)
  14775. "Insert a drawer at point.
  14776. When optional argument ARG is non-nil, insert a property drawer.
  14777. Optional argument DRAWER, when non-nil, is a string representing
  14778. drawer's name. Otherwise, the user is prompted for a name.
  14779. If a region is active, insert the drawer around that region
  14780. instead.
  14781. Point is left between drawer's boundaries."
  14782. (interactive "P")
  14783. (let* ((drawer (if arg "PROPERTIES"
  14784. (or drawer (read-from-minibuffer "Drawer: ")))))
  14785. (cond
  14786. ;; With C-u, fall back on `org-insert-property-drawer'
  14787. (arg (org-insert-property-drawer))
  14788. ;; Check validity of suggested drawer's name.
  14789. ((not (org-string-match-p org-drawer-regexp (format ":%s:" drawer)))
  14790. (user-error "Invalid drawer name"))
  14791. ;; With an active region, insert a drawer at point.
  14792. ((not (org-region-active-p))
  14793. (progn
  14794. (unless (bolp) (insert "\n"))
  14795. (insert (format ":%s:\n\n:END:\n" drawer))
  14796. (forward-line -2)))
  14797. ;; Otherwise, insert the drawer at point
  14798. (t
  14799. (let ((rbeg (region-beginning))
  14800. (rend (copy-marker (region-end))))
  14801. (unwind-protect
  14802. (progn
  14803. (goto-char rbeg)
  14804. (beginning-of-line)
  14805. (when (save-excursion
  14806. (re-search-forward org-outline-regexp-bol rend t))
  14807. (user-error "Drawers cannot contain headlines"))
  14808. ;; Position point at the beginning of the first
  14809. ;; non-blank line in region. Insert drawer's opening
  14810. ;; there, then indent it.
  14811. (org-skip-whitespace)
  14812. (beginning-of-line)
  14813. (insert ":" drawer ":\n")
  14814. (forward-line -1)
  14815. (indent-for-tab-command)
  14816. ;; Move point to the beginning of the first blank line
  14817. ;; after the last non-blank line in region. Insert
  14818. ;; drawer's closing, then indent it.
  14819. (goto-char rend)
  14820. (skip-chars-backward " \r\t\n")
  14821. (insert "\n:END:")
  14822. (deactivate-mark t)
  14823. (indent-for-tab-command)
  14824. (unless (eolp) (insert "\n")))
  14825. ;; Clear marker, whatever the outcome of insertion is.
  14826. (set-marker rend nil)))))))
  14827. (defvar org-property-set-functions-alist nil
  14828. "Property set function alist.
  14829. Each entry should have the following format:
  14830. (PROPERTY . READ-FUNCTION)
  14831. The read function will be called with the same argument as
  14832. `org-completing-read'.")
  14833. (defun org-set-property-function (property)
  14834. "Get the function that should be used to set PROPERTY.
  14835. This is computed according to `org-property-set-functions-alist'."
  14836. (or (cdr (assoc property org-property-set-functions-alist))
  14837. 'org-completing-read))
  14838. (defun org-read-property-value (property)
  14839. "Read PROPERTY value from user."
  14840. (let* ((completion-ignore-case t)
  14841. (allowed (org-property-get-allowed-values nil property 'table))
  14842. (cur (org-entry-get nil property))
  14843. (prompt (concat property " value"
  14844. (if (and cur (string-match "\\S-" cur))
  14845. (concat " [" cur "]") "") ": "))
  14846. (set-function (org-set-property-function property))
  14847. (val (if allowed
  14848. (funcall set-function prompt allowed nil
  14849. (not (get-text-property 0 'org-unrestricted
  14850. (caar allowed))))
  14851. (funcall set-function prompt
  14852. (mapcar 'list (org-property-values property))
  14853. nil nil "" nil cur))))
  14854. (org-trim val)))
  14855. (defvar org-last-set-property nil)
  14856. (defvar org-last-set-property-value nil)
  14857. (defun org-read-property-name ()
  14858. "Read a property name."
  14859. (let ((completion-ignore-case t)
  14860. (default-prop (or (and (org-at-property-p)
  14861. (org-match-string-no-properties 2))
  14862. org-last-set-property)))
  14863. (org-completing-read
  14864. (concat "Property"
  14865. (if default-prop (concat " [" default-prop "]") "")
  14866. ": ")
  14867. (mapcar #'list (org-buffer-property-keys nil t t))
  14868. nil nil nil nil default-prop)))
  14869. (defun org-set-property-and-value (use-last)
  14870. "Allow to set [PROPERTY]: [value] direction from prompt.
  14871. When use-default, don't even ask, just use the last
  14872. \"[PROPERTY]: [value]\" string from the history."
  14873. (interactive "P")
  14874. (let* ((completion-ignore-case t)
  14875. (pv (or (and use-last org-last-set-property-value)
  14876. (org-completing-read
  14877. "Enter a \"[Property]: [value]\" pair: "
  14878. nil nil nil nil nil
  14879. org-last-set-property-value)))
  14880. prop val)
  14881. (when (string-match "^[ \t]*\\([^:]+\\):[ \t]*\\(.*\\)[ \t]*$" pv)
  14882. (setq prop (match-string 1 pv)
  14883. val (match-string 2 pv))
  14884. (org-set-property prop val))))
  14885. (defun org-set-property (property value)
  14886. "In the current entry, set PROPERTY to VALUE.
  14887. When called interactively, this will prompt for a property name, offering
  14888. completion on existing and default properties. And then it will prompt
  14889. for a value, offering completion either on allowed values (via an inherited
  14890. xxx_ALL property) or on existing values in other instances of this property
  14891. in the current file.
  14892. Throw an error when trying to set a property with an invalid name."
  14893. (interactive (list nil nil))
  14894. (let ((property (or property (org-read-property-name))))
  14895. ;; `org-entry-put' also makes the following check, but this one
  14896. ;; avoids polluting `org-last-set-property' and
  14897. ;; `org-last-set-property-value' needlessly.
  14898. (unless (org--valid-property-p property)
  14899. (user-error "Invalid property name: \"%s\"" property))
  14900. (let ((value (or value (org-read-property-value property)))
  14901. (fn (cdr (assoc-string property org-properties-postprocess-alist t))))
  14902. (setq org-last-set-property property)
  14903. (setq org-last-set-property-value (concat property ": " value))
  14904. ;; Possibly postprocess the inserted value:
  14905. (when fn (setq value (funcall fn value)))
  14906. (unless (equal (org-entry-get nil property) value)
  14907. (org-entry-put nil property value)))))
  14908. (defun org-find-property (property &optional value)
  14909. "Find first entry in buffer that sets PROPERTY.
  14910. When optional argument VALUE is non-nil, only consider an entry
  14911. if it contains PROPERTY set to this value. If PROPERTY should be
  14912. explicitly set to nil, use string \"nil\" for VALUE.
  14913. Return position where the entry begins, or nil if there is no
  14914. such entry. If narrowing is in effect, only search the visible
  14915. part of the buffer."
  14916. (save-excursion
  14917. (goto-char (point-min))
  14918. (let ((case-fold-search t)
  14919. (re (org-re-property property nil (not value) value)))
  14920. (catch 'exit
  14921. (while (re-search-forward re nil t)
  14922. (when (if value (org-at-property-p)
  14923. (org-entry-get (point) property nil t))
  14924. (throw 'exit (progn (org-back-to-heading t) (point)))))))))
  14925. (defun org-delete-property (property)
  14926. "In the current entry, delete PROPERTY."
  14927. (interactive
  14928. (let* ((completion-ignore-case t)
  14929. (cat (org-entry-get (point) "CATEGORY"))
  14930. (props0 (org-entry-properties nil 'standard))
  14931. (props (if cat props0
  14932. (delete `("CATEGORY" . ,(org-get-category)) props0)))
  14933. (prop (if (< 1 (length props))
  14934. (completing-read "Property: " props nil t)
  14935. (caar props))))
  14936. (list prop)))
  14937. (if (not property)
  14938. (message "No property to delete in this entry")
  14939. (org-entry-delete nil property)
  14940. (message "Property \"%s\" deleted" property)))
  14941. (defun org-delete-property-globally (property)
  14942. "Remove PROPERTY globally, from all entries.
  14943. This function ignores narrowing, if any."
  14944. (interactive
  14945. (let* ((completion-ignore-case t)
  14946. (prop (completing-read
  14947. "Globally remove property: "
  14948. (mapcar #'list (org-buffer-property-keys)))))
  14949. (list prop)))
  14950. (org-with-wide-buffer
  14951. (goto-char (point-min))
  14952. (let ((count 0)
  14953. (re (org-re-property (concat (regexp-quote property) "\\+?") t t)))
  14954. (while (re-search-forward re nil t)
  14955. (when (org-entry-delete (point) property) (cl-incf count)))
  14956. (message "Property \"%s\" removed from %d entries" property count))))
  14957. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  14958. (defun org-compute-property-at-point ()
  14959. "Compute the property at point.
  14960. This looks for an enclosing column format, extracts the operator and
  14961. then applies it to the property in the column format's scope."
  14962. (interactive)
  14963. (unless (org-at-property-p)
  14964. (user-error "Not at a property"))
  14965. (let ((prop (org-match-string-no-properties 2)))
  14966. (org-columns-get-format-and-top-level)
  14967. (unless (nth 3 (assoc-string prop org-columns-current-fmt-compiled t))
  14968. (user-error "No operator defined for property %s" prop))
  14969. (org-columns-compute prop)))
  14970. (defvar org-property-allowed-value-functions nil
  14971. "Hook for functions supplying allowed values for a specific property.
  14972. The functions must take a single argument, the name of the property, and
  14973. return a flat list of allowed values. If \":ETC\" is one of
  14974. the values, this means that these values are intended as defaults for
  14975. completion, but that other values should be allowed too.
  14976. The functions must return nil if they are not responsible for this
  14977. property.")
  14978. (defun org-property-get-allowed-values (pom property &optional table)
  14979. "Get allowed values for the property PROPERTY.
  14980. When TABLE is non-nil, return an alist that can directly be used for
  14981. completion."
  14982. (let (vals)
  14983. (cond
  14984. ((equal property "TODO")
  14985. (setq vals (org-with-point-at pom
  14986. (append org-todo-keywords-1 '("")))))
  14987. ((equal property "PRIORITY")
  14988. (let ((n org-lowest-priority))
  14989. (while (>= n org-highest-priority)
  14990. (push (char-to-string n) vals)
  14991. (setq n (1- n)))))
  14992. ((equal property "CATEGORY"))
  14993. ((member property org-special-properties))
  14994. ((setq vals (run-hook-with-args-until-success
  14995. 'org-property-allowed-value-functions property)))
  14996. (t
  14997. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  14998. (when (and vals (string-match "\\S-" vals))
  14999. (setq vals (car (read-from-string (concat "(" vals ")"))))
  15000. (setq vals (mapcar (lambda (x)
  15001. (cond ((stringp x) x)
  15002. ((numberp x) (number-to-string x))
  15003. ((symbolp x) (symbol-name x))
  15004. (t "???")))
  15005. vals)))))
  15006. (when (member ":ETC" vals)
  15007. (setq vals (remove ":ETC" vals))
  15008. (org-add-props (car vals) '(org-unrestricted t)))
  15009. (if table (mapcar 'list vals) vals)))
  15010. (defun org-property-previous-allowed-value (&optional _previous)
  15011. "Switch to the next allowed value for this property."
  15012. (interactive)
  15013. (org-property-next-allowed-value t))
  15014. (defun org-property-next-allowed-value (&optional previous)
  15015. "Switch to the next allowed value for this property."
  15016. (interactive)
  15017. (unless (org-at-property-p)
  15018. (user-error "Not at a property"))
  15019. (let* ((prop (car (save-match-data (org-split-string (match-string 1) ":"))))
  15020. (key (match-string 2))
  15021. (value (match-string 3))
  15022. (allowed (or (org-property-get-allowed-values (point) key)
  15023. (and (member value '("[ ]" "[-]" "[X]"))
  15024. '("[ ]" "[X]"))))
  15025. (heading (save-match-data (nth 4 (org-heading-components))))
  15026. nval)
  15027. (unless allowed
  15028. (user-error "Allowed values for this property have not been defined"))
  15029. (when previous (setq allowed (reverse allowed)))
  15030. (when (member value allowed)
  15031. (setq nval (car (cdr (member value allowed)))))
  15032. (setq nval (or nval (car allowed)))
  15033. (when (equal nval value)
  15034. (user-error "Only one allowed value for this property"))
  15035. (org-at-property-p)
  15036. (replace-match (concat " :" key ": " nval) t t)
  15037. (org-indent-line)
  15038. (beginning-of-line 1)
  15039. (skip-chars-forward " \t")
  15040. (when (equal prop org-effort-property)
  15041. (org-refresh-property
  15042. '((effort . identity)
  15043. (effort-minutes . org-duration-string-to-minutes))
  15044. nval)
  15045. (when (string= org-clock-current-task heading)
  15046. (setq org-clock-effort nval)
  15047. (org-clock-update-mode-line)))
  15048. (run-hook-with-args 'org-property-changed-functions key nval)))
  15049. (defun org-find-olp (path &optional this-buffer)
  15050. "Return a marker pointing to the entry at outline path OLP.
  15051. If anything goes wrong, throw an error.
  15052. You can wrap this call to catch the error like this:
  15053. \(condition-case msg
  15054. \(org-mobile-locate-entry (match-string 4))
  15055. \(error (nth 1 msg)))
  15056. The return value will then be either a string with the error message,
  15057. or a marker if everything is OK.
  15058. If THIS-BUFFER is set, the outline path does not contain a file,
  15059. only headings."
  15060. (let* ((file (if this-buffer buffer-file-name (pop path)))
  15061. (buffer (if this-buffer (current-buffer) (find-file-noselect file)))
  15062. (level 1)
  15063. (lmin 1)
  15064. (lmax 1)
  15065. end found flevel)
  15066. (unless buffer (error "File not found :%s" file))
  15067. (with-current-buffer buffer
  15068. (org-with-wide-buffer
  15069. (goto-char (point-min))
  15070. (dolist (heading path)
  15071. (let ((re (format org-complex-heading-regexp-format
  15072. (regexp-quote heading)))
  15073. (cnt 0))
  15074. (while (re-search-forward re end t)
  15075. (setq level (- (match-end 1) (match-beginning 1)))
  15076. (when (and (>= level lmin) (<= level lmax))
  15077. (setq found (match-beginning 0) flevel level cnt (1+ cnt))))
  15078. (when (= cnt 0)
  15079. (error "Heading not found on level %d: %s" lmax heading))
  15080. (when (> cnt 1)
  15081. (error "Heading not unique on level %d: %s" lmax heading))
  15082. (goto-char found)
  15083. (setq lmin (1+ flevel) lmax (+ lmin (if org-odd-levels-only 1 0)))
  15084. (setq end (save-excursion (org-end-of-subtree t t)))))
  15085. (when (org-at-heading-p)
  15086. (point-marker))))))
  15087. (defun org-find-exact-headline-in-buffer (heading &optional buffer pos-only)
  15088. "Find node HEADING in BUFFER.
  15089. Return a marker to the heading if it was found, or nil if not.
  15090. If POS-ONLY is set, return just the position instead of a marker.
  15091. The heading text must match exact, but it may have a TODO keyword,
  15092. a priority cookie and tags in the standard locations."
  15093. (with-current-buffer (or buffer (current-buffer))
  15094. (save-excursion
  15095. (save-restriction
  15096. (widen)
  15097. (goto-char (point-min))
  15098. (let (case-fold-search)
  15099. (when (re-search-forward
  15100. (format org-complex-heading-regexp-format
  15101. (regexp-quote heading)) nil t)
  15102. (if pos-only
  15103. (match-beginning 0)
  15104. (move-marker (make-marker) (match-beginning 0)))))))))
  15105. (defun org-find-exact-heading-in-directory (heading &optional dir)
  15106. "Find Org node headline HEADING in all .org files in directory DIR.
  15107. When the target headline is found, return a marker to this location."
  15108. (let ((files (directory-files (or dir default-directory)
  15109. t "\\`[^.#].*\\.org\\'"))
  15110. visiting m buffer)
  15111. (catch 'found
  15112. (dolist (file files)
  15113. (message "trying %s" file)
  15114. (setq visiting (org-find-base-buffer-visiting file))
  15115. (setq buffer (or visiting (find-file-noselect file)))
  15116. (setq m (org-find-exact-headline-in-buffer
  15117. heading buffer))
  15118. (when (and (not m) (not visiting)) (kill-buffer buffer))
  15119. (and m (throw 'found m))))))
  15120. (defun org-find-entry-with-id (ident)
  15121. "Locate the entry that contains the ID property with exact value IDENT.
  15122. IDENT can be a string, a symbol or a number, this function will search for
  15123. the string representation of it.
  15124. Return the position where this entry starts, or nil if there is no such entry."
  15125. (interactive "sID: ")
  15126. (let ((id (cond
  15127. ((stringp ident) ident)
  15128. ((symbolp ident) (symbol-name ident))
  15129. ((numberp ident) (number-to-string ident))
  15130. (t (error "IDENT %s must be a string, symbol or number" ident)))))
  15131. (org-with-wide-buffer (org-find-property "ID" id))))
  15132. ;;;; Timestamps
  15133. (defvar org-last-changed-timestamp nil)
  15134. (defvar org-last-inserted-timestamp nil
  15135. "The last time stamp inserted with `org-insert-time-stamp'.")
  15136. (defvar org-ts-what) ; dynamically scoped parameter
  15137. (defun org-time-stamp (arg &optional inactive)
  15138. "Prompt for a date/time and insert a time stamp.
  15139. If the user specifies a time like HH:MM or if this command is
  15140. called with at least one prefix argument, the time stamp contains
  15141. the date and the time. Otherwise, only the date is included.
  15142. All parts of a date not specified by the user are filled in from
  15143. the timestamp at point, if any, or the current date/time
  15144. otherwise.
  15145. If there is already a timestamp at the cursor, it is replaced.
  15146. With two universal prefix arguments, insert an active timestamp
  15147. with the current time without prompting the user.
  15148. When called from lisp, the timestamp is inactive if INACTIVE is
  15149. non-nil."
  15150. (interactive "P")
  15151. (let* ((ts (cond
  15152. ((org-at-date-range-p t)
  15153. (match-string (if (< (point) (- (match-beginning 2) 2)) 1 2)))
  15154. ((org-at-timestamp-p t) (match-string 0))))
  15155. ;; Default time is either the timestamp at point or today.
  15156. ;; When entering a range, only the range start is considered.
  15157. (default-time (if (not ts) (current-time)
  15158. (apply #'encode-time (org-parse-time-string ts))))
  15159. (default-input (and ts (org-get-compact-tod ts)))
  15160. (repeater (and ts
  15161. (string-match "\\([.+-]+[0-9]+[hdwmy] ?\\)+" ts)
  15162. (match-string 0 ts)))
  15163. org-time-was-given
  15164. org-end-time-was-given
  15165. (time
  15166. (and (if (equal arg '(16)) (current-time)
  15167. ;; Preserve `this-command' and `last-command'.
  15168. (let ((this-command this-command)
  15169. (last-command last-command))
  15170. (org-read-date
  15171. arg 'totime nil nil default-time default-input
  15172. inactive))))))
  15173. (cond
  15174. ((and ts
  15175. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  15176. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  15177. (insert "--")
  15178. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  15179. (ts
  15180. ;; Make sure we're on a timestamp. When in the middle of a date
  15181. ;; range, move arbitrarily to range end.
  15182. (unless (org-at-timestamp-p t)
  15183. (skip-chars-forward "-")
  15184. (org-at-timestamp-p t))
  15185. (replace-match "")
  15186. (setq org-last-changed-timestamp
  15187. (org-insert-time-stamp
  15188. time (or org-time-was-given arg)
  15189. inactive nil nil (list org-end-time-was-given)))
  15190. (when repeater
  15191. (backward-char)
  15192. (insert " " repeater)
  15193. (setq org-last-changed-timestamp
  15194. (concat (substring org-last-inserted-timestamp 0 -1)
  15195. " " repeater ">")))
  15196. (message "Timestamp updated"))
  15197. ((equal arg '(16)) (org-insert-time-stamp time t inactive))
  15198. (t (org-insert-time-stamp
  15199. time (or org-time-was-given arg) inactive nil nil
  15200. (list org-end-time-was-given))))))
  15201. ;; FIXME: can we use this for something else, like computing time differences?
  15202. (defun org-get-compact-tod (s)
  15203. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  15204. (let* ((t1 (match-string 1 s))
  15205. (h1 (string-to-number (match-string 2 s)))
  15206. (m1 (string-to-number (match-string 3 s)))
  15207. (t2 (and (match-end 4) (match-string 5 s)))
  15208. (h2 (and t2 (string-to-number (match-string 6 s))))
  15209. (m2 (and t2 (string-to-number (match-string 7 s))))
  15210. dh dm)
  15211. (if (not t2)
  15212. t1
  15213. (setq dh (- h2 h1) dm (- m2 m1))
  15214. (when (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  15215. (concat t1 "+" (number-to-string dh)
  15216. (and (/= 0 dm) (format ":%02d" dm)))))))
  15217. (defun org-time-stamp-inactive (&optional arg)
  15218. "Insert an inactive time stamp.
  15219. An inactive time stamp is enclosed in square brackets instead of angle
  15220. brackets. It is inactive in the sense that it does not trigger agenda entries,
  15221. does not link to the calendar and cannot be changed with the S-cursor keys.
  15222. So these are more for recording a certain time/date."
  15223. (interactive "P")
  15224. (org-time-stamp arg 'inactive))
  15225. (defvar org-date-ovl (make-overlay 1 1))
  15226. (overlay-put org-date-ovl 'face 'org-date-selected)
  15227. (org-detach-overlay org-date-ovl)
  15228. (defvar org-ans1) ; dynamically scoped parameter
  15229. (defvar org-ans2) ; dynamically scoped parameter
  15230. (defvar org-plain-time-of-day-regexp) ; defined below
  15231. (defvar org-overriding-default-time nil) ; dynamically scoped
  15232. (defvar org-read-date-overlay nil)
  15233. (defvar org-dcst nil) ; dynamically scoped
  15234. (defvar org-read-date-history nil)
  15235. (defvar org-read-date-final-answer nil)
  15236. (defvar org-read-date-analyze-futurep nil)
  15237. (defvar org-read-date-analyze-forced-year nil)
  15238. (defvar org-read-date-inactive)
  15239. (defvar org-read-date-minibuffer-local-map
  15240. (let* ((map (make-sparse-keymap)))
  15241. (set-keymap-parent map minibuffer-local-map)
  15242. (org-defkey map (kbd ".")
  15243. (lambda () (interactive)
  15244. ;; Are we at the beginning of the prompt?
  15245. (if (org-looking-back "^[^:]+: "
  15246. (let ((inhibit-field-text-motion t))
  15247. (line-beginning-position)))
  15248. (org-eval-in-calendar '(calendar-goto-today))
  15249. (insert "."))))
  15250. (org-defkey map (kbd "C-.")
  15251. (lambda () (interactive)
  15252. (org-eval-in-calendar '(calendar-goto-today))))
  15253. (org-defkey map [(meta shift left)]
  15254. (lambda () (interactive)
  15255. (org-eval-in-calendar '(calendar-backward-month 1))))
  15256. (org-defkey map [(meta shift right)]
  15257. (lambda () (interactive)
  15258. (org-eval-in-calendar '(calendar-forward-month 1))))
  15259. (org-defkey map [(meta shift up)]
  15260. (lambda () (interactive)
  15261. (org-eval-in-calendar '(calendar-backward-year 1))))
  15262. (org-defkey map [(meta shift down)]
  15263. (lambda () (interactive)
  15264. (org-eval-in-calendar '(calendar-forward-year 1))))
  15265. (org-defkey map [?\e (shift left)]
  15266. (lambda () (interactive)
  15267. (org-eval-in-calendar '(calendar-backward-month 1))))
  15268. (org-defkey map [?\e (shift right)]
  15269. (lambda () (interactive)
  15270. (org-eval-in-calendar '(calendar-forward-month 1))))
  15271. (org-defkey map [?\e (shift up)]
  15272. (lambda () (interactive)
  15273. (org-eval-in-calendar '(calendar-backward-year 1))))
  15274. (org-defkey map [?\e (shift down)]
  15275. (lambda () (interactive)
  15276. (org-eval-in-calendar '(calendar-forward-year 1))))
  15277. (org-defkey map [(shift up)]
  15278. (lambda () (interactive)
  15279. (org-eval-in-calendar '(calendar-backward-week 1))))
  15280. (org-defkey map [(shift down)]
  15281. (lambda () (interactive)
  15282. (org-eval-in-calendar '(calendar-forward-week 1))))
  15283. (org-defkey map [(shift left)]
  15284. (lambda () (interactive)
  15285. (org-eval-in-calendar '(calendar-backward-day 1))))
  15286. (org-defkey map [(shift right)]
  15287. (lambda () (interactive)
  15288. (org-eval-in-calendar '(calendar-forward-day 1))))
  15289. (org-defkey map "!"
  15290. (lambda () (interactive)
  15291. (org-eval-in-calendar '(diary-view-entries))
  15292. (message "")))
  15293. (org-defkey map ">"
  15294. (lambda () (interactive)
  15295. (org-eval-in-calendar '(calendar-scroll-left 1))))
  15296. (org-defkey map "<"
  15297. (lambda () (interactive)
  15298. (org-eval-in-calendar '(calendar-scroll-right 1))))
  15299. (org-defkey map "\C-v"
  15300. (lambda () (interactive)
  15301. (org-eval-in-calendar
  15302. '(calendar-scroll-left-three-months 1))))
  15303. (org-defkey map "\M-v"
  15304. (lambda () (interactive)
  15305. (org-eval-in-calendar
  15306. '(calendar-scroll-right-three-months 1))))
  15307. map)
  15308. "Keymap for minibuffer commands when using `org-read-date'.")
  15309. (defvar org-def)
  15310. (defvar org-defdecode)
  15311. (defvar org-with-time)
  15312. (defvar calendar-setup) ; Dynamically scoped.
  15313. (defun org-read-date (&optional with-time to-time from-string prompt
  15314. default-time default-input inactive)
  15315. "Read a date, possibly a time, and make things smooth for the user.
  15316. The prompt will suggest to enter an ISO date, but you can also enter anything
  15317. which will at least partially be understood by `parse-time-string'.
  15318. Unrecognized parts of the date will default to the current day, month, year,
  15319. hour and minute. If this command is called to replace a timestamp at point,
  15320. or to enter the second timestamp of a range, the default time is taken
  15321. from the existing stamp. Furthermore, the command prefers the future,
  15322. so if you are giving a date where the year is not given, and the day-month
  15323. combination is already past in the current year, it will assume you
  15324. mean next year. For details, see the manual. A few examples:
  15325. 3-2-5 --> 2003-02-05
  15326. feb 15 --> currentyear-02-15
  15327. 2/15 --> currentyear-02-15
  15328. sep 12 9 --> 2009-09-12
  15329. 12:45 --> today 12:45
  15330. 22 sept 0:34 --> currentyear-09-22 0:34
  15331. 12 --> currentyear-currentmonth-12
  15332. Fri --> nearest Friday after today
  15333. -Tue --> last Tuesday
  15334. etc.
  15335. Furthermore you can specify a relative date by giving, as the *first* thing
  15336. in the input: a plus/minus sign, a number and a letter [hdwmy] to indicate
  15337. change in days weeks, months, years.
  15338. With a single plus or minus, the date is relative to today. With a double
  15339. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  15340. +4d --> four days from today
  15341. +4 --> same as above
  15342. +2w --> two weeks from today
  15343. ++5 --> five days from default date
  15344. The function understands only English month and weekday abbreviations.
  15345. While prompting, a calendar is popped up - you can also select the
  15346. date with the mouse (button 1). The calendar shows a period of three
  15347. months. To scroll it to other months, use the keys `>' and `<'.
  15348. If you don't like the calendar, turn it off with
  15349. (setq org-read-date-popup-calendar nil)
  15350. With optional argument TO-TIME, the date will immediately be converted
  15351. to an internal time.
  15352. With an optional argument WITH-TIME, the prompt will suggest to
  15353. also insert a time. Note that when WITH-TIME is not set, you can
  15354. still enter a time, and this function will inform the calling routine
  15355. about this change. The calling routine may then choose to change the
  15356. format used to insert the time stamp into the buffer to include the time.
  15357. With optional argument FROM-STRING, read from this string instead from
  15358. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  15359. the time/date that is used for everything that is not specified by the
  15360. user."
  15361. (require 'parse-time)
  15362. (let* ((org-with-time with-time)
  15363. (org-time-stamp-rounding-minutes
  15364. (if (equal org-with-time '(16))
  15365. '(0 0)
  15366. org-time-stamp-rounding-minutes))
  15367. (org-dcst org-display-custom-times)
  15368. (ct (org-current-time))
  15369. (org-def (or org-overriding-default-time default-time ct))
  15370. (org-defdecode (decode-time org-def))
  15371. (cur-frame (selected-frame))
  15372. (mouse-autoselect-window nil) ; Don't let the mouse jump
  15373. (calendar-setup
  15374. (and (eq calendar-setup 'calendar-only) 'calendar-only))
  15375. (calendar-move-hook nil)
  15376. (calendar-view-diary-initially-flag nil)
  15377. (calendar-view-holidays-initially-flag nil)
  15378. ans (org-ans0 "") org-ans1 org-ans2 final cal-frame)
  15379. ;; Rationalize `org-def' and `org-defdecode', if required.
  15380. (when (< (nth 2 org-defdecode) org-extend-today-until)
  15381. (setf (nth 2 org-defdecode) -1)
  15382. (setf (nth 1 org-defdecode) 59)
  15383. (setq org-def (apply #'encode-time org-defdecode))
  15384. (setq org-defdecode (decode-time org-def)))
  15385. (let* ((timestr (format-time-string
  15386. (if org-with-time "%Y-%m-%d %H:%M" "%Y-%m-%d")
  15387. org-def))
  15388. (prompt (concat (if prompt (concat prompt " ") "")
  15389. (format "Date+time [%s]: " timestr))))
  15390. (cond
  15391. (from-string (setq ans from-string))
  15392. (org-read-date-popup-calendar
  15393. (save-excursion
  15394. (save-window-excursion
  15395. (calendar)
  15396. (when (eq calendar-setup 'calendar-only)
  15397. (setq cal-frame
  15398. (window-frame (get-buffer-window "*Calendar*" 'visible)))
  15399. (select-frame cal-frame))
  15400. (org-eval-in-calendar '(setq cursor-type nil) t)
  15401. (unwind-protect
  15402. (progn
  15403. (calendar-forward-day (- (time-to-days org-def)
  15404. (calendar-absolute-from-gregorian
  15405. (calendar-current-date))))
  15406. (org-eval-in-calendar nil t)
  15407. (let* ((old-map (current-local-map))
  15408. (map (copy-keymap calendar-mode-map))
  15409. (minibuffer-local-map
  15410. (copy-keymap org-read-date-minibuffer-local-map)))
  15411. (org-defkey map (kbd "RET") 'org-calendar-select)
  15412. (org-defkey map [mouse-1] 'org-calendar-select-mouse)
  15413. (org-defkey map [mouse-2] 'org-calendar-select-mouse)
  15414. (unwind-protect
  15415. (progn
  15416. (use-local-map map)
  15417. (setq org-read-date-inactive inactive)
  15418. (add-hook 'post-command-hook 'org-read-date-display)
  15419. (setq org-ans0
  15420. (read-string prompt
  15421. default-input
  15422. 'org-read-date-history
  15423. nil))
  15424. ;; org-ans0: from prompt
  15425. ;; org-ans1: from mouse click
  15426. ;; org-ans2: from calendar motion
  15427. (setq ans
  15428. (concat org-ans0 " " (or org-ans1 org-ans2))))
  15429. (remove-hook 'post-command-hook 'org-read-date-display)
  15430. (use-local-map old-map)
  15431. (when org-read-date-overlay
  15432. (delete-overlay org-read-date-overlay)
  15433. (setq org-read-date-overlay nil)))))
  15434. (bury-buffer "*Calendar*")
  15435. (when cal-frame
  15436. (delete-frame cal-frame)
  15437. (select-frame-set-input-focus cur-frame))))))
  15438. (t ; Naked prompt only
  15439. (unwind-protect
  15440. (setq ans (read-string prompt default-input
  15441. 'org-read-date-history timestr))
  15442. (when org-read-date-overlay
  15443. (delete-overlay org-read-date-overlay)
  15444. (setq org-read-date-overlay nil))))))
  15445. (setq final (org-read-date-analyze ans org-def org-defdecode))
  15446. (when org-read-date-analyze-forced-year
  15447. (message "Year was forced into %s"
  15448. (if org-read-date-force-compatible-dates
  15449. "compatible range (1970-2037)"
  15450. "range representable on this machine"))
  15451. (ding))
  15452. ;; One round trip to get rid of 34th of August and stuff like that....
  15453. (setq final (decode-time (apply 'encode-time final)))
  15454. (setq org-read-date-final-answer ans)
  15455. (if to-time
  15456. (apply 'encode-time final)
  15457. (if (and (boundp 'org-time-was-given) org-time-was-given)
  15458. (format "%04d-%02d-%02d %02d:%02d"
  15459. (nth 5 final) (nth 4 final) (nth 3 final)
  15460. (nth 2 final) (nth 1 final))
  15461. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  15462. (defun org-read-date-display ()
  15463. "Display the current date prompt interpretation in the minibuffer."
  15464. (when org-read-date-display-live
  15465. (when org-read-date-overlay
  15466. (delete-overlay org-read-date-overlay))
  15467. (when (minibufferp (current-buffer))
  15468. (save-excursion
  15469. (end-of-line 1)
  15470. (while (not (equal (buffer-substring
  15471. (max (point-min) (- (point) 4)) (point))
  15472. " "))
  15473. (insert " ")))
  15474. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  15475. " " (or org-ans1 org-ans2)))
  15476. (org-end-time-was-given nil)
  15477. (f (org-read-date-analyze ans org-def org-defdecode))
  15478. (fmts (if org-dcst
  15479. org-time-stamp-custom-formats
  15480. org-time-stamp-formats))
  15481. (fmt (if (or org-with-time
  15482. (and (boundp 'org-time-was-given) org-time-was-given))
  15483. (cdr fmts)
  15484. (car fmts)))
  15485. (txt (format-time-string fmt (apply 'encode-time f)))
  15486. (txt (if org-read-date-inactive (concat "[" (substring txt 1 -1) "]") txt))
  15487. (txt (concat "=> " txt)))
  15488. (when (and org-end-time-was-given
  15489. (string-match org-plain-time-of-day-regexp txt))
  15490. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  15491. org-end-time-was-given
  15492. (substring txt (match-end 0)))))
  15493. (when org-read-date-analyze-futurep
  15494. (setq txt (concat txt " (=>F)")))
  15495. (setq org-read-date-overlay
  15496. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  15497. (org-overlay-display org-read-date-overlay txt 'secondary-selection)))))
  15498. (defun org-read-date-analyze (ans def defdecode)
  15499. "Analyze the combined answer of the date prompt."
  15500. ;; FIXME: cleanup and comment
  15501. ;; Pass `current-time' result to `decode-time' (instead of calling
  15502. ;; without arguments) so that only `current-time' has to be
  15503. ;; overriden in tests.
  15504. (let ((org-def def)
  15505. (org-defdecode defdecode)
  15506. (nowdecode (decode-time (current-time)))
  15507. delta deltan deltaw deltadef year month day
  15508. hour minute second wday pm h2 m2 tl wday1
  15509. iso-year iso-weekday iso-week iso-date futurep kill-year)
  15510. (setq org-read-date-analyze-futurep nil
  15511. org-read-date-analyze-forced-year nil)
  15512. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  15513. (setq ans "+0"))
  15514. (when (setq delta (org-read-date-get-relative ans (current-time) org-def))
  15515. (setq ans (replace-match "" t t ans)
  15516. deltan (car delta)
  15517. deltaw (nth 1 delta)
  15518. deltadef (nth 2 delta)))
  15519. ;; Check if there is an iso week date in there. If yes, store the
  15520. ;; info and postpone interpreting it until the rest of the parsing
  15521. ;; is done.
  15522. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  15523. (setq iso-year (when (match-end 1)
  15524. (org-small-year-to-year
  15525. (string-to-number (match-string 1 ans))))
  15526. iso-weekday (when (match-end 3)
  15527. (string-to-number (match-string 3 ans)))
  15528. iso-week (string-to-number (match-string 2 ans)))
  15529. (setq ans (replace-match "" t t ans)))
  15530. ;; Help matching ISO dates with single digit month or day, like 2006-8-11.
  15531. (when (string-match
  15532. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  15533. (setq year (if (match-end 2)
  15534. (string-to-number (match-string 2 ans))
  15535. (progn (setq kill-year t)
  15536. (string-to-number (format-time-string "%Y"))))
  15537. month (string-to-number (match-string 3 ans))
  15538. day (string-to-number (match-string 4 ans)))
  15539. (when (< year 100) (setq year (+ 2000 year)))
  15540. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  15541. t nil ans)))
  15542. ;; Help matching dotted european dates
  15543. (when (string-match
  15544. "^ *\\(3[01]\\|0?[1-9]\\|[12][0-9]\\)\\. ?\\(0?[1-9]\\|1[012]\\)\\.\\( ?[1-9][0-9]\\{3\\}\\)?" ans)
  15545. (setq year (if (match-end 3) (string-to-number (match-string 3 ans))
  15546. (setq kill-year t)
  15547. (string-to-number (format-time-string "%Y")))
  15548. day (string-to-number (match-string 1 ans))
  15549. month (string-to-number (match-string 2 ans))
  15550. ans (replace-match (format "%04d-%02d-%02d" year month day)
  15551. t nil ans)))
  15552. ;; Help matching american dates, like 5/30 or 5/30/7
  15553. (when (string-match
  15554. "^ *\\(0?[1-9]\\|1[012]\\)/\\(0?[1-9]\\|[12][0-9]\\|3[01]\\)\\(/\\([0-9]+\\)\\)?\\([^/0-9]\\|$\\)" ans)
  15555. (setq year (if (match-end 4)
  15556. (string-to-number (match-string 4 ans))
  15557. (progn (setq kill-year t)
  15558. (string-to-number (format-time-string "%Y"))))
  15559. month (string-to-number (match-string 1 ans))
  15560. day (string-to-number (match-string 2 ans)))
  15561. (when (< year 100) (setq year (+ 2000 year)))
  15562. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  15563. t nil ans)))
  15564. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  15565. ;; If there is a time with am/pm, and *no* time without it, we convert
  15566. ;; so that matching will be successful.
  15567. (cl-loop for i from 1 to 2 do ; twice, for end time as well
  15568. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  15569. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  15570. (setq hour (string-to-number (match-string 1 ans))
  15571. minute (if (match-end 3)
  15572. (string-to-number (match-string 3 ans))
  15573. 0)
  15574. pm (equal ?p
  15575. (string-to-char (downcase (match-string 4 ans)))))
  15576. (if (and (= hour 12) (not pm))
  15577. (setq hour 0)
  15578. (when (and pm (< hour 12)) (setq hour (+ 12 hour))))
  15579. (setq ans (replace-match (format "%02d:%02d" hour minute)
  15580. t t ans))))
  15581. ;; Check if a time range is given as a duration
  15582. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  15583. (setq hour (string-to-number (match-string 1 ans))
  15584. h2 (+ hour (string-to-number (match-string 3 ans)))
  15585. minute (string-to-number (match-string 2 ans))
  15586. m2 (+ minute (if (match-end 5) (string-to-number
  15587. (match-string 5 ans))0)))
  15588. (when (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  15589. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  15590. t t ans)))
  15591. ;; Check if there is a time range
  15592. (when (boundp 'org-end-time-was-given)
  15593. (setq org-time-was-given nil)
  15594. (when (and (string-match org-plain-time-of-day-regexp ans)
  15595. (match-end 8))
  15596. (setq org-end-time-was-given (match-string 8 ans))
  15597. (setq ans (concat (substring ans 0 (match-beginning 7))
  15598. (substring ans (match-end 7))))))
  15599. (setq tl (parse-time-string ans)
  15600. day (or (nth 3 tl) (nth 3 org-defdecode))
  15601. month
  15602. (cond ((nth 4 tl))
  15603. ((not org-read-date-prefer-future) (nth 4 org-defdecode))
  15604. ;; Day was specified. Make sure DAY+MONTH
  15605. ;; combination happens in the future.
  15606. ((nth 3 tl)
  15607. (setq futurep t)
  15608. (if (< day (nth 3 nowdecode)) (1+ (nth 4 nowdecode))
  15609. (nth 4 nowdecode)))
  15610. (t (nth 4 org-defdecode)))
  15611. year
  15612. (cond ((and (not kill-year) (nth 5 tl)))
  15613. ((not org-read-date-prefer-future) (nth 5 org-defdecode))
  15614. ;; Month was guessed in the future and is at least
  15615. ;; equal to NOWDECODE's. Fix year accordingly.
  15616. (futurep
  15617. (if (or (> month (nth 4 nowdecode))
  15618. (>= day (nth 3 nowdecode)))
  15619. (nth 5 nowdecode)
  15620. (1+ (nth 5 nowdecode))))
  15621. ;; Month was specified. Make sure MONTH+YEAR
  15622. ;; combination happens in the future.
  15623. ((nth 4 tl)
  15624. (setq futurep t)
  15625. (cond ((> month (nth 4 nowdecode)) (nth 5 nowdecode))
  15626. ((< month (nth 4 nowdecode)) (1+ (nth 5 nowdecode)))
  15627. ((< day (nth 3 nowdecode)) (1+ (nth 5 nowdecode)))
  15628. (t (nth 5 nowdecode))))
  15629. (t (nth 5 org-defdecode)))
  15630. hour (or (nth 2 tl) (nth 2 org-defdecode))
  15631. minute (or (nth 1 tl) (nth 1 org-defdecode))
  15632. second (or (nth 0 tl) 0)
  15633. wday (nth 6 tl))
  15634. (when (and (eq org-read-date-prefer-future 'time)
  15635. (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
  15636. (equal day (nth 3 nowdecode))
  15637. (equal month (nth 4 nowdecode))
  15638. (equal year (nth 5 nowdecode))
  15639. (nth 2 tl)
  15640. (or (< (nth 2 tl) (nth 2 nowdecode))
  15641. (and (= (nth 2 tl) (nth 2 nowdecode))
  15642. (nth 1 tl)
  15643. (< (nth 1 tl) (nth 1 nowdecode)))))
  15644. (setq day (1+ day)
  15645. futurep t))
  15646. ;; Special date definitions below
  15647. (cond
  15648. (iso-week
  15649. ;; There was an iso week
  15650. (require 'cal-iso)
  15651. (setq futurep nil)
  15652. (setq year (or iso-year year)
  15653. day (or iso-weekday wday 1)
  15654. wday nil ; to make sure that the trigger below does not match
  15655. iso-date (calendar-gregorian-from-absolute
  15656. (calendar-iso-to-absolute
  15657. (list iso-week day year))))
  15658. ; FIXME: Should we also push ISO weeks into the future?
  15659. ; (when (and org-read-date-prefer-future
  15660. ; (not iso-year)
  15661. ; (< (calendar-absolute-from-gregorian iso-date)
  15662. ; (time-to-days (current-time))))
  15663. ; (setq year (1+ year)
  15664. ; iso-date (calendar-gregorian-from-absolute
  15665. ; (calendar-iso-to-absolute
  15666. ; (list iso-week day year)))))
  15667. (setq month (car iso-date)
  15668. year (nth 2 iso-date)
  15669. day (nth 1 iso-date)))
  15670. (deltan
  15671. (setq futurep nil)
  15672. (unless deltadef
  15673. ;; Pass `current-time' result to `decode-time' (instead of
  15674. ;; calling without arguments) so that only `current-time' has
  15675. ;; to be overriden in tests.
  15676. (let ((now (decode-time (current-time))))
  15677. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  15678. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  15679. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  15680. ((equal deltaw "m") (setq month (+ month deltan)))
  15681. ((equal deltaw "y") (setq year (+ year deltan)))))
  15682. ((and wday (not (nth 3 tl)))
  15683. ;; Weekday was given, but no day, so pick that day in the week
  15684. ;; on or after the derived date.
  15685. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  15686. (unless (equal wday wday1)
  15687. (setq day (+ day (% (- wday wday1 -7) 7))))))
  15688. (when (and (boundp 'org-time-was-given)
  15689. (nth 2 tl))
  15690. (setq org-time-was-given t))
  15691. (when (< year 100) (setq year (+ 2000 year)))
  15692. ;; Check of the date is representable
  15693. (if org-read-date-force-compatible-dates
  15694. (progn
  15695. (when (< year 1970)
  15696. (setq year 1970 org-read-date-analyze-forced-year t))
  15697. (when (> year 2037)
  15698. (setq year 2037 org-read-date-analyze-forced-year t)))
  15699. (condition-case nil
  15700. (ignore (encode-time second minute hour day month year))
  15701. (error
  15702. (setq year (nth 5 org-defdecode))
  15703. (setq org-read-date-analyze-forced-year t))))
  15704. (setq org-read-date-analyze-futurep futurep)
  15705. (list second minute hour day month year)))
  15706. (defvar parse-time-weekdays)
  15707. (defun org-read-date-get-relative (s today default)
  15708. "Check string S for special relative date string.
  15709. TODAY and DEFAULT are internal times, for today and for a default.
  15710. Return shift list (N what def-flag)
  15711. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  15712. N is the number of WHATs to shift.
  15713. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  15714. the DEFAULT date rather than TODAY."
  15715. (require 'parse-time)
  15716. (when (and
  15717. (string-match
  15718. (concat
  15719. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  15720. "\\([0-9]+\\)?"
  15721. "\\([hdwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  15722. "\\([ \t]\\|$\\)") s)
  15723. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  15724. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  15725. (string-to-char (substring (match-string 1 s) -1))
  15726. ?+))
  15727. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  15728. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  15729. (what (if (match-end 3) (match-string 3 s) "d"))
  15730. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  15731. (date (if rel default today))
  15732. (wday (nth 6 (decode-time date)))
  15733. delta)
  15734. (if wday1
  15735. (progn
  15736. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  15737. (when (= delta 0) (setq delta 7))
  15738. (when (= dir ?-)
  15739. (setq delta (- delta 7))
  15740. (when (= delta 0) (setq delta -7)))
  15741. (when (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  15742. (list delta "d" rel))
  15743. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  15744. (defun org-order-calendar-date-args (arg1 arg2 arg3)
  15745. "Turn a user-specified date into the internal representation.
  15746. The internal representation needed by the calendar is (month day year).
  15747. This is a wrapper to handle the brain-dead convention in calendar that
  15748. user function argument order change dependent on argument order."
  15749. (if (boundp 'calendar-date-style)
  15750. (cond
  15751. ((eq calendar-date-style 'american)
  15752. (list arg1 arg2 arg3))
  15753. ((eq calendar-date-style 'european)
  15754. (list arg2 arg1 arg3))
  15755. ((eq calendar-date-style 'iso)
  15756. (list arg2 arg3 arg1)))
  15757. (org-no-warnings ;; european-calendar-style is obsolete as of version 23.1
  15758. (if (org-bound-and-true-p european-calendar-style)
  15759. (list arg2 arg1 arg3)
  15760. (list arg1 arg2 arg3)))))
  15761. (defun org-eval-in-calendar (form &optional keepdate)
  15762. "Eval FORM in the calendar window and return to current window.
  15763. Unless KEEPDATE is non-nil, update `org-ans2' to the cursor date."
  15764. (let ((sf (selected-frame))
  15765. (sw (selected-window)))
  15766. (select-window (get-buffer-window "*Calendar*" t))
  15767. (eval form)
  15768. (when (and (not keepdate) (calendar-cursor-to-date))
  15769. (let* ((date (calendar-cursor-to-date))
  15770. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15771. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  15772. (move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  15773. (select-window sw)
  15774. (org-select-frame-set-input-focus sf)))
  15775. (defun org-calendar-select ()
  15776. "Return to `org-read-date' with the date currently selected.
  15777. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  15778. (interactive)
  15779. (when (calendar-cursor-to-date)
  15780. (let* ((date (calendar-cursor-to-date))
  15781. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15782. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  15783. (when (active-minibuffer-window) (exit-minibuffer))))
  15784. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  15785. "Insert a date stamp for the date given by the internal TIME.
  15786. See `format-time-string' for the format of TIME.
  15787. WITH-HM means use the stamp format that includes the time of the day.
  15788. INACTIVE means use square brackets instead of angular ones, so that the
  15789. stamp will not contribute to the agenda.
  15790. PRE and POST are optional strings to be inserted before and after the
  15791. stamp.
  15792. The command returns the inserted time stamp."
  15793. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  15794. stamp)
  15795. (when inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  15796. (insert-before-markers (or pre ""))
  15797. (when (listp extra)
  15798. (setq extra (car extra))
  15799. (if (and (stringp extra)
  15800. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  15801. (setq extra (format "-%02d:%02d"
  15802. (string-to-number (match-string 1 extra))
  15803. (string-to-number (match-string 2 extra))))
  15804. (setq extra nil)))
  15805. (when extra
  15806. (setq fmt (concat (substring fmt 0 -1) extra (substring fmt -1))))
  15807. (insert-before-markers (setq stamp (format-time-string fmt time)))
  15808. (insert-before-markers (or post ""))
  15809. (setq org-last-inserted-timestamp stamp)))
  15810. (defun org-toggle-time-stamp-overlays ()
  15811. "Toggle the use of custom time stamp formats."
  15812. (interactive)
  15813. (setq org-display-custom-times (not org-display-custom-times))
  15814. (unless org-display-custom-times
  15815. (let ((p (point-min)) (bmp (buffer-modified-p)))
  15816. (while (setq p (next-single-property-change p 'display))
  15817. (when (and (get-text-property p 'display)
  15818. (eq (get-text-property p 'face) 'org-date))
  15819. (remove-text-properties
  15820. p (setq p (next-single-property-change p 'display))
  15821. '(display t))))
  15822. (set-buffer-modified-p bmp)))
  15823. (when (featurep 'xemacs)
  15824. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  15825. (org-restart-font-lock)
  15826. (setq org-table-may-need-update t)
  15827. (if org-display-custom-times
  15828. (message "Time stamps are overlaid with custom format")
  15829. (message "Time stamp overlays removed")))
  15830. (defun org-display-custom-time (beg end)
  15831. "Overlay modified time stamp format over timestamp between BEG and END."
  15832. (let* ((ts (buffer-substring beg end))
  15833. t1 w1 with-hm tf time str w2 (off 0))
  15834. (save-match-data
  15835. (setq t1 (org-parse-time-string ts t))
  15836. (when (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)?\\'" ts)
  15837. (setq off (- (match-end 0) (match-beginning 0)))))
  15838. (setq end (- end off))
  15839. (setq w1 (- end beg)
  15840. with-hm (and (nth 1 t1) (nth 2 t1))
  15841. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  15842. time (org-fix-decoded-time t1)
  15843. str (org-add-props
  15844. (format-time-string
  15845. (substring tf 1 -1) (apply 'encode-time time))
  15846. nil 'mouse-face 'highlight)
  15847. w2 (length str))
  15848. (unless (= w2 w1)
  15849. (add-text-properties (1+ beg) (+ 2 beg)
  15850. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  15851. (if (featurep 'xemacs)
  15852. (progn
  15853. (put-text-property beg end 'invisible t)
  15854. (put-text-property beg end 'end-glyph (make-glyph str)))
  15855. (put-text-property beg end 'display str))))
  15856. (defun org-fix-decoded-time (time)
  15857. "Set 0 instead of nil for the first 6 elements of time.
  15858. Don't touch the rest."
  15859. (let ((n 0))
  15860. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  15861. (define-obsolete-function-alias 'org-days-to-time 'org-time-stamp-to-now "24.4")
  15862. (defun org-time-stamp-to-now (timestamp-string &optional seconds)
  15863. "Difference between TIMESTAMP-STRING and now in days.
  15864. If SECONDS is non-nil, return the difference in seconds."
  15865. (let ((fdiff (if seconds 'org-float-time 'time-to-days)))
  15866. (- (funcall fdiff (org-time-string-to-time timestamp-string))
  15867. (funcall fdiff (current-time)))))
  15868. (defun org-deadline-close (timestamp-string &optional ndays)
  15869. "Is the time in TIMESTAMP-STRING close to the current date?"
  15870. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  15871. (and (< (org-time-stamp-to-now timestamp-string) ndays)
  15872. (not (org-entry-is-done-p))))
  15873. (defun org-get-wdays (ts &optional delay zero-delay)
  15874. "Get the deadline lead time appropriate for timestring TS.
  15875. When DELAY is non-nil, get the delay time for scheduled items
  15876. instead of the deadline lead time. When ZERO-DELAY is non-nil
  15877. and `org-scheduled-delay-days' is 0, enforce 0 as the delay,
  15878. don't try to find the delay cookie in the scheduled timestamp."
  15879. (let ((tv (if delay org-scheduled-delay-days
  15880. org-deadline-warning-days)))
  15881. (cond
  15882. ((or (and delay (< tv 0))
  15883. (and delay zero-delay (<= tv 0))
  15884. (and (not delay) (<= tv 0)))
  15885. ;; Enforce this value no matter what
  15886. (- tv))
  15887. ((string-match "-\\([0-9]+\\)\\([hdwmy]\\)\\(\\'\\|>\\| \\)" ts)
  15888. ;; lead time is specified.
  15889. (floor (* (string-to-number (match-string 1 ts))
  15890. (cdr (assoc (match-string 2 ts)
  15891. '(("d" . 1) ("w" . 7)
  15892. ("m" . 30.4) ("y" . 365.25)
  15893. ("h" . 0.041667)))))))
  15894. ;; go for the default.
  15895. (t tv))))
  15896. (defun org-calendar-select-mouse (ev)
  15897. "Return to `org-read-date' with the date currently selected.
  15898. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  15899. (interactive "e")
  15900. (mouse-set-point ev)
  15901. (when (calendar-cursor-to-date)
  15902. (let* ((date (calendar-cursor-to-date))
  15903. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15904. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  15905. (when (active-minibuffer-window) (exit-minibuffer))))
  15906. (defun org-check-deadlines (ndays)
  15907. "Check if there are any deadlines due or past due.
  15908. A deadline is considered due if it happens within `org-deadline-warning-days'
  15909. days from today's date. If the deadline appears in an entry marked DONE,
  15910. it is not shown. The prefix arg NDAYS can be used to test that many
  15911. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  15912. (interactive "P")
  15913. (let* ((org-warn-days
  15914. (cond
  15915. ((equal ndays '(4)) 100000)
  15916. (ndays (prefix-numeric-value ndays))
  15917. (t (abs org-deadline-warning-days))))
  15918. (case-fold-search nil)
  15919. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  15920. (callback
  15921. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  15922. (message "%d deadlines past-due or due within %d days"
  15923. (org-occur regexp nil callback)
  15924. org-warn-days)))
  15925. (defsubst org-re-timestamp (type)
  15926. "Return a regexp for timestamp TYPE.
  15927. Allowed values for TYPE are:
  15928. all: all timestamps
  15929. active: only active timestamps (<...>)
  15930. inactive: only inactive timestamps ([...])
  15931. scheduled: only scheduled timestamps
  15932. deadline: only deadline timestamps
  15933. closed: only closed time-stamps
  15934. When TYPE is nil, fall back on returning a regexp that matches
  15935. both scheduled and deadline timestamps."
  15936. (cl-case type
  15937. (all org-ts-regexp-both)
  15938. (active org-ts-regexp)
  15939. (inactive org-ts-regexp-inactive)
  15940. (scheduled org-scheduled-time-regexp)
  15941. (deadline org-deadline-time-regexp)
  15942. (closed org-closed-time-regexp)
  15943. (otherwise
  15944. (concat "\\<"
  15945. (regexp-opt (list org-deadline-string org-scheduled-string))
  15946. " *<\\([^>]+\\)>"))))
  15947. (defun org-check-before-date (d)
  15948. "Check if there are deadlines or scheduled entries before date D."
  15949. (interactive (list (org-read-date)))
  15950. (let* ((case-fold-search nil)
  15951. (regexp (org-re-timestamp org-ts-type))
  15952. (ts-type org-ts-type)
  15953. (callback
  15954. (lambda ()
  15955. (let ((match (match-string 1)))
  15956. (and (if (memq ts-type '(active inactive all))
  15957. (eq (org-element-type (org-element-context)) 'timestamp)
  15958. (org-at-planning-p))
  15959. (time-less-p
  15960. (org-time-string-to-time match)
  15961. (org-time-string-to-time d)))))))
  15962. (message "%d entries before %s"
  15963. (org-occur regexp nil callback)
  15964. d)))
  15965. (defun org-check-after-date (d)
  15966. "Check if there are deadlines or scheduled entries after date D."
  15967. (interactive (list (org-read-date)))
  15968. (let* ((case-fold-search nil)
  15969. (regexp (org-re-timestamp org-ts-type))
  15970. (ts-type org-ts-type)
  15971. (callback
  15972. (lambda ()
  15973. (let ((match (match-string 1)))
  15974. (and (if (memq ts-type '(active inactive all))
  15975. (eq (org-element-type (org-element-context)) 'timestamp)
  15976. (org-at-planning-p))
  15977. (not (time-less-p
  15978. (org-time-string-to-time match)
  15979. (org-time-string-to-time d))))))))
  15980. (message "%d entries after %s"
  15981. (org-occur regexp nil callback)
  15982. d)))
  15983. (defun org-check-dates-range (start-date end-date)
  15984. "Check for deadlines/scheduled entries between START-DATE and END-DATE."
  15985. (interactive (list (org-read-date nil nil nil "Range starts")
  15986. (org-read-date nil nil nil "Range end")))
  15987. (let ((case-fold-search nil)
  15988. (regexp (org-re-timestamp org-ts-type))
  15989. (callback
  15990. `(lambda ()
  15991. (let ((match (match-string 1)))
  15992. (and
  15993. ,(if (memq org-ts-type '(active inactive all))
  15994. '(eq (org-element-type (org-element-context)) 'timestamp)
  15995. '(org-at-planning-p))
  15996. (not (time-less-p
  15997. (org-time-string-to-time match)
  15998. (org-time-string-to-time start-date)))
  15999. (time-less-p
  16000. (org-time-string-to-time match)
  16001. (org-time-string-to-time end-date)))))))
  16002. (message "%d entries between %s and %s"
  16003. (org-occur regexp nil callback) start-date end-date)))
  16004. (defun org-evaluate-time-range (&optional to-buffer)
  16005. "Evaluate a time range by computing the difference between start and end.
  16006. Normally the result is just printed in the echo area, but with prefix arg
  16007. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  16008. If the time range is actually in a table, the result is inserted into the
  16009. next column.
  16010. For time difference computation, a year is assumed to be exactly 365
  16011. days in order to avoid rounding problems."
  16012. (interactive "P")
  16013. (or
  16014. (org-clock-update-time-maybe)
  16015. (save-excursion
  16016. (unless (org-at-date-range-p t)
  16017. (goto-char (point-at-bol))
  16018. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  16019. (unless (org-at-date-range-p t)
  16020. (user-error "Not at a time-stamp range, and none found in current line")))
  16021. (let* ((ts1 (match-string 1))
  16022. (ts2 (match-string 2))
  16023. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  16024. (match-end (match-end 0))
  16025. (time1 (org-time-string-to-time ts1))
  16026. (time2 (org-time-string-to-time ts2))
  16027. (t1 (org-float-time time1))
  16028. (t2 (org-float-time time2))
  16029. (diff (abs (- t2 t1)))
  16030. (negative (< (- t2 t1) 0))
  16031. ;; (ys (floor (* 365 24 60 60)))
  16032. (ds (* 24 60 60))
  16033. (hs (* 60 60))
  16034. (fy "%dy %dd %02d:%02d")
  16035. (fy1 "%dy %dd")
  16036. (fd "%dd %02d:%02d")
  16037. (fd1 "%dd")
  16038. (fh "%02d:%02d")
  16039. y d h m align)
  16040. (if havetime
  16041. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  16042. y 0
  16043. d (floor (/ diff ds)) diff (mod diff ds)
  16044. h (floor (/ diff hs)) diff (mod diff hs)
  16045. m (floor (/ diff 60)))
  16046. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  16047. y 0
  16048. d (floor (+ (/ diff ds) 0.5))
  16049. h 0 m 0))
  16050. (if (not to-buffer)
  16051. (message "%s" (org-make-tdiff-string y d h m))
  16052. (if (org-at-table-p)
  16053. (progn
  16054. (goto-char match-end)
  16055. (setq align t)
  16056. (and (looking-at " *|") (goto-char (match-end 0))))
  16057. (goto-char match-end))
  16058. (when (looking-at
  16059. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  16060. (replace-match ""))
  16061. (when negative (insert " -"))
  16062. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  16063. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  16064. (insert " " (format fh h m))))
  16065. (when align (org-table-align))
  16066. (message "Time difference inserted")))))
  16067. (defun org-make-tdiff-string (y d h m)
  16068. (let ((fmt "")
  16069. (l nil))
  16070. (when (> y 0)
  16071. (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " "))
  16072. (push y l))
  16073. (when (> d 0)
  16074. (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " "))
  16075. (push d l))
  16076. (when (> h 0)
  16077. (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " "))
  16078. (push h l))
  16079. (when (> m 0)
  16080. (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " "))
  16081. (push m l))
  16082. (apply 'format fmt (nreverse l))))
  16083. (defun org-time-string-to-time (s &optional buffer pos)
  16084. "Convert a timestamp string into internal time."
  16085. (condition-case errdata
  16086. (apply 'encode-time (org-parse-time-string s))
  16087. (error (error "Bad timestamp `%s'%s\nError was: %s"
  16088. s (if (not (and buffer pos))
  16089. ""
  16090. (format-message " at %d in buffer `%s'" pos buffer))
  16091. (cdr errdata)))))
  16092. (defun org-time-string-to-seconds (s)
  16093. "Convert a timestamp string to a number of seconds."
  16094. (org-float-time (org-time-string-to-time s)))
  16095. (org-define-error 'org-diary-sexp-no-match "Unable to match diary sexp")
  16096. (defun org-time-string-to-absolute (s &optional daynr prefer show-all buffer pos)
  16097. "Convert time stamp S to an absolute day number.
  16098. If DAYNR in non-nil, and there is a specifier for a cyclic time
  16099. stamp, get the closest date to DAYNR. If PREFER is
  16100. `past' (respectively `future') return a date past (respectively
  16101. after) or equal to DAYNR.
  16102. POS is the location of time stamp S, as a buffer position in
  16103. BUFFER.
  16104. Diary sexp timestamps are matched against DAYNR, when non-nil.
  16105. If matching fails or DAYNR is nil, `org-diary-sexp-no-match' is
  16106. signalled."
  16107. (cond
  16108. ((string-match "\\`%%\\((.*)\\)" s)
  16109. ;; Sexp timestamp: try to match DAYNR, if available, since we're
  16110. ;; only able to match individual dates. If it fails, raise an
  16111. ;; error.
  16112. (if (and daynr
  16113. (org-diary-sexp-entry
  16114. (match-string 1 s) "" (calendar-gregorian-from-absolute daynr)))
  16115. daynr
  16116. (signal 'org-diary-sexp-no-match (list s))))
  16117. ((and daynr show-all) (org-closest-date s daynr prefer))
  16118. (t (time-to-days
  16119. (condition-case errdata
  16120. (apply #'encode-time (org-parse-time-string s))
  16121. (error (error "Bad timestamp `%s'%s\nError was: %s"
  16122. s
  16123. (if (not (and buffer pos)) ""
  16124. (format-message " at %d in buffer `%s'" pos buffer))
  16125. (cdr errdata))))))))
  16126. (defun org-days-to-iso-week (days)
  16127. "Return the iso week number."
  16128. (require 'cal-iso)
  16129. (car (calendar-iso-from-absolute days)))
  16130. (defun org-small-year-to-year (year)
  16131. "Convert 2-digit years into 4-digit years.
  16132. YEAR is expanded into one of the 30 next years, if possible, or
  16133. into a past one. Any year larger than 99 is returned unchanged."
  16134. (if (>= year 100) year
  16135. (let* ((current (string-to-number (format-time-string "%Y" (current-time))))
  16136. (century (/ current 100))
  16137. (offset (- year (% current 100))))
  16138. (cond ((> offset 30) (+ (* (1- century) 100) year))
  16139. ((> offset -70) (+ (* century 100) year))
  16140. (t (+ (* (1+ century) 100) year))))))
  16141. (defun org-time-from-absolute (d)
  16142. "Return the time corresponding to date D.
  16143. D may be an absolute day number, or a calendar-type list (month day year)."
  16144. (when (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  16145. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  16146. (defvar org-agenda-current-date)
  16147. (defun org-calendar-holiday ()
  16148. "List of holidays, for Diary display in Org mode."
  16149. (require 'holidays)
  16150. (let ((hl (calendar-check-holidays org-agenda-current-date)))
  16151. (and hl (mapconcat #'identity hl "; "))))
  16152. (defun org-diary-sexp-entry (sexp entry d)
  16153. "Process a SEXP diary ENTRY for date D."
  16154. (require 'diary-lib)
  16155. ;; `org-anniversary' and alike expect ENTRY and DATE to be bound
  16156. ;; dynamically.
  16157. (let* ((sexp `(let ((entry ,entry)
  16158. (date ',d))
  16159. ,(car (read-from-string sexp))))
  16160. (result (if calendar-debug-sexp (eval sexp)
  16161. (condition-case nil
  16162. (eval sexp)
  16163. (error
  16164. (beep)
  16165. (message "Bad sexp at line %d in %s: %s"
  16166. (org-current-line)
  16167. (buffer-file-name) sexp)
  16168. (sleep-for 2))))))
  16169. (cond ((stringp result) (split-string result "; "))
  16170. ((and (consp result)
  16171. (not (consp (cdr result)))
  16172. (stringp (cdr result))) (cdr result))
  16173. ((and (consp result)
  16174. (stringp (car result))) result)
  16175. (result entry))))
  16176. (defun org-diary-to-ical-string (frombuf)
  16177. "Get iCalendar entries from diary entries in buffer FROMBUF.
  16178. This uses the icalendar.el library."
  16179. (let* ((tmpdir (if (featurep 'xemacs)
  16180. (temp-directory)
  16181. temporary-file-directory))
  16182. (tmpfile (make-temp-name
  16183. (expand-file-name "orgics" tmpdir)))
  16184. buf rtn b e)
  16185. (with-current-buffer frombuf
  16186. (icalendar-export-region (point-min) (point-max) tmpfile)
  16187. (setq buf (find-buffer-visiting tmpfile))
  16188. (set-buffer buf)
  16189. (goto-char (point-min))
  16190. (when (re-search-forward "^BEGIN:VEVENT" nil t)
  16191. (setq b (match-beginning 0)))
  16192. (goto-char (point-max))
  16193. (when (re-search-backward "^END:VEVENT" nil t)
  16194. (setq e (match-end 0)))
  16195. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  16196. (kill-buffer buf)
  16197. (delete-file tmpfile)
  16198. rtn))
  16199. (defun org-closest-date (start current prefer)
  16200. "Return closest date to CURRENT starting from START.
  16201. CURRENT and START are both time stamps.
  16202. When PREFER is `past', return a date that is either CURRENT or
  16203. past. When PREFER is `future', return a date that is either
  16204. CURRENT or future.
  16205. Only time stamps with a repeater are modified. Any other time
  16206. stamp stay unchanged. In any case, return value is an absolute
  16207. day number."
  16208. (if (not (string-match "\\+\\([0-9]+\\)\\([hdwmy]\\)" start))
  16209. ;; No repeater. Do not shift time stamp.
  16210. (time-to-days (apply #'encode-time (org-parse-time-string start)))
  16211. (let ((value (string-to-number (match-string 1 start)))
  16212. (type (match-string 2 start)))
  16213. (if (= 0 value)
  16214. ;; Repeater with a 0-value is considered as void.
  16215. (time-to-days (apply #'encode-time (org-parse-time-string start)))
  16216. (let* ((base (org-date-to-gregorian start))
  16217. (target (org-date-to-gregorian current))
  16218. (sday (calendar-absolute-from-gregorian base))
  16219. (cday (calendar-absolute-from-gregorian target))
  16220. n1 n2)
  16221. ;; If START is already past CURRENT, just return START.
  16222. (if (<= cday sday) sday
  16223. ;; Compute closest date before (N1) and closest date past
  16224. ;; (N2) CURRENT.
  16225. (pcase type
  16226. ("h"
  16227. (let ((missing-hours
  16228. (mod (+ (- (* 24 (- cday sday))
  16229. (nth 2 (org-parse-time-string start)))
  16230. org-extend-today-until)
  16231. value)))
  16232. (setf n1 (if (= missing-hours 0) cday
  16233. (- cday (1+ (/ missing-hours 24)))))
  16234. (setf n2 (+ cday (/ (- value missing-hours) 24)))))
  16235. ((or "d" "w")
  16236. (let ((value (if (equal type "w") (* 7 value) value)))
  16237. (setf n1 (+ sday (* value (/ (- cday sday) value))))
  16238. (setf n2 (+ n1 value))))
  16239. ("m"
  16240. (let* ((add-months
  16241. (lambda (d n)
  16242. ;; Add N months to gregorian date D, i.e.,
  16243. ;; a list (MONTH DAY YEAR). Return a valid
  16244. ;; gregorian date.
  16245. (let ((m (+ (nth 0 d) n)))
  16246. (list (mod m 12)
  16247. (nth 1 d)
  16248. (+ (/ m 12) (nth 2 d))))))
  16249. (months ; Complete months to TARGET.
  16250. (* (/ (+ (* 12 (- (nth 2 target) (nth 2 base)))
  16251. (- (nth 0 target) (nth 0 base))
  16252. ;; If START's day is greater than
  16253. ;; TARGET's, remove incomplete month.
  16254. (if (> (nth 1 target) (nth 1 base)) 0 -1))
  16255. value)
  16256. value))
  16257. (before (funcall add-months base months)))
  16258. (setf n1 (calendar-absolute-from-gregorian before))
  16259. (setf n2
  16260. (calendar-absolute-from-gregorian
  16261. (funcall add-months before value)))))
  16262. (_
  16263. (let* ((d (nth 1 base))
  16264. (m (nth 0 base))
  16265. (y (nth 2 base))
  16266. (years ; Complete years to TARGET.
  16267. (* (/ (- (nth 2 target)
  16268. y
  16269. ;; If START's month and day are
  16270. ;; greater than TARGET's, remove
  16271. ;; incomplete year.
  16272. (if (or (> (nth 0 target) m)
  16273. (and (= (nth 0 target) m)
  16274. (> (nth 1 target) d)))
  16275. 0
  16276. 1))
  16277. value)
  16278. value))
  16279. (before (list m d (+ y years))))
  16280. (setf n1 (calendar-absolute-from-gregorian before))
  16281. (setf n2 (calendar-absolute-from-gregorian
  16282. (list m d (+ (nth 2 before) value)))))))
  16283. ;; Handle PREFER parameter, if any.
  16284. (cond
  16285. ((eq prefer 'past) (if (= cday n2) n2 n1))
  16286. ((eq prefer 'future) (if (= cday n1) n1 n2))
  16287. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))))))))
  16288. (defun org-date-to-gregorian (d)
  16289. "Turn any specification of date D into a Gregorian date for the calendar."
  16290. (cond ((integerp d) (calendar-gregorian-from-absolute d))
  16291. ((and (listp d) (= (length d) 3)) d)
  16292. ((stringp d)
  16293. (let ((d (org-parse-time-string d)))
  16294. (list (nth 4 d) (nth 3 d) (nth 5 d))))
  16295. ((listp d) (list (nth 4 d) (nth 3 d) (nth 5 d)))))
  16296. (defun org-parse-time-string (s &optional nodefault)
  16297. "Parse the standard Org-mode time string.
  16298. This should be a lot faster than the normal `parse-time-string'.
  16299. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  16300. hour and minute fields will be nil if not given."
  16301. (cond ((string-match org-ts-regexp0 s)
  16302. (list 0
  16303. (when (or (match-beginning 8) (not nodefault))
  16304. (string-to-number (or (match-string 8 s) "0")))
  16305. (when (or (match-beginning 7) (not nodefault))
  16306. (string-to-number (or (match-string 7 s) "0")))
  16307. (string-to-number (match-string 4 s))
  16308. (string-to-number (match-string 3 s))
  16309. (string-to-number (match-string 2 s))
  16310. nil nil nil))
  16311. ((string-match "^<[^>]+>$" s)
  16312. (decode-time (seconds-to-time (org-matcher-time s))))
  16313. (t (error "Not a standard Org-mode time string: %s" s))))
  16314. (defun org-timestamp-up (&optional arg)
  16315. "Increase the date item at the cursor by one.
  16316. If the cursor is on the year, change the year. If it is on the month,
  16317. the day or the time, change that.
  16318. With prefix ARG, change by that many units."
  16319. (interactive "p")
  16320. (org-timestamp-change (prefix-numeric-value arg) nil 'updown))
  16321. (defun org-timestamp-down (&optional arg)
  16322. "Decrease the date item at the cursor by one.
  16323. If the cursor is on the year, change the year. If it is on the month,
  16324. the day or the time, change that.
  16325. With prefix ARG, change by that many units."
  16326. (interactive "p")
  16327. (org-timestamp-change (- (prefix-numeric-value arg)) nil 'updown))
  16328. (defun org-timestamp-up-day (&optional arg)
  16329. "Increase the date in the time stamp by one day.
  16330. With prefix ARG, change that many days."
  16331. (interactive "p")
  16332. (if (and (not (org-at-timestamp-p t))
  16333. (org-at-heading-p))
  16334. (org-todo 'up)
  16335. (org-timestamp-change (prefix-numeric-value arg) 'day 'updown)))
  16336. (defun org-timestamp-down-day (&optional arg)
  16337. "Decrease the date in the time stamp by one day.
  16338. With prefix ARG, change that many days."
  16339. (interactive "p")
  16340. (if (and (not (org-at-timestamp-p t))
  16341. (org-at-heading-p))
  16342. (org-todo 'down)
  16343. (org-timestamp-change (- (prefix-numeric-value arg)) 'day) 'updown))
  16344. (defun org-at-timestamp-p (&optional inactive-ok)
  16345. "Non-nil if point is inside a timestamp.
  16346. When optional argument INACTIVE-OK is non-nil, also consider
  16347. inactive timestamps.
  16348. When this function returns a non-nil value, match data is set
  16349. according to `org-ts-regexp3' or `org-ts-regexp2', depending on
  16350. INACTIVE-OK."
  16351. (interactive)
  16352. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  16353. (pos (point))
  16354. (ans (or (looking-at tsr)
  16355. (save-excursion
  16356. (skip-chars-backward "^[<\n\r\t")
  16357. (when (> (point) (point-min)) (backward-char 1))
  16358. (and (looking-at tsr)
  16359. (> (- (match-end 0) pos) -1))))))
  16360. (and ans
  16361. (boundp 'org-ts-what)
  16362. (setq org-ts-what
  16363. (cond
  16364. ((= pos (match-beginning 0)) 'bracket)
  16365. ;; Point is considered to be "on the bracket" whether
  16366. ;; it's really on it or right after it.
  16367. ((= pos (1- (match-end 0))) 'bracket)
  16368. ((= pos (match-end 0)) 'after)
  16369. ((org-pos-in-match-range pos 2) 'year)
  16370. ((org-pos-in-match-range pos 3) 'month)
  16371. ((org-pos-in-match-range pos 7) 'hour)
  16372. ((org-pos-in-match-range pos 8) 'minute)
  16373. ((or (org-pos-in-match-range pos 4)
  16374. (org-pos-in-match-range pos 5)) 'day)
  16375. ((and (> pos (or (match-end 8) (match-end 5)))
  16376. (< pos (match-end 0)))
  16377. (- pos (or (match-end 8) (match-end 5))))
  16378. (t 'day))))
  16379. ans))
  16380. (defun org-toggle-timestamp-type ()
  16381. "Toggle the type (<active> or [inactive]) of a time stamp."
  16382. (interactive)
  16383. (when (org-at-timestamp-p t)
  16384. (let ((beg (match-beginning 0)) (end (match-end 0))
  16385. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  16386. (save-excursion
  16387. (goto-char beg)
  16388. (while (re-search-forward "[][<>]" end t)
  16389. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  16390. t t)))
  16391. (message "Timestamp is now %sactive"
  16392. (if (equal (char-after beg) ?<) "" "in")))))
  16393. (defun org-at-clock-log-p nil
  16394. "Is the cursor on the clock log line?"
  16395. (save-excursion
  16396. (move-beginning-of-line 1)
  16397. (looking-at org-clock-line-re)))
  16398. (defvar org-clock-history) ; defined in org-clock.el
  16399. (defvar org-clock-adjust-closest nil) ; defined in org-clock.el
  16400. (defun org-timestamp-change (n &optional what updown suppress-tmp-delay)
  16401. "Change the date in the time stamp at point.
  16402. The date will be changed by N times WHAT. WHAT can be `day', `month',
  16403. `year', `minute', `second'. If WHAT is not given, the cursor position
  16404. in the timestamp determines what will be changed.
  16405. When SUPPRESS-TMP-DELAY is non-nil, suppress delays like \"--2d\"."
  16406. (let ((origin (point)) origin-cat
  16407. with-hm inactive
  16408. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  16409. org-ts-what
  16410. extra rem
  16411. ts time time0 fixnext clrgx)
  16412. (unless (org-at-timestamp-p t)
  16413. (user-error "Not at a timestamp"))
  16414. (if (and (not what) (eq org-ts-what 'bracket))
  16415. (org-toggle-timestamp-type)
  16416. ;; Point isn't on brackets. Remember the part of the time-stamp
  16417. ;; the point was in. Indeed, size of time-stamps may change,
  16418. ;; but point must be kept in the same category nonetheless.
  16419. (setq origin-cat org-ts-what)
  16420. (when (and (not what) (not (eq org-ts-what 'day))
  16421. org-display-custom-times
  16422. (get-text-property (point) 'display)
  16423. (not (get-text-property (1- (point)) 'display)))
  16424. (setq org-ts-what 'day))
  16425. (setq org-ts-what (or what org-ts-what)
  16426. inactive (= (char-after (match-beginning 0)) ?\[)
  16427. ts (match-string 0))
  16428. (replace-match "")
  16429. (when (string-match
  16430. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?-?[-+][0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)*\\)[]>]"
  16431. ts)
  16432. (setq extra (match-string 1 ts))
  16433. (when suppress-tmp-delay
  16434. (setq extra (replace-regexp-in-string " --[0-9]+[hdwmy]" "" extra))))
  16435. (when (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  16436. (setq with-hm t))
  16437. (setq time0 (org-parse-time-string ts))
  16438. (when (and updown
  16439. (eq org-ts-what 'minute)
  16440. (not current-prefix-arg))
  16441. ;; This looks like s-up and s-down. Change by one rounding step.
  16442. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  16443. (unless (= 0 (setq rem (% (nth 1 time0) dm)))
  16444. (setcar (cdr time0) (+ (nth 1 time0)
  16445. (if (> n 0) (- rem) (- dm rem))))))
  16446. (setq time
  16447. (encode-time (or (car time0) 0)
  16448. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  16449. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  16450. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  16451. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  16452. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  16453. (nthcdr 6 time0)))
  16454. (when (and (member org-ts-what '(hour minute))
  16455. extra
  16456. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  16457. (setq extra (org-modify-ts-extra
  16458. extra
  16459. (if (eq org-ts-what 'hour) 2 5)
  16460. n dm)))
  16461. (when (integerp org-ts-what)
  16462. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  16463. (when (eq what 'calendar)
  16464. (let ((cal-date (org-get-date-from-calendar)))
  16465. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  16466. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  16467. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  16468. (setcar time0 (or (car time0) 0))
  16469. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  16470. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  16471. (setq time (apply 'encode-time time0))))
  16472. ;; Insert the new time-stamp, and ensure point stays in the same
  16473. ;; category as before (i.e. not after the last position in that
  16474. ;; category).
  16475. (let ((pos (point)))
  16476. ;; Stay before inserted string. `save-excursion' is of no use.
  16477. (setq org-last-changed-timestamp
  16478. (org-insert-time-stamp time with-hm inactive nil nil extra))
  16479. (goto-char pos))
  16480. (save-match-data
  16481. (looking-at org-ts-regexp3)
  16482. (goto-char (cond
  16483. ;; `day' category ends before `hour' if any, or at
  16484. ;; the end of the day name.
  16485. ((eq origin-cat 'day)
  16486. (min (or (match-beginning 7) (1- (match-end 5))) origin))
  16487. ((eq origin-cat 'hour) (min (match-end 7) origin))
  16488. ((eq origin-cat 'minute) (min (1- (match-end 8)) origin))
  16489. ((integerp origin-cat) (min (1- (match-end 0)) origin))
  16490. ;; `year' and `month' have both fixed size: point
  16491. ;; couldn't have moved into another part.
  16492. (t origin))))
  16493. ;; Update clock if on a CLOCK line.
  16494. (org-clock-update-time-maybe)
  16495. ;; Maybe adjust the closest clock in `org-clock-history'
  16496. (when org-clock-adjust-closest
  16497. (if (not (and (org-at-clock-log-p)
  16498. (< 1 (length (delq nil (mapcar 'marker-position
  16499. org-clock-history))))))
  16500. (message "No clock to adjust")
  16501. (cond ((save-excursion ; fix previous clock?
  16502. (re-search-backward org-ts-regexp0 nil t)
  16503. (org-looking-back (concat org-clock-string " \\[")
  16504. (line-beginning-position)))
  16505. (setq fixnext 1 clrgx (concat org-ts-regexp0 "\\] =>.*$")))
  16506. ((save-excursion ; fix next clock?
  16507. (re-search-backward org-ts-regexp0 nil t)
  16508. (looking-at (concat org-ts-regexp0 "\\] =>")))
  16509. (setq fixnext -1 clrgx (concat org-clock-string " \\[" org-ts-regexp0))))
  16510. (save-window-excursion
  16511. ;; Find closest clock to point, adjust the previous/next one in history
  16512. (let* ((p (save-excursion (org-back-to-heading t)))
  16513. (cl (mapcar (lambda(c) (abs (- (marker-position c) p))) org-clock-history))
  16514. (clfixnth
  16515. (+ fixnext (- (length cl) (or (length (member (apply 'min cl) cl)) 100))))
  16516. (clfixpos (unless (> 0 clfixnth) (nth clfixnth org-clock-history))))
  16517. (if (not clfixpos)
  16518. (message "No clock to adjust")
  16519. (save-excursion
  16520. (org-goto-marker-or-bmk clfixpos)
  16521. (org-show-subtree)
  16522. (when (re-search-forward clrgx nil t)
  16523. (goto-char (match-beginning 1))
  16524. (let (org-clock-adjust-closest)
  16525. (org-timestamp-change n org-ts-what updown))
  16526. (message "Clock adjusted in %s for heading: %s"
  16527. (file-name-nondirectory (buffer-file-name))
  16528. (org-get-heading t t)))))))))
  16529. ;; Try to recenter the calendar window, if any.
  16530. (when (and org-calendar-follow-timestamp-change
  16531. (get-buffer-window "*Calendar*" t)
  16532. (memq org-ts-what '(day month year)))
  16533. (org-recenter-calendar (time-to-days time))))))
  16534. (defun org-modify-ts-extra (s pos n dm)
  16535. "Change the different parts of the lead-time and repeat fields in timestamp."
  16536. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  16537. ng h m new rem)
  16538. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  16539. (cond
  16540. ((or (org-pos-in-match-range pos 2)
  16541. (org-pos-in-match-range pos 3))
  16542. (setq m (string-to-number (match-string 3 s))
  16543. h (string-to-number (match-string 2 s)))
  16544. (if (org-pos-in-match-range pos 2)
  16545. (setq h (+ h n))
  16546. (setq n (* dm (org-no-warnings (signum n))))
  16547. (unless (= 0 (setq rem (% m dm)))
  16548. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  16549. (setq m (+ m n)))
  16550. (when (< m 0) (setq m (+ m 60) h (1- h)))
  16551. (when (> m 59) (setq m (- m 60) h (1+ h)))
  16552. (setq h (mod h 24))
  16553. (setq ng 1 new (format "-%02d:%02d" h m)))
  16554. ((org-pos-in-match-range pos 6)
  16555. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  16556. ((org-pos-in-match-range pos 5)
  16557. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  16558. ((org-pos-in-match-range pos 9)
  16559. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  16560. ((org-pos-in-match-range pos 8)
  16561. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  16562. (when ng
  16563. (setq s (concat
  16564. (substring s 0 (match-beginning ng))
  16565. new
  16566. (substring s (match-end ng))))))
  16567. s))
  16568. (defun org-recenter-calendar (d)
  16569. "If the calendar is visible, recenter it to date D."
  16570. (let ((cwin (get-buffer-window "*Calendar*" t)))
  16571. (when cwin
  16572. (let ((calendar-move-hook nil))
  16573. (with-selected-window cwin
  16574. (calendar-goto-date
  16575. (if (listp d) d (calendar-gregorian-from-absolute d))))))))
  16576. (defun org-goto-calendar (&optional arg)
  16577. "Go to the Emacs calendar at the current date.
  16578. If there is a time stamp in the current line, go to that date.
  16579. A prefix ARG can be used to force the current date."
  16580. (interactive "P")
  16581. (let ((tsr org-ts-regexp) diff
  16582. (calendar-move-hook nil)
  16583. (calendar-view-holidays-initially-flag nil)
  16584. (calendar-view-diary-initially-flag nil))
  16585. (when (or (org-at-timestamp-p)
  16586. (save-excursion
  16587. (beginning-of-line 1)
  16588. (looking-at (concat ".*" tsr))))
  16589. (let ((d1 (time-to-days (current-time)))
  16590. (d2 (time-to-days
  16591. (org-time-string-to-time (match-string 1)))))
  16592. (setq diff (- d2 d1))))
  16593. (calendar)
  16594. (calendar-goto-today)
  16595. (when (and diff (not arg)) (calendar-forward-day diff))))
  16596. (defun org-get-date-from-calendar ()
  16597. "Return a list (month day year) of date at point in calendar."
  16598. (with-current-buffer "*Calendar*"
  16599. (save-match-data
  16600. (calendar-cursor-to-date))))
  16601. (defun org-date-from-calendar ()
  16602. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  16603. If there is already a time stamp at the cursor position, update it."
  16604. (interactive)
  16605. (if (org-at-timestamp-p t)
  16606. (org-timestamp-change 0 'calendar)
  16607. (let ((cal-date (org-get-date-from-calendar)))
  16608. (org-insert-time-stamp
  16609. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  16610. (defcustom org-effort-durations
  16611. `(("min" . 1)
  16612. ("h" . 60)
  16613. ("d" . ,(* 60 8))
  16614. ("w" . ,(* 60 8 5))
  16615. ("m" . ,(* 60 8 5 4))
  16616. ("y" . ,(* 60 8 5 40)))
  16617. "Conversion factor to minutes for an effort modifier.
  16618. Each entry has the form (MODIFIER . MINUTES).
  16619. In an effort string, a number followed by MODIFIER is multiplied
  16620. by the specified number of MINUTES to obtain an effort in
  16621. minutes.
  16622. For example, if the value of this variable is ((\"hours\" . 60)), then an
  16623. effort string \"2hours\" is equivalent to 120 minutes."
  16624. :group 'org-agenda
  16625. :version "25.1"
  16626. :package-version '(Org . "8.3")
  16627. :type '(alist :key-type (string :tag "Modifier")
  16628. :value-type (number :tag "Minutes")))
  16629. (defun org-minutes-to-clocksum-string (m)
  16630. "Format number of minutes as a clocksum string.
  16631. The format is determined by `org-time-clocksum-format',
  16632. `org-time-clocksum-use-fractional' and
  16633. `org-time-clocksum-fractional-format' and
  16634. `org-time-clocksum-use-effort-durations'."
  16635. (let ((clocksum "")
  16636. (m (round m)) ; Don't allow fractions of minutes
  16637. h d w mo y fmt n)
  16638. (setq h (if org-time-clocksum-use-effort-durations
  16639. (cdr (assoc "h" org-effort-durations)) 60)
  16640. d (if org-time-clocksum-use-effort-durations
  16641. (/ (cdr (assoc "d" org-effort-durations)) h) 24)
  16642. w (if org-time-clocksum-use-effort-durations
  16643. (/ (cdr (assoc "w" org-effort-durations)) (* d h)) 7)
  16644. mo (if org-time-clocksum-use-effort-durations
  16645. (/ (cdr (assoc "m" org-effort-durations)) (* d h)) 30)
  16646. y (if org-time-clocksum-use-effort-durations
  16647. (/ (cdr (assoc "y" org-effort-durations)) (* d h)) 365))
  16648. ;; fractional format
  16649. (if org-time-clocksum-use-fractional
  16650. (cond
  16651. ;; single format string
  16652. ((stringp org-time-clocksum-fractional-format)
  16653. (format org-time-clocksum-fractional-format (/ m (float h))))
  16654. ;; choice of fractional formats for different time units
  16655. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :years))
  16656. (> (/ (truncate m) (* y d h)) 0))
  16657. (format fmt (/ m (* y d (float h)))))
  16658. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :months))
  16659. (> (/ (truncate m) (* mo d h)) 0))
  16660. (format fmt (/ m (* mo d (float h)))))
  16661. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :weeks))
  16662. (> (/ (truncate m) (* w d h)) 0))
  16663. (format fmt (/ m (* w d (float h)))))
  16664. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :days))
  16665. (> (/ (truncate m) (* d h)) 0))
  16666. (format fmt (/ m (* d (float h)))))
  16667. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :hours))
  16668. (> (/ (truncate m) h) 0))
  16669. (format fmt (/ m (float h))))
  16670. ((setq fmt (plist-get org-time-clocksum-fractional-format :minutes))
  16671. (format fmt m))
  16672. ;; fall back to smallest time unit with a format
  16673. ((setq fmt (plist-get org-time-clocksum-fractional-format :hours))
  16674. (format fmt (/ m (float h))))
  16675. ((setq fmt (plist-get org-time-clocksum-fractional-format :days))
  16676. (format fmt (/ m (* d (float h)))))
  16677. ((setq fmt (plist-get org-time-clocksum-fractional-format :weeks))
  16678. (format fmt (/ m (* w d (float h)))))
  16679. ((setq fmt (plist-get org-time-clocksum-fractional-format :months))
  16680. (format fmt (/ m (* mo d (float h)))))
  16681. ((setq fmt (plist-get org-time-clocksum-fractional-format :years))
  16682. (format fmt (/ m (* y d (float h))))))
  16683. ;; standard (non-fractional) format, with single format string
  16684. (if (stringp org-time-clocksum-format)
  16685. (format org-time-clocksum-format (setq n (/ m h)) (- m (* h n)))
  16686. ;; separate formats components
  16687. (and (setq fmt (plist-get org-time-clocksum-format :years))
  16688. (or (> (setq n (/ (truncate m) (* y d h))) 0)
  16689. (plist-get org-time-clocksum-format :require-years))
  16690. (setq clocksum (concat clocksum (format fmt n))
  16691. m (- m (* n y d h))))
  16692. (and (setq fmt (plist-get org-time-clocksum-format :months))
  16693. (or (> (setq n (/ (truncate m) (* mo d h))) 0)
  16694. (plist-get org-time-clocksum-format :require-months))
  16695. (setq clocksum (concat clocksum (format fmt n))
  16696. m (- m (* n mo d h))))
  16697. (and (setq fmt (plist-get org-time-clocksum-format :weeks))
  16698. (or (> (setq n (/ (truncate m) (* w d h))) 0)
  16699. (plist-get org-time-clocksum-format :require-weeks))
  16700. (setq clocksum (concat clocksum (format fmt n))
  16701. m (- m (* n w d h))))
  16702. (and (setq fmt (plist-get org-time-clocksum-format :days))
  16703. (or (> (setq n (/ (truncate m) (* d h))) 0)
  16704. (plist-get org-time-clocksum-format :require-days))
  16705. (setq clocksum (concat clocksum (format fmt n))
  16706. m (- m (* n d h))))
  16707. (and (setq fmt (plist-get org-time-clocksum-format :hours))
  16708. (or (> (setq n (/ (truncate m) h)) 0)
  16709. (plist-get org-time-clocksum-format :require-hours))
  16710. (setq clocksum (concat clocksum (format fmt n))
  16711. m (- m (* n h))))
  16712. (and (setq fmt (plist-get org-time-clocksum-format :minutes))
  16713. (or (> m 0) (plist-get org-time-clocksum-format :require-minutes))
  16714. (setq clocksum (concat clocksum (format fmt m))))
  16715. ;; return formatted time duration
  16716. clocksum))))
  16717. (defalias 'org-minutes-to-hh:mm-string 'org-minutes-to-clocksum-string)
  16718. (make-obsolete 'org-minutes-to-hh:mm-string 'org-minutes-to-clocksum-string
  16719. "Org mode version 8.0")
  16720. (defun org-hours-to-clocksum-string (n)
  16721. (org-minutes-to-clocksum-string (* n 60)))
  16722. (defun org-hh:mm-string-to-minutes (s)
  16723. "Convert a string H:MM to a number of minutes.
  16724. If the string is just a number, interpret it as minutes.
  16725. In fact, the first hh:mm or number in the string will be taken,
  16726. there can be extra stuff in the string.
  16727. If no number is found, the return value is 0."
  16728. (cond
  16729. ((integerp s) s)
  16730. ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  16731. (+ (* (string-to-number (match-string 1 s)) 60)
  16732. (string-to-number (match-string 2 s))))
  16733. ((string-match "\\([0-9]+\\)" s)
  16734. (string-to-number (match-string 1 s)))
  16735. (t 0)))
  16736. (defcustom org-image-actual-width t
  16737. "Should we use the actual width of images when inlining them?
  16738. When set to t, always use the image width.
  16739. When set to a number, use imagemagick (when available) to set
  16740. the image's width to this value.
  16741. When set to a number in a list, try to get the width from any
  16742. #+ATTR.* keyword if it matches a width specification like
  16743. #+ATTR_HTML: :width 300px
  16744. and fall back on that number if none is found.
  16745. When set to nil, try to get the width from an #+ATTR.* keyword
  16746. and fall back on the original width if none is found.
  16747. This requires Emacs >= 24.1, build with imagemagick support."
  16748. :group 'org-appearance
  16749. :version "24.4"
  16750. :package-version '(Org . "8.0")
  16751. :type '(choice
  16752. (const :tag "Use the image width" t)
  16753. (integer :tag "Use a number of pixels")
  16754. (list :tag "Use #+ATTR* or a number of pixels" (integer))
  16755. (const :tag "Use #+ATTR* or don't resize" nil)))
  16756. (defcustom org-agenda-inhibit-startup nil
  16757. "Inhibit startup when preparing agenda buffers.
  16758. When this variable is t, the initialization of the Org agenda
  16759. buffers is inhibited: e.g. the visibility state is not set, the
  16760. tables are not re-aligned, etc."
  16761. :type 'boolean
  16762. :version "24.3"
  16763. :group 'org-agenda)
  16764. (define-obsolete-variable-alias
  16765. 'org-agenda-ignore-drawer-properties
  16766. 'org-agenda-ignore-properties "25.1")
  16767. (defcustom org-agenda-ignore-properties nil
  16768. "Avoid updating text properties when building the agenda.
  16769. Properties are used to prepare buffers for effort estimates,
  16770. appointments, statistics and subtree-local categories.
  16771. If you don't use these in the agenda, you can add them to this
  16772. list and agenda building will be a bit faster.
  16773. The value is a list, with zero or more of the symbols `effort', `appt',
  16774. `stats' or `category'."
  16775. :type '(set :greedy t
  16776. (const effort)
  16777. (const appt)
  16778. (const stats)
  16779. (const category))
  16780. :version "25.1"
  16781. :package-version '(Org . "8.3")
  16782. :group 'org-agenda)
  16783. (defun org-duration-string-to-minutes (s &optional output-to-string)
  16784. "Convert a duration string S to minutes.
  16785. A bare number is interpreted as minutes, modifiers can be set by
  16786. customizing `org-effort-durations' (which see).
  16787. Entries containing a colon are interpreted as H:MM by
  16788. `org-hh:mm-string-to-minutes'."
  16789. (let ((result 0)
  16790. (re (concat "\\([0-9.]+\\) *\\("
  16791. (regexp-opt (mapcar 'car org-effort-durations))
  16792. "\\)")))
  16793. (while (string-match re s)
  16794. (cl-incf result (* (cdr (assoc (match-string 2 s) org-effort-durations))
  16795. (string-to-number (match-string 1 s))))
  16796. (setq s (replace-match "" nil t s)))
  16797. (setq result (floor result))
  16798. (cl-incf result (org-hh:mm-string-to-minutes s))
  16799. (if output-to-string (number-to-string result) result)))
  16800. ;;;; Files
  16801. (defun org-save-all-org-buffers ()
  16802. "Save all Org-mode buffers without user confirmation."
  16803. (interactive)
  16804. (message "Saving all Org-mode buffers...")
  16805. (save-some-buffers t (lambda () (derived-mode-p 'org-mode)))
  16806. (when (featurep 'org-id) (org-id-locations-save))
  16807. (message "Saving all Org-mode buffers... done"))
  16808. (defun org-revert-all-org-buffers ()
  16809. "Revert all Org-mode buffers.
  16810. Prompt for confirmation when there are unsaved changes.
  16811. Be sure you know what you are doing before letting this function
  16812. overwrite your changes.
  16813. This function is useful in a setup where one tracks org files
  16814. with a version control system, to revert on one machine after pulling
  16815. changes from another. I believe the procedure must be like this:
  16816. 1. M-x org-save-all-org-buffers
  16817. 2. Pull changes from the other machine, resolve conflicts
  16818. 3. M-x org-revert-all-org-buffers"
  16819. (interactive)
  16820. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  16821. (user-error "Abort"))
  16822. (save-excursion
  16823. (save-window-excursion
  16824. (dolist (b (buffer-list))
  16825. (when (and (with-current-buffer b (derived-mode-p 'org-mode))
  16826. (with-current-buffer b buffer-file-name))
  16827. (org-pop-to-buffer-same-window b)
  16828. (revert-buffer t 'no-confirm)))
  16829. (when (and (featurep 'org-id) org-id-track-globally)
  16830. (org-id-locations-load)))))
  16831. ;;;; Agenda files
  16832. ;;;###autoload
  16833. (defun org-switchb (&optional arg)
  16834. "Switch between Org buffers.
  16835. With \\[universal-argument] prefix, restrict available buffers to files.
  16836. With \\[universal-argument] \\[universal-argument] \
  16837. prefix, restrict available buffers to agenda files."
  16838. (interactive "P")
  16839. (let ((blist (org-buffer-list
  16840. (cond ((equal arg '(4)) 'files)
  16841. ((equal arg '(16)) 'agenda)))))
  16842. (org-pop-to-buffer-same-window
  16843. (completing-read "Org buffer: "
  16844. (mapcar #'list (mapcar #'buffer-name blist))
  16845. nil t))))
  16846. (defun org-buffer-list (&optional predicate exclude-tmp)
  16847. "Return a list of Org buffers.
  16848. PREDICATE can be `export', `files' or `agenda'.
  16849. export restrict the list to Export buffers.
  16850. files restrict the list to buffers visiting Org files.
  16851. agenda restrict the list to buffers visiting agenda files.
  16852. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  16853. (let* ((bfn nil)
  16854. (agenda-files (and (eq predicate 'agenda)
  16855. (mapcar 'file-truename (org-agenda-files t))))
  16856. (filter
  16857. (cond
  16858. ((eq predicate 'files)
  16859. (lambda (b) (with-current-buffer b (derived-mode-p 'org-mode))))
  16860. ((eq predicate 'export)
  16861. (lambda (b) (string-match "\\*Org .*Export" (buffer-name b))))
  16862. ((eq predicate 'agenda)
  16863. (lambda (b)
  16864. (with-current-buffer b
  16865. (and (derived-mode-p 'org-mode)
  16866. (setq bfn (buffer-file-name b))
  16867. (member (file-truename bfn) agenda-files)))))
  16868. (t (lambda (b) (with-current-buffer b
  16869. (or (derived-mode-p 'org-mode)
  16870. (string-match "\\*Org .*Export"
  16871. (buffer-name b)))))))))
  16872. (delq nil
  16873. (mapcar
  16874. (lambda(b)
  16875. (if (and (funcall filter b)
  16876. (or (not exclude-tmp)
  16877. (not (string-match "tmp" (buffer-name b)))))
  16878. b
  16879. nil))
  16880. (buffer-list)))))
  16881. (defun org-agenda-files (&optional unrestricted archives)
  16882. "Get the list of agenda files.
  16883. Optional UNRESTRICTED means return the full list even if a restriction
  16884. is currently in place.
  16885. When ARCHIVES is t, include all archive files that are really being
  16886. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  16887. `org-agenda-archives-mode' is t."
  16888. (let ((files
  16889. (cond
  16890. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  16891. ((stringp org-agenda-files) (org-read-agenda-file-list))
  16892. ((listp org-agenda-files) org-agenda-files)
  16893. (t (error "Invalid value of `org-agenda-files'")))))
  16894. (setq files (apply 'append
  16895. (mapcar (lambda (f)
  16896. (if (file-directory-p f)
  16897. (directory-files
  16898. f t org-agenda-file-regexp)
  16899. (list f)))
  16900. files)))
  16901. (when org-agenda-skip-unavailable-files
  16902. (setq files (delq nil
  16903. (mapcar (function
  16904. (lambda (file)
  16905. (and (file-readable-p file) file)))
  16906. files))))
  16907. (when (or (eq archives t)
  16908. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  16909. (setq files (org-add-archive-files files)))
  16910. files))
  16911. (defun org-agenda-file-p (&optional file)
  16912. "Return non-nil, if FILE is an agenda file.
  16913. If FILE is omitted, use the file associated with the current
  16914. buffer."
  16915. (let ((fname (or file (buffer-file-name))))
  16916. (and fname
  16917. (member (file-truename fname)
  16918. (mapcar #'file-truename (org-agenda-files t))))))
  16919. (defun org-edit-agenda-file-list ()
  16920. "Edit the list of agenda files.
  16921. Depending on setup, this either uses customize to edit the variable
  16922. `org-agenda-files', or it visits the file that is holding the list. In the
  16923. latter case, the buffer is set up in a way that saving it automatically kills
  16924. the buffer and restores the previous window configuration."
  16925. (interactive)
  16926. (if (stringp org-agenda-files)
  16927. (let ((cw (current-window-configuration)))
  16928. (find-file org-agenda-files)
  16929. (setq-local org-window-configuration cw)
  16930. (org-add-hook 'after-save-hook
  16931. (lambda ()
  16932. (set-window-configuration
  16933. (prog1 org-window-configuration
  16934. (kill-buffer (current-buffer))))
  16935. (org-install-agenda-files-menu)
  16936. (message "New agenda file list installed"))
  16937. nil 'local)
  16938. (message "%s" (substitute-command-keys
  16939. "Edit list and finish with \\[save-buffer]")))
  16940. (customize-variable 'org-agenda-files)))
  16941. (defun org-store-new-agenda-file-list (list)
  16942. "Set new value for the agenda file list and save it correctly."
  16943. (if (stringp org-agenda-files)
  16944. (let ((fe (org-read-agenda-file-list t)) b u)
  16945. (while (setq b (find-buffer-visiting org-agenda-files))
  16946. (kill-buffer b))
  16947. (with-temp-file org-agenda-files
  16948. (insert
  16949. (mapconcat
  16950. (lambda (f) ;; Keep un-expanded entries.
  16951. (if (setq u (assoc f fe))
  16952. (cdr u)
  16953. f))
  16954. list "\n")
  16955. "\n")))
  16956. (let ((org-mode-hook nil) (org-inhibit-startup t)
  16957. (org-insert-mode-line-in-empty-file nil))
  16958. (setq org-agenda-files list)
  16959. (customize-save-variable 'org-agenda-files org-agenda-files))))
  16960. (defun org-read-agenda-file-list (&optional pair-with-expansion)
  16961. "Read the list of agenda files from a file.
  16962. If PAIR-WITH-EXPANSION is t return pairs with un-expanded
  16963. filenames, used by `org-store-new-agenda-file-list' to write back
  16964. un-expanded file names."
  16965. (when (file-directory-p org-agenda-files)
  16966. (error "`org-agenda-files' cannot be a single directory"))
  16967. (when (stringp org-agenda-files)
  16968. (with-temp-buffer
  16969. (insert-file-contents org-agenda-files)
  16970. (mapcar
  16971. (lambda (f)
  16972. (let ((e (expand-file-name (substitute-in-file-name f)
  16973. org-directory)))
  16974. (if pair-with-expansion
  16975. (cons e f)
  16976. e)))
  16977. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*")))))
  16978. ;;;###autoload
  16979. (defun org-cycle-agenda-files ()
  16980. "Cycle through the files in `org-agenda-files'.
  16981. If the current buffer visits an agenda file, find the next one in the list.
  16982. If the current buffer does not, find the first agenda file."
  16983. (interactive)
  16984. (let* ((fs (or (org-agenda-files t)
  16985. (user-error "No agenda files")))
  16986. (files (copy-sequence fs))
  16987. (tcf (and buffer-file-name (file-truename buffer-file-name)))
  16988. file)
  16989. (when tcf
  16990. (while (and (setq file (pop files))
  16991. (not (equal (file-truename file) tcf)))))
  16992. (find-file (car (or files fs)))
  16993. (when (buffer-base-buffer) (org-pop-to-buffer-same-window (buffer-base-buffer)))))
  16994. (defun org-agenda-file-to-front (&optional to-end)
  16995. "Move/add the current file to the top of the agenda file list.
  16996. If the file is not present in the list, it is added to the front. If it is
  16997. present, it is moved there. With optional argument TO-END, add/move to the
  16998. end of the list."
  16999. (interactive "P")
  17000. (let ((org-agenda-skip-unavailable-files nil)
  17001. (file-alist (mapcar (lambda (x)
  17002. (cons (file-truename x) x))
  17003. (org-agenda-files t)))
  17004. (ctf (file-truename
  17005. (or buffer-file-name
  17006. (user-error "Please save the current buffer to a file"))))
  17007. x had)
  17008. (setq x (assoc ctf file-alist) had x)
  17009. (unless x (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  17010. (if to-end
  17011. (setq file-alist (append (delq x file-alist) (list x)))
  17012. (setq file-alist (cons x (delq x file-alist))))
  17013. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  17014. (org-install-agenda-files-menu)
  17015. (message "File %s to %s of agenda file list"
  17016. (if had "moved" "added") (if to-end "end" "front"))))
  17017. (defun org-remove-file (&optional file)
  17018. "Remove current file from the list of files in variable `org-agenda-files'.
  17019. These are the files which are being checked for agenda entries.
  17020. Optional argument FILE means use this file instead of the current."
  17021. (interactive)
  17022. (let* ((org-agenda-skip-unavailable-files nil)
  17023. (file (or file buffer-file-name
  17024. (user-error "Current buffer does not visit a file")))
  17025. (true-file (file-truename file))
  17026. (afile (abbreviate-file-name file))
  17027. (files (delq nil (mapcar
  17028. (lambda (x)
  17029. (unless (equal true-file
  17030. (file-truename x))
  17031. x))
  17032. (org-agenda-files t)))))
  17033. (if (not (= (length files) (length (org-agenda-files t))))
  17034. (progn
  17035. (org-store-new-agenda-file-list files)
  17036. (org-install-agenda-files-menu)
  17037. (message "Removed from Org Agenda list: %s" afile))
  17038. (message "File was not in list: %s (not removed)" afile))))
  17039. (defun org-file-menu-entry (file)
  17040. (vector file (list 'find-file file) t))
  17041. (defun org-check-agenda-file (file)
  17042. "Make sure FILE exists. If not, ask user what to do."
  17043. (unless (file-exists-p file)
  17044. (message "Non-existent agenda file %s. [R]emove from list or [A]bort?"
  17045. (abbreviate-file-name file))
  17046. (let ((r (downcase (read-char-exclusive))))
  17047. (cond
  17048. ((equal r ?r)
  17049. (org-remove-file file)
  17050. (throw 'nextfile t))
  17051. (t (user-error "Abort"))))))
  17052. (defun org-get-agenda-file-buffer (file)
  17053. "Get an agenda buffer visiting FILE.
  17054. If the buffer needs to be created, add it to the list of buffers
  17055. which might be released later."
  17056. (let ((buf (org-find-base-buffer-visiting file)))
  17057. (if buf
  17058. buf ; just return it
  17059. ;; Make a new buffer and remember it
  17060. (setq buf (find-file-noselect file))
  17061. (when buf (push buf org-agenda-new-buffers))
  17062. buf)))
  17063. (defun org-release-buffers (blist)
  17064. "Release all buffers in list, asking the user for confirmation when needed.
  17065. When a buffer is unmodified, it is just killed. When modified, it is saved
  17066. \(if the user agrees) and then killed."
  17067. (let (file)
  17068. (dolist (buf blist)
  17069. (setq file (buffer-file-name buf))
  17070. (when (and (buffer-modified-p buf)
  17071. file
  17072. (y-or-n-p (format "Save file %s? " file)))
  17073. (with-current-buffer buf (save-buffer)))
  17074. (kill-buffer buf))))
  17075. (defun org-agenda-prepare-buffers (files)
  17076. "Create buffers for all agenda files, protect archived trees and comments."
  17077. (interactive)
  17078. (let ((pa '(:org-archived t))
  17079. (pc '(:org-comment t))
  17080. (pall '(:org-archived t :org-comment t))
  17081. (inhibit-read-only t)
  17082. (org-inhibit-startup org-agenda-inhibit-startup)
  17083. (rea (concat ":" org-archive-tag ":"))
  17084. re pos)
  17085. (setq org-tag-alist-for-agenda nil
  17086. org-tag-groups-alist-for-agenda nil)
  17087. (save-excursion
  17088. (save-restriction
  17089. (dolist (file files)
  17090. (catch 'nextfile
  17091. (if (bufferp file)
  17092. (set-buffer file)
  17093. (org-check-agenda-file file)
  17094. (set-buffer (org-get-agenda-file-buffer file)))
  17095. (widen)
  17096. (org-set-regexps-and-options 'tags-only)
  17097. (setq pos (point))
  17098. (or (memq 'category org-agenda-ignore-properties)
  17099. (org-refresh-category-properties))
  17100. (or (memq 'stats org-agenda-ignore-properties)
  17101. (org-refresh-stats-properties))
  17102. (or (memq 'effort org-agenda-ignore-properties)
  17103. (org-refresh-effort-properties))
  17104. (or (memq 'appt org-agenda-ignore-properties)
  17105. (org-refresh-properties "APPT_WARNTIME" 'org-appt-warntime))
  17106. (setq org-todo-keywords-for-agenda
  17107. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  17108. (setq org-done-keywords-for-agenda
  17109. (append org-done-keywords-for-agenda org-done-keywords))
  17110. (setq org-todo-keyword-alist-for-agenda
  17111. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  17112. (setq org-tag-alist-for-agenda
  17113. (org-uniquify
  17114. (append org-tag-alist-for-agenda
  17115. org-tag-alist
  17116. org-tag-persistent-alist)))
  17117. ;; Merge current file's tag groups into global
  17118. ;; `org-tag-groups-alist-for-agenda'.
  17119. (when org-group-tags
  17120. (dolist (alist org-tag-groups-alist)
  17121. (let ((old (assoc (car alist) org-tag-groups-alist-for-agenda)))
  17122. (if old
  17123. (setcdr old (org-uniquify (append (cdr old) (cdr alist))))
  17124. (push alist org-tag-groups-alist-for-agenda)))))
  17125. (org-with-silent-modifications
  17126. (save-excursion
  17127. (remove-text-properties (point-min) (point-max) pall)
  17128. (when org-agenda-skip-archived-trees
  17129. (goto-char (point-min))
  17130. (while (re-search-forward rea nil t)
  17131. (when (org-at-heading-p t)
  17132. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  17133. (goto-char (point-min))
  17134. (setq re (format "^\\*+ .*\\<%s\\>" org-comment-string))
  17135. (while (re-search-forward re nil t)
  17136. (when (save-match-data (org-in-commented-heading-p t))
  17137. (add-text-properties
  17138. (match-beginning 0) (org-end-of-subtree t) pc)))))
  17139. (goto-char pos)))))
  17140. (setq org-todo-keywords-for-agenda
  17141. (org-uniquify org-todo-keywords-for-agenda))
  17142. (setq org-todo-keyword-alist-for-agenda
  17143. (org-uniquify org-todo-keyword-alist-for-agenda))))
  17144. ;;;; CDLaTeX minor mode
  17145. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  17146. "Keymap for the minor `org-cdlatex-mode'.")
  17147. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  17148. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  17149. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  17150. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  17151. (org-defkey org-cdlatex-mode-map "\C-c{" 'org-cdlatex-environment-indent)
  17152. (defvar org-cdlatex-texmathp-advice-is-done nil
  17153. "Flag remembering if we have applied the advice to texmathp already.")
  17154. (define-minor-mode org-cdlatex-mode
  17155. "Toggle the minor `org-cdlatex-mode'.
  17156. This mode supports entering LaTeX environment and math in LaTeX fragments
  17157. in Org-mode.
  17158. \\{org-cdlatex-mode-map}"
  17159. nil " OCDL" nil
  17160. (when org-cdlatex-mode
  17161. (require 'cdlatex)
  17162. (run-hooks 'cdlatex-mode-hook)
  17163. (cdlatex-compute-tables))
  17164. (unless org-cdlatex-texmathp-advice-is-done
  17165. (setq org-cdlatex-texmathp-advice-is-done t)
  17166. (defadvice texmathp (around org-math-always-on activate)
  17167. "Always return t in org-mode buffers.
  17168. This is because we want to insert math symbols without dollars even outside
  17169. the LaTeX math segments. If Orgmode thinks that point is actually inside
  17170. an embedded LaTeX fragment, let texmathp do its job.
  17171. \\[org-cdlatex-mode-map]"
  17172. (interactive)
  17173. (let (p)
  17174. (cond
  17175. ((not (derived-mode-p 'org-mode)) ad-do-it)
  17176. ((eq this-command 'cdlatex-math-symbol)
  17177. (setq ad-return-value t
  17178. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  17179. (t
  17180. (let ((p (org-inside-LaTeX-fragment-p)))
  17181. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  17182. (setq ad-return-value t
  17183. texmathp-why '("Org-mode embedded math" . 0))
  17184. (when p ad-do-it)))))))))
  17185. (defun turn-on-org-cdlatex ()
  17186. "Unconditionally turn on `org-cdlatex-mode'."
  17187. (org-cdlatex-mode 1))
  17188. (defun org-try-cdlatex-tab ()
  17189. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  17190. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  17191. - inside a LaTeX fragment, or
  17192. - after the first word in a line, where an abbreviation expansion could
  17193. insert a LaTeX environment."
  17194. (when org-cdlatex-mode
  17195. (cond
  17196. ;; Before any word on the line: No expansion possible.
  17197. ((save-excursion (skip-chars-backward " \t") (bolp)) nil)
  17198. ;; Just after first word on the line: Expand it. Make sure it
  17199. ;; cannot happen on headlines, though.
  17200. ((save-excursion
  17201. (skip-chars-backward "a-zA-Z0-9*")
  17202. (skip-chars-backward " \t")
  17203. (and (bolp) (not (org-at-heading-p))))
  17204. (cdlatex-tab) t)
  17205. ((org-inside-LaTeX-fragment-p) (cdlatex-tab) t))))
  17206. (defun org-cdlatex-underscore-caret (&optional _arg)
  17207. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  17208. Revert to the normal definition outside of these fragments."
  17209. (interactive "P")
  17210. (if (org-inside-LaTeX-fragment-p)
  17211. (call-interactively 'cdlatex-sub-superscript)
  17212. (let (org-cdlatex-mode)
  17213. (call-interactively (key-binding (vector last-input-event))))))
  17214. (defun org-cdlatex-math-modify (&optional _arg)
  17215. "Execute `cdlatex-math-modify' in LaTeX fragments.
  17216. Revert to the normal definition outside of these fragments."
  17217. (interactive "P")
  17218. (if (org-inside-LaTeX-fragment-p)
  17219. (call-interactively 'cdlatex-math-modify)
  17220. (let (org-cdlatex-mode)
  17221. (call-interactively (key-binding (vector last-input-event))))))
  17222. (defun org-cdlatex-environment-indent (&optional environment item)
  17223. "Execute `cdlatex-environment' and indent the inserted environment.
  17224. ENVIRONMENT and ITEM are passed to `cdlatex-environment'.
  17225. The inserted environment is indented to current indentation
  17226. unless point is at the beginning of the line, in which the
  17227. environment remains unintended."
  17228. (interactive)
  17229. ;; cdlatex-environment always return nil. Therefore, capture output
  17230. ;; first and determine if an environment was selected.
  17231. (let* ((beg (point-marker))
  17232. (end (copy-marker (point) t))
  17233. (inserted (progn
  17234. (ignore-errors (cdlatex-environment environment item))
  17235. (< beg end)))
  17236. ;; Figure out how many lines to move forward after the
  17237. ;; environment has been inserted.
  17238. (lines (when inserted
  17239. (save-excursion
  17240. (- (cl-loop while (< beg (point))
  17241. with x = 0
  17242. do (forward-line -1)
  17243. (cl-incf x)
  17244. finally return x)
  17245. (if (progn (goto-char beg)
  17246. (and (progn (skip-chars-forward " \t") (eolp))
  17247. (progn (skip-chars-backward " \t") (bolp))))
  17248. 1 0)))))
  17249. (env (org-trim (delete-and-extract-region beg end))))
  17250. (when inserted
  17251. ;; Get indentation of next line unless at column 0.
  17252. (let ((ind (if (bolp) 0
  17253. (save-excursion
  17254. (org-return-indent)
  17255. (prog1 (org-get-indentation)
  17256. (when (progn (skip-chars-forward " \t") (eolp))
  17257. (delete-region beg (point)))))))
  17258. (bol (progn (skip-chars-backward " \t") (bolp))))
  17259. ;; Insert a newline before environment unless at column zero
  17260. ;; to "escape" the current line. Insert a newline if
  17261. ;; something is one the same line as \end{ENVIRONMENT}.
  17262. (insert
  17263. (concat (unless bol "\n") env
  17264. (when (and (skip-chars-forward " \t") (not (eolp))) "\n")))
  17265. (unless (zerop ind)
  17266. (save-excursion
  17267. (goto-char beg)
  17268. (while (< (point) end)
  17269. (unless (eolp) (org-indent-line-to ind))
  17270. (forward-line))))
  17271. (goto-char beg)
  17272. (forward-line lines)
  17273. (org-indent-line-to ind)))
  17274. (set-marker beg nil)
  17275. (set-marker end nil)))
  17276. ;;;; LaTeX fragments
  17277. (defun org-inside-LaTeX-fragment-p ()
  17278. "Test if point is inside a LaTeX fragment.
  17279. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  17280. sequence appearing also before point.
  17281. Even though the matchers for math are configurable, this function assumes
  17282. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  17283. delimiters are skipped when they have been removed by customization.
  17284. The return value is nil, or a cons cell with the delimiter and the
  17285. position of this delimiter.
  17286. This function does a reasonably good job, but can locally be fooled by
  17287. for example currency specifications. For example it will assume being in
  17288. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  17289. fragments that are properly closed, but during editing, we have to live
  17290. with the uncertainty caused by missing closing delimiters. This function
  17291. looks only before point, not after."
  17292. (catch 'exit
  17293. (let ((pos (point))
  17294. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  17295. (lim (progn
  17296. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  17297. (point)))
  17298. dd-on str (start 0) m re)
  17299. (goto-char pos)
  17300. (when dodollar
  17301. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  17302. re (nth 1 (assoc "$" org-latex-regexps)))
  17303. (while (string-match re str start)
  17304. (cond
  17305. ((= (match-end 0) (length str))
  17306. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  17307. ((= (match-end 0) (- (length str) 5))
  17308. (throw 'exit nil))
  17309. (t (setq start (match-end 0))))))
  17310. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  17311. (goto-char pos)
  17312. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  17313. (and (match-beginning 2) (throw 'exit nil))
  17314. ;; count $$
  17315. (while (re-search-backward "\\$\\$" lim t)
  17316. (setq dd-on (not dd-on)))
  17317. (goto-char pos)
  17318. (when dd-on (cons "$$" m))))))
  17319. (defun org-inside-latex-macro-p ()
  17320. "Is point inside a LaTeX macro or its arguments?"
  17321. (save-match-data
  17322. (org-in-regexp
  17323. "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")))
  17324. (defun org--format-latex-make-overlay (beg end image)
  17325. "Build an overlay between BEG and END using IMAGE file."
  17326. (let ((ov (make-overlay beg end)))
  17327. (overlay-put ov 'org-overlay-type 'org-latex-overlay)
  17328. (overlay-put ov 'evaporate t)
  17329. (overlay-put ov
  17330. 'modification-hooks
  17331. (list (lambda (o _flag _beg _end &optional _l)
  17332. (delete-overlay o))))
  17333. (if (featurep 'xemacs)
  17334. (progn
  17335. (overlay-put ov 'invisible t)
  17336. (overlay-put ov 'end-glyph (make-glyph (vector 'png :file image))))
  17337. (overlay-put ov
  17338. 'display
  17339. (list 'image :type 'png :file image :ascent 'center)))))
  17340. (defun org--list-latex-overlays (&optional beg end)
  17341. "List all Org LaTeX overlays in current buffer.
  17342. Limit to overlays between BEG and END when those are provided."
  17343. (cl-remove-if-not
  17344. (lambda (o) (eq (overlay-get o 'org-overlay-type) 'org-latex-overlay))
  17345. (overlays-in (or beg (point-min)) (or end (point-max)))))
  17346. (defun org-remove-latex-fragment-image-overlays (&optional beg end)
  17347. "Remove all overlays with LaTeX fragment images in current buffer.
  17348. When optional arguments BEG and END are non-nil, remove all
  17349. overlays between them instead. Return a non-nil value when some
  17350. overlays were removed, nil otherwise."
  17351. (let ((overlays (org--list-latex-overlays beg end)))
  17352. (mapc #'delete-overlay overlays)
  17353. overlays))
  17354. (define-obsolete-function-alias
  17355. 'org-preview-latex-fragment 'org-toggle-latex-fragment "24.4")
  17356. (defun org-toggle-latex-fragment (&optional arg)
  17357. "Preview the LaTeX fragment at point, or all locally or globally.
  17358. If the cursor is on a LaTeX fragment, create the image and overlay
  17359. it over the source code, if there is none. Remove it otherwise.
  17360. If there is no fragment at point, display all fragments in the
  17361. current section.
  17362. With prefix ARG, preview or clear image for all fragments in the
  17363. current subtree or in the whole buffer when used before the first
  17364. headline. With a double prefix ARG \\[universal-argument] \
  17365. \\[universal-argument] preview or clear images
  17366. for all fragments in the buffer."
  17367. (interactive "P")
  17368. (when (display-graphic-p)
  17369. (catch 'exit
  17370. (save-excursion
  17371. (let ((window-start (window-start)) msg)
  17372. (save-restriction
  17373. (cond
  17374. ((or (equal arg '(16))
  17375. (and (equal arg '(4))
  17376. (org-with-limited-levels (org-before-first-heading-p))))
  17377. (if (org-remove-latex-fragment-image-overlays)
  17378. (progn (message "LaTeX fragments images removed from buffer")
  17379. (throw 'exit nil))
  17380. (setq msg "Creating images for buffer...")))
  17381. ((equal arg '(4))
  17382. (org-with-limited-levels (org-back-to-heading t))
  17383. (let ((beg (point))
  17384. (end (progn (org-end-of-subtree t) (point))))
  17385. (if (org-remove-latex-fragment-image-overlays beg end)
  17386. (progn
  17387. (message "LaTeX fragment images removed from subtree")
  17388. (throw 'exit nil))
  17389. (setq msg "Creating images for subtree...")
  17390. (narrow-to-region beg end))))
  17391. ((let ((datum (org-element-context)))
  17392. (when (memq (org-element-type datum)
  17393. '(latex-environment latex-fragment))
  17394. (let* ((beg (org-element-property :begin datum))
  17395. (end (org-element-property :end datum)))
  17396. (if (org-remove-latex-fragment-image-overlays beg end)
  17397. (progn (message "LaTeX fragment image removed")
  17398. (throw 'exit nil))
  17399. (narrow-to-region beg end)
  17400. (setq msg "Creating image..."))))))
  17401. (t
  17402. (org-with-limited-levels
  17403. (let ((beg (if (org-at-heading-p) (line-beginning-position)
  17404. (outline-previous-heading)
  17405. (point)))
  17406. (end (progn (outline-next-heading) (point))))
  17407. (if (org-remove-latex-fragment-image-overlays beg end)
  17408. (progn
  17409. (message "LaTeX fragment images removed from section")
  17410. (throw 'exit nil))
  17411. (setq msg "Creating images for section...")
  17412. (narrow-to-region beg end))))))
  17413. (let ((file (buffer-file-name (buffer-base-buffer))))
  17414. (org-format-latex
  17415. (concat org-latex-preview-ltxpng-directory "org-ltxpng")
  17416. ;; Emacs cannot overlay images from remote hosts.
  17417. ;; Create it in `temporary-file-directory' instead.
  17418. (if (or (not file) (file-remote-p file))
  17419. temporary-file-directory
  17420. default-directory)
  17421. 'overlays msg 'forbuffer
  17422. org-latex-create-formula-image-program)))
  17423. ;; Work around a bug that doesn't restore window's start
  17424. ;; when widening back the buffer.
  17425. (set-window-start nil window-start)
  17426. (message (concat msg "done")))))))
  17427. (defun org-format-latex
  17428. (prefix &optional dir overlays msg forbuffer processing-type)
  17429. "Replace LaTeX fragments with links to an image, and produce images.
  17430. When optional argument OVERLAYS is non-nil, display the image on
  17431. top of the fragment instead of replacing it.
  17432. PROCESSING-TYPE is the conversion method to use, as a symbol.
  17433. Some of the options can be changed using the variable
  17434. `org-format-latex-options', which see."
  17435. (when (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  17436. (unless (eq processing-type 'verbatim)
  17437. (let* ((math-regexp "\\$\\|\\\\[([]\\|^[ \t]*\\\\begin{[A-Za-z0-9*]+}")
  17438. (cnt 0)
  17439. checkdir-flag)
  17440. (goto-char (point-min))
  17441. ;; Optimize overlay creation: (info "(elisp) Managing Overlays").
  17442. (when (and overlays (memq processing-type '(dvipng imagemagick)))
  17443. (overlay-recenter (point-max)))
  17444. (while (re-search-forward math-regexp nil t)
  17445. (unless (and overlays
  17446. (eq (get-char-property (point) 'org-overlay-type)
  17447. 'org-latex-overlay))
  17448. (let* ((context (org-element-context))
  17449. (type (org-element-type context)))
  17450. (when (memq type '(latex-environment latex-fragment))
  17451. (let ((block-type (eq type 'latex-environment))
  17452. (value (org-element-property :value context))
  17453. (beg (org-element-property :begin context))
  17454. (end (save-excursion
  17455. (goto-char (org-element-property :end context))
  17456. (skip-chars-backward " \r\t\n")
  17457. (point))))
  17458. (cl-case processing-type
  17459. (mathjax
  17460. ;; Prepare for MathJax processing.
  17461. (if (not (string-match "\\`\\$\\$?" value))
  17462. (goto-char end)
  17463. (delete-region beg end)
  17464. (if (string= (match-string 0 value) "$$")
  17465. (insert "\\[" (substring value 2 -2) "\\]")
  17466. (insert "\\(" (substring value 1 -1) "\\)"))))
  17467. ((dvipng imagemagick)
  17468. ;; Process to an image.
  17469. (cl-incf cnt)
  17470. (goto-char beg)
  17471. (let* ((face (face-at-point))
  17472. ;; Get the colors from the face at point.
  17473. (fg
  17474. (let ((color (plist-get org-format-latex-options
  17475. :foreground)))
  17476. (if (and forbuffer (eq color 'auto))
  17477. (face-attribute face :foreground nil 'default)
  17478. color)))
  17479. (bg
  17480. (let ((color (plist-get org-format-latex-options
  17481. :background)))
  17482. (if (and forbuffer (eq color 'auto))
  17483. (face-attribute face :background nil 'default)
  17484. color)))
  17485. (hash (sha1 (prin1-to-string
  17486. (list org-format-latex-header
  17487. org-latex-default-packages-alist
  17488. org-latex-packages-alist
  17489. org-format-latex-options
  17490. forbuffer value fg bg))))
  17491. (absprefix (expand-file-name prefix dir))
  17492. (linkfile (format "%s_%s.png" prefix hash))
  17493. (movefile (format "%s_%s.png" absprefix hash))
  17494. (sep (and block-type "\n\n"))
  17495. (link (concat sep "[[file:" linkfile "]]" sep))
  17496. (options
  17497. (org-combine-plists
  17498. org-format-latex-options
  17499. `(:foreground ,fg :background ,bg))))
  17500. (when msg (message msg cnt))
  17501. (unless checkdir-flag ; Ensure the directory exists.
  17502. (setq checkdir-flag t)
  17503. (let ((todir (file-name-directory absprefix)))
  17504. (unless (file-directory-p todir)
  17505. (make-directory todir t))))
  17506. (unless (file-exists-p movefile)
  17507. (org-create-formula-image
  17508. value movefile options forbuffer processing-type))
  17509. (if overlays
  17510. (progn
  17511. (dolist (o (overlays-in beg end))
  17512. (when (eq (overlay-get o 'org-overlay-type)
  17513. 'org-latex-overlay)
  17514. (delete-overlay o)))
  17515. (org--format-latex-make-overlay beg end movefile)
  17516. (goto-char end))
  17517. (delete-region beg end)
  17518. (insert
  17519. (org-add-props link
  17520. (list 'org-latex-src
  17521. (replace-regexp-in-string "\"" "" value)
  17522. 'org-latex-src-embed-type
  17523. (if block-type 'paragraph 'character)))))))
  17524. (mathml
  17525. ;; Process to MathML.
  17526. (unless (org-format-latex-mathml-available-p)
  17527. (user-error "LaTeX to MathML converter not configured"))
  17528. (cl-incf cnt)
  17529. (when msg (message msg cnt))
  17530. (goto-char beg)
  17531. (delete-region beg end)
  17532. (insert (org-format-latex-as-mathml
  17533. value block-type prefix dir)))
  17534. (otherwise
  17535. (error "Unknown conversion type %s for LaTeX fragments"
  17536. processing-type)))))))))))
  17537. (defun org-create-math-formula (latex-frag &optional mathml-file)
  17538. "Convert LATEX-FRAG to MathML and store it in MATHML-FILE.
  17539. Use `org-latex-to-mathml-convert-command'. If the conversion is
  17540. sucessful, return the portion between \"<math...> </math>\"
  17541. elements otherwise return nil. When MATHML-FILE is specified,
  17542. write the results in to that file. When invoked as an
  17543. interactive command, prompt for LATEX-FRAG, with initial value
  17544. set to the current active region and echo the results for user
  17545. inspection."
  17546. (interactive (list (let ((frag (when (org-region-active-p)
  17547. (buffer-substring-no-properties
  17548. (region-beginning) (region-end)))))
  17549. (read-string "LaTeX Fragment: " frag nil frag))))
  17550. (unless latex-frag (user-error "Invalid LaTeX fragment"))
  17551. (let* ((tmp-in-file
  17552. (let ((file (file-relative-name
  17553. (make-temp-name (expand-file-name "ltxmathml-in")))))
  17554. (write-region latex-frag nil file)
  17555. file))
  17556. (tmp-out-file (file-relative-name
  17557. (make-temp-name (expand-file-name "ltxmathml-out"))))
  17558. (cmd (format-spec
  17559. org-latex-to-mathml-convert-command
  17560. `((?j . ,(and org-latex-to-mathml-jar-file
  17561. (shell-quote-argument
  17562. (expand-file-name
  17563. org-latex-to-mathml-jar-file))))
  17564. (?I . ,(shell-quote-argument tmp-in-file))
  17565. (?i . ,latex-frag)
  17566. (?o . ,(shell-quote-argument tmp-out-file)))))
  17567. mathml shell-command-output)
  17568. (when (org-called-interactively-p 'any)
  17569. (unless (org-format-latex-mathml-available-p)
  17570. (user-error "LaTeX to MathML converter not configured")))
  17571. (message "Running %s" cmd)
  17572. (setq shell-command-output (shell-command-to-string cmd))
  17573. (setq mathml
  17574. (when (file-readable-p tmp-out-file)
  17575. (with-current-buffer (find-file-noselect tmp-out-file t)
  17576. (goto-char (point-min))
  17577. (when (re-search-forward
  17578. (concat
  17579. (regexp-quote
  17580. "<math xmlns=\"http://www.w3.org/1998/Math/MathML\"")
  17581. "[^>]*?>"
  17582. "\\(.\\|\n\\)*"
  17583. "</math>")
  17584. nil t)
  17585. (prog1 (match-string 0) (kill-buffer))))))
  17586. (cond
  17587. (mathml
  17588. (setq mathml
  17589. (concat "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" mathml))
  17590. (when mathml-file
  17591. (write-region mathml nil mathml-file))
  17592. (when (org-called-interactively-p 'any)
  17593. (message mathml)))
  17594. ((message "LaTeX to MathML conversion failed")
  17595. (message shell-command-output)))
  17596. (delete-file tmp-in-file)
  17597. (when (file-exists-p tmp-out-file)
  17598. (delete-file tmp-out-file))
  17599. mathml))
  17600. (defun org-format-latex-as-mathml (latex-frag latex-frag-type
  17601. prefix &optional dir)
  17602. "Use `org-create-math-formula' but check local cache first."
  17603. (let* ((absprefix (expand-file-name prefix dir))
  17604. (print-length nil) (print-level nil)
  17605. (formula-id (concat
  17606. "formula-"
  17607. (sha1
  17608. (prin1-to-string
  17609. (list latex-frag
  17610. org-latex-to-mathml-convert-command)))))
  17611. (formula-cache (format "%s-%s.mathml" absprefix formula-id))
  17612. (formula-cache-dir (file-name-directory formula-cache)))
  17613. (unless (file-directory-p formula-cache-dir)
  17614. (make-directory formula-cache-dir t))
  17615. (unless (file-exists-p formula-cache)
  17616. (org-create-math-formula latex-frag formula-cache))
  17617. (if (file-exists-p formula-cache)
  17618. ;; Successful conversion. Return the link to MathML file.
  17619. (org-add-props
  17620. (format "[[file:%s]]" (file-relative-name formula-cache dir))
  17621. (list 'org-latex-src (replace-regexp-in-string "\"" "" latex-frag)
  17622. 'org-latex-src-embed-type (if latex-frag-type
  17623. 'paragraph 'character)))
  17624. ;; Failed conversion. Return the LaTeX fragment verbatim
  17625. latex-frag)))
  17626. (defun org-create-formula-image (string tofile options buffer &optional type)
  17627. "Create an image from LaTeX source using dvipng or convert.
  17628. This function calls either `org-create-formula-image-with-dvipng'
  17629. or `org-create-formula-image-with-imagemagick' depending on the
  17630. value of `org-latex-create-formula-image-program' or on the value
  17631. of the optional TYPE variable.
  17632. Note: ultimately these two function should be combined as they
  17633. share a good deal of logic."
  17634. (org-check-external-command
  17635. "latex" "needed to convert LaTeX fragments to images")
  17636. (funcall
  17637. (cl-case (or type org-latex-create-formula-image-program)
  17638. (dvipng
  17639. (org-check-external-command
  17640. "dvipng" "needed to convert LaTeX fragments to images")
  17641. #'org-create-formula-image-with-dvipng)
  17642. (imagemagick
  17643. (org-check-external-command
  17644. "convert" "you need to install imagemagick")
  17645. #'org-create-formula-image-with-imagemagick)
  17646. (t (error
  17647. "Invalid value of `org-latex-create-formula-image-program'")))
  17648. string tofile options buffer))
  17649. (declare-function org-export-get-backend "ox" (name))
  17650. (declare-function org-export--get-global-options "ox" (&optional backend))
  17651. (declare-function org-export--get-inbuffer-options "ox" (&optional backend))
  17652. (declare-function org-latex-guess-inputenc "ox-latex" (header))
  17653. (declare-function org-latex-guess-babel-language "ox-latex" (header info))
  17654. (defun org-create-formula--latex-header ()
  17655. "Return LaTeX header appropriate for previewing a LaTeX snippet."
  17656. (let ((info (org-combine-plists (org-export--get-global-options
  17657. (org-export-get-backend 'latex))
  17658. (org-export--get-inbuffer-options
  17659. (org-export-get-backend 'latex)))))
  17660. (org-latex-guess-babel-language
  17661. (org-latex-guess-inputenc
  17662. (org-splice-latex-header
  17663. org-format-latex-header
  17664. org-latex-default-packages-alist
  17665. org-latex-packages-alist t
  17666. (plist-get info :latex-header)))
  17667. info)))
  17668. (defun org--get-display-dpi ()
  17669. "Get the DPI of the display.
  17670. Assumes that the display has the same pixel width in the
  17671. horizontal and vertical directions."
  17672. (if (display-graphic-p)
  17673. (round (/ (display-pixel-height)
  17674. (/ (display-mm-height) 25.4)))
  17675. (error "Attempt to calculate the dpi of a non-graphic display")))
  17676. ;; This function borrows from Ganesh Swami's latex2png.el
  17677. (defun org-create-formula-image-with-dvipng (string tofile options buffer)
  17678. "This calls dvipng."
  17679. (require 'ox-latex)
  17680. (let* ((tmpdir (if (featurep 'xemacs)
  17681. (temp-directory)
  17682. temporary-file-directory))
  17683. (texfilebase (make-temp-name
  17684. (expand-file-name "orgtex" tmpdir)))
  17685. (texfile (concat texfilebase ".tex"))
  17686. (dvifile (concat texfilebase ".dvi"))
  17687. (pngfile (concat texfilebase ".png"))
  17688. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  17689. ;; This assumes that the display has the same pixel width in
  17690. ;; the horizontal and vertical directions
  17691. (dpi (number-to-string (* scale (if buffer (org--get-display-dpi) 120))))
  17692. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  17693. "Black"))
  17694. (bg (or (plist-get options (if buffer :background :html-background))
  17695. "Transparent")))
  17696. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground))
  17697. (unless (string= fg "Transparent") (setq fg (org-dvipng-color-format fg))))
  17698. (if (eq bg 'default) (setq bg (org-dvipng-color :background))
  17699. (unless (string= bg "Transparent") (setq bg (org-dvipng-color-format bg))))
  17700. (let ((latex-header (org-create-formula--latex-header)))
  17701. (with-temp-file texfile
  17702. (insert latex-header)
  17703. (insert "\n\\begin{document}\n" string "\n\\end{document}\n")))
  17704. (let ((dir default-directory))
  17705. (ignore-errors
  17706. (cd tmpdir)
  17707. (call-process "latex" nil nil nil texfile))
  17708. (cd dir))
  17709. (if (not (file-exists-p dvifile))
  17710. (progn (message "Failed to create dvi file from %s" texfile) nil)
  17711. (ignore-errors
  17712. (if (featurep 'xemacs)
  17713. (call-process "dvipng" nil nil nil
  17714. "-fg" fg "-bg" bg
  17715. "-T" "tight"
  17716. "-o" pngfile
  17717. dvifile)
  17718. (call-process "dvipng" nil nil nil
  17719. "-fg" fg "-bg" bg
  17720. "-D" dpi
  17721. ;;"-x" scale "-y" scale
  17722. "-T" "tight"
  17723. "-o" pngfile
  17724. dvifile)))
  17725. (if (not (file-exists-p pngfile))
  17726. (if org-format-latex-signal-error
  17727. (error "Failed to create png file from %s" texfile)
  17728. (message "Failed to create png file from %s" texfile)
  17729. nil)
  17730. ;; Use the requested file name and clean up
  17731. (copy-file pngfile tofile 'replace)
  17732. (dolist (e '(".dvi" ".tex" ".aux" ".log" ".png" ".out"))
  17733. (when (file-exists-p (concat texfilebase e))
  17734. (delete-file (concat texfilebase e))))
  17735. pngfile))))
  17736. (declare-function org-latex-compile "ox-latex" (texfile &optional snippet))
  17737. (defun org-create-formula-image-with-imagemagick (string tofile options buffer)
  17738. "This calls convert, which is included into imagemagick."
  17739. (require 'ox-latex)
  17740. (let* ((tmpdir (if (featurep 'xemacs)
  17741. (temp-directory)
  17742. temporary-file-directory))
  17743. (texfilebase (make-temp-name
  17744. (expand-file-name "orgtex" tmpdir)))
  17745. (texfile (concat texfilebase ".tex"))
  17746. (pdffile (concat texfilebase ".pdf"))
  17747. (pngfile (concat texfilebase ".png"))
  17748. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  17749. (dpi (number-to-string (* scale (if buffer (org--get-display-dpi) 120))))
  17750. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  17751. "black"))
  17752. (bg (or (plist-get options (if buffer :background :html-background))
  17753. "white")))
  17754. (if (eq fg 'default) (setq fg (org-latex-color :foreground))
  17755. (setq fg (org-latex-color-format fg)))
  17756. (if (eq bg 'default) (setq bg (org-latex-color :background))
  17757. (setq bg (org-latex-color-format
  17758. (if (string= bg "Transparent") "white" bg))))
  17759. (let ((latex-header (org-create-formula--latex-header)))
  17760. (with-temp-file texfile
  17761. (insert latex-header)
  17762. (insert "\n\\begin{document}\n"
  17763. "\\definecolor{fg}{rgb}{" fg "}\n"
  17764. "\\definecolor{bg}{rgb}{" bg "}\n"
  17765. "\n\\pagecolor{bg}\n"
  17766. "\n{\\color{fg}\n"
  17767. string
  17768. "\n}\n"
  17769. "\n\\end{document}\n")))
  17770. (org-latex-compile texfile t)
  17771. (if (not (file-exists-p pdffile))
  17772. (progn (message "Failed to create pdf file from %s" texfile) nil)
  17773. (ignore-errors
  17774. (if (featurep 'xemacs)
  17775. (call-process "convert" nil nil nil
  17776. "-density" "96"
  17777. "-trim"
  17778. "-antialias"
  17779. pdffile
  17780. "-quality" "100"
  17781. ;; "-sharpen" "0x1.0"
  17782. pngfile)
  17783. (call-process "convert" nil nil nil
  17784. "-density" dpi
  17785. "-trim"
  17786. "-antialias"
  17787. pdffile
  17788. "-quality" "100"
  17789. ;; "-sharpen" "0x1.0"
  17790. pngfile)))
  17791. (if (not (file-exists-p pngfile))
  17792. (if org-format-latex-signal-error
  17793. (error "Failed to create png file from %s" texfile)
  17794. (message "Failed to create png file from %s" texfile)
  17795. nil)
  17796. ;; Use the requested file name and clean up
  17797. (copy-file pngfile tofile 'replace)
  17798. (dolist (e '(".pdf" ".tex" ".aux" ".log" ".png"))
  17799. (when (file-exists-p (concat texfilebase e))
  17800. (delete-file (concat texfilebase e))))
  17801. pngfile))))
  17802. (defun org-splice-latex-header (tpl def-pkg pkg snippets-p &optional extra)
  17803. "Fill a LaTeX header template TPL.
  17804. In the template, the following place holders will be recognized:
  17805. [DEFAULT-PACKAGES] \\usepackage statements for DEF-PKG
  17806. [NO-DEFAULT-PACKAGES] do not include DEF-PKG
  17807. [PACKAGES] \\usepackage statements for PKG
  17808. [NO-PACKAGES] do not include PKG
  17809. [EXTRA] the string EXTRA
  17810. [NO-EXTRA] do not include EXTRA
  17811. For backward compatibility, if both the positive and the negative place
  17812. holder is missing, the positive one (without the \"NO-\") will be
  17813. assumed to be present at the end of the template.
  17814. DEF-PKG and PKG are assumed to be alists of options/packagename lists.
  17815. EXTRA is a string.
  17816. SNIPPETS-P indicates if this is run to create snippet images for HTML."
  17817. (let (rpl (end ""))
  17818. (if (string-match "^[ \t]*\\[\\(NO-\\)?DEFAULT-PACKAGES\\][ \t]*\n?" tpl)
  17819. (setq rpl (if (or (match-end 1) (not def-pkg))
  17820. "" (org-latex-packages-to-string def-pkg snippets-p t))
  17821. tpl (replace-match rpl t t tpl))
  17822. (when def-pkg (setq end (org-latex-packages-to-string def-pkg snippets-p))))
  17823. (if (string-match "\\[\\(NO-\\)?PACKAGES\\][ \t]*\n?" tpl)
  17824. (setq rpl (if (or (match-end 1) (not pkg))
  17825. "" (org-latex-packages-to-string pkg snippets-p t))
  17826. tpl (replace-match rpl t t tpl))
  17827. (when pkg (setq end
  17828. (concat end "\n"
  17829. (org-latex-packages-to-string pkg snippets-p)))))
  17830. (if (string-match "\\[\\(NO-\\)?EXTRA\\][ \t]*\n?" tpl)
  17831. (setq rpl (if (or (match-end 1) (not extra))
  17832. "" (concat extra "\n"))
  17833. tpl (replace-match rpl t t tpl))
  17834. (when (and extra (string-match "\\S-" extra))
  17835. (setq end (concat end "\n" extra))))
  17836. (if (string-match "\\S-" end)
  17837. (concat tpl "\n" end)
  17838. tpl)))
  17839. (defun org-latex-packages-to-string (pkg &optional snippets-p newline)
  17840. "Turn an alist of packages into a string with the \\usepackage macros."
  17841. (setq pkg (mapconcat (lambda(p)
  17842. (cond
  17843. ((stringp p) p)
  17844. ((and snippets-p (>= (length p) 3) (not (nth 2 p)))
  17845. (format "%% Package %s omitted" (cadr p)))
  17846. ((equal "" (car p))
  17847. (format "\\usepackage{%s}" (cadr p)))
  17848. (t
  17849. (format "\\usepackage[%s]{%s}"
  17850. (car p) (cadr p)))))
  17851. pkg
  17852. "\n"))
  17853. (if newline (concat pkg "\n") pkg))
  17854. (defun org-dvipng-color (attr)
  17855. "Return a RGB color specification for dvipng."
  17856. (apply 'format "rgb %s %s %s"
  17857. (mapcar 'org-normalize-color
  17858. (if (featurep 'xemacs)
  17859. (color-rgb-components
  17860. (face-property 'default
  17861. (cond ((eq attr :foreground) 'foreground)
  17862. ((eq attr :background) 'background))))
  17863. (color-values (face-attribute 'default attr nil))))))
  17864. (defun org-dvipng-color-format (color-name)
  17865. "Convert COLOR-NAME to a RGB color value for dvipng."
  17866. (apply 'format "rgb %s %s %s"
  17867. (mapcar 'org-normalize-color
  17868. (color-values color-name))))
  17869. (defun org-latex-color (attr)
  17870. "Return a RGB color for the LaTeX color package."
  17871. (apply 'format "%s,%s,%s"
  17872. (mapcar 'org-normalize-color
  17873. (if (featurep 'xemacs)
  17874. (color-rgb-components
  17875. (face-property 'default
  17876. (cond ((eq attr :foreground) 'foreground)
  17877. ((eq attr :background) 'background))))
  17878. (color-values (face-attribute 'default attr nil))))))
  17879. (defun org-latex-color-format (color-name)
  17880. "Convert COLOR-NAME to a RGB color value."
  17881. (apply 'format "%s,%s,%s"
  17882. (mapcar 'org-normalize-color
  17883. (color-values color-name))))
  17884. (defun org-normalize-color (value)
  17885. "Return string to be used as color value for an RGB component."
  17886. (format "%g" (/ value 65535.0)))
  17887. ;; Image display
  17888. (defvar-local org-inline-image-overlays nil)
  17889. (defun org-toggle-inline-images (&optional include-linked)
  17890. "Toggle the display of inline images.
  17891. INCLUDE-LINKED is passed to `org-display-inline-images'."
  17892. (interactive "P")
  17893. (if org-inline-image-overlays
  17894. (progn
  17895. (org-remove-inline-images)
  17896. (when (org-called-interactively-p 'interactive)
  17897. (message "Inline image display turned off")))
  17898. (org-display-inline-images include-linked)
  17899. (when (org-called-interactively-p 'interactive)
  17900. (message (if org-inline-image-overlays
  17901. (format "%d images displayed inline"
  17902. (length org-inline-image-overlays))
  17903. "No images to display inline")))))
  17904. (defun org-redisplay-inline-images ()
  17905. "Refresh the display of inline images."
  17906. (interactive)
  17907. (if (not org-inline-image-overlays)
  17908. (org-toggle-inline-images)
  17909. (org-toggle-inline-images)
  17910. (org-toggle-inline-images)))
  17911. (defun org-display-inline-images (&optional include-linked refresh beg end)
  17912. "Display inline images.
  17913. An inline image is a link which follows either of these
  17914. conventions:
  17915. 1. Its path is a file with an extension matching return value
  17916. from `image-file-name-regexp' and it has no contents.
  17917. 2. Its description consists in a single link of the previous
  17918. type.
  17919. When optional argument INCLUDE-LINKED is non-nil, also links with
  17920. a text description part will be inlined. This can be nice for
  17921. a quick look at those images, but it does not reflect what
  17922. exported files will look like.
  17923. When optional argument REFRESH is non-nil, refresh existing
  17924. images between BEG and END. This will create new image displays
  17925. only if necessary. BEG and END default to the buffer
  17926. boundaries."
  17927. (interactive "P")
  17928. (when (display-graphic-p)
  17929. (unless refresh
  17930. (org-remove-inline-images)
  17931. (when (fboundp 'clear-image-cache) (clear-image-cache)))
  17932. (org-with-wide-buffer
  17933. (goto-char (or beg (point-min)))
  17934. (let ((case-fold-search t)
  17935. (file-extension-re (image-file-name-regexp)))
  17936. (while (re-search-forward "[][]\\[\\(?:file\\|[./~]\\)" end t)
  17937. (let ((link (save-match-data (org-element-context))))
  17938. ;; Check if we're at an inline image.
  17939. (when (and (equal (org-element-property :type link) "file")
  17940. (or include-linked
  17941. (not (org-element-property :contents-begin link)))
  17942. (let ((parent (org-element-property :parent link)))
  17943. (or (not (eq (org-element-type parent) 'link))
  17944. (not (cdr (org-element-contents parent)))))
  17945. (org-string-match-p file-extension-re
  17946. (org-element-property :path link)))
  17947. (let ((file (expand-file-name
  17948. (org-link-unescape
  17949. (org-element-property :path link)))))
  17950. (when (file-exists-p file)
  17951. (let ((width
  17952. ;; Apply `org-image-actual-width' specifications.
  17953. (cond
  17954. ((not (image-type-available-p 'imagemagick)) nil)
  17955. ((eq org-image-actual-width t) nil)
  17956. ((listp org-image-actual-width)
  17957. (or
  17958. ;; First try to find a width among
  17959. ;; attributes associated to the paragraph
  17960. ;; containing link.
  17961. (let ((paragraph
  17962. (let ((e link))
  17963. (while (and (setq e (org-element-property
  17964. :parent e))
  17965. (not (eq (org-element-type e)
  17966. 'paragraph))))
  17967. e)))
  17968. (when paragraph
  17969. (save-excursion
  17970. (goto-char (org-element-property :begin paragraph))
  17971. (when
  17972. (re-search-forward
  17973. "^[ \t]*#\\+attr_.*?: +.*?:width +\\(\\S-+\\)"
  17974. (org-element-property
  17975. :post-affiliated paragraph)
  17976. t)
  17977. (string-to-number (match-string 1))))))
  17978. ;; Otherwise, fall-back to provided number.
  17979. (car org-image-actual-width)))
  17980. ((numberp org-image-actual-width)
  17981. org-image-actual-width)))
  17982. (old (get-char-property-and-overlay
  17983. (org-element-property :begin link)
  17984. 'org-image-overlay)))
  17985. (if (and (car-safe old) refresh)
  17986. (image-refresh (overlay-get (cdr old) 'display))
  17987. (let ((image (create-image file
  17988. (and width 'imagemagick)
  17989. nil
  17990. :width width)))
  17991. (when image
  17992. (let* ((link
  17993. ;; If inline image is the description
  17994. ;; of another link, be sure to
  17995. ;; consider the latter as the one to
  17996. ;; apply the overlay on.
  17997. (let ((parent
  17998. (org-element-property :parent link)))
  17999. (if (eq (org-element-type parent) 'link)
  18000. parent
  18001. link)))
  18002. (ov (make-overlay
  18003. (org-element-property :begin link)
  18004. (progn
  18005. (goto-char
  18006. (org-element-property :end link))
  18007. (skip-chars-backward " \t")
  18008. (point)))))
  18009. (overlay-put ov 'display image)
  18010. (overlay-put ov 'face 'default)
  18011. (overlay-put ov 'org-image-overlay t)
  18012. (overlay-put
  18013. ov 'modification-hooks
  18014. (list 'org-display-inline-remove-overlay))
  18015. (push ov org-inline-image-overlays)))))))))))))))
  18016. (define-obsolete-function-alias
  18017. 'org-display-inline-modification-hook 'org-display-inline-remove-overlay "24.3")
  18018. (defun org-display-inline-remove-overlay (ov after _beg _end &optional _len)
  18019. "Remove inline-display overlay if a corresponding region is modified."
  18020. (let ((inhibit-modification-hooks t))
  18021. (when (and ov after)
  18022. (delete ov org-inline-image-overlays)
  18023. (delete-overlay ov))))
  18024. (defun org-remove-inline-images ()
  18025. "Remove inline display of images."
  18026. (interactive)
  18027. (mapc #'delete-overlay org-inline-image-overlays)
  18028. (setq org-inline-image-overlays nil))
  18029. ;;;; Key bindings
  18030. ;; Outline functions from `outline-mode-prefix-map'
  18031. ;; that can be remapped in Org:
  18032. (define-key org-mode-map [remap outline-mark-subtree] 'org-mark-subtree)
  18033. (define-key org-mode-map [remap outline-show-subtree] 'org-show-subtree)
  18034. (define-key org-mode-map [remap outline-forward-same-level]
  18035. 'org-forward-heading-same-level)
  18036. (define-key org-mode-map [remap outline-backward-same-level]
  18037. 'org-backward-heading-same-level)
  18038. (define-key org-mode-map [remap outline-show-branches]
  18039. 'org-kill-note-or-show-branches)
  18040. (define-key org-mode-map [remap outline-promote] 'org-promote-subtree)
  18041. (define-key org-mode-map [remap outline-demote] 'org-demote-subtree)
  18042. (define-key org-mode-map [remap outline-insert-heading] 'org-ctrl-c-ret)
  18043. (define-key org-mode-map [remap outline-next-visible-heading]
  18044. 'org-next-visible-heading)
  18045. (define-key org-mode-map [remap outline-previous-visible-heading]
  18046. 'org-previous-visible-heading)
  18047. (define-key org-mode-map [remap show-children] 'org-show-children)
  18048. ;; Outline functions from `outline-mode-prefix-map' that can not
  18049. ;; be remapped in Org:
  18050. ;; - the column "key binding" shows whether the Outline function is still
  18051. ;; available in Org mode on the same key that it has been bound to in
  18052. ;; Outline mode:
  18053. ;; - "overridden": key used for a different functionality in Org mode
  18054. ;; - else: key still bound to the same Outline function in Org mode
  18055. ;; | Outline function | key binding | Org replacement |
  18056. ;; |------------------------------------+-------------+--------------------------|
  18057. ;; | `outline-up-heading' | `C-c C-u' | still same function |
  18058. ;; | `outline-move-subtree-up' | overridden | better: org-shiftup |
  18059. ;; | `outline-move-subtree-down' | overridden | better: org-shiftdown |
  18060. ;; | `show-entry' | overridden | no replacement |
  18061. ;; | `show-branches' | `C-c C-k' | still same function |
  18062. ;; | `show-subtree' | overridden | visibility cycling |
  18063. ;; | `show-all' | overridden | no replacement |
  18064. ;; | `hide-subtree' | overridden | visibility cycling |
  18065. ;; | `hide-body' | overridden | no replacement |
  18066. ;; | `hide-entry' | overridden | visibility cycling |
  18067. ;; | `hide-leaves' | overridden | no replacement |
  18068. ;; | `hide-sublevels' | overridden | no replacement |
  18069. ;; | `hide-other' | overridden | no replacement |
  18070. ;; Make `C-c C-x' a prefix key
  18071. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  18072. ;; TAB key with modifiers
  18073. (org-defkey org-mode-map "\C-i" 'org-cycle)
  18074. (org-defkey org-mode-map [(tab)] 'org-cycle)
  18075. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  18076. (org-defkey org-mode-map "\M-\t" #'pcomplete)
  18077. ;; The following line is necessary under Suse GNU/Linux
  18078. (unless (featurep 'xemacs)
  18079. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  18080. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  18081. (define-key org-mode-map [backtab] 'org-shifttab)
  18082. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  18083. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  18084. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  18085. ;; Cursor keys with modifiers
  18086. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  18087. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  18088. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  18089. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  18090. (org-defkey org-mode-map [(control meta shift right)] 'org-increase-number-at-point)
  18091. (org-defkey org-mode-map [(control meta shift left)] 'org-decrease-number-at-point)
  18092. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  18093. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  18094. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  18095. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  18096. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  18097. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  18098. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  18099. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  18100. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  18101. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  18102. (org-defkey org-mode-map [(control shift up)] 'org-shiftcontrolup)
  18103. (org-defkey org-mode-map [(control shift down)] 'org-shiftcontroldown)
  18104. ;; Babel keys
  18105. (define-key org-mode-map org-babel-key-prefix org-babel-map)
  18106. (dolist (pair org-babel-key-bindings)
  18107. (define-key org-babel-map (car pair) (cdr pair)))
  18108. ;;; Extra keys for tty access.
  18109. ;; We only set them when really needed because otherwise the
  18110. ;; menus don't show the simple keys
  18111. (when (or org-use-extra-keys
  18112. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  18113. (not window-system))
  18114. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  18115. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  18116. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  18117. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  18118. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  18119. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  18120. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  18121. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  18122. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  18123. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  18124. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  18125. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  18126. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  18127. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  18128. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  18129. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  18130. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  18131. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  18132. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  18133. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  18134. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  18135. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  18136. (org-defkey org-mode-map [?\e (tab)] #'pcomplete)
  18137. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  18138. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  18139. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  18140. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  18141. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  18142. ;; All the other keys
  18143. (org-defkey org-mode-map "\C-c\C-a" 'outline-show-all) ; in case allout messed up.
  18144. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  18145. (if (boundp 'narrow-map)
  18146. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  18147. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  18148. (if (boundp 'narrow-map)
  18149. (org-defkey narrow-map "b" 'org-narrow-to-block)
  18150. (org-defkey org-mode-map "\C-xnb" 'org-narrow-to-block))
  18151. (if (boundp 'narrow-map)
  18152. (org-defkey narrow-map "e" 'org-narrow-to-element)
  18153. (org-defkey org-mode-map "\C-xne" 'org-narrow-to-element))
  18154. (org-defkey org-mode-map "\C-\M-t" 'org-transpose-element)
  18155. (org-defkey org-mode-map "\M-}" 'org-forward-element)
  18156. (org-defkey org-mode-map "\M-{" 'org-backward-element)
  18157. (org-defkey org-mode-map "\C-c\C-^" 'org-up-element)
  18158. (org-defkey org-mode-map "\C-c\C-_" 'org-down-element)
  18159. (org-defkey org-mode-map "\C-c\C-f" 'org-forward-heading-same-level)
  18160. (org-defkey org-mode-map "\C-c\C-b" 'org-backward-heading-same-level)
  18161. (org-defkey org-mode-map "\C-c\M-f" 'org-next-block)
  18162. (org-defkey org-mode-map "\C-c\M-b" 'org-previous-block)
  18163. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  18164. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-archive-subtree)
  18165. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
  18166. (org-defkey org-mode-map "\C-c\C-xd" 'org-insert-drawer)
  18167. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  18168. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  18169. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  18170. (org-defkey org-mode-map "\C-c\C-xq" 'org-toggle-tags-groups)
  18171. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  18172. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  18173. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  18174. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  18175. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  18176. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  18177. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  18178. (org-defkey org-mode-map "\C-c\M-w" 'org-copy)
  18179. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  18180. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  18181. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  18182. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  18183. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  18184. (org-defkey org-mode-map "\C-c\C-xv" 'org-copy-visible)
  18185. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  18186. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  18187. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  18188. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  18189. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  18190. (org-defkey org-mode-map "\C-c\M-l" 'org-insert-last-stored-link)
  18191. (org-defkey org-mode-map "\C-c\C-\M-l" 'org-insert-all-links)
  18192. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  18193. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  18194. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  18195. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  18196. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  18197. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  18198. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  18199. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  18200. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  18201. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  18202. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  18203. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  18204. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  18205. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  18206. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  18207. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  18208. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  18209. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  18210. (org-defkey org-mode-map "\C-c^" 'org-sort)
  18211. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  18212. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  18213. (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
  18214. (org-defkey org-mode-map [remap open-line] 'org-open-line)
  18215. (org-defkey org-mode-map [remap comment-dwim] 'org-comment-dwim)
  18216. (org-defkey org-mode-map [remap forward-paragraph] 'org-forward-paragraph)
  18217. (org-defkey org-mode-map [remap backward-paragraph] 'org-backward-paragraph)
  18218. (org-defkey org-mode-map "\M-^" 'org-delete-indentation)
  18219. (org-defkey org-mode-map "\C-m" 'org-return)
  18220. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  18221. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  18222. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  18223. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  18224. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  18225. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  18226. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  18227. (org-defkey org-mode-map "\C-c\"a" 'orgtbl-ascii-plot)
  18228. (org-defkey org-mode-map "\C-c\"g" 'org-plot/gnuplot)
  18229. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  18230. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  18231. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  18232. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  18233. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  18234. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  18235. (org-defkey org-mode-map "\C-c\C-e" 'org-export-dispatch)
  18236. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width)
  18237. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  18238. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  18239. (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
  18240. (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
  18241. (org-defkey org-mode-map "\C-c@" 'org-mark-subtree)
  18242. (org-defkey org-mode-map "\M-h" 'org-mark-element)
  18243. (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
  18244. ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
  18245. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  18246. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  18247. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  18248. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  18249. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  18250. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-in-last)
  18251. (org-defkey org-mode-map "\C-c\C-x\C-z" 'org-resolve-clocks)
  18252. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  18253. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  18254. (org-defkey org-mode-map "\C-c\C-x\C-q" 'org-clock-cancel)
  18255. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  18256. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  18257. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  18258. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-toggle-latex-fragment)
  18259. (org-defkey org-mode-map "\C-c\C-x\C-v" 'org-toggle-inline-images)
  18260. (org-defkey org-mode-map "\C-c\C-x\C-\M-v" 'org-redisplay-inline-images)
  18261. (org-defkey org-mode-map "\C-c\C-x\\" 'org-toggle-pretty-entities)
  18262. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  18263. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  18264. (org-defkey org-mode-map "\C-c\C-xP" 'org-set-property-and-value)
  18265. (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
  18266. (org-defkey org-mode-map "\C-c\C-xE" 'org-inc-effort)
  18267. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  18268. (org-defkey org-mode-map "\C-c\C-xi" 'org-columns-insert-dblock)
  18269. (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
  18270. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  18271. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  18272. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  18273. (org-defkey org-mode-map "\C-c\C-x_" 'org-timer-stop)
  18274. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  18275. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  18276. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  18277. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  18278. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  18279. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  18280. (when (featurep 'xemacs)
  18281. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  18282. (defconst org-speed-commands-default
  18283. '(
  18284. ("Outline Navigation")
  18285. ("n" . (org-speed-move-safe 'org-next-visible-heading))
  18286. ("p" . (org-speed-move-safe 'org-previous-visible-heading))
  18287. ("f" . (org-speed-move-safe 'org-forward-heading-same-level))
  18288. ("b" . (org-speed-move-safe 'org-backward-heading-same-level))
  18289. ("F" . org-next-block)
  18290. ("B" . org-previous-block)
  18291. ("u" . (org-speed-move-safe 'outline-up-heading))
  18292. ("j" . org-goto)
  18293. ("g" . (org-refile t))
  18294. ("Outline Visibility")
  18295. ("c" . org-cycle)
  18296. ("C" . org-shifttab)
  18297. (" " . org-display-outline-path)
  18298. ("s" . org-narrow-to-subtree)
  18299. ("=" . org-columns)
  18300. ("Outline Structure Editing")
  18301. ("U" . org-metaup)
  18302. ("D" . org-metadown)
  18303. ("r" . org-metaright)
  18304. ("l" . org-metaleft)
  18305. ("R" . org-shiftmetaright)
  18306. ("L" . org-shiftmetaleft)
  18307. ("i" . (progn (forward-char 1) (call-interactively
  18308. 'org-insert-heading-respect-content)))
  18309. ("^" . org-sort)
  18310. ("w" . org-refile)
  18311. ("a" . org-archive-subtree-default-with-confirmation)
  18312. ("@" . org-mark-subtree)
  18313. ("#" . org-toggle-comment)
  18314. ("Clock Commands")
  18315. ("I" . org-clock-in)
  18316. ("O" . org-clock-out)
  18317. ("Meta Data Editing")
  18318. ("t" . org-todo)
  18319. ("," . (org-priority))
  18320. ("0" . (org-priority ?\ ))
  18321. ("1" . (org-priority ?A))
  18322. ("2" . (org-priority ?B))
  18323. ("3" . (org-priority ?C))
  18324. (":" . org-set-tags-command)
  18325. ("e" . org-set-effort)
  18326. ("E" . org-inc-effort)
  18327. ("W" . (lambda(m) (interactive "sMinutes before warning: ")
  18328. (org-entry-put (point) "APPT_WARNTIME" m)))
  18329. ("Agenda Views etc")
  18330. ("v" . org-agenda)
  18331. ("/" . org-sparse-tree)
  18332. ("Misc")
  18333. ("o" . org-open-at-point)
  18334. ("?" . org-speed-command-help)
  18335. ("<" . (org-agenda-set-restriction-lock 'subtree))
  18336. (">" . (org-agenda-remove-restriction-lock))
  18337. )
  18338. "The default speed commands.")
  18339. (defun org-print-speed-command (e)
  18340. (if (> (length (car e)) 1)
  18341. (progn
  18342. (princ "\n")
  18343. (princ (car e))
  18344. (princ "\n")
  18345. (princ (make-string (length (car e)) ?-))
  18346. (princ "\n"))
  18347. (princ (car e))
  18348. (princ " ")
  18349. (if (symbolp (cdr e))
  18350. (princ (symbol-name (cdr e)))
  18351. (prin1 (cdr e)))
  18352. (princ "\n")))
  18353. (defun org-speed-command-help ()
  18354. "Show the available speed commands."
  18355. (interactive)
  18356. (if (not org-use-speed-commands)
  18357. (user-error "Speed commands are not activated, customize `org-use-speed-commands'")
  18358. (with-output-to-temp-buffer "*Help*"
  18359. (princ "User-defined Speed commands\n===========================\n")
  18360. (mapc #'org-print-speed-command org-speed-commands-user)
  18361. (princ "\n")
  18362. (princ "Built-in Speed commands\n=======================\n")
  18363. (mapc #'org-print-speed-command org-speed-commands-default))
  18364. (with-current-buffer "*Help*"
  18365. (setq truncate-lines t))))
  18366. (defun org-speed-move-safe (cmd)
  18367. "Execute CMD, but make sure that the cursor always ends up in a headline.
  18368. If not, return to the original position and throw an error."
  18369. (interactive)
  18370. (let ((pos (point)))
  18371. (call-interactively cmd)
  18372. (unless (and (bolp) (org-at-heading-p))
  18373. (goto-char pos)
  18374. (error "Boundary reached while executing %s" cmd))))
  18375. (defvar org-self-insert-command-undo-counter 0)
  18376. (defvar org-table-auto-blank-field) ; defined in org-table.el
  18377. (defvar org-speed-command nil)
  18378. (define-obsolete-function-alias
  18379. 'org-speed-command-default-hook 'org-speed-command-activate "24.3")
  18380. (defun org-speed-command-activate (keys)
  18381. "Hook for activating single-letter speed commands.
  18382. `org-speed-commands-default' specifies a minimal command set.
  18383. Use `org-speed-commands-user' for further customization."
  18384. (when (or (and (bolp) (looking-at org-outline-regexp))
  18385. (and (functionp org-use-speed-commands)
  18386. (funcall org-use-speed-commands)))
  18387. (cdr (assoc keys (append org-speed-commands-user
  18388. org-speed-commands-default)))))
  18389. (define-obsolete-function-alias
  18390. 'org-babel-speed-command-hook 'org-babel-speed-command-activate "24.3")
  18391. (defun org-babel-speed-command-activate (keys)
  18392. "Hook for activating single-letter code block commands."
  18393. (when (and (bolp) (looking-at org-babel-src-block-regexp))
  18394. (cdr (assoc keys org-babel-key-bindings))))
  18395. (defcustom org-speed-command-hook
  18396. '(org-speed-command-default-hook org-babel-speed-command-hook)
  18397. "Hook for activating speed commands at strategic locations.
  18398. Hook functions are called in sequence until a valid handler is
  18399. found.
  18400. Each hook takes a single argument, a user-pressed command key
  18401. which is also a `self-insert-command' from the global map.
  18402. Within the hook, examine the cursor position and the command key
  18403. and return nil or a valid handler as appropriate. Handler could
  18404. be one of an interactive command, a function, or a form.
  18405. Set `org-use-speed-commands' to non-nil value to enable this
  18406. hook. The default setting is `org-speed-command-activate'."
  18407. :group 'org-structure
  18408. :version "24.1"
  18409. :type 'hook)
  18410. (defun org-self-insert-command (N)
  18411. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  18412. If the cursor is in a table looking at whitespace, the whitespace is
  18413. overwritten, and the table is not marked as requiring realignment."
  18414. (interactive "p")
  18415. (org-check-before-invisible-edit 'insert)
  18416. (cond
  18417. ((and org-use-speed-commands
  18418. (let ((kv (this-command-keys-vector)))
  18419. (setq org-speed-command
  18420. (run-hook-with-args-until-success
  18421. 'org-speed-command-hook
  18422. (make-string 1 (aref kv (1- (length kv))))))))
  18423. (cond
  18424. ((commandp org-speed-command)
  18425. (setq this-command org-speed-command)
  18426. (call-interactively org-speed-command))
  18427. ((functionp org-speed-command)
  18428. (funcall org-speed-command))
  18429. ((and org-speed-command (listp org-speed-command))
  18430. (eval org-speed-command))
  18431. (t (let (org-use-speed-commands)
  18432. (call-interactively 'org-self-insert-command)))))
  18433. ((and
  18434. (org-at-table-p)
  18435. (progn
  18436. ;; Check if we blank the field, and if that triggers align.
  18437. (and (featurep 'org-table) org-table-auto-blank-field
  18438. (memq last-command
  18439. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
  18440. (if (or (eq (char-after) ?\s) (looking-at "[^|\n]* |"))
  18441. ;; Got extra space, this field does not determine
  18442. ;; column width.
  18443. (let (org-table-may-need-update) (org-table-blank-field))
  18444. ;; No extra space, this field may determine column
  18445. ;; width.
  18446. (org-table-blank-field)))
  18447. t)
  18448. (eq N 1)
  18449. (looking-at "[^|\n]* \\( \\)|"))
  18450. ;; There is room for insertion without re-aligning the table.
  18451. (delete-region (match-beginning 1) (match-end 1))
  18452. (self-insert-command N))
  18453. (t
  18454. (setq org-table-may-need-update t)
  18455. (self-insert-command N)
  18456. (org-fix-tags-on-the-fly)
  18457. (when org-self-insert-cluster-for-undo
  18458. (if (not (eq last-command 'org-self-insert-command))
  18459. (setq org-self-insert-command-undo-counter 1)
  18460. (if (>= org-self-insert-command-undo-counter 20)
  18461. (setq org-self-insert-command-undo-counter 1)
  18462. (and (> org-self-insert-command-undo-counter 0)
  18463. buffer-undo-list (listp buffer-undo-list)
  18464. (not (cadr buffer-undo-list)) ; remove nil entry
  18465. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  18466. (setq org-self-insert-command-undo-counter
  18467. (1+ org-self-insert-command-undo-counter))))))))
  18468. (defun org-check-before-invisible-edit (kind)
  18469. "Check is editing if kind KIND would be dangerous with invisible text around.
  18470. The detailed reaction depends on the user option `org-catch-invisible-edits'."
  18471. ;; First, try to get out of here as quickly as possible, to reduce overhead
  18472. (when (and org-catch-invisible-edits
  18473. (or (not (boundp 'visible-mode)) (not visible-mode))
  18474. (or (get-char-property (point) 'invisible)
  18475. (get-char-property (max (point-min) (1- (point))) 'invisible)))
  18476. ;; OK, we need to take a closer look
  18477. (let* ((invisible-at-point (get-char-property (point) 'invisible))
  18478. (invisible-before-point (unless (bobp) (get-char-property
  18479. (1- (point)) 'invisible)))
  18480. (border-and-ok-direction
  18481. (or
  18482. ;; Check if we are acting predictably before invisible text
  18483. (and invisible-at-point (not invisible-before-point)
  18484. (memq kind '(insert delete-backward)))
  18485. ;; Check if we are acting predictably after invisible text
  18486. ;; This works not well, and I have turned it off. It seems
  18487. ;; better to always show and stop after invisible text.
  18488. ;; (and (not invisible-at-point) invisible-before-point
  18489. ;; (memq kind '(insert delete)))
  18490. )))
  18491. (when (or (memq invisible-at-point '(outline org-hide-block t))
  18492. (memq invisible-before-point '(outline org-hide-block t)))
  18493. (when (eq org-catch-invisible-edits 'error)
  18494. (user-error "Editing in invisible areas is prohibited, make them visible first"))
  18495. (if (and org-custom-properties-overlays
  18496. (y-or-n-p "Display invisible properties in this buffer? "))
  18497. (org-toggle-custom-properties-visibility)
  18498. ;; Make the area visible
  18499. (save-excursion
  18500. (when invisible-before-point
  18501. (goto-char (previous-single-char-property-change
  18502. (point) 'invisible)))
  18503. (outline-show-subtree))
  18504. (cond
  18505. ((eq org-catch-invisible-edits 'show)
  18506. ;; That's it, we do the edit after showing
  18507. (message
  18508. "Unfolding invisible region around point before editing")
  18509. (sit-for 1))
  18510. ((and (eq org-catch-invisible-edits 'smart)
  18511. border-and-ok-direction)
  18512. (message "Unfolding invisible region around point before editing"))
  18513. (t
  18514. ;; Don't do the edit, make the user repeat it in full visibility
  18515. (user-error "Edit in invisible region aborted, repeat to confirm with text visible"))))))))
  18516. (defun org-fix-tags-on-the-fly ()
  18517. (when (and (equal (char-after (point-at-bol)) ?*)
  18518. (org-at-heading-p))
  18519. (org-align-tags-here org-tags-column)))
  18520. (defun org-delete-backward-char (N)
  18521. "Like `delete-backward-char', insert whitespace at field end in tables.
  18522. When deleting backwards, in tables this function will insert whitespace in
  18523. front of the next \"|\" separator, to keep the table aligned. The table will
  18524. still be marked for re-alignment if the field did fill the entire column,
  18525. because, in this case the deletion might narrow the column."
  18526. (interactive "p")
  18527. (save-match-data
  18528. (org-check-before-invisible-edit 'delete-backward)
  18529. (if (and (org-at-table-p)
  18530. (eq N 1)
  18531. (string-match "|" (buffer-substring (point-at-bol) (point)))
  18532. (looking-at ".*?|"))
  18533. (let ((pos (point))
  18534. (noalign (looking-at "[^|\n\r]* |"))
  18535. (c org-table-may-need-update))
  18536. (backward-delete-char N)
  18537. (unless overwrite-mode
  18538. (skip-chars-forward "^|")
  18539. (insert " ")
  18540. (goto-char (1- pos)))
  18541. ;; noalign: if there were two spaces at the end, this field
  18542. ;; does not determine the width of the column.
  18543. (when noalign (setq org-table-may-need-update c)))
  18544. (backward-delete-char N)
  18545. (org-fix-tags-on-the-fly))))
  18546. (defun org-delete-char (N)
  18547. "Like `delete-char', but insert whitespace at field end in tables.
  18548. When deleting characters, in tables this function will insert whitespace in
  18549. front of the next \"|\" separator, to keep the table aligned. The table will
  18550. still be marked for re-alignment if the field did fill the entire column,
  18551. because, in this case the deletion might narrow the column."
  18552. (interactive "p")
  18553. (save-match-data
  18554. (org-check-before-invisible-edit 'delete)
  18555. (if (and (org-at-table-p)
  18556. (not (bolp))
  18557. (not (= (char-after) ?|))
  18558. (eq N 1))
  18559. (if (looking-at ".*?|")
  18560. (let ((pos (point))
  18561. (noalign (looking-at "[^|\n\r]* |"))
  18562. (c org-table-may-need-update))
  18563. (replace-match
  18564. (concat (substring (match-string 0) 1 -1) " |") nil t)
  18565. (goto-char pos)
  18566. ;; noalign: if there were two spaces at the end, this field
  18567. ;; does not determine the width of the column.
  18568. (when noalign (setq org-table-may-need-update c)))
  18569. (delete-char N))
  18570. (delete-char N)
  18571. (org-fix-tags-on-the-fly))))
  18572. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  18573. (put 'org-self-insert-command 'delete-selection
  18574. (lambda ()
  18575. (not (run-hook-with-args-until-success
  18576. 'self-insert-uses-region-functions))))
  18577. (put 'orgtbl-self-insert-command 'delete-selection
  18578. (lambda ()
  18579. (not (run-hook-with-args-until-success
  18580. 'self-insert-uses-region-functions))))
  18581. (put 'org-delete-char 'delete-selection 'supersede)
  18582. (put 'org-delete-backward-char 'delete-selection 'supersede)
  18583. (put 'org-yank 'delete-selection 'yank)
  18584. ;; Make `flyspell-mode' delay after some commands
  18585. (put 'org-self-insert-command 'flyspell-delayed t)
  18586. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  18587. (put 'org-delete-char 'flyspell-delayed t)
  18588. (put 'org-delete-backward-char 'flyspell-delayed t)
  18589. ;; Make pabbrev-mode expand after org-mode commands
  18590. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  18591. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  18592. (defun org-remap (map &rest commands)
  18593. "In MAP, remap the functions given in COMMANDS.
  18594. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  18595. (let (new old)
  18596. (while commands
  18597. (setq old (pop commands) new (pop commands))
  18598. (if (fboundp 'command-remapping)
  18599. (org-defkey map (vector 'remap old) new)
  18600. (substitute-key-definition old new map global-map)))))
  18601. (defun org-transpose-words ()
  18602. "Transpose words for Org.
  18603. This uses the `org-mode-transpose-word-syntax-table' syntax
  18604. table, which interprets characters in `org-emphasis-alist' as
  18605. word constituents."
  18606. (interactive)
  18607. (with-syntax-table org-mode-transpose-word-syntax-table
  18608. (call-interactively 'transpose-words)))
  18609. (org-remap org-mode-map 'transpose-words 'org-transpose-words)
  18610. (when (eq org-enable-table-editor 'optimized)
  18611. ;; If the user wants maximum table support, we need to hijack
  18612. ;; some standard editing functions
  18613. (org-remap org-mode-map
  18614. 'self-insert-command 'org-self-insert-command
  18615. 'delete-char 'org-delete-char
  18616. 'delete-backward-char 'org-delete-backward-char)
  18617. (org-defkey org-mode-map "|" 'org-force-self-insert))
  18618. (defvar org-ctrl-c-ctrl-c-hook nil
  18619. "Hook for functions attaching themselves to `C-c C-c'.
  18620. This can be used to add additional functionality to the C-c C-c
  18621. key which executes context-dependent commands. This hook is run
  18622. before any other test, while `org-ctrl-c-ctrl-c-final-hook' is
  18623. run after the last test.
  18624. Each function will be called with no arguments. The function
  18625. must check if the context is appropriate for it to act. If yes,
  18626. it should do its thing and then return a non-nil value. If the
  18627. context is wrong, just do nothing and return nil.")
  18628. (defvar org-ctrl-c-ctrl-c-final-hook nil
  18629. "Hook for functions attaching themselves to `C-c C-c'.
  18630. This can be used to add additional functionality to the C-c C-c
  18631. key which executes context-dependent commands. This hook is run
  18632. after any other test, while `org-ctrl-c-ctrl-c-hook' is run
  18633. before the first test.
  18634. Each function will be called with no arguments. The function
  18635. must check if the context is appropriate for it to act. If yes,
  18636. it should do its thing and then return a non-nil value. If the
  18637. context is wrong, just do nothing and return nil.")
  18638. (defvar org-tab-first-hook nil
  18639. "Hook for functions to attach themselves to TAB.
  18640. See `org-ctrl-c-ctrl-c-hook' for more information.
  18641. This hook runs as the first action when TAB is pressed, even before
  18642. `org-cycle' messes around with the `outline-regexp' to cater for
  18643. inline tasks and plain list item folding.
  18644. If any function in this hook returns t, any other actions that
  18645. would have been caused by TAB (such as table field motion or visibility
  18646. cycling) will not occur.")
  18647. (defvar org-tab-after-check-for-table-hook nil
  18648. "Hook for functions to attach themselves to TAB.
  18649. See `org-ctrl-c-ctrl-c-hook' for more information.
  18650. This hook runs after it has been established that the cursor is not in a
  18651. table, but before checking if the cursor is in a headline or if global cycling
  18652. should be done.
  18653. If any function in this hook returns t, not other actions like visibility
  18654. cycling will be done.")
  18655. (defvar org-tab-after-check-for-cycling-hook nil
  18656. "Hook for functions to attach themselves to TAB.
  18657. See `org-ctrl-c-ctrl-c-hook' for more information.
  18658. This hook runs after it has been established that not table field motion and
  18659. not visibility should be done because of current context. This is probably
  18660. the place where a package like yasnippets can hook in.")
  18661. (defvar org-tab-before-tab-emulation-hook nil
  18662. "Hook for functions to attach themselves to TAB.
  18663. See `org-ctrl-c-ctrl-c-hook' for more information.
  18664. This hook runs after every other options for TAB have been exhausted, but
  18665. before indentation and \t insertion takes place.")
  18666. (defvar org-metaleft-hook nil
  18667. "Hook for functions attaching themselves to `M-left'.
  18668. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18669. (defvar org-metaright-hook nil
  18670. "Hook for functions attaching themselves to `M-right'.
  18671. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18672. (defvar org-metaup-hook nil
  18673. "Hook for functions attaching themselves to `M-up'.
  18674. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18675. (defvar org-metadown-hook nil
  18676. "Hook for functions attaching themselves to `M-down'.
  18677. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18678. (defvar org-shiftmetaleft-hook nil
  18679. "Hook for functions attaching themselves to `M-S-left'.
  18680. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18681. (defvar org-shiftmetaright-hook nil
  18682. "Hook for functions attaching themselves to `M-S-right'.
  18683. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18684. (defvar org-shiftmetaup-hook nil
  18685. "Hook for functions attaching themselves to `M-S-up'.
  18686. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18687. (defvar org-shiftmetadown-hook nil
  18688. "Hook for functions attaching themselves to `M-S-down'.
  18689. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18690. (defvar org-metareturn-hook nil
  18691. "Hook for functions attaching themselves to `M-RET'.
  18692. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18693. (defvar org-shiftup-hook nil
  18694. "Hook for functions attaching themselves to `S-up'.
  18695. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18696. (defvar org-shiftup-final-hook nil
  18697. "Hook for functions attaching themselves to `S-up'.
  18698. This one runs after all other options except shift-select have been excluded.
  18699. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18700. (defvar org-shiftdown-hook nil
  18701. "Hook for functions attaching themselves to `S-down'.
  18702. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18703. (defvar org-shiftdown-final-hook nil
  18704. "Hook for functions attaching themselves to `S-down'.
  18705. This one runs after all other options except shift-select have been excluded.
  18706. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18707. (defvar org-shiftleft-hook nil
  18708. "Hook for functions attaching themselves to `S-left'.
  18709. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18710. (defvar org-shiftleft-final-hook nil
  18711. "Hook for functions attaching themselves to `S-left'.
  18712. This one runs after all other options except shift-select have been excluded.
  18713. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18714. (defvar org-shiftright-hook nil
  18715. "Hook for functions attaching themselves to `S-right'.
  18716. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18717. (defvar org-shiftright-final-hook nil
  18718. "Hook for functions attaching themselves to `S-right'.
  18719. This one runs after all other options except shift-select have been excluded.
  18720. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18721. (defun org-modifier-cursor-error ()
  18722. "Throw an error, a modified cursor command was applied in wrong context."
  18723. (user-error "This command is active in special context like tables, headlines or items"))
  18724. (defun org-shiftselect-error ()
  18725. "Throw an error because Shift-Cursor command was applied in wrong context."
  18726. (if (and (boundp 'shift-select-mode) shift-select-mode)
  18727. (user-error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
  18728. (user-error "This command works only in special context like headlines or timestamps")))
  18729. (defun org-call-for-shift-select (cmd)
  18730. (let ((this-command-keys-shift-translated t))
  18731. (call-interactively cmd)))
  18732. (defun org-shifttab (&optional arg)
  18733. "Global visibility cycling or move to previous table field.
  18734. Call `org-table-previous-field' within a table.
  18735. When ARG is nil, cycle globally through visibility states.
  18736. When ARG is a numeric prefix, show contents of this level."
  18737. (interactive "P")
  18738. (cond
  18739. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  18740. ((integerp arg)
  18741. (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
  18742. (message "Content view to level: %d" arg)
  18743. (org-content (prefix-numeric-value arg2))
  18744. (org-cycle-show-empty-lines t)
  18745. (setq org-cycle-global-status 'overview)))
  18746. (t (call-interactively 'org-global-cycle))))
  18747. (defun org-shiftmetaleft ()
  18748. "Promote subtree or delete table column.
  18749. Calls `org-promote-subtree', `org-outdent-item-tree', or
  18750. `org-table-delete-column', depending on context. See the
  18751. individual commands for more information."
  18752. (interactive)
  18753. (cond
  18754. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  18755. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  18756. ((org-at-heading-p) (call-interactively 'org-promote-subtree))
  18757. ((if (not (org-region-active-p)) (org-at-item-p)
  18758. (save-excursion (goto-char (region-beginning))
  18759. (org-at-item-p)))
  18760. (call-interactively 'org-outdent-item-tree))
  18761. (t (org-modifier-cursor-error))))
  18762. (defun org-shiftmetaright ()
  18763. "Demote subtree or insert table column.
  18764. Calls `org-demote-subtree', `org-indent-item-tree', or
  18765. `org-table-insert-column', depending on context. See the
  18766. individual commands for more information."
  18767. (interactive)
  18768. (cond
  18769. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  18770. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  18771. ((org-at-heading-p) (call-interactively 'org-demote-subtree))
  18772. ((if (not (org-region-active-p)) (org-at-item-p)
  18773. (save-excursion (goto-char (region-beginning))
  18774. (org-at-item-p)))
  18775. (call-interactively 'org-indent-item-tree))
  18776. (t (org-modifier-cursor-error))))
  18777. (defun org-shiftmetaup (&optional _arg)
  18778. "Drag the line at point up.
  18779. In a table, kill the current row.
  18780. On a clock timestamp, update the value of the timestamp like `S-<up>'
  18781. but also adjust the previous clocked item in the clock history.
  18782. Everywhere else, drag the line at point up."
  18783. (interactive "P")
  18784. (cond
  18785. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  18786. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  18787. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  18788. (call-interactively 'org-timestamp-up)))
  18789. (t (call-interactively 'org-drag-line-backward))))
  18790. (defun org-shiftmetadown (&optional _arg)
  18791. "Drag the line at point down.
  18792. In a table, insert an empty row at the current line.
  18793. On a clock timestamp, update the value of the timestamp like `S-<down>'
  18794. but also adjust the previous clocked item in the clock history.
  18795. Everywhere else, drag the line at point down."
  18796. (interactive "P")
  18797. (cond
  18798. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  18799. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  18800. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  18801. (call-interactively 'org-timestamp-down)))
  18802. (t (call-interactively 'org-drag-line-forward))))
  18803. (defsubst org-hidden-tree-error ()
  18804. (user-error
  18805. "Hidden subtree, open with TAB or use subtree command M-S-<left>/<right>"))
  18806. (defun org-metaleft (&optional _arg)
  18807. "Promote heading, list item at point or move table column left.
  18808. Calls `org-do-promote', `org-outdent-item' or `org-table-move-column',
  18809. depending on context. With no specific context, calls the Emacs
  18810. default `backward-word'. See the individual commands for more
  18811. information.
  18812. This function runs the hook `org-metaleft-hook' as a first step,
  18813. and returns at first non-nil value."
  18814. (interactive "P")
  18815. (cond
  18816. ((run-hook-with-args-until-success 'org-metaleft-hook))
  18817. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  18818. ((org-with-limited-levels
  18819. (or (org-at-heading-p)
  18820. (and (org-region-active-p)
  18821. (save-excursion
  18822. (goto-char (region-beginning))
  18823. (org-at-heading-p)))))
  18824. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  18825. (call-interactively 'org-do-promote))
  18826. ;; At an inline task.
  18827. ((org-at-heading-p)
  18828. (call-interactively 'org-inlinetask-promote))
  18829. ((or (org-at-item-p)
  18830. (and (org-region-active-p)
  18831. (save-excursion
  18832. (goto-char (region-beginning))
  18833. (org-at-item-p))))
  18834. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  18835. (call-interactively 'org-outdent-item))
  18836. (t (call-interactively 'backward-word))))
  18837. (defun org-metaright (&optional _arg)
  18838. "Demote heading, list item at point or move table column right.
  18839. In front of a drawer or a block keyword, indent it correctly.
  18840. Calls `org-do-demote', `org-indent-item', `org-table-move-column',
  18841. `org-indnet-drawer' or `org-indent-block' depending on context.
  18842. With no specific context, calls the Emacs default `forward-word'.
  18843. See the individual commands for more information.
  18844. This function runs the hook `org-metaright-hook' as a first step,
  18845. and returns at first non-nil value."
  18846. (interactive "P")
  18847. (cond
  18848. ((run-hook-with-args-until-success 'org-metaright-hook))
  18849. ((org-at-table-p) (call-interactively 'org-table-move-column))
  18850. ((org-at-drawer-p) (call-interactively 'org-indent-drawer))
  18851. ((org-at-block-p) (call-interactively 'org-indent-block))
  18852. ((org-with-limited-levels
  18853. (or (org-at-heading-p)
  18854. (and (org-region-active-p)
  18855. (save-excursion
  18856. (goto-char (region-beginning))
  18857. (org-at-heading-p)))))
  18858. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  18859. (call-interactively 'org-do-demote))
  18860. ;; At an inline task.
  18861. ((org-at-heading-p)
  18862. (call-interactively 'org-inlinetask-demote))
  18863. ((or (org-at-item-p)
  18864. (and (org-region-active-p)
  18865. (save-excursion
  18866. (goto-char (region-beginning))
  18867. (org-at-item-p))))
  18868. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  18869. (call-interactively 'org-indent-item))
  18870. (t (call-interactively 'forward-word))))
  18871. (defun org-check-for-hidden (what)
  18872. "Check if there are hidden headlines/items in the current visual line.
  18873. WHAT can be either `headlines' or `items'. If the current line is
  18874. an outline or item heading and it has a folded subtree below it,
  18875. this function returns t, nil otherwise."
  18876. (let ((re (cond
  18877. ((eq what 'headlines) org-outline-regexp-bol)
  18878. ((eq what 'items) (org-item-beginning-re))
  18879. (t (error "This should not happen"))))
  18880. beg end)
  18881. (save-excursion
  18882. (catch 'exit
  18883. (unless (org-region-active-p)
  18884. (setq beg (point-at-bol))
  18885. (beginning-of-line 2)
  18886. (while (and (not (eobp)) ;; this is like `next-line'
  18887. (get-char-property (1- (point)) 'invisible))
  18888. (beginning-of-line 2))
  18889. (setq end (point))
  18890. (goto-char beg)
  18891. (goto-char (point-at-eol))
  18892. (setq end (max end (point)))
  18893. (while (re-search-forward re end t)
  18894. (when (get-char-property (match-beginning 0) 'invisible)
  18895. (throw 'exit t))))
  18896. nil))))
  18897. (defun org-metaup (&optional _arg)
  18898. "Move subtree up or move table row up.
  18899. Calls `org-move-subtree-up' or `org-table-move-row' or
  18900. `org-move-item-up', depending on context. See the individual commands
  18901. for more information."
  18902. (interactive "P")
  18903. (cond
  18904. ((run-hook-with-args-until-success 'org-metaup-hook))
  18905. ((org-region-active-p)
  18906. (let* ((a (min (region-beginning) (region-end)))
  18907. (b (1- (max (region-beginning) (region-end))))
  18908. (c (save-excursion (goto-char a)
  18909. (move-beginning-of-line 0)))
  18910. (d (save-excursion (goto-char a)
  18911. (move-end-of-line 0) (point))))
  18912. (transpose-regions a b c d)
  18913. (goto-char c)))
  18914. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  18915. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  18916. ((org-at-item-p) (call-interactively 'org-move-item-up))
  18917. (t (org-drag-element-backward))))
  18918. (defun org-metadown (&optional _arg)
  18919. "Move subtree down or move table row down.
  18920. Calls `org-move-subtree-down' or `org-table-move-row' or
  18921. `org-move-item-down', depending on context. See the individual
  18922. commands for more information."
  18923. (interactive "P")
  18924. (cond
  18925. ((run-hook-with-args-until-success 'org-metadown-hook))
  18926. ((org-region-active-p)
  18927. (let* ((a (min (region-beginning) (region-end)))
  18928. (b (max (region-beginning) (region-end)))
  18929. (c (save-excursion (goto-char b)
  18930. (move-beginning-of-line 1)))
  18931. (d (save-excursion (goto-char b)
  18932. (move-end-of-line 1) (1+ (point)))))
  18933. (transpose-regions a b c d)
  18934. (goto-char d)))
  18935. ((org-at-table-p) (call-interactively 'org-table-move-row))
  18936. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  18937. ((org-at-item-p) (call-interactively 'org-move-item-down))
  18938. (t (org-drag-element-forward))))
  18939. (defun org-shiftup (&optional arg)
  18940. "Increase item in timestamp or increase priority of current headline.
  18941. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  18942. depending on context. See the individual commands for more information."
  18943. (interactive "P")
  18944. (cond
  18945. ((run-hook-with-args-until-success 'org-shiftup-hook))
  18946. ((and org-support-shift-select (org-region-active-p))
  18947. (org-call-for-shift-select 'previous-line))
  18948. ((org-at-timestamp-p t)
  18949. (call-interactively (if org-edit-timestamp-down-means-later
  18950. 'org-timestamp-down 'org-timestamp-up)))
  18951. ((and (not (eq org-support-shift-select 'always))
  18952. org-enable-priority-commands
  18953. (org-at-heading-p))
  18954. (call-interactively 'org-priority-up))
  18955. ((and (not org-support-shift-select) (org-at-item-p))
  18956. (call-interactively 'org-previous-item))
  18957. ((org-clocktable-try-shift 'up arg))
  18958. ((run-hook-with-args-until-success 'org-shiftup-final-hook))
  18959. (org-support-shift-select
  18960. (org-call-for-shift-select 'previous-line))
  18961. (t (org-shiftselect-error))))
  18962. (defun org-shiftdown (&optional arg)
  18963. "Decrease item in timestamp or decrease priority of current headline.
  18964. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  18965. depending on context. See the individual commands for more information."
  18966. (interactive "P")
  18967. (cond
  18968. ((run-hook-with-args-until-success 'org-shiftdown-hook))
  18969. ((and org-support-shift-select (org-region-active-p))
  18970. (org-call-for-shift-select 'next-line))
  18971. ((org-at-timestamp-p t)
  18972. (call-interactively (if org-edit-timestamp-down-means-later
  18973. 'org-timestamp-up 'org-timestamp-down)))
  18974. ((and (not (eq org-support-shift-select 'always))
  18975. org-enable-priority-commands
  18976. (org-at-heading-p))
  18977. (call-interactively 'org-priority-down))
  18978. ((and (not org-support-shift-select) (org-at-item-p))
  18979. (call-interactively 'org-next-item))
  18980. ((org-clocktable-try-shift 'down arg))
  18981. ((run-hook-with-args-until-success 'org-shiftdown-final-hook))
  18982. (org-support-shift-select
  18983. (org-call-for-shift-select 'next-line))
  18984. (t (org-shiftselect-error))))
  18985. (defun org-shiftright (&optional arg)
  18986. "Cycle the thing at point or in the current line, depending on context.
  18987. Depending on context, this does one of the following:
  18988. - switch a timestamp at point one day into the future
  18989. - on a headline, switch to the next TODO keyword.
  18990. - on an item, switch entire list to the next bullet type
  18991. - on a property line, switch to the next allowed value
  18992. - on a clocktable definition line, move time block into the future"
  18993. (interactive "P")
  18994. (cond
  18995. ((run-hook-with-args-until-success 'org-shiftright-hook))
  18996. ((and org-support-shift-select (org-region-active-p))
  18997. (org-call-for-shift-select 'forward-char))
  18998. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  18999. ((and (not (eq org-support-shift-select 'always))
  19000. (org-at-heading-p))
  19001. (let ((org-inhibit-logging
  19002. (not org-treat-S-cursor-todo-selection-as-state-change))
  19003. (org-inhibit-blocking
  19004. (not org-treat-S-cursor-todo-selection-as-state-change)))
  19005. (org-call-with-arg 'org-todo 'right)))
  19006. ((or (and org-support-shift-select
  19007. (not (eq org-support-shift-select 'always))
  19008. (org-at-item-bullet-p))
  19009. (and (not org-support-shift-select) (org-at-item-p)))
  19010. (org-call-with-arg 'org-cycle-list-bullet nil))
  19011. ((and (not (eq org-support-shift-select 'always))
  19012. (org-at-property-p))
  19013. (call-interactively 'org-property-next-allowed-value))
  19014. ((org-clocktable-try-shift 'right arg))
  19015. ((run-hook-with-args-until-success 'org-shiftright-final-hook))
  19016. (org-support-shift-select
  19017. (org-call-for-shift-select 'forward-char))
  19018. (t (org-shiftselect-error))))
  19019. (defun org-shiftleft (&optional arg)
  19020. "Cycle the thing at point or in the current line, depending on context.
  19021. Depending on context, this does one of the following:
  19022. - switch a timestamp at point one day into the past
  19023. - on a headline, switch to the previous TODO keyword.
  19024. - on an item, switch entire list to the previous bullet type
  19025. - on a property line, switch to the previous allowed value
  19026. - on a clocktable definition line, move time block into the past"
  19027. (interactive "P")
  19028. (cond
  19029. ((run-hook-with-args-until-success 'org-shiftleft-hook))
  19030. ((and org-support-shift-select (org-region-active-p))
  19031. (org-call-for-shift-select 'backward-char))
  19032. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  19033. ((and (not (eq org-support-shift-select 'always))
  19034. (org-at-heading-p))
  19035. (let ((org-inhibit-logging
  19036. (not org-treat-S-cursor-todo-selection-as-state-change))
  19037. (org-inhibit-blocking
  19038. (not org-treat-S-cursor-todo-selection-as-state-change)))
  19039. (org-call-with-arg 'org-todo 'left)))
  19040. ((or (and org-support-shift-select
  19041. (not (eq org-support-shift-select 'always))
  19042. (org-at-item-bullet-p))
  19043. (and (not org-support-shift-select) (org-at-item-p)))
  19044. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  19045. ((and (not (eq org-support-shift-select 'always))
  19046. (org-at-property-p))
  19047. (call-interactively 'org-property-previous-allowed-value))
  19048. ((org-clocktable-try-shift 'left arg))
  19049. ((run-hook-with-args-until-success 'org-shiftleft-final-hook))
  19050. (org-support-shift-select
  19051. (org-call-for-shift-select 'backward-char))
  19052. (t (org-shiftselect-error))))
  19053. (defun org-shiftcontrolright ()
  19054. "Switch to next TODO set."
  19055. (interactive)
  19056. (cond
  19057. ((and org-support-shift-select (org-region-active-p))
  19058. (org-call-for-shift-select 'forward-word))
  19059. ((and (not (eq org-support-shift-select 'always))
  19060. (org-at-heading-p))
  19061. (org-call-with-arg 'org-todo 'nextset))
  19062. (org-support-shift-select
  19063. (org-call-for-shift-select 'forward-word))
  19064. (t (org-shiftselect-error))))
  19065. (defun org-shiftcontrolleft ()
  19066. "Switch to previous TODO set."
  19067. (interactive)
  19068. (cond
  19069. ((and org-support-shift-select (org-region-active-p))
  19070. (org-call-for-shift-select 'backward-word))
  19071. ((and (not (eq org-support-shift-select 'always))
  19072. (org-at-heading-p))
  19073. (org-call-with-arg 'org-todo 'previousset))
  19074. (org-support-shift-select
  19075. (org-call-for-shift-select 'backward-word))
  19076. (t (org-shiftselect-error))))
  19077. (defun org-shiftcontrolup (&optional n)
  19078. "Change timestamps synchronously up in CLOCK log lines.
  19079. Optional argument N tells to change by that many units."
  19080. (interactive "P")
  19081. (if (and (org-at-clock-log-p) (org-at-timestamp-p t))
  19082. (let (org-support-shift-select)
  19083. (org-clock-timestamps-up n))
  19084. (user-error "Not at a clock log")))
  19085. (defun org-shiftcontroldown (&optional n)
  19086. "Change timestamps synchronously down in CLOCK log lines.
  19087. Optional argument N tells to change by that many units."
  19088. (interactive "P")
  19089. (if (and (org-at-clock-log-p) (org-at-timestamp-p t))
  19090. (let (org-support-shift-select)
  19091. (org-clock-timestamps-down n))
  19092. (user-error "Not at a clock log")))
  19093. (defun org-increase-number-at-point (&optional inc)
  19094. "Increment the number at point.
  19095. With an optional prefix numeric argument INC, increment using
  19096. this numeric value."
  19097. (interactive "p")
  19098. (if (not (number-at-point))
  19099. (user-error "Not on a number")
  19100. (unless inc (setq inc 1))
  19101. (let ((pos (point))
  19102. (beg (skip-chars-backward "-+^/*0-9eE."))
  19103. (end (skip-chars-forward "-+^/*0-9eE^.")) nap)
  19104. (setq nap (buffer-substring-no-properties
  19105. (+ pos beg) (+ pos beg end)))
  19106. (delete-region (+ pos beg) (+ pos beg end))
  19107. (insert (calc-eval (concat (number-to-string inc) "+" nap))))
  19108. (when (org-at-table-p)
  19109. (org-table-align)
  19110. (org-table-end-of-field 1))))
  19111. (defun org-decrease-number-at-point (&optional inc)
  19112. "Decrement the number at point.
  19113. With an optional prefix numeric argument INC, decrement using
  19114. this numeric value."
  19115. (interactive "p")
  19116. (org-increase-number-at-point (- (or inc 1))))
  19117. (defun org-ctrl-c-ret ()
  19118. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  19119. (interactive)
  19120. (cond
  19121. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  19122. (t (call-interactively 'org-insert-heading))))
  19123. (defun org-find-visible ()
  19124. (let ((s (point)))
  19125. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  19126. (get-char-property s 'invisible)))
  19127. s))
  19128. (defun org-find-invisible ()
  19129. (let ((s (point)))
  19130. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  19131. (not (get-char-property s 'invisible))))
  19132. s))
  19133. (defun org-copy-visible (beg end)
  19134. "Copy the visible parts of the region."
  19135. (interactive "r")
  19136. (let (snippets s)
  19137. (save-excursion
  19138. (save-restriction
  19139. (narrow-to-region beg end)
  19140. (setq s (goto-char (point-min)))
  19141. (while (not (= (point) (point-max)))
  19142. (goto-char (org-find-invisible))
  19143. (push (buffer-substring s (point)) snippets)
  19144. (setq s (goto-char (org-find-visible))))))
  19145. (kill-new (apply 'concat (nreverse snippets)))))
  19146. (defun org-copy-special ()
  19147. "Copy region in table or copy current subtree.
  19148. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  19149. See the individual commands for more information."
  19150. (interactive)
  19151. (call-interactively
  19152. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  19153. (defun org-cut-special ()
  19154. "Cut region in table or cut current subtree.
  19155. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  19156. See the individual commands for more information."
  19157. (interactive)
  19158. (call-interactively
  19159. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  19160. (defun org-paste-special (arg)
  19161. "Paste rectangular region into table, or past subtree relative to level.
  19162. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  19163. See the individual commands for more information."
  19164. (interactive "P")
  19165. (if (org-at-table-p)
  19166. (org-table-paste-rectangle)
  19167. (org-paste-subtree arg)))
  19168. (defsubst org-in-fixed-width-region-p ()
  19169. "Is point in a fixed-width region?"
  19170. (save-match-data
  19171. (eq 'fixed-width (org-element-type (org-element-at-point)))))
  19172. (defun org-edit-special (&optional arg)
  19173. "Call a special editor for the element at point.
  19174. When at a table, call the formula editor with `org-table-edit-formulas'.
  19175. When in a source code block, call `org-edit-src-code'.
  19176. When in a fixed-width region, call `org-edit-fixed-width-region'.
  19177. When in an export block, call `org-edit-export-block'.
  19178. When at an #+INCLUDE keyword, visit the included file.
  19179. When at a footnote reference, call `org-edit-footnote-reference'
  19180. On a link, call `ffap' to visit the link at point.
  19181. Otherwise, return a user error."
  19182. (interactive "P")
  19183. (let ((element (org-element-at-point)))
  19184. (cl-assert (not buffer-read-only) nil
  19185. "Buffer is read-only: %s" (buffer-name))
  19186. (pcase (org-element-type element)
  19187. (`src-block
  19188. (if (not arg) (org-edit-src-code)
  19189. (let* ((info (org-babel-get-src-block-info))
  19190. (lang (nth 0 info))
  19191. (params (nth 2 info))
  19192. (session (cdr (assq :session params))))
  19193. (if (not session) (org-edit-src-code)
  19194. ;; At a src-block with a session and function called with
  19195. ;; an ARG: switch to the buffer related to the inferior
  19196. ;; process.
  19197. (switch-to-buffer
  19198. (funcall (intern (concat "org-babel-prep-session:" lang))
  19199. session params))))))
  19200. (`keyword
  19201. (if (member (org-element-property :key element) '("INCLUDE" "SETUPFILE"))
  19202. (org-open-link-from-string
  19203. (format "[[%s]]"
  19204. (expand-file-name
  19205. (let ((value (org-element-property :value element)))
  19206. (cond ((not (org-string-nw-p value))
  19207. (user-error "No file to edit"))
  19208. ((string-match "\\`\"\\(.*?\\)\"" value)
  19209. (match-string 1 value))
  19210. ((string-match "\\`[^ \t\"]\\S-*" value)
  19211. (match-string 0 value))
  19212. (t (user-error "No valid file specified")))))))
  19213. (user-error "No special environment to edit here")))
  19214. (`table
  19215. (if (eq (org-element-property :type element) 'table.el)
  19216. (org-edit-table.el)
  19217. (call-interactively 'org-table-edit-formulas)))
  19218. ;; Only Org tables contain `table-row' type elements.
  19219. (`table-row (call-interactively 'org-table-edit-formulas))
  19220. (`example-block (org-edit-src-code))
  19221. (`export-block (org-edit-export-block))
  19222. (`fixed-width (org-edit-fixed-width-region))
  19223. (_
  19224. ;; No notable element at point. Though, we may be at a link or
  19225. ;; a footnote reference, which are objects. Thus, scan deeper.
  19226. (let ((context (org-element-context element)))
  19227. (pcase (org-element-type context)
  19228. (`footnote-reference (org-edit-footnote-reference))
  19229. (`inline-src-block (org-edit-inline-src-code))
  19230. (`link (call-interactively #'ffap))
  19231. (_ (user-error "No special environment to edit here"))))))))
  19232. (defvar org-table-coordinate-overlays) ; defined in org-table.el
  19233. (defun org-ctrl-c-ctrl-c (&optional arg)
  19234. "Set tags in headline, or update according to changed information at point.
  19235. This command does many different things, depending on context:
  19236. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  19237. this is what we do.
  19238. - If the cursor is on a statistics cookie, update it.
  19239. - If the cursor is in a headline, prompt for tags and insert them
  19240. into the current line, aligned to `org-tags-column'. When called
  19241. with prefix arg, realign all tags in the current buffer.
  19242. - If the cursor is in one of the special #+KEYWORD lines, this
  19243. triggers scanning the buffer for these lines and updating the
  19244. information.
  19245. - If the cursor is inside a table, realign the table. This command
  19246. works even if the automatic table editor has been turned off.
  19247. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  19248. the entire table.
  19249. - If the cursor is at a footnote reference or definition, jump to
  19250. the corresponding definition or references, respectively.
  19251. - If the cursor is a the beginning of a dynamic block, update it.
  19252. - If the current buffer is a capture buffer, close note and file it.
  19253. - If the cursor is on a <<<target>>>, update radio targets and
  19254. corresponding links in this buffer.
  19255. - If the cursor is on a numbered item in a plain list, renumber the
  19256. ordered list.
  19257. - If the cursor is on a checkbox, toggle it.
  19258. - If the cursor is on a code block, evaluate it. The variable
  19259. `org-confirm-babel-evaluate' can be used to control prompting
  19260. before code block evaluation, by default every code block
  19261. evaluation requires confirmation. Code block evaluation can be
  19262. inhibited by setting `org-babel-no-eval-on-ctrl-c-ctrl-c'."
  19263. (interactive "P")
  19264. (cond
  19265. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  19266. org-occur-highlights)
  19267. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  19268. (org-remove-occur-highlights)
  19269. (message "Temporary highlights/overlays removed from current buffer"))
  19270. ((and (local-variable-p 'org-finish-function (current-buffer))
  19271. (fboundp org-finish-function))
  19272. (funcall org-finish-function))
  19273. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  19274. (t
  19275. (if (save-excursion (beginning-of-line) (looking-at "[ \t]*$"))
  19276. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  19277. (user-error "C-c C-c can do nothing useful at this location"))
  19278. (let* ((context (org-element-context))
  19279. (type (org-element-type context)))
  19280. (cl-case type
  19281. ;; When at a link, act according to the parent instead.
  19282. (link (setq context (org-element-property :parent context))
  19283. (setq type (org-element-type context)))
  19284. ;; Unsupported object types: refer to the first supported
  19285. ;; element or object containing it.
  19286. ((bold code entity export-snippet inline-babel-call inline-src-block
  19287. italic latex-fragment line-break macro strike-through subscript
  19288. superscript underline verbatim)
  19289. (setq context
  19290. (org-element-lineage
  19291. context '(radio-target paragraph verse-block table-cell)))))
  19292. ;; For convenience: at the first line of a paragraph on the
  19293. ;; same line as an item, apply function on that item instead.
  19294. (when (eq type 'paragraph)
  19295. (let ((parent (org-element-property :parent context)))
  19296. (when (and (eq (org-element-type parent) 'item)
  19297. (= (line-beginning-position)
  19298. (org-element-property :begin parent)))
  19299. (setq context parent type 'item))))
  19300. ;; Act according to type of element or object at point.
  19301. (cl-case type
  19302. (clock (org-clock-update-time-maybe))
  19303. (dynamic-block
  19304. (save-excursion
  19305. (goto-char (org-element-property :post-affiliated context))
  19306. (org-update-dblock)))
  19307. (footnote-definition
  19308. (goto-char (org-element-property :post-affiliated context))
  19309. (call-interactively 'org-footnote-action))
  19310. (footnote-reference (call-interactively 'org-footnote-action))
  19311. ((headline inlinetask)
  19312. (save-excursion (goto-char (org-element-property :begin context))
  19313. (call-interactively 'org-set-tags)))
  19314. (item
  19315. ;; At an item: a double C-u set checkbox to "[-]"
  19316. ;; unconditionally, whereas a single one will toggle its
  19317. ;; presence. Without a universal argument, if the item
  19318. ;; has a checkbox, toggle it. Otherwise repair the list.
  19319. (let* ((box (org-element-property :checkbox context))
  19320. (struct (org-element-property :structure context))
  19321. (old-struct (copy-tree struct))
  19322. (parents (org-list-parents-alist struct))
  19323. (prevs (org-list-prevs-alist struct))
  19324. (orderedp (org-not-nil (org-entry-get nil "ORDERED"))))
  19325. (org-list-set-checkbox
  19326. (org-element-property :begin context) struct
  19327. (cond ((equal arg '(16)) "[-]")
  19328. ((and (not box) (equal arg '(4))) "[ ]")
  19329. ((or (not box) (equal arg '(4))) nil)
  19330. ((eq box 'on) "[ ]")
  19331. (t "[X]")))
  19332. ;; Mimic `org-list-write-struct' but with grabbing
  19333. ;; a return value from `org-list-struct-fix-box'.
  19334. (org-list-struct-fix-ind struct parents 2)
  19335. (org-list-struct-fix-item-end struct)
  19336. (org-list-struct-fix-bul struct prevs)
  19337. (org-list-struct-fix-ind struct parents)
  19338. (let ((block-item
  19339. (org-list-struct-fix-box struct parents prevs orderedp)))
  19340. (if (and box (equal struct old-struct))
  19341. (if (equal arg '(16))
  19342. (message "Checkboxes already reset")
  19343. (user-error "Cannot toggle this checkbox: %s"
  19344. (if (eq box 'on)
  19345. "all subitems checked"
  19346. "unchecked subitems")))
  19347. (org-list-struct-apply-struct struct old-struct)
  19348. (org-update-checkbox-count-maybe))
  19349. (when block-item
  19350. (message "Checkboxes were removed due to empty box at line %d"
  19351. (org-current-line block-item))))))
  19352. (keyword
  19353. (let ((org-inhibit-startup-visibility-stuff t)
  19354. (org-startup-align-all-tables nil))
  19355. (when (boundp 'org-table-coordinate-overlays)
  19356. (mapc #'delete-overlay org-table-coordinate-overlays)
  19357. (setq org-table-coordinate-overlays nil))
  19358. (org-save-outline-visibility 'use-markers (org-mode-restart)))
  19359. (message "Local setup has been refreshed"))
  19360. (plain-list
  19361. ;; At a plain list, with a double C-u argument, set
  19362. ;; checkboxes of each item to "[-]", whereas a single one
  19363. ;; will toggle their presence according to the state of the
  19364. ;; first item in the list. Without an argument, repair the
  19365. ;; list.
  19366. (let* ((begin (org-element-property :contents-begin context))
  19367. (beginm (move-marker (make-marker) begin))
  19368. (struct (org-element-property :structure context))
  19369. (old-struct (copy-tree struct))
  19370. (first-box (save-excursion
  19371. (goto-char begin)
  19372. (looking-at org-list-full-item-re)
  19373. (match-string-no-properties 3)))
  19374. (new-box (cond ((equal arg '(16)) "[-]")
  19375. ((equal arg '(4)) (unless first-box "[ ]"))
  19376. ((equal first-box "[X]") "[ ]")
  19377. (t "[X]"))))
  19378. (cond
  19379. (arg
  19380. (dolist (pos
  19381. (org-list-get-all-items
  19382. begin struct (org-list-prevs-alist struct)))
  19383. (org-list-set-checkbox pos struct new-box)))
  19384. ((and first-box (eq (point) begin))
  19385. ;; For convenience, when point is at bol on the first
  19386. ;; item of the list and no argument is provided, simply
  19387. ;; toggle checkbox of that item, if any.
  19388. (org-list-set-checkbox begin struct new-box)))
  19389. (org-list-write-struct
  19390. struct (org-list-parents-alist struct) old-struct)
  19391. (org-update-checkbox-count-maybe)
  19392. (save-excursion (goto-char beginm) (org-list-send-list 'maybe))))
  19393. ((property-drawer node-property)
  19394. (call-interactively 'org-property-action))
  19395. ((radio-target target)
  19396. (call-interactively 'org-update-radio-target-regexp))
  19397. (statistics-cookie
  19398. (call-interactively 'org-update-statistics-cookies))
  19399. ((table table-cell table-row)
  19400. ;; At a table, recalculate every field and align it. Also
  19401. ;; send the table if necessary. If the table has
  19402. ;; a `table.el' type, just give up. At a table row or
  19403. ;; cell, maybe recalculate line but always align table.
  19404. (if (eq (org-element-property :type context) 'table.el)
  19405. (message "%s" (substitute-command-keys "\\<org-mode-map>\
  19406. Use \\[org-edit-special] to edit table.el tables"))
  19407. (let ((org-enable-table-editor t))
  19408. (if (or (eq type 'table)
  19409. ;; Check if point is at a TBLFM line.
  19410. (and (eq type 'table-row)
  19411. (= (point) (org-element-property :end context))))
  19412. (save-excursion
  19413. (if (org-at-TBLFM-p)
  19414. (progn (require 'org-table)
  19415. (org-table-calc-current-TBLFM))
  19416. (goto-char (org-element-property :contents-begin context))
  19417. (org-call-with-arg 'org-table-recalculate (or arg t))
  19418. (orgtbl-send-table 'maybe)))
  19419. (org-table-maybe-eval-formula)
  19420. (cond (arg (call-interactively 'org-table-recalculate))
  19421. ((org-table-maybe-recalculate-line))
  19422. (t (org-table-align)))))))
  19423. (timestamp (org-timestamp-change 0 'day))
  19424. (otherwise
  19425. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  19426. (user-error
  19427. "C-c C-c can do nothing useful at this location")))))))))
  19428. (defun org-mode-restart ()
  19429. (interactive)
  19430. (let ((indent-status (org-bound-and-true-p org-indent-mode)))
  19431. (funcall major-mode)
  19432. (hack-local-variables)
  19433. (when (and indent-status (not (org-bound-and-true-p org-indent-mode)))
  19434. (org-indent-mode -1)))
  19435. (message "%s restarted" major-mode))
  19436. (defun org-kill-note-or-show-branches ()
  19437. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  19438. (interactive)
  19439. (if (not org-finish-function)
  19440. (progn
  19441. (outline-hide-subtree)
  19442. (call-interactively 'outline-show-branches))
  19443. (let ((org-note-abort t))
  19444. (funcall org-finish-function))))
  19445. (defun org-delete-indentation (&optional ARG)
  19446. "Join current line to previous and fix whitespace at join.
  19447. If previous line is a headline add to headline title. Otherwise
  19448. the function calls `delete-indentation'.
  19449. With argument, join this line to following line."
  19450. (interactive "*P")
  19451. (if (save-excursion
  19452. (if ARG (beginning-of-line)
  19453. (forward-line -1))
  19454. (looking-at org-complex-heading-regexp))
  19455. ;; At headline.
  19456. (let ((tags-column (when (match-beginning 5)
  19457. (save-excursion (goto-char (match-beginning 5))
  19458. (current-column))))
  19459. (string (concat " " (progn (when ARG (forward-line 1))
  19460. (org-trim (delete-and-extract-region
  19461. (line-beginning-position)
  19462. (line-end-position)))))))
  19463. (unless (bobp) (delete-region (point) (1- (point))))
  19464. (goto-char (or (match-end 4)
  19465. (match-beginning 5)
  19466. (match-end 0)))
  19467. (skip-chars-backward " \t")
  19468. (save-excursion (insert string))
  19469. ;; Adjust alignment of tags.
  19470. (when tags-column
  19471. (org-align-tags-here (if org-auto-align-tags
  19472. org-tags-column
  19473. tags-column))))
  19474. (delete-indentation ARG)))
  19475. (defun org-open-line (n)
  19476. "Insert a new row in tables, call `open-line' elsewhere.
  19477. If `org-special-ctrl-o' is nil, just call `open-line' everywhere."
  19478. (interactive "*p")
  19479. (cond
  19480. ((not org-special-ctrl-o)
  19481. (open-line n))
  19482. ((org-at-table-p)
  19483. (org-table-insert-row))
  19484. (t
  19485. (open-line n))))
  19486. (defun org-return (&optional indent)
  19487. "Goto next table row or insert a newline.
  19488. Calls `org-table-next-row' or `newline', depending on context.
  19489. When optional INDENT argument is non-nil, call
  19490. `newline-and-indent' instead of `newline'.
  19491. When `org-return-follows-link' is non-nil and point is on
  19492. a timestamp or a link, call `org-open-at-point'. However, it
  19493. will not happen if point is in a table or on a \"dead\"
  19494. object (e.g., within a comment). In these case, you need to use
  19495. `org-open-at-point' directly."
  19496. (interactive)
  19497. (let ((context (if org-return-follows-link (org-element-context)
  19498. (org-element-at-point))))
  19499. (cond
  19500. ;; In a table, call `org-table-next-row'.
  19501. ((or (and (eq (org-element-type context) 'table)
  19502. (>= (point) (org-element-property :contents-begin context))
  19503. (< (point) (org-element-property :contents-end context)))
  19504. (org-element-lineage context '(table-row table-cell) t))
  19505. (org-table-justify-field-maybe)
  19506. (call-interactively #'org-table-next-row))
  19507. ;; On a link or a timestamp, call `org-open-at-point' if
  19508. ;; `org-return-follows-link' allows it. Tolerate fuzzy
  19509. ;; locations, e.g., in a comment, as `org-open-at-point'.
  19510. ((and org-return-follows-link
  19511. (or (org-in-regexp org-ts-regexp-both nil t)
  19512. (org-in-regexp org-tsr-regexp-both nil t)
  19513. (org-in-regexp org-any-link-re nil t)))
  19514. (call-interactively #'org-open-at-point))
  19515. ;; Insert newline in heading, but preserve tags.
  19516. ((and (not (bolp))
  19517. (save-excursion (beginning-of-line)
  19518. (looking-at org-complex-heading-regexp)))
  19519. ;; At headline. Split line. However, if point is on keyword,
  19520. ;; priority cookie or tags, do not break any of them: add
  19521. ;; a newline after the headline instead.
  19522. (let ((tags-column (and (match-beginning 5)
  19523. (save-excursion (goto-char (match-beginning 5))
  19524. (current-column))))
  19525. (string
  19526. (when (and (match-end 4)
  19527. (>= (point)
  19528. (or (match-end 3) (match-end 2) (1+ (match-end 1))))
  19529. (<= (point) (match-end 4)))
  19530. (delete-and-extract-region (point) (match-end 4)))))
  19531. (when (and tags-column string) ; Adjust tag alignment.
  19532. (org-align-tags-here
  19533. (if org-auto-align-tags org-tags-column tags-column)))
  19534. (end-of-line)
  19535. (org-show-entry)
  19536. (if indent (newline-and-indent) (newline))
  19537. (when string (save-excursion (insert (org-trim string))))))
  19538. ;; In a list, make sure indenting keeps trailing text within.
  19539. ((and indent
  19540. (not (eolp))
  19541. (org-element-lineage context '(item)))
  19542. (let ((trailing-data
  19543. (delete-and-extract-region (point) (line-end-position))))
  19544. (newline-and-indent)
  19545. (save-excursion (insert trailing-data))))
  19546. (t (if indent (newline-and-indent) (newline))))))
  19547. (defun org-return-indent ()
  19548. "Goto next table row or insert a newline and indent.
  19549. Calls `org-table-next-row' or `newline-and-indent', depending on
  19550. context. See the individual commands for more information."
  19551. (interactive)
  19552. (org-return t))
  19553. (defun org-ctrl-c-star ()
  19554. "Compute table, or change heading status of lines.
  19555. Calls `org-table-recalculate' or `org-toggle-heading',
  19556. depending on context."
  19557. (interactive)
  19558. (cond
  19559. ((org-at-table-p)
  19560. (call-interactively 'org-table-recalculate))
  19561. (t
  19562. ;; Convert all lines in region to list items
  19563. (call-interactively 'org-toggle-heading))))
  19564. (defun org-ctrl-c-minus ()
  19565. "Insert separator line in table or modify bullet status of line.
  19566. Also turns a plain line or a region of lines into list items.
  19567. Calls `org-table-insert-hline', `org-toggle-item', or
  19568. `org-cycle-list-bullet', depending on context."
  19569. (interactive)
  19570. (cond
  19571. ((org-at-table-p)
  19572. (call-interactively 'org-table-insert-hline))
  19573. ((org-region-active-p)
  19574. (call-interactively 'org-toggle-item))
  19575. ((org-in-item-p)
  19576. (call-interactively 'org-cycle-list-bullet))
  19577. (t
  19578. (call-interactively 'org-toggle-item))))
  19579. (defun org-toggle-item (arg)
  19580. "Convert headings or normal lines to items, items to normal lines.
  19581. If there is no active region, only the current line is considered.
  19582. If the first non blank line in the region is a headline, convert
  19583. all headlines to items, shifting text accordingly.
  19584. If it is an item, convert all items to normal lines.
  19585. If it is normal text, change region into a list of items.
  19586. With a prefix argument ARG, change the region in a single item."
  19587. (interactive "P")
  19588. (let ((shift-text
  19589. (function
  19590. ;; Shift text in current section to IND, from point to END.
  19591. ;; The function leaves point to END line.
  19592. (lambda (ind end)
  19593. (let ((min-i 1000) (end (copy-marker end)))
  19594. ;; First determine the minimum indentation (MIN-I) of
  19595. ;; the text.
  19596. (save-excursion
  19597. (catch 'exit
  19598. (while (< (point) end)
  19599. (let ((i (org-get-indentation)))
  19600. (cond
  19601. ;; Skip blank lines and inline tasks.
  19602. ((looking-at "^[ \t]*$"))
  19603. ((looking-at org-outline-regexp-bol))
  19604. ;; We can't find less than 0 indentation.
  19605. ((zerop i) (throw 'exit (setq min-i 0)))
  19606. ((< i min-i) (setq min-i i))))
  19607. (forward-line))))
  19608. ;; Then indent each line so that a line indented to
  19609. ;; MIN-I becomes indented to IND. Ignore blank lines
  19610. ;; and inline tasks in the process.
  19611. (let ((delta (- ind min-i)))
  19612. (while (< (point) end)
  19613. (unless (or (looking-at "^[ \t]*$")
  19614. (looking-at org-outline-regexp-bol))
  19615. (org-indent-line-to (+ (org-get-indentation) delta)))
  19616. (forward-line)))))))
  19617. (skip-blanks
  19618. (function
  19619. ;; Return beginning of first non-blank line, starting from
  19620. ;; line at POS.
  19621. (lambda (pos)
  19622. (save-excursion
  19623. (goto-char pos)
  19624. (skip-chars-forward " \r\t\n")
  19625. (point-at-bol)))))
  19626. beg end)
  19627. ;; Determine boundaries of changes.
  19628. (if (org-region-active-p)
  19629. (setq beg (funcall skip-blanks (region-beginning))
  19630. end (copy-marker (region-end)))
  19631. (setq beg (funcall skip-blanks (point-at-bol))
  19632. end (copy-marker (point-at-eol))))
  19633. ;; Depending on the starting line, choose an action on the text
  19634. ;; between BEG and END.
  19635. (org-with-limited-levels
  19636. (save-excursion
  19637. (goto-char beg)
  19638. (cond
  19639. ;; Case 1. Start at an item: de-itemize. Note that it only
  19640. ;; happens when a region is active: `org-ctrl-c-minus'
  19641. ;; would call `org-cycle-list-bullet' otherwise.
  19642. ((org-at-item-p)
  19643. (while (< (point) end)
  19644. (when (org-at-item-p)
  19645. (skip-chars-forward " \t")
  19646. (delete-region (point) (match-end 0)))
  19647. (forward-line)))
  19648. ;; Case 2. Start at an heading: convert to items.
  19649. ((org-at-heading-p)
  19650. (let* ((bul (org-list-bullet-string "-"))
  19651. (bul-len (length bul))
  19652. (done (org-entry-is-done-p))
  19653. (todo (org-entry-is-todo-p))
  19654. ;; Indentation of the first heading. It should be
  19655. ;; relative to the indentation of its parent, if any.
  19656. (start-ind (save-excursion
  19657. (cond
  19658. ((not org-adapt-indentation) 0)
  19659. ((not (outline-previous-heading)) 0)
  19660. (t (length (match-string 0))))))
  19661. ;; Level of first heading. Further headings will be
  19662. ;; compared to it to determine hierarchy in the list.
  19663. (ref-level (org-reduced-level (org-outline-level))))
  19664. (when (or done todo) (org-todo ""))
  19665. (while (< (point) end)
  19666. (let* ((level (org-reduced-level (org-outline-level)))
  19667. (delta (max 0 (- level ref-level))))
  19668. ;; If current headline is less indented than the first
  19669. ;; one, set it as reference, in order to preserve
  19670. ;; subtrees.
  19671. (when (< level ref-level) (setq ref-level level))
  19672. (replace-match bul t t)
  19673. (org-indent-line-to (+ start-ind (* delta bul-len)))
  19674. (when (or done todo)
  19675. (let* ((struct (org-list-struct))
  19676. (old (copy-tree struct)))
  19677. (org-list-set-checkbox (line-beginning-position)
  19678. struct
  19679. (if done "[X]" "[ ]"))
  19680. (org-list-write-struct struct
  19681. (org-list-parents-alist struct)
  19682. old)))
  19683. ;; Ensure all text down to END (or SECTION-END) belongs
  19684. ;; to the newly created item.
  19685. (let ((section-end (save-excursion
  19686. (or (outline-next-heading) (point)))))
  19687. (forward-line)
  19688. (funcall shift-text
  19689. (+ start-ind (* (1+ delta) bul-len))
  19690. (min end section-end)))))))
  19691. ;; Case 3. Normal line with ARG: make the first line of region
  19692. ;; an item, and shift indentation of others lines to
  19693. ;; set them as item's body.
  19694. (arg (let* ((bul (org-list-bullet-string "-"))
  19695. (bul-len (length bul))
  19696. (ref-ind (org-get-indentation)))
  19697. (skip-chars-forward " \t")
  19698. (insert bul)
  19699. (forward-line)
  19700. (while (< (point) end)
  19701. ;; Ensure that lines less indented than first one
  19702. ;; still get included in item body.
  19703. (funcall shift-text
  19704. (+ ref-ind bul-len)
  19705. (min end (save-excursion (or (outline-next-heading)
  19706. (point)))))
  19707. (forward-line))))
  19708. ;; Case 4. Normal line without ARG: turn each non-item line
  19709. ;; into an item.
  19710. (t
  19711. (while (< (point) end)
  19712. (unless (or (org-at-heading-p) (org-at-item-p))
  19713. (when (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  19714. (replace-match
  19715. (concat "\\1" (org-list-bullet-string "-") "\\2"))))
  19716. (forward-line))))))))
  19717. (defun org-toggle-heading (&optional nstars)
  19718. "Convert headings to normal text, or items or text to headings.
  19719. If there is no active region, only convert the current line.
  19720. With a \\[universal-argument] prefix, convert the whole list at
  19721. point into heading.
  19722. In a region:
  19723. - If the first non blank line is a headline, remove the stars
  19724. from all headlines in the region.
  19725. - If it is a normal line, turn each and every normal line (i.e.,
  19726. not an heading or an item) in the region into headings. If you
  19727. want to convert only the first line of this region, use one
  19728. universal prefix argument.
  19729. - If it is a plain list item, turn all plain list items into headings.
  19730. When converting a line into a heading, the number of stars is chosen
  19731. such that the lines become children of the current entry. However,
  19732. when a numeric prefix argument is given, its value determines the
  19733. number of stars to add."
  19734. (interactive "P")
  19735. (let ((skip-blanks
  19736. (function
  19737. ;; Return beginning of first non-blank line, starting from
  19738. ;; line at POS.
  19739. (lambda (pos)
  19740. (save-excursion
  19741. (goto-char pos)
  19742. (while (org-at-comment-p) (forward-line))
  19743. (skip-chars-forward " \r\t\n")
  19744. (point-at-bol)))))
  19745. beg end toggled)
  19746. ;; Determine boundaries of changes. If a universal prefix has
  19747. ;; been given, put the list in a region. If region ends at a bol,
  19748. ;; do not consider the last line to be in the region.
  19749. (when (and current-prefix-arg (org-at-item-p))
  19750. (when (listp current-prefix-arg) (setq current-prefix-arg 1))
  19751. (org-mark-element))
  19752. (if (org-region-active-p)
  19753. (setq beg (funcall skip-blanks (region-beginning))
  19754. end (copy-marker (save-excursion
  19755. (goto-char (region-end))
  19756. (if (bolp) (point) (point-at-eol)))))
  19757. (setq beg (funcall skip-blanks (point-at-bol))
  19758. end (copy-marker (point-at-eol))))
  19759. ;; Ensure inline tasks don't count as headings.
  19760. (org-with-limited-levels
  19761. (save-excursion
  19762. (goto-char beg)
  19763. (cond
  19764. ;; Case 1. Started at an heading: de-star headings.
  19765. ((org-at-heading-p)
  19766. (while (< (point) end)
  19767. (when (org-at-heading-p t)
  19768. (looking-at org-outline-regexp) (replace-match "")
  19769. (setq toggled t))
  19770. (forward-line)))
  19771. ;; Case 2. Started at an item: change items into headlines.
  19772. ;; One star will be added by `org-list-to-subtree'.
  19773. ((org-at-item-p)
  19774. (while (< (point) end)
  19775. (when (org-at-item-p)
  19776. ;; Pay attention to cases when region ends before list.
  19777. (let* ((struct (org-list-struct))
  19778. (list-end
  19779. (min (org-list-get-bottom-point struct) (1+ end))))
  19780. (save-restriction
  19781. (narrow-to-region (point) list-end)
  19782. (insert (org-list-to-subtree (org-list-to-lisp t)) "\n")))
  19783. (setq toggled t))
  19784. (forward-line)))
  19785. ;; Case 3. Started at normal text: make every line an heading,
  19786. ;; skipping headlines and items.
  19787. (t (let* ((stars
  19788. (make-string
  19789. (if (numberp nstars) nstars (or (org-current-level) 0)) ?*))
  19790. (add-stars
  19791. (cond (nstars "") ; stars from prefix only
  19792. ((equal stars "") "*") ; before first heading
  19793. (org-odd-levels-only "**") ; inside heading, odd
  19794. (t "*"))) ; inside heading, oddeven
  19795. (rpl (concat stars add-stars " "))
  19796. (lend (when (listp nstars) (save-excursion (end-of-line) (point)))))
  19797. (while (< (point) (if (equal nstars '(4)) lend end))
  19798. (when (and (not (or (org-at-heading-p) (org-at-item-p) (org-at-comment-p)))
  19799. (looking-at "\\([ \t]*\\)\\(\\S-\\)"))
  19800. (replace-match (concat rpl (match-string 2))) (setq toggled t))
  19801. (forward-line)))))))
  19802. (unless toggled (message "Cannot toggle heading from here"))))
  19803. (defun org-meta-return (&optional _arg)
  19804. "Insert a new heading or wrap a region in a table.
  19805. Calls `org-insert-heading' or `org-table-wrap-region', depending
  19806. on context. See the individual commands for more information."
  19807. (interactive)
  19808. (org-check-before-invisible-edit 'insert)
  19809. (or (run-hook-with-args-until-success 'org-metareturn-hook)
  19810. (call-interactively (if (org-at-table-p) #'org-table-wrap-region
  19811. #'org-insert-heading))))
  19812. ;;; Menu entries
  19813. (defsubst org-in-subtree-not-table-p ()
  19814. "Are we in a subtree and not in a table?"
  19815. (and (not (org-before-first-heading-p))
  19816. (not (org-at-table-p))))
  19817. ;; Define the Org-mode menus
  19818. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  19819. '("Tbl"
  19820. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  19821. ["Next Field" org-cycle (org-at-table-p)]
  19822. ["Previous Field" org-shifttab (org-at-table-p)]
  19823. ["Next Row" org-return (org-at-table-p)]
  19824. "--"
  19825. ["Blank Field" org-table-blank-field (org-at-table-p)]
  19826. ["Edit Field" org-table-edit-field (org-at-table-p)]
  19827. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  19828. "--"
  19829. ("Column"
  19830. ["Move Column Left" org-metaleft (org-at-table-p)]
  19831. ["Move Column Right" org-metaright (org-at-table-p)]
  19832. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  19833. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  19834. ("Row"
  19835. ["Move Row Up" org-metaup (org-at-table-p)]
  19836. ["Move Row Down" org-metadown (org-at-table-p)]
  19837. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  19838. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  19839. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  19840. "--"
  19841. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  19842. ("Rectangle"
  19843. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  19844. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  19845. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  19846. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  19847. "--"
  19848. ("Calculate"
  19849. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  19850. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  19851. ["Edit Formulas" org-edit-special (org-at-table-p)]
  19852. "--"
  19853. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  19854. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  19855. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  19856. "--"
  19857. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  19858. "--"
  19859. ["Sum Column/Rectangle" org-table-sum
  19860. (or (org-at-table-p) (org-region-active-p))]
  19861. ["Which Column?" org-table-current-column (org-at-table-p)])
  19862. ["Debug Formulas"
  19863. org-table-toggle-formula-debugger
  19864. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  19865. ["Show Col/Row Numbers"
  19866. org-table-toggle-coordinate-overlays
  19867. :style toggle
  19868. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  19869. "--"
  19870. ["Create" org-table-create (and (not (org-at-table-p))
  19871. org-enable-table-editor)]
  19872. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  19873. ["Import from File" org-table-import (not (org-at-table-p))]
  19874. ["Export to File" org-table-export (org-at-table-p)]
  19875. "--"
  19876. ["Create/Convert from/to table.el" org-table-create-with-table.el t]
  19877. "--"
  19878. ("Plot"
  19879. ["Ascii plot" orgtbl-ascii-plot :active (org-at-table-p) :keys "C-c \" a"]
  19880. ["Gnuplot" org-plot/gnuplot :active (org-at-table-p) :keys "C-c \" g"])))
  19881. (easy-menu-define org-org-menu org-mode-map "Org menu"
  19882. '("Org"
  19883. ("Show/Hide"
  19884. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  19885. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  19886. ["Sparse Tree..." org-sparse-tree t]
  19887. ["Reveal Context" org-reveal t]
  19888. ["Show All" outline-show-all t]
  19889. "--"
  19890. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  19891. "--"
  19892. ["New Heading" org-insert-heading t]
  19893. ("Navigate Headings"
  19894. ["Up" outline-up-heading t]
  19895. ["Next" outline-next-visible-heading t]
  19896. ["Previous" outline-previous-visible-heading t]
  19897. ["Next Same Level" outline-forward-same-level t]
  19898. ["Previous Same Level" outline-backward-same-level t]
  19899. "--"
  19900. ["Jump" org-goto t])
  19901. ("Edit Structure"
  19902. ["Refile Subtree" org-refile (org-in-subtree-not-table-p)]
  19903. "--"
  19904. ["Move Subtree Up" org-metaup (org-at-heading-p)]
  19905. ["Move Subtree Down" org-metadown (org-at-heading-p)]
  19906. "--"
  19907. ["Copy Subtree" org-copy-special (org-in-subtree-not-table-p)]
  19908. ["Cut Subtree" org-cut-special (org-in-subtree-not-table-p)]
  19909. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  19910. "--"
  19911. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  19912. "--"
  19913. ["Copy visible text" org-copy-visible t]
  19914. "--"
  19915. ["Promote Heading" org-metaleft (org-in-subtree-not-table-p)]
  19916. ["Promote Subtree" org-shiftmetaleft (org-in-subtree-not-table-p)]
  19917. ["Demote Heading" org-metaright (org-in-subtree-not-table-p)]
  19918. ["Demote Subtree" org-shiftmetaright (org-in-subtree-not-table-p)]
  19919. "--"
  19920. ["Sort Region/Children" org-sort t]
  19921. "--"
  19922. ["Convert to odd levels" org-convert-to-odd-levels t]
  19923. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  19924. ("Editing"
  19925. ["Emphasis..." org-emphasize t]
  19926. ["Edit Source Example" org-edit-special t]
  19927. "--"
  19928. ["Footnote new/jump" org-footnote-action t]
  19929. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  19930. ("Archive"
  19931. ["Archive (default method)" org-archive-subtree-default (org-in-subtree-not-table-p)]
  19932. "--"
  19933. ["Move Subtree to Archive file" org-archive-subtree (org-in-subtree-not-table-p)]
  19934. ["Toggle ARCHIVE tag" org-toggle-archive-tag (org-in-subtree-not-table-p)]
  19935. ["Move subtree to Archive sibling" org-archive-to-archive-sibling (org-in-subtree-not-table-p)]
  19936. )
  19937. "--"
  19938. ("Hyperlinks"
  19939. ["Store Link (Global)" org-store-link t]
  19940. ["Find existing link to here" org-occur-link-in-agenda-files t]
  19941. ["Insert Link" org-insert-link t]
  19942. ["Follow Link" org-open-at-point t]
  19943. "--"
  19944. ["Next link" org-next-link t]
  19945. ["Previous link" org-previous-link t]
  19946. "--"
  19947. ["Descriptive Links"
  19948. org-toggle-link-display
  19949. :style radio
  19950. :selected org-descriptive-links
  19951. ]
  19952. ["Literal Links"
  19953. org-toggle-link-display
  19954. :style radio
  19955. :selected (not org-descriptive-links)])
  19956. "--"
  19957. ("TODO Lists"
  19958. ["TODO/DONE/-" org-todo t]
  19959. ("Select keyword"
  19960. ["Next keyword" org-shiftright (org-at-heading-p)]
  19961. ["Previous keyword" org-shiftleft (org-at-heading-p)]
  19962. ["Complete Keyword" pcomplete (assq :todo-keyword (org-context))]
  19963. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))]
  19964. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))])
  19965. ["Show TODO Tree" org-show-todo-tree :active t :keys "C-c / t"]
  19966. ["Global TODO list" org-todo-list :active t :keys "C-c a t"]
  19967. "--"
  19968. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  19969. :selected org-enforce-todo-dependencies :style toggle :active t]
  19970. "Settings for tree at point"
  19971. ["Do Children sequentially" org-toggle-ordered-property :style radio
  19972. :selected (org-entry-get nil "ORDERED")
  19973. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  19974. ["Do Children parallel" org-toggle-ordered-property :style radio
  19975. :selected (not (org-entry-get nil "ORDERED"))
  19976. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  19977. "--"
  19978. ["Set Priority" org-priority t]
  19979. ["Priority Up" org-shiftup t]
  19980. ["Priority Down" org-shiftdown t]
  19981. "--"
  19982. ["Get news from all feeds" org-feed-update-all t]
  19983. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  19984. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  19985. ("TAGS and Properties"
  19986. ["Set Tags" org-set-tags-command (not (org-before-first-heading-p))]
  19987. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  19988. "--"
  19989. ["Set property" org-set-property (not (org-before-first-heading-p))]
  19990. ["Column view of properties" org-columns t]
  19991. ["Insert Column View DBlock" org-columns-insert-dblock t])
  19992. ("Dates and Scheduling"
  19993. ["Timestamp" org-time-stamp (not (org-before-first-heading-p))]
  19994. ["Timestamp (inactive)" org-time-stamp-inactive (not (org-before-first-heading-p))]
  19995. ("Change Date"
  19996. ["1 Day Later" org-shiftright (org-at-timestamp-p)]
  19997. ["1 Day Earlier" org-shiftleft (org-at-timestamp-p)]
  19998. ["1 ... Later" org-shiftup (org-at-timestamp-p)]
  19999. ["1 ... Earlier" org-shiftdown (org-at-timestamp-p)])
  20000. ["Compute Time Range" org-evaluate-time-range t]
  20001. ["Schedule Item" org-schedule (not (org-before-first-heading-p))]
  20002. ["Deadline" org-deadline (not (org-before-first-heading-p))]
  20003. "--"
  20004. ["Custom time format" org-toggle-time-stamp-overlays
  20005. :style radio :selected org-display-custom-times]
  20006. "--"
  20007. ["Goto Calendar" org-goto-calendar t]
  20008. ["Date from Calendar" org-date-from-calendar t]
  20009. "--"
  20010. ["Start/Restart Timer" org-timer-start t]
  20011. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  20012. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  20013. ["Insert Timer String" org-timer t]
  20014. ["Insert Timer Item" org-timer-item t])
  20015. ("Logging work"
  20016. ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
  20017. ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
  20018. ["Clock out" org-clock-out t]
  20019. ["Clock cancel" org-clock-cancel t]
  20020. "--"
  20021. ["Mark as default task" org-clock-mark-default-task t]
  20022. ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
  20023. ["Goto running clock" org-clock-goto t]
  20024. "--"
  20025. ["Display times" org-clock-display t]
  20026. ["Create clock table" org-clock-report t]
  20027. "--"
  20028. ["Record DONE time"
  20029. (progn (setq org-log-done (not org-log-done))
  20030. (message "Switching to %s will %s record a timestamp"
  20031. (car org-done-keywords)
  20032. (if org-log-done "automatically" "not")))
  20033. :style toggle :selected org-log-done])
  20034. "--"
  20035. ["Agenda Command..." org-agenda t]
  20036. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  20037. ("File List for Agenda")
  20038. ("Special views current file"
  20039. ["TODO Tree" org-show-todo-tree t]
  20040. ["Check Deadlines" org-check-deadlines t]
  20041. ["Timeline" org-timeline t]
  20042. ["Tags/Property tree" org-match-sparse-tree t])
  20043. "--"
  20044. ["Export/Publish..." org-export-dispatch t]
  20045. ("LaTeX"
  20046. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  20047. :selected org-cdlatex-mode]
  20048. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  20049. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  20050. ["Modify math symbol" org-cdlatex-math-modify
  20051. (org-inside-LaTeX-fragment-p)]
  20052. ["Insert citation" org-reftex-citation t]
  20053. "--"
  20054. ["Template for BEAMER" (org-beamer-insert-options-template) t])
  20055. "--"
  20056. ("MobileOrg"
  20057. ["Push Files and Views" org-mobile-push t]
  20058. ["Get Captured and Flagged" org-mobile-pull t]
  20059. ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
  20060. "--"
  20061. ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
  20062. "--"
  20063. ("Documentation"
  20064. ["Show Version" org-version t]
  20065. ["Info Documentation" org-info t])
  20066. ("Customize"
  20067. ["Browse Org Group" org-customize t]
  20068. "--"
  20069. ["Expand This Menu" org-create-customize-menu
  20070. (fboundp 'customize-menu-create)])
  20071. ["Send bug report" org-submit-bug-report t]
  20072. "--"
  20073. ("Refresh/Reload"
  20074. ["Refresh setup current buffer" org-mode-restart t]
  20075. ["Reload Org (after update)" org-reload t]
  20076. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x !"])
  20077. ))
  20078. (defun org-info (&optional node)
  20079. "Read documentation for Org-mode in the info system.
  20080. With optional NODE, go directly to that node."
  20081. (interactive)
  20082. (info (format "(org)%s" (or node ""))))
  20083. ;;;###autoload
  20084. (defun org-submit-bug-report ()
  20085. "Submit a bug report on Org-mode via mail.
  20086. Don't hesitate to report any problems or inaccurate documentation.
  20087. If you don't have setup sending mail from (X)Emacs, please copy the
  20088. output buffer into your mail program, as it gives us important
  20089. information about your Org-mode version and configuration."
  20090. (interactive)
  20091. (require 'reporter)
  20092. (defvar reporter-prompt-for-summary-p)
  20093. (org-load-modules-maybe)
  20094. (org-require-autoloaded-modules)
  20095. (let ((reporter-prompt-for-summary-p "Bug report subject: "))
  20096. (reporter-submit-bug-report
  20097. "emacs-orgmode@gnu.org"
  20098. (org-version nil 'full)
  20099. (let (list)
  20100. (save-window-excursion
  20101. (org-pop-to-buffer-same-window (get-buffer-create "*Warn about privacy*"))
  20102. (delete-other-windows)
  20103. (erase-buffer)
  20104. (insert "You are about to submit a bug report to the Org-mode mailing list.
  20105. We would like to add your full Org-mode and Outline configuration to the
  20106. bug report. This greatly simplifies the work of the maintainer and
  20107. other experts on the mailing list.
  20108. HOWEVER, some variables you have customized may contain private
  20109. information. The names of customers, colleagues, or friends, might
  20110. appear in the form of file names, tags, todo states, or search strings.
  20111. If you answer yes to the prompt, you might want to check and remove
  20112. such private information before sending the email.")
  20113. (add-text-properties (point-min) (point-max) '(face org-warning))
  20114. (when (yes-or-no-p "Include your Org-mode configuration ")
  20115. (mapatoms
  20116. (lambda (v)
  20117. (and (boundp v)
  20118. (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
  20119. (or (and (symbol-value v)
  20120. (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
  20121. (and
  20122. (get v 'custom-type) (get v 'standard-value)
  20123. (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
  20124. (push v list)))))
  20125. (kill-buffer (get-buffer "*Warn about privacy*"))
  20126. list))
  20127. nil nil
  20128. "Remember to cover the basics, that is, what you expected to happen and
  20129. what in fact did happen. You don't know how to make a good report? See
  20130. http://orgmode.org/manual/Feedback.html#Feedback
  20131. Your bug report will be posted to the Org-mode mailing list.
  20132. ------------------------------------------------------------------------")
  20133. (save-excursion
  20134. (when (re-search-backward "^\\(Subject: \\)Org-mode version \\(.*?\\);[ \t]*\\(.*\\)" nil t)
  20135. (replace-match "\\1Bug: \\3 [\\2]")))))
  20136. (defun org-install-agenda-files-menu ()
  20137. (let ((bl (buffer-list)))
  20138. (save-excursion
  20139. (while bl
  20140. (set-buffer (pop bl))
  20141. (when (derived-mode-p 'org-mode) (setq bl nil)))
  20142. (when (derived-mode-p 'org-mode)
  20143. (easy-menu-change
  20144. '("Org") "File List for Agenda"
  20145. (append
  20146. (list
  20147. ["Edit File List" (org-edit-agenda-file-list) t]
  20148. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  20149. ["Remove Current File from List" org-remove-file t]
  20150. ["Cycle through agenda files" org-cycle-agenda-files t]
  20151. ["Occur in all agenda files" org-occur-in-agenda-files t]
  20152. "--")
  20153. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  20154. ;;;; Documentation
  20155. (defun org-require-autoloaded-modules ()
  20156. (interactive)
  20157. (mapc #'require
  20158. '(org-agenda org-archive org-attach org-clock org-colview org-id
  20159. org-table org-timer)))
  20160. ;;;###autoload
  20161. (defun org-reload (&optional uncompiled)
  20162. "Reload all org lisp files.
  20163. With prefix arg UNCOMPILED, load the uncompiled versions."
  20164. (interactive "P")
  20165. (require 'loadhist)
  20166. (let* ((org-dir (org-find-library-dir "org"))
  20167. (contrib-dir (or (org-find-library-dir "org-contribdir") org-dir))
  20168. (feature-re "^\\(org\\|ob\\|ox\\)\\(-.*\\)?")
  20169. (remove-re (mapconcat 'identity
  20170. (mapcar (lambda (f) (concat "^" f "$"))
  20171. (list (if (featurep 'xemacs)
  20172. "org-colview"
  20173. "org-colview-xemacs")
  20174. "org" "org-loaddefs" "org-version"))
  20175. "\\|"))
  20176. (feats (delete-dups
  20177. (mapcar 'file-name-sans-extension
  20178. (mapcar 'file-name-nondirectory
  20179. (delq nil
  20180. (mapcar 'feature-file
  20181. features))))))
  20182. (lfeat (append
  20183. (sort
  20184. (setq feats
  20185. (delq nil (mapcar
  20186. (lambda (f)
  20187. (if (and (string-match feature-re f)
  20188. (not (string-match remove-re f)))
  20189. f nil))
  20190. feats)))
  20191. 'string-lessp)
  20192. (list "org-version" "org")))
  20193. (load-suffixes (when (boundp 'load-suffixes) load-suffixes))
  20194. (load-suffixes (if uncompiled (reverse load-suffixes) load-suffixes))
  20195. load-uncore load-misses)
  20196. (setq load-misses
  20197. (delq 't
  20198. (mapcar (lambda (f)
  20199. (or (org-load-noerror-mustsuffix (concat org-dir f))
  20200. (and (string= org-dir contrib-dir)
  20201. (org-load-noerror-mustsuffix (concat contrib-dir f)))
  20202. (and (org-load-noerror-mustsuffix (concat (org-find-library-dir f) f))
  20203. (add-to-list 'load-uncore f 'append)
  20204. 't)
  20205. f))
  20206. lfeat)))
  20207. (when load-uncore
  20208. (message "The following feature%s found in load-path, please check if that's correct:\n%s"
  20209. (if (> (length load-uncore) 1) "s were" " was") load-uncore))
  20210. (if load-misses
  20211. (message "Some error occurred while reloading Org feature%s\n%s\nPlease check *Messages*!\n%s"
  20212. (if (> (length load-misses) 1) "s" "") load-misses (org-version nil 'full))
  20213. (message "Successfully reloaded Org\n%s" (org-version nil 'full)))))
  20214. ;;;###autoload
  20215. (defun org-customize ()
  20216. "Call the customize function with org as argument."
  20217. (interactive)
  20218. (org-load-modules-maybe)
  20219. (org-require-autoloaded-modules)
  20220. (customize-browse 'org))
  20221. (defun org-create-customize-menu ()
  20222. "Create a full customization menu for Org-mode, insert it into the menu."
  20223. (interactive)
  20224. (org-load-modules-maybe)
  20225. (org-require-autoloaded-modules)
  20226. (if (fboundp 'customize-menu-create)
  20227. (progn
  20228. (easy-menu-change
  20229. '("Org") "Customize"
  20230. `(["Browse Org group" org-customize t]
  20231. "--"
  20232. ,(customize-menu-create 'org)
  20233. ["Set" Custom-set t]
  20234. ["Save" Custom-save t]
  20235. ["Reset to Current" Custom-reset-current t]
  20236. ["Reset to Saved" Custom-reset-saved t]
  20237. ["Reset to Standard Settings" Custom-reset-standard t]))
  20238. (message "\"Org\"-menu now contains full customization menu"))
  20239. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  20240. ;;;; Miscellaneous stuff
  20241. ;;; Generally useful functions
  20242. (defun org-get-at-eol (property n)
  20243. "Get text property PROPERTY at the end of line less N characters."
  20244. (get-text-property (- (point-at-eol) n) property))
  20245. (defun org-find-text-property-in-string (prop s)
  20246. "Return the first non-nil value of property PROP in string S."
  20247. (or (get-text-property 0 prop s)
  20248. (get-text-property (or (next-single-property-change 0 prop s) 0)
  20249. prop s)))
  20250. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  20251. "Display the given MESSAGE as a warning."
  20252. (if (fboundp 'display-warning)
  20253. (display-warning 'org message
  20254. (if (featurep 'xemacs) 'warning :warning))
  20255. (let ((buf (get-buffer-create "*Org warnings*")))
  20256. (with-current-buffer buf
  20257. (goto-char (point-max))
  20258. (insert "Warning (Org): " message)
  20259. (unless (bolp)
  20260. (newline)))
  20261. (display-buffer buf)
  20262. (sit-for 0))))
  20263. (defun org-eval (form)
  20264. "Eval FORM and return result."
  20265. (condition-case error
  20266. (eval form)
  20267. (error (format "%%![Error: %s]" error))))
  20268. (defun org-in-clocktable-p ()
  20269. "Check if the cursor is in a clocktable."
  20270. (let ((pos (point)) start)
  20271. (save-excursion
  20272. (end-of-line 1)
  20273. (and (re-search-backward "^[ \t]*#\\+BEGIN:[ \t]+clocktable" nil t)
  20274. (setq start (match-beginning 0))
  20275. (re-search-forward "^[ \t]*#\\+END:.*" nil t)
  20276. (>= (match-end 0) pos)
  20277. start))))
  20278. (defun org-in-verbatim-emphasis ()
  20279. (save-match-data
  20280. (and (org-in-regexp org-emph-re 2)
  20281. (>= (point) (match-beginning 3))
  20282. (<= (point) (match-end 4))
  20283. (member (match-string 3) '("=" "~")))))
  20284. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  20285. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  20286. (if (and marker (marker-buffer marker)
  20287. (buffer-live-p (marker-buffer marker)))
  20288. (progn
  20289. (org-pop-to-buffer-same-window (marker-buffer marker))
  20290. (when (or (> marker (point-max)) (< marker (point-min)))
  20291. (widen))
  20292. (goto-char marker)
  20293. (org-show-context 'org-goto))
  20294. (if bookmark
  20295. (bookmark-jump bookmark)
  20296. (error "Cannot find location"))))
  20297. (defun org-quote-csv-field (s)
  20298. "Quote field for inclusion in CSV material."
  20299. (if (string-match "[\",]" s)
  20300. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  20301. s))
  20302. (defun org-force-self-insert (N)
  20303. "Needed to enforce self-insert under remapping."
  20304. (interactive "p")
  20305. (self-insert-command N))
  20306. (defun org-string-width (s)
  20307. "Compute width of string, ignoring invisible characters.
  20308. This ignores character with invisibility property `org-link', and also
  20309. characters with property `org-cwidth', because these will become invisible
  20310. upon the next fontification round."
  20311. (let (b l)
  20312. (when (or (eq t buffer-invisibility-spec)
  20313. (assq 'org-link buffer-invisibility-spec))
  20314. (while (setq b (text-property-any 0 (length s)
  20315. 'invisible 'org-link s))
  20316. (setq s (concat (substring s 0 b)
  20317. (substring s (or (next-single-property-change
  20318. b 'invisible s)
  20319. (length s)))))))
  20320. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  20321. (setq s (concat (substring s 0 b)
  20322. (substring s (or (next-single-property-change
  20323. b 'org-cwidth s)
  20324. (length s))))))
  20325. (setq l (string-width s) b -1)
  20326. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  20327. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  20328. l))
  20329. (defun org-shorten-string (s maxlength)
  20330. "Shorten string S so tht it is no longer than MAXLENGTH characters.
  20331. If the string is shorter or has length MAXLENGTH, just return the
  20332. original string. If it is longer, the functions finds a space in the
  20333. string, breaks this string off at that locations and adds three dots
  20334. as ellipsis. Including the ellipsis, the string will not be longer
  20335. than MAXLENGTH. If finding a good breaking point in the string does
  20336. not work, the string is just chopped off in the middle of a word
  20337. if necessary."
  20338. (if (<= (length s) maxlength)
  20339. s
  20340. (let* ((n (max (- maxlength 4) 1))
  20341. (re (concat "\\`\\(.\\{1," (int-to-string n) "\\}[^ ]\\)\\([ ]\\|\\'\\)")))
  20342. (if (string-match re s)
  20343. (concat (match-string 1 s) "...")
  20344. (concat (substring s 0 (max (- maxlength 3) 0)) "...")))))
  20345. (defun org-get-indentation (&optional line)
  20346. "Get the indentation of the current line, interpreting tabs.
  20347. When LINE is given, assume it represents a line and compute its indentation."
  20348. (if line
  20349. (when (string-match "^ *" (org-remove-tabs line))
  20350. (match-end 0))
  20351. (save-excursion
  20352. (beginning-of-line 1)
  20353. (skip-chars-forward " \t")
  20354. (current-column))))
  20355. (defun org-get-string-indentation (s)
  20356. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  20357. (let ((n -1) (i 0) (w tab-width) c)
  20358. (catch 'exit
  20359. (while (< (setq n (1+ n)) (length s))
  20360. (setq c (aref s n))
  20361. (cond ((= c ?\ ) (setq i (1+ i)))
  20362. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  20363. (t (throw 'exit t)))))
  20364. i))
  20365. (defun org-remove-tabs (s &optional width)
  20366. "Replace tabulators in S with spaces.
  20367. Assumes that s is a single line, starting in column 0."
  20368. (setq width (or width tab-width))
  20369. (while (string-match "\t" s)
  20370. (setq s (replace-match
  20371. (make-string
  20372. (- (* width (/ (+ (match-beginning 0) width) width))
  20373. (match-beginning 0)) ?\ )
  20374. t t s)))
  20375. s)
  20376. (defun org-fix-indentation (line ind)
  20377. "Fix indentation in LINE.
  20378. IND is a cons cell with target and minimum indentation.
  20379. If the current indentation in LINE is smaller than the minimum,
  20380. leave it alone. If it is larger than ind, set it to the target."
  20381. (let* ((l (org-remove-tabs line))
  20382. (i (org-get-indentation l))
  20383. (i1 (car ind)) (i2 (cdr ind)))
  20384. (when (>= i i2) (setq l (substring line i2)))
  20385. (if (> i1 0)
  20386. (concat (make-string i1 ?\ ) l)
  20387. l)))
  20388. (defun org-remove-indentation (code &optional n)
  20389. "Remove maximum common indentation in string CODE and return it.
  20390. N may optionally be the number of columns to remove. Return CODE
  20391. as-is if removal failed."
  20392. (with-temp-buffer
  20393. (insert code)
  20394. (if (org-do-remove-indentation n) (buffer-string) code)))
  20395. (defun org-do-remove-indentation (&optional n)
  20396. "Remove the maximum common indentation from the buffer.
  20397. When optional argument N is a positive integer, remove exactly
  20398. that much characters from indentation, if possible. Return nil
  20399. if it fails."
  20400. (catch :exit
  20401. (goto-char (point-min))
  20402. ;; Find maximum common indentation, if not specified.
  20403. (let ((n (or n
  20404. (let ((min-ind (point-max)))
  20405. (save-excursion
  20406. (while (re-search-forward "^[ \t]*\\S-" nil t)
  20407. (let ((ind (1- (current-column))))
  20408. (if (zerop ind) (throw :exit nil)
  20409. (setq min-ind (min min-ind ind))))))
  20410. min-ind))))
  20411. (if (zerop n) (throw :exit nil)
  20412. ;; Remove exactly N indentation, but give up if not possible.
  20413. (while (not (eobp))
  20414. (let ((ind (progn (skip-chars-forward " \t") (current-column))))
  20415. (cond ((eolp) (delete-region (line-beginning-position) (point)))
  20416. ((< ind n) (throw :exit nil))
  20417. (t (org-indent-line-to (- ind n))))
  20418. (forward-line)))
  20419. ;; Signal success.
  20420. t))))
  20421. (defun org-fill-template (template alist)
  20422. "Find each %key of ALIST in TEMPLATE and replace it."
  20423. (let ((case-fold-search nil))
  20424. (dolist (entry (sort (copy-sequence alist)
  20425. (lambda (a b) (< (length (car a)) (length (car b))))))
  20426. (setq template
  20427. (replace-regexp-in-string
  20428. (concat "%" (regexp-quote (car entry)))
  20429. (or (cdr entry) "") template t t)))
  20430. template))
  20431. (defun org-base-buffer (buffer)
  20432. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  20433. (if (not buffer)
  20434. buffer
  20435. (or (buffer-base-buffer buffer)
  20436. buffer)))
  20437. (defun org-wrap (string &optional width lines)
  20438. "Wrap string to either a number of lines, or a width in characters.
  20439. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  20440. that costs. If there is a word longer than WIDTH, the text is actually
  20441. wrapped to the length of that word.
  20442. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  20443. many lines, whatever width that takes.
  20444. The return value is a list of lines, without newlines at the end."
  20445. (let* ((words (org-split-string string "[ \t\n]+"))
  20446. (maxword (apply 'max (mapcar 'org-string-width words)))
  20447. w ll)
  20448. (cond (width
  20449. (org-do-wrap words (max maxword width)))
  20450. (lines
  20451. (setq w maxword)
  20452. (setq ll (org-do-wrap words maxword))
  20453. (if (<= (length ll) lines)
  20454. ll
  20455. (setq ll words)
  20456. (while (> (length ll) lines)
  20457. (setq w (1+ w))
  20458. (setq ll (org-do-wrap words w)))
  20459. ll))
  20460. (t (error "Cannot wrap this")))))
  20461. (defun org-do-wrap (words width)
  20462. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  20463. (let (lines line)
  20464. (while words
  20465. (setq line (pop words))
  20466. (while (and words (< (+ (length line) (length (car words))) width))
  20467. (setq line (concat line " " (pop words))))
  20468. (setq lines (push line lines)))
  20469. (nreverse lines)))
  20470. (defun org-split-string (string &optional separators)
  20471. "Splits STRING into substrings at SEPARATORS.
  20472. SEPARATORS is a regular expression.
  20473. No empty strings are returned if there are matches at the beginning
  20474. and end of string."
  20475. ;; FIXME: why not use (split-string STRING SEPARATORS t)?
  20476. (let ((start 0) notfirst list)
  20477. (while (and (string-match (or separators "[ \f\t\n\r\v]+") string
  20478. (if (and notfirst
  20479. (= start (match-beginning 0))
  20480. (< start (length string)))
  20481. (1+ start) start))
  20482. (< (match-beginning 0) (length string)))
  20483. (setq notfirst t)
  20484. (or (eq (match-beginning 0) 0)
  20485. (and (eq (match-beginning 0) (match-end 0))
  20486. (eq (match-beginning 0) start))
  20487. (push (substring string start (match-beginning 0)) list))
  20488. (setq start (match-end 0)))
  20489. (or (eq start (length string))
  20490. (push (substring string start) list))
  20491. (nreverse list)))
  20492. (defun org-quote-vert (s)
  20493. "Replace \"|\" with \"\\vert\"."
  20494. (while (string-match "|" s)
  20495. (setq s (replace-match "\\vert" t t s)))
  20496. s)
  20497. (defun org-uuidgen-p (s)
  20498. "Is S an ID created by UUIDGEN?"
  20499. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  20500. (defun org-in-src-block-p (&optional inside)
  20501. "Whether point is in a code source block.
  20502. When INSIDE is non-nil, don't consider we are within a src block
  20503. when point is at #+BEGIN_SRC or #+END_SRC."
  20504. (let ((case-fold-search t))
  20505. (or (and (eq (get-char-property (point) 'src-block) t))
  20506. (and (not inside)
  20507. (save-match-data
  20508. (save-excursion
  20509. (beginning-of-line)
  20510. (looking-at ".*#\\+\\(begin\\|end\\)_src")))))))
  20511. (defun org-context ()
  20512. "Return a list of contexts of the current cursor position.
  20513. If several contexts apply, all are returned.
  20514. Each context entry is a list with a symbol naming the context, and
  20515. two positions indicating start and end of the context. Possible
  20516. contexts are:
  20517. :headline anywhere in a headline
  20518. :headline-stars on the leading stars in a headline
  20519. :todo-keyword on a TODO keyword (including DONE) in a headline
  20520. :tags on the TAGS in a headline
  20521. :priority on the priority cookie in a headline
  20522. :item on the first line of a plain list item
  20523. :item-bullet on the bullet/number of a plain list item
  20524. :checkbox on the checkbox in a plain list item
  20525. :table in an org-mode table
  20526. :table-special on a special filed in a table
  20527. :table-table in a table.el table
  20528. :clocktable in a clocktable
  20529. :src-block in a source block
  20530. :link on a hyperlink
  20531. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE, COMMENT.
  20532. :target on a <<target>>
  20533. :radio-target on a <<<radio-target>>>
  20534. :latex-fragment on a LaTeX fragment
  20535. :latex-preview on a LaTeX fragment with overlaid preview image
  20536. This function expects the position to be visible because it uses font-lock
  20537. faces as a help to recognize the following contexts: :table-special, :link,
  20538. and :keyword."
  20539. (let* ((f (get-text-property (point) 'face))
  20540. (faces (if (listp f) f (list f)))
  20541. (case-fold-search t)
  20542. (p (point)) clist o)
  20543. ;; First the large context
  20544. (cond
  20545. ((org-at-heading-p t)
  20546. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  20547. (when (progn
  20548. (beginning-of-line 1)
  20549. (looking-at org-todo-line-tags-regexp))
  20550. (push (org-point-in-group p 1 :headline-stars) clist)
  20551. (push (org-point-in-group p 2 :todo-keyword) clist)
  20552. (push (org-point-in-group p 4 :tags) clist))
  20553. (goto-char p)
  20554. (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
  20555. (when (looking-at "\\[#[A-Z0-9]\\]")
  20556. (push (org-point-in-group p 0 :priority) clist)))
  20557. ((org-at-item-p)
  20558. (push (org-point-in-group p 2 :item-bullet) clist)
  20559. (push (list :item (point-at-bol)
  20560. (save-excursion (org-end-of-item) (point)))
  20561. clist)
  20562. (and (org-at-item-checkbox-p)
  20563. (push (org-point-in-group p 0 :checkbox) clist)))
  20564. ((org-at-table-p)
  20565. (push (list :table (org-table-begin) (org-table-end)) clist)
  20566. (when (memq 'org-formula faces)
  20567. (push (list :table-special
  20568. (previous-single-property-change p 'face)
  20569. (next-single-property-change p 'face)) clist)))
  20570. ((org-at-table-p 'any)
  20571. (push (list :table-table) clist)))
  20572. (goto-char p)
  20573. (let ((case-fold-search t))
  20574. ;; New the "medium" contexts: clocktables, source blocks
  20575. (cond ((org-in-clocktable-p)
  20576. (push (list :clocktable
  20577. (and (or (looking-at "[ \t]*\\(#\\+BEGIN: clocktable\\)")
  20578. (re-search-backward "[ \t]*\\(#+BEGIN: clocktable\\)" nil t))
  20579. (match-beginning 1))
  20580. (and (re-search-forward "[ \t]*#\\+END:?" nil t)
  20581. (match-end 0))) clist))
  20582. ((org-in-src-block-p)
  20583. (push (list :src-block
  20584. (and (or (looking-at "[ \t]*\\(#\\+BEGIN_SRC\\)")
  20585. (re-search-backward "[ \t]*\\(#+BEGIN_SRC\\)" nil t))
  20586. (match-beginning 1))
  20587. (and (search-forward "#+END_SRC" nil t)
  20588. (match-beginning 0))) clist))))
  20589. (goto-char p)
  20590. ;; Now the small context
  20591. (cond
  20592. ((org-at-timestamp-p)
  20593. (push (org-point-in-group p 0 :timestamp) clist))
  20594. ((memq 'org-link faces)
  20595. (push (list :link
  20596. (previous-single-property-change p 'face)
  20597. (next-single-property-change p 'face)) clist))
  20598. ((memq 'org-special-keyword faces)
  20599. (push (list :keyword
  20600. (previous-single-property-change p 'face)
  20601. (next-single-property-change p 'face)) clist))
  20602. ((org-at-target-p)
  20603. (push (org-point-in-group p 0 :target) clist)
  20604. (goto-char (1- (match-beginning 0)))
  20605. (when (looking-at org-radio-target-regexp)
  20606. (push (org-point-in-group p 0 :radio-target) clist))
  20607. (goto-char p))
  20608. ((setq o (cl-some
  20609. (lambda (o)
  20610. (and (eq (overlay-get o 'org-overlay-type) 'org-latex-overlay)
  20611. o))
  20612. (overlays-at (point))))
  20613. (push (list :latex-fragment
  20614. (overlay-start o) (overlay-end o)) clist)
  20615. (push (list :latex-preview
  20616. (overlay-start o) (overlay-end o)) clist))
  20617. ((org-inside-LaTeX-fragment-p)
  20618. ;; FIXME: positions wrong.
  20619. (push (list :latex-fragment (point) (point)) clist)))
  20620. (setq clist (nreverse (delq nil clist)))
  20621. clist))
  20622. (defun org-in-regexp (regexp &optional nlines visually)
  20623. "Check if point is inside a match of REGEXP.
  20624. Normally only the current line is checked, but you can include
  20625. NLINES extra lines around point into the search. If VISUALLY is
  20626. set, require that the cursor is not after the match but really
  20627. on, so that the block visually is on the match.
  20628. Return nil or a cons cell (BEG . END) where BEG and END are,
  20629. respectively, the positions at the beginning and the end of the
  20630. match."
  20631. (catch :exit
  20632. (let ((pos (point))
  20633. (eol (line-end-position (if nlines (1+ nlines) 1))))
  20634. (save-excursion
  20635. (beginning-of-line (- 1 (or nlines 0)))
  20636. (while (and (re-search-forward regexp eol t)
  20637. (<= (match-beginning 0) pos))
  20638. (let ((end (match-end 0)))
  20639. (when (or (> end pos) (and (= end pos) (not visually)))
  20640. (throw :exit (cons (match-beginning 0) (match-end 0))))))))))
  20641. (define-obsolete-function-alias 'org-at-regexp-p 'org-in-regexp
  20642. "Org mode 8.3")
  20643. (defun org-between-regexps-p (start-re end-re &optional lim-up lim-down)
  20644. "Non-nil when point is between matches of START-RE and END-RE.
  20645. Also return a non-nil value when point is on one of the matches.
  20646. Optional arguments LIM-UP and LIM-DOWN bound the search; they are
  20647. buffer positions. Default values are the positions of headlines
  20648. surrounding the point.
  20649. The functions returns a cons cell whose car (resp. cdr) is the
  20650. position before START-RE (resp. after END-RE)."
  20651. (save-match-data
  20652. (let ((pos (point))
  20653. (limit-up (or lim-up (save-excursion (outline-previous-heading))))
  20654. (limit-down (or lim-down (save-excursion (outline-next-heading))))
  20655. beg end)
  20656. (save-excursion
  20657. ;; Point is on a block when on START-RE or if START-RE can be
  20658. ;; found before it...
  20659. (and (or (org-in-regexp start-re)
  20660. (re-search-backward start-re limit-up t))
  20661. (setq beg (match-beginning 0))
  20662. ;; ... and END-RE after it...
  20663. (goto-char (match-end 0))
  20664. (re-search-forward end-re limit-down t)
  20665. (> (setq end (match-end 0)) pos)
  20666. ;; ... without another START-RE in-between.
  20667. (goto-char (match-beginning 0))
  20668. (not (re-search-backward start-re (1+ beg) t))
  20669. ;; Return value.
  20670. (cons beg end))))))
  20671. (defun org-in-block-p (names)
  20672. "Non-nil when point belongs to a block whose name belongs to NAMES.
  20673. NAMES is a list of strings containing names of blocks.
  20674. Return first block name matched, or nil. Beware that in case of
  20675. nested blocks, the returned name may not belong to the closest
  20676. block from point."
  20677. (save-match-data
  20678. (catch 'exit
  20679. (let ((case-fold-search t)
  20680. (lim-up (save-excursion (outline-previous-heading)))
  20681. (lim-down (save-excursion (outline-next-heading))))
  20682. (dolist (name names)
  20683. (let ((n (regexp-quote name)))
  20684. (when (org-between-regexps-p
  20685. (concat "^[ \t]*#\\+begin_" n)
  20686. (concat "^[ \t]*#\\+end_" n)
  20687. lim-up lim-down)
  20688. (throw 'exit n)))))
  20689. nil)))
  20690. (defun org-occur-in-agenda-files (regexp &optional _nlines)
  20691. "Call `multi-occur' with buffers for all agenda files."
  20692. (interactive "sOrg-files matching: ")
  20693. (let* ((files (org-agenda-files))
  20694. (tnames (mapcar #'file-truename files))
  20695. (extra org-agenda-text-search-extra-files))
  20696. (when (eq (car extra) 'agenda-archives)
  20697. (setq extra (cdr extra))
  20698. (setq files (org-add-archive-files files)))
  20699. (dolist (f extra)
  20700. (unless (member (file-truename f) tnames)
  20701. (unless (member f files) (setq files (append files (list f))))
  20702. (setq tnames (append tnames (list (file-truename f))))))
  20703. (multi-occur
  20704. (mapcar (lambda (x)
  20705. (with-current-buffer
  20706. ;; FIXME: Why not just (find-file-noselect x)?
  20707. ;; Is it to avoid the "revert buffer" prompt?
  20708. (or (get-file-buffer x) (find-file-noselect x))
  20709. (widen)
  20710. (current-buffer)))
  20711. files)
  20712. regexp)))
  20713. (if (boundp 'occur-mode-find-occurrence-hook)
  20714. ;; Emacs 23
  20715. (add-hook 'occur-mode-find-occurrence-hook
  20716. (lambda ()
  20717. (when (derived-mode-p 'org-mode)
  20718. (org-reveal))))
  20719. ;; Emacs 22
  20720. (defadvice occur-mode-goto-occurrence
  20721. (after org-occur-reveal activate)
  20722. (and (derived-mode-p 'org-mode) (org-reveal)))
  20723. (defadvice occur-mode-goto-occurrence-other-window
  20724. (after org-occur-reveal activate)
  20725. (and (derived-mode-p 'org-mode) (org-reveal)))
  20726. (defadvice occur-mode-display-occurrence
  20727. (after org-occur-reveal activate)
  20728. (when (derived-mode-p 'org-mode)
  20729. (let ((pos (occur-mode-find-occurrence)))
  20730. (with-current-buffer (marker-buffer pos)
  20731. (save-excursion
  20732. (goto-char pos)
  20733. (org-reveal)))))))
  20734. (defun org-occur-link-in-agenda-files ()
  20735. "Create a link and search for it in the agendas.
  20736. The link is not stored in `org-stored-links', it is just created
  20737. for the search purpose."
  20738. (interactive)
  20739. (let ((link (condition-case nil
  20740. (org-store-link nil)
  20741. (error "Unable to create a link to here"))))
  20742. (org-occur-in-agenda-files (regexp-quote link))))
  20743. (defun org-reverse-string (string)
  20744. "Return the reverse of STRING."
  20745. (apply 'string (reverse (string-to-list string))))
  20746. ;; defsubst org-uniquify must be defined before first use
  20747. (defun org-uniquify-alist (alist)
  20748. "Merge elements of ALIST with the same key.
  20749. For example, in this alist:
  20750. \(org-uniquify-alist \\='((a 1) (b 2) (a 3)))
  20751. => \\='((a 1 3) (b 2))
  20752. merge (a 1) and (a 3) into (a 1 3).
  20753. The function returns the new ALIST."
  20754. (let (rtn)
  20755. (dolist (e alist rtn)
  20756. (let (n)
  20757. (if (not (assoc (car e) rtn))
  20758. (push e rtn)
  20759. (setq n (cons (car e) (append (cdr (assoc (car e) rtn)) (cdr e))))
  20760. (setq rtn (assq-delete-all (car e) rtn))
  20761. (push n rtn))))))
  20762. (defun org-delete-all (elts list)
  20763. "Remove all elements in ELTS from LIST."
  20764. (while elts
  20765. (setq list (delete (pop elts) list)))
  20766. list)
  20767. (defun org-back-over-empty-lines ()
  20768. "Move backwards over whitespace, to the beginning of the first empty line.
  20769. Returns the number of empty lines passed."
  20770. (let ((pos (point)))
  20771. (if (cdr (assoc 'heading org-blank-before-new-entry))
  20772. (skip-chars-backward " \t\n\r")
  20773. (unless (eobp)
  20774. (forward-line -1)))
  20775. (beginning-of-line 2)
  20776. (goto-char (min (point) pos))
  20777. (count-lines (point) pos)))
  20778. (defun org-skip-whitespace ()
  20779. (skip-chars-forward " \t\n\r"))
  20780. (defun org-point-in-group (point group &optional context)
  20781. "Check if POINT is in match-group GROUP.
  20782. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  20783. match. If the match group does not exist or point is not inside it,
  20784. return nil."
  20785. (and (match-beginning group)
  20786. (>= point (match-beginning group))
  20787. (<= point (match-end group))
  20788. (if context
  20789. (list context (match-beginning group) (match-end group))
  20790. t)))
  20791. (defun org-switch-to-buffer-other-window (&rest args)
  20792. "Switch to buffer in a second window on the current frame.
  20793. In particular, do not allow pop-up frames.
  20794. Returns the newly created buffer."
  20795. (org-no-popups
  20796. (apply 'switch-to-buffer-other-window args)))
  20797. (defun org-combine-plists (&rest plists)
  20798. "Create a single property list from all plists in PLISTS.
  20799. The process starts by copying the first list, and then setting properties
  20800. from the other lists. Settings in the last list are the most significant
  20801. ones and overrule settings in the other lists."
  20802. (let ((rtn (copy-sequence (pop plists)))
  20803. p v ls)
  20804. (while plists
  20805. (setq ls (pop plists))
  20806. (while ls
  20807. (setq p (pop ls) v (pop ls))
  20808. (setq rtn (plist-put rtn p v))))
  20809. rtn))
  20810. (defun org-replace-escapes (string table)
  20811. "Replace %-escapes in STRING with values in TABLE.
  20812. TABLE is an association list with keys like \"%a\" and string values.
  20813. The sequences in STRING may contain normal field width and padding information,
  20814. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  20815. so values can contain further %-escapes if they are define later in TABLE."
  20816. (let ((tbl (copy-alist table))
  20817. (case-fold-search nil)
  20818. (pchg 0)
  20819. re rpl)
  20820. (dolist (e tbl)
  20821. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  20822. (when (and (cdr e) (string-match re (cdr e)))
  20823. (let ((sref (substring (cdr e) (match-beginning 0) (match-end 0)))
  20824. (safe "SREF"))
  20825. (add-text-properties 0 3 (list 'sref sref) safe)
  20826. (setcdr e (replace-match safe t t (cdr e)))))
  20827. (while (string-match re string)
  20828. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  20829. (cdr e)))
  20830. (setq string (replace-match rpl t t string))))
  20831. (while (setq pchg (next-property-change pchg string))
  20832. (let ((sref (get-text-property pchg 'sref string)))
  20833. (when (and sref (string-match "SREF" string pchg))
  20834. (setq string (replace-match sref t t string)))))
  20835. string))
  20836. (defun org-sublist (list start end)
  20837. "Return a section of LIST, from START to END.
  20838. Counting starts at 1."
  20839. (cl-subseq list (1- start) end))
  20840. (make-obsolete 'org-sublist "cl-subseq (note the 0-based counting)." "Org 9.0")
  20841. (defun org-find-base-buffer-visiting (file)
  20842. "Like `find-buffer-visiting' but always return the base buffer and
  20843. not an indirect buffer."
  20844. (let ((buf (or (get-file-buffer file)
  20845. (find-buffer-visiting file))))
  20846. (if buf
  20847. (or (buffer-base-buffer buf) buf)
  20848. nil)))
  20849. (define-obsolete-function-alias 'org-image-file-name-regexp 'image-file-name-regexp
  20850. "Org 9.0")
  20851. ;;; TODO: Only called once, from ox-odt which should probably use
  20852. ;;; org-export-inline-image-p or something.
  20853. (defun org-file-image-p (file)
  20854. "Return non-nil if FILE is an image."
  20855. (save-match-data
  20856. (string-match (image-file-name-regexp) file)))
  20857. (defun org-get-cursor-date (&optional with-time)
  20858. "Return the date at cursor in as a time.
  20859. This works in the calendar and in the agenda, anywhere else it just
  20860. returns the current time.
  20861. If WITH-TIME is non-nil, returns the time of the event at point (in
  20862. the agenda) or the current time of the day."
  20863. (let (date day defd tp hod mod)
  20864. (when with-time
  20865. (setq tp (get-text-property (point) 'time))
  20866. (when (and tp (string-match "\\([0-9][0-9]\\):\\([0-9][0-9]\\)" tp))
  20867. (setq hod (string-to-number (match-string 1 tp))
  20868. mod (string-to-number (match-string 2 tp))))
  20869. (or tp (let ((now (decode-time)))
  20870. (setq hod (nth 2 now)
  20871. mod (nth 1 now)))))
  20872. (cond
  20873. ((eq major-mode 'calendar-mode)
  20874. (setq date (calendar-cursor-to-date)
  20875. defd (encode-time 0 (or mod 0) (or hod 0)
  20876. (nth 1 date) (nth 0 date) (nth 2 date))))
  20877. ((eq major-mode 'org-agenda-mode)
  20878. (setq day (get-text-property (point) 'day))
  20879. (when day
  20880. (setq date (calendar-gregorian-from-absolute day)
  20881. defd (encode-time 0 (or mod 0) (or hod 0)
  20882. (nth 1 date) (nth 0 date) (nth 2 date))))))
  20883. (or defd (current-time))))
  20884. (defun org-mark-subtree (&optional up)
  20885. "Mark the current subtree.
  20886. This puts point at the start of the current subtree, and mark at
  20887. the end. If a numeric prefix UP is given, move up into the
  20888. hierarchy of headlines by UP levels before marking the subtree."
  20889. (interactive "P")
  20890. (org-with-limited-levels
  20891. (cond ((org-at-heading-p) (beginning-of-line))
  20892. ((org-before-first-heading-p) (user-error "Not in a subtree"))
  20893. (t (outline-previous-visible-heading 1))))
  20894. (when up (while (and (> up 0) (org-up-heading-safe)) (cl-decf up)))
  20895. (if (org-called-interactively-p 'any)
  20896. (call-interactively 'org-mark-element)
  20897. (org-mark-element)))
  20898. ;;; Indentation
  20899. (defun org--get-expected-indentation (element contentsp)
  20900. "Expected indentation column for current line, according to ELEMENT.
  20901. ELEMENT is an element containing point. CONTENTSP is non-nil
  20902. when indentation is to be computed according to contents of
  20903. ELEMENT."
  20904. (let ((type (org-element-type element))
  20905. (start (org-element-property :begin element))
  20906. (post-affiliated (org-element-property :post-affiliated element)))
  20907. (org-with-wide-buffer
  20908. (cond
  20909. (contentsp
  20910. (cl-case type
  20911. ((diary-sexp footnote-definition) 0)
  20912. ((headline inlinetask nil)
  20913. (if (not org-adapt-indentation) 0
  20914. (let ((level (org-current-level)))
  20915. (if level (1+ level) 0))))
  20916. ((item plain-list) (org-list-item-body-column post-affiliated))
  20917. (t
  20918. (goto-char start)
  20919. (org-get-indentation))))
  20920. ((memq type '(headline inlinetask nil))
  20921. (if (save-excursion (beginning-of-line) (looking-at "[ \t]*$"))
  20922. (org--get-expected-indentation element t)
  20923. 0))
  20924. ((memq type '(diary-sexp footnote-definition)) 0)
  20925. ;; First paragraph of a footnote definition or an item.
  20926. ;; Indent like parent.
  20927. ((< (line-beginning-position) start)
  20928. (org--get-expected-indentation
  20929. (org-element-property :parent element) t))
  20930. ;; At first line: indent according to previous sibling, if any,
  20931. ;; ignoring footnote definitions and inline tasks, or parent's
  20932. ;; contents.
  20933. ((= (line-beginning-position) start)
  20934. (catch 'exit
  20935. (while t
  20936. (if (= (point-min) start) (throw 'exit 0)
  20937. (goto-char (1- start))
  20938. (let* ((previous (org-element-at-point))
  20939. (parent previous))
  20940. (while (and parent (<= (org-element-property :end parent) start))
  20941. (setq previous parent
  20942. parent (org-element-property :parent parent)))
  20943. (cond
  20944. ((not previous) (throw 'exit 0))
  20945. ((> (org-element-property :end previous) start)
  20946. (throw 'exit (org--get-expected-indentation previous t)))
  20947. ((memq (org-element-type previous)
  20948. '(footnote-definition inlinetask))
  20949. (setq start (org-element-property :begin previous)))
  20950. (t (goto-char (org-element-property :begin previous))
  20951. (throw 'exit
  20952. (if (bolp) (org-get-indentation)
  20953. ;; At first paragraph in an item or
  20954. ;; a footnote definition.
  20955. (org--get-expected-indentation
  20956. (org-element-property :parent previous) t))))))))))
  20957. ;; Otherwise, move to the first non-blank line above.
  20958. (t
  20959. (beginning-of-line)
  20960. (let ((pos (point)))
  20961. (skip-chars-backward " \r\t\n")
  20962. (cond
  20963. ;; Two blank lines end a footnote definition or a plain
  20964. ;; list. When we indent an empty line after them, the
  20965. ;; containing list or footnote definition is over, so it
  20966. ;; qualifies as a previous sibling. Therefore, we indent
  20967. ;; like its first line.
  20968. ((and (memq type '(footnote-definition plain-list))
  20969. (> (count-lines (point) pos) 2))
  20970. (goto-char start)
  20971. (org-get-indentation))
  20972. ;; Line above is the first one of a paragraph at the
  20973. ;; beginning of an item or a footnote definition. Indent
  20974. ;; like parent.
  20975. ((< (line-beginning-position) start)
  20976. (org--get-expected-indentation
  20977. (org-element-property :parent element) t))
  20978. ;; Line above is the beginning of an element, i.e., point
  20979. ;; was originally on the blank lines between element's start
  20980. ;; and contents.
  20981. ((= (line-beginning-position) post-affiliated)
  20982. (org--get-expected-indentation element t))
  20983. ;; POS is after contents in a greater element. Indent like
  20984. ;; the beginning of the element.
  20985. ;;
  20986. ;; As a special case, if point is at the end of a footnote
  20987. ;; definition or an item, indent like the very last element
  20988. ;; within.
  20989. ((and (not (eq type 'paragraph))
  20990. (let ((cend (org-element-property :contents-end element)))
  20991. (and cend (<= cend pos))))
  20992. (if (memq type '(footnote-definition item plain-list))
  20993. (org--get-expected-indentation (org-element-at-point) nil)
  20994. (goto-char start)
  20995. (org-get-indentation)))
  20996. ;; In any other case, indent like the current line.
  20997. (t (org-get-indentation)))))))))
  20998. (defun org--align-node-property ()
  20999. "Align node property at point.
  21000. Alignment is done according to `org-property-format', which see."
  21001. (when (save-excursion
  21002. (beginning-of-line)
  21003. (looking-at org-property-re))
  21004. (replace-match
  21005. (concat (match-string 4)
  21006. (org-trim
  21007. (format org-property-format (match-string 1) (match-string 3))))
  21008. t t)))
  21009. (defun org-indent-line ()
  21010. "Indent line depending on context.
  21011. Indentation is done according to the following rules:
  21012. - Footnote definitions, diary sexps, headlines and inline tasks
  21013. have to start at column 0.
  21014. - On the very first line of an element, consider, in order, the
  21015. next rules until one matches:
  21016. 1. If there's a sibling element before, ignoring footnote
  21017. definitions and inline tasks, indent like its first line.
  21018. 2. If element has a parent, indent like its contents. More
  21019. precisely, if parent is an item, indent after the
  21020. description part, if any, or the bullet (see
  21021. `org-list-description-max-indent'). Else, indent like
  21022. parent's first line.
  21023. 3. Otherwise, indent relatively to current level, if
  21024. `org-adapt-indentation' is non-nil, or to left margin.
  21025. - On a blank line at the end of an element, indent according to
  21026. the type of the element. More precisely
  21027. 1. If element is a plain list, an item, or a footnote
  21028. definition, indent like the very last element within.
  21029. 2. If element is a paragraph, indent like its last non blank
  21030. line.
  21031. 3. Otherwise, indent like its very first line.
  21032. - In the code part of a source block, use language major mode
  21033. to indent current line if `org-src-tab-acts-natively' is
  21034. non-nil. If it is nil, do nothing.
  21035. - Otherwise, indent like the first non-blank line above.
  21036. The function doesn't indent an item as it could break the whole
  21037. list structure. Instead, use \\<org-mode-map>\\[org-shiftmetaleft] or \
  21038. \\[org-shiftmetaright].
  21039. Also align node properties according to `org-property-format'."
  21040. (interactive)
  21041. (cond
  21042. (orgstruct-is-++
  21043. (let ((indent-line-function
  21044. (cl-cadadr (assq 'indent-line-function org-fb-vars))))
  21045. (indent-according-to-mode)))
  21046. ((org-at-heading-p) 'noindent)
  21047. (t
  21048. (let* ((element (save-excursion (beginning-of-line) (org-element-at-point)))
  21049. (type (org-element-type element)))
  21050. (cond ((and (memq type '(plain-list item))
  21051. (= (line-beginning-position)
  21052. (org-element-property :post-affiliated element)))
  21053. 'noindent)
  21054. ((and (eq type 'src-block)
  21055. org-src-tab-acts-natively
  21056. (> (line-beginning-position)
  21057. (org-element-property :post-affiliated element))
  21058. (< (line-beginning-position)
  21059. (org-with-wide-buffer
  21060. (goto-char (org-element-property :end element))
  21061. (skip-chars-backward " \r\t\n")
  21062. (line-beginning-position))))
  21063. (org-babel-do-key-sequence-in-edit-buffer (kbd "TAB")))
  21064. (t
  21065. (let ((column (org--get-expected-indentation element nil)))
  21066. ;; Preserve current column.
  21067. (if (<= (current-column) (current-indentation))
  21068. (org-indent-line-to column)
  21069. (save-excursion (org-indent-line-to column))))
  21070. ;; Align node property. Also preserve current column.
  21071. (when (eq type 'node-property)
  21072. (let ((column (current-column)))
  21073. (org--align-node-property)
  21074. (org-move-to-column column)))))))))
  21075. (defun org-indent-region (start end)
  21076. "Indent each non-blank line in the region.
  21077. Called from a program, START and END specify the region to
  21078. indent. The function will not indent contents of example blocks,
  21079. verse blocks and export blocks as leading white spaces are
  21080. assumed to be significant there."
  21081. (interactive "r")
  21082. (save-excursion
  21083. (goto-char start)
  21084. (skip-chars-forward " \r\t\n")
  21085. (unless (eobp) (beginning-of-line))
  21086. (let ((indent-to
  21087. (lambda (ind pos)
  21088. ;; Set IND as indentation for all lines between point and
  21089. ;; POS. Blank lines are ignored. Leave point after POS
  21090. ;; once done.
  21091. (let ((limit (copy-marker pos)))
  21092. (while (< (point) limit)
  21093. (unless (org-looking-at-p "[ \t]*$") (org-indent-line-to ind))
  21094. (forward-line))
  21095. (set-marker limit nil))))
  21096. (end (copy-marker end)))
  21097. (while (< (point) end)
  21098. (if (or (org-looking-at-p " \r\t\n") (org-at-heading-p)) (forward-line)
  21099. (let* ((element (org-element-at-point))
  21100. (type (org-element-type element))
  21101. (element-end (copy-marker (org-element-property :end element)))
  21102. (ind (org--get-expected-indentation element nil)))
  21103. (cond
  21104. ((or (memq type '(paragraph table table-row))
  21105. (not (or (org-element-property :contents-begin element)
  21106. (memq type
  21107. '(example-block export-block src-block)))))
  21108. ;; Elements here are indented as a single block. Also
  21109. ;; align node properties.
  21110. (when (eq type 'node-property)
  21111. (org--align-node-property)
  21112. (beginning-of-line))
  21113. (funcall indent-to ind (min element-end end)))
  21114. (t
  21115. ;; Elements in this category consist of three parts:
  21116. ;; before the contents, the contents, and after the
  21117. ;; contents. The contents are treated specially,
  21118. ;; according to the element type, or not indented at
  21119. ;; all. Other parts are indented as a single block.
  21120. (let* ((post (copy-marker
  21121. (org-element-property :post-affiliated element)))
  21122. (cbeg
  21123. (copy-marker
  21124. (cond
  21125. ((not (org-element-property :contents-begin element))
  21126. ;; Fake contents for source blocks.
  21127. (org-with-wide-buffer
  21128. (goto-char post)
  21129. (forward-line)
  21130. (point)))
  21131. ((memq type '(footnote-definition item plain-list))
  21132. ;; Contents in these elements could start on
  21133. ;; the same line as the beginning of the
  21134. ;; element. Make sure we start indenting
  21135. ;; from the second line.
  21136. (org-with-wide-buffer
  21137. (goto-char post)
  21138. (end-of-line)
  21139. (skip-chars-forward " \r\t\n")
  21140. (if (eobp) (point) (line-beginning-position))))
  21141. (t (org-element-property :contents-begin element)))))
  21142. (cend (copy-marker
  21143. (or (org-element-property :contents-end element)
  21144. ;; Fake contents for source blocks.
  21145. (org-with-wide-buffer
  21146. (goto-char element-end)
  21147. (skip-chars-backward " \r\t\n")
  21148. (line-beginning-position)))
  21149. t)))
  21150. ;; Do not change items indentation individually as it
  21151. ;; might break the list as a whole. On the other
  21152. ;; hand, when at a plain list, indent it as a whole.
  21153. (cond ((eq type 'plain-list)
  21154. (let ((offset (- ind (org-get-indentation))))
  21155. (unless (zerop offset)
  21156. (indent-rigidly (org-element-property :begin element)
  21157. (org-element-property :end element)
  21158. offset))
  21159. (goto-char cbeg)))
  21160. ((eq type 'item) (goto-char cbeg))
  21161. (t (funcall indent-to ind (min cbeg end))))
  21162. (when (< (point) end)
  21163. (cl-case type
  21164. ((example-block export-block verse-block))
  21165. (src-block
  21166. ;; In a source block, indent source code
  21167. ;; according to language major mode, but only if
  21168. ;; `org-src-tab-acts-natively' is non-nil.
  21169. (when (and (< (point) end) org-src-tab-acts-natively)
  21170. (ignore-errors
  21171. (org-babel-do-in-edit-buffer
  21172. (indent-region (point-min) (point-max))))))
  21173. (t (org-indent-region (point) (min cend end))))
  21174. (goto-char (min cend end))
  21175. (when (< (point) end)
  21176. (funcall indent-to ind (min element-end end))))
  21177. (set-marker post nil)
  21178. (set-marker cbeg nil)
  21179. (set-marker cend nil))))
  21180. (set-marker element-end nil))))
  21181. (set-marker end nil))))
  21182. (defun org-indent-drawer ()
  21183. "Indent the drawer at point."
  21184. (interactive)
  21185. (unless (save-excursion
  21186. (beginning-of-line)
  21187. (org-looking-at-p org-drawer-regexp))
  21188. (user-error "Not at a drawer"))
  21189. (let ((element (org-element-at-point)))
  21190. (unless (memq (org-element-type element) '(drawer property-drawer))
  21191. (user-error "Not at a drawer"))
  21192. (org-with-wide-buffer
  21193. (org-indent-region (org-element-property :begin element)
  21194. (org-element-property :end element))))
  21195. (message "Drawer at point indented"))
  21196. (defun org-indent-block ()
  21197. "Indent the block at point."
  21198. (interactive)
  21199. (unless (save-excursion
  21200. (beginning-of-line)
  21201. (let ((case-fold-search t))
  21202. (org-looking-at-p "[ \t]*#\\+\\(begin\\|end\\)_")))
  21203. (user-error "Not at a block"))
  21204. (let ((element (org-element-at-point)))
  21205. (unless (memq (org-element-type element)
  21206. '(comment-block center-block dynamic-block example-block
  21207. export-block quote-block special-block
  21208. src-block verse-block))
  21209. (user-error "Not at a block"))
  21210. (org-with-wide-buffer
  21211. (org-indent-region (org-element-property :begin element)
  21212. (org-element-property :end element))))
  21213. (message "Block at point indented"))
  21214. ;;; Filling
  21215. ;; We use our own fill-paragraph and auto-fill functions.
  21216. ;; `org-fill-paragraph' relies on adaptive filling and context
  21217. ;; checking. Appropriate `fill-prefix' is computed with
  21218. ;; `org-adaptive-fill-function'.
  21219. ;; `org-auto-fill-function' takes care of auto-filling. It calls
  21220. ;; `do-auto-fill' only on valid areas with `fill-prefix' shadowed with
  21221. ;; `org-adaptive-fill-function' value. Internally,
  21222. ;; `org-comment-line-break-function' breaks the line.
  21223. ;; `org-setup-filling' installs filling and auto-filling related
  21224. ;; variables during `org-mode' initialization.
  21225. (defvar org-element-paragraph-separate) ; org-element.el
  21226. (defun org-setup-filling ()
  21227. (require 'org-element)
  21228. ;; Prevent auto-fill from inserting unwanted new items.
  21229. (when (boundp 'fill-nobreak-predicate)
  21230. (setq-local
  21231. fill-nobreak-predicate
  21232. (org-uniquify
  21233. (append fill-nobreak-predicate
  21234. '(org-fill-line-break-nobreak-p
  21235. org-fill-paragraph-with-timestamp-nobreak-p)))))
  21236. (let ((paragraph-ending (substring org-element-paragraph-separate 1)))
  21237. (setq-local paragraph-start paragraph-ending)
  21238. (setq-local paragraph-separate paragraph-ending))
  21239. (setq-local fill-paragraph-function 'org-fill-paragraph)
  21240. (setq-local auto-fill-inhibit-regexp nil)
  21241. (setq-local adaptive-fill-function 'org-adaptive-fill-function)
  21242. (setq-local normal-auto-fill-function 'org-auto-fill-function)
  21243. (setq-local comment-line-break-function 'org-comment-line-break-function))
  21244. (defun org-fill-line-break-nobreak-p ()
  21245. "Non-nil when a new line at point would create an Org line break."
  21246. (save-excursion
  21247. (skip-chars-backward "[ \t]")
  21248. (skip-chars-backward "\\\\")
  21249. (looking-at "\\\\\\\\\\($\\|[^\\\\]\\)")))
  21250. (defun org-fill-paragraph-with-timestamp-nobreak-p ()
  21251. "Non-nil when a new line at point would split a timestamp."
  21252. (and (org-at-timestamp-p t)
  21253. (not (looking-at org-ts-regexp-both))))
  21254. (declare-function message-in-body-p "message" ())
  21255. (defvar orgtbl-line-start-regexp) ; From org-table.el
  21256. (defun org-adaptive-fill-function ()
  21257. "Compute a fill prefix for the current line.
  21258. Return fill prefix, as a string, or nil if current line isn't
  21259. meant to be filled. For convenience, if `adaptive-fill-regexp'
  21260. matches in paragraphs or comments, use it."
  21261. (catch 'exit
  21262. (when (derived-mode-p 'message-mode)
  21263. (save-excursion
  21264. (beginning-of-line)
  21265. (cond ((not (message-in-body-p)) (throw 'exit nil))
  21266. ((org-looking-at-p org-table-line-regexp) (throw 'exit nil))
  21267. ((looking-at message-cite-prefix-regexp)
  21268. (throw 'exit (match-string-no-properties 0)))
  21269. ((looking-at org-outline-regexp)
  21270. (throw 'exit (make-string (length (match-string 0)) ?\s))))))
  21271. (org-with-wide-buffer
  21272. (unless (org-at-heading-p)
  21273. (let* ((p (line-beginning-position))
  21274. (element (save-excursion
  21275. (beginning-of-line)
  21276. (org-element-at-point)))
  21277. (type (org-element-type element))
  21278. (post-affiliated (org-element-property :post-affiliated element)))
  21279. (unless (< p post-affiliated)
  21280. (cl-case type
  21281. (comment
  21282. (save-excursion
  21283. (beginning-of-line)
  21284. (looking-at "[ \t]*")
  21285. (concat (match-string 0) "# ")))
  21286. (footnote-definition "")
  21287. ((item plain-list)
  21288. (make-string (org-list-item-body-column post-affiliated) ?\s))
  21289. (paragraph
  21290. ;; Fill prefix is usually the same as the current line,
  21291. ;; unless the paragraph is at the beginning of an item.
  21292. (let ((parent (org-element-property :parent element)))
  21293. (save-excursion
  21294. (beginning-of-line)
  21295. (cond ((eq (org-element-type parent) 'item)
  21296. (make-string (org-list-item-body-column
  21297. (org-element-property :begin parent))
  21298. ?\s))
  21299. ((and adaptive-fill-regexp
  21300. ;; Locally disable
  21301. ;; `adaptive-fill-function' to let
  21302. ;; `fill-context-prefix' handle
  21303. ;; `adaptive-fill-regexp' variable.
  21304. (let (adaptive-fill-function)
  21305. (fill-context-prefix
  21306. post-affiliated
  21307. (org-element-property :end element)))))
  21308. ((looking-at "[ \t]+") (match-string 0))
  21309. (t "")))))
  21310. (comment-block
  21311. ;; Only fill contents if P is within block boundaries.
  21312. (let* ((cbeg (save-excursion (goto-char post-affiliated)
  21313. (forward-line)
  21314. (point)))
  21315. (cend (save-excursion
  21316. (goto-char (org-element-property :end element))
  21317. (skip-chars-backward " \r\t\n")
  21318. (line-beginning-position))))
  21319. (when (and (>= p cbeg) (< p cend))
  21320. (if (save-excursion (beginning-of-line) (looking-at "[ \t]+"))
  21321. (match-string 0)
  21322. "")))))))))))
  21323. (declare-function message-goto-body "message" ())
  21324. (defvar message-cite-prefix-regexp) ; From message.el
  21325. (defun org-fill-paragraph (&optional justify)
  21326. "Fill element at point, when applicable.
  21327. This function only applies to comment blocks, comments, example
  21328. blocks and paragraphs. Also, as a special case, re-align table
  21329. when point is at one.
  21330. If JUSTIFY is non-nil (interactively, with prefix argument),
  21331. justify as well. If `sentence-end-double-space' is non-nil, then
  21332. period followed by one space does not end a sentence, so don't
  21333. break a line there. The variable `fill-column' controls the
  21334. width for filling.
  21335. For convenience, when point is at a plain list, an item or
  21336. a footnote definition, try to fill the first paragraph within."
  21337. (interactive)
  21338. (if (and (derived-mode-p 'message-mode)
  21339. (or (not (message-in-body-p))
  21340. (save-excursion (move-beginning-of-line 1)
  21341. (looking-at message-cite-prefix-regexp))))
  21342. ;; First ensure filling is correct in message-mode.
  21343. (let ((fill-paragraph-function
  21344. (cl-cadadr (assq 'fill-paragraph-function org-fb-vars)))
  21345. (fill-prefix (cl-cadadr (assq 'fill-prefix org-fb-vars)))
  21346. (paragraph-start (cl-cadadr (assq 'paragraph-start org-fb-vars)))
  21347. (paragraph-separate
  21348. (cl-cadadr (assq 'paragraph-separate org-fb-vars))))
  21349. (fill-paragraph nil))
  21350. (with-syntax-table org-mode-transpose-word-syntax-table
  21351. ;; Move to end of line in order to get the first paragraph
  21352. ;; within a plain list or a footnote definition.
  21353. (let ((element (save-excursion
  21354. (end-of-line)
  21355. (or (ignore-errors (org-element-at-point))
  21356. (user-error "An element cannot be parsed line %d"
  21357. (line-number-at-pos (point)))))))
  21358. ;; First check if point is in a blank line at the beginning of
  21359. ;; the buffer. In that case, ignore filling.
  21360. (cl-case (org-element-type element)
  21361. ;; Use major mode filling function is src blocks.
  21362. (src-block (org-babel-do-key-sequence-in-edit-buffer (kbd "M-q")))
  21363. ;; Align Org tables, leave table.el tables as-is.
  21364. (table-row (org-table-align) t)
  21365. (table
  21366. (when (eq (org-element-property :type element) 'org)
  21367. (save-excursion
  21368. (goto-char (org-element-property :post-affiliated element))
  21369. (org-table-align)))
  21370. t)
  21371. (paragraph
  21372. ;; Paragraphs may contain `line-break' type objects.
  21373. (let ((beg (max (point-min)
  21374. (org-element-property :contents-begin element)))
  21375. (end (min (point-max)
  21376. (org-element-property :contents-end element))))
  21377. ;; Do nothing if point is at an affiliated keyword.
  21378. (if (< (line-end-position) beg) t
  21379. (when (derived-mode-p 'message-mode)
  21380. ;; In `message-mode', do not fill following citation
  21381. ;; in current paragraph nor text before message body.
  21382. (let ((body-start (save-excursion (message-goto-body))))
  21383. (when body-start (setq beg (max body-start beg))))
  21384. (when (save-excursion
  21385. (re-search-forward
  21386. (concat "^" message-cite-prefix-regexp) end t))
  21387. (setq end (match-beginning 0))))
  21388. ;; Fill paragraph, taking line breaks into account.
  21389. (save-excursion
  21390. (goto-char beg)
  21391. (let ((cuts (list beg)))
  21392. (while (re-search-forward "\\\\\\\\[ \t]*\n" end t)
  21393. (when (eq 'line-break
  21394. (org-element-type
  21395. (save-excursion (backward-char)
  21396. (org-element-context))))
  21397. (push (point) cuts)))
  21398. (dolist (c (delq end cuts))
  21399. (fill-region-as-paragraph c end justify)
  21400. (setq end c))))
  21401. t)))
  21402. ;; Contents of `comment-block' type elements should be
  21403. ;; filled as plain text, but only if point is within block
  21404. ;; markers.
  21405. (comment-block
  21406. (let* ((case-fold-search t)
  21407. (beg (save-excursion
  21408. (goto-char (org-element-property :begin element))
  21409. (re-search-forward "^[ \t]*#\\+begin_comment" nil t)
  21410. (forward-line)
  21411. (point)))
  21412. (end (save-excursion
  21413. (goto-char (org-element-property :end element))
  21414. (re-search-backward "^[ \t]*#\\+end_comment" nil t)
  21415. (line-beginning-position))))
  21416. (if (or (< (point) beg) (> (point) end)) t
  21417. (fill-region-as-paragraph
  21418. (save-excursion (end-of-line)
  21419. (re-search-backward "^[ \t]*$" beg 'move)
  21420. (line-beginning-position))
  21421. (save-excursion (beginning-of-line)
  21422. (re-search-forward "^[ \t]*$" end 'move)
  21423. (line-beginning-position))
  21424. justify))))
  21425. ;; Fill comments.
  21426. (comment
  21427. (let ((begin (org-element-property :post-affiliated element))
  21428. (end (org-element-property :end element)))
  21429. (when (and (>= (point) begin) (<= (point) end))
  21430. (let ((begin (save-excursion
  21431. (end-of-line)
  21432. (if (re-search-backward "^[ \t]*#[ \t]*$" begin t)
  21433. (progn (forward-line) (point))
  21434. begin)))
  21435. (end (save-excursion
  21436. (end-of-line)
  21437. (if (re-search-forward "^[ \t]*#[ \t]*$" end 'move)
  21438. (1- (line-beginning-position))
  21439. (skip-chars-backward " \r\t\n")
  21440. (line-end-position)))))
  21441. ;; Do not fill comments when at a blank line.
  21442. (when (> end begin)
  21443. (let ((fill-prefix
  21444. (save-excursion
  21445. (beginning-of-line)
  21446. (looking-at "[ \t]*#")
  21447. (let ((comment-prefix (match-string 0)))
  21448. (goto-char (match-end 0))
  21449. (if (looking-at adaptive-fill-regexp)
  21450. (concat comment-prefix (match-string 0))
  21451. (concat comment-prefix " "))))))
  21452. (save-excursion
  21453. (fill-region-as-paragraph begin end justify))))))
  21454. t))
  21455. ;; Ignore every other element.
  21456. (otherwise t))))))
  21457. (defun org-auto-fill-function ()
  21458. "Auto-fill function."
  21459. ;; Check if auto-filling is meaningful.
  21460. (let ((fc (current-fill-column)))
  21461. (when (and fc (> (current-column) fc))
  21462. (let* ((fill-prefix (org-adaptive-fill-function))
  21463. ;; Enforce empty fill prefix, if required. Otherwise, it
  21464. ;; will be computed again.
  21465. (adaptive-fill-mode (not (equal fill-prefix ""))))
  21466. (when fill-prefix (do-auto-fill))))))
  21467. (defun org-comment-line-break-function (&optional soft)
  21468. "Break line at point and indent, continuing comment if within one.
  21469. The inserted newline is marked hard if variable
  21470. `use-hard-newlines' is true, unless optional argument SOFT is
  21471. non-nil."
  21472. (if soft (insert-and-inherit ?\n) (newline 1))
  21473. (save-excursion (forward-char -1) (delete-horizontal-space))
  21474. (delete-horizontal-space)
  21475. (indent-to-left-margin)
  21476. (insert-before-markers-and-inherit fill-prefix))
  21477. ;;; Fixed Width Areas
  21478. (defun org-toggle-fixed-width ()
  21479. "Toggle fixed-width markup.
  21480. Add or remove fixed-width markup on current line, whenever it
  21481. makes sense. Return an error otherwise.
  21482. If a region is active and if it contains only fixed-width areas
  21483. or blank lines, remove all fixed-width markup in it. If the
  21484. region contains anything else, convert all non-fixed-width lines
  21485. to fixed-width ones.
  21486. Blank lines at the end of the region are ignored unless the
  21487. region only contains such lines."
  21488. (interactive)
  21489. (if (not (org-region-active-p))
  21490. ;; No region:
  21491. ;;
  21492. ;; Remove fixed width marker only in a fixed-with element.
  21493. ;;
  21494. ;; Add fixed width maker in paragraphs, in blank lines after
  21495. ;; elements or at the beginning of a headline or an inlinetask,
  21496. ;; and before any one-line elements (e.g., a clock).
  21497. (progn
  21498. (beginning-of-line)
  21499. (let* ((element (org-element-at-point))
  21500. (type (org-element-type element)))
  21501. (cond
  21502. ((and (eq type 'fixed-width)
  21503. (looking-at "[ \t]*\\(:\\(?: \\|$\\)\\)"))
  21504. (replace-match
  21505. "" nil nil nil (if (= (line-end-position) (match-end 0)) 0 1)))
  21506. ((and (memq type '(babel-call clock comment diary-sexp headline
  21507. horizontal-rule keyword paragraph
  21508. planning))
  21509. (<= (org-element-property :post-affiliated element) (point)))
  21510. (skip-chars-forward " \t")
  21511. (insert ": "))
  21512. ((and (org-looking-at-p "[ \t]*$")
  21513. (or (eq type 'inlinetask)
  21514. (save-excursion
  21515. (skip-chars-forward " \r\t\n")
  21516. (<= (org-element-property :end element) (point)))))
  21517. (delete-region (point) (line-end-position))
  21518. (org-indent-line)
  21519. (insert ": "))
  21520. (t (user-error "Cannot insert a fixed-width line here")))))
  21521. ;; Region active.
  21522. (let* ((begin (save-excursion
  21523. (goto-char (region-beginning))
  21524. (line-beginning-position)))
  21525. (end (copy-marker
  21526. (save-excursion
  21527. (goto-char (region-end))
  21528. (unless (eolp) (beginning-of-line))
  21529. (if (save-excursion (re-search-backward "\\S-" begin t))
  21530. (progn (skip-chars-backward " \r\t\n") (point))
  21531. (point)))))
  21532. (all-fixed-width-p
  21533. (catch 'not-all-p
  21534. (save-excursion
  21535. (goto-char begin)
  21536. (skip-chars-forward " \r\t\n")
  21537. (when (eobp) (throw 'not-all-p nil))
  21538. (while (< (point) end)
  21539. (let ((element (org-element-at-point)))
  21540. (if (eq (org-element-type element) 'fixed-width)
  21541. (goto-char (org-element-property :end element))
  21542. (throw 'not-all-p nil))))
  21543. t))))
  21544. (if all-fixed-width-p
  21545. (save-excursion
  21546. (goto-char begin)
  21547. (while (< (point) end)
  21548. (when (looking-at "[ \t]*\\(:\\(?: \\|$\\)\\)")
  21549. (replace-match
  21550. "" nil nil nil
  21551. (if (= (line-end-position) (match-end 0)) 0 1)))
  21552. (forward-line)))
  21553. (let ((min-ind (point-max)))
  21554. ;; Find minimum indentation across all lines.
  21555. (save-excursion
  21556. (goto-char begin)
  21557. (if (not (save-excursion (re-search-forward "\\S-" end t)))
  21558. (setq min-ind 0)
  21559. (catch 'zerop
  21560. (while (< (point) end)
  21561. (unless (org-looking-at-p "[ \t]*$")
  21562. (let ((ind (org-get-indentation)))
  21563. (setq min-ind (min min-ind ind))
  21564. (when (zerop ind) (throw 'zerop t))))
  21565. (forward-line)))))
  21566. ;; Loop over all lines and add fixed-width markup everywhere
  21567. ;; but in fixed-width lines.
  21568. (save-excursion
  21569. (goto-char begin)
  21570. (while (< (point) end)
  21571. (cond
  21572. ((org-at-heading-p)
  21573. (insert ": ")
  21574. (forward-line)
  21575. (while (and (< (point) end) (org-looking-at-p "[ \t]*$"))
  21576. (insert ":")
  21577. (forward-line)))
  21578. ((org-looking-at-p "[ \t]*:\\( \\|$\\)")
  21579. (let* ((element (org-element-at-point))
  21580. (element-end (org-element-property :end element)))
  21581. (if (eq (org-element-type element) 'fixed-width)
  21582. (progn (goto-char element-end)
  21583. (skip-chars-backward " \r\t\n")
  21584. (forward-line))
  21585. (let ((limit (min end element-end)))
  21586. (while (< (point) limit)
  21587. (org-move-to-column min-ind t)
  21588. (insert ": ")
  21589. (forward-line))))))
  21590. (t
  21591. (org-move-to-column min-ind t)
  21592. (insert ": ")
  21593. (forward-line)))))))
  21594. (set-marker end nil))))
  21595. ;;; Comments
  21596. ;; Org comments syntax is quite complex. It requires the entire line
  21597. ;; to be just a comment. Also, even with the right syntax at the
  21598. ;; beginning of line, some some elements (i.e. verse-block or
  21599. ;; example-block) don't accept comments. Usual Emacs comment commands
  21600. ;; cannot cope with those requirements. Therefore, Org replaces them.
  21601. ;; Org still relies on `comment-dwim', but cannot trust
  21602. ;; `comment-only-p'. So, `comment-region-function' and
  21603. ;; `uncomment-region-function' both point
  21604. ;; to`org-comment-or-uncomment-region'. Eventually,
  21605. ;; `org-insert-comment' takes care of insertion of comments at the
  21606. ;; beginning of line.
  21607. ;; `org-setup-comments-handling' install comments related variables
  21608. ;; during `org-mode' initialization.
  21609. (defun org-setup-comments-handling ()
  21610. (interactive)
  21611. (setq-local comment-use-syntax nil)
  21612. (setq-local comment-start "# ")
  21613. (setq-local comment-start-skip "^\\s-*#\\(?: \\|$\\)")
  21614. (setq-local comment-insert-comment-function 'org-insert-comment)
  21615. (setq-local comment-region-function 'org-comment-or-uncomment-region)
  21616. (setq-local uncomment-region-function 'org-comment-or-uncomment-region))
  21617. (defun org-insert-comment ()
  21618. "Insert an empty comment above current line.
  21619. If the line is empty, insert comment at its beginning. When
  21620. point is within a source block, comment according to the related
  21621. major mode."
  21622. (if (let ((element (org-element-at-point)))
  21623. (and (eq (org-element-type element) 'src-block)
  21624. (< (save-excursion
  21625. (goto-char (org-element-property :post-affiliated element))
  21626. (line-end-position))
  21627. (point))
  21628. (> (save-excursion
  21629. (goto-char (org-element-property :end element))
  21630. (skip-chars-backward " \r\t\n")
  21631. (line-beginning-position))
  21632. (point))))
  21633. (org-babel-do-in-edit-buffer (call-interactively 'comment-dwim))
  21634. (beginning-of-line)
  21635. (if (looking-at "\\s-*$") (delete-region (point) (point-at-eol))
  21636. (open-line 1))
  21637. (org-indent-line)
  21638. (insert "# ")))
  21639. (defvar comment-empty-lines) ; From newcomment.el.
  21640. (defun org-comment-or-uncomment-region (beg end &rest _)
  21641. "Comment or uncomment each non-blank line in the region.
  21642. Uncomment each non-blank line between BEG and END if it only
  21643. contains commented lines. Otherwise, comment them. If region is
  21644. strictly within a source block, use appropriate comment syntax."
  21645. (if (let ((element (org-element-at-point)))
  21646. (and (eq (org-element-type element) 'src-block)
  21647. (< (save-excursion
  21648. (goto-char (org-element-property :post-affiliated element))
  21649. (line-end-position))
  21650. beg)
  21651. (>= (save-excursion
  21652. (goto-char (org-element-property :end element))
  21653. (skip-chars-backward " \r\t\n")
  21654. (line-beginning-position))
  21655. end)))
  21656. (org-babel-do-in-edit-buffer (call-interactively 'comment-dwim))
  21657. (save-restriction
  21658. ;; Restrict region
  21659. (narrow-to-region (save-excursion (goto-char beg)
  21660. (skip-chars-forward " \r\t\n" end)
  21661. (line-beginning-position))
  21662. (save-excursion (goto-char end)
  21663. (skip-chars-backward " \r\t\n" beg)
  21664. (line-end-position)))
  21665. (let ((uncommentp
  21666. ;; UNCOMMENTP is non-nil when every non blank line between
  21667. ;; BEG and END is a comment.
  21668. (save-excursion
  21669. (goto-char (point-min))
  21670. (while (and (not (eobp))
  21671. (let ((element (org-element-at-point)))
  21672. (and (eq (org-element-type element) 'comment)
  21673. (goto-char (min (point-max)
  21674. (org-element-property
  21675. :end element)))))))
  21676. (eobp))))
  21677. (if uncommentp
  21678. ;; Only blank lines and comments in region: uncomment it.
  21679. (save-excursion
  21680. (goto-char (point-min))
  21681. (while (not (eobp))
  21682. (when (looking-at "[ \t]*\\(#\\(?: \\|$\\)\\)")
  21683. (replace-match "" nil nil nil 1))
  21684. (forward-line)))
  21685. ;; Comment each line in region.
  21686. (let ((min-indent (point-max)))
  21687. ;; First find the minimum indentation across all lines.
  21688. (save-excursion
  21689. (goto-char (point-min))
  21690. (while (and (not (eobp)) (not (zerop min-indent)))
  21691. (unless (looking-at "[ \t]*$")
  21692. (setq min-indent (min min-indent (current-indentation))))
  21693. (forward-line)))
  21694. ;; Then loop over all lines.
  21695. (save-excursion
  21696. (goto-char (point-min))
  21697. (while (not (eobp))
  21698. (unless (and (not comment-empty-lines) (looking-at "[ \t]*$"))
  21699. ;; Don't get fooled by invisible text (e.g. link path)
  21700. ;; when moving to column MIN-INDENT.
  21701. (let ((buffer-invisibility-spec nil))
  21702. (org-move-to-column min-indent t))
  21703. (insert comment-start))
  21704. (forward-line)))))))))
  21705. (defun org-comment-dwim (_arg)
  21706. "Call `comment-dwim' within a source edit buffer if needed."
  21707. (interactive "*P")
  21708. (if (org-in-src-block-p)
  21709. (org-babel-do-in-edit-buffer (call-interactively 'comment-dwim))
  21710. (call-interactively 'comment-dwim)))
  21711. ;;; Timestamps API
  21712. ;; This section contains tools to operate on timestamp objects, as
  21713. ;; returned by, e.g. `org-element-context'.
  21714. (defun org-timestamp--to-internal-time (timestamp &optional end)
  21715. "Encode TIMESTAMP object into Emacs internal time.
  21716. Use end of date range or time range when END is non-nil."
  21717. (apply #'encode-time
  21718. (cons 0
  21719. (mapcar
  21720. (lambda (prop) (or (org-element-property prop timestamp) 0))
  21721. (if end '(:minute-end :hour-end :day-end :month-end :year-end)
  21722. '(:minute-start :hour-start :day-start :month-start
  21723. :year-start))))))
  21724. (defun org-timestamp-has-time-p (timestamp)
  21725. "Non-nil when TIMESTAMP has a time specified."
  21726. (org-element-property :hour-start timestamp))
  21727. (defun org-timestamp-format (timestamp format &optional end utc)
  21728. "Format a TIMESTAMP object into a string.
  21729. FORMAT is a format specifier to be passed to
  21730. `format-time-string'.
  21731. When optional argument END is non-nil, use end of date-range or
  21732. time-range, if possible.
  21733. When optional argument UTC is non-nil, time will be expressed as
  21734. Universal Time."
  21735. (format-time-string
  21736. format (org-timestamp--to-internal-time timestamp end) utc))
  21737. (defun org-timestamp-split-range (timestamp &optional end)
  21738. "Extract a TIMESTAMP object from a date or time range.
  21739. END, when non-nil, means extract the end of the range.
  21740. Otherwise, extract its start.
  21741. Return a new timestamp object."
  21742. (let ((type (org-element-property :type timestamp)))
  21743. (if (memq type '(active inactive diary)) timestamp
  21744. (let ((split-ts (org-element-copy timestamp)))
  21745. ;; Set new type.
  21746. (org-element-put-property
  21747. split-ts :type (if (eq type 'active-range) 'active 'inactive))
  21748. ;; Copy start properties over end properties if END is
  21749. ;; non-nil. Otherwise, copy end properties over `start' ones.
  21750. (let ((p-alist '((:minute-start . :minute-end)
  21751. (:hour-start . :hour-end)
  21752. (:day-start . :day-end)
  21753. (:month-start . :month-end)
  21754. (:year-start . :year-end))))
  21755. (dolist (p-cell p-alist)
  21756. (org-element-put-property
  21757. split-ts
  21758. (funcall (if end #'car #'cdr) p-cell)
  21759. (org-element-property
  21760. (funcall (if end #'cdr #'car) p-cell) split-ts)))
  21761. ;; Eventually refresh `:raw-value'.
  21762. (org-element-put-property split-ts :raw-value nil)
  21763. (org-element-put-property
  21764. split-ts :raw-value (org-element-interpret-data split-ts)))))))
  21765. (defun org-timestamp-translate (timestamp &optional boundary)
  21766. "Translate TIMESTAMP object to custom format.
  21767. Format string is defined in `org-time-stamp-custom-formats',
  21768. which see.
  21769. When optional argument BOUNDARY is non-nil, it is either the
  21770. symbol `start' or `end'. In this case, only translate the
  21771. starting or ending part of TIMESTAMP if it is a date or time
  21772. range. Otherwise, translate both parts.
  21773. Return timestamp as-is if `org-display-custom-times' is nil or if
  21774. it has a `diary' type."
  21775. (let ((type (org-element-property :type timestamp)))
  21776. (if (or (not org-display-custom-times) (eq type 'diary))
  21777. (org-element-interpret-data timestamp)
  21778. (let ((fmt (funcall (if (org-timestamp-has-time-p timestamp) #'cdr #'car)
  21779. org-time-stamp-custom-formats)))
  21780. (if (and (not boundary) (memq type '(active-range inactive-range)))
  21781. (concat (org-timestamp-format timestamp fmt)
  21782. "--"
  21783. (org-timestamp-format timestamp fmt t))
  21784. (org-timestamp-format timestamp fmt (eq boundary 'end)))))))
  21785. ;;; Other stuff.
  21786. (defvar reftex-docstruct-symbol)
  21787. (defvar reftex-cite-format)
  21788. (defvar org--rds)
  21789. (defun org-reftex-citation ()
  21790. "Use reftex-citation to insert a citation into the buffer.
  21791. This looks for a line like
  21792. #+BIBLIOGRAPHY: foo plain option:-d
  21793. and derives from it that foo.bib is the bibliography file relevant
  21794. for this document. It then installs the necessary environment for RefTeX
  21795. to work in this buffer and calls `reftex-citation' to insert a citation
  21796. into the buffer.
  21797. Export of such citations to both LaTeX and HTML is handled by the contributed
  21798. package ox-bibtex by Taru Karttunen."
  21799. (interactive)
  21800. (let ((reftex-docstruct-symbol 'org--rds)
  21801. (reftex-cite-format "\\cite{%l}")
  21802. org--rds bib)
  21803. (save-excursion
  21804. (save-restriction
  21805. (widen)
  21806. (let ((case-fold-search t)
  21807. (re "^[ \t]*#\\+BIBLIOGRAPHY:[ \t]+\\([^ \t\n]+\\)"))
  21808. (if (not (save-excursion
  21809. (or (re-search-forward re nil t)
  21810. (re-search-backward re nil t))))
  21811. (user-error "No bibliography defined in file")
  21812. (setq bib (concat (match-string 1) ".bib")
  21813. org--rds (list (list 'bib bib)))))))
  21814. (call-interactively 'reftex-citation)))
  21815. ;;;; Functions extending outline functionality
  21816. (defun org-beginning-of-line (&optional arg)
  21817. "Go to the beginning of the current line. If that is invisible, continue
  21818. to a visible line beginning. This makes the function of C-a more intuitive.
  21819. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  21820. first attempt, and only move to after the tags when the cursor is already
  21821. beyond the end of the headline."
  21822. (interactive "P")
  21823. (let ((pos (point))
  21824. (special (if (consp org-special-ctrl-a/e)
  21825. (car org-special-ctrl-a/e)
  21826. org-special-ctrl-a/e))
  21827. deactivate-mark refpos)
  21828. (if (org-bound-and-true-p visual-line-mode)
  21829. (beginning-of-visual-line 1)
  21830. (beginning-of-line 1))
  21831. (if (and arg (fboundp 'move-beginning-of-line))
  21832. (call-interactively 'move-beginning-of-line)
  21833. (unless (bobp)
  21834. (backward-char 1)
  21835. (if (org-truely-invisible-p)
  21836. (while (and (not (bobp)) (org-truely-invisible-p))
  21837. (backward-char 1)
  21838. (beginning-of-line 1))
  21839. (forward-char 1))))
  21840. (when special
  21841. (cond
  21842. ((and (looking-at org-complex-heading-regexp)
  21843. (eq (char-after (match-end 1)) ?\s))
  21844. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  21845. (point-at-eol)))
  21846. (goto-char
  21847. (if (eq special t)
  21848. (cond ((> pos refpos) refpos)
  21849. ((= pos (point)) refpos)
  21850. (t (point)))
  21851. (cond ((> pos (point)) (point))
  21852. ((not (eq last-command this-command)) (point))
  21853. (t refpos)))))
  21854. ((org-at-item-p)
  21855. ;; Being at an item and not looking at an the item means point
  21856. ;; was previously moved to beginning of a visual line, which
  21857. ;; doesn't contain the item. Therefore, do nothing special,
  21858. ;; just stay here.
  21859. (when (looking-at org-list-full-item-re)
  21860. ;; Set special position at first white space character after
  21861. ;; bullet, and check-box, if any.
  21862. (let ((after-bullet
  21863. (let ((box (match-end 3)))
  21864. (if (not box) (match-end 1)
  21865. (let ((after (char-after box)))
  21866. (if (and after (= after ? )) (1+ box) box))))))
  21867. ;; Special case: Move point to special position when
  21868. ;; currently after it or at beginning of line.
  21869. (if (eq special t)
  21870. (when (or (> pos after-bullet) (= (point) pos))
  21871. (goto-char after-bullet))
  21872. ;; Reversed case: Move point to special position when
  21873. ;; point was already at beginning of line and command is
  21874. ;; repeated.
  21875. (when (and (= (point) pos) (eq last-command this-command))
  21876. (goto-char after-bullet))))))))
  21877. (org-no-warnings
  21878. (and (featurep 'xemacs) (setq zmacs-region-stays t))))
  21879. (setq disable-point-adjustment
  21880. (or (not (invisible-p (point)))
  21881. (not (invisible-p (max (point-min) (1- (point))))))))
  21882. (defun org-end-of-line (&optional arg)
  21883. "Go to the end of the line.
  21884. If this is a headline, and `org-special-ctrl-a/e' is set, ignore
  21885. tags on the first attempt, and only move to after the tags when
  21886. the cursor is already beyond the end of the headline."
  21887. (interactive "P")
  21888. (let ((special (if (consp org-special-ctrl-a/e) (cdr org-special-ctrl-a/e)
  21889. org-special-ctrl-a/e))
  21890. (move-fun (cond ((org-bound-and-true-p visual-line-mode)
  21891. 'end-of-visual-line)
  21892. ((fboundp 'move-end-of-line) 'move-end-of-line)
  21893. (t 'end-of-line)))
  21894. deactivate-mark)
  21895. (if (or (not special) arg) (call-interactively move-fun)
  21896. (let* ((element (save-excursion (beginning-of-line)
  21897. (org-element-at-point)))
  21898. (type (org-element-type element)))
  21899. (cond
  21900. ((memq type '(headline inlinetask))
  21901. (let ((pos (point)))
  21902. (beginning-of-line 1)
  21903. (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*\\)?$"))
  21904. (if (eq special t)
  21905. (if (or (< pos (match-beginning 1)) (= pos (match-end 0)))
  21906. (goto-char (match-beginning 1))
  21907. (goto-char (match-end 0)))
  21908. (if (or (< pos (match-end 0))
  21909. (not (eq this-command last-command)))
  21910. (goto-char (match-end 0))
  21911. (goto-char (match-beginning 1))))
  21912. (call-interactively move-fun))))
  21913. ((outline-invisible-p (line-end-position))
  21914. ;; If element is hidden, `move-end-of-line' would put point
  21915. ;; after it. Use `end-of-line' to stay on current line.
  21916. (call-interactively 'end-of-line))
  21917. (t (call-interactively move-fun)))))
  21918. (org-no-warnings (and (featurep 'xemacs) (setq zmacs-region-stays t))))
  21919. (setq disable-point-adjustment
  21920. (or (not (invisible-p (point)))
  21921. (not (invisible-p (max (point-min) (1- (point))))))))
  21922. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  21923. (define-key org-mode-map "\C-e" 'org-end-of-line)
  21924. (defun org-backward-sentence (&optional _arg)
  21925. "Go to beginning of sentence, or beginning of table field.
  21926. This will call `backward-sentence' or `org-table-beginning-of-field',
  21927. depending on context."
  21928. (interactive)
  21929. (let* ((element (org-element-at-point))
  21930. (contents-begin (org-element-property :contents-begin element))
  21931. (table (org-element-lineage element '(table) t)))
  21932. (if (and table
  21933. (> (point) contents-begin)
  21934. (<= (point) (org-element-property :contents-end table)))
  21935. (call-interactively #'org-table-beginning-of-field)
  21936. (save-restriction
  21937. (when (and contents-begin
  21938. (< (point-min) contents-begin)
  21939. (> (point) contents-begin))
  21940. (narrow-to-region contents-begin
  21941. (org-element-property :contents-end element)))
  21942. (call-interactively #'backward-sentence)))))
  21943. (defun org-forward-sentence (&optional _arg)
  21944. "Go to end of sentence, or end of table field.
  21945. This will call `forward-sentence' or `org-table-end-of-field',
  21946. depending on context."
  21947. (interactive)
  21948. (let* ((element (org-element-at-point))
  21949. (contents-end (org-element-property :contents-end element))
  21950. (table (org-element-lineage element '(table) t)))
  21951. (if (and table
  21952. (>= (point) (org-element-property :contents-begin table))
  21953. (< (point) contents-end))
  21954. (call-interactively #'org-table-end-of-field)
  21955. (save-restriction
  21956. (when (and contents-end
  21957. (> (point-max) contents-end)
  21958. ;; Skip blank lines between elements.
  21959. (< (org-element-property :end element)
  21960. (save-excursion (goto-char contents-end)
  21961. (skip-chars-forward " \r\t\n"))))
  21962. (narrow-to-region (org-element-property :contents-begin element)
  21963. contents-end))
  21964. (call-interactively #'forward-sentence)))))
  21965. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  21966. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  21967. (defun org-kill-line (&optional _arg)
  21968. "Kill line, to tags or end of line."
  21969. (interactive)
  21970. (cond
  21971. ((or (not org-special-ctrl-k)
  21972. (bolp)
  21973. (not (org-at-heading-p)))
  21974. (when (and (get-char-property (min (point-max) (point-at-eol)) 'invisible)
  21975. org-ctrl-k-protect-subtree
  21976. (or (eq org-ctrl-k-protect-subtree 'error)
  21977. (not (y-or-n-p "Kill hidden subtree along with headline? "))))
  21978. (user-error "C-k aborted as it would kill a hidden subtree"))
  21979. (call-interactively
  21980. (if (org-bound-and-true-p visual-line-mode) 'kill-visual-line 'kill-line)))
  21981. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$"))
  21982. (kill-region (point) (match-beginning 1))
  21983. (org-set-tags nil t))
  21984. (t (kill-region (point) (point-at-eol)))))
  21985. (define-key org-mode-map "\C-k" 'org-kill-line)
  21986. (defun org-yank (&optional arg)
  21987. "Yank. If the kill is a subtree, treat it specially.
  21988. This command will look at the current kill and check if is a single
  21989. subtree, or a series of subtrees[1]. If it passes the test, and if the
  21990. cursor is at the beginning of a line or after the stars of a currently
  21991. empty headline, then the yank is handled specially. How exactly depends
  21992. on the value of the following variables, both set by default.
  21993. `org-yank-folded-subtrees'
  21994. When set, the subtree(s) will be folded after insertion, but only
  21995. if doing so would now swallow text after the yanked text.
  21996. `org-yank-adjusted-subtrees'
  21997. When set, the subtree will be promoted or demoted in order to
  21998. fit into the local outline tree structure, which means that the
  21999. level will be adjusted so that it becomes the smaller one of the
  22000. two *visible* surrounding headings.
  22001. Any prefix to this command will cause `yank' to be called directly with
  22002. no special treatment. In particular, a simple \\[universal-argument] prefix \
  22003. will just
  22004. plainly yank the text as it is.
  22005. \[1] The test checks if the first non-white line is a heading
  22006. and if there are no other headings with fewer stars."
  22007. (interactive "P")
  22008. (org-yank-generic 'yank arg))
  22009. (defun org-yank-generic (command arg)
  22010. "Perform some yank-like command.
  22011. This function implements the behavior described in the `org-yank'
  22012. documentation. However, it has been generalized to work for any
  22013. interactive command with similar behavior."
  22014. ;; pretend to be command COMMAND
  22015. (setq this-command command)
  22016. (if arg
  22017. (call-interactively command)
  22018. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  22019. (and (org-kill-is-subtree-p)
  22020. (or (bolp)
  22021. (and (looking-at "[ \t]*$")
  22022. (string-match
  22023. "\\`\\*+\\'"
  22024. (buffer-substring (point-at-bol) (point)))))))
  22025. swallowp)
  22026. (cond
  22027. ((and subtreep org-yank-folded-subtrees)
  22028. (let ((beg (point))
  22029. end)
  22030. (if (and subtreep org-yank-adjusted-subtrees)
  22031. (org-paste-subtree nil nil 'for-yank)
  22032. (call-interactively command))
  22033. (setq end (point))
  22034. (goto-char beg)
  22035. (when (and (bolp) subtreep
  22036. (not (setq swallowp
  22037. (org-yank-folding-would-swallow-text beg end))))
  22038. (org-with-limited-levels
  22039. (or (looking-at org-outline-regexp)
  22040. (re-search-forward org-outline-regexp-bol end t))
  22041. (while (and (< (point) end) (looking-at org-outline-regexp))
  22042. (outline-hide-subtree)
  22043. (org-cycle-show-empty-lines 'folded)
  22044. (condition-case nil
  22045. (outline-forward-same-level 1)
  22046. (error (goto-char end))))))
  22047. (when swallowp
  22048. (message
  22049. "Inserted text not folded because that would swallow text"))
  22050. (goto-char end)
  22051. (skip-chars-forward " \t\n\r")
  22052. (beginning-of-line 1)
  22053. (push-mark beg 'nomsg)))
  22054. ((and subtreep org-yank-adjusted-subtrees)
  22055. (let ((beg (point-at-bol)))
  22056. (org-paste-subtree nil nil 'for-yank)
  22057. (push-mark beg 'nomsg)))
  22058. (t
  22059. (call-interactively command))))))
  22060. (defun org-yank-folding-would-swallow-text (beg end)
  22061. "Would hide-subtree at BEG swallow any text after END?"
  22062. (let (level)
  22063. (org-with-limited-levels
  22064. (save-excursion
  22065. (goto-char beg)
  22066. (when (or (looking-at org-outline-regexp)
  22067. (re-search-forward org-outline-regexp-bol end t))
  22068. (setq level (org-outline-level)))
  22069. (goto-char end)
  22070. (skip-chars-forward " \t\r\n\v\f")
  22071. (not (or (eobp)
  22072. (and (bolp) (looking-at-p org-outline-regexp)
  22073. (<= (org-outline-level) level))))))))
  22074. (define-key org-mode-map "\C-y" 'org-yank)
  22075. (defun org-truely-invisible-p ()
  22076. "Check if point is at a character currently not visible.
  22077. This version does not only check the character property, but also
  22078. `visible-mode'."
  22079. ;; Early versions of noutline don't have `outline-invisible-p'.
  22080. (unless (org-bound-and-true-p visible-mode)
  22081. (outline-invisible-p)))
  22082. (defun org-invisible-p2 ()
  22083. "Check if point is at a character currently not visible."
  22084. (save-excursion
  22085. (when (and (eolp) (not (bobp))) (backward-char 1))
  22086. ;; Early versions of noutline don't have `outline-invisible-p'.
  22087. (outline-invisible-p)))
  22088. (defun org-back-to-heading (&optional invisible-ok)
  22089. "Call `outline-back-to-heading', but provide a better error message."
  22090. (condition-case nil
  22091. (outline-back-to-heading invisible-ok)
  22092. (error (error "Before first headline at position %d in buffer %s"
  22093. (point) (current-buffer)))))
  22094. (defun org-before-first-heading-p ()
  22095. "Before first heading?"
  22096. (save-excursion
  22097. (end-of-line)
  22098. (null (re-search-backward org-outline-regexp-bol nil t))))
  22099. (defun org-at-heading-p (&optional ignored)
  22100. (outline-on-heading-p t))
  22101. ;;; Though the function was obsoleted in 7.8.03, the byte-compiler
  22102. ;;; warning was only added in Org 9.0, which should be taken into
  22103. ;;; account when deciding when to remove the alias.
  22104. (define-obsolete-function-alias 'org-on-heading-p 'org-at-heading-p "Org 7.8.03")
  22105. (defun org-in-commented-heading-p (&optional no-inheritance)
  22106. "Non-nil if point is under a commented heading.
  22107. This function also checks ancestors of the current headline,
  22108. unless optional argument NO-INHERITANCE is non-nil."
  22109. (cond
  22110. ((org-before-first-heading-p) nil)
  22111. ((let ((headline (nth 4 (org-heading-components))))
  22112. (and headline
  22113. (let ((case-fold-search nil))
  22114. (org-string-match-p (concat "^" org-comment-string "\\(?: \\|$\\)")
  22115. headline)))))
  22116. (no-inheritance nil)
  22117. (t
  22118. (save-excursion (and (org-up-heading-safe) (org-in-commented-heading-p))))))
  22119. (defun org-at-comment-p nil
  22120. "Is cursor in a commented line?"
  22121. (save-excursion
  22122. (save-match-data
  22123. (beginning-of-line)
  22124. (looking-at "^[ \t]*# "))))
  22125. (defun org-at-drawer-p nil
  22126. "Is cursor at a drawer keyword?"
  22127. (save-excursion
  22128. (move-beginning-of-line 1)
  22129. (looking-at org-drawer-regexp)))
  22130. (defun org-at-block-p nil
  22131. "Is cursor at a block keyword?"
  22132. (save-excursion
  22133. (move-beginning-of-line 1)
  22134. (looking-at org-block-regexp)))
  22135. (defun org-point-at-end-of-empty-headline ()
  22136. "If point is at the end of an empty headline, return t, else nil.
  22137. If the heading only contains a TODO keyword, it is still still considered
  22138. empty."
  22139. (and (looking-at "[ \t]*$")
  22140. (when org-todo-line-regexp
  22141. (save-excursion
  22142. (beginning-of-line 1)
  22143. (let ((case-fold-search nil))
  22144. (looking-at org-todo-line-regexp)
  22145. (string= (match-string 3) ""))))))
  22146. (defun org-at-heading-or-item-p ()
  22147. (or (org-at-heading-p) (org-at-item-p)))
  22148. (defun org-at-target-p ()
  22149. (or (org-in-regexp org-radio-target-regexp)
  22150. (org-in-regexp org-target-regexp)))
  22151. ;; Compatibility alias with Org versions < 7.8.03
  22152. (defalias 'org-on-target-p 'org-at-target-p)
  22153. (defun org-up-heading-all (arg)
  22154. "Move to the heading line of which the present line is a subheading.
  22155. This function considers both visible and invisible heading lines.
  22156. With argument, move up ARG levels."
  22157. (if (fboundp 'outline-up-heading-all)
  22158. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  22159. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  22160. (defun org-up-heading-safe ()
  22161. "Move to the heading line of which the present line is a subheading.
  22162. This version will not throw an error. It will return the level of the
  22163. headline found, or nil if no higher level is found.
  22164. Also, this function will be a lot faster than `outline-up-heading',
  22165. because it relies on stars being the outline starters. This can really
  22166. make a significant difference in outlines with very many siblings."
  22167. (when (ignore-errors (org-back-to-heading t))
  22168. (let ((level-up (1- (funcall outline-level))))
  22169. (and (> level-up 0)
  22170. (re-search-backward (format "^\\*\\{1,%d\\} " level-up) nil t)
  22171. (funcall outline-level)))))
  22172. (defun org-first-sibling-p ()
  22173. "Is this heading the first child of its parents?"
  22174. (interactive)
  22175. (let ((re org-outline-regexp-bol)
  22176. level l)
  22177. (unless (org-at-heading-p t)
  22178. (user-error "Not at a heading"))
  22179. (setq level (funcall outline-level))
  22180. (save-excursion
  22181. (if (not (re-search-backward re nil t))
  22182. t
  22183. (setq l (funcall outline-level))
  22184. (< l level)))))
  22185. (defun org-goto-sibling (&optional previous)
  22186. "Goto the next sibling, even if it is invisible.
  22187. When PREVIOUS is set, go to the previous sibling instead. Returns t
  22188. when a sibling was found. When none is found, return nil and don't
  22189. move point."
  22190. (let ((fun (if previous 're-search-backward 're-search-forward))
  22191. (pos (point))
  22192. (re org-outline-regexp-bol)
  22193. level l)
  22194. (when (ignore-errors (org-back-to-heading t))
  22195. (setq level (funcall outline-level))
  22196. (catch 'exit
  22197. (or previous (forward-char 1))
  22198. (while (funcall fun re nil t)
  22199. (setq l (funcall outline-level))
  22200. (when (< l level) (goto-char pos) (throw 'exit nil))
  22201. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  22202. (goto-char pos)
  22203. nil))))
  22204. (defun org-show-siblings ()
  22205. "Show all siblings of the current headline."
  22206. (save-excursion
  22207. (while (org-goto-sibling) (org-flag-heading nil)))
  22208. (save-excursion
  22209. (while (org-goto-sibling 'previous)
  22210. (org-flag-heading nil))))
  22211. (defun org-goto-first-child ()
  22212. "Goto the first child, even if it is invisible.
  22213. Return t when a child was found. Otherwise don't move point and
  22214. return nil."
  22215. (let (level (pos (point)) (re org-outline-regexp-bol))
  22216. (when (ignore-errors (org-back-to-heading t))
  22217. (setq level (outline-level))
  22218. (forward-char 1)
  22219. (if (and (re-search-forward re nil t) (> (outline-level) level))
  22220. (progn (goto-char (match-beginning 0)) t)
  22221. (goto-char pos) nil))))
  22222. (defun org-show-hidden-entry ()
  22223. "Show an entry where even the heading is hidden."
  22224. (save-excursion
  22225. (org-show-entry)))
  22226. (defun org-flag-heading (flag &optional entry)
  22227. "Flag the current heading. FLAG non-nil means make invisible.
  22228. When ENTRY is non-nil, show the entire entry."
  22229. (save-excursion
  22230. (org-back-to-heading t)
  22231. ;; Check if we should show the entire entry
  22232. (if entry
  22233. (progn
  22234. (org-show-entry)
  22235. (save-excursion
  22236. (and (outline-next-heading)
  22237. (org-flag-heading nil))))
  22238. (outline-flag-region (max (point-min) (1- (point)))
  22239. (save-excursion (outline-end-of-heading) (point))
  22240. flag))))
  22241. (defun org-get-next-sibling ()
  22242. "Move to next heading of the same level, and return point.
  22243. If there is no such heading, return nil.
  22244. This is like outline-next-sibling, but invisible headings are ok."
  22245. (let ((level (funcall outline-level)))
  22246. (outline-next-heading)
  22247. (while (and (not (eobp)) (> (funcall outline-level) level))
  22248. (outline-next-heading))
  22249. (unless (or (eobp) (< (funcall outline-level) level))
  22250. (point))))
  22251. (defun org-get-last-sibling ()
  22252. "Move to previous heading of the same level, and return point.
  22253. If there is no such heading, return nil."
  22254. (let ((opoint (point))
  22255. (level (funcall outline-level)))
  22256. (outline-previous-heading)
  22257. (when (and (/= (point) opoint) (outline-on-heading-p t))
  22258. (while (and (> (funcall outline-level) level)
  22259. (not (bobp)))
  22260. (outline-previous-heading))
  22261. (unless (< (funcall outline-level) level)
  22262. (point)))))
  22263. (defun org-end-of-subtree (&optional invisible-ok to-heading)
  22264. "Goto to the end of a subtree."
  22265. ;; This contains an exact copy of the original function, but it uses
  22266. ;; `org-back-to-heading', to make it work also in invisible
  22267. ;; trees. And is uses an invisible-ok argument.
  22268. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  22269. ;; Furthermore, when used inside Org, finding the end of a large subtree
  22270. ;; with many children and grandchildren etc, this can be much faster
  22271. ;; than the outline version.
  22272. (org-back-to-heading invisible-ok)
  22273. (let ((first t)
  22274. (level (funcall outline-level)))
  22275. (if (and (derived-mode-p 'org-mode) (< level 1000))
  22276. ;; A true heading (not a plain list item), in Org-mode
  22277. ;; This means we can easily find the end by looking
  22278. ;; only for the right number of stars. Using a regexp to do
  22279. ;; this is so much faster than using a Lisp loop.
  22280. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  22281. (forward-char 1)
  22282. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  22283. ;; something else, do it the slow way
  22284. (while (and (not (eobp))
  22285. (or first (> (funcall outline-level) level)))
  22286. (setq first nil)
  22287. (outline-next-heading)))
  22288. (unless to-heading
  22289. (when (memq (preceding-char) '(?\n ?\^M))
  22290. ;; Go to end of line before heading
  22291. (forward-char -1)
  22292. (when (memq (preceding-char) '(?\n ?\^M))
  22293. ;; leave blank line before heading
  22294. (forward-char -1)))))
  22295. (point))
  22296. (defun org-end-of-meta-data (&optional full)
  22297. "Skip planning line and properties drawer in current entry.
  22298. When optional argument FULL is non-nil, also skip empty lines,
  22299. clocking lines and regular drawers at the beginning of the
  22300. entry."
  22301. (org-back-to-heading t)
  22302. (forward-line)
  22303. (when (org-looking-at-p org-planning-line-re) (forward-line))
  22304. (when (looking-at org-property-drawer-re)
  22305. (goto-char (match-end 0))
  22306. (forward-line))
  22307. (when (and full (not (org-at-heading-p)))
  22308. (catch 'exit
  22309. (let ((end (save-excursion (outline-next-heading) (point)))
  22310. (re (concat "[ \t]*$" "\\|" org-clock-line-re)))
  22311. (while (not (eobp))
  22312. (cond ((org-looking-at-p org-drawer-regexp)
  22313. (if (re-search-forward "^[ \t]*:END:[ \t]*$" end t)
  22314. (forward-line)
  22315. (throw 'exit t)))
  22316. ((org-looking-at-p re) (forward-line))
  22317. (t (throw 'exit t))))))))
  22318. (defun org-forward-heading-same-level (arg &optional invisible-ok)
  22319. "Move forward to the ARG'th subheading at same level as this one.
  22320. Stop at the first and last subheadings of a superior heading.
  22321. Normally this only looks at visible headings, but when INVISIBLE-OK is
  22322. non-nil it will also look at invisible ones."
  22323. (interactive "p")
  22324. (if (not (ignore-errors (org-back-to-heading invisible-ok)))
  22325. (if (and arg (< arg 0))
  22326. (goto-char (point-min))
  22327. (outline-next-heading))
  22328. (org-at-heading-p)
  22329. (let ((level (- (match-end 0) (match-beginning 0) 1))
  22330. (f (if (and arg (< arg 0))
  22331. 're-search-backward
  22332. 're-search-forward))
  22333. (count (if arg (abs arg) 1))
  22334. (result (point)))
  22335. (while (and (prog1 (> count 0)
  22336. (forward-char (if (and arg (< arg 0)) -1 1)))
  22337. (funcall f org-outline-regexp-bol nil 'move))
  22338. (let ((l (- (match-end 0) (match-beginning 0) 1)))
  22339. (cond ((< l level) (setq count 0))
  22340. ((and (= l level)
  22341. (or invisible-ok
  22342. (progn
  22343. (goto-char (line-beginning-position))
  22344. (not (outline-invisible-p)))))
  22345. (setq count (1- count))
  22346. (when (eq l level)
  22347. (setq result (point)))))))
  22348. (goto-char result))
  22349. (beginning-of-line 1)))
  22350. (defun org-backward-heading-same-level (arg &optional invisible-ok)
  22351. "Move backward to the ARG'th subheading at same level as this one.
  22352. Stop at the first and last subheadings of a superior heading."
  22353. (interactive "p")
  22354. (org-forward-heading-same-level (if arg (- arg) -1) invisible-ok))
  22355. (defun org-next-visible-heading (arg)
  22356. "Move to the next visible heading.
  22357. This function wraps `outline-next-visible-heading' with
  22358. `org-with-limited-levels' in order to skip over inline tasks and
  22359. respect customization of `org-odd-levels-only'."
  22360. (interactive "p")
  22361. (org-with-limited-levels
  22362. (outline-next-visible-heading arg)))
  22363. (defun org-previous-visible-heading (arg)
  22364. "Move to the previous visible heading.
  22365. This function wraps `outline-previous-visible-heading' with
  22366. `org-with-limited-levels' in order to skip over inline tasks and
  22367. respect customization of `org-odd-levels-only'."
  22368. (interactive "p")
  22369. (org-with-limited-levels
  22370. (outline-previous-visible-heading arg)))
  22371. (defun org-next-block (arg &optional backward block-regexp)
  22372. "Jump to the next block.
  22373. With a prefix argument ARG, jump forward ARG many blocks.
  22374. When BACKWARD is non-nil, jump to the previous block.
  22375. When BLOCK-REGEXP is non-nil, use this regexp to find blocks.
  22376. Match data is set according to this regexp when the function
  22377. returns.
  22378. Return point at beginning of the opening line of found block.
  22379. Throw an error if no block is found."
  22380. (interactive "p")
  22381. (let ((re (or block-regexp "^[ \t]*#\\+BEGIN"))
  22382. (case-fold-search t)
  22383. (search-fn (if backward #'re-search-backward #'re-search-forward))
  22384. (count (or arg 1))
  22385. (origin (point))
  22386. last-element)
  22387. (if backward (beginning-of-line) (end-of-line))
  22388. (while (and (> count 0) (funcall search-fn re nil t))
  22389. (let ((element (save-excursion
  22390. (goto-char (match-beginning 0))
  22391. (save-match-data (org-element-at-point)))))
  22392. (when (and (memq (org-element-type element)
  22393. '(center-block comment-block dynamic-block
  22394. example-block export-block quote-block
  22395. special-block src-block verse-block))
  22396. (<= (match-beginning 0)
  22397. (org-element-property :post-affiliated element)))
  22398. (setq last-element element)
  22399. (cl-decf count))))
  22400. (if (= count 0)
  22401. (prog1 (goto-char (org-element-property :post-affiliated last-element))
  22402. (save-match-data (org-show-context)))
  22403. (goto-char origin)
  22404. (user-error "No %s code blocks" (if backward "previous" "further")))))
  22405. (defun org-previous-block (arg &optional block-regexp)
  22406. "Jump to the previous block.
  22407. With a prefix argument ARG, jump backward ARG many source blocks.
  22408. When BLOCK-REGEXP is non-nil, use this regexp to find blocks."
  22409. (interactive "p")
  22410. (org-next-block arg t block-regexp))
  22411. (defun org-forward-paragraph ()
  22412. "Move forward to beginning of next paragraph or equivalent.
  22413. The function moves point to the beginning of the next visible
  22414. structural element, which can be a paragraph, a table, a list
  22415. item, etc. It also provides some special moves for convenience:
  22416. - On an affiliated keyword, jump to the beginning of the
  22417. relative element.
  22418. - On an item or a footnote definition, move to the second
  22419. element inside, if any.
  22420. - On a table or a property drawer, jump after it.
  22421. - On a verse or source block, stop after blank lines."
  22422. (interactive)
  22423. (when (eobp) (user-error "Cannot move further down"))
  22424. (let* ((deactivate-mark nil)
  22425. (element (org-element-at-point))
  22426. (type (org-element-type element))
  22427. (post-affiliated (org-element-property :post-affiliated element))
  22428. (contents-begin (org-element-property :contents-begin element))
  22429. (contents-end (org-element-property :contents-end element))
  22430. (end (let ((end (org-element-property :end element)) (parent element))
  22431. (while (and (setq parent (org-element-property :parent parent))
  22432. (= (org-element-property :contents-end parent) end))
  22433. (setq end (org-element-property :end parent)))
  22434. end)))
  22435. (cond ((not element)
  22436. (skip-chars-forward " \r\t\n")
  22437. (or (eobp) (beginning-of-line)))
  22438. ;; On affiliated keywords, move to element's beginning.
  22439. ((< (point) post-affiliated)
  22440. (goto-char post-affiliated))
  22441. ;; At a table row, move to the end of the table. Similarly,
  22442. ;; at a node property, move to the end of the property
  22443. ;; drawer.
  22444. ((memq type '(node-property table-row))
  22445. (goto-char (org-element-property
  22446. :end (org-element-property :parent element))))
  22447. ((memq type '(property-drawer table)) (goto-char end))
  22448. ;; Consider blank lines as separators in verse and source
  22449. ;; blocks to ease editing.
  22450. ((memq type '(src-block verse-block))
  22451. (when (eq type 'src-block)
  22452. (setq contents-end
  22453. (save-excursion (goto-char end)
  22454. (skip-chars-backward " \r\t\n")
  22455. (line-beginning-position))))
  22456. (beginning-of-line)
  22457. (when (looking-at "[ \t]*$") (skip-chars-forward " \r\t\n"))
  22458. (if (not (re-search-forward "^[ \t]*$" contents-end t))
  22459. (goto-char end)
  22460. (skip-chars-forward " \r\t\n")
  22461. (if (= (point) contents-end) (goto-char end)
  22462. (beginning-of-line))))
  22463. ;; With no contents, just skip element.
  22464. ((not contents-begin) (goto-char end))
  22465. ;; If contents are invisible, skip the element altogether.
  22466. ((outline-invisible-p (line-end-position))
  22467. (cl-case type
  22468. (headline
  22469. (org-with-limited-levels (outline-next-visible-heading 1)))
  22470. ;; At a plain list, make sure we move to the next item
  22471. ;; instead of skipping the whole list.
  22472. (plain-list (forward-char)
  22473. (org-forward-paragraph))
  22474. (otherwise (goto-char end))))
  22475. ((>= (point) contents-end) (goto-char end))
  22476. ((>= (point) contents-begin)
  22477. ;; This can only happen on paragraphs and plain lists.
  22478. (cl-case type
  22479. (paragraph (goto-char end))
  22480. ;; At a plain list, try to move to second element in
  22481. ;; first item, if possible.
  22482. (plain-list (end-of-line)
  22483. (org-forward-paragraph))))
  22484. ;; When contents start on the middle of a line (e.g. in
  22485. ;; items and footnote definitions), try to reach first
  22486. ;; element starting after current line.
  22487. ((> (line-end-position) contents-begin)
  22488. (end-of-line)
  22489. (org-forward-paragraph))
  22490. (t (goto-char contents-begin)))))
  22491. (defun org-backward-paragraph ()
  22492. "Move backward to start of previous paragraph or equivalent.
  22493. The function moves point to the beginning of the current
  22494. structural element, which can be a paragraph, a table, a list
  22495. item, etc., or to the beginning of the previous visible one if
  22496. point is already there. It also provides some special moves for
  22497. convenience:
  22498. - On an affiliated keyword, jump to the first one.
  22499. - On a table or a property drawer, move to its beginning.
  22500. - On a verse or source block, stop before blank lines."
  22501. (interactive)
  22502. (when (bobp) (user-error "Cannot move further up"))
  22503. (let* ((deactivate-mark nil)
  22504. (element (org-element-at-point))
  22505. (type (org-element-type element))
  22506. (contents-begin (org-element-property :contents-begin element))
  22507. (contents-end (org-element-property :contents-end element))
  22508. (post-affiliated (org-element-property :post-affiliated element))
  22509. (begin (org-element-property :begin element)))
  22510. (cond
  22511. ((not element) (goto-char (point-min)))
  22512. ((= (point) begin)
  22513. (backward-char)
  22514. (org-backward-paragraph))
  22515. ((<= (point) post-affiliated) (goto-char begin))
  22516. ((memq type '(node-property table-row))
  22517. (goto-char (org-element-property
  22518. :post-affiliated (org-element-property :parent element))))
  22519. ((memq type '(property-drawer table)) (goto-char begin))
  22520. ((memq type '(src-block verse-block))
  22521. (when (eq type 'src-block)
  22522. (setq contents-begin
  22523. (save-excursion (goto-char begin) (forward-line) (point))))
  22524. (if (= (point) contents-begin) (goto-char post-affiliated)
  22525. ;; Inside a verse block, see blank lines as paragraph
  22526. ;; separators.
  22527. (let ((origin (point)))
  22528. (skip-chars-backward " \r\t\n" contents-begin)
  22529. (when (re-search-backward "^[ \t]*$" contents-begin 'move)
  22530. (skip-chars-forward " \r\t\n" origin)
  22531. (if (= (point) origin) (goto-char contents-begin)
  22532. (beginning-of-line))))))
  22533. ((not contents-begin) (goto-char (or post-affiliated begin)))
  22534. ((eq type 'paragraph)
  22535. (goto-char contents-begin)
  22536. ;; When at first paragraph in an item or a footnote definition,
  22537. ;; move directly to beginning of line.
  22538. (let ((parent-contents
  22539. (org-element-property
  22540. :contents-begin (org-element-property :parent element))))
  22541. (when (and parent-contents (= parent-contents contents-begin))
  22542. (beginning-of-line))))
  22543. ;; At the end of a greater element, move to the beginning of the
  22544. ;; last element within.
  22545. ((>= (point) contents-end)
  22546. (goto-char (1- contents-end))
  22547. (org-backward-paragraph))
  22548. (t (goto-char (or post-affiliated begin))))
  22549. ;; Ensure we never leave point invisible.
  22550. (when (outline-invisible-p (point)) (beginning-of-visual-line))))
  22551. (defun org-forward-element ()
  22552. "Move forward by one element.
  22553. Move to the next element at the same level, when possible."
  22554. (interactive)
  22555. (cond ((eobp) (user-error "Cannot move further down"))
  22556. ((org-with-limited-levels (org-at-heading-p))
  22557. (let ((origin (point)))
  22558. (goto-char (org-end-of-subtree nil t))
  22559. (unless (org-with-limited-levels (org-at-heading-p))
  22560. (goto-char origin)
  22561. (user-error "Cannot move further down"))))
  22562. (t
  22563. (let* ((elem (org-element-at-point))
  22564. (end (org-element-property :end elem))
  22565. (parent (org-element-property :parent elem)))
  22566. (cond ((and parent (= (org-element-property :contents-end parent) end))
  22567. (goto-char (org-element-property :end parent)))
  22568. ((integer-or-marker-p end) (goto-char end))
  22569. (t (message "No element at point")))))))
  22570. (defun org-backward-element ()
  22571. "Move backward by one element.
  22572. Move to the previous element at the same level, when possible."
  22573. (interactive)
  22574. (cond ((bobp) (user-error "Cannot move further up"))
  22575. ((org-with-limited-levels (org-at-heading-p))
  22576. ;; At a headline, move to the previous one, if any, or stay
  22577. ;; here.
  22578. (let ((origin (point)))
  22579. (org-with-limited-levels (org-backward-heading-same-level 1))
  22580. ;; When current headline has no sibling above, move to its
  22581. ;; parent.
  22582. (when (= (point) origin)
  22583. (or (org-with-limited-levels (org-up-heading-safe))
  22584. (progn (goto-char origin)
  22585. (user-error "Cannot move further up"))))))
  22586. (t
  22587. (let* ((elem (org-element-at-point))
  22588. (beg (org-element-property :begin elem)))
  22589. (cond
  22590. ;; Move to beginning of current element if point isn't
  22591. ;; there already.
  22592. ((null beg) (message "No element at point"))
  22593. ((/= (point) beg) (goto-char beg))
  22594. (t (goto-char beg)
  22595. (skip-chars-backward " \r\t\n")
  22596. (unless (bobp)
  22597. (let ((prev (org-element-at-point)))
  22598. (goto-char (org-element-property :begin prev))
  22599. (while (and (setq prev (org-element-property :parent prev))
  22600. (<= (org-element-property :end prev) beg))
  22601. (goto-char (org-element-property :begin prev)))))))))))
  22602. (defun org-up-element ()
  22603. "Move to upper element."
  22604. (interactive)
  22605. (if (org-with-limited-levels (org-at-heading-p))
  22606. (unless (org-up-heading-safe) (user-error "No surrounding element"))
  22607. (let* ((elem (org-element-at-point))
  22608. (parent (org-element-property :parent elem)))
  22609. (if parent (goto-char (org-element-property :begin parent))
  22610. (if (org-with-limited-levels (org-before-first-heading-p))
  22611. (user-error "No surrounding element")
  22612. (org-with-limited-levels (org-back-to-heading)))))))
  22613. (defvar org-element-greater-elements)
  22614. (defun org-down-element ()
  22615. "Move to inner element."
  22616. (interactive)
  22617. (let ((element (org-element-at-point)))
  22618. (cond
  22619. ((memq (org-element-type element) '(plain-list table))
  22620. (goto-char (org-element-property :contents-begin element))
  22621. (forward-char))
  22622. ((memq (org-element-type element) org-element-greater-elements)
  22623. ;; If contents are hidden, first disclose them.
  22624. (when (outline-invisible-p (line-end-position)) (org-cycle))
  22625. (goto-char (or (org-element-property :contents-begin element)
  22626. (user-error "No content for this element"))))
  22627. (t (user-error "No inner element")))))
  22628. (defun org-drag-element-backward ()
  22629. "Move backward element at point."
  22630. (interactive)
  22631. (if (org-with-limited-levels (org-at-heading-p)) (org-move-subtree-up)
  22632. (let* ((elem (org-element-at-point))
  22633. (prev-elem
  22634. (save-excursion
  22635. (goto-char (org-element-property :begin elem))
  22636. (skip-chars-backward " \r\t\n")
  22637. (unless (bobp)
  22638. (let* ((beg (org-element-property :begin elem))
  22639. (prev (org-element-at-point))
  22640. (up prev))
  22641. (while (and (setq up (org-element-property :parent up))
  22642. (<= (org-element-property :end up) beg))
  22643. (setq prev up))
  22644. prev)))))
  22645. ;; Error out if no previous element or previous element is
  22646. ;; a parent of the current one.
  22647. (if (or (not prev-elem) (org-element-nested-p elem prev-elem))
  22648. (user-error "Cannot drag element backward")
  22649. (let ((pos (point)))
  22650. (org-element-swap-A-B prev-elem elem)
  22651. (goto-char (+ (org-element-property :begin prev-elem)
  22652. (- pos (org-element-property :begin elem)))))))))
  22653. (defun org-drag-element-forward ()
  22654. "Move forward element at point."
  22655. (interactive)
  22656. (let* ((pos (point))
  22657. (elem (org-element-at-point)))
  22658. (when (= (point-max) (org-element-property :end elem))
  22659. (user-error "Cannot drag element forward"))
  22660. (goto-char (org-element-property :end elem))
  22661. (let ((next-elem (org-element-at-point)))
  22662. (when (or (org-element-nested-p elem next-elem)
  22663. (and (eq (org-element-type next-elem) 'headline)
  22664. (not (eq (org-element-type elem) 'headline))))
  22665. (goto-char pos)
  22666. (user-error "Cannot drag element forward"))
  22667. ;; Compute new position of point: it's shifted by NEXT-ELEM
  22668. ;; body's length (without final blanks) and by the length of
  22669. ;; blanks between ELEM and NEXT-ELEM.
  22670. (let ((size-next (- (save-excursion
  22671. (goto-char (org-element-property :end next-elem))
  22672. (skip-chars-backward " \r\t\n")
  22673. (forward-line)
  22674. ;; Small correction if buffer doesn't end
  22675. ;; with a newline character.
  22676. (if (and (eolp) (not (bolp))) (1+ (point)) (point)))
  22677. (org-element-property :begin next-elem)))
  22678. (size-blank (- (org-element-property :end elem)
  22679. (save-excursion
  22680. (goto-char (org-element-property :end elem))
  22681. (skip-chars-backward " \r\t\n")
  22682. (forward-line)
  22683. (point)))))
  22684. (org-element-swap-A-B elem next-elem)
  22685. (goto-char (+ pos size-next size-blank))))))
  22686. (defun org-drag-line-forward (arg)
  22687. "Drag the line at point ARG lines forward."
  22688. (interactive "p")
  22689. (dotimes (_ (abs arg))
  22690. (let ((c (current-column)))
  22691. (if (< 0 arg)
  22692. (progn
  22693. (beginning-of-line 2)
  22694. (transpose-lines 1)
  22695. (beginning-of-line 0))
  22696. (transpose-lines 1)
  22697. (beginning-of-line -1))
  22698. (org-move-to-column c))))
  22699. (defun org-drag-line-backward (arg)
  22700. "Drag the line at point ARG lines backward."
  22701. (interactive "p")
  22702. (org-drag-line-forward (- arg)))
  22703. (defun org-mark-element ()
  22704. "Put point at beginning of this element, mark at end.
  22705. Interactively, if this command is repeated or (in Transient Mark
  22706. mode) if the mark is active, it marks the next element after the
  22707. ones already marked."
  22708. (interactive)
  22709. (let (deactivate-mark)
  22710. (if (and (org-called-interactively-p 'any)
  22711. (or (and (eq last-command this-command) (mark t))
  22712. (and transient-mark-mode mark-active)))
  22713. (set-mark
  22714. (save-excursion
  22715. (goto-char (mark))
  22716. (goto-char (org-element-property :end (org-element-at-point)))))
  22717. (let ((element (org-element-at-point)))
  22718. (end-of-line)
  22719. (push-mark (org-element-property :end element) t t)
  22720. (goto-char (org-element-property :begin element))))))
  22721. (defun org-narrow-to-element ()
  22722. "Narrow buffer to current element."
  22723. (interactive)
  22724. (let ((elem (org-element-at-point)))
  22725. (cond
  22726. ((eq (car elem) 'headline)
  22727. (narrow-to-region
  22728. (org-element-property :begin elem)
  22729. (org-element-property :end elem)))
  22730. ((memq (car elem) org-element-greater-elements)
  22731. (narrow-to-region
  22732. (org-element-property :contents-begin elem)
  22733. (org-element-property :contents-end elem)))
  22734. (t
  22735. (narrow-to-region
  22736. (org-element-property :begin elem)
  22737. (org-element-property :end elem))))))
  22738. (defun org-transpose-element ()
  22739. "Transpose current and previous elements, keeping blank lines between.
  22740. Point is moved after both elements."
  22741. (interactive)
  22742. (org-skip-whitespace)
  22743. (let ((end (org-element-property :end (org-element-at-point))))
  22744. (org-drag-element-backward)
  22745. (goto-char end)))
  22746. (defun org-unindent-buffer ()
  22747. "Un-indent the visible part of the buffer.
  22748. Relative indentation (between items, inside blocks, etc.) isn't
  22749. modified."
  22750. (interactive)
  22751. (unless (eq major-mode 'org-mode)
  22752. (user-error "Cannot un-indent a buffer not in Org mode"))
  22753. (letrec ((parse-tree (org-element-parse-buffer 'greater-element))
  22754. (unindent-tree
  22755. (lambda (contents)
  22756. (dolist (element (reverse contents))
  22757. (if (memq (org-element-type element) '(headline section))
  22758. (funcall unindent-tree (org-element-contents element))
  22759. (save-excursion
  22760. (save-restriction
  22761. (narrow-to-region
  22762. (org-element-property :begin element)
  22763. (org-element-property :end element))
  22764. (org-do-remove-indentation))))))))
  22765. (funcall unindent-tree (org-element-contents parse-tree))))
  22766. (defun org-show-children (&optional level)
  22767. "Show all direct subheadings of this heading.
  22768. Prefix arg LEVEL is how many levels below the current level
  22769. should be shown. Default is enough to cause the following
  22770. heading to appear."
  22771. (interactive "p")
  22772. ;; If `orgstruct-mode' is active, use the slower version.
  22773. (if orgstruct-mode (call-interactively #'outline-show-children)
  22774. (save-excursion
  22775. (org-back-to-heading t)
  22776. (let* ((current-level (funcall outline-level))
  22777. (max-level (org-get-valid-level
  22778. current-level
  22779. (if level (prefix-numeric-value level) 1)))
  22780. (end (save-excursion (org-end-of-subtree t t)))
  22781. (regexp-fmt "^\\*\\{%d,%s\\}\\(?: \\|$\\)")
  22782. (past-first-child nil)
  22783. ;; Make sure to skip inlinetasks.
  22784. (re (format regexp-fmt
  22785. current-level
  22786. (cond
  22787. ((not (featurep 'org-inlinetask)) "")
  22788. (org-odd-levels-only (- (* 2 org-inlinetask-min-level)
  22789. 3))
  22790. (t (1- org-inlinetask-min-level))))))
  22791. ;; Display parent heading.
  22792. (outline-flag-region (line-end-position 0) (line-end-position) nil)
  22793. (forward-line)
  22794. ;; Display children. First child may be deeper than expected
  22795. ;; MAX-LEVEL. Since we want to display it anyway, adjust
  22796. ;; MAX-LEVEL accordingly.
  22797. (while (re-search-forward re end t)
  22798. (unless past-first-child
  22799. (setq re (format regexp-fmt
  22800. current-level
  22801. (max (funcall outline-level) max-level)))
  22802. (setq past-first-child t))
  22803. (outline-flag-region
  22804. (line-end-position 0) (line-end-position) nil))))))
  22805. (defun org-show-subtree ()
  22806. "Show everything after this heading at deeper levels."
  22807. (interactive)
  22808. (outline-flag-region
  22809. (point)
  22810. (save-excursion
  22811. (org-end-of-subtree t t))
  22812. nil))
  22813. (defun org-show-entry ()
  22814. "Show the body directly following this heading.
  22815. Show the heading too, if it is currently invisible."
  22816. (interactive)
  22817. (save-excursion
  22818. (ignore-errors
  22819. (org-back-to-heading t)
  22820. (outline-flag-region
  22821. (max (point-min) (1- (point)))
  22822. (save-excursion
  22823. (if (re-search-forward
  22824. (concat "[\r\n]\\(" org-outline-regexp "\\)") nil t)
  22825. (match-beginning 1)
  22826. (point-max)))
  22827. nil)
  22828. (org-cycle-hide-drawers 'children))))
  22829. (defun org-make-options-regexp (kwds &optional extra)
  22830. "Make a regular expression for keyword lines.
  22831. KWDS is a list of keywords, as strings. Optional argument EXTRA,
  22832. when non-nil, is a regexp matching keywords names."
  22833. (concat "^[ \t]*#\\+\\("
  22834. (regexp-opt kwds)
  22835. (and extra (concat (and kwds "\\|") extra))
  22836. "\\):[ \t]*\\(.*\\)"))
  22837. ;;;; Integration with and fixes for other packages
  22838. ;;; Imenu support
  22839. (defvar-local org-imenu-markers nil
  22840. "All markers currently used by Imenu.")
  22841. (defun org-imenu-new-marker (&optional pos)
  22842. "Return a new marker for use by Imenu, and remember the marker."
  22843. (let ((m (make-marker)))
  22844. (move-marker m (or pos (point)))
  22845. (push m org-imenu-markers)
  22846. m))
  22847. (defun org-imenu-get-tree ()
  22848. "Produce the index for Imenu."
  22849. (dolist (x org-imenu-markers) (move-marker x nil))
  22850. (setq org-imenu-markers nil)
  22851. (let* ((n org-imenu-depth)
  22852. (re (concat "^" (org-get-limited-outline-regexp)))
  22853. (subs (make-vector (1+ n) nil))
  22854. (last-level 0)
  22855. m level head0 head)
  22856. (save-excursion
  22857. (save-restriction
  22858. (widen)
  22859. (goto-char (point-max))
  22860. (while (re-search-backward re nil t)
  22861. (setq level (org-reduced-level (funcall outline-level)))
  22862. (when (and (<= level n)
  22863. (looking-at org-complex-heading-regexp)
  22864. (setq head0 (org-match-string-no-properties 4)))
  22865. (setq head (org-link-display-format head0)
  22866. m (org-imenu-new-marker))
  22867. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  22868. (if (>= level last-level)
  22869. (push (cons head m) (aref subs level))
  22870. (push (cons head (aref subs (1+ level))) (aref subs level))
  22871. (cl-loop for i from (1+ level) to n do (aset subs i nil)))
  22872. (setq last-level level)))))
  22873. (aref subs 1)))
  22874. (eval-after-load "imenu"
  22875. '(progn
  22876. (add-hook 'imenu-after-jump-hook
  22877. (lambda ()
  22878. (when (derived-mode-p 'org-mode)
  22879. (org-show-context 'org-goto))))))
  22880. (defun org-link-display-format (s)
  22881. "Replace links in string S with their description.
  22882. If there is no description, use the link target."
  22883. (save-match-data
  22884. (replace-regexp-in-string
  22885. org-bracket-link-analytic-regexp
  22886. (lambda (m)
  22887. (if (match-end 5) (match-string 5 m)
  22888. (concat (match-string 1 m) (match-string 3 m))))
  22889. s nil t)))
  22890. (defun org-toggle-link-display ()
  22891. "Toggle the literal or descriptive display of links."
  22892. (interactive)
  22893. (if org-descriptive-links
  22894. (progn (org-remove-from-invisibility-spec '(org-link))
  22895. (org-restart-font-lock)
  22896. (setq org-descriptive-links nil))
  22897. (progn (add-to-invisibility-spec '(org-link))
  22898. (org-restart-font-lock)
  22899. (setq org-descriptive-links t))))
  22900. ;; Speedbar support
  22901. (defvar org-speedbar-restriction-lock-overlay (make-overlay 1 1)
  22902. "Overlay marking the agenda restriction line in speedbar.")
  22903. (overlay-put org-speedbar-restriction-lock-overlay
  22904. 'face 'org-agenda-restriction-lock)
  22905. (overlay-put org-speedbar-restriction-lock-overlay
  22906. 'help-echo "Agendas are currently limited to this item.")
  22907. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  22908. (defun org-speedbar-set-agenda-restriction ()
  22909. "Restrict future agenda commands to the location at point in speedbar.
  22910. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  22911. (interactive)
  22912. (require 'org-agenda)
  22913. (let (p m tp np dir txt)
  22914. (cond
  22915. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  22916. 'org-imenu t))
  22917. (setq m (get-text-property p 'org-imenu-marker))
  22918. (with-current-buffer (marker-buffer m)
  22919. (goto-char m)
  22920. (org-agenda-set-restriction-lock 'subtree)))
  22921. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  22922. 'speedbar-function 'speedbar-find-file))
  22923. (setq tp (previous-single-property-change
  22924. (1+ p) 'speedbar-function)
  22925. np (next-single-property-change
  22926. tp 'speedbar-function)
  22927. dir (speedbar-line-directory)
  22928. txt (buffer-substring-no-properties (or tp (point-min))
  22929. (or np (point-max))))
  22930. (with-current-buffer (find-file-noselect
  22931. (let ((default-directory dir))
  22932. (expand-file-name txt)))
  22933. (unless (derived-mode-p 'org-mode)
  22934. (user-error "Cannot restrict to non-Org-mode file"))
  22935. (org-agenda-set-restriction-lock 'file)))
  22936. (t (user-error "Don't know how to restrict Org-mode's agenda")))
  22937. (move-overlay org-speedbar-restriction-lock-overlay
  22938. (point-at-bol) (point-at-eol))
  22939. (setq current-prefix-arg nil)
  22940. (org-agenda-maybe-redo)))
  22941. (defvar speedbar-file-key-map)
  22942. (declare-function speedbar-add-supported-extension "speedbar" (extension))
  22943. (eval-after-load "speedbar"
  22944. '(progn
  22945. (speedbar-add-supported-extension ".org")
  22946. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  22947. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  22948. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  22949. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  22950. (add-hook 'speedbar-visiting-tag-hook
  22951. (lambda () (and (derived-mode-p 'org-mode) (org-show-context 'org-goto))))))
  22952. ;;; Fixes and Hacks for problems with other packages
  22953. (defun org--flyspell-object-check-p (element)
  22954. "Non-nil when Flyspell can check object at point.
  22955. ELEMENT is the element at point."
  22956. (let ((object (save-excursion
  22957. (when (org-looking-at-p "\\>") (backward-char))
  22958. (org-element-context element))))
  22959. (cl-case (org-element-type object)
  22960. ;; Prevent checks in links due to keybinding conflict with
  22961. ;; Flyspell.
  22962. ((code entity export-snippet inline-babel-call
  22963. inline-src-block line-break latex-fragment link macro
  22964. statistics-cookie target timestamp verbatim)
  22965. nil)
  22966. (footnote-reference
  22967. ;; Only in inline footnotes, within the definition.
  22968. (and (eq (org-element-property :type object) 'inline)
  22969. (< (save-excursion
  22970. (goto-char (org-element-property :begin object))
  22971. (search-forward ":" nil t 2))
  22972. (point))))
  22973. (otherwise t))))
  22974. (defun org-mode-flyspell-verify ()
  22975. "Function used for `flyspell-generic-check-word-predicate'."
  22976. (if (org-at-heading-p)
  22977. ;; At a headline or an inlinetask, check title only. This is
  22978. ;; faster than relying on `org-element-at-point'.
  22979. (and (save-excursion (beginning-of-line)
  22980. (and (let ((case-fold-search t))
  22981. (not (looking-at "\\*+ END[ \t]*$")))
  22982. (looking-at org-complex-heading-regexp)))
  22983. (match-beginning 4)
  22984. (>= (point) (match-beginning 4))
  22985. (or (not (match-beginning 5))
  22986. (< (point) (match-beginning 5))))
  22987. (let* ((element (org-element-at-point))
  22988. (post-affiliated (org-element-property :post-affiliated element)))
  22989. (cond
  22990. ;; Ignore checks in all affiliated keywords but captions.
  22991. ((< (point) post-affiliated)
  22992. (and (save-excursion
  22993. (beginning-of-line)
  22994. (let ((case-fold-search t)) (looking-at "[ \t]*#\\+CAPTION:")))
  22995. (> (point) (match-end 0))
  22996. (org--flyspell-object-check-p element)))
  22997. ;; Ignore checks in LOGBOOK (or equivalent) drawer.
  22998. ((let ((log (org-log-into-drawer)))
  22999. (and log
  23000. (let ((drawer (org-element-lineage element '(drawer))))
  23001. (and drawer
  23002. (eq (compare-strings
  23003. log nil nil
  23004. (org-element-property :drawer-name drawer) nil nil t)
  23005. t)))))
  23006. nil)
  23007. (t
  23008. (cl-case (org-element-type element)
  23009. ((comment quote-section) t)
  23010. (comment-block
  23011. ;; Allow checks between block markers, not on them.
  23012. (and (> (line-beginning-position) post-affiliated)
  23013. (save-excursion
  23014. (end-of-line)
  23015. (skip-chars-forward " \r\t\n")
  23016. (< (point) (org-element-property :end element)))))
  23017. ;; Arbitrary list of keywords where checks are meaningful.
  23018. ;; Make sure point is on the value part of the element.
  23019. (keyword
  23020. (and (member (org-element-property :key element)
  23021. '("DESCRIPTION" "TITLE"))
  23022. (save-excursion
  23023. (search-backward ":" (line-beginning-position) t))))
  23024. ;; Check is globally allowed in paragraphs verse blocks and
  23025. ;; table rows (after affiliated keywords) but some objects
  23026. ;; must not be affected.
  23027. ((paragraph table-row verse-block)
  23028. (let ((cbeg (org-element-property :contents-begin element))
  23029. (cend (org-element-property :contents-end element)))
  23030. (and cbeg (>= (point) cbeg) (< (point) cend)
  23031. (org--flyspell-object-check-p element))))))))))
  23032. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  23033. (defun org-remove-flyspell-overlays-in (beg end)
  23034. "Remove flyspell overlays in region."
  23035. (and (org-bound-and-true-p flyspell-mode)
  23036. (fboundp 'flyspell-delete-region-overlays)
  23037. (flyspell-delete-region-overlays beg end)))
  23038. (defvar flyspell-delayed-commands)
  23039. (eval-after-load "flyspell"
  23040. '(add-to-list 'flyspell-delayed-commands 'org-self-insert-command))
  23041. ;; Make `bookmark-jump' shows the jump location if it was hidden.
  23042. (eval-after-load "bookmark"
  23043. '(if (boundp 'bookmark-after-jump-hook)
  23044. ;; We can use the hook
  23045. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  23046. ;; Hook not available, use advice
  23047. (defadvice bookmark-jump (after org-make-visible activate)
  23048. "Make the position visible."
  23049. (org-bookmark-jump-unhide))))
  23050. ;; Make sure saveplace shows the location if it was hidden
  23051. (eval-after-load "saveplace"
  23052. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  23053. "Make the position visible."
  23054. (org-bookmark-jump-unhide)))
  23055. ;; Make sure ecb shows the location if it was hidden
  23056. (eval-after-load "ecb"
  23057. '(defadvice ecb-method-clicked (after esf/org-show-context activate)
  23058. "Make hierarchy visible when jumping into location from ECB tree buffer."
  23059. (when (derived-mode-p 'org-mode)
  23060. (org-show-context))))
  23061. (defun org-bookmark-jump-unhide ()
  23062. "Unhide the current position, to show the bookmark location."
  23063. (and (derived-mode-p 'org-mode)
  23064. (or (outline-invisible-p)
  23065. (save-excursion (goto-char (max (point-min) (1- (point))))
  23066. (outline-invisible-p)))
  23067. (org-show-context 'bookmark-jump)))
  23068. (defun org-mark-jump-unhide ()
  23069. "Make the point visible with `org-show-context' after jumping to the mark."
  23070. (when (and (derived-mode-p 'org-mode)
  23071. (outline-invisible-p))
  23072. (org-show-context 'mark-goto)))
  23073. (eval-after-load "simple"
  23074. '(defadvice pop-to-mark-command (after org-make-visible activate)
  23075. "Make the point visible with `org-show-context'."
  23076. (org-mark-jump-unhide)))
  23077. (eval-after-load "simple"
  23078. '(defadvice exchange-point-and-mark (after org-make-visible activate)
  23079. "Make the point visible with `org-show-context'."
  23080. (org-mark-jump-unhide)))
  23081. (eval-after-load "simple"
  23082. '(defadvice pop-global-mark (after org-make-visible activate)
  23083. "Make the point visible with `org-show-context'."
  23084. (org-mark-jump-unhide)))
  23085. ;; Make session.el ignore our circular variable
  23086. (defvar session-globals-exclude)
  23087. (eval-after-load "session"
  23088. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  23089. ;;;; Finish up
  23090. (provide 'org)
  23091. (run-hooks 'org-load-hook)
  23092. ;;; org.el ends here