org.el 961 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106171071710817109171101711117112171131711417115171161711717118171191712017121171221712317124171251712617127171281712917130171311713217133171341713517136171371713817139171401714117142171431714417145171461714717148171491715017151171521715317154171551715617157171581715917160171611716217163171641716517166171671716817169171701717117172171731717417175171761717717178171791718017181171821718317184171851718617187171881718917190171911719217193171941719517196171971719817199172001720117202172031720417205172061720717208172091721017211172121721317214172151721617217172181721917220172211722217223172241722517226172271722817229172301723117232172331723417235172361723717238172391724017241172421724317244172451724617247172481724917250172511725217253172541725517256172571725817259172601726117262172631726417265172661726717268172691727017271172721727317274172751727617277172781727917280172811728217283172841728517286172871728817289172901729117292172931729417295172961729717298172991730017301173021730317304173051730617307173081730917310173111731217313173141731517316173171731817319173201732117322173231732417325173261732717328173291733017331173321733317334173351733617337173381733917340173411734217343173441734517346173471734817349173501735117352173531735417355173561735717358173591736017361173621736317364173651736617367173681736917370173711737217373173741737517376173771737817379173801738117382173831738417385173861738717388173891739017391173921739317394173951739617397173981739917400174011740217403174041740517406174071740817409174101741117412174131741417415174161741717418174191742017421174221742317424174251742617427174281742917430174311743217433174341743517436174371743817439174401744117442174431744417445174461744717448174491745017451174521745317454174551745617457174581745917460174611746217463174641746517466174671746817469174701747117472174731747417475174761747717478174791748017481174821748317484174851748617487174881748917490174911749217493174941749517496174971749817499175001750117502175031750417505175061750717508175091751017511175121751317514175151751617517175181751917520175211752217523175241752517526175271752817529175301753117532175331753417535175361753717538175391754017541175421754317544175451754617547175481754917550175511755217553175541755517556175571755817559175601756117562175631756417565175661756717568175691757017571175721757317574175751757617577175781757917580175811758217583175841758517586175871758817589175901759117592175931759417595175961759717598175991760017601176021760317604176051760617607176081760917610176111761217613176141761517616176171761817619176201762117622176231762417625176261762717628176291763017631176321763317634176351763617637176381763917640176411764217643176441764517646176471764817649176501765117652176531765417655176561765717658176591766017661176621766317664176651766617667176681766917670176711767217673176741767517676176771767817679176801768117682176831768417685176861768717688176891769017691176921769317694176951769617697176981769917700177011770217703177041770517706177071770817709177101771117712177131771417715177161771717718177191772017721177221772317724177251772617727177281772917730177311773217733177341773517736177371773817739177401774117742177431774417745177461774717748177491775017751177521775317754177551775617757177581775917760177611776217763177641776517766177671776817769177701777117772177731777417775177761777717778177791778017781177821778317784177851778617787177881778917790177911779217793177941779517796177971779817799178001780117802178031780417805178061780717808178091781017811178121781317814178151781617817178181781917820178211782217823178241782517826178271782817829178301783117832178331783417835178361783717838178391784017841178421784317844178451784617847178481784917850178511785217853178541785517856178571785817859178601786117862178631786417865178661786717868178691787017871178721787317874178751787617877178781787917880178811788217883178841788517886178871788817889178901789117892178931789417895178961789717898178991790017901179021790317904179051790617907179081790917910179111791217913179141791517916179171791817919179201792117922179231792417925179261792717928179291793017931179321793317934179351793617937179381793917940179411794217943179441794517946179471794817949179501795117952179531795417955179561795717958179591796017961179621796317964179651796617967179681796917970179711797217973179741797517976179771797817979179801798117982179831798417985179861798717988179891799017991179921799317994179951799617997179981799918000180011800218003180041800518006180071800818009180101801118012180131801418015180161801718018180191802018021180221802318024180251802618027180281802918030180311803218033180341803518036180371803818039180401804118042180431804418045180461804718048180491805018051180521805318054180551805618057180581805918060180611806218063180641806518066180671806818069180701807118072180731807418075180761807718078180791808018081180821808318084180851808618087180881808918090180911809218093180941809518096180971809818099181001810118102181031810418105181061810718108181091811018111181121811318114181151811618117181181811918120181211812218123181241812518126181271812818129181301813118132181331813418135181361813718138181391814018141181421814318144181451814618147181481814918150181511815218153181541815518156181571815818159181601816118162181631816418165181661816718168181691817018171181721817318174181751817618177181781817918180181811818218183181841818518186181871818818189181901819118192181931819418195181961819718198181991820018201182021820318204182051820618207182081820918210182111821218213182141821518216182171821818219182201822118222182231822418225182261822718228182291823018231182321823318234182351823618237182381823918240182411824218243182441824518246182471824818249182501825118252182531825418255182561825718258182591826018261182621826318264182651826618267182681826918270182711827218273182741827518276182771827818279182801828118282182831828418285182861828718288182891829018291182921829318294182951829618297182981829918300183011830218303183041830518306183071830818309183101831118312183131831418315183161831718318183191832018321183221832318324183251832618327183281832918330183311833218333183341833518336183371833818339183401834118342183431834418345183461834718348183491835018351183521835318354183551835618357183581835918360183611836218363183641836518366183671836818369183701837118372183731837418375183761837718378183791838018381183821838318384183851838618387183881838918390183911839218393183941839518396183971839818399184001840118402184031840418405184061840718408184091841018411184121841318414184151841618417184181841918420184211842218423184241842518426184271842818429184301843118432184331843418435184361843718438184391844018441184421844318444184451844618447184481844918450184511845218453184541845518456184571845818459184601846118462184631846418465184661846718468184691847018471184721847318474184751847618477184781847918480184811848218483184841848518486184871848818489184901849118492184931849418495184961849718498184991850018501185021850318504185051850618507185081850918510185111851218513185141851518516185171851818519185201852118522185231852418525185261852718528185291853018531185321853318534185351853618537185381853918540185411854218543185441854518546185471854818549185501855118552185531855418555185561855718558185591856018561185621856318564185651856618567185681856918570185711857218573185741857518576185771857818579185801858118582185831858418585185861858718588185891859018591185921859318594185951859618597185981859918600186011860218603186041860518606186071860818609186101861118612186131861418615186161861718618186191862018621186221862318624186251862618627186281862918630186311863218633186341863518636186371863818639186401864118642186431864418645186461864718648186491865018651186521865318654186551865618657186581865918660186611866218663186641866518666186671866818669186701867118672186731867418675186761867718678186791868018681186821868318684186851868618687186881868918690186911869218693186941869518696186971869818699187001870118702187031870418705187061870718708187091871018711187121871318714187151871618717187181871918720187211872218723187241872518726187271872818729187301873118732187331873418735187361873718738187391874018741187421874318744187451874618747187481874918750187511875218753187541875518756187571875818759187601876118762187631876418765187661876718768187691877018771187721877318774187751877618777187781877918780187811878218783187841878518786187871878818789187901879118792187931879418795187961879718798187991880018801188021880318804188051880618807188081880918810188111881218813188141881518816188171881818819188201882118822188231882418825188261882718828188291883018831188321883318834188351883618837188381883918840188411884218843188441884518846188471884818849188501885118852188531885418855188561885718858188591886018861188621886318864188651886618867188681886918870188711887218873188741887518876188771887818879188801888118882188831888418885188861888718888188891889018891188921889318894188951889618897188981889918900189011890218903189041890518906189071890818909189101891118912189131891418915189161891718918189191892018921189221892318924189251892618927189281892918930189311893218933189341893518936189371893818939189401894118942189431894418945189461894718948189491895018951189521895318954189551895618957189581895918960189611896218963189641896518966189671896818969189701897118972189731897418975189761897718978189791898018981189821898318984189851898618987189881898918990189911899218993189941899518996189971899818999190001900119002190031900419005190061900719008190091901019011190121901319014190151901619017190181901919020190211902219023190241902519026190271902819029190301903119032190331903419035190361903719038190391904019041190421904319044190451904619047190481904919050190511905219053190541905519056190571905819059190601906119062190631906419065190661906719068190691907019071190721907319074190751907619077190781907919080190811908219083190841908519086190871908819089190901909119092190931909419095190961909719098190991910019101191021910319104191051910619107191081910919110191111911219113191141911519116191171911819119191201912119122191231912419125191261912719128191291913019131191321913319134191351913619137191381913919140191411914219143191441914519146191471914819149191501915119152191531915419155191561915719158191591916019161191621916319164191651916619167191681916919170191711917219173191741917519176191771917819179191801918119182191831918419185191861918719188191891919019191191921919319194191951919619197191981919919200192011920219203192041920519206192071920819209192101921119212192131921419215192161921719218192191922019221192221922319224192251922619227192281922919230192311923219233192341923519236192371923819239192401924119242192431924419245192461924719248192491925019251192521925319254192551925619257192581925919260192611926219263192641926519266192671926819269192701927119272192731927419275192761927719278192791928019281192821928319284192851928619287192881928919290192911929219293192941929519296192971929819299193001930119302193031930419305193061930719308193091931019311193121931319314193151931619317193181931919320193211932219323193241932519326193271932819329193301933119332193331933419335193361933719338193391934019341193421934319344193451934619347193481934919350193511935219353193541935519356193571935819359193601936119362193631936419365193661936719368193691937019371193721937319374193751937619377193781937919380193811938219383193841938519386193871938819389193901939119392193931939419395193961939719398193991940019401194021940319404194051940619407194081940919410194111941219413194141941519416194171941819419194201942119422194231942419425194261942719428194291943019431194321943319434194351943619437194381943919440194411944219443194441944519446194471944819449194501945119452194531945419455194561945719458194591946019461194621946319464194651946619467194681946919470194711947219473194741947519476194771947819479194801948119482194831948419485194861948719488194891949019491194921949319494194951949619497194981949919500195011950219503195041950519506195071950819509195101951119512195131951419515195161951719518195191952019521195221952319524195251952619527195281952919530195311953219533195341953519536195371953819539195401954119542195431954419545195461954719548195491955019551195521955319554195551955619557195581955919560195611956219563195641956519566195671956819569195701957119572195731957419575195761957719578195791958019581195821958319584195851958619587195881958919590195911959219593195941959519596195971959819599196001960119602196031960419605196061960719608196091961019611196121961319614196151961619617196181961919620196211962219623196241962519626196271962819629196301963119632196331963419635196361963719638196391964019641196421964319644196451964619647196481964919650196511965219653196541965519656196571965819659196601966119662196631966419665196661966719668196691967019671196721967319674196751967619677196781967919680196811968219683196841968519686196871968819689196901969119692196931969419695196961969719698196991970019701197021970319704197051970619707197081970919710197111971219713197141971519716197171971819719197201972119722197231972419725197261972719728197291973019731197321973319734197351973619737197381973919740197411974219743197441974519746197471974819749197501975119752197531975419755197561975719758197591976019761197621976319764197651976619767197681976919770197711977219773197741977519776197771977819779197801978119782197831978419785197861978719788197891979019791197921979319794197951979619797197981979919800198011980219803198041980519806198071980819809198101981119812198131981419815198161981719818198191982019821198221982319824198251982619827198281982919830198311983219833198341983519836198371983819839198401984119842198431984419845198461984719848198491985019851198521985319854198551985619857198581985919860198611986219863198641986519866198671986819869198701987119872198731987419875198761987719878198791988019881198821988319884198851988619887198881988919890198911989219893198941989519896198971989819899199001990119902199031990419905199061990719908199091991019911199121991319914199151991619917199181991919920199211992219923199241992519926199271992819929199301993119932199331993419935199361993719938199391994019941199421994319944199451994619947199481994919950199511995219953199541995519956199571995819959199601996119962199631996419965199661996719968199691997019971199721997319974199751997619977199781997919980199811998219983199841998519986199871998819989199901999119992199931999419995199961999719998199992000020001200022000320004200052000620007200082000920010200112001220013200142001520016200172001820019200202002120022200232002420025200262002720028200292003020031200322003320034200352003620037200382003920040200412004220043200442004520046200472004820049200502005120052200532005420055200562005720058200592006020061200622006320064200652006620067200682006920070200712007220073200742007520076200772007820079200802008120082200832008420085200862008720088200892009020091200922009320094200952009620097200982009920100201012010220103201042010520106201072010820109201102011120112201132011420115201162011720118201192012020121201222012320124201252012620127201282012920130201312013220133201342013520136201372013820139201402014120142201432014420145201462014720148201492015020151201522015320154201552015620157201582015920160201612016220163201642016520166201672016820169201702017120172201732017420175201762017720178201792018020181201822018320184201852018620187201882018920190201912019220193201942019520196201972019820199202002020120202202032020420205202062020720208202092021020211202122021320214202152021620217202182021920220202212022220223202242022520226202272022820229202302023120232202332023420235202362023720238202392024020241202422024320244202452024620247202482024920250202512025220253202542025520256202572025820259202602026120262202632026420265202662026720268202692027020271202722027320274202752027620277202782027920280202812028220283202842028520286202872028820289202902029120292202932029420295202962029720298202992030020301203022030320304203052030620307203082030920310203112031220313203142031520316203172031820319203202032120322203232032420325203262032720328203292033020331203322033320334203352033620337203382033920340203412034220343203442034520346203472034820349203502035120352203532035420355203562035720358203592036020361203622036320364203652036620367203682036920370203712037220373203742037520376203772037820379203802038120382203832038420385203862038720388203892039020391203922039320394203952039620397203982039920400204012040220403204042040520406204072040820409204102041120412204132041420415204162041720418204192042020421204222042320424204252042620427204282042920430204312043220433204342043520436204372043820439204402044120442204432044420445204462044720448204492045020451204522045320454204552045620457204582045920460204612046220463204642046520466204672046820469204702047120472204732047420475204762047720478204792048020481204822048320484204852048620487204882048920490204912049220493204942049520496204972049820499205002050120502205032050420505205062050720508205092051020511205122051320514205152051620517205182051920520205212052220523205242052520526205272052820529205302053120532205332053420535205362053720538205392054020541205422054320544205452054620547205482054920550205512055220553205542055520556205572055820559205602056120562205632056420565205662056720568205692057020571205722057320574205752057620577205782057920580205812058220583205842058520586205872058820589205902059120592205932059420595205962059720598205992060020601206022060320604206052060620607206082060920610206112061220613206142061520616206172061820619206202062120622206232062420625206262062720628206292063020631206322063320634206352063620637206382063920640206412064220643206442064520646206472064820649206502065120652206532065420655206562065720658206592066020661206622066320664206652066620667206682066920670206712067220673206742067520676206772067820679206802068120682206832068420685206862068720688206892069020691206922069320694206952069620697206982069920700207012070220703207042070520706207072070820709207102071120712207132071420715207162071720718207192072020721207222072320724207252072620727207282072920730207312073220733207342073520736207372073820739207402074120742207432074420745207462074720748207492075020751207522075320754207552075620757207582075920760207612076220763207642076520766207672076820769207702077120772207732077420775207762077720778207792078020781207822078320784207852078620787207882078920790207912079220793207942079520796207972079820799208002080120802208032080420805208062080720808208092081020811208122081320814208152081620817208182081920820208212082220823208242082520826208272082820829208302083120832208332083420835208362083720838208392084020841208422084320844208452084620847208482084920850208512085220853208542085520856208572085820859208602086120862208632086420865208662086720868208692087020871208722087320874208752087620877208782087920880208812088220883208842088520886208872088820889208902089120892208932089420895208962089720898208992090020901209022090320904209052090620907209082090920910209112091220913209142091520916209172091820919209202092120922209232092420925209262092720928209292093020931209322093320934209352093620937209382093920940209412094220943209442094520946209472094820949209502095120952209532095420955209562095720958209592096020961209622096320964209652096620967209682096920970209712097220973209742097520976209772097820979209802098120982209832098420985209862098720988209892099020991209922099320994209952099620997209982099921000210012100221003210042100521006210072100821009210102101121012210132101421015210162101721018210192102021021210222102321024210252102621027210282102921030210312103221033210342103521036210372103821039210402104121042210432104421045210462104721048210492105021051210522105321054210552105621057210582105921060210612106221063210642106521066210672106821069210702107121072210732107421075210762107721078210792108021081210822108321084210852108621087210882108921090210912109221093210942109521096210972109821099211002110121102211032110421105211062110721108211092111021111211122111321114211152111621117211182111921120211212112221123211242112521126211272112821129211302113121132211332113421135211362113721138211392114021141211422114321144211452114621147211482114921150211512115221153211542115521156211572115821159211602116121162211632116421165211662116721168211692117021171211722117321174211752117621177211782117921180211812118221183211842118521186211872118821189211902119121192211932119421195211962119721198211992120021201212022120321204212052120621207212082120921210212112121221213212142121521216212172121821219212202122121222212232122421225212262122721228212292123021231212322123321234212352123621237212382123921240212412124221243212442124521246212472124821249212502125121252212532125421255212562125721258212592126021261212622126321264212652126621267212682126921270212712127221273212742127521276212772127821279212802128121282212832128421285212862128721288212892129021291212922129321294212952129621297212982129921300213012130221303213042130521306213072130821309213102131121312213132131421315213162131721318213192132021321213222132321324213252132621327213282132921330213312133221333213342133521336213372133821339213402134121342213432134421345213462134721348213492135021351213522135321354213552135621357213582135921360213612136221363213642136521366213672136821369213702137121372213732137421375213762137721378213792138021381213822138321384213852138621387213882138921390213912139221393213942139521396213972139821399214002140121402214032140421405214062140721408214092141021411214122141321414214152141621417214182141921420214212142221423214242142521426214272142821429214302143121432214332143421435214362143721438214392144021441214422144321444214452144621447214482144921450214512145221453214542145521456214572145821459214602146121462214632146421465214662146721468214692147021471214722147321474214752147621477214782147921480214812148221483214842148521486214872148821489214902149121492214932149421495214962149721498214992150021501215022150321504215052150621507215082150921510215112151221513215142151521516215172151821519215202152121522215232152421525215262152721528215292153021531215322153321534215352153621537215382153921540215412154221543215442154521546215472154821549215502155121552215532155421555215562155721558215592156021561215622156321564215652156621567215682156921570215712157221573215742157521576215772157821579215802158121582215832158421585215862158721588215892159021591215922159321594215952159621597215982159921600216012160221603216042160521606216072160821609216102161121612216132161421615216162161721618216192162021621216222162321624216252162621627216282162921630216312163221633216342163521636216372163821639216402164121642216432164421645216462164721648216492165021651216522165321654216552165621657216582165921660216612166221663216642166521666216672166821669216702167121672216732167421675216762167721678216792168021681216822168321684216852168621687216882168921690216912169221693216942169521696216972169821699217002170121702217032170421705217062170721708217092171021711217122171321714217152171621717217182171921720217212172221723217242172521726217272172821729217302173121732217332173421735217362173721738217392174021741217422174321744217452174621747217482174921750217512175221753217542175521756217572175821759217602176121762217632176421765217662176721768217692177021771217722177321774217752177621777217782177921780217812178221783217842178521786217872178821789217902179121792217932179421795217962179721798217992180021801218022180321804218052180621807218082180921810218112181221813218142181521816218172181821819218202182121822218232182421825218262182721828218292183021831218322183321834218352183621837218382183921840218412184221843218442184521846218472184821849218502185121852218532185421855218562185721858218592186021861218622186321864218652186621867218682186921870218712187221873218742187521876218772187821879218802188121882218832188421885218862188721888218892189021891218922189321894218952189621897218982189921900219012190221903219042190521906219072190821909219102191121912219132191421915219162191721918219192192021921219222192321924219252192621927219282192921930219312193221933219342193521936219372193821939219402194121942219432194421945219462194721948219492195021951219522195321954219552195621957219582195921960219612196221963219642196521966219672196821969219702197121972219732197421975219762197721978219792198021981219822198321984219852198621987219882198921990219912199221993219942199521996219972199821999220002200122002220032200422005220062200722008220092201022011220122201322014220152201622017220182201922020220212202222023220242202522026220272202822029220302203122032220332203422035220362203722038220392204022041220422204322044220452204622047220482204922050220512205222053220542205522056220572205822059220602206122062220632206422065220662206722068220692207022071220722207322074220752207622077220782207922080220812208222083220842208522086220872208822089220902209122092220932209422095220962209722098220992210022101221022210322104221052210622107221082210922110221112211222113221142211522116221172211822119221202212122122221232212422125221262212722128221292213022131221322213322134221352213622137221382213922140221412214222143221442214522146221472214822149221502215122152221532215422155221562215722158221592216022161221622216322164221652216622167221682216922170221712217222173221742217522176221772217822179221802218122182221832218422185221862218722188221892219022191221922219322194221952219622197221982219922200222012220222203222042220522206222072220822209222102221122212222132221422215222162221722218222192222022221222222222322224222252222622227222282222922230222312223222233222342223522236222372223822239222402224122242222432224422245222462224722248222492225022251222522225322254222552225622257222582225922260222612226222263222642226522266222672226822269222702227122272222732227422275222762227722278222792228022281222822228322284222852228622287222882228922290222912229222293222942229522296222972229822299223002230122302223032230422305223062230722308223092231022311223122231322314223152231622317223182231922320223212232222323223242232522326223272232822329223302233122332223332233422335223362233722338223392234022341223422234322344223452234622347223482234922350223512235222353223542235522356223572235822359223602236122362223632236422365223662236722368223692237022371223722237322374223752237622377223782237922380223812238222383223842238522386223872238822389223902239122392223932239422395223962239722398223992240022401224022240322404224052240622407224082240922410224112241222413224142241522416224172241822419224202242122422224232242422425224262242722428224292243022431224322243322434224352243622437224382243922440224412244222443224442244522446224472244822449224502245122452224532245422455224562245722458224592246022461224622246322464224652246622467224682246922470224712247222473224742247522476224772247822479224802248122482224832248422485224862248722488224892249022491224922249322494224952249622497224982249922500225012250222503225042250522506225072250822509225102251122512225132251422515225162251722518225192252022521225222252322524225252252622527225282252922530225312253222533225342253522536225372253822539225402254122542225432254422545225462254722548225492255022551225522255322554225552255622557225582255922560225612256222563225642256522566225672256822569225702257122572225732257422575225762257722578225792258022581225822258322584225852258622587225882258922590225912259222593225942259522596225972259822599226002260122602226032260422605226062260722608226092261022611226122261322614226152261622617226182261922620226212262222623226242262522626226272262822629226302263122632226332263422635226362263722638226392264022641226422264322644226452264622647226482264922650226512265222653226542265522656226572265822659226602266122662226632266422665226662266722668226692267022671226722267322674226752267622677226782267922680226812268222683226842268522686226872268822689226902269122692226932269422695226962269722698226992270022701227022270322704227052270622707227082270922710227112271222713227142271522716227172271822719227202272122722227232272422725227262272722728227292273022731227322273322734227352273622737227382273922740227412274222743227442274522746227472274822749227502275122752227532275422755227562275722758227592276022761227622276322764227652276622767227682276922770227712277222773227742277522776227772277822779227802278122782227832278422785227862278722788227892279022791227922279322794227952279622797227982279922800228012280222803228042280522806228072280822809228102281122812228132281422815228162281722818228192282022821228222282322824228252282622827228282282922830228312283222833228342283522836228372283822839228402284122842228432284422845228462284722848228492285022851228522285322854228552285622857228582285922860228612286222863228642286522866228672286822869228702287122872228732287422875228762287722878228792288022881228822288322884228852288622887228882288922890228912289222893228942289522896228972289822899229002290122902229032290422905229062290722908229092291022911229122291322914229152291622917229182291922920229212292222923229242292522926229272292822929229302293122932229332293422935229362293722938229392294022941229422294322944229452294622947229482294922950229512295222953229542295522956229572295822959229602296122962229632296422965229662296722968229692297022971229722297322974229752297622977229782297922980229812298222983229842298522986229872298822989229902299122992229932299422995229962299722998229992300023001230022300323004230052300623007230082300923010230112301223013230142301523016230172301823019230202302123022230232302423025230262302723028230292303023031230322303323034230352303623037230382303923040230412304223043230442304523046230472304823049230502305123052230532305423055230562305723058230592306023061230622306323064230652306623067230682306923070230712307223073230742307523076230772307823079230802308123082230832308423085230862308723088230892309023091230922309323094230952309623097230982309923100231012310223103231042310523106231072310823109231102311123112231132311423115231162311723118231192312023121231222312323124231252312623127231282312923130231312313223133231342313523136231372313823139231402314123142231432314423145231462314723148231492315023151231522315323154231552315623157231582315923160231612316223163231642316523166231672316823169231702317123172231732317423175231762317723178231792318023181231822318323184231852318623187231882318923190231912319223193231942319523196231972319823199232002320123202232032320423205232062320723208232092321023211232122321323214232152321623217232182321923220232212322223223232242322523226232272322823229232302323123232232332323423235232362323723238232392324023241232422324323244232452324623247232482324923250232512325223253232542325523256232572325823259232602326123262232632326423265232662326723268232692327023271232722327323274232752327623277232782327923280232812328223283232842328523286232872328823289232902329123292232932329423295232962329723298232992330023301233022330323304233052330623307233082330923310233112331223313233142331523316233172331823319233202332123322233232332423325233262332723328233292333023331233322333323334233352333623337233382333923340233412334223343233442334523346233472334823349233502335123352233532335423355233562335723358233592336023361233622336323364233652336623367233682336923370233712337223373233742337523376233772337823379233802338123382233832338423385233862338723388233892339023391233922339323394233952339623397233982339923400234012340223403234042340523406234072340823409234102341123412234132341423415234162341723418234192342023421234222342323424234252342623427234282342923430234312343223433234342343523436234372343823439234402344123442234432344423445234462344723448234492345023451234522345323454234552345623457234582345923460234612346223463234642346523466234672346823469234702347123472234732347423475234762347723478234792348023481234822348323484234852348623487234882348923490234912349223493234942349523496234972349823499235002350123502235032350423505235062350723508235092351023511235122351323514235152351623517235182351923520235212352223523235242352523526235272352823529235302353123532235332353423535235362353723538235392354023541235422354323544235452354623547235482354923550235512355223553235542355523556235572355823559235602356123562235632356423565235662356723568235692357023571235722357323574235752357623577235782357923580235812358223583235842358523586235872358823589235902359123592235932359423595235962359723598235992360023601236022360323604236052360623607236082360923610236112361223613236142361523616236172361823619236202362123622236232362423625236262362723628236292363023631236322363323634236352363623637236382363923640236412364223643236442364523646236472364823649236502365123652236532365423655236562365723658236592366023661236622366323664236652366623667236682366923670236712367223673236742367523676236772367823679236802368123682236832368423685236862368723688236892369023691236922369323694236952369623697236982369923700237012370223703237042370523706237072370823709237102371123712237132371423715237162371723718237192372023721237222372323724237252372623727237282372923730237312373223733237342373523736237372373823739237402374123742237432374423745237462374723748237492375023751237522375323754237552375623757237582375923760237612376223763237642376523766237672376823769237702377123772237732377423775237762377723778237792378023781237822378323784237852378623787237882378923790237912379223793237942379523796237972379823799238002380123802238032380423805238062380723808238092381023811238122381323814238152381623817238182381923820238212382223823238242382523826238272382823829238302383123832238332383423835238362383723838238392384023841238422384323844238452384623847238482384923850238512385223853238542385523856238572385823859238602386123862238632386423865238662386723868238692387023871238722387323874238752387623877238782387923880238812388223883238842388523886238872388823889238902389123892238932389423895238962389723898238992390023901239022390323904239052390623907239082390923910239112391223913239142391523916239172391823919239202392123922239232392423925239262392723928239292393023931239322393323934239352393623937239382393923940239412394223943239442394523946239472394823949239502395123952239532395423955239562395723958239592396023961239622396323964239652396623967239682396923970239712397223973239742397523976239772397823979239802398123982239832398423985239862398723988239892399023991239922399323994239952399623997239982399924000240012400224003240042400524006240072400824009240102401124012240132401424015240162401724018240192402024021240222402324024240252402624027240282402924030240312403224033240342403524036240372403824039240402404124042240432404424045240462404724048240492405024051240522405324054240552405624057240582405924060240612406224063240642406524066240672406824069240702407124072240732407424075240762407724078240792408024081240822408324084240852408624087240882408924090240912409224093240942409524096240972409824099241002410124102241032410424105241062410724108241092411024111241122411324114241152411624117241182411924120241212412224123241242412524126241272412824129241302413124132241332413424135241362413724138241392414024141241422414324144241452414624147241482414924150241512415224153241542415524156241572415824159241602416124162241632416424165241662416724168241692417024171241722417324174241752417624177241782417924180241812418224183241842418524186241872418824189241902419124192241932419424195241962419724198241992420024201242022420324204242052420624207242082420924210242112421224213242142421524216242172421824219242202422124222242232422424225242262422724228242292423024231242322423324234242352423624237242382423924240242412424224243242442424524246242472424824249242502425124252242532425424255242562425724258242592426024261242622426324264242652426624267242682426924270242712427224273242742427524276242772427824279242802428124282242832428424285242862428724288242892429024291242922429324294242952429624297242982429924300243012430224303243042430524306243072430824309243102431124312243132431424315243162431724318243192432024321243222432324324243252432624327243282432924330243312433224333243342433524336243372433824339243402434124342243432434424345243462434724348243492435024351243522435324354243552435624357243582435924360243612436224363243642436524366243672436824369243702437124372243732437424375243762437724378243792438024381243822438324384243852438624387243882438924390243912439224393243942439524396243972439824399244002440124402244032440424405244062440724408244092441024411244122441324414244152441624417244182441924420244212442224423244242442524426244272442824429244302443124432244332443424435244362443724438244392444024441244422444324444244452444624447244482444924450244512445224453244542445524456244572445824459244602446124462244632446424465244662446724468244692447024471244722447324474244752447624477244782447924480244812448224483244842448524486244872448824489244902449124492244932449424495244962449724498244992450024501245022450324504245052450624507245082450924510245112451224513245142451524516245172451824519245202452124522245232452424525245262452724528245292453024531245322453324534245352453624537245382453924540245412454224543245442454524546245472454824549245502455124552245532455424555245562455724558245592456024561245622456324564245652456624567245682456924570245712457224573245742457524576245772457824579245802458124582245832458424585245862458724588245892459024591245922459324594245952459624597245982459924600246012460224603246042460524606246072460824609246102461124612246132461424615246162461724618246192462024621246222462324624246252462624627246282462924630246312463224633246342463524636246372463824639246402464124642246432464424645246462464724648246492465024651246522465324654246552465624657246582465924660246612466224663246642466524666246672466824669246702467124672246732467424675246762467724678246792468024681246822468324684246852468624687246882468924690246912469224693246942469524696246972469824699247002470124702247032470424705247062470724708247092471024711247122471324714247152471624717247182471924720247212472224723247242472524726247272472824729247302473124732247332473424735247362473724738247392474024741247422474324744247452474624747247482474924750247512475224753247542475524756247572475824759247602476124762247632476424765247662476724768247692477024771247722477324774247752477624777247782477924780247812478224783247842478524786247872478824789247902479124792247932479424795247962479724798247992480024801248022480324804248052480624807248082480924810248112481224813248142481524816248172481824819248202482124822248232482424825248262482724828248292483024831248322483324834248352483624837248382483924840248412484224843248442484524846248472484824849248502485124852248532485424855248562485724858248592486024861248622486324864248652486624867248682486924870248712487224873248742487524876248772487824879248802488124882248832488424885248862488724888248892489024891248922489324894248952489624897248982489924900249012490224903249042490524906249072490824909249102491124912249132491424915249162491724918249192492024921249222492324924249252492624927249282492924930249312493224933249342493524936249372493824939249402494124942249432494424945249462494724948249492495024951249522495324954249552495624957249582495924960249612496224963249642496524966249672496824969249702497124972249732497424975249762497724978249792498024981249822498324984249852498624987249882498924990249912499224993249942499524996249972499824999250002500125002250032500425005250062500725008250092501025011250122501325014250152501625017250182501925020250212502225023250242502525026250272502825029250302503125032250332503425035250362503725038250392504025041250422504325044250452504625047250482504925050250512505225053250542505525056250572505825059250602506125062250632506425065250662506725068250692507025071250722507325074250752507625077250782507925080250812508225083250842508525086250872508825089250902509125092250932509425095250962509725098250992510025101251022510325104251052510625107251082510925110251112511225113251142511525116251172511825119251202512125122251232512425125251262512725128251292513025131251322513325134251352513625137251382513925140251412514225143251442514525146251472514825149251502515125152251532515425155251562515725158251592516025161251622516325164251652516625167251682516925170251712517225173251742517525176251772517825179251802518125182251832518425185251862518725188251892519025191251922519325194251952519625197251982519925200252012520225203252042520525206252072520825209252102521125212252132521425215252162521725218252192522025221252222522325224252252522625227252282522925230252312523225233252342523525236252372523825239252402524125242252432524425245252462524725248252492525025251252522525325254252552525625257252582525925260252612526225263252642526525266252672526825269252702527125272252732527425275252762527725278252792528025281252822528325284252852528625287252882528925290252912529225293252942529525296252972529825299253002530125302253032530425305253062530725308253092531025311253122531325314253152531625317253182531925320253212532225323253242532525326253272532825329253302533125332253332533425335253362533725338253392534025341253422534325344253452534625347253482534925350253512535225353253542535525356253572535825359253602536125362
  1. ;;; org.el --- Outline-based notes management and organizer -*- lexical-binding: t; -*-
  2. ;; Carstens outline-mode for keeping track of everything.
  3. ;; Copyright (C) 2004-2016 Free Software Foundation, Inc.
  4. ;;
  5. ;; Author: Carsten Dominik <carsten at orgmode dot org>
  6. ;; Maintainer: Carsten Dominik <carsten at orgmode dot org>
  7. ;; Keywords: outlines, hypermedia, calendar, wp
  8. ;; Homepage: http://orgmode.org
  9. ;;
  10. ;; This file is part of GNU Emacs.
  11. ;;
  12. ;; GNU Emacs is free software: you can redistribute it and/or modify
  13. ;; it under the terms of the GNU General Public License as published by
  14. ;; the Free Software Foundation, either version 3 of the License, or
  15. ;; (at your option) any later version.
  16. ;; GNU Emacs is distributed in the hope that it will be useful,
  17. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. ;; GNU General Public License for more details.
  20. ;; You should have received a copy of the GNU General Public License
  21. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  22. ;;
  23. ;;; Commentary:
  24. ;;
  25. ;; Org-mode is a mode for keeping notes, maintaining ToDo lists, and doing
  26. ;; project planning with a fast and effective plain-text system.
  27. ;;
  28. ;; Org-mode develops organizational tasks around NOTES files that contain
  29. ;; information about projects as plain text. Org-mode is implemented on
  30. ;; top of outline-mode, which makes it possible to keep the content of
  31. ;; large files well structured. Visibility cycling and structure editing
  32. ;; help to work with the tree. Tables are easily created with a built-in
  33. ;; table editor. Org-mode supports ToDo items, deadlines, time stamps,
  34. ;; and scheduling. It dynamically compiles entries into an agenda that
  35. ;; utilizes and smoothly integrates much of the Emacs calendar and diary.
  36. ;; Plain text URL-like links connect to websites, emails, Usenet
  37. ;; messages, BBDB entries, and any files related to the projects. For
  38. ;; printing and sharing of notes, an Org-mode file can be exported as a
  39. ;; structured ASCII file, as HTML, or (todo and agenda items only) as an
  40. ;; iCalendar file. It can also serve as a publishing tool for a set of
  41. ;; linked webpages.
  42. ;;
  43. ;; Installation and Activation
  44. ;; ---------------------------
  45. ;; See the corresponding sections in the manual at
  46. ;;
  47. ;; http://orgmode.org/org.html#Installation
  48. ;;
  49. ;; Documentation
  50. ;; -------------
  51. ;; The documentation of Org-mode can be found in the TeXInfo file. The
  52. ;; distribution also contains a PDF version of it. At the homepage of
  53. ;; Org-mode, you can read the same text online as HTML. There is also an
  54. ;; excellent reference card made by Philip Rooke. This card can be found
  55. ;; in the etc/ directory of Emacs 22.
  56. ;;
  57. ;; A list of recent changes can be found at
  58. ;; http://orgmode.org/Changes.html
  59. ;;
  60. ;;; Code:
  61. (defvar org-inhibit-highlight-removal nil) ; dynamically scoped param
  62. (defvar-local org-table-formula-constants-local nil
  63. "Local version of `org-table-formula-constants'.")
  64. ;;;; Require other packages
  65. (require 'cl-lib)
  66. (eval-when-compile (require 'gnus-sum))
  67. (require 'calendar)
  68. (require 'find-func)
  69. (require 'format-spec)
  70. (or (equal this-command 'eval-buffer)
  71. (condition-case nil
  72. (load (concat (file-name-directory load-file-name)
  73. "org-loaddefs.el")
  74. nil t t t)
  75. (error
  76. (message "WARNING: No org-loaddefs.el file could be found from where org.el is loaded.")
  77. (sit-for 3)
  78. (message "You need to run \"make\" or \"make autoloads\" from Org lisp directory")
  79. (sit-for 3))))
  80. (require 'org-macs)
  81. (require 'org-compat)
  82. ;; `org-outline-regexp' ought to be a defconst but is let-bound in
  83. ;; some places -- e.g. see the macro `org-with-limited-levels'.
  84. ;;
  85. ;; In Org buffers, the value of `outline-regexp' is that of
  86. ;; `org-outline-regexp'. The only function still directly relying on
  87. ;; `outline-regexp' is `org-overview' so that `org-cycle' can do its
  88. ;; job when `orgstruct-mode' is active.
  89. (defvar org-outline-regexp "\\*+ "
  90. "Regexp to match Org headlines.")
  91. (defvar org-outline-regexp-bol "^\\*+ "
  92. "Regexp to match Org headlines.
  93. This is similar to `org-outline-regexp' but additionally makes
  94. sure that we are at the beginning of the line.")
  95. (defvar org-heading-regexp "^\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  96. "Matches a headline, putting stars and text into groups.
  97. Stars are put in group 1 and the trimmed body in group 2.")
  98. (declare-function calendar-check-holidays "holidays" (date))
  99. (declare-function cdlatex-environment "ext:cdlatex" (environment item))
  100. (declare-function isearch-no-upper-case-p "isearch" (string regexp-flag))
  101. (declare-function org-add-archive-files "org-archive" (files))
  102. (declare-function org-agenda-entry-get-agenda-timestamp "org-agenda" (pom))
  103. (declare-function org-agenda-list "org-agenda"
  104. (&optional arg start-day span with-hour))
  105. (declare-function org-agenda-redo "org-agenda" (&optional all))
  106. (declare-function org-babel-do-in-edit-buffer "ob-core" (&rest body) t)
  107. (declare-function org-babel-tangle-file "ob-tangle" (file &optional target-file lang))
  108. (declare-function org-beamer-mode "ox-beamer" (&optional prefix) t)
  109. (declare-function org-clock-get-last-clock-out-time "org-clock" ())
  110. (declare-function org-clock-out "org-clock" (&optional switch-to-state fail-quietly at-time))
  111. (declare-function org-clock-remove-overlays "org-clock" (&optional beg end noremove))
  112. (declare-function org-clock-sum "org-clock" (&optional tstart tend headline-filter propname))
  113. (declare-function org-clock-sum-current-item "org-clock" (&optional tstart))
  114. (declare-function org-clock-timestamps-down "org-clock" (&optional n))
  115. (declare-function org-clock-timestamps-up "org-clock" (&optional n))
  116. (declare-function org-clock-update-time-maybe "org-clock" ())
  117. (declare-function org-clocktable-shift "org-clock" (dir n))
  118. (declare-function org-element-at-point "org-element" ())
  119. (declare-function org-element-cache-refresh "org-element" (pos))
  120. (declare-function org-element-cache-reset "org-element" (&optional all))
  121. (declare-function org-element-contents "org-element" (element))
  122. (declare-function org-element-context "org-element" (&optional element))
  123. (declare-function org-element-copy "org-element" (datum))
  124. (declare-function org-element-interpret-data "org-element" (data))
  125. (declare-function org-element-lineage "org-element" (blob &optional types with-self))
  126. (declare-function org-element-nested-p "org-element" (elem-a elem-b))
  127. (declare-function org-element-parse-buffer "org-element" (&optional granularity visible-only))
  128. (declare-function org-element-property "org-element" (property element))
  129. (declare-function org-element-put-property "org-element" (element property value))
  130. (declare-function org-element-swap-A-B "org-element" (elem-a elem-b))
  131. (declare-function org-element-type "org-element" (element))
  132. (declare-function org-element-update-syntax "org-element" ())
  133. (declare-function org-id-find-id-file "org-id" (id))
  134. (declare-function org-id-get-create "org-id" (&optional force))
  135. (declare-function org-inlinetask-at-task-p "org-inlinetask" ())
  136. (declare-function org-inlinetask-outline-regexp "org-inlinetask" ())
  137. (declare-function org-inlinetask-toggle-visibility "org-inlinetask" ())
  138. (declare-function org-plot/gnuplot "org-plot" (&optional params))
  139. (declare-function org-pop-to-buffer-same-window "org-compat" (&optional buffer-or-name norecord label))
  140. (declare-function org-table-align "org-table" ())
  141. (declare-function org-table-begin "org-table" (&optional table-type))
  142. (declare-function org-table-beginning-of-field "org-table" (&optional n))
  143. (declare-function org-table-blank-field "org-table" ())
  144. (declare-function org-table-calc-current-TBLFM "org-table" (&optional arg))
  145. (declare-function org-table-edit-field "org-table" (arg))
  146. (declare-function org-table-end "org-table" (&optional table-type))
  147. (declare-function org-table-end-of-field "org-table" (&optional n))
  148. (declare-function org-table-insert-row "org-table" (&optional arg))
  149. (declare-function org-table-justify-field-maybe "org-table" (&optional new))
  150. (declare-function org-table-maybe-eval-formula "org-table" ())
  151. (declare-function org-table-maybe-recalculate-line "org-table" ())
  152. (declare-function org-table-next-row "org-table" ())
  153. (declare-function org-table-paste-rectangle "org-table" ())
  154. (declare-function org-table-wrap-region "org-table" (arg))
  155. (declare-function org-tags-view "org-agenda" (&optional todo-only match))
  156. (declare-function orgtbl-ascii-plot "org-table" (&optional ask))
  157. (declare-function orgtbl-mode "org-table" (&optional arg))
  158. (defsubst org-uniquify (list)
  159. "Non-destructively remove duplicate elements from LIST."
  160. (let ((res (copy-sequence list))) (delete-dups res)))
  161. (defsubst org-get-at-bol (property)
  162. "Get text property PROPERTY at the beginning of line."
  163. (get-text-property (point-at-bol) property))
  164. (defsubst org-trim (s)
  165. "Remove whitespace at the beginning and the end of string S."
  166. (replace-regexp-in-string
  167. "\\`[ \t\n\r]+" ""
  168. (replace-regexp-in-string "[ \t\n\r]+\\'" "" s)))
  169. ;; load languages based on value of `org-babel-load-languages'
  170. (defvar org-babel-load-languages)
  171. ;;;###autoload
  172. (defun org-babel-do-load-languages (sym value)
  173. "Load the languages defined in `org-babel-load-languages'."
  174. (set-default sym value)
  175. (dolist (pair org-babel-load-languages)
  176. (let ((active (cdr pair)) (lang (symbol-name (car pair))))
  177. (if active
  178. (require (intern (concat "ob-" lang)))
  179. (funcall 'fmakunbound
  180. (intern (concat "org-babel-execute:" lang)))
  181. (funcall 'fmakunbound
  182. (intern (concat "org-babel-expand-body:" lang)))))))
  183. (declare-function org-babel-tangle-file "ob-tangle" (file &optional target-file lang))
  184. ;;;###autoload
  185. (defun org-babel-load-file (file &optional compile)
  186. "Load Emacs Lisp source code blocks in the Org-mode FILE.
  187. This function exports the source code using `org-babel-tangle'
  188. and then loads the resulting file using `load-file'. With prefix
  189. arg (noninteractively: 2nd arg) COMPILE the tangled Emacs Lisp
  190. file to byte-code before it is loaded."
  191. (interactive "fFile to load: \nP")
  192. (let* ((age (lambda (file)
  193. (float-time
  194. (time-subtract (current-time)
  195. (nth 5 (or (file-attributes (file-truename file))
  196. (file-attributes file)))))))
  197. (base-name (file-name-sans-extension file))
  198. (exported-file (concat base-name ".el")))
  199. ;; tangle if the org-mode file is newer than the elisp file
  200. (unless (and (file-exists-p exported-file)
  201. (> (funcall age file) (funcall age exported-file)))
  202. ;; Tangle-file traversal returns reversed list of tangled files
  203. ;; and we want to evaluate the first target.
  204. (setq exported-file
  205. (car (last (org-babel-tangle-file file exported-file "emacs-lisp")))))
  206. (message "%s %s"
  207. (if compile
  208. (progn (byte-compile-file exported-file 'load)
  209. "Compiled and loaded")
  210. (progn (load-file exported-file) "Loaded"))
  211. exported-file)))
  212. (defcustom org-babel-load-languages '((emacs-lisp . t))
  213. "Languages which can be evaluated in Org-mode buffers.
  214. This list can be used to load support for any of the languages
  215. below, note that each language will depend on a different set of
  216. system executables and/or Emacs modes. When a language is
  217. \"loaded\", then code blocks in that language can be evaluated
  218. with `org-babel-execute-src-block' bound by default to C-c
  219. C-c (note the `org-babel-no-eval-on-ctrl-c-ctrl-c' variable can
  220. be set to remove code block evaluation from the C-c C-c
  221. keybinding. By default only Emacs Lisp (which has no
  222. requirements) is loaded."
  223. :group 'org-babel
  224. :set 'org-babel-do-load-languages
  225. :version "24.1"
  226. :type '(alist :tag "Babel Languages"
  227. :key-type
  228. (choice
  229. (const :tag "Awk" awk)
  230. (const :tag "C" C)
  231. (const :tag "R" R)
  232. (const :tag "Asymptote" asymptote)
  233. (const :tag "Calc" calc)
  234. (const :tag "Clojure" clojure)
  235. (const :tag "CSS" css)
  236. (const :tag "Ditaa" ditaa)
  237. (const :tag "Dot" dot)
  238. (const :tag "Emacs Lisp" emacs-lisp)
  239. (const :tag "Forth" forth)
  240. (const :tag "Fortran" fortran)
  241. (const :tag "Gnuplot" gnuplot)
  242. (const :tag "Haskell" haskell)
  243. (const :tag "IO" io)
  244. (const :tag "J" J)
  245. (const :tag "Java" java)
  246. (const :tag "Javascript" js)
  247. (const :tag "LaTeX" latex)
  248. (const :tag "Ledger" ledger)
  249. (const :tag "Lilypond" lilypond)
  250. (const :tag "Lisp" lisp)
  251. (const :tag "Makefile" makefile)
  252. (const :tag "Maxima" maxima)
  253. (const :tag "Matlab" matlab)
  254. (const :tag "Mscgen" mscgen)
  255. (const :tag "Ocaml" ocaml)
  256. (const :tag "Octave" octave)
  257. (const :tag "Org" org)
  258. (const :tag "Perl" perl)
  259. (const :tag "Pico Lisp" picolisp)
  260. (const :tag "PlantUML" plantuml)
  261. (const :tag "Python" python)
  262. (const :tag "Ruby" ruby)
  263. (const :tag "Sass" sass)
  264. (const :tag "Scala" scala)
  265. (const :tag "Scheme" scheme)
  266. (const :tag "Screen" screen)
  267. (const :tag "Shell Script" shell)
  268. (const :tag "Shen" shen)
  269. (const :tag "Sql" sql)
  270. (const :tag "Sqlite" sqlite)
  271. (const :tag "Stan" stan)
  272. (const :tag "ebnf2ps" ebnf2ps))
  273. :value-type (boolean :tag "Activate" :value t)))
  274. ;;;; Customization variables
  275. (defcustom org-clone-delete-id nil
  276. "Remove ID property of clones of a subtree.
  277. When non-nil, clones of a subtree don't inherit the ID property.
  278. Otherwise they inherit the ID property with a new unique
  279. identifier."
  280. :type 'boolean
  281. :version "24.1"
  282. :group 'org-id)
  283. ;;; Version
  284. (org-check-version)
  285. ;;;###autoload
  286. (defun org-version (&optional here full message)
  287. "Show the org-mode version.
  288. Interactively, or when MESSAGE is non-nil, show it in echo area.
  289. With prefix argument, or when HERE is non-nil, insert it at point.
  290. In non-interactive uses, a reduced version string is output unless
  291. FULL is given."
  292. (interactive (list current-prefix-arg t (not current-prefix-arg)))
  293. (let ((org-dir (ignore-errors (org-find-library-dir "org")))
  294. (save-load-suffixes (when (boundp 'load-suffixes) load-suffixes))
  295. (load-suffixes (list ".el"))
  296. (org-install-dir
  297. (ignore-errors (org-find-library-dir "org-loaddefs"))))
  298. (unless (and (fboundp 'org-release) (fboundp 'org-git-version))
  299. (org-load-noerror-mustsuffix (concat org-dir "org-version")))
  300. (let* ((load-suffixes save-load-suffixes)
  301. (release (org-release))
  302. (git-version (org-git-version))
  303. (version (format "Org-mode version %s (%s @ %s)"
  304. release
  305. git-version
  306. (if org-install-dir
  307. (if (string= org-dir org-install-dir)
  308. org-install-dir
  309. (concat "mixed installation! "
  310. org-install-dir
  311. " and "
  312. org-dir))
  313. "org-loaddefs.el can not be found!")))
  314. (version1 (if full version release)))
  315. (when here (insert version1))
  316. (when message (message "%s" version1))
  317. version1)))
  318. (defconst org-version (org-version))
  319. ;;; Syntax Constants
  320. ;;;; Block
  321. (defconst org-block-regexp
  322. "^[ \t]*#\\+begin_?\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_?\\1[ \t]*$"
  323. "Regular expression for hiding blocks.")
  324. (defconst org-dblock-start-re
  325. "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  326. "Matches the start line of a dynamic block, with parameters.")
  327. (defconst org-dblock-end-re "^[ \t]*#\\+\\(?:END\\|end\\)\\([: \t\r\n]\\|$\\)"
  328. "Matches the end of a dynamic block.")
  329. ;;;; Clock and Planning
  330. (defconst org-clock-string "CLOCK:"
  331. "String used as prefix for timestamps clocking work hours on an item.")
  332. (defvar org-closed-string "CLOSED:"
  333. "String used as the prefix for timestamps logging closing a TODO entry.")
  334. (defvar org-deadline-string "DEADLINE:"
  335. "String to mark deadline entries.
  336. A deadline is this string, followed by a time stamp. Should be a word,
  337. terminated by a colon. You can insert a schedule keyword and
  338. a timestamp with \\[org-deadline].")
  339. (defvar org-scheduled-string "SCHEDULED:"
  340. "String to mark scheduled TODO entries.
  341. A schedule is this string, followed by a time stamp. Should be a word,
  342. terminated by a colon. You can insert a schedule keyword and
  343. a timestamp with \\[org-schedule].")
  344. (defconst org-ds-keyword-length
  345. (+ 2
  346. (apply #'max
  347. (mapcar #'length
  348. (list org-deadline-string org-scheduled-string
  349. org-clock-string org-closed-string))))
  350. "Maximum length of the DEADLINE and SCHEDULED keywords.")
  351. (defconst org-planning-line-re
  352. (concat "^[ \t]*"
  353. (regexp-opt
  354. (list org-closed-string org-deadline-string org-scheduled-string)
  355. t))
  356. "Matches a line with planning info.
  357. Matched keyword is in group 1.")
  358. (defconst org-clock-line-re
  359. (concat "^[ \t]*" org-clock-string)
  360. "Matches a line with clock info.")
  361. (defconst org-deadline-regexp (concat "\\<" org-deadline-string)
  362. "Matches the DEADLINE keyword.")
  363. (defconst org-deadline-time-regexp
  364. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  365. "Matches the DEADLINE keyword together with a time stamp.")
  366. (defconst org-deadline-time-hour-regexp
  367. (concat "\\<" org-deadline-string
  368. " *<\\([^>]+[0-9]\\{1,2\\}:[0-9]\\{2\\}[0-9-+:hdwmy \t.]*\\)>")
  369. "Matches the DEADLINE keyword together with a time-and-hour stamp.")
  370. (defconst org-deadline-line-regexp
  371. (concat "\\<\\(" org-deadline-string "\\).*")
  372. "Matches the DEADLINE keyword and the rest of the line.")
  373. (defconst org-scheduled-regexp (concat "\\<" org-scheduled-string)
  374. "Matches the SCHEDULED keyword.")
  375. (defconst org-scheduled-time-regexp
  376. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  377. "Matches the SCHEDULED keyword together with a time stamp.")
  378. (defconst org-scheduled-time-hour-regexp
  379. (concat "\\<" org-scheduled-string
  380. " *<\\([^>]+[0-9]\\{1,2\\}:[0-9]\\{2\\}[0-9-+:hdwmy \t.]*\\)>")
  381. "Matches the SCHEDULED keyword together with a time-and-hour stamp.")
  382. (defconst org-closed-time-regexp
  383. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  384. "Matches the CLOSED keyword together with a time stamp.")
  385. (defconst org-keyword-time-regexp
  386. (concat "\\<"
  387. (regexp-opt
  388. (list org-scheduled-string org-deadline-string org-closed-string
  389. org-clock-string)
  390. t)
  391. " *[[<]\\([^]>]+\\)[]>]")
  392. "Matches any of the 4 keywords, together with the time stamp.")
  393. (defconst org-keyword-time-not-clock-regexp
  394. (concat
  395. "\\<"
  396. (regexp-opt
  397. (list org-scheduled-string org-deadline-string org-closed-string) t)
  398. " *[[<]\\([^]>]+\\)[]>]")
  399. "Matches any of the 3 keywords, together with the time stamp.")
  400. (defconst org-maybe-keyword-time-regexp
  401. (concat "\\(\\<"
  402. (regexp-opt
  403. (list org-scheduled-string org-deadline-string org-closed-string
  404. org-clock-string)
  405. t)
  406. "\\)?"
  407. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?[]>]"
  408. "\\|"
  409. "<%%([^\r\n>]*>\\)")
  410. "Matches a timestamp, possibly preceded by a keyword.")
  411. (defconst org-all-time-keywords
  412. (mapcar (lambda (w) (substring w 0 -1))
  413. (list org-scheduled-string org-deadline-string
  414. org-clock-string org-closed-string))
  415. "List of time keywords.")
  416. ;;;; Drawer
  417. (defconst org-drawer-regexp "^[ \t]*:\\(\\(?:\\w\\|[-_]\\)+\\):[ \t]*$"
  418. "Matches first or last line of a hidden block.
  419. Group 1 contains drawer's name or \"END\".")
  420. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  421. "Regular expression matching the first line of a property drawer.")
  422. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  423. "Regular expression matching the last line of a property drawer.")
  424. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  425. "Regular expression matching the first line of a clock drawer.")
  426. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  427. "Regular expression matching the last line of a clock drawer.")
  428. (defconst org-property-drawer-re
  429. (concat "^[ \t]*:PROPERTIES:[ \t]*\n"
  430. "\\(?:[ \t]*:\\S-+:\\(?: .*\\)?[ \t]*\n\\)*?"
  431. "[ \t]*:END:[ \t]*$")
  432. "Matches an entire property drawer.")
  433. (defconst org-clock-drawer-re
  434. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*?\\("
  435. org-clock-drawer-end-re "\\)\n?")
  436. "Matches an entire clock drawer.")
  437. ;;;; Headline
  438. (defconst org-heading-keyword-regexp-format
  439. "^\\(\\*+\\)\\(?: +%s\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  440. "Printf format for a regexp matching a headline with some keyword.
  441. This regexp will match the headline of any node which has the
  442. exact keyword that is put into the format. The keyword isn't in
  443. any group by default, but the stars and the body are.")
  444. (defconst org-heading-keyword-maybe-regexp-format
  445. "^\\(\\*+\\)\\(?: +%s\\)?\\(?: +\\(.*?\\)\\)?[ \t]*$"
  446. "Printf format for a regexp matching a headline, possibly with some keyword.
  447. This regexp can match any headline with the specified keyword, or
  448. without a keyword. The keyword isn't in any group by default,
  449. but the stars and the body are.")
  450. (defconst org-archive-tag "ARCHIVE"
  451. "The tag that marks a subtree as archived.
  452. An archived subtree does not open during visibility cycling, and does
  453. not contribute to the agenda listings.")
  454. (defconst org-comment-string "COMMENT"
  455. "Entries starting with this keyword will never be exported.
  456. An entry can be toggled between COMMENT and normal with
  457. \\[org-toggle-comment].")
  458. ;;;; LaTeX Environments and Fragments
  459. (defconst org-latex-regexps
  460. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  461. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  462. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  463. ("$1" "\\([^$]\\|^\\)\\(\\$[^ \r\n,;.$]\\$\\)\\(\\s.\\|\\s-\\|\\s(\\|\\s)\\|\\s\"\\|\000\\|$\\)" 2 nil)
  464. ("$" "\\([^$]\\|^\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\(\\s.\\|\\s-\\|\\s(\\|\\s)\\|\\s\"\\|\000\\|$\\)" 2 nil)
  465. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  466. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
  467. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
  468. "Regular expressions for matching embedded LaTeX.")
  469. ;;;; Node Property
  470. (defconst org-effort-property "Effort"
  471. "The property that is being used to keep track of effort estimates.
  472. Effort estimates given in this property need to have the format H:MM.")
  473. ;;;; Table
  474. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  475. "Detect an org-type or table-type table.")
  476. (defconst org-table-line-regexp "^[ \t]*|"
  477. "Detect an org-type table line.")
  478. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  479. "Detect an org-type table line.")
  480. (defconst org-table-hline-regexp "^[ \t]*|-"
  481. "Detect an org-type table hline.")
  482. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  483. "Detect a table-type table hline.")
  484. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  485. "Detect the first line outside a table when searching from within it.
  486. This works for both table types.")
  487. (defconst org-TBLFM-regexp "^[ \t]*#\\+TBLFM: "
  488. "Detect a #+TBLFM line.")
  489. ;;;; Timestamp
  490. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^\r\n>]*?\\)>"
  491. "Regular expression for fast time stamp matching.")
  492. (defconst org-ts-regexp-inactive
  493. "\\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^\r\n>]*?\\)\\]"
  494. "Regular expression for fast inactive time stamp matching.")
  495. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?\\)[]>]"
  496. "Regular expression for fast time stamp matching.")
  497. (defconst org-ts-regexp0
  498. "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\)\\( +[^]+0-9>\r\n -]+\\)?\\( +\\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  499. "Regular expression matching time strings for analysis.
  500. This one does not require the space after the date, so it can be used
  501. on a string that terminates immediately after the date.")
  502. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]+0-9>\r\n -]*\\)\\( \\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  503. "Regular expression matching time strings for analysis.")
  504. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  505. "Regular expression matching time stamps, with groups.")
  506. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  507. "Regular expression matching time stamps (also [..]), with groups.")
  508. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  509. "Regular expression matching a time stamp range.")
  510. (defconst org-tr-regexp-both
  511. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  512. "Regular expression matching a time stamp range.")
  513. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  514. org-ts-regexp "\\)?")
  515. "Regular expression matching a time stamp or time stamp range.")
  516. (defconst org-tsr-regexp-both
  517. (concat org-ts-regexp-both "\\(--?-?"
  518. org-ts-regexp-both "\\)?")
  519. "Regular expression matching a time stamp or time stamp range.
  520. The time stamps may be either active or inactive.")
  521. (defconst org-repeat-re
  522. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*?\\([.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)"
  523. "Regular expression for specifying repeated events.
  524. After a match, group 1 contains the repeat expression.")
  525. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  526. "Formats for `format-time-string' which are used for time stamps.")
  527. ;;; The custom variables
  528. (defgroup org nil
  529. "Outline-based notes management and organizer."
  530. :tag "Org"
  531. :group 'outlines
  532. :group 'calendar)
  533. (defcustom org-mode-hook nil
  534. "Mode hook for Org-mode, run after the mode was turned on."
  535. :group 'org
  536. :type 'hook)
  537. (defcustom org-load-hook nil
  538. "Hook that is run after org.el has been loaded."
  539. :group 'org
  540. :type 'hook)
  541. (defcustom org-log-buffer-setup-hook nil
  542. "Hook that is run after an Org log buffer is created."
  543. :group 'org
  544. :version "24.1"
  545. :type 'hook)
  546. (defvar org-modules) ; defined below
  547. (defvar org-modules-loaded nil
  548. "Have the modules been loaded already?")
  549. (defun org-load-modules-maybe (&optional force)
  550. "Load all extensions listed in `org-modules'."
  551. (when (or force (not org-modules-loaded))
  552. (dolist (ext org-modules)
  553. (condition-case nil (require ext)
  554. (error (message "Problems while trying to load feature `%s'" ext))))
  555. (setq org-modules-loaded t)))
  556. (defun org-set-modules (var value)
  557. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  558. (set var value)
  559. (when (featurep 'org)
  560. (org-load-modules-maybe 'force)
  561. (org-element-cache-reset 'all)))
  562. (defcustom org-modules '(org-w3m org-bbdb org-bibtex org-docview org-gnus org-info org-irc org-mhe org-rmail)
  563. "Modules that should always be loaded together with org.el.
  564. If a description starts with <C>, the file is not part of Emacs
  565. and loading it will require that you have downloaded and properly
  566. installed the Org mode distribution.
  567. You can also use this system to load external packages (i.e. neither Org
  568. core modules, nor modules from the CONTRIB directory). Just add symbols
  569. to the end of the list. If the package is called org-xyz.el, then you need
  570. to add the symbol `xyz', and the package must have a call to:
  571. (provide \\='org-xyz)
  572. For export specific modules, see also `org-export-backends'."
  573. :group 'org
  574. :set 'org-set-modules
  575. :version "24.4"
  576. :package-version '(Org . "8.0")
  577. :type
  578. '(set :greedy t
  579. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  580. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  581. (const :tag " crypt: Encryption of subtrees" org-crypt)
  582. (const :tag " ctags: Access to Emacs tags with links" org-ctags)
  583. (const :tag " docview: Links to doc-view buffers" org-docview)
  584. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  585. (const :tag " habit: Track your consistency with habits" org-habit)
  586. (const :tag " id: Global IDs for identifying entries" org-id)
  587. (const :tag " info: Links to Info nodes" org-info)
  588. (const :tag " inlinetask: Tasks independent of outline hierarchy" org-inlinetask)
  589. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  590. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  591. (const :tag " mouse: Additional mouse support" org-mouse)
  592. (const :tag " protocol: Intercept calls from emacsclient" org-protocol)
  593. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  594. (const :tag " w3m: Special cut/paste from w3m to Org-mode." org-w3m)
  595. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  596. (const :tag "C bookmark: Org-mode links to bookmarks" org-bookmark)
  597. (const :tag "C bullets: Add overlays to headlines stars" org-bullets)
  598. (const :tag "C checklist: Extra functions for checklists in repeated tasks" org-checklist)
  599. (const :tag "C choose: Use TODO keywords to mark decisions states" org-choose)
  600. (const :tag "C collector: Collect properties into tables" org-collector)
  601. (const :tag "C depend: TODO dependencies for Org-mode\n\t\t\t(PARTIALLY OBSOLETE, see built-in dependency support))" org-depend)
  602. (const :tag "C drill: Flashcards and spaced repetition for Org-mode" org-drill)
  603. (const :tag "C elisp-symbol: Org-mode links to emacs-lisp symbols" org-elisp-symbol)
  604. (const :tag "C eshell Support for links to working directories in eshell" org-eshell)
  605. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  606. (const :tag "C eval: Include command output as text" org-eval)
  607. (const :tag "C eww: Store link to url of eww" org-eww)
  608. (const :tag "C expiry: Expiry mechanism for Org-mode entries" org-expiry)
  609. (const :tag "C favtable: Lookup table of favorite references and links" org-favtable)
  610. (const :tag "C git-link: Provide org links to specific file version" org-git-link)
  611. (const :tag "C interactive-query: Interactive modification of tags query\n\t\t\t(PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query)
  612. (const :tag "C invoice: Help manage client invoices in Org-mode" org-invoice)
  613. (const :tag "C learn: SuperMemo's incremental learning algorithm" org-learn)
  614. (const :tag "C mac-iCal Imports events from iCal.app to the Emacs diary" org-mac-iCal)
  615. (const :tag "C mac-link: Grab links and url from various mac Applications" org-mac-link)
  616. (const :tag "C mairix: Hook mairix search into Org-mode for different MUAs" org-mairix)
  617. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  618. (const :tag "C mew: Links to Mew folders/messages" org-mew)
  619. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  620. (const :tag "C notmuch: Provide org links to notmuch searches or messages" org-notmuch)
  621. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  622. (const :tag "C registry: A registry for Org-mode links" org-registry)
  623. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  624. (const :tag "C secretary: Team management with org-mode" org-secretary)
  625. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  626. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  627. (const :tag "C track: Keep up with Org-mode development" org-track)
  628. (const :tag "C velocity Something like Notational Velocity for Org" org-velocity)
  629. (const :tag "C vm: Links to VM folders/messages" org-vm)
  630. (const :tag "C wikinodes: CamelCase wiki-like links" org-wikinodes)
  631. (const :tag "C wl: Links to Wanderlust folders/messages" org-wl)
  632. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  633. (defvar org-export-registered-backends) ; From ox.el.
  634. (declare-function org-export-derived-backend-p "ox" (backend &rest backends))
  635. (declare-function org-export-backend-name "ox" (backend) t)
  636. (defcustom org-export-backends '(ascii html icalendar latex odt)
  637. "List of export back-ends that should be always available.
  638. If a description starts with <C>, the file is not part of Emacs
  639. and loading it will require that you have downloaded and properly
  640. installed the Org mode distribution.
  641. Unlike to `org-modules', libraries in this list will not be
  642. loaded along with Org, but only once the export framework is
  643. needed.
  644. This variable needs to be set before org.el is loaded. If you
  645. need to make a change while Emacs is running, use the customize
  646. interface or run the following code, where VAL stands for the new
  647. value of the variable, after updating it:
  648. (progn
  649. (setq org-export-registered-backends
  650. (cl-remove-if-not
  651. (lambda (backend)
  652. (let ((name (org-export-backend-name backend)))
  653. (or (memq name val)
  654. (catch \\='parentp
  655. (dolist (b val)
  656. (and (org-export-derived-backend-p b name)
  657. (throw \\='parentp t)))))))
  658. org-export-registered-backends))
  659. (let ((new-list (mapcar #\\='org-export-backend-name
  660. org-export-registered-backends)))
  661. (dolist (backend val)
  662. (cond
  663. ((not (load (format \"ox-%s\" backend) t t))
  664. (message \"Problems while trying to load export back-end \\=`%s\\='\"
  665. backend))
  666. ((not (memq backend new-list)) (push backend new-list))))
  667. (set-default \\='org-export-backends new-list)))
  668. Adding a back-end to this list will also pull the back-end it
  669. depends on, if any."
  670. :group 'org
  671. :group 'org-export
  672. :version "25.1"
  673. :package-version '(Org . "9.0")
  674. :initialize 'custom-initialize-set
  675. :set (lambda (var val)
  676. (if (not (featurep 'ox)) (set-default var val)
  677. ;; Any back-end not required anymore (not present in VAL and not
  678. ;; a parent of any back-end in the new value) is removed from the
  679. ;; list of registered back-ends.
  680. (setq org-export-registered-backends
  681. (cl-remove-if-not
  682. (lambda (backend)
  683. (let ((name (org-export-backend-name backend)))
  684. (or (memq name val)
  685. (catch 'parentp
  686. (dolist (b val)
  687. (and (org-export-derived-backend-p b name)
  688. (throw 'parentp t)))))))
  689. org-export-registered-backends))
  690. ;; Now build NEW-LIST of both new back-ends and required
  691. ;; parents.
  692. (let ((new-list (mapcar #'org-export-backend-name
  693. org-export-registered-backends)))
  694. (dolist (backend val)
  695. (cond
  696. ((not (load (format "ox-%s" backend) t t))
  697. (message "Problems while trying to load export back-end `%s'"
  698. backend))
  699. ((not (memq backend new-list)) (push backend new-list))))
  700. ;; Set VAR to that list with fixed dependencies.
  701. (set-default var new-list))))
  702. :type '(set :greedy t
  703. (const :tag " ascii Export buffer to ASCII format" ascii)
  704. (const :tag " beamer Export buffer to Beamer presentation" beamer)
  705. (const :tag " html Export buffer to HTML format" html)
  706. (const :tag " icalendar Export buffer to iCalendar format" icalendar)
  707. (const :tag " latex Export buffer to LaTeX format" latex)
  708. (const :tag " man Export buffer to MAN format" man)
  709. (const :tag " md Export buffer to Markdown format" md)
  710. (const :tag " odt Export buffer to ODT format" odt)
  711. (const :tag " org Export buffer to Org format" org)
  712. (const :tag " texinfo Export buffer to Texinfo format" texinfo)
  713. (const :tag "C confluence Export buffer to Confluence Wiki format" confluence)
  714. (const :tag "C deck Export buffer to deck.js presentations" deck)
  715. (const :tag "C freemind Export buffer to Freemind mindmap format" freemind)
  716. (const :tag "C groff Export buffer to Groff format" groff)
  717. (const :tag "C koma-letter Export buffer to KOMA Scrlttrl2 format" koma-letter)
  718. (const :tag "C RSS 2.0 Export buffer to RSS 2.0 format" rss)
  719. (const :tag "C s5 Export buffer to s5 presentations" s5)
  720. (const :tag "C taskjuggler Export buffer to TaskJuggler format" taskjuggler)))
  721. (eval-after-load 'ox
  722. '(dolist (backend org-export-backends)
  723. (condition-case nil (require (intern (format "ox-%s" backend)))
  724. (error (message "Problems while trying to load export back-end `%s'"
  725. backend)))))
  726. (defcustom org-support-shift-select nil
  727. "Non-nil means make shift-cursor commands select text when possible.
  728. \\<org-mode-map>\
  729. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys
  730. start selecting a region, or enlarge regions started in this way.
  731. In Org-mode, in special contexts, these same keys are used for
  732. other purposes, important enough to compete with shift selection.
  733. Org tries to balance these needs by supporting `shift-select-mode'
  734. outside these special contexts, under control of this variable.
  735. The default of this variable is nil, to avoid confusing behavior. Shifted
  736. cursor keys will then execute Org commands in the following contexts:
  737. - on a headline, changing TODO state (left/right) and priority (up/down)
  738. - on a time stamp, changing the time
  739. - in a plain list item, changing the bullet type
  740. - in a property definition line, switching between allowed values
  741. - in the BEGIN line of a clock table (changing the time block).
  742. Outside these contexts, the commands will throw an error.
  743. When this variable is t and the cursor is not in a special
  744. context, Org-mode will support shift-selection for making and
  745. enlarging regions. To make this more effective, the bullet
  746. cycling will no longer happen anywhere in an item line, but only
  747. if the cursor is exactly on the bullet.
  748. If you set this variable to the symbol `always', then the keys
  749. will not be special in headlines, property lines, and item lines,
  750. to make shift selection work there as well. If this is what you
  751. want, you can use the following alternative commands:
  752. `\\[org-todo]' and `\\[org-priority]' \
  753. to change TODO state and priority,
  754. `\\[universal-argument] \\[universal-argument] \\[org-todo]' \
  755. can be used to switch TODO sets,
  756. `\\[org-ctrl-c-minus]' to cycle item bullet types,
  757. and properties can be edited by hand or in column view.
  758. However, when the cursor is on a timestamp, shift-cursor commands
  759. will still edit the time stamp - this is just too good to give up.
  760. XEmacs user should have this variable set to nil, because
  761. `shift-select-mode' is in Emacs 23 or later only."
  762. :group 'org
  763. :type '(choice
  764. (const :tag "Never" nil)
  765. (const :tag "When outside special context" t)
  766. (const :tag "Everywhere except timestamps" always)))
  767. (defcustom org-loop-over-headlines-in-active-region nil
  768. "Shall some commands act upon headlines in the active region?
  769. When set to t, some commands will be performed in all headlines
  770. within the active region.
  771. When set to `start-level', some commands will be performed in all
  772. headlines within the active region, provided that these headlines
  773. are of the same level than the first one.
  774. When set to a string, those commands will be performed on the
  775. matching headlines within the active region. Such string must be
  776. a tags/property/todo match as it is used in the agenda tags view.
  777. The list of commands is: `org-schedule', `org-deadline',
  778. `org-todo', `org-archive-subtree', `org-archive-set-tag' and
  779. `org-archive-to-archive-sibling'. The archiving commands skip
  780. already archived entries."
  781. :type '(choice (const :tag "Don't loop" nil)
  782. (const :tag "All headlines in active region" t)
  783. (const :tag "In active region, headlines at the same level than the first one" start-level)
  784. (string :tag "Tags/Property/Todo matcher"))
  785. :version "24.1"
  786. :group 'org-todo
  787. :group 'org-archive)
  788. (defgroup org-startup nil
  789. "Options concerning startup of Org-mode."
  790. :tag "Org Startup"
  791. :group 'org)
  792. (defcustom org-startup-folded t
  793. "Non-nil means entering Org-mode will switch to OVERVIEW.
  794. This can also be configured on a per-file basis by adding one of
  795. the following lines anywhere in the buffer:
  796. #+STARTUP: fold (or `overview', this is equivalent)
  797. #+STARTUP: nofold (or `showall', this is equivalent)
  798. #+STARTUP: content
  799. #+STARTUP: showeverything
  800. By default, this option is ignored when Org opens agenda files
  801. for the first time. If you want the agenda to honor the startup
  802. option, set `org-agenda-inhibit-startup' to nil."
  803. :group 'org-startup
  804. :type '(choice
  805. (const :tag "nofold: show all" nil)
  806. (const :tag "fold: overview" t)
  807. (const :tag "content: all headlines" content)
  808. (const :tag "show everything, even drawers" showeverything)))
  809. (defcustom org-startup-truncated t
  810. "Non-nil means entering Org-mode will set `truncate-lines'.
  811. This is useful since some lines containing links can be very long and
  812. uninteresting. Also tables look terrible when wrapped."
  813. :group 'org-startup
  814. :type 'boolean)
  815. (defcustom org-startup-indented nil
  816. "Non-nil means turn on `org-indent-mode' on startup.
  817. This can also be configured on a per-file basis by adding one of
  818. the following lines anywhere in the buffer:
  819. #+STARTUP: indent
  820. #+STARTUP: noindent"
  821. :group 'org-structure
  822. :type '(choice
  823. (const :tag "Not" nil)
  824. (const :tag "Globally (slow on startup in large files)" t)))
  825. (defcustom org-use-sub-superscripts t
  826. "Non-nil means interpret \"_\" and \"^\" for display.
  827. If you want to control how Org exports those characters, see
  828. `org-export-with-sub-superscripts'. `org-use-sub-superscripts'
  829. used to be an alias for `org-export-with-sub-superscripts' in
  830. Org <8.0, it is not anymore.
  831. When this option is turned on, you can use TeX-like syntax for
  832. sub- and superscripts within the buffer. Several characters after
  833. \"_\" or \"^\" will be considered as a single item - so grouping
  834. with {} is normally not needed. For example, the following things
  835. will be parsed as single sub- or superscripts:
  836. 10^24 or 10^tau several digits will be considered 1 item.
  837. 10^-12 or 10^-tau a leading sign with digits or a word
  838. x^2-y^3 will be read as x^2 - y^3, because items are
  839. terminated by almost any nonword/nondigit char.
  840. x_{i^2} or x^(2-i) braces or parenthesis do grouping.
  841. Still, ambiguity is possible. So when in doubt, use {} to enclose
  842. the sub/superscript. If you set this variable to the symbol `{}',
  843. the braces are *required* in order to trigger interpretations as
  844. sub/superscript. This can be helpful in documents that need \"_\"
  845. frequently in plain text."
  846. :group 'org-startup
  847. :version "24.4"
  848. :package-version '(Org . "8.0")
  849. :type '(choice
  850. (const :tag "Always interpret" t)
  851. (const :tag "Only with braces" {})
  852. (const :tag "Never interpret" nil)))
  853. (defcustom org-startup-with-beamer-mode nil
  854. "Non-nil means turn on `org-beamer-mode' on startup.
  855. This can also be configured on a per-file basis by adding one of
  856. the following lines anywhere in the buffer:
  857. #+STARTUP: beamer"
  858. :group 'org-startup
  859. :version "24.1"
  860. :type 'boolean)
  861. (defcustom org-startup-align-all-tables nil
  862. "Non-nil means align all tables when visiting a file.
  863. This is useful when the column width in tables is forced with <N> cookies
  864. in table fields. Such tables will look correct only after the first re-align.
  865. This can also be configured on a per-file basis by adding one of
  866. the following lines anywhere in the buffer:
  867. #+STARTUP: align
  868. #+STARTUP: noalign"
  869. :group 'org-startup
  870. :type 'boolean)
  871. (defcustom org-startup-with-inline-images nil
  872. "Non-nil means show inline images when loading a new Org file.
  873. This can also be configured on a per-file basis by adding one of
  874. the following lines anywhere in the buffer:
  875. #+STARTUP: inlineimages
  876. #+STARTUP: noinlineimages"
  877. :group 'org-startup
  878. :version "24.1"
  879. :type 'boolean)
  880. (defcustom org-startup-with-latex-preview nil
  881. "Non-nil means preview LaTeX fragments when loading a new Org file.
  882. This can also be configured on a per-file basis by adding one of
  883. the following lines anywhere in the buffer:
  884. #+STARTUP: latexpreview
  885. #+STARTUP: nolatexpreview"
  886. :group 'org-startup
  887. :version "24.4"
  888. :package-version '(Org . "8.0")
  889. :type 'boolean)
  890. (defcustom org-insert-mode-line-in-empty-file nil
  891. "Non-nil means insert the first line setting Org-mode in empty files.
  892. When the function `org-mode' is called interactively in an empty file, this
  893. normally means that the file name does not automatically trigger Org-mode.
  894. To ensure that the file will always be in Org-mode in the future, a
  895. line enforcing Org-mode will be inserted into the buffer, if this option
  896. has been set."
  897. :group 'org-startup
  898. :type 'boolean)
  899. (defcustom org-replace-disputed-keys nil
  900. "Non-nil means use alternative key bindings for some keys.
  901. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  902. These keys are also used by other packages like shift-selection-mode'
  903. \(built into Emacs 23), `CUA-mode' or `windmove.el'.
  904. If you want to use Org-mode together with one of these other modes,
  905. or more generally if you would like to move some Org-mode commands to
  906. other keys, set this variable and configure the keys with the variable
  907. `org-disputed-keys'.
  908. This option is only relevant at load-time of Org-mode, and must be set
  909. *before* org.el is loaded. Changing it requires a restart of Emacs to
  910. become effective."
  911. :group 'org-startup
  912. :type 'boolean)
  913. (defcustom org-use-extra-keys nil
  914. "Non-nil means use extra key sequence definitions for certain commands.
  915. This happens automatically if you run XEmacs or if `window-system'
  916. is nil. This variable lets you do the same manually. You must
  917. set it before loading org.
  918. Example: on Carbon Emacs 22 running graphically, with an external
  919. keyboard on a Powerbook, the default way of setting M-left might
  920. not work for either Alt or ESC. Setting this variable will make
  921. it work for ESC."
  922. :group 'org-startup
  923. :type 'boolean)
  924. (org-defvaralias 'org-CUA-compatible 'org-replace-disputed-keys)
  925. (defcustom org-disputed-keys
  926. '(([(shift up)] . [(meta p)])
  927. ([(shift down)] . [(meta n)])
  928. ([(shift left)] . [(meta -)])
  929. ([(shift right)] . [(meta +)])
  930. ([(control shift right)] . [(meta shift +)])
  931. ([(control shift left)] . [(meta shift -)]))
  932. "Keys for which Org-mode and other modes compete.
  933. This is an alist, cars are the default keys, second element specifies
  934. the alternative to use when `org-replace-disputed-keys' is t.
  935. Keys can be specified in any syntax supported by `define-key'.
  936. The value of this option takes effect only at Org-mode's startup,
  937. therefore you'll have to restart Emacs to apply it after changing."
  938. :group 'org-startup
  939. :type 'alist)
  940. (defun org-key (key)
  941. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  942. Or return the original if not disputed.
  943. Also apply the translations defined in `org-xemacs-key-equivalents'."
  944. (when org-replace-disputed-keys
  945. (let* ((nkey (key-description key))
  946. (x (org-find-if (lambda (x)
  947. (equal (key-description (car x)) nkey))
  948. org-disputed-keys)))
  949. (setq key (if x (cdr x) key))))
  950. (when (featurep 'xemacs)
  951. (setq key (or (cdr (assoc key org-xemacs-key-equivalents)) key)))
  952. key)
  953. (defun org-find-if (predicate seq)
  954. (catch 'exit
  955. (while seq
  956. (if (funcall predicate (car seq))
  957. (throw 'exit (car seq))
  958. (pop seq)))))
  959. (defun org-defkey (keymap key def)
  960. "Define a key, possibly translated, as returned by `org-key'."
  961. (define-key keymap (org-key key) def))
  962. (defcustom org-ellipsis nil
  963. "The ellipsis to use in the Org-mode outline.
  964. When nil, just use the standard three dots.
  965. When a string, use that string instead.
  966. When a face, use the standard 3 dots, but with the specified face.
  967. The change affects only Org-mode (which will then use its own display table).
  968. Changing this requires executing \\[org-mode] in a buffer to become
  969. effective."
  970. :group 'org-startup
  971. :type '(choice (const :tag "Default" nil)
  972. (face :tag "Face" :value org-warning)
  973. (string :tag "String" :value "...#")))
  974. (defvar org-display-table nil
  975. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  976. (defgroup org-keywords nil
  977. "Keywords in Org-mode."
  978. :tag "Org Keywords"
  979. :group 'org)
  980. (defcustom org-closed-keep-when-no-todo nil
  981. "Remove CLOSED: time-stamp when switching back to a non-todo state?"
  982. :group 'org-todo
  983. :group 'org-keywords
  984. :version "24.4"
  985. :package-version '(Org . "8.0")
  986. :type 'boolean)
  987. (defgroup org-structure nil
  988. "Options concerning the general structure of Org-mode files."
  989. :tag "Org Structure"
  990. :group 'org)
  991. (defgroup org-reveal-location nil
  992. "Options about how to make context of a location visible."
  993. :tag "Org Reveal Location"
  994. :group 'org-structure)
  995. (defcustom org-show-context-detail '((agenda . local)
  996. (bookmark-jump . lineage)
  997. (isearch . lineage)
  998. (default . ancestors))
  999. "Alist between context and visibility span when revealing a location.
  1000. \\<org-mode-map>Some actions may move point into invisible
  1001. locations. As a consequence, Org always expose a neighborhood
  1002. around point. How much is shown depends on the initial action,
  1003. or context. Valid contexts are
  1004. agenda when exposing an entry from the agenda
  1005. org-goto when using the command `org-goto' (\\[org-goto])
  1006. occur-tree when using the command `org-occur' (\\[org-sparse-tree] /)
  1007. tags-tree when constructing a sparse tree based on tags matches
  1008. link-search when exposing search matches associated with a link
  1009. mark-goto when exposing the jump goal of a mark
  1010. bookmark-jump when exposing a bookmark location
  1011. isearch when exiting from an incremental search
  1012. default default for all contexts not set explicitly
  1013. Allowed visibility spans are
  1014. minimal show current headline; if point is not on headline,
  1015. also show entry
  1016. local show current headline, entry and next headline
  1017. ancestors show current headline and its direct ancestors; if
  1018. point is not on headline, also show entry
  1019. lineage show current headline, its direct ancestors and all
  1020. their children; if point is not on headline, also show
  1021. entry and first child
  1022. tree show current headline, its direct ancestors and all
  1023. their children; if point is not on headline, also show
  1024. entry and all children
  1025. canonical show current headline, its direct ancestors along with
  1026. their entries and children; if point is not located on
  1027. the headline, also show current entry and all children
  1028. As special cases, a nil or t value means show all contexts in
  1029. `minimal' or `canonical' view, respectively.
  1030. Some views can make displayed information very compact, but also
  1031. make it harder to edit the location of the match. In such
  1032. a case, use the command `org-reveal' (\\[org-reveal]) to show
  1033. more context."
  1034. :group 'org-reveal-location
  1035. :version "25.2"
  1036. :package-version '(Org . "9.0")
  1037. :type '(choice
  1038. (const :tag "Canonical" t)
  1039. (const :tag "Minimal" nil)
  1040. (repeat :greedy t :tag "Individual contexts"
  1041. (cons
  1042. (choice :tag "Context"
  1043. (const agenda)
  1044. (const org-goto)
  1045. (const occur-tree)
  1046. (const tags-tree)
  1047. (const link-search)
  1048. (const mark-goto)
  1049. (const bookmark-jump)
  1050. (const isearch)
  1051. (const default))
  1052. (choice :tag "Detail level"
  1053. (const minimal)
  1054. (const local)
  1055. (const ancestors)
  1056. (const lineage)
  1057. (const tree)
  1058. (const canonical))))))
  1059. (defcustom org-indirect-buffer-display 'other-window
  1060. "How should indirect tree buffers be displayed?
  1061. This applies to indirect buffers created with the commands
  1062. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  1063. Valid values are:
  1064. current-window Display in the current window
  1065. other-window Just display in another window.
  1066. dedicated-frame Create one new frame, and re-use it each time.
  1067. new-frame Make a new frame each time. Note that in this case
  1068. previously-made indirect buffers are kept, and you need to
  1069. kill these buffers yourself."
  1070. :group 'org-structure
  1071. :group 'org-agenda-windows
  1072. :type '(choice
  1073. (const :tag "In current window" current-window)
  1074. (const :tag "In current frame, other window" other-window)
  1075. (const :tag "Each time a new frame" new-frame)
  1076. (const :tag "One dedicated frame" dedicated-frame)))
  1077. (defcustom org-use-speed-commands nil
  1078. "Non-nil means activate single letter commands at beginning of a headline.
  1079. This may also be a function to test for appropriate locations where speed
  1080. commands should be active.
  1081. For example, to activate speed commands when the point is on any
  1082. star at the beginning of the headline, you can do this:
  1083. (setq org-use-speed-commands
  1084. (lambda () (and (looking-at org-outline-regexp) (looking-back \"^\\**\"))))"
  1085. :group 'org-structure
  1086. :type '(choice
  1087. (const :tag "Never" nil)
  1088. (const :tag "At beginning of headline stars" t)
  1089. (function)))
  1090. (defcustom org-speed-commands-user nil
  1091. "Alist of additional speed commands.
  1092. This list will be checked before `org-speed-commands-default'
  1093. when the variable `org-use-speed-commands' is non-nil
  1094. and when the cursor is at the beginning of a headline.
  1095. The car if each entry is a string with a single letter, which must
  1096. be assigned to `self-insert-command' in the global map.
  1097. The cdr is either a command to be called interactively, a function
  1098. to be called, or a form to be evaluated.
  1099. An entry that is just a list with a single string will be interpreted
  1100. as a descriptive headline that will be added when listing the speed
  1101. commands in the Help buffer using the `?' speed command."
  1102. :group 'org-structure
  1103. :type '(repeat :value ("k" . ignore)
  1104. (choice :value ("k" . ignore)
  1105. (list :tag "Descriptive Headline" (string :tag "Headline"))
  1106. (cons :tag "Letter and Command"
  1107. (string :tag "Command letter")
  1108. (choice
  1109. (function)
  1110. (sexp))))))
  1111. (defcustom org-bookmark-names-plist
  1112. '(:last-capture "org-capture-last-stored"
  1113. :last-refile "org-refile-last-stored"
  1114. :last-capture-marker "org-capture-last-stored-marker")
  1115. "Names for bookmarks automatically set by some Org commands.
  1116. This can provide strings as names for a number of bookmarks Org sets
  1117. automatically. The following keys are currently implemented:
  1118. :last-capture
  1119. :last-capture-marker
  1120. :last-refile
  1121. When a key does not show up in the property list, the corresponding bookmark
  1122. is not set."
  1123. :group 'org-structure
  1124. :type 'plist)
  1125. (defgroup org-cycle nil
  1126. "Options concerning visibility cycling in Org-mode."
  1127. :tag "Org Cycle"
  1128. :group 'org-structure)
  1129. (defcustom org-cycle-skip-children-state-if-no-children t
  1130. "Non-nil means skip CHILDREN state in entries that don't have any."
  1131. :group 'org-cycle
  1132. :type 'boolean)
  1133. (defcustom org-cycle-max-level nil
  1134. "Maximum level which should still be subject to visibility cycling.
  1135. Levels higher than this will, for cycling, be treated as text, not a headline.
  1136. When `org-odd-levels-only' is set, a value of N in this variable actually
  1137. means 2N-1 stars as the limiting headline.
  1138. When nil, cycle all levels.
  1139. Note that the limiting level of cycling is also influenced by
  1140. `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
  1141. `org-inlinetask-min-level' is, cycling will be limited to levels one less
  1142. than its value."
  1143. :group 'org-cycle
  1144. :type '(choice
  1145. (const :tag "No limit" nil)
  1146. (integer :tag "Maximum level")))
  1147. (defcustom org-hide-block-startup nil
  1148. "Non-nil means entering Org-mode will fold all blocks.
  1149. This can also be set in on a per-file basis with
  1150. #+STARTUP: hideblocks
  1151. #+STARTUP: showblocks"
  1152. :group 'org-startup
  1153. :group 'org-cycle
  1154. :type 'boolean)
  1155. (defcustom org-cycle-global-at-bob nil
  1156. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  1157. This makes it possible to do global cycling without having to use S-TAB or
  1158. \\[universal-argument] TAB. For this special case to work, the first line
  1159. of the buffer must not be a headline -- it may be empty or some other text.
  1160. When used in this way, `org-cycle-hook' is disabled temporarily to make
  1161. sure the cursor stays at the beginning of the buffer. When this option is
  1162. nil, don't do anything special at the beginning of the buffer."
  1163. :group 'org-cycle
  1164. :type 'boolean)
  1165. (defcustom org-cycle-level-after-item/entry-creation t
  1166. "Non-nil means cycle entry level or item indentation in new empty entries.
  1167. When the cursor is at the end of an empty headline, i.e., with only stars
  1168. and maybe a TODO keyword, TAB will then switch the entry to become a child,
  1169. and then all possible ancestor states, before returning to the original state.
  1170. This makes data entry extremely fast: M-RET to create a new headline,
  1171. on TAB to make it a child, two or more tabs to make it a (grand-)uncle.
  1172. When the cursor is at the end of an empty plain list item, one TAB will
  1173. make it a subitem, two or more tabs will back up to make this an item
  1174. higher up in the item hierarchy."
  1175. :group 'org-cycle
  1176. :type 'boolean)
  1177. (defcustom org-cycle-emulate-tab t
  1178. "Where should `org-cycle' emulate TAB.
  1179. nil Never
  1180. white Only in completely white lines
  1181. whitestart Only at the beginning of lines, before the first non-white char
  1182. t Everywhere except in headlines
  1183. exc-hl-bol Everywhere except at the start of a headline
  1184. If TAB is used in a place where it does not emulate TAB, the current subtree
  1185. visibility is cycled."
  1186. :group 'org-cycle
  1187. :type '(choice (const :tag "Never" nil)
  1188. (const :tag "Only in completely white lines" white)
  1189. (const :tag "Before first char in a line" whitestart)
  1190. (const :tag "Everywhere except in headlines" t)
  1191. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)))
  1192. (defcustom org-cycle-separator-lines 2
  1193. "Number of empty lines needed to keep an empty line between collapsed trees.
  1194. If you leave an empty line between the end of a subtree and the following
  1195. headline, this empty line is hidden when the subtree is folded.
  1196. Org-mode will leave (exactly) one empty line visible if the number of
  1197. empty lines is equal or larger to the number given in this variable.
  1198. So the default 2 means at least 2 empty lines after the end of a subtree
  1199. are needed to produce free space between a collapsed subtree and the
  1200. following headline.
  1201. If the number is negative, and the number of empty lines is at least -N,
  1202. all empty lines are shown.
  1203. Special case: when 0, never leave empty lines in collapsed view."
  1204. :group 'org-cycle
  1205. :type 'integer)
  1206. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  1207. (defcustom org-pre-cycle-hook nil
  1208. "Hook that is run before visibility cycling is happening.
  1209. The function(s) in this hook must accept a single argument which indicates
  1210. the new state that will be set right after running this hook. The
  1211. argument is a symbol. Before a global state change, it can have the values
  1212. `overview', `content', or `all'. Before a local state change, it can have
  1213. the values `folded', `children', or `subtree'."
  1214. :group 'org-cycle
  1215. :type 'hook)
  1216. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  1217. org-cycle-hide-drawers
  1218. org-cycle-show-empty-lines
  1219. org-optimize-window-after-visibility-change)
  1220. "Hook that is run after `org-cycle' has changed the buffer visibility.
  1221. The function(s) in this hook must accept a single argument which indicates
  1222. the new state that was set by the most recent `org-cycle' command. The
  1223. argument is a symbol. After a global state change, it can have the values
  1224. `overview', `contents', or `all'. After a local state change, it can have
  1225. the values `folded', `children', or `subtree'."
  1226. :group 'org-cycle
  1227. :type 'hook
  1228. :version "25.1"
  1229. :package-version '(Org . "8.3"))
  1230. (defgroup org-edit-structure nil
  1231. "Options concerning structure editing in Org-mode."
  1232. :tag "Org Edit Structure"
  1233. :group 'org-structure)
  1234. (defcustom org-odd-levels-only nil
  1235. "Non-nil means skip even levels and only use odd levels for the outline.
  1236. This has the effect that two stars are being added/taken away in
  1237. promotion/demotion commands. It also influences how levels are
  1238. handled by the exporters.
  1239. Changing it requires restart of `font-lock-mode' to become effective
  1240. for fontification also in regions already fontified.
  1241. You may also set this on a per-file basis by adding one of the following
  1242. lines to the buffer:
  1243. #+STARTUP: odd
  1244. #+STARTUP: oddeven"
  1245. :group 'org-edit-structure
  1246. :group 'org-appearance
  1247. :type 'boolean)
  1248. (defcustom org-adapt-indentation t
  1249. "Non-nil means adapt indentation to outline node level.
  1250. When this variable is set, Org assumes that you write outlines by
  1251. indenting text in each node to align with the headline (after the
  1252. stars). The following issues are influenced by this variable:
  1253. - The indentation is increased by one space in a demotion
  1254. command, and decreased by one in a promotion command. However,
  1255. in the latter case, if shifting some line in the entry body
  1256. would alter document structure (e.g., insert a new headline),
  1257. indentation is not changed at all.
  1258. - Property drawers and planning information is inserted indented
  1259. when this variable is set. When nil, they will not be indented.
  1260. - TAB indents a line relative to current level. The lines below
  1261. a headline will be indented when this variable is set.
  1262. Note that this is all about true indentation, by adding and
  1263. removing space characters. See also `org-indent.el' which does
  1264. level-dependent indentation in a virtual way, i.e. at display
  1265. time in Emacs."
  1266. :group 'org-edit-structure
  1267. :type 'boolean)
  1268. (defcustom org-special-ctrl-a/e nil
  1269. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  1270. When t, `C-a' will bring back the cursor to the beginning of the
  1271. headline text, i.e. after the stars and after a possible TODO
  1272. keyword. In an item, this will be the position after bullet and
  1273. check-box, if any. When the cursor is already at that position,
  1274. another `C-a' will bring it to the beginning of the line.
  1275. `C-e' will jump to the end of the headline, ignoring the presence
  1276. of tags in the headline. A second `C-e' will then jump to the
  1277. true end of the line, after any tags. This also means that, when
  1278. this variable is non-nil, `C-e' also will never jump beyond the
  1279. end of the heading of a folded section, i.e. not after the
  1280. ellipses.
  1281. When set to the symbol `reversed', the first `C-a' or `C-e' works
  1282. normally, going to the true line boundary first. Only a directly
  1283. following, identical keypress will bring the cursor to the
  1284. special positions.
  1285. This may also be a cons cell where the behavior for `C-a' and
  1286. `C-e' is set separately."
  1287. :group 'org-edit-structure
  1288. :type '(choice
  1289. (const :tag "off" nil)
  1290. (const :tag "on: after stars/bullet and before tags first" t)
  1291. (const :tag "reversed: true line boundary first" reversed)
  1292. (cons :tag "Set C-a and C-e separately"
  1293. (choice :tag "Special C-a"
  1294. (const :tag "off" nil)
  1295. (const :tag "on: after stars/bullet first" t)
  1296. (const :tag "reversed: before stars/bullet first" reversed))
  1297. (choice :tag "Special C-e"
  1298. (const :tag "off" nil)
  1299. (const :tag "on: before tags first" t)
  1300. (const :tag "reversed: after tags first" reversed)))))
  1301. (org-defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e)
  1302. (defcustom org-special-ctrl-k nil
  1303. "Non-nil means `C-k' will behave specially in headlines.
  1304. When nil, `C-k' will call the default `kill-line' command.
  1305. When t, the following will happen while the cursor is in the headline:
  1306. - When the cursor is at the beginning of a headline, kill the entire
  1307. line and possible the folded subtree below the line.
  1308. - When in the middle of the headline text, kill the headline up to the tags.
  1309. - When after the headline text, kill the tags."
  1310. :group 'org-edit-structure
  1311. :type 'boolean)
  1312. (defcustom org-ctrl-k-protect-subtree nil
  1313. "Non-nil means, do not delete a hidden subtree with C-k.
  1314. When set to the symbol `error', simply throw an error when C-k is
  1315. used to kill (part-of) a headline that has hidden text behind it.
  1316. Any other non-nil value will result in a query to the user, if it is
  1317. OK to kill that hidden subtree. When nil, kill without remorse."
  1318. :group 'org-edit-structure
  1319. :version "24.1"
  1320. :type '(choice
  1321. (const :tag "Do not protect hidden subtrees" nil)
  1322. (const :tag "Protect hidden subtrees with a security query" t)
  1323. (const :tag "Never kill a hidden subtree with C-k" error)))
  1324. (defcustom org-special-ctrl-o t
  1325. "Non-nil means, make `C-o' insert a row in tables."
  1326. :group 'org-edit-structure
  1327. :type 'boolean)
  1328. (defcustom org-catch-invisible-edits nil
  1329. "Check if in invisible region before inserting or deleting a character.
  1330. Valid values are:
  1331. nil Do not check, so just do invisible edits.
  1332. error Throw an error and do nothing.
  1333. show Make point visible, and do the requested edit.
  1334. show-and-error Make point visible, then throw an error and abort the edit.
  1335. smart Make point visible, and do insertion/deletion if it is
  1336. adjacent to visible text and the change feels predictable.
  1337. Never delete a previously invisible character or add in the
  1338. middle or right after an invisible region. Basically, this
  1339. allows insertion and backward-delete right before ellipses.
  1340. FIXME: maybe in this case we should not even show?"
  1341. :group 'org-edit-structure
  1342. :version "24.1"
  1343. :type '(choice
  1344. (const :tag "Do not check" nil)
  1345. (const :tag "Throw error when trying to edit" error)
  1346. (const :tag "Unhide, but do not do the edit" show-and-error)
  1347. (const :tag "Show invisible part and do the edit" show)
  1348. (const :tag "Be smart and do the right thing" smart)))
  1349. (defcustom org-yank-folded-subtrees t
  1350. "Non-nil means when yanking subtrees, fold them.
  1351. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  1352. it starts with a heading and all other headings in it are either children
  1353. or siblings, then fold all the subtrees. However, do this only if no
  1354. text after the yank would be swallowed into a folded tree by this action."
  1355. :group 'org-edit-structure
  1356. :type 'boolean)
  1357. (defcustom org-yank-adjusted-subtrees nil
  1358. "Non-nil means when yanking subtrees, adjust the level.
  1359. With this setting, `org-paste-subtree' is used to insert the subtree, see
  1360. this function for details."
  1361. :group 'org-edit-structure
  1362. :type 'boolean)
  1363. (defcustom org-M-RET-may-split-line '((default . t))
  1364. "Non-nil means M-RET will split the line at the cursor position.
  1365. When nil, it will go to the end of the line before making a
  1366. new line.
  1367. You may also set this option in a different way for different
  1368. contexts. Valid contexts are:
  1369. headline when creating a new headline
  1370. item when creating a new item
  1371. table in a table field
  1372. default the value to be used for all contexts not explicitly
  1373. customized"
  1374. :group 'org-structure
  1375. :group 'org-table
  1376. :type '(choice
  1377. (const :tag "Always" t)
  1378. (const :tag "Never" nil)
  1379. (repeat :greedy t :tag "Individual contexts"
  1380. (cons
  1381. (choice :tag "Context"
  1382. (const headline)
  1383. (const item)
  1384. (const table)
  1385. (const default))
  1386. (boolean)))))
  1387. (defcustom org-insert-heading-respect-content nil
  1388. "Non-nil means insert new headings after the current subtree.
  1389. When nil, the new heading is created directly after the current line.
  1390. The commands \\[org-insert-heading-respect-content] and \\[org-insert-todo-heading-respect-content] turn
  1391. this variable on for the duration of the command."
  1392. :group 'org-structure
  1393. :type 'boolean)
  1394. (defcustom org-blank-before-new-entry '((heading . auto)
  1395. (plain-list-item . auto))
  1396. "Should `org-insert-heading' leave a blank line before new heading/item?
  1397. The value is an alist, with `heading' and `plain-list-item' as CAR,
  1398. and a boolean flag as CDR. The cdr may also be the symbol `auto', in
  1399. which case Org will look at the surrounding headings/items and try to
  1400. make an intelligent decision whether to insert a blank line or not."
  1401. :group 'org-edit-structure
  1402. :type '(list
  1403. (cons (const heading)
  1404. (choice (const :tag "Never" nil)
  1405. (const :tag "Always" t)
  1406. (const :tag "Auto" auto)))
  1407. (cons (const plain-list-item)
  1408. (choice (const :tag "Never" nil)
  1409. (const :tag "Always" t)
  1410. (const :tag "Auto" auto)))))
  1411. (defcustom org-insert-heading-hook nil
  1412. "Hook being run after inserting a new heading."
  1413. :group 'org-edit-structure
  1414. :type 'hook)
  1415. (defcustom org-enable-fixed-width-editor t
  1416. "Non-nil means lines starting with \":\" are treated as fixed-width.
  1417. This currently only means they are never auto-wrapped.
  1418. When nil, such lines will be treated like ordinary lines."
  1419. :group 'org-edit-structure
  1420. :type 'boolean)
  1421. (defcustom org-goto-auto-isearch t
  1422. "Non-nil means typing characters in `org-goto' starts incremental search.
  1423. When nil, you can use these keybindings to navigate the buffer:
  1424. q Quit the org-goto interface
  1425. n Go to the next visible heading
  1426. p Go to the previous visible heading
  1427. f Go one heading forward on same level
  1428. b Go one heading backward on same level
  1429. u Go one heading up"
  1430. :group 'org-edit-structure
  1431. :type 'boolean)
  1432. (defgroup org-sparse-trees nil
  1433. "Options concerning sparse trees in Org-mode."
  1434. :tag "Org Sparse Trees"
  1435. :group 'org-structure)
  1436. (defcustom org-highlight-sparse-tree-matches t
  1437. "Non-nil means highlight all matches that define a sparse tree.
  1438. The highlights will automatically disappear the next time the buffer is
  1439. changed by an edit command."
  1440. :group 'org-sparse-trees
  1441. :type 'boolean)
  1442. (defcustom org-remove-highlights-with-change t
  1443. "Non-nil means any change to the buffer will remove temporary highlights.
  1444. \\<org-mode-map>\
  1445. Such highlights are created by `org-occur' and `org-clock-display'.
  1446. When nil, `\\[org-ctrl-c-ctrl-c]' needs to be used \
  1447. to get rid of the highlights.
  1448. The highlights created by `org-toggle-latex-fragment' always need
  1449. `\\[org-toggle-latex-fragment]' to be removed."
  1450. :group 'org-sparse-trees
  1451. :group 'org-time
  1452. :type 'boolean)
  1453. (defcustom org-occur-case-fold-search t
  1454. "Non-nil means `org-occur' should be case-insensitive.
  1455. If set to `smart' the search will be case-insensitive only if it
  1456. doesn't specify any upper case character."
  1457. :group 'org-sparse-trees
  1458. :version "25.1"
  1459. :type '(choice
  1460. (const :tag "Case-sensitive" nil)
  1461. (const :tag "Case-insensitive" t)
  1462. (const :tag "Case-insensitive for lower case searches only" 'smart)))
  1463. (defcustom org-occur-hook '(org-first-headline-recenter)
  1464. "Hook that is run after `org-occur' has constructed a sparse tree.
  1465. This can be used to recenter the window to show as much of the structure
  1466. as possible."
  1467. :group 'org-sparse-trees
  1468. :type 'hook)
  1469. (defgroup org-imenu-and-speedbar nil
  1470. "Options concerning imenu and speedbar in Org-mode."
  1471. :tag "Org Imenu and Speedbar"
  1472. :group 'org-structure)
  1473. (defcustom org-imenu-depth 2
  1474. "The maximum level for Imenu access to Org-mode headlines.
  1475. This also applied for speedbar access."
  1476. :group 'org-imenu-and-speedbar
  1477. :type 'integer)
  1478. (defgroup org-table nil
  1479. "Options concerning tables in Org-mode."
  1480. :tag "Org Table"
  1481. :group 'org)
  1482. (defcustom org-enable-table-editor 'optimized
  1483. "Non-nil means lines starting with \"|\" are handled by the table editor.
  1484. When nil, such lines will be treated like ordinary lines.
  1485. When equal to the symbol `optimized', the table editor will be optimized to
  1486. do the following:
  1487. - Automatic overwrite mode in front of whitespace in table fields.
  1488. This makes the structure of the table stay in tact as long as the edited
  1489. field does not exceed the column width.
  1490. - Minimize the number of realigns. Normally, the table is aligned each time
  1491. TAB or RET are pressed to move to another field. With optimization this
  1492. happens only if changes to a field might have changed the column width.
  1493. Optimization requires replacing the functions `self-insert-command',
  1494. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  1495. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  1496. very good at guessing when a re-align will be necessary, but you can always
  1497. force one with \\[org-ctrl-c-ctrl-c].
  1498. If you would like to use the optimized version in Org-mode, but the
  1499. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  1500. This variable can be used to turn on and off the table editor during a session,
  1501. but in order to toggle optimization, a restart is required.
  1502. See also the variable `org-table-auto-blank-field'."
  1503. :group 'org-table
  1504. :type '(choice
  1505. (const :tag "off" nil)
  1506. (const :tag "on" t)
  1507. (const :tag "on, optimized" optimized)))
  1508. (defcustom org-self-insert-cluster-for-undo (or (featurep 'xemacs)
  1509. (version<= emacs-version "24.1"))
  1510. "Non-nil means cluster self-insert commands for undo when possible.
  1511. If this is set, then, like in the Emacs command loop, 20 consecutive
  1512. characters will be undone together.
  1513. This is configurable, because there is some impact on typing performance."
  1514. :group 'org-table
  1515. :type 'boolean)
  1516. (defcustom org-table-tab-recognizes-table.el t
  1517. "Non-nil means TAB will automatically notice a table.el table.
  1518. When it sees such a table, it moves point into it and - if necessary -
  1519. calls `table-recognize-table'."
  1520. :group 'org-table-editing
  1521. :type 'boolean)
  1522. (defgroup org-link nil
  1523. "Options concerning links in Org-mode."
  1524. :tag "Org Link"
  1525. :group 'org)
  1526. (defvar-local org-link-abbrev-alist-local nil
  1527. "Buffer-local version of `org-link-abbrev-alist', which see.
  1528. The value of this is taken from the #+LINK lines.")
  1529. (defcustom org-link-abbrev-alist nil
  1530. "Alist of link abbreviations.
  1531. The car of each element is a string, to be replaced at the start of a link.
  1532. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  1533. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  1534. [[linkkey:tag][description]]
  1535. The `linkkey' must be a word word, starting with a letter, followed
  1536. by letters, numbers, `-' or `_'.
  1537. If REPLACE is a string, the tag will simply be appended to create the link.
  1538. If the string contains \"%s\", the tag will be inserted there. If the string
  1539. contains \"%h\", it will cause a url-encoded version of the tag to be inserted
  1540. at that point (see the function `url-hexify-string'). If the string contains
  1541. the specifier \"%(my-function)\", then the custom function `my-function' will
  1542. be invoked: this function takes the tag as its only argument and must return
  1543. a string.
  1544. REPLACE may also be a function that will be called with the tag as the
  1545. only argument to create the link, which should be returned as a string.
  1546. See the manual for examples."
  1547. :group 'org-link
  1548. :type '(repeat
  1549. (cons
  1550. (string :tag "Protocol")
  1551. (choice
  1552. (string :tag "Format")
  1553. (function)))))
  1554. (defcustom org-descriptive-links t
  1555. "Non-nil means Org will display descriptive links.
  1556. E.g. [[http://orgmode.org][Org website]] will be displayed as
  1557. \"Org Website\", hiding the link itself and just displaying its
  1558. description. When set to nil, Org will display the full links
  1559. literally.
  1560. You can interactively set the value of this variable by calling
  1561. `org-toggle-link-display' or from the menu Org>Hyperlinks menu."
  1562. :group 'org-link
  1563. :type 'boolean)
  1564. (defcustom org-link-file-path-type 'adaptive
  1565. "How the path name in file links should be stored.
  1566. Valid values are:
  1567. relative Relative to the current directory, i.e. the directory of the file
  1568. into which the link is being inserted.
  1569. absolute Absolute path, if possible with ~ for home directory.
  1570. noabbrev Absolute path, no abbreviation of home directory.
  1571. adaptive Use relative path for files in the current directory and sub-
  1572. directories of it. For other files, use an absolute path."
  1573. :group 'org-link
  1574. :type '(choice
  1575. (const relative)
  1576. (const absolute)
  1577. (const noabbrev)
  1578. (const adaptive)))
  1579. (defvaralias 'org-activate-links 'org-highlight-links)
  1580. (defcustom org-highlight-links '(bracket angle plain radio tag date footnote)
  1581. "Types of links that should be highlighted in Org-mode files.
  1582. This is a list of symbols, each one of them leading to the
  1583. highlighting of a certain link type.
  1584. You can still open links that are not highlighted.
  1585. In principle, it does not hurt to turn on highlighting for all
  1586. link types. There may be a small gain when turning off unused
  1587. link types. The types are:
  1588. bracket The recommended [[link][description]] or [[link]] links with hiding.
  1589. angle Links in angular brackets that may contain whitespace like
  1590. <bbdb:Carsten Dominik>.
  1591. plain Plain links in normal text, no whitespace, like http://google.com.
  1592. radio Text that is matched by a radio target, see manual for details.
  1593. tag Tag settings in a headline (link to tag search).
  1594. date Time stamps (link to calendar).
  1595. footnote Footnote labels.
  1596. If you set this variable during an Emacs session, use `org-mode-restart'
  1597. in the Org buffer so that the change takes effect."
  1598. :group 'org-link
  1599. :group 'org-appearance
  1600. :type '(set :greedy t
  1601. (const :tag "Double bracket links" bracket)
  1602. (const :tag "Angular bracket links" angle)
  1603. (const :tag "Plain text links" plain)
  1604. (const :tag "Radio target matches" radio)
  1605. (const :tag "Tags" tag)
  1606. (const :tag "Timestamps" date)
  1607. (const :tag "Footnotes" footnote)))
  1608. (defcustom org-make-link-description-function nil
  1609. "Function to use for generating link descriptions from links.
  1610. When nil, the link location will be used. This function must take
  1611. two parameters: the first one is the link, the second one is the
  1612. description generated by `org-insert-link'. The function should
  1613. return the description to use."
  1614. :group 'org-link
  1615. :type '(choice (const nil) (function)))
  1616. (defgroup org-link-store nil
  1617. "Options concerning storing links in Org-mode."
  1618. :tag "Org Store Link"
  1619. :group 'org-link)
  1620. (defcustom org-url-hexify-p t
  1621. "When non-nil, hexify URL when creating a link."
  1622. :type 'boolean
  1623. :version "24.3"
  1624. :group 'org-link-store)
  1625. (defcustom org-email-link-description-format "Email %c: %.30s"
  1626. "Format of the description part of a link to an email or usenet message.
  1627. The following %-escapes will be replaced by corresponding information:
  1628. %F full \"From\" field
  1629. %f name, taken from \"From\" field, address if no name
  1630. %T full \"To\" field
  1631. %t first name in \"To\" field, address if no name
  1632. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  1633. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  1634. %s subject
  1635. %d date
  1636. %m message-id.
  1637. You may use normal field width specification between the % and the letter.
  1638. This is for example useful to limit the length of the subject.
  1639. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  1640. :group 'org-link-store
  1641. :type 'string)
  1642. (defcustom org-from-is-user-regexp
  1643. (let (r1 r2)
  1644. (when (and user-mail-address (not (string= user-mail-address "")))
  1645. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  1646. (when (and user-full-name (not (string= user-full-name "")))
  1647. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  1648. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  1649. "Regexp matched against the \"From:\" header of an email or usenet message.
  1650. It should match if the message is from the user him/herself."
  1651. :group 'org-link-store
  1652. :type 'regexp)
  1653. (defcustom org-context-in-file-links t
  1654. "Non-nil means file links from `org-store-link' contain context.
  1655. A search string will be added to the file name with :: as separator and
  1656. used to find the context when the link is activated by the command
  1657. `org-open-at-point'. When this option is t, the entire active region
  1658. will be placed in the search string of the file link. If set to a
  1659. positive integer, only the first n lines of context will be stored.
  1660. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1661. negates this setting for the duration of the command."
  1662. :group 'org-link-store
  1663. :type '(choice boolean integer))
  1664. (defcustom org-keep-stored-link-after-insertion nil
  1665. "Non-nil means keep link in list for entire session.
  1666. The command `org-store-link' adds a link pointing to the current
  1667. location to an internal list. These links accumulate during a session.
  1668. The command `org-insert-link' can be used to insert links into any
  1669. Org-mode file (offering completion for all stored links). When this
  1670. option is nil, every link which has been inserted once using \\[org-insert-link]
  1671. will be removed from the list, to make completing the unused links
  1672. more efficient."
  1673. :group 'org-link-store
  1674. :type 'boolean)
  1675. (defgroup org-link-follow nil
  1676. "Options concerning following links in Org-mode."
  1677. :tag "Org Follow Link"
  1678. :group 'org-link)
  1679. (defcustom org-link-translation-function nil
  1680. "Function to translate links with different syntax to Org syntax.
  1681. This can be used to translate links created for example by the Planner
  1682. or emacs-wiki packages to Org syntax.
  1683. The function must accept two parameters, a TYPE containing the link
  1684. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  1685. which is everything after the link protocol. It should return a cons
  1686. with possibly modified values of type and path.
  1687. Org contains a function for this, so if you set this variable to
  1688. `org-translate-link-from-planner', you should be able follow many
  1689. links created by planner."
  1690. :group 'org-link-follow
  1691. :type '(choice (const nil) (function)))
  1692. (defcustom org-follow-link-hook nil
  1693. "Hook that is run after a link has been followed."
  1694. :group 'org-link-follow
  1695. :type 'hook)
  1696. (defcustom org-tab-follows-link nil
  1697. "Non-nil means on links TAB will follow the link.
  1698. Needs to be set before org.el is loaded.
  1699. This really should not be used, it does not make sense, and the
  1700. implementation is bad."
  1701. :group 'org-link-follow
  1702. :type 'boolean)
  1703. (defcustom org-return-follows-link nil
  1704. "Non-nil means on links RET will follow the link.
  1705. In tables, the special behavior of RET has precedence."
  1706. :group 'org-link-follow
  1707. :type 'boolean)
  1708. (defcustom org-mouse-1-follows-link
  1709. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1710. "Non-nil means mouse-1 on a link will follow the link.
  1711. A longer mouse click will still set point. Does not work on XEmacs.
  1712. Needs to be set before org.el is loaded."
  1713. :group 'org-link-follow
  1714. :version "24.4"
  1715. :package-version '(Org . "8.3")
  1716. :type '(choice
  1717. (const :tag "A double click follows the link" double)
  1718. (const :tag "Unconditionally follow the link with mouse-1" t)
  1719. (integer :tag "mouse-1 click does not follow the link if longer than N ms" 450)))
  1720. (defcustom org-mark-ring-length 4
  1721. "Number of different positions to be recorded in the ring.
  1722. Changing this requires a restart of Emacs to work correctly."
  1723. :group 'org-link-follow
  1724. :type 'integer)
  1725. (defcustom org-link-search-must-match-exact-headline 'query-to-create
  1726. "Non-nil means internal links in Org files must exactly match a headline.
  1727. When nil, the link search tries to match a phrase with all words
  1728. in the search text."
  1729. :group 'org-link-follow
  1730. :version "24.1"
  1731. :type '(choice
  1732. (const :tag "Use fuzzy text search" nil)
  1733. (const :tag "Match only exact headline" t)
  1734. (const :tag "Match exact headline or query to create it"
  1735. query-to-create)))
  1736. (defcustom org-link-frame-setup
  1737. '((vm . vm-visit-folder-other-frame)
  1738. (vm-imap . vm-visit-imap-folder-other-frame)
  1739. (gnus . org-gnus-no-new-news)
  1740. (file . find-file-other-window)
  1741. (wl . wl-other-frame))
  1742. "Setup the frame configuration for following links.
  1743. When following a link with Emacs, it may often be useful to display
  1744. this link in another window or frame. This variable can be used to
  1745. set this up for the different types of links.
  1746. For VM, use any of
  1747. `vm-visit-folder'
  1748. `vm-visit-folder-other-window'
  1749. `vm-visit-folder-other-frame'
  1750. For Gnus, use any of
  1751. `gnus'
  1752. `gnus-other-frame'
  1753. `org-gnus-no-new-news'
  1754. For FILE, use any of
  1755. `find-file'
  1756. `find-file-other-window'
  1757. `find-file-other-frame'
  1758. For Wanderlust use any of
  1759. `wl'
  1760. `wl-other-frame'
  1761. For the calendar, use the variable `calendar-setup'.
  1762. For BBDB, it is currently only possible to display the matches in
  1763. another window."
  1764. :group 'org-link-follow
  1765. :type '(list
  1766. (cons (const vm)
  1767. (choice
  1768. (const vm-visit-folder)
  1769. (const vm-visit-folder-other-window)
  1770. (const vm-visit-folder-other-frame)))
  1771. (cons (const vm-imap)
  1772. (choice
  1773. (const vm-visit-imap-folder)
  1774. (const vm-visit-imap-folder-other-window)
  1775. (const vm-visit-imap-folder-other-frame)))
  1776. (cons (const gnus)
  1777. (choice
  1778. (const gnus)
  1779. (const gnus-other-frame)
  1780. (const org-gnus-no-new-news)))
  1781. (cons (const file)
  1782. (choice
  1783. (const find-file)
  1784. (const find-file-other-window)
  1785. (const find-file-other-frame)))
  1786. (cons (const wl)
  1787. (choice
  1788. (const wl)
  1789. (const wl-other-frame)))))
  1790. (defcustom org-display-internal-link-with-indirect-buffer nil
  1791. "Non-nil means use indirect buffer to display infile links.
  1792. Activating internal links (from one location in a file to another location
  1793. in the same file) normally just jumps to the location. When the link is
  1794. activated with a \\[universal-argument] prefix (or with mouse-3), the link \
  1795. is displayed in
  1796. another window. When this option is set, the other window actually displays
  1797. an indirect buffer clone of the current buffer, to avoid any visibility
  1798. changes to the current buffer."
  1799. :group 'org-link-follow
  1800. :type 'boolean)
  1801. (defcustom org-open-non-existing-files nil
  1802. "Non-nil means `org-open-file' will open non-existing files.
  1803. When nil, an error will be generated.
  1804. This variable applies only to external applications because they
  1805. might choke on non-existing files. If the link is to a file that
  1806. will be opened in Emacs, the variable is ignored."
  1807. :group 'org-link-follow
  1808. :type 'boolean)
  1809. (defcustom org-open-directory-means-index-dot-org nil
  1810. "Non-nil means a link to a directory really means to index.org.
  1811. When nil, following a directory link will run dired or open a finder/explorer
  1812. window on that directory."
  1813. :group 'org-link-follow
  1814. :type 'boolean)
  1815. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1816. "Non-nil means ask for confirmation before executing shell links.
  1817. Shell links can be dangerous: just think about a link
  1818. [[shell:rm -rf ~/*][Google Search]]
  1819. This link would show up in your Org-mode document as \"Google Search\",
  1820. but really it would remove your entire home directory.
  1821. Therefore we advise against setting this variable to nil.
  1822. Just change it to `y-or-n-p' if you want to confirm with a
  1823. single keystroke rather than having to type \"yes\"."
  1824. :group 'org-link-follow
  1825. :type '(choice
  1826. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1827. (const :tag "with y-or-n (faster)" y-or-n-p)
  1828. (const :tag "no confirmation (dangerous)" nil)))
  1829. (put 'org-confirm-shell-link-function
  1830. 'safe-local-variable
  1831. (lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1832. (defcustom org-confirm-shell-link-not-regexp ""
  1833. "A regexp to skip confirmation for shell links."
  1834. :group 'org-link-follow
  1835. :version "24.1"
  1836. :type 'regexp)
  1837. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1838. "Non-nil means ask for confirmation before executing Emacs Lisp links.
  1839. Elisp links can be dangerous: just think about a link
  1840. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1841. This link would show up in your Org-mode document as \"Google Search\",
  1842. but really it would remove your entire home directory.
  1843. Therefore we advise against setting this variable to nil.
  1844. Just change it to `y-or-n-p' if you want to confirm with a
  1845. single keystroke rather than having to type \"yes\"."
  1846. :group 'org-link-follow
  1847. :type '(choice
  1848. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1849. (const :tag "with y-or-n (faster)" y-or-n-p)
  1850. (const :tag "no confirmation (dangerous)" nil)))
  1851. (put 'org-confirm-shell-link-function
  1852. 'safe-local-variable
  1853. (lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1854. (defcustom org-confirm-elisp-link-not-regexp ""
  1855. "A regexp to skip confirmation for Elisp links."
  1856. :group 'org-link-follow
  1857. :version "24.1"
  1858. :type 'regexp)
  1859. (defconst org-file-apps-defaults-gnu
  1860. '((remote . emacs)
  1861. (system . mailcap)
  1862. (t . mailcap))
  1863. "Default file applications on a UNIX or GNU/Linux system.
  1864. See `org-file-apps'.")
  1865. (defconst org-file-apps-defaults-macosx
  1866. '((remote . emacs)
  1867. (system . "open %s")
  1868. ("ps.gz" . "gv %s")
  1869. ("eps.gz" . "gv %s")
  1870. ("dvi" . "xdvi %s")
  1871. ("fig" . "xfig %s")
  1872. (t . "open %s"))
  1873. "Default file applications on a MacOS X system.
  1874. The system \"open\" is known as a default, but we use X11 applications
  1875. for some files for which the OS does not have a good default.
  1876. See `org-file-apps'.")
  1877. (defconst org-file-apps-defaults-windowsnt
  1878. (list '(remote . emacs)
  1879. (cons 'system (lambda (file _path)
  1880. (with-no-warnings (w32-shell-execute "open" file))))
  1881. (cons t (lambda (file _path)
  1882. (with-no-warnings (w32-shell-execute "open" file)))))
  1883. "Default file applications on a Windows NT system.
  1884. The system \"open\" is used for most files.
  1885. See `org-file-apps'.")
  1886. (defcustom org-file-apps
  1887. '((auto-mode . emacs)
  1888. ("\\.mm\\'" . default)
  1889. ("\\.x?html?\\'" . default)
  1890. ("\\.pdf\\'" . default))
  1891. "External applications for opening `file:path' items in a document.
  1892. \\<org-mode-map>\
  1893. Org mode uses system defaults for different file types, but
  1894. you can use this variable to set the application for a given file
  1895. extension. The entries in this list are cons cells where the car identifies
  1896. files and the cdr the corresponding command. Possible values for the
  1897. file identifier are
  1898. \"string\" A string as a file identifier can be interpreted in different
  1899. ways, depending on its contents:
  1900. - Alphanumeric characters only:
  1901. Match links with this file extension.
  1902. Example: (\"pdf\" . \"evince %s\")
  1903. to open PDFs with evince.
  1904. - Regular expression: Match links where the
  1905. filename matches the regexp. If you want to
  1906. use groups here, use shy groups.
  1907. Example: (\"\\.x?html\\\\='\" . \"firefox %s\")
  1908. \(\"\\(?:xhtml\\|html\\)\" . \"firefox %s\")
  1909. to open *.html and *.xhtml with firefox.
  1910. - Regular expression which contains (non-shy) groups:
  1911. Match links where the whole link, including \"::\", and
  1912. anything after that, matches the regexp.
  1913. In a custom command string, %1, %2, etc. are replaced with
  1914. the parts of the link that were matched by the groups.
  1915. For backwards compatibility, if a command string is given
  1916. that does not use any of the group matches, this case is
  1917. handled identically to the second one (i.e. match against
  1918. file name only).
  1919. In a custom function, you can access the group matches with
  1920. \(match-string n link).
  1921. Example: (\"\\.pdf::\\(\\d+\\)\\\\='\" . \"evince -p %1 %s\")
  1922. to open [[file:document.pdf::5]] with evince at page 5.
  1923. `directory' Matches a directory
  1924. `remote' Matches a remote file, accessible through tramp or efs.
  1925. Remote files most likely should be visited through Emacs
  1926. because external applications cannot handle such paths.
  1927. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1928. so all files Emacs knows how to handle. Using this with
  1929. command `emacs' will open most files in Emacs. Beware that this
  1930. will also open html files inside Emacs, unless you add
  1931. \(\"html\" . default) to the list as well.
  1932. `system' The system command to open files, like `open' on Windows
  1933. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1934. that will be selected if you call \\[org-open-at-point] with a double
  1935. \\[universal-argument] \\[universal-argument] prefix.
  1936. t Default for files not matched by any of the other options.
  1937. Possible values for the command are:
  1938. `emacs' The file will be visited by the current Emacs process.
  1939. `default' Use the default application for this file type, which is the
  1940. association for t in the list, most likely in the system-specific
  1941. part. This can be used to overrule an unwanted setting in the
  1942. system-specific variable.
  1943. `system' Use the system command for opening files, like \"open\".
  1944. This command is specified by the entry whose car is `system'.
  1945. Most likely, the system-specific version of this variable
  1946. does define this command, but you can overrule/replace it
  1947. here.
  1948. `mailcap' Use command specified in the mailcaps.
  1949. string A command to be executed by a shell; %s will be replaced
  1950. by the path to the file.
  1951. function A Lisp function, which will be called with two arguments:
  1952. the file path and the original link string, without the
  1953. \"file:\" prefix.
  1954. For more examples, see the system specific constants
  1955. `org-file-apps-defaults-macosx'
  1956. `org-file-apps-defaults-windowsnt'
  1957. `org-file-apps-defaults-gnu'."
  1958. :group 'org-link-follow
  1959. :type '(repeat
  1960. (cons (choice :value ""
  1961. (string :tag "Extension")
  1962. (const :tag "System command to open files" system)
  1963. (const :tag "Default for unrecognized files" t)
  1964. (const :tag "Remote file" remote)
  1965. (const :tag "Links to a directory" directory)
  1966. (const :tag "Any files that have Emacs modes"
  1967. auto-mode))
  1968. (choice :value ""
  1969. (const :tag "Visit with Emacs" emacs)
  1970. (const :tag "Use default" default)
  1971. (const :tag "Use the system command" system)
  1972. (string :tag "Command")
  1973. (function :tag "Function")))))
  1974. (defcustom org-doi-server-url "http://dx.doi.org/"
  1975. "The URL of the DOI server."
  1976. :type 'string
  1977. :version "24.3"
  1978. :group 'org-link-follow)
  1979. (defgroup org-refile nil
  1980. "Options concerning refiling entries in Org-mode."
  1981. :tag "Org Refile"
  1982. :group 'org)
  1983. (defcustom org-directory "~/org"
  1984. "Directory with Org files.
  1985. This is just a default location to look for Org files. There is no need
  1986. at all to put your files into this directory. It is used in the
  1987. following situations:
  1988. 1. When a capture template specifies a target file that is not an
  1989. absolute path. The path will then be interpreted relative to
  1990. `org-directory'
  1991. 2. When the value of variable `org-agenda-files' is a single file, any
  1992. relative paths in this file will be taken as relative to
  1993. `org-directory'."
  1994. :group 'org-refile
  1995. :group 'org-capture
  1996. :type 'directory)
  1997. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1998. "Default target for storing notes.
  1999. Used as a fall back file for org-capture.el, for templates that
  2000. do not specify a target file."
  2001. :group 'org-refile
  2002. :group 'org-capture
  2003. :type 'file)
  2004. (defcustom org-goto-interface 'outline
  2005. "The default interface to be used for `org-goto'.
  2006. Allowed values are:
  2007. outline The interface shows an outline of the relevant file
  2008. and the correct heading is found by moving through
  2009. the outline or by searching with incremental search.
  2010. outline-path-completion Headlines in the current buffer are offered via
  2011. completion. This is the interface also used by
  2012. the refile command."
  2013. :group 'org-refile
  2014. :type '(choice
  2015. (const :tag "Outline" outline)
  2016. (const :tag "Outline-path-completion" outline-path-completion)))
  2017. (defcustom org-goto-max-level 5
  2018. "Maximum target level when running `org-goto' with refile interface."
  2019. :group 'org-refile
  2020. :type 'integer)
  2021. (defcustom org-reverse-note-order nil
  2022. "Non-nil means store new notes at the beginning of a file or entry.
  2023. When nil, new notes will be filed to the end of a file or entry.
  2024. This can also be a list with cons cells of regular expressions that
  2025. are matched against file names, and values."
  2026. :group 'org-capture
  2027. :group 'org-refile
  2028. :type '(choice
  2029. (const :tag "Reverse always" t)
  2030. (const :tag "Reverse never" nil)
  2031. (repeat :tag "By file name regexp"
  2032. (cons regexp boolean))))
  2033. (defcustom org-log-refile nil
  2034. "Information to record when a task is refiled.
  2035. Possible values are:
  2036. nil Don't add anything
  2037. time Add a time stamp to the task
  2038. note Prompt for a note and add it with template `org-log-note-headings'
  2039. This option can also be set with on a per-file-basis with
  2040. #+STARTUP: nologrefile
  2041. #+STARTUP: logrefile
  2042. #+STARTUP: lognoterefile
  2043. You can have local logging settings for a subtree by setting the LOGGING
  2044. property to one or more of these keywords.
  2045. When bulk-refiling from the agenda, the value `note' is forbidden and
  2046. will temporarily be changed to `time'."
  2047. :group 'org-refile
  2048. :group 'org-progress
  2049. :version "24.1"
  2050. :type '(choice
  2051. (const :tag "No logging" nil)
  2052. (const :tag "Record timestamp" time)
  2053. (const :tag "Record timestamp with note." note)))
  2054. (defcustom org-refile-targets nil
  2055. "Targets for refiling entries with \\[org-refile].
  2056. This is a list of cons cells. Each cell contains:
  2057. - a specification of the files to be considered, either a list of files,
  2058. or a symbol whose function or variable value will be used to retrieve
  2059. a file name or a list of file names. If you use `org-agenda-files' for
  2060. that, all agenda files will be scanned for targets. Nil means consider
  2061. headings in the current buffer.
  2062. - A specification of how to find candidate refile targets. This may be
  2063. any of:
  2064. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  2065. This tag has to be present in all target headlines, inheritance will
  2066. not be considered.
  2067. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  2068. todo keyword.
  2069. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  2070. headlines that are refiling targets.
  2071. - a cons cell (:level . N). Any headline of level N is considered a target.
  2072. Note that, when `org-odd-levels-only' is set, level corresponds to
  2073. order in hierarchy, not to the number of stars.
  2074. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  2075. Note that, when `org-odd-levels-only' is set, level corresponds to
  2076. order in hierarchy, not to the number of stars.
  2077. Each element of this list generates a set of possible targets.
  2078. The union of these sets is presented (with completion) to
  2079. the user by `org-refile'.
  2080. You can set the variable `org-refile-target-verify-function' to a function
  2081. to verify each headline found by the simple criteria above.
  2082. When this variable is nil, all top-level headlines in the current buffer
  2083. are used, equivalent to the value `((nil . (:level . 1))'."
  2084. :group 'org-refile
  2085. :type '(repeat
  2086. (cons
  2087. (choice :value org-agenda-files
  2088. (const :tag "All agenda files" org-agenda-files)
  2089. (const :tag "Current buffer" nil)
  2090. (function) (variable) (file))
  2091. (choice :tag "Identify target headline by"
  2092. (cons :tag "Specific tag" (const :value :tag) (string))
  2093. (cons :tag "TODO keyword" (const :value :todo) (string))
  2094. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  2095. (cons :tag "Level number" (const :value :level) (integer))
  2096. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  2097. (defcustom org-refile-target-verify-function nil
  2098. "Function to verify if the headline at point should be a refile target.
  2099. The function will be called without arguments, with point at the
  2100. beginning of the headline. It should return t and leave point
  2101. where it is if the headline is a valid target for refiling.
  2102. If the target should not be selected, the function must return nil.
  2103. In addition to this, it may move point to a place from where the search
  2104. should be continued. For example, the function may decide that the entire
  2105. subtree of the current entry should be excluded and move point to the end
  2106. of the subtree."
  2107. :group 'org-refile
  2108. :type '(choice
  2109. (const nil)
  2110. (function)))
  2111. (defcustom org-refile-use-cache nil
  2112. "Non-nil means cache refile targets to speed up the process.
  2113. \\<org-mode-map>\
  2114. The cache for a particular file will be updated automatically when
  2115. the buffer has been killed, or when any of the marker used for flagging
  2116. refile targets no longer points at a live buffer.
  2117. If you have added new entries to a buffer that might themselves be targets,
  2118. you need to clear the cache manually by pressing `C-0 \\[org-refile]' or,
  2119. if you find that easier, \
  2120. `\\[universal-argument] \\[universal-argument] \\[universal-argument] \
  2121. \\[org-refile]'."
  2122. :group 'org-refile
  2123. :version "24.1"
  2124. :type 'boolean)
  2125. (defcustom org-refile-use-outline-path nil
  2126. "Non-nil means provide refile targets as paths.
  2127. So a level 3 headline will be available as level1/level2/level3.
  2128. When the value is `file', also include the file name (without directory)
  2129. into the path. In this case, you can also stop the completion after
  2130. the file name, to get entries inserted as top level in the file.
  2131. When `full-file-path', include the full file path."
  2132. :group 'org-refile
  2133. :type '(choice
  2134. (const :tag "Not" nil)
  2135. (const :tag "Yes" t)
  2136. (const :tag "Start with file name" file)
  2137. (const :tag "Start with full file path" full-file-path)))
  2138. (defcustom org-outline-path-complete-in-steps t
  2139. "Non-nil means complete the outline path in hierarchical steps.
  2140. When Org-mode uses the refile interface to select an outline path
  2141. \(see variable `org-refile-use-outline-path'), the completion of
  2142. the path can be done in a single go, or it can be done in steps down
  2143. the headline hierarchy. Going in steps is probably the best if you
  2144. do not use a special completion package like `ido' or `icicles'.
  2145. However, when using these packages, going in one step can be very
  2146. fast, while still showing the whole path to the entry."
  2147. :group 'org-refile
  2148. :type 'boolean)
  2149. (defcustom org-refile-allow-creating-parent-nodes nil
  2150. "Non-nil means allow the creation of new nodes as refile targets.
  2151. New nodes are then created by adding \"/new node name\" to the completion
  2152. of an existing node. When the value of this variable is `confirm',
  2153. new node creation must be confirmed by the user (recommended).
  2154. When nil, the completion must match an existing entry.
  2155. Note that, if the new heading is not seen by the criteria
  2156. listed in `org-refile-targets', multiple instances of the same
  2157. heading would be created by trying again to file under the new
  2158. heading."
  2159. :group 'org-refile
  2160. :type '(choice
  2161. (const :tag "Never" nil)
  2162. (const :tag "Always" t)
  2163. (const :tag "Prompt for confirmation" confirm)))
  2164. (defcustom org-refile-active-region-within-subtree nil
  2165. "Non-nil means also refile active region within a subtree.
  2166. By default `org-refile' doesn't allow refiling regions if they
  2167. don't contain a set of subtrees, but it might be convenient to
  2168. do so sometimes: in that case, the first line of the region is
  2169. converted to a headline before refiling."
  2170. :group 'org-refile
  2171. :version "24.1"
  2172. :type 'boolean)
  2173. (defgroup org-todo nil
  2174. "Options concerning TODO items in Org-mode."
  2175. :tag "Org TODO"
  2176. :group 'org)
  2177. (defgroup org-progress nil
  2178. "Options concerning Progress logging in Org-mode."
  2179. :tag "Org Progress"
  2180. :group 'org-time)
  2181. (defvar org-todo-interpretation-widgets
  2182. '((:tag "Sequence (cycling hits every state)" sequence)
  2183. (:tag "Type (cycling directly to DONE)" type))
  2184. "The available interpretation symbols for customizing `org-todo-keywords'.
  2185. Interested libraries should add to this list.")
  2186. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  2187. "List of TODO entry keyword sequences and their interpretation.
  2188. \\<org-mode-map>This is a list of sequences.
  2189. Each sequence starts with a symbol, either `sequence' or `type',
  2190. indicating if the keywords should be interpreted as a sequence of
  2191. action steps, or as different types of TODO items. The first
  2192. keywords are states requiring action - these states will select a headline
  2193. for inclusion into the global TODO list Org-mode produces. If one of
  2194. the \"keywords\" is the vertical bar, \"|\", the remaining keywords
  2195. signify that no further action is necessary. If \"|\" is not found,
  2196. the last keyword is treated as the only DONE state of the sequence.
  2197. The command \\[org-todo] cycles an entry through these states, and one
  2198. additional state where no keyword is present. For details about this
  2199. cycling, see the manual.
  2200. TODO keywords and interpretation can also be set on a per-file basis with
  2201. the special #+SEQ_TODO and #+TYP_TODO lines.
  2202. Each keyword can optionally specify a character for fast state selection
  2203. \(in combination with the variable `org-use-fast-todo-selection')
  2204. and specifiers for state change logging, using the same syntax that
  2205. is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says that
  2206. the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  2207. indicates to record a time stamp each time this state is selected.
  2208. Each keyword may also specify if a timestamp or a note should be
  2209. recorded when entering or leaving the state, by adding additional
  2210. characters in the parenthesis after the keyword. This looks like this:
  2211. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  2212. record only the time of the state change. With X and Y being either
  2213. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  2214. Y when leaving the state if and only if the *target* state does not
  2215. define X. You may omit any of the fast-selection key or X or /Y,
  2216. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  2217. For backward compatibility, this variable may also be just a list
  2218. of keywords. In this case the interpretation (sequence or type) will be
  2219. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  2220. :group 'org-todo
  2221. :group 'org-keywords
  2222. :type '(choice
  2223. (repeat :tag "Old syntax, just keywords"
  2224. (string :tag "Keyword"))
  2225. (repeat :tag "New syntax"
  2226. (cons
  2227. (choice
  2228. :tag "Interpretation"
  2229. ;;Quick and dirty way to see
  2230. ;;`org-todo-interpretations'. This takes the
  2231. ;;place of item arguments
  2232. :convert-widget
  2233. (lambda (widget)
  2234. (widget-put widget
  2235. :args (mapcar
  2236. (lambda (x)
  2237. (widget-convert
  2238. (cons 'const x)))
  2239. org-todo-interpretation-widgets))
  2240. widget))
  2241. (repeat
  2242. (string :tag "Keyword"))))))
  2243. (defvar-local org-todo-keywords-1 nil
  2244. "All TODO and DONE keywords active in a buffer.")
  2245. (defvar org-todo-keywords-for-agenda nil)
  2246. (defvar org-done-keywords-for-agenda nil)
  2247. (defvar org-todo-keyword-alist-for-agenda nil)
  2248. (defvar org-tag-alist-for-agenda nil
  2249. "Alist of all tags from all agenda files.")
  2250. (defvar org-tag-groups-alist-for-agenda nil
  2251. "Alist of all groups tags from all current agenda files.")
  2252. (defvar-local org-tag-groups-alist nil)
  2253. (defvar org-agenda-contributing-files nil)
  2254. (defvar-local org-current-tag-alist nil
  2255. "Alist of all tag groups in current buffer.
  2256. This variable takes into consideration `org-tag-alist',
  2257. `org-tag-persistent-alist' and TAGS keywords in the buffer.")
  2258. (defvar-local org-not-done-keywords nil)
  2259. (defvar-local org-done-keywords nil)
  2260. (defvar-local org-todo-heads nil)
  2261. (defvar-local org-todo-sets nil)
  2262. (defvar-local org-todo-log-states nil)
  2263. (defvar-local org-todo-kwd-alist nil)
  2264. (defvar-local org-todo-key-alist nil)
  2265. (defvar-local org-todo-key-trigger nil)
  2266. (defcustom org-todo-interpretation 'sequence
  2267. "Controls how TODO keywords are interpreted.
  2268. This variable is in principle obsolete and is only used for
  2269. backward compatibility, if the interpretation of todo keywords is
  2270. not given already in `org-todo-keywords'. See that variable for
  2271. more information."
  2272. :group 'org-todo
  2273. :group 'org-keywords
  2274. :type '(choice (const sequence)
  2275. (const type)))
  2276. (defcustom org-use-fast-todo-selection t
  2277. "\\<org-mode-map>\
  2278. Non-nil means use the fast todo selection scheme with \\[org-todo].
  2279. This variable describes if and under what circumstances the cycling
  2280. mechanism for TODO keywords will be replaced by a single-key, direct
  2281. selection scheme.
  2282. When nil, fast selection is never used.
  2283. When the symbol `prefix', it will be used when `org-todo' is called
  2284. with a prefix argument, i.e. `\\[universal-argument] \\[org-todo]' \
  2285. in an Org-mode buffer, and
  2286. `\\[universal-argument] t' in an agenda buffer.
  2287. When t, fast selection is used by default. In this case, the prefix
  2288. argument forces cycling instead.
  2289. In all cases, the special interface is only used if access keys have
  2290. actually been assigned by the user, i.e. if keywords in the configuration
  2291. are followed by a letter in parenthesis, like TODO(t)."
  2292. :group 'org-todo
  2293. :type '(choice
  2294. (const :tag "Never" nil)
  2295. (const :tag "By default" t)
  2296. (const :tag "Only with C-u C-c C-t" prefix)))
  2297. (defcustom org-provide-todo-statistics t
  2298. "Non-nil means update todo statistics after insert and toggle.
  2299. ALL-HEADLINES means update todo statistics by including headlines
  2300. with no TODO keyword as well, counting them as not done.
  2301. A list of TODO keywords means the same, but skip keywords that are
  2302. not in this list.
  2303. When set to a list of two lists, the first list contains keywords
  2304. to consider as TODO keywords, the second list contains keywords
  2305. to consider as DONE keywords.
  2306. When this is set, todo statistics is updated in the parent of the
  2307. current entry each time a todo state is changed."
  2308. :group 'org-todo
  2309. :type '(choice
  2310. (const :tag "Yes, only for TODO entries" t)
  2311. (const :tag "Yes, including all entries" all-headlines)
  2312. (repeat :tag "Yes, for TODOs in this list"
  2313. (string :tag "TODO keyword"))
  2314. (list :tag "Yes, for TODOs and DONEs in these lists"
  2315. (repeat (string :tag "TODO keyword"))
  2316. (repeat (string :tag "DONE keyword")))
  2317. (other :tag "No TODO statistics" nil)))
  2318. (defcustom org-hierarchical-todo-statistics t
  2319. "Non-nil means TODO statistics covers just direct children.
  2320. When nil, all entries in the subtree are considered.
  2321. This has only an effect if `org-provide-todo-statistics' is set.
  2322. To set this to nil for only a single subtree, use a COOKIE_DATA
  2323. property and include the word \"recursive\" into the value."
  2324. :group 'org-todo
  2325. :type 'boolean)
  2326. (defcustom org-after-todo-state-change-hook nil
  2327. "Hook which is run after the state of a TODO item was changed.
  2328. The new state (a string with a TODO keyword, or nil) is available in the
  2329. Lisp variable `org-state'."
  2330. :group 'org-todo
  2331. :type 'hook)
  2332. (defvar org-blocker-hook nil
  2333. "Hook for functions that are allowed to block a state change.
  2334. Functions in this hook should not modify the buffer.
  2335. Each function gets as its single argument a property list,
  2336. see `org-trigger-hook' for more information about this list.
  2337. If any of the functions in this hook returns nil, the state change
  2338. is blocked.")
  2339. (defvar org-trigger-hook nil
  2340. "Hook for functions that are triggered by a state change.
  2341. Each function gets as its single argument a property list with at
  2342. least the following elements:
  2343. (:type type-of-change :position pos-at-entry-start
  2344. :from old-state :to new-state)
  2345. Depending on the type, more properties may be present.
  2346. This mechanism is currently implemented for:
  2347. TODO state changes
  2348. ------------------
  2349. :type todo-state-change
  2350. :from previous state (keyword as a string), or nil, or a symbol
  2351. `todo' or `done', to indicate the general type of state.
  2352. :to new state, like in :from")
  2353. (defcustom org-enforce-todo-dependencies nil
  2354. "Non-nil means undone TODO entries will block switching the parent to DONE.
  2355. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  2356. be blocked if any prior sibling is not yet done.
  2357. Finally, if the parent is blocked because of ordered siblings of its own,
  2358. the child will also be blocked."
  2359. :set (lambda (var val)
  2360. (set var val)
  2361. (if val
  2362. (add-hook 'org-blocker-hook
  2363. 'org-block-todo-from-children-or-siblings-or-parent)
  2364. (remove-hook 'org-blocker-hook
  2365. 'org-block-todo-from-children-or-siblings-or-parent)))
  2366. :group 'org-todo
  2367. :type 'boolean)
  2368. (defcustom org-enforce-todo-checkbox-dependencies nil
  2369. "Non-nil means unchecked boxes will block switching the parent to DONE.
  2370. When this is nil, checkboxes have no influence on switching TODO states.
  2371. When non-nil, you first need to check off all check boxes before the TODO
  2372. entry can be switched to DONE.
  2373. This variable needs to be set before org.el is loaded, and you need to
  2374. restart Emacs after a change to make the change effective. The only way
  2375. to change is while Emacs is running is through the customize interface."
  2376. :set (lambda (var val)
  2377. (set var val)
  2378. (if val
  2379. (add-hook 'org-blocker-hook
  2380. 'org-block-todo-from-checkboxes)
  2381. (remove-hook 'org-blocker-hook
  2382. 'org-block-todo-from-checkboxes)))
  2383. :group 'org-todo
  2384. :type 'boolean)
  2385. (defcustom org-treat-insert-todo-heading-as-state-change nil
  2386. "Non-nil means inserting a TODO heading is treated as state change.
  2387. So when the command \\[org-insert-todo-heading] is used, state change
  2388. logging will apply if appropriate. When nil, the new TODO item will
  2389. be inserted directly, and no logging will take place."
  2390. :group 'org-todo
  2391. :type 'boolean)
  2392. (defcustom org-treat-S-cursor-todo-selection-as-state-change t
  2393. "Non-nil means switching TODO states with S-cursor counts as state change.
  2394. This is the default behavior. However, setting this to nil allows a
  2395. convenient way to select a TODO state and bypass any logging associated
  2396. with that."
  2397. :group 'org-todo
  2398. :type 'boolean)
  2399. (defcustom org-todo-state-tags-triggers nil
  2400. "Tag changes that should be triggered by TODO state changes.
  2401. This is a list. Each entry is
  2402. (state-change (tag . flag) .......)
  2403. State-change can be a string with a state, and empty string to indicate the
  2404. state that has no TODO keyword, or it can be one of the symbols `todo'
  2405. or `done', meaning any not-done or done state, respectively."
  2406. :group 'org-todo
  2407. :group 'org-tags
  2408. :type '(repeat
  2409. (cons (choice :tag "When changing to"
  2410. (const :tag "Not-done state" todo)
  2411. (const :tag "Done state" done)
  2412. (string :tag "State"))
  2413. (repeat
  2414. (cons :tag "Tag action"
  2415. (string :tag "Tag")
  2416. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  2417. (defcustom org-log-done nil
  2418. "Information to record when a task moves to the DONE state.
  2419. Possible values are:
  2420. nil Don't add anything, just change the keyword
  2421. time Add a time stamp to the task
  2422. note Prompt for a note and add it with template `org-log-note-headings'
  2423. This option can also be set with on a per-file-basis with
  2424. #+STARTUP: nologdone
  2425. #+STARTUP: logdone
  2426. #+STARTUP: lognotedone
  2427. You can have local logging settings for a subtree by setting the LOGGING
  2428. property to one or more of these keywords."
  2429. :group 'org-todo
  2430. :group 'org-progress
  2431. :type '(choice
  2432. (const :tag "No logging" nil)
  2433. (const :tag "Record CLOSED timestamp" time)
  2434. (const :tag "Record CLOSED timestamp with note." note)))
  2435. ;; Normalize old uses of org-log-done.
  2436. (cond
  2437. ((eq org-log-done t) (setq org-log-done 'time))
  2438. ((and (listp org-log-done) (memq 'done org-log-done))
  2439. (setq org-log-done 'note)))
  2440. (defcustom org-log-reschedule nil
  2441. "Information to record when the scheduling date of a tasks is modified.
  2442. Possible values are:
  2443. nil Don't add anything, just change the date
  2444. time Add a time stamp to the task
  2445. note Prompt for a note and add it with template `org-log-note-headings'
  2446. This option can also be set with on a per-file-basis with
  2447. #+STARTUP: nologreschedule
  2448. #+STARTUP: logreschedule
  2449. #+STARTUP: lognotereschedule"
  2450. :group 'org-todo
  2451. :group 'org-progress
  2452. :type '(choice
  2453. (const :tag "No logging" nil)
  2454. (const :tag "Record timestamp" time)
  2455. (const :tag "Record timestamp with note." note)))
  2456. (defcustom org-log-redeadline nil
  2457. "Information to record when the deadline date of a tasks is modified.
  2458. Possible values are:
  2459. nil Don't add anything, just change the date
  2460. time Add a time stamp to the task
  2461. note Prompt for a note and add it with template `org-log-note-headings'
  2462. This option can also be set with on a per-file-basis with
  2463. #+STARTUP: nologredeadline
  2464. #+STARTUP: logredeadline
  2465. #+STARTUP: lognoteredeadline
  2466. You can have local logging settings for a subtree by setting the LOGGING
  2467. property to one or more of these keywords."
  2468. :group 'org-todo
  2469. :group 'org-progress
  2470. :type '(choice
  2471. (const :tag "No logging" nil)
  2472. (const :tag "Record timestamp" time)
  2473. (const :tag "Record timestamp with note." note)))
  2474. (defcustom org-log-note-clock-out nil
  2475. "Non-nil means record a note when clocking out of an item.
  2476. This can also be configured on a per-file basis by adding one of
  2477. the following lines anywhere in the buffer:
  2478. #+STARTUP: lognoteclock-out
  2479. #+STARTUP: nolognoteclock-out"
  2480. :group 'org-todo
  2481. :group 'org-progress
  2482. :type 'boolean)
  2483. (defcustom org-log-done-with-time t
  2484. "Non-nil means the CLOSED time stamp will contain date and time.
  2485. When nil, only the date will be recorded."
  2486. :group 'org-progress
  2487. :type 'boolean)
  2488. (defcustom org-log-note-headings
  2489. '((done . "CLOSING NOTE %t")
  2490. (state . "State %-12s from %-12S %t")
  2491. (note . "Note taken on %t")
  2492. (reschedule . "Rescheduled from %S on %t")
  2493. (delschedule . "Not scheduled, was %S on %t")
  2494. (redeadline . "New deadline from %S on %t")
  2495. (deldeadline . "Removed deadline, was %S on %t")
  2496. (refile . "Refiled on %t")
  2497. (clock-out . ""))
  2498. "Headings for notes added to entries.
  2499. The value is an alist, with the car being a symbol indicating the
  2500. note context, and the cdr is the heading to be used. The heading
  2501. may also be the empty string. The following placeholders can be
  2502. used:
  2503. %t a time stamp.
  2504. %T an active time stamp instead the default inactive one
  2505. %d a short-format time stamp.
  2506. %D an active short-format time stamp.
  2507. %s the new TODO state or time stamp (inactive), in double quotes.
  2508. %S the old TODO state or time stamp (inactive), in double quotes.
  2509. %u the user name.
  2510. %U full user name.
  2511. In fact, it is not a good idea to change the `state' entry,
  2512. because Agenda Log mode depends on the format of these entries."
  2513. :group 'org-todo
  2514. :group 'org-progress
  2515. :type '(list :greedy t
  2516. (cons (const :tag "Heading when closing an item" done) string)
  2517. (cons (const :tag
  2518. "Heading when changing todo state (todo sequence only)"
  2519. state) string)
  2520. (cons (const :tag "Heading when just taking a note" note) string)
  2521. (cons (const :tag "Heading when rescheduling" reschedule) string)
  2522. (cons (const :tag "Heading when an item is no longer scheduled" delschedule) string)
  2523. (cons (const :tag "Heading when changing deadline" redeadline) string)
  2524. (cons (const :tag "Heading when deleting a deadline" deldeadline) string)
  2525. (cons (const :tag "Heading when refiling" refile) string)
  2526. (cons (const :tag "Heading when clocking out" clock-out) string)))
  2527. (unless (assq 'note org-log-note-headings)
  2528. (push '(note . "%t") org-log-note-headings))
  2529. (defcustom org-log-into-drawer nil
  2530. "Non-nil means insert state change notes and time stamps into a drawer.
  2531. When nil, state changes notes will be inserted after the headline and
  2532. any scheduling and clock lines, but not inside a drawer.
  2533. The value of this variable should be the name of the drawer to use.
  2534. LOGBOOK is proposed as the default drawer for this purpose, you can
  2535. also set this to a string to define the drawer of your choice.
  2536. A value of t is also allowed, representing \"LOGBOOK\".
  2537. A value of t or nil can also be set with on a per-file-basis with
  2538. #+STARTUP: logdrawer
  2539. #+STARTUP: nologdrawer
  2540. If this variable is set, `org-log-state-notes-insert-after-drawers'
  2541. will be ignored.
  2542. You can set the property LOG_INTO_DRAWER to overrule this setting for
  2543. a subtree.
  2544. Do not check directly this variable in a Lisp program. Call
  2545. function `org-log-into-drawer' instead."
  2546. :group 'org-todo
  2547. :group 'org-progress
  2548. :type '(choice
  2549. (const :tag "Not into a drawer" nil)
  2550. (const :tag "LOGBOOK" t)
  2551. (string :tag "Other")))
  2552. (org-defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer)
  2553. (defun org-log-into-drawer ()
  2554. "Name of the log drawer, as a string, or nil.
  2555. This is the value of `org-log-into-drawer'. However, if the
  2556. current entry has or inherits a LOG_INTO_DRAWER property, it will
  2557. be used instead of the default value."
  2558. (let ((p (org-entry-get nil "LOG_INTO_DRAWER" 'inherit t)))
  2559. (cond ((equal p "nil") nil)
  2560. ((equal p "t") "LOGBOOK")
  2561. ((stringp p) p)
  2562. (p "LOGBOOK")
  2563. ((stringp org-log-into-drawer) org-log-into-drawer)
  2564. (org-log-into-drawer "LOGBOOK"))))
  2565. (defcustom org-log-state-notes-insert-after-drawers nil
  2566. "Non-nil means insert state change notes after any drawers in entry.
  2567. Only the drawers that *immediately* follow the headline and the
  2568. deadline/scheduled line are skipped.
  2569. When nil, insert notes right after the heading and perhaps the line
  2570. with deadline/scheduling if present.
  2571. This variable will have no effect if `org-log-into-drawer' is
  2572. set."
  2573. :group 'org-todo
  2574. :group 'org-progress
  2575. :type 'boolean)
  2576. (defcustom org-log-states-order-reversed t
  2577. "Non-nil means the latest state note will be directly after heading.
  2578. When nil, the state change notes will be ordered according to time.
  2579. This option can also be set with on a per-file-basis with
  2580. #+STARTUP: logstatesreversed
  2581. #+STARTUP: nologstatesreversed"
  2582. :group 'org-todo
  2583. :group 'org-progress
  2584. :type 'boolean)
  2585. (defcustom org-todo-repeat-to-state nil
  2586. "The TODO state to which a repeater should return the repeating task.
  2587. By default this is the first task in a TODO sequence, or the previous state
  2588. in a TODO_TYP set. But you can specify another task here.
  2589. alternatively, set the :REPEAT_TO_STATE: property of the entry."
  2590. :group 'org-todo
  2591. :version "24.1"
  2592. :type '(choice (const :tag "Head of sequence" nil)
  2593. (string :tag "Specific state")))
  2594. (defcustom org-log-repeat 'time
  2595. "Non-nil means record moving through the DONE state when triggering repeat.
  2596. An auto-repeating task is immediately switched back to TODO when
  2597. marked DONE. If you are not logging state changes (by adding \"@\"
  2598. or \"!\" to the TODO keyword definition), or set `org-log-done' to
  2599. record a closing note, there will be no record of the task moving
  2600. through DONE. This variable forces taking a note anyway.
  2601. nil Don't force a record
  2602. time Record a time stamp
  2603. note Prompt for a note and add it with template `org-log-note-headings'
  2604. This option can also be set with on a per-file-basis with
  2605. #+STARTUP: nologrepeat
  2606. #+STARTUP: logrepeat
  2607. #+STARTUP: lognoterepeat
  2608. You can have local logging settings for a subtree by setting the LOGGING
  2609. property to one or more of these keywords."
  2610. :group 'org-todo
  2611. :group 'org-progress
  2612. :type '(choice
  2613. (const :tag "Don't force a record" nil)
  2614. (const :tag "Force recording the DONE state" time)
  2615. (const :tag "Force recording a note with the DONE state" note)))
  2616. (defgroup org-priorities nil
  2617. "Priorities in Org-mode."
  2618. :tag "Org Priorities"
  2619. :group 'org-todo)
  2620. (defcustom org-enable-priority-commands t
  2621. "Non-nil means priority commands are active.
  2622. When nil, these commands will be disabled, so that you never accidentally
  2623. set a priority."
  2624. :group 'org-priorities
  2625. :type 'boolean)
  2626. (defcustom org-highest-priority ?A
  2627. "The highest priority of TODO items. A character like ?A, ?B etc.
  2628. Must have a smaller ASCII number than `org-lowest-priority'."
  2629. :group 'org-priorities
  2630. :type 'character)
  2631. (defcustom org-lowest-priority ?C
  2632. "The lowest priority of TODO items. A character like ?A, ?B etc.
  2633. Must have a larger ASCII number than `org-highest-priority'."
  2634. :group 'org-priorities
  2635. :type 'character)
  2636. (defcustom org-default-priority ?B
  2637. "The default priority of TODO items.
  2638. This is the priority an item gets if no explicit priority is given.
  2639. When starting to cycle on an empty priority the first step in the cycle
  2640. depends on `org-priority-start-cycle-with-default'. The resulting first
  2641. step priority must not exceed the range from `org-highest-priority' to
  2642. `org-lowest-priority' which means that `org-default-priority' has to be
  2643. in this range exclusive or inclusive the range boundaries. Else the
  2644. first step refuses to set the default and the second will fall back
  2645. to (depending on the command used) the highest or lowest priority."
  2646. :group 'org-priorities
  2647. :type 'character)
  2648. (defcustom org-priority-start-cycle-with-default t
  2649. "Non-nil means start with default priority when starting to cycle.
  2650. When this is nil, the first step in the cycle will be (depending on the
  2651. command used) one higher or lower than the default priority.
  2652. See also `org-default-priority'."
  2653. :group 'org-priorities
  2654. :type 'boolean)
  2655. (defcustom org-get-priority-function nil
  2656. "Function to extract the priority from a string.
  2657. The string is normally the headline. If this is nil Org computes the
  2658. priority from the priority cookie like [#A] in the headline. It returns
  2659. an integer, increasing by 1000 for each priority level.
  2660. The user can set a different function here, which should take a string
  2661. as an argument and return the numeric priority."
  2662. :group 'org-priorities
  2663. :version "24.1"
  2664. :type '(choice
  2665. (const nil)
  2666. (function)))
  2667. (defgroup org-time nil
  2668. "Options concerning time stamps and deadlines in Org-mode."
  2669. :tag "Org Time"
  2670. :group 'org)
  2671. (defcustom org-time-stamp-rounding-minutes '(0 5)
  2672. "Number of minutes to round time stamps to.
  2673. \\<org-mode-map>\
  2674. These are two values, the first applies when first creating a time stamp.
  2675. The second applies when changing it with the commands `S-up' and `S-down'.
  2676. When changing the time stamp, this means that it will change in steps
  2677. of N minutes, as given by the second value.
  2678. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  2679. numbers should be factors of 60, so for example 5, 10, 15.
  2680. When this is larger than 1, you can still force an exact time stamp by using
  2681. a double prefix argument to a time stamp command like \
  2682. `\\[org-time-stamp]' or `\\[org-time-stamp-inactive],
  2683. and by using a prefix arg to `S-up/down' to specify the exact number
  2684. of minutes to shift."
  2685. :group 'org-time
  2686. :get (lambda (var) ; Make sure both elements are there
  2687. (if (integerp (default-value var))
  2688. (list (default-value var) 5)
  2689. (default-value var)))
  2690. :type '(list
  2691. (integer :tag "when inserting times")
  2692. (integer :tag "when modifying times")))
  2693. ;; Normalize old customizations of this variable.
  2694. (when (integerp org-time-stamp-rounding-minutes)
  2695. (setq org-time-stamp-rounding-minutes
  2696. (list org-time-stamp-rounding-minutes
  2697. org-time-stamp-rounding-minutes)))
  2698. (defcustom org-display-custom-times nil
  2699. "Non-nil means overlay custom formats over all time stamps.
  2700. The formats are defined through the variable `org-time-stamp-custom-formats'.
  2701. To turn this on on a per-file basis, insert anywhere in the file:
  2702. #+STARTUP: customtime"
  2703. :group 'org-time
  2704. :set 'set-default
  2705. :type 'sexp)
  2706. (make-variable-buffer-local 'org-display-custom-times)
  2707. (defcustom org-time-stamp-custom-formats
  2708. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  2709. "Custom formats for time stamps. See `format-time-string' for the syntax.
  2710. These are overlaid over the default ISO format if the variable
  2711. `org-display-custom-times' is set. Time like %H:%M should be at the
  2712. end of the second format. The custom formats are also honored by export
  2713. commands, if custom time display is turned on at the time of export."
  2714. :group 'org-time
  2715. :type 'sexp)
  2716. (defun org-time-stamp-format (&optional long inactive)
  2717. "Get the right format for a time string."
  2718. (let ((f (if long (cdr org-time-stamp-formats)
  2719. (car org-time-stamp-formats))))
  2720. (if inactive
  2721. (concat "[" (substring f 1 -1) "]")
  2722. f)))
  2723. (defcustom org-time-clocksum-format
  2724. '(:days "%dd " :hours "%d" :require-hours t :minutes ":%02d" :require-minutes t)
  2725. "The format string used when creating CLOCKSUM lines.
  2726. This is also used when Org mode generates a time duration.
  2727. The value can be a single format string containing two
  2728. %-sequences, which will be filled with the number of hours and
  2729. minutes in that order.
  2730. Alternatively, the value can be a plist associating any of the
  2731. keys :years, :months, :weeks, :days, :hours or :minutes with
  2732. format strings. The time duration is formatted using only the
  2733. time components that are needed and concatenating the results.
  2734. If a time unit in absent, it falls back to the next smallest
  2735. unit.
  2736. The keys :require-years, :require-months, :require-days,
  2737. :require-weeks, :require-hours, :require-minutes are also
  2738. meaningful. A non-nil value for these keys indicates that the
  2739. corresponding time component should always be included, even if
  2740. its value is 0.
  2741. For example,
  2742. (:days \"%dd\" :hours \"%d\" :require-hours t :minutes \":%02d\"
  2743. :require-minutes t)
  2744. means durations longer than a day will be expressed in days,
  2745. hours and minutes, and durations less than a day will always be
  2746. expressed in hours and minutes (even for durations less than an
  2747. hour).
  2748. The value
  2749. (:days \"%dd\" :minutes \"%dm\")
  2750. means durations longer than a day will be expressed in days and
  2751. minutes, and durations less than a day will be expressed entirely
  2752. in minutes (even for durations longer than an hour)."
  2753. :group 'org-time
  2754. :group 'org-clock
  2755. :version "24.4"
  2756. :package-version '(Org . "8.0")
  2757. :type '(choice (string :tag "Format string")
  2758. (set :tag "Plist"
  2759. (group :inline t (const :tag "Years" :years)
  2760. (string :tag "Format string"))
  2761. (group :inline t
  2762. (const :tag "Always show years" :require-years)
  2763. (const t))
  2764. (group :inline t (const :tag "Months" :months)
  2765. (string :tag "Format string"))
  2766. (group :inline t
  2767. (const :tag "Always show months" :require-months)
  2768. (const t))
  2769. (group :inline t (const :tag "Weeks" :weeks)
  2770. (string :tag "Format string"))
  2771. (group :inline t
  2772. (const :tag "Always show weeks" :require-weeks)
  2773. (const t))
  2774. (group :inline t (const :tag "Days" :days)
  2775. (string :tag "Format string"))
  2776. (group :inline t
  2777. (const :tag "Always show days" :require-days)
  2778. (const t))
  2779. (group :inline t (const :tag "Hours" :hours)
  2780. (string :tag "Format string"))
  2781. (group :inline t
  2782. (const :tag "Always show hours" :require-hours)
  2783. (const t))
  2784. (group :inline t (const :tag "Minutes" :minutes)
  2785. (string :tag "Format string"))
  2786. (group :inline t
  2787. (const :tag "Always show minutes" :require-minutes)
  2788. (const t)))))
  2789. (defcustom org-time-clocksum-use-fractional nil
  2790. "When non-nil, \\[org-clock-display] uses fractional times.
  2791. See `org-time-clocksum-format' for more on time clock formats."
  2792. :group 'org-time
  2793. :group 'org-clock
  2794. :version "24.3"
  2795. :type 'boolean)
  2796. (defcustom org-time-clocksum-use-effort-durations nil
  2797. "When non-nil, \\[org-clock-display] uses effort durations.
  2798. E.g. by default, one day is considered to be a 8 hours effort,
  2799. so a task that has been clocked for 16 hours will be displayed
  2800. as during 2 days in the clock display or in the clocktable.
  2801. See `org-effort-durations' on how to set effort durations
  2802. and `org-time-clocksum-format' for more on time clock formats."
  2803. :group 'org-time
  2804. :group 'org-clock
  2805. :version "24.4"
  2806. :package-version '(Org . "8.0")
  2807. :type 'boolean)
  2808. (defcustom org-time-clocksum-fractional-format "%.2f"
  2809. "The format string used when creating CLOCKSUM lines,
  2810. or when Org mode generates a time duration, if
  2811. `org-time-clocksum-use-fractional' is enabled.
  2812. The value can be a single format string containing one
  2813. %-sequence, which will be filled with the number of hours as
  2814. a float.
  2815. Alternatively, the value can be a plist associating any of the
  2816. keys :years, :months, :weeks, :days, :hours or :minutes with
  2817. a format string. The time duration is formatted using the
  2818. largest time unit which gives a non-zero integer part. If all
  2819. specified formats have zero integer part, the smallest time unit
  2820. is used."
  2821. :group 'org-time
  2822. :type '(choice (string :tag "Format string")
  2823. (set (group :inline t (const :tag "Years" :years)
  2824. (string :tag "Format string"))
  2825. (group :inline t (const :tag "Months" :months)
  2826. (string :tag "Format string"))
  2827. (group :inline t (const :tag "Weeks" :weeks)
  2828. (string :tag "Format string"))
  2829. (group :inline t (const :tag "Days" :days)
  2830. (string :tag "Format string"))
  2831. (group :inline t (const :tag "Hours" :hours)
  2832. (string :tag "Format string"))
  2833. (group :inline t (const :tag "Minutes" :minutes)
  2834. (string :tag "Format string")))))
  2835. (defcustom org-deadline-warning-days 14
  2836. "Number of days before expiration during which a deadline becomes active.
  2837. This variable governs the display in sparse trees and in the agenda.
  2838. When 0 or negative, it means use this number (the absolute value of it)
  2839. even if a deadline has a different individual lead time specified.
  2840. Custom commands can set this variable in the options section."
  2841. :group 'org-time
  2842. :group 'org-agenda-daily/weekly
  2843. :type 'integer)
  2844. (defcustom org-scheduled-delay-days 0
  2845. "Number of days before a scheduled item becomes active.
  2846. This variable governs the display in sparse trees and in the agenda.
  2847. The default value (i.e. 0) means: don't delay scheduled item.
  2848. When negative, it means use this number (the absolute value of it)
  2849. even if a scheduled item has a different individual delay time
  2850. specified.
  2851. Custom commands can set this variable in the options section."
  2852. :group 'org-time
  2853. :group 'org-agenda-daily/weekly
  2854. :version "24.4"
  2855. :package-version '(Org . "8.0")
  2856. :type 'integer)
  2857. (defcustom org-read-date-prefer-future t
  2858. "Non-nil means assume future for incomplete date input from user.
  2859. This affects the following situations:
  2860. 1. The user gives a month but not a year.
  2861. For example, if it is April and you enter \"feb 2\", this will be read
  2862. as Feb 2, *next* year. \"May 5\", however, will be this year.
  2863. 2. The user gives a day, but no month.
  2864. For example, if today is the 15th, and you enter \"3\", Org-mode will
  2865. read this as the third of *next* month. However, if you enter \"17\",
  2866. it will be considered as *this* month.
  2867. If you set this variable to the symbol `time', then also the following
  2868. will work:
  2869. 3. If the user gives a time.
  2870. If the time is before now, it will be interpreted as tomorrow.
  2871. Currently none of this works for ISO week specifications.
  2872. When this option is nil, the current day, month and year will always be
  2873. used as defaults.
  2874. See also `org-agenda-jump-prefer-future'."
  2875. :group 'org-time
  2876. :type '(choice
  2877. (const :tag "Never" nil)
  2878. (const :tag "Check month and day" t)
  2879. (const :tag "Check month, day, and time" time)))
  2880. (defcustom org-agenda-jump-prefer-future 'org-read-date-prefer-future
  2881. "Should the agenda jump command prefer the future for incomplete dates?
  2882. The default is to do the same as configured in `org-read-date-prefer-future'.
  2883. But you can also set a deviating value here.
  2884. This may t or nil, or the symbol `org-read-date-prefer-future'."
  2885. :group 'org-agenda
  2886. :group 'org-time
  2887. :version "24.1"
  2888. :type '(choice
  2889. (const :tag "Use org-read-date-prefer-future"
  2890. org-read-date-prefer-future)
  2891. (const :tag "Never" nil)
  2892. (const :tag "Always" t)))
  2893. (defcustom org-read-date-force-compatible-dates t
  2894. "Should date/time prompt force dates that are guaranteed to work in Emacs?
  2895. Depending on the system Emacs is running on, certain dates cannot
  2896. be represented with the type used internally to represent time.
  2897. Dates between 1970-1-1 and 2038-1-1 can always be represented
  2898. correctly. Some systems allow for earlier dates, some for later,
  2899. some for both. One way to find out it to insert any date into an
  2900. Org buffer, putting the cursor on the year and hitting S-up and
  2901. S-down to test the range.
  2902. When this variable is set to t, the date/time prompt will not let
  2903. you specify dates outside the 1970-2037 range, so it is certain that
  2904. these dates will work in whatever version of Emacs you are
  2905. running, and also that you can move a file from one Emacs implementation
  2906. to another. WHenever Org is forcing the year for you, it will display
  2907. a message and beep.
  2908. When this variable is nil, Org will check if the date is
  2909. representable in the specific Emacs implementation you are using.
  2910. If not, it will force a year, usually the current year, and beep
  2911. to remind you. Currently this setting is not recommended because
  2912. the likelihood that you will open your Org files in an Emacs that
  2913. has limited date range is not negligible.
  2914. A workaround for this problem is to use diary sexp dates for time
  2915. stamps outside of this range."
  2916. :group 'org-time
  2917. :version "24.1"
  2918. :type 'boolean)
  2919. (defcustom org-read-date-display-live t
  2920. "Non-nil means display current interpretation of date prompt live.
  2921. This display will be in an overlay, in the minibuffer."
  2922. :group 'org-time
  2923. :type 'boolean)
  2924. (defcustom org-read-date-popup-calendar t
  2925. "Non-nil means pop up a calendar when prompting for a date.
  2926. In the calendar, the date can be selected with mouse-1. However, the
  2927. minibuffer will also be active, and you can simply enter the date as well.
  2928. When nil, only the minibuffer will be available."
  2929. :group 'org-time
  2930. :type 'boolean)
  2931. (org-defvaralias 'org-popup-calendar-for-date-prompt
  2932. 'org-read-date-popup-calendar)
  2933. (make-obsolete-variable
  2934. 'org-read-date-minibuffer-setup-hook
  2935. "Set `org-read-date-minibuffer-local-map' instead." "24.4")
  2936. (defcustom org-read-date-minibuffer-setup-hook nil
  2937. "Hook to be used to set up keys for the date/time interface.
  2938. Add key definitions to `minibuffer-local-map', which will be a
  2939. temporary copy.
  2940. WARNING: This option is obsolete, you should use
  2941. `org-read-date-minibuffer-local-map' to set up keys."
  2942. :group 'org-time
  2943. :type 'hook)
  2944. (defcustom org-extend-today-until 0
  2945. "The hour when your day really ends. Must be an integer.
  2946. This has influence for the following applications:
  2947. - When switching the agenda to \"today\". It it is still earlier than
  2948. the time given here, the day recognized as TODAY is actually yesterday.
  2949. - When a date is read from the user and it is still before the time given
  2950. here, the current date and time will be assumed to be yesterday, 23:59.
  2951. Also, timestamps inserted in capture templates follow this rule.
  2952. IMPORTANT: This is a feature whose implementation is and likely will
  2953. remain incomplete. Really, it is only here because past midnight seems to
  2954. be the favorite working time of John Wiegley :-)"
  2955. :group 'org-time
  2956. :type 'integer)
  2957. (defcustom org-use-effective-time nil
  2958. "If non-nil, consider `org-extend-today-until' when creating timestamps.
  2959. For example, if `org-extend-today-until' is 8, and it's 4am, then the
  2960. \"effective time\" of any timestamps between midnight and 8am will be
  2961. 23:59 of the previous day."
  2962. :group 'org-time
  2963. :version "24.1"
  2964. :type 'boolean)
  2965. (defcustom org-use-last-clock-out-time-as-effective-time nil
  2966. "When non-nil, use the last clock out time for `org-todo'.
  2967. Note that this option has precedence over the combined use of
  2968. `org-use-effective-time' and `org-extend-today-until'."
  2969. :group 'org-time
  2970. :version "24.4"
  2971. :package-version '(Org . "8.0")
  2972. :type 'boolean)
  2973. (defcustom org-edit-timestamp-down-means-later nil
  2974. "Non-nil means S-down will increase the time in a time stamp.
  2975. When nil, S-up will increase."
  2976. :group 'org-time
  2977. :type 'boolean)
  2978. (defcustom org-calendar-follow-timestamp-change t
  2979. "Non-nil means make the calendar window follow timestamp changes.
  2980. When a timestamp is modified and the calendar window is visible, it will be
  2981. moved to the new date."
  2982. :group 'org-time
  2983. :type 'boolean)
  2984. (defgroup org-tags nil
  2985. "Options concerning tags in Org-mode."
  2986. :tag "Org Tags"
  2987. :group 'org)
  2988. (defcustom org-tag-alist nil
  2989. "Default tags available in Org files.
  2990. The value of this variable is an alist. Associations either:
  2991. (TAG)
  2992. (TAG . SELECT)
  2993. (SPECIAL)
  2994. where TAG is a tag as a string, SELECT is a character, used to
  2995. select that tag through the fast tag selection interface, and
  2996. SPECIAL is one of the following keywords: `:startgroup',
  2997. `:startgrouptag', `:grouptags', `:engroup', `:endgrouptag' or
  2998. `:newline'. These keywords are used to define a hierarchy of
  2999. tags. See manual for details.
  3000. When this variable is nil, Org mode bases tag input on what is
  3001. already in the buffer. The value can be overridden locally by
  3002. using a TAGS keyword, e.g.,
  3003. #+TAGS: tag1 tag2
  3004. See also `org-tag-persistent-alist' to sidestep this behavior."
  3005. :group 'org-tags
  3006. :type '(repeat
  3007. (choice
  3008. (cons (string :tag "Tag name")
  3009. (character :tag "Access char"))
  3010. (const :tag "Start radio group" (:startgroup))
  3011. (const :tag "Start tag group, non distinct" (:startgrouptag))
  3012. (const :tag "Group tags delimiter" (:grouptags))
  3013. (const :tag "End radio group" (:endgroup))
  3014. (const :tag "End tag group, non distinct" (:endgrouptag))
  3015. (const :tag "New line" (:newline)))))
  3016. (defcustom org-tag-persistent-alist nil
  3017. "Tags always available in Org files.
  3018. The value of this variable is an alist. Associations either:
  3019. (TAG)
  3020. (TAG . SELECT)
  3021. (SPECIAL)
  3022. where TAG is a tag as a string, SELECT is a character, used to
  3023. select that tag through the fast tag selection interface, and
  3024. SPECIAL is one of the following keywords: `:startgroup',
  3025. `:startgrouptag', `:grouptags', `:engroup', `:endgrouptag' or
  3026. `:newline'. These keywords are used to define a hierarchy of
  3027. tags. See manual for details.
  3028. Unlike to `org-tag-alist', tags defined in this variable do not
  3029. depend on a local TAGS keyword. Instead, to disable these tags
  3030. on a per-file basis, insert anywhere in the file:
  3031. #+STARTUP: noptag"
  3032. :group 'org-tags
  3033. :type '(repeat
  3034. (choice
  3035. (cons (string :tag "Tag name")
  3036. (character :tag "Access char"))
  3037. (const :tag "Start radio group" (:startgroup))
  3038. (const :tag "Start tag group, non distinct" (:startgrouptag))
  3039. (const :tag "Group tags delimiter" (:grouptags))
  3040. (const :tag "End radio group" (:endgroup))
  3041. (const :tag "End tag group, non distinct" (:endgrouptag))
  3042. (const :tag "New line" (:newline)))))
  3043. (defcustom org-complete-tags-always-offer-all-agenda-tags nil
  3044. "If non-nil, always offer completion for all tags of all agenda files.
  3045. Instead of customizing this variable directly, you might want to
  3046. set it locally for capture buffers, because there no list of
  3047. tags in that file can be created dynamically (there are none).
  3048. (add-hook \\='org-capture-mode-hook
  3049. (lambda ()
  3050. (setq-local org-complete-tags-always-offer-all-agenda-tags t)))"
  3051. :group 'org-tags
  3052. :version "24.1"
  3053. :type 'boolean)
  3054. (defvar org-file-tags nil
  3055. "List of tags that can be inherited by all entries in the file.
  3056. The tags will be inherited if the variable `org-use-tag-inheritance'
  3057. says they should be.
  3058. This variable is populated from #+FILETAGS lines.")
  3059. (defcustom org-use-fast-tag-selection 'auto
  3060. "Non-nil means use fast tag selection scheme.
  3061. This is a special interface to select and deselect tags with single keys.
  3062. When nil, fast selection is never used.
  3063. When the symbol `auto', fast selection is used if and only if selection
  3064. characters for tags have been configured, either through the variable
  3065. `org-tag-alist' or through a #+TAGS line in the buffer.
  3066. When t, fast selection is always used and selection keys are assigned
  3067. automatically if necessary."
  3068. :group 'org-tags
  3069. :type '(choice
  3070. (const :tag "Always" t)
  3071. (const :tag "Never" nil)
  3072. (const :tag "When selection characters are configured" auto)))
  3073. (defcustom org-fast-tag-selection-single-key nil
  3074. "Non-nil means fast tag selection exits after first change.
  3075. When nil, you have to press RET to exit it.
  3076. During fast tag selection, you can toggle this flag with `C-c'.
  3077. This variable can also have the value `expert'. In this case, the window
  3078. displaying the tags menu is not even shown, until you press C-c again."
  3079. :group 'org-tags
  3080. :type '(choice
  3081. (const :tag "No" nil)
  3082. (const :tag "Yes" t)
  3083. (const :tag "Expert" expert)))
  3084. (defvar org-fast-tag-selection-include-todo nil
  3085. "Non-nil means fast tags selection interface will also offer TODO states.
  3086. This is an undocumented feature, you should not rely on it.")
  3087. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  3088. "The column to which tags should be indented in a headline.
  3089. If this number is positive, it specifies the column. If it is negative,
  3090. it means that the tags should be flushright to that column. For example,
  3091. -80 works well for a normal 80 character screen.
  3092. When 0, place tags directly after headline text, with only one space in
  3093. between."
  3094. :group 'org-tags
  3095. :type 'integer)
  3096. (defcustom org-auto-align-tags t
  3097. "Non-nil keeps tags aligned when modifying headlines.
  3098. Some operations (i.e. demoting) change the length of a headline and
  3099. therefore shift the tags around. With this option turned on, after
  3100. each such operation the tags are again aligned to `org-tags-column'."
  3101. :group 'org-tags
  3102. :type 'boolean)
  3103. (defcustom org-use-tag-inheritance t
  3104. "Non-nil means tags in levels apply also for sublevels.
  3105. When nil, only the tags directly given in a specific line apply there.
  3106. This may also be a list of tags that should be inherited, or a regexp that
  3107. matches tags that should be inherited. Additional control is possible
  3108. with the variable `org-tags-exclude-from-inheritance' which gives an
  3109. explicit list of tags to be excluded from inheritance, even if the value of
  3110. `org-use-tag-inheritance' would select it for inheritance.
  3111. If this option is t, a match early-on in a tree can lead to a large
  3112. number of matches in the subtree when constructing the agenda or creating
  3113. a sparse tree. If you only want to see the first match in a tree during
  3114. a search, check out the variable `org-tags-match-list-sublevels'."
  3115. :group 'org-tags
  3116. :type '(choice
  3117. (const :tag "Not" nil)
  3118. (const :tag "Always" t)
  3119. (repeat :tag "Specific tags" (string :tag "Tag"))
  3120. (regexp :tag "Tags matched by regexp")))
  3121. (defcustom org-tags-exclude-from-inheritance nil
  3122. "List of tags that should never be inherited.
  3123. This is a way to exclude a few tags from inheritance. For way to do
  3124. the opposite, to actively allow inheritance for selected tags,
  3125. see the variable `org-use-tag-inheritance'."
  3126. :group 'org-tags
  3127. :type '(repeat (string :tag "Tag")))
  3128. (defun org-tag-inherit-p (tag)
  3129. "Check if TAG is one that should be inherited."
  3130. (cond
  3131. ((member tag org-tags-exclude-from-inheritance) nil)
  3132. ((eq org-use-tag-inheritance t) t)
  3133. ((not org-use-tag-inheritance) nil)
  3134. ((stringp org-use-tag-inheritance)
  3135. (string-match org-use-tag-inheritance tag))
  3136. ((listp org-use-tag-inheritance)
  3137. (member tag org-use-tag-inheritance))
  3138. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  3139. (defcustom org-tags-match-list-sublevels t
  3140. "Non-nil means list also sublevels of headlines matching a search.
  3141. This variable applies to tags/property searches, and also to stuck
  3142. projects because this search is based on a tags match as well.
  3143. When set to the symbol `indented', sublevels are indented with
  3144. leading dots.
  3145. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  3146. the sublevels of a headline matching a tag search often also match
  3147. the same search. Listing all of them can create very long lists.
  3148. Setting this variable to nil causes subtrees of a match to be skipped.
  3149. This variable is semi-obsolete and probably should always be true. It
  3150. is better to limit inheritance to certain tags using the variables
  3151. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  3152. :group 'org-tags
  3153. :type '(choice
  3154. (const :tag "No, don't list them" nil)
  3155. (const :tag "Yes, do list them" t)
  3156. (const :tag "List them, indented with leading dots" indented)))
  3157. (defcustom org-tags-sort-function nil
  3158. "When set, tags are sorted using this function as a comparator."
  3159. :group 'org-tags
  3160. :type '(choice
  3161. (const :tag "No sorting" nil)
  3162. (const :tag "Alphabetical" string<)
  3163. (const :tag "Reverse alphabetical" string>)
  3164. (function :tag "Custom function" nil)))
  3165. (defvar org-tags-history nil
  3166. "History of minibuffer reads for tags.")
  3167. (defvar org-last-tags-completion-table nil
  3168. "The last used completion table for tags.")
  3169. (defvar org-after-tags-change-hook nil
  3170. "Hook that is run after the tags in a line have changed.")
  3171. (defgroup org-properties nil
  3172. "Options concerning properties in Org-mode."
  3173. :tag "Org Properties"
  3174. :group 'org)
  3175. (defcustom org-property-format "%-10s %s"
  3176. "How property key/value pairs should be formatted by `indent-line'.
  3177. When `indent-line' hits a property definition, it will format the line
  3178. according to this format, mainly to make sure that the values are
  3179. lined-up with respect to each other."
  3180. :group 'org-properties
  3181. :type 'string)
  3182. (defcustom org-properties-postprocess-alist nil
  3183. "Alist of properties and functions to adjust inserted values.
  3184. Elements of this alist must be of the form
  3185. ([string] [function])
  3186. where [string] must be a property name and [function] must be a
  3187. lambda expression: this lambda expression must take one argument,
  3188. the value to adjust, and return the new value as a string.
  3189. For example, this element will allow the property \"Remaining\"
  3190. to be updated wrt the relation between the \"Effort\" property
  3191. and the clock summary:
  3192. ((\"Remaining\" (lambda(value)
  3193. (let ((clocksum (org-clock-sum-current-item))
  3194. (effort (org-duration-string-to-minutes
  3195. (org-entry-get (point) \"Effort\"))))
  3196. (org-minutes-to-clocksum-string (- effort clocksum))))))"
  3197. :group 'org-properties
  3198. :version "24.1"
  3199. :type '(alist :key-type (string :tag "Property")
  3200. :value-type (function :tag "Function")))
  3201. (defcustom org-use-property-inheritance nil
  3202. "Non-nil means properties apply also for sublevels.
  3203. This setting is chiefly used during property searches. Turning it on can
  3204. cause significant overhead when doing a search, which is why it is not
  3205. on by default.
  3206. When nil, only the properties directly given in the current entry count.
  3207. When t, every property is inherited. The value may also be a list of
  3208. properties that should have inheritance, or a regular expression matching
  3209. properties that should be inherited.
  3210. However, note that some special properties use inheritance under special
  3211. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  3212. and the properties ending in \"_ALL\" when they are used as descriptor
  3213. for valid values of a property.
  3214. Note for programmers:
  3215. When querying an entry with `org-entry-get', you can control if inheritance
  3216. should be used. By default, `org-entry-get' looks only at the local
  3217. properties. You can request inheritance by setting the inherit argument
  3218. to t (to force inheritance) or to `selective' (to respect the setting
  3219. in this variable)."
  3220. :group 'org-properties
  3221. :type '(choice
  3222. (const :tag "Not" nil)
  3223. (const :tag "Always" t)
  3224. (repeat :tag "Specific properties" (string :tag "Property"))
  3225. (regexp :tag "Properties matched by regexp")))
  3226. (defun org-property-inherit-p (property)
  3227. "Check if PROPERTY is one that should be inherited."
  3228. (cond
  3229. ((eq org-use-property-inheritance t) t)
  3230. ((not org-use-property-inheritance) nil)
  3231. ((stringp org-use-property-inheritance)
  3232. (string-match org-use-property-inheritance property))
  3233. ((listp org-use-property-inheritance)
  3234. (member property org-use-property-inheritance))
  3235. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  3236. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  3237. "The default column format, if no other format has been defined.
  3238. This variable can be set on the per-file basis by inserting a line
  3239. #+COLUMNS: %25ITEM ....."
  3240. :group 'org-properties
  3241. :type 'string)
  3242. (defcustom org-columns-ellipses ".."
  3243. "The ellipses to be used when a field in column view is truncated.
  3244. When this is the empty string, as many characters as possible are shown,
  3245. but then there will be no visual indication that the field has been truncated.
  3246. When this is a string of length N, the last N characters of a truncated
  3247. field are replaced by this string. If the column is narrower than the
  3248. ellipses string, only part of the ellipses string will be shown."
  3249. :group 'org-properties
  3250. :type 'string)
  3251. (defconst org-global-properties-fixed
  3252. '(("VISIBILITY_ALL" . "folded children content all")
  3253. ("CLOCK_MODELINE_TOTAL_ALL" . "current today repeat all auto"))
  3254. "List of property/value pairs that can be inherited by any entry.
  3255. These are fixed values, for the preset properties. The user variable
  3256. that can be used to add to this list is `org-global-properties'.
  3257. The entries in this list are cons cells where the car is a property
  3258. name and cdr is a string with the value. If the value represents
  3259. multiple items like an \"_ALL\" property, separate the items by
  3260. spaces.")
  3261. (defcustom org-global-properties nil
  3262. "List of property/value pairs that can be inherited by any entry.
  3263. This list will be combined with the constant `org-global-properties-fixed'.
  3264. The entries in this list are cons cells where the car is a property
  3265. name and cdr is a string with the value.
  3266. You can set buffer-local values for the same purpose in the variable
  3267. `org-file-properties' this by adding lines like
  3268. #+PROPERTY: NAME VALUE"
  3269. :group 'org-properties
  3270. :type '(repeat
  3271. (cons (string :tag "Property")
  3272. (string :tag "Value"))))
  3273. (defvar-local org-file-properties nil
  3274. "List of property/value pairs that can be inherited by any entry.
  3275. Valid for the current buffer.
  3276. This variable is populated from #+PROPERTY lines.")
  3277. (defgroup org-agenda nil
  3278. "Options concerning agenda views in Org-mode."
  3279. :tag "Org Agenda"
  3280. :group 'org)
  3281. (defvar-local org-category nil
  3282. "Variable used by org files to set a category for agenda display.
  3283. Such files should use a file variable to set it, for example
  3284. # -*- mode: org; org-category: \"ELisp\"
  3285. or contain a special line
  3286. #+CATEGORY: ELisp
  3287. If the file does not specify a category, then file's base name
  3288. is used instead.")
  3289. (put 'org-category 'safe-local-variable (lambda (x) (or (symbolp x) (stringp x))))
  3290. (defcustom org-agenda-files nil
  3291. "The files to be used for agenda display.
  3292. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  3293. \\[org-remove-file]. You can also use customize to edit the list.
  3294. If an entry is a directory, all files in that directory that are matched by
  3295. `org-agenda-file-regexp' will be part of the file list.
  3296. If the value of the variable is not a list but a single file name, then
  3297. the list of agenda files is actually stored and maintained in that file, one
  3298. agenda file per line. In this file paths can be given relative to
  3299. `org-directory'. Tilde expansion and environment variable substitution
  3300. are also made."
  3301. :group 'org-agenda
  3302. :type '(choice
  3303. (repeat :tag "List of files and directories" file)
  3304. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  3305. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  3306. "Regular expression to match files for `org-agenda-files'.
  3307. If any element in the list in that variable contains a directory instead
  3308. of a normal file, all files in that directory that are matched by this
  3309. regular expression will be included."
  3310. :group 'org-agenda
  3311. :type 'regexp)
  3312. (defcustom org-agenda-text-search-extra-files nil
  3313. "List of extra files to be searched by text search commands.
  3314. These files will be searched in addition to the agenda files by the
  3315. commands `org-search-view' (`\\[org-agenda] s') \
  3316. and `org-occur-in-agenda-files'.
  3317. Note that these files will only be searched for text search commands,
  3318. not for the other agenda views like todo lists, tag searches or the weekly
  3319. agenda. This variable is intended to list notes and possibly archive files
  3320. that should also be searched by these two commands.
  3321. In fact, if the first element in the list is the symbol `agenda-archives',
  3322. then all archive files of all agenda files will be added to the search
  3323. scope."
  3324. :group 'org-agenda
  3325. :type '(set :greedy t
  3326. (const :tag "Agenda Archives" agenda-archives)
  3327. (repeat :inline t (file))))
  3328. (org-defvaralias 'org-agenda-multi-occur-extra-files
  3329. 'org-agenda-text-search-extra-files)
  3330. (defcustom org-agenda-skip-unavailable-files nil
  3331. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  3332. A nil value means to remove them, after a query, from the list."
  3333. :group 'org-agenda
  3334. :type 'boolean)
  3335. (defcustom org-calendar-to-agenda-key [?c]
  3336. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  3337. The command `org-calendar-goto-agenda' will be bound to this key. The
  3338. default is the character `c' because then `c' can be used to switch back and
  3339. forth between agenda and calendar."
  3340. :group 'org-agenda
  3341. :type 'sexp)
  3342. (defcustom org-calendar-insert-diary-entry-key [?i]
  3343. "The key to be installed in `calendar-mode-map' for adding diary entries.
  3344. This option is irrelevant until `org-agenda-diary-file' has been configured
  3345. to point to an Org-mode file. When that is the case, the command
  3346. `org-agenda-diary-entry' will be bound to the key given here, by default
  3347. `i'. In the calendar, `i' normally adds entries to `diary-file'. So
  3348. if you want to continue doing this, you need to change this to a different
  3349. key."
  3350. :group 'org-agenda
  3351. :type 'sexp)
  3352. (defcustom org-agenda-diary-file 'diary-file
  3353. "File to which to add new entries with the `i' key in agenda and calendar.
  3354. When this is the symbol `diary-file', the functionality in the Emacs
  3355. calendar will be used to add entries to the `diary-file'. But when this
  3356. points to a file, `org-agenda-diary-entry' will be used instead."
  3357. :group 'org-agenda
  3358. :type '(choice
  3359. (const :tag "The standard Emacs diary file" diary-file)
  3360. (file :tag "Special Org file diary entries")))
  3361. (eval-after-load "calendar"
  3362. '(progn
  3363. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  3364. 'org-calendar-goto-agenda)
  3365. (add-hook 'calendar-mode-hook
  3366. (lambda ()
  3367. (unless (eq org-agenda-diary-file 'diary-file)
  3368. (define-key calendar-mode-map
  3369. org-calendar-insert-diary-entry-key
  3370. 'org-agenda-diary-entry))))))
  3371. (defgroup org-latex nil
  3372. "Options for embedding LaTeX code into Org-mode."
  3373. :tag "Org LaTeX"
  3374. :group 'org)
  3375. (defcustom org-format-latex-options
  3376. '(:foreground default :background default :scale 1.0
  3377. :html-foreground "Black" :html-background "Transparent"
  3378. :html-scale 1.0 :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  3379. "Options for creating images from LaTeX fragments.
  3380. This is a property list with the following properties:
  3381. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  3382. `default' means use the foreground of the default face.
  3383. `auto' means use the foreground from the text face.
  3384. :background the background color, or \"Transparent\".
  3385. `default' means use the background of the default face.
  3386. `auto' means use the background from the text face.
  3387. :scale a scaling factor for the size of the images, to get more pixels
  3388. :html-foreground, :html-background, :html-scale
  3389. the same numbers for HTML export.
  3390. :matchers a list indicating which matchers should be used to
  3391. find LaTeX fragments. Valid members of this list are:
  3392. \"begin\" find environments
  3393. \"$1\" find single characters surrounded by $.$
  3394. \"$\" find math expressions surrounded by $...$
  3395. \"$$\" find math expressions surrounded by $$....$$
  3396. \"\\(\" find math expressions surrounded by \\(...\\)
  3397. \"\\=\\[\" find math expressions surrounded by \\=\\[...\\]"
  3398. :group 'org-latex
  3399. :type 'plist)
  3400. (defcustom org-format-latex-signal-error t
  3401. "Non-nil means signal an error when image creation of LaTeX snippets fails.
  3402. When nil, just push out a message."
  3403. :group 'org-latex
  3404. :version "24.1"
  3405. :type 'boolean)
  3406. (defcustom org-latex-to-mathml-jar-file nil
  3407. "Value of\"%j\" in `org-latex-to-mathml-convert-command'.
  3408. Use this to specify additional executable file say a jar file.
  3409. When using MathToWeb as the converter, specify the full-path to
  3410. your mathtoweb.jar file."
  3411. :group 'org-latex
  3412. :version "24.1"
  3413. :type '(choice
  3414. (const :tag "None" nil)
  3415. (file :tag "JAR file" :must-match t)))
  3416. (defcustom org-latex-to-mathml-convert-command nil
  3417. "Command to convert LaTeX fragments to MathML.
  3418. Replace format-specifiers in the command as noted below and use
  3419. `shell-command' to convert LaTeX to MathML.
  3420. %j: Executable file in fully expanded form as specified by
  3421. `org-latex-to-mathml-jar-file'.
  3422. %I: Input LaTeX file in fully expanded form.
  3423. %i: The latex fragment to be converted.
  3424. %o: Output MathML file.
  3425. This command is used by `org-create-math-formula'.
  3426. When using MathToWeb as the converter, set this option to
  3427. \"java -jar %j -unicode -force -df %o %I\".
  3428. When using LaTeXML set this option to
  3429. \"latexmlmath \"%i\" --presentationmathml=%o\"."
  3430. :group 'org-latex
  3431. :version "24.1"
  3432. :type '(choice
  3433. (const :tag "None" nil)
  3434. (string :tag "\nShell command")))
  3435. (define-obsolete-variable-alias
  3436. 'org-latex-create-formula-image-program
  3437. 'org-preview-latex-default-process "25.1")
  3438. (defcustom org-preview-latex-default-process 'dvipng
  3439. "The default process to convert LaTeX fragments to image files.
  3440. All available processes and theirs documents can be found in
  3441. `org-preview-latex-process-alist', which see."
  3442. :group 'org-latex
  3443. :version "25.2"
  3444. :package-version '(Org . "9.0")
  3445. :type 'symbol)
  3446. (defcustom org-preview-latex-process-alist
  3447. '((dvipng
  3448. :programs ("latex" "dvipng" "gs")
  3449. :description "dvi > png"
  3450. :message "you need to install programs: latex, dvipng and ghostscript."
  3451. :image-input-type "dvi"
  3452. :image-output-type "png"
  3453. :image-size-adjust (1.0 . 1.0)
  3454. :latex-compiler ("latex -interaction nonstopmode -output-directory %o %f")
  3455. :image-converter ("dvipng -fg %F -bg %B -D %D -T tight -o %b.png %f"))
  3456. (dvisvgm
  3457. :programs ("latex" "dvisvgm" "gs")
  3458. :description "dvi > svg"
  3459. :message "you need to install programs: latex, dvisvgm and ghostscript."
  3460. :use-xcolor t
  3461. :image-input-type "dvi"
  3462. :image-output-type "svg"
  3463. :image-size-adjust (1.7 . 1.5)
  3464. :latex-compiler ("latex -interaction nonstopmode -output-directory %o %f")
  3465. :image-converter ("dvisvgm %f -n -b min -c %S -o %b.svg"))
  3466. (imagemagick
  3467. :programs ("latex" "convert" "gs")
  3468. :description "pdf > png"
  3469. :message
  3470. "you need to install programs: latex, imagemagick and ghostscript."
  3471. :use-xcolor t
  3472. :image-input-type "pdf"
  3473. :image-output-type "png"
  3474. :image-size-adjust (1.0 . 1.0)
  3475. :latex-compiler ("pdflatex -interaction nonstopmode -output-directory %o %f")
  3476. :image-converter
  3477. ("convert -density %D -trim -antialias %f -quality 100 %b.png")))
  3478. "Definitions of external processes for LaTeX previewing.
  3479. Org mode can use some external commands to generate TeX snippet's images for
  3480. previewing or inserting into HTML files, e.g., \"dvipng\". This variable tells
  3481. `org-create-formula-image' how to call them.
  3482. The value is an alist with the pattern (NAME . PROPERTIES). NAME is a symbol.
  3483. PROPERTIES accepts the following attributes:
  3484. :programs list of strings, required programs.
  3485. :description string, describe the process.
  3486. :message string, message it when required programs cannot be found.
  3487. :image-input-type string, input file type of image converter (e.g., \"dvi\").
  3488. :image-output-type string, output file type of image converter (e.g., \"png\").
  3489. :use-xcolor boolean, when non-nil, LaTeX \"xcolor\" macro is used to
  3490. deal with background and foreground color of image.
  3491. Otherwise, dvipng style background and foregroud color
  3492. format are generated. You may then refer to them in
  3493. command options with \"%F\" and \"%B\".
  3494. :image-size-adjust cons of numbers, the car element is used to adjust LaTeX
  3495. image size showed in buffer and the cdr element is for
  3496. HTML file. This option is only useful for process
  3497. developers, users should use variable
  3498. `org-format-latex-options' instead.
  3499. :post-clean list of strings, files matched are to be cleaned up once
  3500. the image is generated. When nil, the files with \".dvi\",
  3501. \".xdv\", \".pdf\", \".tex\", \".aux\", \".log\", \".svg\",
  3502. \".png\", \".jpg\", \".jpeg\" or \".out\" extension will
  3503. be cleaned up.
  3504. :latex-header list of strings, the LaTeX header of the snippet file.
  3505. When nil, the fallback value is used instead, which is
  3506. controlled by `org-format-latex-header',
  3507. `org-latex-default-packages-alist' and
  3508. `org-latex-packages-alist', which see.
  3509. :latex-compiler list of LaTeX commands, as strings. Each of them is given
  3510. to the shell. Place-holders \"%t\", \"%b\" and \"%o\" are
  3511. replaced with values defined below.
  3512. :image-converter list of image converter commands strings. Each of them is
  3513. given to the shell and supports any of the following
  3514. place-holders defined below.
  3515. Place-holders used by `:image-converter' and `:latex-compiler':
  3516. %i input file name.
  3517. %b base name of input file.
  3518. %o base directory of input file.
  3519. Place-holders only used by `:image-converter':
  3520. %F foreground of image
  3521. %B background of image
  3522. %D dpi, which is used to adjust image size by some processing commands.
  3523. %S the image size scale ratio, which is used to adjust image size by some
  3524. processing commands."
  3525. :group 'org-latex
  3526. :version "25.2"
  3527. :package-version '(Org . "9.0")
  3528. :type '(alist :tag "LaTeX to image backends"
  3529. :value-type (plist)))
  3530. (define-obsolete-variable-alias
  3531. 'org-latex-preview-ltxpng-directory
  3532. 'org-preview-latex-image-directory "25.1")
  3533. (defcustom org-preview-latex-image-directory "ltximg/"
  3534. "Path to store latex preview images.
  3535. A relative path here creates many directories relative to the
  3536. processed org files paths. An absolute path puts all preview
  3537. images at the same place."
  3538. :group 'org-latex
  3539. :version "25.1"
  3540. :package-version '(Org . "9.0")
  3541. :type 'string)
  3542. (defun org-format-latex-mathml-available-p ()
  3543. "Return t if `org-latex-to-mathml-convert-command' is usable."
  3544. (save-match-data
  3545. (when (and (boundp 'org-latex-to-mathml-convert-command)
  3546. org-latex-to-mathml-convert-command)
  3547. (let ((executable (car (split-string
  3548. org-latex-to-mathml-convert-command))))
  3549. (when (executable-find executable)
  3550. (if (string-match
  3551. "%j" org-latex-to-mathml-convert-command)
  3552. (file-readable-p org-latex-to-mathml-jar-file)
  3553. t))))))
  3554. (defcustom org-format-latex-header "\\documentclass{article}
  3555. \\usepackage[usenames]{color}
  3556. \[PACKAGES]
  3557. \[DEFAULT-PACKAGES]
  3558. \\pagestyle{empty} % do not remove
  3559. % The settings below are copied from fullpage.sty
  3560. \\setlength{\\textwidth}{\\paperwidth}
  3561. \\addtolength{\\textwidth}{-3cm}
  3562. \\setlength{\\oddsidemargin}{1.5cm}
  3563. \\addtolength{\\oddsidemargin}{-2.54cm}
  3564. \\setlength{\\evensidemargin}{\\oddsidemargin}
  3565. \\setlength{\\textheight}{\\paperheight}
  3566. \\addtolength{\\textheight}{-\\headheight}
  3567. \\addtolength{\\textheight}{-\\headsep}
  3568. \\addtolength{\\textheight}{-\\footskip}
  3569. \\addtolength{\\textheight}{-3cm}
  3570. \\setlength{\\topmargin}{1.5cm}
  3571. \\addtolength{\\topmargin}{-2.54cm}"
  3572. "The document header used for processing LaTeX fragments.
  3573. It is imperative that this header make sure that no page number
  3574. appears on the page. The package defined in the variables
  3575. `org-latex-default-packages-alist' and `org-latex-packages-alist'
  3576. will either replace the placeholder \"[PACKAGES]\" in this
  3577. header, or they will be appended."
  3578. :group 'org-latex
  3579. :type 'string)
  3580. (defun org-set-packages-alist (var val)
  3581. "Set the packages alist and make sure it has 3 elements per entry."
  3582. (set var (mapcar (lambda (x)
  3583. (if (and (consp x) (= (length x) 2))
  3584. (list (car x) (nth 1 x) t)
  3585. x))
  3586. val)))
  3587. (defun org-get-packages-alist (var)
  3588. "Get the packages alist and make sure it has 3 elements per entry."
  3589. (mapcar (lambda (x)
  3590. (if (and (consp x) (= (length x) 2))
  3591. (list (car x) (nth 1 x) t)
  3592. x))
  3593. (default-value var)))
  3594. (defcustom org-latex-default-packages-alist
  3595. '(("AUTO" "inputenc" t ("pdflatex"))
  3596. ("T1" "fontenc" t ("pdflatex"))
  3597. ("" "graphicx" t)
  3598. ("" "grffile" t)
  3599. ("" "longtable" nil)
  3600. ("" "wrapfig" nil)
  3601. ("" "rotating" nil)
  3602. ("normalem" "ulem" t)
  3603. ("" "amsmath" t)
  3604. ("" "textcomp" t)
  3605. ("" "amssymb" t)
  3606. ("" "capt-of" nil)
  3607. ("" "hyperref" nil))
  3608. "Alist of default packages to be inserted in the header.
  3609. Change this only if one of the packages here causes an
  3610. incompatibility with another package you are using.
  3611. The packages in this list are needed by one part or another of
  3612. Org mode to function properly:
  3613. - inputenc, fontenc: for basic font and character selection
  3614. - graphicx: for including images
  3615. - grffile: allow periods and spaces in graphics file names
  3616. - longtable: For multipage tables
  3617. - wrapfig: for figure placement
  3618. - rotating: for sideways figures and tables
  3619. - ulem: for underline and strike-through
  3620. - amsmath: for subscript and superscript and math environments
  3621. - textcomp, amssymb: for various symbols used
  3622. for interpreting the entities in `org-entities'. You can skip
  3623. some of these packages if you don't use any of their symbols.
  3624. - capt-of: for captions outside of floats
  3625. - hyperref: for cross references
  3626. Therefore you should not modify this variable unless you know
  3627. what you are doing. The one reason to change it anyway is that
  3628. you might be loading some other package that conflicts with one
  3629. of the default packages. Each element is either a cell or
  3630. a string.
  3631. A cell is of the format
  3632. (\"options\" \"package\" SNIPPET-FLAG COMPILERS)
  3633. If SNIPPET-FLAG is non-nil, the package also needs to be included
  3634. when compiling LaTeX snippets into images for inclusion into
  3635. non-LaTeX output. COMPILERS is a list of compilers that should
  3636. include the package, see `org-latex-compiler'. If the document
  3637. compiler is not in the list, and the list is non-nil, the package
  3638. will not be inserted in the final document.
  3639. A string will be inserted as-is in the header of the document."
  3640. :group 'org-latex
  3641. :group 'org-export-latex
  3642. :set 'org-set-packages-alist
  3643. :get 'org-get-packages-alist
  3644. :version "25.1"
  3645. :package-version '(Org . "8.3")
  3646. :type '(repeat
  3647. (choice
  3648. (list :tag "options/package pair"
  3649. (string :tag "options")
  3650. (string :tag "package")
  3651. (boolean :tag "Snippet"))
  3652. (string :tag "A line of LaTeX"))))
  3653. (defcustom org-latex-packages-alist nil
  3654. "Alist of packages to be inserted in every LaTeX header.
  3655. These will be inserted after `org-latex-default-packages-alist'.
  3656. Each element is either a cell or a string.
  3657. A cell is of the format:
  3658. (\"options\" \"package\" SNIPPET-FLAG)
  3659. SNIPPET-FLAG, when non-nil, indicates that this package is also
  3660. needed when turning LaTeX snippets into images for inclusion into
  3661. non-LaTeX output.
  3662. A string will be inserted as-is in the header of the document.
  3663. Make sure that you only list packages here which:
  3664. - you want in every file;
  3665. - do not conflict with the setup in `org-format-latex-header';
  3666. - do not conflict with the default packages in
  3667. `org-latex-default-packages-alist'."
  3668. :group 'org-latex
  3669. :group 'org-export-latex
  3670. :set 'org-set-packages-alist
  3671. :get 'org-get-packages-alist
  3672. :type '(repeat
  3673. (choice
  3674. (list :tag "options/package pair"
  3675. (string :tag "options")
  3676. (string :tag "package")
  3677. (boolean :tag "Snippet"))
  3678. (string :tag "A line of LaTeX"))))
  3679. (defgroup org-appearance nil
  3680. "Settings for Org-mode appearance."
  3681. :tag "Org Appearance"
  3682. :group 'org)
  3683. (defcustom org-level-color-stars-only nil
  3684. "Non-nil means fontify only the stars in each headline.
  3685. When nil, the entire headline is fontified.
  3686. Changing it requires restart of `font-lock-mode' to become effective
  3687. also in regions already fontified."
  3688. :group 'org-appearance
  3689. :type 'boolean)
  3690. (defcustom org-hide-leading-stars nil
  3691. "Non-nil means hide the first N-1 stars in a headline.
  3692. This works by using the face `org-hide' for these stars. This
  3693. face is white for a light background, and black for a dark
  3694. background. You may have to customize the face `org-hide' to
  3695. make this work.
  3696. Changing it requires restart of `font-lock-mode' to become effective
  3697. also in regions already fontified.
  3698. You may also set this on a per-file basis by adding one of the following
  3699. lines to the buffer:
  3700. #+STARTUP: hidestars
  3701. #+STARTUP: showstars"
  3702. :group 'org-appearance
  3703. :type 'boolean)
  3704. (defcustom org-hidden-keywords nil
  3705. "List of symbols corresponding to keywords to be hidden the org buffer.
  3706. For example, a value \\='(title) for this list will make the document's title
  3707. appear in the buffer without the initial #+TITLE: keyword."
  3708. :group 'org-appearance
  3709. :version "24.1"
  3710. :type '(set (const :tag "#+AUTHOR" author)
  3711. (const :tag "#+DATE" date)
  3712. (const :tag "#+EMAIL" email)
  3713. (const :tag "#+TITLE" title)))
  3714. (defcustom org-custom-properties nil
  3715. "List of properties (as strings) with a special meaning.
  3716. The default use of these custom properties is to let the user
  3717. hide them with `org-toggle-custom-properties-visibility'."
  3718. :group 'org-properties
  3719. :group 'org-appearance
  3720. :version "24.3"
  3721. :type '(repeat (string :tag "Property Name")))
  3722. (defcustom org-fontify-done-headline nil
  3723. "Non-nil means change the face of a headline if it is marked DONE.
  3724. Normally, only the TODO/DONE keyword indicates the state of a headline.
  3725. When this is non-nil, the headline after the keyword is set to the
  3726. `org-headline-done' as an additional indication."
  3727. :group 'org-appearance
  3728. :type 'boolean)
  3729. (defcustom org-fontify-emphasized-text t
  3730. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  3731. Changing this variable requires a restart of Emacs to take effect."
  3732. :group 'org-appearance
  3733. :type 'boolean)
  3734. (defcustom org-fontify-whole-heading-line nil
  3735. "Non-nil means fontify the whole line for headings.
  3736. This is useful when setting a background color for the
  3737. org-level-* faces."
  3738. :group 'org-appearance
  3739. :type 'boolean)
  3740. (defcustom org-highlight-latex-and-related nil
  3741. "Non-nil means highlight LaTeX related syntax in the buffer.
  3742. When non nil, the value should be a list containing any of the
  3743. following symbols:
  3744. `latex' Highlight LaTeX snippets and environments.
  3745. `script' Highlight subscript and superscript.
  3746. `entities' Highlight entities."
  3747. :group 'org-appearance
  3748. :version "24.4"
  3749. :package-version '(Org . "8.0")
  3750. :type '(choice
  3751. (const :tag "No highlighting" nil)
  3752. (set :greedy t :tag "Highlight"
  3753. (const :tag "LaTeX snippets and environments" latex)
  3754. (const :tag "Subscript and superscript" script)
  3755. (const :tag "Entities" entities))))
  3756. (defcustom org-hide-emphasis-markers nil
  3757. "Non-nil mean font-lock should hide the emphasis marker characters."
  3758. :group 'org-appearance
  3759. :type 'boolean)
  3760. (defcustom org-hide-macro-markers nil
  3761. "Non-nil mean font-lock should hide the brackets marking macro calls."
  3762. :group 'org-appearance
  3763. :type 'boolean)
  3764. (defcustom org-pretty-entities nil
  3765. "Non-nil means show entities as UTF8 characters.
  3766. When nil, the \\name form remains in the buffer."
  3767. :group 'org-appearance
  3768. :version "24.1"
  3769. :type 'boolean)
  3770. (defcustom org-pretty-entities-include-sub-superscripts t
  3771. "Non-nil means, pretty entity display includes formatting sub/superscripts."
  3772. :group 'org-appearance
  3773. :version "24.1"
  3774. :type 'boolean)
  3775. (defvar org-emph-re nil
  3776. "Regular expression for matching emphasis.
  3777. After a match, the match groups contain these elements:
  3778. 0 The match of the full regular expression, including the characters
  3779. before and after the proper match
  3780. 1 The character before the proper match, or empty at beginning of line
  3781. 2 The proper match, including the leading and trailing markers
  3782. 3 The leading marker like * or /, indicating the type of highlighting
  3783. 4 The text between the emphasis markers, not including the markers
  3784. 5 The character after the match, empty at the end of a line")
  3785. (defvar org-verbatim-re nil
  3786. "Regular expression for matching verbatim text.")
  3787. (defvar org-emphasis-regexp-components) ; defined just below
  3788. (defvar org-emphasis-alist) ; defined just below
  3789. (defun org-set-emph-re (var val)
  3790. "Set variable and compute the emphasis regular expression."
  3791. (set var val)
  3792. (when (and (boundp 'org-emphasis-alist)
  3793. (boundp 'org-emphasis-regexp-components)
  3794. org-emphasis-alist org-emphasis-regexp-components)
  3795. (let* ((e org-emphasis-regexp-components)
  3796. (pre (car e))
  3797. (post (nth 1 e))
  3798. (border (nth 2 e))
  3799. (body (nth 3 e))
  3800. (nl (nth 4 e))
  3801. (body1 (concat body "*?"))
  3802. (markers (mapconcat 'car org-emphasis-alist ""))
  3803. (vmarkers (mapconcat
  3804. (lambda (x) (if (eq (nth 2 x) 'verbatim) (car x) ""))
  3805. org-emphasis-alist "")))
  3806. ;; make sure special characters appear at the right position in the class
  3807. (if (string-match "\\^" markers)
  3808. (setq markers (concat (replace-match "" t t markers) "^")))
  3809. (if (string-match "-" markers)
  3810. (setq markers (concat (replace-match "" t t markers) "-")))
  3811. (if (string-match "\\^" vmarkers)
  3812. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  3813. (if (string-match "-" vmarkers)
  3814. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  3815. (if (> nl 0)
  3816. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  3817. (int-to-string nl) "\\}")))
  3818. ;; Make the regexp
  3819. (setq org-emph-re
  3820. (concat "\\([" pre "]\\|^\\)"
  3821. "\\("
  3822. "\\([" markers "]\\)"
  3823. "\\("
  3824. "[^" border "]\\|"
  3825. "[^" border "]"
  3826. body1
  3827. "[^" border "]"
  3828. "\\)"
  3829. "\\3\\)"
  3830. "\\([" post "]\\|$\\)"))
  3831. (setq org-verbatim-re
  3832. (concat "\\([" pre "]\\|^\\)"
  3833. "\\("
  3834. "\\([" vmarkers "]\\)"
  3835. "\\("
  3836. "[^" border "]\\|"
  3837. "[^" border "]"
  3838. body1
  3839. "[^" border "]"
  3840. "\\)"
  3841. "\\3\\)"
  3842. "\\([" post "]\\|$\\)")))))
  3843. ;; This used to be a defcustom (Org <8.0) but allowing the users to
  3844. ;; set this option proved cumbersome. See this message/thread:
  3845. ;; http://article.gmane.org/gmane.emacs.orgmode/68681
  3846. (defvar org-emphasis-regexp-components
  3847. '(" \t('\"{" "- \t.,:!?;'\")}\\[" " \t\r\n" "." 1)
  3848. "Components used to build the regular expression for emphasis.
  3849. This is a list with five entries. Terminology: In an emphasis string
  3850. like \" *strong word* \", we call the initial space PREMATCH, the final
  3851. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  3852. and \"trong wor\" is the body. The different components in this variable
  3853. specify what is allowed/forbidden in each part:
  3854. pre Chars allowed as prematch. Beginning of line will be allowed too.
  3855. post Chars allowed as postmatch. End of line will be allowed too.
  3856. border The chars *forbidden* as border characters.
  3857. body-regexp A regexp like \".\" to match a body character. Don't use
  3858. non-shy groups here, and don't allow newline here.
  3859. newline The maximum number of newlines allowed in an emphasis exp.
  3860. You need to reload Org or to restart Emacs after customizing this.")
  3861. (defcustom org-emphasis-alist
  3862. `(("*" bold)
  3863. ("/" italic)
  3864. ("_" underline)
  3865. ("=" org-verbatim verbatim)
  3866. ("~" org-code verbatim)
  3867. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))))
  3868. "Alist of characters and faces to emphasize text.
  3869. Text starting and ending with a special character will be emphasized,
  3870. for example *bold*, _underlined_ and /italic/. This variable sets the
  3871. marker characters and the face to be used by font-lock for highlighting
  3872. in Org-mode Emacs buffers.
  3873. You need to reload Org or to restart Emacs after customizing this."
  3874. :group 'org-appearance
  3875. :set 'org-set-emph-re
  3876. :version "24.4"
  3877. :package-version '(Org . "8.0")
  3878. :type '(repeat
  3879. (list
  3880. (string :tag "Marker character")
  3881. (choice
  3882. (face :tag "Font-lock-face")
  3883. (plist :tag "Face property list"))
  3884. (option (const verbatim)))))
  3885. (defvar org-protecting-blocks '("src" "example" "export")
  3886. "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
  3887. This is needed for font-lock setup.")
  3888. ;;; Functions and variables from their packages
  3889. ;; Declared here to avoid compiler warnings
  3890. ;; XEmacs only
  3891. (defvar outline-mode-menu-heading)
  3892. (defvar outline-mode-menu-show)
  3893. (defvar outline-mode-menu-hide)
  3894. (defvar zmacs-regions) ; XEmacs regions
  3895. ;; Emacs only
  3896. (defvar mark-active)
  3897. ;; Various packages
  3898. (declare-function calc-eval "calc" (str &optional separator &rest args))
  3899. (declare-function calendar-forward-day "cal-move" (arg))
  3900. (declare-function calendar-goto-date "cal-move" (date))
  3901. (declare-function calendar-goto-today "cal-move" ())
  3902. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  3903. (declare-function calendar-iso-to-absolute "cal-iso" (date))
  3904. (declare-function cdlatex-compute-tables "ext:cdlatex" ())
  3905. (declare-function cdlatex-tab "ext:cdlatex" ())
  3906. (declare-function dired-get-filename
  3907. "dired"
  3908. (&optional localp no-error-if-not-filep))
  3909. (declare-function iswitchb-read-buffer
  3910. "iswitchb"
  3911. (prompt &optional
  3912. default require-match _predicate start matches-set))
  3913. (declare-function org-agenda-change-all-lines
  3914. "org-agenda"
  3915. (newhead hdmarker &optional fixface just-this))
  3916. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  3917. "org-agenda"
  3918. (&optional end))
  3919. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  3920. (declare-function org-agenda-format-item
  3921. "org-agenda"
  3922. (extra txt &optional level category tags dotime
  3923. remove-re habitp))
  3924. (declare-function org-agenda-maybe-redo "org-agenda" ())
  3925. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  3926. (declare-function org-agenda-save-markers-for-cut-and-paste
  3927. "org-agenda"
  3928. (beg end))
  3929. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  3930. (declare-function org-agenda-skip "org-agenda" ())
  3931. (declare-function org-attach-reveal "org-attach" (&optional if-exists))
  3932. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  3933. (declare-function org-indent-mode "org-indent" (&optional arg))
  3934. (declare-function org-inlinetask-goto-beginning "org-inlinetask" ())
  3935. (declare-function org-inlinetask-goto-end "org-inlinetask" ())
  3936. (declare-function org-inlinetask-in-task-p "org-inlinetask" ())
  3937. (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
  3938. (declare-function orgtbl-send-table "org-table" (&optional maybe))
  3939. (declare-function parse-time-string "parse-time" (string))
  3940. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  3941. (declare-function table--at-cell-p
  3942. "table"
  3943. (position &optional object at-column))
  3944. (defvar align-mode-rules-list)
  3945. (defvar calc-embedded-close-formula)
  3946. (defvar calc-embedded-open-formula)
  3947. (defvar calc-embedded-open-mode)
  3948. (defvar font-lock-unfontify-region-function)
  3949. (defvar iswitchb-temp-buflist)
  3950. (defvar org-agenda-tags-todo-honor-ignore-options)
  3951. (defvar remember-data-file)
  3952. (defvar texmathp-why)
  3953. ;;;###autoload
  3954. (defun turn-on-orgtbl ()
  3955. "Unconditionally turn on `orgtbl-mode'."
  3956. (require 'org-table)
  3957. (orgtbl-mode 1))
  3958. (defun org-at-table-p (&optional table-type)
  3959. "Non-nil if the cursor is inside an Org table.
  3960. If TABLE-TYPE is non-nil, also check for table.el-type tables.
  3961. If `org-enable-table-editor' is nil, return nil unconditionally."
  3962. (and
  3963. org-enable-table-editor
  3964. (save-excursion
  3965. (beginning-of-line)
  3966. (org-looking-at-p (if table-type "[ \t]*[|+]" "[ \t]*|")))
  3967. (or (not (derived-mode-p 'org-mode))
  3968. (let ((e (org-element-lineage (org-element-at-point) '(table) t)))
  3969. (and e (or table-type (eq (org-element-property :type e) 'org)))))))
  3970. (define-obsolete-function-alias 'org-table-p 'org-at-table-p "Org 9.0")
  3971. (defun org-at-table.el-p ()
  3972. "Non-nil when point is at a table.el table."
  3973. (and (save-excursion (beginning-of-line) (looking-at "[ \t]*[|+]"))
  3974. (let ((element (org-element-at-point)))
  3975. (and (eq (org-element-type element) 'table)
  3976. (eq (org-element-property :type element) 'table.el)))))
  3977. (defun org-table-recognize-table.el ()
  3978. "If there is a table.el table nearby, recognize it and move into it."
  3979. (when (and org-table-tab-recognizes-table.el (org-at-table.el-p))
  3980. (beginning-of-line)
  3981. (unless (or (looking-at org-table-dataline-regexp)
  3982. (not (looking-at org-table1-hline-regexp)))
  3983. (forward-line)
  3984. (when (looking-at org-table-any-border-regexp)
  3985. (forward-line -2)))
  3986. (if (re-search-forward "|" (org-table-end t) t)
  3987. (progn
  3988. (require 'table)
  3989. (if (table--at-cell-p (point)) t
  3990. (message "recognizing table.el table...")
  3991. (table-recognize-table)
  3992. (message "recognizing table.el table...done")))
  3993. (error "This should not happen"))))
  3994. ;;; This function is not used by org core since commit 6d1e3082, Feb 2010
  3995. (make-obsolete 'org-table-recognize-table.el
  3996. "please notify the org mailing list if you use this function."
  3997. "Org 9.0")
  3998. (defun org-at-table-hline-p ()
  3999. "Non-nil when point is inside a hline in a table.
  4000. Assume point is already in a table. If `org-enable-table-editor'
  4001. is nil, return nil unconditionally."
  4002. (and org-enable-table-editor
  4003. (save-excursion
  4004. (beginning-of-line)
  4005. (looking-at org-table-hline-regexp))))
  4006. (defun org-table-map-tables (function &optional quietly)
  4007. "Apply FUNCTION to the start of all tables in the buffer."
  4008. (save-excursion
  4009. (save-restriction
  4010. (widen)
  4011. (goto-char (point-min))
  4012. (while (re-search-forward org-table-any-line-regexp nil t)
  4013. (unless quietly
  4014. (message "Mapping tables: %d%%"
  4015. (floor (* 100.0 (point)) (buffer-size))))
  4016. (beginning-of-line 1)
  4017. (when (and (looking-at org-table-line-regexp)
  4018. ;; Exclude tables in src/example/verbatim/clocktable blocks
  4019. (not (org-in-block-p '("src" "example" "verbatim" "clocktable"))))
  4020. (save-excursion (funcall function))
  4021. (or (looking-at org-table-line-regexp)
  4022. (forward-char 1)))
  4023. (re-search-forward org-table-any-border-regexp nil 1))))
  4024. (unless quietly (message "Mapping tables: done")))
  4025. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock" (beg end))
  4026. (declare-function org-clock-update-mode-line "org-clock" ())
  4027. (declare-function org-resolve-clocks "org-clock"
  4028. (&optional also-non-dangling-p prompt last-valid))
  4029. (defun org-at-TBLFM-p (&optional pos)
  4030. "Non-nil when point (or POS) is in #+TBLFM line."
  4031. (save-excursion
  4032. (goto-char (or pos (point)))
  4033. (beginning-of-line)
  4034. (and (let ((case-fold-search t)) (looking-at org-TBLFM-regexp))
  4035. (eq (org-element-type (org-element-at-point)) 'table))))
  4036. (defvar org-clock-start-time)
  4037. (defvar org-clock-marker (make-marker)
  4038. "Marker recording the last clock-in.")
  4039. (defvar org-clock-hd-marker (make-marker)
  4040. "Marker recording the last clock-in, but the headline position.")
  4041. (defvar org-clock-heading ""
  4042. "The heading of the current clock entry.")
  4043. (defun org-clock-is-active ()
  4044. "Return the buffer where the clock is currently running.
  4045. Return nil if no clock is running."
  4046. (marker-buffer org-clock-marker))
  4047. (defun org-check-running-clock ()
  4048. "Check if the current buffer contains the running clock.
  4049. If yes, offer to stop it and to save the buffer with the changes."
  4050. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  4051. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  4052. (buffer-name))))
  4053. (org-clock-out)
  4054. (when (y-or-n-p "Save changed buffer?")
  4055. (save-buffer))))
  4056. (defun org-clocktable-try-shift (dir n)
  4057. "Check if this line starts a clock table, if yes, shift the time block."
  4058. (when (org-match-line "^[ \t]*#\\+BEGIN:[ \t]+clocktable\\>")
  4059. (org-clocktable-shift dir n)))
  4060. ;;;###autoload
  4061. (defun org-clock-persistence-insinuate ()
  4062. "Set up hooks for clock persistence."
  4063. (require 'org-clock)
  4064. (add-hook 'org-mode-hook 'org-clock-load)
  4065. (add-hook 'kill-emacs-hook 'org-clock-save))
  4066. (defgroup org-archive nil
  4067. "Options concerning archiving in Org-mode."
  4068. :tag "Org Archive"
  4069. :group 'org-structure)
  4070. (defcustom org-archive-location "%s_archive::"
  4071. "The location where subtrees should be archived.
  4072. The value of this variable is a string, consisting of two parts,
  4073. separated by a double-colon. The first part is a filename and
  4074. the second part is a headline.
  4075. When the filename is omitted, archiving happens in the same file.
  4076. %s in the filename will be replaced by the current file
  4077. name (without the directory part). Archiving to a different file
  4078. is useful to keep archived entries from contributing to the
  4079. Org-mode Agenda.
  4080. The archived entries will be filed as subtrees of the specified
  4081. headline. When the headline is omitted, the subtrees are simply
  4082. filed away at the end of the file, as top-level entries. Also in
  4083. the heading you can use %s to represent the file name, this can be
  4084. useful when using the same archive for a number of different files.
  4085. Here are a few examples:
  4086. \"%s_archive::\"
  4087. If the current file is Projects.org, archive in file
  4088. Projects.org_archive, as top-level trees. This is the default.
  4089. \"::* Archived Tasks\"
  4090. Archive in the current file, under the top-level headline
  4091. \"* Archived Tasks\".
  4092. \"~/org/archive.org::\"
  4093. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  4094. \"~/org/archive.org::* From %s\"
  4095. Archive in file ~/org/archive.org (absolute path), under headlines
  4096. \"From FILENAME\" where file name is the current file name.
  4097. \"~/org/datetree.org::datetree/* Finished Tasks\"
  4098. The \"datetree/\" string is special, signifying to archive
  4099. items to the datetree. Items are placed in either the CLOSED
  4100. date of the item, or the current date if there is no CLOSED date.
  4101. The heading will be a subentry to the current date. There doesn't
  4102. need to be a heading, but there always needs to be a slash after
  4103. datetree. For example, to store archived items directly in the
  4104. datetree, use \"~/org/datetree.org::datetree/\".
  4105. \"basement::** Finished Tasks\"
  4106. Archive in file ./basement (relative path), as level 3 trees
  4107. below the level 2 heading \"** Finished Tasks\".
  4108. You may set this option on a per-file basis by adding to the buffer a
  4109. line like
  4110. #+ARCHIVE: basement::** Finished Tasks
  4111. You may also define it locally for a subtree by setting an ARCHIVE property
  4112. in the entry. If such a property is found in an entry, or anywhere up
  4113. the hierarchy, it will be used."
  4114. :group 'org-archive
  4115. :type 'string)
  4116. (defcustom org-agenda-skip-archived-trees t
  4117. "Non-nil means the agenda will skip any items located in archived trees.
  4118. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  4119. variable is no longer recommended, you should leave it at the value t.
  4120. Instead, use the key `v' to cycle the archives-mode in the agenda."
  4121. :group 'org-archive
  4122. :group 'org-agenda-skip
  4123. :type 'boolean)
  4124. (defcustom org-columns-skip-archived-trees t
  4125. "Non-nil means ignore archived trees when creating column view."
  4126. :group 'org-archive
  4127. :group 'org-properties
  4128. :type 'boolean)
  4129. (defcustom org-cycle-open-archived-trees nil
  4130. "Non-nil means `org-cycle' will open archived trees.
  4131. An archived tree is a tree marked with the tag ARCHIVE.
  4132. When nil, archived trees will stay folded. You can still open them with
  4133. normal outline commands like `show-all', but not with the cycling commands."
  4134. :group 'org-archive
  4135. :group 'org-cycle
  4136. :type 'boolean)
  4137. (defcustom org-sparse-tree-open-archived-trees nil
  4138. "Non-nil means sparse tree construction shows matches in archived trees.
  4139. When nil, matches in these trees are highlighted, but the trees are kept in
  4140. collapsed state."
  4141. :group 'org-archive
  4142. :group 'org-sparse-trees
  4143. :type 'boolean)
  4144. (defcustom org-sparse-tree-default-date-type nil
  4145. "The default date type when building a sparse tree.
  4146. When this is nil, a date is a scheduled or a deadline timestamp.
  4147. Otherwise, these types are allowed:
  4148. all: all timestamps
  4149. active: only active timestamps (<...>)
  4150. inactive: only inactive timestamps ([...])
  4151. scheduled: only scheduled timestamps
  4152. deadline: only deadline timestamps"
  4153. :type '(choice (const :tag "Scheduled or deadline" nil)
  4154. (const :tag "All timestamps" all)
  4155. (const :tag "Only active timestamps" active)
  4156. (const :tag "Only inactive timestamps" inactive)
  4157. (const :tag "Only scheduled timestamps" scheduled)
  4158. (const :tag "Only deadline timestamps" deadline)
  4159. (const :tag "Only closed timestamps" closed))
  4160. :version "25.1"
  4161. :package-version '(Org . "8.3")
  4162. :group 'org-sparse-trees)
  4163. (defun org-cycle-hide-archived-subtrees (state)
  4164. "Re-hide all archived subtrees after a visibility state change."
  4165. (when (and (not org-cycle-open-archived-trees)
  4166. (not (memq state '(overview folded))))
  4167. (save-excursion
  4168. (let* ((globalp (memq state '(contents all)))
  4169. (beg (if globalp (point-min) (point)))
  4170. (end (if globalp (point-max) (org-end-of-subtree t))))
  4171. (org-hide-archived-subtrees beg end)
  4172. (goto-char beg)
  4173. (when (looking-at-p (concat ".*:" org-archive-tag ":"))
  4174. (message "%s" (substitute-command-keys
  4175. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  4176. (defun org-force-cycle-archived ()
  4177. "Cycle subtree even if it is archived."
  4178. (interactive)
  4179. (setq this-command 'org-cycle)
  4180. (let ((org-cycle-open-archived-trees t))
  4181. (call-interactively 'org-cycle)))
  4182. (defun org-hide-archived-subtrees (beg end)
  4183. "Re-hide all archived subtrees after a visibility state change."
  4184. (org-with-wide-buffer
  4185. (let ((case-fold-search nil)
  4186. (re (concat org-outline-regexp-bol ".*:" org-archive-tag ":")))
  4187. (goto-char beg)
  4188. ;; Include headline point is currently on.
  4189. (beginning-of-line)
  4190. (while (and (< (point) end) (re-search-forward re end t))
  4191. (when (member org-archive-tag (org-get-tags))
  4192. (org-flag-subtree t)
  4193. (org-end-of-subtree t))))))
  4194. (declare-function outline-end-of-heading "outline" ())
  4195. (declare-function outline-flag-region "outline" (from to flag))
  4196. (defun org-flag-subtree (flag)
  4197. (save-excursion
  4198. (org-back-to-heading t)
  4199. (outline-end-of-heading)
  4200. (outline-flag-region (point)
  4201. (progn (org-end-of-subtree t) (point))
  4202. flag)))
  4203. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  4204. ;; Declare Column View Code
  4205. (declare-function org-columns-get-format-and-top-level "org-colview" ())
  4206. (declare-function org-columns-compute "org-colview" (property))
  4207. ;; Declare ID code
  4208. (declare-function org-id-store-link "org-id")
  4209. (declare-function org-id-locations-load "org-id")
  4210. (declare-function org-id-locations-save "org-id")
  4211. (defvar org-id-track-globally)
  4212. ;;; Variables for pre-computed regular expressions, all buffer local
  4213. (defvar-local org-todo-regexp nil
  4214. "Matches any of the TODO state keywords.")
  4215. (defvar-local org-not-done-regexp nil
  4216. "Matches any of the TODO state keywords except the last one.")
  4217. (defvar-local org-not-done-heading-regexp nil
  4218. "Matches a TODO headline that is not done.")
  4219. (defvar-local org-todo-line-regexp nil
  4220. "Matches a headline and puts TODO state into group 2 if present.")
  4221. (defvar-local org-complex-heading-regexp nil
  4222. "Matches a headline and puts everything into groups:
  4223. group 1: the stars
  4224. group 2: The todo keyword, maybe
  4225. group 3: Priority cookie
  4226. group 4: True headline
  4227. group 5: Tags")
  4228. (defvar-local org-complex-heading-regexp-format nil
  4229. "Printf format to make regexp to match an exact headline.
  4230. This regexp will match the headline of any node which has the
  4231. exact headline text that is put into the format, but may have any
  4232. TODO state, priority and tags.")
  4233. (defvar-local org-todo-line-tags-regexp nil
  4234. "Matches a headline and puts TODO state into group 2 if present.
  4235. Also put tags into group 4 if tags are present.")
  4236. (defconst org-plain-time-of-day-regexp
  4237. (concat
  4238. "\\(\\<[012]?[0-9]"
  4239. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4240. "\\(--?"
  4241. "\\(\\<[012]?[0-9]"
  4242. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4243. "\\)?")
  4244. "Regular expression to match a plain time or time range.
  4245. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  4246. groups carry important information:
  4247. 0 the full match
  4248. 1 the first time, range or not
  4249. 8 the second time, if it is a range.")
  4250. (defconst org-plain-time-extension-regexp
  4251. (concat
  4252. "\\(\\<[012]?[0-9]"
  4253. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4254. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  4255. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  4256. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  4257. groups carry important information:
  4258. 0 the full match
  4259. 7 hours of duration
  4260. 9 minutes of duration")
  4261. (defconst org-stamp-time-of-day-regexp
  4262. (concat
  4263. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  4264. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  4265. "\\(--?"
  4266. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  4267. "Regular expression to match a timestamp time or time range.
  4268. After a match, the following groups carry important information:
  4269. 0 the full match
  4270. 1 date plus weekday, for back referencing to make sure both times are on the same day
  4271. 2 the first time, range or not
  4272. 4 the second time, if it is a range.")
  4273. (defconst org-startup-options
  4274. '(("fold" org-startup-folded t)
  4275. ("overview" org-startup-folded t)
  4276. ("nofold" org-startup-folded nil)
  4277. ("showall" org-startup-folded nil)
  4278. ("showeverything" org-startup-folded showeverything)
  4279. ("content" org-startup-folded content)
  4280. ("indent" org-startup-indented t)
  4281. ("noindent" org-startup-indented nil)
  4282. ("hidestars" org-hide-leading-stars t)
  4283. ("showstars" org-hide-leading-stars nil)
  4284. ("odd" org-odd-levels-only t)
  4285. ("oddeven" org-odd-levels-only nil)
  4286. ("align" org-startup-align-all-tables t)
  4287. ("noalign" org-startup-align-all-tables nil)
  4288. ("inlineimages" org-startup-with-inline-images t)
  4289. ("noinlineimages" org-startup-with-inline-images nil)
  4290. ("latexpreview" org-startup-with-latex-preview t)
  4291. ("nolatexpreview" org-startup-with-latex-preview nil)
  4292. ("customtime" org-display-custom-times t)
  4293. ("logdone" org-log-done time)
  4294. ("lognotedone" org-log-done note)
  4295. ("nologdone" org-log-done nil)
  4296. ("lognoteclock-out" org-log-note-clock-out t)
  4297. ("nolognoteclock-out" org-log-note-clock-out nil)
  4298. ("logrepeat" org-log-repeat state)
  4299. ("lognoterepeat" org-log-repeat note)
  4300. ("logdrawer" org-log-into-drawer t)
  4301. ("nologdrawer" org-log-into-drawer nil)
  4302. ("logstatesreversed" org-log-states-order-reversed t)
  4303. ("nologstatesreversed" org-log-states-order-reversed nil)
  4304. ("nologrepeat" org-log-repeat nil)
  4305. ("logreschedule" org-log-reschedule time)
  4306. ("lognotereschedule" org-log-reschedule note)
  4307. ("nologreschedule" org-log-reschedule nil)
  4308. ("logredeadline" org-log-redeadline time)
  4309. ("lognoteredeadline" org-log-redeadline note)
  4310. ("nologredeadline" org-log-redeadline nil)
  4311. ("logrefile" org-log-refile time)
  4312. ("lognoterefile" org-log-refile note)
  4313. ("nologrefile" org-log-refile nil)
  4314. ("fninline" org-footnote-define-inline t)
  4315. ("nofninline" org-footnote-define-inline nil)
  4316. ("fnlocal" org-footnote-section nil)
  4317. ("fnauto" org-footnote-auto-label t)
  4318. ("fnprompt" org-footnote-auto-label nil)
  4319. ("fnconfirm" org-footnote-auto-label confirm)
  4320. ("fnplain" org-footnote-auto-label plain)
  4321. ("fnadjust" org-footnote-auto-adjust t)
  4322. ("nofnadjust" org-footnote-auto-adjust nil)
  4323. ("constcgs" constants-unit-system cgs)
  4324. ("constSI" constants-unit-system SI)
  4325. ("noptag" org-tag-persistent-alist nil)
  4326. ("hideblocks" org-hide-block-startup t)
  4327. ("nohideblocks" org-hide-block-startup nil)
  4328. ("beamer" org-startup-with-beamer-mode t)
  4329. ("entitiespretty" org-pretty-entities t)
  4330. ("entitiesplain" org-pretty-entities nil))
  4331. "Variable associated with STARTUP options for org-mode.
  4332. Each element is a list of three items: the startup options (as written
  4333. in the #+STARTUP line), the corresponding variable, and the value to set
  4334. this variable to if the option is found. An optional forth element PUSH
  4335. means to push this value onto the list in the variable.")
  4336. (defcustom org-group-tags t
  4337. "When non-nil (the default), use group tags.
  4338. This can be turned on/off through `org-toggle-tags-groups'."
  4339. :group 'org-tags
  4340. :group 'org-startup
  4341. :type 'boolean)
  4342. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  4343. (defun org-toggle-tags-groups ()
  4344. "Toggle support for group tags.
  4345. Support for group tags is controlled by the option
  4346. `org-group-tags', which is non-nil by default."
  4347. (interactive)
  4348. (setq org-group-tags (not org-group-tags))
  4349. (cond ((and (derived-mode-p 'org-agenda-mode)
  4350. org-group-tags)
  4351. (org-agenda-redo))
  4352. ((derived-mode-p 'org-mode)
  4353. (let ((org-inhibit-startup t)) (org-mode))))
  4354. (message "Groups tags support has been turned %s"
  4355. (if org-group-tags "on" "off")))
  4356. (defun org-set-regexps-and-options (&optional tags-only)
  4357. "Precompute regular expressions used in the current buffer.
  4358. When optional argument TAGS-ONLY is non-nil, only compute tags
  4359. related expressions."
  4360. (when (derived-mode-p 'org-mode)
  4361. (let ((alist (org--setup-collect-keywords
  4362. (org-make-options-regexp
  4363. (append '("FILETAGS" "TAGS" "SETUPFILE")
  4364. (and (not tags-only)
  4365. '("ARCHIVE" "CATEGORY" "COLUMNS" "CONSTANTS"
  4366. "LINK" "OPTIONS" "PRIORITIES" "PROPERTY"
  4367. "SEQ_TODO" "STARTUP" "TODO" "TYP_TODO")))))))
  4368. ;; Startup options. Get this early since it does change
  4369. ;; behavior for other options (e.g., tags).
  4370. (let ((startup (cdr (assq 'startup alist))))
  4371. (dolist (option startup)
  4372. (let ((entry (assoc-string option org-startup-options t)))
  4373. (when entry
  4374. (let ((var (nth 1 entry))
  4375. (val (nth 2 entry)))
  4376. (if (not (nth 3 entry)) (set (make-local-variable var) val)
  4377. (unless (listp (symbol-value var))
  4378. (set (make-local-variable var) nil))
  4379. (add-to-list var val)))))))
  4380. (setq-local org-file-tags
  4381. (mapcar #'org-add-prop-inherited
  4382. (cdr (assq 'filetags alist))))
  4383. (setq org-current-tag-alist
  4384. (append org-tag-persistent-alist
  4385. (let ((tags (cdr (assq 'tags alist))))
  4386. (if tags (org-tag-string-to-alist tags)
  4387. org-tag-alist))))
  4388. (setq org-tag-groups-alist
  4389. (org-tag-alist-to-groups org-current-tag-alist))
  4390. (unless tags-only
  4391. ;; File properties.
  4392. (setq-local org-file-properties (cdr (assq 'property alist)))
  4393. ;; Archive location.
  4394. (let ((archive (cdr (assq 'archive alist))))
  4395. (when archive (setq-local org-archive-location archive)))
  4396. ;; Category.
  4397. (let ((cat (org-string-nw-p (cdr (assq 'category alist)))))
  4398. (when cat
  4399. (setq-local org-category (intern cat))
  4400. (setq-local org-file-properties
  4401. (org--update-property-plist
  4402. "CATEGORY" cat org-file-properties))))
  4403. ;; Columns.
  4404. (let ((column (cdr (assq 'columns alist))))
  4405. (when column (setq-local org-columns-default-format column)))
  4406. ;; Constants.
  4407. (setq org-table-formula-constants-local (cdr (assq 'constants alist)))
  4408. ;; Link abbreviations.
  4409. (let ((links (cdr (assq 'link alist))))
  4410. (when links (setq org-link-abbrev-alist-local (nreverse links))))
  4411. ;; Priorities.
  4412. (let ((priorities (cdr (assq 'priorities alist))))
  4413. (when priorities
  4414. (setq-local org-highest-priority (nth 0 priorities))
  4415. (setq-local org-lowest-priority (nth 1 priorities))
  4416. (setq-local org-default-priority (nth 2 priorities))))
  4417. ;; Scripts.
  4418. (let ((scripts (assq 'scripts alist)))
  4419. (when scripts
  4420. (setq-local org-use-sub-superscripts (cdr scripts))))
  4421. ;; TODO keywords.
  4422. (setq-local org-todo-kwd-alist nil)
  4423. (setq-local org-todo-key-alist nil)
  4424. (setq-local org-todo-key-trigger nil)
  4425. (setq-local org-todo-keywords-1 nil)
  4426. (setq-local org-done-keywords nil)
  4427. (setq-local org-todo-heads nil)
  4428. (setq-local org-todo-sets nil)
  4429. (setq-local org-todo-log-states nil)
  4430. (let ((todo-sequences
  4431. (or (nreverse (cdr (assq 'todo alist)))
  4432. (let ((d (default-value 'org-todo-keywords)))
  4433. (if (not (stringp (car d))) d
  4434. ;; XXX: Backward compatibility code.
  4435. (list (cons org-todo-interpretation d)))))))
  4436. (dolist (sequence todo-sequences)
  4437. (let* ((sequence (or (run-hook-with-args-until-success
  4438. 'org-todo-setup-filter-hook sequence)
  4439. sequence))
  4440. (sequence-type (car sequence))
  4441. (keywords (cdr sequence))
  4442. (sep (member "|" keywords))
  4443. names alist)
  4444. (dolist (k (remove "|" keywords))
  4445. (unless (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$"
  4446. k)
  4447. (error "Invalid TODO keyword %s" k))
  4448. (let ((name (match-string 1 k))
  4449. (key (match-string 2 k))
  4450. (log (org-extract-log-state-settings k)))
  4451. (push name names)
  4452. (push (cons name (and key (string-to-char key))) alist)
  4453. (when log (push log org-todo-log-states))))
  4454. (let* ((names (nreverse names))
  4455. (done (if sep (org-remove-keyword-keys (cdr sep))
  4456. (last names)))
  4457. (head (car names))
  4458. (tail (list sequence-type head (car done) (org-last done))))
  4459. (add-to-list 'org-todo-heads head 'append)
  4460. (push names org-todo-sets)
  4461. (setq org-done-keywords (append org-done-keywords done nil))
  4462. (setq org-todo-keywords-1 (append org-todo-keywords-1 names nil))
  4463. (setq org-todo-key-alist
  4464. (append org-todo-key-alist
  4465. (and alist
  4466. (append '((:startgroup))
  4467. (nreverse alist)
  4468. '((:endgroup))))))
  4469. (dolist (k names) (push (cons k tail) org-todo-kwd-alist))))))
  4470. (setq org-todo-sets (nreverse org-todo-sets)
  4471. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  4472. org-todo-key-trigger (delq nil (mapcar #'cdr org-todo-key-alist))
  4473. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist))
  4474. ;; Compute the regular expressions and other local variables.
  4475. ;; Using `org-outline-regexp-bol' would complicate them much,
  4476. ;; because of the fixed white space at the end of that string.
  4477. (unless org-done-keywords
  4478. (setq org-done-keywords
  4479. (and org-todo-keywords-1 (last org-todo-keywords-1))))
  4480. (setq org-not-done-keywords
  4481. (org-delete-all org-done-keywords
  4482. (copy-sequence org-todo-keywords-1))
  4483. org-todo-regexp (regexp-opt org-todo-keywords-1 t)
  4484. org-not-done-regexp (regexp-opt org-not-done-keywords t)
  4485. org-not-done-heading-regexp
  4486. (format org-heading-keyword-regexp-format org-not-done-regexp)
  4487. org-todo-line-regexp
  4488. (format org-heading-keyword-maybe-regexp-format org-todo-regexp)
  4489. org-complex-heading-regexp
  4490. (concat "^\\(\\*+\\)"
  4491. "\\(?: +" org-todo-regexp "\\)?"
  4492. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4493. "\\(?: +\\(.*?\\)\\)??"
  4494. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?")
  4495. "[ \t]*$")
  4496. org-complex-heading-regexp-format
  4497. (concat "^\\(\\*+\\)"
  4498. "\\(?: +" org-todo-regexp "\\)?"
  4499. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4500. "\\(?: +"
  4501. ;; Stats cookies can be stuck to body.
  4502. "\\(?:\\[[0-9%%/]+\\] *\\)*"
  4503. "\\(%s\\)"
  4504. "\\(?: *\\[[0-9%%/]+\\]\\)*"
  4505. "\\)"
  4506. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%%:]+:\\)\\)?")
  4507. "[ \t]*$")
  4508. org-todo-line-tags-regexp
  4509. (concat "^\\(\\*+\\)"
  4510. "\\(?: +" org-todo-regexp "\\)?"
  4511. "\\(?: +\\(.*?\\)\\)??"
  4512. (org-re "\\(?:[ \t]+\\(:[[:alnum:]:_@#%]+:\\)\\)?")
  4513. "[ \t]*$"))
  4514. (org-compute-latex-and-related-regexp)))))
  4515. (defun org--setup-collect-keywords (regexp &optional files alist)
  4516. "Return setup keywords values as an alist.
  4517. REGEXP matches a subset of setup keywords. FILES is a list of
  4518. file names already visited. It is used to avoid circular setup
  4519. files. ALIST, when non-nil, is the alist computed so far.
  4520. Return value contains the following keys: `archive', `category',
  4521. `columns', `constants', `filetags', `link', `priorities',
  4522. `property', `scripts', `startup', `tags' and `todo'."
  4523. (org-with-wide-buffer
  4524. (goto-char (point-min))
  4525. (let ((case-fold-search t))
  4526. (while (re-search-forward regexp nil t)
  4527. (let ((element (org-element-at-point)))
  4528. (when (eq (org-element-type element) 'keyword)
  4529. (let ((key (org-element-property :key element))
  4530. (value (org-element-property :value element)))
  4531. (cond
  4532. ((equal key "ARCHIVE")
  4533. (when (org-string-nw-p value)
  4534. (push (cons 'archive value) alist)))
  4535. ((equal key "CATEGORY") (push (cons 'category value) alist))
  4536. ((equal key "COLUMNS") (push (cons 'columns value) alist))
  4537. ((equal key "CONSTANTS")
  4538. (let* ((constants (assq 'constants alist))
  4539. (store (cdr constants)))
  4540. (dolist (pair (org-split-string value))
  4541. (when (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)"
  4542. pair)
  4543. (let* ((name (match-string 1 pair))
  4544. (value (match-string 2 pair))
  4545. (old (assoc name store)))
  4546. (if old (setcdr old value)
  4547. (push (cons name value) store)))))
  4548. (if constants (setcdr constants store)
  4549. (push (cons 'constants store) alist))))
  4550. ((equal key "FILETAGS")
  4551. (when (org-string-nw-p value)
  4552. (let ((old (assq 'filetags alist))
  4553. (new (apply #'nconc
  4554. (mapcar (lambda (x) (org-split-string x ":"))
  4555. (org-split-string value)))))
  4556. (if old (setcdr old (append new (cdr old)))
  4557. (push (cons 'filetags new) alist)))))
  4558. ((equal key "LINK")
  4559. (when (string-match "\\`\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  4560. (let ((links (assq 'link alist))
  4561. (pair (cons (org-match-string-no-properties 1 value)
  4562. (org-match-string-no-properties 2 value))))
  4563. (if links (push pair (cdr links))
  4564. (push (list 'link pair) alist)))))
  4565. ((equal key "OPTIONS")
  4566. (when (and (org-string-nw-p value)
  4567. (string-match "\\^:\\(t\\|nil\\|{}\\)" value))
  4568. (push (cons 'scripts (read (match-string 1 value))) alist)))
  4569. ((equal key "PRIORITIES")
  4570. (push (cons 'priorities
  4571. (let ((prio (org-split-string value)))
  4572. (if (< (length prio) 3) '(?A ?C ?B)
  4573. (mapcar #'string-to-char prio))))
  4574. alist))
  4575. ((equal key "PROPERTY")
  4576. (when (string-match "\\(\\S-+\\)[ \t]+\\(.*\\)" value)
  4577. (let* ((property (assq 'property alist))
  4578. (value (org--update-property-plist
  4579. (org-match-string-no-properties 1 value)
  4580. (org-match-string-no-properties 2 value)
  4581. (cdr property))))
  4582. (if property (setcdr property value)
  4583. (push (cons 'property value) alist)))))
  4584. ((equal key "STARTUP")
  4585. (let ((startup (assq 'startup alist)))
  4586. (if startup
  4587. (setcdr startup
  4588. (append (cdr startup) (org-split-string value)))
  4589. (push (cons 'startup (org-split-string value)) alist))))
  4590. ((equal key "TAGS")
  4591. (let ((tag-cell (assq 'tags alist)))
  4592. (if tag-cell
  4593. (setcdr tag-cell (concat (cdr tag-cell) "\n" value))
  4594. (push (cons 'tags value) alist))))
  4595. ((member key '("TODO" "SEQ_TODO" "TYP_TODO"))
  4596. (let ((todo (assq 'todo alist))
  4597. (value (cons (if (equal key "TYP_TODO") 'type 'sequence)
  4598. (org-split-string value))))
  4599. (if todo (push value (cdr todo))
  4600. (push (list 'todo value) alist))))
  4601. ((equal key "SETUPFILE")
  4602. (unless buffer-read-only ; Do not check in Gnus messages.
  4603. (let ((f (and (org-string-nw-p value)
  4604. (expand-file-name
  4605. (org-remove-double-quotes value)))))
  4606. (when (and f (file-readable-p f) (not (member f files)))
  4607. (with-temp-buffer
  4608. (setq default-directory (file-name-directory f))
  4609. (insert-file-contents f)
  4610. (setq alist
  4611. ;; Fake Org mode to benefit from cache
  4612. ;; without recurring needlessly.
  4613. (let ((major-mode 'org-mode))
  4614. (org--setup-collect-keywords
  4615. regexp (cons f files) alist)))))))))))))))
  4616. alist)
  4617. (defun org-tag-string-to-alist (s)
  4618. "Return tag alist associated to string S.
  4619. S is a value for TAGS keyword or produced with
  4620. `org-tag-alist-to-string'. Return value is an alist suitable for
  4621. `org-tag-alist' or `org-tag-persistent-alist'."
  4622. (let ((lines (mapcar #'split-string (split-string s "\n" t)))
  4623. (tag-re (concat "\\`\\([[:alnum:]_@#%]+"
  4624. "\\|{.+?}\\)" ; regular expression
  4625. "\\(?:(\\(.\\))\\)?\\'"))
  4626. alist group-flag)
  4627. (dolist (tokens lines (cdr (nreverse alist)))
  4628. (push '(:newline) alist)
  4629. (while tokens
  4630. (let ((token (pop tokens)))
  4631. (pcase token
  4632. ("{"
  4633. (push '(:startgroup) alist)
  4634. (when (equal (nth 1 tokens) ":") (setq group-flag t)))
  4635. ("}"
  4636. (push '(:endgroup) alist)
  4637. (setq group-flag nil))
  4638. ("["
  4639. (push '(:startgrouptag) alist)
  4640. (when (equal (nth 1 tokens) ":") (setq group-flag t)))
  4641. ("]"
  4642. (push '(:endgrouptag) alist)
  4643. (setq group-flag nil))
  4644. (":"
  4645. (push '(:grouptags) alist))
  4646. ((guard (string-match tag-re token))
  4647. (let ((tag (match-string 1 token))
  4648. (key (and (match-beginning 2)
  4649. (string-to-char (match-string 2 token)))))
  4650. ;; Push all tags in groups, no matter if they already
  4651. ;; appear somewhere else in the list.
  4652. (when (or group-flag (not (assoc tag alist)))
  4653. (push (cons tag key) alist))))))))))
  4654. (defun org-tag-alist-to-string (alist &optional skip-key)
  4655. "Return tag string associated to ALIST.
  4656. ALIST is an alist, as defined in `org-tag-alist' or
  4657. `org-tag-persistent-alist', or produced with
  4658. `org-tag-string-to-alist'.
  4659. Return value is a string suitable as a value for \"TAGS\"
  4660. keyword.
  4661. When optional argument SKIP-KEY is non-nil, skip selection keys
  4662. next to tags."
  4663. (mapconcat (lambda (token)
  4664. (pcase token
  4665. (`(:startgroup) "{")
  4666. (`(:endgroup) "}")
  4667. (`(:startgrouptag) "[")
  4668. (`(:endgrouptag) "]")
  4669. (`(:grouptags) ":")
  4670. (`(:newline) "\\n")
  4671. ((and
  4672. (guard (not skip-key))
  4673. `(,(and tag (pred stringp)) . ,(and key (pred characterp))))
  4674. (format "%s(%c)" tag key))
  4675. (`(,(and tag (pred stringp)) . ,_) tag)
  4676. (_ (user-error "Invalid tag token: %S" token))))
  4677. alist
  4678. " "))
  4679. (defun org-tag-alist-to-groups (alist)
  4680. "Return group alist from tag ALIST.
  4681. ALIST is an alist, as defined in `org-tag-alist' or
  4682. `org-tag-persistent-alist', or produced with
  4683. `org-tag-string-to-alist'. Return value is an alist following
  4684. the pattern (GROUP-TAG TAGS) where GROUP-TAG is the tag, as
  4685. a string, summarizing TAGS, as a list of strings."
  4686. (let (groups group-status current-group)
  4687. (dolist (token alist (nreverse groups))
  4688. (pcase token
  4689. (`(,(or :startgroup :startgrouptag)) (setq group-status t))
  4690. (`(,(or :endgroup :endgrouptag))
  4691. (when (eq group-status 'append)
  4692. (push (nreverse current-group) groups))
  4693. (setq group-status nil))
  4694. (`(:grouptags) (setq group-status 'append))
  4695. ((and `(,tag . ,_) (guard group-status))
  4696. (if (eq group-status 'append) (push tag current-group)
  4697. (setq current-group (list tag))))
  4698. (_ nil)))))
  4699. (defun org-file-contents (file &optional noerror)
  4700. "Return the contents of FILE, as a string."
  4701. (if (and file (file-readable-p file))
  4702. (with-temp-buffer
  4703. (insert-file-contents file)
  4704. (buffer-string))
  4705. (funcall (if noerror 'message 'error)
  4706. "Cannot read file \"%s\"%s"
  4707. file
  4708. (let ((from (buffer-file-name (buffer-base-buffer))))
  4709. (if from (concat " (referenced in file \"" from "\")") "")))))
  4710. (defun org-extract-log-state-settings (x)
  4711. "Extract the log state setting from a TODO keyword string.
  4712. This will extract info from a string like \"WAIT(w@/!)\"."
  4713. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  4714. (let ((kw (match-string 1 x))
  4715. (log1 (and (match-end 3) (match-string 3 x)))
  4716. (log2 (and (match-end 4) (match-string 4 x))))
  4717. (and (or log1 log2)
  4718. (list kw
  4719. (and log1 (if (equal log1 "!") 'time 'note))
  4720. (and log2 (if (equal log2 "!") 'time 'note)))))))
  4721. (defun org-remove-keyword-keys (list)
  4722. "Remove a pair of parenthesis at the end of each string in LIST."
  4723. (mapcar (lambda (x)
  4724. (if (string-match "(.*)$" x)
  4725. (substring x 0 (match-beginning 0))
  4726. x))
  4727. list))
  4728. (defun org-assign-fast-keys (alist)
  4729. "Assign fast keys to a keyword-key alist.
  4730. Respect keys that are already there."
  4731. (let (new e (alt ?0))
  4732. (while (setq e (pop alist))
  4733. (if (or (memq (car e) '(:newline :grouptags :endgroup :startgroup))
  4734. (cdr e)) ;; Key already assigned.
  4735. (push e new)
  4736. (let ((clist (string-to-list (downcase (car e))))
  4737. (used (append new alist)))
  4738. (when (= (car clist) ?@)
  4739. (pop clist))
  4740. (while (and clist (rassoc (car clist) used))
  4741. (pop clist))
  4742. (unless clist
  4743. (while (rassoc alt used)
  4744. (cl-incf alt)))
  4745. (push (cons (car e) (or (car clist) alt)) new))))
  4746. (nreverse new)))
  4747. ;;; Some variables used in various places
  4748. (defvar org-window-configuration nil
  4749. "Used in various places to store a window configuration.")
  4750. (defvar org-selected-window nil
  4751. "Used in various places to store a window configuration.")
  4752. (defvar org-finish-function nil
  4753. "Function to be called when `C-c C-c' is used.
  4754. This is for getting out of special buffers like capture.")
  4755. (defvar org-last-state)
  4756. ;; Defined somewhere in this file, but used before definition.
  4757. (defvar org-entities) ;; defined in org-entities.el
  4758. (defvar org-struct-menu)
  4759. (defvar org-org-menu)
  4760. (defvar org-tbl-menu)
  4761. ;;;; Define the Org-mode
  4762. ;; We use a before-change function to check if a table might need
  4763. ;; an update.
  4764. (defvar org-table-may-need-update t
  4765. "Indicates that a table might need an update.
  4766. This variable is set by `org-before-change-function'.
  4767. `org-table-align' sets it back to nil.")
  4768. (defun org-before-change-function (_beg _end)
  4769. "Every change indicates that a table might need an update."
  4770. (setq org-table-may-need-update t))
  4771. (defvar org-mode-map)
  4772. (defvar org-inhibit-startup-visibility-stuff nil) ; Dynamically-scoped param.
  4773. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  4774. (defvar org-inhibit-logging nil) ; Dynamically-scoped param.
  4775. (defvar org-inhibit-blocking nil) ; Dynamically-scoped param.
  4776. (defvar org-table-buffer-is-an nil)
  4777. (defvar bidi-paragraph-direction)
  4778. (defvar buffer-face-mode-face)
  4779. (require 'outline)
  4780. (when (and (not (keymapp outline-mode-map)) (featurep 'allout))
  4781. (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"))
  4782. (require 'noutline "noutline" 'noerror) ;; stock XEmacs does not have it
  4783. ;; Other stuff we need.
  4784. (require 'time-date)
  4785. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  4786. (require 'easymenu)
  4787. (autoload 'easy-menu-add "easymenu")
  4788. (require 'overlay)
  4789. ;; (require 'org-macs) moved higher up in the file before it is first used
  4790. (require 'org-entities)
  4791. ;; (require 'org-compat) moved higher up in the file before it is first used
  4792. (require 'org-faces)
  4793. (require 'org-list)
  4794. (require 'org-pcomplete)
  4795. (require 'org-src)
  4796. (require 'org-footnote)
  4797. (require 'org-macro)
  4798. ;; babel
  4799. (require 'ob)
  4800. ;;;###autoload
  4801. (define-derived-mode org-mode outline-mode "Org"
  4802. "Outline-based notes management and organizer, alias
  4803. \"Carsten's outline-mode for keeping track of everything.\"
  4804. Org-mode develops organizational tasks around a NOTES file which
  4805. contains information about projects as plain text. Org-mode is
  4806. implemented on top of outline-mode, which is ideal to keep the content
  4807. of large files well structured. It supports ToDo items, deadlines and
  4808. time stamps, which magically appear in the diary listing of the Emacs
  4809. calendar. Tables are easily created with a built-in table editor.
  4810. Plain text URL-like links connect to websites, emails (VM), Usenet
  4811. messages (Gnus), BBDB entries, and any files related to the project.
  4812. For printing and sharing of notes, an Org-mode file (or a part of it)
  4813. can be exported as a structured ASCII or HTML file.
  4814. The following commands are available:
  4815. \\{org-mode-map}"
  4816. ;; Get rid of Outline menus, they are not needed
  4817. ;; Need to do this here because define-derived-mode sets up
  4818. ;; the keymap so late. Still, it is a waste to call this each time
  4819. ;; we switch another buffer into org-mode.
  4820. (if (featurep 'xemacs)
  4821. (when (boundp 'outline-mode-menu-heading)
  4822. ;; Assume this is Greg's port, it uses easymenu
  4823. (easy-menu-remove outline-mode-menu-heading)
  4824. (easy-menu-remove outline-mode-menu-show)
  4825. (easy-menu-remove outline-mode-menu-hide))
  4826. (define-key org-mode-map [menu-bar headings] 'undefined)
  4827. (define-key org-mode-map [menu-bar hide] 'undefined)
  4828. (define-key org-mode-map [menu-bar show] 'undefined))
  4829. (org-load-modules-maybe)
  4830. (when (featurep 'xemacs)
  4831. (easy-menu-add org-org-menu)
  4832. (easy-menu-add org-tbl-menu))
  4833. (org-install-agenda-files-menu)
  4834. (when org-descriptive-links (add-to-invisibility-spec '(org-link)))
  4835. (add-to-invisibility-spec '(org-cwidth))
  4836. (add-to-invisibility-spec '(org-hide-block . t))
  4837. (when (featurep 'xemacs)
  4838. (setq-local line-move-ignore-invisible t))
  4839. (setq-local outline-regexp org-outline-regexp)
  4840. (setq-local outline-level 'org-outline-level)
  4841. (setq bidi-paragraph-direction 'left-to-right)
  4842. (when (and org-ellipsis
  4843. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  4844. (fboundp 'make-glyph-code))
  4845. (unless org-display-table
  4846. (setq org-display-table (make-display-table)))
  4847. (set-display-table-slot
  4848. org-display-table 4
  4849. (vconcat (mapcar
  4850. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  4851. org-ellipsis)))
  4852. (if (stringp org-ellipsis) org-ellipsis "..."))))
  4853. (setq buffer-display-table org-display-table))
  4854. (org-set-regexps-and-options)
  4855. (org-set-font-lock-defaults)
  4856. (when (and org-tag-faces (not org-tags-special-faces-re))
  4857. ;; tag faces set outside customize.... force initialization.
  4858. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  4859. ;; Calc embedded
  4860. (setq-local calc-embedded-open-mode "# ")
  4861. ;; Modify a few syntax entries
  4862. (modify-syntax-entry ?@ "w")
  4863. (modify-syntax-entry ?\" "\"")
  4864. (modify-syntax-entry ?\\ "_")
  4865. (modify-syntax-entry ?~ "_")
  4866. (setq-local font-lock-unfontify-region-function 'org-unfontify-region)
  4867. ;; Activate before-change-function
  4868. (setq-local org-table-may-need-update t)
  4869. (org-add-hook 'before-change-functions 'org-before-change-function nil 'local)
  4870. ;; Check for running clock before killing a buffer
  4871. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  4872. ;; Initialize macros templates.
  4873. (org-macro-initialize-templates)
  4874. ;; Initialize radio targets.
  4875. (org-update-radio-target-regexp)
  4876. ;; Indentation.
  4877. (setq-local indent-line-function 'org-indent-line)
  4878. (setq-local indent-region-function 'org-indent-region)
  4879. ;; Filling and auto-filling.
  4880. (org-setup-filling)
  4881. ;; Comments.
  4882. (org-setup-comments-handling)
  4883. ;; Initialize cache.
  4884. (org-element-cache-reset)
  4885. ;; Beginning/end of defun
  4886. (setq-local beginning-of-defun-function 'org-backward-element)
  4887. (setq-local end-of-defun-function
  4888. (lambda ()
  4889. (if (not (org-at-heading-p))
  4890. (org-forward-element)
  4891. (org-forward-element)
  4892. (forward-char -1))))
  4893. ;; Next error for sparse trees
  4894. (setq-local next-error-function 'org-occur-next-match)
  4895. ;; Make sure dependence stuff works reliably, even for users who set it
  4896. ;; too late :-(
  4897. (if org-enforce-todo-dependencies
  4898. (add-hook 'org-blocker-hook
  4899. 'org-block-todo-from-children-or-siblings-or-parent)
  4900. (remove-hook 'org-blocker-hook
  4901. 'org-block-todo-from-children-or-siblings-or-parent))
  4902. (if org-enforce-todo-checkbox-dependencies
  4903. (add-hook 'org-blocker-hook
  4904. 'org-block-todo-from-checkboxes)
  4905. (remove-hook 'org-blocker-hook
  4906. 'org-block-todo-from-checkboxes))
  4907. ;; Align options lines
  4908. (setq-local
  4909. align-mode-rules-list
  4910. '((org-in-buffer-settings
  4911. (regexp . "^[ \t]*#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  4912. (modes . '(org-mode)))))
  4913. ;; Imenu
  4914. (setq-local imenu-create-index-function 'org-imenu-get-tree)
  4915. ;; Make isearch reveal context
  4916. (setq-local outline-isearch-open-invisible-function
  4917. (lambda (&rest _) (org-show-context 'isearch)))
  4918. ;; Setup the pcomplete hooks
  4919. (setq-local pcomplete-command-completion-function 'org-pcomplete-initial)
  4920. (setq-local pcomplete-command-name-function 'org-command-at-point)
  4921. (setq-local pcomplete-default-completion-function 'ignore)
  4922. (setq-local pcomplete-parse-arguments-function 'org-parse-arguments)
  4923. (setq-local pcomplete-termination-string "")
  4924. (setq-local buffer-face-mode-face 'org-default)
  4925. ;; If empty file that did not turn on Org mode automatically, make
  4926. ;; it to.
  4927. (when (and org-insert-mode-line-in-empty-file
  4928. (org-called-interactively-p 'any)
  4929. (= (point-min) (point-max)))
  4930. (insert "# -*- mode: org -*-\n\n"))
  4931. (unless org-inhibit-startup
  4932. (org-unmodified
  4933. (when org-startup-with-beamer-mode (org-beamer-mode))
  4934. (when org-startup-align-all-tables
  4935. (org-table-map-tables #'org-table-align t))
  4936. (when org-startup-with-inline-images (org-display-inline-images))
  4937. (when org-startup-with-latex-preview (org-toggle-latex-fragment))
  4938. (unless org-inhibit-startup-visibility-stuff (org-set-startup-visibility))
  4939. (when org-startup-truncated (setq truncate-lines t))
  4940. (when org-startup-indented (require 'org-indent) (org-indent-mode 1))
  4941. (org-refresh-effort-properties)))
  4942. ;; Try to set `org-hide' face correctly.
  4943. (let ((foreground (org-find-invisible-foreground)))
  4944. (when foreground
  4945. (set-face-foreground 'org-hide foreground))))
  4946. ;; Update `customize-package-emacs-version-alist'
  4947. (add-to-list 'customize-package-emacs-version-alist
  4948. '(Org ("6.21b" . "23.1") ("6.33x" . "23.2")
  4949. ("7.8.11" . "24.1") ("7.9.4" . "24.3")
  4950. ("8.2.6" . "24.4") ("8.3" . "25.1")))
  4951. (defvar org-mode-transpose-word-syntax-table
  4952. (let ((st (make-syntax-table text-mode-syntax-table)))
  4953. (dolist (c org-emphasis-alist st)
  4954. (modify-syntax-entry (string-to-char (car c)) "w p" st))))
  4955. (when (fboundp 'abbrev-table-put)
  4956. (abbrev-table-put org-mode-abbrev-table
  4957. :parents (list text-mode-abbrev-table)))
  4958. (defun org-find-invisible-foreground ()
  4959. (let ((candidates (remove
  4960. "unspecified-bg"
  4961. (nconc
  4962. (list (face-background 'default)
  4963. (face-background 'org-default))
  4964. (mapcar
  4965. (lambda (alist)
  4966. (when (boundp alist)
  4967. (cdr (assoc 'background-color (symbol-value alist)))))
  4968. '(default-frame-alist initial-frame-alist window-system-default-frame-alist))
  4969. (list (face-foreground 'org-hide))))))
  4970. (car (remove nil candidates))))
  4971. (defun org-current-time (&optional rounding-minutes past)
  4972. "Current time, possibly rounded to ROUNDING-MINUTES.
  4973. When ROUNDING-MINUTES is not an integer, fall back on the car of
  4974. `org-time-stamp-rounding-minutes'. When PAST is non-nil, ensure
  4975. the rounding returns a past time."
  4976. (let ((r (or (and (integerp rounding-minutes) rounding-minutes)
  4977. (car org-time-stamp-rounding-minutes)))
  4978. (time (decode-time)) res)
  4979. (if (< r 1)
  4980. (current-time)
  4981. (setq res
  4982. (apply 'encode-time
  4983. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  4984. (nthcdr 2 time))))
  4985. (if (and past (< (org-float-time (time-subtract (current-time) res)) 0))
  4986. (seconds-to-time (- (org-float-time res) (* r 60)))
  4987. res))))
  4988. (defun org-today ()
  4989. "Return today date, considering `org-extend-today-until'."
  4990. (time-to-days
  4991. (time-subtract (current-time)
  4992. (list 0 (* 3600 org-extend-today-until) 0))))
  4993. ;;;; Font-Lock stuff, including the activators
  4994. (defvar org-mouse-map (make-sparse-keymap))
  4995. (org-defkey org-mouse-map [mouse-2] 'org-open-at-mouse)
  4996. (org-defkey org-mouse-map [mouse-3] 'org-find-file-at-mouse)
  4997. (when org-mouse-1-follows-link
  4998. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  4999. (when org-tab-follows-link
  5000. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  5001. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  5002. (require 'font-lock)
  5003. (defconst org-non-link-chars "]\t\n\r<>")
  5004. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "file+emacs"
  5005. "file+sys" "news" "shell" "elisp" "doi" "message"
  5006. "help"))
  5007. (defvar org-link-types-re nil
  5008. "Matches a link that has a url-like prefix like \"http:\"")
  5009. (defvar org-link-re-with-space nil
  5010. "Matches a link with spaces, optional angular brackets around it.")
  5011. (defvar org-link-re-with-space2 nil
  5012. "Matches a link with spaces, optional angular brackets around it.")
  5013. (defvar org-link-re-with-space3 nil
  5014. "Matches a link with spaces, only for internal part in bracket links.")
  5015. (defvar org-angle-link-re nil
  5016. "Matches link with angular brackets, spaces are allowed.")
  5017. (defvar org-plain-link-re nil
  5018. "Matches plain link, without spaces.")
  5019. (defvar org-bracket-link-regexp nil
  5020. "Matches a link in double brackets.")
  5021. (defvar org-bracket-link-analytic-regexp nil
  5022. "Regular expression used to analyze links.
  5023. Here is what the match groups contain after a match:
  5024. 1: http:
  5025. 2: http
  5026. 3: path
  5027. 4: [desc]
  5028. 5: desc")
  5029. (defvar org-bracket-link-analytic-regexp++ nil
  5030. "Like `org-bracket-link-analytic-regexp', but include coderef internal type.")
  5031. (defvar org-any-link-re nil
  5032. "Regular expression matching any link.")
  5033. (defconst org-match-sexp-depth 3
  5034. "Number of stacked braces for sub/superscript matching.")
  5035. (defun org-create-multibrace-regexp (left right n)
  5036. "Create a regular expression which will match a balanced sexp.
  5037. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  5038. as single character strings.
  5039. The regexp returned will match the entire expression including the
  5040. delimiters. It will also define a single group which contains the
  5041. match except for the outermost delimiters. The maximum depth of
  5042. stacked delimiters is N. Escaping delimiters is not possible."
  5043. (let* ((nothing (concat "[^" left right "]*?"))
  5044. (or "\\|")
  5045. (re nothing)
  5046. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  5047. (while (> n 1)
  5048. (setq n (1- n)
  5049. re (concat re or next)
  5050. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  5051. (concat left "\\(" re "\\)" right)))
  5052. (defconst org-match-substring-regexp
  5053. (concat
  5054. "\\(\\S-\\)\\([_^]\\)\\("
  5055. "\\(?:" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  5056. "\\|"
  5057. "\\(?:" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  5058. "\\|"
  5059. "\\(?:\\*\\|[+-]?[[:alnum:].,\\]*[[:alnum:]]\\)\\)")
  5060. "The regular expression matching a sub- or superscript.")
  5061. (defconst org-match-substring-with-braces-regexp
  5062. (concat
  5063. "\\(\\S-\\)\\([_^]\\)"
  5064. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)")
  5065. "The regular expression matching a sub- or superscript, forcing braces.")
  5066. (defun org-make-link-regexps ()
  5067. "Update the link regular expressions.
  5068. This should be called after the variable `org-link-types' has changed."
  5069. (let ((types-re (regexp-opt org-link-types t)))
  5070. (setq org-link-types-re
  5071. (concat "\\`" types-re ":")
  5072. org-link-re-with-space
  5073. (concat "<?" types-re ":"
  5074. "\\([^" org-non-link-chars " ]"
  5075. "[^" org-non-link-chars "]*"
  5076. "[^" org-non-link-chars " ]\\)>?")
  5077. org-link-re-with-space2
  5078. (concat "<?" types-re ":"
  5079. "\\([^" org-non-link-chars " ]"
  5080. "[^\t\n\r]*"
  5081. "[^" org-non-link-chars " ]\\)>?")
  5082. org-link-re-with-space3
  5083. (concat "<?" types-re ":"
  5084. "\\([^" org-non-link-chars " ]"
  5085. "[^\t\n\r]*\\)")
  5086. org-angle-link-re
  5087. (format "<%s:\\([^>\n]*\\(?:\n[ \t]*[^> \t\n][^>\n]*\\)*\\)>"
  5088. types-re)
  5089. org-plain-link-re
  5090. (concat
  5091. "\\<" types-re ":"
  5092. (org-re "\\([^ \t\n()<>]+\\(?:([[:word:]0-9_]+)\\|\\([^[:punct:] \t\n]\\|/\\)\\)\\)"))
  5093. ;; "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  5094. org-bracket-link-regexp
  5095. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  5096. org-bracket-link-analytic-regexp
  5097. (concat
  5098. "\\[\\["
  5099. "\\(" types-re ":\\)?"
  5100. "\\([^]]+\\)"
  5101. "\\]"
  5102. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  5103. "\\]")
  5104. org-bracket-link-analytic-regexp++
  5105. (concat
  5106. "\\[\\["
  5107. "\\(" (regexp-opt (cons "coderef" org-link-types) t) ":\\)?"
  5108. "\\([^]]+\\)"
  5109. "\\]"
  5110. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  5111. "\\]")
  5112. org-any-link-re
  5113. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  5114. org-angle-link-re "\\)\\|\\("
  5115. org-plain-link-re "\\)"))))
  5116. (org-make-link-regexps)
  5117. (defvar org-emph-face nil)
  5118. (defun org-do-emphasis-faces (limit)
  5119. "Run through the buffer and emphasize strings."
  5120. (let (rtn a)
  5121. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  5122. (let* ((border (char-after (match-beginning 3)))
  5123. (bre (regexp-quote (char-to-string border))))
  5124. (when (and (not (= border (char-after (match-beginning 4))))
  5125. (not (string-match-p (concat bre ".*" bre)
  5126. (replace-regexp-in-string
  5127. "\n" " "
  5128. (substring (match-string 2) 1 -1)))))
  5129. (setq rtn t)
  5130. (setq a (assoc (match-string 3) org-emphasis-alist))
  5131. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  5132. 'face
  5133. (nth 1 a))
  5134. (and (nth 2 a)
  5135. (org-remove-flyspell-overlays-in
  5136. (match-beginning 0) (match-end 0)))
  5137. (add-text-properties (match-beginning 2) (match-end 2)
  5138. '(font-lock-multiline t org-emphasis t))
  5139. (when org-hide-emphasis-markers
  5140. (add-text-properties (match-end 4) (match-beginning 5)
  5141. '(invisible org-link))
  5142. (add-text-properties (match-beginning 3) (match-end 3)
  5143. '(invisible org-link)))))
  5144. (goto-char (1+ (match-beginning 0))))
  5145. rtn))
  5146. (defun org-emphasize (&optional char)
  5147. "Insert or change an emphasis, i.e. a font like bold or italic.
  5148. If there is an active region, change that region to a new emphasis.
  5149. If there is no region, just insert the marker characters and position
  5150. the cursor between them.
  5151. CHAR should be the marker character. If it is a space, it means to
  5152. remove the emphasis of the selected region.
  5153. If CHAR is not given (for example in an interactive call) it will be
  5154. prompted for."
  5155. (interactive)
  5156. (let ((erc org-emphasis-regexp-components)
  5157. (string "") beg end move s)
  5158. (if (org-region-active-p)
  5159. (setq beg (region-beginning)
  5160. end (region-end)
  5161. string (buffer-substring beg end))
  5162. (setq move t))
  5163. (unless char
  5164. (message "Emphasis marker or tag: [%s]"
  5165. (mapconcat #'car org-emphasis-alist ""))
  5166. (setq char (read-char-exclusive)))
  5167. (if (equal char ?\s)
  5168. (setq s ""
  5169. move nil)
  5170. (unless (assoc (char-to-string char) org-emphasis-alist)
  5171. (user-error "No such emphasis marker: \"%c\"" char))
  5172. (setq s (char-to-string char)))
  5173. (while (and (> (length string) 1)
  5174. (equal (substring string 0 1) (substring string -1))
  5175. (assoc (substring string 0 1) org-emphasis-alist))
  5176. (setq string (substring string 1 -1)))
  5177. (setq string (concat s string s))
  5178. (when beg (delete-region beg end))
  5179. (unless (or (bolp)
  5180. (string-match (concat "[" (nth 0 erc) "\n]")
  5181. (char-to-string (char-before (point)))))
  5182. (insert " "))
  5183. (unless (or (eobp)
  5184. (string-match (concat "[" (nth 1 erc) "\n]")
  5185. (char-to-string (char-after (point)))))
  5186. (insert " ") (backward-char 1))
  5187. (insert string)
  5188. (and move (backward-char 1))))
  5189. (defconst org-nonsticky-props
  5190. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text htmlize-link))
  5191. (defsubst org-rear-nonsticky-at (pos)
  5192. (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
  5193. (defun org-activate-plain-links (limit)
  5194. "Add link properties for plain links."
  5195. (when (and (re-search-forward org-plain-link-re limit t)
  5196. (not (org-in-src-block-p)))
  5197. (let ((face (get-text-property (max (1- (match-beginning 0)) (point-min))
  5198. 'face))
  5199. (link (org-match-string-no-properties 0)))
  5200. (unless (if (consp face) (memq 'org-tag face) (eq 'org-tag face))
  5201. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5202. (add-text-properties (match-beginning 0) (match-end 0)
  5203. (list 'mouse-face 'highlight
  5204. 'face 'org-link
  5205. 'htmlize-link `(:uri ,link)
  5206. 'keymap org-mouse-map))
  5207. (org-rear-nonsticky-at (match-end 0))
  5208. t))))
  5209. (defun org-activate-code (limit)
  5210. (when (re-search-forward "^[ \t]*\\(:\\(?: .*\\|$\\)\n?\\)" limit t)
  5211. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5212. (remove-text-properties (match-beginning 0) (match-end 0)
  5213. '(display t invisible t intangible t))
  5214. t))
  5215. (defcustom org-src-fontify-natively t
  5216. "When non-nil, fontify code in code blocks."
  5217. :type 'boolean
  5218. :version "24.4"
  5219. :package-version '(Org . "8.3")
  5220. :group 'org-appearance
  5221. :group 'org-babel)
  5222. (defcustom org-allow-promoting-top-level-subtree nil
  5223. "When non-nil, allow promoting a top level subtree.
  5224. The leading star of the top level headline will be replaced
  5225. by a #."
  5226. :type 'boolean
  5227. :version "24.1"
  5228. :group 'org-appearance)
  5229. (defun org-fontify-meta-lines-and-blocks (limit)
  5230. (condition-case nil
  5231. (org-fontify-meta-lines-and-blocks-1 limit)
  5232. (error (message "org-mode fontification error in %S at %d"
  5233. (current-buffer)
  5234. (line-number-at-pos)))))
  5235. (defun org-fontify-meta-lines-and-blocks-1 (limit)
  5236. "Fontify #+ lines and blocks."
  5237. (let ((case-fold-search t))
  5238. (when (re-search-forward
  5239. "^\\([ \t]*#\\(\\(\\+[a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)[ \t]*\\(\\([^ \t\n]*\\)[ \t]*\\(.*\\)\\)\\)"
  5240. limit t)
  5241. (let ((beg (match-beginning 0))
  5242. (block-start (match-end 0))
  5243. (block-end nil)
  5244. (lang (match-string 7))
  5245. (beg1 (line-beginning-position 2))
  5246. (dc1 (downcase (match-string 2)))
  5247. (dc3 (downcase (match-string 3)))
  5248. end end1 quoting block-type)
  5249. (cond
  5250. ((and (match-end 4) (equal dc3 "+begin"))
  5251. ;; Truly a block
  5252. (setq block-type (downcase (match-string 5))
  5253. quoting (member block-type org-protecting-blocks))
  5254. (when (re-search-forward
  5255. (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
  5256. nil t) ;; on purpose, we look further than LIMIT
  5257. (setq end (min (point-max) (match-end 0))
  5258. end1 (min (point-max) (1- (match-beginning 0))))
  5259. (setq block-end (match-beginning 0))
  5260. (when quoting
  5261. (org-remove-flyspell-overlays-in beg1 end1)
  5262. (remove-text-properties beg end
  5263. '(display t invisible t intangible t)))
  5264. (add-text-properties
  5265. beg end '(font-lock-fontified t font-lock-multiline t))
  5266. (add-text-properties beg beg1 '(face org-meta-line))
  5267. (org-remove-flyspell-overlays-in beg beg1)
  5268. (add-text-properties ; For end_src
  5269. end1 (min (point-max) (1+ end)) '(face org-meta-line))
  5270. (org-remove-flyspell-overlays-in end1 end)
  5271. (cond
  5272. ((and lang (not (string= lang "")) org-src-fontify-natively)
  5273. (org-src-font-lock-fontify-block lang block-start block-end)
  5274. (add-text-properties beg1 block-end '(src-block t)))
  5275. (quoting
  5276. (add-text-properties beg1 (min (point-max) (1+ end1))
  5277. '(face org-block))) ; end of source block
  5278. ((not org-fontify-quote-and-verse-blocks))
  5279. ((string= block-type "quote")
  5280. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-quote)))
  5281. ((string= block-type "verse")
  5282. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-verse))))
  5283. (add-text-properties beg beg1 '(face org-block-begin-line))
  5284. (add-text-properties (min (point-max) (1+ end)) (min (point-max) (1+ end1))
  5285. '(face org-block-end-line))
  5286. t))
  5287. ((member dc1 '("+title:" "+author:" "+email:" "+date:"))
  5288. (org-remove-flyspell-overlays-in
  5289. (match-beginning 0)
  5290. (if (equal "+title:" dc1) (match-end 2) (match-end 0)))
  5291. (add-text-properties
  5292. beg (match-end 3)
  5293. (if (member (intern (substring dc1 1 -1)) org-hidden-keywords)
  5294. '(font-lock-fontified t invisible t)
  5295. '(font-lock-fontified t face org-document-info-keyword)))
  5296. (add-text-properties
  5297. (match-beginning 6) (min (point-max) (1+ (match-end 6)))
  5298. (if (string-equal dc1 "+title:")
  5299. '(font-lock-fontified t face org-document-title)
  5300. '(font-lock-fontified t face org-document-info))))
  5301. ((equal dc1 "+caption:")
  5302. (org-remove-flyspell-overlays-in (match-end 2) (match-end 0))
  5303. (remove-text-properties (match-beginning 0) (match-end 0)
  5304. '(display t invisible t intangible t))
  5305. (add-text-properties (match-beginning 1) (match-end 3)
  5306. '(font-lock-fontified t face org-meta-line))
  5307. (add-text-properties (match-beginning 6) (+ (match-end 6) 1)
  5308. '(font-lock-fontified t face org-block))
  5309. t)
  5310. ((member dc3 '(" " ""))
  5311. (org-remove-flyspell-overlays-in beg (match-end 0))
  5312. (add-text-properties
  5313. beg (match-end 0)
  5314. '(font-lock-fontified t face font-lock-comment-face)))
  5315. (t ;; just any other in-buffer setting, but not indented
  5316. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5317. (remove-text-properties (match-beginning 0) (match-end 0)
  5318. '(display t invisible t intangible t))
  5319. (add-text-properties beg (match-end 0)
  5320. '(font-lock-fontified t face org-meta-line))
  5321. t))))))
  5322. (defun org-fontify-drawers (limit)
  5323. "Fontify drawers."
  5324. (when (re-search-forward org-drawer-regexp limit t)
  5325. (add-text-properties
  5326. (match-beginning 0) (match-end 0)
  5327. '(font-lock-fontified t face org-special-keyword))
  5328. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5329. t))
  5330. (defun org-fontify-macros (limit)
  5331. "Fontify macros."
  5332. (when (re-search-forward "\\({{{\\).+?\\(}}}\\)" limit t)
  5333. (add-text-properties
  5334. (match-beginning 0) (match-end 0)
  5335. '(font-lock-fontified t face org-macro))
  5336. (when org-hide-macro-markers
  5337. (add-text-properties (match-end 2) (match-beginning 2)
  5338. '(invisible t))
  5339. (add-text-properties (match-beginning 1) (match-end 1)
  5340. '(invisible t)))
  5341. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5342. t))
  5343. (defun org-activate-angle-links (limit)
  5344. "Add text properties for angle links."
  5345. (when (and (re-search-forward org-angle-link-re limit t)
  5346. (not (org-in-src-block-p)))
  5347. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5348. (add-text-properties (match-beginning 0) (match-end 0)
  5349. (list 'mouse-face 'highlight
  5350. 'keymap org-mouse-map
  5351. 'font-lock-multiline t))
  5352. (org-rear-nonsticky-at (match-end 0))
  5353. t))
  5354. (defun org-activate-footnote-links (limit)
  5355. "Add text properties for footnotes."
  5356. (let ((fn (org-footnote-next-reference-or-definition limit)))
  5357. (when fn
  5358. (let* ((beg (nth 1 fn))
  5359. (end (nth 2 fn))
  5360. (label (car fn))
  5361. (referencep (/= (line-beginning-position) beg)))
  5362. (when (and referencep (nth 3 fn))
  5363. (save-excursion
  5364. (goto-char beg)
  5365. (search-forward (or label "fn:"))
  5366. (org-remove-flyspell-overlays-in beg (match-end 0))))
  5367. (add-text-properties beg end
  5368. (list 'mouse-face 'highlight
  5369. 'keymap org-mouse-map
  5370. 'help-echo
  5371. (if referencep "Footnote reference"
  5372. "Footnote definition")
  5373. 'font-lock-fontified t
  5374. 'font-lock-multiline t
  5375. 'face 'org-footnote))))))
  5376. (defun org-activate-bracket-links (limit)
  5377. "Add text properties for bracketed links."
  5378. (when (and (re-search-forward org-bracket-link-regexp limit t)
  5379. (not (org-in-src-block-p)))
  5380. (let* ((hl (org-match-string-no-properties 1))
  5381. (help (concat "LINK: " (save-match-data (org-link-unescape hl))))
  5382. (ip (org-maybe-intangible
  5383. (list 'invisible 'org-link
  5384. 'keymap org-mouse-map 'mouse-face 'highlight
  5385. 'font-lock-multiline t 'help-echo help
  5386. 'htmlize-link `(:uri ,hl))))
  5387. (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
  5388. 'font-lock-multiline t 'help-echo help
  5389. 'htmlize-link `(:uri ,hl))))
  5390. ;; We need to remove the invisible property here. Table narrowing
  5391. ;; may have made some of this invisible.
  5392. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5393. (remove-text-properties (match-beginning 0) (match-end 0)
  5394. '(invisible nil))
  5395. (if (match-end 3)
  5396. (progn
  5397. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  5398. (org-rear-nonsticky-at (match-beginning 3))
  5399. (add-text-properties (match-beginning 3) (match-end 3) vp)
  5400. (org-rear-nonsticky-at (match-end 3))
  5401. (add-text-properties (match-end 3) (match-end 0) ip)
  5402. (org-rear-nonsticky-at (match-end 0)))
  5403. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  5404. (org-rear-nonsticky-at (match-beginning 1))
  5405. (add-text-properties (match-beginning 1) (match-end 1) vp)
  5406. (org-rear-nonsticky-at (match-end 1))
  5407. (add-text-properties (match-end 1) (match-end 0) ip)
  5408. (org-rear-nonsticky-at (match-end 0)))
  5409. t)))
  5410. (defun org-activate-dates (limit)
  5411. "Add text properties for dates."
  5412. (when (and (re-search-forward org-tsr-regexp-both limit t)
  5413. (not (equal (char-before (match-beginning 0)) 91)))
  5414. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5415. (add-text-properties (match-beginning 0) (match-end 0)
  5416. (list 'mouse-face 'highlight
  5417. 'keymap org-mouse-map))
  5418. (org-rear-nonsticky-at (match-end 0))
  5419. (when org-display-custom-times
  5420. (if (match-end 3)
  5421. (org-display-custom-time (match-beginning 3) (match-end 3))
  5422. (org-display-custom-time (match-beginning 1) (match-end 1))))
  5423. t))
  5424. (defvar-local org-target-link-regexp nil
  5425. "Regular expression matching radio targets in plain text.")
  5426. (defconst org-target-regexp (let ((border "[^<>\n\r \t]"))
  5427. (format "<<\\(%s\\|%s[^<>\n\r]*%s\\)>>"
  5428. border border border))
  5429. "Regular expression matching a link target.")
  5430. (defconst org-radio-target-regexp (format "<%s>" org-target-regexp)
  5431. "Regular expression matching a radio target.")
  5432. (defconst org-any-target-regexp
  5433. (format "%s\\|%s" org-radio-target-regexp org-target-regexp)
  5434. "Regular expression matching any target.")
  5435. (defun org-activate-target-links (limit)
  5436. "Add text properties for target matches."
  5437. (when org-target-link-regexp
  5438. (let ((case-fold-search t))
  5439. (when (re-search-forward org-target-link-regexp limit t)
  5440. (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
  5441. (add-text-properties (match-beginning 1) (match-end 1)
  5442. (list 'mouse-face 'highlight
  5443. 'keymap org-mouse-map
  5444. 'help-echo "Radio target link"
  5445. 'org-linked-text t))
  5446. (org-rear-nonsticky-at (match-end 1))
  5447. t))))
  5448. (defun org-update-radio-target-regexp ()
  5449. "Find all radio targets in this file and update the regular expression.
  5450. Also refresh fontification if needed."
  5451. (interactive)
  5452. (let ((old-regexp org-target-link-regexp)
  5453. (before-re "\\(?:^\\|[^[:alnum:]]\\)\\(")
  5454. (after-re "\\)\\(?:$\\|[^[:alnum:]]\\)")
  5455. (targets
  5456. (org-with-wide-buffer
  5457. (goto-char (point-min))
  5458. (let (rtn)
  5459. (while (re-search-forward org-radio-target-regexp nil t)
  5460. ;; Make sure point is really within the object.
  5461. (backward-char)
  5462. (let ((obj (org-element-context)))
  5463. (when (eq (org-element-type obj) 'radio-target)
  5464. (cl-pushnew (org-element-property :value obj) rtn
  5465. :test #'equal))))
  5466. rtn))))
  5467. (setq org-target-link-regexp
  5468. (and targets
  5469. (concat before-re
  5470. (mapconcat
  5471. (lambda (x)
  5472. (replace-regexp-in-string
  5473. " +" "\\s-+" (regexp-quote x) t t))
  5474. targets
  5475. "\\|")
  5476. after-re)))
  5477. (unless (equal old-regexp org-target-link-regexp)
  5478. ;; Clean-up cache.
  5479. (let ((regexp (cond ((not old-regexp) org-target-link-regexp)
  5480. ((not org-target-link-regexp) old-regexp)
  5481. (t
  5482. (concat before-re
  5483. (mapconcat
  5484. (lambda (re)
  5485. (substring re (length before-re)
  5486. (- (length after-re))))
  5487. (list old-regexp org-target-link-regexp)
  5488. "\\|")
  5489. after-re)))))
  5490. (org-with-wide-buffer
  5491. (goto-char (point-min))
  5492. (while (re-search-forward regexp nil t)
  5493. (org-element-cache-refresh (match-beginning 1)))))
  5494. ;; Re fontify buffer.
  5495. (when (memq 'radio org-highlight-links)
  5496. (org-restart-font-lock)))))
  5497. (defun org-hide-wide-columns (limit)
  5498. (let (s e)
  5499. (setq s (text-property-any (point) (or limit (point-max))
  5500. 'org-cwidth t))
  5501. (when s
  5502. (setq e (next-single-property-change s 'org-cwidth))
  5503. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  5504. (goto-char e)
  5505. t)))
  5506. (defvar org-latex-and-related-regexp nil
  5507. "Regular expression for highlighting LaTeX, entities and sub/superscript.")
  5508. (defun org-compute-latex-and-related-regexp ()
  5509. "Compute regular expression for LaTeX, entities and sub/superscript.
  5510. Result depends on variable `org-highlight-latex-and-related'."
  5511. (setq-local
  5512. org-latex-and-related-regexp
  5513. (let* ((re-sub
  5514. (cond ((not (memq 'script org-highlight-latex-and-related)) nil)
  5515. ((eq org-use-sub-superscripts '{})
  5516. (list org-match-substring-with-braces-regexp))
  5517. (org-use-sub-superscripts (list org-match-substring-regexp))))
  5518. (re-latex
  5519. (when (memq 'latex org-highlight-latex-and-related)
  5520. (let ((matchers (plist-get org-format-latex-options :matchers)))
  5521. (delq nil
  5522. (mapcar (lambda (x)
  5523. (and (member (car x) matchers) (nth 1 x)))
  5524. org-latex-regexps)))))
  5525. (re-entities
  5526. (when (memq 'entities org-highlight-latex-and-related)
  5527. (list "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)"))))
  5528. (mapconcat 'identity (append re-latex re-entities re-sub) "\\|"))))
  5529. (defun org-do-latex-and-related (limit)
  5530. "Highlight LaTeX snippets and environments, entities and sub/superscript.
  5531. LIMIT bounds the search for syntax to highlight. Stop at first
  5532. highlighted object, if any. Return t if some highlighting was
  5533. done, nil otherwise."
  5534. (when (org-string-nw-p org-latex-and-related-regexp)
  5535. (catch 'found
  5536. (while (re-search-forward org-latex-and-related-regexp limit t)
  5537. (unless
  5538. (cl-some
  5539. (lambda (f)
  5540. (memq f '(org-code org-verbatim underline org-special-keyword)))
  5541. (save-excursion
  5542. (goto-char (1+ (match-beginning 0)))
  5543. (face-at-point nil t)))
  5544. (let ((offset (if (memq (char-after (1+ (match-beginning 0)))
  5545. '(?_ ?^))
  5546. 1
  5547. 0)))
  5548. (font-lock-prepend-text-property
  5549. (+ offset (match-beginning 0)) (match-end 0)
  5550. 'face 'org-latex-and-related)
  5551. (add-text-properties (+ offset (match-beginning 0)) (match-end 0)
  5552. '(font-lock-multiline t)))
  5553. (throw 'found t)))
  5554. nil)))
  5555. (defun org-restart-font-lock ()
  5556. "Restart `font-lock-mode', to force refontification."
  5557. (when (and (boundp 'font-lock-mode) font-lock-mode)
  5558. (font-lock-mode -1)
  5559. (font-lock-mode 1)))
  5560. (defun org-activate-tags (limit)
  5561. (when (re-search-forward
  5562. (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$") limit t)
  5563. (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
  5564. (add-text-properties (match-beginning 1) (match-end 1)
  5565. (list 'mouse-face 'highlight
  5566. 'keymap org-mouse-map))
  5567. (org-rear-nonsticky-at (match-end 1))
  5568. t))
  5569. (defun org-outline-level ()
  5570. "Compute the outline level of the heading at point.
  5571. If this is called at a normal headline, the level is the number
  5572. of stars. Use `org-reduced-level' to remove the effect of
  5573. `org-odd-levels'. Unlike to `org-current-level', this function
  5574. takes into consideration inlinetasks."
  5575. (org-with-wide-buffer
  5576. (end-of-line)
  5577. (if (re-search-backward org-outline-regexp-bol nil t)
  5578. (1- (- (match-end 0) (match-beginning 0)))
  5579. 0)))
  5580. (defvar org-font-lock-keywords nil)
  5581. (defsubst org-re-property (property &optional literal allow-null value)
  5582. "Return a regexp matching a PROPERTY line.
  5583. When optional argument LITERAL is non-nil, do not quote PROPERTY.
  5584. This is useful when PROPERTY is a regexp. When ALLOW-NULL is
  5585. non-nil, match properties even without a value.
  5586. Match group 3 is set to the value when it exists. If there is no
  5587. value and ALLOW-NULL is non-nil, it is set to the empty string.
  5588. With optional argument VALUE, match only property lines with
  5589. that value; in this case, ALLOW-NULL is ignored. VALUE is quoted
  5590. unless LITERAL is non-nil."
  5591. (concat
  5592. "^\\(?4:[ \t]*\\)"
  5593. (format "\\(?1::\\(?2:%s\\):\\)"
  5594. (if literal property (regexp-quote property)))
  5595. (cond (value
  5596. (format "[ \t]+\\(?3:%s\\)\\(?5:[ \t]*\\)$"
  5597. (if literal value (regexp-quote value))))
  5598. (allow-null
  5599. "\\(?:\\(?3:$\\)\\|[ \t]+\\(?3:.*?\\)\\)\\(?5:[ \t]*\\)$")
  5600. (t
  5601. "[ \t]+\\(?3:[^ \r\t\n]+.*?\\)\\(?5:[ \t]*\\)$"))))
  5602. (defconst org-property-re
  5603. (org-re-property "\\S-+" 'literal t)
  5604. "Regular expression matching a property line.
  5605. There are four matching groups:
  5606. 1: :PROPKEY: including the leading and trailing colon,
  5607. 2: PROPKEY without the leading and trailing colon,
  5608. 3: PROPVAL without leading or trailing spaces,
  5609. 4: the indentation of the current line,
  5610. 5: trailing whitespace.")
  5611. (defvar org-font-lock-hook nil
  5612. "Functions to be called for special font lock stuff.")
  5613. (defvar org-font-lock-extra-keywords nil) ;Dynamically scoped.
  5614. (defvar org-font-lock-set-keywords-hook nil
  5615. "Functions that can manipulate `org-font-lock-extra-keywords'.
  5616. This is called after `org-font-lock-extra-keywords' is defined, but before
  5617. it is installed to be used by font lock. This can be useful if something
  5618. needs to be inserted at a specific position in the font-lock sequence.")
  5619. (defun org-font-lock-hook (limit)
  5620. "Run `org-font-lock-hook' within LIMIT."
  5621. (run-hook-with-args 'org-font-lock-hook limit))
  5622. (defun org-set-font-lock-defaults ()
  5623. "Set font lock defaults for the current buffer."
  5624. (let* ((em org-fontify-emphasized-text)
  5625. (lk org-highlight-links)
  5626. (org-font-lock-extra-keywords
  5627. (list
  5628. ;; Call the hook
  5629. '(org-font-lock-hook)
  5630. ;; Headlines
  5631. `(,(if org-fontify-whole-heading-line
  5632. "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
  5633. "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
  5634. (1 (org-get-level-face 1))
  5635. (2 (org-get-level-face 2))
  5636. (3 (org-get-level-face 3)))
  5637. ;; Table lines
  5638. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  5639. (1 'org-table t))
  5640. ;; Table internals
  5641. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  5642. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  5643. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  5644. '("| *\\(<[lrc]?[0-9]*>\\)" (1 'org-formula t))
  5645. ;; Drawers
  5646. '(org-fontify-drawers)
  5647. ;; Properties
  5648. (list org-property-re
  5649. '(1 'org-special-keyword t)
  5650. '(3 'org-property-value t))
  5651. ;; Links
  5652. (when (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  5653. (when (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  5654. (when (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  5655. (when (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  5656. (when (memq 'radio lk) '(org-activate-target-links (1 'org-link t)))
  5657. (when (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  5658. (when (memq 'footnote lk) '(org-activate-footnote-links))
  5659. ;; Targets.
  5660. (list org-any-target-regexp '(0 'org-target t))
  5661. ;; Diary sexps.
  5662. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  5663. ;; Macro
  5664. '(org-fontify-macros)
  5665. '(org-hide-wide-columns (0 nil append))
  5666. ;; TODO keyword
  5667. (list (format org-heading-keyword-regexp-format
  5668. org-todo-regexp)
  5669. '(2 (org-get-todo-face 2) t))
  5670. ;; DONE
  5671. (if org-fontify-done-headline
  5672. (list (format org-heading-keyword-regexp-format
  5673. (concat
  5674. "\\(?:"
  5675. (mapconcat 'regexp-quote org-done-keywords "\\|")
  5676. "\\)"))
  5677. '(2 'org-headline-done t))
  5678. nil)
  5679. ;; Priorities
  5680. '(org-font-lock-add-priority-faces)
  5681. ;; Tags
  5682. '(org-font-lock-add-tag-faces)
  5683. ;; Tags groups
  5684. (when (and org-group-tags org-tag-groups-alist)
  5685. (list (concat org-outline-regexp-bol ".+\\(:"
  5686. (regexp-opt (mapcar 'car org-tag-groups-alist))
  5687. ":\\).*$")
  5688. '(1 'org-tag-group prepend)))
  5689. ;; Special keywords
  5690. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  5691. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  5692. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  5693. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  5694. ;; Emphasis
  5695. (when em
  5696. (if (featurep 'xemacs)
  5697. '(org-do-emphasis-faces (0 nil append))
  5698. '(org-do-emphasis-faces)))
  5699. ;; Checkboxes
  5700. '("^[ \t]*\\(?:[-+*]\\|[0-9]+[.)]\\)[ \t]+\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?\\(\\[[- X]\\]\\)"
  5701. 1 'org-checkbox prepend)
  5702. (when (cdr (assq 'checkbox org-list-automatic-rules))
  5703. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  5704. (0 (org-get-checkbox-statistics-face) t)))
  5705. ;; Description list items
  5706. '("^[ \t]*[-+*][ \t]+\\(.*?[ \t]+::\\)\\([ \t]+\\|$\\)"
  5707. 1 'org-list-dt prepend)
  5708. ;; ARCHIVEd headings
  5709. (list (concat
  5710. org-outline-regexp-bol
  5711. "\\(.*:" org-archive-tag ":.*\\)")
  5712. '(1 'org-archived prepend))
  5713. ;; Specials
  5714. '(org-do-latex-and-related)
  5715. '(org-fontify-entities)
  5716. '(org-raise-scripts)
  5717. ;; Code
  5718. '(org-activate-code (1 'org-code t))
  5719. ;; COMMENT
  5720. (list (format
  5721. "^\\*+\\(?: +%s\\)?\\(?: +\\[#[A-Z0-9]\\]\\)? +\\(?9:%s\\)\\(?: \\|$\\)"
  5722. org-todo-regexp
  5723. org-comment-string)
  5724. '(9 'org-special-keyword t))
  5725. ;; Blocks and meta lines
  5726. '(org-fontify-meta-lines-and-blocks))))
  5727. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  5728. (run-hooks 'org-font-lock-set-keywords-hook)
  5729. ;; Now set the full font-lock-keywords
  5730. (setq-local org-font-lock-keywords org-font-lock-extra-keywords)
  5731. (setq-local font-lock-defaults
  5732. '(org-font-lock-keywords t nil nil backward-paragraph))
  5733. (kill-local-variable 'font-lock-keywords) nil))
  5734. (defun org-toggle-pretty-entities ()
  5735. "Toggle the composition display of entities as UTF8 characters."
  5736. (interactive)
  5737. (setq-local org-pretty-entities (not org-pretty-entities))
  5738. (org-restart-font-lock)
  5739. (if org-pretty-entities
  5740. (message "Entities are now displayed as UTF8 characters")
  5741. (save-restriction
  5742. (widen)
  5743. (org-decompose-region (point-min) (point-max))
  5744. (message "Entities are now displayed as plain text"))))
  5745. (defvar-local org-custom-properties-overlays nil
  5746. "List of overlays used for custom properties.")
  5747. (defun org-toggle-custom-properties-visibility ()
  5748. "Display or hide properties in `org-custom-properties'."
  5749. (interactive)
  5750. (if org-custom-properties-overlays
  5751. (progn (mapc #'delete-overlay org-custom-properties-overlays)
  5752. (setq org-custom-properties-overlays nil))
  5753. (when org-custom-properties
  5754. (org-with-wide-buffer
  5755. (goto-char (point-min))
  5756. (let ((regexp (org-re-property (regexp-opt org-custom-properties) t t)))
  5757. (while (re-search-forward regexp nil t)
  5758. (let ((end (cdr (save-match-data (org-get-property-block)))))
  5759. (when (and end (< (point) end))
  5760. ;; Hide first custom property in current drawer.
  5761. (let ((o (make-overlay (match-beginning 0) (1+ (match-end 0)))))
  5762. (overlay-put o 'invisible t)
  5763. (overlay-put o 'org-custom-property t)
  5764. (push o org-custom-properties-overlays))
  5765. ;; Hide additional custom properties in the same drawer.
  5766. (while (re-search-forward regexp end t)
  5767. (let ((o (make-overlay (match-beginning 0) (1+ (match-end 0)))))
  5768. (overlay-put o 'invisible t)
  5769. (overlay-put o 'org-custom-property t)
  5770. (push o org-custom-properties-overlays)))))
  5771. ;; Each entry is limited to a single property drawer.
  5772. (outline-next-heading)))))))
  5773. (defun org-fontify-entities (limit)
  5774. "Find an entity to fontify."
  5775. (let (ee)
  5776. (when org-pretty-entities
  5777. (catch 'match
  5778. ;; "\_ "-family is left out on purpose. Only the first one,
  5779. ;; i.e., "\_ ", could be fontified anyway, and it would be
  5780. ;; confusing when adding a second white space character.
  5781. (while (re-search-forward
  5782. "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]\n]\\)"
  5783. limit t)
  5784. (when (and (not (org-at-comment-p))
  5785. (setq ee (org-entity-get (match-string 1)))
  5786. (= (length (nth 6 ee)) 1))
  5787. (let* ((end (if (equal (match-string 2) "{}")
  5788. (match-end 2)
  5789. (match-end 1))))
  5790. (add-text-properties
  5791. (match-beginning 0) end
  5792. (list 'font-lock-fontified t))
  5793. (compose-region (match-beginning 0) end
  5794. (nth 6 ee) nil)
  5795. (backward-char 1)
  5796. (throw 'match t))))
  5797. nil))))
  5798. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  5799. "Fontify string S like in Org-mode."
  5800. (with-temp-buffer
  5801. (insert s)
  5802. (let ((org-odd-levels-only odd-levels))
  5803. (org-mode)
  5804. (org-font-lock-ensure)
  5805. (buffer-string))))
  5806. (defvar org-m nil)
  5807. (defvar org-l nil)
  5808. (defvar org-f nil)
  5809. (defun org-get-level-face (n)
  5810. "Get the right face for match N in font-lock matching of headlines."
  5811. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  5812. (when org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  5813. (if org-cycle-level-faces
  5814. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  5815. (setq org-f (nth (1- (min org-l org-n-level-faces)) org-level-faces)))
  5816. (cond
  5817. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  5818. ((eq n 2) org-f)
  5819. (t (unless org-level-color-stars-only org-f))))
  5820. (defun org-face-from-face-or-color (context inherit face-or-color)
  5821. "Create a face list that inherits INHERIT, but sets the foreground color.
  5822. When FACE-OR-COLOR is not a string, just return it."
  5823. (if (stringp face-or-color)
  5824. (list :inherit inherit
  5825. (cdr (assoc context org-faces-easy-properties))
  5826. face-or-color)
  5827. face-or-color))
  5828. (defun org-get-todo-face (kwd)
  5829. "Get the right face for a TODO keyword KWD.
  5830. If KWD is a number, get the corresponding match group."
  5831. (when (numberp kwd) (setq kwd (match-string kwd)))
  5832. (or (org-face-from-face-or-color
  5833. 'todo 'org-todo (cdr (assoc kwd org-todo-keyword-faces)))
  5834. (and (member kwd org-done-keywords) 'org-done)
  5835. 'org-todo))
  5836. (defun org-get-priority-face (priority)
  5837. "Get the right face for PRIORITY.
  5838. PRIORITY is a character."
  5839. (or (org-face-from-face-or-color
  5840. 'priority 'org-priority (cdr (assq priority org-priority-faces)))
  5841. 'org-priority))
  5842. (defun org-get-tag-face (tag)
  5843. "Get the right face for TAG.
  5844. If TAG is a number, get the corresponding match group."
  5845. (let ((tag (if (wholenump tag) (match-string tag) tag)))
  5846. (or (org-face-from-face-or-color
  5847. 'tag 'org-tag (cdr (assoc tag org-tag-faces)))
  5848. 'org-tag)))
  5849. (defun org-font-lock-add-priority-faces (limit)
  5850. "Add the special priority faces."
  5851. (while (re-search-forward "^\\*+ .*?\\(\\[#\\(.\\)\\]\\)" limit t)
  5852. (add-text-properties
  5853. (match-beginning 1) (match-end 1)
  5854. (list 'face (org-get-priority-face (string-to-char (match-string 2)))
  5855. 'font-lock-fontified t))))
  5856. (defun org-font-lock-add-tag-faces (limit)
  5857. "Add the special tag faces."
  5858. (when (and org-tag-faces org-tags-special-faces-re)
  5859. (while (re-search-forward org-tags-special-faces-re limit t)
  5860. (add-text-properties (match-beginning 1) (match-end 1)
  5861. (list 'face (org-get-tag-face 1)
  5862. 'font-lock-fontified t))
  5863. (backward-char 1))))
  5864. (defun org-unfontify-region (beg end &optional _maybe_loudly)
  5865. "Remove fontification and activation overlays from links."
  5866. (font-lock-default-unfontify-region beg end)
  5867. (let* ((buffer-undo-list t)
  5868. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  5869. (inhibit-modification-hooks t)
  5870. deactivate-mark buffer-file-name buffer-file-truename)
  5871. (org-decompose-region beg end)
  5872. (remove-text-properties beg end
  5873. '(mouse-face t keymap t org-linked-text t
  5874. invisible t intangible t
  5875. org-emphasis t))
  5876. (org-remove-font-lock-display-properties beg end)))
  5877. (defconst org-script-display '(((raise -0.3) (height 0.7))
  5878. ((raise 0.3) (height 0.7))
  5879. ((raise -0.5))
  5880. ((raise 0.5)))
  5881. "Display properties for showing superscripts and subscripts.")
  5882. (defun org-remove-font-lock-display-properties (beg end)
  5883. "Remove specific display properties that have been added by font lock.
  5884. The will remove the raise properties that are used to show superscripts
  5885. and subscripts."
  5886. (let (next prop)
  5887. (while (< beg end)
  5888. (setq next (next-single-property-change beg 'display nil end)
  5889. prop (get-text-property beg 'display))
  5890. (when (member prop org-script-display)
  5891. (put-text-property beg next 'display nil))
  5892. (setq beg next))))
  5893. (defun org-raise-scripts (limit)
  5894. "Add raise properties to sub/superscripts."
  5895. (when (and org-pretty-entities org-pretty-entities-include-sub-superscripts
  5896. (re-search-forward
  5897. (if (eq org-use-sub-superscripts t)
  5898. org-match-substring-regexp
  5899. org-match-substring-with-braces-regexp)
  5900. limit t))
  5901. (let* ((pos (point)) table-p comment-p
  5902. (mpos (match-beginning 3))
  5903. (emph-p (get-text-property mpos 'org-emphasis))
  5904. (link-p (get-text-property mpos 'mouse-face))
  5905. (keyw-p (eq 'org-special-keyword (get-text-property mpos 'face))))
  5906. (goto-char (point-at-bol))
  5907. (setq table-p (org-looking-at-p org-table-dataline-regexp)
  5908. comment-p (org-looking-at-p "^[ \t]*#[ +]"))
  5909. (goto-char pos)
  5910. ;; Handle a_b^c
  5911. (when (member (char-after) '(?_ ?^)) (goto-char (1- pos)))
  5912. (unless (or comment-p emph-p link-p keyw-p)
  5913. (put-text-property (match-beginning 3) (match-end 0)
  5914. 'display
  5915. (if (equal (char-after (match-beginning 2)) ?^)
  5916. (nth (if table-p 3 1) org-script-display)
  5917. (nth (if table-p 2 0) org-script-display)))
  5918. (add-text-properties (match-beginning 2) (match-end 2)
  5919. (list 'invisible t
  5920. 'org-dwidth t 'org-dwidth-n 1))
  5921. (if (and (eq (char-after (match-beginning 3)) ?{)
  5922. (eq (char-before (match-end 3)) ?}))
  5923. (progn
  5924. (add-text-properties
  5925. (match-beginning 3) (1+ (match-beginning 3))
  5926. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))
  5927. (add-text-properties
  5928. (1- (match-end 3)) (match-end 3)
  5929. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1)))))
  5930. t)))
  5931. ;;;; Visibility cycling, including org-goto and indirect buffer
  5932. ;;; Cycling
  5933. (defvar-local org-cycle-global-status nil)
  5934. (put 'org-cycle-global-status 'org-state t)
  5935. (defvar-local org-cycle-subtree-status nil)
  5936. (put 'org-cycle-subtree-status 'org-state t)
  5937. (defvar org-inlinetask-min-level)
  5938. (defun org-unlogged-message (&rest args)
  5939. "Display a message, but avoid logging it in the *Messages* buffer."
  5940. (let ((message-log-max nil))
  5941. (apply 'message args)))
  5942. ;;;###autoload
  5943. (defun org-cycle (&optional arg)
  5944. "TAB-action and visibility cycling for Org-mode.
  5945. This is the command invoked in Org-mode by the TAB key. Its main purpose
  5946. is outline visibility cycling, but it also invokes other actions
  5947. in special contexts.
  5948. - When this function is called with a prefix argument, rotate the entire
  5949. buffer through 3 states (global cycling)
  5950. 1. OVERVIEW: Show only top-level headlines.
  5951. 2. CONTENTS: Show all headlines of all levels, but no body text.
  5952. 3. SHOW ALL: Show everything.
  5953. With a double \\[universal-argument] prefix argument, \
  5954. switch to the startup visibility,
  5955. determined by the variable `org-startup-folded', and by any VISIBILITY
  5956. properties in the buffer.
  5957. With a triple \\[universal-argument] prefix argument, \
  5958. show the entire buffer, including any drawers.
  5959. - When inside a table, re-align the table and move to the next field.
  5960. - When point is at the beginning of a headline, rotate the subtree started
  5961. by this line through 3 different states (local cycling)
  5962. 1. FOLDED: Only the main headline is shown.
  5963. 2. CHILDREN: The main headline and the direct children are shown.
  5964. From this state, you can move to one of the children
  5965. and zoom in further.
  5966. 3. SUBTREE: Show the entire subtree, including body text.
  5967. If there is no subtree, switch directly from CHILDREN to FOLDED.
  5968. - When point is at the beginning of an empty headline and the variable
  5969. `org-cycle-level-after-item/entry-creation' is set, cycle the level
  5970. of the headline by demoting and promoting it to likely levels. This
  5971. speeds up creation document structure by pressing TAB once or several
  5972. times right after creating a new headline.
  5973. - When there is a numeric prefix, go up to a heading with level ARG, do
  5974. a `show-subtree' and return to the previous cursor position. If ARG
  5975. is negative, go up that many levels.
  5976. - When point is not at the beginning of a headline, execute the global
  5977. binding for TAB, which is re-indenting the line. See the option
  5978. `org-cycle-emulate-tab' for details.
  5979. - Special case: if point is at the beginning of the buffer and there is
  5980. no headline in line 1, this function will act as if called with prefix arg
  5981. (\\[universal-argument] TAB, same as S-TAB) also when called without prefix arg.
  5982. But only if also the variable `org-cycle-global-at-bob' is t."
  5983. (interactive "P")
  5984. (org-load-modules-maybe)
  5985. (unless (or (run-hook-with-args-until-success 'org-tab-first-hook)
  5986. (and org-cycle-level-after-item/entry-creation
  5987. (or (org-cycle-level)
  5988. (org-cycle-item-indentation))))
  5989. (let* ((limit-level
  5990. (or org-cycle-max-level
  5991. (and (boundp 'org-inlinetask-min-level)
  5992. org-inlinetask-min-level
  5993. (1- org-inlinetask-min-level))))
  5994. (nstars (and limit-level
  5995. (if org-odd-levels-only
  5996. (and limit-level (1- (* limit-level 2)))
  5997. limit-level)))
  5998. (org-outline-regexp
  5999. (if (not (derived-mode-p 'org-mode))
  6000. outline-regexp
  6001. (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ "))))
  6002. (bob-special (and org-cycle-global-at-bob (not arg) (bobp)
  6003. (not (looking-at org-outline-regexp))))
  6004. (org-cycle-hook
  6005. (if bob-special
  6006. (delq 'org-optimize-window-after-visibility-change
  6007. (copy-sequence org-cycle-hook))
  6008. org-cycle-hook))
  6009. (pos (point)))
  6010. (when (or bob-special (equal arg '(4)))
  6011. ;; special case: use global cycling
  6012. (setq arg t))
  6013. (cond
  6014. ((equal arg '(16))
  6015. (setq last-command 'dummy)
  6016. (org-set-startup-visibility)
  6017. (org-unlogged-message "Startup visibility, plus VISIBILITY properties"))
  6018. ((equal arg '(64))
  6019. (outline-show-all)
  6020. (org-unlogged-message "Entire buffer visible, including drawers"))
  6021. ;; Try cdlatex TAB completion
  6022. ((org-try-cdlatex-tab))
  6023. ;; Table: enter it or move to the next field.
  6024. ((org-at-table-p 'any)
  6025. (if (org-at-table.el-p)
  6026. (message "%s" (substitute-command-keys "\\<org-mode-map>\
  6027. Use \\[org-edit-special] to edit table.el tables"))
  6028. (if arg (org-table-edit-field t)
  6029. (org-table-justify-field-maybe)
  6030. (call-interactively 'org-table-next-field))))
  6031. ((run-hook-with-args-until-success
  6032. 'org-tab-after-check-for-table-hook))
  6033. ;; Global cycling: delegate to `org-cycle-internal-global'.
  6034. ((eq arg t) (org-cycle-internal-global))
  6035. ;; Drawers: delegate to `org-flag-drawer'.
  6036. ((save-excursion
  6037. (beginning-of-line 1)
  6038. (looking-at org-drawer-regexp))
  6039. (org-flag-drawer ; toggle block visibility
  6040. (not (get-char-property (match-end 0) 'invisible))))
  6041. ;; Show-subtree, ARG levels up from here.
  6042. ((integerp arg)
  6043. (save-excursion
  6044. (org-back-to-heading)
  6045. (outline-up-heading (if (< arg 0) (- arg)
  6046. (- (funcall outline-level) arg)))
  6047. (org-show-subtree)))
  6048. ;; Inline task: delegate to `org-inlinetask-toggle-visibility'.
  6049. ((and (featurep 'org-inlinetask)
  6050. (org-inlinetask-at-task-p)
  6051. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  6052. (org-inlinetask-toggle-visibility))
  6053. ;; At an item/headline: delegate to `org-cycle-internal-local'.
  6054. ((and (or (and org-cycle-include-plain-lists (org-at-item-p))
  6055. (save-excursion (move-beginning-of-line 1)
  6056. (looking-at org-outline-regexp)))
  6057. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  6058. (org-cycle-internal-local))
  6059. ;; From there: TAB emulation and template completion.
  6060. (buffer-read-only (org-back-to-heading))
  6061. ((run-hook-with-args-until-success
  6062. 'org-tab-after-check-for-cycling-hook))
  6063. ((org-try-structure-completion))
  6064. ((run-hook-with-args-until-success
  6065. 'org-tab-before-tab-emulation-hook))
  6066. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  6067. (or (not (bolp))
  6068. (not (looking-at org-outline-regexp))))
  6069. (call-interactively (global-key-binding "\t")))
  6070. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  6071. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  6072. (or (and (eq org-cycle-emulate-tab 'white)
  6073. (= (match-end 0) (point-at-eol)))
  6074. (and (eq org-cycle-emulate-tab 'whitestart)
  6075. (>= (match-end 0) pos))))
  6076. t
  6077. (eq org-cycle-emulate-tab t))
  6078. (call-interactively (global-key-binding "\t")))
  6079. (t (save-excursion
  6080. (org-back-to-heading)
  6081. (org-cycle)))))))
  6082. (defun org-cycle-internal-global ()
  6083. "Do the global cycling action."
  6084. ;; Hack to avoid display of messages for .org attachments in Gnus
  6085. (let ((ga (string-match "\\*fontification" (buffer-name))))
  6086. (cond
  6087. ((and (eq last-command this-command)
  6088. (eq org-cycle-global-status 'overview))
  6089. ;; We just created the overview - now do table of contents
  6090. ;; This can be slow in very large buffers, so indicate action
  6091. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  6092. (unless ga (org-unlogged-message "CONTENTS..."))
  6093. (org-content)
  6094. (unless ga (org-unlogged-message "CONTENTS...done"))
  6095. (setq org-cycle-global-status 'contents)
  6096. (run-hook-with-args 'org-cycle-hook 'contents))
  6097. ((and (eq last-command this-command)
  6098. (eq org-cycle-global-status 'contents))
  6099. ;; We just showed the table of contents - now show everything
  6100. (run-hook-with-args 'org-pre-cycle-hook 'all)
  6101. (outline-show-all)
  6102. (unless ga (org-unlogged-message "SHOW ALL"))
  6103. (setq org-cycle-global-status 'all)
  6104. (run-hook-with-args 'org-cycle-hook 'all))
  6105. (t
  6106. ;; Default action: go to overview
  6107. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  6108. (org-overview)
  6109. (unless ga (org-unlogged-message "OVERVIEW"))
  6110. (setq org-cycle-global-status 'overview)
  6111. (run-hook-with-args 'org-cycle-hook 'overview)))))
  6112. (defvar org-called-with-limited-levels nil
  6113. "Non-nil when `org-with-limited-levels' is currently active.")
  6114. (defun org-cycle-internal-local ()
  6115. "Do the local cycling action."
  6116. (let ((goal-column 0) eoh eol eos has-children children-skipped struct)
  6117. ;; First, determine end of headline (EOH), end of subtree or item
  6118. ;; (EOS), and if item or heading has children (HAS-CHILDREN).
  6119. (save-excursion
  6120. (if (org-at-item-p)
  6121. (progn
  6122. (beginning-of-line)
  6123. (setq struct (org-list-struct))
  6124. (setq eoh (point-at-eol))
  6125. (setq eos (org-list-get-item-end-before-blank (point) struct))
  6126. (setq has-children (org-list-has-child-p (point) struct)))
  6127. (org-back-to-heading)
  6128. (setq eoh (save-excursion (outline-end-of-heading) (point)))
  6129. (setq eos (save-excursion (org-end-of-subtree t t)
  6130. (when (bolp) (backward-char)) (point)))
  6131. (setq has-children
  6132. (or (save-excursion
  6133. (let ((level (funcall outline-level)))
  6134. (outline-next-heading)
  6135. (and (org-at-heading-p t)
  6136. (> (funcall outline-level) level))))
  6137. (save-excursion
  6138. (org-list-search-forward (org-item-beginning-re) eos t)))))
  6139. ;; Determine end invisible part of buffer (EOL)
  6140. (beginning-of-line 2)
  6141. ;; XEmacs doesn't have `next-single-char-property-change'
  6142. (if (featurep 'xemacs)
  6143. (while (and (not (eobp)) ;; this is like `next-line'
  6144. (get-char-property (1- (point)) 'invisible))
  6145. (beginning-of-line 2))
  6146. (while (and (not (eobp)) ;; this is like `next-line'
  6147. (get-char-property (1- (point)) 'invisible))
  6148. (goto-char (next-single-char-property-change (point) 'invisible))
  6149. (and (eolp) (beginning-of-line 2))))
  6150. (setq eol (point)))
  6151. ;; Find out what to do next and set `this-command'
  6152. (cond
  6153. ((= eos eoh)
  6154. ;; Nothing is hidden behind this heading
  6155. (unless (org-before-first-heading-p)
  6156. (run-hook-with-args 'org-pre-cycle-hook 'empty))
  6157. (org-unlogged-message "EMPTY ENTRY")
  6158. (setq org-cycle-subtree-status nil)
  6159. (save-excursion
  6160. (goto-char eos)
  6161. (outline-next-heading)
  6162. (when (outline-invisible-p) (org-flag-heading nil))))
  6163. ((and (or (>= eol eos)
  6164. (not (string-match "\\S-" (buffer-substring eol eos))))
  6165. (or has-children
  6166. (not (setq children-skipped
  6167. org-cycle-skip-children-state-if-no-children))))
  6168. ;; Entire subtree is hidden in one line: children view
  6169. (unless (org-before-first-heading-p)
  6170. (run-hook-with-args 'org-pre-cycle-hook 'children))
  6171. (if (org-at-item-p)
  6172. (org-list-set-item-visibility (point-at-bol) struct 'children)
  6173. (org-show-entry)
  6174. (org-with-limited-levels (org-show-children))
  6175. ;; FIXME: This slows down the func way too much.
  6176. ;; How keep drawers hidden in subtree anyway?
  6177. ;; (when (memq 'org-cycle-hide-drawers org-cycle-hook)
  6178. ;; (org-cycle-hide-drawers 'subtree))
  6179. ;; Fold every list in subtree to top-level items.
  6180. (when (eq org-cycle-include-plain-lists 'integrate)
  6181. (save-excursion
  6182. (org-back-to-heading)
  6183. (while (org-list-search-forward (org-item-beginning-re) eos t)
  6184. (beginning-of-line 1)
  6185. (let* ((struct (org-list-struct))
  6186. (prevs (org-list-prevs-alist struct))
  6187. (end (org-list-get-bottom-point struct)))
  6188. (dolist (e (org-list-get-all-items (point) struct prevs))
  6189. (org-list-set-item-visibility e struct 'folded))
  6190. (goto-char (if (< end eos) end eos)))))))
  6191. (org-unlogged-message "CHILDREN")
  6192. (save-excursion
  6193. (goto-char eos)
  6194. (outline-next-heading)
  6195. (when (outline-invisible-p) (org-flag-heading nil)))
  6196. (setq org-cycle-subtree-status 'children)
  6197. (unless (org-before-first-heading-p)
  6198. (run-hook-with-args 'org-cycle-hook 'children)))
  6199. ((or children-skipped
  6200. (and (eq last-command this-command)
  6201. (eq org-cycle-subtree-status 'children)))
  6202. ;; We just showed the children, or no children are there,
  6203. ;; now show everything.
  6204. (unless (org-before-first-heading-p)
  6205. (run-hook-with-args 'org-pre-cycle-hook 'subtree))
  6206. (outline-flag-region eoh eos nil)
  6207. (org-unlogged-message
  6208. (if children-skipped "SUBTREE (NO CHILDREN)" "SUBTREE"))
  6209. (setq org-cycle-subtree-status 'subtree)
  6210. (unless (org-before-first-heading-p)
  6211. (run-hook-with-args 'org-cycle-hook 'subtree)))
  6212. (t
  6213. ;; Default action: hide the subtree.
  6214. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  6215. (outline-flag-region eoh eos t)
  6216. (org-unlogged-message "FOLDED")
  6217. (setq org-cycle-subtree-status 'folded)
  6218. (unless (org-before-first-heading-p)
  6219. (run-hook-with-args 'org-cycle-hook 'folded))))))
  6220. ;;;###autoload
  6221. (defun org-global-cycle (&optional arg)
  6222. "Cycle the global visibility. For details see `org-cycle'.
  6223. With \\[universal-argument] prefix arg, switch to startup visibility.
  6224. With a numeric prefix, show all headlines up to that level."
  6225. (interactive "P")
  6226. (let ((org-cycle-include-plain-lists
  6227. (if (derived-mode-p 'org-mode) org-cycle-include-plain-lists nil)))
  6228. (cond
  6229. ((integerp arg)
  6230. (outline-show-all)
  6231. (outline-hide-sublevels arg)
  6232. (setq org-cycle-global-status 'contents))
  6233. ((equal arg '(4))
  6234. (org-set-startup-visibility)
  6235. (org-unlogged-message "Startup visibility, plus VISIBILITY properties."))
  6236. (t
  6237. (org-cycle '(4))))))
  6238. (defun org-set-startup-visibility ()
  6239. "Set the visibility required by startup options and properties."
  6240. (cond
  6241. ((eq org-startup-folded t)
  6242. (org-overview))
  6243. ((eq org-startup-folded 'content)
  6244. (org-content))
  6245. ((or (eq org-startup-folded 'showeverything)
  6246. (eq org-startup-folded nil))
  6247. (outline-show-all)))
  6248. (unless (eq org-startup-folded 'showeverything)
  6249. (when org-hide-block-startup (org-hide-block-all))
  6250. (org-set-visibility-according-to-property 'no-cleanup)
  6251. (org-cycle-hide-archived-subtrees 'all)
  6252. (org-cycle-hide-drawers 'all)
  6253. (org-cycle-show-empty-lines t)))
  6254. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  6255. "Switch subtree visibilities according to :VISIBILITY: property."
  6256. (interactive)
  6257. (org-with-wide-buffer
  6258. (goto-char (point-min))
  6259. (while (re-search-forward "^[ \t]*:VISIBILITY:" nil t)
  6260. (if (not (org-at-property-p)) (outline-next-heading)
  6261. (let ((state (match-string 3)))
  6262. (save-excursion
  6263. (org-back-to-heading t)
  6264. (outline-hide-subtree)
  6265. (org-reveal)
  6266. (cond
  6267. ((equal state "folded")
  6268. (outline-hide-subtree))
  6269. ((equal state "children")
  6270. (org-show-hidden-entry)
  6271. (org-show-children))
  6272. ((equal state "content")
  6273. (save-excursion
  6274. (save-restriction
  6275. (org-narrow-to-subtree)
  6276. (org-content))))
  6277. ((member state '("all" "showall"))
  6278. (outline-show-subtree)))))))
  6279. (unless no-cleanup
  6280. (org-cycle-hide-archived-subtrees 'all)
  6281. (org-cycle-hide-drawers 'all)
  6282. (org-cycle-show-empty-lines 'all))))
  6283. ;; This function uses outline-regexp instead of the more fundamental
  6284. ;; org-outline-regexp so that org-cycle-global works outside of Org
  6285. ;; buffers, where outline-regexp is needed.
  6286. (defun org-overview ()
  6287. "Switch to overview mode, showing only top-level headlines.
  6288. This shows all headlines with a level equal or greater than the level
  6289. of the first headline in the buffer. This is important, because if the
  6290. first headline is not level one, then (hide-sublevels 1) gives confusing
  6291. results."
  6292. (interactive)
  6293. (save-excursion
  6294. (let ((level
  6295. (save-excursion
  6296. (goto-char (point-min))
  6297. (when (re-search-forward (concat "^" outline-regexp) nil t)
  6298. (goto-char (match-beginning 0))
  6299. (funcall outline-level)))))
  6300. (and level (outline-hide-sublevels level)))))
  6301. (defun org-content (&optional arg)
  6302. "Show all headlines in the buffer, like a table of contents.
  6303. With numerical argument N, show content up to level N."
  6304. (interactive "P")
  6305. (org-overview)
  6306. (save-excursion
  6307. ;; Visit all headings and show their offspring
  6308. (and (integerp arg) (org-overview))
  6309. (goto-char (point-max))
  6310. (catch 'exit
  6311. (while (and (progn (condition-case nil
  6312. (outline-previous-visible-heading 1)
  6313. (error (goto-char (point-min))))
  6314. t)
  6315. (looking-at org-outline-regexp))
  6316. (if (integerp arg)
  6317. (org-show-children (1- arg))
  6318. (outline-show-branches))
  6319. (when (bobp) (throw 'exit nil))))))
  6320. (defun org-optimize-window-after-visibility-change (state)
  6321. "Adjust the window after a change in outline visibility.
  6322. This function is the default value of the hook `org-cycle-hook'."
  6323. (when (get-buffer-window (current-buffer))
  6324. (cond
  6325. ((eq state 'content) nil)
  6326. ((eq state 'all) nil)
  6327. ((eq state 'folded) nil)
  6328. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  6329. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  6330. (defun org-remove-empty-overlays-at (pos)
  6331. "Remove outline overlays that do not contain non-white stuff."
  6332. (dolist (o (overlays-at pos))
  6333. (and (eq 'outline (overlay-get o 'invisible))
  6334. (not (string-match "\\S-" (buffer-substring (overlay-start o)
  6335. (overlay-end o))))
  6336. (delete-overlay o))))
  6337. (defun org-clean-visibility-after-subtree-move ()
  6338. "Fix visibility issues after moving a subtree."
  6339. ;; First, find a reasonable region to look at:
  6340. ;; Start two siblings above, end three below
  6341. (let* ((beg (save-excursion
  6342. (and (org-get-last-sibling)
  6343. (org-get-last-sibling))
  6344. (point)))
  6345. (end (save-excursion
  6346. (and (org-get-next-sibling)
  6347. (org-get-next-sibling)
  6348. (org-get-next-sibling))
  6349. (if (org-at-heading-p)
  6350. (point-at-eol)
  6351. (point))))
  6352. (level (looking-at "\\*+"))
  6353. (re (when level (concat "^" (regexp-quote (match-string 0)) " "))))
  6354. (save-excursion
  6355. (save-restriction
  6356. (narrow-to-region beg end)
  6357. (when re
  6358. ;; Properly fold already folded siblings
  6359. (goto-char (point-min))
  6360. (while (re-search-forward re nil t)
  6361. (when (and (not (outline-invisible-p))
  6362. (save-excursion
  6363. (goto-char (point-at-eol)) (outline-invisible-p)))
  6364. (outline-hide-entry))))
  6365. (org-cycle-show-empty-lines 'overview)
  6366. (org-cycle-hide-drawers 'overview)))))
  6367. (defun org-cycle-show-empty-lines (state)
  6368. "Show empty lines above all visible headlines.
  6369. The region to be covered depends on STATE when called through
  6370. `org-cycle-hook'. Lisp program can use t for STATE to get the
  6371. entire buffer covered. Note that an empty line is only shown if there
  6372. are at least `org-cycle-separator-lines' empty lines before the headline."
  6373. (when (/= org-cycle-separator-lines 0)
  6374. (save-excursion
  6375. (let* ((n (abs org-cycle-separator-lines))
  6376. (re (cond
  6377. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  6378. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  6379. (t (let ((ns (number-to-string (- n 2))))
  6380. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  6381. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  6382. beg end)
  6383. (cond
  6384. ((memq state '(overview contents t))
  6385. (setq beg (point-min) end (point-max)))
  6386. ((memq state '(children folded))
  6387. (setq beg (point)
  6388. end (progn (org-end-of-subtree t t)
  6389. (line-beginning-position 2)))))
  6390. (when beg
  6391. (goto-char beg)
  6392. (while (re-search-forward re end t)
  6393. (unless (get-char-property (match-end 1) 'invisible)
  6394. (let ((e (match-end 1))
  6395. (b (if (>= org-cycle-separator-lines 0)
  6396. (match-beginning 1)
  6397. (save-excursion
  6398. (goto-char (match-beginning 0))
  6399. (skip-chars-backward " \t\n")
  6400. (line-end-position)))))
  6401. (outline-flag-region b e nil))))))))
  6402. ;; Never hide empty lines at the end of the file.
  6403. (save-excursion
  6404. (goto-char (point-max))
  6405. (outline-previous-heading)
  6406. (outline-end-of-heading)
  6407. (when (and (looking-at "[ \t\n]+")
  6408. (= (match-end 0) (point-max)))
  6409. (outline-flag-region (point) (match-end 0) nil))))
  6410. (defun org-show-empty-lines-in-parent ()
  6411. "Move to the parent and re-show empty lines before visible headlines."
  6412. (save-excursion
  6413. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  6414. (org-cycle-show-empty-lines context))))
  6415. (defun org-files-list ()
  6416. "Return `org-agenda-files' list, plus all open org-mode files.
  6417. This is useful for operations that need to scan all of a user's
  6418. open and agenda-wise Org files."
  6419. (let ((files (mapcar 'expand-file-name (org-agenda-files))))
  6420. (dolist (buf (buffer-list))
  6421. (with-current-buffer buf
  6422. (when (and (derived-mode-p 'org-mode) (buffer-file-name))
  6423. (cl-pushnew (expand-file-name (buffer-file-name)) files))))
  6424. files))
  6425. (defsubst org-entry-beginning-position ()
  6426. "Return the beginning position of the current entry."
  6427. (save-excursion (org-back-to-heading t) (point)))
  6428. (defsubst org-entry-end-position ()
  6429. "Return the end position of the current entry."
  6430. (save-excursion (outline-next-heading) (point)))
  6431. (defun org-cycle-hide-drawers (state &optional exceptions)
  6432. "Re-hide all drawers after a visibility state change.
  6433. When non-nil, optional argument EXCEPTIONS is a list of strings
  6434. specifying which drawers should not be hidden."
  6435. (when (and (derived-mode-p 'org-mode)
  6436. (not (memq state '(overview folded contents))))
  6437. (save-excursion
  6438. (let* ((globalp (memq state '(contents all)))
  6439. (beg (if globalp (point-min) (point)))
  6440. (end (if globalp (point-max)
  6441. (if (eq state 'children)
  6442. (save-excursion (outline-next-heading) (point))
  6443. (org-end-of-subtree t)))))
  6444. (goto-char beg)
  6445. (while (re-search-forward org-drawer-regexp (max end (point)) t)
  6446. (unless (member-ignore-case (match-string 1) exceptions)
  6447. (let ((drawer (org-element-at-point)))
  6448. (when (memq (org-element-type drawer) '(drawer property-drawer))
  6449. (org-flag-drawer t drawer)
  6450. ;; Make sure to skip drawer entirely or we might flag
  6451. ;; it another time when matching its ending line with
  6452. ;; `org-drawer-regexp'.
  6453. (goto-char (org-element-property :end drawer))))))))))
  6454. (defun org-flag-drawer (flag &optional element)
  6455. "When FLAG is non-nil, hide the drawer we are at.
  6456. Otherwise make it visible. When optional argument ELEMENT is
  6457. a parsed drawer, as returned by `org-element-at-point', hide or
  6458. show that drawer instead."
  6459. (let ((drawer (or element
  6460. (and (save-excursion
  6461. (beginning-of-line)
  6462. (org-looking-at-p org-drawer-regexp))
  6463. (org-element-at-point)))))
  6464. (when (memq (org-element-type drawer) '(drawer property-drawer))
  6465. (let ((post (org-element-property :post-affiliated drawer)))
  6466. (save-excursion
  6467. (outline-flag-region
  6468. (progn (goto-char post) (line-end-position))
  6469. (progn (goto-char (org-element-property :end drawer))
  6470. (skip-chars-backward " \r\t\n")
  6471. (line-end-position))
  6472. flag))
  6473. ;; When the drawer is hidden away, make sure point lies in
  6474. ;; a visible part of the buffer.
  6475. (when (and flag (> (line-beginning-position) post))
  6476. (goto-char post))))))
  6477. (defun org-subtree-end-visible-p ()
  6478. "Is the end of the current subtree visible?"
  6479. (pos-visible-in-window-p
  6480. (save-excursion (org-end-of-subtree t) (point))))
  6481. (defun org-first-headline-recenter ()
  6482. "Move cursor to the first headline and recenter the headline."
  6483. (let ((window (get-buffer-window)))
  6484. (when window
  6485. (goto-char (point-min))
  6486. (when (re-search-forward (concat "^\\(" org-outline-regexp "\\)") nil t)
  6487. (set-window-start window (line-beginning-position))))))
  6488. ;;; Saving and restoring visibility
  6489. (defun org-outline-overlay-data (&optional use-markers)
  6490. "Return a list of the locations of all outline overlays.
  6491. These are overlays with the `invisible' property value `outline'.
  6492. The return value is a list of cons cells, with start and stop
  6493. positions for each overlay.
  6494. If USE-MARKERS is set, return the positions as markers."
  6495. (let (beg end)
  6496. (save-excursion
  6497. (save-restriction
  6498. (widen)
  6499. (delq nil
  6500. (mapcar (lambda (o)
  6501. (when (eq (overlay-get o 'invisible) 'outline)
  6502. (setq beg (overlay-start o)
  6503. end (overlay-end o))
  6504. (and beg end (> end beg)
  6505. (if use-markers
  6506. (cons (copy-marker beg)
  6507. (copy-marker end t))
  6508. (cons beg end)))))
  6509. (overlays-in (point-min) (point-max))))))))
  6510. (defun org-set-outline-overlay-data (data)
  6511. "Create visibility overlays for all positions in DATA.
  6512. DATA should have been made by `org-outline-overlay-data'."
  6513. (org-with-wide-buffer
  6514. (outline-show-all)
  6515. (dolist (c data) (outline-flag-region (car c) (cdr c) t))))
  6516. ;;; Folding of blocks
  6517. (defvar-local org-hide-block-overlays nil
  6518. "Overlays hiding blocks.")
  6519. (defun org-block-map (function &optional start end)
  6520. "Call FUNCTION at the head of all source blocks in the current buffer.
  6521. Optional arguments START and END can be used to limit the range."
  6522. (let ((start (or start (point-min)))
  6523. (end (or end (point-max))))
  6524. (save-excursion
  6525. (goto-char start)
  6526. (while (and (< (point) end) (re-search-forward org-block-regexp end t))
  6527. (save-excursion
  6528. (save-match-data
  6529. (goto-char (match-beginning 0))
  6530. (funcall function)))))))
  6531. (defun org-hide-block-toggle-all ()
  6532. "Toggle the visibility of all blocks in the current buffer."
  6533. (org-block-map 'org-hide-block-toggle))
  6534. (defun org-hide-block-all ()
  6535. "Fold all blocks in the current buffer."
  6536. (interactive)
  6537. (org-show-block-all)
  6538. (org-block-map 'org-hide-block-toggle-maybe))
  6539. (defun org-show-block-all ()
  6540. "Unfold all blocks in the current buffer."
  6541. (interactive)
  6542. (mapc #'delete-overlay org-hide-block-overlays)
  6543. (setq org-hide-block-overlays nil))
  6544. (defun org-hide-block-toggle-maybe ()
  6545. "Toggle visibility of block at point.
  6546. Unlike to `org-hide-block-toggle', this function does not throw
  6547. an error. Return a non-nil value when toggling is successful."
  6548. (interactive)
  6549. (ignore-errors (org-hide-block-toggle)))
  6550. (defun org-hide-block-toggle (&optional force)
  6551. "Toggle the visibility of the current block.
  6552. When optional argument FORCE is `off', make block visible. If it
  6553. is non-nil, hide it unconditionally. Throw an error when not at
  6554. a block. Return a non-nil value when toggling is successful."
  6555. (interactive)
  6556. (let ((element (org-element-at-point)))
  6557. (unless (memq (org-element-type element)
  6558. '(center-block comment-block dynamic-block example-block
  6559. export-block quote-block special-block
  6560. src-block verse-block))
  6561. (user-error "Not at a block"))
  6562. (let* ((start (save-excursion
  6563. (goto-char (org-element-property :post-affiliated element))
  6564. (line-end-position)))
  6565. (end (save-excursion
  6566. (goto-char (org-element-property :end element))
  6567. (skip-chars-backward " \r\t\n")
  6568. (line-end-position)))
  6569. (overlays (overlays-at start)))
  6570. (cond
  6571. ;; Do nothing when not before or at the block opening line or
  6572. ;; at the block closing line.
  6573. ((let ((eol (line-end-position))) (and (> eol start) (/= eol end))) nil)
  6574. ((and (not (eq force 'off))
  6575. (not (memq t (mapcar
  6576. (lambda (o)
  6577. (eq (overlay-get o 'invisible) 'org-hide-block))
  6578. overlays))))
  6579. (let ((ov (make-overlay start end)))
  6580. (overlay-put ov 'invisible 'org-hide-block)
  6581. ;; Make the block accessible to `isearch'.
  6582. (overlay-put
  6583. ov 'isearch-open-invisible
  6584. (lambda (ov)
  6585. (when (memq ov org-hide-block-overlays)
  6586. (setq org-hide-block-overlays (delq ov org-hide-block-overlays)))
  6587. (when (eq (overlay-get ov 'invisible) 'org-hide-block)
  6588. (delete-overlay ov))))
  6589. (push ov org-hide-block-overlays)
  6590. ;; When the block is hidden away, make sure point is left in
  6591. ;; a visible part of the buffer.
  6592. (when (> (line-beginning-position) start)
  6593. (goto-char start)
  6594. (beginning-of-line))
  6595. ;; Signal successful toggling.
  6596. t))
  6597. ((or (not force) (eq force 'off))
  6598. (dolist (ov overlays t)
  6599. (when (memq ov org-hide-block-overlays)
  6600. (setq org-hide-block-overlays (delq ov org-hide-block-overlays)))
  6601. (when (eq (overlay-get ov 'invisible) 'org-hide-block)
  6602. (delete-overlay ov))))))))
  6603. ;; org-tab-after-check-for-cycling-hook
  6604. (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
  6605. ;; Remove overlays when changing major mode
  6606. (add-hook 'org-mode-hook
  6607. (lambda () (org-add-hook 'change-major-mode-hook
  6608. 'org-show-block-all 'append 'local)))
  6609. ;;; Org-goto
  6610. (defvar org-goto-window-configuration nil)
  6611. (defvar org-goto-marker nil)
  6612. (defvar org-goto-map)
  6613. (defun org-goto-map ()
  6614. "Set the keymap `org-goto'."
  6615. (setq org-goto-map
  6616. (let ((map (make-sparse-keymap)))
  6617. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command
  6618. mouse-drag-region universal-argument org-occur)))
  6619. (dolist (cmd cmds)
  6620. (substitute-key-definition cmd cmd map global-map)))
  6621. (suppress-keymap map)
  6622. (org-defkey map "\C-m" 'org-goto-ret)
  6623. (org-defkey map [(return)] 'org-goto-ret)
  6624. (org-defkey map [(left)] 'org-goto-left)
  6625. (org-defkey map [(right)] 'org-goto-right)
  6626. (org-defkey map [(control ?g)] 'org-goto-quit)
  6627. (org-defkey map "\C-i" 'org-cycle)
  6628. (org-defkey map [(tab)] 'org-cycle)
  6629. (org-defkey map [(down)] 'outline-next-visible-heading)
  6630. (org-defkey map [(up)] 'outline-previous-visible-heading)
  6631. (if org-goto-auto-isearch
  6632. (if (fboundp 'define-key-after)
  6633. (define-key-after map [t] 'org-goto-local-auto-isearch)
  6634. nil)
  6635. (org-defkey map "q" 'org-goto-quit)
  6636. (org-defkey map "n" 'outline-next-visible-heading)
  6637. (org-defkey map "p" 'outline-previous-visible-heading)
  6638. (org-defkey map "f" 'outline-forward-same-level)
  6639. (org-defkey map "b" 'outline-backward-same-level)
  6640. (org-defkey map "u" 'outline-up-heading))
  6641. (org-defkey map "/" 'org-occur)
  6642. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  6643. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  6644. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  6645. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  6646. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  6647. map)))
  6648. (defconst org-goto-help
  6649. "Browse buffer copy, to find location or copy text.%s
  6650. RET=jump to location C-g=quit and return to previous location
  6651. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  6652. (defvar org-goto-start-pos) ; dynamically scoped parameter
  6653. (defun org-goto (&optional alternative-interface)
  6654. "Look up a different location in the current file, keeping current visibility.
  6655. When you want look-up or go to a different location in a
  6656. document, the fastest way is often to fold the entire buffer and
  6657. then dive into the tree. This method has the disadvantage, that
  6658. the previous location will be folded, which may not be what you
  6659. want.
  6660. This command works around this by showing a copy of the current
  6661. buffer in an indirect buffer, in overview mode. You can dive
  6662. into the tree in that copy, use org-occur and incremental search
  6663. to find a location. When pressing RET or `Q', the command
  6664. returns to the original buffer in which the visibility is still
  6665. unchanged. After RET it will also jump to the location selected
  6666. in the indirect buffer and expose the headline hierarchy above.
  6667. With a prefix argument, use the alternative interface: e.g., if
  6668. `org-goto-interface' is `outline' use `outline-path-completion'."
  6669. (interactive "P")
  6670. (org-goto-map)
  6671. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  6672. (org-refile-use-outline-path t)
  6673. (org-refile-target-verify-function nil)
  6674. (interface
  6675. (if (not alternative-interface)
  6676. org-goto-interface
  6677. (if (eq org-goto-interface 'outline)
  6678. 'outline-path-completion
  6679. 'outline)))
  6680. (org-goto-start-pos (point))
  6681. (selected-point
  6682. (if (eq interface 'outline)
  6683. (car (org-get-location (current-buffer) org-goto-help))
  6684. (let ((pa (org-refile-get-location "Goto")))
  6685. (org-refile-check-position pa)
  6686. (nth 3 pa)))))
  6687. (if selected-point
  6688. (progn
  6689. (org-mark-ring-push org-goto-start-pos)
  6690. (goto-char selected-point)
  6691. (when (or (outline-invisible-p) (org-invisible-p2))
  6692. (org-show-context 'org-goto)))
  6693. (message "Quit"))))
  6694. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  6695. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  6696. (defvar org-goto-local-auto-isearch-map) ; defined below
  6697. (defun org-get-location (_buf help)
  6698. "Let the user select a location in current buffer.
  6699. This function uses a recursive edit. It returns the selected position
  6700. or nil."
  6701. (org-no-popups
  6702. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  6703. (isearch-hide-immediately nil)
  6704. (isearch-search-fun-function
  6705. (lambda () 'org-goto-local-search-headings))
  6706. (org-goto-selected-point org-goto-exit-command))
  6707. (save-excursion
  6708. (save-window-excursion
  6709. (delete-other-windows)
  6710. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  6711. (org-pop-to-buffer-same-window
  6712. (condition-case nil
  6713. (make-indirect-buffer (current-buffer) "*org-goto*")
  6714. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  6715. (with-output-to-temp-buffer "*Org Help*"
  6716. (princ (format help (if org-goto-auto-isearch
  6717. " Just type for auto-isearch."
  6718. " n/p/f/b/u to navigate, q to quit."))))
  6719. (org-fit-window-to-buffer (get-buffer-window "*Org Help*"))
  6720. (setq buffer-read-only nil)
  6721. (let ((org-startup-truncated t)
  6722. (org-startup-folded nil)
  6723. (org-startup-align-all-tables nil))
  6724. (org-mode)
  6725. (org-overview))
  6726. (setq buffer-read-only t)
  6727. (if (and (boundp 'org-goto-start-pos)
  6728. (integer-or-marker-p org-goto-start-pos))
  6729. (progn (goto-char org-goto-start-pos)
  6730. (when (outline-invisible-p)
  6731. (org-show-set-visibility 'lineage)))
  6732. (goto-char (point-min)))
  6733. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  6734. (message "Select location and press RET")
  6735. (use-local-map org-goto-map)
  6736. (recursive-edit)))
  6737. (kill-buffer "*org-goto*")
  6738. (cons org-goto-selected-point org-goto-exit-command))))
  6739. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  6740. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  6741. ;; `isearch-other-control-char' was removed in Emacs 24.4.
  6742. (if (fboundp 'isearch-other-control-char)
  6743. (progn
  6744. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  6745. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char))
  6746. (define-key org-goto-local-auto-isearch-map "\C-i" nil)
  6747. (define-key org-goto-local-auto-isearch-map "\C-m" nil)
  6748. (define-key org-goto-local-auto-isearch-map [return] nil))
  6749. (defun org-goto-local-search-headings (string bound noerror)
  6750. "Search and make sure that any matches are in headlines."
  6751. (catch 'return
  6752. (while (if isearch-forward
  6753. (search-forward string bound noerror)
  6754. (search-backward string bound noerror))
  6755. (when (save-match-data
  6756. (and (save-excursion
  6757. (beginning-of-line)
  6758. (looking-at org-complex-heading-regexp))
  6759. (or (not (match-beginning 5))
  6760. (< (point) (match-beginning 5)))))
  6761. (throw 'return (point))))))
  6762. (defun org-goto-local-auto-isearch ()
  6763. "Start isearch."
  6764. (interactive)
  6765. (goto-char (point-min))
  6766. (let ((keys (this-command-keys)))
  6767. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  6768. (isearch-mode t)
  6769. (isearch-process-search-char (string-to-char keys)))))
  6770. (defun org-goto-ret (&optional _arg)
  6771. "Finish `org-goto' by going to the new location."
  6772. (interactive "P")
  6773. (setq org-goto-selected-point (point))
  6774. (setq org-goto-exit-command 'return)
  6775. (throw 'exit nil))
  6776. (defun org-goto-left ()
  6777. "Finish `org-goto' by going to the new location."
  6778. (interactive)
  6779. (if (org-at-heading-p)
  6780. (progn
  6781. (beginning-of-line 1)
  6782. (setq org-goto-selected-point (point)
  6783. org-goto-exit-command 'left)
  6784. (throw 'exit nil))
  6785. (user-error "Not on a heading")))
  6786. (defun org-goto-right ()
  6787. "Finish `org-goto' by going to the new location."
  6788. (interactive)
  6789. (if (org-at-heading-p)
  6790. (progn
  6791. (setq org-goto-selected-point (point)
  6792. org-goto-exit-command 'right)
  6793. (throw 'exit nil))
  6794. (user-error "Not on a heading")))
  6795. (defun org-goto-quit ()
  6796. "Finish `org-goto' without cursor motion."
  6797. (interactive)
  6798. (setq org-goto-selected-point nil)
  6799. (setq org-goto-exit-command 'quit)
  6800. (throw 'exit nil))
  6801. ;;; Indirect buffer display of subtrees
  6802. (defvar org-indirect-dedicated-frame nil
  6803. "This is the frame being used for indirect tree display.")
  6804. (defvar org-last-indirect-buffer nil)
  6805. (defun org-tree-to-indirect-buffer (&optional arg)
  6806. "Create indirect buffer and narrow it to current subtree.
  6807. With a numerical prefix ARG, go up to this level and then take that tree.
  6808. If ARG is negative, go up that many levels.
  6809. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  6810. indirect buffer previously made with this command, to avoid proliferation of
  6811. indirect buffers. However, when you call the command with a \
  6812. \\[universal-argument] prefix, or
  6813. when `org-indirect-buffer-display' is `new-frame', the last buffer
  6814. is kept so that you can work with several indirect buffers at the same time.
  6815. If `org-indirect-buffer-display' is `dedicated-frame', the \
  6816. \\[universal-argument] prefix also
  6817. requests that a new frame be made for the new buffer, so that the dedicated
  6818. frame is not changed."
  6819. (interactive "P")
  6820. (let ((cbuf (current-buffer))
  6821. (cwin (selected-window))
  6822. (pos (point))
  6823. beg end level heading ibuf)
  6824. (save-excursion
  6825. (org-back-to-heading t)
  6826. (when (numberp arg)
  6827. (setq level (org-outline-level))
  6828. (when (< arg 0) (setq arg (+ level arg)))
  6829. (while (> (setq level (org-outline-level)) arg)
  6830. (org-up-heading-safe)))
  6831. (setq beg (point)
  6832. heading (org-get-heading 'no-tags))
  6833. (org-end-of-subtree t t)
  6834. (when (org-at-heading-p) (backward-char 1))
  6835. (setq end (point)))
  6836. (when (and (buffer-live-p org-last-indirect-buffer)
  6837. (not (eq org-indirect-buffer-display 'new-frame))
  6838. (not arg))
  6839. (kill-buffer org-last-indirect-buffer))
  6840. (setq ibuf (org-get-indirect-buffer cbuf heading)
  6841. org-last-indirect-buffer ibuf)
  6842. (cond
  6843. ((or (eq org-indirect-buffer-display 'new-frame)
  6844. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  6845. (select-frame (make-frame))
  6846. (delete-other-windows)
  6847. (org-pop-to-buffer-same-window ibuf)
  6848. (org-set-frame-title heading))
  6849. ((eq org-indirect-buffer-display 'dedicated-frame)
  6850. (raise-frame
  6851. (select-frame (or (and org-indirect-dedicated-frame
  6852. (frame-live-p org-indirect-dedicated-frame)
  6853. org-indirect-dedicated-frame)
  6854. (setq org-indirect-dedicated-frame (make-frame)))))
  6855. (delete-other-windows)
  6856. (org-pop-to-buffer-same-window ibuf)
  6857. (org-set-frame-title (concat "Indirect: " heading)))
  6858. ((eq org-indirect-buffer-display 'current-window)
  6859. (org-pop-to-buffer-same-window ibuf))
  6860. ((eq org-indirect-buffer-display 'other-window)
  6861. (pop-to-buffer ibuf))
  6862. (t (error "Invalid value")))
  6863. (when (featurep 'xemacs)
  6864. (save-excursion (org-mode) (turn-on-font-lock)))
  6865. (narrow-to-region beg end)
  6866. (outline-show-all)
  6867. (goto-char pos)
  6868. (run-hook-with-args 'org-cycle-hook 'all)
  6869. (and (window-live-p cwin) (select-window cwin))))
  6870. (defun org-get-indirect-buffer (&optional buffer heading)
  6871. (setq buffer (or buffer (current-buffer)))
  6872. (let ((n 1) (base (buffer-name buffer)) bname)
  6873. (while (buffer-live-p
  6874. (get-buffer
  6875. (setq bname
  6876. (concat base "-"
  6877. (if heading (concat heading "-" (number-to-string n))
  6878. (number-to-string n))))))
  6879. (setq n (1+ n)))
  6880. (condition-case nil
  6881. (make-indirect-buffer buffer bname 'clone)
  6882. (error (make-indirect-buffer buffer bname)))))
  6883. (defun org-set-frame-title (title)
  6884. "Set the title of the current frame to the string TITLE."
  6885. ;; FIXME: how to name a single frame in XEmacs???
  6886. (unless (featurep 'xemacs)
  6887. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  6888. ;;;; Structure editing
  6889. ;;; Inserting headlines
  6890. (defun org--line-empty-p (n)
  6891. "Is the Nth next line empty?
  6892. Counts the current line as N = 1 and the previous line as N = 0;
  6893. see `beginning-of-line'."
  6894. (save-excursion
  6895. (and (not (bobp))
  6896. (or (beginning-of-line n) t)
  6897. (save-match-data
  6898. (looking-at "[ \t]*$")))))
  6899. (defun org-previous-line-empty-p ()
  6900. "Is the previous line a blank line?
  6901. When NEXT is non-nil, check the next line instead."
  6902. (org--line-empty-p 0))
  6903. (defun org-next-line-empty-p ()
  6904. "Is the previous line a blank line?
  6905. When NEXT is non-nil, check the next line instead."
  6906. (org--line-empty-p 2))
  6907. (defun org-insert-heading (&optional arg invisible-ok top)
  6908. "Insert a new heading or an item with the same depth at point.
  6909. If point is at the beginning of a heading or a list item, insert
  6910. a new heading or a new item above the current one. If point is
  6911. at the beginning of a normal line, turn the line into a heading.
  6912. If point is in the middle of a headline or a list item, split the
  6913. headline or the item and create a new headline/item with the text
  6914. in the current line after point \(see `org-M-RET-may-split-line'
  6915. on how to modify this behavior).
  6916. With one universal prefix argument, set the user option
  6917. `org-insert-heading-respect-content' to t for the duration of
  6918. the command. This modifies the behavior described above in this
  6919. ways: on list items and at the beginning of normal lines, force
  6920. the insertion of a heading after the current subtree.
  6921. With two universal prefix arguments, insert the heading at the
  6922. end of the grandparent subtree. For example, if point is within
  6923. a 2nd-level heading, then it will insert a 2nd-level heading at
  6924. the end of the 1st-level parent heading.
  6925. If point is at the beginning of a headline, insert a sibling
  6926. before the current headline. If point is not at the beginning,
  6927. split the line and create a new headline with the text in the
  6928. current line after point \(see `org-M-RET-may-split-line' on how
  6929. to modify this behavior).
  6930. If point is at the beginning of a normal line, turn this line
  6931. into a heading.
  6932. When INVISIBLE-OK is set, stop at invisible headlines when going
  6933. back. This is important for non-interactive uses of the
  6934. command.
  6935. When optional argument TOP is non-nil, insert a level 1 heading,
  6936. unconditionally."
  6937. (interactive "P")
  6938. (when (org-called-interactively-p 'any) (org-reveal))
  6939. (let ((itemp (and (not top) (org-in-item-p)))
  6940. (may-split (org-get-alist-option org-M-RET-may-split-line 'headline))
  6941. (respect-content (or org-insert-heading-respect-content
  6942. (equal arg '(4))))
  6943. (initial-content ""))
  6944. (cond
  6945. ((or (= (buffer-size) 0)
  6946. (and (not (save-excursion
  6947. (and (ignore-errors (org-back-to-heading invisible-ok))
  6948. (org-at-heading-p))))
  6949. (or arg (not itemp))))
  6950. ;; At beginning of buffer or so high up that only a heading
  6951. ;; makes sense.
  6952. (cond ((and (bolp) (not respect-content)) (insert "* "))
  6953. ((not respect-content)
  6954. (unless may-split (end-of-line))
  6955. (insert "\n* "))
  6956. ((re-search-forward org-outline-regexp-bol nil t)
  6957. (beginning-of-line)
  6958. (insert "* \n")
  6959. (backward-char))
  6960. (t (goto-char (point-max))
  6961. (insert "\n* ")))
  6962. (run-hooks 'org-insert-heading-hook))
  6963. ((and itemp (not (member arg '((4) (16)))) (org-insert-item)))
  6964. (t
  6965. ;; Maybe move at the end of the subtree
  6966. (when (equal arg '(16))
  6967. (org-up-heading-safe)
  6968. (org-end-of-subtree t))
  6969. ;; Insert a heading
  6970. (save-restriction
  6971. (widen)
  6972. (let* ((level nil)
  6973. (on-heading (org-at-heading-p))
  6974. (empty-line-p (if on-heading
  6975. (org-previous-line-empty-p)
  6976. ;; We will decide later
  6977. nil))
  6978. ;; Get a level string to fall back on.
  6979. (fix-level
  6980. (if (org-before-first-heading-p) "*"
  6981. (save-excursion
  6982. (org-back-to-heading t)
  6983. (when (org-previous-line-empty-p) (setq empty-line-p t))
  6984. (looking-at org-outline-regexp)
  6985. (make-string (1- (length (match-string 0))) ?*))))
  6986. (stars
  6987. (save-excursion
  6988. (condition-case nil
  6989. (if top "* "
  6990. (org-back-to-heading invisible-ok)
  6991. (when (and (not on-heading)
  6992. (featurep 'org-inlinetask)
  6993. (integerp org-inlinetask-min-level)
  6994. (>= (length (match-string 0))
  6995. org-inlinetask-min-level))
  6996. ;; Find a heading level before the inline
  6997. ;; task.
  6998. (while (and (setq level (org-up-heading-safe))
  6999. (>= level org-inlinetask-min-level)))
  7000. (if (org-at-heading-p)
  7001. (org-back-to-heading invisible-ok)
  7002. (error "This should not happen")))
  7003. (unless (and (save-excursion
  7004. (save-match-data
  7005. (org-backward-heading-same-level
  7006. 1 invisible-ok))
  7007. (= (point) (match-beginning 0)))
  7008. (not (org-next-line-empty-p)))
  7009. (setq empty-line-p (or empty-line-p
  7010. (org-previous-line-empty-p))))
  7011. (match-string 0))
  7012. (error (or fix-level "* ")))))
  7013. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  7014. (blank (if (eq blank-a 'auto) empty-line-p blank-a)))
  7015. ;; If we insert after content, move there and clean up
  7016. ;; whitespace.
  7017. (when (and respect-content
  7018. (not (org-looking-at-p org-outline-regexp-bol)))
  7019. (if (not (org-before-first-heading-p))
  7020. (org-end-of-subtree nil t)
  7021. (re-search-forward org-outline-regexp-bol)
  7022. (beginning-of-line 0))
  7023. (skip-chars-backward " \r\t\n")
  7024. (and (not (org-looking-back "^\\*+" (line-beginning-position)))
  7025. (looking-at "[ \t]+") (replace-match ""))
  7026. (unless (eobp) (forward-char 1))
  7027. (when (looking-at "^\\*")
  7028. (unless (bobp) (backward-char 1))
  7029. (insert "\n")))
  7030. ;; If we are splitting, grab the text that should be moved
  7031. ;; to the new headline.
  7032. (when may-split
  7033. (if (org-at-heading-p)
  7034. ;; This is a heading: split intelligently (keeping
  7035. ;; tags).
  7036. (let ((pos (point)))
  7037. (beginning-of-line)
  7038. (unless (looking-at org-complex-heading-regexp)
  7039. (error "This should not happen"))
  7040. (when (and (match-beginning 4)
  7041. (> pos (match-beginning 4))
  7042. (< pos (match-end 4)))
  7043. (setq initial-content (buffer-substring pos (match-end 4)))
  7044. (goto-char pos)
  7045. (delete-region (point) (match-end 4))
  7046. (if (looking-at "[ \t]*$")
  7047. (replace-match "")
  7048. (insert (make-string (length initial-content) ?\s)))
  7049. (setq initial-content (org-trim initial-content)))
  7050. (goto-char pos))
  7051. ;; A normal line.
  7052. (setq initial-content
  7053. (org-trim
  7054. (delete-and-extract-region (point) (line-end-position))))))
  7055. ;; If we are at the beginning of the line, insert before it.
  7056. ;; Otherwise, after it.
  7057. (cond
  7058. ((and (bolp) (looking-at "[ \t]*$")))
  7059. ((bolp) (save-excursion (insert "\n")))
  7060. (t (end-of-line)
  7061. (insert "\n")))
  7062. ;; Insert the new heading
  7063. (insert stars)
  7064. (just-one-space)
  7065. (insert initial-content)
  7066. (unless (and blank (org-previous-line-empty-p))
  7067. (org-N-empty-lines-before-current (if blank 1 0)))
  7068. ;; Adjust visibility, which may be messed up if we removed
  7069. ;; blank lines while previous entry was hidden.
  7070. (let ((bol (line-beginning-position)))
  7071. (dolist (o (overlays-at (1- bol)))
  7072. (when (and (eq (overlay-get o 'invisible) 'outline)
  7073. (eq (overlay-end o) bol))
  7074. (move-overlay o (overlay-start o) (1- bol)))))
  7075. (run-hooks 'org-insert-heading-hook)))))))
  7076. (defun org-N-empty-lines-before-current (N)
  7077. "Make the number of empty lines before current exactly N.
  7078. So this will delete or add empty lines."
  7079. (save-excursion
  7080. (beginning-of-line)
  7081. (let ((p (point)))
  7082. (skip-chars-backward " \r\t\n")
  7083. (unless (bolp) (forward-line))
  7084. (delete-region (point) p))
  7085. (when (> N 0) (insert (make-string N ?\n)))))
  7086. (defun org-get-heading (&optional no-tags no-todo)
  7087. "Return the heading of the current entry, without the stars.
  7088. When NO-TAGS is non-nil, don't include tags.
  7089. When NO-TODO is non-nil, don't include TODO keywords."
  7090. (save-excursion
  7091. (org-back-to-heading t)
  7092. (cond
  7093. ((and no-tags no-todo)
  7094. (looking-at org-complex-heading-regexp)
  7095. (match-string 4))
  7096. (no-tags
  7097. (looking-at (concat org-outline-regexp
  7098. "\\(.*?\\)"
  7099. "\\(?:[ \t]+:[[:alnum:]:_@#%]+:\\)?[ \t]*$"))
  7100. (match-string 1))
  7101. (no-todo
  7102. (looking-at org-todo-line-regexp)
  7103. (match-string 3))
  7104. (t (looking-at org-heading-regexp)
  7105. (match-string 2)))))
  7106. (defvar orgstruct-mode) ; defined below
  7107. (defun org-heading-components ()
  7108. "Return the components of the current heading.
  7109. This is a list with the following elements:
  7110. - the level as an integer
  7111. - the reduced level, different if `org-odd-levels-only' is set.
  7112. - the TODO keyword, or nil
  7113. - the priority character, like ?A, or nil if no priority is given
  7114. - the headline text itself, or the tags string if no headline text
  7115. - the tags string, or nil."
  7116. (save-excursion
  7117. (org-back-to-heading t)
  7118. (when (let (case-fold-search)
  7119. (looking-at
  7120. (if orgstruct-mode
  7121. org-heading-regexp
  7122. org-complex-heading-regexp)))
  7123. (if orgstruct-mode
  7124. (list (length (match-string 1))
  7125. (org-reduced-level (length (match-string 1)))
  7126. nil
  7127. nil
  7128. (match-string 2)
  7129. nil)
  7130. (list (length (match-string 1))
  7131. (org-reduced-level (length (match-string 1)))
  7132. (org-match-string-no-properties 2)
  7133. (and (match-end 3) (aref (match-string 3) 2))
  7134. (org-match-string-no-properties 4)
  7135. (org-match-string-no-properties 5))))))
  7136. (defun org-get-entry ()
  7137. "Get the entry text, after heading, entire subtree."
  7138. (save-excursion
  7139. (org-back-to-heading t)
  7140. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  7141. (defun org-edit-headline (&optional heading)
  7142. "Edit the current headline.
  7143. Set it to HEADING when provided."
  7144. (interactive)
  7145. (org-with-wide-buffer
  7146. (org-back-to-heading t)
  7147. (when (looking-at org-complex-heading-regexp)
  7148. (let* ((old (match-string-no-properties 4))
  7149. (new (org-trim (or heading (read-string "Edit: " old)))))
  7150. (unless (equal old new)
  7151. (if old (replace-match new t t nil 4)
  7152. (goto-char (or (match-end 3) (match-end 2) (match-end 1)))
  7153. (insert " " new))
  7154. (org-set-tags nil t)
  7155. (when (looking-at "[ \t]*$") (replace-match "")))))))
  7156. (defun org-insert-heading-after-current ()
  7157. "Insert a new heading with same level as current, after current subtree."
  7158. (interactive)
  7159. (org-back-to-heading)
  7160. (org-insert-heading)
  7161. (org-move-subtree-down)
  7162. (end-of-line 1))
  7163. (defun org-insert-heading-respect-content (&optional invisible-ok)
  7164. "Insert heading with `org-insert-heading-respect-content' set to t."
  7165. (interactive)
  7166. (org-insert-heading '(4) invisible-ok))
  7167. (defun org-insert-todo-heading-respect-content (&optional force-state)
  7168. "Insert TODO heading with `org-insert-heading-respect-content' set to t."
  7169. (interactive)
  7170. (org-insert-todo-heading force-state '(4)))
  7171. (defun org-insert-todo-heading (arg &optional force-heading)
  7172. "Insert a new heading with the same level and TODO state as current heading.
  7173. If the heading has no TODO state, or if the state is DONE, use the first
  7174. state (TODO by default). Also with one prefix arg, force first state. With
  7175. two prefix args, force inserting at the end of the parent subtree."
  7176. (interactive "P")
  7177. (when (or force-heading (not (org-insert-item 'checkbox)))
  7178. (org-insert-heading (or (and (equal arg '(16)) '(16))
  7179. force-heading))
  7180. (save-excursion
  7181. (org-back-to-heading)
  7182. (outline-previous-heading)
  7183. (looking-at org-todo-line-regexp))
  7184. (let*
  7185. ((new-mark-x
  7186. (if (or (equal arg '(4))
  7187. (not (match-beginning 2))
  7188. (member (match-string 2) org-done-keywords))
  7189. (car org-todo-keywords-1)
  7190. (match-string 2)))
  7191. (new-mark
  7192. (or
  7193. (run-hook-with-args-until-success
  7194. 'org-todo-get-default-hook new-mark-x nil)
  7195. new-mark-x)))
  7196. (beginning-of-line 1)
  7197. (and (looking-at org-outline-regexp) (goto-char (match-end 0))
  7198. (if org-treat-insert-todo-heading-as-state-change
  7199. (org-todo new-mark)
  7200. (insert new-mark " "))))
  7201. (when org-provide-todo-statistics
  7202. (org-update-parent-todo-statistics))))
  7203. (defun org-insert-subheading (arg)
  7204. "Insert a new subheading and demote it.
  7205. Works for outline headings and for plain lists alike."
  7206. (interactive "P")
  7207. (org-insert-heading arg)
  7208. (cond
  7209. ((org-at-heading-p) (org-do-demote))
  7210. ((org-at-item-p) (org-indent-item))))
  7211. (defun org-insert-todo-subheading (arg)
  7212. "Insert a new subheading with TODO keyword or checkbox and demote it.
  7213. Works for outline headings and for plain lists alike."
  7214. (interactive "P")
  7215. (org-insert-todo-heading arg)
  7216. (cond
  7217. ((org-at-heading-p) (org-do-demote))
  7218. ((org-at-item-p) (org-indent-item))))
  7219. ;;; Promotion and Demotion
  7220. (defvar org-after-demote-entry-hook nil
  7221. "Hook run after an entry has been demoted.
  7222. The cursor will be at the beginning of the entry.
  7223. When a subtree is being demoted, the hook will be called for each node.")
  7224. (defvar org-after-promote-entry-hook nil
  7225. "Hook run after an entry has been promoted.
  7226. The cursor will be at the beginning of the entry.
  7227. When a subtree is being promoted, the hook will be called for each node.")
  7228. (defun org-promote-subtree ()
  7229. "Promote the entire subtree.
  7230. See also `org-promote'."
  7231. (interactive)
  7232. (save-excursion
  7233. (org-with-limited-levels (org-map-tree 'org-promote)))
  7234. (org-fix-position-after-promote))
  7235. (defun org-demote-subtree ()
  7236. "Demote the entire subtree.
  7237. See `org-demote' and `org-promote'."
  7238. (interactive)
  7239. (save-excursion
  7240. (org-with-limited-levels (org-map-tree 'org-demote)))
  7241. (org-fix-position-after-promote))
  7242. (defun org-do-promote ()
  7243. "Promote the current heading higher up the tree.
  7244. If the region is active in `transient-mark-mode', promote all
  7245. headings in the region."
  7246. (interactive)
  7247. (save-excursion
  7248. (if (org-region-active-p)
  7249. (org-map-region 'org-promote (region-beginning) (region-end))
  7250. (org-promote)))
  7251. (org-fix-position-after-promote))
  7252. (defun org-do-demote ()
  7253. "Demote the current heading lower down the tree.
  7254. If the region is active in `transient-mark-mode', demote all
  7255. headings in the region."
  7256. (interactive)
  7257. (save-excursion
  7258. (if (org-region-active-p)
  7259. (org-map-region 'org-demote (region-beginning) (region-end))
  7260. (org-demote)))
  7261. (org-fix-position-after-promote))
  7262. (defun org-fix-position-after-promote ()
  7263. "Fix cursor position and indentation after demoting/promoting."
  7264. (let ((pos (point)))
  7265. (when (save-excursion
  7266. (beginning-of-line 1)
  7267. (looking-at org-todo-line-regexp)
  7268. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  7269. (cond ((eobp) (insert " "))
  7270. ((eolp) (insert " "))
  7271. ((equal (char-after) ?\ ) (forward-char 1))))))
  7272. (defun org-current-level ()
  7273. "Return the level of the current entry, or nil if before the first headline.
  7274. The level is the number of stars at the beginning of the
  7275. headline. Use `org-reduced-level' to remove the effect of
  7276. `org-odd-levels'. Unlike to `org-outline-level', this function
  7277. ignores inlinetasks."
  7278. (let ((level (org-with-limited-levels (org-outline-level))))
  7279. (and (> level 0) level)))
  7280. (defun org-get-previous-line-level ()
  7281. "Return the outline depth of the last headline before the current line.
  7282. Returns 0 for the first headline in the buffer, and nil if before the
  7283. first headline."
  7284. (and (org-current-level)
  7285. (or (and (/= (line-beginning-position) (point-min))
  7286. (save-excursion (beginning-of-line 0) (org-current-level)))
  7287. 0)))
  7288. (defun org-reduced-level (l)
  7289. "Compute the effective level of a heading.
  7290. This takes into account the setting of `org-odd-levels-only'."
  7291. (cond
  7292. ((zerop l) 0)
  7293. (org-odd-levels-only (1+ (floor (/ l 2))))
  7294. (t l)))
  7295. (defun org-level-increment ()
  7296. "Return the number of stars that will be added or removed at a
  7297. time to headlines when structure editing, based on the value of
  7298. `org-odd-levels-only'."
  7299. (if org-odd-levels-only 2 1))
  7300. (defun org-get-valid-level (level &optional change)
  7301. "Rectify a level change under the influence of `org-odd-levels-only'
  7302. LEVEL is a current level, CHANGE is by how much the level should be
  7303. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  7304. even level numbers will become the next higher odd number."
  7305. (if org-odd-levels-only
  7306. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  7307. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  7308. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  7309. (max 1 (+ level (or change 0)))))
  7310. (define-obsolete-function-alias 'org-get-legal-level 'org-get-valid-level "23.1")
  7311. (defun org-promote ()
  7312. "Promote the current heading higher up the tree."
  7313. (org-with-wide-buffer
  7314. (org-back-to-heading t)
  7315. (let* ((after-change-functions (remq 'flyspell-after-change-function
  7316. after-change-functions))
  7317. (level (save-match-data (funcall outline-level)))
  7318. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  7319. (diff (abs (- level (length up-head) -1))))
  7320. (cond
  7321. ((and (= level 1) org-allow-promoting-top-level-subtree)
  7322. (replace-match "# " nil t))
  7323. ((= level 1)
  7324. (user-error "Cannot promote to level 0. UNDO to recover if necessary"))
  7325. (t (replace-match up-head nil t)))
  7326. (unless (= level 1)
  7327. (when org-auto-align-tags (org-set-tags nil 'ignore-column))
  7328. (when org-adapt-indentation (org-fixup-indentation (- diff))))
  7329. (run-hooks 'org-after-promote-entry-hook))))
  7330. (defun org-demote ()
  7331. "Demote the current heading lower down the tree."
  7332. (org-with-wide-buffer
  7333. (org-back-to-heading t)
  7334. (let* ((after-change-functions (remq 'flyspell-after-change-function
  7335. after-change-functions))
  7336. (level (save-match-data (funcall outline-level)))
  7337. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  7338. (diff (abs (- level (length down-head) -1))))
  7339. (replace-match down-head nil t)
  7340. (when org-auto-align-tags (org-set-tags nil 'ignore-column))
  7341. (when org-adapt-indentation (org-fixup-indentation diff))
  7342. (run-hooks 'org-after-demote-entry-hook))))
  7343. (defun org-cycle-level ()
  7344. "Cycle the level of an empty headline through possible states.
  7345. This goes first to child, then to parent, level, then up the hierarchy.
  7346. After top level, it switches back to sibling level."
  7347. (interactive)
  7348. (let ((org-adapt-indentation nil))
  7349. (when (org-point-at-end-of-empty-headline)
  7350. (setq this-command 'org-cycle-level) ; Only needed for caching
  7351. (let ((cur-level (org-current-level))
  7352. (prev-level (org-get-previous-line-level)))
  7353. (cond
  7354. ;; If first headline in file, promote to top-level.
  7355. ((= prev-level 0)
  7356. (cl-loop repeat (/ (- cur-level 1) (org-level-increment))
  7357. do (org-do-promote)))
  7358. ;; If same level as prev, demote one.
  7359. ((= prev-level cur-level)
  7360. (org-do-demote))
  7361. ;; If parent is top-level, promote to top level if not already.
  7362. ((= prev-level 1)
  7363. (cl-loop repeat (/ (- cur-level 1) (org-level-increment))
  7364. do (org-do-promote)))
  7365. ;; If top-level, return to prev-level.
  7366. ((= cur-level 1)
  7367. (cl-loop repeat (/ (- prev-level 1) (org-level-increment))
  7368. do (org-do-demote)))
  7369. ;; If less than prev-level, promote one.
  7370. ((< cur-level prev-level)
  7371. (org-do-promote))
  7372. ;; If deeper than prev-level, promote until higher than
  7373. ;; prev-level.
  7374. ((> cur-level prev-level)
  7375. (cl-loop repeat (+ 1 (/ (- cur-level prev-level) (org-level-increment)))
  7376. do (org-do-promote))))
  7377. t))))
  7378. (defun org-map-tree (fun)
  7379. "Call FUN for every heading underneath the current one."
  7380. (org-back-to-heading)
  7381. (let ((level (funcall outline-level)))
  7382. (save-excursion
  7383. (funcall fun)
  7384. (while (and (progn
  7385. (outline-next-heading)
  7386. (> (funcall outline-level) level))
  7387. (not (eobp)))
  7388. (funcall fun)))))
  7389. (defun org-map-region (fun beg end)
  7390. "Call FUN for every heading between BEG and END."
  7391. (let ((org-ignore-region t))
  7392. (save-excursion
  7393. (setq end (copy-marker end))
  7394. (goto-char beg)
  7395. (when (and (re-search-forward org-outline-regexp-bol nil t)
  7396. (< (point) end))
  7397. (funcall fun))
  7398. (while (and (progn
  7399. (outline-next-heading)
  7400. (< (point) end))
  7401. (not (eobp)))
  7402. (funcall fun)))))
  7403. (defun org-fixup-indentation (diff)
  7404. "Change the indentation in the current entry by DIFF.
  7405. DIFF is an integer. Indentation is done according to the
  7406. following rules:
  7407. - Planning information and property drawers are always indented
  7408. according to the new level of the headline;
  7409. - Footnote definitions and their contents are ignored;
  7410. - Inlinetasks' boundaries are not shifted;
  7411. - Empty lines are ignored;
  7412. - Other lines' indentation are shifted by DIFF columns, unless
  7413. it would introduce a structural change in the document, in
  7414. which case no shifting is done at all.
  7415. Assume point is at a heading or an inlinetask beginning."
  7416. (org-with-wide-buffer
  7417. (narrow-to-region (line-beginning-position)
  7418. (save-excursion
  7419. (if (org-with-limited-levels (org-at-heading-p))
  7420. (org-with-limited-levels (outline-next-heading))
  7421. (org-inlinetask-goto-end))
  7422. (point)))
  7423. (forward-line)
  7424. ;; Indent properly planning info and property drawer.
  7425. (when (org-looking-at-p org-planning-line-re)
  7426. (org-indent-line)
  7427. (forward-line))
  7428. (when (looking-at org-property-drawer-re)
  7429. (goto-char (match-end 0))
  7430. (forward-line)
  7431. (save-excursion (org-indent-region (match-beginning 0) (match-end 0))))
  7432. (catch 'no-shift
  7433. (when (zerop diff) (throw 'no-shift nil))
  7434. ;; If DIFF is negative, first check if a shift is possible at all
  7435. ;; (e.g., it doesn't break structure). This can only happen if
  7436. ;; some contents are not properly indented.
  7437. (let ((case-fold-search t))
  7438. (when (< diff 0)
  7439. (let ((diff (- diff))
  7440. (forbidden-re (concat org-outline-regexp
  7441. "\\|"
  7442. (substring org-footnote-definition-re 1))))
  7443. (save-excursion
  7444. (while (not (eobp))
  7445. (cond
  7446. ((org-looking-at-p "[ \t]*$") (forward-line))
  7447. ((and (org-looking-at-p org-footnote-definition-re)
  7448. (let ((e (org-element-at-point)))
  7449. (and (eq (org-element-type e) 'footnote-definition)
  7450. (goto-char (org-element-property :end e))))))
  7451. ((org-looking-at-p org-outline-regexp) (forward-line))
  7452. ;; Give up if shifting would move before column 0 or
  7453. ;; if it would introduce a headline or a footnote
  7454. ;; definition.
  7455. (t
  7456. (skip-chars-forward " \t")
  7457. (let ((ind (current-column)))
  7458. (when (or (< ind diff)
  7459. (and (= ind diff) (org-looking-at-p forbidden-re)))
  7460. (throw 'no-shift nil)))
  7461. ;; Ignore contents of example blocks and source
  7462. ;; blocks if their indentation is meant to be
  7463. ;; preserved. Jump to block's closing line.
  7464. (beginning-of-line)
  7465. (or (and (org-looking-at-p "[ \t]*#\\+BEGIN_\\(EXAMPLE\\|SRC\\)")
  7466. (let ((e (org-element-at-point)))
  7467. (and (memq (org-element-type e)
  7468. '(example-block src-block))
  7469. (or org-src-preserve-indentation
  7470. (org-element-property :preserve-indent e))
  7471. (goto-char (org-element-property :end e))
  7472. (progn (skip-chars-backward " \r\t\n")
  7473. (beginning-of-line)
  7474. t))))
  7475. (forward-line))))))))
  7476. ;; Shift lines but footnote definitions, inlinetasks boundaries
  7477. ;; by DIFF. Also skip contents of source or example blocks
  7478. ;; when indentation is meant to be preserved.
  7479. (while (not (eobp))
  7480. (cond
  7481. ((and (org-looking-at-p org-footnote-definition-re)
  7482. (let ((e (org-element-at-point)))
  7483. (and (eq (org-element-type e) 'footnote-definition)
  7484. (goto-char (org-element-property :end e))))))
  7485. ((org-looking-at-p org-outline-regexp) (forward-line))
  7486. ((org-looking-at-p "[ \t]*$") (forward-line))
  7487. (t
  7488. (org-indent-line-to (+ (org-get-indentation) diff))
  7489. (beginning-of-line)
  7490. (or (and (org-looking-at-p "[ \t]*#\\+BEGIN_\\(EXAMPLE\\|SRC\\)")
  7491. (let ((e (org-element-at-point)))
  7492. (and (memq (org-element-type e)
  7493. '(example-block src-block))
  7494. (or org-src-preserve-indentation
  7495. (org-element-property :preserve-indent e))
  7496. (goto-char (org-element-property :end e))
  7497. (progn (skip-chars-backward " \r\t\n")
  7498. (beginning-of-line)
  7499. t))))
  7500. (forward-line)))))))))
  7501. (defun org-convert-to-odd-levels ()
  7502. "Convert an org-mode file with all levels allowed to one with odd levels.
  7503. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  7504. level 5 etc."
  7505. (interactive)
  7506. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  7507. (let ((outline-level 'org-outline-level)
  7508. (org-odd-levels-only nil) n)
  7509. (save-excursion
  7510. (goto-char (point-min))
  7511. (while (re-search-forward "^\\*\\*+ " nil t)
  7512. (setq n (- (length (match-string 0)) 2))
  7513. (while (>= (setq n (1- n)) 0)
  7514. (org-demote))
  7515. (end-of-line 1))))))
  7516. (defun org-convert-to-oddeven-levels ()
  7517. "Convert an org-mode file with only odd levels to one with odd/even levels.
  7518. This promotes level 3 to level 2, level 5 to level 3 etc. If the
  7519. file contains a section with an even level, conversion would
  7520. destroy the structure of the file. An error is signaled in this
  7521. case."
  7522. (interactive)
  7523. (goto-char (point-min))
  7524. ;; First check if there are no even levels
  7525. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  7526. (org-show-set-visibility 'canonical)
  7527. (error "Not all levels are odd in this file. Conversion not possible"))
  7528. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  7529. (let ((outline-regexp org-outline-regexp)
  7530. (outline-level 'org-outline-level)
  7531. (org-odd-levels-only nil) n)
  7532. (save-excursion
  7533. (goto-char (point-min))
  7534. (while (re-search-forward "^\\*\\*+ " nil t)
  7535. (setq n (/ (1- (length (match-string 0))) 2))
  7536. (while (>= (setq n (1- n)) 0)
  7537. (org-promote))
  7538. (end-of-line 1))))))
  7539. (defun org-tr-level (n)
  7540. "Make N odd if required."
  7541. (if org-odd-levels-only (1+ (/ n 2)) n))
  7542. ;;; Vertical tree motion, cutting and pasting of subtrees
  7543. (defun org-move-subtree-up (&optional arg)
  7544. "Move the current subtree up past ARG headlines of the same level."
  7545. (interactive "p")
  7546. (org-move-subtree-down (- (prefix-numeric-value arg))))
  7547. (defun org-move-subtree-down (&optional arg)
  7548. "Move the current subtree down past ARG headlines of the same level."
  7549. (interactive "p")
  7550. (setq arg (prefix-numeric-value arg))
  7551. (let ((movfunc (if (> arg 0) 'org-get-next-sibling
  7552. 'org-get-last-sibling))
  7553. (ins-point (make-marker))
  7554. (cnt (abs arg))
  7555. (col (current-column))
  7556. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  7557. ;; Select the tree
  7558. (org-back-to-heading)
  7559. (setq beg0 (point))
  7560. (save-excursion
  7561. (setq ne-beg (org-back-over-empty-lines))
  7562. (setq beg (point)))
  7563. (save-match-data
  7564. (save-excursion (outline-end-of-heading)
  7565. (setq folded (outline-invisible-p)))
  7566. (progn (org-end-of-subtree nil t)
  7567. (unless (eobp) (backward-char))))
  7568. (outline-next-heading)
  7569. (setq ne-end (org-back-over-empty-lines))
  7570. (setq end (point))
  7571. (goto-char beg0)
  7572. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  7573. ;; include less whitespace
  7574. (save-excursion
  7575. (goto-char beg)
  7576. (forward-line (- ne-beg ne-end))
  7577. (setq beg (point))))
  7578. ;; Find insertion point, with error handling
  7579. (while (> cnt 0)
  7580. (or (and (funcall movfunc) (looking-at org-outline-regexp))
  7581. (progn (goto-char beg0)
  7582. (user-error "Cannot move past superior level or buffer limit")))
  7583. (setq cnt (1- cnt)))
  7584. (when (> arg 0)
  7585. ;; Moving forward - still need to move over subtree
  7586. (org-end-of-subtree t t)
  7587. (save-excursion
  7588. (org-back-over-empty-lines)
  7589. (or (bolp) (newline))))
  7590. (setq ne-ins (org-back-over-empty-lines))
  7591. (move-marker ins-point (point))
  7592. (setq txt (buffer-substring beg end))
  7593. (org-save-markers-in-region beg end)
  7594. (delete-region beg end)
  7595. (org-remove-empty-overlays-at beg)
  7596. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  7597. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  7598. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  7599. (let ((bbb (point)))
  7600. (insert-before-markers txt)
  7601. (org-reinstall-markers-in-region bbb)
  7602. (move-marker ins-point bbb))
  7603. (or (bolp) (insert "\n"))
  7604. (setq ins-end (point))
  7605. (goto-char ins-point)
  7606. (org-skip-whitespace)
  7607. (when (and (< arg 0)
  7608. (org-first-sibling-p)
  7609. (> ne-ins ne-beg))
  7610. ;; Move whitespace back to beginning
  7611. (save-excursion
  7612. (goto-char ins-end)
  7613. (let ((kill-whole-line t))
  7614. (kill-line (- ne-ins ne-beg)) (point)))
  7615. (insert (make-string (- ne-ins ne-beg) ?\n)))
  7616. (move-marker ins-point nil)
  7617. (if folded
  7618. (outline-hide-subtree)
  7619. (org-show-entry)
  7620. (org-show-children)
  7621. (org-cycle-hide-drawers 'children))
  7622. (org-clean-visibility-after-subtree-move)
  7623. ;; move back to the initial column we were at
  7624. (move-to-column col)))
  7625. (defvar org-subtree-clip ""
  7626. "Clipboard for cut and paste of subtrees.
  7627. This is actually only a copy of the kill, because we use the normal kill
  7628. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  7629. (defvar org-subtree-clip-folded nil
  7630. "Was the last copied subtree folded?
  7631. This is used to fold the tree back after pasting.")
  7632. (defun org-cut-subtree (&optional n)
  7633. "Cut the current subtree into the clipboard.
  7634. With prefix arg N, cut this many sequential subtrees.
  7635. This is a short-hand for marking the subtree and then cutting it."
  7636. (interactive "p")
  7637. (org-copy-subtree n 'cut))
  7638. (defun org-copy-subtree (&optional n cut force-store-markers nosubtrees)
  7639. "Copy the current subtree it in the clipboard.
  7640. With prefix arg N, copy this many sequential subtrees.
  7641. This is a short-hand for marking the subtree and then copying it.
  7642. If CUT is non-nil, actually cut the subtree.
  7643. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  7644. of some markers in the region, even if CUT is non-nil. This is
  7645. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  7646. (interactive "p")
  7647. (let (beg end folded (beg0 (point)))
  7648. (if (org-called-interactively-p 'any)
  7649. (org-back-to-heading nil) ; take what looks like a subtree
  7650. (org-back-to-heading t)) ; take what is really there
  7651. (setq beg (point))
  7652. (skip-chars-forward " \t\r\n")
  7653. (save-match-data
  7654. (if nosubtrees
  7655. (outline-next-heading)
  7656. (save-excursion (outline-end-of-heading)
  7657. (setq folded (outline-invisible-p)))
  7658. (ignore-errors (org-forward-heading-same-level (1- n) t))
  7659. (org-end-of-subtree t t)))
  7660. ;; Include the end of an inlinetask
  7661. (when (and (featurep 'org-inlinetask)
  7662. (looking-at-p (concat (org-inlinetask-outline-regexp)
  7663. "END[ \t]*$")))
  7664. (end-of-line))
  7665. (setq end (point))
  7666. (goto-char beg0)
  7667. (when (> end beg)
  7668. (setq org-subtree-clip-folded folded)
  7669. (when (or cut force-store-markers)
  7670. (org-save-markers-in-region beg end))
  7671. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  7672. (setq org-subtree-clip (current-kill 0))
  7673. (message "%s: Subtree(s) with %d characters"
  7674. (if cut "Cut" "Copied")
  7675. (length org-subtree-clip)))))
  7676. (defun org-paste-subtree (&optional level tree for-yank remove)
  7677. "Paste the clipboard as a subtree, with modification of headline level.
  7678. The entire subtree is promoted or demoted in order to match a new headline
  7679. level.
  7680. If the cursor is at the beginning of a headline, the same level as
  7681. that headline is used to paste the tree.
  7682. If not, the new level is derived from the *visible* headings
  7683. before and after the insertion point, and taken to be the inferior headline
  7684. level of the two. So if the previous visible heading is level 3 and the
  7685. next is level 4 (or vice versa), level 4 will be used for insertion.
  7686. This makes sure that the subtree remains an independent subtree and does
  7687. not swallow low level entries.
  7688. You can also force a different level, either by using a numeric prefix
  7689. argument, or by inserting the heading marker by hand. For example, if the
  7690. cursor is after \"*****\", then the tree will be shifted to level 5.
  7691. If optional TREE is given, use this text instead of the kill ring.
  7692. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  7693. move back over whitespace before inserting, and move point to the end of
  7694. the inserted text when done.
  7695. When REMOVE is non-nil, remove the subtree from the clipboard."
  7696. (interactive "P")
  7697. (setq tree (or tree (and kill-ring (current-kill 0))))
  7698. (unless (org-kill-is-subtree-p tree)
  7699. (user-error "%s"
  7700. (substitute-command-keys
  7701. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  7702. (org-with-limited-levels
  7703. (let* ((visp (not (outline-invisible-p)))
  7704. (txt tree)
  7705. (^re_ "\\(\\*+\\)[ \t]*")
  7706. (old-level (if (string-match org-outline-regexp-bol txt)
  7707. (- (match-end 0) (match-beginning 0) 1)
  7708. -1))
  7709. (force-level (cond (level (prefix-numeric-value level))
  7710. ((and (looking-at "[ \t]*$")
  7711. (string-match
  7712. "^\\*+$" (buffer-substring
  7713. (point-at-bol) (point))))
  7714. (- (match-end 0) (match-beginning 0)))
  7715. ((and (bolp)
  7716. (looking-at org-outline-regexp))
  7717. (- (match-end 0) (point) 1))))
  7718. (previous-level (save-excursion
  7719. (condition-case nil
  7720. (progn
  7721. (outline-previous-visible-heading 1)
  7722. (if (looking-at ^re_)
  7723. (- (match-end 0) (match-beginning 0) 1)
  7724. 1))
  7725. (error 1))))
  7726. (next-level (save-excursion
  7727. (condition-case nil
  7728. (progn
  7729. (or (looking-at org-outline-regexp)
  7730. (outline-next-visible-heading 1))
  7731. (if (looking-at ^re_)
  7732. (- (match-end 0) (match-beginning 0) 1)
  7733. 1))
  7734. (error 1))))
  7735. (new-level (or force-level (max previous-level next-level)))
  7736. (shift (if (or (= old-level -1)
  7737. (= new-level -1)
  7738. (= old-level new-level))
  7739. 0
  7740. (- new-level old-level)))
  7741. (delta (if (> shift 0) -1 1))
  7742. (func (if (> shift 0) 'org-demote 'org-promote))
  7743. (org-odd-levels-only nil)
  7744. beg end newend)
  7745. ;; Remove the forced level indicator
  7746. (when force-level
  7747. (delete-region (point-at-bol) (point)))
  7748. ;; Paste
  7749. (beginning-of-line (if (bolp) 1 2))
  7750. (setq beg (point))
  7751. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  7752. (insert-before-markers txt)
  7753. (unless (string-match "\n\\'" txt) (insert "\n"))
  7754. (setq newend (point))
  7755. (org-reinstall-markers-in-region beg)
  7756. (setq end (point))
  7757. (goto-char beg)
  7758. (skip-chars-forward " \t\n\r")
  7759. (setq beg (point))
  7760. (when (and (outline-invisible-p) visp)
  7761. (save-excursion (outline-show-heading)))
  7762. ;; Shift if necessary
  7763. (unless (= shift 0)
  7764. (save-restriction
  7765. (narrow-to-region beg end)
  7766. (while (not (= shift 0))
  7767. (org-map-region func (point-min) (point-max))
  7768. (setq shift (+ delta shift)))
  7769. (goto-char (point-min))
  7770. (setq newend (point-max))))
  7771. (when (or (org-called-interactively-p 'interactive) for-yank)
  7772. (message "Clipboard pasted as level %d subtree" new-level))
  7773. (when (and (not for-yank) ; in this case, org-yank will decide about folding
  7774. kill-ring
  7775. (eq org-subtree-clip (current-kill 0))
  7776. org-subtree-clip-folded)
  7777. ;; The tree was folded before it was killed/copied
  7778. (outline-hide-subtree))
  7779. (and for-yank (goto-char newend))
  7780. (and remove (setq kill-ring (cdr kill-ring))))))
  7781. (defun org-kill-is-subtree-p (&optional txt)
  7782. "Check if the current kill is an outline subtree, or a set of trees.
  7783. Returns nil if kill does not start with a headline, or if the first
  7784. headline level is not the largest headline level in the tree.
  7785. So this will actually accept several entries of equal levels as well,
  7786. which is OK for `org-paste-subtree'.
  7787. If optional TXT is given, check this string instead of the current kill."
  7788. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  7789. (re (org-get-limited-outline-regexp))
  7790. (^re (concat "^" re))
  7791. (start-level (and kill
  7792. (string-match
  7793. (concat "\\`\\([ \t\n\r]*?\n\\)?\\(" re "\\)")
  7794. kill)
  7795. (- (match-end 2) (match-beginning 2) 1)))
  7796. (start (1+ (or (match-beginning 2) -1))))
  7797. (if (not start-level)
  7798. (progn
  7799. nil) ;; does not even start with a heading
  7800. (catch 'exit
  7801. (while (setq start (string-match ^re kill (1+ start)))
  7802. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  7803. (throw 'exit nil)))
  7804. t))))
  7805. (defvar org-markers-to-move nil
  7806. "Markers that should be moved with a cut-and-paste operation.
  7807. Those markers are stored together with their positions relative to
  7808. the start of the region.")
  7809. (defun org-save-markers-in-region (beg end)
  7810. "Check markers in region.
  7811. If these markers are between BEG and END, record their position relative
  7812. to BEG, so that after moving the block of text, we can put the markers back
  7813. into place.
  7814. This function gets called just before an entry or tree gets cut from the
  7815. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  7816. called immediately, to move the markers with the entries."
  7817. (setq org-markers-to-move nil)
  7818. (when (featurep 'org-clock)
  7819. (org-clock-save-markers-for-cut-and-paste beg end))
  7820. (when (featurep 'org-agenda)
  7821. (org-agenda-save-markers-for-cut-and-paste beg end)))
  7822. (defun org-check-and-save-marker (marker beg end)
  7823. "Check if MARKER is between BEG and END.
  7824. If yes, remember the marker and the distance to BEG."
  7825. (when (and (marker-buffer marker)
  7826. (equal (marker-buffer marker) (current-buffer))
  7827. (>= marker beg) (< marker end))
  7828. (push (cons marker (- marker beg)) org-markers-to-move)))
  7829. (defun org-reinstall-markers-in-region (beg)
  7830. "Move all remembered markers to their position relative to BEG."
  7831. (dolist (x org-markers-to-move)
  7832. (move-marker (car x) (+ beg (cdr x))))
  7833. (setq org-markers-to-move nil))
  7834. (defun org-narrow-to-subtree ()
  7835. "Narrow buffer to the current subtree."
  7836. (interactive)
  7837. (save-excursion
  7838. (save-match-data
  7839. (org-with-limited-levels
  7840. (narrow-to-region
  7841. (progn (org-back-to-heading t) (point))
  7842. (progn (org-end-of-subtree t t)
  7843. (when (and (org-at-heading-p) (not (eobp))) (backward-char 1))
  7844. (point)))))))
  7845. (defun org-narrow-to-block ()
  7846. "Narrow buffer to the current block."
  7847. (interactive)
  7848. (let* ((case-fold-search t)
  7849. (blockp (org-between-regexps-p "^[ \t]*#\\+begin_.*"
  7850. "^[ \t]*#\\+end_.*")))
  7851. (if blockp
  7852. (narrow-to-region (car blockp) (cdr blockp))
  7853. (user-error "Not in a block"))))
  7854. (defun org-clone-subtree-with-time-shift (n &optional shift)
  7855. "Clone the task (subtree) at point N times.
  7856. The clones will be inserted as siblings.
  7857. In interactive use, the user will be prompted for the number of
  7858. clones to be produced. If the entry has a timestamp, the user
  7859. will also be prompted for a time shift, which may be a repeater
  7860. as used in time stamps, for example `+3d'. To disable this,
  7861. you can call the function with a universal prefix argument.
  7862. When a valid repeater is given and the entry contains any time
  7863. stamps, the clones will become a sequence in time, with time
  7864. stamps in the subtree shifted for each clone produced. If SHIFT
  7865. is nil or the empty string, time stamps will be left alone. The
  7866. ID property of the original subtree is removed.
  7867. If the original subtree did contain time stamps with a repeater,
  7868. the following will happen:
  7869. - the repeater will be removed in each clone
  7870. - an additional clone will be produced, with the current, unshifted
  7871. date(s) in the entry.
  7872. - the original entry will be placed *after* all the clones, with
  7873. repeater intact.
  7874. - the start days in the repeater in the original entry will be shifted
  7875. to past the last clone.
  7876. In this way you can spell out a number of instances of a repeating task,
  7877. and still retain the repeater to cover future instances of the task.
  7878. As described above, N+1 clones are produced when the original
  7879. subtree has a repeater. Setting N to 0, then, can be used to
  7880. remove the repeater from a subtree and create a shifted clone
  7881. with the original repeater."
  7882. (interactive "nNumber of clones to produce: ")
  7883. (let ((shift
  7884. (or shift
  7885. (if (and (not (equal current-prefix-arg '(4)))
  7886. (save-excursion
  7887. (re-search-forward org-ts-regexp-both
  7888. (save-excursion
  7889. (org-end-of-subtree t)
  7890. (point)) t)))
  7891. (read-from-minibuffer
  7892. "Date shift per clone (e.g. +1w, empty to copy unchanged): ")
  7893. ""))) ;; No time shift
  7894. (n-no-remove -1)
  7895. (drawer-re org-drawer-regexp)
  7896. (org-clock-re (format "^[ \t]*%s.*$" org-clock-string))
  7897. beg end template task idprop
  7898. shift-n shift-what doshift nmin nmax)
  7899. (unless (wholenump n)
  7900. (user-error "Invalid number of replications %s" n))
  7901. (when (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  7902. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([hdwmy]\\)[ \t]*\\'"
  7903. shift)))
  7904. (user-error "Invalid shift specification %s" shift))
  7905. (when doshift
  7906. (setq shift-n (string-to-number (match-string 1 shift))
  7907. shift-what (cdr (assoc (match-string 2 shift)
  7908. '(("d" . day) ("w" . week)
  7909. ("m" . month) ("y" . year))))))
  7910. (when (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  7911. (setq nmin 1 nmax n)
  7912. (org-back-to-heading t)
  7913. (setq beg (point))
  7914. (setq idprop (org-entry-get nil "ID"))
  7915. (org-end-of-subtree t t)
  7916. (or (bolp) (insert "\n"))
  7917. (setq end (point))
  7918. (setq template (buffer-substring beg end))
  7919. (when (and doshift
  7920. (string-match "<[^<>\n]+ [.+]?\\+[0-9]+[hdwmy][^<>\n]*>" template))
  7921. (delete-region beg end)
  7922. (setq end beg)
  7923. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  7924. (goto-char end)
  7925. (cl-loop for n from nmin to nmax do
  7926. ;; prepare clone
  7927. (with-temp-buffer
  7928. (insert template)
  7929. (org-mode)
  7930. (goto-char (point-min))
  7931. (org-show-subtree)
  7932. (and idprop (if org-clone-delete-id
  7933. (org-entry-delete nil "ID")
  7934. (org-id-get-create t)))
  7935. (unless (= n 0)
  7936. (while (re-search-forward org-clock-re nil t)
  7937. (kill-whole-line))
  7938. (goto-char (point-min))
  7939. (while (re-search-forward drawer-re nil t)
  7940. (org-remove-empty-drawer-at (point))))
  7941. (goto-char (point-min))
  7942. (when doshift
  7943. (while (re-search-forward org-ts-regexp-both nil t)
  7944. (org-timestamp-change (* n shift-n) shift-what))
  7945. (unless (= n n-no-remove)
  7946. (goto-char (point-min))
  7947. (while (re-search-forward org-ts-regexp nil t)
  7948. (save-excursion
  7949. (goto-char (match-beginning 0))
  7950. (when (looking-at "<[^<>\n]+\\( +[.+]?\\+[0-9]+[hdwmy]\\)")
  7951. (delete-region (match-beginning 1) (match-end 1)))))))
  7952. (setq task (buffer-string)))
  7953. (insert task))
  7954. (goto-char beg)))
  7955. ;;; Outline Sorting
  7956. (defun org-sort (with-case)
  7957. "Call `org-sort-entries', `org-table-sort-lines' or `org-sort-list'.
  7958. Optional argument WITH-CASE means sort case-sensitively."
  7959. (interactive "P")
  7960. (cond
  7961. ((org-at-table-p) (org-call-with-arg 'org-table-sort-lines with-case))
  7962. ((org-at-item-p) (org-call-with-arg 'org-sort-list with-case))
  7963. (t
  7964. (org-call-with-arg 'org-sort-entries with-case))))
  7965. (defun org-sort-remove-invisible (s)
  7966. "Remove invisible links from string S."
  7967. (remove-text-properties 0 (length s) org-rm-props s)
  7968. (while (string-match org-bracket-link-regexp s)
  7969. (setq s (replace-match (if (match-end 2)
  7970. (match-string 3 s)
  7971. (match-string 1 s))
  7972. t t s)))
  7973. (let ((st (format " %s " s)))
  7974. (while (string-match org-emph-re st)
  7975. (setq st (replace-match (format " %s " (match-string 4 st)) t t st)))
  7976. (setq s (substring st 1 -1)))
  7977. s)
  7978. (defvar org-priority-regexp) ; defined later in the file
  7979. (defvar org-after-sorting-entries-or-items-hook nil
  7980. "Hook that is run after a bunch of entries or items have been sorted.
  7981. When children are sorted, the cursor is in the parent line when this
  7982. hook gets called. When a region or a plain list is sorted, the cursor
  7983. will be in the first entry of the sorted region/list.")
  7984. (defun org-sort-entries
  7985. (&optional with-case sorting-type getkey-func compare-func property)
  7986. "Sort entries on a certain level of an outline tree.
  7987. If there is an active region, the entries in the region are sorted.
  7988. Else, if the cursor is before the first entry, sort the top-level items.
  7989. Else, the children of the entry at point are sorted.
  7990. Sorting can be alphabetically, numerically, by date/time as given by
  7991. a time stamp, by a property, by priority order, or by a custom function.
  7992. The command prompts for the sorting type unless it has been given to the
  7993. function through the SORTING-TYPE argument, which needs to be a character,
  7994. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?o ?O ?r ?R ?f ?F ?k ?K). Here is
  7995. the precise meaning of each character:
  7996. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  7997. c By creation time, which is assumed to be the first inactive time stamp
  7998. at the beginning of a line.
  7999. d By deadline date/time.
  8000. k By clocking time.
  8001. n Numerically, by converting the beginning of the entry/item to a number.
  8002. o By order of TODO keywords.
  8003. p By priority according to the cookie.
  8004. r By the value of a property.
  8005. s By scheduled date/time.
  8006. t By date/time, either the first active time stamp in the entry, or, if
  8007. none exist, by the first inactive one.
  8008. Capital letters will reverse the sort order.
  8009. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  8010. called with point at the beginning of the record. It must return either
  8011. a string or a number that should serve as the sorting key for that record.
  8012. Comparing entries ignores case by default. However, with an optional argument
  8013. WITH-CASE, the sorting considers case as well.
  8014. Sorting is done against the visible part of the headlines, it ignores hidden
  8015. links.
  8016. When sorting is done, call `org-after-sorting-entries-or-items-hook'."
  8017. (interactive "P")
  8018. (let ((case-func (if with-case 'identity 'downcase))
  8019. (cmstr
  8020. ;; The clock marker is lost when using `sort-subr', let's
  8021. ;; store the clocking string.
  8022. (when (equal (marker-buffer org-clock-marker) (current-buffer))
  8023. (save-excursion
  8024. (goto-char org-clock-marker)
  8025. (buffer-substring-no-properties (line-beginning-position)
  8026. (point)))))
  8027. start beg end stars re re2
  8028. txt what tmp)
  8029. ;; Find beginning and end of region to sort
  8030. (cond
  8031. ((org-region-active-p)
  8032. ;; we will sort the region
  8033. (setq end (region-end)
  8034. what "region")
  8035. (goto-char (region-beginning))
  8036. (unless (org-at-heading-p) (outline-next-heading))
  8037. (setq start (point)))
  8038. ((or (org-at-heading-p)
  8039. (ignore-errors (progn (org-back-to-heading) t)))
  8040. ;; we will sort the children of the current headline
  8041. (org-back-to-heading)
  8042. (setq start (point)
  8043. end (progn (org-end-of-subtree t t)
  8044. (or (bolp) (insert "\n"))
  8045. (when (>= (org-back-over-empty-lines) 1)
  8046. (forward-line 1))
  8047. (point))
  8048. what "children")
  8049. (goto-char start)
  8050. (outline-show-subtree)
  8051. (outline-next-heading))
  8052. (t
  8053. ;; we will sort the top-level entries in this file
  8054. (goto-char (point-min))
  8055. (or (org-at-heading-p) (outline-next-heading))
  8056. (setq start (point))
  8057. (goto-char (point-max))
  8058. (beginning-of-line 1)
  8059. (when (looking-at ".*?\\S-")
  8060. ;; File ends in a non-white line
  8061. (end-of-line 1)
  8062. (insert "\n"))
  8063. (setq end (point-max))
  8064. (setq what "top-level")
  8065. (goto-char start)
  8066. (outline-show-all)))
  8067. (setq beg (point))
  8068. (when (>= beg end) (goto-char start) (user-error "Nothing to sort"))
  8069. (looking-at "\\(\\*+\\)")
  8070. (setq stars (match-string 1)
  8071. re (concat "^" (regexp-quote stars) " +")
  8072. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[ \t\n]")
  8073. txt (buffer-substring beg end))
  8074. (unless (equal (substring txt -1) "\n") (setq txt (concat txt "\n")))
  8075. (when (and (not (equal stars "*")) (string-match re2 txt))
  8076. (user-error "Region to sort contains a level above the first entry"))
  8077. (unless sorting-type
  8078. (message
  8079. "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
  8080. [t]ime [s]cheduled [d]eadline [c]reated cloc[k]ing
  8081. A/N/P/R/O/F/T/S/D/C/K means reversed:"
  8082. what)
  8083. (setq sorting-type (read-char-exclusive))
  8084. (unless getkey-func
  8085. (and (= (downcase sorting-type) ?f)
  8086. (setq getkey-func
  8087. (completing-read "Sort using function: "
  8088. obarray 'fboundp t nil nil))
  8089. (setq getkey-func (intern getkey-func))))
  8090. (and (= (downcase sorting-type) ?r)
  8091. (not property)
  8092. (setq property
  8093. (completing-read "Property: "
  8094. (mapcar #'list (org-buffer-property-keys t))
  8095. nil t))))
  8096. (when (member sorting-type '(?k ?K)) (org-clock-sum))
  8097. (message "Sorting entries...")
  8098. (save-restriction
  8099. (narrow-to-region start end)
  8100. (let ((dcst (downcase sorting-type))
  8101. (case-fold-search nil)
  8102. (now (current-time)))
  8103. (sort-subr
  8104. (/= dcst sorting-type)
  8105. ;; This function moves to the beginning character of the "record" to
  8106. ;; be sorted.
  8107. (lambda nil
  8108. (if (re-search-forward re nil t)
  8109. (goto-char (match-beginning 0))
  8110. (goto-char (point-max))))
  8111. ;; This function moves to the last character of the "record" being
  8112. ;; sorted.
  8113. (lambda nil
  8114. (save-match-data
  8115. (condition-case nil
  8116. (outline-forward-same-level 1)
  8117. (error
  8118. (goto-char (point-max))))))
  8119. ;; This function returns the value that gets sorted against.
  8120. (lambda nil
  8121. (cond
  8122. ((= dcst ?n)
  8123. (if (looking-at org-complex-heading-regexp)
  8124. (string-to-number (org-sort-remove-invisible (match-string 4)))
  8125. nil))
  8126. ((= dcst ?a)
  8127. (if (looking-at org-complex-heading-regexp)
  8128. (funcall case-func (org-sort-remove-invisible (match-string 4)))
  8129. nil))
  8130. ((= dcst ?k)
  8131. (or (get-text-property (point) :org-clock-minutes) 0))
  8132. ((= dcst ?t)
  8133. (let ((end (save-excursion (outline-next-heading) (point))))
  8134. (if (or (re-search-forward org-ts-regexp end t)
  8135. (re-search-forward org-ts-regexp-both end t))
  8136. (org-time-string-to-seconds (match-string 0))
  8137. (org-float-time now))))
  8138. ((= dcst ?c)
  8139. (let ((end (save-excursion (outline-next-heading) (point))))
  8140. (if (re-search-forward
  8141. (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
  8142. end t)
  8143. (org-time-string-to-seconds (match-string 0))
  8144. (org-float-time now))))
  8145. ((= dcst ?s)
  8146. (let ((end (save-excursion (outline-next-heading) (point))))
  8147. (if (re-search-forward org-scheduled-time-regexp end t)
  8148. (org-time-string-to-seconds (match-string 1))
  8149. (org-float-time now))))
  8150. ((= dcst ?d)
  8151. (let ((end (save-excursion (outline-next-heading) (point))))
  8152. (if (re-search-forward org-deadline-time-regexp end t)
  8153. (org-time-string-to-seconds (match-string 1))
  8154. (org-float-time now))))
  8155. ((= dcst ?p)
  8156. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  8157. (string-to-char (match-string 2))
  8158. org-default-priority))
  8159. ((= dcst ?r)
  8160. (or (org-entry-get nil property) ""))
  8161. ((= dcst ?o)
  8162. (when (looking-at org-complex-heading-regexp)
  8163. (let* ((m (match-string 2))
  8164. (s (if (member m org-done-keywords) '- '+)))
  8165. (- 99 (funcall s (length (member m org-todo-keywords-1)))))))
  8166. ((= dcst ?f)
  8167. (if getkey-func
  8168. (progn
  8169. (setq tmp (funcall getkey-func))
  8170. (when (stringp tmp) (setq tmp (funcall case-func tmp)))
  8171. tmp)
  8172. (error "Invalid key function `%s'" getkey-func)))
  8173. (t (error "Invalid sorting type `%c'" sorting-type))))
  8174. nil
  8175. (cond
  8176. ((= dcst ?a) 'string<)
  8177. ((= dcst ?f) compare-func)
  8178. ((member dcst '(?p ?t ?s ?d ?c ?k)) '<)))))
  8179. (run-hooks 'org-after-sorting-entries-or-items-hook)
  8180. ;; Reset the clock marker if needed
  8181. (when cmstr
  8182. (save-excursion
  8183. (goto-char start)
  8184. (search-forward cmstr nil t)
  8185. (move-marker org-clock-marker (point))))
  8186. (message "Sorting entries...done")))
  8187. ;;; The orgstruct minor mode
  8188. ;; Define a minor mode which can be used in other modes in order to
  8189. ;; integrate the org-mode structure editing commands.
  8190. ;; This is really a hack, because the org-mode structure commands use
  8191. ;; keys which normally belong to the major mode. Here is how it
  8192. ;; works: The minor mode defines all the keys necessary to operate the
  8193. ;; structure commands, but wraps the commands into a function which
  8194. ;; tests if the cursor is currently at a headline or a plain list
  8195. ;; item. If that is the case, the structure command is used,
  8196. ;; temporarily setting many Org-mode variables like regular
  8197. ;; expressions for filling etc. However, when any of those keys is
  8198. ;; used at a different location, function uses `key-binding' to look
  8199. ;; up if the key has an associated command in another currently active
  8200. ;; keymap (minor modes, major mode, global), and executes that
  8201. ;; command. There might be problems if any of the keys is otherwise
  8202. ;; used as a prefix key.
  8203. (defcustom orgstruct-heading-prefix-regexp ""
  8204. "Regexp that matches the custom prefix of Org headlines in
  8205. orgstruct(++)-mode."
  8206. :group 'org
  8207. :version "24.4"
  8208. :package-version '(Org . "8.3")
  8209. :type 'regexp)
  8210. ;;;###autoload(put 'orgstruct-heading-prefix-regexp 'safe-local-variable 'stringp)
  8211. (defcustom orgstruct-setup-hook nil
  8212. "Hook run after orgstruct-mode-map is filled."
  8213. :group 'org
  8214. :version "24.4"
  8215. :package-version '(Org . "8.0")
  8216. :type 'hook)
  8217. (defvar orgstruct-initialized nil)
  8218. (defvar org-local-vars nil
  8219. "List of local variables, for use by `orgstruct-mode'.")
  8220. ;;;###autoload
  8221. (define-minor-mode orgstruct-mode
  8222. "Toggle the minor mode `orgstruct-mode'.
  8223. This mode is for using Org-mode structure commands in other
  8224. modes. The following keys behave as if Org-mode were active, if
  8225. the cursor is on a headline, or on a plain list item (both as
  8226. defined by Org-mode)."
  8227. nil " OrgStruct" (make-sparse-keymap)
  8228. (funcall (if orgstruct-mode
  8229. 'add-to-invisibility-spec
  8230. 'remove-from-invisibility-spec)
  8231. '(outline . t))
  8232. (when orgstruct-mode
  8233. (org-load-modules-maybe)
  8234. (unless orgstruct-initialized
  8235. (orgstruct-setup)
  8236. (setq orgstruct-initialized t))))
  8237. ;;;###autoload
  8238. (defun turn-on-orgstruct ()
  8239. "Unconditionally turn on `orgstruct-mode'."
  8240. (orgstruct-mode 1))
  8241. (defvar-local orgstruct-is-++ nil
  8242. "Is `orgstruct-mode' in ++ version in the current-buffer?")
  8243. (defvar-local org-fb-vars nil)
  8244. (defun orgstruct++-mode (&optional arg)
  8245. "Toggle `orgstruct-mode', the enhanced version of it.
  8246. In addition to setting orgstruct-mode, this also exports all
  8247. indentation and autofilling variables from org-mode into the
  8248. buffer. It will also recognize item context in multiline items."
  8249. (interactive "P")
  8250. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  8251. (if (< arg 1)
  8252. (progn (orgstruct-mode -1)
  8253. (dolist (v org-fb-vars)
  8254. (set (make-local-variable (car v))
  8255. (if (eq (car-safe (cadr v)) 'quote)
  8256. (cl-cadadr v)
  8257. (nth 1 v)))))
  8258. (orgstruct-mode 1)
  8259. (setq org-fb-vars nil)
  8260. (unless org-local-vars
  8261. (setq org-local-vars (org-get-local-variables)))
  8262. (let (var val)
  8263. (dolist (x org-local-vars)
  8264. (when (string-match
  8265. "^\\(paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\
  8266. \\|fill-prefix\\|indent-\\)"
  8267. (symbol-name (car x)))
  8268. (setq var (car x) val (nth 1 x))
  8269. (push (list var `(quote ,(eval var))) org-fb-vars)
  8270. (set (make-local-variable var)
  8271. (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  8272. (setq-local orgstruct-is-++ t))))
  8273. ;;;###autoload
  8274. (defun turn-on-orgstruct++ ()
  8275. "Unconditionally turn on `orgstruct++-mode'."
  8276. (orgstruct++-mode 1))
  8277. (defun orgstruct-error ()
  8278. "Error when there is no default binding for a structure key."
  8279. (interactive)
  8280. (funcall (if (fboundp 'user-error)
  8281. 'user-error
  8282. 'error)
  8283. "This key has no function outside structure elements"))
  8284. (defun orgstruct-setup ()
  8285. "Setup orgstruct keymap."
  8286. (dolist (cell '((org-demote . t)
  8287. (org-metaleft . t)
  8288. (org-metaright . t)
  8289. (org-promote . t)
  8290. (org-shiftmetaleft . t)
  8291. (org-shiftmetaright . t)
  8292. org-backward-element
  8293. org-backward-heading-same-level
  8294. org-ctrl-c-ret
  8295. org-ctrl-c-minus
  8296. org-ctrl-c-star
  8297. org-cycle
  8298. org-force-cycle-archived
  8299. org-forward-heading-same-level
  8300. org-insert-heading
  8301. org-insert-heading-respect-content
  8302. org-kill-note-or-show-branches
  8303. org-mark-subtree
  8304. org-meta-return
  8305. org-metadown
  8306. org-metaup
  8307. org-narrow-to-subtree
  8308. org-promote-subtree
  8309. org-reveal
  8310. org-shiftdown
  8311. org-shiftleft
  8312. org-shiftmetadown
  8313. org-shiftmetaup
  8314. org-shiftright
  8315. org-shifttab
  8316. org-shifttab
  8317. org-shiftup
  8318. org-show-children
  8319. org-show-subtree
  8320. org-sort
  8321. org-up-element
  8322. outline-demote
  8323. outline-next-visible-heading
  8324. outline-previous-visible-heading
  8325. outline-promote
  8326. outline-up-heading))
  8327. (let ((f (or (car-safe cell) cell))
  8328. (disable-when-heading-prefix (cdr-safe cell)))
  8329. (when (fboundp f)
  8330. (let ((new-bindings))
  8331. (dolist (binding (nconc (where-is-internal f org-mode-map)
  8332. (where-is-internal f outline-mode-map)))
  8333. (push binding new-bindings)
  8334. ;; TODO use local-function-key-map
  8335. (dolist (rep '(("<tab>" . "TAB")
  8336. ("<return>" . "RET")
  8337. ("<escape>" . "ESC")
  8338. ("<delete>" . "DEL")))
  8339. (setq binding (read-kbd-macro
  8340. (let ((case-fold-search))
  8341. (replace-regexp-in-string
  8342. (regexp-quote (cdr rep))
  8343. (car rep)
  8344. (key-description binding)))))
  8345. (cl-pushnew binding new-bindings :test 'equal)))
  8346. (dolist (binding new-bindings)
  8347. (let ((key (lookup-key orgstruct-mode-map binding)))
  8348. (when (or (not key) (numberp key))
  8349. (ignore-errors
  8350. (org-defkey orgstruct-mode-map
  8351. binding
  8352. (orgstruct-make-binding
  8353. f binding disable-when-heading-prefix))))))))))
  8354. (run-hooks 'orgstruct-setup-hook))
  8355. (defun orgstruct-make-binding (fun key disable-when-heading-prefix)
  8356. "Create a function for binding in the structure minor mode.
  8357. FUN is the command to call inside a table. KEY is the key that
  8358. should be checked in for a command to execute outside of tables.
  8359. Non-nil `disable-when-heading-prefix' means to disable the command
  8360. if `orgstruct-heading-prefix-regexp' is not empty."
  8361. (let ((name (concat "orgstruct-hijacker-" (symbol-name fun))))
  8362. (let ((nname name)
  8363. (i 0))
  8364. (while (fboundp (intern nname))
  8365. (setq nname (format "%s-%d" name (setq i (1+ i)))))
  8366. (setq name (intern nname)))
  8367. (eval
  8368. (let ((bindings '((org-heading-regexp
  8369. (concat "^"
  8370. orgstruct-heading-prefix-regexp
  8371. "\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ ]*$"))
  8372. (org-outline-regexp
  8373. (concat orgstruct-heading-prefix-regexp "\\*+ "))
  8374. (org-outline-regexp-bol
  8375. (concat "^" org-outline-regexp))
  8376. (outline-regexp org-outline-regexp)
  8377. (outline-heading-end-regexp "\n")
  8378. (outline-level 'org-outline-level)
  8379. (outline-heading-alist))))
  8380. `(defun ,name (arg)
  8381. ,(concat "In Structure, run `" (symbol-name fun) "'.\n"
  8382. "Outside of structure, run the binding of `"
  8383. (key-description key) "'."
  8384. (when disable-when-heading-prefix
  8385. (concat
  8386. "\nIf `orgstruct-heading-prefix-regexp' is not empty, this command will always fall\n"
  8387. "back to the default binding due to limitations of Org's implementation of\n"
  8388. "`" (symbol-name fun) "'.")))
  8389. (interactive "p")
  8390. (let* ((disable
  8391. ,(and disable-when-heading-prefix
  8392. '(not (string= orgstruct-heading-prefix-regexp ""))))
  8393. (fallback
  8394. (or disable
  8395. (not
  8396. (let* ,bindings
  8397. (org-context-p 'headline 'item
  8398. ,(when (memq fun
  8399. '(org-insert-heading
  8400. org-insert-heading-respect-content
  8401. org-meta-return))
  8402. '(when orgstruct-is-++
  8403. 'item-body))))))))
  8404. (if fallback
  8405. (let* ((orgstruct-mode)
  8406. (binding
  8407. (let ((key ,key))
  8408. (catch 'exit
  8409. (dolist
  8410. (rep
  8411. '(nil
  8412. ("<\\([^>]*\\)tab>" . "\\1TAB")
  8413. ("<\\([^>]*\\)return>" . "\\1RET")
  8414. ("<\\([^>]*\\)escape>" . "\\1ESC")
  8415. ("<\\([^>]*\\)delete>" . "\\1DEL"))
  8416. nil)
  8417. (when rep
  8418. (setq key (read-kbd-macro
  8419. (let ((case-fold-search))
  8420. (replace-regexp-in-string
  8421. (car rep)
  8422. (cdr rep)
  8423. (key-description key))))))
  8424. (when (key-binding key)
  8425. (throw 'exit (key-binding key))))))))
  8426. (if (keymapp binding)
  8427. (org-set-transient-map binding)
  8428. (let ((func (or binding
  8429. (unless disable
  8430. 'orgstruct-error))))
  8431. (when func
  8432. (call-interactively func)))))
  8433. (org-run-like-in-org-mode
  8434. (lambda ()
  8435. (interactive)
  8436. (let* ,bindings
  8437. (call-interactively ',fun)))))))))
  8438. name))
  8439. (defun org-contextualize-keys (alist contexts)
  8440. "Return valid elements in ALIST depending on CONTEXTS.
  8441. `org-agenda-custom-commands' or `org-capture-templates' are the
  8442. values used for ALIST, and `org-agenda-custom-commands-contexts'
  8443. or `org-capture-templates-contexts' are the associated contexts
  8444. definitions."
  8445. (let ((contexts
  8446. ;; normalize contexts
  8447. (mapcar
  8448. (lambda(c) (cond ((listp (cadr c))
  8449. (list (car c) (car c) (nth 1 c)))
  8450. ((string= "" (cadr c))
  8451. (list (car c) (car c) (nth 2 c)))
  8452. (t c)))
  8453. contexts))
  8454. (a alist) r s)
  8455. ;; loop over all commands or templates
  8456. (dolist (c a)
  8457. (let (vrules repl)
  8458. (cond
  8459. ((not (assoc (car c) contexts))
  8460. (push c r))
  8461. ((and (assoc (car c) contexts)
  8462. (setq vrules (org-contextualize-validate-key
  8463. (car c) contexts)))
  8464. (mapc (lambda (vr)
  8465. (unless (equal (car vr) (cadr vr))
  8466. (setq repl vr)))
  8467. vrules)
  8468. (if (not repl) (push c r)
  8469. (push (cadr repl) s)
  8470. (push
  8471. (cons (car c)
  8472. (cdr (or (assoc (cadr repl) alist)
  8473. (error "Undefined key `%s' as contextual replacement for `%s'"
  8474. (cadr repl) (car c)))))
  8475. r))))))
  8476. ;; Return limited ALIST, possibly with keys modified, and deduplicated
  8477. (delq
  8478. nil
  8479. (delete-dups
  8480. (mapcar (lambda (x)
  8481. (let ((tpl (car x)))
  8482. (unless (delq
  8483. nil
  8484. (mapcar (lambda (y)
  8485. (equal y tpl))
  8486. s))
  8487. x)))
  8488. (reverse r))))))
  8489. (defun org-contextualize-validate-key (key contexts)
  8490. "Check CONTEXTS for agenda or capture KEY."
  8491. (let (res)
  8492. (dolist (r contexts)
  8493. (dolist (rr (car (last r)))
  8494. (when
  8495. (and (equal key (car r))
  8496. (if (functionp rr) (funcall rr)
  8497. (or (and (eq (car rr) 'in-file)
  8498. (buffer-file-name)
  8499. (string-match (cdr rr) (buffer-file-name)))
  8500. (and (eq (car rr) 'in-mode)
  8501. (string-match (cdr rr) (symbol-name major-mode)))
  8502. (and (eq (car rr) 'in-buffer)
  8503. (string-match (cdr rr) (buffer-name)))
  8504. (when (and (eq (car rr) 'not-in-file)
  8505. (buffer-file-name))
  8506. (not (string-match (cdr rr) (buffer-file-name))))
  8507. (when (eq (car rr) 'not-in-mode)
  8508. (not (string-match (cdr rr) (symbol-name major-mode))))
  8509. (when (eq (car rr) 'not-in-buffer)
  8510. (not (string-match (cdr rr) (buffer-name)))))))
  8511. (push r res))))
  8512. (delete-dups (delq nil res))))
  8513. (defun org-context-p (&rest contexts)
  8514. "Check if local context is any of CONTEXTS.
  8515. Possible values in the list of contexts are `table', `headline', and `item'."
  8516. (let ((pos (point)))
  8517. (goto-char (point-at-bol))
  8518. (prog1 (or (and (memq 'table contexts)
  8519. (looking-at "[ \t]*|"))
  8520. (and (memq 'headline contexts)
  8521. (looking-at org-outline-regexp))
  8522. (and (memq 'item contexts)
  8523. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  8524. (and (memq 'item-body contexts)
  8525. (org-in-item-p)))
  8526. (goto-char pos))))
  8527. (defconst org-unique-local-variables
  8528. '(org-element--cache
  8529. org-element--cache-objects
  8530. org-element--cache-sync-keys
  8531. org-element--cache-sync-requests
  8532. org-element--cache-sync-timer)
  8533. "List of local variables that cannot be transferred to another buffer.")
  8534. (defun org-get-local-variables ()
  8535. "Return a list of all local variables in an Org mode buffer."
  8536. (delq nil
  8537. (mapcar
  8538. (lambda (x)
  8539. (let* ((binding (if (symbolp x) (list x) (list (car x) (cdr x))))
  8540. (name (car binding)))
  8541. (and (not (get name 'org-state))
  8542. (not (memq name org-unique-local-variables))
  8543. (string-match-p
  8544. "\\`\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|\
  8545. auto-fill\\|normal-auto-fill\\|fill-paragraph\\|indent-\\)"
  8546. (symbol-name name))
  8547. binding)))
  8548. (with-temp-buffer
  8549. (org-mode)
  8550. (buffer-local-variables)))))
  8551. (defun org-clone-local-variables (from-buffer &optional regexp)
  8552. "Clone local variables from FROM-BUFFER.
  8553. Optional argument REGEXP selects variables to clone."
  8554. (dolist (pair (buffer-local-variables from-buffer))
  8555. (let ((name (car pair)))
  8556. (when (and (symbolp name)
  8557. (not (memq name org-unique-local-variables))
  8558. (or (null regexp) (string-match regexp (symbol-name name))))
  8559. (set (make-local-variable name) (cdr pair))))))
  8560. ;;;###autoload
  8561. (defun org-run-like-in-org-mode (cmd)
  8562. "Run a command, pretending that the current buffer is in Org-mode.
  8563. This will temporarily bind local variables that are typically bound in
  8564. Org-mode to the values they have in Org-mode, and then interactively
  8565. call CMD."
  8566. (org-load-modules-maybe)
  8567. (unless org-local-vars
  8568. (setq org-local-vars (org-get-local-variables)))
  8569. (let (binds)
  8570. (dolist (var org-local-vars)
  8571. (when (or (not (boundp (car var)))
  8572. (eq (symbol-value (car var))
  8573. (default-value (car var))))
  8574. (push (list (car var) `(quote ,(cadr var))) binds)))
  8575. (eval `(let ,binds
  8576. (call-interactively (quote ,cmd))))))
  8577. (defun org-get-category (&optional pos force-refresh)
  8578. "Get the category applying to position POS."
  8579. (save-match-data
  8580. (when force-refresh (org-refresh-category-properties))
  8581. (let ((pos (or pos (point))))
  8582. (or (get-text-property pos 'org-category)
  8583. (progn (org-refresh-category-properties)
  8584. (get-text-property pos 'org-category))))))
  8585. ;;; Refresh properties
  8586. (defun org-refresh-properties (dprop tprop)
  8587. "Refresh buffer text properties.
  8588. DPROP is the drawer property and TPROP is either the
  8589. corresponding text property to set, or an alist with each element
  8590. being a text property (as a symbol) and a function to apply to
  8591. the value of the drawer property."
  8592. (let ((case-fold-search t)
  8593. (inhibit-read-only t))
  8594. (org-with-silent-modifications
  8595. (save-excursion
  8596. (save-restriction
  8597. (widen)
  8598. (goto-char (point-min))
  8599. (while (re-search-forward (concat "^[ \t]*:" dprop ": +\\(.*\\)[ \t]*$") nil t)
  8600. (org-refresh-property tprop (org-match-string-no-properties 1))))))))
  8601. (defun org-refresh-property (tprop p)
  8602. "Refresh the buffer text property TPROP from the drawer property P.
  8603. The refresh happens only for the current tree (not subtree)."
  8604. (unless (org-before-first-heading-p)
  8605. (save-excursion
  8606. (org-back-to-heading t)
  8607. (if (symbolp tprop)
  8608. ;; TPROP is a text property symbol
  8609. (put-text-property
  8610. (point) (or (outline-next-heading) (point-max)) tprop p)
  8611. ;; TPROP is an alist with (properties . function) elements
  8612. (dolist (al tprop)
  8613. (save-excursion
  8614. (put-text-property
  8615. (line-beginning-position) (or (outline-next-heading) (point-max))
  8616. (car al)
  8617. (funcall (cdr al) p))))))))
  8618. (defun org-refresh-category-properties ()
  8619. "Refresh category text properties in the buffer."
  8620. (let ((case-fold-search t)
  8621. (inhibit-read-only t)
  8622. (default-category
  8623. (cond ((null org-category)
  8624. (if buffer-file-name
  8625. (file-name-sans-extension
  8626. (file-name-nondirectory buffer-file-name))
  8627. "???"))
  8628. ((symbolp org-category) (symbol-name org-category))
  8629. (t org-category))))
  8630. (org-with-silent-modifications
  8631. (org-with-wide-buffer
  8632. ;; Set buffer-wide category. Search last #+CATEGORY keyword.
  8633. ;; This is the default category for the buffer. If none is
  8634. ;; found, fall-back to `org-category' or buffer file name.
  8635. (put-text-property
  8636. (point-min) (point-max)
  8637. 'org-category
  8638. (catch 'buffer-category
  8639. (goto-char (point-max))
  8640. (while (re-search-backward "^[ \t]*#\\+CATEGORY:" (point-min) t)
  8641. (let ((element (org-element-at-point)))
  8642. (when (eq (org-element-type element) 'keyword)
  8643. (throw 'buffer-category
  8644. (org-element-property :value element)))))
  8645. default-category))
  8646. ;; Set sub-tree specific categories.
  8647. (goto-char (point-min))
  8648. (let ((regexp (org-re-property "CATEGORY")))
  8649. (while (re-search-forward regexp nil t)
  8650. (let ((value (org-match-string-no-properties 3)))
  8651. (when (org-at-property-p)
  8652. (put-text-property
  8653. (save-excursion (org-back-to-heading t) (point))
  8654. (save-excursion (org-end-of-subtree t t) (point))
  8655. 'org-category
  8656. value)))))))))
  8657. (defun org-refresh-stats-properties ()
  8658. "Refresh stats text properties in the buffer."
  8659. (let (stats)
  8660. (org-with-silent-modifications
  8661. (save-excursion
  8662. (save-restriction
  8663. (widen)
  8664. (goto-char (point-min))
  8665. (while (re-search-forward
  8666. (concat org-outline-regexp-bol ".*"
  8667. "\\(?:\\[\\([0-9]+\\)%\\|\\([0-9]+\\)/\\([0-9]+\\)\\]\\)")
  8668. nil t)
  8669. (setq stats (cond ((equal (match-string 3) "0") 0)
  8670. ((match-string 2)
  8671. (/ (* (string-to-number (match-string 2)) 100)
  8672. (string-to-number (match-string 3))))
  8673. (t (string-to-number (match-string 1)))))
  8674. (org-back-to-heading t)
  8675. (put-text-property (point) (progn (org-end-of-subtree t t) (point))
  8676. 'org-stats stats)))))))
  8677. (defun org-refresh-effort-properties ()
  8678. "Refresh effort properties"
  8679. (org-refresh-properties
  8680. org-effort-property
  8681. '((effort . identity)
  8682. (effort-minutes . org-duration-string-to-minutes))))
  8683. ;;;; Link Stuff
  8684. ;;; Link abbreviations
  8685. (defun org-link-expand-abbrev (link)
  8686. "Apply replacements as defined in `org-link-abbrev-alist'."
  8687. (if (string-match "^\\([^:]*\\)\\(::?\\(.*\\)\\)?$" link)
  8688. (let* ((key (match-string 1 link))
  8689. (as (or (assoc key org-link-abbrev-alist-local)
  8690. (assoc key org-link-abbrev-alist)))
  8691. (tag (and (match-end 2) (match-string 3 link)))
  8692. rpl)
  8693. (if (not as)
  8694. link
  8695. (setq rpl (cdr as))
  8696. (cond
  8697. ((symbolp rpl) (funcall rpl tag))
  8698. ((string-match "%(\\([^)]+\\))" rpl)
  8699. (replace-match
  8700. (save-match-data
  8701. (funcall (intern-soft (match-string 1 rpl)) tag)) t t rpl))
  8702. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  8703. ((string-match "%h" rpl)
  8704. (replace-match (url-hexify-string (or tag "")) t t rpl))
  8705. (t (concat rpl tag)))))
  8706. link))
  8707. ;;; Storing and inserting links
  8708. (defvar org-insert-link-history nil
  8709. "Minibuffer history for links inserted with `org-insert-link'.")
  8710. (defvar org-stored-links nil
  8711. "Contains the links stored with `org-store-link'.")
  8712. (defvar org-store-link-plist nil
  8713. "Plist with info about the most recently link created with `org-store-link'.")
  8714. (defvar org-link-protocols nil
  8715. "Link protocols added to Org-mode using `org-add-link-type'.")
  8716. (defvar org-store-link-functions nil
  8717. "List of functions that are called to create and store a link.
  8718. Each function will be called in turn until one returns a non-nil
  8719. value. Each function should check if it is responsible for creating
  8720. this link (for example by looking at the major mode).
  8721. If not, it must exit and return nil.
  8722. If yes, it should return a non-nil value after a calling
  8723. `org-store-link-props' with a list of properties and values.
  8724. Special properties are:
  8725. :type The link prefix, like \"http\". This must be given.
  8726. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  8727. This is obligatory as well.
  8728. :description Optional default description for the second pair
  8729. of brackets in an Org-mode link. The user can still change
  8730. this when inserting this link into an Org-mode buffer.
  8731. In addition to these, any additional properties can be specified
  8732. and then used in capture templates.")
  8733. (defun org-add-link-type (type &optional follow export)
  8734. "Add TYPE to the list of `org-link-types'.
  8735. Re-compute all regular expressions depending on `org-link-types'
  8736. FOLLOW and EXPORT are two functions.
  8737. FOLLOW should take the link path as the single argument and do whatever
  8738. is necessary to follow the link, for example find a file or display
  8739. a mail message.
  8740. EXPORT should format the link path for export to one of the export formats.
  8741. It should be a function accepting three arguments:
  8742. path the path of the link, the text after the prefix (like \"http:\")
  8743. desc the description of the link, if any
  8744. format the export format, a symbol like `html' or `latex' or `ascii'.
  8745. The function may use the FORMAT information to return different values
  8746. depending on the format. The return value will be put literally into
  8747. the exported file. If the return value is nil, this means Org should
  8748. do what it normally does with links which do not have EXPORT defined.
  8749. Org mode has a built-in default for exporting links. If you are happy with
  8750. this default, there is no need to define an export function for the link
  8751. type. For a simple example of an export function, see `org-bbdb.el'."
  8752. (add-to-list 'org-link-types type t)
  8753. (org-make-link-regexps)
  8754. (org-element-update-syntax)
  8755. (if (assoc type org-link-protocols)
  8756. (setcdr (assoc type org-link-protocols) (list follow export))
  8757. (push (list type follow export) org-link-protocols)))
  8758. (defvar org-agenda-buffer-name) ; Defined in org-agenda.el
  8759. (defvar org-id-link-to-org-use-id) ; Defined in org-id.el
  8760. ;;;###autoload
  8761. (defun org-store-link (arg)
  8762. "\\<org-mode-map>Store an org-link to the current location.
  8763. This link is added to `org-stored-links' and can later be inserted
  8764. into an Org buffer with \\[org-insert-link].
  8765. For some link types, a prefix ARG is interpreted.
  8766. For links to Usenet articles, ARG negates `org-gnus-prefer-web-links'.
  8767. For file links, ARG negates `org-context-in-file-links'.
  8768. A double prefix ARG force skipping storing functions that are not
  8769. part of Org's core.
  8770. A triple prefix ARG force storing a link for each line in the
  8771. active region."
  8772. (interactive "P")
  8773. (org-load-modules-maybe)
  8774. (if (and (equal arg '(64)) (org-region-active-p))
  8775. (save-excursion
  8776. (let ((end (region-end)))
  8777. (goto-char (region-beginning))
  8778. (set-mark (point))
  8779. (while (< (point-at-eol) end)
  8780. (move-end-of-line 1) (activate-mark)
  8781. (let (current-prefix-arg)
  8782. (call-interactively 'org-store-link))
  8783. (move-beginning-of-line 2)
  8784. (set-mark (point)))))
  8785. (org-with-limited-levels
  8786. (setq org-store-link-plist nil)
  8787. (let (link cpltxt desc description search
  8788. txt custom-id agenda-link sfuns sfunsn)
  8789. (cond
  8790. ;; Store a link using an external link type
  8791. ((and (not (equal arg '(16)))
  8792. (setq sfuns
  8793. (delq
  8794. nil (mapcar (lambda (f)
  8795. (let (fs) (if (funcall f) (push f fs))))
  8796. org-store-link-functions))
  8797. sfunsn (mapcar (lambda (fu) (symbol-name (car fu))) sfuns))
  8798. (or (and (cdr sfuns)
  8799. (funcall (intern
  8800. (completing-read
  8801. "Which function for creating the link? "
  8802. sfunsn nil t (car sfunsn)))))
  8803. (funcall (caar sfuns)))
  8804. (setq link (plist-get org-store-link-plist :link)
  8805. desc (or (plist-get org-store-link-plist
  8806. :description)
  8807. link))))
  8808. ;; Store a link from a source code buffer.
  8809. ((org-src-edit-buffer-p)
  8810. (cond
  8811. ((save-excursion
  8812. (beginning-of-line)
  8813. (looking-at (concat (format org-coderef-label-format "\\(.*?\\)")
  8814. "[ \t]*$")))
  8815. (setq link (format "(%s)" (org-match-string-no-properties 1))))
  8816. ((org-called-interactively-p 'any)
  8817. (let (label)
  8818. (while (or (not label)
  8819. (org-with-wide-buffer
  8820. (goto-char (point-min))
  8821. (re-search-forward
  8822. (regexp-quote (format org-coderef-label-format label))
  8823. nil t)))
  8824. (when label (message "Label exists already") (sit-for 2))
  8825. (setq label (read-string "Code line label: " label)))
  8826. (end-of-line)
  8827. (setq link (format org-coderef-label-format label))
  8828. (let ((gc (- 79 (length link))))
  8829. (if (< (current-column) gc) (org-move-to-column gc t)
  8830. (insert " ")))
  8831. (insert link)
  8832. (setq link (concat "(" label ")") desc nil)))
  8833. (t (setq link nil))))
  8834. ;; We are in the agenda, link to referenced location
  8835. ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
  8836. (let ((m (or (get-text-property (point) 'org-hd-marker)
  8837. (get-text-property (point) 'org-marker))))
  8838. (when m
  8839. (org-with-point-at m
  8840. (setq agenda-link
  8841. (if (org-called-interactively-p 'any)
  8842. (call-interactively 'org-store-link)
  8843. (org-store-link nil)))))))
  8844. ((eq major-mode 'calendar-mode)
  8845. (let ((cd (calendar-cursor-to-date)))
  8846. (setq link
  8847. (format-time-string
  8848. (car org-time-stamp-formats)
  8849. (apply 'encode-time
  8850. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  8851. nil nil nil))))
  8852. (org-store-link-props :type "calendar" :date cd)))
  8853. ((eq major-mode 'help-mode)
  8854. (setq link (concat "help:" (save-excursion
  8855. (goto-char (point-min))
  8856. (looking-at "^[^ ]+")
  8857. (match-string 0))))
  8858. (org-store-link-props :type "help"))
  8859. ((eq major-mode 'w3-mode)
  8860. (setq cpltxt (if (and (buffer-name)
  8861. (not (string-match "Untitled" (buffer-name))))
  8862. (buffer-name)
  8863. (url-view-url t))
  8864. link (url-view-url t))
  8865. (org-store-link-props :type "w3" :url (url-view-url t)))
  8866. ((eq major-mode 'image-mode)
  8867. (setq cpltxt (concat "file:"
  8868. (abbreviate-file-name buffer-file-name))
  8869. link cpltxt)
  8870. (org-store-link-props :type "image" :file buffer-file-name))
  8871. ;; In dired, store a link to the file of the current line
  8872. ((derived-mode-p 'dired-mode)
  8873. (let ((file (dired-get-filename nil t)))
  8874. (setq file (if file
  8875. (abbreviate-file-name
  8876. (expand-file-name (dired-get-filename nil t)))
  8877. ;; otherwise, no file so use current directory.
  8878. default-directory))
  8879. (setq cpltxt (concat "file:" file)
  8880. link cpltxt)))
  8881. ((setq search (run-hook-with-args-until-success
  8882. 'org-create-file-search-functions))
  8883. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  8884. "::" search))
  8885. (setq cpltxt (or description link)))
  8886. ((and (buffer-file-name (buffer-base-buffer)) (derived-mode-p 'org-mode))
  8887. (setq custom-id (org-entry-get nil "CUSTOM_ID"))
  8888. (cond
  8889. ;; Store a link using the target at point
  8890. ((org-in-regexp "[^<]<<\\([^<>]+\\)>>[^>]" 1)
  8891. (setq cpltxt
  8892. (concat "file:"
  8893. (abbreviate-file-name
  8894. (buffer-file-name (buffer-base-buffer)))
  8895. "::" (match-string 1))
  8896. link cpltxt))
  8897. ((and (featurep 'org-id)
  8898. (or (eq org-id-link-to-org-use-id t)
  8899. (and (org-called-interactively-p 'any)
  8900. (or (eq org-id-link-to-org-use-id 'create-if-interactive)
  8901. (and (eq org-id-link-to-org-use-id
  8902. 'create-if-interactive-and-no-custom-id)
  8903. (not custom-id))))
  8904. (and org-id-link-to-org-use-id (org-entry-get nil "ID"))))
  8905. ;; Store a link using the ID at point
  8906. (setq link (condition-case nil
  8907. (prog1 (org-id-store-link)
  8908. (setq desc (or (plist-get org-store-link-plist
  8909. :description)
  8910. "")))
  8911. (error
  8912. ;; Probably before first headline, link only to file
  8913. (concat "file:"
  8914. (abbreviate-file-name
  8915. (buffer-file-name (buffer-base-buffer))))))))
  8916. (t
  8917. ;; Just link to current headline
  8918. (setq cpltxt (concat "file:"
  8919. (abbreviate-file-name
  8920. (buffer-file-name (buffer-base-buffer)))))
  8921. ;; Add a context search string
  8922. (when (org-xor org-context-in-file-links arg)
  8923. (let* ((element (org-element-at-point))
  8924. (name (org-element-property :name element)))
  8925. (setq txt (cond
  8926. ((org-at-heading-p) nil)
  8927. (name)
  8928. ((org-region-active-p)
  8929. (buffer-substring (region-beginning) (region-end)))))
  8930. (when (or (null txt) (string-match "\\S-" txt))
  8931. (setq cpltxt
  8932. (concat cpltxt "::"
  8933. (condition-case nil
  8934. (org-make-org-heading-search-string txt)
  8935. (error "")))
  8936. desc (or name
  8937. (nth 4 (ignore-errors (org-heading-components)))
  8938. "NONE")))))
  8939. (when (string-match "::\\'" cpltxt)
  8940. (setq cpltxt (substring cpltxt 0 -2)))
  8941. (setq link cpltxt))))
  8942. ((buffer-file-name (buffer-base-buffer))
  8943. ;; Just link to this file here.
  8944. (setq cpltxt (concat "file:"
  8945. (abbreviate-file-name
  8946. (buffer-file-name (buffer-base-buffer)))))
  8947. ;; Add a context string.
  8948. (when (org-xor org-context-in-file-links arg)
  8949. (setq txt (if (org-region-active-p)
  8950. (buffer-substring (region-beginning) (region-end))
  8951. (buffer-substring (point-at-bol) (point-at-eol))))
  8952. ;; Only use search option if there is some text.
  8953. (when (string-match "\\S-" txt)
  8954. (setq cpltxt
  8955. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  8956. desc "NONE")))
  8957. (setq link cpltxt))
  8958. ((org-called-interactively-p 'interactive)
  8959. (user-error "No method for storing a link from this buffer"))
  8960. (t (setq link nil)))
  8961. ;; We're done setting link and desc, clean up
  8962. (when (consp link) (setq cpltxt (car link) link (cdr link)))
  8963. (setq link (or link cpltxt)
  8964. desc (or desc cpltxt))
  8965. (cond ((not desc))
  8966. ((equal desc "NONE") (setq desc nil))
  8967. (t (setq desc
  8968. (replace-regexp-in-string
  8969. org-bracket-link-analytic-regexp
  8970. (lambda (m) (or (match-string 5 m) (match-string 3 m)))
  8971. desc))))
  8972. ;; Return the link
  8973. (if (not (and (or (org-called-interactively-p 'any)
  8974. executing-kbd-macro)
  8975. link))
  8976. (or agenda-link (and link (org-make-link-string link desc)))
  8977. (push (list link desc) org-stored-links)
  8978. (message "Stored: %s" (or desc link))
  8979. (when custom-id
  8980. (setq link (concat "file:" (abbreviate-file-name
  8981. (buffer-file-name)) "::#" custom-id))
  8982. (push (list link desc) org-stored-links))
  8983. (car org-stored-links))))))
  8984. (defun org-store-link-props (&rest plist)
  8985. "Store link properties, extract names, addresses and dates."
  8986. (let ((x (plist-get plist :from)))
  8987. (when x
  8988. (let ((adr (mail-extract-address-components x)))
  8989. (setq plist (plist-put plist :fromname (car adr)))
  8990. (setq plist (plist-put plist :fromaddress (nth 1 adr))))))
  8991. (let ((x (plist-get plist :to)))
  8992. (when x
  8993. (let ((adr (mail-extract-address-components x)))
  8994. (setq plist (plist-put plist :toname (car adr)))
  8995. (setq plist (plist-put plist :toaddress (nth 1 adr))))))
  8996. (let ((x (ignore-errors (date-to-time (plist-get plist :date)))))
  8997. (when x
  8998. (setq plist (plist-put plist :date-timestamp
  8999. (format-time-string
  9000. (org-time-stamp-format t) x)))
  9001. (setq plist (plist-put plist :date-timestamp-inactive
  9002. (format-time-string
  9003. (org-time-stamp-format t t) x)))))
  9004. (let ((from (plist-get plist :from))
  9005. (to (plist-get plist :to)))
  9006. (when (and from to org-from-is-user-regexp)
  9007. (setq plist
  9008. (plist-put plist :fromto
  9009. (if (string-match org-from-is-user-regexp from)
  9010. (concat "to %t")
  9011. (concat "from %f"))))))
  9012. (setq org-store-link-plist plist))
  9013. (defun org-add-link-props (&rest plist)
  9014. "Add these properties to the link property list."
  9015. (let (key value)
  9016. (while plist
  9017. (setq key (pop plist) value (pop plist))
  9018. (setq org-store-link-plist
  9019. (plist-put org-store-link-plist key value)))))
  9020. (defun org-email-link-description (&optional fmt)
  9021. "Return the description part of an email link.
  9022. This takes information from `org-store-link-plist' and formats it
  9023. according to FMT (default from `org-email-link-description-format')."
  9024. (setq fmt (or fmt org-email-link-description-format))
  9025. (let* ((p org-store-link-plist)
  9026. (to (plist-get p :toaddress))
  9027. (from (plist-get p :fromaddress))
  9028. (table
  9029. (list
  9030. (cons "%c" (plist-get p :fromto))
  9031. (cons "%F" (plist-get p :from))
  9032. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  9033. (cons "%T" (plist-get p :to))
  9034. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  9035. (cons "%s" (plist-get p :subject))
  9036. (cons "%d" (plist-get p :date))
  9037. (cons "%m" (plist-get p :message-id)))))
  9038. (when (string-match "%c" fmt)
  9039. ;; Check if the user wrote this message
  9040. (if (and org-from-is-user-regexp from to
  9041. (save-match-data (string-match org-from-is-user-regexp from)))
  9042. (setq fmt (replace-match "to %t" t t fmt))
  9043. (setq fmt (replace-match "from %f" t t fmt))))
  9044. (org-replace-escapes fmt table)))
  9045. (defun org-make-org-heading-search-string (&optional string)
  9046. "Make search string for the current headline or STRING."
  9047. (let ((s (or string
  9048. (and (derived-mode-p 'org-mode)
  9049. (save-excursion
  9050. (org-back-to-heading t)
  9051. (org-element-property :raw-value (org-element-at-point))))))
  9052. (lines org-context-in-file-links))
  9053. (or string (setq s (concat "*" s))) ; Add * for headlines
  9054. (setq s (replace-regexp-in-string "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" "" s))
  9055. (when (and string (integerp lines) (> lines 0))
  9056. (let ((slines (org-split-string s "\n")))
  9057. (when (< lines (length slines))
  9058. (setq s (mapconcat
  9059. 'identity
  9060. (reverse (nthcdr (- (length slines) lines)
  9061. (reverse slines))) "\n")))))
  9062. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  9063. (defun org-make-link-string (link &optional description)
  9064. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  9065. (unless (org-string-nw-p link) (error "Empty link"))
  9066. (let ((uri (cond ((string-match org-link-types-re link)
  9067. (concat (match-string 1 link)
  9068. (org-link-escape (substring link (match-end 1)))))
  9069. ;; For readability, url-encode internal links only
  9070. ;; when absolutely needed (i.e, when they contain
  9071. ;; square brackets). File links however, are
  9072. ;; encoded since, e.g., spaces are significant.
  9073. ((or (file-name-absolute-p link)
  9074. (org-string-match-p "\\`\\.\\.?/\\|[][]" link))
  9075. (org-link-escape link))
  9076. (t link)))
  9077. (description
  9078. (and (org-string-nw-p description)
  9079. ;; Remove brackets from description, as they are fatal.
  9080. (replace-regexp-in-string
  9081. "[][]" (lambda (m) (if (equal "[" m) "{" "}"))
  9082. (org-trim description)))))
  9083. (format "[[%s]%s]"
  9084. uri
  9085. (if description (format "[%s]" description) ""))))
  9086. (defconst org-link-escape-chars
  9087. ;;%20 %5B %5D %25
  9088. '(?\s ?\[ ?\] ?%)
  9089. "List of characters that should be escaped in a link when stored to Org.
  9090. This is the list that is used for internal purposes.")
  9091. (defconst org-link-escape-chars-browser
  9092. ;;%20 %22
  9093. '(?\s ?\")
  9094. "List of characters to be escaped before handing over to the browser.
  9095. If you consider using this constant then you probably want to use
  9096. the function `org-link-escape-browser' instead. See there why
  9097. this constant is a candidate to be removed once Org drops support
  9098. for Emacs 24.1 and 24.2.")
  9099. (defun org-link-escape (text &optional table merge)
  9100. "Return percent escaped representation of TEXT.
  9101. TEXT is a string with the text to escape.
  9102. Optional argument TABLE is a list with characters that should be
  9103. escaped. When nil, `org-link-escape-chars' is used.
  9104. If optional argument MERGE is set, merge TABLE into
  9105. `org-link-escape-chars'."
  9106. (let ((characters-to-encode
  9107. (cond ((null table) org-link-escape-chars)
  9108. (merge (append org-link-escape-chars table))
  9109. (t table))))
  9110. (mapconcat
  9111. (lambda (c)
  9112. (if (or (memq c characters-to-encode)
  9113. (and org-url-hexify-p (or (< c 32) (> c 126))))
  9114. (mapconcat (lambda (e) (format "%%%.2X" e))
  9115. (or (encode-coding-char c 'utf-8)
  9116. (error "Unable to percent escape character: %c" c))
  9117. "")
  9118. (char-to-string c)))
  9119. text "")))
  9120. (defun org-link-escape-browser (text)
  9121. "Escape some characters before handing over to the browser.
  9122. This function is a candidate to be removed together with the
  9123. constant `org-link-escape-chars-browser' once Org drops support
  9124. for Emacs 24.1 and 24.2. All calls to this function will have to
  9125. be replaced with `url-encode-url' which is available since Emacs
  9126. 24.3.1."
  9127. ;; Example with the Org link
  9128. ;; [[http://lists.gnu.org/archive/cgi-bin/namazu.cgi?idxname=emacs-orgmode&query=%252Bsubject:"Release+8.2"]]
  9129. ;; to open the browser with +subject:"Release 8.2" filled into the
  9130. ;; query field: In this case the variable TEXT contains the
  9131. ;; unescaped [...]=%2Bsubject:"Release+8.2". Then `url-encode-url'
  9132. ;; converts correctly to [...]=%2Bsubject:%22Release+8.2%22 or
  9133. ;; `org-link-escape' with `org-link-escape-chars-browser' converts
  9134. ;; wrongly to [...]=%252Bsubject:%22Release+8.2%22.
  9135. (if (fboundp 'url-encode-url)
  9136. (url-encode-url text)
  9137. (if (org-string-match-p
  9138. (concat "[[:nonascii:]" org-link-escape-chars-browser "]")
  9139. text)
  9140. (org-link-escape text org-link-escape-chars-browser)
  9141. text)))
  9142. (defun org-link-unescape (str)
  9143. "Unhex hexified Unicode parts in string STR.
  9144. E.g. `%C3%B6' becomes the german o-Umlaut. This is the
  9145. reciprocal of `org-link-escape', which see."
  9146. (if (org-string-nw-p str)
  9147. (replace-regexp-in-string
  9148. "\\(%[0-9A-Za-z]\\{2\\}\\)+" #'org-link-unescape-compound str t t)
  9149. str))
  9150. (defun org-link-unescape-compound (hex)
  9151. "Unhexify Unicode hex-chars. E.g. `%C3%B6' is the German o-Umlaut.
  9152. Note: this function also decodes single byte encodings like
  9153. `%E1' (a-acute) if not followed by another `%[A-F0-9]{2}' group."
  9154. (save-match-data
  9155. (let* ((bytes (cdr (split-string hex "%")))
  9156. (ret "")
  9157. (eat 0)
  9158. (sum 0))
  9159. (while bytes
  9160. (let* ((val (string-to-number (pop bytes) 16))
  9161. (shift-xor
  9162. (if (= 0 eat)
  9163. (cond
  9164. ((>= val 252) (cons 6 252))
  9165. ((>= val 248) (cons 5 248))
  9166. ((>= val 240) (cons 4 240))
  9167. ((>= val 224) (cons 3 224))
  9168. ((>= val 192) (cons 2 192))
  9169. (t (cons 0 0)))
  9170. (cons 6 128))))
  9171. (when (>= val 192) (setq eat (car shift-xor)))
  9172. (setq val (logxor val (cdr shift-xor)))
  9173. (setq sum (+ (lsh sum (car shift-xor)) val))
  9174. (when (> eat 0) (setq eat (- eat 1)))
  9175. (cond
  9176. ((= 0 eat) ;multi byte
  9177. (setq ret (concat ret (org-char-to-string sum)))
  9178. (setq sum 0))
  9179. ((not bytes) ; single byte(s)
  9180. (setq ret (org-link-unescape-single-byte-sequence hex))))))
  9181. ret)))
  9182. (defun org-link-unescape-single-byte-sequence (hex)
  9183. "Unhexify hex-encoded single byte character sequences."
  9184. (mapconcat (lambda (byte)
  9185. (char-to-string (string-to-number byte 16)))
  9186. (cdr (split-string hex "%")) ""))
  9187. (defun org-xor (a b)
  9188. "Exclusive or."
  9189. (if a (not b) b))
  9190. (defun org-fixup-message-id-for-http (s)
  9191. "Replace special characters in a message id, so it can be used in an http query."
  9192. (when (string-match "%" s)
  9193. (setq s (mapconcat (lambda (c)
  9194. (if (eq c ?%)
  9195. "%25"
  9196. (char-to-string c)))
  9197. s "")))
  9198. (while (string-match "<" s)
  9199. (setq s (replace-match "%3C" t t s)))
  9200. (while (string-match ">" s)
  9201. (setq s (replace-match "%3E" t t s)))
  9202. (while (string-match "@" s)
  9203. (setq s (replace-match "%40" t t s)))
  9204. s)
  9205. (defun org-link-prettify (link)
  9206. "Return a human-readable representation of LINK.
  9207. The car of LINK must be a raw link.
  9208. The cdr of LINK must be either a link description or nil."
  9209. (let ((desc (or (cadr link) "<no description>")))
  9210. (concat (format "%-45s" (substring desc 0 (min (length desc) 40)))
  9211. "<" (car link) ">")))
  9212. ;;;###autoload
  9213. (defun org-insert-link-global ()
  9214. "Insert a link like Org-mode does.
  9215. This command can be called in any mode to insert a link in Org-mode syntax."
  9216. (interactive)
  9217. (org-load-modules-maybe)
  9218. (org-run-like-in-org-mode 'org-insert-link))
  9219. (defun org-insert-all-links (arg &optional pre post)
  9220. "Insert all links in `org-stored-links'.
  9221. When a universal prefix, do not delete the links from `org-stored-links'.
  9222. When `ARG' is a number, insert the last N link(s).
  9223. `PRE' and `POST' are optional arguments to define a string to
  9224. prepend or to append."
  9225. (interactive "P")
  9226. (let ((org-keep-stored-link-after-insertion (equal arg '(4)))
  9227. (links (copy-sequence org-stored-links))
  9228. (pr (or pre "- "))
  9229. (po (or post "\n"))
  9230. (cnt 1) l)
  9231. (if (null org-stored-links)
  9232. (message "No link to insert")
  9233. (while (and (or (listp arg) (>= arg cnt))
  9234. (setq l (if (listp arg)
  9235. (pop links)
  9236. (pop org-stored-links))))
  9237. (setq cnt (1+ cnt))
  9238. (insert pr)
  9239. (org-insert-link nil (car l) (or (cadr l) "<no description>"))
  9240. (insert po)))))
  9241. (defun org-insert-last-stored-link (arg)
  9242. "Insert the last link stored in `org-stored-links'."
  9243. (interactive "p")
  9244. (org-insert-all-links arg "" "\n"))
  9245. (defun org-link-fontify-links-to-this-file ()
  9246. "Fontify links to the current file in `org-stored-links'."
  9247. (let ((f (buffer-file-name)) a b)
  9248. (setq a (mapcar (lambda(l)
  9249. (let ((ll (car l)))
  9250. (when (and (string-match "^file:\\(.+\\)::" ll)
  9251. (equal f (expand-file-name (match-string 1 ll))))
  9252. ll)))
  9253. org-stored-links))
  9254. (when (featurep 'org-id)
  9255. (setq b (mapcar (lambda(l)
  9256. (let ((ll (car l)))
  9257. (when (and (string-match "^id:\\(.+\\)$" ll)
  9258. (equal f (expand-file-name
  9259. (or (org-id-find-id-file
  9260. (match-string 1 ll)) ""))))
  9261. ll)))
  9262. org-stored-links)))
  9263. (mapcar (lambda(l)
  9264. (put-text-property 0 (length l) 'face 'font-lock-comment-face l))
  9265. (delq nil (append a b)))))
  9266. (defvar org--links-history nil)
  9267. (defun org-insert-link (&optional complete-file link-location default-description)
  9268. "Insert a link. At the prompt, enter the link.
  9269. Completion can be used to insert any of the link protocol prefixes like
  9270. http or ftp in use.
  9271. The history can be used to select a link previously stored with
  9272. `org-store-link'. When the empty string is entered (i.e. if you just
  9273. press RET at the prompt), the link defaults to the most recently
  9274. stored link. As SPC triggers completion in the minibuffer, you need to
  9275. use M-SPC or C-q SPC to force the insertion of a space character.
  9276. You will also be prompted for a description, and if one is given, it will
  9277. be displayed in the buffer instead of the link.
  9278. If there is already a link at point, this command will allow you to edit link
  9279. and description parts.
  9280. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  9281. be selected using completion. The path to the file will be relative to the
  9282. current directory if the file is in the current directory or a subdirectory.
  9283. Otherwise, the link will be the absolute path as completed in the minibuffer
  9284. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  9285. option `org-link-file-path-type'.
  9286. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  9287. the current directory or below.
  9288. With three \\[universal-argument] prefixes, negate the meaning of
  9289. `org-keep-stored-link-after-insertion'.
  9290. If `org-make-link-description-function' is non-nil, this function will be
  9291. called with the link target, and the result will be the default
  9292. link description.
  9293. If the LINK-LOCATION parameter is non-nil, this value will be
  9294. used as the link location instead of reading one interactively.
  9295. If the DEFAULT-DESCRIPTION parameter is non-nil, this value will
  9296. be used as the default description."
  9297. (interactive "P")
  9298. (let* ((wcf (current-window-configuration))
  9299. (origbuf (current-buffer))
  9300. (region (when (org-region-active-p)
  9301. (buffer-substring (region-beginning) (region-end))))
  9302. (remove (and region (list (region-beginning) (region-end))))
  9303. (desc region)
  9304. (link link-location)
  9305. (abbrevs org-link-abbrev-alist-local)
  9306. entry all-prefixes auto-desc)
  9307. (cond
  9308. (link-location) ; specified by arg, just use it.
  9309. ((org-in-regexp org-bracket-link-regexp 1)
  9310. ;; We do have a link at point, and we are going to edit it.
  9311. (setq remove (list (match-beginning 0) (match-end 0)))
  9312. (setq desc (when (match-end 3) (org-match-string-no-properties 3)))
  9313. (setq link (read-string "Link: "
  9314. (org-link-unescape
  9315. (org-match-string-no-properties 1)))))
  9316. ((or (org-in-regexp org-angle-link-re)
  9317. (org-in-regexp org-plain-link-re))
  9318. ;; Convert to bracket link
  9319. (setq remove (list (match-beginning 0) (match-end 0))
  9320. link (read-string "Link: "
  9321. (org-remove-angle-brackets (match-string 0)))))
  9322. ((member complete-file '((4) (16)))
  9323. ;; Completing read for file names.
  9324. (setq link (org-file-complete-link complete-file)))
  9325. (t
  9326. ;; Read link, with completion for stored links.
  9327. (org-link-fontify-links-to-this-file)
  9328. (org-switch-to-buffer-other-window "*Org Links*")
  9329. (with-current-buffer "*Org Links*"
  9330. (erase-buffer)
  9331. (insert "Insert a link.
  9332. Use TAB to complete link prefixes, then RET for type-specific completion support\n")
  9333. (when org-stored-links
  9334. (insert "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  9335. (insert (mapconcat 'org-link-prettify
  9336. (reverse org-stored-links) "\n")))
  9337. (goto-char (point-min)))
  9338. (let ((cw (selected-window)))
  9339. (select-window (get-buffer-window "*Org Links*" 'visible))
  9340. (with-current-buffer "*Org Links*" (setq truncate-lines t))
  9341. (unless (pos-visible-in-window-p (point-max))
  9342. (org-fit-window-to-buffer))
  9343. (and (window-live-p cw) (select-window cw)))
  9344. (setq all-prefixes (append (mapcar 'car abbrevs)
  9345. (mapcar 'car org-link-abbrev-alist)
  9346. org-link-types))
  9347. (unwind-protect
  9348. ;; Fake a link history, containing the stored links.
  9349. (let ((org--links-history
  9350. (append (mapcar #'car org-stored-links)
  9351. org-insert-link-history)))
  9352. (setq link
  9353. (org-completing-read
  9354. "Link: "
  9355. (append
  9356. (mapcar (lambda (x) (concat x ":")) all-prefixes)
  9357. (mapcar #'car org-stored-links))
  9358. nil nil nil
  9359. 'org--links-history
  9360. (caar org-stored-links)))
  9361. (unless (org-string-nw-p link) (user-error "No link selected"))
  9362. (dolist (l org-stored-links)
  9363. (when (equal link (cadr l))
  9364. (setq link (car l))
  9365. (setq auto-desc t)))
  9366. (when (or (member link all-prefixes)
  9367. (and (equal ":" (substring link -1))
  9368. (member (substring link 0 -1) all-prefixes)
  9369. (setq link (substring link 0 -1))))
  9370. (setq link (with-current-buffer origbuf
  9371. (org-link-try-special-completion link)))))
  9372. (set-window-configuration wcf)
  9373. (kill-buffer "*Org Links*"))
  9374. (setq entry (assoc link org-stored-links))
  9375. (or entry (push link org-insert-link-history))
  9376. (setq desc (or desc (nth 1 entry)))))
  9377. (when (funcall (if (equal complete-file '(64)) 'not 'identity)
  9378. (not org-keep-stored-link-after-insertion))
  9379. (setq org-stored-links (delq (assoc link org-stored-links)
  9380. org-stored-links)))
  9381. (when (and (string-match org-plain-link-re link)
  9382. (not (string-match org-ts-regexp link)))
  9383. ;; URL-like link, normalize the use of angular brackets.
  9384. (setq link (org-remove-angle-brackets link)))
  9385. ;; Check if we are linking to the current file with a search
  9386. ;; option If yes, simplify the link by using only the search
  9387. ;; option.
  9388. (when (and buffer-file-name
  9389. (string-match "^file:\\(.+?\\)::\\(.+\\)" link))
  9390. (let* ((path (match-string 1 link))
  9391. (case-fold-search nil)
  9392. (search (match-string 2 link)))
  9393. (save-match-data
  9394. (when (equal (file-truename buffer-file-name) (file-truename path))
  9395. ;; We are linking to this same file, with a search option
  9396. (setq link search)))))
  9397. ;; Check if we can/should use a relative path. If yes, simplify the link
  9398. (when (string-match "^\\(file:\\|docview:\\)\\(.*\\)" link)
  9399. (let* ((type (match-string 1 link))
  9400. (path (match-string 2 link))
  9401. (origpath path)
  9402. (case-fold-search nil))
  9403. (cond
  9404. ((or (eq org-link-file-path-type 'absolute)
  9405. (equal complete-file '(16)))
  9406. (setq path (abbreviate-file-name (expand-file-name path))))
  9407. ((eq org-link-file-path-type 'noabbrev)
  9408. (setq path (expand-file-name path)))
  9409. ((eq org-link-file-path-type 'relative)
  9410. (setq path (file-relative-name path)))
  9411. (t
  9412. (save-match-data
  9413. (if (string-match (concat "^" (regexp-quote
  9414. (expand-file-name
  9415. (file-name-as-directory
  9416. default-directory))))
  9417. (expand-file-name path))
  9418. ;; We are linking a file with relative path name.
  9419. (setq path (substring (expand-file-name path)
  9420. (match-end 0)))
  9421. (setq path (abbreviate-file-name (expand-file-name path)))))))
  9422. (setq link (concat type path))
  9423. (when (equal desc origpath)
  9424. (setq desc path))))
  9425. (if org-make-link-description-function
  9426. (setq desc
  9427. (or (condition-case nil
  9428. (funcall org-make-link-description-function link desc)
  9429. (error (progn (message "Can't get link description from `%s'"
  9430. (symbol-name org-make-link-description-function))
  9431. (sit-for 2) nil)))
  9432. (read-string "Description: " default-description)))
  9433. (if default-description (setq desc default-description)
  9434. (setq desc (or (and auto-desc desc)
  9435. (read-string "Description: " desc)))))
  9436. (unless (string-match "\\S-" desc) (setq desc nil))
  9437. (when remove (apply 'delete-region remove))
  9438. (insert (org-make-link-string link desc))
  9439. ;; Redisplay so as the new link has proper invisible characters.
  9440. (sit-for 0)))
  9441. (defun org-link-try-special-completion (type)
  9442. "If there is completion support for link type TYPE, offer it."
  9443. (let ((fun (intern (concat "org-" type "-complete-link"))))
  9444. (if (functionp fun)
  9445. (funcall fun)
  9446. (read-string "Link (no completion support): " (concat type ":")))))
  9447. (defun org-file-complete-link (&optional arg)
  9448. "Create a file link using completion."
  9449. (let ((file (read-file-name "File: "))
  9450. (pwd (file-name-as-directory (expand-file-name ".")))
  9451. (pwd1 (file-name-as-directory (abbreviate-file-name
  9452. (expand-file-name ".")))))
  9453. (cond ((equal arg '(16))
  9454. (concat "file:"
  9455. (abbreviate-file-name (expand-file-name file))))
  9456. ((string-match
  9457. (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  9458. (concat "file:" (match-string 1 file)))
  9459. ((string-match
  9460. (concat "^" (regexp-quote pwd) "\\(.+\\)")
  9461. (expand-file-name file))
  9462. (concat "file:"
  9463. (match-string 1 (expand-file-name file))))
  9464. (t (concat "file:" file)))))
  9465. (defun org-completing-read (&rest args)
  9466. "Completing-read with SPACE being a normal character."
  9467. (let ((enable-recursive-minibuffers t)
  9468. (minibuffer-local-completion-map
  9469. (copy-keymap minibuffer-local-completion-map)))
  9470. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  9471. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  9472. (org-defkey minibuffer-local-completion-map (kbd "C-c !")
  9473. 'org-time-stamp-inactive)
  9474. (apply #'completing-read args)))
  9475. (define-obsolete-function-alias
  9476. 'org-completing-read-no-i 'completing-read "Org 9.0")
  9477. (define-obsolete-function-alias
  9478. 'org-icompleting-read 'completing-read "Org 9.0")
  9479. (define-obsolete-function-alias
  9480. 'org-iread-file-name 'read-file-name "Org 9.0")
  9481. ;;; Opening/following a link
  9482. (defvar org-link-search-failed nil)
  9483. (defvar org-open-link-functions nil
  9484. "Hook for functions finding a plain text link.
  9485. These functions must take a single argument, the link content.
  9486. They will be called for links that look like [[link text][description]]
  9487. when LINK TEXT does not have a protocol like \"http:\" and does not look
  9488. like a filename (e.g. \"./blue.png\").
  9489. These functions will be called *before* Org attempts to resolve the
  9490. link by doing text searches in the current buffer - so if you want a
  9491. link \"[[target]]\" to still find \"<<target>>\", your function should
  9492. handle this as a special case.
  9493. When the function does handle the link, it must return a non-nil value.
  9494. If it decides that it is not responsible for this link, it must return
  9495. nil to indicate that that Org-mode can continue with other options
  9496. like exact and fuzzy text search.")
  9497. (defun org-next-link (&optional search-backward)
  9498. "Move forward to the next link.
  9499. If the link is in hidden text, expose it."
  9500. (interactive "P")
  9501. (when (and org-link-search-failed (eq this-command last-command))
  9502. (goto-char (point-min))
  9503. (message "Link search wrapped back to beginning of buffer"))
  9504. (setq org-link-search-failed nil)
  9505. (let* ((pos (point))
  9506. (ct (org-context))
  9507. (a (assoc :link ct))
  9508. (srch-fun (if search-backward 're-search-backward 're-search-forward)))
  9509. (cond (a (goto-char (nth (if search-backward 1 2) a)))
  9510. ((looking-at org-any-link-re)
  9511. ;; Don't stay stuck at link without an org-link face
  9512. (forward-char (if search-backward -1 1))))
  9513. (if (funcall srch-fun org-any-link-re nil t)
  9514. (progn
  9515. (goto-char (match-beginning 0))
  9516. (when (outline-invisible-p) (org-show-context)))
  9517. (goto-char pos)
  9518. (setq org-link-search-failed t)
  9519. (message "No further link found"))))
  9520. (defun org-previous-link ()
  9521. "Move backward to the previous link.
  9522. If the link is in hidden text, expose it."
  9523. (interactive)
  9524. (funcall 'org-next-link t))
  9525. (defun org-translate-link (s)
  9526. "Translate a link string if a translation function has been defined."
  9527. (with-temp-buffer
  9528. (insert (org-trim s))
  9529. (org-trim (org-element-interpret-data (org-element-context)))))
  9530. (defun org-translate-link-from-planner (type path)
  9531. "Translate a link from Emacs Planner syntax so that Org can follow it.
  9532. This is still an experimental function, your mileage may vary."
  9533. (cond
  9534. ((member type '("http" "https" "news" "ftp"))
  9535. ;; standard Internet links are the same.
  9536. nil)
  9537. ((and (equal type "irc") (string-match "^//" path))
  9538. ;; Planner has two / at the beginning of an irc link, we have 1.
  9539. ;; We should have zero, actually....
  9540. (setq path (substring path 1)))
  9541. ((and (equal type "lisp") (string-match "^/" path))
  9542. ;; Planner has a slash, we do not.
  9543. (setq type "elisp" path (substring path 1)))
  9544. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  9545. ;; A typical message link. Planner has the id after the final slash,
  9546. ;; we separate it with a hash mark
  9547. (setq path (concat (match-string 1 path) "#"
  9548. (org-remove-angle-brackets (match-string 2 path))))))
  9549. (cons type path))
  9550. (defun org-find-file-at-mouse (ev)
  9551. "Open file link or URL at mouse."
  9552. (interactive "e")
  9553. (mouse-set-point ev)
  9554. (org-open-at-point 'in-emacs))
  9555. (defun org-open-at-mouse (ev)
  9556. "Open file link or URL at mouse.
  9557. See the docstring of `org-open-file' for details."
  9558. (interactive "e")
  9559. (mouse-set-point ev)
  9560. (when (eq major-mode 'org-agenda-mode)
  9561. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  9562. (org-open-at-point))
  9563. (defvar org-window-config-before-follow-link nil
  9564. "The window configuration before following a link.
  9565. This is saved in case the need arises to restore it.")
  9566. ;;;###autoload
  9567. (defun org-open-at-point-global ()
  9568. "Follow a link or time-stamp like Org mode does.
  9569. This command can be called in any mode to follow an external link
  9570. or a time-stamp that has Org mode syntax. Its behavior is
  9571. undefined when called on internal links (e.g., fuzzy links).
  9572. Raise an error when there is nothing to follow. "
  9573. (interactive)
  9574. (cond ((org-in-regexp org-any-link-re)
  9575. (org-open-link-from-string (match-string-no-properties 0)))
  9576. ((or (org-in-regexp org-ts-regexp-both nil t)
  9577. (org-in-regexp org-tsr-regexp-both nil t))
  9578. (org-follow-timestamp-link))
  9579. (t (user-error "No link found"))))
  9580. ;;;###autoload
  9581. (defun org-open-link-from-string (s &optional arg reference-buffer)
  9582. "Open a link in the string S, as if it was in Org-mode."
  9583. (interactive "sLink: \nP")
  9584. (let ((reference-buffer (or reference-buffer (current-buffer))))
  9585. (with-temp-buffer
  9586. (let ((org-inhibit-startup (not reference-buffer)))
  9587. (org-mode)
  9588. (insert s)
  9589. (goto-char (point-min))
  9590. (when reference-buffer
  9591. (setq org-link-abbrev-alist-local
  9592. (with-current-buffer reference-buffer
  9593. org-link-abbrev-alist-local)))
  9594. (org-open-at-point arg reference-buffer)))))
  9595. (defvar org-open-at-point-functions nil
  9596. "Hook that is run when following a link at point.
  9597. Functions in this hook must return t if they identify and follow
  9598. a link at point. If they don't find anything interesting at point,
  9599. they must return nil.")
  9600. (defvar org-link-search-inhibit-query nil) ;; dynamically scoped
  9601. (defvar clean-buffer-list-kill-buffer-names) ; Defined in midnight.el
  9602. (defun org-open-at-point (&optional arg reference-buffer)
  9603. "Open link, timestamp, footnote or tags at point.
  9604. When point is on a link, follow it. Normally, files will be
  9605. opened by an appropriate application. If the optional prefix
  9606. argument ARG is non-nil, Emacs will visit the file. With
  9607. a double prefix argument, try to open outside of Emacs, in the
  9608. application the system uses for this file type.
  9609. When point is on a timestamp, open the agenda at the day
  9610. specified.
  9611. When point is a footnote definition, move to the first reference
  9612. found. If it is on a reference, move to the associated
  9613. definition.
  9614. When point is on a headline, display a list of every link in the
  9615. entry, so it is possible to pick one, or all, of them. If point
  9616. is on a tag, call `org-tags-view' instead.
  9617. When optional argument REFERENCE-BUFFER is non-nil, it should
  9618. specify a buffer from where the link search should happen. This
  9619. is used internally by `org-open-link-from-string'.
  9620. On top of syntactically correct links, this function will open
  9621. the link at point in comments or comment blocks and the first
  9622. link in a property drawer line."
  9623. (interactive "P")
  9624. ;; On a code block, open block's results.
  9625. (unless (call-interactively 'org-babel-open-src-block-result)
  9626. (org-load-modules-maybe)
  9627. (setq org-window-config-before-follow-link (current-window-configuration))
  9628. (org-remove-occur-highlights nil nil t)
  9629. (unless (run-hook-with-args-until-success 'org-open-at-point-functions)
  9630. (let* ((context
  9631. ;; Only consider supported types, even if they are not
  9632. ;; the closest one.
  9633. (org-element-lineage
  9634. (org-element-context)
  9635. '(clock comment comment-block footnote-definition
  9636. footnote-reference headline inlinetask keyword link
  9637. node-property timestamp)
  9638. t))
  9639. (type (org-element-type context))
  9640. (value (org-element-property :value context)))
  9641. (cond
  9642. ((not context) (user-error "No link found"))
  9643. ;; Exception: open timestamps and links in properties
  9644. ;; drawers, keywords and comments.
  9645. ((memq type '(comment comment-block keyword node-property))
  9646. (call-interactively #'org-open-at-point-global))
  9647. ;; On a headline or an inlinetask, but not on a timestamp,
  9648. ;; a link, a footnote reference or on tags.
  9649. ((and (memq type '(headline inlinetask))
  9650. ;; Not on tags.
  9651. (progn (save-excursion (beginning-of-line)
  9652. (looking-at org-complex-heading-regexp))
  9653. (or (not (match-beginning 5))
  9654. (< (point) (match-beginning 5)))))
  9655. (let* ((data (org-offer-links-in-entry (current-buffer) (point) arg))
  9656. (links (car data))
  9657. (links-end (cdr data)))
  9658. (if links
  9659. (dolist (link (if (stringp links) (list links) links))
  9660. (search-forward link nil links-end)
  9661. (goto-char (match-beginning 0))
  9662. (org-open-at-point))
  9663. (require 'org-attach)
  9664. (org-attach-reveal 'if-exists))))
  9665. ;; On a clock line, make sure point is on the timestamp
  9666. ;; before opening it.
  9667. ((and (eq type 'clock)
  9668. value
  9669. (>= (point) (org-element-property :begin value))
  9670. (<= (point) (org-element-property :end value)))
  9671. (org-follow-timestamp-link))
  9672. ;; Do nothing on white spaces after an object.
  9673. ((>= (point)
  9674. (save-excursion
  9675. (goto-char (org-element-property :end context))
  9676. (skip-chars-backward " \t")
  9677. (point)))
  9678. (user-error "No link found"))
  9679. ((eq type 'timestamp) (org-follow-timestamp-link))
  9680. ;; On tags within a headline or an inlinetask.
  9681. ((and (memq type '(headline inlinetask))
  9682. (progn (save-excursion (beginning-of-line)
  9683. (looking-at org-complex-heading-regexp))
  9684. (and (match-beginning 5)
  9685. (>= (point) (match-beginning 5)))))
  9686. (org-tags-view arg (substring (match-string 5) 0 -1)))
  9687. ((eq type 'link)
  9688. ;; When link is located within the description of another
  9689. ;; link (e.g., an inline image), always open the parent
  9690. ;; link.
  9691. (let*((link (let ((up (org-element-property :parent context)))
  9692. (if (eq (org-element-type up) 'link) up context)))
  9693. (type (org-element-property :type link))
  9694. (path (org-link-unescape (org-element-property :path link))))
  9695. ;; Switch back to REFERENCE-BUFFER needed when called in
  9696. ;; a temporary buffer through `org-open-link-from-string'.
  9697. (with-current-buffer (or reference-buffer (current-buffer))
  9698. (cond
  9699. ((equal type "file")
  9700. (if (string-match "[*?{]" (file-name-nondirectory path))
  9701. (dired path)
  9702. ;; Look into `org-link-protocols' in order to find
  9703. ;; a DEDICATED-FUNCTION to open file. The function
  9704. ;; will be applied on raw link instead of parsed
  9705. ;; link due to the limitation in `org-add-link-type'
  9706. ;; ("open" function called with a single argument).
  9707. ;; If no such function is found, fallback to
  9708. ;; `org-open-file'.
  9709. ;;
  9710. ;; Note : "file+emacs" and "file+sys" types are
  9711. ;; hard-coded in order to escape the previous
  9712. ;; limitation.
  9713. (let* ((option (org-element-property :search-option link))
  9714. (app (org-element-property :application link))
  9715. (dedicated-function
  9716. (nth 1 (assoc app org-link-protocols))))
  9717. (if dedicated-function
  9718. (funcall dedicated-function
  9719. (concat path
  9720. (and option (concat "::" option))))
  9721. (apply #'org-open-file
  9722. path
  9723. (cond (arg)
  9724. ((equal app "emacs") 'emacs)
  9725. ((equal app "sys") 'system))
  9726. (cond ((not option) nil)
  9727. ((org-string-match-p "\\`[0-9]+\\'" option)
  9728. (list (string-to-number option)))
  9729. (t (list nil
  9730. (org-link-unescape option)))))))))
  9731. ((assoc type org-link-protocols)
  9732. (funcall (nth 1 (assoc type org-link-protocols)) path))
  9733. ((equal type "help")
  9734. (let ((f-or-v (intern path)))
  9735. (cond ((fboundp f-or-v) (describe-function f-or-v))
  9736. ((boundp f-or-v) (describe-variable f-or-v))
  9737. (t (error "Not a known function or variable")))))
  9738. ((member type '("http" "https" "ftp" "mailto" "news"))
  9739. (browse-url (org-link-escape-browser (concat type ":" path))))
  9740. ((equal type "doi")
  9741. (browse-url
  9742. (org-link-escape-browser (concat org-doi-server-url path))))
  9743. ((equal type "message") (browse-url (concat type ":" path)))
  9744. ((equal type "shell")
  9745. (let ((buf (generate-new-buffer "*Org Shell Output*"))
  9746. (cmd path))
  9747. (if (or (and (org-string-nw-p
  9748. org-confirm-shell-link-not-regexp)
  9749. (string-match
  9750. org-confirm-shell-link-not-regexp cmd))
  9751. (not org-confirm-shell-link-function)
  9752. (funcall org-confirm-shell-link-function
  9753. (format "Execute \"%s\" in shell? "
  9754. (org-add-props cmd nil
  9755. 'face 'org-warning))))
  9756. (progn
  9757. (message "Executing %s" cmd)
  9758. (shell-command cmd buf)
  9759. (when (featurep 'midnight)
  9760. (setq clean-buffer-list-kill-buffer-names
  9761. (cons (buffer-name buf)
  9762. clean-buffer-list-kill-buffer-names))))
  9763. (user-error "Abort"))))
  9764. ((equal type "elisp")
  9765. (let ((cmd path))
  9766. (if (or (and (org-string-nw-p
  9767. org-confirm-elisp-link-not-regexp)
  9768. (org-string-match-p
  9769. org-confirm-elisp-link-not-regexp cmd))
  9770. (not org-confirm-elisp-link-function)
  9771. (funcall org-confirm-elisp-link-function
  9772. (format "Execute \"%s\" as elisp? "
  9773. (org-add-props cmd nil
  9774. 'face 'org-warning))))
  9775. (message "%s => %s" cmd
  9776. (if (eq (string-to-char cmd) ?\()
  9777. (eval (read cmd))
  9778. (call-interactively (read cmd))))
  9779. (user-error "Abort"))))
  9780. ((equal type "id")
  9781. (require 'org-id)
  9782. (funcall (nth 1 (assoc "id" org-link-protocols)) path))
  9783. ((member type '("coderef" "custom-id" "fuzzy" "radio"))
  9784. (unless (run-hook-with-args-until-success
  9785. 'org-open-link-functions path)
  9786. (if (not arg) (org-mark-ring-push)
  9787. (switch-to-buffer-other-window
  9788. (org-get-buffer-for-internal-link (current-buffer))))
  9789. (let ((destination
  9790. (org-with-wide-buffer
  9791. (if (equal type "radio")
  9792. (org-search-radio-target
  9793. (org-element-property :path link))
  9794. (org-link-search
  9795. (if (member type '("custom-id" "coderef"))
  9796. (org-element-property :raw-link link)
  9797. path)
  9798. ;; Prevent fuzzy links from matching
  9799. ;; themselves.
  9800. (and (equal type "fuzzy")
  9801. (+ 2 (org-element-property :begin link)))))
  9802. (point))))
  9803. (unless (and (<= (point-min) destination)
  9804. (>= (point-max) destination))
  9805. (widen))
  9806. (goto-char destination))))
  9807. (t (browse-url-at-point))))))
  9808. ;; On a footnote reference or at a footnote definition's label.
  9809. ((or (eq type 'footnote-reference)
  9810. (and (eq type 'footnote-definition)
  9811. (save-excursion
  9812. ;; Do not validate action when point is on the
  9813. ;; spaces right after the footnote label, in
  9814. ;; order to be on par with behaviour on links.
  9815. (skip-chars-forward " \t")
  9816. (let ((begin
  9817. (org-element-property :contents-begin context)))
  9818. (if begin (< (point) begin)
  9819. (= (org-element-property :post-affiliated context)
  9820. (line-beginning-position)))))))
  9821. (org-footnote-action))
  9822. (t (user-error "No link found")))))
  9823. (run-hook-with-args 'org-follow-link-hook)))
  9824. (defun org-offer-links-in-entry (buffer marker &optional nth zero)
  9825. "Offer links in the current entry and return the selected link.
  9826. If there is only one link, return it.
  9827. If NTH is an integer, return the NTH link found.
  9828. If ZERO is a string, check also this string for a link, and if
  9829. there is one, return it."
  9830. (with-current-buffer buffer
  9831. (save-excursion
  9832. (save-restriction
  9833. (widen)
  9834. (goto-char marker)
  9835. (let ((cnt ?0)
  9836. have-zero end links link c)
  9837. (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
  9838. (push (match-string 0 zero) links)
  9839. (setq cnt (1- cnt) have-zero t))
  9840. (save-excursion
  9841. (org-back-to-heading t)
  9842. (setq end (save-excursion (outline-next-heading) (point)))
  9843. (while (re-search-forward org-any-link-re end t)
  9844. (push (match-string 0) links))
  9845. (setq links (org-uniquify (reverse links))))
  9846. (cond
  9847. ((null links)
  9848. (message "No links"))
  9849. ((equal (length links) 1)
  9850. (setq link (car links)))
  9851. ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
  9852. (setq link (nth (if have-zero nth (1- nth)) links)))
  9853. (t ; we have to select a link
  9854. (save-excursion
  9855. (save-window-excursion
  9856. (delete-other-windows)
  9857. (with-output-to-temp-buffer "*Select Link*"
  9858. (dolist (l links)
  9859. (cond
  9860. ((not (string-match org-bracket-link-regexp l))
  9861. (princ (format "[%c] %s\n" (cl-incf cnt)
  9862. (org-remove-angle-brackets l))))
  9863. ((match-end 3)
  9864. (princ (format "[%c] %s (%s)\n" (cl-incf cnt)
  9865. (match-string 3 l) (match-string 1 l))))
  9866. (t (princ (format "[%c] %s\n" (cl-incf cnt)
  9867. (match-string 1 l)))))))
  9868. (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
  9869. (message "Select link to open, RET to open all:")
  9870. (setq c (read-char-exclusive))
  9871. (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
  9872. (when (equal c ?q) (user-error "Abort"))
  9873. (if (equal c ?\C-m)
  9874. (setq link links)
  9875. (setq nth (- c ?0))
  9876. (when have-zero (setq nth (1+ nth)))
  9877. (unless (and (integerp nth) (>= (length links) nth))
  9878. (user-error "Invalid link selection"))
  9879. (setq link (nth (1- nth) links)))))
  9880. (cons link end))))))
  9881. ;; TODO: These functions are deprecated since `org-open-at-point'
  9882. ;; hard-codes behaviour for "file+emacs" and "file+sys" types.
  9883. (defun org-open-file-with-system (path)
  9884. "Open file at PATH using the system way of opening it."
  9885. (org-open-file path 'system))
  9886. (defun org-open-file-with-emacs (path)
  9887. "Open file at PATH in Emacs."
  9888. (org-open-file path 'emacs))
  9889. ;;; File search
  9890. (defvar org-create-file-search-functions nil
  9891. "List of functions to construct the right search string for a file link.
  9892. These functions are called in turn with point at the location to
  9893. which the link should point.
  9894. A function in the hook should first test if it would like to
  9895. handle this file type, for example by checking the `major-mode'
  9896. or the file extension. If it decides not to handle this file, it
  9897. should just return nil to give other functions a chance. If it
  9898. does handle the file, it must return the search string to be used
  9899. when following the link. The search string will be part of the
  9900. file link, given after a double colon, and `org-open-at-point'
  9901. will automatically search for it. If special measures must be
  9902. taken to make the search successful, another function should be
  9903. added to the companion hook `org-execute-file-search-functions',
  9904. which see.
  9905. A function in this hook may also use `setq' to set the variable
  9906. `description' to provide a suggestion for the descriptive text to
  9907. be used for this link when it gets inserted into an Org-mode
  9908. buffer with \\[org-insert-link].")
  9909. (defvar org-execute-file-search-functions nil
  9910. "List of functions to execute a file search triggered by a link.
  9911. Functions added to this hook must accept a single argument, the
  9912. search string that was part of the file link, the part after the
  9913. double colon. The function must first check if it would like to
  9914. handle this search, for example by checking the `major-mode' or
  9915. the file extension. If it decides not to handle this search, it
  9916. should just return nil to give other functions a chance. If it
  9917. does handle the search, it must return a non-nil value to keep
  9918. other functions from trying.
  9919. Each function can access the current prefix argument through the
  9920. variable `current-prefix-arg'. Note that a single prefix is used
  9921. to force opening a link in Emacs, so it may be good to only use a
  9922. numeric or double prefix to guide the search function.
  9923. In case this is needed, a function in this hook can also restore
  9924. the window configuration before `org-open-at-point' was called using:
  9925. \(set-window-configuration org-window-config-before-follow-link)")
  9926. (defun org-search-radio-target (target)
  9927. "Search a radio target matching TARGET in current buffer.
  9928. White spaces are not significant."
  9929. (let ((re (format "<<<%s>>>"
  9930. (mapconcat #'regexp-quote
  9931. (org-split-string target "[ \t\n]+")
  9932. "[ \t]+\\(?:\n[ \t]*\\)?")))
  9933. (origin (point)))
  9934. (goto-char (point-min))
  9935. (catch :radio-match
  9936. (while (re-search-forward re nil t)
  9937. (backward-char)
  9938. (let ((object (org-element-context)))
  9939. (when (eq (org-element-type object) 'radio-target)
  9940. (goto-char (org-element-property :begin object))
  9941. (org-show-context 'link-search)
  9942. (throw :radio-match nil))))
  9943. (goto-char origin)
  9944. (user-error "No match for radio target: %s" target))))
  9945. (defun org-link-search (s &optional avoid-pos stealth)
  9946. "Search for a search string S.
  9947. If S starts with \"#\", it triggers a custom ID search.
  9948. If S is enclosed within parenthesis, it initiates a coderef
  9949. search.
  9950. If S is surrounded by forward slashes, it is interpreted as
  9951. a regular expression. In Org mode files, this will create an
  9952. `org-occur' sparse tree. In ordinary files, `occur' will be used
  9953. to list matches. If the current buffer is in `dired-mode', grep
  9954. will be used to search in all files.
  9955. When AVOID-POS is given, ignore matches near that position.
  9956. When optional argument STEALTH is non-nil, do not modify
  9957. visibility around point, thus ignoring `org-show-context-detail'
  9958. variable.
  9959. Search is case-insensitive and ignores white spaces. Return type
  9960. of matched result, with is either `dedicated' or `fuzzy'."
  9961. (unless (org-string-nw-p s) (error "Invalid search string \"%s\"" s))
  9962. (let* ((case-fold-search t)
  9963. (origin (point))
  9964. (normalized (replace-regexp-in-string "\n[ \t]*" " " s))
  9965. (words (org-split-string s "[ \t\n]+"))
  9966. (s-multi-re (mapconcat #'regexp-quote words "[ \t]+\\(?:\n[ \t]*\\)?"))
  9967. (s-single-re (mapconcat #'regexp-quote words "[ \t]+"))
  9968. type)
  9969. (cond
  9970. ;; Check if there are any special search functions.
  9971. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  9972. ((eq (string-to-char s) ?#)
  9973. ;; Look for a custom ID S if S starts with "#".
  9974. (let* ((id (substring normalized 1))
  9975. (match (org-find-property "CUSTOM_ID" id)))
  9976. (if match (progn (goto-char match) (setf type 'dedicated))
  9977. (error "No match for custom ID: %s" id))))
  9978. ((string-match "\\`(\\(.*\\))\\'" normalized)
  9979. ;; Look for coderef targets if S is enclosed within parenthesis.
  9980. (let ((coderef (match-string-no-properties 1 normalized))
  9981. (re (substring s-single-re 1 -1)))
  9982. (goto-char (point-min))
  9983. (catch :coderef-match
  9984. (while (re-search-forward re nil t)
  9985. (let ((element (org-element-at-point)))
  9986. (when (and (memq (org-element-type element)
  9987. '(example-block src-block))
  9988. ;; Build proper regexp according to current
  9989. ;; block's label format.
  9990. (let ((label-fmt
  9991. (regexp-quote
  9992. (or (org-element-property :label-fmt element)
  9993. org-coderef-label-format))))
  9994. (save-excursion
  9995. (beginning-of-line)
  9996. (looking-at (format ".*?\\(%s\\)[ \t]*$"
  9997. (format label-fmt coderef))))))
  9998. (setq type 'dedicated)
  9999. (goto-char (match-beginning 1))
  10000. (throw :coderef-match nil))))
  10001. (goto-char origin)
  10002. (error "No match for coderef: %s" coderef))))
  10003. ((string-match "\\`/\\(.*\\)/\\'" normalized)
  10004. ;; Look for a regular expression.
  10005. (funcall (if (derived-mode-p 'org-mode) #'org-occur #'org-do-occur)
  10006. (match-string 1 s)))
  10007. ;; Fuzzy links.
  10008. (t
  10009. (let ((starred (eq (string-to-char normalized) ?*)))
  10010. (cond
  10011. ;; Look for targets, only if not in a headline search.
  10012. ((and (not starred)
  10013. (let ((target (format "<<%s>>" s-multi-re)))
  10014. (catch :target-match
  10015. (goto-char (point-min))
  10016. (while (re-search-forward target nil t)
  10017. (backward-char)
  10018. (let ((context (org-element-context)))
  10019. (when (eq (org-element-type context) 'target)
  10020. (setq type 'dedicated)
  10021. (goto-char (org-element-property :begin context))
  10022. (throw :target-match t))))
  10023. nil))))
  10024. ;; Look for elements named after S, only if not in a headline
  10025. ;; search.
  10026. ((and (not starred)
  10027. (let ((name (format "^[ \t]*#\\+NAME: +%s[ \t]*$" s-single-re)))
  10028. (catch :name-match
  10029. (goto-char (point-min))
  10030. (while (re-search-forward name nil t)
  10031. (let ((element (org-element-at-point)))
  10032. (when (equal (org-split-string
  10033. (org-element-property :name element)
  10034. "[ \t]+")
  10035. words)
  10036. (setq type 'dedicated)
  10037. (beginning-of-line)
  10038. (throw :name-match t))))
  10039. nil))))
  10040. ;; Regular text search. Prefer headlines in Org mode
  10041. ;; buffers.
  10042. ((and (derived-mode-p 'org-mode)
  10043. (let* ((wspace "[ \t]")
  10044. (wspaceopt (concat wspace "*"))
  10045. (cookie (concat "\\(?:"
  10046. wspaceopt
  10047. "\\[[0-9]*\\(?:%\\|/[0-9]*\\)\\]"
  10048. wspaceopt
  10049. "\\)"))
  10050. (sep (concat "\\(?:\\(?:" wspace "\\|" cookie "\\)+\\)"))
  10051. (re (concat
  10052. org-outline-regexp-bol
  10053. "\\(?:" org-todo-regexp "[ \t]+\\)?"
  10054. "\\(?:\\[#.\\][ \t]+\\)?"
  10055. "\\(?:" org-comment-string "[ \t]+\\)?"
  10056. sep "?"
  10057. (let ((title (mapconcat #'regexp-quote
  10058. words
  10059. sep)))
  10060. (if starred (substring title 1) title))
  10061. sep "?"
  10062. (org-re "\\(?:[ \t]+:[[:alnum:]_@#%%:]+:\\)?")
  10063. "[ \t]*$")))
  10064. (goto-char (point-min))
  10065. (re-search-forward re nil t)))
  10066. (goto-char (match-beginning 0))
  10067. (setq type 'dedicated))
  10068. ;; Offer to create non-existent headline depending on
  10069. ;; `org-link-search-must-match-exact-headline'.
  10070. ((and (derived-mode-p 'org-mode)
  10071. (not org-link-search-inhibit-query)
  10072. (eq org-link-search-must-match-exact-headline 'query-to-create)
  10073. (yes-or-no-p "No match - create this as a new heading? "))
  10074. (goto-char (point-max))
  10075. (unless (bolp) (newline))
  10076. (org-insert-heading nil t t)
  10077. (insert s "\n")
  10078. (beginning-of-line 0))
  10079. ;; Only headlines are looked after. No need to process
  10080. ;; further: throw an error.
  10081. ((and (derived-mode-p 'org-mode)
  10082. (or starred org-link-search-must-match-exact-headline))
  10083. (goto-char origin)
  10084. (error "No match for fuzzy expression: %s" normalized))
  10085. ;; Regular text search.
  10086. ((catch :fuzzy-match
  10087. (goto-char (point-min))
  10088. (while (re-search-forward s-multi-re nil t)
  10089. ;; Skip match if it contains AVOID-POS or it is included
  10090. ;; in a link with a description but outside the
  10091. ;; description.
  10092. (unless (or (and avoid-pos
  10093. (<= (match-beginning 0) avoid-pos)
  10094. (> (match-end 0) avoid-pos))
  10095. (and (save-match-data
  10096. (org-in-regexp org-bracket-link-regexp))
  10097. (match-beginning 3)
  10098. (or (> (match-beginning 3) (point))
  10099. (<= (match-end 3) (point)))
  10100. (org-element-lineage
  10101. (save-match-data (org-element-context))
  10102. '(link) t)))
  10103. (goto-char (match-beginning 0))
  10104. (setq type 'fuzzy)
  10105. (throw :fuzzy-match t)))
  10106. nil))
  10107. ;; All failed. Throw an error.
  10108. (t (goto-char origin)
  10109. (error "No match for fuzzy expression: %s" normalized))))))
  10110. ;; Disclose surroundings of match, if appropriate.
  10111. (when (and (derived-mode-p 'org-mode) (not stealth))
  10112. (org-show-context 'link-search))
  10113. type))
  10114. (defun org-get-buffer-for-internal-link (buffer)
  10115. "Return a buffer to be used for displaying the link target of internal links."
  10116. (cond
  10117. ((not org-display-internal-link-with-indirect-buffer)
  10118. buffer)
  10119. ((string-match "(Clone)$" (buffer-name buffer))
  10120. (message "Buffer is already a clone, not making another one")
  10121. ;; we also do not modify visibility in this case
  10122. buffer)
  10123. (t ; make a new indirect buffer for displaying the link
  10124. (let* ((bn (buffer-name buffer))
  10125. (ibn (concat bn "(Clone)"))
  10126. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  10127. (with-current-buffer ib (org-overview))
  10128. ib))))
  10129. (defun org-do-occur (regexp &optional cleanup)
  10130. "Call the Emacs command `occur'.
  10131. If CLEANUP is non-nil, remove the printout of the regular expression
  10132. in the *Occur* buffer. This is useful if the regex is long and not useful
  10133. to read."
  10134. (occur regexp)
  10135. (when cleanup
  10136. (let ((cwin (selected-window)) win beg end)
  10137. (when (setq win (get-buffer-window "*Occur*"))
  10138. (select-window win))
  10139. (goto-char (point-min))
  10140. (when (re-search-forward "match[a-z]+" nil t)
  10141. (setq beg (match-end 0))
  10142. (when (re-search-forward "^[ \t]*[0-9]+" nil t)
  10143. (setq end (1- (match-beginning 0)))))
  10144. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  10145. (goto-char (point-min))
  10146. (select-window cwin))))
  10147. ;;; The mark ring for links jumps
  10148. (defvar org-mark-ring nil
  10149. "Mark ring for positions before jumps in Org-mode.")
  10150. (defvar org-mark-ring-last-goto nil
  10151. "Last position in the mark ring used to go back.")
  10152. ;; Fill and close the ring
  10153. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  10154. (dotimes (_ org-mark-ring-length)
  10155. (push (make-marker) org-mark-ring))
  10156. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  10157. org-mark-ring)
  10158. (defun org-mark-ring-push (&optional pos buffer)
  10159. "Put the current position or POS into the mark ring and rotate it."
  10160. (interactive)
  10161. (setq pos (or pos (point)))
  10162. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  10163. (move-marker (car org-mark-ring)
  10164. (or pos (point))
  10165. (or buffer (current-buffer)))
  10166. (message "%s"
  10167. (substitute-command-keys
  10168. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  10169. (defun org-mark-ring-goto (&optional n)
  10170. "Jump to the previous position in the mark ring.
  10171. With prefix arg N, jump back that many stored positions. When
  10172. called several times in succession, walk through the entire ring.
  10173. Org-mode commands jumping to a different position in the current file,
  10174. or to another Org-mode file, automatically push the old position
  10175. onto the ring."
  10176. (interactive "p")
  10177. (let (p m)
  10178. (if (eq last-command this-command)
  10179. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  10180. (setq p org-mark-ring))
  10181. (setq org-mark-ring-last-goto p)
  10182. (setq m (car p))
  10183. (org-pop-to-buffer-same-window (marker-buffer m))
  10184. (goto-char m)
  10185. (when (or (outline-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  10186. ;;; TODO: Use string-remove-prefix and -suffix once we only support
  10187. ;;; Emacs 24.4+
  10188. (defun org-remove-angle-brackets (s)
  10189. (when (equal (substring s 0 1) "<") (setq s (substring s 1)))
  10190. (when (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  10191. s)
  10192. (defun org-add-angle-brackets (s)
  10193. (unless (equal (substring s 0 1) "<") (setq s (concat "<" s)))
  10194. (unless (equal (substring s -1) ">") (setq s (concat s ">")))
  10195. s)
  10196. (defun org-remove-double-quotes (s)
  10197. (when (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  10198. (when (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  10199. s)
  10200. ;;; Following specific links
  10201. (defvar org-agenda-buffer-tmp-name)
  10202. (defvar org-agenda-start-on-weekday)
  10203. (defun org-follow-timestamp-link ()
  10204. "Open an agenda view for the time-stamp date/range at point."
  10205. (cond
  10206. ((org-at-date-range-p t)
  10207. (let ((org-agenda-start-on-weekday)
  10208. (t1 (match-string 1))
  10209. (t2 (match-string 2)) tt1 tt2)
  10210. (setq tt1 (time-to-days (org-time-string-to-time t1))
  10211. tt2 (time-to-days (org-time-string-to-time t2)))
  10212. (let ((org-agenda-buffer-tmp-name
  10213. (format "*Org Agenda(a:%s)"
  10214. (concat (substring t1 0 10) "--" (substring t2 0 10)))))
  10215. (org-agenda-list nil tt1 (1+ (- tt2 tt1))))))
  10216. ((org-at-timestamp-p t)
  10217. (let ((org-agenda-buffer-tmp-name
  10218. (format "*Org Agenda(a:%s)" (substring (match-string 1) 0 10))))
  10219. (org-agenda-list nil (time-to-days (org-time-string-to-time
  10220. (substring (match-string 1) 0 10)))
  10221. 1)))
  10222. (t (error "This should not happen"))))
  10223. ;;; Following file links
  10224. (declare-function mailcap-parse-mailcaps "mailcap" (&optional path force))
  10225. (declare-function mailcap-extension-to-mime "mailcap" (extn))
  10226. (declare-function mailcap-mime-info
  10227. "mailcap" (string &optional request no-decode))
  10228. (defvar org-wait nil)
  10229. (defun org-open-file (path &optional in-emacs line search)
  10230. "Open the file at PATH.
  10231. First, this expands any special file name abbreviations. Then the
  10232. configuration variable `org-file-apps' is checked if it contains an
  10233. entry for this file type, and if yes, the corresponding command is launched.
  10234. If no application is found, Emacs simply visits the file.
  10235. With optional prefix argument IN-EMACS, Emacs will visit the file.
  10236. With a double \\[universal-argument] \\[universal-argument] \
  10237. prefix arg, Org tries to avoid opening in Emacs
  10238. and to use an external application to visit the file.
  10239. Optional LINE specifies a line to go to, optional SEARCH a string
  10240. to search for. If LINE or SEARCH is given, the file will be
  10241. opened in Emacs, unless an entry from org-file-apps that makes
  10242. use of groups in a regexp matches.
  10243. If you want to change the way frames are used when following a
  10244. link, please customize `org-link-frame-setup'.
  10245. If the file does not exist, an error is thrown."
  10246. (let* ((file (if (equal path "")
  10247. buffer-file-name
  10248. (substitute-in-file-name (expand-file-name path))))
  10249. (file-apps (append org-file-apps (org-default-apps)))
  10250. (apps (cl-remove-if
  10251. 'org-file-apps-entry-match-against-dlink-p file-apps))
  10252. (apps-dlink (cl-remove-if-not
  10253. 'org-file-apps-entry-match-against-dlink-p file-apps))
  10254. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  10255. (dirp (unless remp (file-directory-p file)))
  10256. (file (if (and dirp org-open-directory-means-index-dot-org)
  10257. (concat (file-name-as-directory file) "index.org")
  10258. file))
  10259. (a-m-a-p (assq 'auto-mode apps))
  10260. (dfile (downcase file))
  10261. ;; Reconstruct the original link from the PATH, LINE and
  10262. ;; SEARCH args.
  10263. (link (cond (line (concat file "::" (number-to-string line)))
  10264. (search (concat file "::" search))
  10265. (t file)))
  10266. (dlink (downcase link))
  10267. (old-buffer (current-buffer))
  10268. (old-pos (point))
  10269. (old-mode major-mode)
  10270. (ext
  10271. (and (string-match "\\`.*?\\.\\([a-zA-Z0-9]+\\(\\.gz\\)?\\)\\'" dfile)
  10272. (match-string 1 dfile)))
  10273. cmd link-match-data)
  10274. (cond
  10275. ((member in-emacs '((16) system))
  10276. (setq cmd (cdr (assq 'system apps))))
  10277. (in-emacs (setq cmd 'emacs))
  10278. (t
  10279. (setq cmd (or (and remp (cdr (assq 'remote apps)))
  10280. (and dirp (cdr (assq 'directory apps)))
  10281. ;; First, try matching against apps-dlink if we
  10282. ;; get a match here, store the match data for
  10283. ;; later.
  10284. (let ((match (assoc-default dlink apps-dlink
  10285. 'string-match)))
  10286. (if match
  10287. (progn (setq link-match-data (match-data))
  10288. match)
  10289. (progn (setq in-emacs (or in-emacs line search))
  10290. nil))) ; if we have no match in apps-dlink,
  10291. ; always open the file in emacs if line or search
  10292. ; is given (for backwards compatibility)
  10293. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  10294. 'string-match)
  10295. (cdr (assoc ext apps))
  10296. (cdr (assq t apps))))))
  10297. (when (eq cmd 'system)
  10298. (setq cmd (cdr (assoc 'system apps))))
  10299. (when (eq cmd 'default)
  10300. (setq cmd (cdr (assoc t apps))))
  10301. (when (eq cmd 'mailcap)
  10302. (require 'mailcap)
  10303. (mailcap-parse-mailcaps)
  10304. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  10305. (command (mailcap-mime-info mime-type)))
  10306. (if (stringp command)
  10307. (setq cmd command)
  10308. (setq cmd 'emacs))))
  10309. (when (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  10310. (not (file-exists-p file))
  10311. (not org-open-non-existing-files))
  10312. (user-error "No such file: %s" file))
  10313. (cond
  10314. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  10315. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  10316. (while (string-match "['\"]%s['\"]" cmd)
  10317. (setq cmd (replace-match "%s" t t cmd)))
  10318. (while (string-match "%s" cmd)
  10319. (setq cmd (replace-match
  10320. (save-match-data
  10321. (shell-quote-argument
  10322. (convert-standard-filename file)))
  10323. t t cmd)))
  10324. ;; Replace "%1", "%2" etc. in command with group matches from regex
  10325. (save-match-data
  10326. (let ((match-index 1)
  10327. (number-of-groups (- (/ (length link-match-data) 2) 1)))
  10328. (set-match-data link-match-data)
  10329. (while (<= match-index number-of-groups)
  10330. (let ((regex (concat "%" (number-to-string match-index)))
  10331. (replace-with (match-string match-index dlink)))
  10332. (while (string-match regex cmd)
  10333. (setq cmd (replace-match replace-with t t cmd))))
  10334. (setq match-index (+ match-index 1)))))
  10335. (save-window-excursion
  10336. (message "Running %s...done" cmd)
  10337. (start-process-shell-command cmd nil cmd)
  10338. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))))
  10339. ((or (stringp cmd)
  10340. (eq cmd 'emacs))
  10341. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  10342. (widen)
  10343. (cond (line (org-goto-line line)
  10344. (when (derived-mode-p 'org-mode) (org-reveal)))
  10345. (search (org-link-search search))))
  10346. ((functionp cmd)
  10347. (save-match-data
  10348. (set-match-data link-match-data)
  10349. (condition-case nil
  10350. (funcall cmd file link)
  10351. ;; FIXME: Remove this check when most default installations
  10352. ;; of Emacs have at least Org 9.0.
  10353. ((debug wrong-number-of-arguments wrong-type-argument
  10354. invalid-function)
  10355. (user-error "Please see Org News for version 9.0 about \
  10356. `org-file-apps'--Lisp error: %S" cmd)))))
  10357. ((consp cmd)
  10358. ;; FIXME: Remove this check when most default installations of
  10359. ;; Emacs have at least Org 9.0.
  10360. ;; Heads-up instead of silently fall back to
  10361. ;; `org-link-frame-setup' for an old usage of `org-file-apps'
  10362. ;; with sexp instead of a function for `cmd'.
  10363. (user-error "Please see Org News for version 9.0 about \
  10364. `org-file-apps'--Error: Deprecated usage of %S" cmd))
  10365. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  10366. (and (derived-mode-p 'org-mode)
  10367. (eq old-mode 'org-mode)
  10368. (or (not (eq old-buffer (current-buffer)))
  10369. (not (eq old-pos (point))))
  10370. (org-mark-ring-push old-pos old-buffer))))
  10371. (defun org-file-apps-entry-match-against-dlink-p (entry)
  10372. "This function returns non-nil if `entry' uses a regular
  10373. expression which should be matched against the whole link by
  10374. org-open-file.
  10375. It assumes that is the case when the entry uses a regular
  10376. expression which has at least one grouping construct and the
  10377. action is either a lisp form or a command string containing
  10378. `%1', i.e. using at least one subexpression match as a
  10379. parameter."
  10380. (let ((selector (car entry))
  10381. (action (cdr entry)))
  10382. (if (stringp selector)
  10383. (and (> (regexp-opt-depth selector) 0)
  10384. (or (and (stringp action)
  10385. (string-match "%[0-9]" action))
  10386. (consp action)))
  10387. nil)))
  10388. (defun org-default-apps ()
  10389. "Return the default applications for this operating system."
  10390. (cond
  10391. ((eq system-type 'darwin)
  10392. org-file-apps-defaults-macosx)
  10393. ((eq system-type 'windows-nt)
  10394. org-file-apps-defaults-windowsnt)
  10395. (t org-file-apps-defaults-gnu)))
  10396. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  10397. "Convert extensions to regular expressions in the cars of LIST.
  10398. Also, weed out any non-string entries, because the return value is used
  10399. only for regexp matching.
  10400. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  10401. point to the symbol `emacs', indicating that the file should
  10402. be opened in Emacs."
  10403. (append
  10404. (delq nil
  10405. (mapcar (lambda (x)
  10406. (unless (not (stringp (car x)))
  10407. (if (string-match "\\W" (car x))
  10408. x
  10409. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  10410. list))
  10411. (when add-auto-mode
  10412. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  10413. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  10414. (defun org-file-remote-p (file)
  10415. "Test whether FILE specifies a location on a remote system.
  10416. Return non-nil if the location is indeed remote.
  10417. For example, the filename \"/user@host:/foo\" specifies a location
  10418. on the system \"/user@host:\"."
  10419. (cond ((fboundp 'file-remote-p)
  10420. (file-remote-p file))
  10421. ((fboundp 'tramp-handle-file-remote-p)
  10422. (tramp-handle-file-remote-p file))
  10423. ((and (boundp 'ange-ftp-name-format)
  10424. (string-match (car ange-ftp-name-format) file))
  10425. t)))
  10426. ;;;; Refiling
  10427. (defun org-get-org-file ()
  10428. "Read a filename, with default directory `org-directory'."
  10429. (let ((default (or org-default-notes-file remember-data-file)))
  10430. (read-file-name (format "File name [%s]: " default)
  10431. (file-name-as-directory org-directory)
  10432. default)))
  10433. (defun org-notes-order-reversed-p ()
  10434. "Check if the current file should receive notes in reversed order."
  10435. (cond
  10436. ((not org-reverse-note-order) nil)
  10437. ((eq t org-reverse-note-order) t)
  10438. ((not (listp org-reverse-note-order)) nil)
  10439. (t (catch 'exit
  10440. (dolist (entry org-reverse-note-order)
  10441. (when (string-match (car entry) buffer-file-name)
  10442. (throw 'exit (cdr entry))))))))
  10443. (defvar org-refile-target-table nil
  10444. "The list of refile targets, created by `org-refile'.")
  10445. (defvar org-agenda-new-buffers nil
  10446. "Buffers created to visit agenda files.")
  10447. (defvar org-refile-cache nil
  10448. "Cache for refile targets.")
  10449. (defvar org-refile-markers nil
  10450. "All the markers used for caching refile locations.")
  10451. (defun org-refile-marker (pos)
  10452. "Get a new refile marker, but only if caching is in use."
  10453. (if (not org-refile-use-cache)
  10454. pos
  10455. (let ((m (make-marker)))
  10456. (move-marker m pos)
  10457. (push m org-refile-markers)
  10458. m)))
  10459. (defun org-refile-cache-clear ()
  10460. "Clear the refile cache and disable all the markers."
  10461. (dolist (m org-refile-markers) (move-marker m nil))
  10462. (setq org-refile-markers nil)
  10463. (setq org-refile-cache nil)
  10464. (message "Refile cache has been cleared"))
  10465. (defun org-refile-cache-check-set (set)
  10466. "Check if all the markers in the cache still have live buffers."
  10467. (let (marker)
  10468. (catch 'exit
  10469. (while (and set (setq marker (nth 3 (pop set))))
  10470. ;; If `org-refile-use-outline-path' is 'file, marker may be nil
  10471. (when (and marker (null (marker-buffer marker)))
  10472. (message "Please regenerate the refile cache with `C-0 C-c C-w'")
  10473. (sit-for 3)
  10474. (throw 'exit nil)))
  10475. t)))
  10476. (defun org-refile-cache-put (set &rest identifiers)
  10477. "Push the refile targets SET into the cache, under IDENTIFIERS."
  10478. (let* ((key (sha1 (prin1-to-string identifiers)))
  10479. (entry (assoc key org-refile-cache)))
  10480. (if entry
  10481. (setcdr entry set)
  10482. (push (cons key set) org-refile-cache))))
  10483. (defun org-refile-cache-get (&rest identifiers)
  10484. "Retrieve the cached value for refile targets given by IDENTIFIERS."
  10485. (cond
  10486. ((not org-refile-cache) nil)
  10487. ((not org-refile-use-cache) (org-refile-cache-clear) nil)
  10488. (t
  10489. (let ((set (cdr (assoc (sha1 (prin1-to-string identifiers))
  10490. org-refile-cache))))
  10491. (and set (org-refile-cache-check-set set) set)))))
  10492. (defvar org-outline-path-cache nil
  10493. "Alist between buffer positions and outline paths.
  10494. It value is an alist (POSITION . PATH) where POSITION is the
  10495. buffer position at the beginning of an entry and PATH is a list
  10496. of strings describing the outline path for that entry, in reverse
  10497. order.")
  10498. (defun org-refile-get-targets (&optional default-buffer)
  10499. "Produce a table with refile targets."
  10500. (let ((case-fold-search nil)
  10501. ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
  10502. (entries (or org-refile-targets '((nil . (:level . 1)))))
  10503. targets tgs files desc descre)
  10504. (message "Getting targets...")
  10505. (with-current-buffer (or default-buffer (current-buffer))
  10506. (dolist (entry entries)
  10507. (setq files (car entry) desc (cdr entry))
  10508. (cond
  10509. ((null files) (setq files (list (current-buffer))))
  10510. ((eq files 'org-agenda-files)
  10511. (setq files (org-agenda-files 'unrestricted)))
  10512. ((and (symbolp files) (fboundp files))
  10513. (setq files (funcall files)))
  10514. ((and (symbolp files) (boundp files))
  10515. (setq files (symbol-value files))))
  10516. (when (stringp files) (setq files (list files)))
  10517. (cond
  10518. ((eq (car desc) :tag)
  10519. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  10520. ((eq (car desc) :todo)
  10521. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  10522. ((eq (car desc) :regexp)
  10523. (setq descre (cdr desc)))
  10524. ((eq (car desc) :level)
  10525. (setq descre (concat "^\\*\\{" (number-to-string
  10526. (if org-odd-levels-only
  10527. (1- (* 2 (cdr desc)))
  10528. (cdr desc)))
  10529. "\\}[ \t]")))
  10530. ((eq (car desc) :maxlevel)
  10531. (setq descre (concat "^\\*\\{1," (number-to-string
  10532. (if org-odd-levels-only
  10533. (1- (* 2 (cdr desc)))
  10534. (cdr desc)))
  10535. "\\}[ \t]")))
  10536. (t (error "Bad refiling target description %s" desc)))
  10537. (dolist (f files)
  10538. (with-current-buffer (if (bufferp f) f (org-get-agenda-file-buffer f))
  10539. (or
  10540. (setq tgs (org-refile-cache-get (buffer-file-name) descre))
  10541. (progn
  10542. (when (bufferp f)
  10543. (setq f (buffer-file-name (buffer-base-buffer f))))
  10544. (setq f (and f (expand-file-name f)))
  10545. (when (eq org-refile-use-outline-path 'file)
  10546. (push (list (file-name-nondirectory f) f nil nil) tgs))
  10547. (org-with-wide-buffer
  10548. (goto-char (point-min))
  10549. (setq org-outline-path-cache nil)
  10550. (while (re-search-forward descre nil t)
  10551. (beginning-of-line)
  10552. (looking-at org-complex-heading-regexp)
  10553. (let ((begin (point))
  10554. (heading (org-match-string-no-properties 4)))
  10555. (unless (or (and
  10556. org-refile-target-verify-function
  10557. (not
  10558. (funcall org-refile-target-verify-function)))
  10559. (not heading))
  10560. (let ((re (format org-complex-heading-regexp-format
  10561. (regexp-quote heading)))
  10562. (target
  10563. (if (not org-refile-use-outline-path) heading
  10564. (mapconcat
  10565. #'org-protect-slash
  10566. (append
  10567. (pcase org-refile-use-outline-path
  10568. (`file (list (file-name-nondirectory
  10569. (buffer-file-name
  10570. (buffer-base-buffer)))))
  10571. (`full-file-path
  10572. (list (buffer-file-name
  10573. (buffer-base-buffer))))
  10574. (_ nil))
  10575. (org-get-outline-path t t))
  10576. "/"))))
  10577. (push (list target f re (org-refile-marker (point)))
  10578. tgs)))
  10579. (when (= (point) begin)
  10580. ;; Verification function has not moved point.
  10581. (end-of-line)))))))
  10582. (when org-refile-use-cache
  10583. (org-refile-cache-put tgs (buffer-file-name) descre))
  10584. (setq targets (append tgs targets))))))
  10585. (message "Getting targets...done")
  10586. (nreverse targets)))
  10587. (defun org-protect-slash (s)
  10588. (while (string-match "/" s)
  10589. (setq s (replace-match "\\" t t s)))
  10590. s)
  10591. (defun org--get-outline-path-1 (&optional use-cache)
  10592. "Return outline path to current headline.
  10593. Outline path is a list of strings, in reverse order. When
  10594. optional argument USE-CACHE is non-nil, make use of a cache. See
  10595. `org-get-outline-path' for details.
  10596. Assume buffer is widened and point is on a headline."
  10597. (or (and use-cache (cdr (assq (point) org-outline-path-cache)))
  10598. (let ((p (point))
  10599. (heading (progn
  10600. (looking-at org-complex-heading-regexp)
  10601. (if (not (match-end 4)) ""
  10602. ;; Remove statistics cookies.
  10603. (org-trim
  10604. (org-link-display-format
  10605. (replace-regexp-in-string
  10606. "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" ""
  10607. (org-match-string-no-properties 4))))))))
  10608. (if (org-up-heading-safe)
  10609. (let ((path (cons heading (org--get-outline-path-1 use-cache))))
  10610. (when use-cache
  10611. (push (cons p path) org-outline-path-cache))
  10612. path)
  10613. ;; This is a new root node. Since we assume we are moving
  10614. ;; forward, we can drop previous cache so as to limit number
  10615. ;; of associations there.
  10616. (let ((path (list heading)))
  10617. (when use-cache (setq org-outline-path-cache (list (cons p path))))
  10618. path)))))
  10619. (defun org-get-outline-path (&optional with-self use-cache)
  10620. "Return the outline path to the current entry.
  10621. An outline path is a list of ancestors for current headline, as
  10622. a list of strings. Statistics cookies are removed and links are
  10623. replaced with their description, if any, or their path otherwise.
  10624. When optional argument WITH-SELF is non-nil, the path also
  10625. includes the current headline.
  10626. When optional argument USE-CACHE is non-nil, cache outline paths
  10627. between calls to this function so as to avoid backtracking. This
  10628. argument is useful when planning to find more than one outline
  10629. path in the same document. In that case, there are two
  10630. conditions to satisfy:
  10631. - `org-outline-path-cache' is set to nil before starting the
  10632. process;
  10633. - outline paths are computed by increasing buffer positions."
  10634. (org-with-wide-buffer
  10635. (and (or (and with-self (org-back-to-heading t))
  10636. (org-up-heading-safe))
  10637. (reverse (org--get-outline-path-1 use-cache)))))
  10638. (defun org-format-outline-path (path &optional width prefix separator)
  10639. "Format the outline path PATH for display.
  10640. WIDTH is the maximum number of characters that is available.
  10641. PREFIX is a prefix to be included in the returned string,
  10642. such as the file name.
  10643. SEPARATOR is inserted between the different parts of the path,
  10644. the default is \"/\"."
  10645. (setq width (or width 79))
  10646. (setq path (delq nil path))
  10647. (unless (> width 0)
  10648. (user-error "Argument `width' must be positive"))
  10649. (setq separator (or separator "/"))
  10650. (let* ((org-odd-levels-only nil)
  10651. (fpath (concat
  10652. prefix (and prefix path separator)
  10653. (mapconcat
  10654. (lambda (s) (replace-regexp-in-string "[ \t]+\\'" "" s))
  10655. (cl-loop for head in path
  10656. for n from 0
  10657. collect (org-add-props
  10658. head nil 'face
  10659. (nth (% n org-n-level-faces) org-level-faces)))
  10660. separator))))
  10661. (when (> (length fpath) width)
  10662. (if (< width 7)
  10663. ;; It's unlikely that `width' will be this small, but don't
  10664. ;; waste characters by adding ".." if it is.
  10665. (setq fpath (substring fpath 0 width))
  10666. (setf (substring fpath (- width 2)) "..")))
  10667. fpath))
  10668. (defun org-display-outline-path (&optional file current separator just-return-string)
  10669. "Display the current outline path in the echo area.
  10670. If FILE is non-nil, prepend the output with the file name.
  10671. If CURRENT is non-nil, append the current heading to the output.
  10672. SEPARATOR is passed through to `org-format-outline-path'. It separates
  10673. the different parts of the path and defaults to \"/\".
  10674. If JUST-RETURN-STRING is non-nil, return a string, don't display a message."
  10675. (interactive "P")
  10676. (let* (case-fold-search
  10677. (bfn (buffer-file-name (buffer-base-buffer)))
  10678. (path (and (derived-mode-p 'org-mode) (org-get-outline-path)))
  10679. res)
  10680. (when current (setq path (append path
  10681. (save-excursion
  10682. (org-back-to-heading t)
  10683. (when (looking-at org-complex-heading-regexp)
  10684. (list (match-string 4)))))))
  10685. (setq res
  10686. (org-format-outline-path
  10687. path
  10688. (1- (frame-width))
  10689. (and file bfn (concat (file-name-nondirectory bfn) separator))
  10690. separator))
  10691. (if just-return-string
  10692. (org-no-properties res)
  10693. (org-unlogged-message "%s" res))))
  10694. (defvar org-refile-history nil
  10695. "History for refiling operations.")
  10696. (defvar org-after-refile-insert-hook nil
  10697. "Hook run after `org-refile' has inserted its stuff at the new location.
  10698. Note that this is still *before* the stuff will be removed from
  10699. the *old* location.")
  10700. (defvar org-capture-last-stored-marker)
  10701. (defvar org-refile-keep nil
  10702. "Non-nil means `org-refile' will copy instead of refile.")
  10703. (defun org-copy ()
  10704. "Like `org-refile', but copy."
  10705. (interactive)
  10706. (let ((org-refile-keep t))
  10707. (funcall 'org-refile nil nil nil "Copy")))
  10708. (defun org-refile (&optional arg default-buffer rfloc msg)
  10709. "Move the entry or entries at point to another heading.
  10710. The list of target headings is compiled using the information in
  10711. `org-refile-targets', which see.
  10712. At the target location, the entry is filed as a subitem of the
  10713. target heading. Depending on `org-reverse-note-order', the new
  10714. subitem will either be the first or the last subitem.
  10715. If there is an active region, all entries in that region will be
  10716. refiled. However, the region must fulfill the requirement that
  10717. the first heading sets the top-level of the moved text.
  10718. With prefix arg ARG, the command will only visit the target
  10719. location and not actually move anything.
  10720. With a double prefix arg \\[universal-argument] \\[universal-argument], go to the location where the last
  10721. refiling operation has put the subtree.
  10722. With a numeric prefix argument of `2', refile to the running clock.
  10723. With a numeric prefix argument of `3', emulate `org-refile-keep'
  10724. being set to t and copy to the target location, don't move it.
  10725. Beware that keeping refiled entries may result in duplicated ID
  10726. properties.
  10727. RFLOC can be a refile location obtained in a different way.
  10728. MSG is a string to replace \"Refile\" in the default prompt with
  10729. another verb. E.g. `org-copy' sets this parameter to \"Copy\".
  10730. See also `org-refile-use-outline-path'.
  10731. If you are using target caching (see `org-refile-use-cache'), you
  10732. have to clear the target cache in order to find new targets.
  10733. This can be done with a 0 prefix (`C-0 C-c C-w') or a triple
  10734. prefix argument (`C-u C-u C-u C-c C-w')."
  10735. (interactive "P")
  10736. (if (member arg '(0 (64)))
  10737. (org-refile-cache-clear)
  10738. (let* ((actionmsg (cond (msg msg)
  10739. ((equal arg 3) "Refile (and keep)")
  10740. (t "Refile")))
  10741. (regionp (org-region-active-p))
  10742. (region-start (and regionp (region-beginning)))
  10743. (region-end (and regionp (region-end)))
  10744. (org-refile-keep (if (equal arg 3) t org-refile-keep))
  10745. pos it nbuf file level reversed)
  10746. (setq last-command nil)
  10747. (when regionp
  10748. (goto-char region-start)
  10749. (or (bolp) (goto-char (point-at-bol)))
  10750. (setq region-start (point))
  10751. (unless (or (org-kill-is-subtree-p
  10752. (buffer-substring region-start region-end))
  10753. (prog1 org-refile-active-region-within-subtree
  10754. (let ((s (point-at-eol)))
  10755. (org-toggle-heading)
  10756. (setq region-end (+ (- (point-at-eol) s) region-end)))))
  10757. (user-error "The region is not a (sequence of) subtree(s)")))
  10758. (if (equal arg '(16))
  10759. (org-refile-goto-last-stored)
  10760. (when (or
  10761. (and (equal arg 2)
  10762. org-clock-hd-marker (marker-buffer org-clock-hd-marker)
  10763. (prog1
  10764. (setq it (list (or org-clock-heading "running clock")
  10765. (buffer-file-name
  10766. (marker-buffer org-clock-hd-marker))
  10767. ""
  10768. (marker-position org-clock-hd-marker)))
  10769. (setq arg nil)))
  10770. (setq it
  10771. (or rfloc
  10772. (let (heading-text)
  10773. (save-excursion
  10774. (unless (and arg (listp arg))
  10775. (org-back-to-heading t)
  10776. (setq heading-text
  10777. (replace-regexp-in-string
  10778. org-bracket-link-regexp
  10779. "\\3"
  10780. (nth 4 (org-heading-components)))))
  10781. (org-refile-get-location
  10782. (cond ((and arg (listp arg)) "Goto")
  10783. (regionp (concat actionmsg " region to"))
  10784. (t (concat actionmsg " subtree \""
  10785. heading-text "\" to")))
  10786. default-buffer
  10787. (and (not (equal '(4) arg))
  10788. org-refile-allow-creating-parent-nodes)))))))
  10789. (setq file (nth 1 it)
  10790. pos (nth 3 it))
  10791. (when (and (not arg)
  10792. pos
  10793. (equal (buffer-file-name) file)
  10794. (if regionp
  10795. (and (>= pos region-start)
  10796. (<= pos region-end))
  10797. (and (>= pos (point))
  10798. (< pos (save-excursion
  10799. (org-end-of-subtree t t))))))
  10800. (error "Cannot refile to position inside the tree or region"))
  10801. (setq nbuf (or (find-buffer-visiting file)
  10802. (find-file-noselect file)))
  10803. (if (and arg (not (equal arg 3)))
  10804. (progn
  10805. (org-pop-to-buffer-same-window nbuf)
  10806. (goto-char pos)
  10807. (org-show-context 'org-goto))
  10808. (if regionp
  10809. (progn
  10810. (org-kill-new (buffer-substring region-start region-end))
  10811. (org-save-markers-in-region region-start region-end))
  10812. (org-copy-subtree 1 nil t))
  10813. (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
  10814. (find-file-noselect file)))
  10815. (setq reversed (org-notes-order-reversed-p))
  10816. (save-excursion
  10817. (save-restriction
  10818. (widen)
  10819. (if pos
  10820. (progn
  10821. (goto-char pos)
  10822. (looking-at org-outline-regexp)
  10823. (setq level (org-get-valid-level (funcall outline-level) 1))
  10824. (goto-char
  10825. (if reversed
  10826. (or (outline-next-heading) (point-max))
  10827. (or (save-excursion (org-get-next-sibling))
  10828. (org-end-of-subtree t t)
  10829. (point-max)))))
  10830. (setq level 1)
  10831. (if (not reversed)
  10832. (goto-char (point-max))
  10833. (goto-char (point-min))
  10834. (or (outline-next-heading) (goto-char (point-max)))))
  10835. (unless (bolp) (newline))
  10836. (org-paste-subtree level nil nil t)
  10837. (when org-log-refile
  10838. (org-add-log-setup 'refile nil nil org-log-refile)
  10839. (unless (eq org-log-refile 'note)
  10840. (save-excursion (org-add-log-note))))
  10841. (and org-auto-align-tags
  10842. (let ((org-loop-over-headlines-in-active-region nil))
  10843. (org-set-tags nil t)))
  10844. (let ((bookmark-name (plist-get org-bookmark-names-plist
  10845. :last-refile)))
  10846. (when bookmark-name
  10847. (with-demoted-errors
  10848. (bookmark-set bookmark-name))))
  10849. ;; If we are refiling for capture, make sure that the
  10850. ;; last-capture pointers point here
  10851. (when (org-bound-and-true-p org-capture-is-refiling)
  10852. (let ((bookmark-name (plist-get org-bookmark-names-plist
  10853. :last-capture-marker)))
  10854. (when bookmark-name
  10855. (with-demoted-errors
  10856. (bookmark-set bookmark-name))))
  10857. (move-marker org-capture-last-stored-marker (point)))
  10858. (when (fboundp 'deactivate-mark) (deactivate-mark))
  10859. (run-hooks 'org-after-refile-insert-hook))))
  10860. (unless org-refile-keep
  10861. (if regionp
  10862. (delete-region (point) (+ (point) (- region-end region-start)))
  10863. (delete-region
  10864. (and (org-back-to-heading t) (point))
  10865. (min (1+ (buffer-size)) (org-end-of-subtree t t) (point)))))
  10866. (when (featurep 'org-inlinetask)
  10867. (org-inlinetask-remove-END-maybe))
  10868. (setq org-markers-to-move nil)
  10869. (message (concat actionmsg " to \"%s\" in file %s: done") (car it) file)))))))
  10870. (defun org-refile-goto-last-stored ()
  10871. "Go to the location where the last refile was stored."
  10872. (interactive)
  10873. (bookmark-jump (plist-get org-bookmark-names-plist :last-refile))
  10874. (message "This is the location of the last refile"))
  10875. (defun org-refile--get-location (refloc tbl)
  10876. "When user refile to REFLOC, find the associated target in TBL.
  10877. Also check `org-refile-target-table'."
  10878. (car (delq
  10879. nil
  10880. (mapcar
  10881. (lambda (r) (or (assoc r tbl)
  10882. (assoc r org-refile-target-table)))
  10883. (list (replace-regexp-in-string "/$" "" refloc)
  10884. (replace-regexp-in-string "\\([^/]\\)$" "\\1/" refloc))))))
  10885. (defun org-refile-get-location (&optional prompt default-buffer new-nodes)
  10886. "Prompt the user for a refile location, using PROMPT.
  10887. PROMPT should not be suffixed with a colon and a space, because
  10888. this function appends the default value from
  10889. `org-refile-history' automatically, if that is not empty."
  10890. (let ((org-refile-targets org-refile-targets)
  10891. (org-refile-use-outline-path org-refile-use-outline-path))
  10892. (setq org-refile-target-table (org-refile-get-targets default-buffer)))
  10893. (unless org-refile-target-table
  10894. (user-error "No refile targets"))
  10895. (let* ((cbuf (current-buffer))
  10896. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  10897. (cfunc (if (and org-refile-use-outline-path
  10898. org-outline-path-complete-in-steps)
  10899. #'org-olpath-completing-read
  10900. #'completing-read))
  10901. (extra (if org-refile-use-outline-path "/" ""))
  10902. (cbnex (concat (buffer-name) extra))
  10903. (filename (and cfn (expand-file-name cfn)))
  10904. (tbl (mapcar
  10905. (lambda (x)
  10906. (if (and (not (member org-refile-use-outline-path
  10907. '(file full-file-path)))
  10908. (not (equal filename (nth 1 x))))
  10909. (cons (concat (car x) extra " ("
  10910. (file-name-nondirectory (nth 1 x)) ")")
  10911. (cdr x))
  10912. (cons (concat (car x) extra) (cdr x))))
  10913. org-refile-target-table))
  10914. (completion-ignore-case t)
  10915. cdef
  10916. (prompt (concat prompt
  10917. (or (and (car org-refile-history)
  10918. (concat " (default " (car org-refile-history) ")"))
  10919. (and (assoc cbnex tbl) (setq cdef cbnex)
  10920. (concat " (default " cbnex ")"))) ": "))
  10921. pa answ parent-target child parent old-hist)
  10922. (setq old-hist org-refile-history)
  10923. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  10924. nil 'org-refile-history (or cdef (car org-refile-history))))
  10925. (if (setq pa (org-refile--get-location answ tbl))
  10926. (progn
  10927. (org-refile-check-position pa)
  10928. (when (or (not org-refile-history)
  10929. (not (eq old-hist org-refile-history))
  10930. (not (equal (car pa) (car org-refile-history))))
  10931. (setq org-refile-history
  10932. (cons (car pa) (if (assoc (car org-refile-history) tbl)
  10933. org-refile-history
  10934. (cdr org-refile-history))))
  10935. (when (equal (car org-refile-history) (nth 1 org-refile-history))
  10936. (pop org-refile-history)))
  10937. pa)
  10938. (if (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  10939. (progn
  10940. (setq parent (match-string 1 answ)
  10941. child (match-string 2 answ))
  10942. (setq parent-target (org-refile--get-location parent tbl))
  10943. (when (and parent-target
  10944. (or (eq new-nodes t)
  10945. (and (eq new-nodes 'confirm)
  10946. (y-or-n-p (format "Create new node \"%s\"? "
  10947. child)))))
  10948. (org-refile-new-child parent-target child)))
  10949. (user-error "Invalid target location")))))
  10950. (declare-function org-string-nw-p "org-macs" (s))
  10951. (defun org-refile-check-position (refile-pointer)
  10952. "Check if the refile pointer matches the headline to which it points."
  10953. (let* ((file (nth 1 refile-pointer))
  10954. (re (nth 2 refile-pointer))
  10955. (pos (nth 3 refile-pointer))
  10956. buffer)
  10957. (if (and (not (markerp pos)) (not file))
  10958. (user-error "Please indicate a target file in the refile path")
  10959. (when (org-string-nw-p re)
  10960. (setq buffer (if (markerp pos)
  10961. (marker-buffer pos)
  10962. (or (find-buffer-visiting file)
  10963. (find-file-noselect file))))
  10964. (with-current-buffer buffer
  10965. (save-excursion
  10966. (save-restriction
  10967. (widen)
  10968. (goto-char pos)
  10969. (beginning-of-line 1)
  10970. (unless (org-looking-at-p re)
  10971. (user-error "Invalid refile position, please clear the cache with `C-0 C-c C-w' before refiling")))))))))
  10972. (defun org-refile-new-child (parent-target child)
  10973. "Use refile target PARENT-TARGET to add new CHILD below it."
  10974. (unless parent-target
  10975. (error "Cannot find parent for new node"))
  10976. (let ((file (nth 1 parent-target))
  10977. (pos (nth 3 parent-target))
  10978. level)
  10979. (with-current-buffer (or (find-buffer-visiting file)
  10980. (find-file-noselect file))
  10981. (save-excursion
  10982. (save-restriction
  10983. (widen)
  10984. (if pos
  10985. (goto-char pos)
  10986. (goto-char (point-max))
  10987. (unless (bolp) (newline)))
  10988. (when (looking-at org-outline-regexp)
  10989. (setq level (funcall outline-level))
  10990. (org-end-of-subtree t t))
  10991. (org-back-over-empty-lines)
  10992. (insert "\n" (make-string
  10993. (if pos (org-get-valid-level level 1) 1) ?*)
  10994. " " child "\n")
  10995. (beginning-of-line 0)
  10996. (list (concat (car parent-target) "/" child) file "" (point)))))))
  10997. (defun org-olpath-completing-read (prompt collection &rest args)
  10998. "Read an outline path like a file name."
  10999. (let ((thetable collection))
  11000. (apply #'completing-read
  11001. prompt
  11002. (lambda (string predicate &optional flag)
  11003. (cond
  11004. ((eq flag nil) (try-completion string thetable))
  11005. ((eq flag t)
  11006. (let ((l (length string)))
  11007. (mapcar (lambda (x)
  11008. (let ((r (substring x l))
  11009. (f (if (string-match " ([^)]*)$" x)
  11010. (match-string 0 x)
  11011. "")))
  11012. (if (string-match "/" r)
  11013. (concat string (substring r 0 (match-end 0)) f)
  11014. x)))
  11015. (all-completions string thetable predicate))))
  11016. ;; Exact match?
  11017. ((eq flag 'lambda) (assoc string thetable))))
  11018. args)))
  11019. ;;;; Dynamic blocks
  11020. (defun org-find-dblock (name)
  11021. "Find the first dynamic block with name NAME in the buffer.
  11022. If not found, stay at current position and return nil."
  11023. (let ((case-fold-search t) pos)
  11024. (save-excursion
  11025. (goto-char (point-min))
  11026. (setq pos (and (re-search-forward
  11027. (concat "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+" name "\\>") nil t)
  11028. (match-beginning 0))))
  11029. (when pos (goto-char pos))
  11030. pos))
  11031. (defun org-create-dblock (plist)
  11032. "Create a dynamic block section, with parameters taken from PLIST.
  11033. PLIST must contain a :name entry which is used as the name of the block."
  11034. (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
  11035. (end-of-line 1)
  11036. (newline))
  11037. (let ((col (current-column))
  11038. (name (plist-get plist :name)))
  11039. (insert "#+BEGIN: " name)
  11040. (while plist
  11041. (if (eq (car plist) :name)
  11042. (setq plist (cddr plist))
  11043. (insert " " (prin1-to-string (pop plist)))))
  11044. (insert "\n\n" (make-string col ?\ ) "#+END:\n")
  11045. (beginning-of-line -2)))
  11046. (defun org-prepare-dblock ()
  11047. "Prepare dynamic block for refresh.
  11048. This empties the block, puts the cursor at the insert position and returns
  11049. the property list including an extra property :name with the block name."
  11050. (unless (looking-at org-dblock-start-re)
  11051. (user-error "Not at a dynamic block"))
  11052. (let* ((begdel (1+ (match-end 0)))
  11053. (name (org-no-properties (match-string 1)))
  11054. (params (append (list :name name)
  11055. (read (concat "(" (match-string 3) ")")))))
  11056. (save-excursion
  11057. (beginning-of-line 1)
  11058. (skip-chars-forward " \t")
  11059. (setq params (plist-put params :indentation-column (current-column))))
  11060. (unless (re-search-forward org-dblock-end-re nil t)
  11061. (error "Dynamic block not terminated"))
  11062. (setq params
  11063. (append params
  11064. (list :content (buffer-substring
  11065. begdel (match-beginning 0)))))
  11066. (delete-region begdel (match-beginning 0))
  11067. (goto-char begdel)
  11068. (open-line 1)
  11069. params))
  11070. (defun org-map-dblocks (&optional command)
  11071. "Apply COMMAND to all dynamic blocks in the current buffer.
  11072. If COMMAND is not given, use `org-update-dblock'."
  11073. (let ((cmd (or command 'org-update-dblock)))
  11074. (save-excursion
  11075. (goto-char (point-min))
  11076. (while (re-search-forward org-dblock-start-re nil t)
  11077. (goto-char (match-beginning 0))
  11078. (save-excursion
  11079. (condition-case nil
  11080. (funcall cmd)
  11081. (error (message "Error during update of dynamic block"))))
  11082. (unless (re-search-forward org-dblock-end-re nil t)
  11083. (error "Dynamic block not terminated"))))))
  11084. (defun org-dblock-update (&optional arg)
  11085. "User command for updating dynamic blocks.
  11086. Update the dynamic block at point. With prefix ARG, update all dynamic
  11087. blocks in the buffer."
  11088. (interactive "P")
  11089. (if arg
  11090. (org-update-all-dblocks)
  11091. (or (looking-at org-dblock-start-re)
  11092. (org-beginning-of-dblock))
  11093. (org-update-dblock)))
  11094. (defun org-update-dblock ()
  11095. "Update the dynamic block at point.
  11096. This means to empty the block, parse for parameters and then call
  11097. the correct writing function."
  11098. (interactive)
  11099. (save-excursion
  11100. (let* ((win (selected-window))
  11101. (pos (point))
  11102. (line (org-current-line))
  11103. (params (org-prepare-dblock))
  11104. (name (plist-get params :name))
  11105. (indent (plist-get params :indentation-column))
  11106. (cmd (intern (concat "org-dblock-write:" name))))
  11107. (message "Updating dynamic block `%s' at line %d..." name line)
  11108. (funcall cmd params)
  11109. (message "Updating dynamic block `%s' at line %d...done" name line)
  11110. (goto-char pos)
  11111. (when (and indent (> indent 0))
  11112. (setq indent (make-string indent ?\ ))
  11113. (save-excursion
  11114. (select-window win)
  11115. (org-beginning-of-dblock)
  11116. (forward-line 1)
  11117. (while (not (looking-at org-dblock-end-re))
  11118. (insert indent)
  11119. (beginning-of-line 2))
  11120. (when (looking-at org-dblock-end-re)
  11121. (and (looking-at "[ \t]+")
  11122. (replace-match ""))
  11123. (insert indent)))))))
  11124. (defun org-beginning-of-dblock ()
  11125. "Find the beginning of the dynamic block at point.
  11126. Error if there is no such block at point."
  11127. (let ((pos (point))
  11128. beg)
  11129. (end-of-line 1)
  11130. (if (and (re-search-backward org-dblock-start-re nil t)
  11131. (setq beg (match-beginning 0))
  11132. (re-search-forward org-dblock-end-re nil t)
  11133. (> (match-end 0) pos))
  11134. (goto-char beg)
  11135. (goto-char pos)
  11136. (error "Not in a dynamic block"))))
  11137. (defun org-update-all-dblocks ()
  11138. "Update all dynamic blocks in the buffer.
  11139. This function can be used in a hook."
  11140. (interactive)
  11141. (when (derived-mode-p 'org-mode)
  11142. (org-map-dblocks 'org-update-dblock)))
  11143. ;;;; Completion
  11144. (declare-function org-export-backend-options "ox" (cl-x) t)
  11145. (defun org-get-export-keywords ()
  11146. "Return a list of all currently understood export keywords.
  11147. Export keywords include options, block names, attributes and
  11148. keywords relative to each registered export back-end."
  11149. (let (keywords)
  11150. (dolist (backend
  11151. (org-bound-and-true-p org-export-registered-backends)
  11152. (delq nil keywords))
  11153. ;; Back-end name (for keywords, like #+LATEX:)
  11154. (push (upcase (symbol-name (org-export-backend-name backend))) keywords)
  11155. (dolist (option-entry (org-export-backend-options backend))
  11156. ;; Back-end options.
  11157. (push (nth 1 option-entry) keywords)))))
  11158. (defconst org-options-keywords
  11159. '("ARCHIVE:" "AUTHOR:" "BIND:" "CATEGORY:" "COLUMNS:" "CREATOR:" "DATE:"
  11160. "DESCRIPTION:" "DRAWERS:" "EMAIL:" "EXCLUDE_TAGS:" "FILETAGS:" "INCLUDE:"
  11161. "INDEX:" "KEYWORDS:" "LANGUAGE:" "MACRO:" "OPTIONS:" "PROPERTY:"
  11162. "PRIORITIES:" "SELECT_TAGS:" "SEQ_TODO:" "SETUPFILE:" "STARTUP:" "TAGS:"
  11163. "TITLE:" "TODO:" "TYP_TODO:" "SELECT_TAGS:" "EXCLUDE_TAGS:"))
  11164. (defcustom org-structure-template-alist
  11165. '(("s" "#+BEGIN_SRC ?\n\n#+END_SRC")
  11166. ("e" "#+BEGIN_EXAMPLE\n?\n#+END_EXAMPLE")
  11167. ("q" "#+BEGIN_QUOTE\n?\n#+END_QUOTE")
  11168. ("v" "#+BEGIN_VERSE\n?\n#+END_VERSE")
  11169. ("V" "#+BEGIN_VERBATIM\n?\n#+END_VERBATIM")
  11170. ("c" "#+BEGIN_CENTER\n?\n#+END_CENTER")
  11171. ("l" "#+BEGIN_EXPORT latex\n?\n#+END_EXPORT")
  11172. ("L" "#+LaTeX: ")
  11173. ("h" "#+BEGIN_EXPORT html\n?\n#+END_EXPORT")
  11174. ("H" "#+HTML: ")
  11175. ("a" "#+BEGIN_EXPORT ascii\n?\n#+END_EXPORT")
  11176. ("A" "#+ASCII: ")
  11177. ("i" "#+INDEX: ?")
  11178. ("I" "#+INCLUDE: %file ?"))
  11179. "Structure completion elements.
  11180. This is a list of abbreviation keys and values. The value gets inserted
  11181. if you type `<' followed by the key and then press the completion key,
  11182. usually `TAB'. %file will be replaced by a file name after prompting
  11183. for the file using completion. The cursor will be placed at the position
  11184. of the `?' in the template.
  11185. There are two templates for each key, the first uses the original Org syntax,
  11186. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  11187. the default when the /org-mtags.el/ module has been loaded. See also the
  11188. variable `org-mtags-prefer-muse-templates'."
  11189. :group 'org-completion
  11190. :type '(repeat
  11191. (list
  11192. (string :tag "Key")
  11193. (string :tag "Template")))
  11194. :version "25.1"
  11195. :package-version '(Org . "8.3"))
  11196. (defun org-try-structure-completion ()
  11197. "Try to complete a structure template before point.
  11198. This looks for strings like \"<e\" on an otherwise empty line and
  11199. expands them."
  11200. (let ((l (buffer-substring (point-at-bol) (point)))
  11201. a)
  11202. (when (and (looking-at "[ \t]*$")
  11203. (string-match "^[ \t]*<\\([a-zA-Z]+\\)$" l)
  11204. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  11205. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  11206. (match-beginning 1)) a)
  11207. t)))
  11208. (defun org-complete-expand-structure-template (start cell)
  11209. "Expand a structure template."
  11210. (let ((rpl (nth 1 cell))
  11211. (ind ""))
  11212. (delete-region start (point))
  11213. (when (string-match "\\`[ \t]*#\\+" rpl)
  11214. (cond
  11215. ((bolp))
  11216. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  11217. (setq ind (buffer-substring (point-at-bol) (point))))
  11218. (t (newline))))
  11219. (setq start (point))
  11220. (when (string-match "%file" rpl)
  11221. (setq rpl (replace-match
  11222. (concat
  11223. "\""
  11224. (save-match-data
  11225. (abbreviate-file-name (read-file-name "Include file: ")))
  11226. "\"")
  11227. t t rpl)))
  11228. (setq rpl (mapconcat 'identity (split-string rpl "\n")
  11229. (concat "\n" ind)))
  11230. (insert rpl)
  11231. (when (re-search-backward "\\?" start t) (delete-char 1))))
  11232. ;;;; TODO, DEADLINE, Comments
  11233. (defun org-toggle-comment ()
  11234. "Change the COMMENT state of an entry."
  11235. (interactive)
  11236. (save-excursion
  11237. (org-back-to-heading)
  11238. (looking-at org-complex-heading-regexp)
  11239. (goto-char (or (match-end 3) (match-end 2) (match-end 1)))
  11240. (skip-chars-forward " \t")
  11241. (unless (memq (char-before) '(?\s ?\t)) (insert " "))
  11242. (if (org-in-commented-heading-p t)
  11243. (delete-region (point)
  11244. (progn (search-forward " " (line-end-position) 'move)
  11245. (skip-chars-forward " \t")
  11246. (point)))
  11247. (insert org-comment-string)
  11248. (unless (eolp) (insert " ")))))
  11249. (defvar org-last-todo-state-is-todo nil
  11250. "This is non-nil when the last TODO state change led to a TODO state.
  11251. If the last change removed the TODO tag or switched to DONE, then
  11252. this is nil.")
  11253. (defvar org-setting-tags nil) ; dynamically skipped
  11254. (defvar org-todo-setup-filter-hook nil
  11255. "Hook for functions that pre-filter todo specs.
  11256. Each function takes a todo spec and returns either nil or the spec
  11257. transformed into canonical form." )
  11258. (defvar org-todo-get-default-hook nil
  11259. "Hook for functions that get a default item for todo.
  11260. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  11261. nil or a string to be used for the todo mark." )
  11262. (defvar org-agenda-headline-snapshot-before-repeat)
  11263. (defun org-current-effective-time ()
  11264. "Return current time adjusted for `org-extend-today-until' variable."
  11265. (let* ((ct (org-current-time))
  11266. (dct (decode-time ct))
  11267. (ct1
  11268. (cond
  11269. (org-use-last-clock-out-time-as-effective-time
  11270. (or (org-clock-get-last-clock-out-time) ct))
  11271. ((and org-use-effective-time (< (nth 2 dct) org-extend-today-until))
  11272. (encode-time 0 59 23 (1- (nth 3 dct)) (nth 4 dct) (nth 5 dct)))
  11273. (t ct))))
  11274. ct1))
  11275. (defun org-todo-yesterday (&optional arg)
  11276. "Like `org-todo' but the time of change will be 23:59 of yesterday."
  11277. (interactive "P")
  11278. (if (eq major-mode 'org-agenda-mode)
  11279. (apply 'org-agenda-todo-yesterday arg)
  11280. (let* ((org-use-effective-time t)
  11281. (hour (nth 2 (decode-time (org-current-time))))
  11282. (org-extend-today-until (1+ hour)))
  11283. (org-todo arg))))
  11284. (defvar org-block-entry-blocking ""
  11285. "First entry preventing the TODO state change.")
  11286. (defun org-cancel-repeater ()
  11287. "Cancel a repeater by setting its numeric value to zero."
  11288. (interactive)
  11289. (save-excursion
  11290. (org-back-to-heading t)
  11291. (let ((bound1 (point))
  11292. (bound0 (save-excursion (outline-next-heading) (point))))
  11293. (when (and (re-search-forward
  11294. (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  11295. org-deadline-time-regexp "\\)\\|\\("
  11296. org-ts-regexp "\\)")
  11297. bound0 t)
  11298. (re-search-backward "[ \t]+\\(?:[.+]\\)?\\+\\([0-9]+\\)[hdwmy]"
  11299. bound1 t))
  11300. (replace-match "0" t nil nil 1)))))
  11301. (defvar org-state) ;; dynamically scoped into this function
  11302. (defun org-todo (&optional arg)
  11303. "Change the TODO state of an item.
  11304. The state of an item is given by a keyword at the start of the heading,
  11305. like
  11306. *** TODO Write paper
  11307. *** DONE Call mom
  11308. The different keywords are specified in the variable `org-todo-keywords'.
  11309. By default the available states are \"TODO\" and \"DONE\".
  11310. So for this example: when the item starts with TODO, it is changed to DONE.
  11311. When it starts with DONE, the DONE is removed. And when neither TODO nor
  11312. DONE are present, add TODO at the beginning of the heading.
  11313. With \\[universal-argument] prefix arg, use completion to determine the new \
  11314. state.
  11315. With numeric prefix arg, switch to that state.
  11316. With a double \\[universal-argument] prefix, switch to the next set of TODO \
  11317. keywords (nextset).
  11318. With a triple \\[universal-argument] prefix, circumvent any state blocking.
  11319. With a numeric prefix arg of 0, inhibit note taking for the change.
  11320. With a numeric prefix arg of -1, cancel repeater to allow marking as DONE.
  11321. When called through ELisp, arg is also interpreted in the following way:
  11322. `none' -> empty state
  11323. \"\"(empty string) -> switch to empty state
  11324. `done' -> switch to DONE
  11325. `nextset' -> switch to the next set of keywords
  11326. `previousset' -> switch to the previous set of keywords
  11327. \"WAITING\" -> switch to the specified keyword, but only if it
  11328. really is a member of `org-todo-keywords'."
  11329. (interactive "P")
  11330. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  11331. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11332. 'region-start-level 'region))
  11333. org-loop-over-headlines-in-active-region)
  11334. (org-map-entries
  11335. `(org-todo ,arg)
  11336. org-loop-over-headlines-in-active-region
  11337. cl (when (outline-invisible-p) (org-end-of-subtree nil t))))
  11338. (when (equal arg '(16)) (setq arg 'nextset))
  11339. (when (equal arg -1) (org-cancel-repeater) (setq arg nil))
  11340. (let ((org-blocker-hook org-blocker-hook)
  11341. commentp
  11342. case-fold-search)
  11343. (when (equal arg '(64))
  11344. (setq arg nil org-blocker-hook nil))
  11345. (when (and org-blocker-hook
  11346. (or org-inhibit-blocking
  11347. (org-entry-get nil "NOBLOCKING")))
  11348. (setq org-blocker-hook nil))
  11349. (save-excursion
  11350. (catch 'exit
  11351. (org-back-to-heading t)
  11352. (when (org-in-commented-heading-p t)
  11353. (org-toggle-comment)
  11354. (setq commentp t))
  11355. (when (looking-at org-outline-regexp) (goto-char (1- (match-end 0))))
  11356. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|[ \t]*$\\)"))
  11357. (looking-at "\\(?: *\\|[ \t]*$\\)"))
  11358. (let* ((match-data (match-data))
  11359. (startpos (point-at-bol))
  11360. (logging (save-match-data (org-entry-get nil "LOGGING" t t)))
  11361. (org-log-done org-log-done)
  11362. (org-log-repeat org-log-repeat)
  11363. (org-todo-log-states org-todo-log-states)
  11364. (org-inhibit-logging
  11365. (if (equal arg 0)
  11366. (progn (setq arg nil) 'note) org-inhibit-logging))
  11367. (this (match-string 1))
  11368. (hl-pos (match-beginning 0))
  11369. (head (org-get-todo-sequence-head this))
  11370. (ass (assoc head org-todo-kwd-alist))
  11371. (interpret (nth 1 ass))
  11372. (done-word (nth 3 ass))
  11373. (final-done-word (nth 4 ass))
  11374. (org-last-state (or this ""))
  11375. (completion-ignore-case t)
  11376. (member (member this org-todo-keywords-1))
  11377. (tail (cdr member))
  11378. (org-state (cond
  11379. ((and org-todo-key-trigger
  11380. (or (and (equal arg '(4))
  11381. (eq org-use-fast-todo-selection 'prefix))
  11382. (and (not arg) org-use-fast-todo-selection
  11383. (not (eq org-use-fast-todo-selection
  11384. 'prefix)))))
  11385. ;; Use fast selection
  11386. (org-fast-todo-selection))
  11387. ((and (equal arg '(4))
  11388. (or (not org-use-fast-todo-selection)
  11389. (not org-todo-key-trigger)))
  11390. ;; Read a state with completion
  11391. (completing-read
  11392. "State: " (mapcar #'list org-todo-keywords-1)
  11393. nil t))
  11394. ((eq arg 'right)
  11395. (if this
  11396. (if tail (car tail) nil)
  11397. (car org-todo-keywords-1)))
  11398. ((eq arg 'left)
  11399. (unless (equal member org-todo-keywords-1)
  11400. (if this
  11401. (nth (- (length org-todo-keywords-1)
  11402. (length tail) 2)
  11403. org-todo-keywords-1)
  11404. (org-last org-todo-keywords-1))))
  11405. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  11406. (setq arg nil))) ; hack to fall back to cycling
  11407. (arg
  11408. ;; user or caller requests a specific state
  11409. (cond
  11410. ((equal arg "") nil)
  11411. ((eq arg 'none) nil)
  11412. ((eq arg 'done) (or done-word (car org-done-keywords)))
  11413. ((eq arg 'nextset)
  11414. (or (car (cdr (member head org-todo-heads)))
  11415. (car org-todo-heads)))
  11416. ((eq arg 'previousset)
  11417. (let ((org-todo-heads (reverse org-todo-heads)))
  11418. (or (car (cdr (member head org-todo-heads)))
  11419. (car org-todo-heads))))
  11420. ((car (member arg org-todo-keywords-1)))
  11421. ((stringp arg)
  11422. (user-error "State `%s' not valid in this file" arg))
  11423. ((nth (1- (prefix-numeric-value arg))
  11424. org-todo-keywords-1))))
  11425. ((null member) (or head (car org-todo-keywords-1)))
  11426. ((equal this final-done-word) nil) ;; -> make empty
  11427. ((null tail) nil) ;; -> first entry
  11428. ((memq interpret '(type priority))
  11429. (if (eq this-command last-command)
  11430. (car tail)
  11431. (if (> (length tail) 0)
  11432. (or done-word (car org-done-keywords))
  11433. nil)))
  11434. (t
  11435. (car tail))))
  11436. (org-state (or
  11437. (run-hook-with-args-until-success
  11438. 'org-todo-get-default-hook org-state org-last-state)
  11439. org-state))
  11440. (next (if org-state (concat " " org-state " ") " "))
  11441. (change-plist (list :type 'todo-state-change :from this :to org-state
  11442. :position startpos))
  11443. dolog now-done-p)
  11444. (when org-blocker-hook
  11445. (setq org-last-todo-state-is-todo
  11446. (not (member this org-done-keywords)))
  11447. (unless (save-excursion
  11448. (save-match-data
  11449. (org-with-wide-buffer
  11450. (run-hook-with-args-until-failure
  11451. 'org-blocker-hook change-plist))))
  11452. (if (org-called-interactively-p 'interactive)
  11453. (user-error "TODO state change from %s to %s blocked (by \"%s\")"
  11454. this org-state org-block-entry-blocking)
  11455. ;; fail silently
  11456. (message "TODO state change from %s to %s blocked (by \"%s\")"
  11457. this org-state org-block-entry-blocking)
  11458. (throw 'exit nil))))
  11459. (store-match-data match-data)
  11460. (replace-match next t t)
  11461. (cond ((equal this org-state)
  11462. (message "TODO state was already %s" (org-trim next)))
  11463. ((pos-visible-in-window-p hl-pos)
  11464. (message "TODO state changed to %s" (org-trim next))))
  11465. (unless head
  11466. (setq head (org-get-todo-sequence-head org-state)
  11467. ass (assoc head org-todo-kwd-alist)
  11468. interpret (nth 1 ass)
  11469. done-word (nth 3 ass)
  11470. final-done-word (nth 4 ass)))
  11471. (when (memq arg '(nextset previousset))
  11472. (message "Keyword-Set %d/%d: %s"
  11473. (- (length org-todo-sets) -1
  11474. (length (memq (assoc org-state org-todo-sets) org-todo-sets)))
  11475. (length org-todo-sets)
  11476. (mapconcat 'identity (assoc org-state org-todo-sets) " ")))
  11477. (setq org-last-todo-state-is-todo
  11478. (not (member org-state org-done-keywords)))
  11479. (setq now-done-p (and (member org-state org-done-keywords)
  11480. (not (member this org-done-keywords))))
  11481. (and logging (org-local-logging logging))
  11482. (when (and (or org-todo-log-states org-log-done)
  11483. (not (eq org-inhibit-logging t))
  11484. (not (memq arg '(nextset previousset))))
  11485. ;; we need to look at recording a time and note
  11486. (setq dolog (or (nth 1 (assoc org-state org-todo-log-states))
  11487. (nth 2 (assoc this org-todo-log-states))))
  11488. (when (and (eq dolog 'note) (eq org-inhibit-logging 'note))
  11489. (setq dolog 'time))
  11490. (when (or (and (not org-state) (not org-closed-keep-when-no-todo))
  11491. (and org-state
  11492. (member org-state org-not-done-keywords)
  11493. (not (member this org-not-done-keywords))))
  11494. ;; This is now a todo state and was not one before
  11495. ;; If there was a CLOSED time stamp, get rid of it.
  11496. (org-add-planning-info nil nil 'closed))
  11497. (when (and now-done-p org-log-done)
  11498. ;; It is now done, and it was not done before
  11499. (org-add-planning-info 'closed (org-current-effective-time))
  11500. (when (and (not dolog) (eq 'note org-log-done))
  11501. (org-add-log-setup 'done org-state this 'note)))
  11502. (when (and org-state dolog)
  11503. ;; This is a non-nil state, and we need to log it
  11504. (org-add-log-setup 'state org-state this dolog)))
  11505. ;; Fixup tag positioning
  11506. (org-todo-trigger-tag-changes org-state)
  11507. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  11508. (when org-provide-todo-statistics
  11509. (org-update-parent-todo-statistics))
  11510. (run-hooks 'org-after-todo-state-change-hook)
  11511. (when (and arg (not (member org-state org-done-keywords)))
  11512. (setq head (org-get-todo-sequence-head org-state)))
  11513. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  11514. ;; Do we need to trigger a repeat?
  11515. (when now-done-p
  11516. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  11517. ;; This is for the agenda, take a snapshot of the headline.
  11518. (save-match-data
  11519. (setq org-agenda-headline-snapshot-before-repeat
  11520. (org-get-heading))))
  11521. (org-auto-repeat-maybe org-state))
  11522. ;; Fixup cursor location if close to the keyword
  11523. (when (and (outline-on-heading-p)
  11524. (not (bolp))
  11525. (save-excursion (beginning-of-line 1)
  11526. (looking-at org-todo-line-regexp))
  11527. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  11528. (goto-char (or (match-end 2) (match-end 1)))
  11529. (and (looking-at " ") (just-one-space)))
  11530. (when org-trigger-hook
  11531. (save-excursion
  11532. (run-hook-with-args 'org-trigger-hook change-plist)))
  11533. (when commentp (org-toggle-comment))))))))
  11534. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  11535. "Block turning an entry into a TODO, using the hierarchy.
  11536. This checks whether the current task should be blocked from state
  11537. changes. Such blocking occurs when:
  11538. 1. The task has children which are not all in a completed state.
  11539. 2. A task has a parent with the property :ORDERED:, and there
  11540. are siblings prior to the current task with incomplete
  11541. status.
  11542. 3. The parent of the task is blocked because it has siblings that should
  11543. be done first, or is child of a block grandparent TODO entry."
  11544. (if (not org-enforce-todo-dependencies)
  11545. t ; if locally turned off don't block
  11546. (catch 'dont-block
  11547. ;; If this is not a todo state change, or if this entry is already DONE,
  11548. ;; do not block
  11549. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  11550. (member (plist-get change-plist :from)
  11551. (cons 'done org-done-keywords))
  11552. (member (plist-get change-plist :to)
  11553. (cons 'todo org-not-done-keywords))
  11554. (not (plist-get change-plist :to)))
  11555. (throw 'dont-block t))
  11556. ;; If this task has children, and any are undone, it's blocked
  11557. (save-excursion
  11558. (org-back-to-heading t)
  11559. (let ((this-level (funcall outline-level)))
  11560. (outline-next-heading)
  11561. (let ((child-level (funcall outline-level)))
  11562. (while (and (not (eobp))
  11563. (> child-level this-level))
  11564. ;; this todo has children, check whether they are all
  11565. ;; completed
  11566. (when (and (not (org-entry-is-done-p))
  11567. (org-entry-is-todo-p))
  11568. (setq org-block-entry-blocking (org-get-heading))
  11569. (throw 'dont-block nil))
  11570. (outline-next-heading)
  11571. (setq child-level (funcall outline-level))))))
  11572. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  11573. ;; any previous siblings are undone, it's blocked
  11574. (save-excursion
  11575. (org-back-to-heading t)
  11576. (let* ((pos (point))
  11577. (parent-pos (and (org-up-heading-safe) (point))))
  11578. (unless parent-pos (throw 'dont-block t)) ; no parent
  11579. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  11580. (forward-line 1)
  11581. (re-search-forward org-not-done-heading-regexp pos t))
  11582. (setq org-block-entry-blocking (match-string 0))
  11583. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  11584. ;; Search further up the hierarchy, to see if an ancestor is blocked
  11585. (while t
  11586. (goto-char parent-pos)
  11587. (unless (looking-at org-not-done-heading-regexp)
  11588. (throw 'dont-block t)) ; do not block, parent is not a TODO
  11589. (setq pos (point))
  11590. (setq parent-pos (and (org-up-heading-safe) (point)))
  11591. (unless parent-pos (throw 'dont-block t)) ; no parent
  11592. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  11593. (forward-line 1)
  11594. (re-search-forward org-not-done-heading-regexp pos t)
  11595. (setq org-block-entry-blocking (org-get-heading)))
  11596. (throw 'dont-block nil)))))))) ; block, older sibling not done.
  11597. (defcustom org-track-ordered-property-with-tag nil
  11598. "Should the ORDERED property also be shown as a tag?
  11599. The ORDERED property decides if an entry should require subtasks to be
  11600. completed in sequence. Since a property is not very visible, setting
  11601. this option means that toggling the ORDERED property with the command
  11602. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  11603. not relevant for the behavior, but it makes things more visible.
  11604. Note that toggling the tag with tags commands will not change the property
  11605. and therefore not influence behavior!
  11606. This can be t, meaning the tag ORDERED should be used, It can also be a
  11607. string to select a different tag for this task."
  11608. :group 'org-todo
  11609. :type '(choice
  11610. (const :tag "No tracking" nil)
  11611. (const :tag "Track with ORDERED tag" t)
  11612. (string :tag "Use other tag")))
  11613. (defun org-toggle-ordered-property ()
  11614. "Toggle the ORDERED property of the current entry.
  11615. For better visibility, you can track the value of this property with a tag.
  11616. See variable `org-track-ordered-property-with-tag'."
  11617. (interactive)
  11618. (let* ((t1 org-track-ordered-property-with-tag)
  11619. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  11620. (save-excursion
  11621. (org-back-to-heading)
  11622. (if (org-entry-get nil "ORDERED")
  11623. (progn
  11624. (org-delete-property "ORDERED")
  11625. (and tag (org-toggle-tag tag 'off))
  11626. (message "Subtasks can be completed in arbitrary order"))
  11627. (org-entry-put nil "ORDERED" "t")
  11628. (and tag (org-toggle-tag tag 'on))
  11629. (message "Subtasks must be completed in sequence")))))
  11630. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  11631. (defun org-block-todo-from-checkboxes (change-plist)
  11632. "Block turning an entry into a TODO, using checkboxes.
  11633. This checks whether the current task should be blocked from state
  11634. changes because there are unchecked boxes in this entry."
  11635. (if (not org-enforce-todo-checkbox-dependencies)
  11636. t ; if locally turned off don't block
  11637. (catch 'dont-block
  11638. ;; If this is not a todo state change, or if this entry is already DONE,
  11639. ;; do not block
  11640. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  11641. (member (plist-get change-plist :from)
  11642. (cons 'done org-done-keywords))
  11643. (member (plist-get change-plist :to)
  11644. (cons 'todo org-not-done-keywords))
  11645. (not (plist-get change-plist :to)))
  11646. (throw 'dont-block t))
  11647. ;; If this task has checkboxes that are not checked, it's blocked
  11648. (save-excursion
  11649. (org-back-to-heading t)
  11650. (let ((beg (point)) end)
  11651. (outline-next-heading)
  11652. (setq end (point))
  11653. (goto-char beg)
  11654. (when (org-list-search-forward
  11655. (concat (org-item-beginning-re)
  11656. "\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?"
  11657. "\\[[- ]\\]")
  11658. end t)
  11659. (when (boundp 'org-blocked-by-checkboxes)
  11660. (setq org-blocked-by-checkboxes t))
  11661. (throw 'dont-block nil))))
  11662. t))) ; do not block
  11663. (defun org-entry-blocked-p ()
  11664. "Non-nil if entry at point is blocked."
  11665. (and (not (org-entry-get nil "NOBLOCKING"))
  11666. (member (org-entry-get nil "TODO") org-not-done-keywords)
  11667. (not (run-hook-with-args-until-failure
  11668. 'org-blocker-hook
  11669. (list :type 'todo-state-change
  11670. :position (point)
  11671. :from 'todo
  11672. :to 'done)))))
  11673. (defun org-update-statistics-cookies (all)
  11674. "Update the statistics cookie, either from TODO or from checkboxes.
  11675. This should be called with the cursor in a line with a statistics cookie."
  11676. (interactive "P")
  11677. (if all
  11678. (progn
  11679. (org-update-checkbox-count 'all)
  11680. (org-map-entries 'org-update-parent-todo-statistics))
  11681. (if (not (org-at-heading-p))
  11682. (org-update-checkbox-count)
  11683. (let ((pos (point-marker))
  11684. end l1 l2)
  11685. (ignore-errors (org-back-to-heading t))
  11686. (if (not (org-at-heading-p))
  11687. (org-update-checkbox-count)
  11688. (setq l1 (org-outline-level))
  11689. (setq end (save-excursion
  11690. (outline-next-heading)
  11691. (when (org-at-heading-p) (setq l2 (org-outline-level)))
  11692. (point)))
  11693. (if (and (save-excursion
  11694. (re-search-forward
  11695. "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) \\[[- X]\\]" end t))
  11696. (not (save-excursion (re-search-forward
  11697. ":COOKIE_DATA:.*\\<todo\\>" end t))))
  11698. (org-update-checkbox-count)
  11699. (if (and l2 (> l2 l1))
  11700. (progn
  11701. (goto-char end)
  11702. (org-update-parent-todo-statistics))
  11703. (goto-char pos)
  11704. (beginning-of-line 1)
  11705. (while (re-search-forward
  11706. "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)"
  11707. (point-at-eol) t)
  11708. (replace-match (if (match-end 2) "[100%]" "[0/0]") t t)))))
  11709. (goto-char pos)
  11710. (move-marker pos nil)))))
  11711. (defvar org-entry-property-inherited-from) ;; defined below
  11712. (defun org-update-parent-todo-statistics ()
  11713. "Update any statistics cookie in the parent of the current headline.
  11714. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  11715. the entire subtree and this will travel up the hierarchy and update
  11716. statistics everywhere."
  11717. (let* ((prop (save-excursion (org-up-heading-safe)
  11718. (org-entry-get nil "COOKIE_DATA" 'inherit)))
  11719. (recursive (or (not org-hierarchical-todo-statistics)
  11720. (and prop (string-match "\\<recursive\\>" prop))))
  11721. (lim (or (and prop (marker-position org-entry-property-inherited-from))
  11722. 0))
  11723. (first t)
  11724. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  11725. level ltoggle l1 new ndel
  11726. (cnt-all 0) (cnt-done 0) is-percent kwd
  11727. checkbox-beg ov ovs ove cookie-present)
  11728. (catch 'exit
  11729. (save-excursion
  11730. (beginning-of-line 1)
  11731. (setq ltoggle (funcall outline-level))
  11732. ;; Three situations are to consider:
  11733. ;; 1. if `org-hierarchical-todo-statistics' is nil, repeat up
  11734. ;; to the top-level ancestor on the headline;
  11735. ;; 2. If parent has "recursive" property, repeat up to the
  11736. ;; headline setting that property, taking inheritance into
  11737. ;; account;
  11738. ;; 3. Else, move up to direct parent and proceed only once.
  11739. (while (and (setq level (org-up-heading-safe))
  11740. (or recursive first)
  11741. (>= (point) lim))
  11742. (setq first nil cookie-present nil)
  11743. (unless (and level
  11744. (not (string-match
  11745. "\\<checkbox\\>"
  11746. (downcase (or (org-entry-get nil "COOKIE_DATA")
  11747. "")))))
  11748. (throw 'exit nil))
  11749. (while (re-search-forward box-re (point-at-eol) t)
  11750. (setq cnt-all 0 cnt-done 0 cookie-present t)
  11751. (setq is-percent (match-end 2) checkbox-beg (match-beginning 0))
  11752. (save-match-data
  11753. (unless (outline-next-heading) (throw 'exit nil))
  11754. (while (and (looking-at org-complex-heading-regexp)
  11755. (> (setq l1 (length (match-string 1))) level))
  11756. (setq kwd (and (or recursive (= l1 ltoggle))
  11757. (match-string 2)))
  11758. (if (or (eq org-provide-todo-statistics 'all-headlines)
  11759. (and (eq org-provide-todo-statistics t)
  11760. (or (member kwd org-done-keywords)))
  11761. (and (listp org-provide-todo-statistics)
  11762. (stringp (car org-provide-todo-statistics))
  11763. (or (member kwd org-provide-todo-statistics)
  11764. (member kwd org-done-keywords)))
  11765. (and (listp org-provide-todo-statistics)
  11766. (listp (car org-provide-todo-statistics))
  11767. (or (member kwd (car org-provide-todo-statistics))
  11768. (and (member kwd org-done-keywords)
  11769. (member kwd (cadr org-provide-todo-statistics))))))
  11770. (setq cnt-all (1+ cnt-all))
  11771. (and (eq org-provide-todo-statistics t)
  11772. kwd
  11773. (setq cnt-all (1+ cnt-all))))
  11774. (when (or (and (member org-provide-todo-statistics '(t all-headlines))
  11775. (member kwd org-done-keywords))
  11776. (and (listp org-provide-todo-statistics)
  11777. (listp (car org-provide-todo-statistics))
  11778. (member kwd org-done-keywords)
  11779. (member kwd (cadr org-provide-todo-statistics)))
  11780. (and (listp org-provide-todo-statistics)
  11781. (stringp (car org-provide-todo-statistics))
  11782. (member kwd org-done-keywords)))
  11783. (setq cnt-done (1+ cnt-done)))
  11784. (outline-next-heading)))
  11785. (setq new
  11786. (if is-percent
  11787. (format "[%d%%]" (floor (* 100.0 cnt-done)
  11788. (max 1 cnt-all)))
  11789. (format "[%d/%d]" cnt-done cnt-all))
  11790. ndel (- (match-end 0) checkbox-beg))
  11791. ;; handle overlays when updating cookie from column view
  11792. (when (setq ov (car (overlays-at checkbox-beg)))
  11793. (setq ovs (overlay-start ov) ove (overlay-end ov))
  11794. (delete-overlay ov))
  11795. (goto-char checkbox-beg)
  11796. (insert new)
  11797. (delete-region (point) (+ (point) ndel))
  11798. (when org-auto-align-tags (org-fix-tags-on-the-fly))
  11799. (when ov (move-overlay ov ovs ove)))
  11800. (when cookie-present
  11801. (run-hook-with-args 'org-after-todo-statistics-hook
  11802. cnt-done (- cnt-all cnt-done))))))
  11803. (run-hooks 'org-todo-statistics-hook)))
  11804. (defvar org-after-todo-statistics-hook nil
  11805. "Hook that is called after a TODO statistics cookie has been updated.
  11806. Each function is called with two arguments: the number of not-done entries
  11807. and the number of done entries.
  11808. For example, the following function, when added to this hook, will switch
  11809. an entry to DONE when all children are done, and back to TODO when new
  11810. entries are set to a TODO status. Note that this hook is only called
  11811. when there is a statistics cookie in the headline!
  11812. \(defun org-summary-todo (n-done n-not-done)
  11813. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  11814. \(let (org-log-done org-log-states) ; turn off logging
  11815. \(org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  11816. ")
  11817. (defvar org-todo-statistics-hook nil
  11818. "Hook that is run whenever Org thinks TODO statistics should be updated.
  11819. This hook runs even if there is no statistics cookie present, in which case
  11820. `org-after-todo-statistics-hook' would not run.")
  11821. (defun org-todo-trigger-tag-changes (state)
  11822. "Apply the changes defined in `org-todo-state-tags-triggers'."
  11823. (let ((l org-todo-state-tags-triggers)
  11824. changes)
  11825. (when (or (not state) (equal state ""))
  11826. (setq changes (append changes (cdr (assoc "" l)))))
  11827. (when (and (stringp state) (> (length state) 0))
  11828. (setq changes (append changes (cdr (assoc state l)))))
  11829. (when (member state org-not-done-keywords)
  11830. (setq changes (append changes (cdr (assoc 'todo l)))))
  11831. (when (member state org-done-keywords)
  11832. (setq changes (append changes (cdr (assoc 'done l)))))
  11833. (dolist (c changes)
  11834. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  11835. (defun org-local-logging (value)
  11836. "Get logging settings from a property VALUE."
  11837. ;; Directly set the variables, they are already local.
  11838. (setq org-log-done nil
  11839. org-log-repeat nil
  11840. org-todo-log-states nil)
  11841. (dolist (w (org-split-string value))
  11842. (let (a)
  11843. (cond
  11844. ((setq a (assoc w org-startup-options))
  11845. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  11846. (set (nth 1 a) (nth 2 a))))
  11847. ((setq a (org-extract-log-state-settings w))
  11848. (and (member (car a) org-todo-keywords-1)
  11849. (push a org-todo-log-states)))))))
  11850. (defun org-get-todo-sequence-head (kwd)
  11851. "Return the head of the TODO sequence to which KWD belongs.
  11852. If KWD is not set, check if there is a text property remembering the
  11853. right sequence."
  11854. (let (p)
  11855. (cond
  11856. ((not kwd)
  11857. (or (get-text-property (point-at-bol) 'org-todo-head)
  11858. (progn
  11859. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  11860. nil (point-at-eol)))
  11861. (get-text-property p 'org-todo-head))))
  11862. ((not (member kwd org-todo-keywords-1))
  11863. (car org-todo-keywords-1))
  11864. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  11865. (defun org-fast-todo-selection ()
  11866. "Fast TODO keyword selection with single keys.
  11867. Returns the new TODO keyword, or nil if no state change should occur."
  11868. (let* ((fulltable org-todo-key-alist)
  11869. (done-keywords org-done-keywords) ;; needed for the faces.
  11870. (maxlen (apply 'max (mapcar
  11871. (lambda (x)
  11872. (if (stringp (car x)) (string-width (car x)) 0))
  11873. fulltable)))
  11874. (expert nil)
  11875. (fwidth (+ maxlen 3 1 3))
  11876. (ncol (/ (- (window-width) 4) fwidth))
  11877. tg cnt e c tbl
  11878. groups ingroup)
  11879. (save-excursion
  11880. (save-window-excursion
  11881. (if expert
  11882. (set-buffer (get-buffer-create " *Org todo*"))
  11883. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  11884. (erase-buffer)
  11885. (setq-local org-done-keywords done-keywords)
  11886. (setq tbl fulltable cnt 0)
  11887. (while (setq e (pop tbl))
  11888. (cond
  11889. ((equal e '(:startgroup))
  11890. (push '() groups) (setq ingroup t)
  11891. (unless (= cnt 0)
  11892. (setq cnt 0)
  11893. (insert "\n"))
  11894. (insert "{ "))
  11895. ((equal e '(:endgroup))
  11896. (setq ingroup nil cnt 0)
  11897. (insert "}\n"))
  11898. ((equal e '(:newline))
  11899. (unless (= cnt 0)
  11900. (setq cnt 0)
  11901. (insert "\n")
  11902. (setq e (car tbl))
  11903. (while (equal (car tbl) '(:newline))
  11904. (insert "\n")
  11905. (setq tbl (cdr tbl)))))
  11906. (t
  11907. (setq tg (car e) c (cdr e))
  11908. (when ingroup (push tg (car groups)))
  11909. (setq tg (org-add-props tg nil 'face
  11910. (org-get-todo-face tg)))
  11911. (when (and (= cnt 0) (not ingroup)) (insert " "))
  11912. (insert "[" c "] " tg (make-string
  11913. (- fwidth 4 (length tg)) ?\ ))
  11914. (when (= (setq cnt (1+ cnt)) ncol)
  11915. (insert "\n")
  11916. (when ingroup (insert " "))
  11917. (setq cnt 0)))))
  11918. (insert "\n")
  11919. (goto-char (point-min))
  11920. (unless expert (org-fit-window-to-buffer))
  11921. (message "[a-z..]:Set [SPC]:clear")
  11922. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  11923. (cond
  11924. ((or (= c ?\C-g)
  11925. (and (= c ?q) (not (rassoc c fulltable))))
  11926. (setq quit-flag t))
  11927. ((= c ?\ ) nil)
  11928. ((setq e (rassoc c fulltable) tg (car e))
  11929. tg)
  11930. (t (setq quit-flag t)))))))
  11931. (defun org-entry-is-todo-p ()
  11932. (member (org-get-todo-state) org-not-done-keywords))
  11933. (defun org-entry-is-done-p ()
  11934. (member (org-get-todo-state) org-done-keywords))
  11935. (defun org-get-todo-state ()
  11936. "Return the TODO keyword of the current subtree."
  11937. (save-excursion
  11938. (org-back-to-heading t)
  11939. (and (looking-at org-todo-line-regexp)
  11940. (match-end 2)
  11941. (match-string 2))))
  11942. (defun org-at-date-range-p (&optional inactive-ok)
  11943. "Non-nil if point is inside a date range.
  11944. When optional argument INACTIVE-OK is non-nil, also consider
  11945. inactive time ranges.
  11946. When this function returns a non-nil value, match data is set
  11947. according to `org-tr-regexp-both' or `org-tr-regexp', depending
  11948. on INACTIVE-OK."
  11949. (interactive)
  11950. (save-excursion
  11951. (catch 'exit
  11952. (let ((pos (point)))
  11953. (skip-chars-backward "^[<\r\n")
  11954. (skip-chars-backward "<[")
  11955. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  11956. (>= (match-end 0) pos)
  11957. (throw 'exit t))
  11958. (skip-chars-backward "^<[\r\n")
  11959. (skip-chars-backward "<[")
  11960. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  11961. (>= (match-end 0) pos)
  11962. (throw 'exit t)))
  11963. nil)))
  11964. (defun org-get-repeat (&optional tagline)
  11965. "Check if there is a deadline/schedule with repeater in this entry."
  11966. (save-match-data
  11967. (save-excursion
  11968. (org-back-to-heading t)
  11969. (and (re-search-forward (if tagline
  11970. (concat tagline "\\s-*" org-repeat-re)
  11971. org-repeat-re)
  11972. (org-entry-end-position) t)
  11973. (match-string-no-properties 1)))))
  11974. (defvar org-last-changed-timestamp)
  11975. (defvar org-last-inserted-timestamp)
  11976. (defvar org-log-post-message)
  11977. (defvar org-log-note-purpose)
  11978. (defvar org-log-note-how nil)
  11979. (defvar org-log-note-extra)
  11980. (defun org-auto-repeat-maybe (done-word)
  11981. "Check if the current headline contains a repeated time-stamp.
  11982. If yes, set TODO state back to what it was and change the base date
  11983. of repeating deadline/scheduled time stamps to new date.
  11984. This function is run automatically after each state change to a DONE state."
  11985. (let* ((repeat (org-get-repeat))
  11986. (aa (assoc org-last-state org-todo-kwd-alist))
  11987. (interpret (nth 1 aa))
  11988. (head (nth 2 aa))
  11989. (whata '(("h" . hour) ("d" . day) ("m" . month) ("y" . year)))
  11990. (msg "Entry repeats: ")
  11991. (org-log-done nil)
  11992. (org-todo-log-states nil))
  11993. (when (and repeat (not (zerop (string-to-number (substring repeat 1)))))
  11994. (when (eq org-log-repeat t) (setq org-log-repeat 'state))
  11995. (let ((to-state (or (org-entry-get nil "REPEAT_TO_STATE" 'selective)
  11996. org-todo-repeat-to-state)))
  11997. (unless (and to-state (member to-state org-todo-keywords-1))
  11998. (setq to-state (if (eq interpret 'type) org-last-state head)))
  11999. (org-todo to-state))
  12000. (when (or org-log-repeat (org-entry-get nil "CLOCK"))
  12001. (org-entry-put nil "LAST_REPEAT" (format-time-string
  12002. (org-time-stamp-format t t))))
  12003. (when org-log-repeat
  12004. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  12005. (memq 'org-add-log-note post-command-hook))
  12006. ;; We are already setup for some record.
  12007. (when (eq org-log-repeat 'note)
  12008. ;; Make sure we take a note, not only a time stamp.
  12009. (setq org-log-note-how 'note))
  12010. ;; Set up for taking a record.
  12011. (org-add-log-setup 'state
  12012. (or done-word (car org-done-keywords))
  12013. org-last-state
  12014. org-log-repeat)))
  12015. (org-back-to-heading t)
  12016. (org-add-planning-info nil nil 'closed)
  12017. (let ((end (save-excursion (outline-next-heading) (point))))
  12018. (while (re-search-forward org-ts-regexp end t)
  12019. (when (save-match-data
  12020. (or (org-at-planning-p)
  12021. (org-at-property-p)
  12022. (eq (org-element-type (save-excursion
  12023. (backward-char)
  12024. (org-element-context)))
  12025. 'timestamp)))
  12026. (let ((type (cond ((match-end 1) org-scheduled-string)
  12027. ((match-end 3) org-deadline-string)
  12028. (t "Plain:")))
  12029. (ts (or (match-string 2) (match-string 4) (match-string 0))))
  12030. (cond
  12031. ((not
  12032. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts))
  12033. ;; Time-stamps without a repeater are usually skipped.
  12034. ;; However, a SCHEDULED time-stamp without one is
  12035. ;; removed, as it is considered as no longer relevant.
  12036. (when (equal type org-scheduled-string)
  12037. (org-remove-timestamp-with-keyword type)))
  12038. (t
  12039. (let ((n (string-to-number (match-string 2 ts)))
  12040. (what (match-string 3 ts)))
  12041. (when (equal what "w") (setq n (* n 7) what "d"))
  12042. (when (and (equal what "h")
  12043. (not (string-match-p "[0-9]\\{1,2\\}:[0-9]\\{2\\}"
  12044. ts)))
  12045. (user-error
  12046. "Cannot repeat in Repeat in %d hour(s) because no hour \
  12047. has been set"
  12048. n))
  12049. ;; Preparation, see if we need to modify the start
  12050. ;; date for the change.
  12051. (when (match-end 1)
  12052. (let ((time (save-match-data (org-time-string-to-time ts))))
  12053. (cond
  12054. ((equal (match-string 1 ts) ".")
  12055. ;; Shift starting date to today
  12056. (org-timestamp-change
  12057. (- (org-today) (time-to-days time))
  12058. 'day))
  12059. ((equal (match-string 1 ts) "+")
  12060. (let ((nshiftmax 10)
  12061. (nshift 0))
  12062. (while (or (= nshift 0)
  12063. (<= (time-to-days time)
  12064. (time-to-days (current-time))))
  12065. (when (= (cl-incf nshift) nshiftmax)
  12066. (or (y-or-n-p
  12067. (format "%d repeater intervals were not \
  12068. enough to shift date past today. Continue? "
  12069. nshift))
  12070. (user-error "Abort")))
  12071. (org-timestamp-change n (cdr (assoc what whata)))
  12072. (org-at-timestamp-p t)
  12073. (setq ts (match-string 1))
  12074. (setq time
  12075. (save-match-data
  12076. (org-time-string-to-time ts)))))
  12077. (org-timestamp-change (- n) (cdr (assoc what whata)))
  12078. ;; Rematch, so that we have everything in
  12079. ;; place for the real shift.
  12080. (org-at-timestamp-p t)
  12081. (setq ts (match-string 1))
  12082. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)"
  12083. ts)))))
  12084. (save-excursion
  12085. (org-timestamp-change n (cdr (assoc what whata)) nil t))
  12086. (setq msg
  12087. (concat
  12088. msg type " " org-last-changed-timestamp " ")))))))))
  12089. (setq org-log-post-message msg)
  12090. (message "%s" msg))))
  12091. (defun org-show-todo-tree (arg)
  12092. "Make a compact tree which shows all headlines marked with TODO.
  12093. The tree will show the lines where the regexp matches, and all higher
  12094. headlines above the match.
  12095. With a \\[universal-argument] prefix, prompt for a regexp to match.
  12096. With a numeric prefix N, construct a sparse tree for the Nth element
  12097. of `org-todo-keywords-1'."
  12098. (interactive "P")
  12099. (let ((case-fold-search nil)
  12100. (kwd-re
  12101. (cond ((null arg) org-not-done-regexp)
  12102. ((equal arg '(4))
  12103. (let ((kwd
  12104. (completing-read "Keyword (or KWD1|KWD2|...): "
  12105. (mapcar #'list org-todo-keywords-1))))
  12106. (concat "\\("
  12107. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  12108. "\\)\\>")))
  12109. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  12110. (regexp-quote (nth (1- (prefix-numeric-value arg))
  12111. org-todo-keywords-1)))
  12112. (t (user-error "Invalid prefix argument: %s" arg)))))
  12113. (message "%d TODO entries found"
  12114. (org-occur (concat "^" org-outline-regexp " *" kwd-re )))))
  12115. (defun org-deadline (arg &optional time)
  12116. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  12117. With one universal prefix argument, remove any deadline from the item.
  12118. With two universal prefix arguments, prompt for a warning delay.
  12119. With argument TIME, set the deadline at the corresponding date. TIME
  12120. can either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  12121. (interactive "P")
  12122. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  12123. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  12124. 'region-start-level 'region))
  12125. org-loop-over-headlines-in-active-region)
  12126. (org-map-entries
  12127. `(org-deadline ',arg ,time)
  12128. org-loop-over-headlines-in-active-region
  12129. cl (when (outline-invisible-p) (org-end-of-subtree nil t))))
  12130. (let* ((old-date (org-entry-get nil "DEADLINE"))
  12131. (old-date-time (when old-date (org-time-string-to-time old-date)))
  12132. (repeater (and old-date
  12133. (string-match
  12134. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  12135. old-date)
  12136. (match-string 1 old-date))))
  12137. (cond
  12138. ((equal arg '(4))
  12139. (when (and old-date org-log-redeadline)
  12140. (org-add-log-setup 'deldeadline nil old-date org-log-redeadline))
  12141. (org-remove-timestamp-with-keyword org-deadline-string)
  12142. (message "Item no longer has a deadline."))
  12143. ((equal arg '(16))
  12144. (save-excursion
  12145. (org-back-to-heading t)
  12146. (if (re-search-forward
  12147. org-deadline-time-regexp
  12148. (save-excursion (outline-next-heading) (point)) t)
  12149. (let* ((rpl0 (match-string 1))
  12150. (rpl (replace-regexp-in-string " -[0-9]+[hdwmy]" "" rpl0)))
  12151. (replace-match
  12152. (concat org-deadline-string
  12153. " <" rpl
  12154. (format " -%dd"
  12155. (abs
  12156. (- (time-to-days
  12157. (save-match-data
  12158. (org-read-date nil t nil "Warn starting from" old-date-time)))
  12159. (time-to-days old-date-time))))
  12160. ">") t t))
  12161. (user-error "No deadline information to update"))))
  12162. (t
  12163. (org-add-planning-info 'deadline time 'closed)
  12164. (when (and old-date
  12165. org-log-redeadline
  12166. (not (equal old-date org-last-inserted-timestamp)))
  12167. (org-add-log-setup
  12168. 'redeadline org-last-inserted-timestamp old-date org-log-redeadline))
  12169. (when repeater
  12170. (save-excursion
  12171. (org-back-to-heading t)
  12172. (when (re-search-forward (concat org-deadline-string " "
  12173. org-last-inserted-timestamp)
  12174. (save-excursion
  12175. (outline-next-heading) (point)) t)
  12176. (goto-char (1- (match-end 0)))
  12177. (insert " " repeater)
  12178. (setq org-last-inserted-timestamp
  12179. (concat (substring org-last-inserted-timestamp 0 -1)
  12180. " " repeater
  12181. (substring org-last-inserted-timestamp -1))))))
  12182. (message "Deadline on %s" org-last-inserted-timestamp))))))
  12183. (defun org-schedule (arg &optional time)
  12184. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  12185. With one universal prefix argument, remove any scheduling date from the item.
  12186. With two universal prefix arguments, prompt for a delay cookie.
  12187. With argument TIME, scheduled at the corresponding date. TIME can
  12188. either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  12189. (interactive "P")
  12190. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  12191. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  12192. 'region-start-level 'region))
  12193. org-loop-over-headlines-in-active-region)
  12194. (org-map-entries
  12195. `(org-schedule ',arg ,time)
  12196. org-loop-over-headlines-in-active-region
  12197. cl (when (outline-invisible-p) (org-end-of-subtree nil t))))
  12198. (let* ((old-date (org-entry-get nil "SCHEDULED"))
  12199. (old-date-time (when old-date (org-time-string-to-time old-date)))
  12200. (repeater (and old-date
  12201. (string-match
  12202. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  12203. old-date)
  12204. (match-string 1 old-date))))
  12205. (cond
  12206. ((equal arg '(4))
  12207. (progn
  12208. (when (and old-date org-log-reschedule)
  12209. (org-add-log-setup 'delschedule nil old-date org-log-reschedule))
  12210. (org-remove-timestamp-with-keyword org-scheduled-string)
  12211. (message "Item is no longer scheduled.")))
  12212. ((equal arg '(16))
  12213. (save-excursion
  12214. (org-back-to-heading t)
  12215. (if (re-search-forward
  12216. org-scheduled-time-regexp
  12217. (save-excursion (outline-next-heading) (point)) t)
  12218. (let* ((rpl0 (match-string 1))
  12219. (rpl (replace-regexp-in-string " -[0-9]+[hdwmy]" "" rpl0)))
  12220. (replace-match
  12221. (concat org-scheduled-string
  12222. " <" rpl
  12223. (format " -%dd"
  12224. (abs
  12225. (- (time-to-days
  12226. (save-match-data
  12227. (org-read-date nil t nil "Delay until" old-date-time)))
  12228. (time-to-days old-date-time))))
  12229. ">") t t))
  12230. (user-error "No scheduled information to update"))))
  12231. (t
  12232. (org-add-planning-info 'scheduled time 'closed)
  12233. (when (and old-date
  12234. org-log-reschedule
  12235. (not (equal old-date org-last-inserted-timestamp)))
  12236. (org-add-log-setup
  12237. 'reschedule org-last-inserted-timestamp old-date org-log-reschedule))
  12238. (when repeater
  12239. (save-excursion
  12240. (org-back-to-heading t)
  12241. (when (re-search-forward (concat org-scheduled-string " "
  12242. org-last-inserted-timestamp)
  12243. (save-excursion
  12244. (outline-next-heading) (point)) t)
  12245. (goto-char (1- (match-end 0)))
  12246. (insert " " repeater)
  12247. (setq org-last-inserted-timestamp
  12248. (concat (substring org-last-inserted-timestamp 0 -1)
  12249. " " repeater
  12250. (substring org-last-inserted-timestamp -1))))))
  12251. (message "Scheduled to %s" org-last-inserted-timestamp))))))
  12252. (defun org-get-scheduled-time (pom &optional inherit)
  12253. "Get the scheduled time as a time tuple, of a format suitable
  12254. for calling org-schedule with, or if there is no scheduling,
  12255. returns nil."
  12256. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  12257. (when time
  12258. (apply 'encode-time (org-parse-time-string time)))))
  12259. (defun org-get-deadline-time (pom &optional inherit)
  12260. "Get the deadline as a time tuple, of a format suitable for
  12261. calling org-deadline with, or if there is no scheduling, returns
  12262. nil."
  12263. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  12264. (when time
  12265. (apply 'encode-time (org-parse-time-string time)))))
  12266. (defun org-remove-timestamp-with-keyword (keyword)
  12267. "Remove all time stamps with KEYWORD in the current entry."
  12268. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  12269. beg)
  12270. (save-excursion
  12271. (org-back-to-heading t)
  12272. (setq beg (point))
  12273. (outline-next-heading)
  12274. (while (re-search-backward re beg t)
  12275. (replace-match "")
  12276. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  12277. (equal (char-before) ?\ ))
  12278. (backward-delete-char 1)
  12279. (when (string-match "^[ \t]*$" (buffer-substring
  12280. (point-at-bol) (point-at-eol)))
  12281. (delete-region (point-at-bol)
  12282. (min (point-max) (1+ (point-at-eol))))))))))
  12283. (defvar org-time-was-given) ; dynamically scoped parameter
  12284. (defvar org-end-time-was-given) ; dynamically scoped parameter
  12285. (defun org-at-planning-p ()
  12286. "Non-nil when point is on a planning info line."
  12287. ;; This is as accurate and faster than `org-element-at-point' since
  12288. ;; planning info location is fixed in the section.
  12289. (org-with-wide-buffer
  12290. (beginning-of-line)
  12291. (and (org-looking-at-p org-planning-line-re)
  12292. (eq (point)
  12293. (ignore-errors
  12294. (if (and (featurep 'org-inlinetask) (org-inlinetask-in-task-p))
  12295. (org-back-to-heading t)
  12296. (org-with-limited-levels (org-back-to-heading t)))
  12297. (line-beginning-position 2))))))
  12298. (defun org-add-planning-info (what &optional time &rest remove)
  12299. "Insert new timestamp with keyword in the planning line.
  12300. WHAT indicates what kind of time stamp to add. It is a symbol
  12301. among `closed', `deadline', `scheduled' and nil. TIME indicates
  12302. the time to use. If none is given, the user is prompted for
  12303. a date. REMOVE indicates what kind of entries to remove. An old
  12304. WHAT entry will also be removed."
  12305. (let (org-time-was-given org-end-time-was-given default-time default-input)
  12306. (catch 'exit
  12307. (when (and (memq what '(scheduled deadline))
  12308. (or (not time)
  12309. (and (stringp time)
  12310. (string-match "^[-+]+[0-9]" time))))
  12311. ;; Try to get a default date/time from existing timestamp
  12312. (save-excursion
  12313. (org-back-to-heading t)
  12314. (let ((end (save-excursion (outline-next-heading) (point))) ts)
  12315. (when (re-search-forward (if (eq what 'scheduled)
  12316. org-scheduled-time-regexp
  12317. org-deadline-time-regexp)
  12318. end t)
  12319. (setq ts (match-string 1)
  12320. default-time (apply 'encode-time (org-parse-time-string ts))
  12321. default-input (and ts (org-get-compact-tod ts)))))))
  12322. (when what
  12323. (setq time
  12324. (if (stringp time)
  12325. ;; This is a string (relative or absolute), set
  12326. ;; proper date.
  12327. (apply #'encode-time
  12328. (org-read-date-analyze
  12329. time default-time (decode-time default-time)))
  12330. ;; If necessary, get the time from the user
  12331. (or time (org-read-date nil 'to-time nil nil
  12332. default-time default-input)))))
  12333. (org-with-wide-buffer
  12334. (org-back-to-heading t)
  12335. (forward-line)
  12336. (unless (bolp) (insert "\n"))
  12337. (cond ((org-looking-at-p org-planning-line-re)
  12338. ;; Move to current indentation.
  12339. (skip-chars-forward " \t")
  12340. ;; Check if we have to remove something.
  12341. (dolist (type (if what (cons what remove) remove))
  12342. (save-excursion
  12343. (when (re-search-forward
  12344. (cl-case type
  12345. (closed org-closed-time-regexp)
  12346. (deadline org-deadline-time-regexp)
  12347. (scheduled org-scheduled-time-regexp)
  12348. (otherwise
  12349. (error "Invalid planning type: %s" type)))
  12350. (line-end-position) t)
  12351. ;; Delete until next keyword or end of line.
  12352. (delete-region
  12353. (match-beginning 0)
  12354. (if (re-search-forward org-keyword-time-not-clock-regexp
  12355. (line-end-position)
  12356. t)
  12357. (match-beginning 0)
  12358. (line-end-position))))))
  12359. ;; If there is nothing more to add and no more keyword
  12360. ;; is left, remove the line completely.
  12361. (if (and (org-looking-at-p "[ \t]*$") (not what))
  12362. (delete-region (line-beginning-position)
  12363. (line-beginning-position 2))
  12364. ;; If we removed last keyword, do not leave trailing
  12365. ;; white space at the end of line.
  12366. (let ((p (point)))
  12367. (save-excursion
  12368. (end-of-line)
  12369. (unless (= (skip-chars-backward " \t" p) 0)
  12370. (delete-region (point) (line-end-position)))))))
  12371. ((not what) (throw 'exit nil)) ; Nothing to do.
  12372. (t (insert-before-markers "\n")
  12373. (backward-char 1)
  12374. (when org-adapt-indentation
  12375. (org-indent-to-column (1+ (org-outline-level))))))
  12376. (when what
  12377. ;; Insert planning keyword.
  12378. (insert (cl-case what
  12379. (closed org-closed-string)
  12380. (deadline org-deadline-string)
  12381. (scheduled org-scheduled-string)
  12382. (otherwise (error "Invalid planning type: %s" what)))
  12383. " ")
  12384. ;; Insert associated timestamp.
  12385. (let ((ts (org-insert-time-stamp
  12386. time
  12387. (or org-time-was-given
  12388. (and (eq what 'closed) org-log-done-with-time))
  12389. (eq what 'closed)
  12390. nil nil (list org-end-time-was-given))))
  12391. (unless (eolp) (insert " "))
  12392. ts))))))
  12393. (defvar org-log-note-marker (make-marker)
  12394. "Marker pointing at the entry where the note is to be inserted.")
  12395. (defvar org-log-note-purpose nil)
  12396. (defvar org-log-note-state nil)
  12397. (defvar org-log-note-previous-state nil)
  12398. (defvar org-log-note-extra nil)
  12399. (defvar org-log-note-window-configuration nil)
  12400. (defvar org-log-note-return-to (make-marker))
  12401. (defvar org-log-note-effective-time nil
  12402. "Remembered current time so that dynamically scoped
  12403. `org-extend-today-until' affects timestamps in state change log")
  12404. (defvar org-log-post-message nil
  12405. "Message to be displayed after a log note has been stored.
  12406. The auto-repeater uses this.")
  12407. (defun org-add-note ()
  12408. "Add a note to the current entry.
  12409. This is done in the same way as adding a state change note."
  12410. (interactive)
  12411. (org-add-log-setup 'note))
  12412. (defun org-log-beginning (&optional create)
  12413. "Return expected start of log notes in current entry.
  12414. When optional argument CREATE is non-nil, the function creates
  12415. a drawer to store notes, if necessary. Returned position ignores
  12416. narrowing."
  12417. (org-with-wide-buffer
  12418. (let ((drawer (org-log-into-drawer)))
  12419. (cond
  12420. (drawer
  12421. (org-end-of-meta-data)
  12422. (let ((regexp (concat "^[ \t]*:" (regexp-quote drawer) ":[ \t]*$"))
  12423. (end (if (org-at-heading-p) (point)
  12424. (save-excursion (outline-next-heading) (point))))
  12425. (case-fold-search t))
  12426. (catch 'exit
  12427. ;; Try to find existing drawer.
  12428. (while (re-search-forward regexp end t)
  12429. (let ((element (org-element-at-point)))
  12430. (when (eq (org-element-type element) 'drawer)
  12431. (let ((cend (org-element-property :contents-end element)))
  12432. (when (and (not org-log-states-order-reversed) cend)
  12433. (goto-char cend)))
  12434. (throw 'exit nil))))
  12435. ;; No drawer found. Create one, if permitted.
  12436. (when create
  12437. (unless (bolp) (insert "\n"))
  12438. (let ((beg (point)))
  12439. (insert ":" drawer ":\n:END:\n")
  12440. (org-indent-region beg (point)))
  12441. (end-of-line -1)))))
  12442. (t
  12443. (org-end-of-meta-data org-log-state-notes-insert-after-drawers)
  12444. (skip-chars-forward " \t\n")
  12445. (beginning-of-line)
  12446. (unless org-log-states-order-reversed
  12447. (org-skip-over-state-notes)
  12448. (skip-chars-backward " \t\n")
  12449. (forward-line)))))
  12450. (if (bolp) (point) (line-beginning-position 2))))
  12451. (defun org-add-log-setup (&optional purpose state prev-state how extra)
  12452. "Set up the post command hook to take a note.
  12453. If this is about to TODO state change, the new state is expected in STATE.
  12454. HOW is an indicator what kind of note should be created.
  12455. EXTRA is additional text that will be inserted into the notes buffer."
  12456. (move-marker org-log-note-marker (point))
  12457. (setq org-log-note-purpose purpose
  12458. org-log-note-state state
  12459. org-log-note-previous-state prev-state
  12460. org-log-note-how how
  12461. org-log-note-extra extra
  12462. org-log-note-effective-time (org-current-effective-time))
  12463. (add-hook 'post-command-hook 'org-add-log-note 'append))
  12464. (defun org-skip-over-state-notes ()
  12465. "Skip past the list of State notes in an entry."
  12466. (when (ignore-errors (goto-char (org-in-item-p)))
  12467. (let* ((struct (org-list-struct))
  12468. (prevs (org-list-prevs-alist struct))
  12469. (regexp
  12470. (concat "[ \t]*- +"
  12471. (replace-regexp-in-string
  12472. " +" " +"
  12473. (org-replace-escapes
  12474. (regexp-quote (cdr (assq 'state org-log-note-headings)))
  12475. `(("%d" . ,org-ts-regexp-inactive)
  12476. ("%D" . ,org-ts-regexp)
  12477. ("%s" . "\"\\S-+\"")
  12478. ("%S" . "\"\\S-+\"")
  12479. ("%t" . ,org-ts-regexp-inactive)
  12480. ("%T" . ,org-ts-regexp)
  12481. ("%u" . ".*?")
  12482. ("%U" . ".*?")))))))
  12483. (while (org-looking-at-p regexp)
  12484. (goto-char (or (org-list-get-next-item (point) struct prevs)
  12485. (org-list-get-item-end (point) struct)))))))
  12486. (defun org-add-log-note (&optional _purpose)
  12487. "Pop up a window for taking a note, and add this note later."
  12488. (remove-hook 'post-command-hook 'org-add-log-note)
  12489. (setq org-log-note-window-configuration (current-window-configuration))
  12490. (delete-other-windows)
  12491. (move-marker org-log-note-return-to (point))
  12492. (org-pop-to-buffer-same-window (marker-buffer org-log-note-marker))
  12493. (goto-char org-log-note-marker)
  12494. (org-switch-to-buffer-other-window "*Org Note*")
  12495. (erase-buffer)
  12496. (if (memq org-log-note-how '(time state))
  12497. (let (current-prefix-arg) (org-store-log-note))
  12498. (let ((org-inhibit-startup t)) (org-mode))
  12499. (insert (format "# Insert note for %s.
  12500. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  12501. (cond
  12502. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  12503. ((eq org-log-note-purpose 'done) "closed todo item")
  12504. ((eq org-log-note-purpose 'state)
  12505. (format "state change from \"%s\" to \"%s\""
  12506. (or org-log-note-previous-state "")
  12507. (or org-log-note-state "")))
  12508. ((eq org-log-note-purpose 'reschedule)
  12509. "rescheduling")
  12510. ((eq org-log-note-purpose 'delschedule)
  12511. "no longer scheduled")
  12512. ((eq org-log-note-purpose 'redeadline)
  12513. "changing deadline")
  12514. ((eq org-log-note-purpose 'deldeadline)
  12515. "removing deadline")
  12516. ((eq org-log-note-purpose 'refile)
  12517. "refiling")
  12518. ((eq org-log-note-purpose 'note)
  12519. "this entry")
  12520. (t (error "This should not happen")))))
  12521. (when org-log-note-extra (insert org-log-note-extra))
  12522. (setq-local org-finish-function 'org-store-log-note)
  12523. (run-hooks 'org-log-buffer-setup-hook)))
  12524. (defvar org-note-abort nil) ; dynamically scoped
  12525. (defun org-store-log-note ()
  12526. "Finish taking a log note, and insert it to where it belongs."
  12527. (let ((txt (prog1 (buffer-string)
  12528. (kill-buffer)))
  12529. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  12530. lines)
  12531. (while (string-match "\\`# .*\n[ \t\n]*" txt)
  12532. (setq txt (replace-match "" t t txt)))
  12533. (when (string-match "\\s-+\\'" txt)
  12534. (setq txt (replace-match "" t t txt)))
  12535. (setq lines (org-split-string txt "\n"))
  12536. (when (org-string-nw-p note)
  12537. (setq note
  12538. (org-replace-escapes
  12539. note
  12540. (list (cons "%u" (user-login-name))
  12541. (cons "%U" user-full-name)
  12542. (cons "%t" (format-time-string
  12543. (org-time-stamp-format 'long 'inactive)
  12544. org-log-note-effective-time))
  12545. (cons "%T" (format-time-string
  12546. (org-time-stamp-format 'long nil)
  12547. org-log-note-effective-time))
  12548. (cons "%d" (format-time-string
  12549. (org-time-stamp-format nil 'inactive)
  12550. org-log-note-effective-time))
  12551. (cons "%D" (format-time-string
  12552. (org-time-stamp-format nil nil)
  12553. org-log-note-effective-time))
  12554. (cons "%s" (cond
  12555. ((not org-log-note-state) "")
  12556. ((org-string-match-p org-ts-regexp
  12557. org-log-note-state)
  12558. (format "\"[%s]\""
  12559. (substring org-log-note-state 1 -1)))
  12560. (t (format "\"%s\"" org-log-note-state))))
  12561. (cons "%S"
  12562. (cond
  12563. ((not org-log-note-previous-state) "")
  12564. ((org-string-match-p org-ts-regexp
  12565. org-log-note-previous-state)
  12566. (format "\"[%s]\""
  12567. (substring
  12568. org-log-note-previous-state 1 -1)))
  12569. (t (format "\"%s\""
  12570. org-log-note-previous-state)))))))
  12571. (when lines (setq note (concat note " \\\\")))
  12572. (push note lines))
  12573. (when (and lines (not (or current-prefix-arg org-note-abort)))
  12574. (with-current-buffer (marker-buffer org-log-note-marker)
  12575. (org-with-wide-buffer
  12576. ;; Find location for the new note.
  12577. (goto-char org-log-note-marker)
  12578. (set-marker org-log-note-marker nil)
  12579. (goto-char (org-log-beginning t))
  12580. ;; Make sure point is at the beginning of an empty line.
  12581. (cond ((not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  12582. ((looking-at "[ \t]*\\S-") (save-excursion (insert "\n"))))
  12583. ;; In an existing list, add a new item at the top level.
  12584. ;; Otherwise, indent line like a regular one.
  12585. (let ((itemp (org-in-item-p)))
  12586. (if itemp
  12587. (org-indent-line-to
  12588. (let ((struct (save-excursion
  12589. (goto-char itemp) (org-list-struct))))
  12590. (org-list-get-ind (org-list-get-top-point struct) struct)))
  12591. (org-indent-line)))
  12592. (insert (org-list-bullet-string "-") (pop lines))
  12593. (let ((ind (org-list-item-body-column (line-beginning-position))))
  12594. (dolist (line lines)
  12595. (insert "\n")
  12596. (org-indent-line-to ind)
  12597. (insert line)))
  12598. (message "Note stored")
  12599. (org-back-to-heading t)
  12600. (org-cycle-hide-drawers 'children))
  12601. ;; Fix `buffer-undo-list' when `org-store-log-note' is called
  12602. ;; from within `org-add-log-note' because `buffer-undo-list'
  12603. ;; is then modified outside of `org-with-remote-undo'.
  12604. (when (eq this-command 'org-agenda-todo)
  12605. (setcdr buffer-undo-list (cddr buffer-undo-list))))))
  12606. ;; Don't add undo information when called from `org-agenda-todo'.
  12607. (let ((buffer-undo-list (eq this-command 'org-agenda-todo)))
  12608. (set-window-configuration org-log-note-window-configuration)
  12609. (with-current-buffer (marker-buffer org-log-note-return-to)
  12610. (goto-char org-log-note-return-to))
  12611. (move-marker org-log-note-return-to nil)
  12612. (when org-log-post-message (message "%s" org-log-post-message))))
  12613. (defun org-remove-empty-drawer-at (pos)
  12614. "Remove an empty drawer at position POS.
  12615. POS may also be a marker."
  12616. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  12617. (org-with-wide-buffer
  12618. (goto-char pos)
  12619. (let ((drawer (org-element-at-point)))
  12620. (when (and (memq (org-element-type drawer) '(drawer property-drawer))
  12621. (not (org-element-property :contents-begin drawer)))
  12622. (delete-region (org-element-property :begin drawer)
  12623. (progn (goto-char (org-element-property :end drawer))
  12624. (skip-chars-backward " \r\t\n")
  12625. (forward-line)
  12626. (point))))))))
  12627. (defvar org-ts-type nil)
  12628. (defun org-sparse-tree (&optional arg type)
  12629. "Create a sparse tree, prompt for the details.
  12630. This command can create sparse trees. You first need to select the type
  12631. of match used to create the tree:
  12632. t Show all TODO entries.
  12633. T Show entries with a specific TODO keyword.
  12634. m Show entries selected by a tags/property match.
  12635. p Enter a property name and its value (both with completion on existing
  12636. names/values) and show entries with that property.
  12637. r Show entries matching a regular expression (`/' can be used as well).
  12638. b Show deadlines and scheduled items before a date.
  12639. a Show deadlines and scheduled items after a date.
  12640. d Show deadlines due within `org-deadline-warning-days'.
  12641. D Show deadlines and scheduled items between a date range."
  12642. (interactive "P")
  12643. (setq type (or type org-sparse-tree-default-date-type))
  12644. (setq org-ts-type type)
  12645. (message "Sparse tree: [/]regexp [t]odo [T]odo-kwd [m]atch [p]roperty
  12646. \[d]eadlines [b]efore-date [a]fter-date [D]ates range
  12647. \[c]ycle through date types: %s"
  12648. (cl-case type
  12649. (all "all timestamps")
  12650. (scheduled "only scheduled")
  12651. (deadline "only deadline")
  12652. (active "only active timestamps")
  12653. (inactive "only inactive timestamps")
  12654. (closed "with a closed time-stamp")
  12655. (otherwise "scheduled/deadline")))
  12656. (let ((answer (read-char-exclusive)))
  12657. (cl-case answer
  12658. (?c
  12659. (org-sparse-tree
  12660. arg
  12661. (cadr
  12662. (memq type '(nil all scheduled deadline active inactive closed)))))
  12663. (?d (call-interactively 'org-check-deadlines))
  12664. (?b (call-interactively 'org-check-before-date))
  12665. (?a (call-interactively 'org-check-after-date))
  12666. (?D (call-interactively 'org-check-dates-range))
  12667. (?t (call-interactively 'org-show-todo-tree))
  12668. (?T (org-show-todo-tree '(4)))
  12669. (?m (call-interactively 'org-match-sparse-tree))
  12670. ((?p ?P)
  12671. (let* ((kwd (completing-read
  12672. "Property: " (mapcar #'list (org-buffer-property-keys))))
  12673. (value (completing-read
  12674. "Value: " (mapcar #'list (org-property-values kwd)))))
  12675. (unless (string-match "\\`{.*}\\'" value)
  12676. (setq value (concat "\"" value "\"")))
  12677. (org-match-sparse-tree arg (concat kwd "=" value))))
  12678. ((?r ?R ?/) (call-interactively 'org-occur))
  12679. (otherwise (user-error "No such sparse tree command \"%c\"" answer)))))
  12680. (defvar-local org-occur-highlights nil
  12681. "List of overlays used for occur matches.")
  12682. (defvar-local org-occur-parameters nil
  12683. "Parameters of the active org-occur calls.
  12684. This is a list, each call to org-occur pushes as cons cell,
  12685. containing the regular expression and the callback, onto the list.
  12686. The list can contain several entries if `org-occur' has been called
  12687. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  12688. will only contain one set of parameters. When the highlights are
  12689. removed (for example with `C-c C-c', or with the next edit (depending
  12690. on `org-remove-highlights-with-change'), this variable is emptied
  12691. as well.")
  12692. (defun org-occur (regexp &optional keep-previous callback)
  12693. "Make a compact tree which shows all matches of REGEXP.
  12694. The tree will show the lines where the regexp matches, and any other context
  12695. defined in `org-show-context-detail', which see.
  12696. When optional argument KEEP-PREVIOUS is non-nil, highlighting and exposing
  12697. done by a previous call to `org-occur' will be kept, to allow stacking of
  12698. calls to this command.
  12699. Optional argument CALLBACK can be a function of no argument. In this case,
  12700. it is called with point at the end of the match, match data being set
  12701. accordingly. Current match is shown only if the return value is non-nil.
  12702. The function must neither move point nor alter narrowing."
  12703. (interactive "sRegexp: \nP")
  12704. (when (equal regexp "")
  12705. (user-error "Regexp cannot be empty"))
  12706. (unless keep-previous
  12707. (org-remove-occur-highlights nil nil t))
  12708. (push (cons regexp callback) org-occur-parameters)
  12709. (let ((cnt 0))
  12710. (save-excursion
  12711. (goto-char (point-min))
  12712. (when (or (not keep-previous) ; do not want to keep
  12713. (not org-occur-highlights)) ; no previous matches
  12714. ;; hide everything
  12715. (org-overview))
  12716. (let ((case-fold-search (if (eq org-occur-case-fold-search 'smart)
  12717. (isearch-no-upper-case-p regexp t)
  12718. org-occur-case-fold-search)))
  12719. (while (re-search-forward regexp nil t)
  12720. (when (or (not callback)
  12721. (save-match-data (funcall callback)))
  12722. (setq cnt (1+ cnt))
  12723. (when org-highlight-sparse-tree-matches
  12724. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  12725. (org-show-context 'occur-tree)))))
  12726. (when org-remove-highlights-with-change
  12727. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  12728. nil 'local))
  12729. (unless org-sparse-tree-open-archived-trees
  12730. (org-hide-archived-subtrees (point-min) (point-max)))
  12731. (run-hooks 'org-occur-hook)
  12732. (when (org-called-interactively-p 'interactive)
  12733. (message "%d match(es) for regexp %s" cnt regexp))
  12734. cnt))
  12735. (defun org-occur-next-match (&optional n _reset)
  12736. "Function for `next-error-function' to find sparse tree matches.
  12737. N is the number of matches to move, when negative move backwards.
  12738. This function always goes back to the starting point when no
  12739. match is found."
  12740. (let* ((limit (if (< n 0) (point-min) (point-max)))
  12741. (search-func (if (< n 0)
  12742. 'previous-single-char-property-change
  12743. 'next-single-char-property-change))
  12744. (n (abs n))
  12745. (pos (point))
  12746. p1)
  12747. (catch 'exit
  12748. (while (setq p1 (funcall search-func (point) 'org-type))
  12749. (when (equal p1 limit)
  12750. (goto-char pos)
  12751. (user-error "No more matches"))
  12752. (when (equal (get-char-property p1 'org-type) 'org-occur)
  12753. (setq n (1- n))
  12754. (when (= n 0)
  12755. (goto-char p1)
  12756. (throw 'exit (point))))
  12757. (goto-char p1))
  12758. (goto-char p1)
  12759. (user-error "No more matches"))))
  12760. (defun org-show-context (&optional key)
  12761. "Make sure point and context are visible.
  12762. Optional argument KEY, when non-nil, is a symbol. See
  12763. `org-show-context-detail' for allowed values and how much is to
  12764. be shown."
  12765. (org-show-set-visibility
  12766. (cond ((symbolp org-show-context-detail) org-show-context-detail)
  12767. ((cdr (assq key org-show-context-detail)))
  12768. (t (cdr (assq 'default org-show-context-detail))))))
  12769. (defun org-show-set-visibility (detail)
  12770. "Set visibility around point according to DETAIL.
  12771. DETAIL is either nil, `minimal', `local', `ancestors', `lineage',
  12772. `tree', `canonical' or t. See `org-show-context-detail' for more
  12773. information."
  12774. ;; Show current heading and possibly its entry, following headline
  12775. ;; or all children.
  12776. (if (and (org-at-heading-p) (not (eq detail 'local)))
  12777. (org-flag-heading nil)
  12778. (org-show-entry)
  12779. ;; If point is hidden within a drawer or a block, make sure to
  12780. ;; expose it.
  12781. (dolist (o (overlays-at (point)))
  12782. (when (memq (overlay-get o 'invisible) '(org-hide-block outline))
  12783. (delete-overlay o)))
  12784. (unless (org-before-first-heading-p)
  12785. (org-with-limited-levels
  12786. (cl-case detail
  12787. ((tree canonical t) (org-show-children))
  12788. ((nil minimal ancestors))
  12789. (t (save-excursion
  12790. (outline-next-heading)
  12791. (org-flag-heading nil)))))))
  12792. ;; Show all siblings.
  12793. (when (eq detail 'lineage) (org-show-siblings))
  12794. ;; Show ancestors, possibly with their children.
  12795. (when (memq detail '(ancestors lineage tree canonical t))
  12796. (save-excursion
  12797. (while (org-up-heading-safe)
  12798. (org-flag-heading nil)
  12799. (when (memq detail '(canonical t)) (org-show-entry))
  12800. (when (memq detail '(tree canonical t)) (org-show-children))))))
  12801. (defvar org-reveal-start-hook nil
  12802. "Hook run before revealing a location.")
  12803. (defun org-reveal (&optional siblings)
  12804. "Show current entry, hierarchy above it, and the following headline.
  12805. This can be used to show a consistent set of context around
  12806. locations exposed with `org-show-context'.
  12807. With optional argument SIBLINGS, on each level of the hierarchy all
  12808. siblings are shown. This repairs the tree structure to what it would
  12809. look like when opened with hierarchical calls to `org-cycle'.
  12810. With double optional argument \\[universal-argument] \\[universal-argument], \
  12811. go to the parent and show the
  12812. entire tree."
  12813. (interactive "P")
  12814. (run-hooks 'org-reveal-start-hook)
  12815. (cond ((equal siblings '(4)) (org-show-set-visibility 'canonical))
  12816. ((equal siblings '(16))
  12817. (save-excursion
  12818. (when (org-up-heading-safe)
  12819. (org-show-subtree)
  12820. (run-hook-with-args 'org-cycle-hook 'subtree))))
  12821. (t (org-show-set-visibility 'lineage))))
  12822. (defun org-highlight-new-match (beg end)
  12823. "Highlight from BEG to END and mark the highlight is an occur headline."
  12824. (let ((ov (make-overlay beg end)))
  12825. (overlay-put ov 'face 'secondary-selection)
  12826. (overlay-put ov 'org-type 'org-occur)
  12827. (push ov org-occur-highlights)))
  12828. (defun org-remove-occur-highlights (&optional _beg _end noremove)
  12829. "Remove the occur highlights from the buffer.
  12830. BEG and END are ignored. If NOREMOVE is nil, remove this function
  12831. from the `before-change-functions' in the current buffer."
  12832. (interactive)
  12833. (unless org-inhibit-highlight-removal
  12834. (mapc #'delete-overlay org-occur-highlights)
  12835. (setq org-occur-highlights nil)
  12836. (setq org-occur-parameters nil)
  12837. (unless noremove
  12838. (remove-hook 'before-change-functions
  12839. 'org-remove-occur-highlights 'local))))
  12840. ;;;; Priorities
  12841. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  12842. "Regular expression matching the priority indicator.")
  12843. (defvar org-remove-priority-next-time nil)
  12844. (defun org-priority-up ()
  12845. "Increase the priority of the current item."
  12846. (interactive)
  12847. (org-priority 'up))
  12848. (defun org-priority-down ()
  12849. "Decrease the priority of the current item."
  12850. (interactive)
  12851. (org-priority 'down))
  12852. (defun org-priority (&optional action _show)
  12853. "Change the priority of an item.
  12854. ACTION can be `set', `up', `down', or a character."
  12855. (interactive "P")
  12856. (if (equal action '(4))
  12857. (org-show-priority)
  12858. (unless org-enable-priority-commands
  12859. (user-error "Priority commands are disabled"))
  12860. (setq action (or action 'set))
  12861. (let (current new news have remove)
  12862. (save-excursion
  12863. (org-back-to-heading t)
  12864. (when (looking-at org-priority-regexp)
  12865. (setq current (string-to-char (match-string 2))
  12866. have t))
  12867. (cond
  12868. ((eq action 'remove)
  12869. (setq remove t new ?\ ))
  12870. ((or (eq action 'set)
  12871. (if (featurep 'xemacs) (characterp action) (integerp action)))
  12872. (if (not (eq action 'set))
  12873. (setq new action)
  12874. (message "Priority %c-%c, SPC to remove: "
  12875. org-highest-priority org-lowest-priority)
  12876. (save-match-data
  12877. (setq new (read-char-exclusive))))
  12878. (when (and (= (upcase org-highest-priority) org-highest-priority)
  12879. (= (upcase org-lowest-priority) org-lowest-priority))
  12880. (setq new (upcase new)))
  12881. (cond ((equal new ?\ ) (setq remove t))
  12882. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  12883. (user-error "Priority must be between `%c' and `%c'"
  12884. org-highest-priority org-lowest-priority))))
  12885. ((eq action 'up)
  12886. (setq new (if have
  12887. (1- current) ; normal cycling
  12888. ;; last priority was empty
  12889. (if (eq last-command this-command)
  12890. org-lowest-priority ; wrap around empty to lowest
  12891. ;; default
  12892. (if org-priority-start-cycle-with-default
  12893. org-default-priority
  12894. (1- org-default-priority))))))
  12895. ((eq action 'down)
  12896. (setq new (if have
  12897. (1+ current) ; normal cycling
  12898. ;; last priority was empty
  12899. (if (eq last-command this-command)
  12900. org-highest-priority ; wrap around empty to highest
  12901. ;; default
  12902. (if org-priority-start-cycle-with-default
  12903. org-default-priority
  12904. (1+ org-default-priority))))))
  12905. (t (user-error "Invalid action")))
  12906. (when (or (< (upcase new) org-highest-priority)
  12907. (> (upcase new) org-lowest-priority))
  12908. (if (and (memq action '(up down))
  12909. (not have) (not (eq last-command this-command)))
  12910. ;; `new' is from default priority
  12911. (error
  12912. "The default can not be set, see `org-default-priority' why")
  12913. ;; normal cycling: `new' is beyond highest/lowest priority
  12914. ;; and is wrapped around to the empty priority
  12915. (setq remove t)))
  12916. (setq news (format "%c" new))
  12917. (if have
  12918. (if remove
  12919. (replace-match "" t t nil 1)
  12920. (replace-match news t t nil 2))
  12921. (if remove
  12922. (user-error "No priority cookie found in line")
  12923. (let ((case-fold-search nil))
  12924. (looking-at org-todo-line-regexp))
  12925. (if (match-end 2)
  12926. (progn
  12927. (goto-char (match-end 2))
  12928. (insert " [#" news "]"))
  12929. (goto-char (match-beginning 3))
  12930. (insert "[#" news "] "))))
  12931. (org-set-tags nil 'align))
  12932. (if remove
  12933. (message "Priority removed")
  12934. (message "Priority of current item set to %s" news)))))
  12935. (defun org-show-priority ()
  12936. "Show the priority of the current item.
  12937. This priority is composed of the main priority given with the [#A] cookies,
  12938. and by additional input from the age of a schedules or deadline entry."
  12939. (interactive)
  12940. (let ((pri (if (eq major-mode 'org-agenda-mode)
  12941. (org-get-at-bol 'priority)
  12942. (save-excursion
  12943. (save-match-data
  12944. (beginning-of-line)
  12945. (and (looking-at org-heading-regexp)
  12946. (org-get-priority (match-string 0))))))))
  12947. (message "Priority is %d" (if pri pri -1000))))
  12948. (defun org-get-priority (s)
  12949. "Find priority cookie and return priority."
  12950. (save-match-data
  12951. (if (functionp org-get-priority-function)
  12952. (funcall org-get-priority-function)
  12953. (if (not (string-match org-priority-regexp s))
  12954. (* 1000 (- org-lowest-priority org-default-priority))
  12955. (* 1000 (- org-lowest-priority
  12956. (string-to-char (match-string 2 s))))))))
  12957. ;;;; Tags
  12958. (defvar org-agenda-archives-mode)
  12959. (defvar org-map-continue-from nil
  12960. "Position from where mapping should continue.
  12961. Can be set by the action argument to `org-scan-tags' and `org-map-entries'.")
  12962. (defvar org-scanner-tags nil
  12963. "The current tag list while the tags scanner is running.")
  12964. (defvar org-trust-scanner-tags nil
  12965. "Should `org-get-tags-at' use the tags for the scanner.
  12966. This is for internal dynamical scoping only.
  12967. When this is non-nil, the function `org-get-tags-at' will return the value
  12968. of `org-scanner-tags' instead of building the list by itself. This
  12969. can lead to large speed-ups when the tags scanner is used in a file with
  12970. many entries, and when the list of tags is retrieved, for example to
  12971. obtain a list of properties. Building the tags list for each entry in such
  12972. a file becomes an N^2 operation - but with this variable set, it scales
  12973. as N.")
  12974. (defvar org--matcher-tags-todo-only nil)
  12975. (defun org-scan-tags (action matcher todo-only &optional start-level)
  12976. "Scan headline tags with inheritance and produce output ACTION.
  12977. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  12978. or `agenda' to produce an entry list for an agenda view. It can also be
  12979. a Lisp form or a function that should be called at each matched headline, in
  12980. this case the return value is a list of all return values from these calls.
  12981. MATCHER is a function accepting three arguments, returning
  12982. a non-nil value whenever a given set of tags qualifies a headline
  12983. for inclusion. See `org-make-tags-matcher' for more information.
  12984. As a special case, it can also be set to t (respectively nil) in
  12985. order to match all (respectively none) headline.
  12986. When TODO-ONLY is non-nil, only lines with a not-done TODO
  12987. keyword are included in the output.
  12988. START-LEVEL can be a string with asterisks, reducing the scope to
  12989. headlines matching this string."
  12990. (require 'org-agenda)
  12991. (let* ((re (concat "^"
  12992. (if start-level
  12993. ;; Get the correct level to match
  12994. (concat "\\*\\{" (number-to-string start-level) "\\} ")
  12995. org-outline-regexp)
  12996. " *\\(\\<\\("
  12997. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  12998. (org-re "\\)\\>\\)? *\\(.*?\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*$")))
  12999. (props (list 'face 'default
  13000. 'done-face 'org-agenda-done
  13001. 'undone-face 'default
  13002. 'mouse-face 'highlight
  13003. 'org-not-done-regexp org-not-done-regexp
  13004. 'org-todo-regexp org-todo-regexp
  13005. 'org-complex-heading-regexp org-complex-heading-regexp
  13006. 'help-echo
  13007. (format "mouse-2 or RET jump to org file %s"
  13008. (abbreviate-file-name
  13009. (or (buffer-file-name (buffer-base-buffer))
  13010. (buffer-name (buffer-base-buffer)))))))
  13011. (org-map-continue-from nil)
  13012. lspos tags tags-list
  13013. (tags-alist (list (cons 0 org-file-tags)))
  13014. (llast 0) rtn rtn1 level category i txt
  13015. todo marker entry priority
  13016. ts-date ts-date-type ts-date-pair)
  13017. (unless (or (member action '(agenda sparse-tree)) (functionp action))
  13018. (setq action (list 'lambda nil action)))
  13019. (save-excursion
  13020. (goto-char (point-min))
  13021. (when (eq action 'sparse-tree)
  13022. (org-overview)
  13023. (org-remove-occur-highlights))
  13024. (while (let (case-fold-search)
  13025. (re-search-forward re nil t))
  13026. (setq org-map-continue-from nil)
  13027. (catch :skip
  13028. (setq todo
  13029. ;; TODO: is the 1-2 difference a bug?
  13030. (when (match-end 1) (org-match-string-no-properties 2))
  13031. tags (when (match-end 4) (org-match-string-no-properties 4)))
  13032. (goto-char (setq lspos (match-beginning 0)))
  13033. (setq level (org-reduced-level (org-outline-level))
  13034. category (org-get-category))
  13035. (when (eq action 'agenda)
  13036. (setq ts-date-pair (org-agenda-entry-get-agenda-timestamp (point))
  13037. ts-date (car ts-date-pair)
  13038. ts-date-type (cdr ts-date-pair)))
  13039. (setq i llast llast level)
  13040. ;; remove tag lists from same and sublevels
  13041. (while (>= i level)
  13042. (when (setq entry (assoc i tags-alist))
  13043. (setq tags-alist (delete entry tags-alist)))
  13044. (setq i (1- i)))
  13045. ;; add the next tags
  13046. (when tags
  13047. (setq tags (org-split-string tags ":")
  13048. tags-alist
  13049. (cons (cons level tags) tags-alist)))
  13050. ;; compile tags for current headline
  13051. (setq tags-list
  13052. (if org-use-tag-inheritance
  13053. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  13054. tags)
  13055. org-scanner-tags tags-list)
  13056. (when org-use-tag-inheritance
  13057. (setcdr (car tags-alist)
  13058. (mapcar (lambda (x)
  13059. (setq x (copy-sequence x))
  13060. (org-add-prop-inherited x))
  13061. (cdar tags-alist))))
  13062. (when (and tags org-use-tag-inheritance
  13063. (or (not (eq t org-use-tag-inheritance))
  13064. org-tags-exclude-from-inheritance))
  13065. ;; Selective inheritance, remove uninherited ones.
  13066. (setcdr (car tags-alist)
  13067. (org-remove-uninherited-tags (cdar tags-alist))))
  13068. (when (and
  13069. ;; eval matcher only when the todo condition is OK
  13070. (and (or (not todo-only) (member todo org-not-done-keywords))
  13071. (if (functionp matcher)
  13072. (let ((case-fold-search t) (org-trust-scanner-tags t))
  13073. (funcall matcher todo tags-list level))
  13074. matcher))
  13075. ;; Call the skipper, but return t if it does not
  13076. ;; skip, so that the `and' form continues evaluating.
  13077. (progn
  13078. (unless (eq action 'sparse-tree) (org-agenda-skip))
  13079. t)
  13080. ;; Check if timestamps are deselecting this entry
  13081. (or (not todo-only)
  13082. (and (member todo org-not-done-keywords)
  13083. (or (not org-agenda-tags-todo-honor-ignore-options)
  13084. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item))))))
  13085. ;; select this headline
  13086. (cond
  13087. ((eq action 'sparse-tree)
  13088. (and org-highlight-sparse-tree-matches
  13089. (org-get-heading) (match-end 0)
  13090. (org-highlight-new-match
  13091. (match-beginning 1) (match-end 1)))
  13092. (org-show-context 'tags-tree))
  13093. ((eq action 'agenda)
  13094. (setq txt (org-agenda-format-item
  13095. ""
  13096. (concat
  13097. (if (eq org-tags-match-list-sublevels 'indented)
  13098. (make-string (1- level) ?.) "")
  13099. (org-get-heading))
  13100. (make-string level ?\s)
  13101. category
  13102. tags-list)
  13103. priority (org-get-priority txt))
  13104. (goto-char lspos)
  13105. (setq marker (org-agenda-new-marker))
  13106. (org-add-props txt props
  13107. 'org-marker marker 'org-hd-marker marker 'org-category category
  13108. 'todo-state todo
  13109. 'ts-date ts-date
  13110. 'priority priority
  13111. 'type (concat "tagsmatch" ts-date-type))
  13112. (push txt rtn))
  13113. ((functionp action)
  13114. (setq org-map-continue-from nil)
  13115. (save-excursion
  13116. (setq rtn1 (funcall action))
  13117. (push rtn1 rtn)))
  13118. (t (user-error "Invalid action")))
  13119. ;; if we are to skip sublevels, jump to end of subtree
  13120. (unless org-tags-match-list-sublevels
  13121. (org-end-of-subtree t)
  13122. (backward-char 1))))
  13123. ;; Get the correct position from where to continue
  13124. (if org-map-continue-from
  13125. (goto-char org-map-continue-from)
  13126. (and (= (point) lspos) (end-of-line 1)))))
  13127. (when (and (eq action 'sparse-tree)
  13128. (not org-sparse-tree-open-archived-trees))
  13129. (org-hide-archived-subtrees (point-min) (point-max)))
  13130. (nreverse rtn)))
  13131. (defun org-remove-uninherited-tags (tags)
  13132. "Remove all tags that are not inherited from the list TAGS."
  13133. (cond
  13134. ((eq org-use-tag-inheritance t)
  13135. (if org-tags-exclude-from-inheritance
  13136. (org-delete-all org-tags-exclude-from-inheritance tags)
  13137. tags))
  13138. ((not org-use-tag-inheritance) nil)
  13139. ((stringp org-use-tag-inheritance)
  13140. (delq nil (mapcar
  13141. (lambda (x)
  13142. (if (and (string-match org-use-tag-inheritance x)
  13143. (not (member x org-tags-exclude-from-inheritance)))
  13144. x nil))
  13145. tags)))
  13146. ((listp org-use-tag-inheritance)
  13147. (delq nil (mapcar
  13148. (lambda (x)
  13149. (if (member x org-use-tag-inheritance) x nil))
  13150. tags)))))
  13151. (defun org-match-sparse-tree (&optional todo-only match)
  13152. "Create a sparse tree according to tags string MATCH.
  13153. MATCH can contain positive and negative selection of tags, like
  13154. \"+WORK+URGENT-WITHBOSS\".
  13155. If optional argument TODO-ONLY is non-nil, only select lines that are
  13156. also TODO lines."
  13157. (interactive "P")
  13158. (org-agenda-prepare-buffers (list (current-buffer)))
  13159. (let ((org--matcher-tags-todo-only todo-only))
  13160. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match))
  13161. org--matcher-tags-todo-only)))
  13162. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  13163. (defvar org-cached-props nil)
  13164. (defun org-cached-entry-get (pom property)
  13165. (if (or (eq t org-use-property-inheritance)
  13166. (and (stringp org-use-property-inheritance)
  13167. (let ((case-fold-search t))
  13168. (org-string-match-p org-use-property-inheritance property)))
  13169. (and (listp org-use-property-inheritance)
  13170. (member-ignore-case property org-use-property-inheritance)))
  13171. ;; Caching is not possible, check it directly.
  13172. (org-entry-get pom property 'inherit)
  13173. ;; Get all properties, so we can do complicated checks easily.
  13174. (cdr (assoc-string property
  13175. (or org-cached-props
  13176. (setq org-cached-props (org-entry-properties pom)))
  13177. t))))
  13178. (defun org-global-tags-completion-table (&optional files)
  13179. "Return the list of all tags in all agenda buffer/files.
  13180. Optional FILES argument is a list of files which can be used
  13181. instead of the agenda files."
  13182. (save-excursion
  13183. (org-uniquify
  13184. (delq nil
  13185. (apply #'append
  13186. (mapcar
  13187. (lambda (file)
  13188. (set-buffer (find-file-noselect file))
  13189. (mapcar (lambda (x)
  13190. (and (stringp (car-safe x))
  13191. (list (car-safe x))))
  13192. (or org-current-tag-alist (org-get-buffer-tags))))
  13193. (if (car-safe files) files
  13194. (org-agenda-files))))))))
  13195. (defun org-make-tags-matcher (match)
  13196. "Create the TAGS/TODO matcher form for the selection string MATCH.
  13197. Returns a cons of the selection string MATCH and a function
  13198. implementing the matcher.
  13199. The matcher is to be called at an Org entry, with point on the
  13200. headline, and returns non-nil if the entry matches the selection
  13201. string MATCH. It must be called with three arguments: the TODO
  13202. keyword at the entry (or nil if none), the list of all tags at
  13203. the entry including inherited ones and the reduced level of the
  13204. headline. Additionally, the category of the entry, if any, must
  13205. be specified as the text property `org-category' on the headline.
  13206. This function sets the variable `org--matcher-tags-todo-only' to
  13207. a non-nil value if the matcher restricts matching to TODO
  13208. entries, otherwise it is not touched.
  13209. See also `org-scan-tags'."
  13210. (unless match
  13211. ;; Get a new match request, with completion against the global
  13212. ;; tags table and the local tags in current buffer.
  13213. (let ((org-last-tags-completion-table
  13214. (org-uniquify
  13215. (delq nil (append (org-get-buffer-tags)
  13216. (org-global-tags-completion-table))))))
  13217. (setq match
  13218. (completing-read
  13219. "Match: "
  13220. 'org-tags-completion-function nil nil nil 'org-tags-history))))
  13221. (let ((match0 match)
  13222. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\(\\(?:[[:alnum:]_]+\\(?:\\\\-\\)*\\)+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@#%]+\\)"))
  13223. (start 0)
  13224. tagsmatch todomatch tagsmatcher todomatcher)
  13225. ;; Expand group tags.
  13226. (setq match (org-tags-expand match))
  13227. ;; Check if there is a TODO part of this match, which would be the
  13228. ;; part after a "/". To make sure that this slash is not part of
  13229. ;; a property value to be matched against, we also check that
  13230. ;; there is no / after that slash. First, find the last slash.
  13231. (let ((s 0))
  13232. (while (string-match "/+" match s)
  13233. (setq start (match-beginning 0))
  13234. (setq s (match-end 0))))
  13235. (if (and (string-match "/+" match start)
  13236. (not (string-match-p "\"" match start)))
  13237. ;; Match contains also a TODO-matching request.
  13238. (progn
  13239. (setq tagsmatch (substring match 0 (match-beginning 0)))
  13240. (setq todomatch (substring match (match-end 0)))
  13241. (when (string-match "\\`!" todomatch)
  13242. (setq org--matcher-tags-todo-only t)
  13243. (setq todomatch (substring todomatch 1)))
  13244. (when (string-match "\\`\\s-*\\'" todomatch)
  13245. (setq todomatch nil)))
  13246. ;; Only matching tags.
  13247. (setq tagsmatch match)
  13248. (setq todomatch nil))
  13249. ;; Make the tags matcher.
  13250. (when (org-string-nw-p tagsmatch)
  13251. (let ((orlist nil)
  13252. (orterms (org-split-string tagsmatch "|"))
  13253. term)
  13254. (while (setq term (pop orterms))
  13255. (while (and (equal (substring term -1) "\\") orterms)
  13256. (setq term (concat term "|" (pop orterms)))) ;repair bad split.
  13257. (while (string-match re term)
  13258. (let* ((rest (substring term (match-end 0)))
  13259. (minus (and (match-end 1)
  13260. (equal (match-string 1 term) "-")))
  13261. (tag (save-match-data
  13262. (replace-regexp-in-string
  13263. "\\\\-" "-" (match-string 2 term))))
  13264. (regexp (eq (string-to-char tag) ?{))
  13265. (levelp (match-end 4))
  13266. (propp (match-end 5))
  13267. (mm
  13268. (cond
  13269. (regexp `(org-match-any-p ,(substring tag 1 -1) tags-list))
  13270. (levelp
  13271. `(,(org-op-to-function (match-string 3 term))
  13272. level
  13273. ,(string-to-number (match-string 4 term))))
  13274. (propp
  13275. (let* ((gv (pcase (upcase (match-string 5 term))
  13276. ("CATEGORY"
  13277. '(get-text-property (point) 'org-category))
  13278. ("TODO" 'todo)
  13279. (p `(org-cached-entry-get nil ,p))))
  13280. (pv (match-string 7 term))
  13281. (regexp (eq (string-to-char pv) ?{))
  13282. (strp (eq (string-to-char pv) ?\"))
  13283. (timep (string-match-p "^\"[[<].*[]>]\"$" pv))
  13284. (po (org-op-to-function (match-string 6 term)
  13285. (if timep 'time strp))))
  13286. (setq pv (if (or regexp strp) (substring pv 1 -1) pv))
  13287. (when timep (setq pv (org-matcher-time pv)))
  13288. (cond ((and regexp (eq po 'org<>))
  13289. `(not (string-match ,pv (or ,gv ""))))
  13290. (regexp `(string-match ,pv (or ,gv "")))
  13291. (strp `(,po (or ,gv "") ,pv))
  13292. (t
  13293. `(,po
  13294. (string-to-number (or ,gv ""))
  13295. ,(string-to-number pv))))))
  13296. (t `(member ,tag tags-list)))))
  13297. (push (if minus `(not ,mm) mm) tagsmatcher)
  13298. (setq term rest)))
  13299. (push `(and ,@tagsmatcher) orlist)
  13300. (setq tagsmatcher nil))
  13301. (setq tagsmatcher `(progn (setq org-cached-props nil) (or ,@orlist)))))
  13302. ;; Make the TODO matcher.
  13303. (when (org-string-nw-p todomatch)
  13304. (let ((orlist nil))
  13305. (dolist (term (org-split-string todomatch "|"))
  13306. (while (string-match re term)
  13307. (let* ((minus (and (match-end 1)
  13308. (equal (match-string 1 term) "-")))
  13309. (kwd (match-string 2 term))
  13310. (regexp (eq (string-to-char kwd) ?{))
  13311. (mm (if regexp `(string-match ,(substring kwd 1 -1) todo)
  13312. `(equal todo ,kwd))))
  13313. (push (if minus `(not ,mm) mm) todomatcher))
  13314. (setq term (substring term (match-end 0))))
  13315. (push (if (> (length todomatcher) 1)
  13316. (cons 'and todomatcher)
  13317. (car todomatcher))
  13318. orlist)
  13319. (setq todomatcher nil))
  13320. (setq todomatcher (cons 'or orlist))))
  13321. ;; Return the string and function of the matcher. If no
  13322. ;; tags-specific or todo-specific matcher exists, match
  13323. ;; everything.
  13324. (let ((matcher (if (and tagsmatcher todomatcher)
  13325. `(and ,tagsmatcher ,todomatcher)
  13326. (or tagsmatcher todomatcher t))))
  13327. (when org--matcher-tags-todo-only
  13328. (setq matcher `(and (member todo org-not-done-keywords) ,matcher)))
  13329. (cons match0 `(lambda (todo tags-list level) ,matcher)))))
  13330. (defun org-tags-expand (match &optional single-as-list downcased tags-already-expanded)
  13331. "Expand group tags in MATCH.
  13332. This replaces every group tag in MATCH with a regexp tag search.
  13333. For example, a group tag \"Work\" defined as { Work : Lab Conf }
  13334. will be replaced like this:
  13335. Work => {\\<\\(?:Work\\|Lab\\|Conf\\)\\>}
  13336. +Work => +{\\<\\(?:Work\\|Lab\\|Conf\\)\\>}
  13337. -Work => -{\\<\\(?:Work\\|Lab\\|Conf\\)\\>}
  13338. Replacing by a regexp preserves the structure of the match.
  13339. E.g., this expansion
  13340. Work|Home => {\\(?:Work\\|Lab\\|Conf\\}|Home
  13341. will match anything tagged with \"Lab\" and \"Home\", or tagged
  13342. with \"Conf\" and \"Home\" or tagged with \"Work\" and \"home\".
  13343. A group tag in MATCH can contain regular expressions of its own.
  13344. For example, a group tag \"Proj\" defined as { Proj : {P@.+} }
  13345. will be replaced like this:
  13346. Proj => {\\<\\(?:Proj\\)\\>\\|P@.+}
  13347. When the optional argument SINGLE-AS-LIST is non-nil, MATCH is
  13348. assumed to be a single group tag, and the function will return
  13349. the list of tags in this group.
  13350. When DOWNCASE is non-nil, expand downcased TAGS."
  13351. (if org-group-tags
  13352. (let* ((case-fold-search t)
  13353. (stable org-mode-syntax-table)
  13354. (taggroups (or org-tag-groups-alist-for-agenda org-tag-groups-alist))
  13355. (taggroups (if downcased
  13356. (mapcar (lambda (tg) (mapcar #'downcase tg))
  13357. taggroups)
  13358. taggroups))
  13359. (taggroups-keys (mapcar #'car taggroups))
  13360. (return-match (if downcased (downcase match) match))
  13361. (count 0)
  13362. (work-already-expanded tags-already-expanded)
  13363. regexps-in-match tags-in-group regexp-in-group regexp-in-group-escaped)
  13364. ;; @ and _ are allowed as word-components in tags.
  13365. (modify-syntax-entry ?@ "w" stable)
  13366. (modify-syntax-entry ?_ "w" stable)
  13367. ;; Temporarily replace regexp-expressions in the match-expression.
  13368. (while (string-match "{.+?}" return-match)
  13369. (cl-incf count)
  13370. (push (match-string 0 return-match) regexps-in-match)
  13371. (setq return-match (replace-match (format "<%d>" count) t nil return-match)))
  13372. (while (and taggroups-keys
  13373. (with-syntax-table stable
  13374. (string-match
  13375. (concat "\\(?1:[+-]?\\)\\(?2:\\<"
  13376. (regexp-opt taggroups-keys) "\\>\\)")
  13377. return-match)))
  13378. (let* ((dir (match-string 1 return-match))
  13379. (tag (match-string 2 return-match))
  13380. (tag (if downcased (downcase tag) tag)))
  13381. (unless (or (get-text-property 0 'grouptag (match-string 2 return-match))
  13382. (member tag work-already-expanded))
  13383. (setq tags-in-group (assoc tag taggroups))
  13384. (push tag work-already-expanded)
  13385. ;; Recursively expand each tag in the group, if the tag hasn't
  13386. ;; already been expanded. Restore the match-data after all recursive calls.
  13387. (save-match-data
  13388. (let (tags-expanded)
  13389. (dolist (x (cdr tags-in-group))
  13390. (if (and (member x taggroups-keys)
  13391. (not (member x work-already-expanded)))
  13392. (setq tags-expanded
  13393. (delete-dups
  13394. (append
  13395. (org-tags-expand x t downcased
  13396. work-already-expanded)
  13397. tags-expanded)))
  13398. (setq tags-expanded
  13399. (append (list x) tags-expanded)))
  13400. (setq work-already-expanded
  13401. (delete-dups
  13402. (append tags-expanded
  13403. work-already-expanded))))
  13404. (setq tags-in-group
  13405. (delete-dups (cons (car tags-in-group)
  13406. tags-expanded)))))
  13407. ;; Filter tag-regexps from tags.
  13408. (setq regexp-in-group-escaped
  13409. (delq nil (mapcar (lambda (x)
  13410. (if (stringp x)
  13411. (and (equal "{" (substring x 0 1))
  13412. (equal "}" (substring x -1))
  13413. x)
  13414. x))
  13415. tags-in-group))
  13416. regexp-in-group
  13417. (mapcar (lambda (x)
  13418. (substring x 1 -1))
  13419. regexp-in-group-escaped)
  13420. tags-in-group
  13421. (delq nil (mapcar (lambda (x)
  13422. (if (stringp x)
  13423. (and (not (equal "{" (substring x 0 1)))
  13424. (not (equal "}" (substring x -1)))
  13425. x)
  13426. x))
  13427. tags-in-group)))
  13428. ;; If single-as-list, do no more in the while-loop.
  13429. (if (not single-as-list)
  13430. (progn
  13431. (when regexp-in-group
  13432. (setq regexp-in-group
  13433. (concat "\\|"
  13434. (mapconcat 'identity regexp-in-group
  13435. "\\|"))))
  13436. (setq tags-in-group
  13437. (concat dir
  13438. "{\\<"
  13439. (regexp-opt tags-in-group)
  13440. "\\>"
  13441. regexp-in-group
  13442. "}"))
  13443. (when (stringp tags-in-group)
  13444. (org-add-props tags-in-group '(grouptag t)))
  13445. (setq return-match
  13446. (replace-match tags-in-group t t return-match)))
  13447. (setq tags-in-group
  13448. (append regexp-in-group-escaped tags-in-group))))
  13449. (setq taggroups-keys (delete tag taggroups-keys))))
  13450. ;; Add the regular expressions back into the match-expression again.
  13451. (while regexps-in-match
  13452. (setq return-match (replace-regexp-in-string (format "<%d>" count)
  13453. (pop regexps-in-match)
  13454. return-match t t))
  13455. (cl-decf count))
  13456. (if single-as-list
  13457. (if tags-in-group tags-in-group (list return-match))
  13458. return-match))
  13459. (if single-as-list
  13460. (list (if downcased (downcase match) match))
  13461. match)))
  13462. (defun org-op-to-function (op &optional stringp)
  13463. "Turn an operator into the appropriate function."
  13464. (setq op
  13465. (cond
  13466. ((equal op "<" ) '(< string< org-time<))
  13467. ((equal op ">" ) '(> org-string> org-time>))
  13468. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  13469. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  13470. ((member op '("=" "==")) '(= string= org-time=))
  13471. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  13472. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  13473. (defun org<> (a b) (not (= a b)))
  13474. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  13475. (defun org-string>= (a b) (not (string< a b)))
  13476. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  13477. (defun org-string<> (a b) (not (string= a b)))
  13478. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  13479. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  13480. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  13481. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  13482. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  13483. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  13484. (defun org-2ft (s)
  13485. "Convert S to a floating point time.
  13486. If S is already a number, just return it. If it is a string, parse
  13487. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  13488. (cond
  13489. ((numberp s) s)
  13490. ((stringp s)
  13491. (condition-case nil
  13492. (float-time (apply 'encode-time (org-parse-time-string s)))
  13493. (error 0.)))
  13494. (t 0.)))
  13495. (defun org-time-today ()
  13496. "Time in seconds today at 0:00.
  13497. Returns the float number of seconds since the beginning of the
  13498. epoch to the beginning of today (00:00)."
  13499. (float-time (apply 'encode-time
  13500. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  13501. (defun org-matcher-time (s)
  13502. "Interpret a time comparison value."
  13503. (save-match-data
  13504. (cond
  13505. ((string= s "<now>") (float-time))
  13506. ((string= s "<today>") (org-time-today))
  13507. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  13508. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  13509. ((string-match "^<\\([-+][0-9]+\\)\\([hdwmy]\\)>$" s)
  13510. (+ (org-time-today)
  13511. (* (string-to-number (match-string 1 s))
  13512. (cdr (assoc (match-string 2 s)
  13513. '(("d" . 86400.0) ("w" . 604800.0)
  13514. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  13515. (t (org-2ft s)))))
  13516. (defun org-match-any-p (re list)
  13517. "Does re match any element of list?"
  13518. (setq list (mapcar (lambda (x) (string-match re x)) list))
  13519. (delq nil list))
  13520. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  13521. (defvar org-tags-overlay (make-overlay 1 1))
  13522. (org-detach-overlay org-tags-overlay)
  13523. (defun org-get-local-tags-at (&optional pos)
  13524. "Get a list of tags defined in the current headline."
  13525. (org-get-tags-at pos 'local))
  13526. (defun org-get-local-tags ()
  13527. "Get a list of tags defined in the current headline."
  13528. (org-get-tags-at nil 'local))
  13529. (defun org-get-tags-at (&optional pos local)
  13530. "Get a list of all headline tags applicable at POS.
  13531. POS defaults to point. If tags are inherited, the list contains
  13532. the targets in the same sequence as the headlines appear, i.e.
  13533. the tags of the current headline come last.
  13534. When LOCAL is non-nil, only return tags from the current headline,
  13535. ignore inherited ones."
  13536. (interactive)
  13537. (if (and org-trust-scanner-tags
  13538. (or (not pos) (equal pos (point)))
  13539. (not local))
  13540. org-scanner-tags
  13541. (let (tags ltags lastpos parent)
  13542. (save-excursion
  13543. (save-restriction
  13544. (widen)
  13545. (goto-char (or pos (point)))
  13546. (save-match-data
  13547. (catch 'done
  13548. (condition-case nil
  13549. (progn
  13550. (org-back-to-heading t)
  13551. (while (not (equal lastpos (point)))
  13552. (setq lastpos (point))
  13553. (when (looking-at
  13554. (org-re "[^\r\n]+?:\\([[:alnum:]_@#%:]+\\):[ \t]*$"))
  13555. (setq ltags (org-split-string
  13556. (org-match-string-no-properties 1) ":"))
  13557. (when parent
  13558. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  13559. (setq tags (append
  13560. (if parent
  13561. (org-remove-uninherited-tags ltags)
  13562. ltags)
  13563. tags)))
  13564. (or org-use-tag-inheritance (throw 'done t))
  13565. (when local (throw 'done t))
  13566. (or (org-up-heading-safe) (error nil))
  13567. (setq parent t)))
  13568. (error nil)))))
  13569. (if local
  13570. tags
  13571. (reverse (delete-dups
  13572. (reverse (append
  13573. (org-remove-uninherited-tags
  13574. org-file-tags)
  13575. tags)))))))))
  13576. (defun org-add-prop-inherited (s)
  13577. (add-text-properties 0 (length s) '(inherited t) s)
  13578. s)
  13579. (defun org-toggle-tag (tag &optional onoff)
  13580. "Toggle the tag TAG for the current line.
  13581. If ONOFF is `on' or `off', don't toggle but set to this state."
  13582. (let (res current)
  13583. (save-excursion
  13584. (org-back-to-heading t)
  13585. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t]*$")
  13586. (point-at-eol) t)
  13587. (progn
  13588. (setq current (match-string 1))
  13589. (replace-match ""))
  13590. (setq current ""))
  13591. (setq current (nreverse (org-split-string current ":")))
  13592. (cond
  13593. ((eq onoff 'on)
  13594. (setq res t)
  13595. (or (member tag current) (push tag current)))
  13596. ((eq onoff 'off)
  13597. (or (not (member tag current)) (setq current (delete tag current))))
  13598. (t (if (member tag current)
  13599. (setq current (delete tag current))
  13600. (setq res t)
  13601. (push tag current))))
  13602. (end-of-line 1)
  13603. (if current
  13604. (progn
  13605. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  13606. (org-set-tags nil t))
  13607. (delete-horizontal-space))
  13608. (run-hooks 'org-after-tags-change-hook))
  13609. res))
  13610. (defun org-align-tags-here (to-col)
  13611. ;; Assumes that this is a headline
  13612. "Align tags on the current headline to TO-COL."
  13613. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  13614. (beginning-of-line 1)
  13615. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13616. (< pos (match-beginning 2)))
  13617. (progn
  13618. (setq tags-l (- (match-end 2) (match-beginning 2)))
  13619. (goto-char (match-beginning 1))
  13620. (insert " ")
  13621. (delete-region (point) (1+ (match-beginning 2)))
  13622. (setq ncol (max (current-column)
  13623. (1+ col)
  13624. (if (> to-col 0)
  13625. to-col
  13626. (- (abs to-col) tags-l))))
  13627. (setq p (point))
  13628. (insert (make-string (- ncol (current-column)) ?\ ))
  13629. (setq ncol (current-column))
  13630. (when indent-tabs-mode (tabify p (point-at-eol)))
  13631. (org-move-to-column (min ncol col)))
  13632. (goto-char pos))))
  13633. (defun org-set-tags-command (&optional arg just-align)
  13634. "Call the set-tags command for the current entry."
  13635. (interactive "P")
  13636. (if (or (org-at-heading-p) (and arg (org-before-first-heading-p)))
  13637. (org-set-tags arg just-align)
  13638. (save-excursion
  13639. (unless (and (org-region-active-p)
  13640. org-loop-over-headlines-in-active-region)
  13641. (org-back-to-heading t))
  13642. (org-set-tags arg just-align))))
  13643. (defun org-set-tags-to (data)
  13644. "Set the tags of the current entry to DATA, replacing the current tags.
  13645. DATA may be a tags string like :aa:bb:cc:, or a list of tags.
  13646. If DATA is nil or the empty string, any tags will be removed."
  13647. (interactive "sTags: ")
  13648. (setq data
  13649. (cond
  13650. ((eq data nil) "")
  13651. ((equal data "") "")
  13652. ((stringp data)
  13653. (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
  13654. ":"))
  13655. ((listp data)
  13656. (concat ":" (mapconcat 'identity data ":") ":"))))
  13657. (when data
  13658. (save-excursion
  13659. (org-back-to-heading t)
  13660. (when (looking-at org-complex-heading-regexp)
  13661. (if (match-end 5)
  13662. (progn
  13663. (goto-char (match-beginning 5))
  13664. (insert data)
  13665. (delete-region (point) (point-at-eol))
  13666. (org-set-tags nil 'align))
  13667. (goto-char (point-at-eol))
  13668. (insert " " data)
  13669. (org-set-tags nil 'align)))
  13670. (beginning-of-line 1)
  13671. (when (looking-at ".*?\\([ \t]+\\)$")
  13672. (delete-region (match-beginning 1) (match-end 1))))))
  13673. (defun org-align-all-tags ()
  13674. "Align the tags in all headings."
  13675. (interactive)
  13676. (save-excursion
  13677. (or (ignore-errors (org-back-to-heading t))
  13678. (outline-next-heading))
  13679. (if (org-at-heading-p)
  13680. (org-set-tags t)
  13681. (message "No headings"))))
  13682. (defvar org-indent-indentation-per-level)
  13683. (defun org-set-tags (&optional arg just-align)
  13684. "Set the tags for the current headline.
  13685. With prefix ARG, realign all tags in headings in the current buffer.
  13686. When JUST-ALIGN is non-nil, only align tags."
  13687. (interactive "P")
  13688. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  13689. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  13690. 'region-start-level
  13691. 'region))
  13692. org-loop-over-headlines-in-active-region)
  13693. (org-map-entries
  13694. ;; We don't use ARG and JUST-ALIGN here because these args
  13695. ;; are not useful when looping over headlines.
  13696. #'org-set-tags
  13697. org-loop-over-headlines-in-active-region
  13698. cl
  13699. '(when (outline-invisible-p) (org-end-of-subtree nil t))))
  13700. (let ((org-setting-tags t))
  13701. (if arg
  13702. (save-excursion
  13703. (goto-char (point-min))
  13704. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  13705. (while (re-search-forward org-outline-regexp-bol nil t)
  13706. (org-set-tags nil t)
  13707. (end-of-line)))
  13708. (message "All tags realigned to column %d" org-tags-column))
  13709. (let* ((current (org-get-tags-string))
  13710. (col (current-column))
  13711. (tags
  13712. (if just-align current
  13713. ;; Get a new set of tags from the user.
  13714. (save-excursion
  13715. (let* ((seen)
  13716. (table
  13717. (setq
  13718. org-last-tags-completion-table
  13719. ;; Uniquify tags in alists, yet preserve
  13720. ;; structure (i.e., keywords).
  13721. (delq nil
  13722. (mapcar
  13723. (lambda (pair)
  13724. (let ((head (car pair)))
  13725. (cond ((symbolp head) pair)
  13726. ((member head seen) nil)
  13727. (t (push head seen)
  13728. pair))))
  13729. (append
  13730. (or org-current-tag-alist
  13731. (org-get-buffer-tags))
  13732. (and
  13733. org-complete-tags-always-offer-all-agenda-tags
  13734. (org-global-tags-completion-table
  13735. (org-agenda-files))))))))
  13736. (current-tags (org-split-string current ":"))
  13737. (inherited-tags
  13738. (nreverse (nthcdr (length current-tags)
  13739. (nreverse (org-get-tags-at))))))
  13740. (replace-regexp-in-string
  13741. "\\([-+&]+\\|,\\)"
  13742. ":"
  13743. (if (or (eq t org-use-fast-tag-selection)
  13744. (and org-use-fast-tag-selection
  13745. (delq nil (mapcar #'cdr table))))
  13746. (org-fast-tag-selection
  13747. current-tags inherited-tags table
  13748. (and org-fast-tag-selection-include-todo
  13749. org-todo-key-alist))
  13750. (let ((org-add-colon-after-tag-completion
  13751. (< 1 (length table))))
  13752. (org-trim
  13753. (completing-read
  13754. "Tags: "
  13755. #'org-tags-completion-function
  13756. nil nil current 'org-tags-history))))))))))
  13757. (when org-tags-sort-function
  13758. (setq tags
  13759. (mapconcat
  13760. #'identity
  13761. (sort (org-split-string tags (org-re "[^[:alnum:]_@#%]+"))
  13762. org-tags-sort-function)
  13763. ":")))
  13764. (if (not (org-string-nw-p tags)) (setq tags "")
  13765. (unless (string-match ":\\'" tags) (setq tags (concat tags ":")))
  13766. (unless (string-match "\\`:" tags) (setq tags (concat ":" tags))))
  13767. ;; Insert new tags at the correct column
  13768. (beginning-of-line)
  13769. (let ((level (if (looking-at org-outline-regexp)
  13770. (- (match-end 0) (point) 1)
  13771. 1)))
  13772. (cond
  13773. ((and (equal current "") (equal tags "")))
  13774. ((re-search-forward
  13775. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  13776. (line-end-position)
  13777. t)
  13778. (if (equal tags "") (replace-match "" t t)
  13779. (goto-char (match-beginning 0))
  13780. (let* ((c0 (current-column))
  13781. ;; Compute offset for the case of org-indent-mode
  13782. ;; active.
  13783. (di (if (org-bound-and-true-p org-indent-mode)
  13784. (* (1- org-indent-indentation-per-level)
  13785. (1- level))
  13786. 0))
  13787. (p0 (if (eq (char-before) ?*) (1+ (point)) (point)))
  13788. (tc (+ org-tags-column
  13789. (if (> org-tags-column 0) (- di) di)))
  13790. (c1 (max (1+ c0)
  13791. (if (> tc 0) tc
  13792. (- (- tc) (string-width tags)))))
  13793. (rpl (concat (make-string (max 0 (- c1 c0)) ?\s) tags)))
  13794. (replace-match rpl t t)
  13795. (when (and (not (featurep 'xemacs)) indent-tabs-mode)
  13796. (tabify p0 (point))))))
  13797. (t (error "Tags alignment failed"))))
  13798. (org-move-to-column col))
  13799. (unless just-align (run-hooks 'org-after-tags-change-hook))))))
  13800. (defun org-change-tag-in-region (beg end tag off)
  13801. "Add or remove TAG for each entry in the region.
  13802. This works in the agenda, and also in an org-mode buffer."
  13803. (interactive
  13804. (list (region-beginning) (region-end)
  13805. (let ((org-last-tags-completion-table
  13806. (if (derived-mode-p 'org-mode)
  13807. (org-uniquify
  13808. (delq nil (append (org-get-buffer-tags)
  13809. (org-global-tags-completion-table))))
  13810. (org-global-tags-completion-table))))
  13811. (completing-read
  13812. "Tag: " 'org-tags-completion-function nil nil nil
  13813. 'org-tags-history))
  13814. (progn
  13815. (message "[s]et or [r]emove? ")
  13816. (equal (read-char-exclusive) ?r))))
  13817. (when (fboundp 'deactivate-mark) (deactivate-mark))
  13818. (let ((agendap (equal major-mode 'org-agenda-mode))
  13819. l1 l2 m buf pos newhead (cnt 0))
  13820. (goto-char end)
  13821. (setq l2 (1- (org-current-line)))
  13822. (goto-char beg)
  13823. (setq l1 (org-current-line))
  13824. (cl-loop for l from l1 to l2 do
  13825. (org-goto-line l)
  13826. (setq m (get-text-property (point) 'org-hd-marker))
  13827. (when (or (and (derived-mode-p 'org-mode) (org-at-heading-p))
  13828. (and agendap m))
  13829. (setq buf (if agendap (marker-buffer m) (current-buffer))
  13830. pos (if agendap m (point)))
  13831. (with-current-buffer buf
  13832. (save-excursion
  13833. (save-restriction
  13834. (goto-char pos)
  13835. (setq cnt (1+ cnt))
  13836. (org-toggle-tag tag (if off 'off 'on))
  13837. (setq newhead (org-get-heading)))))
  13838. (and agendap (org-agenda-change-all-lines newhead m))))
  13839. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  13840. (defun org-tags-completion-function (string _predicate &optional flag)
  13841. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  13842. (confirm (lambda (x) (stringp (car x)))))
  13843. (if (string-match "^\\(.*[-+:&,|]\\)\\([^-+:&,|]*\\)$" string)
  13844. (setq s1 (match-string 1 string)
  13845. s2 (match-string 2 string))
  13846. (setq s1 "" s2 string))
  13847. (cond
  13848. ((eq flag nil)
  13849. ;; try completion
  13850. (setq rtn (try-completion s2 ctable confirm))
  13851. (when (stringp rtn)
  13852. (setq rtn
  13853. (concat s1 s2 (substring rtn (length s2))
  13854. (if (and org-add-colon-after-tag-completion
  13855. (assoc rtn ctable))
  13856. ":" ""))))
  13857. rtn)
  13858. ((eq flag t)
  13859. ;; all-completions
  13860. (all-completions s2 ctable confirm))
  13861. ((eq flag 'lambda)
  13862. ;; exact match?
  13863. (assoc s2 ctable)))))
  13864. (defun org-fast-tag-insert (kwd tags face &optional end)
  13865. "Insert KDW, and the TAGS, the latter with face FACE.
  13866. Also insert END."
  13867. (insert (format "%-12s" (concat kwd ":"))
  13868. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  13869. (or end "")))
  13870. (defun org-fast-tag-show-exit (flag)
  13871. (save-excursion
  13872. (org-goto-line 3)
  13873. (when (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  13874. (replace-match ""))
  13875. (when flag
  13876. (end-of-line 1)
  13877. (org-move-to-column (- (window-width) 19) t)
  13878. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  13879. (defun org-set-current-tags-overlay (current prefix)
  13880. "Add an overlay to CURRENT tag with PREFIX."
  13881. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  13882. (if (featurep 'xemacs)
  13883. (org-overlay-display org-tags-overlay (concat prefix s)
  13884. 'secondary-selection)
  13885. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  13886. (org-overlay-display org-tags-overlay (concat prefix s)))))
  13887. (defvar org-last-tag-selection-key nil)
  13888. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  13889. "Fast tag selection with single keys.
  13890. CURRENT is the current list of tags in the headline, INHERITED is the
  13891. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  13892. possibly with grouping information. TODO-TABLE is a similar table with
  13893. TODO keywords, should these have keys assigned to them.
  13894. If the keys are nil, a-z are automatically assigned.
  13895. Returns the new tags string, or nil to not change the current settings."
  13896. (let* ((fulltable (append table todo-table))
  13897. (maxlen (apply 'max (mapcar
  13898. (lambda (x)
  13899. (if (stringp (car x)) (string-width (car x)) 0))
  13900. fulltable)))
  13901. (buf (current-buffer))
  13902. (expert (eq org-fast-tag-selection-single-key 'expert))
  13903. (buffer-tags nil)
  13904. (fwidth (+ maxlen 3 1 3))
  13905. (ncol (/ (- (window-width) 4) fwidth))
  13906. (i-face 'org-done)
  13907. (c-face 'org-todo)
  13908. tg cnt e c char c1 c2 ntable tbl rtn
  13909. ov-start ov-end ov-prefix
  13910. (exit-after-next org-fast-tag-selection-single-key)
  13911. (done-keywords org-done-keywords)
  13912. groups ingroup intaggroup)
  13913. (save-excursion
  13914. (beginning-of-line 1)
  13915. (if (looking-at
  13916. (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13917. (setq ov-start (match-beginning 1)
  13918. ov-end (match-end 1)
  13919. ov-prefix "")
  13920. (setq ov-start (1- (point-at-eol))
  13921. ov-end (1+ ov-start))
  13922. (skip-chars-forward "^\n\r")
  13923. (setq ov-prefix
  13924. (concat
  13925. (buffer-substring (1- (point)) (point))
  13926. (if (> (current-column) org-tags-column)
  13927. " "
  13928. (make-string (- org-tags-column (current-column)) ?\ ))))))
  13929. (move-overlay org-tags-overlay ov-start ov-end)
  13930. (save-window-excursion
  13931. (if expert
  13932. (set-buffer (get-buffer-create " *Org tags*"))
  13933. (delete-other-windows)
  13934. (set-window-buffer (split-window-vertically) (get-buffer-create " *Org tags*"))
  13935. (org-switch-to-buffer-other-window " *Org tags*"))
  13936. (erase-buffer)
  13937. (setq-local org-done-keywords done-keywords)
  13938. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  13939. (org-fast-tag-insert "Current" current c-face "\n\n")
  13940. (org-fast-tag-show-exit exit-after-next)
  13941. (org-set-current-tags-overlay current ov-prefix)
  13942. (setq tbl fulltable char ?a cnt 0)
  13943. (while (setq e (pop tbl))
  13944. (cond
  13945. ((eq (car e) :startgroup)
  13946. (push '() groups) (setq ingroup t)
  13947. (unless (zerop cnt)
  13948. (setq cnt 0)
  13949. (insert "\n"))
  13950. (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
  13951. ((eq (car e) :endgroup)
  13952. (setq ingroup nil cnt 0)
  13953. (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
  13954. ((eq (car e) :startgrouptag)
  13955. (setq intaggroup t)
  13956. (unless (zerop cnt)
  13957. (setq cnt 0)
  13958. (insert "\n"))
  13959. (insert "[ "))
  13960. ((eq (car e) :endgrouptag)
  13961. (setq intaggroup nil cnt 0)
  13962. (insert "]\n"))
  13963. ((equal e '(:newline))
  13964. (unless (zerop cnt)
  13965. (setq cnt 0)
  13966. (insert "\n")
  13967. (setq e (car tbl))
  13968. (while (equal (car tbl) '(:newline))
  13969. (insert "\n")
  13970. (setq tbl (cdr tbl)))))
  13971. ((equal e '(:grouptags)) (insert " : "))
  13972. (t
  13973. (setq tg (copy-sequence (car e)) c2 nil)
  13974. (if (cdr e)
  13975. (setq c (cdr e))
  13976. ;; automatically assign a character.
  13977. (setq c1 (string-to-char
  13978. (downcase (substring
  13979. tg (if (= (string-to-char tg) ?@) 1 0)))))
  13980. (if (or (rassoc c1 ntable) (rassoc c1 table))
  13981. (while (or (rassoc char ntable) (rassoc char table))
  13982. (setq char (1+ char)))
  13983. (setq c2 c1))
  13984. (setq c (or c2 char)))
  13985. (when ingroup (push tg (car groups)))
  13986. (setq tg (org-add-props tg nil 'face
  13987. (cond
  13988. ((not (assoc tg table))
  13989. (org-get-todo-face tg))
  13990. ((member tg current) c-face)
  13991. ((member tg inherited) i-face))))
  13992. (when (equal (caar tbl) :grouptags)
  13993. (org-add-props tg nil 'face 'org-tag-group))
  13994. (when (and (zerop cnt) (not ingroup) (not intaggroup)) (insert " "))
  13995. (insert "[" c "] " tg (make-string
  13996. (- fwidth 4 (length tg)) ?\ ))
  13997. (push (cons tg c) ntable)
  13998. (when (= (cl-incf cnt) ncol)
  13999. (insert "\n")
  14000. (when (or ingroup intaggroup) (insert " "))
  14001. (setq cnt 0)))))
  14002. (setq ntable (nreverse ntable))
  14003. (insert "\n")
  14004. (goto-char (point-min))
  14005. (unless expert (org-fit-window-to-buffer))
  14006. (setq rtn
  14007. (catch 'exit
  14008. (while t
  14009. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
  14010. (if (not groups) "no " "")
  14011. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  14012. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  14013. (setq org-last-tag-selection-key c)
  14014. (cond
  14015. ((= c ?\r) (throw 'exit t))
  14016. ((= c ?!)
  14017. (setq groups (not groups))
  14018. (goto-char (point-min))
  14019. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  14020. ((= c ?\C-c)
  14021. (if (not expert)
  14022. (org-fast-tag-show-exit
  14023. (setq exit-after-next (not exit-after-next)))
  14024. (setq expert nil)
  14025. (delete-other-windows)
  14026. (set-window-buffer (split-window-vertically) " *Org tags*")
  14027. (org-switch-to-buffer-other-window " *Org tags*")
  14028. (org-fit-window-to-buffer)))
  14029. ((or (= c ?\C-g)
  14030. (and (= c ?q) (not (rassoc c ntable))))
  14031. (org-detach-overlay org-tags-overlay)
  14032. (setq quit-flag t))
  14033. ((= c ?\ )
  14034. (setq current nil)
  14035. (when exit-after-next (setq exit-after-next 'now)))
  14036. ((= c ?\t)
  14037. (condition-case nil
  14038. (setq tg (completing-read
  14039. "Tag: "
  14040. (or buffer-tags
  14041. (with-current-buffer buf
  14042. (setq buffer-tags
  14043. (org-get-buffer-tags))))))
  14044. (quit (setq tg "")))
  14045. (when (string-match "\\S-" tg)
  14046. (cl-pushnew (list tg) buffer-tags :test #'equal)
  14047. (if (member tg current)
  14048. (setq current (delete tg current))
  14049. (push tg current)))
  14050. (when exit-after-next (setq exit-after-next 'now)))
  14051. ((setq e (rassoc c todo-table) tg (car e))
  14052. (with-current-buffer buf
  14053. (save-excursion (org-todo tg)))
  14054. (when exit-after-next (setq exit-after-next 'now)))
  14055. ((setq e (rassoc c ntable) tg (car e))
  14056. (if (member tg current)
  14057. (setq current (delete tg current))
  14058. (cl-loop for g in groups do
  14059. (when (member tg g)
  14060. (dolist (x g) (setq current (delete x current)))))
  14061. (push tg current))
  14062. (when exit-after-next (setq exit-after-next 'now))))
  14063. ;; Create a sorted list
  14064. (setq current
  14065. (sort current
  14066. (lambda (a b)
  14067. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  14068. (when (eq exit-after-next 'now) (throw 'exit t))
  14069. (goto-char (point-min))
  14070. (beginning-of-line 2)
  14071. (delete-region (point) (point-at-eol))
  14072. (org-fast-tag-insert "Current" current c-face)
  14073. (org-set-current-tags-overlay current ov-prefix)
  14074. (while (re-search-forward
  14075. (org-re "\\[.\\] \\([[:alnum:]_@#%]+\\)") nil t)
  14076. (setq tg (match-string 1))
  14077. (add-text-properties
  14078. (match-beginning 1) (match-end 1)
  14079. (list 'face
  14080. (cond
  14081. ((member tg current) c-face)
  14082. ((member tg inherited) i-face)
  14083. (t (get-text-property (match-beginning 1) 'face))))))
  14084. (goto-char (point-min)))))
  14085. (org-detach-overlay org-tags-overlay)
  14086. (if rtn
  14087. (mapconcat 'identity current ":")
  14088. nil))))
  14089. (defun org-get-tags-string ()
  14090. "Get the TAGS string in the current headline."
  14091. (unless (org-at-heading-p t)
  14092. (user-error "Not on a heading"))
  14093. (save-excursion
  14094. (beginning-of-line 1)
  14095. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  14096. (org-match-string-no-properties 1)
  14097. "")))
  14098. (defun org-get-tags ()
  14099. "Get the list of tags specified in the current headline."
  14100. (org-split-string (org-get-tags-string) ":"))
  14101. (defun org-get-buffer-tags ()
  14102. "Get a table of all tags used in the buffer, for completion."
  14103. (org-with-wide-buffer
  14104. (goto-char (point-min))
  14105. (let ((tag-re (concat org-outline-regexp-bol
  14106. "\\(?:.*?[ \t]\\)?"
  14107. (org-re ":\\([[:alnum:]_@#%:]+\\):[ \t]*$")))
  14108. tags)
  14109. (while (re-search-forward tag-re nil t)
  14110. (dolist (tag (org-split-string (org-match-string-no-properties 1) ":"))
  14111. (push tag tags)))
  14112. (mapcar #'list (append org-file-tags (org-uniquify tags))))))
  14113. ;;;; The mapping API
  14114. (defvar org-agenda-skip-comment-trees)
  14115. (defvar org-agenda-skip-function)
  14116. (defun org-map-entries (func &optional match scope &rest skip)
  14117. "Call FUNC at each headline selected by MATCH in SCOPE.
  14118. FUNC is a function or a lisp form. The function will be called without
  14119. arguments, with the cursor positioned at the beginning of the headline.
  14120. The return values of all calls to the function will be collected and
  14121. returned as a list.
  14122. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  14123. does not need to preserve point. After evaluation, the cursor will be
  14124. moved to the end of the line (presumably of the headline of the
  14125. processed entry) and search continues from there. Under some
  14126. circumstances, this may not produce the wanted results. For example,
  14127. if you have removed (e.g. archived) the current (sub)tree it could
  14128. mean that the next entry will be skipped entirely. In such cases, you
  14129. can specify the position from where search should continue by making
  14130. FUNC set the variable `org-map-continue-from' to the desired buffer
  14131. position.
  14132. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  14133. Only headlines that are matched by this query will be considered during
  14134. the iteration. When MATCH is nil or t, all headlines will be
  14135. visited by the iteration.
  14136. SCOPE determines the scope of this command. It can be any of:
  14137. nil The current buffer, respecting the restriction if any
  14138. tree The subtree started with the entry at point
  14139. region The entries within the active region, if any
  14140. region-start-level
  14141. The entries within the active region, but only those at
  14142. the same level than the first one.
  14143. file The current buffer, without restriction
  14144. file-with-archives
  14145. The current buffer, and any archives associated with it
  14146. agenda All agenda files
  14147. agenda-with-archives
  14148. All agenda files with any archive files associated with them
  14149. \(file1 file2 ...)
  14150. If this is a list, all files in the list will be scanned
  14151. The remaining args are treated as settings for the skipping facilities of
  14152. the scanner. The following items can be given here:
  14153. archive skip trees with the archive tag
  14154. comment skip trees with the COMMENT keyword
  14155. function or Emacs Lisp form:
  14156. will be used as value for `org-agenda-skip-function', so
  14157. whenever the function returns a position, FUNC will not be
  14158. called for that entry and search will continue from the
  14159. position returned
  14160. If your function needs to retrieve the tags including inherited tags
  14161. at the *current* entry, you can use the value of the variable
  14162. `org-scanner-tags' which will be much faster than getting the value
  14163. with `org-get-tags-at'. If your function gets properties with
  14164. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  14165. to t around the call to `org-entry-properties' to get the same speedup.
  14166. Note that if your function moves around to retrieve tags and properties at
  14167. a *different* entry, you cannot use these techniques."
  14168. (unless (and (or (eq scope 'region) (eq scope 'region-start-level))
  14169. (not (org-region-active-p)))
  14170. (let* ((org-agenda-archives-mode nil) ; just to make sure
  14171. (org-agenda-skip-archived-trees (memq 'archive skip))
  14172. (org-agenda-skip-comment-trees (memq 'comment skip))
  14173. (org-agenda-skip-function
  14174. (car (org-delete-all '(comment archive) skip)))
  14175. (org-tags-match-list-sublevels t)
  14176. (start-level (eq scope 'region-start-level))
  14177. matcher res
  14178. org-todo-keywords-for-agenda
  14179. org-done-keywords-for-agenda
  14180. org-todo-keyword-alist-for-agenda
  14181. org-tag-alist-for-agenda
  14182. org--matcher-tags-todo-only)
  14183. (cond
  14184. ((eq match t) (setq matcher t))
  14185. ((eq match nil) (setq matcher t))
  14186. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  14187. (save-excursion
  14188. (save-restriction
  14189. (cond ((eq scope 'tree)
  14190. (org-back-to-heading t)
  14191. (org-narrow-to-subtree)
  14192. (setq scope nil))
  14193. ((and (or (eq scope 'region) (eq scope 'region-start-level))
  14194. (org-region-active-p))
  14195. ;; If needed, set start-level to a string like "2"
  14196. (when start-level
  14197. (save-excursion
  14198. (goto-char (region-beginning))
  14199. (unless (org-at-heading-p) (outline-next-heading))
  14200. (setq start-level (org-current-level))))
  14201. (narrow-to-region (region-beginning)
  14202. (save-excursion
  14203. (goto-char (region-end))
  14204. (unless (and (bolp) (org-at-heading-p))
  14205. (outline-next-heading))
  14206. (point)))
  14207. (setq scope nil)))
  14208. (if (not scope)
  14209. (progn
  14210. (org-agenda-prepare-buffers
  14211. (and buffer-file-name (list buffer-file-name)))
  14212. (setq res
  14213. (org-scan-tags
  14214. func matcher org--matcher-tags-todo-only start-level)))
  14215. ;; Get the right scope
  14216. (cond
  14217. ((and scope (listp scope) (symbolp (car scope)))
  14218. (setq scope (eval scope)))
  14219. ((eq scope 'agenda)
  14220. (setq scope (org-agenda-files t)))
  14221. ((eq scope 'agenda-with-archives)
  14222. (setq scope (org-agenda-files t))
  14223. (setq scope (org-add-archive-files scope)))
  14224. ((eq scope 'file)
  14225. (setq scope (and buffer-file-name (list buffer-file-name))))
  14226. ((eq scope 'file-with-archives)
  14227. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  14228. (org-agenda-prepare-buffers scope)
  14229. (dolist (file scope)
  14230. (with-current-buffer (org-find-base-buffer-visiting file)
  14231. (save-excursion
  14232. (save-restriction
  14233. (widen)
  14234. (goto-char (point-min))
  14235. (setq res
  14236. (append
  14237. res
  14238. (org-scan-tags
  14239. func matcher org--matcher-tags-todo-only))))))))))
  14240. res)))
  14241. ;;; Properties API
  14242. (defconst org-special-properties
  14243. '("ALLTAGS" "BLOCKED" "CLOCKSUM" "CLOCKSUM_T" "CLOSED" "DEADLINE" "FILE"
  14244. "ITEM" "PRIORITY" "SCHEDULED" "TAGS" "TIMESTAMP" "TIMESTAMP_IA" "TODO")
  14245. "The special properties valid in Org mode.
  14246. These are properties that are not defined in the property drawer,
  14247. but in some other way.")
  14248. (defconst org-default-properties
  14249. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  14250. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  14251. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  14252. "EXPORT_OPTIONS" "EXPORT_TEXT" "EXPORT_FILE_NAME"
  14253. "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE" "UNNUMBERED"
  14254. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER" "REPEAT_TO_STATE"
  14255. "CLOCK_MODELINE_TOTAL" "STYLE" "HTML_CONTAINER_CLASS")
  14256. "Some properties that are used by Org mode for various purposes.
  14257. Being in this list makes sure that they are offered for completion.")
  14258. (defun org--valid-property-p (property)
  14259. "Non nil when string PROPERTY is a valid property name."
  14260. (not
  14261. (or (equal property "")
  14262. (org-string-match-p "\\s-" property))))
  14263. (defun org--update-property-plist (key val props)
  14264. "Associate KEY to VAL in alist PROPS.
  14265. Modifications are made by side-effect. Return new alist."
  14266. (let* ((appending (string= (substring key -1) "+"))
  14267. (key (if appending (substring key 0 -1) key))
  14268. (old (assoc-string key props t)))
  14269. (if (not old) (cons (cons key val) props)
  14270. (setcdr old (if appending (concat (cdr old) " " val) val))
  14271. props)))
  14272. (defun org-get-property-block (&optional beg force)
  14273. "Return the (beg . end) range of the body of the property drawer.
  14274. BEG is the beginning of the current subtree, or of the part
  14275. before the first headline. If it is not given, it will be found.
  14276. If the drawer does not exist, create it if FORCE is non-nil, or
  14277. return nil."
  14278. (org-with-wide-buffer
  14279. (when beg (goto-char beg))
  14280. (unless (org-before-first-heading-p)
  14281. (let ((beg (cond (beg)
  14282. ((or (not (featurep 'org-inlinetask))
  14283. (org-inlinetask-in-task-p))
  14284. (org-back-to-heading t))
  14285. (t (org-with-limited-levels (org-back-to-heading t))))))
  14286. (forward-line)
  14287. (when (org-looking-at-p org-planning-line-re) (forward-line))
  14288. (cond ((looking-at org-property-drawer-re)
  14289. (forward-line)
  14290. (cons (point) (progn (goto-char (match-end 0))
  14291. (line-beginning-position))))
  14292. (force
  14293. (goto-char beg)
  14294. (org-insert-property-drawer)
  14295. (let ((pos (save-excursion (search-forward ":END:")
  14296. (line-beginning-position))))
  14297. (cons pos pos))))))))
  14298. (defun org-at-property-p ()
  14299. "Non-nil when point is inside a property drawer.
  14300. See `org-property-re' for match data, if applicable."
  14301. (save-excursion
  14302. (beginning-of-line)
  14303. (and (looking-at org-property-re)
  14304. (let ((property-drawer (save-match-data (org-get-property-block))))
  14305. (and property-drawer
  14306. (>= (point) (car property-drawer))
  14307. (< (point) (cdr property-drawer)))))))
  14308. (defun org-property-action ()
  14309. "Do an action on properties."
  14310. (interactive)
  14311. (unless (org-at-property-p) (user-error "Not at a property"))
  14312. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  14313. (let ((c (read-char-exclusive)))
  14314. (cl-case c
  14315. (?s (call-interactively #'org-set-property))
  14316. (?d (call-interactively #'org-delete-property))
  14317. (?D (call-interactively #'org-delete-property-globally))
  14318. (?c (call-interactively #'org-compute-property-at-point))
  14319. (otherwise (user-error "No such property action %c" c)))))
  14320. (defun org-inc-effort ()
  14321. "Increment the value of the effort property in the current entry."
  14322. (interactive)
  14323. (org-set-effort nil t))
  14324. (defvar org-clock-effort) ; Defined in org-clock.el.
  14325. (defvar org-clock-current-task) ; Defined in org-clock.el.
  14326. (defun org-set-effort (&optional value increment)
  14327. "Set the effort property of the current entry.
  14328. With numerical prefix arg, use the nth allowed value, 0 stands for the
  14329. 10th allowed value.
  14330. When INCREMENT is non-nil, set the property to the next allowed value."
  14331. (interactive "P")
  14332. (when (equal value 0) (setq value 10))
  14333. (let* ((completion-ignore-case t)
  14334. (prop org-effort-property)
  14335. (cur (org-entry-get nil prop))
  14336. (allowed (org-property-get-allowed-values nil prop 'table))
  14337. (existing (mapcar 'list (org-property-values prop)))
  14338. (heading (nth 4 (org-heading-components)))
  14339. rpl
  14340. (val (cond
  14341. ((stringp value) value)
  14342. ((and allowed (integerp value))
  14343. (or (car (nth (1- value) allowed))
  14344. (car (org-last allowed))))
  14345. ((and allowed increment)
  14346. (or (cl-caadr (member (list cur) allowed))
  14347. (user-error "Allowed effort values are not set")))
  14348. (allowed
  14349. (message "Select 1-9,0, [RET%s]: %s"
  14350. (if cur (concat "=" cur) "")
  14351. (mapconcat 'car allowed " "))
  14352. (setq rpl (read-char-exclusive))
  14353. (if (equal rpl ?\r)
  14354. cur
  14355. (setq rpl (- rpl ?0))
  14356. (when (equal rpl 0) (setq rpl 10))
  14357. (if (and (> rpl 0) (<= rpl (length allowed)))
  14358. (car (nth (1- rpl) allowed))
  14359. (org-completing-read "Effort: " allowed nil))))
  14360. (t
  14361. (org-completing-read
  14362. (concat "Effort " (if (and cur (string-match "\\S-" cur))
  14363. (concat "[" cur "]") "")
  14364. ": ")
  14365. existing nil nil "" nil cur)))))
  14366. (unless (equal (org-entry-get nil prop) val)
  14367. (org-entry-put nil prop val))
  14368. (org-refresh-property
  14369. '((effort . identity)
  14370. (effort-minutes . org-duration-string-to-minutes))
  14371. val)
  14372. (when (string= heading org-clock-current-task)
  14373. (setq org-clock-effort (get-text-property (point-at-bol) 'effort))
  14374. (org-clock-update-mode-line))
  14375. (message "%s is now %s" prop val)))
  14376. (defun org-entry-properties (&optional pom which)
  14377. "Get all properties of the current entry.
  14378. When POM is a buffer position, get all properties from the entry
  14379. there instead.
  14380. This includes the TODO keyword, the tags, time strings for
  14381. deadline, scheduled, and clocking, and any additional properties
  14382. defined in the entry.
  14383. If WHICH is nil or `all', get all properties. If WHICH is
  14384. `special' or `standard', only get that subclass. If WHICH is
  14385. a string, only get that property.
  14386. Return value is an alist. Keys are properties, as upcased
  14387. strings."
  14388. (org-with-point-at pom
  14389. (when (and (derived-mode-p 'org-mode)
  14390. (ignore-errors (org-back-to-heading t)))
  14391. (catch 'exit
  14392. (let* ((beg (point))
  14393. (specific (and (stringp which) (upcase which)))
  14394. (which (cond ((not specific) which)
  14395. ((member specific org-special-properties) 'special)
  14396. (t 'standard)))
  14397. props)
  14398. ;; Get the special properties, like TODO and TAGS.
  14399. (when (memq which '(nil all special))
  14400. (when (or (not specific) (string= specific "CLOCKSUM"))
  14401. (let ((clocksum (get-text-property (point) :org-clock-minutes)))
  14402. (when clocksum
  14403. (push (cons "CLOCKSUM"
  14404. (org-minutes-to-clocksum-string clocksum))
  14405. props)))
  14406. (when specific (throw 'exit props)))
  14407. (when (or (not specific) (string= specific "CLOCKSUM_T"))
  14408. (let ((clocksumt (get-text-property (point)
  14409. :org-clock-minutes-today)))
  14410. (when clocksumt
  14411. (push (cons "CLOCKSUM_T"
  14412. (org-minutes-to-clocksum-string clocksumt))
  14413. props)))
  14414. (when specific (throw 'exit props)))
  14415. (when (or (not specific) (string= specific "ITEM"))
  14416. (when (looking-at org-complex-heading-regexp)
  14417. (push (cons "ITEM"
  14418. (let ((title (match-string-no-properties 4)))
  14419. (if (org-string-nw-p title)
  14420. (org-remove-tabs title)
  14421. "")))
  14422. props))
  14423. (when specific (throw 'exit props)))
  14424. (when (or (not specific) (string= specific "TODO"))
  14425. (let ((case-fold-search nil))
  14426. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  14427. (push (cons "TODO" (org-match-string-no-properties 2)) props)))
  14428. (when specific (throw 'exit props)))
  14429. (when (or (not specific) (string= specific "PRIORITY"))
  14430. (push (cons "PRIORITY"
  14431. (if (looking-at org-priority-regexp)
  14432. (org-match-string-no-properties 2)
  14433. (char-to-string org-default-priority)))
  14434. props)
  14435. (when specific (throw 'exit props)))
  14436. (when (or (not specific) (string= specific "FILE"))
  14437. (push (cons "FILE" (buffer-file-name (buffer-base-buffer)))
  14438. props)
  14439. (when specific (throw 'exit props)))
  14440. (when (or (not specific) (string= specific "TAGS"))
  14441. (let ((value (org-string-nw-p (org-get-tags-string))))
  14442. (when value (push (cons "TAGS" value) props)))
  14443. (when specific (throw 'exit props)))
  14444. (when (or (not specific) (string= specific "ALLTAGS"))
  14445. (let ((value (org-get-tags-at)))
  14446. (when value
  14447. (push (cons "ALLTAGS"
  14448. (format ":%s:" (mapconcat #'identity value ":")))
  14449. props)))
  14450. (when specific (throw 'exit props)))
  14451. (when (or (not specific) (string= specific "BLOCKED"))
  14452. (push (cons "BLOCKED" (if (org-entry-blocked-p) "t" "")) props)
  14453. (when specific (throw 'exit props)))
  14454. (when (or (not specific)
  14455. (member specific '("CLOSED" "DEADLINE" "SCHEDULED")))
  14456. (forward-line)
  14457. (when (org-looking-at-p org-planning-line-re)
  14458. (end-of-line)
  14459. (let ((bol (line-beginning-position))
  14460. ;; Backward compatibility: time keywords used to
  14461. ;; be configurable (before 8.3). Make sure we
  14462. ;; get the correct keyword.
  14463. (key-assoc `(("CLOSED" . ,org-closed-string)
  14464. ("DEADLINE" . ,org-deadline-string)
  14465. ("SCHEDULED" . ,org-scheduled-string))))
  14466. (dolist (pair (if specific (list (assoc specific key-assoc))
  14467. key-assoc))
  14468. (save-excursion
  14469. (when (search-backward (cdr pair) bol t)
  14470. (goto-char (match-end 0))
  14471. (skip-chars-forward " \t")
  14472. (and (looking-at org-ts-regexp-both)
  14473. (push (cons (car pair)
  14474. (org-match-string-no-properties 0))
  14475. props)))))))
  14476. (when specific (throw 'exit props)))
  14477. (when (or (not specific)
  14478. (member specific '("TIMESTAMP" "TIMESTAMP_IA")))
  14479. (let ((find-ts
  14480. (lambda (end ts)
  14481. ;; Fix next time-stamp before END. TS is the
  14482. ;; list of time-stamps found so far.
  14483. (let ((ts ts)
  14484. (regexp (cond
  14485. ((string= specific "TIMESTAMP")
  14486. org-ts-regexp)
  14487. ((string= specific "TIMESTAMP_IA")
  14488. org-ts-regexp-inactive)
  14489. ((assoc "TIMESTAMP_IA" ts)
  14490. org-ts-regexp)
  14491. ((assoc "TIMESTAMP" ts)
  14492. org-ts-regexp-inactive)
  14493. (t org-ts-regexp-both))))
  14494. (catch 'next
  14495. (while (re-search-forward regexp end t)
  14496. (backward-char)
  14497. (let ((object (org-element-context)))
  14498. ;; Accept to match timestamps in node
  14499. ;; properties, too.
  14500. (when (memq (org-element-type object)
  14501. '(node-property timestamp))
  14502. (let ((type
  14503. (org-element-property :type object)))
  14504. (cond
  14505. ((and (memq type '(active active-range))
  14506. (not (equal specific "TIMESTAMP_IA")))
  14507. (unless (assoc "TIMESTAMP" ts)
  14508. (push (cons "TIMESTAMP"
  14509. (org-element-property
  14510. :raw-value object))
  14511. ts)
  14512. (when specific (throw 'exit ts))))
  14513. ((and (memq type '(inactive inactive-range))
  14514. (not (string= specific "TIMESTAMP")))
  14515. (unless (assoc "TIMESTAMP_IA" ts)
  14516. (push (cons "TIMESTAMP_IA"
  14517. (org-element-property
  14518. :raw-value object))
  14519. ts)
  14520. (when specific (throw 'exit ts))))))
  14521. ;; Both timestamp types are found,
  14522. ;; move to next part.
  14523. (when (= (length ts) 2) (throw 'next ts)))))
  14524. ts)))))
  14525. (goto-char beg)
  14526. ;; First look for timestamps within headline.
  14527. (let ((ts (funcall find-ts (line-end-position) nil)))
  14528. (if (= (length ts) 2) (setq props (nconc ts props))
  14529. (forward-line)
  14530. ;; Then find timestamps in the section, skipping
  14531. ;; planning line.
  14532. (when (org-looking-at-p org-planning-line-re)
  14533. (forward-line))
  14534. (let ((end (save-excursion (outline-next-heading))))
  14535. (setq props (nconc (funcall find-ts end ts) props))))))))
  14536. ;; Get the standard properties, like :PROP:.
  14537. (when (memq which '(nil all standard))
  14538. ;; If we are looking after a specific property, delegate
  14539. ;; to `org-entry-get', which is faster. However, make an
  14540. ;; exception for "CATEGORY", since it can be also set
  14541. ;; through keywords (i.e. #+CATEGORY).
  14542. (if (and specific (not (equal specific "CATEGORY")))
  14543. (let ((value (org-entry-get beg specific nil t)))
  14544. (throw 'exit (and value (list (cons specific value)))))
  14545. (let ((range (org-get-property-block beg)))
  14546. (when range
  14547. (let ((end (cdr range)) seen-base)
  14548. (goto-char (car range))
  14549. ;; Unlike to `org--update-property-plist', we
  14550. ;; handle the case where base values is found
  14551. ;; after its extension. We also forbid standard
  14552. ;; properties to be named as special properties.
  14553. (while (re-search-forward org-property-re end t)
  14554. (let* ((key (upcase (org-match-string-no-properties 2)))
  14555. (extendp (org-string-match-p "\\+\\'" key))
  14556. (key-base (if extendp (substring key 0 -1) key))
  14557. (value (org-match-string-no-properties 3)))
  14558. (cond
  14559. ((member-ignore-case key-base org-special-properties))
  14560. (extendp
  14561. (setq props
  14562. (org--update-property-plist key value props)))
  14563. ((member key seen-base))
  14564. (t (push key seen-base)
  14565. (let ((p (assoc-string key props t)))
  14566. (if p (setcdr p (concat value " " (cdr p)))
  14567. (push (cons key value) props))))))))))))
  14568. (unless (assoc "CATEGORY" props)
  14569. (push (cons "CATEGORY" (org-get-category beg)) props)
  14570. (when (string= specific "CATEGORY") (throw 'exit props)))
  14571. ;; Return value.
  14572. props)))))
  14573. (defun org-property--local-values (property literal-nil)
  14574. "Return value for PROPERTY in current entry.
  14575. Value is a list whose car is the base value for PROPERTY and cdr
  14576. a list of accumulated values. Return nil if neither is found in
  14577. the entry. Also return nil when PROPERTY is set to \"nil\",
  14578. unless LITERAL-NIL is non-nil."
  14579. (let ((range (org-get-property-block)))
  14580. (when range
  14581. (goto-char (car range))
  14582. (let* ((case-fold-search t)
  14583. (end (cdr range))
  14584. (value
  14585. ;; Base value.
  14586. (save-excursion
  14587. (let ((v (and (re-search-forward
  14588. (org-re-property property nil t) end t)
  14589. (org-match-string-no-properties 3))))
  14590. (list (if literal-nil v (org-not-nil v)))))))
  14591. ;; Find additional values.
  14592. (let* ((property+ (org-re-property (concat property "+") nil t)))
  14593. (while (re-search-forward property+ end t)
  14594. (push (org-match-string-no-properties 3) value)))
  14595. ;; Return final values.
  14596. (and (not (equal value '(nil))) (nreverse value))))))
  14597. (defun org-entry-get (pom property &optional inherit literal-nil)
  14598. "Get value of PROPERTY for entry or content at point-or-marker POM.
  14599. If INHERIT is non-nil and the entry does not have the property,
  14600. then also check higher levels of the hierarchy. If INHERIT is
  14601. the symbol `selective', use inheritance only if the setting in
  14602. `org-use-property-inheritance' selects PROPERTY for inheritance.
  14603. If the property is present but empty, the return value is the
  14604. empty string. If the property is not present at all, nil is
  14605. returned. In any other case, return the value as a string.
  14606. Search is case-insensitive.
  14607. If LITERAL-NIL is set, return the string value \"nil\" as
  14608. a string, do not interpret it as the list atom nil. This is used
  14609. for inheritance when a \"nil\" value can supersede a non-nil
  14610. value higher up the hierarchy."
  14611. (org-with-point-at pom
  14612. (cond
  14613. ((member-ignore-case property (cons "CATEGORY" org-special-properties))
  14614. ;; We need a special property. Use `org-entry-properties' to
  14615. ;; retrieve it, but specify the wanted property.
  14616. (cdr (assoc-string property (org-entry-properties nil property))))
  14617. ((and inherit
  14618. (or (not (eq inherit 'selective)) (org-property-inherit-p property)))
  14619. (org-entry-get-with-inheritance property literal-nil))
  14620. (t
  14621. (let* ((local (org-property--local-values property literal-nil))
  14622. (value (and local (mapconcat #'identity (delq nil local) " "))))
  14623. (if literal-nil value (org-not-nil value)))))))
  14624. (defun org-property-or-variable-value (var &optional inherit)
  14625. "Check if there is a property fixing the value of VAR.
  14626. If yes, return this value. If not, return the current value of the variable."
  14627. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  14628. (if (and prop (stringp prop) (string-match "\\S-" prop))
  14629. (read prop)
  14630. (symbol-value var))))
  14631. (defun org-entry-delete (pom property)
  14632. "Delete PROPERTY from entry at point-or-marker POM.
  14633. Accumulated properties, i.e. PROPERTY+, are also removed. Return
  14634. non-nil when a property was removed."
  14635. (unless (member property org-special-properties)
  14636. (org-with-point-at pom
  14637. (let ((range (org-get-property-block)))
  14638. (when range
  14639. (let* ((begin (car range))
  14640. (origin (cdr range))
  14641. (end (copy-marker origin))
  14642. (re (org-re-property
  14643. (concat (regexp-quote property) "\\+?") t t)))
  14644. (goto-char begin)
  14645. (while (re-search-forward re end t)
  14646. (delete-region (match-beginning 0) (line-beginning-position 2)))
  14647. ;; If drawer is empty, remove it altogether.
  14648. (when (= begin end)
  14649. (delete-region (line-beginning-position 0)
  14650. (line-beginning-position 2)))
  14651. ;; Return non-nil if some property was removed.
  14652. (prog1 (/= end origin) (set-marker end nil))))))))
  14653. ;; Multi-values properties are properties that contain multiple values
  14654. ;; These values are assumed to be single words, separated by whitespace.
  14655. (defun org-entry-add-to-multivalued-property (pom property value)
  14656. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  14657. (let* ((old (org-entry-get pom property))
  14658. (values (and old (org-split-string old "[ \t]"))))
  14659. (setq value (org-entry-protect-space value))
  14660. (unless (member value values)
  14661. (setq values (append values (list value)))
  14662. (org-entry-put pom property
  14663. (mapconcat 'identity values " ")))))
  14664. (defun org-entry-remove-from-multivalued-property (pom property value)
  14665. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  14666. (let* ((old (org-entry-get pom property))
  14667. (values (and old (org-split-string old "[ \t]"))))
  14668. (setq value (org-entry-protect-space value))
  14669. (when (member value values)
  14670. (setq values (delete value values))
  14671. (org-entry-put pom property
  14672. (mapconcat 'identity values " ")))))
  14673. (defun org-entry-member-in-multivalued-property (pom property value)
  14674. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  14675. (let* ((old (org-entry-get pom property))
  14676. (values (and old (org-split-string old "[ \t]"))))
  14677. (setq value (org-entry-protect-space value))
  14678. (member value values)))
  14679. (defun org-entry-get-multivalued-property (pom property)
  14680. "Return a list of values in a multivalued property."
  14681. (let* ((value (org-entry-get pom property))
  14682. (values (and value (org-split-string value "[ \t]"))))
  14683. (mapcar 'org-entry-restore-space values)))
  14684. (defun org-entry-put-multivalued-property (pom property &rest values)
  14685. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  14686. VALUES should be a list of strings. Spaces will be protected."
  14687. (org-entry-put pom property
  14688. (mapconcat 'org-entry-protect-space values " "))
  14689. (let* ((value (org-entry-get pom property))
  14690. (values (and value (org-split-string value "[ \t]"))))
  14691. (mapcar 'org-entry-restore-space values)))
  14692. (defun org-entry-protect-space (s)
  14693. "Protect spaces and newline in string S."
  14694. (while (string-match " " s)
  14695. (setq s (replace-match "%20" t t s)))
  14696. (while (string-match "\n" s)
  14697. (setq s (replace-match "%0A" t t s)))
  14698. s)
  14699. (defun org-entry-restore-space (s)
  14700. "Restore spaces and newline in string S."
  14701. (while (string-match "%20" s)
  14702. (setq s (replace-match " " t t s)))
  14703. (while (string-match "%0A" s)
  14704. (setq s (replace-match "\n" t t s)))
  14705. s)
  14706. (defvar org-entry-property-inherited-from (make-marker)
  14707. "Marker pointing to the entry from where a property was inherited.
  14708. Each call to `org-entry-get-with-inheritance' will set this marker to the
  14709. location of the entry where the inheritance search matched. If there was
  14710. no match, the marker will point nowhere.
  14711. Note that also `org-entry-get' calls this function, if the INHERIT flag
  14712. is set.")
  14713. (defun org-entry-get-with-inheritance (property &optional literal-nil)
  14714. "Get PROPERTY of entry or content at point, search higher levels if needed.
  14715. The search will stop at the first ancestor which has the property defined.
  14716. If the value found is \"nil\", return nil to show that the property
  14717. should be considered as undefined (this is the meaning of nil here).
  14718. However, if LITERAL-NIL is set, return the string value \"nil\" instead."
  14719. (move-marker org-entry-property-inherited-from nil)
  14720. (org-with-wide-buffer
  14721. (let (value)
  14722. (catch 'exit
  14723. (while t
  14724. (let ((v (org-property--local-values property literal-nil)))
  14725. (when v
  14726. (setq value
  14727. (concat (mapconcat #'identity (delq nil v) " ")
  14728. (and value " ")
  14729. value)))
  14730. (cond
  14731. ((car v)
  14732. (org-back-to-heading t)
  14733. (move-marker org-entry-property-inherited-from (point))
  14734. (throw 'exit nil))
  14735. ((org-up-heading-safe))
  14736. (t
  14737. (let ((global
  14738. (cdr (or (assoc-string property org-file-properties t)
  14739. (assoc-string property org-global-properties t)
  14740. (assoc-string property org-global-properties-fixed t)))))
  14741. (cond ((not global))
  14742. (value (setq value (concat global " " value)))
  14743. (t (setq value global))))
  14744. (throw 'exit nil))))))
  14745. (if literal-nil value (org-not-nil value)))))
  14746. (defvar org-property-changed-functions nil
  14747. "Hook called when the value of a property has changed.
  14748. Each hook function should accept two arguments, the name of the property
  14749. and the new value.")
  14750. (defun org-entry-put (pom property value)
  14751. "Set PROPERTY to VALUE for entry at point-or-marker POM.
  14752. If the value is nil, it is converted to the empty string. If it
  14753. is not a string, an error is raised. Also raise an error on
  14754. invalid property names.
  14755. PROPERTY can be any regular property (see
  14756. `org-special-properties'). It can also be \"TODO\",
  14757. \"PRIORITY\", \"SCHEDULED\" and \"DEADLINE\".
  14758. For the last two properties, VALUE may have any of the special
  14759. values \"earlier\" and \"later\". The function then increases or
  14760. decreases scheduled or deadline date by one day."
  14761. (cond ((null value) (setq value ""))
  14762. ((not (stringp value)) (error "Properties values should be strings"))
  14763. ((not (org--valid-property-p property))
  14764. (user-error "Invalid property name: \"%s\"" property)))
  14765. (org-with-point-at pom
  14766. (if (or (not (featurep 'org-inlinetask)) (org-inlinetask-in-task-p))
  14767. (org-back-to-heading t)
  14768. (org-with-limited-levels (org-back-to-heading t)))
  14769. (let ((beg (point)))
  14770. (cond
  14771. ((equal property "TODO")
  14772. (cond ((not (org-string-nw-p value)) (setq value 'none))
  14773. ((not (member value org-todo-keywords-1))
  14774. (user-error "\"%s\" is not a valid TODO state" value)))
  14775. (org-todo value)
  14776. (org-set-tags nil 'align))
  14777. ((equal property "PRIORITY")
  14778. (org-priority (if (org-string-nw-p value) (string-to-char value) ?\s))
  14779. (org-set-tags nil 'align))
  14780. ((equal property "SCHEDULED")
  14781. (forward-line)
  14782. (if (and (org-looking-at-p org-planning-line-re)
  14783. (re-search-forward
  14784. org-scheduled-time-regexp (line-end-position) t))
  14785. (cond ((string= value "earlier") (org-timestamp-change -1 'day))
  14786. ((string= value "later") (org-timestamp-change 1 'day))
  14787. ((string= value "") (org-schedule '(4)))
  14788. (t (org-schedule nil value)))
  14789. (if (member value '("earlier" "later" ""))
  14790. (call-interactively #'org-schedule)
  14791. (org-schedule nil value))))
  14792. ((equal property "DEADLINE")
  14793. (forward-line)
  14794. (if (and (org-looking-at-p org-planning-line-re)
  14795. (re-search-forward
  14796. org-deadline-time-regexp (line-end-position) t))
  14797. (cond ((string= value "earlier") (org-timestamp-change -1 'day))
  14798. ((string= value "later") (org-timestamp-change 1 'day))
  14799. ((string= value "") (org-deadline '(4)))
  14800. (t (org-deadline nil value)))
  14801. (if (member value '("earlier" "later" ""))
  14802. (call-interactively #'org-deadline)
  14803. (org-deadline nil value))))
  14804. ((member property org-special-properties)
  14805. (error "The %s property cannot be set with `org-entry-put'" property))
  14806. (t
  14807. (let* ((range (org-get-property-block beg 'force))
  14808. (end (cdr range))
  14809. (case-fold-search t))
  14810. (goto-char (car range))
  14811. (if (re-search-forward (org-re-property property nil t) end t)
  14812. (progn (delete-region (match-beginning 0) (match-end 0))
  14813. (goto-char (match-beginning 0)))
  14814. (goto-char end)
  14815. (insert "\n")
  14816. (backward-char))
  14817. (insert ":" property ":")
  14818. (when value (insert " " value))
  14819. (org-indent-line)))))
  14820. (run-hook-with-args 'org-property-changed-functions property value)))
  14821. (defun org-buffer-property-keys
  14822. (&optional specials defaults columns ignore-malformed)
  14823. "Get all property keys in the current buffer.
  14824. When SPECIALS is non-nil, also list the special properties that
  14825. reflect things like tags and TODO state.
  14826. When DEFAULTS is non-nil, also include properties that has
  14827. special meaning internally: ARCHIVE, CATEGORY, SUMMARY,
  14828. DESCRIPTION, LOCATION, and LOGGING and others.
  14829. When COLUMNS in non-nil, also include property names given in
  14830. COLUMN formats in the current buffer.
  14831. When IGNORE-MALFORMED is non-nil, malformed drawer repair will not be
  14832. automatically performed, such drawers will be silently ignored."
  14833. (let ((case-fold-search t)
  14834. (props (append
  14835. (and specials org-special-properties)
  14836. (and defaults (cons org-effort-property org-default-properties))
  14837. nil)))
  14838. (org-with-wide-buffer
  14839. (goto-char (point-min))
  14840. (while (re-search-forward org-property-start-re nil t)
  14841. (let ((range (org-get-property-block)))
  14842. (catch 'skip
  14843. (unless range
  14844. (when (and (not ignore-malformed)
  14845. (not (org-before-first-heading-p))
  14846. (y-or-n-p (format "Malformed drawer at %d, repair?"
  14847. (line-beginning-position))))
  14848. (org-get-property-block nil t))
  14849. (throw 'skip nil))
  14850. (goto-char (car range))
  14851. (let ((begin (car range))
  14852. (end (cdr range)))
  14853. ;; Make sure that found property block is not located
  14854. ;; before current point, as it would generate an infloop.
  14855. ;; It can happen, for example, in the following
  14856. ;; situation:
  14857. ;;
  14858. ;; * Headline
  14859. ;; :PROPERTIES:
  14860. ;; ...
  14861. ;; :END:
  14862. ;; *************** Inlinetask
  14863. ;; #+BEGIN_EXAMPLE
  14864. ;; :PROPERTIES:
  14865. ;; #+END_EXAMPLE
  14866. ;;
  14867. (if (< begin (point)) (throw 'skip nil) (goto-char begin))
  14868. (while (< (point) end)
  14869. (let ((p (progn (looking-at org-property-re)
  14870. (org-match-string-no-properties 2))))
  14871. ;; Only add true property name, not extension symbol.
  14872. (push (if (not (org-string-match-p "\\+\\'" p)) p
  14873. (substring p 0 -1))
  14874. props))
  14875. (forward-line))))
  14876. (outline-next-heading)))
  14877. (when columns
  14878. (goto-char (point-min))
  14879. (while (re-search-forward "^[ \t]*\\(?:#\\+\\|:\\)COLUMNS:" nil t)
  14880. (let ((element (org-element-at-point)))
  14881. (when (memq (org-element-type element) '(keyword node-property))
  14882. (let ((value (org-element-property :value element))
  14883. (start 0))
  14884. (while (string-match "%[0-9]*\\(\\S-+\\)" value start)
  14885. (setq start (match-end 0))
  14886. (let ((p (org-match-string-no-properties 1 value)))
  14887. (unless (member-ignore-case p org-special-properties)
  14888. (push p props))))))))))
  14889. (sort (delete-dups props) (lambda (a b) (string< (upcase a) (upcase b))))))
  14890. (defun org-property-values (key)
  14891. "List all non-nil values of property KEY in current buffer."
  14892. (org-with-wide-buffer
  14893. (goto-char (point-min))
  14894. (let ((case-fold-search t)
  14895. (re (org-re-property key))
  14896. values)
  14897. (while (re-search-forward re nil t)
  14898. (push (org-entry-get (point) key) values))
  14899. (delete-dups values))))
  14900. (defun org-insert-property-drawer ()
  14901. "Insert a property drawer into the current entry."
  14902. (org-with-wide-buffer
  14903. (if (or (not (featurep 'org-inlinetask)) (org-inlinetask-in-task-p))
  14904. (org-back-to-heading t)
  14905. (org-with-limited-levels (org-back-to-heading t)))
  14906. (forward-line)
  14907. (when (org-looking-at-p org-planning-line-re) (forward-line))
  14908. (unless (org-looking-at-p org-property-drawer-re)
  14909. ;; Make sure we start editing a line from current entry, not from
  14910. ;; next one. It prevents extending text properties or overlays
  14911. ;; belonging to the latter.
  14912. (when (bolp) (backward-char))
  14913. (let ((begin (1+ (point)))
  14914. (inhibit-read-only t))
  14915. (insert "\n:PROPERTIES:\n:END:")
  14916. (when (eobp) (insert "\n"))
  14917. (org-indent-region begin (point))))))
  14918. (defun org-insert-drawer (&optional arg drawer)
  14919. "Insert a drawer at point.
  14920. When optional argument ARG is non-nil, insert a property drawer.
  14921. Optional argument DRAWER, when non-nil, is a string representing
  14922. drawer's name. Otherwise, the user is prompted for a name.
  14923. If a region is active, insert the drawer around that region
  14924. instead.
  14925. Point is left between drawer's boundaries."
  14926. (interactive "P")
  14927. (let* ((drawer (if arg "PROPERTIES"
  14928. (or drawer (read-from-minibuffer "Drawer: ")))))
  14929. (cond
  14930. ;; With C-u, fall back on `org-insert-property-drawer'
  14931. (arg (org-insert-property-drawer))
  14932. ;; Check validity of suggested drawer's name.
  14933. ((not (org-string-match-p org-drawer-regexp (format ":%s:" drawer)))
  14934. (user-error "Invalid drawer name"))
  14935. ;; With an active region, insert a drawer at point.
  14936. ((not (org-region-active-p))
  14937. (progn
  14938. (unless (bolp) (insert "\n"))
  14939. (insert (format ":%s:\n\n:END:\n" drawer))
  14940. (forward-line -2)))
  14941. ;; Otherwise, insert the drawer at point
  14942. (t
  14943. (let ((rbeg (region-beginning))
  14944. (rend (copy-marker (region-end))))
  14945. (unwind-protect
  14946. (progn
  14947. (goto-char rbeg)
  14948. (beginning-of-line)
  14949. (when (save-excursion
  14950. (re-search-forward org-outline-regexp-bol rend t))
  14951. (user-error "Drawers cannot contain headlines"))
  14952. ;; Position point at the beginning of the first
  14953. ;; non-blank line in region. Insert drawer's opening
  14954. ;; there, then indent it.
  14955. (org-skip-whitespace)
  14956. (beginning-of-line)
  14957. (insert ":" drawer ":\n")
  14958. (forward-line -1)
  14959. (indent-for-tab-command)
  14960. ;; Move point to the beginning of the first blank line
  14961. ;; after the last non-blank line in region. Insert
  14962. ;; drawer's closing, then indent it.
  14963. (goto-char rend)
  14964. (skip-chars-backward " \r\t\n")
  14965. (insert "\n:END:")
  14966. (deactivate-mark t)
  14967. (indent-for-tab-command)
  14968. (unless (eolp) (insert "\n")))
  14969. ;; Clear marker, whatever the outcome of insertion is.
  14970. (set-marker rend nil)))))))
  14971. (defvar org-property-set-functions-alist nil
  14972. "Property set function alist.
  14973. Each entry should have the following format:
  14974. (PROPERTY . READ-FUNCTION)
  14975. The read function will be called with the same argument as
  14976. `org-completing-read'.")
  14977. (defun org-set-property-function (property)
  14978. "Get the function that should be used to set PROPERTY.
  14979. This is computed according to `org-property-set-functions-alist'."
  14980. (or (cdr (assoc property org-property-set-functions-alist))
  14981. 'org-completing-read))
  14982. (defun org-read-property-value (property)
  14983. "Read PROPERTY value from user."
  14984. (let* ((completion-ignore-case t)
  14985. (allowed (org-property-get-allowed-values nil property 'table))
  14986. (cur (org-entry-get nil property))
  14987. (prompt (concat property " value"
  14988. (if (and cur (string-match "\\S-" cur))
  14989. (concat " [" cur "]") "") ": "))
  14990. (set-function (org-set-property-function property))
  14991. (val (if allowed
  14992. (funcall set-function prompt allowed nil
  14993. (not (get-text-property 0 'org-unrestricted
  14994. (caar allowed))))
  14995. (funcall set-function prompt
  14996. (mapcar 'list (org-property-values property))
  14997. nil nil "" nil cur))))
  14998. (org-trim val)))
  14999. (defvar org-last-set-property nil)
  15000. (defvar org-last-set-property-value nil)
  15001. (defun org-read-property-name ()
  15002. "Read a property name."
  15003. (let ((completion-ignore-case t)
  15004. (default-prop (or (and (org-at-property-p)
  15005. (org-match-string-no-properties 2))
  15006. org-last-set-property)))
  15007. (org-completing-read
  15008. (concat "Property"
  15009. (if default-prop (concat " [" default-prop "]") "")
  15010. ": ")
  15011. (mapcar #'list (org-buffer-property-keys nil t t))
  15012. nil nil nil nil default-prop)))
  15013. (defun org-set-property-and-value (use-last)
  15014. "Allow to set [PROPERTY]: [value] direction from prompt.
  15015. When use-default, don't even ask, just use the last
  15016. \"[PROPERTY]: [value]\" string from the history."
  15017. (interactive "P")
  15018. (let* ((completion-ignore-case t)
  15019. (pv (or (and use-last org-last-set-property-value)
  15020. (org-completing-read
  15021. "Enter a \"[Property]: [value]\" pair: "
  15022. nil nil nil nil nil
  15023. org-last-set-property-value)))
  15024. prop val)
  15025. (when (string-match "^[ \t]*\\([^:]+\\):[ \t]*\\(.*\\)[ \t]*$" pv)
  15026. (setq prop (match-string 1 pv)
  15027. val (match-string 2 pv))
  15028. (org-set-property prop val))))
  15029. (defun org-set-property (property value)
  15030. "In the current entry, set PROPERTY to VALUE.
  15031. When called interactively, this will prompt for a property name, offering
  15032. completion on existing and default properties. And then it will prompt
  15033. for a value, offering completion either on allowed values (via an inherited
  15034. xxx_ALL property) or on existing values in other instances of this property
  15035. in the current file.
  15036. Throw an error when trying to set a property with an invalid name."
  15037. (interactive (list nil nil))
  15038. (let ((property (or property (org-read-property-name))))
  15039. ;; `org-entry-put' also makes the following check, but this one
  15040. ;; avoids polluting `org-last-set-property' and
  15041. ;; `org-last-set-property-value' needlessly.
  15042. (unless (org--valid-property-p property)
  15043. (user-error "Invalid property name: \"%s\"" property))
  15044. (let ((value (or value (org-read-property-value property)))
  15045. (fn (cdr (assoc-string property org-properties-postprocess-alist t))))
  15046. (setq org-last-set-property property)
  15047. (setq org-last-set-property-value (concat property ": " value))
  15048. ;; Possibly postprocess the inserted value:
  15049. (when fn (setq value (funcall fn value)))
  15050. (unless (equal (org-entry-get nil property) value)
  15051. (org-entry-put nil property value)))))
  15052. (defun org-find-property (property &optional value)
  15053. "Find first entry in buffer that sets PROPERTY.
  15054. When optional argument VALUE is non-nil, only consider an entry
  15055. if it contains PROPERTY set to this value. If PROPERTY should be
  15056. explicitly set to nil, use string \"nil\" for VALUE.
  15057. Return position where the entry begins, or nil if there is no
  15058. such entry. If narrowing is in effect, only search the visible
  15059. part of the buffer."
  15060. (save-excursion
  15061. (goto-char (point-min))
  15062. (let ((case-fold-search t)
  15063. (re (org-re-property property nil (not value) value)))
  15064. (catch 'exit
  15065. (while (re-search-forward re nil t)
  15066. (when (if value (org-at-property-p)
  15067. (org-entry-get (point) property nil t))
  15068. (throw 'exit (progn (org-back-to-heading t) (point)))))))))
  15069. (defun org-delete-property (property)
  15070. "In the current entry, delete PROPERTY."
  15071. (interactive
  15072. (let* ((completion-ignore-case t)
  15073. (cat (org-entry-get (point) "CATEGORY"))
  15074. (props0 (org-entry-properties nil 'standard))
  15075. (props (if cat props0
  15076. (delete `("CATEGORY" . ,(org-get-category)) props0)))
  15077. (prop (if (< 1 (length props))
  15078. (completing-read "Property: " props nil t)
  15079. (caar props))))
  15080. (list prop)))
  15081. (if (not property)
  15082. (message "No property to delete in this entry")
  15083. (org-entry-delete nil property)
  15084. (message "Property \"%s\" deleted" property)))
  15085. (defun org-delete-property-globally (property)
  15086. "Remove PROPERTY globally, from all entries.
  15087. This function ignores narrowing, if any."
  15088. (interactive
  15089. (let* ((completion-ignore-case t)
  15090. (prop (completing-read
  15091. "Globally remove property: "
  15092. (mapcar #'list (org-buffer-property-keys)))))
  15093. (list prop)))
  15094. (org-with-wide-buffer
  15095. (goto-char (point-min))
  15096. (let ((count 0)
  15097. (re (org-re-property (concat (regexp-quote property) "\\+?") t t)))
  15098. (while (re-search-forward re nil t)
  15099. (when (org-entry-delete (point) property) (cl-incf count)))
  15100. (message "Property \"%s\" removed from %d entries" property count))))
  15101. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  15102. (defun org-compute-property-at-point ()
  15103. "Compute the property at point.
  15104. This looks for an enclosing column format, extracts the operator and
  15105. then applies it to the property in the column format's scope."
  15106. (interactive)
  15107. (unless (org-at-property-p)
  15108. (user-error "Not at a property"))
  15109. (let ((prop (org-match-string-no-properties 2)))
  15110. (org-columns-get-format-and-top-level)
  15111. (unless (nth 3 (assoc-string prop org-columns-current-fmt-compiled t))
  15112. (user-error "No operator defined for property %s" prop))
  15113. (org-columns-compute prop)))
  15114. (defvar org-property-allowed-value-functions nil
  15115. "Hook for functions supplying allowed values for a specific property.
  15116. The functions must take a single argument, the name of the property, and
  15117. return a flat list of allowed values. If \":ETC\" is one of
  15118. the values, this means that these values are intended as defaults for
  15119. completion, but that other values should be allowed too.
  15120. The functions must return nil if they are not responsible for this
  15121. property.")
  15122. (defun org-property-get-allowed-values (pom property &optional table)
  15123. "Get allowed values for the property PROPERTY.
  15124. When TABLE is non-nil, return an alist that can directly be used for
  15125. completion."
  15126. (let (vals)
  15127. (cond
  15128. ((equal property "TODO")
  15129. (setq vals (org-with-point-at pom
  15130. (append org-todo-keywords-1 '("")))))
  15131. ((equal property "PRIORITY")
  15132. (let ((n org-lowest-priority))
  15133. (while (>= n org-highest-priority)
  15134. (push (char-to-string n) vals)
  15135. (setq n (1- n)))))
  15136. ((equal property "CATEGORY"))
  15137. ((member property org-special-properties))
  15138. ((setq vals (run-hook-with-args-until-success
  15139. 'org-property-allowed-value-functions property)))
  15140. (t
  15141. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  15142. (when (and vals (string-match "\\S-" vals))
  15143. (setq vals (car (read-from-string (concat "(" vals ")"))))
  15144. (setq vals (mapcar (lambda (x)
  15145. (cond ((stringp x) x)
  15146. ((numberp x) (number-to-string x))
  15147. ((symbolp x) (symbol-name x))
  15148. (t "???")))
  15149. vals)))))
  15150. (when (member ":ETC" vals)
  15151. (setq vals (remove ":ETC" vals))
  15152. (org-add-props (car vals) '(org-unrestricted t)))
  15153. (if table (mapcar 'list vals) vals)))
  15154. (defun org-property-previous-allowed-value (&optional _previous)
  15155. "Switch to the next allowed value for this property."
  15156. (interactive)
  15157. (org-property-next-allowed-value t))
  15158. (defun org-property-next-allowed-value (&optional previous)
  15159. "Switch to the next allowed value for this property."
  15160. (interactive)
  15161. (unless (org-at-property-p)
  15162. (user-error "Not at a property"))
  15163. (let* ((prop (car (save-match-data (org-split-string (match-string 1) ":"))))
  15164. (key (match-string 2))
  15165. (value (match-string 3))
  15166. (allowed (or (org-property-get-allowed-values (point) key)
  15167. (and (member value '("[ ]" "[-]" "[X]"))
  15168. '("[ ]" "[X]"))))
  15169. (heading (save-match-data (nth 4 (org-heading-components))))
  15170. nval)
  15171. (unless allowed
  15172. (user-error "Allowed values for this property have not been defined"))
  15173. (when previous (setq allowed (reverse allowed)))
  15174. (when (member value allowed)
  15175. (setq nval (car (cdr (member value allowed)))))
  15176. (setq nval (or nval (car allowed)))
  15177. (when (equal nval value)
  15178. (user-error "Only one allowed value for this property"))
  15179. (org-at-property-p)
  15180. (replace-match (concat " :" key ": " nval) t t)
  15181. (org-indent-line)
  15182. (beginning-of-line 1)
  15183. (skip-chars-forward " \t")
  15184. (when (equal prop org-effort-property)
  15185. (org-refresh-property
  15186. '((effort . identity)
  15187. (effort-minutes . org-duration-string-to-minutes))
  15188. nval)
  15189. (when (string= org-clock-current-task heading)
  15190. (setq org-clock-effort nval)
  15191. (org-clock-update-mode-line)))
  15192. (run-hook-with-args 'org-property-changed-functions key nval)))
  15193. (defun org-find-olp (path &optional this-buffer)
  15194. "Return a marker pointing to the entry at outline path OLP.
  15195. If anything goes wrong, throw an error.
  15196. You can wrap this call to catch the error like this:
  15197. \(condition-case msg
  15198. \(org-mobile-locate-entry (match-string 4))
  15199. \(error (nth 1 msg)))
  15200. The return value will then be either a string with the error message,
  15201. or a marker if everything is OK.
  15202. If THIS-BUFFER is set, the outline path does not contain a file,
  15203. only headings."
  15204. (let* ((file (if this-buffer buffer-file-name (pop path)))
  15205. (buffer (if this-buffer (current-buffer) (find-file-noselect file)))
  15206. (level 1)
  15207. (lmin 1)
  15208. (lmax 1)
  15209. end found flevel)
  15210. (unless buffer (error "File not found :%s" file))
  15211. (with-current-buffer buffer
  15212. (org-with-wide-buffer
  15213. (goto-char (point-min))
  15214. (dolist (heading path)
  15215. (let ((re (format org-complex-heading-regexp-format
  15216. (regexp-quote heading)))
  15217. (cnt 0))
  15218. (while (re-search-forward re end t)
  15219. (setq level (- (match-end 1) (match-beginning 1)))
  15220. (when (and (>= level lmin) (<= level lmax))
  15221. (setq found (match-beginning 0) flevel level cnt (1+ cnt))))
  15222. (when (= cnt 0)
  15223. (error "Heading not found on level %d: %s" lmax heading))
  15224. (when (> cnt 1)
  15225. (error "Heading not unique on level %d: %s" lmax heading))
  15226. (goto-char found)
  15227. (setq lmin (1+ flevel) lmax (+ lmin (if org-odd-levels-only 1 0)))
  15228. (setq end (save-excursion (org-end-of-subtree t t)))))
  15229. (when (org-at-heading-p)
  15230. (point-marker))))))
  15231. (defun org-find-exact-headline-in-buffer (heading &optional buffer pos-only)
  15232. "Find node HEADING in BUFFER.
  15233. Return a marker to the heading if it was found, or nil if not.
  15234. If POS-ONLY is set, return just the position instead of a marker.
  15235. The heading text must match exact, but it may have a TODO keyword,
  15236. a priority cookie and tags in the standard locations."
  15237. (with-current-buffer (or buffer (current-buffer))
  15238. (save-excursion
  15239. (save-restriction
  15240. (widen)
  15241. (goto-char (point-min))
  15242. (let (case-fold-search)
  15243. (when (re-search-forward
  15244. (format org-complex-heading-regexp-format
  15245. (regexp-quote heading)) nil t)
  15246. (if pos-only
  15247. (match-beginning 0)
  15248. (move-marker (make-marker) (match-beginning 0)))))))))
  15249. (defun org-find-exact-heading-in-directory (heading &optional dir)
  15250. "Find Org node headline HEADING in all .org files in directory DIR.
  15251. When the target headline is found, return a marker to this location."
  15252. (let ((files (directory-files (or dir default-directory)
  15253. t "\\`[^.#].*\\.org\\'"))
  15254. visiting m buffer)
  15255. (catch 'found
  15256. (dolist (file files)
  15257. (message "trying %s" file)
  15258. (setq visiting (org-find-base-buffer-visiting file))
  15259. (setq buffer (or visiting (find-file-noselect file)))
  15260. (setq m (org-find-exact-headline-in-buffer
  15261. heading buffer))
  15262. (when (and (not m) (not visiting)) (kill-buffer buffer))
  15263. (and m (throw 'found m))))))
  15264. (defun org-find-entry-with-id (ident)
  15265. "Locate the entry that contains the ID property with exact value IDENT.
  15266. IDENT can be a string, a symbol or a number, this function will search for
  15267. the string representation of it.
  15268. Return the position where this entry starts, or nil if there is no such entry."
  15269. (interactive "sID: ")
  15270. (let ((id (cond
  15271. ((stringp ident) ident)
  15272. ((symbolp ident) (symbol-name ident))
  15273. ((numberp ident) (number-to-string ident))
  15274. (t (error "IDENT %s must be a string, symbol or number" ident)))))
  15275. (org-with-wide-buffer (org-find-property "ID" id))))
  15276. ;;;; Timestamps
  15277. (defvar org-last-changed-timestamp nil)
  15278. (defvar org-last-inserted-timestamp nil
  15279. "The last time stamp inserted with `org-insert-time-stamp'.")
  15280. (defvar org-ts-what) ; dynamically scoped parameter
  15281. (defun org-time-stamp (arg &optional inactive)
  15282. "Prompt for a date/time and insert a time stamp.
  15283. If the user specifies a time like HH:MM or if this command is
  15284. called with at least one prefix argument, the time stamp contains
  15285. the date and the time. Otherwise, only the date is included.
  15286. All parts of a date not specified by the user are filled in from
  15287. the timestamp at point, if any, or the current date/time
  15288. otherwise.
  15289. If there is already a timestamp at the cursor, it is replaced.
  15290. With two universal prefix arguments, insert an active timestamp
  15291. with the current time without prompting the user.
  15292. When called from lisp, the timestamp is inactive if INACTIVE is
  15293. non-nil."
  15294. (interactive "P")
  15295. (let* ((ts (cond
  15296. ((org-at-date-range-p t)
  15297. (match-string (if (< (point) (- (match-beginning 2) 2)) 1 2)))
  15298. ((org-at-timestamp-p t) (match-string 0))))
  15299. ;; Default time is either the timestamp at point or today.
  15300. ;; When entering a range, only the range start is considered.
  15301. (default-time (if (not ts) (current-time)
  15302. (apply #'encode-time (org-parse-time-string ts))))
  15303. (default-input (and ts (org-get-compact-tod ts)))
  15304. (repeater (and ts
  15305. (string-match "\\([.+-]+[0-9]+[hdwmy] ?\\)+" ts)
  15306. (match-string 0 ts)))
  15307. org-time-was-given
  15308. org-end-time-was-given
  15309. (time
  15310. (and (if (equal arg '(16)) (current-time)
  15311. ;; Preserve `this-command' and `last-command'.
  15312. (let ((this-command this-command)
  15313. (last-command last-command))
  15314. (org-read-date
  15315. arg 'totime nil nil default-time default-input
  15316. inactive))))))
  15317. (cond
  15318. ((and ts
  15319. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  15320. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  15321. (insert "--")
  15322. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  15323. (ts
  15324. ;; Make sure we're on a timestamp. When in the middle of a date
  15325. ;; range, move arbitrarily to range end.
  15326. (unless (org-at-timestamp-p t)
  15327. (skip-chars-forward "-")
  15328. (org-at-timestamp-p t))
  15329. (replace-match "")
  15330. (setq org-last-changed-timestamp
  15331. (org-insert-time-stamp
  15332. time (or org-time-was-given arg)
  15333. inactive nil nil (list org-end-time-was-given)))
  15334. (when repeater
  15335. (backward-char)
  15336. (insert " " repeater)
  15337. (setq org-last-changed-timestamp
  15338. (concat (substring org-last-inserted-timestamp 0 -1)
  15339. " " repeater ">")))
  15340. (message "Timestamp updated"))
  15341. ((equal arg '(16)) (org-insert-time-stamp time t inactive))
  15342. (t (org-insert-time-stamp
  15343. time (or org-time-was-given arg) inactive nil nil
  15344. (list org-end-time-was-given))))))
  15345. ;; FIXME: can we use this for something else, like computing time differences?
  15346. (defun org-get-compact-tod (s)
  15347. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  15348. (let* ((t1 (match-string 1 s))
  15349. (h1 (string-to-number (match-string 2 s)))
  15350. (m1 (string-to-number (match-string 3 s)))
  15351. (t2 (and (match-end 4) (match-string 5 s)))
  15352. (h2 (and t2 (string-to-number (match-string 6 s))))
  15353. (m2 (and t2 (string-to-number (match-string 7 s))))
  15354. dh dm)
  15355. (if (not t2)
  15356. t1
  15357. (setq dh (- h2 h1) dm (- m2 m1))
  15358. (when (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  15359. (concat t1 "+" (number-to-string dh)
  15360. (and (/= 0 dm) (format ":%02d" dm)))))))
  15361. (defun org-time-stamp-inactive (&optional arg)
  15362. "Insert an inactive time stamp.
  15363. An inactive time stamp is enclosed in square brackets instead of angle
  15364. brackets. It is inactive in the sense that it does not trigger agenda entries,
  15365. does not link to the calendar and cannot be changed with the S-cursor keys.
  15366. So these are more for recording a certain time/date."
  15367. (interactive "P")
  15368. (org-time-stamp arg 'inactive))
  15369. (defvar org-date-ovl (make-overlay 1 1))
  15370. (overlay-put org-date-ovl 'face 'org-date-selected)
  15371. (org-detach-overlay org-date-ovl)
  15372. (defvar org-ans1) ; dynamically scoped parameter
  15373. (defvar org-ans2) ; dynamically scoped parameter
  15374. (defvar org-plain-time-of-day-regexp) ; defined below
  15375. (defvar org-overriding-default-time nil) ; dynamically scoped
  15376. (defvar org-read-date-overlay nil)
  15377. (defvar org-dcst nil) ; dynamically scoped
  15378. (defvar org-read-date-history nil)
  15379. (defvar org-read-date-final-answer nil)
  15380. (defvar org-read-date-analyze-futurep nil)
  15381. (defvar org-read-date-analyze-forced-year nil)
  15382. (defvar org-read-date-inactive)
  15383. (defvar org-read-date-minibuffer-local-map
  15384. (let* ((map (make-sparse-keymap)))
  15385. (set-keymap-parent map minibuffer-local-map)
  15386. (org-defkey map (kbd ".")
  15387. (lambda () (interactive)
  15388. ;; Are we at the beginning of the prompt?
  15389. (if (org-looking-back "^[^:]+: "
  15390. (let ((inhibit-field-text-motion t))
  15391. (line-beginning-position)))
  15392. (org-eval-in-calendar '(calendar-goto-today))
  15393. (insert "."))))
  15394. (org-defkey map (kbd "C-.")
  15395. (lambda () (interactive)
  15396. (org-eval-in-calendar '(calendar-goto-today))))
  15397. (org-defkey map [(meta shift left)]
  15398. (lambda () (interactive)
  15399. (org-eval-in-calendar '(calendar-backward-month 1))))
  15400. (org-defkey map [(meta shift right)]
  15401. (lambda () (interactive)
  15402. (org-eval-in-calendar '(calendar-forward-month 1))))
  15403. (org-defkey map [(meta shift up)]
  15404. (lambda () (interactive)
  15405. (org-eval-in-calendar '(calendar-backward-year 1))))
  15406. (org-defkey map [(meta shift down)]
  15407. (lambda () (interactive)
  15408. (org-eval-in-calendar '(calendar-forward-year 1))))
  15409. (org-defkey map [?\e (shift left)]
  15410. (lambda () (interactive)
  15411. (org-eval-in-calendar '(calendar-backward-month 1))))
  15412. (org-defkey map [?\e (shift right)]
  15413. (lambda () (interactive)
  15414. (org-eval-in-calendar '(calendar-forward-month 1))))
  15415. (org-defkey map [?\e (shift up)]
  15416. (lambda () (interactive)
  15417. (org-eval-in-calendar '(calendar-backward-year 1))))
  15418. (org-defkey map [?\e (shift down)]
  15419. (lambda () (interactive)
  15420. (org-eval-in-calendar '(calendar-forward-year 1))))
  15421. (org-defkey map [(shift up)]
  15422. (lambda () (interactive)
  15423. (org-eval-in-calendar '(calendar-backward-week 1))))
  15424. (org-defkey map [(shift down)]
  15425. (lambda () (interactive)
  15426. (org-eval-in-calendar '(calendar-forward-week 1))))
  15427. (org-defkey map [(shift left)]
  15428. (lambda () (interactive)
  15429. (org-eval-in-calendar '(calendar-backward-day 1))))
  15430. (org-defkey map [(shift right)]
  15431. (lambda () (interactive)
  15432. (org-eval-in-calendar '(calendar-forward-day 1))))
  15433. (org-defkey map "!"
  15434. (lambda () (interactive)
  15435. (org-eval-in-calendar '(diary-view-entries))
  15436. (message "")))
  15437. (org-defkey map ">"
  15438. (lambda () (interactive)
  15439. (org-eval-in-calendar '(calendar-scroll-left 1))))
  15440. (org-defkey map "<"
  15441. (lambda () (interactive)
  15442. (org-eval-in-calendar '(calendar-scroll-right 1))))
  15443. (org-defkey map "\C-v"
  15444. (lambda () (interactive)
  15445. (org-eval-in-calendar
  15446. '(calendar-scroll-left-three-months 1))))
  15447. (org-defkey map "\M-v"
  15448. (lambda () (interactive)
  15449. (org-eval-in-calendar
  15450. '(calendar-scroll-right-three-months 1))))
  15451. map)
  15452. "Keymap for minibuffer commands when using `org-read-date'.")
  15453. (defvar org-def)
  15454. (defvar org-defdecode)
  15455. (defvar org-with-time)
  15456. (defvar calendar-setup) ; Dynamically scoped.
  15457. (defun org-read-date (&optional with-time to-time from-string prompt
  15458. default-time default-input inactive)
  15459. "Read a date, possibly a time, and make things smooth for the user.
  15460. The prompt will suggest to enter an ISO date, but you can also enter anything
  15461. which will at least partially be understood by `parse-time-string'.
  15462. Unrecognized parts of the date will default to the current day, month, year,
  15463. hour and minute. If this command is called to replace a timestamp at point,
  15464. or to enter the second timestamp of a range, the default time is taken
  15465. from the existing stamp. Furthermore, the command prefers the future,
  15466. so if you are giving a date where the year is not given, and the day-month
  15467. combination is already past in the current year, it will assume you
  15468. mean next year. For details, see the manual. A few examples:
  15469. 3-2-5 --> 2003-02-05
  15470. feb 15 --> currentyear-02-15
  15471. 2/15 --> currentyear-02-15
  15472. sep 12 9 --> 2009-09-12
  15473. 12:45 --> today 12:45
  15474. 22 sept 0:34 --> currentyear-09-22 0:34
  15475. 12 --> currentyear-currentmonth-12
  15476. Fri --> nearest Friday after today
  15477. -Tue --> last Tuesday
  15478. etc.
  15479. Furthermore you can specify a relative date by giving, as the *first* thing
  15480. in the input: a plus/minus sign, a number and a letter [hdwmy] to indicate
  15481. change in days weeks, months, years.
  15482. With a single plus or minus, the date is relative to today. With a double
  15483. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  15484. +4d --> four days from today
  15485. +4 --> same as above
  15486. +2w --> two weeks from today
  15487. ++5 --> five days from default date
  15488. The function understands only English month and weekday abbreviations.
  15489. While prompting, a calendar is popped up - you can also select the
  15490. date with the mouse (button 1). The calendar shows a period of three
  15491. months. To scroll it to other months, use the keys `>' and `<'.
  15492. If you don't like the calendar, turn it off with
  15493. (setq org-read-date-popup-calendar nil)
  15494. With optional argument TO-TIME, the date will immediately be converted
  15495. to an internal time.
  15496. With an optional argument WITH-TIME, the prompt will suggest to
  15497. also insert a time. Note that when WITH-TIME is not set, you can
  15498. still enter a time, and this function will inform the calling routine
  15499. about this change. The calling routine may then choose to change the
  15500. format used to insert the time stamp into the buffer to include the time.
  15501. With optional argument FROM-STRING, read from this string instead from
  15502. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  15503. the time/date that is used for everything that is not specified by the
  15504. user."
  15505. (require 'parse-time)
  15506. (let* ((org-with-time with-time)
  15507. (org-time-stamp-rounding-minutes
  15508. (if (equal org-with-time '(16))
  15509. '(0 0)
  15510. org-time-stamp-rounding-minutes))
  15511. (org-dcst org-display-custom-times)
  15512. (ct (org-current-time))
  15513. (org-def (or org-overriding-default-time default-time ct))
  15514. (org-defdecode (decode-time org-def))
  15515. (cur-frame (selected-frame))
  15516. (mouse-autoselect-window nil) ; Don't let the mouse jump
  15517. (calendar-setup
  15518. (and (eq calendar-setup 'calendar-only) 'calendar-only))
  15519. (calendar-move-hook nil)
  15520. (calendar-view-diary-initially-flag nil)
  15521. (calendar-view-holidays-initially-flag nil)
  15522. ans (org-ans0 "") org-ans1 org-ans2 final cal-frame)
  15523. ;; Rationalize `org-def' and `org-defdecode', if required.
  15524. (when (< (nth 2 org-defdecode) org-extend-today-until)
  15525. (setf (nth 2 org-defdecode) -1)
  15526. (setf (nth 1 org-defdecode) 59)
  15527. (setq org-def (apply #'encode-time org-defdecode))
  15528. (setq org-defdecode (decode-time org-def)))
  15529. (let* ((timestr (format-time-string
  15530. (if org-with-time "%Y-%m-%d %H:%M" "%Y-%m-%d")
  15531. org-def))
  15532. (prompt (concat (if prompt (concat prompt " ") "")
  15533. (format "Date+time [%s]: " timestr))))
  15534. (cond
  15535. (from-string (setq ans from-string))
  15536. (org-read-date-popup-calendar
  15537. (save-excursion
  15538. (save-window-excursion
  15539. (calendar)
  15540. (when (eq calendar-setup 'calendar-only)
  15541. (setq cal-frame
  15542. (window-frame (get-buffer-window "*Calendar*" 'visible)))
  15543. (select-frame cal-frame))
  15544. (org-eval-in-calendar '(setq cursor-type nil) t)
  15545. (unwind-protect
  15546. (progn
  15547. (calendar-forward-day (- (time-to-days org-def)
  15548. (calendar-absolute-from-gregorian
  15549. (calendar-current-date))))
  15550. (org-eval-in-calendar nil t)
  15551. (let* ((old-map (current-local-map))
  15552. (map (copy-keymap calendar-mode-map))
  15553. (minibuffer-local-map
  15554. (copy-keymap org-read-date-minibuffer-local-map)))
  15555. (org-defkey map (kbd "RET") 'org-calendar-select)
  15556. (org-defkey map [mouse-1] 'org-calendar-select-mouse)
  15557. (org-defkey map [mouse-2] 'org-calendar-select-mouse)
  15558. (unwind-protect
  15559. (progn
  15560. (use-local-map map)
  15561. (setq org-read-date-inactive inactive)
  15562. (add-hook 'post-command-hook 'org-read-date-display)
  15563. (setq org-ans0
  15564. (read-string prompt
  15565. default-input
  15566. 'org-read-date-history
  15567. nil))
  15568. ;; org-ans0: from prompt
  15569. ;; org-ans1: from mouse click
  15570. ;; org-ans2: from calendar motion
  15571. (setq ans
  15572. (concat org-ans0 " " (or org-ans1 org-ans2))))
  15573. (remove-hook 'post-command-hook 'org-read-date-display)
  15574. (use-local-map old-map)
  15575. (when org-read-date-overlay
  15576. (delete-overlay org-read-date-overlay)
  15577. (setq org-read-date-overlay nil)))))
  15578. (bury-buffer "*Calendar*")
  15579. (when cal-frame
  15580. (delete-frame cal-frame)
  15581. (select-frame-set-input-focus cur-frame))))))
  15582. (t ; Naked prompt only
  15583. (unwind-protect
  15584. (setq ans (read-string prompt default-input
  15585. 'org-read-date-history timestr))
  15586. (when org-read-date-overlay
  15587. (delete-overlay org-read-date-overlay)
  15588. (setq org-read-date-overlay nil))))))
  15589. (setq final (org-read-date-analyze ans org-def org-defdecode))
  15590. (when org-read-date-analyze-forced-year
  15591. (message "Year was forced into %s"
  15592. (if org-read-date-force-compatible-dates
  15593. "compatible range (1970-2037)"
  15594. "range representable on this machine"))
  15595. (ding))
  15596. ;; One round trip to get rid of 34th of August and stuff like that....
  15597. (setq final (decode-time (apply 'encode-time final)))
  15598. (setq org-read-date-final-answer ans)
  15599. (if to-time
  15600. (apply 'encode-time final)
  15601. (if (and (boundp 'org-time-was-given) org-time-was-given)
  15602. (format "%04d-%02d-%02d %02d:%02d"
  15603. (nth 5 final) (nth 4 final) (nth 3 final)
  15604. (nth 2 final) (nth 1 final))
  15605. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  15606. (defun org-read-date-display ()
  15607. "Display the current date prompt interpretation in the minibuffer."
  15608. (when org-read-date-display-live
  15609. (when org-read-date-overlay
  15610. (delete-overlay org-read-date-overlay))
  15611. (when (minibufferp (current-buffer))
  15612. (save-excursion
  15613. (end-of-line 1)
  15614. (while (not (equal (buffer-substring
  15615. (max (point-min) (- (point) 4)) (point))
  15616. " "))
  15617. (insert " ")))
  15618. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  15619. " " (or org-ans1 org-ans2)))
  15620. (org-end-time-was-given nil)
  15621. (f (org-read-date-analyze ans org-def org-defdecode))
  15622. (fmts (if org-dcst
  15623. org-time-stamp-custom-formats
  15624. org-time-stamp-formats))
  15625. (fmt (if (or org-with-time
  15626. (and (boundp 'org-time-was-given) org-time-was-given))
  15627. (cdr fmts)
  15628. (car fmts)))
  15629. (txt (format-time-string fmt (apply 'encode-time f)))
  15630. (txt (if org-read-date-inactive (concat "[" (substring txt 1 -1) "]") txt))
  15631. (txt (concat "=> " txt)))
  15632. (when (and org-end-time-was-given
  15633. (string-match org-plain-time-of-day-regexp txt))
  15634. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  15635. org-end-time-was-given
  15636. (substring txt (match-end 0)))))
  15637. (when org-read-date-analyze-futurep
  15638. (setq txt (concat txt " (=>F)")))
  15639. (setq org-read-date-overlay
  15640. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  15641. (org-overlay-display org-read-date-overlay txt 'secondary-selection)))))
  15642. (defun org-read-date-analyze (ans def defdecode)
  15643. "Analyze the combined answer of the date prompt."
  15644. ;; FIXME: cleanup and comment
  15645. ;; Pass `current-time' result to `decode-time' (instead of calling
  15646. ;; without arguments) so that only `current-time' has to be
  15647. ;; overriden in tests.
  15648. (let ((org-def def)
  15649. (org-defdecode defdecode)
  15650. (nowdecode (decode-time (current-time)))
  15651. delta deltan deltaw deltadef year month day
  15652. hour minute second wday pm h2 m2 tl wday1
  15653. iso-year iso-weekday iso-week iso-date futurep kill-year)
  15654. (setq org-read-date-analyze-futurep nil
  15655. org-read-date-analyze-forced-year nil)
  15656. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  15657. (setq ans "+0"))
  15658. (when (setq delta (org-read-date-get-relative ans (current-time) org-def))
  15659. (setq ans (replace-match "" t t ans)
  15660. deltan (car delta)
  15661. deltaw (nth 1 delta)
  15662. deltadef (nth 2 delta)))
  15663. ;; Check if there is an iso week date in there. If yes, store the
  15664. ;; info and postpone interpreting it until the rest of the parsing
  15665. ;; is done.
  15666. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  15667. (setq iso-year (when (match-end 1)
  15668. (org-small-year-to-year
  15669. (string-to-number (match-string 1 ans))))
  15670. iso-weekday (when (match-end 3)
  15671. (string-to-number (match-string 3 ans)))
  15672. iso-week (string-to-number (match-string 2 ans)))
  15673. (setq ans (replace-match "" t t ans)))
  15674. ;; Help matching ISO dates with single digit month or day, like 2006-8-11.
  15675. (when (string-match
  15676. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  15677. (setq year (if (match-end 2)
  15678. (string-to-number (match-string 2 ans))
  15679. (progn (setq kill-year t)
  15680. (string-to-number (format-time-string "%Y"))))
  15681. month (string-to-number (match-string 3 ans))
  15682. day (string-to-number (match-string 4 ans)))
  15683. (setq year (org-small-year-to-year year))
  15684. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  15685. t nil ans)))
  15686. ;; Help matching dotted european dates
  15687. (when (string-match
  15688. "^ *\\(3[01]\\|0?[1-9]\\|[12][0-9]\\)\\. ?\\(0?[1-9]\\|1[012]\\)\\.\\( ?[1-9][0-9]\\{3\\}\\)?" ans)
  15689. (setq year (if (match-end 3) (string-to-number (match-string 3 ans))
  15690. (setq kill-year t)
  15691. (string-to-number (format-time-string "%Y")))
  15692. day (string-to-number (match-string 1 ans))
  15693. month (string-to-number (match-string 2 ans))
  15694. ans (replace-match (format "%04d-%02d-%02d" year month day)
  15695. t nil ans)))
  15696. ;; Help matching american dates, like 5/30 or 5/30/7
  15697. (when (string-match
  15698. "^ *\\(0?[1-9]\\|1[012]\\)/\\(0?[1-9]\\|[12][0-9]\\|3[01]\\)\\(/\\([0-9]+\\)\\)?\\([^/0-9]\\|$\\)" ans)
  15699. (setq year (if (match-end 4)
  15700. (string-to-number (match-string 4 ans))
  15701. (progn (setq kill-year t)
  15702. (string-to-number (format-time-string "%Y"))))
  15703. month (string-to-number (match-string 1 ans))
  15704. day (string-to-number (match-string 2 ans)))
  15705. (setq year (org-small-year-to-year year))
  15706. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  15707. t nil ans)))
  15708. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  15709. ;; If there is a time with am/pm, and *no* time without it, we convert
  15710. ;; so that matching will be successful.
  15711. (cl-loop for i from 1 to 2 do ; twice, for end time as well
  15712. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  15713. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  15714. (setq hour (string-to-number (match-string 1 ans))
  15715. minute (if (match-end 3)
  15716. (string-to-number (match-string 3 ans))
  15717. 0)
  15718. pm (equal ?p
  15719. (string-to-char (downcase (match-string 4 ans)))))
  15720. (if (and (= hour 12) (not pm))
  15721. (setq hour 0)
  15722. (when (and pm (< hour 12)) (setq hour (+ 12 hour))))
  15723. (setq ans (replace-match (format "%02d:%02d" hour minute)
  15724. t t ans))))
  15725. ;; Check if a time range is given as a duration
  15726. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  15727. (setq hour (string-to-number (match-string 1 ans))
  15728. h2 (+ hour (string-to-number (match-string 3 ans)))
  15729. minute (string-to-number (match-string 2 ans))
  15730. m2 (+ minute (if (match-end 5) (string-to-number
  15731. (match-string 5 ans))0)))
  15732. (when (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  15733. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  15734. t t ans)))
  15735. ;; Check if there is a time range
  15736. (when (boundp 'org-end-time-was-given)
  15737. (setq org-time-was-given nil)
  15738. (when (and (string-match org-plain-time-of-day-regexp ans)
  15739. (match-end 8))
  15740. (setq org-end-time-was-given (match-string 8 ans))
  15741. (setq ans (concat (substring ans 0 (match-beginning 7))
  15742. (substring ans (match-end 7))))))
  15743. (setq tl (parse-time-string ans)
  15744. day (or (nth 3 tl) (nth 3 org-defdecode))
  15745. month
  15746. (cond ((nth 4 tl))
  15747. ((not org-read-date-prefer-future) (nth 4 org-defdecode))
  15748. ;; Day was specified. Make sure DAY+MONTH
  15749. ;; combination happens in the future.
  15750. ((nth 3 tl)
  15751. (setq futurep t)
  15752. (if (< day (nth 3 nowdecode)) (1+ (nth 4 nowdecode))
  15753. (nth 4 nowdecode)))
  15754. (t (nth 4 org-defdecode)))
  15755. year
  15756. (cond ((and (not kill-year) (nth 5 tl)))
  15757. ((not org-read-date-prefer-future) (nth 5 org-defdecode))
  15758. ;; Month was guessed in the future and is at least
  15759. ;; equal to NOWDECODE's. Fix year accordingly.
  15760. (futurep
  15761. (if (or (> month (nth 4 nowdecode))
  15762. (>= day (nth 3 nowdecode)))
  15763. (nth 5 nowdecode)
  15764. (1+ (nth 5 nowdecode))))
  15765. ;; Month was specified. Make sure MONTH+YEAR
  15766. ;; combination happens in the future.
  15767. ((nth 4 tl)
  15768. (setq futurep t)
  15769. (cond ((> month (nth 4 nowdecode)) (nth 5 nowdecode))
  15770. ((< month (nth 4 nowdecode)) (1+ (nth 5 nowdecode)))
  15771. ((< day (nth 3 nowdecode)) (1+ (nth 5 nowdecode)))
  15772. (t (nth 5 nowdecode))))
  15773. (t (nth 5 org-defdecode)))
  15774. hour (or (nth 2 tl) (nth 2 org-defdecode))
  15775. minute (or (nth 1 tl) (nth 1 org-defdecode))
  15776. second (or (nth 0 tl) 0)
  15777. wday (nth 6 tl))
  15778. (when (and (eq org-read-date-prefer-future 'time)
  15779. (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
  15780. (equal day (nth 3 nowdecode))
  15781. (equal month (nth 4 nowdecode))
  15782. (equal year (nth 5 nowdecode))
  15783. (nth 2 tl)
  15784. (or (< (nth 2 tl) (nth 2 nowdecode))
  15785. (and (= (nth 2 tl) (nth 2 nowdecode))
  15786. (nth 1 tl)
  15787. (< (nth 1 tl) (nth 1 nowdecode)))))
  15788. (setq day (1+ day)
  15789. futurep t))
  15790. ;; Special date definitions below
  15791. (cond
  15792. (iso-week
  15793. ;; There was an iso week
  15794. (require 'cal-iso)
  15795. (setq futurep nil)
  15796. (setq year (or iso-year year)
  15797. day (or iso-weekday wday 1)
  15798. wday nil ; to make sure that the trigger below does not match
  15799. iso-date (calendar-gregorian-from-absolute
  15800. (calendar-iso-to-absolute
  15801. (list iso-week day year))))
  15802. ; FIXME: Should we also push ISO weeks into the future?
  15803. ; (when (and org-read-date-prefer-future
  15804. ; (not iso-year)
  15805. ; (< (calendar-absolute-from-gregorian iso-date)
  15806. ; (time-to-days (current-time))))
  15807. ; (setq year (1+ year)
  15808. ; iso-date (calendar-gregorian-from-absolute
  15809. ; (calendar-iso-to-absolute
  15810. ; (list iso-week day year)))))
  15811. (setq month (car iso-date)
  15812. year (nth 2 iso-date)
  15813. day (nth 1 iso-date)))
  15814. (deltan
  15815. (setq futurep nil)
  15816. (unless deltadef
  15817. ;; Pass `current-time' result to `decode-time' (instead of
  15818. ;; calling without arguments) so that only `current-time' has
  15819. ;; to be overriden in tests.
  15820. (let ((now (decode-time (current-time))))
  15821. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  15822. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  15823. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  15824. ((equal deltaw "m") (setq month (+ month deltan)))
  15825. ((equal deltaw "y") (setq year (+ year deltan)))))
  15826. ((and wday (not (nth 3 tl)))
  15827. ;; Weekday was given, but no day, so pick that day in the week
  15828. ;; on or after the derived date.
  15829. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  15830. (unless (equal wday wday1)
  15831. (setq day (+ day (% (- wday wday1 -7) 7))))))
  15832. (when (and (boundp 'org-time-was-given)
  15833. (nth 2 tl))
  15834. (setq org-time-was-given t))
  15835. (when (< year 100) (setq year (+ 2000 year)))
  15836. ;; Check of the date is representable
  15837. (if org-read-date-force-compatible-dates
  15838. (progn
  15839. (when (< year 1970)
  15840. (setq year 1970 org-read-date-analyze-forced-year t))
  15841. (when (> year 2037)
  15842. (setq year 2037 org-read-date-analyze-forced-year t)))
  15843. (condition-case nil
  15844. (ignore (encode-time second minute hour day month year))
  15845. (error
  15846. (setq year (nth 5 org-defdecode))
  15847. (setq org-read-date-analyze-forced-year t))))
  15848. (setq org-read-date-analyze-futurep futurep)
  15849. (list second minute hour day month year)))
  15850. (defvar parse-time-weekdays)
  15851. (defun org-read-date-get-relative (s today default)
  15852. "Check string S for special relative date string.
  15853. TODAY and DEFAULT are internal times, for today and for a default.
  15854. Return shift list (N what def-flag)
  15855. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  15856. N is the number of WHATs to shift.
  15857. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  15858. the DEFAULT date rather than TODAY."
  15859. (require 'parse-time)
  15860. (when (and
  15861. (string-match
  15862. (concat
  15863. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  15864. "\\([0-9]+\\)?"
  15865. "\\([hdwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  15866. "\\([ \t]\\|$\\)") s)
  15867. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  15868. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  15869. (string-to-char (substring (match-string 1 s) -1))
  15870. ?+))
  15871. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  15872. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  15873. (what (if (match-end 3) (match-string 3 s) "d"))
  15874. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  15875. (date (if rel default today))
  15876. (wday (nth 6 (decode-time date)))
  15877. delta)
  15878. (if wday1
  15879. (progn
  15880. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  15881. (when (= delta 0) (setq delta 7))
  15882. (when (= dir ?-)
  15883. (setq delta (- delta 7))
  15884. (when (= delta 0) (setq delta -7)))
  15885. (when (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  15886. (list delta "d" rel))
  15887. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  15888. (defun org-order-calendar-date-args (arg1 arg2 arg3)
  15889. "Turn a user-specified date into the internal representation.
  15890. The internal representation needed by the calendar is (month day year).
  15891. This is a wrapper to handle the brain-dead convention in calendar that
  15892. user function argument order change dependent on argument order."
  15893. (if (boundp 'calendar-date-style)
  15894. (cond
  15895. ((eq calendar-date-style 'american)
  15896. (list arg1 arg2 arg3))
  15897. ((eq calendar-date-style 'european)
  15898. (list arg2 arg1 arg3))
  15899. ((eq calendar-date-style 'iso)
  15900. (list arg2 arg3 arg1)))
  15901. (org-no-warnings ;; european-calendar-style is obsolete as of version 23.1
  15902. (if (org-bound-and-true-p european-calendar-style)
  15903. (list arg2 arg1 arg3)
  15904. (list arg1 arg2 arg3)))))
  15905. (defun org-eval-in-calendar (form &optional keepdate)
  15906. "Eval FORM in the calendar window and return to current window.
  15907. Unless KEEPDATE is non-nil, update `org-ans2' to the cursor date."
  15908. (let ((sf (selected-frame))
  15909. (sw (selected-window)))
  15910. (select-window (get-buffer-window "*Calendar*" t))
  15911. (eval form)
  15912. (when (and (not keepdate) (calendar-cursor-to-date))
  15913. (let* ((date (calendar-cursor-to-date))
  15914. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15915. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  15916. (move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  15917. (select-window sw)
  15918. (org-select-frame-set-input-focus sf)))
  15919. (defun org-calendar-select ()
  15920. "Return to `org-read-date' with the date currently selected.
  15921. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  15922. (interactive)
  15923. (when (calendar-cursor-to-date)
  15924. (let* ((date (calendar-cursor-to-date))
  15925. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15926. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  15927. (when (active-minibuffer-window) (exit-minibuffer))))
  15928. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  15929. "Insert a date stamp for the date given by the internal TIME.
  15930. See `format-time-string' for the format of TIME.
  15931. WITH-HM means use the stamp format that includes the time of the day.
  15932. INACTIVE means use square brackets instead of angular ones, so that the
  15933. stamp will not contribute to the agenda.
  15934. PRE and POST are optional strings to be inserted before and after the
  15935. stamp.
  15936. The command returns the inserted time stamp."
  15937. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  15938. stamp)
  15939. (when inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  15940. (insert-before-markers (or pre ""))
  15941. (when (listp extra)
  15942. (setq extra (car extra))
  15943. (if (and (stringp extra)
  15944. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  15945. (setq extra (format "-%02d:%02d"
  15946. (string-to-number (match-string 1 extra))
  15947. (string-to-number (match-string 2 extra))))
  15948. (setq extra nil)))
  15949. (when extra
  15950. (setq fmt (concat (substring fmt 0 -1) extra (substring fmt -1))))
  15951. (insert-before-markers (setq stamp (format-time-string fmt time)))
  15952. (insert-before-markers (or post ""))
  15953. (setq org-last-inserted-timestamp stamp)))
  15954. (defun org-toggle-time-stamp-overlays ()
  15955. "Toggle the use of custom time stamp formats."
  15956. (interactive)
  15957. (setq org-display-custom-times (not org-display-custom-times))
  15958. (unless org-display-custom-times
  15959. (let ((p (point-min)) (bmp (buffer-modified-p)))
  15960. (while (setq p (next-single-property-change p 'display))
  15961. (when (and (get-text-property p 'display)
  15962. (eq (get-text-property p 'face) 'org-date))
  15963. (remove-text-properties
  15964. p (setq p (next-single-property-change p 'display))
  15965. '(display t))))
  15966. (set-buffer-modified-p bmp)))
  15967. (when (featurep 'xemacs)
  15968. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  15969. (org-restart-font-lock)
  15970. (setq org-table-may-need-update t)
  15971. (if org-display-custom-times
  15972. (message "Time stamps are overlaid with custom format")
  15973. (message "Time stamp overlays removed")))
  15974. (defun org-display-custom-time (beg end)
  15975. "Overlay modified time stamp format over timestamp between BEG and END."
  15976. (let* ((ts (buffer-substring beg end))
  15977. t1 w1 with-hm tf time str w2 (off 0))
  15978. (save-match-data
  15979. (setq t1 (org-parse-time-string ts t))
  15980. (when (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)?\\'" ts)
  15981. (setq off (- (match-end 0) (match-beginning 0)))))
  15982. (setq end (- end off))
  15983. (setq w1 (- end beg)
  15984. with-hm (and (nth 1 t1) (nth 2 t1))
  15985. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  15986. time (org-fix-decoded-time t1)
  15987. str (org-add-props
  15988. (format-time-string
  15989. (substring tf 1 -1) (apply 'encode-time time))
  15990. nil 'mouse-face 'highlight)
  15991. w2 (length str))
  15992. (unless (= w2 w1)
  15993. (add-text-properties (1+ beg) (+ 2 beg)
  15994. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  15995. (if (featurep 'xemacs)
  15996. (progn
  15997. (put-text-property beg end 'invisible t)
  15998. (put-text-property beg end 'end-glyph (make-glyph str)))
  15999. (put-text-property beg end 'display str))))
  16000. (defun org-fix-decoded-time (time)
  16001. "Set 0 instead of nil for the first 6 elements of time.
  16002. Don't touch the rest."
  16003. (let ((n 0))
  16004. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  16005. (define-obsolete-function-alias 'org-days-to-time 'org-time-stamp-to-now "24.4")
  16006. (defun org-time-stamp-to-now (timestamp-string &optional seconds)
  16007. "Difference between TIMESTAMP-STRING and now in days.
  16008. If SECONDS is non-nil, return the difference in seconds."
  16009. (let ((fdiff (if seconds 'org-float-time 'time-to-days)))
  16010. (- (funcall fdiff (org-time-string-to-time timestamp-string))
  16011. (funcall fdiff (current-time)))))
  16012. (defun org-deadline-close (timestamp-string &optional ndays)
  16013. "Is the time in TIMESTAMP-STRING close to the current date?"
  16014. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  16015. (and (< (org-time-stamp-to-now timestamp-string) ndays)
  16016. (not (org-entry-is-done-p))))
  16017. (defun org-get-wdays (ts &optional delay zero-delay)
  16018. "Get the deadline lead time appropriate for timestring TS.
  16019. When DELAY is non-nil, get the delay time for scheduled items
  16020. instead of the deadline lead time. When ZERO-DELAY is non-nil
  16021. and `org-scheduled-delay-days' is 0, enforce 0 as the delay,
  16022. don't try to find the delay cookie in the scheduled timestamp."
  16023. (let ((tv (if delay org-scheduled-delay-days
  16024. org-deadline-warning-days)))
  16025. (cond
  16026. ((or (and delay (< tv 0))
  16027. (and delay zero-delay (<= tv 0))
  16028. (and (not delay) (<= tv 0)))
  16029. ;; Enforce this value no matter what
  16030. (- tv))
  16031. ((string-match "-\\([0-9]+\\)\\([hdwmy]\\)\\(\\'\\|>\\| \\)" ts)
  16032. ;; lead time is specified.
  16033. (floor (* (string-to-number (match-string 1 ts))
  16034. (cdr (assoc (match-string 2 ts)
  16035. '(("d" . 1) ("w" . 7)
  16036. ("m" . 30.4) ("y" . 365.25)
  16037. ("h" . 0.041667)))))))
  16038. ;; go for the default.
  16039. (t tv))))
  16040. (defun org-calendar-select-mouse (ev)
  16041. "Return to `org-read-date' with the date currently selected.
  16042. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  16043. (interactive "e")
  16044. (mouse-set-point ev)
  16045. (when (calendar-cursor-to-date)
  16046. (let* ((date (calendar-cursor-to-date))
  16047. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  16048. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  16049. (when (active-minibuffer-window) (exit-minibuffer))))
  16050. (defun org-check-deadlines (ndays)
  16051. "Check if there are any deadlines due or past due.
  16052. A deadline is considered due if it happens within `org-deadline-warning-days'
  16053. days from today's date. If the deadline appears in an entry marked DONE,
  16054. it is not shown. The prefix arg NDAYS can be used to test that many
  16055. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  16056. (interactive "P")
  16057. (let* ((org-warn-days
  16058. (cond
  16059. ((equal ndays '(4)) 100000)
  16060. (ndays (prefix-numeric-value ndays))
  16061. (t (abs org-deadline-warning-days))))
  16062. (case-fold-search nil)
  16063. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  16064. (callback
  16065. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  16066. (message "%d deadlines past-due or due within %d days"
  16067. (org-occur regexp nil callback)
  16068. org-warn-days)))
  16069. (defsubst org-re-timestamp (type)
  16070. "Return a regexp for timestamp TYPE.
  16071. Allowed values for TYPE are:
  16072. all: all timestamps
  16073. active: only active timestamps (<...>)
  16074. inactive: only inactive timestamps ([...])
  16075. scheduled: only scheduled timestamps
  16076. deadline: only deadline timestamps
  16077. closed: only closed time-stamps
  16078. When TYPE is nil, fall back on returning a regexp that matches
  16079. both scheduled and deadline timestamps."
  16080. (cl-case type
  16081. (all org-ts-regexp-both)
  16082. (active org-ts-regexp)
  16083. (inactive org-ts-regexp-inactive)
  16084. (scheduled org-scheduled-time-regexp)
  16085. (deadline org-deadline-time-regexp)
  16086. (closed org-closed-time-regexp)
  16087. (otherwise
  16088. (concat "\\<"
  16089. (regexp-opt (list org-deadline-string org-scheduled-string))
  16090. " *<\\([^>]+\\)>"))))
  16091. (defun org-check-before-date (d)
  16092. "Check if there are deadlines or scheduled entries before date D."
  16093. (interactive (list (org-read-date)))
  16094. (let* ((case-fold-search nil)
  16095. (regexp (org-re-timestamp org-ts-type))
  16096. (ts-type org-ts-type)
  16097. (callback
  16098. (lambda ()
  16099. (let ((match (match-string 1)))
  16100. (and (if (memq ts-type '(active inactive all))
  16101. (eq (org-element-type (save-excursion
  16102. (backward-char)
  16103. (org-element-context)))
  16104. 'timestamp)
  16105. (org-at-planning-p))
  16106. (time-less-p
  16107. (org-time-string-to-time match)
  16108. (org-time-string-to-time d)))))))
  16109. (message "%d entries before %s"
  16110. (org-occur regexp nil callback)
  16111. d)))
  16112. (defun org-check-after-date (d)
  16113. "Check if there are deadlines or scheduled entries after date D."
  16114. (interactive (list (org-read-date)))
  16115. (let* ((case-fold-search nil)
  16116. (regexp (org-re-timestamp org-ts-type))
  16117. (ts-type org-ts-type)
  16118. (callback
  16119. (lambda ()
  16120. (let ((match (match-string 1)))
  16121. (and (if (memq ts-type '(active inactive all))
  16122. (eq (org-element-type (save-excursion
  16123. (backward-char)
  16124. (org-element-context)))
  16125. 'timestamp)
  16126. (org-at-planning-p))
  16127. (not (time-less-p
  16128. (org-time-string-to-time match)
  16129. (org-time-string-to-time d))))))))
  16130. (message "%d entries after %s"
  16131. (org-occur regexp nil callback)
  16132. d)))
  16133. (defun org-check-dates-range (start-date end-date)
  16134. "Check for deadlines/scheduled entries between START-DATE and END-DATE."
  16135. (interactive (list (org-read-date nil nil nil "Range starts")
  16136. (org-read-date nil nil nil "Range end")))
  16137. (let ((case-fold-search nil)
  16138. (regexp (org-re-timestamp org-ts-type))
  16139. (callback
  16140. (let ((type org-ts-type))
  16141. (lambda ()
  16142. (let ((match (match-string 1)))
  16143. (and
  16144. (if (memq type '(active inactive all))
  16145. (eq (org-element-type (save-excursion
  16146. (backward-char)
  16147. (org-element-context)))
  16148. 'timestamp)
  16149. (org-at-planning-p))
  16150. (not (time-less-p
  16151. (org-time-string-to-time match)
  16152. (org-time-string-to-time start-date)))
  16153. (time-less-p
  16154. (org-time-string-to-time match)
  16155. (org-time-string-to-time end-date))))))))
  16156. (message "%d entries between %s and %s"
  16157. (org-occur regexp nil callback) start-date end-date)))
  16158. (defun org-evaluate-time-range (&optional to-buffer)
  16159. "Evaluate a time range by computing the difference between start and end.
  16160. Normally the result is just printed in the echo area, but with prefix arg
  16161. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  16162. If the time range is actually in a table, the result is inserted into the
  16163. next column.
  16164. For time difference computation, a year is assumed to be exactly 365
  16165. days in order to avoid rounding problems."
  16166. (interactive "P")
  16167. (or
  16168. (org-clock-update-time-maybe)
  16169. (save-excursion
  16170. (unless (org-at-date-range-p t)
  16171. (goto-char (point-at-bol))
  16172. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  16173. (unless (org-at-date-range-p t)
  16174. (user-error "Not at a time-stamp range, and none found in current line")))
  16175. (let* ((ts1 (match-string 1))
  16176. (ts2 (match-string 2))
  16177. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  16178. (match-end (match-end 0))
  16179. (time1 (org-time-string-to-time ts1))
  16180. (time2 (org-time-string-to-time ts2))
  16181. (t1 (org-float-time time1))
  16182. (t2 (org-float-time time2))
  16183. (diff (abs (- t2 t1)))
  16184. (negative (< (- t2 t1) 0))
  16185. ;; (ys (floor (* 365 24 60 60)))
  16186. (ds (* 24 60 60))
  16187. (hs (* 60 60))
  16188. (fy "%dy %dd %02d:%02d")
  16189. (fy1 "%dy %dd")
  16190. (fd "%dd %02d:%02d")
  16191. (fd1 "%dd")
  16192. (fh "%02d:%02d")
  16193. y d h m align)
  16194. (if havetime
  16195. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  16196. y 0
  16197. d (floor (/ diff ds)) diff (mod diff ds)
  16198. h (floor (/ diff hs)) diff (mod diff hs)
  16199. m (floor (/ diff 60)))
  16200. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  16201. y 0
  16202. d (floor (+ (/ diff ds) 0.5))
  16203. h 0 m 0))
  16204. (if (not to-buffer)
  16205. (message "%s" (org-make-tdiff-string y d h m))
  16206. (if (org-at-table-p)
  16207. (progn
  16208. (goto-char match-end)
  16209. (setq align t)
  16210. (and (looking-at " *|") (goto-char (match-end 0))))
  16211. (goto-char match-end))
  16212. (when (looking-at
  16213. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  16214. (replace-match ""))
  16215. (when negative (insert " -"))
  16216. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  16217. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  16218. (insert " " (format fh h m))))
  16219. (when align (org-table-align))
  16220. (message "Time difference inserted")))))
  16221. (defun org-make-tdiff-string (y d h m)
  16222. (let ((fmt "")
  16223. (l nil))
  16224. (when (> y 0)
  16225. (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " "))
  16226. (push y l))
  16227. (when (> d 0)
  16228. (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " "))
  16229. (push d l))
  16230. (when (> h 0)
  16231. (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " "))
  16232. (push h l))
  16233. (when (> m 0)
  16234. (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " "))
  16235. (push m l))
  16236. (apply 'format fmt (nreverse l))))
  16237. (defun org-time-string-to-time (s &optional buffer pos)
  16238. "Convert a timestamp string into internal time."
  16239. (condition-case errdata
  16240. (apply 'encode-time (org-parse-time-string s))
  16241. (error (error "Bad timestamp `%s'%s\nError was: %s"
  16242. s (if (not (and buffer pos))
  16243. ""
  16244. (format-message " at %d in buffer `%s'" pos buffer))
  16245. (cdr errdata)))))
  16246. (defun org-time-string-to-seconds (s)
  16247. "Convert a timestamp string to a number of seconds."
  16248. (org-float-time (org-time-string-to-time s)))
  16249. (org-define-error 'org-diary-sexp-no-match "Unable to match diary sexp")
  16250. (defun org-time-string-to-absolute (s &optional daynr prefer show-all buffer pos)
  16251. "Convert time stamp S to an absolute day number.
  16252. If DAYNR in non-nil, and there is a specifier for a cyclic time
  16253. stamp, get the closest date to DAYNR. If PREFER is
  16254. `past' (respectively `future') return a date past (respectively
  16255. after) or equal to DAYNR.
  16256. POS is the location of time stamp S, as a buffer position in
  16257. BUFFER.
  16258. Diary sexp timestamps are matched against DAYNR, when non-nil.
  16259. If matching fails or DAYNR is nil, `org-diary-sexp-no-match' is
  16260. signalled."
  16261. (cond
  16262. ((string-match "\\`%%\\((.*)\\)" s)
  16263. ;; Sexp timestamp: try to match DAYNR, if available, since we're
  16264. ;; only able to match individual dates. If it fails, raise an
  16265. ;; error.
  16266. (if (and daynr
  16267. (org-diary-sexp-entry
  16268. (match-string 1 s) "" (calendar-gregorian-from-absolute daynr)))
  16269. daynr
  16270. (signal 'org-diary-sexp-no-match (list s))))
  16271. ((and daynr show-all) (org-closest-date s daynr prefer))
  16272. (t (time-to-days
  16273. (condition-case errdata
  16274. (apply #'encode-time (org-parse-time-string s))
  16275. (error (error "Bad timestamp `%s'%s\nError was: %s"
  16276. s
  16277. (if (not (and buffer pos)) ""
  16278. (format-message " at %d in buffer `%s'" pos buffer))
  16279. (cdr errdata))))))))
  16280. (defun org-days-to-iso-week (days)
  16281. "Return the iso week number."
  16282. (require 'cal-iso)
  16283. (car (calendar-iso-from-absolute days)))
  16284. (defun org-small-year-to-year (year)
  16285. "Convert 2-digit years into 4-digit years.
  16286. YEAR is expanded into one of the 30 next years, if possible, or
  16287. into a past one. Any year larger than 99 is returned unchanged."
  16288. (if (>= year 100) year
  16289. (let* ((current (string-to-number (format-time-string "%Y" (current-time))))
  16290. (century (/ current 100))
  16291. (offset (- year (% current 100))))
  16292. (cond ((> offset 30) (+ (* (1- century) 100) year))
  16293. ((> offset -70) (+ (* century 100) year))
  16294. (t (+ (* (1+ century) 100) year))))))
  16295. (defun org-time-from-absolute (d)
  16296. "Return the time corresponding to date D.
  16297. D may be an absolute day number, or a calendar-type list (month day year)."
  16298. (when (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  16299. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  16300. (defvar org-agenda-current-date)
  16301. (defun org-calendar-holiday ()
  16302. "List of holidays, for Diary display in Org mode."
  16303. (require 'holidays)
  16304. (let ((hl (calendar-check-holidays org-agenda-current-date)))
  16305. (and hl (mapconcat #'identity hl "; "))))
  16306. (defun org-diary-sexp-entry (sexp entry d)
  16307. "Process a SEXP diary ENTRY for date D."
  16308. (require 'diary-lib)
  16309. ;; `org-anniversary' and alike expect ENTRY and DATE to be bound
  16310. ;; dynamically.
  16311. (let* ((sexp `(let ((entry ,entry)
  16312. (date ',d))
  16313. ,(car (read-from-string sexp))))
  16314. (result (if calendar-debug-sexp (eval sexp)
  16315. (condition-case nil
  16316. (eval sexp)
  16317. (error
  16318. (beep)
  16319. (message "Bad sexp at line %d in %s: %s"
  16320. (org-current-line)
  16321. (buffer-file-name) sexp)
  16322. (sleep-for 2))))))
  16323. (cond ((stringp result) (split-string result "; "))
  16324. ((and (consp result)
  16325. (not (consp (cdr result)))
  16326. (stringp (cdr result))) (cdr result))
  16327. ((and (consp result)
  16328. (stringp (car result))) result)
  16329. (result entry))))
  16330. (defun org-diary-to-ical-string (frombuf)
  16331. "Get iCalendar entries from diary entries in buffer FROMBUF.
  16332. This uses the icalendar.el library."
  16333. (let* ((tmpdir (if (featurep 'xemacs)
  16334. (temp-directory)
  16335. temporary-file-directory))
  16336. (tmpfile (make-temp-name
  16337. (expand-file-name "orgics" tmpdir)))
  16338. buf rtn b e)
  16339. (with-current-buffer frombuf
  16340. (icalendar-export-region (point-min) (point-max) tmpfile)
  16341. (setq buf (find-buffer-visiting tmpfile))
  16342. (set-buffer buf)
  16343. (goto-char (point-min))
  16344. (when (re-search-forward "^BEGIN:VEVENT" nil t)
  16345. (setq b (match-beginning 0)))
  16346. (goto-char (point-max))
  16347. (when (re-search-backward "^END:VEVENT" nil t)
  16348. (setq e (match-end 0)))
  16349. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  16350. (kill-buffer buf)
  16351. (delete-file tmpfile)
  16352. rtn))
  16353. (defun org-closest-date (start current prefer)
  16354. "Return closest date to CURRENT starting from START.
  16355. CURRENT and START are both time stamps.
  16356. When PREFER is `past', return a date that is either CURRENT or
  16357. past. When PREFER is `future', return a date that is either
  16358. CURRENT or future.
  16359. Only time stamps with a repeater are modified. Any other time
  16360. stamp stay unchanged. In any case, return value is an absolute
  16361. day number."
  16362. (if (not (string-match "\\+\\([0-9]+\\)\\([hdwmy]\\)" start))
  16363. ;; No repeater. Do not shift time stamp.
  16364. (time-to-days (apply #'encode-time (org-parse-time-string start)))
  16365. (let ((value (string-to-number (match-string 1 start)))
  16366. (type (match-string 2 start)))
  16367. (if (= 0 value)
  16368. ;; Repeater with a 0-value is considered as void.
  16369. (time-to-days (apply #'encode-time (org-parse-time-string start)))
  16370. (let* ((base (org-date-to-gregorian start))
  16371. (target (org-date-to-gregorian current))
  16372. (sday (calendar-absolute-from-gregorian base))
  16373. (cday (calendar-absolute-from-gregorian target))
  16374. n1 n2)
  16375. ;; If START is already past CURRENT, just return START.
  16376. (if (<= cday sday) sday
  16377. ;; Compute closest date before (N1) and closest date past
  16378. ;; (N2) CURRENT.
  16379. (pcase type
  16380. ("h"
  16381. (let ((missing-hours
  16382. (mod (+ (- (* 24 (- cday sday))
  16383. (nth 2 (org-parse-time-string start)))
  16384. org-extend-today-until)
  16385. value)))
  16386. (setf n1 (if (= missing-hours 0) cday
  16387. (- cday (1+ (/ missing-hours 24)))))
  16388. (setf n2 (+ cday (/ (- value missing-hours) 24)))))
  16389. ((or "d" "w")
  16390. (let ((value (if (equal type "w") (* 7 value) value)))
  16391. (setf n1 (+ sday (* value (/ (- cday sday) value))))
  16392. (setf n2 (+ n1 value))))
  16393. ("m"
  16394. (let* ((add-months
  16395. (lambda (d n)
  16396. ;; Add N months to gregorian date D, i.e.,
  16397. ;; a list (MONTH DAY YEAR). Return a valid
  16398. ;; gregorian date.
  16399. (let ((m (+ (nth 0 d) n)))
  16400. (list (mod m 12)
  16401. (nth 1 d)
  16402. (+ (/ m 12) (nth 2 d))))))
  16403. (months ; Complete months to TARGET.
  16404. (* (/ (+ (* 12 (- (nth 2 target) (nth 2 base)))
  16405. (- (nth 0 target) (nth 0 base))
  16406. ;; If START's day is greater than
  16407. ;; TARGET's, remove incomplete month.
  16408. (if (> (nth 1 target) (nth 1 base)) 0 -1))
  16409. value)
  16410. value))
  16411. (before (funcall add-months base months)))
  16412. (setf n1 (calendar-absolute-from-gregorian before))
  16413. (setf n2
  16414. (calendar-absolute-from-gregorian
  16415. (funcall add-months before value)))))
  16416. (_
  16417. (let* ((d (nth 1 base))
  16418. (m (nth 0 base))
  16419. (y (nth 2 base))
  16420. (years ; Complete years to TARGET.
  16421. (* (/ (- (nth 2 target)
  16422. y
  16423. ;; If START's month and day are
  16424. ;; greater than TARGET's, remove
  16425. ;; incomplete year.
  16426. (if (or (> (nth 0 target) m)
  16427. (and (= (nth 0 target) m)
  16428. (> (nth 1 target) d)))
  16429. 0
  16430. 1))
  16431. value)
  16432. value))
  16433. (before (list m d (+ y years))))
  16434. (setf n1 (calendar-absolute-from-gregorian before))
  16435. (setf n2 (calendar-absolute-from-gregorian
  16436. (list m d (+ (nth 2 before) value)))))))
  16437. ;; Handle PREFER parameter, if any.
  16438. (cond
  16439. ((eq prefer 'past) (if (= cday n2) n2 n1))
  16440. ((eq prefer 'future) (if (= cday n1) n1 n2))
  16441. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))))))))
  16442. (defun org-date-to-gregorian (d)
  16443. "Turn any specification of date D into a Gregorian date for the calendar."
  16444. (cond ((integerp d) (calendar-gregorian-from-absolute d))
  16445. ((and (listp d) (= (length d) 3)) d)
  16446. ((stringp d)
  16447. (let ((d (org-parse-time-string d)))
  16448. (list (nth 4 d) (nth 3 d) (nth 5 d))))
  16449. ((listp d) (list (nth 4 d) (nth 3 d) (nth 5 d)))))
  16450. (defun org-parse-time-string (s &optional nodefault)
  16451. "Parse the standard Org-mode time string.
  16452. This should be a lot faster than the normal `parse-time-string'.
  16453. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  16454. hour and minute fields will be nil if not given."
  16455. (cond ((string-match org-ts-regexp0 s)
  16456. (list 0
  16457. (when (or (match-beginning 8) (not nodefault))
  16458. (string-to-number (or (match-string 8 s) "0")))
  16459. (when (or (match-beginning 7) (not nodefault))
  16460. (string-to-number (or (match-string 7 s) "0")))
  16461. (string-to-number (match-string 4 s))
  16462. (string-to-number (match-string 3 s))
  16463. (string-to-number (match-string 2 s))
  16464. nil nil nil))
  16465. ((string-match "^<[^>]+>$" s)
  16466. (decode-time (seconds-to-time (org-matcher-time s))))
  16467. (t (error "Not a standard Org-mode time string: %s" s))))
  16468. (defun org-timestamp-up (&optional arg)
  16469. "Increase the date item at the cursor by one.
  16470. If the cursor is on the year, change the year. If it is on the month,
  16471. the day or the time, change that.
  16472. With prefix ARG, change by that many units."
  16473. (interactive "p")
  16474. (org-timestamp-change (prefix-numeric-value arg) nil 'updown))
  16475. (defun org-timestamp-down (&optional arg)
  16476. "Decrease the date item at the cursor by one.
  16477. If the cursor is on the year, change the year. If it is on the month,
  16478. the day or the time, change that.
  16479. With prefix ARG, change by that many units."
  16480. (interactive "p")
  16481. (org-timestamp-change (- (prefix-numeric-value arg)) nil 'updown))
  16482. (defun org-timestamp-up-day (&optional arg)
  16483. "Increase the date in the time stamp by one day.
  16484. With prefix ARG, change that many days."
  16485. (interactive "p")
  16486. (if (and (not (org-at-timestamp-p t))
  16487. (org-at-heading-p))
  16488. (org-todo 'up)
  16489. (org-timestamp-change (prefix-numeric-value arg) 'day 'updown)))
  16490. (defun org-timestamp-down-day (&optional arg)
  16491. "Decrease the date in the time stamp by one day.
  16492. With prefix ARG, change that many days."
  16493. (interactive "p")
  16494. (if (and (not (org-at-timestamp-p t))
  16495. (org-at-heading-p))
  16496. (org-todo 'down)
  16497. (org-timestamp-change (- (prefix-numeric-value arg)) 'day) 'updown))
  16498. (defun org-at-timestamp-p (&optional inactive-ok)
  16499. "Non-nil if point is inside a timestamp.
  16500. When optional argument INACTIVE-OK is non-nil, also consider
  16501. inactive timestamps.
  16502. When this function returns a non-nil value, match data is set
  16503. according to `org-ts-regexp3' or `org-ts-regexp2', depending on
  16504. INACTIVE-OK."
  16505. (interactive)
  16506. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  16507. (pos (point))
  16508. (ans (or (looking-at tsr)
  16509. (save-excursion
  16510. (skip-chars-backward "^[<\n\r\t")
  16511. (when (> (point) (point-min)) (backward-char 1))
  16512. (and (looking-at tsr)
  16513. (> (- (match-end 0) pos) -1))))))
  16514. (and ans
  16515. (boundp 'org-ts-what)
  16516. (setq org-ts-what
  16517. (cond
  16518. ((= pos (match-beginning 0)) 'bracket)
  16519. ;; Point is considered to be "on the bracket" whether
  16520. ;; it's really on it or right after it.
  16521. ((= pos (1- (match-end 0))) 'bracket)
  16522. ((= pos (match-end 0)) 'after)
  16523. ((org-pos-in-match-range pos 2) 'year)
  16524. ((org-pos-in-match-range pos 3) 'month)
  16525. ((org-pos-in-match-range pos 7) 'hour)
  16526. ((org-pos-in-match-range pos 8) 'minute)
  16527. ((or (org-pos-in-match-range pos 4)
  16528. (org-pos-in-match-range pos 5)) 'day)
  16529. ((and (> pos (or (match-end 8) (match-end 5)))
  16530. (< pos (match-end 0)))
  16531. (- pos (or (match-end 8) (match-end 5))))
  16532. (t 'day))))
  16533. ans))
  16534. (defun org-toggle-timestamp-type ()
  16535. "Toggle the type (<active> or [inactive]) of a time stamp."
  16536. (interactive)
  16537. (when (org-at-timestamp-p t)
  16538. (let ((beg (match-beginning 0)) (end (match-end 0))
  16539. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  16540. (save-excursion
  16541. (goto-char beg)
  16542. (while (re-search-forward "[][<>]" end t)
  16543. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  16544. t t)))
  16545. (message "Timestamp is now %sactive"
  16546. (if (equal (char-after beg) ?<) "" "in")))))
  16547. (defun org-at-clock-log-p nil
  16548. "Is the cursor on the clock log line?"
  16549. (save-excursion
  16550. (move-beginning-of-line 1)
  16551. (looking-at org-clock-line-re)))
  16552. (defvar org-clock-history) ; defined in org-clock.el
  16553. (defvar org-clock-adjust-closest nil) ; defined in org-clock.el
  16554. (defun org-timestamp-change (n &optional what updown suppress-tmp-delay)
  16555. "Change the date in the time stamp at point.
  16556. The date will be changed by N times WHAT. WHAT can be `day', `month',
  16557. `year', `minute', `second'. If WHAT is not given, the cursor position
  16558. in the timestamp determines what will be changed.
  16559. When SUPPRESS-TMP-DELAY is non-nil, suppress delays like \"--2d\"."
  16560. (let ((origin (point)) origin-cat
  16561. with-hm inactive
  16562. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  16563. org-ts-what
  16564. extra rem
  16565. ts time time0 fixnext clrgx)
  16566. (unless (org-at-timestamp-p t)
  16567. (user-error "Not at a timestamp"))
  16568. (if (and (not what) (eq org-ts-what 'bracket))
  16569. (org-toggle-timestamp-type)
  16570. ;; Point isn't on brackets. Remember the part of the time-stamp
  16571. ;; the point was in. Indeed, size of time-stamps may change,
  16572. ;; but point must be kept in the same category nonetheless.
  16573. (setq origin-cat org-ts-what)
  16574. (when (and (not what) (not (eq org-ts-what 'day))
  16575. org-display-custom-times
  16576. (get-text-property (point) 'display)
  16577. (not (get-text-property (1- (point)) 'display)))
  16578. (setq org-ts-what 'day))
  16579. (setq org-ts-what (or what org-ts-what)
  16580. inactive (= (char-after (match-beginning 0)) ?\[)
  16581. ts (match-string 0))
  16582. (replace-match "")
  16583. (when (string-match
  16584. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?-?[-+][0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)*\\)[]>]"
  16585. ts)
  16586. (setq extra (match-string 1 ts))
  16587. (when suppress-tmp-delay
  16588. (setq extra (replace-regexp-in-string " --[0-9]+[hdwmy]" "" extra))))
  16589. (when (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  16590. (setq with-hm t))
  16591. (setq time0 (org-parse-time-string ts))
  16592. (when (and updown
  16593. (eq org-ts-what 'minute)
  16594. (not current-prefix-arg))
  16595. ;; This looks like s-up and s-down. Change by one rounding step.
  16596. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  16597. (unless (= 0 (setq rem (% (nth 1 time0) dm)))
  16598. (setcar (cdr time0) (+ (nth 1 time0)
  16599. (if (> n 0) (- rem) (- dm rem))))))
  16600. (setq time
  16601. (apply #'encode-time
  16602. (or (car time0) 0)
  16603. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  16604. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  16605. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  16606. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  16607. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  16608. (nthcdr 6 time0)))
  16609. (when (and (member org-ts-what '(hour minute))
  16610. extra
  16611. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  16612. (setq extra (org-modify-ts-extra
  16613. extra
  16614. (if (eq org-ts-what 'hour) 2 5)
  16615. n dm)))
  16616. (when (integerp org-ts-what)
  16617. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  16618. (when (eq what 'calendar)
  16619. (let ((cal-date (org-get-date-from-calendar)))
  16620. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  16621. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  16622. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  16623. (setcar time0 (or (car time0) 0))
  16624. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  16625. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  16626. (setq time (apply 'encode-time time0))))
  16627. ;; Insert the new time-stamp, and ensure point stays in the same
  16628. ;; category as before (i.e. not after the last position in that
  16629. ;; category).
  16630. (let ((pos (point)))
  16631. ;; Stay before inserted string. `save-excursion' is of no use.
  16632. (setq org-last-changed-timestamp
  16633. (org-insert-time-stamp time with-hm inactive nil nil extra))
  16634. (goto-char pos))
  16635. (save-match-data
  16636. (looking-at org-ts-regexp3)
  16637. (goto-char (cond
  16638. ;; `day' category ends before `hour' if any, or at
  16639. ;; the end of the day name.
  16640. ((eq origin-cat 'day)
  16641. (min (or (match-beginning 7) (1- (match-end 5))) origin))
  16642. ((eq origin-cat 'hour) (min (match-end 7) origin))
  16643. ((eq origin-cat 'minute) (min (1- (match-end 8)) origin))
  16644. ((integerp origin-cat) (min (1- (match-end 0)) origin))
  16645. ;; `year' and `month' have both fixed size: point
  16646. ;; couldn't have moved into another part.
  16647. (t origin))))
  16648. ;; Update clock if on a CLOCK line.
  16649. (org-clock-update-time-maybe)
  16650. ;; Maybe adjust the closest clock in `org-clock-history'
  16651. (when org-clock-adjust-closest
  16652. (if (not (and (org-at-clock-log-p)
  16653. (< 1 (length (delq nil (mapcar 'marker-position
  16654. org-clock-history))))))
  16655. (message "No clock to adjust")
  16656. (cond ((save-excursion ; fix previous clock?
  16657. (re-search-backward org-ts-regexp0 nil t)
  16658. (org-looking-back (concat org-clock-string " \\[")
  16659. (line-beginning-position)))
  16660. (setq fixnext 1 clrgx (concat org-ts-regexp0 "\\] =>.*$")))
  16661. ((save-excursion ; fix next clock?
  16662. (re-search-backward org-ts-regexp0 nil t)
  16663. (looking-at (concat org-ts-regexp0 "\\] =>")))
  16664. (setq fixnext -1 clrgx (concat org-clock-string " \\[" org-ts-regexp0))))
  16665. (save-window-excursion
  16666. ;; Find closest clock to point, adjust the previous/next one in history
  16667. (let* ((p (save-excursion (org-back-to-heading t)))
  16668. (cl (mapcar (lambda(c) (abs (- (marker-position c) p))) org-clock-history))
  16669. (clfixnth
  16670. (+ fixnext (- (length cl) (or (length (member (apply 'min cl) cl)) 100))))
  16671. (clfixpos (unless (> 0 clfixnth) (nth clfixnth org-clock-history))))
  16672. (if (not clfixpos)
  16673. (message "No clock to adjust")
  16674. (save-excursion
  16675. (org-goto-marker-or-bmk clfixpos)
  16676. (org-show-subtree)
  16677. (when (re-search-forward clrgx nil t)
  16678. (goto-char (match-beginning 1))
  16679. (let (org-clock-adjust-closest)
  16680. (org-timestamp-change n org-ts-what updown))
  16681. (message "Clock adjusted in %s for heading: %s"
  16682. (file-name-nondirectory (buffer-file-name))
  16683. (org-get-heading t t)))))))))
  16684. ;; Try to recenter the calendar window, if any.
  16685. (when (and org-calendar-follow-timestamp-change
  16686. (get-buffer-window "*Calendar*" t)
  16687. (memq org-ts-what '(day month year)))
  16688. (org-recenter-calendar (time-to-days time))))))
  16689. (defun org-modify-ts-extra (s pos n dm)
  16690. "Change the different parts of the lead-time and repeat fields in timestamp."
  16691. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  16692. ng h m new rem)
  16693. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  16694. (cond
  16695. ((or (org-pos-in-match-range pos 2)
  16696. (org-pos-in-match-range pos 3))
  16697. (setq m (string-to-number (match-string 3 s))
  16698. h (string-to-number (match-string 2 s)))
  16699. (if (org-pos-in-match-range pos 2)
  16700. (setq h (+ h n))
  16701. (setq n (* dm (org-no-warnings (signum n))))
  16702. (unless (= 0 (setq rem (% m dm)))
  16703. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  16704. (setq m (+ m n)))
  16705. (when (< m 0) (setq m (+ m 60) h (1- h)))
  16706. (when (> m 59) (setq m (- m 60) h (1+ h)))
  16707. (setq h (mod h 24))
  16708. (setq ng 1 new (format "-%02d:%02d" h m)))
  16709. ((org-pos-in-match-range pos 6)
  16710. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  16711. ((org-pos-in-match-range pos 5)
  16712. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  16713. ((org-pos-in-match-range pos 9)
  16714. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  16715. ((org-pos-in-match-range pos 8)
  16716. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  16717. (when ng
  16718. (setq s (concat
  16719. (substring s 0 (match-beginning ng))
  16720. new
  16721. (substring s (match-end ng))))))
  16722. s))
  16723. (defun org-recenter-calendar (d)
  16724. "If the calendar is visible, recenter it to date D."
  16725. (let ((cwin (get-buffer-window "*Calendar*" t)))
  16726. (when cwin
  16727. (let ((calendar-move-hook nil))
  16728. (with-selected-window cwin
  16729. (calendar-goto-date
  16730. (if (listp d) d (calendar-gregorian-from-absolute d))))))))
  16731. (defun org-goto-calendar (&optional arg)
  16732. "Go to the Emacs calendar at the current date.
  16733. If there is a time stamp in the current line, go to that date.
  16734. A prefix ARG can be used to force the current date."
  16735. (interactive "P")
  16736. (let ((tsr org-ts-regexp) diff
  16737. (calendar-move-hook nil)
  16738. (calendar-view-holidays-initially-flag nil)
  16739. (calendar-view-diary-initially-flag nil))
  16740. (when (or (org-at-timestamp-p)
  16741. (save-excursion
  16742. (beginning-of-line 1)
  16743. (looking-at (concat ".*" tsr))))
  16744. (let ((d1 (time-to-days (current-time)))
  16745. (d2 (time-to-days
  16746. (org-time-string-to-time (match-string 1)))))
  16747. (setq diff (- d2 d1))))
  16748. (calendar)
  16749. (calendar-goto-today)
  16750. (when (and diff (not arg)) (calendar-forward-day diff))))
  16751. (defun org-get-date-from-calendar ()
  16752. "Return a list (month day year) of date at point in calendar."
  16753. (with-current-buffer "*Calendar*"
  16754. (save-match-data
  16755. (calendar-cursor-to-date))))
  16756. (defun org-date-from-calendar ()
  16757. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  16758. If there is already a time stamp at the cursor position, update it."
  16759. (interactive)
  16760. (if (org-at-timestamp-p t)
  16761. (org-timestamp-change 0 'calendar)
  16762. (let ((cal-date (org-get-date-from-calendar)))
  16763. (org-insert-time-stamp
  16764. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  16765. (defcustom org-effort-durations
  16766. `(("min" . 1)
  16767. ("h" . 60)
  16768. ("d" . ,(* 60 8))
  16769. ("w" . ,(* 60 8 5))
  16770. ("m" . ,(* 60 8 5 4))
  16771. ("y" . ,(* 60 8 5 40)))
  16772. "Conversion factor to minutes for an effort modifier.
  16773. Each entry has the form (MODIFIER . MINUTES).
  16774. In an effort string, a number followed by MODIFIER is multiplied
  16775. by the specified number of MINUTES to obtain an effort in
  16776. minutes.
  16777. For example, if the value of this variable is ((\"hours\" . 60)), then an
  16778. effort string \"2hours\" is equivalent to 120 minutes."
  16779. :group 'org-agenda
  16780. :version "25.1"
  16781. :package-version '(Org . "8.3")
  16782. :type '(alist :key-type (string :tag "Modifier")
  16783. :value-type (number :tag "Minutes")))
  16784. (defun org-minutes-to-clocksum-string (m)
  16785. "Format number of minutes as a clocksum string.
  16786. The format is determined by `org-time-clocksum-format',
  16787. `org-time-clocksum-use-fractional' and
  16788. `org-time-clocksum-fractional-format' and
  16789. `org-time-clocksum-use-effort-durations'."
  16790. (let ((clocksum "")
  16791. (m (round m)) ; Don't allow fractions of minutes
  16792. h d w mo y fmt n)
  16793. (setq h (if org-time-clocksum-use-effort-durations
  16794. (cdr (assoc "h" org-effort-durations)) 60)
  16795. d (if org-time-clocksum-use-effort-durations
  16796. (/ (cdr (assoc "d" org-effort-durations)) h) 24)
  16797. w (if org-time-clocksum-use-effort-durations
  16798. (/ (cdr (assoc "w" org-effort-durations)) (* d h)) 7)
  16799. mo (if org-time-clocksum-use-effort-durations
  16800. (/ (cdr (assoc "m" org-effort-durations)) (* d h)) 30)
  16801. y (if org-time-clocksum-use-effort-durations
  16802. (/ (cdr (assoc "y" org-effort-durations)) (* d h)) 365))
  16803. ;; fractional format
  16804. (if org-time-clocksum-use-fractional
  16805. (cond
  16806. ;; single format string
  16807. ((stringp org-time-clocksum-fractional-format)
  16808. (format org-time-clocksum-fractional-format (/ m (float h))))
  16809. ;; choice of fractional formats for different time units
  16810. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :years))
  16811. (> (/ (truncate m) (* y d h)) 0))
  16812. (format fmt (/ m (* y d (float h)))))
  16813. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :months))
  16814. (> (/ (truncate m) (* mo d h)) 0))
  16815. (format fmt (/ m (* mo d (float h)))))
  16816. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :weeks))
  16817. (> (/ (truncate m) (* w d h)) 0))
  16818. (format fmt (/ m (* w d (float h)))))
  16819. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :days))
  16820. (> (/ (truncate m) (* d h)) 0))
  16821. (format fmt (/ m (* d (float h)))))
  16822. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :hours))
  16823. (> (/ (truncate m) h) 0))
  16824. (format fmt (/ m (float h))))
  16825. ((setq fmt (plist-get org-time-clocksum-fractional-format :minutes))
  16826. (format fmt m))
  16827. ;; fall back to smallest time unit with a format
  16828. ((setq fmt (plist-get org-time-clocksum-fractional-format :hours))
  16829. (format fmt (/ m (float h))))
  16830. ((setq fmt (plist-get org-time-clocksum-fractional-format :days))
  16831. (format fmt (/ m (* d (float h)))))
  16832. ((setq fmt (plist-get org-time-clocksum-fractional-format :weeks))
  16833. (format fmt (/ m (* w d (float h)))))
  16834. ((setq fmt (plist-get org-time-clocksum-fractional-format :months))
  16835. (format fmt (/ m (* mo d (float h)))))
  16836. ((setq fmt (plist-get org-time-clocksum-fractional-format :years))
  16837. (format fmt (/ m (* y d (float h))))))
  16838. ;; standard (non-fractional) format, with single format string
  16839. (if (stringp org-time-clocksum-format)
  16840. (format org-time-clocksum-format (setq n (/ m h)) (- m (* h n)))
  16841. ;; separate formats components
  16842. (and (setq fmt (plist-get org-time-clocksum-format :years))
  16843. (or (> (setq n (/ (truncate m) (* y d h))) 0)
  16844. (plist-get org-time-clocksum-format :require-years))
  16845. (setq clocksum (concat clocksum (format fmt n))
  16846. m (- m (* n y d h))))
  16847. (and (setq fmt (plist-get org-time-clocksum-format :months))
  16848. (or (> (setq n (/ (truncate m) (* mo d h))) 0)
  16849. (plist-get org-time-clocksum-format :require-months))
  16850. (setq clocksum (concat clocksum (format fmt n))
  16851. m (- m (* n mo d h))))
  16852. (and (setq fmt (plist-get org-time-clocksum-format :weeks))
  16853. (or (> (setq n (/ (truncate m) (* w d h))) 0)
  16854. (plist-get org-time-clocksum-format :require-weeks))
  16855. (setq clocksum (concat clocksum (format fmt n))
  16856. m (- m (* n w d h))))
  16857. (and (setq fmt (plist-get org-time-clocksum-format :days))
  16858. (or (> (setq n (/ (truncate m) (* d h))) 0)
  16859. (plist-get org-time-clocksum-format :require-days))
  16860. (setq clocksum (concat clocksum (format fmt n))
  16861. m (- m (* n d h))))
  16862. (and (setq fmt (plist-get org-time-clocksum-format :hours))
  16863. (or (> (setq n (/ (truncate m) h)) 0)
  16864. (plist-get org-time-clocksum-format :require-hours))
  16865. (setq clocksum (concat clocksum (format fmt n))
  16866. m (- m (* n h))))
  16867. (and (setq fmt (plist-get org-time-clocksum-format :minutes))
  16868. (or (> m 0) (plist-get org-time-clocksum-format :require-minutes))
  16869. (setq clocksum (concat clocksum (format fmt m))))
  16870. ;; return formatted time duration
  16871. clocksum))))
  16872. (defalias 'org-minutes-to-hh:mm-string 'org-minutes-to-clocksum-string)
  16873. (make-obsolete 'org-minutes-to-hh:mm-string 'org-minutes-to-clocksum-string
  16874. "Org mode version 8.0")
  16875. (defun org-hours-to-clocksum-string (n)
  16876. (org-minutes-to-clocksum-string (* n 60)))
  16877. (defun org-hh:mm-string-to-minutes (s)
  16878. "Convert a string H:MM to a number of minutes.
  16879. If the string is just a number, interpret it as minutes.
  16880. In fact, the first hh:mm or number in the string will be taken,
  16881. there can be extra stuff in the string.
  16882. If no number is found, the return value is 0."
  16883. (cond
  16884. ((integerp s) s)
  16885. ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  16886. (+ (* (string-to-number (match-string 1 s)) 60)
  16887. (string-to-number (match-string 2 s))))
  16888. ((string-match "\\([0-9]+\\)" s)
  16889. (string-to-number (match-string 1 s)))
  16890. (t 0)))
  16891. (defcustom org-image-actual-width t
  16892. "Should we use the actual width of images when inlining them?
  16893. When set to t, always use the image width.
  16894. When set to a number, use imagemagick (when available) to set
  16895. the image's width to this value.
  16896. When set to a number in a list, try to get the width from any
  16897. #+ATTR.* keyword if it matches a width specification like
  16898. #+ATTR_HTML: :width 300px
  16899. and fall back on that number if none is found.
  16900. When set to nil, try to get the width from an #+ATTR.* keyword
  16901. and fall back on the original width if none is found.
  16902. This requires Emacs >= 24.1, build with imagemagick support."
  16903. :group 'org-appearance
  16904. :version "24.4"
  16905. :package-version '(Org . "8.0")
  16906. :type '(choice
  16907. (const :tag "Use the image width" t)
  16908. (integer :tag "Use a number of pixels")
  16909. (list :tag "Use #+ATTR* or a number of pixels" (integer))
  16910. (const :tag "Use #+ATTR* or don't resize" nil)))
  16911. (defcustom org-agenda-inhibit-startup nil
  16912. "Inhibit startup when preparing agenda buffers.
  16913. When this variable is t, the initialization of the Org agenda
  16914. buffers is inhibited: e.g. the visibility state is not set, the
  16915. tables are not re-aligned, etc."
  16916. :type 'boolean
  16917. :version "24.3"
  16918. :group 'org-agenda)
  16919. (define-obsolete-variable-alias
  16920. 'org-agenda-ignore-drawer-properties
  16921. 'org-agenda-ignore-properties "25.1")
  16922. (defcustom org-agenda-ignore-properties nil
  16923. "Avoid updating text properties when building the agenda.
  16924. Properties are used to prepare buffers for effort estimates,
  16925. appointments, statistics and subtree-local categories.
  16926. If you don't use these in the agenda, you can add them to this
  16927. list and agenda building will be a bit faster.
  16928. The value is a list, with zero or more of the symbols `effort', `appt',
  16929. `stats' or `category'."
  16930. :type '(set :greedy t
  16931. (const effort)
  16932. (const appt)
  16933. (const stats)
  16934. (const category))
  16935. :version "25.1"
  16936. :package-version '(Org . "8.3")
  16937. :group 'org-agenda)
  16938. (defun org-duration-string-to-minutes (s &optional output-to-string)
  16939. "Convert a duration string S to minutes.
  16940. A bare number is interpreted as minutes, modifiers can be set by
  16941. customizing `org-effort-durations' (which see).
  16942. Entries containing a colon are interpreted as H:MM by
  16943. `org-hh:mm-string-to-minutes'."
  16944. (let ((result 0)
  16945. (re (concat "\\([0-9.]+\\) *\\("
  16946. (regexp-opt (mapcar 'car org-effort-durations))
  16947. "\\)")))
  16948. (while (string-match re s)
  16949. (cl-incf result (* (cdr (assoc (match-string 2 s) org-effort-durations))
  16950. (string-to-number (match-string 1 s))))
  16951. (setq s (replace-match "" nil t s)))
  16952. (setq result (floor result))
  16953. (cl-incf result (org-hh:mm-string-to-minutes s))
  16954. (if output-to-string (number-to-string result) result)))
  16955. ;;;; Files
  16956. (defun org-save-all-org-buffers ()
  16957. "Save all Org-mode buffers without user confirmation."
  16958. (interactive)
  16959. (message "Saving all Org-mode buffers...")
  16960. (save-some-buffers t (lambda () (derived-mode-p 'org-mode)))
  16961. (when (featurep 'org-id) (org-id-locations-save))
  16962. (message "Saving all Org-mode buffers... done"))
  16963. (defun org-revert-all-org-buffers ()
  16964. "Revert all Org-mode buffers.
  16965. Prompt for confirmation when there are unsaved changes.
  16966. Be sure you know what you are doing before letting this function
  16967. overwrite your changes.
  16968. This function is useful in a setup where one tracks org files
  16969. with a version control system, to revert on one machine after pulling
  16970. changes from another. I believe the procedure must be like this:
  16971. 1. M-x org-save-all-org-buffers
  16972. 2. Pull changes from the other machine, resolve conflicts
  16973. 3. M-x org-revert-all-org-buffers"
  16974. (interactive)
  16975. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  16976. (user-error "Abort"))
  16977. (save-excursion
  16978. (save-window-excursion
  16979. (dolist (b (buffer-list))
  16980. (when (and (with-current-buffer b (derived-mode-p 'org-mode))
  16981. (with-current-buffer b buffer-file-name))
  16982. (org-pop-to-buffer-same-window b)
  16983. (revert-buffer t 'no-confirm)))
  16984. (when (and (featurep 'org-id) org-id-track-globally)
  16985. (org-id-locations-load)))))
  16986. ;;;; Agenda files
  16987. ;;;###autoload
  16988. (defun org-switchb (&optional arg)
  16989. "Switch between Org buffers.
  16990. With \\[universal-argument] prefix, restrict available buffers to files.
  16991. With \\[universal-argument] \\[universal-argument] \
  16992. prefix, restrict available buffers to agenda files."
  16993. (interactive "P")
  16994. (let ((blist (org-buffer-list
  16995. (cond ((equal arg '(4)) 'files)
  16996. ((equal arg '(16)) 'agenda)))))
  16997. (org-pop-to-buffer-same-window
  16998. (completing-read "Org buffer: "
  16999. (mapcar #'list (mapcar #'buffer-name blist))
  17000. nil t))))
  17001. (defun org-buffer-list (&optional predicate exclude-tmp)
  17002. "Return a list of Org buffers.
  17003. PREDICATE can be `export', `files' or `agenda'.
  17004. export restrict the list to Export buffers.
  17005. files restrict the list to buffers visiting Org files.
  17006. agenda restrict the list to buffers visiting agenda files.
  17007. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  17008. (let* ((bfn nil)
  17009. (agenda-files (and (eq predicate 'agenda)
  17010. (mapcar 'file-truename (org-agenda-files t))))
  17011. (filter
  17012. (cond
  17013. ((eq predicate 'files)
  17014. (lambda (b) (with-current-buffer b (derived-mode-p 'org-mode))))
  17015. ((eq predicate 'export)
  17016. (lambda (b) (string-match "\\*Org .*Export" (buffer-name b))))
  17017. ((eq predicate 'agenda)
  17018. (lambda (b)
  17019. (with-current-buffer b
  17020. (and (derived-mode-p 'org-mode)
  17021. (setq bfn (buffer-file-name b))
  17022. (member (file-truename bfn) agenda-files)))))
  17023. (t (lambda (b) (with-current-buffer b
  17024. (or (derived-mode-p 'org-mode)
  17025. (string-match "\\*Org .*Export"
  17026. (buffer-name b)))))))))
  17027. (delq nil
  17028. (mapcar
  17029. (lambda(b)
  17030. (if (and (funcall filter b)
  17031. (or (not exclude-tmp)
  17032. (not (string-match "tmp" (buffer-name b)))))
  17033. b
  17034. nil))
  17035. (buffer-list)))))
  17036. (defun org-agenda-files (&optional unrestricted archives)
  17037. "Get the list of agenda files.
  17038. Optional UNRESTRICTED means return the full list even if a restriction
  17039. is currently in place.
  17040. When ARCHIVES is t, include all archive files that are really being
  17041. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  17042. `org-agenda-archives-mode' is t."
  17043. (let ((files
  17044. (cond
  17045. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  17046. ((stringp org-agenda-files) (org-read-agenda-file-list))
  17047. ((listp org-agenda-files) org-agenda-files)
  17048. (t (error "Invalid value of `org-agenda-files'")))))
  17049. (setq files (apply 'append
  17050. (mapcar (lambda (f)
  17051. (if (file-directory-p f)
  17052. (directory-files
  17053. f t org-agenda-file-regexp)
  17054. (list f)))
  17055. files)))
  17056. (when org-agenda-skip-unavailable-files
  17057. (setq files (delq nil
  17058. (mapcar (function
  17059. (lambda (file)
  17060. (and (file-readable-p file) file)))
  17061. files))))
  17062. (when (or (eq archives t)
  17063. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  17064. (setq files (org-add-archive-files files)))
  17065. files))
  17066. (defun org-agenda-file-p (&optional file)
  17067. "Return non-nil, if FILE is an agenda file.
  17068. If FILE is omitted, use the file associated with the current
  17069. buffer."
  17070. (let ((fname (or file (buffer-file-name))))
  17071. (and fname
  17072. (member (file-truename fname)
  17073. (mapcar #'file-truename (org-agenda-files t))))))
  17074. (defun org-edit-agenda-file-list ()
  17075. "Edit the list of agenda files.
  17076. Depending on setup, this either uses customize to edit the variable
  17077. `org-agenda-files', or it visits the file that is holding the list. In the
  17078. latter case, the buffer is set up in a way that saving it automatically kills
  17079. the buffer and restores the previous window configuration."
  17080. (interactive)
  17081. (if (stringp org-agenda-files)
  17082. (let ((cw (current-window-configuration)))
  17083. (find-file org-agenda-files)
  17084. (setq-local org-window-configuration cw)
  17085. (org-add-hook 'after-save-hook
  17086. (lambda ()
  17087. (set-window-configuration
  17088. (prog1 org-window-configuration
  17089. (kill-buffer (current-buffer))))
  17090. (org-install-agenda-files-menu)
  17091. (message "New agenda file list installed"))
  17092. nil 'local)
  17093. (message "%s" (substitute-command-keys
  17094. "Edit list and finish with \\[save-buffer]")))
  17095. (customize-variable 'org-agenda-files)))
  17096. (defun org-store-new-agenda-file-list (list)
  17097. "Set new value for the agenda file list and save it correctly."
  17098. (if (stringp org-agenda-files)
  17099. (let ((fe (org-read-agenda-file-list t)) b u)
  17100. (while (setq b (find-buffer-visiting org-agenda-files))
  17101. (kill-buffer b))
  17102. (with-temp-file org-agenda-files
  17103. (insert
  17104. (mapconcat
  17105. (lambda (f) ;; Keep un-expanded entries.
  17106. (if (setq u (assoc f fe))
  17107. (cdr u)
  17108. f))
  17109. list "\n")
  17110. "\n")))
  17111. (let ((org-mode-hook nil) (org-inhibit-startup t)
  17112. (org-insert-mode-line-in-empty-file nil))
  17113. (setq org-agenda-files list)
  17114. (customize-save-variable 'org-agenda-files org-agenda-files))))
  17115. (defun org-read-agenda-file-list (&optional pair-with-expansion)
  17116. "Read the list of agenda files from a file.
  17117. If PAIR-WITH-EXPANSION is t return pairs with un-expanded
  17118. filenames, used by `org-store-new-agenda-file-list' to write back
  17119. un-expanded file names."
  17120. (when (file-directory-p org-agenda-files)
  17121. (error "`org-agenda-files' cannot be a single directory"))
  17122. (when (stringp org-agenda-files)
  17123. (with-temp-buffer
  17124. (insert-file-contents org-agenda-files)
  17125. (mapcar
  17126. (lambda (f)
  17127. (let ((e (expand-file-name (substitute-in-file-name f)
  17128. org-directory)))
  17129. (if pair-with-expansion
  17130. (cons e f)
  17131. e)))
  17132. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*")))))
  17133. ;;;###autoload
  17134. (defun org-cycle-agenda-files ()
  17135. "Cycle through the files in `org-agenda-files'.
  17136. If the current buffer visits an agenda file, find the next one in the list.
  17137. If the current buffer does not, find the first agenda file."
  17138. (interactive)
  17139. (let* ((fs (or (org-agenda-files t)
  17140. (user-error "No agenda files")))
  17141. (files (copy-sequence fs))
  17142. (tcf (and buffer-file-name (file-truename buffer-file-name)))
  17143. file)
  17144. (when tcf
  17145. (while (and (setq file (pop files))
  17146. (not (equal (file-truename file) tcf)))))
  17147. (find-file (car (or files fs)))
  17148. (when (buffer-base-buffer) (org-pop-to-buffer-same-window (buffer-base-buffer)))))
  17149. (defun org-agenda-file-to-front (&optional to-end)
  17150. "Move/add the current file to the top of the agenda file list.
  17151. If the file is not present in the list, it is added to the front. If it is
  17152. present, it is moved there. With optional argument TO-END, add/move to the
  17153. end of the list."
  17154. (interactive "P")
  17155. (let ((org-agenda-skip-unavailable-files nil)
  17156. (file-alist (mapcar (lambda (x)
  17157. (cons (file-truename x) x))
  17158. (org-agenda-files t)))
  17159. (ctf (file-truename
  17160. (or buffer-file-name
  17161. (user-error "Please save the current buffer to a file"))))
  17162. x had)
  17163. (setq x (assoc ctf file-alist) had x)
  17164. (unless x (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  17165. (if to-end
  17166. (setq file-alist (append (delq x file-alist) (list x)))
  17167. (setq file-alist (cons x (delq x file-alist))))
  17168. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  17169. (org-install-agenda-files-menu)
  17170. (message "File %s to %s of agenda file list"
  17171. (if had "moved" "added") (if to-end "end" "front"))))
  17172. (defun org-remove-file (&optional file)
  17173. "Remove current file from the list of files in variable `org-agenda-files'.
  17174. These are the files which are being checked for agenda entries.
  17175. Optional argument FILE means use this file instead of the current."
  17176. (interactive)
  17177. (let* ((org-agenda-skip-unavailable-files nil)
  17178. (file (or file buffer-file-name
  17179. (user-error "Current buffer does not visit a file")))
  17180. (true-file (file-truename file))
  17181. (afile (abbreviate-file-name file))
  17182. (files (delq nil (mapcar
  17183. (lambda (x)
  17184. (unless (equal true-file
  17185. (file-truename x))
  17186. x))
  17187. (org-agenda-files t)))))
  17188. (if (not (= (length files) (length (org-agenda-files t))))
  17189. (progn
  17190. (org-store-new-agenda-file-list files)
  17191. (org-install-agenda-files-menu)
  17192. (message "Removed from Org Agenda list: %s" afile))
  17193. (message "File was not in list: %s (not removed)" afile))))
  17194. (defun org-file-menu-entry (file)
  17195. (vector file (list 'find-file file) t))
  17196. (defun org-check-agenda-file (file)
  17197. "Make sure FILE exists. If not, ask user what to do."
  17198. (unless (file-exists-p file)
  17199. (message "Non-existent agenda file %s. [R]emove from list or [A]bort?"
  17200. (abbreviate-file-name file))
  17201. (let ((r (downcase (read-char-exclusive))))
  17202. (cond
  17203. ((equal r ?r)
  17204. (org-remove-file file)
  17205. (throw 'nextfile t))
  17206. (t (user-error "Abort"))))))
  17207. (defun org-get-agenda-file-buffer (file)
  17208. "Get an agenda buffer visiting FILE.
  17209. If the buffer needs to be created, add it to the list of buffers
  17210. which might be released later."
  17211. (let ((buf (org-find-base-buffer-visiting file)))
  17212. (if buf
  17213. buf ; just return it
  17214. ;; Make a new buffer and remember it
  17215. (setq buf (find-file-noselect file))
  17216. (when buf (push buf org-agenda-new-buffers))
  17217. buf)))
  17218. (defun org-release-buffers (blist)
  17219. "Release all buffers in list, asking the user for confirmation when needed.
  17220. When a buffer is unmodified, it is just killed. When modified, it is saved
  17221. \(if the user agrees) and then killed."
  17222. (let (file)
  17223. (dolist (buf blist)
  17224. (setq file (buffer-file-name buf))
  17225. (when (and (buffer-modified-p buf)
  17226. file
  17227. (y-or-n-p (format "Save file %s? " file)))
  17228. (with-current-buffer buf (save-buffer)))
  17229. (kill-buffer buf))))
  17230. (defun org-agenda-prepare-buffers (files)
  17231. "Create buffers for all agenda files, protect archived trees and comments."
  17232. (interactive)
  17233. (let ((pa '(:org-archived t))
  17234. (pc '(:org-comment t))
  17235. (pall '(:org-archived t :org-comment t))
  17236. (inhibit-read-only t)
  17237. (org-inhibit-startup org-agenda-inhibit-startup)
  17238. (rea (concat ":" org-archive-tag ":"))
  17239. re pos)
  17240. (setq org-tag-alist-for-agenda nil
  17241. org-tag-groups-alist-for-agenda nil)
  17242. (save-excursion
  17243. (save-restriction
  17244. (dolist (file files)
  17245. (catch 'nextfile
  17246. (if (bufferp file)
  17247. (set-buffer file)
  17248. (org-check-agenda-file file)
  17249. (set-buffer (org-get-agenda-file-buffer file)))
  17250. (widen)
  17251. (org-set-regexps-and-options 'tags-only)
  17252. (setq pos (point))
  17253. (or (memq 'category org-agenda-ignore-properties)
  17254. (org-refresh-category-properties))
  17255. (or (memq 'stats org-agenda-ignore-properties)
  17256. (org-refresh-stats-properties))
  17257. (or (memq 'effort org-agenda-ignore-properties)
  17258. (org-refresh-effort-properties))
  17259. (or (memq 'appt org-agenda-ignore-properties)
  17260. (org-refresh-properties "APPT_WARNTIME" 'org-appt-warntime))
  17261. (setq org-todo-keywords-for-agenda
  17262. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  17263. (setq org-done-keywords-for-agenda
  17264. (append org-done-keywords-for-agenda org-done-keywords))
  17265. (setq org-todo-keyword-alist-for-agenda
  17266. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  17267. (setq org-tag-alist-for-agenda
  17268. (org-uniquify
  17269. (append org-tag-alist-for-agenda
  17270. org-current-tag-alist)))
  17271. ;; Merge current file's tag groups into global
  17272. ;; `org-tag-groups-alist-for-agenda'.
  17273. (when org-group-tags
  17274. (dolist (alist org-tag-groups-alist)
  17275. (let ((old (assoc (car alist) org-tag-groups-alist-for-agenda)))
  17276. (if old
  17277. (setcdr old (org-uniquify (append (cdr old) (cdr alist))))
  17278. (push alist org-tag-groups-alist-for-agenda)))))
  17279. (org-with-silent-modifications
  17280. (save-excursion
  17281. (remove-text-properties (point-min) (point-max) pall)
  17282. (when org-agenda-skip-archived-trees
  17283. (goto-char (point-min))
  17284. (while (re-search-forward rea nil t)
  17285. (when (org-at-heading-p t)
  17286. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  17287. (goto-char (point-min))
  17288. (setq re (format "^\\*+ .*\\<%s\\>" org-comment-string))
  17289. (while (re-search-forward re nil t)
  17290. (when (save-match-data (org-in-commented-heading-p t))
  17291. (add-text-properties
  17292. (match-beginning 0) (org-end-of-subtree t) pc)))))
  17293. (goto-char pos)))))
  17294. (setq org-todo-keywords-for-agenda
  17295. (org-uniquify org-todo-keywords-for-agenda))
  17296. (setq org-todo-keyword-alist-for-agenda
  17297. (org-uniquify org-todo-keyword-alist-for-agenda))))
  17298. ;;;; CDLaTeX minor mode
  17299. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  17300. "Keymap for the minor `org-cdlatex-mode'.")
  17301. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  17302. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  17303. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  17304. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  17305. (org-defkey org-cdlatex-mode-map "\C-c{" 'org-cdlatex-environment-indent)
  17306. (defvar org-cdlatex-texmathp-advice-is-done nil
  17307. "Flag remembering if we have applied the advice to texmathp already.")
  17308. (define-minor-mode org-cdlatex-mode
  17309. "Toggle the minor `org-cdlatex-mode'.
  17310. This mode supports entering LaTeX environment and math in LaTeX fragments
  17311. in Org-mode.
  17312. \\{org-cdlatex-mode-map}"
  17313. nil " OCDL" nil
  17314. (when org-cdlatex-mode
  17315. (require 'cdlatex)
  17316. (run-hooks 'cdlatex-mode-hook)
  17317. (cdlatex-compute-tables))
  17318. (unless org-cdlatex-texmathp-advice-is-done
  17319. (setq org-cdlatex-texmathp-advice-is-done t)
  17320. (defadvice texmathp (around org-math-always-on activate)
  17321. "Always return t in org-mode buffers.
  17322. This is because we want to insert math symbols without dollars even outside
  17323. the LaTeX math segments. If Orgmode thinks that point is actually inside
  17324. an embedded LaTeX fragment, let texmathp do its job.
  17325. \\[org-cdlatex-mode-map]"
  17326. (interactive)
  17327. (let (p)
  17328. (cond
  17329. ((not (derived-mode-p 'org-mode)) ad-do-it)
  17330. ((eq this-command 'cdlatex-math-symbol)
  17331. (setq ad-return-value t
  17332. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  17333. (t
  17334. (let ((p (org-inside-LaTeX-fragment-p)))
  17335. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  17336. (setq ad-return-value t
  17337. texmathp-why '("Org-mode embedded math" . 0))
  17338. (when p ad-do-it)))))))))
  17339. (defun turn-on-org-cdlatex ()
  17340. "Unconditionally turn on `org-cdlatex-mode'."
  17341. (org-cdlatex-mode 1))
  17342. (defun org-try-cdlatex-tab ()
  17343. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  17344. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  17345. - inside a LaTeX fragment, or
  17346. - after the first word in a line, where an abbreviation expansion could
  17347. insert a LaTeX environment."
  17348. (when org-cdlatex-mode
  17349. (cond
  17350. ;; Before any word on the line: No expansion possible.
  17351. ((save-excursion (skip-chars-backward " \t") (bolp)) nil)
  17352. ;; Just after first word on the line: Expand it. Make sure it
  17353. ;; cannot happen on headlines, though.
  17354. ((save-excursion
  17355. (skip-chars-backward "a-zA-Z0-9*")
  17356. (skip-chars-backward " \t")
  17357. (and (bolp) (not (org-at-heading-p))))
  17358. (cdlatex-tab) t)
  17359. ((org-inside-LaTeX-fragment-p) (cdlatex-tab) t))))
  17360. (defun org-cdlatex-underscore-caret (&optional _arg)
  17361. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  17362. Revert to the normal definition outside of these fragments."
  17363. (interactive "P")
  17364. (if (org-inside-LaTeX-fragment-p)
  17365. (call-interactively 'cdlatex-sub-superscript)
  17366. (let (org-cdlatex-mode)
  17367. (call-interactively (key-binding (vector last-input-event))))))
  17368. (defun org-cdlatex-math-modify (&optional _arg)
  17369. "Execute `cdlatex-math-modify' in LaTeX fragments.
  17370. Revert to the normal definition outside of these fragments."
  17371. (interactive "P")
  17372. (if (org-inside-LaTeX-fragment-p)
  17373. (call-interactively 'cdlatex-math-modify)
  17374. (let (org-cdlatex-mode)
  17375. (call-interactively (key-binding (vector last-input-event))))))
  17376. (defun org-cdlatex-environment-indent (&optional environment item)
  17377. "Execute `cdlatex-environment' and indent the inserted environment.
  17378. ENVIRONMENT and ITEM are passed to `cdlatex-environment'.
  17379. The inserted environment is indented to current indentation
  17380. unless point is at the beginning of the line, in which the
  17381. environment remains unintended."
  17382. (interactive)
  17383. ;; cdlatex-environment always return nil. Therefore, capture output
  17384. ;; first and determine if an environment was selected.
  17385. (let* ((beg (point-marker))
  17386. (end (copy-marker (point) t))
  17387. (inserted (progn
  17388. (ignore-errors (cdlatex-environment environment item))
  17389. (< beg end)))
  17390. ;; Figure out how many lines to move forward after the
  17391. ;; environment has been inserted.
  17392. (lines (when inserted
  17393. (save-excursion
  17394. (- (cl-loop while (< beg (point))
  17395. with x = 0
  17396. do (forward-line -1)
  17397. (cl-incf x)
  17398. finally return x)
  17399. (if (progn (goto-char beg)
  17400. (and (progn (skip-chars-forward " \t") (eolp))
  17401. (progn (skip-chars-backward " \t") (bolp))))
  17402. 1 0)))))
  17403. (env (org-trim (delete-and-extract-region beg end))))
  17404. (when inserted
  17405. ;; Get indentation of next line unless at column 0.
  17406. (let ((ind (if (bolp) 0
  17407. (save-excursion
  17408. (org-return-indent)
  17409. (prog1 (org-get-indentation)
  17410. (when (progn (skip-chars-forward " \t") (eolp))
  17411. (delete-region beg (point)))))))
  17412. (bol (progn (skip-chars-backward " \t") (bolp))))
  17413. ;; Insert a newline before environment unless at column zero
  17414. ;; to "escape" the current line. Insert a newline if
  17415. ;; something is one the same line as \end{ENVIRONMENT}.
  17416. (insert
  17417. (concat (unless bol "\n") env
  17418. (when (and (skip-chars-forward " \t") (not (eolp))) "\n")))
  17419. (unless (zerop ind)
  17420. (save-excursion
  17421. (goto-char beg)
  17422. (while (< (point) end)
  17423. (unless (eolp) (org-indent-line-to ind))
  17424. (forward-line))))
  17425. (goto-char beg)
  17426. (forward-line lines)
  17427. (org-indent-line-to ind)))
  17428. (set-marker beg nil)
  17429. (set-marker end nil)))
  17430. ;;;; LaTeX fragments
  17431. (defun org-inside-LaTeX-fragment-p ()
  17432. "Test if point is inside a LaTeX fragment.
  17433. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  17434. sequence appearing also before point.
  17435. Even though the matchers for math are configurable, this function assumes
  17436. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  17437. delimiters are skipped when they have been removed by customization.
  17438. The return value is nil, or a cons cell with the delimiter and the
  17439. position of this delimiter.
  17440. This function does a reasonably good job, but can locally be fooled by
  17441. for example currency specifications. For example it will assume being in
  17442. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  17443. fragments that are properly closed, but during editing, we have to live
  17444. with the uncertainty caused by missing closing delimiters. This function
  17445. looks only before point, not after."
  17446. (catch 'exit
  17447. (let ((pos (point))
  17448. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  17449. (lim (progn
  17450. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  17451. (point)))
  17452. dd-on str (start 0) m re)
  17453. (goto-char pos)
  17454. (when dodollar
  17455. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  17456. re (nth 1 (assoc "$" org-latex-regexps)))
  17457. (while (string-match re str start)
  17458. (cond
  17459. ((= (match-end 0) (length str))
  17460. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  17461. ((= (match-end 0) (- (length str) 5))
  17462. (throw 'exit nil))
  17463. (t (setq start (match-end 0))))))
  17464. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  17465. (goto-char pos)
  17466. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  17467. (and (match-beginning 2) (throw 'exit nil))
  17468. ;; count $$
  17469. (while (re-search-backward "\\$\\$" lim t)
  17470. (setq dd-on (not dd-on)))
  17471. (goto-char pos)
  17472. (when dd-on (cons "$$" m))))))
  17473. (defun org-inside-latex-macro-p ()
  17474. "Is point inside a LaTeX macro or its arguments?"
  17475. (save-match-data
  17476. (org-in-regexp
  17477. "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")))
  17478. (defun org--format-latex-make-overlay (beg end image &optional imagetype)
  17479. "Build an overlay between BEG and END using IMAGE file.
  17480. Argument IMAGETYPE is the extension of the displayed image,
  17481. as a string. It defaults to \"png\"."
  17482. (let ((ov (make-overlay beg end))
  17483. (imagetype (or (intern imagetype) 'png)))
  17484. (overlay-put ov 'org-overlay-type 'org-latex-overlay)
  17485. (overlay-put ov 'evaporate t)
  17486. (overlay-put ov
  17487. 'modification-hooks
  17488. (list (lambda (o _flag _beg _end &optional _l)
  17489. (delete-overlay o))))
  17490. (if (featurep 'xemacs)
  17491. (progn
  17492. (overlay-put ov 'invisible t)
  17493. (overlay-put ov 'end-glyph (make-glyph (vector imagetype :file image))))
  17494. (overlay-put ov
  17495. 'display
  17496. (list 'image :type imagetype :file image :ascent 'center)))))
  17497. (defun org--list-latex-overlays (&optional beg end)
  17498. "List all Org LaTeX overlays in current buffer.
  17499. Limit to overlays between BEG and END when those are provided."
  17500. (cl-remove-if-not
  17501. (lambda (o) (eq (overlay-get o 'org-overlay-type) 'org-latex-overlay))
  17502. (overlays-in (or beg (point-min)) (or end (point-max)))))
  17503. (defun org-remove-latex-fragment-image-overlays (&optional beg end)
  17504. "Remove all overlays with LaTeX fragment images in current buffer.
  17505. When optional arguments BEG and END are non-nil, remove all
  17506. overlays between them instead. Return a non-nil value when some
  17507. overlays were removed, nil otherwise."
  17508. (let ((overlays (org--list-latex-overlays beg end)))
  17509. (mapc #'delete-overlay overlays)
  17510. overlays))
  17511. (define-obsolete-function-alias
  17512. 'org-preview-latex-fragment 'org-toggle-latex-fragment "24.4")
  17513. (defun org-toggle-latex-fragment (&optional arg)
  17514. "Preview the LaTeX fragment at point, or all locally or globally.
  17515. If the cursor is on a LaTeX fragment, create the image and overlay
  17516. it over the source code, if there is none. Remove it otherwise.
  17517. If there is no fragment at point, display all fragments in the
  17518. current section.
  17519. With prefix ARG, preview or clear image for all fragments in the
  17520. current subtree or in the whole buffer when used before the first
  17521. headline. With a double prefix ARG \\[universal-argument] \
  17522. \\[universal-argument] preview or clear images
  17523. for all fragments in the buffer."
  17524. (interactive "P")
  17525. (when (display-graphic-p)
  17526. (catch 'exit
  17527. (save-excursion
  17528. (let ((window-start (window-start)) msg)
  17529. (save-restriction
  17530. (cond
  17531. ((or (equal arg '(16))
  17532. (and (equal arg '(4))
  17533. (org-with-limited-levels (org-before-first-heading-p))))
  17534. (if (org-remove-latex-fragment-image-overlays)
  17535. (progn (message "LaTeX fragments images removed from buffer")
  17536. (throw 'exit nil))
  17537. (setq msg "Creating images for buffer...")))
  17538. ((equal arg '(4))
  17539. (org-with-limited-levels (org-back-to-heading t))
  17540. (let ((beg (point))
  17541. (end (progn (org-end-of-subtree t) (point))))
  17542. (if (org-remove-latex-fragment-image-overlays beg end)
  17543. (progn
  17544. (message "LaTeX fragment images removed from subtree")
  17545. (throw 'exit nil))
  17546. (setq msg "Creating images for subtree...")
  17547. (narrow-to-region beg end))))
  17548. ((let ((datum (org-element-context)))
  17549. (when (memq (org-element-type datum)
  17550. '(latex-environment latex-fragment))
  17551. (let* ((beg (org-element-property :begin datum))
  17552. (end (org-element-property :end datum)))
  17553. (if (org-remove-latex-fragment-image-overlays beg end)
  17554. (progn (message "LaTeX fragment image removed")
  17555. (throw 'exit nil))
  17556. (narrow-to-region beg end)
  17557. (setq msg "Creating image..."))))))
  17558. (t
  17559. (org-with-limited-levels
  17560. (let ((beg (if (org-at-heading-p) (line-beginning-position)
  17561. (outline-previous-heading)
  17562. (point)))
  17563. (end (progn (outline-next-heading) (point))))
  17564. (if (org-remove-latex-fragment-image-overlays beg end)
  17565. (progn
  17566. (message "LaTeX fragment images removed from section")
  17567. (throw 'exit nil))
  17568. (setq msg "Creating images for section...")
  17569. (narrow-to-region beg end))))))
  17570. (let ((file (buffer-file-name (buffer-base-buffer))))
  17571. (org-format-latex
  17572. (concat org-preview-latex-image-directory "org-ltximg")
  17573. ;; Emacs cannot overlay images from remote hosts.
  17574. ;; Create it in `temporary-file-directory' instead.
  17575. (if (or (not file) (file-remote-p file))
  17576. temporary-file-directory
  17577. default-directory)
  17578. 'overlays msg 'forbuffer org-preview-latex-default-process)))
  17579. ;; Work around a bug that doesn't restore window's start
  17580. ;; when widening back the buffer.
  17581. (set-window-start nil window-start)
  17582. (message (concat msg "done")))))))
  17583. (defun org-format-latex
  17584. (prefix &optional dir overlays msg forbuffer processing-type)
  17585. "Replace LaTeX fragments with links to an image, and produce images.
  17586. When optional argument OVERLAYS is non-nil, display the image on
  17587. top of the fragment instead of replacing it.
  17588. PROCESSING-TYPE is the conversion method to use, as a symbol.
  17589. Some of the options can be changed using the variable
  17590. `org-format-latex-options', which see."
  17591. (when (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  17592. (unless (eq processing-type 'verbatim)
  17593. (let* ((math-regexp "\\$\\|\\\\[([]\\|^[ \t]*\\\\begin{[A-Za-z0-9*]+}")
  17594. (cnt 0)
  17595. checkdir-flag)
  17596. (goto-char (point-min))
  17597. ;; Optimize overlay creation: (info "(elisp) Managing Overlays").
  17598. (when (and overlays (memq processing-type '(dvipng imagemagick)))
  17599. (overlay-recenter (point-max)))
  17600. (while (re-search-forward math-regexp nil t)
  17601. (unless (and overlays
  17602. (eq (get-char-property (point) 'org-overlay-type)
  17603. 'org-latex-overlay))
  17604. (let* ((context (org-element-context))
  17605. (type (org-element-type context)))
  17606. (when (memq type '(latex-environment latex-fragment))
  17607. (let ((block-type (eq type 'latex-environment))
  17608. (value (org-element-property :value context))
  17609. (beg (org-element-property :begin context))
  17610. (end (save-excursion
  17611. (goto-char (org-element-property :end context))
  17612. (skip-chars-backward " \r\t\n")
  17613. (point))))
  17614. (cond
  17615. ((eq processing-type 'mathjax)
  17616. ;; Prepare for MathJax processing.
  17617. (if (not (string-match "\\`\\$\\$?" value))
  17618. (goto-char end)
  17619. (delete-region beg end)
  17620. (if (string= (match-string 0 value) "$$")
  17621. (insert "\\[" (substring value 2 -2) "\\]")
  17622. (insert "\\(" (substring value 1 -1) "\\)"))))
  17623. ((assq processing-type org-preview-latex-process-alist)
  17624. ;; Process to an image.
  17625. (cl-incf cnt)
  17626. (goto-char beg)
  17627. (let* ((processing-info
  17628. (cdr (assq processing-type org-preview-latex-process-alist)))
  17629. (face (face-at-point))
  17630. ;; Get the colors from the face at point.
  17631. (fg
  17632. (let ((color (plist-get org-format-latex-options
  17633. :foreground)))
  17634. (if (and forbuffer (eq color 'auto))
  17635. (face-attribute face :foreground nil 'default)
  17636. color)))
  17637. (bg
  17638. (let ((color (plist-get org-format-latex-options
  17639. :background)))
  17640. (if (and forbuffer (eq color 'auto))
  17641. (face-attribute face :background nil 'default)
  17642. color)))
  17643. (hash (sha1 (prin1-to-string
  17644. (list org-format-latex-header
  17645. org-latex-default-packages-alist
  17646. org-latex-packages-alist
  17647. org-format-latex-options
  17648. forbuffer value fg bg))))
  17649. (imagetype (or (plist-get processing-info :image-output-type) "png"))
  17650. (absprefix (expand-file-name prefix dir))
  17651. (linkfile (format "%s_%s.%s" prefix hash imagetype))
  17652. (movefile (format "%s_%s.%s" absprefix hash imagetype))
  17653. (sep (and block-type "\n\n"))
  17654. (link (concat sep "[[file:" linkfile "]]" sep))
  17655. (options
  17656. (org-combine-plists
  17657. org-format-latex-options
  17658. `(:foreground ,fg :background ,bg))))
  17659. (when msg (message msg cnt))
  17660. (unless checkdir-flag ; Ensure the directory exists.
  17661. (setq checkdir-flag t)
  17662. (let ((todir (file-name-directory absprefix)))
  17663. (unless (file-directory-p todir)
  17664. (make-directory todir t))))
  17665. (unless (file-exists-p movefile)
  17666. (org-create-formula-image
  17667. value movefile options forbuffer processing-type))
  17668. (if overlays
  17669. (progn
  17670. (dolist (o (overlays-in beg end))
  17671. (when (eq (overlay-get o 'org-overlay-type)
  17672. 'org-latex-overlay)
  17673. (delete-overlay o)))
  17674. (org--format-latex-make-overlay beg end movefile imagetype)
  17675. (goto-char end))
  17676. (delete-region beg end)
  17677. (insert
  17678. (org-add-props link
  17679. (list 'org-latex-src
  17680. (replace-regexp-in-string "\"" "" value)
  17681. 'org-latex-src-embed-type
  17682. (if block-type 'paragraph 'character)))))))
  17683. ((eq processing-type 'mathml)
  17684. ;; Process to MathML.
  17685. (unless (org-format-latex-mathml-available-p)
  17686. (user-error "LaTeX to MathML converter not configured"))
  17687. (cl-incf cnt)
  17688. (when msg (message msg cnt))
  17689. (goto-char beg)
  17690. (delete-region beg end)
  17691. (insert (org-format-latex-as-mathml
  17692. value block-type prefix dir)))
  17693. (t
  17694. (error "Unknown conversion process %s for LaTeX fragments"
  17695. processing-type)))))))))))
  17696. (defun org-create-math-formula (latex-frag &optional mathml-file)
  17697. "Convert LATEX-FRAG to MathML and store it in MATHML-FILE.
  17698. Use `org-latex-to-mathml-convert-command'. If the conversion is
  17699. sucessful, return the portion between \"<math...> </math>\"
  17700. elements otherwise return nil. When MATHML-FILE is specified,
  17701. write the results in to that file. When invoked as an
  17702. interactive command, prompt for LATEX-FRAG, with initial value
  17703. set to the current active region and echo the results for user
  17704. inspection."
  17705. (interactive (list (let ((frag (when (org-region-active-p)
  17706. (buffer-substring-no-properties
  17707. (region-beginning) (region-end)))))
  17708. (read-string "LaTeX Fragment: " frag nil frag))))
  17709. (unless latex-frag (user-error "Invalid LaTeX fragment"))
  17710. (let* ((tmp-in-file
  17711. (let ((file (file-relative-name
  17712. (make-temp-name (expand-file-name "ltxmathml-in")))))
  17713. (write-region latex-frag nil file)
  17714. file))
  17715. (tmp-out-file (file-relative-name
  17716. (make-temp-name (expand-file-name "ltxmathml-out"))))
  17717. (cmd (format-spec
  17718. org-latex-to-mathml-convert-command
  17719. `((?j . ,(and org-latex-to-mathml-jar-file
  17720. (shell-quote-argument
  17721. (expand-file-name
  17722. org-latex-to-mathml-jar-file))))
  17723. (?I . ,(shell-quote-argument tmp-in-file))
  17724. (?i . ,latex-frag)
  17725. (?o . ,(shell-quote-argument tmp-out-file)))))
  17726. mathml shell-command-output)
  17727. (when (org-called-interactively-p 'any)
  17728. (unless (org-format-latex-mathml-available-p)
  17729. (user-error "LaTeX to MathML converter not configured")))
  17730. (message "Running %s" cmd)
  17731. (setq shell-command-output (shell-command-to-string cmd))
  17732. (setq mathml
  17733. (when (file-readable-p tmp-out-file)
  17734. (with-current-buffer (find-file-noselect tmp-out-file t)
  17735. (goto-char (point-min))
  17736. (when (re-search-forward
  17737. (concat
  17738. (regexp-quote
  17739. "<math xmlns=\"http://www.w3.org/1998/Math/MathML\"")
  17740. "[^>]*?>"
  17741. "\\(.\\|\n\\)*"
  17742. "</math>")
  17743. nil t)
  17744. (prog1 (match-string 0) (kill-buffer))))))
  17745. (cond
  17746. (mathml
  17747. (setq mathml
  17748. (concat "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" mathml))
  17749. (when mathml-file
  17750. (write-region mathml nil mathml-file))
  17751. (when (org-called-interactively-p 'any)
  17752. (message mathml)))
  17753. ((message "LaTeX to MathML conversion failed")
  17754. (message shell-command-output)))
  17755. (delete-file tmp-in-file)
  17756. (when (file-exists-p tmp-out-file)
  17757. (delete-file tmp-out-file))
  17758. mathml))
  17759. (defun org-format-latex-as-mathml (latex-frag latex-frag-type
  17760. prefix &optional dir)
  17761. "Use `org-create-math-formula' but check local cache first."
  17762. (let* ((absprefix (expand-file-name prefix dir))
  17763. (print-length nil) (print-level nil)
  17764. (formula-id (concat
  17765. "formula-"
  17766. (sha1
  17767. (prin1-to-string
  17768. (list latex-frag
  17769. org-latex-to-mathml-convert-command)))))
  17770. (formula-cache (format "%s-%s.mathml" absprefix formula-id))
  17771. (formula-cache-dir (file-name-directory formula-cache)))
  17772. (unless (file-directory-p formula-cache-dir)
  17773. (make-directory formula-cache-dir t))
  17774. (unless (file-exists-p formula-cache)
  17775. (org-create-math-formula latex-frag formula-cache))
  17776. (if (file-exists-p formula-cache)
  17777. ;; Successful conversion. Return the link to MathML file.
  17778. (org-add-props
  17779. (format "[[file:%s]]" (file-relative-name formula-cache dir))
  17780. (list 'org-latex-src (replace-regexp-in-string "\"" "" latex-frag)
  17781. 'org-latex-src-embed-type (if latex-frag-type
  17782. 'paragraph 'character)))
  17783. ;; Failed conversion. Return the LaTeX fragment verbatim
  17784. latex-frag)))
  17785. (declare-function org-export-get-backend "ox" (name))
  17786. (declare-function org-export--get-global-options "ox" (&optional backend))
  17787. (declare-function org-export--get-inbuffer-options "ox" (&optional backend))
  17788. (declare-function org-latex-guess-inputenc "ox-latex" (header))
  17789. (declare-function org-latex-guess-babel-language "ox-latex" (header info))
  17790. (defun org-create-formula--latex-header ()
  17791. "Return LaTeX header appropriate for previewing a LaTeX snippet."
  17792. (let ((info (org-combine-plists (org-export--get-global-options
  17793. (org-export-get-backend 'latex))
  17794. (org-export--get-inbuffer-options
  17795. (org-export-get-backend 'latex)))))
  17796. (org-latex-guess-babel-language
  17797. (org-latex-guess-inputenc
  17798. (org-splice-latex-header
  17799. org-format-latex-header
  17800. org-latex-default-packages-alist
  17801. org-latex-packages-alist t
  17802. (plist-get info :latex-header)))
  17803. info)))
  17804. (defun org--get-display-dpi ()
  17805. "Get the DPI of the display.
  17806. Assumes that the display has the same pixel width in the
  17807. horizontal and vertical directions."
  17808. (if (display-graphic-p)
  17809. (round (/ (display-pixel-height)
  17810. (/ (display-mm-height) 25.4)))
  17811. (error "Attempt to calculate the dpi of a non-graphic display")))
  17812. (defun org-create-formula-image
  17813. (string tofile options buffer &optional processing-type)
  17814. "Create an image from LaTeX source using external processes.
  17815. The LaTeX STRING is saved to a temporary LaTeX file, then
  17816. converted to an image file by process PROCESSING-TYPE defined in
  17817. `org-preview-latex-process-alist'. A nil value defaults to
  17818. `org-preview-latex-default-process'.
  17819. The generated image file is eventually moved to TOFILE.
  17820. The OPTIONS argument controls the size, foreground color and
  17821. background color of the generated image.
  17822. When BUFFER non-nil, this function is used for LaTeX previewing.
  17823. Otherwise, it is used to deal with LaTeX snippets showed in
  17824. a HTML file."
  17825. (let* ((processing-type (or processing-type
  17826. org-preview-latex-default-process))
  17827. (processing-info
  17828. (cdr (assq processing-type org-preview-latex-process-alist)))
  17829. (programs (plist-get processing-info :programs))
  17830. (error-message (or (plist-get processing-info :message) ""))
  17831. (use-xcolor (plist-get processing-info :use-xcolor))
  17832. (image-input-type (plist-get processing-info :image-input-type))
  17833. (image-output-type (plist-get processing-info :image-output-type))
  17834. (post-clean (or (plist-get processing-info :post-clean)
  17835. '(".dvi" ".xdv" ".pdf" ".tex" ".aux" ".log"
  17836. ".svg" ".png" ".jpg" ".jpeg" ".out")))
  17837. (latex-header (or (plist-get processing-info :latex-header)
  17838. (org-create-formula--latex-header)))
  17839. (latex-compiler (plist-get processing-info :latex-compiler))
  17840. (image-converter (plist-get processing-info :image-converter))
  17841. (tmpdir temporary-file-directory)
  17842. (texfilebase (make-temp-name
  17843. (expand-file-name "orgtex" tmpdir)))
  17844. (texfile (concat texfilebase ".tex"))
  17845. (font-height (face-attribute 'default :height nil))
  17846. (image-size-adjust (or (plist-get processing-info :image-size-adjust)
  17847. '(1.0 . 1.0)))
  17848. (scale (* (if buffer (car image-size-adjust) (cdr image-size-adjust))
  17849. (or (plist-get options (if buffer :scale :html-scale)) 1.0)))
  17850. (dpi (* scale (floor (if buffer font-height 140.0))))
  17851. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  17852. "Black"))
  17853. (bg (or (plist-get options (if buffer :background :html-background))
  17854. "Transparent"))
  17855. (log-buf (get-buffer-create "*Org Preview LaTeX Output*"))
  17856. (resize-mini-windows nil)) ;Fix Emacs flicker when creating image.
  17857. (dolist (program programs)
  17858. (org-check-external-command program error-message))
  17859. (if use-xcolor
  17860. (progn (if (eq fg 'default)
  17861. (setq fg (org-latex-color :foreground))
  17862. (setq fg (org-latex-color-format fg)))
  17863. (if (eq bg 'default)
  17864. (setq bg (org-latex-color :background))
  17865. (setq bg (org-latex-color-format
  17866. (if (string= bg "Transparent") "white" bg))))
  17867. (with-temp-file texfile
  17868. (insert latex-header)
  17869. (insert "\n\\begin{document}\n"
  17870. "\\definecolor{fg}{rgb}{" fg "}\n"
  17871. "\\definecolor{bg}{rgb}{" bg "}\n"
  17872. "\n\\pagecolor{bg}\n"
  17873. "\n{\\color{fg}\n"
  17874. string
  17875. "\n}\n"
  17876. "\n\\end{document}\n")))
  17877. (if (eq fg 'default)
  17878. (setq fg (org-dvipng-color :foreground))
  17879. (unless (string= fg "Transparent")
  17880. (setq fg (org-dvipng-color-format fg))))
  17881. (if (eq bg 'default)
  17882. (setq bg (org-dvipng-color :background))
  17883. (unless (string= bg "Transparent")
  17884. (setq bg (org-dvipng-color-format bg))))
  17885. (with-temp-file texfile
  17886. (insert latex-header)
  17887. (insert "\n\\begin{document}\n" string "\n\\end{document}\n")))
  17888. (let* ((err-msg (format "Please adjust '%s' part of \
  17889. `org-preview-latex-process-alist'."
  17890. processing-type))
  17891. (image-input-file
  17892. (org-compile-file
  17893. texfile latex-compiler image-input-type err-msg log-buf))
  17894. (image-output-file
  17895. (org-compile-file
  17896. image-input-file image-converter image-output-type err-msg log-buf
  17897. `((?F . ,(shell-quote-argument fg))
  17898. (?B . ,(shell-quote-argument bg))
  17899. (?D . ,(shell-quote-argument (format "%s" dpi)))
  17900. (?S . ,(shell-quote-argument (format "%s" (/ dpi 140.0))))))))
  17901. (copy-file image-output-file tofile 'replace)
  17902. (dolist (e post-clean)
  17903. (when (file-exists-p (concat texfilebase e))
  17904. (delete-file (concat texfilebase e))))
  17905. image-output-file)))
  17906. (defun org-splice-latex-header (tpl def-pkg pkg snippets-p &optional extra)
  17907. "Fill a LaTeX header template TPL.
  17908. In the template, the following place holders will be recognized:
  17909. [DEFAULT-PACKAGES] \\usepackage statements for DEF-PKG
  17910. [NO-DEFAULT-PACKAGES] do not include DEF-PKG
  17911. [PACKAGES] \\usepackage statements for PKG
  17912. [NO-PACKAGES] do not include PKG
  17913. [EXTRA] the string EXTRA
  17914. [NO-EXTRA] do not include EXTRA
  17915. For backward compatibility, if both the positive and the negative place
  17916. holder is missing, the positive one (without the \"NO-\") will be
  17917. assumed to be present at the end of the template.
  17918. DEF-PKG and PKG are assumed to be alists of options/packagename lists.
  17919. EXTRA is a string.
  17920. SNIPPETS-P indicates if this is run to create snippet images for HTML."
  17921. (let (rpl (end ""))
  17922. (if (string-match "^[ \t]*\\[\\(NO-\\)?DEFAULT-PACKAGES\\][ \t]*\n?" tpl)
  17923. (setq rpl (if (or (match-end 1) (not def-pkg))
  17924. "" (org-latex-packages-to-string def-pkg snippets-p t))
  17925. tpl (replace-match rpl t t tpl))
  17926. (when def-pkg (setq end (org-latex-packages-to-string def-pkg snippets-p))))
  17927. (if (string-match "\\[\\(NO-\\)?PACKAGES\\][ \t]*\n?" tpl)
  17928. (setq rpl (if (or (match-end 1) (not pkg))
  17929. "" (org-latex-packages-to-string pkg snippets-p t))
  17930. tpl (replace-match rpl t t tpl))
  17931. (when pkg (setq end
  17932. (concat end "\n"
  17933. (org-latex-packages-to-string pkg snippets-p)))))
  17934. (if (string-match "\\[\\(NO-\\)?EXTRA\\][ \t]*\n?" tpl)
  17935. (setq rpl (if (or (match-end 1) (not extra))
  17936. "" (concat extra "\n"))
  17937. tpl (replace-match rpl t t tpl))
  17938. (when (and extra (string-match "\\S-" extra))
  17939. (setq end (concat end "\n" extra))))
  17940. (if (string-match "\\S-" end)
  17941. (concat tpl "\n" end)
  17942. tpl)))
  17943. (defun org-latex-packages-to-string (pkg &optional snippets-p newline)
  17944. "Turn an alist of packages into a string with the \\usepackage macros."
  17945. (setq pkg (mapconcat (lambda(p)
  17946. (cond
  17947. ((stringp p) p)
  17948. ((and snippets-p (>= (length p) 3) (not (nth 2 p)))
  17949. (format "%% Package %s omitted" (cadr p)))
  17950. ((equal "" (car p))
  17951. (format "\\usepackage{%s}" (cadr p)))
  17952. (t
  17953. (format "\\usepackage[%s]{%s}"
  17954. (car p) (cadr p)))))
  17955. pkg
  17956. "\n"))
  17957. (if newline (concat pkg "\n") pkg))
  17958. (defun org-dvipng-color (attr)
  17959. "Return a RGB color specification for dvipng."
  17960. (apply 'format "rgb %s %s %s"
  17961. (mapcar 'org-normalize-color
  17962. (if (featurep 'xemacs)
  17963. (color-rgb-components
  17964. (face-property 'default
  17965. (cond ((eq attr :foreground) 'foreground)
  17966. ((eq attr :background) 'background))))
  17967. (color-values (face-attribute 'default attr nil))))))
  17968. (defun org-dvipng-color-format (color-name)
  17969. "Convert COLOR-NAME to a RGB color value for dvipng."
  17970. (apply 'format "rgb %s %s %s"
  17971. (mapcar 'org-normalize-color
  17972. (color-values color-name))))
  17973. (defun org-latex-color (attr)
  17974. "Return a RGB color for the LaTeX color package."
  17975. (apply 'format "%s,%s,%s"
  17976. (mapcar 'org-normalize-color
  17977. (if (featurep 'xemacs)
  17978. (color-rgb-components
  17979. (face-property 'default
  17980. (cond ((eq attr :foreground) 'foreground)
  17981. ((eq attr :background) 'background))))
  17982. (color-values (face-attribute 'default attr nil))))))
  17983. (defun org-latex-color-format (color-name)
  17984. "Convert COLOR-NAME to a RGB color value."
  17985. (apply 'format "%s,%s,%s"
  17986. (mapcar 'org-normalize-color
  17987. (color-values color-name))))
  17988. (defun org-normalize-color (value)
  17989. "Return string to be used as color value for an RGB component."
  17990. (format "%g" (/ value 65535.0)))
  17991. ;; Image display
  17992. (defvar-local org-inline-image-overlays nil)
  17993. (defun org-toggle-inline-images (&optional include-linked)
  17994. "Toggle the display of inline images.
  17995. INCLUDE-LINKED is passed to `org-display-inline-images'."
  17996. (interactive "P")
  17997. (if org-inline-image-overlays
  17998. (progn
  17999. (org-remove-inline-images)
  18000. (when (org-called-interactively-p 'interactive)
  18001. (message "Inline image display turned off")))
  18002. (org-display-inline-images include-linked)
  18003. (when (org-called-interactively-p 'interactive)
  18004. (message (if org-inline-image-overlays
  18005. (format "%d images displayed inline"
  18006. (length org-inline-image-overlays))
  18007. "No images to display inline")))))
  18008. (defun org-redisplay-inline-images ()
  18009. "Refresh the display of inline images."
  18010. (interactive)
  18011. (if (not org-inline-image-overlays)
  18012. (org-toggle-inline-images)
  18013. (org-toggle-inline-images)
  18014. (org-toggle-inline-images)))
  18015. (defun org-display-inline-images (&optional include-linked refresh beg end)
  18016. "Display inline images.
  18017. An inline image is a link which follows either of these
  18018. conventions:
  18019. 1. Its path is a file with an extension matching return value
  18020. from `image-file-name-regexp' and it has no contents.
  18021. 2. Its description consists in a single link of the previous
  18022. type.
  18023. When optional argument INCLUDE-LINKED is non-nil, also links with
  18024. a text description part will be inlined. This can be nice for
  18025. a quick look at those images, but it does not reflect what
  18026. exported files will look like.
  18027. When optional argument REFRESH is non-nil, refresh existing
  18028. images between BEG and END. This will create new image displays
  18029. only if necessary. BEG and END default to the buffer
  18030. boundaries."
  18031. (interactive "P")
  18032. (when (display-graphic-p)
  18033. (unless refresh
  18034. (org-remove-inline-images)
  18035. (when (fboundp 'clear-image-cache) (clear-image-cache)))
  18036. (org-with-wide-buffer
  18037. (goto-char (or beg (point-min)))
  18038. (let ((case-fold-search t)
  18039. (file-extension-re (image-file-name-regexp)))
  18040. (while (re-search-forward "[][]\\[\\(?:file\\|[./~]\\)" end t)
  18041. (let ((link (save-match-data (org-element-context))))
  18042. ;; Check if we're at an inline image.
  18043. (when (and (equal (org-element-property :type link) "file")
  18044. (or include-linked
  18045. (not (org-element-property :contents-begin link)))
  18046. (let ((parent (org-element-property :parent link)))
  18047. (or (not (eq (org-element-type parent) 'link))
  18048. (not (cdr (org-element-contents parent)))))
  18049. (org-string-match-p file-extension-re
  18050. (org-element-property :path link)))
  18051. (let ((file (expand-file-name
  18052. (org-link-unescape
  18053. (org-element-property :path link)))))
  18054. (when (file-exists-p file)
  18055. (let ((width
  18056. ;; Apply `org-image-actual-width' specifications.
  18057. (cond
  18058. ((not (image-type-available-p 'imagemagick)) nil)
  18059. ((eq org-image-actual-width t) nil)
  18060. ((listp org-image-actual-width)
  18061. (or
  18062. ;; First try to find a width among
  18063. ;; attributes associated to the paragraph
  18064. ;; containing link.
  18065. (let ((paragraph
  18066. (let ((e link))
  18067. (while (and (setq e (org-element-property
  18068. :parent e))
  18069. (not (eq (org-element-type e)
  18070. 'paragraph))))
  18071. e)))
  18072. (when paragraph
  18073. (save-excursion
  18074. (goto-char (org-element-property :begin paragraph))
  18075. (when
  18076. (re-search-forward
  18077. "^[ \t]*#\\+attr_.*?: +.*?:width +\\(\\S-+\\)"
  18078. (org-element-property
  18079. :post-affiliated paragraph)
  18080. t)
  18081. (string-to-number (match-string 1))))))
  18082. ;; Otherwise, fall-back to provided number.
  18083. (car org-image-actual-width)))
  18084. ((numberp org-image-actual-width)
  18085. org-image-actual-width)))
  18086. (old (get-char-property-and-overlay
  18087. (org-element-property :begin link)
  18088. 'org-image-overlay)))
  18089. (if (and (car-safe old) refresh)
  18090. (image-refresh (overlay-get (cdr old) 'display))
  18091. (let ((image (create-image file
  18092. (and width 'imagemagick)
  18093. nil
  18094. :width width)))
  18095. (when image
  18096. (let* ((link
  18097. ;; If inline image is the description
  18098. ;; of another link, be sure to
  18099. ;; consider the latter as the one to
  18100. ;; apply the overlay on.
  18101. (let ((parent
  18102. (org-element-property :parent link)))
  18103. (if (eq (org-element-type parent) 'link)
  18104. parent
  18105. link)))
  18106. (ov (make-overlay
  18107. (org-element-property :begin link)
  18108. (progn
  18109. (goto-char
  18110. (org-element-property :end link))
  18111. (skip-chars-backward " \t")
  18112. (point)))))
  18113. (overlay-put ov 'display image)
  18114. (overlay-put ov 'face 'default)
  18115. (overlay-put ov 'org-image-overlay t)
  18116. (overlay-put
  18117. ov 'modification-hooks
  18118. (list 'org-display-inline-remove-overlay))
  18119. (push ov org-inline-image-overlays)))))))))))))))
  18120. (define-obsolete-function-alias
  18121. 'org-display-inline-modification-hook 'org-display-inline-remove-overlay "24.3")
  18122. (defun org-display-inline-remove-overlay (ov after _beg _end &optional _len)
  18123. "Remove inline-display overlay if a corresponding region is modified."
  18124. (let ((inhibit-modification-hooks t))
  18125. (when (and ov after)
  18126. (delete ov org-inline-image-overlays)
  18127. (delete-overlay ov))))
  18128. (defun org-remove-inline-images ()
  18129. "Remove inline display of images."
  18130. (interactive)
  18131. (mapc #'delete-overlay org-inline-image-overlays)
  18132. (setq org-inline-image-overlays nil))
  18133. ;;;; Key bindings
  18134. ;; Outline functions from `outline-mode-prefix-map'
  18135. ;; that can be remapped in Org:
  18136. (define-key org-mode-map [remap outline-mark-subtree] 'org-mark-subtree)
  18137. (define-key org-mode-map [remap outline-show-subtree] 'org-show-subtree)
  18138. (define-key org-mode-map [remap outline-forward-same-level]
  18139. 'org-forward-heading-same-level)
  18140. (define-key org-mode-map [remap outline-backward-same-level]
  18141. 'org-backward-heading-same-level)
  18142. (define-key org-mode-map [remap outline-show-branches]
  18143. 'org-kill-note-or-show-branches)
  18144. (define-key org-mode-map [remap outline-promote] 'org-promote-subtree)
  18145. (define-key org-mode-map [remap outline-demote] 'org-demote-subtree)
  18146. (define-key org-mode-map [remap outline-insert-heading] 'org-ctrl-c-ret)
  18147. (define-key org-mode-map [remap outline-next-visible-heading]
  18148. 'org-next-visible-heading)
  18149. (define-key org-mode-map [remap outline-previous-visible-heading]
  18150. 'org-previous-visible-heading)
  18151. (define-key org-mode-map [remap show-children] 'org-show-children)
  18152. ;; Outline functions from `outline-mode-prefix-map' that can not
  18153. ;; be remapped in Org:
  18154. ;; - the column "key binding" shows whether the Outline function is still
  18155. ;; available in Org mode on the same key that it has been bound to in
  18156. ;; Outline mode:
  18157. ;; - "overridden": key used for a different functionality in Org mode
  18158. ;; - else: key still bound to the same Outline function in Org mode
  18159. ;; | Outline function | key binding | Org replacement |
  18160. ;; |------------------------------------+-------------+--------------------------|
  18161. ;; | `outline-up-heading' | `C-c C-u' | still same function |
  18162. ;; | `outline-move-subtree-up' | overridden | better: org-shiftup |
  18163. ;; | `outline-move-subtree-down' | overridden | better: org-shiftdown |
  18164. ;; | `show-entry' | overridden | no replacement |
  18165. ;; | `show-branches' | `C-c C-k' | still same function |
  18166. ;; | `show-subtree' | overridden | visibility cycling |
  18167. ;; | `show-all' | overridden | no replacement |
  18168. ;; | `hide-subtree' | overridden | visibility cycling |
  18169. ;; | `hide-body' | overridden | no replacement |
  18170. ;; | `hide-entry' | overridden | visibility cycling |
  18171. ;; | `hide-leaves' | overridden | no replacement |
  18172. ;; | `hide-sublevels' | overridden | no replacement |
  18173. ;; | `hide-other' | overridden | no replacement |
  18174. ;; Make `C-c C-x' a prefix key
  18175. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  18176. ;; TAB key with modifiers
  18177. (org-defkey org-mode-map "\C-i" 'org-cycle)
  18178. (org-defkey org-mode-map [(tab)] 'org-cycle)
  18179. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  18180. (org-defkey org-mode-map "\M-\t" #'pcomplete)
  18181. ;; The following line is necessary under Suse GNU/Linux
  18182. (unless (featurep 'xemacs)
  18183. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  18184. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  18185. (define-key org-mode-map [backtab] 'org-shifttab)
  18186. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  18187. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  18188. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  18189. ;; Cursor keys with modifiers
  18190. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  18191. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  18192. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  18193. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  18194. (org-defkey org-mode-map [(control meta shift right)] 'org-increase-number-at-point)
  18195. (org-defkey org-mode-map [(control meta shift left)] 'org-decrease-number-at-point)
  18196. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  18197. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  18198. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  18199. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  18200. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  18201. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  18202. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  18203. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  18204. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  18205. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  18206. (org-defkey org-mode-map [(control shift up)] 'org-shiftcontrolup)
  18207. (org-defkey org-mode-map [(control shift down)] 'org-shiftcontroldown)
  18208. ;; Babel keys
  18209. (define-key org-mode-map org-babel-key-prefix org-babel-map)
  18210. (dolist (pair org-babel-key-bindings)
  18211. (define-key org-babel-map (car pair) (cdr pair)))
  18212. ;;; Extra keys for tty access.
  18213. ;; We only set them when really needed because otherwise the
  18214. ;; menus don't show the simple keys
  18215. (when (or org-use-extra-keys
  18216. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  18217. (not window-system))
  18218. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  18219. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  18220. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  18221. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  18222. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  18223. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  18224. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  18225. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  18226. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  18227. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  18228. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  18229. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  18230. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  18231. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  18232. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  18233. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  18234. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  18235. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  18236. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  18237. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  18238. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  18239. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  18240. (org-defkey org-mode-map [?\e (tab)] #'pcomplete)
  18241. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  18242. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  18243. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  18244. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  18245. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  18246. ;; All the other keys
  18247. (org-defkey org-mode-map "\C-c\C-a" 'outline-show-all) ; in case allout messed up.
  18248. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  18249. (if (boundp 'narrow-map)
  18250. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  18251. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  18252. (if (boundp 'narrow-map)
  18253. (org-defkey narrow-map "b" 'org-narrow-to-block)
  18254. (org-defkey org-mode-map "\C-xnb" 'org-narrow-to-block))
  18255. (if (boundp 'narrow-map)
  18256. (org-defkey narrow-map "e" 'org-narrow-to-element)
  18257. (org-defkey org-mode-map "\C-xne" 'org-narrow-to-element))
  18258. (org-defkey org-mode-map "\C-\M-t" 'org-transpose-element)
  18259. (org-defkey org-mode-map "\M-}" 'org-forward-element)
  18260. (org-defkey org-mode-map "\M-{" 'org-backward-element)
  18261. (org-defkey org-mode-map "\C-c\C-^" 'org-up-element)
  18262. (org-defkey org-mode-map "\C-c\C-_" 'org-down-element)
  18263. (org-defkey org-mode-map "\C-c\C-f" 'org-forward-heading-same-level)
  18264. (org-defkey org-mode-map "\C-c\C-b" 'org-backward-heading-same-level)
  18265. (org-defkey org-mode-map "\C-c\M-f" 'org-next-block)
  18266. (org-defkey org-mode-map "\C-c\M-b" 'org-previous-block)
  18267. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  18268. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-archive-subtree)
  18269. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
  18270. (org-defkey org-mode-map "\C-c\C-xd" 'org-insert-drawer)
  18271. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  18272. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  18273. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  18274. (org-defkey org-mode-map "\C-c\C-xq" 'org-toggle-tags-groups)
  18275. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  18276. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  18277. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  18278. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  18279. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  18280. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  18281. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  18282. (org-defkey org-mode-map "\C-c\M-w" 'org-copy)
  18283. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  18284. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  18285. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  18286. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  18287. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  18288. (org-defkey org-mode-map "\C-c\C-xv" 'org-copy-visible)
  18289. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  18290. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  18291. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  18292. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  18293. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  18294. (org-defkey org-mode-map "\C-c\M-l" 'org-insert-last-stored-link)
  18295. (org-defkey org-mode-map "\C-c\C-\M-l" 'org-insert-all-links)
  18296. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  18297. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  18298. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  18299. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  18300. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  18301. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  18302. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  18303. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  18304. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  18305. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  18306. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  18307. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  18308. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  18309. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  18310. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  18311. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  18312. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  18313. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  18314. (org-defkey org-mode-map "\C-c^" 'org-sort)
  18315. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  18316. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  18317. (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
  18318. (org-defkey org-mode-map [remap open-line] 'org-open-line)
  18319. (org-defkey org-mode-map [remap comment-dwim] 'org-comment-dwim)
  18320. (org-defkey org-mode-map [remap forward-paragraph] 'org-forward-paragraph)
  18321. (org-defkey org-mode-map [remap backward-paragraph] 'org-backward-paragraph)
  18322. (org-defkey org-mode-map "\M-^" 'org-delete-indentation)
  18323. (org-defkey org-mode-map "\C-m" 'org-return)
  18324. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  18325. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  18326. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  18327. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  18328. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  18329. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  18330. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  18331. (org-defkey org-mode-map "\C-c\"a" 'orgtbl-ascii-plot)
  18332. (org-defkey org-mode-map "\C-c\"g" 'org-plot/gnuplot)
  18333. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  18334. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  18335. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  18336. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  18337. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  18338. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  18339. (org-defkey org-mode-map "\C-c\C-e" 'org-export-dispatch)
  18340. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width)
  18341. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  18342. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  18343. (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
  18344. (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
  18345. (org-defkey org-mode-map "\C-c@" 'org-mark-subtree)
  18346. (org-defkey org-mode-map "\M-h" 'org-mark-element)
  18347. (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
  18348. ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
  18349. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  18350. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  18351. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  18352. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  18353. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  18354. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-in-last)
  18355. (org-defkey org-mode-map "\C-c\C-x\C-z" 'org-resolve-clocks)
  18356. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  18357. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  18358. (org-defkey org-mode-map "\C-c\C-x\C-q" 'org-clock-cancel)
  18359. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  18360. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  18361. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  18362. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-toggle-latex-fragment)
  18363. (org-defkey org-mode-map "\C-c\C-x\C-v" 'org-toggle-inline-images)
  18364. (org-defkey org-mode-map "\C-c\C-x\C-\M-v" 'org-redisplay-inline-images)
  18365. (org-defkey org-mode-map "\C-c\C-x\\" 'org-toggle-pretty-entities)
  18366. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  18367. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  18368. (org-defkey org-mode-map "\C-c\C-xP" 'org-set-property-and-value)
  18369. (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
  18370. (org-defkey org-mode-map "\C-c\C-xE" 'org-inc-effort)
  18371. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  18372. (org-defkey org-mode-map "\C-c\C-xi" 'org-columns-insert-dblock)
  18373. (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
  18374. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  18375. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  18376. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  18377. (org-defkey org-mode-map "\C-c\C-x_" 'org-timer-stop)
  18378. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  18379. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  18380. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  18381. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  18382. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  18383. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  18384. (when (featurep 'xemacs)
  18385. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  18386. (defconst org-speed-commands-default
  18387. '(
  18388. ("Outline Navigation")
  18389. ("n" . (org-speed-move-safe 'org-next-visible-heading))
  18390. ("p" . (org-speed-move-safe 'org-previous-visible-heading))
  18391. ("f" . (org-speed-move-safe 'org-forward-heading-same-level))
  18392. ("b" . (org-speed-move-safe 'org-backward-heading-same-level))
  18393. ("F" . org-next-block)
  18394. ("B" . org-previous-block)
  18395. ("u" . (org-speed-move-safe 'outline-up-heading))
  18396. ("j" . org-goto)
  18397. ("g" . (org-refile t))
  18398. ("Outline Visibility")
  18399. ("c" . org-cycle)
  18400. ("C" . org-shifttab)
  18401. (" " . org-display-outline-path)
  18402. ("s" . org-narrow-to-subtree)
  18403. ("=" . org-columns)
  18404. ("Outline Structure Editing")
  18405. ("U" . org-metaup)
  18406. ("D" . org-metadown)
  18407. ("r" . org-metaright)
  18408. ("l" . org-metaleft)
  18409. ("R" . org-shiftmetaright)
  18410. ("L" . org-shiftmetaleft)
  18411. ("i" . (progn (forward-char 1) (call-interactively
  18412. 'org-insert-heading-respect-content)))
  18413. ("^" . org-sort)
  18414. ("w" . org-refile)
  18415. ("a" . org-archive-subtree-default-with-confirmation)
  18416. ("@" . org-mark-subtree)
  18417. ("#" . org-toggle-comment)
  18418. ("Clock Commands")
  18419. ("I" . org-clock-in)
  18420. ("O" . org-clock-out)
  18421. ("Meta Data Editing")
  18422. ("t" . org-todo)
  18423. ("," . (org-priority))
  18424. ("0" . (org-priority ?\ ))
  18425. ("1" . (org-priority ?A))
  18426. ("2" . (org-priority ?B))
  18427. ("3" . (org-priority ?C))
  18428. (":" . org-set-tags-command)
  18429. ("e" . org-set-effort)
  18430. ("E" . org-inc-effort)
  18431. ("W" . (lambda(m) (interactive "sMinutes before warning: ")
  18432. (org-entry-put (point) "APPT_WARNTIME" m)))
  18433. ("Agenda Views etc")
  18434. ("v" . org-agenda)
  18435. ("/" . org-sparse-tree)
  18436. ("Misc")
  18437. ("o" . org-open-at-point)
  18438. ("?" . org-speed-command-help)
  18439. ("<" . (org-agenda-set-restriction-lock 'subtree))
  18440. (">" . (org-agenda-remove-restriction-lock))
  18441. )
  18442. "The default speed commands.")
  18443. (defun org-print-speed-command (e)
  18444. (if (> (length (car e)) 1)
  18445. (progn
  18446. (princ "\n")
  18447. (princ (car e))
  18448. (princ "\n")
  18449. (princ (make-string (length (car e)) ?-))
  18450. (princ "\n"))
  18451. (princ (car e))
  18452. (princ " ")
  18453. (if (symbolp (cdr e))
  18454. (princ (symbol-name (cdr e)))
  18455. (prin1 (cdr e)))
  18456. (princ "\n")))
  18457. (defun org-speed-command-help ()
  18458. "Show the available speed commands."
  18459. (interactive)
  18460. (if (not org-use-speed-commands)
  18461. (user-error "Speed commands are not activated, customize `org-use-speed-commands'")
  18462. (with-output-to-temp-buffer "*Help*"
  18463. (princ "User-defined Speed commands\n===========================\n")
  18464. (mapc #'org-print-speed-command org-speed-commands-user)
  18465. (princ "\n")
  18466. (princ "Built-in Speed commands\n=======================\n")
  18467. (mapc #'org-print-speed-command org-speed-commands-default))
  18468. (with-current-buffer "*Help*"
  18469. (setq truncate-lines t))))
  18470. (defun org-speed-move-safe (cmd)
  18471. "Execute CMD, but make sure that the cursor always ends up in a headline.
  18472. If not, return to the original position and throw an error."
  18473. (interactive)
  18474. (let ((pos (point)))
  18475. (call-interactively cmd)
  18476. (unless (and (bolp) (org-at-heading-p))
  18477. (goto-char pos)
  18478. (error "Boundary reached while executing %s" cmd))))
  18479. (defvar org-self-insert-command-undo-counter 0)
  18480. (defvar org-table-auto-blank-field) ; defined in org-table.el
  18481. (defvar org-speed-command nil)
  18482. (define-obsolete-function-alias
  18483. 'org-speed-command-default-hook 'org-speed-command-activate "24.3")
  18484. (defun org-speed-command-activate (keys)
  18485. "Hook for activating single-letter speed commands.
  18486. `org-speed-commands-default' specifies a minimal command set.
  18487. Use `org-speed-commands-user' for further customization."
  18488. (when (or (and (bolp) (looking-at org-outline-regexp))
  18489. (and (functionp org-use-speed-commands)
  18490. (funcall org-use-speed-commands)))
  18491. (cdr (assoc keys (append org-speed-commands-user
  18492. org-speed-commands-default)))))
  18493. (define-obsolete-function-alias
  18494. 'org-babel-speed-command-hook 'org-babel-speed-command-activate "24.3")
  18495. (defun org-babel-speed-command-activate (keys)
  18496. "Hook for activating single-letter code block commands."
  18497. (when (and (bolp) (looking-at org-babel-src-block-regexp))
  18498. (cdr (assoc keys org-babel-key-bindings))))
  18499. (defcustom org-speed-command-hook
  18500. '(org-speed-command-default-hook org-babel-speed-command-hook)
  18501. "Hook for activating speed commands at strategic locations.
  18502. Hook functions are called in sequence until a valid handler is
  18503. found.
  18504. Each hook takes a single argument, a user-pressed command key
  18505. which is also a `self-insert-command' from the global map.
  18506. Within the hook, examine the cursor position and the command key
  18507. and return nil or a valid handler as appropriate. Handler could
  18508. be one of an interactive command, a function, or a form.
  18509. Set `org-use-speed-commands' to non-nil value to enable this
  18510. hook. The default setting is `org-speed-command-activate'."
  18511. :group 'org-structure
  18512. :version "24.1"
  18513. :type 'hook)
  18514. (defun org-self-insert-command (N)
  18515. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  18516. If the cursor is in a table looking at whitespace, the whitespace is
  18517. overwritten, and the table is not marked as requiring realignment."
  18518. (interactive "p")
  18519. (org-check-before-invisible-edit 'insert)
  18520. (cond
  18521. ((and org-use-speed-commands
  18522. (let ((kv (this-command-keys-vector)))
  18523. (setq org-speed-command
  18524. (run-hook-with-args-until-success
  18525. 'org-speed-command-hook
  18526. (make-string 1 (aref kv (1- (length kv))))))))
  18527. (cond
  18528. ((commandp org-speed-command)
  18529. (setq this-command org-speed-command)
  18530. (call-interactively org-speed-command))
  18531. ((functionp org-speed-command)
  18532. (funcall org-speed-command))
  18533. ((and org-speed-command (listp org-speed-command))
  18534. (eval org-speed-command))
  18535. (t (let (org-use-speed-commands)
  18536. (call-interactively 'org-self-insert-command)))))
  18537. ((and
  18538. (org-at-table-p)
  18539. (progn
  18540. ;; Check if we blank the field, and if that triggers align.
  18541. (and (featurep 'org-table) org-table-auto-blank-field
  18542. (memq last-command
  18543. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
  18544. (if (or (eq (char-after) ?\s) (looking-at "[^|\n]* |"))
  18545. ;; Got extra space, this field does not determine
  18546. ;; column width.
  18547. (let (org-table-may-need-update) (org-table-blank-field))
  18548. ;; No extra space, this field may determine column
  18549. ;; width.
  18550. (org-table-blank-field)))
  18551. t)
  18552. (eq N 1)
  18553. (looking-at "[^|\n]* \\( \\)|"))
  18554. ;; There is room for insertion without re-aligning the table.
  18555. (delete-region (match-beginning 1) (match-end 1))
  18556. (self-insert-command N))
  18557. (t
  18558. (setq org-table-may-need-update t)
  18559. (self-insert-command N)
  18560. (org-fix-tags-on-the-fly)
  18561. (when org-self-insert-cluster-for-undo
  18562. (if (not (eq last-command 'org-self-insert-command))
  18563. (setq org-self-insert-command-undo-counter 1)
  18564. (if (>= org-self-insert-command-undo-counter 20)
  18565. (setq org-self-insert-command-undo-counter 1)
  18566. (and (> org-self-insert-command-undo-counter 0)
  18567. buffer-undo-list (listp buffer-undo-list)
  18568. (not (cadr buffer-undo-list)) ; remove nil entry
  18569. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  18570. (setq org-self-insert-command-undo-counter
  18571. (1+ org-self-insert-command-undo-counter))))))))
  18572. (defun org-check-before-invisible-edit (kind)
  18573. "Check is editing if kind KIND would be dangerous with invisible text around.
  18574. The detailed reaction depends on the user option `org-catch-invisible-edits'."
  18575. ;; First, try to get out of here as quickly as possible, to reduce overhead
  18576. (when (and org-catch-invisible-edits
  18577. (or (not (boundp 'visible-mode)) (not visible-mode))
  18578. (or (get-char-property (point) 'invisible)
  18579. (get-char-property (max (point-min) (1- (point))) 'invisible)))
  18580. ;; OK, we need to take a closer look
  18581. (let* ((invisible-at-point (get-char-property (point) 'invisible))
  18582. (invisible-before-point (unless (bobp) (get-char-property
  18583. (1- (point)) 'invisible)))
  18584. (border-and-ok-direction
  18585. (or
  18586. ;; Check if we are acting predictably before invisible text
  18587. (and invisible-at-point (not invisible-before-point)
  18588. (memq kind '(insert delete-backward)))
  18589. ;; Check if we are acting predictably after invisible text
  18590. ;; This works not well, and I have turned it off. It seems
  18591. ;; better to always show and stop after invisible text.
  18592. ;; (and (not invisible-at-point) invisible-before-point
  18593. ;; (memq kind '(insert delete)))
  18594. )))
  18595. (when (or (memq invisible-at-point '(outline org-hide-block t))
  18596. (memq invisible-before-point '(outline org-hide-block t)))
  18597. (when (eq org-catch-invisible-edits 'error)
  18598. (user-error "Editing in invisible areas is prohibited, make them visible first"))
  18599. (if (and org-custom-properties-overlays
  18600. (y-or-n-p "Display invisible properties in this buffer? "))
  18601. (org-toggle-custom-properties-visibility)
  18602. ;; Make the area visible
  18603. (save-excursion
  18604. (when invisible-before-point
  18605. (goto-char (previous-single-char-property-change
  18606. (point) 'invisible)))
  18607. (outline-show-subtree))
  18608. (cond
  18609. ((eq org-catch-invisible-edits 'show)
  18610. ;; That's it, we do the edit after showing
  18611. (message
  18612. "Unfolding invisible region around point before editing")
  18613. (sit-for 1))
  18614. ((and (eq org-catch-invisible-edits 'smart)
  18615. border-and-ok-direction)
  18616. (message "Unfolding invisible region around point before editing"))
  18617. (t
  18618. ;; Don't do the edit, make the user repeat it in full visibility
  18619. (user-error "Edit in invisible region aborted, repeat to confirm with text visible"))))))))
  18620. (defun org-fix-tags-on-the-fly ()
  18621. (when (and (equal (char-after (point-at-bol)) ?*)
  18622. (org-at-heading-p))
  18623. (org-align-tags-here org-tags-column)))
  18624. (defun org-delete-backward-char (N)
  18625. "Like `delete-backward-char', insert whitespace at field end in tables.
  18626. When deleting backwards, in tables this function will insert whitespace in
  18627. front of the next \"|\" separator, to keep the table aligned. The table will
  18628. still be marked for re-alignment if the field did fill the entire column,
  18629. because, in this case the deletion might narrow the column."
  18630. (interactive "p")
  18631. (save-match-data
  18632. (org-check-before-invisible-edit 'delete-backward)
  18633. (if (and (org-at-table-p)
  18634. (eq N 1)
  18635. (string-match "|" (buffer-substring (point-at-bol) (point)))
  18636. (looking-at ".*?|"))
  18637. (let ((pos (point))
  18638. (noalign (looking-at "[^|\n\r]* |"))
  18639. (c org-table-may-need-update))
  18640. (backward-delete-char N)
  18641. (unless overwrite-mode
  18642. (skip-chars-forward "^|")
  18643. (insert " ")
  18644. (goto-char (1- pos)))
  18645. ;; noalign: if there were two spaces at the end, this field
  18646. ;; does not determine the width of the column.
  18647. (when noalign (setq org-table-may-need-update c)))
  18648. (backward-delete-char N)
  18649. (org-fix-tags-on-the-fly))))
  18650. (defun org-delete-char (N)
  18651. "Like `delete-char', but insert whitespace at field end in tables.
  18652. When deleting characters, in tables this function will insert whitespace in
  18653. front of the next \"|\" separator, to keep the table aligned. The table will
  18654. still be marked for re-alignment if the field did fill the entire column,
  18655. because, in this case the deletion might narrow the column."
  18656. (interactive "p")
  18657. (save-match-data
  18658. (org-check-before-invisible-edit 'delete)
  18659. (if (and (org-at-table-p)
  18660. (not (bolp))
  18661. (not (= (char-after) ?|))
  18662. (eq N 1))
  18663. (if (looking-at ".*?|")
  18664. (let ((pos (point))
  18665. (noalign (looking-at "[^|\n\r]* |"))
  18666. (c org-table-may-need-update))
  18667. (replace-match
  18668. (concat (substring (match-string 0) 1 -1) " |") nil t)
  18669. (goto-char pos)
  18670. ;; noalign: if there were two spaces at the end, this field
  18671. ;; does not determine the width of the column.
  18672. (when noalign (setq org-table-may-need-update c)))
  18673. (delete-char N))
  18674. (delete-char N)
  18675. (org-fix-tags-on-the-fly))))
  18676. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  18677. (put 'org-self-insert-command 'delete-selection
  18678. (lambda ()
  18679. (not (run-hook-with-args-until-success
  18680. 'self-insert-uses-region-functions))))
  18681. (put 'orgtbl-self-insert-command 'delete-selection
  18682. (lambda ()
  18683. (not (run-hook-with-args-until-success
  18684. 'self-insert-uses-region-functions))))
  18685. (put 'org-delete-char 'delete-selection 'supersede)
  18686. (put 'org-delete-backward-char 'delete-selection 'supersede)
  18687. (put 'org-yank 'delete-selection 'yank)
  18688. ;; Make `flyspell-mode' delay after some commands
  18689. (put 'org-self-insert-command 'flyspell-delayed t)
  18690. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  18691. (put 'org-delete-char 'flyspell-delayed t)
  18692. (put 'org-delete-backward-char 'flyspell-delayed t)
  18693. ;; Make pabbrev-mode expand after org-mode commands
  18694. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  18695. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  18696. (defun org-remap (map &rest commands)
  18697. "In MAP, remap the functions given in COMMANDS.
  18698. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  18699. (let (new old)
  18700. (while commands
  18701. (setq old (pop commands) new (pop commands))
  18702. (if (fboundp 'command-remapping)
  18703. (org-defkey map (vector 'remap old) new)
  18704. (substitute-key-definition old new map global-map)))))
  18705. (defun org-transpose-words ()
  18706. "Transpose words for Org.
  18707. This uses the `org-mode-transpose-word-syntax-table' syntax
  18708. table, which interprets characters in `org-emphasis-alist' as
  18709. word constituents."
  18710. (interactive)
  18711. (with-syntax-table org-mode-transpose-word-syntax-table
  18712. (call-interactively 'transpose-words)))
  18713. (org-remap org-mode-map 'transpose-words 'org-transpose-words)
  18714. (when (eq org-enable-table-editor 'optimized)
  18715. ;; If the user wants maximum table support, we need to hijack
  18716. ;; some standard editing functions
  18717. (org-remap org-mode-map
  18718. 'self-insert-command 'org-self-insert-command
  18719. 'delete-char 'org-delete-char
  18720. 'delete-backward-char 'org-delete-backward-char)
  18721. (org-defkey org-mode-map "|" 'org-force-self-insert))
  18722. (defvar org-ctrl-c-ctrl-c-hook nil
  18723. "Hook for functions attaching themselves to `C-c C-c'.
  18724. This can be used to add additional functionality to the C-c C-c
  18725. key which executes context-dependent commands. This hook is run
  18726. before any other test, while `org-ctrl-c-ctrl-c-final-hook' is
  18727. run after the last test.
  18728. Each function will be called with no arguments. The function
  18729. must check if the context is appropriate for it to act. If yes,
  18730. it should do its thing and then return a non-nil value. If the
  18731. context is wrong, just do nothing and return nil.")
  18732. (defvar org-ctrl-c-ctrl-c-final-hook nil
  18733. "Hook for functions attaching themselves to `C-c C-c'.
  18734. This can be used to add additional functionality to the C-c C-c
  18735. key which executes context-dependent commands. This hook is run
  18736. after any other test, while `org-ctrl-c-ctrl-c-hook' is run
  18737. before the first test.
  18738. Each function will be called with no arguments. The function
  18739. must check if the context is appropriate for it to act. If yes,
  18740. it should do its thing and then return a non-nil value. If the
  18741. context is wrong, just do nothing and return nil.")
  18742. (defvar org-tab-first-hook nil
  18743. "Hook for functions to attach themselves to TAB.
  18744. See `org-ctrl-c-ctrl-c-hook' for more information.
  18745. This hook runs as the first action when TAB is pressed, even before
  18746. `org-cycle' messes around with the `outline-regexp' to cater for
  18747. inline tasks and plain list item folding.
  18748. If any function in this hook returns t, any other actions that
  18749. would have been caused by TAB (such as table field motion or visibility
  18750. cycling) will not occur.")
  18751. (defvar org-tab-after-check-for-table-hook nil
  18752. "Hook for functions to attach themselves to TAB.
  18753. See `org-ctrl-c-ctrl-c-hook' for more information.
  18754. This hook runs after it has been established that the cursor is not in a
  18755. table, but before checking if the cursor is in a headline or if global cycling
  18756. should be done.
  18757. If any function in this hook returns t, not other actions like visibility
  18758. cycling will be done.")
  18759. (defvar org-tab-after-check-for-cycling-hook nil
  18760. "Hook for functions to attach themselves to TAB.
  18761. See `org-ctrl-c-ctrl-c-hook' for more information.
  18762. This hook runs after it has been established that not table field motion and
  18763. not visibility should be done because of current context. This is probably
  18764. the place where a package like yasnippets can hook in.")
  18765. (defvar org-tab-before-tab-emulation-hook nil
  18766. "Hook for functions to attach themselves to TAB.
  18767. See `org-ctrl-c-ctrl-c-hook' for more information.
  18768. This hook runs after every other options for TAB have been exhausted, but
  18769. before indentation and \t insertion takes place.")
  18770. (defvar org-metaleft-hook nil
  18771. "Hook for functions attaching themselves to `M-left'.
  18772. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18773. (defvar org-metaright-hook nil
  18774. "Hook for functions attaching themselves to `M-right'.
  18775. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18776. (defvar org-metaup-hook nil
  18777. "Hook for functions attaching themselves to `M-up'.
  18778. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18779. (defvar org-metadown-hook nil
  18780. "Hook for functions attaching themselves to `M-down'.
  18781. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18782. (defvar org-shiftmetaleft-hook nil
  18783. "Hook for functions attaching themselves to `M-S-left'.
  18784. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18785. (defvar org-shiftmetaright-hook nil
  18786. "Hook for functions attaching themselves to `M-S-right'.
  18787. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18788. (defvar org-shiftmetaup-hook nil
  18789. "Hook for functions attaching themselves to `M-S-up'.
  18790. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18791. (defvar org-shiftmetadown-hook nil
  18792. "Hook for functions attaching themselves to `M-S-down'.
  18793. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18794. (defvar org-metareturn-hook nil
  18795. "Hook for functions attaching themselves to `M-RET'.
  18796. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18797. (defvar org-shiftup-hook nil
  18798. "Hook for functions attaching themselves to `S-up'.
  18799. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18800. (defvar org-shiftup-final-hook nil
  18801. "Hook for functions attaching themselves to `S-up'.
  18802. This one runs after all other options except shift-select have been excluded.
  18803. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18804. (defvar org-shiftdown-hook nil
  18805. "Hook for functions attaching themselves to `S-down'.
  18806. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18807. (defvar org-shiftdown-final-hook nil
  18808. "Hook for functions attaching themselves to `S-down'.
  18809. This one runs after all other options except shift-select have been excluded.
  18810. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18811. (defvar org-shiftleft-hook nil
  18812. "Hook for functions attaching themselves to `S-left'.
  18813. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18814. (defvar org-shiftleft-final-hook nil
  18815. "Hook for functions attaching themselves to `S-left'.
  18816. This one runs after all other options except shift-select have been excluded.
  18817. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18818. (defvar org-shiftright-hook nil
  18819. "Hook for functions attaching themselves to `S-right'.
  18820. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18821. (defvar org-shiftright-final-hook nil
  18822. "Hook for functions attaching themselves to `S-right'.
  18823. This one runs after all other options except shift-select have been excluded.
  18824. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18825. (defun org-modifier-cursor-error ()
  18826. "Throw an error, a modified cursor command was applied in wrong context."
  18827. (user-error "This command is active in special context like tables, headlines or items"))
  18828. (defun org-shiftselect-error ()
  18829. "Throw an error because Shift-Cursor command was applied in wrong context."
  18830. (if (and (boundp 'shift-select-mode) shift-select-mode)
  18831. (user-error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
  18832. (user-error "This command works only in special context like headlines or timestamps")))
  18833. (defun org-call-for-shift-select (cmd)
  18834. (let ((this-command-keys-shift-translated t))
  18835. (call-interactively cmd)))
  18836. (defun org-shifttab (&optional arg)
  18837. "Global visibility cycling or move to previous table field.
  18838. Call `org-table-previous-field' within a table.
  18839. When ARG is nil, cycle globally through visibility states.
  18840. When ARG is a numeric prefix, show contents of this level."
  18841. (interactive "P")
  18842. (cond
  18843. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  18844. ((integerp arg)
  18845. (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
  18846. (message "Content view to level: %d" arg)
  18847. (org-content (prefix-numeric-value arg2))
  18848. (org-cycle-show-empty-lines t)
  18849. (setq org-cycle-global-status 'overview)))
  18850. (t (call-interactively 'org-global-cycle))))
  18851. (defun org-shiftmetaleft ()
  18852. "Promote subtree or delete table column.
  18853. Calls `org-promote-subtree', `org-outdent-item-tree', or
  18854. `org-table-delete-column', depending on context. See the
  18855. individual commands for more information."
  18856. (interactive)
  18857. (cond
  18858. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  18859. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  18860. ((org-at-heading-p) (call-interactively 'org-promote-subtree))
  18861. ((if (not (org-region-active-p)) (org-at-item-p)
  18862. (save-excursion (goto-char (region-beginning))
  18863. (org-at-item-p)))
  18864. (call-interactively 'org-outdent-item-tree))
  18865. (t (org-modifier-cursor-error))))
  18866. (defun org-shiftmetaright ()
  18867. "Demote subtree or insert table column.
  18868. Calls `org-demote-subtree', `org-indent-item-tree', or
  18869. `org-table-insert-column', depending on context. See the
  18870. individual commands for more information."
  18871. (interactive)
  18872. (cond
  18873. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  18874. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  18875. ((org-at-heading-p) (call-interactively 'org-demote-subtree))
  18876. ((if (not (org-region-active-p)) (org-at-item-p)
  18877. (save-excursion (goto-char (region-beginning))
  18878. (org-at-item-p)))
  18879. (call-interactively 'org-indent-item-tree))
  18880. (t (org-modifier-cursor-error))))
  18881. (defun org-shiftmetaup (&optional _arg)
  18882. "Drag the line at point up.
  18883. In a table, kill the current row.
  18884. On a clock timestamp, update the value of the timestamp like `S-<up>'
  18885. but also adjust the previous clocked item in the clock history.
  18886. Everywhere else, drag the line at point up."
  18887. (interactive "P")
  18888. (cond
  18889. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  18890. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  18891. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  18892. (call-interactively 'org-timestamp-up)))
  18893. (t (call-interactively 'org-drag-line-backward))))
  18894. (defun org-shiftmetadown (&optional _arg)
  18895. "Drag the line at point down.
  18896. In a table, insert an empty row at the current line.
  18897. On a clock timestamp, update the value of the timestamp like `S-<down>'
  18898. but also adjust the previous clocked item in the clock history.
  18899. Everywhere else, drag the line at point down."
  18900. (interactive "P")
  18901. (cond
  18902. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  18903. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  18904. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  18905. (call-interactively 'org-timestamp-down)))
  18906. (t (call-interactively 'org-drag-line-forward))))
  18907. (defsubst org-hidden-tree-error ()
  18908. (user-error
  18909. "Hidden subtree, open with TAB or use subtree command M-S-<left>/<right>"))
  18910. (defun org-metaleft (&optional _arg)
  18911. "Promote heading, list item at point or move table column left.
  18912. Calls `org-do-promote', `org-outdent-item' or `org-table-move-column',
  18913. depending on context. With no specific context, calls the Emacs
  18914. default `backward-word'. See the individual commands for more
  18915. information.
  18916. This function runs the hook `org-metaleft-hook' as a first step,
  18917. and returns at first non-nil value."
  18918. (interactive "P")
  18919. (cond
  18920. ((run-hook-with-args-until-success 'org-metaleft-hook))
  18921. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  18922. ((org-with-limited-levels
  18923. (or (org-at-heading-p)
  18924. (and (org-region-active-p)
  18925. (save-excursion
  18926. (goto-char (region-beginning))
  18927. (org-at-heading-p)))))
  18928. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  18929. (call-interactively 'org-do-promote))
  18930. ;; At an inline task.
  18931. ((org-at-heading-p)
  18932. (call-interactively 'org-inlinetask-promote))
  18933. ((or (org-at-item-p)
  18934. (and (org-region-active-p)
  18935. (save-excursion
  18936. (goto-char (region-beginning))
  18937. (org-at-item-p))))
  18938. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  18939. (call-interactively 'org-outdent-item))
  18940. (t (call-interactively 'backward-word))))
  18941. (defun org-metaright (&optional _arg)
  18942. "Demote heading, list item at point or move table column right.
  18943. In front of a drawer or a block keyword, indent it correctly.
  18944. Calls `org-do-demote', `org-indent-item', `org-table-move-column',
  18945. `org-indnet-drawer' or `org-indent-block' depending on context.
  18946. With no specific context, calls the Emacs default `forward-word'.
  18947. See the individual commands for more information.
  18948. This function runs the hook `org-metaright-hook' as a first step,
  18949. and returns at first non-nil value."
  18950. (interactive "P")
  18951. (cond
  18952. ((run-hook-with-args-until-success 'org-metaright-hook))
  18953. ((org-at-table-p) (call-interactively 'org-table-move-column))
  18954. ((org-at-drawer-p) (call-interactively 'org-indent-drawer))
  18955. ((org-at-block-p) (call-interactively 'org-indent-block))
  18956. ((org-with-limited-levels
  18957. (or (org-at-heading-p)
  18958. (and (org-region-active-p)
  18959. (save-excursion
  18960. (goto-char (region-beginning))
  18961. (org-at-heading-p)))))
  18962. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  18963. (call-interactively 'org-do-demote))
  18964. ;; At an inline task.
  18965. ((org-at-heading-p)
  18966. (call-interactively 'org-inlinetask-demote))
  18967. ((or (org-at-item-p)
  18968. (and (org-region-active-p)
  18969. (save-excursion
  18970. (goto-char (region-beginning))
  18971. (org-at-item-p))))
  18972. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  18973. (call-interactively 'org-indent-item))
  18974. (t (call-interactively 'forward-word))))
  18975. (defun org-check-for-hidden (what)
  18976. "Check if there are hidden headlines/items in the current visual line.
  18977. WHAT can be either `headlines' or `items'. If the current line is
  18978. an outline or item heading and it has a folded subtree below it,
  18979. this function returns t, nil otherwise."
  18980. (let ((re (cond
  18981. ((eq what 'headlines) org-outline-regexp-bol)
  18982. ((eq what 'items) (org-item-beginning-re))
  18983. (t (error "This should not happen"))))
  18984. beg end)
  18985. (save-excursion
  18986. (catch 'exit
  18987. (unless (org-region-active-p)
  18988. (setq beg (point-at-bol))
  18989. (beginning-of-line 2)
  18990. (while (and (not (eobp)) ;; this is like `next-line'
  18991. (get-char-property (1- (point)) 'invisible))
  18992. (beginning-of-line 2))
  18993. (setq end (point))
  18994. (goto-char beg)
  18995. (goto-char (point-at-eol))
  18996. (setq end (max end (point)))
  18997. (while (re-search-forward re end t)
  18998. (when (get-char-property (match-beginning 0) 'invisible)
  18999. (throw 'exit t))))
  19000. nil))))
  19001. (defun org-metaup (&optional _arg)
  19002. "Move subtree up or move table row up.
  19003. Calls `org-move-subtree-up' or `org-table-move-row' or
  19004. `org-move-item-up', depending on context. See the individual commands
  19005. for more information."
  19006. (interactive "P")
  19007. (cond
  19008. ((run-hook-with-args-until-success 'org-metaup-hook))
  19009. ((org-region-active-p)
  19010. (let* ((a (min (region-beginning) (region-end)))
  19011. (b (1- (max (region-beginning) (region-end))))
  19012. (c (save-excursion (goto-char a)
  19013. (move-beginning-of-line 0)))
  19014. (d (save-excursion (goto-char a)
  19015. (move-end-of-line 0) (point))))
  19016. (transpose-regions a b c d)
  19017. (goto-char c)))
  19018. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  19019. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  19020. ((org-at-item-p) (call-interactively 'org-move-item-up))
  19021. (t (org-drag-element-backward))))
  19022. (defun org-metadown (&optional _arg)
  19023. "Move subtree down or move table row down.
  19024. Calls `org-move-subtree-down' or `org-table-move-row' or
  19025. `org-move-item-down', depending on context. See the individual
  19026. commands for more information."
  19027. (interactive "P")
  19028. (cond
  19029. ((run-hook-with-args-until-success 'org-metadown-hook))
  19030. ((org-region-active-p)
  19031. (let* ((a (min (region-beginning) (region-end)))
  19032. (b (max (region-beginning) (region-end)))
  19033. (c (save-excursion (goto-char b)
  19034. (move-beginning-of-line 1)))
  19035. (d (save-excursion (goto-char b)
  19036. (move-end-of-line 1) (1+ (point)))))
  19037. (transpose-regions a b c d)
  19038. (goto-char d)))
  19039. ((org-at-table-p) (call-interactively 'org-table-move-row))
  19040. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  19041. ((org-at-item-p) (call-interactively 'org-move-item-down))
  19042. (t (org-drag-element-forward))))
  19043. (defun org-shiftup (&optional arg)
  19044. "Increase item in timestamp or increase priority of current headline.
  19045. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  19046. depending on context. See the individual commands for more information."
  19047. (interactive "P")
  19048. (cond
  19049. ((run-hook-with-args-until-success 'org-shiftup-hook))
  19050. ((and org-support-shift-select (org-region-active-p))
  19051. (org-call-for-shift-select 'previous-line))
  19052. ((org-at-timestamp-p t)
  19053. (call-interactively (if org-edit-timestamp-down-means-later
  19054. 'org-timestamp-down 'org-timestamp-up)))
  19055. ((and (not (eq org-support-shift-select 'always))
  19056. org-enable-priority-commands
  19057. (org-at-heading-p))
  19058. (call-interactively 'org-priority-up))
  19059. ((and (not org-support-shift-select) (org-at-item-p))
  19060. (call-interactively 'org-previous-item))
  19061. ((org-clocktable-try-shift 'up arg))
  19062. ((run-hook-with-args-until-success 'org-shiftup-final-hook))
  19063. (org-support-shift-select
  19064. (org-call-for-shift-select 'previous-line))
  19065. (t (org-shiftselect-error))))
  19066. (defun org-shiftdown (&optional arg)
  19067. "Decrease item in timestamp or decrease priority of current headline.
  19068. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  19069. depending on context. See the individual commands for more information."
  19070. (interactive "P")
  19071. (cond
  19072. ((run-hook-with-args-until-success 'org-shiftdown-hook))
  19073. ((and org-support-shift-select (org-region-active-p))
  19074. (org-call-for-shift-select 'next-line))
  19075. ((org-at-timestamp-p t)
  19076. (call-interactively (if org-edit-timestamp-down-means-later
  19077. 'org-timestamp-up 'org-timestamp-down)))
  19078. ((and (not (eq org-support-shift-select 'always))
  19079. org-enable-priority-commands
  19080. (org-at-heading-p))
  19081. (call-interactively 'org-priority-down))
  19082. ((and (not org-support-shift-select) (org-at-item-p))
  19083. (call-interactively 'org-next-item))
  19084. ((org-clocktable-try-shift 'down arg))
  19085. ((run-hook-with-args-until-success 'org-shiftdown-final-hook))
  19086. (org-support-shift-select
  19087. (org-call-for-shift-select 'next-line))
  19088. (t (org-shiftselect-error))))
  19089. (defun org-shiftright (&optional arg)
  19090. "Cycle the thing at point or in the current line, depending on context.
  19091. Depending on context, this does one of the following:
  19092. - switch a timestamp at point one day into the future
  19093. - on a headline, switch to the next TODO keyword.
  19094. - on an item, switch entire list to the next bullet type
  19095. - on a property line, switch to the next allowed value
  19096. - on a clocktable definition line, move time block into the future"
  19097. (interactive "P")
  19098. (cond
  19099. ((run-hook-with-args-until-success 'org-shiftright-hook))
  19100. ((and org-support-shift-select (org-region-active-p))
  19101. (org-call-for-shift-select 'forward-char))
  19102. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  19103. ((and (not (eq org-support-shift-select 'always))
  19104. (org-at-heading-p))
  19105. (let ((org-inhibit-logging
  19106. (not org-treat-S-cursor-todo-selection-as-state-change))
  19107. (org-inhibit-blocking
  19108. (not org-treat-S-cursor-todo-selection-as-state-change)))
  19109. (org-call-with-arg 'org-todo 'right)))
  19110. ((or (and org-support-shift-select
  19111. (not (eq org-support-shift-select 'always))
  19112. (org-at-item-bullet-p))
  19113. (and (not org-support-shift-select) (org-at-item-p)))
  19114. (org-call-with-arg 'org-cycle-list-bullet nil))
  19115. ((and (not (eq org-support-shift-select 'always))
  19116. (org-at-property-p))
  19117. (call-interactively 'org-property-next-allowed-value))
  19118. ((org-clocktable-try-shift 'right arg))
  19119. ((run-hook-with-args-until-success 'org-shiftright-final-hook))
  19120. (org-support-shift-select
  19121. (org-call-for-shift-select 'forward-char))
  19122. (t (org-shiftselect-error))))
  19123. (defun org-shiftleft (&optional arg)
  19124. "Cycle the thing at point or in the current line, depending on context.
  19125. Depending on context, this does one of the following:
  19126. - switch a timestamp at point one day into the past
  19127. - on a headline, switch to the previous TODO keyword.
  19128. - on an item, switch entire list to the previous bullet type
  19129. - on a property line, switch to the previous allowed value
  19130. - on a clocktable definition line, move time block into the past"
  19131. (interactive "P")
  19132. (cond
  19133. ((run-hook-with-args-until-success 'org-shiftleft-hook))
  19134. ((and org-support-shift-select (org-region-active-p))
  19135. (org-call-for-shift-select 'backward-char))
  19136. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  19137. ((and (not (eq org-support-shift-select 'always))
  19138. (org-at-heading-p))
  19139. (let ((org-inhibit-logging
  19140. (not org-treat-S-cursor-todo-selection-as-state-change))
  19141. (org-inhibit-blocking
  19142. (not org-treat-S-cursor-todo-selection-as-state-change)))
  19143. (org-call-with-arg 'org-todo 'left)))
  19144. ((or (and org-support-shift-select
  19145. (not (eq org-support-shift-select 'always))
  19146. (org-at-item-bullet-p))
  19147. (and (not org-support-shift-select) (org-at-item-p)))
  19148. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  19149. ((and (not (eq org-support-shift-select 'always))
  19150. (org-at-property-p))
  19151. (call-interactively 'org-property-previous-allowed-value))
  19152. ((org-clocktable-try-shift 'left arg))
  19153. ((run-hook-with-args-until-success 'org-shiftleft-final-hook))
  19154. (org-support-shift-select
  19155. (org-call-for-shift-select 'backward-char))
  19156. (t (org-shiftselect-error))))
  19157. (defun org-shiftcontrolright ()
  19158. "Switch to next TODO set."
  19159. (interactive)
  19160. (cond
  19161. ((and org-support-shift-select (org-region-active-p))
  19162. (org-call-for-shift-select 'forward-word))
  19163. ((and (not (eq org-support-shift-select 'always))
  19164. (org-at-heading-p))
  19165. (org-call-with-arg 'org-todo 'nextset))
  19166. (org-support-shift-select
  19167. (org-call-for-shift-select 'forward-word))
  19168. (t (org-shiftselect-error))))
  19169. (defun org-shiftcontrolleft ()
  19170. "Switch to previous TODO set."
  19171. (interactive)
  19172. (cond
  19173. ((and org-support-shift-select (org-region-active-p))
  19174. (org-call-for-shift-select 'backward-word))
  19175. ((and (not (eq org-support-shift-select 'always))
  19176. (org-at-heading-p))
  19177. (org-call-with-arg 'org-todo 'previousset))
  19178. (org-support-shift-select
  19179. (org-call-for-shift-select 'backward-word))
  19180. (t (org-shiftselect-error))))
  19181. (defun org-shiftcontrolup (&optional n)
  19182. "Change timestamps synchronously up in CLOCK log lines.
  19183. Optional argument N tells to change by that many units."
  19184. (interactive "P")
  19185. (if (and (org-at-clock-log-p) (org-at-timestamp-p t))
  19186. (let (org-support-shift-select)
  19187. (org-clock-timestamps-up n))
  19188. (user-error "Not at a clock log")))
  19189. (defun org-shiftcontroldown (&optional n)
  19190. "Change timestamps synchronously down in CLOCK log lines.
  19191. Optional argument N tells to change by that many units."
  19192. (interactive "P")
  19193. (if (and (org-at-clock-log-p) (org-at-timestamp-p t))
  19194. (let (org-support-shift-select)
  19195. (org-clock-timestamps-down n))
  19196. (user-error "Not at a clock log")))
  19197. (defun org-increase-number-at-point (&optional inc)
  19198. "Increment the number at point.
  19199. With an optional prefix numeric argument INC, increment using
  19200. this numeric value."
  19201. (interactive "p")
  19202. (if (not (number-at-point))
  19203. (user-error "Not on a number")
  19204. (unless inc (setq inc 1))
  19205. (let ((pos (point))
  19206. (beg (skip-chars-backward "-+^/*0-9eE."))
  19207. (end (skip-chars-forward "-+^/*0-9eE^.")) nap)
  19208. (setq nap (buffer-substring-no-properties
  19209. (+ pos beg) (+ pos beg end)))
  19210. (delete-region (+ pos beg) (+ pos beg end))
  19211. (insert (calc-eval (concat (number-to-string inc) "+" nap))))
  19212. (when (org-at-table-p)
  19213. (org-table-align)
  19214. (org-table-end-of-field 1))))
  19215. (defun org-decrease-number-at-point (&optional inc)
  19216. "Decrement the number at point.
  19217. With an optional prefix numeric argument INC, decrement using
  19218. this numeric value."
  19219. (interactive "p")
  19220. (org-increase-number-at-point (- (or inc 1))))
  19221. (defun org-ctrl-c-ret ()
  19222. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  19223. (interactive)
  19224. (cond
  19225. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  19226. (t (call-interactively 'org-insert-heading))))
  19227. (defun org-find-visible ()
  19228. (let ((s (point)))
  19229. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  19230. (get-char-property s 'invisible)))
  19231. s))
  19232. (defun org-find-invisible ()
  19233. (let ((s (point)))
  19234. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  19235. (not (get-char-property s 'invisible))))
  19236. s))
  19237. (defun org-copy-visible (beg end)
  19238. "Copy the visible parts of the region."
  19239. (interactive "r")
  19240. (let (snippets s)
  19241. (save-excursion
  19242. (save-restriction
  19243. (narrow-to-region beg end)
  19244. (setq s (goto-char (point-min)))
  19245. (while (not (= (point) (point-max)))
  19246. (goto-char (org-find-invisible))
  19247. (push (buffer-substring s (point)) snippets)
  19248. (setq s (goto-char (org-find-visible))))))
  19249. (kill-new (apply 'concat (nreverse snippets)))))
  19250. (defun org-copy-special ()
  19251. "Copy region in table or copy current subtree.
  19252. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  19253. See the individual commands for more information."
  19254. (interactive)
  19255. (call-interactively
  19256. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  19257. (defun org-cut-special ()
  19258. "Cut region in table or cut current subtree.
  19259. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  19260. See the individual commands for more information."
  19261. (interactive)
  19262. (call-interactively
  19263. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  19264. (defun org-paste-special (arg)
  19265. "Paste rectangular region into table, or past subtree relative to level.
  19266. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  19267. See the individual commands for more information."
  19268. (interactive "P")
  19269. (if (org-at-table-p)
  19270. (org-table-paste-rectangle)
  19271. (org-paste-subtree arg)))
  19272. (defsubst org-in-fixed-width-region-p ()
  19273. "Is point in a fixed-width region?"
  19274. (save-match-data
  19275. (eq 'fixed-width (org-element-type (org-element-at-point)))))
  19276. (defun org-edit-special (&optional arg)
  19277. "Call a special editor for the element at point.
  19278. When at a table, call the formula editor with `org-table-edit-formulas'.
  19279. When in a source code block, call `org-edit-src-code'.
  19280. When in a fixed-width region, call `org-edit-fixed-width-region'.
  19281. When in an export block, call `org-edit-export-block'.
  19282. When at an #+INCLUDE keyword, visit the included file.
  19283. When at a footnote reference, call `org-edit-footnote-reference'
  19284. On a link, call `ffap' to visit the link at point.
  19285. Otherwise, return a user error."
  19286. (interactive "P")
  19287. (let ((element (org-element-at-point)))
  19288. (cl-assert (not buffer-read-only) nil
  19289. "Buffer is read-only: %s" (buffer-name))
  19290. (pcase (org-element-type element)
  19291. (`src-block
  19292. (if (not arg) (org-edit-src-code)
  19293. (let* ((info (org-babel-get-src-block-info))
  19294. (lang (nth 0 info))
  19295. (params (nth 2 info))
  19296. (session (cdr (assq :session params))))
  19297. (if (not session) (org-edit-src-code)
  19298. ;; At a src-block with a session and function called with
  19299. ;; an ARG: switch to the buffer related to the inferior
  19300. ;; process.
  19301. (switch-to-buffer
  19302. (funcall (intern (concat "org-babel-prep-session:" lang))
  19303. session params))))))
  19304. (`keyword
  19305. (if (member (org-element-property :key element) '("INCLUDE" "SETUPFILE"))
  19306. (org-open-link-from-string
  19307. (format "[[%s]]"
  19308. (expand-file-name
  19309. (let ((value (org-element-property :value element)))
  19310. (cond ((not (org-string-nw-p value))
  19311. (user-error "No file to edit"))
  19312. ((string-match "\\`\"\\(.*?\\)\"" value)
  19313. (match-string 1 value))
  19314. ((string-match "\\`[^ \t\"]\\S-*" value)
  19315. (match-string 0 value))
  19316. (t (user-error "No valid file specified")))))))
  19317. (user-error "No special environment to edit here")))
  19318. (`table
  19319. (if (eq (org-element-property :type element) 'table.el)
  19320. (org-edit-table.el)
  19321. (call-interactively 'org-table-edit-formulas)))
  19322. ;; Only Org tables contain `table-row' type elements.
  19323. (`table-row (call-interactively 'org-table-edit-formulas))
  19324. (`example-block (org-edit-src-code))
  19325. (`export-block (org-edit-export-block))
  19326. (`fixed-width (org-edit-fixed-width-region))
  19327. (_
  19328. ;; No notable element at point. Though, we may be at a link or
  19329. ;; a footnote reference, which are objects. Thus, scan deeper.
  19330. (let ((context (org-element-context element)))
  19331. (pcase (org-element-type context)
  19332. (`footnote-reference (org-edit-footnote-reference))
  19333. (`inline-src-block (org-edit-inline-src-code))
  19334. (`link (call-interactively #'ffap))
  19335. (_ (user-error "No special environment to edit here"))))))))
  19336. (defvar org-table-coordinate-overlays) ; defined in org-table.el
  19337. (defun org-ctrl-c-ctrl-c (&optional arg)
  19338. "Set tags in headline, or update according to changed information at point.
  19339. This command does many different things, depending on context:
  19340. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  19341. this is what we do.
  19342. - If the cursor is on a statistics cookie, update it.
  19343. - If the cursor is in a headline, prompt for tags and insert them
  19344. into the current line, aligned to `org-tags-column'. When called
  19345. with prefix arg, realign all tags in the current buffer.
  19346. - If the cursor is in one of the special #+KEYWORD lines, this
  19347. triggers scanning the buffer for these lines and updating the
  19348. information.
  19349. - If the cursor is inside a table, realign the table. This command
  19350. works even if the automatic table editor has been turned off.
  19351. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  19352. the entire table.
  19353. - If the cursor is at a footnote reference or definition, jump to
  19354. the corresponding definition or references, respectively.
  19355. - If the cursor is a the beginning of a dynamic block, update it.
  19356. - If the current buffer is a capture buffer, close note and file it.
  19357. - If the cursor is on a <<<target>>>, update radio targets and
  19358. corresponding links in this buffer.
  19359. - If the cursor is on a numbered item in a plain list, renumber the
  19360. ordered list.
  19361. - If the cursor is on a checkbox, toggle it.
  19362. - If the cursor is on a code block, evaluate it. The variable
  19363. `org-confirm-babel-evaluate' can be used to control prompting
  19364. before code block evaluation, by default every code block
  19365. evaluation requires confirmation. Code block evaluation can be
  19366. inhibited by setting `org-babel-no-eval-on-ctrl-c-ctrl-c'."
  19367. (interactive "P")
  19368. (cond
  19369. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  19370. org-occur-highlights)
  19371. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  19372. (org-remove-occur-highlights)
  19373. (message "Temporary highlights/overlays removed from current buffer"))
  19374. ((and (local-variable-p 'org-finish-function (current-buffer))
  19375. (fboundp org-finish-function))
  19376. (funcall org-finish-function))
  19377. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  19378. (t
  19379. (if (save-excursion (beginning-of-line) (looking-at "[ \t]*$"))
  19380. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  19381. (user-error "C-c C-c can do nothing useful at this location"))
  19382. (let* ((context (org-element-context))
  19383. (type (org-element-type context)))
  19384. (cl-case type
  19385. ;; When at a link, act according to the parent instead.
  19386. (link (setq context (org-element-property :parent context))
  19387. (setq type (org-element-type context)))
  19388. ;; Unsupported object types: refer to the first supported
  19389. ;; element or object containing it.
  19390. ((bold code entity export-snippet inline-babel-call inline-src-block
  19391. italic latex-fragment line-break macro strike-through subscript
  19392. superscript underline verbatim)
  19393. (setq context
  19394. (org-element-lineage
  19395. context '(radio-target paragraph verse-block table-cell)))))
  19396. ;; For convenience: at the first line of a paragraph on the
  19397. ;; same line as an item, apply function on that item instead.
  19398. (when (eq type 'paragraph)
  19399. (let ((parent (org-element-property :parent context)))
  19400. (when (and (eq (org-element-type parent) 'item)
  19401. (= (line-beginning-position)
  19402. (org-element-property :begin parent)))
  19403. (setq context parent type 'item))))
  19404. ;; Act according to type of element or object at point.
  19405. (cl-case type
  19406. (clock (org-clock-update-time-maybe))
  19407. (dynamic-block
  19408. (save-excursion
  19409. (goto-char (org-element-property :post-affiliated context))
  19410. (org-update-dblock)))
  19411. (footnote-definition
  19412. (goto-char (org-element-property :post-affiliated context))
  19413. (call-interactively 'org-footnote-action))
  19414. (footnote-reference (call-interactively 'org-footnote-action))
  19415. ((headline inlinetask)
  19416. (save-excursion (goto-char (org-element-property :begin context))
  19417. (call-interactively 'org-set-tags)))
  19418. (item
  19419. ;; At an item: a double C-u set checkbox to "[-]"
  19420. ;; unconditionally, whereas a single one will toggle its
  19421. ;; presence. Without a universal argument, if the item
  19422. ;; has a checkbox, toggle it. Otherwise repair the list.
  19423. (let* ((box (org-element-property :checkbox context))
  19424. (struct (org-element-property :structure context))
  19425. (old-struct (copy-tree struct))
  19426. (parents (org-list-parents-alist struct))
  19427. (prevs (org-list-prevs-alist struct))
  19428. (orderedp (org-not-nil (org-entry-get nil "ORDERED"))))
  19429. (org-list-set-checkbox
  19430. (org-element-property :begin context) struct
  19431. (cond ((equal arg '(16)) "[-]")
  19432. ((and (not box) (equal arg '(4))) "[ ]")
  19433. ((or (not box) (equal arg '(4))) nil)
  19434. ((eq box 'on) "[ ]")
  19435. (t "[X]")))
  19436. ;; Mimic `org-list-write-struct' but with grabbing
  19437. ;; a return value from `org-list-struct-fix-box'.
  19438. (org-list-struct-fix-ind struct parents 2)
  19439. (org-list-struct-fix-item-end struct)
  19440. (org-list-struct-fix-bul struct prevs)
  19441. (org-list-struct-fix-ind struct parents)
  19442. (let ((block-item
  19443. (org-list-struct-fix-box struct parents prevs orderedp)))
  19444. (if (and box (equal struct old-struct))
  19445. (if (equal arg '(16))
  19446. (message "Checkboxes already reset")
  19447. (user-error "Cannot toggle this checkbox: %s"
  19448. (if (eq box 'on)
  19449. "all subitems checked"
  19450. "unchecked subitems")))
  19451. (org-list-struct-apply-struct struct old-struct)
  19452. (org-update-checkbox-count-maybe))
  19453. (when block-item
  19454. (message "Checkboxes were removed due to empty box at line %d"
  19455. (org-current-line block-item))))))
  19456. (keyword
  19457. (let ((org-inhibit-startup-visibility-stuff t)
  19458. (org-startup-align-all-tables nil))
  19459. (when (boundp 'org-table-coordinate-overlays)
  19460. (mapc #'delete-overlay org-table-coordinate-overlays)
  19461. (setq org-table-coordinate-overlays nil))
  19462. (org-save-outline-visibility 'use-markers (org-mode-restart)))
  19463. (message "Local setup has been refreshed"))
  19464. (plain-list
  19465. ;; At a plain list, with a double C-u argument, set
  19466. ;; checkboxes of each item to "[-]", whereas a single one
  19467. ;; will toggle their presence according to the state of the
  19468. ;; first item in the list. Without an argument, repair the
  19469. ;; list.
  19470. (let* ((begin (org-element-property :contents-begin context))
  19471. (beginm (move-marker (make-marker) begin))
  19472. (struct (org-element-property :structure context))
  19473. (old-struct (copy-tree struct))
  19474. (first-box (save-excursion
  19475. (goto-char begin)
  19476. (looking-at org-list-full-item-re)
  19477. (match-string-no-properties 3)))
  19478. (new-box (cond ((equal arg '(16)) "[-]")
  19479. ((equal arg '(4)) (unless first-box "[ ]"))
  19480. ((equal first-box "[X]") "[ ]")
  19481. (t "[X]"))))
  19482. (cond
  19483. (arg
  19484. (dolist (pos
  19485. (org-list-get-all-items
  19486. begin struct (org-list-prevs-alist struct)))
  19487. (org-list-set-checkbox pos struct new-box)))
  19488. ((and first-box (eq (point) begin))
  19489. ;; For convenience, when point is at bol on the first
  19490. ;; item of the list and no argument is provided, simply
  19491. ;; toggle checkbox of that item, if any.
  19492. (org-list-set-checkbox begin struct new-box)))
  19493. (org-list-write-struct
  19494. struct (org-list-parents-alist struct) old-struct)
  19495. (org-update-checkbox-count-maybe)
  19496. (save-excursion (goto-char beginm) (org-list-send-list 'maybe))))
  19497. ((property-drawer node-property)
  19498. (call-interactively 'org-property-action))
  19499. ((radio-target target)
  19500. (call-interactively 'org-update-radio-target-regexp))
  19501. (statistics-cookie
  19502. (call-interactively 'org-update-statistics-cookies))
  19503. ((table table-cell table-row)
  19504. ;; At a table, recalculate every field and align it. Also
  19505. ;; send the table if necessary. If the table has
  19506. ;; a `table.el' type, just give up. At a table row or
  19507. ;; cell, maybe recalculate line but always align table.
  19508. (if (eq (org-element-property :type context) 'table.el)
  19509. (message "%s" (substitute-command-keys "\\<org-mode-map>\
  19510. Use \\[org-edit-special] to edit table.el tables"))
  19511. (let ((org-enable-table-editor t))
  19512. (if (or (eq type 'table)
  19513. ;; Check if point is at a TBLFM line.
  19514. (and (eq type 'table-row)
  19515. (= (point) (org-element-property :end context))))
  19516. (save-excursion
  19517. (if (org-at-TBLFM-p)
  19518. (progn (require 'org-table)
  19519. (org-table-calc-current-TBLFM))
  19520. (goto-char (org-element-property :contents-begin context))
  19521. (org-call-with-arg 'org-table-recalculate (or arg t))
  19522. (orgtbl-send-table 'maybe)))
  19523. (org-table-maybe-eval-formula)
  19524. (cond (arg (call-interactively 'org-table-recalculate))
  19525. ((org-table-maybe-recalculate-line))
  19526. (t (org-table-align)))))))
  19527. (timestamp (org-timestamp-change 0 'day))
  19528. (otherwise
  19529. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  19530. (user-error
  19531. "C-c C-c can do nothing useful at this location")))))))))
  19532. (defun org-mode-restart ()
  19533. (interactive)
  19534. (let ((indent-status (org-bound-and-true-p org-indent-mode)))
  19535. (funcall major-mode)
  19536. (hack-local-variables)
  19537. (when (and indent-status (not (org-bound-and-true-p org-indent-mode)))
  19538. (org-indent-mode -1)))
  19539. (message "%s restarted" major-mode))
  19540. (defun org-kill-note-or-show-branches ()
  19541. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  19542. (interactive)
  19543. (if (not org-finish-function)
  19544. (progn
  19545. (outline-hide-subtree)
  19546. (call-interactively 'outline-show-branches))
  19547. (let ((org-note-abort t))
  19548. (funcall org-finish-function))))
  19549. (defun org-delete-indentation (&optional ARG)
  19550. "Join current line to previous and fix whitespace at join.
  19551. If previous line is a headline add to headline title. Otherwise
  19552. the function calls `delete-indentation'.
  19553. With argument, join this line to following line."
  19554. (interactive "*P")
  19555. (if (save-excursion
  19556. (if ARG (beginning-of-line)
  19557. (forward-line -1))
  19558. (looking-at org-complex-heading-regexp))
  19559. ;; At headline.
  19560. (let ((tags-column (when (match-beginning 5)
  19561. (save-excursion (goto-char (match-beginning 5))
  19562. (current-column))))
  19563. (string (concat " " (progn (when ARG (forward-line 1))
  19564. (org-trim (delete-and-extract-region
  19565. (line-beginning-position)
  19566. (line-end-position)))))))
  19567. (unless (bobp) (delete-region (point) (1- (point))))
  19568. (goto-char (or (match-end 4)
  19569. (match-beginning 5)
  19570. (match-end 0)))
  19571. (skip-chars-backward " \t")
  19572. (save-excursion (insert string))
  19573. ;; Adjust alignment of tags.
  19574. (when tags-column
  19575. (org-align-tags-here (if org-auto-align-tags
  19576. org-tags-column
  19577. tags-column))))
  19578. (delete-indentation ARG)))
  19579. (defun org-open-line (n)
  19580. "Insert a new row in tables, call `open-line' elsewhere.
  19581. If `org-special-ctrl-o' is nil, just call `open-line' everywhere."
  19582. (interactive "*p")
  19583. (cond
  19584. ((not org-special-ctrl-o)
  19585. (open-line n))
  19586. ((org-at-table-p)
  19587. (org-table-insert-row))
  19588. (t
  19589. (open-line n))))
  19590. (defun org-return (&optional indent)
  19591. "Goto next table row or insert a newline.
  19592. Calls `org-table-next-row' or `newline', depending on context.
  19593. When optional INDENT argument is non-nil, call
  19594. `newline-and-indent' instead of `newline'.
  19595. When `org-return-follows-link' is non-nil and point is on
  19596. a timestamp or a link, call `org-open-at-point'. However, it
  19597. will not happen if point is in a table or on a \"dead\"
  19598. object (e.g., within a comment). In these case, you need to use
  19599. `org-open-at-point' directly."
  19600. (interactive)
  19601. (let ((context (if org-return-follows-link (org-element-context)
  19602. (org-element-at-point))))
  19603. (cond
  19604. ;; In a table, call `org-table-next-row'.
  19605. ((or (and (eq (org-element-type context) 'table)
  19606. (>= (point) (org-element-property :contents-begin context))
  19607. (< (point) (org-element-property :contents-end context)))
  19608. (org-element-lineage context '(table-row table-cell) t))
  19609. (org-table-justify-field-maybe)
  19610. (call-interactively #'org-table-next-row))
  19611. ;; On a link or a timestamp, call `org-open-at-point' if
  19612. ;; `org-return-follows-link' allows it. Tolerate fuzzy
  19613. ;; locations, e.g., in a comment, as `org-open-at-point'.
  19614. ((and org-return-follows-link
  19615. (or (org-in-regexp org-ts-regexp-both nil t)
  19616. (org-in-regexp org-tsr-regexp-both nil t)
  19617. (org-in-regexp org-any-link-re nil t)))
  19618. (call-interactively #'org-open-at-point))
  19619. ;; Insert newline in heading, but preserve tags.
  19620. ((and (not (bolp))
  19621. (save-excursion (beginning-of-line)
  19622. (looking-at org-complex-heading-regexp)))
  19623. ;; At headline. Split line. However, if point is on keyword,
  19624. ;; priority cookie or tags, do not break any of them: add
  19625. ;; a newline after the headline instead.
  19626. (let ((tags-column (and (match-beginning 5)
  19627. (save-excursion (goto-char (match-beginning 5))
  19628. (current-column))))
  19629. (string
  19630. (when (and (match-end 4)
  19631. (>= (point)
  19632. (or (match-end 3) (match-end 2) (1+ (match-end 1))))
  19633. (<= (point) (match-end 4)))
  19634. (delete-and-extract-region (point) (match-end 4)))))
  19635. (when (and tags-column string) ; Adjust tag alignment.
  19636. (org-align-tags-here
  19637. (if org-auto-align-tags org-tags-column tags-column)))
  19638. (end-of-line)
  19639. (org-show-entry)
  19640. (if indent (newline-and-indent) (newline))
  19641. (when string (save-excursion (insert (org-trim string))))))
  19642. ;; In a list, make sure indenting keeps trailing text within.
  19643. ((and indent
  19644. (not (eolp))
  19645. (org-element-lineage context '(item)))
  19646. (let ((trailing-data
  19647. (delete-and-extract-region (point) (line-end-position))))
  19648. (newline-and-indent)
  19649. (save-excursion (insert trailing-data))))
  19650. (t (if indent (newline-and-indent) (newline))))))
  19651. (defun org-return-indent ()
  19652. "Goto next table row or insert a newline and indent.
  19653. Calls `org-table-next-row' or `newline-and-indent', depending on
  19654. context. See the individual commands for more information."
  19655. (interactive)
  19656. (org-return t))
  19657. (defun org-ctrl-c-star ()
  19658. "Compute table, or change heading status of lines.
  19659. Calls `org-table-recalculate' or `org-toggle-heading',
  19660. depending on context."
  19661. (interactive)
  19662. (cond
  19663. ((org-at-table-p)
  19664. (call-interactively 'org-table-recalculate))
  19665. (t
  19666. ;; Convert all lines in region to list items
  19667. (call-interactively 'org-toggle-heading))))
  19668. (defun org-ctrl-c-minus ()
  19669. "Insert separator line in table or modify bullet status of line.
  19670. Also turns a plain line or a region of lines into list items.
  19671. Calls `org-table-insert-hline', `org-toggle-item', or
  19672. `org-cycle-list-bullet', depending on context."
  19673. (interactive)
  19674. (cond
  19675. ((org-at-table-p)
  19676. (call-interactively 'org-table-insert-hline))
  19677. ((org-region-active-p)
  19678. (call-interactively 'org-toggle-item))
  19679. ((org-in-item-p)
  19680. (call-interactively 'org-cycle-list-bullet))
  19681. (t
  19682. (call-interactively 'org-toggle-item))))
  19683. (defun org-toggle-item (arg)
  19684. "Convert headings or normal lines to items, items to normal lines.
  19685. If there is no active region, only the current line is considered.
  19686. If the first non blank line in the region is a headline, convert
  19687. all headlines to items, shifting text accordingly.
  19688. If it is an item, convert all items to normal lines.
  19689. If it is normal text, change region into a list of items.
  19690. With a prefix argument ARG, change the region in a single item."
  19691. (interactive "P")
  19692. (let ((shift-text
  19693. (function
  19694. ;; Shift text in current section to IND, from point to END.
  19695. ;; The function leaves point to END line.
  19696. (lambda (ind end)
  19697. (let ((min-i 1000) (end (copy-marker end)))
  19698. ;; First determine the minimum indentation (MIN-I) of
  19699. ;; the text.
  19700. (save-excursion
  19701. (catch 'exit
  19702. (while (< (point) end)
  19703. (let ((i (org-get-indentation)))
  19704. (cond
  19705. ;; Skip blank lines and inline tasks.
  19706. ((looking-at "^[ \t]*$"))
  19707. ((looking-at org-outline-regexp-bol))
  19708. ;; We can't find less than 0 indentation.
  19709. ((zerop i) (throw 'exit (setq min-i 0)))
  19710. ((< i min-i) (setq min-i i))))
  19711. (forward-line))))
  19712. ;; Then indent each line so that a line indented to
  19713. ;; MIN-I becomes indented to IND. Ignore blank lines
  19714. ;; and inline tasks in the process.
  19715. (let ((delta (- ind min-i)))
  19716. (while (< (point) end)
  19717. (unless (or (looking-at "^[ \t]*$")
  19718. (looking-at org-outline-regexp-bol))
  19719. (org-indent-line-to (+ (org-get-indentation) delta)))
  19720. (forward-line)))))))
  19721. (skip-blanks
  19722. (function
  19723. ;; Return beginning of first non-blank line, starting from
  19724. ;; line at POS.
  19725. (lambda (pos)
  19726. (save-excursion
  19727. (goto-char pos)
  19728. (skip-chars-forward " \r\t\n")
  19729. (point-at-bol)))))
  19730. beg end)
  19731. ;; Determine boundaries of changes.
  19732. (if (org-region-active-p)
  19733. (setq beg (funcall skip-blanks (region-beginning))
  19734. end (copy-marker (region-end)))
  19735. (setq beg (funcall skip-blanks (point-at-bol))
  19736. end (copy-marker (point-at-eol))))
  19737. ;; Depending on the starting line, choose an action on the text
  19738. ;; between BEG and END.
  19739. (org-with-limited-levels
  19740. (save-excursion
  19741. (goto-char beg)
  19742. (cond
  19743. ;; Case 1. Start at an item: de-itemize. Note that it only
  19744. ;; happens when a region is active: `org-ctrl-c-minus'
  19745. ;; would call `org-cycle-list-bullet' otherwise.
  19746. ((org-at-item-p)
  19747. (while (< (point) end)
  19748. (when (org-at-item-p)
  19749. (skip-chars-forward " \t")
  19750. (delete-region (point) (match-end 0)))
  19751. (forward-line)))
  19752. ;; Case 2. Start at an heading: convert to items.
  19753. ((org-at-heading-p)
  19754. (let* ((bul (org-list-bullet-string "-"))
  19755. (bul-len (length bul))
  19756. (done (org-entry-is-done-p))
  19757. (todo (org-entry-is-todo-p))
  19758. ;; Indentation of the first heading. It should be
  19759. ;; relative to the indentation of its parent, if any.
  19760. (start-ind (save-excursion
  19761. (cond
  19762. ((not org-adapt-indentation) 0)
  19763. ((not (outline-previous-heading)) 0)
  19764. (t (length (match-string 0))))))
  19765. ;; Level of first heading. Further headings will be
  19766. ;; compared to it to determine hierarchy in the list.
  19767. (ref-level (org-reduced-level (org-outline-level))))
  19768. (when (or done todo) (org-todo ""))
  19769. (while (< (point) end)
  19770. (let* ((level (org-reduced-level (org-outline-level)))
  19771. (delta (max 0 (- level ref-level))))
  19772. ;; If current headline is less indented than the first
  19773. ;; one, set it as reference, in order to preserve
  19774. ;; subtrees.
  19775. (when (< level ref-level) (setq ref-level level))
  19776. (replace-match bul t t)
  19777. (org-indent-line-to (+ start-ind (* delta bul-len)))
  19778. (when (or done todo)
  19779. (let* ((struct (org-list-struct))
  19780. (old (copy-tree struct)))
  19781. (org-list-set-checkbox (line-beginning-position)
  19782. struct
  19783. (if done "[X]" "[ ]"))
  19784. (org-list-write-struct struct
  19785. (org-list-parents-alist struct)
  19786. old)))
  19787. ;; Ensure all text down to END (or SECTION-END) belongs
  19788. ;; to the newly created item.
  19789. (let ((section-end (save-excursion
  19790. (or (outline-next-heading) (point)))))
  19791. (forward-line)
  19792. (funcall shift-text
  19793. (+ start-ind (* (1+ delta) bul-len))
  19794. (min end section-end)))))))
  19795. ;; Case 3. Normal line with ARG: make the first line of region
  19796. ;; an item, and shift indentation of others lines to
  19797. ;; set them as item's body.
  19798. (arg (let* ((bul (org-list-bullet-string "-"))
  19799. (bul-len (length bul))
  19800. (ref-ind (org-get-indentation)))
  19801. (skip-chars-forward " \t")
  19802. (insert bul)
  19803. (forward-line)
  19804. (while (< (point) end)
  19805. ;; Ensure that lines less indented than first one
  19806. ;; still get included in item body.
  19807. (funcall shift-text
  19808. (+ ref-ind bul-len)
  19809. (min end (save-excursion (or (outline-next-heading)
  19810. (point)))))
  19811. (forward-line))))
  19812. ;; Case 4. Normal line without ARG: turn each non-item line
  19813. ;; into an item.
  19814. (t
  19815. (while (< (point) end)
  19816. (unless (or (org-at-heading-p) (org-at-item-p))
  19817. (when (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  19818. (replace-match
  19819. (concat "\\1" (org-list-bullet-string "-") "\\2"))))
  19820. (forward-line))))))))
  19821. (defun org-toggle-heading (&optional nstars)
  19822. "Convert headings to normal text, or items or text to headings.
  19823. If there is no active region, only convert the current line.
  19824. With a \\[universal-argument] prefix, convert the whole list at
  19825. point into heading.
  19826. In a region:
  19827. - If the first non blank line is a headline, remove the stars
  19828. from all headlines in the region.
  19829. - If it is a normal line, turn each and every normal line (i.e.,
  19830. not an heading or an item) in the region into headings. If you
  19831. want to convert only the first line of this region, use one
  19832. universal prefix argument.
  19833. - If it is a plain list item, turn all plain list items into headings.
  19834. When converting a line into a heading, the number of stars is chosen
  19835. such that the lines become children of the current entry. However,
  19836. when a numeric prefix argument is given, its value determines the
  19837. number of stars to add."
  19838. (interactive "P")
  19839. (let ((skip-blanks
  19840. (function
  19841. ;; Return beginning of first non-blank line, starting from
  19842. ;; line at POS.
  19843. (lambda (pos)
  19844. (save-excursion
  19845. (goto-char pos)
  19846. (while (org-at-comment-p) (forward-line))
  19847. (skip-chars-forward " \r\t\n")
  19848. (point-at-bol)))))
  19849. beg end toggled)
  19850. ;; Determine boundaries of changes. If a universal prefix has
  19851. ;; been given, put the list in a region. If region ends at a bol,
  19852. ;; do not consider the last line to be in the region.
  19853. (when (and current-prefix-arg (org-at-item-p))
  19854. (when (listp current-prefix-arg) (setq current-prefix-arg 1))
  19855. (org-mark-element))
  19856. (if (org-region-active-p)
  19857. (setq beg (funcall skip-blanks (region-beginning))
  19858. end (copy-marker (save-excursion
  19859. (goto-char (region-end))
  19860. (if (bolp) (point) (point-at-eol)))))
  19861. (setq beg (funcall skip-blanks (point-at-bol))
  19862. end (copy-marker (point-at-eol))))
  19863. ;; Ensure inline tasks don't count as headings.
  19864. (org-with-limited-levels
  19865. (save-excursion
  19866. (goto-char beg)
  19867. (cond
  19868. ;; Case 1. Started at an heading: de-star headings.
  19869. ((org-at-heading-p)
  19870. (while (< (point) end)
  19871. (when (org-at-heading-p t)
  19872. (looking-at org-outline-regexp) (replace-match "")
  19873. (setq toggled t))
  19874. (forward-line)))
  19875. ;; Case 2. Started at an item: change items into headlines.
  19876. ;; One star will be added by `org-list-to-subtree'.
  19877. ((org-at-item-p)
  19878. (while (< (point) end)
  19879. (when (org-at-item-p)
  19880. ;; Pay attention to cases when region ends before list.
  19881. (let* ((struct (org-list-struct))
  19882. (list-end
  19883. (min (org-list-get-bottom-point struct) (1+ end))))
  19884. (save-restriction
  19885. (narrow-to-region (point) list-end)
  19886. (insert (org-list-to-subtree (org-list-to-lisp t)) "\n")))
  19887. (setq toggled t))
  19888. (forward-line)))
  19889. ;; Case 3. Started at normal text: make every line an heading,
  19890. ;; skipping headlines and items.
  19891. (t (let* ((stars
  19892. (make-string
  19893. (if (numberp nstars) nstars (or (org-current-level) 0)) ?*))
  19894. (add-stars
  19895. (cond (nstars "") ; stars from prefix only
  19896. ((equal stars "") "*") ; before first heading
  19897. (org-odd-levels-only "**") ; inside heading, odd
  19898. (t "*"))) ; inside heading, oddeven
  19899. (rpl (concat stars add-stars " "))
  19900. (lend (when (listp nstars) (save-excursion (end-of-line) (point)))))
  19901. (while (< (point) (if (equal nstars '(4)) lend end))
  19902. (when (and (not (or (org-at-heading-p) (org-at-item-p) (org-at-comment-p)))
  19903. (looking-at "\\([ \t]*\\)\\(\\S-\\)"))
  19904. (replace-match (concat rpl (match-string 2))) (setq toggled t))
  19905. (forward-line)))))))
  19906. (unless toggled (message "Cannot toggle heading from here"))))
  19907. (defun org-meta-return (&optional _arg)
  19908. "Insert a new heading or wrap a region in a table.
  19909. Calls `org-insert-heading' or `org-table-wrap-region', depending
  19910. on context. See the individual commands for more information."
  19911. (interactive)
  19912. (org-check-before-invisible-edit 'insert)
  19913. (or (run-hook-with-args-until-success 'org-metareturn-hook)
  19914. (call-interactively (if (org-at-table-p) #'org-table-wrap-region
  19915. #'org-insert-heading))))
  19916. ;;; Menu entries
  19917. (defsubst org-in-subtree-not-table-p ()
  19918. "Are we in a subtree and not in a table?"
  19919. (and (not (org-before-first-heading-p))
  19920. (not (org-at-table-p))))
  19921. ;; Define the Org-mode menus
  19922. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  19923. '("Tbl"
  19924. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  19925. ["Next Field" org-cycle (org-at-table-p)]
  19926. ["Previous Field" org-shifttab (org-at-table-p)]
  19927. ["Next Row" org-return (org-at-table-p)]
  19928. "--"
  19929. ["Blank Field" org-table-blank-field (org-at-table-p)]
  19930. ["Edit Field" org-table-edit-field (org-at-table-p)]
  19931. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  19932. "--"
  19933. ("Column"
  19934. ["Move Column Left" org-metaleft (org-at-table-p)]
  19935. ["Move Column Right" org-metaright (org-at-table-p)]
  19936. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  19937. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  19938. ("Row"
  19939. ["Move Row Up" org-metaup (org-at-table-p)]
  19940. ["Move Row Down" org-metadown (org-at-table-p)]
  19941. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  19942. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  19943. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  19944. "--"
  19945. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  19946. ("Rectangle"
  19947. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  19948. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  19949. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  19950. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  19951. "--"
  19952. ("Calculate"
  19953. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  19954. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  19955. ["Edit Formulas" org-edit-special (org-at-table-p)]
  19956. "--"
  19957. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  19958. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  19959. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  19960. "--"
  19961. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  19962. "--"
  19963. ["Sum Column/Rectangle" org-table-sum
  19964. (or (org-at-table-p) (org-region-active-p))]
  19965. ["Which Column?" org-table-current-column (org-at-table-p)])
  19966. ["Debug Formulas"
  19967. org-table-toggle-formula-debugger
  19968. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  19969. ["Show Col/Row Numbers"
  19970. org-table-toggle-coordinate-overlays
  19971. :style toggle
  19972. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  19973. "--"
  19974. ["Create" org-table-create (and (not (org-at-table-p))
  19975. org-enable-table-editor)]
  19976. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  19977. ["Import from File" org-table-import (not (org-at-table-p))]
  19978. ["Export to File" org-table-export (org-at-table-p)]
  19979. "--"
  19980. ["Create/Convert from/to table.el" org-table-create-with-table.el t]
  19981. "--"
  19982. ("Plot"
  19983. ["Ascii plot" orgtbl-ascii-plot :active (org-at-table-p) :keys "C-c \" a"]
  19984. ["Gnuplot" org-plot/gnuplot :active (org-at-table-p) :keys "C-c \" g"])))
  19985. (easy-menu-define org-org-menu org-mode-map "Org menu"
  19986. '("Org"
  19987. ("Show/Hide"
  19988. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  19989. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  19990. ["Sparse Tree..." org-sparse-tree t]
  19991. ["Reveal Context" org-reveal t]
  19992. ["Show All" outline-show-all t]
  19993. "--"
  19994. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  19995. "--"
  19996. ["New Heading" org-insert-heading t]
  19997. ("Navigate Headings"
  19998. ["Up" outline-up-heading t]
  19999. ["Next" outline-next-visible-heading t]
  20000. ["Previous" outline-previous-visible-heading t]
  20001. ["Next Same Level" outline-forward-same-level t]
  20002. ["Previous Same Level" outline-backward-same-level t]
  20003. "--"
  20004. ["Jump" org-goto t])
  20005. ("Edit Structure"
  20006. ["Refile Subtree" org-refile (org-in-subtree-not-table-p)]
  20007. "--"
  20008. ["Move Subtree Up" org-metaup (org-at-heading-p)]
  20009. ["Move Subtree Down" org-metadown (org-at-heading-p)]
  20010. "--"
  20011. ["Copy Subtree" org-copy-special (org-in-subtree-not-table-p)]
  20012. ["Cut Subtree" org-cut-special (org-in-subtree-not-table-p)]
  20013. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  20014. "--"
  20015. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  20016. "--"
  20017. ["Copy visible text" org-copy-visible t]
  20018. "--"
  20019. ["Promote Heading" org-metaleft (org-in-subtree-not-table-p)]
  20020. ["Promote Subtree" org-shiftmetaleft (org-in-subtree-not-table-p)]
  20021. ["Demote Heading" org-metaright (org-in-subtree-not-table-p)]
  20022. ["Demote Subtree" org-shiftmetaright (org-in-subtree-not-table-p)]
  20023. "--"
  20024. ["Sort Region/Children" org-sort t]
  20025. "--"
  20026. ["Convert to odd levels" org-convert-to-odd-levels t]
  20027. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  20028. ("Editing"
  20029. ["Emphasis..." org-emphasize t]
  20030. ["Edit Source Example" org-edit-special t]
  20031. "--"
  20032. ["Footnote new/jump" org-footnote-action t]
  20033. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  20034. ("Archive"
  20035. ["Archive (default method)" org-archive-subtree-default (org-in-subtree-not-table-p)]
  20036. "--"
  20037. ["Move Subtree to Archive file" org-archive-subtree (org-in-subtree-not-table-p)]
  20038. ["Toggle ARCHIVE tag" org-toggle-archive-tag (org-in-subtree-not-table-p)]
  20039. ["Move subtree to Archive sibling" org-archive-to-archive-sibling (org-in-subtree-not-table-p)]
  20040. )
  20041. "--"
  20042. ("Hyperlinks"
  20043. ["Store Link (Global)" org-store-link t]
  20044. ["Find existing link to here" org-occur-link-in-agenda-files t]
  20045. ["Insert Link" org-insert-link t]
  20046. ["Follow Link" org-open-at-point t]
  20047. "--"
  20048. ["Next link" org-next-link t]
  20049. ["Previous link" org-previous-link t]
  20050. "--"
  20051. ["Descriptive Links"
  20052. org-toggle-link-display
  20053. :style radio
  20054. :selected org-descriptive-links
  20055. ]
  20056. ["Literal Links"
  20057. org-toggle-link-display
  20058. :style radio
  20059. :selected (not org-descriptive-links)])
  20060. "--"
  20061. ("TODO Lists"
  20062. ["TODO/DONE/-" org-todo t]
  20063. ("Select keyword"
  20064. ["Next keyword" org-shiftright (org-at-heading-p)]
  20065. ["Previous keyword" org-shiftleft (org-at-heading-p)]
  20066. ["Complete Keyword" pcomplete (assq :todo-keyword (org-context))]
  20067. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))]
  20068. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))])
  20069. ["Show TODO Tree" org-show-todo-tree :active t :keys "C-c / t"]
  20070. ["Global TODO list" org-todo-list :active t :keys "C-c a t"]
  20071. "--"
  20072. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  20073. :selected org-enforce-todo-dependencies :style toggle :active t]
  20074. "Settings for tree at point"
  20075. ["Do Children sequentially" org-toggle-ordered-property :style radio
  20076. :selected (org-entry-get nil "ORDERED")
  20077. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  20078. ["Do Children parallel" org-toggle-ordered-property :style radio
  20079. :selected (not (org-entry-get nil "ORDERED"))
  20080. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  20081. "--"
  20082. ["Set Priority" org-priority t]
  20083. ["Priority Up" org-shiftup t]
  20084. ["Priority Down" org-shiftdown t]
  20085. "--"
  20086. ["Get news from all feeds" org-feed-update-all t]
  20087. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  20088. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  20089. ("TAGS and Properties"
  20090. ["Set Tags" org-set-tags-command (not (org-before-first-heading-p))]
  20091. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  20092. "--"
  20093. ["Set property" org-set-property (not (org-before-first-heading-p))]
  20094. ["Column view of properties" org-columns t]
  20095. ["Insert Column View DBlock" org-columns-insert-dblock t])
  20096. ("Dates and Scheduling"
  20097. ["Timestamp" org-time-stamp (not (org-before-first-heading-p))]
  20098. ["Timestamp (inactive)" org-time-stamp-inactive (not (org-before-first-heading-p))]
  20099. ("Change Date"
  20100. ["1 Day Later" org-shiftright (org-at-timestamp-p)]
  20101. ["1 Day Earlier" org-shiftleft (org-at-timestamp-p)]
  20102. ["1 ... Later" org-shiftup (org-at-timestamp-p)]
  20103. ["1 ... Earlier" org-shiftdown (org-at-timestamp-p)])
  20104. ["Compute Time Range" org-evaluate-time-range t]
  20105. ["Schedule Item" org-schedule (not (org-before-first-heading-p))]
  20106. ["Deadline" org-deadline (not (org-before-first-heading-p))]
  20107. "--"
  20108. ["Custom time format" org-toggle-time-stamp-overlays
  20109. :style radio :selected org-display-custom-times]
  20110. "--"
  20111. ["Goto Calendar" org-goto-calendar t]
  20112. ["Date from Calendar" org-date-from-calendar t]
  20113. "--"
  20114. ["Start/Restart Timer" org-timer-start t]
  20115. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  20116. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  20117. ["Insert Timer String" org-timer t]
  20118. ["Insert Timer Item" org-timer-item t])
  20119. ("Logging work"
  20120. ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
  20121. ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
  20122. ["Clock out" org-clock-out t]
  20123. ["Clock cancel" org-clock-cancel t]
  20124. "--"
  20125. ["Mark as default task" org-clock-mark-default-task t]
  20126. ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
  20127. ["Goto running clock" org-clock-goto t]
  20128. "--"
  20129. ["Display times" org-clock-display t]
  20130. ["Create clock table" org-clock-report t]
  20131. "--"
  20132. ["Record DONE time"
  20133. (progn (setq org-log-done (not org-log-done))
  20134. (message "Switching to %s will %s record a timestamp"
  20135. (car org-done-keywords)
  20136. (if org-log-done "automatically" "not")))
  20137. :style toggle :selected org-log-done])
  20138. "--"
  20139. ["Agenda Command..." org-agenda t]
  20140. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  20141. ("File List for Agenda")
  20142. ("Special views current file"
  20143. ["TODO Tree" org-show-todo-tree t]
  20144. ["Check Deadlines" org-check-deadlines t]
  20145. ["Timeline" org-timeline t]
  20146. ["Tags/Property tree" org-match-sparse-tree t])
  20147. "--"
  20148. ["Export/Publish..." org-export-dispatch t]
  20149. ("LaTeX"
  20150. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  20151. :selected org-cdlatex-mode]
  20152. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  20153. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  20154. ["Modify math symbol" org-cdlatex-math-modify
  20155. (org-inside-LaTeX-fragment-p)]
  20156. ["Insert citation" org-reftex-citation t]
  20157. "--"
  20158. ["Template for BEAMER" (org-beamer-insert-options-template) t])
  20159. "--"
  20160. ("MobileOrg"
  20161. ["Push Files and Views" org-mobile-push t]
  20162. ["Get Captured and Flagged" org-mobile-pull t]
  20163. ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
  20164. "--"
  20165. ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
  20166. "--"
  20167. ("Documentation"
  20168. ["Show Version" org-version t]
  20169. ["Info Documentation" org-info t])
  20170. ("Customize"
  20171. ["Browse Org Group" org-customize t]
  20172. "--"
  20173. ["Expand This Menu" org-create-customize-menu
  20174. (fboundp 'customize-menu-create)])
  20175. ["Send bug report" org-submit-bug-report t]
  20176. "--"
  20177. ("Refresh/Reload"
  20178. ["Refresh setup current buffer" org-mode-restart t]
  20179. ["Reload Org (after update)" org-reload t]
  20180. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x !"])
  20181. ))
  20182. (defun org-info (&optional node)
  20183. "Read documentation for Org-mode in the info system.
  20184. With optional NODE, go directly to that node."
  20185. (interactive)
  20186. (info (format "(org)%s" (or node ""))))
  20187. ;;;###autoload
  20188. (defun org-submit-bug-report ()
  20189. "Submit a bug report on Org-mode via mail.
  20190. Don't hesitate to report any problems or inaccurate documentation.
  20191. If you don't have setup sending mail from (X)Emacs, please copy the
  20192. output buffer into your mail program, as it gives us important
  20193. information about your Org-mode version and configuration."
  20194. (interactive)
  20195. (require 'reporter)
  20196. (defvar reporter-prompt-for-summary-p)
  20197. (org-load-modules-maybe)
  20198. (org-require-autoloaded-modules)
  20199. (let ((reporter-prompt-for-summary-p "Bug report subject: "))
  20200. (reporter-submit-bug-report
  20201. "emacs-orgmode@gnu.org"
  20202. (org-version nil 'full)
  20203. (let (list)
  20204. (save-window-excursion
  20205. (org-pop-to-buffer-same-window (get-buffer-create "*Warn about privacy*"))
  20206. (delete-other-windows)
  20207. (erase-buffer)
  20208. (insert "You are about to submit a bug report to the Org-mode mailing list.
  20209. We would like to add your full Org-mode and Outline configuration to the
  20210. bug report. This greatly simplifies the work of the maintainer and
  20211. other experts on the mailing list.
  20212. HOWEVER, some variables you have customized may contain private
  20213. information. The names of customers, colleagues, or friends, might
  20214. appear in the form of file names, tags, todo states, or search strings.
  20215. If you answer yes to the prompt, you might want to check and remove
  20216. such private information before sending the email.")
  20217. (add-text-properties (point-min) (point-max) '(face org-warning))
  20218. (when (yes-or-no-p "Include your Org-mode configuration ")
  20219. (mapatoms
  20220. (lambda (v)
  20221. (and (boundp v)
  20222. (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
  20223. (or (and (symbol-value v)
  20224. (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
  20225. (and
  20226. (get v 'custom-type) (get v 'standard-value)
  20227. (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
  20228. (push v list)))))
  20229. (kill-buffer (get-buffer "*Warn about privacy*"))
  20230. list))
  20231. nil nil
  20232. "Remember to cover the basics, that is, what you expected to happen and
  20233. what in fact did happen. You don't know how to make a good report? See
  20234. http://orgmode.org/manual/Feedback.html#Feedback
  20235. Your bug report will be posted to the Org-mode mailing list.
  20236. ------------------------------------------------------------------------")
  20237. (save-excursion
  20238. (when (re-search-backward "^\\(Subject: \\)Org-mode version \\(.*?\\);[ \t]*\\(.*\\)" nil t)
  20239. (replace-match "\\1Bug: \\3 [\\2]")))))
  20240. (defun org-install-agenda-files-menu ()
  20241. (let ((bl (buffer-list)))
  20242. (save-excursion
  20243. (while bl
  20244. (set-buffer (pop bl))
  20245. (when (derived-mode-p 'org-mode) (setq bl nil)))
  20246. (when (derived-mode-p 'org-mode)
  20247. (easy-menu-change
  20248. '("Org") "File List for Agenda"
  20249. (append
  20250. (list
  20251. ["Edit File List" (org-edit-agenda-file-list) t]
  20252. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  20253. ["Remove Current File from List" org-remove-file t]
  20254. ["Cycle through agenda files" org-cycle-agenda-files t]
  20255. ["Occur in all agenda files" org-occur-in-agenda-files t]
  20256. "--")
  20257. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  20258. ;;;; Documentation
  20259. (defun org-require-autoloaded-modules ()
  20260. (interactive)
  20261. (mapc #'require
  20262. '(org-agenda org-archive org-attach org-clock org-colview org-id
  20263. org-table org-timer)))
  20264. ;;;###autoload
  20265. (defun org-reload (&optional uncompiled)
  20266. "Reload all org lisp files.
  20267. With prefix arg UNCOMPILED, load the uncompiled versions."
  20268. (interactive "P")
  20269. (require 'loadhist)
  20270. (let* ((org-dir (org-find-library-dir "org"))
  20271. (contrib-dir (or (org-find-library-dir "org-contribdir") org-dir))
  20272. (feature-re "^\\(org\\|ob\\|ox\\)\\(-.*\\)?")
  20273. (remove-re (mapconcat 'identity
  20274. (mapcar (lambda (f) (concat "^" f "$"))
  20275. (list (if (featurep 'xemacs)
  20276. "org-colview"
  20277. "org-colview-xemacs")
  20278. "org" "org-loaddefs" "org-version"))
  20279. "\\|"))
  20280. (feats (delete-dups
  20281. (mapcar 'file-name-sans-extension
  20282. (mapcar 'file-name-nondirectory
  20283. (delq nil
  20284. (mapcar 'feature-file
  20285. features))))))
  20286. (lfeat (append
  20287. (sort
  20288. (setq feats
  20289. (delq nil (mapcar
  20290. (lambda (f)
  20291. (if (and (string-match feature-re f)
  20292. (not (string-match remove-re f)))
  20293. f nil))
  20294. feats)))
  20295. 'string-lessp)
  20296. (list "org-version" "org")))
  20297. (load-suffixes (when (boundp 'load-suffixes) load-suffixes))
  20298. (load-suffixes (if uncompiled (reverse load-suffixes) load-suffixes))
  20299. load-uncore load-misses)
  20300. (setq load-misses
  20301. (delq 't
  20302. (mapcar (lambda (f)
  20303. (or (org-load-noerror-mustsuffix (concat org-dir f))
  20304. (and (string= org-dir contrib-dir)
  20305. (org-load-noerror-mustsuffix (concat contrib-dir f)))
  20306. (and (org-load-noerror-mustsuffix (concat (org-find-library-dir f) f))
  20307. (add-to-list 'load-uncore f 'append)
  20308. 't)
  20309. f))
  20310. lfeat)))
  20311. (when load-uncore
  20312. (message "The following feature%s found in load-path, please check if that's correct:\n%s"
  20313. (if (> (length load-uncore) 1) "s were" " was") load-uncore))
  20314. (if load-misses
  20315. (message "Some error occurred while reloading Org feature%s\n%s\nPlease check *Messages*!\n%s"
  20316. (if (> (length load-misses) 1) "s" "") load-misses (org-version nil 'full))
  20317. (message "Successfully reloaded Org\n%s" (org-version nil 'full)))))
  20318. ;;;###autoload
  20319. (defun org-customize ()
  20320. "Call the customize function with org as argument."
  20321. (interactive)
  20322. (org-load-modules-maybe)
  20323. (org-require-autoloaded-modules)
  20324. (customize-browse 'org))
  20325. (defun org-create-customize-menu ()
  20326. "Create a full customization menu for Org-mode, insert it into the menu."
  20327. (interactive)
  20328. (org-load-modules-maybe)
  20329. (org-require-autoloaded-modules)
  20330. (if (fboundp 'customize-menu-create)
  20331. (progn
  20332. (easy-menu-change
  20333. '("Org") "Customize"
  20334. `(["Browse Org group" org-customize t]
  20335. "--"
  20336. ,(customize-menu-create 'org)
  20337. ["Set" Custom-set t]
  20338. ["Save" Custom-save t]
  20339. ["Reset to Current" Custom-reset-current t]
  20340. ["Reset to Saved" Custom-reset-saved t]
  20341. ["Reset to Standard Settings" Custom-reset-standard t]))
  20342. (message "\"Org\"-menu now contains full customization menu"))
  20343. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  20344. ;;;; Miscellaneous stuff
  20345. ;;; Generally useful functions
  20346. (defun org-get-at-eol (property n)
  20347. "Get text property PROPERTY at the end of line less N characters."
  20348. (get-text-property (- (point-at-eol) n) property))
  20349. (defun org-find-text-property-in-string (prop s)
  20350. "Return the first non-nil value of property PROP in string S."
  20351. (or (get-text-property 0 prop s)
  20352. (get-text-property (or (next-single-property-change 0 prop s) 0)
  20353. prop s)))
  20354. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  20355. "Display the given MESSAGE as a warning."
  20356. (if (fboundp 'display-warning)
  20357. (display-warning 'org message
  20358. (if (featurep 'xemacs) 'warning :warning))
  20359. (let ((buf (get-buffer-create "*Org warnings*")))
  20360. (with-current-buffer buf
  20361. (goto-char (point-max))
  20362. (insert "Warning (Org): " message)
  20363. (unless (bolp)
  20364. (newline)))
  20365. (display-buffer buf)
  20366. (sit-for 0))))
  20367. (defun org-eval (form)
  20368. "Eval FORM and return result."
  20369. (condition-case error
  20370. (eval form)
  20371. (error (format "%%![Error: %s]" error))))
  20372. (defun org-in-clocktable-p ()
  20373. "Check if the cursor is in a clocktable."
  20374. (let ((pos (point)) start)
  20375. (save-excursion
  20376. (end-of-line 1)
  20377. (and (re-search-backward "^[ \t]*#\\+BEGIN:[ \t]+clocktable" nil t)
  20378. (setq start (match-beginning 0))
  20379. (re-search-forward "^[ \t]*#\\+END:.*" nil t)
  20380. (>= (match-end 0) pos)
  20381. start))))
  20382. (defun org-in-verbatim-emphasis ()
  20383. (save-match-data
  20384. (and (org-in-regexp org-emph-re 2)
  20385. (>= (point) (match-beginning 3))
  20386. (<= (point) (match-end 4))
  20387. (member (match-string 3) '("=" "~")))))
  20388. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  20389. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  20390. (if (and marker (marker-buffer marker)
  20391. (buffer-live-p (marker-buffer marker)))
  20392. (progn
  20393. (org-pop-to-buffer-same-window (marker-buffer marker))
  20394. (when (or (> marker (point-max)) (< marker (point-min)))
  20395. (widen))
  20396. (goto-char marker)
  20397. (org-show-context 'org-goto))
  20398. (if bookmark
  20399. (bookmark-jump bookmark)
  20400. (error "Cannot find location"))))
  20401. (defun org-quote-csv-field (s)
  20402. "Quote field for inclusion in CSV material."
  20403. (if (string-match "[\",]" s)
  20404. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  20405. s))
  20406. (defun org-force-self-insert (N)
  20407. "Needed to enforce self-insert under remapping."
  20408. (interactive "p")
  20409. (self-insert-command N))
  20410. (defun org-string-width (s)
  20411. "Compute width of string, ignoring invisible characters.
  20412. This ignores character with invisibility property `org-link', and also
  20413. characters with property `org-cwidth', because these will become invisible
  20414. upon the next fontification round."
  20415. (let (b l)
  20416. (when (or (eq t buffer-invisibility-spec)
  20417. (assq 'org-link buffer-invisibility-spec))
  20418. (while (setq b (text-property-any 0 (length s)
  20419. 'invisible 'org-link s))
  20420. (setq s (concat (substring s 0 b)
  20421. (substring s (or (next-single-property-change
  20422. b 'invisible s)
  20423. (length s)))))))
  20424. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  20425. (setq s (concat (substring s 0 b)
  20426. (substring s (or (next-single-property-change
  20427. b 'org-cwidth s)
  20428. (length s))))))
  20429. (setq l (string-width s) b -1)
  20430. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  20431. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  20432. l))
  20433. (defun org-shorten-string (s maxlength)
  20434. "Shorten string S so tht it is no longer than MAXLENGTH characters.
  20435. If the string is shorter or has length MAXLENGTH, just return the
  20436. original string. If it is longer, the functions finds a space in the
  20437. string, breaks this string off at that locations and adds three dots
  20438. as ellipsis. Including the ellipsis, the string will not be longer
  20439. than MAXLENGTH. If finding a good breaking point in the string does
  20440. not work, the string is just chopped off in the middle of a word
  20441. if necessary."
  20442. (if (<= (length s) maxlength)
  20443. s
  20444. (let* ((n (max (- maxlength 4) 1))
  20445. (re (concat "\\`\\(.\\{1," (int-to-string n) "\\}[^ ]\\)\\([ ]\\|\\'\\)")))
  20446. (if (string-match re s)
  20447. (concat (match-string 1 s) "...")
  20448. (concat (substring s 0 (max (- maxlength 3) 0)) "...")))))
  20449. (defun org-get-indentation (&optional line)
  20450. "Get the indentation of the current line, interpreting tabs.
  20451. When LINE is given, assume it represents a line and compute its indentation."
  20452. (if line
  20453. (when (string-match "^ *" (org-remove-tabs line))
  20454. (match-end 0))
  20455. (save-excursion
  20456. (beginning-of-line 1)
  20457. (skip-chars-forward " \t")
  20458. (current-column))))
  20459. (defun org-get-string-indentation (s)
  20460. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  20461. (let ((n -1) (i 0) (w tab-width) c)
  20462. (catch 'exit
  20463. (while (< (setq n (1+ n)) (length s))
  20464. (setq c (aref s n))
  20465. (cond ((= c ?\ ) (setq i (1+ i)))
  20466. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  20467. (t (throw 'exit t)))))
  20468. i))
  20469. (defun org-remove-tabs (s &optional width)
  20470. "Replace tabulators in S with spaces.
  20471. Assumes that s is a single line, starting in column 0."
  20472. (setq width (or width tab-width))
  20473. (while (string-match "\t" s)
  20474. (setq s (replace-match
  20475. (make-string
  20476. (- (* width (/ (+ (match-beginning 0) width) width))
  20477. (match-beginning 0)) ?\ )
  20478. t t s)))
  20479. s)
  20480. (defun org-fix-indentation (line ind)
  20481. "Fix indentation in LINE.
  20482. IND is a cons cell with target and minimum indentation.
  20483. If the current indentation in LINE is smaller than the minimum,
  20484. leave it alone. If it is larger than ind, set it to the target."
  20485. (let* ((l (org-remove-tabs line))
  20486. (i (org-get-indentation l))
  20487. (i1 (car ind)) (i2 (cdr ind)))
  20488. (when (>= i i2) (setq l (substring line i2)))
  20489. (if (> i1 0)
  20490. (concat (make-string i1 ?\ ) l)
  20491. l)))
  20492. (defun org-remove-indentation (code &optional n)
  20493. "Remove maximum common indentation in string CODE and return it.
  20494. N may optionally be the number of columns to remove. Return CODE
  20495. as-is if removal failed."
  20496. (with-temp-buffer
  20497. (insert code)
  20498. (if (org-do-remove-indentation n) (buffer-string) code)))
  20499. (defun org-do-remove-indentation (&optional n)
  20500. "Remove the maximum common indentation from the buffer.
  20501. When optional argument N is a positive integer, remove exactly
  20502. that much characters from indentation, if possible. Return nil
  20503. if it fails."
  20504. (catch :exit
  20505. (goto-char (point-min))
  20506. ;; Find maximum common indentation, if not specified.
  20507. (let ((n (or n
  20508. (let ((min-ind (point-max)))
  20509. (save-excursion
  20510. (while (re-search-forward "^[ \t]*\\S-" nil t)
  20511. (let ((ind (1- (current-column))))
  20512. (if (zerop ind) (throw :exit nil)
  20513. (setq min-ind (min min-ind ind))))))
  20514. min-ind))))
  20515. (if (zerop n) (throw :exit nil)
  20516. ;; Remove exactly N indentation, but give up if not possible.
  20517. (while (not (eobp))
  20518. (let ((ind (progn (skip-chars-forward " \t") (current-column))))
  20519. (cond ((eolp) (delete-region (line-beginning-position) (point)))
  20520. ((< ind n) (throw :exit nil))
  20521. (t (org-indent-line-to (- ind n))))
  20522. (forward-line)))
  20523. ;; Signal success.
  20524. t))))
  20525. (defun org-fill-template (template alist)
  20526. "Find each %key of ALIST in TEMPLATE and replace it."
  20527. (let ((case-fold-search nil))
  20528. (dolist (entry (sort (copy-sequence alist)
  20529. (lambda (a b) (< (length (car a)) (length (car b))))))
  20530. (setq template
  20531. (replace-regexp-in-string
  20532. (concat "%" (regexp-quote (car entry)))
  20533. (or (cdr entry) "") template t t)))
  20534. template))
  20535. (defun org-base-buffer (buffer)
  20536. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  20537. (if (not buffer)
  20538. buffer
  20539. (or (buffer-base-buffer buffer)
  20540. buffer)))
  20541. (defun org-wrap (string &optional width lines)
  20542. "Wrap string to either a number of lines, or a width in characters.
  20543. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  20544. that costs. If there is a word longer than WIDTH, the text is actually
  20545. wrapped to the length of that word.
  20546. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  20547. many lines, whatever width that takes.
  20548. The return value is a list of lines, without newlines at the end."
  20549. (let* ((words (org-split-string string "[ \t\n]+"))
  20550. (maxword (apply 'max (mapcar 'org-string-width words)))
  20551. w ll)
  20552. (cond (width
  20553. (org-do-wrap words (max maxword width)))
  20554. (lines
  20555. (setq w maxword)
  20556. (setq ll (org-do-wrap words maxword))
  20557. (if (<= (length ll) lines)
  20558. ll
  20559. (setq ll words)
  20560. (while (> (length ll) lines)
  20561. (setq w (1+ w))
  20562. (setq ll (org-do-wrap words w)))
  20563. ll))
  20564. (t (error "Cannot wrap this")))))
  20565. (defun org-do-wrap (words width)
  20566. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  20567. (let (lines line)
  20568. (while words
  20569. (setq line (pop words))
  20570. (while (and words (< (+ (length line) (length (car words))) width))
  20571. (setq line (concat line " " (pop words))))
  20572. (setq lines (push line lines)))
  20573. (nreverse lines)))
  20574. (defun org-split-string (string &optional separators)
  20575. "Splits STRING into substrings at SEPARATORS.
  20576. SEPARATORS is a regular expression.
  20577. No empty strings are returned if there are matches at the beginning
  20578. and end of string."
  20579. ;; FIXME: why not use (split-string STRING SEPARATORS t)?
  20580. (let ((start 0) notfirst list)
  20581. (while (and (string-match (or separators "[ \f\t\n\r\v]+") string
  20582. (if (and notfirst
  20583. (= start (match-beginning 0))
  20584. (< start (length string)))
  20585. (1+ start) start))
  20586. (< (match-beginning 0) (length string)))
  20587. (setq notfirst t)
  20588. (or (eq (match-beginning 0) 0)
  20589. (and (eq (match-beginning 0) (match-end 0))
  20590. (eq (match-beginning 0) start))
  20591. (push (substring string start (match-beginning 0)) list))
  20592. (setq start (match-end 0)))
  20593. (or (eq start (length string))
  20594. (push (substring string start) list))
  20595. (nreverse list)))
  20596. (defun org-quote-vert (s)
  20597. "Replace \"|\" with \"\\vert\"."
  20598. (while (string-match "|" s)
  20599. (setq s (replace-match "\\vert" t t s)))
  20600. s)
  20601. (defun org-uuidgen-p (s)
  20602. "Is S an ID created by UUIDGEN?"
  20603. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  20604. (defun org-in-src-block-p (&optional inside)
  20605. "Whether point is in a code source block.
  20606. When INSIDE is non-nil, don't consider we are within a src block
  20607. when point is at #+BEGIN_SRC or #+END_SRC."
  20608. (let ((case-fold-search t))
  20609. (or (and (eq (get-char-property (point) 'src-block) t))
  20610. (and (not inside)
  20611. (save-match-data
  20612. (save-excursion
  20613. (beginning-of-line)
  20614. (looking-at ".*#\\+\\(begin\\|end\\)_src")))))))
  20615. (defun org-context ()
  20616. "Return a list of contexts of the current cursor position.
  20617. If several contexts apply, all are returned.
  20618. Each context entry is a list with a symbol naming the context, and
  20619. two positions indicating start and end of the context. Possible
  20620. contexts are:
  20621. :headline anywhere in a headline
  20622. :headline-stars on the leading stars in a headline
  20623. :todo-keyword on a TODO keyword (including DONE) in a headline
  20624. :tags on the TAGS in a headline
  20625. :priority on the priority cookie in a headline
  20626. :item on the first line of a plain list item
  20627. :item-bullet on the bullet/number of a plain list item
  20628. :checkbox on the checkbox in a plain list item
  20629. :table in an org-mode table
  20630. :table-special on a special filed in a table
  20631. :table-table in a table.el table
  20632. :clocktable in a clocktable
  20633. :src-block in a source block
  20634. :link on a hyperlink
  20635. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE, COMMENT.
  20636. :target on a <<target>>
  20637. :radio-target on a <<<radio-target>>>
  20638. :latex-fragment on a LaTeX fragment
  20639. :latex-preview on a LaTeX fragment with overlaid preview image
  20640. This function expects the position to be visible because it uses font-lock
  20641. faces as a help to recognize the following contexts: :table-special, :link,
  20642. and :keyword."
  20643. (let* ((f (get-text-property (point) 'face))
  20644. (faces (if (listp f) f (list f)))
  20645. (case-fold-search t)
  20646. (p (point)) clist o)
  20647. ;; First the large context
  20648. (cond
  20649. ((org-at-heading-p t)
  20650. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  20651. (when (progn
  20652. (beginning-of-line 1)
  20653. (looking-at org-todo-line-tags-regexp))
  20654. (push (org-point-in-group p 1 :headline-stars) clist)
  20655. (push (org-point-in-group p 2 :todo-keyword) clist)
  20656. (push (org-point-in-group p 4 :tags) clist))
  20657. (goto-char p)
  20658. (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
  20659. (when (looking-at "\\[#[A-Z0-9]\\]")
  20660. (push (org-point-in-group p 0 :priority) clist)))
  20661. ((org-at-item-p)
  20662. (push (org-point-in-group p 2 :item-bullet) clist)
  20663. (push (list :item (point-at-bol)
  20664. (save-excursion (org-end-of-item) (point)))
  20665. clist)
  20666. (and (org-at-item-checkbox-p)
  20667. (push (org-point-in-group p 0 :checkbox) clist)))
  20668. ((org-at-table-p)
  20669. (push (list :table (org-table-begin) (org-table-end)) clist)
  20670. (when (memq 'org-formula faces)
  20671. (push (list :table-special
  20672. (previous-single-property-change p 'face)
  20673. (next-single-property-change p 'face)) clist)))
  20674. ((org-at-table-p 'any)
  20675. (push (list :table-table) clist)))
  20676. (goto-char p)
  20677. (let ((case-fold-search t))
  20678. ;; New the "medium" contexts: clocktables, source blocks
  20679. (cond ((org-in-clocktable-p)
  20680. (push (list :clocktable
  20681. (and (or (looking-at "[ \t]*\\(#\\+BEGIN: clocktable\\)")
  20682. (re-search-backward "[ \t]*\\(#+BEGIN: clocktable\\)" nil t))
  20683. (match-beginning 1))
  20684. (and (re-search-forward "[ \t]*#\\+END:?" nil t)
  20685. (match-end 0))) clist))
  20686. ((org-in-src-block-p)
  20687. (push (list :src-block
  20688. (and (or (looking-at "[ \t]*\\(#\\+BEGIN_SRC\\)")
  20689. (re-search-backward "[ \t]*\\(#+BEGIN_SRC\\)" nil t))
  20690. (match-beginning 1))
  20691. (and (search-forward "#+END_SRC" nil t)
  20692. (match-beginning 0))) clist))))
  20693. (goto-char p)
  20694. ;; Now the small context
  20695. (cond
  20696. ((org-at-timestamp-p)
  20697. (push (org-point-in-group p 0 :timestamp) clist))
  20698. ((memq 'org-link faces)
  20699. (push (list :link
  20700. (previous-single-property-change p 'face)
  20701. (next-single-property-change p 'face)) clist))
  20702. ((memq 'org-special-keyword faces)
  20703. (push (list :keyword
  20704. (previous-single-property-change p 'face)
  20705. (next-single-property-change p 'face)) clist))
  20706. ((org-at-target-p)
  20707. (push (org-point-in-group p 0 :target) clist)
  20708. (goto-char (1- (match-beginning 0)))
  20709. (when (looking-at org-radio-target-regexp)
  20710. (push (org-point-in-group p 0 :radio-target) clist))
  20711. (goto-char p))
  20712. ((setq o (cl-some
  20713. (lambda (o)
  20714. (and (eq (overlay-get o 'org-overlay-type) 'org-latex-overlay)
  20715. o))
  20716. (overlays-at (point))))
  20717. (push (list :latex-fragment
  20718. (overlay-start o) (overlay-end o)) clist)
  20719. (push (list :latex-preview
  20720. (overlay-start o) (overlay-end o)) clist))
  20721. ((org-inside-LaTeX-fragment-p)
  20722. ;; FIXME: positions wrong.
  20723. (push (list :latex-fragment (point) (point)) clist)))
  20724. (setq clist (nreverse (delq nil clist)))
  20725. clist))
  20726. (defun org-in-regexp (regexp &optional nlines visually)
  20727. "Check if point is inside a match of REGEXP.
  20728. Normally only the current line is checked, but you can include
  20729. NLINES extra lines around point into the search. If VISUALLY is
  20730. set, require that the cursor is not after the match but really
  20731. on, so that the block visually is on the match.
  20732. Return nil or a cons cell (BEG . END) where BEG and END are,
  20733. respectively, the positions at the beginning and the end of the
  20734. match."
  20735. (catch :exit
  20736. (let ((pos (point))
  20737. (eol (line-end-position (if nlines (1+ nlines) 1))))
  20738. (save-excursion
  20739. (beginning-of-line (- 1 (or nlines 0)))
  20740. (while (and (re-search-forward regexp eol t)
  20741. (<= (match-beginning 0) pos))
  20742. (let ((end (match-end 0)))
  20743. (when (or (> end pos) (and (= end pos) (not visually)))
  20744. (throw :exit (cons (match-beginning 0) (match-end 0))))))))))
  20745. (define-obsolete-function-alias 'org-at-regexp-p 'org-in-regexp
  20746. "Org mode 8.3")
  20747. (defun org-between-regexps-p (start-re end-re &optional lim-up lim-down)
  20748. "Non-nil when point is between matches of START-RE and END-RE.
  20749. Also return a non-nil value when point is on one of the matches.
  20750. Optional arguments LIM-UP and LIM-DOWN bound the search; they are
  20751. buffer positions. Default values are the positions of headlines
  20752. surrounding the point.
  20753. The functions returns a cons cell whose car (resp. cdr) is the
  20754. position before START-RE (resp. after END-RE)."
  20755. (save-match-data
  20756. (let ((pos (point))
  20757. (limit-up (or lim-up (save-excursion (outline-previous-heading))))
  20758. (limit-down (or lim-down (save-excursion (outline-next-heading))))
  20759. beg end)
  20760. (save-excursion
  20761. ;; Point is on a block when on START-RE or if START-RE can be
  20762. ;; found before it...
  20763. (and (or (org-in-regexp start-re)
  20764. (re-search-backward start-re limit-up t))
  20765. (setq beg (match-beginning 0))
  20766. ;; ... and END-RE after it...
  20767. (goto-char (match-end 0))
  20768. (re-search-forward end-re limit-down t)
  20769. (> (setq end (match-end 0)) pos)
  20770. ;; ... without another START-RE in-between.
  20771. (goto-char (match-beginning 0))
  20772. (not (re-search-backward start-re (1+ beg) t))
  20773. ;; Return value.
  20774. (cons beg end))))))
  20775. (defun org-in-block-p (names)
  20776. "Non-nil when point belongs to a block whose name belongs to NAMES.
  20777. NAMES is a list of strings containing names of blocks.
  20778. Return first block name matched, or nil. Beware that in case of
  20779. nested blocks, the returned name may not belong to the closest
  20780. block from point."
  20781. (save-match-data
  20782. (catch 'exit
  20783. (let ((case-fold-search t)
  20784. (lim-up (save-excursion (outline-previous-heading)))
  20785. (lim-down (save-excursion (outline-next-heading))))
  20786. (dolist (name names)
  20787. (let ((n (regexp-quote name)))
  20788. (when (org-between-regexps-p
  20789. (concat "^[ \t]*#\\+begin_" n)
  20790. (concat "^[ \t]*#\\+end_" n)
  20791. lim-up lim-down)
  20792. (throw 'exit n)))))
  20793. nil)))
  20794. (defun org-occur-in-agenda-files (regexp &optional _nlines)
  20795. "Call `multi-occur' with buffers for all agenda files."
  20796. (interactive "sOrg-files matching: ")
  20797. (let* ((files (org-agenda-files))
  20798. (tnames (mapcar #'file-truename files))
  20799. (extra org-agenda-text-search-extra-files))
  20800. (when (eq (car extra) 'agenda-archives)
  20801. (setq extra (cdr extra))
  20802. (setq files (org-add-archive-files files)))
  20803. (dolist (f extra)
  20804. (unless (member (file-truename f) tnames)
  20805. (unless (member f files) (setq files (append files (list f))))
  20806. (setq tnames (append tnames (list (file-truename f))))))
  20807. (multi-occur
  20808. (mapcar (lambda (x)
  20809. (with-current-buffer
  20810. ;; FIXME: Why not just (find-file-noselect x)?
  20811. ;; Is it to avoid the "revert buffer" prompt?
  20812. (or (get-file-buffer x) (find-file-noselect x))
  20813. (widen)
  20814. (current-buffer)))
  20815. files)
  20816. regexp)))
  20817. (if (boundp 'occur-mode-find-occurrence-hook)
  20818. ;; Emacs 23
  20819. (add-hook 'occur-mode-find-occurrence-hook
  20820. (lambda ()
  20821. (when (derived-mode-p 'org-mode)
  20822. (org-reveal))))
  20823. ;; Emacs 22
  20824. (defadvice occur-mode-goto-occurrence
  20825. (after org-occur-reveal activate)
  20826. (and (derived-mode-p 'org-mode) (org-reveal)))
  20827. (defadvice occur-mode-goto-occurrence-other-window
  20828. (after org-occur-reveal activate)
  20829. (and (derived-mode-p 'org-mode) (org-reveal)))
  20830. (defadvice occur-mode-display-occurrence
  20831. (after org-occur-reveal activate)
  20832. (when (derived-mode-p 'org-mode)
  20833. (let ((pos (occur-mode-find-occurrence)))
  20834. (with-current-buffer (marker-buffer pos)
  20835. (save-excursion
  20836. (goto-char pos)
  20837. (org-reveal)))))))
  20838. (defun org-occur-link-in-agenda-files ()
  20839. "Create a link and search for it in the agendas.
  20840. The link is not stored in `org-stored-links', it is just created
  20841. for the search purpose."
  20842. (interactive)
  20843. (let ((link (condition-case nil
  20844. (org-store-link nil)
  20845. (error "Unable to create a link to here"))))
  20846. (org-occur-in-agenda-files (regexp-quote link))))
  20847. (defun org-reverse-string (string)
  20848. "Return the reverse of STRING."
  20849. (apply 'string (reverse (string-to-list string))))
  20850. ;; defsubst org-uniquify must be defined before first use
  20851. (defun org-uniquify-alist (alist)
  20852. "Merge elements of ALIST with the same key.
  20853. For example, in this alist:
  20854. \(org-uniquify-alist \\='((a 1) (b 2) (a 3)))
  20855. => \\='((a 1 3) (b 2))
  20856. merge (a 1) and (a 3) into (a 1 3).
  20857. The function returns the new ALIST."
  20858. (let (rtn)
  20859. (dolist (e alist rtn)
  20860. (let (n)
  20861. (if (not (assoc (car e) rtn))
  20862. (push e rtn)
  20863. (setq n (cons (car e) (append (cdr (assoc (car e) rtn)) (cdr e))))
  20864. (setq rtn (assq-delete-all (car e) rtn))
  20865. (push n rtn))))))
  20866. (defun org-delete-all (elts list)
  20867. "Remove all elements in ELTS from LIST."
  20868. (while elts
  20869. (setq list (delete (pop elts) list)))
  20870. list)
  20871. (defun org-back-over-empty-lines ()
  20872. "Move backwards over whitespace, to the beginning of the first empty line.
  20873. Returns the number of empty lines passed."
  20874. (let ((pos (point)))
  20875. (if (cdr (assoc 'heading org-blank-before-new-entry))
  20876. (skip-chars-backward " \t\n\r")
  20877. (unless (eobp)
  20878. (forward-line -1)))
  20879. (beginning-of-line 2)
  20880. (goto-char (min (point) pos))
  20881. (count-lines (point) pos)))
  20882. (defun org-skip-whitespace ()
  20883. (skip-chars-forward " \t\n\r"))
  20884. (defun org-point-in-group (point group &optional context)
  20885. "Check if POINT is in match-group GROUP.
  20886. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  20887. match. If the match group does not exist or point is not inside it,
  20888. return nil."
  20889. (and (match-beginning group)
  20890. (>= point (match-beginning group))
  20891. (<= point (match-end group))
  20892. (if context
  20893. (list context (match-beginning group) (match-end group))
  20894. t)))
  20895. (defun org-switch-to-buffer-other-window (&rest args)
  20896. "Switch to buffer in a second window on the current frame.
  20897. In particular, do not allow pop-up frames.
  20898. Returns the newly created buffer."
  20899. (org-no-popups
  20900. (apply 'switch-to-buffer-other-window args)))
  20901. (defun org-combine-plists (&rest plists)
  20902. "Create a single property list from all plists in PLISTS.
  20903. The process starts by copying the first list, and then setting properties
  20904. from the other lists. Settings in the last list are the most significant
  20905. ones and overrule settings in the other lists."
  20906. (let ((rtn (copy-sequence (pop plists)))
  20907. p v ls)
  20908. (while plists
  20909. (setq ls (pop plists))
  20910. (while ls
  20911. (setq p (pop ls) v (pop ls))
  20912. (setq rtn (plist-put rtn p v))))
  20913. rtn))
  20914. (defun org-replace-escapes (string table)
  20915. "Replace %-escapes in STRING with values in TABLE.
  20916. TABLE is an association list with keys like \"%a\" and string values.
  20917. The sequences in STRING may contain normal field width and padding information,
  20918. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  20919. so values can contain further %-escapes if they are define later in TABLE."
  20920. (let ((tbl (copy-alist table))
  20921. (case-fold-search nil)
  20922. (pchg 0)
  20923. re rpl)
  20924. (dolist (e tbl)
  20925. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  20926. (when (and (cdr e) (string-match re (cdr e)))
  20927. (let ((sref (substring (cdr e) (match-beginning 0) (match-end 0)))
  20928. (safe "SREF"))
  20929. (add-text-properties 0 3 (list 'sref sref) safe)
  20930. (setcdr e (replace-match safe t t (cdr e)))))
  20931. (while (string-match re string)
  20932. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  20933. (cdr e)))
  20934. (setq string (replace-match rpl t t string))))
  20935. (while (setq pchg (next-property-change pchg string))
  20936. (let ((sref (get-text-property pchg 'sref string)))
  20937. (when (and sref (string-match "SREF" string pchg))
  20938. (setq string (replace-match sref t t string)))))
  20939. string))
  20940. (defun org-sublist (list start end)
  20941. "Return a section of LIST, from START to END.
  20942. Counting starts at 1."
  20943. (cl-subseq list (1- start) end))
  20944. (make-obsolete 'org-sublist "cl-subseq (note the 0-based counting)." "Org 9.0")
  20945. (defun org-find-base-buffer-visiting (file)
  20946. "Like `find-buffer-visiting' but always return the base buffer and
  20947. not an indirect buffer."
  20948. (let ((buf (or (get-file-buffer file)
  20949. (find-buffer-visiting file))))
  20950. (if buf
  20951. (or (buffer-base-buffer buf) buf)
  20952. nil)))
  20953. (define-obsolete-function-alias 'org-image-file-name-regexp 'image-file-name-regexp
  20954. "Org 9.0")
  20955. ;;; TODO: Only called once, from ox-odt which should probably use
  20956. ;;; org-export-inline-image-p or something.
  20957. (defun org-file-image-p (file)
  20958. "Return non-nil if FILE is an image."
  20959. (save-match-data
  20960. (string-match (image-file-name-regexp) file)))
  20961. (defun org-get-cursor-date (&optional with-time)
  20962. "Return the date at cursor in as a time.
  20963. This works in the calendar and in the agenda, anywhere else it just
  20964. returns the current time.
  20965. If WITH-TIME is non-nil, returns the time of the event at point (in
  20966. the agenda) or the current time of the day."
  20967. (let (date day defd tp hod mod)
  20968. (when with-time
  20969. (setq tp (get-text-property (point) 'time))
  20970. (when (and tp (string-match "\\([0-9][0-9]\\):\\([0-9][0-9]\\)" tp))
  20971. (setq hod (string-to-number (match-string 1 tp))
  20972. mod (string-to-number (match-string 2 tp))))
  20973. (or tp (let ((now (decode-time)))
  20974. (setq hod (nth 2 now)
  20975. mod (nth 1 now)))))
  20976. (cond
  20977. ((eq major-mode 'calendar-mode)
  20978. (setq date (calendar-cursor-to-date)
  20979. defd (encode-time 0 (or mod 0) (or hod 0)
  20980. (nth 1 date) (nth 0 date) (nth 2 date))))
  20981. ((eq major-mode 'org-agenda-mode)
  20982. (setq day (get-text-property (point) 'day))
  20983. (when day
  20984. (setq date (calendar-gregorian-from-absolute day)
  20985. defd (encode-time 0 (or mod 0) (or hod 0)
  20986. (nth 1 date) (nth 0 date) (nth 2 date))))))
  20987. (or defd (current-time))))
  20988. (defun org-mark-subtree (&optional up)
  20989. "Mark the current subtree.
  20990. This puts point at the start of the current subtree, and mark at
  20991. the end. If a numeric prefix UP is given, move up into the
  20992. hierarchy of headlines by UP levels before marking the subtree."
  20993. (interactive "P")
  20994. (org-with-limited-levels
  20995. (cond ((org-at-heading-p) (beginning-of-line))
  20996. ((org-before-first-heading-p) (user-error "Not in a subtree"))
  20997. (t (outline-previous-visible-heading 1))))
  20998. (when up (while (and (> up 0) (org-up-heading-safe)) (cl-decf up)))
  20999. (if (org-called-interactively-p 'any)
  21000. (call-interactively 'org-mark-element)
  21001. (org-mark-element)))
  21002. (defun org-file-newer-than-p (file time)
  21003. "Non-nil if FILE is newer than TIME.
  21004. FILE is a filename, as a string, TIME is a list of integers, as
  21005. returned by, e.g., `current-time'."
  21006. (and (file-exists-p file)
  21007. ;; Only compare times up to whole seconds as some file-systems
  21008. ;; (e.g. HFS+) do not retain any finer granularity. As
  21009. ;; a consequence, make sure we return non-nil when the two
  21010. ;; times are equal.
  21011. (not (time-less-p (cl-subseq (nth 5 (file-attributes file)) 0 2)
  21012. (cl-subseq time 0 2)))))
  21013. (defun org-compile-file (source process ext &optional err-msg log-buf spec)
  21014. "Compile a SOURCE file using PROCESS.
  21015. PROCESS is either a function or a list of shell commands, as
  21016. strings. EXT is a file extension, without the leading dot, as
  21017. a string. It is used to check if the process actually succeeded.
  21018. PROCESS must create a file with the same base name and directory
  21019. as SOURCE, but ending with EXT. The function then returns its
  21020. filename. Otherwise, it raises an error. The error message can
  21021. then be refined by providing string ERR-MSG, which is appended to
  21022. the standard message.
  21023. If PROCESS is a function, it is called with a single argument:
  21024. the SOURCE file.
  21025. If it is a list of commands, each of them is called using
  21026. `shell-command'. By default, in each command, %b, %f and %o are
  21027. replaced, respectively, with SOURCE base name, SOURCE full name
  21028. and SOURCE directory. It is possible, however, to use more
  21029. place-holders by specifying them in optional argument SPEC, as an
  21030. alist following the pattern (CHARACTER . REPLACEMENT-STRING).
  21031. When PROCESS is a list of commands, optional argument LOG-BUF can
  21032. be set to a buffer or a buffer name. `shell-command' then uses
  21033. it for output.
  21034. `default-directory' is set to SOURCE directory during the whole
  21035. process."
  21036. (let* ((base-name (file-name-sans-extension (file-name-nondirectory source)))
  21037. (full-name (file-truename source))
  21038. (out-dir (file-name-directory source))
  21039. ;; Properly set working directory for compilation.
  21040. (default-directory (if (file-name-absolute-p source)
  21041. (file-name-directory full-name)
  21042. default-directory))
  21043. (time (current-time))
  21044. (err-msg (if (stringp err-msg) (concat ". " err-msg) "")))
  21045. (save-window-excursion
  21046. (pcase process
  21047. ((pred functionp) (funcall process (shell-quote-argument source)))
  21048. ((pred consp)
  21049. (let ((log-buf (and log-buf (get-buffer-create log-buf)))
  21050. (spec (append spec
  21051. `((?b . ,(shell-quote-argument base-name))
  21052. (?f . ,(shell-quote-argument full-name))
  21053. (?o . ,(shell-quote-argument out-dir))))))
  21054. (dolist (command process)
  21055. (shell-command (format-spec command spec) log-buf))))
  21056. (_ (error "No valid command to process %S%s" source err-msg)))
  21057. ;; Check for process failure.
  21058. (let ((output (concat out-dir base-name "." ext)))
  21059. (unless (org-file-newer-than-p output time)
  21060. (error (format "File %S wasn't produced%s" output err-msg)))
  21061. output))))
  21062. ;;; Indentation
  21063. (defun org--get-expected-indentation (element contentsp)
  21064. "Expected indentation column for current line, according to ELEMENT.
  21065. ELEMENT is an element containing point. CONTENTSP is non-nil
  21066. when indentation is to be computed according to contents of
  21067. ELEMENT."
  21068. (let ((type (org-element-type element))
  21069. (start (org-element-property :begin element))
  21070. (post-affiliated (org-element-property :post-affiliated element)))
  21071. (org-with-wide-buffer
  21072. (cond
  21073. (contentsp
  21074. (cl-case type
  21075. ((diary-sexp footnote-definition) 0)
  21076. ((headline inlinetask nil)
  21077. (if (not org-adapt-indentation) 0
  21078. (let ((level (org-current-level)))
  21079. (if level (1+ level) 0))))
  21080. ((item plain-list) (org-list-item-body-column post-affiliated))
  21081. (t
  21082. (goto-char start)
  21083. (org-get-indentation))))
  21084. ((memq type '(headline inlinetask nil))
  21085. (if (save-excursion (beginning-of-line) (looking-at "[ \t]*$"))
  21086. (org--get-expected-indentation element t)
  21087. 0))
  21088. ((memq type '(diary-sexp footnote-definition)) 0)
  21089. ;; First paragraph of a footnote definition or an item.
  21090. ;; Indent like parent.
  21091. ((< (line-beginning-position) start)
  21092. (org--get-expected-indentation
  21093. (org-element-property :parent element) t))
  21094. ;; At first line: indent according to previous sibling, if any,
  21095. ;; ignoring footnote definitions and inline tasks, or parent's
  21096. ;; contents.
  21097. ((= (line-beginning-position) start)
  21098. (catch 'exit
  21099. (while t
  21100. (if (= (point-min) start) (throw 'exit 0)
  21101. (goto-char (1- start))
  21102. (let* ((previous (org-element-at-point))
  21103. (parent previous))
  21104. (while (and parent (<= (org-element-property :end parent) start))
  21105. (setq previous parent
  21106. parent (org-element-property :parent parent)))
  21107. (cond
  21108. ((not previous) (throw 'exit 0))
  21109. ((> (org-element-property :end previous) start)
  21110. (throw 'exit (org--get-expected-indentation previous t)))
  21111. ((memq (org-element-type previous)
  21112. '(footnote-definition inlinetask))
  21113. (setq start (org-element-property :begin previous)))
  21114. (t (goto-char (org-element-property :begin previous))
  21115. (throw 'exit
  21116. (if (bolp) (org-get-indentation)
  21117. ;; At first paragraph in an item or
  21118. ;; a footnote definition.
  21119. (org--get-expected-indentation
  21120. (org-element-property :parent previous) t))))))))))
  21121. ;; Otherwise, move to the first non-blank line above.
  21122. (t
  21123. (beginning-of-line)
  21124. (let ((pos (point)))
  21125. (skip-chars-backward " \r\t\n")
  21126. (cond
  21127. ;; Two blank lines end a footnote definition or a plain
  21128. ;; list. When we indent an empty line after them, the
  21129. ;; containing list or footnote definition is over, so it
  21130. ;; qualifies as a previous sibling. Therefore, we indent
  21131. ;; like its first line.
  21132. ((and (memq type '(footnote-definition plain-list))
  21133. (> (count-lines (point) pos) 2))
  21134. (goto-char start)
  21135. (org-get-indentation))
  21136. ;; Line above is the first one of a paragraph at the
  21137. ;; beginning of an item or a footnote definition. Indent
  21138. ;; like parent.
  21139. ((< (line-beginning-position) start)
  21140. (org--get-expected-indentation
  21141. (org-element-property :parent element) t))
  21142. ;; Line above is the beginning of an element, i.e., point
  21143. ;; was originally on the blank lines between element's start
  21144. ;; and contents.
  21145. ((= (line-beginning-position) post-affiliated)
  21146. (org--get-expected-indentation element t))
  21147. ;; POS is after contents in a greater element. Indent like
  21148. ;; the beginning of the element.
  21149. ;;
  21150. ;; As a special case, if point is at the end of a footnote
  21151. ;; definition or an item, indent like the very last element
  21152. ;; within. If that last element is an item, indent like its
  21153. ;; contents.
  21154. ((and (not (eq type 'paragraph))
  21155. (let ((cend (org-element-property :contents-end element)))
  21156. (and cend (<= cend pos))))
  21157. (if (memq type '(footnote-definition item plain-list))
  21158. (let ((last (org-element-at-point)))
  21159. (org--get-expected-indentation
  21160. last (eq (org-element-type last) 'item)))
  21161. (goto-char start)
  21162. (org-get-indentation)))
  21163. ;; In any other case, indent like the current line.
  21164. (t (org-get-indentation)))))))))
  21165. (defun org--align-node-property ()
  21166. "Align node property at point.
  21167. Alignment is done according to `org-property-format', which see."
  21168. (when (save-excursion
  21169. (beginning-of-line)
  21170. (looking-at org-property-re))
  21171. (replace-match
  21172. (concat (match-string 4)
  21173. (org-trim
  21174. (format org-property-format (match-string 1) (match-string 3))))
  21175. t t)))
  21176. (defun org-indent-line ()
  21177. "Indent line depending on context.
  21178. Indentation is done according to the following rules:
  21179. - Footnote definitions, diary sexps, headlines and inline tasks
  21180. have to start at column 0.
  21181. - On the very first line of an element, consider, in order, the
  21182. next rules until one matches:
  21183. 1. If there's a sibling element before, ignoring footnote
  21184. definitions and inline tasks, indent like its first line.
  21185. 2. If element has a parent, indent like its contents. More
  21186. precisely, if parent is an item, indent after the
  21187. description part, if any, or the bullet (see
  21188. `org-list-description-max-indent'). Else, indent like
  21189. parent's first line.
  21190. 3. Otherwise, indent relatively to current level, if
  21191. `org-adapt-indentation' is non-nil, or to left margin.
  21192. - On a blank line at the end of an element, indent according to
  21193. the type of the element. More precisely
  21194. 1. If element is a plain list, an item, or a footnote
  21195. definition, indent like the very last element within.
  21196. 2. If element is a paragraph, indent like its last non blank
  21197. line.
  21198. 3. Otherwise, indent like its very first line.
  21199. - In the code part of a source block, use language major mode
  21200. to indent current line if `org-src-tab-acts-natively' is
  21201. non-nil. If it is nil, do nothing.
  21202. - Otherwise, indent like the first non-blank line above.
  21203. The function doesn't indent an item as it could break the whole
  21204. list structure. Instead, use \\<org-mode-map>\\[org-shiftmetaleft] or \
  21205. \\[org-shiftmetaright].
  21206. Also align node properties according to `org-property-format'."
  21207. (interactive)
  21208. (cond
  21209. (orgstruct-is-++
  21210. (let ((indent-line-function
  21211. (cl-cadadr (assq 'indent-line-function org-fb-vars))))
  21212. (indent-according-to-mode)))
  21213. ((org-at-heading-p) 'noindent)
  21214. (t
  21215. (let* ((element (save-excursion (beginning-of-line) (org-element-at-point)))
  21216. (type (org-element-type element)))
  21217. (cond ((and (memq type '(plain-list item))
  21218. (= (line-beginning-position)
  21219. (org-element-property :post-affiliated element)))
  21220. 'noindent)
  21221. ((and (eq type 'src-block)
  21222. org-src-tab-acts-natively
  21223. (> (line-beginning-position)
  21224. (org-element-property :post-affiliated element))
  21225. (< (line-beginning-position)
  21226. (org-with-wide-buffer
  21227. (goto-char (org-element-property :end element))
  21228. (skip-chars-backward " \r\t\n")
  21229. (line-beginning-position))))
  21230. (org-babel-do-key-sequence-in-edit-buffer (kbd "TAB")))
  21231. (t
  21232. (let ((column (org--get-expected-indentation element nil)))
  21233. ;; Preserve current column.
  21234. (if (<= (current-column) (current-indentation))
  21235. (org-indent-line-to column)
  21236. (save-excursion (org-indent-line-to column))))
  21237. ;; Align node property. Also preserve current column.
  21238. (when (eq type 'node-property)
  21239. (let ((column (current-column)))
  21240. (org--align-node-property)
  21241. (org-move-to-column column)))))))))
  21242. (defun org-indent-region (start end)
  21243. "Indent each non-blank line in the region.
  21244. Called from a program, START and END specify the region to
  21245. indent. The function will not indent contents of example blocks,
  21246. verse blocks and export blocks as leading white spaces are
  21247. assumed to be significant there."
  21248. (interactive "r")
  21249. (save-excursion
  21250. (goto-char start)
  21251. (skip-chars-forward " \r\t\n")
  21252. (unless (eobp) (beginning-of-line))
  21253. (let ((indent-to
  21254. (lambda (ind pos)
  21255. ;; Set IND as indentation for all lines between point and
  21256. ;; POS. Blank lines are ignored. Leave point after POS
  21257. ;; once done.
  21258. (let ((limit (copy-marker pos)))
  21259. (while (< (point) limit)
  21260. (unless (org-looking-at-p "[ \t]*$") (org-indent-line-to ind))
  21261. (forward-line))
  21262. (set-marker limit nil))))
  21263. (end (copy-marker end)))
  21264. (while (< (point) end)
  21265. (if (or (org-looking-at-p " \r\t\n") (org-at-heading-p)) (forward-line)
  21266. (let* ((element (org-element-at-point))
  21267. (type (org-element-type element))
  21268. (element-end (copy-marker (org-element-property :end element)))
  21269. (ind (org--get-expected-indentation element nil)))
  21270. (cond
  21271. ((or (memq type '(paragraph table table-row))
  21272. (not (or (org-element-property :contents-begin element)
  21273. (memq type
  21274. '(example-block export-block src-block)))))
  21275. ;; Elements here are indented as a single block. Also
  21276. ;; align node properties.
  21277. (when (eq type 'node-property)
  21278. (org--align-node-property)
  21279. (beginning-of-line))
  21280. (funcall indent-to ind (min element-end end)))
  21281. (t
  21282. ;; Elements in this category consist of three parts:
  21283. ;; before the contents, the contents, and after the
  21284. ;; contents. The contents are treated specially,
  21285. ;; according to the element type, or not indented at
  21286. ;; all. Other parts are indented as a single block.
  21287. (let* ((post (copy-marker
  21288. (org-element-property :post-affiliated element)))
  21289. (cbeg
  21290. (copy-marker
  21291. (cond
  21292. ((not (org-element-property :contents-begin element))
  21293. ;; Fake contents for source blocks.
  21294. (org-with-wide-buffer
  21295. (goto-char post)
  21296. (forward-line)
  21297. (point)))
  21298. ((memq type '(footnote-definition item plain-list))
  21299. ;; Contents in these elements could start on
  21300. ;; the same line as the beginning of the
  21301. ;; element. Make sure we start indenting
  21302. ;; from the second line.
  21303. (org-with-wide-buffer
  21304. (goto-char post)
  21305. (end-of-line)
  21306. (skip-chars-forward " \r\t\n")
  21307. (if (eobp) (point) (line-beginning-position))))
  21308. (t (org-element-property :contents-begin element)))))
  21309. (cend (copy-marker
  21310. (or (org-element-property :contents-end element)
  21311. ;; Fake contents for source blocks.
  21312. (org-with-wide-buffer
  21313. (goto-char element-end)
  21314. (skip-chars-backward " \r\t\n")
  21315. (line-beginning-position)))
  21316. t)))
  21317. ;; Do not change items indentation individually as it
  21318. ;; might break the list as a whole. On the other
  21319. ;; hand, when at a plain list, indent it as a whole.
  21320. (cond ((eq type 'plain-list)
  21321. (let ((offset (- ind (org-get-indentation))))
  21322. (unless (zerop offset)
  21323. (indent-rigidly (org-element-property :begin element)
  21324. (org-element-property :end element)
  21325. offset))
  21326. (goto-char cbeg)))
  21327. ((eq type 'item) (goto-char cbeg))
  21328. (t (funcall indent-to ind (min cbeg end))))
  21329. (when (< (point) end)
  21330. (cl-case type
  21331. ((example-block export-block verse-block))
  21332. (src-block
  21333. ;; In a source block, indent source code
  21334. ;; according to language major mode, but only if
  21335. ;; `org-src-tab-acts-natively' is non-nil.
  21336. (when (and (< (point) end) org-src-tab-acts-natively)
  21337. (ignore-errors
  21338. (org-babel-do-in-edit-buffer
  21339. (indent-region (point-min) (point-max))))))
  21340. (t (org-indent-region (point) (min cend end))))
  21341. (goto-char (min cend end))
  21342. (when (< (point) end)
  21343. (funcall indent-to ind (min element-end end))))
  21344. (set-marker post nil)
  21345. (set-marker cbeg nil)
  21346. (set-marker cend nil))))
  21347. (set-marker element-end nil))))
  21348. (set-marker end nil))))
  21349. (defun org-indent-drawer ()
  21350. "Indent the drawer at point."
  21351. (interactive)
  21352. (unless (save-excursion
  21353. (beginning-of-line)
  21354. (org-looking-at-p org-drawer-regexp))
  21355. (user-error "Not at a drawer"))
  21356. (let ((element (org-element-at-point)))
  21357. (unless (memq (org-element-type element) '(drawer property-drawer))
  21358. (user-error "Not at a drawer"))
  21359. (org-with-wide-buffer
  21360. (org-indent-region (org-element-property :begin element)
  21361. (org-element-property :end element))))
  21362. (message "Drawer at point indented"))
  21363. (defun org-indent-block ()
  21364. "Indent the block at point."
  21365. (interactive)
  21366. (unless (save-excursion
  21367. (beginning-of-line)
  21368. (let ((case-fold-search t))
  21369. (org-looking-at-p "[ \t]*#\\+\\(begin\\|end\\)_")))
  21370. (user-error "Not at a block"))
  21371. (let ((element (org-element-at-point)))
  21372. (unless (memq (org-element-type element)
  21373. '(comment-block center-block dynamic-block example-block
  21374. export-block quote-block special-block
  21375. src-block verse-block))
  21376. (user-error "Not at a block"))
  21377. (org-with-wide-buffer
  21378. (org-indent-region (org-element-property :begin element)
  21379. (org-element-property :end element))))
  21380. (message "Block at point indented"))
  21381. ;;; Filling
  21382. ;; We use our own fill-paragraph and auto-fill functions.
  21383. ;; `org-fill-paragraph' relies on adaptive filling and context
  21384. ;; checking. Appropriate `fill-prefix' is computed with
  21385. ;; `org-adaptive-fill-function'.
  21386. ;; `org-auto-fill-function' takes care of auto-filling. It calls
  21387. ;; `do-auto-fill' only on valid areas with `fill-prefix' shadowed with
  21388. ;; `org-adaptive-fill-function' value. Internally,
  21389. ;; `org-comment-line-break-function' breaks the line.
  21390. ;; `org-setup-filling' installs filling and auto-filling related
  21391. ;; variables during `org-mode' initialization.
  21392. (defvar org-element-paragraph-separate) ; org-element.el
  21393. (defun org-setup-filling ()
  21394. (require 'org-element)
  21395. ;; Prevent auto-fill from inserting unwanted new items.
  21396. (when (boundp 'fill-nobreak-predicate)
  21397. (setq-local
  21398. fill-nobreak-predicate
  21399. (org-uniquify
  21400. (append fill-nobreak-predicate
  21401. '(org-fill-line-break-nobreak-p
  21402. org-fill-paragraph-with-timestamp-nobreak-p)))))
  21403. (let ((paragraph-ending (substring org-element-paragraph-separate 1)))
  21404. (setq-local paragraph-start paragraph-ending)
  21405. (setq-local paragraph-separate paragraph-ending))
  21406. (setq-local fill-paragraph-function 'org-fill-paragraph)
  21407. (setq-local auto-fill-inhibit-regexp nil)
  21408. (setq-local adaptive-fill-function 'org-adaptive-fill-function)
  21409. (setq-local normal-auto-fill-function 'org-auto-fill-function)
  21410. (setq-local comment-line-break-function 'org-comment-line-break-function))
  21411. (defun org-fill-line-break-nobreak-p ()
  21412. "Non-nil when a new line at point would create an Org line break."
  21413. (save-excursion
  21414. (skip-chars-backward "[ \t]")
  21415. (skip-chars-backward "\\\\")
  21416. (looking-at "\\\\\\\\\\($\\|[^\\\\]\\)")))
  21417. (defun org-fill-paragraph-with-timestamp-nobreak-p ()
  21418. "Non-nil when a new line at point would split a timestamp."
  21419. (and (org-at-timestamp-p t)
  21420. (not (looking-at org-ts-regexp-both))))
  21421. (declare-function message-in-body-p "message" ())
  21422. (defvar orgtbl-line-start-regexp) ; From org-table.el
  21423. (defun org-adaptive-fill-function ()
  21424. "Compute a fill prefix for the current line.
  21425. Return fill prefix, as a string, or nil if current line isn't
  21426. meant to be filled. For convenience, if `adaptive-fill-regexp'
  21427. matches in paragraphs or comments, use it."
  21428. (catch 'exit
  21429. (when (derived-mode-p 'message-mode)
  21430. (save-excursion
  21431. (beginning-of-line)
  21432. (cond ((not (message-in-body-p)) (throw 'exit nil))
  21433. ((org-looking-at-p org-table-line-regexp) (throw 'exit nil))
  21434. ((looking-at message-cite-prefix-regexp)
  21435. (throw 'exit (match-string-no-properties 0)))
  21436. ((looking-at org-outline-regexp)
  21437. (throw 'exit (make-string (length (match-string 0)) ?\s))))))
  21438. (org-with-wide-buffer
  21439. (unless (org-at-heading-p)
  21440. (let* ((p (line-beginning-position))
  21441. (element (save-excursion
  21442. (beginning-of-line)
  21443. (org-element-at-point)))
  21444. (type (org-element-type element))
  21445. (post-affiliated (org-element-property :post-affiliated element)))
  21446. (unless (< p post-affiliated)
  21447. (cl-case type
  21448. (comment
  21449. (save-excursion
  21450. (beginning-of-line)
  21451. (looking-at "[ \t]*")
  21452. (concat (match-string 0) "# ")))
  21453. (footnote-definition "")
  21454. ((item plain-list)
  21455. (make-string (org-list-item-body-column post-affiliated) ?\s))
  21456. (paragraph
  21457. ;; Fill prefix is usually the same as the current line,
  21458. ;; unless the paragraph is at the beginning of an item.
  21459. (let ((parent (org-element-property :parent element)))
  21460. (save-excursion
  21461. (beginning-of-line)
  21462. (cond ((eq (org-element-type parent) 'item)
  21463. (make-string (org-list-item-body-column
  21464. (org-element-property :begin parent))
  21465. ?\s))
  21466. ((and adaptive-fill-regexp
  21467. ;; Locally disable
  21468. ;; `adaptive-fill-function' to let
  21469. ;; `fill-context-prefix' handle
  21470. ;; `adaptive-fill-regexp' variable.
  21471. (let (adaptive-fill-function)
  21472. (fill-context-prefix
  21473. post-affiliated
  21474. (org-element-property :end element)))))
  21475. ((looking-at "[ \t]+") (match-string 0))
  21476. (t "")))))
  21477. (comment-block
  21478. ;; Only fill contents if P is within block boundaries.
  21479. (let* ((cbeg (save-excursion (goto-char post-affiliated)
  21480. (forward-line)
  21481. (point)))
  21482. (cend (save-excursion
  21483. (goto-char (org-element-property :end element))
  21484. (skip-chars-backward " \r\t\n")
  21485. (line-beginning-position))))
  21486. (when (and (>= p cbeg) (< p cend))
  21487. (if (save-excursion (beginning-of-line) (looking-at "[ \t]+"))
  21488. (match-string 0)
  21489. "")))))))))))
  21490. (declare-function message-goto-body "message" ())
  21491. (defvar message-cite-prefix-regexp) ; From message.el
  21492. (defun org-fill-paragraph (&optional justify)
  21493. "Fill element at point, when applicable.
  21494. This function only applies to comment blocks, comments, example
  21495. blocks and paragraphs. Also, as a special case, re-align table
  21496. when point is at one.
  21497. If JUSTIFY is non-nil (interactively, with prefix argument),
  21498. justify as well. If `sentence-end-double-space' is non-nil, then
  21499. period followed by one space does not end a sentence, so don't
  21500. break a line there. The variable `fill-column' controls the
  21501. width for filling.
  21502. For convenience, when point is at a plain list, an item or
  21503. a footnote definition, try to fill the first paragraph within."
  21504. (interactive)
  21505. (if (and (derived-mode-p 'message-mode)
  21506. (or (not (message-in-body-p))
  21507. (save-excursion (move-beginning-of-line 1)
  21508. (looking-at message-cite-prefix-regexp))))
  21509. ;; First ensure filling is correct in message-mode.
  21510. (let ((fill-paragraph-function
  21511. (cl-cadadr (assq 'fill-paragraph-function org-fb-vars)))
  21512. (fill-prefix (cl-cadadr (assq 'fill-prefix org-fb-vars)))
  21513. (paragraph-start (cl-cadadr (assq 'paragraph-start org-fb-vars)))
  21514. (paragraph-separate
  21515. (cl-cadadr (assq 'paragraph-separate org-fb-vars))))
  21516. (fill-paragraph nil))
  21517. (with-syntax-table org-mode-transpose-word-syntax-table
  21518. ;; Move to end of line in order to get the first paragraph
  21519. ;; within a plain list or a footnote definition.
  21520. (let ((element (save-excursion
  21521. (end-of-line)
  21522. (or (ignore-errors (org-element-at-point))
  21523. (user-error "An element cannot be parsed line %d"
  21524. (line-number-at-pos (point)))))))
  21525. ;; First check if point is in a blank line at the beginning of
  21526. ;; the buffer. In that case, ignore filling.
  21527. (cl-case (org-element-type element)
  21528. ;; Use major mode filling function is src blocks.
  21529. (src-block (org-babel-do-key-sequence-in-edit-buffer (kbd "M-q")))
  21530. ;; Align Org tables, leave table.el tables as-is.
  21531. (table-row (org-table-align) t)
  21532. (table
  21533. (when (eq (org-element-property :type element) 'org)
  21534. (save-excursion
  21535. (goto-char (org-element-property :post-affiliated element))
  21536. (org-table-align)))
  21537. t)
  21538. (paragraph
  21539. ;; Paragraphs may contain `line-break' type objects.
  21540. (let ((beg (max (point-min)
  21541. (org-element-property :contents-begin element)))
  21542. (end (min (point-max)
  21543. (org-element-property :contents-end element))))
  21544. ;; Do nothing if point is at an affiliated keyword.
  21545. (if (< (line-end-position) beg) t
  21546. (when (derived-mode-p 'message-mode)
  21547. ;; In `message-mode', do not fill following citation
  21548. ;; in current paragraph nor text before message body.
  21549. (let ((body-start (save-excursion (message-goto-body))))
  21550. (when body-start (setq beg (max body-start beg))))
  21551. (when (save-excursion
  21552. (re-search-forward
  21553. (concat "^" message-cite-prefix-regexp) end t))
  21554. (setq end (match-beginning 0))))
  21555. ;; Fill paragraph, taking line breaks into account.
  21556. (save-excursion
  21557. (goto-char beg)
  21558. (let ((cuts (list beg)))
  21559. (while (re-search-forward "\\\\\\\\[ \t]*\n" end t)
  21560. (when (eq 'line-break
  21561. (org-element-type
  21562. (save-excursion (backward-char)
  21563. (org-element-context))))
  21564. (push (point) cuts)))
  21565. (dolist (c (delq end cuts))
  21566. (fill-region-as-paragraph c end justify)
  21567. (setq end c))))
  21568. t)))
  21569. ;; Contents of `comment-block' type elements should be
  21570. ;; filled as plain text, but only if point is within block
  21571. ;; markers.
  21572. (comment-block
  21573. (let* ((case-fold-search t)
  21574. (beg (save-excursion
  21575. (goto-char (org-element-property :begin element))
  21576. (re-search-forward "^[ \t]*#\\+begin_comment" nil t)
  21577. (forward-line)
  21578. (point)))
  21579. (end (save-excursion
  21580. (goto-char (org-element-property :end element))
  21581. (re-search-backward "^[ \t]*#\\+end_comment" nil t)
  21582. (line-beginning-position))))
  21583. (if (or (< (point) beg) (> (point) end)) t
  21584. (fill-region-as-paragraph
  21585. (save-excursion (end-of-line)
  21586. (re-search-backward "^[ \t]*$" beg 'move)
  21587. (line-beginning-position))
  21588. (save-excursion (beginning-of-line)
  21589. (re-search-forward "^[ \t]*$" end 'move)
  21590. (line-beginning-position))
  21591. justify))))
  21592. ;; Fill comments.
  21593. (comment
  21594. (let ((begin (org-element-property :post-affiliated element))
  21595. (end (org-element-property :end element)))
  21596. (when (and (>= (point) begin) (<= (point) end))
  21597. (let ((begin (save-excursion
  21598. (end-of-line)
  21599. (if (re-search-backward "^[ \t]*#[ \t]*$" begin t)
  21600. (progn (forward-line) (point))
  21601. begin)))
  21602. (end (save-excursion
  21603. (end-of-line)
  21604. (if (re-search-forward "^[ \t]*#[ \t]*$" end 'move)
  21605. (1- (line-beginning-position))
  21606. (skip-chars-backward " \r\t\n")
  21607. (line-end-position)))))
  21608. ;; Do not fill comments when at a blank line.
  21609. (when (> end begin)
  21610. (let ((fill-prefix
  21611. (save-excursion
  21612. (beginning-of-line)
  21613. (looking-at "[ \t]*#")
  21614. (let ((comment-prefix (match-string 0)))
  21615. (goto-char (match-end 0))
  21616. (if (looking-at adaptive-fill-regexp)
  21617. (concat comment-prefix (match-string 0))
  21618. (concat comment-prefix " "))))))
  21619. (save-excursion
  21620. (fill-region-as-paragraph begin end justify))))))
  21621. t))
  21622. ;; Ignore every other element.
  21623. (otherwise t))))))
  21624. (defun org-auto-fill-function ()
  21625. "Auto-fill function."
  21626. ;; Check if auto-filling is meaningful.
  21627. (let ((fc (current-fill-column)))
  21628. (when (and fc (> (current-column) fc))
  21629. (let* ((fill-prefix (org-adaptive-fill-function))
  21630. ;; Enforce empty fill prefix, if required. Otherwise, it
  21631. ;; will be computed again.
  21632. (adaptive-fill-mode (not (equal fill-prefix ""))))
  21633. (when fill-prefix (do-auto-fill))))))
  21634. (defun org-comment-line-break-function (&optional soft)
  21635. "Break line at point and indent, continuing comment if within one.
  21636. The inserted newline is marked hard if variable
  21637. `use-hard-newlines' is true, unless optional argument SOFT is
  21638. non-nil."
  21639. (if soft (insert-and-inherit ?\n) (newline 1))
  21640. (save-excursion (forward-char -1) (delete-horizontal-space))
  21641. (delete-horizontal-space)
  21642. (indent-to-left-margin)
  21643. (insert-before-markers-and-inherit fill-prefix))
  21644. ;;; Fixed Width Areas
  21645. (defun org-toggle-fixed-width ()
  21646. "Toggle fixed-width markup.
  21647. Add or remove fixed-width markup on current line, whenever it
  21648. makes sense. Return an error otherwise.
  21649. If a region is active and if it contains only fixed-width areas
  21650. or blank lines, remove all fixed-width markup in it. If the
  21651. region contains anything else, convert all non-fixed-width lines
  21652. to fixed-width ones.
  21653. Blank lines at the end of the region are ignored unless the
  21654. region only contains such lines."
  21655. (interactive)
  21656. (if (not (org-region-active-p))
  21657. ;; No region:
  21658. ;;
  21659. ;; Remove fixed width marker only in a fixed-with element.
  21660. ;;
  21661. ;; Add fixed width maker in paragraphs, in blank lines after
  21662. ;; elements or at the beginning of a headline or an inlinetask,
  21663. ;; and before any one-line elements (e.g., a clock).
  21664. (progn
  21665. (beginning-of-line)
  21666. (let* ((element (org-element-at-point))
  21667. (type (org-element-type element)))
  21668. (cond
  21669. ((and (eq type 'fixed-width)
  21670. (looking-at "[ \t]*\\(:\\(?: \\|$\\)\\)"))
  21671. (replace-match
  21672. "" nil nil nil (if (= (line-end-position) (match-end 0)) 0 1)))
  21673. ((and (memq type '(babel-call clock comment diary-sexp headline
  21674. horizontal-rule keyword paragraph
  21675. planning))
  21676. (<= (org-element-property :post-affiliated element) (point)))
  21677. (skip-chars-forward " \t")
  21678. (insert ": "))
  21679. ((and (org-looking-at-p "[ \t]*$")
  21680. (or (eq type 'inlinetask)
  21681. (save-excursion
  21682. (skip-chars-forward " \r\t\n")
  21683. (<= (org-element-property :end element) (point)))))
  21684. (delete-region (point) (line-end-position))
  21685. (org-indent-line)
  21686. (insert ": "))
  21687. (t (user-error "Cannot insert a fixed-width line here")))))
  21688. ;; Region active.
  21689. (let* ((begin (save-excursion
  21690. (goto-char (region-beginning))
  21691. (line-beginning-position)))
  21692. (end (copy-marker
  21693. (save-excursion
  21694. (goto-char (region-end))
  21695. (unless (eolp) (beginning-of-line))
  21696. (if (save-excursion (re-search-backward "\\S-" begin t))
  21697. (progn (skip-chars-backward " \r\t\n") (point))
  21698. (point)))))
  21699. (all-fixed-width-p
  21700. (catch 'not-all-p
  21701. (save-excursion
  21702. (goto-char begin)
  21703. (skip-chars-forward " \r\t\n")
  21704. (when (eobp) (throw 'not-all-p nil))
  21705. (while (< (point) end)
  21706. (let ((element (org-element-at-point)))
  21707. (if (eq (org-element-type element) 'fixed-width)
  21708. (goto-char (org-element-property :end element))
  21709. (throw 'not-all-p nil))))
  21710. t))))
  21711. (if all-fixed-width-p
  21712. (save-excursion
  21713. (goto-char begin)
  21714. (while (< (point) end)
  21715. (when (looking-at "[ \t]*\\(:\\(?: \\|$\\)\\)")
  21716. (replace-match
  21717. "" nil nil nil
  21718. (if (= (line-end-position) (match-end 0)) 0 1)))
  21719. (forward-line)))
  21720. (let ((min-ind (point-max)))
  21721. ;; Find minimum indentation across all lines.
  21722. (save-excursion
  21723. (goto-char begin)
  21724. (if (not (save-excursion (re-search-forward "\\S-" end t)))
  21725. (setq min-ind 0)
  21726. (catch 'zerop
  21727. (while (< (point) end)
  21728. (unless (org-looking-at-p "[ \t]*$")
  21729. (let ((ind (org-get-indentation)))
  21730. (setq min-ind (min min-ind ind))
  21731. (when (zerop ind) (throw 'zerop t))))
  21732. (forward-line)))))
  21733. ;; Loop over all lines and add fixed-width markup everywhere
  21734. ;; but in fixed-width lines.
  21735. (save-excursion
  21736. (goto-char begin)
  21737. (while (< (point) end)
  21738. (cond
  21739. ((org-at-heading-p)
  21740. (insert ": ")
  21741. (forward-line)
  21742. (while (and (< (point) end) (org-looking-at-p "[ \t]*$"))
  21743. (insert ":")
  21744. (forward-line)))
  21745. ((org-looking-at-p "[ \t]*:\\( \\|$\\)")
  21746. (let* ((element (org-element-at-point))
  21747. (element-end (org-element-property :end element)))
  21748. (if (eq (org-element-type element) 'fixed-width)
  21749. (progn (goto-char element-end)
  21750. (skip-chars-backward " \r\t\n")
  21751. (forward-line))
  21752. (let ((limit (min end element-end)))
  21753. (while (< (point) limit)
  21754. (org-move-to-column min-ind t)
  21755. (insert ": ")
  21756. (forward-line))))))
  21757. (t
  21758. (org-move-to-column min-ind t)
  21759. (insert ": ")
  21760. (forward-line)))))))
  21761. (set-marker end nil))))
  21762. ;;; Comments
  21763. ;; Org comments syntax is quite complex. It requires the entire line
  21764. ;; to be just a comment. Also, even with the right syntax at the
  21765. ;; beginning of line, some some elements (i.e. verse-block or
  21766. ;; example-block) don't accept comments. Usual Emacs comment commands
  21767. ;; cannot cope with those requirements. Therefore, Org replaces them.
  21768. ;; Org still relies on `comment-dwim', but cannot trust
  21769. ;; `comment-only-p'. So, `comment-region-function' and
  21770. ;; `uncomment-region-function' both point
  21771. ;; to`org-comment-or-uncomment-region'. Eventually,
  21772. ;; `org-insert-comment' takes care of insertion of comments at the
  21773. ;; beginning of line.
  21774. ;; `org-setup-comments-handling' install comments related variables
  21775. ;; during `org-mode' initialization.
  21776. (defun org-setup-comments-handling ()
  21777. (interactive)
  21778. (setq-local comment-use-syntax nil)
  21779. (setq-local comment-start "# ")
  21780. (setq-local comment-start-skip "^\\s-*#\\(?: \\|$\\)")
  21781. (setq-local comment-insert-comment-function 'org-insert-comment)
  21782. (setq-local comment-region-function 'org-comment-or-uncomment-region)
  21783. (setq-local uncomment-region-function 'org-comment-or-uncomment-region))
  21784. (defun org-insert-comment ()
  21785. "Insert an empty comment above current line.
  21786. If the line is empty, insert comment at its beginning. When
  21787. point is within a source block, comment according to the related
  21788. major mode."
  21789. (if (let ((element (org-element-at-point)))
  21790. (and (eq (org-element-type element) 'src-block)
  21791. (< (save-excursion
  21792. (goto-char (org-element-property :post-affiliated element))
  21793. (line-end-position))
  21794. (point))
  21795. (> (save-excursion
  21796. (goto-char (org-element-property :end element))
  21797. (skip-chars-backward " \r\t\n")
  21798. (line-beginning-position))
  21799. (point))))
  21800. (org-babel-do-in-edit-buffer (call-interactively 'comment-dwim))
  21801. (beginning-of-line)
  21802. (if (looking-at "\\s-*$") (delete-region (point) (point-at-eol))
  21803. (open-line 1))
  21804. (org-indent-line)
  21805. (insert "# ")))
  21806. (defvar comment-empty-lines) ; From newcomment.el.
  21807. (defun org-comment-or-uncomment-region (beg end &rest _)
  21808. "Comment or uncomment each non-blank line in the region.
  21809. Uncomment each non-blank line between BEG and END if it only
  21810. contains commented lines. Otherwise, comment them. If region is
  21811. strictly within a source block, use appropriate comment syntax."
  21812. (if (let ((element (org-element-at-point)))
  21813. (and (eq (org-element-type element) 'src-block)
  21814. (< (save-excursion
  21815. (goto-char (org-element-property :post-affiliated element))
  21816. (line-end-position))
  21817. beg)
  21818. (>= (save-excursion
  21819. (goto-char (org-element-property :end element))
  21820. (skip-chars-backward " \r\t\n")
  21821. (line-beginning-position))
  21822. end)))
  21823. (org-babel-do-in-edit-buffer (call-interactively 'comment-dwim))
  21824. (save-restriction
  21825. ;; Restrict region
  21826. (narrow-to-region (save-excursion (goto-char beg)
  21827. (skip-chars-forward " \r\t\n" end)
  21828. (line-beginning-position))
  21829. (save-excursion (goto-char end)
  21830. (skip-chars-backward " \r\t\n" beg)
  21831. (line-end-position)))
  21832. (let ((uncommentp
  21833. ;; UNCOMMENTP is non-nil when every non blank line between
  21834. ;; BEG and END is a comment.
  21835. (save-excursion
  21836. (goto-char (point-min))
  21837. (while (and (not (eobp))
  21838. (let ((element (org-element-at-point)))
  21839. (and (eq (org-element-type element) 'comment)
  21840. (goto-char (min (point-max)
  21841. (org-element-property
  21842. :end element)))))))
  21843. (eobp))))
  21844. (if uncommentp
  21845. ;; Only blank lines and comments in region: uncomment it.
  21846. (save-excursion
  21847. (goto-char (point-min))
  21848. (while (not (eobp))
  21849. (when (looking-at "[ \t]*\\(#\\(?: \\|$\\)\\)")
  21850. (replace-match "" nil nil nil 1))
  21851. (forward-line)))
  21852. ;; Comment each line in region.
  21853. (let ((min-indent (point-max)))
  21854. ;; First find the minimum indentation across all lines.
  21855. (save-excursion
  21856. (goto-char (point-min))
  21857. (while (and (not (eobp)) (not (zerop min-indent)))
  21858. (unless (looking-at "[ \t]*$")
  21859. (setq min-indent (min min-indent (current-indentation))))
  21860. (forward-line)))
  21861. ;; Then loop over all lines.
  21862. (save-excursion
  21863. (goto-char (point-min))
  21864. (while (not (eobp))
  21865. (unless (and (not comment-empty-lines) (looking-at "[ \t]*$"))
  21866. ;; Don't get fooled by invisible text (e.g. link path)
  21867. ;; when moving to column MIN-INDENT.
  21868. (let ((buffer-invisibility-spec nil))
  21869. (org-move-to-column min-indent t))
  21870. (insert comment-start))
  21871. (forward-line)))))))))
  21872. (defun org-comment-dwim (_arg)
  21873. "Call `comment-dwim' within a source edit buffer if needed."
  21874. (interactive "*P")
  21875. (if (org-in-src-block-p)
  21876. (org-babel-do-in-edit-buffer (call-interactively 'comment-dwim))
  21877. (call-interactively 'comment-dwim)))
  21878. ;;; Timestamps API
  21879. ;; This section contains tools to operate on timestamp objects, as
  21880. ;; returned by, e.g. `org-element-context'.
  21881. (defun org-timestamp--to-internal-time (timestamp &optional end)
  21882. "Encode TIMESTAMP object into Emacs internal time.
  21883. Use end of date range or time range when END is non-nil."
  21884. (apply #'encode-time
  21885. (cons 0
  21886. (mapcar
  21887. (lambda (prop) (or (org-element-property prop timestamp) 0))
  21888. (if end '(:minute-end :hour-end :day-end :month-end :year-end)
  21889. '(:minute-start :hour-start :day-start :month-start
  21890. :year-start))))))
  21891. (defun org-timestamp-has-time-p (timestamp)
  21892. "Non-nil when TIMESTAMP has a time specified."
  21893. (org-element-property :hour-start timestamp))
  21894. (defun org-timestamp-format (timestamp format &optional end utc)
  21895. "Format a TIMESTAMP object into a string.
  21896. FORMAT is a format specifier to be passed to
  21897. `format-time-string'.
  21898. When optional argument END is non-nil, use end of date-range or
  21899. time-range, if possible.
  21900. When optional argument UTC is non-nil, time will be expressed as
  21901. Universal Time."
  21902. (format-time-string
  21903. format (org-timestamp--to-internal-time timestamp end)
  21904. (and utc t)))
  21905. (defun org-timestamp-split-range (timestamp &optional end)
  21906. "Extract a TIMESTAMP object from a date or time range.
  21907. END, when non-nil, means extract the end of the range.
  21908. Otherwise, extract its start.
  21909. Return a new timestamp object."
  21910. (let ((type (org-element-property :type timestamp)))
  21911. (if (memq type '(active inactive diary)) timestamp
  21912. (let ((split-ts (org-element-copy timestamp)))
  21913. ;; Set new type.
  21914. (org-element-put-property
  21915. split-ts :type (if (eq type 'active-range) 'active 'inactive))
  21916. ;; Copy start properties over end properties if END is
  21917. ;; non-nil. Otherwise, copy end properties over `start' ones.
  21918. (let ((p-alist '((:minute-start . :minute-end)
  21919. (:hour-start . :hour-end)
  21920. (:day-start . :day-end)
  21921. (:month-start . :month-end)
  21922. (:year-start . :year-end))))
  21923. (dolist (p-cell p-alist)
  21924. (org-element-put-property
  21925. split-ts
  21926. (funcall (if end #'car #'cdr) p-cell)
  21927. (org-element-property
  21928. (funcall (if end #'cdr #'car) p-cell) split-ts)))
  21929. ;; Eventually refresh `:raw-value'.
  21930. (org-element-put-property split-ts :raw-value nil)
  21931. (org-element-put-property
  21932. split-ts :raw-value (org-element-interpret-data split-ts)))))))
  21933. (defun org-timestamp-translate (timestamp &optional boundary)
  21934. "Translate TIMESTAMP object to custom format.
  21935. Format string is defined in `org-time-stamp-custom-formats',
  21936. which see.
  21937. When optional argument BOUNDARY is non-nil, it is either the
  21938. symbol `start' or `end'. In this case, only translate the
  21939. starting or ending part of TIMESTAMP if it is a date or time
  21940. range. Otherwise, translate both parts.
  21941. Return timestamp as-is if `org-display-custom-times' is nil or if
  21942. it has a `diary' type."
  21943. (let ((type (org-element-property :type timestamp)))
  21944. (if (or (not org-display-custom-times) (eq type 'diary))
  21945. (org-element-interpret-data timestamp)
  21946. (let ((fmt (funcall (if (org-timestamp-has-time-p timestamp) #'cdr #'car)
  21947. org-time-stamp-custom-formats)))
  21948. (if (and (not boundary) (memq type '(active-range inactive-range)))
  21949. (concat (org-timestamp-format timestamp fmt)
  21950. "--"
  21951. (org-timestamp-format timestamp fmt t))
  21952. (org-timestamp-format timestamp fmt (eq boundary 'end)))))))
  21953. ;;; Other stuff.
  21954. (defvar reftex-docstruct-symbol)
  21955. (defvar reftex-cite-format)
  21956. (defvar org--rds)
  21957. (defun org-reftex-citation ()
  21958. "Use reftex-citation to insert a citation into the buffer.
  21959. This looks for a line like
  21960. #+BIBLIOGRAPHY: foo plain option:-d
  21961. and derives from it that foo.bib is the bibliography file relevant
  21962. for this document. It then installs the necessary environment for RefTeX
  21963. to work in this buffer and calls `reftex-citation' to insert a citation
  21964. into the buffer.
  21965. Export of such citations to both LaTeX and HTML is handled by the contributed
  21966. package ox-bibtex by Taru Karttunen."
  21967. (interactive)
  21968. (let ((reftex-docstruct-symbol 'org--rds)
  21969. (reftex-cite-format "\\cite{%l}")
  21970. org--rds bib)
  21971. (save-excursion
  21972. (save-restriction
  21973. (widen)
  21974. (let ((case-fold-search t)
  21975. (re "^[ \t]*#\\+BIBLIOGRAPHY:[ \t]+\\([^ \t\n]+\\)"))
  21976. (if (not (save-excursion
  21977. (or (re-search-forward re nil t)
  21978. (re-search-backward re nil t))))
  21979. (user-error "No bibliography defined in file")
  21980. (setq bib (concat (match-string 1) ".bib")
  21981. org--rds (list (list 'bib bib)))))))
  21982. (call-interactively 'reftex-citation)))
  21983. ;;;; Functions extending outline functionality
  21984. (defun org-beginning-of-line (&optional arg)
  21985. "Go to the beginning of the current line. If that is invisible, continue
  21986. to a visible line beginning. This makes the function of C-a more intuitive.
  21987. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  21988. first attempt, and only move to after the tags when the cursor is already
  21989. beyond the end of the headline."
  21990. (interactive "P")
  21991. (let ((pos (point))
  21992. (special (if (consp org-special-ctrl-a/e)
  21993. (car org-special-ctrl-a/e)
  21994. org-special-ctrl-a/e))
  21995. deactivate-mark refpos)
  21996. (call-interactively (if (org-bound-and-true-p visual-line-mode)
  21997. #'beginning-of-visual-line
  21998. #'move-beginning-of-line))
  21999. (cond
  22000. ((or arg (not special)))
  22001. ((and (looking-at org-complex-heading-regexp)
  22002. (eq (char-after (match-end 1)) ?\s))
  22003. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  22004. (point-at-eol)))
  22005. (goto-char
  22006. (if (eq special t)
  22007. (cond ((> pos refpos) refpos)
  22008. ((= pos (point)) refpos)
  22009. (t (point)))
  22010. (cond ((> pos (point)) (point))
  22011. ((not (eq last-command this-command)) (point))
  22012. (t refpos)))))
  22013. ((org-at-item-p)
  22014. ;; Being at an item and not looking at an the item means point
  22015. ;; was previously moved to beginning of a visual line, which
  22016. ;; doesn't contain the item. Therefore, do nothing special,
  22017. ;; just stay here.
  22018. (when (looking-at org-list-full-item-re)
  22019. ;; Set special position at first white space character after
  22020. ;; bullet, and check-box, if any.
  22021. (let ((after-bullet
  22022. (let ((box (match-end 3)))
  22023. (if (not box) (match-end 1)
  22024. (let ((after (char-after box)))
  22025. (if (and after (= after ? )) (1+ box) box))))))
  22026. ;; Special case: Move point to special position when
  22027. ;; currently after it or at beginning of line.
  22028. (if (eq special t)
  22029. (when (or (> pos after-bullet) (= (point) pos))
  22030. (goto-char after-bullet))
  22031. ;; Reversed case: Move point to special position when
  22032. ;; point was already at beginning of line and command is
  22033. ;; repeated.
  22034. (when (and (= (point) pos) (eq last-command this-command))
  22035. (goto-char after-bullet))))))))
  22036. (setq disable-point-adjustment
  22037. (or (not (invisible-p (point)))
  22038. (not (invisible-p (max (point-min) (1- (point))))))))
  22039. (defun org-end-of-line (&optional arg)
  22040. "Go to the end of the line.
  22041. If this is a headline, and `org-special-ctrl-a/e' is set, ignore
  22042. tags on the first attempt, and only move to after the tags when
  22043. the cursor is already beyond the end of the headline."
  22044. (interactive "P")
  22045. (let ((special (if (consp org-special-ctrl-a/e) (cdr org-special-ctrl-a/e)
  22046. org-special-ctrl-a/e))
  22047. (move-fun (cond ((org-bound-and-true-p visual-line-mode)
  22048. 'end-of-visual-line)
  22049. ((fboundp 'move-end-of-line) 'move-end-of-line)
  22050. (t 'end-of-line)))
  22051. deactivate-mark)
  22052. (if (or (not special) arg) (call-interactively move-fun)
  22053. (let* ((element (save-excursion (beginning-of-line)
  22054. (org-element-at-point)))
  22055. (type (org-element-type element)))
  22056. (cond
  22057. ((memq type '(headline inlinetask))
  22058. (let ((pos (point)))
  22059. (beginning-of-line 1)
  22060. (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*\\)?$"))
  22061. (if (eq special t)
  22062. (if (or (< pos (match-beginning 1)) (= pos (match-end 0)))
  22063. (goto-char (match-beginning 1))
  22064. (goto-char (match-end 0)))
  22065. (if (or (< pos (match-end 0))
  22066. (not (eq this-command last-command)))
  22067. (goto-char (match-end 0))
  22068. (goto-char (match-beginning 1))))
  22069. (call-interactively move-fun))))
  22070. ((outline-invisible-p (line-end-position))
  22071. ;; If element is hidden, `move-end-of-line' would put point
  22072. ;; after it. Use `end-of-line' to stay on current line.
  22073. (call-interactively 'end-of-line))
  22074. (t (call-interactively move-fun)))))
  22075. (org-no-warnings (and (featurep 'xemacs) (setq zmacs-region-stays t))))
  22076. (setq disable-point-adjustment
  22077. (or (not (invisible-p (point)))
  22078. (not (invisible-p (max (point-min) (1- (point))))))))
  22079. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  22080. (define-key org-mode-map "\C-e" 'org-end-of-line)
  22081. (defun org-backward-sentence (&optional _arg)
  22082. "Go to beginning of sentence, or beginning of table field.
  22083. This will call `backward-sentence' or `org-table-beginning-of-field',
  22084. depending on context."
  22085. (interactive)
  22086. (let* ((element (org-element-at-point))
  22087. (contents-begin (org-element-property :contents-begin element))
  22088. (table (org-element-lineage element '(table) t)))
  22089. (if (and table
  22090. (> (point) contents-begin)
  22091. (<= (point) (org-element-property :contents-end table)))
  22092. (call-interactively #'org-table-beginning-of-field)
  22093. (save-restriction
  22094. (when (and contents-begin
  22095. (< (point-min) contents-begin)
  22096. (> (point) contents-begin))
  22097. (narrow-to-region contents-begin
  22098. (org-element-property :contents-end element)))
  22099. (call-interactively #'backward-sentence)))))
  22100. (defun org-forward-sentence (&optional _arg)
  22101. "Go to end of sentence, or end of table field.
  22102. This will call `forward-sentence' or `org-table-end-of-field',
  22103. depending on context."
  22104. (interactive)
  22105. (let* ((element (org-element-at-point))
  22106. (contents-end (org-element-property :contents-end element))
  22107. (table (org-element-lineage element '(table) t)))
  22108. (if (and table
  22109. (>= (point) (org-element-property :contents-begin table))
  22110. (< (point) contents-end))
  22111. (call-interactively #'org-table-end-of-field)
  22112. (save-restriction
  22113. (when (and contents-end
  22114. (> (point-max) contents-end)
  22115. ;; Skip blank lines between elements.
  22116. (< (org-element-property :end element)
  22117. (save-excursion (goto-char contents-end)
  22118. (skip-chars-forward " \r\t\n"))))
  22119. (narrow-to-region (org-element-property :contents-begin element)
  22120. contents-end))
  22121. (call-interactively #'forward-sentence)))))
  22122. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  22123. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  22124. (defun org-kill-line (&optional _arg)
  22125. "Kill line, to tags or end of line."
  22126. (interactive)
  22127. (cond
  22128. ((or (not org-special-ctrl-k)
  22129. (bolp)
  22130. (not (org-at-heading-p)))
  22131. (when (and (get-char-property (min (point-max) (point-at-eol)) 'invisible)
  22132. org-ctrl-k-protect-subtree
  22133. (or (eq org-ctrl-k-protect-subtree 'error)
  22134. (not (y-or-n-p "Kill hidden subtree along with headline? "))))
  22135. (user-error "C-k aborted as it would kill a hidden subtree"))
  22136. (call-interactively
  22137. (if (org-bound-and-true-p visual-line-mode) 'kill-visual-line 'kill-line)))
  22138. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$"))
  22139. (kill-region (point) (match-beginning 1))
  22140. (org-set-tags nil t))
  22141. (t (kill-region (point) (point-at-eol)))))
  22142. (define-key org-mode-map "\C-k" 'org-kill-line)
  22143. (defun org-yank (&optional arg)
  22144. "Yank. If the kill is a subtree, treat it specially.
  22145. This command will look at the current kill and check if is a single
  22146. subtree, or a series of subtrees[1]. If it passes the test, and if the
  22147. cursor is at the beginning of a line or after the stars of a currently
  22148. empty headline, then the yank is handled specially. How exactly depends
  22149. on the value of the following variables.
  22150. `org-yank-folded-subtrees'
  22151. By default, this variable is non-nil, which results in subtree(s)
  22152. being folded after insertion, but only if doing so would now
  22153. swallow text after the yanked text.
  22154. `org-yank-adjusted-subtrees'
  22155. When non-nil (the default value is nil), the subtree will be
  22156. promoted or demoted in order to fit into the local outline tree
  22157. structure, which means that the level will be adjusted so that it
  22158. becomes the smaller one of the two *visible* surrounding headings.
  22159. Any prefix to this command will cause `yank' to be called directly with
  22160. no special treatment. In particular, a simple \\[universal-argument] prefix \
  22161. will just
  22162. plainly yank the text as it is.
  22163. \[1] The test checks if the first non-white line is a heading
  22164. and if there are no other headings with fewer stars."
  22165. (interactive "P")
  22166. (org-yank-generic 'yank arg))
  22167. (defun org-yank-generic (command arg)
  22168. "Perform some yank-like command.
  22169. This function implements the behavior described in the `org-yank'
  22170. documentation. However, it has been generalized to work for any
  22171. interactive command with similar behavior."
  22172. ;; pretend to be command COMMAND
  22173. (setq this-command command)
  22174. (if arg
  22175. (call-interactively command)
  22176. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  22177. (and (org-kill-is-subtree-p)
  22178. (or (bolp)
  22179. (and (looking-at "[ \t]*$")
  22180. (string-match
  22181. "\\`\\*+\\'"
  22182. (buffer-substring (point-at-bol) (point)))))))
  22183. swallowp)
  22184. (cond
  22185. ((and subtreep org-yank-folded-subtrees)
  22186. (let ((beg (point))
  22187. end)
  22188. (if (and subtreep org-yank-adjusted-subtrees)
  22189. (org-paste-subtree nil nil 'for-yank)
  22190. (call-interactively command))
  22191. (setq end (point))
  22192. (goto-char beg)
  22193. (when (and (bolp) subtreep
  22194. (not (setq swallowp
  22195. (org-yank-folding-would-swallow-text beg end))))
  22196. (org-with-limited-levels
  22197. (or (looking-at org-outline-regexp)
  22198. (re-search-forward org-outline-regexp-bol end t))
  22199. (while (and (< (point) end) (looking-at org-outline-regexp))
  22200. (outline-hide-subtree)
  22201. (org-cycle-show-empty-lines 'folded)
  22202. (condition-case nil
  22203. (outline-forward-same-level 1)
  22204. (error (goto-char end))))))
  22205. (when swallowp
  22206. (message
  22207. "Inserted text not folded because that would swallow text"))
  22208. (goto-char end)
  22209. (skip-chars-forward " \t\n\r")
  22210. (beginning-of-line 1)
  22211. (push-mark beg 'nomsg)))
  22212. ((and subtreep org-yank-adjusted-subtrees)
  22213. (let ((beg (point-at-bol)))
  22214. (org-paste-subtree nil nil 'for-yank)
  22215. (push-mark beg 'nomsg)))
  22216. (t
  22217. (call-interactively command))))))
  22218. (defun org-yank-folding-would-swallow-text (beg end)
  22219. "Would hide-subtree at BEG swallow any text after END?"
  22220. (let (level)
  22221. (org-with-limited-levels
  22222. (save-excursion
  22223. (goto-char beg)
  22224. (when (or (looking-at org-outline-regexp)
  22225. (re-search-forward org-outline-regexp-bol end t))
  22226. (setq level (org-outline-level)))
  22227. (goto-char end)
  22228. (skip-chars-forward " \t\r\n\v\f")
  22229. (not (or (eobp)
  22230. (and (bolp) (looking-at-p org-outline-regexp)
  22231. (<= (org-outline-level) level))))))))
  22232. (define-key org-mode-map "\C-y" 'org-yank)
  22233. (defun org-truely-invisible-p ()
  22234. "Check if point is at a character currently not visible.
  22235. This version does not only check the character property, but also
  22236. `visible-mode'."
  22237. ;; Early versions of noutline don't have `outline-invisible-p'.
  22238. (unless (org-bound-and-true-p visible-mode)
  22239. (outline-invisible-p)))
  22240. (defun org-invisible-p2 ()
  22241. "Check if point is at a character currently not visible."
  22242. (save-excursion
  22243. (when (and (eolp) (not (bobp))) (backward-char 1))
  22244. ;; Early versions of noutline don't have `outline-invisible-p'.
  22245. (outline-invisible-p)))
  22246. (defun org-back-to-heading (&optional invisible-ok)
  22247. "Call `outline-back-to-heading', but provide a better error message."
  22248. (condition-case nil
  22249. (outline-back-to-heading invisible-ok)
  22250. (error (error "Before first headline at position %d in buffer %s"
  22251. (point) (current-buffer)))))
  22252. (defun org-before-first-heading-p ()
  22253. "Before first heading?"
  22254. (save-excursion
  22255. (end-of-line)
  22256. (null (re-search-backward org-outline-regexp-bol nil t))))
  22257. (defun org-at-heading-p (&optional ignored)
  22258. (outline-on-heading-p t))
  22259. ;;; Though the function was obsoleted in 7.8.03, the byte-compiler
  22260. ;;; warning was only added in Org 9.0, which should be taken into
  22261. ;;; account when deciding when to remove the alias.
  22262. (define-obsolete-function-alias 'org-on-heading-p 'org-at-heading-p "Org 7.8.03")
  22263. (defun org-in-commented-heading-p (&optional no-inheritance)
  22264. "Non-nil if point is under a commented heading.
  22265. This function also checks ancestors of the current headline,
  22266. unless optional argument NO-INHERITANCE is non-nil."
  22267. (cond
  22268. ((org-before-first-heading-p) nil)
  22269. ((let ((headline (nth 4 (org-heading-components))))
  22270. (and headline
  22271. (let ((case-fold-search nil))
  22272. (org-string-match-p (concat "^" org-comment-string "\\(?: \\|$\\)")
  22273. headline)))))
  22274. (no-inheritance nil)
  22275. (t
  22276. (save-excursion (and (org-up-heading-safe) (org-in-commented-heading-p))))))
  22277. (defun org-at-comment-p nil
  22278. "Is cursor in a commented line?"
  22279. (save-excursion
  22280. (save-match-data
  22281. (beginning-of-line)
  22282. (looking-at "^[ \t]*# "))))
  22283. (defun org-at-drawer-p nil
  22284. "Is cursor at a drawer keyword?"
  22285. (save-excursion
  22286. (move-beginning-of-line 1)
  22287. (looking-at org-drawer-regexp)))
  22288. (defun org-at-block-p nil
  22289. "Is cursor at a block keyword?"
  22290. (save-excursion
  22291. (move-beginning-of-line 1)
  22292. (looking-at org-block-regexp)))
  22293. (defun org-point-at-end-of-empty-headline ()
  22294. "If point is at the end of an empty headline, return t, else nil.
  22295. If the heading only contains a TODO keyword, it is still still considered
  22296. empty."
  22297. (and (looking-at "[ \t]*$")
  22298. (when org-todo-line-regexp
  22299. (save-excursion
  22300. (beginning-of-line 1)
  22301. (let ((case-fold-search nil))
  22302. (looking-at org-todo-line-regexp)
  22303. (string= (match-string 3) ""))))))
  22304. (defun org-at-heading-or-item-p ()
  22305. (or (org-at-heading-p) (org-at-item-p)))
  22306. (defun org-at-target-p ()
  22307. (or (org-in-regexp org-radio-target-regexp)
  22308. (org-in-regexp org-target-regexp)))
  22309. ;; Compatibility alias with Org versions < 7.8.03
  22310. (defalias 'org-on-target-p 'org-at-target-p)
  22311. (defun org-up-heading-all (arg)
  22312. "Move to the heading line of which the present line is a subheading.
  22313. This function considers both visible and invisible heading lines.
  22314. With argument, move up ARG levels."
  22315. (if (fboundp 'outline-up-heading-all)
  22316. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  22317. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  22318. (defun org-up-heading-safe ()
  22319. "Move to the heading line of which the present line is a subheading.
  22320. This version will not throw an error. It will return the level of the
  22321. headline found, or nil if no higher level is found.
  22322. Also, this function will be a lot faster than `outline-up-heading',
  22323. because it relies on stars being the outline starters. This can really
  22324. make a significant difference in outlines with very many siblings."
  22325. (when (ignore-errors (org-back-to-heading t))
  22326. (let ((level-up (1- (funcall outline-level))))
  22327. (and (> level-up 0)
  22328. (re-search-backward (format "^\\*\\{1,%d\\} " level-up) nil t)
  22329. (funcall outline-level)))))
  22330. (defun org-first-sibling-p ()
  22331. "Is this heading the first child of its parents?"
  22332. (interactive)
  22333. (let ((re org-outline-regexp-bol)
  22334. level l)
  22335. (unless (org-at-heading-p t)
  22336. (user-error "Not at a heading"))
  22337. (setq level (funcall outline-level))
  22338. (save-excursion
  22339. (if (not (re-search-backward re nil t))
  22340. t
  22341. (setq l (funcall outline-level))
  22342. (< l level)))))
  22343. (defun org-goto-sibling (&optional previous)
  22344. "Goto the next sibling, even if it is invisible.
  22345. When PREVIOUS is set, go to the previous sibling instead. Returns t
  22346. when a sibling was found. When none is found, return nil and don't
  22347. move point."
  22348. (let ((fun (if previous 're-search-backward 're-search-forward))
  22349. (pos (point))
  22350. (re org-outline-regexp-bol)
  22351. level l)
  22352. (when (ignore-errors (org-back-to-heading t))
  22353. (setq level (funcall outline-level))
  22354. (catch 'exit
  22355. (or previous (forward-char 1))
  22356. (while (funcall fun re nil t)
  22357. (setq l (funcall outline-level))
  22358. (when (< l level) (goto-char pos) (throw 'exit nil))
  22359. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  22360. (goto-char pos)
  22361. nil))))
  22362. (defun org-show-siblings ()
  22363. "Show all siblings of the current headline."
  22364. (save-excursion
  22365. (while (org-goto-sibling) (org-flag-heading nil)))
  22366. (save-excursion
  22367. (while (org-goto-sibling 'previous)
  22368. (org-flag-heading nil))))
  22369. (defun org-goto-first-child ()
  22370. "Goto the first child, even if it is invisible.
  22371. Return t when a child was found. Otherwise don't move point and
  22372. return nil."
  22373. (let (level (pos (point)) (re org-outline-regexp-bol))
  22374. (when (ignore-errors (org-back-to-heading t))
  22375. (setq level (outline-level))
  22376. (forward-char 1)
  22377. (if (and (re-search-forward re nil t) (> (outline-level) level))
  22378. (progn (goto-char (match-beginning 0)) t)
  22379. (goto-char pos) nil))))
  22380. (defun org-show-hidden-entry ()
  22381. "Show an entry where even the heading is hidden."
  22382. (save-excursion
  22383. (org-show-entry)))
  22384. (defun org-flag-heading (flag &optional entry)
  22385. "Flag the current heading. FLAG non-nil means make invisible.
  22386. When ENTRY is non-nil, show the entire entry."
  22387. (save-excursion
  22388. (org-back-to-heading t)
  22389. ;; Check if we should show the entire entry
  22390. (if entry
  22391. (progn
  22392. (org-show-entry)
  22393. (save-excursion
  22394. (and (outline-next-heading)
  22395. (org-flag-heading nil))))
  22396. (outline-flag-region (max (point-min) (1- (point)))
  22397. (save-excursion (outline-end-of-heading) (point))
  22398. flag))))
  22399. (defun org-get-next-sibling ()
  22400. "Move to next heading of the same level, and return point.
  22401. If there is no such heading, return nil.
  22402. This is like outline-next-sibling, but invisible headings are ok."
  22403. (let ((level (funcall outline-level)))
  22404. (outline-next-heading)
  22405. (while (and (not (eobp)) (> (funcall outline-level) level))
  22406. (outline-next-heading))
  22407. (unless (or (eobp) (< (funcall outline-level) level))
  22408. (point))))
  22409. (defun org-get-last-sibling ()
  22410. "Move to previous heading of the same level, and return point.
  22411. If there is no such heading, return nil."
  22412. (let ((opoint (point))
  22413. (level (funcall outline-level)))
  22414. (outline-previous-heading)
  22415. (when (and (/= (point) opoint) (outline-on-heading-p t))
  22416. (while (and (> (funcall outline-level) level)
  22417. (not (bobp)))
  22418. (outline-previous-heading))
  22419. (unless (< (funcall outline-level) level)
  22420. (point)))))
  22421. (defun org-end-of-subtree (&optional invisible-ok to-heading)
  22422. "Goto to the end of a subtree."
  22423. ;; This contains an exact copy of the original function, but it uses
  22424. ;; `org-back-to-heading', to make it work also in invisible
  22425. ;; trees. And is uses an invisible-ok argument.
  22426. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  22427. ;; Furthermore, when used inside Org, finding the end of a large subtree
  22428. ;; with many children and grandchildren etc, this can be much faster
  22429. ;; than the outline version.
  22430. (org-back-to-heading invisible-ok)
  22431. (let ((first t)
  22432. (level (funcall outline-level)))
  22433. (if (and (derived-mode-p 'org-mode) (< level 1000))
  22434. ;; A true heading (not a plain list item), in Org-mode
  22435. ;; This means we can easily find the end by looking
  22436. ;; only for the right number of stars. Using a regexp to do
  22437. ;; this is so much faster than using a Lisp loop.
  22438. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  22439. (forward-char 1)
  22440. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  22441. ;; something else, do it the slow way
  22442. (while (and (not (eobp))
  22443. (or first (> (funcall outline-level) level)))
  22444. (setq first nil)
  22445. (outline-next-heading)))
  22446. (unless to-heading
  22447. (when (memq (preceding-char) '(?\n ?\^M))
  22448. ;; Go to end of line before heading
  22449. (forward-char -1)
  22450. (when (memq (preceding-char) '(?\n ?\^M))
  22451. ;; leave blank line before heading
  22452. (forward-char -1)))))
  22453. (point))
  22454. (defun org-end-of-meta-data (&optional full)
  22455. "Skip planning line and properties drawer in current entry.
  22456. When optional argument FULL is non-nil, also skip empty lines,
  22457. clocking lines and regular drawers at the beginning of the
  22458. entry."
  22459. (org-back-to-heading t)
  22460. (forward-line)
  22461. (when (org-looking-at-p org-planning-line-re) (forward-line))
  22462. (when (looking-at org-property-drawer-re)
  22463. (goto-char (match-end 0))
  22464. (forward-line))
  22465. (when (and full (not (org-at-heading-p)))
  22466. (catch 'exit
  22467. (let ((end (save-excursion (outline-next-heading) (point)))
  22468. (re (concat "[ \t]*$" "\\|" org-clock-line-re)))
  22469. (while (not (eobp))
  22470. (cond ((org-looking-at-p org-drawer-regexp)
  22471. (if (re-search-forward "^[ \t]*:END:[ \t]*$" end t)
  22472. (forward-line)
  22473. (throw 'exit t)))
  22474. ((org-looking-at-p re) (forward-line))
  22475. (t (throw 'exit t))))))))
  22476. (defun org-forward-heading-same-level (arg &optional invisible-ok)
  22477. "Move forward to the ARG'th subheading at same level as this one.
  22478. Stop at the first and last subheadings of a superior heading.
  22479. Normally this only looks at visible headings, but when INVISIBLE-OK is
  22480. non-nil it will also look at invisible ones."
  22481. (interactive "p")
  22482. (if (not (ignore-errors (org-back-to-heading invisible-ok)))
  22483. (if (and arg (< arg 0))
  22484. (goto-char (point-min))
  22485. (outline-next-heading))
  22486. (org-at-heading-p)
  22487. (let ((level (- (match-end 0) (match-beginning 0) 1))
  22488. (f (if (and arg (< arg 0))
  22489. 're-search-backward
  22490. 're-search-forward))
  22491. (count (if arg (abs arg) 1))
  22492. (result (point)))
  22493. (while (and (prog1 (> count 0)
  22494. (forward-char (if (and arg (< arg 0)) -1 1)))
  22495. (funcall f org-outline-regexp-bol nil 'move))
  22496. (let ((l (- (match-end 0) (match-beginning 0) 1)))
  22497. (cond ((< l level) (setq count 0))
  22498. ((and (= l level)
  22499. (or invisible-ok
  22500. (progn
  22501. (goto-char (line-beginning-position))
  22502. (not (outline-invisible-p)))))
  22503. (setq count (1- count))
  22504. (when (eq l level)
  22505. (setq result (point)))))))
  22506. (goto-char result))
  22507. (beginning-of-line 1)))
  22508. (defun org-backward-heading-same-level (arg &optional invisible-ok)
  22509. "Move backward to the ARG'th subheading at same level as this one.
  22510. Stop at the first and last subheadings of a superior heading."
  22511. (interactive "p")
  22512. (org-forward-heading-same-level (if arg (- arg) -1) invisible-ok))
  22513. (defun org-next-visible-heading (arg)
  22514. "Move to the next visible heading.
  22515. This function wraps `outline-next-visible-heading' with
  22516. `org-with-limited-levels' in order to skip over inline tasks and
  22517. respect customization of `org-odd-levels-only'."
  22518. (interactive "p")
  22519. (org-with-limited-levels
  22520. (outline-next-visible-heading arg)))
  22521. (defun org-previous-visible-heading (arg)
  22522. "Move to the previous visible heading.
  22523. This function wraps `outline-previous-visible-heading' with
  22524. `org-with-limited-levels' in order to skip over inline tasks and
  22525. respect customization of `org-odd-levels-only'."
  22526. (interactive "p")
  22527. (org-with-limited-levels
  22528. (outline-previous-visible-heading arg)))
  22529. (defun org-next-block (arg &optional backward block-regexp)
  22530. "Jump to the next block.
  22531. With a prefix argument ARG, jump forward ARG many blocks.
  22532. When BACKWARD is non-nil, jump to the previous block.
  22533. When BLOCK-REGEXP is non-nil, use this regexp to find blocks.
  22534. Match data is set according to this regexp when the function
  22535. returns.
  22536. Return point at beginning of the opening line of found block.
  22537. Throw an error if no block is found."
  22538. (interactive "p")
  22539. (let ((re (or block-regexp "^[ \t]*#\\+BEGIN"))
  22540. (case-fold-search t)
  22541. (search-fn (if backward #'re-search-backward #'re-search-forward))
  22542. (count (or arg 1))
  22543. (origin (point))
  22544. last-element)
  22545. (if backward (beginning-of-line) (end-of-line))
  22546. (while (and (> count 0) (funcall search-fn re nil t))
  22547. (let ((element (save-excursion
  22548. (goto-char (match-beginning 0))
  22549. (save-match-data (org-element-at-point)))))
  22550. (when (and (memq (org-element-type element)
  22551. '(center-block comment-block dynamic-block
  22552. example-block export-block quote-block
  22553. special-block src-block verse-block))
  22554. (<= (match-beginning 0)
  22555. (org-element-property :post-affiliated element)))
  22556. (setq last-element element)
  22557. (cl-decf count))))
  22558. (if (= count 0)
  22559. (prog1 (goto-char (org-element-property :post-affiliated last-element))
  22560. (save-match-data (org-show-context)))
  22561. (goto-char origin)
  22562. (user-error "No %s code blocks" (if backward "previous" "further")))))
  22563. (defun org-previous-block (arg &optional block-regexp)
  22564. "Jump to the previous block.
  22565. With a prefix argument ARG, jump backward ARG many source blocks.
  22566. When BLOCK-REGEXP is non-nil, use this regexp to find blocks."
  22567. (interactive "p")
  22568. (org-next-block arg t block-regexp))
  22569. (defun org-forward-paragraph ()
  22570. "Move forward to beginning of next paragraph or equivalent.
  22571. The function moves point to the beginning of the next visible
  22572. structural element, which can be a paragraph, a table, a list
  22573. item, etc. It also provides some special moves for convenience:
  22574. - On an affiliated keyword, jump to the beginning of the
  22575. relative element.
  22576. - On an item or a footnote definition, move to the second
  22577. element inside, if any.
  22578. - On a table or a property drawer, jump after it.
  22579. - On a verse or source block, stop after blank lines."
  22580. (interactive)
  22581. (when (eobp) (user-error "Cannot move further down"))
  22582. (let* ((deactivate-mark nil)
  22583. (element (org-element-at-point))
  22584. (type (org-element-type element))
  22585. (post-affiliated (org-element-property :post-affiliated element))
  22586. (contents-begin (org-element-property :contents-begin element))
  22587. (contents-end (org-element-property :contents-end element))
  22588. (end (let ((end (org-element-property :end element)) (parent element))
  22589. (while (and (setq parent (org-element-property :parent parent))
  22590. (= (org-element-property :contents-end parent) end))
  22591. (setq end (org-element-property :end parent)))
  22592. end)))
  22593. (cond ((not element)
  22594. (skip-chars-forward " \r\t\n")
  22595. (or (eobp) (beginning-of-line)))
  22596. ;; On affiliated keywords, move to element's beginning.
  22597. ((< (point) post-affiliated)
  22598. (goto-char post-affiliated))
  22599. ;; At a table row, move to the end of the table. Similarly,
  22600. ;; at a node property, move to the end of the property
  22601. ;; drawer.
  22602. ((memq type '(node-property table-row))
  22603. (goto-char (org-element-property
  22604. :end (org-element-property :parent element))))
  22605. ((memq type '(property-drawer table)) (goto-char end))
  22606. ;; Consider blank lines as separators in verse and source
  22607. ;; blocks to ease editing.
  22608. ((memq type '(src-block verse-block))
  22609. (when (eq type 'src-block)
  22610. (setq contents-end
  22611. (save-excursion (goto-char end)
  22612. (skip-chars-backward " \r\t\n")
  22613. (line-beginning-position))))
  22614. (beginning-of-line)
  22615. (when (looking-at "[ \t]*$") (skip-chars-forward " \r\t\n"))
  22616. (if (not (re-search-forward "^[ \t]*$" contents-end t))
  22617. (goto-char end)
  22618. (skip-chars-forward " \r\t\n")
  22619. (if (= (point) contents-end) (goto-char end)
  22620. (beginning-of-line))))
  22621. ;; With no contents, just skip element.
  22622. ((not contents-begin) (goto-char end))
  22623. ;; If contents are invisible, skip the element altogether.
  22624. ((outline-invisible-p (line-end-position))
  22625. (cl-case type
  22626. (headline
  22627. (org-with-limited-levels (outline-next-visible-heading 1)))
  22628. ;; At a plain list, make sure we move to the next item
  22629. ;; instead of skipping the whole list.
  22630. (plain-list (forward-char)
  22631. (org-forward-paragraph))
  22632. (otherwise (goto-char end))))
  22633. ((>= (point) contents-end) (goto-char end))
  22634. ((>= (point) contents-begin)
  22635. ;; This can only happen on paragraphs and plain lists.
  22636. (cl-case type
  22637. (paragraph (goto-char end))
  22638. ;; At a plain list, try to move to second element in
  22639. ;; first item, if possible.
  22640. (plain-list (end-of-line)
  22641. (org-forward-paragraph))))
  22642. ;; When contents start on the middle of a line (e.g. in
  22643. ;; items and footnote definitions), try to reach first
  22644. ;; element starting after current line.
  22645. ((> (line-end-position) contents-begin)
  22646. (end-of-line)
  22647. (org-forward-paragraph))
  22648. (t (goto-char contents-begin)))))
  22649. (defun org-backward-paragraph ()
  22650. "Move backward to start of previous paragraph or equivalent.
  22651. The function moves point to the beginning of the current
  22652. structural element, which can be a paragraph, a table, a list
  22653. item, etc., or to the beginning of the previous visible one if
  22654. point is already there. It also provides some special moves for
  22655. convenience:
  22656. - On an affiliated keyword, jump to the first one.
  22657. - On a table or a property drawer, move to its beginning.
  22658. - On a verse or source block, stop before blank lines."
  22659. (interactive)
  22660. (when (bobp) (user-error "Cannot move further up"))
  22661. (let* ((deactivate-mark nil)
  22662. (element (org-element-at-point))
  22663. (type (org-element-type element))
  22664. (contents-begin (org-element-property :contents-begin element))
  22665. (contents-end (org-element-property :contents-end element))
  22666. (post-affiliated (org-element-property :post-affiliated element))
  22667. (begin (org-element-property :begin element)))
  22668. (cond
  22669. ((not element) (goto-char (point-min)))
  22670. ((= (point) begin)
  22671. (backward-char)
  22672. (org-backward-paragraph))
  22673. ((<= (point) post-affiliated) (goto-char begin))
  22674. ((memq type '(node-property table-row))
  22675. (goto-char (org-element-property
  22676. :post-affiliated (org-element-property :parent element))))
  22677. ((memq type '(property-drawer table)) (goto-char begin))
  22678. ((memq type '(src-block verse-block))
  22679. (when (eq type 'src-block)
  22680. (setq contents-begin
  22681. (save-excursion (goto-char begin) (forward-line) (point))))
  22682. (if (= (point) contents-begin) (goto-char post-affiliated)
  22683. ;; Inside a verse block, see blank lines as paragraph
  22684. ;; separators.
  22685. (let ((origin (point)))
  22686. (skip-chars-backward " \r\t\n" contents-begin)
  22687. (when (re-search-backward "^[ \t]*$" contents-begin 'move)
  22688. (skip-chars-forward " \r\t\n" origin)
  22689. (if (= (point) origin) (goto-char contents-begin)
  22690. (beginning-of-line))))))
  22691. ((not contents-begin) (goto-char (or post-affiliated begin)))
  22692. ((eq type 'paragraph)
  22693. (goto-char contents-begin)
  22694. ;; When at first paragraph in an item or a footnote definition,
  22695. ;; move directly to beginning of line.
  22696. (let ((parent-contents
  22697. (org-element-property
  22698. :contents-begin (org-element-property :parent element))))
  22699. (when (and parent-contents (= parent-contents contents-begin))
  22700. (beginning-of-line))))
  22701. ;; At the end of a greater element, move to the beginning of the
  22702. ;; last element within.
  22703. ((>= (point) contents-end)
  22704. (goto-char (1- contents-end))
  22705. (org-backward-paragraph))
  22706. (t (goto-char (or post-affiliated begin))))
  22707. ;; Ensure we never leave point invisible.
  22708. (when (outline-invisible-p (point)) (beginning-of-visual-line))))
  22709. (defun org-forward-element ()
  22710. "Move forward by one element.
  22711. Move to the next element at the same level, when possible."
  22712. (interactive)
  22713. (cond ((eobp) (user-error "Cannot move further down"))
  22714. ((org-with-limited-levels (org-at-heading-p))
  22715. (let ((origin (point)))
  22716. (goto-char (org-end-of-subtree nil t))
  22717. (unless (org-with-limited-levels (org-at-heading-p))
  22718. (goto-char origin)
  22719. (user-error "Cannot move further down"))))
  22720. (t
  22721. (let* ((elem (org-element-at-point))
  22722. (end (org-element-property :end elem))
  22723. (parent (org-element-property :parent elem)))
  22724. (cond ((and parent (= (org-element-property :contents-end parent) end))
  22725. (goto-char (org-element-property :end parent)))
  22726. ((integer-or-marker-p end) (goto-char end))
  22727. (t (message "No element at point")))))))
  22728. (defun org-backward-element ()
  22729. "Move backward by one element.
  22730. Move to the previous element at the same level, when possible."
  22731. (interactive)
  22732. (cond ((bobp) (user-error "Cannot move further up"))
  22733. ((org-with-limited-levels (org-at-heading-p))
  22734. ;; At a headline, move to the previous one, if any, or stay
  22735. ;; here.
  22736. (let ((origin (point)))
  22737. (org-with-limited-levels (org-backward-heading-same-level 1))
  22738. ;; When current headline has no sibling above, move to its
  22739. ;; parent.
  22740. (when (= (point) origin)
  22741. (or (org-with-limited-levels (org-up-heading-safe))
  22742. (progn (goto-char origin)
  22743. (user-error "Cannot move further up"))))))
  22744. (t
  22745. (let* ((elem (org-element-at-point))
  22746. (beg (org-element-property :begin elem)))
  22747. (cond
  22748. ;; Move to beginning of current element if point isn't
  22749. ;; there already.
  22750. ((null beg) (message "No element at point"))
  22751. ((/= (point) beg) (goto-char beg))
  22752. (t (goto-char beg)
  22753. (skip-chars-backward " \r\t\n")
  22754. (unless (bobp)
  22755. (let ((prev (org-element-at-point)))
  22756. (goto-char (org-element-property :begin prev))
  22757. (while (and (setq prev (org-element-property :parent prev))
  22758. (<= (org-element-property :end prev) beg))
  22759. (goto-char (org-element-property :begin prev)))))))))))
  22760. (defun org-up-element ()
  22761. "Move to upper element."
  22762. (interactive)
  22763. (if (org-with-limited-levels (org-at-heading-p))
  22764. (unless (org-up-heading-safe) (user-error "No surrounding element"))
  22765. (let* ((elem (org-element-at-point))
  22766. (parent (org-element-property :parent elem)))
  22767. (if parent (goto-char (org-element-property :begin parent))
  22768. (if (org-with-limited-levels (org-before-first-heading-p))
  22769. (user-error "No surrounding element")
  22770. (org-with-limited-levels (org-back-to-heading)))))))
  22771. (defvar org-element-greater-elements)
  22772. (defun org-down-element ()
  22773. "Move to inner element."
  22774. (interactive)
  22775. (let ((element (org-element-at-point)))
  22776. (cond
  22777. ((memq (org-element-type element) '(plain-list table))
  22778. (goto-char (org-element-property :contents-begin element))
  22779. (forward-char))
  22780. ((memq (org-element-type element) org-element-greater-elements)
  22781. ;; If contents are hidden, first disclose them.
  22782. (when (outline-invisible-p (line-end-position)) (org-cycle))
  22783. (goto-char (or (org-element-property :contents-begin element)
  22784. (user-error "No content for this element"))))
  22785. (t (user-error "No inner element")))))
  22786. (defun org-drag-element-backward ()
  22787. "Move backward element at point."
  22788. (interactive)
  22789. (if (org-with-limited-levels (org-at-heading-p)) (org-move-subtree-up)
  22790. (let* ((elem (org-element-at-point))
  22791. (prev-elem
  22792. (save-excursion
  22793. (goto-char (org-element-property :begin elem))
  22794. (skip-chars-backward " \r\t\n")
  22795. (unless (bobp)
  22796. (let* ((beg (org-element-property :begin elem))
  22797. (prev (org-element-at-point))
  22798. (up prev))
  22799. (while (and (setq up (org-element-property :parent up))
  22800. (<= (org-element-property :end up) beg))
  22801. (setq prev up))
  22802. prev)))))
  22803. ;; Error out if no previous element or previous element is
  22804. ;; a parent of the current one.
  22805. (if (or (not prev-elem) (org-element-nested-p elem prev-elem))
  22806. (user-error "Cannot drag element backward")
  22807. (let ((pos (point)))
  22808. (org-element-swap-A-B prev-elem elem)
  22809. (goto-char (+ (org-element-property :begin prev-elem)
  22810. (- pos (org-element-property :begin elem)))))))))
  22811. (defun org-drag-element-forward ()
  22812. "Move forward element at point."
  22813. (interactive)
  22814. (let* ((pos (point))
  22815. (elem (org-element-at-point)))
  22816. (when (= (point-max) (org-element-property :end elem))
  22817. (user-error "Cannot drag element forward"))
  22818. (goto-char (org-element-property :end elem))
  22819. (let ((next-elem (org-element-at-point)))
  22820. (when (or (org-element-nested-p elem next-elem)
  22821. (and (eq (org-element-type next-elem) 'headline)
  22822. (not (eq (org-element-type elem) 'headline))))
  22823. (goto-char pos)
  22824. (user-error "Cannot drag element forward"))
  22825. ;; Compute new position of point: it's shifted by NEXT-ELEM
  22826. ;; body's length (without final blanks) and by the length of
  22827. ;; blanks between ELEM and NEXT-ELEM.
  22828. (let ((size-next (- (save-excursion
  22829. (goto-char (org-element-property :end next-elem))
  22830. (skip-chars-backward " \r\t\n")
  22831. (forward-line)
  22832. ;; Small correction if buffer doesn't end
  22833. ;; with a newline character.
  22834. (if (and (eolp) (not (bolp))) (1+ (point)) (point)))
  22835. (org-element-property :begin next-elem)))
  22836. (size-blank (- (org-element-property :end elem)
  22837. (save-excursion
  22838. (goto-char (org-element-property :end elem))
  22839. (skip-chars-backward " \r\t\n")
  22840. (forward-line)
  22841. (point)))))
  22842. (org-element-swap-A-B elem next-elem)
  22843. (goto-char (+ pos size-next size-blank))))))
  22844. (defun org-drag-line-forward (arg)
  22845. "Drag the line at point ARG lines forward."
  22846. (interactive "p")
  22847. (dotimes (_ (abs arg))
  22848. (let ((c (current-column)))
  22849. (if (< 0 arg)
  22850. (progn
  22851. (beginning-of-line 2)
  22852. (transpose-lines 1)
  22853. (beginning-of-line 0))
  22854. (transpose-lines 1)
  22855. (beginning-of-line -1))
  22856. (org-move-to-column c))))
  22857. (defun org-drag-line-backward (arg)
  22858. "Drag the line at point ARG lines backward."
  22859. (interactive "p")
  22860. (org-drag-line-forward (- arg)))
  22861. (defun org-mark-element ()
  22862. "Put point at beginning of this element, mark at end.
  22863. Interactively, if this command is repeated or (in Transient Mark
  22864. mode) if the mark is active, it marks the next element after the
  22865. ones already marked."
  22866. (interactive)
  22867. (let (deactivate-mark)
  22868. (if (and (org-called-interactively-p 'any)
  22869. (or (and (eq last-command this-command) (mark t))
  22870. (and transient-mark-mode mark-active)))
  22871. (set-mark
  22872. (save-excursion
  22873. (goto-char (mark))
  22874. (goto-char (org-element-property :end (org-element-at-point)))))
  22875. (let ((element (org-element-at-point)))
  22876. (end-of-line)
  22877. (push-mark (org-element-property :end element) t t)
  22878. (goto-char (org-element-property :begin element))))))
  22879. (defun org-narrow-to-element ()
  22880. "Narrow buffer to current element."
  22881. (interactive)
  22882. (let ((elem (org-element-at-point)))
  22883. (cond
  22884. ((eq (car elem) 'headline)
  22885. (narrow-to-region
  22886. (org-element-property :begin elem)
  22887. (org-element-property :end elem)))
  22888. ((memq (car elem) org-element-greater-elements)
  22889. (narrow-to-region
  22890. (org-element-property :contents-begin elem)
  22891. (org-element-property :contents-end elem)))
  22892. (t
  22893. (narrow-to-region
  22894. (org-element-property :begin elem)
  22895. (org-element-property :end elem))))))
  22896. (defun org-transpose-element ()
  22897. "Transpose current and previous elements, keeping blank lines between.
  22898. Point is moved after both elements."
  22899. (interactive)
  22900. (org-skip-whitespace)
  22901. (let ((end (org-element-property :end (org-element-at-point))))
  22902. (org-drag-element-backward)
  22903. (goto-char end)))
  22904. (defun org-unindent-buffer ()
  22905. "Un-indent the visible part of the buffer.
  22906. Relative indentation (between items, inside blocks, etc.) isn't
  22907. modified."
  22908. (interactive)
  22909. (unless (eq major-mode 'org-mode)
  22910. (user-error "Cannot un-indent a buffer not in Org mode"))
  22911. (letrec ((parse-tree (org-element-parse-buffer 'greater-element))
  22912. (unindent-tree
  22913. (lambda (contents)
  22914. (dolist (element (reverse contents))
  22915. (if (memq (org-element-type element) '(headline section))
  22916. (funcall unindent-tree (org-element-contents element))
  22917. (save-excursion
  22918. (save-restriction
  22919. (narrow-to-region
  22920. (org-element-property :begin element)
  22921. (org-element-property :end element))
  22922. (org-do-remove-indentation))))))))
  22923. (funcall unindent-tree (org-element-contents parse-tree))))
  22924. (defun org-show-children (&optional level)
  22925. "Show all direct subheadings of this heading.
  22926. Prefix arg LEVEL is how many levels below the current level
  22927. should be shown. Default is enough to cause the following
  22928. heading to appear."
  22929. (interactive "p")
  22930. ;; If `orgstruct-mode' is active, use the slower version.
  22931. (if orgstruct-mode (call-interactively #'outline-show-children)
  22932. (save-excursion
  22933. (org-back-to-heading t)
  22934. (let* ((current-level (funcall outline-level))
  22935. (max-level (org-get-valid-level
  22936. current-level
  22937. (if level (prefix-numeric-value level) 1)))
  22938. (end (save-excursion (org-end-of-subtree t t)))
  22939. (regexp-fmt "^\\*\\{%d,%s\\}\\(?: \\|$\\)")
  22940. (past-first-child nil)
  22941. ;; Make sure to skip inlinetasks.
  22942. (re (format regexp-fmt
  22943. current-level
  22944. (cond
  22945. ((not (featurep 'org-inlinetask)) "")
  22946. (org-odd-levels-only (- (* 2 org-inlinetask-min-level)
  22947. 3))
  22948. (t (1- org-inlinetask-min-level))))))
  22949. ;; Display parent heading.
  22950. (outline-flag-region (line-end-position 0) (line-end-position) nil)
  22951. (forward-line)
  22952. ;; Display children. First child may be deeper than expected
  22953. ;; MAX-LEVEL. Since we want to display it anyway, adjust
  22954. ;; MAX-LEVEL accordingly.
  22955. (while (re-search-forward re end t)
  22956. (unless past-first-child
  22957. (setq re (format regexp-fmt
  22958. current-level
  22959. (max (funcall outline-level) max-level)))
  22960. (setq past-first-child t))
  22961. (outline-flag-region
  22962. (line-end-position 0) (line-end-position) nil))))))
  22963. (defun org-show-subtree ()
  22964. "Show everything after this heading at deeper levels."
  22965. (interactive)
  22966. (outline-flag-region
  22967. (point)
  22968. (save-excursion
  22969. (org-end-of-subtree t t))
  22970. nil))
  22971. (defun org-show-entry ()
  22972. "Show the body directly following this heading.
  22973. Show the heading too, if it is currently invisible."
  22974. (interactive)
  22975. (save-excursion
  22976. (ignore-errors
  22977. (org-back-to-heading t)
  22978. (outline-flag-region
  22979. (max (point-min) (1- (point)))
  22980. (save-excursion
  22981. (if (re-search-forward
  22982. (concat "[\r\n]\\(" org-outline-regexp "\\)") nil t)
  22983. (match-beginning 1)
  22984. (point-max)))
  22985. nil)
  22986. (org-cycle-hide-drawers 'children))))
  22987. (defun org-make-options-regexp (kwds &optional extra)
  22988. "Make a regular expression for keyword lines.
  22989. KWDS is a list of keywords, as strings. Optional argument EXTRA,
  22990. when non-nil, is a regexp matching keywords names."
  22991. (concat "^[ \t]*#\\+\\("
  22992. (regexp-opt kwds)
  22993. (and extra (concat (and kwds "\\|") extra))
  22994. "\\):[ \t]*\\(.*\\)"))
  22995. ;;;; Integration with and fixes for other packages
  22996. ;;; Imenu support
  22997. (defvar-local org-imenu-markers nil
  22998. "All markers currently used by Imenu.")
  22999. (defun org-imenu-new-marker (&optional pos)
  23000. "Return a new marker for use by Imenu, and remember the marker."
  23001. (let ((m (make-marker)))
  23002. (move-marker m (or pos (point)))
  23003. (push m org-imenu-markers)
  23004. m))
  23005. (defun org-imenu-get-tree ()
  23006. "Produce the index for Imenu."
  23007. (dolist (x org-imenu-markers) (move-marker x nil))
  23008. (setq org-imenu-markers nil)
  23009. (let* ((n org-imenu-depth)
  23010. (re (concat "^" (org-get-limited-outline-regexp)))
  23011. (subs (make-vector (1+ n) nil))
  23012. (last-level 0)
  23013. m level head0 head)
  23014. (save-excursion
  23015. (save-restriction
  23016. (widen)
  23017. (goto-char (point-max))
  23018. (while (re-search-backward re nil t)
  23019. (setq level (org-reduced-level (funcall outline-level)))
  23020. (when (and (<= level n)
  23021. (looking-at org-complex-heading-regexp)
  23022. (setq head0 (org-match-string-no-properties 4)))
  23023. (setq head (org-link-display-format head0)
  23024. m (org-imenu-new-marker))
  23025. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  23026. (if (>= level last-level)
  23027. (push (cons head m) (aref subs level))
  23028. (push (cons head (aref subs (1+ level))) (aref subs level))
  23029. (cl-loop for i from (1+ level) to n do (aset subs i nil)))
  23030. (setq last-level level)))))
  23031. (aref subs 1)))
  23032. (eval-after-load "imenu"
  23033. '(progn
  23034. (add-hook 'imenu-after-jump-hook
  23035. (lambda ()
  23036. (when (derived-mode-p 'org-mode)
  23037. (org-show-context 'org-goto))))))
  23038. (defun org-link-display-format (s)
  23039. "Replace links in string S with their description.
  23040. If there is no description, use the link target."
  23041. (save-match-data
  23042. (replace-regexp-in-string
  23043. org-bracket-link-analytic-regexp
  23044. (lambda (m)
  23045. (if (match-end 5) (match-string 5 m)
  23046. (concat (match-string 1 m) (match-string 3 m))))
  23047. s nil t)))
  23048. (defun org-toggle-link-display ()
  23049. "Toggle the literal or descriptive display of links."
  23050. (interactive)
  23051. (if org-descriptive-links
  23052. (progn (org-remove-from-invisibility-spec '(org-link))
  23053. (org-restart-font-lock)
  23054. (setq org-descriptive-links nil))
  23055. (progn (add-to-invisibility-spec '(org-link))
  23056. (org-restart-font-lock)
  23057. (setq org-descriptive-links t))))
  23058. ;; Speedbar support
  23059. (defvar org-speedbar-restriction-lock-overlay (make-overlay 1 1)
  23060. "Overlay marking the agenda restriction line in speedbar.")
  23061. (overlay-put org-speedbar-restriction-lock-overlay
  23062. 'face 'org-agenda-restriction-lock)
  23063. (overlay-put org-speedbar-restriction-lock-overlay
  23064. 'help-echo "Agendas are currently limited to this item.")
  23065. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  23066. (defun org-speedbar-set-agenda-restriction ()
  23067. "Restrict future agenda commands to the location at point in speedbar.
  23068. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  23069. (interactive)
  23070. (require 'org-agenda)
  23071. (let (p m tp np dir txt)
  23072. (cond
  23073. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  23074. 'org-imenu t))
  23075. (setq m (get-text-property p 'org-imenu-marker))
  23076. (with-current-buffer (marker-buffer m)
  23077. (goto-char m)
  23078. (org-agenda-set-restriction-lock 'subtree)))
  23079. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  23080. 'speedbar-function 'speedbar-find-file))
  23081. (setq tp (previous-single-property-change
  23082. (1+ p) 'speedbar-function)
  23083. np (next-single-property-change
  23084. tp 'speedbar-function)
  23085. dir (speedbar-line-directory)
  23086. txt (buffer-substring-no-properties (or tp (point-min))
  23087. (or np (point-max))))
  23088. (with-current-buffer (find-file-noselect
  23089. (let ((default-directory dir))
  23090. (expand-file-name txt)))
  23091. (unless (derived-mode-p 'org-mode)
  23092. (user-error "Cannot restrict to non-Org-mode file"))
  23093. (org-agenda-set-restriction-lock 'file)))
  23094. (t (user-error "Don't know how to restrict Org-mode's agenda")))
  23095. (move-overlay org-speedbar-restriction-lock-overlay
  23096. (point-at-bol) (point-at-eol))
  23097. (setq current-prefix-arg nil)
  23098. (org-agenda-maybe-redo)))
  23099. (defvar speedbar-file-key-map)
  23100. (declare-function speedbar-add-supported-extension "speedbar" (extension))
  23101. (eval-after-load "speedbar"
  23102. '(progn
  23103. (speedbar-add-supported-extension ".org")
  23104. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  23105. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  23106. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  23107. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  23108. (add-hook 'speedbar-visiting-tag-hook
  23109. (lambda () (and (derived-mode-p 'org-mode) (org-show-context 'org-goto))))))
  23110. ;;; Fixes and Hacks for problems with other packages
  23111. (defun org--flyspell-object-check-p (element)
  23112. "Non-nil when Flyspell can check object at point.
  23113. ELEMENT is the element at point."
  23114. (let ((object (save-excursion
  23115. (when (org-looking-at-p "\\>") (backward-char))
  23116. (org-element-context element))))
  23117. (cl-case (org-element-type object)
  23118. ;; Prevent checks in links due to keybinding conflict with
  23119. ;; Flyspell.
  23120. ((code entity export-snippet inline-babel-call
  23121. inline-src-block line-break latex-fragment link macro
  23122. statistics-cookie target timestamp verbatim)
  23123. nil)
  23124. (footnote-reference
  23125. ;; Only in inline footnotes, within the definition.
  23126. (and (eq (org-element-property :type object) 'inline)
  23127. (< (save-excursion
  23128. (goto-char (org-element-property :begin object))
  23129. (search-forward ":" nil t 2))
  23130. (point))))
  23131. (otherwise t))))
  23132. (defun org-mode-flyspell-verify ()
  23133. "Function used for `flyspell-generic-check-word-predicate'."
  23134. (if (org-at-heading-p)
  23135. ;; At a headline or an inlinetask, check title only. This is
  23136. ;; faster than relying on `org-element-at-point'.
  23137. (and (save-excursion (beginning-of-line)
  23138. (and (let ((case-fold-search t))
  23139. (not (looking-at "\\*+ END[ \t]*$")))
  23140. (looking-at org-complex-heading-regexp)))
  23141. (match-beginning 4)
  23142. (>= (point) (match-beginning 4))
  23143. (or (not (match-beginning 5))
  23144. (< (point) (match-beginning 5))))
  23145. (let* ((element (org-element-at-point))
  23146. (post-affiliated (org-element-property :post-affiliated element)))
  23147. (cond
  23148. ;; Ignore checks in all affiliated keywords but captions.
  23149. ((< (point) post-affiliated)
  23150. (and (save-excursion
  23151. (beginning-of-line)
  23152. (let ((case-fold-search t)) (looking-at "[ \t]*#\\+CAPTION:")))
  23153. (> (point) (match-end 0))
  23154. (org--flyspell-object-check-p element)))
  23155. ;; Ignore checks in LOGBOOK (or equivalent) drawer.
  23156. ((let ((log (org-log-into-drawer)))
  23157. (and log
  23158. (let ((drawer (org-element-lineage element '(drawer))))
  23159. (and drawer
  23160. (eq (compare-strings
  23161. log nil nil
  23162. (org-element-property :drawer-name drawer) nil nil t)
  23163. t)))))
  23164. nil)
  23165. (t
  23166. (cl-case (org-element-type element)
  23167. ((comment quote-section) t)
  23168. (comment-block
  23169. ;; Allow checks between block markers, not on them.
  23170. (and (> (line-beginning-position) post-affiliated)
  23171. (save-excursion
  23172. (end-of-line)
  23173. (skip-chars-forward " \r\t\n")
  23174. (< (point) (org-element-property :end element)))))
  23175. ;; Arbitrary list of keywords where checks are meaningful.
  23176. ;; Make sure point is on the value part of the element.
  23177. (keyword
  23178. (and (member (org-element-property :key element)
  23179. '("DESCRIPTION" "TITLE"))
  23180. (save-excursion
  23181. (search-backward ":" (line-beginning-position) t))))
  23182. ;; Check is globally allowed in paragraphs verse blocks and
  23183. ;; table rows (after affiliated keywords) but some objects
  23184. ;; must not be affected.
  23185. ((paragraph table-row verse-block)
  23186. (let ((cbeg (org-element-property :contents-begin element))
  23187. (cend (org-element-property :contents-end element)))
  23188. (and cbeg (>= (point) cbeg) (< (point) cend)
  23189. (org--flyspell-object-check-p element))))))))))
  23190. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  23191. (defun org-remove-flyspell-overlays-in (beg end)
  23192. "Remove flyspell overlays in region."
  23193. (and (org-bound-and-true-p flyspell-mode)
  23194. (fboundp 'flyspell-delete-region-overlays)
  23195. (flyspell-delete-region-overlays beg end)))
  23196. (defvar flyspell-delayed-commands)
  23197. (eval-after-load "flyspell"
  23198. '(add-to-list 'flyspell-delayed-commands 'org-self-insert-command))
  23199. ;; Make `bookmark-jump' shows the jump location if it was hidden.
  23200. (eval-after-load "bookmark"
  23201. '(if (boundp 'bookmark-after-jump-hook)
  23202. ;; We can use the hook
  23203. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  23204. ;; Hook not available, use advice
  23205. (defadvice bookmark-jump (after org-make-visible activate)
  23206. "Make the position visible."
  23207. (org-bookmark-jump-unhide))))
  23208. ;; Make sure saveplace shows the location if it was hidden
  23209. (eval-after-load "saveplace"
  23210. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  23211. "Make the position visible."
  23212. (org-bookmark-jump-unhide)))
  23213. ;; Make sure ecb shows the location if it was hidden
  23214. (eval-after-load "ecb"
  23215. '(defadvice ecb-method-clicked (after esf/org-show-context activate)
  23216. "Make hierarchy visible when jumping into location from ECB tree buffer."
  23217. (when (derived-mode-p 'org-mode)
  23218. (org-show-context))))
  23219. (defun org-bookmark-jump-unhide ()
  23220. "Unhide the current position, to show the bookmark location."
  23221. (and (derived-mode-p 'org-mode)
  23222. (or (outline-invisible-p)
  23223. (save-excursion (goto-char (max (point-min) (1- (point))))
  23224. (outline-invisible-p)))
  23225. (org-show-context 'bookmark-jump)))
  23226. (defun org-mark-jump-unhide ()
  23227. "Make the point visible with `org-show-context' after jumping to the mark."
  23228. (when (and (derived-mode-p 'org-mode)
  23229. (outline-invisible-p))
  23230. (org-show-context 'mark-goto)))
  23231. (eval-after-load "simple"
  23232. '(defadvice pop-to-mark-command (after org-make-visible activate)
  23233. "Make the point visible with `org-show-context'."
  23234. (org-mark-jump-unhide)))
  23235. (eval-after-load "simple"
  23236. '(defadvice exchange-point-and-mark (after org-make-visible activate)
  23237. "Make the point visible with `org-show-context'."
  23238. (org-mark-jump-unhide)))
  23239. (eval-after-load "simple"
  23240. '(defadvice pop-global-mark (after org-make-visible activate)
  23241. "Make the point visible with `org-show-context'."
  23242. (org-mark-jump-unhide)))
  23243. ;; Make session.el ignore our circular variable
  23244. (defvar session-globals-exclude)
  23245. (eval-after-load "session"
  23246. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  23247. ;;;; Finish up
  23248. (provide 'org)
  23249. (run-hooks 'org-load-hook)
  23250. ;;; org.el ends here