org.el 954 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783177841778517786177871778817789177901779117792177931779417795177961779717798177991780017801178021780317804178051780617807178081780917810178111781217813178141781517816178171781817819178201782117822178231782417825178261782717828178291783017831178321783317834178351783617837178381783917840178411784217843178441784517846178471784817849178501785117852178531785417855178561785717858178591786017861178621786317864178651786617867178681786917870178711787217873178741787517876178771787817879178801788117882178831788417885178861788717888178891789017891178921789317894178951789617897178981789917900179011790217903179041790517906179071790817909179101791117912179131791417915179161791717918179191792017921179221792317924179251792617927179281792917930179311793217933179341793517936179371793817939179401794117942179431794417945179461794717948179491795017951179521795317954179551795617957179581795917960179611796217963179641796517966179671796817969179701797117972179731797417975179761797717978179791798017981179821798317984179851798617987179881798917990179911799217993179941799517996179971799817999180001800118002180031800418005180061800718008180091801018011180121801318014180151801618017180181801918020180211802218023180241802518026180271802818029180301803118032180331803418035180361803718038180391804018041180421804318044180451804618047180481804918050180511805218053180541805518056180571805818059180601806118062180631806418065180661806718068180691807018071180721807318074180751807618077180781807918080180811808218083180841808518086180871808818089180901809118092180931809418095180961809718098180991810018101181021810318104181051810618107181081810918110181111811218113181141811518116181171811818119181201812118122181231812418125181261812718128181291813018131181321813318134181351813618137181381813918140181411814218143181441814518146181471814818149181501815118152181531815418155181561815718158181591816018161181621816318164181651816618167181681816918170181711817218173181741817518176181771817818179181801818118182181831818418185181861818718188181891819018191181921819318194181951819618197181981819918200182011820218203182041820518206182071820818209182101821118212182131821418215182161821718218182191822018221182221822318224182251822618227182281822918230182311823218233182341823518236182371823818239182401824118242182431824418245182461824718248182491825018251182521825318254182551825618257182581825918260182611826218263182641826518266182671826818269182701827118272182731827418275182761827718278182791828018281182821828318284182851828618287182881828918290182911829218293182941829518296182971829818299183001830118302183031830418305183061830718308183091831018311183121831318314183151831618317183181831918320183211832218323183241832518326183271832818329183301833118332183331833418335183361833718338183391834018341183421834318344183451834618347183481834918350183511835218353183541835518356183571835818359183601836118362183631836418365183661836718368183691837018371183721837318374183751837618377183781837918380183811838218383183841838518386183871838818389183901839118392183931839418395183961839718398183991840018401184021840318404184051840618407184081840918410184111841218413184141841518416184171841818419184201842118422184231842418425184261842718428184291843018431184321843318434184351843618437184381843918440184411844218443184441844518446184471844818449184501845118452184531845418455184561845718458184591846018461184621846318464184651846618467184681846918470184711847218473184741847518476184771847818479184801848118482184831848418485184861848718488184891849018491184921849318494184951849618497184981849918500185011850218503185041850518506185071850818509185101851118512185131851418515185161851718518185191852018521185221852318524185251852618527185281852918530185311853218533185341853518536185371853818539185401854118542185431854418545185461854718548185491855018551185521855318554185551855618557185581855918560185611856218563185641856518566185671856818569185701857118572185731857418575185761857718578185791858018581185821858318584185851858618587185881858918590185911859218593185941859518596185971859818599186001860118602186031860418605186061860718608186091861018611186121861318614186151861618617186181861918620186211862218623186241862518626186271862818629186301863118632186331863418635186361863718638186391864018641186421864318644186451864618647186481864918650186511865218653186541865518656186571865818659186601866118662186631866418665186661866718668186691867018671186721867318674186751867618677186781867918680186811868218683186841868518686186871868818689186901869118692186931869418695186961869718698186991870018701187021870318704187051870618707187081870918710187111871218713187141871518716187171871818719187201872118722187231872418725187261872718728187291873018731187321873318734187351873618737187381873918740187411874218743187441874518746187471874818749187501875118752187531875418755187561875718758187591876018761187621876318764187651876618767187681876918770187711877218773187741877518776187771877818779187801878118782187831878418785187861878718788187891879018791187921879318794187951879618797187981879918800188011880218803188041880518806188071880818809188101881118812188131881418815188161881718818188191882018821188221882318824188251882618827188281882918830188311883218833188341883518836188371883818839188401884118842188431884418845188461884718848188491885018851188521885318854188551885618857188581885918860188611886218863188641886518866188671886818869188701887118872188731887418875188761887718878188791888018881188821888318884188851888618887188881888918890188911889218893188941889518896188971889818899189001890118902189031890418905189061890718908189091891018911189121891318914189151891618917189181891918920189211892218923189241892518926189271892818929189301893118932189331893418935189361893718938189391894018941189421894318944189451894618947189481894918950189511895218953189541895518956189571895818959189601896118962189631896418965189661896718968189691897018971189721897318974189751897618977189781897918980189811898218983189841898518986189871898818989189901899118992189931899418995189961899718998189991900019001190021900319004190051900619007190081900919010190111901219013190141901519016190171901819019190201902119022190231902419025190261902719028190291903019031190321903319034190351903619037190381903919040190411904219043190441904519046190471904819049190501905119052190531905419055190561905719058190591906019061190621906319064190651906619067190681906919070190711907219073190741907519076190771907819079190801908119082190831908419085190861908719088190891909019091190921909319094190951909619097190981909919100191011910219103191041910519106191071910819109191101911119112191131911419115191161911719118191191912019121191221912319124191251912619127191281912919130191311913219133191341913519136191371913819139191401914119142191431914419145191461914719148191491915019151191521915319154191551915619157191581915919160191611916219163191641916519166191671916819169191701917119172191731917419175191761917719178191791918019181191821918319184191851918619187191881918919190191911919219193191941919519196191971919819199192001920119202192031920419205192061920719208192091921019211192121921319214192151921619217192181921919220192211922219223192241922519226192271922819229192301923119232192331923419235192361923719238192391924019241192421924319244192451924619247192481924919250192511925219253192541925519256192571925819259192601926119262192631926419265192661926719268192691927019271192721927319274192751927619277192781927919280192811928219283192841928519286192871928819289192901929119292192931929419295192961929719298192991930019301193021930319304193051930619307193081930919310193111931219313193141931519316193171931819319193201932119322193231932419325193261932719328193291933019331193321933319334193351933619337193381933919340193411934219343193441934519346193471934819349193501935119352193531935419355193561935719358193591936019361193621936319364193651936619367193681936919370193711937219373193741937519376193771937819379193801938119382193831938419385193861938719388193891939019391193921939319394193951939619397193981939919400194011940219403194041940519406194071940819409194101941119412194131941419415194161941719418194191942019421194221942319424194251942619427194281942919430194311943219433194341943519436194371943819439194401944119442194431944419445194461944719448194491945019451194521945319454194551945619457194581945919460194611946219463194641946519466194671946819469194701947119472194731947419475194761947719478194791948019481194821948319484194851948619487194881948919490194911949219493194941949519496194971949819499195001950119502195031950419505195061950719508195091951019511195121951319514195151951619517195181951919520195211952219523195241952519526195271952819529195301953119532195331953419535195361953719538195391954019541195421954319544195451954619547195481954919550195511955219553195541955519556195571955819559195601956119562195631956419565195661956719568195691957019571195721957319574195751957619577195781957919580195811958219583195841958519586195871958819589195901959119592195931959419595195961959719598195991960019601196021960319604196051960619607196081960919610196111961219613196141961519616196171961819619196201962119622196231962419625196261962719628196291963019631196321963319634196351963619637196381963919640196411964219643196441964519646196471964819649196501965119652196531965419655196561965719658196591966019661196621966319664196651966619667196681966919670196711967219673196741967519676196771967819679196801968119682196831968419685196861968719688196891969019691196921969319694196951969619697196981969919700197011970219703197041970519706197071970819709197101971119712197131971419715197161971719718197191972019721197221972319724197251972619727197281972919730197311973219733197341973519736197371973819739197401974119742197431974419745197461974719748197491975019751197521975319754197551975619757197581975919760197611976219763197641976519766197671976819769197701977119772197731977419775197761977719778197791978019781197821978319784197851978619787197881978919790197911979219793197941979519796197971979819799198001980119802198031980419805198061980719808198091981019811198121981319814198151981619817198181981919820198211982219823198241982519826198271982819829198301983119832198331983419835198361983719838198391984019841198421984319844198451984619847198481984919850198511985219853198541985519856198571985819859198601986119862198631986419865198661986719868198691987019871198721987319874198751987619877198781987919880198811988219883198841988519886198871988819889198901989119892198931989419895198961989719898198991990019901199021990319904199051990619907199081990919910199111991219913199141991519916199171991819919199201992119922199231992419925199261992719928199291993019931199321993319934199351993619937199381993919940199411994219943199441994519946199471994819949199501995119952199531995419955199561995719958199591996019961199621996319964199651996619967199681996919970199711997219973199741997519976199771997819979199801998119982199831998419985199861998719988199891999019991199921999319994199951999619997199981999920000200012000220003200042000520006200072000820009200102001120012200132001420015200162001720018200192002020021200222002320024200252002620027200282002920030200312003220033200342003520036200372003820039200402004120042200432004420045200462004720048200492005020051200522005320054200552005620057200582005920060200612006220063200642006520066200672006820069200702007120072200732007420075200762007720078200792008020081200822008320084200852008620087200882008920090200912009220093200942009520096200972009820099201002010120102201032010420105201062010720108201092011020111201122011320114201152011620117201182011920120201212012220123201242012520126201272012820129201302013120132201332013420135201362013720138201392014020141201422014320144201452014620147201482014920150201512015220153201542015520156201572015820159201602016120162201632016420165201662016720168201692017020171201722017320174201752017620177201782017920180201812018220183201842018520186201872018820189201902019120192201932019420195201962019720198201992020020201202022020320204202052020620207202082020920210202112021220213202142021520216202172021820219202202022120222202232022420225202262022720228202292023020231202322023320234202352023620237202382023920240202412024220243202442024520246202472024820249202502025120252202532025420255202562025720258202592026020261202622026320264202652026620267202682026920270202712027220273202742027520276202772027820279202802028120282202832028420285202862028720288202892029020291202922029320294202952029620297202982029920300203012030220303203042030520306203072030820309203102031120312203132031420315203162031720318203192032020321203222032320324203252032620327203282032920330203312033220333203342033520336203372033820339203402034120342203432034420345203462034720348203492035020351203522035320354203552035620357203582035920360203612036220363203642036520366203672036820369203702037120372203732037420375203762037720378203792038020381203822038320384203852038620387203882038920390203912039220393203942039520396203972039820399204002040120402204032040420405204062040720408204092041020411204122041320414204152041620417204182041920420204212042220423204242042520426204272042820429204302043120432204332043420435204362043720438204392044020441204422044320444204452044620447204482044920450204512045220453204542045520456204572045820459204602046120462204632046420465204662046720468204692047020471204722047320474204752047620477204782047920480204812048220483204842048520486204872048820489204902049120492204932049420495204962049720498204992050020501205022050320504205052050620507205082050920510205112051220513205142051520516205172051820519205202052120522205232052420525205262052720528205292053020531205322053320534205352053620537205382053920540205412054220543205442054520546205472054820549205502055120552205532055420555205562055720558205592056020561205622056320564205652056620567205682056920570205712057220573205742057520576205772057820579205802058120582205832058420585205862058720588205892059020591205922059320594205952059620597205982059920600206012060220603206042060520606206072060820609206102061120612206132061420615206162061720618206192062020621206222062320624206252062620627206282062920630206312063220633206342063520636206372063820639206402064120642206432064420645206462064720648206492065020651206522065320654206552065620657206582065920660206612066220663206642066520666206672066820669206702067120672206732067420675206762067720678206792068020681206822068320684206852068620687206882068920690206912069220693206942069520696206972069820699207002070120702207032070420705207062070720708207092071020711207122071320714207152071620717207182071920720207212072220723207242072520726207272072820729207302073120732207332073420735207362073720738207392074020741207422074320744207452074620747207482074920750207512075220753207542075520756207572075820759207602076120762207632076420765207662076720768207692077020771207722077320774207752077620777207782077920780207812078220783207842078520786207872078820789207902079120792207932079420795207962079720798207992080020801208022080320804208052080620807208082080920810208112081220813208142081520816208172081820819208202082120822208232082420825208262082720828208292083020831208322083320834208352083620837208382083920840208412084220843208442084520846208472084820849208502085120852208532085420855208562085720858208592086020861208622086320864208652086620867208682086920870208712087220873208742087520876208772087820879208802088120882208832088420885208862088720888208892089020891208922089320894208952089620897208982089920900209012090220903209042090520906209072090820909209102091120912209132091420915209162091720918209192092020921209222092320924209252092620927209282092920930209312093220933209342093520936209372093820939209402094120942209432094420945209462094720948209492095020951209522095320954209552095620957209582095920960209612096220963209642096520966209672096820969209702097120972209732097420975209762097720978209792098020981209822098320984209852098620987209882098920990209912099220993209942099520996209972099820999210002100121002210032100421005210062100721008210092101021011210122101321014210152101621017210182101921020210212102221023210242102521026210272102821029210302103121032210332103421035210362103721038210392104021041210422104321044210452104621047210482104921050210512105221053210542105521056210572105821059210602106121062210632106421065210662106721068210692107021071210722107321074210752107621077210782107921080210812108221083210842108521086210872108821089210902109121092210932109421095210962109721098210992110021101211022110321104211052110621107211082110921110211112111221113211142111521116211172111821119211202112121122211232112421125211262112721128211292113021131211322113321134211352113621137211382113921140211412114221143211442114521146211472114821149211502115121152211532115421155211562115721158211592116021161211622116321164211652116621167211682116921170211712117221173211742117521176211772117821179211802118121182211832118421185211862118721188211892119021191211922119321194211952119621197211982119921200212012120221203212042120521206212072120821209212102121121212212132121421215212162121721218212192122021221212222122321224212252122621227212282122921230212312123221233212342123521236212372123821239212402124121242212432124421245212462124721248212492125021251212522125321254212552125621257212582125921260212612126221263212642126521266212672126821269212702127121272212732127421275212762127721278212792128021281212822128321284212852128621287212882128921290212912129221293212942129521296212972129821299213002130121302213032130421305213062130721308213092131021311213122131321314213152131621317213182131921320213212132221323213242132521326213272132821329213302133121332213332133421335213362133721338213392134021341213422134321344213452134621347213482134921350213512135221353213542135521356213572135821359213602136121362213632136421365213662136721368213692137021371213722137321374213752137621377213782137921380213812138221383213842138521386213872138821389213902139121392213932139421395213962139721398213992140021401214022140321404214052140621407214082140921410214112141221413214142141521416214172141821419214202142121422214232142421425214262142721428214292143021431214322143321434214352143621437214382143921440214412144221443214442144521446214472144821449214502145121452214532145421455214562145721458214592146021461214622146321464214652146621467214682146921470214712147221473214742147521476214772147821479214802148121482214832148421485214862148721488214892149021491214922149321494214952149621497214982149921500215012150221503215042150521506215072150821509215102151121512215132151421515215162151721518215192152021521215222152321524215252152621527215282152921530215312153221533215342153521536215372153821539215402154121542215432154421545215462154721548215492155021551215522155321554215552155621557215582155921560215612156221563215642156521566215672156821569215702157121572215732157421575215762157721578215792158021581215822158321584215852158621587215882158921590215912159221593215942159521596215972159821599216002160121602216032160421605216062160721608216092161021611216122161321614216152161621617216182161921620216212162221623216242162521626216272162821629216302163121632216332163421635216362163721638216392164021641216422164321644216452164621647216482164921650216512165221653216542165521656216572165821659216602166121662216632166421665216662166721668216692167021671216722167321674216752167621677216782167921680216812168221683216842168521686216872168821689216902169121692216932169421695216962169721698216992170021701217022170321704217052170621707217082170921710217112171221713217142171521716217172171821719217202172121722217232172421725217262172721728217292173021731217322173321734217352173621737217382173921740217412174221743217442174521746217472174821749217502175121752217532175421755217562175721758217592176021761217622176321764217652176621767217682176921770217712177221773217742177521776217772177821779217802178121782217832178421785217862178721788217892179021791217922179321794217952179621797217982179921800218012180221803218042180521806218072180821809218102181121812218132181421815218162181721818218192182021821218222182321824218252182621827218282182921830218312183221833218342183521836218372183821839218402184121842218432184421845218462184721848218492185021851218522185321854218552185621857218582185921860218612186221863218642186521866218672186821869218702187121872218732187421875218762187721878218792188021881218822188321884218852188621887218882188921890218912189221893218942189521896218972189821899219002190121902219032190421905219062190721908219092191021911219122191321914219152191621917219182191921920219212192221923219242192521926219272192821929219302193121932219332193421935219362193721938219392194021941219422194321944219452194621947219482194921950219512195221953219542195521956219572195821959219602196121962219632196421965219662196721968219692197021971219722197321974219752197621977219782197921980219812198221983219842198521986219872198821989219902199121992219932199421995219962199721998219992200022001220022200322004220052200622007220082200922010220112201222013220142201522016220172201822019220202202122022220232202422025220262202722028220292203022031220322203322034220352203622037220382203922040220412204222043220442204522046220472204822049220502205122052220532205422055220562205722058220592206022061220622206322064220652206622067220682206922070220712207222073220742207522076220772207822079220802208122082220832208422085220862208722088220892209022091220922209322094220952209622097220982209922100221012210222103221042210522106221072210822109221102211122112221132211422115221162211722118221192212022121221222212322124221252212622127221282212922130221312213222133221342213522136221372213822139221402214122142221432214422145221462214722148221492215022151221522215322154221552215622157221582215922160221612216222163221642216522166221672216822169221702217122172221732217422175221762217722178221792218022181221822218322184221852218622187221882218922190221912219222193221942219522196221972219822199222002220122202222032220422205222062220722208222092221022211222122221322214222152221622217222182221922220222212222222223222242222522226222272222822229222302223122232222332223422235222362223722238222392224022241222422224322244222452224622247222482224922250222512225222253222542225522256222572225822259222602226122262222632226422265222662226722268222692227022271222722227322274222752227622277222782227922280222812228222283222842228522286222872228822289222902229122292222932229422295222962229722298222992230022301223022230322304223052230622307223082230922310223112231222313223142231522316223172231822319223202232122322223232232422325223262232722328223292233022331223322233322334223352233622337223382233922340223412234222343223442234522346223472234822349223502235122352223532235422355223562235722358223592236022361223622236322364223652236622367223682236922370223712237222373223742237522376223772237822379223802238122382223832238422385223862238722388223892239022391223922239322394223952239622397223982239922400224012240222403224042240522406224072240822409224102241122412224132241422415224162241722418224192242022421224222242322424224252242622427224282242922430224312243222433224342243522436224372243822439224402244122442224432244422445224462244722448224492245022451224522245322454224552245622457224582245922460224612246222463224642246522466224672246822469224702247122472224732247422475224762247722478224792248022481224822248322484224852248622487224882248922490224912249222493224942249522496224972249822499225002250122502225032250422505225062250722508225092251022511225122251322514225152251622517225182251922520225212252222523225242252522526225272252822529225302253122532225332253422535225362253722538225392254022541225422254322544225452254622547225482254922550225512255222553225542255522556225572255822559225602256122562225632256422565225662256722568225692257022571225722257322574225752257622577225782257922580225812258222583225842258522586225872258822589225902259122592225932259422595225962259722598225992260022601226022260322604226052260622607226082260922610226112261222613226142261522616226172261822619226202262122622226232262422625226262262722628226292263022631226322263322634226352263622637226382263922640226412264222643226442264522646226472264822649226502265122652226532265422655226562265722658226592266022661226622266322664226652266622667226682266922670226712267222673226742267522676226772267822679226802268122682226832268422685226862268722688226892269022691226922269322694226952269622697226982269922700227012270222703227042270522706227072270822709227102271122712227132271422715227162271722718227192272022721227222272322724227252272622727227282272922730227312273222733227342273522736227372273822739227402274122742227432274422745227462274722748227492275022751227522275322754227552275622757227582275922760227612276222763227642276522766227672276822769227702277122772227732277422775227762277722778227792278022781227822278322784227852278622787227882278922790227912279222793227942279522796227972279822799228002280122802228032280422805228062280722808228092281022811228122281322814228152281622817228182281922820228212282222823228242282522826228272282822829228302283122832228332283422835228362283722838228392284022841228422284322844228452284622847228482284922850228512285222853228542285522856228572285822859228602286122862228632286422865228662286722868228692287022871228722287322874228752287622877228782287922880228812288222883228842288522886228872288822889228902289122892228932289422895228962289722898228992290022901229022290322904229052290622907229082290922910229112291222913229142291522916229172291822919229202292122922229232292422925229262292722928229292293022931229322293322934229352293622937229382293922940229412294222943229442294522946229472294822949229502295122952229532295422955229562295722958229592296022961229622296322964229652296622967229682296922970229712297222973229742297522976229772297822979229802298122982229832298422985229862298722988229892299022991229922299322994229952299622997229982299923000230012300223003230042300523006230072300823009230102301123012230132301423015230162301723018230192302023021230222302323024230252302623027230282302923030230312303223033230342303523036230372303823039230402304123042230432304423045230462304723048230492305023051230522305323054230552305623057230582305923060230612306223063230642306523066230672306823069230702307123072230732307423075230762307723078230792308023081230822308323084230852308623087230882308923090230912309223093230942309523096230972309823099231002310123102231032310423105231062310723108231092311023111231122311323114231152311623117231182311923120231212312223123231242312523126231272312823129231302313123132231332313423135231362313723138231392314023141231422314323144231452314623147231482314923150231512315223153231542315523156231572315823159231602316123162231632316423165231662316723168231692317023171231722317323174231752317623177231782317923180231812318223183231842318523186231872318823189231902319123192231932319423195231962319723198231992320023201232022320323204232052320623207232082320923210232112321223213232142321523216232172321823219232202322123222232232322423225232262322723228232292323023231232322323323234232352323623237232382323923240232412324223243232442324523246232472324823249232502325123252232532325423255232562325723258232592326023261232622326323264232652326623267232682326923270232712327223273232742327523276232772327823279232802328123282232832328423285232862328723288232892329023291232922329323294232952329623297232982329923300233012330223303233042330523306233072330823309233102331123312233132331423315233162331723318233192332023321233222332323324233252332623327233282332923330233312333223333233342333523336233372333823339233402334123342233432334423345233462334723348233492335023351233522335323354233552335623357233582335923360233612336223363233642336523366233672336823369233702337123372233732337423375233762337723378233792338023381233822338323384233852338623387233882338923390233912339223393233942339523396233972339823399234002340123402234032340423405234062340723408234092341023411234122341323414234152341623417234182341923420234212342223423234242342523426234272342823429234302343123432234332343423435234362343723438234392344023441234422344323444234452344623447234482344923450234512345223453234542345523456234572345823459234602346123462234632346423465234662346723468234692347023471234722347323474234752347623477234782347923480234812348223483234842348523486234872348823489234902349123492234932349423495234962349723498234992350023501235022350323504235052350623507235082350923510235112351223513235142351523516235172351823519235202352123522235232352423525235262352723528235292353023531235322353323534235352353623537235382353923540235412354223543235442354523546235472354823549235502355123552235532355423555235562355723558235592356023561235622356323564235652356623567235682356923570235712357223573235742357523576235772357823579235802358123582235832358423585235862358723588235892359023591235922359323594235952359623597235982359923600236012360223603236042360523606236072360823609236102361123612236132361423615236162361723618236192362023621236222362323624236252362623627236282362923630236312363223633236342363523636236372363823639236402364123642236432364423645236462364723648236492365023651236522365323654236552365623657236582365923660236612366223663236642366523666236672366823669236702367123672236732367423675236762367723678236792368023681236822368323684236852368623687236882368923690236912369223693236942369523696236972369823699237002370123702237032370423705237062370723708237092371023711237122371323714237152371623717237182371923720237212372223723237242372523726237272372823729237302373123732237332373423735237362373723738237392374023741237422374323744237452374623747237482374923750237512375223753237542375523756237572375823759237602376123762237632376423765237662376723768237692377023771237722377323774237752377623777237782377923780237812378223783237842378523786237872378823789237902379123792237932379423795237962379723798237992380023801238022380323804238052380623807238082380923810238112381223813238142381523816238172381823819238202382123822238232382423825238262382723828238292383023831238322383323834238352383623837238382383923840238412384223843238442384523846238472384823849238502385123852238532385423855238562385723858238592386023861238622386323864238652386623867238682386923870238712387223873238742387523876238772387823879238802388123882238832388423885238862388723888238892389023891238922389323894238952389623897238982389923900239012390223903239042390523906239072390823909239102391123912239132391423915239162391723918239192392023921239222392323924239252392623927239282392923930239312393223933239342393523936239372393823939239402394123942239432394423945239462394723948239492395023951239522395323954239552395623957239582395923960239612396223963239642396523966239672396823969239702397123972239732397423975239762397723978239792398023981239822398323984239852398623987239882398923990239912399223993239942399523996239972399823999240002400124002240032400424005240062400724008240092401024011240122401324014240152401624017240182401924020240212402224023240242402524026240272402824029240302403124032240332403424035240362403724038240392404024041240422404324044240452404624047240482404924050240512405224053240542405524056240572405824059240602406124062240632406424065240662406724068240692407024071240722407324074240752407624077240782407924080240812408224083240842408524086240872408824089240902409124092240932409424095240962409724098240992410024101241022410324104241052410624107241082410924110241112411224113241142411524116241172411824119241202412124122241232412424125241262412724128241292413024131241322413324134241352413624137241382413924140241412414224143241442414524146241472414824149241502415124152241532415424155241562415724158241592416024161241622416324164241652416624167241682416924170241712417224173241742417524176241772417824179241802418124182241832418424185241862418724188241892419024191241922419324194241952419624197241982419924200242012420224203242042420524206242072420824209242102421124212242132421424215242162421724218242192422024221242222422324224242252422624227242282422924230242312423224233242342423524236242372423824239242402424124242242432424424245242462424724248242492425024251242522425324254242552425624257242582425924260242612426224263242642426524266242672426824269242702427124272242732427424275242762427724278242792428024281242822428324284242852428624287242882428924290242912429224293242942429524296242972429824299243002430124302243032430424305243062430724308243092431024311243122431324314243152431624317243182431924320243212432224323243242432524326243272432824329243302433124332243332433424335243362433724338243392434024341243422434324344243452434624347243482434924350243512435224353243542435524356243572435824359243602436124362243632436424365243662436724368243692437024371243722437324374243752437624377243782437924380243812438224383243842438524386243872438824389243902439124392243932439424395243962439724398243992440024401244022440324404244052440624407244082440924410244112441224413244142441524416244172441824419244202442124422244232442424425244262442724428244292443024431244322443324434244352443624437244382443924440244412444224443244442444524446244472444824449244502445124452244532445424455244562445724458244592446024461244622446324464244652446624467244682446924470244712447224473244742447524476244772447824479244802448124482244832448424485244862448724488244892449024491244922449324494244952449624497244982449924500245012450224503245042450524506245072450824509245102451124512245132451424515245162451724518245192452024521245222452324524245252452624527245282452924530245312453224533245342453524536245372453824539245402454124542245432454424545245462454724548245492455024551245522455324554245552455624557245582455924560245612456224563245642456524566245672456824569245702457124572245732457424575245762457724578245792458024581245822458324584245852458624587245882458924590245912459224593245942459524596245972459824599246002460124602246032460424605246062460724608246092461024611246122461324614246152461624617246182461924620246212462224623246242462524626246272462824629246302463124632246332463424635246362463724638246392464024641246422464324644246452464624647246482464924650246512465224653246542465524656246572465824659246602466124662246632466424665246662466724668246692467024671246722467324674246752467624677246782467924680246812468224683246842468524686246872468824689246902469124692246932469424695246962469724698246992470024701247022470324704247052470624707247082470924710247112471224713247142471524716247172471824719247202472124722247232472424725247262472724728247292473024731247322473324734247352473624737247382473924740247412474224743247442474524746247472474824749247502475124752247532475424755247562475724758247592476024761247622476324764247652476624767247682476924770247712477224773247742477524776247772477824779247802478124782247832478424785247862478724788247892479024791247922479324794247952479624797247982479924800248012480224803248042480524806248072480824809248102481124812248132481424815248162481724818248192482024821248222482324824248252482624827248282482924830248312483224833248342483524836248372483824839248402484124842248432484424845248462484724848248492485024851248522485324854248552485624857248582485924860248612486224863248642486524866248672486824869248702487124872248732487424875248762487724878248792488024881248822488324884248852488624887248882488924890248912489224893248942489524896248972489824899249002490124902249032490424905249062490724908249092491024911249122491324914249152491624917249182491924920249212492224923249242492524926249272492824929249302493124932249332493424935249362493724938249392494024941249422494324944249452494624947249482494924950249512495224953249542495524956249572495824959249602496124962249632496424965249662496724968249692497024971249722497324974249752497624977249782497924980249812498224983249842498524986249872498824989249902499124992249932499424995249962499724998249992500025001250022500325004250052500625007250082500925010250112501225013250142501525016250172501825019250202502125022250232502425025250262502725028250292503025031250322503325034250352503625037250382503925040250412504225043250442504525046250472504825049250502505125052250532505425055250562505725058250592506025061250622506325064250652506625067250682506925070250712507225073250742507525076250772507825079250802508125082250832508425085250862508725088250892509025091250922509325094250952509625097250982509925100251012510225103251042510525106251072510825109251102511125112251132511425115251162511725118251192512025121251222512325124251252512625127251282512925130251312513225133251342513525136251372513825139251402514125142251432514425145251462514725148251492515025151251522515325154251552515625157251582515925160251612516225163251642516525166251672516825169251702517125172251732517425175251762517725178251792518025181251822518325184251852518625187251882518925190251912519225193251942519525196
  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 odt)
  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 "25.1"
  671. :package-version '(Org . "9.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-current-tag-alist nil
  2242. "Alist of all tag groups in current buffer.
  2243. This variable takes into consideration `org-tag-alist',
  2244. `org-tag-persistent-alist' and TAGS keywords in the buffer.")
  2245. (defvar-local org-not-done-keywords nil)
  2246. (defvar-local org-done-keywords nil)
  2247. (defvar-local org-todo-heads nil)
  2248. (defvar-local org-todo-sets nil)
  2249. (defvar-local org-todo-log-states nil)
  2250. (defvar-local org-todo-kwd-alist nil)
  2251. (defvar-local org-todo-key-alist nil)
  2252. (defvar-local org-todo-key-trigger nil)
  2253. (defcustom org-todo-interpretation 'sequence
  2254. "Controls how TODO keywords are interpreted.
  2255. This variable is in principle obsolete and is only used for
  2256. backward compatibility, if the interpretation of todo keywords is
  2257. not given already in `org-todo-keywords'. See that variable for
  2258. more information."
  2259. :group 'org-todo
  2260. :group 'org-keywords
  2261. :type '(choice (const sequence)
  2262. (const type)))
  2263. (defcustom org-use-fast-todo-selection t
  2264. "\\<org-mode-map>\
  2265. Non-nil means use the fast todo selection scheme with \\[org-todo].
  2266. This variable describes if and under what circumstances the cycling
  2267. mechanism for TODO keywords will be replaced by a single-key, direct
  2268. selection scheme.
  2269. When nil, fast selection is never used.
  2270. When the symbol `prefix', it will be used when `org-todo' is called
  2271. with a prefix argument, i.e. `\\[universal-argument] \\[org-todo]' \
  2272. in an Org-mode buffer, and
  2273. `\\[universal-argument] t' in an agenda buffer.
  2274. When t, fast selection is used by default. In this case, the prefix
  2275. argument forces cycling instead.
  2276. In all cases, the special interface is only used if access keys have
  2277. actually been assigned by the user, i.e. if keywords in the configuration
  2278. are followed by a letter in parenthesis, like TODO(t)."
  2279. :group 'org-todo
  2280. :type '(choice
  2281. (const :tag "Never" nil)
  2282. (const :tag "By default" t)
  2283. (const :tag "Only with C-u C-c C-t" prefix)))
  2284. (defcustom org-provide-todo-statistics t
  2285. "Non-nil means update todo statistics after insert and toggle.
  2286. ALL-HEADLINES means update todo statistics by including headlines
  2287. with no TODO keyword as well, counting them as not done.
  2288. A list of TODO keywords means the same, but skip keywords that are
  2289. not in this list.
  2290. When set to a list of two lists, the first list contains keywords
  2291. to consider as TODO keywords, the second list contains keywords
  2292. to consider as DONE keywords.
  2293. When this is set, todo statistics is updated in the parent of the
  2294. current entry each time a todo state is changed."
  2295. :group 'org-todo
  2296. :type '(choice
  2297. (const :tag "Yes, only for TODO entries" t)
  2298. (const :tag "Yes, including all entries" all-headlines)
  2299. (repeat :tag "Yes, for TODOs in this list"
  2300. (string :tag "TODO keyword"))
  2301. (list :tag "Yes, for TODOs and DONEs in these lists"
  2302. (repeat (string :tag "TODO keyword"))
  2303. (repeat (string :tag "DONE keyword")))
  2304. (other :tag "No TODO statistics" nil)))
  2305. (defcustom org-hierarchical-todo-statistics t
  2306. "Non-nil means TODO statistics covers just direct children.
  2307. When nil, all entries in the subtree are considered.
  2308. This has only an effect if `org-provide-todo-statistics' is set.
  2309. To set this to nil for only a single subtree, use a COOKIE_DATA
  2310. property and include the word \"recursive\" into the value."
  2311. :group 'org-todo
  2312. :type 'boolean)
  2313. (defcustom org-after-todo-state-change-hook nil
  2314. "Hook which is run after the state of a TODO item was changed.
  2315. The new state (a string with a TODO keyword, or nil) is available in the
  2316. Lisp variable `org-state'."
  2317. :group 'org-todo
  2318. :type 'hook)
  2319. (defvar org-blocker-hook nil
  2320. "Hook for functions that are allowed to block a state change.
  2321. Functions in this hook should not modify the buffer.
  2322. Each function gets as its single argument a property list,
  2323. see `org-trigger-hook' for more information about this list.
  2324. If any of the functions in this hook returns nil, the state change
  2325. is blocked.")
  2326. (defvar org-trigger-hook nil
  2327. "Hook for functions that are triggered by a state change.
  2328. Each function gets as its single argument a property list with at
  2329. least the following elements:
  2330. (:type type-of-change :position pos-at-entry-start
  2331. :from old-state :to new-state)
  2332. Depending on the type, more properties may be present.
  2333. This mechanism is currently implemented for:
  2334. TODO state changes
  2335. ------------------
  2336. :type todo-state-change
  2337. :from previous state (keyword as a string), or nil, or a symbol
  2338. `todo' or `done', to indicate the general type of state.
  2339. :to new state, like in :from")
  2340. (defcustom org-enforce-todo-dependencies nil
  2341. "Non-nil means undone TODO entries will block switching the parent to DONE.
  2342. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  2343. be blocked if any prior sibling is not yet done.
  2344. Finally, if the parent is blocked because of ordered siblings of its own,
  2345. the child will also be blocked."
  2346. :set (lambda (var val)
  2347. (set var val)
  2348. (if val
  2349. (add-hook 'org-blocker-hook
  2350. 'org-block-todo-from-children-or-siblings-or-parent)
  2351. (remove-hook 'org-blocker-hook
  2352. 'org-block-todo-from-children-or-siblings-or-parent)))
  2353. :group 'org-todo
  2354. :type 'boolean)
  2355. (defcustom org-enforce-todo-checkbox-dependencies nil
  2356. "Non-nil means unchecked boxes will block switching the parent to DONE.
  2357. When this is nil, checkboxes have no influence on switching TODO states.
  2358. When non-nil, you first need to check off all check boxes before the TODO
  2359. entry can be switched to DONE.
  2360. This variable needs to be set before org.el is loaded, and you need to
  2361. restart Emacs after a change to make the change effective. The only way
  2362. to change is while Emacs is running is through the customize interface."
  2363. :set (lambda (var val)
  2364. (set var val)
  2365. (if val
  2366. (add-hook 'org-blocker-hook
  2367. 'org-block-todo-from-checkboxes)
  2368. (remove-hook 'org-blocker-hook
  2369. 'org-block-todo-from-checkboxes)))
  2370. :group 'org-todo
  2371. :type 'boolean)
  2372. (defcustom org-treat-insert-todo-heading-as-state-change nil
  2373. "Non-nil means inserting a TODO heading is treated as state change.
  2374. So when the command \\[org-insert-todo-heading] is used, state change
  2375. logging will apply if appropriate. When nil, the new TODO item will
  2376. be inserted directly, and no logging will take place."
  2377. :group 'org-todo
  2378. :type 'boolean)
  2379. (defcustom org-treat-S-cursor-todo-selection-as-state-change t
  2380. "Non-nil means switching TODO states with S-cursor counts as state change.
  2381. This is the default behavior. However, setting this to nil allows a
  2382. convenient way to select a TODO state and bypass any logging associated
  2383. with that."
  2384. :group 'org-todo
  2385. :type 'boolean)
  2386. (defcustom org-todo-state-tags-triggers nil
  2387. "Tag changes that should be triggered by TODO state changes.
  2388. This is a list. Each entry is
  2389. (state-change (tag . flag) .......)
  2390. State-change can be a string with a state, and empty string to indicate the
  2391. state that has no TODO keyword, or it can be one of the symbols `todo'
  2392. or `done', meaning any not-done or done state, respectively."
  2393. :group 'org-todo
  2394. :group 'org-tags
  2395. :type '(repeat
  2396. (cons (choice :tag "When changing to"
  2397. (const :tag "Not-done state" todo)
  2398. (const :tag "Done state" done)
  2399. (string :tag "State"))
  2400. (repeat
  2401. (cons :tag "Tag action"
  2402. (string :tag "Tag")
  2403. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  2404. (defcustom org-log-done nil
  2405. "Information to record when a task moves to the DONE state.
  2406. Possible values are:
  2407. nil Don't add anything, just change the keyword
  2408. time Add a time stamp to the task
  2409. note Prompt for a note and add it with template `org-log-note-headings'
  2410. This option can also be set with on a per-file-basis with
  2411. #+STARTUP: nologdone
  2412. #+STARTUP: logdone
  2413. #+STARTUP: lognotedone
  2414. You can have local logging settings for a subtree by setting the LOGGING
  2415. property to one or more of these keywords."
  2416. :group 'org-todo
  2417. :group 'org-progress
  2418. :type '(choice
  2419. (const :tag "No logging" nil)
  2420. (const :tag "Record CLOSED timestamp" time)
  2421. (const :tag "Record CLOSED timestamp with note." note)))
  2422. ;; Normalize old uses of org-log-done.
  2423. (cond
  2424. ((eq org-log-done t) (setq org-log-done 'time))
  2425. ((and (listp org-log-done) (memq 'done org-log-done))
  2426. (setq org-log-done 'note)))
  2427. (defcustom org-log-reschedule nil
  2428. "Information to record when the scheduling date of a tasks is modified.
  2429. Possible values are:
  2430. nil Don't add anything, just change the date
  2431. time Add a time stamp to the task
  2432. note Prompt for a note and add it with template `org-log-note-headings'
  2433. This option can also be set with on a per-file-basis with
  2434. #+STARTUP: nologreschedule
  2435. #+STARTUP: logreschedule
  2436. #+STARTUP: lognotereschedule"
  2437. :group 'org-todo
  2438. :group 'org-progress
  2439. :type '(choice
  2440. (const :tag "No logging" nil)
  2441. (const :tag "Record timestamp" time)
  2442. (const :tag "Record timestamp with note." note)))
  2443. (defcustom org-log-redeadline nil
  2444. "Information to record when the deadline date of a tasks is modified.
  2445. Possible values are:
  2446. nil Don't add anything, just change the date
  2447. time Add a time stamp to the task
  2448. note Prompt for a note and add it with template `org-log-note-headings'
  2449. This option can also be set with on a per-file-basis with
  2450. #+STARTUP: nologredeadline
  2451. #+STARTUP: logredeadline
  2452. #+STARTUP: lognoteredeadline
  2453. You can have local logging settings for a subtree by setting the LOGGING
  2454. property to one or more of these keywords."
  2455. :group 'org-todo
  2456. :group 'org-progress
  2457. :type '(choice
  2458. (const :tag "No logging" nil)
  2459. (const :tag "Record timestamp" time)
  2460. (const :tag "Record timestamp with note." note)))
  2461. (defcustom org-log-note-clock-out nil
  2462. "Non-nil means record a note when clocking out of an item.
  2463. This can also be configured on a per-file basis by adding one of
  2464. the following lines anywhere in the buffer:
  2465. #+STARTUP: lognoteclock-out
  2466. #+STARTUP: nolognoteclock-out"
  2467. :group 'org-todo
  2468. :group 'org-progress
  2469. :type 'boolean)
  2470. (defcustom org-log-done-with-time t
  2471. "Non-nil means the CLOSED time stamp will contain date and time.
  2472. When nil, only the date will be recorded."
  2473. :group 'org-progress
  2474. :type 'boolean)
  2475. (defcustom org-log-note-headings
  2476. '((done . "CLOSING NOTE %t")
  2477. (state . "State %-12s from %-12S %t")
  2478. (note . "Note taken on %t")
  2479. (reschedule . "Rescheduled from %S on %t")
  2480. (delschedule . "Not scheduled, was %S on %t")
  2481. (redeadline . "New deadline from %S on %t")
  2482. (deldeadline . "Removed deadline, was %S on %t")
  2483. (refile . "Refiled on %t")
  2484. (clock-out . ""))
  2485. "Headings for notes added to entries.
  2486. The value is an alist, with the car being a symbol indicating the
  2487. note context, and the cdr is the heading to be used. The heading
  2488. may also be the empty string. The following placeholders can be
  2489. used:
  2490. %t a time stamp.
  2491. %T an active time stamp instead the default inactive one
  2492. %d a short-format time stamp.
  2493. %D an active short-format time stamp.
  2494. %s the new TODO state or time stamp (inactive), in double quotes.
  2495. %S the old TODO state or time stamp (inactive), in double quotes.
  2496. %u the user name.
  2497. %U full user name.
  2498. In fact, it is not a good idea to change the `state' entry,
  2499. because Agenda Log mode depends on the format of these entries."
  2500. :group 'org-todo
  2501. :group 'org-progress
  2502. :type '(list :greedy t
  2503. (cons (const :tag "Heading when closing an item" done) string)
  2504. (cons (const :tag
  2505. "Heading when changing todo state (todo sequence only)"
  2506. state) string)
  2507. (cons (const :tag "Heading when just taking a note" note) string)
  2508. (cons (const :tag "Heading when rescheduling" reschedule) string)
  2509. (cons (const :tag "Heading when an item is no longer scheduled" delschedule) string)
  2510. (cons (const :tag "Heading when changing deadline" redeadline) string)
  2511. (cons (const :tag "Heading when deleting a deadline" deldeadline) string)
  2512. (cons (const :tag "Heading when refiling" refile) string)
  2513. (cons (const :tag "Heading when clocking out" clock-out) string)))
  2514. (unless (assq 'note org-log-note-headings)
  2515. (push '(note . "%t") org-log-note-headings))
  2516. (defcustom org-log-into-drawer nil
  2517. "Non-nil means insert state change notes and time stamps into a drawer.
  2518. When nil, state changes notes will be inserted after the headline and
  2519. any scheduling and clock lines, but not inside a drawer.
  2520. The value of this variable should be the name of the drawer to use.
  2521. LOGBOOK is proposed as the default drawer for this purpose, you can
  2522. also set this to a string to define the drawer of your choice.
  2523. A value of t is also allowed, representing \"LOGBOOK\".
  2524. A value of t or nil can also be set with on a per-file-basis with
  2525. #+STARTUP: logdrawer
  2526. #+STARTUP: nologdrawer
  2527. If this variable is set, `org-log-state-notes-insert-after-drawers'
  2528. will be ignored.
  2529. You can set the property LOG_INTO_DRAWER to overrule this setting for
  2530. a subtree.
  2531. Do not check directly this variable in a Lisp program. Call
  2532. function `org-log-into-drawer' instead."
  2533. :group 'org-todo
  2534. :group 'org-progress
  2535. :type '(choice
  2536. (const :tag "Not into a drawer" nil)
  2537. (const :tag "LOGBOOK" t)
  2538. (string :tag "Other")))
  2539. (org-defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer)
  2540. (defun org-log-into-drawer ()
  2541. "Name of the log drawer, as a string, or nil.
  2542. This is the value of `org-log-into-drawer'. However, if the
  2543. current entry has or inherits a LOG_INTO_DRAWER property, it will
  2544. be used instead of the default value."
  2545. (let ((p (org-entry-get nil "LOG_INTO_DRAWER" 'inherit t)))
  2546. (cond ((equal p "nil") nil)
  2547. ((equal p "t") "LOGBOOK")
  2548. ((stringp p) p)
  2549. (p "LOGBOOK")
  2550. ((stringp org-log-into-drawer) org-log-into-drawer)
  2551. (org-log-into-drawer "LOGBOOK"))))
  2552. (defcustom org-log-state-notes-insert-after-drawers nil
  2553. "Non-nil means insert state change notes after any drawers in entry.
  2554. Only the drawers that *immediately* follow the headline and the
  2555. deadline/scheduled line are skipped.
  2556. When nil, insert notes right after the heading and perhaps the line
  2557. with deadline/scheduling if present.
  2558. This variable will have no effect if `org-log-into-drawer' is
  2559. set."
  2560. :group 'org-todo
  2561. :group 'org-progress
  2562. :type 'boolean)
  2563. (defcustom org-log-states-order-reversed t
  2564. "Non-nil means the latest state note will be directly after heading.
  2565. When nil, the state change notes will be ordered according to time.
  2566. This option can also be set with on a per-file-basis with
  2567. #+STARTUP: logstatesreversed
  2568. #+STARTUP: nologstatesreversed"
  2569. :group 'org-todo
  2570. :group 'org-progress
  2571. :type 'boolean)
  2572. (defcustom org-todo-repeat-to-state nil
  2573. "The TODO state to which a repeater should return the repeating task.
  2574. By default this is the first task in a TODO sequence, or the previous state
  2575. in a TODO_TYP set. But you can specify another task here.
  2576. alternatively, set the :REPEAT_TO_STATE: property of the entry."
  2577. :group 'org-todo
  2578. :version "24.1"
  2579. :type '(choice (const :tag "Head of sequence" nil)
  2580. (string :tag "Specific state")))
  2581. (defcustom org-log-repeat 'time
  2582. "Non-nil means record moving through the DONE state when triggering repeat.
  2583. An auto-repeating task is immediately switched back to TODO when
  2584. marked DONE. If you are not logging state changes (by adding \"@\"
  2585. or \"!\" to the TODO keyword definition), or set `org-log-done' to
  2586. record a closing note, there will be no record of the task moving
  2587. through DONE. This variable forces taking a note anyway.
  2588. nil Don't force a record
  2589. time Record a time stamp
  2590. note Prompt for a note and add it with template `org-log-note-headings'
  2591. This option can also be set with on a per-file-basis with
  2592. #+STARTUP: nologrepeat
  2593. #+STARTUP: logrepeat
  2594. #+STARTUP: lognoterepeat
  2595. You can have local logging settings for a subtree by setting the LOGGING
  2596. property to one or more of these keywords."
  2597. :group 'org-todo
  2598. :group 'org-progress
  2599. :type '(choice
  2600. (const :tag "Don't force a record" nil)
  2601. (const :tag "Force recording the DONE state" time)
  2602. (const :tag "Force recording a note with the DONE state" note)))
  2603. (defgroup org-priorities nil
  2604. "Priorities in Org-mode."
  2605. :tag "Org Priorities"
  2606. :group 'org-todo)
  2607. (defcustom org-enable-priority-commands t
  2608. "Non-nil means priority commands are active.
  2609. When nil, these commands will be disabled, so that you never accidentally
  2610. set a priority."
  2611. :group 'org-priorities
  2612. :type 'boolean)
  2613. (defcustom org-highest-priority ?A
  2614. "The highest priority of TODO items. A character like ?A, ?B etc.
  2615. Must have a smaller ASCII number than `org-lowest-priority'."
  2616. :group 'org-priorities
  2617. :type 'character)
  2618. (defcustom org-lowest-priority ?C
  2619. "The lowest priority of TODO items. A character like ?A, ?B etc.
  2620. Must have a larger ASCII number than `org-highest-priority'."
  2621. :group 'org-priorities
  2622. :type 'character)
  2623. (defcustom org-default-priority ?B
  2624. "The default priority of TODO items.
  2625. This is the priority an item gets if no explicit priority is given.
  2626. When starting to cycle on an empty priority the first step in the cycle
  2627. depends on `org-priority-start-cycle-with-default'. The resulting first
  2628. step priority must not exceed the range from `org-highest-priority' to
  2629. `org-lowest-priority' which means that `org-default-priority' has to be
  2630. in this range exclusive or inclusive the range boundaries. Else the
  2631. first step refuses to set the default and the second will fall back
  2632. to (depending on the command used) the highest or lowest priority."
  2633. :group 'org-priorities
  2634. :type 'character)
  2635. (defcustom org-priority-start-cycle-with-default t
  2636. "Non-nil means start with default priority when starting to cycle.
  2637. When this is nil, the first step in the cycle will be (depending on the
  2638. command used) one higher or lower than the default priority.
  2639. See also `org-default-priority'."
  2640. :group 'org-priorities
  2641. :type 'boolean)
  2642. (defcustom org-get-priority-function nil
  2643. "Function to extract the priority from a string.
  2644. The string is normally the headline. If this is nil Org computes the
  2645. priority from the priority cookie like [#A] in the headline. It returns
  2646. an integer, increasing by 1000 for each priority level.
  2647. The user can set a different function here, which should take a string
  2648. as an argument and return the numeric priority."
  2649. :group 'org-priorities
  2650. :version "24.1"
  2651. :type '(choice
  2652. (const nil)
  2653. (function)))
  2654. (defgroup org-time nil
  2655. "Options concerning time stamps and deadlines in Org-mode."
  2656. :tag "Org Time"
  2657. :group 'org)
  2658. (defcustom org-time-stamp-rounding-minutes '(0 5)
  2659. "Number of minutes to round time stamps to.
  2660. \\<org-mode-map>\
  2661. These are two values, the first applies when first creating a time stamp.
  2662. The second applies when changing it with the commands `S-up' and `S-down'.
  2663. When changing the time stamp, this means that it will change in steps
  2664. of N minutes, as given by the second value.
  2665. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  2666. numbers should be factors of 60, so for example 5, 10, 15.
  2667. When this is larger than 1, you can still force an exact time stamp by using
  2668. a double prefix argument to a time stamp command like \
  2669. `\\[org-time-stamp]' or `\\[org-time-stamp-inactive],
  2670. and by using a prefix arg to `S-up/down' to specify the exact number
  2671. of minutes to shift."
  2672. :group 'org-time
  2673. :get (lambda (var) ; Make sure both elements are there
  2674. (if (integerp (default-value var))
  2675. (list (default-value var) 5)
  2676. (default-value var)))
  2677. :type '(list
  2678. (integer :tag "when inserting times")
  2679. (integer :tag "when modifying times")))
  2680. ;; Normalize old customizations of this variable.
  2681. (when (integerp org-time-stamp-rounding-minutes)
  2682. (setq org-time-stamp-rounding-minutes
  2683. (list org-time-stamp-rounding-minutes
  2684. org-time-stamp-rounding-minutes)))
  2685. (defcustom org-display-custom-times nil
  2686. "Non-nil means overlay custom formats over all time stamps.
  2687. The formats are defined through the variable `org-time-stamp-custom-formats'.
  2688. To turn this on on a per-file basis, insert anywhere in the file:
  2689. #+STARTUP: customtime"
  2690. :group 'org-time
  2691. :set 'set-default
  2692. :type 'sexp)
  2693. (make-variable-buffer-local 'org-display-custom-times)
  2694. (defcustom org-time-stamp-custom-formats
  2695. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  2696. "Custom formats for time stamps. See `format-time-string' for the syntax.
  2697. These are overlaid over the default ISO format if the variable
  2698. `org-display-custom-times' is set. Time like %H:%M should be at the
  2699. end of the second format. The custom formats are also honored by export
  2700. commands, if custom time display is turned on at the time of export."
  2701. :group 'org-time
  2702. :type 'sexp)
  2703. (defun org-time-stamp-format (&optional long inactive)
  2704. "Get the right format for a time string."
  2705. (let ((f (if long (cdr org-time-stamp-formats)
  2706. (car org-time-stamp-formats))))
  2707. (if inactive
  2708. (concat "[" (substring f 1 -1) "]")
  2709. f)))
  2710. (defcustom org-time-clocksum-format
  2711. '(:days "%dd " :hours "%d" :require-hours t :minutes ":%02d" :require-minutes t)
  2712. "The format string used when creating CLOCKSUM lines.
  2713. This is also used when Org mode generates a time duration.
  2714. The value can be a single format string containing two
  2715. %-sequences, which will be filled with the number of hours and
  2716. minutes in that order.
  2717. Alternatively, the value can be a plist associating any of the
  2718. keys :years, :months, :weeks, :days, :hours or :minutes with
  2719. format strings. The time duration is formatted using only the
  2720. time components that are needed and concatenating the results.
  2721. If a time unit in absent, it falls back to the next smallest
  2722. unit.
  2723. The keys :require-years, :require-months, :require-days,
  2724. :require-weeks, :require-hours, :require-minutes are also
  2725. meaningful. A non-nil value for these keys indicates that the
  2726. corresponding time component should always be included, even if
  2727. its value is 0.
  2728. For example,
  2729. (:days \"%dd\" :hours \"%d\" :require-hours t :minutes \":%02d\"
  2730. :require-minutes t)
  2731. means durations longer than a day will be expressed in days,
  2732. hours and minutes, and durations less than a day will always be
  2733. expressed in hours and minutes (even for durations less than an
  2734. hour).
  2735. The value
  2736. (:days \"%dd\" :minutes \"%dm\")
  2737. means durations longer than a day will be expressed in days and
  2738. minutes, and durations less than a day will be expressed entirely
  2739. in minutes (even for durations longer than an hour)."
  2740. :group 'org-time
  2741. :group 'org-clock
  2742. :version "24.4"
  2743. :package-version '(Org . "8.0")
  2744. :type '(choice (string :tag "Format string")
  2745. (set :tag "Plist"
  2746. (group :inline t (const :tag "Years" :years)
  2747. (string :tag "Format string"))
  2748. (group :inline t
  2749. (const :tag "Always show years" :require-years)
  2750. (const t))
  2751. (group :inline t (const :tag "Months" :months)
  2752. (string :tag "Format string"))
  2753. (group :inline t
  2754. (const :tag "Always show months" :require-months)
  2755. (const t))
  2756. (group :inline t (const :tag "Weeks" :weeks)
  2757. (string :tag "Format string"))
  2758. (group :inline t
  2759. (const :tag "Always show weeks" :require-weeks)
  2760. (const t))
  2761. (group :inline t (const :tag "Days" :days)
  2762. (string :tag "Format string"))
  2763. (group :inline t
  2764. (const :tag "Always show days" :require-days)
  2765. (const t))
  2766. (group :inline t (const :tag "Hours" :hours)
  2767. (string :tag "Format string"))
  2768. (group :inline t
  2769. (const :tag "Always show hours" :require-hours)
  2770. (const t))
  2771. (group :inline t (const :tag "Minutes" :minutes)
  2772. (string :tag "Format string"))
  2773. (group :inline t
  2774. (const :tag "Always show minutes" :require-minutes)
  2775. (const t)))))
  2776. (defcustom org-time-clocksum-use-fractional nil
  2777. "When non-nil, \\[org-clock-display] uses fractional times.
  2778. See `org-time-clocksum-format' for more on time clock formats."
  2779. :group 'org-time
  2780. :group 'org-clock
  2781. :version "24.3"
  2782. :type 'boolean)
  2783. (defcustom org-time-clocksum-use-effort-durations nil
  2784. "When non-nil, \\[org-clock-display] uses effort durations.
  2785. E.g. by default, one day is considered to be a 8 hours effort,
  2786. so a task that has been clocked for 16 hours will be displayed
  2787. as during 2 days in the clock display or in the clocktable.
  2788. See `org-effort-durations' on how to set effort durations
  2789. and `org-time-clocksum-format' for more on time clock formats."
  2790. :group 'org-time
  2791. :group 'org-clock
  2792. :version "24.4"
  2793. :package-version '(Org . "8.0")
  2794. :type 'boolean)
  2795. (defcustom org-time-clocksum-fractional-format "%.2f"
  2796. "The format string used when creating CLOCKSUM lines,
  2797. or when Org mode generates a time duration, if
  2798. `org-time-clocksum-use-fractional' is enabled.
  2799. The value can be a single format string containing one
  2800. %-sequence, which will be filled with the number of hours as
  2801. a float.
  2802. Alternatively, the value can be a plist associating any of the
  2803. keys :years, :months, :weeks, :days, :hours or :minutes with
  2804. a format string. The time duration is formatted using the
  2805. largest time unit which gives a non-zero integer part. If all
  2806. specified formats have zero integer part, the smallest time unit
  2807. is used."
  2808. :group 'org-time
  2809. :type '(choice (string :tag "Format string")
  2810. (set (group :inline t (const :tag "Years" :years)
  2811. (string :tag "Format string"))
  2812. (group :inline t (const :tag "Months" :months)
  2813. (string :tag "Format string"))
  2814. (group :inline t (const :tag "Weeks" :weeks)
  2815. (string :tag "Format string"))
  2816. (group :inline t (const :tag "Days" :days)
  2817. (string :tag "Format string"))
  2818. (group :inline t (const :tag "Hours" :hours)
  2819. (string :tag "Format string"))
  2820. (group :inline t (const :tag "Minutes" :minutes)
  2821. (string :tag "Format string")))))
  2822. (defcustom org-deadline-warning-days 14
  2823. "Number of days before expiration during which a deadline becomes active.
  2824. This variable governs the display in sparse trees and in the agenda.
  2825. When 0 or negative, it means use this number (the absolute value of it)
  2826. even if a deadline has a different individual lead time specified.
  2827. Custom commands can set this variable in the options section."
  2828. :group 'org-time
  2829. :group 'org-agenda-daily/weekly
  2830. :type 'integer)
  2831. (defcustom org-scheduled-delay-days 0
  2832. "Number of days before a scheduled item becomes active.
  2833. This variable governs the display in sparse trees and in the agenda.
  2834. The default value (i.e. 0) means: don't delay scheduled item.
  2835. When negative, it means use this number (the absolute value of it)
  2836. even if a scheduled item has a different individual delay time
  2837. specified.
  2838. Custom commands can set this variable in the options section."
  2839. :group 'org-time
  2840. :group 'org-agenda-daily/weekly
  2841. :version "24.4"
  2842. :package-version '(Org . "8.0")
  2843. :type 'integer)
  2844. (defcustom org-read-date-prefer-future t
  2845. "Non-nil means assume future for incomplete date input from user.
  2846. This affects the following situations:
  2847. 1. The user gives a month but not a year.
  2848. For example, if it is April and you enter \"feb 2\", this will be read
  2849. as Feb 2, *next* year. \"May 5\", however, will be this year.
  2850. 2. The user gives a day, but no month.
  2851. For example, if today is the 15th, and you enter \"3\", Org-mode will
  2852. read this as the third of *next* month. However, if you enter \"17\",
  2853. it will be considered as *this* month.
  2854. If you set this variable to the symbol `time', then also the following
  2855. will work:
  2856. 3. If the user gives a time.
  2857. If the time is before now, it will be interpreted as tomorrow.
  2858. Currently none of this works for ISO week specifications.
  2859. When this option is nil, the current day, month and year will always be
  2860. used as defaults.
  2861. See also `org-agenda-jump-prefer-future'."
  2862. :group 'org-time
  2863. :type '(choice
  2864. (const :tag "Never" nil)
  2865. (const :tag "Check month and day" t)
  2866. (const :tag "Check month, day, and time" time)))
  2867. (defcustom org-agenda-jump-prefer-future 'org-read-date-prefer-future
  2868. "Should the agenda jump command prefer the future for incomplete dates?
  2869. The default is to do the same as configured in `org-read-date-prefer-future'.
  2870. But you can also set a deviating value here.
  2871. This may t or nil, or the symbol `org-read-date-prefer-future'."
  2872. :group 'org-agenda
  2873. :group 'org-time
  2874. :version "24.1"
  2875. :type '(choice
  2876. (const :tag "Use org-read-date-prefer-future"
  2877. org-read-date-prefer-future)
  2878. (const :tag "Never" nil)
  2879. (const :tag "Always" t)))
  2880. (defcustom org-read-date-force-compatible-dates t
  2881. "Should date/time prompt force dates that are guaranteed to work in Emacs?
  2882. Depending on the system Emacs is running on, certain dates cannot
  2883. be represented with the type used internally to represent time.
  2884. Dates between 1970-1-1 and 2038-1-1 can always be represented
  2885. correctly. Some systems allow for earlier dates, some for later,
  2886. some for both. One way to find out it to insert any date into an
  2887. Org buffer, putting the cursor on the year and hitting S-up and
  2888. S-down to test the range.
  2889. When this variable is set to t, the date/time prompt will not let
  2890. you specify dates outside the 1970-2037 range, so it is certain that
  2891. these dates will work in whatever version of Emacs you are
  2892. running, and also that you can move a file from one Emacs implementation
  2893. to another. WHenever Org is forcing the year for you, it will display
  2894. a message and beep.
  2895. When this variable is nil, Org will check if the date is
  2896. representable in the specific Emacs implementation you are using.
  2897. If not, it will force a year, usually the current year, and beep
  2898. to remind you. Currently this setting is not recommended because
  2899. the likelihood that you will open your Org files in an Emacs that
  2900. has limited date range is not negligible.
  2901. A workaround for this problem is to use diary sexp dates for time
  2902. stamps outside of this range."
  2903. :group 'org-time
  2904. :version "24.1"
  2905. :type 'boolean)
  2906. (defcustom org-read-date-display-live t
  2907. "Non-nil means display current interpretation of date prompt live.
  2908. This display will be in an overlay, in the minibuffer."
  2909. :group 'org-time
  2910. :type 'boolean)
  2911. (defcustom org-read-date-popup-calendar t
  2912. "Non-nil means pop up a calendar when prompting for a date.
  2913. In the calendar, the date can be selected with mouse-1. However, the
  2914. minibuffer will also be active, and you can simply enter the date as well.
  2915. When nil, only the minibuffer will be available."
  2916. :group 'org-time
  2917. :type 'boolean)
  2918. (org-defvaralias 'org-popup-calendar-for-date-prompt
  2919. 'org-read-date-popup-calendar)
  2920. (make-obsolete-variable
  2921. 'org-read-date-minibuffer-setup-hook
  2922. "Set `org-read-date-minibuffer-local-map' instead." "24.4")
  2923. (defcustom org-read-date-minibuffer-setup-hook nil
  2924. "Hook to be used to set up keys for the date/time interface.
  2925. Add key definitions to `minibuffer-local-map', which will be a
  2926. temporary copy.
  2927. WARNING: This option is obsolete, you should use
  2928. `org-read-date-minibuffer-local-map' to set up keys."
  2929. :group 'org-time
  2930. :type 'hook)
  2931. (defcustom org-extend-today-until 0
  2932. "The hour when your day really ends. Must be an integer.
  2933. This has influence for the following applications:
  2934. - When switching the agenda to \"today\". It it is still earlier than
  2935. the time given here, the day recognized as TODAY is actually yesterday.
  2936. - When a date is read from the user and it is still before the time given
  2937. here, the current date and time will be assumed to be yesterday, 23:59.
  2938. Also, timestamps inserted in capture templates follow this rule.
  2939. IMPORTANT: This is a feature whose implementation is and likely will
  2940. remain incomplete. Really, it is only here because past midnight seems to
  2941. be the favorite working time of John Wiegley :-)"
  2942. :group 'org-time
  2943. :type 'integer)
  2944. (defcustom org-use-effective-time nil
  2945. "If non-nil, consider `org-extend-today-until' when creating timestamps.
  2946. For example, if `org-extend-today-until' is 8, and it's 4am, then the
  2947. \"effective time\" of any timestamps between midnight and 8am will be
  2948. 23:59 of the previous day."
  2949. :group 'org-time
  2950. :version "24.1"
  2951. :type 'boolean)
  2952. (defcustom org-use-last-clock-out-time-as-effective-time nil
  2953. "When non-nil, use the last clock out time for `org-todo'.
  2954. Note that this option has precedence over the combined use of
  2955. `org-use-effective-time' and `org-extend-today-until'."
  2956. :group 'org-time
  2957. :version "24.4"
  2958. :package-version '(Org . "8.0")
  2959. :type 'boolean)
  2960. (defcustom org-edit-timestamp-down-means-later nil
  2961. "Non-nil means S-down will increase the time in a time stamp.
  2962. When nil, S-up will increase."
  2963. :group 'org-time
  2964. :type 'boolean)
  2965. (defcustom org-calendar-follow-timestamp-change t
  2966. "Non-nil means make the calendar window follow timestamp changes.
  2967. When a timestamp is modified and the calendar window is visible, it will be
  2968. moved to the new date."
  2969. :group 'org-time
  2970. :type 'boolean)
  2971. (defgroup org-tags nil
  2972. "Options concerning tags in Org-mode."
  2973. :tag "Org Tags"
  2974. :group 'org)
  2975. (defcustom org-tag-alist nil
  2976. "List of tags allowed in Org-mode files.
  2977. When this list is nil, Org-mode will base TAG input on what is already in the
  2978. buffer.
  2979. The value of this variable is an alist, the car of each entry must be a
  2980. keyword as a string, the cdr may be a character that is used to select
  2981. that tag through the fast-tag-selection interface.
  2982. See the manual for details."
  2983. :group 'org-tags
  2984. :type '(repeat
  2985. (choice
  2986. (cons (string :tag "Tag name")
  2987. (character :tag "Access char"))
  2988. (list :tag "Start radio group"
  2989. (const :startgroup)
  2990. (option (string :tag "Group description")))
  2991. (list :tag "Start tag group, non distinct"
  2992. (const :startgrouptag)
  2993. (option (string :tag "Group description")))
  2994. (list :tag "Group tags delimiter"
  2995. (const :grouptags))
  2996. (list :tag "End radio group"
  2997. (const :endgroup)
  2998. (option (string :tag "Group description")))
  2999. (list :tag "End tag group, non distinct"
  3000. (const :endgrouptag)
  3001. (option (string :tag "Group description")))
  3002. (const :tag "New line" (:newline)))))
  3003. (defcustom org-tag-persistent-alist nil
  3004. "List of tags that will always appear in all Org-mode files.
  3005. This is in addition to any in buffer settings or customizations
  3006. of `org-tag-alist'.
  3007. When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
  3008. The value of this variable is an alist, the car of each entry must be a
  3009. keyword as a string, the cdr may be a character that is used to select
  3010. that tag through the fast-tag-selection interface.
  3011. See the manual for details.
  3012. To disable these tags on a per-file basis, insert anywhere in the file:
  3013. #+STARTUP: noptag"
  3014. :group 'org-tags
  3015. :type '(repeat
  3016. (choice
  3017. (cons (string :tag "Tag name")
  3018. (character :tag "Access char"))
  3019. (const :tag "Start radio group" (:startgroup))
  3020. (const :tag "Group tags delimiter" (:grouptags))
  3021. (const :tag "End radio group" (:endgroup))
  3022. (const :tag "New line" (:newline)))))
  3023. (defcustom org-complete-tags-always-offer-all-agenda-tags nil
  3024. "If non-nil, always offer completion for all tags of all agenda files.
  3025. Instead of customizing this variable directly, you might want to
  3026. set it locally for capture buffers, because there no list of
  3027. tags in that file can be created dynamically (there are none).
  3028. (add-hook \\='org-capture-mode-hook
  3029. (lambda ()
  3030. (setq-local org-complete-tags-always-offer-all-agenda-tags t)))"
  3031. :group 'org-tags
  3032. :version "24.1"
  3033. :type 'boolean)
  3034. (defvar org-file-tags nil
  3035. "List of tags that can be inherited by all entries in the file.
  3036. The tags will be inherited if the variable `org-use-tag-inheritance'
  3037. says they should be.
  3038. This variable is populated from #+FILETAGS lines.")
  3039. (defcustom org-use-fast-tag-selection 'auto
  3040. "Non-nil means use fast tag selection scheme.
  3041. This is a special interface to select and deselect tags with single keys.
  3042. When nil, fast selection is never used.
  3043. When the symbol `auto', fast selection is used if and only if selection
  3044. characters for tags have been configured, either through the variable
  3045. `org-tag-alist' or through a #+TAGS line in the buffer.
  3046. When t, fast selection is always used and selection keys are assigned
  3047. automatically if necessary."
  3048. :group 'org-tags
  3049. :type '(choice
  3050. (const :tag "Always" t)
  3051. (const :tag "Never" nil)
  3052. (const :tag "When selection characters are configured" auto)))
  3053. (defcustom org-fast-tag-selection-single-key nil
  3054. "Non-nil means fast tag selection exits after first change.
  3055. When nil, you have to press RET to exit it.
  3056. During fast tag selection, you can toggle this flag with `C-c'.
  3057. This variable can also have the value `expert'. In this case, the window
  3058. displaying the tags menu is not even shown, until you press C-c again."
  3059. :group 'org-tags
  3060. :type '(choice
  3061. (const :tag "No" nil)
  3062. (const :tag "Yes" t)
  3063. (const :tag "Expert" expert)))
  3064. (defvar org-fast-tag-selection-include-todo nil
  3065. "Non-nil means fast tags selection interface will also offer TODO states.
  3066. This is an undocumented feature, you should not rely on it.")
  3067. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  3068. "The column to which tags should be indented in a headline.
  3069. If this number is positive, it specifies the column. If it is negative,
  3070. it means that the tags should be flushright to that column. For example,
  3071. -80 works well for a normal 80 character screen.
  3072. When 0, place tags directly after headline text, with only one space in
  3073. between."
  3074. :group 'org-tags
  3075. :type 'integer)
  3076. (defcustom org-auto-align-tags t
  3077. "Non-nil keeps tags aligned when modifying headlines.
  3078. Some operations (i.e. demoting) change the length of a headline and
  3079. therefore shift the tags around. With this option turned on, after
  3080. each such operation the tags are again aligned to `org-tags-column'."
  3081. :group 'org-tags
  3082. :type 'boolean)
  3083. (defcustom org-use-tag-inheritance t
  3084. "Non-nil means tags in levels apply also for sublevels.
  3085. When nil, only the tags directly given in a specific line apply there.
  3086. This may also be a list of tags that should be inherited, or a regexp that
  3087. matches tags that should be inherited. Additional control is possible
  3088. with the variable `org-tags-exclude-from-inheritance' which gives an
  3089. explicit list of tags to be excluded from inheritance, even if the value of
  3090. `org-use-tag-inheritance' would select it for inheritance.
  3091. If this option is t, a match early-on in a tree can lead to a large
  3092. number of matches in the subtree when constructing the agenda or creating
  3093. a sparse tree. If you only want to see the first match in a tree during
  3094. a search, check out the variable `org-tags-match-list-sublevels'."
  3095. :group 'org-tags
  3096. :type '(choice
  3097. (const :tag "Not" nil)
  3098. (const :tag "Always" t)
  3099. (repeat :tag "Specific tags" (string :tag "Tag"))
  3100. (regexp :tag "Tags matched by regexp")))
  3101. (defcustom org-tags-exclude-from-inheritance nil
  3102. "List of tags that should never be inherited.
  3103. This is a way to exclude a few tags from inheritance. For way to do
  3104. the opposite, to actively allow inheritance for selected tags,
  3105. see the variable `org-use-tag-inheritance'."
  3106. :group 'org-tags
  3107. :type '(repeat (string :tag "Tag")))
  3108. (defun org-tag-inherit-p (tag)
  3109. "Check if TAG is one that should be inherited."
  3110. (cond
  3111. ((member tag org-tags-exclude-from-inheritance) nil)
  3112. ((eq org-use-tag-inheritance t) t)
  3113. ((not org-use-tag-inheritance) nil)
  3114. ((stringp org-use-tag-inheritance)
  3115. (string-match org-use-tag-inheritance tag))
  3116. ((listp org-use-tag-inheritance)
  3117. (member tag org-use-tag-inheritance))
  3118. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  3119. (defcustom org-tags-match-list-sublevels t
  3120. "Non-nil means list also sublevels of headlines matching a search.
  3121. This variable applies to tags/property searches, and also to stuck
  3122. projects because this search is based on a tags match as well.
  3123. When set to the symbol `indented', sublevels are indented with
  3124. leading dots.
  3125. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  3126. the sublevels of a headline matching a tag search often also match
  3127. the same search. Listing all of them can create very long lists.
  3128. Setting this variable to nil causes subtrees of a match to be skipped.
  3129. This variable is semi-obsolete and probably should always be true. It
  3130. is better to limit inheritance to certain tags using the variables
  3131. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  3132. :group 'org-tags
  3133. :type '(choice
  3134. (const :tag "No, don't list them" nil)
  3135. (const :tag "Yes, do list them" t)
  3136. (const :tag "List them, indented with leading dots" indented)))
  3137. (defcustom org-tags-sort-function nil
  3138. "When set, tags are sorted using this function as a comparator."
  3139. :group 'org-tags
  3140. :type '(choice
  3141. (const :tag "No sorting" nil)
  3142. (const :tag "Alphabetical" string<)
  3143. (const :tag "Reverse alphabetical" string>)
  3144. (function :tag "Custom function" nil)))
  3145. (defvar org-tags-history nil
  3146. "History of minibuffer reads for tags.")
  3147. (defvar org-last-tags-completion-table nil
  3148. "The last used completion table for tags.")
  3149. (defvar org-after-tags-change-hook nil
  3150. "Hook that is run after the tags in a line have changed.")
  3151. (defgroup org-properties nil
  3152. "Options concerning properties in Org-mode."
  3153. :tag "Org Properties"
  3154. :group 'org)
  3155. (defcustom org-property-format "%-10s %s"
  3156. "How property key/value pairs should be formatted by `indent-line'.
  3157. When `indent-line' hits a property definition, it will format the line
  3158. according to this format, mainly to make sure that the values are
  3159. lined-up with respect to each other."
  3160. :group 'org-properties
  3161. :type 'string)
  3162. (defcustom org-properties-postprocess-alist nil
  3163. "Alist of properties and functions to adjust inserted values.
  3164. Elements of this alist must be of the form
  3165. ([string] [function])
  3166. where [string] must be a property name and [function] must be a
  3167. lambda expression: this lambda expression must take one argument,
  3168. the value to adjust, and return the new value as a string.
  3169. For example, this element will allow the property \"Remaining\"
  3170. to be updated wrt the relation between the \"Effort\" property
  3171. and the clock summary:
  3172. ((\"Remaining\" (lambda(value)
  3173. (let ((clocksum (org-clock-sum-current-item))
  3174. (effort (org-duration-string-to-minutes
  3175. (org-entry-get (point) \"Effort\"))))
  3176. (org-minutes-to-clocksum-string (- effort clocksum))))))"
  3177. :group 'org-properties
  3178. :version "24.1"
  3179. :type '(alist :key-type (string :tag "Property")
  3180. :value-type (function :tag "Function")))
  3181. (defcustom org-use-property-inheritance nil
  3182. "Non-nil means properties apply also for sublevels.
  3183. This setting is chiefly used during property searches. Turning it on can
  3184. cause significant overhead when doing a search, which is why it is not
  3185. on by default.
  3186. When nil, only the properties directly given in the current entry count.
  3187. When t, every property is inherited. The value may also be a list of
  3188. properties that should have inheritance, or a regular expression matching
  3189. properties that should be inherited.
  3190. However, note that some special properties use inheritance under special
  3191. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  3192. and the properties ending in \"_ALL\" when they are used as descriptor
  3193. for valid values of a property.
  3194. Note for programmers:
  3195. When querying an entry with `org-entry-get', you can control if inheritance
  3196. should be used. By default, `org-entry-get' looks only at the local
  3197. properties. You can request inheritance by setting the inherit argument
  3198. to t (to force inheritance) or to `selective' (to respect the setting
  3199. in this variable)."
  3200. :group 'org-properties
  3201. :type '(choice
  3202. (const :tag "Not" nil)
  3203. (const :tag "Always" t)
  3204. (repeat :tag "Specific properties" (string :tag "Property"))
  3205. (regexp :tag "Properties matched by regexp")))
  3206. (defun org-property-inherit-p (property)
  3207. "Check if PROPERTY is one that should be inherited."
  3208. (cond
  3209. ((eq org-use-property-inheritance t) t)
  3210. ((not org-use-property-inheritance) nil)
  3211. ((stringp org-use-property-inheritance)
  3212. (string-match org-use-property-inheritance property))
  3213. ((listp org-use-property-inheritance)
  3214. (member property org-use-property-inheritance))
  3215. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  3216. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  3217. "The default column format, if no other format has been defined.
  3218. This variable can be set on the per-file basis by inserting a line
  3219. #+COLUMNS: %25ITEM ....."
  3220. :group 'org-properties
  3221. :type 'string)
  3222. (defcustom org-columns-ellipses ".."
  3223. "The ellipses to be used when a field in column view is truncated.
  3224. When this is the empty string, as many characters as possible are shown,
  3225. but then there will be no visual indication that the field has been truncated.
  3226. When this is a string of length N, the last N characters of a truncated
  3227. field are replaced by this string. If the column is narrower than the
  3228. ellipses string, only part of the ellipses string will be shown."
  3229. :group 'org-properties
  3230. :type 'string)
  3231. (defconst org-global-properties-fixed
  3232. '(("VISIBILITY_ALL" . "folded children content all")
  3233. ("CLOCK_MODELINE_TOTAL_ALL" . "current today repeat all auto"))
  3234. "List of property/value pairs that can be inherited by any entry.
  3235. These are fixed values, for the preset properties. The user variable
  3236. that can be used to add to this list is `org-global-properties'.
  3237. The entries in this list are cons cells where the car is a property
  3238. name and cdr is a string with the value. If the value represents
  3239. multiple items like an \"_ALL\" property, separate the items by
  3240. spaces.")
  3241. (defcustom org-global-properties nil
  3242. "List of property/value pairs that can be inherited by any entry.
  3243. This list will be combined with the constant `org-global-properties-fixed'.
  3244. The entries in this list are cons cells where the car is a property
  3245. name and cdr is a string with the value.
  3246. You can set buffer-local values for the same purpose in the variable
  3247. `org-file-properties' this by adding lines like
  3248. #+PROPERTY: NAME VALUE"
  3249. :group 'org-properties
  3250. :type '(repeat
  3251. (cons (string :tag "Property")
  3252. (string :tag "Value"))))
  3253. (defvar-local org-file-properties nil
  3254. "List of property/value pairs that can be inherited by any entry.
  3255. Valid for the current buffer.
  3256. This variable is populated from #+PROPERTY lines.")
  3257. (defgroup org-agenda nil
  3258. "Options concerning agenda views in Org-mode."
  3259. :tag "Org Agenda"
  3260. :group 'org)
  3261. (defvar-local org-category nil
  3262. "Variable used by org files to set a category for agenda display.
  3263. Such files should use a file variable to set it, for example
  3264. # -*- mode: org; org-category: \"ELisp\"
  3265. or contain a special line
  3266. #+CATEGORY: ELisp
  3267. If the file does not specify a category, then file's base name
  3268. is used instead.")
  3269. (put 'org-category 'safe-local-variable (lambda (x) (or (symbolp x) (stringp x))))
  3270. (defcustom org-agenda-files nil
  3271. "The files to be used for agenda display.
  3272. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  3273. \\[org-remove-file]. You can also use customize to edit the list.
  3274. If an entry is a directory, all files in that directory that are matched by
  3275. `org-agenda-file-regexp' will be part of the file list.
  3276. If the value of the variable is not a list but a single file name, then
  3277. the list of agenda files is actually stored and maintained in that file, one
  3278. agenda file per line. In this file paths can be given relative to
  3279. `org-directory'. Tilde expansion and environment variable substitution
  3280. are also made."
  3281. :group 'org-agenda
  3282. :type '(choice
  3283. (repeat :tag "List of files and directories" file)
  3284. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  3285. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  3286. "Regular expression to match files for `org-agenda-files'.
  3287. If any element in the list in that variable contains a directory instead
  3288. of a normal file, all files in that directory that are matched by this
  3289. regular expression will be included."
  3290. :group 'org-agenda
  3291. :type 'regexp)
  3292. (defcustom org-agenda-text-search-extra-files nil
  3293. "List of extra files to be searched by text search commands.
  3294. These files will be searched in addition to the agenda files by the
  3295. commands `org-search-view' (`\\[org-agenda] s') \
  3296. and `org-occur-in-agenda-files'.
  3297. Note that these files will only be searched for text search commands,
  3298. not for the other agenda views like todo lists, tag searches or the weekly
  3299. agenda. This variable is intended to list notes and possibly archive files
  3300. that should also be searched by these two commands.
  3301. In fact, if the first element in the list is the symbol `agenda-archives',
  3302. then all archive files of all agenda files will be added to the search
  3303. scope."
  3304. :group 'org-agenda
  3305. :type '(set :greedy t
  3306. (const :tag "Agenda Archives" agenda-archives)
  3307. (repeat :inline t (file))))
  3308. (org-defvaralias 'org-agenda-multi-occur-extra-files
  3309. 'org-agenda-text-search-extra-files)
  3310. (defcustom org-agenda-skip-unavailable-files nil
  3311. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  3312. A nil value means to remove them, after a query, from the list."
  3313. :group 'org-agenda
  3314. :type 'boolean)
  3315. (defcustom org-calendar-to-agenda-key [?c]
  3316. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  3317. The command `org-calendar-goto-agenda' will be bound to this key. The
  3318. default is the character `c' because then `c' can be used to switch back and
  3319. forth between agenda and calendar."
  3320. :group 'org-agenda
  3321. :type 'sexp)
  3322. (defcustom org-calendar-insert-diary-entry-key [?i]
  3323. "The key to be installed in `calendar-mode-map' for adding diary entries.
  3324. This option is irrelevant until `org-agenda-diary-file' has been configured
  3325. to point to an Org-mode file. When that is the case, the command
  3326. `org-agenda-diary-entry' will be bound to the key given here, by default
  3327. `i'. In the calendar, `i' normally adds entries to `diary-file'. So
  3328. if you want to continue doing this, you need to change this to a different
  3329. key."
  3330. :group 'org-agenda
  3331. :type 'sexp)
  3332. (defcustom org-agenda-diary-file 'diary-file
  3333. "File to which to add new entries with the `i' key in agenda and calendar.
  3334. When this is the symbol `diary-file', the functionality in the Emacs
  3335. calendar will be used to add entries to the `diary-file'. But when this
  3336. points to a file, `org-agenda-diary-entry' will be used instead."
  3337. :group 'org-agenda
  3338. :type '(choice
  3339. (const :tag "The standard Emacs diary file" diary-file)
  3340. (file :tag "Special Org file diary entries")))
  3341. (eval-after-load "calendar"
  3342. '(progn
  3343. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  3344. 'org-calendar-goto-agenda)
  3345. (add-hook 'calendar-mode-hook
  3346. (lambda ()
  3347. (unless (eq org-agenda-diary-file 'diary-file)
  3348. (define-key calendar-mode-map
  3349. org-calendar-insert-diary-entry-key
  3350. 'org-agenda-diary-entry))))))
  3351. (defgroup org-latex nil
  3352. "Options for embedding LaTeX code into Org-mode."
  3353. :tag "Org LaTeX"
  3354. :group 'org)
  3355. (defcustom org-format-latex-options
  3356. '(:foreground default :background default :scale 1.0
  3357. :html-foreground "Black" :html-background "Transparent"
  3358. :html-scale 1.0 :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  3359. "Options for creating images from LaTeX fragments.
  3360. This is a property list with the following properties:
  3361. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  3362. `default' means use the foreground of the default face.
  3363. `auto' means use the foreground from the text face.
  3364. :background the background color, or \"Transparent\".
  3365. `default' means use the background of the default face.
  3366. `auto' means use the background from the text face.
  3367. :scale a scaling factor for the size of the images, to get more pixels
  3368. :html-foreground, :html-background, :html-scale
  3369. the same numbers for HTML export.
  3370. :matchers a list indicating which matchers should be used to
  3371. find LaTeX fragments. Valid members of this list are:
  3372. \"begin\" find environments
  3373. \"$1\" find single characters surrounded by $.$
  3374. \"$\" find math expressions surrounded by $...$
  3375. \"$$\" find math expressions surrounded by $$....$$
  3376. \"\\(\" find math expressions surrounded by \\(...\\)
  3377. \"\\=\\[\" find math expressions surrounded by \\=\\[...\\]"
  3378. :group 'org-latex
  3379. :type 'plist)
  3380. (defcustom org-format-latex-signal-error t
  3381. "Non-nil means signal an error when image creation of LaTeX snippets fails.
  3382. When nil, just push out a message."
  3383. :group 'org-latex
  3384. :version "24.1"
  3385. :type 'boolean)
  3386. (defcustom org-latex-to-mathml-jar-file nil
  3387. "Value of\"%j\" in `org-latex-to-mathml-convert-command'.
  3388. Use this to specify additional executable file say a jar file.
  3389. When using MathToWeb as the converter, specify the full-path to
  3390. your mathtoweb.jar file."
  3391. :group 'org-latex
  3392. :version "24.1"
  3393. :type '(choice
  3394. (const :tag "None" nil)
  3395. (file :tag "JAR file" :must-match t)))
  3396. (defcustom org-latex-to-mathml-convert-command nil
  3397. "Command to convert LaTeX fragments to MathML.
  3398. Replace format-specifiers in the command as noted below and use
  3399. `shell-command' to convert LaTeX to MathML.
  3400. %j: Executable file in fully expanded form as specified by
  3401. `org-latex-to-mathml-jar-file'.
  3402. %I: Input LaTeX file in fully expanded form.
  3403. %i: The latex fragment to be converted.
  3404. %o: Output MathML file.
  3405. This command is used by `org-create-math-formula'.
  3406. When using MathToWeb as the converter, set this option to
  3407. \"java -jar %j -unicode -force -df %o %I\".
  3408. When using LaTeXML set this option to
  3409. \"latexmlmath \"%i\" --presentationmathml=%o\"."
  3410. :group 'org-latex
  3411. :version "24.1"
  3412. :type '(choice
  3413. (const :tag "None" nil)
  3414. (string :tag "\nShell command")))
  3415. (defcustom org-latex-create-formula-image-program 'dvipng
  3416. "Program to convert LaTeX fragments with.
  3417. dvipng Process the LaTeX fragments to dvi file, then convert
  3418. dvi files to png files using dvipng.
  3419. This will also include processing of non-math environments.
  3420. imagemagick Convert the LaTeX fragments to pdf files and use imagemagick
  3421. to convert pdf files to png files"
  3422. :group 'org-latex
  3423. :version "24.1"
  3424. :type '(choice
  3425. (const :tag "dvipng" dvipng)
  3426. (const :tag "imagemagick" imagemagick)))
  3427. (defcustom org-latex-preview-ltxpng-directory "ltxpng/"
  3428. "Path to store latex preview images.
  3429. A relative path here creates many directories relative to the
  3430. processed org files paths. An absolute path puts all preview
  3431. images at the same place."
  3432. :group 'org-latex
  3433. :version "24.3"
  3434. :type 'string)
  3435. (defun org-format-latex-mathml-available-p ()
  3436. "Return t if `org-latex-to-mathml-convert-command' is usable."
  3437. (save-match-data
  3438. (when (and (boundp 'org-latex-to-mathml-convert-command)
  3439. org-latex-to-mathml-convert-command)
  3440. (let ((executable (car (split-string
  3441. org-latex-to-mathml-convert-command))))
  3442. (when (executable-find executable)
  3443. (if (string-match
  3444. "%j" org-latex-to-mathml-convert-command)
  3445. (file-readable-p org-latex-to-mathml-jar-file)
  3446. t))))))
  3447. (defcustom org-format-latex-header "\\documentclass{article}
  3448. \\usepackage[usenames]{color}
  3449. \[PACKAGES]
  3450. \[DEFAULT-PACKAGES]
  3451. \\pagestyle{empty} % do not remove
  3452. % The settings below are copied from fullpage.sty
  3453. \\setlength{\\textwidth}{\\paperwidth}
  3454. \\addtolength{\\textwidth}{-3cm}
  3455. \\setlength{\\oddsidemargin}{1.5cm}
  3456. \\addtolength{\\oddsidemargin}{-2.54cm}
  3457. \\setlength{\\evensidemargin}{\\oddsidemargin}
  3458. \\setlength{\\textheight}{\\paperheight}
  3459. \\addtolength{\\textheight}{-\\headheight}
  3460. \\addtolength{\\textheight}{-\\headsep}
  3461. \\addtolength{\\textheight}{-\\footskip}
  3462. \\addtolength{\\textheight}{-3cm}
  3463. \\setlength{\\topmargin}{1.5cm}
  3464. \\addtolength{\\topmargin}{-2.54cm}"
  3465. "The document header used for processing LaTeX fragments.
  3466. It is imperative that this header make sure that no page number
  3467. appears on the page. The package defined in the variables
  3468. `org-latex-default-packages-alist' and `org-latex-packages-alist'
  3469. will either replace the placeholder \"[PACKAGES]\" in this
  3470. header, or they will be appended."
  3471. :group 'org-latex
  3472. :type 'string)
  3473. (defun org-set-packages-alist (var val)
  3474. "Set the packages alist and make sure it has 3 elements per entry."
  3475. (set var (mapcar (lambda (x)
  3476. (if (and (consp x) (= (length x) 2))
  3477. (list (car x) (nth 1 x) t)
  3478. x))
  3479. val)))
  3480. (defun org-get-packages-alist (var)
  3481. "Get the packages alist and make sure it has 3 elements per entry."
  3482. (mapcar (lambda (x)
  3483. (if (and (consp x) (= (length x) 2))
  3484. (list (car x) (nth 1 x) t)
  3485. x))
  3486. (default-value var)))
  3487. (defcustom org-latex-default-packages-alist
  3488. '(("AUTO" "inputenc" t ("pdflatex"))
  3489. ("T1" "fontenc" t ("pdflatex"))
  3490. ("" "graphicx" t)
  3491. ("" "grffile" t)
  3492. ("" "longtable" nil)
  3493. ("" "wrapfig" nil)
  3494. ("" "rotating" nil)
  3495. ("normalem" "ulem" t)
  3496. ("" "amsmath" t)
  3497. ("" "textcomp" t)
  3498. ("" "amssymb" t)
  3499. ("" "capt-of" nil)
  3500. ("" "hyperref" nil))
  3501. "Alist of default packages to be inserted in the header.
  3502. Change this only if one of the packages here causes an
  3503. incompatibility with another package you are using.
  3504. The packages in this list are needed by one part or another of
  3505. Org mode to function properly:
  3506. - inputenc, fontenc: for basic font and character selection
  3507. - graphicx: for including images
  3508. - grffile: allow periods and spaces in graphics file names
  3509. - longtable: For multipage tables
  3510. - wrapfig: for figure placement
  3511. - rotating: for sideways figures and tables
  3512. - ulem: for underline and strike-through
  3513. - amsmath: for subscript and superscript and math environments
  3514. - textcomp, amssymb: for various symbols used
  3515. for interpreting the entities in `org-entities'. You can skip
  3516. some of these packages if you don't use any of their symbols.
  3517. - capt-of: for captions outside of floats
  3518. - hyperref: for cross references
  3519. Therefore you should not modify this variable unless you know
  3520. what you are doing. The one reason to change it anyway is that
  3521. you might be loading some other package that conflicts with one
  3522. of the default packages. Each element is either a cell or
  3523. a string.
  3524. A cell is of the format
  3525. (\"options\" \"package\" SNIPPET-FLAG COMPILERS)
  3526. If SNIPPET-FLAG is non-nil, the package also needs to be included
  3527. when compiling LaTeX snippets into images for inclusion into
  3528. non-LaTeX output. COMPILERS is a list of compilers that should
  3529. include the package, see `org-latex-compiler'. If the document
  3530. compiler is not in the list, and the list is non-nil, the package
  3531. will not be inserted in the final document.
  3532. A string will be inserted as-is in the header of the document."
  3533. :group 'org-latex
  3534. :group 'org-export-latex
  3535. :set 'org-set-packages-alist
  3536. :get 'org-get-packages-alist
  3537. :version "25.1"
  3538. :package-version '(Org . "8.3")
  3539. :type '(repeat
  3540. (choice
  3541. (list :tag "options/package pair"
  3542. (string :tag "options")
  3543. (string :tag "package")
  3544. (boolean :tag "Snippet"))
  3545. (string :tag "A line of LaTeX"))))
  3546. (defcustom org-latex-packages-alist nil
  3547. "Alist of packages to be inserted in every LaTeX header.
  3548. These will be inserted after `org-latex-default-packages-alist'.
  3549. Each element is either a cell or a string.
  3550. A cell is of the format:
  3551. (\"options\" \"package\" SNIPPET-FLAG)
  3552. SNIPPET-FLAG, when non-nil, indicates that this package is also
  3553. needed when turning LaTeX snippets into images for inclusion into
  3554. non-LaTeX output.
  3555. A string will be inserted as-is in the header of the document.
  3556. Make sure that you only list packages here which:
  3557. - you want in every file;
  3558. - do not conflict with the setup in `org-format-latex-header';
  3559. - do not conflict with the default packages in
  3560. `org-latex-default-packages-alist'."
  3561. :group 'org-latex
  3562. :group 'org-export-latex
  3563. :set 'org-set-packages-alist
  3564. :get 'org-get-packages-alist
  3565. :type '(repeat
  3566. (choice
  3567. (list :tag "options/package pair"
  3568. (string :tag "options")
  3569. (string :tag "package")
  3570. (boolean :tag "Snippet"))
  3571. (string :tag "A line of LaTeX"))))
  3572. (defgroup org-appearance nil
  3573. "Settings for Org-mode appearance."
  3574. :tag "Org Appearance"
  3575. :group 'org)
  3576. (defcustom org-level-color-stars-only nil
  3577. "Non-nil means fontify only the stars in each headline.
  3578. When nil, the entire headline is fontified.
  3579. Changing it requires restart of `font-lock-mode' to become effective
  3580. also in regions already fontified."
  3581. :group 'org-appearance
  3582. :type 'boolean)
  3583. (defcustom org-hide-leading-stars nil
  3584. "Non-nil means hide the first N-1 stars in a headline.
  3585. This works by using the face `org-hide' for these stars. This
  3586. face is white for a light background, and black for a dark
  3587. background. You may have to customize the face `org-hide' to
  3588. make this work.
  3589. Changing it requires restart of `font-lock-mode' to become effective
  3590. also in regions already fontified.
  3591. You may also set this on a per-file basis by adding one of the following
  3592. lines to the buffer:
  3593. #+STARTUP: hidestars
  3594. #+STARTUP: showstars"
  3595. :group 'org-appearance
  3596. :type 'boolean)
  3597. (defcustom org-hidden-keywords nil
  3598. "List of symbols corresponding to keywords to be hidden the org buffer.
  3599. For example, a value \\='(title) for this list will make the document's title
  3600. appear in the buffer without the initial #+TITLE: keyword."
  3601. :group 'org-appearance
  3602. :version "24.1"
  3603. :type '(set (const :tag "#+AUTHOR" author)
  3604. (const :tag "#+DATE" date)
  3605. (const :tag "#+EMAIL" email)
  3606. (const :tag "#+TITLE" title)))
  3607. (defcustom org-custom-properties nil
  3608. "List of properties (as strings) with a special meaning.
  3609. The default use of these custom properties is to let the user
  3610. hide them with `org-toggle-custom-properties-visibility'."
  3611. :group 'org-properties
  3612. :group 'org-appearance
  3613. :version "24.3"
  3614. :type '(repeat (string :tag "Property Name")))
  3615. (defcustom org-fontify-done-headline nil
  3616. "Non-nil means change the face of a headline if it is marked DONE.
  3617. Normally, only the TODO/DONE keyword indicates the state of a headline.
  3618. When this is non-nil, the headline after the keyword is set to the
  3619. `org-headline-done' as an additional indication."
  3620. :group 'org-appearance
  3621. :type 'boolean)
  3622. (defcustom org-fontify-emphasized-text t
  3623. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  3624. Changing this variable requires a restart of Emacs to take effect."
  3625. :group 'org-appearance
  3626. :type 'boolean)
  3627. (defcustom org-fontify-whole-heading-line nil
  3628. "Non-nil means fontify the whole line for headings.
  3629. This is useful when setting a background color for the
  3630. org-level-* faces."
  3631. :group 'org-appearance
  3632. :type 'boolean)
  3633. (defcustom org-highlight-latex-and-related nil
  3634. "Non-nil means highlight LaTeX related syntax in the buffer.
  3635. When non nil, the value should be a list containing any of the
  3636. following symbols:
  3637. `latex' Highlight LaTeX snippets and environments.
  3638. `script' Highlight subscript and superscript.
  3639. `entities' Highlight entities."
  3640. :group 'org-appearance
  3641. :version "24.4"
  3642. :package-version '(Org . "8.0")
  3643. :type '(choice
  3644. (const :tag "No highlighting" nil)
  3645. (set :greedy t :tag "Highlight"
  3646. (const :tag "LaTeX snippets and environments" latex)
  3647. (const :tag "Subscript and superscript" script)
  3648. (const :tag "Entities" entities))))
  3649. (defcustom org-hide-emphasis-markers nil
  3650. "Non-nil mean font-lock should hide the emphasis marker characters."
  3651. :group 'org-appearance
  3652. :type 'boolean)
  3653. (defcustom org-hide-macro-markers nil
  3654. "Non-nil mean font-lock should hide the brackets marking macro calls."
  3655. :group 'org-appearance
  3656. :type 'boolean)
  3657. (defcustom org-pretty-entities nil
  3658. "Non-nil means show entities as UTF8 characters.
  3659. When nil, the \\name form remains in the buffer."
  3660. :group 'org-appearance
  3661. :version "24.1"
  3662. :type 'boolean)
  3663. (defcustom org-pretty-entities-include-sub-superscripts t
  3664. "Non-nil means, pretty entity display includes formatting sub/superscripts."
  3665. :group 'org-appearance
  3666. :version "24.1"
  3667. :type 'boolean)
  3668. (defvar org-emph-re nil
  3669. "Regular expression for matching emphasis.
  3670. After a match, the match groups contain these elements:
  3671. 0 The match of the full regular expression, including the characters
  3672. before and after the proper match
  3673. 1 The character before the proper match, or empty at beginning of line
  3674. 2 The proper match, including the leading and trailing markers
  3675. 3 The leading marker like * or /, indicating the type of highlighting
  3676. 4 The text between the emphasis markers, not including the markers
  3677. 5 The character after the match, empty at the end of a line")
  3678. (defvar org-verbatim-re nil
  3679. "Regular expression for matching verbatim text.")
  3680. (defvar org-emphasis-regexp-components) ; defined just below
  3681. (defvar org-emphasis-alist) ; defined just below
  3682. (defun org-set-emph-re (var val)
  3683. "Set variable and compute the emphasis regular expression."
  3684. (set var val)
  3685. (when (and (boundp 'org-emphasis-alist)
  3686. (boundp 'org-emphasis-regexp-components)
  3687. org-emphasis-alist org-emphasis-regexp-components)
  3688. (let* ((e org-emphasis-regexp-components)
  3689. (pre (car e))
  3690. (post (nth 1 e))
  3691. (border (nth 2 e))
  3692. (body (nth 3 e))
  3693. (nl (nth 4 e))
  3694. (body1 (concat body "*?"))
  3695. (markers (mapconcat 'car org-emphasis-alist ""))
  3696. (vmarkers (mapconcat
  3697. (lambda (x) (if (eq (nth 2 x) 'verbatim) (car x) ""))
  3698. org-emphasis-alist "")))
  3699. ;; make sure special characters appear at the right position in the class
  3700. (if (string-match "\\^" markers)
  3701. (setq markers (concat (replace-match "" t t markers) "^")))
  3702. (if (string-match "-" markers)
  3703. (setq markers (concat (replace-match "" t t markers) "-")))
  3704. (if (string-match "\\^" vmarkers)
  3705. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  3706. (if (string-match "-" vmarkers)
  3707. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  3708. (if (> nl 0)
  3709. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  3710. (int-to-string nl) "\\}")))
  3711. ;; Make the regexp
  3712. (setq org-emph-re
  3713. (concat "\\([" pre "]\\|^\\)"
  3714. "\\("
  3715. "\\([" markers "]\\)"
  3716. "\\("
  3717. "[^" border "]\\|"
  3718. "[^" border "]"
  3719. body1
  3720. "[^" border "]"
  3721. "\\)"
  3722. "\\3\\)"
  3723. "\\([" post "]\\|$\\)"))
  3724. (setq org-verbatim-re
  3725. (concat "\\([" pre "]\\|^\\)"
  3726. "\\("
  3727. "\\([" vmarkers "]\\)"
  3728. "\\("
  3729. "[^" border "]\\|"
  3730. "[^" border "]"
  3731. body1
  3732. "[^" border "]"
  3733. "\\)"
  3734. "\\3\\)"
  3735. "\\([" post "]\\|$\\)")))))
  3736. ;; This used to be a defcustom (Org <8.0) but allowing the users to
  3737. ;; set this option proved cumbersome. See this message/thread:
  3738. ;; http://article.gmane.org/gmane.emacs.orgmode/68681
  3739. (defvar org-emphasis-regexp-components
  3740. '(" \t('\"{" "- \t.,:!?;'\")}\\[" " \t\r\n" "." 1)
  3741. "Components used to build the regular expression for emphasis.
  3742. This is a list with five entries. Terminology: In an emphasis string
  3743. like \" *strong word* \", we call the initial space PREMATCH, the final
  3744. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  3745. and \"trong wor\" is the body. The different components in this variable
  3746. specify what is allowed/forbidden in each part:
  3747. pre Chars allowed as prematch. Beginning of line will be allowed too.
  3748. post Chars allowed as postmatch. End of line will be allowed too.
  3749. border The chars *forbidden* as border characters.
  3750. body-regexp A regexp like \".\" to match a body character. Don't use
  3751. non-shy groups here, and don't allow newline here.
  3752. newline The maximum number of newlines allowed in an emphasis exp.
  3753. You need to reload Org or to restart Emacs after customizing this.")
  3754. (defcustom org-emphasis-alist
  3755. `(("*" bold)
  3756. ("/" italic)
  3757. ("_" underline)
  3758. ("=" org-verbatim verbatim)
  3759. ("~" org-code verbatim)
  3760. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))))
  3761. "Alist of characters and faces to emphasize text.
  3762. Text starting and ending with a special character will be emphasized,
  3763. for example *bold*, _underlined_ and /italic/. This variable sets the
  3764. marker characters and the face to be used by font-lock for highlighting
  3765. in Org-mode Emacs buffers.
  3766. You need to reload Org or to restart Emacs after customizing this."
  3767. :group 'org-appearance
  3768. :set 'org-set-emph-re
  3769. :version "24.4"
  3770. :package-version '(Org . "8.0")
  3771. :type '(repeat
  3772. (list
  3773. (string :tag "Marker character")
  3774. (choice
  3775. (face :tag "Font-lock-face")
  3776. (plist :tag "Face property list"))
  3777. (option (const verbatim)))))
  3778. (defvar org-protecting-blocks '("src" "example" "export")
  3779. "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
  3780. This is needed for font-lock setup.")
  3781. ;;; Functions and variables from their packages
  3782. ;; Declared here to avoid compiler warnings
  3783. ;; XEmacs only
  3784. (defvar outline-mode-menu-heading)
  3785. (defvar outline-mode-menu-show)
  3786. (defvar outline-mode-menu-hide)
  3787. (defvar zmacs-regions) ; XEmacs regions
  3788. ;; Emacs only
  3789. (defvar mark-active)
  3790. ;; Various packages
  3791. (declare-function calc-eval "calc" (str &optional separator &rest args))
  3792. (declare-function calendar-forward-day "cal-move" (arg))
  3793. (declare-function calendar-goto-date "cal-move" (date))
  3794. (declare-function calendar-goto-today "cal-move" ())
  3795. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  3796. (declare-function calendar-iso-to-absolute "cal-iso" (date))
  3797. (declare-function cdlatex-compute-tables "ext:cdlatex" ())
  3798. (declare-function cdlatex-tab "ext:cdlatex" ())
  3799. (declare-function dired-get-filename
  3800. "dired"
  3801. (&optional localp no-error-if-not-filep))
  3802. (declare-function iswitchb-read-buffer
  3803. "iswitchb"
  3804. (prompt &optional default require-match start matches-set))
  3805. (declare-function org-agenda-change-all-lines
  3806. "org-agenda"
  3807. (newhead hdmarker &optional fixface just-this))
  3808. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  3809. "org-agenda"
  3810. (&optional end))
  3811. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  3812. (declare-function org-agenda-format-item
  3813. "org-agenda"
  3814. (extra txt &optional level category tags dotime noprefix
  3815. remove-re habitp))
  3816. (declare-function org-agenda-maybe-redo "org-agenda" ())
  3817. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  3818. (declare-function org-agenda-save-markers-for-cut-and-paste
  3819. "org-agenda"
  3820. (beg end))
  3821. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  3822. (declare-function org-agenda-skip "org-agenda" ())
  3823. (declare-function org-attach-reveal "org-attach" (&optional if-exists))
  3824. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  3825. (declare-function org-indent-mode "org-indent" (&optional arg))
  3826. (declare-function org-inlinetask-goto-beginning "org-inlinetask" ())
  3827. (declare-function org-inlinetask-goto-end "org-inlinetask" ())
  3828. (declare-function org-inlinetask-in-task-p "org-inlinetask" ())
  3829. (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
  3830. (declare-function orgtbl-send-table "org-table" (&optional maybe))
  3831. (declare-function parse-time-string "parse-time" (string))
  3832. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  3833. (declare-function table--at-cell-p
  3834. "table"
  3835. (position &optional object at-column))
  3836. (defvar align-mode-rules-list)
  3837. (defvar calc-embedded-close-formula)
  3838. (defvar calc-embedded-open-formula)
  3839. (defvar calc-embedded-open-mode)
  3840. (defvar font-lock-unfontify-region-function)
  3841. (defvar iswitchb-temp-buflist)
  3842. (defvar org-agenda-tags-todo-honor-ignore-options)
  3843. (defvar remember-data-file)
  3844. (defvar texmathp-why)
  3845. ;;;###autoload
  3846. (defun turn-on-orgtbl ()
  3847. "Unconditionally turn on `orgtbl-mode'."
  3848. (require 'org-table)
  3849. (orgtbl-mode 1))
  3850. (defun org-at-table-p (&optional table-type)
  3851. "Non-nil if the cursor is inside an Org table.
  3852. If TABLE-TYPE is non-nil, also check for table.el-type tables.
  3853. If `org-enable-table-editor' is nil, return nil unconditionally."
  3854. (and
  3855. org-enable-table-editor
  3856. (save-excursion
  3857. (beginning-of-line)
  3858. (org-looking-at-p (if table-type "[ \t]*[|+]" "[ \t]*|")))
  3859. (or (not (derived-mode-p 'org-mode))
  3860. (let ((e (org-element-lineage (org-element-at-point) '(table) t)))
  3861. (and e (or table-type (eq (org-element-property :type e) 'org)))))))
  3862. (define-obsolete-function-alias 'org-table-p 'org-at-table-p "Org 9.0")
  3863. (defun org-at-table.el-p ()
  3864. "Non-nil when point is at a table.el table."
  3865. (and (save-excursion (beginning-of-line) (looking-at "[ \t]*[|+]"))
  3866. (let ((element (org-element-at-point)))
  3867. (and (eq (org-element-type element) 'table)
  3868. (eq (org-element-property :type element) 'table.el)))))
  3869. (defun org-table-recognize-table.el ()
  3870. "If there is a table.el table nearby, recognize it and move into it."
  3871. (when (and org-table-tab-recognizes-table.el (org-at-table.el-p))
  3872. (beginning-of-line)
  3873. (unless (or (looking-at org-table-dataline-regexp)
  3874. (not (looking-at org-table1-hline-regexp)))
  3875. (forward-line)
  3876. (when (looking-at org-table-any-border-regexp)
  3877. (forward-line -2)))
  3878. (if (re-search-forward "|" (org-table-end t) t)
  3879. (progn
  3880. (require 'table)
  3881. (if (table--at-cell-p (point)) t
  3882. (message "recognizing table.el table...")
  3883. (table-recognize-table)
  3884. (message "recognizing table.el table...done")))
  3885. (error "This should not happen"))))
  3886. ;;; This function is not used by org core since commit 6d1e3082, Feb 2010
  3887. (make-obsolete 'org-table-recognize-table.el
  3888. "please notify the org mailing list if you use this function."
  3889. "Org 9.0")
  3890. (defun org-at-table-hline-p ()
  3891. "Non-nil when point is inside a hline in a table.
  3892. Assume point is already in a table. If `org-enable-table-editor'
  3893. is nil, return nil unconditionally."
  3894. (and org-enable-table-editor
  3895. (save-excursion
  3896. (beginning-of-line)
  3897. (looking-at org-table-hline-regexp))))
  3898. (defun org-table-map-tables (function &optional quietly)
  3899. "Apply FUNCTION to the start of all tables in the buffer."
  3900. (save-excursion
  3901. (save-restriction
  3902. (widen)
  3903. (goto-char (point-min))
  3904. (while (re-search-forward org-table-any-line-regexp nil t)
  3905. (unless quietly
  3906. (message "Mapping tables: %d%%"
  3907. (floor (* 100.0 (point)) (buffer-size))))
  3908. (beginning-of-line 1)
  3909. (when (and (looking-at org-table-line-regexp)
  3910. ;; Exclude tables in src/example/verbatim/clocktable blocks
  3911. (not (org-in-block-p '("src" "example" "verbatim" "clocktable"))))
  3912. (save-excursion (funcall function))
  3913. (or (looking-at org-table-line-regexp)
  3914. (forward-char 1)))
  3915. (re-search-forward org-table-any-border-regexp nil 1))))
  3916. (unless quietly (message "Mapping tables: done")))
  3917. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock" (beg end))
  3918. (declare-function org-clock-update-mode-line "org-clock" ())
  3919. (declare-function org-resolve-clocks "org-clock"
  3920. (&optional also-non-dangling-p prompt last-valid))
  3921. (defun org-at-TBLFM-p (&optional pos)
  3922. "Non-nil when point (or POS) is in #+TBLFM line."
  3923. (save-excursion
  3924. (goto-char (or pos (point)))
  3925. (beginning-of-line)
  3926. (and (let ((case-fold-search t)) (looking-at org-TBLFM-regexp))
  3927. (eq (org-element-type (org-element-at-point)) 'table))))
  3928. (defvar org-clock-start-time)
  3929. (defvar org-clock-marker (make-marker)
  3930. "Marker recording the last clock-in.")
  3931. (defvar org-clock-hd-marker (make-marker)
  3932. "Marker recording the last clock-in, but the headline position.")
  3933. (defvar org-clock-heading ""
  3934. "The heading of the current clock entry.")
  3935. (defun org-clock-is-active ()
  3936. "Return the buffer where the clock is currently running.
  3937. Return nil if no clock is running."
  3938. (marker-buffer org-clock-marker))
  3939. (defun org-check-running-clock ()
  3940. "Check if the current buffer contains the running clock.
  3941. If yes, offer to stop it and to save the buffer with the changes."
  3942. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3943. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  3944. (buffer-name))))
  3945. (org-clock-out)
  3946. (when (y-or-n-p "Save changed buffer?")
  3947. (save-buffer))))
  3948. (defun org-clocktable-try-shift (dir n)
  3949. "Check if this line starts a clock table, if yes, shift the time block."
  3950. (when (org-match-line "^[ \t]*#\\+BEGIN:[ \t]+clocktable\\>")
  3951. (org-clocktable-shift dir n)))
  3952. ;;;###autoload
  3953. (defun org-clock-persistence-insinuate ()
  3954. "Set up hooks for clock persistence."
  3955. (require 'org-clock)
  3956. (add-hook 'org-mode-hook 'org-clock-load)
  3957. (add-hook 'kill-emacs-hook 'org-clock-save))
  3958. (defgroup org-archive nil
  3959. "Options concerning archiving in Org-mode."
  3960. :tag "Org Archive"
  3961. :group 'org-structure)
  3962. (defcustom org-archive-location "%s_archive::"
  3963. "The location where subtrees should be archived.
  3964. The value of this variable is a string, consisting of two parts,
  3965. separated by a double-colon. The first part is a filename and
  3966. the second part is a headline.
  3967. When the filename is omitted, archiving happens in the same file.
  3968. %s in the filename will be replaced by the current file
  3969. name (without the directory part). Archiving to a different file
  3970. is useful to keep archived entries from contributing to the
  3971. Org-mode Agenda.
  3972. The archived entries will be filed as subtrees of the specified
  3973. headline. When the headline is omitted, the subtrees are simply
  3974. filed away at the end of the file, as top-level entries. Also in
  3975. the heading you can use %s to represent the file name, this can be
  3976. useful when using the same archive for a number of different files.
  3977. Here are a few examples:
  3978. \"%s_archive::\"
  3979. If the current file is Projects.org, archive in file
  3980. Projects.org_archive, as top-level trees. This is the default.
  3981. \"::* Archived Tasks\"
  3982. Archive in the current file, under the top-level headline
  3983. \"* Archived Tasks\".
  3984. \"~/org/archive.org::\"
  3985. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  3986. \"~/org/archive.org::* From %s\"
  3987. Archive in file ~/org/archive.org (absolute path), under headlines
  3988. \"From FILENAME\" where file name is the current file name.
  3989. \"~/org/datetree.org::datetree/* Finished Tasks\"
  3990. The \"datetree/\" string is special, signifying to archive
  3991. items to the datetree. Items are placed in either the CLOSED
  3992. date of the item, or the current date if there is no CLOSED date.
  3993. The heading will be a subentry to the current date. There doesn't
  3994. need to be a heading, but there always needs to be a slash after
  3995. datetree. For example, to store archived items directly in the
  3996. datetree, use \"~/org/datetree.org::datetree/\".
  3997. \"basement::** Finished Tasks\"
  3998. Archive in file ./basement (relative path), as level 3 trees
  3999. below the level 2 heading \"** Finished Tasks\".
  4000. You may set this option on a per-file basis by adding to the buffer a
  4001. line like
  4002. #+ARCHIVE: basement::** Finished Tasks
  4003. You may also define it locally for a subtree by setting an ARCHIVE property
  4004. in the entry. If such a property is found in an entry, or anywhere up
  4005. the hierarchy, it will be used."
  4006. :group 'org-archive
  4007. :type 'string)
  4008. (defcustom org-agenda-skip-archived-trees t
  4009. "Non-nil means the agenda will skip any items located in archived trees.
  4010. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  4011. variable is no longer recommended, you should leave it at the value t.
  4012. Instead, use the key `v' to cycle the archives-mode in the agenda."
  4013. :group 'org-archive
  4014. :group 'org-agenda-skip
  4015. :type 'boolean)
  4016. (defcustom org-columns-skip-archived-trees t
  4017. "Non-nil means ignore archived trees when creating column view."
  4018. :group 'org-archive
  4019. :group 'org-properties
  4020. :type 'boolean)
  4021. (defcustom org-cycle-open-archived-trees nil
  4022. "Non-nil means `org-cycle' will open archived trees.
  4023. An archived tree is a tree marked with the tag ARCHIVE.
  4024. When nil, archived trees will stay folded. You can still open them with
  4025. normal outline commands like `show-all', but not with the cycling commands."
  4026. :group 'org-archive
  4027. :group 'org-cycle
  4028. :type 'boolean)
  4029. (defcustom org-sparse-tree-open-archived-trees nil
  4030. "Non-nil means sparse tree construction shows matches in archived trees.
  4031. When nil, matches in these trees are highlighted, but the trees are kept in
  4032. collapsed state."
  4033. :group 'org-archive
  4034. :group 'org-sparse-trees
  4035. :type 'boolean)
  4036. (defcustom org-sparse-tree-default-date-type nil
  4037. "The default date type when building a sparse tree.
  4038. When this is nil, a date is a scheduled or a deadline timestamp.
  4039. Otherwise, these types are allowed:
  4040. all: all timestamps
  4041. active: only active timestamps (<...>)
  4042. inactive: only inactive timestamps ([...])
  4043. scheduled: only scheduled timestamps
  4044. deadline: only deadline timestamps"
  4045. :type '(choice (const :tag "Scheduled or deadline" nil)
  4046. (const :tag "All timestamps" all)
  4047. (const :tag "Only active timestamps" active)
  4048. (const :tag "Only inactive timestamps" inactive)
  4049. (const :tag "Only scheduled timestamps" scheduled)
  4050. (const :tag "Only deadline timestamps" deadline)
  4051. (const :tag "Only closed timestamps" closed))
  4052. :version "25.1"
  4053. :package-version '(Org . "8.3")
  4054. :group 'org-sparse-trees)
  4055. (defun org-cycle-hide-archived-subtrees (state)
  4056. "Re-hide all archived subtrees after a visibility state change."
  4057. (when (and (not org-cycle-open-archived-trees)
  4058. (not (memq state '(overview folded))))
  4059. (save-excursion
  4060. (let* ((globalp (memq state '(contents all)))
  4061. (beg (if globalp (point-min) (point)))
  4062. (end (if globalp (point-max) (org-end-of-subtree t))))
  4063. (org-hide-archived-subtrees beg end)
  4064. (goto-char beg)
  4065. (when (looking-at-p (concat ".*:" org-archive-tag ":"))
  4066. (message "%s" (substitute-command-keys
  4067. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  4068. (defun org-force-cycle-archived ()
  4069. "Cycle subtree even if it is archived."
  4070. (interactive)
  4071. (setq this-command 'org-cycle)
  4072. (let ((org-cycle-open-archived-trees t))
  4073. (call-interactively 'org-cycle)))
  4074. (defun org-hide-archived-subtrees (beg end)
  4075. "Re-hide all archived subtrees after a visibility state change."
  4076. (org-with-wide-buffer
  4077. (let ((case-fold-search nil)
  4078. (re (concat org-outline-regexp-bol ".*:" org-archive-tag ":")))
  4079. (goto-char beg)
  4080. ;; Include headline point is currently on.
  4081. (beginning-of-line)
  4082. (while (and (< (point) end) (re-search-forward re end t))
  4083. (when (member org-archive-tag (org-get-tags))
  4084. (org-flag-subtree t)
  4085. (org-end-of-subtree t))))))
  4086. (declare-function outline-end-of-heading "outline" ())
  4087. (declare-function outline-flag-region "outline" (from to flag))
  4088. (defun org-flag-subtree (flag)
  4089. (save-excursion
  4090. (org-back-to-heading t)
  4091. (outline-end-of-heading)
  4092. (outline-flag-region (point)
  4093. (progn (org-end-of-subtree t) (point))
  4094. flag)))
  4095. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  4096. ;; Declare Column View Code
  4097. (declare-function org-columns-get-format-and-top-level "org-colview" ())
  4098. (declare-function org-columns-compute "org-colview" (property))
  4099. ;; Declare ID code
  4100. (declare-function org-id-store-link "org-id")
  4101. (declare-function org-id-locations-load "org-id")
  4102. (declare-function org-id-locations-save "org-id")
  4103. (defvar org-id-track-globally)
  4104. ;;; Variables for pre-computed regular expressions, all buffer local
  4105. (defvar-local org-todo-regexp nil
  4106. "Matches any of the TODO state keywords.")
  4107. (defvar-local org-not-done-regexp nil
  4108. "Matches any of the TODO state keywords except the last one.")
  4109. (defvar-local org-not-done-heading-regexp nil
  4110. "Matches a TODO headline that is not done.")
  4111. (defvar-local org-todo-line-regexp nil
  4112. "Matches a headline and puts TODO state into group 2 if present.")
  4113. (defvar-local org-complex-heading-regexp nil
  4114. "Matches a headline and puts everything into groups:
  4115. group 1: the stars
  4116. group 2: The todo keyword, maybe
  4117. group 3: Priority cookie
  4118. group 4: True headline
  4119. group 5: Tags")
  4120. (defvar-local org-complex-heading-regexp-format nil
  4121. "Printf format to make regexp to match an exact headline.
  4122. This regexp will match the headline of any node which has the
  4123. exact headline text that is put into the format, but may have any
  4124. TODO state, priority and tags.")
  4125. (defvar-local org-todo-line-tags-regexp nil
  4126. "Matches a headline and puts TODO state into group 2 if present.
  4127. Also put tags into group 4 if tags are present.")
  4128. (defconst org-plain-time-of-day-regexp
  4129. (concat
  4130. "\\(\\<[012]?[0-9]"
  4131. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4132. "\\(--?"
  4133. "\\(\\<[012]?[0-9]"
  4134. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4135. "\\)?")
  4136. "Regular expression to match a plain time or time range.
  4137. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  4138. groups carry important information:
  4139. 0 the full match
  4140. 1 the first time, range or not
  4141. 8 the second time, if it is a range.")
  4142. (defconst org-plain-time-extension-regexp
  4143. (concat
  4144. "\\(\\<[012]?[0-9]"
  4145. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4146. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  4147. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  4148. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  4149. groups carry important information:
  4150. 0 the full match
  4151. 7 hours of duration
  4152. 9 minutes of duration")
  4153. (defconst org-stamp-time-of-day-regexp
  4154. (concat
  4155. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  4156. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  4157. "\\(--?"
  4158. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  4159. "Regular expression to match a timestamp time or time range.
  4160. After a match, the following groups carry important information:
  4161. 0 the full match
  4162. 1 date plus weekday, for back referencing to make sure both times are on the same day
  4163. 2 the first time, range or not
  4164. 4 the second time, if it is a range.")
  4165. (defconst org-startup-options
  4166. '(("fold" org-startup-folded t)
  4167. ("overview" org-startup-folded t)
  4168. ("nofold" org-startup-folded nil)
  4169. ("showall" org-startup-folded nil)
  4170. ("showeverything" org-startup-folded showeverything)
  4171. ("content" org-startup-folded content)
  4172. ("indent" org-startup-indented t)
  4173. ("noindent" org-startup-indented nil)
  4174. ("hidestars" org-hide-leading-stars t)
  4175. ("showstars" org-hide-leading-stars nil)
  4176. ("odd" org-odd-levels-only t)
  4177. ("oddeven" org-odd-levels-only nil)
  4178. ("align" org-startup-align-all-tables t)
  4179. ("noalign" org-startup-align-all-tables nil)
  4180. ("inlineimages" org-startup-with-inline-images t)
  4181. ("noinlineimages" org-startup-with-inline-images nil)
  4182. ("latexpreview" org-startup-with-latex-preview t)
  4183. ("nolatexpreview" org-startup-with-latex-preview nil)
  4184. ("customtime" org-display-custom-times t)
  4185. ("logdone" org-log-done time)
  4186. ("lognotedone" org-log-done note)
  4187. ("nologdone" org-log-done nil)
  4188. ("lognoteclock-out" org-log-note-clock-out t)
  4189. ("nolognoteclock-out" org-log-note-clock-out nil)
  4190. ("logrepeat" org-log-repeat state)
  4191. ("lognoterepeat" org-log-repeat note)
  4192. ("logdrawer" org-log-into-drawer t)
  4193. ("nologdrawer" org-log-into-drawer nil)
  4194. ("logstatesreversed" org-log-states-order-reversed t)
  4195. ("nologstatesreversed" org-log-states-order-reversed nil)
  4196. ("nologrepeat" org-log-repeat nil)
  4197. ("logreschedule" org-log-reschedule time)
  4198. ("lognotereschedule" org-log-reschedule note)
  4199. ("nologreschedule" org-log-reschedule nil)
  4200. ("logredeadline" org-log-redeadline time)
  4201. ("lognoteredeadline" org-log-redeadline note)
  4202. ("nologredeadline" org-log-redeadline nil)
  4203. ("logrefile" org-log-refile time)
  4204. ("lognoterefile" org-log-refile note)
  4205. ("nologrefile" org-log-refile nil)
  4206. ("fninline" org-footnote-define-inline t)
  4207. ("nofninline" org-footnote-define-inline nil)
  4208. ("fnlocal" org-footnote-section nil)
  4209. ("fnauto" org-footnote-auto-label t)
  4210. ("fnprompt" org-footnote-auto-label nil)
  4211. ("fnconfirm" org-footnote-auto-label confirm)
  4212. ("fnplain" org-footnote-auto-label plain)
  4213. ("fnadjust" org-footnote-auto-adjust t)
  4214. ("nofnadjust" org-footnote-auto-adjust nil)
  4215. ("constcgs" constants-unit-system cgs)
  4216. ("constSI" constants-unit-system SI)
  4217. ("noptag" org-tag-persistent-alist nil)
  4218. ("hideblocks" org-hide-block-startup t)
  4219. ("nohideblocks" org-hide-block-startup nil)
  4220. ("beamer" org-startup-with-beamer-mode t)
  4221. ("entitiespretty" org-pretty-entities t)
  4222. ("entitiesplain" org-pretty-entities nil))
  4223. "Variable associated with STARTUP options for org-mode.
  4224. Each element is a list of three items: the startup options (as written
  4225. in the #+STARTUP line), the corresponding variable, and the value to set
  4226. this variable to if the option is found. An optional forth element PUSH
  4227. means to push this value onto the list in the variable.")
  4228. (defcustom org-group-tags t
  4229. "When non-nil (the default), use group tags.
  4230. This can be turned on/off through `org-toggle-tags-groups'."
  4231. :group 'org-tags
  4232. :group 'org-startup
  4233. :type 'boolean)
  4234. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  4235. (defun org-toggle-tags-groups ()
  4236. "Toggle support for group tags.
  4237. Support for group tags is controlled by the option
  4238. `org-group-tags', which is non-nil by default."
  4239. (interactive)
  4240. (setq org-group-tags (not org-group-tags))
  4241. (cond ((and (derived-mode-p 'org-agenda-mode)
  4242. org-group-tags)
  4243. (org-agenda-redo))
  4244. ((derived-mode-p 'org-mode)
  4245. (let ((org-inhibit-startup t)) (org-mode))))
  4246. (message "Groups tags support has been turned %s"
  4247. (if org-group-tags "on" "off")))
  4248. (defun org-set-regexps-and-options (&optional tags-only)
  4249. "Precompute regular expressions used in the current buffer.
  4250. When optional argument TAGS-ONLY is non-nil, only compute tags
  4251. related expressions."
  4252. (when (derived-mode-p 'org-mode)
  4253. (let ((alist (org--setup-collect-keywords
  4254. (org-make-options-regexp
  4255. (append '("FILETAGS" "TAGS" "SETUPFILE")
  4256. (and (not tags-only)
  4257. '("ARCHIVE" "CATEGORY" "COLUMNS" "CONSTANTS"
  4258. "LINK" "OPTIONS" "PRIORITIES" "PROPERTY"
  4259. "SEQ_TODO" "STARTUP" "TODO" "TYP_TODO")))))))
  4260. ;; Startup options. Get this early since it does change
  4261. ;; behavior for other options (e.g., tags).
  4262. (let ((startup (cdr (assq 'startup alist))))
  4263. (dolist (option startup)
  4264. (let ((entry (assoc-string option org-startup-options t)))
  4265. (when entry
  4266. (let ((var (nth 1 entry))
  4267. (val (nth 2 entry)))
  4268. (if (not (nth 3 entry)) (set (make-local-variable var) val)
  4269. (unless (listp (symbol-value var))
  4270. (set (make-local-variable var) nil))
  4271. (add-to-list var val)))))))
  4272. (setq-local org-file-tags
  4273. (mapcar #'org-add-prop-inherited
  4274. (cdr (assq 'filetags alist))))
  4275. (setq org-current-tag-alist
  4276. (append org-tag-persistent-alist
  4277. (let ((tags (cdr (assq 'tags alist))))
  4278. (if tags (org-tag-string-to-alist tags)
  4279. org-tag-alist))))
  4280. (setq org-tag-groups-alist
  4281. (org-tag-alist-to-groups org-current-tag-alist))
  4282. (unless tags-only
  4283. ;; File properties.
  4284. (setq-local org-file-properties (cdr (assq 'property alist)))
  4285. ;; Archive location.
  4286. (let ((archive (cdr (assq 'archive alist))))
  4287. (when archive (setq-local org-archive-location archive)))
  4288. ;; Category.
  4289. (let ((cat (org-string-nw-p (cdr (assq 'category alist)))))
  4290. (when cat
  4291. (setq-local org-category (intern cat))
  4292. (setq-local org-file-properties
  4293. (org--update-property-plist
  4294. "CATEGORY" cat org-file-properties))))
  4295. ;; Columns.
  4296. (let ((column (cdr (assq 'columns alist))))
  4297. (when column (setq-local org-columns-default-format column)))
  4298. ;; Constants.
  4299. (setq org-table-formula-constants-local (cdr (assq 'constants alist)))
  4300. ;; Link abbreviations.
  4301. (let ((links (cdr (assq 'link alist))))
  4302. (when links (setq org-link-abbrev-alist-local (nreverse links))))
  4303. ;; Priorities.
  4304. (let ((priorities (cdr (assq 'priorities alist))))
  4305. (when priorities
  4306. (setq-local org-highest-priority (nth 0 priorities))
  4307. (setq-local org-lowest-priority (nth 1 priorities))
  4308. (setq-local org-default-priority (nth 2 priorities))))
  4309. ;; Scripts.
  4310. (let ((scripts (assq 'scripts alist)))
  4311. (when scripts
  4312. (setq-local org-use-sub-superscripts (cdr scripts))))
  4313. ;; TODO keywords.
  4314. (setq-local org-todo-kwd-alist nil)
  4315. (setq-local org-todo-key-alist nil)
  4316. (setq-local org-todo-key-trigger nil)
  4317. (setq-local org-todo-keywords-1 nil)
  4318. (setq-local org-done-keywords nil)
  4319. (setq-local org-todo-heads nil)
  4320. (setq-local org-todo-sets nil)
  4321. (setq-local org-todo-log-states nil)
  4322. (let ((todo-sequences
  4323. (or (nreverse (cdr (assq 'todo alist)))
  4324. (let ((d (default-value 'org-todo-keywords)))
  4325. (if (not (stringp (car d))) d
  4326. ;; XXX: Backward compatibility code.
  4327. (list (cons org-todo-interpretation d)))))))
  4328. (dolist (sequence todo-sequences)
  4329. (let* ((sequence (or (run-hook-with-args-until-success
  4330. 'org-todo-setup-filter-hook sequence)
  4331. sequence))
  4332. (sequence-type (car sequence))
  4333. (keywords (cdr sequence))
  4334. (sep (member "|" keywords))
  4335. names alist)
  4336. (dolist (k (remove "|" keywords))
  4337. (unless (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$"
  4338. k)
  4339. (error "Invalid TODO keyword %s" k))
  4340. (let ((name (match-string 1 k))
  4341. (key (match-string 2 k))
  4342. (log (org-extract-log-state-settings k)))
  4343. (push name names)
  4344. (push (cons name (and key (string-to-char key))) alist)
  4345. (when log (push log org-todo-log-states))))
  4346. (let* ((names (nreverse names))
  4347. (done (if sep (org-remove-keyword-keys (cdr sep))
  4348. (last names)))
  4349. (head (car names))
  4350. (tail (list sequence-type head (car done) (org-last done))))
  4351. (add-to-list 'org-todo-heads head 'append)
  4352. (push names org-todo-sets)
  4353. (setq org-done-keywords (append org-done-keywords done nil))
  4354. (setq org-todo-keywords-1 (append org-todo-keywords-1 names nil))
  4355. (setq org-todo-key-alist
  4356. (append org-todo-key-alist
  4357. (and alist
  4358. (append '((:startgroup))
  4359. (nreverse alist)
  4360. '((:endgroup))))))
  4361. (dolist (k names) (push (cons k tail) org-todo-kwd-alist))))))
  4362. (setq org-todo-sets (nreverse org-todo-sets)
  4363. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  4364. org-todo-key-trigger (delq nil (mapcar #'cdr org-todo-key-alist))
  4365. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist))
  4366. ;; Compute the regular expressions and other local variables.
  4367. ;; Using `org-outline-regexp-bol' would complicate them much,
  4368. ;; because of the fixed white space at the end of that string.
  4369. (unless org-done-keywords
  4370. (setq org-done-keywords
  4371. (and org-todo-keywords-1 (last org-todo-keywords-1))))
  4372. (setq org-not-done-keywords
  4373. (org-delete-all org-done-keywords
  4374. (copy-sequence org-todo-keywords-1))
  4375. org-todo-regexp (regexp-opt org-todo-keywords-1 t)
  4376. org-not-done-regexp (regexp-opt org-not-done-keywords t)
  4377. org-not-done-heading-regexp
  4378. (format org-heading-keyword-regexp-format org-not-done-regexp)
  4379. org-todo-line-regexp
  4380. (format org-heading-keyword-maybe-regexp-format org-todo-regexp)
  4381. org-complex-heading-regexp
  4382. (concat "^\\(\\*+\\)"
  4383. "\\(?: +" org-todo-regexp "\\)?"
  4384. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4385. "\\(?: +\\(.*?\\)\\)??"
  4386. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?")
  4387. "[ \t]*$")
  4388. org-complex-heading-regexp-format
  4389. (concat "^\\(\\*+\\)"
  4390. "\\(?: +" org-todo-regexp "\\)?"
  4391. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4392. "\\(?: +"
  4393. ;; Stats cookies can be stuck to body.
  4394. "\\(?:\\[[0-9%%/]+\\] *\\)*"
  4395. "\\(%s\\)"
  4396. "\\(?: *\\[[0-9%%/]+\\]\\)*"
  4397. "\\)"
  4398. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%%:]+:\\)\\)?")
  4399. "[ \t]*$")
  4400. org-todo-line-tags-regexp
  4401. (concat "^\\(\\*+\\)"
  4402. "\\(?: +" org-todo-regexp "\\)?"
  4403. "\\(?: +\\(.*?\\)\\)??"
  4404. (org-re "\\(?:[ \t]+\\(:[[:alnum:]:_@#%]+:\\)\\)?")
  4405. "[ \t]*$"))
  4406. (org-compute-latex-and-related-regexp)))))
  4407. (defun org--setup-collect-keywords (regexp &optional files alist)
  4408. "Return setup keywords values as an alist.
  4409. REGEXP matches a subset of setup keywords. FILES is a list of
  4410. file names already visited. It is used to avoid circular setup
  4411. files. ALIST, when non-nil, is the alist computed so far.
  4412. Return value contains the following keys: `archive', `category',
  4413. `columns', `constants', `filetags', `link', `priorities',
  4414. `property', `scripts', `startup', `tags' and `todo'."
  4415. (org-with-wide-buffer
  4416. (goto-char (point-min))
  4417. (let ((case-fold-search t))
  4418. (while (re-search-forward regexp nil t)
  4419. (let ((element (org-element-at-point)))
  4420. (when (eq (org-element-type element) 'keyword)
  4421. (let ((key (org-element-property :key element))
  4422. (value (org-element-property :value element)))
  4423. (cond
  4424. ((equal key "ARCHIVE")
  4425. (when (org-string-nw-p value)
  4426. (push (cons 'archive value) alist)))
  4427. ((equal key "CATEGORY") (push (cons 'category value) alist))
  4428. ((equal key "COLUMNS") (push (cons 'columns value) alist))
  4429. ((equal key "CONSTANTS")
  4430. (let* ((constants (assq 'constants alist))
  4431. (store (cdr constants)))
  4432. (dolist (pair (org-split-string value))
  4433. (when (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)"
  4434. pair)
  4435. (let* ((name (match-string 1 pair))
  4436. (value (match-string 2 pair))
  4437. (old (assoc name store)))
  4438. (if old (setcdr old value)
  4439. (push (cons name value) store)))))
  4440. (if constants (setcdr constants store)
  4441. (push (cons 'constants store) alist))))
  4442. ((equal key "FILETAGS")
  4443. (when (org-string-nw-p value)
  4444. (let ((old (assq 'filetags alist))
  4445. (new (apply #'nconc
  4446. (mapcar (lambda (x) (org-split-string x ":"))
  4447. (org-split-string value)))))
  4448. (if old (setcdr old (append new (cdr old)))
  4449. (push (cons 'filetags new) alist)))))
  4450. ((equal key "LINK")
  4451. (when (string-match "\\`\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  4452. (let ((links (assq 'link alist))
  4453. (pair (cons (org-match-string-no-properties 1 value)
  4454. (org-match-string-no-properties 2 value))))
  4455. (if links (push pair (cdr links))
  4456. (push (list 'link pair) alist)))))
  4457. ((equal key "OPTIONS")
  4458. (when (and (org-string-nw-p value)
  4459. (string-match "\\^:\\(t\\|nil\\|{}\\)" value))
  4460. (push (cons 'scripts (read (match-string 1 value))) alist)))
  4461. ((equal key "PRIORITIES")
  4462. (push (cons 'priorities
  4463. (let ((prio (org-split-string value)))
  4464. (if (< (length prio) 3) '(?A ?C ?B)
  4465. (mapcar #'string-to-char prio))))
  4466. alist))
  4467. ((equal key "PROPERTY")
  4468. (when (string-match "\\(\\S-+\\)[ \t]+\\(.*\\)" value)
  4469. (let* ((property (assq 'property alist))
  4470. (value (org--update-property-plist
  4471. (org-match-string-no-properties 1 value)
  4472. (org-match-string-no-properties 2 value)
  4473. (cdr property))))
  4474. (if property (setcdr property value)
  4475. (push (cons 'property value) alist)))))
  4476. ((equal key "STARTUP")
  4477. (let ((startup (assq 'startup alist)))
  4478. (if startup
  4479. (setcdr startup
  4480. (append (cdr startup) (org-split-string value)))
  4481. (push (cons 'startup (org-split-string value)) alist))))
  4482. ((equal key "TAGS")
  4483. (let ((tag-cell (assq 'tags alist)))
  4484. (if tag-cell
  4485. (setcdr tag-cell (concat (cdr tag-cell) "\n" value))
  4486. (push (cons 'tags value) alist))))
  4487. ((member key '("TODO" "SEQ_TODO" "TYP_TODO"))
  4488. (let ((todo (assq 'todo alist))
  4489. (value (cons (if (equal key "TYP_TODO") 'type 'sequence)
  4490. (org-split-string value))))
  4491. (if todo (push value (cdr todo))
  4492. (push (list 'todo value) alist))))
  4493. ((equal key "SETUPFILE")
  4494. (unless buffer-read-only ; Do not check in Gnus messages.
  4495. (let ((f (and (org-string-nw-p value)
  4496. (expand-file-name
  4497. (org-remove-double-quotes value)))))
  4498. (when (and f (file-readable-p f) (not (member f files)))
  4499. (with-temp-buffer
  4500. (setq default-directory (file-name-directory f))
  4501. (insert-file-contents f)
  4502. (setq alist
  4503. ;; Fake Org mode to benefit from cache
  4504. ;; without recurring needlessly.
  4505. (let ((major-mode 'org-mode))
  4506. (org--setup-collect-keywords
  4507. regexp (cons f files) alist)))))))))))))))
  4508. alist)
  4509. (defun org-tag-string-to-alist (s)
  4510. "Return tag alist associated to string S.
  4511. S is a value for TAGS keyword or produced with
  4512. `org-tag-alist-to-string'. Return value is an alist suitable for
  4513. `org-tag-alist' or `org-tag-persistent-alist'."
  4514. (let ((lines (mapcar #'split-string (split-string s "\n" t)))
  4515. (tag-re (concat "\\`\\([[:alnum:]_@#%]+"
  4516. "\\|{.+?}\\)" ; regular expression
  4517. "\\(?:(\\(.\\))\\)?\\'"))
  4518. alist group-flag)
  4519. (dolist (tokens lines (cdr (nreverse alist)))
  4520. (push '(:newline) alist)
  4521. (while tokens
  4522. (let ((token (pop tokens)))
  4523. (pcase token
  4524. ("{"
  4525. (push '(:startgroup) alist)
  4526. (when (equal (nth 1 tokens) ":") (setq group-flag t)))
  4527. ("}"
  4528. (push '(:endgroup) alist)
  4529. (setq group-flag nil))
  4530. ("["
  4531. (push '(:startgrouptag) alist)
  4532. (when (equal (nth 1 tokens) ":") (setq group-flag t)))
  4533. ("]"
  4534. (push '(:endgrouptag) alist)
  4535. (setq group-flag nil))
  4536. (":"
  4537. (push '(:grouptags) alist))
  4538. ((guard (string-match tag-re token))
  4539. (let ((tag (match-string 1 token))
  4540. (key (and (match-beginning 2)
  4541. (string-to-char (match-string 2 token)))))
  4542. ;; Push all tags in groups, no matter if they already
  4543. ;; appear somewhere else in the list.
  4544. (when (or group-flag (not (assoc tag alist)))
  4545. (push (cons tag key) alist))))))))))
  4546. (defun org-tag-alist-to-string (alist &optional skip-key)
  4547. "Return tag string associated to ALIST.
  4548. ALIST is an alist, as defined in `org-tag-alist' or
  4549. `org-tag-persistent-alist', or produced with
  4550. `org-tag-string-to-alist'.
  4551. Return value is a string suitable as a value for \"TAGS\"
  4552. keyword.
  4553. When optional argument SKIP-KEY is non-nil, skip selection keys
  4554. next to tags."
  4555. (mapconcat (lambda (token)
  4556. (pcase token
  4557. (`(:startgroup) "{")
  4558. (`(:endgroup) "}")
  4559. (`(:startgrouptag) "[")
  4560. (`(:endgrouptag) "]")
  4561. (`(:grouptags) ":")
  4562. (`(:newline) "\\n")
  4563. ((and
  4564. (guard (not skip-key))
  4565. `(,(and tag (pred stringp)) . ,(and key (pred characterp))))
  4566. (format "%s(%c)" tag key))
  4567. (`(,(and tag (pred stringp)) . ,_) tag)
  4568. (_ (user-error "Invalid tag token: %S" token))))
  4569. alist
  4570. " "))
  4571. (defun org-tag-alist-to-groups (alist)
  4572. "Return group alist from tag ALIST.
  4573. ALIST is an alist, as defined in `org-tag-alist' or
  4574. `org-tag-persistent-alist', or produced with
  4575. `org-tag-string-to-alist'. Return value is an alist following
  4576. the pattern (GROUP-TAG TAGS) where GROUP-TAG is the tag, as
  4577. a string, summarizing TAGS, as a list of strings."
  4578. (let (groups group-status current-group)
  4579. (dolist (token alist (nreverse groups))
  4580. (pcase token
  4581. (`(,(or :startgroup :startgrouptag)) (setq group-status t))
  4582. (`(,(or :endgroup :endgrouptag))
  4583. (when (eq group-status 'append)
  4584. (push (nreverse current-group) groups))
  4585. (setq group-status nil))
  4586. (`(:grouptags) (setq group-status 'append))
  4587. ((and `(,tag . ,_) (guard group-status))
  4588. (if (eq group-status 'append) (push tag current-group)
  4589. (setq current-group (list tag))))
  4590. (_ nil)))))
  4591. (defun org-file-contents (file &optional noerror)
  4592. "Return the contents of FILE, as a string."
  4593. (if (and file (file-readable-p file))
  4594. (with-temp-buffer
  4595. (insert-file-contents file)
  4596. (buffer-string))
  4597. (funcall (if noerror 'message 'error)
  4598. "Cannot read file \"%s\"%s"
  4599. file
  4600. (let ((from (buffer-file-name (buffer-base-buffer))))
  4601. (if from (concat " (referenced in file \"" from "\")") "")))))
  4602. (defun org-extract-log-state-settings (x)
  4603. "Extract the log state setting from a TODO keyword string.
  4604. This will extract info from a string like \"WAIT(w@/!)\"."
  4605. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  4606. (let ((kw (match-string 1 x))
  4607. (log1 (and (match-end 3) (match-string 3 x)))
  4608. (log2 (and (match-end 4) (match-string 4 x))))
  4609. (and (or log1 log2)
  4610. (list kw
  4611. (and log1 (if (equal log1 "!") 'time 'note))
  4612. (and log2 (if (equal log2 "!") 'time 'note)))))))
  4613. (defun org-remove-keyword-keys (list)
  4614. "Remove a pair of parenthesis at the end of each string in LIST."
  4615. (mapcar (lambda (x)
  4616. (if (string-match "(.*)$" x)
  4617. (substring x 0 (match-beginning 0))
  4618. x))
  4619. list))
  4620. (defun org-assign-fast-keys (alist)
  4621. "Assign fast keys to a keyword-key alist.
  4622. Respect keys that are already there."
  4623. (let (new e (alt ?0))
  4624. (while (setq e (pop alist))
  4625. (if (or (memq (car e) '(:newline :grouptags :endgroup :startgroup))
  4626. (cdr e)) ;; Key already assigned.
  4627. (push e new)
  4628. (let ((clist (string-to-list (downcase (car e))))
  4629. (used (append new alist)))
  4630. (when (= (car clist) ?@)
  4631. (pop clist))
  4632. (while (and clist (rassoc (car clist) used))
  4633. (pop clist))
  4634. (unless clist
  4635. (while (rassoc alt used)
  4636. (cl-incf alt)))
  4637. (push (cons (car e) (or (car clist) alt)) new))))
  4638. (nreverse new)))
  4639. ;;; Some variables used in various places
  4640. (defvar org-window-configuration nil
  4641. "Used in various places to store a window configuration.")
  4642. (defvar org-selected-window nil
  4643. "Used in various places to store a window configuration.")
  4644. (defvar org-finish-function nil
  4645. "Function to be called when `C-c C-c' is used.
  4646. This is for getting out of special buffers like capture.")
  4647. (defvar org-last-state)
  4648. ;; Defined somewhere in this file, but used before definition.
  4649. (defvar org-entities) ;; defined in org-entities.el
  4650. (defvar org-struct-menu)
  4651. (defvar org-org-menu)
  4652. (defvar org-tbl-menu)
  4653. ;;;; Define the Org-mode
  4654. ;; We use a before-change function to check if a table might need
  4655. ;; an update.
  4656. (defvar org-table-may-need-update t
  4657. "Indicates that a table might need an update.
  4658. This variable is set by `org-before-change-function'.
  4659. `org-table-align' sets it back to nil.")
  4660. (defun org-before-change-function (_beg _end)
  4661. "Every change indicates that a table might need an update."
  4662. (setq org-table-may-need-update t))
  4663. (defvar org-mode-map)
  4664. (defvar org-inhibit-startup-visibility-stuff nil) ; Dynamically-scoped param.
  4665. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  4666. (defvar org-inhibit-logging nil) ; Dynamically-scoped param.
  4667. (defvar org-inhibit-blocking nil) ; Dynamically-scoped param.
  4668. (defvar org-table-buffer-is-an nil)
  4669. (defvar bidi-paragraph-direction)
  4670. (defvar buffer-face-mode-face)
  4671. (require 'outline)
  4672. (when (and (not (keymapp outline-mode-map)) (featurep 'allout))
  4673. (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"))
  4674. (require 'noutline "noutline" 'noerror) ;; stock XEmacs does not have it
  4675. ;; Other stuff we need.
  4676. (require 'time-date)
  4677. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  4678. (require 'easymenu)
  4679. (autoload 'easy-menu-add "easymenu")
  4680. (require 'overlay)
  4681. ;; (require 'org-macs) moved higher up in the file before it is first used
  4682. (require 'org-entities)
  4683. ;; (require 'org-compat) moved higher up in the file before it is first used
  4684. (require 'org-faces)
  4685. (require 'org-list)
  4686. (require 'org-pcomplete)
  4687. (require 'org-src)
  4688. (require 'org-footnote)
  4689. (require 'org-macro)
  4690. ;; babel
  4691. (require 'ob)
  4692. ;;;###autoload
  4693. (define-derived-mode org-mode outline-mode "Org"
  4694. "Outline-based notes management and organizer, alias
  4695. \"Carsten's outline-mode for keeping track of everything.\"
  4696. Org-mode develops organizational tasks around a NOTES file which
  4697. contains information about projects as plain text. Org-mode is
  4698. implemented on top of outline-mode, which is ideal to keep the content
  4699. of large files well structured. It supports ToDo items, deadlines and
  4700. time stamps, which magically appear in the diary listing of the Emacs
  4701. calendar. Tables are easily created with a built-in table editor.
  4702. Plain text URL-like links connect to websites, emails (VM), Usenet
  4703. messages (Gnus), BBDB entries, and any files related to the project.
  4704. For printing and sharing of notes, an Org-mode file (or a part of it)
  4705. can be exported as a structured ASCII or HTML file.
  4706. The following commands are available:
  4707. \\{org-mode-map}"
  4708. ;; Get rid of Outline menus, they are not needed
  4709. ;; Need to do this here because define-derived-mode sets up
  4710. ;; the keymap so late. Still, it is a waste to call this each time
  4711. ;; we switch another buffer into org-mode.
  4712. (if (featurep 'xemacs)
  4713. (when (boundp 'outline-mode-menu-heading)
  4714. ;; Assume this is Greg's port, it uses easymenu
  4715. (easy-menu-remove outline-mode-menu-heading)
  4716. (easy-menu-remove outline-mode-menu-show)
  4717. (easy-menu-remove outline-mode-menu-hide))
  4718. (define-key org-mode-map [menu-bar headings] 'undefined)
  4719. (define-key org-mode-map [menu-bar hide] 'undefined)
  4720. (define-key org-mode-map [menu-bar show] 'undefined))
  4721. (org-load-modules-maybe)
  4722. (when (featurep 'xemacs)
  4723. (easy-menu-add org-org-menu)
  4724. (easy-menu-add org-tbl-menu))
  4725. (org-install-agenda-files-menu)
  4726. (when org-descriptive-links (add-to-invisibility-spec '(org-link)))
  4727. (add-to-invisibility-spec '(org-cwidth))
  4728. (add-to-invisibility-spec '(org-hide-block . t))
  4729. (when (featurep 'xemacs)
  4730. (setq-local line-move-ignore-invisible t))
  4731. (setq-local outline-regexp org-outline-regexp)
  4732. (setq-local outline-level 'org-outline-level)
  4733. (setq bidi-paragraph-direction 'left-to-right)
  4734. (when (and org-ellipsis
  4735. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  4736. (fboundp 'make-glyph-code))
  4737. (unless org-display-table
  4738. (setq org-display-table (make-display-table)))
  4739. (set-display-table-slot
  4740. org-display-table 4
  4741. (vconcat (mapcar
  4742. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  4743. org-ellipsis)))
  4744. (if (stringp org-ellipsis) org-ellipsis "..."))))
  4745. (setq buffer-display-table org-display-table))
  4746. (org-set-regexps-and-options)
  4747. (org-set-font-lock-defaults)
  4748. (when (and org-tag-faces (not org-tags-special-faces-re))
  4749. ;; tag faces set outside customize.... force initialization.
  4750. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  4751. ;; Calc embedded
  4752. (setq-local calc-embedded-open-mode "# ")
  4753. ;; Modify a few syntax entries
  4754. (modify-syntax-entry ?@ "w")
  4755. (modify-syntax-entry ?\" "\"")
  4756. (modify-syntax-entry ?\\ "_")
  4757. (modify-syntax-entry ?~ "_")
  4758. (setq-local font-lock-unfontify-region-function 'org-unfontify-region)
  4759. ;; Activate before-change-function
  4760. (setq-local org-table-may-need-update t)
  4761. (org-add-hook 'before-change-functions 'org-before-change-function nil 'local)
  4762. ;; Check for running clock before killing a buffer
  4763. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  4764. ;; Initialize macros templates.
  4765. (org-macro-initialize-templates)
  4766. ;; Initialize radio targets.
  4767. (org-update-radio-target-regexp)
  4768. ;; Indentation.
  4769. (setq-local indent-line-function 'org-indent-line)
  4770. (setq-local indent-region-function 'org-indent-region)
  4771. ;; Filling and auto-filling.
  4772. (org-setup-filling)
  4773. ;; Comments.
  4774. (org-setup-comments-handling)
  4775. ;; Initialize cache.
  4776. (org-element-cache-reset)
  4777. ;; Beginning/end of defun
  4778. (setq-local beginning-of-defun-function 'org-backward-element)
  4779. (setq-local end-of-defun-function
  4780. (lambda ()
  4781. (if (not (org-at-heading-p))
  4782. (org-forward-element)
  4783. (org-forward-element)
  4784. (forward-char -1))))
  4785. ;; Next error for sparse trees
  4786. (setq-local next-error-function 'org-occur-next-match)
  4787. ;; Make sure dependence stuff works reliably, even for users who set it
  4788. ;; too late :-(
  4789. (if org-enforce-todo-dependencies
  4790. (add-hook 'org-blocker-hook
  4791. 'org-block-todo-from-children-or-siblings-or-parent)
  4792. (remove-hook 'org-blocker-hook
  4793. 'org-block-todo-from-children-or-siblings-or-parent))
  4794. (if org-enforce-todo-checkbox-dependencies
  4795. (add-hook 'org-blocker-hook
  4796. 'org-block-todo-from-checkboxes)
  4797. (remove-hook 'org-blocker-hook
  4798. 'org-block-todo-from-checkboxes))
  4799. ;; Align options lines
  4800. (setq-local
  4801. align-mode-rules-list
  4802. '((org-in-buffer-settings
  4803. (regexp . "^[ \t]*#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  4804. (modes . '(org-mode)))))
  4805. ;; Imenu
  4806. (setq-local imenu-create-index-function 'org-imenu-get-tree)
  4807. ;; Make isearch reveal context
  4808. (setq-local outline-isearch-open-invisible-function
  4809. (lambda (&rest _) (org-show-context 'isearch)))
  4810. ;; Setup the pcomplete hooks
  4811. (setq-local pcomplete-command-completion-function 'org-pcomplete-initial)
  4812. (setq-local pcomplete-command-name-function 'org-command-at-point)
  4813. (setq-local pcomplete-default-completion-function 'ignore)
  4814. (setq-local pcomplete-parse-arguments-function 'org-parse-arguments)
  4815. (setq-local pcomplete-termination-string "")
  4816. (setq-local buffer-face-mode-face 'org-default)
  4817. ;; If empty file that did not turn on Org mode automatically, make
  4818. ;; it to.
  4819. (when (and org-insert-mode-line-in-empty-file
  4820. (org-called-interactively-p 'any)
  4821. (= (point-min) (point-max)))
  4822. (insert "# -*- mode: org -*-\n\n"))
  4823. (unless org-inhibit-startup
  4824. (org-unmodified
  4825. (when org-startup-with-beamer-mode (org-beamer-mode))
  4826. (when org-startup-align-all-tables
  4827. (org-table-map-tables #'org-table-align t))
  4828. (when org-startup-with-inline-images (org-display-inline-images))
  4829. (when org-startup-with-latex-preview (org-toggle-latex-fragment))
  4830. (unless org-inhibit-startup-visibility-stuff (org-set-startup-visibility))
  4831. (when org-startup-truncated (setq truncate-lines t))
  4832. (when org-startup-indented (require 'org-indent) (org-indent-mode 1))
  4833. (org-refresh-effort-properties)))
  4834. ;; Try to set `org-hide' face correctly.
  4835. (let ((foreground (org-find-invisible-foreground)))
  4836. (when foreground
  4837. (set-face-foreground 'org-hide foreground))))
  4838. ;; Update `customize-package-emacs-version-alist'
  4839. (add-to-list 'customize-package-emacs-version-alist
  4840. '(Org ("6.21b" . "23.1") ("6.33x" . "23.2")
  4841. ("7.8.11" . "24.1") ("7.9.4" . "24.3")
  4842. ("8.2.6" . "24.4") ("8.3" . "25.1")))
  4843. (defvar org-mode-transpose-word-syntax-table
  4844. (let ((st (make-syntax-table text-mode-syntax-table)))
  4845. (dolist (c org-emphasis-alist st)
  4846. (modify-syntax-entry (string-to-char (car c)) "w p" st))))
  4847. (when (fboundp 'abbrev-table-put)
  4848. (abbrev-table-put org-mode-abbrev-table
  4849. :parents (list text-mode-abbrev-table)))
  4850. (defun org-find-invisible-foreground ()
  4851. (let ((candidates (remove
  4852. "unspecified-bg"
  4853. (nconc
  4854. (list (face-background 'default)
  4855. (face-background 'org-default))
  4856. (mapcar
  4857. (lambda (alist)
  4858. (when (boundp alist)
  4859. (cdr (assoc 'background-color (symbol-value alist)))))
  4860. '(default-frame-alist initial-frame-alist window-system-default-frame-alist))
  4861. (list (face-foreground 'org-hide))))))
  4862. (car (remove nil candidates))))
  4863. (defun org-current-time (&optional rounding-minutes past)
  4864. "Current time, possibly rounded to ROUNDING-MINUTES.
  4865. When ROUNDING-MINUTES is not an integer, fall back on the car of
  4866. `org-time-stamp-rounding-minutes'. When PAST is non-nil, ensure
  4867. the rounding returns a past time."
  4868. (let ((r (or (and (integerp rounding-minutes) rounding-minutes)
  4869. (car org-time-stamp-rounding-minutes)))
  4870. (time (decode-time)) res)
  4871. (if (< r 1)
  4872. (current-time)
  4873. (setq res
  4874. (apply 'encode-time
  4875. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  4876. (nthcdr 2 time))))
  4877. (if (and past (< (org-float-time (time-subtract (current-time) res)) 0))
  4878. (seconds-to-time (- (org-float-time res) (* r 60)))
  4879. res))))
  4880. (defun org-today ()
  4881. "Return today date, considering `org-extend-today-until'."
  4882. (time-to-days
  4883. (time-subtract (current-time)
  4884. (list 0 (* 3600 org-extend-today-until) 0))))
  4885. ;;;; Font-Lock stuff, including the activators
  4886. (defvar org-mouse-map (make-sparse-keymap))
  4887. (org-defkey org-mouse-map [mouse-2] 'org-open-at-mouse)
  4888. (org-defkey org-mouse-map [mouse-3] 'org-find-file-at-mouse)
  4889. (when org-mouse-1-follows-link
  4890. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  4891. (when org-tab-follows-link
  4892. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  4893. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  4894. (require 'font-lock)
  4895. (defconst org-non-link-chars "]\t\n\r<>")
  4896. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "file+emacs"
  4897. "file+sys" "news" "shell" "elisp" "doi" "message"
  4898. "help"))
  4899. (defvar org-link-types-re nil
  4900. "Matches a link that has a url-like prefix like \"http:\"")
  4901. (defvar org-link-re-with-space nil
  4902. "Matches a link with spaces, optional angular brackets around it.")
  4903. (defvar org-link-re-with-space2 nil
  4904. "Matches a link with spaces, optional angular brackets around it.")
  4905. (defvar org-link-re-with-space3 nil
  4906. "Matches a link with spaces, only for internal part in bracket links.")
  4907. (defvar org-angle-link-re nil
  4908. "Matches link with angular brackets, spaces are allowed.")
  4909. (defvar org-plain-link-re nil
  4910. "Matches plain link, without spaces.")
  4911. (defvar org-bracket-link-regexp nil
  4912. "Matches a link in double brackets.")
  4913. (defvar org-bracket-link-analytic-regexp nil
  4914. "Regular expression used to analyze links.
  4915. Here is what the match groups contain after a match:
  4916. 1: http:
  4917. 2: http
  4918. 3: path
  4919. 4: [desc]
  4920. 5: desc")
  4921. (defvar org-bracket-link-analytic-regexp++ nil
  4922. "Like `org-bracket-link-analytic-regexp', but include coderef internal type.")
  4923. (defvar org-any-link-re nil
  4924. "Regular expression matching any link.")
  4925. (defconst org-match-sexp-depth 3
  4926. "Number of stacked braces for sub/superscript matching.")
  4927. (defun org-create-multibrace-regexp (left right n)
  4928. "Create a regular expression which will match a balanced sexp.
  4929. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  4930. as single character strings.
  4931. The regexp returned will match the entire expression including the
  4932. delimiters. It will also define a single group which contains the
  4933. match except for the outermost delimiters. The maximum depth of
  4934. stacked delimiters is N. Escaping delimiters is not possible."
  4935. (let* ((nothing (concat "[^" left right "]*?"))
  4936. (or "\\|")
  4937. (re nothing)
  4938. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  4939. (while (> n 1)
  4940. (setq n (1- n)
  4941. re (concat re or next)
  4942. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  4943. (concat left "\\(" re "\\)" right)))
  4944. (defconst org-match-substring-regexp
  4945. (concat
  4946. "\\(\\S-\\)\\([_^]\\)\\("
  4947. "\\(?:" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4948. "\\|"
  4949. "\\(?:" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  4950. "\\|"
  4951. "\\(?:\\*\\|[+-]?[[:alnum:].,\\]*[[:alnum:]]\\)\\)")
  4952. "The regular expression matching a sub- or superscript.")
  4953. (defconst org-match-substring-with-braces-regexp
  4954. (concat
  4955. "\\(\\S-\\)\\([_^]\\)"
  4956. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)")
  4957. "The regular expression matching a sub- or superscript, forcing braces.")
  4958. (defun org-make-link-regexps ()
  4959. "Update the link regular expressions.
  4960. This should be called after the variable `org-link-types' has changed."
  4961. (let ((types-re (regexp-opt org-link-types t)))
  4962. (setq org-link-types-re
  4963. (concat "\\`" types-re ":")
  4964. org-link-re-with-space
  4965. (concat "<?" types-re ":"
  4966. "\\([^" org-non-link-chars " ]"
  4967. "[^" org-non-link-chars "]*"
  4968. "[^" org-non-link-chars " ]\\)>?")
  4969. org-link-re-with-space2
  4970. (concat "<?" types-re ":"
  4971. "\\([^" org-non-link-chars " ]"
  4972. "[^\t\n\r]*"
  4973. "[^" org-non-link-chars " ]\\)>?")
  4974. org-link-re-with-space3
  4975. (concat "<?" types-re ":"
  4976. "\\([^" org-non-link-chars " ]"
  4977. "[^\t\n\r]*\\)")
  4978. org-angle-link-re
  4979. (format "<%s:\\([^>\n]*\\(?:\n[ \t]*[^> \t\n][^>\n]*\\)*\\)>"
  4980. types-re)
  4981. org-plain-link-re
  4982. (concat
  4983. "\\<" types-re ":"
  4984. (org-re "\\([^ \t\n()<>]+\\(?:([[:word:]0-9_]+)\\|\\([^[:punct:] \t\n]\\|/\\)\\)\\)"))
  4985. ;; "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  4986. org-bracket-link-regexp
  4987. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  4988. org-bracket-link-analytic-regexp
  4989. (concat
  4990. "\\[\\["
  4991. "\\(" types-re ":\\)?"
  4992. "\\([^]]+\\)"
  4993. "\\]"
  4994. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  4995. "\\]")
  4996. org-bracket-link-analytic-regexp++
  4997. (concat
  4998. "\\[\\["
  4999. "\\(" (regexp-opt (cons "coderef" org-link-types) t) ":\\)?"
  5000. "\\([^]]+\\)"
  5001. "\\]"
  5002. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  5003. "\\]")
  5004. org-any-link-re
  5005. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  5006. org-angle-link-re "\\)\\|\\("
  5007. org-plain-link-re "\\)"))))
  5008. (org-make-link-regexps)
  5009. (defvar org-emph-face nil)
  5010. (defun org-do-emphasis-faces (limit)
  5011. "Run through the buffer and emphasize strings."
  5012. (let (rtn a)
  5013. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  5014. (let* ((border (char-after (match-beginning 3)))
  5015. (bre (regexp-quote (char-to-string border))))
  5016. (when (and (not (= border (char-after (match-beginning 4))))
  5017. (not (string-match-p (concat bre ".*" bre)
  5018. (replace-regexp-in-string
  5019. "\n" " "
  5020. (substring (match-string 2) 1 -1)))))
  5021. (setq rtn t)
  5022. (setq a (assoc (match-string 3) org-emphasis-alist))
  5023. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  5024. 'face
  5025. (nth 1 a))
  5026. (and (nth 2 a)
  5027. (org-remove-flyspell-overlays-in
  5028. (match-beginning 0) (match-end 0)))
  5029. (add-text-properties (match-beginning 2) (match-end 2)
  5030. '(font-lock-multiline t org-emphasis t))
  5031. (when org-hide-emphasis-markers
  5032. (add-text-properties (match-end 4) (match-beginning 5)
  5033. '(invisible org-link))
  5034. (add-text-properties (match-beginning 3) (match-end 3)
  5035. '(invisible org-link)))))
  5036. (goto-char (1+ (match-beginning 0))))
  5037. rtn))
  5038. (defun org-emphasize (&optional char)
  5039. "Insert or change an emphasis, i.e. a font like bold or italic.
  5040. If there is an active region, change that region to a new emphasis.
  5041. If there is no region, just insert the marker characters and position
  5042. the cursor between them.
  5043. CHAR should be the marker character. If it is a space, it means to
  5044. remove the emphasis of the selected region.
  5045. If CHAR is not given (for example in an interactive call) it will be
  5046. prompted for."
  5047. (interactive)
  5048. (let ((erc org-emphasis-regexp-components)
  5049. (string "") beg end move s)
  5050. (if (org-region-active-p)
  5051. (setq beg (region-beginning)
  5052. end (region-end)
  5053. string (buffer-substring beg end))
  5054. (setq move t))
  5055. (unless char
  5056. (message "Emphasis marker or tag: [%s]"
  5057. (mapconcat #'car org-emphasis-alist ""))
  5058. (setq char (read-char-exclusive)))
  5059. (if (equal char ?\s)
  5060. (setq s ""
  5061. move nil)
  5062. (unless (assoc (char-to-string char) org-emphasis-alist)
  5063. (user-error "No such emphasis marker: \"%c\"" char))
  5064. (setq s (char-to-string char)))
  5065. (while (and (> (length string) 1)
  5066. (equal (substring string 0 1) (substring string -1))
  5067. (assoc (substring string 0 1) org-emphasis-alist))
  5068. (setq string (substring string 1 -1)))
  5069. (setq string (concat s string s))
  5070. (when beg (delete-region beg end))
  5071. (unless (or (bolp)
  5072. (string-match (concat "[" (nth 0 erc) "\n]")
  5073. (char-to-string (char-before (point)))))
  5074. (insert " "))
  5075. (unless (or (eobp)
  5076. (string-match (concat "[" (nth 1 erc) "\n]")
  5077. (char-to-string (char-after (point)))))
  5078. (insert " ") (backward-char 1))
  5079. (insert string)
  5080. (and move (backward-char 1))))
  5081. (defconst org-nonsticky-props
  5082. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text htmlize-link))
  5083. (defsubst org-rear-nonsticky-at (pos)
  5084. (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
  5085. (defun org-activate-plain-links (limit)
  5086. "Add link properties for plain links."
  5087. (when (and (re-search-forward org-plain-link-re limit t)
  5088. (not (org-in-src-block-p)))
  5089. (let ((face (get-text-property (max (1- (match-beginning 0)) (point-min))
  5090. 'face))
  5091. (link (org-match-string-no-properties 0)))
  5092. (unless (if (consp face) (memq 'org-tag face) (eq 'org-tag face))
  5093. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5094. (add-text-properties (match-beginning 0) (match-end 0)
  5095. (list 'mouse-face 'highlight
  5096. 'face 'org-link
  5097. 'htmlize-link `(:uri ,link)
  5098. 'keymap org-mouse-map))
  5099. (org-rear-nonsticky-at (match-end 0))
  5100. t))))
  5101. (defun org-activate-code (limit)
  5102. (when (re-search-forward "^[ \t]*\\(:\\(?: .*\\|$\\)\n?\\)" limit t)
  5103. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5104. (remove-text-properties (match-beginning 0) (match-end 0)
  5105. '(display t invisible t intangible t))
  5106. t))
  5107. (defcustom org-src-fontify-natively t
  5108. "When non-nil, fontify code in code blocks."
  5109. :type 'boolean
  5110. :version "24.4"
  5111. :package-version '(Org . "8.3")
  5112. :group 'org-appearance
  5113. :group 'org-babel)
  5114. (defcustom org-allow-promoting-top-level-subtree nil
  5115. "When non-nil, allow promoting a top level subtree.
  5116. The leading star of the top level headline will be replaced
  5117. by a #."
  5118. :type 'boolean
  5119. :version "24.1"
  5120. :group 'org-appearance)
  5121. (defun org-fontify-meta-lines-and-blocks (limit)
  5122. (condition-case nil
  5123. (org-fontify-meta-lines-and-blocks-1 limit)
  5124. (error (message "org-mode fontification error"))))
  5125. (defun org-fontify-meta-lines-and-blocks-1 (limit)
  5126. "Fontify #+ lines and blocks."
  5127. (let ((case-fold-search t))
  5128. (when (re-search-forward
  5129. "^\\([ \t]*#\\(\\(\\+[a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)[ \t]*\\(\\([^ \t\n]*\\)[ \t]*\\(.*\\)\\)\\)"
  5130. limit t)
  5131. (let ((beg (match-beginning 0))
  5132. (block-start (match-end 0))
  5133. (block-end nil)
  5134. (lang (match-string 7))
  5135. (beg1 (line-beginning-position 2))
  5136. (dc1 (downcase (match-string 2)))
  5137. (dc3 (downcase (match-string 3)))
  5138. end end1 quoting block-type)
  5139. (cond
  5140. ((and (match-end 4) (equal dc3 "+begin"))
  5141. ;; Truly a block
  5142. (setq block-type (downcase (match-string 5))
  5143. quoting (member block-type org-protecting-blocks))
  5144. (when (re-search-forward
  5145. (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
  5146. nil t) ;; on purpose, we look further than LIMIT
  5147. (setq end (min (point-max) (match-end 0))
  5148. end1 (min (point-max) (1- (match-beginning 0))))
  5149. (setq block-end (match-beginning 0))
  5150. (when quoting
  5151. (org-remove-flyspell-overlays-in beg1 end1)
  5152. (remove-text-properties beg end
  5153. '(display t invisible t intangible t)))
  5154. (add-text-properties
  5155. beg end '(font-lock-fontified t font-lock-multiline t))
  5156. (add-text-properties beg beg1 '(face org-meta-line))
  5157. (org-remove-flyspell-overlays-in beg beg1)
  5158. (add-text-properties ; For end_src
  5159. end1 (min (point-max) (1+ end)) '(face org-meta-line))
  5160. (org-remove-flyspell-overlays-in end1 end)
  5161. (cond
  5162. ((and lang (not (string= lang "")) org-src-fontify-natively)
  5163. (org-src-font-lock-fontify-block lang block-start block-end)
  5164. (add-text-properties beg1 block-end '(src-block t)))
  5165. (quoting
  5166. (add-text-properties beg1 (min (point-max) (1+ end1))
  5167. '(face org-block))) ; end of source block
  5168. ((not org-fontify-quote-and-verse-blocks))
  5169. ((string= block-type "quote")
  5170. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-quote)))
  5171. ((string= block-type "verse")
  5172. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-verse))))
  5173. (add-text-properties beg beg1 '(face org-block-begin-line))
  5174. (add-text-properties (min (point-max) (1+ end)) (min (point-max) (1+ end1))
  5175. '(face org-block-end-line))
  5176. t))
  5177. ((member dc1 '("+title:" "+author:" "+email:" "+date:"))
  5178. (org-remove-flyspell-overlays-in
  5179. (match-beginning 0)
  5180. (if (equal "+title:" dc1) (match-end 2) (match-end 0)))
  5181. (add-text-properties
  5182. beg (match-end 3)
  5183. (if (member (intern (substring dc1 1 -1)) org-hidden-keywords)
  5184. '(font-lock-fontified t invisible t)
  5185. '(font-lock-fontified t face org-document-info-keyword)))
  5186. (add-text-properties
  5187. (match-beginning 6) (min (point-max) (1+ (match-end 6)))
  5188. (if (string-equal dc1 "+title:")
  5189. '(font-lock-fontified t face org-document-title)
  5190. '(font-lock-fontified t face org-document-info))))
  5191. ((equal dc1 "+caption:")
  5192. (org-remove-flyspell-overlays-in (match-end 2) (match-end 0))
  5193. (remove-text-properties (match-beginning 0) (match-end 0)
  5194. '(display t invisible t intangible t))
  5195. (add-text-properties (match-beginning 1) (match-end 3)
  5196. '(font-lock-fontified t face org-meta-line))
  5197. (add-text-properties (match-beginning 6) (+ (match-end 6) 1)
  5198. '(font-lock-fontified t face org-block))
  5199. t)
  5200. ((member dc3 '(" " ""))
  5201. (org-remove-flyspell-overlays-in beg (match-end 0))
  5202. (add-text-properties
  5203. beg (match-end 0)
  5204. '(font-lock-fontified t face font-lock-comment-face)))
  5205. (t ;; just any other in-buffer setting, but not indented
  5206. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5207. (remove-text-properties (match-beginning 0) (match-end 0)
  5208. '(display t invisible t intangible t))
  5209. (add-text-properties beg (match-end 0)
  5210. '(font-lock-fontified t face org-meta-line))
  5211. t))))))
  5212. (defun org-fontify-drawers (limit)
  5213. "Fontify drawers."
  5214. (when (re-search-forward org-drawer-regexp limit t)
  5215. (add-text-properties
  5216. (match-beginning 0) (match-end 0)
  5217. '(font-lock-fontified t face org-special-keyword))
  5218. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5219. t))
  5220. (defun org-fontify-macros (limit)
  5221. "Fontify macros."
  5222. (when (re-search-forward "\\({{{\\).+?\\(}}}\\)" limit t)
  5223. (add-text-properties
  5224. (match-beginning 0) (match-end 0)
  5225. '(font-lock-fontified t face org-macro))
  5226. (when org-hide-macro-markers
  5227. (add-text-properties (match-end 2) (match-beginning 2)
  5228. '(invisible t))
  5229. (add-text-properties (match-beginning 1) (match-end 1)
  5230. '(invisible t)))
  5231. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5232. t))
  5233. (defun org-activate-angle-links (limit)
  5234. "Add text properties for angle links."
  5235. (when (and (re-search-forward org-angle-link-re limit t)
  5236. (not (org-in-src-block-p)))
  5237. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5238. (add-text-properties (match-beginning 0) (match-end 0)
  5239. (list 'mouse-face 'highlight
  5240. 'keymap org-mouse-map
  5241. 'font-lock-multiline t))
  5242. (org-rear-nonsticky-at (match-end 0))
  5243. t))
  5244. (defun org-activate-footnote-links (limit)
  5245. "Add text properties for footnotes."
  5246. (let ((fn (org-footnote-next-reference-or-definition limit)))
  5247. (when fn
  5248. (let* ((beg (nth 1 fn))
  5249. (end (nth 2 fn))
  5250. (label (car fn))
  5251. (referencep (/= (line-beginning-position) beg)))
  5252. (when (and referencep (nth 3 fn))
  5253. (save-excursion
  5254. (goto-char beg)
  5255. (search-forward (or label "fn:"))
  5256. (org-remove-flyspell-overlays-in beg (match-end 0))))
  5257. (add-text-properties beg end
  5258. (list 'mouse-face 'highlight
  5259. 'keymap org-mouse-map
  5260. 'help-echo
  5261. (if referencep "Footnote reference"
  5262. "Footnote definition")
  5263. 'font-lock-fontified t
  5264. 'font-lock-multiline t
  5265. 'face 'org-footnote))))))
  5266. (defun org-activate-bracket-links (limit)
  5267. "Add text properties for bracketed links."
  5268. (when (and (re-search-forward org-bracket-link-regexp limit t)
  5269. (not (org-in-src-block-p)))
  5270. (let* ((hl (org-match-string-no-properties 1))
  5271. (help (concat "LINK: " (save-match-data (org-link-unescape hl))))
  5272. (ip (org-maybe-intangible
  5273. (list 'invisible 'org-link
  5274. 'keymap org-mouse-map 'mouse-face 'highlight
  5275. 'font-lock-multiline t 'help-echo help
  5276. 'htmlize-link `(:uri ,hl))))
  5277. (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
  5278. 'font-lock-multiline t 'help-echo help
  5279. 'htmlize-link `(:uri ,hl))))
  5280. ;; We need to remove the invisible property here. Table narrowing
  5281. ;; may have made some of this invisible.
  5282. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5283. (remove-text-properties (match-beginning 0) (match-end 0)
  5284. '(invisible nil))
  5285. (if (match-end 3)
  5286. (progn
  5287. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  5288. (org-rear-nonsticky-at (match-beginning 3))
  5289. (add-text-properties (match-beginning 3) (match-end 3) vp)
  5290. (org-rear-nonsticky-at (match-end 3))
  5291. (add-text-properties (match-end 3) (match-end 0) ip)
  5292. (org-rear-nonsticky-at (match-end 0)))
  5293. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  5294. (org-rear-nonsticky-at (match-beginning 1))
  5295. (add-text-properties (match-beginning 1) (match-end 1) vp)
  5296. (org-rear-nonsticky-at (match-end 1))
  5297. (add-text-properties (match-end 1) (match-end 0) ip)
  5298. (org-rear-nonsticky-at (match-end 0)))
  5299. t)))
  5300. (defun org-activate-dates (limit)
  5301. "Add text properties for dates."
  5302. (when (and (re-search-forward org-tsr-regexp-both limit t)
  5303. (not (equal (char-before (match-beginning 0)) 91)))
  5304. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5305. (add-text-properties (match-beginning 0) (match-end 0)
  5306. (list 'mouse-face 'highlight
  5307. 'keymap org-mouse-map))
  5308. (org-rear-nonsticky-at (match-end 0))
  5309. (when org-display-custom-times
  5310. (if (match-end 3)
  5311. (org-display-custom-time (match-beginning 3) (match-end 3))
  5312. (org-display-custom-time (match-beginning 1) (match-end 1))))
  5313. t))
  5314. (defvar-local org-target-link-regexp nil
  5315. "Regular expression matching radio targets in plain text.")
  5316. (defconst org-target-regexp (let ((border "[^<>\n\r \t]"))
  5317. (format "<<\\(%s\\|%s[^<>\n\r]*%s\\)>>"
  5318. border border border))
  5319. "Regular expression matching a link target.")
  5320. (defconst org-radio-target-regexp (format "<%s>" org-target-regexp)
  5321. "Regular expression matching a radio target.")
  5322. (defconst org-any-target-regexp
  5323. (format "%s\\|%s" org-radio-target-regexp org-target-regexp)
  5324. "Regular expression matching any target.")
  5325. (defun org-activate-target-links (limit)
  5326. "Add text properties for target matches."
  5327. (when org-target-link-regexp
  5328. (let ((case-fold-search t))
  5329. (when (re-search-forward org-target-link-regexp limit t)
  5330. (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
  5331. (add-text-properties (match-beginning 1) (match-end 1)
  5332. (list 'mouse-face 'highlight
  5333. 'keymap org-mouse-map
  5334. 'help-echo "Radio target link"
  5335. 'org-linked-text t))
  5336. (org-rear-nonsticky-at (match-end 1))
  5337. t))))
  5338. (defun org-update-radio-target-regexp ()
  5339. "Find all radio targets in this file and update the regular expression.
  5340. Also refresh fontification if needed."
  5341. (interactive)
  5342. (let ((old-regexp org-target-link-regexp)
  5343. (before-re "\\(?:^\\|[^[:alnum:]]\\)\\(")
  5344. (after-re "\\)\\(?:$\\|[^[:alnum:]]\\)")
  5345. (targets
  5346. (org-with-wide-buffer
  5347. (goto-char (point-min))
  5348. (let (rtn)
  5349. (while (re-search-forward org-radio-target-regexp nil t)
  5350. ;; Make sure point is really within the object.
  5351. (backward-char)
  5352. (let ((obj (org-element-context)))
  5353. (when (eq (org-element-type obj) 'radio-target)
  5354. (cl-pushnew (org-element-property :value obj) rtn
  5355. :test #'equal))))
  5356. rtn))))
  5357. (setq org-target-link-regexp
  5358. (and targets
  5359. (concat before-re
  5360. (mapconcat
  5361. (lambda (x)
  5362. (replace-regexp-in-string
  5363. " +" "\\s-+" (regexp-quote x) t t))
  5364. targets
  5365. "\\|")
  5366. after-re)))
  5367. (unless (equal old-regexp org-target-link-regexp)
  5368. ;; Clean-up cache.
  5369. (let ((regexp (cond ((not old-regexp) org-target-link-regexp)
  5370. ((not org-target-link-regexp) old-regexp)
  5371. (t
  5372. (concat before-re
  5373. (mapconcat
  5374. (lambda (re)
  5375. (substring re (length before-re)
  5376. (- (length after-re))))
  5377. (list old-regexp org-target-link-regexp)
  5378. "\\|")
  5379. after-re)))))
  5380. (org-with-wide-buffer
  5381. (goto-char (point-min))
  5382. (while (re-search-forward regexp nil t)
  5383. (org-element-cache-refresh (match-beginning 1)))))
  5384. ;; Re fontify buffer.
  5385. (when (memq 'radio org-highlight-links)
  5386. (org-restart-font-lock)))))
  5387. (defun org-hide-wide-columns (limit)
  5388. (let (s e)
  5389. (setq s (text-property-any (point) (or limit (point-max))
  5390. 'org-cwidth t))
  5391. (when s
  5392. (setq e (next-single-property-change s 'org-cwidth))
  5393. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  5394. (goto-char e)
  5395. t)))
  5396. (defvar org-latex-and-related-regexp nil
  5397. "Regular expression for highlighting LaTeX, entities and sub/superscript.")
  5398. (defun org-compute-latex-and-related-regexp ()
  5399. "Compute regular expression for LaTeX, entities and sub/superscript.
  5400. Result depends on variable `org-highlight-latex-and-related'."
  5401. (setq-local
  5402. org-latex-and-related-regexp
  5403. (let* ((re-sub
  5404. (cond ((not (memq 'script org-highlight-latex-and-related)) nil)
  5405. ((eq org-use-sub-superscripts '{})
  5406. (list org-match-substring-with-braces-regexp))
  5407. (org-use-sub-superscripts (list org-match-substring-regexp))))
  5408. (re-latex
  5409. (when (memq 'latex org-highlight-latex-and-related)
  5410. (let ((matchers (plist-get org-format-latex-options :matchers)))
  5411. (delq nil
  5412. (mapcar (lambda (x)
  5413. (and (member (car x) matchers) (nth 1 x)))
  5414. org-latex-regexps)))))
  5415. (re-entities
  5416. (when (memq 'entities org-highlight-latex-and-related)
  5417. (list "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)"))))
  5418. (mapconcat 'identity (append re-latex re-entities re-sub) "\\|"))))
  5419. (defun org-do-latex-and-related (limit)
  5420. "Highlight LaTeX snippets and environments, entities and sub/superscript.
  5421. LIMIT bounds the search for syntax to highlight. Stop at first
  5422. highlighted object, if any. Return t if some highlighting was
  5423. done, nil otherwise."
  5424. (when (org-string-nw-p org-latex-and-related-regexp)
  5425. (catch 'found
  5426. (while (re-search-forward org-latex-and-related-regexp limit t)
  5427. (unless
  5428. (cl-some
  5429. (lambda (f)
  5430. (memq f '(org-code org-verbatim underline org-special-keyword)))
  5431. (save-excursion
  5432. (goto-char (1+ (match-beginning 0)))
  5433. (face-at-point nil t)))
  5434. (let ((offset (if (memq (char-after (1+ (match-beginning 0)))
  5435. '(?_ ?^))
  5436. 1
  5437. 0)))
  5438. (font-lock-prepend-text-property
  5439. (+ offset (match-beginning 0)) (match-end 0)
  5440. 'face 'org-latex-and-related)
  5441. (add-text-properties (+ offset (match-beginning 0)) (match-end 0)
  5442. '(font-lock-multiline t)))
  5443. (throw 'found t)))
  5444. nil)))
  5445. (defun org-restart-font-lock ()
  5446. "Restart `font-lock-mode', to force refontification."
  5447. (when (and (boundp 'font-lock-mode) font-lock-mode)
  5448. (font-lock-mode -1)
  5449. (font-lock-mode 1)))
  5450. (defun org-activate-tags (limit)
  5451. (when (re-search-forward
  5452. (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$") limit t)
  5453. (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
  5454. (add-text-properties (match-beginning 1) (match-end 1)
  5455. (list 'mouse-face 'highlight
  5456. 'keymap org-mouse-map))
  5457. (org-rear-nonsticky-at (match-end 1))
  5458. t))
  5459. (defun org-outline-level ()
  5460. "Compute the outline level of the heading at point.
  5461. If this is called at a normal headline, the level is the number
  5462. of stars. Use `org-reduced-level' to remove the effect of
  5463. `org-odd-levels'. Unlike to `org-current-level', this function
  5464. takes into consideration inlinetasks."
  5465. (org-with-wide-buffer
  5466. (end-of-line)
  5467. (if (re-search-backward org-outline-regexp-bol nil t)
  5468. (1- (- (match-end 0) (match-beginning 0)))
  5469. 0)))
  5470. (defvar org-font-lock-keywords nil)
  5471. (defsubst org-re-property (property &optional literal allow-null value)
  5472. "Return a regexp matching a PROPERTY line.
  5473. When optional argument LITERAL is non-nil, do not quote PROPERTY.
  5474. This is useful when PROPERTY is a regexp. When ALLOW-NULL is
  5475. non-nil, match properties even without a value.
  5476. Match group 3 is set to the value when it exists. If there is no
  5477. value and ALLOW-NULL is non-nil, it is set to the empty string.
  5478. With optional argument VALUE, match only property lines with
  5479. that value; in this case, ALLOW-NULL is ignored. VALUE is quoted
  5480. unless LITERAL is non-nil."
  5481. (concat
  5482. "^\\(?4:[ \t]*\\)"
  5483. (format "\\(?1::\\(?2:%s\\):\\)"
  5484. (if literal property (regexp-quote property)))
  5485. (cond (value
  5486. (format "[ \t]+\\(?3:%s\\)\\(?5:[ \t]*\\)$"
  5487. (if literal value (regexp-quote value))))
  5488. (allow-null
  5489. "\\(?:\\(?3:$\\)\\|[ \t]+\\(?3:.*?\\)\\)\\(?5:[ \t]*\\)$")
  5490. (t
  5491. "[ \t]+\\(?3:[^ \r\t\n]+.*?\\)\\(?5:[ \t]*\\)$"))))
  5492. (defconst org-property-re
  5493. (org-re-property "\\S-+" 'literal t)
  5494. "Regular expression matching a property line.
  5495. There are four matching groups:
  5496. 1: :PROPKEY: including the leading and trailing colon,
  5497. 2: PROPKEY without the leading and trailing colon,
  5498. 3: PROPVAL without leading or trailing spaces,
  5499. 4: the indentation of the current line,
  5500. 5: trailing whitespace.")
  5501. (defvar org-font-lock-hook nil
  5502. "Functions to be called for special font lock stuff.")
  5503. (defvar org-font-lock-extra-keywords nil) ;Dynamically scoped.
  5504. (defvar org-font-lock-set-keywords-hook nil
  5505. "Functions that can manipulate `org-font-lock-extra-keywords'.
  5506. This is called after `org-font-lock-extra-keywords' is defined, but before
  5507. it is installed to be used by font lock. This can be useful if something
  5508. needs to be inserted at a specific position in the font-lock sequence.")
  5509. (defun org-font-lock-hook (limit)
  5510. "Run `org-font-lock-hook' within LIMIT."
  5511. (run-hook-with-args 'org-font-lock-hook limit))
  5512. (defun org-set-font-lock-defaults ()
  5513. "Set font lock defaults for the current buffer."
  5514. (let* ((em org-fontify-emphasized-text)
  5515. (lk org-highlight-links)
  5516. (org-font-lock-extra-keywords
  5517. (list
  5518. ;; Call the hook
  5519. '(org-font-lock-hook)
  5520. ;; Headlines
  5521. `(,(if org-fontify-whole-heading-line
  5522. "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
  5523. "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
  5524. (1 (org-get-level-face 1))
  5525. (2 (org-get-level-face 2))
  5526. (3 (org-get-level-face 3)))
  5527. ;; Table lines
  5528. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  5529. (1 'org-table t))
  5530. ;; Table internals
  5531. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  5532. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  5533. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  5534. '("| *\\(<[lrc]?[0-9]*>\\)" (1 'org-formula t))
  5535. ;; Drawers
  5536. '(org-fontify-drawers)
  5537. ;; Properties
  5538. (list org-property-re
  5539. '(1 'org-special-keyword t)
  5540. '(3 'org-property-value t))
  5541. ;; Links
  5542. (when (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  5543. (when (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  5544. (when (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  5545. (when (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  5546. (when (memq 'radio lk) '(org-activate-target-links (1 'org-link t)))
  5547. (when (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  5548. (when (memq 'footnote lk) '(org-activate-footnote-links))
  5549. ;; Targets.
  5550. (list org-any-target-regexp '(0 'org-target t))
  5551. ;; Diary sexps.
  5552. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  5553. ;; Macro
  5554. '(org-fontify-macros)
  5555. '(org-hide-wide-columns (0 nil append))
  5556. ;; TODO keyword
  5557. (list (format org-heading-keyword-regexp-format
  5558. org-todo-regexp)
  5559. '(2 (org-get-todo-face 2) t))
  5560. ;; DONE
  5561. (if org-fontify-done-headline
  5562. (list (format org-heading-keyword-regexp-format
  5563. (concat
  5564. "\\(?:"
  5565. (mapconcat 'regexp-quote org-done-keywords "\\|")
  5566. "\\)"))
  5567. '(2 'org-headline-done t))
  5568. nil)
  5569. ;; Priorities
  5570. '(org-font-lock-add-priority-faces)
  5571. ;; Tags
  5572. '(org-font-lock-add-tag-faces)
  5573. ;; Tags groups
  5574. (when (and org-group-tags org-tag-groups-alist)
  5575. (list (concat org-outline-regexp-bol ".+\\(:"
  5576. (regexp-opt (mapcar 'car org-tag-groups-alist))
  5577. ":\\).*$")
  5578. '(1 'org-tag-group prepend)))
  5579. ;; Special keywords
  5580. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  5581. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  5582. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  5583. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  5584. ;; Emphasis
  5585. (when em
  5586. (if (featurep 'xemacs)
  5587. '(org-do-emphasis-faces (0 nil append))
  5588. '(org-do-emphasis-faces)))
  5589. ;; Checkboxes
  5590. '("^[ \t]*\\(?:[-+*]\\|[0-9]+[.)]\\)[ \t]+\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?\\(\\[[- X]\\]\\)"
  5591. 1 'org-checkbox prepend)
  5592. (when (cdr (assq 'checkbox org-list-automatic-rules))
  5593. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  5594. (0 (org-get-checkbox-statistics-face) t)))
  5595. ;; Description list items
  5596. '("^[ \t]*[-+*][ \t]+\\(.*?[ \t]+::\\)\\([ \t]+\\|$\\)"
  5597. 1 'org-list-dt prepend)
  5598. ;; ARCHIVEd headings
  5599. (list (concat
  5600. org-outline-regexp-bol
  5601. "\\(.*:" org-archive-tag ":.*\\)")
  5602. '(1 'org-archived prepend))
  5603. ;; Specials
  5604. '(org-do-latex-and-related)
  5605. '(org-fontify-entities)
  5606. '(org-raise-scripts)
  5607. ;; Code
  5608. '(org-activate-code (1 'org-code t))
  5609. ;; COMMENT
  5610. (list (format
  5611. "^\\*+\\(?: +%s\\)?\\(?: +\\[#[A-Z0-9]\\]\\)? +\\(?9:%s\\)\\(?: \\|$\\)"
  5612. org-todo-regexp
  5613. org-comment-string)
  5614. '(9 'org-special-keyword t))
  5615. ;; Blocks and meta lines
  5616. '(org-fontify-meta-lines-and-blocks))))
  5617. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  5618. (run-hooks 'org-font-lock-set-keywords-hook)
  5619. ;; Now set the full font-lock-keywords
  5620. (setq-local org-font-lock-keywords org-font-lock-extra-keywords)
  5621. (setq-local font-lock-defaults
  5622. '(org-font-lock-keywords t nil nil backward-paragraph))
  5623. (kill-local-variable 'font-lock-keywords) nil))
  5624. (defun org-toggle-pretty-entities ()
  5625. "Toggle the composition display of entities as UTF8 characters."
  5626. (interactive)
  5627. (setq-local org-pretty-entities (not org-pretty-entities))
  5628. (org-restart-font-lock)
  5629. (if org-pretty-entities
  5630. (message "Entities are now displayed as UTF8 characters")
  5631. (save-restriction
  5632. (widen)
  5633. (org-decompose-region (point-min) (point-max))
  5634. (message "Entities are now displayed as plain text"))))
  5635. (defvar-local org-custom-properties-overlays nil
  5636. "List of overlays used for custom properties.")
  5637. (defun org-toggle-custom-properties-visibility ()
  5638. "Display or hide properties in `org-custom-properties'."
  5639. (interactive)
  5640. (if org-custom-properties-overlays
  5641. (progn (mapc #'delete-overlay org-custom-properties-overlays)
  5642. (setq org-custom-properties-overlays nil))
  5643. (when org-custom-properties
  5644. (org-with-wide-buffer
  5645. (goto-char (point-min))
  5646. (let ((regexp (org-re-property (regexp-opt org-custom-properties) t t)))
  5647. (while (re-search-forward regexp nil t)
  5648. (let ((end (cdr (save-match-data (org-get-property-block)))))
  5649. (when (and end (< (point) end))
  5650. ;; Hide first custom property in current drawer.
  5651. (let ((o (make-overlay (match-beginning 0) (1+ (match-end 0)))))
  5652. (overlay-put o 'invisible t)
  5653. (overlay-put o 'org-custom-property t)
  5654. (push o org-custom-properties-overlays))
  5655. ;; Hide additional custom properties in the same drawer.
  5656. (while (re-search-forward regexp end t)
  5657. (let ((o (make-overlay (match-beginning 0) (1+ (match-end 0)))))
  5658. (overlay-put o 'invisible t)
  5659. (overlay-put o 'org-custom-property t)
  5660. (push o org-custom-properties-overlays)))))
  5661. ;; Each entry is limited to a single property drawer.
  5662. (outline-next-heading)))))))
  5663. (defun org-fontify-entities (limit)
  5664. "Find an entity to fontify."
  5665. (let (ee)
  5666. (when org-pretty-entities
  5667. (catch 'match
  5668. ;; "\_ "-family is left out on purpose. Only the first one,
  5669. ;; i.e., "\_ ", could be fontified anyway, and it would be
  5670. ;; confusing when adding a second white space character.
  5671. (while (re-search-forward
  5672. "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]\n]\\)"
  5673. limit t)
  5674. (when (and (not (org-at-comment-p))
  5675. (setq ee (org-entity-get (match-string 1)))
  5676. (= (length (nth 6 ee)) 1))
  5677. (let* ((end (if (equal (match-string 2) "{}")
  5678. (match-end 2)
  5679. (match-end 1))))
  5680. (add-text-properties
  5681. (match-beginning 0) end
  5682. (list 'font-lock-fontified t))
  5683. (compose-region (match-beginning 0) end
  5684. (nth 6 ee) nil)
  5685. (backward-char 1)
  5686. (throw 'match t))))
  5687. nil))))
  5688. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  5689. "Fontify string S like in Org-mode."
  5690. (with-temp-buffer
  5691. (insert s)
  5692. (let ((org-odd-levels-only odd-levels))
  5693. (org-mode)
  5694. (org-font-lock-ensure)
  5695. (buffer-string))))
  5696. (defvar org-m nil)
  5697. (defvar org-l nil)
  5698. (defvar org-f nil)
  5699. (defun org-get-level-face (n)
  5700. "Get the right face for match N in font-lock matching of headlines."
  5701. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  5702. (when org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  5703. (if org-cycle-level-faces
  5704. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  5705. (setq org-f (nth (1- (min org-l org-n-level-faces)) org-level-faces)))
  5706. (cond
  5707. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  5708. ((eq n 2) org-f)
  5709. (t (unless org-level-color-stars-only org-f))))
  5710. (defun org-face-from-face-or-color (context inherit face-or-color)
  5711. "Create a face list that inherits INHERIT, but sets the foreground color.
  5712. When FACE-OR-COLOR is not a string, just return it."
  5713. (if (stringp face-or-color)
  5714. (list :inherit inherit
  5715. (cdr (assoc context org-faces-easy-properties))
  5716. face-or-color)
  5717. face-or-color))
  5718. (defun org-get-todo-face (kwd)
  5719. "Get the right face for a TODO keyword KWD.
  5720. If KWD is a number, get the corresponding match group."
  5721. (when (numberp kwd) (setq kwd (match-string kwd)))
  5722. (or (org-face-from-face-or-color
  5723. 'todo 'org-todo (cdr (assoc kwd org-todo-keyword-faces)))
  5724. (and (member kwd org-done-keywords) 'org-done)
  5725. 'org-todo))
  5726. (defun org-get-priority-face (priority)
  5727. "Get the right face for PRIORITY.
  5728. PRIORITY is a character."
  5729. (or (org-face-from-face-or-color
  5730. 'priority 'org-priority (cdr (assq priority org-priority-faces)))
  5731. 'org-priority))
  5732. (defun org-get-tag-face (tag)
  5733. "Get the right face for TAG.
  5734. If TAG is a number, get the corresponding match group."
  5735. (let ((tag (if (wholenump tag) (match-string tag) tag)))
  5736. (or (org-face-from-face-or-color
  5737. 'tag 'org-tag (cdr (assoc tag org-tag-faces)))
  5738. 'org-tag)))
  5739. (defun org-font-lock-add-priority-faces (limit)
  5740. "Add the special priority faces."
  5741. (while (re-search-forward "^\\*+ .*?\\(\\[#\\(.\\)\\]\\)" limit t)
  5742. (add-text-properties
  5743. (match-beginning 1) (match-end 1)
  5744. (list 'face (org-get-priority-face (string-to-char (match-string 2)))
  5745. 'font-lock-fontified t))))
  5746. (defun org-font-lock-add-tag-faces (limit)
  5747. "Add the special tag faces."
  5748. (when (and org-tag-faces org-tags-special-faces-re)
  5749. (while (re-search-forward org-tags-special-faces-re limit t)
  5750. (add-text-properties (match-beginning 1) (match-end 1)
  5751. (list 'face (org-get-tag-face 1)
  5752. 'font-lock-fontified t))
  5753. (backward-char 1))))
  5754. (defun org-unfontify-region (beg end &optional _maybe_loudly)
  5755. "Remove fontification and activation overlays from links."
  5756. (font-lock-default-unfontify-region beg end)
  5757. (let* ((buffer-undo-list t)
  5758. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  5759. (inhibit-modification-hooks t)
  5760. deactivate-mark buffer-file-name buffer-file-truename)
  5761. (org-decompose-region beg end)
  5762. (remove-text-properties beg end
  5763. '(mouse-face t keymap t org-linked-text t
  5764. invisible t intangible t
  5765. org-emphasis t))
  5766. (org-remove-font-lock-display-properties beg end)))
  5767. (defconst org-script-display '(((raise -0.3) (height 0.7))
  5768. ((raise 0.3) (height 0.7))
  5769. ((raise -0.5))
  5770. ((raise 0.5)))
  5771. "Display properties for showing superscripts and subscripts.")
  5772. (defun org-remove-font-lock-display-properties (beg end)
  5773. "Remove specific display properties that have been added by font lock.
  5774. The will remove the raise properties that are used to show superscripts
  5775. and subscripts."
  5776. (let (next prop)
  5777. (while (< beg end)
  5778. (setq next (next-single-property-change beg 'display nil end)
  5779. prop (get-text-property beg 'display))
  5780. (when (member prop org-script-display)
  5781. (put-text-property beg next 'display nil))
  5782. (setq beg next))))
  5783. (defun org-raise-scripts (limit)
  5784. "Add raise properties to sub/superscripts."
  5785. (when (and org-pretty-entities org-pretty-entities-include-sub-superscripts
  5786. (re-search-forward
  5787. (if (eq org-use-sub-superscripts t)
  5788. org-match-substring-regexp
  5789. org-match-substring-with-braces-regexp)
  5790. limit t))
  5791. (let* ((pos (point)) table-p comment-p
  5792. (mpos (match-beginning 3))
  5793. (emph-p (get-text-property mpos 'org-emphasis))
  5794. (link-p (get-text-property mpos 'mouse-face))
  5795. (keyw-p (eq 'org-special-keyword (get-text-property mpos 'face))))
  5796. (goto-char (point-at-bol))
  5797. (setq table-p (org-looking-at-p org-table-dataline-regexp)
  5798. comment-p (org-looking-at-p "^[ \t]*#[ +]"))
  5799. (goto-char pos)
  5800. ;; Handle a_b^c
  5801. (when (member (char-after) '(?_ ?^)) (goto-char (1- pos)))
  5802. (unless (or comment-p emph-p link-p keyw-p)
  5803. (put-text-property (match-beginning 3) (match-end 0)
  5804. 'display
  5805. (if (equal (char-after (match-beginning 2)) ?^)
  5806. (nth (if table-p 3 1) org-script-display)
  5807. (nth (if table-p 2 0) org-script-display)))
  5808. (add-text-properties (match-beginning 2) (match-end 2)
  5809. (list 'invisible t
  5810. 'org-dwidth t 'org-dwidth-n 1))
  5811. (if (and (eq (char-after (match-beginning 3)) ?{)
  5812. (eq (char-before (match-end 3)) ?}))
  5813. (progn
  5814. (add-text-properties
  5815. (match-beginning 3) (1+ (match-beginning 3))
  5816. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))
  5817. (add-text-properties
  5818. (1- (match-end 3)) (match-end 3)
  5819. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1)))))
  5820. t)))
  5821. ;;;; Visibility cycling, including org-goto and indirect buffer
  5822. ;;; Cycling
  5823. (defvar-local org-cycle-global-status nil)
  5824. (put 'org-cycle-global-status 'org-state t)
  5825. (defvar-local org-cycle-subtree-status nil)
  5826. (put 'org-cycle-subtree-status 'org-state t)
  5827. (defvar org-inlinetask-min-level)
  5828. (defun org-unlogged-message (&rest args)
  5829. "Display a message, but avoid logging it in the *Messages* buffer."
  5830. (let ((message-log-max nil))
  5831. (apply 'message args)))
  5832. ;;;###autoload
  5833. (defun org-cycle (&optional arg)
  5834. "TAB-action and visibility cycling for Org-mode.
  5835. This is the command invoked in Org-mode by the TAB key. Its main purpose
  5836. is outline visibility cycling, but it also invokes other actions
  5837. in special contexts.
  5838. - When this function is called with a prefix argument, rotate the entire
  5839. buffer through 3 states (global cycling)
  5840. 1. OVERVIEW: Show only top-level headlines.
  5841. 2. CONTENTS: Show all headlines of all levels, but no body text.
  5842. 3. SHOW ALL: Show everything.
  5843. With a double \\[universal-argument] prefix argument, \
  5844. switch to the startup visibility,
  5845. determined by the variable `org-startup-folded', and by any VISIBILITY
  5846. properties in the buffer.
  5847. With a triple \\[universal-argument] prefix argument, \
  5848. show the entire buffer, including any drawers.
  5849. - When inside a table, re-align the table and move to the next field.
  5850. - When point is at the beginning of a headline, rotate the subtree started
  5851. by this line through 3 different states (local cycling)
  5852. 1. FOLDED: Only the main headline is shown.
  5853. 2. CHILDREN: The main headline and the direct children are shown.
  5854. From this state, you can move to one of the children
  5855. and zoom in further.
  5856. 3. SUBTREE: Show the entire subtree, including body text.
  5857. If there is no subtree, switch directly from CHILDREN to FOLDED.
  5858. - When point is at the beginning of an empty headline and the variable
  5859. `org-cycle-level-after-item/entry-creation' is set, cycle the level
  5860. of the headline by demoting and promoting it to likely levels. This
  5861. speeds up creation document structure by pressing TAB once or several
  5862. times right after creating a new headline.
  5863. - When there is a numeric prefix, go up to a heading with level ARG, do
  5864. a `show-subtree' and return to the previous cursor position. If ARG
  5865. is negative, go up that many levels.
  5866. - When point is not at the beginning of a headline, execute the global
  5867. binding for TAB, which is re-indenting the line. See the option
  5868. `org-cycle-emulate-tab' for details.
  5869. - Special case: if point is at the beginning of the buffer and there is
  5870. no headline in line 1, this function will act as if called with prefix arg
  5871. (\\[universal-argument] TAB, same as S-TAB) also when called without prefix arg.
  5872. But only if also the variable `org-cycle-global-at-bob' is t."
  5873. (interactive "P")
  5874. (org-load-modules-maybe)
  5875. (unless (or (run-hook-with-args-until-success 'org-tab-first-hook)
  5876. (and org-cycle-level-after-item/entry-creation
  5877. (or (org-cycle-level)
  5878. (org-cycle-item-indentation))))
  5879. (let* ((limit-level
  5880. (or org-cycle-max-level
  5881. (and (boundp 'org-inlinetask-min-level)
  5882. org-inlinetask-min-level
  5883. (1- org-inlinetask-min-level))))
  5884. (nstars (and limit-level
  5885. (if org-odd-levels-only
  5886. (and limit-level (1- (* limit-level 2)))
  5887. limit-level)))
  5888. (org-outline-regexp
  5889. (if (not (derived-mode-p 'org-mode))
  5890. outline-regexp
  5891. (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ "))))
  5892. (bob-special (and org-cycle-global-at-bob (not arg) (bobp)
  5893. (not (looking-at org-outline-regexp))))
  5894. (org-cycle-hook
  5895. (if bob-special
  5896. (delq 'org-optimize-window-after-visibility-change
  5897. (copy-sequence org-cycle-hook))
  5898. org-cycle-hook))
  5899. (pos (point)))
  5900. (when (or bob-special (equal arg '(4)))
  5901. ;; special case: use global cycling
  5902. (setq arg t))
  5903. (cond
  5904. ((equal arg '(16))
  5905. (setq last-command 'dummy)
  5906. (org-set-startup-visibility)
  5907. (org-unlogged-message "Startup visibility, plus VISIBILITY properties"))
  5908. ((equal arg '(64))
  5909. (outline-show-all)
  5910. (org-unlogged-message "Entire buffer visible, including drawers"))
  5911. ;; Try cdlatex TAB completion
  5912. ((org-try-cdlatex-tab))
  5913. ;; Table: enter it or move to the next field.
  5914. ((org-at-table-p 'any)
  5915. (if (org-at-table.el-p)
  5916. (message "%s" (substitute-command-keys "\\<org-mode-map>\
  5917. Use \\[org-edit-special] to edit table.el tables"))
  5918. (if arg (org-table-edit-field t)
  5919. (org-table-justify-field-maybe)
  5920. (call-interactively 'org-table-next-field))))
  5921. ((run-hook-with-args-until-success
  5922. 'org-tab-after-check-for-table-hook))
  5923. ;; Global cycling: delegate to `org-cycle-internal-global'.
  5924. ((eq arg t) (org-cycle-internal-global))
  5925. ;; Drawers: delegate to `org-flag-drawer'.
  5926. ((save-excursion
  5927. (beginning-of-line 1)
  5928. (looking-at org-drawer-regexp))
  5929. (org-flag-drawer ; toggle block visibility
  5930. (not (get-char-property (match-end 0) 'invisible))))
  5931. ;; Show-subtree, ARG levels up from here.
  5932. ((integerp arg)
  5933. (save-excursion
  5934. (org-back-to-heading)
  5935. (outline-up-heading (if (< arg 0) (- arg)
  5936. (- (funcall outline-level) arg)))
  5937. (org-show-subtree)))
  5938. ;; Inline task: delegate to `org-inlinetask-toggle-visibility'.
  5939. ((and (featurep 'org-inlinetask)
  5940. (org-inlinetask-at-task-p)
  5941. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5942. (org-inlinetask-toggle-visibility))
  5943. ;; At an item/headline: delegate to `org-cycle-internal-local'.
  5944. ((and (or (and org-cycle-include-plain-lists (org-at-item-p))
  5945. (save-excursion (move-beginning-of-line 1)
  5946. (looking-at org-outline-regexp)))
  5947. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5948. (org-cycle-internal-local))
  5949. ;; From there: TAB emulation and template completion.
  5950. (buffer-read-only (org-back-to-heading))
  5951. ((run-hook-with-args-until-success
  5952. 'org-tab-after-check-for-cycling-hook))
  5953. ((org-try-structure-completion))
  5954. ((run-hook-with-args-until-success
  5955. 'org-tab-before-tab-emulation-hook))
  5956. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  5957. (or (not (bolp))
  5958. (not (looking-at org-outline-regexp))))
  5959. (call-interactively (global-key-binding "\t")))
  5960. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  5961. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  5962. (or (and (eq org-cycle-emulate-tab 'white)
  5963. (= (match-end 0) (point-at-eol)))
  5964. (and (eq org-cycle-emulate-tab 'whitestart)
  5965. (>= (match-end 0) pos))))
  5966. t
  5967. (eq org-cycle-emulate-tab t))
  5968. (call-interactively (global-key-binding "\t")))
  5969. (t (save-excursion
  5970. (org-back-to-heading)
  5971. (org-cycle)))))))
  5972. (defun org-cycle-internal-global ()
  5973. "Do the global cycling action."
  5974. ;; Hack to avoid display of messages for .org attachments in Gnus
  5975. (let ((ga (string-match "\\*fontification" (buffer-name))))
  5976. (cond
  5977. ((and (eq last-command this-command)
  5978. (eq org-cycle-global-status 'overview))
  5979. ;; We just created the overview - now do table of contents
  5980. ;; This can be slow in very large buffers, so indicate action
  5981. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  5982. (unless ga (org-unlogged-message "CONTENTS..."))
  5983. (org-content)
  5984. (unless ga (org-unlogged-message "CONTENTS...done"))
  5985. (setq org-cycle-global-status 'contents)
  5986. (run-hook-with-args 'org-cycle-hook 'contents))
  5987. ((and (eq last-command this-command)
  5988. (eq org-cycle-global-status 'contents))
  5989. ;; We just showed the table of contents - now show everything
  5990. (run-hook-with-args 'org-pre-cycle-hook 'all)
  5991. (outline-show-all)
  5992. (unless ga (org-unlogged-message "SHOW ALL"))
  5993. (setq org-cycle-global-status 'all)
  5994. (run-hook-with-args 'org-cycle-hook 'all))
  5995. (t
  5996. ;; Default action: go to overview
  5997. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  5998. (org-overview)
  5999. (unless ga (org-unlogged-message "OVERVIEW"))
  6000. (setq org-cycle-global-status 'overview)
  6001. (run-hook-with-args 'org-cycle-hook 'overview)))))
  6002. (defvar org-called-with-limited-levels nil
  6003. "Non-nil when `org-with-limited-levels' is currently active.")
  6004. (defun org-cycle-internal-local ()
  6005. "Do the local cycling action."
  6006. (let ((goal-column 0) eoh eol eos has-children children-skipped struct)
  6007. ;; First, determine end of headline (EOH), end of subtree or item
  6008. ;; (EOS), and if item or heading has children (HAS-CHILDREN).
  6009. (save-excursion
  6010. (if (org-at-item-p)
  6011. (progn
  6012. (beginning-of-line)
  6013. (setq struct (org-list-struct))
  6014. (setq eoh (point-at-eol))
  6015. (setq eos (org-list-get-item-end-before-blank (point) struct))
  6016. (setq has-children (org-list-has-child-p (point) struct)))
  6017. (org-back-to-heading)
  6018. (setq eoh (save-excursion (outline-end-of-heading) (point)))
  6019. (setq eos (save-excursion (org-end-of-subtree t t)
  6020. (when (bolp) (backward-char)) (point)))
  6021. (setq has-children
  6022. (or (save-excursion
  6023. (let ((level (funcall outline-level)))
  6024. (outline-next-heading)
  6025. (and (org-at-heading-p t)
  6026. (> (funcall outline-level) level))))
  6027. (save-excursion
  6028. (org-list-search-forward (org-item-beginning-re) eos t)))))
  6029. ;; Determine end invisible part of buffer (EOL)
  6030. (beginning-of-line 2)
  6031. ;; XEmacs doesn't have `next-single-char-property-change'
  6032. (if (featurep 'xemacs)
  6033. (while (and (not (eobp)) ;; this is like `next-line'
  6034. (get-char-property (1- (point)) 'invisible))
  6035. (beginning-of-line 2))
  6036. (while (and (not (eobp)) ;; this is like `next-line'
  6037. (get-char-property (1- (point)) 'invisible))
  6038. (goto-char (next-single-char-property-change (point) 'invisible))
  6039. (and (eolp) (beginning-of-line 2))))
  6040. (setq eol (point)))
  6041. ;; Find out what to do next and set `this-command'
  6042. (cond
  6043. ((= eos eoh)
  6044. ;; Nothing is hidden behind this heading
  6045. (unless (org-before-first-heading-p)
  6046. (run-hook-with-args 'org-pre-cycle-hook 'empty))
  6047. (org-unlogged-message "EMPTY ENTRY")
  6048. (setq org-cycle-subtree-status nil)
  6049. (save-excursion
  6050. (goto-char eos)
  6051. (outline-next-heading)
  6052. (when (outline-invisible-p) (org-flag-heading nil))))
  6053. ((and (or (>= eol eos)
  6054. (not (string-match "\\S-" (buffer-substring eol eos))))
  6055. (or has-children
  6056. (not (setq children-skipped
  6057. org-cycle-skip-children-state-if-no-children))))
  6058. ;; Entire subtree is hidden in one line: children view
  6059. (unless (org-before-first-heading-p)
  6060. (run-hook-with-args 'org-pre-cycle-hook 'children))
  6061. (if (org-at-item-p)
  6062. (org-list-set-item-visibility (point-at-bol) struct 'children)
  6063. (org-show-entry)
  6064. (org-with-limited-levels (org-show-children))
  6065. ;; FIXME: This slows down the func way too much.
  6066. ;; How keep drawers hidden in subtree anyway?
  6067. ;; (when (memq 'org-cycle-hide-drawers org-cycle-hook)
  6068. ;; (org-cycle-hide-drawers 'subtree))
  6069. ;; Fold every list in subtree to top-level items.
  6070. (when (eq org-cycle-include-plain-lists 'integrate)
  6071. (save-excursion
  6072. (org-back-to-heading)
  6073. (while (org-list-search-forward (org-item-beginning-re) eos t)
  6074. (beginning-of-line 1)
  6075. (let* ((struct (org-list-struct))
  6076. (prevs (org-list-prevs-alist struct))
  6077. (end (org-list-get-bottom-point struct)))
  6078. (dolist (e (org-list-get-all-items (point) struct prevs))
  6079. (org-list-set-item-visibility e struct 'folded))
  6080. (goto-char (if (< end eos) end eos)))))))
  6081. (org-unlogged-message "CHILDREN")
  6082. (save-excursion
  6083. (goto-char eos)
  6084. (outline-next-heading)
  6085. (when (outline-invisible-p) (org-flag-heading nil)))
  6086. (setq org-cycle-subtree-status 'children)
  6087. (unless (org-before-first-heading-p)
  6088. (run-hook-with-args 'org-cycle-hook 'children)))
  6089. ((or children-skipped
  6090. (and (eq last-command this-command)
  6091. (eq org-cycle-subtree-status 'children)))
  6092. ;; We just showed the children, or no children are there,
  6093. ;; now show everything.
  6094. (unless (org-before-first-heading-p)
  6095. (run-hook-with-args 'org-pre-cycle-hook 'subtree))
  6096. (outline-flag-region eoh eos nil)
  6097. (org-unlogged-message
  6098. (if children-skipped "SUBTREE (NO CHILDREN)" "SUBTREE"))
  6099. (setq org-cycle-subtree-status 'subtree)
  6100. (unless (org-before-first-heading-p)
  6101. (run-hook-with-args 'org-cycle-hook 'subtree)))
  6102. (t
  6103. ;; Default action: hide the subtree.
  6104. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  6105. (outline-flag-region eoh eos t)
  6106. (org-unlogged-message "FOLDED")
  6107. (setq org-cycle-subtree-status 'folded)
  6108. (unless (org-before-first-heading-p)
  6109. (run-hook-with-args 'org-cycle-hook 'folded))))))
  6110. ;;;###autoload
  6111. (defun org-global-cycle (&optional arg)
  6112. "Cycle the global visibility. For details see `org-cycle'.
  6113. With \\[universal-argument] prefix arg, switch to startup visibility.
  6114. With a numeric prefix, show all headlines up to that level."
  6115. (interactive "P")
  6116. (let ((org-cycle-include-plain-lists
  6117. (if (derived-mode-p 'org-mode) org-cycle-include-plain-lists nil)))
  6118. (cond
  6119. ((integerp arg)
  6120. (outline-show-all)
  6121. (outline-hide-sublevels arg)
  6122. (setq org-cycle-global-status 'contents))
  6123. ((equal arg '(4))
  6124. (org-set-startup-visibility)
  6125. (org-unlogged-message "Startup visibility, plus VISIBILITY properties."))
  6126. (t
  6127. (org-cycle '(4))))))
  6128. (defun org-set-startup-visibility ()
  6129. "Set the visibility required by startup options and properties."
  6130. (cond
  6131. ((eq org-startup-folded t)
  6132. (org-overview))
  6133. ((eq org-startup-folded 'content)
  6134. (org-content))
  6135. ((or (eq org-startup-folded 'showeverything)
  6136. (eq org-startup-folded nil))
  6137. (outline-show-all)))
  6138. (unless (eq org-startup-folded 'showeverything)
  6139. (when org-hide-block-startup (org-hide-block-all))
  6140. (org-set-visibility-according-to-property 'no-cleanup)
  6141. (org-cycle-hide-archived-subtrees 'all)
  6142. (org-cycle-hide-drawers 'all)
  6143. (org-cycle-show-empty-lines t)))
  6144. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  6145. "Switch subtree visibilities according to :VISIBILITY: property."
  6146. (interactive)
  6147. (org-with-wide-buffer
  6148. (goto-char (point-min))
  6149. (while (re-search-forward "^[ \t]*:VISIBILITY:" nil t)
  6150. (if (not (org-at-property-p)) (outline-next-heading)
  6151. (let ((state (match-string 3)))
  6152. (save-excursion
  6153. (org-back-to-heading t)
  6154. (outline-hide-subtree)
  6155. (org-reveal)
  6156. (cond
  6157. ((equal state "folded")
  6158. (outline-hide-subtree))
  6159. ((equal state "children")
  6160. (org-show-hidden-entry)
  6161. (org-show-children))
  6162. ((equal state "content")
  6163. (save-excursion
  6164. (save-restriction
  6165. (org-narrow-to-subtree)
  6166. (org-content))))
  6167. ((member state '("all" "showall"))
  6168. (outline-show-subtree)))))))
  6169. (unless no-cleanup
  6170. (org-cycle-hide-archived-subtrees 'all)
  6171. (org-cycle-hide-drawers 'all)
  6172. (org-cycle-show-empty-lines 'all))))
  6173. ;; This function uses outline-regexp instead of the more fundamental
  6174. ;; org-outline-regexp so that org-cycle-global works outside of Org
  6175. ;; buffers, where outline-regexp is needed.
  6176. (defun org-overview ()
  6177. "Switch to overview mode, showing only top-level headlines.
  6178. This shows all headlines with a level equal or greater than the level
  6179. of the first headline in the buffer. This is important, because if the
  6180. first headline is not level one, then (hide-sublevels 1) gives confusing
  6181. results."
  6182. (interactive)
  6183. (save-excursion
  6184. (let ((level
  6185. (save-excursion
  6186. (goto-char (point-min))
  6187. (when (re-search-forward (concat "^" outline-regexp) nil t)
  6188. (goto-char (match-beginning 0))
  6189. (funcall outline-level)))))
  6190. (and level (outline-hide-sublevels level)))))
  6191. (defun org-content (&optional arg)
  6192. "Show all headlines in the buffer, like a table of contents.
  6193. With numerical argument N, show content up to level N."
  6194. (interactive "P")
  6195. (org-overview)
  6196. (save-excursion
  6197. ;; Visit all headings and show their offspring
  6198. (and (integerp arg) (org-overview))
  6199. (goto-char (point-max))
  6200. (catch 'exit
  6201. (while (and (progn (condition-case nil
  6202. (outline-previous-visible-heading 1)
  6203. (error (goto-char (point-min))))
  6204. t)
  6205. (looking-at org-outline-regexp))
  6206. (if (integerp arg)
  6207. (org-show-children (1- arg))
  6208. (outline-show-branches))
  6209. (when (bobp) (throw 'exit nil))))))
  6210. (defun org-optimize-window-after-visibility-change (state)
  6211. "Adjust the window after a change in outline visibility.
  6212. This function is the default value of the hook `org-cycle-hook'."
  6213. (when (get-buffer-window (current-buffer))
  6214. (cond
  6215. ((eq state 'content) nil)
  6216. ((eq state 'all) nil)
  6217. ((eq state 'folded) nil)
  6218. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  6219. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  6220. (defun org-remove-empty-overlays-at (pos)
  6221. "Remove outline overlays that do not contain non-white stuff."
  6222. (dolist (o (overlays-at pos))
  6223. (and (eq 'outline (overlay-get o 'invisible))
  6224. (not (string-match "\\S-" (buffer-substring (overlay-start o)
  6225. (overlay-end o))))
  6226. (delete-overlay o))))
  6227. (defun org-clean-visibility-after-subtree-move ()
  6228. "Fix visibility issues after moving a subtree."
  6229. ;; First, find a reasonable region to look at:
  6230. ;; Start two siblings above, end three below
  6231. (let* ((beg (save-excursion
  6232. (and (org-get-last-sibling)
  6233. (org-get-last-sibling))
  6234. (point)))
  6235. (end (save-excursion
  6236. (and (org-get-next-sibling)
  6237. (org-get-next-sibling)
  6238. (org-get-next-sibling))
  6239. (if (org-at-heading-p)
  6240. (point-at-eol)
  6241. (point))))
  6242. (level (looking-at "\\*+"))
  6243. (re (when level (concat "^" (regexp-quote (match-string 0)) " "))))
  6244. (save-excursion
  6245. (save-restriction
  6246. (narrow-to-region beg end)
  6247. (when re
  6248. ;; Properly fold already folded siblings
  6249. (goto-char (point-min))
  6250. (while (re-search-forward re nil t)
  6251. (when (and (not (outline-invisible-p))
  6252. (save-excursion
  6253. (goto-char (point-at-eol)) (outline-invisible-p)))
  6254. (outline-hide-entry))))
  6255. (org-cycle-show-empty-lines 'overview)
  6256. (org-cycle-hide-drawers 'overview)))))
  6257. (defun org-cycle-show-empty-lines (state)
  6258. "Show empty lines above all visible headlines.
  6259. The region to be covered depends on STATE when called through
  6260. `org-cycle-hook'. Lisp program can use t for STATE to get the
  6261. entire buffer covered. Note that an empty line is only shown if there
  6262. are at least `org-cycle-separator-lines' empty lines before the headline."
  6263. (when (/= org-cycle-separator-lines 0)
  6264. (save-excursion
  6265. (let* ((n (abs org-cycle-separator-lines))
  6266. (re (cond
  6267. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  6268. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  6269. (t (let ((ns (number-to-string (- n 2))))
  6270. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  6271. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  6272. beg end)
  6273. (cond
  6274. ((memq state '(overview contents t))
  6275. (setq beg (point-min) end (point-max)))
  6276. ((memq state '(children folded))
  6277. (setq beg (point)
  6278. end (progn (org-end-of-subtree t t)
  6279. (line-beginning-position 2)))))
  6280. (when beg
  6281. (goto-char beg)
  6282. (while (re-search-forward re end t)
  6283. (unless (get-char-property (match-end 1) 'invisible)
  6284. (let ((e (match-end 1))
  6285. (b (if (>= org-cycle-separator-lines 0)
  6286. (match-beginning 1)
  6287. (save-excursion
  6288. (goto-char (match-beginning 0))
  6289. (skip-chars-backward " \t\n")
  6290. (line-end-position)))))
  6291. (outline-flag-region b e nil))))))))
  6292. ;; Never hide empty lines at the end of the file.
  6293. (save-excursion
  6294. (goto-char (point-max))
  6295. (outline-previous-heading)
  6296. (outline-end-of-heading)
  6297. (when (and (looking-at "[ \t\n]+")
  6298. (= (match-end 0) (point-max)))
  6299. (outline-flag-region (point) (match-end 0) nil))))
  6300. (defun org-show-empty-lines-in-parent ()
  6301. "Move to the parent and re-show empty lines before visible headlines."
  6302. (save-excursion
  6303. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  6304. (org-cycle-show-empty-lines context))))
  6305. (defun org-files-list ()
  6306. "Return `org-agenda-files' list, plus all open org-mode files.
  6307. This is useful for operations that need to scan all of a user's
  6308. open and agenda-wise Org files."
  6309. (let ((files (mapcar 'expand-file-name (org-agenda-files))))
  6310. (dolist (buf (buffer-list))
  6311. (with-current-buffer buf
  6312. (when (and (derived-mode-p 'org-mode) (buffer-file-name))
  6313. (cl-pushnew (expand-file-name (buffer-file-name)) files))))
  6314. files))
  6315. (defsubst org-entry-beginning-position ()
  6316. "Return the beginning position of the current entry."
  6317. (save-excursion (org-back-to-heading t) (point)))
  6318. (defsubst org-entry-end-position ()
  6319. "Return the end position of the current entry."
  6320. (save-excursion (outline-next-heading) (point)))
  6321. (defun org-cycle-hide-drawers (state &optional exceptions)
  6322. "Re-hide all drawers after a visibility state change.
  6323. When non-nil, optional argument EXCEPTIONS is a list of strings
  6324. specifying which drawers should not be hidden."
  6325. (when (and (derived-mode-p 'org-mode)
  6326. (not (memq state '(overview folded contents))))
  6327. (save-excursion
  6328. (let* ((globalp (memq state '(contents all)))
  6329. (beg (if globalp (point-min) (point)))
  6330. (end (if globalp (point-max)
  6331. (if (eq state 'children)
  6332. (save-excursion (outline-next-heading) (point))
  6333. (org-end-of-subtree t)))))
  6334. (goto-char beg)
  6335. (while (re-search-forward org-drawer-regexp (max end (point)) t)
  6336. (unless (member-ignore-case (match-string 1) exceptions)
  6337. (let ((drawer (org-element-at-point)))
  6338. (when (memq (org-element-type drawer) '(drawer property-drawer))
  6339. (org-flag-drawer t drawer)
  6340. ;; Make sure to skip drawer entirely or we might flag
  6341. ;; it another time when matching its ending line with
  6342. ;; `org-drawer-regexp'.
  6343. (goto-char (org-element-property :end drawer))))))))))
  6344. (defun org-flag-drawer (flag &optional element)
  6345. "When FLAG is non-nil, hide the drawer we are at.
  6346. Otherwise make it visible. When optional argument ELEMENT is
  6347. a parsed drawer, as returned by `org-element-at-point', hide or
  6348. show that drawer instead."
  6349. (when (save-excursion
  6350. (beginning-of-line)
  6351. (org-looking-at-p org-drawer-regexp))
  6352. (let ((drawer (or element (org-element-at-point))))
  6353. (when (memq (org-element-type drawer) '(drawer property-drawer))
  6354. (let ((post (org-element-property :post-affiliated drawer)))
  6355. (save-excursion
  6356. (outline-flag-region
  6357. (progn (goto-char post) (line-end-position))
  6358. (progn (goto-char (org-element-property :end drawer))
  6359. (skip-chars-backward " \r\t\n")
  6360. (line-end-position))
  6361. flag))
  6362. ;; When the drawer is hidden away, make sure point lies in
  6363. ;; a visible part of the buffer.
  6364. (when (and flag (> (line-beginning-position) post))
  6365. (goto-char post)))))))
  6366. (defun org-subtree-end-visible-p ()
  6367. "Is the end of the current subtree visible?"
  6368. (pos-visible-in-window-p
  6369. (save-excursion (org-end-of-subtree t) (point))))
  6370. (defun org-first-headline-recenter ()
  6371. "Move cursor to the first headline and recenter the headline."
  6372. (goto-char (point-min))
  6373. (when (re-search-forward (concat "^\\(" org-outline-regexp "\\)") nil t)
  6374. (set-window-start (selected-window) (point-at-bol))))
  6375. ;;; Saving and restoring visibility
  6376. (defun org-outline-overlay-data (&optional use-markers)
  6377. "Return a list of the locations of all outline overlays.
  6378. These are overlays with the `invisible' property value `outline'.
  6379. The return value is a list of cons cells, with start and stop
  6380. positions for each overlay.
  6381. If USE-MARKERS is set, return the positions as markers."
  6382. (let (beg end)
  6383. (save-excursion
  6384. (save-restriction
  6385. (widen)
  6386. (delq nil
  6387. (mapcar (lambda (o)
  6388. (when (eq (overlay-get o 'invisible) 'outline)
  6389. (setq beg (overlay-start o)
  6390. end (overlay-end o))
  6391. (and beg end (> end beg)
  6392. (if use-markers
  6393. (cons (copy-marker beg)
  6394. (copy-marker end t))
  6395. (cons beg end)))))
  6396. (overlays-in (point-min) (point-max))))))))
  6397. (defun org-set-outline-overlay-data (data)
  6398. "Create visibility overlays for all positions in DATA.
  6399. DATA should have been made by `org-outline-overlay-data'."
  6400. (org-with-wide-buffer
  6401. (outline-show-all)
  6402. (dolist (c data) (outline-flag-region (car c) (cdr c) t))))
  6403. ;;; Folding of blocks
  6404. (defvar-local org-hide-block-overlays nil
  6405. "Overlays hiding blocks.")
  6406. (defun org-block-map (function &optional start end)
  6407. "Call FUNCTION at the head of all source blocks in the current buffer.
  6408. Optional arguments START and END can be used to limit the range."
  6409. (let ((start (or start (point-min)))
  6410. (end (or end (point-max))))
  6411. (save-excursion
  6412. (goto-char start)
  6413. (while (and (< (point) end) (re-search-forward org-block-regexp end t))
  6414. (save-excursion
  6415. (save-match-data
  6416. (goto-char (match-beginning 0))
  6417. (funcall function)))))))
  6418. (defun org-hide-block-toggle-all ()
  6419. "Toggle the visibility of all blocks in the current buffer."
  6420. (org-block-map 'org-hide-block-toggle))
  6421. (defun org-hide-block-all ()
  6422. "Fold all blocks in the current buffer."
  6423. (interactive)
  6424. (org-show-block-all)
  6425. (org-block-map 'org-hide-block-toggle-maybe))
  6426. (defun org-show-block-all ()
  6427. "Unfold all blocks in the current buffer."
  6428. (interactive)
  6429. (mapc #'delete-overlay org-hide-block-overlays)
  6430. (setq org-hide-block-overlays nil))
  6431. (defun org-hide-block-toggle-maybe ()
  6432. "Toggle visibility of block at point.
  6433. Unlike to `org-hide-block-toggle', this function does not throw
  6434. an error. Return a non-nil value when toggling is successful."
  6435. (interactive)
  6436. (ignore-errors (org-hide-block-toggle)))
  6437. (defun org-hide-block-toggle (&optional force)
  6438. "Toggle the visibility of the current block.
  6439. When optional argument FORCE is `off', make block visible. If it
  6440. is non-nil, hide it unconditionally. Throw an error when not at
  6441. a block. Return a non-nil value when toggling is successful."
  6442. (interactive)
  6443. (let ((element (org-element-at-point)))
  6444. (unless (memq (org-element-type element)
  6445. '(center-block comment-block dynamic-block example-block
  6446. export-block quote-block special-block
  6447. src-block verse-block))
  6448. (user-error "Not at a block"))
  6449. (let* ((start (save-excursion
  6450. (goto-char (org-element-property :post-affiliated element))
  6451. (line-end-position)))
  6452. (end (save-excursion
  6453. (goto-char (org-element-property :end element))
  6454. (skip-chars-backward " \r\t\n")
  6455. (line-end-position)))
  6456. (overlays (overlays-at start)))
  6457. (cond
  6458. ;; Do nothing when not before or at the block opening line or
  6459. ;; at the block closing line.
  6460. ((let ((eol (line-end-position))) (and (> eol start) (/= eol end))) nil)
  6461. ((and (not (eq force 'off))
  6462. (not (memq t (mapcar
  6463. (lambda (o)
  6464. (eq (overlay-get o 'invisible) 'org-hide-block))
  6465. overlays))))
  6466. (let ((ov (make-overlay start end)))
  6467. (overlay-put ov 'invisible 'org-hide-block)
  6468. ;; Make the block accessible to `isearch'.
  6469. (overlay-put
  6470. ov 'isearch-open-invisible
  6471. (lambda (ov)
  6472. (when (memq ov org-hide-block-overlays)
  6473. (setq org-hide-block-overlays (delq ov org-hide-block-overlays)))
  6474. (when (eq (overlay-get ov 'invisible) 'org-hide-block)
  6475. (delete-overlay ov))))
  6476. (push ov org-hide-block-overlays)
  6477. ;; When the block is hidden away, make sure point is left in
  6478. ;; a visible part of the buffer.
  6479. (when (> (line-beginning-position) start)
  6480. (goto-char start)
  6481. (beginning-of-line))
  6482. ;; Signal successful toggling.
  6483. t))
  6484. ((or (not force) (eq force 'off))
  6485. (dolist (ov overlays t)
  6486. (when (memq ov org-hide-block-overlays)
  6487. (setq org-hide-block-overlays (delq ov org-hide-block-overlays)))
  6488. (when (eq (overlay-get ov 'invisible) 'org-hide-block)
  6489. (delete-overlay ov))))))))
  6490. ;; org-tab-after-check-for-cycling-hook
  6491. (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
  6492. ;; Remove overlays when changing major mode
  6493. (add-hook 'org-mode-hook
  6494. (lambda () (org-add-hook 'change-major-mode-hook
  6495. 'org-show-block-all 'append 'local)))
  6496. ;;; Org-goto
  6497. (defvar org-goto-window-configuration nil)
  6498. (defvar org-goto-marker nil)
  6499. (defvar org-goto-map)
  6500. (defun org-goto-map ()
  6501. "Set the keymap `org-goto'."
  6502. (setq org-goto-map
  6503. (let ((map (make-sparse-keymap)))
  6504. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command
  6505. mouse-drag-region universal-argument org-occur)))
  6506. (dolist (cmd cmds)
  6507. (substitute-key-definition cmd cmd map global-map)))
  6508. (suppress-keymap map)
  6509. (org-defkey map "\C-m" 'org-goto-ret)
  6510. (org-defkey map [(return)] 'org-goto-ret)
  6511. (org-defkey map [(left)] 'org-goto-left)
  6512. (org-defkey map [(right)] 'org-goto-right)
  6513. (org-defkey map [(control ?g)] 'org-goto-quit)
  6514. (org-defkey map "\C-i" 'org-cycle)
  6515. (org-defkey map [(tab)] 'org-cycle)
  6516. (org-defkey map [(down)] 'outline-next-visible-heading)
  6517. (org-defkey map [(up)] 'outline-previous-visible-heading)
  6518. (if org-goto-auto-isearch
  6519. (if (fboundp 'define-key-after)
  6520. (define-key-after map [t] 'org-goto-local-auto-isearch)
  6521. nil)
  6522. (org-defkey map "q" 'org-goto-quit)
  6523. (org-defkey map "n" 'outline-next-visible-heading)
  6524. (org-defkey map "p" 'outline-previous-visible-heading)
  6525. (org-defkey map "f" 'outline-forward-same-level)
  6526. (org-defkey map "b" 'outline-backward-same-level)
  6527. (org-defkey map "u" 'outline-up-heading))
  6528. (org-defkey map "/" 'org-occur)
  6529. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  6530. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  6531. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  6532. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  6533. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  6534. map)))
  6535. (defconst org-goto-help
  6536. "Browse buffer copy, to find location or copy text.%s
  6537. RET=jump to location C-g=quit and return to previous location
  6538. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  6539. (defvar org-goto-start-pos) ; dynamically scoped parameter
  6540. (defun org-goto (&optional alternative-interface)
  6541. "Look up a different location in the current file, keeping current visibility.
  6542. When you want look-up or go to a different location in a
  6543. document, the fastest way is often to fold the entire buffer and
  6544. then dive into the tree. This method has the disadvantage, that
  6545. the previous location will be folded, which may not be what you
  6546. want.
  6547. This command works around this by showing a copy of the current
  6548. buffer in an indirect buffer, in overview mode. You can dive
  6549. into the tree in that copy, use org-occur and incremental search
  6550. to find a location. When pressing RET or `Q', the command
  6551. returns to the original buffer in which the visibility is still
  6552. unchanged. After RET it will also jump to the location selected
  6553. in the indirect buffer and expose the headline hierarchy above.
  6554. With a prefix argument, use the alternative interface: e.g., if
  6555. `org-goto-interface' is `outline' use `outline-path-completion'."
  6556. (interactive "P")
  6557. (org-goto-map)
  6558. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  6559. (org-refile-use-outline-path t)
  6560. (org-refile-target-verify-function nil)
  6561. (interface
  6562. (if (not alternative-interface)
  6563. org-goto-interface
  6564. (if (eq org-goto-interface 'outline)
  6565. 'outline-path-completion
  6566. 'outline)))
  6567. (org-goto-start-pos (point))
  6568. (selected-point
  6569. (if (eq interface 'outline)
  6570. (car (org-get-location (current-buffer) org-goto-help))
  6571. (let ((pa (org-refile-get-location "Goto")))
  6572. (org-refile-check-position pa)
  6573. (nth 3 pa)))))
  6574. (if selected-point
  6575. (progn
  6576. (org-mark-ring-push org-goto-start-pos)
  6577. (goto-char selected-point)
  6578. (when (or (outline-invisible-p) (org-invisible-p2))
  6579. (org-show-context 'org-goto)))
  6580. (message "Quit"))))
  6581. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  6582. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  6583. (defvar org-goto-local-auto-isearch-map) ; defined below
  6584. (defun org-get-location (_buf help)
  6585. "Let the user select a location in current buffer.
  6586. This function uses a recursive edit. It returns the selected position
  6587. or nil."
  6588. (org-no-popups
  6589. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  6590. (isearch-hide-immediately nil)
  6591. (isearch-search-fun-function
  6592. (lambda () 'org-goto-local-search-headings))
  6593. (org-goto-selected-point org-goto-exit-command))
  6594. (save-excursion
  6595. (save-window-excursion
  6596. (delete-other-windows)
  6597. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  6598. (org-pop-to-buffer-same-window
  6599. (condition-case nil
  6600. (make-indirect-buffer (current-buffer) "*org-goto*")
  6601. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  6602. (with-output-to-temp-buffer "*Org Help*"
  6603. (princ (format help (if org-goto-auto-isearch
  6604. " Just type for auto-isearch."
  6605. " n/p/f/b/u to navigate, q to quit."))))
  6606. (org-fit-window-to-buffer (get-buffer-window "*Org Help*"))
  6607. (setq buffer-read-only nil)
  6608. (let ((org-startup-truncated t)
  6609. (org-startup-folded nil)
  6610. (org-startup-align-all-tables nil))
  6611. (org-mode)
  6612. (org-overview))
  6613. (setq buffer-read-only t)
  6614. (if (and (boundp 'org-goto-start-pos)
  6615. (integer-or-marker-p org-goto-start-pos))
  6616. (progn (goto-char org-goto-start-pos)
  6617. (when (outline-invisible-p)
  6618. (org-show-set-visibility 'lineage)))
  6619. (goto-char (point-min)))
  6620. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  6621. (message "Select location and press RET")
  6622. (use-local-map org-goto-map)
  6623. (recursive-edit)))
  6624. (kill-buffer "*org-goto*")
  6625. (cons org-goto-selected-point org-goto-exit-command))))
  6626. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  6627. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  6628. ;; `isearch-other-control-char' was removed in Emacs 24.4.
  6629. (if (fboundp 'isearch-other-control-char)
  6630. (progn
  6631. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  6632. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char))
  6633. (define-key org-goto-local-auto-isearch-map "\C-i" nil)
  6634. (define-key org-goto-local-auto-isearch-map "\C-m" nil)
  6635. (define-key org-goto-local-auto-isearch-map [return] nil))
  6636. (defun org-goto-local-search-headings (string bound noerror)
  6637. "Search and make sure that any matches are in headlines."
  6638. (catch 'return
  6639. (while (if isearch-forward
  6640. (search-forward string bound noerror)
  6641. (search-backward string bound noerror))
  6642. (when (save-match-data
  6643. (and (save-excursion
  6644. (beginning-of-line)
  6645. (looking-at org-complex-heading-regexp))
  6646. (or (not (match-beginning 5))
  6647. (< (point) (match-beginning 5)))))
  6648. (throw 'return (point))))))
  6649. (defun org-goto-local-auto-isearch ()
  6650. "Start isearch."
  6651. (interactive)
  6652. (goto-char (point-min))
  6653. (let ((keys (this-command-keys)))
  6654. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  6655. (isearch-mode t)
  6656. (isearch-process-search-char (string-to-char keys)))))
  6657. (defun org-goto-ret (&optional _arg)
  6658. "Finish `org-goto' by going to the new location."
  6659. (interactive "P")
  6660. (setq org-goto-selected-point (point))
  6661. (setq org-goto-exit-command 'return)
  6662. (throw 'exit nil))
  6663. (defun org-goto-left ()
  6664. "Finish `org-goto' by going to the new location."
  6665. (interactive)
  6666. (if (org-at-heading-p)
  6667. (progn
  6668. (beginning-of-line 1)
  6669. (setq org-goto-selected-point (point)
  6670. org-goto-exit-command 'left)
  6671. (throw 'exit nil))
  6672. (user-error "Not on a heading")))
  6673. (defun org-goto-right ()
  6674. "Finish `org-goto' by going to the new location."
  6675. (interactive)
  6676. (if (org-at-heading-p)
  6677. (progn
  6678. (setq org-goto-selected-point (point)
  6679. org-goto-exit-command 'right)
  6680. (throw 'exit nil))
  6681. (user-error "Not on a heading")))
  6682. (defun org-goto-quit ()
  6683. "Finish `org-goto' without cursor motion."
  6684. (interactive)
  6685. (setq org-goto-selected-point nil)
  6686. (setq org-goto-exit-command 'quit)
  6687. (throw 'exit nil))
  6688. ;;; Indirect buffer display of subtrees
  6689. (defvar org-indirect-dedicated-frame nil
  6690. "This is the frame being used for indirect tree display.")
  6691. (defvar org-last-indirect-buffer nil)
  6692. (defun org-tree-to-indirect-buffer (&optional arg)
  6693. "Create indirect buffer and narrow it to current subtree.
  6694. With a numerical prefix ARG, go up to this level and then take that tree.
  6695. If ARG is negative, go up that many levels.
  6696. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  6697. indirect buffer previously made with this command, to avoid proliferation of
  6698. indirect buffers. However, when you call the command with a \
  6699. \\[universal-argument] prefix, or
  6700. when `org-indirect-buffer-display' is `new-frame', the last buffer
  6701. is kept so that you can work with several indirect buffers at the same time.
  6702. If `org-indirect-buffer-display' is `dedicated-frame', the \
  6703. \\[universal-argument] prefix also
  6704. requests that a new frame be made for the new buffer, so that the dedicated
  6705. frame is not changed."
  6706. (interactive "P")
  6707. (let ((cbuf (current-buffer))
  6708. (cwin (selected-window))
  6709. (pos (point))
  6710. beg end level heading ibuf)
  6711. (save-excursion
  6712. (org-back-to-heading t)
  6713. (when (numberp arg)
  6714. (setq level (org-outline-level))
  6715. (when (< arg 0) (setq arg (+ level arg)))
  6716. (while (> (setq level (org-outline-level)) arg)
  6717. (org-up-heading-safe)))
  6718. (setq beg (point)
  6719. heading (org-get-heading 'no-tags))
  6720. (org-end-of-subtree t t)
  6721. (when (org-at-heading-p) (backward-char 1))
  6722. (setq end (point)))
  6723. (when (and (buffer-live-p org-last-indirect-buffer)
  6724. (not (eq org-indirect-buffer-display 'new-frame))
  6725. (not arg))
  6726. (kill-buffer org-last-indirect-buffer))
  6727. (setq ibuf (org-get-indirect-buffer cbuf heading)
  6728. org-last-indirect-buffer ibuf)
  6729. (cond
  6730. ((or (eq org-indirect-buffer-display 'new-frame)
  6731. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  6732. (select-frame (make-frame))
  6733. (delete-other-windows)
  6734. (org-pop-to-buffer-same-window ibuf)
  6735. (org-set-frame-title heading))
  6736. ((eq org-indirect-buffer-display 'dedicated-frame)
  6737. (raise-frame
  6738. (select-frame (or (and org-indirect-dedicated-frame
  6739. (frame-live-p org-indirect-dedicated-frame)
  6740. org-indirect-dedicated-frame)
  6741. (setq org-indirect-dedicated-frame (make-frame)))))
  6742. (delete-other-windows)
  6743. (org-pop-to-buffer-same-window ibuf)
  6744. (org-set-frame-title (concat "Indirect: " heading)))
  6745. ((eq org-indirect-buffer-display 'current-window)
  6746. (org-pop-to-buffer-same-window ibuf))
  6747. ((eq org-indirect-buffer-display 'other-window)
  6748. (pop-to-buffer ibuf))
  6749. (t (error "Invalid value")))
  6750. (when (featurep 'xemacs)
  6751. (save-excursion (org-mode) (turn-on-font-lock)))
  6752. (narrow-to-region beg end)
  6753. (outline-show-all)
  6754. (goto-char pos)
  6755. (run-hook-with-args 'org-cycle-hook 'all)
  6756. (and (window-live-p cwin) (select-window cwin))))
  6757. (defun org-get-indirect-buffer (&optional buffer heading)
  6758. (setq buffer (or buffer (current-buffer)))
  6759. (let ((n 1) (base (buffer-name buffer)) bname)
  6760. (while (buffer-live-p
  6761. (get-buffer
  6762. (setq bname
  6763. (concat base "-"
  6764. (if heading (concat heading "-" (number-to-string n))
  6765. (number-to-string n))))))
  6766. (setq n (1+ n)))
  6767. (condition-case nil
  6768. (make-indirect-buffer buffer bname 'clone)
  6769. (error (make-indirect-buffer buffer bname)))))
  6770. (defun org-set-frame-title (title)
  6771. "Set the title of the current frame to the string TITLE."
  6772. ;; FIXME: how to name a single frame in XEmacs???
  6773. (unless (featurep 'xemacs)
  6774. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  6775. ;;;; Structure editing
  6776. ;;; Inserting headlines
  6777. (defun org--line-empty-p (n)
  6778. "Is the Nth next line empty?
  6779. Counts the current line as N = 1 and the previous line as N = 0;
  6780. see `beginning-of-line'."
  6781. (save-excursion
  6782. (and (not (bobp))
  6783. (or (beginning-of-line n) t)
  6784. (save-match-data
  6785. (looking-at "[ \t]*$")))))
  6786. (defun org-previous-line-empty-p ()
  6787. "Is the previous line a blank line?
  6788. When NEXT is non-nil, check the next line instead."
  6789. (org--line-empty-p 0))
  6790. (defun org-next-line-empty-p ()
  6791. "Is the previous line a blank line?
  6792. When NEXT is non-nil, check the next line instead."
  6793. (org--line-empty-p 2))
  6794. (defun org-insert-heading (&optional arg invisible-ok top)
  6795. "Insert a new heading or an item with the same depth at point.
  6796. If point is at the beginning of a heading or a list item, insert
  6797. a new heading or a new item above the current one. If point is
  6798. at the beginning of a normal line, turn the line into a heading.
  6799. If point is in the middle of a headline or a list item, split the
  6800. headline or the item and create a new headline/item with the text
  6801. in the current line after point \(see `org-M-RET-may-split-line'
  6802. on how to modify this behavior).
  6803. With one universal prefix argument, set the user option
  6804. `org-insert-heading-respect-content' to t for the duration of
  6805. the command. This modifies the behavior described above in this
  6806. ways: on list items and at the beginning of normal lines, force
  6807. the insertion of a heading after the current subtree.
  6808. With two universal prefix arguments, insert the heading at the
  6809. end of the grandparent subtree. For example, if point is within
  6810. a 2nd-level heading, then it will insert a 2nd-level heading at
  6811. the end of the 1st-level parent heading.
  6812. If point is at the beginning of a headline, insert a sibling
  6813. before the current headline. If point is not at the beginning,
  6814. split the line and create a new headline with the text in the
  6815. current line after point \(see `org-M-RET-may-split-line' on how
  6816. to modify this behavior).
  6817. If point is at the beginning of a normal line, turn this line
  6818. into a heading.
  6819. When INVISIBLE-OK is set, stop at invisible headlines when going
  6820. back. This is important for non-interactive uses of the
  6821. command.
  6822. When optional argument TOP is non-nil, insert a level 1 heading,
  6823. unconditionally."
  6824. (interactive "P")
  6825. (when (org-called-interactively-p 'any) (org-reveal))
  6826. (let ((itemp (and (not top) (org-in-item-p)))
  6827. (may-split (org-get-alist-option org-M-RET-may-split-line 'headline))
  6828. (respect-content (or org-insert-heading-respect-content
  6829. (equal arg '(4))))
  6830. (initial-content ""))
  6831. (cond
  6832. ((or (= (buffer-size) 0)
  6833. (and (not (save-excursion
  6834. (and (ignore-errors (org-back-to-heading invisible-ok))
  6835. (org-at-heading-p))))
  6836. (or arg (not itemp))))
  6837. ;; At beginning of buffer or so high up that only a heading
  6838. ;; makes sense.
  6839. (cond ((and (bolp) (not respect-content)) (insert "* "))
  6840. ((not respect-content)
  6841. (unless may-split (end-of-line))
  6842. (insert "\n* "))
  6843. ((re-search-forward org-outline-regexp-bol nil t)
  6844. (beginning-of-line)
  6845. (insert "* \n")
  6846. (backward-char))
  6847. (t (goto-char (point-max))
  6848. (insert "\n* ")))
  6849. (run-hooks 'org-insert-heading-hook))
  6850. ((and itemp (not (member arg '((4) (16)))) (org-insert-item)))
  6851. (t
  6852. ;; Maybe move at the end of the subtree
  6853. (when (equal arg '(16))
  6854. (org-up-heading-safe)
  6855. (org-end-of-subtree t))
  6856. ;; Insert a heading
  6857. (save-restriction
  6858. (widen)
  6859. (let* ((level nil)
  6860. (on-heading (org-at-heading-p))
  6861. (empty-line-p (if on-heading
  6862. (org-previous-line-empty-p)
  6863. ;; We will decide later
  6864. nil))
  6865. ;; Get a level string to fall back on.
  6866. (fix-level
  6867. (if (org-before-first-heading-p) "*"
  6868. (save-excursion
  6869. (org-back-to-heading t)
  6870. (when (org-previous-line-empty-p) (setq empty-line-p t))
  6871. (looking-at org-outline-regexp)
  6872. (make-string (1- (length (match-string 0))) ?*))))
  6873. (stars
  6874. (save-excursion
  6875. (condition-case nil
  6876. (if top "* "
  6877. (org-back-to-heading invisible-ok)
  6878. (when (and (not on-heading)
  6879. (featurep 'org-inlinetask)
  6880. (integerp org-inlinetask-min-level)
  6881. (>= (length (match-string 0))
  6882. org-inlinetask-min-level))
  6883. ;; Find a heading level before the inline
  6884. ;; task.
  6885. (while (and (setq level (org-up-heading-safe))
  6886. (>= level org-inlinetask-min-level)))
  6887. (if (org-at-heading-p)
  6888. (org-back-to-heading invisible-ok)
  6889. (error "This should not happen")))
  6890. (unless (and (save-excursion
  6891. (save-match-data
  6892. (org-backward-heading-same-level
  6893. 1 invisible-ok))
  6894. (= (point) (match-beginning 0)))
  6895. (not (org-next-line-empty-p)))
  6896. (setq empty-line-p (or empty-line-p
  6897. (org-previous-line-empty-p))))
  6898. (match-string 0))
  6899. (error (or fix-level "* ")))))
  6900. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  6901. (blank (if (eq blank-a 'auto) empty-line-p blank-a)))
  6902. ;; If we insert after content, move there and clean up
  6903. ;; whitespace.
  6904. (when (and respect-content
  6905. (not (org-looking-at-p org-outline-regexp-bol)))
  6906. (if (not (org-before-first-heading-p))
  6907. (org-end-of-subtree nil t)
  6908. (re-search-forward org-outline-regexp-bol)
  6909. (beginning-of-line 0))
  6910. (skip-chars-backward " \r\t\n")
  6911. (and (not (org-looking-back "^\\*+" (line-beginning-position)))
  6912. (looking-at "[ \t]+") (replace-match ""))
  6913. (unless (eobp) (forward-char 1))
  6914. (when (looking-at "^\\*")
  6915. (unless (bobp) (backward-char 1))
  6916. (insert "\n")))
  6917. ;; If we are splitting, grab the text that should be moved
  6918. ;; to the new headline.
  6919. (when may-split
  6920. (if (org-at-heading-p)
  6921. ;; This is a heading: split intelligently (keeping
  6922. ;; tags).
  6923. (let ((pos (point)))
  6924. (beginning-of-line)
  6925. (unless (looking-at org-complex-heading-regexp)
  6926. (error "This should not happen"))
  6927. (when (and (match-beginning 4)
  6928. (> pos (match-beginning 4))
  6929. (< pos (match-end 4)))
  6930. (setq initial-content (buffer-substring pos (match-end 4)))
  6931. (goto-char pos)
  6932. (delete-region (point) (match-end 4))
  6933. (if (looking-at "[ \t]*$")
  6934. (replace-match "")
  6935. (insert (make-string (length initial-content) ?\s)))
  6936. (setq initial-content (org-trim initial-content)))
  6937. (goto-char pos))
  6938. ;; A normal line.
  6939. (setq initial-content
  6940. (org-trim
  6941. (delete-and-extract-region (point) (line-end-position))))))
  6942. ;; If we are at the beginning of the line, insert before it.
  6943. ;; Otherwise, after it.
  6944. (cond
  6945. ((and (bolp) (looking-at "[ \t]*$")))
  6946. ((bolp) (save-excursion (insert "\n")))
  6947. (t (end-of-line)
  6948. (insert "\n")))
  6949. ;; Insert the new heading
  6950. (insert stars)
  6951. (just-one-space)
  6952. (insert initial-content)
  6953. (unless (and blank (org-previous-line-empty-p))
  6954. (org-N-empty-lines-before-current (if blank 1 0)))
  6955. ;; Adjust visibility, which may be messed up if we removed
  6956. ;; blank lines while previous entry was hidden.
  6957. (let ((bol (line-beginning-position)))
  6958. (dolist (o (overlays-at (1- bol)))
  6959. (when (and (eq (overlay-get o 'invisible) 'outline)
  6960. (eq (overlay-end o) bol))
  6961. (move-overlay o (overlay-start o) (1- bol)))))
  6962. (run-hooks 'org-insert-heading-hook)))))))
  6963. (defun org-N-empty-lines-before-current (N)
  6964. "Make the number of empty lines before current exactly N.
  6965. So this will delete or add empty lines."
  6966. (save-excursion
  6967. (beginning-of-line)
  6968. (let ((p (point)))
  6969. (skip-chars-backward " \r\t\n")
  6970. (unless (bolp) (forward-line))
  6971. (delete-region (point) p))
  6972. (when (> N 0) (insert (make-string N ?\n)))))
  6973. (defun org-get-heading (&optional no-tags no-todo)
  6974. "Return the heading of the current entry, without the stars.
  6975. When NO-TAGS is non-nil, don't include tags.
  6976. When NO-TODO is non-nil, don't include TODO keywords."
  6977. (save-excursion
  6978. (org-back-to-heading t)
  6979. (cond
  6980. ((and no-tags no-todo)
  6981. (looking-at org-complex-heading-regexp)
  6982. (match-string 4))
  6983. (no-tags
  6984. (looking-at (concat org-outline-regexp
  6985. "\\(.*?\\)"
  6986. "\\(?:[ \t]+:[[:alnum:]:_@#%]+:\\)?[ \t]*$"))
  6987. (match-string 1))
  6988. (no-todo
  6989. (looking-at org-todo-line-regexp)
  6990. (match-string 3))
  6991. (t (looking-at org-heading-regexp)
  6992. (match-string 2)))))
  6993. (defvar orgstruct-mode) ; defined below
  6994. (defun org-heading-components ()
  6995. "Return the components of the current heading.
  6996. This is a list with the following elements:
  6997. - the level as an integer
  6998. - the reduced level, different if `org-odd-levels-only' is set.
  6999. - the TODO keyword, or nil
  7000. - the priority character, like ?A, or nil if no priority is given
  7001. - the headline text itself, or the tags string if no headline text
  7002. - the tags string, or nil."
  7003. (save-excursion
  7004. (org-back-to-heading t)
  7005. (when (let (case-fold-search)
  7006. (looking-at
  7007. (if orgstruct-mode
  7008. org-heading-regexp
  7009. org-complex-heading-regexp)))
  7010. (if orgstruct-mode
  7011. (list (length (match-string 1))
  7012. (org-reduced-level (length (match-string 1)))
  7013. nil
  7014. nil
  7015. (match-string 2)
  7016. nil)
  7017. (list (length (match-string 1))
  7018. (org-reduced-level (length (match-string 1)))
  7019. (org-match-string-no-properties 2)
  7020. (and (match-end 3) (aref (match-string 3) 2))
  7021. (org-match-string-no-properties 4)
  7022. (org-match-string-no-properties 5))))))
  7023. (defun org-get-entry ()
  7024. "Get the entry text, after heading, entire subtree."
  7025. (save-excursion
  7026. (org-back-to-heading t)
  7027. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  7028. (defun org-edit-headline (&optional heading)
  7029. "Edit the current headline.
  7030. Set it to HEADING when provided."
  7031. (interactive)
  7032. (org-with-wide-buffer
  7033. (org-back-to-heading t)
  7034. (when (looking-at org-complex-heading-regexp)
  7035. (let* ((old (match-string-no-properties 4))
  7036. (new (org-trim (or heading (read-string "Edit: " old)))))
  7037. (unless (equal old new)
  7038. (if old (replace-match new t t nil 4)
  7039. (goto-char (or (match-end 3) (match-end 2) (match-end 1)))
  7040. (insert " " new))
  7041. (org-set-tags nil t)
  7042. (when (looking-at "[ \t]*$") (replace-match "")))))))
  7043. (defun org-insert-heading-after-current ()
  7044. "Insert a new heading with same level as current, after current subtree."
  7045. (interactive)
  7046. (org-back-to-heading)
  7047. (org-insert-heading)
  7048. (org-move-subtree-down)
  7049. (end-of-line 1))
  7050. (defun org-insert-heading-respect-content (&optional invisible-ok)
  7051. "Insert heading with `org-insert-heading-respect-content' set to t."
  7052. (interactive)
  7053. (org-insert-heading '(4) invisible-ok))
  7054. (defun org-insert-todo-heading-respect-content (&optional force-state)
  7055. "Insert TODO heading with `org-insert-heading-respect-content' set to t."
  7056. (interactive)
  7057. (org-insert-todo-heading force-state '(4)))
  7058. (defun org-insert-todo-heading (arg &optional force-heading)
  7059. "Insert a new heading with the same level and TODO state as current heading.
  7060. If the heading has no TODO state, or if the state is DONE, use the first
  7061. state (TODO by default). Also with one prefix arg, force first state. With
  7062. two prefix args, force inserting at the end of the parent subtree."
  7063. (interactive "P")
  7064. (when (or force-heading (not (org-insert-item 'checkbox)))
  7065. (org-insert-heading (or (and (equal arg '(16)) '(16))
  7066. force-heading))
  7067. (save-excursion
  7068. (org-back-to-heading)
  7069. (outline-previous-heading)
  7070. (looking-at org-todo-line-regexp))
  7071. (let*
  7072. ((new-mark-x
  7073. (if (or (equal arg '(4))
  7074. (not (match-beginning 2))
  7075. (member (match-string 2) org-done-keywords))
  7076. (car org-todo-keywords-1)
  7077. (match-string 2)))
  7078. (new-mark
  7079. (or
  7080. (run-hook-with-args-until-success
  7081. 'org-todo-get-default-hook new-mark-x nil)
  7082. new-mark-x)))
  7083. (beginning-of-line 1)
  7084. (and (looking-at org-outline-regexp) (goto-char (match-end 0))
  7085. (if org-treat-insert-todo-heading-as-state-change
  7086. (org-todo new-mark)
  7087. (insert new-mark " "))))
  7088. (when org-provide-todo-statistics
  7089. (org-update-parent-todo-statistics))))
  7090. (defun org-insert-subheading (arg)
  7091. "Insert a new subheading and demote it.
  7092. Works for outline headings and for plain lists alike."
  7093. (interactive "P")
  7094. (org-insert-heading arg)
  7095. (cond
  7096. ((org-at-heading-p) (org-do-demote))
  7097. ((org-at-item-p) (org-indent-item))))
  7098. (defun org-insert-todo-subheading (arg)
  7099. "Insert a new subheading with TODO keyword or checkbox and demote it.
  7100. Works for outline headings and for plain lists alike."
  7101. (interactive "P")
  7102. (org-insert-todo-heading arg)
  7103. (cond
  7104. ((org-at-heading-p) (org-do-demote))
  7105. ((org-at-item-p) (org-indent-item))))
  7106. ;;; Promotion and Demotion
  7107. (defvar org-after-demote-entry-hook nil
  7108. "Hook run after an entry has been demoted.
  7109. The cursor will be at the beginning of the entry.
  7110. When a subtree is being demoted, the hook will be called for each node.")
  7111. (defvar org-after-promote-entry-hook nil
  7112. "Hook run after an entry has been promoted.
  7113. The cursor will be at the beginning of the entry.
  7114. When a subtree is being promoted, the hook will be called for each node.")
  7115. (defun org-promote-subtree ()
  7116. "Promote the entire subtree.
  7117. See also `org-promote'."
  7118. (interactive)
  7119. (save-excursion
  7120. (org-with-limited-levels (org-map-tree 'org-promote)))
  7121. (org-fix-position-after-promote))
  7122. (defun org-demote-subtree ()
  7123. "Demote the entire subtree.
  7124. See `org-demote' and `org-promote'."
  7125. (interactive)
  7126. (save-excursion
  7127. (org-with-limited-levels (org-map-tree 'org-demote)))
  7128. (org-fix-position-after-promote))
  7129. (defun org-do-promote ()
  7130. "Promote the current heading higher up the tree.
  7131. If the region is active in `transient-mark-mode', promote all
  7132. headings in the region."
  7133. (interactive)
  7134. (save-excursion
  7135. (if (org-region-active-p)
  7136. (org-map-region 'org-promote (region-beginning) (region-end))
  7137. (org-promote)))
  7138. (org-fix-position-after-promote))
  7139. (defun org-do-demote ()
  7140. "Demote the current heading lower down the tree.
  7141. If the region is active in `transient-mark-mode', demote all
  7142. headings in the region."
  7143. (interactive)
  7144. (save-excursion
  7145. (if (org-region-active-p)
  7146. (org-map-region 'org-demote (region-beginning) (region-end))
  7147. (org-demote)))
  7148. (org-fix-position-after-promote))
  7149. (defun org-fix-position-after-promote ()
  7150. "Fix cursor position and indentation after demoting/promoting."
  7151. (let ((pos (point)))
  7152. (when (save-excursion
  7153. (beginning-of-line 1)
  7154. (looking-at org-todo-line-regexp)
  7155. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  7156. (cond ((eobp) (insert " "))
  7157. ((eolp) (insert " "))
  7158. ((equal (char-after) ?\ ) (forward-char 1))))))
  7159. (defun org-current-level ()
  7160. "Return the level of the current entry, or nil if before the first headline.
  7161. The level is the number of stars at the beginning of the
  7162. headline. Use `org-reduced-level' to remove the effect of
  7163. `org-odd-levels'. Unlike to `org-outline-level', this function
  7164. ignores inlinetasks."
  7165. (let ((level (org-with-limited-levels (org-outline-level))))
  7166. (and (> level 0) level)))
  7167. (defun org-get-previous-line-level ()
  7168. "Return the outline depth of the last headline before the current line.
  7169. Returns 0 for the first headline in the buffer, and nil if before the
  7170. first headline."
  7171. (and (org-current-level)
  7172. (or (and (/= (line-beginning-position) (point-min))
  7173. (save-excursion (beginning-of-line 0) (org-current-level)))
  7174. 0)))
  7175. (defun org-reduced-level (l)
  7176. "Compute the effective level of a heading.
  7177. This takes into account the setting of `org-odd-levels-only'."
  7178. (cond
  7179. ((zerop l) 0)
  7180. (org-odd-levels-only (1+ (floor (/ l 2))))
  7181. (t l)))
  7182. (defun org-level-increment ()
  7183. "Return the number of stars that will be added or removed at a
  7184. time to headlines when structure editing, based on the value of
  7185. `org-odd-levels-only'."
  7186. (if org-odd-levels-only 2 1))
  7187. (defun org-get-valid-level (level &optional change)
  7188. "Rectify a level change under the influence of `org-odd-levels-only'
  7189. LEVEL is a current level, CHANGE is by how much the level should be
  7190. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  7191. even level numbers will become the next higher odd number."
  7192. (if org-odd-levels-only
  7193. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  7194. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  7195. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  7196. (max 1 (+ level (or change 0)))))
  7197. (define-obsolete-function-alias 'org-get-legal-level 'org-get-valid-level "23.1")
  7198. (defun org-promote ()
  7199. "Promote the current heading higher up the tree."
  7200. (org-with-wide-buffer
  7201. (org-back-to-heading t)
  7202. (let* ((after-change-functions (remq 'flyspell-after-change-function
  7203. after-change-functions))
  7204. (level (save-match-data (funcall outline-level)))
  7205. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  7206. (diff (abs (- level (length up-head) -1))))
  7207. (cond
  7208. ((and (= level 1) org-allow-promoting-top-level-subtree)
  7209. (replace-match "# " nil t))
  7210. ((= level 1)
  7211. (user-error "Cannot promote to level 0. UNDO to recover if necessary"))
  7212. (t (replace-match up-head nil t)))
  7213. (unless (= level 1)
  7214. (when org-auto-align-tags (org-set-tags nil 'ignore-column))
  7215. (when org-adapt-indentation (org-fixup-indentation (- diff))))
  7216. (run-hooks 'org-after-promote-entry-hook))))
  7217. (defun org-demote ()
  7218. "Demote the current heading lower down the tree."
  7219. (org-with-wide-buffer
  7220. (org-back-to-heading t)
  7221. (let* ((after-change-functions (remq 'flyspell-after-change-function
  7222. after-change-functions))
  7223. (level (save-match-data (funcall outline-level)))
  7224. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  7225. (diff (abs (- level (length down-head) -1))))
  7226. (replace-match down-head nil t)
  7227. (when org-auto-align-tags (org-set-tags nil 'ignore-column))
  7228. (when org-adapt-indentation (org-fixup-indentation diff))
  7229. (run-hooks 'org-after-demote-entry-hook))))
  7230. (defun org-cycle-level ()
  7231. "Cycle the level of an empty headline through possible states.
  7232. This goes first to child, then to parent, level, then up the hierarchy.
  7233. After top level, it switches back to sibling level."
  7234. (interactive)
  7235. (let ((org-adapt-indentation nil))
  7236. (when (org-point-at-end-of-empty-headline)
  7237. (setq this-command 'org-cycle-level) ; Only needed for caching
  7238. (let ((cur-level (org-current-level))
  7239. (prev-level (org-get-previous-line-level)))
  7240. (cond
  7241. ;; If first headline in file, promote to top-level.
  7242. ((= prev-level 0)
  7243. (cl-loop repeat (/ (- cur-level 1) (org-level-increment))
  7244. do (org-do-promote)))
  7245. ;; If same level as prev, demote one.
  7246. ((= prev-level cur-level)
  7247. (org-do-demote))
  7248. ;; If parent is top-level, promote to top level if not already.
  7249. ((= prev-level 1)
  7250. (cl-loop repeat (/ (- cur-level 1) (org-level-increment))
  7251. do (org-do-promote)))
  7252. ;; If top-level, return to prev-level.
  7253. ((= cur-level 1)
  7254. (cl-loop repeat (/ (- prev-level 1) (org-level-increment))
  7255. do (org-do-demote)))
  7256. ;; If less than prev-level, promote one.
  7257. ((< cur-level prev-level)
  7258. (org-do-promote))
  7259. ;; If deeper than prev-level, promote until higher than
  7260. ;; prev-level.
  7261. ((> cur-level prev-level)
  7262. (cl-loop repeat (+ 1 (/ (- cur-level prev-level) (org-level-increment)))
  7263. do (org-do-promote))))
  7264. t))))
  7265. (defun org-map-tree (fun)
  7266. "Call FUN for every heading underneath the current one."
  7267. (org-back-to-heading)
  7268. (let ((level (funcall outline-level)))
  7269. (save-excursion
  7270. (funcall fun)
  7271. (while (and (progn
  7272. (outline-next-heading)
  7273. (> (funcall outline-level) level))
  7274. (not (eobp)))
  7275. (funcall fun)))))
  7276. (defun org-map-region (fun beg end)
  7277. "Call FUN for every heading between BEG and END."
  7278. (let ((org-ignore-region t))
  7279. (save-excursion
  7280. (setq end (copy-marker end))
  7281. (goto-char beg)
  7282. (when (and (re-search-forward org-outline-regexp-bol nil t)
  7283. (< (point) end))
  7284. (funcall fun))
  7285. (while (and (progn
  7286. (outline-next-heading)
  7287. (< (point) end))
  7288. (not (eobp)))
  7289. (funcall fun)))))
  7290. (defun org-fixup-indentation (diff)
  7291. "Change the indentation in the current entry by DIFF.
  7292. DIFF is an integer. Indentation is done according to the
  7293. following rules:
  7294. - Planning information and property drawers are always indented
  7295. according to the new level of the headline;
  7296. - Footnote definitions and their contents are ignored;
  7297. - Inlinetasks' boundaries are not shifted;
  7298. - Empty lines are ignored;
  7299. - Other lines' indentation are shifted by DIFF columns, unless
  7300. it would introduce a structural change in the document, in
  7301. which case no shifting is done at all.
  7302. Assume point is at a heading or an inlinetask beginning."
  7303. (org-with-wide-buffer
  7304. (narrow-to-region (line-beginning-position)
  7305. (save-excursion
  7306. (if (org-with-limited-levels (org-at-heading-p))
  7307. (org-with-limited-levels (outline-next-heading))
  7308. (org-inlinetask-goto-end))
  7309. (point)))
  7310. (forward-line)
  7311. ;; Indent properly planning info and property drawer.
  7312. (when (org-looking-at-p org-planning-line-re)
  7313. (org-indent-line)
  7314. (forward-line))
  7315. (when (looking-at org-property-drawer-re)
  7316. (goto-char (match-end 0))
  7317. (forward-line)
  7318. (save-excursion (org-indent-region (match-beginning 0) (match-end 0))))
  7319. (catch 'no-shift
  7320. (when (zerop diff) (throw 'no-shift nil))
  7321. ;; If DIFF is negative, first check if a shift is possible at all
  7322. ;; (e.g., it doesn't break structure). This can only happen if
  7323. ;; some contents are not properly indented.
  7324. (let ((case-fold-search t))
  7325. (when (< diff 0)
  7326. (let ((diff (- diff))
  7327. (forbidden-re (concat org-outline-regexp
  7328. "\\|"
  7329. (substring org-footnote-definition-re 1))))
  7330. (save-excursion
  7331. (while (not (eobp))
  7332. (cond
  7333. ((org-looking-at-p "[ \t]*$") (forward-line))
  7334. ((and (org-looking-at-p org-footnote-definition-re)
  7335. (let ((e (org-element-at-point)))
  7336. (and (eq (org-element-type e) 'footnote-definition)
  7337. (goto-char (org-element-property :end e))))))
  7338. ((org-looking-at-p org-outline-regexp) (forward-line))
  7339. ;; Give up if shifting would move before column 0 or
  7340. ;; if it would introduce a headline or a footnote
  7341. ;; definition.
  7342. (t
  7343. (skip-chars-forward " \t")
  7344. (let ((ind (current-column)))
  7345. (when (or (< ind diff)
  7346. (and (= ind diff) (org-looking-at-p forbidden-re)))
  7347. (throw 'no-shift nil)))
  7348. ;; Ignore contents of example blocks and source
  7349. ;; blocks if their indentation is meant to be
  7350. ;; preserved. Jump to block's closing line.
  7351. (beginning-of-line)
  7352. (or (and (org-looking-at-p "[ \t]*#\\+BEGIN_\\(EXAMPLE\\|SRC\\)")
  7353. (let ((e (org-element-at-point)))
  7354. (and (memq (org-element-type e)
  7355. '(example-block src-block))
  7356. (or org-src-preserve-indentation
  7357. (org-element-property :preserve-indent e))
  7358. (goto-char (org-element-property :end e))
  7359. (progn (skip-chars-backward " \r\t\n")
  7360. (beginning-of-line)
  7361. t))))
  7362. (forward-line))))))))
  7363. ;; Shift lines but footnote definitions, inlinetasks boundaries
  7364. ;; by DIFF. Also skip contents of source or example blocks
  7365. ;; when indentation is meant to be preserved.
  7366. (while (not (eobp))
  7367. (cond
  7368. ((and (org-looking-at-p org-footnote-definition-re)
  7369. (let ((e (org-element-at-point)))
  7370. (and (eq (org-element-type e) 'footnote-definition)
  7371. (goto-char (org-element-property :end e))))))
  7372. ((org-looking-at-p org-outline-regexp) (forward-line))
  7373. ((org-looking-at-p "[ \t]*$") (forward-line))
  7374. (t
  7375. (org-indent-line-to (+ (org-get-indentation) diff))
  7376. (beginning-of-line)
  7377. (or (and (org-looking-at-p "[ \t]*#\\+BEGIN_\\(EXAMPLE\\|SRC\\)")
  7378. (let ((e (org-element-at-point)))
  7379. (and (memq (org-element-type e)
  7380. '(example-block src-block))
  7381. (or org-src-preserve-indentation
  7382. (org-element-property :preserve-indent e))
  7383. (goto-char (org-element-property :end e))
  7384. (progn (skip-chars-backward " \r\t\n")
  7385. (beginning-of-line)
  7386. t))))
  7387. (forward-line)))))))))
  7388. (defun org-convert-to-odd-levels ()
  7389. "Convert an org-mode file with all levels allowed to one with odd levels.
  7390. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  7391. level 5 etc."
  7392. (interactive)
  7393. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  7394. (let ((outline-level 'org-outline-level)
  7395. (org-odd-levels-only nil) n)
  7396. (save-excursion
  7397. (goto-char (point-min))
  7398. (while (re-search-forward "^\\*\\*+ " nil t)
  7399. (setq n (- (length (match-string 0)) 2))
  7400. (while (>= (setq n (1- n)) 0)
  7401. (org-demote))
  7402. (end-of-line 1))))))
  7403. (defun org-convert-to-oddeven-levels ()
  7404. "Convert an org-mode file with only odd levels to one with odd/even levels.
  7405. This promotes level 3 to level 2, level 5 to level 3 etc. If the
  7406. file contains a section with an even level, conversion would
  7407. destroy the structure of the file. An error is signaled in this
  7408. case."
  7409. (interactive)
  7410. (goto-char (point-min))
  7411. ;; First check if there are no even levels
  7412. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  7413. (org-show-set-visibility 'canonical)
  7414. (error "Not all levels are odd in this file. Conversion not possible"))
  7415. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  7416. (let ((outline-regexp org-outline-regexp)
  7417. (outline-level 'org-outline-level)
  7418. (org-odd-levels-only nil) n)
  7419. (save-excursion
  7420. (goto-char (point-min))
  7421. (while (re-search-forward "^\\*\\*+ " nil t)
  7422. (setq n (/ (1- (length (match-string 0))) 2))
  7423. (while (>= (setq n (1- n)) 0)
  7424. (org-promote))
  7425. (end-of-line 1))))))
  7426. (defun org-tr-level (n)
  7427. "Make N odd if required."
  7428. (if org-odd-levels-only (1+ (/ n 2)) n))
  7429. ;;; Vertical tree motion, cutting and pasting of subtrees
  7430. (defun org-move-subtree-up (&optional arg)
  7431. "Move the current subtree up past ARG headlines of the same level."
  7432. (interactive "p")
  7433. (org-move-subtree-down (- (prefix-numeric-value arg))))
  7434. (defun org-move-subtree-down (&optional arg)
  7435. "Move the current subtree down past ARG headlines of the same level."
  7436. (interactive "p")
  7437. (setq arg (prefix-numeric-value arg))
  7438. (let ((movfunc (if (> arg 0) 'org-get-next-sibling
  7439. 'org-get-last-sibling))
  7440. (ins-point (make-marker))
  7441. (cnt (abs arg))
  7442. (col (current-column))
  7443. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  7444. ;; Select the tree
  7445. (org-back-to-heading)
  7446. (setq beg0 (point))
  7447. (save-excursion
  7448. (setq ne-beg (org-back-over-empty-lines))
  7449. (setq beg (point)))
  7450. (save-match-data
  7451. (save-excursion (outline-end-of-heading)
  7452. (setq folded (outline-invisible-p)))
  7453. (progn (org-end-of-subtree nil t)
  7454. (unless (eobp) (backward-char))))
  7455. (outline-next-heading)
  7456. (setq ne-end (org-back-over-empty-lines))
  7457. (setq end (point))
  7458. (goto-char beg0)
  7459. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  7460. ;; include less whitespace
  7461. (save-excursion
  7462. (goto-char beg)
  7463. (forward-line (- ne-beg ne-end))
  7464. (setq beg (point))))
  7465. ;; Find insertion point, with error handling
  7466. (while (> cnt 0)
  7467. (or (and (funcall movfunc) (looking-at org-outline-regexp))
  7468. (progn (goto-char beg0)
  7469. (user-error "Cannot move past superior level or buffer limit")))
  7470. (setq cnt (1- cnt)))
  7471. (when (> arg 0)
  7472. ;; Moving forward - still need to move over subtree
  7473. (org-end-of-subtree t t)
  7474. (save-excursion
  7475. (org-back-over-empty-lines)
  7476. (or (bolp) (newline))))
  7477. (setq ne-ins (org-back-over-empty-lines))
  7478. (move-marker ins-point (point))
  7479. (setq txt (buffer-substring beg end))
  7480. (org-save-markers-in-region beg end)
  7481. (delete-region beg end)
  7482. (org-remove-empty-overlays-at beg)
  7483. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  7484. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  7485. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  7486. (let ((bbb (point)))
  7487. (insert-before-markers txt)
  7488. (org-reinstall-markers-in-region bbb)
  7489. (move-marker ins-point bbb))
  7490. (or (bolp) (insert "\n"))
  7491. (setq ins-end (point))
  7492. (goto-char ins-point)
  7493. (org-skip-whitespace)
  7494. (when (and (< arg 0)
  7495. (org-first-sibling-p)
  7496. (> ne-ins ne-beg))
  7497. ;; Move whitespace back to beginning
  7498. (save-excursion
  7499. (goto-char ins-end)
  7500. (let ((kill-whole-line t))
  7501. (kill-line (- ne-ins ne-beg)) (point)))
  7502. (insert (make-string (- ne-ins ne-beg) ?\n)))
  7503. (move-marker ins-point nil)
  7504. (if folded
  7505. (outline-hide-subtree)
  7506. (org-show-entry)
  7507. (org-show-children)
  7508. (org-cycle-hide-drawers 'children))
  7509. (org-clean-visibility-after-subtree-move)
  7510. ;; move back to the initial column we were at
  7511. (move-to-column col)))
  7512. (defvar org-subtree-clip ""
  7513. "Clipboard for cut and paste of subtrees.
  7514. This is actually only a copy of the kill, because we use the normal kill
  7515. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  7516. (defvar org-subtree-clip-folded nil
  7517. "Was the last copied subtree folded?
  7518. This is used to fold the tree back after pasting.")
  7519. (defun org-cut-subtree (&optional n)
  7520. "Cut the current subtree into the clipboard.
  7521. With prefix arg N, cut this many sequential subtrees.
  7522. This is a short-hand for marking the subtree and then cutting it."
  7523. (interactive "p")
  7524. (org-copy-subtree n 'cut))
  7525. (defun org-copy-subtree (&optional n cut force-store-markers nosubtrees)
  7526. "Copy the current subtree it in the clipboard.
  7527. With prefix arg N, copy this many sequential subtrees.
  7528. This is a short-hand for marking the subtree and then copying it.
  7529. If CUT is non-nil, actually cut the subtree.
  7530. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  7531. of some markers in the region, even if CUT is non-nil. This is
  7532. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  7533. (interactive "p")
  7534. (let (beg end folded (beg0 (point)))
  7535. (if (org-called-interactively-p 'any)
  7536. (org-back-to-heading nil) ; take what looks like a subtree
  7537. (org-back-to-heading t)) ; take what is really there
  7538. (setq beg (point))
  7539. (skip-chars-forward " \t\r\n")
  7540. (save-match-data
  7541. (if nosubtrees
  7542. (outline-next-heading)
  7543. (save-excursion (outline-end-of-heading)
  7544. (setq folded (outline-invisible-p)))
  7545. (ignore-errors (org-forward-heading-same-level (1- n) t))
  7546. (org-end-of-subtree t t)))
  7547. ;; Include the end of an inlinetask
  7548. (when (and (featurep 'org-inlinetask)
  7549. (looking-at-p (concat (org-inlinetask-outline-regexp)
  7550. "END[ \t]*$")))
  7551. (end-of-line))
  7552. (setq end (point))
  7553. (goto-char beg0)
  7554. (when (> end beg)
  7555. (setq org-subtree-clip-folded folded)
  7556. (when (or cut force-store-markers)
  7557. (org-save-markers-in-region beg end))
  7558. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  7559. (setq org-subtree-clip (current-kill 0))
  7560. (message "%s: Subtree(s) with %d characters"
  7561. (if cut "Cut" "Copied")
  7562. (length org-subtree-clip)))))
  7563. (defun org-paste-subtree (&optional level tree for-yank remove)
  7564. "Paste the clipboard as a subtree, with modification of headline level.
  7565. The entire subtree is promoted or demoted in order to match a new headline
  7566. level.
  7567. If the cursor is at the beginning of a headline, the same level as
  7568. that headline is used to paste the tree.
  7569. If not, the new level is derived from the *visible* headings
  7570. before and after the insertion point, and taken to be the inferior headline
  7571. level of the two. So if the previous visible heading is level 3 and the
  7572. next is level 4 (or vice versa), level 4 will be used for insertion.
  7573. This makes sure that the subtree remains an independent subtree and does
  7574. not swallow low level entries.
  7575. You can also force a different level, either by using a numeric prefix
  7576. argument, or by inserting the heading marker by hand. For example, if the
  7577. cursor is after \"*****\", then the tree will be shifted to level 5.
  7578. If optional TREE is given, use this text instead of the kill ring.
  7579. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  7580. move back over whitespace before inserting, and move point to the end of
  7581. the inserted text when done.
  7582. When REMOVE is non-nil, remove the subtree from the clipboard."
  7583. (interactive "P")
  7584. (setq tree (or tree (and kill-ring (current-kill 0))))
  7585. (unless (org-kill-is-subtree-p tree)
  7586. (user-error "%s"
  7587. (substitute-command-keys
  7588. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  7589. (org-with-limited-levels
  7590. (let* ((visp (not (outline-invisible-p)))
  7591. (txt tree)
  7592. (^re_ "\\(\\*+\\)[ \t]*")
  7593. (old-level (if (string-match org-outline-regexp-bol txt)
  7594. (- (match-end 0) (match-beginning 0) 1)
  7595. -1))
  7596. (force-level (cond (level (prefix-numeric-value level))
  7597. ((and (looking-at "[ \t]*$")
  7598. (string-match
  7599. "^\\*+$" (buffer-substring
  7600. (point-at-bol) (point))))
  7601. (- (match-end 0) (match-beginning 0)))
  7602. ((and (bolp)
  7603. (looking-at org-outline-regexp))
  7604. (- (match-end 0) (point) 1))))
  7605. (previous-level (save-excursion
  7606. (condition-case nil
  7607. (progn
  7608. (outline-previous-visible-heading 1)
  7609. (if (looking-at ^re_)
  7610. (- (match-end 0) (match-beginning 0) 1)
  7611. 1))
  7612. (error 1))))
  7613. (next-level (save-excursion
  7614. (condition-case nil
  7615. (progn
  7616. (or (looking-at org-outline-regexp)
  7617. (outline-next-visible-heading 1))
  7618. (if (looking-at ^re_)
  7619. (- (match-end 0) (match-beginning 0) 1)
  7620. 1))
  7621. (error 1))))
  7622. (new-level (or force-level (max previous-level next-level)))
  7623. (shift (if (or (= old-level -1)
  7624. (= new-level -1)
  7625. (= old-level new-level))
  7626. 0
  7627. (- new-level old-level)))
  7628. (delta (if (> shift 0) -1 1))
  7629. (func (if (> shift 0) 'org-demote 'org-promote))
  7630. (org-odd-levels-only nil)
  7631. beg end newend)
  7632. ;; Remove the forced level indicator
  7633. (when force-level
  7634. (delete-region (point-at-bol) (point)))
  7635. ;; Paste
  7636. (beginning-of-line (if (bolp) 1 2))
  7637. (setq beg (point))
  7638. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  7639. (insert-before-markers txt)
  7640. (unless (string-match "\n\\'" txt) (insert "\n"))
  7641. (setq newend (point))
  7642. (org-reinstall-markers-in-region beg)
  7643. (setq end (point))
  7644. (goto-char beg)
  7645. (skip-chars-forward " \t\n\r")
  7646. (setq beg (point))
  7647. (when (and (outline-invisible-p) visp)
  7648. (save-excursion (outline-show-heading)))
  7649. ;; Shift if necessary
  7650. (unless (= shift 0)
  7651. (save-restriction
  7652. (narrow-to-region beg end)
  7653. (while (not (= shift 0))
  7654. (org-map-region func (point-min) (point-max))
  7655. (setq shift (+ delta shift)))
  7656. (goto-char (point-min))
  7657. (setq newend (point-max))))
  7658. (when (or (org-called-interactively-p 'interactive) for-yank)
  7659. (message "Clipboard pasted as level %d subtree" new-level))
  7660. (when (and (not for-yank) ; in this case, org-yank will decide about folding
  7661. kill-ring
  7662. (eq org-subtree-clip (current-kill 0))
  7663. org-subtree-clip-folded)
  7664. ;; The tree was folded before it was killed/copied
  7665. (outline-hide-subtree))
  7666. (and for-yank (goto-char newend))
  7667. (and remove (setq kill-ring (cdr kill-ring))))))
  7668. (defun org-kill-is-subtree-p (&optional txt)
  7669. "Check if the current kill is an outline subtree, or a set of trees.
  7670. Returns nil if kill does not start with a headline, or if the first
  7671. headline level is not the largest headline level in the tree.
  7672. So this will actually accept several entries of equal levels as well,
  7673. which is OK for `org-paste-subtree'.
  7674. If optional TXT is given, check this string instead of the current kill."
  7675. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  7676. (re (org-get-limited-outline-regexp))
  7677. (^re (concat "^" re))
  7678. (start-level (and kill
  7679. (string-match
  7680. (concat "\\`\\([ \t\n\r]*?\n\\)?\\(" re "\\)")
  7681. kill)
  7682. (- (match-end 2) (match-beginning 2) 1)))
  7683. (start (1+ (or (match-beginning 2) -1))))
  7684. (if (not start-level)
  7685. (progn
  7686. nil) ;; does not even start with a heading
  7687. (catch 'exit
  7688. (while (setq start (string-match ^re kill (1+ start)))
  7689. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  7690. (throw 'exit nil)))
  7691. t))))
  7692. (defvar org-markers-to-move nil
  7693. "Markers that should be moved with a cut-and-paste operation.
  7694. Those markers are stored together with their positions relative to
  7695. the start of the region.")
  7696. (defun org-save-markers-in-region (beg end)
  7697. "Check markers in region.
  7698. If these markers are between BEG and END, record their position relative
  7699. to BEG, so that after moving the block of text, we can put the markers back
  7700. into place.
  7701. This function gets called just before an entry or tree gets cut from the
  7702. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  7703. called immediately, to move the markers with the entries."
  7704. (setq org-markers-to-move nil)
  7705. (when (featurep 'org-clock)
  7706. (org-clock-save-markers-for-cut-and-paste beg end))
  7707. (when (featurep 'org-agenda)
  7708. (org-agenda-save-markers-for-cut-and-paste beg end)))
  7709. (defun org-check-and-save-marker (marker beg end)
  7710. "Check if MARKER is between BEG and END.
  7711. If yes, remember the marker and the distance to BEG."
  7712. (when (and (marker-buffer marker)
  7713. (equal (marker-buffer marker) (current-buffer))
  7714. (>= marker beg) (< marker end))
  7715. (push (cons marker (- marker beg)) org-markers-to-move)))
  7716. (defun org-reinstall-markers-in-region (beg)
  7717. "Move all remembered markers to their position relative to BEG."
  7718. (dolist (x org-markers-to-move)
  7719. (move-marker (car x) (+ beg (cdr x))))
  7720. (setq org-markers-to-move nil))
  7721. (defun org-narrow-to-subtree ()
  7722. "Narrow buffer to the current subtree."
  7723. (interactive)
  7724. (save-excursion
  7725. (save-match-data
  7726. (org-with-limited-levels
  7727. (narrow-to-region
  7728. (progn (org-back-to-heading t) (point))
  7729. (progn (org-end-of-subtree t t)
  7730. (when (and (org-at-heading-p) (not (eobp))) (backward-char 1))
  7731. (point)))))))
  7732. (defun org-narrow-to-block ()
  7733. "Narrow buffer to the current block."
  7734. (interactive)
  7735. (let* ((case-fold-search t)
  7736. (blockp (org-between-regexps-p "^[ \t]*#\\+begin_.*"
  7737. "^[ \t]*#\\+end_.*")))
  7738. (if blockp
  7739. (narrow-to-region (car blockp) (cdr blockp))
  7740. (user-error "Not in a block"))))
  7741. (defun org-clone-subtree-with-time-shift (n &optional shift)
  7742. "Clone the task (subtree) at point N times.
  7743. The clones will be inserted as siblings.
  7744. In interactive use, the user will be prompted for the number of
  7745. clones to be produced. If the entry has a timestamp, the user
  7746. will also be prompted for a time shift, which may be a repeater
  7747. as used in time stamps, for example `+3d'. To disable this,
  7748. you can call the function with a universal prefix argument.
  7749. When a valid repeater is given and the entry contains any time
  7750. stamps, the clones will become a sequence in time, with time
  7751. stamps in the subtree shifted for each clone produced. If SHIFT
  7752. is nil or the empty string, time stamps will be left alone. The
  7753. ID property of the original subtree is removed.
  7754. If the original subtree did contain time stamps with a repeater,
  7755. the following will happen:
  7756. - the repeater will be removed in each clone
  7757. - an additional clone will be produced, with the current, unshifted
  7758. date(s) in the entry.
  7759. - the original entry will be placed *after* all the clones, with
  7760. repeater intact.
  7761. - the start days in the repeater in the original entry will be shifted
  7762. to past the last clone.
  7763. In this way you can spell out a number of instances of a repeating task,
  7764. and still retain the repeater to cover future instances of the task.
  7765. As described above, N+1 clones are produced when the original
  7766. subtree has a repeater. Setting N to 0, then, can be used to
  7767. remove the repeater from a subtree and create a shifted clone
  7768. with the original repeater."
  7769. (interactive "nNumber of clones to produce: ")
  7770. (let ((shift
  7771. (or shift
  7772. (if (and (not (equal current-prefix-arg '(4)))
  7773. (save-excursion
  7774. (re-search-forward org-ts-regexp-both
  7775. (save-excursion
  7776. (org-end-of-subtree t)
  7777. (point)) t)))
  7778. (read-from-minibuffer
  7779. "Date shift per clone (e.g. +1w, empty to copy unchanged): ")
  7780. ""))) ;; No time shift
  7781. (n-no-remove -1)
  7782. (drawer-re org-drawer-regexp)
  7783. (org-clock-re (format "^[ \t]*%s.*$" org-clock-string))
  7784. beg end template task idprop
  7785. shift-n shift-what doshift nmin nmax)
  7786. (unless (wholenump n)
  7787. (user-error "Invalid number of replications %s" n))
  7788. (when (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  7789. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([hdwmy]\\)[ \t]*\\'"
  7790. shift)))
  7791. (user-error "Invalid shift specification %s" shift))
  7792. (when doshift
  7793. (setq shift-n (string-to-number (match-string 1 shift))
  7794. shift-what (cdr (assoc (match-string 2 shift)
  7795. '(("d" . day) ("w" . week)
  7796. ("m" . month) ("y" . year))))))
  7797. (when (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  7798. (setq nmin 1 nmax n)
  7799. (org-back-to-heading t)
  7800. (setq beg (point))
  7801. (setq idprop (org-entry-get nil "ID"))
  7802. (org-end-of-subtree t t)
  7803. (or (bolp) (insert "\n"))
  7804. (setq end (point))
  7805. (setq template (buffer-substring beg end))
  7806. (when (and doshift
  7807. (string-match "<[^<>\n]+ [.+]?\\+[0-9]+[hdwmy][^<>\n]*>" template))
  7808. (delete-region beg end)
  7809. (setq end beg)
  7810. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  7811. (goto-char end)
  7812. (cl-loop for n from nmin to nmax do
  7813. ;; prepare clone
  7814. (with-temp-buffer
  7815. (insert template)
  7816. (org-mode)
  7817. (goto-char (point-min))
  7818. (org-show-subtree)
  7819. (and idprop (if org-clone-delete-id
  7820. (org-entry-delete nil "ID")
  7821. (org-id-get-create t)))
  7822. (unless (= n 0)
  7823. (while (re-search-forward org-clock-re nil t)
  7824. (kill-whole-line))
  7825. (goto-char (point-min))
  7826. (while (re-search-forward drawer-re nil t)
  7827. (org-remove-empty-drawer-at (point))))
  7828. (goto-char (point-min))
  7829. (when doshift
  7830. (while (re-search-forward org-ts-regexp-both nil t)
  7831. (org-timestamp-change (* n shift-n) shift-what))
  7832. (unless (= n n-no-remove)
  7833. (goto-char (point-min))
  7834. (while (re-search-forward org-ts-regexp nil t)
  7835. (save-excursion
  7836. (goto-char (match-beginning 0))
  7837. (when (looking-at "<[^<>\n]+\\( +[.+]?\\+[0-9]+[hdwmy]\\)")
  7838. (delete-region (match-beginning 1) (match-end 1)))))))
  7839. (setq task (buffer-string)))
  7840. (insert task))
  7841. (goto-char beg)))
  7842. ;;; Outline Sorting
  7843. (defun org-sort (with-case)
  7844. "Call `org-sort-entries', `org-table-sort-lines' or `org-sort-list'.
  7845. Optional argument WITH-CASE means sort case-sensitively."
  7846. (interactive "P")
  7847. (cond
  7848. ((org-at-table-p) (org-call-with-arg 'org-table-sort-lines with-case))
  7849. ((org-at-item-p) (org-call-with-arg 'org-sort-list with-case))
  7850. (t
  7851. (org-call-with-arg 'org-sort-entries with-case))))
  7852. (defun org-sort-remove-invisible (s)
  7853. "Remove invisible links from string S."
  7854. (remove-text-properties 0 (length s) org-rm-props s)
  7855. (while (string-match org-bracket-link-regexp s)
  7856. (setq s (replace-match (if (match-end 2)
  7857. (match-string 3 s)
  7858. (match-string 1 s))
  7859. t t s)))
  7860. (let ((st (format " %s " s)))
  7861. (while (string-match org-emph-re st)
  7862. (setq st (replace-match (format " %s " (match-string 4 st)) t t st)))
  7863. (setq s (substring st 1 -1)))
  7864. s)
  7865. (defvar org-priority-regexp) ; defined later in the file
  7866. (defvar org-after-sorting-entries-or-items-hook nil
  7867. "Hook that is run after a bunch of entries or items have been sorted.
  7868. When children are sorted, the cursor is in the parent line when this
  7869. hook gets called. When a region or a plain list is sorted, the cursor
  7870. will be in the first entry of the sorted region/list.")
  7871. (defun org-sort-entries
  7872. (&optional with-case sorting-type getkey-func compare-func property)
  7873. "Sort entries on a certain level of an outline tree.
  7874. If there is an active region, the entries in the region are sorted.
  7875. Else, if the cursor is before the first entry, sort the top-level items.
  7876. Else, the children of the entry at point are sorted.
  7877. Sorting can be alphabetically, numerically, by date/time as given by
  7878. a time stamp, by a property, by priority order, or by a custom function.
  7879. The command prompts for the sorting type unless it has been given to the
  7880. function through the SORTING-TYPE argument, which needs to be a character,
  7881. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?o ?O ?r ?R ?f ?F ?k ?K). Here is
  7882. the precise meaning of each character:
  7883. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  7884. c By creation time, which is assumed to be the first inactive time stamp
  7885. at the beginning of a line.
  7886. d By deadline date/time.
  7887. k By clocking time.
  7888. n Numerically, by converting the beginning of the entry/item to a number.
  7889. o By order of TODO keywords.
  7890. p By priority according to the cookie.
  7891. r By the value of a property.
  7892. s By scheduled date/time.
  7893. t By date/time, either the first active time stamp in the entry, or, if
  7894. none exist, by the first inactive one.
  7895. Capital letters will reverse the sort order.
  7896. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  7897. called with point at the beginning of the record. It must return either
  7898. a string or a number that should serve as the sorting key for that record.
  7899. Comparing entries ignores case by default. However, with an optional argument
  7900. WITH-CASE, the sorting considers case as well.
  7901. Sorting is done against the visible part of the headlines, it ignores hidden
  7902. links.
  7903. When sorting is done, call `org-after-sorting-entries-or-items-hook'."
  7904. (interactive "P")
  7905. (let ((case-func (if with-case 'identity 'downcase))
  7906. (cmstr
  7907. ;; The clock marker is lost when using `sort-subr', let's
  7908. ;; store the clocking string.
  7909. (when (equal (marker-buffer org-clock-marker) (current-buffer))
  7910. (save-excursion
  7911. (goto-char org-clock-marker)
  7912. (buffer-substring-no-properties (line-beginning-position)
  7913. (point)))))
  7914. start beg end stars re re2
  7915. txt what tmp)
  7916. ;; Find beginning and end of region to sort
  7917. (cond
  7918. ((org-region-active-p)
  7919. ;; we will sort the region
  7920. (setq end (region-end)
  7921. what "region")
  7922. (goto-char (region-beginning))
  7923. (unless (org-at-heading-p) (outline-next-heading))
  7924. (setq start (point)))
  7925. ((or (org-at-heading-p)
  7926. (ignore-errors (progn (org-back-to-heading) t)))
  7927. ;; we will sort the children of the current headline
  7928. (org-back-to-heading)
  7929. (setq start (point)
  7930. end (progn (org-end-of-subtree t t)
  7931. (or (bolp) (insert "\n"))
  7932. (when (>= (org-back-over-empty-lines) 1)
  7933. (forward-line 1))
  7934. (point))
  7935. what "children")
  7936. (goto-char start)
  7937. (outline-show-subtree)
  7938. (outline-next-heading))
  7939. (t
  7940. ;; we will sort the top-level entries in this file
  7941. (goto-char (point-min))
  7942. (or (org-at-heading-p) (outline-next-heading))
  7943. (setq start (point))
  7944. (goto-char (point-max))
  7945. (beginning-of-line 1)
  7946. (when (looking-at ".*?\\S-")
  7947. ;; File ends in a non-white line
  7948. (end-of-line 1)
  7949. (insert "\n"))
  7950. (setq end (point-max))
  7951. (setq what "top-level")
  7952. (goto-char start)
  7953. (outline-show-all)))
  7954. (setq beg (point))
  7955. (when (>= beg end) (goto-char start) (user-error "Nothing to sort"))
  7956. (looking-at "\\(\\*+\\)")
  7957. (setq stars (match-string 1)
  7958. re (concat "^" (regexp-quote stars) " +")
  7959. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[ \t\n]")
  7960. txt (buffer-substring beg end))
  7961. (unless (equal (substring txt -1) "\n") (setq txt (concat txt "\n")))
  7962. (when (and (not (equal stars "*")) (string-match re2 txt))
  7963. (user-error "Region to sort contains a level above the first entry"))
  7964. (unless sorting-type
  7965. (message
  7966. "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
  7967. [t]ime [s]cheduled [d]eadline [c]reated cloc[k]ing
  7968. A/N/P/R/O/F/T/S/D/C/K means reversed:"
  7969. what)
  7970. (setq sorting-type (read-char-exclusive))
  7971. (unless getkey-func
  7972. (and (= (downcase sorting-type) ?f)
  7973. (setq getkey-func
  7974. (completing-read "Sort using function: "
  7975. obarray 'fboundp t nil nil))
  7976. (setq getkey-func (intern getkey-func))))
  7977. (and (= (downcase sorting-type) ?r)
  7978. (not property)
  7979. (setq property
  7980. (completing-read "Property: "
  7981. (mapcar #'list (org-buffer-property-keys t))
  7982. nil t))))
  7983. (when (member sorting-type '(?k ?K)) (org-clock-sum))
  7984. (message "Sorting entries...")
  7985. (save-restriction
  7986. (narrow-to-region start end)
  7987. (let ((dcst (downcase sorting-type))
  7988. (case-fold-search nil)
  7989. (now (current-time)))
  7990. (sort-subr
  7991. (/= dcst sorting-type)
  7992. ;; This function moves to the beginning character of the "record" to
  7993. ;; be sorted.
  7994. (lambda nil
  7995. (if (re-search-forward re nil t)
  7996. (goto-char (match-beginning 0))
  7997. (goto-char (point-max))))
  7998. ;; This function moves to the last character of the "record" being
  7999. ;; sorted.
  8000. (lambda nil
  8001. (save-match-data
  8002. (condition-case nil
  8003. (outline-forward-same-level 1)
  8004. (error
  8005. (goto-char (point-max))))))
  8006. ;; This function returns the value that gets sorted against.
  8007. (lambda nil
  8008. (cond
  8009. ((= dcst ?n)
  8010. (if (looking-at org-complex-heading-regexp)
  8011. (string-to-number (org-sort-remove-invisible (match-string 4)))
  8012. nil))
  8013. ((= dcst ?a)
  8014. (if (looking-at org-complex-heading-regexp)
  8015. (funcall case-func (org-sort-remove-invisible (match-string 4)))
  8016. nil))
  8017. ((= dcst ?k)
  8018. (or (get-text-property (point) :org-clock-minutes) 0))
  8019. ((= dcst ?t)
  8020. (let ((end (save-excursion (outline-next-heading) (point))))
  8021. (if (or (re-search-forward org-ts-regexp end t)
  8022. (re-search-forward org-ts-regexp-both end t))
  8023. (org-time-string-to-seconds (match-string 0))
  8024. (org-float-time now))))
  8025. ((= dcst ?c)
  8026. (let ((end (save-excursion (outline-next-heading) (point))))
  8027. (if (re-search-forward
  8028. (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
  8029. end t)
  8030. (org-time-string-to-seconds (match-string 0))
  8031. (org-float-time now))))
  8032. ((= dcst ?s)
  8033. (let ((end (save-excursion (outline-next-heading) (point))))
  8034. (if (re-search-forward org-scheduled-time-regexp end t)
  8035. (org-time-string-to-seconds (match-string 1))
  8036. (org-float-time now))))
  8037. ((= dcst ?d)
  8038. (let ((end (save-excursion (outline-next-heading) (point))))
  8039. (if (re-search-forward org-deadline-time-regexp end t)
  8040. (org-time-string-to-seconds (match-string 1))
  8041. (org-float-time now))))
  8042. ((= dcst ?p)
  8043. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  8044. (string-to-char (match-string 2))
  8045. org-default-priority))
  8046. ((= dcst ?r)
  8047. (or (org-entry-get nil property) ""))
  8048. ((= dcst ?o)
  8049. (when (looking-at org-complex-heading-regexp)
  8050. (let* ((m (match-string 2))
  8051. (s (if (member m org-done-keywords) '- '+)))
  8052. (- 99 (funcall s (length (member m org-todo-keywords-1)))))))
  8053. ((= dcst ?f)
  8054. (if getkey-func
  8055. (progn
  8056. (setq tmp (funcall getkey-func))
  8057. (when (stringp tmp) (setq tmp (funcall case-func tmp)))
  8058. tmp)
  8059. (error "Invalid key function `%s'" getkey-func)))
  8060. (t (error "Invalid sorting type `%c'" sorting-type))))
  8061. nil
  8062. (cond
  8063. ((= dcst ?a) 'string<)
  8064. ((= dcst ?f) compare-func)
  8065. ((member dcst '(?p ?t ?s ?d ?c ?k)) '<)))))
  8066. (run-hooks 'org-after-sorting-entries-or-items-hook)
  8067. ;; Reset the clock marker if needed
  8068. (when cmstr
  8069. (save-excursion
  8070. (goto-char start)
  8071. (search-forward cmstr nil t)
  8072. (move-marker org-clock-marker (point))))
  8073. (message "Sorting entries...done")))
  8074. ;;; The orgstruct minor mode
  8075. ;; Define a minor mode which can be used in other modes in order to
  8076. ;; integrate the org-mode structure editing commands.
  8077. ;; This is really a hack, because the org-mode structure commands use
  8078. ;; keys which normally belong to the major mode. Here is how it
  8079. ;; works: The minor mode defines all the keys necessary to operate the
  8080. ;; structure commands, but wraps the commands into a function which
  8081. ;; tests if the cursor is currently at a headline or a plain list
  8082. ;; item. If that is the case, the structure command is used,
  8083. ;; temporarily setting many Org-mode variables like regular
  8084. ;; expressions for filling etc. However, when any of those keys is
  8085. ;; used at a different location, function uses `key-binding' to look
  8086. ;; up if the key has an associated command in another currently active
  8087. ;; keymap (minor modes, major mode, global), and executes that
  8088. ;; command. There might be problems if any of the keys is otherwise
  8089. ;; used as a prefix key.
  8090. (defcustom orgstruct-heading-prefix-regexp ""
  8091. "Regexp that matches the custom prefix of Org headlines in
  8092. orgstruct(++)-mode."
  8093. :group 'org
  8094. :version "24.4"
  8095. :package-version '(Org . "8.3")
  8096. :type 'regexp)
  8097. ;;;###autoload(put 'orgstruct-heading-prefix-regexp 'safe-local-variable 'stringp)
  8098. (defcustom orgstruct-setup-hook nil
  8099. "Hook run after orgstruct-mode-map is filled."
  8100. :group 'org
  8101. :version "24.4"
  8102. :package-version '(Org . "8.0")
  8103. :type 'hook)
  8104. (defvar orgstruct-initialized nil)
  8105. (defvar org-local-vars nil
  8106. "List of local variables, for use by `orgstruct-mode'.")
  8107. ;;;###autoload
  8108. (define-minor-mode orgstruct-mode
  8109. "Toggle the minor mode `orgstruct-mode'.
  8110. This mode is for using Org-mode structure commands in other
  8111. modes. The following keys behave as if Org-mode were active, if
  8112. the cursor is on a headline, or on a plain list item (both as
  8113. defined by Org-mode)."
  8114. nil " OrgStruct" (make-sparse-keymap)
  8115. (funcall (if orgstruct-mode
  8116. 'add-to-invisibility-spec
  8117. 'remove-from-invisibility-spec)
  8118. '(outline . t))
  8119. (when orgstruct-mode
  8120. (org-load-modules-maybe)
  8121. (unless orgstruct-initialized
  8122. (orgstruct-setup)
  8123. (setq orgstruct-initialized t))))
  8124. ;;;###autoload
  8125. (defun turn-on-orgstruct ()
  8126. "Unconditionally turn on `orgstruct-mode'."
  8127. (orgstruct-mode 1))
  8128. (defvar-local orgstruct-is-++ nil
  8129. "Is `orgstruct-mode' in ++ version in the current-buffer?")
  8130. (defvar-local org-fb-vars nil)
  8131. (defun orgstruct++-mode (&optional arg)
  8132. "Toggle `orgstruct-mode', the enhanced version of it.
  8133. In addition to setting orgstruct-mode, this also exports all
  8134. indentation and autofilling variables from org-mode into the
  8135. buffer. It will also recognize item context in multiline items."
  8136. (interactive "P")
  8137. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  8138. (if (< arg 1)
  8139. (progn (orgstruct-mode -1)
  8140. (dolist (v org-fb-vars)
  8141. (set (make-local-variable (car v))
  8142. (if (eq (car-safe (cadr v)) 'quote)
  8143. (cl-cadadr v)
  8144. (nth 1 v)))))
  8145. (orgstruct-mode 1)
  8146. (setq org-fb-vars nil)
  8147. (unless org-local-vars
  8148. (setq org-local-vars (org-get-local-variables)))
  8149. (let (var val)
  8150. (dolist (x org-local-vars)
  8151. (when (string-match
  8152. "^\\(paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\
  8153. \\|fill-prefix\\|indent-\\)"
  8154. (symbol-name (car x)))
  8155. (setq var (car x) val (nth 1 x))
  8156. (push (list var `(quote ,(eval var))) org-fb-vars)
  8157. (set (make-local-variable var)
  8158. (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  8159. (setq-local orgstruct-is-++ t))))
  8160. ;;;###autoload
  8161. (defun turn-on-orgstruct++ ()
  8162. "Unconditionally turn on `orgstruct++-mode'."
  8163. (orgstruct++-mode 1))
  8164. (defun orgstruct-error ()
  8165. "Error when there is no default binding for a structure key."
  8166. (interactive)
  8167. (funcall (if (fboundp 'user-error)
  8168. 'user-error
  8169. 'error)
  8170. "This key has no function outside structure elements"))
  8171. (defun orgstruct-setup ()
  8172. "Setup orgstruct keymap."
  8173. (dolist (cell '((org-demote . t)
  8174. (org-metaleft . t)
  8175. (org-metaright . t)
  8176. (org-promote . t)
  8177. (org-shiftmetaleft . t)
  8178. (org-shiftmetaright . t)
  8179. org-backward-element
  8180. org-backward-heading-same-level
  8181. org-ctrl-c-ret
  8182. org-ctrl-c-minus
  8183. org-ctrl-c-star
  8184. org-cycle
  8185. org-force-cycle-archived
  8186. org-forward-heading-same-level
  8187. org-insert-heading
  8188. org-insert-heading-respect-content
  8189. org-kill-note-or-show-branches
  8190. org-mark-subtree
  8191. org-meta-return
  8192. org-metadown
  8193. org-metaup
  8194. org-narrow-to-subtree
  8195. org-promote-subtree
  8196. org-reveal
  8197. org-shiftdown
  8198. org-shiftleft
  8199. org-shiftmetadown
  8200. org-shiftmetaup
  8201. org-shiftright
  8202. org-shifttab
  8203. org-shifttab
  8204. org-shiftup
  8205. org-show-children
  8206. org-show-subtree
  8207. org-sort
  8208. org-up-element
  8209. outline-demote
  8210. outline-next-visible-heading
  8211. outline-previous-visible-heading
  8212. outline-promote
  8213. outline-up-heading))
  8214. (let ((f (or (car-safe cell) cell))
  8215. (disable-when-heading-prefix (cdr-safe cell)))
  8216. (when (fboundp f)
  8217. (let ((new-bindings))
  8218. (dolist (binding (nconc (where-is-internal f org-mode-map)
  8219. (where-is-internal f outline-mode-map)))
  8220. (push binding new-bindings)
  8221. ;; TODO use local-function-key-map
  8222. (dolist (rep '(("<tab>" . "TAB")
  8223. ("<return>" . "RET")
  8224. ("<escape>" . "ESC")
  8225. ("<delete>" . "DEL")))
  8226. (setq binding (read-kbd-macro
  8227. (let ((case-fold-search))
  8228. (replace-regexp-in-string
  8229. (regexp-quote (cdr rep))
  8230. (car rep)
  8231. (key-description binding)))))
  8232. (cl-pushnew binding new-bindings :test 'equal)))
  8233. (dolist (binding new-bindings)
  8234. (let ((key (lookup-key orgstruct-mode-map binding)))
  8235. (when (or (not key) (numberp key))
  8236. (ignore-errors
  8237. (org-defkey orgstruct-mode-map
  8238. binding
  8239. (orgstruct-make-binding
  8240. f binding disable-when-heading-prefix))))))))))
  8241. (run-hooks 'orgstruct-setup-hook))
  8242. (defun orgstruct-make-binding (fun key disable-when-heading-prefix)
  8243. "Create a function for binding in the structure minor mode.
  8244. FUN is the command to call inside a table. KEY is the key that
  8245. should be checked in for a command to execute outside of tables.
  8246. Non-nil `disable-when-heading-prefix' means to disable the command
  8247. if `orgstruct-heading-prefix-regexp' is not empty."
  8248. (let ((name (concat "orgstruct-hijacker-" (symbol-name fun))))
  8249. (let ((nname name)
  8250. (i 0))
  8251. (while (fboundp (intern nname))
  8252. (setq nname (format "%s-%d" name (setq i (1+ i)))))
  8253. (setq name (intern nname)))
  8254. (eval
  8255. (let ((bindings '((org-heading-regexp
  8256. (concat "^"
  8257. orgstruct-heading-prefix-regexp
  8258. "\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ ]*$"))
  8259. (org-outline-regexp
  8260. (concat orgstruct-heading-prefix-regexp "\\*+ "))
  8261. (org-outline-regexp-bol
  8262. (concat "^" org-outline-regexp))
  8263. (outline-regexp org-outline-regexp)
  8264. (outline-heading-end-regexp "\n")
  8265. (outline-level 'org-outline-level)
  8266. (outline-heading-alist))))
  8267. `(defun ,name (arg)
  8268. ,(concat "In Structure, run `" (symbol-name fun) "'.\n"
  8269. "Outside of structure, run the binding of `"
  8270. (key-description key) "'."
  8271. (when disable-when-heading-prefix
  8272. (concat
  8273. "\nIf `orgstruct-heading-prefix-regexp' is not empty, this command will always fall\n"
  8274. "back to the default binding due to limitations of Org's implementation of\n"
  8275. "`" (symbol-name fun) "'.")))
  8276. (interactive "p")
  8277. (let* ((disable
  8278. ,(and disable-when-heading-prefix
  8279. '(not (string= orgstruct-heading-prefix-regexp ""))))
  8280. (fallback
  8281. (or disable
  8282. (not
  8283. (let* ,bindings
  8284. (org-context-p 'headline 'item
  8285. ,(when (memq fun
  8286. '(org-insert-heading
  8287. org-insert-heading-respect-content
  8288. org-meta-return))
  8289. '(when orgstruct-is-++
  8290. 'item-body))))))))
  8291. (if fallback
  8292. (let* ((orgstruct-mode)
  8293. (binding
  8294. (let ((key ,key))
  8295. (catch 'exit
  8296. (dolist
  8297. (rep
  8298. '(nil
  8299. ("<\\([^>]*\\)tab>" . "\\1TAB")
  8300. ("<\\([^>]*\\)return>" . "\\1RET")
  8301. ("<\\([^>]*\\)escape>" . "\\1ESC")
  8302. ("<\\([^>]*\\)delete>" . "\\1DEL"))
  8303. nil)
  8304. (when rep
  8305. (setq key (read-kbd-macro
  8306. (let ((case-fold-search))
  8307. (replace-regexp-in-string
  8308. (car rep)
  8309. (cdr rep)
  8310. (key-description key))))))
  8311. (when (key-binding key)
  8312. (throw 'exit (key-binding key))))))))
  8313. (if (keymapp binding)
  8314. (org-set-transient-map binding)
  8315. (let ((func (or binding
  8316. (unless disable
  8317. 'orgstruct-error))))
  8318. (when func
  8319. (call-interactively func)))))
  8320. (org-run-like-in-org-mode
  8321. (lambda ()
  8322. (interactive)
  8323. (let* ,bindings
  8324. (call-interactively ',fun)))))))))
  8325. name))
  8326. (defun org-contextualize-keys (alist contexts)
  8327. "Return valid elements in ALIST depending on CONTEXTS.
  8328. `org-agenda-custom-commands' or `org-capture-templates' are the
  8329. values used for ALIST, and `org-agenda-custom-commands-contexts'
  8330. or `org-capture-templates-contexts' are the associated contexts
  8331. definitions."
  8332. (let ((contexts
  8333. ;; normalize contexts
  8334. (mapcar
  8335. (lambda(c) (cond ((listp (cadr c))
  8336. (list (car c) (car c) (nth 1 c)))
  8337. ((string= "" (cadr c))
  8338. (list (car c) (car c) (nth 2 c)))
  8339. (t c)))
  8340. contexts))
  8341. (a alist) r s)
  8342. ;; loop over all commands or templates
  8343. (dolist (c a)
  8344. (let (vrules repl)
  8345. (cond
  8346. ((not (assoc (car c) contexts))
  8347. (push c r))
  8348. ((and (assoc (car c) contexts)
  8349. (setq vrules (org-contextualize-validate-key
  8350. (car c) contexts)))
  8351. (mapc (lambda (vr)
  8352. (unless (equal (car vr) (cadr vr))
  8353. (setq repl vr)))
  8354. vrules)
  8355. (if (not repl) (push c r)
  8356. (push (cadr repl) s)
  8357. (push
  8358. (cons (car c)
  8359. (cdr (or (assoc (cadr repl) alist)
  8360. (error "Undefined key `%s' as contextual replacement for `%s'"
  8361. (cadr repl) (car c)))))
  8362. r))))))
  8363. ;; Return limited ALIST, possibly with keys modified, and deduplicated
  8364. (delq
  8365. nil
  8366. (delete-dups
  8367. (mapcar (lambda (x)
  8368. (let ((tpl (car x)))
  8369. (unless (delq
  8370. nil
  8371. (mapcar (lambda (y)
  8372. (equal y tpl))
  8373. s))
  8374. x)))
  8375. (reverse r))))))
  8376. (defun org-contextualize-validate-key (key contexts)
  8377. "Check CONTEXTS for agenda or capture KEY."
  8378. (let (res)
  8379. (dolist (r contexts)
  8380. (dolist (rr (car (last r)))
  8381. (when
  8382. (and (equal key (car r))
  8383. (if (functionp rr) (funcall rr)
  8384. (or (and (eq (car rr) 'in-file)
  8385. (buffer-file-name)
  8386. (string-match (cdr rr) (buffer-file-name)))
  8387. (and (eq (car rr) 'in-mode)
  8388. (string-match (cdr rr) (symbol-name major-mode)))
  8389. (and (eq (car rr) 'in-buffer)
  8390. (string-match (cdr rr) (buffer-name)))
  8391. (when (and (eq (car rr) 'not-in-file)
  8392. (buffer-file-name))
  8393. (not (string-match (cdr rr) (buffer-file-name))))
  8394. (when (eq (car rr) 'not-in-mode)
  8395. (not (string-match (cdr rr) (symbol-name major-mode))))
  8396. (when (eq (car rr) 'not-in-buffer)
  8397. (not (string-match (cdr rr) (buffer-name)))))))
  8398. (push r res))))
  8399. (delete-dups (delq nil res))))
  8400. (defun org-context-p (&rest contexts)
  8401. "Check if local context is any of CONTEXTS.
  8402. Possible values in the list of contexts are `table', `headline', and `item'."
  8403. (let ((pos (point)))
  8404. (goto-char (point-at-bol))
  8405. (prog1 (or (and (memq 'table contexts)
  8406. (looking-at "[ \t]*|"))
  8407. (and (memq 'headline contexts)
  8408. (looking-at org-outline-regexp))
  8409. (and (memq 'item contexts)
  8410. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  8411. (and (memq 'item-body contexts)
  8412. (org-in-item-p)))
  8413. (goto-char pos))))
  8414. (defconst org-unique-local-variables
  8415. '(org-element--cache
  8416. org-element--cache-objects
  8417. org-element--cache-sync-keys
  8418. org-element--cache-sync-requests
  8419. org-element--cache-sync-timer)
  8420. "List of local variables that cannot be transferred to another buffer.")
  8421. (defun org-get-local-variables ()
  8422. "Return a list of all local variables in an Org mode buffer."
  8423. (delq nil
  8424. (mapcar
  8425. (lambda (x)
  8426. (let* ((binding (if (symbolp x) (list x) (list (car x) (cdr x))))
  8427. (name (car binding)))
  8428. (and (not (get name 'org-state))
  8429. (not (memq name org-unique-local-variables))
  8430. (string-match-p
  8431. "\\`\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|\
  8432. auto-fill\\|normal-auto-fill\\|fill-paragraph\\|indent-\\)"
  8433. (symbol-name name))
  8434. binding)))
  8435. (with-temp-buffer
  8436. (org-mode)
  8437. (buffer-local-variables)))))
  8438. (defun org-clone-local-variables (from-buffer &optional regexp)
  8439. "Clone local variables from FROM-BUFFER.
  8440. Optional argument REGEXP selects variables to clone."
  8441. (dolist (pair (buffer-local-variables from-buffer))
  8442. (let ((name (car pair)))
  8443. (when (and (symbolp name)
  8444. (not (memq name org-unique-local-variables))
  8445. (or (null regexp) (string-match regexp (symbol-name name))))
  8446. (set (make-local-variable name) (cdr pair))))))
  8447. ;;;###autoload
  8448. (defun org-run-like-in-org-mode (cmd)
  8449. "Run a command, pretending that the current buffer is in Org-mode.
  8450. This will temporarily bind local variables that are typically bound in
  8451. Org-mode to the values they have in Org-mode, and then interactively
  8452. call CMD."
  8453. (org-load-modules-maybe)
  8454. (unless org-local-vars
  8455. (setq org-local-vars (org-get-local-variables)))
  8456. (let (binds)
  8457. (dolist (var org-local-vars)
  8458. (when (or (not (boundp (car var)))
  8459. (eq (symbol-value (car var))
  8460. (default-value (car var))))
  8461. (push (list (car var) `(quote ,(cadr var))) binds)))
  8462. (eval `(let ,binds
  8463. (call-interactively (quote ,cmd))))))
  8464. (defun org-get-category (&optional pos force-refresh)
  8465. "Get the category applying to position POS."
  8466. (save-match-data
  8467. (when force-refresh (org-refresh-category-properties))
  8468. (let ((pos (or pos (point))))
  8469. (or (get-text-property pos 'org-category)
  8470. (progn (org-refresh-category-properties)
  8471. (get-text-property pos 'org-category))))))
  8472. ;;; Refresh properties
  8473. (defun org-refresh-properties (dprop tprop)
  8474. "Refresh buffer text properties.
  8475. DPROP is the drawer property and TPROP is either the
  8476. corresponding text property to set, or an alist with each element
  8477. being a text property (as a symbol) and a function to apply to
  8478. the value of the drawer property."
  8479. (let ((case-fold-search t)
  8480. (inhibit-read-only t))
  8481. (org-with-silent-modifications
  8482. (save-excursion
  8483. (save-restriction
  8484. (widen)
  8485. (goto-char (point-min))
  8486. (while (re-search-forward (concat "^[ \t]*:" dprop ": +\\(.*\\)[ \t]*$") nil t)
  8487. (org-refresh-property tprop (org-match-string-no-properties 1))))))))
  8488. (defun org-refresh-property (tprop p)
  8489. "Refresh the buffer text property TPROP from the drawer property P.
  8490. The refresh happens only for the current tree (not subtree)."
  8491. (unless (org-before-first-heading-p)
  8492. (save-excursion
  8493. (org-back-to-heading t)
  8494. (if (symbolp tprop)
  8495. ;; TPROP is a text property symbol
  8496. (put-text-property
  8497. (point) (or (outline-next-heading) (point-max)) tprop p)
  8498. ;; TPROP is an alist with (properties . function) elements
  8499. (dolist (al tprop)
  8500. (save-excursion
  8501. (put-text-property
  8502. (line-beginning-position) (or (outline-next-heading) (point-max))
  8503. (car al)
  8504. (funcall (cdr al) p))))))))
  8505. (defun org-refresh-category-properties ()
  8506. "Refresh category text properties in the buffer."
  8507. (let ((case-fold-search t)
  8508. (inhibit-read-only t)
  8509. (default-category
  8510. (cond ((null org-category)
  8511. (if buffer-file-name
  8512. (file-name-sans-extension
  8513. (file-name-nondirectory buffer-file-name))
  8514. "???"))
  8515. ((symbolp org-category) (symbol-name org-category))
  8516. (t org-category))))
  8517. (org-with-silent-modifications
  8518. (org-with-wide-buffer
  8519. ;; Set buffer-wide category. Search last #+CATEGORY keyword.
  8520. ;; This is the default category for the buffer. If none is
  8521. ;; found, fall-back to `org-category' or buffer file name.
  8522. (put-text-property
  8523. (point-min) (point-max)
  8524. 'org-category
  8525. (catch 'buffer-category
  8526. (goto-char (point-max))
  8527. (while (re-search-backward "^[ \t]*#\\+CATEGORY:" (point-min) t)
  8528. (let ((element (org-element-at-point)))
  8529. (when (eq (org-element-type element) 'keyword)
  8530. (throw 'buffer-category
  8531. (org-element-property :value element)))))
  8532. default-category))
  8533. ;; Set sub-tree specific categories.
  8534. (goto-char (point-min))
  8535. (let ((regexp (org-re-property "CATEGORY")))
  8536. (while (re-search-forward regexp nil t)
  8537. (let ((value (org-match-string-no-properties 3)))
  8538. (when (org-at-property-p)
  8539. (put-text-property
  8540. (save-excursion (org-back-to-heading t) (point))
  8541. (save-excursion (org-end-of-subtree t t) (point))
  8542. 'org-category
  8543. value)))))))))
  8544. (defun org-refresh-stats-properties ()
  8545. "Refresh stats text properties in the buffer."
  8546. (let (stats)
  8547. (org-with-silent-modifications
  8548. (save-excursion
  8549. (save-restriction
  8550. (widen)
  8551. (goto-char (point-min))
  8552. (while (re-search-forward
  8553. (concat org-outline-regexp-bol ".*"
  8554. "\\(?:\\[\\([0-9]+\\)%\\|\\([0-9]+\\)/\\([0-9]+\\)\\]\\)")
  8555. nil t)
  8556. (setq stats (cond ((equal (match-string 3) "0") 0)
  8557. ((match-string 2)
  8558. (/ (* (string-to-number (match-string 2)) 100)
  8559. (string-to-number (match-string 3))))
  8560. (t (string-to-number (match-string 1)))))
  8561. (org-back-to-heading t)
  8562. (put-text-property (point) (progn (org-end-of-subtree t t) (point))
  8563. 'org-stats stats)))))))
  8564. (defun org-refresh-effort-properties ()
  8565. "Refresh effort properties"
  8566. (org-refresh-properties
  8567. org-effort-property
  8568. '((effort . identity)
  8569. (effort-minutes . org-duration-string-to-minutes))))
  8570. ;;;; Link Stuff
  8571. ;;; Link abbreviations
  8572. (defun org-link-expand-abbrev (link)
  8573. "Apply replacements as defined in `org-link-abbrev-alist'."
  8574. (if (string-match "^\\([^:]*\\)\\(::?\\(.*\\)\\)?$" link)
  8575. (let* ((key (match-string 1 link))
  8576. (as (or (assoc key org-link-abbrev-alist-local)
  8577. (assoc key org-link-abbrev-alist)))
  8578. (tag (and (match-end 2) (match-string 3 link)))
  8579. rpl)
  8580. (if (not as)
  8581. link
  8582. (setq rpl (cdr as))
  8583. (cond
  8584. ((symbolp rpl) (funcall rpl tag))
  8585. ((string-match "%(\\([^)]+\\))" rpl)
  8586. (replace-match
  8587. (save-match-data
  8588. (funcall (intern-soft (match-string 1 rpl)) tag)) t t rpl))
  8589. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  8590. ((string-match "%h" rpl)
  8591. (replace-match (url-hexify-string (or tag "")) t t rpl))
  8592. (t (concat rpl tag)))))
  8593. link))
  8594. ;;; Storing and inserting links
  8595. (defvar org-insert-link-history nil
  8596. "Minibuffer history for links inserted with `org-insert-link'.")
  8597. (defvar org-stored-links nil
  8598. "Contains the links stored with `org-store-link'.")
  8599. (defvar org-store-link-plist nil
  8600. "Plist with info about the most recently link created with `org-store-link'.")
  8601. (defvar org-link-protocols nil
  8602. "Link protocols added to Org-mode using `org-add-link-type'.")
  8603. (defvar org-store-link-functions nil
  8604. "List of functions that are called to create and store a link.
  8605. Each function will be called in turn until one returns a non-nil
  8606. value. Each function should check if it is responsible for creating
  8607. this link (for example by looking at the major mode).
  8608. If not, it must exit and return nil.
  8609. If yes, it should return a non-nil value after a calling
  8610. `org-store-link-props' with a list of properties and values.
  8611. Special properties are:
  8612. :type The link prefix, like \"http\". This must be given.
  8613. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  8614. This is obligatory as well.
  8615. :description Optional default description for the second pair
  8616. of brackets in an Org-mode link. The user can still change
  8617. this when inserting this link into an Org-mode buffer.
  8618. In addition to these, any additional properties can be specified
  8619. and then used in capture templates.")
  8620. (defun org-add-link-type (type &optional follow export)
  8621. "Add TYPE to the list of `org-link-types'.
  8622. Re-compute all regular expressions depending on `org-link-types'
  8623. FOLLOW and EXPORT are two functions.
  8624. FOLLOW should take the link path as the single argument and do whatever
  8625. is necessary to follow the link, for example find a file or display
  8626. a mail message.
  8627. EXPORT should format the link path for export to one of the export formats.
  8628. It should be a function accepting three arguments:
  8629. path the path of the link, the text after the prefix (like \"http:\")
  8630. desc the description of the link, if any
  8631. format the export format, a symbol like `html' or `latex' or `ascii'.
  8632. The function may use the FORMAT information to return different values
  8633. depending on the format. The return value will be put literally into
  8634. the exported file. If the return value is nil, this means Org should
  8635. do what it normally does with links which do not have EXPORT defined.
  8636. Org mode has a built-in default for exporting links. If you are happy with
  8637. this default, there is no need to define an export function for the link
  8638. type. For a simple example of an export function, see `org-bbdb.el'."
  8639. (add-to-list 'org-link-types type t)
  8640. (org-make-link-regexps)
  8641. (org-element-update-syntax)
  8642. (if (assoc type org-link-protocols)
  8643. (setcdr (assoc type org-link-protocols) (list follow export))
  8644. (push (list type follow export) org-link-protocols)))
  8645. (defvar org-agenda-buffer-name) ; Defined in org-agenda.el
  8646. (defvar org-id-link-to-org-use-id) ; Defined in org-id.el
  8647. ;;;###autoload
  8648. (defun org-store-link (arg)
  8649. "\\<org-mode-map>Store an org-link to the current location.
  8650. This link is added to `org-stored-links' and can later be inserted
  8651. into an Org buffer with \\[org-insert-link].
  8652. For some link types, a prefix ARG is interpreted.
  8653. For links to Usenet articles, ARG negates `org-gnus-prefer-web-links'.
  8654. For file links, ARG negates `org-context-in-file-links'.
  8655. A double prefix ARG force skipping storing functions that are not
  8656. part of Org's core.
  8657. A triple prefix ARG force storing a link for each line in the
  8658. active region."
  8659. (interactive "P")
  8660. (org-load-modules-maybe)
  8661. (if (and (equal arg '(64)) (org-region-active-p))
  8662. (save-excursion
  8663. (let ((end (region-end)))
  8664. (goto-char (region-beginning))
  8665. (set-mark (point))
  8666. (while (< (point-at-eol) end)
  8667. (move-end-of-line 1) (activate-mark)
  8668. (let (current-prefix-arg)
  8669. (call-interactively 'org-store-link))
  8670. (move-beginning-of-line 2)
  8671. (set-mark (point)))))
  8672. (org-with-limited-levels
  8673. (setq org-store-link-plist nil)
  8674. (let (link cpltxt desc description search
  8675. txt custom-id agenda-link sfuns sfunsn)
  8676. (cond
  8677. ;; Store a link using an external link type
  8678. ((and (not (equal arg '(16)))
  8679. (setq sfuns
  8680. (delq
  8681. nil (mapcar (lambda (f)
  8682. (let (fs) (if (funcall f) (push f fs))))
  8683. org-store-link-functions))
  8684. sfunsn (mapcar (lambda (fu) (symbol-name (car fu))) sfuns))
  8685. (or (and (cdr sfuns)
  8686. (funcall (intern
  8687. (completing-read
  8688. "Which function for creating the link? "
  8689. sfunsn nil t (car sfunsn)))))
  8690. (funcall (caar sfuns)))
  8691. (setq link (plist-get org-store-link-plist :link)
  8692. desc (or (plist-get org-store-link-plist
  8693. :description)
  8694. link))))
  8695. ;; Store a link from a source code buffer.
  8696. ((org-src-edit-buffer-p)
  8697. (cond
  8698. ((save-excursion
  8699. (beginning-of-line)
  8700. (looking-at (concat (format org-coderef-label-format "\\(.*?\\)")
  8701. "[ \t]*$")))
  8702. (setq link (format "(%s)" (org-match-string-no-properties 1))))
  8703. ((org-called-interactively-p 'any)
  8704. (let (label)
  8705. (while (or (not label)
  8706. (org-with-wide-buffer
  8707. (goto-char (point-min))
  8708. (re-search-forward
  8709. (regexp-quote (format org-coderef-label-format label))
  8710. nil t)))
  8711. (when label (message "Label exists already") (sit-for 2))
  8712. (setq label (read-string "Code line label: " label)))
  8713. (end-of-line)
  8714. (setq link (format org-coderef-label-format label))
  8715. (let ((gc (- 79 (length link))))
  8716. (if (< (current-column) gc) (org-move-to-column gc t)
  8717. (insert " ")))
  8718. (insert link)
  8719. (setq link (concat "(" label ")") desc nil)))
  8720. (t (setq link nil))))
  8721. ;; We are in the agenda, link to referenced location
  8722. ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
  8723. (let ((m (or (get-text-property (point) 'org-hd-marker)
  8724. (get-text-property (point) 'org-marker))))
  8725. (when m
  8726. (org-with-point-at m
  8727. (setq agenda-link
  8728. (if (org-called-interactively-p 'any)
  8729. (call-interactively 'org-store-link)
  8730. (org-store-link nil)))))))
  8731. ((eq major-mode 'calendar-mode)
  8732. (let ((cd (calendar-cursor-to-date)))
  8733. (setq link
  8734. (format-time-string
  8735. (car org-time-stamp-formats)
  8736. (apply 'encode-time
  8737. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  8738. nil nil nil))))
  8739. (org-store-link-props :type "calendar" :date cd)))
  8740. ((eq major-mode 'help-mode)
  8741. (setq link (concat "help:" (save-excursion
  8742. (goto-char (point-min))
  8743. (looking-at "^[^ ]+")
  8744. (match-string 0))))
  8745. (org-store-link-props :type "help"))
  8746. ((eq major-mode 'w3-mode)
  8747. (setq cpltxt (if (and (buffer-name)
  8748. (not (string-match "Untitled" (buffer-name))))
  8749. (buffer-name)
  8750. (url-view-url t))
  8751. link (url-view-url t))
  8752. (org-store-link-props :type "w3" :url (url-view-url t)))
  8753. ((eq major-mode 'image-mode)
  8754. (setq cpltxt (concat "file:"
  8755. (abbreviate-file-name buffer-file-name))
  8756. link cpltxt)
  8757. (org-store-link-props :type "image" :file buffer-file-name))
  8758. ;; In dired, store a link to the file of the current line
  8759. ((derived-mode-p 'dired-mode)
  8760. (let ((file (dired-get-filename nil t)))
  8761. (setq file (if file
  8762. (abbreviate-file-name
  8763. (expand-file-name (dired-get-filename nil t)))
  8764. ;; otherwise, no file so use current directory.
  8765. default-directory))
  8766. (setq cpltxt (concat "file:" file)
  8767. link cpltxt)))
  8768. ((setq search (run-hook-with-args-until-success
  8769. 'org-create-file-search-functions))
  8770. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  8771. "::" search))
  8772. (setq cpltxt (or description link)))
  8773. ((and (buffer-file-name (buffer-base-buffer)) (derived-mode-p 'org-mode))
  8774. (setq custom-id (org-entry-get nil "CUSTOM_ID"))
  8775. (cond
  8776. ;; Store a link using the target at point
  8777. ((org-in-regexp "[^<]<<\\([^<>]+\\)>>[^>]" 1)
  8778. (setq cpltxt
  8779. (concat "file:"
  8780. (abbreviate-file-name
  8781. (buffer-file-name (buffer-base-buffer)))
  8782. "::" (match-string 1))
  8783. link cpltxt))
  8784. ((and (featurep 'org-id)
  8785. (or (eq org-id-link-to-org-use-id t)
  8786. (and (org-called-interactively-p 'any)
  8787. (or (eq org-id-link-to-org-use-id 'create-if-interactive)
  8788. (and (eq org-id-link-to-org-use-id
  8789. 'create-if-interactive-and-no-custom-id)
  8790. (not custom-id))))
  8791. (and org-id-link-to-org-use-id (org-entry-get nil "ID"))))
  8792. ;; Store a link using the ID at point
  8793. (setq link (condition-case nil
  8794. (prog1 (org-id-store-link)
  8795. (setq desc (or (plist-get org-store-link-plist
  8796. :description)
  8797. "")))
  8798. (error
  8799. ;; Probably before first headline, link only to file
  8800. (concat "file:"
  8801. (abbreviate-file-name
  8802. (buffer-file-name (buffer-base-buffer))))))))
  8803. (t
  8804. ;; Just link to current headline
  8805. (setq cpltxt (concat "file:"
  8806. (abbreviate-file-name
  8807. (buffer-file-name (buffer-base-buffer)))))
  8808. ;; Add a context search string
  8809. (when (org-xor org-context-in-file-links arg)
  8810. (let* ((ee (org-element-at-point))
  8811. (et (org-element-type ee))
  8812. (ev (plist-get (cadr ee) :value))
  8813. (ek (plist-get (cadr ee) :key))
  8814. (eok (and (stringp ek) (string-match "name" ek))))
  8815. (setq txt (cond
  8816. ((org-at-heading-p) nil)
  8817. ((and (eq et 'keyword) eok) ev)
  8818. ((org-region-active-p)
  8819. (buffer-substring (region-beginning) (region-end)))))
  8820. (when (or (null txt) (string-match "\\S-" txt))
  8821. (setq cpltxt
  8822. (concat cpltxt "::"
  8823. (condition-case nil
  8824. (org-make-org-heading-search-string txt)
  8825. (error "")))
  8826. desc (or (and (eq et 'keyword) eok ev)
  8827. (nth 4 (ignore-errors (org-heading-components)))
  8828. "NONE")))))
  8829. (when (string-match "::\\'" cpltxt)
  8830. (setq cpltxt (substring cpltxt 0 -2)))
  8831. (setq link cpltxt))))
  8832. ((buffer-file-name (buffer-base-buffer))
  8833. ;; Just link to this file here.
  8834. (setq cpltxt (concat "file:"
  8835. (abbreviate-file-name
  8836. (buffer-file-name (buffer-base-buffer)))))
  8837. ;; Add a context string.
  8838. (when (org-xor org-context-in-file-links arg)
  8839. (setq txt (if (org-region-active-p)
  8840. (buffer-substring (region-beginning) (region-end))
  8841. (buffer-substring (point-at-bol) (point-at-eol))))
  8842. ;; Only use search option if there is some text.
  8843. (when (string-match "\\S-" txt)
  8844. (setq cpltxt
  8845. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  8846. desc "NONE")))
  8847. (setq link cpltxt))
  8848. ((org-called-interactively-p 'interactive)
  8849. (user-error "No method for storing a link from this buffer"))
  8850. (t (setq link nil)))
  8851. ;; We're done setting link and desc, clean up
  8852. (when (consp link) (setq cpltxt (car link) link (cdr link)))
  8853. (setq link (or link cpltxt)
  8854. desc (or desc cpltxt))
  8855. (cond ((equal desc "NONE") (setq desc nil))
  8856. ((and desc (string-match org-bracket-link-analytic-regexp desc))
  8857. (let ((d0 (match-string 3 desc))
  8858. (p0 (match-string 5 desc)))
  8859. (setq desc
  8860. (replace-regexp-in-string
  8861. org-bracket-link-regexp
  8862. (concat (or p0 d0)
  8863. (if (equal (length (match-string 0 desc))
  8864. (length desc)) "*" "")) desc)))))
  8865. ;; Return the link
  8866. (if (not (and (or (org-called-interactively-p 'any)
  8867. executing-kbd-macro)
  8868. link))
  8869. (or agenda-link (and link (org-make-link-string link desc)))
  8870. (push (list link desc) org-stored-links)
  8871. (message "Stored: %s" (or desc link))
  8872. (when custom-id
  8873. (setq link (concat "file:" (abbreviate-file-name
  8874. (buffer-file-name)) "::#" custom-id))
  8875. (push (list link desc) org-stored-links))
  8876. (car org-stored-links))))))
  8877. (defun org-store-link-props (&rest plist)
  8878. "Store link properties, extract names, addresses and dates."
  8879. (let ((x (plist-get plist :from)))
  8880. (when x
  8881. (let ((adr (mail-extract-address-components x)))
  8882. (setq plist (plist-put plist :fromname (car adr)))
  8883. (setq plist (plist-put plist :fromaddress (nth 1 adr))))))
  8884. (let ((x (plist-get plist :to)))
  8885. (when x
  8886. (let ((adr (mail-extract-address-components x)))
  8887. (setq plist (plist-put plist :toname (car adr)))
  8888. (setq plist (plist-put plist :toaddress (nth 1 adr))))))
  8889. (let ((x (ignore-errors (date-to-time (plist-get plist :date)))))
  8890. (when x
  8891. (setq plist (plist-put plist :date-timestamp
  8892. (format-time-string
  8893. (org-time-stamp-format t) x)))
  8894. (setq plist (plist-put plist :date-timestamp-inactive
  8895. (format-time-string
  8896. (org-time-stamp-format t t) x)))))
  8897. (let ((from (plist-get plist :from))
  8898. (to (plist-get plist :to)))
  8899. (when (and from to org-from-is-user-regexp)
  8900. (setq plist
  8901. (plist-put plist :fromto
  8902. (if (string-match org-from-is-user-regexp from)
  8903. (concat "to %t")
  8904. (concat "from %f"))))))
  8905. (setq org-store-link-plist plist))
  8906. (defun org-add-link-props (&rest plist)
  8907. "Add these properties to the link property list."
  8908. (let (key value)
  8909. (while plist
  8910. (setq key (pop plist) value (pop plist))
  8911. (setq org-store-link-plist
  8912. (plist-put org-store-link-plist key value)))))
  8913. (defun org-email-link-description (&optional fmt)
  8914. "Return the description part of an email link.
  8915. This takes information from `org-store-link-plist' and formats it
  8916. according to FMT (default from `org-email-link-description-format')."
  8917. (setq fmt (or fmt org-email-link-description-format))
  8918. (let* ((p org-store-link-plist)
  8919. (to (plist-get p :toaddress))
  8920. (from (plist-get p :fromaddress))
  8921. (table
  8922. (list
  8923. (cons "%c" (plist-get p :fromto))
  8924. (cons "%F" (plist-get p :from))
  8925. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  8926. (cons "%T" (plist-get p :to))
  8927. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  8928. (cons "%s" (plist-get p :subject))
  8929. (cons "%d" (plist-get p :date))
  8930. (cons "%m" (plist-get p :message-id)))))
  8931. (when (string-match "%c" fmt)
  8932. ;; Check if the user wrote this message
  8933. (if (and org-from-is-user-regexp from to
  8934. (save-match-data (string-match org-from-is-user-regexp from)))
  8935. (setq fmt (replace-match "to %t" t t fmt))
  8936. (setq fmt (replace-match "from %f" t t fmt))))
  8937. (org-replace-escapes fmt table)))
  8938. (defun org-make-org-heading-search-string (&optional string)
  8939. "Make search string for the current headline or STRING."
  8940. (let ((s (or string
  8941. (and (derived-mode-p 'org-mode)
  8942. (save-excursion
  8943. (org-back-to-heading t)
  8944. (org-element-property :raw-value (org-element-at-point))))))
  8945. (lines org-context-in-file-links))
  8946. (or string (setq s (concat "*" s))) ; Add * for headlines
  8947. (setq s (replace-regexp-in-string "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" "" s))
  8948. (when (and string (integerp lines) (> lines 0))
  8949. (let ((slines (org-split-string s "\n")))
  8950. (when (< lines (length slines))
  8951. (setq s (mapconcat
  8952. 'identity
  8953. (reverse (nthcdr (- (length slines) lines)
  8954. (reverse slines))) "\n")))))
  8955. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  8956. (defun org-make-link-string (link &optional description)
  8957. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  8958. (unless (org-string-nw-p link) (error "Empty link"))
  8959. (let ((uri (cond ((string-match org-link-types-re link)
  8960. (concat (match-string 1 link)
  8961. (org-link-escape (substring link (match-end 1)))))
  8962. ;; For readability, url-encode internal links only
  8963. ;; when absolutely needed (i.e, when they contain
  8964. ;; square brackets). File links however, are
  8965. ;; encoded since, e.g., spaces are significant.
  8966. ((or (file-name-absolute-p link)
  8967. (org-string-match-p "\\`\\.\\.?/\\|[][]" link))
  8968. (org-link-escape link))
  8969. (t link)))
  8970. (description
  8971. (and (org-string-nw-p description)
  8972. ;; Remove brackets from description, as they are fatal.
  8973. (replace-regexp-in-string
  8974. "[][]" (lambda (m) (if (equal "[" m) "{" "}"))
  8975. (org-trim description)))))
  8976. (format "[[%s]%s]"
  8977. uri
  8978. (if description (format "[%s]" description) ""))))
  8979. (defconst org-link-escape-chars
  8980. ;;%20 %5B %5D %25
  8981. '(?\s ?\[ ?\] ?%)
  8982. "List of characters that should be escaped in a link when stored to Org.
  8983. This is the list that is used for internal purposes.")
  8984. (defconst org-link-escape-chars-browser
  8985. ;;%20 %22
  8986. '(?\s ?\")
  8987. "List of characters to be escaped before handing over to the browser.
  8988. If you consider using this constant then you probably want to use
  8989. the function `org-link-escape-browser' instead. See there why
  8990. this constant is a candidate to be removed once Org drops support
  8991. for Emacs 24.1 and 24.2.")
  8992. (defun org-link-escape (text &optional table merge)
  8993. "Return percent escaped representation of TEXT.
  8994. TEXT is a string with the text to escape.
  8995. Optional argument TABLE is a list with characters that should be
  8996. escaped. When nil, `org-link-escape-chars' is used.
  8997. If optional argument MERGE is set, merge TABLE into
  8998. `org-link-escape-chars'."
  8999. (let ((characters-to-encode
  9000. (cond ((null table) org-link-escape-chars)
  9001. (merge (append org-link-escape-chars table))
  9002. (t table))))
  9003. (mapconcat
  9004. (lambda (c)
  9005. (if (or (memq c characters-to-encode)
  9006. (and org-url-hexify-p (or (< c 32) (> c 126))))
  9007. (mapconcat (lambda (e) (format "%%%.2X" e))
  9008. (or (encode-coding-char c 'utf-8)
  9009. (error "Unable to percent escape character: %c" c))
  9010. "")
  9011. (char-to-string c)))
  9012. text "")))
  9013. (defun org-link-escape-browser (text)
  9014. "Escape some characters before handing over to the browser.
  9015. This function is a candidate to be removed together with the
  9016. constant `org-link-escape-chars-browser' once Org drops support
  9017. for Emacs 24.1 and 24.2. All calls to this function will have to
  9018. be replaced with `url-encode-url' which is available since Emacs
  9019. 24.3.1."
  9020. ;; Example with the Org link
  9021. ;; [[http://lists.gnu.org/archive/cgi-bin/namazu.cgi?idxname=emacs-orgmode&query=%252Bsubject:"Release+8.2"]]
  9022. ;; to open the browser with +subject:"Release 8.2" filled into the
  9023. ;; query field: In this case the variable TEXT contains the
  9024. ;; unescaped [...]=%2Bsubject:"Release+8.2". Then `url-encode-url'
  9025. ;; converts correctly to [...]=%2Bsubject:%22Release+8.2%22 or
  9026. ;; `org-link-escape' with `org-link-escape-chars-browser' converts
  9027. ;; wrongly to [...]=%252Bsubject:%22Release+8.2%22.
  9028. (if (fboundp 'url-encode-url)
  9029. (url-encode-url text)
  9030. (if (org-string-match-p
  9031. (concat "[[:nonascii:]" org-link-escape-chars-browser "]")
  9032. text)
  9033. (org-link-escape text org-link-escape-chars-browser)
  9034. text)))
  9035. (defun org-link-unescape (str)
  9036. "Unhex hexified Unicode parts in string STR.
  9037. E.g. `%C3%B6' becomes the german o-Umlaut. This is the
  9038. reciprocal of `org-link-escape', which see."
  9039. (if (org-string-nw-p str)
  9040. (replace-regexp-in-string
  9041. "\\(%[0-9A-Za-z]\\{2\\}\\)+" #'org-link-unescape-compound str t t)
  9042. str))
  9043. (defun org-link-unescape-compound (hex)
  9044. "Unhexify Unicode hex-chars. E.g. `%C3%B6' is the German o-Umlaut.
  9045. Note: this function also decodes single byte encodings like
  9046. `%E1' (a-acute) if not followed by another `%[A-F0-9]{2}' group."
  9047. (save-match-data
  9048. (let* ((bytes (cdr (split-string hex "%")))
  9049. (ret "")
  9050. (eat 0)
  9051. (sum 0))
  9052. (while bytes
  9053. (let* ((val (string-to-number (pop bytes) 16))
  9054. (shift-xor
  9055. (if (= 0 eat)
  9056. (cond
  9057. ((>= val 252) (cons 6 252))
  9058. ((>= val 248) (cons 5 248))
  9059. ((>= val 240) (cons 4 240))
  9060. ((>= val 224) (cons 3 224))
  9061. ((>= val 192) (cons 2 192))
  9062. (t (cons 0 0)))
  9063. (cons 6 128))))
  9064. (when (>= val 192) (setq eat (car shift-xor)))
  9065. (setq val (logxor val (cdr shift-xor)))
  9066. (setq sum (+ (lsh sum (car shift-xor)) val))
  9067. (when (> eat 0) (setq eat (- eat 1)))
  9068. (cond
  9069. ((= 0 eat) ;multi byte
  9070. (setq ret (concat ret (org-char-to-string sum)))
  9071. (setq sum 0))
  9072. ((not bytes) ; single byte(s)
  9073. (setq ret (org-link-unescape-single-byte-sequence hex))))))
  9074. ret)))
  9075. (defun org-link-unescape-single-byte-sequence (hex)
  9076. "Unhexify hex-encoded single byte character sequences."
  9077. (mapconcat (lambda (byte)
  9078. (char-to-string (string-to-number byte 16)))
  9079. (cdr (split-string hex "%")) ""))
  9080. (defun org-xor (a b)
  9081. "Exclusive or."
  9082. (if a (not b) b))
  9083. (defun org-fixup-message-id-for-http (s)
  9084. "Replace special characters in a message id, so it can be used in an http query."
  9085. (when (string-match "%" s)
  9086. (setq s (mapconcat (lambda (c)
  9087. (if (eq c ?%)
  9088. "%25"
  9089. (char-to-string c)))
  9090. s "")))
  9091. (while (string-match "<" s)
  9092. (setq s (replace-match "%3C" t t s)))
  9093. (while (string-match ">" s)
  9094. (setq s (replace-match "%3E" t t s)))
  9095. (while (string-match "@" s)
  9096. (setq s (replace-match "%40" t t s)))
  9097. s)
  9098. (defun org-link-prettify (link)
  9099. "Return a human-readable representation of LINK.
  9100. The car of LINK must be a raw link.
  9101. The cdr of LINK must be either a link description or nil."
  9102. (let ((desc (or (cadr link) "<no description>")))
  9103. (concat (format "%-45s" (substring desc 0 (min (length desc) 40)))
  9104. "<" (car link) ">")))
  9105. ;;;###autoload
  9106. (defun org-insert-link-global ()
  9107. "Insert a link like Org-mode does.
  9108. This command can be called in any mode to insert a link in Org-mode syntax."
  9109. (interactive)
  9110. (org-load-modules-maybe)
  9111. (org-run-like-in-org-mode 'org-insert-link))
  9112. (defun org-insert-all-links (arg &optional pre post)
  9113. "Insert all links in `org-stored-links'.
  9114. When a universal prefix, do not delete the links from `org-stored-links'.
  9115. When `ARG' is a number, insert the last N link(s).
  9116. `PRE' and `POST' are optional arguments to define a string to
  9117. prepend or to append."
  9118. (interactive "P")
  9119. (let ((org-keep-stored-link-after-insertion (equal arg '(4)))
  9120. (links (copy-sequence org-stored-links))
  9121. (pr (or pre "- "))
  9122. (po (or post "\n"))
  9123. (cnt 1) l)
  9124. (if (null org-stored-links)
  9125. (message "No link to insert")
  9126. (while (and (or (listp arg) (>= arg cnt))
  9127. (setq l (if (listp arg)
  9128. (pop links)
  9129. (pop org-stored-links))))
  9130. (setq cnt (1+ cnt))
  9131. (insert pr)
  9132. (org-insert-link nil (car l) (or (cadr l) "<no description>"))
  9133. (insert po)))))
  9134. (defun org-insert-last-stored-link (arg)
  9135. "Insert the last link stored in `org-stored-links'."
  9136. (interactive "p")
  9137. (org-insert-all-links arg "" "\n"))
  9138. (defun org-link-fontify-links-to-this-file ()
  9139. "Fontify links to the current file in `org-stored-links'."
  9140. (let ((f (buffer-file-name)) a b)
  9141. (setq a (mapcar (lambda(l)
  9142. (let ((ll (car l)))
  9143. (when (and (string-match "^file:\\(.+\\)::" ll)
  9144. (equal f (expand-file-name (match-string 1 ll))))
  9145. ll)))
  9146. org-stored-links))
  9147. (when (featurep 'org-id)
  9148. (setq b (mapcar (lambda(l)
  9149. (let ((ll (car l)))
  9150. (when (and (string-match "^id:\\(.+\\)$" ll)
  9151. (equal f (expand-file-name
  9152. (or (org-id-find-id-file
  9153. (match-string 1 ll)) ""))))
  9154. ll)))
  9155. org-stored-links)))
  9156. (mapcar (lambda(l)
  9157. (put-text-property 0 (length l) 'face 'font-lock-comment-face l))
  9158. (delq nil (append a b)))))
  9159. (defvar org--links-history nil)
  9160. (defun org-insert-link (&optional complete-file link-location default-description)
  9161. "Insert a link. At the prompt, enter the link.
  9162. Completion can be used to insert any of the link protocol prefixes like
  9163. http or ftp in use.
  9164. The history can be used to select a link previously stored with
  9165. `org-store-link'. When the empty string is entered (i.e. if you just
  9166. press RET at the prompt), the link defaults to the most recently
  9167. stored link. As SPC triggers completion in the minibuffer, you need to
  9168. use M-SPC or C-q SPC to force the insertion of a space character.
  9169. You will also be prompted for a description, and if one is given, it will
  9170. be displayed in the buffer instead of the link.
  9171. If there is already a link at point, this command will allow you to edit link
  9172. and description parts.
  9173. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  9174. be selected using completion. The path to the file will be relative to the
  9175. current directory if the file is in the current directory or a subdirectory.
  9176. Otherwise, the link will be the absolute path as completed in the minibuffer
  9177. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  9178. option `org-link-file-path-type'.
  9179. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  9180. the current directory or below.
  9181. With three \\[universal-argument] prefixes, negate the meaning of
  9182. `org-keep-stored-link-after-insertion'.
  9183. If `org-make-link-description-function' is non-nil, this function will be
  9184. called with the link target, and the result will be the default
  9185. link description.
  9186. If the LINK-LOCATION parameter is non-nil, this value will be
  9187. used as the link location instead of reading one interactively.
  9188. If the DEFAULT-DESCRIPTION parameter is non-nil, this value will
  9189. be used as the default description."
  9190. (interactive "P")
  9191. (let* ((wcf (current-window-configuration))
  9192. (origbuf (current-buffer))
  9193. (region (when (org-region-active-p)
  9194. (buffer-substring (region-beginning) (region-end))))
  9195. (remove (and region (list (region-beginning) (region-end))))
  9196. (desc region)
  9197. (link link-location)
  9198. (abbrevs org-link-abbrev-alist-local)
  9199. entry all-prefixes auto-desc)
  9200. (cond
  9201. (link-location) ; specified by arg, just use it.
  9202. ((org-in-regexp org-bracket-link-regexp 1)
  9203. ;; We do have a link at point, and we are going to edit it.
  9204. (setq remove (list (match-beginning 0) (match-end 0)))
  9205. (setq desc (when (match-end 3) (org-match-string-no-properties 3)))
  9206. (setq link (read-string "Link: "
  9207. (org-link-unescape
  9208. (org-match-string-no-properties 1)))))
  9209. ((or (org-in-regexp org-angle-link-re)
  9210. (org-in-regexp org-plain-link-re))
  9211. ;; Convert to bracket link
  9212. (setq remove (list (match-beginning 0) (match-end 0))
  9213. link (read-string "Link: "
  9214. (org-remove-angle-brackets (match-string 0)))))
  9215. ((member complete-file '((4) (16)))
  9216. ;; Completing read for file names.
  9217. (setq link (org-file-complete-link complete-file)))
  9218. (t
  9219. ;; Read link, with completion for stored links.
  9220. (org-link-fontify-links-to-this-file)
  9221. (org-switch-to-buffer-other-window "*Org Links*")
  9222. (with-current-buffer "*Org Links*"
  9223. (erase-buffer)
  9224. (insert "Insert a link.
  9225. Use TAB to complete link prefixes, then RET for type-specific completion support\n")
  9226. (when org-stored-links
  9227. (insert "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  9228. (insert (mapconcat 'org-link-prettify
  9229. (reverse org-stored-links) "\n")))
  9230. (goto-char (point-min)))
  9231. (let ((cw (selected-window)))
  9232. (select-window (get-buffer-window "*Org Links*" 'visible))
  9233. (with-current-buffer "*Org Links*" (setq truncate-lines t))
  9234. (unless (pos-visible-in-window-p (point-max))
  9235. (org-fit-window-to-buffer))
  9236. (and (window-live-p cw) (select-window cw)))
  9237. (setq all-prefixes (append (mapcar 'car abbrevs)
  9238. (mapcar 'car org-link-abbrev-alist)
  9239. org-link-types))
  9240. (unwind-protect
  9241. ;; Fake a link history, containing the stored links.
  9242. (let ((org--links-history
  9243. (append (mapcar #'car org-stored-links)
  9244. org-insert-link-history)))
  9245. (setq link
  9246. (org-completing-read
  9247. "Link: "
  9248. (append
  9249. (mapcar (lambda (x) (concat x ":")) all-prefixes)
  9250. (mapcar #'car org-stored-links))
  9251. nil nil nil
  9252. 'org--links-history
  9253. (caar org-stored-links)))
  9254. (unless (org-string-nw-p link) (user-error "No link selected"))
  9255. (dolist (l org-stored-links)
  9256. (when (equal link (cadr l))
  9257. (setq link (car l))
  9258. (setq auto-desc t)))
  9259. (when (or (member link all-prefixes)
  9260. (and (equal ":" (substring link -1))
  9261. (member (substring link 0 -1) all-prefixes)
  9262. (setq link (substring link 0 -1))))
  9263. (setq link (with-current-buffer origbuf
  9264. (org-link-try-special-completion link)))))
  9265. (set-window-configuration wcf)
  9266. (kill-buffer "*Org Links*"))
  9267. (setq entry (assoc link org-stored-links))
  9268. (or entry (push link org-insert-link-history))
  9269. (setq desc (or desc (nth 1 entry)))))
  9270. (when (funcall (if (equal complete-file '(64)) 'not 'identity)
  9271. (not org-keep-stored-link-after-insertion))
  9272. (setq org-stored-links (delq (assoc link org-stored-links)
  9273. org-stored-links)))
  9274. (when (and (string-match org-plain-link-re link)
  9275. (not (string-match org-ts-regexp link)))
  9276. ;; URL-like link, normalize the use of angular brackets.
  9277. (setq link (org-remove-angle-brackets link)))
  9278. ;; Check if we are linking to the current file with a search
  9279. ;; option If yes, simplify the link by using only the search
  9280. ;; option.
  9281. (when (and buffer-file-name
  9282. (string-match "^file:\\(.+?\\)::\\(.+\\)" link))
  9283. (let* ((path (match-string 1 link))
  9284. (case-fold-search nil)
  9285. (search (match-string 2 link)))
  9286. (save-match-data
  9287. (when (equal (file-truename buffer-file-name) (file-truename path))
  9288. ;; We are linking to this same file, with a search option
  9289. (setq link search)))))
  9290. ;; Check if we can/should use a relative path. If yes, simplify the link
  9291. (when (string-match "^\\(file:\\|docview:\\)\\(.*\\)" link)
  9292. (let* ((type (match-string 1 link))
  9293. (path (match-string 2 link))
  9294. (origpath path)
  9295. (case-fold-search nil))
  9296. (cond
  9297. ((or (eq org-link-file-path-type 'absolute)
  9298. (equal complete-file '(16)))
  9299. (setq path (abbreviate-file-name (expand-file-name path))))
  9300. ((eq org-link-file-path-type 'noabbrev)
  9301. (setq path (expand-file-name path)))
  9302. ((eq org-link-file-path-type 'relative)
  9303. (setq path (file-relative-name path)))
  9304. (t
  9305. (save-match-data
  9306. (if (string-match (concat "^" (regexp-quote
  9307. (expand-file-name
  9308. (file-name-as-directory
  9309. default-directory))))
  9310. (expand-file-name path))
  9311. ;; We are linking a file with relative path name.
  9312. (setq path (substring (expand-file-name path)
  9313. (match-end 0)))
  9314. (setq path (abbreviate-file-name (expand-file-name path)))))))
  9315. (setq link (concat type path))
  9316. (when (equal desc origpath)
  9317. (setq desc path))))
  9318. (if org-make-link-description-function
  9319. (setq desc
  9320. (or (condition-case nil
  9321. (funcall org-make-link-description-function link desc)
  9322. (error (progn (message "Can't get link description from `%s'"
  9323. (symbol-name org-make-link-description-function))
  9324. (sit-for 2) nil)))
  9325. (read-string "Description: " default-description)))
  9326. (if default-description (setq desc default-description)
  9327. (setq desc (or (and auto-desc desc)
  9328. (read-string "Description: " desc)))))
  9329. (unless (string-match "\\S-" desc) (setq desc nil))
  9330. (when remove (apply 'delete-region remove))
  9331. (insert (org-make-link-string link desc))
  9332. ;; Redisplay so as the new link has proper invisible characters.
  9333. (sit-for 0)))
  9334. (defun org-link-try-special-completion (type)
  9335. "If there is completion support for link type TYPE, offer it."
  9336. (let ((fun (intern (concat "org-" type "-complete-link"))))
  9337. (if (functionp fun)
  9338. (funcall fun)
  9339. (read-string "Link (no completion support): " (concat type ":")))))
  9340. (defun org-file-complete-link (&optional arg)
  9341. "Create a file link using completion."
  9342. (let ((file (read-file-name "File: "))
  9343. (pwd (file-name-as-directory (expand-file-name ".")))
  9344. (pwd1 (file-name-as-directory (abbreviate-file-name
  9345. (expand-file-name ".")))))
  9346. (cond ((equal arg '(16))
  9347. (concat "file:"
  9348. (abbreviate-file-name (expand-file-name file))))
  9349. ((string-match
  9350. (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  9351. (concat "file:" (match-string 1 file)))
  9352. ((string-match
  9353. (concat "^" (regexp-quote pwd) "\\(.+\\)")
  9354. (expand-file-name file))
  9355. (concat "file:"
  9356. (match-string 1 (expand-file-name file))))
  9357. (t (concat "file:" file)))))
  9358. (defun org-completing-read (&rest args)
  9359. "Completing-read with SPACE being a normal character."
  9360. (let ((enable-recursive-minibuffers t)
  9361. (minibuffer-local-completion-map
  9362. (copy-keymap minibuffer-local-completion-map)))
  9363. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  9364. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  9365. (org-defkey minibuffer-local-completion-map (kbd "C-c !")
  9366. 'org-time-stamp-inactive)
  9367. (apply #'completing-read args)))
  9368. (define-obsolete-function-alias
  9369. 'org-completing-read-no-i 'completing-read "Org 9.0")
  9370. (define-obsolete-function-alias
  9371. 'org-icompleting-read 'completing-read "Org 9.0")
  9372. (define-obsolete-function-alias
  9373. 'org-iread-file-name 'read-file-name "Org 9.0")
  9374. ;;; Opening/following a link
  9375. (defvar org-link-search-failed nil)
  9376. (defvar org-open-link-functions nil
  9377. "Hook for functions finding a plain text link.
  9378. These functions must take a single argument, the link content.
  9379. They will be called for links that look like [[link text][description]]
  9380. when LINK TEXT does not have a protocol like \"http:\" and does not look
  9381. like a filename (e.g. \"./blue.png\").
  9382. These functions will be called *before* Org attempts to resolve the
  9383. link by doing text searches in the current buffer - so if you want a
  9384. link \"[[target]]\" to still find \"<<target>>\", your function should
  9385. handle this as a special case.
  9386. When the function does handle the link, it must return a non-nil value.
  9387. If it decides that it is not responsible for this link, it must return
  9388. nil to indicate that that Org-mode can continue with other options
  9389. like exact and fuzzy text search.")
  9390. (defun org-next-link (&optional search-backward)
  9391. "Move forward to the next link.
  9392. If the link is in hidden text, expose it."
  9393. (interactive "P")
  9394. (when (and org-link-search-failed (eq this-command last-command))
  9395. (goto-char (point-min))
  9396. (message "Link search wrapped back to beginning of buffer"))
  9397. (setq org-link-search-failed nil)
  9398. (let* ((pos (point))
  9399. (ct (org-context))
  9400. (a (assoc :link ct))
  9401. (srch-fun (if search-backward 're-search-backward 're-search-forward)))
  9402. (cond (a (goto-char (nth (if search-backward 1 2) a)))
  9403. ((looking-at org-any-link-re)
  9404. ;; Don't stay stuck at link without an org-link face
  9405. (forward-char (if search-backward -1 1))))
  9406. (if (funcall srch-fun org-any-link-re nil t)
  9407. (progn
  9408. (goto-char (match-beginning 0))
  9409. (when (outline-invisible-p) (org-show-context)))
  9410. (goto-char pos)
  9411. (setq org-link-search-failed t)
  9412. (message "No further link found"))))
  9413. (defun org-previous-link ()
  9414. "Move backward to the previous link.
  9415. If the link is in hidden text, expose it."
  9416. (interactive)
  9417. (funcall 'org-next-link t))
  9418. (defun org-translate-link (s)
  9419. "Translate a link string if a translation function has been defined."
  9420. (with-temp-buffer
  9421. (insert (org-trim s))
  9422. (org-trim (org-element-interpret-data (org-element-context)))))
  9423. (defun org-translate-link-from-planner (type path)
  9424. "Translate a link from Emacs Planner syntax so that Org can follow it.
  9425. This is still an experimental function, your mileage may vary."
  9426. (cond
  9427. ((member type '("http" "https" "news" "ftp"))
  9428. ;; standard Internet links are the same.
  9429. nil)
  9430. ((and (equal type "irc") (string-match "^//" path))
  9431. ;; Planner has two / at the beginning of an irc link, we have 1.
  9432. ;; We should have zero, actually....
  9433. (setq path (substring path 1)))
  9434. ((and (equal type "lisp") (string-match "^/" path))
  9435. ;; Planner has a slash, we do not.
  9436. (setq type "elisp" path (substring path 1)))
  9437. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  9438. ;; A typical message link. Planner has the id after the final slash,
  9439. ;; we separate it with a hash mark
  9440. (setq path (concat (match-string 1 path) "#"
  9441. (org-remove-angle-brackets (match-string 2 path))))))
  9442. (cons type path))
  9443. (defun org-find-file-at-mouse (ev)
  9444. "Open file link or URL at mouse."
  9445. (interactive "e")
  9446. (mouse-set-point ev)
  9447. (org-open-at-point 'in-emacs))
  9448. (defun org-open-at-mouse (ev)
  9449. "Open file link or URL at mouse.
  9450. See the docstring of `org-open-file' for details."
  9451. (interactive "e")
  9452. (mouse-set-point ev)
  9453. (when (eq major-mode 'org-agenda-mode)
  9454. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  9455. (org-open-at-point))
  9456. (defvar org-window-config-before-follow-link nil
  9457. "The window configuration before following a link.
  9458. This is saved in case the need arises to restore it.")
  9459. ;;;###autoload
  9460. (defun org-open-at-point-global ()
  9461. "Follow a link like Org-mode does.
  9462. This command can be called in any mode to follow a link that has
  9463. Org-mode syntax."
  9464. (interactive)
  9465. (org-run-like-in-org-mode 'org-open-at-point))
  9466. ;;;###autoload
  9467. (defun org-open-link-from-string (s &optional arg reference-buffer)
  9468. "Open a link in the string S, as if it was in Org-mode."
  9469. (interactive "sLink: \nP")
  9470. (let ((reference-buffer (or reference-buffer (current-buffer))))
  9471. (with-temp-buffer
  9472. (let ((org-inhibit-startup (not reference-buffer)))
  9473. (org-mode)
  9474. (insert s)
  9475. (goto-char (point-min))
  9476. (when reference-buffer
  9477. (setq org-link-abbrev-alist-local
  9478. (with-current-buffer reference-buffer
  9479. org-link-abbrev-alist-local)))
  9480. (org-open-at-point arg reference-buffer)))))
  9481. (defvar org-open-at-point-functions nil
  9482. "Hook that is run when following a link at point.
  9483. Functions in this hook must return t if they identify and follow
  9484. a link at point. If they don't find anything interesting at point,
  9485. they must return nil.")
  9486. (defvar org-link-search-inhibit-query nil) ;; dynamically scoped
  9487. (defvar clean-buffer-list-kill-buffer-names) ; Defined in midnight.el
  9488. (defun org-open-at-point (&optional arg reference-buffer)
  9489. "Open link, timestamp, footnote or tags at point.
  9490. When point is on a link, follow it. Normally, files will be
  9491. opened by an appropriate application. If the optional prefix
  9492. argument ARG is non-nil, Emacs will visit the file. With
  9493. a double prefix argument, try to open outside of Emacs, in the
  9494. application the system uses for this file type.
  9495. When point is on a timestamp, open the agenda at the day
  9496. specified.
  9497. When point is a footnote definition, move to the first reference
  9498. found. If it is on a reference, move to the associated
  9499. definition.
  9500. When point is on a headline, display a list of every link in the
  9501. entry, so it is possible to pick one, or all, of them. If point
  9502. is on a tag, call `org-tags-view' instead.
  9503. When optional argument REFERENCE-BUFFER is non-nil, it should
  9504. specify a buffer from where the link search should happen. This
  9505. is used internally by `org-open-link-from-string'.
  9506. On top of syntactically correct links, this function will open
  9507. the link at point in comments or comment blocks and the first
  9508. link in a property drawer line."
  9509. (interactive "P")
  9510. ;; On a code block, open block's results.
  9511. (unless (call-interactively 'org-babel-open-src-block-result)
  9512. (org-load-modules-maybe)
  9513. (setq org-window-config-before-follow-link (current-window-configuration))
  9514. (org-remove-occur-highlights nil nil t)
  9515. (unless (run-hook-with-args-until-success 'org-open-at-point-functions)
  9516. (let* ((context
  9517. ;; Only consider supported types, even if they are not
  9518. ;; the closest one.
  9519. (org-element-lineage
  9520. (org-element-context)
  9521. '(clock comment comment-block footnote-definition
  9522. footnote-reference headline inlinetask keyword link
  9523. node-property timestamp)
  9524. t))
  9525. (type (org-element-type context))
  9526. (value (org-element-property :value context)))
  9527. (cond
  9528. ((not context) (user-error "No link found"))
  9529. ;; Exception: open timestamps and links in properties
  9530. ;; drawers, keywords and comments.
  9531. ((memq type '(comment comment-block keyword node-property))
  9532. (cond ((org-in-regexp org-any-link-re)
  9533. (org-open-link-from-string (match-string-no-properties 0)))
  9534. ((or (org-in-regexp org-ts-regexp-both nil t)
  9535. (org-in-regexp org-tsr-regexp-both nil t))
  9536. (org-follow-timestamp-link))
  9537. (t (user-error "No link found"))))
  9538. ;; On a headline or an inlinetask, but not on a timestamp,
  9539. ;; a link, a footnote reference or on tags.
  9540. ((and (memq type '(headline inlinetask))
  9541. ;; Not on tags.
  9542. (progn (save-excursion (beginning-of-line)
  9543. (looking-at org-complex-heading-regexp))
  9544. (or (not (match-beginning 5))
  9545. (< (point) (match-beginning 5)))))
  9546. (let* ((data (org-offer-links-in-entry (current-buffer) (point) arg))
  9547. (links (car data))
  9548. (links-end (cdr data)))
  9549. (if links
  9550. (dolist (link (if (stringp links) (list links) links))
  9551. (search-forward link nil links-end)
  9552. (goto-char (match-beginning 0))
  9553. (org-open-at-point))
  9554. (require 'org-attach)
  9555. (org-attach-reveal 'if-exists))))
  9556. ;; On a clock line, make sure point is on the timestamp
  9557. ;; before opening it.
  9558. ((and (eq type 'clock)
  9559. value
  9560. (>= (point) (org-element-property :begin value))
  9561. (<= (point) (org-element-property :end value)))
  9562. (org-follow-timestamp-link))
  9563. ;; Do nothing on white spaces after an object.
  9564. ((>= (point)
  9565. (save-excursion
  9566. (goto-char (org-element-property :end context))
  9567. (skip-chars-backward " \t")
  9568. (point)))
  9569. (user-error "No link found"))
  9570. ((eq type 'timestamp) (org-follow-timestamp-link))
  9571. ;; On tags within a headline or an inlinetask.
  9572. ((and (memq type '(headline inlinetask))
  9573. (progn (save-excursion (beginning-of-line)
  9574. (looking-at org-complex-heading-regexp))
  9575. (and (match-beginning 5)
  9576. (>= (point) (match-beginning 5)))))
  9577. (org-tags-view arg (substring (match-string 5) 0 -1)))
  9578. ((eq type 'link)
  9579. ;; When link is located within the description of another
  9580. ;; link (e.g., an inline image), always open the parent
  9581. ;; link.
  9582. (let*((link (let ((up (org-element-property :parent context)))
  9583. (if (eq (org-element-type up) 'link) up context)))
  9584. (type (org-element-property :type link))
  9585. (path (org-link-unescape (org-element-property :path link))))
  9586. ;; Switch back to REFERENCE-BUFFER needed when called in
  9587. ;; a temporary buffer through `org-open-link-from-string'.
  9588. (with-current-buffer (or reference-buffer (current-buffer))
  9589. (cond
  9590. ((equal type "file")
  9591. (if (string-match "[*?{]" (file-name-nondirectory path))
  9592. (dired path)
  9593. ;; Look into `org-link-protocols' in order to find
  9594. ;; a DEDICATED-FUNCTION to open file. The function
  9595. ;; will be applied on raw link instead of parsed
  9596. ;; link due to the limitation in `org-add-link-type'
  9597. ;; ("open" function called with a single argument).
  9598. ;; If no such function is found, fallback to
  9599. ;; `org-open-file'.
  9600. ;;
  9601. ;; Note : "file+emacs" and "file+sys" types are
  9602. ;; hard-coded in order to escape the previous
  9603. ;; limitation.
  9604. (let* ((option (org-element-property :search-option link))
  9605. (app (org-element-property :application link))
  9606. (dedicated-function
  9607. (nth 1 (assoc app org-link-protocols))))
  9608. (if dedicated-function
  9609. (funcall dedicated-function
  9610. (concat path
  9611. (and option (concat "::" option))))
  9612. (apply #'org-open-file
  9613. path
  9614. (cond (arg)
  9615. ((equal app "emacs") 'emacs)
  9616. ((equal app "sys") 'system))
  9617. (cond ((not option) nil)
  9618. ((org-string-match-p "\\`[0-9]+\\'" option)
  9619. (list (string-to-number option)))
  9620. (t (list nil
  9621. (org-link-unescape option)))))))))
  9622. ((assoc type org-link-protocols)
  9623. (funcall (nth 1 (assoc type org-link-protocols)) path))
  9624. ((equal type "help")
  9625. (let ((f-or-v (intern path)))
  9626. (cond ((fboundp f-or-v) (describe-function f-or-v))
  9627. ((boundp f-or-v) (describe-variable f-or-v))
  9628. (t (error "Not a known function or variable")))))
  9629. ((member type '("http" "https" "ftp" "mailto" "news"))
  9630. (browse-url (org-link-escape-browser (concat type ":" path))))
  9631. ((equal type "doi")
  9632. (browse-url
  9633. (org-link-escape-browser (concat org-doi-server-url path))))
  9634. ((equal type "message") (browse-url (concat type ":" path)))
  9635. ((equal type "shell")
  9636. (let ((buf (generate-new-buffer "*Org Shell Output*"))
  9637. (cmd path))
  9638. (if (or (and (org-string-nw-p
  9639. org-confirm-shell-link-not-regexp)
  9640. (string-match
  9641. org-confirm-shell-link-not-regexp cmd))
  9642. (not org-confirm-shell-link-function)
  9643. (funcall org-confirm-shell-link-function
  9644. (format "Execute \"%s\" in shell? "
  9645. (org-add-props cmd nil
  9646. 'face 'org-warning))))
  9647. (progn
  9648. (message "Executing %s" cmd)
  9649. (shell-command cmd buf)
  9650. (when (featurep 'midnight)
  9651. (setq clean-buffer-list-kill-buffer-names
  9652. (cons (buffer-name buf)
  9653. clean-buffer-list-kill-buffer-names))))
  9654. (user-error "Abort"))))
  9655. ((equal type "elisp")
  9656. (let ((cmd path))
  9657. (if (or (and (org-string-nw-p
  9658. org-confirm-elisp-link-not-regexp)
  9659. (org-string-match-p
  9660. org-confirm-elisp-link-not-regexp cmd))
  9661. (not org-confirm-elisp-link-function)
  9662. (funcall org-confirm-elisp-link-function
  9663. (format "Execute \"%s\" as elisp? "
  9664. (org-add-props cmd nil
  9665. 'face 'org-warning))))
  9666. (message "%s => %s" cmd
  9667. (if (eq (string-to-char cmd) ?\()
  9668. (eval (read cmd))
  9669. (call-interactively (read cmd))))
  9670. (user-error "Abort"))))
  9671. ((equal type "id")
  9672. (require 'org-id)
  9673. (funcall (nth 1 (assoc "id" org-link-protocols)) path))
  9674. ((member type '("coderef" "custom-id" "fuzzy" "radio"))
  9675. (unless (run-hook-with-args-until-success
  9676. 'org-open-link-functions path)
  9677. (if (not arg) (org-mark-ring-push)
  9678. (switch-to-buffer-other-window
  9679. (org-get-buffer-for-internal-link (current-buffer))))
  9680. (let ((destination
  9681. (org-with-wide-buffer
  9682. (if (equal type "radio")
  9683. (org-search-radio-target
  9684. (org-element-property :path link))
  9685. (org-link-search
  9686. (if (member type '("custom-id" "coderef"))
  9687. (org-element-property :raw-link link)
  9688. path)
  9689. ;; Prevent fuzzy links from matching
  9690. ;; themselves.
  9691. (and (equal type "fuzzy")
  9692. (+ 2 (org-element-property :begin link)))))
  9693. (point))))
  9694. (unless (and (<= (point-min) destination)
  9695. (>= (point-max) destination))
  9696. (widen))
  9697. (goto-char destination))))
  9698. (t (browse-url-at-point))))))
  9699. ;; On a footnote reference or at a footnote definition's label.
  9700. ((or (eq type 'footnote-reference)
  9701. (and (eq type 'footnote-definition)
  9702. (save-excursion
  9703. ;; Do not validate action when point is on the
  9704. ;; spaces right after the footnote label, in
  9705. ;; order to be on par with behaviour on links.
  9706. (skip-chars-forward " \t")
  9707. (let ((begin
  9708. (org-element-property :contents-begin context)))
  9709. (if begin (< (point) begin)
  9710. (= (org-element-property :post-affiliated context)
  9711. (line-beginning-position)))))))
  9712. (org-footnote-action))
  9713. (t (user-error "No link found")))))
  9714. (run-hook-with-args 'org-follow-link-hook)))
  9715. (defun org-offer-links-in-entry (buffer marker &optional nth zero)
  9716. "Offer links in the current entry and return the selected link.
  9717. If there is only one link, return it.
  9718. If NTH is an integer, return the NTH link found.
  9719. If ZERO is a string, check also this string for a link, and if
  9720. there is one, return it."
  9721. (with-current-buffer buffer
  9722. (save-excursion
  9723. (save-restriction
  9724. (widen)
  9725. (goto-char marker)
  9726. (let ((cnt ?0)
  9727. have-zero end links link c)
  9728. (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
  9729. (push (match-string 0 zero) links)
  9730. (setq cnt (1- cnt) have-zero t))
  9731. (save-excursion
  9732. (org-back-to-heading t)
  9733. (setq end (save-excursion (outline-next-heading) (point)))
  9734. (while (re-search-forward org-any-link-re end t)
  9735. (push (match-string 0) links))
  9736. (setq links (org-uniquify (reverse links))))
  9737. (cond
  9738. ((null links)
  9739. (message "No links"))
  9740. ((equal (length links) 1)
  9741. (setq link (car links)))
  9742. ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
  9743. (setq link (nth (if have-zero nth (1- nth)) links)))
  9744. (t ; we have to select a link
  9745. (save-excursion
  9746. (save-window-excursion
  9747. (delete-other-windows)
  9748. (with-output-to-temp-buffer "*Select Link*"
  9749. (dolist (l links)
  9750. (cond
  9751. ((not (string-match org-bracket-link-regexp l))
  9752. (princ (format "[%c] %s\n" (cl-incf cnt)
  9753. (org-remove-angle-brackets l))))
  9754. ((match-end 3)
  9755. (princ (format "[%c] %s (%s)\n" (cl-incf cnt)
  9756. (match-string 3 l) (match-string 1 l))))
  9757. (t (princ (format "[%c] %s\n" (cl-incf cnt)
  9758. (match-string 1 l)))))))
  9759. (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
  9760. (message "Select link to open, RET to open all:")
  9761. (setq c (read-char-exclusive))
  9762. (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
  9763. (when (equal c ?q) (user-error "Abort"))
  9764. (if (equal c ?\C-m)
  9765. (setq link links)
  9766. (setq nth (- c ?0))
  9767. (when have-zero (setq nth (1+ nth)))
  9768. (unless (and (integerp nth) (>= (length links) nth))
  9769. (user-error "Invalid link selection"))
  9770. (setq link (nth (1- nth) links)))))
  9771. (cons link end))))))
  9772. ;; TODO: These functions are deprecated since `org-open-at-point'
  9773. ;; hard-codes behaviour for "file+emacs" and "file+sys" types.
  9774. (defun org-open-file-with-system (path)
  9775. "Open file at PATH using the system way of opening it."
  9776. (org-open-file path 'system))
  9777. (defun org-open-file-with-emacs (path)
  9778. "Open file at PATH in Emacs."
  9779. (org-open-file path 'emacs))
  9780. ;;; File search
  9781. (defvar org-create-file-search-functions nil
  9782. "List of functions to construct the right search string for a file link.
  9783. These functions are called in turn with point at the location to
  9784. which the link should point.
  9785. A function in the hook should first test if it would like to
  9786. handle this file type, for example by checking the `major-mode'
  9787. or the file extension. If it decides not to handle this file, it
  9788. should just return nil to give other functions a chance. If it
  9789. does handle the file, it must return the search string to be used
  9790. when following the link. The search string will be part of the
  9791. file link, given after a double colon, and `org-open-at-point'
  9792. will automatically search for it. If special measures must be
  9793. taken to make the search successful, another function should be
  9794. added to the companion hook `org-execute-file-search-functions',
  9795. which see.
  9796. A function in this hook may also use `setq' to set the variable
  9797. `description' to provide a suggestion for the descriptive text to
  9798. be used for this link when it gets inserted into an Org-mode
  9799. buffer with \\[org-insert-link].")
  9800. (defvar org-execute-file-search-functions nil
  9801. "List of functions to execute a file search triggered by a link.
  9802. Functions added to this hook must accept a single argument, the
  9803. search string that was part of the file link, the part after the
  9804. double colon. The function must first check if it would like to
  9805. handle this search, for example by checking the `major-mode' or
  9806. the file extension. If it decides not to handle this search, it
  9807. should just return nil to give other functions a chance. If it
  9808. does handle the search, it must return a non-nil value to keep
  9809. other functions from trying.
  9810. Each function can access the current prefix argument through the
  9811. variable `current-prefix-arg'. Note that a single prefix is used
  9812. to force opening a link in Emacs, so it may be good to only use a
  9813. numeric or double prefix to guide the search function.
  9814. In case this is needed, a function in this hook can also restore
  9815. the window configuration before `org-open-at-point' was called using:
  9816. \(set-window-configuration org-window-config-before-follow-link)")
  9817. (defun org-search-radio-target (target)
  9818. "Search a radio target matching TARGET in current buffer.
  9819. White spaces are not significant."
  9820. (let ((re (format "<<<%s>>>"
  9821. (mapconcat #'regexp-quote
  9822. (org-split-string target "[ \t\n]+")
  9823. "[ \t]+\\(?:\n[ \t]*\\)?")))
  9824. (origin (point)))
  9825. (goto-char (point-min))
  9826. (catch :radio-match
  9827. (while (re-search-forward re nil t)
  9828. (backward-char)
  9829. (let ((object (org-element-context)))
  9830. (when (eq (org-element-type object) 'radio-target)
  9831. (goto-char (org-element-property :begin object))
  9832. (org-show-context 'link-search)
  9833. (throw :radio-match nil))))
  9834. (goto-char origin)
  9835. (user-error "No match for radio target: %s" target))))
  9836. (defun org-link-search (s &optional avoid-pos stealth)
  9837. "Search for a search string S.
  9838. If S starts with \"#\", it triggers a custom ID search.
  9839. If S is enclosed within parenthesis, it initiates a coderef
  9840. search.
  9841. If S is surrounded by forward slashes, it is interpreted as
  9842. a regular expression. In Org mode files, this will create an
  9843. `org-occur' sparse tree. In ordinary files, `occur' will be used
  9844. to list matches. If the current buffer is in `dired-mode', grep
  9845. will be used to search in all files.
  9846. When AVOID-POS is given, ignore matches near that position.
  9847. When optional argument STEALTH is non-nil, do not modify
  9848. visibility around point, thus ignoring `org-show-context-detail'
  9849. variable.
  9850. Search is case-insensitive and ignores white spaces. Return type
  9851. of matched result, with is either `dedicated' or `fuzzy'."
  9852. (unless (org-string-nw-p s) (error "Invalid search string \"%s\"" s))
  9853. (let* ((case-fold-search t)
  9854. (origin (point))
  9855. (normalized (replace-regexp-in-string "\n[ \t]*" " " s))
  9856. (words (org-split-string s "[ \t\n]+"))
  9857. (s-multi-re (mapconcat #'regexp-quote words "[ \t]+\\(?:\n[ \t]*\\)?"))
  9858. (s-single-re (mapconcat #'regexp-quote words "[ \t]+"))
  9859. type)
  9860. (cond
  9861. ;; Check if there are any special search functions.
  9862. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  9863. ((eq (string-to-char s) ?#)
  9864. ;; Look for a custom ID S if S starts with "#".
  9865. (let* ((id (substring normalized 1))
  9866. (match (org-find-property "CUSTOM_ID" id)))
  9867. (if match (progn (goto-char match) (setf type 'dedicated))
  9868. (error "No match for custom ID: %s" id))))
  9869. ((string-match "\\`(\\(.*\\))\\'" normalized)
  9870. ;; Look for coderef targets if S is enclosed within parenthesis.
  9871. (let ((coderef (match-string-no-properties 1 normalized))
  9872. (re (substring s-single-re 1 -1)))
  9873. (goto-char (point-min))
  9874. (catch :coderef-match
  9875. (while (re-search-forward re nil t)
  9876. (let ((element (org-element-at-point)))
  9877. (when (and (memq (org-element-type element)
  9878. '(example-block src-block))
  9879. ;; Build proper regexp according to current
  9880. ;; block's label format.
  9881. (let ((label-fmt
  9882. (regexp-quote
  9883. (or (org-element-property :label-fmt element)
  9884. org-coderef-label-format))))
  9885. (save-excursion
  9886. (beginning-of-line)
  9887. (looking-at (format ".*?\\(%s\\)[ \t]*$"
  9888. (format label-fmt coderef))))))
  9889. (setq type 'dedicated)
  9890. (goto-char (match-beginning 1))
  9891. (throw :coderef-match nil))))
  9892. (goto-char origin)
  9893. (error "No match for coderef: %s" coderef))))
  9894. ((string-match "\\`/\\(.*\\)/\\'" normalized)
  9895. ;; Look for a regular expression.
  9896. (funcall (if (derived-mode-p 'org-mode) #'org-occur #'org-do-occur)
  9897. (match-string 1 s)))
  9898. ;; Fuzzy links.
  9899. (t
  9900. (let ((starred (eq (string-to-char normalized) ?*)))
  9901. (cond
  9902. ;; Look for targets, only if not in a headline search.
  9903. ((and (not starred)
  9904. (let ((target (format "<<%s>>" s-multi-re)))
  9905. (catch :target-match
  9906. (goto-char (point-min))
  9907. (while (re-search-forward target nil t)
  9908. (backward-char)
  9909. (let ((context (org-element-context)))
  9910. (when (eq (org-element-type context) 'target)
  9911. (setq type 'dedicated)
  9912. (goto-char (org-element-property :begin context))
  9913. (throw :target-match t))))
  9914. nil))))
  9915. ;; Look for elements named after S, only if not in a headline
  9916. ;; search.
  9917. ((and (not starred)
  9918. (let ((name (format "^[ \t]*#\\+NAME: +%s[ \t]*$" s-single-re)))
  9919. (catch :name-match
  9920. (goto-char (point-min))
  9921. (while (re-search-forward name nil t)
  9922. (let ((element (org-element-at-point)))
  9923. (when (equal (org-split-string
  9924. (org-element-property :name element)
  9925. "[ \t]+")
  9926. words)
  9927. (setq type 'dedicated)
  9928. (beginning-of-line)
  9929. (throw :name-match t))))
  9930. nil))))
  9931. ;; Regular text search. Prefer headlines in Org mode
  9932. ;; buffers.
  9933. ((and (derived-mode-p 'org-mode)
  9934. (let* ((wspace "[ \t]")
  9935. (wspaceopt (concat wspace "*"))
  9936. (cookie (concat "\\(?:"
  9937. wspaceopt
  9938. "\\[[0-9]*\\(?:%\\|/[0-9]*\\)\\]"
  9939. wspaceopt
  9940. "\\)"))
  9941. (sep (concat "\\(?:\\(?:" wspace "\\|" cookie "\\)+\\)"))
  9942. (re (concat
  9943. org-outline-regexp-bol
  9944. "\\(?:" org-todo-regexp "[ \t]+\\)?"
  9945. "\\(?:\\[#.\\][ \t]+\\)?"
  9946. "\\(?:" org-comment-string "[ \t]+\\)?"
  9947. sep "?"
  9948. (let ((title (mapconcat #'regexp-quote
  9949. words
  9950. sep)))
  9951. (if starred (substring title 1) title))
  9952. sep "?"
  9953. (org-re "\\(?:[ \t]+:[[:alnum:]_@#%%:]+:\\)?")
  9954. "[ \t]*$")))
  9955. (goto-char (point-min))
  9956. (re-search-forward re nil t)))
  9957. (goto-char (match-beginning 0))
  9958. (setq type 'dedicated))
  9959. ;; Offer to create non-existent headline depending on
  9960. ;; `org-link-search-must-match-exact-headline'.
  9961. ((and (derived-mode-p 'org-mode)
  9962. (not org-link-search-inhibit-query)
  9963. (eq org-link-search-must-match-exact-headline 'query-to-create)
  9964. (yes-or-no-p "No match - create this as a new heading? "))
  9965. (goto-char (point-max))
  9966. (unless (bolp) (newline))
  9967. (org-insert-heading nil t t)
  9968. (insert s "\n")
  9969. (beginning-of-line 0))
  9970. ;; Only headlines are looked after. No need to process
  9971. ;; further: throw an error.
  9972. ((and (derived-mode-p 'org-mode)
  9973. (or starred org-link-search-must-match-exact-headline))
  9974. (goto-char origin)
  9975. (error "No match for fuzzy expression: %s" normalized))
  9976. ;; Regular text search.
  9977. ((catch :fuzzy-match
  9978. (goto-char (point-min))
  9979. (while (re-search-forward s-multi-re nil t)
  9980. ;; Skip match if it contains AVOID-POS or it is included
  9981. ;; in a link with a description but outside the
  9982. ;; description.
  9983. (unless (or (and avoid-pos
  9984. (<= (match-beginning 0) avoid-pos)
  9985. (> (match-end 0) avoid-pos))
  9986. (and (save-match-data
  9987. (org-in-regexp org-bracket-link-regexp))
  9988. (match-beginning 3)
  9989. (or (> (match-beginning 3) (point))
  9990. (<= (match-end 3) (point)))
  9991. (org-element-lineage
  9992. (save-match-data (org-element-context))
  9993. '(link) t)))
  9994. (goto-char (match-beginning 0))
  9995. (setq type 'fuzzy)
  9996. (throw :fuzzy-match t)))
  9997. nil))
  9998. ;; All failed. Throw an error.
  9999. (t (goto-char origin)
  10000. (error "No match for fuzzy expression: %s" normalized))))))
  10001. ;; Disclose surroundings of match, if appropriate.
  10002. (when (and (derived-mode-p 'org-mode) (not stealth))
  10003. (org-show-context 'link-search))
  10004. type))
  10005. (defun org-get-buffer-for-internal-link (buffer)
  10006. "Return a buffer to be used for displaying the link target of internal links."
  10007. (cond
  10008. ((not org-display-internal-link-with-indirect-buffer)
  10009. buffer)
  10010. ((string-match "(Clone)$" (buffer-name buffer))
  10011. (message "Buffer is already a clone, not making another one")
  10012. ;; we also do not modify visibility in this case
  10013. buffer)
  10014. (t ; make a new indirect buffer for displaying the link
  10015. (let* ((bn (buffer-name buffer))
  10016. (ibn (concat bn "(Clone)"))
  10017. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  10018. (with-current-buffer ib (org-overview))
  10019. ib))))
  10020. (defun org-do-occur (regexp &optional cleanup)
  10021. "Call the Emacs command `occur'.
  10022. If CLEANUP is non-nil, remove the printout of the regular expression
  10023. in the *Occur* buffer. This is useful if the regex is long and not useful
  10024. to read."
  10025. (occur regexp)
  10026. (when cleanup
  10027. (let ((cwin (selected-window)) win beg end)
  10028. (when (setq win (get-buffer-window "*Occur*"))
  10029. (select-window win))
  10030. (goto-char (point-min))
  10031. (when (re-search-forward "match[a-z]+" nil t)
  10032. (setq beg (match-end 0))
  10033. (when (re-search-forward "^[ \t]*[0-9]+" nil t)
  10034. (setq end (1- (match-beginning 0)))))
  10035. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  10036. (goto-char (point-min))
  10037. (select-window cwin))))
  10038. ;;; The mark ring for links jumps
  10039. (defvar org-mark-ring nil
  10040. "Mark ring for positions before jumps in Org-mode.")
  10041. (defvar org-mark-ring-last-goto nil
  10042. "Last position in the mark ring used to go back.")
  10043. ;; Fill and close the ring
  10044. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  10045. (dotimes (_ org-mark-ring-length)
  10046. (push (make-marker) org-mark-ring))
  10047. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  10048. org-mark-ring)
  10049. (defun org-mark-ring-push (&optional pos buffer)
  10050. "Put the current position or POS into the mark ring and rotate it."
  10051. (interactive)
  10052. (setq pos (or pos (point)))
  10053. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  10054. (move-marker (car org-mark-ring)
  10055. (or pos (point))
  10056. (or buffer (current-buffer)))
  10057. (message "%s"
  10058. (substitute-command-keys
  10059. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  10060. (defun org-mark-ring-goto (&optional n)
  10061. "Jump to the previous position in the mark ring.
  10062. With prefix arg N, jump back that many stored positions. When
  10063. called several times in succession, walk through the entire ring.
  10064. Org-mode commands jumping to a different position in the current file,
  10065. or to another Org-mode file, automatically push the old position
  10066. onto the ring."
  10067. (interactive "p")
  10068. (let (p m)
  10069. (if (eq last-command this-command)
  10070. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  10071. (setq p org-mark-ring))
  10072. (setq org-mark-ring-last-goto p)
  10073. (setq m (car p))
  10074. (org-pop-to-buffer-same-window (marker-buffer m))
  10075. (goto-char m)
  10076. (when (or (outline-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  10077. ;;; TODO: Use string-remove-prefix and -suffix once we only support
  10078. ;;; Emacs 24.4+
  10079. (defun org-remove-angle-brackets (s)
  10080. (when (equal (substring s 0 1) "<") (setq s (substring s 1)))
  10081. (when (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  10082. s)
  10083. (defun org-add-angle-brackets (s)
  10084. (unless (equal (substring s 0 1) "<") (setq s (concat "<" s)))
  10085. (unless (equal (substring s -1) ">") (setq s (concat s ">")))
  10086. s)
  10087. (defun org-remove-double-quotes (s)
  10088. (when (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  10089. (when (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  10090. s)
  10091. ;;; Following specific links
  10092. (defvar org-agenda-buffer-tmp-name)
  10093. (defvar org-agenda-start-on-weekday)
  10094. (defun org-follow-timestamp-link ()
  10095. "Open an agenda view for the time-stamp date/range at point."
  10096. (cond
  10097. ((org-at-date-range-p t)
  10098. (let ((org-agenda-start-on-weekday)
  10099. (t1 (match-string 1))
  10100. (t2 (match-string 2)) tt1 tt2)
  10101. (setq tt1 (time-to-days (org-time-string-to-time t1))
  10102. tt2 (time-to-days (org-time-string-to-time t2)))
  10103. (let ((org-agenda-buffer-tmp-name
  10104. (format "*Org Agenda(a:%s)"
  10105. (concat (substring t1 0 10) "--" (substring t2 0 10)))))
  10106. (org-agenda-list nil tt1 (1+ (- tt2 tt1))))))
  10107. ((org-at-timestamp-p t)
  10108. (let ((org-agenda-buffer-tmp-name
  10109. (format "*Org Agenda(a:%s)" (substring (match-string 1) 0 10))))
  10110. (org-agenda-list nil (time-to-days (org-time-string-to-time
  10111. (substring (match-string 1) 0 10)))
  10112. 1)))
  10113. (t (error "This should not happen"))))
  10114. ;;; Following file links
  10115. (declare-function mailcap-parse-mailcaps "mailcap" (&optional path force))
  10116. (declare-function mailcap-extension-to-mime "mailcap" (extn))
  10117. (declare-function mailcap-mime-info
  10118. "mailcap" (string &optional request no-decode))
  10119. (defvar org-wait nil)
  10120. (defun org-open-file (path &optional in-emacs line search)
  10121. "Open the file at PATH.
  10122. First, this expands any special file name abbreviations. Then the
  10123. configuration variable `org-file-apps' is checked if it contains an
  10124. entry for this file type, and if yes, the corresponding command is launched.
  10125. If no application is found, Emacs simply visits the file.
  10126. With optional prefix argument IN-EMACS, Emacs will visit the file.
  10127. With a double \\[universal-argument] \\[universal-argument] \
  10128. prefix arg, Org tries to avoid opening in Emacs
  10129. and to use an external application to visit the file.
  10130. Optional LINE specifies a line to go to, optional SEARCH a string
  10131. to search for. If LINE or SEARCH is given, the file will be
  10132. opened in Emacs, unless an entry from org-file-apps that makes
  10133. use of groups in a regexp matches.
  10134. If you want to change the way frames are used when following a
  10135. link, please customize `org-link-frame-setup'.
  10136. If the file does not exist, an error is thrown."
  10137. (let* ((file (if (equal path "")
  10138. buffer-file-name
  10139. (substitute-in-file-name (expand-file-name path))))
  10140. (file-apps (append org-file-apps (org-default-apps)))
  10141. (apps (cl-remove-if
  10142. 'org-file-apps-entry-match-against-dlink-p file-apps))
  10143. (apps-dlink (cl-remove-if-not
  10144. 'org-file-apps-entry-match-against-dlink-p file-apps))
  10145. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  10146. (dirp (unless remp (file-directory-p file)))
  10147. (file (if (and dirp org-open-directory-means-index-dot-org)
  10148. (concat (file-name-as-directory file) "index.org")
  10149. file))
  10150. (a-m-a-p (assq 'auto-mode apps))
  10151. (dfile (downcase file))
  10152. ;; Reconstruct the original link from the PATH, LINE and
  10153. ;; SEARCH args.
  10154. (link (cond (line (concat file "::" (number-to-string line)))
  10155. (search (concat file "::" search))
  10156. (t file)))
  10157. (dlink (downcase link))
  10158. (old-buffer (current-buffer))
  10159. (old-pos (point))
  10160. (old-mode major-mode)
  10161. (ext
  10162. (and (string-match "\\`.*?\\.\\([a-zA-Z0-9]+\\(\\.gz\\)?\\)\\'" dfile)
  10163. (match-string 1 dfile)))
  10164. cmd link-match-data)
  10165. (cond
  10166. ((member in-emacs '((16) system))
  10167. (setq cmd (cdr (assq 'system apps))))
  10168. (in-emacs (setq cmd 'emacs))
  10169. (t
  10170. (setq cmd (or (and remp (cdr (assq 'remote apps)))
  10171. (and dirp (cdr (assq 'directory apps)))
  10172. ;; First, try matching against apps-dlink if we
  10173. ;; get a match here, store the match data for
  10174. ;; later.
  10175. (let ((match (assoc-default dlink apps-dlink
  10176. 'string-match)))
  10177. (if match
  10178. (progn (setq link-match-data (match-data))
  10179. match)
  10180. (progn (setq in-emacs (or in-emacs line search))
  10181. nil))) ; if we have no match in apps-dlink,
  10182. ; always open the file in emacs if line or search
  10183. ; is given (for backwards compatibility)
  10184. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  10185. 'string-match)
  10186. (cdr (assoc ext apps))
  10187. (cdr (assq t apps))))))
  10188. (when (eq cmd 'system)
  10189. (setq cmd (cdr (assoc 'system apps))))
  10190. (when (eq cmd 'default)
  10191. (setq cmd (cdr (assoc t apps))))
  10192. (when (eq cmd 'mailcap)
  10193. (require 'mailcap)
  10194. (mailcap-parse-mailcaps)
  10195. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  10196. (command (mailcap-mime-info mime-type)))
  10197. (if (stringp command)
  10198. (setq cmd command)
  10199. (setq cmd 'emacs))))
  10200. (when (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  10201. (not (file-exists-p file))
  10202. (not org-open-non-existing-files))
  10203. (user-error "No such file: %s" file))
  10204. (cond
  10205. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  10206. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  10207. (while (string-match "['\"]%s['\"]" cmd)
  10208. (setq cmd (replace-match "%s" t t cmd)))
  10209. (while (string-match "%s" cmd)
  10210. (setq cmd (replace-match
  10211. (save-match-data
  10212. (shell-quote-argument
  10213. (convert-standard-filename file)))
  10214. t t cmd)))
  10215. ;; Replace "%1", "%2" etc. in command with group matches from regex
  10216. (save-match-data
  10217. (let ((match-index 1)
  10218. (number-of-groups (- (/ (length link-match-data) 2) 1)))
  10219. (set-match-data link-match-data)
  10220. (while (<= match-index number-of-groups)
  10221. (let ((regex (concat "%" (number-to-string match-index)))
  10222. (replace-with (match-string match-index dlink)))
  10223. (while (string-match regex cmd)
  10224. (setq cmd (replace-match replace-with t t cmd))))
  10225. (setq match-index (+ match-index 1)))))
  10226. (save-window-excursion
  10227. (message "Running %s...done" cmd)
  10228. (start-process-shell-command cmd nil cmd)
  10229. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))))
  10230. ((or (stringp cmd)
  10231. (eq cmd 'emacs))
  10232. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  10233. (widen)
  10234. (cond (line (org-goto-line line)
  10235. (when (derived-mode-p 'org-mode) (org-reveal)))
  10236. (search (org-link-search search))))
  10237. ((functionp cmd)
  10238. (save-match-data
  10239. (set-match-data link-match-data)
  10240. (condition-case nil
  10241. (funcall cmd file link)
  10242. ;; FIXME: Remove this check when most default installations
  10243. ;; of Emacs have at least Org 9.0.
  10244. ((debug wrong-number-of-arguments wrong-type-argument
  10245. invalid-function)
  10246. (user-error "Please see Org News for version 9.0 about \
  10247. `org-file-apps'--Lisp error: %S" cmd)))))
  10248. ((consp cmd)
  10249. ;; FIXME: Remove this check when most default installations of
  10250. ;; Emacs have at least Org 9.0.
  10251. ;; Heads-up instead of silently fall back to
  10252. ;; `org-link-frame-setup' for an old usage of `org-file-apps'
  10253. ;; with sexp instead of a function for `cmd'.
  10254. (user-error "Please see Org News for version 9.0 about \
  10255. `org-file-apps'--Error: Deprecated usage of %S" cmd))
  10256. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  10257. (and (derived-mode-p 'org-mode)
  10258. (eq old-mode 'org-mode)
  10259. (or (not (eq old-buffer (current-buffer)))
  10260. (not (eq old-pos (point))))
  10261. (org-mark-ring-push old-pos old-buffer))))
  10262. (defun org-file-apps-entry-match-against-dlink-p (entry)
  10263. "This function returns non-nil if `entry' uses a regular
  10264. expression which should be matched against the whole link by
  10265. org-open-file.
  10266. It assumes that is the case when the entry uses a regular
  10267. expression which has at least one grouping construct and the
  10268. action is either a lisp form or a command string containing
  10269. `%1', i.e. using at least one subexpression match as a
  10270. parameter."
  10271. (let ((selector (car entry))
  10272. (action (cdr entry)))
  10273. (if (stringp selector)
  10274. (and (> (regexp-opt-depth selector) 0)
  10275. (or (and (stringp action)
  10276. (string-match "%[0-9]" action))
  10277. (consp action)))
  10278. nil)))
  10279. (defun org-default-apps ()
  10280. "Return the default applications for this operating system."
  10281. (cond
  10282. ((eq system-type 'darwin)
  10283. org-file-apps-defaults-macosx)
  10284. ((eq system-type 'windows-nt)
  10285. org-file-apps-defaults-windowsnt)
  10286. (t org-file-apps-defaults-gnu)))
  10287. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  10288. "Convert extensions to regular expressions in the cars of LIST.
  10289. Also, weed out any non-string entries, because the return value is used
  10290. only for regexp matching.
  10291. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  10292. point to the symbol `emacs', indicating that the file should
  10293. be opened in Emacs."
  10294. (append
  10295. (delq nil
  10296. (mapcar (lambda (x)
  10297. (unless (not (stringp (car x)))
  10298. (if (string-match "\\W" (car x))
  10299. x
  10300. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  10301. list))
  10302. (when add-auto-mode
  10303. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  10304. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  10305. (defun org-file-remote-p (file)
  10306. "Test whether FILE specifies a location on a remote system.
  10307. Return non-nil if the location is indeed remote.
  10308. For example, the filename \"/user@host:/foo\" specifies a location
  10309. on the system \"/user@host:\"."
  10310. (cond ((fboundp 'file-remote-p)
  10311. (file-remote-p file))
  10312. ((fboundp 'tramp-handle-file-remote-p)
  10313. (tramp-handle-file-remote-p file))
  10314. ((and (boundp 'ange-ftp-name-format)
  10315. (string-match (car ange-ftp-name-format) file))
  10316. t)))
  10317. ;;;; Refiling
  10318. (defun org-get-org-file ()
  10319. "Read a filename, with default directory `org-directory'."
  10320. (let ((default (or org-default-notes-file remember-data-file)))
  10321. (read-file-name (format "File name [%s]: " default)
  10322. (file-name-as-directory org-directory)
  10323. default)))
  10324. (defun org-notes-order-reversed-p ()
  10325. "Check if the current file should receive notes in reversed order."
  10326. (cond
  10327. ((not org-reverse-note-order) nil)
  10328. ((eq t org-reverse-note-order) t)
  10329. ((not (listp org-reverse-note-order)) nil)
  10330. (t (catch 'exit
  10331. (dolist (entry org-reverse-note-order)
  10332. (when (string-match (car entry) buffer-file-name)
  10333. (throw 'exit (cdr entry))))))))
  10334. (defvar org-refile-target-table nil
  10335. "The list of refile targets, created by `org-refile'.")
  10336. (defvar org-agenda-new-buffers nil
  10337. "Buffers created to visit agenda files.")
  10338. (defvar org-refile-cache nil
  10339. "Cache for refile targets.")
  10340. (defvar org-refile-markers nil
  10341. "All the markers used for caching refile locations.")
  10342. (defun org-refile-marker (pos)
  10343. "Get a new refile marker, but only if caching is in use."
  10344. (if (not org-refile-use-cache)
  10345. pos
  10346. (let ((m (make-marker)))
  10347. (move-marker m pos)
  10348. (push m org-refile-markers)
  10349. m)))
  10350. (defun org-refile-cache-clear ()
  10351. "Clear the refile cache and disable all the markers."
  10352. (dolist (m org-refile-markers) (move-marker m nil))
  10353. (setq org-refile-markers nil)
  10354. (setq org-refile-cache nil)
  10355. (message "Refile cache has been cleared"))
  10356. (defun org-refile-cache-check-set (set)
  10357. "Check if all the markers in the cache still have live buffers."
  10358. (let (marker)
  10359. (catch 'exit
  10360. (while (and set (setq marker (nth 3 (pop set))))
  10361. ;; If `org-refile-use-outline-path' is 'file, marker may be nil
  10362. (when (and marker (null (marker-buffer marker)))
  10363. (message "Please regenerate the refile cache with `C-0 C-c C-w'")
  10364. (sit-for 3)
  10365. (throw 'exit nil)))
  10366. t)))
  10367. (defun org-refile-cache-put (set &rest identifiers)
  10368. "Push the refile targets SET into the cache, under IDENTIFIERS."
  10369. (let* ((key (sha1 (prin1-to-string identifiers)))
  10370. (entry (assoc key org-refile-cache)))
  10371. (if entry
  10372. (setcdr entry set)
  10373. (push (cons key set) org-refile-cache))))
  10374. (defun org-refile-cache-get (&rest identifiers)
  10375. "Retrieve the cached value for refile targets given by IDENTIFIERS."
  10376. (cond
  10377. ((not org-refile-cache) nil)
  10378. ((not org-refile-use-cache) (org-refile-cache-clear) nil)
  10379. (t
  10380. (let ((set (cdr (assoc (sha1 (prin1-to-string identifiers))
  10381. org-refile-cache))))
  10382. (and set (org-refile-cache-check-set set) set)))))
  10383. (defvar org-outline-path-cache nil
  10384. "Alist between buffer positions and outline paths.
  10385. It value is an alist (POSITION . PATH) where POSITION is the
  10386. buffer position at the beginning of an entry and PATH is a list
  10387. of strings describing the outline path for that entry, in reverse
  10388. order.")
  10389. (defun org-refile-get-targets (&optional default-buffer)
  10390. "Produce a table with refile targets."
  10391. (let ((case-fold-search nil)
  10392. ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
  10393. (entries (or org-refile-targets '((nil . (:level . 1)))))
  10394. targets tgs files desc descre)
  10395. (message "Getting targets...")
  10396. (with-current-buffer (or default-buffer (current-buffer))
  10397. (dolist (entry entries)
  10398. (setq files (car entry) desc (cdr entry))
  10399. (cond
  10400. ((null files) (setq files (list (current-buffer))))
  10401. ((eq files 'org-agenda-files)
  10402. (setq files (org-agenda-files 'unrestricted)))
  10403. ((and (symbolp files) (fboundp files))
  10404. (setq files (funcall files)))
  10405. ((and (symbolp files) (boundp files))
  10406. (setq files (symbol-value files))))
  10407. (when (stringp files) (setq files (list files)))
  10408. (cond
  10409. ((eq (car desc) :tag)
  10410. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  10411. ((eq (car desc) :todo)
  10412. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  10413. ((eq (car desc) :regexp)
  10414. (setq descre (cdr desc)))
  10415. ((eq (car desc) :level)
  10416. (setq descre (concat "^\\*\\{" (number-to-string
  10417. (if org-odd-levels-only
  10418. (1- (* 2 (cdr desc)))
  10419. (cdr desc)))
  10420. "\\}[ \t]")))
  10421. ((eq (car desc) :maxlevel)
  10422. (setq descre (concat "^\\*\\{1," (number-to-string
  10423. (if org-odd-levels-only
  10424. (1- (* 2 (cdr desc)))
  10425. (cdr desc)))
  10426. "\\}[ \t]")))
  10427. (t (error "Bad refiling target description %s" desc)))
  10428. (dolist (f files)
  10429. (with-current-buffer (if (bufferp f) f (org-get-agenda-file-buffer f))
  10430. (or
  10431. (setq tgs (org-refile-cache-get (buffer-file-name) descre))
  10432. (progn
  10433. (when (bufferp f)
  10434. (setq f (buffer-file-name (buffer-base-buffer f))))
  10435. (setq f (and f (expand-file-name f)))
  10436. (when (eq org-refile-use-outline-path 'file)
  10437. (push (list (file-name-nondirectory f) f nil nil) tgs))
  10438. (org-with-wide-buffer
  10439. (goto-char (point-min))
  10440. (setq org-outline-path-cache nil)
  10441. (while (re-search-forward descre nil t)
  10442. (beginning-of-line)
  10443. (looking-at org-complex-heading-regexp)
  10444. (let ((begin (point))
  10445. (heading (org-match-string-no-properties 4)))
  10446. (unless (or (and
  10447. org-refile-target-verify-function
  10448. (not
  10449. (funcall org-refile-target-verify-function)))
  10450. (not heading))
  10451. (let ((re (format org-complex-heading-regexp-format
  10452. (regexp-quote heading)))
  10453. (target
  10454. (if (not org-refile-use-outline-path) heading
  10455. (mapconcat
  10456. #'org-protect-slash
  10457. (append
  10458. (pcase org-refile-use-outline-path
  10459. (`file (list (file-name-nondirectory
  10460. (buffer-file-name
  10461. (buffer-base-buffer)))))
  10462. (`full-file-path
  10463. (list (buffer-file-name
  10464. (buffer-base-buffer))))
  10465. (_ nil))
  10466. (org-get-outline-path t t))
  10467. "/"))))
  10468. (push (list target f re (org-refile-marker (point)))
  10469. tgs)))
  10470. (when (= (point) begin)
  10471. ;; Verification function has not moved point.
  10472. (end-of-line)))))))
  10473. (when org-refile-use-cache
  10474. (org-refile-cache-put tgs (buffer-file-name) descre))
  10475. (setq targets (append tgs targets))))))
  10476. (message "Getting targets...done")
  10477. (nreverse targets)))
  10478. (defun org-protect-slash (s)
  10479. (while (string-match "/" s)
  10480. (setq s (replace-match "\\" t t s)))
  10481. s)
  10482. (defun org--get-outline-path-1 (&optional use-cache)
  10483. "Return outline path to current headline.
  10484. Outline path is a list of strings, in reverse order. When
  10485. optional argument USE-CACHE is non-nil, make use of a cache. See
  10486. `org-get-outline-path' for details.
  10487. Assume buffer is widened and point is on a headline."
  10488. (or (and use-cache (cdr (assq (point) org-outline-path-cache)))
  10489. (let ((p (point))
  10490. (heading (progn
  10491. (looking-at org-complex-heading-regexp)
  10492. (if (not (match-end 4)) ""
  10493. ;; Remove statistics cookies.
  10494. (org-trim
  10495. (org-link-display-format
  10496. (replace-regexp-in-string
  10497. "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" ""
  10498. (org-match-string-no-properties 4))))))))
  10499. (if (org-up-heading-safe)
  10500. (let ((path (cons heading (org--get-outline-path-1 use-cache))))
  10501. (when use-cache
  10502. (push (cons p path) org-outline-path-cache))
  10503. path)
  10504. ;; This is a new root node. Since we assume we are moving
  10505. ;; forward, we can drop previous cache so as to limit number
  10506. ;; of associations there.
  10507. (let ((path (list heading)))
  10508. (when use-cache (setq org-outline-path-cache (list (cons p path))))
  10509. path)))))
  10510. (defun org-get-outline-path (&optional with-self use-cache)
  10511. "Return the outline path to the current entry.
  10512. An outline path is a list of ancestors for current headline, as
  10513. a list of strings. Statistics cookies are removed and links are
  10514. replaced with their description, if any, or their path otherwise.
  10515. When optional argument WITH-SELF is non-nil, the path also
  10516. includes the current headline.
  10517. When optional argument USE-CACHE is non-nil, cache outline paths
  10518. between calls to this function so as to avoid backtracking. This
  10519. argument is useful when planning to find more than one outline
  10520. path in the same document. In that case, there are two
  10521. conditions to satisfy:
  10522. - `org-outline-path-cache' is set to nil before starting the
  10523. process;
  10524. - outline paths are computed by increasing buffer positions."
  10525. (org-with-wide-buffer
  10526. (and (or (and with-self (org-back-to-heading t))
  10527. (org-up-heading-safe))
  10528. (reverse (org--get-outline-path-1 use-cache)))))
  10529. (defun org-format-outline-path (path &optional width prefix separator)
  10530. "Format the outline path PATH for display.
  10531. WIDTH is the maximum number of characters that is available.
  10532. PREFIX is a prefix to be included in the returned string,
  10533. such as the file name.
  10534. SEPARATOR is inserted between the different parts of the path,
  10535. the default is \"/\"."
  10536. (setq width (or width 79))
  10537. (setq path (delq nil path))
  10538. (unless (> width 0)
  10539. (user-error "Argument `width' must be positive"))
  10540. (setq separator (or separator "/"))
  10541. (let* ((org-odd-levels-only nil)
  10542. (fpath (concat
  10543. prefix (and prefix path separator)
  10544. (mapconcat
  10545. (lambda (s) (replace-regexp-in-string "[ \t]+\\'" "" s))
  10546. (cl-loop for head in path
  10547. for n from 0
  10548. collect (org-add-props
  10549. head nil 'face
  10550. (nth (% n org-n-level-faces) org-level-faces)))
  10551. separator))))
  10552. (when (> (length fpath) width)
  10553. (if (< width 7)
  10554. ;; It's unlikely that `width' will be this small, but don't
  10555. ;; waste characters by adding ".." if it is.
  10556. (setq fpath (substring fpath 0 width))
  10557. (setf (substring fpath (- width 2)) "..")))
  10558. fpath))
  10559. (defun org-display-outline-path (&optional file current separator just-return-string)
  10560. "Display the current outline path in the echo area.
  10561. If FILE is non-nil, prepend the output with the file name.
  10562. If CURRENT is non-nil, append the current heading to the output.
  10563. SEPARATOR is passed through to `org-format-outline-path'. It separates
  10564. the different parts of the path and defaults to \"/\".
  10565. If JUST-RETURN-STRING is non-nil, return a string, don't display a message."
  10566. (interactive "P")
  10567. (let* (case-fold-search
  10568. (bfn (buffer-file-name (buffer-base-buffer)))
  10569. (path (and (derived-mode-p 'org-mode) (org-get-outline-path)))
  10570. res)
  10571. (when current (setq path (append path
  10572. (save-excursion
  10573. (org-back-to-heading t)
  10574. (when (looking-at org-complex-heading-regexp)
  10575. (list (match-string 4)))))))
  10576. (setq res
  10577. (org-format-outline-path
  10578. path
  10579. (1- (frame-width))
  10580. (and file bfn (concat (file-name-nondirectory bfn) separator))
  10581. separator))
  10582. (if just-return-string
  10583. (org-no-properties res)
  10584. (org-unlogged-message "%s" res))))
  10585. (defvar org-refile-history nil
  10586. "History for refiling operations.")
  10587. (defvar org-after-refile-insert-hook nil
  10588. "Hook run after `org-refile' has inserted its stuff at the new location.
  10589. Note that this is still *before* the stuff will be removed from
  10590. the *old* location.")
  10591. (defvar org-capture-last-stored-marker)
  10592. (defvar org-refile-keep nil
  10593. "Non-nil means `org-refile' will copy instead of refile.")
  10594. (defun org-copy ()
  10595. "Like `org-refile', but copy."
  10596. (interactive)
  10597. (let ((org-refile-keep t))
  10598. (funcall 'org-refile nil nil nil "Copy")))
  10599. (defun org-refile (&optional arg default-buffer rfloc msg)
  10600. "Move the entry or entries at point to another heading.
  10601. The list of target headings is compiled using the information in
  10602. `org-refile-targets', which see.
  10603. At the target location, the entry is filed as a subitem of the
  10604. target heading. Depending on `org-reverse-note-order', the new
  10605. subitem will either be the first or the last subitem.
  10606. If there is an active region, all entries in that region will be
  10607. refiled. However, the region must fulfill the requirement that
  10608. the first heading sets the top-level of the moved text.
  10609. With prefix arg ARG, the command will only visit the target
  10610. location and not actually move anything.
  10611. With a double prefix arg \\[universal-argument] \\[universal-argument], go to the location where the last
  10612. refiling operation has put the subtree.
  10613. With a numeric prefix argument of `2', refile to the running clock.
  10614. With a numeric prefix argument of `3', emulate `org-refile-keep'
  10615. being set to t and copy to the target location, don't move it.
  10616. Beware that keeping refiled entries may result in duplicated ID
  10617. properties.
  10618. RFLOC can be a refile location obtained in a different way.
  10619. MSG is a string to replace \"Refile\" in the default prompt with
  10620. another verb. E.g. `org-copy' sets this parameter to \"Copy\".
  10621. See also `org-refile-use-outline-path'.
  10622. If you are using target caching (see `org-refile-use-cache'), you
  10623. have to clear the target cache in order to find new targets.
  10624. This can be done with a 0 prefix (`C-0 C-c C-w') or a triple
  10625. prefix argument (`C-u C-u C-u C-c C-w')."
  10626. (interactive "P")
  10627. (if (member arg '(0 (64)))
  10628. (org-refile-cache-clear)
  10629. (let* ((actionmsg (cond (msg msg)
  10630. ((equal arg 3) "Refile (and keep)")
  10631. (t "Refile")))
  10632. (regionp (org-region-active-p))
  10633. (region-start (and regionp (region-beginning)))
  10634. (region-end (and regionp (region-end)))
  10635. (org-refile-keep (if (equal arg 3) t org-refile-keep))
  10636. pos it nbuf file level reversed)
  10637. (setq last-command nil)
  10638. (when regionp
  10639. (goto-char region-start)
  10640. (or (bolp) (goto-char (point-at-bol)))
  10641. (setq region-start (point))
  10642. (unless (or (org-kill-is-subtree-p
  10643. (buffer-substring region-start region-end))
  10644. (prog1 org-refile-active-region-within-subtree
  10645. (let ((s (point-at-eol)))
  10646. (org-toggle-heading)
  10647. (setq region-end (+ (- (point-at-eol) s) region-end)))))
  10648. (user-error "The region is not a (sequence of) subtree(s)")))
  10649. (if (equal arg '(16))
  10650. (org-refile-goto-last-stored)
  10651. (when (or
  10652. (and (equal arg 2)
  10653. org-clock-hd-marker (marker-buffer org-clock-hd-marker)
  10654. (prog1
  10655. (setq it (list (or org-clock-heading "running clock")
  10656. (buffer-file-name
  10657. (marker-buffer org-clock-hd-marker))
  10658. ""
  10659. (marker-position org-clock-hd-marker)))
  10660. (setq arg nil)))
  10661. (setq it
  10662. (or rfloc
  10663. (let (heading-text)
  10664. (save-excursion
  10665. (unless (and arg (listp arg))
  10666. (org-back-to-heading t)
  10667. (setq heading-text
  10668. (replace-regexp-in-string
  10669. org-bracket-link-regexp
  10670. "\\3"
  10671. (nth 4 (org-heading-components)))))
  10672. (org-refile-get-location
  10673. (cond ((and arg (listp arg)) "Goto")
  10674. (regionp (concat actionmsg " region to"))
  10675. (t (concat actionmsg " subtree \""
  10676. heading-text "\" to")))
  10677. default-buffer
  10678. (and (not (equal '(4) arg))
  10679. org-refile-allow-creating-parent-nodes)))))))
  10680. (setq file (nth 1 it)
  10681. pos (nth 3 it))
  10682. (when (and (not arg)
  10683. pos
  10684. (equal (buffer-file-name) file)
  10685. (if regionp
  10686. (and (>= pos region-start)
  10687. (<= pos region-end))
  10688. (and (>= pos (point))
  10689. (< pos (save-excursion
  10690. (org-end-of-subtree t t))))))
  10691. (error "Cannot refile to position inside the tree or region"))
  10692. (setq nbuf (or (find-buffer-visiting file)
  10693. (find-file-noselect file)))
  10694. (if (and arg (not (equal arg 3)))
  10695. (progn
  10696. (org-pop-to-buffer-same-window nbuf)
  10697. (goto-char pos)
  10698. (org-show-context 'org-goto))
  10699. (if regionp
  10700. (progn
  10701. (org-kill-new (buffer-substring region-start region-end))
  10702. (org-save-markers-in-region region-start region-end))
  10703. (org-copy-subtree 1 nil t))
  10704. (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
  10705. (find-file-noselect file)))
  10706. (setq reversed (org-notes-order-reversed-p))
  10707. (save-excursion
  10708. (save-restriction
  10709. (widen)
  10710. (if pos
  10711. (progn
  10712. (goto-char pos)
  10713. (looking-at org-outline-regexp)
  10714. (setq level (org-get-valid-level (funcall outline-level) 1))
  10715. (goto-char
  10716. (if reversed
  10717. (or (outline-next-heading) (point-max))
  10718. (or (save-excursion (org-get-next-sibling))
  10719. (org-end-of-subtree t t)
  10720. (point-max)))))
  10721. (setq level 1)
  10722. (if (not reversed)
  10723. (goto-char (point-max))
  10724. (goto-char (point-min))
  10725. (or (outline-next-heading) (goto-char (point-max)))))
  10726. (unless (bolp) (newline))
  10727. (org-paste-subtree level nil nil t)
  10728. (when org-log-refile
  10729. (org-add-log-setup 'refile nil nil org-log-refile)
  10730. (unless (eq org-log-refile 'note)
  10731. (save-excursion (org-add-log-note))))
  10732. (and org-auto-align-tags
  10733. (let ((org-loop-over-headlines-in-active-region nil))
  10734. (org-set-tags nil t)))
  10735. (let ((bookmark-name (plist-get org-bookmark-names-plist
  10736. :last-refile)))
  10737. (when bookmark-name
  10738. (with-demoted-errors
  10739. (bookmark-set bookmark-name))))
  10740. ;; If we are refiling for capture, make sure that the
  10741. ;; last-capture pointers point here
  10742. (when (org-bound-and-true-p org-capture-is-refiling)
  10743. (let ((bookmark-name (plist-get org-bookmark-names-plist
  10744. :last-capture-marker)))
  10745. (when bookmark-name
  10746. (with-demoted-errors
  10747. (bookmark-set bookmark-name))))
  10748. (move-marker org-capture-last-stored-marker (point)))
  10749. (when (fboundp 'deactivate-mark) (deactivate-mark))
  10750. (run-hooks 'org-after-refile-insert-hook))))
  10751. (unless org-refile-keep
  10752. (if regionp
  10753. (delete-region (point) (+ (point) (- region-end region-start)))
  10754. (delete-region
  10755. (and (org-back-to-heading t) (point))
  10756. (min (1+ (buffer-size)) (org-end-of-subtree t t) (point)))))
  10757. (when (featurep 'org-inlinetask)
  10758. (org-inlinetask-remove-END-maybe))
  10759. (setq org-markers-to-move nil)
  10760. (message (concat actionmsg " to \"%s\" in file %s: done") (car it) file)))))))
  10761. (defun org-refile-goto-last-stored ()
  10762. "Go to the location where the last refile was stored."
  10763. (interactive)
  10764. (bookmark-jump (plist-get org-bookmark-names-plist :last-refile))
  10765. (message "This is the location of the last refile"))
  10766. (defun org-refile--get-location (refloc tbl)
  10767. "When user refile to REFLOC, find the associated target in TBL.
  10768. Also check `org-refile-target-table'."
  10769. (car (delq
  10770. nil
  10771. (mapcar
  10772. (lambda (r) (or (assoc r tbl)
  10773. (assoc r org-refile-target-table)))
  10774. (list (replace-regexp-in-string "/$" "" refloc)
  10775. (replace-regexp-in-string "\\([^/]\\)$" "\\1/" refloc))))))
  10776. (defun org-refile-get-location (&optional prompt default-buffer new-nodes)
  10777. "Prompt the user for a refile location, using PROMPT.
  10778. PROMPT should not be suffixed with a colon and a space, because
  10779. this function appends the default value from
  10780. `org-refile-history' automatically, if that is not empty."
  10781. (let ((org-refile-targets org-refile-targets)
  10782. (org-refile-use-outline-path org-refile-use-outline-path))
  10783. (setq org-refile-target-table (org-refile-get-targets default-buffer)))
  10784. (unless org-refile-target-table
  10785. (user-error "No refile targets"))
  10786. (let* ((cbuf (current-buffer))
  10787. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  10788. (cfunc (if (and org-refile-use-outline-path
  10789. org-outline-path-complete-in-steps)
  10790. #'org-olpath-completing-read
  10791. #'completing-read))
  10792. (extra (if org-refile-use-outline-path "/" ""))
  10793. (cbnex (concat (buffer-name) extra))
  10794. (filename (and cfn (expand-file-name cfn)))
  10795. (tbl (mapcar
  10796. (lambda (x)
  10797. (if (and (not (member org-refile-use-outline-path
  10798. '(file full-file-path)))
  10799. (not (equal filename (nth 1 x))))
  10800. (cons (concat (car x) extra " ("
  10801. (file-name-nondirectory (nth 1 x)) ")")
  10802. (cdr x))
  10803. (cons (concat (car x) extra) (cdr x))))
  10804. org-refile-target-table))
  10805. (completion-ignore-case t)
  10806. cdef
  10807. (prompt (concat prompt
  10808. (or (and (car org-refile-history)
  10809. (concat " (default " (car org-refile-history) ")"))
  10810. (and (assoc cbnex tbl) (setq cdef cbnex)
  10811. (concat " (default " cbnex ")"))) ": "))
  10812. pa answ parent-target child parent old-hist)
  10813. (setq old-hist org-refile-history)
  10814. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  10815. nil 'org-refile-history (or cdef (car org-refile-history))))
  10816. (if (setq pa (org-refile--get-location answ tbl))
  10817. (progn
  10818. (org-refile-check-position pa)
  10819. (when (or (not org-refile-history)
  10820. (not (eq old-hist org-refile-history))
  10821. (not (equal (car pa) (car org-refile-history))))
  10822. (setq org-refile-history
  10823. (cons (car pa) (if (assoc (car org-refile-history) tbl)
  10824. org-refile-history
  10825. (cdr org-refile-history))))
  10826. (when (equal (car org-refile-history) (nth 1 org-refile-history))
  10827. (pop org-refile-history)))
  10828. pa)
  10829. (if (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  10830. (progn
  10831. (setq parent (match-string 1 answ)
  10832. child (match-string 2 answ))
  10833. (setq parent-target (org-refile--get-location parent tbl))
  10834. (when (and parent-target
  10835. (or (eq new-nodes t)
  10836. (and (eq new-nodes 'confirm)
  10837. (y-or-n-p (format "Create new node \"%s\"? "
  10838. child)))))
  10839. (org-refile-new-child parent-target child)))
  10840. (user-error "Invalid target location")))))
  10841. (declare-function org-string-nw-p "org-macs" (s))
  10842. (defun org-refile-check-position (refile-pointer)
  10843. "Check if the refile pointer matches the headline to which it points."
  10844. (let* ((file (nth 1 refile-pointer))
  10845. (re (nth 2 refile-pointer))
  10846. (pos (nth 3 refile-pointer))
  10847. buffer)
  10848. (if (and (not (markerp pos)) (not file))
  10849. (user-error "Please indicate a target file in the refile path")
  10850. (when (org-string-nw-p re)
  10851. (setq buffer (if (markerp pos)
  10852. (marker-buffer pos)
  10853. (or (find-buffer-visiting file)
  10854. (find-file-noselect file))))
  10855. (with-current-buffer buffer
  10856. (save-excursion
  10857. (save-restriction
  10858. (widen)
  10859. (goto-char pos)
  10860. (beginning-of-line 1)
  10861. (unless (org-looking-at-p re)
  10862. (user-error "Invalid refile position, please clear the cache with `C-0 C-c C-w' before refiling")))))))))
  10863. (defun org-refile-new-child (parent-target child)
  10864. "Use refile target PARENT-TARGET to add new CHILD below it."
  10865. (unless parent-target
  10866. (error "Cannot find parent for new node"))
  10867. (let ((file (nth 1 parent-target))
  10868. (pos (nth 3 parent-target))
  10869. level)
  10870. (with-current-buffer (or (find-buffer-visiting file)
  10871. (find-file-noselect file))
  10872. (save-excursion
  10873. (save-restriction
  10874. (widen)
  10875. (if pos
  10876. (goto-char pos)
  10877. (goto-char (point-max))
  10878. (unless (bolp) (newline)))
  10879. (when (looking-at org-outline-regexp)
  10880. (setq level (funcall outline-level))
  10881. (org-end-of-subtree t t))
  10882. (org-back-over-empty-lines)
  10883. (insert "\n" (make-string
  10884. (if pos (org-get-valid-level level 1) 1) ?*)
  10885. " " child "\n")
  10886. (beginning-of-line 0)
  10887. (list (concat (car parent-target) "/" child) file "" (point)))))))
  10888. (defun org-olpath-completing-read (prompt collection &rest args)
  10889. "Read an outline path like a file name."
  10890. (let ((thetable collection))
  10891. (apply #'completing-read
  10892. prompt
  10893. (lambda (string predicate &optional flag)
  10894. (cond
  10895. ((eq flag nil) (try-completion string thetable))
  10896. ((eq flag t)
  10897. (let ((l (length string)))
  10898. (mapcar (lambda (x)
  10899. (let ((r (substring x l))
  10900. (f (if (string-match " ([^)]*)$" x)
  10901. (match-string 0 x)
  10902. "")))
  10903. (if (string-match "/" r)
  10904. (concat string (substring r 0 (match-end 0)) f)
  10905. x)))
  10906. (all-completions string thetable predicate))))
  10907. ;; Exact match?
  10908. ((eq flag 'lambda) (assoc string thetable))))
  10909. args)))
  10910. ;;;; Dynamic blocks
  10911. (defun org-find-dblock (name)
  10912. "Find the first dynamic block with name NAME in the buffer.
  10913. If not found, stay at current position and return nil."
  10914. (let ((case-fold-search t) pos)
  10915. (save-excursion
  10916. (goto-char (point-min))
  10917. (setq pos (and (re-search-forward
  10918. (concat "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+" name "\\>") nil t)
  10919. (match-beginning 0))))
  10920. (when pos (goto-char pos))
  10921. pos))
  10922. (defun org-create-dblock (plist)
  10923. "Create a dynamic block section, with parameters taken from PLIST.
  10924. PLIST must contain a :name entry which is used as the name of the block."
  10925. (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
  10926. (end-of-line 1)
  10927. (newline))
  10928. (let ((col (current-column))
  10929. (name (plist-get plist :name)))
  10930. (insert "#+BEGIN: " name)
  10931. (while plist
  10932. (if (eq (car plist) :name)
  10933. (setq plist (cddr plist))
  10934. (insert " " (prin1-to-string (pop plist)))))
  10935. (insert "\n\n" (make-string col ?\ ) "#+END:\n")
  10936. (beginning-of-line -2)))
  10937. (defun org-prepare-dblock ()
  10938. "Prepare dynamic block for refresh.
  10939. This empties the block, puts the cursor at the insert position and returns
  10940. the property list including an extra property :name with the block name."
  10941. (unless (looking-at org-dblock-start-re)
  10942. (user-error "Not at a dynamic block"))
  10943. (let* ((begdel (1+ (match-end 0)))
  10944. (name (org-no-properties (match-string 1)))
  10945. (params (append (list :name name)
  10946. (read (concat "(" (match-string 3) ")")))))
  10947. (save-excursion
  10948. (beginning-of-line 1)
  10949. (skip-chars-forward " \t")
  10950. (setq params (plist-put params :indentation-column (current-column))))
  10951. (unless (re-search-forward org-dblock-end-re nil t)
  10952. (error "Dynamic block not terminated"))
  10953. (setq params
  10954. (append params
  10955. (list :content (buffer-substring
  10956. begdel (match-beginning 0)))))
  10957. (delete-region begdel (match-beginning 0))
  10958. (goto-char begdel)
  10959. (open-line 1)
  10960. params))
  10961. (defun org-map-dblocks (&optional command)
  10962. "Apply COMMAND to all dynamic blocks in the current buffer.
  10963. If COMMAND is not given, use `org-update-dblock'."
  10964. (let ((cmd (or command 'org-update-dblock)))
  10965. (save-excursion
  10966. (goto-char (point-min))
  10967. (while (re-search-forward org-dblock-start-re nil t)
  10968. (goto-char (match-beginning 0))
  10969. (save-excursion
  10970. (condition-case nil
  10971. (funcall cmd)
  10972. (error (message "Error during update of dynamic block"))))
  10973. (unless (re-search-forward org-dblock-end-re nil t)
  10974. (error "Dynamic block not terminated"))))))
  10975. (defun org-dblock-update (&optional arg)
  10976. "User command for updating dynamic blocks.
  10977. Update the dynamic block at point. With prefix ARG, update all dynamic
  10978. blocks in the buffer."
  10979. (interactive "P")
  10980. (if arg
  10981. (org-update-all-dblocks)
  10982. (or (looking-at org-dblock-start-re)
  10983. (org-beginning-of-dblock))
  10984. (org-update-dblock)))
  10985. (defun org-update-dblock ()
  10986. "Update the dynamic block at point.
  10987. This means to empty the block, parse for parameters and then call
  10988. the correct writing function."
  10989. (interactive)
  10990. (save-excursion
  10991. (let* ((win (selected-window))
  10992. (pos (point))
  10993. (line (org-current-line))
  10994. (params (org-prepare-dblock))
  10995. (name (plist-get params :name))
  10996. (indent (plist-get params :indentation-column))
  10997. (cmd (intern (concat "org-dblock-write:" name))))
  10998. (message "Updating dynamic block `%s' at line %d..." name line)
  10999. (funcall cmd params)
  11000. (message "Updating dynamic block `%s' at line %d...done" name line)
  11001. (goto-char pos)
  11002. (when (and indent (> indent 0))
  11003. (setq indent (make-string indent ?\ ))
  11004. (save-excursion
  11005. (select-window win)
  11006. (org-beginning-of-dblock)
  11007. (forward-line 1)
  11008. (while (not (looking-at org-dblock-end-re))
  11009. (insert indent)
  11010. (beginning-of-line 2))
  11011. (when (looking-at org-dblock-end-re)
  11012. (and (looking-at "[ \t]+")
  11013. (replace-match ""))
  11014. (insert indent)))))))
  11015. (defun org-beginning-of-dblock ()
  11016. "Find the beginning of the dynamic block at point.
  11017. Error if there is no such block at point."
  11018. (let ((pos (point))
  11019. beg)
  11020. (end-of-line 1)
  11021. (if (and (re-search-backward org-dblock-start-re nil t)
  11022. (setq beg (match-beginning 0))
  11023. (re-search-forward org-dblock-end-re nil t)
  11024. (> (match-end 0) pos))
  11025. (goto-char beg)
  11026. (goto-char pos)
  11027. (error "Not in a dynamic block"))))
  11028. (defun org-update-all-dblocks ()
  11029. "Update all dynamic blocks in the buffer.
  11030. This function can be used in a hook."
  11031. (interactive)
  11032. (when (derived-mode-p 'org-mode)
  11033. (org-map-dblocks 'org-update-dblock)))
  11034. ;;;; Completion
  11035. (declare-function org-export-backend-name "org-export" (cl-x))
  11036. (declare-function org-export-backend-options "org-export" (cl-x))
  11037. (defun org-get-export-keywords ()
  11038. "Return a list of all currently understood export keywords.
  11039. Export keywords include options, block names, attributes and
  11040. keywords relative to each registered export back-end."
  11041. (let (keywords)
  11042. (dolist (backend
  11043. (org-bound-and-true-p org-export-registered-backends)
  11044. (delq nil keywords))
  11045. ;; Back-end name (for keywords, like #+LATEX:)
  11046. (push (upcase (symbol-name (org-export-backend-name backend))) keywords)
  11047. (dolist (option-entry (org-export-backend-options backend))
  11048. ;; Back-end options.
  11049. (push (nth 1 option-entry) keywords)))))
  11050. (defconst org-options-keywords
  11051. '("ARCHIVE:" "AUTHOR:" "BIND:" "CATEGORY:" "COLUMNS:" "CREATOR:" "DATE:"
  11052. "DESCRIPTION:" "DRAWERS:" "EMAIL:" "EXCLUDE_TAGS:" "FILETAGS:" "INCLUDE:"
  11053. "INDEX:" "KEYWORDS:" "LANGUAGE:" "MACRO:" "OPTIONS:" "PROPERTY:"
  11054. "PRIORITIES:" "SELECT_TAGS:" "SEQ_TODO:" "SETUPFILE:" "STARTUP:" "TAGS:"
  11055. "TITLE:" "TODO:" "TYP_TODO:" "SELECT_TAGS:" "EXCLUDE_TAGS:"))
  11056. (defcustom org-structure-template-alist
  11057. '(("s" "#+BEGIN_SRC ?\n\n#+END_SRC")
  11058. ("e" "#+BEGIN_EXAMPLE\n?\n#+END_EXAMPLE")
  11059. ("q" "#+BEGIN_QUOTE\n?\n#+END_QUOTE")
  11060. ("v" "#+BEGIN_VERSE\n?\n#+END_VERSE")
  11061. ("V" "#+BEGIN_VERBATIM\n?\n#+END_VERBATIM")
  11062. ("c" "#+BEGIN_CENTER\n?\n#+END_CENTER")
  11063. ("l" "#+BEGIN_EXPORT latex\n?\n#+END_EXPORT")
  11064. ("L" "#+LaTeX: ")
  11065. ("h" "#+BEGIN_EXPORT html\n?\n#+END_EXPORT")
  11066. ("H" "#+HTML: ")
  11067. ("a" "#+BEGIN_EXPORT ascii\n?\n#+END_EXPORT")
  11068. ("A" "#+ASCII: ")
  11069. ("i" "#+INDEX: ?")
  11070. ("I" "#+INCLUDE: %file ?"))
  11071. "Structure completion elements.
  11072. This is a list of abbreviation keys and values. The value gets inserted
  11073. if you type `<' followed by the key and then press the completion key,
  11074. usually `TAB'. %file will be replaced by a file name after prompting
  11075. for the file using completion. The cursor will be placed at the position
  11076. of the `?' in the template.
  11077. There are two templates for each key, the first uses the original Org syntax,
  11078. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  11079. the default when the /org-mtags.el/ module has been loaded. See also the
  11080. variable `org-mtags-prefer-muse-templates'."
  11081. :group 'org-completion
  11082. :type '(repeat
  11083. (list
  11084. (string :tag "Key")
  11085. (string :tag "Template")))
  11086. :version "25.1"
  11087. :package-version '(Org . "8.3"))
  11088. (defun org-try-structure-completion ()
  11089. "Try to complete a structure template before point.
  11090. This looks for strings like \"<e\" on an otherwise empty line and
  11091. expands them."
  11092. (let ((l (buffer-substring (point-at-bol) (point)))
  11093. a)
  11094. (when (and (looking-at "[ \t]*$")
  11095. (string-match "^[ \t]*<\\([a-zA-Z]+\\)$" l)
  11096. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  11097. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  11098. (match-beginning 1)) a)
  11099. t)))
  11100. (defun org-complete-expand-structure-template (start cell)
  11101. "Expand a structure template."
  11102. (let ((rpl (nth 1 cell))
  11103. (ind ""))
  11104. (delete-region start (point))
  11105. (when (string-match "\\`[ \t]*#\\+" rpl)
  11106. (cond
  11107. ((bolp))
  11108. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  11109. (setq ind (buffer-substring (point-at-bol) (point))))
  11110. (t (newline))))
  11111. (setq start (point))
  11112. (when (string-match "%file" rpl)
  11113. (setq rpl (replace-match
  11114. (concat
  11115. "\""
  11116. (save-match-data
  11117. (abbreviate-file-name (read-file-name "Include file: ")))
  11118. "\"")
  11119. t t rpl)))
  11120. (setq rpl (mapconcat 'identity (split-string rpl "\n")
  11121. (concat "\n" ind)))
  11122. (insert rpl)
  11123. (when (re-search-backward "\\?" start t) (delete-char 1))))
  11124. ;;;; TODO, DEADLINE, Comments
  11125. (defun org-toggle-comment ()
  11126. "Change the COMMENT state of an entry."
  11127. (interactive)
  11128. (save-excursion
  11129. (org-back-to-heading)
  11130. (looking-at org-complex-heading-regexp)
  11131. (goto-char (or (match-end 3) (match-end 2) (match-end 1)))
  11132. (skip-chars-forward " \t")
  11133. (unless (memq (char-before) '(?\s ?\t)) (insert " "))
  11134. (if (org-in-commented-heading-p t)
  11135. (delete-region (point)
  11136. (progn (search-forward " " (line-end-position) 'move)
  11137. (skip-chars-forward " \t")
  11138. (point)))
  11139. (insert org-comment-string)
  11140. (unless (eolp) (insert " ")))))
  11141. (defvar org-last-todo-state-is-todo nil
  11142. "This is non-nil when the last TODO state change led to a TODO state.
  11143. If the last change removed the TODO tag or switched to DONE, then
  11144. this is nil.")
  11145. (defvar org-setting-tags nil) ; dynamically skipped
  11146. (defvar org-todo-setup-filter-hook nil
  11147. "Hook for functions that pre-filter todo specs.
  11148. Each function takes a todo spec and returns either nil or the spec
  11149. transformed into canonical form." )
  11150. (defvar org-todo-get-default-hook nil
  11151. "Hook for functions that get a default item for todo.
  11152. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  11153. nil or a string to be used for the todo mark." )
  11154. (defvar org-agenda-headline-snapshot-before-repeat)
  11155. (defun org-current-effective-time ()
  11156. "Return current time adjusted for `org-extend-today-until' variable."
  11157. (let* ((ct (org-current-time))
  11158. (dct (decode-time ct))
  11159. (ct1
  11160. (cond
  11161. (org-use-last-clock-out-time-as-effective-time
  11162. (or (org-clock-get-last-clock-out-time) ct))
  11163. ((and org-use-effective-time (< (nth 2 dct) org-extend-today-until))
  11164. (encode-time 0 59 23 (1- (nth 3 dct)) (nth 4 dct) (nth 5 dct)))
  11165. (t ct))))
  11166. ct1))
  11167. (defun org-todo-yesterday (&optional arg)
  11168. "Like `org-todo' but the time of change will be 23:59 of yesterday."
  11169. (interactive "P")
  11170. (if (eq major-mode 'org-agenda-mode)
  11171. (apply 'org-agenda-todo-yesterday arg)
  11172. (let* ((org-use-effective-time t)
  11173. (hour (nth 2 (decode-time (org-current-time))))
  11174. (org-extend-today-until (1+ hour)))
  11175. (org-todo arg))))
  11176. (defvar org-block-entry-blocking ""
  11177. "First entry preventing the TODO state change.")
  11178. (defun org-cancel-repeater ()
  11179. "Cancel a repeater by setting its numeric value to zero."
  11180. (interactive)
  11181. (save-excursion
  11182. (org-back-to-heading t)
  11183. (let ((bound1 (point))
  11184. (bound0 (save-excursion (outline-next-heading) (point))))
  11185. (when (and (re-search-forward
  11186. (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  11187. org-deadline-time-regexp "\\)\\|\\("
  11188. org-ts-regexp "\\)")
  11189. bound0 t)
  11190. (re-search-backward "[ \t]+\\(?:[.+]\\)?\\+\\([0-9]+\\)[hdwmy]"
  11191. bound1 t))
  11192. (replace-match "0" t nil nil 1)))))
  11193. (defvar org-state) ;; dynamically scoped into this function
  11194. (defun org-todo (&optional arg)
  11195. "Change the TODO state of an item.
  11196. The state of an item is given by a keyword at the start of the heading,
  11197. like
  11198. *** TODO Write paper
  11199. *** DONE Call mom
  11200. The different keywords are specified in the variable `org-todo-keywords'.
  11201. By default the available states are \"TODO\" and \"DONE\".
  11202. So for this example: when the item starts with TODO, it is changed to DONE.
  11203. When it starts with DONE, the DONE is removed. And when neither TODO nor
  11204. DONE are present, add TODO at the beginning of the heading.
  11205. With \\[universal-argument] prefix arg, use completion to determine the new \
  11206. state.
  11207. With numeric prefix arg, switch to that state.
  11208. With a double \\[universal-argument] prefix, switch to the next set of TODO \
  11209. keywords (nextset).
  11210. With a triple \\[universal-argument] prefix, circumvent any state blocking.
  11211. With a numeric prefix arg of 0, inhibit note taking for the change.
  11212. With a numeric prefix arg of -1, cancel repeater to allow marking as DONE.
  11213. When called through ELisp, arg is also interpreted in the following way:
  11214. `none' -> empty state
  11215. \"\"(empty string) -> switch to empty state
  11216. `done' -> switch to DONE
  11217. `nextset' -> switch to the next set of keywords
  11218. `previousset' -> switch to the previous set of keywords
  11219. \"WAITING\" -> switch to the specified keyword, but only if it
  11220. really is a member of `org-todo-keywords'."
  11221. (interactive "P")
  11222. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  11223. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11224. 'region-start-level 'region))
  11225. org-loop-over-headlines-in-active-region)
  11226. (org-map-entries
  11227. `(org-todo ,arg)
  11228. org-loop-over-headlines-in-active-region
  11229. cl (when (outline-invisible-p) (org-end-of-subtree nil t))))
  11230. (when (equal arg '(16)) (setq arg 'nextset))
  11231. (when (equal arg -1) (org-cancel-repeater) (setq arg nil))
  11232. (let ((org-blocker-hook org-blocker-hook)
  11233. commentp
  11234. case-fold-search)
  11235. (when (equal arg '(64))
  11236. (setq arg nil org-blocker-hook nil))
  11237. (when (and org-blocker-hook
  11238. (or org-inhibit-blocking
  11239. (org-entry-get nil "NOBLOCKING")))
  11240. (setq org-blocker-hook nil))
  11241. (save-excursion
  11242. (catch 'exit
  11243. (org-back-to-heading t)
  11244. (when (org-in-commented-heading-p t)
  11245. (org-toggle-comment)
  11246. (setq commentp t))
  11247. (when (looking-at org-outline-regexp) (goto-char (1- (match-end 0))))
  11248. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|[ \t]*$\\)"))
  11249. (looking-at "\\(?: *\\|[ \t]*$\\)"))
  11250. (let* ((match-data (match-data))
  11251. (startpos (point-at-bol))
  11252. (logging (save-match-data (org-entry-get nil "LOGGING" t t)))
  11253. (org-log-done org-log-done)
  11254. (org-log-repeat org-log-repeat)
  11255. (org-todo-log-states org-todo-log-states)
  11256. (org-inhibit-logging
  11257. (if (equal arg 0)
  11258. (progn (setq arg nil) 'note) org-inhibit-logging))
  11259. (this (match-string 1))
  11260. (hl-pos (match-beginning 0))
  11261. (head (org-get-todo-sequence-head this))
  11262. (ass (assoc head org-todo-kwd-alist))
  11263. (interpret (nth 1 ass))
  11264. (done-word (nth 3 ass))
  11265. (final-done-word (nth 4 ass))
  11266. (org-last-state (or this ""))
  11267. (completion-ignore-case t)
  11268. (member (member this org-todo-keywords-1))
  11269. (tail (cdr member))
  11270. (org-state (cond
  11271. ((and org-todo-key-trigger
  11272. (or (and (equal arg '(4))
  11273. (eq org-use-fast-todo-selection 'prefix))
  11274. (and (not arg) org-use-fast-todo-selection
  11275. (not (eq org-use-fast-todo-selection
  11276. 'prefix)))))
  11277. ;; Use fast selection
  11278. (org-fast-todo-selection))
  11279. ((and (equal arg '(4))
  11280. (or (not org-use-fast-todo-selection)
  11281. (not org-todo-key-trigger)))
  11282. ;; Read a state with completion
  11283. (completing-read
  11284. "State: " (mapcar #'list org-todo-keywords-1)
  11285. nil t))
  11286. ((eq arg 'right)
  11287. (if this
  11288. (if tail (car tail) nil)
  11289. (car org-todo-keywords-1)))
  11290. ((eq arg 'left)
  11291. (unless (equal member org-todo-keywords-1)
  11292. (if this
  11293. (nth (- (length org-todo-keywords-1)
  11294. (length tail) 2)
  11295. org-todo-keywords-1)
  11296. (org-last org-todo-keywords-1))))
  11297. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  11298. (setq arg nil))) ; hack to fall back to cycling
  11299. (arg
  11300. ;; user or caller requests a specific state
  11301. (cond
  11302. ((equal arg "") nil)
  11303. ((eq arg 'none) nil)
  11304. ((eq arg 'done) (or done-word (car org-done-keywords)))
  11305. ((eq arg 'nextset)
  11306. (or (car (cdr (member head org-todo-heads)))
  11307. (car org-todo-heads)))
  11308. ((eq arg 'previousset)
  11309. (let ((org-todo-heads (reverse org-todo-heads)))
  11310. (or (car (cdr (member head org-todo-heads)))
  11311. (car org-todo-heads))))
  11312. ((car (member arg org-todo-keywords-1)))
  11313. ((stringp arg)
  11314. (user-error "State `%s' not valid in this file" arg))
  11315. ((nth (1- (prefix-numeric-value arg))
  11316. org-todo-keywords-1))))
  11317. ((null member) (or head (car org-todo-keywords-1)))
  11318. ((equal this final-done-word) nil) ;; -> make empty
  11319. ((null tail) nil) ;; -> first entry
  11320. ((memq interpret '(type priority))
  11321. (if (eq this-command last-command)
  11322. (car tail)
  11323. (if (> (length tail) 0)
  11324. (or done-word (car org-done-keywords))
  11325. nil)))
  11326. (t
  11327. (car tail))))
  11328. (org-state (or
  11329. (run-hook-with-args-until-success
  11330. 'org-todo-get-default-hook org-state org-last-state)
  11331. org-state))
  11332. (next (if org-state (concat " " org-state " ") " "))
  11333. (change-plist (list :type 'todo-state-change :from this :to org-state
  11334. :position startpos))
  11335. dolog now-done-p)
  11336. (when org-blocker-hook
  11337. (setq org-last-todo-state-is-todo
  11338. (not (member this org-done-keywords)))
  11339. (unless (save-excursion
  11340. (save-match-data
  11341. (org-with-wide-buffer
  11342. (run-hook-with-args-until-failure
  11343. 'org-blocker-hook change-plist))))
  11344. (if (org-called-interactively-p 'interactive)
  11345. (user-error "TODO state change from %s to %s blocked (by \"%s\")"
  11346. this org-state org-block-entry-blocking)
  11347. ;; fail silently
  11348. (message "TODO state change from %s to %s blocked (by \"%s\")"
  11349. this org-state org-block-entry-blocking)
  11350. (throw 'exit nil))))
  11351. (store-match-data match-data)
  11352. (replace-match next t t)
  11353. (cond ((equal this org-state)
  11354. (message "TODO state was already %s" (org-trim next)))
  11355. ((pos-visible-in-window-p hl-pos)
  11356. (message "TODO state changed to %s" (org-trim next))))
  11357. (unless head
  11358. (setq head (org-get-todo-sequence-head org-state)
  11359. ass (assoc head org-todo-kwd-alist)
  11360. interpret (nth 1 ass)
  11361. done-word (nth 3 ass)
  11362. final-done-word (nth 4 ass)))
  11363. (when (memq arg '(nextset previousset))
  11364. (message "Keyword-Set %d/%d: %s"
  11365. (- (length org-todo-sets) -1
  11366. (length (memq (assoc org-state org-todo-sets) org-todo-sets)))
  11367. (length org-todo-sets)
  11368. (mapconcat 'identity (assoc org-state org-todo-sets) " ")))
  11369. (setq org-last-todo-state-is-todo
  11370. (not (member org-state org-done-keywords)))
  11371. (setq now-done-p (and (member org-state org-done-keywords)
  11372. (not (member this org-done-keywords))))
  11373. (and logging (org-local-logging logging))
  11374. (when (and (or org-todo-log-states org-log-done)
  11375. (not (eq org-inhibit-logging t))
  11376. (not (memq arg '(nextset previousset))))
  11377. ;; we need to look at recording a time and note
  11378. (setq dolog (or (nth 1 (assoc org-state org-todo-log-states))
  11379. (nth 2 (assoc this org-todo-log-states))))
  11380. (when (and (eq dolog 'note) (eq org-inhibit-logging 'note))
  11381. (setq dolog 'time))
  11382. (when (or (and (not org-state) (not org-closed-keep-when-no-todo))
  11383. (and org-state
  11384. (member org-state org-not-done-keywords)
  11385. (not (member this org-not-done-keywords))))
  11386. ;; This is now a todo state and was not one before
  11387. ;; If there was a CLOSED time stamp, get rid of it.
  11388. (org-add-planning-info nil nil 'closed))
  11389. (when (and now-done-p org-log-done)
  11390. ;; It is now done, and it was not done before
  11391. (org-add-planning-info 'closed (org-current-effective-time))
  11392. (when (and (not dolog) (eq 'note org-log-done))
  11393. (org-add-log-setup 'done org-state this 'note)))
  11394. (when (and org-state dolog)
  11395. ;; This is a non-nil state, and we need to log it
  11396. (org-add-log-setup 'state org-state this dolog)))
  11397. ;; Fixup tag positioning
  11398. (org-todo-trigger-tag-changes org-state)
  11399. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  11400. (when org-provide-todo-statistics
  11401. (org-update-parent-todo-statistics))
  11402. (run-hooks 'org-after-todo-state-change-hook)
  11403. (when (and arg (not (member org-state org-done-keywords)))
  11404. (setq head (org-get-todo-sequence-head org-state)))
  11405. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  11406. ;; Do we need to trigger a repeat?
  11407. (when now-done-p
  11408. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  11409. ;; This is for the agenda, take a snapshot of the headline.
  11410. (save-match-data
  11411. (setq org-agenda-headline-snapshot-before-repeat
  11412. (org-get-heading))))
  11413. (org-auto-repeat-maybe org-state))
  11414. ;; Fixup cursor location if close to the keyword
  11415. (when (and (outline-on-heading-p)
  11416. (not (bolp))
  11417. (save-excursion (beginning-of-line 1)
  11418. (looking-at org-todo-line-regexp))
  11419. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  11420. (goto-char (or (match-end 2) (match-end 1)))
  11421. (and (looking-at " ") (just-one-space)))
  11422. (when org-trigger-hook
  11423. (save-excursion
  11424. (run-hook-with-args 'org-trigger-hook change-plist)))
  11425. (when commentp (org-toggle-comment))))))))
  11426. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  11427. "Block turning an entry into a TODO, using the hierarchy.
  11428. This checks whether the current task should be blocked from state
  11429. changes. Such blocking occurs when:
  11430. 1. The task has children which are not all in a completed state.
  11431. 2. A task has a parent with the property :ORDERED:, and there
  11432. are siblings prior to the current task with incomplete
  11433. status.
  11434. 3. The parent of the task is blocked because it has siblings that should
  11435. be done first, or is child of a block grandparent TODO entry."
  11436. (if (not org-enforce-todo-dependencies)
  11437. t ; if locally turned off don't block
  11438. (catch 'dont-block
  11439. ;; If this is not a todo state change, or if this entry is already DONE,
  11440. ;; do not block
  11441. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  11442. (member (plist-get change-plist :from)
  11443. (cons 'done org-done-keywords))
  11444. (member (plist-get change-plist :to)
  11445. (cons 'todo org-not-done-keywords))
  11446. (not (plist-get change-plist :to)))
  11447. (throw 'dont-block t))
  11448. ;; If this task has children, and any are undone, it's blocked
  11449. (save-excursion
  11450. (org-back-to-heading t)
  11451. (let ((this-level (funcall outline-level)))
  11452. (outline-next-heading)
  11453. (let ((child-level (funcall outline-level)))
  11454. (while (and (not (eobp))
  11455. (> child-level this-level))
  11456. ;; this todo has children, check whether they are all
  11457. ;; completed
  11458. (when (and (not (org-entry-is-done-p))
  11459. (org-entry-is-todo-p))
  11460. (setq org-block-entry-blocking (org-get-heading))
  11461. (throw 'dont-block nil))
  11462. (outline-next-heading)
  11463. (setq child-level (funcall outline-level))))))
  11464. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  11465. ;; any previous siblings are undone, it's blocked
  11466. (save-excursion
  11467. (org-back-to-heading t)
  11468. (let* ((pos (point))
  11469. (parent-pos (and (org-up-heading-safe) (point))))
  11470. (unless parent-pos (throw 'dont-block t)) ; no parent
  11471. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  11472. (forward-line 1)
  11473. (re-search-forward org-not-done-heading-regexp pos t))
  11474. (setq org-block-entry-blocking (match-string 0))
  11475. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  11476. ;; Search further up the hierarchy, to see if an ancestor is blocked
  11477. (while t
  11478. (goto-char parent-pos)
  11479. (unless (looking-at org-not-done-heading-regexp)
  11480. (throw 'dont-block t)) ; do not block, parent is not a TODO
  11481. (setq pos (point))
  11482. (setq parent-pos (and (org-up-heading-safe) (point)))
  11483. (unless parent-pos (throw 'dont-block t)) ; no parent
  11484. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  11485. (forward-line 1)
  11486. (re-search-forward org-not-done-heading-regexp pos t)
  11487. (setq org-block-entry-blocking (org-get-heading)))
  11488. (throw 'dont-block nil)))))))) ; block, older sibling not done.
  11489. (defcustom org-track-ordered-property-with-tag nil
  11490. "Should the ORDERED property also be shown as a tag?
  11491. The ORDERED property decides if an entry should require subtasks to be
  11492. completed in sequence. Since a property is not very visible, setting
  11493. this option means that toggling the ORDERED property with the command
  11494. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  11495. not relevant for the behavior, but it makes things more visible.
  11496. Note that toggling the tag with tags commands will not change the property
  11497. and therefore not influence behavior!
  11498. This can be t, meaning the tag ORDERED should be used, It can also be a
  11499. string to select a different tag for this task."
  11500. :group 'org-todo
  11501. :type '(choice
  11502. (const :tag "No tracking" nil)
  11503. (const :tag "Track with ORDERED tag" t)
  11504. (string :tag "Use other tag")))
  11505. (defun org-toggle-ordered-property ()
  11506. "Toggle the ORDERED property of the current entry.
  11507. For better visibility, you can track the value of this property with a tag.
  11508. See variable `org-track-ordered-property-with-tag'."
  11509. (interactive)
  11510. (let* ((t1 org-track-ordered-property-with-tag)
  11511. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  11512. (save-excursion
  11513. (org-back-to-heading)
  11514. (if (org-entry-get nil "ORDERED")
  11515. (progn
  11516. (org-delete-property "ORDERED")
  11517. (and tag (org-toggle-tag tag 'off))
  11518. (message "Subtasks can be completed in arbitrary order"))
  11519. (org-entry-put nil "ORDERED" "t")
  11520. (and tag (org-toggle-tag tag 'on))
  11521. (message "Subtasks must be completed in sequence")))))
  11522. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  11523. (defun org-block-todo-from-checkboxes (change-plist)
  11524. "Block turning an entry into a TODO, using checkboxes.
  11525. This checks whether the current task should be blocked from state
  11526. changes because there are unchecked boxes in this entry."
  11527. (if (not org-enforce-todo-checkbox-dependencies)
  11528. t ; if locally turned off don't block
  11529. (catch 'dont-block
  11530. ;; If this is not a todo state change, or if this entry is already DONE,
  11531. ;; do not block
  11532. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  11533. (member (plist-get change-plist :from)
  11534. (cons 'done org-done-keywords))
  11535. (member (plist-get change-plist :to)
  11536. (cons 'todo org-not-done-keywords))
  11537. (not (plist-get change-plist :to)))
  11538. (throw 'dont-block t))
  11539. ;; If this task has checkboxes that are not checked, it's blocked
  11540. (save-excursion
  11541. (org-back-to-heading t)
  11542. (let ((beg (point)) end)
  11543. (outline-next-heading)
  11544. (setq end (point))
  11545. (goto-char beg)
  11546. (when (org-list-search-forward
  11547. (concat (org-item-beginning-re)
  11548. "\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?"
  11549. "\\[[- ]\\]")
  11550. end t)
  11551. (when (boundp 'org-blocked-by-checkboxes)
  11552. (setq org-blocked-by-checkboxes t))
  11553. (throw 'dont-block nil))))
  11554. t))) ; do not block
  11555. (defun org-entry-blocked-p ()
  11556. "Non-nil if entry at point is blocked."
  11557. (and (not (org-entry-get nil "NOBLOCKING"))
  11558. (member (org-entry-get nil "TODO") org-not-done-keywords)
  11559. (not (run-hook-with-args-until-failure
  11560. 'org-blocker-hook
  11561. (list :type 'todo-state-change
  11562. :position (point)
  11563. :from 'todo
  11564. :to 'done)))))
  11565. (defun org-update-statistics-cookies (all)
  11566. "Update the statistics cookie, either from TODO or from checkboxes.
  11567. This should be called with the cursor in a line with a statistics cookie."
  11568. (interactive "P")
  11569. (if all
  11570. (progn
  11571. (org-update-checkbox-count 'all)
  11572. (org-map-entries 'org-update-parent-todo-statistics))
  11573. (if (not (org-at-heading-p))
  11574. (org-update-checkbox-count)
  11575. (let ((pos (point-marker))
  11576. end l1 l2)
  11577. (ignore-errors (org-back-to-heading t))
  11578. (if (not (org-at-heading-p))
  11579. (org-update-checkbox-count)
  11580. (setq l1 (org-outline-level))
  11581. (setq end (save-excursion
  11582. (outline-next-heading)
  11583. (when (org-at-heading-p) (setq l2 (org-outline-level)))
  11584. (point)))
  11585. (if (and (save-excursion
  11586. (re-search-forward
  11587. "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) \\[[- X]\\]" end t))
  11588. (not (save-excursion (re-search-forward
  11589. ":COOKIE_DATA:.*\\<todo\\>" end t))))
  11590. (org-update-checkbox-count)
  11591. (if (and l2 (> l2 l1))
  11592. (progn
  11593. (goto-char end)
  11594. (org-update-parent-todo-statistics))
  11595. (goto-char pos)
  11596. (beginning-of-line 1)
  11597. (while (re-search-forward
  11598. "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)"
  11599. (point-at-eol) t)
  11600. (replace-match (if (match-end 2) "[100%]" "[0/0]") t t)))))
  11601. (goto-char pos)
  11602. (move-marker pos nil)))))
  11603. (defvar org-entry-property-inherited-from) ;; defined below
  11604. (defun org-update-parent-todo-statistics ()
  11605. "Update any statistics cookie in the parent of the current headline.
  11606. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  11607. the entire subtree and this will travel up the hierarchy and update
  11608. statistics everywhere."
  11609. (let* ((prop (save-excursion (org-up-heading-safe)
  11610. (org-entry-get nil "COOKIE_DATA" 'inherit)))
  11611. (recursive (or (not org-hierarchical-todo-statistics)
  11612. (and prop (string-match "\\<recursive\\>" prop))))
  11613. (lim (or (and prop (marker-position org-entry-property-inherited-from))
  11614. 0))
  11615. (first t)
  11616. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  11617. level ltoggle l1 new ndel
  11618. (cnt-all 0) (cnt-done 0) is-percent kwd
  11619. checkbox-beg ov ovs ove cookie-present)
  11620. (catch 'exit
  11621. (save-excursion
  11622. (beginning-of-line 1)
  11623. (setq ltoggle (funcall outline-level))
  11624. ;; Three situations are to consider:
  11625. ;; 1. if `org-hierarchical-todo-statistics' is nil, repeat up
  11626. ;; to the top-level ancestor on the headline;
  11627. ;; 2. If parent has "recursive" property, repeat up to the
  11628. ;; headline setting that property, taking inheritance into
  11629. ;; account;
  11630. ;; 3. Else, move up to direct parent and proceed only once.
  11631. (while (and (setq level (org-up-heading-safe))
  11632. (or recursive first)
  11633. (>= (point) lim))
  11634. (setq first nil cookie-present nil)
  11635. (unless (and level
  11636. (not (string-match
  11637. "\\<checkbox\\>"
  11638. (downcase (or (org-entry-get nil "COOKIE_DATA")
  11639. "")))))
  11640. (throw 'exit nil))
  11641. (while (re-search-forward box-re (point-at-eol) t)
  11642. (setq cnt-all 0 cnt-done 0 cookie-present t)
  11643. (setq is-percent (match-end 2) checkbox-beg (match-beginning 0))
  11644. (save-match-data
  11645. (unless (outline-next-heading) (throw 'exit nil))
  11646. (while (and (looking-at org-complex-heading-regexp)
  11647. (> (setq l1 (length (match-string 1))) level))
  11648. (setq kwd (and (or recursive (= l1 ltoggle))
  11649. (match-string 2)))
  11650. (if (or (eq org-provide-todo-statistics 'all-headlines)
  11651. (and (eq org-provide-todo-statistics t)
  11652. (or (member kwd org-done-keywords)))
  11653. (and (listp org-provide-todo-statistics)
  11654. (stringp (car org-provide-todo-statistics))
  11655. (or (member kwd org-provide-todo-statistics)
  11656. (member kwd org-done-keywords)))
  11657. (and (listp org-provide-todo-statistics)
  11658. (listp (car org-provide-todo-statistics))
  11659. (or (member kwd (car org-provide-todo-statistics))
  11660. (and (member kwd org-done-keywords)
  11661. (member kwd (cadr org-provide-todo-statistics))))))
  11662. (setq cnt-all (1+ cnt-all))
  11663. (and (eq org-provide-todo-statistics t)
  11664. kwd
  11665. (setq cnt-all (1+ cnt-all))))
  11666. (when (or (and (member org-provide-todo-statistics '(t all-headlines))
  11667. (member kwd org-done-keywords))
  11668. (and (listp org-provide-todo-statistics)
  11669. (listp (car org-provide-todo-statistics))
  11670. (member kwd org-done-keywords)
  11671. (member kwd (cadr org-provide-todo-statistics)))
  11672. (and (listp org-provide-todo-statistics)
  11673. (stringp (car org-provide-todo-statistics))
  11674. (member kwd org-done-keywords)))
  11675. (setq cnt-done (1+ cnt-done)))
  11676. (outline-next-heading)))
  11677. (setq new
  11678. (if is-percent
  11679. (format "[%d%%]" (floor (* 100.0 cnt-done)
  11680. (max 1 cnt-all)))
  11681. (format "[%d/%d]" cnt-done cnt-all))
  11682. ndel (- (match-end 0) checkbox-beg))
  11683. ;; handle overlays when updating cookie from column view
  11684. (when (setq ov (car (overlays-at checkbox-beg)))
  11685. (setq ovs (overlay-start ov) ove (overlay-end ov))
  11686. (delete-overlay ov))
  11687. (goto-char checkbox-beg)
  11688. (insert new)
  11689. (delete-region (point) (+ (point) ndel))
  11690. (when org-auto-align-tags (org-fix-tags-on-the-fly))
  11691. (when ov (move-overlay ov ovs ove)))
  11692. (when cookie-present
  11693. (run-hook-with-args 'org-after-todo-statistics-hook
  11694. cnt-done (- cnt-all cnt-done))))))
  11695. (run-hooks 'org-todo-statistics-hook)))
  11696. (defvar org-after-todo-statistics-hook nil
  11697. "Hook that is called after a TODO statistics cookie has been updated.
  11698. Each function is called with two arguments: the number of not-done entries
  11699. and the number of done entries.
  11700. For example, the following function, when added to this hook, will switch
  11701. an entry to DONE when all children are done, and back to TODO when new
  11702. entries are set to a TODO status. Note that this hook is only called
  11703. when there is a statistics cookie in the headline!
  11704. \(defun org-summary-todo (n-done n-not-done)
  11705. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  11706. \(let (org-log-done org-log-states) ; turn off logging
  11707. \(org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  11708. ")
  11709. (defvar org-todo-statistics-hook nil
  11710. "Hook that is run whenever Org thinks TODO statistics should be updated.
  11711. This hook runs even if there is no statistics cookie present, in which case
  11712. `org-after-todo-statistics-hook' would not run.")
  11713. (defun org-todo-trigger-tag-changes (state)
  11714. "Apply the changes defined in `org-todo-state-tags-triggers'."
  11715. (let ((l org-todo-state-tags-triggers)
  11716. changes)
  11717. (when (or (not state) (equal state ""))
  11718. (setq changes (append changes (cdr (assoc "" l)))))
  11719. (when (and (stringp state) (> (length state) 0))
  11720. (setq changes (append changes (cdr (assoc state l)))))
  11721. (when (member state org-not-done-keywords)
  11722. (setq changes (append changes (cdr (assoc 'todo l)))))
  11723. (when (member state org-done-keywords)
  11724. (setq changes (append changes (cdr (assoc 'done l)))))
  11725. (dolist (c changes)
  11726. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  11727. (defun org-local-logging (value)
  11728. "Get logging settings from a property VALUE."
  11729. ;; Directly set the variables, they are already local.
  11730. (setq org-log-done nil
  11731. org-log-repeat nil
  11732. org-todo-log-states nil)
  11733. (dolist (w (org-split-string value))
  11734. (let (a)
  11735. (cond
  11736. ((setq a (assoc w org-startup-options))
  11737. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  11738. (set (nth 1 a) (nth 2 a))))
  11739. ((setq a (org-extract-log-state-settings w))
  11740. (and (member (car a) org-todo-keywords-1)
  11741. (push a org-todo-log-states)))))))
  11742. (defun org-get-todo-sequence-head (kwd)
  11743. "Return the head of the TODO sequence to which KWD belongs.
  11744. If KWD is not set, check if there is a text property remembering the
  11745. right sequence."
  11746. (let (p)
  11747. (cond
  11748. ((not kwd)
  11749. (or (get-text-property (point-at-bol) 'org-todo-head)
  11750. (progn
  11751. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  11752. nil (point-at-eol)))
  11753. (get-text-property p 'org-todo-head))))
  11754. ((not (member kwd org-todo-keywords-1))
  11755. (car org-todo-keywords-1))
  11756. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  11757. (defun org-fast-todo-selection ()
  11758. "Fast TODO keyword selection with single keys.
  11759. Returns the new TODO keyword, or nil if no state change should occur."
  11760. (let* ((fulltable org-todo-key-alist)
  11761. (done-keywords org-done-keywords) ;; needed for the faces.
  11762. (maxlen (apply 'max (mapcar
  11763. (lambda (x)
  11764. (if (stringp (car x)) (string-width (car x)) 0))
  11765. fulltable)))
  11766. (expert nil)
  11767. (fwidth (+ maxlen 3 1 3))
  11768. (ncol (/ (- (window-width) 4) fwidth))
  11769. tg cnt e c tbl
  11770. groups ingroup)
  11771. (save-excursion
  11772. (save-window-excursion
  11773. (if expert
  11774. (set-buffer (get-buffer-create " *Org todo*"))
  11775. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  11776. (erase-buffer)
  11777. (setq-local org-done-keywords done-keywords)
  11778. (setq tbl fulltable cnt 0)
  11779. (while (setq e (pop tbl))
  11780. (cond
  11781. ((equal e '(:startgroup))
  11782. (push '() groups) (setq ingroup t)
  11783. (unless (= cnt 0)
  11784. (setq cnt 0)
  11785. (insert "\n"))
  11786. (insert "{ "))
  11787. ((equal e '(:endgroup))
  11788. (setq ingroup nil cnt 0)
  11789. (insert "}\n"))
  11790. ((equal e '(:newline))
  11791. (unless (= cnt 0)
  11792. (setq cnt 0)
  11793. (insert "\n")
  11794. (setq e (car tbl))
  11795. (while (equal (car tbl) '(:newline))
  11796. (insert "\n")
  11797. (setq tbl (cdr tbl)))))
  11798. (t
  11799. (setq tg (car e) c (cdr e))
  11800. (when ingroup (push tg (car groups)))
  11801. (setq tg (org-add-props tg nil 'face
  11802. (org-get-todo-face tg)))
  11803. (when (and (= cnt 0) (not ingroup)) (insert " "))
  11804. (insert "[" c "] " tg (make-string
  11805. (- fwidth 4 (length tg)) ?\ ))
  11806. (when (= (setq cnt (1+ cnt)) ncol)
  11807. (insert "\n")
  11808. (when ingroup (insert " "))
  11809. (setq cnt 0)))))
  11810. (insert "\n")
  11811. (goto-char (point-min))
  11812. (unless expert (org-fit-window-to-buffer))
  11813. (message "[a-z..]:Set [SPC]:clear")
  11814. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  11815. (cond
  11816. ((or (= c ?\C-g)
  11817. (and (= c ?q) (not (rassoc c fulltable))))
  11818. (setq quit-flag t))
  11819. ((= c ?\ ) nil)
  11820. ((setq e (rassoc c fulltable) tg (car e))
  11821. tg)
  11822. (t (setq quit-flag t)))))))
  11823. (defun org-entry-is-todo-p ()
  11824. (member (org-get-todo-state) org-not-done-keywords))
  11825. (defun org-entry-is-done-p ()
  11826. (member (org-get-todo-state) org-done-keywords))
  11827. (defun org-get-todo-state ()
  11828. "Return the TODO keyword of the current subtree."
  11829. (save-excursion
  11830. (org-back-to-heading t)
  11831. (and (looking-at org-todo-line-regexp)
  11832. (match-end 2)
  11833. (match-string 2))))
  11834. (defun org-at-date-range-p (&optional inactive-ok)
  11835. "Non-nil if point is inside a date range.
  11836. When optional argument INACTIVE-OK is non-nil, also consider
  11837. inactive time ranges.
  11838. When this function returns a non-nil value, match data is set
  11839. according to `org-tr-regexp-both' or `org-tr-regexp', depending
  11840. on INACTIVE-OK."
  11841. (interactive)
  11842. (save-excursion
  11843. (catch 'exit
  11844. (let ((pos (point)))
  11845. (skip-chars-backward "^[<\r\n")
  11846. (skip-chars-backward "<[")
  11847. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  11848. (>= (match-end 0) pos)
  11849. (throw 'exit t))
  11850. (skip-chars-backward "^<[\r\n")
  11851. (skip-chars-backward "<[")
  11852. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  11853. (>= (match-end 0) pos)
  11854. (throw 'exit t)))
  11855. nil)))
  11856. (defun org-get-repeat (&optional tagline)
  11857. "Check if there is a deadline/schedule with repeater in this entry."
  11858. (save-match-data
  11859. (save-excursion
  11860. (org-back-to-heading t)
  11861. (and (re-search-forward (if tagline
  11862. (concat tagline "\\s-*" org-repeat-re)
  11863. org-repeat-re)
  11864. (org-entry-end-position) t)
  11865. (match-string-no-properties 1)))))
  11866. (defvar org-last-changed-timestamp)
  11867. (defvar org-last-inserted-timestamp)
  11868. (defvar org-log-post-message)
  11869. (defvar org-log-note-purpose)
  11870. (defvar org-log-note-how nil)
  11871. (defvar org-log-note-extra)
  11872. (defun org-auto-repeat-maybe (done-word)
  11873. "Check if the current headline contains a repeated time-stamp.
  11874. If yes, set TODO state back to what it was and change the base date
  11875. of repeating deadline/scheduled time stamps to new date.
  11876. This function is run automatically after each state change to a DONE state."
  11877. (let* ((repeat (org-get-repeat))
  11878. (aa (assoc org-last-state org-todo-kwd-alist))
  11879. (interpret (nth 1 aa))
  11880. (head (nth 2 aa))
  11881. (whata '(("h" . hour) ("d" . day) ("m" . month) ("y" . year)))
  11882. (msg "Entry repeats: ")
  11883. (org-log-done nil)
  11884. (org-todo-log-states nil))
  11885. (when (and repeat (not (zerop (string-to-number (substring repeat 1)))))
  11886. (when (eq org-log-repeat t) (setq org-log-repeat 'state))
  11887. (let ((to-state (or (org-entry-get nil "REPEAT_TO_STATE" 'selective)
  11888. org-todo-repeat-to-state)))
  11889. (unless (and to-state (member to-state org-todo-keywords-1))
  11890. (setq to-state (if (eq interpret 'type) org-last-state head)))
  11891. (org-todo to-state))
  11892. (when (or org-log-repeat (org-entry-get nil "CLOCK"))
  11893. (org-entry-put nil "LAST_REPEAT" (format-time-string
  11894. (org-time-stamp-format t t))))
  11895. (when org-log-repeat
  11896. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  11897. (memq 'org-add-log-note post-command-hook))
  11898. ;; We are already setup for some record.
  11899. (when (eq org-log-repeat 'note)
  11900. ;; Make sure we take a note, not only a time stamp.
  11901. (setq org-log-note-how 'note))
  11902. ;; Set up for taking a record.
  11903. (org-add-log-setup 'state
  11904. (or done-word (car org-done-keywords))
  11905. org-last-state
  11906. org-log-repeat)))
  11907. (org-back-to-heading t)
  11908. (org-add-planning-info nil nil 'closed)
  11909. (let ((end (save-excursion (outline-next-heading) (point))))
  11910. (while (re-search-forward org-ts-regexp end t)
  11911. (when (save-match-data
  11912. (or (org-at-planning-p)
  11913. (org-at-property-p)
  11914. (eq (org-element-type (save-excursion
  11915. (backward-char)
  11916. (org-element-context)))
  11917. 'timestamp)))
  11918. (let ((type (cond ((match-end 1) org-scheduled-string)
  11919. ((match-end 3) org-deadline-string)
  11920. (t "Plain:")))
  11921. (ts (or (match-string 2) (match-string 4) (match-string 0))))
  11922. (cond
  11923. ((not
  11924. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts))
  11925. ;; Time-stamps without a repeater are usually skipped.
  11926. ;; However, a SCHEDULED time-stamp without one is
  11927. ;; removed, as it is considered as no longer relevant.
  11928. (when (equal type org-scheduled-string)
  11929. (org-remove-timestamp-with-keyword type)))
  11930. (t
  11931. (let ((n (string-to-number (match-string 2 ts)))
  11932. (what (match-string 3 ts)))
  11933. (when (equal what "w") (setq n (* n 7) what "d"))
  11934. (when (and (equal what "h")
  11935. (not (string-match-p "[0-9]\\{1,2\\}:[0-9]\\{2\\}"
  11936. ts)))
  11937. (user-error
  11938. "Cannot repeat in Repeat in %d hour(s) because no hour \
  11939. has been set"
  11940. n))
  11941. ;; Preparation, see if we need to modify the start
  11942. ;; date for the change.
  11943. (when (match-end 1)
  11944. (let ((time (save-match-data (org-time-string-to-time ts))))
  11945. (cond
  11946. ((equal (match-string 1 ts) ".")
  11947. ;; Shift starting date to today
  11948. (org-timestamp-change
  11949. (- (org-today) (time-to-days time))
  11950. 'day))
  11951. ((equal (match-string 1 ts) "+")
  11952. (let ((nshiftmax 10)
  11953. (nshift 0))
  11954. (while (or (= nshift 0)
  11955. (<= (time-to-days time)
  11956. (time-to-days (current-time))))
  11957. (when (= (cl-incf nshift) nshiftmax)
  11958. (or (y-or-n-p
  11959. (format "%d repeater intervals were not \
  11960. enough to shift date past today. Continue? "
  11961. nshift))
  11962. (user-error "Abort")))
  11963. (org-timestamp-change n (cdr (assoc what whata)))
  11964. (org-at-timestamp-p t)
  11965. (setq ts (match-string 1))
  11966. (setq time
  11967. (save-match-data
  11968. (org-time-string-to-time ts)))))
  11969. (org-timestamp-change (- n) (cdr (assoc what whata)))
  11970. ;; Rematch, so that we have everything in
  11971. ;; place for the real shift.
  11972. (org-at-timestamp-p t)
  11973. (setq ts (match-string 1))
  11974. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)"
  11975. ts)))))
  11976. (save-excursion
  11977. (org-timestamp-change n (cdr (assoc what whata)) nil t))
  11978. (setq msg
  11979. (concat
  11980. msg type " " org-last-changed-timestamp " ")))))))))
  11981. (setq org-log-post-message msg)
  11982. (message "%s" msg))))
  11983. (defun org-show-todo-tree (arg)
  11984. "Make a compact tree which shows all headlines marked with TODO.
  11985. The tree will show the lines where the regexp matches, and all higher
  11986. headlines above the match.
  11987. With a \\[universal-argument] prefix, prompt for a regexp to match.
  11988. With a numeric prefix N, construct a sparse tree for the Nth element
  11989. of `org-todo-keywords-1'."
  11990. (interactive "P")
  11991. (let ((case-fold-search nil)
  11992. (kwd-re
  11993. (cond ((null arg) org-not-done-regexp)
  11994. ((equal arg '(4))
  11995. (let ((kwd
  11996. (completing-read "Keyword (or KWD1|KWD2|...): "
  11997. (mapcar #'list org-todo-keywords-1))))
  11998. (concat "\\("
  11999. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  12000. "\\)\\>")))
  12001. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  12002. (regexp-quote (nth (1- (prefix-numeric-value arg))
  12003. org-todo-keywords-1)))
  12004. (t (user-error "Invalid prefix argument: %s" arg)))))
  12005. (message "%d TODO entries found"
  12006. (org-occur (concat "^" org-outline-regexp " *" kwd-re )))))
  12007. (defun org-deadline (arg &optional time)
  12008. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  12009. With one universal prefix argument, remove any deadline from the item.
  12010. With two universal prefix arguments, prompt for a warning delay.
  12011. With argument TIME, set the deadline at the corresponding date. TIME
  12012. can either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  12013. (interactive "P")
  12014. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  12015. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  12016. 'region-start-level 'region))
  12017. org-loop-over-headlines-in-active-region)
  12018. (org-map-entries
  12019. `(org-deadline ',arg ,time)
  12020. org-loop-over-headlines-in-active-region
  12021. cl (when (outline-invisible-p) (org-end-of-subtree nil t))))
  12022. (let* ((old-date (org-entry-get nil "DEADLINE"))
  12023. (old-date-time (when old-date (org-time-string-to-time old-date)))
  12024. (repeater (and old-date
  12025. (string-match
  12026. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  12027. old-date)
  12028. (match-string 1 old-date))))
  12029. (cond
  12030. ((equal arg '(4))
  12031. (when (and old-date org-log-redeadline)
  12032. (org-add-log-setup 'deldeadline nil old-date org-log-redeadline))
  12033. (org-remove-timestamp-with-keyword org-deadline-string)
  12034. (message "Item no longer has a deadline."))
  12035. ((equal arg '(16))
  12036. (save-excursion
  12037. (org-back-to-heading t)
  12038. (if (re-search-forward
  12039. org-deadline-time-regexp
  12040. (save-excursion (outline-next-heading) (point)) t)
  12041. (let* ((rpl0 (match-string 1))
  12042. (rpl (replace-regexp-in-string " -[0-9]+[hdwmy]" "" rpl0)))
  12043. (replace-match
  12044. (concat org-deadline-string
  12045. " <" rpl
  12046. (format " -%dd"
  12047. (abs
  12048. (- (time-to-days
  12049. (save-match-data
  12050. (org-read-date nil t nil "Warn starting from" old-date-time)))
  12051. (time-to-days old-date-time))))
  12052. ">") t t))
  12053. (user-error "No deadline information to update"))))
  12054. (t
  12055. (org-add-planning-info 'deadline time 'closed)
  12056. (when (and old-date
  12057. org-log-redeadline
  12058. (not (equal old-date org-last-inserted-timestamp)))
  12059. (org-add-log-setup
  12060. 'redeadline org-last-inserted-timestamp old-date org-log-redeadline))
  12061. (when repeater
  12062. (save-excursion
  12063. (org-back-to-heading t)
  12064. (when (re-search-forward (concat org-deadline-string " "
  12065. org-last-inserted-timestamp)
  12066. (save-excursion
  12067. (outline-next-heading) (point)) t)
  12068. (goto-char (1- (match-end 0)))
  12069. (insert " " repeater)
  12070. (setq org-last-inserted-timestamp
  12071. (concat (substring org-last-inserted-timestamp 0 -1)
  12072. " " repeater
  12073. (substring org-last-inserted-timestamp -1))))))
  12074. (message "Deadline on %s" org-last-inserted-timestamp))))))
  12075. (defun org-schedule (arg &optional time)
  12076. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  12077. With one universal prefix argument, remove any scheduling date from the item.
  12078. With two universal prefix arguments, prompt for a delay cookie.
  12079. With argument TIME, scheduled at the corresponding date. TIME can
  12080. either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  12081. (interactive "P")
  12082. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  12083. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  12084. 'region-start-level 'region))
  12085. org-loop-over-headlines-in-active-region)
  12086. (org-map-entries
  12087. `(org-schedule ',arg ,time)
  12088. org-loop-over-headlines-in-active-region
  12089. cl (when (outline-invisible-p) (org-end-of-subtree nil t))))
  12090. (let* ((old-date (org-entry-get nil "SCHEDULED"))
  12091. (old-date-time (when old-date (org-time-string-to-time old-date)))
  12092. (repeater (and old-date
  12093. (string-match
  12094. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  12095. old-date)
  12096. (match-string 1 old-date))))
  12097. (cond
  12098. ((equal arg '(4))
  12099. (progn
  12100. (when (and old-date org-log-reschedule)
  12101. (org-add-log-setup 'delschedule nil old-date org-log-reschedule))
  12102. (org-remove-timestamp-with-keyword org-scheduled-string)
  12103. (message "Item is no longer scheduled.")))
  12104. ((equal arg '(16))
  12105. (save-excursion
  12106. (org-back-to-heading t)
  12107. (if (re-search-forward
  12108. org-scheduled-time-regexp
  12109. (save-excursion (outline-next-heading) (point)) t)
  12110. (let* ((rpl0 (match-string 1))
  12111. (rpl (replace-regexp-in-string " -[0-9]+[hdwmy]" "" rpl0)))
  12112. (replace-match
  12113. (concat org-scheduled-string
  12114. " <" rpl
  12115. (format " -%dd"
  12116. (abs
  12117. (- (time-to-days
  12118. (save-match-data
  12119. (org-read-date nil t nil "Delay until" old-date-time)))
  12120. (time-to-days old-date-time))))
  12121. ">") t t))
  12122. (user-error "No scheduled information to update"))))
  12123. (t
  12124. (org-add-planning-info 'scheduled time 'closed)
  12125. (when (and old-date
  12126. org-log-reschedule
  12127. (not (equal old-date org-last-inserted-timestamp)))
  12128. (org-add-log-setup
  12129. 'reschedule org-last-inserted-timestamp old-date org-log-reschedule))
  12130. (when repeater
  12131. (save-excursion
  12132. (org-back-to-heading t)
  12133. (when (re-search-forward (concat org-scheduled-string " "
  12134. org-last-inserted-timestamp)
  12135. (save-excursion
  12136. (outline-next-heading) (point)) t)
  12137. (goto-char (1- (match-end 0)))
  12138. (insert " " repeater)
  12139. (setq org-last-inserted-timestamp
  12140. (concat (substring org-last-inserted-timestamp 0 -1)
  12141. " " repeater
  12142. (substring org-last-inserted-timestamp -1))))))
  12143. (message "Scheduled to %s" org-last-inserted-timestamp))))))
  12144. (defun org-get-scheduled-time (pom &optional inherit)
  12145. "Get the scheduled time as a time tuple, of a format suitable
  12146. for calling org-schedule with, or if there is no scheduling,
  12147. returns nil."
  12148. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  12149. (when time
  12150. (apply 'encode-time (org-parse-time-string time)))))
  12151. (defun org-get-deadline-time (pom &optional inherit)
  12152. "Get the deadline as a time tuple, of a format suitable for
  12153. calling org-deadline with, or if there is no scheduling, returns
  12154. nil."
  12155. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  12156. (when time
  12157. (apply 'encode-time (org-parse-time-string time)))))
  12158. (defun org-remove-timestamp-with-keyword (keyword)
  12159. "Remove all time stamps with KEYWORD in the current entry."
  12160. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  12161. beg)
  12162. (save-excursion
  12163. (org-back-to-heading t)
  12164. (setq beg (point))
  12165. (outline-next-heading)
  12166. (while (re-search-backward re beg t)
  12167. (replace-match "")
  12168. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  12169. (equal (char-before) ?\ ))
  12170. (backward-delete-char 1)
  12171. (when (string-match "^[ \t]*$" (buffer-substring
  12172. (point-at-bol) (point-at-eol)))
  12173. (delete-region (point-at-bol)
  12174. (min (point-max) (1+ (point-at-eol))))))))))
  12175. (defvar org-time-was-given) ; dynamically scoped parameter
  12176. (defvar org-end-time-was-given) ; dynamically scoped parameter
  12177. (defun org-at-planning-p ()
  12178. "Non-nil when point is on a planning info line."
  12179. ;; This is as accurate and faster than `org-element-at-point' since
  12180. ;; planning info location is fixed in the section.
  12181. (org-with-wide-buffer
  12182. (beginning-of-line)
  12183. (and (org-looking-at-p org-planning-line-re)
  12184. (eq (point)
  12185. (ignore-errors
  12186. (if (and (featurep 'org-inlinetask) (org-inlinetask-in-task-p))
  12187. (org-back-to-heading t)
  12188. (org-with-limited-levels (org-back-to-heading t)))
  12189. (line-beginning-position 2))))))
  12190. (defun org-add-planning-info (what &optional time &rest remove)
  12191. "Insert new timestamp with keyword in the planning line.
  12192. WHAT indicates what kind of time stamp to add. It is a symbol
  12193. among `closed', `deadline', `scheduled' and nil. TIME indicates
  12194. the time to use. If none is given, the user is prompted for
  12195. a date. REMOVE indicates what kind of entries to remove. An old
  12196. WHAT entry will also be removed."
  12197. (let (org-time-was-given org-end-time-was-given default-time default-input)
  12198. (catch 'exit
  12199. (when (and (memq what '(scheduled deadline))
  12200. (or (not time)
  12201. (and (stringp time)
  12202. (string-match "^[-+]+[0-9]" time))))
  12203. ;; Try to get a default date/time from existing timestamp
  12204. (save-excursion
  12205. (org-back-to-heading t)
  12206. (let ((end (save-excursion (outline-next-heading) (point))) ts)
  12207. (when (re-search-forward (if (eq what 'scheduled)
  12208. org-scheduled-time-regexp
  12209. org-deadline-time-regexp)
  12210. end t)
  12211. (setq ts (match-string 1)
  12212. default-time (apply 'encode-time (org-parse-time-string ts))
  12213. default-input (and ts (org-get-compact-tod ts)))))))
  12214. (when what
  12215. (setq time
  12216. (if (stringp time)
  12217. ;; This is a string (relative or absolute), set
  12218. ;; proper date.
  12219. (apply #'encode-time
  12220. (org-read-date-analyze
  12221. time default-time (decode-time default-time)))
  12222. ;; If necessary, get the time from the user
  12223. (or time (org-read-date nil 'to-time nil nil
  12224. default-time default-input)))))
  12225. (org-with-wide-buffer
  12226. (org-back-to-heading t)
  12227. (forward-line)
  12228. (unless (bolp) (insert "\n"))
  12229. (cond ((org-looking-at-p org-planning-line-re)
  12230. ;; Move to current indentation.
  12231. (skip-chars-forward " \t")
  12232. ;; Check if we have to remove something.
  12233. (dolist (type (if what (cons what remove) remove))
  12234. (save-excursion
  12235. (when (re-search-forward
  12236. (cl-case type
  12237. (closed org-closed-time-regexp)
  12238. (deadline org-deadline-time-regexp)
  12239. (scheduled org-scheduled-time-regexp)
  12240. (otherwise
  12241. (error "Invalid planning type: %s" type)))
  12242. (line-end-position) t)
  12243. ;; Delete until next keyword or end of line.
  12244. (delete-region
  12245. (match-beginning 0)
  12246. (if (re-search-forward org-keyword-time-not-clock-regexp
  12247. (line-end-position)
  12248. t)
  12249. (match-beginning 0)
  12250. (line-end-position))))))
  12251. ;; If there is nothing more to add and no more keyword
  12252. ;; is left, remove the line completely.
  12253. (if (and (org-looking-at-p "[ \t]*$") (not what))
  12254. (delete-region (line-beginning-position)
  12255. (line-beginning-position 2))
  12256. ;; If we removed last keyword, do not leave trailing
  12257. ;; white space at the end of line.
  12258. (let ((p (point)))
  12259. (save-excursion
  12260. (end-of-line)
  12261. (unless (= (skip-chars-backward " \t" p) 0)
  12262. (delete-region (point) (line-end-position)))))))
  12263. ((not what) (throw 'exit nil)) ; Nothing to do.
  12264. (t (insert-before-markers "\n")
  12265. (backward-char 1)
  12266. (when org-adapt-indentation
  12267. (org-indent-to-column (1+ (org-outline-level))))))
  12268. (when what
  12269. ;; Insert planning keyword.
  12270. (insert (cl-case what
  12271. (closed org-closed-string)
  12272. (deadline org-deadline-string)
  12273. (scheduled org-scheduled-string)
  12274. (otherwise (error "Invalid planning type: %s" what)))
  12275. " ")
  12276. ;; Insert associated timestamp.
  12277. (let ((ts (org-insert-time-stamp
  12278. time
  12279. (or org-time-was-given
  12280. (and (eq what 'closed) org-log-done-with-time))
  12281. (eq what 'closed)
  12282. nil nil (list org-end-time-was-given))))
  12283. (unless (eolp) (insert " "))
  12284. ts))))))
  12285. (defvar org-log-note-marker (make-marker)
  12286. "Marker pointing at the entry where the note is to be inserted.")
  12287. (defvar org-log-note-purpose nil)
  12288. (defvar org-log-note-state nil)
  12289. (defvar org-log-note-previous-state nil)
  12290. (defvar org-log-note-extra nil)
  12291. (defvar org-log-note-window-configuration nil)
  12292. (defvar org-log-note-return-to (make-marker))
  12293. (defvar org-log-note-effective-time nil
  12294. "Remembered current time so that dynamically scoped
  12295. `org-extend-today-until' affects timestamps in state change log")
  12296. (defvar org-log-post-message nil
  12297. "Message to be displayed after a log note has been stored.
  12298. The auto-repeater uses this.")
  12299. (defun org-add-note ()
  12300. "Add a note to the current entry.
  12301. This is done in the same way as adding a state change note."
  12302. (interactive)
  12303. (org-add-log-setup 'note))
  12304. (defun org-log-beginning (&optional create)
  12305. "Return expected start of log notes in current entry.
  12306. When optional argument CREATE is non-nil, the function creates
  12307. a drawer to store notes, if necessary. Returned position ignores
  12308. narrowing."
  12309. (org-with-wide-buffer
  12310. (let ((drawer (org-log-into-drawer)))
  12311. (cond
  12312. (drawer
  12313. (org-end-of-meta-data)
  12314. (let ((regexp (concat "^[ \t]*:" (regexp-quote drawer) ":[ \t]*$"))
  12315. (end (if (org-at-heading-p) (point)
  12316. (save-excursion (outline-next-heading) (point))))
  12317. (case-fold-search t))
  12318. (catch 'exit
  12319. ;; Try to find existing drawer.
  12320. (while (re-search-forward regexp end t)
  12321. (let ((element (org-element-at-point)))
  12322. (when (eq (org-element-type element) 'drawer)
  12323. (let ((cend (org-element-property :contents-end element)))
  12324. (when (and (not org-log-states-order-reversed) cend)
  12325. (goto-char cend)))
  12326. (throw 'exit nil))))
  12327. ;; No drawer found. Create one, if permitted.
  12328. (when create
  12329. (unless (bolp) (insert "\n"))
  12330. (let ((beg (point)))
  12331. (insert ":" drawer ":\n:END:\n")
  12332. (org-indent-region beg (point)))
  12333. (end-of-line -1)))))
  12334. (t
  12335. (org-end-of-meta-data org-log-state-notes-insert-after-drawers)
  12336. (skip-chars-forward " \t\n")
  12337. (beginning-of-line)
  12338. (unless org-log-states-order-reversed
  12339. (org-skip-over-state-notes)
  12340. (skip-chars-backward " \t\n")
  12341. (forward-line)))))
  12342. (if (bolp) (point) (line-beginning-position 2))))
  12343. (defun org-add-log-setup (&optional purpose state prev-state how extra)
  12344. "Set up the post command hook to take a note.
  12345. If this is about to TODO state change, the new state is expected in STATE.
  12346. HOW is an indicator what kind of note should be created.
  12347. EXTRA is additional text that will be inserted into the notes buffer."
  12348. (move-marker org-log-note-marker (point))
  12349. (setq org-log-note-purpose purpose
  12350. org-log-note-state state
  12351. org-log-note-previous-state prev-state
  12352. org-log-note-how how
  12353. org-log-note-extra extra
  12354. org-log-note-effective-time (org-current-effective-time))
  12355. (add-hook 'post-command-hook 'org-add-log-note 'append))
  12356. (defun org-skip-over-state-notes ()
  12357. "Skip past the list of State notes in an entry."
  12358. (when (ignore-errors (goto-char (org-in-item-p)))
  12359. (let* ((struct (org-list-struct))
  12360. (prevs (org-list-prevs-alist struct))
  12361. (regexp
  12362. (concat "[ \t]*- +"
  12363. (replace-regexp-in-string
  12364. " +" " +"
  12365. (org-replace-escapes
  12366. (regexp-quote (cdr (assq 'state org-log-note-headings)))
  12367. `(("%d" . ,org-ts-regexp-inactive)
  12368. ("%D" . ,org-ts-regexp)
  12369. ("%s" . "\"\\S-+\"")
  12370. ("%S" . "\"\\S-+\"")
  12371. ("%t" . ,org-ts-regexp-inactive)
  12372. ("%T" . ,org-ts-regexp)
  12373. ("%u" . ".*?")
  12374. ("%U" . ".*?")))))))
  12375. (while (org-looking-at-p regexp)
  12376. (goto-char (or (org-list-get-next-item (point) struct prevs)
  12377. (org-list-get-item-end (point) struct)))))))
  12378. (defun org-add-log-note (&optional _purpose)
  12379. "Pop up a window for taking a note, and add this note later."
  12380. (remove-hook 'post-command-hook 'org-add-log-note)
  12381. (setq org-log-note-window-configuration (current-window-configuration))
  12382. (delete-other-windows)
  12383. (move-marker org-log-note-return-to (point))
  12384. (org-pop-to-buffer-same-window (marker-buffer org-log-note-marker))
  12385. (goto-char org-log-note-marker)
  12386. (org-switch-to-buffer-other-window "*Org Note*")
  12387. (erase-buffer)
  12388. (if (memq org-log-note-how '(time state))
  12389. (let (current-prefix-arg) (org-store-log-note))
  12390. (let ((org-inhibit-startup t)) (org-mode))
  12391. (insert (format "# Insert note for %s.
  12392. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  12393. (cond
  12394. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  12395. ((eq org-log-note-purpose 'done) "closed todo item")
  12396. ((eq org-log-note-purpose 'state)
  12397. (format "state change from \"%s\" to \"%s\""
  12398. (or org-log-note-previous-state "")
  12399. (or org-log-note-state "")))
  12400. ((eq org-log-note-purpose 'reschedule)
  12401. "rescheduling")
  12402. ((eq org-log-note-purpose 'delschedule)
  12403. "no longer scheduled")
  12404. ((eq org-log-note-purpose 'redeadline)
  12405. "changing deadline")
  12406. ((eq org-log-note-purpose 'deldeadline)
  12407. "removing deadline")
  12408. ((eq org-log-note-purpose 'refile)
  12409. "refiling")
  12410. ((eq org-log-note-purpose 'note)
  12411. "this entry")
  12412. (t (error "This should not happen")))))
  12413. (when org-log-note-extra (insert org-log-note-extra))
  12414. (setq-local org-finish-function 'org-store-log-note)
  12415. (run-hooks 'org-log-buffer-setup-hook)))
  12416. (defvar org-note-abort nil) ; dynamically scoped
  12417. (defun org-store-log-note ()
  12418. "Finish taking a log note, and insert it to where it belongs."
  12419. (let ((txt (prog1 (buffer-string)
  12420. (kill-buffer)))
  12421. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  12422. lines)
  12423. (while (string-match "\\`# .*\n[ \t\n]*" txt)
  12424. (setq txt (replace-match "" t t txt)))
  12425. (when (string-match "\\s-+\\'" txt)
  12426. (setq txt (replace-match "" t t txt)))
  12427. (setq lines (org-split-string txt "\n"))
  12428. (when (org-string-nw-p note)
  12429. (setq note
  12430. (org-replace-escapes
  12431. note
  12432. (list (cons "%u" (user-login-name))
  12433. (cons "%U" user-full-name)
  12434. (cons "%t" (format-time-string
  12435. (org-time-stamp-format 'long 'inactive)
  12436. org-log-note-effective-time))
  12437. (cons "%T" (format-time-string
  12438. (org-time-stamp-format 'long nil)
  12439. org-log-note-effective-time))
  12440. (cons "%d" (format-time-string
  12441. (org-time-stamp-format nil 'inactive)
  12442. org-log-note-effective-time))
  12443. (cons "%D" (format-time-string
  12444. (org-time-stamp-format nil nil)
  12445. org-log-note-effective-time))
  12446. (cons "%s" (cond
  12447. ((not org-log-note-state) "")
  12448. ((org-string-match-p org-ts-regexp
  12449. org-log-note-state)
  12450. (format "\"[%s]\""
  12451. (substring org-log-note-state 1 -1)))
  12452. (t (format "\"%s\"" org-log-note-state))))
  12453. (cons "%S"
  12454. (cond
  12455. ((not org-log-note-previous-state) "")
  12456. ((org-string-match-p org-ts-regexp
  12457. org-log-note-previous-state)
  12458. (format "\"[%s]\""
  12459. (substring
  12460. org-log-note-previous-state 1 -1)))
  12461. (t (format "\"%s\""
  12462. org-log-note-previous-state)))))))
  12463. (when lines (setq note (concat note " \\\\")))
  12464. (push note lines))
  12465. (when (and lines (not (or current-prefix-arg org-note-abort)))
  12466. (with-current-buffer (marker-buffer org-log-note-marker)
  12467. (org-with-wide-buffer
  12468. ;; Find location for the new note.
  12469. (goto-char org-log-note-marker)
  12470. (set-marker org-log-note-marker nil)
  12471. (goto-char (org-log-beginning t))
  12472. ;; Make sure point is at the beginning of an empty line.
  12473. (cond ((not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  12474. ((looking-at "[ \t]*\\S-") (save-excursion (insert "\n"))))
  12475. ;; In an existing list, add a new item at the top level.
  12476. ;; Otherwise, indent line like a regular one.
  12477. (let ((itemp (org-in-item-p)))
  12478. (if itemp
  12479. (org-indent-line-to
  12480. (let ((struct (save-excursion
  12481. (goto-char itemp) (org-list-struct))))
  12482. (org-list-get-ind (org-list-get-top-point struct) struct)))
  12483. (org-indent-line)))
  12484. (insert (org-list-bullet-string "-") (pop lines))
  12485. (let ((ind (org-list-item-body-column (line-beginning-position))))
  12486. (dolist (line lines)
  12487. (insert "\n")
  12488. (org-indent-line-to ind)
  12489. (insert line)))
  12490. (message "Note stored")
  12491. (org-back-to-heading t)
  12492. (org-cycle-hide-drawers 'children))
  12493. ;; Fix `buffer-undo-list' when `org-store-log-note' is called
  12494. ;; from within `org-add-log-note' because `buffer-undo-list'
  12495. ;; is then modified outside of `org-with-remote-undo'.
  12496. (when (eq this-command 'org-agenda-todo)
  12497. (setcdr buffer-undo-list (cddr buffer-undo-list))))))
  12498. ;; Don't add undo information when called from `org-agenda-todo'.
  12499. (let ((buffer-undo-list (eq this-command 'org-agenda-todo)))
  12500. (set-window-configuration org-log-note-window-configuration)
  12501. (with-current-buffer (marker-buffer org-log-note-return-to)
  12502. (goto-char org-log-note-return-to))
  12503. (move-marker org-log-note-return-to nil)
  12504. (when org-log-post-message (message "%s" org-log-post-message))))
  12505. (defun org-remove-empty-drawer-at (pos)
  12506. "Remove an empty drawer at position POS.
  12507. POS may also be a marker."
  12508. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  12509. (org-with-wide-buffer
  12510. (goto-char pos)
  12511. (let ((drawer (org-element-at-point)))
  12512. (when (and (memq (org-element-type drawer) '(drawer property-drawer))
  12513. (not (org-element-property :contents-begin drawer)))
  12514. (delete-region (org-element-property :begin drawer)
  12515. (progn (goto-char (org-element-property :end drawer))
  12516. (skip-chars-backward " \r\t\n")
  12517. (forward-line)
  12518. (point))))))))
  12519. (defvar org-ts-type nil)
  12520. (defun org-sparse-tree (&optional arg type)
  12521. "Create a sparse tree, prompt for the details.
  12522. This command can create sparse trees. You first need to select the type
  12523. of match used to create the tree:
  12524. t Show all TODO entries.
  12525. T Show entries with a specific TODO keyword.
  12526. m Show entries selected by a tags/property match.
  12527. p Enter a property name and its value (both with completion on existing
  12528. names/values) and show entries with that property.
  12529. r Show entries matching a regular expression (`/' can be used as well).
  12530. b Show deadlines and scheduled items before a date.
  12531. a Show deadlines and scheduled items after a date.
  12532. d Show deadlines due within `org-deadline-warning-days'.
  12533. D Show deadlines and scheduled items between a date range."
  12534. (interactive "P")
  12535. (setq type (or type org-sparse-tree-default-date-type))
  12536. (setq org-ts-type type)
  12537. (message "Sparse tree: [/]regexp [t]odo [T]odo-kwd [m]atch [p]roperty
  12538. \[d]eadlines [b]efore-date [a]fter-date [D]ates range
  12539. \[c]ycle through date types: %s"
  12540. (cl-case type
  12541. (all "all timestamps")
  12542. (scheduled "only scheduled")
  12543. (deadline "only deadline")
  12544. (active "only active timestamps")
  12545. (inactive "only inactive timestamps")
  12546. (closed "with a closed time-stamp")
  12547. (otherwise "scheduled/deadline")))
  12548. (let ((answer (read-char-exclusive)))
  12549. (cl-case answer
  12550. (?c
  12551. (org-sparse-tree
  12552. arg
  12553. (cadr
  12554. (memq type '(nil all scheduled deadline active inactive closed)))))
  12555. (?d (call-interactively 'org-check-deadlines))
  12556. (?b (call-interactively 'org-check-before-date))
  12557. (?a (call-interactively 'org-check-after-date))
  12558. (?D (call-interactively 'org-check-dates-range))
  12559. (?t (call-interactively 'org-show-todo-tree))
  12560. (?T (org-show-todo-tree '(4)))
  12561. (?m (call-interactively 'org-match-sparse-tree))
  12562. ((?p ?P)
  12563. (let* ((kwd (completing-read
  12564. "Property: " (mapcar #'list (org-buffer-property-keys))))
  12565. (value (completing-read
  12566. "Value: " (mapcar #'list (org-property-values kwd)))))
  12567. (unless (string-match "\\`{.*}\\'" value)
  12568. (setq value (concat "\"" value "\"")))
  12569. (org-match-sparse-tree arg (concat kwd "=" value))))
  12570. ((?r ?R ?/) (call-interactively 'org-occur))
  12571. (otherwise (user-error "No such sparse tree command \"%c\"" answer)))))
  12572. (defvar-local org-occur-highlights nil
  12573. "List of overlays used for occur matches.")
  12574. (defvar-local org-occur-parameters nil
  12575. "Parameters of the active org-occur calls.
  12576. This is a list, each call to org-occur pushes as cons cell,
  12577. containing the regular expression and the callback, onto the list.
  12578. The list can contain several entries if `org-occur' has been called
  12579. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  12580. will only contain one set of parameters. When the highlights are
  12581. removed (for example with `C-c C-c', or with the next edit (depending
  12582. on `org-remove-highlights-with-change'), this variable is emptied
  12583. as well.")
  12584. (defun org-occur (regexp &optional keep-previous callback)
  12585. "Make a compact tree which shows all matches of REGEXP.
  12586. The tree will show the lines where the regexp matches, and all higher
  12587. headlines above the match. It will also show the heading after the match,
  12588. to make sure editing the matching entry is easy.
  12589. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  12590. call to `org-occur' will be kept, to allow stacking of calls to this
  12591. command.
  12592. If CALLBACK is non-nil, it is a function which is called to confirm
  12593. that the match should indeed be shown."
  12594. (interactive "sRegexp: \nP")
  12595. (when (equal regexp "")
  12596. (user-error "Regexp cannot be empty"))
  12597. (unless keep-previous
  12598. (org-remove-occur-highlights nil nil t))
  12599. (push (cons regexp callback) org-occur-parameters)
  12600. (let ((cnt 0))
  12601. (save-excursion
  12602. (goto-char (point-min))
  12603. (when (or (not keep-previous) ; do not want to keep
  12604. (not org-occur-highlights)) ; no previous matches
  12605. ;; hide everything
  12606. (org-overview))
  12607. (while (re-search-forward regexp nil t)
  12608. (backward-char) ;; FIXME: Match timestamps at the end of a headline
  12609. (when (or (not callback)
  12610. (save-match-data (funcall callback)))
  12611. (setq cnt (1+ cnt))
  12612. (when org-highlight-sparse-tree-matches
  12613. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  12614. (org-show-context 'occur-tree))))
  12615. (when org-remove-highlights-with-change
  12616. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  12617. nil 'local))
  12618. (unless org-sparse-tree-open-archived-trees
  12619. (org-hide-archived-subtrees (point-min) (point-max)))
  12620. (run-hooks 'org-occur-hook)
  12621. (when (org-called-interactively-p 'interactive)
  12622. (message "%d match(es) for regexp %s" cnt regexp))
  12623. cnt))
  12624. (defun org-occur-next-match (&optional n _reset)
  12625. "Function for `next-error-function' to find sparse tree matches.
  12626. N is the number of matches to move, when negative move backwards.
  12627. This function always goes back to the starting point when no
  12628. match is found."
  12629. (let* ((limit (if (< n 0) (point-min) (point-max)))
  12630. (search-func (if (< n 0)
  12631. 'previous-single-char-property-change
  12632. 'next-single-char-property-change))
  12633. (n (abs n))
  12634. (pos (point))
  12635. p1)
  12636. (catch 'exit
  12637. (while (setq p1 (funcall search-func (point) 'org-type))
  12638. (when (equal p1 limit)
  12639. (goto-char pos)
  12640. (user-error "No more matches"))
  12641. (when (equal (get-char-property p1 'org-type) 'org-occur)
  12642. (setq n (1- n))
  12643. (when (= n 0)
  12644. (goto-char p1)
  12645. (throw 'exit (point))))
  12646. (goto-char p1))
  12647. (goto-char p1)
  12648. (user-error "No more matches"))))
  12649. (defun org-show-context (&optional key)
  12650. "Make sure point and context are visible.
  12651. Optional argument KEY, when non-nil, is a symbol. See
  12652. `org-show-context-detail' for allowed values and how much is to
  12653. be shown."
  12654. (org-show-set-visibility
  12655. (cond ((symbolp org-show-context-detail) org-show-context-detail)
  12656. ((cdr (assq key org-show-context-detail)))
  12657. (t (cdr (assq 'default org-show-context-detail))))))
  12658. (defun org-show-set-visibility (detail)
  12659. "Set visibility around point according to DETAIL.
  12660. DETAIL is either nil, `minimal', `local', `ancestors', `lineage',
  12661. `tree', `canonical' or t. See `org-show-context-detail' for more
  12662. information."
  12663. (unless (org-before-first-heading-p)
  12664. ;; Show current heading and possibly its entry, following headline
  12665. ;; or all children.
  12666. (if (and (org-at-heading-p) (not (eq detail 'local)))
  12667. (org-flag-heading nil)
  12668. (org-show-entry)
  12669. (org-with-limited-levels
  12670. (cl-case detail
  12671. ((tree canonical t) (org-show-children))
  12672. ((nil minimal ancestors))
  12673. (t (save-excursion
  12674. (outline-next-heading)
  12675. (org-flag-heading nil))))))
  12676. ;; Show all siblings.
  12677. (when (eq detail 'lineage) (org-show-siblings))
  12678. ;; Show ancestors, possibly with their children.
  12679. (when (memq detail '(ancestors lineage tree canonical t))
  12680. (save-excursion
  12681. (while (org-up-heading-safe)
  12682. (org-flag-heading nil)
  12683. (when (memq detail '(canonical t)) (org-show-entry))
  12684. (when (memq detail '(tree canonical t)) (org-show-children)))))))
  12685. (defvar org-reveal-start-hook nil
  12686. "Hook run before revealing a location.")
  12687. (defun org-reveal (&optional siblings)
  12688. "Show current entry, hierarchy above it, and the following headline.
  12689. This can be used to show a consistent set of context around
  12690. locations exposed with `org-show-context'.
  12691. With optional argument SIBLINGS, on each level of the hierarchy all
  12692. siblings are shown. This repairs the tree structure to what it would
  12693. look like when opened with hierarchical calls to `org-cycle'.
  12694. With double optional argument \\[universal-argument] \\[universal-argument], \
  12695. go to the parent and show the
  12696. entire tree."
  12697. (interactive "P")
  12698. (run-hooks 'org-reveal-start-hook)
  12699. (cond ((equal siblings '(4)) (org-show-set-visibility 'canonical))
  12700. ((equal siblings '(16))
  12701. (save-excursion
  12702. (when (org-up-heading-safe)
  12703. (org-show-subtree)
  12704. (run-hook-with-args 'org-cycle-hook 'subtree))))
  12705. (t (org-show-set-visibility 'lineage))))
  12706. (defun org-highlight-new-match (beg end)
  12707. "Highlight from BEG to END and mark the highlight is an occur headline."
  12708. (let ((ov (make-overlay beg end)))
  12709. (overlay-put ov 'face 'secondary-selection)
  12710. (overlay-put ov 'org-type 'org-occur)
  12711. (push ov org-occur-highlights)))
  12712. (defun org-remove-occur-highlights (&optional _beg _end noremove)
  12713. "Remove the occur highlights from the buffer.
  12714. BEG and END are ignored. If NOREMOVE is nil, remove this function
  12715. from the `before-change-functions' in the current buffer."
  12716. (interactive)
  12717. (unless org-inhibit-highlight-removal
  12718. (mapc #'delete-overlay org-occur-highlights)
  12719. (setq org-occur-highlights nil)
  12720. (setq org-occur-parameters nil)
  12721. (unless noremove
  12722. (remove-hook 'before-change-functions
  12723. 'org-remove-occur-highlights 'local))))
  12724. ;;;; Priorities
  12725. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  12726. "Regular expression matching the priority indicator.")
  12727. (defvar org-remove-priority-next-time nil)
  12728. (defun org-priority-up ()
  12729. "Increase the priority of the current item."
  12730. (interactive)
  12731. (org-priority 'up))
  12732. (defun org-priority-down ()
  12733. "Decrease the priority of the current item."
  12734. (interactive)
  12735. (org-priority 'down))
  12736. (defun org-priority (&optional action _show)
  12737. "Change the priority of an item.
  12738. ACTION can be `set', `up', `down', or a character."
  12739. (interactive "P")
  12740. (if (equal action '(4))
  12741. (org-show-priority)
  12742. (unless org-enable-priority-commands
  12743. (user-error "Priority commands are disabled"))
  12744. (setq action (or action 'set))
  12745. (let (current new news have remove)
  12746. (save-excursion
  12747. (org-back-to-heading t)
  12748. (when (looking-at org-priority-regexp)
  12749. (setq current (string-to-char (match-string 2))
  12750. have t))
  12751. (cond
  12752. ((eq action 'remove)
  12753. (setq remove t new ?\ ))
  12754. ((or (eq action 'set)
  12755. (if (featurep 'xemacs) (characterp action) (integerp action)))
  12756. (if (not (eq action 'set))
  12757. (setq new action)
  12758. (message "Priority %c-%c, SPC to remove: "
  12759. org-highest-priority org-lowest-priority)
  12760. (save-match-data
  12761. (setq new (read-char-exclusive))))
  12762. (when (and (= (upcase org-highest-priority) org-highest-priority)
  12763. (= (upcase org-lowest-priority) org-lowest-priority))
  12764. (setq new (upcase new)))
  12765. (cond ((equal new ?\ ) (setq remove t))
  12766. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  12767. (user-error "Priority must be between `%c' and `%c'"
  12768. org-highest-priority org-lowest-priority))))
  12769. ((eq action 'up)
  12770. (setq new (if have
  12771. (1- current) ; normal cycling
  12772. ;; last priority was empty
  12773. (if (eq last-command this-command)
  12774. org-lowest-priority ; wrap around empty to lowest
  12775. ;; default
  12776. (if org-priority-start-cycle-with-default
  12777. org-default-priority
  12778. (1- org-default-priority))))))
  12779. ((eq action 'down)
  12780. (setq new (if have
  12781. (1+ current) ; normal cycling
  12782. ;; last priority was empty
  12783. (if (eq last-command this-command)
  12784. org-highest-priority ; wrap around empty to highest
  12785. ;; default
  12786. (if org-priority-start-cycle-with-default
  12787. org-default-priority
  12788. (1+ org-default-priority))))))
  12789. (t (user-error "Invalid action")))
  12790. (when (or (< (upcase new) org-highest-priority)
  12791. (> (upcase new) org-lowest-priority))
  12792. (if (and (memq action '(up down))
  12793. (not have) (not (eq last-command this-command)))
  12794. ;; `new' is from default priority
  12795. (error
  12796. "The default can not be set, see `org-default-priority' why")
  12797. ;; normal cycling: `new' is beyond highest/lowest priority
  12798. ;; and is wrapped around to the empty priority
  12799. (setq remove t)))
  12800. (setq news (format "%c" new))
  12801. (if have
  12802. (if remove
  12803. (replace-match "" t t nil 1)
  12804. (replace-match news t t nil 2))
  12805. (if remove
  12806. (user-error "No priority cookie found in line")
  12807. (let ((case-fold-search nil))
  12808. (looking-at org-todo-line-regexp))
  12809. (if (match-end 2)
  12810. (progn
  12811. (goto-char (match-end 2))
  12812. (insert " [#" news "]"))
  12813. (goto-char (match-beginning 3))
  12814. (insert "[#" news "] "))))
  12815. (org-set-tags nil 'align))
  12816. (if remove
  12817. (message "Priority removed")
  12818. (message "Priority of current item set to %s" news)))))
  12819. (defun org-show-priority ()
  12820. "Show the priority of the current item.
  12821. This priority is composed of the main priority given with the [#A] cookies,
  12822. and by additional input from the age of a schedules or deadline entry."
  12823. (interactive)
  12824. (let ((pri (if (eq major-mode 'org-agenda-mode)
  12825. (org-get-at-bol 'priority)
  12826. (save-excursion
  12827. (save-match-data
  12828. (beginning-of-line)
  12829. (and (looking-at org-heading-regexp)
  12830. (org-get-priority (match-string 0))))))))
  12831. (message "Priority is %d" (if pri pri -1000))))
  12832. (defun org-get-priority (s)
  12833. "Find priority cookie and return priority."
  12834. (save-match-data
  12835. (if (functionp org-get-priority-function)
  12836. (funcall org-get-priority-function)
  12837. (if (not (string-match org-priority-regexp s))
  12838. (* 1000 (- org-lowest-priority org-default-priority))
  12839. (* 1000 (- org-lowest-priority
  12840. (string-to-char (match-string 2 s))))))))
  12841. ;;;; Tags
  12842. (defvar org-agenda-archives-mode)
  12843. (defvar org-map-continue-from nil
  12844. "Position from where mapping should continue.
  12845. Can be set by the action argument to `org-scan-tags' and `org-map-entries'.")
  12846. (defvar org-scanner-tags nil
  12847. "The current tag list while the tags scanner is running.")
  12848. (defvar org-trust-scanner-tags nil
  12849. "Should `org-get-tags-at' use the tags for the scanner.
  12850. This is for internal dynamical scoping only.
  12851. When this is non-nil, the function `org-get-tags-at' will return the value
  12852. of `org-scanner-tags' instead of building the list by itself. This
  12853. can lead to large speed-ups when the tags scanner is used in a file with
  12854. many entries, and when the list of tags is retrieved, for example to
  12855. obtain a list of properties. Building the tags list for each entry in such
  12856. a file becomes an N^2 operation - but with this variable set, it scales
  12857. as N.")
  12858. (defvar org--matcher-tags-todo-only nil)
  12859. (defun org-scan-tags (action matcher todo-only &optional start-level)
  12860. "Scan headline tags with inheritance and produce output ACTION.
  12861. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  12862. or `agenda' to produce an entry list for an agenda view. It can also be
  12863. a Lisp form or a function that should be called at each matched headline, in
  12864. this case the return value is a list of all return values from these calls.
  12865. MATCHER is a function accepting three arguments, returning
  12866. a non-nil value whenever a given set of tags qualifies a headline
  12867. for inclusion. See `org-make-tags-matcher' for more information.
  12868. As a special case, it can also be set to t (respectively nil) in
  12869. order to match all (respectively none) headline.
  12870. When TODO-ONLY is non-nil, only lines with a not-done TODO
  12871. keyword are included in the output.
  12872. START-LEVEL can be a string with asterisks, reducing the scope to
  12873. headlines matching this string."
  12874. (require 'org-agenda)
  12875. (let* ((re (concat "^"
  12876. (if start-level
  12877. ;; Get the correct level to match
  12878. (concat "\\*\\{" (number-to-string start-level) "\\} ")
  12879. org-outline-regexp)
  12880. " *\\(\\<\\("
  12881. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  12882. (org-re "\\)\\>\\)? *\\(.*?\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*$")))
  12883. (props (list 'face 'default
  12884. 'done-face 'org-agenda-done
  12885. 'undone-face 'default
  12886. 'mouse-face 'highlight
  12887. 'org-not-done-regexp org-not-done-regexp
  12888. 'org-todo-regexp org-todo-regexp
  12889. 'org-complex-heading-regexp org-complex-heading-regexp
  12890. 'help-echo
  12891. (format "mouse-2 or RET jump to org file %s"
  12892. (abbreviate-file-name
  12893. (or (buffer-file-name (buffer-base-buffer))
  12894. (buffer-name (buffer-base-buffer)))))))
  12895. (org-map-continue-from nil)
  12896. lspos tags tags-list
  12897. (tags-alist (list (cons 0 org-file-tags)))
  12898. (llast 0) rtn rtn1 level category i txt
  12899. todo marker entry priority
  12900. ts-date ts-date-type ts-date-pair)
  12901. (unless (or (member action '(agenda sparse-tree)) (functionp action))
  12902. (setq action (list 'lambda nil action)))
  12903. (save-excursion
  12904. (goto-char (point-min))
  12905. (when (eq action 'sparse-tree)
  12906. (org-overview)
  12907. (org-remove-occur-highlights))
  12908. (while (let (case-fold-search)
  12909. (re-search-forward re nil t))
  12910. (setq org-map-continue-from nil)
  12911. (catch :skip
  12912. (setq todo
  12913. ;; TODO: is the 1-2 difference a bug?
  12914. (when (match-end 1) (org-match-string-no-properties 2))
  12915. tags (when (match-end 4) (org-match-string-no-properties 4)))
  12916. (goto-char (setq lspos (match-beginning 0)))
  12917. (setq level (org-reduced-level (org-outline-level))
  12918. category (org-get-category))
  12919. (when (eq action 'agenda)
  12920. (setq ts-date-pair (org-agenda-entry-get-agenda-timestamp (point))
  12921. ts-date (car ts-date-pair)
  12922. ts-date-type (cdr ts-date-pair)))
  12923. (setq i llast llast level)
  12924. ;; remove tag lists from same and sublevels
  12925. (while (>= i level)
  12926. (when (setq entry (assoc i tags-alist))
  12927. (setq tags-alist (delete entry tags-alist)))
  12928. (setq i (1- i)))
  12929. ;; add the next tags
  12930. (when tags
  12931. (setq tags (org-split-string tags ":")
  12932. tags-alist
  12933. (cons (cons level tags) tags-alist)))
  12934. ;; compile tags for current headline
  12935. (setq tags-list
  12936. (if org-use-tag-inheritance
  12937. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  12938. tags)
  12939. org-scanner-tags tags-list)
  12940. (when org-use-tag-inheritance
  12941. (setcdr (car tags-alist)
  12942. (mapcar (lambda (x)
  12943. (setq x (copy-sequence x))
  12944. (org-add-prop-inherited x))
  12945. (cdar tags-alist))))
  12946. (when (and tags org-use-tag-inheritance
  12947. (or (not (eq t org-use-tag-inheritance))
  12948. org-tags-exclude-from-inheritance))
  12949. ;; Selective inheritance, remove uninherited ones.
  12950. (setcdr (car tags-alist)
  12951. (org-remove-uninherited-tags (cdar tags-alist))))
  12952. (when (and
  12953. ;; eval matcher only when the todo condition is OK
  12954. (and (or (not todo-only) (member todo org-not-done-keywords))
  12955. (if (functionp matcher)
  12956. (let ((case-fold-search t) (org-trust-scanner-tags t))
  12957. (funcall matcher todo tags-list level))
  12958. matcher))
  12959. ;; Call the skipper, but return t if it does not
  12960. ;; skip, so that the `and' form continues evaluating.
  12961. (progn
  12962. (unless (eq action 'sparse-tree) (org-agenda-skip))
  12963. t)
  12964. ;; Check if timestamps are deselecting this entry
  12965. (or (not todo-only)
  12966. (and (member todo org-not-done-keywords)
  12967. (or (not org-agenda-tags-todo-honor-ignore-options)
  12968. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item))))))
  12969. ;; select this headline
  12970. (cond
  12971. ((eq action 'sparse-tree)
  12972. (and org-highlight-sparse-tree-matches
  12973. (org-get-heading) (match-end 0)
  12974. (org-highlight-new-match
  12975. (match-beginning 1) (match-end 1)))
  12976. (org-show-context 'tags-tree))
  12977. ((eq action 'agenda)
  12978. (setq txt (org-agenda-format-item
  12979. ""
  12980. (concat
  12981. (if (eq org-tags-match-list-sublevels 'indented)
  12982. (make-string (1- level) ?.) "")
  12983. (org-get-heading))
  12984. (make-string level ?\s)
  12985. category
  12986. tags-list)
  12987. priority (org-get-priority txt))
  12988. (goto-char lspos)
  12989. (setq marker (org-agenda-new-marker))
  12990. (org-add-props txt props
  12991. 'org-marker marker 'org-hd-marker marker 'org-category category
  12992. 'todo-state todo
  12993. 'ts-date ts-date
  12994. 'priority priority
  12995. 'type (concat "tagsmatch" ts-date-type))
  12996. (push txt rtn))
  12997. ((functionp action)
  12998. (setq org-map-continue-from nil)
  12999. (save-excursion
  13000. (setq rtn1 (funcall action))
  13001. (push rtn1 rtn)))
  13002. (t (user-error "Invalid action")))
  13003. ;; if we are to skip sublevels, jump to end of subtree
  13004. (unless org-tags-match-list-sublevels
  13005. (org-end-of-subtree t)
  13006. (backward-char 1))))
  13007. ;; Get the correct position from where to continue
  13008. (if org-map-continue-from
  13009. (goto-char org-map-continue-from)
  13010. (and (= (point) lspos) (end-of-line 1)))))
  13011. (when (and (eq action 'sparse-tree)
  13012. (not org-sparse-tree-open-archived-trees))
  13013. (org-hide-archived-subtrees (point-min) (point-max)))
  13014. (nreverse rtn)))
  13015. (defun org-remove-uninherited-tags (tags)
  13016. "Remove all tags that are not inherited from the list TAGS."
  13017. (cond
  13018. ((eq org-use-tag-inheritance t)
  13019. (if org-tags-exclude-from-inheritance
  13020. (org-delete-all org-tags-exclude-from-inheritance tags)
  13021. tags))
  13022. ((not org-use-tag-inheritance) nil)
  13023. ((stringp org-use-tag-inheritance)
  13024. (delq nil (mapcar
  13025. (lambda (x)
  13026. (if (and (string-match org-use-tag-inheritance x)
  13027. (not (member x org-tags-exclude-from-inheritance)))
  13028. x nil))
  13029. tags)))
  13030. ((listp org-use-tag-inheritance)
  13031. (delq nil (mapcar
  13032. (lambda (x)
  13033. (if (member x org-use-tag-inheritance) x nil))
  13034. tags)))))
  13035. (defun org-match-sparse-tree (&optional todo-only match)
  13036. "Create a sparse tree according to tags string MATCH.
  13037. MATCH can contain positive and negative selection of tags, like
  13038. \"+WORK+URGENT-WITHBOSS\".
  13039. If optional argument TODO-ONLY is non-nil, only select lines that are
  13040. also TODO lines."
  13041. (interactive "P")
  13042. (org-agenda-prepare-buffers (list (current-buffer)))
  13043. (let ((org--matcher-tags-todo-only todo-only))
  13044. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match))
  13045. org--matcher-tags-todo-only)))
  13046. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  13047. (defvar org-cached-props nil)
  13048. (defun org-cached-entry-get (pom property)
  13049. (if (or (eq t org-use-property-inheritance)
  13050. (and (stringp org-use-property-inheritance)
  13051. (let ((case-fold-search t))
  13052. (org-string-match-p org-use-property-inheritance property)))
  13053. (and (listp org-use-property-inheritance)
  13054. (member-ignore-case property org-use-property-inheritance)))
  13055. ;; Caching is not possible, check it directly.
  13056. (org-entry-get pom property 'inherit)
  13057. ;; Get all properties, so we can do complicated checks easily.
  13058. (cdr (assoc-string property
  13059. (or org-cached-props
  13060. (setq org-cached-props (org-entry-properties pom)))
  13061. t))))
  13062. (defun org-global-tags-completion-table (&optional files)
  13063. "Return the list of all tags in all agenda buffer/files.
  13064. Optional FILES argument is a list of files which can be used
  13065. instead of the agenda files."
  13066. (save-excursion
  13067. (org-uniquify
  13068. (delq nil
  13069. (apply #'append
  13070. (mapcar
  13071. (lambda (file)
  13072. (set-buffer (find-file-noselect file))
  13073. (mapcar (lambda (x)
  13074. (and (stringp (car-safe x))
  13075. (list (car-safe x))))
  13076. (or org-current-tag-alist (org-get-buffer-tags))))
  13077. (if (car-safe files) files
  13078. (org-agenda-files))))))))
  13079. (defun org-make-tags-matcher (match)
  13080. "Create the TAGS/TODO matcher form for the selection string MATCH.
  13081. Returns a cons of the selection string MATCH and a function
  13082. implementing the matcher.
  13083. The matcher is to be called at an Org entry, with point on the
  13084. headline, and returns non-nil if the entry matches the selection
  13085. string MATCH. It must be called with three arguments: the TODO
  13086. keyword at the entry (or nil if none), the list of all tags at
  13087. the entry including inherited ones and the reduced level of the
  13088. headline. Additionally, the category of the entry, if any, must
  13089. be specified as the text property `org-category' on the headline.
  13090. This function sets the variable `org--matcher-tags-todo-only' to
  13091. a non-nil value if the matcher restricts matching to TODO
  13092. entries, otherwise it is not touched.
  13093. See also `org-scan-tags'."
  13094. (unless match
  13095. ;; Get a new match request, with completion against the global
  13096. ;; tags table and the local tags in current buffer.
  13097. (let ((org-last-tags-completion-table
  13098. (org-uniquify
  13099. (delq nil (append (org-get-buffer-tags)
  13100. (org-global-tags-completion-table))))))
  13101. (setq match
  13102. (completing-read
  13103. "Match: "
  13104. 'org-tags-completion-function nil nil nil 'org-tags-history))))
  13105. (let ((match0 match)
  13106. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\(\\(?:[[:alnum:]_]+\\(?:\\\\-\\)*\\)+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@#%]+\\)"))
  13107. (start 0)
  13108. tagsmatch todomatch tagsmatcher todomatcher)
  13109. ;; Expand group tags.
  13110. (setq match (org-tags-expand match))
  13111. ;; Check if there is a TODO part of this match, which would be the
  13112. ;; part after a "/". To make sure that this slash is not part of
  13113. ;; a property value to be matched against, we also check that
  13114. ;; there is no / after that slash. First, find the last slash.
  13115. (let ((s 0))
  13116. (while (string-match "/+" match s)
  13117. (setq start (match-beginning 0))
  13118. (setq s (match-end 0))))
  13119. (if (and (string-match "/+" match start)
  13120. (not (string-match-p "\"" match start)))
  13121. ;; Match contains also a TODO-matching request.
  13122. (progn
  13123. (setq tagsmatch (substring match 0 (match-beginning 0)))
  13124. (setq todomatch (substring match (match-end 0)))
  13125. (when (string-match "\\`!" todomatch)
  13126. (setq org--matcher-tags-todo-only t)
  13127. (setq todomatch (substring todomatch 1)))
  13128. (when (string-match "\\`\\s-*\\'" todomatch)
  13129. (setq todomatch nil)))
  13130. ;; Only matching tags.
  13131. (setq tagsmatch match)
  13132. (setq todomatch nil))
  13133. ;; Make the tags matcher.
  13134. (when (org-string-nw-p tagsmatch)
  13135. (let ((orlist nil)
  13136. (orterms (org-split-string tagsmatch "|"))
  13137. term)
  13138. (while (setq term (pop orterms))
  13139. (while (and (equal (substring term -1) "\\") orterms)
  13140. (setq term (concat term "|" (pop orterms)))) ;repair bad split.
  13141. (while (string-match re term)
  13142. (let* ((rest (substring term (match-end 0)))
  13143. (minus (and (match-end 1)
  13144. (equal (match-string 1 term) "-")))
  13145. (tag (save-match-data
  13146. (replace-regexp-in-string
  13147. "\\\\-" "-" (match-string 2 term))))
  13148. (regexp (eq (string-to-char tag) ?{))
  13149. (levelp (match-end 4))
  13150. (propp (match-end 5))
  13151. (mm
  13152. (cond
  13153. (regexp `(org-match-any-p ,(substring tag 1 -1) tags-list))
  13154. (levelp
  13155. `(,(org-op-to-function (match-string 3 term))
  13156. level
  13157. ,(string-to-number (match-string 4 term))))
  13158. (propp
  13159. (let* ((gv (pcase (upcase (match-string 5 term))
  13160. ("CATEGORY"
  13161. '(get-text-property (point) 'org-category))
  13162. ("TODO" 'todo)
  13163. (p `(org-cached-entry-get nil ,p))))
  13164. (pv (match-string 7 term))
  13165. (regexp (eq (string-to-char pv) ?{))
  13166. (strp (eq (string-to-char pv) ?\"))
  13167. (timep (string-match-p "^\"[[<].*[]>]\"$" pv))
  13168. (po (org-op-to-function (match-string 6 term)
  13169. (if timep 'time strp))))
  13170. (setq pv (if (or regexp strp) (substring pv 1 -1) pv))
  13171. (when timep (setq pv (org-matcher-time pv)))
  13172. (cond ((and regexp (eq po 'org<>))
  13173. `(not (string-match ,pv (or ,gv ""))))
  13174. (regexp `(string-match ,pv (or ,gv "")))
  13175. (strp `(,po (or ,gv "") ,pv))
  13176. (t
  13177. `(,po
  13178. (string-to-number (or ,gv ""))
  13179. ,(string-to-number pv))))))
  13180. (t `(member ,tag tags-list)))))
  13181. (push (if minus `(not ,mm) mm) tagsmatcher)
  13182. (setq term rest)))
  13183. (push `(and ,@tagsmatcher) orlist)
  13184. (setq tagsmatcher nil))
  13185. (setq tagsmatcher `(progn (setq org-cached-props nil) (or ,@orlist)))))
  13186. ;; Make the TODO matcher.
  13187. (when (org-string-nw-p todomatch)
  13188. (let ((orlist nil))
  13189. (dolist (term (org-split-string todomatch "|"))
  13190. (while (string-match re term)
  13191. (let* ((minus (and (match-end 1)
  13192. (equal (match-string 1 term) "-")))
  13193. (kwd (match-string 2 term))
  13194. (regexp (eq (string-to-char kwd) ?{))
  13195. (mm (if regexp `(string-match ,(substring kwd 1 -1) todo)
  13196. `(equal todo ,kwd))))
  13197. (push (if minus `(not ,mm) mm) todomatcher))
  13198. (setq term (substring term (match-end 0))))
  13199. (push (if (> (length todomatcher) 1)
  13200. (cons 'and todomatcher)
  13201. (car todomatcher))
  13202. orlist)
  13203. (setq todomatcher nil))
  13204. (setq todomatcher (cons 'or orlist))))
  13205. ;; Return the string and function of the matcher. If no
  13206. ;; tags-specific or todo-specific matcher exists, match
  13207. ;; everything.
  13208. (let ((matcher (if (and tagsmatcher todomatcher)
  13209. `(and ,tagsmatcher ,todomatcher)
  13210. (or tagsmatcher todomatcher t))))
  13211. (when org--matcher-tags-todo-only
  13212. (setq matcher `(and (member todo org-not-done-keywords) ,matcher)))
  13213. (cons match0 `(lambda (todo tags-list level) ,matcher)))))
  13214. (defun org-tags-expand (match &optional single-as-list downcased tags-already-expanded)
  13215. "Expand group tags in MATCH.
  13216. This replaces every group tag in MATCH with a regexp tag search.
  13217. For example, a group tag \"Work\" defined as { Work : Lab Conf }
  13218. will be replaced like this:
  13219. Work => {\\<\\(?:Work\\|Lab\\|Conf\\)\\>}
  13220. +Work => +{\\<\\(?:Work\\|Lab\\|Conf\\)\\>}
  13221. -Work => -{\\<\\(?:Work\\|Lab\\|Conf\\)\\>}
  13222. Replacing by a regexp preserves the structure of the match.
  13223. E.g., this expansion
  13224. Work|Home => {\\(?:Work\\|Lab\\|Conf\\}|Home
  13225. will match anything tagged with \"Lab\" and \"Home\", or tagged
  13226. with \"Conf\" and \"Home\" or tagged with \"Work\" and \"home\".
  13227. A group tag in MATCH can contain regular expressions of its own.
  13228. For example, a group tag \"Proj\" defined as { Proj : {P@.+} }
  13229. will be replaced like this:
  13230. Proj => {\\<\\(?:Proj\\)\\>\\|P@.+}
  13231. When the optional argument SINGLE-AS-LIST is non-nil, MATCH is
  13232. assumed to be a single group tag, and the function will return
  13233. the list of tags in this group.
  13234. When DOWNCASE is non-nil, expand downcased TAGS."
  13235. (if org-group-tags
  13236. (let* ((case-fold-search t)
  13237. (stable org-mode-syntax-table)
  13238. (taggroups (or org-tag-groups-alist-for-agenda org-tag-groups-alist))
  13239. (taggroups (if downcased
  13240. (mapcar (lambda (tg) (mapcar #'downcase tg))
  13241. taggroups)
  13242. taggroups))
  13243. (taggroups-keys (mapcar #'car taggroups))
  13244. (return-match (if downcased (downcase match) match))
  13245. (count 0)
  13246. (work-already-expanded tags-already-expanded)
  13247. regexps-in-match tags-in-group regexp-in-group regexp-in-group-escaped)
  13248. ;; @ and _ are allowed as word-components in tags.
  13249. (modify-syntax-entry ?@ "w" stable)
  13250. (modify-syntax-entry ?_ "w" stable)
  13251. ;; Temporarily replace regexp-expressions in the match-expression.
  13252. (while (string-match "{.+?}" return-match)
  13253. (cl-incf count)
  13254. (push (match-string 0 return-match) regexps-in-match)
  13255. (setq return-match (replace-match (format "<%d>" count) t nil return-match)))
  13256. (while (and taggroups-keys
  13257. (with-syntax-table stable
  13258. (string-match
  13259. (concat "\\(?1:[+-]?\\)\\(?2:\\<"
  13260. (regexp-opt taggroups-keys) "\\>\\)")
  13261. return-match)))
  13262. (let* ((dir (match-string 1 return-match))
  13263. (tag (match-string 2 return-match))
  13264. (tag (if downcased (downcase tag) tag)))
  13265. (unless (or (get-text-property 0 'grouptag (match-string 2 return-match))
  13266. (member tag work-already-expanded))
  13267. (setq tags-in-group (assoc tag taggroups))
  13268. (push tag work-already-expanded)
  13269. ;; Recursively expand each tag in the group, if the tag hasn't
  13270. ;; already been expanded. Restore the match-data after all recursive calls.
  13271. (save-match-data
  13272. (let (tags-expanded)
  13273. (dolist (x (cdr tags-in-group))
  13274. (if (and (member x taggroups-keys)
  13275. (not (member x work-already-expanded)))
  13276. (setq tags-expanded
  13277. (delete-dups
  13278. (append
  13279. (org-tags-expand x t downcased
  13280. work-already-expanded)
  13281. tags-expanded)))
  13282. (setq tags-expanded
  13283. (append (list x) tags-expanded)))
  13284. (setq work-already-expanded
  13285. (delete-dups
  13286. (append tags-expanded
  13287. work-already-expanded))))
  13288. (setq tags-in-group
  13289. (delete-dups (cons (car tags-in-group)
  13290. tags-expanded)))))
  13291. ;; Filter tag-regexps from tags.
  13292. (setq regexp-in-group-escaped
  13293. (delq nil (mapcar (lambda (x)
  13294. (if (stringp x)
  13295. (and (equal "{" (substring x 0 1))
  13296. (equal "}" (substring x -1))
  13297. x)
  13298. x))
  13299. tags-in-group))
  13300. regexp-in-group
  13301. (mapcar (lambda (x)
  13302. (substring x 1 -1))
  13303. regexp-in-group-escaped)
  13304. tags-in-group
  13305. (delq nil (mapcar (lambda (x)
  13306. (if (stringp x)
  13307. (and (not (equal "{" (substring x 0 1)))
  13308. (not (equal "}" (substring x -1)))
  13309. x)
  13310. x))
  13311. tags-in-group)))
  13312. ;; If single-as-list, do no more in the while-loop.
  13313. (if (not single-as-list)
  13314. (progn
  13315. (when regexp-in-group
  13316. (setq regexp-in-group
  13317. (concat "\\|"
  13318. (mapconcat 'identity regexp-in-group
  13319. "\\|"))))
  13320. (setq tags-in-group
  13321. (concat dir
  13322. "{\\<"
  13323. (regexp-opt tags-in-group)
  13324. "\\>"
  13325. regexp-in-group
  13326. "}"))
  13327. (when (stringp tags-in-group)
  13328. (org-add-props tags-in-group '(grouptag t)))
  13329. (setq return-match
  13330. (replace-match tags-in-group t t return-match)))
  13331. (setq tags-in-group
  13332. (append regexp-in-group-escaped tags-in-group))))
  13333. (setq taggroups-keys (delete tag taggroups-keys))))
  13334. ;; Add the regular expressions back into the match-expression again.
  13335. (while regexps-in-match
  13336. (setq return-match (replace-regexp-in-string (format "<%d>" count)
  13337. (pop regexps-in-match)
  13338. return-match t t))
  13339. (cl-decf count))
  13340. (if single-as-list
  13341. (if tags-in-group tags-in-group (list return-match))
  13342. return-match))
  13343. (if single-as-list
  13344. (list (if downcased (downcase match) match))
  13345. match)))
  13346. (defun org-op-to-function (op &optional stringp)
  13347. "Turn an operator into the appropriate function."
  13348. (setq op
  13349. (cond
  13350. ((equal op "<" ) '(< string< org-time<))
  13351. ((equal op ">" ) '(> org-string> org-time>))
  13352. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  13353. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  13354. ((member op '("=" "==")) '(= string= org-time=))
  13355. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  13356. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  13357. (defun org<> (a b) (not (= a b)))
  13358. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  13359. (defun org-string>= (a b) (not (string< a b)))
  13360. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  13361. (defun org-string<> (a b) (not (string= a b)))
  13362. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  13363. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  13364. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  13365. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  13366. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  13367. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  13368. (defun org-2ft (s)
  13369. "Convert S to a floating point time.
  13370. If S is already a number, just return it. If it is a string, parse
  13371. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  13372. (cond
  13373. ((numberp s) s)
  13374. ((stringp s)
  13375. (condition-case nil
  13376. (float-time (apply 'encode-time (org-parse-time-string s)))
  13377. (error 0.)))
  13378. (t 0.)))
  13379. (defun org-time-today ()
  13380. "Time in seconds today at 0:00.
  13381. Returns the float number of seconds since the beginning of the
  13382. epoch to the beginning of today (00:00)."
  13383. (float-time (apply 'encode-time
  13384. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  13385. (defun org-matcher-time (s)
  13386. "Interpret a time comparison value."
  13387. (save-match-data
  13388. (cond
  13389. ((string= s "<now>") (float-time))
  13390. ((string= s "<today>") (org-time-today))
  13391. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  13392. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  13393. ((string-match "^<\\([-+][0-9]+\\)\\([hdwmy]\\)>$" s)
  13394. (+ (org-time-today)
  13395. (* (string-to-number (match-string 1 s))
  13396. (cdr (assoc (match-string 2 s)
  13397. '(("d" . 86400.0) ("w" . 604800.0)
  13398. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  13399. (t (org-2ft s)))))
  13400. (defun org-match-any-p (re list)
  13401. "Does re match any element of list?"
  13402. (setq list (mapcar (lambda (x) (string-match re x)) list))
  13403. (delq nil list))
  13404. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  13405. (defvar org-tags-overlay (make-overlay 1 1))
  13406. (org-detach-overlay org-tags-overlay)
  13407. (defun org-get-local-tags-at (&optional pos)
  13408. "Get a list of tags defined in the current headline."
  13409. (org-get-tags-at pos 'local))
  13410. (defun org-get-local-tags ()
  13411. "Get a list of tags defined in the current headline."
  13412. (org-get-tags-at nil 'local))
  13413. (defun org-get-tags-at (&optional pos local)
  13414. "Get a list of all headline tags applicable at POS.
  13415. POS defaults to point. If tags are inherited, the list contains
  13416. the targets in the same sequence as the headlines appear, i.e.
  13417. the tags of the current headline come last.
  13418. When LOCAL is non-nil, only return tags from the current headline,
  13419. ignore inherited ones."
  13420. (interactive)
  13421. (if (and org-trust-scanner-tags
  13422. (or (not pos) (equal pos (point)))
  13423. (not local))
  13424. org-scanner-tags
  13425. (let (tags ltags lastpos parent)
  13426. (save-excursion
  13427. (save-restriction
  13428. (widen)
  13429. (goto-char (or pos (point)))
  13430. (save-match-data
  13431. (catch 'done
  13432. (condition-case nil
  13433. (progn
  13434. (org-back-to-heading t)
  13435. (while (not (equal lastpos (point)))
  13436. (setq lastpos (point))
  13437. (when (looking-at
  13438. (org-re "[^\r\n]+?:\\([[:alnum:]_@#%:]+\\):[ \t]*$"))
  13439. (setq ltags (org-split-string
  13440. (org-match-string-no-properties 1) ":"))
  13441. (when parent
  13442. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  13443. (setq tags (append
  13444. (if parent
  13445. (org-remove-uninherited-tags ltags)
  13446. ltags)
  13447. tags)))
  13448. (or org-use-tag-inheritance (throw 'done t))
  13449. (when local (throw 'done t))
  13450. (or (org-up-heading-safe) (error nil))
  13451. (setq parent t)))
  13452. (error nil)))))
  13453. (if local
  13454. tags
  13455. (reverse (delete-dups
  13456. (reverse (append
  13457. (org-remove-uninherited-tags
  13458. org-file-tags)
  13459. tags)))))))))
  13460. (defun org-add-prop-inherited (s)
  13461. (add-text-properties 0 (length s) '(inherited t) s)
  13462. s)
  13463. (defun org-toggle-tag (tag &optional onoff)
  13464. "Toggle the tag TAG for the current line.
  13465. If ONOFF is `on' or `off', don't toggle but set to this state."
  13466. (let (res current)
  13467. (save-excursion
  13468. (org-back-to-heading t)
  13469. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t]*$")
  13470. (point-at-eol) t)
  13471. (progn
  13472. (setq current (match-string 1))
  13473. (replace-match ""))
  13474. (setq current ""))
  13475. (setq current (nreverse (org-split-string current ":")))
  13476. (cond
  13477. ((eq onoff 'on)
  13478. (setq res t)
  13479. (or (member tag current) (push tag current)))
  13480. ((eq onoff 'off)
  13481. (or (not (member tag current)) (setq current (delete tag current))))
  13482. (t (if (member tag current)
  13483. (setq current (delete tag current))
  13484. (setq res t)
  13485. (push tag current))))
  13486. (end-of-line 1)
  13487. (if current
  13488. (progn
  13489. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  13490. (org-set-tags nil t))
  13491. (delete-horizontal-space))
  13492. (run-hooks 'org-after-tags-change-hook))
  13493. res))
  13494. (defun org-align-tags-here (to-col)
  13495. ;; Assumes that this is a headline
  13496. "Align tags on the current headline to TO-COL."
  13497. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  13498. (beginning-of-line 1)
  13499. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13500. (< pos (match-beginning 2)))
  13501. (progn
  13502. (setq tags-l (- (match-end 2) (match-beginning 2)))
  13503. (goto-char (match-beginning 1))
  13504. (insert " ")
  13505. (delete-region (point) (1+ (match-beginning 2)))
  13506. (setq ncol (max (current-column)
  13507. (1+ col)
  13508. (if (> to-col 0)
  13509. to-col
  13510. (- (abs to-col) tags-l))))
  13511. (setq p (point))
  13512. (insert (make-string (- ncol (current-column)) ?\ ))
  13513. (setq ncol (current-column))
  13514. (when indent-tabs-mode (tabify p (point-at-eol)))
  13515. (org-move-to-column (min ncol col)))
  13516. (goto-char pos))))
  13517. (defun org-set-tags-command (&optional arg just-align)
  13518. "Call the set-tags command for the current entry."
  13519. (interactive "P")
  13520. (if (or (org-at-heading-p) (and arg (org-before-first-heading-p)))
  13521. (org-set-tags arg just-align)
  13522. (save-excursion
  13523. (unless (and (org-region-active-p)
  13524. org-loop-over-headlines-in-active-region)
  13525. (org-back-to-heading t))
  13526. (org-set-tags arg just-align))))
  13527. (defun org-set-tags-to (data)
  13528. "Set the tags of the current entry to DATA, replacing the current tags.
  13529. DATA may be a tags string like :aa:bb:cc:, or a list of tags.
  13530. If DATA is nil or the empty string, any tags will be removed."
  13531. (interactive "sTags: ")
  13532. (setq data
  13533. (cond
  13534. ((eq data nil) "")
  13535. ((equal data "") "")
  13536. ((stringp data)
  13537. (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
  13538. ":"))
  13539. ((listp data)
  13540. (concat ":" (mapconcat 'identity data ":") ":"))))
  13541. (when data
  13542. (save-excursion
  13543. (org-back-to-heading t)
  13544. (when (looking-at org-complex-heading-regexp)
  13545. (if (match-end 5)
  13546. (progn
  13547. (goto-char (match-beginning 5))
  13548. (insert data)
  13549. (delete-region (point) (point-at-eol))
  13550. (org-set-tags nil 'align))
  13551. (goto-char (point-at-eol))
  13552. (insert " " data)
  13553. (org-set-tags nil 'align)))
  13554. (beginning-of-line 1)
  13555. (when (looking-at ".*?\\([ \t]+\\)$")
  13556. (delete-region (match-beginning 1) (match-end 1))))))
  13557. (defun org-align-all-tags ()
  13558. "Align the tags in all headings."
  13559. (interactive)
  13560. (save-excursion
  13561. (or (ignore-errors (org-back-to-heading t))
  13562. (outline-next-heading))
  13563. (if (org-at-heading-p)
  13564. (org-set-tags t)
  13565. (message "No headings"))))
  13566. (defvar org-indent-indentation-per-level)
  13567. (defun org-set-tags (&optional arg just-align)
  13568. "Set the tags for the current headline.
  13569. With prefix ARG, realign all tags in headings in the current buffer.
  13570. When JUST-ALIGN is non-nil, only align tags."
  13571. (interactive "P")
  13572. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  13573. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  13574. 'region-start-level
  13575. 'region))
  13576. org-loop-over-headlines-in-active-region)
  13577. (org-map-entries
  13578. ;; We don't use ARG and JUST-ALIGN here because these args
  13579. ;; are not useful when looping over headlines.
  13580. #'org-set-tags
  13581. org-loop-over-headlines-in-active-region
  13582. cl
  13583. '(when (outline-invisible-p) (org-end-of-subtree nil t))))
  13584. (let ((org-setting-tags t))
  13585. (if arg
  13586. (save-excursion
  13587. (goto-char (point-min))
  13588. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  13589. (while (re-search-forward org-outline-regexp-bol nil t)
  13590. (org-set-tags nil t)
  13591. (end-of-line)))
  13592. (message "All tags realigned to column %d" org-tags-column))
  13593. (let* ((current (org-get-tags-string))
  13594. (col (current-column))
  13595. (tags
  13596. (if just-align current
  13597. ;; Get a new set of tags from the user.
  13598. (save-excursion
  13599. (let* ((table
  13600. (setq
  13601. org-last-tags-completion-table
  13602. (append
  13603. org-tag-persistent-alist
  13604. (or org-current-tag-alist (org-get-buffer-tags))
  13605. (and
  13606. org-complete-tags-always-offer-all-agenda-tags
  13607. (org-global-tags-completion-table
  13608. (org-agenda-files))))))
  13609. (current-tags (org-split-string current ":"))
  13610. (inherited-tags
  13611. (nreverse (nthcdr (length current-tags)
  13612. (nreverse (org-get-tags-at))))))
  13613. (replace-regexp-in-string
  13614. "\\([-+&]+\\|,\\)"
  13615. ":"
  13616. (if (or (eq t org-use-fast-tag-selection)
  13617. (and org-use-fast-tag-selection
  13618. (delq nil (mapcar #'cdr table))))
  13619. (org-fast-tag-selection
  13620. current-tags inherited-tags table
  13621. (and org-fast-tag-selection-include-todo
  13622. org-todo-key-alist))
  13623. (let ((org-add-colon-after-tag-completion
  13624. (< 1 (length table))))
  13625. (org-trim
  13626. (completing-read
  13627. "Tags: "
  13628. #'org-tags-completion-function
  13629. nil nil current 'org-tags-history))))))))))
  13630. (when org-tags-sort-function
  13631. (setq tags
  13632. (mapconcat
  13633. #'identity
  13634. (sort (org-split-string tags (org-re "[^[:alnum:]_@#%]+"))
  13635. org-tags-sort-function)
  13636. ":")))
  13637. (if (not (org-string-nw-p tags)) (setq tags "")
  13638. (unless (string-match ":\\'" tags) (setq tags (concat tags ":")))
  13639. (unless (string-match "\\`:" tags) (setq tags (concat ":" tags))))
  13640. ;; Insert new tags at the correct column
  13641. (beginning-of-line)
  13642. (let ((level (if (looking-at org-outline-regexp)
  13643. (- (match-end 0) (point) 1)
  13644. 1)))
  13645. (cond
  13646. ((and (equal current "") (equal tags "")))
  13647. ((re-search-forward
  13648. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  13649. (line-end-position)
  13650. t)
  13651. (if (equal tags "") (replace-match "" t t)
  13652. (goto-char (match-beginning 0))
  13653. (let* ((c0 (current-column))
  13654. ;; Compute offset for the case of org-indent-mode
  13655. ;; active.
  13656. (di (if (org-bound-and-true-p org-indent-mode)
  13657. (* (1- org-indent-indentation-per-level)
  13658. (1- level))
  13659. 0))
  13660. (p0 (if (eq (char-before) ?*) (1+ (point)) (point)))
  13661. (tc (+ org-tags-column
  13662. (if (> org-tags-column 0) (- di) di)))
  13663. (c1 (max (1+ c0)
  13664. (if (> tc 0) tc
  13665. (- (- tc) (string-width tags)))))
  13666. (rpl (concat (make-string (max 0 (- c1 c0)) ?\s) tags)))
  13667. (replace-match rpl t t)
  13668. (when (and (not (featurep 'xemacs)) indent-tabs-mode)
  13669. (tabify p0 (point))))))
  13670. (t (error "Tags alignment failed"))))
  13671. (org-move-to-column col))
  13672. (unless just-align (run-hooks 'org-after-tags-change-hook))))))
  13673. (defun org-change-tag-in-region (beg end tag off)
  13674. "Add or remove TAG for each entry in the region.
  13675. This works in the agenda, and also in an org-mode buffer."
  13676. (interactive
  13677. (list (region-beginning) (region-end)
  13678. (let ((org-last-tags-completion-table
  13679. (if (derived-mode-p 'org-mode)
  13680. (org-uniquify
  13681. (delq nil (append (org-get-buffer-tags)
  13682. (org-global-tags-completion-table))))
  13683. (org-global-tags-completion-table))))
  13684. (completing-read
  13685. "Tag: " 'org-tags-completion-function nil nil nil
  13686. 'org-tags-history))
  13687. (progn
  13688. (message "[s]et or [r]emove? ")
  13689. (equal (read-char-exclusive) ?r))))
  13690. (when (fboundp 'deactivate-mark) (deactivate-mark))
  13691. (let ((agendap (equal major-mode 'org-agenda-mode))
  13692. l1 l2 m buf pos newhead (cnt 0))
  13693. (goto-char end)
  13694. (setq l2 (1- (org-current-line)))
  13695. (goto-char beg)
  13696. (setq l1 (org-current-line))
  13697. (cl-loop for l from l1 to l2 do
  13698. (org-goto-line l)
  13699. (setq m (get-text-property (point) 'org-hd-marker))
  13700. (when (or (and (derived-mode-p 'org-mode) (org-at-heading-p))
  13701. (and agendap m))
  13702. (setq buf (if agendap (marker-buffer m) (current-buffer))
  13703. pos (if agendap m (point)))
  13704. (with-current-buffer buf
  13705. (save-excursion
  13706. (save-restriction
  13707. (goto-char pos)
  13708. (setq cnt (1+ cnt))
  13709. (org-toggle-tag tag (if off 'off 'on))
  13710. (setq newhead (org-get-heading)))))
  13711. (and agendap (org-agenda-change-all-lines newhead m))))
  13712. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  13713. (defun org-tags-completion-function (string _predicate &optional flag)
  13714. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  13715. (confirm (lambda (x) (stringp (car x)))))
  13716. (if (string-match "^\\(.*[-+:&,|]\\)\\([^-+:&,|]*\\)$" string)
  13717. (setq s1 (match-string 1 string)
  13718. s2 (match-string 2 string))
  13719. (setq s1 "" s2 string))
  13720. (cond
  13721. ((eq flag nil)
  13722. ;; try completion
  13723. (setq rtn (try-completion s2 ctable confirm))
  13724. (when (stringp rtn)
  13725. (setq rtn
  13726. (concat s1 s2 (substring rtn (length s2))
  13727. (if (and org-add-colon-after-tag-completion
  13728. (assoc rtn ctable))
  13729. ":" ""))))
  13730. rtn)
  13731. ((eq flag t)
  13732. ;; all-completions
  13733. (all-completions s2 ctable confirm))
  13734. ((eq flag 'lambda)
  13735. ;; exact match?
  13736. (assoc s2 ctable)))))
  13737. (defun org-fast-tag-insert (kwd tags face &optional end)
  13738. "Insert KDW, and the TAGS, the latter with face FACE.
  13739. Also insert END."
  13740. (insert (format "%-12s" (concat kwd ":"))
  13741. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  13742. (or end "")))
  13743. (defun org-fast-tag-show-exit (flag)
  13744. (save-excursion
  13745. (org-goto-line 3)
  13746. (when (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  13747. (replace-match ""))
  13748. (when flag
  13749. (end-of-line 1)
  13750. (org-move-to-column (- (window-width) 19) t)
  13751. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  13752. (defun org-set-current-tags-overlay (current prefix)
  13753. "Add an overlay to CURRENT tag with PREFIX."
  13754. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  13755. (if (featurep 'xemacs)
  13756. (org-overlay-display org-tags-overlay (concat prefix s)
  13757. 'secondary-selection)
  13758. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  13759. (org-overlay-display org-tags-overlay (concat prefix s)))))
  13760. (defvar org-last-tag-selection-key nil)
  13761. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  13762. "Fast tag selection with single keys.
  13763. CURRENT is the current list of tags in the headline, INHERITED is the
  13764. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  13765. possibly with grouping information. TODO-TABLE is a similar table with
  13766. TODO keywords, should these have keys assigned to them.
  13767. If the keys are nil, a-z are automatically assigned.
  13768. Returns the new tags string, or nil to not change the current settings."
  13769. (let* ((fulltable (append table todo-table))
  13770. (maxlen (apply 'max (mapcar
  13771. (lambda (x)
  13772. (if (stringp (car x)) (string-width (car x)) 0))
  13773. fulltable)))
  13774. (buf (current-buffer))
  13775. (expert (eq org-fast-tag-selection-single-key 'expert))
  13776. (buffer-tags nil)
  13777. (fwidth (+ maxlen 3 1 3))
  13778. (ncol (/ (- (window-width) 4) fwidth))
  13779. (i-face 'org-done)
  13780. (c-face 'org-todo)
  13781. tg cnt e c char c1 c2 ntable tbl rtn
  13782. ov-start ov-end ov-prefix
  13783. (exit-after-next org-fast-tag-selection-single-key)
  13784. (done-keywords org-done-keywords)
  13785. groups ingroup intaggroup)
  13786. (save-excursion
  13787. (beginning-of-line 1)
  13788. (if (looking-at
  13789. (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13790. (setq ov-start (match-beginning 1)
  13791. ov-end (match-end 1)
  13792. ov-prefix "")
  13793. (setq ov-start (1- (point-at-eol))
  13794. ov-end (1+ ov-start))
  13795. (skip-chars-forward "^\n\r")
  13796. (setq ov-prefix
  13797. (concat
  13798. (buffer-substring (1- (point)) (point))
  13799. (if (> (current-column) org-tags-column)
  13800. " "
  13801. (make-string (- org-tags-column (current-column)) ?\ ))))))
  13802. (move-overlay org-tags-overlay ov-start ov-end)
  13803. (save-window-excursion
  13804. (if expert
  13805. (set-buffer (get-buffer-create " *Org tags*"))
  13806. (delete-other-windows)
  13807. (set-window-buffer (split-window-vertically) (get-buffer-create " *Org tags*"))
  13808. (org-switch-to-buffer-other-window " *Org tags*"))
  13809. (erase-buffer)
  13810. (setq-local org-done-keywords done-keywords)
  13811. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  13812. (org-fast-tag-insert "Current" current c-face "\n\n")
  13813. (org-fast-tag-show-exit exit-after-next)
  13814. (org-set-current-tags-overlay current ov-prefix)
  13815. (setq tbl fulltable char ?a cnt 0)
  13816. (while (setq e (pop tbl))
  13817. (cond
  13818. ((eq (car e) :startgroup)
  13819. (push '() groups) (setq ingroup t)
  13820. (unless (zerop cnt)
  13821. (setq cnt 0)
  13822. (insert "\n"))
  13823. (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
  13824. ((eq (car e) :endgroup)
  13825. (setq ingroup nil cnt 0)
  13826. (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
  13827. ((eq (car e) :startgrouptag)
  13828. (setq intaggroup t)
  13829. (unless (zerop cnt)
  13830. (setq cnt 0)
  13831. (insert "\n"))
  13832. (insert "[ "))
  13833. ((eq (car e) :endgrouptag)
  13834. (setq intaggroup nil cnt 0)
  13835. (insert "]\n"))
  13836. ((equal e '(:newline))
  13837. (unless (zerop cnt)
  13838. (setq cnt 0)
  13839. (insert "\n")
  13840. (setq e (car tbl))
  13841. (while (equal (car tbl) '(:newline))
  13842. (insert "\n")
  13843. (setq tbl (cdr tbl)))))
  13844. ((equal e '(:grouptags)) (insert " : "))
  13845. (t
  13846. (setq tg (copy-sequence (car e)) c2 nil)
  13847. (if (cdr e)
  13848. (setq c (cdr e))
  13849. ;; automatically assign a character.
  13850. (setq c1 (string-to-char
  13851. (downcase (substring
  13852. tg (if (= (string-to-char tg) ?@) 1 0)))))
  13853. (if (or (rassoc c1 ntable) (rassoc c1 table))
  13854. (while (or (rassoc char ntable) (rassoc char table))
  13855. (setq char (1+ char)))
  13856. (setq c2 c1))
  13857. (setq c (or c2 char)))
  13858. (when ingroup (push tg (car groups)))
  13859. (setq tg (org-add-props tg nil 'face
  13860. (cond
  13861. ((not (assoc tg table))
  13862. (org-get-todo-face tg))
  13863. ((member tg current) c-face)
  13864. ((member tg inherited) i-face))))
  13865. (when (equal (caar tbl) :grouptags)
  13866. (org-add-props tg nil 'face 'org-tag-group))
  13867. (when (and (zerop cnt) (not ingroup) (not intaggroup)) (insert " "))
  13868. (insert "[" c "] " tg (make-string
  13869. (- fwidth 4 (length tg)) ?\ ))
  13870. (push (cons tg c) ntable)
  13871. (when (= (cl-incf cnt) ncol)
  13872. (insert "\n")
  13873. (when (or ingroup intaggroup) (insert " "))
  13874. (setq cnt 0)))))
  13875. (setq ntable (nreverse ntable))
  13876. (insert "\n")
  13877. (goto-char (point-min))
  13878. (unless expert (org-fit-window-to-buffer))
  13879. (setq rtn
  13880. (catch 'exit
  13881. (while t
  13882. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
  13883. (if (not groups) "no " "")
  13884. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  13885. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  13886. (setq org-last-tag-selection-key c)
  13887. (cond
  13888. ((= c ?\r) (throw 'exit t))
  13889. ((= c ?!)
  13890. (setq groups (not groups))
  13891. (goto-char (point-min))
  13892. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  13893. ((= c ?\C-c)
  13894. (if (not expert)
  13895. (org-fast-tag-show-exit
  13896. (setq exit-after-next (not exit-after-next)))
  13897. (setq expert nil)
  13898. (delete-other-windows)
  13899. (set-window-buffer (split-window-vertically) " *Org tags*")
  13900. (org-switch-to-buffer-other-window " *Org tags*")
  13901. (org-fit-window-to-buffer)))
  13902. ((or (= c ?\C-g)
  13903. (and (= c ?q) (not (rassoc c ntable))))
  13904. (org-detach-overlay org-tags-overlay)
  13905. (setq quit-flag t))
  13906. ((= c ?\ )
  13907. (setq current nil)
  13908. (when exit-after-next (setq exit-after-next 'now)))
  13909. ((= c ?\t)
  13910. (condition-case nil
  13911. (setq tg (completing-read
  13912. "Tag: "
  13913. (or buffer-tags
  13914. (with-current-buffer buf
  13915. (setq buffer-tags
  13916. (org-get-buffer-tags))))))
  13917. (quit (setq tg "")))
  13918. (when (string-match "\\S-" tg)
  13919. (cl-pushnew (list tg) buffer-tags :test #'equal)
  13920. (if (member tg current)
  13921. (setq current (delete tg current))
  13922. (push tg current)))
  13923. (when exit-after-next (setq exit-after-next 'now)))
  13924. ((setq e (rassoc c todo-table) tg (car e))
  13925. (with-current-buffer buf
  13926. (save-excursion (org-todo tg)))
  13927. (when exit-after-next (setq exit-after-next 'now)))
  13928. ((setq e (rassoc c ntable) tg (car e))
  13929. (if (member tg current)
  13930. (setq current (delete tg current))
  13931. (cl-loop for g in groups do
  13932. (when (member tg g)
  13933. (dolist (x g) (setq current (delete x current)))))
  13934. (push tg current))
  13935. (when exit-after-next (setq exit-after-next 'now))))
  13936. ;; Create a sorted list
  13937. (setq current
  13938. (sort current
  13939. (lambda (a b)
  13940. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  13941. (when (eq exit-after-next 'now) (throw 'exit t))
  13942. (goto-char (point-min))
  13943. (beginning-of-line 2)
  13944. (delete-region (point) (point-at-eol))
  13945. (org-fast-tag-insert "Current" current c-face)
  13946. (org-set-current-tags-overlay current ov-prefix)
  13947. (while (re-search-forward
  13948. (org-re "\\[.\\] \\([[:alnum:]_@#%]+\\)") nil t)
  13949. (setq tg (match-string 1))
  13950. (add-text-properties
  13951. (match-beginning 1) (match-end 1)
  13952. (list 'face
  13953. (cond
  13954. ((member tg current) c-face)
  13955. ((member tg inherited) i-face)
  13956. (t (get-text-property (match-beginning 1) 'face))))))
  13957. (goto-char (point-min)))))
  13958. (org-detach-overlay org-tags-overlay)
  13959. (if rtn
  13960. (mapconcat 'identity current ":")
  13961. nil))))
  13962. (defun org-get-tags-string ()
  13963. "Get the TAGS string in the current headline."
  13964. (unless (org-at-heading-p t)
  13965. (user-error "Not on a heading"))
  13966. (save-excursion
  13967. (beginning-of-line 1)
  13968. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13969. (org-match-string-no-properties 1)
  13970. "")))
  13971. (defun org-get-tags ()
  13972. "Get the list of tags specified in the current headline."
  13973. (org-split-string (org-get-tags-string) ":"))
  13974. (defun org-get-buffer-tags ()
  13975. "Get a table of all tags used in the buffer, for completion."
  13976. (org-with-wide-buffer
  13977. (goto-char (point-min))
  13978. (let ((tag-re (concat org-outline-regexp-bol
  13979. "\\(?:.*?[ \t]\\)?"
  13980. (org-re ":\\([[:alnum:]_@#%:]+\\):[ \t]*$")))
  13981. tags)
  13982. (while (re-search-forward tag-re nil t)
  13983. (dolist (tag (org-split-string (org-match-string-no-properties 1) ":"))
  13984. (push tag tags)))
  13985. (mapcar #'list (append org-file-tags (org-uniquify tags))))))
  13986. ;;;; The mapping API
  13987. (defvar org-agenda-skip-comment-trees)
  13988. (defvar org-agenda-skip-function)
  13989. (defun org-map-entries (func &optional match scope &rest skip)
  13990. "Call FUNC at each headline selected by MATCH in SCOPE.
  13991. FUNC is a function or a lisp form. The function will be called without
  13992. arguments, with the cursor positioned at the beginning of the headline.
  13993. The return values of all calls to the function will be collected and
  13994. returned as a list.
  13995. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  13996. does not need to preserve point. After evaluation, the cursor will be
  13997. moved to the end of the line (presumably of the headline of the
  13998. processed entry) and search continues from there. Under some
  13999. circumstances, this may not produce the wanted results. For example,
  14000. if you have removed (e.g. archived) the current (sub)tree it could
  14001. mean that the next entry will be skipped entirely. In such cases, you
  14002. can specify the position from where search should continue by making
  14003. FUNC set the variable `org-map-continue-from' to the desired buffer
  14004. position.
  14005. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  14006. Only headlines that are matched by this query will be considered during
  14007. the iteration. When MATCH is nil or t, all headlines will be
  14008. visited by the iteration.
  14009. SCOPE determines the scope of this command. It can be any of:
  14010. nil The current buffer, respecting the restriction if any
  14011. tree The subtree started with the entry at point
  14012. region The entries within the active region, if any
  14013. region-start-level
  14014. The entries within the active region, but only those at
  14015. the same level than the first one.
  14016. file The current buffer, without restriction
  14017. file-with-archives
  14018. The current buffer, and any archives associated with it
  14019. agenda All agenda files
  14020. agenda-with-archives
  14021. All agenda files with any archive files associated with them
  14022. \(file1 file2 ...)
  14023. If this is a list, all files in the list will be scanned
  14024. The remaining args are treated as settings for the skipping facilities of
  14025. the scanner. The following items can be given here:
  14026. archive skip trees with the archive tag
  14027. comment skip trees with the COMMENT keyword
  14028. function or Emacs Lisp form:
  14029. will be used as value for `org-agenda-skip-function', so
  14030. whenever the function returns a position, FUNC will not be
  14031. called for that entry and search will continue from the
  14032. position returned
  14033. If your function needs to retrieve the tags including inherited tags
  14034. at the *current* entry, you can use the value of the variable
  14035. `org-scanner-tags' which will be much faster than getting the value
  14036. with `org-get-tags-at'. If your function gets properties with
  14037. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  14038. to t around the call to `org-entry-properties' to get the same speedup.
  14039. Note that if your function moves around to retrieve tags and properties at
  14040. a *different* entry, you cannot use these techniques."
  14041. (unless (and (or (eq scope 'region) (eq scope 'region-start-level))
  14042. (not (org-region-active-p)))
  14043. (let* ((org-agenda-archives-mode nil) ; just to make sure
  14044. (org-agenda-skip-archived-trees (memq 'archive skip))
  14045. (org-agenda-skip-comment-trees (memq 'comment skip))
  14046. (org-agenda-skip-function
  14047. (car (org-delete-all '(comment archive) skip)))
  14048. (org-tags-match-list-sublevels t)
  14049. (start-level (eq scope 'region-start-level))
  14050. matcher res
  14051. org-todo-keywords-for-agenda
  14052. org-done-keywords-for-agenda
  14053. org-todo-keyword-alist-for-agenda
  14054. org-tag-alist-for-agenda
  14055. org--matcher-tags-todo-only)
  14056. (cond
  14057. ((eq match t) (setq matcher t))
  14058. ((eq match nil) (setq matcher t))
  14059. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  14060. (save-excursion
  14061. (save-restriction
  14062. (cond ((eq scope 'tree)
  14063. (org-back-to-heading t)
  14064. (org-narrow-to-subtree)
  14065. (setq scope nil))
  14066. ((and (or (eq scope 'region) (eq scope 'region-start-level))
  14067. (org-region-active-p))
  14068. ;; If needed, set start-level to a string like "2"
  14069. (when start-level
  14070. (save-excursion
  14071. (goto-char (region-beginning))
  14072. (unless (org-at-heading-p) (outline-next-heading))
  14073. (setq start-level (org-current-level))))
  14074. (narrow-to-region (region-beginning)
  14075. (save-excursion
  14076. (goto-char (region-end))
  14077. (unless (and (bolp) (org-at-heading-p))
  14078. (outline-next-heading))
  14079. (point)))
  14080. (setq scope nil)))
  14081. (if (not scope)
  14082. (progn
  14083. (org-agenda-prepare-buffers
  14084. (and buffer-file-name (list buffer-file-name)))
  14085. (setq res
  14086. (org-scan-tags
  14087. func matcher org--matcher-tags-todo-only start-level)))
  14088. ;; Get the right scope
  14089. (cond
  14090. ((and scope (listp scope) (symbolp (car scope)))
  14091. (setq scope (eval scope)))
  14092. ((eq scope 'agenda)
  14093. (setq scope (org-agenda-files t)))
  14094. ((eq scope 'agenda-with-archives)
  14095. (setq scope (org-agenda-files t))
  14096. (setq scope (org-add-archive-files scope)))
  14097. ((eq scope 'file)
  14098. (setq scope (and buffer-file-name (list buffer-file-name))))
  14099. ((eq scope 'file-with-archives)
  14100. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  14101. (org-agenda-prepare-buffers scope)
  14102. (dolist (file scope)
  14103. (with-current-buffer (org-find-base-buffer-visiting file)
  14104. (save-excursion
  14105. (save-restriction
  14106. (widen)
  14107. (goto-char (point-min))
  14108. (setq res
  14109. (append
  14110. res
  14111. (org-scan-tags
  14112. func matcher org--matcher-tags-todo-only))))))))))
  14113. res)))
  14114. ;;; Properties API
  14115. (defconst org-special-properties
  14116. '("ALLTAGS" "BLOCKED" "CLOCKSUM" "CLOCKSUM_T" "CLOSED" "DEADLINE" "FILE"
  14117. "ITEM" "PRIORITY" "SCHEDULED" "TAGS" "TIMESTAMP" "TIMESTAMP_IA" "TODO")
  14118. "The special properties valid in Org mode.
  14119. These are properties that are not defined in the property drawer,
  14120. but in some other way.")
  14121. (defconst org-default-properties
  14122. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  14123. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  14124. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  14125. "EXPORT_OPTIONS" "EXPORT_TEXT" "EXPORT_FILE_NAME"
  14126. "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE" "UNNUMBERED"
  14127. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER" "REPEAT_TO_STATE"
  14128. "CLOCK_MODELINE_TOTAL" "STYLE" "HTML_CONTAINER_CLASS")
  14129. "Some properties that are used by Org mode for various purposes.
  14130. Being in this list makes sure that they are offered for completion.")
  14131. (defun org--valid-property-p (property)
  14132. "Non nil when string PROPERTY is a valid property name."
  14133. (not
  14134. (or (equal property "")
  14135. (org-string-match-p "\\s-" property))))
  14136. (defun org--update-property-plist (key val props)
  14137. "Associate KEY to VAL in alist PROPS.
  14138. Modifications are made by side-effect. Return new alist."
  14139. (let* ((appending (string= (substring key -1) "+"))
  14140. (key (if appending (substring key 0 -1) key))
  14141. (old (assoc-string key props t)))
  14142. (if (not old) (cons (cons key val) props)
  14143. (setcdr old (if appending (concat (cdr old) " " val) val))
  14144. props)))
  14145. (defun org-get-property-block (&optional beg force)
  14146. "Return the (beg . end) range of the body of the property drawer.
  14147. BEG is the beginning of the current subtree, or of the part
  14148. before the first headline. If it is not given, it will be found.
  14149. If the drawer does not exist, create it if FORCE is non-nil, or
  14150. return nil."
  14151. (org-with-wide-buffer
  14152. (when beg (goto-char beg))
  14153. (unless (org-before-first-heading-p)
  14154. (let ((beg (cond (beg)
  14155. ((or (not (featurep 'org-inlinetask))
  14156. (org-inlinetask-in-task-p))
  14157. (org-back-to-heading t))
  14158. (t (org-with-limited-levels (org-back-to-heading t))))))
  14159. (forward-line)
  14160. (when (org-looking-at-p org-planning-line-re) (forward-line))
  14161. (cond ((looking-at org-property-drawer-re)
  14162. (forward-line)
  14163. (cons (point) (progn (goto-char (match-end 0))
  14164. (line-beginning-position))))
  14165. (force
  14166. (goto-char beg)
  14167. (org-insert-property-drawer)
  14168. (let ((pos (save-excursion (search-forward ":END:")
  14169. (line-beginning-position))))
  14170. (cons pos pos))))))))
  14171. (defun org-at-property-p ()
  14172. "Non-nil when point is inside a property drawer.
  14173. See `org-property-re' for match data, if applicable."
  14174. (save-excursion
  14175. (beginning-of-line)
  14176. (and (looking-at org-property-re)
  14177. (let ((property-drawer (save-match-data (org-get-property-block))))
  14178. (and property-drawer
  14179. (>= (point) (car property-drawer))
  14180. (< (point) (cdr property-drawer)))))))
  14181. (defun org-property-action ()
  14182. "Do an action on properties."
  14183. (interactive)
  14184. (unless (org-at-property-p) (user-error "Not at a property"))
  14185. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  14186. (let ((c (read-char-exclusive)))
  14187. (cl-case c
  14188. (?s (call-interactively #'org-set-property))
  14189. (?d (call-interactively #'org-delete-property))
  14190. (?D (call-interactively #'org-delete-property-globally))
  14191. (?c (call-interactively #'org-compute-property-at-point))
  14192. (otherwise (user-error "No such property action %c" c)))))
  14193. (defun org-inc-effort ()
  14194. "Increment the value of the effort property in the current entry."
  14195. (interactive)
  14196. (org-set-effort nil t))
  14197. (defvar org-clock-effort) ; Defined in org-clock.el.
  14198. (defvar org-clock-current-task) ; Defined in org-clock.el.
  14199. (defun org-set-effort (&optional value increment)
  14200. "Set the effort property of the current entry.
  14201. With numerical prefix arg, use the nth allowed value, 0 stands for the
  14202. 10th allowed value.
  14203. When INCREMENT is non-nil, set the property to the next allowed value."
  14204. (interactive "P")
  14205. (when (equal value 0) (setq value 10))
  14206. (let* ((completion-ignore-case t)
  14207. (prop org-effort-property)
  14208. (cur (org-entry-get nil prop))
  14209. (allowed (org-property-get-allowed-values nil prop 'table))
  14210. (existing (mapcar 'list (org-property-values prop)))
  14211. (heading (nth 4 (org-heading-components)))
  14212. rpl
  14213. (val (cond
  14214. ((stringp value) value)
  14215. ((and allowed (integerp value))
  14216. (or (car (nth (1- value) allowed))
  14217. (car (org-last allowed))))
  14218. ((and allowed increment)
  14219. (or (cl-caadr (member (list cur) allowed))
  14220. (user-error "Allowed effort values are not set")))
  14221. (allowed
  14222. (message "Select 1-9,0, [RET%s]: %s"
  14223. (if cur (concat "=" cur) "")
  14224. (mapconcat 'car allowed " "))
  14225. (setq rpl (read-char-exclusive))
  14226. (if (equal rpl ?\r)
  14227. cur
  14228. (setq rpl (- rpl ?0))
  14229. (when (equal rpl 0) (setq rpl 10))
  14230. (if (and (> rpl 0) (<= rpl (length allowed)))
  14231. (car (nth (1- rpl) allowed))
  14232. (org-completing-read "Effort: " allowed nil))))
  14233. (t
  14234. (org-completing-read
  14235. (concat "Effort " (if (and cur (string-match "\\S-" cur))
  14236. (concat "[" cur "]") "")
  14237. ": ")
  14238. existing nil nil "" nil cur)))))
  14239. (unless (equal (org-entry-get nil prop) val)
  14240. (org-entry-put nil prop val))
  14241. (org-refresh-property
  14242. '((effort . identity)
  14243. (effort-minutes . org-duration-string-to-minutes))
  14244. val)
  14245. (when (string= heading org-clock-current-task)
  14246. (setq org-clock-effort (get-text-property (point-at-bol) 'effort))
  14247. (org-clock-update-mode-line))
  14248. (message "%s is now %s" prop val)))
  14249. (defun org-entry-properties (&optional pom which)
  14250. "Get all properties of the current entry.
  14251. When POM is a buffer position, get all properties from the entry
  14252. there instead.
  14253. This includes the TODO keyword, the tags, time strings for
  14254. deadline, scheduled, and clocking, and any additional properties
  14255. defined in the entry.
  14256. If WHICH is nil or `all', get all properties. If WHICH is
  14257. `special' or `standard', only get that subclass. If WHICH is
  14258. a string, only get that property.
  14259. Return value is an alist. Keys are properties, as upcased
  14260. strings."
  14261. (org-with-point-at pom
  14262. (when (and (derived-mode-p 'org-mode)
  14263. (ignore-errors (org-back-to-heading t)))
  14264. (catch 'exit
  14265. (let* ((beg (point))
  14266. (specific (and (stringp which) (upcase which)))
  14267. (which (cond ((not specific) which)
  14268. ((member specific org-special-properties) 'special)
  14269. (t 'standard)))
  14270. props)
  14271. ;; Get the special properties, like TODO and TAGS.
  14272. (when (memq which '(nil all special))
  14273. (when (or (not specific) (string= specific "CLOCKSUM"))
  14274. (let ((clocksum (get-text-property (point) :org-clock-minutes)))
  14275. (when clocksum
  14276. (push (cons "CLOCKSUM"
  14277. (org-minutes-to-clocksum-string clocksum))
  14278. props)))
  14279. (when specific (throw 'exit props)))
  14280. (when (or (not specific) (string= specific "CLOCKSUM_T"))
  14281. (let ((clocksumt (get-text-property (point)
  14282. :org-clock-minutes-today)))
  14283. (when clocksumt
  14284. (push (cons "CLOCKSUM_T"
  14285. (org-minutes-to-clocksum-string clocksumt))
  14286. props)))
  14287. (when specific (throw 'exit props)))
  14288. (when (or (not specific) (string= specific "ITEM"))
  14289. (when (looking-at org-complex-heading-regexp)
  14290. (push (cons "ITEM"
  14291. (let ((title (match-string-no-properties 4)))
  14292. (if (org-string-nw-p title)
  14293. (org-remove-tabs title)
  14294. "")))
  14295. props))
  14296. (when specific (throw 'exit props)))
  14297. (when (or (not specific) (string= specific "TODO"))
  14298. (let ((case-fold-search nil))
  14299. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  14300. (push (cons "TODO" (org-match-string-no-properties 2)) props)))
  14301. (when specific (throw 'exit props)))
  14302. (when (or (not specific) (string= specific "PRIORITY"))
  14303. (push (cons "PRIORITY"
  14304. (if (looking-at org-priority-regexp)
  14305. (org-match-string-no-properties 2)
  14306. (char-to-string org-default-priority)))
  14307. props)
  14308. (when specific (throw 'exit props)))
  14309. (when (or (not specific) (string= specific "FILE"))
  14310. (push (cons "FILE" (buffer-file-name (buffer-base-buffer)))
  14311. props)
  14312. (when specific (throw 'exit props)))
  14313. (when (or (not specific) (string= specific "TAGS"))
  14314. (let ((value (org-string-nw-p (org-get-tags-string))))
  14315. (when value (push (cons "TAGS" value) props)))
  14316. (when specific (throw 'exit props)))
  14317. (when (or (not specific) (string= specific "ALLTAGS"))
  14318. (let ((value (org-get-tags-at)))
  14319. (when value
  14320. (push (cons "ALLTAGS"
  14321. (format ":%s:" (mapconcat #'identity value ":")))
  14322. props)))
  14323. (when specific (throw 'exit props)))
  14324. (when (or (not specific) (string= specific "BLOCKED"))
  14325. (push (cons "BLOCKED" (if (org-entry-blocked-p) "t" "")) props)
  14326. (when specific (throw 'exit props)))
  14327. (when (or (not specific)
  14328. (member specific '("CLOSED" "DEADLINE" "SCHEDULED")))
  14329. (forward-line)
  14330. (when (org-looking-at-p org-planning-line-re)
  14331. (end-of-line)
  14332. (let ((bol (line-beginning-position))
  14333. ;; Backward compatibility: time keywords used to
  14334. ;; be configurable (before 8.3). Make sure we
  14335. ;; get the correct keyword.
  14336. (key-assoc `(("CLOSED" . ,org-closed-string)
  14337. ("DEADLINE" . ,org-deadline-string)
  14338. ("SCHEDULED" . ,org-scheduled-string))))
  14339. (dolist (pair (if specific (list (assoc specific key-assoc))
  14340. key-assoc))
  14341. (save-excursion
  14342. (when (search-backward (cdr pair) bol t)
  14343. (goto-char (match-end 0))
  14344. (skip-chars-forward " \t")
  14345. (and (looking-at org-ts-regexp-both)
  14346. (push (cons (car pair)
  14347. (org-match-string-no-properties 0))
  14348. props)))))))
  14349. (when specific (throw 'exit props)))
  14350. (when (or (not specific)
  14351. (member specific '("TIMESTAMP" "TIMESTAMP_IA")))
  14352. (let ((find-ts
  14353. (lambda (end ts)
  14354. ;; Fix next time-stamp before END. TS is the
  14355. ;; list of time-stamps found so far.
  14356. (let ((ts ts)
  14357. (regexp (cond
  14358. ((string= specific "TIMESTAMP")
  14359. org-ts-regexp)
  14360. ((string= specific "TIMESTAMP_IA")
  14361. org-ts-regexp-inactive)
  14362. ((assoc "TIMESTAMP_IA" ts)
  14363. org-ts-regexp)
  14364. ((assoc "TIMESTAMP" ts)
  14365. org-ts-regexp-inactive)
  14366. (t org-ts-regexp-both))))
  14367. (catch 'next
  14368. (while (re-search-forward regexp end t)
  14369. (backward-char)
  14370. (let ((object (org-element-context)))
  14371. ;; Accept to match timestamps in node
  14372. ;; properties, too.
  14373. (when (memq (org-element-type object)
  14374. '(node-property timestamp))
  14375. (let ((type
  14376. (org-element-property :type object)))
  14377. (cond
  14378. ((and (memq type '(active active-range))
  14379. (not (equal specific "TIMESTAMP_IA")))
  14380. (unless (assoc "TIMESTAMP" ts)
  14381. (push (cons "TIMESTAMP"
  14382. (org-element-property
  14383. :raw-value object))
  14384. ts)
  14385. (when specific (throw 'exit ts))))
  14386. ((and (memq type '(inactive inactive-range))
  14387. (not (string= specific "TIMESTAMP")))
  14388. (unless (assoc "TIMESTAMP_IA" ts)
  14389. (push (cons "TIMESTAMP_IA"
  14390. (org-element-property
  14391. :raw-value object))
  14392. ts)
  14393. (when specific (throw 'exit ts))))))
  14394. ;; Both timestamp types are found,
  14395. ;; move to next part.
  14396. (when (= (length ts) 2) (throw 'next ts)))))
  14397. ts)))))
  14398. (goto-char beg)
  14399. ;; First look for timestamps within headline.
  14400. (let ((ts (funcall find-ts (line-end-position) nil)))
  14401. (if (= (length ts) 2) (setq props (nconc ts props))
  14402. (forward-line)
  14403. ;; Then find timestamps in the section, skipping
  14404. ;; planning line.
  14405. (when (org-looking-at-p org-planning-line-re)
  14406. (forward-line))
  14407. (let ((end (save-excursion (outline-next-heading))))
  14408. (setq props (nconc (funcall find-ts end ts) props))))))))
  14409. ;; Get the standard properties, like :PROP:.
  14410. (when (memq which '(nil all standard))
  14411. ;; If we are looking after a specific property, delegate
  14412. ;; to `org-entry-get', which is faster. However, make an
  14413. ;; exception for "CATEGORY", since it can be also set
  14414. ;; through keywords (i.e. #+CATEGORY).
  14415. (if (and specific (not (equal specific "CATEGORY")))
  14416. (let ((value (org-entry-get beg specific nil t)))
  14417. (throw 'exit (and value (list (cons specific value)))))
  14418. (let ((range (org-get-property-block beg)))
  14419. (when range
  14420. (let ((end (cdr range)) seen-base)
  14421. (goto-char (car range))
  14422. ;; Unlike to `org--update-property-plist', we
  14423. ;; handle the case where base values is found
  14424. ;; after its extension. We also forbid standard
  14425. ;; properties to be named as special properties.
  14426. (while (re-search-forward org-property-re end t)
  14427. (let* ((key (upcase (org-match-string-no-properties 2)))
  14428. (extendp (org-string-match-p "\\+\\'" key))
  14429. (key-base (if extendp (substring key 0 -1) key))
  14430. (value (org-match-string-no-properties 3)))
  14431. (cond
  14432. ((member-ignore-case key-base org-special-properties))
  14433. (extendp
  14434. (setq props
  14435. (org--update-property-plist key value props)))
  14436. ((member key seen-base))
  14437. (t (push key seen-base)
  14438. (let ((p (assoc-string key props t)))
  14439. (if p (setcdr p (concat value " " (cdr p)))
  14440. (push (cons key value) props))))))))))))
  14441. (unless (assoc "CATEGORY" props)
  14442. (push (cons "CATEGORY" (org-get-category beg)) props)
  14443. (when (string= specific "CATEGORY") (throw 'exit props)))
  14444. ;; Return value.
  14445. props)))))
  14446. (defun org-property--local-values (property literal-nil)
  14447. "Return value for PROPERTY in current entry.
  14448. Value is a list whose car is the base value for PROPERTY and cdr
  14449. a list of accumulated values. Return nil if neither is found in
  14450. the entry. Also return nil when PROPERTY is set to \"nil\",
  14451. unless LITERAL-NIL is non-nil."
  14452. (let ((range (org-get-property-block)))
  14453. (when range
  14454. (goto-char (car range))
  14455. (let* ((case-fold-search t)
  14456. (end (cdr range))
  14457. (value
  14458. ;; Base value.
  14459. (save-excursion
  14460. (let ((v (and (re-search-forward
  14461. (org-re-property property nil t) end t)
  14462. (org-match-string-no-properties 3))))
  14463. (list (if literal-nil v (org-not-nil v)))))))
  14464. ;; Find additional values.
  14465. (let* ((property+ (org-re-property (concat property "+") nil t)))
  14466. (while (re-search-forward property+ end t)
  14467. (push (org-match-string-no-properties 3) value)))
  14468. ;; Return final values.
  14469. (and (not (equal value '(nil))) (nreverse value))))))
  14470. (defun org-entry-get (pom property &optional inherit literal-nil)
  14471. "Get value of PROPERTY for entry or content at point-or-marker POM.
  14472. If INHERIT is non-nil and the entry does not have the property,
  14473. then also check higher levels of the hierarchy. If INHERIT is
  14474. the symbol `selective', use inheritance only if the setting in
  14475. `org-use-property-inheritance' selects PROPERTY for inheritance.
  14476. If the property is present but empty, the return value is the
  14477. empty string. If the property is not present at all, nil is
  14478. returned. In any other case, return the value as a string.
  14479. Search is case-insensitive.
  14480. If LITERAL-NIL is set, return the string value \"nil\" as
  14481. a string, do not interpret it as the list atom nil. This is used
  14482. for inheritance when a \"nil\" value can supersede a non-nil
  14483. value higher up the hierarchy."
  14484. (org-with-point-at pom
  14485. (cond
  14486. ((member-ignore-case property (cons "CATEGORY" org-special-properties))
  14487. ;; We need a special property. Use `org-entry-properties' to
  14488. ;; retrieve it, but specify the wanted property.
  14489. (cdr (assoc-string property (org-entry-properties nil property))))
  14490. ((and inherit
  14491. (or (not (eq inherit 'selective)) (org-property-inherit-p property)))
  14492. (org-entry-get-with-inheritance property literal-nil))
  14493. (t
  14494. (let* ((local (org-property--local-values property literal-nil))
  14495. (value (and local (mapconcat #'identity (delq nil local) " "))))
  14496. (if literal-nil value (org-not-nil value)))))))
  14497. (defun org-property-or-variable-value (var &optional inherit)
  14498. "Check if there is a property fixing the value of VAR.
  14499. If yes, return this value. If not, return the current value of the variable."
  14500. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  14501. (if (and prop (stringp prop) (string-match "\\S-" prop))
  14502. (read prop)
  14503. (symbol-value var))))
  14504. (defun org-entry-delete (pom property)
  14505. "Delete PROPERTY from entry at point-or-marker POM.
  14506. Accumulated properties, i.e. PROPERTY+, are also removed. Return
  14507. non-nil when a property was removed."
  14508. (unless (member property org-special-properties)
  14509. (org-with-point-at pom
  14510. (let ((range (org-get-property-block)))
  14511. (when range
  14512. (let* ((begin (car range))
  14513. (origin (cdr range))
  14514. (end (copy-marker origin))
  14515. (re (org-re-property
  14516. (concat (regexp-quote property) "\\+?") t t)))
  14517. (goto-char begin)
  14518. (while (re-search-forward re end t)
  14519. (delete-region (match-beginning 0) (line-beginning-position 2)))
  14520. ;; If drawer is empty, remove it altogether.
  14521. (when (= begin end)
  14522. (delete-region (line-beginning-position 0)
  14523. (line-beginning-position 2)))
  14524. ;; Return non-nil if some property was removed.
  14525. (prog1 (/= end origin) (set-marker end nil))))))))
  14526. ;; Multi-values properties are properties that contain multiple values
  14527. ;; These values are assumed to be single words, separated by whitespace.
  14528. (defun org-entry-add-to-multivalued-property (pom property value)
  14529. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  14530. (let* ((old (org-entry-get pom property))
  14531. (values (and old (org-split-string old "[ \t]"))))
  14532. (setq value (org-entry-protect-space value))
  14533. (unless (member value values)
  14534. (setq values (append values (list value)))
  14535. (org-entry-put pom property
  14536. (mapconcat 'identity values " ")))))
  14537. (defun org-entry-remove-from-multivalued-property (pom property value)
  14538. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  14539. (let* ((old (org-entry-get pom property))
  14540. (values (and old (org-split-string old "[ \t]"))))
  14541. (setq value (org-entry-protect-space value))
  14542. (when (member value values)
  14543. (setq values (delete value values))
  14544. (org-entry-put pom property
  14545. (mapconcat 'identity values " ")))))
  14546. (defun org-entry-member-in-multivalued-property (pom property value)
  14547. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  14548. (let* ((old (org-entry-get pom property))
  14549. (values (and old (org-split-string old "[ \t]"))))
  14550. (setq value (org-entry-protect-space value))
  14551. (member value values)))
  14552. (defun org-entry-get-multivalued-property (pom property)
  14553. "Return a list of values in a multivalued property."
  14554. (let* ((value (org-entry-get pom property))
  14555. (values (and value (org-split-string value "[ \t]"))))
  14556. (mapcar 'org-entry-restore-space values)))
  14557. (defun org-entry-put-multivalued-property (pom property &rest values)
  14558. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  14559. VALUES should be a list of strings. Spaces will be protected."
  14560. (org-entry-put pom property
  14561. (mapconcat 'org-entry-protect-space values " "))
  14562. (let* ((value (org-entry-get pom property))
  14563. (values (and value (org-split-string value "[ \t]"))))
  14564. (mapcar 'org-entry-restore-space values)))
  14565. (defun org-entry-protect-space (s)
  14566. "Protect spaces and newline in string S."
  14567. (while (string-match " " s)
  14568. (setq s (replace-match "%20" t t s)))
  14569. (while (string-match "\n" s)
  14570. (setq s (replace-match "%0A" t t s)))
  14571. s)
  14572. (defun org-entry-restore-space (s)
  14573. "Restore spaces and newline in string S."
  14574. (while (string-match "%20" s)
  14575. (setq s (replace-match " " t t s)))
  14576. (while (string-match "%0A" s)
  14577. (setq s (replace-match "\n" t t s)))
  14578. s)
  14579. (defvar org-entry-property-inherited-from (make-marker)
  14580. "Marker pointing to the entry from where a property was inherited.
  14581. Each call to `org-entry-get-with-inheritance' will set this marker to the
  14582. location of the entry where the inheritance search matched. If there was
  14583. no match, the marker will point nowhere.
  14584. Note that also `org-entry-get' calls this function, if the INHERIT flag
  14585. is set.")
  14586. (defun org-entry-get-with-inheritance (property &optional literal-nil)
  14587. "Get PROPERTY of entry or content at point, search higher levels if needed.
  14588. The search will stop at the first ancestor which has the property defined.
  14589. If the value found is \"nil\", return nil to show that the property
  14590. should be considered as undefined (this is the meaning of nil here).
  14591. However, if LITERAL-NIL is set, return the string value \"nil\" instead."
  14592. (move-marker org-entry-property-inherited-from nil)
  14593. (org-with-wide-buffer
  14594. (let (value)
  14595. (catch 'exit
  14596. (while t
  14597. (let ((v (org-property--local-values property literal-nil)))
  14598. (when v
  14599. (setq value
  14600. (concat (mapconcat #'identity (delq nil v) " ")
  14601. (and value " ")
  14602. value)))
  14603. (cond
  14604. ((car v)
  14605. (org-back-to-heading t)
  14606. (move-marker org-entry-property-inherited-from (point))
  14607. (throw 'exit nil))
  14608. ((org-up-heading-safe))
  14609. (t
  14610. (let ((global
  14611. (cdr (or (assoc-string property org-file-properties t)
  14612. (assoc-string property org-global-properties t)
  14613. (assoc-string property org-global-properties-fixed t)))))
  14614. (cond ((not global))
  14615. (value (setq value (concat global " " value)))
  14616. (t (setq value global))))
  14617. (throw 'exit nil))))))
  14618. (if literal-nil value (org-not-nil value)))))
  14619. (defvar org-property-changed-functions nil
  14620. "Hook called when the value of a property has changed.
  14621. Each hook function should accept two arguments, the name of the property
  14622. and the new value.")
  14623. (defun org-entry-put (pom property value)
  14624. "Set PROPERTY to VALUE for entry at point-or-marker POM.
  14625. If the value is nil, it is converted to the empty string. If it
  14626. is not a string, an error is raised. Also raise an error on
  14627. invalid property names.
  14628. PROPERTY can be any regular property (see
  14629. `org-special-properties'). It can also be \"TODO\",
  14630. \"PRIORITY\", \"SCHEDULED\" and \"DEADLINE\".
  14631. For the last two properties, VALUE may have any of the special
  14632. values \"earlier\" and \"later\". The function then increases or
  14633. decreases scheduled or deadline date by one day."
  14634. (cond ((null value) (setq value ""))
  14635. ((not (stringp value)) (error "Properties values should be strings"))
  14636. ((not (org--valid-property-p property))
  14637. (user-error "Invalid property name: \"%s\"" property)))
  14638. (org-with-point-at pom
  14639. (if (or (not (featurep 'org-inlinetask)) (org-inlinetask-in-task-p))
  14640. (org-back-to-heading t)
  14641. (org-with-limited-levels (org-back-to-heading t)))
  14642. (let ((beg (point)))
  14643. (cond
  14644. ((equal property "TODO")
  14645. (cond ((not (org-string-nw-p value)) (setq value 'none))
  14646. ((not (member value org-todo-keywords-1))
  14647. (user-error "\"%s\" is not a valid TODO state" value)))
  14648. (org-todo value)
  14649. (org-set-tags nil 'align))
  14650. ((equal property "PRIORITY")
  14651. (org-priority (if (org-string-nw-p value) (string-to-char value) ?\s))
  14652. (org-set-tags nil 'align))
  14653. ((equal property "SCHEDULED")
  14654. (forward-line)
  14655. (if (and (org-looking-at-p org-planning-line-re)
  14656. (re-search-forward
  14657. org-scheduled-time-regexp (line-end-position) t))
  14658. (cond ((string= value "earlier") (org-timestamp-change -1 'day))
  14659. ((string= value "later") (org-timestamp-change 1 'day))
  14660. ((string= value "") (org-schedule '(4)))
  14661. (t (org-schedule nil value)))
  14662. (if (member value '("earlier" "later" ""))
  14663. (call-interactively #'org-schedule)
  14664. (org-schedule nil value))))
  14665. ((equal property "DEADLINE")
  14666. (forward-line)
  14667. (if (and (org-looking-at-p org-planning-line-re)
  14668. (re-search-forward
  14669. org-deadline-time-regexp (line-end-position) t))
  14670. (cond ((string= value "earlier") (org-timestamp-change -1 'day))
  14671. ((string= value "later") (org-timestamp-change 1 'day))
  14672. ((string= value "") (org-deadline '(4)))
  14673. (t (org-deadline nil value)))
  14674. (if (member value '("earlier" "later" ""))
  14675. (call-interactively #'org-deadline)
  14676. (org-deadline nil value))))
  14677. ((member property org-special-properties)
  14678. (error "The %s property cannot be set with `org-entry-put'" property))
  14679. (t
  14680. (let* ((range (org-get-property-block beg 'force))
  14681. (end (cdr range))
  14682. (case-fold-search t))
  14683. (goto-char (car range))
  14684. (if (re-search-forward (org-re-property property nil t) end t)
  14685. (progn (delete-region (match-beginning 0) (match-end 0))
  14686. (goto-char (match-beginning 0)))
  14687. (goto-char end)
  14688. (insert "\n")
  14689. (backward-char))
  14690. (insert ":" property ":")
  14691. (when value (insert " " value))
  14692. (org-indent-line)))))
  14693. (run-hook-with-args 'org-property-changed-functions property value)))
  14694. (defun org-buffer-property-keys
  14695. (&optional specials defaults columns ignore-malformed)
  14696. "Get all property keys in the current buffer.
  14697. When SPECIALS is non-nil, also list the special properties that
  14698. reflect things like tags and TODO state.
  14699. When DEFAULTS is non-nil, also include properties that has
  14700. special meaning internally: ARCHIVE, CATEGORY, SUMMARY,
  14701. DESCRIPTION, LOCATION, and LOGGING and others.
  14702. When COLUMNS in non-nil, also include property names given in
  14703. COLUMN formats in the current buffer.
  14704. When IGNORE-MALFORMED is non-nil, malformed drawer repair will not be
  14705. automatically performed, such drawers will be silently ignored."
  14706. (let ((case-fold-search t)
  14707. (props (append
  14708. (and specials org-special-properties)
  14709. (and defaults (cons org-effort-property org-default-properties))
  14710. nil)))
  14711. (org-with-wide-buffer
  14712. (goto-char (point-min))
  14713. (while (re-search-forward org-property-start-re nil t)
  14714. (let ((range (org-get-property-block)))
  14715. (catch 'skip
  14716. (unless range
  14717. (when (and (not ignore-malformed)
  14718. (not (org-before-first-heading-p))
  14719. (y-or-n-p (format "Malformed drawer at %d, repair?"
  14720. (line-beginning-position))))
  14721. (org-get-property-block nil t))
  14722. (throw 'skip nil))
  14723. (goto-char (car range))
  14724. (let ((begin (car range))
  14725. (end (cdr range)))
  14726. ;; Make sure that found property block is not located
  14727. ;; before current point, as it would generate an infloop.
  14728. ;; It can happen, for example, in the following
  14729. ;; situation:
  14730. ;;
  14731. ;; * Headline
  14732. ;; :PROPERTIES:
  14733. ;; ...
  14734. ;; :END:
  14735. ;; *************** Inlinetask
  14736. ;; #+BEGIN_EXAMPLE
  14737. ;; :PROPERTIES:
  14738. ;; #+END_EXAMPLE
  14739. ;;
  14740. (if (< begin (point)) (throw 'skip nil) (goto-char begin))
  14741. (while (< (point) end)
  14742. (let ((p (progn (looking-at org-property-re)
  14743. (org-match-string-no-properties 2))))
  14744. ;; Only add true property name, not extension symbol.
  14745. (push (if (not (org-string-match-p "\\+\\'" p)) p
  14746. (substring p 0 -1))
  14747. props))
  14748. (forward-line))))
  14749. (outline-next-heading)))
  14750. (when columns
  14751. (goto-char (point-min))
  14752. (while (re-search-forward "^[ \t]*\\(?:#\\+\\|:\\)COLUMNS:" nil t)
  14753. (let ((element (org-element-at-point)))
  14754. (when (memq (org-element-type element) '(keyword node-property))
  14755. (let ((value (org-element-property :value element))
  14756. (start 0))
  14757. (while (string-match "%[0-9]*\\(\\S-+\\)" value start)
  14758. (setq start (match-end 0))
  14759. (let ((p (org-match-string-no-properties 1 value)))
  14760. (unless (member-ignore-case p org-special-properties)
  14761. (push p props))))))))))
  14762. (sort (delete-dups props) (lambda (a b) (string< (upcase a) (upcase b))))))
  14763. (defun org-property-values (key)
  14764. "List all non-nil values of property KEY in current buffer."
  14765. (org-with-wide-buffer
  14766. (goto-char (point-min))
  14767. (let ((case-fold-search t)
  14768. (re (org-re-property key))
  14769. values)
  14770. (while (re-search-forward re nil t)
  14771. (push (org-entry-get (point) key) values))
  14772. (delete-dups values))))
  14773. (defun org-insert-property-drawer ()
  14774. "Insert a property drawer into the current entry."
  14775. (org-with-wide-buffer
  14776. (if (or (not (featurep 'org-inlinetask)) (org-inlinetask-in-task-p))
  14777. (org-back-to-heading t)
  14778. (org-with-limited-levels (org-back-to-heading t)))
  14779. (forward-line)
  14780. (when (org-looking-at-p org-planning-line-re) (forward-line))
  14781. (unless (org-looking-at-p org-property-drawer-re)
  14782. ;; Make sure we start editing a line from current entry, not from
  14783. ;; next one. It prevents extending text properties or overlays
  14784. ;; belonging to the latter.
  14785. (when (bolp) (backward-char))
  14786. (let ((begin (1+ (point)))
  14787. (inhibit-read-only t))
  14788. (insert "\n:PROPERTIES:\n:END:")
  14789. (when (eobp) (insert "\n"))
  14790. (org-indent-region begin (point))))))
  14791. (defun org-insert-drawer (&optional arg drawer)
  14792. "Insert a drawer at point.
  14793. When optional argument ARG is non-nil, insert a property drawer.
  14794. Optional argument DRAWER, when non-nil, is a string representing
  14795. drawer's name. Otherwise, the user is prompted for a name.
  14796. If a region is active, insert the drawer around that region
  14797. instead.
  14798. Point is left between drawer's boundaries."
  14799. (interactive "P")
  14800. (let* ((drawer (if arg "PROPERTIES"
  14801. (or drawer (read-from-minibuffer "Drawer: ")))))
  14802. (cond
  14803. ;; With C-u, fall back on `org-insert-property-drawer'
  14804. (arg (org-insert-property-drawer))
  14805. ;; Check validity of suggested drawer's name.
  14806. ((not (org-string-match-p org-drawer-regexp (format ":%s:" drawer)))
  14807. (user-error "Invalid drawer name"))
  14808. ;; With an active region, insert a drawer at point.
  14809. ((not (org-region-active-p))
  14810. (progn
  14811. (unless (bolp) (insert "\n"))
  14812. (insert (format ":%s:\n\n:END:\n" drawer))
  14813. (forward-line -2)))
  14814. ;; Otherwise, insert the drawer at point
  14815. (t
  14816. (let ((rbeg (region-beginning))
  14817. (rend (copy-marker (region-end))))
  14818. (unwind-protect
  14819. (progn
  14820. (goto-char rbeg)
  14821. (beginning-of-line)
  14822. (when (save-excursion
  14823. (re-search-forward org-outline-regexp-bol rend t))
  14824. (user-error "Drawers cannot contain headlines"))
  14825. ;; Position point at the beginning of the first
  14826. ;; non-blank line in region. Insert drawer's opening
  14827. ;; there, then indent it.
  14828. (org-skip-whitespace)
  14829. (beginning-of-line)
  14830. (insert ":" drawer ":\n")
  14831. (forward-line -1)
  14832. (indent-for-tab-command)
  14833. ;; Move point to the beginning of the first blank line
  14834. ;; after the last non-blank line in region. Insert
  14835. ;; drawer's closing, then indent it.
  14836. (goto-char rend)
  14837. (skip-chars-backward " \r\t\n")
  14838. (insert "\n:END:")
  14839. (deactivate-mark t)
  14840. (indent-for-tab-command)
  14841. (unless (eolp) (insert "\n")))
  14842. ;; Clear marker, whatever the outcome of insertion is.
  14843. (set-marker rend nil)))))))
  14844. (defvar org-property-set-functions-alist nil
  14845. "Property set function alist.
  14846. Each entry should have the following format:
  14847. (PROPERTY . READ-FUNCTION)
  14848. The read function will be called with the same argument as
  14849. `org-completing-read'.")
  14850. (defun org-set-property-function (property)
  14851. "Get the function that should be used to set PROPERTY.
  14852. This is computed according to `org-property-set-functions-alist'."
  14853. (or (cdr (assoc property org-property-set-functions-alist))
  14854. 'org-completing-read))
  14855. (defun org-read-property-value (property)
  14856. "Read PROPERTY value from user."
  14857. (let* ((completion-ignore-case t)
  14858. (allowed (org-property-get-allowed-values nil property 'table))
  14859. (cur (org-entry-get nil property))
  14860. (prompt (concat property " value"
  14861. (if (and cur (string-match "\\S-" cur))
  14862. (concat " [" cur "]") "") ": "))
  14863. (set-function (org-set-property-function property))
  14864. (val (if allowed
  14865. (funcall set-function prompt allowed nil
  14866. (not (get-text-property 0 'org-unrestricted
  14867. (caar allowed))))
  14868. (funcall set-function prompt
  14869. (mapcar 'list (org-property-values property))
  14870. nil nil "" nil cur))))
  14871. (org-trim val)))
  14872. (defvar org-last-set-property nil)
  14873. (defvar org-last-set-property-value nil)
  14874. (defun org-read-property-name ()
  14875. "Read a property name."
  14876. (let ((completion-ignore-case t)
  14877. (default-prop (or (and (org-at-property-p)
  14878. (org-match-string-no-properties 2))
  14879. org-last-set-property)))
  14880. (org-completing-read
  14881. (concat "Property"
  14882. (if default-prop (concat " [" default-prop "]") "")
  14883. ": ")
  14884. (mapcar #'list (org-buffer-property-keys nil t t))
  14885. nil nil nil nil default-prop)))
  14886. (defun org-set-property-and-value (use-last)
  14887. "Allow to set [PROPERTY]: [value] direction from prompt.
  14888. When use-default, don't even ask, just use the last
  14889. \"[PROPERTY]: [value]\" string from the history."
  14890. (interactive "P")
  14891. (let* ((completion-ignore-case t)
  14892. (pv (or (and use-last org-last-set-property-value)
  14893. (org-completing-read
  14894. "Enter a \"[Property]: [value]\" pair: "
  14895. nil nil nil nil nil
  14896. org-last-set-property-value)))
  14897. prop val)
  14898. (when (string-match "^[ \t]*\\([^:]+\\):[ \t]*\\(.*\\)[ \t]*$" pv)
  14899. (setq prop (match-string 1 pv)
  14900. val (match-string 2 pv))
  14901. (org-set-property prop val))))
  14902. (defun org-set-property (property value)
  14903. "In the current entry, set PROPERTY to VALUE.
  14904. When called interactively, this will prompt for a property name, offering
  14905. completion on existing and default properties. And then it will prompt
  14906. for a value, offering completion either on allowed values (via an inherited
  14907. xxx_ALL property) or on existing values in other instances of this property
  14908. in the current file.
  14909. Throw an error when trying to set a property with an invalid name."
  14910. (interactive (list nil nil))
  14911. (let ((property (or property (org-read-property-name))))
  14912. ;; `org-entry-put' also makes the following check, but this one
  14913. ;; avoids polluting `org-last-set-property' and
  14914. ;; `org-last-set-property-value' needlessly.
  14915. (unless (org--valid-property-p property)
  14916. (user-error "Invalid property name: \"%s\"" property))
  14917. (let ((value (or value (org-read-property-value property)))
  14918. (fn (cdr (assoc-string property org-properties-postprocess-alist t))))
  14919. (setq org-last-set-property property)
  14920. (setq org-last-set-property-value (concat property ": " value))
  14921. ;; Possibly postprocess the inserted value:
  14922. (when fn (setq value (funcall fn value)))
  14923. (unless (equal (org-entry-get nil property) value)
  14924. (org-entry-put nil property value)))))
  14925. (defun org-find-property (property &optional value)
  14926. "Find first entry in buffer that sets PROPERTY.
  14927. When optional argument VALUE is non-nil, only consider an entry
  14928. if it contains PROPERTY set to this value. If PROPERTY should be
  14929. explicitly set to nil, use string \"nil\" for VALUE.
  14930. Return position where the entry begins, or nil if there is no
  14931. such entry. If narrowing is in effect, only search the visible
  14932. part of the buffer."
  14933. (save-excursion
  14934. (goto-char (point-min))
  14935. (let ((case-fold-search t)
  14936. (re (org-re-property property nil (not value) value)))
  14937. (catch 'exit
  14938. (while (re-search-forward re nil t)
  14939. (when (if value (org-at-property-p)
  14940. (org-entry-get (point) property nil t))
  14941. (throw 'exit (progn (org-back-to-heading t) (point)))))))))
  14942. (defun org-delete-property (property)
  14943. "In the current entry, delete PROPERTY."
  14944. (interactive
  14945. (let* ((completion-ignore-case t)
  14946. (cat (org-entry-get (point) "CATEGORY"))
  14947. (props0 (org-entry-properties nil 'standard))
  14948. (props (if cat props0
  14949. (delete `("CATEGORY" . ,(org-get-category)) props0)))
  14950. (prop (if (< 1 (length props))
  14951. (completing-read "Property: " props nil t)
  14952. (caar props))))
  14953. (list prop)))
  14954. (if (not property)
  14955. (message "No property to delete in this entry")
  14956. (org-entry-delete nil property)
  14957. (message "Property \"%s\" deleted" property)))
  14958. (defun org-delete-property-globally (property)
  14959. "Remove PROPERTY globally, from all entries.
  14960. This function ignores narrowing, if any."
  14961. (interactive
  14962. (let* ((completion-ignore-case t)
  14963. (prop (completing-read
  14964. "Globally remove property: "
  14965. (mapcar #'list (org-buffer-property-keys)))))
  14966. (list prop)))
  14967. (org-with-wide-buffer
  14968. (goto-char (point-min))
  14969. (let ((count 0)
  14970. (re (org-re-property (concat (regexp-quote property) "\\+?") t t)))
  14971. (while (re-search-forward re nil t)
  14972. (when (org-entry-delete (point) property) (cl-incf count)))
  14973. (message "Property \"%s\" removed from %d entries" property count))))
  14974. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  14975. (defun org-compute-property-at-point ()
  14976. "Compute the property at point.
  14977. This looks for an enclosing column format, extracts the operator and
  14978. then applies it to the property in the column format's scope."
  14979. (interactive)
  14980. (unless (org-at-property-p)
  14981. (user-error "Not at a property"))
  14982. (let ((prop (org-match-string-no-properties 2)))
  14983. (org-columns-get-format-and-top-level)
  14984. (unless (nth 3 (assoc-string prop org-columns-current-fmt-compiled t))
  14985. (user-error "No operator defined for property %s" prop))
  14986. (org-columns-compute prop)))
  14987. (defvar org-property-allowed-value-functions nil
  14988. "Hook for functions supplying allowed values for a specific property.
  14989. The functions must take a single argument, the name of the property, and
  14990. return a flat list of allowed values. If \":ETC\" is one of
  14991. the values, this means that these values are intended as defaults for
  14992. completion, but that other values should be allowed too.
  14993. The functions must return nil if they are not responsible for this
  14994. property.")
  14995. (defun org-property-get-allowed-values (pom property &optional table)
  14996. "Get allowed values for the property PROPERTY.
  14997. When TABLE is non-nil, return an alist that can directly be used for
  14998. completion."
  14999. (let (vals)
  15000. (cond
  15001. ((equal property "TODO")
  15002. (setq vals (org-with-point-at pom
  15003. (append org-todo-keywords-1 '("")))))
  15004. ((equal property "PRIORITY")
  15005. (let ((n org-lowest-priority))
  15006. (while (>= n org-highest-priority)
  15007. (push (char-to-string n) vals)
  15008. (setq n (1- n)))))
  15009. ((equal property "CATEGORY"))
  15010. ((member property org-special-properties))
  15011. ((setq vals (run-hook-with-args-until-success
  15012. 'org-property-allowed-value-functions property)))
  15013. (t
  15014. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  15015. (when (and vals (string-match "\\S-" vals))
  15016. (setq vals (car (read-from-string (concat "(" vals ")"))))
  15017. (setq vals (mapcar (lambda (x)
  15018. (cond ((stringp x) x)
  15019. ((numberp x) (number-to-string x))
  15020. ((symbolp x) (symbol-name x))
  15021. (t "???")))
  15022. vals)))))
  15023. (when (member ":ETC" vals)
  15024. (setq vals (remove ":ETC" vals))
  15025. (org-add-props (car vals) '(org-unrestricted t)))
  15026. (if table (mapcar 'list vals) vals)))
  15027. (defun org-property-previous-allowed-value (&optional _previous)
  15028. "Switch to the next allowed value for this property."
  15029. (interactive)
  15030. (org-property-next-allowed-value t))
  15031. (defun org-property-next-allowed-value (&optional previous)
  15032. "Switch to the next allowed value for this property."
  15033. (interactive)
  15034. (unless (org-at-property-p)
  15035. (user-error "Not at a property"))
  15036. (let* ((prop (car (save-match-data (org-split-string (match-string 1) ":"))))
  15037. (key (match-string 2))
  15038. (value (match-string 3))
  15039. (allowed (or (org-property-get-allowed-values (point) key)
  15040. (and (member value '("[ ]" "[-]" "[X]"))
  15041. '("[ ]" "[X]"))))
  15042. (heading (save-match-data (nth 4 (org-heading-components))))
  15043. nval)
  15044. (unless allowed
  15045. (user-error "Allowed values for this property have not been defined"))
  15046. (when previous (setq allowed (reverse allowed)))
  15047. (when (member value allowed)
  15048. (setq nval (car (cdr (member value allowed)))))
  15049. (setq nval (or nval (car allowed)))
  15050. (when (equal nval value)
  15051. (user-error "Only one allowed value for this property"))
  15052. (org-at-property-p)
  15053. (replace-match (concat " :" key ": " nval) t t)
  15054. (org-indent-line)
  15055. (beginning-of-line 1)
  15056. (skip-chars-forward " \t")
  15057. (when (equal prop org-effort-property)
  15058. (org-refresh-property
  15059. '((effort . identity)
  15060. (effort-minutes . org-duration-string-to-minutes))
  15061. nval)
  15062. (when (string= org-clock-current-task heading)
  15063. (setq org-clock-effort nval)
  15064. (org-clock-update-mode-line)))
  15065. (run-hook-with-args 'org-property-changed-functions key nval)))
  15066. (defun org-find-olp (path &optional this-buffer)
  15067. "Return a marker pointing to the entry at outline path OLP.
  15068. If anything goes wrong, throw an error.
  15069. You can wrap this call to catch the error like this:
  15070. \(condition-case msg
  15071. \(org-mobile-locate-entry (match-string 4))
  15072. \(error (nth 1 msg)))
  15073. The return value will then be either a string with the error message,
  15074. or a marker if everything is OK.
  15075. If THIS-BUFFER is set, the outline path does not contain a file,
  15076. only headings."
  15077. (let* ((file (if this-buffer buffer-file-name (pop path)))
  15078. (buffer (if this-buffer (current-buffer) (find-file-noselect file)))
  15079. (level 1)
  15080. (lmin 1)
  15081. (lmax 1)
  15082. end found flevel)
  15083. (unless buffer (error "File not found :%s" file))
  15084. (with-current-buffer buffer
  15085. (org-with-wide-buffer
  15086. (goto-char (point-min))
  15087. (dolist (heading path)
  15088. (let ((re (format org-complex-heading-regexp-format
  15089. (regexp-quote heading)))
  15090. (cnt 0))
  15091. (while (re-search-forward re end t)
  15092. (setq level (- (match-end 1) (match-beginning 1)))
  15093. (when (and (>= level lmin) (<= level lmax))
  15094. (setq found (match-beginning 0) flevel level cnt (1+ cnt))))
  15095. (when (= cnt 0)
  15096. (error "Heading not found on level %d: %s" lmax heading))
  15097. (when (> cnt 1)
  15098. (error "Heading not unique on level %d: %s" lmax heading))
  15099. (goto-char found)
  15100. (setq lmin (1+ flevel) lmax (+ lmin (if org-odd-levels-only 1 0)))
  15101. (setq end (save-excursion (org-end-of-subtree t t)))))
  15102. (when (org-at-heading-p)
  15103. (point-marker))))))
  15104. (defun org-find-exact-headline-in-buffer (heading &optional buffer pos-only)
  15105. "Find node HEADING in BUFFER.
  15106. Return a marker to the heading if it was found, or nil if not.
  15107. If POS-ONLY is set, return just the position instead of a marker.
  15108. The heading text must match exact, but it may have a TODO keyword,
  15109. a priority cookie and tags in the standard locations."
  15110. (with-current-buffer (or buffer (current-buffer))
  15111. (save-excursion
  15112. (save-restriction
  15113. (widen)
  15114. (goto-char (point-min))
  15115. (let (case-fold-search)
  15116. (when (re-search-forward
  15117. (format org-complex-heading-regexp-format
  15118. (regexp-quote heading)) nil t)
  15119. (if pos-only
  15120. (match-beginning 0)
  15121. (move-marker (make-marker) (match-beginning 0)))))))))
  15122. (defun org-find-exact-heading-in-directory (heading &optional dir)
  15123. "Find Org node headline HEADING in all .org files in directory DIR.
  15124. When the target headline is found, return a marker to this location."
  15125. (let ((files (directory-files (or dir default-directory)
  15126. t "\\`[^.#].*\\.org\\'"))
  15127. visiting m buffer)
  15128. (catch 'found
  15129. (dolist (file files)
  15130. (message "trying %s" file)
  15131. (setq visiting (org-find-base-buffer-visiting file))
  15132. (setq buffer (or visiting (find-file-noselect file)))
  15133. (setq m (org-find-exact-headline-in-buffer
  15134. heading buffer))
  15135. (when (and (not m) (not visiting)) (kill-buffer buffer))
  15136. (and m (throw 'found m))))))
  15137. (defun org-find-entry-with-id (ident)
  15138. "Locate the entry that contains the ID property with exact value IDENT.
  15139. IDENT can be a string, a symbol or a number, this function will search for
  15140. the string representation of it.
  15141. Return the position where this entry starts, or nil if there is no such entry."
  15142. (interactive "sID: ")
  15143. (let ((id (cond
  15144. ((stringp ident) ident)
  15145. ((symbolp ident) (symbol-name ident))
  15146. ((numberp ident) (number-to-string ident))
  15147. (t (error "IDENT %s must be a string, symbol or number" ident)))))
  15148. (org-with-wide-buffer (org-find-property "ID" id))))
  15149. ;;;; Timestamps
  15150. (defvar org-last-changed-timestamp nil)
  15151. (defvar org-last-inserted-timestamp nil
  15152. "The last time stamp inserted with `org-insert-time-stamp'.")
  15153. (defvar org-ts-what) ; dynamically scoped parameter
  15154. (defun org-time-stamp (arg &optional inactive)
  15155. "Prompt for a date/time and insert a time stamp.
  15156. If the user specifies a time like HH:MM or if this command is
  15157. called with at least one prefix argument, the time stamp contains
  15158. the date and the time. Otherwise, only the date is included.
  15159. All parts of a date not specified by the user are filled in from
  15160. the timestamp at point, if any, or the current date/time
  15161. otherwise.
  15162. If there is already a timestamp at the cursor, it is replaced.
  15163. With two universal prefix arguments, insert an active timestamp
  15164. with the current time without prompting the user.
  15165. When called from lisp, the timestamp is inactive if INACTIVE is
  15166. non-nil."
  15167. (interactive "P")
  15168. (let* ((ts (cond
  15169. ((org-at-date-range-p t)
  15170. (match-string (if (< (point) (- (match-beginning 2) 2)) 1 2)))
  15171. ((org-at-timestamp-p t) (match-string 0))))
  15172. ;; Default time is either the timestamp at point or today.
  15173. ;; When entering a range, only the range start is considered.
  15174. (default-time (if (not ts) (current-time)
  15175. (apply #'encode-time (org-parse-time-string ts))))
  15176. (default-input (and ts (org-get-compact-tod ts)))
  15177. (repeater (and ts
  15178. (string-match "\\([.+-]+[0-9]+[hdwmy] ?\\)+" ts)
  15179. (match-string 0 ts)))
  15180. org-time-was-given
  15181. org-end-time-was-given
  15182. (time
  15183. (and (if (equal arg '(16)) (current-time)
  15184. ;; Preserve `this-command' and `last-command'.
  15185. (let ((this-command this-command)
  15186. (last-command last-command))
  15187. (org-read-date
  15188. arg 'totime nil nil default-time default-input
  15189. inactive))))))
  15190. (cond
  15191. ((and ts
  15192. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  15193. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  15194. (insert "--")
  15195. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  15196. (ts
  15197. ;; Make sure we're on a timestamp. When in the middle of a date
  15198. ;; range, move arbitrarily to range end.
  15199. (unless (org-at-timestamp-p t)
  15200. (skip-chars-forward "-")
  15201. (org-at-timestamp-p t))
  15202. (replace-match "")
  15203. (setq org-last-changed-timestamp
  15204. (org-insert-time-stamp
  15205. time (or org-time-was-given arg)
  15206. inactive nil nil (list org-end-time-was-given)))
  15207. (when repeater
  15208. (backward-char)
  15209. (insert " " repeater)
  15210. (setq org-last-changed-timestamp
  15211. (concat (substring org-last-inserted-timestamp 0 -1)
  15212. " " repeater ">")))
  15213. (message "Timestamp updated"))
  15214. ((equal arg '(16)) (org-insert-time-stamp time t inactive))
  15215. (t (org-insert-time-stamp
  15216. time (or org-time-was-given arg) inactive nil nil
  15217. (list org-end-time-was-given))))))
  15218. ;; FIXME: can we use this for something else, like computing time differences?
  15219. (defun org-get-compact-tod (s)
  15220. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  15221. (let* ((t1 (match-string 1 s))
  15222. (h1 (string-to-number (match-string 2 s)))
  15223. (m1 (string-to-number (match-string 3 s)))
  15224. (t2 (and (match-end 4) (match-string 5 s)))
  15225. (h2 (and t2 (string-to-number (match-string 6 s))))
  15226. (m2 (and t2 (string-to-number (match-string 7 s))))
  15227. dh dm)
  15228. (if (not t2)
  15229. t1
  15230. (setq dh (- h2 h1) dm (- m2 m1))
  15231. (when (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  15232. (concat t1 "+" (number-to-string dh)
  15233. (and (/= 0 dm) (format ":%02d" dm)))))))
  15234. (defun org-time-stamp-inactive (&optional arg)
  15235. "Insert an inactive time stamp.
  15236. An inactive time stamp is enclosed in square brackets instead of angle
  15237. brackets. It is inactive in the sense that it does not trigger agenda entries,
  15238. does not link to the calendar and cannot be changed with the S-cursor keys.
  15239. So these are more for recording a certain time/date."
  15240. (interactive "P")
  15241. (org-time-stamp arg 'inactive))
  15242. (defvar org-date-ovl (make-overlay 1 1))
  15243. (overlay-put org-date-ovl 'face 'org-date-selected)
  15244. (org-detach-overlay org-date-ovl)
  15245. (defvar org-ans1) ; dynamically scoped parameter
  15246. (defvar org-ans2) ; dynamically scoped parameter
  15247. (defvar org-plain-time-of-day-regexp) ; defined below
  15248. (defvar org-overriding-default-time nil) ; dynamically scoped
  15249. (defvar org-read-date-overlay nil)
  15250. (defvar org-dcst nil) ; dynamically scoped
  15251. (defvar org-read-date-history nil)
  15252. (defvar org-read-date-final-answer nil)
  15253. (defvar org-read-date-analyze-futurep nil)
  15254. (defvar org-read-date-analyze-forced-year nil)
  15255. (defvar org-read-date-inactive)
  15256. (defvar org-read-date-minibuffer-local-map
  15257. (let* ((map (make-sparse-keymap)))
  15258. (set-keymap-parent map minibuffer-local-map)
  15259. (org-defkey map (kbd ".")
  15260. (lambda () (interactive)
  15261. ;; Are we at the beginning of the prompt?
  15262. (if (org-looking-back "^[^:]+: "
  15263. (let ((inhibit-field-text-motion t))
  15264. (line-beginning-position)))
  15265. (org-eval-in-calendar '(calendar-goto-today))
  15266. (insert "."))))
  15267. (org-defkey map (kbd "C-.")
  15268. (lambda () (interactive)
  15269. (org-eval-in-calendar '(calendar-goto-today))))
  15270. (org-defkey map [(meta shift left)]
  15271. (lambda () (interactive)
  15272. (org-eval-in-calendar '(calendar-backward-month 1))))
  15273. (org-defkey map [(meta shift right)]
  15274. (lambda () (interactive)
  15275. (org-eval-in-calendar '(calendar-forward-month 1))))
  15276. (org-defkey map [(meta shift up)]
  15277. (lambda () (interactive)
  15278. (org-eval-in-calendar '(calendar-backward-year 1))))
  15279. (org-defkey map [(meta shift down)]
  15280. (lambda () (interactive)
  15281. (org-eval-in-calendar '(calendar-forward-year 1))))
  15282. (org-defkey map [?\e (shift left)]
  15283. (lambda () (interactive)
  15284. (org-eval-in-calendar '(calendar-backward-month 1))))
  15285. (org-defkey map [?\e (shift right)]
  15286. (lambda () (interactive)
  15287. (org-eval-in-calendar '(calendar-forward-month 1))))
  15288. (org-defkey map [?\e (shift up)]
  15289. (lambda () (interactive)
  15290. (org-eval-in-calendar '(calendar-backward-year 1))))
  15291. (org-defkey map [?\e (shift down)]
  15292. (lambda () (interactive)
  15293. (org-eval-in-calendar '(calendar-forward-year 1))))
  15294. (org-defkey map [(shift up)]
  15295. (lambda () (interactive)
  15296. (org-eval-in-calendar '(calendar-backward-week 1))))
  15297. (org-defkey map [(shift down)]
  15298. (lambda () (interactive)
  15299. (org-eval-in-calendar '(calendar-forward-week 1))))
  15300. (org-defkey map [(shift left)]
  15301. (lambda () (interactive)
  15302. (org-eval-in-calendar '(calendar-backward-day 1))))
  15303. (org-defkey map [(shift right)]
  15304. (lambda () (interactive)
  15305. (org-eval-in-calendar '(calendar-forward-day 1))))
  15306. (org-defkey map "!"
  15307. (lambda () (interactive)
  15308. (org-eval-in-calendar '(diary-view-entries))
  15309. (message "")))
  15310. (org-defkey map ">"
  15311. (lambda () (interactive)
  15312. (org-eval-in-calendar '(calendar-scroll-left 1))))
  15313. (org-defkey map "<"
  15314. (lambda () (interactive)
  15315. (org-eval-in-calendar '(calendar-scroll-right 1))))
  15316. (org-defkey map "\C-v"
  15317. (lambda () (interactive)
  15318. (org-eval-in-calendar
  15319. '(calendar-scroll-left-three-months 1))))
  15320. (org-defkey map "\M-v"
  15321. (lambda () (interactive)
  15322. (org-eval-in-calendar
  15323. '(calendar-scroll-right-three-months 1))))
  15324. map)
  15325. "Keymap for minibuffer commands when using `org-read-date'.")
  15326. (defvar org-def)
  15327. (defvar org-defdecode)
  15328. (defvar org-with-time)
  15329. (defvar calendar-setup) ; Dynamically scoped.
  15330. (defun org-read-date (&optional with-time to-time from-string prompt
  15331. default-time default-input inactive)
  15332. "Read a date, possibly a time, and make things smooth for the user.
  15333. The prompt will suggest to enter an ISO date, but you can also enter anything
  15334. which will at least partially be understood by `parse-time-string'.
  15335. Unrecognized parts of the date will default to the current day, month, year,
  15336. hour and minute. If this command is called to replace a timestamp at point,
  15337. or to enter the second timestamp of a range, the default time is taken
  15338. from the existing stamp. Furthermore, the command prefers the future,
  15339. so if you are giving a date where the year is not given, and the day-month
  15340. combination is already past in the current year, it will assume you
  15341. mean next year. For details, see the manual. A few examples:
  15342. 3-2-5 --> 2003-02-05
  15343. feb 15 --> currentyear-02-15
  15344. 2/15 --> currentyear-02-15
  15345. sep 12 9 --> 2009-09-12
  15346. 12:45 --> today 12:45
  15347. 22 sept 0:34 --> currentyear-09-22 0:34
  15348. 12 --> currentyear-currentmonth-12
  15349. Fri --> nearest Friday after today
  15350. -Tue --> last Tuesday
  15351. etc.
  15352. Furthermore you can specify a relative date by giving, as the *first* thing
  15353. in the input: a plus/minus sign, a number and a letter [hdwmy] to indicate
  15354. change in days weeks, months, years.
  15355. With a single plus or minus, the date is relative to today. With a double
  15356. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  15357. +4d --> four days from today
  15358. +4 --> same as above
  15359. +2w --> two weeks from today
  15360. ++5 --> five days from default date
  15361. The function understands only English month and weekday abbreviations.
  15362. While prompting, a calendar is popped up - you can also select the
  15363. date with the mouse (button 1). The calendar shows a period of three
  15364. months. To scroll it to other months, use the keys `>' and `<'.
  15365. If you don't like the calendar, turn it off with
  15366. (setq org-read-date-popup-calendar nil)
  15367. With optional argument TO-TIME, the date will immediately be converted
  15368. to an internal time.
  15369. With an optional argument WITH-TIME, the prompt will suggest to
  15370. also insert a time. Note that when WITH-TIME is not set, you can
  15371. still enter a time, and this function will inform the calling routine
  15372. about this change. The calling routine may then choose to change the
  15373. format used to insert the time stamp into the buffer to include the time.
  15374. With optional argument FROM-STRING, read from this string instead from
  15375. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  15376. the time/date that is used for everything that is not specified by the
  15377. user."
  15378. (require 'parse-time)
  15379. (let* ((org-with-time with-time)
  15380. (org-time-stamp-rounding-minutes
  15381. (if (equal org-with-time '(16))
  15382. '(0 0)
  15383. org-time-stamp-rounding-minutes))
  15384. (org-dcst org-display-custom-times)
  15385. (ct (org-current-time))
  15386. (org-def (or org-overriding-default-time default-time ct))
  15387. (org-defdecode (decode-time org-def))
  15388. (cur-frame (selected-frame))
  15389. (mouse-autoselect-window nil) ; Don't let the mouse jump
  15390. (calendar-setup
  15391. (and (eq calendar-setup 'calendar-only) 'calendar-only))
  15392. (calendar-move-hook nil)
  15393. (calendar-view-diary-initially-flag nil)
  15394. (calendar-view-holidays-initially-flag nil)
  15395. ans (org-ans0 "") org-ans1 org-ans2 final cal-frame)
  15396. ;; Rationalize `org-def' and `org-defdecode', if required.
  15397. (when (< (nth 2 org-defdecode) org-extend-today-until)
  15398. (setf (nth 2 org-defdecode) -1)
  15399. (setf (nth 1 org-defdecode) 59)
  15400. (setq org-def (apply #'encode-time org-defdecode))
  15401. (setq org-defdecode (decode-time org-def)))
  15402. (let* ((timestr (format-time-string
  15403. (if org-with-time "%Y-%m-%d %H:%M" "%Y-%m-%d")
  15404. org-def))
  15405. (prompt (concat (if prompt (concat prompt " ") "")
  15406. (format "Date+time [%s]: " timestr))))
  15407. (cond
  15408. (from-string (setq ans from-string))
  15409. (org-read-date-popup-calendar
  15410. (save-excursion
  15411. (save-window-excursion
  15412. (calendar)
  15413. (when (eq calendar-setup 'calendar-only)
  15414. (setq cal-frame
  15415. (window-frame (get-buffer-window "*Calendar*" 'visible)))
  15416. (select-frame cal-frame))
  15417. (org-eval-in-calendar '(setq cursor-type nil) t)
  15418. (unwind-protect
  15419. (progn
  15420. (calendar-forward-day (- (time-to-days org-def)
  15421. (calendar-absolute-from-gregorian
  15422. (calendar-current-date))))
  15423. (org-eval-in-calendar nil t)
  15424. (let* ((old-map (current-local-map))
  15425. (map (copy-keymap calendar-mode-map))
  15426. (minibuffer-local-map
  15427. (copy-keymap org-read-date-minibuffer-local-map)))
  15428. (org-defkey map (kbd "RET") 'org-calendar-select)
  15429. (org-defkey map [mouse-1] 'org-calendar-select-mouse)
  15430. (org-defkey map [mouse-2] 'org-calendar-select-mouse)
  15431. (unwind-protect
  15432. (progn
  15433. (use-local-map map)
  15434. (setq org-read-date-inactive inactive)
  15435. (add-hook 'post-command-hook 'org-read-date-display)
  15436. (setq org-ans0
  15437. (read-string prompt
  15438. default-input
  15439. 'org-read-date-history
  15440. nil))
  15441. ;; org-ans0: from prompt
  15442. ;; org-ans1: from mouse click
  15443. ;; org-ans2: from calendar motion
  15444. (setq ans
  15445. (concat org-ans0 " " (or org-ans1 org-ans2))))
  15446. (remove-hook 'post-command-hook 'org-read-date-display)
  15447. (use-local-map old-map)
  15448. (when org-read-date-overlay
  15449. (delete-overlay org-read-date-overlay)
  15450. (setq org-read-date-overlay nil)))))
  15451. (bury-buffer "*Calendar*")
  15452. (when cal-frame
  15453. (delete-frame cal-frame)
  15454. (select-frame-set-input-focus cur-frame))))))
  15455. (t ; Naked prompt only
  15456. (unwind-protect
  15457. (setq ans (read-string prompt default-input
  15458. 'org-read-date-history timestr))
  15459. (when org-read-date-overlay
  15460. (delete-overlay org-read-date-overlay)
  15461. (setq org-read-date-overlay nil))))))
  15462. (setq final (org-read-date-analyze ans org-def org-defdecode))
  15463. (when org-read-date-analyze-forced-year
  15464. (message "Year was forced into %s"
  15465. (if org-read-date-force-compatible-dates
  15466. "compatible range (1970-2037)"
  15467. "range representable on this machine"))
  15468. (ding))
  15469. ;; One round trip to get rid of 34th of August and stuff like that....
  15470. (setq final (decode-time (apply 'encode-time final)))
  15471. (setq org-read-date-final-answer ans)
  15472. (if to-time
  15473. (apply 'encode-time final)
  15474. (if (and (boundp 'org-time-was-given) org-time-was-given)
  15475. (format "%04d-%02d-%02d %02d:%02d"
  15476. (nth 5 final) (nth 4 final) (nth 3 final)
  15477. (nth 2 final) (nth 1 final))
  15478. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  15479. (defun org-read-date-display ()
  15480. "Display the current date prompt interpretation in the minibuffer."
  15481. (when org-read-date-display-live
  15482. (when org-read-date-overlay
  15483. (delete-overlay org-read-date-overlay))
  15484. (when (minibufferp (current-buffer))
  15485. (save-excursion
  15486. (end-of-line 1)
  15487. (while (not (equal (buffer-substring
  15488. (max (point-min) (- (point) 4)) (point))
  15489. " "))
  15490. (insert " ")))
  15491. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  15492. " " (or org-ans1 org-ans2)))
  15493. (org-end-time-was-given nil)
  15494. (f (org-read-date-analyze ans org-def org-defdecode))
  15495. (fmts (if org-dcst
  15496. org-time-stamp-custom-formats
  15497. org-time-stamp-formats))
  15498. (fmt (if (or org-with-time
  15499. (and (boundp 'org-time-was-given) org-time-was-given))
  15500. (cdr fmts)
  15501. (car fmts)))
  15502. (txt (format-time-string fmt (apply 'encode-time f)))
  15503. (txt (if org-read-date-inactive (concat "[" (substring txt 1 -1) "]") txt))
  15504. (txt (concat "=> " txt)))
  15505. (when (and org-end-time-was-given
  15506. (string-match org-plain-time-of-day-regexp txt))
  15507. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  15508. org-end-time-was-given
  15509. (substring txt (match-end 0)))))
  15510. (when org-read-date-analyze-futurep
  15511. (setq txt (concat txt " (=>F)")))
  15512. (setq org-read-date-overlay
  15513. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  15514. (org-overlay-display org-read-date-overlay txt 'secondary-selection)))))
  15515. (defun org-read-date-analyze (ans def defdecode)
  15516. "Analyze the combined answer of the date prompt."
  15517. ;; FIXME: cleanup and comment
  15518. ;; Pass `current-time' result to `decode-time' (instead of calling
  15519. ;; without arguments) so that only `current-time' has to be
  15520. ;; overriden in tests.
  15521. (let ((org-def def)
  15522. (org-defdecode defdecode)
  15523. (nowdecode (decode-time (current-time)))
  15524. delta deltan deltaw deltadef year month day
  15525. hour minute second wday pm h2 m2 tl wday1
  15526. iso-year iso-weekday iso-week iso-date futurep kill-year)
  15527. (setq org-read-date-analyze-futurep nil
  15528. org-read-date-analyze-forced-year nil)
  15529. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  15530. (setq ans "+0"))
  15531. (when (setq delta (org-read-date-get-relative ans (current-time) org-def))
  15532. (setq ans (replace-match "" t t ans)
  15533. deltan (car delta)
  15534. deltaw (nth 1 delta)
  15535. deltadef (nth 2 delta)))
  15536. ;; Check if there is an iso week date in there. If yes, store the
  15537. ;; info and postpone interpreting it until the rest of the parsing
  15538. ;; is done.
  15539. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  15540. (setq iso-year (when (match-end 1)
  15541. (org-small-year-to-year
  15542. (string-to-number (match-string 1 ans))))
  15543. iso-weekday (when (match-end 3)
  15544. (string-to-number (match-string 3 ans)))
  15545. iso-week (string-to-number (match-string 2 ans)))
  15546. (setq ans (replace-match "" t t ans)))
  15547. ;; Help matching ISO dates with single digit month or day, like 2006-8-11.
  15548. (when (string-match
  15549. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  15550. (setq year (if (match-end 2)
  15551. (string-to-number (match-string 2 ans))
  15552. (progn (setq kill-year t)
  15553. (string-to-number (format-time-string "%Y"))))
  15554. month (string-to-number (match-string 3 ans))
  15555. day (string-to-number (match-string 4 ans)))
  15556. (setq year (org-small-year-to-year year))
  15557. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  15558. t nil ans)))
  15559. ;; Help matching dotted european dates
  15560. (when (string-match
  15561. "^ *\\(3[01]\\|0?[1-9]\\|[12][0-9]\\)\\. ?\\(0?[1-9]\\|1[012]\\)\\.\\( ?[1-9][0-9]\\{3\\}\\)?" ans)
  15562. (setq year (if (match-end 3) (string-to-number (match-string 3 ans))
  15563. (setq kill-year t)
  15564. (string-to-number (format-time-string "%Y")))
  15565. day (string-to-number (match-string 1 ans))
  15566. month (string-to-number (match-string 2 ans))
  15567. ans (replace-match (format "%04d-%02d-%02d" year month day)
  15568. t nil ans)))
  15569. ;; Help matching american dates, like 5/30 or 5/30/7
  15570. (when (string-match
  15571. "^ *\\(0?[1-9]\\|1[012]\\)/\\(0?[1-9]\\|[12][0-9]\\|3[01]\\)\\(/\\([0-9]+\\)\\)?\\([^/0-9]\\|$\\)" ans)
  15572. (setq year (if (match-end 4)
  15573. (string-to-number (match-string 4 ans))
  15574. (progn (setq kill-year t)
  15575. (string-to-number (format-time-string "%Y"))))
  15576. month (string-to-number (match-string 1 ans))
  15577. day (string-to-number (match-string 2 ans)))
  15578. (setq year (org-small-year-to-year year))
  15579. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  15580. t nil ans)))
  15581. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  15582. ;; If there is a time with am/pm, and *no* time without it, we convert
  15583. ;; so that matching will be successful.
  15584. (cl-loop for i from 1 to 2 do ; twice, for end time as well
  15585. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  15586. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  15587. (setq hour (string-to-number (match-string 1 ans))
  15588. minute (if (match-end 3)
  15589. (string-to-number (match-string 3 ans))
  15590. 0)
  15591. pm (equal ?p
  15592. (string-to-char (downcase (match-string 4 ans)))))
  15593. (if (and (= hour 12) (not pm))
  15594. (setq hour 0)
  15595. (when (and pm (< hour 12)) (setq hour (+ 12 hour))))
  15596. (setq ans (replace-match (format "%02d:%02d" hour minute)
  15597. t t ans))))
  15598. ;; Check if a time range is given as a duration
  15599. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  15600. (setq hour (string-to-number (match-string 1 ans))
  15601. h2 (+ hour (string-to-number (match-string 3 ans)))
  15602. minute (string-to-number (match-string 2 ans))
  15603. m2 (+ minute (if (match-end 5) (string-to-number
  15604. (match-string 5 ans))0)))
  15605. (when (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  15606. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  15607. t t ans)))
  15608. ;; Check if there is a time range
  15609. (when (boundp 'org-end-time-was-given)
  15610. (setq org-time-was-given nil)
  15611. (when (and (string-match org-plain-time-of-day-regexp ans)
  15612. (match-end 8))
  15613. (setq org-end-time-was-given (match-string 8 ans))
  15614. (setq ans (concat (substring ans 0 (match-beginning 7))
  15615. (substring ans (match-end 7))))))
  15616. (setq tl (parse-time-string ans)
  15617. day (or (nth 3 tl) (nth 3 org-defdecode))
  15618. month
  15619. (cond ((nth 4 tl))
  15620. ((not org-read-date-prefer-future) (nth 4 org-defdecode))
  15621. ;; Day was specified. Make sure DAY+MONTH
  15622. ;; combination happens in the future.
  15623. ((nth 3 tl)
  15624. (setq futurep t)
  15625. (if (< day (nth 3 nowdecode)) (1+ (nth 4 nowdecode))
  15626. (nth 4 nowdecode)))
  15627. (t (nth 4 org-defdecode)))
  15628. year
  15629. (cond ((and (not kill-year) (nth 5 tl)))
  15630. ((not org-read-date-prefer-future) (nth 5 org-defdecode))
  15631. ;; Month was guessed in the future and is at least
  15632. ;; equal to NOWDECODE's. Fix year accordingly.
  15633. (futurep
  15634. (if (or (> month (nth 4 nowdecode))
  15635. (>= day (nth 3 nowdecode)))
  15636. (nth 5 nowdecode)
  15637. (1+ (nth 5 nowdecode))))
  15638. ;; Month was specified. Make sure MONTH+YEAR
  15639. ;; combination happens in the future.
  15640. ((nth 4 tl)
  15641. (setq futurep t)
  15642. (cond ((> month (nth 4 nowdecode)) (nth 5 nowdecode))
  15643. ((< month (nth 4 nowdecode)) (1+ (nth 5 nowdecode)))
  15644. ((< day (nth 3 nowdecode)) (1+ (nth 5 nowdecode)))
  15645. (t (nth 5 nowdecode))))
  15646. (t (nth 5 org-defdecode)))
  15647. hour (or (nth 2 tl) (nth 2 org-defdecode))
  15648. minute (or (nth 1 tl) (nth 1 org-defdecode))
  15649. second (or (nth 0 tl) 0)
  15650. wday (nth 6 tl))
  15651. (when (and (eq org-read-date-prefer-future 'time)
  15652. (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
  15653. (equal day (nth 3 nowdecode))
  15654. (equal month (nth 4 nowdecode))
  15655. (equal year (nth 5 nowdecode))
  15656. (nth 2 tl)
  15657. (or (< (nth 2 tl) (nth 2 nowdecode))
  15658. (and (= (nth 2 tl) (nth 2 nowdecode))
  15659. (nth 1 tl)
  15660. (< (nth 1 tl) (nth 1 nowdecode)))))
  15661. (setq day (1+ day)
  15662. futurep t))
  15663. ;; Special date definitions below
  15664. (cond
  15665. (iso-week
  15666. ;; There was an iso week
  15667. (require 'cal-iso)
  15668. (setq futurep nil)
  15669. (setq year (or iso-year year)
  15670. day (or iso-weekday wday 1)
  15671. wday nil ; to make sure that the trigger below does not match
  15672. iso-date (calendar-gregorian-from-absolute
  15673. (calendar-iso-to-absolute
  15674. (list iso-week day year))))
  15675. ; FIXME: Should we also push ISO weeks into the future?
  15676. ; (when (and org-read-date-prefer-future
  15677. ; (not iso-year)
  15678. ; (< (calendar-absolute-from-gregorian iso-date)
  15679. ; (time-to-days (current-time))))
  15680. ; (setq year (1+ year)
  15681. ; iso-date (calendar-gregorian-from-absolute
  15682. ; (calendar-iso-to-absolute
  15683. ; (list iso-week day year)))))
  15684. (setq month (car iso-date)
  15685. year (nth 2 iso-date)
  15686. day (nth 1 iso-date)))
  15687. (deltan
  15688. (setq futurep nil)
  15689. (unless deltadef
  15690. ;; Pass `current-time' result to `decode-time' (instead of
  15691. ;; calling without arguments) so that only `current-time' has
  15692. ;; to be overriden in tests.
  15693. (let ((now (decode-time (current-time))))
  15694. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  15695. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  15696. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  15697. ((equal deltaw "m") (setq month (+ month deltan)))
  15698. ((equal deltaw "y") (setq year (+ year deltan)))))
  15699. ((and wday (not (nth 3 tl)))
  15700. ;; Weekday was given, but no day, so pick that day in the week
  15701. ;; on or after the derived date.
  15702. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  15703. (unless (equal wday wday1)
  15704. (setq day (+ day (% (- wday wday1 -7) 7))))))
  15705. (when (and (boundp 'org-time-was-given)
  15706. (nth 2 tl))
  15707. (setq org-time-was-given t))
  15708. (when (< year 100) (setq year (+ 2000 year)))
  15709. ;; Check of the date is representable
  15710. (if org-read-date-force-compatible-dates
  15711. (progn
  15712. (when (< year 1970)
  15713. (setq year 1970 org-read-date-analyze-forced-year t))
  15714. (when (> year 2037)
  15715. (setq year 2037 org-read-date-analyze-forced-year t)))
  15716. (condition-case nil
  15717. (ignore (encode-time second minute hour day month year))
  15718. (error
  15719. (setq year (nth 5 org-defdecode))
  15720. (setq org-read-date-analyze-forced-year t))))
  15721. (setq org-read-date-analyze-futurep futurep)
  15722. (list second minute hour day month year)))
  15723. (defvar parse-time-weekdays)
  15724. (defun org-read-date-get-relative (s today default)
  15725. "Check string S for special relative date string.
  15726. TODAY and DEFAULT are internal times, for today and for a default.
  15727. Return shift list (N what def-flag)
  15728. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  15729. N is the number of WHATs to shift.
  15730. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  15731. the DEFAULT date rather than TODAY."
  15732. (require 'parse-time)
  15733. (when (and
  15734. (string-match
  15735. (concat
  15736. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  15737. "\\([0-9]+\\)?"
  15738. "\\([hdwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  15739. "\\([ \t]\\|$\\)") s)
  15740. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  15741. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  15742. (string-to-char (substring (match-string 1 s) -1))
  15743. ?+))
  15744. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  15745. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  15746. (what (if (match-end 3) (match-string 3 s) "d"))
  15747. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  15748. (date (if rel default today))
  15749. (wday (nth 6 (decode-time date)))
  15750. delta)
  15751. (if wday1
  15752. (progn
  15753. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  15754. (when (= delta 0) (setq delta 7))
  15755. (when (= dir ?-)
  15756. (setq delta (- delta 7))
  15757. (when (= delta 0) (setq delta -7)))
  15758. (when (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  15759. (list delta "d" rel))
  15760. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  15761. (defun org-order-calendar-date-args (arg1 arg2 arg3)
  15762. "Turn a user-specified date into the internal representation.
  15763. The internal representation needed by the calendar is (month day year).
  15764. This is a wrapper to handle the brain-dead convention in calendar that
  15765. user function argument order change dependent on argument order."
  15766. (if (boundp 'calendar-date-style)
  15767. (cond
  15768. ((eq calendar-date-style 'american)
  15769. (list arg1 arg2 arg3))
  15770. ((eq calendar-date-style 'european)
  15771. (list arg2 arg1 arg3))
  15772. ((eq calendar-date-style 'iso)
  15773. (list arg2 arg3 arg1)))
  15774. (org-no-warnings ;; european-calendar-style is obsolete as of version 23.1
  15775. (if (org-bound-and-true-p european-calendar-style)
  15776. (list arg2 arg1 arg3)
  15777. (list arg1 arg2 arg3)))))
  15778. (defun org-eval-in-calendar (form &optional keepdate)
  15779. "Eval FORM in the calendar window and return to current window.
  15780. Unless KEEPDATE is non-nil, update `org-ans2' to the cursor date."
  15781. (let ((sf (selected-frame))
  15782. (sw (selected-window)))
  15783. (select-window (get-buffer-window "*Calendar*" t))
  15784. (eval form)
  15785. (when (and (not keepdate) (calendar-cursor-to-date))
  15786. (let* ((date (calendar-cursor-to-date))
  15787. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15788. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  15789. (move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  15790. (select-window sw)
  15791. (org-select-frame-set-input-focus sf)))
  15792. (defun org-calendar-select ()
  15793. "Return to `org-read-date' with the date currently selected.
  15794. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  15795. (interactive)
  15796. (when (calendar-cursor-to-date)
  15797. (let* ((date (calendar-cursor-to-date))
  15798. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15799. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  15800. (when (active-minibuffer-window) (exit-minibuffer))))
  15801. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  15802. "Insert a date stamp for the date given by the internal TIME.
  15803. See `format-time-string' for the format of TIME.
  15804. WITH-HM means use the stamp format that includes the time of the day.
  15805. INACTIVE means use square brackets instead of angular ones, so that the
  15806. stamp will not contribute to the agenda.
  15807. PRE and POST are optional strings to be inserted before and after the
  15808. stamp.
  15809. The command returns the inserted time stamp."
  15810. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  15811. stamp)
  15812. (when inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  15813. (insert-before-markers (or pre ""))
  15814. (when (listp extra)
  15815. (setq extra (car extra))
  15816. (if (and (stringp extra)
  15817. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  15818. (setq extra (format "-%02d:%02d"
  15819. (string-to-number (match-string 1 extra))
  15820. (string-to-number (match-string 2 extra))))
  15821. (setq extra nil)))
  15822. (when extra
  15823. (setq fmt (concat (substring fmt 0 -1) extra (substring fmt -1))))
  15824. (insert-before-markers (setq stamp (format-time-string fmt time)))
  15825. (insert-before-markers (or post ""))
  15826. (setq org-last-inserted-timestamp stamp)))
  15827. (defun org-toggle-time-stamp-overlays ()
  15828. "Toggle the use of custom time stamp formats."
  15829. (interactive)
  15830. (setq org-display-custom-times (not org-display-custom-times))
  15831. (unless org-display-custom-times
  15832. (let ((p (point-min)) (bmp (buffer-modified-p)))
  15833. (while (setq p (next-single-property-change p 'display))
  15834. (when (and (get-text-property p 'display)
  15835. (eq (get-text-property p 'face) 'org-date))
  15836. (remove-text-properties
  15837. p (setq p (next-single-property-change p 'display))
  15838. '(display t))))
  15839. (set-buffer-modified-p bmp)))
  15840. (when (featurep 'xemacs)
  15841. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  15842. (org-restart-font-lock)
  15843. (setq org-table-may-need-update t)
  15844. (if org-display-custom-times
  15845. (message "Time stamps are overlaid with custom format")
  15846. (message "Time stamp overlays removed")))
  15847. (defun org-display-custom-time (beg end)
  15848. "Overlay modified time stamp format over timestamp between BEG and END."
  15849. (let* ((ts (buffer-substring beg end))
  15850. t1 w1 with-hm tf time str w2 (off 0))
  15851. (save-match-data
  15852. (setq t1 (org-parse-time-string ts t))
  15853. (when (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)?\\'" ts)
  15854. (setq off (- (match-end 0) (match-beginning 0)))))
  15855. (setq end (- end off))
  15856. (setq w1 (- end beg)
  15857. with-hm (and (nth 1 t1) (nth 2 t1))
  15858. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  15859. time (org-fix-decoded-time t1)
  15860. str (org-add-props
  15861. (format-time-string
  15862. (substring tf 1 -1) (apply 'encode-time time))
  15863. nil 'mouse-face 'highlight)
  15864. w2 (length str))
  15865. (unless (= w2 w1)
  15866. (add-text-properties (1+ beg) (+ 2 beg)
  15867. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  15868. (if (featurep 'xemacs)
  15869. (progn
  15870. (put-text-property beg end 'invisible t)
  15871. (put-text-property beg end 'end-glyph (make-glyph str)))
  15872. (put-text-property beg end 'display str))))
  15873. (defun org-fix-decoded-time (time)
  15874. "Set 0 instead of nil for the first 6 elements of time.
  15875. Don't touch the rest."
  15876. (let ((n 0))
  15877. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  15878. (define-obsolete-function-alias 'org-days-to-time 'org-time-stamp-to-now "24.4")
  15879. (defun org-time-stamp-to-now (timestamp-string &optional seconds)
  15880. "Difference between TIMESTAMP-STRING and now in days.
  15881. If SECONDS is non-nil, return the difference in seconds."
  15882. (let ((fdiff (if seconds 'org-float-time 'time-to-days)))
  15883. (- (funcall fdiff (org-time-string-to-time timestamp-string))
  15884. (funcall fdiff (current-time)))))
  15885. (defun org-deadline-close (timestamp-string &optional ndays)
  15886. "Is the time in TIMESTAMP-STRING close to the current date?"
  15887. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  15888. (and (< (org-time-stamp-to-now timestamp-string) ndays)
  15889. (not (org-entry-is-done-p))))
  15890. (defun org-get-wdays (ts &optional delay zero-delay)
  15891. "Get the deadline lead time appropriate for timestring TS.
  15892. When DELAY is non-nil, get the delay time for scheduled items
  15893. instead of the deadline lead time. When ZERO-DELAY is non-nil
  15894. and `org-scheduled-delay-days' is 0, enforce 0 as the delay,
  15895. don't try to find the delay cookie in the scheduled timestamp."
  15896. (let ((tv (if delay org-scheduled-delay-days
  15897. org-deadline-warning-days)))
  15898. (cond
  15899. ((or (and delay (< tv 0))
  15900. (and delay zero-delay (<= tv 0))
  15901. (and (not delay) (<= tv 0)))
  15902. ;; Enforce this value no matter what
  15903. (- tv))
  15904. ((string-match "-\\([0-9]+\\)\\([hdwmy]\\)\\(\\'\\|>\\| \\)" ts)
  15905. ;; lead time is specified.
  15906. (floor (* (string-to-number (match-string 1 ts))
  15907. (cdr (assoc (match-string 2 ts)
  15908. '(("d" . 1) ("w" . 7)
  15909. ("m" . 30.4) ("y" . 365.25)
  15910. ("h" . 0.041667)))))))
  15911. ;; go for the default.
  15912. (t tv))))
  15913. (defun org-calendar-select-mouse (ev)
  15914. "Return to `org-read-date' with the date currently selected.
  15915. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  15916. (interactive "e")
  15917. (mouse-set-point ev)
  15918. (when (calendar-cursor-to-date)
  15919. (let* ((date (calendar-cursor-to-date))
  15920. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15921. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  15922. (when (active-minibuffer-window) (exit-minibuffer))))
  15923. (defun org-check-deadlines (ndays)
  15924. "Check if there are any deadlines due or past due.
  15925. A deadline is considered due if it happens within `org-deadline-warning-days'
  15926. days from today's date. If the deadline appears in an entry marked DONE,
  15927. it is not shown. The prefix arg NDAYS can be used to test that many
  15928. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  15929. (interactive "P")
  15930. (let* ((org-warn-days
  15931. (cond
  15932. ((equal ndays '(4)) 100000)
  15933. (ndays (prefix-numeric-value ndays))
  15934. (t (abs org-deadline-warning-days))))
  15935. (case-fold-search nil)
  15936. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  15937. (callback
  15938. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  15939. (message "%d deadlines past-due or due within %d days"
  15940. (org-occur regexp nil callback)
  15941. org-warn-days)))
  15942. (defsubst org-re-timestamp (type)
  15943. "Return a regexp for timestamp TYPE.
  15944. Allowed values for TYPE are:
  15945. all: all timestamps
  15946. active: only active timestamps (<...>)
  15947. inactive: only inactive timestamps ([...])
  15948. scheduled: only scheduled timestamps
  15949. deadline: only deadline timestamps
  15950. closed: only closed time-stamps
  15951. When TYPE is nil, fall back on returning a regexp that matches
  15952. both scheduled and deadline timestamps."
  15953. (cl-case type
  15954. (all org-ts-regexp-both)
  15955. (active org-ts-regexp)
  15956. (inactive org-ts-regexp-inactive)
  15957. (scheduled org-scheduled-time-regexp)
  15958. (deadline org-deadline-time-regexp)
  15959. (closed org-closed-time-regexp)
  15960. (otherwise
  15961. (concat "\\<"
  15962. (regexp-opt (list org-deadline-string org-scheduled-string))
  15963. " *<\\([^>]+\\)>"))))
  15964. (defun org-check-before-date (d)
  15965. "Check if there are deadlines or scheduled entries before date D."
  15966. (interactive (list (org-read-date)))
  15967. (let* ((case-fold-search nil)
  15968. (regexp (org-re-timestamp org-ts-type))
  15969. (ts-type org-ts-type)
  15970. (callback
  15971. (lambda ()
  15972. (let ((match (match-string 1)))
  15973. (and (if (memq ts-type '(active inactive all))
  15974. (eq (org-element-type (org-element-context)) 'timestamp)
  15975. (org-at-planning-p))
  15976. (time-less-p
  15977. (org-time-string-to-time match)
  15978. (org-time-string-to-time d)))))))
  15979. (message "%d entries before %s"
  15980. (org-occur regexp nil callback)
  15981. d)))
  15982. (defun org-check-after-date (d)
  15983. "Check if there are deadlines or scheduled entries after date D."
  15984. (interactive (list (org-read-date)))
  15985. (let* ((case-fold-search nil)
  15986. (regexp (org-re-timestamp org-ts-type))
  15987. (ts-type org-ts-type)
  15988. (callback
  15989. (lambda ()
  15990. (let ((match (match-string 1)))
  15991. (and (if (memq ts-type '(active inactive all))
  15992. (eq (org-element-type (org-element-context)) 'timestamp)
  15993. (org-at-planning-p))
  15994. (not (time-less-p
  15995. (org-time-string-to-time match)
  15996. (org-time-string-to-time d))))))))
  15997. (message "%d entries after %s"
  15998. (org-occur regexp nil callback)
  15999. d)))
  16000. (defun org-check-dates-range (start-date end-date)
  16001. "Check for deadlines/scheduled entries between START-DATE and END-DATE."
  16002. (interactive (list (org-read-date nil nil nil "Range starts")
  16003. (org-read-date nil nil nil "Range end")))
  16004. (let ((case-fold-search nil)
  16005. (regexp (org-re-timestamp org-ts-type))
  16006. (callback
  16007. (let ((type org-ts-type))
  16008. (lambda ()
  16009. (let ((match (match-string 1)))
  16010. (and
  16011. (if (memq type '(active inactive all))
  16012. (eq (org-element-type (org-element-context)) 'timestamp)
  16013. (org-at-planning-p))
  16014. (not (time-less-p
  16015. (org-time-string-to-time match)
  16016. (org-time-string-to-time start-date)))
  16017. (time-less-p
  16018. (org-time-string-to-time match)
  16019. (org-time-string-to-time end-date))))))))
  16020. (message "%d entries between %s and %s"
  16021. (org-occur regexp nil callback) start-date end-date)))
  16022. (defun org-evaluate-time-range (&optional to-buffer)
  16023. "Evaluate a time range by computing the difference between start and end.
  16024. Normally the result is just printed in the echo area, but with prefix arg
  16025. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  16026. If the time range is actually in a table, the result is inserted into the
  16027. next column.
  16028. For time difference computation, a year is assumed to be exactly 365
  16029. days in order to avoid rounding problems."
  16030. (interactive "P")
  16031. (or
  16032. (org-clock-update-time-maybe)
  16033. (save-excursion
  16034. (unless (org-at-date-range-p t)
  16035. (goto-char (point-at-bol))
  16036. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  16037. (unless (org-at-date-range-p t)
  16038. (user-error "Not at a time-stamp range, and none found in current line")))
  16039. (let* ((ts1 (match-string 1))
  16040. (ts2 (match-string 2))
  16041. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  16042. (match-end (match-end 0))
  16043. (time1 (org-time-string-to-time ts1))
  16044. (time2 (org-time-string-to-time ts2))
  16045. (t1 (org-float-time time1))
  16046. (t2 (org-float-time time2))
  16047. (diff (abs (- t2 t1)))
  16048. (negative (< (- t2 t1) 0))
  16049. ;; (ys (floor (* 365 24 60 60)))
  16050. (ds (* 24 60 60))
  16051. (hs (* 60 60))
  16052. (fy "%dy %dd %02d:%02d")
  16053. (fy1 "%dy %dd")
  16054. (fd "%dd %02d:%02d")
  16055. (fd1 "%dd")
  16056. (fh "%02d:%02d")
  16057. y d h m align)
  16058. (if havetime
  16059. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  16060. y 0
  16061. d (floor (/ diff ds)) diff (mod diff ds)
  16062. h (floor (/ diff hs)) diff (mod diff hs)
  16063. m (floor (/ diff 60)))
  16064. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  16065. y 0
  16066. d (floor (+ (/ diff ds) 0.5))
  16067. h 0 m 0))
  16068. (if (not to-buffer)
  16069. (message "%s" (org-make-tdiff-string y d h m))
  16070. (if (org-at-table-p)
  16071. (progn
  16072. (goto-char match-end)
  16073. (setq align t)
  16074. (and (looking-at " *|") (goto-char (match-end 0))))
  16075. (goto-char match-end))
  16076. (when (looking-at
  16077. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  16078. (replace-match ""))
  16079. (when negative (insert " -"))
  16080. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  16081. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  16082. (insert " " (format fh h m))))
  16083. (when align (org-table-align))
  16084. (message "Time difference inserted")))))
  16085. (defun org-make-tdiff-string (y d h m)
  16086. (let ((fmt "")
  16087. (l nil))
  16088. (when (> y 0)
  16089. (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " "))
  16090. (push y l))
  16091. (when (> d 0)
  16092. (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " "))
  16093. (push d l))
  16094. (when (> h 0)
  16095. (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " "))
  16096. (push h l))
  16097. (when (> m 0)
  16098. (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " "))
  16099. (push m l))
  16100. (apply 'format fmt (nreverse l))))
  16101. (defun org-time-string-to-time (s &optional buffer pos)
  16102. "Convert a timestamp string into internal time."
  16103. (condition-case errdata
  16104. (apply 'encode-time (org-parse-time-string s))
  16105. (error (error "Bad timestamp `%s'%s\nError was: %s"
  16106. s (if (not (and buffer pos))
  16107. ""
  16108. (format-message " at %d in buffer `%s'" pos buffer))
  16109. (cdr errdata)))))
  16110. (defun org-time-string-to-seconds (s)
  16111. "Convert a timestamp string to a number of seconds."
  16112. (org-float-time (org-time-string-to-time s)))
  16113. (org-define-error 'org-diary-sexp-no-match "Unable to match diary sexp")
  16114. (defun org-time-string-to-absolute (s &optional daynr prefer show-all buffer pos)
  16115. "Convert time stamp S to an absolute day number.
  16116. If DAYNR in non-nil, and there is a specifier for a cyclic time
  16117. stamp, get the closest date to DAYNR. If PREFER is
  16118. `past' (respectively `future') return a date past (respectively
  16119. after) or equal to DAYNR.
  16120. POS is the location of time stamp S, as a buffer position in
  16121. BUFFER.
  16122. Diary sexp timestamps are matched against DAYNR, when non-nil.
  16123. If matching fails or DAYNR is nil, `org-diary-sexp-no-match' is
  16124. signalled."
  16125. (cond
  16126. ((string-match "\\`%%\\((.*)\\)" s)
  16127. ;; Sexp timestamp: try to match DAYNR, if available, since we're
  16128. ;; only able to match individual dates. If it fails, raise an
  16129. ;; error.
  16130. (if (and daynr
  16131. (org-diary-sexp-entry
  16132. (match-string 1 s) "" (calendar-gregorian-from-absolute daynr)))
  16133. daynr
  16134. (signal 'org-diary-sexp-no-match (list s))))
  16135. ((and daynr show-all) (org-closest-date s daynr prefer))
  16136. (t (time-to-days
  16137. (condition-case errdata
  16138. (apply #'encode-time (org-parse-time-string s))
  16139. (error (error "Bad timestamp `%s'%s\nError was: %s"
  16140. s
  16141. (if (not (and buffer pos)) ""
  16142. (format-message " at %d in buffer `%s'" pos buffer))
  16143. (cdr errdata))))))))
  16144. (defun org-days-to-iso-week (days)
  16145. "Return the iso week number."
  16146. (require 'cal-iso)
  16147. (car (calendar-iso-from-absolute days)))
  16148. (defun org-small-year-to-year (year)
  16149. "Convert 2-digit years into 4-digit years.
  16150. YEAR is expanded into one of the 30 next years, if possible, or
  16151. into a past one. Any year larger than 99 is returned unchanged."
  16152. (if (>= year 100) year
  16153. (let* ((current (string-to-number (format-time-string "%Y" (current-time))))
  16154. (century (/ current 100))
  16155. (offset (- year (% current 100))))
  16156. (cond ((> offset 30) (+ (* (1- century) 100) year))
  16157. ((> offset -70) (+ (* century 100) year))
  16158. (t (+ (* (1+ century) 100) year))))))
  16159. (defun org-time-from-absolute (d)
  16160. "Return the time corresponding to date D.
  16161. D may be an absolute day number, or a calendar-type list (month day year)."
  16162. (when (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  16163. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  16164. (defvar org-agenda-current-date)
  16165. (defun org-calendar-holiday ()
  16166. "List of holidays, for Diary display in Org mode."
  16167. (require 'holidays)
  16168. (let ((hl (calendar-check-holidays org-agenda-current-date)))
  16169. (and hl (mapconcat #'identity hl "; "))))
  16170. (defun org-diary-sexp-entry (sexp entry d)
  16171. "Process a SEXP diary ENTRY for date D."
  16172. (require 'diary-lib)
  16173. ;; `org-anniversary' and alike expect ENTRY and DATE to be bound
  16174. ;; dynamically.
  16175. (let* ((sexp `(let ((entry ,entry)
  16176. (date ',d))
  16177. ,(car (read-from-string sexp))))
  16178. (result (if calendar-debug-sexp (eval sexp)
  16179. (condition-case nil
  16180. (eval sexp)
  16181. (error
  16182. (beep)
  16183. (message "Bad sexp at line %d in %s: %s"
  16184. (org-current-line)
  16185. (buffer-file-name) sexp)
  16186. (sleep-for 2))))))
  16187. (cond ((stringp result) (split-string result "; "))
  16188. ((and (consp result)
  16189. (not (consp (cdr result)))
  16190. (stringp (cdr result))) (cdr result))
  16191. ((and (consp result)
  16192. (stringp (car result))) result)
  16193. (result entry))))
  16194. (defun org-diary-to-ical-string (frombuf)
  16195. "Get iCalendar entries from diary entries in buffer FROMBUF.
  16196. This uses the icalendar.el library."
  16197. (let* ((tmpdir (if (featurep 'xemacs)
  16198. (temp-directory)
  16199. temporary-file-directory))
  16200. (tmpfile (make-temp-name
  16201. (expand-file-name "orgics" tmpdir)))
  16202. buf rtn b e)
  16203. (with-current-buffer frombuf
  16204. (icalendar-export-region (point-min) (point-max) tmpfile)
  16205. (setq buf (find-buffer-visiting tmpfile))
  16206. (set-buffer buf)
  16207. (goto-char (point-min))
  16208. (when (re-search-forward "^BEGIN:VEVENT" nil t)
  16209. (setq b (match-beginning 0)))
  16210. (goto-char (point-max))
  16211. (when (re-search-backward "^END:VEVENT" nil t)
  16212. (setq e (match-end 0)))
  16213. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  16214. (kill-buffer buf)
  16215. (delete-file tmpfile)
  16216. rtn))
  16217. (defun org-closest-date (start current prefer)
  16218. "Return closest date to CURRENT starting from START.
  16219. CURRENT and START are both time stamps.
  16220. When PREFER is `past', return a date that is either CURRENT or
  16221. past. When PREFER is `future', return a date that is either
  16222. CURRENT or future.
  16223. Only time stamps with a repeater are modified. Any other time
  16224. stamp stay unchanged. In any case, return value is an absolute
  16225. day number."
  16226. (if (not (string-match "\\+\\([0-9]+\\)\\([hdwmy]\\)" start))
  16227. ;; No repeater. Do not shift time stamp.
  16228. (time-to-days (apply #'encode-time (org-parse-time-string start)))
  16229. (let ((value (string-to-number (match-string 1 start)))
  16230. (type (match-string 2 start)))
  16231. (if (= 0 value)
  16232. ;; Repeater with a 0-value is considered as void.
  16233. (time-to-days (apply #'encode-time (org-parse-time-string start)))
  16234. (let* ((base (org-date-to-gregorian start))
  16235. (target (org-date-to-gregorian current))
  16236. (sday (calendar-absolute-from-gregorian base))
  16237. (cday (calendar-absolute-from-gregorian target))
  16238. n1 n2)
  16239. ;; If START is already past CURRENT, just return START.
  16240. (if (<= cday sday) sday
  16241. ;; Compute closest date before (N1) and closest date past
  16242. ;; (N2) CURRENT.
  16243. (pcase type
  16244. ("h"
  16245. (let ((missing-hours
  16246. (mod (+ (- (* 24 (- cday sday))
  16247. (nth 2 (org-parse-time-string start)))
  16248. org-extend-today-until)
  16249. value)))
  16250. (setf n1 (if (= missing-hours 0) cday
  16251. (- cday (1+ (/ missing-hours 24)))))
  16252. (setf n2 (+ cday (/ (- value missing-hours) 24)))))
  16253. ((or "d" "w")
  16254. (let ((value (if (equal type "w") (* 7 value) value)))
  16255. (setf n1 (+ sday (* value (/ (- cday sday) value))))
  16256. (setf n2 (+ n1 value))))
  16257. ("m"
  16258. (let* ((add-months
  16259. (lambda (d n)
  16260. ;; Add N months to gregorian date D, i.e.,
  16261. ;; a list (MONTH DAY YEAR). Return a valid
  16262. ;; gregorian date.
  16263. (let ((m (+ (nth 0 d) n)))
  16264. (list (mod m 12)
  16265. (nth 1 d)
  16266. (+ (/ m 12) (nth 2 d))))))
  16267. (months ; Complete months to TARGET.
  16268. (* (/ (+ (* 12 (- (nth 2 target) (nth 2 base)))
  16269. (- (nth 0 target) (nth 0 base))
  16270. ;; If START's day is greater than
  16271. ;; TARGET's, remove incomplete month.
  16272. (if (> (nth 1 target) (nth 1 base)) 0 -1))
  16273. value)
  16274. value))
  16275. (before (funcall add-months base months)))
  16276. (setf n1 (calendar-absolute-from-gregorian before))
  16277. (setf n2
  16278. (calendar-absolute-from-gregorian
  16279. (funcall add-months before value)))))
  16280. (_
  16281. (let* ((d (nth 1 base))
  16282. (m (nth 0 base))
  16283. (y (nth 2 base))
  16284. (years ; Complete years to TARGET.
  16285. (* (/ (- (nth 2 target)
  16286. y
  16287. ;; If START's month and day are
  16288. ;; greater than TARGET's, remove
  16289. ;; incomplete year.
  16290. (if (or (> (nth 0 target) m)
  16291. (and (= (nth 0 target) m)
  16292. (> (nth 1 target) d)))
  16293. 0
  16294. 1))
  16295. value)
  16296. value))
  16297. (before (list m d (+ y years))))
  16298. (setf n1 (calendar-absolute-from-gregorian before))
  16299. (setf n2 (calendar-absolute-from-gregorian
  16300. (list m d (+ (nth 2 before) value)))))))
  16301. ;; Handle PREFER parameter, if any.
  16302. (cond
  16303. ((eq prefer 'past) (if (= cday n2) n2 n1))
  16304. ((eq prefer 'future) (if (= cday n1) n1 n2))
  16305. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))))))))
  16306. (defun org-date-to-gregorian (d)
  16307. "Turn any specification of date D into a Gregorian date for the calendar."
  16308. (cond ((integerp d) (calendar-gregorian-from-absolute d))
  16309. ((and (listp d) (= (length d) 3)) d)
  16310. ((stringp d)
  16311. (let ((d (org-parse-time-string d)))
  16312. (list (nth 4 d) (nth 3 d) (nth 5 d))))
  16313. ((listp d) (list (nth 4 d) (nth 3 d) (nth 5 d)))))
  16314. (defun org-parse-time-string (s &optional nodefault)
  16315. "Parse the standard Org-mode time string.
  16316. This should be a lot faster than the normal `parse-time-string'.
  16317. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  16318. hour and minute fields will be nil if not given."
  16319. (cond ((string-match org-ts-regexp0 s)
  16320. (list 0
  16321. (when (or (match-beginning 8) (not nodefault))
  16322. (string-to-number (or (match-string 8 s) "0")))
  16323. (when (or (match-beginning 7) (not nodefault))
  16324. (string-to-number (or (match-string 7 s) "0")))
  16325. (string-to-number (match-string 4 s))
  16326. (string-to-number (match-string 3 s))
  16327. (string-to-number (match-string 2 s))
  16328. nil nil nil))
  16329. ((string-match "^<[^>]+>$" s)
  16330. (decode-time (seconds-to-time (org-matcher-time s))))
  16331. (t (error "Not a standard Org-mode time string: %s" s))))
  16332. (defun org-timestamp-up (&optional arg)
  16333. "Increase the date item at the cursor by one.
  16334. If the cursor is on the year, change the year. If it is on the month,
  16335. the day or the time, change that.
  16336. With prefix ARG, change by that many units."
  16337. (interactive "p")
  16338. (org-timestamp-change (prefix-numeric-value arg) nil 'updown))
  16339. (defun org-timestamp-down (&optional arg)
  16340. "Decrease the date item at the cursor by one.
  16341. If the cursor is on the year, change the year. If it is on the month,
  16342. the day or the time, change that.
  16343. With prefix ARG, change by that many units."
  16344. (interactive "p")
  16345. (org-timestamp-change (- (prefix-numeric-value arg)) nil 'updown))
  16346. (defun org-timestamp-up-day (&optional arg)
  16347. "Increase the date in the time stamp by one day.
  16348. With prefix ARG, change that many days."
  16349. (interactive "p")
  16350. (if (and (not (org-at-timestamp-p t))
  16351. (org-at-heading-p))
  16352. (org-todo 'up)
  16353. (org-timestamp-change (prefix-numeric-value arg) 'day 'updown)))
  16354. (defun org-timestamp-down-day (&optional arg)
  16355. "Decrease the date in the time stamp by one day.
  16356. With prefix ARG, change that many days."
  16357. (interactive "p")
  16358. (if (and (not (org-at-timestamp-p t))
  16359. (org-at-heading-p))
  16360. (org-todo 'down)
  16361. (org-timestamp-change (- (prefix-numeric-value arg)) 'day) 'updown))
  16362. (defun org-at-timestamp-p (&optional inactive-ok)
  16363. "Non-nil if point is inside a timestamp.
  16364. When optional argument INACTIVE-OK is non-nil, also consider
  16365. inactive timestamps.
  16366. When this function returns a non-nil value, match data is set
  16367. according to `org-ts-regexp3' or `org-ts-regexp2', depending on
  16368. INACTIVE-OK."
  16369. (interactive)
  16370. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  16371. (pos (point))
  16372. (ans (or (looking-at tsr)
  16373. (save-excursion
  16374. (skip-chars-backward "^[<\n\r\t")
  16375. (when (> (point) (point-min)) (backward-char 1))
  16376. (and (looking-at tsr)
  16377. (> (- (match-end 0) pos) -1))))))
  16378. (and ans
  16379. (boundp 'org-ts-what)
  16380. (setq org-ts-what
  16381. (cond
  16382. ((= pos (match-beginning 0)) 'bracket)
  16383. ;; Point is considered to be "on the bracket" whether
  16384. ;; it's really on it or right after it.
  16385. ((= pos (1- (match-end 0))) 'bracket)
  16386. ((= pos (match-end 0)) 'after)
  16387. ((org-pos-in-match-range pos 2) 'year)
  16388. ((org-pos-in-match-range pos 3) 'month)
  16389. ((org-pos-in-match-range pos 7) 'hour)
  16390. ((org-pos-in-match-range pos 8) 'minute)
  16391. ((or (org-pos-in-match-range pos 4)
  16392. (org-pos-in-match-range pos 5)) 'day)
  16393. ((and (> pos (or (match-end 8) (match-end 5)))
  16394. (< pos (match-end 0)))
  16395. (- pos (or (match-end 8) (match-end 5))))
  16396. (t 'day))))
  16397. ans))
  16398. (defun org-toggle-timestamp-type ()
  16399. "Toggle the type (<active> or [inactive]) of a time stamp."
  16400. (interactive)
  16401. (when (org-at-timestamp-p t)
  16402. (let ((beg (match-beginning 0)) (end (match-end 0))
  16403. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  16404. (save-excursion
  16405. (goto-char beg)
  16406. (while (re-search-forward "[][<>]" end t)
  16407. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  16408. t t)))
  16409. (message "Timestamp is now %sactive"
  16410. (if (equal (char-after beg) ?<) "" "in")))))
  16411. (defun org-at-clock-log-p nil
  16412. "Is the cursor on the clock log line?"
  16413. (save-excursion
  16414. (move-beginning-of-line 1)
  16415. (looking-at org-clock-line-re)))
  16416. (defvar org-clock-history) ; defined in org-clock.el
  16417. (defvar org-clock-adjust-closest nil) ; defined in org-clock.el
  16418. (defun org-timestamp-change (n &optional what updown suppress-tmp-delay)
  16419. "Change the date in the time stamp at point.
  16420. The date will be changed by N times WHAT. WHAT can be `day', `month',
  16421. `year', `minute', `second'. If WHAT is not given, the cursor position
  16422. in the timestamp determines what will be changed.
  16423. When SUPPRESS-TMP-DELAY is non-nil, suppress delays like \"--2d\"."
  16424. (let ((origin (point)) origin-cat
  16425. with-hm inactive
  16426. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  16427. org-ts-what
  16428. extra rem
  16429. ts time time0 fixnext clrgx)
  16430. (unless (org-at-timestamp-p t)
  16431. (user-error "Not at a timestamp"))
  16432. (if (and (not what) (eq org-ts-what 'bracket))
  16433. (org-toggle-timestamp-type)
  16434. ;; Point isn't on brackets. Remember the part of the time-stamp
  16435. ;; the point was in. Indeed, size of time-stamps may change,
  16436. ;; but point must be kept in the same category nonetheless.
  16437. (setq origin-cat org-ts-what)
  16438. (when (and (not what) (not (eq org-ts-what 'day))
  16439. org-display-custom-times
  16440. (get-text-property (point) 'display)
  16441. (not (get-text-property (1- (point)) 'display)))
  16442. (setq org-ts-what 'day))
  16443. (setq org-ts-what (or what org-ts-what)
  16444. inactive (= (char-after (match-beginning 0)) ?\[)
  16445. ts (match-string 0))
  16446. (replace-match "")
  16447. (when (string-match
  16448. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?-?[-+][0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)*\\)[]>]"
  16449. ts)
  16450. (setq extra (match-string 1 ts))
  16451. (when suppress-tmp-delay
  16452. (setq extra (replace-regexp-in-string " --[0-9]+[hdwmy]" "" extra))))
  16453. (when (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  16454. (setq with-hm t))
  16455. (setq time0 (org-parse-time-string ts))
  16456. (when (and updown
  16457. (eq org-ts-what 'minute)
  16458. (not current-prefix-arg))
  16459. ;; This looks like s-up and s-down. Change by one rounding step.
  16460. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  16461. (unless (= 0 (setq rem (% (nth 1 time0) dm)))
  16462. (setcar (cdr time0) (+ (nth 1 time0)
  16463. (if (> n 0) (- rem) (- dm rem))))))
  16464. (setq time
  16465. (apply #'encode-time
  16466. (or (car time0) 0)
  16467. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  16468. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  16469. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  16470. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  16471. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  16472. (nthcdr 6 time0)))
  16473. (when (and (member org-ts-what '(hour minute))
  16474. extra
  16475. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  16476. (setq extra (org-modify-ts-extra
  16477. extra
  16478. (if (eq org-ts-what 'hour) 2 5)
  16479. n dm)))
  16480. (when (integerp org-ts-what)
  16481. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  16482. (when (eq what 'calendar)
  16483. (let ((cal-date (org-get-date-from-calendar)))
  16484. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  16485. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  16486. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  16487. (setcar time0 (or (car time0) 0))
  16488. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  16489. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  16490. (setq time (apply 'encode-time time0))))
  16491. ;; Insert the new time-stamp, and ensure point stays in the same
  16492. ;; category as before (i.e. not after the last position in that
  16493. ;; category).
  16494. (let ((pos (point)))
  16495. ;; Stay before inserted string. `save-excursion' is of no use.
  16496. (setq org-last-changed-timestamp
  16497. (org-insert-time-stamp time with-hm inactive nil nil extra))
  16498. (goto-char pos))
  16499. (save-match-data
  16500. (looking-at org-ts-regexp3)
  16501. (goto-char (cond
  16502. ;; `day' category ends before `hour' if any, or at
  16503. ;; the end of the day name.
  16504. ((eq origin-cat 'day)
  16505. (min (or (match-beginning 7) (1- (match-end 5))) origin))
  16506. ((eq origin-cat 'hour) (min (match-end 7) origin))
  16507. ((eq origin-cat 'minute) (min (1- (match-end 8)) origin))
  16508. ((integerp origin-cat) (min (1- (match-end 0)) origin))
  16509. ;; `year' and `month' have both fixed size: point
  16510. ;; couldn't have moved into another part.
  16511. (t origin))))
  16512. ;; Update clock if on a CLOCK line.
  16513. (org-clock-update-time-maybe)
  16514. ;; Maybe adjust the closest clock in `org-clock-history'
  16515. (when org-clock-adjust-closest
  16516. (if (not (and (org-at-clock-log-p)
  16517. (< 1 (length (delq nil (mapcar 'marker-position
  16518. org-clock-history))))))
  16519. (message "No clock to adjust")
  16520. (cond ((save-excursion ; fix previous clock?
  16521. (re-search-backward org-ts-regexp0 nil t)
  16522. (org-looking-back (concat org-clock-string " \\[")
  16523. (line-beginning-position)))
  16524. (setq fixnext 1 clrgx (concat org-ts-regexp0 "\\] =>.*$")))
  16525. ((save-excursion ; fix next clock?
  16526. (re-search-backward org-ts-regexp0 nil t)
  16527. (looking-at (concat org-ts-regexp0 "\\] =>")))
  16528. (setq fixnext -1 clrgx (concat org-clock-string " \\[" org-ts-regexp0))))
  16529. (save-window-excursion
  16530. ;; Find closest clock to point, adjust the previous/next one in history
  16531. (let* ((p (save-excursion (org-back-to-heading t)))
  16532. (cl (mapcar (lambda(c) (abs (- (marker-position c) p))) org-clock-history))
  16533. (clfixnth
  16534. (+ fixnext (- (length cl) (or (length (member (apply 'min cl) cl)) 100))))
  16535. (clfixpos (unless (> 0 clfixnth) (nth clfixnth org-clock-history))))
  16536. (if (not clfixpos)
  16537. (message "No clock to adjust")
  16538. (save-excursion
  16539. (org-goto-marker-or-bmk clfixpos)
  16540. (org-show-subtree)
  16541. (when (re-search-forward clrgx nil t)
  16542. (goto-char (match-beginning 1))
  16543. (let (org-clock-adjust-closest)
  16544. (org-timestamp-change n org-ts-what updown))
  16545. (message "Clock adjusted in %s for heading: %s"
  16546. (file-name-nondirectory (buffer-file-name))
  16547. (org-get-heading t t)))))))))
  16548. ;; Try to recenter the calendar window, if any.
  16549. (when (and org-calendar-follow-timestamp-change
  16550. (get-buffer-window "*Calendar*" t)
  16551. (memq org-ts-what '(day month year)))
  16552. (org-recenter-calendar (time-to-days time))))))
  16553. (defun org-modify-ts-extra (s pos n dm)
  16554. "Change the different parts of the lead-time and repeat fields in timestamp."
  16555. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  16556. ng h m new rem)
  16557. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  16558. (cond
  16559. ((or (org-pos-in-match-range pos 2)
  16560. (org-pos-in-match-range pos 3))
  16561. (setq m (string-to-number (match-string 3 s))
  16562. h (string-to-number (match-string 2 s)))
  16563. (if (org-pos-in-match-range pos 2)
  16564. (setq h (+ h n))
  16565. (setq n (* dm (org-no-warnings (signum n))))
  16566. (unless (= 0 (setq rem (% m dm)))
  16567. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  16568. (setq m (+ m n)))
  16569. (when (< m 0) (setq m (+ m 60) h (1- h)))
  16570. (when (> m 59) (setq m (- m 60) h (1+ h)))
  16571. (setq h (mod h 24))
  16572. (setq ng 1 new (format "-%02d:%02d" h m)))
  16573. ((org-pos-in-match-range pos 6)
  16574. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  16575. ((org-pos-in-match-range pos 5)
  16576. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  16577. ((org-pos-in-match-range pos 9)
  16578. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  16579. ((org-pos-in-match-range pos 8)
  16580. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  16581. (when ng
  16582. (setq s (concat
  16583. (substring s 0 (match-beginning ng))
  16584. new
  16585. (substring s (match-end ng))))))
  16586. s))
  16587. (defun org-recenter-calendar (d)
  16588. "If the calendar is visible, recenter it to date D."
  16589. (let ((cwin (get-buffer-window "*Calendar*" t)))
  16590. (when cwin
  16591. (let ((calendar-move-hook nil))
  16592. (with-selected-window cwin
  16593. (calendar-goto-date
  16594. (if (listp d) d (calendar-gregorian-from-absolute d))))))))
  16595. (defun org-goto-calendar (&optional arg)
  16596. "Go to the Emacs calendar at the current date.
  16597. If there is a time stamp in the current line, go to that date.
  16598. A prefix ARG can be used to force the current date."
  16599. (interactive "P")
  16600. (let ((tsr org-ts-regexp) diff
  16601. (calendar-move-hook nil)
  16602. (calendar-view-holidays-initially-flag nil)
  16603. (calendar-view-diary-initially-flag nil))
  16604. (when (or (org-at-timestamp-p)
  16605. (save-excursion
  16606. (beginning-of-line 1)
  16607. (looking-at (concat ".*" tsr))))
  16608. (let ((d1 (time-to-days (current-time)))
  16609. (d2 (time-to-days
  16610. (org-time-string-to-time (match-string 1)))))
  16611. (setq diff (- d2 d1))))
  16612. (calendar)
  16613. (calendar-goto-today)
  16614. (when (and diff (not arg)) (calendar-forward-day diff))))
  16615. (defun org-get-date-from-calendar ()
  16616. "Return a list (month day year) of date at point in calendar."
  16617. (with-current-buffer "*Calendar*"
  16618. (save-match-data
  16619. (calendar-cursor-to-date))))
  16620. (defun org-date-from-calendar ()
  16621. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  16622. If there is already a time stamp at the cursor position, update it."
  16623. (interactive)
  16624. (if (org-at-timestamp-p t)
  16625. (org-timestamp-change 0 'calendar)
  16626. (let ((cal-date (org-get-date-from-calendar)))
  16627. (org-insert-time-stamp
  16628. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  16629. (defcustom org-effort-durations
  16630. `(("min" . 1)
  16631. ("h" . 60)
  16632. ("d" . ,(* 60 8))
  16633. ("w" . ,(* 60 8 5))
  16634. ("m" . ,(* 60 8 5 4))
  16635. ("y" . ,(* 60 8 5 40)))
  16636. "Conversion factor to minutes for an effort modifier.
  16637. Each entry has the form (MODIFIER . MINUTES).
  16638. In an effort string, a number followed by MODIFIER is multiplied
  16639. by the specified number of MINUTES to obtain an effort in
  16640. minutes.
  16641. For example, if the value of this variable is ((\"hours\" . 60)), then an
  16642. effort string \"2hours\" is equivalent to 120 minutes."
  16643. :group 'org-agenda
  16644. :version "25.1"
  16645. :package-version '(Org . "8.3")
  16646. :type '(alist :key-type (string :tag "Modifier")
  16647. :value-type (number :tag "Minutes")))
  16648. (defun org-minutes-to-clocksum-string (m)
  16649. "Format number of minutes as a clocksum string.
  16650. The format is determined by `org-time-clocksum-format',
  16651. `org-time-clocksum-use-fractional' and
  16652. `org-time-clocksum-fractional-format' and
  16653. `org-time-clocksum-use-effort-durations'."
  16654. (let ((clocksum "")
  16655. (m (round m)) ; Don't allow fractions of minutes
  16656. h d w mo y fmt n)
  16657. (setq h (if org-time-clocksum-use-effort-durations
  16658. (cdr (assoc "h" org-effort-durations)) 60)
  16659. d (if org-time-clocksum-use-effort-durations
  16660. (/ (cdr (assoc "d" org-effort-durations)) h) 24)
  16661. w (if org-time-clocksum-use-effort-durations
  16662. (/ (cdr (assoc "w" org-effort-durations)) (* d h)) 7)
  16663. mo (if org-time-clocksum-use-effort-durations
  16664. (/ (cdr (assoc "m" org-effort-durations)) (* d h)) 30)
  16665. y (if org-time-clocksum-use-effort-durations
  16666. (/ (cdr (assoc "y" org-effort-durations)) (* d h)) 365))
  16667. ;; fractional format
  16668. (if org-time-clocksum-use-fractional
  16669. (cond
  16670. ;; single format string
  16671. ((stringp org-time-clocksum-fractional-format)
  16672. (format org-time-clocksum-fractional-format (/ m (float h))))
  16673. ;; choice of fractional formats for different time units
  16674. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :years))
  16675. (> (/ (truncate m) (* y d h)) 0))
  16676. (format fmt (/ m (* y d (float h)))))
  16677. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :months))
  16678. (> (/ (truncate m) (* mo d h)) 0))
  16679. (format fmt (/ m (* mo d (float h)))))
  16680. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :weeks))
  16681. (> (/ (truncate m) (* w d h)) 0))
  16682. (format fmt (/ m (* w d (float h)))))
  16683. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :days))
  16684. (> (/ (truncate m) (* d h)) 0))
  16685. (format fmt (/ m (* d (float h)))))
  16686. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :hours))
  16687. (> (/ (truncate m) h) 0))
  16688. (format fmt (/ m (float h))))
  16689. ((setq fmt (plist-get org-time-clocksum-fractional-format :minutes))
  16690. (format fmt m))
  16691. ;; fall back to smallest time unit with a format
  16692. ((setq fmt (plist-get org-time-clocksum-fractional-format :hours))
  16693. (format fmt (/ m (float h))))
  16694. ((setq fmt (plist-get org-time-clocksum-fractional-format :days))
  16695. (format fmt (/ m (* d (float h)))))
  16696. ((setq fmt (plist-get org-time-clocksum-fractional-format :weeks))
  16697. (format fmt (/ m (* w d (float h)))))
  16698. ((setq fmt (plist-get org-time-clocksum-fractional-format :months))
  16699. (format fmt (/ m (* mo d (float h)))))
  16700. ((setq fmt (plist-get org-time-clocksum-fractional-format :years))
  16701. (format fmt (/ m (* y d (float h))))))
  16702. ;; standard (non-fractional) format, with single format string
  16703. (if (stringp org-time-clocksum-format)
  16704. (format org-time-clocksum-format (setq n (/ m h)) (- m (* h n)))
  16705. ;; separate formats components
  16706. (and (setq fmt (plist-get org-time-clocksum-format :years))
  16707. (or (> (setq n (/ (truncate m) (* y d h))) 0)
  16708. (plist-get org-time-clocksum-format :require-years))
  16709. (setq clocksum (concat clocksum (format fmt n))
  16710. m (- m (* n y d h))))
  16711. (and (setq fmt (plist-get org-time-clocksum-format :months))
  16712. (or (> (setq n (/ (truncate m) (* mo d h))) 0)
  16713. (plist-get org-time-clocksum-format :require-months))
  16714. (setq clocksum (concat clocksum (format fmt n))
  16715. m (- m (* n mo d h))))
  16716. (and (setq fmt (plist-get org-time-clocksum-format :weeks))
  16717. (or (> (setq n (/ (truncate m) (* w d h))) 0)
  16718. (plist-get org-time-clocksum-format :require-weeks))
  16719. (setq clocksum (concat clocksum (format fmt n))
  16720. m (- m (* n w d h))))
  16721. (and (setq fmt (plist-get org-time-clocksum-format :days))
  16722. (or (> (setq n (/ (truncate m) (* d h))) 0)
  16723. (plist-get org-time-clocksum-format :require-days))
  16724. (setq clocksum (concat clocksum (format fmt n))
  16725. m (- m (* n d h))))
  16726. (and (setq fmt (plist-get org-time-clocksum-format :hours))
  16727. (or (> (setq n (/ (truncate m) h)) 0)
  16728. (plist-get org-time-clocksum-format :require-hours))
  16729. (setq clocksum (concat clocksum (format fmt n))
  16730. m (- m (* n h))))
  16731. (and (setq fmt (plist-get org-time-clocksum-format :minutes))
  16732. (or (> m 0) (plist-get org-time-clocksum-format :require-minutes))
  16733. (setq clocksum (concat clocksum (format fmt m))))
  16734. ;; return formatted time duration
  16735. clocksum))))
  16736. (defalias 'org-minutes-to-hh:mm-string 'org-minutes-to-clocksum-string)
  16737. (make-obsolete 'org-minutes-to-hh:mm-string 'org-minutes-to-clocksum-string
  16738. "Org mode version 8.0")
  16739. (defun org-hours-to-clocksum-string (n)
  16740. (org-minutes-to-clocksum-string (* n 60)))
  16741. (defun org-hh:mm-string-to-minutes (s)
  16742. "Convert a string H:MM to a number of minutes.
  16743. If the string is just a number, interpret it as minutes.
  16744. In fact, the first hh:mm or number in the string will be taken,
  16745. there can be extra stuff in the string.
  16746. If no number is found, the return value is 0."
  16747. (cond
  16748. ((integerp s) s)
  16749. ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  16750. (+ (* (string-to-number (match-string 1 s)) 60)
  16751. (string-to-number (match-string 2 s))))
  16752. ((string-match "\\([0-9]+\\)" s)
  16753. (string-to-number (match-string 1 s)))
  16754. (t 0)))
  16755. (defcustom org-image-actual-width t
  16756. "Should we use the actual width of images when inlining them?
  16757. When set to t, always use the image width.
  16758. When set to a number, use imagemagick (when available) to set
  16759. the image's width to this value.
  16760. When set to a number in a list, try to get the width from any
  16761. #+ATTR.* keyword if it matches a width specification like
  16762. #+ATTR_HTML: :width 300px
  16763. and fall back on that number if none is found.
  16764. When set to nil, try to get the width from an #+ATTR.* keyword
  16765. and fall back on the original width if none is found.
  16766. This requires Emacs >= 24.1, build with imagemagick support."
  16767. :group 'org-appearance
  16768. :version "24.4"
  16769. :package-version '(Org . "8.0")
  16770. :type '(choice
  16771. (const :tag "Use the image width" t)
  16772. (integer :tag "Use a number of pixels")
  16773. (list :tag "Use #+ATTR* or a number of pixels" (integer))
  16774. (const :tag "Use #+ATTR* or don't resize" nil)))
  16775. (defcustom org-agenda-inhibit-startup nil
  16776. "Inhibit startup when preparing agenda buffers.
  16777. When this variable is t, the initialization of the Org agenda
  16778. buffers is inhibited: e.g. the visibility state is not set, the
  16779. tables are not re-aligned, etc."
  16780. :type 'boolean
  16781. :version "24.3"
  16782. :group 'org-agenda)
  16783. (define-obsolete-variable-alias
  16784. 'org-agenda-ignore-drawer-properties
  16785. 'org-agenda-ignore-properties "25.1")
  16786. (defcustom org-agenda-ignore-properties nil
  16787. "Avoid updating text properties when building the agenda.
  16788. Properties are used to prepare buffers for effort estimates,
  16789. appointments, statistics and subtree-local categories.
  16790. If you don't use these in the agenda, you can add them to this
  16791. list and agenda building will be a bit faster.
  16792. The value is a list, with zero or more of the symbols `effort', `appt',
  16793. `stats' or `category'."
  16794. :type '(set :greedy t
  16795. (const effort)
  16796. (const appt)
  16797. (const stats)
  16798. (const category))
  16799. :version "25.1"
  16800. :package-version '(Org . "8.3")
  16801. :group 'org-agenda)
  16802. (defun org-duration-string-to-minutes (s &optional output-to-string)
  16803. "Convert a duration string S to minutes.
  16804. A bare number is interpreted as minutes, modifiers can be set by
  16805. customizing `org-effort-durations' (which see).
  16806. Entries containing a colon are interpreted as H:MM by
  16807. `org-hh:mm-string-to-minutes'."
  16808. (let ((result 0)
  16809. (re (concat "\\([0-9.]+\\) *\\("
  16810. (regexp-opt (mapcar 'car org-effort-durations))
  16811. "\\)")))
  16812. (while (string-match re s)
  16813. (cl-incf result (* (cdr (assoc (match-string 2 s) org-effort-durations))
  16814. (string-to-number (match-string 1 s))))
  16815. (setq s (replace-match "" nil t s)))
  16816. (setq result (floor result))
  16817. (cl-incf result (org-hh:mm-string-to-minutes s))
  16818. (if output-to-string (number-to-string result) result)))
  16819. ;;;; Files
  16820. (defun org-save-all-org-buffers ()
  16821. "Save all Org-mode buffers without user confirmation."
  16822. (interactive)
  16823. (message "Saving all Org-mode buffers...")
  16824. (save-some-buffers t (lambda () (derived-mode-p 'org-mode)))
  16825. (when (featurep 'org-id) (org-id-locations-save))
  16826. (message "Saving all Org-mode buffers... done"))
  16827. (defun org-revert-all-org-buffers ()
  16828. "Revert all Org-mode buffers.
  16829. Prompt for confirmation when there are unsaved changes.
  16830. Be sure you know what you are doing before letting this function
  16831. overwrite your changes.
  16832. This function is useful in a setup where one tracks org files
  16833. with a version control system, to revert on one machine after pulling
  16834. changes from another. I believe the procedure must be like this:
  16835. 1. M-x org-save-all-org-buffers
  16836. 2. Pull changes from the other machine, resolve conflicts
  16837. 3. M-x org-revert-all-org-buffers"
  16838. (interactive)
  16839. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  16840. (user-error "Abort"))
  16841. (save-excursion
  16842. (save-window-excursion
  16843. (dolist (b (buffer-list))
  16844. (when (and (with-current-buffer b (derived-mode-p 'org-mode))
  16845. (with-current-buffer b buffer-file-name))
  16846. (org-pop-to-buffer-same-window b)
  16847. (revert-buffer t 'no-confirm)))
  16848. (when (and (featurep 'org-id) org-id-track-globally)
  16849. (org-id-locations-load)))))
  16850. ;;;; Agenda files
  16851. ;;;###autoload
  16852. (defun org-switchb (&optional arg)
  16853. "Switch between Org buffers.
  16854. With \\[universal-argument] prefix, restrict available buffers to files.
  16855. With \\[universal-argument] \\[universal-argument] \
  16856. prefix, restrict available buffers to agenda files."
  16857. (interactive "P")
  16858. (let ((blist (org-buffer-list
  16859. (cond ((equal arg '(4)) 'files)
  16860. ((equal arg '(16)) 'agenda)))))
  16861. (org-pop-to-buffer-same-window
  16862. (completing-read "Org buffer: "
  16863. (mapcar #'list (mapcar #'buffer-name blist))
  16864. nil t))))
  16865. (defun org-buffer-list (&optional predicate exclude-tmp)
  16866. "Return a list of Org buffers.
  16867. PREDICATE can be `export', `files' or `agenda'.
  16868. export restrict the list to Export buffers.
  16869. files restrict the list to buffers visiting Org files.
  16870. agenda restrict the list to buffers visiting agenda files.
  16871. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  16872. (let* ((bfn nil)
  16873. (agenda-files (and (eq predicate 'agenda)
  16874. (mapcar 'file-truename (org-agenda-files t))))
  16875. (filter
  16876. (cond
  16877. ((eq predicate 'files)
  16878. (lambda (b) (with-current-buffer b (derived-mode-p 'org-mode))))
  16879. ((eq predicate 'export)
  16880. (lambda (b) (string-match "\\*Org .*Export" (buffer-name b))))
  16881. ((eq predicate 'agenda)
  16882. (lambda (b)
  16883. (with-current-buffer b
  16884. (and (derived-mode-p 'org-mode)
  16885. (setq bfn (buffer-file-name b))
  16886. (member (file-truename bfn) agenda-files)))))
  16887. (t (lambda (b) (with-current-buffer b
  16888. (or (derived-mode-p 'org-mode)
  16889. (string-match "\\*Org .*Export"
  16890. (buffer-name b)))))))))
  16891. (delq nil
  16892. (mapcar
  16893. (lambda(b)
  16894. (if (and (funcall filter b)
  16895. (or (not exclude-tmp)
  16896. (not (string-match "tmp" (buffer-name b)))))
  16897. b
  16898. nil))
  16899. (buffer-list)))))
  16900. (defun org-agenda-files (&optional unrestricted archives)
  16901. "Get the list of agenda files.
  16902. Optional UNRESTRICTED means return the full list even if a restriction
  16903. is currently in place.
  16904. When ARCHIVES is t, include all archive files that are really being
  16905. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  16906. `org-agenda-archives-mode' is t."
  16907. (let ((files
  16908. (cond
  16909. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  16910. ((stringp org-agenda-files) (org-read-agenda-file-list))
  16911. ((listp org-agenda-files) org-agenda-files)
  16912. (t (error "Invalid value of `org-agenda-files'")))))
  16913. (setq files (apply 'append
  16914. (mapcar (lambda (f)
  16915. (if (file-directory-p f)
  16916. (directory-files
  16917. f t org-agenda-file-regexp)
  16918. (list f)))
  16919. files)))
  16920. (when org-agenda-skip-unavailable-files
  16921. (setq files (delq nil
  16922. (mapcar (function
  16923. (lambda (file)
  16924. (and (file-readable-p file) file)))
  16925. files))))
  16926. (when (or (eq archives t)
  16927. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  16928. (setq files (org-add-archive-files files)))
  16929. files))
  16930. (defun org-agenda-file-p (&optional file)
  16931. "Return non-nil, if FILE is an agenda file.
  16932. If FILE is omitted, use the file associated with the current
  16933. buffer."
  16934. (let ((fname (or file (buffer-file-name))))
  16935. (and fname
  16936. (member (file-truename fname)
  16937. (mapcar #'file-truename (org-agenda-files t))))))
  16938. (defun org-edit-agenda-file-list ()
  16939. "Edit the list of agenda files.
  16940. Depending on setup, this either uses customize to edit the variable
  16941. `org-agenda-files', or it visits the file that is holding the list. In the
  16942. latter case, the buffer is set up in a way that saving it automatically kills
  16943. the buffer and restores the previous window configuration."
  16944. (interactive)
  16945. (if (stringp org-agenda-files)
  16946. (let ((cw (current-window-configuration)))
  16947. (find-file org-agenda-files)
  16948. (setq-local org-window-configuration cw)
  16949. (org-add-hook 'after-save-hook
  16950. (lambda ()
  16951. (set-window-configuration
  16952. (prog1 org-window-configuration
  16953. (kill-buffer (current-buffer))))
  16954. (org-install-agenda-files-menu)
  16955. (message "New agenda file list installed"))
  16956. nil 'local)
  16957. (message "%s" (substitute-command-keys
  16958. "Edit list and finish with \\[save-buffer]")))
  16959. (customize-variable 'org-agenda-files)))
  16960. (defun org-store-new-agenda-file-list (list)
  16961. "Set new value for the agenda file list and save it correctly."
  16962. (if (stringp org-agenda-files)
  16963. (let ((fe (org-read-agenda-file-list t)) b u)
  16964. (while (setq b (find-buffer-visiting org-agenda-files))
  16965. (kill-buffer b))
  16966. (with-temp-file org-agenda-files
  16967. (insert
  16968. (mapconcat
  16969. (lambda (f) ;; Keep un-expanded entries.
  16970. (if (setq u (assoc f fe))
  16971. (cdr u)
  16972. f))
  16973. list "\n")
  16974. "\n")))
  16975. (let ((org-mode-hook nil) (org-inhibit-startup t)
  16976. (org-insert-mode-line-in-empty-file nil))
  16977. (setq org-agenda-files list)
  16978. (customize-save-variable 'org-agenda-files org-agenda-files))))
  16979. (defun org-read-agenda-file-list (&optional pair-with-expansion)
  16980. "Read the list of agenda files from a file.
  16981. If PAIR-WITH-EXPANSION is t return pairs with un-expanded
  16982. filenames, used by `org-store-new-agenda-file-list' to write back
  16983. un-expanded file names."
  16984. (when (file-directory-p org-agenda-files)
  16985. (error "`org-agenda-files' cannot be a single directory"))
  16986. (when (stringp org-agenda-files)
  16987. (with-temp-buffer
  16988. (insert-file-contents org-agenda-files)
  16989. (mapcar
  16990. (lambda (f)
  16991. (let ((e (expand-file-name (substitute-in-file-name f)
  16992. org-directory)))
  16993. (if pair-with-expansion
  16994. (cons e f)
  16995. e)))
  16996. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*")))))
  16997. ;;;###autoload
  16998. (defun org-cycle-agenda-files ()
  16999. "Cycle through the files in `org-agenda-files'.
  17000. If the current buffer visits an agenda file, find the next one in the list.
  17001. If the current buffer does not, find the first agenda file."
  17002. (interactive)
  17003. (let* ((fs (or (org-agenda-files t)
  17004. (user-error "No agenda files")))
  17005. (files (copy-sequence fs))
  17006. (tcf (and buffer-file-name (file-truename buffer-file-name)))
  17007. file)
  17008. (when tcf
  17009. (while (and (setq file (pop files))
  17010. (not (equal (file-truename file) tcf)))))
  17011. (find-file (car (or files fs)))
  17012. (when (buffer-base-buffer) (org-pop-to-buffer-same-window (buffer-base-buffer)))))
  17013. (defun org-agenda-file-to-front (&optional to-end)
  17014. "Move/add the current file to the top of the agenda file list.
  17015. If the file is not present in the list, it is added to the front. If it is
  17016. present, it is moved there. With optional argument TO-END, add/move to the
  17017. end of the list."
  17018. (interactive "P")
  17019. (let ((org-agenda-skip-unavailable-files nil)
  17020. (file-alist (mapcar (lambda (x)
  17021. (cons (file-truename x) x))
  17022. (org-agenda-files t)))
  17023. (ctf (file-truename
  17024. (or buffer-file-name
  17025. (user-error "Please save the current buffer to a file"))))
  17026. x had)
  17027. (setq x (assoc ctf file-alist) had x)
  17028. (unless x (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  17029. (if to-end
  17030. (setq file-alist (append (delq x file-alist) (list x)))
  17031. (setq file-alist (cons x (delq x file-alist))))
  17032. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  17033. (org-install-agenda-files-menu)
  17034. (message "File %s to %s of agenda file list"
  17035. (if had "moved" "added") (if to-end "end" "front"))))
  17036. (defun org-remove-file (&optional file)
  17037. "Remove current file from the list of files in variable `org-agenda-files'.
  17038. These are the files which are being checked for agenda entries.
  17039. Optional argument FILE means use this file instead of the current."
  17040. (interactive)
  17041. (let* ((org-agenda-skip-unavailable-files nil)
  17042. (file (or file buffer-file-name
  17043. (user-error "Current buffer does not visit a file")))
  17044. (true-file (file-truename file))
  17045. (afile (abbreviate-file-name file))
  17046. (files (delq nil (mapcar
  17047. (lambda (x)
  17048. (unless (equal true-file
  17049. (file-truename x))
  17050. x))
  17051. (org-agenda-files t)))))
  17052. (if (not (= (length files) (length (org-agenda-files t))))
  17053. (progn
  17054. (org-store-new-agenda-file-list files)
  17055. (org-install-agenda-files-menu)
  17056. (message "Removed from Org Agenda list: %s" afile))
  17057. (message "File was not in list: %s (not removed)" afile))))
  17058. (defun org-file-menu-entry (file)
  17059. (vector file (list 'find-file file) t))
  17060. (defun org-check-agenda-file (file)
  17061. "Make sure FILE exists. If not, ask user what to do."
  17062. (unless (file-exists-p file)
  17063. (message "Non-existent agenda file %s. [R]emove from list or [A]bort?"
  17064. (abbreviate-file-name file))
  17065. (let ((r (downcase (read-char-exclusive))))
  17066. (cond
  17067. ((equal r ?r)
  17068. (org-remove-file file)
  17069. (throw 'nextfile t))
  17070. (t (user-error "Abort"))))))
  17071. (defun org-get-agenda-file-buffer (file)
  17072. "Get an agenda buffer visiting FILE.
  17073. If the buffer needs to be created, add it to the list of buffers
  17074. which might be released later."
  17075. (let ((buf (org-find-base-buffer-visiting file)))
  17076. (if buf
  17077. buf ; just return it
  17078. ;; Make a new buffer and remember it
  17079. (setq buf (find-file-noselect file))
  17080. (when buf (push buf org-agenda-new-buffers))
  17081. buf)))
  17082. (defun org-release-buffers (blist)
  17083. "Release all buffers in list, asking the user for confirmation when needed.
  17084. When a buffer is unmodified, it is just killed. When modified, it is saved
  17085. \(if the user agrees) and then killed."
  17086. (let (file)
  17087. (dolist (buf blist)
  17088. (setq file (buffer-file-name buf))
  17089. (when (and (buffer-modified-p buf)
  17090. file
  17091. (y-or-n-p (format "Save file %s? " file)))
  17092. (with-current-buffer buf (save-buffer)))
  17093. (kill-buffer buf))))
  17094. (defun org-agenda-prepare-buffers (files)
  17095. "Create buffers for all agenda files, protect archived trees and comments."
  17096. (interactive)
  17097. (let ((pa '(:org-archived t))
  17098. (pc '(:org-comment t))
  17099. (pall '(:org-archived t :org-comment t))
  17100. (inhibit-read-only t)
  17101. (org-inhibit-startup org-agenda-inhibit-startup)
  17102. (rea (concat ":" org-archive-tag ":"))
  17103. re pos)
  17104. (setq org-tag-alist-for-agenda nil
  17105. org-tag-groups-alist-for-agenda nil)
  17106. (save-excursion
  17107. (save-restriction
  17108. (dolist (file files)
  17109. (catch 'nextfile
  17110. (if (bufferp file)
  17111. (set-buffer file)
  17112. (org-check-agenda-file file)
  17113. (set-buffer (org-get-agenda-file-buffer file)))
  17114. (widen)
  17115. (org-set-regexps-and-options 'tags-only)
  17116. (setq pos (point))
  17117. (or (memq 'category org-agenda-ignore-properties)
  17118. (org-refresh-category-properties))
  17119. (or (memq 'stats org-agenda-ignore-properties)
  17120. (org-refresh-stats-properties))
  17121. (or (memq 'effort org-agenda-ignore-properties)
  17122. (org-refresh-effort-properties))
  17123. (or (memq 'appt org-agenda-ignore-properties)
  17124. (org-refresh-properties "APPT_WARNTIME" 'org-appt-warntime))
  17125. (setq org-todo-keywords-for-agenda
  17126. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  17127. (setq org-done-keywords-for-agenda
  17128. (append org-done-keywords-for-agenda org-done-keywords))
  17129. (setq org-todo-keyword-alist-for-agenda
  17130. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  17131. (setq org-tag-alist-for-agenda
  17132. (org-uniquify
  17133. (append org-tag-alist-for-agenda
  17134. org-current-tag-alist)))
  17135. ;; Merge current file's tag groups into global
  17136. ;; `org-tag-groups-alist-for-agenda'.
  17137. (when org-group-tags
  17138. (dolist (alist org-tag-groups-alist)
  17139. (let ((old (assoc (car alist) org-tag-groups-alist-for-agenda)))
  17140. (if old
  17141. (setcdr old (org-uniquify (append (cdr old) (cdr alist))))
  17142. (push alist org-tag-groups-alist-for-agenda)))))
  17143. (org-with-silent-modifications
  17144. (save-excursion
  17145. (remove-text-properties (point-min) (point-max) pall)
  17146. (when org-agenda-skip-archived-trees
  17147. (goto-char (point-min))
  17148. (while (re-search-forward rea nil t)
  17149. (when (org-at-heading-p t)
  17150. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  17151. (goto-char (point-min))
  17152. (setq re (format "^\\*+ .*\\<%s\\>" org-comment-string))
  17153. (while (re-search-forward re nil t)
  17154. (when (save-match-data (org-in-commented-heading-p t))
  17155. (add-text-properties
  17156. (match-beginning 0) (org-end-of-subtree t) pc)))))
  17157. (goto-char pos)))))
  17158. (setq org-todo-keywords-for-agenda
  17159. (org-uniquify org-todo-keywords-for-agenda))
  17160. (setq org-todo-keyword-alist-for-agenda
  17161. (org-uniquify org-todo-keyword-alist-for-agenda))))
  17162. ;;;; CDLaTeX minor mode
  17163. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  17164. "Keymap for the minor `org-cdlatex-mode'.")
  17165. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  17166. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  17167. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  17168. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  17169. (org-defkey org-cdlatex-mode-map "\C-c{" 'org-cdlatex-environment-indent)
  17170. (defvar org-cdlatex-texmathp-advice-is-done nil
  17171. "Flag remembering if we have applied the advice to texmathp already.")
  17172. (define-minor-mode org-cdlatex-mode
  17173. "Toggle the minor `org-cdlatex-mode'.
  17174. This mode supports entering LaTeX environment and math in LaTeX fragments
  17175. in Org-mode.
  17176. \\{org-cdlatex-mode-map}"
  17177. nil " OCDL" nil
  17178. (when org-cdlatex-mode
  17179. (require 'cdlatex)
  17180. (run-hooks 'cdlatex-mode-hook)
  17181. (cdlatex-compute-tables))
  17182. (unless org-cdlatex-texmathp-advice-is-done
  17183. (setq org-cdlatex-texmathp-advice-is-done t)
  17184. (defadvice texmathp (around org-math-always-on activate)
  17185. "Always return t in org-mode buffers.
  17186. This is because we want to insert math symbols without dollars even outside
  17187. the LaTeX math segments. If Orgmode thinks that point is actually inside
  17188. an embedded LaTeX fragment, let texmathp do its job.
  17189. \\[org-cdlatex-mode-map]"
  17190. (interactive)
  17191. (let (p)
  17192. (cond
  17193. ((not (derived-mode-p 'org-mode)) ad-do-it)
  17194. ((eq this-command 'cdlatex-math-symbol)
  17195. (setq ad-return-value t
  17196. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  17197. (t
  17198. (let ((p (org-inside-LaTeX-fragment-p)))
  17199. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  17200. (setq ad-return-value t
  17201. texmathp-why '("Org-mode embedded math" . 0))
  17202. (when p ad-do-it)))))))))
  17203. (defun turn-on-org-cdlatex ()
  17204. "Unconditionally turn on `org-cdlatex-mode'."
  17205. (org-cdlatex-mode 1))
  17206. (defun org-try-cdlatex-tab ()
  17207. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  17208. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  17209. - inside a LaTeX fragment, or
  17210. - after the first word in a line, where an abbreviation expansion could
  17211. insert a LaTeX environment."
  17212. (when org-cdlatex-mode
  17213. (cond
  17214. ;; Before any word on the line: No expansion possible.
  17215. ((save-excursion (skip-chars-backward " \t") (bolp)) nil)
  17216. ;; Just after first word on the line: Expand it. Make sure it
  17217. ;; cannot happen on headlines, though.
  17218. ((save-excursion
  17219. (skip-chars-backward "a-zA-Z0-9*")
  17220. (skip-chars-backward " \t")
  17221. (and (bolp) (not (org-at-heading-p))))
  17222. (cdlatex-tab) t)
  17223. ((org-inside-LaTeX-fragment-p) (cdlatex-tab) t))))
  17224. (defun org-cdlatex-underscore-caret (&optional _arg)
  17225. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  17226. Revert to the normal definition outside of these fragments."
  17227. (interactive "P")
  17228. (if (org-inside-LaTeX-fragment-p)
  17229. (call-interactively 'cdlatex-sub-superscript)
  17230. (let (org-cdlatex-mode)
  17231. (call-interactively (key-binding (vector last-input-event))))))
  17232. (defun org-cdlatex-math-modify (&optional _arg)
  17233. "Execute `cdlatex-math-modify' in LaTeX fragments.
  17234. Revert to the normal definition outside of these fragments."
  17235. (interactive "P")
  17236. (if (org-inside-LaTeX-fragment-p)
  17237. (call-interactively 'cdlatex-math-modify)
  17238. (let (org-cdlatex-mode)
  17239. (call-interactively (key-binding (vector last-input-event))))))
  17240. (defun org-cdlatex-environment-indent (&optional environment item)
  17241. "Execute `cdlatex-environment' and indent the inserted environment.
  17242. ENVIRONMENT and ITEM are passed to `cdlatex-environment'.
  17243. The inserted environment is indented to current indentation
  17244. unless point is at the beginning of the line, in which the
  17245. environment remains unintended."
  17246. (interactive)
  17247. ;; cdlatex-environment always return nil. Therefore, capture output
  17248. ;; first and determine if an environment was selected.
  17249. (let* ((beg (point-marker))
  17250. (end (copy-marker (point) t))
  17251. (inserted (progn
  17252. (ignore-errors (cdlatex-environment environment item))
  17253. (< beg end)))
  17254. ;; Figure out how many lines to move forward after the
  17255. ;; environment has been inserted.
  17256. (lines (when inserted
  17257. (save-excursion
  17258. (- (cl-loop while (< beg (point))
  17259. with x = 0
  17260. do (forward-line -1)
  17261. (cl-incf x)
  17262. finally return x)
  17263. (if (progn (goto-char beg)
  17264. (and (progn (skip-chars-forward " \t") (eolp))
  17265. (progn (skip-chars-backward " \t") (bolp))))
  17266. 1 0)))))
  17267. (env (org-trim (delete-and-extract-region beg end))))
  17268. (when inserted
  17269. ;; Get indentation of next line unless at column 0.
  17270. (let ((ind (if (bolp) 0
  17271. (save-excursion
  17272. (org-return-indent)
  17273. (prog1 (org-get-indentation)
  17274. (when (progn (skip-chars-forward " \t") (eolp))
  17275. (delete-region beg (point)))))))
  17276. (bol (progn (skip-chars-backward " \t") (bolp))))
  17277. ;; Insert a newline before environment unless at column zero
  17278. ;; to "escape" the current line. Insert a newline if
  17279. ;; something is one the same line as \end{ENVIRONMENT}.
  17280. (insert
  17281. (concat (unless bol "\n") env
  17282. (when (and (skip-chars-forward " \t") (not (eolp))) "\n")))
  17283. (unless (zerop ind)
  17284. (save-excursion
  17285. (goto-char beg)
  17286. (while (< (point) end)
  17287. (unless (eolp) (org-indent-line-to ind))
  17288. (forward-line))))
  17289. (goto-char beg)
  17290. (forward-line lines)
  17291. (org-indent-line-to ind)))
  17292. (set-marker beg nil)
  17293. (set-marker end nil)))
  17294. ;;;; LaTeX fragments
  17295. (defun org-inside-LaTeX-fragment-p ()
  17296. "Test if point is inside a LaTeX fragment.
  17297. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  17298. sequence appearing also before point.
  17299. Even though the matchers for math are configurable, this function assumes
  17300. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  17301. delimiters are skipped when they have been removed by customization.
  17302. The return value is nil, or a cons cell with the delimiter and the
  17303. position of this delimiter.
  17304. This function does a reasonably good job, but can locally be fooled by
  17305. for example currency specifications. For example it will assume being in
  17306. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  17307. fragments that are properly closed, but during editing, we have to live
  17308. with the uncertainty caused by missing closing delimiters. This function
  17309. looks only before point, not after."
  17310. (catch 'exit
  17311. (let ((pos (point))
  17312. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  17313. (lim (progn
  17314. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  17315. (point)))
  17316. dd-on str (start 0) m re)
  17317. (goto-char pos)
  17318. (when dodollar
  17319. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  17320. re (nth 1 (assoc "$" org-latex-regexps)))
  17321. (while (string-match re str start)
  17322. (cond
  17323. ((= (match-end 0) (length str))
  17324. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  17325. ((= (match-end 0) (- (length str) 5))
  17326. (throw 'exit nil))
  17327. (t (setq start (match-end 0))))))
  17328. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  17329. (goto-char pos)
  17330. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  17331. (and (match-beginning 2) (throw 'exit nil))
  17332. ;; count $$
  17333. (while (re-search-backward "\\$\\$" lim t)
  17334. (setq dd-on (not dd-on)))
  17335. (goto-char pos)
  17336. (when dd-on (cons "$$" m))))))
  17337. (defun org-inside-latex-macro-p ()
  17338. "Is point inside a LaTeX macro or its arguments?"
  17339. (save-match-data
  17340. (org-in-regexp
  17341. "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")))
  17342. (defun org--format-latex-make-overlay (beg end image)
  17343. "Build an overlay between BEG and END using IMAGE file."
  17344. (let ((ov (make-overlay beg end)))
  17345. (overlay-put ov 'org-overlay-type 'org-latex-overlay)
  17346. (overlay-put ov 'evaporate t)
  17347. (overlay-put ov
  17348. 'modification-hooks
  17349. (list (lambda (o _flag _beg _end &optional _l)
  17350. (delete-overlay o))))
  17351. (if (featurep 'xemacs)
  17352. (progn
  17353. (overlay-put ov 'invisible t)
  17354. (overlay-put ov 'end-glyph (make-glyph (vector 'png :file image))))
  17355. (overlay-put ov
  17356. 'display
  17357. (list 'image :type 'png :file image :ascent 'center)))))
  17358. (defun org--list-latex-overlays (&optional beg end)
  17359. "List all Org LaTeX overlays in current buffer.
  17360. Limit to overlays between BEG and END when those are provided."
  17361. (cl-remove-if-not
  17362. (lambda (o) (eq (overlay-get o 'org-overlay-type) 'org-latex-overlay))
  17363. (overlays-in (or beg (point-min)) (or end (point-max)))))
  17364. (defun org-remove-latex-fragment-image-overlays (&optional beg end)
  17365. "Remove all overlays with LaTeX fragment images in current buffer.
  17366. When optional arguments BEG and END are non-nil, remove all
  17367. overlays between them instead. Return a non-nil value when some
  17368. overlays were removed, nil otherwise."
  17369. (let ((overlays (org--list-latex-overlays beg end)))
  17370. (mapc #'delete-overlay overlays)
  17371. overlays))
  17372. (define-obsolete-function-alias
  17373. 'org-preview-latex-fragment 'org-toggle-latex-fragment "24.4")
  17374. (defun org-toggle-latex-fragment (&optional arg)
  17375. "Preview the LaTeX fragment at point, or all locally or globally.
  17376. If the cursor is on a LaTeX fragment, create the image and overlay
  17377. it over the source code, if there is none. Remove it otherwise.
  17378. If there is no fragment at point, display all fragments in the
  17379. current section.
  17380. With prefix ARG, preview or clear image for all fragments in the
  17381. current subtree or in the whole buffer when used before the first
  17382. headline. With a double prefix ARG \\[universal-argument] \
  17383. \\[universal-argument] preview or clear images
  17384. for all fragments in the buffer."
  17385. (interactive "P")
  17386. (when (display-graphic-p)
  17387. (catch 'exit
  17388. (save-excursion
  17389. (let ((window-start (window-start)) msg)
  17390. (save-restriction
  17391. (cond
  17392. ((or (equal arg '(16))
  17393. (and (equal arg '(4))
  17394. (org-with-limited-levels (org-before-first-heading-p))))
  17395. (if (org-remove-latex-fragment-image-overlays)
  17396. (progn (message "LaTeX fragments images removed from buffer")
  17397. (throw 'exit nil))
  17398. (setq msg "Creating images for buffer...")))
  17399. ((equal arg '(4))
  17400. (org-with-limited-levels (org-back-to-heading t))
  17401. (let ((beg (point))
  17402. (end (progn (org-end-of-subtree t) (point))))
  17403. (if (org-remove-latex-fragment-image-overlays beg end)
  17404. (progn
  17405. (message "LaTeX fragment images removed from subtree")
  17406. (throw 'exit nil))
  17407. (setq msg "Creating images for subtree...")
  17408. (narrow-to-region beg end))))
  17409. ((let ((datum (org-element-context)))
  17410. (when (memq (org-element-type datum)
  17411. '(latex-environment latex-fragment))
  17412. (let* ((beg (org-element-property :begin datum))
  17413. (end (org-element-property :end datum)))
  17414. (if (org-remove-latex-fragment-image-overlays beg end)
  17415. (progn (message "LaTeX fragment image removed")
  17416. (throw 'exit nil))
  17417. (narrow-to-region beg end)
  17418. (setq msg "Creating image..."))))))
  17419. (t
  17420. (org-with-limited-levels
  17421. (let ((beg (if (org-at-heading-p) (line-beginning-position)
  17422. (outline-previous-heading)
  17423. (point)))
  17424. (end (progn (outline-next-heading) (point))))
  17425. (if (org-remove-latex-fragment-image-overlays beg end)
  17426. (progn
  17427. (message "LaTeX fragment images removed from section")
  17428. (throw 'exit nil))
  17429. (setq msg "Creating images for section...")
  17430. (narrow-to-region beg end))))))
  17431. (let ((file (buffer-file-name (buffer-base-buffer))))
  17432. (org-format-latex
  17433. (concat org-latex-preview-ltxpng-directory "org-ltxpng")
  17434. ;; Emacs cannot overlay images from remote hosts.
  17435. ;; Create it in `temporary-file-directory' instead.
  17436. (if (or (not file) (file-remote-p file))
  17437. temporary-file-directory
  17438. default-directory)
  17439. 'overlays msg 'forbuffer
  17440. org-latex-create-formula-image-program)))
  17441. ;; Work around a bug that doesn't restore window's start
  17442. ;; when widening back the buffer.
  17443. (set-window-start nil window-start)
  17444. (message (concat msg "done")))))))
  17445. (defun org-format-latex
  17446. (prefix &optional dir overlays msg forbuffer processing-type)
  17447. "Replace LaTeX fragments with links to an image, and produce images.
  17448. When optional argument OVERLAYS is non-nil, display the image on
  17449. top of the fragment instead of replacing it.
  17450. PROCESSING-TYPE is the conversion method to use, as a symbol.
  17451. Some of the options can be changed using the variable
  17452. `org-format-latex-options', which see."
  17453. (when (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  17454. (unless (eq processing-type 'verbatim)
  17455. (let* ((math-regexp "\\$\\|\\\\[([]\\|^[ \t]*\\\\begin{[A-Za-z0-9*]+}")
  17456. (cnt 0)
  17457. checkdir-flag)
  17458. (goto-char (point-min))
  17459. ;; Optimize overlay creation: (info "(elisp) Managing Overlays").
  17460. (when (and overlays (memq processing-type '(dvipng imagemagick)))
  17461. (overlay-recenter (point-max)))
  17462. (while (re-search-forward math-regexp nil t)
  17463. (unless (and overlays
  17464. (eq (get-char-property (point) 'org-overlay-type)
  17465. 'org-latex-overlay))
  17466. (let* ((context (org-element-context))
  17467. (type (org-element-type context)))
  17468. (when (memq type '(latex-environment latex-fragment))
  17469. (let ((block-type (eq type 'latex-environment))
  17470. (value (org-element-property :value context))
  17471. (beg (org-element-property :begin context))
  17472. (end (save-excursion
  17473. (goto-char (org-element-property :end context))
  17474. (skip-chars-backward " \r\t\n")
  17475. (point))))
  17476. (cl-case processing-type
  17477. (mathjax
  17478. ;; Prepare for MathJax processing.
  17479. (if (not (string-match "\\`\\$\\$?" value))
  17480. (goto-char end)
  17481. (delete-region beg end)
  17482. (if (string= (match-string 0 value) "$$")
  17483. (insert "\\[" (substring value 2 -2) "\\]")
  17484. (insert "\\(" (substring value 1 -1) "\\)"))))
  17485. ((dvipng imagemagick)
  17486. ;; Process to an image.
  17487. (cl-incf cnt)
  17488. (goto-char beg)
  17489. (let* ((face (face-at-point))
  17490. ;; Get the colors from the face at point.
  17491. (fg
  17492. (let ((color (plist-get org-format-latex-options
  17493. :foreground)))
  17494. (if (and forbuffer (eq color 'auto))
  17495. (face-attribute face :foreground nil 'default)
  17496. color)))
  17497. (bg
  17498. (let ((color (plist-get org-format-latex-options
  17499. :background)))
  17500. (if (and forbuffer (eq color 'auto))
  17501. (face-attribute face :background nil 'default)
  17502. color)))
  17503. (hash (sha1 (prin1-to-string
  17504. (list org-format-latex-header
  17505. org-latex-default-packages-alist
  17506. org-latex-packages-alist
  17507. org-format-latex-options
  17508. forbuffer value fg bg))))
  17509. (absprefix (expand-file-name prefix dir))
  17510. (linkfile (format "%s_%s.png" prefix hash))
  17511. (movefile (format "%s_%s.png" absprefix hash))
  17512. (sep (and block-type "\n\n"))
  17513. (link (concat sep "[[file:" linkfile "]]" sep))
  17514. (options
  17515. (org-combine-plists
  17516. org-format-latex-options
  17517. `(:foreground ,fg :background ,bg))))
  17518. (when msg (message msg cnt))
  17519. (unless checkdir-flag ; Ensure the directory exists.
  17520. (setq checkdir-flag t)
  17521. (let ((todir (file-name-directory absprefix)))
  17522. (unless (file-directory-p todir)
  17523. (make-directory todir t))))
  17524. (unless (file-exists-p movefile)
  17525. (org-create-formula-image
  17526. value movefile options forbuffer processing-type))
  17527. (if overlays
  17528. (progn
  17529. (dolist (o (overlays-in beg end))
  17530. (when (eq (overlay-get o 'org-overlay-type)
  17531. 'org-latex-overlay)
  17532. (delete-overlay o)))
  17533. (org--format-latex-make-overlay beg end movefile)
  17534. (goto-char end))
  17535. (delete-region beg end)
  17536. (insert
  17537. (org-add-props link
  17538. (list 'org-latex-src
  17539. (replace-regexp-in-string "\"" "" value)
  17540. 'org-latex-src-embed-type
  17541. (if block-type 'paragraph 'character)))))))
  17542. (mathml
  17543. ;; Process to MathML.
  17544. (unless (org-format-latex-mathml-available-p)
  17545. (user-error "LaTeX to MathML converter not configured"))
  17546. (cl-incf cnt)
  17547. (when msg (message msg cnt))
  17548. (goto-char beg)
  17549. (delete-region beg end)
  17550. (insert (org-format-latex-as-mathml
  17551. value block-type prefix dir)))
  17552. (otherwise
  17553. (error "Unknown conversion type %s for LaTeX fragments"
  17554. processing-type)))))))))))
  17555. (defun org-create-math-formula (latex-frag &optional mathml-file)
  17556. "Convert LATEX-FRAG to MathML and store it in MATHML-FILE.
  17557. Use `org-latex-to-mathml-convert-command'. If the conversion is
  17558. sucessful, return the portion between \"<math...> </math>\"
  17559. elements otherwise return nil. When MATHML-FILE is specified,
  17560. write the results in to that file. When invoked as an
  17561. interactive command, prompt for LATEX-FRAG, with initial value
  17562. set to the current active region and echo the results for user
  17563. inspection."
  17564. (interactive (list (let ((frag (when (org-region-active-p)
  17565. (buffer-substring-no-properties
  17566. (region-beginning) (region-end)))))
  17567. (read-string "LaTeX Fragment: " frag nil frag))))
  17568. (unless latex-frag (user-error "Invalid LaTeX fragment"))
  17569. (let* ((tmp-in-file
  17570. (let ((file (file-relative-name
  17571. (make-temp-name (expand-file-name "ltxmathml-in")))))
  17572. (write-region latex-frag nil file)
  17573. file))
  17574. (tmp-out-file (file-relative-name
  17575. (make-temp-name (expand-file-name "ltxmathml-out"))))
  17576. (cmd (format-spec
  17577. org-latex-to-mathml-convert-command
  17578. `((?j . ,(and org-latex-to-mathml-jar-file
  17579. (shell-quote-argument
  17580. (expand-file-name
  17581. org-latex-to-mathml-jar-file))))
  17582. (?I . ,(shell-quote-argument tmp-in-file))
  17583. (?i . ,latex-frag)
  17584. (?o . ,(shell-quote-argument tmp-out-file)))))
  17585. mathml shell-command-output)
  17586. (when (org-called-interactively-p 'any)
  17587. (unless (org-format-latex-mathml-available-p)
  17588. (user-error "LaTeX to MathML converter not configured")))
  17589. (message "Running %s" cmd)
  17590. (setq shell-command-output (shell-command-to-string cmd))
  17591. (setq mathml
  17592. (when (file-readable-p tmp-out-file)
  17593. (with-current-buffer (find-file-noselect tmp-out-file t)
  17594. (goto-char (point-min))
  17595. (when (re-search-forward
  17596. (concat
  17597. (regexp-quote
  17598. "<math xmlns=\"http://www.w3.org/1998/Math/MathML\"")
  17599. "[^>]*?>"
  17600. "\\(.\\|\n\\)*"
  17601. "</math>")
  17602. nil t)
  17603. (prog1 (match-string 0) (kill-buffer))))))
  17604. (cond
  17605. (mathml
  17606. (setq mathml
  17607. (concat "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" mathml))
  17608. (when mathml-file
  17609. (write-region mathml nil mathml-file))
  17610. (when (org-called-interactively-p 'any)
  17611. (message mathml)))
  17612. ((message "LaTeX to MathML conversion failed")
  17613. (message shell-command-output)))
  17614. (delete-file tmp-in-file)
  17615. (when (file-exists-p tmp-out-file)
  17616. (delete-file tmp-out-file))
  17617. mathml))
  17618. (defun org-format-latex-as-mathml (latex-frag latex-frag-type
  17619. prefix &optional dir)
  17620. "Use `org-create-math-formula' but check local cache first."
  17621. (let* ((absprefix (expand-file-name prefix dir))
  17622. (print-length nil) (print-level nil)
  17623. (formula-id (concat
  17624. "formula-"
  17625. (sha1
  17626. (prin1-to-string
  17627. (list latex-frag
  17628. org-latex-to-mathml-convert-command)))))
  17629. (formula-cache (format "%s-%s.mathml" absprefix formula-id))
  17630. (formula-cache-dir (file-name-directory formula-cache)))
  17631. (unless (file-directory-p formula-cache-dir)
  17632. (make-directory formula-cache-dir t))
  17633. (unless (file-exists-p formula-cache)
  17634. (org-create-math-formula latex-frag formula-cache))
  17635. (if (file-exists-p formula-cache)
  17636. ;; Successful conversion. Return the link to MathML file.
  17637. (org-add-props
  17638. (format "[[file:%s]]" (file-relative-name formula-cache dir))
  17639. (list 'org-latex-src (replace-regexp-in-string "\"" "" latex-frag)
  17640. 'org-latex-src-embed-type (if latex-frag-type
  17641. 'paragraph 'character)))
  17642. ;; Failed conversion. Return the LaTeX fragment verbatim
  17643. latex-frag)))
  17644. (defun org-create-formula-image (string tofile options buffer &optional type)
  17645. "Create an image from LaTeX source using dvipng or convert.
  17646. This function calls either `org-create-formula-image-with-dvipng'
  17647. or `org-create-formula-image-with-imagemagick' depending on the
  17648. value of `org-latex-create-formula-image-program' or on the value
  17649. of the optional TYPE variable.
  17650. Note: ultimately these two function should be combined as they
  17651. share a good deal of logic."
  17652. (org-check-external-command
  17653. "latex" "needed to convert LaTeX fragments to images")
  17654. (funcall
  17655. (cl-case (or type org-latex-create-formula-image-program)
  17656. (dvipng
  17657. (org-check-external-command
  17658. "dvipng" "needed to convert LaTeX fragments to images")
  17659. #'org-create-formula-image-with-dvipng)
  17660. (imagemagick
  17661. (org-check-external-command
  17662. "convert" "you need to install imagemagick")
  17663. #'org-create-formula-image-with-imagemagick)
  17664. (t (error
  17665. "Invalid value of `org-latex-create-formula-image-program'")))
  17666. string tofile options buffer))
  17667. (declare-function org-export-get-backend "ox" (name))
  17668. (declare-function org-export--get-global-options "ox" (&optional backend))
  17669. (declare-function org-export--get-inbuffer-options "ox" (&optional backend))
  17670. (declare-function org-latex-guess-inputenc "ox-latex" (header))
  17671. (declare-function org-latex-guess-babel-language "ox-latex" (header info))
  17672. (defun org-create-formula--latex-header ()
  17673. "Return LaTeX header appropriate for previewing a LaTeX snippet."
  17674. (let ((info (org-combine-plists (org-export--get-global-options
  17675. (org-export-get-backend 'latex))
  17676. (org-export--get-inbuffer-options
  17677. (org-export-get-backend 'latex)))))
  17678. (org-latex-guess-babel-language
  17679. (org-latex-guess-inputenc
  17680. (org-splice-latex-header
  17681. org-format-latex-header
  17682. org-latex-default-packages-alist
  17683. org-latex-packages-alist t
  17684. (plist-get info :latex-header)))
  17685. info)))
  17686. (defun org--get-display-dpi ()
  17687. "Get the DPI of the display.
  17688. Assumes that the display has the same pixel width in the
  17689. horizontal and vertical directions."
  17690. (if (display-graphic-p)
  17691. (round (/ (display-pixel-height)
  17692. (/ (display-mm-height) 25.4)))
  17693. (error "Attempt to calculate the dpi of a non-graphic display")))
  17694. ;; This function borrows from Ganesh Swami's latex2png.el
  17695. (defun org-create-formula-image-with-dvipng (string tofile options buffer)
  17696. "This calls dvipng."
  17697. (require 'ox-latex)
  17698. (let* ((tmpdir (if (featurep 'xemacs)
  17699. (temp-directory)
  17700. temporary-file-directory))
  17701. (texfilebase (make-temp-name
  17702. (expand-file-name "orgtex" tmpdir)))
  17703. (texfile (concat texfilebase ".tex"))
  17704. (dvifile (concat texfilebase ".dvi"))
  17705. (pngfile (concat texfilebase ".png"))
  17706. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  17707. ;; This assumes that the display has the same pixel width in
  17708. ;; the horizontal and vertical directions
  17709. (dpi (number-to-string (* scale (if buffer (org--get-display-dpi) 120))))
  17710. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  17711. "Black"))
  17712. (bg (or (plist-get options (if buffer :background :html-background))
  17713. "Transparent")))
  17714. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground))
  17715. (unless (string= fg "Transparent") (setq fg (org-dvipng-color-format fg))))
  17716. (if (eq bg 'default) (setq bg (org-dvipng-color :background))
  17717. (unless (string= bg "Transparent") (setq bg (org-dvipng-color-format bg))))
  17718. (let ((latex-header (org-create-formula--latex-header)))
  17719. (with-temp-file texfile
  17720. (insert latex-header)
  17721. (insert "\n\\begin{document}\n" string "\n\\end{document}\n")))
  17722. (let ((dir default-directory))
  17723. (ignore-errors
  17724. (cd tmpdir)
  17725. (call-process "latex" nil nil nil texfile))
  17726. (cd dir))
  17727. (if (not (file-exists-p dvifile))
  17728. (progn (message "Failed to create dvi file from %s" texfile) nil)
  17729. (ignore-errors
  17730. (if (featurep 'xemacs)
  17731. (call-process "dvipng" nil nil nil
  17732. "-fg" fg "-bg" bg
  17733. "-T" "tight"
  17734. "-o" pngfile
  17735. dvifile)
  17736. (call-process "dvipng" nil nil nil
  17737. "-fg" fg "-bg" bg
  17738. "-D" dpi
  17739. ;;"-x" scale "-y" scale
  17740. "-T" "tight"
  17741. "-o" pngfile
  17742. dvifile)))
  17743. (if (not (file-exists-p pngfile))
  17744. (if org-format-latex-signal-error
  17745. (error "Failed to create png file from %s" texfile)
  17746. (message "Failed to create png file from %s" texfile)
  17747. nil)
  17748. ;; Use the requested file name and clean up
  17749. (copy-file pngfile tofile 'replace)
  17750. (dolist (e '(".dvi" ".tex" ".aux" ".log" ".png" ".out"))
  17751. (when (file-exists-p (concat texfilebase e))
  17752. (delete-file (concat texfilebase e))))
  17753. pngfile))))
  17754. (declare-function org-latex-compile "ox-latex" (texfile &optional snippet))
  17755. (defun org-create-formula-image-with-imagemagick (string tofile options buffer)
  17756. "This calls convert, which is included into imagemagick."
  17757. (require 'ox-latex)
  17758. (let* ((tmpdir (if (featurep 'xemacs)
  17759. (temp-directory)
  17760. temporary-file-directory))
  17761. (texfilebase (make-temp-name
  17762. (expand-file-name "orgtex" tmpdir)))
  17763. (texfile (concat texfilebase ".tex"))
  17764. (pdffile (concat texfilebase ".pdf"))
  17765. (pngfile (concat texfilebase ".png"))
  17766. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  17767. (dpi (number-to-string (* scale (if buffer (org--get-display-dpi) 120))))
  17768. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  17769. "black"))
  17770. (bg (or (plist-get options (if buffer :background :html-background))
  17771. "white")))
  17772. (if (eq fg 'default) (setq fg (org-latex-color :foreground))
  17773. (setq fg (org-latex-color-format fg)))
  17774. (if (eq bg 'default) (setq bg (org-latex-color :background))
  17775. (setq bg (org-latex-color-format
  17776. (if (string= bg "Transparent") "white" bg))))
  17777. (let ((latex-header (org-create-formula--latex-header)))
  17778. (with-temp-file texfile
  17779. (insert latex-header)
  17780. (insert "\n\\begin{document}\n"
  17781. "\\definecolor{fg}{rgb}{" fg "}\n"
  17782. "\\definecolor{bg}{rgb}{" bg "}\n"
  17783. "\n\\pagecolor{bg}\n"
  17784. "\n{\\color{fg}\n"
  17785. string
  17786. "\n}\n"
  17787. "\n\\end{document}\n")))
  17788. (org-latex-compile texfile t)
  17789. (if (not (file-exists-p pdffile))
  17790. (progn (message "Failed to create pdf file from %s" texfile) nil)
  17791. (ignore-errors
  17792. (if (featurep 'xemacs)
  17793. (call-process "convert" nil nil nil
  17794. "-density" "96"
  17795. "-trim"
  17796. "-antialias"
  17797. pdffile
  17798. "-quality" "100"
  17799. ;; "-sharpen" "0x1.0"
  17800. pngfile)
  17801. (call-process "convert" nil nil nil
  17802. "-density" dpi
  17803. "-trim"
  17804. "-antialias"
  17805. pdffile
  17806. "-quality" "100"
  17807. ;; "-sharpen" "0x1.0"
  17808. pngfile)))
  17809. (if (not (file-exists-p pngfile))
  17810. (if org-format-latex-signal-error
  17811. (error "Failed to create png file from %s" texfile)
  17812. (message "Failed to create png file from %s" texfile)
  17813. nil)
  17814. ;; Use the requested file name and clean up
  17815. (copy-file pngfile tofile 'replace)
  17816. (dolist (e '(".pdf" ".tex" ".aux" ".log" ".png"))
  17817. (when (file-exists-p (concat texfilebase e))
  17818. (delete-file (concat texfilebase e))))
  17819. pngfile))))
  17820. (defun org-splice-latex-header (tpl def-pkg pkg snippets-p &optional extra)
  17821. "Fill a LaTeX header template TPL.
  17822. In the template, the following place holders will be recognized:
  17823. [DEFAULT-PACKAGES] \\usepackage statements for DEF-PKG
  17824. [NO-DEFAULT-PACKAGES] do not include DEF-PKG
  17825. [PACKAGES] \\usepackage statements for PKG
  17826. [NO-PACKAGES] do not include PKG
  17827. [EXTRA] the string EXTRA
  17828. [NO-EXTRA] do not include EXTRA
  17829. For backward compatibility, if both the positive and the negative place
  17830. holder is missing, the positive one (without the \"NO-\") will be
  17831. assumed to be present at the end of the template.
  17832. DEF-PKG and PKG are assumed to be alists of options/packagename lists.
  17833. EXTRA is a string.
  17834. SNIPPETS-P indicates if this is run to create snippet images for HTML."
  17835. (let (rpl (end ""))
  17836. (if (string-match "^[ \t]*\\[\\(NO-\\)?DEFAULT-PACKAGES\\][ \t]*\n?" tpl)
  17837. (setq rpl (if (or (match-end 1) (not def-pkg))
  17838. "" (org-latex-packages-to-string def-pkg snippets-p t))
  17839. tpl (replace-match rpl t t tpl))
  17840. (when def-pkg (setq end (org-latex-packages-to-string def-pkg snippets-p))))
  17841. (if (string-match "\\[\\(NO-\\)?PACKAGES\\][ \t]*\n?" tpl)
  17842. (setq rpl (if (or (match-end 1) (not pkg))
  17843. "" (org-latex-packages-to-string pkg snippets-p t))
  17844. tpl (replace-match rpl t t tpl))
  17845. (when pkg (setq end
  17846. (concat end "\n"
  17847. (org-latex-packages-to-string pkg snippets-p)))))
  17848. (if (string-match "\\[\\(NO-\\)?EXTRA\\][ \t]*\n?" tpl)
  17849. (setq rpl (if (or (match-end 1) (not extra))
  17850. "" (concat extra "\n"))
  17851. tpl (replace-match rpl t t tpl))
  17852. (when (and extra (string-match "\\S-" extra))
  17853. (setq end (concat end "\n" extra))))
  17854. (if (string-match "\\S-" end)
  17855. (concat tpl "\n" end)
  17856. tpl)))
  17857. (defun org-latex-packages-to-string (pkg &optional snippets-p newline)
  17858. "Turn an alist of packages into a string with the \\usepackage macros."
  17859. (setq pkg (mapconcat (lambda(p)
  17860. (cond
  17861. ((stringp p) p)
  17862. ((and snippets-p (>= (length p) 3) (not (nth 2 p)))
  17863. (format "%% Package %s omitted" (cadr p)))
  17864. ((equal "" (car p))
  17865. (format "\\usepackage{%s}" (cadr p)))
  17866. (t
  17867. (format "\\usepackage[%s]{%s}"
  17868. (car p) (cadr p)))))
  17869. pkg
  17870. "\n"))
  17871. (if newline (concat pkg "\n") pkg))
  17872. (defun org-dvipng-color (attr)
  17873. "Return a RGB color specification for dvipng."
  17874. (apply 'format "rgb %s %s %s"
  17875. (mapcar 'org-normalize-color
  17876. (if (featurep 'xemacs)
  17877. (color-rgb-components
  17878. (face-property 'default
  17879. (cond ((eq attr :foreground) 'foreground)
  17880. ((eq attr :background) 'background))))
  17881. (color-values (face-attribute 'default attr nil))))))
  17882. (defun org-dvipng-color-format (color-name)
  17883. "Convert COLOR-NAME to a RGB color value for dvipng."
  17884. (apply 'format "rgb %s %s %s"
  17885. (mapcar 'org-normalize-color
  17886. (color-values color-name))))
  17887. (defun org-latex-color (attr)
  17888. "Return a RGB color for the LaTeX color package."
  17889. (apply 'format "%s,%s,%s"
  17890. (mapcar 'org-normalize-color
  17891. (if (featurep 'xemacs)
  17892. (color-rgb-components
  17893. (face-property 'default
  17894. (cond ((eq attr :foreground) 'foreground)
  17895. ((eq attr :background) 'background))))
  17896. (color-values (face-attribute 'default attr nil))))))
  17897. (defun org-latex-color-format (color-name)
  17898. "Convert COLOR-NAME to a RGB color value."
  17899. (apply 'format "%s,%s,%s"
  17900. (mapcar 'org-normalize-color
  17901. (color-values color-name))))
  17902. (defun org-normalize-color (value)
  17903. "Return string to be used as color value for an RGB component."
  17904. (format "%g" (/ value 65535.0)))
  17905. ;; Image display
  17906. (defvar-local org-inline-image-overlays nil)
  17907. (defun org-toggle-inline-images (&optional include-linked)
  17908. "Toggle the display of inline images.
  17909. INCLUDE-LINKED is passed to `org-display-inline-images'."
  17910. (interactive "P")
  17911. (if org-inline-image-overlays
  17912. (progn
  17913. (org-remove-inline-images)
  17914. (when (org-called-interactively-p 'interactive)
  17915. (message "Inline image display turned off")))
  17916. (org-display-inline-images include-linked)
  17917. (when (org-called-interactively-p 'interactive)
  17918. (message (if org-inline-image-overlays
  17919. (format "%d images displayed inline"
  17920. (length org-inline-image-overlays))
  17921. "No images to display inline")))))
  17922. (defun org-redisplay-inline-images ()
  17923. "Refresh the display of inline images."
  17924. (interactive)
  17925. (if (not org-inline-image-overlays)
  17926. (org-toggle-inline-images)
  17927. (org-toggle-inline-images)
  17928. (org-toggle-inline-images)))
  17929. (defun org-display-inline-images (&optional include-linked refresh beg end)
  17930. "Display inline images.
  17931. An inline image is a link which follows either of these
  17932. conventions:
  17933. 1. Its path is a file with an extension matching return value
  17934. from `image-file-name-regexp' and it has no contents.
  17935. 2. Its description consists in a single link of the previous
  17936. type.
  17937. When optional argument INCLUDE-LINKED is non-nil, also links with
  17938. a text description part will be inlined. This can be nice for
  17939. a quick look at those images, but it does not reflect what
  17940. exported files will look like.
  17941. When optional argument REFRESH is non-nil, refresh existing
  17942. images between BEG and END. This will create new image displays
  17943. only if necessary. BEG and END default to the buffer
  17944. boundaries."
  17945. (interactive "P")
  17946. (when (display-graphic-p)
  17947. (unless refresh
  17948. (org-remove-inline-images)
  17949. (when (fboundp 'clear-image-cache) (clear-image-cache)))
  17950. (org-with-wide-buffer
  17951. (goto-char (or beg (point-min)))
  17952. (let ((case-fold-search t)
  17953. (file-extension-re (image-file-name-regexp)))
  17954. (while (re-search-forward "[][]\\[\\(?:file\\|[./~]\\)" end t)
  17955. (let ((link (save-match-data (org-element-context))))
  17956. ;; Check if we're at an inline image.
  17957. (when (and (equal (org-element-property :type link) "file")
  17958. (or include-linked
  17959. (not (org-element-property :contents-begin link)))
  17960. (let ((parent (org-element-property :parent link)))
  17961. (or (not (eq (org-element-type parent) 'link))
  17962. (not (cdr (org-element-contents parent)))))
  17963. (org-string-match-p file-extension-re
  17964. (org-element-property :path link)))
  17965. (let ((file (expand-file-name
  17966. (org-link-unescape
  17967. (org-element-property :path link)))))
  17968. (when (file-exists-p file)
  17969. (let ((width
  17970. ;; Apply `org-image-actual-width' specifications.
  17971. (cond
  17972. ((not (image-type-available-p 'imagemagick)) nil)
  17973. ((eq org-image-actual-width t) nil)
  17974. ((listp org-image-actual-width)
  17975. (or
  17976. ;; First try to find a width among
  17977. ;; attributes associated to the paragraph
  17978. ;; containing link.
  17979. (let ((paragraph
  17980. (let ((e link))
  17981. (while (and (setq e (org-element-property
  17982. :parent e))
  17983. (not (eq (org-element-type e)
  17984. 'paragraph))))
  17985. e)))
  17986. (when paragraph
  17987. (save-excursion
  17988. (goto-char (org-element-property :begin paragraph))
  17989. (when
  17990. (re-search-forward
  17991. "^[ \t]*#\\+attr_.*?: +.*?:width +\\(\\S-+\\)"
  17992. (org-element-property
  17993. :post-affiliated paragraph)
  17994. t)
  17995. (string-to-number (match-string 1))))))
  17996. ;; Otherwise, fall-back to provided number.
  17997. (car org-image-actual-width)))
  17998. ((numberp org-image-actual-width)
  17999. org-image-actual-width)))
  18000. (old (get-char-property-and-overlay
  18001. (org-element-property :begin link)
  18002. 'org-image-overlay)))
  18003. (if (and (car-safe old) refresh)
  18004. (image-refresh (overlay-get (cdr old) 'display))
  18005. (let ((image (create-image file
  18006. (and width 'imagemagick)
  18007. nil
  18008. :width width)))
  18009. (when image
  18010. (let* ((link
  18011. ;; If inline image is the description
  18012. ;; of another link, be sure to
  18013. ;; consider the latter as the one to
  18014. ;; apply the overlay on.
  18015. (let ((parent
  18016. (org-element-property :parent link)))
  18017. (if (eq (org-element-type parent) 'link)
  18018. parent
  18019. link)))
  18020. (ov (make-overlay
  18021. (org-element-property :begin link)
  18022. (progn
  18023. (goto-char
  18024. (org-element-property :end link))
  18025. (skip-chars-backward " \t")
  18026. (point)))))
  18027. (overlay-put ov 'display image)
  18028. (overlay-put ov 'face 'default)
  18029. (overlay-put ov 'org-image-overlay t)
  18030. (overlay-put
  18031. ov 'modification-hooks
  18032. (list 'org-display-inline-remove-overlay))
  18033. (push ov org-inline-image-overlays)))))))))))))))
  18034. (define-obsolete-function-alias
  18035. 'org-display-inline-modification-hook 'org-display-inline-remove-overlay "24.3")
  18036. (defun org-display-inline-remove-overlay (ov after _beg _end &optional _len)
  18037. "Remove inline-display overlay if a corresponding region is modified."
  18038. (let ((inhibit-modification-hooks t))
  18039. (when (and ov after)
  18040. (delete ov org-inline-image-overlays)
  18041. (delete-overlay ov))))
  18042. (defun org-remove-inline-images ()
  18043. "Remove inline display of images."
  18044. (interactive)
  18045. (mapc #'delete-overlay org-inline-image-overlays)
  18046. (setq org-inline-image-overlays nil))
  18047. ;;;; Key bindings
  18048. ;; Outline functions from `outline-mode-prefix-map'
  18049. ;; that can be remapped in Org:
  18050. (define-key org-mode-map [remap outline-mark-subtree] 'org-mark-subtree)
  18051. (define-key org-mode-map [remap outline-show-subtree] 'org-show-subtree)
  18052. (define-key org-mode-map [remap outline-forward-same-level]
  18053. 'org-forward-heading-same-level)
  18054. (define-key org-mode-map [remap outline-backward-same-level]
  18055. 'org-backward-heading-same-level)
  18056. (define-key org-mode-map [remap outline-show-branches]
  18057. 'org-kill-note-or-show-branches)
  18058. (define-key org-mode-map [remap outline-promote] 'org-promote-subtree)
  18059. (define-key org-mode-map [remap outline-demote] 'org-demote-subtree)
  18060. (define-key org-mode-map [remap outline-insert-heading] 'org-ctrl-c-ret)
  18061. (define-key org-mode-map [remap outline-next-visible-heading]
  18062. 'org-next-visible-heading)
  18063. (define-key org-mode-map [remap outline-previous-visible-heading]
  18064. 'org-previous-visible-heading)
  18065. (define-key org-mode-map [remap show-children] 'org-show-children)
  18066. ;; Outline functions from `outline-mode-prefix-map' that can not
  18067. ;; be remapped in Org:
  18068. ;; - the column "key binding" shows whether the Outline function is still
  18069. ;; available in Org mode on the same key that it has been bound to in
  18070. ;; Outline mode:
  18071. ;; - "overridden": key used for a different functionality in Org mode
  18072. ;; - else: key still bound to the same Outline function in Org mode
  18073. ;; | Outline function | key binding | Org replacement |
  18074. ;; |------------------------------------+-------------+--------------------------|
  18075. ;; | `outline-up-heading' | `C-c C-u' | still same function |
  18076. ;; | `outline-move-subtree-up' | overridden | better: org-shiftup |
  18077. ;; | `outline-move-subtree-down' | overridden | better: org-shiftdown |
  18078. ;; | `show-entry' | overridden | no replacement |
  18079. ;; | `show-branches' | `C-c C-k' | still same function |
  18080. ;; | `show-subtree' | overridden | visibility cycling |
  18081. ;; | `show-all' | overridden | no replacement |
  18082. ;; | `hide-subtree' | overridden | visibility cycling |
  18083. ;; | `hide-body' | overridden | no replacement |
  18084. ;; | `hide-entry' | overridden | visibility cycling |
  18085. ;; | `hide-leaves' | overridden | no replacement |
  18086. ;; | `hide-sublevels' | overridden | no replacement |
  18087. ;; | `hide-other' | overridden | no replacement |
  18088. ;; Make `C-c C-x' a prefix key
  18089. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  18090. ;; TAB key with modifiers
  18091. (org-defkey org-mode-map "\C-i" 'org-cycle)
  18092. (org-defkey org-mode-map [(tab)] 'org-cycle)
  18093. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  18094. (org-defkey org-mode-map "\M-\t" #'pcomplete)
  18095. ;; The following line is necessary under Suse GNU/Linux
  18096. (unless (featurep 'xemacs)
  18097. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  18098. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  18099. (define-key org-mode-map [backtab] 'org-shifttab)
  18100. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  18101. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  18102. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  18103. ;; Cursor keys with modifiers
  18104. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  18105. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  18106. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  18107. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  18108. (org-defkey org-mode-map [(control meta shift right)] 'org-increase-number-at-point)
  18109. (org-defkey org-mode-map [(control meta shift left)] 'org-decrease-number-at-point)
  18110. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  18111. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  18112. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  18113. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  18114. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  18115. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  18116. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  18117. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  18118. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  18119. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  18120. (org-defkey org-mode-map [(control shift up)] 'org-shiftcontrolup)
  18121. (org-defkey org-mode-map [(control shift down)] 'org-shiftcontroldown)
  18122. ;; Babel keys
  18123. (define-key org-mode-map org-babel-key-prefix org-babel-map)
  18124. (dolist (pair org-babel-key-bindings)
  18125. (define-key org-babel-map (car pair) (cdr pair)))
  18126. ;;; Extra keys for tty access.
  18127. ;; We only set them when really needed because otherwise the
  18128. ;; menus don't show the simple keys
  18129. (when (or org-use-extra-keys
  18130. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  18131. (not window-system))
  18132. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  18133. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  18134. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  18135. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  18136. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  18137. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  18138. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  18139. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  18140. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  18141. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  18142. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  18143. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  18144. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  18145. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  18146. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  18147. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  18148. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  18149. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  18150. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  18151. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  18152. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  18153. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  18154. (org-defkey org-mode-map [?\e (tab)] #'pcomplete)
  18155. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  18156. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  18157. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  18158. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  18159. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  18160. ;; All the other keys
  18161. (org-defkey org-mode-map "\C-c\C-a" 'outline-show-all) ; in case allout messed up.
  18162. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  18163. (if (boundp 'narrow-map)
  18164. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  18165. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  18166. (if (boundp 'narrow-map)
  18167. (org-defkey narrow-map "b" 'org-narrow-to-block)
  18168. (org-defkey org-mode-map "\C-xnb" 'org-narrow-to-block))
  18169. (if (boundp 'narrow-map)
  18170. (org-defkey narrow-map "e" 'org-narrow-to-element)
  18171. (org-defkey org-mode-map "\C-xne" 'org-narrow-to-element))
  18172. (org-defkey org-mode-map "\C-\M-t" 'org-transpose-element)
  18173. (org-defkey org-mode-map "\M-}" 'org-forward-element)
  18174. (org-defkey org-mode-map "\M-{" 'org-backward-element)
  18175. (org-defkey org-mode-map "\C-c\C-^" 'org-up-element)
  18176. (org-defkey org-mode-map "\C-c\C-_" 'org-down-element)
  18177. (org-defkey org-mode-map "\C-c\C-f" 'org-forward-heading-same-level)
  18178. (org-defkey org-mode-map "\C-c\C-b" 'org-backward-heading-same-level)
  18179. (org-defkey org-mode-map "\C-c\M-f" 'org-next-block)
  18180. (org-defkey org-mode-map "\C-c\M-b" 'org-previous-block)
  18181. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  18182. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-archive-subtree)
  18183. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
  18184. (org-defkey org-mode-map "\C-c\C-xd" 'org-insert-drawer)
  18185. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  18186. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  18187. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  18188. (org-defkey org-mode-map "\C-c\C-xq" 'org-toggle-tags-groups)
  18189. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  18190. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  18191. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  18192. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  18193. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  18194. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  18195. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  18196. (org-defkey org-mode-map "\C-c\M-w" 'org-copy)
  18197. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  18198. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  18199. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  18200. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  18201. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  18202. (org-defkey org-mode-map "\C-c\C-xv" 'org-copy-visible)
  18203. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  18204. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  18205. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  18206. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  18207. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  18208. (org-defkey org-mode-map "\C-c\M-l" 'org-insert-last-stored-link)
  18209. (org-defkey org-mode-map "\C-c\C-\M-l" 'org-insert-all-links)
  18210. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  18211. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  18212. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  18213. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  18214. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  18215. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  18216. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  18217. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  18218. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  18219. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  18220. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  18221. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  18222. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  18223. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  18224. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  18225. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  18226. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  18227. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  18228. (org-defkey org-mode-map "\C-c^" 'org-sort)
  18229. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  18230. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  18231. (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
  18232. (org-defkey org-mode-map [remap open-line] 'org-open-line)
  18233. (org-defkey org-mode-map [remap comment-dwim] 'org-comment-dwim)
  18234. (org-defkey org-mode-map [remap forward-paragraph] 'org-forward-paragraph)
  18235. (org-defkey org-mode-map [remap backward-paragraph] 'org-backward-paragraph)
  18236. (org-defkey org-mode-map "\M-^" 'org-delete-indentation)
  18237. (org-defkey org-mode-map "\C-m" 'org-return)
  18238. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  18239. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  18240. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  18241. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  18242. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  18243. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  18244. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  18245. (org-defkey org-mode-map "\C-c\"a" 'orgtbl-ascii-plot)
  18246. (org-defkey org-mode-map "\C-c\"g" 'org-plot/gnuplot)
  18247. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  18248. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  18249. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  18250. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  18251. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  18252. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  18253. (org-defkey org-mode-map "\C-c\C-e" 'org-export-dispatch)
  18254. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width)
  18255. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  18256. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  18257. (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
  18258. (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
  18259. (org-defkey org-mode-map "\C-c@" 'org-mark-subtree)
  18260. (org-defkey org-mode-map "\M-h" 'org-mark-element)
  18261. (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
  18262. ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
  18263. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  18264. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  18265. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  18266. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  18267. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  18268. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-in-last)
  18269. (org-defkey org-mode-map "\C-c\C-x\C-z" 'org-resolve-clocks)
  18270. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  18271. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  18272. (org-defkey org-mode-map "\C-c\C-x\C-q" 'org-clock-cancel)
  18273. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  18274. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  18275. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  18276. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-toggle-latex-fragment)
  18277. (org-defkey org-mode-map "\C-c\C-x\C-v" 'org-toggle-inline-images)
  18278. (org-defkey org-mode-map "\C-c\C-x\C-\M-v" 'org-redisplay-inline-images)
  18279. (org-defkey org-mode-map "\C-c\C-x\\" 'org-toggle-pretty-entities)
  18280. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  18281. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  18282. (org-defkey org-mode-map "\C-c\C-xP" 'org-set-property-and-value)
  18283. (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
  18284. (org-defkey org-mode-map "\C-c\C-xE" 'org-inc-effort)
  18285. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  18286. (org-defkey org-mode-map "\C-c\C-xi" 'org-columns-insert-dblock)
  18287. (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
  18288. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  18289. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  18290. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  18291. (org-defkey org-mode-map "\C-c\C-x_" 'org-timer-stop)
  18292. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  18293. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  18294. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  18295. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  18296. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  18297. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  18298. (when (featurep 'xemacs)
  18299. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  18300. (defconst org-speed-commands-default
  18301. '(
  18302. ("Outline Navigation")
  18303. ("n" . (org-speed-move-safe 'org-next-visible-heading))
  18304. ("p" . (org-speed-move-safe 'org-previous-visible-heading))
  18305. ("f" . (org-speed-move-safe 'org-forward-heading-same-level))
  18306. ("b" . (org-speed-move-safe 'org-backward-heading-same-level))
  18307. ("F" . org-next-block)
  18308. ("B" . org-previous-block)
  18309. ("u" . (org-speed-move-safe 'outline-up-heading))
  18310. ("j" . org-goto)
  18311. ("g" . (org-refile t))
  18312. ("Outline Visibility")
  18313. ("c" . org-cycle)
  18314. ("C" . org-shifttab)
  18315. (" " . org-display-outline-path)
  18316. ("s" . org-narrow-to-subtree)
  18317. ("=" . org-columns)
  18318. ("Outline Structure Editing")
  18319. ("U" . org-metaup)
  18320. ("D" . org-metadown)
  18321. ("r" . org-metaright)
  18322. ("l" . org-metaleft)
  18323. ("R" . org-shiftmetaright)
  18324. ("L" . org-shiftmetaleft)
  18325. ("i" . (progn (forward-char 1) (call-interactively
  18326. 'org-insert-heading-respect-content)))
  18327. ("^" . org-sort)
  18328. ("w" . org-refile)
  18329. ("a" . org-archive-subtree-default-with-confirmation)
  18330. ("@" . org-mark-subtree)
  18331. ("#" . org-toggle-comment)
  18332. ("Clock Commands")
  18333. ("I" . org-clock-in)
  18334. ("O" . org-clock-out)
  18335. ("Meta Data Editing")
  18336. ("t" . org-todo)
  18337. ("," . (org-priority))
  18338. ("0" . (org-priority ?\ ))
  18339. ("1" . (org-priority ?A))
  18340. ("2" . (org-priority ?B))
  18341. ("3" . (org-priority ?C))
  18342. (":" . org-set-tags-command)
  18343. ("e" . org-set-effort)
  18344. ("E" . org-inc-effort)
  18345. ("W" . (lambda(m) (interactive "sMinutes before warning: ")
  18346. (org-entry-put (point) "APPT_WARNTIME" m)))
  18347. ("Agenda Views etc")
  18348. ("v" . org-agenda)
  18349. ("/" . org-sparse-tree)
  18350. ("Misc")
  18351. ("o" . org-open-at-point)
  18352. ("?" . org-speed-command-help)
  18353. ("<" . (org-agenda-set-restriction-lock 'subtree))
  18354. (">" . (org-agenda-remove-restriction-lock))
  18355. )
  18356. "The default speed commands.")
  18357. (defun org-print-speed-command (e)
  18358. (if (> (length (car e)) 1)
  18359. (progn
  18360. (princ "\n")
  18361. (princ (car e))
  18362. (princ "\n")
  18363. (princ (make-string (length (car e)) ?-))
  18364. (princ "\n"))
  18365. (princ (car e))
  18366. (princ " ")
  18367. (if (symbolp (cdr e))
  18368. (princ (symbol-name (cdr e)))
  18369. (prin1 (cdr e)))
  18370. (princ "\n")))
  18371. (defun org-speed-command-help ()
  18372. "Show the available speed commands."
  18373. (interactive)
  18374. (if (not org-use-speed-commands)
  18375. (user-error "Speed commands are not activated, customize `org-use-speed-commands'")
  18376. (with-output-to-temp-buffer "*Help*"
  18377. (princ "User-defined Speed commands\n===========================\n")
  18378. (mapc #'org-print-speed-command org-speed-commands-user)
  18379. (princ "\n")
  18380. (princ "Built-in Speed commands\n=======================\n")
  18381. (mapc #'org-print-speed-command org-speed-commands-default))
  18382. (with-current-buffer "*Help*"
  18383. (setq truncate-lines t))))
  18384. (defun org-speed-move-safe (cmd)
  18385. "Execute CMD, but make sure that the cursor always ends up in a headline.
  18386. If not, return to the original position and throw an error."
  18387. (interactive)
  18388. (let ((pos (point)))
  18389. (call-interactively cmd)
  18390. (unless (and (bolp) (org-at-heading-p))
  18391. (goto-char pos)
  18392. (error "Boundary reached while executing %s" cmd))))
  18393. (defvar org-self-insert-command-undo-counter 0)
  18394. (defvar org-table-auto-blank-field) ; defined in org-table.el
  18395. (defvar org-speed-command nil)
  18396. (define-obsolete-function-alias
  18397. 'org-speed-command-default-hook 'org-speed-command-activate "24.3")
  18398. (defun org-speed-command-activate (keys)
  18399. "Hook for activating single-letter speed commands.
  18400. `org-speed-commands-default' specifies a minimal command set.
  18401. Use `org-speed-commands-user' for further customization."
  18402. (when (or (and (bolp) (looking-at org-outline-regexp))
  18403. (and (functionp org-use-speed-commands)
  18404. (funcall org-use-speed-commands)))
  18405. (cdr (assoc keys (append org-speed-commands-user
  18406. org-speed-commands-default)))))
  18407. (define-obsolete-function-alias
  18408. 'org-babel-speed-command-hook 'org-babel-speed-command-activate "24.3")
  18409. (defun org-babel-speed-command-activate (keys)
  18410. "Hook for activating single-letter code block commands."
  18411. (when (and (bolp) (looking-at org-babel-src-block-regexp))
  18412. (cdr (assoc keys org-babel-key-bindings))))
  18413. (defcustom org-speed-command-hook
  18414. '(org-speed-command-default-hook org-babel-speed-command-hook)
  18415. "Hook for activating speed commands at strategic locations.
  18416. Hook functions are called in sequence until a valid handler is
  18417. found.
  18418. Each hook takes a single argument, a user-pressed command key
  18419. which is also a `self-insert-command' from the global map.
  18420. Within the hook, examine the cursor position and the command key
  18421. and return nil or a valid handler as appropriate. Handler could
  18422. be one of an interactive command, a function, or a form.
  18423. Set `org-use-speed-commands' to non-nil value to enable this
  18424. hook. The default setting is `org-speed-command-activate'."
  18425. :group 'org-structure
  18426. :version "24.1"
  18427. :type 'hook)
  18428. (defun org-self-insert-command (N)
  18429. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  18430. If the cursor is in a table looking at whitespace, the whitespace is
  18431. overwritten, and the table is not marked as requiring realignment."
  18432. (interactive "p")
  18433. (org-check-before-invisible-edit 'insert)
  18434. (cond
  18435. ((and org-use-speed-commands
  18436. (let ((kv (this-command-keys-vector)))
  18437. (setq org-speed-command
  18438. (run-hook-with-args-until-success
  18439. 'org-speed-command-hook
  18440. (make-string 1 (aref kv (1- (length kv))))))))
  18441. (cond
  18442. ((commandp org-speed-command)
  18443. (setq this-command org-speed-command)
  18444. (call-interactively org-speed-command))
  18445. ((functionp org-speed-command)
  18446. (funcall org-speed-command))
  18447. ((and org-speed-command (listp org-speed-command))
  18448. (eval org-speed-command))
  18449. (t (let (org-use-speed-commands)
  18450. (call-interactively 'org-self-insert-command)))))
  18451. ((and
  18452. (org-at-table-p)
  18453. (progn
  18454. ;; Check if we blank the field, and if that triggers align.
  18455. (and (featurep 'org-table) org-table-auto-blank-field
  18456. (memq last-command
  18457. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
  18458. (if (or (eq (char-after) ?\s) (looking-at "[^|\n]* |"))
  18459. ;; Got extra space, this field does not determine
  18460. ;; column width.
  18461. (let (org-table-may-need-update) (org-table-blank-field))
  18462. ;; No extra space, this field may determine column
  18463. ;; width.
  18464. (org-table-blank-field)))
  18465. t)
  18466. (eq N 1)
  18467. (looking-at "[^|\n]* \\( \\)|"))
  18468. ;; There is room for insertion without re-aligning the table.
  18469. (delete-region (match-beginning 1) (match-end 1))
  18470. (self-insert-command N))
  18471. (t
  18472. (setq org-table-may-need-update t)
  18473. (self-insert-command N)
  18474. (org-fix-tags-on-the-fly)
  18475. (when org-self-insert-cluster-for-undo
  18476. (if (not (eq last-command 'org-self-insert-command))
  18477. (setq org-self-insert-command-undo-counter 1)
  18478. (if (>= org-self-insert-command-undo-counter 20)
  18479. (setq org-self-insert-command-undo-counter 1)
  18480. (and (> org-self-insert-command-undo-counter 0)
  18481. buffer-undo-list (listp buffer-undo-list)
  18482. (not (cadr buffer-undo-list)) ; remove nil entry
  18483. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  18484. (setq org-self-insert-command-undo-counter
  18485. (1+ org-self-insert-command-undo-counter))))))))
  18486. (defun org-check-before-invisible-edit (kind)
  18487. "Check is editing if kind KIND would be dangerous with invisible text around.
  18488. The detailed reaction depends on the user option `org-catch-invisible-edits'."
  18489. ;; First, try to get out of here as quickly as possible, to reduce overhead
  18490. (when (and org-catch-invisible-edits
  18491. (or (not (boundp 'visible-mode)) (not visible-mode))
  18492. (or (get-char-property (point) 'invisible)
  18493. (get-char-property (max (point-min) (1- (point))) 'invisible)))
  18494. ;; OK, we need to take a closer look
  18495. (let* ((invisible-at-point (get-char-property (point) 'invisible))
  18496. (invisible-before-point (unless (bobp) (get-char-property
  18497. (1- (point)) 'invisible)))
  18498. (border-and-ok-direction
  18499. (or
  18500. ;; Check if we are acting predictably before invisible text
  18501. (and invisible-at-point (not invisible-before-point)
  18502. (memq kind '(insert delete-backward)))
  18503. ;; Check if we are acting predictably after invisible text
  18504. ;; This works not well, and I have turned it off. It seems
  18505. ;; better to always show and stop after invisible text.
  18506. ;; (and (not invisible-at-point) invisible-before-point
  18507. ;; (memq kind '(insert delete)))
  18508. )))
  18509. (when (or (memq invisible-at-point '(outline org-hide-block t))
  18510. (memq invisible-before-point '(outline org-hide-block t)))
  18511. (when (eq org-catch-invisible-edits 'error)
  18512. (user-error "Editing in invisible areas is prohibited, make them visible first"))
  18513. (if (and org-custom-properties-overlays
  18514. (y-or-n-p "Display invisible properties in this buffer? "))
  18515. (org-toggle-custom-properties-visibility)
  18516. ;; Make the area visible
  18517. (save-excursion
  18518. (when invisible-before-point
  18519. (goto-char (previous-single-char-property-change
  18520. (point) 'invisible)))
  18521. (outline-show-subtree))
  18522. (cond
  18523. ((eq org-catch-invisible-edits 'show)
  18524. ;; That's it, we do the edit after showing
  18525. (message
  18526. "Unfolding invisible region around point before editing")
  18527. (sit-for 1))
  18528. ((and (eq org-catch-invisible-edits 'smart)
  18529. border-and-ok-direction)
  18530. (message "Unfolding invisible region around point before editing"))
  18531. (t
  18532. ;; Don't do the edit, make the user repeat it in full visibility
  18533. (user-error "Edit in invisible region aborted, repeat to confirm with text visible"))))))))
  18534. (defun org-fix-tags-on-the-fly ()
  18535. (when (and (equal (char-after (point-at-bol)) ?*)
  18536. (org-at-heading-p))
  18537. (org-align-tags-here org-tags-column)))
  18538. (defun org-delete-backward-char (N)
  18539. "Like `delete-backward-char', insert whitespace at field end in tables.
  18540. When deleting backwards, in tables this function will insert whitespace in
  18541. front of the next \"|\" separator, to keep the table aligned. The table will
  18542. still be marked for re-alignment if the field did fill the entire column,
  18543. because, in this case the deletion might narrow the column."
  18544. (interactive "p")
  18545. (save-match-data
  18546. (org-check-before-invisible-edit 'delete-backward)
  18547. (if (and (org-at-table-p)
  18548. (eq N 1)
  18549. (string-match "|" (buffer-substring (point-at-bol) (point)))
  18550. (looking-at ".*?|"))
  18551. (let ((pos (point))
  18552. (noalign (looking-at "[^|\n\r]* |"))
  18553. (c org-table-may-need-update))
  18554. (backward-delete-char N)
  18555. (unless overwrite-mode
  18556. (skip-chars-forward "^|")
  18557. (insert " ")
  18558. (goto-char (1- pos)))
  18559. ;; noalign: if there were two spaces at the end, this field
  18560. ;; does not determine the width of the column.
  18561. (when noalign (setq org-table-may-need-update c)))
  18562. (backward-delete-char N)
  18563. (org-fix-tags-on-the-fly))))
  18564. (defun org-delete-char (N)
  18565. "Like `delete-char', but insert whitespace at field end in tables.
  18566. When deleting characters, in tables this function will insert whitespace in
  18567. front of the next \"|\" separator, to keep the table aligned. The table will
  18568. still be marked for re-alignment if the field did fill the entire column,
  18569. because, in this case the deletion might narrow the column."
  18570. (interactive "p")
  18571. (save-match-data
  18572. (org-check-before-invisible-edit 'delete)
  18573. (if (and (org-at-table-p)
  18574. (not (bolp))
  18575. (not (= (char-after) ?|))
  18576. (eq N 1))
  18577. (if (looking-at ".*?|")
  18578. (let ((pos (point))
  18579. (noalign (looking-at "[^|\n\r]* |"))
  18580. (c org-table-may-need-update))
  18581. (replace-match
  18582. (concat (substring (match-string 0) 1 -1) " |") nil t)
  18583. (goto-char pos)
  18584. ;; noalign: if there were two spaces at the end, this field
  18585. ;; does not determine the width of the column.
  18586. (when noalign (setq org-table-may-need-update c)))
  18587. (delete-char N))
  18588. (delete-char N)
  18589. (org-fix-tags-on-the-fly))))
  18590. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  18591. (put 'org-self-insert-command 'delete-selection
  18592. (lambda ()
  18593. (not (run-hook-with-args-until-success
  18594. 'self-insert-uses-region-functions))))
  18595. (put 'orgtbl-self-insert-command 'delete-selection
  18596. (lambda ()
  18597. (not (run-hook-with-args-until-success
  18598. 'self-insert-uses-region-functions))))
  18599. (put 'org-delete-char 'delete-selection 'supersede)
  18600. (put 'org-delete-backward-char 'delete-selection 'supersede)
  18601. (put 'org-yank 'delete-selection 'yank)
  18602. ;; Make `flyspell-mode' delay after some commands
  18603. (put 'org-self-insert-command 'flyspell-delayed t)
  18604. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  18605. (put 'org-delete-char 'flyspell-delayed t)
  18606. (put 'org-delete-backward-char 'flyspell-delayed t)
  18607. ;; Make pabbrev-mode expand after org-mode commands
  18608. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  18609. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  18610. (defun org-remap (map &rest commands)
  18611. "In MAP, remap the functions given in COMMANDS.
  18612. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  18613. (let (new old)
  18614. (while commands
  18615. (setq old (pop commands) new (pop commands))
  18616. (if (fboundp 'command-remapping)
  18617. (org-defkey map (vector 'remap old) new)
  18618. (substitute-key-definition old new map global-map)))))
  18619. (defun org-transpose-words ()
  18620. "Transpose words for Org.
  18621. This uses the `org-mode-transpose-word-syntax-table' syntax
  18622. table, which interprets characters in `org-emphasis-alist' as
  18623. word constituents."
  18624. (interactive)
  18625. (with-syntax-table org-mode-transpose-word-syntax-table
  18626. (call-interactively 'transpose-words)))
  18627. (org-remap org-mode-map 'transpose-words 'org-transpose-words)
  18628. (when (eq org-enable-table-editor 'optimized)
  18629. ;; If the user wants maximum table support, we need to hijack
  18630. ;; some standard editing functions
  18631. (org-remap org-mode-map
  18632. 'self-insert-command 'org-self-insert-command
  18633. 'delete-char 'org-delete-char
  18634. 'delete-backward-char 'org-delete-backward-char)
  18635. (org-defkey org-mode-map "|" 'org-force-self-insert))
  18636. (defvar org-ctrl-c-ctrl-c-hook nil
  18637. "Hook for functions attaching themselves to `C-c C-c'.
  18638. This can be used to add additional functionality to the C-c C-c
  18639. key which executes context-dependent commands. This hook is run
  18640. before any other test, while `org-ctrl-c-ctrl-c-final-hook' is
  18641. run after the last test.
  18642. Each function will be called with no arguments. The function
  18643. must check if the context is appropriate for it to act. If yes,
  18644. it should do its thing and then return a non-nil value. If the
  18645. context is wrong, just do nothing and return nil.")
  18646. (defvar org-ctrl-c-ctrl-c-final-hook nil
  18647. "Hook for functions attaching themselves to `C-c C-c'.
  18648. This can be used to add additional functionality to the C-c C-c
  18649. key which executes context-dependent commands. This hook is run
  18650. after any other test, while `org-ctrl-c-ctrl-c-hook' is run
  18651. before the first test.
  18652. Each function will be called with no arguments. The function
  18653. must check if the context is appropriate for it to act. If yes,
  18654. it should do its thing and then return a non-nil value. If the
  18655. context is wrong, just do nothing and return nil.")
  18656. (defvar org-tab-first-hook nil
  18657. "Hook for functions to attach themselves to TAB.
  18658. See `org-ctrl-c-ctrl-c-hook' for more information.
  18659. This hook runs as the first action when TAB is pressed, even before
  18660. `org-cycle' messes around with the `outline-regexp' to cater for
  18661. inline tasks and plain list item folding.
  18662. If any function in this hook returns t, any other actions that
  18663. would have been caused by TAB (such as table field motion or visibility
  18664. cycling) will not occur.")
  18665. (defvar org-tab-after-check-for-table-hook nil
  18666. "Hook for functions to attach themselves to TAB.
  18667. See `org-ctrl-c-ctrl-c-hook' for more information.
  18668. This hook runs after it has been established that the cursor is not in a
  18669. table, but before checking if the cursor is in a headline or if global cycling
  18670. should be done.
  18671. If any function in this hook returns t, not other actions like visibility
  18672. cycling will be done.")
  18673. (defvar org-tab-after-check-for-cycling-hook nil
  18674. "Hook for functions to attach themselves to TAB.
  18675. See `org-ctrl-c-ctrl-c-hook' for more information.
  18676. This hook runs after it has been established that not table field motion and
  18677. not visibility should be done because of current context. This is probably
  18678. the place where a package like yasnippets can hook in.")
  18679. (defvar org-tab-before-tab-emulation-hook nil
  18680. "Hook for functions to attach themselves to TAB.
  18681. See `org-ctrl-c-ctrl-c-hook' for more information.
  18682. This hook runs after every other options for TAB have been exhausted, but
  18683. before indentation and \t insertion takes place.")
  18684. (defvar org-metaleft-hook nil
  18685. "Hook for functions attaching themselves to `M-left'.
  18686. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18687. (defvar org-metaright-hook nil
  18688. "Hook for functions attaching themselves to `M-right'.
  18689. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18690. (defvar org-metaup-hook nil
  18691. "Hook for functions attaching themselves to `M-up'.
  18692. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18693. (defvar org-metadown-hook nil
  18694. "Hook for functions attaching themselves to `M-down'.
  18695. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18696. (defvar org-shiftmetaleft-hook nil
  18697. "Hook for functions attaching themselves to `M-S-left'.
  18698. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18699. (defvar org-shiftmetaright-hook nil
  18700. "Hook for functions attaching themselves to `M-S-right'.
  18701. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18702. (defvar org-shiftmetaup-hook nil
  18703. "Hook for functions attaching themselves to `M-S-up'.
  18704. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18705. (defvar org-shiftmetadown-hook nil
  18706. "Hook for functions attaching themselves to `M-S-down'.
  18707. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18708. (defvar org-metareturn-hook nil
  18709. "Hook for functions attaching themselves to `M-RET'.
  18710. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18711. (defvar org-shiftup-hook nil
  18712. "Hook for functions attaching themselves to `S-up'.
  18713. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18714. (defvar org-shiftup-final-hook nil
  18715. "Hook for functions attaching themselves to `S-up'.
  18716. This one runs after all other options except shift-select have been excluded.
  18717. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18718. (defvar org-shiftdown-hook nil
  18719. "Hook for functions attaching themselves to `S-down'.
  18720. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18721. (defvar org-shiftdown-final-hook nil
  18722. "Hook for functions attaching themselves to `S-down'.
  18723. This one runs after all other options except shift-select have been excluded.
  18724. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18725. (defvar org-shiftleft-hook nil
  18726. "Hook for functions attaching themselves to `S-left'.
  18727. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18728. (defvar org-shiftleft-final-hook nil
  18729. "Hook for functions attaching themselves to `S-left'.
  18730. This one runs after all other options except shift-select have been excluded.
  18731. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18732. (defvar org-shiftright-hook nil
  18733. "Hook for functions attaching themselves to `S-right'.
  18734. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18735. (defvar org-shiftright-final-hook nil
  18736. "Hook for functions attaching themselves to `S-right'.
  18737. This one runs after all other options except shift-select have been excluded.
  18738. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18739. (defun org-modifier-cursor-error ()
  18740. "Throw an error, a modified cursor command was applied in wrong context."
  18741. (user-error "This command is active in special context like tables, headlines or items"))
  18742. (defun org-shiftselect-error ()
  18743. "Throw an error because Shift-Cursor command was applied in wrong context."
  18744. (if (and (boundp 'shift-select-mode) shift-select-mode)
  18745. (user-error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
  18746. (user-error "This command works only in special context like headlines or timestamps")))
  18747. (defun org-call-for-shift-select (cmd)
  18748. (let ((this-command-keys-shift-translated t))
  18749. (call-interactively cmd)))
  18750. (defun org-shifttab (&optional arg)
  18751. "Global visibility cycling or move to previous table field.
  18752. Call `org-table-previous-field' within a table.
  18753. When ARG is nil, cycle globally through visibility states.
  18754. When ARG is a numeric prefix, show contents of this level."
  18755. (interactive "P")
  18756. (cond
  18757. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  18758. ((integerp arg)
  18759. (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
  18760. (message "Content view to level: %d" arg)
  18761. (org-content (prefix-numeric-value arg2))
  18762. (org-cycle-show-empty-lines t)
  18763. (setq org-cycle-global-status 'overview)))
  18764. (t (call-interactively 'org-global-cycle))))
  18765. (defun org-shiftmetaleft ()
  18766. "Promote subtree or delete table column.
  18767. Calls `org-promote-subtree', `org-outdent-item-tree', or
  18768. `org-table-delete-column', depending on context. See the
  18769. individual commands for more information."
  18770. (interactive)
  18771. (cond
  18772. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  18773. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  18774. ((org-at-heading-p) (call-interactively 'org-promote-subtree))
  18775. ((if (not (org-region-active-p)) (org-at-item-p)
  18776. (save-excursion (goto-char (region-beginning))
  18777. (org-at-item-p)))
  18778. (call-interactively 'org-outdent-item-tree))
  18779. (t (org-modifier-cursor-error))))
  18780. (defun org-shiftmetaright ()
  18781. "Demote subtree or insert table column.
  18782. Calls `org-demote-subtree', `org-indent-item-tree', or
  18783. `org-table-insert-column', depending on context. See the
  18784. individual commands for more information."
  18785. (interactive)
  18786. (cond
  18787. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  18788. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  18789. ((org-at-heading-p) (call-interactively 'org-demote-subtree))
  18790. ((if (not (org-region-active-p)) (org-at-item-p)
  18791. (save-excursion (goto-char (region-beginning))
  18792. (org-at-item-p)))
  18793. (call-interactively 'org-indent-item-tree))
  18794. (t (org-modifier-cursor-error))))
  18795. (defun org-shiftmetaup (&optional _arg)
  18796. "Drag the line at point up.
  18797. In a table, kill the current row.
  18798. On a clock timestamp, update the value of the timestamp like `S-<up>'
  18799. but also adjust the previous clocked item in the clock history.
  18800. Everywhere else, drag the line at point up."
  18801. (interactive "P")
  18802. (cond
  18803. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  18804. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  18805. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  18806. (call-interactively 'org-timestamp-up)))
  18807. (t (call-interactively 'org-drag-line-backward))))
  18808. (defun org-shiftmetadown (&optional _arg)
  18809. "Drag the line at point down.
  18810. In a table, insert an empty row at the current line.
  18811. On a clock timestamp, update the value of the timestamp like `S-<down>'
  18812. but also adjust the previous clocked item in the clock history.
  18813. Everywhere else, drag the line at point down."
  18814. (interactive "P")
  18815. (cond
  18816. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  18817. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  18818. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  18819. (call-interactively 'org-timestamp-down)))
  18820. (t (call-interactively 'org-drag-line-forward))))
  18821. (defsubst org-hidden-tree-error ()
  18822. (user-error
  18823. "Hidden subtree, open with TAB or use subtree command M-S-<left>/<right>"))
  18824. (defun org-metaleft (&optional _arg)
  18825. "Promote heading, list item at point or move table column left.
  18826. Calls `org-do-promote', `org-outdent-item' or `org-table-move-column',
  18827. depending on context. With no specific context, calls the Emacs
  18828. default `backward-word'. See the individual commands for more
  18829. information.
  18830. This function runs the hook `org-metaleft-hook' as a first step,
  18831. and returns at first non-nil value."
  18832. (interactive "P")
  18833. (cond
  18834. ((run-hook-with-args-until-success 'org-metaleft-hook))
  18835. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  18836. ((org-with-limited-levels
  18837. (or (org-at-heading-p)
  18838. (and (org-region-active-p)
  18839. (save-excursion
  18840. (goto-char (region-beginning))
  18841. (org-at-heading-p)))))
  18842. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  18843. (call-interactively 'org-do-promote))
  18844. ;; At an inline task.
  18845. ((org-at-heading-p)
  18846. (call-interactively 'org-inlinetask-promote))
  18847. ((or (org-at-item-p)
  18848. (and (org-region-active-p)
  18849. (save-excursion
  18850. (goto-char (region-beginning))
  18851. (org-at-item-p))))
  18852. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  18853. (call-interactively 'org-outdent-item))
  18854. (t (call-interactively 'backward-word))))
  18855. (defun org-metaright (&optional _arg)
  18856. "Demote heading, list item at point or move table column right.
  18857. In front of a drawer or a block keyword, indent it correctly.
  18858. Calls `org-do-demote', `org-indent-item', `org-table-move-column',
  18859. `org-indnet-drawer' or `org-indent-block' depending on context.
  18860. With no specific context, calls the Emacs default `forward-word'.
  18861. See the individual commands for more information.
  18862. This function runs the hook `org-metaright-hook' as a first step,
  18863. and returns at first non-nil value."
  18864. (interactive "P")
  18865. (cond
  18866. ((run-hook-with-args-until-success 'org-metaright-hook))
  18867. ((org-at-table-p) (call-interactively 'org-table-move-column))
  18868. ((org-at-drawer-p) (call-interactively 'org-indent-drawer))
  18869. ((org-at-block-p) (call-interactively 'org-indent-block))
  18870. ((org-with-limited-levels
  18871. (or (org-at-heading-p)
  18872. (and (org-region-active-p)
  18873. (save-excursion
  18874. (goto-char (region-beginning))
  18875. (org-at-heading-p)))))
  18876. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  18877. (call-interactively 'org-do-demote))
  18878. ;; At an inline task.
  18879. ((org-at-heading-p)
  18880. (call-interactively 'org-inlinetask-demote))
  18881. ((or (org-at-item-p)
  18882. (and (org-region-active-p)
  18883. (save-excursion
  18884. (goto-char (region-beginning))
  18885. (org-at-item-p))))
  18886. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  18887. (call-interactively 'org-indent-item))
  18888. (t (call-interactively 'forward-word))))
  18889. (defun org-check-for-hidden (what)
  18890. "Check if there are hidden headlines/items in the current visual line.
  18891. WHAT can be either `headlines' or `items'. If the current line is
  18892. an outline or item heading and it has a folded subtree below it,
  18893. this function returns t, nil otherwise."
  18894. (let ((re (cond
  18895. ((eq what 'headlines) org-outline-regexp-bol)
  18896. ((eq what 'items) (org-item-beginning-re))
  18897. (t (error "This should not happen"))))
  18898. beg end)
  18899. (save-excursion
  18900. (catch 'exit
  18901. (unless (org-region-active-p)
  18902. (setq beg (point-at-bol))
  18903. (beginning-of-line 2)
  18904. (while (and (not (eobp)) ;; this is like `next-line'
  18905. (get-char-property (1- (point)) 'invisible))
  18906. (beginning-of-line 2))
  18907. (setq end (point))
  18908. (goto-char beg)
  18909. (goto-char (point-at-eol))
  18910. (setq end (max end (point)))
  18911. (while (re-search-forward re end t)
  18912. (when (get-char-property (match-beginning 0) 'invisible)
  18913. (throw 'exit t))))
  18914. nil))))
  18915. (defun org-metaup (&optional _arg)
  18916. "Move subtree up or move table row up.
  18917. Calls `org-move-subtree-up' or `org-table-move-row' or
  18918. `org-move-item-up', depending on context. See the individual commands
  18919. for more information."
  18920. (interactive "P")
  18921. (cond
  18922. ((run-hook-with-args-until-success 'org-metaup-hook))
  18923. ((org-region-active-p)
  18924. (let* ((a (min (region-beginning) (region-end)))
  18925. (b (1- (max (region-beginning) (region-end))))
  18926. (c (save-excursion (goto-char a)
  18927. (move-beginning-of-line 0)))
  18928. (d (save-excursion (goto-char a)
  18929. (move-end-of-line 0) (point))))
  18930. (transpose-regions a b c d)
  18931. (goto-char c)))
  18932. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  18933. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  18934. ((org-at-item-p) (call-interactively 'org-move-item-up))
  18935. (t (org-drag-element-backward))))
  18936. (defun org-metadown (&optional _arg)
  18937. "Move subtree down or move table row down.
  18938. Calls `org-move-subtree-down' or `org-table-move-row' or
  18939. `org-move-item-down', depending on context. See the individual
  18940. commands for more information."
  18941. (interactive "P")
  18942. (cond
  18943. ((run-hook-with-args-until-success 'org-metadown-hook))
  18944. ((org-region-active-p)
  18945. (let* ((a (min (region-beginning) (region-end)))
  18946. (b (max (region-beginning) (region-end)))
  18947. (c (save-excursion (goto-char b)
  18948. (move-beginning-of-line 1)))
  18949. (d (save-excursion (goto-char b)
  18950. (move-end-of-line 1) (1+ (point)))))
  18951. (transpose-regions a b c d)
  18952. (goto-char d)))
  18953. ((org-at-table-p) (call-interactively 'org-table-move-row))
  18954. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  18955. ((org-at-item-p) (call-interactively 'org-move-item-down))
  18956. (t (org-drag-element-forward))))
  18957. (defun org-shiftup (&optional arg)
  18958. "Increase item in timestamp or increase priority of current headline.
  18959. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  18960. depending on context. See the individual commands for more information."
  18961. (interactive "P")
  18962. (cond
  18963. ((run-hook-with-args-until-success 'org-shiftup-hook))
  18964. ((and org-support-shift-select (org-region-active-p))
  18965. (org-call-for-shift-select 'previous-line))
  18966. ((org-at-timestamp-p t)
  18967. (call-interactively (if org-edit-timestamp-down-means-later
  18968. 'org-timestamp-down 'org-timestamp-up)))
  18969. ((and (not (eq org-support-shift-select 'always))
  18970. org-enable-priority-commands
  18971. (org-at-heading-p))
  18972. (call-interactively 'org-priority-up))
  18973. ((and (not org-support-shift-select) (org-at-item-p))
  18974. (call-interactively 'org-previous-item))
  18975. ((org-clocktable-try-shift 'up arg))
  18976. ((run-hook-with-args-until-success 'org-shiftup-final-hook))
  18977. (org-support-shift-select
  18978. (org-call-for-shift-select 'previous-line))
  18979. (t (org-shiftselect-error))))
  18980. (defun org-shiftdown (&optional arg)
  18981. "Decrease item in timestamp or decrease priority of current headline.
  18982. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  18983. depending on context. See the individual commands for more information."
  18984. (interactive "P")
  18985. (cond
  18986. ((run-hook-with-args-until-success 'org-shiftdown-hook))
  18987. ((and org-support-shift-select (org-region-active-p))
  18988. (org-call-for-shift-select 'next-line))
  18989. ((org-at-timestamp-p t)
  18990. (call-interactively (if org-edit-timestamp-down-means-later
  18991. 'org-timestamp-up 'org-timestamp-down)))
  18992. ((and (not (eq org-support-shift-select 'always))
  18993. org-enable-priority-commands
  18994. (org-at-heading-p))
  18995. (call-interactively 'org-priority-down))
  18996. ((and (not org-support-shift-select) (org-at-item-p))
  18997. (call-interactively 'org-next-item))
  18998. ((org-clocktable-try-shift 'down arg))
  18999. ((run-hook-with-args-until-success 'org-shiftdown-final-hook))
  19000. (org-support-shift-select
  19001. (org-call-for-shift-select 'next-line))
  19002. (t (org-shiftselect-error))))
  19003. (defun org-shiftright (&optional arg)
  19004. "Cycle the thing at point or in the current line, depending on context.
  19005. Depending on context, this does one of the following:
  19006. - switch a timestamp at point one day into the future
  19007. - on a headline, switch to the next TODO keyword.
  19008. - on an item, switch entire list to the next bullet type
  19009. - on a property line, switch to the next allowed value
  19010. - on a clocktable definition line, move time block into the future"
  19011. (interactive "P")
  19012. (cond
  19013. ((run-hook-with-args-until-success 'org-shiftright-hook))
  19014. ((and org-support-shift-select (org-region-active-p))
  19015. (org-call-for-shift-select 'forward-char))
  19016. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  19017. ((and (not (eq org-support-shift-select 'always))
  19018. (org-at-heading-p))
  19019. (let ((org-inhibit-logging
  19020. (not org-treat-S-cursor-todo-selection-as-state-change))
  19021. (org-inhibit-blocking
  19022. (not org-treat-S-cursor-todo-selection-as-state-change)))
  19023. (org-call-with-arg 'org-todo 'right)))
  19024. ((or (and org-support-shift-select
  19025. (not (eq org-support-shift-select 'always))
  19026. (org-at-item-bullet-p))
  19027. (and (not org-support-shift-select) (org-at-item-p)))
  19028. (org-call-with-arg 'org-cycle-list-bullet nil))
  19029. ((and (not (eq org-support-shift-select 'always))
  19030. (org-at-property-p))
  19031. (call-interactively 'org-property-next-allowed-value))
  19032. ((org-clocktable-try-shift 'right arg))
  19033. ((run-hook-with-args-until-success 'org-shiftright-final-hook))
  19034. (org-support-shift-select
  19035. (org-call-for-shift-select 'forward-char))
  19036. (t (org-shiftselect-error))))
  19037. (defun org-shiftleft (&optional arg)
  19038. "Cycle the thing at point or in the current line, depending on context.
  19039. Depending on context, this does one of the following:
  19040. - switch a timestamp at point one day into the past
  19041. - on a headline, switch to the previous TODO keyword.
  19042. - on an item, switch entire list to the previous bullet type
  19043. - on a property line, switch to the previous allowed value
  19044. - on a clocktable definition line, move time block into the past"
  19045. (interactive "P")
  19046. (cond
  19047. ((run-hook-with-args-until-success 'org-shiftleft-hook))
  19048. ((and org-support-shift-select (org-region-active-p))
  19049. (org-call-for-shift-select 'backward-char))
  19050. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  19051. ((and (not (eq org-support-shift-select 'always))
  19052. (org-at-heading-p))
  19053. (let ((org-inhibit-logging
  19054. (not org-treat-S-cursor-todo-selection-as-state-change))
  19055. (org-inhibit-blocking
  19056. (not org-treat-S-cursor-todo-selection-as-state-change)))
  19057. (org-call-with-arg 'org-todo 'left)))
  19058. ((or (and org-support-shift-select
  19059. (not (eq org-support-shift-select 'always))
  19060. (org-at-item-bullet-p))
  19061. (and (not org-support-shift-select) (org-at-item-p)))
  19062. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  19063. ((and (not (eq org-support-shift-select 'always))
  19064. (org-at-property-p))
  19065. (call-interactively 'org-property-previous-allowed-value))
  19066. ((org-clocktable-try-shift 'left arg))
  19067. ((run-hook-with-args-until-success 'org-shiftleft-final-hook))
  19068. (org-support-shift-select
  19069. (org-call-for-shift-select 'backward-char))
  19070. (t (org-shiftselect-error))))
  19071. (defun org-shiftcontrolright ()
  19072. "Switch to next TODO set."
  19073. (interactive)
  19074. (cond
  19075. ((and org-support-shift-select (org-region-active-p))
  19076. (org-call-for-shift-select 'forward-word))
  19077. ((and (not (eq org-support-shift-select 'always))
  19078. (org-at-heading-p))
  19079. (org-call-with-arg 'org-todo 'nextset))
  19080. (org-support-shift-select
  19081. (org-call-for-shift-select 'forward-word))
  19082. (t (org-shiftselect-error))))
  19083. (defun org-shiftcontrolleft ()
  19084. "Switch to previous TODO set."
  19085. (interactive)
  19086. (cond
  19087. ((and org-support-shift-select (org-region-active-p))
  19088. (org-call-for-shift-select 'backward-word))
  19089. ((and (not (eq org-support-shift-select 'always))
  19090. (org-at-heading-p))
  19091. (org-call-with-arg 'org-todo 'previousset))
  19092. (org-support-shift-select
  19093. (org-call-for-shift-select 'backward-word))
  19094. (t (org-shiftselect-error))))
  19095. (defun org-shiftcontrolup (&optional n)
  19096. "Change timestamps synchronously up in CLOCK log lines.
  19097. Optional argument N tells to change by that many units."
  19098. (interactive "P")
  19099. (if (and (org-at-clock-log-p) (org-at-timestamp-p t))
  19100. (let (org-support-shift-select)
  19101. (org-clock-timestamps-up n))
  19102. (user-error "Not at a clock log")))
  19103. (defun org-shiftcontroldown (&optional n)
  19104. "Change timestamps synchronously down in CLOCK log lines.
  19105. Optional argument N tells to change by that many units."
  19106. (interactive "P")
  19107. (if (and (org-at-clock-log-p) (org-at-timestamp-p t))
  19108. (let (org-support-shift-select)
  19109. (org-clock-timestamps-down n))
  19110. (user-error "Not at a clock log")))
  19111. (defun org-increase-number-at-point (&optional inc)
  19112. "Increment the number at point.
  19113. With an optional prefix numeric argument INC, increment using
  19114. this numeric value."
  19115. (interactive "p")
  19116. (if (not (number-at-point))
  19117. (user-error "Not on a number")
  19118. (unless inc (setq inc 1))
  19119. (let ((pos (point))
  19120. (beg (skip-chars-backward "-+^/*0-9eE."))
  19121. (end (skip-chars-forward "-+^/*0-9eE^.")) nap)
  19122. (setq nap (buffer-substring-no-properties
  19123. (+ pos beg) (+ pos beg end)))
  19124. (delete-region (+ pos beg) (+ pos beg end))
  19125. (insert (calc-eval (concat (number-to-string inc) "+" nap))))
  19126. (when (org-at-table-p)
  19127. (org-table-align)
  19128. (org-table-end-of-field 1))))
  19129. (defun org-decrease-number-at-point (&optional inc)
  19130. "Decrement the number at point.
  19131. With an optional prefix numeric argument INC, decrement using
  19132. this numeric value."
  19133. (interactive "p")
  19134. (org-increase-number-at-point (- (or inc 1))))
  19135. (defun org-ctrl-c-ret ()
  19136. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  19137. (interactive)
  19138. (cond
  19139. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  19140. (t (call-interactively 'org-insert-heading))))
  19141. (defun org-find-visible ()
  19142. (let ((s (point)))
  19143. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  19144. (get-char-property s 'invisible)))
  19145. s))
  19146. (defun org-find-invisible ()
  19147. (let ((s (point)))
  19148. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  19149. (not (get-char-property s 'invisible))))
  19150. s))
  19151. (defun org-copy-visible (beg end)
  19152. "Copy the visible parts of the region."
  19153. (interactive "r")
  19154. (let (snippets s)
  19155. (save-excursion
  19156. (save-restriction
  19157. (narrow-to-region beg end)
  19158. (setq s (goto-char (point-min)))
  19159. (while (not (= (point) (point-max)))
  19160. (goto-char (org-find-invisible))
  19161. (push (buffer-substring s (point)) snippets)
  19162. (setq s (goto-char (org-find-visible))))))
  19163. (kill-new (apply 'concat (nreverse snippets)))))
  19164. (defun org-copy-special ()
  19165. "Copy region in table or copy current subtree.
  19166. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  19167. See the individual commands for more information."
  19168. (interactive)
  19169. (call-interactively
  19170. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  19171. (defun org-cut-special ()
  19172. "Cut region in table or cut current subtree.
  19173. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  19174. See the individual commands for more information."
  19175. (interactive)
  19176. (call-interactively
  19177. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  19178. (defun org-paste-special (arg)
  19179. "Paste rectangular region into table, or past subtree relative to level.
  19180. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  19181. See the individual commands for more information."
  19182. (interactive "P")
  19183. (if (org-at-table-p)
  19184. (org-table-paste-rectangle)
  19185. (org-paste-subtree arg)))
  19186. (defsubst org-in-fixed-width-region-p ()
  19187. "Is point in a fixed-width region?"
  19188. (save-match-data
  19189. (eq 'fixed-width (org-element-type (org-element-at-point)))))
  19190. (defun org-edit-special (&optional arg)
  19191. "Call a special editor for the element at point.
  19192. When at a table, call the formula editor with `org-table-edit-formulas'.
  19193. When in a source code block, call `org-edit-src-code'.
  19194. When in a fixed-width region, call `org-edit-fixed-width-region'.
  19195. When in an export block, call `org-edit-export-block'.
  19196. When at an #+INCLUDE keyword, visit the included file.
  19197. When at a footnote reference, call `org-edit-footnote-reference'
  19198. On a link, call `ffap' to visit the link at point.
  19199. Otherwise, return a user error."
  19200. (interactive "P")
  19201. (let ((element (org-element-at-point)))
  19202. (cl-assert (not buffer-read-only) nil
  19203. "Buffer is read-only: %s" (buffer-name))
  19204. (pcase (org-element-type element)
  19205. (`src-block
  19206. (if (not arg) (org-edit-src-code)
  19207. (let* ((info (org-babel-get-src-block-info))
  19208. (lang (nth 0 info))
  19209. (params (nth 2 info))
  19210. (session (cdr (assq :session params))))
  19211. (if (not session) (org-edit-src-code)
  19212. ;; At a src-block with a session and function called with
  19213. ;; an ARG: switch to the buffer related to the inferior
  19214. ;; process.
  19215. (switch-to-buffer
  19216. (funcall (intern (concat "org-babel-prep-session:" lang))
  19217. session params))))))
  19218. (`keyword
  19219. (if (member (org-element-property :key element) '("INCLUDE" "SETUPFILE"))
  19220. (org-open-link-from-string
  19221. (format "[[%s]]"
  19222. (expand-file-name
  19223. (let ((value (org-element-property :value element)))
  19224. (cond ((not (org-string-nw-p value))
  19225. (user-error "No file to edit"))
  19226. ((string-match "\\`\"\\(.*?\\)\"" value)
  19227. (match-string 1 value))
  19228. ((string-match "\\`[^ \t\"]\\S-*" value)
  19229. (match-string 0 value))
  19230. (t (user-error "No valid file specified")))))))
  19231. (user-error "No special environment to edit here")))
  19232. (`table
  19233. (if (eq (org-element-property :type element) 'table.el)
  19234. (org-edit-table.el)
  19235. (call-interactively 'org-table-edit-formulas)))
  19236. ;; Only Org tables contain `table-row' type elements.
  19237. (`table-row (call-interactively 'org-table-edit-formulas))
  19238. (`example-block (org-edit-src-code))
  19239. (`export-block (org-edit-export-block))
  19240. (`fixed-width (org-edit-fixed-width-region))
  19241. (_
  19242. ;; No notable element at point. Though, we may be at a link or
  19243. ;; a footnote reference, which are objects. Thus, scan deeper.
  19244. (let ((context (org-element-context element)))
  19245. (pcase (org-element-type context)
  19246. (`footnote-reference (org-edit-footnote-reference))
  19247. (`inline-src-block (org-edit-inline-src-code))
  19248. (`link (call-interactively #'ffap))
  19249. (_ (user-error "No special environment to edit here"))))))))
  19250. (defvar org-table-coordinate-overlays) ; defined in org-table.el
  19251. (defun org-ctrl-c-ctrl-c (&optional arg)
  19252. "Set tags in headline, or update according to changed information at point.
  19253. This command does many different things, depending on context:
  19254. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  19255. this is what we do.
  19256. - If the cursor is on a statistics cookie, update it.
  19257. - If the cursor is in a headline, prompt for tags and insert them
  19258. into the current line, aligned to `org-tags-column'. When called
  19259. with prefix arg, realign all tags in the current buffer.
  19260. - If the cursor is in one of the special #+KEYWORD lines, this
  19261. triggers scanning the buffer for these lines and updating the
  19262. information.
  19263. - If the cursor is inside a table, realign the table. This command
  19264. works even if the automatic table editor has been turned off.
  19265. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  19266. the entire table.
  19267. - If the cursor is at a footnote reference or definition, jump to
  19268. the corresponding definition or references, respectively.
  19269. - If the cursor is a the beginning of a dynamic block, update it.
  19270. - If the current buffer is a capture buffer, close note and file it.
  19271. - If the cursor is on a <<<target>>>, update radio targets and
  19272. corresponding links in this buffer.
  19273. - If the cursor is on a numbered item in a plain list, renumber the
  19274. ordered list.
  19275. - If the cursor is on a checkbox, toggle it.
  19276. - If the cursor is on a code block, evaluate it. The variable
  19277. `org-confirm-babel-evaluate' can be used to control prompting
  19278. before code block evaluation, by default every code block
  19279. evaluation requires confirmation. Code block evaluation can be
  19280. inhibited by setting `org-babel-no-eval-on-ctrl-c-ctrl-c'."
  19281. (interactive "P")
  19282. (cond
  19283. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  19284. org-occur-highlights)
  19285. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  19286. (org-remove-occur-highlights)
  19287. (message "Temporary highlights/overlays removed from current buffer"))
  19288. ((and (local-variable-p 'org-finish-function (current-buffer))
  19289. (fboundp org-finish-function))
  19290. (funcall org-finish-function))
  19291. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  19292. (t
  19293. (if (save-excursion (beginning-of-line) (looking-at "[ \t]*$"))
  19294. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  19295. (user-error "C-c C-c can do nothing useful at this location"))
  19296. (let* ((context (org-element-context))
  19297. (type (org-element-type context)))
  19298. (cl-case type
  19299. ;; When at a link, act according to the parent instead.
  19300. (link (setq context (org-element-property :parent context))
  19301. (setq type (org-element-type context)))
  19302. ;; Unsupported object types: refer to the first supported
  19303. ;; element or object containing it.
  19304. ((bold code entity export-snippet inline-babel-call inline-src-block
  19305. italic latex-fragment line-break macro strike-through subscript
  19306. superscript underline verbatim)
  19307. (setq context
  19308. (org-element-lineage
  19309. context '(radio-target paragraph verse-block table-cell)))))
  19310. ;; For convenience: at the first line of a paragraph on the
  19311. ;; same line as an item, apply function on that item instead.
  19312. (when (eq type 'paragraph)
  19313. (let ((parent (org-element-property :parent context)))
  19314. (when (and (eq (org-element-type parent) 'item)
  19315. (= (line-beginning-position)
  19316. (org-element-property :begin parent)))
  19317. (setq context parent type 'item))))
  19318. ;; Act according to type of element or object at point.
  19319. (cl-case type
  19320. (clock (org-clock-update-time-maybe))
  19321. (dynamic-block
  19322. (save-excursion
  19323. (goto-char (org-element-property :post-affiliated context))
  19324. (org-update-dblock)))
  19325. (footnote-definition
  19326. (goto-char (org-element-property :post-affiliated context))
  19327. (call-interactively 'org-footnote-action))
  19328. (footnote-reference (call-interactively 'org-footnote-action))
  19329. ((headline inlinetask)
  19330. (save-excursion (goto-char (org-element-property :begin context))
  19331. (call-interactively 'org-set-tags)))
  19332. (item
  19333. ;; At an item: a double C-u set checkbox to "[-]"
  19334. ;; unconditionally, whereas a single one will toggle its
  19335. ;; presence. Without a universal argument, if the item
  19336. ;; has a checkbox, toggle it. Otherwise repair the list.
  19337. (let* ((box (org-element-property :checkbox context))
  19338. (struct (org-element-property :structure context))
  19339. (old-struct (copy-tree struct))
  19340. (parents (org-list-parents-alist struct))
  19341. (prevs (org-list-prevs-alist struct))
  19342. (orderedp (org-not-nil (org-entry-get nil "ORDERED"))))
  19343. (org-list-set-checkbox
  19344. (org-element-property :begin context) struct
  19345. (cond ((equal arg '(16)) "[-]")
  19346. ((and (not box) (equal arg '(4))) "[ ]")
  19347. ((or (not box) (equal arg '(4))) nil)
  19348. ((eq box 'on) "[ ]")
  19349. (t "[X]")))
  19350. ;; Mimic `org-list-write-struct' but with grabbing
  19351. ;; a return value from `org-list-struct-fix-box'.
  19352. (org-list-struct-fix-ind struct parents 2)
  19353. (org-list-struct-fix-item-end struct)
  19354. (org-list-struct-fix-bul struct prevs)
  19355. (org-list-struct-fix-ind struct parents)
  19356. (let ((block-item
  19357. (org-list-struct-fix-box struct parents prevs orderedp)))
  19358. (if (and box (equal struct old-struct))
  19359. (if (equal arg '(16))
  19360. (message "Checkboxes already reset")
  19361. (user-error "Cannot toggle this checkbox: %s"
  19362. (if (eq box 'on)
  19363. "all subitems checked"
  19364. "unchecked subitems")))
  19365. (org-list-struct-apply-struct struct old-struct)
  19366. (org-update-checkbox-count-maybe))
  19367. (when block-item
  19368. (message "Checkboxes were removed due to empty box at line %d"
  19369. (org-current-line block-item))))))
  19370. (keyword
  19371. (let ((org-inhibit-startup-visibility-stuff t)
  19372. (org-startup-align-all-tables nil))
  19373. (when (boundp 'org-table-coordinate-overlays)
  19374. (mapc #'delete-overlay org-table-coordinate-overlays)
  19375. (setq org-table-coordinate-overlays nil))
  19376. (org-save-outline-visibility 'use-markers (org-mode-restart)))
  19377. (message "Local setup has been refreshed"))
  19378. (plain-list
  19379. ;; At a plain list, with a double C-u argument, set
  19380. ;; checkboxes of each item to "[-]", whereas a single one
  19381. ;; will toggle their presence according to the state of the
  19382. ;; first item in the list. Without an argument, repair the
  19383. ;; list.
  19384. (let* ((begin (org-element-property :contents-begin context))
  19385. (beginm (move-marker (make-marker) begin))
  19386. (struct (org-element-property :structure context))
  19387. (old-struct (copy-tree struct))
  19388. (first-box (save-excursion
  19389. (goto-char begin)
  19390. (looking-at org-list-full-item-re)
  19391. (match-string-no-properties 3)))
  19392. (new-box (cond ((equal arg '(16)) "[-]")
  19393. ((equal arg '(4)) (unless first-box "[ ]"))
  19394. ((equal first-box "[X]") "[ ]")
  19395. (t "[X]"))))
  19396. (cond
  19397. (arg
  19398. (dolist (pos
  19399. (org-list-get-all-items
  19400. begin struct (org-list-prevs-alist struct)))
  19401. (org-list-set-checkbox pos struct new-box)))
  19402. ((and first-box (eq (point) begin))
  19403. ;; For convenience, when point is at bol on the first
  19404. ;; item of the list and no argument is provided, simply
  19405. ;; toggle checkbox of that item, if any.
  19406. (org-list-set-checkbox begin struct new-box)))
  19407. (org-list-write-struct
  19408. struct (org-list-parents-alist struct) old-struct)
  19409. (org-update-checkbox-count-maybe)
  19410. (save-excursion (goto-char beginm) (org-list-send-list 'maybe))))
  19411. ((property-drawer node-property)
  19412. (call-interactively 'org-property-action))
  19413. ((radio-target target)
  19414. (call-interactively 'org-update-radio-target-regexp))
  19415. (statistics-cookie
  19416. (call-interactively 'org-update-statistics-cookies))
  19417. ((table table-cell table-row)
  19418. ;; At a table, recalculate every field and align it. Also
  19419. ;; send the table if necessary. If the table has
  19420. ;; a `table.el' type, just give up. At a table row or
  19421. ;; cell, maybe recalculate line but always align table.
  19422. (if (eq (org-element-property :type context) 'table.el)
  19423. (message "%s" (substitute-command-keys "\\<org-mode-map>\
  19424. Use \\[org-edit-special] to edit table.el tables"))
  19425. (let ((org-enable-table-editor t))
  19426. (if (or (eq type 'table)
  19427. ;; Check if point is at a TBLFM line.
  19428. (and (eq type 'table-row)
  19429. (= (point) (org-element-property :end context))))
  19430. (save-excursion
  19431. (if (org-at-TBLFM-p)
  19432. (progn (require 'org-table)
  19433. (org-table-calc-current-TBLFM))
  19434. (goto-char (org-element-property :contents-begin context))
  19435. (org-call-with-arg 'org-table-recalculate (or arg t))
  19436. (orgtbl-send-table 'maybe)))
  19437. (org-table-maybe-eval-formula)
  19438. (cond (arg (call-interactively 'org-table-recalculate))
  19439. ((org-table-maybe-recalculate-line))
  19440. (t (org-table-align)))))))
  19441. (timestamp (org-timestamp-change 0 'day))
  19442. (otherwise
  19443. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  19444. (user-error
  19445. "C-c C-c can do nothing useful at this location")))))))))
  19446. (defun org-mode-restart ()
  19447. (interactive)
  19448. (let ((indent-status (org-bound-and-true-p org-indent-mode)))
  19449. (funcall major-mode)
  19450. (hack-local-variables)
  19451. (when (and indent-status (not (org-bound-and-true-p org-indent-mode)))
  19452. (org-indent-mode -1)))
  19453. (message "%s restarted" major-mode))
  19454. (defun org-kill-note-or-show-branches ()
  19455. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  19456. (interactive)
  19457. (if (not org-finish-function)
  19458. (progn
  19459. (outline-hide-subtree)
  19460. (call-interactively 'outline-show-branches))
  19461. (let ((org-note-abort t))
  19462. (funcall org-finish-function))))
  19463. (defun org-delete-indentation (&optional ARG)
  19464. "Join current line to previous and fix whitespace at join.
  19465. If previous line is a headline add to headline title. Otherwise
  19466. the function calls `delete-indentation'.
  19467. With argument, join this line to following line."
  19468. (interactive "*P")
  19469. (if (save-excursion
  19470. (if ARG (beginning-of-line)
  19471. (forward-line -1))
  19472. (looking-at org-complex-heading-regexp))
  19473. ;; At headline.
  19474. (let ((tags-column (when (match-beginning 5)
  19475. (save-excursion (goto-char (match-beginning 5))
  19476. (current-column))))
  19477. (string (concat " " (progn (when ARG (forward-line 1))
  19478. (org-trim (delete-and-extract-region
  19479. (line-beginning-position)
  19480. (line-end-position)))))))
  19481. (unless (bobp) (delete-region (point) (1- (point))))
  19482. (goto-char (or (match-end 4)
  19483. (match-beginning 5)
  19484. (match-end 0)))
  19485. (skip-chars-backward " \t")
  19486. (save-excursion (insert string))
  19487. ;; Adjust alignment of tags.
  19488. (when tags-column
  19489. (org-align-tags-here (if org-auto-align-tags
  19490. org-tags-column
  19491. tags-column))))
  19492. (delete-indentation ARG)))
  19493. (defun org-open-line (n)
  19494. "Insert a new row in tables, call `open-line' elsewhere.
  19495. If `org-special-ctrl-o' is nil, just call `open-line' everywhere."
  19496. (interactive "*p")
  19497. (cond
  19498. ((not org-special-ctrl-o)
  19499. (open-line n))
  19500. ((org-at-table-p)
  19501. (org-table-insert-row))
  19502. (t
  19503. (open-line n))))
  19504. (defun org-return (&optional indent)
  19505. "Goto next table row or insert a newline.
  19506. Calls `org-table-next-row' or `newline', depending on context.
  19507. When optional INDENT argument is non-nil, call
  19508. `newline-and-indent' instead of `newline'.
  19509. When `org-return-follows-link' is non-nil and point is on
  19510. a timestamp or a link, call `org-open-at-point'. However, it
  19511. will not happen if point is in a table or on a \"dead\"
  19512. object (e.g., within a comment). In these case, you need to use
  19513. `org-open-at-point' directly."
  19514. (interactive)
  19515. (let ((context (if org-return-follows-link (org-element-context)
  19516. (org-element-at-point))))
  19517. (cond
  19518. ;; In a table, call `org-table-next-row'.
  19519. ((or (and (eq (org-element-type context) 'table)
  19520. (>= (point) (org-element-property :contents-begin context))
  19521. (< (point) (org-element-property :contents-end context)))
  19522. (org-element-lineage context '(table-row table-cell) t))
  19523. (org-table-justify-field-maybe)
  19524. (call-interactively #'org-table-next-row))
  19525. ;; On a link or a timestamp, call `org-open-at-point' if
  19526. ;; `org-return-follows-link' allows it. Tolerate fuzzy
  19527. ;; locations, e.g., in a comment, as `org-open-at-point'.
  19528. ((and org-return-follows-link
  19529. (or (org-in-regexp org-ts-regexp-both nil t)
  19530. (org-in-regexp org-tsr-regexp-both nil t)
  19531. (org-in-regexp org-any-link-re nil t)))
  19532. (call-interactively #'org-open-at-point))
  19533. ;; Insert newline in heading, but preserve tags.
  19534. ((and (not (bolp))
  19535. (save-excursion (beginning-of-line)
  19536. (looking-at org-complex-heading-regexp)))
  19537. ;; At headline. Split line. However, if point is on keyword,
  19538. ;; priority cookie or tags, do not break any of them: add
  19539. ;; a newline after the headline instead.
  19540. (let ((tags-column (and (match-beginning 5)
  19541. (save-excursion (goto-char (match-beginning 5))
  19542. (current-column))))
  19543. (string
  19544. (when (and (match-end 4)
  19545. (>= (point)
  19546. (or (match-end 3) (match-end 2) (1+ (match-end 1))))
  19547. (<= (point) (match-end 4)))
  19548. (delete-and-extract-region (point) (match-end 4)))))
  19549. (when (and tags-column string) ; Adjust tag alignment.
  19550. (org-align-tags-here
  19551. (if org-auto-align-tags org-tags-column tags-column)))
  19552. (end-of-line)
  19553. (org-show-entry)
  19554. (if indent (newline-and-indent) (newline))
  19555. (when string (save-excursion (insert (org-trim string))))))
  19556. ;; In a list, make sure indenting keeps trailing text within.
  19557. ((and indent
  19558. (not (eolp))
  19559. (org-element-lineage context '(item)))
  19560. (let ((trailing-data
  19561. (delete-and-extract-region (point) (line-end-position))))
  19562. (newline-and-indent)
  19563. (save-excursion (insert trailing-data))))
  19564. (t (if indent (newline-and-indent) (newline))))))
  19565. (defun org-return-indent ()
  19566. "Goto next table row or insert a newline and indent.
  19567. Calls `org-table-next-row' or `newline-and-indent', depending on
  19568. context. See the individual commands for more information."
  19569. (interactive)
  19570. (org-return t))
  19571. (defun org-ctrl-c-star ()
  19572. "Compute table, or change heading status of lines.
  19573. Calls `org-table-recalculate' or `org-toggle-heading',
  19574. depending on context."
  19575. (interactive)
  19576. (cond
  19577. ((org-at-table-p)
  19578. (call-interactively 'org-table-recalculate))
  19579. (t
  19580. ;; Convert all lines in region to list items
  19581. (call-interactively 'org-toggle-heading))))
  19582. (defun org-ctrl-c-minus ()
  19583. "Insert separator line in table or modify bullet status of line.
  19584. Also turns a plain line or a region of lines into list items.
  19585. Calls `org-table-insert-hline', `org-toggle-item', or
  19586. `org-cycle-list-bullet', depending on context."
  19587. (interactive)
  19588. (cond
  19589. ((org-at-table-p)
  19590. (call-interactively 'org-table-insert-hline))
  19591. ((org-region-active-p)
  19592. (call-interactively 'org-toggle-item))
  19593. ((org-in-item-p)
  19594. (call-interactively 'org-cycle-list-bullet))
  19595. (t
  19596. (call-interactively 'org-toggle-item))))
  19597. (defun org-toggle-item (arg)
  19598. "Convert headings or normal lines to items, items to normal lines.
  19599. If there is no active region, only the current line is considered.
  19600. If the first non blank line in the region is a headline, convert
  19601. all headlines to items, shifting text accordingly.
  19602. If it is an item, convert all items to normal lines.
  19603. If it is normal text, change region into a list of items.
  19604. With a prefix argument ARG, change the region in a single item."
  19605. (interactive "P")
  19606. (let ((shift-text
  19607. (function
  19608. ;; Shift text in current section to IND, from point to END.
  19609. ;; The function leaves point to END line.
  19610. (lambda (ind end)
  19611. (let ((min-i 1000) (end (copy-marker end)))
  19612. ;; First determine the minimum indentation (MIN-I) of
  19613. ;; the text.
  19614. (save-excursion
  19615. (catch 'exit
  19616. (while (< (point) end)
  19617. (let ((i (org-get-indentation)))
  19618. (cond
  19619. ;; Skip blank lines and inline tasks.
  19620. ((looking-at "^[ \t]*$"))
  19621. ((looking-at org-outline-regexp-bol))
  19622. ;; We can't find less than 0 indentation.
  19623. ((zerop i) (throw 'exit (setq min-i 0)))
  19624. ((< i min-i) (setq min-i i))))
  19625. (forward-line))))
  19626. ;; Then indent each line so that a line indented to
  19627. ;; MIN-I becomes indented to IND. Ignore blank lines
  19628. ;; and inline tasks in the process.
  19629. (let ((delta (- ind min-i)))
  19630. (while (< (point) end)
  19631. (unless (or (looking-at "^[ \t]*$")
  19632. (looking-at org-outline-regexp-bol))
  19633. (org-indent-line-to (+ (org-get-indentation) delta)))
  19634. (forward-line)))))))
  19635. (skip-blanks
  19636. (function
  19637. ;; Return beginning of first non-blank line, starting from
  19638. ;; line at POS.
  19639. (lambda (pos)
  19640. (save-excursion
  19641. (goto-char pos)
  19642. (skip-chars-forward " \r\t\n")
  19643. (point-at-bol)))))
  19644. beg end)
  19645. ;; Determine boundaries of changes.
  19646. (if (org-region-active-p)
  19647. (setq beg (funcall skip-blanks (region-beginning))
  19648. end (copy-marker (region-end)))
  19649. (setq beg (funcall skip-blanks (point-at-bol))
  19650. end (copy-marker (point-at-eol))))
  19651. ;; Depending on the starting line, choose an action on the text
  19652. ;; between BEG and END.
  19653. (org-with-limited-levels
  19654. (save-excursion
  19655. (goto-char beg)
  19656. (cond
  19657. ;; Case 1. Start at an item: de-itemize. Note that it only
  19658. ;; happens when a region is active: `org-ctrl-c-minus'
  19659. ;; would call `org-cycle-list-bullet' otherwise.
  19660. ((org-at-item-p)
  19661. (while (< (point) end)
  19662. (when (org-at-item-p)
  19663. (skip-chars-forward " \t")
  19664. (delete-region (point) (match-end 0)))
  19665. (forward-line)))
  19666. ;; Case 2. Start at an heading: convert to items.
  19667. ((org-at-heading-p)
  19668. (let* ((bul (org-list-bullet-string "-"))
  19669. (bul-len (length bul))
  19670. (done (org-entry-is-done-p))
  19671. (todo (org-entry-is-todo-p))
  19672. ;; Indentation of the first heading. It should be
  19673. ;; relative to the indentation of its parent, if any.
  19674. (start-ind (save-excursion
  19675. (cond
  19676. ((not org-adapt-indentation) 0)
  19677. ((not (outline-previous-heading)) 0)
  19678. (t (length (match-string 0))))))
  19679. ;; Level of first heading. Further headings will be
  19680. ;; compared to it to determine hierarchy in the list.
  19681. (ref-level (org-reduced-level (org-outline-level))))
  19682. (when (or done todo) (org-todo ""))
  19683. (while (< (point) end)
  19684. (let* ((level (org-reduced-level (org-outline-level)))
  19685. (delta (max 0 (- level ref-level))))
  19686. ;; If current headline is less indented than the first
  19687. ;; one, set it as reference, in order to preserve
  19688. ;; subtrees.
  19689. (when (< level ref-level) (setq ref-level level))
  19690. (replace-match bul t t)
  19691. (org-indent-line-to (+ start-ind (* delta bul-len)))
  19692. (when (or done todo)
  19693. (let* ((struct (org-list-struct))
  19694. (old (copy-tree struct)))
  19695. (org-list-set-checkbox (line-beginning-position)
  19696. struct
  19697. (if done "[X]" "[ ]"))
  19698. (org-list-write-struct struct
  19699. (org-list-parents-alist struct)
  19700. old)))
  19701. ;; Ensure all text down to END (or SECTION-END) belongs
  19702. ;; to the newly created item.
  19703. (let ((section-end (save-excursion
  19704. (or (outline-next-heading) (point)))))
  19705. (forward-line)
  19706. (funcall shift-text
  19707. (+ start-ind (* (1+ delta) bul-len))
  19708. (min end section-end)))))))
  19709. ;; Case 3. Normal line with ARG: make the first line of region
  19710. ;; an item, and shift indentation of others lines to
  19711. ;; set them as item's body.
  19712. (arg (let* ((bul (org-list-bullet-string "-"))
  19713. (bul-len (length bul))
  19714. (ref-ind (org-get-indentation)))
  19715. (skip-chars-forward " \t")
  19716. (insert bul)
  19717. (forward-line)
  19718. (while (< (point) end)
  19719. ;; Ensure that lines less indented than first one
  19720. ;; still get included in item body.
  19721. (funcall shift-text
  19722. (+ ref-ind bul-len)
  19723. (min end (save-excursion (or (outline-next-heading)
  19724. (point)))))
  19725. (forward-line))))
  19726. ;; Case 4. Normal line without ARG: turn each non-item line
  19727. ;; into an item.
  19728. (t
  19729. (while (< (point) end)
  19730. (unless (or (org-at-heading-p) (org-at-item-p))
  19731. (when (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  19732. (replace-match
  19733. (concat "\\1" (org-list-bullet-string "-") "\\2"))))
  19734. (forward-line))))))))
  19735. (defun org-toggle-heading (&optional nstars)
  19736. "Convert headings to normal text, or items or text to headings.
  19737. If there is no active region, only convert the current line.
  19738. With a \\[universal-argument] prefix, convert the whole list at
  19739. point into heading.
  19740. In a region:
  19741. - If the first non blank line is a headline, remove the stars
  19742. from all headlines in the region.
  19743. - If it is a normal line, turn each and every normal line (i.e.,
  19744. not an heading or an item) in the region into headings. If you
  19745. want to convert only the first line of this region, use one
  19746. universal prefix argument.
  19747. - If it is a plain list item, turn all plain list items into headings.
  19748. When converting a line into a heading, the number of stars is chosen
  19749. such that the lines become children of the current entry. However,
  19750. when a numeric prefix argument is given, its value determines the
  19751. number of stars to add."
  19752. (interactive "P")
  19753. (let ((skip-blanks
  19754. (function
  19755. ;; Return beginning of first non-blank line, starting from
  19756. ;; line at POS.
  19757. (lambda (pos)
  19758. (save-excursion
  19759. (goto-char pos)
  19760. (while (org-at-comment-p) (forward-line))
  19761. (skip-chars-forward " \r\t\n")
  19762. (point-at-bol)))))
  19763. beg end toggled)
  19764. ;; Determine boundaries of changes. If a universal prefix has
  19765. ;; been given, put the list in a region. If region ends at a bol,
  19766. ;; do not consider the last line to be in the region.
  19767. (when (and current-prefix-arg (org-at-item-p))
  19768. (when (listp current-prefix-arg) (setq current-prefix-arg 1))
  19769. (org-mark-element))
  19770. (if (org-region-active-p)
  19771. (setq beg (funcall skip-blanks (region-beginning))
  19772. end (copy-marker (save-excursion
  19773. (goto-char (region-end))
  19774. (if (bolp) (point) (point-at-eol)))))
  19775. (setq beg (funcall skip-blanks (point-at-bol))
  19776. end (copy-marker (point-at-eol))))
  19777. ;; Ensure inline tasks don't count as headings.
  19778. (org-with-limited-levels
  19779. (save-excursion
  19780. (goto-char beg)
  19781. (cond
  19782. ;; Case 1. Started at an heading: de-star headings.
  19783. ((org-at-heading-p)
  19784. (while (< (point) end)
  19785. (when (org-at-heading-p t)
  19786. (looking-at org-outline-regexp) (replace-match "")
  19787. (setq toggled t))
  19788. (forward-line)))
  19789. ;; Case 2. Started at an item: change items into headlines.
  19790. ;; One star will be added by `org-list-to-subtree'.
  19791. ((org-at-item-p)
  19792. (while (< (point) end)
  19793. (when (org-at-item-p)
  19794. ;; Pay attention to cases when region ends before list.
  19795. (let* ((struct (org-list-struct))
  19796. (list-end
  19797. (min (org-list-get-bottom-point struct) (1+ end))))
  19798. (save-restriction
  19799. (narrow-to-region (point) list-end)
  19800. (insert (org-list-to-subtree (org-list-to-lisp t)) "\n")))
  19801. (setq toggled t))
  19802. (forward-line)))
  19803. ;; Case 3. Started at normal text: make every line an heading,
  19804. ;; skipping headlines and items.
  19805. (t (let* ((stars
  19806. (make-string
  19807. (if (numberp nstars) nstars (or (org-current-level) 0)) ?*))
  19808. (add-stars
  19809. (cond (nstars "") ; stars from prefix only
  19810. ((equal stars "") "*") ; before first heading
  19811. (org-odd-levels-only "**") ; inside heading, odd
  19812. (t "*"))) ; inside heading, oddeven
  19813. (rpl (concat stars add-stars " "))
  19814. (lend (when (listp nstars) (save-excursion (end-of-line) (point)))))
  19815. (while (< (point) (if (equal nstars '(4)) lend end))
  19816. (when (and (not (or (org-at-heading-p) (org-at-item-p) (org-at-comment-p)))
  19817. (looking-at "\\([ \t]*\\)\\(\\S-\\)"))
  19818. (replace-match (concat rpl (match-string 2))) (setq toggled t))
  19819. (forward-line)))))))
  19820. (unless toggled (message "Cannot toggle heading from here"))))
  19821. (defun org-meta-return (&optional _arg)
  19822. "Insert a new heading or wrap a region in a table.
  19823. Calls `org-insert-heading' or `org-table-wrap-region', depending
  19824. on context. See the individual commands for more information."
  19825. (interactive)
  19826. (org-check-before-invisible-edit 'insert)
  19827. (or (run-hook-with-args-until-success 'org-metareturn-hook)
  19828. (call-interactively (if (org-at-table-p) #'org-table-wrap-region
  19829. #'org-insert-heading))))
  19830. ;;; Menu entries
  19831. (defsubst org-in-subtree-not-table-p ()
  19832. "Are we in a subtree and not in a table?"
  19833. (and (not (org-before-first-heading-p))
  19834. (not (org-at-table-p))))
  19835. ;; Define the Org-mode menus
  19836. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  19837. '("Tbl"
  19838. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  19839. ["Next Field" org-cycle (org-at-table-p)]
  19840. ["Previous Field" org-shifttab (org-at-table-p)]
  19841. ["Next Row" org-return (org-at-table-p)]
  19842. "--"
  19843. ["Blank Field" org-table-blank-field (org-at-table-p)]
  19844. ["Edit Field" org-table-edit-field (org-at-table-p)]
  19845. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  19846. "--"
  19847. ("Column"
  19848. ["Move Column Left" org-metaleft (org-at-table-p)]
  19849. ["Move Column Right" org-metaright (org-at-table-p)]
  19850. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  19851. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  19852. ("Row"
  19853. ["Move Row Up" org-metaup (org-at-table-p)]
  19854. ["Move Row Down" org-metadown (org-at-table-p)]
  19855. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  19856. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  19857. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  19858. "--"
  19859. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  19860. ("Rectangle"
  19861. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  19862. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  19863. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  19864. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  19865. "--"
  19866. ("Calculate"
  19867. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  19868. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  19869. ["Edit Formulas" org-edit-special (org-at-table-p)]
  19870. "--"
  19871. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  19872. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  19873. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  19874. "--"
  19875. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  19876. "--"
  19877. ["Sum Column/Rectangle" org-table-sum
  19878. (or (org-at-table-p) (org-region-active-p))]
  19879. ["Which Column?" org-table-current-column (org-at-table-p)])
  19880. ["Debug Formulas"
  19881. org-table-toggle-formula-debugger
  19882. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  19883. ["Show Col/Row Numbers"
  19884. org-table-toggle-coordinate-overlays
  19885. :style toggle
  19886. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  19887. "--"
  19888. ["Create" org-table-create (and (not (org-at-table-p))
  19889. org-enable-table-editor)]
  19890. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  19891. ["Import from File" org-table-import (not (org-at-table-p))]
  19892. ["Export to File" org-table-export (org-at-table-p)]
  19893. "--"
  19894. ["Create/Convert from/to table.el" org-table-create-with-table.el t]
  19895. "--"
  19896. ("Plot"
  19897. ["Ascii plot" orgtbl-ascii-plot :active (org-at-table-p) :keys "C-c \" a"]
  19898. ["Gnuplot" org-plot/gnuplot :active (org-at-table-p) :keys "C-c \" g"])))
  19899. (easy-menu-define org-org-menu org-mode-map "Org menu"
  19900. '("Org"
  19901. ("Show/Hide"
  19902. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  19903. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  19904. ["Sparse Tree..." org-sparse-tree t]
  19905. ["Reveal Context" org-reveal t]
  19906. ["Show All" outline-show-all t]
  19907. "--"
  19908. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  19909. "--"
  19910. ["New Heading" org-insert-heading t]
  19911. ("Navigate Headings"
  19912. ["Up" outline-up-heading t]
  19913. ["Next" outline-next-visible-heading t]
  19914. ["Previous" outline-previous-visible-heading t]
  19915. ["Next Same Level" outline-forward-same-level t]
  19916. ["Previous Same Level" outline-backward-same-level t]
  19917. "--"
  19918. ["Jump" org-goto t])
  19919. ("Edit Structure"
  19920. ["Refile Subtree" org-refile (org-in-subtree-not-table-p)]
  19921. "--"
  19922. ["Move Subtree Up" org-metaup (org-at-heading-p)]
  19923. ["Move Subtree Down" org-metadown (org-at-heading-p)]
  19924. "--"
  19925. ["Copy Subtree" org-copy-special (org-in-subtree-not-table-p)]
  19926. ["Cut Subtree" org-cut-special (org-in-subtree-not-table-p)]
  19927. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  19928. "--"
  19929. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  19930. "--"
  19931. ["Copy visible text" org-copy-visible t]
  19932. "--"
  19933. ["Promote Heading" org-metaleft (org-in-subtree-not-table-p)]
  19934. ["Promote Subtree" org-shiftmetaleft (org-in-subtree-not-table-p)]
  19935. ["Demote Heading" org-metaright (org-in-subtree-not-table-p)]
  19936. ["Demote Subtree" org-shiftmetaright (org-in-subtree-not-table-p)]
  19937. "--"
  19938. ["Sort Region/Children" org-sort t]
  19939. "--"
  19940. ["Convert to odd levels" org-convert-to-odd-levels t]
  19941. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  19942. ("Editing"
  19943. ["Emphasis..." org-emphasize t]
  19944. ["Edit Source Example" org-edit-special t]
  19945. "--"
  19946. ["Footnote new/jump" org-footnote-action t]
  19947. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  19948. ("Archive"
  19949. ["Archive (default method)" org-archive-subtree-default (org-in-subtree-not-table-p)]
  19950. "--"
  19951. ["Move Subtree to Archive file" org-archive-subtree (org-in-subtree-not-table-p)]
  19952. ["Toggle ARCHIVE tag" org-toggle-archive-tag (org-in-subtree-not-table-p)]
  19953. ["Move subtree to Archive sibling" org-archive-to-archive-sibling (org-in-subtree-not-table-p)]
  19954. )
  19955. "--"
  19956. ("Hyperlinks"
  19957. ["Store Link (Global)" org-store-link t]
  19958. ["Find existing link to here" org-occur-link-in-agenda-files t]
  19959. ["Insert Link" org-insert-link t]
  19960. ["Follow Link" org-open-at-point t]
  19961. "--"
  19962. ["Next link" org-next-link t]
  19963. ["Previous link" org-previous-link t]
  19964. "--"
  19965. ["Descriptive Links"
  19966. org-toggle-link-display
  19967. :style radio
  19968. :selected org-descriptive-links
  19969. ]
  19970. ["Literal Links"
  19971. org-toggle-link-display
  19972. :style radio
  19973. :selected (not org-descriptive-links)])
  19974. "--"
  19975. ("TODO Lists"
  19976. ["TODO/DONE/-" org-todo t]
  19977. ("Select keyword"
  19978. ["Next keyword" org-shiftright (org-at-heading-p)]
  19979. ["Previous keyword" org-shiftleft (org-at-heading-p)]
  19980. ["Complete Keyword" pcomplete (assq :todo-keyword (org-context))]
  19981. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))]
  19982. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))])
  19983. ["Show TODO Tree" org-show-todo-tree :active t :keys "C-c / t"]
  19984. ["Global TODO list" org-todo-list :active t :keys "C-c a t"]
  19985. "--"
  19986. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  19987. :selected org-enforce-todo-dependencies :style toggle :active t]
  19988. "Settings for tree at point"
  19989. ["Do Children sequentially" org-toggle-ordered-property :style radio
  19990. :selected (org-entry-get nil "ORDERED")
  19991. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  19992. ["Do Children parallel" org-toggle-ordered-property :style radio
  19993. :selected (not (org-entry-get nil "ORDERED"))
  19994. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  19995. "--"
  19996. ["Set Priority" org-priority t]
  19997. ["Priority Up" org-shiftup t]
  19998. ["Priority Down" org-shiftdown t]
  19999. "--"
  20000. ["Get news from all feeds" org-feed-update-all t]
  20001. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  20002. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  20003. ("TAGS and Properties"
  20004. ["Set Tags" org-set-tags-command (not (org-before-first-heading-p))]
  20005. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  20006. "--"
  20007. ["Set property" org-set-property (not (org-before-first-heading-p))]
  20008. ["Column view of properties" org-columns t]
  20009. ["Insert Column View DBlock" org-columns-insert-dblock t])
  20010. ("Dates and Scheduling"
  20011. ["Timestamp" org-time-stamp (not (org-before-first-heading-p))]
  20012. ["Timestamp (inactive)" org-time-stamp-inactive (not (org-before-first-heading-p))]
  20013. ("Change Date"
  20014. ["1 Day Later" org-shiftright (org-at-timestamp-p)]
  20015. ["1 Day Earlier" org-shiftleft (org-at-timestamp-p)]
  20016. ["1 ... Later" org-shiftup (org-at-timestamp-p)]
  20017. ["1 ... Earlier" org-shiftdown (org-at-timestamp-p)])
  20018. ["Compute Time Range" org-evaluate-time-range t]
  20019. ["Schedule Item" org-schedule (not (org-before-first-heading-p))]
  20020. ["Deadline" org-deadline (not (org-before-first-heading-p))]
  20021. "--"
  20022. ["Custom time format" org-toggle-time-stamp-overlays
  20023. :style radio :selected org-display-custom-times]
  20024. "--"
  20025. ["Goto Calendar" org-goto-calendar t]
  20026. ["Date from Calendar" org-date-from-calendar t]
  20027. "--"
  20028. ["Start/Restart Timer" org-timer-start t]
  20029. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  20030. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  20031. ["Insert Timer String" org-timer t]
  20032. ["Insert Timer Item" org-timer-item t])
  20033. ("Logging work"
  20034. ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
  20035. ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
  20036. ["Clock out" org-clock-out t]
  20037. ["Clock cancel" org-clock-cancel t]
  20038. "--"
  20039. ["Mark as default task" org-clock-mark-default-task t]
  20040. ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
  20041. ["Goto running clock" org-clock-goto t]
  20042. "--"
  20043. ["Display times" org-clock-display t]
  20044. ["Create clock table" org-clock-report t]
  20045. "--"
  20046. ["Record DONE time"
  20047. (progn (setq org-log-done (not org-log-done))
  20048. (message "Switching to %s will %s record a timestamp"
  20049. (car org-done-keywords)
  20050. (if org-log-done "automatically" "not")))
  20051. :style toggle :selected org-log-done])
  20052. "--"
  20053. ["Agenda Command..." org-agenda t]
  20054. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  20055. ("File List for Agenda")
  20056. ("Special views current file"
  20057. ["TODO Tree" org-show-todo-tree t]
  20058. ["Check Deadlines" org-check-deadlines t]
  20059. ["Timeline" org-timeline t]
  20060. ["Tags/Property tree" org-match-sparse-tree t])
  20061. "--"
  20062. ["Export/Publish..." org-export-dispatch t]
  20063. ("LaTeX"
  20064. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  20065. :selected org-cdlatex-mode]
  20066. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  20067. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  20068. ["Modify math symbol" org-cdlatex-math-modify
  20069. (org-inside-LaTeX-fragment-p)]
  20070. ["Insert citation" org-reftex-citation t]
  20071. "--"
  20072. ["Template for BEAMER" (org-beamer-insert-options-template) t])
  20073. "--"
  20074. ("MobileOrg"
  20075. ["Push Files and Views" org-mobile-push t]
  20076. ["Get Captured and Flagged" org-mobile-pull t]
  20077. ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
  20078. "--"
  20079. ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
  20080. "--"
  20081. ("Documentation"
  20082. ["Show Version" org-version t]
  20083. ["Info Documentation" org-info t])
  20084. ("Customize"
  20085. ["Browse Org Group" org-customize t]
  20086. "--"
  20087. ["Expand This Menu" org-create-customize-menu
  20088. (fboundp 'customize-menu-create)])
  20089. ["Send bug report" org-submit-bug-report t]
  20090. "--"
  20091. ("Refresh/Reload"
  20092. ["Refresh setup current buffer" org-mode-restart t]
  20093. ["Reload Org (after update)" org-reload t]
  20094. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x !"])
  20095. ))
  20096. (defun org-info (&optional node)
  20097. "Read documentation for Org-mode in the info system.
  20098. With optional NODE, go directly to that node."
  20099. (interactive)
  20100. (info (format "(org)%s" (or node ""))))
  20101. ;;;###autoload
  20102. (defun org-submit-bug-report ()
  20103. "Submit a bug report on Org-mode via mail.
  20104. Don't hesitate to report any problems or inaccurate documentation.
  20105. If you don't have setup sending mail from (X)Emacs, please copy the
  20106. output buffer into your mail program, as it gives us important
  20107. information about your Org-mode version and configuration."
  20108. (interactive)
  20109. (require 'reporter)
  20110. (defvar reporter-prompt-for-summary-p)
  20111. (org-load-modules-maybe)
  20112. (org-require-autoloaded-modules)
  20113. (let ((reporter-prompt-for-summary-p "Bug report subject: "))
  20114. (reporter-submit-bug-report
  20115. "emacs-orgmode@gnu.org"
  20116. (org-version nil 'full)
  20117. (let (list)
  20118. (save-window-excursion
  20119. (org-pop-to-buffer-same-window (get-buffer-create "*Warn about privacy*"))
  20120. (delete-other-windows)
  20121. (erase-buffer)
  20122. (insert "You are about to submit a bug report to the Org-mode mailing list.
  20123. We would like to add your full Org-mode and Outline configuration to the
  20124. bug report. This greatly simplifies the work of the maintainer and
  20125. other experts on the mailing list.
  20126. HOWEVER, some variables you have customized may contain private
  20127. information. The names of customers, colleagues, or friends, might
  20128. appear in the form of file names, tags, todo states, or search strings.
  20129. If you answer yes to the prompt, you might want to check and remove
  20130. such private information before sending the email.")
  20131. (add-text-properties (point-min) (point-max) '(face org-warning))
  20132. (when (yes-or-no-p "Include your Org-mode configuration ")
  20133. (mapatoms
  20134. (lambda (v)
  20135. (and (boundp v)
  20136. (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
  20137. (or (and (symbol-value v)
  20138. (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
  20139. (and
  20140. (get v 'custom-type) (get v 'standard-value)
  20141. (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
  20142. (push v list)))))
  20143. (kill-buffer (get-buffer "*Warn about privacy*"))
  20144. list))
  20145. nil nil
  20146. "Remember to cover the basics, that is, what you expected to happen and
  20147. what in fact did happen. You don't know how to make a good report? See
  20148. http://orgmode.org/manual/Feedback.html#Feedback
  20149. Your bug report will be posted to the Org-mode mailing list.
  20150. ------------------------------------------------------------------------")
  20151. (save-excursion
  20152. (when (re-search-backward "^\\(Subject: \\)Org-mode version \\(.*?\\);[ \t]*\\(.*\\)" nil t)
  20153. (replace-match "\\1Bug: \\3 [\\2]")))))
  20154. (defun org-install-agenda-files-menu ()
  20155. (let ((bl (buffer-list)))
  20156. (save-excursion
  20157. (while bl
  20158. (set-buffer (pop bl))
  20159. (when (derived-mode-p 'org-mode) (setq bl nil)))
  20160. (when (derived-mode-p 'org-mode)
  20161. (easy-menu-change
  20162. '("Org") "File List for Agenda"
  20163. (append
  20164. (list
  20165. ["Edit File List" (org-edit-agenda-file-list) t]
  20166. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  20167. ["Remove Current File from List" org-remove-file t]
  20168. ["Cycle through agenda files" org-cycle-agenda-files t]
  20169. ["Occur in all agenda files" org-occur-in-agenda-files t]
  20170. "--")
  20171. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  20172. ;;;; Documentation
  20173. (defun org-require-autoloaded-modules ()
  20174. (interactive)
  20175. (mapc #'require
  20176. '(org-agenda org-archive org-attach org-clock org-colview org-id
  20177. org-table org-timer)))
  20178. ;;;###autoload
  20179. (defun org-reload (&optional uncompiled)
  20180. "Reload all org lisp files.
  20181. With prefix arg UNCOMPILED, load the uncompiled versions."
  20182. (interactive "P")
  20183. (require 'loadhist)
  20184. (let* ((org-dir (org-find-library-dir "org"))
  20185. (contrib-dir (or (org-find-library-dir "org-contribdir") org-dir))
  20186. (feature-re "^\\(org\\|ob\\|ox\\)\\(-.*\\)?")
  20187. (remove-re (mapconcat 'identity
  20188. (mapcar (lambda (f) (concat "^" f "$"))
  20189. (list (if (featurep 'xemacs)
  20190. "org-colview"
  20191. "org-colview-xemacs")
  20192. "org" "org-loaddefs" "org-version"))
  20193. "\\|"))
  20194. (feats (delete-dups
  20195. (mapcar 'file-name-sans-extension
  20196. (mapcar 'file-name-nondirectory
  20197. (delq nil
  20198. (mapcar 'feature-file
  20199. features))))))
  20200. (lfeat (append
  20201. (sort
  20202. (setq feats
  20203. (delq nil (mapcar
  20204. (lambda (f)
  20205. (if (and (string-match feature-re f)
  20206. (not (string-match remove-re f)))
  20207. f nil))
  20208. feats)))
  20209. 'string-lessp)
  20210. (list "org-version" "org")))
  20211. (load-suffixes (when (boundp 'load-suffixes) load-suffixes))
  20212. (load-suffixes (if uncompiled (reverse load-suffixes) load-suffixes))
  20213. load-uncore load-misses)
  20214. (setq load-misses
  20215. (delq 't
  20216. (mapcar (lambda (f)
  20217. (or (org-load-noerror-mustsuffix (concat org-dir f))
  20218. (and (string= org-dir contrib-dir)
  20219. (org-load-noerror-mustsuffix (concat contrib-dir f)))
  20220. (and (org-load-noerror-mustsuffix (concat (org-find-library-dir f) f))
  20221. (add-to-list 'load-uncore f 'append)
  20222. 't)
  20223. f))
  20224. lfeat)))
  20225. (when load-uncore
  20226. (message "The following feature%s found in load-path, please check if that's correct:\n%s"
  20227. (if (> (length load-uncore) 1) "s were" " was") load-uncore))
  20228. (if load-misses
  20229. (message "Some error occurred while reloading Org feature%s\n%s\nPlease check *Messages*!\n%s"
  20230. (if (> (length load-misses) 1) "s" "") load-misses (org-version nil 'full))
  20231. (message "Successfully reloaded Org\n%s" (org-version nil 'full)))))
  20232. ;;;###autoload
  20233. (defun org-customize ()
  20234. "Call the customize function with org as argument."
  20235. (interactive)
  20236. (org-load-modules-maybe)
  20237. (org-require-autoloaded-modules)
  20238. (customize-browse 'org))
  20239. (defun org-create-customize-menu ()
  20240. "Create a full customization menu for Org-mode, insert it into the menu."
  20241. (interactive)
  20242. (org-load-modules-maybe)
  20243. (org-require-autoloaded-modules)
  20244. (if (fboundp 'customize-menu-create)
  20245. (progn
  20246. (easy-menu-change
  20247. '("Org") "Customize"
  20248. `(["Browse Org group" org-customize t]
  20249. "--"
  20250. ,(customize-menu-create 'org)
  20251. ["Set" Custom-set t]
  20252. ["Save" Custom-save t]
  20253. ["Reset to Current" Custom-reset-current t]
  20254. ["Reset to Saved" Custom-reset-saved t]
  20255. ["Reset to Standard Settings" Custom-reset-standard t]))
  20256. (message "\"Org\"-menu now contains full customization menu"))
  20257. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  20258. ;;;; Miscellaneous stuff
  20259. ;;; Generally useful functions
  20260. (defun org-get-at-eol (property n)
  20261. "Get text property PROPERTY at the end of line less N characters."
  20262. (get-text-property (- (point-at-eol) n) property))
  20263. (defun org-find-text-property-in-string (prop s)
  20264. "Return the first non-nil value of property PROP in string S."
  20265. (or (get-text-property 0 prop s)
  20266. (get-text-property (or (next-single-property-change 0 prop s) 0)
  20267. prop s)))
  20268. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  20269. "Display the given MESSAGE as a warning."
  20270. (if (fboundp 'display-warning)
  20271. (display-warning 'org message
  20272. (if (featurep 'xemacs) 'warning :warning))
  20273. (let ((buf (get-buffer-create "*Org warnings*")))
  20274. (with-current-buffer buf
  20275. (goto-char (point-max))
  20276. (insert "Warning (Org): " message)
  20277. (unless (bolp)
  20278. (newline)))
  20279. (display-buffer buf)
  20280. (sit-for 0))))
  20281. (defun org-eval (form)
  20282. "Eval FORM and return result."
  20283. (condition-case error
  20284. (eval form)
  20285. (error (format "%%![Error: %s]" error))))
  20286. (defun org-in-clocktable-p ()
  20287. "Check if the cursor is in a clocktable."
  20288. (let ((pos (point)) start)
  20289. (save-excursion
  20290. (end-of-line 1)
  20291. (and (re-search-backward "^[ \t]*#\\+BEGIN:[ \t]+clocktable" nil t)
  20292. (setq start (match-beginning 0))
  20293. (re-search-forward "^[ \t]*#\\+END:.*" nil t)
  20294. (>= (match-end 0) pos)
  20295. start))))
  20296. (defun org-in-verbatim-emphasis ()
  20297. (save-match-data
  20298. (and (org-in-regexp org-emph-re 2)
  20299. (>= (point) (match-beginning 3))
  20300. (<= (point) (match-end 4))
  20301. (member (match-string 3) '("=" "~")))))
  20302. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  20303. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  20304. (if (and marker (marker-buffer marker)
  20305. (buffer-live-p (marker-buffer marker)))
  20306. (progn
  20307. (org-pop-to-buffer-same-window (marker-buffer marker))
  20308. (when (or (> marker (point-max)) (< marker (point-min)))
  20309. (widen))
  20310. (goto-char marker)
  20311. (org-show-context 'org-goto))
  20312. (if bookmark
  20313. (bookmark-jump bookmark)
  20314. (error "Cannot find location"))))
  20315. (defun org-quote-csv-field (s)
  20316. "Quote field for inclusion in CSV material."
  20317. (if (string-match "[\",]" s)
  20318. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  20319. s))
  20320. (defun org-force-self-insert (N)
  20321. "Needed to enforce self-insert under remapping."
  20322. (interactive "p")
  20323. (self-insert-command N))
  20324. (defun org-string-width (s)
  20325. "Compute width of string, ignoring invisible characters.
  20326. This ignores character with invisibility property `org-link', and also
  20327. characters with property `org-cwidth', because these will become invisible
  20328. upon the next fontification round."
  20329. (let (b l)
  20330. (when (or (eq t buffer-invisibility-spec)
  20331. (assq 'org-link buffer-invisibility-spec))
  20332. (while (setq b (text-property-any 0 (length s)
  20333. 'invisible 'org-link s))
  20334. (setq s (concat (substring s 0 b)
  20335. (substring s (or (next-single-property-change
  20336. b 'invisible s)
  20337. (length s)))))))
  20338. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  20339. (setq s (concat (substring s 0 b)
  20340. (substring s (or (next-single-property-change
  20341. b 'org-cwidth s)
  20342. (length s))))))
  20343. (setq l (string-width s) b -1)
  20344. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  20345. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  20346. l))
  20347. (defun org-shorten-string (s maxlength)
  20348. "Shorten string S so tht it is no longer than MAXLENGTH characters.
  20349. If the string is shorter or has length MAXLENGTH, just return the
  20350. original string. If it is longer, the functions finds a space in the
  20351. string, breaks this string off at that locations and adds three dots
  20352. as ellipsis. Including the ellipsis, the string will not be longer
  20353. than MAXLENGTH. If finding a good breaking point in the string does
  20354. not work, the string is just chopped off in the middle of a word
  20355. if necessary."
  20356. (if (<= (length s) maxlength)
  20357. s
  20358. (let* ((n (max (- maxlength 4) 1))
  20359. (re (concat "\\`\\(.\\{1," (int-to-string n) "\\}[^ ]\\)\\([ ]\\|\\'\\)")))
  20360. (if (string-match re s)
  20361. (concat (match-string 1 s) "...")
  20362. (concat (substring s 0 (max (- maxlength 3) 0)) "...")))))
  20363. (defun org-get-indentation (&optional line)
  20364. "Get the indentation of the current line, interpreting tabs.
  20365. When LINE is given, assume it represents a line and compute its indentation."
  20366. (if line
  20367. (when (string-match "^ *" (org-remove-tabs line))
  20368. (match-end 0))
  20369. (save-excursion
  20370. (beginning-of-line 1)
  20371. (skip-chars-forward " \t")
  20372. (current-column))))
  20373. (defun org-get-string-indentation (s)
  20374. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  20375. (let ((n -1) (i 0) (w tab-width) c)
  20376. (catch 'exit
  20377. (while (< (setq n (1+ n)) (length s))
  20378. (setq c (aref s n))
  20379. (cond ((= c ?\ ) (setq i (1+ i)))
  20380. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  20381. (t (throw 'exit t)))))
  20382. i))
  20383. (defun org-remove-tabs (s &optional width)
  20384. "Replace tabulators in S with spaces.
  20385. Assumes that s is a single line, starting in column 0."
  20386. (setq width (or width tab-width))
  20387. (while (string-match "\t" s)
  20388. (setq s (replace-match
  20389. (make-string
  20390. (- (* width (/ (+ (match-beginning 0) width) width))
  20391. (match-beginning 0)) ?\ )
  20392. t t s)))
  20393. s)
  20394. (defun org-fix-indentation (line ind)
  20395. "Fix indentation in LINE.
  20396. IND is a cons cell with target and minimum indentation.
  20397. If the current indentation in LINE is smaller than the minimum,
  20398. leave it alone. If it is larger than ind, set it to the target."
  20399. (let* ((l (org-remove-tabs line))
  20400. (i (org-get-indentation l))
  20401. (i1 (car ind)) (i2 (cdr ind)))
  20402. (when (>= i i2) (setq l (substring line i2)))
  20403. (if (> i1 0)
  20404. (concat (make-string i1 ?\ ) l)
  20405. l)))
  20406. (defun org-remove-indentation (code &optional n)
  20407. "Remove maximum common indentation in string CODE and return it.
  20408. N may optionally be the number of columns to remove. Return CODE
  20409. as-is if removal failed."
  20410. (with-temp-buffer
  20411. (insert code)
  20412. (if (org-do-remove-indentation n) (buffer-string) code)))
  20413. (defun org-do-remove-indentation (&optional n)
  20414. "Remove the maximum common indentation from the buffer.
  20415. When optional argument N is a positive integer, remove exactly
  20416. that much characters from indentation, if possible. Return nil
  20417. if it fails."
  20418. (catch :exit
  20419. (goto-char (point-min))
  20420. ;; Find maximum common indentation, if not specified.
  20421. (let ((n (or n
  20422. (let ((min-ind (point-max)))
  20423. (save-excursion
  20424. (while (re-search-forward "^[ \t]*\\S-" nil t)
  20425. (let ((ind (1- (current-column))))
  20426. (if (zerop ind) (throw :exit nil)
  20427. (setq min-ind (min min-ind ind))))))
  20428. min-ind))))
  20429. (if (zerop n) (throw :exit nil)
  20430. ;; Remove exactly N indentation, but give up if not possible.
  20431. (while (not (eobp))
  20432. (let ((ind (progn (skip-chars-forward " \t") (current-column))))
  20433. (cond ((eolp) (delete-region (line-beginning-position) (point)))
  20434. ((< ind n) (throw :exit nil))
  20435. (t (org-indent-line-to (- ind n))))
  20436. (forward-line)))
  20437. ;; Signal success.
  20438. t))))
  20439. (defun org-fill-template (template alist)
  20440. "Find each %key of ALIST in TEMPLATE and replace it."
  20441. (let ((case-fold-search nil))
  20442. (dolist (entry (sort (copy-sequence alist)
  20443. (lambda (a b) (< (length (car a)) (length (car b))))))
  20444. (setq template
  20445. (replace-regexp-in-string
  20446. (concat "%" (regexp-quote (car entry)))
  20447. (or (cdr entry) "") template t t)))
  20448. template))
  20449. (defun org-base-buffer (buffer)
  20450. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  20451. (if (not buffer)
  20452. buffer
  20453. (or (buffer-base-buffer buffer)
  20454. buffer)))
  20455. (defun org-wrap (string &optional width lines)
  20456. "Wrap string to either a number of lines, or a width in characters.
  20457. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  20458. that costs. If there is a word longer than WIDTH, the text is actually
  20459. wrapped to the length of that word.
  20460. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  20461. many lines, whatever width that takes.
  20462. The return value is a list of lines, without newlines at the end."
  20463. (let* ((words (org-split-string string "[ \t\n]+"))
  20464. (maxword (apply 'max (mapcar 'org-string-width words)))
  20465. w ll)
  20466. (cond (width
  20467. (org-do-wrap words (max maxword width)))
  20468. (lines
  20469. (setq w maxword)
  20470. (setq ll (org-do-wrap words maxword))
  20471. (if (<= (length ll) lines)
  20472. ll
  20473. (setq ll words)
  20474. (while (> (length ll) lines)
  20475. (setq w (1+ w))
  20476. (setq ll (org-do-wrap words w)))
  20477. ll))
  20478. (t (error "Cannot wrap this")))))
  20479. (defun org-do-wrap (words width)
  20480. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  20481. (let (lines line)
  20482. (while words
  20483. (setq line (pop words))
  20484. (while (and words (< (+ (length line) (length (car words))) width))
  20485. (setq line (concat line " " (pop words))))
  20486. (setq lines (push line lines)))
  20487. (nreverse lines)))
  20488. (defun org-split-string (string &optional separators)
  20489. "Splits STRING into substrings at SEPARATORS.
  20490. SEPARATORS is a regular expression.
  20491. No empty strings are returned if there are matches at the beginning
  20492. and end of string."
  20493. ;; FIXME: why not use (split-string STRING SEPARATORS t)?
  20494. (let ((start 0) notfirst list)
  20495. (while (and (string-match (or separators "[ \f\t\n\r\v]+") string
  20496. (if (and notfirst
  20497. (= start (match-beginning 0))
  20498. (< start (length string)))
  20499. (1+ start) start))
  20500. (< (match-beginning 0) (length string)))
  20501. (setq notfirst t)
  20502. (or (eq (match-beginning 0) 0)
  20503. (and (eq (match-beginning 0) (match-end 0))
  20504. (eq (match-beginning 0) start))
  20505. (push (substring string start (match-beginning 0)) list))
  20506. (setq start (match-end 0)))
  20507. (or (eq start (length string))
  20508. (push (substring string start) list))
  20509. (nreverse list)))
  20510. (defun org-quote-vert (s)
  20511. "Replace \"|\" with \"\\vert\"."
  20512. (while (string-match "|" s)
  20513. (setq s (replace-match "\\vert" t t s)))
  20514. s)
  20515. (defun org-uuidgen-p (s)
  20516. "Is S an ID created by UUIDGEN?"
  20517. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  20518. (defun org-in-src-block-p (&optional inside)
  20519. "Whether point is in a code source block.
  20520. When INSIDE is non-nil, don't consider we are within a src block
  20521. when point is at #+BEGIN_SRC or #+END_SRC."
  20522. (let ((case-fold-search t))
  20523. (or (and (eq (get-char-property (point) 'src-block) t))
  20524. (and (not inside)
  20525. (save-match-data
  20526. (save-excursion
  20527. (beginning-of-line)
  20528. (looking-at ".*#\\+\\(begin\\|end\\)_src")))))))
  20529. (defun org-context ()
  20530. "Return a list of contexts of the current cursor position.
  20531. If several contexts apply, all are returned.
  20532. Each context entry is a list with a symbol naming the context, and
  20533. two positions indicating start and end of the context. Possible
  20534. contexts are:
  20535. :headline anywhere in a headline
  20536. :headline-stars on the leading stars in a headline
  20537. :todo-keyword on a TODO keyword (including DONE) in a headline
  20538. :tags on the TAGS in a headline
  20539. :priority on the priority cookie in a headline
  20540. :item on the first line of a plain list item
  20541. :item-bullet on the bullet/number of a plain list item
  20542. :checkbox on the checkbox in a plain list item
  20543. :table in an org-mode table
  20544. :table-special on a special filed in a table
  20545. :table-table in a table.el table
  20546. :clocktable in a clocktable
  20547. :src-block in a source block
  20548. :link on a hyperlink
  20549. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE, COMMENT.
  20550. :target on a <<target>>
  20551. :radio-target on a <<<radio-target>>>
  20552. :latex-fragment on a LaTeX fragment
  20553. :latex-preview on a LaTeX fragment with overlaid preview image
  20554. This function expects the position to be visible because it uses font-lock
  20555. faces as a help to recognize the following contexts: :table-special, :link,
  20556. and :keyword."
  20557. (let* ((f (get-text-property (point) 'face))
  20558. (faces (if (listp f) f (list f)))
  20559. (case-fold-search t)
  20560. (p (point)) clist o)
  20561. ;; First the large context
  20562. (cond
  20563. ((org-at-heading-p t)
  20564. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  20565. (when (progn
  20566. (beginning-of-line 1)
  20567. (looking-at org-todo-line-tags-regexp))
  20568. (push (org-point-in-group p 1 :headline-stars) clist)
  20569. (push (org-point-in-group p 2 :todo-keyword) clist)
  20570. (push (org-point-in-group p 4 :tags) clist))
  20571. (goto-char p)
  20572. (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
  20573. (when (looking-at "\\[#[A-Z0-9]\\]")
  20574. (push (org-point-in-group p 0 :priority) clist)))
  20575. ((org-at-item-p)
  20576. (push (org-point-in-group p 2 :item-bullet) clist)
  20577. (push (list :item (point-at-bol)
  20578. (save-excursion (org-end-of-item) (point)))
  20579. clist)
  20580. (and (org-at-item-checkbox-p)
  20581. (push (org-point-in-group p 0 :checkbox) clist)))
  20582. ((org-at-table-p)
  20583. (push (list :table (org-table-begin) (org-table-end)) clist)
  20584. (when (memq 'org-formula faces)
  20585. (push (list :table-special
  20586. (previous-single-property-change p 'face)
  20587. (next-single-property-change p 'face)) clist)))
  20588. ((org-at-table-p 'any)
  20589. (push (list :table-table) clist)))
  20590. (goto-char p)
  20591. (let ((case-fold-search t))
  20592. ;; New the "medium" contexts: clocktables, source blocks
  20593. (cond ((org-in-clocktable-p)
  20594. (push (list :clocktable
  20595. (and (or (looking-at "[ \t]*\\(#\\+BEGIN: clocktable\\)")
  20596. (re-search-backward "[ \t]*\\(#+BEGIN: clocktable\\)" nil t))
  20597. (match-beginning 1))
  20598. (and (re-search-forward "[ \t]*#\\+END:?" nil t)
  20599. (match-end 0))) clist))
  20600. ((org-in-src-block-p)
  20601. (push (list :src-block
  20602. (and (or (looking-at "[ \t]*\\(#\\+BEGIN_SRC\\)")
  20603. (re-search-backward "[ \t]*\\(#+BEGIN_SRC\\)" nil t))
  20604. (match-beginning 1))
  20605. (and (search-forward "#+END_SRC" nil t)
  20606. (match-beginning 0))) clist))))
  20607. (goto-char p)
  20608. ;; Now the small context
  20609. (cond
  20610. ((org-at-timestamp-p)
  20611. (push (org-point-in-group p 0 :timestamp) clist))
  20612. ((memq 'org-link faces)
  20613. (push (list :link
  20614. (previous-single-property-change p 'face)
  20615. (next-single-property-change p 'face)) clist))
  20616. ((memq 'org-special-keyword faces)
  20617. (push (list :keyword
  20618. (previous-single-property-change p 'face)
  20619. (next-single-property-change p 'face)) clist))
  20620. ((org-at-target-p)
  20621. (push (org-point-in-group p 0 :target) clist)
  20622. (goto-char (1- (match-beginning 0)))
  20623. (when (looking-at org-radio-target-regexp)
  20624. (push (org-point-in-group p 0 :radio-target) clist))
  20625. (goto-char p))
  20626. ((setq o (cl-some
  20627. (lambda (o)
  20628. (and (eq (overlay-get o 'org-overlay-type) 'org-latex-overlay)
  20629. o))
  20630. (overlays-at (point))))
  20631. (push (list :latex-fragment
  20632. (overlay-start o) (overlay-end o)) clist)
  20633. (push (list :latex-preview
  20634. (overlay-start o) (overlay-end o)) clist))
  20635. ((org-inside-LaTeX-fragment-p)
  20636. ;; FIXME: positions wrong.
  20637. (push (list :latex-fragment (point) (point)) clist)))
  20638. (setq clist (nreverse (delq nil clist)))
  20639. clist))
  20640. (defun org-in-regexp (regexp &optional nlines visually)
  20641. "Check if point is inside a match of REGEXP.
  20642. Normally only the current line is checked, but you can include
  20643. NLINES extra lines around point into the search. If VISUALLY is
  20644. set, require that the cursor is not after the match but really
  20645. on, so that the block visually is on the match.
  20646. Return nil or a cons cell (BEG . END) where BEG and END are,
  20647. respectively, the positions at the beginning and the end of the
  20648. match."
  20649. (catch :exit
  20650. (let ((pos (point))
  20651. (eol (line-end-position (if nlines (1+ nlines) 1))))
  20652. (save-excursion
  20653. (beginning-of-line (- 1 (or nlines 0)))
  20654. (while (and (re-search-forward regexp eol t)
  20655. (<= (match-beginning 0) pos))
  20656. (let ((end (match-end 0)))
  20657. (when (or (> end pos) (and (= end pos) (not visually)))
  20658. (throw :exit (cons (match-beginning 0) (match-end 0))))))))))
  20659. (define-obsolete-function-alias 'org-at-regexp-p 'org-in-regexp
  20660. "Org mode 8.3")
  20661. (defun org-between-regexps-p (start-re end-re &optional lim-up lim-down)
  20662. "Non-nil when point is between matches of START-RE and END-RE.
  20663. Also return a non-nil value when point is on one of the matches.
  20664. Optional arguments LIM-UP and LIM-DOWN bound the search; they are
  20665. buffer positions. Default values are the positions of headlines
  20666. surrounding the point.
  20667. The functions returns a cons cell whose car (resp. cdr) is the
  20668. position before START-RE (resp. after END-RE)."
  20669. (save-match-data
  20670. (let ((pos (point))
  20671. (limit-up (or lim-up (save-excursion (outline-previous-heading))))
  20672. (limit-down (or lim-down (save-excursion (outline-next-heading))))
  20673. beg end)
  20674. (save-excursion
  20675. ;; Point is on a block when on START-RE or if START-RE can be
  20676. ;; found before it...
  20677. (and (or (org-in-regexp start-re)
  20678. (re-search-backward start-re limit-up t))
  20679. (setq beg (match-beginning 0))
  20680. ;; ... and END-RE after it...
  20681. (goto-char (match-end 0))
  20682. (re-search-forward end-re limit-down t)
  20683. (> (setq end (match-end 0)) pos)
  20684. ;; ... without another START-RE in-between.
  20685. (goto-char (match-beginning 0))
  20686. (not (re-search-backward start-re (1+ beg) t))
  20687. ;; Return value.
  20688. (cons beg end))))))
  20689. (defun org-in-block-p (names)
  20690. "Non-nil when point belongs to a block whose name belongs to NAMES.
  20691. NAMES is a list of strings containing names of blocks.
  20692. Return first block name matched, or nil. Beware that in case of
  20693. nested blocks, the returned name may not belong to the closest
  20694. block from point."
  20695. (save-match-data
  20696. (catch 'exit
  20697. (let ((case-fold-search t)
  20698. (lim-up (save-excursion (outline-previous-heading)))
  20699. (lim-down (save-excursion (outline-next-heading))))
  20700. (dolist (name names)
  20701. (let ((n (regexp-quote name)))
  20702. (when (org-between-regexps-p
  20703. (concat "^[ \t]*#\\+begin_" n)
  20704. (concat "^[ \t]*#\\+end_" n)
  20705. lim-up lim-down)
  20706. (throw 'exit n)))))
  20707. nil)))
  20708. (defun org-occur-in-agenda-files (regexp &optional _nlines)
  20709. "Call `multi-occur' with buffers for all agenda files."
  20710. (interactive "sOrg-files matching: ")
  20711. (let* ((files (org-agenda-files))
  20712. (tnames (mapcar #'file-truename files))
  20713. (extra org-agenda-text-search-extra-files))
  20714. (when (eq (car extra) 'agenda-archives)
  20715. (setq extra (cdr extra))
  20716. (setq files (org-add-archive-files files)))
  20717. (dolist (f extra)
  20718. (unless (member (file-truename f) tnames)
  20719. (unless (member f files) (setq files (append files (list f))))
  20720. (setq tnames (append tnames (list (file-truename f))))))
  20721. (multi-occur
  20722. (mapcar (lambda (x)
  20723. (with-current-buffer
  20724. ;; FIXME: Why not just (find-file-noselect x)?
  20725. ;; Is it to avoid the "revert buffer" prompt?
  20726. (or (get-file-buffer x) (find-file-noselect x))
  20727. (widen)
  20728. (current-buffer)))
  20729. files)
  20730. regexp)))
  20731. (if (boundp 'occur-mode-find-occurrence-hook)
  20732. ;; Emacs 23
  20733. (add-hook 'occur-mode-find-occurrence-hook
  20734. (lambda ()
  20735. (when (derived-mode-p 'org-mode)
  20736. (org-reveal))))
  20737. ;; Emacs 22
  20738. (defadvice occur-mode-goto-occurrence
  20739. (after org-occur-reveal activate)
  20740. (and (derived-mode-p 'org-mode) (org-reveal)))
  20741. (defadvice occur-mode-goto-occurrence-other-window
  20742. (after org-occur-reveal activate)
  20743. (and (derived-mode-p 'org-mode) (org-reveal)))
  20744. (defadvice occur-mode-display-occurrence
  20745. (after org-occur-reveal activate)
  20746. (when (derived-mode-p 'org-mode)
  20747. (let ((pos (occur-mode-find-occurrence)))
  20748. (with-current-buffer (marker-buffer pos)
  20749. (save-excursion
  20750. (goto-char pos)
  20751. (org-reveal)))))))
  20752. (defun org-occur-link-in-agenda-files ()
  20753. "Create a link and search for it in the agendas.
  20754. The link is not stored in `org-stored-links', it is just created
  20755. for the search purpose."
  20756. (interactive)
  20757. (let ((link (condition-case nil
  20758. (org-store-link nil)
  20759. (error "Unable to create a link to here"))))
  20760. (org-occur-in-agenda-files (regexp-quote link))))
  20761. (defun org-reverse-string (string)
  20762. "Return the reverse of STRING."
  20763. (apply 'string (reverse (string-to-list string))))
  20764. ;; defsubst org-uniquify must be defined before first use
  20765. (defun org-uniquify-alist (alist)
  20766. "Merge elements of ALIST with the same key.
  20767. For example, in this alist:
  20768. \(org-uniquify-alist \\='((a 1) (b 2) (a 3)))
  20769. => \\='((a 1 3) (b 2))
  20770. merge (a 1) and (a 3) into (a 1 3).
  20771. The function returns the new ALIST."
  20772. (let (rtn)
  20773. (dolist (e alist rtn)
  20774. (let (n)
  20775. (if (not (assoc (car e) rtn))
  20776. (push e rtn)
  20777. (setq n (cons (car e) (append (cdr (assoc (car e) rtn)) (cdr e))))
  20778. (setq rtn (assq-delete-all (car e) rtn))
  20779. (push n rtn))))))
  20780. (defun org-delete-all (elts list)
  20781. "Remove all elements in ELTS from LIST."
  20782. (while elts
  20783. (setq list (delete (pop elts) list)))
  20784. list)
  20785. (defun org-back-over-empty-lines ()
  20786. "Move backwards over whitespace, to the beginning of the first empty line.
  20787. Returns the number of empty lines passed."
  20788. (let ((pos (point)))
  20789. (if (cdr (assoc 'heading org-blank-before-new-entry))
  20790. (skip-chars-backward " \t\n\r")
  20791. (unless (eobp)
  20792. (forward-line -1)))
  20793. (beginning-of-line 2)
  20794. (goto-char (min (point) pos))
  20795. (count-lines (point) pos)))
  20796. (defun org-skip-whitespace ()
  20797. (skip-chars-forward " \t\n\r"))
  20798. (defun org-point-in-group (point group &optional context)
  20799. "Check if POINT is in match-group GROUP.
  20800. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  20801. match. If the match group does not exist or point is not inside it,
  20802. return nil."
  20803. (and (match-beginning group)
  20804. (>= point (match-beginning group))
  20805. (<= point (match-end group))
  20806. (if context
  20807. (list context (match-beginning group) (match-end group))
  20808. t)))
  20809. (defun org-switch-to-buffer-other-window (&rest args)
  20810. "Switch to buffer in a second window on the current frame.
  20811. In particular, do not allow pop-up frames.
  20812. Returns the newly created buffer."
  20813. (org-no-popups
  20814. (apply 'switch-to-buffer-other-window args)))
  20815. (defun org-combine-plists (&rest plists)
  20816. "Create a single property list from all plists in PLISTS.
  20817. The process starts by copying the first list, and then setting properties
  20818. from the other lists. Settings in the last list are the most significant
  20819. ones and overrule settings in the other lists."
  20820. (let ((rtn (copy-sequence (pop plists)))
  20821. p v ls)
  20822. (while plists
  20823. (setq ls (pop plists))
  20824. (while ls
  20825. (setq p (pop ls) v (pop ls))
  20826. (setq rtn (plist-put rtn p v))))
  20827. rtn))
  20828. (defun org-replace-escapes (string table)
  20829. "Replace %-escapes in STRING with values in TABLE.
  20830. TABLE is an association list with keys like \"%a\" and string values.
  20831. The sequences in STRING may contain normal field width and padding information,
  20832. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  20833. so values can contain further %-escapes if they are define later in TABLE."
  20834. (let ((tbl (copy-alist table))
  20835. (case-fold-search nil)
  20836. (pchg 0)
  20837. re rpl)
  20838. (dolist (e tbl)
  20839. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  20840. (when (and (cdr e) (string-match re (cdr e)))
  20841. (let ((sref (substring (cdr e) (match-beginning 0) (match-end 0)))
  20842. (safe "SREF"))
  20843. (add-text-properties 0 3 (list 'sref sref) safe)
  20844. (setcdr e (replace-match safe t t (cdr e)))))
  20845. (while (string-match re string)
  20846. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  20847. (cdr e)))
  20848. (setq string (replace-match rpl t t string))))
  20849. (while (setq pchg (next-property-change pchg string))
  20850. (let ((sref (get-text-property pchg 'sref string)))
  20851. (when (and sref (string-match "SREF" string pchg))
  20852. (setq string (replace-match sref t t string)))))
  20853. string))
  20854. (defun org-sublist (list start end)
  20855. "Return a section of LIST, from START to END.
  20856. Counting starts at 1."
  20857. (cl-subseq list (1- start) end))
  20858. (make-obsolete 'org-sublist "cl-subseq (note the 0-based counting)." "Org 9.0")
  20859. (defun org-find-base-buffer-visiting (file)
  20860. "Like `find-buffer-visiting' but always return the base buffer and
  20861. not an indirect buffer."
  20862. (let ((buf (or (get-file-buffer file)
  20863. (find-buffer-visiting file))))
  20864. (if buf
  20865. (or (buffer-base-buffer buf) buf)
  20866. nil)))
  20867. (define-obsolete-function-alias 'org-image-file-name-regexp 'image-file-name-regexp
  20868. "Org 9.0")
  20869. ;;; TODO: Only called once, from ox-odt which should probably use
  20870. ;;; org-export-inline-image-p or something.
  20871. (defun org-file-image-p (file)
  20872. "Return non-nil if FILE is an image."
  20873. (save-match-data
  20874. (string-match (image-file-name-regexp) file)))
  20875. (defun org-get-cursor-date (&optional with-time)
  20876. "Return the date at cursor in as a time.
  20877. This works in the calendar and in the agenda, anywhere else it just
  20878. returns the current time.
  20879. If WITH-TIME is non-nil, returns the time of the event at point (in
  20880. the agenda) or the current time of the day."
  20881. (let (date day defd tp hod mod)
  20882. (when with-time
  20883. (setq tp (get-text-property (point) 'time))
  20884. (when (and tp (string-match "\\([0-9][0-9]\\):\\([0-9][0-9]\\)" tp))
  20885. (setq hod (string-to-number (match-string 1 tp))
  20886. mod (string-to-number (match-string 2 tp))))
  20887. (or tp (let ((now (decode-time)))
  20888. (setq hod (nth 2 now)
  20889. mod (nth 1 now)))))
  20890. (cond
  20891. ((eq major-mode 'calendar-mode)
  20892. (setq date (calendar-cursor-to-date)
  20893. defd (encode-time 0 (or mod 0) (or hod 0)
  20894. (nth 1 date) (nth 0 date) (nth 2 date))))
  20895. ((eq major-mode 'org-agenda-mode)
  20896. (setq day (get-text-property (point) 'day))
  20897. (when day
  20898. (setq date (calendar-gregorian-from-absolute day)
  20899. defd (encode-time 0 (or mod 0) (or hod 0)
  20900. (nth 1 date) (nth 0 date) (nth 2 date))))))
  20901. (or defd (current-time))))
  20902. (defun org-mark-subtree (&optional up)
  20903. "Mark the current subtree.
  20904. This puts point at the start of the current subtree, and mark at
  20905. the end. If a numeric prefix UP is given, move up into the
  20906. hierarchy of headlines by UP levels before marking the subtree."
  20907. (interactive "P")
  20908. (org-with-limited-levels
  20909. (cond ((org-at-heading-p) (beginning-of-line))
  20910. ((org-before-first-heading-p) (user-error "Not in a subtree"))
  20911. (t (outline-previous-visible-heading 1))))
  20912. (when up (while (and (> up 0) (org-up-heading-safe)) (cl-decf up)))
  20913. (if (org-called-interactively-p 'any)
  20914. (call-interactively 'org-mark-element)
  20915. (org-mark-element)))
  20916. ;;; Indentation
  20917. (defun org--get-expected-indentation (element contentsp)
  20918. "Expected indentation column for current line, according to ELEMENT.
  20919. ELEMENT is an element containing point. CONTENTSP is non-nil
  20920. when indentation is to be computed according to contents of
  20921. ELEMENT."
  20922. (let ((type (org-element-type element))
  20923. (start (org-element-property :begin element))
  20924. (post-affiliated (org-element-property :post-affiliated element)))
  20925. (org-with-wide-buffer
  20926. (cond
  20927. (contentsp
  20928. (cl-case type
  20929. ((diary-sexp footnote-definition) 0)
  20930. ((headline inlinetask nil)
  20931. (if (not org-adapt-indentation) 0
  20932. (let ((level (org-current-level)))
  20933. (if level (1+ level) 0))))
  20934. ((item plain-list) (org-list-item-body-column post-affiliated))
  20935. (t
  20936. (goto-char start)
  20937. (org-get-indentation))))
  20938. ((memq type '(headline inlinetask nil))
  20939. (if (save-excursion (beginning-of-line) (looking-at "[ \t]*$"))
  20940. (org--get-expected-indentation element t)
  20941. 0))
  20942. ((memq type '(diary-sexp footnote-definition)) 0)
  20943. ;; First paragraph of a footnote definition or an item.
  20944. ;; Indent like parent.
  20945. ((< (line-beginning-position) start)
  20946. (org--get-expected-indentation
  20947. (org-element-property :parent element) t))
  20948. ;; At first line: indent according to previous sibling, if any,
  20949. ;; ignoring footnote definitions and inline tasks, or parent's
  20950. ;; contents.
  20951. ((= (line-beginning-position) start)
  20952. (catch 'exit
  20953. (while t
  20954. (if (= (point-min) start) (throw 'exit 0)
  20955. (goto-char (1- start))
  20956. (let* ((previous (org-element-at-point))
  20957. (parent previous))
  20958. (while (and parent (<= (org-element-property :end parent) start))
  20959. (setq previous parent
  20960. parent (org-element-property :parent parent)))
  20961. (cond
  20962. ((not previous) (throw 'exit 0))
  20963. ((> (org-element-property :end previous) start)
  20964. (throw 'exit (org--get-expected-indentation previous t)))
  20965. ((memq (org-element-type previous)
  20966. '(footnote-definition inlinetask))
  20967. (setq start (org-element-property :begin previous)))
  20968. (t (goto-char (org-element-property :begin previous))
  20969. (throw 'exit
  20970. (if (bolp) (org-get-indentation)
  20971. ;; At first paragraph in an item or
  20972. ;; a footnote definition.
  20973. (org--get-expected-indentation
  20974. (org-element-property :parent previous) t))))))))))
  20975. ;; Otherwise, move to the first non-blank line above.
  20976. (t
  20977. (beginning-of-line)
  20978. (let ((pos (point)))
  20979. (skip-chars-backward " \r\t\n")
  20980. (cond
  20981. ;; Two blank lines end a footnote definition or a plain
  20982. ;; list. When we indent an empty line after them, the
  20983. ;; containing list or footnote definition is over, so it
  20984. ;; qualifies as a previous sibling. Therefore, we indent
  20985. ;; like its first line.
  20986. ((and (memq type '(footnote-definition plain-list))
  20987. (> (count-lines (point) pos) 2))
  20988. (goto-char start)
  20989. (org-get-indentation))
  20990. ;; Line above is the first one of a paragraph at the
  20991. ;; beginning of an item or a footnote definition. Indent
  20992. ;; like parent.
  20993. ((< (line-beginning-position) start)
  20994. (org--get-expected-indentation
  20995. (org-element-property :parent element) t))
  20996. ;; Line above is the beginning of an element, i.e., point
  20997. ;; was originally on the blank lines between element's start
  20998. ;; and contents.
  20999. ((= (line-beginning-position) post-affiliated)
  21000. (org--get-expected-indentation element t))
  21001. ;; POS is after contents in a greater element. Indent like
  21002. ;; the beginning of the element.
  21003. ;;
  21004. ;; As a special case, if point is at the end of a footnote
  21005. ;; definition or an item, indent like the very last element
  21006. ;; within. If that last element is an item, indent like its
  21007. ;; contents.
  21008. ((and (not (eq type 'paragraph))
  21009. (let ((cend (org-element-property :contents-end element)))
  21010. (and cend (<= cend pos))))
  21011. (if (memq type '(footnote-definition item plain-list))
  21012. (let ((last (org-element-at-point)))
  21013. (org--get-expected-indentation
  21014. last (eq (org-element-type last) 'item)))
  21015. (goto-char start)
  21016. (org-get-indentation)))
  21017. ;; In any other case, indent like the current line.
  21018. (t (org-get-indentation)))))))))
  21019. (defun org--align-node-property ()
  21020. "Align node property at point.
  21021. Alignment is done according to `org-property-format', which see."
  21022. (when (save-excursion
  21023. (beginning-of-line)
  21024. (looking-at org-property-re))
  21025. (replace-match
  21026. (concat (match-string 4)
  21027. (org-trim
  21028. (format org-property-format (match-string 1) (match-string 3))))
  21029. t t)))
  21030. (defun org-indent-line ()
  21031. "Indent line depending on context.
  21032. Indentation is done according to the following rules:
  21033. - Footnote definitions, diary sexps, headlines and inline tasks
  21034. have to start at column 0.
  21035. - On the very first line of an element, consider, in order, the
  21036. next rules until one matches:
  21037. 1. If there's a sibling element before, ignoring footnote
  21038. definitions and inline tasks, indent like its first line.
  21039. 2. If element has a parent, indent like its contents. More
  21040. precisely, if parent is an item, indent after the
  21041. description part, if any, or the bullet (see
  21042. `org-list-description-max-indent'). Else, indent like
  21043. parent's first line.
  21044. 3. Otherwise, indent relatively to current level, if
  21045. `org-adapt-indentation' is non-nil, or to left margin.
  21046. - On a blank line at the end of an element, indent according to
  21047. the type of the element. More precisely
  21048. 1. If element is a plain list, an item, or a footnote
  21049. definition, indent like the very last element within.
  21050. 2. If element is a paragraph, indent like its last non blank
  21051. line.
  21052. 3. Otherwise, indent like its very first line.
  21053. - In the code part of a source block, use language major mode
  21054. to indent current line if `org-src-tab-acts-natively' is
  21055. non-nil. If it is nil, do nothing.
  21056. - Otherwise, indent like the first non-blank line above.
  21057. The function doesn't indent an item as it could break the whole
  21058. list structure. Instead, use \\<org-mode-map>\\[org-shiftmetaleft] or \
  21059. \\[org-shiftmetaright].
  21060. Also align node properties according to `org-property-format'."
  21061. (interactive)
  21062. (cond
  21063. (orgstruct-is-++
  21064. (let ((indent-line-function
  21065. (cl-cadadr (assq 'indent-line-function org-fb-vars))))
  21066. (indent-according-to-mode)))
  21067. ((org-at-heading-p) 'noindent)
  21068. (t
  21069. (let* ((element (save-excursion (beginning-of-line) (org-element-at-point)))
  21070. (type (org-element-type element)))
  21071. (cond ((and (memq type '(plain-list item))
  21072. (= (line-beginning-position)
  21073. (org-element-property :post-affiliated element)))
  21074. 'noindent)
  21075. ((and (eq type 'src-block)
  21076. org-src-tab-acts-natively
  21077. (> (line-beginning-position)
  21078. (org-element-property :post-affiliated element))
  21079. (< (line-beginning-position)
  21080. (org-with-wide-buffer
  21081. (goto-char (org-element-property :end element))
  21082. (skip-chars-backward " \r\t\n")
  21083. (line-beginning-position))))
  21084. (org-babel-do-key-sequence-in-edit-buffer (kbd "TAB")))
  21085. (t
  21086. (let ((column (org--get-expected-indentation element nil)))
  21087. ;; Preserve current column.
  21088. (if (<= (current-column) (current-indentation))
  21089. (org-indent-line-to column)
  21090. (save-excursion (org-indent-line-to column))))
  21091. ;; Align node property. Also preserve current column.
  21092. (when (eq type 'node-property)
  21093. (let ((column (current-column)))
  21094. (org--align-node-property)
  21095. (org-move-to-column column)))))))))
  21096. (defun org-indent-region (start end)
  21097. "Indent each non-blank line in the region.
  21098. Called from a program, START and END specify the region to
  21099. indent. The function will not indent contents of example blocks,
  21100. verse blocks and export blocks as leading white spaces are
  21101. assumed to be significant there."
  21102. (interactive "r")
  21103. (save-excursion
  21104. (goto-char start)
  21105. (skip-chars-forward " \r\t\n")
  21106. (unless (eobp) (beginning-of-line))
  21107. (let ((indent-to
  21108. (lambda (ind pos)
  21109. ;; Set IND as indentation for all lines between point and
  21110. ;; POS. Blank lines are ignored. Leave point after POS
  21111. ;; once done.
  21112. (let ((limit (copy-marker pos)))
  21113. (while (< (point) limit)
  21114. (unless (org-looking-at-p "[ \t]*$") (org-indent-line-to ind))
  21115. (forward-line))
  21116. (set-marker limit nil))))
  21117. (end (copy-marker end)))
  21118. (while (< (point) end)
  21119. (if (or (org-looking-at-p " \r\t\n") (org-at-heading-p)) (forward-line)
  21120. (let* ((element (org-element-at-point))
  21121. (type (org-element-type element))
  21122. (element-end (copy-marker (org-element-property :end element)))
  21123. (ind (org--get-expected-indentation element nil)))
  21124. (cond
  21125. ((or (memq type '(paragraph table table-row))
  21126. (not (or (org-element-property :contents-begin element)
  21127. (memq type
  21128. '(example-block export-block src-block)))))
  21129. ;; Elements here are indented as a single block. Also
  21130. ;; align node properties.
  21131. (when (eq type 'node-property)
  21132. (org--align-node-property)
  21133. (beginning-of-line))
  21134. (funcall indent-to ind (min element-end end)))
  21135. (t
  21136. ;; Elements in this category consist of three parts:
  21137. ;; before the contents, the contents, and after the
  21138. ;; contents. The contents are treated specially,
  21139. ;; according to the element type, or not indented at
  21140. ;; all. Other parts are indented as a single block.
  21141. (let* ((post (copy-marker
  21142. (org-element-property :post-affiliated element)))
  21143. (cbeg
  21144. (copy-marker
  21145. (cond
  21146. ((not (org-element-property :contents-begin element))
  21147. ;; Fake contents for source blocks.
  21148. (org-with-wide-buffer
  21149. (goto-char post)
  21150. (forward-line)
  21151. (point)))
  21152. ((memq type '(footnote-definition item plain-list))
  21153. ;; Contents in these elements could start on
  21154. ;; the same line as the beginning of the
  21155. ;; element. Make sure we start indenting
  21156. ;; from the second line.
  21157. (org-with-wide-buffer
  21158. (goto-char post)
  21159. (end-of-line)
  21160. (skip-chars-forward " \r\t\n")
  21161. (if (eobp) (point) (line-beginning-position))))
  21162. (t (org-element-property :contents-begin element)))))
  21163. (cend (copy-marker
  21164. (or (org-element-property :contents-end element)
  21165. ;; Fake contents for source blocks.
  21166. (org-with-wide-buffer
  21167. (goto-char element-end)
  21168. (skip-chars-backward " \r\t\n")
  21169. (line-beginning-position)))
  21170. t)))
  21171. ;; Do not change items indentation individually as it
  21172. ;; might break the list as a whole. On the other
  21173. ;; hand, when at a plain list, indent it as a whole.
  21174. (cond ((eq type 'plain-list)
  21175. (let ((offset (- ind (org-get-indentation))))
  21176. (unless (zerop offset)
  21177. (indent-rigidly (org-element-property :begin element)
  21178. (org-element-property :end element)
  21179. offset))
  21180. (goto-char cbeg)))
  21181. ((eq type 'item) (goto-char cbeg))
  21182. (t (funcall indent-to ind (min cbeg end))))
  21183. (when (< (point) end)
  21184. (cl-case type
  21185. ((example-block export-block verse-block))
  21186. (src-block
  21187. ;; In a source block, indent source code
  21188. ;; according to language major mode, but only if
  21189. ;; `org-src-tab-acts-natively' is non-nil.
  21190. (when (and (< (point) end) org-src-tab-acts-natively)
  21191. (ignore-errors
  21192. (org-babel-do-in-edit-buffer
  21193. (indent-region (point-min) (point-max))))))
  21194. (t (org-indent-region (point) (min cend end))))
  21195. (goto-char (min cend end))
  21196. (when (< (point) end)
  21197. (funcall indent-to ind (min element-end end))))
  21198. (set-marker post nil)
  21199. (set-marker cbeg nil)
  21200. (set-marker cend nil))))
  21201. (set-marker element-end nil))))
  21202. (set-marker end nil))))
  21203. (defun org-indent-drawer ()
  21204. "Indent the drawer at point."
  21205. (interactive)
  21206. (unless (save-excursion
  21207. (beginning-of-line)
  21208. (org-looking-at-p org-drawer-regexp))
  21209. (user-error "Not at a drawer"))
  21210. (let ((element (org-element-at-point)))
  21211. (unless (memq (org-element-type element) '(drawer property-drawer))
  21212. (user-error "Not at a drawer"))
  21213. (org-with-wide-buffer
  21214. (org-indent-region (org-element-property :begin element)
  21215. (org-element-property :end element))))
  21216. (message "Drawer at point indented"))
  21217. (defun org-indent-block ()
  21218. "Indent the block at point."
  21219. (interactive)
  21220. (unless (save-excursion
  21221. (beginning-of-line)
  21222. (let ((case-fold-search t))
  21223. (org-looking-at-p "[ \t]*#\\+\\(begin\\|end\\)_")))
  21224. (user-error "Not at a block"))
  21225. (let ((element (org-element-at-point)))
  21226. (unless (memq (org-element-type element)
  21227. '(comment-block center-block dynamic-block example-block
  21228. export-block quote-block special-block
  21229. src-block verse-block))
  21230. (user-error "Not at a block"))
  21231. (org-with-wide-buffer
  21232. (org-indent-region (org-element-property :begin element)
  21233. (org-element-property :end element))))
  21234. (message "Block at point indented"))
  21235. ;;; Filling
  21236. ;; We use our own fill-paragraph and auto-fill functions.
  21237. ;; `org-fill-paragraph' relies on adaptive filling and context
  21238. ;; checking. Appropriate `fill-prefix' is computed with
  21239. ;; `org-adaptive-fill-function'.
  21240. ;; `org-auto-fill-function' takes care of auto-filling. It calls
  21241. ;; `do-auto-fill' only on valid areas with `fill-prefix' shadowed with
  21242. ;; `org-adaptive-fill-function' value. Internally,
  21243. ;; `org-comment-line-break-function' breaks the line.
  21244. ;; `org-setup-filling' installs filling and auto-filling related
  21245. ;; variables during `org-mode' initialization.
  21246. (defvar org-element-paragraph-separate) ; org-element.el
  21247. (defun org-setup-filling ()
  21248. (require 'org-element)
  21249. ;; Prevent auto-fill from inserting unwanted new items.
  21250. (when (boundp 'fill-nobreak-predicate)
  21251. (setq-local
  21252. fill-nobreak-predicate
  21253. (org-uniquify
  21254. (append fill-nobreak-predicate
  21255. '(org-fill-line-break-nobreak-p
  21256. org-fill-paragraph-with-timestamp-nobreak-p)))))
  21257. (let ((paragraph-ending (substring org-element-paragraph-separate 1)))
  21258. (setq-local paragraph-start paragraph-ending)
  21259. (setq-local paragraph-separate paragraph-ending))
  21260. (setq-local fill-paragraph-function 'org-fill-paragraph)
  21261. (setq-local auto-fill-inhibit-regexp nil)
  21262. (setq-local adaptive-fill-function 'org-adaptive-fill-function)
  21263. (setq-local normal-auto-fill-function 'org-auto-fill-function)
  21264. (setq-local comment-line-break-function 'org-comment-line-break-function))
  21265. (defun org-fill-line-break-nobreak-p ()
  21266. "Non-nil when a new line at point would create an Org line break."
  21267. (save-excursion
  21268. (skip-chars-backward "[ \t]")
  21269. (skip-chars-backward "\\\\")
  21270. (looking-at "\\\\\\\\\\($\\|[^\\\\]\\)")))
  21271. (defun org-fill-paragraph-with-timestamp-nobreak-p ()
  21272. "Non-nil when a new line at point would split a timestamp."
  21273. (and (org-at-timestamp-p t)
  21274. (not (looking-at org-ts-regexp-both))))
  21275. (declare-function message-in-body-p "message" ())
  21276. (defvar orgtbl-line-start-regexp) ; From org-table.el
  21277. (defun org-adaptive-fill-function ()
  21278. "Compute a fill prefix for the current line.
  21279. Return fill prefix, as a string, or nil if current line isn't
  21280. meant to be filled. For convenience, if `adaptive-fill-regexp'
  21281. matches in paragraphs or comments, use it."
  21282. (catch 'exit
  21283. (when (derived-mode-p 'message-mode)
  21284. (save-excursion
  21285. (beginning-of-line)
  21286. (cond ((not (message-in-body-p)) (throw 'exit nil))
  21287. ((org-looking-at-p org-table-line-regexp) (throw 'exit nil))
  21288. ((looking-at message-cite-prefix-regexp)
  21289. (throw 'exit (match-string-no-properties 0)))
  21290. ((looking-at org-outline-regexp)
  21291. (throw 'exit (make-string (length (match-string 0)) ?\s))))))
  21292. (org-with-wide-buffer
  21293. (unless (org-at-heading-p)
  21294. (let* ((p (line-beginning-position))
  21295. (element (save-excursion
  21296. (beginning-of-line)
  21297. (org-element-at-point)))
  21298. (type (org-element-type element))
  21299. (post-affiliated (org-element-property :post-affiliated element)))
  21300. (unless (< p post-affiliated)
  21301. (cl-case type
  21302. (comment
  21303. (save-excursion
  21304. (beginning-of-line)
  21305. (looking-at "[ \t]*")
  21306. (concat (match-string 0) "# ")))
  21307. (footnote-definition "")
  21308. ((item plain-list)
  21309. (make-string (org-list-item-body-column post-affiliated) ?\s))
  21310. (paragraph
  21311. ;; Fill prefix is usually the same as the current line,
  21312. ;; unless the paragraph is at the beginning of an item.
  21313. (let ((parent (org-element-property :parent element)))
  21314. (save-excursion
  21315. (beginning-of-line)
  21316. (cond ((eq (org-element-type parent) 'item)
  21317. (make-string (org-list-item-body-column
  21318. (org-element-property :begin parent))
  21319. ?\s))
  21320. ((and adaptive-fill-regexp
  21321. ;; Locally disable
  21322. ;; `adaptive-fill-function' to let
  21323. ;; `fill-context-prefix' handle
  21324. ;; `adaptive-fill-regexp' variable.
  21325. (let (adaptive-fill-function)
  21326. (fill-context-prefix
  21327. post-affiliated
  21328. (org-element-property :end element)))))
  21329. ((looking-at "[ \t]+") (match-string 0))
  21330. (t "")))))
  21331. (comment-block
  21332. ;; Only fill contents if P is within block boundaries.
  21333. (let* ((cbeg (save-excursion (goto-char post-affiliated)
  21334. (forward-line)
  21335. (point)))
  21336. (cend (save-excursion
  21337. (goto-char (org-element-property :end element))
  21338. (skip-chars-backward " \r\t\n")
  21339. (line-beginning-position))))
  21340. (when (and (>= p cbeg) (< p cend))
  21341. (if (save-excursion (beginning-of-line) (looking-at "[ \t]+"))
  21342. (match-string 0)
  21343. "")))))))))))
  21344. (declare-function message-goto-body "message" ())
  21345. (defvar message-cite-prefix-regexp) ; From message.el
  21346. (defun org-fill-paragraph (&optional justify)
  21347. "Fill element at point, when applicable.
  21348. This function only applies to comment blocks, comments, example
  21349. blocks and paragraphs. Also, as a special case, re-align table
  21350. when point is at one.
  21351. If JUSTIFY is non-nil (interactively, with prefix argument),
  21352. justify as well. If `sentence-end-double-space' is non-nil, then
  21353. period followed by one space does not end a sentence, so don't
  21354. break a line there. The variable `fill-column' controls the
  21355. width for filling.
  21356. For convenience, when point is at a plain list, an item or
  21357. a footnote definition, try to fill the first paragraph within."
  21358. (interactive)
  21359. (if (and (derived-mode-p 'message-mode)
  21360. (or (not (message-in-body-p))
  21361. (save-excursion (move-beginning-of-line 1)
  21362. (looking-at message-cite-prefix-regexp))))
  21363. ;; First ensure filling is correct in message-mode.
  21364. (let ((fill-paragraph-function
  21365. (cl-cadadr (assq 'fill-paragraph-function org-fb-vars)))
  21366. (fill-prefix (cl-cadadr (assq 'fill-prefix org-fb-vars)))
  21367. (paragraph-start (cl-cadadr (assq 'paragraph-start org-fb-vars)))
  21368. (paragraph-separate
  21369. (cl-cadadr (assq 'paragraph-separate org-fb-vars))))
  21370. (fill-paragraph nil))
  21371. (with-syntax-table org-mode-transpose-word-syntax-table
  21372. ;; Move to end of line in order to get the first paragraph
  21373. ;; within a plain list or a footnote definition.
  21374. (let ((element (save-excursion
  21375. (end-of-line)
  21376. (or (ignore-errors (org-element-at-point))
  21377. (user-error "An element cannot be parsed line %d"
  21378. (line-number-at-pos (point)))))))
  21379. ;; First check if point is in a blank line at the beginning of
  21380. ;; the buffer. In that case, ignore filling.
  21381. (cl-case (org-element-type element)
  21382. ;; Use major mode filling function is src blocks.
  21383. (src-block (org-babel-do-key-sequence-in-edit-buffer (kbd "M-q")))
  21384. ;; Align Org tables, leave table.el tables as-is.
  21385. (table-row (org-table-align) t)
  21386. (table
  21387. (when (eq (org-element-property :type element) 'org)
  21388. (save-excursion
  21389. (goto-char (org-element-property :post-affiliated element))
  21390. (org-table-align)))
  21391. t)
  21392. (paragraph
  21393. ;; Paragraphs may contain `line-break' type objects.
  21394. (let ((beg (max (point-min)
  21395. (org-element-property :contents-begin element)))
  21396. (end (min (point-max)
  21397. (org-element-property :contents-end element))))
  21398. ;; Do nothing if point is at an affiliated keyword.
  21399. (if (< (line-end-position) beg) t
  21400. (when (derived-mode-p 'message-mode)
  21401. ;; In `message-mode', do not fill following citation
  21402. ;; in current paragraph nor text before message body.
  21403. (let ((body-start (save-excursion (message-goto-body))))
  21404. (when body-start (setq beg (max body-start beg))))
  21405. (when (save-excursion
  21406. (re-search-forward
  21407. (concat "^" message-cite-prefix-regexp) end t))
  21408. (setq end (match-beginning 0))))
  21409. ;; Fill paragraph, taking line breaks into account.
  21410. (save-excursion
  21411. (goto-char beg)
  21412. (let ((cuts (list beg)))
  21413. (while (re-search-forward "\\\\\\\\[ \t]*\n" end t)
  21414. (when (eq 'line-break
  21415. (org-element-type
  21416. (save-excursion (backward-char)
  21417. (org-element-context))))
  21418. (push (point) cuts)))
  21419. (dolist (c (delq end cuts))
  21420. (fill-region-as-paragraph c end justify)
  21421. (setq end c))))
  21422. t)))
  21423. ;; Contents of `comment-block' type elements should be
  21424. ;; filled as plain text, but only if point is within block
  21425. ;; markers.
  21426. (comment-block
  21427. (let* ((case-fold-search t)
  21428. (beg (save-excursion
  21429. (goto-char (org-element-property :begin element))
  21430. (re-search-forward "^[ \t]*#\\+begin_comment" nil t)
  21431. (forward-line)
  21432. (point)))
  21433. (end (save-excursion
  21434. (goto-char (org-element-property :end element))
  21435. (re-search-backward "^[ \t]*#\\+end_comment" nil t)
  21436. (line-beginning-position))))
  21437. (if (or (< (point) beg) (> (point) end)) t
  21438. (fill-region-as-paragraph
  21439. (save-excursion (end-of-line)
  21440. (re-search-backward "^[ \t]*$" beg 'move)
  21441. (line-beginning-position))
  21442. (save-excursion (beginning-of-line)
  21443. (re-search-forward "^[ \t]*$" end 'move)
  21444. (line-beginning-position))
  21445. justify))))
  21446. ;; Fill comments.
  21447. (comment
  21448. (let ((begin (org-element-property :post-affiliated element))
  21449. (end (org-element-property :end element)))
  21450. (when (and (>= (point) begin) (<= (point) end))
  21451. (let ((begin (save-excursion
  21452. (end-of-line)
  21453. (if (re-search-backward "^[ \t]*#[ \t]*$" begin t)
  21454. (progn (forward-line) (point))
  21455. begin)))
  21456. (end (save-excursion
  21457. (end-of-line)
  21458. (if (re-search-forward "^[ \t]*#[ \t]*$" end 'move)
  21459. (1- (line-beginning-position))
  21460. (skip-chars-backward " \r\t\n")
  21461. (line-end-position)))))
  21462. ;; Do not fill comments when at a blank line.
  21463. (when (> end begin)
  21464. (let ((fill-prefix
  21465. (save-excursion
  21466. (beginning-of-line)
  21467. (looking-at "[ \t]*#")
  21468. (let ((comment-prefix (match-string 0)))
  21469. (goto-char (match-end 0))
  21470. (if (looking-at adaptive-fill-regexp)
  21471. (concat comment-prefix (match-string 0))
  21472. (concat comment-prefix " "))))))
  21473. (save-excursion
  21474. (fill-region-as-paragraph begin end justify))))))
  21475. t))
  21476. ;; Ignore every other element.
  21477. (otherwise t))))))
  21478. (defun org-auto-fill-function ()
  21479. "Auto-fill function."
  21480. ;; Check if auto-filling is meaningful.
  21481. (let ((fc (current-fill-column)))
  21482. (when (and fc (> (current-column) fc))
  21483. (let* ((fill-prefix (org-adaptive-fill-function))
  21484. ;; Enforce empty fill prefix, if required. Otherwise, it
  21485. ;; will be computed again.
  21486. (adaptive-fill-mode (not (equal fill-prefix ""))))
  21487. (when fill-prefix (do-auto-fill))))))
  21488. (defun org-comment-line-break-function (&optional soft)
  21489. "Break line at point and indent, continuing comment if within one.
  21490. The inserted newline is marked hard if variable
  21491. `use-hard-newlines' is true, unless optional argument SOFT is
  21492. non-nil."
  21493. (if soft (insert-and-inherit ?\n) (newline 1))
  21494. (save-excursion (forward-char -1) (delete-horizontal-space))
  21495. (delete-horizontal-space)
  21496. (indent-to-left-margin)
  21497. (insert-before-markers-and-inherit fill-prefix))
  21498. ;;; Fixed Width Areas
  21499. (defun org-toggle-fixed-width ()
  21500. "Toggle fixed-width markup.
  21501. Add or remove fixed-width markup on current line, whenever it
  21502. makes sense. Return an error otherwise.
  21503. If a region is active and if it contains only fixed-width areas
  21504. or blank lines, remove all fixed-width markup in it. If the
  21505. region contains anything else, convert all non-fixed-width lines
  21506. to fixed-width ones.
  21507. Blank lines at the end of the region are ignored unless the
  21508. region only contains such lines."
  21509. (interactive)
  21510. (if (not (org-region-active-p))
  21511. ;; No region:
  21512. ;;
  21513. ;; Remove fixed width marker only in a fixed-with element.
  21514. ;;
  21515. ;; Add fixed width maker in paragraphs, in blank lines after
  21516. ;; elements or at the beginning of a headline or an inlinetask,
  21517. ;; and before any one-line elements (e.g., a clock).
  21518. (progn
  21519. (beginning-of-line)
  21520. (let* ((element (org-element-at-point))
  21521. (type (org-element-type element)))
  21522. (cond
  21523. ((and (eq type 'fixed-width)
  21524. (looking-at "[ \t]*\\(:\\(?: \\|$\\)\\)"))
  21525. (replace-match
  21526. "" nil nil nil (if (= (line-end-position) (match-end 0)) 0 1)))
  21527. ((and (memq type '(babel-call clock comment diary-sexp headline
  21528. horizontal-rule keyword paragraph
  21529. planning))
  21530. (<= (org-element-property :post-affiliated element) (point)))
  21531. (skip-chars-forward " \t")
  21532. (insert ": "))
  21533. ((and (org-looking-at-p "[ \t]*$")
  21534. (or (eq type 'inlinetask)
  21535. (save-excursion
  21536. (skip-chars-forward " \r\t\n")
  21537. (<= (org-element-property :end element) (point)))))
  21538. (delete-region (point) (line-end-position))
  21539. (org-indent-line)
  21540. (insert ": "))
  21541. (t (user-error "Cannot insert a fixed-width line here")))))
  21542. ;; Region active.
  21543. (let* ((begin (save-excursion
  21544. (goto-char (region-beginning))
  21545. (line-beginning-position)))
  21546. (end (copy-marker
  21547. (save-excursion
  21548. (goto-char (region-end))
  21549. (unless (eolp) (beginning-of-line))
  21550. (if (save-excursion (re-search-backward "\\S-" begin t))
  21551. (progn (skip-chars-backward " \r\t\n") (point))
  21552. (point)))))
  21553. (all-fixed-width-p
  21554. (catch 'not-all-p
  21555. (save-excursion
  21556. (goto-char begin)
  21557. (skip-chars-forward " \r\t\n")
  21558. (when (eobp) (throw 'not-all-p nil))
  21559. (while (< (point) end)
  21560. (let ((element (org-element-at-point)))
  21561. (if (eq (org-element-type element) 'fixed-width)
  21562. (goto-char (org-element-property :end element))
  21563. (throw 'not-all-p nil))))
  21564. t))))
  21565. (if all-fixed-width-p
  21566. (save-excursion
  21567. (goto-char begin)
  21568. (while (< (point) end)
  21569. (when (looking-at "[ \t]*\\(:\\(?: \\|$\\)\\)")
  21570. (replace-match
  21571. "" nil nil nil
  21572. (if (= (line-end-position) (match-end 0)) 0 1)))
  21573. (forward-line)))
  21574. (let ((min-ind (point-max)))
  21575. ;; Find minimum indentation across all lines.
  21576. (save-excursion
  21577. (goto-char begin)
  21578. (if (not (save-excursion (re-search-forward "\\S-" end t)))
  21579. (setq min-ind 0)
  21580. (catch 'zerop
  21581. (while (< (point) end)
  21582. (unless (org-looking-at-p "[ \t]*$")
  21583. (let ((ind (org-get-indentation)))
  21584. (setq min-ind (min min-ind ind))
  21585. (when (zerop ind) (throw 'zerop t))))
  21586. (forward-line)))))
  21587. ;; Loop over all lines and add fixed-width markup everywhere
  21588. ;; but in fixed-width lines.
  21589. (save-excursion
  21590. (goto-char begin)
  21591. (while (< (point) end)
  21592. (cond
  21593. ((org-at-heading-p)
  21594. (insert ": ")
  21595. (forward-line)
  21596. (while (and (< (point) end) (org-looking-at-p "[ \t]*$"))
  21597. (insert ":")
  21598. (forward-line)))
  21599. ((org-looking-at-p "[ \t]*:\\( \\|$\\)")
  21600. (let* ((element (org-element-at-point))
  21601. (element-end (org-element-property :end element)))
  21602. (if (eq (org-element-type element) 'fixed-width)
  21603. (progn (goto-char element-end)
  21604. (skip-chars-backward " \r\t\n")
  21605. (forward-line))
  21606. (let ((limit (min end element-end)))
  21607. (while (< (point) limit)
  21608. (org-move-to-column min-ind t)
  21609. (insert ": ")
  21610. (forward-line))))))
  21611. (t
  21612. (org-move-to-column min-ind t)
  21613. (insert ": ")
  21614. (forward-line)))))))
  21615. (set-marker end nil))))
  21616. ;;; Comments
  21617. ;; Org comments syntax is quite complex. It requires the entire line
  21618. ;; to be just a comment. Also, even with the right syntax at the
  21619. ;; beginning of line, some some elements (i.e. verse-block or
  21620. ;; example-block) don't accept comments. Usual Emacs comment commands
  21621. ;; cannot cope with those requirements. Therefore, Org replaces them.
  21622. ;; Org still relies on `comment-dwim', but cannot trust
  21623. ;; `comment-only-p'. So, `comment-region-function' and
  21624. ;; `uncomment-region-function' both point
  21625. ;; to`org-comment-or-uncomment-region'. Eventually,
  21626. ;; `org-insert-comment' takes care of insertion of comments at the
  21627. ;; beginning of line.
  21628. ;; `org-setup-comments-handling' install comments related variables
  21629. ;; during `org-mode' initialization.
  21630. (defun org-setup-comments-handling ()
  21631. (interactive)
  21632. (setq-local comment-use-syntax nil)
  21633. (setq-local comment-start "# ")
  21634. (setq-local comment-start-skip "^\\s-*#\\(?: \\|$\\)")
  21635. (setq-local comment-insert-comment-function 'org-insert-comment)
  21636. (setq-local comment-region-function 'org-comment-or-uncomment-region)
  21637. (setq-local uncomment-region-function 'org-comment-or-uncomment-region))
  21638. (defun org-insert-comment ()
  21639. "Insert an empty comment above current line.
  21640. If the line is empty, insert comment at its beginning. When
  21641. point is within a source block, comment according to the related
  21642. major mode."
  21643. (if (let ((element (org-element-at-point)))
  21644. (and (eq (org-element-type element) 'src-block)
  21645. (< (save-excursion
  21646. (goto-char (org-element-property :post-affiliated element))
  21647. (line-end-position))
  21648. (point))
  21649. (> (save-excursion
  21650. (goto-char (org-element-property :end element))
  21651. (skip-chars-backward " \r\t\n")
  21652. (line-beginning-position))
  21653. (point))))
  21654. (org-babel-do-in-edit-buffer (call-interactively 'comment-dwim))
  21655. (beginning-of-line)
  21656. (if (looking-at "\\s-*$") (delete-region (point) (point-at-eol))
  21657. (open-line 1))
  21658. (org-indent-line)
  21659. (insert "# ")))
  21660. (defvar comment-empty-lines) ; From newcomment.el.
  21661. (defun org-comment-or-uncomment-region (beg end &rest _)
  21662. "Comment or uncomment each non-blank line in the region.
  21663. Uncomment each non-blank line between BEG and END if it only
  21664. contains commented lines. Otherwise, comment them. If region is
  21665. strictly within a source block, use appropriate comment syntax."
  21666. (if (let ((element (org-element-at-point)))
  21667. (and (eq (org-element-type element) 'src-block)
  21668. (< (save-excursion
  21669. (goto-char (org-element-property :post-affiliated element))
  21670. (line-end-position))
  21671. beg)
  21672. (>= (save-excursion
  21673. (goto-char (org-element-property :end element))
  21674. (skip-chars-backward " \r\t\n")
  21675. (line-beginning-position))
  21676. end)))
  21677. (org-babel-do-in-edit-buffer (call-interactively 'comment-dwim))
  21678. (save-restriction
  21679. ;; Restrict region
  21680. (narrow-to-region (save-excursion (goto-char beg)
  21681. (skip-chars-forward " \r\t\n" end)
  21682. (line-beginning-position))
  21683. (save-excursion (goto-char end)
  21684. (skip-chars-backward " \r\t\n" beg)
  21685. (line-end-position)))
  21686. (let ((uncommentp
  21687. ;; UNCOMMENTP is non-nil when every non blank line between
  21688. ;; BEG and END is a comment.
  21689. (save-excursion
  21690. (goto-char (point-min))
  21691. (while (and (not (eobp))
  21692. (let ((element (org-element-at-point)))
  21693. (and (eq (org-element-type element) 'comment)
  21694. (goto-char (min (point-max)
  21695. (org-element-property
  21696. :end element)))))))
  21697. (eobp))))
  21698. (if uncommentp
  21699. ;; Only blank lines and comments in region: uncomment it.
  21700. (save-excursion
  21701. (goto-char (point-min))
  21702. (while (not (eobp))
  21703. (when (looking-at "[ \t]*\\(#\\(?: \\|$\\)\\)")
  21704. (replace-match "" nil nil nil 1))
  21705. (forward-line)))
  21706. ;; Comment each line in region.
  21707. (let ((min-indent (point-max)))
  21708. ;; First find the minimum indentation across all lines.
  21709. (save-excursion
  21710. (goto-char (point-min))
  21711. (while (and (not (eobp)) (not (zerop min-indent)))
  21712. (unless (looking-at "[ \t]*$")
  21713. (setq min-indent (min min-indent (current-indentation))))
  21714. (forward-line)))
  21715. ;; Then loop over all lines.
  21716. (save-excursion
  21717. (goto-char (point-min))
  21718. (while (not (eobp))
  21719. (unless (and (not comment-empty-lines) (looking-at "[ \t]*$"))
  21720. ;; Don't get fooled by invisible text (e.g. link path)
  21721. ;; when moving to column MIN-INDENT.
  21722. (let ((buffer-invisibility-spec nil))
  21723. (org-move-to-column min-indent t))
  21724. (insert comment-start))
  21725. (forward-line)))))))))
  21726. (defun org-comment-dwim (_arg)
  21727. "Call `comment-dwim' within a source edit buffer if needed."
  21728. (interactive "*P")
  21729. (if (org-in-src-block-p)
  21730. (org-babel-do-in-edit-buffer (call-interactively 'comment-dwim))
  21731. (call-interactively 'comment-dwim)))
  21732. ;;; Timestamps API
  21733. ;; This section contains tools to operate on timestamp objects, as
  21734. ;; returned by, e.g. `org-element-context'.
  21735. (defun org-timestamp--to-internal-time (timestamp &optional end)
  21736. "Encode TIMESTAMP object into Emacs internal time.
  21737. Use end of date range or time range when END is non-nil."
  21738. (apply #'encode-time
  21739. (cons 0
  21740. (mapcar
  21741. (lambda (prop) (or (org-element-property prop timestamp) 0))
  21742. (if end '(:minute-end :hour-end :day-end :month-end :year-end)
  21743. '(:minute-start :hour-start :day-start :month-start
  21744. :year-start))))))
  21745. (defun org-timestamp-has-time-p (timestamp)
  21746. "Non-nil when TIMESTAMP has a time specified."
  21747. (org-element-property :hour-start timestamp))
  21748. (defun org-timestamp-format (timestamp format &optional end utc)
  21749. "Format a TIMESTAMP object into a string.
  21750. FORMAT is a format specifier to be passed to
  21751. `format-time-string'.
  21752. When optional argument END is non-nil, use end of date-range or
  21753. time-range, if possible.
  21754. When optional argument UTC is non-nil, time will be expressed as
  21755. Universal Time."
  21756. (format-time-string
  21757. format (org-timestamp--to-internal-time timestamp end)
  21758. (and utc t)))
  21759. (defun org-timestamp-split-range (timestamp &optional end)
  21760. "Extract a TIMESTAMP object from a date or time range.
  21761. END, when non-nil, means extract the end of the range.
  21762. Otherwise, extract its start.
  21763. Return a new timestamp object."
  21764. (let ((type (org-element-property :type timestamp)))
  21765. (if (memq type '(active inactive diary)) timestamp
  21766. (let ((split-ts (org-element-copy timestamp)))
  21767. ;; Set new type.
  21768. (org-element-put-property
  21769. split-ts :type (if (eq type 'active-range) 'active 'inactive))
  21770. ;; Copy start properties over end properties if END is
  21771. ;; non-nil. Otherwise, copy end properties over `start' ones.
  21772. (let ((p-alist '((:minute-start . :minute-end)
  21773. (:hour-start . :hour-end)
  21774. (:day-start . :day-end)
  21775. (:month-start . :month-end)
  21776. (:year-start . :year-end))))
  21777. (dolist (p-cell p-alist)
  21778. (org-element-put-property
  21779. split-ts
  21780. (funcall (if end #'car #'cdr) p-cell)
  21781. (org-element-property
  21782. (funcall (if end #'cdr #'car) p-cell) split-ts)))
  21783. ;; Eventually refresh `:raw-value'.
  21784. (org-element-put-property split-ts :raw-value nil)
  21785. (org-element-put-property
  21786. split-ts :raw-value (org-element-interpret-data split-ts)))))))
  21787. (defun org-timestamp-translate (timestamp &optional boundary)
  21788. "Translate TIMESTAMP object to custom format.
  21789. Format string is defined in `org-time-stamp-custom-formats',
  21790. which see.
  21791. When optional argument BOUNDARY is non-nil, it is either the
  21792. symbol `start' or `end'. In this case, only translate the
  21793. starting or ending part of TIMESTAMP if it is a date or time
  21794. range. Otherwise, translate both parts.
  21795. Return timestamp as-is if `org-display-custom-times' is nil or if
  21796. it has a `diary' type."
  21797. (let ((type (org-element-property :type timestamp)))
  21798. (if (or (not org-display-custom-times) (eq type 'diary))
  21799. (org-element-interpret-data timestamp)
  21800. (let ((fmt (funcall (if (org-timestamp-has-time-p timestamp) #'cdr #'car)
  21801. org-time-stamp-custom-formats)))
  21802. (if (and (not boundary) (memq type '(active-range inactive-range)))
  21803. (concat (org-timestamp-format timestamp fmt)
  21804. "--"
  21805. (org-timestamp-format timestamp fmt t))
  21806. (org-timestamp-format timestamp fmt (eq boundary 'end)))))))
  21807. ;;; Other stuff.
  21808. (defvar reftex-docstruct-symbol)
  21809. (defvar reftex-cite-format)
  21810. (defvar org--rds)
  21811. (defun org-reftex-citation ()
  21812. "Use reftex-citation to insert a citation into the buffer.
  21813. This looks for a line like
  21814. #+BIBLIOGRAPHY: foo plain option:-d
  21815. and derives from it that foo.bib is the bibliography file relevant
  21816. for this document. It then installs the necessary environment for RefTeX
  21817. to work in this buffer and calls `reftex-citation' to insert a citation
  21818. into the buffer.
  21819. Export of such citations to both LaTeX and HTML is handled by the contributed
  21820. package ox-bibtex by Taru Karttunen."
  21821. (interactive)
  21822. (let ((reftex-docstruct-symbol 'org--rds)
  21823. (reftex-cite-format "\\cite{%l}")
  21824. org--rds bib)
  21825. (save-excursion
  21826. (save-restriction
  21827. (widen)
  21828. (let ((case-fold-search t)
  21829. (re "^[ \t]*#\\+BIBLIOGRAPHY:[ \t]+\\([^ \t\n]+\\)"))
  21830. (if (not (save-excursion
  21831. (or (re-search-forward re nil t)
  21832. (re-search-backward re nil t))))
  21833. (user-error "No bibliography defined in file")
  21834. (setq bib (concat (match-string 1) ".bib")
  21835. org--rds (list (list 'bib bib)))))))
  21836. (call-interactively 'reftex-citation)))
  21837. ;;;; Functions extending outline functionality
  21838. (defun org-beginning-of-line (&optional arg)
  21839. "Go to the beginning of the current line. If that is invisible, continue
  21840. to a visible line beginning. This makes the function of C-a more intuitive.
  21841. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  21842. first attempt, and only move to after the tags when the cursor is already
  21843. beyond the end of the headline."
  21844. (interactive "P")
  21845. (let ((pos (point))
  21846. (special (if (consp org-special-ctrl-a/e)
  21847. (car org-special-ctrl-a/e)
  21848. org-special-ctrl-a/e))
  21849. deactivate-mark refpos)
  21850. (if (org-bound-and-true-p visual-line-mode)
  21851. (beginning-of-visual-line 1)
  21852. (beginning-of-line 1))
  21853. (if (and arg (fboundp 'move-beginning-of-line))
  21854. (call-interactively 'move-beginning-of-line)
  21855. (unless (bobp)
  21856. (backward-char 1)
  21857. (if (org-truely-invisible-p)
  21858. (while (and (not (bobp)) (org-truely-invisible-p))
  21859. (backward-char 1)
  21860. (beginning-of-line 1))
  21861. (forward-char 1))))
  21862. (when special
  21863. (cond
  21864. ((and (looking-at org-complex-heading-regexp)
  21865. (eq (char-after (match-end 1)) ?\s))
  21866. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  21867. (point-at-eol)))
  21868. (goto-char
  21869. (if (eq special t)
  21870. (cond ((> pos refpos) refpos)
  21871. ((= pos (point)) refpos)
  21872. (t (point)))
  21873. (cond ((> pos (point)) (point))
  21874. ((not (eq last-command this-command)) (point))
  21875. (t refpos)))))
  21876. ((org-at-item-p)
  21877. ;; Being at an item and not looking at an the item means point
  21878. ;; was previously moved to beginning of a visual line, which
  21879. ;; doesn't contain the item. Therefore, do nothing special,
  21880. ;; just stay here.
  21881. (when (looking-at org-list-full-item-re)
  21882. ;; Set special position at first white space character after
  21883. ;; bullet, and check-box, if any.
  21884. (let ((after-bullet
  21885. (let ((box (match-end 3)))
  21886. (if (not box) (match-end 1)
  21887. (let ((after (char-after box)))
  21888. (if (and after (= after ? )) (1+ box) box))))))
  21889. ;; Special case: Move point to special position when
  21890. ;; currently after it or at beginning of line.
  21891. (if (eq special t)
  21892. (when (or (> pos after-bullet) (= (point) pos))
  21893. (goto-char after-bullet))
  21894. ;; Reversed case: Move point to special position when
  21895. ;; point was already at beginning of line and command is
  21896. ;; repeated.
  21897. (when (and (= (point) pos) (eq last-command this-command))
  21898. (goto-char after-bullet))))))))
  21899. (org-no-warnings
  21900. (and (featurep 'xemacs) (setq zmacs-region-stays t))))
  21901. (setq disable-point-adjustment
  21902. (or (not (invisible-p (point)))
  21903. (not (invisible-p (max (point-min) (1- (point))))))))
  21904. (defun org-end-of-line (&optional arg)
  21905. "Go to the end of the line.
  21906. If this is a headline, and `org-special-ctrl-a/e' is set, ignore
  21907. tags on the first attempt, and only move to after the tags when
  21908. the cursor is already beyond the end of the headline."
  21909. (interactive "P")
  21910. (let ((special (if (consp org-special-ctrl-a/e) (cdr org-special-ctrl-a/e)
  21911. org-special-ctrl-a/e))
  21912. (move-fun (cond ((org-bound-and-true-p visual-line-mode)
  21913. 'end-of-visual-line)
  21914. ((fboundp 'move-end-of-line) 'move-end-of-line)
  21915. (t 'end-of-line)))
  21916. deactivate-mark)
  21917. (if (or (not special) arg) (call-interactively move-fun)
  21918. (let* ((element (save-excursion (beginning-of-line)
  21919. (org-element-at-point)))
  21920. (type (org-element-type element)))
  21921. (cond
  21922. ((memq type '(headline inlinetask))
  21923. (let ((pos (point)))
  21924. (beginning-of-line 1)
  21925. (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*\\)?$"))
  21926. (if (eq special t)
  21927. (if (or (< pos (match-beginning 1)) (= pos (match-end 0)))
  21928. (goto-char (match-beginning 1))
  21929. (goto-char (match-end 0)))
  21930. (if (or (< pos (match-end 0))
  21931. (not (eq this-command last-command)))
  21932. (goto-char (match-end 0))
  21933. (goto-char (match-beginning 1))))
  21934. (call-interactively move-fun))))
  21935. ((outline-invisible-p (line-end-position))
  21936. ;; If element is hidden, `move-end-of-line' would put point
  21937. ;; after it. Use `end-of-line' to stay on current line.
  21938. (call-interactively 'end-of-line))
  21939. (t (call-interactively move-fun)))))
  21940. (org-no-warnings (and (featurep 'xemacs) (setq zmacs-region-stays t))))
  21941. (setq disable-point-adjustment
  21942. (or (not (invisible-p (point)))
  21943. (not (invisible-p (max (point-min) (1- (point))))))))
  21944. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  21945. (define-key org-mode-map "\C-e" 'org-end-of-line)
  21946. (defun org-backward-sentence (&optional _arg)
  21947. "Go to beginning of sentence, or beginning of table field.
  21948. This will call `backward-sentence' or `org-table-beginning-of-field',
  21949. depending on context."
  21950. (interactive)
  21951. (let* ((element (org-element-at-point))
  21952. (contents-begin (org-element-property :contents-begin element))
  21953. (table (org-element-lineage element '(table) t)))
  21954. (if (and table
  21955. (> (point) contents-begin)
  21956. (<= (point) (org-element-property :contents-end table)))
  21957. (call-interactively #'org-table-beginning-of-field)
  21958. (save-restriction
  21959. (when (and contents-begin
  21960. (< (point-min) contents-begin)
  21961. (> (point) contents-begin))
  21962. (narrow-to-region contents-begin
  21963. (org-element-property :contents-end element)))
  21964. (call-interactively #'backward-sentence)))))
  21965. (defun org-forward-sentence (&optional _arg)
  21966. "Go to end of sentence, or end of table field.
  21967. This will call `forward-sentence' or `org-table-end-of-field',
  21968. depending on context."
  21969. (interactive)
  21970. (let* ((element (org-element-at-point))
  21971. (contents-end (org-element-property :contents-end element))
  21972. (table (org-element-lineage element '(table) t)))
  21973. (if (and table
  21974. (>= (point) (org-element-property :contents-begin table))
  21975. (< (point) contents-end))
  21976. (call-interactively #'org-table-end-of-field)
  21977. (save-restriction
  21978. (when (and contents-end
  21979. (> (point-max) contents-end)
  21980. ;; Skip blank lines between elements.
  21981. (< (org-element-property :end element)
  21982. (save-excursion (goto-char contents-end)
  21983. (skip-chars-forward " \r\t\n"))))
  21984. (narrow-to-region (org-element-property :contents-begin element)
  21985. contents-end))
  21986. (call-interactively #'forward-sentence)))))
  21987. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  21988. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  21989. (defun org-kill-line (&optional _arg)
  21990. "Kill line, to tags or end of line."
  21991. (interactive)
  21992. (cond
  21993. ((or (not org-special-ctrl-k)
  21994. (bolp)
  21995. (not (org-at-heading-p)))
  21996. (when (and (get-char-property (min (point-max) (point-at-eol)) 'invisible)
  21997. org-ctrl-k-protect-subtree
  21998. (or (eq org-ctrl-k-protect-subtree 'error)
  21999. (not (y-or-n-p "Kill hidden subtree along with headline? "))))
  22000. (user-error "C-k aborted as it would kill a hidden subtree"))
  22001. (call-interactively
  22002. (if (org-bound-and-true-p visual-line-mode) 'kill-visual-line 'kill-line)))
  22003. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$"))
  22004. (kill-region (point) (match-beginning 1))
  22005. (org-set-tags nil t))
  22006. (t (kill-region (point) (point-at-eol)))))
  22007. (define-key org-mode-map "\C-k" 'org-kill-line)
  22008. (defun org-yank (&optional arg)
  22009. "Yank. If the kill is a subtree, treat it specially.
  22010. This command will look at the current kill and check if is a single
  22011. subtree, or a series of subtrees[1]. If it passes the test, and if the
  22012. cursor is at the beginning of a line or after the stars of a currently
  22013. empty headline, then the yank is handled specially. How exactly depends
  22014. on the value of the following variables, both set by default.
  22015. `org-yank-folded-subtrees'
  22016. When set, the subtree(s) will be folded after insertion, but only
  22017. if doing so would now swallow text after the yanked text.
  22018. `org-yank-adjusted-subtrees'
  22019. When set, the subtree will be promoted or demoted in order to
  22020. fit into the local outline tree structure, which means that the
  22021. level will be adjusted so that it becomes the smaller one of the
  22022. two *visible* surrounding headings.
  22023. Any prefix to this command will cause `yank' to be called directly with
  22024. no special treatment. In particular, a simple \\[universal-argument] prefix \
  22025. will just
  22026. plainly yank the text as it is.
  22027. \[1] The test checks if the first non-white line is a heading
  22028. and if there are no other headings with fewer stars."
  22029. (interactive "P")
  22030. (org-yank-generic 'yank arg))
  22031. (defun org-yank-generic (command arg)
  22032. "Perform some yank-like command.
  22033. This function implements the behavior described in the `org-yank'
  22034. documentation. However, it has been generalized to work for any
  22035. interactive command with similar behavior."
  22036. ;; pretend to be command COMMAND
  22037. (setq this-command command)
  22038. (if arg
  22039. (call-interactively command)
  22040. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  22041. (and (org-kill-is-subtree-p)
  22042. (or (bolp)
  22043. (and (looking-at "[ \t]*$")
  22044. (string-match
  22045. "\\`\\*+\\'"
  22046. (buffer-substring (point-at-bol) (point)))))))
  22047. swallowp)
  22048. (cond
  22049. ((and subtreep org-yank-folded-subtrees)
  22050. (let ((beg (point))
  22051. end)
  22052. (if (and subtreep org-yank-adjusted-subtrees)
  22053. (org-paste-subtree nil nil 'for-yank)
  22054. (call-interactively command))
  22055. (setq end (point))
  22056. (goto-char beg)
  22057. (when (and (bolp) subtreep
  22058. (not (setq swallowp
  22059. (org-yank-folding-would-swallow-text beg end))))
  22060. (org-with-limited-levels
  22061. (or (looking-at org-outline-regexp)
  22062. (re-search-forward org-outline-regexp-bol end t))
  22063. (while (and (< (point) end) (looking-at org-outline-regexp))
  22064. (outline-hide-subtree)
  22065. (org-cycle-show-empty-lines 'folded)
  22066. (condition-case nil
  22067. (outline-forward-same-level 1)
  22068. (error (goto-char end))))))
  22069. (when swallowp
  22070. (message
  22071. "Inserted text not folded because that would swallow text"))
  22072. (goto-char end)
  22073. (skip-chars-forward " \t\n\r")
  22074. (beginning-of-line 1)
  22075. (push-mark beg 'nomsg)))
  22076. ((and subtreep org-yank-adjusted-subtrees)
  22077. (let ((beg (point-at-bol)))
  22078. (org-paste-subtree nil nil 'for-yank)
  22079. (push-mark beg 'nomsg)))
  22080. (t
  22081. (call-interactively command))))))
  22082. (defun org-yank-folding-would-swallow-text (beg end)
  22083. "Would hide-subtree at BEG swallow any text after END?"
  22084. (let (level)
  22085. (org-with-limited-levels
  22086. (save-excursion
  22087. (goto-char beg)
  22088. (when (or (looking-at org-outline-regexp)
  22089. (re-search-forward org-outline-regexp-bol end t))
  22090. (setq level (org-outline-level)))
  22091. (goto-char end)
  22092. (skip-chars-forward " \t\r\n\v\f")
  22093. (not (or (eobp)
  22094. (and (bolp) (looking-at-p org-outline-regexp)
  22095. (<= (org-outline-level) level))))))))
  22096. (define-key org-mode-map "\C-y" 'org-yank)
  22097. (defun org-truely-invisible-p ()
  22098. "Check if point is at a character currently not visible.
  22099. This version does not only check the character property, but also
  22100. `visible-mode'."
  22101. ;; Early versions of noutline don't have `outline-invisible-p'.
  22102. (unless (org-bound-and-true-p visible-mode)
  22103. (outline-invisible-p)))
  22104. (defun org-invisible-p2 ()
  22105. "Check if point is at a character currently not visible."
  22106. (save-excursion
  22107. (when (and (eolp) (not (bobp))) (backward-char 1))
  22108. ;; Early versions of noutline don't have `outline-invisible-p'.
  22109. (outline-invisible-p)))
  22110. (defun org-back-to-heading (&optional invisible-ok)
  22111. "Call `outline-back-to-heading', but provide a better error message."
  22112. (condition-case nil
  22113. (outline-back-to-heading invisible-ok)
  22114. (error (error "Before first headline at position %d in buffer %s"
  22115. (point) (current-buffer)))))
  22116. (defun org-before-first-heading-p ()
  22117. "Before first heading?"
  22118. (save-excursion
  22119. (end-of-line)
  22120. (null (re-search-backward org-outline-regexp-bol nil t))))
  22121. (defun org-at-heading-p (&optional ignored)
  22122. (outline-on-heading-p t))
  22123. ;;; Though the function was obsoleted in 7.8.03, the byte-compiler
  22124. ;;; warning was only added in Org 9.0, which should be taken into
  22125. ;;; account when deciding when to remove the alias.
  22126. (define-obsolete-function-alias 'org-on-heading-p 'org-at-heading-p "Org 7.8.03")
  22127. (defun org-in-commented-heading-p (&optional no-inheritance)
  22128. "Non-nil if point is under a commented heading.
  22129. This function also checks ancestors of the current headline,
  22130. unless optional argument NO-INHERITANCE is non-nil."
  22131. (cond
  22132. ((org-before-first-heading-p) nil)
  22133. ((let ((headline (nth 4 (org-heading-components))))
  22134. (and headline
  22135. (let ((case-fold-search nil))
  22136. (org-string-match-p (concat "^" org-comment-string "\\(?: \\|$\\)")
  22137. headline)))))
  22138. (no-inheritance nil)
  22139. (t
  22140. (save-excursion (and (org-up-heading-safe) (org-in-commented-heading-p))))))
  22141. (defun org-at-comment-p nil
  22142. "Is cursor in a commented line?"
  22143. (save-excursion
  22144. (save-match-data
  22145. (beginning-of-line)
  22146. (looking-at "^[ \t]*# "))))
  22147. (defun org-at-drawer-p nil
  22148. "Is cursor at a drawer keyword?"
  22149. (save-excursion
  22150. (move-beginning-of-line 1)
  22151. (looking-at org-drawer-regexp)))
  22152. (defun org-at-block-p nil
  22153. "Is cursor at a block keyword?"
  22154. (save-excursion
  22155. (move-beginning-of-line 1)
  22156. (looking-at org-block-regexp)))
  22157. (defun org-point-at-end-of-empty-headline ()
  22158. "If point is at the end of an empty headline, return t, else nil.
  22159. If the heading only contains a TODO keyword, it is still still considered
  22160. empty."
  22161. (and (looking-at "[ \t]*$")
  22162. (when org-todo-line-regexp
  22163. (save-excursion
  22164. (beginning-of-line 1)
  22165. (let ((case-fold-search nil))
  22166. (looking-at org-todo-line-regexp)
  22167. (string= (match-string 3) ""))))))
  22168. (defun org-at-heading-or-item-p ()
  22169. (or (org-at-heading-p) (org-at-item-p)))
  22170. (defun org-at-target-p ()
  22171. (or (org-in-regexp org-radio-target-regexp)
  22172. (org-in-regexp org-target-regexp)))
  22173. ;; Compatibility alias with Org versions < 7.8.03
  22174. (defalias 'org-on-target-p 'org-at-target-p)
  22175. (defun org-up-heading-all (arg)
  22176. "Move to the heading line of which the present line is a subheading.
  22177. This function considers both visible and invisible heading lines.
  22178. With argument, move up ARG levels."
  22179. (if (fboundp 'outline-up-heading-all)
  22180. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  22181. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  22182. (defun org-up-heading-safe ()
  22183. "Move to the heading line of which the present line is a subheading.
  22184. This version will not throw an error. It will return the level of the
  22185. headline found, or nil if no higher level is found.
  22186. Also, this function will be a lot faster than `outline-up-heading',
  22187. because it relies on stars being the outline starters. This can really
  22188. make a significant difference in outlines with very many siblings."
  22189. (when (ignore-errors (org-back-to-heading t))
  22190. (let ((level-up (1- (funcall outline-level))))
  22191. (and (> level-up 0)
  22192. (re-search-backward (format "^\\*\\{1,%d\\} " level-up) nil t)
  22193. (funcall outline-level)))))
  22194. (defun org-first-sibling-p ()
  22195. "Is this heading the first child of its parents?"
  22196. (interactive)
  22197. (let ((re org-outline-regexp-bol)
  22198. level l)
  22199. (unless (org-at-heading-p t)
  22200. (user-error "Not at a heading"))
  22201. (setq level (funcall outline-level))
  22202. (save-excursion
  22203. (if (not (re-search-backward re nil t))
  22204. t
  22205. (setq l (funcall outline-level))
  22206. (< l level)))))
  22207. (defun org-goto-sibling (&optional previous)
  22208. "Goto the next sibling, even if it is invisible.
  22209. When PREVIOUS is set, go to the previous sibling instead. Returns t
  22210. when a sibling was found. When none is found, return nil and don't
  22211. move point."
  22212. (let ((fun (if previous 're-search-backward 're-search-forward))
  22213. (pos (point))
  22214. (re org-outline-regexp-bol)
  22215. level l)
  22216. (when (ignore-errors (org-back-to-heading t))
  22217. (setq level (funcall outline-level))
  22218. (catch 'exit
  22219. (or previous (forward-char 1))
  22220. (while (funcall fun re nil t)
  22221. (setq l (funcall outline-level))
  22222. (when (< l level) (goto-char pos) (throw 'exit nil))
  22223. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  22224. (goto-char pos)
  22225. nil))))
  22226. (defun org-show-siblings ()
  22227. "Show all siblings of the current headline."
  22228. (save-excursion
  22229. (while (org-goto-sibling) (org-flag-heading nil)))
  22230. (save-excursion
  22231. (while (org-goto-sibling 'previous)
  22232. (org-flag-heading nil))))
  22233. (defun org-goto-first-child ()
  22234. "Goto the first child, even if it is invisible.
  22235. Return t when a child was found. Otherwise don't move point and
  22236. return nil."
  22237. (let (level (pos (point)) (re org-outline-regexp-bol))
  22238. (when (ignore-errors (org-back-to-heading t))
  22239. (setq level (outline-level))
  22240. (forward-char 1)
  22241. (if (and (re-search-forward re nil t) (> (outline-level) level))
  22242. (progn (goto-char (match-beginning 0)) t)
  22243. (goto-char pos) nil))))
  22244. (defun org-show-hidden-entry ()
  22245. "Show an entry where even the heading is hidden."
  22246. (save-excursion
  22247. (org-show-entry)))
  22248. (defun org-flag-heading (flag &optional entry)
  22249. "Flag the current heading. FLAG non-nil means make invisible.
  22250. When ENTRY is non-nil, show the entire entry."
  22251. (save-excursion
  22252. (org-back-to-heading t)
  22253. ;; Check if we should show the entire entry
  22254. (if entry
  22255. (progn
  22256. (org-show-entry)
  22257. (save-excursion
  22258. (and (outline-next-heading)
  22259. (org-flag-heading nil))))
  22260. (outline-flag-region (max (point-min) (1- (point)))
  22261. (save-excursion (outline-end-of-heading) (point))
  22262. flag))))
  22263. (defun org-get-next-sibling ()
  22264. "Move to next heading of the same level, and return point.
  22265. If there is no such heading, return nil.
  22266. This is like outline-next-sibling, but invisible headings are ok."
  22267. (let ((level (funcall outline-level)))
  22268. (outline-next-heading)
  22269. (while (and (not (eobp)) (> (funcall outline-level) level))
  22270. (outline-next-heading))
  22271. (unless (or (eobp) (< (funcall outline-level) level))
  22272. (point))))
  22273. (defun org-get-last-sibling ()
  22274. "Move to previous heading of the same level, and return point.
  22275. If there is no such heading, return nil."
  22276. (let ((opoint (point))
  22277. (level (funcall outline-level)))
  22278. (outline-previous-heading)
  22279. (when (and (/= (point) opoint) (outline-on-heading-p t))
  22280. (while (and (> (funcall outline-level) level)
  22281. (not (bobp)))
  22282. (outline-previous-heading))
  22283. (unless (< (funcall outline-level) level)
  22284. (point)))))
  22285. (defun org-end-of-subtree (&optional invisible-ok to-heading)
  22286. "Goto to the end of a subtree."
  22287. ;; This contains an exact copy of the original function, but it uses
  22288. ;; `org-back-to-heading', to make it work also in invisible
  22289. ;; trees. And is uses an invisible-ok argument.
  22290. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  22291. ;; Furthermore, when used inside Org, finding the end of a large subtree
  22292. ;; with many children and grandchildren etc, this can be much faster
  22293. ;; than the outline version.
  22294. (org-back-to-heading invisible-ok)
  22295. (let ((first t)
  22296. (level (funcall outline-level)))
  22297. (if (and (derived-mode-p 'org-mode) (< level 1000))
  22298. ;; A true heading (not a plain list item), in Org-mode
  22299. ;; This means we can easily find the end by looking
  22300. ;; only for the right number of stars. Using a regexp to do
  22301. ;; this is so much faster than using a Lisp loop.
  22302. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  22303. (forward-char 1)
  22304. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  22305. ;; something else, do it the slow way
  22306. (while (and (not (eobp))
  22307. (or first (> (funcall outline-level) level)))
  22308. (setq first nil)
  22309. (outline-next-heading)))
  22310. (unless to-heading
  22311. (when (memq (preceding-char) '(?\n ?\^M))
  22312. ;; Go to end of line before heading
  22313. (forward-char -1)
  22314. (when (memq (preceding-char) '(?\n ?\^M))
  22315. ;; leave blank line before heading
  22316. (forward-char -1)))))
  22317. (point))
  22318. (defun org-end-of-meta-data (&optional full)
  22319. "Skip planning line and properties drawer in current entry.
  22320. When optional argument FULL is non-nil, also skip empty lines,
  22321. clocking lines and regular drawers at the beginning of the
  22322. entry."
  22323. (org-back-to-heading t)
  22324. (forward-line)
  22325. (when (org-looking-at-p org-planning-line-re) (forward-line))
  22326. (when (looking-at org-property-drawer-re)
  22327. (goto-char (match-end 0))
  22328. (forward-line))
  22329. (when (and full (not (org-at-heading-p)))
  22330. (catch 'exit
  22331. (let ((end (save-excursion (outline-next-heading) (point)))
  22332. (re (concat "[ \t]*$" "\\|" org-clock-line-re)))
  22333. (while (not (eobp))
  22334. (cond ((org-looking-at-p org-drawer-regexp)
  22335. (if (re-search-forward "^[ \t]*:END:[ \t]*$" end t)
  22336. (forward-line)
  22337. (throw 'exit t)))
  22338. ((org-looking-at-p re) (forward-line))
  22339. (t (throw 'exit t))))))))
  22340. (defun org-forward-heading-same-level (arg &optional invisible-ok)
  22341. "Move forward to the ARG'th subheading at same level as this one.
  22342. Stop at the first and last subheadings of a superior heading.
  22343. Normally this only looks at visible headings, but when INVISIBLE-OK is
  22344. non-nil it will also look at invisible ones."
  22345. (interactive "p")
  22346. (if (not (ignore-errors (org-back-to-heading invisible-ok)))
  22347. (if (and arg (< arg 0))
  22348. (goto-char (point-min))
  22349. (outline-next-heading))
  22350. (org-at-heading-p)
  22351. (let ((level (- (match-end 0) (match-beginning 0) 1))
  22352. (f (if (and arg (< arg 0))
  22353. 're-search-backward
  22354. 're-search-forward))
  22355. (count (if arg (abs arg) 1))
  22356. (result (point)))
  22357. (while (and (prog1 (> count 0)
  22358. (forward-char (if (and arg (< arg 0)) -1 1)))
  22359. (funcall f org-outline-regexp-bol nil 'move))
  22360. (let ((l (- (match-end 0) (match-beginning 0) 1)))
  22361. (cond ((< l level) (setq count 0))
  22362. ((and (= l level)
  22363. (or invisible-ok
  22364. (progn
  22365. (goto-char (line-beginning-position))
  22366. (not (outline-invisible-p)))))
  22367. (setq count (1- count))
  22368. (when (eq l level)
  22369. (setq result (point)))))))
  22370. (goto-char result))
  22371. (beginning-of-line 1)))
  22372. (defun org-backward-heading-same-level (arg &optional invisible-ok)
  22373. "Move backward to the ARG'th subheading at same level as this one.
  22374. Stop at the first and last subheadings of a superior heading."
  22375. (interactive "p")
  22376. (org-forward-heading-same-level (if arg (- arg) -1) invisible-ok))
  22377. (defun org-next-visible-heading (arg)
  22378. "Move to the next visible heading.
  22379. This function wraps `outline-next-visible-heading' with
  22380. `org-with-limited-levels' in order to skip over inline tasks and
  22381. respect customization of `org-odd-levels-only'."
  22382. (interactive "p")
  22383. (org-with-limited-levels
  22384. (outline-next-visible-heading arg)))
  22385. (defun org-previous-visible-heading (arg)
  22386. "Move to the previous visible heading.
  22387. This function wraps `outline-previous-visible-heading' with
  22388. `org-with-limited-levels' in order to skip over inline tasks and
  22389. respect customization of `org-odd-levels-only'."
  22390. (interactive "p")
  22391. (org-with-limited-levels
  22392. (outline-previous-visible-heading arg)))
  22393. (defun org-next-block (arg &optional backward block-regexp)
  22394. "Jump to the next block.
  22395. With a prefix argument ARG, jump forward ARG many blocks.
  22396. When BACKWARD is non-nil, jump to the previous block.
  22397. When BLOCK-REGEXP is non-nil, use this regexp to find blocks.
  22398. Match data is set according to this regexp when the function
  22399. returns.
  22400. Return point at beginning of the opening line of found block.
  22401. Throw an error if no block is found."
  22402. (interactive "p")
  22403. (let ((re (or block-regexp "^[ \t]*#\\+BEGIN"))
  22404. (case-fold-search t)
  22405. (search-fn (if backward #'re-search-backward #'re-search-forward))
  22406. (count (or arg 1))
  22407. (origin (point))
  22408. last-element)
  22409. (if backward (beginning-of-line) (end-of-line))
  22410. (while (and (> count 0) (funcall search-fn re nil t))
  22411. (let ((element (save-excursion
  22412. (goto-char (match-beginning 0))
  22413. (save-match-data (org-element-at-point)))))
  22414. (when (and (memq (org-element-type element)
  22415. '(center-block comment-block dynamic-block
  22416. example-block export-block quote-block
  22417. special-block src-block verse-block))
  22418. (<= (match-beginning 0)
  22419. (org-element-property :post-affiliated element)))
  22420. (setq last-element element)
  22421. (cl-decf count))))
  22422. (if (= count 0)
  22423. (prog1 (goto-char (org-element-property :post-affiliated last-element))
  22424. (save-match-data (org-show-context)))
  22425. (goto-char origin)
  22426. (user-error "No %s code blocks" (if backward "previous" "further")))))
  22427. (defun org-previous-block (arg &optional block-regexp)
  22428. "Jump to the previous block.
  22429. With a prefix argument ARG, jump backward ARG many source blocks.
  22430. When BLOCK-REGEXP is non-nil, use this regexp to find blocks."
  22431. (interactive "p")
  22432. (org-next-block arg t block-regexp))
  22433. (defun org-forward-paragraph ()
  22434. "Move forward to beginning of next paragraph or equivalent.
  22435. The function moves point to the beginning of the next visible
  22436. structural element, which can be a paragraph, a table, a list
  22437. item, etc. It also provides some special moves for convenience:
  22438. - On an affiliated keyword, jump to the beginning of the
  22439. relative element.
  22440. - On an item or a footnote definition, move to the second
  22441. element inside, if any.
  22442. - On a table or a property drawer, jump after it.
  22443. - On a verse or source block, stop after blank lines."
  22444. (interactive)
  22445. (when (eobp) (user-error "Cannot move further down"))
  22446. (let* ((deactivate-mark nil)
  22447. (element (org-element-at-point))
  22448. (type (org-element-type element))
  22449. (post-affiliated (org-element-property :post-affiliated element))
  22450. (contents-begin (org-element-property :contents-begin element))
  22451. (contents-end (org-element-property :contents-end element))
  22452. (end (let ((end (org-element-property :end element)) (parent element))
  22453. (while (and (setq parent (org-element-property :parent parent))
  22454. (= (org-element-property :contents-end parent) end))
  22455. (setq end (org-element-property :end parent)))
  22456. end)))
  22457. (cond ((not element)
  22458. (skip-chars-forward " \r\t\n")
  22459. (or (eobp) (beginning-of-line)))
  22460. ;; On affiliated keywords, move to element's beginning.
  22461. ((< (point) post-affiliated)
  22462. (goto-char post-affiliated))
  22463. ;; At a table row, move to the end of the table. Similarly,
  22464. ;; at a node property, move to the end of the property
  22465. ;; drawer.
  22466. ((memq type '(node-property table-row))
  22467. (goto-char (org-element-property
  22468. :end (org-element-property :parent element))))
  22469. ((memq type '(property-drawer table)) (goto-char end))
  22470. ;; Consider blank lines as separators in verse and source
  22471. ;; blocks to ease editing.
  22472. ((memq type '(src-block verse-block))
  22473. (when (eq type 'src-block)
  22474. (setq contents-end
  22475. (save-excursion (goto-char end)
  22476. (skip-chars-backward " \r\t\n")
  22477. (line-beginning-position))))
  22478. (beginning-of-line)
  22479. (when (looking-at "[ \t]*$") (skip-chars-forward " \r\t\n"))
  22480. (if (not (re-search-forward "^[ \t]*$" contents-end t))
  22481. (goto-char end)
  22482. (skip-chars-forward " \r\t\n")
  22483. (if (= (point) contents-end) (goto-char end)
  22484. (beginning-of-line))))
  22485. ;; With no contents, just skip element.
  22486. ((not contents-begin) (goto-char end))
  22487. ;; If contents are invisible, skip the element altogether.
  22488. ((outline-invisible-p (line-end-position))
  22489. (cl-case type
  22490. (headline
  22491. (org-with-limited-levels (outline-next-visible-heading 1)))
  22492. ;; At a plain list, make sure we move to the next item
  22493. ;; instead of skipping the whole list.
  22494. (plain-list (forward-char)
  22495. (org-forward-paragraph))
  22496. (otherwise (goto-char end))))
  22497. ((>= (point) contents-end) (goto-char end))
  22498. ((>= (point) contents-begin)
  22499. ;; This can only happen on paragraphs and plain lists.
  22500. (cl-case type
  22501. (paragraph (goto-char end))
  22502. ;; At a plain list, try to move to second element in
  22503. ;; first item, if possible.
  22504. (plain-list (end-of-line)
  22505. (org-forward-paragraph))))
  22506. ;; When contents start on the middle of a line (e.g. in
  22507. ;; items and footnote definitions), try to reach first
  22508. ;; element starting after current line.
  22509. ((> (line-end-position) contents-begin)
  22510. (end-of-line)
  22511. (org-forward-paragraph))
  22512. (t (goto-char contents-begin)))))
  22513. (defun org-backward-paragraph ()
  22514. "Move backward to start of previous paragraph or equivalent.
  22515. The function moves point to the beginning of the current
  22516. structural element, which can be a paragraph, a table, a list
  22517. item, etc., or to the beginning of the previous visible one if
  22518. point is already there. It also provides some special moves for
  22519. convenience:
  22520. - On an affiliated keyword, jump to the first one.
  22521. - On a table or a property drawer, move to its beginning.
  22522. - On a verse or source block, stop before blank lines."
  22523. (interactive)
  22524. (when (bobp) (user-error "Cannot move further up"))
  22525. (let* ((deactivate-mark nil)
  22526. (element (org-element-at-point))
  22527. (type (org-element-type element))
  22528. (contents-begin (org-element-property :contents-begin element))
  22529. (contents-end (org-element-property :contents-end element))
  22530. (post-affiliated (org-element-property :post-affiliated element))
  22531. (begin (org-element-property :begin element)))
  22532. (cond
  22533. ((not element) (goto-char (point-min)))
  22534. ((= (point) begin)
  22535. (backward-char)
  22536. (org-backward-paragraph))
  22537. ((<= (point) post-affiliated) (goto-char begin))
  22538. ((memq type '(node-property table-row))
  22539. (goto-char (org-element-property
  22540. :post-affiliated (org-element-property :parent element))))
  22541. ((memq type '(property-drawer table)) (goto-char begin))
  22542. ((memq type '(src-block verse-block))
  22543. (when (eq type 'src-block)
  22544. (setq contents-begin
  22545. (save-excursion (goto-char begin) (forward-line) (point))))
  22546. (if (= (point) contents-begin) (goto-char post-affiliated)
  22547. ;; Inside a verse block, see blank lines as paragraph
  22548. ;; separators.
  22549. (let ((origin (point)))
  22550. (skip-chars-backward " \r\t\n" contents-begin)
  22551. (when (re-search-backward "^[ \t]*$" contents-begin 'move)
  22552. (skip-chars-forward " \r\t\n" origin)
  22553. (if (= (point) origin) (goto-char contents-begin)
  22554. (beginning-of-line))))))
  22555. ((not contents-begin) (goto-char (or post-affiliated begin)))
  22556. ((eq type 'paragraph)
  22557. (goto-char contents-begin)
  22558. ;; When at first paragraph in an item or a footnote definition,
  22559. ;; move directly to beginning of line.
  22560. (let ((parent-contents
  22561. (org-element-property
  22562. :contents-begin (org-element-property :parent element))))
  22563. (when (and parent-contents (= parent-contents contents-begin))
  22564. (beginning-of-line))))
  22565. ;; At the end of a greater element, move to the beginning of the
  22566. ;; last element within.
  22567. ((>= (point) contents-end)
  22568. (goto-char (1- contents-end))
  22569. (org-backward-paragraph))
  22570. (t (goto-char (or post-affiliated begin))))
  22571. ;; Ensure we never leave point invisible.
  22572. (when (outline-invisible-p (point)) (beginning-of-visual-line))))
  22573. (defun org-forward-element ()
  22574. "Move forward by one element.
  22575. Move to the next element at the same level, when possible."
  22576. (interactive)
  22577. (cond ((eobp) (user-error "Cannot move further down"))
  22578. ((org-with-limited-levels (org-at-heading-p))
  22579. (let ((origin (point)))
  22580. (goto-char (org-end-of-subtree nil t))
  22581. (unless (org-with-limited-levels (org-at-heading-p))
  22582. (goto-char origin)
  22583. (user-error "Cannot move further down"))))
  22584. (t
  22585. (let* ((elem (org-element-at-point))
  22586. (end (org-element-property :end elem))
  22587. (parent (org-element-property :parent elem)))
  22588. (cond ((and parent (= (org-element-property :contents-end parent) end))
  22589. (goto-char (org-element-property :end parent)))
  22590. ((integer-or-marker-p end) (goto-char end))
  22591. (t (message "No element at point")))))))
  22592. (defun org-backward-element ()
  22593. "Move backward by one element.
  22594. Move to the previous element at the same level, when possible."
  22595. (interactive)
  22596. (cond ((bobp) (user-error "Cannot move further up"))
  22597. ((org-with-limited-levels (org-at-heading-p))
  22598. ;; At a headline, move to the previous one, if any, or stay
  22599. ;; here.
  22600. (let ((origin (point)))
  22601. (org-with-limited-levels (org-backward-heading-same-level 1))
  22602. ;; When current headline has no sibling above, move to its
  22603. ;; parent.
  22604. (when (= (point) origin)
  22605. (or (org-with-limited-levels (org-up-heading-safe))
  22606. (progn (goto-char origin)
  22607. (user-error "Cannot move further up"))))))
  22608. (t
  22609. (let* ((elem (org-element-at-point))
  22610. (beg (org-element-property :begin elem)))
  22611. (cond
  22612. ;; Move to beginning of current element if point isn't
  22613. ;; there already.
  22614. ((null beg) (message "No element at point"))
  22615. ((/= (point) beg) (goto-char beg))
  22616. (t (goto-char beg)
  22617. (skip-chars-backward " \r\t\n")
  22618. (unless (bobp)
  22619. (let ((prev (org-element-at-point)))
  22620. (goto-char (org-element-property :begin prev))
  22621. (while (and (setq prev (org-element-property :parent prev))
  22622. (<= (org-element-property :end prev) beg))
  22623. (goto-char (org-element-property :begin prev)))))))))))
  22624. (defun org-up-element ()
  22625. "Move to upper element."
  22626. (interactive)
  22627. (if (org-with-limited-levels (org-at-heading-p))
  22628. (unless (org-up-heading-safe) (user-error "No surrounding element"))
  22629. (let* ((elem (org-element-at-point))
  22630. (parent (org-element-property :parent elem)))
  22631. (if parent (goto-char (org-element-property :begin parent))
  22632. (if (org-with-limited-levels (org-before-first-heading-p))
  22633. (user-error "No surrounding element")
  22634. (org-with-limited-levels (org-back-to-heading)))))))
  22635. (defvar org-element-greater-elements)
  22636. (defun org-down-element ()
  22637. "Move to inner element."
  22638. (interactive)
  22639. (let ((element (org-element-at-point)))
  22640. (cond
  22641. ((memq (org-element-type element) '(plain-list table))
  22642. (goto-char (org-element-property :contents-begin element))
  22643. (forward-char))
  22644. ((memq (org-element-type element) org-element-greater-elements)
  22645. ;; If contents are hidden, first disclose them.
  22646. (when (outline-invisible-p (line-end-position)) (org-cycle))
  22647. (goto-char (or (org-element-property :contents-begin element)
  22648. (user-error "No content for this element"))))
  22649. (t (user-error "No inner element")))))
  22650. (defun org-drag-element-backward ()
  22651. "Move backward element at point."
  22652. (interactive)
  22653. (if (org-with-limited-levels (org-at-heading-p)) (org-move-subtree-up)
  22654. (let* ((elem (org-element-at-point))
  22655. (prev-elem
  22656. (save-excursion
  22657. (goto-char (org-element-property :begin elem))
  22658. (skip-chars-backward " \r\t\n")
  22659. (unless (bobp)
  22660. (let* ((beg (org-element-property :begin elem))
  22661. (prev (org-element-at-point))
  22662. (up prev))
  22663. (while (and (setq up (org-element-property :parent up))
  22664. (<= (org-element-property :end up) beg))
  22665. (setq prev up))
  22666. prev)))))
  22667. ;; Error out if no previous element or previous element is
  22668. ;; a parent of the current one.
  22669. (if (or (not prev-elem) (org-element-nested-p elem prev-elem))
  22670. (user-error "Cannot drag element backward")
  22671. (let ((pos (point)))
  22672. (org-element-swap-A-B prev-elem elem)
  22673. (goto-char (+ (org-element-property :begin prev-elem)
  22674. (- pos (org-element-property :begin elem)))))))))
  22675. (defun org-drag-element-forward ()
  22676. "Move forward element at point."
  22677. (interactive)
  22678. (let* ((pos (point))
  22679. (elem (org-element-at-point)))
  22680. (when (= (point-max) (org-element-property :end elem))
  22681. (user-error "Cannot drag element forward"))
  22682. (goto-char (org-element-property :end elem))
  22683. (let ((next-elem (org-element-at-point)))
  22684. (when (or (org-element-nested-p elem next-elem)
  22685. (and (eq (org-element-type next-elem) 'headline)
  22686. (not (eq (org-element-type elem) 'headline))))
  22687. (goto-char pos)
  22688. (user-error "Cannot drag element forward"))
  22689. ;; Compute new position of point: it's shifted by NEXT-ELEM
  22690. ;; body's length (without final blanks) and by the length of
  22691. ;; blanks between ELEM and NEXT-ELEM.
  22692. (let ((size-next (- (save-excursion
  22693. (goto-char (org-element-property :end next-elem))
  22694. (skip-chars-backward " \r\t\n")
  22695. (forward-line)
  22696. ;; Small correction if buffer doesn't end
  22697. ;; with a newline character.
  22698. (if (and (eolp) (not (bolp))) (1+ (point)) (point)))
  22699. (org-element-property :begin next-elem)))
  22700. (size-blank (- (org-element-property :end elem)
  22701. (save-excursion
  22702. (goto-char (org-element-property :end elem))
  22703. (skip-chars-backward " \r\t\n")
  22704. (forward-line)
  22705. (point)))))
  22706. (org-element-swap-A-B elem next-elem)
  22707. (goto-char (+ pos size-next size-blank))))))
  22708. (defun org-drag-line-forward (arg)
  22709. "Drag the line at point ARG lines forward."
  22710. (interactive "p")
  22711. (dotimes (_ (abs arg))
  22712. (let ((c (current-column)))
  22713. (if (< 0 arg)
  22714. (progn
  22715. (beginning-of-line 2)
  22716. (transpose-lines 1)
  22717. (beginning-of-line 0))
  22718. (transpose-lines 1)
  22719. (beginning-of-line -1))
  22720. (org-move-to-column c))))
  22721. (defun org-drag-line-backward (arg)
  22722. "Drag the line at point ARG lines backward."
  22723. (interactive "p")
  22724. (org-drag-line-forward (- arg)))
  22725. (defun org-mark-element ()
  22726. "Put point at beginning of this element, mark at end.
  22727. Interactively, if this command is repeated or (in Transient Mark
  22728. mode) if the mark is active, it marks the next element after the
  22729. ones already marked."
  22730. (interactive)
  22731. (let (deactivate-mark)
  22732. (if (and (org-called-interactively-p 'any)
  22733. (or (and (eq last-command this-command) (mark t))
  22734. (and transient-mark-mode mark-active)))
  22735. (set-mark
  22736. (save-excursion
  22737. (goto-char (mark))
  22738. (goto-char (org-element-property :end (org-element-at-point)))))
  22739. (let ((element (org-element-at-point)))
  22740. (end-of-line)
  22741. (push-mark (org-element-property :end element) t t)
  22742. (goto-char (org-element-property :begin element))))))
  22743. (defun org-narrow-to-element ()
  22744. "Narrow buffer to current element."
  22745. (interactive)
  22746. (let ((elem (org-element-at-point)))
  22747. (cond
  22748. ((eq (car elem) 'headline)
  22749. (narrow-to-region
  22750. (org-element-property :begin elem)
  22751. (org-element-property :end elem)))
  22752. ((memq (car elem) org-element-greater-elements)
  22753. (narrow-to-region
  22754. (org-element-property :contents-begin elem)
  22755. (org-element-property :contents-end elem)))
  22756. (t
  22757. (narrow-to-region
  22758. (org-element-property :begin elem)
  22759. (org-element-property :end elem))))))
  22760. (defun org-transpose-element ()
  22761. "Transpose current and previous elements, keeping blank lines between.
  22762. Point is moved after both elements."
  22763. (interactive)
  22764. (org-skip-whitespace)
  22765. (let ((end (org-element-property :end (org-element-at-point))))
  22766. (org-drag-element-backward)
  22767. (goto-char end)))
  22768. (defun org-unindent-buffer ()
  22769. "Un-indent the visible part of the buffer.
  22770. Relative indentation (between items, inside blocks, etc.) isn't
  22771. modified."
  22772. (interactive)
  22773. (unless (eq major-mode 'org-mode)
  22774. (user-error "Cannot un-indent a buffer not in Org mode"))
  22775. (letrec ((parse-tree (org-element-parse-buffer 'greater-element))
  22776. (unindent-tree
  22777. (lambda (contents)
  22778. (dolist (element (reverse contents))
  22779. (if (memq (org-element-type element) '(headline section))
  22780. (funcall unindent-tree (org-element-contents element))
  22781. (save-excursion
  22782. (save-restriction
  22783. (narrow-to-region
  22784. (org-element-property :begin element)
  22785. (org-element-property :end element))
  22786. (org-do-remove-indentation))))))))
  22787. (funcall unindent-tree (org-element-contents parse-tree))))
  22788. (defun org-show-children (&optional level)
  22789. "Show all direct subheadings of this heading.
  22790. Prefix arg LEVEL is how many levels below the current level
  22791. should be shown. Default is enough to cause the following
  22792. heading to appear."
  22793. (interactive "p")
  22794. ;; If `orgstruct-mode' is active, use the slower version.
  22795. (if orgstruct-mode (call-interactively #'outline-show-children)
  22796. (save-excursion
  22797. (org-back-to-heading t)
  22798. (let* ((current-level (funcall outline-level))
  22799. (max-level (org-get-valid-level
  22800. current-level
  22801. (if level (prefix-numeric-value level) 1)))
  22802. (end (save-excursion (org-end-of-subtree t t)))
  22803. (regexp-fmt "^\\*\\{%d,%s\\}\\(?: \\|$\\)")
  22804. (past-first-child nil)
  22805. ;; Make sure to skip inlinetasks.
  22806. (re (format regexp-fmt
  22807. current-level
  22808. (cond
  22809. ((not (featurep 'org-inlinetask)) "")
  22810. (org-odd-levels-only (- (* 2 org-inlinetask-min-level)
  22811. 3))
  22812. (t (1- org-inlinetask-min-level))))))
  22813. ;; Display parent heading.
  22814. (outline-flag-region (line-end-position 0) (line-end-position) nil)
  22815. (forward-line)
  22816. ;; Display children. First child may be deeper than expected
  22817. ;; MAX-LEVEL. Since we want to display it anyway, adjust
  22818. ;; MAX-LEVEL accordingly.
  22819. (while (re-search-forward re end t)
  22820. (unless past-first-child
  22821. (setq re (format regexp-fmt
  22822. current-level
  22823. (max (funcall outline-level) max-level)))
  22824. (setq past-first-child t))
  22825. (outline-flag-region
  22826. (line-end-position 0) (line-end-position) nil))))))
  22827. (defun org-show-subtree ()
  22828. "Show everything after this heading at deeper levels."
  22829. (interactive)
  22830. (outline-flag-region
  22831. (point)
  22832. (save-excursion
  22833. (org-end-of-subtree t t))
  22834. nil))
  22835. (defun org-show-entry ()
  22836. "Show the body directly following this heading.
  22837. Show the heading too, if it is currently invisible."
  22838. (interactive)
  22839. (save-excursion
  22840. (ignore-errors
  22841. (org-back-to-heading t)
  22842. (outline-flag-region
  22843. (max (point-min) (1- (point)))
  22844. (save-excursion
  22845. (if (re-search-forward
  22846. (concat "[\r\n]\\(" org-outline-regexp "\\)") nil t)
  22847. (match-beginning 1)
  22848. (point-max)))
  22849. nil)
  22850. (org-cycle-hide-drawers 'children))))
  22851. (defun org-make-options-regexp (kwds &optional extra)
  22852. "Make a regular expression for keyword lines.
  22853. KWDS is a list of keywords, as strings. Optional argument EXTRA,
  22854. when non-nil, is a regexp matching keywords names."
  22855. (concat "^[ \t]*#\\+\\("
  22856. (regexp-opt kwds)
  22857. (and extra (concat (and kwds "\\|") extra))
  22858. "\\):[ \t]*\\(.*\\)"))
  22859. ;;;; Integration with and fixes for other packages
  22860. ;;; Imenu support
  22861. (defvar-local org-imenu-markers nil
  22862. "All markers currently used by Imenu.")
  22863. (defun org-imenu-new-marker (&optional pos)
  22864. "Return a new marker for use by Imenu, and remember the marker."
  22865. (let ((m (make-marker)))
  22866. (move-marker m (or pos (point)))
  22867. (push m org-imenu-markers)
  22868. m))
  22869. (defun org-imenu-get-tree ()
  22870. "Produce the index for Imenu."
  22871. (dolist (x org-imenu-markers) (move-marker x nil))
  22872. (setq org-imenu-markers nil)
  22873. (let* ((n org-imenu-depth)
  22874. (re (concat "^" (org-get-limited-outline-regexp)))
  22875. (subs (make-vector (1+ n) nil))
  22876. (last-level 0)
  22877. m level head0 head)
  22878. (save-excursion
  22879. (save-restriction
  22880. (widen)
  22881. (goto-char (point-max))
  22882. (while (re-search-backward re nil t)
  22883. (setq level (org-reduced-level (funcall outline-level)))
  22884. (when (and (<= level n)
  22885. (looking-at org-complex-heading-regexp)
  22886. (setq head0 (org-match-string-no-properties 4)))
  22887. (setq head (org-link-display-format head0)
  22888. m (org-imenu-new-marker))
  22889. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  22890. (if (>= level last-level)
  22891. (push (cons head m) (aref subs level))
  22892. (push (cons head (aref subs (1+ level))) (aref subs level))
  22893. (cl-loop for i from (1+ level) to n do (aset subs i nil)))
  22894. (setq last-level level)))))
  22895. (aref subs 1)))
  22896. (eval-after-load "imenu"
  22897. '(progn
  22898. (add-hook 'imenu-after-jump-hook
  22899. (lambda ()
  22900. (when (derived-mode-p 'org-mode)
  22901. (org-show-context 'org-goto))))))
  22902. (defun org-link-display-format (s)
  22903. "Replace links in string S with their description.
  22904. If there is no description, use the link target."
  22905. (save-match-data
  22906. (replace-regexp-in-string
  22907. org-bracket-link-analytic-regexp
  22908. (lambda (m)
  22909. (if (match-end 5) (match-string 5 m)
  22910. (concat (match-string 1 m) (match-string 3 m))))
  22911. s nil t)))
  22912. (defun org-toggle-link-display ()
  22913. "Toggle the literal or descriptive display of links."
  22914. (interactive)
  22915. (if org-descriptive-links
  22916. (progn (org-remove-from-invisibility-spec '(org-link))
  22917. (org-restart-font-lock)
  22918. (setq org-descriptive-links nil))
  22919. (progn (add-to-invisibility-spec '(org-link))
  22920. (org-restart-font-lock)
  22921. (setq org-descriptive-links t))))
  22922. ;; Speedbar support
  22923. (defvar org-speedbar-restriction-lock-overlay (make-overlay 1 1)
  22924. "Overlay marking the agenda restriction line in speedbar.")
  22925. (overlay-put org-speedbar-restriction-lock-overlay
  22926. 'face 'org-agenda-restriction-lock)
  22927. (overlay-put org-speedbar-restriction-lock-overlay
  22928. 'help-echo "Agendas are currently limited to this item.")
  22929. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  22930. (defun org-speedbar-set-agenda-restriction ()
  22931. "Restrict future agenda commands to the location at point in speedbar.
  22932. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  22933. (interactive)
  22934. (require 'org-agenda)
  22935. (let (p m tp np dir txt)
  22936. (cond
  22937. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  22938. 'org-imenu t))
  22939. (setq m (get-text-property p 'org-imenu-marker))
  22940. (with-current-buffer (marker-buffer m)
  22941. (goto-char m)
  22942. (org-agenda-set-restriction-lock 'subtree)))
  22943. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  22944. 'speedbar-function 'speedbar-find-file))
  22945. (setq tp (previous-single-property-change
  22946. (1+ p) 'speedbar-function)
  22947. np (next-single-property-change
  22948. tp 'speedbar-function)
  22949. dir (speedbar-line-directory)
  22950. txt (buffer-substring-no-properties (or tp (point-min))
  22951. (or np (point-max))))
  22952. (with-current-buffer (find-file-noselect
  22953. (let ((default-directory dir))
  22954. (expand-file-name txt)))
  22955. (unless (derived-mode-p 'org-mode)
  22956. (user-error "Cannot restrict to non-Org-mode file"))
  22957. (org-agenda-set-restriction-lock 'file)))
  22958. (t (user-error "Don't know how to restrict Org-mode's agenda")))
  22959. (move-overlay org-speedbar-restriction-lock-overlay
  22960. (point-at-bol) (point-at-eol))
  22961. (setq current-prefix-arg nil)
  22962. (org-agenda-maybe-redo)))
  22963. (defvar speedbar-file-key-map)
  22964. (declare-function speedbar-add-supported-extension "speedbar" (extension))
  22965. (eval-after-load "speedbar"
  22966. '(progn
  22967. (speedbar-add-supported-extension ".org")
  22968. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  22969. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  22970. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  22971. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  22972. (add-hook 'speedbar-visiting-tag-hook
  22973. (lambda () (and (derived-mode-p 'org-mode) (org-show-context 'org-goto))))))
  22974. ;;; Fixes and Hacks for problems with other packages
  22975. (defun org--flyspell-object-check-p (element)
  22976. "Non-nil when Flyspell can check object at point.
  22977. ELEMENT is the element at point."
  22978. (let ((object (save-excursion
  22979. (when (org-looking-at-p "\\>") (backward-char))
  22980. (org-element-context element))))
  22981. (cl-case (org-element-type object)
  22982. ;; Prevent checks in links due to keybinding conflict with
  22983. ;; Flyspell.
  22984. ((code entity export-snippet inline-babel-call
  22985. inline-src-block line-break latex-fragment link macro
  22986. statistics-cookie target timestamp verbatim)
  22987. nil)
  22988. (footnote-reference
  22989. ;; Only in inline footnotes, within the definition.
  22990. (and (eq (org-element-property :type object) 'inline)
  22991. (< (save-excursion
  22992. (goto-char (org-element-property :begin object))
  22993. (search-forward ":" nil t 2))
  22994. (point))))
  22995. (otherwise t))))
  22996. (defun org-mode-flyspell-verify ()
  22997. "Function used for `flyspell-generic-check-word-predicate'."
  22998. (if (org-at-heading-p)
  22999. ;; At a headline or an inlinetask, check title only. This is
  23000. ;; faster than relying on `org-element-at-point'.
  23001. (and (save-excursion (beginning-of-line)
  23002. (and (let ((case-fold-search t))
  23003. (not (looking-at "\\*+ END[ \t]*$")))
  23004. (looking-at org-complex-heading-regexp)))
  23005. (match-beginning 4)
  23006. (>= (point) (match-beginning 4))
  23007. (or (not (match-beginning 5))
  23008. (< (point) (match-beginning 5))))
  23009. (let* ((element (org-element-at-point))
  23010. (post-affiliated (org-element-property :post-affiliated element)))
  23011. (cond
  23012. ;; Ignore checks in all affiliated keywords but captions.
  23013. ((< (point) post-affiliated)
  23014. (and (save-excursion
  23015. (beginning-of-line)
  23016. (let ((case-fold-search t)) (looking-at "[ \t]*#\\+CAPTION:")))
  23017. (> (point) (match-end 0))
  23018. (org--flyspell-object-check-p element)))
  23019. ;; Ignore checks in LOGBOOK (or equivalent) drawer.
  23020. ((let ((log (org-log-into-drawer)))
  23021. (and log
  23022. (let ((drawer (org-element-lineage element '(drawer))))
  23023. (and drawer
  23024. (eq (compare-strings
  23025. log nil nil
  23026. (org-element-property :drawer-name drawer) nil nil t)
  23027. t)))))
  23028. nil)
  23029. (t
  23030. (cl-case (org-element-type element)
  23031. ((comment quote-section) t)
  23032. (comment-block
  23033. ;; Allow checks between block markers, not on them.
  23034. (and (> (line-beginning-position) post-affiliated)
  23035. (save-excursion
  23036. (end-of-line)
  23037. (skip-chars-forward " \r\t\n")
  23038. (< (point) (org-element-property :end element)))))
  23039. ;; Arbitrary list of keywords where checks are meaningful.
  23040. ;; Make sure point is on the value part of the element.
  23041. (keyword
  23042. (and (member (org-element-property :key element)
  23043. '("DESCRIPTION" "TITLE"))
  23044. (save-excursion
  23045. (search-backward ":" (line-beginning-position) t))))
  23046. ;; Check is globally allowed in paragraphs verse blocks and
  23047. ;; table rows (after affiliated keywords) but some objects
  23048. ;; must not be affected.
  23049. ((paragraph table-row verse-block)
  23050. (let ((cbeg (org-element-property :contents-begin element))
  23051. (cend (org-element-property :contents-end element)))
  23052. (and cbeg (>= (point) cbeg) (< (point) cend)
  23053. (org--flyspell-object-check-p element))))))))))
  23054. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  23055. (defun org-remove-flyspell-overlays-in (beg end)
  23056. "Remove flyspell overlays in region."
  23057. (and (org-bound-and-true-p flyspell-mode)
  23058. (fboundp 'flyspell-delete-region-overlays)
  23059. (flyspell-delete-region-overlays beg end)))
  23060. (defvar flyspell-delayed-commands)
  23061. (eval-after-load "flyspell"
  23062. '(add-to-list 'flyspell-delayed-commands 'org-self-insert-command))
  23063. ;; Make `bookmark-jump' shows the jump location if it was hidden.
  23064. (eval-after-load "bookmark"
  23065. '(if (boundp 'bookmark-after-jump-hook)
  23066. ;; We can use the hook
  23067. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  23068. ;; Hook not available, use advice
  23069. (defadvice bookmark-jump (after org-make-visible activate)
  23070. "Make the position visible."
  23071. (org-bookmark-jump-unhide))))
  23072. ;; Make sure saveplace shows the location if it was hidden
  23073. (eval-after-load "saveplace"
  23074. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  23075. "Make the position visible."
  23076. (org-bookmark-jump-unhide)))
  23077. ;; Make sure ecb shows the location if it was hidden
  23078. (eval-after-load "ecb"
  23079. '(defadvice ecb-method-clicked (after esf/org-show-context activate)
  23080. "Make hierarchy visible when jumping into location from ECB tree buffer."
  23081. (when (derived-mode-p 'org-mode)
  23082. (org-show-context))))
  23083. (defun org-bookmark-jump-unhide ()
  23084. "Unhide the current position, to show the bookmark location."
  23085. (and (derived-mode-p 'org-mode)
  23086. (or (outline-invisible-p)
  23087. (save-excursion (goto-char (max (point-min) (1- (point))))
  23088. (outline-invisible-p)))
  23089. (org-show-context 'bookmark-jump)))
  23090. (defun org-mark-jump-unhide ()
  23091. "Make the point visible with `org-show-context' after jumping to the mark."
  23092. (when (and (derived-mode-p 'org-mode)
  23093. (outline-invisible-p))
  23094. (org-show-context 'mark-goto)))
  23095. (eval-after-load "simple"
  23096. '(defadvice pop-to-mark-command (after org-make-visible activate)
  23097. "Make the point visible with `org-show-context'."
  23098. (org-mark-jump-unhide)))
  23099. (eval-after-load "simple"
  23100. '(defadvice exchange-point-and-mark (after org-make-visible activate)
  23101. "Make the point visible with `org-show-context'."
  23102. (org-mark-jump-unhide)))
  23103. (eval-after-load "simple"
  23104. '(defadvice pop-global-mark (after org-make-visible activate)
  23105. "Make the point visible with `org-show-context'."
  23106. (org-mark-jump-unhide)))
  23107. ;; Make session.el ignore our circular variable
  23108. (defvar session-globals-exclude)
  23109. (eval-after-load "session"
  23110. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  23111. ;;;; Finish up
  23112. (provide 'org)
  23113. (run-hooks 'org-load-hook)
  23114. ;;; org.el ends here