org.el 954 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106171071710817109171101711117112171131711417115171161711717118171191712017121171221712317124171251712617127171281712917130171311713217133171341713517136171371713817139171401714117142171431714417145171461714717148171491715017151171521715317154171551715617157171581715917160171611716217163171641716517166171671716817169171701717117172171731717417175171761717717178171791718017181171821718317184171851718617187171881718917190171911719217193171941719517196171971719817199172001720117202172031720417205172061720717208172091721017211172121721317214172151721617217172181721917220172211722217223172241722517226172271722817229172301723117232172331723417235172361723717238172391724017241172421724317244172451724617247172481724917250172511725217253172541725517256172571725817259172601726117262172631726417265172661726717268172691727017271172721727317274172751727617277172781727917280172811728217283172841728517286172871728817289172901729117292172931729417295172961729717298172991730017301173021730317304173051730617307173081730917310173111731217313173141731517316173171731817319173201732117322173231732417325173261732717328173291733017331173321733317334173351733617337173381733917340173411734217343173441734517346173471734817349173501735117352173531735417355173561735717358173591736017361173621736317364173651736617367173681736917370173711737217373173741737517376173771737817379173801738117382173831738417385173861738717388173891739017391173921739317394173951739617397173981739917400174011740217403174041740517406174071740817409174101741117412174131741417415174161741717418174191742017421174221742317424174251742617427174281742917430174311743217433174341743517436174371743817439174401744117442174431744417445174461744717448174491745017451174521745317454174551745617457174581745917460174611746217463174641746517466174671746817469174701747117472174731747417475174761747717478174791748017481174821748317484174851748617487174881748917490174911749217493174941749517496174971749817499175001750117502175031750417505175061750717508175091751017511175121751317514175151751617517175181751917520175211752217523175241752517526175271752817529175301753117532175331753417535175361753717538175391754017541175421754317544175451754617547175481754917550175511755217553175541755517556175571755817559175601756117562175631756417565175661756717568175691757017571175721757317574175751757617577175781757917580175811758217583175841758517586175871758817589175901759117592175931759417595175961759717598175991760017601176021760317604176051760617607176081760917610176111761217613176141761517616176171761817619176201762117622176231762417625176261762717628176291763017631176321763317634176351763617637176381763917640176411764217643176441764517646176471764817649176501765117652176531765417655176561765717658176591766017661176621766317664176651766617667176681766917670176711767217673176741767517676176771767817679176801768117682176831768417685176861768717688176891769017691176921769317694176951769617697176981769917700177011770217703177041770517706177071770817709177101771117712177131771417715177161771717718177191772017721177221772317724177251772617727177281772917730177311773217733177341773517736177371773817739177401774117742177431774417745177461774717748177491775017751177521775317754177551775617757177581775917760177611776217763177641776517766177671776817769177701777117772177731777417775177761777717778177791778017781177821778317784177851778617787177881778917790177911779217793177941779517796177971779817799178001780117802178031780417805178061780717808178091781017811178121781317814178151781617817178181781917820178211782217823178241782517826178271782817829178301783117832178331783417835178361783717838178391784017841178421784317844178451784617847178481784917850178511785217853178541785517856178571785817859178601786117862178631786417865178661786717868178691787017871178721787317874178751787617877178781787917880178811788217883178841788517886178871788817889178901789117892178931789417895178961789717898178991790017901179021790317904179051790617907179081790917910179111791217913179141791517916179171791817919179201792117922179231792417925179261792717928179291793017931179321793317934179351793617937179381793917940179411794217943179441794517946179471794817949179501795117952179531795417955179561795717958179591796017961179621796317964179651796617967179681796917970179711797217973179741797517976179771797817979179801798117982179831798417985179861798717988179891799017991179921799317994179951799617997179981799918000180011800218003180041800518006180071800818009180101801118012180131801418015180161801718018180191802018021180221802318024180251802618027180281802918030180311803218033180341803518036180371803818039180401804118042180431804418045180461804718048180491805018051180521805318054180551805618057180581805918060180611806218063180641806518066180671806818069180701807118072180731807418075180761807718078180791808018081180821808318084180851808618087180881808918090180911809218093180941809518096180971809818099181001810118102181031810418105181061810718108181091811018111181121811318114181151811618117181181811918120181211812218123181241812518126181271812818129181301813118132181331813418135181361813718138181391814018141181421814318144181451814618147181481814918150181511815218153181541815518156181571815818159181601816118162181631816418165181661816718168181691817018171181721817318174181751817618177181781817918180181811818218183181841818518186181871818818189181901819118192181931819418195181961819718198181991820018201182021820318204182051820618207182081820918210182111821218213182141821518216182171821818219182201822118222182231822418225182261822718228182291823018231182321823318234182351823618237182381823918240182411824218243182441824518246182471824818249182501825118252182531825418255182561825718258182591826018261182621826318264182651826618267182681826918270182711827218273182741827518276182771827818279182801828118282182831828418285182861828718288182891829018291182921829318294182951829618297182981829918300183011830218303183041830518306183071830818309183101831118312183131831418315183161831718318183191832018321183221832318324183251832618327183281832918330183311833218333183341833518336183371833818339183401834118342183431834418345183461834718348183491835018351183521835318354183551835618357183581835918360183611836218363183641836518366183671836818369183701837118372183731837418375183761837718378183791838018381183821838318384183851838618387183881838918390183911839218393183941839518396183971839818399184001840118402184031840418405184061840718408184091841018411184121841318414184151841618417184181841918420184211842218423184241842518426184271842818429184301843118432184331843418435184361843718438184391844018441184421844318444184451844618447184481844918450184511845218453184541845518456184571845818459184601846118462184631846418465184661846718468184691847018471184721847318474184751847618477184781847918480184811848218483184841848518486184871848818489184901849118492184931849418495184961849718498184991850018501185021850318504185051850618507185081850918510185111851218513185141851518516185171851818519185201852118522185231852418525185261852718528185291853018531185321853318534185351853618537185381853918540185411854218543185441854518546185471854818549185501855118552185531855418555185561855718558185591856018561185621856318564185651856618567185681856918570185711857218573185741857518576185771857818579185801858118582185831858418585185861858718588185891859018591185921859318594185951859618597185981859918600186011860218603186041860518606186071860818609186101861118612186131861418615186161861718618186191862018621186221862318624186251862618627186281862918630186311863218633186341863518636186371863818639186401864118642186431864418645186461864718648186491865018651186521865318654186551865618657186581865918660186611866218663186641866518666186671866818669186701867118672186731867418675186761867718678186791868018681186821868318684186851868618687186881868918690186911869218693186941869518696186971869818699187001870118702187031870418705187061870718708187091871018711187121871318714187151871618717187181871918720187211872218723187241872518726187271872818729187301873118732187331873418735187361873718738187391874018741187421874318744187451874618747187481874918750187511875218753187541875518756187571875818759187601876118762187631876418765187661876718768187691877018771187721877318774187751877618777187781877918780187811878218783187841878518786187871878818789187901879118792187931879418795187961879718798187991880018801188021880318804188051880618807188081880918810188111881218813188141881518816188171881818819188201882118822188231882418825188261882718828188291883018831188321883318834188351883618837188381883918840188411884218843188441884518846188471884818849188501885118852188531885418855188561885718858188591886018861188621886318864188651886618867188681886918870188711887218873188741887518876188771887818879188801888118882188831888418885188861888718888188891889018891188921889318894188951889618897188981889918900189011890218903189041890518906189071890818909189101891118912189131891418915189161891718918189191892018921189221892318924189251892618927189281892918930189311893218933189341893518936189371893818939189401894118942189431894418945189461894718948189491895018951189521895318954189551895618957189581895918960189611896218963189641896518966189671896818969189701897118972189731897418975189761897718978189791898018981189821898318984189851898618987189881898918990189911899218993189941899518996189971899818999190001900119002190031900419005190061900719008190091901019011190121901319014190151901619017190181901919020190211902219023190241902519026190271902819029190301903119032190331903419035190361903719038190391904019041190421904319044190451904619047190481904919050190511905219053190541905519056190571905819059190601906119062190631906419065190661906719068190691907019071190721907319074190751907619077190781907919080190811908219083190841908519086190871908819089190901909119092190931909419095190961909719098190991910019101191021910319104191051910619107191081910919110191111911219113191141911519116191171911819119191201912119122191231912419125191261912719128191291913019131191321913319134191351913619137191381913919140191411914219143191441914519146191471914819149191501915119152191531915419155191561915719158191591916019161191621916319164191651916619167191681916919170191711917219173191741917519176191771917819179191801918119182191831918419185191861918719188191891919019191191921919319194191951919619197191981919919200192011920219203192041920519206192071920819209192101921119212192131921419215192161921719218192191922019221192221922319224192251922619227192281922919230192311923219233192341923519236192371923819239192401924119242192431924419245192461924719248192491925019251192521925319254192551925619257192581925919260192611926219263192641926519266192671926819269192701927119272192731927419275192761927719278192791928019281192821928319284192851928619287192881928919290192911929219293192941929519296192971929819299193001930119302193031930419305193061930719308193091931019311193121931319314193151931619317193181931919320193211932219323193241932519326193271932819329193301933119332193331933419335193361933719338193391934019341193421934319344193451934619347193481934919350193511935219353193541935519356193571935819359193601936119362193631936419365193661936719368193691937019371193721937319374193751937619377193781937919380193811938219383193841938519386193871938819389193901939119392193931939419395193961939719398193991940019401194021940319404194051940619407194081940919410194111941219413194141941519416194171941819419194201942119422194231942419425194261942719428194291943019431194321943319434194351943619437194381943919440194411944219443194441944519446194471944819449194501945119452194531945419455194561945719458194591946019461194621946319464194651946619467194681946919470194711947219473194741947519476194771947819479194801948119482194831948419485194861948719488194891949019491194921949319494194951949619497194981949919500195011950219503195041950519506195071950819509195101951119512195131951419515195161951719518195191952019521195221952319524195251952619527195281952919530195311953219533195341953519536195371953819539195401954119542195431954419545195461954719548195491955019551195521955319554195551955619557195581955919560195611956219563195641956519566195671956819569195701957119572195731957419575195761957719578195791958019581195821958319584195851958619587195881958919590195911959219593195941959519596195971959819599196001960119602196031960419605196061960719608196091961019611196121961319614196151961619617196181961919620196211962219623196241962519626196271962819629196301963119632196331963419635196361963719638196391964019641196421964319644196451964619647196481964919650196511965219653196541965519656196571965819659196601966119662196631966419665196661966719668196691967019671196721967319674196751967619677196781967919680196811968219683196841968519686196871968819689196901969119692196931969419695196961969719698196991970019701197021970319704197051970619707197081970919710197111971219713197141971519716197171971819719197201972119722197231972419725197261972719728197291973019731197321973319734197351973619737197381973919740197411974219743197441974519746197471974819749197501975119752197531975419755197561975719758197591976019761197621976319764197651976619767197681976919770197711977219773197741977519776197771977819779197801978119782197831978419785197861978719788197891979019791197921979319794197951979619797197981979919800198011980219803198041980519806198071980819809198101981119812198131981419815198161981719818198191982019821198221982319824198251982619827198281982919830198311983219833198341983519836198371983819839198401984119842198431984419845198461984719848198491985019851198521985319854198551985619857198581985919860198611986219863198641986519866198671986819869198701987119872198731987419875198761987719878198791988019881198821988319884198851988619887198881988919890198911989219893198941989519896198971989819899199001990119902199031990419905199061990719908199091991019911199121991319914199151991619917199181991919920199211992219923199241992519926199271992819929199301993119932199331993419935199361993719938199391994019941199421994319944199451994619947199481994919950199511995219953199541995519956199571995819959199601996119962199631996419965199661996719968199691997019971199721997319974199751997619977199781997919980199811998219983199841998519986199871998819989199901999119992199931999419995199961999719998199992000020001200022000320004200052000620007200082000920010200112001220013200142001520016200172001820019200202002120022200232002420025200262002720028200292003020031200322003320034200352003620037200382003920040200412004220043200442004520046200472004820049200502005120052200532005420055200562005720058200592006020061200622006320064200652006620067200682006920070200712007220073200742007520076200772007820079200802008120082200832008420085200862008720088200892009020091200922009320094200952009620097200982009920100201012010220103201042010520106201072010820109201102011120112201132011420115201162011720118201192012020121201222012320124201252012620127201282012920130201312013220133201342013520136201372013820139201402014120142201432014420145201462014720148201492015020151201522015320154201552015620157201582015920160201612016220163201642016520166201672016820169201702017120172201732017420175201762017720178201792018020181201822018320184201852018620187201882018920190201912019220193201942019520196201972019820199202002020120202202032020420205202062020720208202092021020211202122021320214202152021620217202182021920220202212022220223202242022520226202272022820229202302023120232202332023420235202362023720238202392024020241202422024320244202452024620247202482024920250202512025220253202542025520256202572025820259202602026120262202632026420265202662026720268202692027020271202722027320274202752027620277202782027920280202812028220283202842028520286202872028820289202902029120292202932029420295202962029720298202992030020301203022030320304203052030620307203082030920310203112031220313203142031520316203172031820319203202032120322203232032420325203262032720328203292033020331203322033320334203352033620337203382033920340203412034220343203442034520346203472034820349203502035120352203532035420355203562035720358203592036020361203622036320364203652036620367203682036920370203712037220373203742037520376203772037820379203802038120382203832038420385203862038720388203892039020391203922039320394203952039620397203982039920400204012040220403204042040520406204072040820409204102041120412204132041420415204162041720418204192042020421204222042320424204252042620427204282042920430204312043220433204342043520436204372043820439204402044120442204432044420445204462044720448204492045020451204522045320454204552045620457204582045920460204612046220463204642046520466204672046820469204702047120472204732047420475204762047720478204792048020481204822048320484204852048620487204882048920490204912049220493204942049520496204972049820499205002050120502205032050420505205062050720508205092051020511205122051320514205152051620517205182051920520205212052220523205242052520526205272052820529205302053120532205332053420535205362053720538205392054020541205422054320544205452054620547205482054920550205512055220553205542055520556205572055820559205602056120562205632056420565205662056720568205692057020571205722057320574205752057620577205782057920580205812058220583205842058520586205872058820589205902059120592205932059420595205962059720598205992060020601206022060320604206052060620607206082060920610206112061220613206142061520616206172061820619206202062120622206232062420625206262062720628206292063020631206322063320634206352063620637206382063920640206412064220643206442064520646206472064820649206502065120652206532065420655206562065720658206592066020661206622066320664206652066620667206682066920670206712067220673206742067520676206772067820679206802068120682206832068420685206862068720688206892069020691206922069320694206952069620697206982069920700207012070220703207042070520706207072070820709207102071120712207132071420715207162071720718207192072020721207222072320724207252072620727207282072920730207312073220733207342073520736207372073820739207402074120742207432074420745207462074720748207492075020751207522075320754207552075620757207582075920760207612076220763207642076520766207672076820769207702077120772207732077420775207762077720778207792078020781207822078320784207852078620787207882078920790207912079220793207942079520796207972079820799208002080120802208032080420805208062080720808208092081020811208122081320814208152081620817208182081920820208212082220823208242082520826208272082820829208302083120832208332083420835208362083720838208392084020841208422084320844208452084620847208482084920850208512085220853208542085520856208572085820859208602086120862208632086420865208662086720868208692087020871208722087320874208752087620877208782087920880208812088220883208842088520886208872088820889208902089120892208932089420895208962089720898208992090020901209022090320904209052090620907209082090920910209112091220913209142091520916209172091820919209202092120922209232092420925209262092720928209292093020931209322093320934209352093620937209382093920940209412094220943209442094520946209472094820949209502095120952209532095420955209562095720958209592096020961209622096320964209652096620967209682096920970209712097220973209742097520976209772097820979209802098120982209832098420985209862098720988209892099020991209922099320994209952099620997209982099921000210012100221003210042100521006210072100821009210102101121012210132101421015210162101721018210192102021021210222102321024210252102621027210282102921030210312103221033210342103521036210372103821039210402104121042210432104421045210462104721048210492105021051210522105321054210552105621057210582105921060210612106221063210642106521066210672106821069210702107121072210732107421075210762107721078210792108021081210822108321084210852108621087210882108921090210912109221093210942109521096210972109821099211002110121102211032110421105211062110721108211092111021111211122111321114211152111621117211182111921120211212112221123211242112521126211272112821129211302113121132211332113421135211362113721138211392114021141211422114321144211452114621147211482114921150211512115221153211542115521156211572115821159211602116121162211632116421165211662116721168211692117021171211722117321174211752117621177211782117921180211812118221183211842118521186211872118821189211902119121192211932119421195211962119721198211992120021201212022120321204212052120621207212082120921210212112121221213212142121521216212172121821219212202122121222212232122421225212262122721228212292123021231212322123321234212352123621237212382123921240212412124221243212442124521246212472124821249212502125121252212532125421255212562125721258212592126021261212622126321264212652126621267212682126921270212712127221273212742127521276212772127821279212802128121282212832128421285212862128721288212892129021291212922129321294212952129621297212982129921300213012130221303213042130521306213072130821309213102131121312213132131421315213162131721318213192132021321213222132321324213252132621327213282132921330213312133221333213342133521336213372133821339213402134121342213432134421345213462134721348213492135021351213522135321354213552135621357213582135921360213612136221363213642136521366213672136821369213702137121372213732137421375213762137721378213792138021381213822138321384213852138621387213882138921390213912139221393213942139521396213972139821399214002140121402214032140421405214062140721408214092141021411214122141321414214152141621417214182141921420214212142221423214242142521426214272142821429214302143121432214332143421435214362143721438214392144021441214422144321444214452144621447214482144921450214512145221453214542145521456214572145821459214602146121462214632146421465214662146721468214692147021471214722147321474214752147621477214782147921480214812148221483214842148521486214872148821489214902149121492214932149421495214962149721498214992150021501215022150321504215052150621507215082150921510215112151221513215142151521516215172151821519215202152121522215232152421525215262152721528215292153021531215322153321534215352153621537215382153921540215412154221543215442154521546215472154821549215502155121552215532155421555215562155721558215592156021561215622156321564215652156621567215682156921570215712157221573215742157521576215772157821579215802158121582215832158421585215862158721588215892159021591215922159321594215952159621597215982159921600216012160221603216042160521606216072160821609216102161121612216132161421615216162161721618216192162021621216222162321624216252162621627216282162921630216312163221633216342163521636216372163821639216402164121642216432164421645216462164721648216492165021651216522165321654216552165621657216582165921660216612166221663216642166521666216672166821669216702167121672216732167421675216762167721678216792168021681216822168321684216852168621687216882168921690216912169221693216942169521696216972169821699217002170121702217032170421705217062170721708217092171021711217122171321714217152171621717217182171921720217212172221723217242172521726217272172821729217302173121732217332173421735217362173721738217392174021741217422174321744217452174621747217482174921750217512175221753217542175521756217572175821759217602176121762217632176421765217662176721768217692177021771217722177321774217752177621777217782177921780217812178221783217842178521786217872178821789217902179121792217932179421795217962179721798217992180021801218022180321804218052180621807218082180921810218112181221813218142181521816218172181821819218202182121822218232182421825218262182721828218292183021831218322183321834218352183621837218382183921840218412184221843218442184521846218472184821849218502185121852218532185421855218562185721858218592186021861218622186321864218652186621867218682186921870218712187221873218742187521876218772187821879218802188121882218832188421885218862188721888218892189021891218922189321894218952189621897218982189921900219012190221903219042190521906219072190821909219102191121912219132191421915219162191721918219192192021921219222192321924219252192621927219282192921930219312193221933219342193521936219372193821939219402194121942219432194421945219462194721948219492195021951219522195321954219552195621957219582195921960219612196221963219642196521966219672196821969219702197121972219732197421975219762197721978219792198021981219822198321984219852198621987219882198921990219912199221993219942199521996219972199821999220002200122002220032200422005220062200722008220092201022011220122201322014220152201622017220182201922020220212202222023220242202522026220272202822029220302203122032220332203422035220362203722038220392204022041220422204322044220452204622047220482204922050220512205222053220542205522056220572205822059220602206122062220632206422065220662206722068220692207022071220722207322074220752207622077220782207922080220812208222083220842208522086220872208822089220902209122092220932209422095220962209722098220992210022101221022210322104221052210622107221082210922110221112211222113221142211522116221172211822119221202212122122221232212422125221262212722128221292213022131221322213322134221352213622137221382213922140221412214222143221442214522146221472214822149221502215122152221532215422155221562215722158221592216022161221622216322164221652216622167221682216922170221712217222173221742217522176221772217822179221802218122182221832218422185221862218722188221892219022191221922219322194221952219622197221982219922200222012220222203222042220522206222072220822209222102221122212222132221422215222162221722218222192222022221222222222322224222252222622227222282222922230222312223222233222342223522236222372223822239222402224122242222432224422245222462224722248222492225022251222522225322254222552225622257222582225922260222612226222263222642226522266222672226822269222702227122272222732227422275222762227722278222792228022281222822228322284222852228622287222882228922290222912229222293222942229522296222972229822299223002230122302223032230422305223062230722308223092231022311223122231322314223152231622317223182231922320223212232222323223242232522326223272232822329223302233122332223332233422335223362233722338223392234022341223422234322344223452234622347223482234922350223512235222353223542235522356223572235822359223602236122362223632236422365223662236722368223692237022371223722237322374223752237622377223782237922380223812238222383223842238522386223872238822389223902239122392223932239422395223962239722398223992240022401224022240322404224052240622407224082240922410224112241222413224142241522416224172241822419224202242122422224232242422425224262242722428224292243022431224322243322434224352243622437224382243922440224412244222443224442244522446224472244822449224502245122452224532245422455224562245722458224592246022461224622246322464224652246622467224682246922470224712247222473224742247522476224772247822479224802248122482224832248422485224862248722488224892249022491224922249322494224952249622497224982249922500225012250222503225042250522506225072250822509225102251122512225132251422515225162251722518225192252022521225222252322524225252252622527225282252922530225312253222533225342253522536225372253822539225402254122542225432254422545225462254722548225492255022551225522255322554225552255622557225582255922560225612256222563225642256522566225672256822569225702257122572225732257422575225762257722578225792258022581225822258322584225852258622587225882258922590225912259222593225942259522596225972259822599226002260122602226032260422605226062260722608226092261022611226122261322614226152261622617226182261922620226212262222623226242262522626226272262822629226302263122632226332263422635226362263722638226392264022641226422264322644226452264622647226482264922650226512265222653226542265522656226572265822659226602266122662226632266422665226662266722668226692267022671226722267322674226752267622677226782267922680226812268222683226842268522686226872268822689226902269122692226932269422695226962269722698226992270022701227022270322704227052270622707227082270922710227112271222713227142271522716227172271822719227202272122722227232272422725227262272722728227292273022731227322273322734227352273622737227382273922740227412274222743227442274522746227472274822749227502275122752227532275422755227562275722758227592276022761227622276322764227652276622767227682276922770227712277222773227742277522776227772277822779227802278122782227832278422785227862278722788227892279022791227922279322794227952279622797227982279922800228012280222803228042280522806228072280822809228102281122812228132281422815228162281722818228192282022821228222282322824228252282622827228282282922830228312283222833228342283522836228372283822839228402284122842228432284422845228462284722848228492285022851228522285322854228552285622857228582285922860228612286222863228642286522866228672286822869228702287122872228732287422875228762287722878228792288022881228822288322884228852288622887228882288922890228912289222893228942289522896228972289822899229002290122902229032290422905229062290722908229092291022911229122291322914229152291622917229182291922920229212292222923229242292522926229272292822929229302293122932229332293422935229362293722938229392294022941229422294322944229452294622947229482294922950229512295222953229542295522956229572295822959229602296122962229632296422965229662296722968229692297022971229722297322974229752297622977229782297922980229812298222983229842298522986229872298822989229902299122992229932299422995229962299722998229992300023001230022300323004230052300623007230082300923010230112301223013230142301523016230172301823019230202302123022230232302423025230262302723028230292303023031230322303323034230352303623037230382303923040230412304223043230442304523046230472304823049230502305123052230532305423055230562305723058230592306023061230622306323064230652306623067230682306923070230712307223073230742307523076230772307823079230802308123082230832308423085230862308723088230892309023091230922309323094230952309623097230982309923100231012310223103231042310523106231072310823109231102311123112231132311423115231162311723118231192312023121231222312323124231252312623127231282312923130231312313223133231342313523136231372313823139231402314123142231432314423145231462314723148231492315023151231522315323154231552315623157231582315923160231612316223163231642316523166231672316823169231702317123172231732317423175231762317723178231792318023181231822318323184231852318623187231882318923190231912319223193231942319523196231972319823199232002320123202232032320423205232062320723208232092321023211232122321323214232152321623217232182321923220232212322223223232242322523226232272322823229232302323123232232332323423235232362323723238232392324023241232422324323244232452324623247232482324923250232512325223253232542325523256232572325823259232602326123262232632326423265232662326723268232692327023271232722327323274232752327623277232782327923280232812328223283232842328523286232872328823289232902329123292232932329423295232962329723298232992330023301233022330323304233052330623307233082330923310233112331223313233142331523316233172331823319233202332123322233232332423325233262332723328233292333023331233322333323334233352333623337233382333923340233412334223343233442334523346233472334823349233502335123352233532335423355233562335723358233592336023361233622336323364233652336623367233682336923370233712337223373233742337523376233772337823379233802338123382233832338423385233862338723388233892339023391233922339323394233952339623397233982339923400234012340223403234042340523406234072340823409234102341123412234132341423415234162341723418234192342023421234222342323424234252342623427234282342923430234312343223433234342343523436234372343823439234402344123442234432344423445234462344723448234492345023451234522345323454234552345623457234582345923460234612346223463234642346523466234672346823469234702347123472234732347423475234762347723478234792348023481234822348323484234852348623487234882348923490234912349223493234942349523496234972349823499235002350123502235032350423505235062350723508235092351023511235122351323514235152351623517235182351923520235212352223523235242352523526235272352823529235302353123532235332353423535235362353723538235392354023541235422354323544235452354623547235482354923550235512355223553235542355523556235572355823559235602356123562235632356423565235662356723568235692357023571235722357323574235752357623577235782357923580235812358223583235842358523586235872358823589235902359123592235932359423595235962359723598235992360023601236022360323604236052360623607236082360923610236112361223613236142361523616236172361823619236202362123622236232362423625236262362723628236292363023631236322363323634236352363623637236382363923640236412364223643236442364523646236472364823649236502365123652236532365423655236562365723658236592366023661236622366323664236652366623667236682366923670236712367223673236742367523676236772367823679236802368123682236832368423685236862368723688236892369023691236922369323694236952369623697236982369923700237012370223703237042370523706237072370823709237102371123712237132371423715237162371723718237192372023721237222372323724237252372623727237282372923730237312373223733237342373523736237372373823739237402374123742237432374423745237462374723748237492375023751237522375323754237552375623757237582375923760237612376223763237642376523766237672376823769237702377123772237732377423775237762377723778237792378023781237822378323784237852378623787237882378923790237912379223793237942379523796237972379823799238002380123802238032380423805238062380723808238092381023811238122381323814238152381623817238182381923820238212382223823238242382523826238272382823829238302383123832238332383423835238362383723838238392384023841238422384323844238452384623847238482384923850238512385223853238542385523856238572385823859238602386123862238632386423865238662386723868238692387023871238722387323874238752387623877238782387923880238812388223883238842388523886238872388823889238902389123892238932389423895238962389723898238992390023901239022390323904239052390623907239082390923910239112391223913239142391523916239172391823919239202392123922239232392423925239262392723928239292393023931239322393323934239352393623937239382393923940239412394223943239442394523946239472394823949239502395123952239532395423955239562395723958239592396023961239622396323964239652396623967239682396923970239712397223973239742397523976239772397823979239802398123982239832398423985239862398723988239892399023991239922399323994239952399623997239982399924000240012400224003240042400524006240072400824009240102401124012240132401424015240162401724018240192402024021240222402324024240252402624027240282402924030240312403224033240342403524036240372403824039240402404124042240432404424045240462404724048240492405024051240522405324054240552405624057240582405924060240612406224063240642406524066240672406824069240702407124072240732407424075240762407724078240792408024081240822408324084240852408624087240882408924090240912409224093240942409524096240972409824099241002410124102241032410424105241062410724108241092411024111241122411324114241152411624117241182411924120241212412224123241242412524126241272412824129241302413124132241332413424135241362413724138241392414024141241422414324144241452414624147241482414924150241512415224153241542415524156241572415824159241602416124162241632416424165241662416724168241692417024171241722417324174241752417624177241782417924180241812418224183241842418524186241872418824189241902419124192241932419424195241962419724198241992420024201242022420324204242052420624207242082420924210242112421224213242142421524216242172421824219242202422124222242232422424225242262422724228242292423024231242322423324234242352423624237242382423924240242412424224243242442424524246242472424824249242502425124252242532425424255242562425724258242592426024261242622426324264242652426624267242682426924270242712427224273242742427524276242772427824279242802428124282242832428424285242862428724288242892429024291242922429324294242952429624297242982429924300243012430224303243042430524306243072430824309243102431124312243132431424315243162431724318243192432024321243222432324324243252432624327243282432924330243312433224333243342433524336243372433824339243402434124342243432434424345243462434724348243492435024351243522435324354243552435624357243582435924360243612436224363243642436524366243672436824369243702437124372243732437424375243762437724378243792438024381243822438324384243852438624387243882438924390243912439224393243942439524396243972439824399244002440124402244032440424405244062440724408244092441024411244122441324414244152441624417244182441924420244212442224423244242442524426244272442824429244302443124432244332443424435244362443724438244392444024441244422444324444244452444624447244482444924450244512445224453244542445524456244572445824459244602446124462244632446424465244662446724468244692447024471244722447324474244752447624477244782447924480244812448224483244842448524486244872448824489244902449124492244932449424495244962449724498244992450024501245022450324504245052450624507245082450924510245112451224513245142451524516245172451824519245202452124522245232452424525245262452724528245292453024531245322453324534245352453624537245382453924540245412454224543245442454524546245472454824549245502455124552245532455424555245562455724558245592456024561245622456324564245652456624567245682456924570245712457224573245742457524576245772457824579245802458124582245832458424585245862458724588245892459024591245922459324594245952459624597245982459924600246012460224603246042460524606246072460824609246102461124612246132461424615246162461724618246192462024621246222462324624246252462624627246282462924630246312463224633246342463524636246372463824639246402464124642246432464424645246462464724648246492465024651246522465324654246552465624657246582465924660246612466224663246642466524666246672466824669246702467124672246732467424675246762467724678246792468024681246822468324684246852468624687246882468924690246912469224693246942469524696246972469824699247002470124702247032470424705247062470724708247092471024711247122471324714247152471624717247182471924720247212472224723247242472524726247272472824729247302473124732247332473424735247362473724738247392474024741247422474324744247452474624747247482474924750247512475224753247542475524756247572475824759247602476124762247632476424765247662476724768247692477024771247722477324774247752477624777247782477924780247812478224783247842478524786247872478824789247902479124792247932479424795247962479724798247992480024801248022480324804248052480624807248082480924810248112481224813248142481524816248172481824819248202482124822248232482424825248262482724828248292483024831248322483324834248352483624837248382483924840248412484224843248442484524846248472484824849248502485124852248532485424855248562485724858248592486024861248622486324864248652486624867248682486924870248712487224873248742487524876248772487824879248802488124882248832488424885248862488724888248892489024891248922489324894248952489624897248982489924900249012490224903249042490524906249072490824909249102491124912249132491424915249162491724918249192492024921249222492324924249252492624927249282492924930249312493224933249342493524936249372493824939249402494124942249432494424945249462494724948249492495024951249522495324954249552495624957249582495924960249612496224963249642496524966249672496824969249702497124972249732497424975249762497724978249792498024981249822498324984249852498624987249882498924990249912499224993249942499524996249972499824999250002500125002250032500425005250062500725008250092501025011250122501325014250152501625017250182501925020250212502225023250242502525026250272502825029250302503125032250332503425035250362503725038250392504025041250422504325044250452504625047250482504925050250512505225053250542505525056250572505825059250602506125062250632506425065250662506725068250692507025071250722507325074250752507625077250782507925080250812508225083250842508525086250872508825089250902509125092250932509425095250962509725098250992510025101251022510325104251052510625107251082510925110251112511225113251142511525116251172511825119251202512125122251232512425125251262512725128251292513025131251322513325134251352513625137251382513925140251412514225143251442514525146251472514825149251502515125152251532515425155251562515725158251592516025161251622516325164251652516625167251682516925170251712517225173251742517525176251772517825179251802518125182251832518425185251862518725188251892519025191251922519325194251952519625197251982519925200252012520225203252042520525206252072520825209252102521125212252132521425215252162521725218252192522025221252222522325224
  1. ;;; org.el --- Outline-based notes management and organizer -*- lexical-binding: t; -*-
  2. ;; Carstens outline-mode for keeping track of everything.
  3. ;; Copyright (C) 2004-2016 Free Software Foundation, Inc.
  4. ;;
  5. ;; Author: Carsten Dominik <carsten at orgmode dot org>
  6. ;; Maintainer: Carsten Dominik <carsten at orgmode dot org>
  7. ;; Keywords: outlines, hypermedia, calendar, wp
  8. ;; Homepage: http://orgmode.org
  9. ;;
  10. ;; This file is part of GNU Emacs.
  11. ;;
  12. ;; GNU Emacs is free software: you can redistribute it and/or modify
  13. ;; it under the terms of the GNU General Public License as published by
  14. ;; the Free Software Foundation, either version 3 of the License, or
  15. ;; (at your option) any later version.
  16. ;; GNU Emacs is distributed in the hope that it will be useful,
  17. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. ;; GNU General Public License for more details.
  20. ;; You should have received a copy of the GNU General Public License
  21. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  22. ;;
  23. ;;; Commentary:
  24. ;;
  25. ;; Org-mode is a mode for keeping notes, maintaining ToDo lists, and doing
  26. ;; project planning with a fast and effective plain-text system.
  27. ;;
  28. ;; Org-mode develops organizational tasks around NOTES files that contain
  29. ;; information about projects as plain text. Org-mode is implemented on
  30. ;; top of outline-mode, which makes it possible to keep the content of
  31. ;; large files well structured. Visibility cycling and structure editing
  32. ;; help to work with the tree. Tables are easily created with a built-in
  33. ;; table editor. Org-mode supports ToDo items, deadlines, time stamps,
  34. ;; and scheduling. It dynamically compiles entries into an agenda that
  35. ;; utilizes and smoothly integrates much of the Emacs calendar and diary.
  36. ;; Plain text URL-like links connect to websites, emails, Usenet
  37. ;; messages, BBDB entries, and any files related to the projects. For
  38. ;; printing and sharing of notes, an Org-mode file can be exported as a
  39. ;; structured ASCII file, as HTML, or (todo and agenda items only) as an
  40. ;; iCalendar file. It can also serve as a publishing tool for a set of
  41. ;; linked webpages.
  42. ;;
  43. ;; Installation and Activation
  44. ;; ---------------------------
  45. ;; See the corresponding sections in the manual at
  46. ;;
  47. ;; http://orgmode.org/org.html#Installation
  48. ;;
  49. ;; Documentation
  50. ;; -------------
  51. ;; The documentation of Org-mode can be found in the TeXInfo file. The
  52. ;; distribution also contains a PDF version of it. At the homepage of
  53. ;; Org-mode, you can read the same text online as HTML. There is also an
  54. ;; excellent reference card made by Philip Rooke. This card can be found
  55. ;; in the etc/ directory of Emacs 22.
  56. ;;
  57. ;; A list of recent changes can be found at
  58. ;; http://orgmode.org/Changes.html
  59. ;;
  60. ;;; Code:
  61. (defvar org-inhibit-highlight-removal nil) ; dynamically scoped param
  62. (defvar-local org-table-formula-constants-local nil
  63. "Local version of `org-table-formula-constants'.")
  64. ;;;; Require other packages
  65. (require 'cl-lib)
  66. (eval-when-compile (require 'gnus-sum))
  67. (require 'calendar)
  68. (require 'find-func)
  69. (require 'format-spec)
  70. (or (equal this-command 'eval-buffer)
  71. (condition-case nil
  72. (load (concat (file-name-directory load-file-name)
  73. "org-loaddefs.el")
  74. nil t t t)
  75. (error
  76. (message "WARNING: No org-loaddefs.el file could be found from where org.el is loaded.")
  77. (sit-for 3)
  78. (message "You need to run \"make\" or \"make autoloads\" from Org lisp directory")
  79. (sit-for 3))))
  80. (require 'org-macs)
  81. (require 'org-compat)
  82. ;; `org-outline-regexp' ought to be a defconst but is let-bound in
  83. ;; some places -- e.g. see the macro `org-with-limited-levels'.
  84. ;;
  85. ;; In Org buffers, the value of `outline-regexp' is that of
  86. ;; `org-outline-regexp'. The only function still directly relying on
  87. ;; `outline-regexp' is `org-overview' so that `org-cycle' can do its
  88. ;; job when `orgstruct-mode' is active.
  89. (defvar org-outline-regexp "\\*+ "
  90. "Regexp to match Org headlines.")
  91. (defvar org-outline-regexp-bol "^\\*+ "
  92. "Regexp to match Org headlines.
  93. This is similar to `org-outline-regexp' but additionally makes
  94. sure that we are at the beginning of the line.")
  95. (defvar org-heading-regexp "^\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  96. "Matches a headline, putting stars and text into groups.
  97. Stars are put in group 1 and the trimmed body in group 2.")
  98. (declare-function calendar-check-holidays "holidays" (&optional date))
  99. (declare-function cdlatex-environment "ext:cdlatex" (environment item))
  100. (declare-function org-add-archive-files "org-archive" (files))
  101. (declare-function org-agenda-entry-get-agenda-timestamp "org-agenda" (pom))
  102. (declare-function org-agenda-list "org-agenda" (&optional arg start-day span))
  103. (declare-function org-agenda-redo "org-agenda" (&optional all))
  104. (declare-function org-babel-do-in-edit-buffer "ob-core" (&rest body))
  105. (declare-function org-babel-tangle-file "ob-tangle" (file &optional target-file lang))
  106. (declare-function org-beamer-mode "ox-beamer" ())
  107. (declare-function org-clock-get-last-clock-out-time "org-clock" ())
  108. (declare-function org-clock-out "org-clock" (&optional switch-to-state fail-quietly at-time))
  109. (declare-function org-clock-remove-overlays "org-clock" (&optional beg end noremove))
  110. (declare-function org-clock-sum "org-clock" (&optional tstart tend headline-filter propname))
  111. (declare-function org-clock-sum-current-item "org-clock" (&optional tstart))
  112. (declare-function org-clock-timestamps-down "org-clock" (&optional n))
  113. (declare-function org-clock-timestamps-up "org-clock" (&optional n))
  114. (declare-function org-clock-update-time-maybe "org-clock" ())
  115. (declare-function org-clocktable-shift "org-clock" (dir n))
  116. (declare-function org-element-at-point "org-element" ())
  117. (declare-function org-element-cache-refresh "org-element" (pos))
  118. (declare-function org-element-cache-reset "org-element" (&optional all))
  119. (declare-function org-element-contents "org-element" (element))
  120. (declare-function org-element-context "org-element" (&optional element))
  121. (declare-function org-element-copy "org-element" (datum))
  122. (declare-function org-element-interpret-data "org-element" (data &optional parent))
  123. (declare-function org-element-lineage "org-element" (blob &optional types with-self))
  124. (declare-function org-element-nested-p "org-element" (elem-a elem-b))
  125. (declare-function org-element-parse-buffer "org-element" (&optional granularity visible-only))
  126. (declare-function org-element-property "org-element" (property element))
  127. (declare-function org-element-put-property "org-element" (element property value))
  128. (declare-function org-element-swap-A-B "org-element" (elem-a elem-b))
  129. (declare-function org-element-type "org-element" (element))
  130. (declare-function org-element-update-syntax "org-element" ())
  131. (declare-function org-id-find-id-file "org-id" (id))
  132. (declare-function org-id-get-create "org-id" (&optional force))
  133. (declare-function org-inlinetask-at-task-p "org-inlinetask" ())
  134. (declare-function org-inlinetask-outline-regexp "org-inlinetask" ())
  135. (declare-function org-inlinetask-toggle-visibility "org-inlinetask" ())
  136. (declare-function org-plot/gnuplot "org-plot" (&optional params))
  137. (declare-function org-pop-to-buffer-same-window "org-compat" (&optional buffer-or-name norecord label))
  138. (declare-function org-table-align "org-table" ())
  139. (declare-function org-table-begin "org-table" (&optional table-type))
  140. (declare-function org-table-beginning-of-field "org-table" (&optional n))
  141. (declare-function org-table-blank-field "org-table" ())
  142. (declare-function org-table-calc-current-TBLFM "org-table" (&optional arg))
  143. (declare-function org-table-edit-field "org-table" (arg))
  144. (declare-function org-table-end "org-table" (&optional table-type))
  145. (declare-function org-table-end-of-field "org-table" (&optional n))
  146. (declare-function org-table-insert-row "org-table" (&optional arg))
  147. (declare-function org-table-justify-field-maybe "org-table" (&optional new))
  148. (declare-function org-table-maybe-eval-formula "org-table" ())
  149. (declare-function org-table-maybe-recalculate-line "org-table" ())
  150. (declare-function org-table-next-row "org-table" ())
  151. (declare-function org-table-paste-rectangle "org-table" ())
  152. (declare-function org-table-wrap-region "org-table" (arg))
  153. (declare-function org-tags-view "org-agenda" (&optional todo-only match))
  154. (declare-function orgtbl-ascii-plot "org-table" (&optional ask))
  155. (declare-function orgtbl-mode "org-table" (&optional arg))
  156. (defsubst org-uniquify (list)
  157. "Non-destructively remove duplicate elements from LIST."
  158. (let ((res (copy-sequence list))) (delete-dups res)))
  159. (defsubst org-get-at-bol (property)
  160. "Get text property PROPERTY at the beginning of line."
  161. (get-text-property (point-at-bol) property))
  162. (defsubst org-trim (s)
  163. "Remove whitespace at the beginning and the end of string S."
  164. (replace-regexp-in-string
  165. "\\`[ \t\n\r]+" ""
  166. (replace-regexp-in-string "[ \t\n\r]+\\'" "" s)))
  167. ;; load languages based on value of `org-babel-load-languages'
  168. (defvar org-babel-load-languages)
  169. ;;;###autoload
  170. (defun org-babel-do-load-languages (sym value)
  171. "Load the languages defined in `org-babel-load-languages'."
  172. (set-default sym value)
  173. (dolist (pair org-babel-load-languages)
  174. (let ((active (cdr pair)) (lang (symbol-name (car pair))))
  175. (if active
  176. (require (intern (concat "ob-" lang)))
  177. (funcall 'fmakunbound
  178. (intern (concat "org-babel-execute:" lang)))
  179. (funcall 'fmakunbound
  180. (intern (concat "org-babel-expand-body:" lang)))))))
  181. (declare-function org-babel-tangle-file "ob-tangle" (file &optional target-file lang))
  182. ;;;###autoload
  183. (defun org-babel-load-file (file &optional compile)
  184. "Load Emacs Lisp source code blocks in the Org-mode FILE.
  185. This function exports the source code using `org-babel-tangle'
  186. and then loads the resulting file using `load-file'. With prefix
  187. arg (noninteractively: 2nd arg) COMPILE the tangled Emacs Lisp
  188. file to byte-code before it is loaded."
  189. (interactive "fFile to load: \nP")
  190. (let* ((age (lambda (file)
  191. (float-time
  192. (time-subtract (current-time)
  193. (nth 5 (or (file-attributes (file-truename file))
  194. (file-attributes file)))))))
  195. (base-name (file-name-sans-extension file))
  196. (exported-file (concat base-name ".el")))
  197. ;; tangle if the org-mode file is newer than the elisp file
  198. (unless (and (file-exists-p exported-file)
  199. (> (funcall age file) (funcall age exported-file)))
  200. ;; Tangle-file traversal returns reversed list of tangled files
  201. ;; and we want to evaluate the first target.
  202. (setq exported-file
  203. (car (last (org-babel-tangle-file file exported-file "emacs-lisp")))))
  204. (message "%s %s"
  205. (if compile
  206. (progn (byte-compile-file exported-file 'load)
  207. "Compiled and loaded")
  208. (progn (load-file exported-file) "Loaded"))
  209. exported-file)))
  210. (defcustom org-babel-load-languages '((emacs-lisp . t))
  211. "Languages which can be evaluated in Org-mode buffers.
  212. This list can be used to load support for any of the languages
  213. below, note that each language will depend on a different set of
  214. system executables and/or Emacs modes. When a language is
  215. \"loaded\", then code blocks in that language can be evaluated
  216. with `org-babel-execute-src-block' bound by default to C-c
  217. C-c (note the `org-babel-no-eval-on-ctrl-c-ctrl-c' variable can
  218. be set to remove code block evaluation from the C-c C-c
  219. keybinding. By default only Emacs Lisp (which has no
  220. requirements) is loaded."
  221. :group 'org-babel
  222. :set 'org-babel-do-load-languages
  223. :version "24.1"
  224. :type '(alist :tag "Babel Languages"
  225. :key-type
  226. (choice
  227. (const :tag "Awk" awk)
  228. (const :tag "C" C)
  229. (const :tag "R" R)
  230. (const :tag "Asymptote" asymptote)
  231. (const :tag "Calc" calc)
  232. (const :tag "Clojure" clojure)
  233. (const :tag "CSS" css)
  234. (const :tag "Ditaa" ditaa)
  235. (const :tag "Dot" dot)
  236. (const :tag "Emacs Lisp" emacs-lisp)
  237. (const :tag "Forth" forth)
  238. (const :tag "Fortran" fortran)
  239. (const :tag "Gnuplot" gnuplot)
  240. (const :tag "Haskell" haskell)
  241. (const :tag "IO" io)
  242. (const :tag "J" J)
  243. (const :tag "Java" java)
  244. (const :tag "Javascript" js)
  245. (const :tag "LaTeX" latex)
  246. (const :tag "Ledger" ledger)
  247. (const :tag "Lilypond" lilypond)
  248. (const :tag "Lisp" lisp)
  249. (const :tag "Makefile" makefile)
  250. (const :tag "Maxima" maxima)
  251. (const :tag "Matlab" matlab)
  252. (const :tag "Mscgen" mscgen)
  253. (const :tag "Ocaml" ocaml)
  254. (const :tag "Octave" octave)
  255. (const :tag "Org" org)
  256. (const :tag "Perl" perl)
  257. (const :tag "Pico Lisp" picolisp)
  258. (const :tag "PlantUML" plantuml)
  259. (const :tag "Python" python)
  260. (const :tag "Ruby" ruby)
  261. (const :tag "Sass" sass)
  262. (const :tag "Scala" scala)
  263. (const :tag "Scheme" scheme)
  264. (const :tag "Screen" screen)
  265. (const :tag "Shell Script" shell)
  266. (const :tag "Shen" shen)
  267. (const :tag "Sql" sql)
  268. (const :tag "Sqlite" sqlite)
  269. (const :tag "Stan" stan)
  270. (const :tag "ebnf2ps" ebnf2ps))
  271. :value-type (boolean :tag "Activate" :value t)))
  272. ;;;; Customization variables
  273. (defcustom org-clone-delete-id nil
  274. "Remove ID property of clones of a subtree.
  275. When non-nil, clones of a subtree don't inherit the ID property.
  276. Otherwise they inherit the ID property with a new unique
  277. identifier."
  278. :type 'boolean
  279. :version "24.1"
  280. :group 'org-id)
  281. ;;; Version
  282. (org-check-version)
  283. ;;;###autoload
  284. (defun org-version (&optional here full message)
  285. "Show the org-mode version.
  286. Interactively, or when MESSAGE is non-nil, show it in echo area.
  287. With prefix argument, or when HERE is non-nil, insert it at point.
  288. In non-interactive uses, a reduced version string is output unless
  289. FULL is given."
  290. (interactive (list current-prefix-arg t (not current-prefix-arg)))
  291. (let ((org-dir (ignore-errors (org-find-library-dir "org")))
  292. (save-load-suffixes (when (boundp 'load-suffixes) load-suffixes))
  293. (load-suffixes (list ".el"))
  294. (org-install-dir
  295. (ignore-errors (org-find-library-dir "org-loaddefs"))))
  296. (unless (and (fboundp 'org-release) (fboundp 'org-git-version))
  297. (org-load-noerror-mustsuffix (concat org-dir "org-version")))
  298. (let* ((load-suffixes save-load-suffixes)
  299. (release (org-release))
  300. (git-version (org-git-version))
  301. (version (format "Org-mode version %s (%s @ %s)"
  302. release
  303. git-version
  304. (if org-install-dir
  305. (if (string= org-dir org-install-dir)
  306. org-install-dir
  307. (concat "mixed installation! "
  308. org-install-dir
  309. " and "
  310. org-dir))
  311. "org-loaddefs.el can not be found!")))
  312. (version1 (if full version release)))
  313. (when here (insert version1))
  314. (when message (message "%s" version1))
  315. version1)))
  316. (defconst org-version (org-version))
  317. ;;; Syntax Constants
  318. ;;;; Block
  319. (defconst org-block-regexp
  320. "^[ \t]*#\\+begin_?\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_?\\1[ \t]*$"
  321. "Regular expression for hiding blocks.")
  322. (defconst org-dblock-start-re
  323. "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  324. "Matches the start line of a dynamic block, with parameters.")
  325. (defconst org-dblock-end-re "^[ \t]*#\\+\\(?:END\\|end\\)\\([: \t\r\n]\\|$\\)"
  326. "Matches the end of a dynamic block.")
  327. ;;;; Clock and Planning
  328. (defconst org-clock-string "CLOCK:"
  329. "String used as prefix for timestamps clocking work hours on an item.")
  330. (defvar org-closed-string "CLOSED:"
  331. "String used as the prefix for timestamps logging closing a TODO entry.")
  332. (defvar org-deadline-string "DEADLINE:"
  333. "String to mark deadline entries.
  334. A deadline is this string, followed by a time stamp. Should be a word,
  335. terminated by a colon. You can insert a schedule keyword and
  336. a timestamp with \\[org-deadline].")
  337. (defvar org-scheduled-string "SCHEDULED:"
  338. "String to mark scheduled TODO entries.
  339. A schedule is this string, followed by a time stamp. Should be a word,
  340. terminated by a colon. You can insert a schedule keyword and
  341. a timestamp with \\[org-schedule].")
  342. (defconst org-ds-keyword-length
  343. (+ 2
  344. (apply #'max
  345. (mapcar #'length
  346. (list org-deadline-string org-scheduled-string
  347. org-clock-string org-closed-string))))
  348. "Maximum length of the DEADLINE and SCHEDULED keywords.")
  349. (defconst org-planning-line-re
  350. (concat "^[ \t]*"
  351. (regexp-opt
  352. (list org-closed-string org-deadline-string org-scheduled-string)
  353. t))
  354. "Matches a line with planning info.
  355. Matched keyword is in group 1.")
  356. (defconst org-clock-line-re
  357. (concat "^[ \t]*" org-clock-string)
  358. "Matches a line with clock info.")
  359. (defconst org-deadline-regexp (concat "\\<" org-deadline-string)
  360. "Matches the DEADLINE keyword.")
  361. (defconst org-deadline-time-regexp
  362. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  363. "Matches the DEADLINE keyword together with a time stamp.")
  364. (defconst org-deadline-time-hour-regexp
  365. (concat "\\<" org-deadline-string
  366. " *<\\([^>]+[0-9]\\{1,2\\}:[0-9]\\{2\\}[0-9-+:hdwmy \t.]*\\)>")
  367. "Matches the DEADLINE keyword together with a time-and-hour stamp.")
  368. (defconst org-deadline-line-regexp
  369. (concat "\\<\\(" org-deadline-string "\\).*")
  370. "Matches the DEADLINE keyword and the rest of the line.")
  371. (defconst org-scheduled-regexp (concat "\\<" org-scheduled-string)
  372. "Matches the SCHEDULED keyword.")
  373. (defconst org-scheduled-time-regexp
  374. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  375. "Matches the SCHEDULED keyword together with a time stamp.")
  376. (defconst org-scheduled-time-hour-regexp
  377. (concat "\\<" org-scheduled-string
  378. " *<\\([^>]+[0-9]\\{1,2\\}:[0-9]\\{2\\}[0-9-+:hdwmy \t.]*\\)>")
  379. "Matches the SCHEDULED keyword together with a time-and-hour stamp.")
  380. (defconst org-closed-time-regexp
  381. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  382. "Matches the CLOSED keyword together with a time stamp.")
  383. (defconst org-keyword-time-regexp
  384. (concat "\\<"
  385. (regexp-opt
  386. (list org-scheduled-string org-deadline-string org-closed-string
  387. org-clock-string)
  388. t)
  389. " *[[<]\\([^]>]+\\)[]>]")
  390. "Matches any of the 4 keywords, together with the time stamp.")
  391. (defconst org-keyword-time-not-clock-regexp
  392. (concat
  393. "\\<"
  394. (regexp-opt
  395. (list org-scheduled-string org-deadline-string org-closed-string) t)
  396. " *[[<]\\([^]>]+\\)[]>]")
  397. "Matches any of the 3 keywords, together with the time stamp.")
  398. (defconst org-maybe-keyword-time-regexp
  399. (concat "\\(\\<"
  400. (regexp-opt
  401. (list org-scheduled-string org-deadline-string org-closed-string
  402. org-clock-string)
  403. t)
  404. "\\)?"
  405. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?[]>]"
  406. "\\|"
  407. "<%%([^\r\n>]*>\\)")
  408. "Matches a timestamp, possibly preceded by a keyword.")
  409. (defconst org-all-time-keywords
  410. (mapcar (lambda (w) (substring w 0 -1))
  411. (list org-scheduled-string org-deadline-string
  412. org-clock-string org-closed-string))
  413. "List of time keywords.")
  414. ;;;; Drawer
  415. (defconst org-drawer-regexp "^[ \t]*:\\(\\(?:\\w\\|[-_]\\)+\\):[ \t]*$"
  416. "Matches first or last line of a hidden block.
  417. Group 1 contains drawer's name or \"END\".")
  418. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  419. "Regular expression matching the first line of a property drawer.")
  420. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  421. "Regular expression matching the last line of a property drawer.")
  422. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  423. "Regular expression matching the first line of a clock drawer.")
  424. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  425. "Regular expression matching the last line of a clock drawer.")
  426. (defconst org-property-drawer-re
  427. (concat "^[ \t]*:PROPERTIES:[ \t]*\n"
  428. "\\(?:[ \t]*:\\S-+:\\(?: .*\\)?[ \t]*\n\\)*?"
  429. "[ \t]*:END:[ \t]*$")
  430. "Matches an entire property drawer.")
  431. (defconst org-clock-drawer-re
  432. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*?\\("
  433. org-clock-drawer-end-re "\\)\n?")
  434. "Matches an entire clock drawer.")
  435. ;;;; Headline
  436. (defconst org-heading-keyword-regexp-format
  437. "^\\(\\*+\\)\\(?: +%s\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  438. "Printf format for a regexp matching a headline with some keyword.
  439. This regexp will match the headline of any node which has the
  440. exact keyword that is put into the format. The keyword isn't in
  441. any group by default, but the stars and the body are.")
  442. (defconst org-heading-keyword-maybe-regexp-format
  443. "^\\(\\*+\\)\\(?: +%s\\)?\\(?: +\\(.*?\\)\\)?[ \t]*$"
  444. "Printf format for a regexp matching a headline, possibly with some keyword.
  445. This regexp can match any headline with the specified keyword, or
  446. without a keyword. The keyword isn't in any group by default,
  447. but the stars and the body are.")
  448. (defconst org-archive-tag "ARCHIVE"
  449. "The tag that marks a subtree as archived.
  450. An archived subtree does not open during visibility cycling, and does
  451. not contribute to the agenda listings.")
  452. (defconst org-comment-string "COMMENT"
  453. "Entries starting with this keyword will never be exported.
  454. An entry can be toggled between COMMENT and normal with
  455. \\[org-toggle-comment].")
  456. ;;;; LaTeX Environments and Fragments
  457. (defconst org-latex-regexps
  458. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  459. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  460. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  461. ("$1" "\\([^$]\\|^\\)\\(\\$[^ \r\n,;.$]\\$\\)\\(\\s.\\|\\s-\\|\\s(\\|\\s)\\|\\s\"\\|\000\\|$\\)" 2 nil)
  462. ("$" "\\([^$]\\|^\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\(\\s.\\|\\s-\\|\\s(\\|\\s)\\|\\s\"\\|\000\\|$\\)" 2 nil)
  463. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  464. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
  465. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
  466. "Regular expressions for matching embedded LaTeX.")
  467. ;;;; Node Property
  468. (defconst org-effort-property "Effort"
  469. "The property that is being used to keep track of effort estimates.
  470. Effort estimates given in this property need to have the format H:MM.")
  471. ;;;; Table
  472. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  473. "Detect an org-type or table-type table.")
  474. (defconst org-table-line-regexp "^[ \t]*|"
  475. "Detect an org-type table line.")
  476. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  477. "Detect an org-type table line.")
  478. (defconst org-table-hline-regexp "^[ \t]*|-"
  479. "Detect an org-type table hline.")
  480. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  481. "Detect a table-type table hline.")
  482. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  483. "Detect the first line outside a table when searching from within it.
  484. This works for both table types.")
  485. (defconst org-TBLFM-regexp "^[ \t]*#\\+TBLFM: "
  486. "Detect a #+TBLFM line.")
  487. ;;;; Timestamp
  488. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^\r\n>]*?\\)>"
  489. "Regular expression for fast time stamp matching.")
  490. (defconst org-ts-regexp-inactive
  491. "\\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^\r\n>]*?\\)\\]"
  492. "Regular expression for fast inactive time stamp matching.")
  493. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?\\)[]>]"
  494. "Regular expression for fast time stamp matching.")
  495. (defconst org-ts-regexp0
  496. "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\)\\( +[^]+0-9>\r\n -]+\\)?\\( +\\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  497. "Regular expression matching time strings for analysis.
  498. This one does not require the space after the date, so it can be used
  499. on a string that terminates immediately after the date.")
  500. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]+0-9>\r\n -]*\\)\\( \\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  501. "Regular expression matching time strings for analysis.")
  502. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  503. "Regular expression matching time stamps, with groups.")
  504. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  505. "Regular expression matching time stamps (also [..]), with groups.")
  506. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  507. "Regular expression matching a time stamp range.")
  508. (defconst org-tr-regexp-both
  509. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  510. "Regular expression matching a time stamp range.")
  511. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  512. org-ts-regexp "\\)?")
  513. "Regular expression matching a time stamp or time stamp range.")
  514. (defconst org-tsr-regexp-both
  515. (concat org-ts-regexp-both "\\(--?-?"
  516. org-ts-regexp-both "\\)?")
  517. "Regular expression matching a time stamp or time stamp range.
  518. The time stamps may be either active or inactive.")
  519. (defconst org-repeat-re
  520. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*?\\([.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)"
  521. "Regular expression for specifying repeated events.
  522. After a match, group 1 contains the repeat expression.")
  523. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  524. "Formats for `format-time-string' which are used for time stamps.")
  525. ;;; The custom variables
  526. (defgroup org nil
  527. "Outline-based notes management and organizer."
  528. :tag "Org"
  529. :group 'outlines
  530. :group 'calendar)
  531. (defcustom org-mode-hook nil
  532. "Mode hook for Org-mode, run after the mode was turned on."
  533. :group 'org
  534. :type 'hook)
  535. (defcustom org-load-hook nil
  536. "Hook that is run after org.el has been loaded."
  537. :group 'org
  538. :type 'hook)
  539. (defcustom org-log-buffer-setup-hook nil
  540. "Hook that is run after an Org log buffer is created."
  541. :group 'org
  542. :version "24.1"
  543. :type 'hook)
  544. (defvar org-modules) ; defined below
  545. (defvar org-modules-loaded nil
  546. "Have the modules been loaded already?")
  547. (defun org-load-modules-maybe (&optional force)
  548. "Load all extensions listed in `org-modules'."
  549. (when (or force (not org-modules-loaded))
  550. (dolist (ext org-modules)
  551. (condition-case nil (require ext)
  552. (error (message "Problems while trying to load feature `%s'" ext))))
  553. (setq org-modules-loaded t)))
  554. (defun org-set-modules (var value)
  555. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  556. (set var value)
  557. (when (featurep 'org)
  558. (org-load-modules-maybe 'force)
  559. (org-element-cache-reset 'all)))
  560. (defcustom org-modules '(org-w3m org-bbdb org-bibtex org-docview org-gnus org-info org-irc org-mhe org-rmail)
  561. "Modules that should always be loaded together with org.el.
  562. If a description starts with <C>, the file is not part of Emacs
  563. and loading it will require that you have downloaded and properly
  564. installed the Org mode distribution.
  565. You can also use this system to load external packages (i.e. neither Org
  566. core modules, nor modules from the CONTRIB directory). Just add symbols
  567. to the end of the list. If the package is called org-xyz.el, then you need
  568. to add the symbol `xyz', and the package must have a call to:
  569. (provide \\='org-xyz)
  570. For export specific modules, see also `org-export-backends'."
  571. :group 'org
  572. :set 'org-set-modules
  573. :version "24.4"
  574. :package-version '(Org . "8.0")
  575. :type
  576. '(set :greedy t
  577. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  578. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  579. (const :tag " crypt: Encryption of subtrees" org-crypt)
  580. (const :tag " ctags: Access to Emacs tags with links" org-ctags)
  581. (const :tag " docview: Links to doc-view buffers" org-docview)
  582. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  583. (const :tag " habit: Track your consistency with habits" org-habit)
  584. (const :tag " id: Global IDs for identifying entries" org-id)
  585. (const :tag " info: Links to Info nodes" org-info)
  586. (const :tag " inlinetask: Tasks independent of outline hierarchy" org-inlinetask)
  587. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  588. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  589. (const :tag " mouse: Additional mouse support" org-mouse)
  590. (const :tag " protocol: Intercept calls from emacsclient" org-protocol)
  591. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  592. (const :tag " w3m: Special cut/paste from w3m to Org-mode." org-w3m)
  593. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  594. (const :tag "C bookmark: Org-mode links to bookmarks" org-bookmark)
  595. (const :tag "C bullets: Add overlays to headlines stars" org-bullets)
  596. (const :tag "C checklist: Extra functions for checklists in repeated tasks" org-checklist)
  597. (const :tag "C choose: Use TODO keywords to mark decisions states" org-choose)
  598. (const :tag "C collector: Collect properties into tables" org-collector)
  599. (const :tag "C depend: TODO dependencies for Org-mode\n\t\t\t(PARTIALLY OBSOLETE, see built-in dependency support))" org-depend)
  600. (const :tag "C drill: Flashcards and spaced repetition for Org-mode" org-drill)
  601. (const :tag "C elisp-symbol: Org-mode links to emacs-lisp symbols" org-elisp-symbol)
  602. (const :tag "C eshell Support for links to working directories in eshell" org-eshell)
  603. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  604. (const :tag "C eval: Include command output as text" org-eval)
  605. (const :tag "C eww: Store link to url of eww" org-eww)
  606. (const :tag "C expiry: Expiry mechanism for Org-mode entries" org-expiry)
  607. (const :tag "C favtable: Lookup table of favorite references and links" org-favtable)
  608. (const :tag "C git-link: Provide org links to specific file version" org-git-link)
  609. (const :tag "C interactive-query: Interactive modification of tags query\n\t\t\t(PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query)
  610. (const :tag "C invoice: Help manage client invoices in Org-mode" org-invoice)
  611. (const :tag "C learn: SuperMemo's incremental learning algorithm" org-learn)
  612. (const :tag "C mac-iCal Imports events from iCal.app to the Emacs diary" org-mac-iCal)
  613. (const :tag "C mac-link: Grab links and url from various mac Applications" org-mac-link)
  614. (const :tag "C mairix: Hook mairix search into Org-mode for different MUAs" org-mairix)
  615. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  616. (const :tag "C mew: Links to Mew folders/messages" org-mew)
  617. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  618. (const :tag "C notmuch: Provide org links to notmuch searches or messages" org-notmuch)
  619. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  620. (const :tag "C registry: A registry for Org-mode links" org-registry)
  621. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  622. (const :tag "C secretary: Team management with org-mode" org-secretary)
  623. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  624. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  625. (const :tag "C track: Keep up with Org-mode development" org-track)
  626. (const :tag "C velocity Something like Notational Velocity for Org" org-velocity)
  627. (const :tag "C vm: Links to VM folders/messages" org-vm)
  628. (const :tag "C wikinodes: CamelCase wiki-like links" org-wikinodes)
  629. (const :tag "C wl: Links to Wanderlust folders/messages" org-wl)
  630. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  631. (defvar org-export-registered-backends) ; From ox.el.
  632. (declare-function org-export-derived-backend-p "ox" (backend &rest backends))
  633. (declare-function org-export-backend-name "ox" (backend))
  634. (defcustom org-export-backends '(ascii html icalendar latex)
  635. "List of export back-ends that should be always available.
  636. If a description starts with <C>, the file is not part of Emacs
  637. and loading it will require that you have downloaded and properly
  638. installed the Org mode distribution.
  639. Unlike to `org-modules', libraries in this list will not be
  640. loaded along with Org, but only once the export framework is
  641. needed.
  642. This variable needs to be set before org.el is loaded. If you
  643. need to make a change while Emacs is running, use the customize
  644. interface or run the following code, where VAL stands for the new
  645. value of the variable, after updating it:
  646. (progn
  647. (setq org-export-registered-backends
  648. (cl-remove-if-not
  649. (lambda (backend)
  650. (let ((name (org-export-backend-name backend)))
  651. (or (memq name val)
  652. (catch \\='parentp
  653. (dolist (b val)
  654. (and (org-export-derived-backend-p b name)
  655. (throw \\='parentp t)))))))
  656. org-export-registered-backends))
  657. (let ((new-list (mapcar #\\='org-export-backend-name
  658. org-export-registered-backends)))
  659. (dolist (backend val)
  660. (cond
  661. ((not (load (format \"ox-%s\" backend) t t))
  662. (message \"Problems while trying to load export back-end \\=`%s\\='\"
  663. backend))
  664. ((not (memq backend new-list)) (push backend new-list))))
  665. (set-default \\='org-export-backends new-list)))
  666. Adding a back-end to this list will also pull the back-end it
  667. depends on, if any."
  668. :group 'org
  669. :group 'org-export
  670. :version "24.4"
  671. :package-version '(Org . "8.0")
  672. :initialize 'custom-initialize-set
  673. :set (lambda (var val)
  674. (if (not (featurep 'ox)) (set-default var val)
  675. ;; Any back-end not required anymore (not present in VAL and not
  676. ;; a parent of any back-end in the new value) is removed from the
  677. ;; list of registered back-ends.
  678. (setq org-export-registered-backends
  679. (cl-remove-if-not
  680. (lambda (backend)
  681. (let ((name (org-export-backend-name backend)))
  682. (or (memq name val)
  683. (catch 'parentp
  684. (dolist (b val)
  685. (and (org-export-derived-backend-p b name)
  686. (throw 'parentp t)))))))
  687. org-export-registered-backends))
  688. ;; Now build NEW-LIST of both new back-ends and required
  689. ;; parents.
  690. (let ((new-list (mapcar #'org-export-backend-name
  691. org-export-registered-backends)))
  692. (dolist (backend val)
  693. (cond
  694. ((not (load (format "ox-%s" backend) t t))
  695. (message "Problems while trying to load export back-end `%s'"
  696. backend))
  697. ((not (memq backend new-list)) (push backend new-list))))
  698. ;; Set VAR to that list with fixed dependencies.
  699. (set-default var new-list))))
  700. :type '(set :greedy t
  701. (const :tag " ascii Export buffer to ASCII format" ascii)
  702. (const :tag " beamer Export buffer to Beamer presentation" beamer)
  703. (const :tag " html Export buffer to HTML format" html)
  704. (const :tag " icalendar Export buffer to iCalendar format" icalendar)
  705. (const :tag " latex Export buffer to LaTeX format" latex)
  706. (const :tag " man Export buffer to MAN format" man)
  707. (const :tag " md Export buffer to Markdown format" md)
  708. (const :tag " odt Export buffer to ODT format" odt)
  709. (const :tag " org Export buffer to Org format" org)
  710. (const :tag " texinfo Export buffer to Texinfo format" texinfo)
  711. (const :tag "C confluence Export buffer to Confluence Wiki format" confluence)
  712. (const :tag "C deck Export buffer to deck.js presentations" deck)
  713. (const :tag "C freemind Export buffer to Freemind mindmap format" freemind)
  714. (const :tag "C groff Export buffer to Groff format" groff)
  715. (const :tag "C koma-letter Export buffer to KOMA Scrlttrl2 format" koma-letter)
  716. (const :tag "C RSS 2.0 Export buffer to RSS 2.0 format" rss)
  717. (const :tag "C s5 Export buffer to s5 presentations" s5)
  718. (const :tag "C taskjuggler Export buffer to TaskJuggler format" taskjuggler)))
  719. (eval-after-load 'ox
  720. '(dolist (backend org-export-backends)
  721. (condition-case nil (require (intern (format "ox-%s" backend)))
  722. (error (message "Problems while trying to load export back-end `%s'"
  723. backend)))))
  724. (defcustom org-support-shift-select nil
  725. "Non-nil means make shift-cursor commands select text when possible.
  726. \\<org-mode-map>\
  727. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys
  728. start selecting a region, or enlarge regions started in this way.
  729. In Org-mode, in special contexts, these same keys are used for
  730. other purposes, important enough to compete with shift selection.
  731. Org tries to balance these needs by supporting `shift-select-mode'
  732. outside these special contexts, under control of this variable.
  733. The default of this variable is nil, to avoid confusing behavior. Shifted
  734. cursor keys will then execute Org commands in the following contexts:
  735. - on a headline, changing TODO state (left/right) and priority (up/down)
  736. - on a time stamp, changing the time
  737. - in a plain list item, changing the bullet type
  738. - in a property definition line, switching between allowed values
  739. - in the BEGIN line of a clock table (changing the time block).
  740. Outside these contexts, the commands will throw an error.
  741. When this variable is t and the cursor is not in a special
  742. context, Org-mode will support shift-selection for making and
  743. enlarging regions. To make this more effective, the bullet
  744. cycling will no longer happen anywhere in an item line, but only
  745. if the cursor is exactly on the bullet.
  746. If you set this variable to the symbol `always', then the keys
  747. will not be special in headlines, property lines, and item lines,
  748. to make shift selection work there as well. If this is what you
  749. want, you can use the following alternative commands:
  750. `\\[org-todo]' and `\\[org-priority]' \
  751. to change TODO state and priority,
  752. `\\[universal-argument] \\[universal-argument] \\[org-todo]' \
  753. can be used to switch TODO sets,
  754. `\\[org-ctrl-c-minus]' to cycle item bullet types,
  755. and properties can be edited by hand or in column view.
  756. However, when the cursor is on a timestamp, shift-cursor commands
  757. will still edit the time stamp - this is just too good to give up.
  758. XEmacs user should have this variable set to nil, because
  759. `shift-select-mode' is in Emacs 23 or later only."
  760. :group 'org
  761. :type '(choice
  762. (const :tag "Never" nil)
  763. (const :tag "When outside special context" t)
  764. (const :tag "Everywhere except timestamps" always)))
  765. (defcustom org-loop-over-headlines-in-active-region nil
  766. "Shall some commands act upon headlines in the active region?
  767. When set to t, some commands will be performed in all headlines
  768. within the active region.
  769. When set to `start-level', some commands will be performed in all
  770. headlines within the active region, provided that these headlines
  771. are of the same level than the first one.
  772. When set to a string, those commands will be performed on the
  773. matching headlines within the active region. Such string must be
  774. a tags/property/todo match as it is used in the agenda tags view.
  775. The list of commands is: `org-schedule', `org-deadline',
  776. `org-todo', `org-archive-subtree', `org-archive-set-tag' and
  777. `org-archive-to-archive-sibling'. The archiving commands skip
  778. already archived entries."
  779. :type '(choice (const :tag "Don't loop" nil)
  780. (const :tag "All headlines in active region" t)
  781. (const :tag "In active region, headlines at the same level than the first one" start-level)
  782. (string :tag "Tags/Property/Todo matcher"))
  783. :version "24.1"
  784. :group 'org-todo
  785. :group 'org-archive)
  786. (defgroup org-startup nil
  787. "Options concerning startup of Org-mode."
  788. :tag "Org Startup"
  789. :group 'org)
  790. (defcustom org-startup-folded t
  791. "Non-nil means entering Org-mode will switch to OVERVIEW.
  792. This can also be configured on a per-file basis by adding one of
  793. the following lines anywhere in the buffer:
  794. #+STARTUP: fold (or `overview', this is equivalent)
  795. #+STARTUP: nofold (or `showall', this is equivalent)
  796. #+STARTUP: content
  797. #+STARTUP: showeverything
  798. By default, this option is ignored when Org opens agenda files
  799. for the first time. If you want the agenda to honor the startup
  800. option, set `org-agenda-inhibit-startup' to nil."
  801. :group 'org-startup
  802. :type '(choice
  803. (const :tag "nofold: show all" nil)
  804. (const :tag "fold: overview" t)
  805. (const :tag "content: all headlines" content)
  806. (const :tag "show everything, even drawers" showeverything)))
  807. (defcustom org-startup-truncated t
  808. "Non-nil means entering Org-mode will set `truncate-lines'.
  809. This is useful since some lines containing links can be very long and
  810. uninteresting. Also tables look terrible when wrapped."
  811. :group 'org-startup
  812. :type 'boolean)
  813. (defcustom org-startup-indented nil
  814. "Non-nil means turn on `org-indent-mode' on startup.
  815. This can also be configured on a per-file basis by adding one of
  816. the following lines anywhere in the buffer:
  817. #+STARTUP: indent
  818. #+STARTUP: noindent"
  819. :group 'org-structure
  820. :type '(choice
  821. (const :tag "Not" nil)
  822. (const :tag "Globally (slow on startup in large files)" t)))
  823. (defcustom org-use-sub-superscripts t
  824. "Non-nil means interpret \"_\" and \"^\" for display.
  825. If you want to control how Org exports those characters, see
  826. `org-export-with-sub-superscripts'. `org-use-sub-superscripts'
  827. used to be an alias for `org-export-with-sub-superscripts' in
  828. Org <8.0, it is not anymore.
  829. When this option is turned on, you can use TeX-like syntax for
  830. sub- and superscripts within the buffer. Several characters after
  831. \"_\" or \"^\" will be considered as a single item - so grouping
  832. with {} is normally not needed. For example, the following things
  833. will be parsed as single sub- or superscripts:
  834. 10^24 or 10^tau several digits will be considered 1 item.
  835. 10^-12 or 10^-tau a leading sign with digits or a word
  836. x^2-y^3 will be read as x^2 - y^3, because items are
  837. terminated by almost any nonword/nondigit char.
  838. x_{i^2} or x^(2-i) braces or parenthesis do grouping.
  839. Still, ambiguity is possible. So when in doubt, use {} to enclose
  840. the sub/superscript. If you set this variable to the symbol `{}',
  841. the braces are *required* in order to trigger interpretations as
  842. sub/superscript. This can be helpful in documents that need \"_\"
  843. frequently in plain text."
  844. :group 'org-startup
  845. :version "24.4"
  846. :package-version '(Org . "8.0")
  847. :type '(choice
  848. (const :tag "Always interpret" t)
  849. (const :tag "Only with braces" {})
  850. (const :tag "Never interpret" nil)))
  851. (defcustom org-startup-with-beamer-mode nil
  852. "Non-nil means turn on `org-beamer-mode' on startup.
  853. This can also be configured on a per-file basis by adding one of
  854. the following lines anywhere in the buffer:
  855. #+STARTUP: beamer"
  856. :group 'org-startup
  857. :version "24.1"
  858. :type 'boolean)
  859. (defcustom org-startup-align-all-tables nil
  860. "Non-nil means align all tables when visiting a file.
  861. This is useful when the column width in tables is forced with <N> cookies
  862. in table fields. Such tables will look correct only after the first re-align.
  863. This can also be configured on a per-file basis by adding one of
  864. the following lines anywhere in the buffer:
  865. #+STARTUP: align
  866. #+STARTUP: noalign"
  867. :group 'org-startup
  868. :type 'boolean)
  869. (defcustom org-startup-with-inline-images nil
  870. "Non-nil means show inline images when loading a new Org file.
  871. This can also be configured on a per-file basis by adding one of
  872. the following lines anywhere in the buffer:
  873. #+STARTUP: inlineimages
  874. #+STARTUP: noinlineimages"
  875. :group 'org-startup
  876. :version "24.1"
  877. :type 'boolean)
  878. (defcustom org-startup-with-latex-preview nil
  879. "Non-nil means preview LaTeX fragments when loading a new Org file.
  880. This can also be configured on a per-file basis by adding one of
  881. the following lines anywhere in the buffer:
  882. #+STARTUP: latexpreview
  883. #+STARTUP: nolatexpreview"
  884. :group 'org-startup
  885. :version "24.4"
  886. :package-version '(Org . "8.0")
  887. :type 'boolean)
  888. (defcustom org-insert-mode-line-in-empty-file nil
  889. "Non-nil means insert the first line setting Org-mode in empty files.
  890. When the function `org-mode' is called interactively in an empty file, this
  891. normally means that the file name does not automatically trigger Org-mode.
  892. To ensure that the file will always be in Org-mode in the future, a
  893. line enforcing Org-mode will be inserted into the buffer, if this option
  894. has been set."
  895. :group 'org-startup
  896. :type 'boolean)
  897. (defcustom org-replace-disputed-keys nil
  898. "Non-nil means use alternative key bindings for some keys.
  899. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  900. These keys are also used by other packages like shift-selection-mode'
  901. \(built into Emacs 23), `CUA-mode' or `windmove.el'.
  902. If you want to use Org-mode together with one of these other modes,
  903. or more generally if you would like to move some Org-mode commands to
  904. other keys, set this variable and configure the keys with the variable
  905. `org-disputed-keys'.
  906. This option is only relevant at load-time of Org-mode, and must be set
  907. *before* org.el is loaded. Changing it requires a restart of Emacs to
  908. become effective."
  909. :group 'org-startup
  910. :type 'boolean)
  911. (defcustom org-use-extra-keys nil
  912. "Non-nil means use extra key sequence definitions for certain commands.
  913. This happens automatically if you run XEmacs or if `window-system'
  914. is nil. This variable lets you do the same manually. You must
  915. set it before loading org.
  916. Example: on Carbon Emacs 22 running graphically, with an external
  917. keyboard on a Powerbook, the default way of setting M-left might
  918. not work for either Alt or ESC. Setting this variable will make
  919. it work for ESC."
  920. :group 'org-startup
  921. :type 'boolean)
  922. (org-defvaralias 'org-CUA-compatible 'org-replace-disputed-keys)
  923. (defcustom org-disputed-keys
  924. '(([(shift up)] . [(meta p)])
  925. ([(shift down)] . [(meta n)])
  926. ([(shift left)] . [(meta -)])
  927. ([(shift right)] . [(meta +)])
  928. ([(control shift right)] . [(meta shift +)])
  929. ([(control shift left)] . [(meta shift -)]))
  930. "Keys for which Org-mode and other modes compete.
  931. This is an alist, cars are the default keys, second element specifies
  932. the alternative to use when `org-replace-disputed-keys' is t.
  933. Keys can be specified in any syntax supported by `define-key'.
  934. The value of this option takes effect only at Org-mode's startup,
  935. therefore you'll have to restart Emacs to apply it after changing."
  936. :group 'org-startup
  937. :type 'alist)
  938. (defun org-key (key)
  939. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  940. Or return the original if not disputed.
  941. Also apply the translations defined in `org-xemacs-key-equivalents'."
  942. (when org-replace-disputed-keys
  943. (let* ((nkey (key-description key))
  944. (x (org-find-if (lambda (x)
  945. (equal (key-description (car x)) nkey))
  946. org-disputed-keys)))
  947. (setq key (if x (cdr x) key))))
  948. (when (featurep 'xemacs)
  949. (setq key (or (cdr (assoc key org-xemacs-key-equivalents)) key)))
  950. key)
  951. (defun org-find-if (predicate seq)
  952. (catch 'exit
  953. (while seq
  954. (if (funcall predicate (car seq))
  955. (throw 'exit (car seq))
  956. (pop seq)))))
  957. (defun org-defkey (keymap key def)
  958. "Define a key, possibly translated, as returned by `org-key'."
  959. (define-key keymap (org-key key) def))
  960. (defcustom org-ellipsis nil
  961. "The ellipsis to use in the Org-mode outline.
  962. When nil, just use the standard three dots.
  963. When a string, use that string instead.
  964. When a face, use the standard 3 dots, but with the specified face.
  965. The change affects only Org-mode (which will then use its own display table).
  966. Changing this requires executing \\[org-mode] in a buffer to become
  967. effective."
  968. :group 'org-startup
  969. :type '(choice (const :tag "Default" nil)
  970. (face :tag "Face" :value org-warning)
  971. (string :tag "String" :value "...#")))
  972. (defvar org-display-table nil
  973. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  974. (defgroup org-keywords nil
  975. "Keywords in Org-mode."
  976. :tag "Org Keywords"
  977. :group 'org)
  978. (defcustom org-closed-keep-when-no-todo nil
  979. "Remove CLOSED: time-stamp when switching back to a non-todo state?"
  980. :group 'org-todo
  981. :group 'org-keywords
  982. :version "24.4"
  983. :package-version '(Org . "8.0")
  984. :type 'boolean)
  985. (defgroup org-structure nil
  986. "Options concerning the general structure of Org-mode files."
  987. :tag "Org Structure"
  988. :group 'org)
  989. (defgroup org-reveal-location nil
  990. "Options about how to make context of a location visible."
  991. :tag "Org Reveal Location"
  992. :group 'org-structure)
  993. (defcustom org-show-context-detail '((isearch . lineage)
  994. (bookmark-jump . lineage)
  995. (default . ancestors))
  996. "Alist between context and visibility span when revealing a location.
  997. \\<org-mode-map>Some actions may move point into invisible
  998. locations. As a consequence, Org always expose a neighborhood
  999. around point. How much is shown depends on the initial action,
  1000. or context. Valid contexts are
  1001. agenda when exposing an entry from the agenda
  1002. org-goto when using the command `org-goto' (\\[org-goto])
  1003. occur-tree when using the command `org-occur' (\\[org-sparse-tree] /)
  1004. tags-tree when constructing a sparse tree based on tags matches
  1005. link-search when exposing search matches associated with a link
  1006. mark-goto when exposing the jump goal of a mark
  1007. bookmark-jump when exposing a bookmark location
  1008. isearch when exiting from an incremental search
  1009. default default for all contexts not set explicitly
  1010. Allowed visibility spans are
  1011. minimal show current headline; if point is not on headline,
  1012. also show entry
  1013. local show current headline, entry and next headline
  1014. ancestors show current headline and its direct ancestors; if
  1015. point is not on headline, also show entry
  1016. lineage show current headline, its direct ancestors and all
  1017. their children; if point is not on headline, also show
  1018. entry and first child
  1019. tree show current headline, its direct ancestors and all
  1020. their children; if point is not on headline, also show
  1021. entry and all children
  1022. canonical show current headline, its direct ancestors along with
  1023. their entries and children; if point is not located on
  1024. the headline, also show current entry and all children
  1025. As special cases, a nil or t value means show all contexts in
  1026. `minimal' or `canonical' view, respectively.
  1027. Some views can make displayed information very compact, but also
  1028. make it harder to edit the location of the match. In such
  1029. a case, use the command `org-reveal' (\\[org-reveal]) to show
  1030. more context."
  1031. :group 'org-reveal-location
  1032. :version "25.1"
  1033. :package-version '(Org . "8.3")
  1034. :type '(choice
  1035. (const :tag "Canonical" t)
  1036. (const :tag "Minimal" nil)
  1037. (repeat :greedy t :tag "Individual contexts"
  1038. (cons
  1039. (choice :tag "Context"
  1040. (const agenda)
  1041. (const org-goto)
  1042. (const occur-tree)
  1043. (const tags-tree)
  1044. (const link-search)
  1045. (const mark-goto)
  1046. (const bookmark-jump)
  1047. (const isearch)
  1048. (const default))
  1049. (choice :tag "Detail level"
  1050. (const minimal)
  1051. (const local)
  1052. (const ancestors)
  1053. (const lineage)
  1054. (const tree)
  1055. (const canonical))))))
  1056. (defcustom org-indirect-buffer-display 'other-window
  1057. "How should indirect tree buffers be displayed?
  1058. This applies to indirect buffers created with the commands
  1059. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  1060. Valid values are:
  1061. current-window Display in the current window
  1062. other-window Just display in another window.
  1063. dedicated-frame Create one new frame, and re-use it each time.
  1064. new-frame Make a new frame each time. Note that in this case
  1065. previously-made indirect buffers are kept, and you need to
  1066. kill these buffers yourself."
  1067. :group 'org-structure
  1068. :group 'org-agenda-windows
  1069. :type '(choice
  1070. (const :tag "In current window" current-window)
  1071. (const :tag "In current frame, other window" other-window)
  1072. (const :tag "Each time a new frame" new-frame)
  1073. (const :tag "One dedicated frame" dedicated-frame)))
  1074. (defcustom org-use-speed-commands nil
  1075. "Non-nil means activate single letter commands at beginning of a headline.
  1076. This may also be a function to test for appropriate locations where speed
  1077. commands should be active.
  1078. For example, to activate speed commands when the point is on any
  1079. star at the beginning of the headline, you can do this:
  1080. (setq org-use-speed-commands
  1081. (lambda () (and (looking-at org-outline-regexp) (looking-back \"^\\**\"))))"
  1082. :group 'org-structure
  1083. :type '(choice
  1084. (const :tag "Never" nil)
  1085. (const :tag "At beginning of headline stars" t)
  1086. (function)))
  1087. (defcustom org-speed-commands-user nil
  1088. "Alist of additional speed commands.
  1089. This list will be checked before `org-speed-commands-default'
  1090. when the variable `org-use-speed-commands' is non-nil
  1091. and when the cursor is at the beginning of a headline.
  1092. The car if each entry is a string with a single letter, which must
  1093. be assigned to `self-insert-command' in the global map.
  1094. The cdr is either a command to be called interactively, a function
  1095. to be called, or a form to be evaluated.
  1096. An entry that is just a list with a single string will be interpreted
  1097. as a descriptive headline that will be added when listing the speed
  1098. commands in the Help buffer using the `?' speed command."
  1099. :group 'org-structure
  1100. :type '(repeat :value ("k" . ignore)
  1101. (choice :value ("k" . ignore)
  1102. (list :tag "Descriptive Headline" (string :tag "Headline"))
  1103. (cons :tag "Letter and Command"
  1104. (string :tag "Command letter")
  1105. (choice
  1106. (function)
  1107. (sexp))))))
  1108. (defcustom org-bookmark-names-plist
  1109. '(:last-capture "org-capture-last-stored"
  1110. :last-refile "org-refile-last-stored"
  1111. :last-capture-marker "org-capture-last-stored-marker")
  1112. "Names for bookmarks automatically set by some Org commands.
  1113. This can provide strings as names for a number of bookmarks Org sets
  1114. automatically. The following keys are currently implemented:
  1115. :last-capture
  1116. :last-capture-marker
  1117. :last-refile
  1118. When a key does not show up in the property list, the corresponding bookmark
  1119. is not set."
  1120. :group 'org-structure
  1121. :type 'plist)
  1122. (defgroup org-cycle nil
  1123. "Options concerning visibility cycling in Org-mode."
  1124. :tag "Org Cycle"
  1125. :group 'org-structure)
  1126. (defcustom org-cycle-skip-children-state-if-no-children t
  1127. "Non-nil means skip CHILDREN state in entries that don't have any."
  1128. :group 'org-cycle
  1129. :type 'boolean)
  1130. (defcustom org-cycle-max-level nil
  1131. "Maximum level which should still be subject to visibility cycling.
  1132. Levels higher than this will, for cycling, be treated as text, not a headline.
  1133. When `org-odd-levels-only' is set, a value of N in this variable actually
  1134. means 2N-1 stars as the limiting headline.
  1135. When nil, cycle all levels.
  1136. Note that the limiting level of cycling is also influenced by
  1137. `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
  1138. `org-inlinetask-min-level' is, cycling will be limited to levels one less
  1139. than its value."
  1140. :group 'org-cycle
  1141. :type '(choice
  1142. (const :tag "No limit" nil)
  1143. (integer :tag "Maximum level")))
  1144. (defcustom org-hide-block-startup nil
  1145. "Non-nil means entering Org-mode will fold all blocks.
  1146. This can also be set in on a per-file basis with
  1147. #+STARTUP: hideblocks
  1148. #+STARTUP: showblocks"
  1149. :group 'org-startup
  1150. :group 'org-cycle
  1151. :type 'boolean)
  1152. (defcustom org-cycle-global-at-bob nil
  1153. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  1154. This makes it possible to do global cycling without having to use S-TAB or
  1155. \\[universal-argument] TAB. For this special case to work, the first line
  1156. of the buffer must not be a headline -- it may be empty or some other text.
  1157. When used in this way, `org-cycle-hook' is disabled temporarily to make
  1158. sure the cursor stays at the beginning of the buffer. When this option is
  1159. nil, don't do anything special at the beginning of the buffer."
  1160. :group 'org-cycle
  1161. :type 'boolean)
  1162. (defcustom org-cycle-level-after-item/entry-creation t
  1163. "Non-nil means cycle entry level or item indentation in new empty entries.
  1164. When the cursor is at the end of an empty headline, i.e., with only stars
  1165. and maybe a TODO keyword, TAB will then switch the entry to become a child,
  1166. and then all possible ancestor states, before returning to the original state.
  1167. This makes data entry extremely fast: M-RET to create a new headline,
  1168. on TAB to make it a child, two or more tabs to make it a (grand-)uncle.
  1169. When the cursor is at the end of an empty plain list item, one TAB will
  1170. make it a subitem, two or more tabs will back up to make this an item
  1171. higher up in the item hierarchy."
  1172. :group 'org-cycle
  1173. :type 'boolean)
  1174. (defcustom org-cycle-emulate-tab t
  1175. "Where should `org-cycle' emulate TAB.
  1176. nil Never
  1177. white Only in completely white lines
  1178. whitestart Only at the beginning of lines, before the first non-white char
  1179. t Everywhere except in headlines
  1180. exc-hl-bol Everywhere except at the start of a headline
  1181. If TAB is used in a place where it does not emulate TAB, the current subtree
  1182. visibility is cycled."
  1183. :group 'org-cycle
  1184. :type '(choice (const :tag "Never" nil)
  1185. (const :tag "Only in completely white lines" white)
  1186. (const :tag "Before first char in a line" whitestart)
  1187. (const :tag "Everywhere except in headlines" t)
  1188. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)))
  1189. (defcustom org-cycle-separator-lines 2
  1190. "Number of empty lines needed to keep an empty line between collapsed trees.
  1191. If you leave an empty line between the end of a subtree and the following
  1192. headline, this empty line is hidden when the subtree is folded.
  1193. Org-mode will leave (exactly) one empty line visible if the number of
  1194. empty lines is equal or larger to the number given in this variable.
  1195. So the default 2 means at least 2 empty lines after the end of a subtree
  1196. are needed to produce free space between a collapsed subtree and the
  1197. following headline.
  1198. If the number is negative, and the number of empty lines is at least -N,
  1199. all empty lines are shown.
  1200. Special case: when 0, never leave empty lines in collapsed view."
  1201. :group 'org-cycle
  1202. :type 'integer)
  1203. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  1204. (defcustom org-pre-cycle-hook nil
  1205. "Hook that is run before visibility cycling is happening.
  1206. The function(s) in this hook must accept a single argument which indicates
  1207. the new state that will be set right after running this hook. The
  1208. argument is a symbol. Before a global state change, it can have the values
  1209. `overview', `content', or `all'. Before a local state change, it can have
  1210. the values `folded', `children', or `subtree'."
  1211. :group 'org-cycle
  1212. :type 'hook)
  1213. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  1214. org-cycle-hide-drawers
  1215. org-cycle-show-empty-lines
  1216. org-optimize-window-after-visibility-change)
  1217. "Hook that is run after `org-cycle' has changed the buffer visibility.
  1218. The function(s) in this hook must accept a single argument which indicates
  1219. the new state that was set by the most recent `org-cycle' command. The
  1220. argument is a symbol. After a global state change, it can have the values
  1221. `overview', `contents', or `all'. After a local state change, it can have
  1222. the values `folded', `children', or `subtree'."
  1223. :group 'org-cycle
  1224. :type 'hook
  1225. :version "25.1"
  1226. :package-version '(Org . "8.3"))
  1227. (defgroup org-edit-structure nil
  1228. "Options concerning structure editing in Org-mode."
  1229. :tag "Org Edit Structure"
  1230. :group 'org-structure)
  1231. (defcustom org-odd-levels-only nil
  1232. "Non-nil means skip even levels and only use odd levels for the outline.
  1233. This has the effect that two stars are being added/taken away in
  1234. promotion/demotion commands. It also influences how levels are
  1235. handled by the exporters.
  1236. Changing it requires restart of `font-lock-mode' to become effective
  1237. for fontification also in regions already fontified.
  1238. You may also set this on a per-file basis by adding one of the following
  1239. lines to the buffer:
  1240. #+STARTUP: odd
  1241. #+STARTUP: oddeven"
  1242. :group 'org-edit-structure
  1243. :group 'org-appearance
  1244. :type 'boolean)
  1245. (defcustom org-adapt-indentation t
  1246. "Non-nil means adapt indentation to outline node level.
  1247. When this variable is set, Org assumes that you write outlines by
  1248. indenting text in each node to align with the headline (after the
  1249. stars). The following issues are influenced by this variable:
  1250. - The indentation is increased by one space in a demotion
  1251. command, and decreased by one in a promotion command. However,
  1252. in the latter case, if shifting some line in the entry body
  1253. would alter document structure (e.g., insert a new headline),
  1254. indentation is not changed at all.
  1255. - Property drawers and planning information is inserted indented
  1256. when this variable is set. When nil, they will not be indented.
  1257. - TAB indents a line relative to current level. The lines below
  1258. a headline will be indented when this variable is set.
  1259. Note that this is all about true indentation, by adding and
  1260. removing space characters. See also `org-indent.el' which does
  1261. level-dependent indentation in a virtual way, i.e. at display
  1262. time in Emacs."
  1263. :group 'org-edit-structure
  1264. :type 'boolean)
  1265. (defcustom org-special-ctrl-a/e nil
  1266. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  1267. When t, `C-a' will bring back the cursor to the beginning of the
  1268. headline text, i.e. after the stars and after a possible TODO
  1269. keyword. In an item, this will be the position after bullet and
  1270. check-box, if any. When the cursor is already at that position,
  1271. another `C-a' will bring it to the beginning of the line.
  1272. `C-e' will jump to the end of the headline, ignoring the presence
  1273. of tags in the headline. A second `C-e' will then jump to the
  1274. true end of the line, after any tags. This also means that, when
  1275. this variable is non-nil, `C-e' also will never jump beyond the
  1276. end of the heading of a folded section, i.e. not after the
  1277. ellipses.
  1278. When set to the symbol `reversed', the first `C-a' or `C-e' works
  1279. normally, going to the true line boundary first. Only a directly
  1280. following, identical keypress will bring the cursor to the
  1281. special positions.
  1282. This may also be a cons cell where the behavior for `C-a' and
  1283. `C-e' is set separately."
  1284. :group 'org-edit-structure
  1285. :type '(choice
  1286. (const :tag "off" nil)
  1287. (const :tag "on: after stars/bullet and before tags first" t)
  1288. (const :tag "reversed: true line boundary first" reversed)
  1289. (cons :tag "Set C-a and C-e separately"
  1290. (choice :tag "Special C-a"
  1291. (const :tag "off" nil)
  1292. (const :tag "on: after stars/bullet first" t)
  1293. (const :tag "reversed: before stars/bullet first" reversed))
  1294. (choice :tag "Special C-e"
  1295. (const :tag "off" nil)
  1296. (const :tag "on: before tags first" t)
  1297. (const :tag "reversed: after tags first" reversed)))))
  1298. (org-defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e)
  1299. (defcustom org-special-ctrl-k nil
  1300. "Non-nil means `C-k' will behave specially in headlines.
  1301. When nil, `C-k' will call the default `kill-line' command.
  1302. When t, the following will happen while the cursor is in the headline:
  1303. - When the cursor is at the beginning of a headline, kill the entire
  1304. line and possible the folded subtree below the line.
  1305. - When in the middle of the headline text, kill the headline up to the tags.
  1306. - When after the headline text, kill the tags."
  1307. :group 'org-edit-structure
  1308. :type 'boolean)
  1309. (defcustom org-ctrl-k-protect-subtree nil
  1310. "Non-nil means, do not delete a hidden subtree with C-k.
  1311. When set to the symbol `error', simply throw an error when C-k is
  1312. used to kill (part-of) a headline that has hidden text behind it.
  1313. Any other non-nil value will result in a query to the user, if it is
  1314. OK to kill that hidden subtree. When nil, kill without remorse."
  1315. :group 'org-edit-structure
  1316. :version "24.1"
  1317. :type '(choice
  1318. (const :tag "Do not protect hidden subtrees" nil)
  1319. (const :tag "Protect hidden subtrees with a security query" t)
  1320. (const :tag "Never kill a hidden subtree with C-k" error)))
  1321. (defcustom org-special-ctrl-o t
  1322. "Non-nil means, make `C-o' insert a row in tables."
  1323. :group 'org-edit-structure
  1324. :type 'boolean)
  1325. (defcustom org-catch-invisible-edits nil
  1326. "Check if in invisible region before inserting or deleting a character.
  1327. Valid values are:
  1328. nil Do not check, so just do invisible edits.
  1329. error Throw an error and do nothing.
  1330. show Make point visible, and do the requested edit.
  1331. show-and-error Make point visible, then throw an error and abort the edit.
  1332. smart Make point visible, and do insertion/deletion if it is
  1333. adjacent to visible text and the change feels predictable.
  1334. Never delete a previously invisible character or add in the
  1335. middle or right after an invisible region. Basically, this
  1336. allows insertion and backward-delete right before ellipses.
  1337. FIXME: maybe in this case we should not even show?"
  1338. :group 'org-edit-structure
  1339. :version "24.1"
  1340. :type '(choice
  1341. (const :tag "Do not check" nil)
  1342. (const :tag "Throw error when trying to edit" error)
  1343. (const :tag "Unhide, but do not do the edit" show-and-error)
  1344. (const :tag "Show invisible part and do the edit" show)
  1345. (const :tag "Be smart and do the right thing" smart)))
  1346. (defcustom org-yank-folded-subtrees t
  1347. "Non-nil means when yanking subtrees, fold them.
  1348. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  1349. it starts with a heading and all other headings in it are either children
  1350. or siblings, then fold all the subtrees. However, do this only if no
  1351. text after the yank would be swallowed into a folded tree by this action."
  1352. :group 'org-edit-structure
  1353. :type 'boolean)
  1354. (defcustom org-yank-adjusted-subtrees nil
  1355. "Non-nil means when yanking subtrees, adjust the level.
  1356. With this setting, `org-paste-subtree' is used to insert the subtree, see
  1357. this function for details."
  1358. :group 'org-edit-structure
  1359. :type 'boolean)
  1360. (defcustom org-M-RET-may-split-line '((default . t))
  1361. "Non-nil means M-RET will split the line at the cursor position.
  1362. When nil, it will go to the end of the line before making a
  1363. new line.
  1364. You may also set this option in a different way for different
  1365. contexts. Valid contexts are:
  1366. headline when creating a new headline
  1367. item when creating a new item
  1368. table in a table field
  1369. default the value to be used for all contexts not explicitly
  1370. customized"
  1371. :group 'org-structure
  1372. :group 'org-table
  1373. :type '(choice
  1374. (const :tag "Always" t)
  1375. (const :tag "Never" nil)
  1376. (repeat :greedy t :tag "Individual contexts"
  1377. (cons
  1378. (choice :tag "Context"
  1379. (const headline)
  1380. (const item)
  1381. (const table)
  1382. (const default))
  1383. (boolean)))))
  1384. (defcustom org-insert-heading-respect-content nil
  1385. "Non-nil means insert new headings after the current subtree.
  1386. When nil, the new heading is created directly after the current line.
  1387. The commands \\[org-insert-heading-respect-content] and \\[org-insert-todo-heading-respect-content] turn
  1388. this variable on for the duration of the command."
  1389. :group 'org-structure
  1390. :type 'boolean)
  1391. (defcustom org-blank-before-new-entry '((heading . auto)
  1392. (plain-list-item . auto))
  1393. "Should `org-insert-heading' leave a blank line before new heading/item?
  1394. The value is an alist, with `heading' and `plain-list-item' as CAR,
  1395. and a boolean flag as CDR. The cdr may also be the symbol `auto', in
  1396. which case Org will look at the surrounding headings/items and try to
  1397. make an intelligent decision whether to insert a blank line or not."
  1398. :group 'org-edit-structure
  1399. :type '(list
  1400. (cons (const heading)
  1401. (choice (const :tag "Never" nil)
  1402. (const :tag "Always" t)
  1403. (const :tag "Auto" auto)))
  1404. (cons (const plain-list-item)
  1405. (choice (const :tag "Never" nil)
  1406. (const :tag "Always" t)
  1407. (const :tag "Auto" auto)))))
  1408. (defcustom org-insert-heading-hook nil
  1409. "Hook being run after inserting a new heading."
  1410. :group 'org-edit-structure
  1411. :type 'hook)
  1412. (defcustom org-enable-fixed-width-editor t
  1413. "Non-nil means lines starting with \":\" are treated as fixed-width.
  1414. This currently only means they are never auto-wrapped.
  1415. When nil, such lines will be treated like ordinary lines."
  1416. :group 'org-edit-structure
  1417. :type 'boolean)
  1418. (defcustom org-goto-auto-isearch t
  1419. "Non-nil means typing characters in `org-goto' starts incremental search.
  1420. When nil, you can use these keybindings to navigate the buffer:
  1421. q Quit the org-goto interface
  1422. n Go to the next visible heading
  1423. p Go to the previous visible heading
  1424. f Go one heading forward on same level
  1425. b Go one heading backward on same level
  1426. u Go one heading up"
  1427. :group 'org-edit-structure
  1428. :type 'boolean)
  1429. (defgroup org-sparse-trees nil
  1430. "Options concerning sparse trees in Org-mode."
  1431. :tag "Org Sparse Trees"
  1432. :group 'org-structure)
  1433. (defcustom org-highlight-sparse-tree-matches t
  1434. "Non-nil means highlight all matches that define a sparse tree.
  1435. The highlights will automatically disappear the next time the buffer is
  1436. changed by an edit command."
  1437. :group 'org-sparse-trees
  1438. :type 'boolean)
  1439. (defcustom org-remove-highlights-with-change t
  1440. "Non-nil means any change to the buffer will remove temporary highlights.
  1441. \\<org-mode-map>\
  1442. Such highlights are created by `org-occur' and `org-clock-display'.
  1443. When nil, `\\[org-ctrl-c-ctrl-c]' needs to be used \
  1444. to get rid of the highlights.
  1445. The highlights created by `org-toggle-latex-fragment' always need
  1446. `\\[org-toggle-latex-fragment]' to be removed."
  1447. :group 'org-sparse-trees
  1448. :group 'org-time
  1449. :type 'boolean)
  1450. (defcustom org-occur-hook '(org-first-headline-recenter)
  1451. "Hook that is run after `org-occur' has constructed a sparse tree.
  1452. This can be used to recenter the window to show as much of the structure
  1453. as possible."
  1454. :group 'org-sparse-trees
  1455. :type 'hook)
  1456. (defgroup org-imenu-and-speedbar nil
  1457. "Options concerning imenu and speedbar in Org-mode."
  1458. :tag "Org Imenu and Speedbar"
  1459. :group 'org-structure)
  1460. (defcustom org-imenu-depth 2
  1461. "The maximum level for Imenu access to Org-mode headlines.
  1462. This also applied for speedbar access."
  1463. :group 'org-imenu-and-speedbar
  1464. :type 'integer)
  1465. (defgroup org-table nil
  1466. "Options concerning tables in Org-mode."
  1467. :tag "Org Table"
  1468. :group 'org)
  1469. (defcustom org-enable-table-editor 'optimized
  1470. "Non-nil means lines starting with \"|\" are handled by the table editor.
  1471. When nil, such lines will be treated like ordinary lines.
  1472. When equal to the symbol `optimized', the table editor will be optimized to
  1473. do the following:
  1474. - Automatic overwrite mode in front of whitespace in table fields.
  1475. This makes the structure of the table stay in tact as long as the edited
  1476. field does not exceed the column width.
  1477. - Minimize the number of realigns. Normally, the table is aligned each time
  1478. TAB or RET are pressed to move to another field. With optimization this
  1479. happens only if changes to a field might have changed the column width.
  1480. Optimization requires replacing the functions `self-insert-command',
  1481. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  1482. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  1483. very good at guessing when a re-align will be necessary, but you can always
  1484. force one with \\[org-ctrl-c-ctrl-c].
  1485. If you would like to use the optimized version in Org-mode, but the
  1486. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  1487. This variable can be used to turn on and off the table editor during a session,
  1488. but in order to toggle optimization, a restart is required.
  1489. See also the variable `org-table-auto-blank-field'."
  1490. :group 'org-table
  1491. :type '(choice
  1492. (const :tag "off" nil)
  1493. (const :tag "on" t)
  1494. (const :tag "on, optimized" optimized)))
  1495. (defcustom org-self-insert-cluster-for-undo (or (featurep 'xemacs)
  1496. (version<= emacs-version "24.1"))
  1497. "Non-nil means cluster self-insert commands for undo when possible.
  1498. If this is set, then, like in the Emacs command loop, 20 consecutive
  1499. characters will be undone together.
  1500. This is configurable, because there is some impact on typing performance."
  1501. :group 'org-table
  1502. :type 'boolean)
  1503. (defcustom org-table-tab-recognizes-table.el t
  1504. "Non-nil means TAB will automatically notice a table.el table.
  1505. When it sees such a table, it moves point into it and - if necessary -
  1506. calls `table-recognize-table'."
  1507. :group 'org-table-editing
  1508. :type 'boolean)
  1509. (defgroup org-link nil
  1510. "Options concerning links in Org-mode."
  1511. :tag "Org Link"
  1512. :group 'org)
  1513. (defvar-local org-link-abbrev-alist-local nil
  1514. "Buffer-local version of `org-link-abbrev-alist', which see.
  1515. The value of this is taken from the #+LINK lines.")
  1516. (defcustom org-link-abbrev-alist nil
  1517. "Alist of link abbreviations.
  1518. The car of each element is a string, to be replaced at the start of a link.
  1519. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  1520. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  1521. [[linkkey:tag][description]]
  1522. The `linkkey' must be a word word, starting with a letter, followed
  1523. by letters, numbers, `-' or `_'.
  1524. If REPLACE is a string, the tag will simply be appended to create the link.
  1525. If the string contains \"%s\", the tag will be inserted there. If the string
  1526. contains \"%h\", it will cause a url-encoded version of the tag to be inserted
  1527. at that point (see the function `url-hexify-string'). If the string contains
  1528. the specifier \"%(my-function)\", then the custom function `my-function' will
  1529. be invoked: this function takes the tag as its only argument and must return
  1530. a string.
  1531. REPLACE may also be a function that will be called with the tag as the
  1532. only argument to create the link, which should be returned as a string.
  1533. See the manual for examples."
  1534. :group 'org-link
  1535. :type '(repeat
  1536. (cons
  1537. (string :tag "Protocol")
  1538. (choice
  1539. (string :tag "Format")
  1540. (function)))))
  1541. (defcustom org-descriptive-links t
  1542. "Non-nil means Org will display descriptive links.
  1543. E.g. [[http://orgmode.org][Org website]] will be displayed as
  1544. \"Org Website\", hiding the link itself and just displaying its
  1545. description. When set to nil, Org will display the full links
  1546. literally.
  1547. You can interactively set the value of this variable by calling
  1548. `org-toggle-link-display' or from the menu Org>Hyperlinks menu."
  1549. :group 'org-link
  1550. :type 'boolean)
  1551. (defcustom org-link-file-path-type 'adaptive
  1552. "How the path name in file links should be stored.
  1553. Valid values are:
  1554. relative Relative to the current directory, i.e. the directory of the file
  1555. into which the link is being inserted.
  1556. absolute Absolute path, if possible with ~ for home directory.
  1557. noabbrev Absolute path, no abbreviation of home directory.
  1558. adaptive Use relative path for files in the current directory and sub-
  1559. directories of it. For other files, use an absolute path."
  1560. :group 'org-link
  1561. :type '(choice
  1562. (const relative)
  1563. (const absolute)
  1564. (const noabbrev)
  1565. (const adaptive)))
  1566. (defvaralias 'org-activate-links 'org-highlight-links)
  1567. (defcustom org-highlight-links '(bracket angle plain radio tag date footnote)
  1568. "Types of links that should be highlighted in Org-mode files.
  1569. This is a list of symbols, each one of them leading to the
  1570. highlighting of a certain link type.
  1571. You can still open links that are not highlighted.
  1572. In principle, it does not hurt to turn on highlighting for all
  1573. link types. There may be a small gain when turning off unused
  1574. link types. The types are:
  1575. bracket The recommended [[link][description]] or [[link]] links with hiding.
  1576. angle Links in angular brackets that may contain whitespace like
  1577. <bbdb:Carsten Dominik>.
  1578. plain Plain links in normal text, no whitespace, like http://google.com.
  1579. radio Text that is matched by a radio target, see manual for details.
  1580. tag Tag settings in a headline (link to tag search).
  1581. date Time stamps (link to calendar).
  1582. footnote Footnote labels.
  1583. If you set this variable during an Emacs session, use `org-mode-restart'
  1584. in the Org buffer so that the change takes effect."
  1585. :group 'org-link
  1586. :group 'org-appearance
  1587. :type '(set :greedy t
  1588. (const :tag "Double bracket links" bracket)
  1589. (const :tag "Angular bracket links" angle)
  1590. (const :tag "Plain text links" plain)
  1591. (const :tag "Radio target matches" radio)
  1592. (const :tag "Tags" tag)
  1593. (const :tag "Timestamps" date)
  1594. (const :tag "Footnotes" footnote)))
  1595. (defcustom org-make-link-description-function nil
  1596. "Function to use for generating link descriptions from links.
  1597. When nil, the link location will be used. This function must take
  1598. two parameters: the first one is the link, the second one is the
  1599. description generated by `org-insert-link'. The function should
  1600. return the description to use."
  1601. :group 'org-link
  1602. :type '(choice (const nil) (function)))
  1603. (defgroup org-link-store nil
  1604. "Options concerning storing links in Org-mode."
  1605. :tag "Org Store Link"
  1606. :group 'org-link)
  1607. (defcustom org-url-hexify-p t
  1608. "When non-nil, hexify URL when creating a link."
  1609. :type 'boolean
  1610. :version "24.3"
  1611. :group 'org-link-store)
  1612. (defcustom org-email-link-description-format "Email %c: %.30s"
  1613. "Format of the description part of a link to an email or usenet message.
  1614. The following %-escapes will be replaced by corresponding information:
  1615. %F full \"From\" field
  1616. %f name, taken from \"From\" field, address if no name
  1617. %T full \"To\" field
  1618. %t first name in \"To\" field, address if no name
  1619. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  1620. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  1621. %s subject
  1622. %d date
  1623. %m message-id.
  1624. You may use normal field width specification between the % and the letter.
  1625. This is for example useful to limit the length of the subject.
  1626. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  1627. :group 'org-link-store
  1628. :type 'string)
  1629. (defcustom org-from-is-user-regexp
  1630. (let (r1 r2)
  1631. (when (and user-mail-address (not (string= user-mail-address "")))
  1632. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  1633. (when (and user-full-name (not (string= user-full-name "")))
  1634. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  1635. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  1636. "Regexp matched against the \"From:\" header of an email or usenet message.
  1637. It should match if the message is from the user him/herself."
  1638. :group 'org-link-store
  1639. :type 'regexp)
  1640. (defcustom org-context-in-file-links t
  1641. "Non-nil means file links from `org-store-link' contain context.
  1642. A search string will be added to the file name with :: as separator and
  1643. used to find the context when the link is activated by the command
  1644. `org-open-at-point'. When this option is t, the entire active region
  1645. will be placed in the search string of the file link. If set to a
  1646. positive integer, only the first n lines of context will be stored.
  1647. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1648. negates this setting for the duration of the command."
  1649. :group 'org-link-store
  1650. :type '(choice boolean integer))
  1651. (defcustom org-keep-stored-link-after-insertion nil
  1652. "Non-nil means keep link in list for entire session.
  1653. The command `org-store-link' adds a link pointing to the current
  1654. location to an internal list. These links accumulate during a session.
  1655. The command `org-insert-link' can be used to insert links into any
  1656. Org-mode file (offering completion for all stored links). When this
  1657. option is nil, every link which has been inserted once using \\[org-insert-link]
  1658. will be removed from the list, to make completing the unused links
  1659. more efficient."
  1660. :group 'org-link-store
  1661. :type 'boolean)
  1662. (defgroup org-link-follow nil
  1663. "Options concerning following links in Org-mode."
  1664. :tag "Org Follow Link"
  1665. :group 'org-link)
  1666. (defcustom org-link-translation-function nil
  1667. "Function to translate links with different syntax to Org syntax.
  1668. This can be used to translate links created for example by the Planner
  1669. or emacs-wiki packages to Org syntax.
  1670. The function must accept two parameters, a TYPE containing the link
  1671. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  1672. which is everything after the link protocol. It should return a cons
  1673. with possibly modified values of type and path.
  1674. Org contains a function for this, so if you set this variable to
  1675. `org-translate-link-from-planner', you should be able follow many
  1676. links created by planner."
  1677. :group 'org-link-follow
  1678. :type '(choice (const nil) (function)))
  1679. (defcustom org-follow-link-hook nil
  1680. "Hook that is run after a link has been followed."
  1681. :group 'org-link-follow
  1682. :type 'hook)
  1683. (defcustom org-tab-follows-link nil
  1684. "Non-nil means on links TAB will follow the link.
  1685. Needs to be set before org.el is loaded.
  1686. This really should not be used, it does not make sense, and the
  1687. implementation is bad."
  1688. :group 'org-link-follow
  1689. :type 'boolean)
  1690. (defcustom org-return-follows-link nil
  1691. "Non-nil means on links RET will follow the link.
  1692. In tables, the special behavior of RET has precedence."
  1693. :group 'org-link-follow
  1694. :type 'boolean)
  1695. (defcustom org-mouse-1-follows-link
  1696. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1697. "Non-nil means mouse-1 on a link will follow the link.
  1698. A longer mouse click will still set point. Does not work on XEmacs.
  1699. Needs to be set before org.el is loaded."
  1700. :group 'org-link-follow
  1701. :version "24.4"
  1702. :package-version '(Org . "8.3")
  1703. :type '(choice
  1704. (const :tag "A double click follows the link" double)
  1705. (const :tag "Unconditionally follow the link with mouse-1" t)
  1706. (integer :tag "mouse-1 click does not follow the link if longer than N ms" 450)))
  1707. (defcustom org-mark-ring-length 4
  1708. "Number of different positions to be recorded in the ring.
  1709. Changing this requires a restart of Emacs to work correctly."
  1710. :group 'org-link-follow
  1711. :type 'integer)
  1712. (defcustom org-link-search-must-match-exact-headline 'query-to-create
  1713. "Non-nil means internal links in Org files must exactly match a headline.
  1714. When nil, the link search tries to match a phrase with all words
  1715. in the search text."
  1716. :group 'org-link-follow
  1717. :version "24.1"
  1718. :type '(choice
  1719. (const :tag "Use fuzzy text search" nil)
  1720. (const :tag "Match only exact headline" t)
  1721. (const :tag "Match exact headline or query to create it"
  1722. query-to-create)))
  1723. (defcustom org-link-frame-setup
  1724. '((vm . vm-visit-folder-other-frame)
  1725. (vm-imap . vm-visit-imap-folder-other-frame)
  1726. (gnus . org-gnus-no-new-news)
  1727. (file . find-file-other-window)
  1728. (wl . wl-other-frame))
  1729. "Setup the frame configuration for following links.
  1730. When following a link with Emacs, it may often be useful to display
  1731. this link in another window or frame. This variable can be used to
  1732. set this up for the different types of links.
  1733. For VM, use any of
  1734. `vm-visit-folder'
  1735. `vm-visit-folder-other-window'
  1736. `vm-visit-folder-other-frame'
  1737. For Gnus, use any of
  1738. `gnus'
  1739. `gnus-other-frame'
  1740. `org-gnus-no-new-news'
  1741. For FILE, use any of
  1742. `find-file'
  1743. `find-file-other-window'
  1744. `find-file-other-frame'
  1745. For Wanderlust use any of
  1746. `wl'
  1747. `wl-other-frame'
  1748. For the calendar, use the variable `calendar-setup'.
  1749. For BBDB, it is currently only possible to display the matches in
  1750. another window."
  1751. :group 'org-link-follow
  1752. :type '(list
  1753. (cons (const vm)
  1754. (choice
  1755. (const vm-visit-folder)
  1756. (const vm-visit-folder-other-window)
  1757. (const vm-visit-folder-other-frame)))
  1758. (cons (const vm-imap)
  1759. (choice
  1760. (const vm-visit-imap-folder)
  1761. (const vm-visit-imap-folder-other-window)
  1762. (const vm-visit-imap-folder-other-frame)))
  1763. (cons (const gnus)
  1764. (choice
  1765. (const gnus)
  1766. (const gnus-other-frame)
  1767. (const org-gnus-no-new-news)))
  1768. (cons (const file)
  1769. (choice
  1770. (const find-file)
  1771. (const find-file-other-window)
  1772. (const find-file-other-frame)))
  1773. (cons (const wl)
  1774. (choice
  1775. (const wl)
  1776. (const wl-other-frame)))))
  1777. (defcustom org-display-internal-link-with-indirect-buffer nil
  1778. "Non-nil means use indirect buffer to display infile links.
  1779. Activating internal links (from one location in a file to another location
  1780. in the same file) normally just jumps to the location. When the link is
  1781. activated with a \\[universal-argument] prefix (or with mouse-3), the link \
  1782. is displayed in
  1783. another window. When this option is set, the other window actually displays
  1784. an indirect buffer clone of the current buffer, to avoid any visibility
  1785. changes to the current buffer."
  1786. :group 'org-link-follow
  1787. :type 'boolean)
  1788. (defcustom org-open-non-existing-files nil
  1789. "Non-nil means `org-open-file' will open non-existing files.
  1790. When nil, an error will be generated.
  1791. This variable applies only to external applications because they
  1792. might choke on non-existing files. If the link is to a file that
  1793. will be opened in Emacs, the variable is ignored."
  1794. :group 'org-link-follow
  1795. :type 'boolean)
  1796. (defcustom org-open-directory-means-index-dot-org nil
  1797. "Non-nil means a link to a directory really means to index.org.
  1798. When nil, following a directory link will run dired or open a finder/explorer
  1799. window on that directory."
  1800. :group 'org-link-follow
  1801. :type 'boolean)
  1802. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1803. "Non-nil means ask for confirmation before executing shell links.
  1804. Shell links can be dangerous: just think about a link
  1805. [[shell:rm -rf ~/*][Google Search]]
  1806. This link would show up in your Org-mode document as \"Google Search\",
  1807. but really it would remove your entire home directory.
  1808. Therefore we advise against setting this variable to nil.
  1809. Just change it to `y-or-n-p' if you want to confirm with a
  1810. single keystroke rather than having to type \"yes\"."
  1811. :group 'org-link-follow
  1812. :type '(choice
  1813. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1814. (const :tag "with y-or-n (faster)" y-or-n-p)
  1815. (const :tag "no confirmation (dangerous)" nil)))
  1816. (put 'org-confirm-shell-link-function
  1817. 'safe-local-variable
  1818. (lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1819. (defcustom org-confirm-shell-link-not-regexp ""
  1820. "A regexp to skip confirmation for shell links."
  1821. :group 'org-link-follow
  1822. :version "24.1"
  1823. :type 'regexp)
  1824. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1825. "Non-nil means ask for confirmation before executing Emacs Lisp links.
  1826. Elisp links can be dangerous: just think about a link
  1827. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1828. This link would show up in your Org-mode document as \"Google Search\",
  1829. but really it would remove your entire home directory.
  1830. Therefore we advise against setting this variable to nil.
  1831. Just change it to `y-or-n-p' if you want to confirm with a
  1832. single keystroke rather than having to type \"yes\"."
  1833. :group 'org-link-follow
  1834. :type '(choice
  1835. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1836. (const :tag "with y-or-n (faster)" y-or-n-p)
  1837. (const :tag "no confirmation (dangerous)" nil)))
  1838. (put 'org-confirm-shell-link-function
  1839. 'safe-local-variable
  1840. (lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1841. (defcustom org-confirm-elisp-link-not-regexp ""
  1842. "A regexp to skip confirmation for Elisp links."
  1843. :group 'org-link-follow
  1844. :version "24.1"
  1845. :type 'regexp)
  1846. (defconst org-file-apps-defaults-gnu
  1847. '((remote . emacs)
  1848. (system . mailcap)
  1849. (t . mailcap))
  1850. "Default file applications on a UNIX or GNU/Linux system.
  1851. See `org-file-apps'.")
  1852. (defconst org-file-apps-defaults-macosx
  1853. '((remote . emacs)
  1854. (t . "open %s")
  1855. (system . "open %s")
  1856. ("ps.gz" . "gv %s")
  1857. ("eps.gz" . "gv %s")
  1858. ("dvi" . "xdvi %s")
  1859. ("fig" . "xfig %s"))
  1860. "Default file applications on a MacOS X system.
  1861. The system \"open\" is known as a default, but we use X11 applications
  1862. for some files for which the OS does not have a good default.
  1863. See `org-file-apps'.")
  1864. (defconst org-file-apps-defaults-windowsnt
  1865. (list
  1866. '(remote . emacs)
  1867. (cons t
  1868. (list (if (featurep 'xemacs)
  1869. 'mswindows-shell-execute
  1870. 'w32-shell-execute)
  1871. "open" 'file))
  1872. (cons 'system
  1873. (list (if (featurep 'xemacs)
  1874. 'mswindows-shell-execute
  1875. 'w32-shell-execute)
  1876. "open" 'file)))
  1877. "Default file applications on a Windows NT system.
  1878. The system \"open\" is used for most files.
  1879. See `org-file-apps'.")
  1880. (defcustom org-file-apps
  1881. '((auto-mode . emacs)
  1882. ("\\.mm\\'" . default)
  1883. ("\\.x?html?\\'" . default)
  1884. ("\\.pdf\\'" . default))
  1885. "External applications for opening `file:path' items in a document.
  1886. Org-mode uses system defaults for different file types, but
  1887. you can use this variable to set the application for a given file
  1888. extension. The entries in this list are cons cells where the car identifies
  1889. files and the cdr the corresponding command. Possible values for the
  1890. file identifier are
  1891. \"string\" A string as a file identifier can be interpreted in different
  1892. ways, depending on its contents:
  1893. - Alphanumeric characters only:
  1894. Match links with this file extension.
  1895. Example: (\"pdf\" . \"evince %s\")
  1896. to open PDFs with evince.
  1897. - Regular expression: Match links where the
  1898. filename matches the regexp. If you want to
  1899. use groups here, use shy groups.
  1900. Example: (\"\\.x?html\\\\='\" . \"firefox %s\")
  1901. (\"\\(?:xhtml\\|html\\)\" . \"firefox %s\")
  1902. to open *.html and *.xhtml with firefox.
  1903. - Regular expression which contains (non-shy) groups:
  1904. Match links where the whole link, including \"::\", and
  1905. anything after that, matches the regexp.
  1906. In a custom command string, %1, %2, etc. are replaced with
  1907. the parts of the link that were matched by the groups.
  1908. For backwards compatibility, if a command string is given
  1909. that does not use any of the group matches, this case is
  1910. handled identically to the second one (i.e. match against
  1911. file name only).
  1912. In a custom lisp form, you can access the group matches with
  1913. (match-string n link).
  1914. Example: (\"\\.pdf::\\(\\d+\\)\\\\='\" . \"evince -p %1 %s\")
  1915. to open [[file:document.pdf::5]] with evince at page 5.
  1916. `directory' Matches a directory
  1917. `remote' Matches a remote file, accessible through tramp or efs.
  1918. Remote files most likely should be visited through Emacs
  1919. because external applications cannot handle such paths.
  1920. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1921. so all files Emacs knows how to handle. Using this with
  1922. command `emacs' will open most files in Emacs. Beware that this
  1923. will also open html files inside Emacs, unless you add
  1924. (\"html\" . default) to the list as well.
  1925. t Default for files not matched by any of the other options.
  1926. `system' The system command to open files, like `open' on Windows
  1927. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1928. that will be selected if you call `C-c C-o' with a double
  1929. \\[universal-argument] \\[universal-argument] prefix.
  1930. Possible values for the command are:
  1931. `emacs' The file will be visited by the current Emacs process.
  1932. `default' Use the default application for this file type, which is the
  1933. association for t in the list, most likely in the system-specific
  1934. part.
  1935. This can be used to overrule an unwanted setting in the
  1936. system-specific variable.
  1937. `system' Use the system command for opening files, like \"open\".
  1938. This command is specified by the entry whose car is `system'.
  1939. Most likely, the system-specific version of this variable
  1940. does define this command, but you can overrule/replace it
  1941. here.
  1942. string A command to be executed by a shell; %s will be replaced
  1943. by the path to the file.
  1944. sexp A Lisp form which will be evaluated. The file path will
  1945. be available in the Lisp variable `file'.
  1946. For more examples, see the system specific constants
  1947. `org-file-apps-defaults-macosx'
  1948. `org-file-apps-defaults-windowsnt'
  1949. `org-file-apps-defaults-gnu'."
  1950. :group 'org-link-follow
  1951. :type '(repeat
  1952. (cons (choice :value ""
  1953. (string :tag "Extension")
  1954. (const :tag "System command to open files" system)
  1955. (const :tag "Default for unrecognized files" t)
  1956. (const :tag "Remote file" remote)
  1957. (const :tag "Links to a directory" directory)
  1958. (const :tag "Any files that have Emacs modes"
  1959. auto-mode))
  1960. (choice :value ""
  1961. (const :tag "Visit with Emacs" emacs)
  1962. (const :tag "Use default" default)
  1963. (const :tag "Use the system command" system)
  1964. (string :tag "Command")
  1965. (sexp :tag "Lisp form")))))
  1966. (defcustom org-doi-server-url "http://dx.doi.org/"
  1967. "The URL of the DOI server."
  1968. :type 'string
  1969. :version "24.3"
  1970. :group 'org-link-follow)
  1971. (defgroup org-refile nil
  1972. "Options concerning refiling entries in Org-mode."
  1973. :tag "Org Refile"
  1974. :group 'org)
  1975. (defcustom org-directory "~/org"
  1976. "Directory with Org files.
  1977. This is just a default location to look for Org files. There is no need
  1978. at all to put your files into this directory. It is used in the
  1979. following situations:
  1980. 1. When a capture template specifies a target file that is not an
  1981. absolute path. The path will then be interpreted relative to
  1982. `org-directory'
  1983. 2. When the value of variable `org-agenda-files' is a single file, any
  1984. relative paths in this file will be taken as relative to
  1985. `org-directory'."
  1986. :group 'org-refile
  1987. :group 'org-capture
  1988. :type 'directory)
  1989. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1990. "Default target for storing notes.
  1991. Used as a fall back file for org-capture.el, for templates that
  1992. do not specify a target file."
  1993. :group 'org-refile
  1994. :group 'org-capture
  1995. :type 'file)
  1996. (defcustom org-goto-interface 'outline
  1997. "The default interface to be used for `org-goto'.
  1998. Allowed values are:
  1999. outline The interface shows an outline of the relevant file
  2000. and the correct heading is found by moving through
  2001. the outline or by searching with incremental search.
  2002. outline-path-completion Headlines in the current buffer are offered via
  2003. completion. This is the interface also used by
  2004. the refile command."
  2005. :group 'org-refile
  2006. :type '(choice
  2007. (const :tag "Outline" outline)
  2008. (const :tag "Outline-path-completion" outline-path-completion)))
  2009. (defcustom org-goto-max-level 5
  2010. "Maximum target level when running `org-goto' with refile interface."
  2011. :group 'org-refile
  2012. :type 'integer)
  2013. (defcustom org-reverse-note-order nil
  2014. "Non-nil means store new notes at the beginning of a file or entry.
  2015. When nil, new notes will be filed to the end of a file or entry.
  2016. This can also be a list with cons cells of regular expressions that
  2017. are matched against file names, and values."
  2018. :group 'org-capture
  2019. :group 'org-refile
  2020. :type '(choice
  2021. (const :tag "Reverse always" t)
  2022. (const :tag "Reverse never" nil)
  2023. (repeat :tag "By file name regexp"
  2024. (cons regexp boolean))))
  2025. (defcustom org-log-refile nil
  2026. "Information to record when a task is refiled.
  2027. Possible values are:
  2028. nil Don't add anything
  2029. time Add a time stamp to the task
  2030. note Prompt for a note and add it with template `org-log-note-headings'
  2031. This option can also be set with on a per-file-basis with
  2032. #+STARTUP: nologrefile
  2033. #+STARTUP: logrefile
  2034. #+STARTUP: lognoterefile
  2035. You can have local logging settings for a subtree by setting the LOGGING
  2036. property to one or more of these keywords.
  2037. When bulk-refiling from the agenda, the value `note' is forbidden and
  2038. will temporarily be changed to `time'."
  2039. :group 'org-refile
  2040. :group 'org-progress
  2041. :version "24.1"
  2042. :type '(choice
  2043. (const :tag "No logging" nil)
  2044. (const :tag "Record timestamp" time)
  2045. (const :tag "Record timestamp with note." note)))
  2046. (defcustom org-refile-targets nil
  2047. "Targets for refiling entries with \\[org-refile].
  2048. This is a list of cons cells. Each cell contains:
  2049. - a specification of the files to be considered, either a list of files,
  2050. or a symbol whose function or variable value will be used to retrieve
  2051. a file name or a list of file names. If you use `org-agenda-files' for
  2052. that, all agenda files will be scanned for targets. Nil means consider
  2053. headings in the current buffer.
  2054. - A specification of how to find candidate refile targets. This may be
  2055. any of:
  2056. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  2057. This tag has to be present in all target headlines, inheritance will
  2058. not be considered.
  2059. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  2060. todo keyword.
  2061. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  2062. headlines that are refiling targets.
  2063. - a cons cell (:level . N). Any headline of level N is considered a target.
  2064. Note that, when `org-odd-levels-only' is set, level corresponds to
  2065. order in hierarchy, not to the number of stars.
  2066. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  2067. Note that, when `org-odd-levels-only' is set, level corresponds to
  2068. order in hierarchy, not to the number of stars.
  2069. Each element of this list generates a set of possible targets.
  2070. The union of these sets is presented (with completion) to
  2071. the user by `org-refile'.
  2072. You can set the variable `org-refile-target-verify-function' to a function
  2073. to verify each headline found by the simple criteria above.
  2074. When this variable is nil, all top-level headlines in the current buffer
  2075. are used, equivalent to the value `((nil . (:level . 1))'."
  2076. :group 'org-refile
  2077. :type '(repeat
  2078. (cons
  2079. (choice :value org-agenda-files
  2080. (const :tag "All agenda files" org-agenda-files)
  2081. (const :tag "Current buffer" nil)
  2082. (function) (variable) (file))
  2083. (choice :tag "Identify target headline by"
  2084. (cons :tag "Specific tag" (const :value :tag) (string))
  2085. (cons :tag "TODO keyword" (const :value :todo) (string))
  2086. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  2087. (cons :tag "Level number" (const :value :level) (integer))
  2088. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  2089. (defcustom org-refile-target-verify-function nil
  2090. "Function to verify if the headline at point should be a refile target.
  2091. The function will be called without arguments, with point at the
  2092. beginning of the headline. It should return t and leave point
  2093. where it is if the headline is a valid target for refiling.
  2094. If the target should not be selected, the function must return nil.
  2095. In addition to this, it may move point to a place from where the search
  2096. should be continued. For example, the function may decide that the entire
  2097. subtree of the current entry should be excluded and move point to the end
  2098. of the subtree."
  2099. :group 'org-refile
  2100. :type '(choice
  2101. (const nil)
  2102. (function)))
  2103. (defcustom org-refile-use-cache nil
  2104. "Non-nil means cache refile targets to speed up the process.
  2105. \\<org-mode-map>\
  2106. The cache for a particular file will be updated automatically when
  2107. the buffer has been killed, or when any of the marker used for flagging
  2108. refile targets no longer points at a live buffer.
  2109. If you have added new entries to a buffer that might themselves be targets,
  2110. you need to clear the cache manually by pressing `C-0 \\[org-refile]' or,
  2111. if you find that easier, \
  2112. `\\[universal-argument] \\[universal-argument] \\[universal-argument] \
  2113. \\[org-refile]'."
  2114. :group 'org-refile
  2115. :version "24.1"
  2116. :type 'boolean)
  2117. (defcustom org-refile-use-outline-path nil
  2118. "Non-nil means provide refile targets as paths.
  2119. So a level 3 headline will be available as level1/level2/level3.
  2120. When the value is `file', also include the file name (without directory)
  2121. into the path. In this case, you can also stop the completion after
  2122. the file name, to get entries inserted as top level in the file.
  2123. When `full-file-path', include the full file path."
  2124. :group 'org-refile
  2125. :type '(choice
  2126. (const :tag "Not" nil)
  2127. (const :tag "Yes" t)
  2128. (const :tag "Start with file name" file)
  2129. (const :tag "Start with full file path" full-file-path)))
  2130. (defcustom org-outline-path-complete-in-steps t
  2131. "Non-nil means complete the outline path in hierarchical steps.
  2132. When Org-mode uses the refile interface to select an outline path
  2133. \(see variable `org-refile-use-outline-path'), the completion of
  2134. the path can be done in a single go, or it can be done in steps down
  2135. the headline hierarchy. Going in steps is probably the best if you
  2136. do not use a special completion package like `ido' or `icicles'.
  2137. However, when using these packages, going in one step can be very
  2138. fast, while still showing the whole path to the entry."
  2139. :group 'org-refile
  2140. :type 'boolean)
  2141. (defcustom org-refile-allow-creating-parent-nodes nil
  2142. "Non-nil means allow to create new nodes as refile targets.
  2143. New nodes are then created by adding \"/new node name\" to the completion
  2144. of an existing node. When the value of this variable is `confirm',
  2145. new node creation must be confirmed by the user (recommended).
  2146. When nil, the completion must match an existing entry.
  2147. Note that, if the new heading is not seen by the criteria
  2148. listed in `org-refile-targets', multiple instances of the same
  2149. heading would be created by trying again to file under the new
  2150. heading."
  2151. :group 'org-refile
  2152. :type '(choice
  2153. (const :tag "Never" nil)
  2154. (const :tag "Always" t)
  2155. (const :tag "Prompt for confirmation" confirm)))
  2156. (defcustom org-refile-active-region-within-subtree nil
  2157. "Non-nil means also refile active region within a subtree.
  2158. By default `org-refile' doesn't allow refiling regions if they
  2159. don't contain a set of subtrees, but it might be convenient to
  2160. do so sometimes: in that case, the first line of the region is
  2161. converted to a headline before refiling."
  2162. :group 'org-refile
  2163. :version "24.1"
  2164. :type 'boolean)
  2165. (defgroup org-todo nil
  2166. "Options concerning TODO items in Org-mode."
  2167. :tag "Org TODO"
  2168. :group 'org)
  2169. (defgroup org-progress nil
  2170. "Options concerning Progress logging in Org-mode."
  2171. :tag "Org Progress"
  2172. :group 'org-time)
  2173. (defvar org-todo-interpretation-widgets
  2174. '((:tag "Sequence (cycling hits every state)" sequence)
  2175. (:tag "Type (cycling directly to DONE)" type))
  2176. "The available interpretation symbols for customizing `org-todo-keywords'.
  2177. Interested libraries should add to this list.")
  2178. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  2179. "List of TODO entry keyword sequences and their interpretation.
  2180. \\<org-mode-map>This is a list of sequences.
  2181. Each sequence starts with a symbol, either `sequence' or `type',
  2182. indicating if the keywords should be interpreted as a sequence of
  2183. action steps, or as different types of TODO items. The first
  2184. keywords are states requiring action - these states will select a headline
  2185. for inclusion into the global TODO list Org-mode produces. If one of
  2186. the \"keywords\" is the vertical bar, \"|\", the remaining keywords
  2187. signify that no further action is necessary. If \"|\" is not found,
  2188. the last keyword is treated as the only DONE state of the sequence.
  2189. The command \\[org-todo] cycles an entry through these states, and one
  2190. additional state where no keyword is present. For details about this
  2191. cycling, see the manual.
  2192. TODO keywords and interpretation can also be set on a per-file basis with
  2193. the special #+SEQ_TODO and #+TYP_TODO lines.
  2194. Each keyword can optionally specify a character for fast state selection
  2195. \(in combination with the variable `org-use-fast-todo-selection')
  2196. and specifiers for state change logging, using the same syntax that
  2197. is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says that
  2198. the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  2199. indicates to record a time stamp each time this state is selected.
  2200. Each keyword may also specify if a timestamp or a note should be
  2201. recorded when entering or leaving the state, by adding additional
  2202. characters in the parenthesis after the keyword. This looks like this:
  2203. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  2204. record only the time of the state change. With X and Y being either
  2205. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  2206. Y when leaving the state if and only if the *target* state does not
  2207. define X. You may omit any of the fast-selection key or X or /Y,
  2208. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  2209. For backward compatibility, this variable may also be just a list
  2210. of keywords. In this case the interpretation (sequence or type) will be
  2211. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  2212. :group 'org-todo
  2213. :group 'org-keywords
  2214. :type '(choice
  2215. (repeat :tag "Old syntax, just keywords"
  2216. (string :tag "Keyword"))
  2217. (repeat :tag "New syntax"
  2218. (cons
  2219. (choice
  2220. :tag "Interpretation"
  2221. ;;Quick and dirty way to see
  2222. ;;`org-todo-interpretations'. This takes the
  2223. ;;place of item arguments
  2224. :convert-widget
  2225. (lambda (widget)
  2226. (widget-put widget
  2227. :args (mapcar
  2228. (lambda (x)
  2229. (widget-convert
  2230. (cons 'const x)))
  2231. org-todo-interpretation-widgets))
  2232. widget))
  2233. (repeat
  2234. (string :tag "Keyword"))))))
  2235. (defvar-local org-todo-keywords-1 nil
  2236. "All TODO and DONE keywords active in a buffer.")
  2237. (defvar org-todo-keywords-for-agenda nil)
  2238. (defvar org-done-keywords-for-agenda nil)
  2239. (defvar org-todo-keyword-alist-for-agenda nil)
  2240. (defvar org-tag-alist-for-agenda nil
  2241. "Alist of all tags from all agenda files.")
  2242. (defvar org-tag-groups-alist-for-agenda nil
  2243. "Alist of all groups tags from all current agenda files.")
  2244. (defvar-local org-tag-groups-alist nil)
  2245. (defvar org-agenda-contributing-files nil)
  2246. (defvar-local org-not-done-keywords nil)
  2247. (defvar-local org-done-keywords nil)
  2248. (defvar-local org-todo-heads nil)
  2249. (defvar-local org-todo-sets nil)
  2250. (defvar-local org-todo-log-states nil)
  2251. (defvar-local org-todo-kwd-alist nil)
  2252. (defvar-local org-todo-key-alist nil)
  2253. (defvar-local org-todo-key-trigger nil)
  2254. (defcustom org-todo-interpretation 'sequence
  2255. "Controls how TODO keywords are interpreted.
  2256. This variable is in principle obsolete and is only used for
  2257. backward compatibility, if the interpretation of todo keywords is
  2258. not given already in `org-todo-keywords'. See that variable for
  2259. more information."
  2260. :group 'org-todo
  2261. :group 'org-keywords
  2262. :type '(choice (const sequence)
  2263. (const type)))
  2264. (defcustom org-use-fast-todo-selection t
  2265. "\\<org-mode-map>\
  2266. Non-nil means use the fast todo selection scheme with \\[org-todo].
  2267. This variable describes if and under what circumstances the cycling
  2268. mechanism for TODO keywords will be replaced by a single-key, direct
  2269. selection scheme.
  2270. When nil, fast selection is never used.
  2271. When the symbol `prefix', it will be used when `org-todo' is called
  2272. with a prefix argument, i.e. `\\[universal-argument] \\[org-todo]' \
  2273. in an Org-mode buffer, and
  2274. `\\[universal-argument] t' in an agenda buffer.
  2275. When t, fast selection is used by default. In this case, the prefix
  2276. argument forces cycling instead.
  2277. In all cases, the special interface is only used if access keys have
  2278. actually been assigned by the user, i.e. if keywords in the configuration
  2279. are followed by a letter in parenthesis, like TODO(t)."
  2280. :group 'org-todo
  2281. :type '(choice
  2282. (const :tag "Never" nil)
  2283. (const :tag "By default" t)
  2284. (const :tag "Only with C-u C-c C-t" prefix)))
  2285. (defcustom org-provide-todo-statistics t
  2286. "Non-nil means update todo statistics after insert and toggle.
  2287. ALL-HEADLINES means update todo statistics by including headlines
  2288. with no TODO keyword as well, counting them as not done.
  2289. A list of TODO keywords means the same, but skip keywords that are
  2290. not in this list.
  2291. When set to a list of two lists, the first list contains keywords
  2292. to consider as TODO keywords, the second list contains keywords
  2293. to consider as DONE keywords.
  2294. When this is set, todo statistics is updated in the parent of the
  2295. current entry each time a todo state is changed."
  2296. :group 'org-todo
  2297. :type '(choice
  2298. (const :tag "Yes, only for TODO entries" t)
  2299. (const :tag "Yes, including all entries" all-headlines)
  2300. (repeat :tag "Yes, for TODOs in this list"
  2301. (string :tag "TODO keyword"))
  2302. (list :tag "Yes, for TODOs and DONEs in these lists"
  2303. (repeat (string :tag "TODO keyword"))
  2304. (repeat (string :tag "DONE keyword")))
  2305. (other :tag "No TODO statistics" nil)))
  2306. (defcustom org-hierarchical-todo-statistics t
  2307. "Non-nil means TODO statistics covers just direct children.
  2308. When nil, all entries in the subtree are considered.
  2309. This has only an effect if `org-provide-todo-statistics' is set.
  2310. To set this to nil for only a single subtree, use a COOKIE_DATA
  2311. property and include the word \"recursive\" into the value."
  2312. :group 'org-todo
  2313. :type 'boolean)
  2314. (defcustom org-after-todo-state-change-hook nil
  2315. "Hook which is run after the state of a TODO item was changed.
  2316. The new state (a string with a TODO keyword, or nil) is available in the
  2317. Lisp variable `org-state'."
  2318. :group 'org-todo
  2319. :type 'hook)
  2320. (defvar org-blocker-hook nil
  2321. "Hook for functions that are allowed to block a state change.
  2322. Functions in this hook should not modify the buffer.
  2323. Each function gets as its single argument a property list,
  2324. see `org-trigger-hook' for more information about this list.
  2325. If any of the functions in this hook returns nil, the state change
  2326. is blocked.")
  2327. (defvar org-trigger-hook nil
  2328. "Hook for functions that are triggered by a state change.
  2329. Each function gets as its single argument a property list with at
  2330. least the following elements:
  2331. (:type type-of-change :position pos-at-entry-start
  2332. :from old-state :to new-state)
  2333. Depending on the type, more properties may be present.
  2334. This mechanism is currently implemented for:
  2335. TODO state changes
  2336. ------------------
  2337. :type todo-state-change
  2338. :from previous state (keyword as a string), or nil, or a symbol
  2339. `todo' or `done', to indicate the general type of state.
  2340. :to new state, like in :from")
  2341. (defcustom org-enforce-todo-dependencies nil
  2342. "Non-nil means undone TODO entries will block switching the parent to DONE.
  2343. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  2344. be blocked if any prior sibling is not yet done.
  2345. Finally, if the parent is blocked because of ordered siblings of its own,
  2346. the child will also be blocked."
  2347. :set (lambda (var val)
  2348. (set var val)
  2349. (if val
  2350. (add-hook 'org-blocker-hook
  2351. 'org-block-todo-from-children-or-siblings-or-parent)
  2352. (remove-hook 'org-blocker-hook
  2353. 'org-block-todo-from-children-or-siblings-or-parent)))
  2354. :group 'org-todo
  2355. :type 'boolean)
  2356. (defcustom org-enforce-todo-checkbox-dependencies nil
  2357. "Non-nil means unchecked boxes will block switching the parent to DONE.
  2358. When this is nil, checkboxes have no influence on switching TODO states.
  2359. When non-nil, you first need to check off all check boxes before the TODO
  2360. entry can be switched to DONE.
  2361. This variable needs to be set before org.el is loaded, and you need to
  2362. restart Emacs after a change to make the change effective. The only way
  2363. to change is while Emacs is running is through the customize interface."
  2364. :set (lambda (var val)
  2365. (set var val)
  2366. (if val
  2367. (add-hook 'org-blocker-hook
  2368. 'org-block-todo-from-checkboxes)
  2369. (remove-hook 'org-blocker-hook
  2370. 'org-block-todo-from-checkboxes)))
  2371. :group 'org-todo
  2372. :type 'boolean)
  2373. (defcustom org-treat-insert-todo-heading-as-state-change nil
  2374. "Non-nil means inserting a TODO heading is treated as state change.
  2375. So when the command \\[org-insert-todo-heading] is used, state change
  2376. logging will apply if appropriate. When nil, the new TODO item will
  2377. be inserted directly, and no logging will take place."
  2378. :group 'org-todo
  2379. :type 'boolean)
  2380. (defcustom org-treat-S-cursor-todo-selection-as-state-change t
  2381. "Non-nil means switching TODO states with S-cursor counts as state change.
  2382. This is the default behavior. However, setting this to nil allows a
  2383. convenient way to select a TODO state and bypass any logging associated
  2384. with that."
  2385. :group 'org-todo
  2386. :type 'boolean)
  2387. (defcustom org-todo-state-tags-triggers nil
  2388. "Tag changes that should be triggered by TODO state changes.
  2389. This is a list. Each entry is
  2390. (state-change (tag . flag) .......)
  2391. State-change can be a string with a state, and empty string to indicate the
  2392. state that has no TODO keyword, or it can be one of the symbols `todo'
  2393. or `done', meaning any not-done or done state, respectively."
  2394. :group 'org-todo
  2395. :group 'org-tags
  2396. :type '(repeat
  2397. (cons (choice :tag "When changing to"
  2398. (const :tag "Not-done state" todo)
  2399. (const :tag "Done state" done)
  2400. (string :tag "State"))
  2401. (repeat
  2402. (cons :tag "Tag action"
  2403. (string :tag "Tag")
  2404. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  2405. (defcustom org-log-done nil
  2406. "Information to record when a task moves to the DONE state.
  2407. Possible values are:
  2408. nil Don't add anything, just change the keyword
  2409. time Add a time stamp to the task
  2410. note Prompt for a note and add it with template `org-log-note-headings'
  2411. This option can also be set with on a per-file-basis with
  2412. #+STARTUP: nologdone
  2413. #+STARTUP: logdone
  2414. #+STARTUP: lognotedone
  2415. You can have local logging settings for a subtree by setting the LOGGING
  2416. property to one or more of these keywords."
  2417. :group 'org-todo
  2418. :group 'org-progress
  2419. :type '(choice
  2420. (const :tag "No logging" nil)
  2421. (const :tag "Record CLOSED timestamp" time)
  2422. (const :tag "Record CLOSED timestamp with note." note)))
  2423. ;; Normalize old uses of org-log-done.
  2424. (cond
  2425. ((eq org-log-done t) (setq org-log-done 'time))
  2426. ((and (listp org-log-done) (memq 'done org-log-done))
  2427. (setq org-log-done 'note)))
  2428. (defcustom org-log-reschedule nil
  2429. "Information to record when the scheduling date of a tasks is modified.
  2430. Possible values are:
  2431. nil Don't add anything, just change the date
  2432. time Add a time stamp to the task
  2433. note Prompt for a note and add it with template `org-log-note-headings'
  2434. This option can also be set with on a per-file-basis with
  2435. #+STARTUP: nologreschedule
  2436. #+STARTUP: logreschedule
  2437. #+STARTUP: lognotereschedule"
  2438. :group 'org-todo
  2439. :group 'org-progress
  2440. :type '(choice
  2441. (const :tag "No logging" nil)
  2442. (const :tag "Record timestamp" time)
  2443. (const :tag "Record timestamp with note." note)))
  2444. (defcustom org-log-redeadline nil
  2445. "Information to record when the deadline date of a tasks is modified.
  2446. Possible values are:
  2447. nil Don't add anything, just change the date
  2448. time Add a time stamp to the task
  2449. note Prompt for a note and add it with template `org-log-note-headings'
  2450. This option can also be set with on a per-file-basis with
  2451. #+STARTUP: nologredeadline
  2452. #+STARTUP: logredeadline
  2453. #+STARTUP: lognoteredeadline
  2454. You can have local logging settings for a subtree by setting the LOGGING
  2455. property to one or more of these keywords."
  2456. :group 'org-todo
  2457. :group 'org-progress
  2458. :type '(choice
  2459. (const :tag "No logging" nil)
  2460. (const :tag "Record timestamp" time)
  2461. (const :tag "Record timestamp with note." note)))
  2462. (defcustom org-log-note-clock-out nil
  2463. "Non-nil means record a note when clocking out of an item.
  2464. This can also be configured on a per-file basis by adding one of
  2465. the following lines anywhere in the buffer:
  2466. #+STARTUP: lognoteclock-out
  2467. #+STARTUP: nolognoteclock-out"
  2468. :group 'org-todo
  2469. :group 'org-progress
  2470. :type 'boolean)
  2471. (defcustom org-log-done-with-time t
  2472. "Non-nil means the CLOSED time stamp will contain date and time.
  2473. When nil, only the date will be recorded."
  2474. :group 'org-progress
  2475. :type 'boolean)
  2476. (defcustom org-log-note-headings
  2477. '((done . "CLOSING NOTE %t")
  2478. (state . "State %-12s from %-12S %t")
  2479. (note . "Note taken on %t")
  2480. (reschedule . "Rescheduled from %S on %t")
  2481. (delschedule . "Not scheduled, was %S on %t")
  2482. (redeadline . "New deadline from %S on %t")
  2483. (deldeadline . "Removed deadline, was %S on %t")
  2484. (refile . "Refiled on %t")
  2485. (clock-out . ""))
  2486. "Headings for notes added to entries.
  2487. The value is an alist, with the car being a symbol indicating the
  2488. note context, and the cdr is the heading to be used. The heading
  2489. may also be the empty string. The following placeholders can be
  2490. used:
  2491. %t a time stamp.
  2492. %T an active time stamp instead the default inactive one
  2493. %d a short-format time stamp.
  2494. %D an active short-format time stamp.
  2495. %s the new TODO state or time stamp (inactive), in double quotes.
  2496. %S the old TODO state or time stamp (inactive), in double quotes.
  2497. %u the user name.
  2498. %U full user name.
  2499. In fact, it is not a good idea to change the `state' entry,
  2500. because Agenda Log mode depends on the format of these entries."
  2501. :group 'org-todo
  2502. :group 'org-progress
  2503. :type '(list :greedy t
  2504. (cons (const :tag "Heading when closing an item" done) string)
  2505. (cons (const :tag
  2506. "Heading when changing todo state (todo sequence only)"
  2507. state) string)
  2508. (cons (const :tag "Heading when just taking a note" note) string)
  2509. (cons (const :tag "Heading when rescheduling" reschedule) string)
  2510. (cons (const :tag "Heading when an item is no longer scheduled" delschedule) string)
  2511. (cons (const :tag "Heading when changing deadline" redeadline) string)
  2512. (cons (const :tag "Heading when deleting a deadline" deldeadline) string)
  2513. (cons (const :tag "Heading when refiling" refile) string)
  2514. (cons (const :tag "Heading when clocking out" clock-out) string)))
  2515. (unless (assq 'note org-log-note-headings)
  2516. (push '(note . "%t") org-log-note-headings))
  2517. (defcustom org-log-into-drawer nil
  2518. "Non-nil means insert state change notes and time stamps into a drawer.
  2519. When nil, state changes notes will be inserted after the headline and
  2520. any scheduling and clock lines, but not inside a drawer.
  2521. The value of this variable should be the name of the drawer to use.
  2522. LOGBOOK is proposed as the default drawer for this purpose, you can
  2523. also set this to a string to define the drawer of your choice.
  2524. A value of t is also allowed, representing \"LOGBOOK\".
  2525. A value of t or nil can also be set with on a per-file-basis with
  2526. #+STARTUP: logdrawer
  2527. #+STARTUP: nologdrawer
  2528. If this variable is set, `org-log-state-notes-insert-after-drawers'
  2529. will be ignored.
  2530. You can set the property LOG_INTO_DRAWER to overrule this setting for
  2531. a subtree.
  2532. Do not check directly this variable in a Lisp program. Call
  2533. function `org-log-into-drawer' instead."
  2534. :group 'org-todo
  2535. :group 'org-progress
  2536. :type '(choice
  2537. (const :tag "Not into a drawer" nil)
  2538. (const :tag "LOGBOOK" t)
  2539. (string :tag "Other")))
  2540. (org-defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer)
  2541. (defun org-log-into-drawer ()
  2542. "Name of the log drawer, as a string, or nil.
  2543. This is the value of `org-log-into-drawer'. However, if the
  2544. current entry has or inherits a LOG_INTO_DRAWER property, it will
  2545. be used instead of the default value."
  2546. (let ((p (org-entry-get nil "LOG_INTO_DRAWER" 'inherit t)))
  2547. (cond ((equal p "nil") nil)
  2548. ((equal p "t") "LOGBOOK")
  2549. ((stringp p) p)
  2550. (p "LOGBOOK")
  2551. ((stringp org-log-into-drawer) org-log-into-drawer)
  2552. (org-log-into-drawer "LOGBOOK"))))
  2553. (defcustom org-log-state-notes-insert-after-drawers nil
  2554. "Non-nil means insert state change notes after any drawers in entry.
  2555. Only the drawers that *immediately* follow the headline and the
  2556. deadline/scheduled line are skipped.
  2557. When nil, insert notes right after the heading and perhaps the line
  2558. with deadline/scheduling if present.
  2559. This variable will have no effect if `org-log-into-drawer' is
  2560. set."
  2561. :group 'org-todo
  2562. :group 'org-progress
  2563. :type 'boolean)
  2564. (defcustom org-log-states-order-reversed t
  2565. "Non-nil means the latest state note will be directly after heading.
  2566. When nil, the state change notes will be ordered according to time.
  2567. This option can also be set with on a per-file-basis with
  2568. #+STARTUP: logstatesreversed
  2569. #+STARTUP: nologstatesreversed"
  2570. :group 'org-todo
  2571. :group 'org-progress
  2572. :type 'boolean)
  2573. (defcustom org-todo-repeat-to-state nil
  2574. "The TODO state to which a repeater should return the repeating task.
  2575. By default this is the first task in a TODO sequence, or the previous state
  2576. in a TODO_TYP set. But you can specify another task here.
  2577. alternatively, set the :REPEAT_TO_STATE: property of the entry."
  2578. :group 'org-todo
  2579. :version "24.1"
  2580. :type '(choice (const :tag "Head of sequence" nil)
  2581. (string :tag "Specific state")))
  2582. (defcustom org-log-repeat 'time
  2583. "Non-nil means record moving through the DONE state when triggering repeat.
  2584. An auto-repeating task is immediately switched back to TODO when
  2585. marked DONE. If you are not logging state changes (by adding \"@\"
  2586. or \"!\" to the TODO keyword definition), or set `org-log-done' to
  2587. record a closing note, there will be no record of the task moving
  2588. through DONE. This variable forces taking a note anyway.
  2589. nil Don't force a record
  2590. time Record a time stamp
  2591. note Prompt for a note and add it with template `org-log-note-headings'
  2592. This option can also be set with on a per-file-basis with
  2593. #+STARTUP: nologrepeat
  2594. #+STARTUP: logrepeat
  2595. #+STARTUP: lognoterepeat
  2596. You can have local logging settings for a subtree by setting the LOGGING
  2597. property to one or more of these keywords."
  2598. :group 'org-todo
  2599. :group 'org-progress
  2600. :type '(choice
  2601. (const :tag "Don't force a record" nil)
  2602. (const :tag "Force recording the DONE state" time)
  2603. (const :tag "Force recording a note with the DONE state" note)))
  2604. (defgroup org-priorities nil
  2605. "Priorities in Org-mode."
  2606. :tag "Org Priorities"
  2607. :group 'org-todo)
  2608. (defcustom org-enable-priority-commands t
  2609. "Non-nil means priority commands are active.
  2610. When nil, these commands will be disabled, so that you never accidentally
  2611. set a priority."
  2612. :group 'org-priorities
  2613. :type 'boolean)
  2614. (defcustom org-highest-priority ?A
  2615. "The highest priority of TODO items. A character like ?A, ?B etc.
  2616. Must have a smaller ASCII number than `org-lowest-priority'."
  2617. :group 'org-priorities
  2618. :type 'character)
  2619. (defcustom org-lowest-priority ?C
  2620. "The lowest priority of TODO items. A character like ?A, ?B etc.
  2621. Must have a larger ASCII number than `org-highest-priority'."
  2622. :group 'org-priorities
  2623. :type 'character)
  2624. (defcustom org-default-priority ?B
  2625. "The default priority of TODO items.
  2626. This is the priority an item gets if no explicit priority is given.
  2627. When starting to cycle on an empty priority the first step in the cycle
  2628. depends on `org-priority-start-cycle-with-default'. The resulting first
  2629. step priority must not exceed the range from `org-highest-priority' to
  2630. `org-lowest-priority' which means that `org-default-priority' has to be
  2631. in this range exclusive or inclusive the range boundaries. Else the
  2632. first step refuses to set the default and the second will fall back
  2633. to (depending on the command used) the highest or lowest priority."
  2634. :group 'org-priorities
  2635. :type 'character)
  2636. (defcustom org-priority-start-cycle-with-default t
  2637. "Non-nil means start with default priority when starting to cycle.
  2638. When this is nil, the first step in the cycle will be (depending on the
  2639. command used) one higher or lower than the default priority.
  2640. See also `org-default-priority'."
  2641. :group 'org-priorities
  2642. :type 'boolean)
  2643. (defcustom org-get-priority-function nil
  2644. "Function to extract the priority from a string.
  2645. The string is normally the headline. If this is nil Org computes the
  2646. priority from the priority cookie like [#A] in the headline. It returns
  2647. an integer, increasing by 1000 for each priority level.
  2648. The user can set a different function here, which should take a string
  2649. as an argument and return the numeric priority."
  2650. :group 'org-priorities
  2651. :version "24.1"
  2652. :type '(choice
  2653. (const nil)
  2654. (function)))
  2655. (defgroup org-time nil
  2656. "Options concerning time stamps and deadlines in Org-mode."
  2657. :tag "Org Time"
  2658. :group 'org)
  2659. (defcustom org-time-stamp-rounding-minutes '(0 5)
  2660. "Number of minutes to round time stamps to.
  2661. \\<org-mode-map>\
  2662. These are two values, the first applies when first creating a time stamp.
  2663. The second applies when changing it with the commands `S-up' and `S-down'.
  2664. When changing the time stamp, this means that it will change in steps
  2665. of N minutes, as given by the second value.
  2666. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  2667. numbers should be factors of 60, so for example 5, 10, 15.
  2668. When this is larger than 1, you can still force an exact time stamp by using
  2669. a double prefix argument to a time stamp command like \
  2670. `\\[org-time-stamp]' or `\\[org-time-stamp-inactive],
  2671. and by using a prefix arg to `S-up/down' to specify the exact number
  2672. of minutes to shift."
  2673. :group 'org-time
  2674. :get (lambda (var) ; Make sure both elements are there
  2675. (if (integerp (default-value var))
  2676. (list (default-value var) 5)
  2677. (default-value var)))
  2678. :type '(list
  2679. (integer :tag "when inserting times")
  2680. (integer :tag "when modifying times")))
  2681. ;; Normalize old customizations of this variable.
  2682. (when (integerp org-time-stamp-rounding-minutes)
  2683. (setq org-time-stamp-rounding-minutes
  2684. (list org-time-stamp-rounding-minutes
  2685. org-time-stamp-rounding-minutes)))
  2686. (defcustom org-display-custom-times nil
  2687. "Non-nil means overlay custom formats over all time stamps.
  2688. The formats are defined through the variable `org-time-stamp-custom-formats'.
  2689. To turn this on on a per-file basis, insert anywhere in the file:
  2690. #+STARTUP: customtime"
  2691. :group 'org-time
  2692. :set 'set-default
  2693. :type 'sexp)
  2694. (make-variable-buffer-local 'org-display-custom-times)
  2695. (defcustom org-time-stamp-custom-formats
  2696. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  2697. "Custom formats for time stamps. See `format-time-string' for the syntax.
  2698. These are overlaid over the default ISO format if the variable
  2699. `org-display-custom-times' is set. Time like %H:%M should be at the
  2700. end of the second format. The custom formats are also honored by export
  2701. commands, if custom time display is turned on at the time of export."
  2702. :group 'org-time
  2703. :type 'sexp)
  2704. (defun org-time-stamp-format (&optional long inactive)
  2705. "Get the right format for a time string."
  2706. (let ((f (if long (cdr org-time-stamp-formats)
  2707. (car org-time-stamp-formats))))
  2708. (if inactive
  2709. (concat "[" (substring f 1 -1) "]")
  2710. f)))
  2711. (defcustom org-time-clocksum-format
  2712. '(:days "%dd " :hours "%d" :require-hours t :minutes ":%02d" :require-minutes t)
  2713. "The format string used when creating CLOCKSUM lines.
  2714. This is also used when Org mode generates a time duration.
  2715. The value can be a single format string containing two
  2716. %-sequences, which will be filled with the number of hours and
  2717. minutes in that order.
  2718. Alternatively, the value can be a plist associating any of the
  2719. keys :years, :months, :weeks, :days, :hours or :minutes with
  2720. format strings. The time duration is formatted using only the
  2721. time components that are needed and concatenating the results.
  2722. If a time unit in absent, it falls back to the next smallest
  2723. unit.
  2724. The keys :require-years, :require-months, :require-days,
  2725. :require-weeks, :require-hours, :require-minutes are also
  2726. meaningful. A non-nil value for these keys indicates that the
  2727. corresponding time component should always be included, even if
  2728. its value is 0.
  2729. For example,
  2730. (:days \"%dd\" :hours \"%d\" :require-hours t :minutes \":%02d\"
  2731. :require-minutes t)
  2732. means durations longer than a day will be expressed in days,
  2733. hours and minutes, and durations less than a day will always be
  2734. expressed in hours and minutes (even for durations less than an
  2735. hour).
  2736. The value
  2737. (:days \"%dd\" :minutes \"%dm\")
  2738. means durations longer than a day will be expressed in days and
  2739. minutes, and durations less than a day will be expressed entirely
  2740. in minutes (even for durations longer than an hour)."
  2741. :group 'org-time
  2742. :group 'org-clock
  2743. :version "24.4"
  2744. :package-version '(Org . "8.0")
  2745. :type '(choice (string :tag "Format string")
  2746. (set :tag "Plist"
  2747. (group :inline t (const :tag "Years" :years)
  2748. (string :tag "Format string"))
  2749. (group :inline t
  2750. (const :tag "Always show years" :require-years)
  2751. (const t))
  2752. (group :inline t (const :tag "Months" :months)
  2753. (string :tag "Format string"))
  2754. (group :inline t
  2755. (const :tag "Always show months" :require-months)
  2756. (const t))
  2757. (group :inline t (const :tag "Weeks" :weeks)
  2758. (string :tag "Format string"))
  2759. (group :inline t
  2760. (const :tag "Always show weeks" :require-weeks)
  2761. (const t))
  2762. (group :inline t (const :tag "Days" :days)
  2763. (string :tag "Format string"))
  2764. (group :inline t
  2765. (const :tag "Always show days" :require-days)
  2766. (const t))
  2767. (group :inline t (const :tag "Hours" :hours)
  2768. (string :tag "Format string"))
  2769. (group :inline t
  2770. (const :tag "Always show hours" :require-hours)
  2771. (const t))
  2772. (group :inline t (const :tag "Minutes" :minutes)
  2773. (string :tag "Format string"))
  2774. (group :inline t
  2775. (const :tag "Always show minutes" :require-minutes)
  2776. (const t)))))
  2777. (defcustom org-time-clocksum-use-fractional nil
  2778. "When non-nil, \\[org-clock-display] uses fractional times.
  2779. See `org-time-clocksum-format' for more on time clock formats."
  2780. :group 'org-time
  2781. :group 'org-clock
  2782. :version "24.3"
  2783. :type 'boolean)
  2784. (defcustom org-time-clocksum-use-effort-durations nil
  2785. "When non-nil, \\[org-clock-display] uses effort durations.
  2786. E.g. by default, one day is considered to be a 8 hours effort,
  2787. so a task that has been clocked for 16 hours will be displayed
  2788. as during 2 days in the clock display or in the clocktable.
  2789. See `org-effort-durations' on how to set effort durations
  2790. and `org-time-clocksum-format' for more on time clock formats."
  2791. :group 'org-time
  2792. :group 'org-clock
  2793. :version "24.4"
  2794. :package-version '(Org . "8.0")
  2795. :type 'boolean)
  2796. (defcustom org-time-clocksum-fractional-format "%.2f"
  2797. "The format string used when creating CLOCKSUM lines,
  2798. or when Org mode generates a time duration, if
  2799. `org-time-clocksum-use-fractional' is enabled.
  2800. The value can be a single format string containing one
  2801. %-sequence, which will be filled with the number of hours as
  2802. a float.
  2803. Alternatively, the value can be a plist associating any of the
  2804. keys :years, :months, :weeks, :days, :hours or :minutes with
  2805. a format string. The time duration is formatted using the
  2806. largest time unit which gives a non-zero integer part. If all
  2807. specified formats have zero integer part, the smallest time unit
  2808. is used."
  2809. :group 'org-time
  2810. :type '(choice (string :tag "Format string")
  2811. (set (group :inline t (const :tag "Years" :years)
  2812. (string :tag "Format string"))
  2813. (group :inline t (const :tag "Months" :months)
  2814. (string :tag "Format string"))
  2815. (group :inline t (const :tag "Weeks" :weeks)
  2816. (string :tag "Format string"))
  2817. (group :inline t (const :tag "Days" :days)
  2818. (string :tag "Format string"))
  2819. (group :inline t (const :tag "Hours" :hours)
  2820. (string :tag "Format string"))
  2821. (group :inline t (const :tag "Minutes" :minutes)
  2822. (string :tag "Format string")))))
  2823. (defcustom org-deadline-warning-days 14
  2824. "Number of days before expiration during which a deadline becomes active.
  2825. This variable governs the display in sparse trees and in the agenda.
  2826. When 0 or negative, it means use this number (the absolute value of it)
  2827. even if a deadline has a different individual lead time specified.
  2828. Custom commands can set this variable in the options section."
  2829. :group 'org-time
  2830. :group 'org-agenda-daily/weekly
  2831. :type 'integer)
  2832. (defcustom org-scheduled-delay-days 0
  2833. "Number of days before a scheduled item becomes active.
  2834. This variable governs the display in sparse trees and in the agenda.
  2835. The default value (i.e. 0) means: don't delay scheduled item.
  2836. When negative, it means use this number (the absolute value of it)
  2837. even if a scheduled item has a different individual delay time
  2838. specified.
  2839. Custom commands can set this variable in the options section."
  2840. :group 'org-time
  2841. :group 'org-agenda-daily/weekly
  2842. :version "24.4"
  2843. :package-version '(Org . "8.0")
  2844. :type 'integer)
  2845. (defcustom org-read-date-prefer-future t
  2846. "Non-nil means assume future for incomplete date input from user.
  2847. This affects the following situations:
  2848. 1. The user gives a month but not a year.
  2849. For example, if it is April and you enter \"feb 2\", this will be read
  2850. as Feb 2, *next* year. \"May 5\", however, will be this year.
  2851. 2. The user gives a day, but no month.
  2852. For example, if today is the 15th, and you enter \"3\", Org-mode will
  2853. read this as the third of *next* month. However, if you enter \"17\",
  2854. it will be considered as *this* month.
  2855. If you set this variable to the symbol `time', then also the following
  2856. will work:
  2857. 3. If the user gives a time.
  2858. If the time is before now, it will be interpreted as tomorrow.
  2859. Currently none of this works for ISO week specifications.
  2860. When this option is nil, the current day, month and year will always be
  2861. used as defaults.
  2862. See also `org-agenda-jump-prefer-future'."
  2863. :group 'org-time
  2864. :type '(choice
  2865. (const :tag "Never" nil)
  2866. (const :tag "Check month and day" t)
  2867. (const :tag "Check month, day, and time" time)))
  2868. (defcustom org-agenda-jump-prefer-future 'org-read-date-prefer-future
  2869. "Should the agenda jump command prefer the future for incomplete dates?
  2870. The default is to do the same as configured in `org-read-date-prefer-future'.
  2871. But you can also set a deviating value here.
  2872. This may t or nil, or the symbol `org-read-date-prefer-future'."
  2873. :group 'org-agenda
  2874. :group 'org-time
  2875. :version "24.1"
  2876. :type '(choice
  2877. (const :tag "Use org-read-date-prefer-future"
  2878. org-read-date-prefer-future)
  2879. (const :tag "Never" nil)
  2880. (const :tag "Always" t)))
  2881. (defcustom org-read-date-force-compatible-dates t
  2882. "Should date/time prompt force dates that are guaranteed to work in Emacs?
  2883. Depending on the system Emacs is running on, certain dates cannot
  2884. be represented with the type used internally to represent time.
  2885. Dates between 1970-1-1 and 2038-1-1 can always be represented
  2886. correctly. Some systems allow for earlier dates, some for later,
  2887. some for both. One way to find out it to insert any date into an
  2888. Org buffer, putting the cursor on the year and hitting S-up and
  2889. S-down to test the range.
  2890. When this variable is set to t, the date/time prompt will not let
  2891. you specify dates outside the 1970-2037 range, so it is certain that
  2892. these dates will work in whatever version of Emacs you are
  2893. running, and also that you can move a file from one Emacs implementation
  2894. to another. WHenever Org is forcing the year for you, it will display
  2895. a message and beep.
  2896. When this variable is nil, Org will check if the date is
  2897. representable in the specific Emacs implementation you are using.
  2898. If not, it will force a year, usually the current year, and beep
  2899. to remind you. Currently this setting is not recommended because
  2900. the likelihood that you will open your Org files in an Emacs that
  2901. has limited date range is not negligible.
  2902. A workaround for this problem is to use diary sexp dates for time
  2903. stamps outside of this range."
  2904. :group 'org-time
  2905. :version "24.1"
  2906. :type 'boolean)
  2907. (defcustom org-read-date-display-live t
  2908. "Non-nil means display current interpretation of date prompt live.
  2909. This display will be in an overlay, in the minibuffer."
  2910. :group 'org-time
  2911. :type 'boolean)
  2912. (defcustom org-read-date-popup-calendar t
  2913. "Non-nil means pop up a calendar when prompting for a date.
  2914. In the calendar, the date can be selected with mouse-1. However, the
  2915. minibuffer will also be active, and you can simply enter the date as well.
  2916. When nil, only the minibuffer will be available."
  2917. :group 'org-time
  2918. :type 'boolean)
  2919. (org-defvaralias 'org-popup-calendar-for-date-prompt
  2920. 'org-read-date-popup-calendar)
  2921. (make-obsolete-variable
  2922. 'org-read-date-minibuffer-setup-hook
  2923. "Set `org-read-date-minibuffer-local-map' instead." "24.4")
  2924. (defcustom org-read-date-minibuffer-setup-hook nil
  2925. "Hook to be used to set up keys for the date/time interface.
  2926. Add key definitions to `minibuffer-local-map', which will be a
  2927. temporary copy.
  2928. WARNING: This option is obsolete, you should use
  2929. `org-read-date-minibuffer-local-map' to set up keys."
  2930. :group 'org-time
  2931. :type 'hook)
  2932. (defcustom org-extend-today-until 0
  2933. "The hour when your day really ends. Must be an integer.
  2934. This has influence for the following applications:
  2935. - When switching the agenda to \"today\". It it is still earlier than
  2936. the time given here, the day recognized as TODAY is actually yesterday.
  2937. - When a date is read from the user and it is still before the time given
  2938. here, the current date and time will be assumed to be yesterday, 23:59.
  2939. Also, timestamps inserted in capture templates follow this rule.
  2940. IMPORTANT: This is a feature whose implementation is and likely will
  2941. remain incomplete. Really, it is only here because past midnight seems to
  2942. be the favorite working time of John Wiegley :-)"
  2943. :group 'org-time
  2944. :type 'integer)
  2945. (defcustom org-use-effective-time nil
  2946. "If non-nil, consider `org-extend-today-until' when creating timestamps.
  2947. For example, if `org-extend-today-until' is 8, and it's 4am, then the
  2948. \"effective time\" of any timestamps between midnight and 8am will be
  2949. 23:59 of the previous day."
  2950. :group 'org-time
  2951. :version "24.1"
  2952. :type 'boolean)
  2953. (defcustom org-use-last-clock-out-time-as-effective-time nil
  2954. "When non-nil, use the last clock out time for `org-todo'.
  2955. Note that this option has precedence over the combined use of
  2956. `org-use-effective-time' and `org-extend-today-until'."
  2957. :group 'org-time
  2958. :version "24.4"
  2959. :package-version '(Org . "8.0")
  2960. :type 'boolean)
  2961. (defcustom org-edit-timestamp-down-means-later nil
  2962. "Non-nil means S-down will increase the time in a time stamp.
  2963. When nil, S-up will increase."
  2964. :group 'org-time
  2965. :type 'boolean)
  2966. (defcustom org-calendar-follow-timestamp-change t
  2967. "Non-nil means make the calendar window follow timestamp changes.
  2968. When a timestamp is modified and the calendar window is visible, it will be
  2969. moved to the new date."
  2970. :group 'org-time
  2971. :type 'boolean)
  2972. (defgroup org-tags nil
  2973. "Options concerning tags in Org-mode."
  2974. :tag "Org Tags"
  2975. :group 'org)
  2976. (defcustom org-tag-alist nil
  2977. "List of tags allowed in Org-mode files.
  2978. When this list is nil, Org-mode will base TAG input on what is already in the
  2979. buffer.
  2980. The value of this variable is an alist, the car of each entry must be a
  2981. keyword as a string, the cdr may be a character that is used to select
  2982. that tag through the fast-tag-selection interface.
  2983. See the manual for details."
  2984. :group 'org-tags
  2985. :type '(repeat
  2986. (choice
  2987. (cons (string :tag "Tag name")
  2988. (character :tag "Access char"))
  2989. (list :tag "Start radio group"
  2990. (const :startgroup)
  2991. (option (string :tag "Group description")))
  2992. (list :tag "Start tag group, non distinct"
  2993. (const :startgrouptag)
  2994. (option (string :tag "Group description")))
  2995. (list :tag "Group tags delimiter"
  2996. (const :grouptags))
  2997. (list :tag "End radio group"
  2998. (const :endgroup)
  2999. (option (string :tag "Group description")))
  3000. (list :tag "End tag group, non distinct"
  3001. (const :endgrouptag)
  3002. (option (string :tag "Group description")))
  3003. (const :tag "New line" (:newline)))))
  3004. (defcustom org-tag-persistent-alist nil
  3005. "List of tags that will always appear in all Org-mode files.
  3006. This is in addition to any in buffer settings or customizations
  3007. of `org-tag-alist'.
  3008. When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
  3009. The value of this variable is an alist, the car of each entry must be a
  3010. keyword as a string, the cdr may be a character that is used to select
  3011. that tag through the fast-tag-selection interface.
  3012. See the manual for details.
  3013. To disable these tags on a per-file basis, insert anywhere in the file:
  3014. #+STARTUP: noptag"
  3015. :group 'org-tags
  3016. :type '(repeat
  3017. (choice
  3018. (cons (string :tag "Tag name")
  3019. (character :tag "Access char"))
  3020. (const :tag "Start radio group" (:startgroup))
  3021. (const :tag "Group tags delimiter" (:grouptags))
  3022. (const :tag "End radio group" (:endgroup))
  3023. (const :tag "New line" (:newline)))))
  3024. (defcustom org-complete-tags-always-offer-all-agenda-tags nil
  3025. "If non-nil, always offer completion for all tags of all agenda files.
  3026. Instead of customizing this variable directly, you might want to
  3027. set it locally for capture buffers, because there no list of
  3028. tags in that file can be created dynamically (there are none).
  3029. (add-hook \\='org-capture-mode-hook
  3030. (lambda ()
  3031. (setq-local org-complete-tags-always-offer-all-agenda-tags t)))"
  3032. :group 'org-tags
  3033. :version "24.1"
  3034. :type 'boolean)
  3035. (defvar org-file-tags nil
  3036. "List of tags that can be inherited by all entries in the file.
  3037. The tags will be inherited if the variable `org-use-tag-inheritance'
  3038. says they should be.
  3039. This variable is populated from #+FILETAGS lines.")
  3040. (defcustom org-use-fast-tag-selection 'auto
  3041. "Non-nil means use fast tag selection scheme.
  3042. This is a special interface to select and deselect tags with single keys.
  3043. When nil, fast selection is never used.
  3044. When the symbol `auto', fast selection is used if and only if selection
  3045. characters for tags have been configured, either through the variable
  3046. `org-tag-alist' or through a #+TAGS line in the buffer.
  3047. When t, fast selection is always used and selection keys are assigned
  3048. automatically if necessary."
  3049. :group 'org-tags
  3050. :type '(choice
  3051. (const :tag "Always" t)
  3052. (const :tag "Never" nil)
  3053. (const :tag "When selection characters are configured" auto)))
  3054. (defcustom org-fast-tag-selection-single-key nil
  3055. "Non-nil means fast tag selection exits after first change.
  3056. When nil, you have to press RET to exit it.
  3057. During fast tag selection, you can toggle this flag with `C-c'.
  3058. This variable can also have the value `expert'. In this case, the window
  3059. displaying the tags menu is not even shown, until you press C-c again."
  3060. :group 'org-tags
  3061. :type '(choice
  3062. (const :tag "No" nil)
  3063. (const :tag "Yes" t)
  3064. (const :tag "Expert" expert)))
  3065. (defvar org-fast-tag-selection-include-todo nil
  3066. "Non-nil means fast tags selection interface will also offer TODO states.
  3067. This is an undocumented feature, you should not rely on it.")
  3068. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  3069. "The column to which tags should be indented in a headline.
  3070. If this number is positive, it specifies the column. If it is negative,
  3071. it means that the tags should be flushright to that column. For example,
  3072. -80 works well for a normal 80 character screen.
  3073. When 0, place tags directly after headline text, with only one space in
  3074. between."
  3075. :group 'org-tags
  3076. :type 'integer)
  3077. (defcustom org-auto-align-tags t
  3078. "Non-nil keeps tags aligned when modifying headlines.
  3079. Some operations (i.e. demoting) change the length of a headline and
  3080. therefore shift the tags around. With this option turned on, after
  3081. each such operation the tags are again aligned to `org-tags-column'."
  3082. :group 'org-tags
  3083. :type 'boolean)
  3084. (defcustom org-use-tag-inheritance t
  3085. "Non-nil means tags in levels apply also for sublevels.
  3086. When nil, only the tags directly given in a specific line apply there.
  3087. This may also be a list of tags that should be inherited, or a regexp that
  3088. matches tags that should be inherited. Additional control is possible
  3089. with the variable `org-tags-exclude-from-inheritance' which gives an
  3090. explicit list of tags to be excluded from inheritance, even if the value of
  3091. `org-use-tag-inheritance' would select it for inheritance.
  3092. If this option is t, a match early-on in a tree can lead to a large
  3093. number of matches in the subtree when constructing the agenda or creating
  3094. a sparse tree. If you only want to see the first match in a tree during
  3095. a search, check out the variable `org-tags-match-list-sublevels'."
  3096. :group 'org-tags
  3097. :type '(choice
  3098. (const :tag "Not" nil)
  3099. (const :tag "Always" t)
  3100. (repeat :tag "Specific tags" (string :tag "Tag"))
  3101. (regexp :tag "Tags matched by regexp")))
  3102. (defcustom org-tags-exclude-from-inheritance nil
  3103. "List of tags that should never be inherited.
  3104. This is a way to exclude a few tags from inheritance. For way to do
  3105. the opposite, to actively allow inheritance for selected tags,
  3106. see the variable `org-use-tag-inheritance'."
  3107. :group 'org-tags
  3108. :type '(repeat (string :tag "Tag")))
  3109. (defun org-tag-inherit-p (tag)
  3110. "Check if TAG is one that should be inherited."
  3111. (cond
  3112. ((member tag org-tags-exclude-from-inheritance) nil)
  3113. ((eq org-use-tag-inheritance t) t)
  3114. ((not org-use-tag-inheritance) nil)
  3115. ((stringp org-use-tag-inheritance)
  3116. (string-match org-use-tag-inheritance tag))
  3117. ((listp org-use-tag-inheritance)
  3118. (member tag org-use-tag-inheritance))
  3119. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  3120. (defcustom org-tags-match-list-sublevels t
  3121. "Non-nil means list also sublevels of headlines matching a search.
  3122. This variable applies to tags/property searches, and also to stuck
  3123. projects because this search is based on a tags match as well.
  3124. When set to the symbol `indented', sublevels are indented with
  3125. leading dots.
  3126. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  3127. the sublevels of a headline matching a tag search often also match
  3128. the same search. Listing all of them can create very long lists.
  3129. Setting this variable to nil causes subtrees of a match to be skipped.
  3130. This variable is semi-obsolete and probably should always be true. It
  3131. is better to limit inheritance to certain tags using the variables
  3132. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  3133. :group 'org-tags
  3134. :type '(choice
  3135. (const :tag "No, don't list them" nil)
  3136. (const :tag "Yes, do list them" t)
  3137. (const :tag "List them, indented with leading dots" indented)))
  3138. (defcustom org-tags-sort-function nil
  3139. "When set, tags are sorted using this function as a comparator."
  3140. :group 'org-tags
  3141. :type '(choice
  3142. (const :tag "No sorting" nil)
  3143. (const :tag "Alphabetical" string<)
  3144. (const :tag "Reverse alphabetical" string>)
  3145. (function :tag "Custom function" nil)))
  3146. (defvar org-tags-history nil
  3147. "History of minibuffer reads for tags.")
  3148. (defvar org-last-tags-completion-table nil
  3149. "The last used completion table for tags.")
  3150. (defvar org-after-tags-change-hook nil
  3151. "Hook that is run after the tags in a line have changed.")
  3152. (defgroup org-properties nil
  3153. "Options concerning properties in Org-mode."
  3154. :tag "Org Properties"
  3155. :group 'org)
  3156. (defcustom org-property-format "%-10s %s"
  3157. "How property key/value pairs should be formatted by `indent-line'.
  3158. When `indent-line' hits a property definition, it will format the line
  3159. according to this format, mainly to make sure that the values are
  3160. lined-up with respect to each other."
  3161. :group 'org-properties
  3162. :type 'string)
  3163. (defcustom org-properties-postprocess-alist nil
  3164. "Alist of properties and functions to adjust inserted values.
  3165. Elements of this alist must be of the form
  3166. ([string] [function])
  3167. where [string] must be a property name and [function] must be a
  3168. lambda expression: this lambda expression must take one argument,
  3169. the value to adjust, and return the new value as a string.
  3170. For example, this element will allow the property \"Remaining\"
  3171. to be updated wrt the relation between the \"Effort\" property
  3172. and the clock summary:
  3173. ((\"Remaining\" (lambda(value)
  3174. (let ((clocksum (org-clock-sum-current-item))
  3175. (effort (org-duration-string-to-minutes
  3176. (org-entry-get (point) \"Effort\"))))
  3177. (org-minutes-to-clocksum-string (- effort clocksum))))))"
  3178. :group 'org-properties
  3179. :version "24.1"
  3180. :type '(alist :key-type (string :tag "Property")
  3181. :value-type (function :tag "Function")))
  3182. (defcustom org-use-property-inheritance nil
  3183. "Non-nil means properties apply also for sublevels.
  3184. This setting is chiefly used during property searches. Turning it on can
  3185. cause significant overhead when doing a search, which is why it is not
  3186. on by default.
  3187. When nil, only the properties directly given in the current entry count.
  3188. When t, every property is inherited. The value may also be a list of
  3189. properties that should have inheritance, or a regular expression matching
  3190. properties that should be inherited.
  3191. However, note that some special properties use inheritance under special
  3192. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  3193. and the properties ending in \"_ALL\" when they are used as descriptor
  3194. for valid values of a property.
  3195. Note for programmers:
  3196. When querying an entry with `org-entry-get', you can control if inheritance
  3197. should be used. By default, `org-entry-get' looks only at the local
  3198. properties. You can request inheritance by setting the inherit argument
  3199. to t (to force inheritance) or to `selective' (to respect the setting
  3200. in this variable)."
  3201. :group 'org-properties
  3202. :type '(choice
  3203. (const :tag "Not" nil)
  3204. (const :tag "Always" t)
  3205. (repeat :tag "Specific properties" (string :tag "Property"))
  3206. (regexp :tag "Properties matched by regexp")))
  3207. (defun org-property-inherit-p (property)
  3208. "Check if PROPERTY is one that should be inherited."
  3209. (cond
  3210. ((eq org-use-property-inheritance t) t)
  3211. ((not org-use-property-inheritance) nil)
  3212. ((stringp org-use-property-inheritance)
  3213. (string-match org-use-property-inheritance property))
  3214. ((listp org-use-property-inheritance)
  3215. (member property org-use-property-inheritance))
  3216. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  3217. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  3218. "The default column format, if no other format has been defined.
  3219. This variable can be set on the per-file basis by inserting a line
  3220. #+COLUMNS: %25ITEM ....."
  3221. :group 'org-properties
  3222. :type 'string)
  3223. (defcustom org-columns-ellipses ".."
  3224. "The ellipses to be used when a field in column view is truncated.
  3225. When this is the empty string, as many characters as possible are shown,
  3226. but then there will be no visual indication that the field has been truncated.
  3227. When this is a string of length N, the last N characters of a truncated
  3228. field are replaced by this string. If the column is narrower than the
  3229. ellipses string, only part of the ellipses string will be shown."
  3230. :group 'org-properties
  3231. :type 'string)
  3232. (defcustom org-columns-modify-value-for-display-function nil
  3233. "Function that modifies values for display in column view.
  3234. For example, it can be used to cut out a certain part from a time stamp.
  3235. The function must take 2 arguments:
  3236. column-title The title of the column (*not* the property name)
  3237. value The value that should be modified.
  3238. The function should return the value that should be displayed,
  3239. or nil if the normal value should be used."
  3240. :group 'org-properties
  3241. :type '(choice (const nil) (function)))
  3242. (defconst org-global-properties-fixed
  3243. '(("VISIBILITY_ALL" . "folded children content all")
  3244. ("CLOCK_MODELINE_TOTAL_ALL" . "current today repeat all auto"))
  3245. "List of property/value pairs that can be inherited by any entry.
  3246. These are fixed values, for the preset properties. The user variable
  3247. that can be used to add to this list is `org-global-properties'.
  3248. The entries in this list are cons cells where the car is a property
  3249. name and cdr is a string with the value. If the value represents
  3250. multiple items like an \"_ALL\" property, separate the items by
  3251. spaces.")
  3252. (defcustom org-global-properties nil
  3253. "List of property/value pairs that can be inherited by any entry.
  3254. This list will be combined with the constant `org-global-properties-fixed'.
  3255. The entries in this list are cons cells where the car is a property
  3256. name and cdr is a string with the value.
  3257. You can set buffer-local values for the same purpose in the variable
  3258. `org-file-properties' this by adding lines like
  3259. #+PROPERTY: NAME VALUE"
  3260. :group 'org-properties
  3261. :type '(repeat
  3262. (cons (string :tag "Property")
  3263. (string :tag "Value"))))
  3264. (defvar-local org-file-properties nil
  3265. "List of property/value pairs that can be inherited by any entry.
  3266. Valid for the current buffer.
  3267. This variable is populated from #+PROPERTY lines.")
  3268. (defgroup org-agenda nil
  3269. "Options concerning agenda views in Org-mode."
  3270. :tag "Org Agenda"
  3271. :group 'org)
  3272. (defvar-local org-category nil
  3273. "Variable used by org files to set a category for agenda display.
  3274. Such files should use a file variable to set it, for example
  3275. # -*- mode: org; org-category: \"ELisp\"
  3276. or contain a special line
  3277. #+CATEGORY: ELisp
  3278. If the file does not specify a category, then file's base name
  3279. is used instead.")
  3280. (put 'org-category 'safe-local-variable (lambda (x) (or (symbolp x) (stringp x))))
  3281. (defcustom org-agenda-files nil
  3282. "The files to be used for agenda display.
  3283. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  3284. \\[org-remove-file]. You can also use customize to edit the list.
  3285. If an entry is a directory, all files in that directory that are matched by
  3286. `org-agenda-file-regexp' will be part of the file list.
  3287. If the value of the variable is not a list but a single file name, then
  3288. the list of agenda files is actually stored and maintained in that file, one
  3289. agenda file per line. In this file paths can be given relative to
  3290. `org-directory'. Tilde expansion and environment variable substitution
  3291. are also made."
  3292. :group 'org-agenda
  3293. :type '(choice
  3294. (repeat :tag "List of files and directories" file)
  3295. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  3296. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  3297. "Regular expression to match files for `org-agenda-files'.
  3298. If any element in the list in that variable contains a directory instead
  3299. of a normal file, all files in that directory that are matched by this
  3300. regular expression will be included."
  3301. :group 'org-agenda
  3302. :type 'regexp)
  3303. (defcustom org-agenda-text-search-extra-files nil
  3304. "List of extra files to be searched by text search commands.
  3305. These files will be searched in addition to the agenda files by the
  3306. commands `org-search-view' (`\\[org-agenda] s') \
  3307. and `org-occur-in-agenda-files'.
  3308. Note that these files will only be searched for text search commands,
  3309. not for the other agenda views like todo lists, tag searches or the weekly
  3310. agenda. This variable is intended to list notes and possibly archive files
  3311. that should also be searched by these two commands.
  3312. In fact, if the first element in the list is the symbol `agenda-archives',
  3313. then all archive files of all agenda files will be added to the search
  3314. scope."
  3315. :group 'org-agenda
  3316. :type '(set :greedy t
  3317. (const :tag "Agenda Archives" agenda-archives)
  3318. (repeat :inline t (file))))
  3319. (org-defvaralias 'org-agenda-multi-occur-extra-files
  3320. 'org-agenda-text-search-extra-files)
  3321. (defcustom org-agenda-skip-unavailable-files nil
  3322. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  3323. A nil value means to remove them, after a query, from the list."
  3324. :group 'org-agenda
  3325. :type 'boolean)
  3326. (defcustom org-calendar-to-agenda-key [?c]
  3327. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  3328. The command `org-calendar-goto-agenda' will be bound to this key. The
  3329. default is the character `c' because then `c' can be used to switch back and
  3330. forth between agenda and calendar."
  3331. :group 'org-agenda
  3332. :type 'sexp)
  3333. (defcustom org-calendar-insert-diary-entry-key [?i]
  3334. "The key to be installed in `calendar-mode-map' for adding diary entries.
  3335. This option is irrelevant until `org-agenda-diary-file' has been configured
  3336. to point to an Org-mode file. When that is the case, the command
  3337. `org-agenda-diary-entry' will be bound to the key given here, by default
  3338. `i'. In the calendar, `i' normally adds entries to `diary-file'. So
  3339. if you want to continue doing this, you need to change this to a different
  3340. key."
  3341. :group 'org-agenda
  3342. :type 'sexp)
  3343. (defcustom org-agenda-diary-file 'diary-file
  3344. "File to which to add new entries with the `i' key in agenda and calendar.
  3345. When this is the symbol `diary-file', the functionality in the Emacs
  3346. calendar will be used to add entries to the `diary-file'. But when this
  3347. points to a file, `org-agenda-diary-entry' will be used instead."
  3348. :group 'org-agenda
  3349. :type '(choice
  3350. (const :tag "The standard Emacs diary file" diary-file)
  3351. (file :tag "Special Org file diary entries")))
  3352. (eval-after-load "calendar"
  3353. '(progn
  3354. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  3355. 'org-calendar-goto-agenda)
  3356. (add-hook 'calendar-mode-hook
  3357. (lambda ()
  3358. (unless (eq org-agenda-diary-file 'diary-file)
  3359. (define-key calendar-mode-map
  3360. org-calendar-insert-diary-entry-key
  3361. 'org-agenda-diary-entry))))))
  3362. (defgroup org-latex nil
  3363. "Options for embedding LaTeX code into Org-mode."
  3364. :tag "Org LaTeX"
  3365. :group 'org)
  3366. (defcustom org-format-latex-options
  3367. '(:foreground default :background default :scale 1.0
  3368. :html-foreground "Black" :html-background "Transparent"
  3369. :html-scale 1.0 :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  3370. "Options for creating images from LaTeX fragments.
  3371. This is a property list with the following properties:
  3372. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  3373. `default' means use the foreground of the default face.
  3374. `auto' means use the foreground from the text face.
  3375. :background the background color, or \"Transparent\".
  3376. `default' means use the background of the default face.
  3377. `auto' means use the background from the text face.
  3378. :scale a scaling factor for the size of the images, to get more pixels
  3379. :html-foreground, :html-background, :html-scale
  3380. the same numbers for HTML export.
  3381. :matchers a list indicating which matchers should be used to
  3382. find LaTeX fragments. Valid members of this list are:
  3383. \"begin\" find environments
  3384. \"$1\" find single characters surrounded by $.$
  3385. \"$\" find math expressions surrounded by $...$
  3386. \"$$\" find math expressions surrounded by $$....$$
  3387. \"\\(\" find math expressions surrounded by \\(...\\)
  3388. \"\\=\\[\" find math expressions surrounded by \\=\\[...\\]"
  3389. :group 'org-latex
  3390. :type 'plist)
  3391. (defcustom org-format-latex-signal-error t
  3392. "Non-nil means signal an error when image creation of LaTeX snippets fails.
  3393. When nil, just push out a message."
  3394. :group 'org-latex
  3395. :version "24.1"
  3396. :type 'boolean)
  3397. (defcustom org-latex-to-mathml-jar-file nil
  3398. "Value of\"%j\" in `org-latex-to-mathml-convert-command'.
  3399. Use this to specify additional executable file say a jar file.
  3400. When using MathToWeb as the converter, specify the full-path to
  3401. your mathtoweb.jar file."
  3402. :group 'org-latex
  3403. :version "24.1"
  3404. :type '(choice
  3405. (const :tag "None" nil)
  3406. (file :tag "JAR file" :must-match t)))
  3407. (defcustom org-latex-to-mathml-convert-command nil
  3408. "Command to convert LaTeX fragments to MathML.
  3409. Replace format-specifiers in the command as noted below and use
  3410. `shell-command' to convert LaTeX to MathML.
  3411. %j: Executable file in fully expanded form as specified by
  3412. `org-latex-to-mathml-jar-file'.
  3413. %I: Input LaTeX file in fully expanded form.
  3414. %i: The latex fragment to be converted.
  3415. %o: Output MathML file.
  3416. This command is used by `org-create-math-formula'.
  3417. When using MathToWeb as the converter, set this option to
  3418. \"java -jar %j -unicode -force -df %o %I\".
  3419. When using LaTeXML set this option to
  3420. \"latexmlmath \"%i\" --presentationmathml=%o\"."
  3421. :group 'org-latex
  3422. :version "24.1"
  3423. :type '(choice
  3424. (const :tag "None" nil)
  3425. (string :tag "\nShell command")))
  3426. (defcustom org-latex-create-formula-image-program 'dvipng
  3427. "Program to convert LaTeX fragments with.
  3428. dvipng Process the LaTeX fragments to dvi file, then convert
  3429. dvi files to png files using dvipng.
  3430. This will also include processing of non-math environments.
  3431. imagemagick Convert the LaTeX fragments to pdf files and use imagemagick
  3432. to convert pdf files to png files"
  3433. :group 'org-latex
  3434. :version "24.1"
  3435. :type '(choice
  3436. (const :tag "dvipng" dvipng)
  3437. (const :tag "imagemagick" imagemagick)))
  3438. (defcustom org-latex-preview-ltxpng-directory "ltxpng/"
  3439. "Path to store latex preview images.
  3440. A relative path here creates many directories relative to the
  3441. processed org files paths. An absolute path puts all preview
  3442. images at the same place."
  3443. :group 'org-latex
  3444. :version "24.3"
  3445. :type 'string)
  3446. (defun org-format-latex-mathml-available-p ()
  3447. "Return t if `org-latex-to-mathml-convert-command' is usable."
  3448. (save-match-data
  3449. (when (and (boundp 'org-latex-to-mathml-convert-command)
  3450. org-latex-to-mathml-convert-command)
  3451. (let ((executable (car (split-string
  3452. org-latex-to-mathml-convert-command))))
  3453. (when (executable-find executable)
  3454. (if (string-match
  3455. "%j" org-latex-to-mathml-convert-command)
  3456. (file-readable-p org-latex-to-mathml-jar-file)
  3457. t))))))
  3458. (defcustom org-format-latex-header "\\documentclass{article}
  3459. \\usepackage[usenames]{color}
  3460. \[PACKAGES]
  3461. \[DEFAULT-PACKAGES]
  3462. \\pagestyle{empty} % do not remove
  3463. % The settings below are copied from fullpage.sty
  3464. \\setlength{\\textwidth}{\\paperwidth}
  3465. \\addtolength{\\textwidth}{-3cm}
  3466. \\setlength{\\oddsidemargin}{1.5cm}
  3467. \\addtolength{\\oddsidemargin}{-2.54cm}
  3468. \\setlength{\\evensidemargin}{\\oddsidemargin}
  3469. \\setlength{\\textheight}{\\paperheight}
  3470. \\addtolength{\\textheight}{-\\headheight}
  3471. \\addtolength{\\textheight}{-\\headsep}
  3472. \\addtolength{\\textheight}{-\\footskip}
  3473. \\addtolength{\\textheight}{-3cm}
  3474. \\setlength{\\topmargin}{1.5cm}
  3475. \\addtolength{\\topmargin}{-2.54cm}"
  3476. "The document header used for processing LaTeX fragments.
  3477. It is imperative that this header make sure that no page number
  3478. appears on the page. The package defined in the variables
  3479. `org-latex-default-packages-alist' and `org-latex-packages-alist'
  3480. will either replace the placeholder \"[PACKAGES]\" in this
  3481. header, or they will be appended."
  3482. :group 'org-latex
  3483. :type 'string)
  3484. (defun org-set-packages-alist (var val)
  3485. "Set the packages alist and make sure it has 3 elements per entry."
  3486. (set var (mapcar (lambda (x)
  3487. (if (and (consp x) (= (length x) 2))
  3488. (list (car x) (nth 1 x) t)
  3489. x))
  3490. val)))
  3491. (defun org-get-packages-alist (var)
  3492. "Get the packages alist and make sure it has 3 elements per entry."
  3493. (mapcar (lambda (x)
  3494. (if (and (consp x) (= (length x) 2))
  3495. (list (car x) (nth 1 x) t)
  3496. x))
  3497. (default-value var)))
  3498. (defcustom org-latex-default-packages-alist
  3499. '(("AUTO" "inputenc" t ("pdflatex"))
  3500. ("T1" "fontenc" t ("pdflatex"))
  3501. ("" "graphicx" t)
  3502. ("" "grffile" t)
  3503. ("" "longtable" nil)
  3504. ("" "wrapfig" nil)
  3505. ("" "rotating" nil)
  3506. ("normalem" "ulem" t)
  3507. ("" "amsmath" t)
  3508. ("" "textcomp" t)
  3509. ("" "amssymb" t)
  3510. ("" "capt-of" nil)
  3511. ("" "hyperref" nil))
  3512. "Alist of default packages to be inserted in the header.
  3513. Change this only if one of the packages here causes an
  3514. incompatibility with another package you are using.
  3515. The packages in this list are needed by one part or another of
  3516. Org mode to function properly:
  3517. - inputenc, fontenc: for basic font and character selection
  3518. - graphicx: for including images
  3519. - grffile: allow periods and spaces in graphics file names
  3520. - longtable: For multipage tables
  3521. - wrapfig: for figure placement
  3522. - rotating: for sideways figures and tables
  3523. - ulem: for underline and strike-through
  3524. - amsmath: for subscript and superscript and math environments
  3525. - textcomp, amssymb: for various symbols used
  3526. for interpreting the entities in `org-entities'. You can skip
  3527. some of these packages if you don't use any of their symbols.
  3528. - capt-of: for captions outside of floats
  3529. - hyperref: for cross references
  3530. Therefore you should not modify this variable unless you know
  3531. what you are doing. The one reason to change it anyway is that
  3532. you might be loading some other package that conflicts with one
  3533. of the default packages. Each element is either a cell or
  3534. a string.
  3535. A cell is of the format
  3536. (\"options\" \"package\" SNIPPET-FLAG COMPILERS)
  3537. If SNIPPET-FLAG is non-nil, the package also needs to be included
  3538. when compiling LaTeX snippets into images for inclusion into
  3539. non-LaTeX output. COMPILERS is a list of compilers that should
  3540. include the package, see `org-latex-compiler'. If the document
  3541. compiler is not in the list, and the list is non-nil, the package
  3542. will not be inserted in the final document.
  3543. A string will be inserted as-is in the header of the document."
  3544. :group 'org-latex
  3545. :group 'org-export-latex
  3546. :set 'org-set-packages-alist
  3547. :get 'org-get-packages-alist
  3548. :version "25.1"
  3549. :package-version '(Org . "8.3")
  3550. :type '(repeat
  3551. (choice
  3552. (list :tag "options/package pair"
  3553. (string :tag "options")
  3554. (string :tag "package")
  3555. (boolean :tag "Snippet"))
  3556. (string :tag "A line of LaTeX"))))
  3557. (defcustom org-latex-packages-alist nil
  3558. "Alist of packages to be inserted in every LaTeX header.
  3559. These will be inserted after `org-latex-default-packages-alist'.
  3560. Each element is either a cell or a string.
  3561. A cell is of the format:
  3562. (\"options\" \"package\" SNIPPET-FLAG)
  3563. SNIPPET-FLAG, when non-nil, indicates that this package is also
  3564. needed when turning LaTeX snippets into images for inclusion into
  3565. non-LaTeX output.
  3566. A string will be inserted as-is in the header of the document.
  3567. Make sure that you only list packages here which:
  3568. - you want in every file;
  3569. - do not conflict with the setup in `org-format-latex-header';
  3570. - do not conflict with the default packages in
  3571. `org-latex-default-packages-alist'."
  3572. :group 'org-latex
  3573. :group 'org-export-latex
  3574. :set 'org-set-packages-alist
  3575. :get 'org-get-packages-alist
  3576. :type '(repeat
  3577. (choice
  3578. (list :tag "options/package pair"
  3579. (string :tag "options")
  3580. (string :tag "package")
  3581. (boolean :tag "Snippet"))
  3582. (string :tag "A line of LaTeX"))))
  3583. (defgroup org-appearance nil
  3584. "Settings for Org-mode appearance."
  3585. :tag "Org Appearance"
  3586. :group 'org)
  3587. (defcustom org-level-color-stars-only nil
  3588. "Non-nil means fontify only the stars in each headline.
  3589. When nil, the entire headline is fontified.
  3590. Changing it requires restart of `font-lock-mode' to become effective
  3591. also in regions already fontified."
  3592. :group 'org-appearance
  3593. :type 'boolean)
  3594. (defcustom org-hide-leading-stars nil
  3595. "Non-nil means hide the first N-1 stars in a headline.
  3596. This works by using the face `org-hide' for these stars. This
  3597. face is white for a light background, and black for a dark
  3598. background. You may have to customize the face `org-hide' to
  3599. make this work.
  3600. Changing it requires restart of `font-lock-mode' to become effective
  3601. also in regions already fontified.
  3602. You may also set this on a per-file basis by adding one of the following
  3603. lines to the buffer:
  3604. #+STARTUP: hidestars
  3605. #+STARTUP: showstars"
  3606. :group 'org-appearance
  3607. :type 'boolean)
  3608. (defcustom org-hidden-keywords nil
  3609. "List of symbols corresponding to keywords to be hidden the org buffer.
  3610. For example, a value \\='(title) for this list will make the document's title
  3611. appear in the buffer without the initial #+TITLE: keyword."
  3612. :group 'org-appearance
  3613. :version "24.1"
  3614. :type '(set (const :tag "#+AUTHOR" author)
  3615. (const :tag "#+DATE" date)
  3616. (const :tag "#+EMAIL" email)
  3617. (const :tag "#+TITLE" title)))
  3618. (defcustom org-custom-properties nil
  3619. "List of properties (as strings) with a special meaning.
  3620. The default use of these custom properties is to let the user
  3621. hide them with `org-toggle-custom-properties-visibility'."
  3622. :group 'org-properties
  3623. :group 'org-appearance
  3624. :version "24.3"
  3625. :type '(repeat (string :tag "Property Name")))
  3626. (defcustom org-fontify-done-headline nil
  3627. "Non-nil means change the face of a headline if it is marked DONE.
  3628. Normally, only the TODO/DONE keyword indicates the state of a headline.
  3629. When this is non-nil, the headline after the keyword is set to the
  3630. `org-headline-done' as an additional indication."
  3631. :group 'org-appearance
  3632. :type 'boolean)
  3633. (defcustom org-fontify-emphasized-text t
  3634. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  3635. Changing this variable requires a restart of Emacs to take effect."
  3636. :group 'org-appearance
  3637. :type 'boolean)
  3638. (defcustom org-fontify-whole-heading-line nil
  3639. "Non-nil means fontify the whole line for headings.
  3640. This is useful when setting a background color for the
  3641. org-level-* faces."
  3642. :group 'org-appearance
  3643. :type 'boolean)
  3644. (defcustom org-highlight-latex-and-related nil
  3645. "Non-nil means highlight LaTeX related syntax in the buffer.
  3646. When non nil, the value should be a list containing any of the
  3647. following symbols:
  3648. `latex' Highlight LaTeX snippets and environments.
  3649. `script' Highlight subscript and superscript.
  3650. `entities' Highlight entities."
  3651. :group 'org-appearance
  3652. :version "24.4"
  3653. :package-version '(Org . "8.0")
  3654. :type '(choice
  3655. (const :tag "No highlighting" nil)
  3656. (set :greedy t :tag "Highlight"
  3657. (const :tag "LaTeX snippets and environments" latex)
  3658. (const :tag "Subscript and superscript" script)
  3659. (const :tag "Entities" entities))))
  3660. (defcustom org-hide-emphasis-markers nil
  3661. "Non-nil mean font-lock should hide the emphasis marker characters."
  3662. :group 'org-appearance
  3663. :type 'boolean)
  3664. (defcustom org-hide-macro-markers nil
  3665. "Non-nil mean font-lock should hide the brackets marking macro calls."
  3666. :group 'org-appearance
  3667. :type 'boolean)
  3668. (defcustom org-pretty-entities nil
  3669. "Non-nil means show entities as UTF8 characters.
  3670. When nil, the \\name form remains in the buffer."
  3671. :group 'org-appearance
  3672. :version "24.1"
  3673. :type 'boolean)
  3674. (defcustom org-pretty-entities-include-sub-superscripts t
  3675. "Non-nil means, pretty entity display includes formatting sub/superscripts."
  3676. :group 'org-appearance
  3677. :version "24.1"
  3678. :type 'boolean)
  3679. (defvar org-emph-re nil
  3680. "Regular expression for matching emphasis.
  3681. After a match, the match groups contain these elements:
  3682. 0 The match of the full regular expression, including the characters
  3683. before and after the proper match
  3684. 1 The character before the proper match, or empty at beginning of line
  3685. 2 The proper match, including the leading and trailing markers
  3686. 3 The leading marker like * or /, indicating the type of highlighting
  3687. 4 The text between the emphasis markers, not including the markers
  3688. 5 The character after the match, empty at the end of a line")
  3689. (defvar org-verbatim-re nil
  3690. "Regular expression for matching verbatim text.")
  3691. (defvar org-emphasis-regexp-components) ; defined just below
  3692. (defvar org-emphasis-alist) ; defined just below
  3693. (defun org-set-emph-re (var val)
  3694. "Set variable and compute the emphasis regular expression."
  3695. (set var val)
  3696. (when (and (boundp 'org-emphasis-alist)
  3697. (boundp 'org-emphasis-regexp-components)
  3698. org-emphasis-alist org-emphasis-regexp-components)
  3699. (let* ((e org-emphasis-regexp-components)
  3700. (pre (car e))
  3701. (post (nth 1 e))
  3702. (border (nth 2 e))
  3703. (body (nth 3 e))
  3704. (nl (nth 4 e))
  3705. (body1 (concat body "*?"))
  3706. (markers (mapconcat 'car org-emphasis-alist ""))
  3707. (vmarkers (mapconcat
  3708. (lambda (x) (if (eq (nth 2 x) 'verbatim) (car x) ""))
  3709. org-emphasis-alist "")))
  3710. ;; make sure special characters appear at the right position in the class
  3711. (if (string-match "\\^" markers)
  3712. (setq markers (concat (replace-match "" t t markers) "^")))
  3713. (if (string-match "-" markers)
  3714. (setq markers (concat (replace-match "" t t markers) "-")))
  3715. (if (string-match "\\^" vmarkers)
  3716. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  3717. (if (string-match "-" vmarkers)
  3718. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  3719. (if (> nl 0)
  3720. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  3721. (int-to-string nl) "\\}")))
  3722. ;; Make the regexp
  3723. (setq org-emph-re
  3724. (concat "\\([" pre "]\\|^\\)"
  3725. "\\("
  3726. "\\([" markers "]\\)"
  3727. "\\("
  3728. "[^" border "]\\|"
  3729. "[^" border "]"
  3730. body1
  3731. "[^" border "]"
  3732. "\\)"
  3733. "\\3\\)"
  3734. "\\([" post "]\\|$\\)"))
  3735. (setq org-verbatim-re
  3736. (concat "\\([" pre "]\\|^\\)"
  3737. "\\("
  3738. "\\([" vmarkers "]\\)"
  3739. "\\("
  3740. "[^" border "]\\|"
  3741. "[^" border "]"
  3742. body1
  3743. "[^" border "]"
  3744. "\\)"
  3745. "\\3\\)"
  3746. "\\([" post "]\\|$\\)")))))
  3747. ;; This used to be a defcustom (Org <8.0) but allowing the users to
  3748. ;; set this option proved cumbersome. See this message/thread:
  3749. ;; http://article.gmane.org/gmane.emacs.orgmode/68681
  3750. (defvar org-emphasis-regexp-components
  3751. '(" \t('\"{" "- \t.,:!?;'\")}\\[" " \t\r\n" "." 1)
  3752. "Components used to build the regular expression for emphasis.
  3753. This is a list with five entries. Terminology: In an emphasis string
  3754. like \" *strong word* \", we call the initial space PREMATCH, the final
  3755. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  3756. and \"trong wor\" is the body. The different components in this variable
  3757. specify what is allowed/forbidden in each part:
  3758. pre Chars allowed as prematch. Beginning of line will be allowed too.
  3759. post Chars allowed as postmatch. End of line will be allowed too.
  3760. border The chars *forbidden* as border characters.
  3761. body-regexp A regexp like \".\" to match a body character. Don't use
  3762. non-shy groups here, and don't allow newline here.
  3763. newline The maximum number of newlines allowed in an emphasis exp.
  3764. You need to reload Org or to restart Emacs after customizing this.")
  3765. (defcustom org-emphasis-alist
  3766. `(("*" bold)
  3767. ("/" italic)
  3768. ("_" underline)
  3769. ("=" org-verbatim verbatim)
  3770. ("~" org-code verbatim)
  3771. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))))
  3772. "Alist of characters and faces to emphasize text.
  3773. Text starting and ending with a special character will be emphasized,
  3774. for example *bold*, _underlined_ and /italic/. This variable sets the
  3775. marker characters and the face to be used by font-lock for highlighting
  3776. in Org-mode Emacs buffers.
  3777. You need to reload Org or to restart Emacs after customizing this."
  3778. :group 'org-appearance
  3779. :set 'org-set-emph-re
  3780. :version "24.4"
  3781. :package-version '(Org . "8.0")
  3782. :type '(repeat
  3783. (list
  3784. (string :tag "Marker character")
  3785. (choice
  3786. (face :tag "Font-lock-face")
  3787. (plist :tag "Face property list"))
  3788. (option (const verbatim)))))
  3789. (defvar org-protecting-blocks '("src" "example" "export")
  3790. "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
  3791. This is needed for font-lock setup.")
  3792. ;;; Miscellaneous options
  3793. (defgroup org-completion nil
  3794. "Completion in Org-mode."
  3795. :tag "Org Completion"
  3796. :group 'org)
  3797. (defcustom org-completion-use-ido nil
  3798. "Non-nil means use ido completion wherever possible.
  3799. Note that `ido-mode' must be active for this variable to be relevant.
  3800. If you decide to turn this variable on, you might well want to turn off
  3801. `org-outline-path-complete-in-steps'.
  3802. See also `org-completion-use-iswitchb'."
  3803. :group 'org-completion
  3804. :type 'boolean)
  3805. (defcustom org-completion-use-iswitchb nil
  3806. "Non-nil means use iswitchb completion wherever possible.
  3807. Note that `iswitchb-mode' must be active for this variable to be relevant.
  3808. If you decide to turn this variable on, you might well want to turn off
  3809. `org-outline-path-complete-in-steps'.
  3810. Note that this variable has only an effect if `org-completion-use-ido' is nil."
  3811. :group 'org-completion
  3812. :type 'boolean)
  3813. (defcustom org-completion-fallback-command 'hippie-expand
  3814. "The expansion command called by \\[pcomplete] in normal context.
  3815. Normal means, no org-mode-specific context."
  3816. :group 'org-completion
  3817. :type 'function)
  3818. ;;; Functions and variables from their packages
  3819. ;; Declared here to avoid compiler warnings
  3820. ;; XEmacs only
  3821. (defvar outline-mode-menu-heading)
  3822. (defvar outline-mode-menu-show)
  3823. (defvar outline-mode-menu-hide)
  3824. (defvar zmacs-regions) ; XEmacs regions
  3825. ;; Emacs only
  3826. (defvar mark-active)
  3827. ;; Various packages
  3828. (declare-function calc-eval "calc" (str &optional separator &rest args))
  3829. (declare-function calendar-forward-day "cal-move" (arg))
  3830. (declare-function calendar-goto-date "cal-move" (date))
  3831. (declare-function calendar-goto-today "cal-move" ())
  3832. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  3833. (declare-function calendar-iso-to-absolute "cal-iso" (date))
  3834. (declare-function cdlatex-compute-tables "ext:cdlatex" ())
  3835. (declare-function cdlatex-tab "ext:cdlatex" ())
  3836. (declare-function dired-get-filename
  3837. "dired"
  3838. (&optional localp no-error-if-not-filep))
  3839. (declare-function iswitchb-read-buffer
  3840. "iswitchb"
  3841. (prompt &optional default require-match start matches-set))
  3842. (declare-function org-agenda-change-all-lines
  3843. "org-agenda"
  3844. (newhead hdmarker &optional fixface just-this))
  3845. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  3846. "org-agenda"
  3847. (&optional end))
  3848. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  3849. (declare-function org-agenda-format-item
  3850. "org-agenda"
  3851. (extra txt &optional level category tags dotime noprefix
  3852. remove-re habitp))
  3853. (declare-function org-agenda-maybe-redo "org-agenda" ())
  3854. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  3855. (declare-function org-agenda-save-markers-for-cut-and-paste
  3856. "org-agenda"
  3857. (beg end))
  3858. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  3859. (declare-function org-agenda-skip "org-agenda" ())
  3860. (declare-function org-attach-reveal "org-attach" (&optional if-exists))
  3861. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  3862. (declare-function org-indent-mode "org-indent" (&optional arg))
  3863. (declare-function org-inlinetask-goto-beginning "org-inlinetask" ())
  3864. (declare-function org-inlinetask-goto-end "org-inlinetask" ())
  3865. (declare-function org-inlinetask-in-task-p "org-inlinetask" ())
  3866. (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
  3867. (declare-function orgtbl-send-table "org-table" (&optional maybe))
  3868. (declare-function parse-time-string "parse-time" (string))
  3869. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  3870. (declare-function table--at-cell-p
  3871. "table"
  3872. (position &optional object at-column))
  3873. (defvar align-mode-rules-list)
  3874. (defvar calc-embedded-close-formula)
  3875. (defvar calc-embedded-open-formula)
  3876. (defvar calc-embedded-open-mode)
  3877. (defvar font-lock-unfontify-region-function)
  3878. (defvar iswitchb-temp-buflist)
  3879. (defvar org-agenda-tags-todo-honor-ignore-options)
  3880. (defvar remember-data-file)
  3881. (defvar texmathp-why)
  3882. ;;;###autoload
  3883. (defun turn-on-orgtbl ()
  3884. "Unconditionally turn on `orgtbl-mode'."
  3885. (require 'org-table)
  3886. (orgtbl-mode 1))
  3887. (defun org-at-table-p (&optional table-type)
  3888. "Non-nil if the cursor is inside an Org table.
  3889. If TABLE-TYPE is non-nil, also check for table.el-type tables.
  3890. If `org-enable-table-editor' is nil, return nil unconditionally."
  3891. (and
  3892. org-enable-table-editor
  3893. (save-excursion
  3894. (beginning-of-line)
  3895. (org-looking-at-p (if table-type "[ \t]*[|+]" "[ \t]*|")))
  3896. (or (not (derived-mode-p 'org-mode))
  3897. (let ((e (org-element-lineage (org-element-at-point) '(table) t)))
  3898. (and e (or table-type (eq (org-element-property :type e) 'org)))))))
  3899. (define-obsolete-function-alias 'org-table-p 'org-at-table-p "Org 9.0")
  3900. (defun org-at-table.el-p ()
  3901. "Non-nil when point is at a table.el table."
  3902. (and (save-excursion (beginning-of-line) (looking-at "[ \t]*[|+]"))
  3903. (let ((element (org-element-at-point)))
  3904. (and (eq (org-element-type element) 'table)
  3905. (eq (org-element-property :type element) 'table.el)))))
  3906. (defun org-table-recognize-table.el ()
  3907. "If there is a table.el table nearby, recognize it and move into it."
  3908. (when (and org-table-tab-recognizes-table.el (org-at-table.el-p))
  3909. (beginning-of-line)
  3910. (unless (or (looking-at org-table-dataline-regexp)
  3911. (not (looking-at org-table1-hline-regexp)))
  3912. (forward-line)
  3913. (when (looking-at org-table-any-border-regexp)
  3914. (forward-line -2)))
  3915. (if (re-search-forward "|" (org-table-end t) t)
  3916. (progn
  3917. (require 'table)
  3918. (if (table--at-cell-p (point)) t
  3919. (message "recognizing table.el table...")
  3920. (table-recognize-table)
  3921. (message "recognizing table.el table...done")))
  3922. (error "This should not happen"))))
  3923. ;;; This function is not used by org core since commit 6d1e3082, Feb 2010
  3924. (make-obsolete 'org-table-recognize-table.el
  3925. "please notify the org mailing list if you use this function."
  3926. "Org 9.0")
  3927. (defun org-at-table-hline-p ()
  3928. "Non-nil when point is inside a hline in a table.
  3929. Assume point is already in a table. If `org-enable-table-editor'
  3930. is nil, return nil unconditionally."
  3931. (and org-enable-table-editor
  3932. (save-excursion
  3933. (beginning-of-line)
  3934. (looking-at org-table-hline-regexp))))
  3935. (defun org-table-map-tables (function &optional quietly)
  3936. "Apply FUNCTION to the start of all tables in the buffer."
  3937. (save-excursion
  3938. (save-restriction
  3939. (widen)
  3940. (goto-char (point-min))
  3941. (while (re-search-forward org-table-any-line-regexp nil t)
  3942. (unless quietly
  3943. (message "Mapping tables: %d%%"
  3944. (floor (* 100.0 (point)) (buffer-size))))
  3945. (beginning-of-line 1)
  3946. (when (and (looking-at org-table-line-regexp)
  3947. ;; Exclude tables in src/example/verbatim/clocktable blocks
  3948. (not (org-in-block-p '("src" "example" "verbatim" "clocktable"))))
  3949. (save-excursion (funcall function))
  3950. (or (looking-at org-table-line-regexp)
  3951. (forward-char 1)))
  3952. (re-search-forward org-table-any-border-regexp nil 1))))
  3953. (unless quietly (message "Mapping tables: done")))
  3954. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock" (beg end))
  3955. (declare-function org-clock-update-mode-line "org-clock" ())
  3956. (declare-function org-resolve-clocks "org-clock"
  3957. (&optional also-non-dangling-p prompt last-valid))
  3958. (defun org-at-TBLFM-p (&optional pos)
  3959. "Non-nil when point (or POS) is in #+TBLFM line."
  3960. (save-excursion
  3961. (goto-char (or pos (point)))
  3962. (beginning-of-line)
  3963. (and (let ((case-fold-search t)) (looking-at org-TBLFM-regexp))
  3964. (eq (org-element-type (org-element-at-point)) 'table))))
  3965. (defvar org-clock-start-time)
  3966. (defvar org-clock-marker (make-marker)
  3967. "Marker recording the last clock-in.")
  3968. (defvar org-clock-hd-marker (make-marker)
  3969. "Marker recording the last clock-in, but the headline position.")
  3970. (defvar org-clock-heading ""
  3971. "The heading of the current clock entry.")
  3972. (defun org-clock-is-active ()
  3973. "Return the buffer where the clock is currently running.
  3974. Return nil if no clock is running."
  3975. (marker-buffer org-clock-marker))
  3976. (defun org-check-running-clock ()
  3977. "Check if the current buffer contains the running clock.
  3978. If yes, offer to stop it and to save the buffer with the changes."
  3979. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3980. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  3981. (buffer-name))))
  3982. (org-clock-out)
  3983. (when (y-or-n-p "Save changed buffer?")
  3984. (save-buffer))))
  3985. (defun org-clocktable-try-shift (dir n)
  3986. "Check if this line starts a clock table, if yes, shift the time block."
  3987. (when (org-match-line "^[ \t]*#\\+BEGIN:[ \t]+clocktable\\>")
  3988. (org-clocktable-shift dir n)))
  3989. ;;;###autoload
  3990. (defun org-clock-persistence-insinuate ()
  3991. "Set up hooks for clock persistence."
  3992. (require 'org-clock)
  3993. (add-hook 'org-mode-hook 'org-clock-load)
  3994. (add-hook 'kill-emacs-hook 'org-clock-save))
  3995. (defgroup org-archive nil
  3996. "Options concerning archiving in Org-mode."
  3997. :tag "Org Archive"
  3998. :group 'org-structure)
  3999. (defcustom org-archive-location "%s_archive::"
  4000. "The location where subtrees should be archived.
  4001. The value of this variable is a string, consisting of two parts,
  4002. separated by a double-colon. The first part is a filename and
  4003. the second part is a headline.
  4004. When the filename is omitted, archiving happens in the same file.
  4005. %s in the filename will be replaced by the current file
  4006. name (without the directory part). Archiving to a different file
  4007. is useful to keep archived entries from contributing to the
  4008. Org-mode Agenda.
  4009. The archived entries will be filed as subtrees of the specified
  4010. headline. When the headline is omitted, the subtrees are simply
  4011. filed away at the end of the file, as top-level entries. Also in
  4012. the heading you can use %s to represent the file name, this can be
  4013. useful when using the same archive for a number of different files.
  4014. Here are a few examples:
  4015. \"%s_archive::\"
  4016. If the current file is Projects.org, archive in file
  4017. Projects.org_archive, as top-level trees. This is the default.
  4018. \"::* Archived Tasks\"
  4019. Archive in the current file, under the top-level headline
  4020. \"* Archived Tasks\".
  4021. \"~/org/archive.org::\"
  4022. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  4023. \"~/org/archive.org::* From %s\"
  4024. Archive in file ~/org/archive.org (absolute path), under headlines
  4025. \"From FILENAME\" where file name is the current file name.
  4026. \"~/org/datetree.org::datetree/* Finished Tasks\"
  4027. The \"datetree/\" string is special, signifying to archive
  4028. items to the datetree. Items are placed in either the CLOSED
  4029. date of the item, or the current date if there is no CLOSED date.
  4030. The heading will be a subentry to the current date. There doesn't
  4031. need to be a heading, but there always needs to be a slash after
  4032. datetree. For example, to store archived items directly in the
  4033. datetree, use \"~/org/datetree.org::datetree/\".
  4034. \"basement::** Finished Tasks\"
  4035. Archive in file ./basement (relative path), as level 3 trees
  4036. below the level 2 heading \"** Finished Tasks\".
  4037. You may set this option on a per-file basis by adding to the buffer a
  4038. line like
  4039. #+ARCHIVE: basement::** Finished Tasks
  4040. You may also define it locally for a subtree by setting an ARCHIVE property
  4041. in the entry. If such a property is found in an entry, or anywhere up
  4042. the hierarchy, it will be used."
  4043. :group 'org-archive
  4044. :type 'string)
  4045. (defcustom org-agenda-skip-archived-trees t
  4046. "Non-nil means the agenda will skip any items located in archived trees.
  4047. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  4048. variable is no longer recommended, you should leave it at the value t.
  4049. Instead, use the key `v' to cycle the archives-mode in the agenda."
  4050. :group 'org-archive
  4051. :group 'org-agenda-skip
  4052. :type 'boolean)
  4053. (defcustom org-columns-skip-archived-trees t
  4054. "Non-nil means ignore archived trees when creating column view."
  4055. :group 'org-archive
  4056. :group 'org-properties
  4057. :type 'boolean)
  4058. (defcustom org-cycle-open-archived-trees nil
  4059. "Non-nil means `org-cycle' will open archived trees.
  4060. An archived tree is a tree marked with the tag ARCHIVE.
  4061. When nil, archived trees will stay folded. You can still open them with
  4062. normal outline commands like `show-all', but not with the cycling commands."
  4063. :group 'org-archive
  4064. :group 'org-cycle
  4065. :type 'boolean)
  4066. (defcustom org-sparse-tree-open-archived-trees nil
  4067. "Non-nil means sparse tree construction shows matches in archived trees.
  4068. When nil, matches in these trees are highlighted, but the trees are kept in
  4069. collapsed state."
  4070. :group 'org-archive
  4071. :group 'org-sparse-trees
  4072. :type 'boolean)
  4073. (defcustom org-sparse-tree-default-date-type nil
  4074. "The default date type when building a sparse tree.
  4075. When this is nil, a date is a scheduled or a deadline timestamp.
  4076. Otherwise, these types are allowed:
  4077. all: all timestamps
  4078. active: only active timestamps (<...>)
  4079. inactive: only inactive timestamps ([...])
  4080. scheduled: only scheduled timestamps
  4081. deadline: only deadline timestamps"
  4082. :type '(choice (const :tag "Scheduled or deadline" nil)
  4083. (const :tag "All timestamps" all)
  4084. (const :tag "Only active timestamps" active)
  4085. (const :tag "Only inactive timestamps" inactive)
  4086. (const :tag "Only scheduled timestamps" scheduled)
  4087. (const :tag "Only deadline timestamps" deadline)
  4088. (const :tag "Only closed timestamps" closed))
  4089. :version "25.1"
  4090. :package-version '(Org . "8.3")
  4091. :group 'org-sparse-trees)
  4092. (defun org-cycle-hide-archived-subtrees (state)
  4093. "Re-hide all archived subtrees after a visibility state change."
  4094. (when (and (not org-cycle-open-archived-trees)
  4095. (not (memq state '(overview folded))))
  4096. (save-excursion
  4097. (let* ((globalp (memq state '(contents all)))
  4098. (beg (if globalp (point-min) (point)))
  4099. (end (if globalp (point-max) (org-end-of-subtree t))))
  4100. (org-hide-archived-subtrees beg end)
  4101. (goto-char beg)
  4102. (when (looking-at-p (concat ".*:" org-archive-tag ":"))
  4103. (message "%s" (substitute-command-keys
  4104. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  4105. (defun org-force-cycle-archived ()
  4106. "Cycle subtree even if it is archived."
  4107. (interactive)
  4108. (setq this-command 'org-cycle)
  4109. (let ((org-cycle-open-archived-trees t))
  4110. (call-interactively 'org-cycle)))
  4111. (defun org-hide-archived-subtrees (beg end)
  4112. "Re-hide all archived subtrees after a visibility state change."
  4113. (org-with-wide-buffer
  4114. (let ((case-fold-search nil)
  4115. (re (concat org-outline-regexp-bol ".*:" org-archive-tag ":")))
  4116. (goto-char beg)
  4117. ;; Include headline point is currently on.
  4118. (beginning-of-line)
  4119. (while (and (< (point) end) (re-search-forward re end t))
  4120. (when (member org-archive-tag (org-get-tags))
  4121. (org-flag-subtree t)
  4122. (org-end-of-subtree t))))))
  4123. (declare-function outline-end-of-heading "outline" ())
  4124. (declare-function outline-flag-region "outline" (from to flag))
  4125. (defun org-flag-subtree (flag)
  4126. (save-excursion
  4127. (org-back-to-heading t)
  4128. (outline-end-of-heading)
  4129. (outline-flag-region (point)
  4130. (progn (org-end-of-subtree t) (point))
  4131. flag)))
  4132. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  4133. ;; Declare Column View Code
  4134. (declare-function org-columns-number-to-string "org-colview" (n fmt &optional printf))
  4135. (declare-function org-columns-get-format-and-top-level "org-colview" ())
  4136. (declare-function org-columns-compute "org-colview" (property))
  4137. ;; Declare ID code
  4138. (declare-function org-id-store-link "org-id")
  4139. (declare-function org-id-locations-load "org-id")
  4140. (declare-function org-id-locations-save "org-id")
  4141. (defvar org-id-track-globally)
  4142. ;;; Variables for pre-computed regular expressions, all buffer local
  4143. (defvar-local org-todo-regexp nil
  4144. "Matches any of the TODO state keywords.")
  4145. (defvar-local org-not-done-regexp nil
  4146. "Matches any of the TODO state keywords except the last one.")
  4147. (defvar-local org-not-done-heading-regexp nil
  4148. "Matches a TODO headline that is not done.")
  4149. (defvar-local org-todo-line-regexp nil
  4150. "Matches a headline and puts TODO state into group 2 if present.")
  4151. (defvar-local org-complex-heading-regexp nil
  4152. "Matches a headline and puts everything into groups:
  4153. group 1: the stars
  4154. group 2: The todo keyword, maybe
  4155. group 3: Priority cookie
  4156. group 4: True headline
  4157. group 5: Tags")
  4158. (defvar-local org-complex-heading-regexp-format nil
  4159. "Printf format to make regexp to match an exact headline.
  4160. This regexp will match the headline of any node which has the
  4161. exact headline text that is put into the format, but may have any
  4162. TODO state, priority and tags.")
  4163. (defvar-local org-todo-line-tags-regexp nil
  4164. "Matches a headline and puts TODO state into group 2 if present.
  4165. Also put tags into group 4 if tags are present.")
  4166. (defconst org-plain-time-of-day-regexp
  4167. (concat
  4168. "\\(\\<[012]?[0-9]"
  4169. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4170. "\\(--?"
  4171. "\\(\\<[012]?[0-9]"
  4172. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4173. "\\)?")
  4174. "Regular expression to match a plain time or time range.
  4175. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  4176. groups carry important information:
  4177. 0 the full match
  4178. 1 the first time, range or not
  4179. 8 the second time, if it is a range.")
  4180. (defconst org-plain-time-extension-regexp
  4181. (concat
  4182. "\\(\\<[012]?[0-9]"
  4183. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4184. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  4185. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  4186. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  4187. groups carry important information:
  4188. 0 the full match
  4189. 7 hours of duration
  4190. 9 minutes of duration")
  4191. (defconst org-stamp-time-of-day-regexp
  4192. (concat
  4193. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  4194. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  4195. "\\(--?"
  4196. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  4197. "Regular expression to match a timestamp time or time range.
  4198. After a match, the following groups carry important information:
  4199. 0 the full match
  4200. 1 date plus weekday, for back referencing to make sure both times are on the same day
  4201. 2 the first time, range or not
  4202. 4 the second time, if it is a range.")
  4203. (defconst org-startup-options
  4204. '(("fold" org-startup-folded t)
  4205. ("overview" org-startup-folded t)
  4206. ("nofold" org-startup-folded nil)
  4207. ("showall" org-startup-folded nil)
  4208. ("showeverything" org-startup-folded showeverything)
  4209. ("content" org-startup-folded content)
  4210. ("indent" org-startup-indented t)
  4211. ("noindent" org-startup-indented nil)
  4212. ("hidestars" org-hide-leading-stars t)
  4213. ("showstars" org-hide-leading-stars nil)
  4214. ("odd" org-odd-levels-only t)
  4215. ("oddeven" org-odd-levels-only nil)
  4216. ("align" org-startup-align-all-tables t)
  4217. ("noalign" org-startup-align-all-tables nil)
  4218. ("inlineimages" org-startup-with-inline-images t)
  4219. ("noinlineimages" org-startup-with-inline-images nil)
  4220. ("latexpreview" org-startup-with-latex-preview t)
  4221. ("nolatexpreview" org-startup-with-latex-preview nil)
  4222. ("customtime" org-display-custom-times t)
  4223. ("logdone" org-log-done time)
  4224. ("lognotedone" org-log-done note)
  4225. ("nologdone" org-log-done nil)
  4226. ("lognoteclock-out" org-log-note-clock-out t)
  4227. ("nolognoteclock-out" org-log-note-clock-out nil)
  4228. ("logrepeat" org-log-repeat state)
  4229. ("lognoterepeat" org-log-repeat note)
  4230. ("logdrawer" org-log-into-drawer t)
  4231. ("nologdrawer" org-log-into-drawer nil)
  4232. ("logstatesreversed" org-log-states-order-reversed t)
  4233. ("nologstatesreversed" org-log-states-order-reversed nil)
  4234. ("nologrepeat" org-log-repeat nil)
  4235. ("logreschedule" org-log-reschedule time)
  4236. ("lognotereschedule" org-log-reschedule note)
  4237. ("nologreschedule" org-log-reschedule nil)
  4238. ("logredeadline" org-log-redeadline time)
  4239. ("lognoteredeadline" org-log-redeadline note)
  4240. ("nologredeadline" org-log-redeadline nil)
  4241. ("logrefile" org-log-refile time)
  4242. ("lognoterefile" org-log-refile note)
  4243. ("nologrefile" org-log-refile nil)
  4244. ("fninline" org-footnote-define-inline t)
  4245. ("nofninline" org-footnote-define-inline nil)
  4246. ("fnlocal" org-footnote-section nil)
  4247. ("fnauto" org-footnote-auto-label t)
  4248. ("fnprompt" org-footnote-auto-label nil)
  4249. ("fnconfirm" org-footnote-auto-label confirm)
  4250. ("fnplain" org-footnote-auto-label plain)
  4251. ("fnadjust" org-footnote-auto-adjust t)
  4252. ("nofnadjust" org-footnote-auto-adjust nil)
  4253. ("constcgs" constants-unit-system cgs)
  4254. ("constSI" constants-unit-system SI)
  4255. ("noptag" org-tag-persistent-alist nil)
  4256. ("hideblocks" org-hide-block-startup t)
  4257. ("nohideblocks" org-hide-block-startup nil)
  4258. ("beamer" org-startup-with-beamer-mode t)
  4259. ("entitiespretty" org-pretty-entities t)
  4260. ("entitiesplain" org-pretty-entities nil))
  4261. "Variable associated with STARTUP options for org-mode.
  4262. Each element is a list of three items: the startup options (as written
  4263. in the #+STARTUP line), the corresponding variable, and the value to set
  4264. this variable to if the option is found. An optional forth element PUSH
  4265. means to push this value onto the list in the variable.")
  4266. (defcustom org-group-tags t
  4267. "When non-nil (the default), use group tags.
  4268. This can be turned on/off through `org-toggle-tags-groups'."
  4269. :group 'org-tags
  4270. :group 'org-startup
  4271. :type 'boolean)
  4272. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  4273. (defun org-toggle-tags-groups ()
  4274. "Toggle support for group tags.
  4275. Support for group tags is controlled by the option
  4276. `org-group-tags', which is non-nil by default."
  4277. (interactive)
  4278. (setq org-group-tags (not org-group-tags))
  4279. (cond ((and (derived-mode-p 'org-agenda-mode)
  4280. org-group-tags)
  4281. (org-agenda-redo))
  4282. ((derived-mode-p 'org-mode)
  4283. (let ((org-inhibit-startup t)) (org-mode))))
  4284. (message "Groups tags support has been turned %s"
  4285. (if org-group-tags "on" "off")))
  4286. (defun org-set-regexps-and-options (&optional tags-only)
  4287. "Precompute regular expressions used in the current buffer.
  4288. When optional argument TAGS-ONLY is non-nil, only compute tags
  4289. related expressions."
  4290. (when (derived-mode-p 'org-mode)
  4291. (let ((alist (org--setup-collect-keywords
  4292. (org-make-options-regexp
  4293. (append '("FILETAGS" "TAGS" "SETUPFILE")
  4294. (and (not tags-only)
  4295. '("ARCHIVE" "CATEGORY" "COLUMNS" "CONSTANTS"
  4296. "LINK" "OPTIONS" "PRIORITIES" "PROPERTY"
  4297. "SEQ_TODO" "STARTUP" "TODO" "TYP_TODO")))))))
  4298. (org--setup-process-tags
  4299. (cdr (assq 'tags alist)) (cdr (assq 'filetags alist)))
  4300. (unless tags-only
  4301. ;; File properties.
  4302. (setq-local org-file-properties (cdr (assq 'property alist)))
  4303. ;; Archive location.
  4304. (let ((archive (cdr (assq 'archive alist))))
  4305. (when archive (setq-local org-archive-location archive)))
  4306. ;; Category.
  4307. (let ((cat (org-string-nw-p (cdr (assq 'category alist)))))
  4308. (when cat
  4309. (setq-local org-category (intern cat))
  4310. (setq-local org-file-properties
  4311. (org--update-property-plist
  4312. "CATEGORY" cat org-file-properties))))
  4313. ;; Columns.
  4314. (let ((column (cdr (assq 'columns alist))))
  4315. (when column (setq-local org-columns-default-format column)))
  4316. ;; Constants.
  4317. (setq org-table-formula-constants-local (cdr (assq 'constants alist)))
  4318. ;; Link abbreviations.
  4319. (let ((links (cdr (assq 'link alist))))
  4320. (when links (setq org-link-abbrev-alist-local (nreverse links))))
  4321. ;; Priorities.
  4322. (let ((priorities (cdr (assq 'priorities alist))))
  4323. (when priorities
  4324. (setq-local org-highest-priority (nth 0 priorities))
  4325. (setq-local org-lowest-priority (nth 1 priorities))
  4326. (setq-local org-default-priority (nth 2 priorities))))
  4327. ;; Scripts.
  4328. (let ((scripts (assq 'scripts alist)))
  4329. (when scripts
  4330. (setq-local org-use-sub-superscripts (cdr scripts))))
  4331. ;; Startup options.
  4332. (let ((startup (cdr (assq 'startup alist))))
  4333. (dolist (option startup)
  4334. (let ((entry (assoc-string option org-startup-options t)))
  4335. (when entry
  4336. (let ((var (nth 1 entry))
  4337. (val (nth 2 entry)))
  4338. (if (not (nth 3 entry)) (set (make-local-variable var) val)
  4339. (unless (listp (symbol-value var))
  4340. (set (make-local-variable var) nil))
  4341. (add-to-list var val)))))))
  4342. ;; TODO keywords.
  4343. (setq-local org-todo-kwd-alist nil)
  4344. (setq-local org-todo-key-alist nil)
  4345. (setq-local org-todo-key-trigger nil)
  4346. (setq-local org-todo-keywords-1 nil)
  4347. (setq-local org-done-keywords nil)
  4348. (setq-local org-todo-heads nil)
  4349. (setq-local org-todo-sets nil)
  4350. (setq-local org-todo-log-states nil)
  4351. (let ((todo-sequences
  4352. (or (nreverse (cdr (assq 'todo alist)))
  4353. (let ((d (default-value 'org-todo-keywords)))
  4354. (if (not (stringp (car d))) d
  4355. ;; XXX: Backward compatibility code.
  4356. (list (cons org-todo-interpretation d)))))))
  4357. (dolist (sequence todo-sequences)
  4358. (let* ((sequence (or (run-hook-with-args-until-success
  4359. 'org-todo-setup-filter-hook sequence)
  4360. sequence))
  4361. (sequence-type (car sequence))
  4362. (keywords (cdr sequence))
  4363. (sep (member "|" keywords))
  4364. names alist)
  4365. (dolist (k (remove "|" keywords))
  4366. (unless (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$"
  4367. k)
  4368. (error "Invalid TODO keyword %s" k))
  4369. (let ((name (match-string 1 k))
  4370. (key (match-string 2 k))
  4371. (log (org-extract-log-state-settings k)))
  4372. (push name names)
  4373. (push (cons name (and key (string-to-char key))) alist)
  4374. (when log (push log org-todo-log-states))))
  4375. (let* ((names (nreverse names))
  4376. (done (if sep (org-remove-keyword-keys (cdr sep))
  4377. (last names)))
  4378. (head (car names))
  4379. (tail (list sequence-type head (car done) (org-last done))))
  4380. (add-to-list 'org-todo-heads head 'append)
  4381. (push names org-todo-sets)
  4382. (setq org-done-keywords (append org-done-keywords done nil))
  4383. (setq org-todo-keywords-1 (append org-todo-keywords-1 names nil))
  4384. (setq org-todo-key-alist
  4385. (append org-todo-key-alist
  4386. (and alist
  4387. (append '((:startgroup))
  4388. (nreverse alist)
  4389. '((:endgroup))))))
  4390. (dolist (k names) (push (cons k tail) org-todo-kwd-alist))))))
  4391. (setq org-todo-sets (nreverse org-todo-sets)
  4392. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  4393. org-todo-key-trigger (delq nil (mapcar #'cdr org-todo-key-alist))
  4394. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist))
  4395. ;; Compute the regular expressions and other local variables.
  4396. ;; Using `org-outline-regexp-bol' would complicate them much,
  4397. ;; because of the fixed white space at the end of that string.
  4398. (unless org-done-keywords
  4399. (setq org-done-keywords
  4400. (and org-todo-keywords-1 (last org-todo-keywords-1))))
  4401. (setq org-not-done-keywords
  4402. (org-delete-all org-done-keywords
  4403. (copy-sequence org-todo-keywords-1))
  4404. org-todo-regexp (regexp-opt org-todo-keywords-1 t)
  4405. org-not-done-regexp (regexp-opt org-not-done-keywords t)
  4406. org-not-done-heading-regexp
  4407. (format org-heading-keyword-regexp-format org-not-done-regexp)
  4408. org-todo-line-regexp
  4409. (format org-heading-keyword-maybe-regexp-format org-todo-regexp)
  4410. org-complex-heading-regexp
  4411. (concat "^\\(\\*+\\)"
  4412. "\\(?: +" org-todo-regexp "\\)?"
  4413. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4414. "\\(?: +\\(.*?\\)\\)??"
  4415. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?")
  4416. "[ \t]*$")
  4417. org-complex-heading-regexp-format
  4418. (concat "^\\(\\*+\\)"
  4419. "\\(?: +" org-todo-regexp "\\)?"
  4420. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4421. "\\(?: +"
  4422. ;; Stats cookies can be stuck to body.
  4423. "\\(?:\\[[0-9%%/]+\\] *\\)*"
  4424. "\\(%s\\)"
  4425. "\\(?: *\\[[0-9%%/]+\\]\\)*"
  4426. "\\)"
  4427. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%%:]+:\\)\\)?")
  4428. "[ \t]*$")
  4429. org-todo-line-tags-regexp
  4430. (concat "^\\(\\*+\\)"
  4431. "\\(?: +" org-todo-regexp "\\)?"
  4432. "\\(?: +\\(.*?\\)\\)??"
  4433. (org-re "\\(?:[ \t]+\\(:[[:alnum:]:_@#%]+:\\)\\)?")
  4434. "[ \t]*$"))
  4435. (org-compute-latex-and-related-regexp)))))
  4436. (defun org--setup-collect-keywords (regexp &optional files alist)
  4437. "Return setup keywords values as an alist.
  4438. REGEXP matches a subset of setup keywords. FILES is a list of
  4439. file names already visited. It is used to avoid circular setup
  4440. files. ALIST, when non-nil, is the alist computed so far.
  4441. Return value contains the following keys: `archive', `category',
  4442. `columns', `constants', `filetags', `link', `priorities',
  4443. `property', `scripts', `startup', `tags' and `todo'."
  4444. (org-with-wide-buffer
  4445. (goto-char (point-min))
  4446. (let ((case-fold-search t))
  4447. (while (re-search-forward regexp nil t)
  4448. (let ((element (org-element-at-point)))
  4449. (when (eq (org-element-type element) 'keyword)
  4450. (let ((key (org-element-property :key element))
  4451. (value (org-element-property :value element)))
  4452. (cond
  4453. ((equal key "ARCHIVE")
  4454. (when (org-string-nw-p value)
  4455. (push (cons 'archive value) alist)))
  4456. ((equal key "CATEGORY") (push (cons 'category value) alist))
  4457. ((equal key "COLUMNS") (push (cons 'columns value) alist))
  4458. ((equal key "CONSTANTS")
  4459. (let* ((constants (assq 'constants alist))
  4460. (store (cdr constants)))
  4461. (dolist (pair (org-split-string value))
  4462. (when (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)"
  4463. pair)
  4464. (let* ((name (match-string 1 pair))
  4465. (value (match-string 2 pair))
  4466. (old (assoc name store)))
  4467. (if old (setcdr old value)
  4468. (push (cons name value) store)))))
  4469. (if constants (setcdr constants store)
  4470. (push (cons 'constants store) alist))))
  4471. ((equal key "FILETAGS")
  4472. (when (org-string-nw-p value)
  4473. (let ((old (assq 'filetags alist))
  4474. (new (apply #'nconc
  4475. (mapcar (lambda (x) (org-split-string x ":"))
  4476. (org-split-string value)))))
  4477. (if old (setcdr old (append new (cdr old)))
  4478. (push (cons 'filetags new) alist)))))
  4479. ((equal key "LINK")
  4480. (when (string-match "\\`\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  4481. (let ((links (assq 'link alist))
  4482. (pair (cons (org-match-string-no-properties 1 value)
  4483. (org-match-string-no-properties 2 value))))
  4484. (if links (push pair (cdr links))
  4485. (push (list 'link pair) alist)))))
  4486. ((equal key "OPTIONS")
  4487. (when (and (org-string-nw-p value)
  4488. (string-match "\\^:\\(t\\|nil\\|{}\\)" value))
  4489. (push (cons 'scripts (read (match-string 1 value))) alist)))
  4490. ((equal key "PRIORITIES")
  4491. (push (cons 'priorities
  4492. (let ((prio (org-split-string value)))
  4493. (if (< (length prio) 3) '(?A ?C ?B)
  4494. (mapcar #'string-to-char prio))))
  4495. alist))
  4496. ((equal key "PROPERTY")
  4497. (when (string-match "\\(\\S-+\\)[ \t]+\\(.*\\)" value)
  4498. (let* ((property (assq 'property alist))
  4499. (value (org--update-property-plist
  4500. (org-match-string-no-properties 1 value)
  4501. (org-match-string-no-properties 2 value)
  4502. (cdr property))))
  4503. (if property (setcdr property value)
  4504. (push (cons 'property value) alist)))))
  4505. ((equal key "STARTUP")
  4506. (let ((startup (assq 'startup alist)))
  4507. (if startup
  4508. (setcdr startup
  4509. (append (cdr startup) (org-split-string value)))
  4510. (push (cons 'startup (org-split-string value)) alist))))
  4511. ((equal key "TAGS")
  4512. (let ((tag-cell (assq 'tags alist)))
  4513. (if tag-cell
  4514. (setcdr tag-cell
  4515. (append (cdr tag-cell)
  4516. '("\\n")
  4517. (org-split-string value)))
  4518. (push (cons 'tags (org-split-string value)) alist))))
  4519. ((member key '("TODO" "SEQ_TODO" "TYP_TODO"))
  4520. (let ((todo (assq 'todo alist))
  4521. (value (cons (if (equal key "TYP_TODO") 'type 'sequence)
  4522. (org-split-string value))))
  4523. (if todo (push value (cdr todo))
  4524. (push (list 'todo value) alist))))
  4525. ((equal key "SETUPFILE")
  4526. (unless buffer-read-only ; Do not check in Gnus messages.
  4527. (let ((f (and (org-string-nw-p value)
  4528. (expand-file-name
  4529. (org-remove-double-quotes value)))))
  4530. (when (and f (file-readable-p f) (not (member f files)))
  4531. (with-temp-buffer
  4532. (setq default-directory (file-name-directory f))
  4533. (insert-file-contents f)
  4534. (setq alist
  4535. ;; Fake Org mode to benefit from cache
  4536. ;; without recurring needlessly.
  4537. (let ((major-mode 'org-mode))
  4538. (org--setup-collect-keywords
  4539. regexp (cons f files) alist)))))))))))))))
  4540. alist)
  4541. (defun org--setup-process-tags (tags filetags)
  4542. "Precompute variables used for tags.
  4543. TAGS is a list of tags and tag group symbols, as strings.
  4544. FILETAGS is a list of tags, as strings."
  4545. ;; Process the file tags.
  4546. (setq-local org-file-tags
  4547. (mapcar #'org-add-prop-inherited filetags))
  4548. ;; Provide default tags if no local tags are found.
  4549. (when (and (not tags) org-tag-alist)
  4550. (setq tags
  4551. (mapcar (lambda (tag)
  4552. (cl-case (car tag)
  4553. (:startgroup "{")
  4554. (:endgroup "}")
  4555. (:startgrouptag "[")
  4556. (:endgrouptag "]")
  4557. (:grouptags ":")
  4558. (:newline "\\n")
  4559. (otherwise (concat (car tag)
  4560. (and (characterp (cdr tag))
  4561. (format "(%c)" (cdr tag)))))))
  4562. org-tag-alist)))
  4563. ;; Process the tags.
  4564. (setq-local org-tag-groups-alist nil)
  4565. (setq-local org-tag-alist nil)
  4566. (let (group-flag)
  4567. (while tags
  4568. (let ((e (car tags)))
  4569. (setq tags (cdr tags))
  4570. (cond
  4571. ((equal e "{")
  4572. (push '(:startgroup) org-tag-alist)
  4573. (when (equal (nth 1 tags) ":") (setq group-flag t)))
  4574. ((equal e "}")
  4575. (push '(:endgroup) org-tag-alist)
  4576. (setq group-flag nil))
  4577. ((equal e "[")
  4578. (push '(:startgrouptag) org-tag-alist)
  4579. (when (equal (nth 1 tags) ":") (setq group-flag t)))
  4580. ((equal e "]")
  4581. (push '(:endgrouptag) org-tag-alist)
  4582. (setq group-flag nil))
  4583. ((equal e ":")
  4584. (push '(:grouptags) org-tag-alist)
  4585. (setq group-flag 'append))
  4586. ((equal e "\\n") (push '(:newline) org-tag-alist))
  4587. ((string-match
  4588. (org-re (concat "\\`\\([[:alnum:]_@#%]+"
  4589. "\\|{.+?}\\)" ; regular expression
  4590. "\\(?:(\\(.\\))\\)?\\'")) e)
  4591. (let ((tag (match-string 1 e))
  4592. (key (and (match-beginning 2)
  4593. (string-to-char (match-string 2 e)))))
  4594. (cond ((eq group-flag 'append)
  4595. (setcar org-tag-groups-alist
  4596. (append (car org-tag-groups-alist) (list tag))))
  4597. (group-flag (push (list tag) org-tag-groups-alist)))
  4598. ;; Push all tags in groups, no matter if they already exist.
  4599. (unless (and (not group-flag) (assoc tag org-tag-alist))
  4600. (push (cons tag key) org-tag-alist))))))))
  4601. (setq org-tag-alist (nreverse org-tag-alist)))
  4602. (defun org-file-contents (file &optional noerror)
  4603. "Return the contents of FILE, as a string."
  4604. (if (and file (file-readable-p file))
  4605. (with-temp-buffer
  4606. (insert-file-contents file)
  4607. (buffer-string))
  4608. (funcall (if noerror 'message 'error)
  4609. "Cannot read file \"%s\"%s"
  4610. file
  4611. (let ((from (buffer-file-name (buffer-base-buffer))))
  4612. (if from (concat " (referenced in file \"" from "\")") "")))))
  4613. (defun org-extract-log-state-settings (x)
  4614. "Extract the log state setting from a TODO keyword string.
  4615. This will extract info from a string like \"WAIT(w@/!)\"."
  4616. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  4617. (let ((kw (match-string 1 x))
  4618. (log1 (and (match-end 3) (match-string 3 x)))
  4619. (log2 (and (match-end 4) (match-string 4 x))))
  4620. (and (or log1 log2)
  4621. (list kw
  4622. (and log1 (if (equal log1 "!") 'time 'note))
  4623. (and log2 (if (equal log2 "!") 'time 'note)))))))
  4624. (defun org-remove-keyword-keys (list)
  4625. "Remove a pair of parenthesis at the end of each string in LIST."
  4626. (mapcar (lambda (x)
  4627. (if (string-match "(.*)$" x)
  4628. (substring x 0 (match-beginning 0))
  4629. x))
  4630. list))
  4631. (defun org-assign-fast-keys (alist)
  4632. "Assign fast keys to a keyword-key alist.
  4633. Respect keys that are already there."
  4634. (let (new e (alt ?0))
  4635. (while (setq e (pop alist))
  4636. (if (or (memq (car e) '(:newline :grouptags :endgroup :startgroup))
  4637. (cdr e)) ;; Key already assigned.
  4638. (push e new)
  4639. (let ((clist (string-to-list (downcase (car e))))
  4640. (used (append new alist)))
  4641. (when (= (car clist) ?@)
  4642. (pop clist))
  4643. (while (and clist (rassoc (car clist) used))
  4644. (pop clist))
  4645. (unless clist
  4646. (while (rassoc alt used)
  4647. (cl-incf alt)))
  4648. (push (cons (car e) (or (car clist) alt)) new))))
  4649. (nreverse new)))
  4650. ;;; Some variables used in various places
  4651. (defvar org-window-configuration nil
  4652. "Used in various places to store a window configuration.")
  4653. (defvar org-selected-window nil
  4654. "Used in various places to store a window configuration.")
  4655. (defvar org-finish-function nil
  4656. "Function to be called when `C-c C-c' is used.
  4657. This is for getting out of special buffers like capture.")
  4658. (defvar org-last-state)
  4659. ;; Defined somewhere in this file, but used before definition.
  4660. (defvar org-entities) ;; defined in org-entities.el
  4661. (defvar org-struct-menu)
  4662. (defvar org-org-menu)
  4663. (defvar org-tbl-menu)
  4664. ;;;; Define the Org-mode
  4665. ;; We use a before-change function to check if a table might need
  4666. ;; an update.
  4667. (defvar org-table-may-need-update t
  4668. "Indicates that a table might need an update.
  4669. This variable is set by `org-before-change-function'.
  4670. `org-table-align' sets it back to nil.")
  4671. (defun org-before-change-function (_beg _end)
  4672. "Every change indicates that a table might need an update."
  4673. (setq org-table-may-need-update t))
  4674. (defvar org-mode-map)
  4675. (defvar org-inhibit-startup-visibility-stuff nil) ; Dynamically-scoped param.
  4676. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  4677. (defvar org-inhibit-logging nil) ; Dynamically-scoped param.
  4678. (defvar org-inhibit-blocking nil) ; Dynamically-scoped param.
  4679. (defvar org-table-buffer-is-an nil)
  4680. (defvar bidi-paragraph-direction)
  4681. (defvar buffer-face-mode-face)
  4682. (require 'outline)
  4683. (when (and (not (keymapp outline-mode-map)) (featurep 'allout))
  4684. (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"))
  4685. (require 'noutline "noutline" 'noerror) ;; stock XEmacs does not have it
  4686. ;; Other stuff we need.
  4687. (require 'time-date)
  4688. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  4689. (require 'easymenu)
  4690. (autoload 'easy-menu-add "easymenu")
  4691. (require 'overlay)
  4692. ;; (require 'org-macs) moved higher up in the file before it is first used
  4693. (require 'org-entities)
  4694. ;; (require 'org-compat) moved higher up in the file before it is first used
  4695. (require 'org-faces)
  4696. (require 'org-list)
  4697. (require 'org-pcomplete)
  4698. (require 'org-src)
  4699. (require 'org-footnote)
  4700. (require 'org-macro)
  4701. ;; babel
  4702. (require 'ob)
  4703. ;;;###autoload
  4704. (define-derived-mode org-mode outline-mode "Org"
  4705. "Outline-based notes management and organizer, alias
  4706. \"Carsten's outline-mode for keeping track of everything.\"
  4707. Org-mode develops organizational tasks around a NOTES file which
  4708. contains information about projects as plain text. Org-mode is
  4709. implemented on top of outline-mode, which is ideal to keep the content
  4710. of large files well structured. It supports ToDo items, deadlines and
  4711. time stamps, which magically appear in the diary listing of the Emacs
  4712. calendar. Tables are easily created with a built-in table editor.
  4713. Plain text URL-like links connect to websites, emails (VM), Usenet
  4714. messages (Gnus), BBDB entries, and any files related to the project.
  4715. For printing and sharing of notes, an Org-mode file (or a part of it)
  4716. can be exported as a structured ASCII or HTML file.
  4717. The following commands are available:
  4718. \\{org-mode-map}"
  4719. ;; Get rid of Outline menus, they are not needed
  4720. ;; Need to do this here because define-derived-mode sets up
  4721. ;; the keymap so late. Still, it is a waste to call this each time
  4722. ;; we switch another buffer into org-mode.
  4723. (if (featurep 'xemacs)
  4724. (when (boundp 'outline-mode-menu-heading)
  4725. ;; Assume this is Greg's port, it uses easymenu
  4726. (easy-menu-remove outline-mode-menu-heading)
  4727. (easy-menu-remove outline-mode-menu-show)
  4728. (easy-menu-remove outline-mode-menu-hide))
  4729. (define-key org-mode-map [menu-bar headings] 'undefined)
  4730. (define-key org-mode-map [menu-bar hide] 'undefined)
  4731. (define-key org-mode-map [menu-bar show] 'undefined))
  4732. (org-load-modules-maybe)
  4733. (when (featurep 'xemacs)
  4734. (easy-menu-add org-org-menu)
  4735. (easy-menu-add org-tbl-menu))
  4736. (org-install-agenda-files-menu)
  4737. (when org-descriptive-links (add-to-invisibility-spec '(org-link)))
  4738. (add-to-invisibility-spec '(org-cwidth))
  4739. (add-to-invisibility-spec '(org-hide-block . t))
  4740. (when (featurep 'xemacs)
  4741. (setq-local line-move-ignore-invisible t))
  4742. (setq-local outline-regexp org-outline-regexp)
  4743. (setq-local outline-level 'org-outline-level)
  4744. (setq bidi-paragraph-direction 'left-to-right)
  4745. (when (and org-ellipsis
  4746. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  4747. (fboundp 'make-glyph-code))
  4748. (unless org-display-table
  4749. (setq org-display-table (make-display-table)))
  4750. (set-display-table-slot
  4751. org-display-table 4
  4752. (vconcat (mapcar
  4753. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  4754. org-ellipsis)))
  4755. (if (stringp org-ellipsis) org-ellipsis "..."))))
  4756. (setq buffer-display-table org-display-table))
  4757. (org-set-regexps-and-options)
  4758. (org-set-font-lock-defaults)
  4759. (when (and org-tag-faces (not org-tags-special-faces-re))
  4760. ;; tag faces set outside customize.... force initialization.
  4761. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  4762. ;; Calc embedded
  4763. (setq-local calc-embedded-open-mode "# ")
  4764. ;; Modify a few syntax entries
  4765. (modify-syntax-entry ?@ "w")
  4766. (modify-syntax-entry ?\" "\"")
  4767. (modify-syntax-entry ?\\ "_")
  4768. (modify-syntax-entry ?~ "_")
  4769. (setq-local font-lock-unfontify-region-function 'org-unfontify-region)
  4770. ;; Activate before-change-function
  4771. (setq-local org-table-may-need-update t)
  4772. (org-add-hook 'before-change-functions 'org-before-change-function nil 'local)
  4773. ;; Check for running clock before killing a buffer
  4774. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  4775. ;; Initialize macros templates.
  4776. (org-macro-initialize-templates)
  4777. ;; Initialize radio targets.
  4778. (org-update-radio-target-regexp)
  4779. ;; Indentation.
  4780. (setq-local indent-line-function 'org-indent-line)
  4781. (setq-local indent-region-function 'org-indent-region)
  4782. ;; Filling and auto-filling.
  4783. (org-setup-filling)
  4784. ;; Comments.
  4785. (org-setup-comments-handling)
  4786. ;; Initialize cache.
  4787. (org-element-cache-reset)
  4788. ;; Beginning/end of defun
  4789. (setq-local beginning-of-defun-function 'org-backward-element)
  4790. (setq-local end-of-defun-function
  4791. (lambda ()
  4792. (if (not (org-at-heading-p))
  4793. (org-forward-element)
  4794. (org-forward-element)
  4795. (forward-char -1))))
  4796. ;; Next error for sparse trees
  4797. (setq-local next-error-function 'org-occur-next-match)
  4798. ;; Make sure dependence stuff works reliably, even for users who set it
  4799. ;; too late :-(
  4800. (if org-enforce-todo-dependencies
  4801. (add-hook 'org-blocker-hook
  4802. 'org-block-todo-from-children-or-siblings-or-parent)
  4803. (remove-hook 'org-blocker-hook
  4804. 'org-block-todo-from-children-or-siblings-or-parent))
  4805. (if org-enforce-todo-checkbox-dependencies
  4806. (add-hook 'org-blocker-hook
  4807. 'org-block-todo-from-checkboxes)
  4808. (remove-hook 'org-blocker-hook
  4809. 'org-block-todo-from-checkboxes))
  4810. ;; Align options lines
  4811. (setq-local
  4812. align-mode-rules-list
  4813. '((org-in-buffer-settings
  4814. (regexp . "^[ \t]*#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  4815. (modes . '(org-mode)))))
  4816. ;; Imenu
  4817. (setq-local imenu-create-index-function 'org-imenu-get-tree)
  4818. ;; Make isearch reveal context
  4819. (if (or (featurep 'xemacs)
  4820. (not (boundp 'outline-isearch-open-invisible-function)))
  4821. ;; Emacs 21 and XEmacs make use of the hook
  4822. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  4823. ;; Emacs 22 deals with this through a special variable
  4824. (setq-local outline-isearch-open-invisible-function
  4825. (lambda (&rest _) (org-show-context 'isearch))))
  4826. ;; Setup the pcomplete hooks
  4827. (setq-local pcomplete-command-completion-function 'org-pcomplete-initial)
  4828. (setq-local pcomplete-command-name-function 'org-command-at-point)
  4829. (setq-local pcomplete-default-completion-function 'ignore)
  4830. (setq-local pcomplete-parse-arguments-function 'org-parse-arguments)
  4831. (setq-local pcomplete-termination-string "")
  4832. (setq-local buffer-face-mode-face 'org-default)
  4833. ;; If empty file that did not turn on Org mode automatically, make
  4834. ;; it to.
  4835. (when (and org-insert-mode-line-in-empty-file
  4836. (org-called-interactively-p 'any)
  4837. (= (point-min) (point-max)))
  4838. (insert "# -*- mode: org -*-\n\n"))
  4839. (unless org-inhibit-startup
  4840. (org-unmodified
  4841. (when org-startup-with-beamer-mode (org-beamer-mode))
  4842. (when org-startup-align-all-tables
  4843. (org-table-map-tables #'org-table-align t))
  4844. (when org-startup-with-inline-images (org-display-inline-images))
  4845. (when org-startup-with-latex-preview (org-toggle-latex-fragment))
  4846. (unless org-inhibit-startup-visibility-stuff (org-set-startup-visibility))
  4847. (when org-startup-truncated (setq truncate-lines t))
  4848. (when org-startup-indented (require 'org-indent) (org-indent-mode 1))
  4849. (org-refresh-effort-properties)))
  4850. ;; Try to set `org-hide' face correctly.
  4851. (let ((foreground (org-find-invisible-foreground)))
  4852. (when foreground
  4853. (set-face-foreground 'org-hide foreground))))
  4854. ;; Update `customize-package-emacs-version-alist'
  4855. (add-to-list 'customize-package-emacs-version-alist
  4856. '(Org ("6.21b" . "23.1") ("6.33x" . "23.2")
  4857. ("7.8.11" . "24.1") ("7.9.4" . "24.3")
  4858. ("8.2.6" . "24.4") ("8.3" . "25.1")))
  4859. (defvar org-mode-transpose-word-syntax-table
  4860. (let ((st (make-syntax-table text-mode-syntax-table)))
  4861. (dolist (c org-emphasis-alist st)
  4862. (modify-syntax-entry (string-to-char (car c)) "w p" st))))
  4863. (when (fboundp 'abbrev-table-put)
  4864. (abbrev-table-put org-mode-abbrev-table
  4865. :parents (list text-mode-abbrev-table)))
  4866. (defun org-find-invisible-foreground ()
  4867. (let ((candidates (remove
  4868. "unspecified-bg"
  4869. (nconc
  4870. (list (face-background 'default)
  4871. (face-background 'org-default))
  4872. (mapcar
  4873. (lambda (alist)
  4874. (when (boundp alist)
  4875. (cdr (assoc 'background-color (symbol-value alist)))))
  4876. '(default-frame-alist initial-frame-alist window-system-default-frame-alist))
  4877. (list (face-foreground 'org-hide))))))
  4878. (car (remove nil candidates))))
  4879. (defun org-current-time (&optional rounding-minutes past)
  4880. "Current time, possibly rounded to ROUNDING-MINUTES.
  4881. When ROUNDING-MINUTES is not an integer, fall back on the car of
  4882. `org-time-stamp-rounding-minutes'. When PAST is non-nil, ensure
  4883. the rounding returns a past time."
  4884. (let ((r (or (and (integerp rounding-minutes) rounding-minutes)
  4885. (car org-time-stamp-rounding-minutes)))
  4886. (time (decode-time)) res)
  4887. (if (< r 1)
  4888. (current-time)
  4889. (setq res
  4890. (apply 'encode-time
  4891. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  4892. (nthcdr 2 time))))
  4893. (if (and past (< (org-float-time (time-subtract (current-time) res)) 0))
  4894. (seconds-to-time (- (org-float-time res) (* r 60)))
  4895. res))))
  4896. (defun org-today ()
  4897. "Return today date, considering `org-extend-today-until'."
  4898. (time-to-days
  4899. (time-subtract (current-time)
  4900. (list 0 (* 3600 org-extend-today-until) 0))))
  4901. ;;;; Font-Lock stuff, including the activators
  4902. (defvar org-mouse-map (make-sparse-keymap))
  4903. (org-defkey org-mouse-map [mouse-2] 'org-open-at-mouse)
  4904. (org-defkey org-mouse-map [mouse-3] 'org-find-file-at-mouse)
  4905. (when org-mouse-1-follows-link
  4906. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  4907. (when org-tab-follows-link
  4908. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  4909. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  4910. (require 'font-lock)
  4911. (defconst org-non-link-chars "]\t\n\r<>")
  4912. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "file+emacs"
  4913. "file+sys" "news" "shell" "elisp" "doi" "message"
  4914. "help"))
  4915. (defvar org-link-types-re nil
  4916. "Matches a link that has a url-like prefix like \"http:\"")
  4917. (defvar org-link-re-with-space nil
  4918. "Matches a link with spaces, optional angular brackets around it.")
  4919. (defvar org-link-re-with-space2 nil
  4920. "Matches a link with spaces, optional angular brackets around it.")
  4921. (defvar org-link-re-with-space3 nil
  4922. "Matches a link with spaces, only for internal part in bracket links.")
  4923. (defvar org-angle-link-re nil
  4924. "Matches link with angular brackets, spaces are allowed.")
  4925. (defvar org-plain-link-re nil
  4926. "Matches plain link, without spaces.")
  4927. (defvar org-bracket-link-regexp nil
  4928. "Matches a link in double brackets.")
  4929. (defvar org-bracket-link-analytic-regexp nil
  4930. "Regular expression used to analyze links.
  4931. Here is what the match groups contain after a match:
  4932. 1: http:
  4933. 2: http
  4934. 3: path
  4935. 4: [desc]
  4936. 5: desc")
  4937. (defvar org-bracket-link-analytic-regexp++ nil
  4938. "Like `org-bracket-link-analytic-regexp', but include coderef internal type.")
  4939. (defvar org-any-link-re nil
  4940. "Regular expression matching any link.")
  4941. (defconst org-match-sexp-depth 3
  4942. "Number of stacked braces for sub/superscript matching.")
  4943. (defun org-create-multibrace-regexp (left right n)
  4944. "Create a regular expression which will match a balanced sexp.
  4945. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  4946. as single character strings.
  4947. The regexp returned will match the entire expression including the
  4948. delimiters. It will also define a single group which contains the
  4949. match except for the outermost delimiters. The maximum depth of
  4950. stacked delimiters is N. Escaping delimiters is not possible."
  4951. (let* ((nothing (concat "[^" left right "]*?"))
  4952. (or "\\|")
  4953. (re nothing)
  4954. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  4955. (while (> n 1)
  4956. (setq n (1- n)
  4957. re (concat re or next)
  4958. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  4959. (concat left "\\(" re "\\)" right)))
  4960. (defconst org-match-substring-regexp
  4961. (concat
  4962. "\\(\\S-\\)\\([_^]\\)\\("
  4963. "\\(?:" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4964. "\\|"
  4965. "\\(?:" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  4966. "\\|"
  4967. "\\(?:\\*\\|[+-]?[[:alnum:].,\\]*[[:alnum:]]\\)\\)")
  4968. "The regular expression matching a sub- or superscript.")
  4969. (defconst org-match-substring-with-braces-regexp
  4970. (concat
  4971. "\\(\\S-\\)\\([_^]\\)"
  4972. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)")
  4973. "The regular expression matching a sub- or superscript, forcing braces.")
  4974. (defun org-make-link-regexps ()
  4975. "Update the link regular expressions.
  4976. This should be called after the variable `org-link-types' has changed."
  4977. (let ((types-re (regexp-opt org-link-types t)))
  4978. (setq org-link-types-re
  4979. (concat "\\`" types-re ":")
  4980. org-link-re-with-space
  4981. (concat "<?" types-re ":"
  4982. "\\([^" org-non-link-chars " ]"
  4983. "[^" org-non-link-chars "]*"
  4984. "[^" org-non-link-chars " ]\\)>?")
  4985. org-link-re-with-space2
  4986. (concat "<?" types-re ":"
  4987. "\\([^" org-non-link-chars " ]"
  4988. "[^\t\n\r]*"
  4989. "[^" org-non-link-chars " ]\\)>?")
  4990. org-link-re-with-space3
  4991. (concat "<?" types-re ":"
  4992. "\\([^" org-non-link-chars " ]"
  4993. "[^\t\n\r]*\\)")
  4994. org-angle-link-re
  4995. (format "<%s:\\([^>\n]*\\(?:\n[ \t]*[^> \t\n][^>\n]*\\)*\\)>"
  4996. types-re)
  4997. org-plain-link-re
  4998. (concat
  4999. "\\<" types-re ":"
  5000. (org-re "\\([^ \t\n()<>]+\\(?:([[:word:]0-9_]+)\\|\\([^[:punct:] \t\n]\\|/\\)\\)\\)"))
  5001. ;; "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  5002. org-bracket-link-regexp
  5003. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  5004. org-bracket-link-analytic-regexp
  5005. (concat
  5006. "\\[\\["
  5007. "\\(" types-re ":\\)?"
  5008. "\\([^]]+\\)"
  5009. "\\]"
  5010. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  5011. "\\]")
  5012. org-bracket-link-analytic-regexp++
  5013. (concat
  5014. "\\[\\["
  5015. "\\(" (regexp-opt (cons "coderef" org-link-types) t) ":\\)?"
  5016. "\\([^]]+\\)"
  5017. "\\]"
  5018. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  5019. "\\]")
  5020. org-any-link-re
  5021. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  5022. org-angle-link-re "\\)\\|\\("
  5023. org-plain-link-re "\\)"))))
  5024. (org-make-link-regexps)
  5025. (defvar org-emph-face nil)
  5026. (defun org-do-emphasis-faces (limit)
  5027. "Run through the buffer and emphasize strings."
  5028. (let (rtn a)
  5029. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  5030. (let* ((border (char-after (match-beginning 3)))
  5031. (bre (regexp-quote (char-to-string border))))
  5032. (when (and (not (= border (char-after (match-beginning 4))))
  5033. (not (string-match-p (concat bre ".*" bre)
  5034. (replace-regexp-in-string
  5035. "\n" " "
  5036. (substring (match-string 2) 1 -1)))))
  5037. (setq rtn t)
  5038. (setq a (assoc (match-string 3) org-emphasis-alist))
  5039. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  5040. 'face
  5041. (nth 1 a))
  5042. (and (nth 2 a)
  5043. (org-remove-flyspell-overlays-in
  5044. (match-beginning 0) (match-end 0)))
  5045. (add-text-properties (match-beginning 2) (match-end 2)
  5046. '(font-lock-multiline t org-emphasis t))
  5047. (when org-hide-emphasis-markers
  5048. (add-text-properties (match-end 4) (match-beginning 5)
  5049. '(invisible org-link))
  5050. (add-text-properties (match-beginning 3) (match-end 3)
  5051. '(invisible org-link)))))
  5052. (goto-char (1+ (match-beginning 0))))
  5053. rtn))
  5054. (defun org-emphasize (&optional char)
  5055. "Insert or change an emphasis, i.e. a font like bold or italic.
  5056. If there is an active region, change that region to a new emphasis.
  5057. If there is no region, just insert the marker characters and position
  5058. the cursor between them.
  5059. CHAR should be the marker character. If it is a space, it means to
  5060. remove the emphasis of the selected region.
  5061. If CHAR is not given (for example in an interactive call) it will be
  5062. prompted for."
  5063. (interactive)
  5064. (let ((erc org-emphasis-regexp-components)
  5065. (string "") beg end move s)
  5066. (if (org-region-active-p)
  5067. (setq beg (region-beginning)
  5068. end (region-end)
  5069. string (buffer-substring beg end))
  5070. (setq move t))
  5071. (unless char
  5072. (message "Emphasis marker or tag: [%s]"
  5073. (mapconcat #'car org-emphasis-alist ""))
  5074. (setq char (read-char-exclusive)))
  5075. (if (equal char ?\s)
  5076. (setq s ""
  5077. move nil)
  5078. (unless (assoc (char-to-string char) org-emphasis-alist)
  5079. (user-error "No such emphasis marker: \"%c\"" char))
  5080. (setq s (char-to-string char)))
  5081. (while (and (> (length string) 1)
  5082. (equal (substring string 0 1) (substring string -1))
  5083. (assoc (substring string 0 1) org-emphasis-alist))
  5084. (setq string (substring string 1 -1)))
  5085. (setq string (concat s string s))
  5086. (when beg (delete-region beg end))
  5087. (unless (or (bolp)
  5088. (string-match (concat "[" (nth 0 erc) "\n]")
  5089. (char-to-string (char-before (point)))))
  5090. (insert " "))
  5091. (unless (or (eobp)
  5092. (string-match (concat "[" (nth 1 erc) "\n]")
  5093. (char-to-string (char-after (point)))))
  5094. (insert " ") (backward-char 1))
  5095. (insert string)
  5096. (and move (backward-char 1))))
  5097. (defconst org-nonsticky-props
  5098. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text htmlize-link))
  5099. (defsubst org-rear-nonsticky-at (pos)
  5100. (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
  5101. (defun org-activate-plain-links (limit)
  5102. "Add link properties for plain links."
  5103. (let (f hl)
  5104. (when (and (re-search-forward (concat org-plain-link-re) limit t)
  5105. (not (member 'org-tag
  5106. (get-text-property (1- (match-beginning 0)) 'face)))
  5107. (not (org-in-src-block-p)))
  5108. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5109. (setq f (get-text-property (match-beginning 0) 'face))
  5110. (setq hl (org-match-string-no-properties 0))
  5111. (unless (or (eq f 'org-tag)
  5112. (and (listp f) (memq 'org-tag f)))
  5113. (add-text-properties (match-beginning 0) (match-end 0)
  5114. (list 'mouse-face 'highlight
  5115. 'face 'org-link
  5116. 'htmlize-link `(:uri ,hl)
  5117. 'keymap org-mouse-map))
  5118. (org-rear-nonsticky-at (match-end 0)))
  5119. t)))
  5120. (defun org-activate-code (limit)
  5121. (when (re-search-forward "^[ \t]*\\(:\\(?: .*\\|$\\)\n?\\)" limit t)
  5122. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5123. (remove-text-properties (match-beginning 0) (match-end 0)
  5124. '(display t invisible t intangible t))
  5125. t))
  5126. (defcustom org-src-fontify-natively t
  5127. "When non-nil, fontify code in code blocks."
  5128. :type 'boolean
  5129. :version "24.4"
  5130. :package-version '(Org . "8.3")
  5131. :group 'org-appearance
  5132. :group 'org-babel)
  5133. (defcustom org-allow-promoting-top-level-subtree nil
  5134. "When non-nil, allow promoting a top level subtree.
  5135. The leading star of the top level headline will be replaced
  5136. by a #."
  5137. :type 'boolean
  5138. :version "24.1"
  5139. :group 'org-appearance)
  5140. (defun org-fontify-meta-lines-and-blocks (limit)
  5141. (condition-case nil
  5142. (org-fontify-meta-lines-and-blocks-1 limit)
  5143. (error (message "org-mode fontification error"))))
  5144. (defun org-fontify-meta-lines-and-blocks-1 (limit)
  5145. "Fontify #+ lines and blocks."
  5146. (let ((case-fold-search t))
  5147. (when (re-search-forward
  5148. "^\\([ \t]*#\\(\\(\\+[a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)[ \t]*\\(\\([^ \t\n]*\\)[ \t]*\\(.*\\)\\)\\)"
  5149. limit t)
  5150. (let ((beg (match-beginning 0))
  5151. (block-start (match-end 0))
  5152. (block-end nil)
  5153. (lang (match-string 7))
  5154. (beg1 (line-beginning-position 2))
  5155. (dc1 (downcase (match-string 2)))
  5156. (dc3 (downcase (match-string 3)))
  5157. end end1 quoting block-type)
  5158. (cond
  5159. ((and (match-end 4) (equal dc3 "+begin"))
  5160. ;; Truly a block
  5161. (setq block-type (downcase (match-string 5))
  5162. quoting (member block-type org-protecting-blocks))
  5163. (when (re-search-forward
  5164. (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
  5165. nil t) ;; on purpose, we look further than LIMIT
  5166. (setq end (min (point-max) (match-end 0))
  5167. end1 (min (point-max) (1- (match-beginning 0))))
  5168. (setq block-end (match-beginning 0))
  5169. (when quoting
  5170. (org-remove-flyspell-overlays-in beg1 end1)
  5171. (remove-text-properties beg end
  5172. '(display t invisible t intangible t)))
  5173. (add-text-properties
  5174. beg end '(font-lock-fontified t font-lock-multiline t))
  5175. (add-text-properties beg beg1 '(face org-meta-line))
  5176. (org-remove-flyspell-overlays-in beg beg1)
  5177. (add-text-properties ; For end_src
  5178. end1 (min (point-max) (1+ end)) '(face org-meta-line))
  5179. (org-remove-flyspell-overlays-in end1 end)
  5180. (cond
  5181. ((and lang (not (string= lang "")) org-src-fontify-natively)
  5182. (org-src-font-lock-fontify-block lang block-start block-end)
  5183. (add-text-properties beg1 block-end '(src-block t)))
  5184. (quoting
  5185. (add-text-properties beg1 (min (point-max) (1+ end1))
  5186. '(face org-block))) ; end of source block
  5187. ((not org-fontify-quote-and-verse-blocks))
  5188. ((string= block-type "quote")
  5189. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-quote)))
  5190. ((string= block-type "verse")
  5191. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-verse))))
  5192. (add-text-properties beg beg1 '(face org-block-begin-line))
  5193. (add-text-properties (min (point-max) (1+ end)) (min (point-max) (1+ end1))
  5194. '(face org-block-end-line))
  5195. t))
  5196. ((member dc1 '("+title:" "+author:" "+email:" "+date:"))
  5197. (org-remove-flyspell-overlays-in
  5198. (match-beginning 0)
  5199. (if (equal "+title:" dc1) (match-end 2) (match-end 0)))
  5200. (add-text-properties
  5201. beg (match-end 3)
  5202. (if (member (intern (substring dc1 1 -1)) org-hidden-keywords)
  5203. '(font-lock-fontified t invisible t)
  5204. '(font-lock-fontified t face org-document-info-keyword)))
  5205. (add-text-properties
  5206. (match-beginning 6) (min (point-max) (1+ (match-end 6)))
  5207. (if (string-equal dc1 "+title:")
  5208. '(font-lock-fontified t face org-document-title)
  5209. '(font-lock-fontified t face org-document-info))))
  5210. ((equal dc1 "+caption:")
  5211. (org-remove-flyspell-overlays-in (match-end 2) (match-end 0))
  5212. (remove-text-properties (match-beginning 0) (match-end 0)
  5213. '(display t invisible t intangible t))
  5214. (add-text-properties (match-beginning 1) (match-end 3)
  5215. '(font-lock-fontified t face org-meta-line))
  5216. (add-text-properties (match-beginning 6) (+ (match-end 6) 1)
  5217. '(font-lock-fontified t face org-block))
  5218. t)
  5219. ((member dc3 '(" " ""))
  5220. (org-remove-flyspell-overlays-in beg (match-end 0))
  5221. (add-text-properties
  5222. beg (match-end 0)
  5223. '(font-lock-fontified t face font-lock-comment-face)))
  5224. (t ;; just any other in-buffer setting, but not indented
  5225. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5226. (remove-text-properties (match-beginning 0) (match-end 0)
  5227. '(display t invisible t intangible t))
  5228. (add-text-properties beg (match-end 0)
  5229. '(font-lock-fontified t face org-meta-line))
  5230. t))))))
  5231. (defun org-fontify-drawers (limit)
  5232. "Fontify drawers."
  5233. (when (re-search-forward org-drawer-regexp limit t)
  5234. (add-text-properties
  5235. (match-beginning 0) (match-end 0)
  5236. '(font-lock-fontified t face org-special-keyword))
  5237. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5238. t))
  5239. (defun org-fontify-macros (limit)
  5240. "Fontify macros."
  5241. (when (re-search-forward "\\({{{\\).+?\\(}}}\\)" limit t)
  5242. (add-text-properties
  5243. (match-beginning 0) (match-end 0)
  5244. '(font-lock-fontified t face org-macro))
  5245. (when org-hide-macro-markers
  5246. (add-text-properties (match-end 2) (match-beginning 2)
  5247. '(invisible t))
  5248. (add-text-properties (match-beginning 1) (match-end 1)
  5249. '(invisible t)))
  5250. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5251. t))
  5252. (defun org-activate-angle-links (limit)
  5253. "Add text properties for angle links."
  5254. (when (and (re-search-forward org-angle-link-re limit t)
  5255. (not (org-in-src-block-p)))
  5256. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5257. (add-text-properties (match-beginning 0) (match-end 0)
  5258. (list 'mouse-face 'highlight
  5259. 'keymap org-mouse-map
  5260. 'font-lock-multiline t))
  5261. (org-rear-nonsticky-at (match-end 0))
  5262. t))
  5263. (defun org-activate-footnote-links (limit)
  5264. "Add text properties for footnotes."
  5265. (let ((fn (org-footnote-next-reference-or-definition limit)))
  5266. (when fn
  5267. (let* ((beg (nth 1 fn))
  5268. (end (nth 2 fn))
  5269. (label (car fn))
  5270. (referencep (/= (line-beginning-position) beg)))
  5271. (when (and referencep (nth 3 fn))
  5272. (save-excursion
  5273. (goto-char beg)
  5274. (search-forward (or label "fn:"))
  5275. (org-remove-flyspell-overlays-in beg (match-end 0))))
  5276. (add-text-properties beg end
  5277. (list 'mouse-face 'highlight
  5278. 'keymap org-mouse-map
  5279. 'help-echo
  5280. (if referencep "Footnote reference"
  5281. "Footnote definition")
  5282. 'font-lock-fontified t
  5283. 'font-lock-multiline t
  5284. 'face 'org-footnote))))))
  5285. (defun org-activate-bracket-links (limit)
  5286. "Add text properties for bracketed links."
  5287. (when (and (re-search-forward org-bracket-link-regexp limit t)
  5288. (not (org-in-src-block-p)))
  5289. (let* ((hl (org-match-string-no-properties 1))
  5290. (help (concat "LINK: " (save-match-data (org-link-unescape hl))))
  5291. (ip (org-maybe-intangible
  5292. (list 'invisible 'org-link
  5293. 'keymap org-mouse-map 'mouse-face 'highlight
  5294. 'font-lock-multiline t 'help-echo help
  5295. 'htmlize-link `(:uri ,hl))))
  5296. (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
  5297. 'font-lock-multiline t 'help-echo help
  5298. 'htmlize-link `(:uri ,hl))))
  5299. ;; We need to remove the invisible property here. Table narrowing
  5300. ;; may have made some of this invisible.
  5301. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5302. (remove-text-properties (match-beginning 0) (match-end 0)
  5303. '(invisible nil))
  5304. (if (match-end 3)
  5305. (progn
  5306. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  5307. (org-rear-nonsticky-at (match-beginning 3))
  5308. (add-text-properties (match-beginning 3) (match-end 3) vp)
  5309. (org-rear-nonsticky-at (match-end 3))
  5310. (add-text-properties (match-end 3) (match-end 0) ip)
  5311. (org-rear-nonsticky-at (match-end 0)))
  5312. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  5313. (org-rear-nonsticky-at (match-beginning 1))
  5314. (add-text-properties (match-beginning 1) (match-end 1) vp)
  5315. (org-rear-nonsticky-at (match-end 1))
  5316. (add-text-properties (match-end 1) (match-end 0) ip)
  5317. (org-rear-nonsticky-at (match-end 0)))
  5318. t)))
  5319. (defun org-activate-dates (limit)
  5320. "Add text properties for dates."
  5321. (when (and (re-search-forward org-tsr-regexp-both limit t)
  5322. (not (equal (char-before (match-beginning 0)) 91)))
  5323. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5324. (add-text-properties (match-beginning 0) (match-end 0)
  5325. (list 'mouse-face 'highlight
  5326. 'keymap org-mouse-map))
  5327. (org-rear-nonsticky-at (match-end 0))
  5328. (when org-display-custom-times
  5329. (if (match-end 3)
  5330. (org-display-custom-time (match-beginning 3) (match-end 3))
  5331. (org-display-custom-time (match-beginning 1) (match-end 1))))
  5332. t))
  5333. (defvar-local org-target-link-regexp nil
  5334. "Regular expression matching radio targets in plain text.")
  5335. (defconst org-target-regexp (let ((border "[^<>\n\r \t]"))
  5336. (format "<<\\(%s\\|%s[^<>\n\r]*%s\\)>>"
  5337. border border border))
  5338. "Regular expression matching a link target.")
  5339. (defconst org-radio-target-regexp (format "<%s>" org-target-regexp)
  5340. "Regular expression matching a radio target.")
  5341. (defconst org-any-target-regexp
  5342. (format "%s\\|%s" org-radio-target-regexp org-target-regexp)
  5343. "Regular expression matching any target.")
  5344. (defun org-activate-target-links (limit)
  5345. "Add text properties for target matches."
  5346. (when org-target-link-regexp
  5347. (let ((case-fold-search t))
  5348. (when (re-search-forward org-target-link-regexp limit t)
  5349. (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
  5350. (add-text-properties (match-beginning 1) (match-end 1)
  5351. (list 'mouse-face 'highlight
  5352. 'keymap org-mouse-map
  5353. 'help-echo "Radio target link"
  5354. 'org-linked-text t))
  5355. (org-rear-nonsticky-at (match-end 1))
  5356. t))))
  5357. (defun org-update-radio-target-regexp ()
  5358. "Find all radio targets in this file and update the regular expression.
  5359. Also refresh fontification if needed."
  5360. (interactive)
  5361. (let ((old-regexp org-target-link-regexp)
  5362. (before-re "\\(?:^\\|[^[:alnum:]]\\)\\(")
  5363. (after-re "\\)\\(?:$\\|[^[:alnum:]]\\)")
  5364. (targets
  5365. (org-with-wide-buffer
  5366. (goto-char (point-min))
  5367. (let (rtn)
  5368. (while (re-search-forward org-radio-target-regexp nil t)
  5369. ;; Make sure point is really within the object.
  5370. (backward-char)
  5371. (let ((obj (org-element-context)))
  5372. (when (eq (org-element-type obj) 'radio-target)
  5373. (cl-pushnew (org-element-property :value obj) rtn
  5374. :test #'equal))))
  5375. rtn))))
  5376. (setq org-target-link-regexp
  5377. (and targets
  5378. (concat before-re
  5379. (mapconcat
  5380. (lambda (x)
  5381. (replace-regexp-in-string
  5382. " +" "\\s-+" (regexp-quote x) t t))
  5383. targets
  5384. "\\|")
  5385. after-re)))
  5386. (unless (equal old-regexp org-target-link-regexp)
  5387. ;; Clean-up cache.
  5388. (let ((regexp (cond ((not old-regexp) org-target-link-regexp)
  5389. ((not org-target-link-regexp) old-regexp)
  5390. (t
  5391. (concat before-re
  5392. (mapconcat
  5393. (lambda (re)
  5394. (substring re (length before-re)
  5395. (- (length after-re))))
  5396. (list old-regexp org-target-link-regexp)
  5397. "\\|")
  5398. after-re)))))
  5399. (org-with-wide-buffer
  5400. (goto-char (point-min))
  5401. (while (re-search-forward regexp nil t)
  5402. (org-element-cache-refresh (match-beginning 1)))))
  5403. ;; Re fontify buffer.
  5404. (when (memq 'radio org-highlight-links)
  5405. (org-restart-font-lock)))))
  5406. (defun org-hide-wide-columns (limit)
  5407. (let (s e)
  5408. (setq s (text-property-any (point) (or limit (point-max))
  5409. 'org-cwidth t))
  5410. (when s
  5411. (setq e (next-single-property-change s 'org-cwidth))
  5412. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  5413. (goto-char e)
  5414. t)))
  5415. (defvar org-latex-and-related-regexp nil
  5416. "Regular expression for highlighting LaTeX, entities and sub/superscript.")
  5417. (defun org-compute-latex-and-related-regexp ()
  5418. "Compute regular expression for LaTeX, entities and sub/superscript.
  5419. Result depends on variable `org-highlight-latex-and-related'."
  5420. (setq-local
  5421. org-latex-and-related-regexp
  5422. (let* ((re-sub
  5423. (cond ((not (memq 'script org-highlight-latex-and-related)) nil)
  5424. ((eq org-use-sub-superscripts '{})
  5425. (list org-match-substring-with-braces-regexp))
  5426. (org-use-sub-superscripts (list org-match-substring-regexp))))
  5427. (re-latex
  5428. (when (memq 'latex org-highlight-latex-and-related)
  5429. (let ((matchers (plist-get org-format-latex-options :matchers)))
  5430. (delq nil
  5431. (mapcar (lambda (x)
  5432. (and (member (car x) matchers) (nth 1 x)))
  5433. org-latex-regexps)))))
  5434. (re-entities
  5435. (when (memq 'entities org-highlight-latex-and-related)
  5436. (list "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)"))))
  5437. (mapconcat 'identity (append re-latex re-entities re-sub) "\\|"))))
  5438. (defun org-do-latex-and-related (limit)
  5439. "Highlight LaTeX snippets and environments, entities and sub/superscript.
  5440. LIMIT bounds the search for syntax to highlight. Stop at first
  5441. highlighted object, if any. Return t if some highlighting was
  5442. done, nil otherwise."
  5443. (when (org-string-nw-p org-latex-and-related-regexp)
  5444. (catch 'found
  5445. (while (re-search-forward org-latex-and-related-regexp limit t)
  5446. (unless (memq (car-safe (get-text-property (1+ (match-beginning 0))
  5447. 'face))
  5448. '(org-code org-verbatim underline))
  5449. (let ((offset (if (memq (char-after (1+ (match-beginning 0)))
  5450. '(?_ ?^))
  5451. 1
  5452. 0)))
  5453. (font-lock-prepend-text-property
  5454. (+ offset (match-beginning 0)) (match-end 0)
  5455. 'face 'org-latex-and-related)
  5456. (add-text-properties (+ offset (match-beginning 0)) (match-end 0)
  5457. '(font-lock-multiline t)))
  5458. (throw 'found t)))
  5459. nil)))
  5460. (defun org-restart-font-lock ()
  5461. "Restart `font-lock-mode', to force refontification."
  5462. (when (and (boundp 'font-lock-mode) font-lock-mode)
  5463. (font-lock-mode -1)
  5464. (font-lock-mode 1)))
  5465. (defun org-activate-tags (limit)
  5466. (when (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \r\n]") limit t)
  5467. (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
  5468. (add-text-properties (match-beginning 1) (match-end 1)
  5469. (list 'mouse-face 'highlight
  5470. 'keymap org-mouse-map))
  5471. (org-rear-nonsticky-at (match-end 1))
  5472. t))
  5473. (defun org-outline-level ()
  5474. "Compute the outline level of the heading at point.
  5475. If this is called at a normal headline, the level is the number
  5476. of stars. Use `org-reduced-level' to remove the effect of
  5477. `org-odd-levels'. Unlike to `org-current-level', this function
  5478. takes into consideration inlinetasks."
  5479. (org-with-wide-buffer
  5480. (end-of-line)
  5481. (if (re-search-backward org-outline-regexp-bol nil t)
  5482. (1- (- (match-end 0) (match-beginning 0)))
  5483. 0)))
  5484. (defvar org-font-lock-keywords nil)
  5485. (defsubst org-re-property (property &optional literal allow-null value)
  5486. "Return a regexp matching a PROPERTY line.
  5487. When optional argument LITERAL is non-nil, do not quote PROPERTY.
  5488. This is useful when PROPERTY is a regexp. When ALLOW-NULL is
  5489. non-nil, match properties even without a value.
  5490. Match group 3 is set to the value when it exists. If there is no
  5491. value and ALLOW-NULL is non-nil, it is set to the empty string.
  5492. With optional argument VALUE, match only property lines with
  5493. that value; in this case, ALLOW-NULL is ignored. VALUE is quoted
  5494. unless LITERAL is non-nil."
  5495. (concat
  5496. "^\\(?4:[ \t]*\\)"
  5497. (format "\\(?1::\\(?2:%s\\):\\)"
  5498. (if literal property (regexp-quote property)))
  5499. (cond (value
  5500. (format "[ \t]+\\(?3:%s\\)\\(?5:[ \t]*\\)$"
  5501. (if literal value (regexp-quote value))))
  5502. (allow-null
  5503. "\\(?:\\(?3:$\\)\\|[ \t]+\\(?3:.*?\\)\\)\\(?5:[ \t]*\\)$")
  5504. (t
  5505. "[ \t]+\\(?3:[^ \r\t\n]+.*?\\)\\(?5:[ \t]*\\)$"))))
  5506. (defconst org-property-re
  5507. (org-re-property "\\S-+" 'literal t)
  5508. "Regular expression matching a property line.
  5509. There are four matching groups:
  5510. 1: :PROPKEY: including the leading and trailing colon,
  5511. 2: PROPKEY without the leading and trailing colon,
  5512. 3: PROPVAL without leading or trailing spaces,
  5513. 4: the indentation of the current line,
  5514. 5: trailing whitespace.")
  5515. (defvar org-font-lock-hook nil
  5516. "Functions to be called for special font lock stuff.")
  5517. (defvar org-font-lock-set-keywords-hook nil
  5518. "Functions that can manipulate `org-font-lock-extra-keywords'.
  5519. This is called after `org-font-lock-extra-keywords' is defined, but before
  5520. it is installed to be used by font lock. This can be useful if something
  5521. needs to be inserted at a specific position in the font-lock sequence.")
  5522. (defun org-font-lock-hook (limit)
  5523. "Run `org-font-lock-hook' within LIMIT."
  5524. (run-hook-with-args 'org-font-lock-hook limit))
  5525. (defun org-set-font-lock-defaults ()
  5526. "Set font lock defaults for the current buffer."
  5527. (let* ((em org-fontify-emphasized-text)
  5528. (lk org-highlight-links)
  5529. (org-font-lock-extra-keywords
  5530. (list
  5531. ;; Call the hook
  5532. '(org-font-lock-hook)
  5533. ;; Headlines
  5534. `(,(if org-fontify-whole-heading-line
  5535. "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
  5536. "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
  5537. (1 (org-get-level-face 1))
  5538. (2 (org-get-level-face 2))
  5539. (3 (org-get-level-face 3)))
  5540. ;; Table lines
  5541. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  5542. (1 'org-table t))
  5543. ;; Table internals
  5544. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  5545. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  5546. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  5547. '("| *\\(<[lrc]?[0-9]*>\\)" (1 'org-formula t))
  5548. ;; Drawers
  5549. '(org-fontify-drawers)
  5550. ;; Properties
  5551. (list org-property-re
  5552. '(1 'org-special-keyword t)
  5553. '(3 'org-property-value t))
  5554. ;; Links
  5555. (when (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  5556. (when (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  5557. (when (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  5558. (when (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  5559. (when (memq 'radio lk) '(org-activate-target-links (1 'org-link t)))
  5560. (when (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  5561. (when (memq 'footnote lk) '(org-activate-footnote-links))
  5562. ;; Targets.
  5563. (list org-any-target-regexp '(0 'org-target t))
  5564. ;; Diary sexps.
  5565. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  5566. ;; Macro
  5567. '(org-fontify-macros)
  5568. '(org-hide-wide-columns (0 nil append))
  5569. ;; TODO keyword
  5570. (list (format org-heading-keyword-regexp-format
  5571. org-todo-regexp)
  5572. '(2 (org-get-todo-face 2) t))
  5573. ;; DONE
  5574. (if org-fontify-done-headline
  5575. (list (format org-heading-keyword-regexp-format
  5576. (concat
  5577. "\\(?:"
  5578. (mapconcat 'regexp-quote org-done-keywords "\\|")
  5579. "\\)"))
  5580. '(2 'org-headline-done t))
  5581. nil)
  5582. ;; Priorities
  5583. '(org-font-lock-add-priority-faces)
  5584. ;; Tags
  5585. '(org-font-lock-add-tag-faces)
  5586. ;; Tags groups
  5587. (when (and org-group-tags org-tag-groups-alist)
  5588. (list (concat org-outline-regexp-bol ".+\\(:"
  5589. (regexp-opt (mapcar 'car org-tag-groups-alist))
  5590. ":\\).*$")
  5591. '(1 'org-tag-group prepend)))
  5592. ;; Special keywords
  5593. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  5594. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  5595. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  5596. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  5597. ;; Emphasis
  5598. (when em
  5599. (if (featurep 'xemacs)
  5600. '(org-do-emphasis-faces (0 nil append))
  5601. '(org-do-emphasis-faces)))
  5602. ;; Checkboxes
  5603. '("^[ \t]*\\(?:[-+*]\\|[0-9]+[.)]\\)[ \t]+\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?\\(\\[[- X]\\]\\)"
  5604. 1 'org-checkbox prepend)
  5605. (when (cdr (assq 'checkbox org-list-automatic-rules))
  5606. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  5607. (0 (org-get-checkbox-statistics-face) t)))
  5608. ;; Description list items
  5609. '("^[ \t]*[-+*][ \t]+\\(.*?[ \t]+::\\)\\([ \t]+\\|$\\)"
  5610. 1 'org-list-dt prepend)
  5611. ;; ARCHIVEd headings
  5612. (list (concat
  5613. org-outline-regexp-bol
  5614. "\\(.*:" org-archive-tag ":.*\\)")
  5615. '(1 'org-archived prepend))
  5616. ;; Specials
  5617. '(org-do-latex-and-related)
  5618. '(org-fontify-entities)
  5619. '(org-raise-scripts)
  5620. ;; Code
  5621. '(org-activate-code (1 'org-code t))
  5622. ;; COMMENT
  5623. (list (format
  5624. "^\\*+\\(?: +%s\\)?\\(?: +\\[#[A-Z0-9]\\]\\)? +\\(?9:%s\\)\\(?: \\|$\\)"
  5625. org-todo-regexp
  5626. org-comment-string)
  5627. '(9 'org-special-keyword t))
  5628. ;; Blocks and meta lines
  5629. '(org-fontify-meta-lines-and-blocks))))
  5630. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  5631. (run-hooks 'org-font-lock-set-keywords-hook)
  5632. ;; Now set the full font-lock-keywords
  5633. (setq-local org-font-lock-keywords org-font-lock-extra-keywords)
  5634. (setq-local font-lock-defaults
  5635. '(org-font-lock-keywords t nil nil backward-paragraph))
  5636. (kill-local-variable 'font-lock-keywords) nil))
  5637. (defun org-toggle-pretty-entities ()
  5638. "Toggle the composition display of entities as UTF8 characters."
  5639. (interactive)
  5640. (setq-local org-pretty-entities (not org-pretty-entities))
  5641. (org-restart-font-lock)
  5642. (if org-pretty-entities
  5643. (message "Entities are now displayed as UTF8 characters")
  5644. (save-restriction
  5645. (widen)
  5646. (org-decompose-region (point-min) (point-max))
  5647. (message "Entities are now displayed as plain text"))))
  5648. (defvar-local org-custom-properties-overlays nil
  5649. "List of overlays used for custom properties.")
  5650. (defun org-toggle-custom-properties-visibility ()
  5651. "Display or hide properties in `org-custom-properties'."
  5652. (interactive)
  5653. (if org-custom-properties-overlays
  5654. (progn (mapc #'delete-overlay org-custom-properties-overlays)
  5655. (setq org-custom-properties-overlays nil))
  5656. (when org-custom-properties
  5657. (org-with-wide-buffer
  5658. (goto-char (point-min))
  5659. (let ((regexp (org-re-property (regexp-opt org-custom-properties) t t)))
  5660. (while (re-search-forward regexp nil t)
  5661. (let ((end (cdr (save-match-data (org-get-property-block)))))
  5662. (when (and end (< (point) end))
  5663. ;; Hide first custom property in current drawer.
  5664. (let ((o (make-overlay (match-beginning 0) (1+ (match-end 0)))))
  5665. (overlay-put o 'invisible t)
  5666. (overlay-put o 'org-custom-property t)
  5667. (push o org-custom-properties-overlays))
  5668. ;; Hide additional custom properties in the same drawer.
  5669. (while (re-search-forward regexp end t)
  5670. (let ((o (make-overlay (match-beginning 0) (1+ (match-end 0)))))
  5671. (overlay-put o 'invisible t)
  5672. (overlay-put o 'org-custom-property t)
  5673. (push o org-custom-properties-overlays)))))
  5674. ;; Each entry is limited to a single property drawer.
  5675. (outline-next-heading)))))))
  5676. (defun org-fontify-entities (limit)
  5677. "Find an entity to fontify."
  5678. (let (ee)
  5679. (when org-pretty-entities
  5680. (catch 'match
  5681. ;; "\_ "-family is left out on purpose. Only the first one,
  5682. ;; i.e., "\_ ", could be fontified anyway, and it would be
  5683. ;; confusing when adding a second white space character.
  5684. (while (re-search-forward
  5685. "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]\n]\\)"
  5686. limit t)
  5687. (when (and (not (org-at-comment-p))
  5688. (setq ee (org-entity-get (match-string 1)))
  5689. (= (length (nth 6 ee)) 1))
  5690. (let* ((end (if (equal (match-string 2) "{}")
  5691. (match-end 2)
  5692. (match-end 1))))
  5693. (add-text-properties
  5694. (match-beginning 0) end
  5695. (list 'font-lock-fontified t))
  5696. (compose-region (match-beginning 0) end
  5697. (nth 6 ee) nil)
  5698. (backward-char 1)
  5699. (throw 'match t))))
  5700. nil))))
  5701. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  5702. "Fontify string S like in Org-mode."
  5703. (with-temp-buffer
  5704. (insert s)
  5705. (let ((org-odd-levels-only odd-levels))
  5706. (org-mode)
  5707. (font-lock-ensure)
  5708. (buffer-string))))
  5709. (defvar org-m nil)
  5710. (defvar org-l nil)
  5711. (defvar org-f nil)
  5712. (defun org-get-level-face (n)
  5713. "Get the right face for match N in font-lock matching of headlines."
  5714. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  5715. (when org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  5716. (if org-cycle-level-faces
  5717. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  5718. (setq org-f (nth (1- (min org-l org-n-level-faces)) org-level-faces)))
  5719. (cond
  5720. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  5721. ((eq n 2) org-f)
  5722. (t (unless org-level-color-stars-only org-f))))
  5723. (defun org-face-from-face-or-color (context inherit face-or-color)
  5724. "Create a face list that inherits INHERIT, but sets the foreground color.
  5725. When FACE-OR-COLOR is not a string, just return it."
  5726. (if (stringp face-or-color)
  5727. (list :inherit inherit
  5728. (cdr (assoc context org-faces-easy-properties))
  5729. face-or-color)
  5730. face-or-color))
  5731. (defun org-get-todo-face (kwd)
  5732. "Get the right face for a TODO keyword KWD.
  5733. If KWD is a number, get the corresponding match group."
  5734. (when (numberp kwd) (setq kwd (match-string kwd)))
  5735. (or (org-face-from-face-or-color
  5736. 'todo 'org-todo (cdr (assoc kwd org-todo-keyword-faces)))
  5737. (and (member kwd org-done-keywords) 'org-done)
  5738. 'org-todo))
  5739. (defun org-get-priority-face (priority)
  5740. "Get the right face for PRIORITY.
  5741. PRIORITY is a character."
  5742. (or (org-face-from-face-or-color
  5743. 'priority 'org-priority (cdr (assq priority org-priority-faces)))
  5744. 'org-priority))
  5745. (defun org-get-tag-face (tag)
  5746. "Get the right face for TAG.
  5747. If TAG is a number, get the corresponding match group."
  5748. (let ((tag (if (wholenump tag) (match-string tag) tag)))
  5749. (or (org-face-from-face-or-color
  5750. 'tag 'org-tag (cdr (assoc tag org-tag-faces)))
  5751. 'org-tag)))
  5752. (defun org-font-lock-add-priority-faces (limit)
  5753. "Add the special priority faces."
  5754. (while (re-search-forward "^\\*+ .*?\\(\\[#\\(.\\)\\]\\)" limit t)
  5755. (add-text-properties
  5756. (match-beginning 1) (match-end 1)
  5757. (list 'face (org-get-priority-face (string-to-char (match-string 2)))
  5758. 'font-lock-fontified t))))
  5759. (defun org-font-lock-add-tag-faces (limit)
  5760. "Add the special tag faces."
  5761. (when (and org-tag-faces org-tags-special-faces-re)
  5762. (while (re-search-forward org-tags-special-faces-re limit t)
  5763. (add-text-properties (match-beginning 1) (match-end 1)
  5764. (list 'face (org-get-tag-face 1)
  5765. 'font-lock-fontified t))
  5766. (backward-char 1))))
  5767. (defun org-unfontify-region (beg end &optional _maybe_loudly)
  5768. "Remove fontification and activation overlays from links."
  5769. (font-lock-default-unfontify-region beg end)
  5770. (let* ((buffer-undo-list t)
  5771. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  5772. (inhibit-modification-hooks t)
  5773. deactivate-mark buffer-file-name buffer-file-truename)
  5774. (org-decompose-region beg end)
  5775. (remove-text-properties beg end
  5776. '(mouse-face t keymap t org-linked-text t
  5777. invisible t intangible t
  5778. org-emphasis t))
  5779. (org-remove-font-lock-display-properties beg end)))
  5780. (defconst org-script-display '(((raise -0.3) (height 0.7))
  5781. ((raise 0.3) (height 0.7))
  5782. ((raise -0.5))
  5783. ((raise 0.5)))
  5784. "Display properties for showing superscripts and subscripts.")
  5785. (defun org-remove-font-lock-display-properties (beg end)
  5786. "Remove specific display properties that have been added by font lock.
  5787. The will remove the raise properties that are used to show superscripts
  5788. and subscripts."
  5789. (let (next prop)
  5790. (while (< beg end)
  5791. (setq next (next-single-property-change beg 'display nil end)
  5792. prop (get-text-property beg 'display))
  5793. (when (member prop org-script-display)
  5794. (put-text-property beg next 'display nil))
  5795. (setq beg next))))
  5796. (defun org-raise-scripts (limit)
  5797. "Add raise properties to sub/superscripts."
  5798. (when (and org-pretty-entities org-pretty-entities-include-sub-superscripts
  5799. (re-search-forward
  5800. (if (eq org-use-sub-superscripts t)
  5801. org-match-substring-regexp
  5802. org-match-substring-with-braces-regexp)
  5803. limit t))
  5804. (let* ((pos (point)) table-p comment-p
  5805. (mpos (match-beginning 3))
  5806. (emph-p (get-text-property mpos 'org-emphasis))
  5807. (link-p (get-text-property mpos 'mouse-face))
  5808. (keyw-p (eq 'org-special-keyword (get-text-property mpos 'face))))
  5809. (goto-char (point-at-bol))
  5810. (setq table-p (org-looking-at-p org-table-dataline-regexp)
  5811. comment-p (org-looking-at-p "^[ \t]*#[ +]"))
  5812. (goto-char pos)
  5813. ;; Handle a_b^c
  5814. (when (member (char-after) '(?_ ?^)) (goto-char (1- pos)))
  5815. (unless (or comment-p emph-p link-p keyw-p)
  5816. (put-text-property (match-beginning 3) (match-end 0)
  5817. 'display
  5818. (if (equal (char-after (match-beginning 2)) ?^)
  5819. (nth (if table-p 3 1) org-script-display)
  5820. (nth (if table-p 2 0) org-script-display)))
  5821. (add-text-properties (match-beginning 2) (match-end 2)
  5822. (list 'invisible t
  5823. 'org-dwidth t 'org-dwidth-n 1))
  5824. (if (and (eq (char-after (match-beginning 3)) ?{)
  5825. (eq (char-before (match-end 3)) ?}))
  5826. (progn
  5827. (add-text-properties
  5828. (match-beginning 3) (1+ (match-beginning 3))
  5829. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))
  5830. (add-text-properties
  5831. (1- (match-end 3)) (match-end 3)
  5832. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1)))))
  5833. t)))
  5834. ;;;; Visibility cycling, including org-goto and indirect buffer
  5835. ;;; Cycling
  5836. (defvar-local org-cycle-global-status nil)
  5837. (put 'org-cycle-global-status 'org-state t)
  5838. (defvar-local org-cycle-subtree-status nil)
  5839. (put 'org-cycle-subtree-status 'org-state t)
  5840. (defvar org-inlinetask-min-level)
  5841. (defun org-unlogged-message (&rest args)
  5842. "Display a message, but avoid logging it in the *Messages* buffer."
  5843. (let ((message-log-max nil))
  5844. (apply 'message args)))
  5845. ;;;###autoload
  5846. (defun org-cycle (&optional arg)
  5847. "TAB-action and visibility cycling for Org-mode.
  5848. This is the command invoked in Org-mode by the TAB key. Its main purpose
  5849. is outline visibility cycling, but it also invokes other actions
  5850. in special contexts.
  5851. - When this function is called with a prefix argument, rotate the entire
  5852. buffer through 3 states (global cycling)
  5853. 1. OVERVIEW: Show only top-level headlines.
  5854. 2. CONTENTS: Show all headlines of all levels, but no body text.
  5855. 3. SHOW ALL: Show everything.
  5856. With a double \\[universal-argument] prefix argument, \
  5857. switch to the startup visibility,
  5858. determined by the variable `org-startup-folded', and by any VISIBILITY
  5859. properties in the buffer.
  5860. With a triple \\[universal-argument] prefix argument, \
  5861. show the entire buffer, including any drawers.
  5862. - When inside a table, re-align the table and move to the next field.
  5863. - When point is at the beginning of a headline, rotate the subtree started
  5864. by this line through 3 different states (local cycling)
  5865. 1. FOLDED: Only the main headline is shown.
  5866. 2. CHILDREN: The main headline and the direct children are shown.
  5867. From this state, you can move to one of the children
  5868. and zoom in further.
  5869. 3. SUBTREE: Show the entire subtree, including body text.
  5870. If there is no subtree, switch directly from CHILDREN to FOLDED.
  5871. - When point is at the beginning of an empty headline and the variable
  5872. `org-cycle-level-after-item/entry-creation' is set, cycle the level
  5873. of the headline by demoting and promoting it to likely levels. This
  5874. speeds up creation document structure by pressing TAB once or several
  5875. times right after creating a new headline.
  5876. - When there is a numeric prefix, go up to a heading with level ARG, do
  5877. a `show-subtree' and return to the previous cursor position. If ARG
  5878. is negative, go up that many levels.
  5879. - When point is not at the beginning of a headline, execute the global
  5880. binding for TAB, which is re-indenting the line. See the option
  5881. `org-cycle-emulate-tab' for details.
  5882. - Special case: if point is at the beginning of the buffer and there is
  5883. no headline in line 1, this function will act as if called with prefix arg
  5884. (\\[universal-argument] TAB, same as S-TAB) also when called without prefix arg.
  5885. But only if also the variable `org-cycle-global-at-bob' is t."
  5886. (interactive "P")
  5887. (org-load-modules-maybe)
  5888. (unless (or (run-hook-with-args-until-success 'org-tab-first-hook)
  5889. (and org-cycle-level-after-item/entry-creation
  5890. (or (org-cycle-level)
  5891. (org-cycle-item-indentation))))
  5892. (let* ((limit-level
  5893. (or org-cycle-max-level
  5894. (and (boundp 'org-inlinetask-min-level)
  5895. org-inlinetask-min-level
  5896. (1- org-inlinetask-min-level))))
  5897. (nstars (and limit-level
  5898. (if org-odd-levels-only
  5899. (and limit-level (1- (* limit-level 2)))
  5900. limit-level)))
  5901. (org-outline-regexp
  5902. (if (not (derived-mode-p 'org-mode))
  5903. outline-regexp
  5904. (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ "))))
  5905. (bob-special (and org-cycle-global-at-bob (not arg) (bobp)
  5906. (not (looking-at org-outline-regexp))))
  5907. (org-cycle-hook
  5908. (if bob-special
  5909. (delq 'org-optimize-window-after-visibility-change
  5910. (copy-sequence org-cycle-hook))
  5911. org-cycle-hook))
  5912. (pos (point)))
  5913. (when (or bob-special (equal arg '(4)))
  5914. ;; special case: use global cycling
  5915. (setq arg t))
  5916. (cond
  5917. ((equal arg '(16))
  5918. (setq last-command 'dummy)
  5919. (org-set-startup-visibility)
  5920. (org-unlogged-message "Startup visibility, plus VISIBILITY properties"))
  5921. ((equal arg '(64))
  5922. (outline-show-all)
  5923. (org-unlogged-message "Entire buffer visible, including drawers"))
  5924. ;; Try cdlatex TAB completion
  5925. ((org-try-cdlatex-tab))
  5926. ;; Table: enter it or move to the next field.
  5927. ((org-at-table-p 'any)
  5928. (if (org-at-table.el-p)
  5929. (message "%s" (substitute-command-keys "\\<org-mode-map>\
  5930. Use \\[org-edit-special] to edit table.el tables"))
  5931. (if arg (org-table-edit-field t)
  5932. (org-table-justify-field-maybe)
  5933. (call-interactively 'org-table-next-field))))
  5934. ((run-hook-with-args-until-success
  5935. 'org-tab-after-check-for-table-hook))
  5936. ;; Global cycling: delegate to `org-cycle-internal-global'.
  5937. ((eq arg t) (org-cycle-internal-global))
  5938. ;; Drawers: delegate to `org-flag-drawer'.
  5939. ((save-excursion
  5940. (beginning-of-line 1)
  5941. (looking-at org-drawer-regexp))
  5942. (org-flag-drawer ; toggle block visibility
  5943. (not (get-char-property (match-end 0) 'invisible))))
  5944. ;; Show-subtree, ARG levels up from here.
  5945. ((integerp arg)
  5946. (save-excursion
  5947. (org-back-to-heading)
  5948. (outline-up-heading (if (< arg 0) (- arg)
  5949. (- (funcall outline-level) arg)))
  5950. (org-show-subtree)))
  5951. ;; Inline task: delegate to `org-inlinetask-toggle-visibility'.
  5952. ((and (featurep 'org-inlinetask)
  5953. (org-inlinetask-at-task-p)
  5954. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5955. (org-inlinetask-toggle-visibility))
  5956. ;; At an item/headline: delegate to `org-cycle-internal-local'.
  5957. ((and (or (and org-cycle-include-plain-lists (org-at-item-p))
  5958. (save-excursion (move-beginning-of-line 1)
  5959. (looking-at org-outline-regexp)))
  5960. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5961. (org-cycle-internal-local))
  5962. ;; From there: TAB emulation and template completion.
  5963. (buffer-read-only (org-back-to-heading))
  5964. ((run-hook-with-args-until-success
  5965. 'org-tab-after-check-for-cycling-hook))
  5966. ((org-try-structure-completion))
  5967. ((run-hook-with-args-until-success
  5968. 'org-tab-before-tab-emulation-hook))
  5969. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  5970. (or (not (bolp))
  5971. (not (looking-at org-outline-regexp))))
  5972. (call-interactively (global-key-binding "\t")))
  5973. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  5974. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  5975. (or (and (eq org-cycle-emulate-tab 'white)
  5976. (= (match-end 0) (point-at-eol)))
  5977. (and (eq org-cycle-emulate-tab 'whitestart)
  5978. (>= (match-end 0) pos))))
  5979. t
  5980. (eq org-cycle-emulate-tab t))
  5981. (call-interactively (global-key-binding "\t")))
  5982. (t (save-excursion
  5983. (org-back-to-heading)
  5984. (org-cycle)))))))
  5985. (defun org-cycle-internal-global ()
  5986. "Do the global cycling action."
  5987. ;; Hack to avoid display of messages for .org attachments in Gnus
  5988. (let ((ga (string-match "\\*fontification" (buffer-name))))
  5989. (cond
  5990. ((and (eq last-command this-command)
  5991. (eq org-cycle-global-status 'overview))
  5992. ;; We just created the overview - now do table of contents
  5993. ;; This can be slow in very large buffers, so indicate action
  5994. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  5995. (unless ga (org-unlogged-message "CONTENTS..."))
  5996. (org-content)
  5997. (unless ga (org-unlogged-message "CONTENTS...done"))
  5998. (setq org-cycle-global-status 'contents)
  5999. (run-hook-with-args 'org-cycle-hook 'contents))
  6000. ((and (eq last-command this-command)
  6001. (eq org-cycle-global-status 'contents))
  6002. ;; We just showed the table of contents - now show everything
  6003. (run-hook-with-args 'org-pre-cycle-hook 'all)
  6004. (outline-show-all)
  6005. (unless ga (org-unlogged-message "SHOW ALL"))
  6006. (setq org-cycle-global-status 'all)
  6007. (run-hook-with-args 'org-cycle-hook 'all))
  6008. (t
  6009. ;; Default action: go to overview
  6010. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  6011. (org-overview)
  6012. (unless ga (org-unlogged-message "OVERVIEW"))
  6013. (setq org-cycle-global-status 'overview)
  6014. (run-hook-with-args 'org-cycle-hook 'overview)))))
  6015. (defvar org-called-with-limited-levels nil
  6016. "Non-nil when `org-with-limited-levels' is currently active.")
  6017. (defun org-cycle-internal-local ()
  6018. "Do the local cycling action."
  6019. (let ((goal-column 0) eoh eol eos has-children children-skipped struct)
  6020. ;; First, determine end of headline (EOH), end of subtree or item
  6021. ;; (EOS), and if item or heading has children (HAS-CHILDREN).
  6022. (save-excursion
  6023. (if (org-at-item-p)
  6024. (progn
  6025. (beginning-of-line)
  6026. (setq struct (org-list-struct))
  6027. (setq eoh (point-at-eol))
  6028. (setq eos (org-list-get-item-end-before-blank (point) struct))
  6029. (setq has-children (org-list-has-child-p (point) struct)))
  6030. (org-back-to-heading)
  6031. (setq eoh (save-excursion (outline-end-of-heading) (point)))
  6032. (setq eos (save-excursion (org-end-of-subtree t t)
  6033. (when (bolp) (backward-char)) (point)))
  6034. (setq has-children
  6035. (or (save-excursion
  6036. (let ((level (funcall outline-level)))
  6037. (outline-next-heading)
  6038. (and (org-at-heading-p t)
  6039. (> (funcall outline-level) level))))
  6040. (save-excursion
  6041. (org-list-search-forward (org-item-beginning-re) eos t)))))
  6042. ;; Determine end invisible part of buffer (EOL)
  6043. (beginning-of-line 2)
  6044. ;; XEmacs doesn't have `next-single-char-property-change'
  6045. (if (featurep 'xemacs)
  6046. (while (and (not (eobp)) ;; this is like `next-line'
  6047. (get-char-property (1- (point)) 'invisible))
  6048. (beginning-of-line 2))
  6049. (while (and (not (eobp)) ;; this is like `next-line'
  6050. (get-char-property (1- (point)) 'invisible))
  6051. (goto-char (next-single-char-property-change (point) 'invisible))
  6052. (and (eolp) (beginning-of-line 2))))
  6053. (setq eol (point)))
  6054. ;; Find out what to do next and set `this-command'
  6055. (cond
  6056. ((= eos eoh)
  6057. ;; Nothing is hidden behind this heading
  6058. (unless (org-before-first-heading-p)
  6059. (run-hook-with-args 'org-pre-cycle-hook 'empty))
  6060. (org-unlogged-message "EMPTY ENTRY")
  6061. (setq org-cycle-subtree-status nil)
  6062. (save-excursion
  6063. (goto-char eos)
  6064. (outline-next-heading)
  6065. (when (outline-invisible-p) (org-flag-heading nil))))
  6066. ((and (or (>= eol eos)
  6067. (not (string-match "\\S-" (buffer-substring eol eos))))
  6068. (or has-children
  6069. (not (setq children-skipped
  6070. org-cycle-skip-children-state-if-no-children))))
  6071. ;; Entire subtree is hidden in one line: children view
  6072. (unless (org-before-first-heading-p)
  6073. (run-hook-with-args 'org-pre-cycle-hook 'children))
  6074. (if (org-at-item-p)
  6075. (org-list-set-item-visibility (point-at-bol) struct 'children)
  6076. (org-show-entry)
  6077. (org-with-limited-levels (org-show-children))
  6078. ;; FIXME: This slows down the func way too much.
  6079. ;; How keep drawers hidden in subtree anyway?
  6080. ;; (when (memq 'org-cycle-hide-drawers org-cycle-hook)
  6081. ;; (org-cycle-hide-drawers 'subtree))
  6082. ;; Fold every list in subtree to top-level items.
  6083. (when (eq org-cycle-include-plain-lists 'integrate)
  6084. (save-excursion
  6085. (org-back-to-heading)
  6086. (while (org-list-search-forward (org-item-beginning-re) eos t)
  6087. (beginning-of-line 1)
  6088. (let* ((struct (org-list-struct))
  6089. (prevs (org-list-prevs-alist struct))
  6090. (end (org-list-get-bottom-point struct)))
  6091. (dolist (e (org-list-get-all-items (point) struct prevs))
  6092. (org-list-set-item-visibility e struct 'folded))
  6093. (goto-char (if (< end eos) end eos)))))))
  6094. (org-unlogged-message "CHILDREN")
  6095. (save-excursion
  6096. (goto-char eos)
  6097. (outline-next-heading)
  6098. (when (outline-invisible-p) (org-flag-heading nil)))
  6099. (setq org-cycle-subtree-status 'children)
  6100. (unless (org-before-first-heading-p)
  6101. (run-hook-with-args 'org-cycle-hook 'children)))
  6102. ((or children-skipped
  6103. (and (eq last-command this-command)
  6104. (eq org-cycle-subtree-status 'children)))
  6105. ;; We just showed the children, or no children are there,
  6106. ;; now show everything.
  6107. (unless (org-before-first-heading-p)
  6108. (run-hook-with-args 'org-pre-cycle-hook 'subtree))
  6109. (outline-flag-region eoh eos nil)
  6110. (org-unlogged-message
  6111. (if children-skipped "SUBTREE (NO CHILDREN)" "SUBTREE"))
  6112. (setq org-cycle-subtree-status 'subtree)
  6113. (unless (org-before-first-heading-p)
  6114. (run-hook-with-args 'org-cycle-hook 'subtree)))
  6115. (t
  6116. ;; Default action: hide the subtree.
  6117. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  6118. (outline-flag-region eoh eos t)
  6119. (org-unlogged-message "FOLDED")
  6120. (setq org-cycle-subtree-status 'folded)
  6121. (unless (org-before-first-heading-p)
  6122. (run-hook-with-args 'org-cycle-hook 'folded))))))
  6123. ;;;###autoload
  6124. (defun org-global-cycle (&optional arg)
  6125. "Cycle the global visibility. For details see `org-cycle'.
  6126. With \\[universal-argument] prefix arg, switch to startup visibility.
  6127. With a numeric prefix, show all headlines up to that level."
  6128. (interactive "P")
  6129. (let ((org-cycle-include-plain-lists
  6130. (if (derived-mode-p 'org-mode) org-cycle-include-plain-lists nil)))
  6131. (cond
  6132. ((integerp arg)
  6133. (outline-show-all)
  6134. (outline-hide-sublevels arg)
  6135. (setq org-cycle-global-status 'contents))
  6136. ((equal arg '(4))
  6137. (org-set-startup-visibility)
  6138. (org-unlogged-message "Startup visibility, plus VISIBILITY properties."))
  6139. (t
  6140. (org-cycle '(4))))))
  6141. (defun org-set-startup-visibility ()
  6142. "Set the visibility required by startup options and properties."
  6143. (cond
  6144. ((eq org-startup-folded t)
  6145. (org-overview))
  6146. ((eq org-startup-folded 'content)
  6147. (org-content))
  6148. ((or (eq org-startup-folded 'showeverything)
  6149. (eq org-startup-folded nil))
  6150. (outline-show-all)))
  6151. (unless (eq org-startup-folded 'showeverything)
  6152. (when org-hide-block-startup (org-hide-block-all))
  6153. (org-set-visibility-according-to-property 'no-cleanup)
  6154. (org-cycle-hide-archived-subtrees 'all)
  6155. (org-cycle-hide-drawers 'all)
  6156. (org-cycle-show-empty-lines t)))
  6157. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  6158. "Switch subtree visibilities according to :VISIBILITY: property."
  6159. (interactive)
  6160. (org-with-wide-buffer
  6161. (goto-char (point-min))
  6162. (while (re-search-forward "^[ \t]*:VISIBILITY:" nil t)
  6163. (if (not (org-at-property-p)) (outline-next-heading)
  6164. (let ((state (match-string 3)))
  6165. (save-excursion
  6166. (org-back-to-heading t)
  6167. (outline-hide-subtree)
  6168. (org-reveal)
  6169. (cond
  6170. ((equal state "folded")
  6171. (outline-hide-subtree))
  6172. ((equal state "children")
  6173. (org-show-hidden-entry)
  6174. (org-show-children))
  6175. ((equal state "content")
  6176. (save-excursion
  6177. (save-restriction
  6178. (org-narrow-to-subtree)
  6179. (org-content))))
  6180. ((member state '("all" "showall"))
  6181. (outline-show-subtree)))))))
  6182. (unless no-cleanup
  6183. (org-cycle-hide-archived-subtrees 'all)
  6184. (org-cycle-hide-drawers 'all)
  6185. (org-cycle-show-empty-lines 'all))))
  6186. ;; This function uses outline-regexp instead of the more fundamental
  6187. ;; org-outline-regexp so that org-cycle-global works outside of Org
  6188. ;; buffers, where outline-regexp is needed.
  6189. (defun org-overview ()
  6190. "Switch to overview mode, showing only top-level headlines.
  6191. This shows all headlines with a level equal or greater than the level
  6192. of the first headline in the buffer. This is important, because if the
  6193. first headline is not level one, then (hide-sublevels 1) gives confusing
  6194. results."
  6195. (interactive)
  6196. (save-excursion
  6197. (let ((level
  6198. (save-excursion
  6199. (goto-char (point-min))
  6200. (when (re-search-forward (concat "^" outline-regexp) nil t)
  6201. (goto-char (match-beginning 0))
  6202. (funcall outline-level)))))
  6203. (and level (outline-hide-sublevels level)))))
  6204. (defun org-content (&optional arg)
  6205. "Show all headlines in the buffer, like a table of contents.
  6206. With numerical argument N, show content up to level N."
  6207. (interactive "P")
  6208. (org-overview)
  6209. (save-excursion
  6210. ;; Visit all headings and show their offspring
  6211. (and (integerp arg) (org-overview))
  6212. (goto-char (point-max))
  6213. (catch 'exit
  6214. (while (and (progn (condition-case nil
  6215. (outline-previous-visible-heading 1)
  6216. (error (goto-char (point-min))))
  6217. t)
  6218. (looking-at org-outline-regexp))
  6219. (if (integerp arg)
  6220. (org-show-children (1- arg))
  6221. (outline-show-branches))
  6222. (when (bobp) (throw 'exit nil))))))
  6223. (defun org-optimize-window-after-visibility-change (state)
  6224. "Adjust the window after a change in outline visibility.
  6225. This function is the default value of the hook `org-cycle-hook'."
  6226. (when (get-buffer-window (current-buffer))
  6227. (cond
  6228. ((eq state 'content) nil)
  6229. ((eq state 'all) nil)
  6230. ((eq state 'folded) nil)
  6231. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  6232. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  6233. (defun org-remove-empty-overlays-at (pos)
  6234. "Remove outline overlays that do not contain non-white stuff."
  6235. (dolist (o (overlays-at pos))
  6236. (and (eq 'outline (overlay-get o 'invisible))
  6237. (not (string-match "\\S-" (buffer-substring (overlay-start o)
  6238. (overlay-end o))))
  6239. (delete-overlay o))))
  6240. (defun org-clean-visibility-after-subtree-move ()
  6241. "Fix visibility issues after moving a subtree."
  6242. ;; First, find a reasonable region to look at:
  6243. ;; Start two siblings above, end three below
  6244. (let* ((beg (save-excursion
  6245. (and (org-get-last-sibling)
  6246. (org-get-last-sibling))
  6247. (point)))
  6248. (end (save-excursion
  6249. (and (org-get-next-sibling)
  6250. (org-get-next-sibling)
  6251. (org-get-next-sibling))
  6252. (if (org-at-heading-p)
  6253. (point-at-eol)
  6254. (point))))
  6255. (level (looking-at "\\*+"))
  6256. (re (when level (concat "^" (regexp-quote (match-string 0)) " "))))
  6257. (save-excursion
  6258. (save-restriction
  6259. (narrow-to-region beg end)
  6260. (when re
  6261. ;; Properly fold already folded siblings
  6262. (goto-char (point-min))
  6263. (while (re-search-forward re nil t)
  6264. (when (and (not (outline-invisible-p))
  6265. (save-excursion
  6266. (goto-char (point-at-eol)) (outline-invisible-p)))
  6267. (outline-hide-entry))))
  6268. (org-cycle-show-empty-lines 'overview)
  6269. (org-cycle-hide-drawers 'overview)))))
  6270. (defun org-cycle-show-empty-lines (state)
  6271. "Show empty lines above all visible headlines.
  6272. The region to be covered depends on STATE when called through
  6273. `org-cycle-hook'. Lisp program can use t for STATE to get the
  6274. entire buffer covered. Note that an empty line is only shown if there
  6275. are at least `org-cycle-separator-lines' empty lines before the headline."
  6276. (when (/= org-cycle-separator-lines 0)
  6277. (save-excursion
  6278. (let* ((n (abs org-cycle-separator-lines))
  6279. (re (cond
  6280. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  6281. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  6282. (t (let ((ns (number-to-string (- n 2))))
  6283. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  6284. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  6285. beg end)
  6286. (cond
  6287. ((memq state '(overview contents t))
  6288. (setq beg (point-min) end (point-max)))
  6289. ((memq state '(children folded))
  6290. (setq beg (point)
  6291. end (progn (org-end-of-subtree t t)
  6292. (line-beginning-position 2)))))
  6293. (when beg
  6294. (goto-char beg)
  6295. (while (re-search-forward re end t)
  6296. (unless (get-char-property (match-end 1) 'invisible)
  6297. (let ((e (match-end 1))
  6298. (b (if (>= org-cycle-separator-lines 0)
  6299. (match-beginning 1)
  6300. (save-excursion
  6301. (goto-char (match-beginning 0))
  6302. (skip-chars-backward " \t\n")
  6303. (line-end-position)))))
  6304. (outline-flag-region b e nil))))))))
  6305. ;; Never hide empty lines at the end of the file.
  6306. (save-excursion
  6307. (goto-char (point-max))
  6308. (outline-previous-heading)
  6309. (outline-end-of-heading)
  6310. (when (and (looking-at "[ \t\n]+")
  6311. (= (match-end 0) (point-max)))
  6312. (outline-flag-region (point) (match-end 0) nil))))
  6313. (defun org-show-empty-lines-in-parent ()
  6314. "Move to the parent and re-show empty lines before visible headlines."
  6315. (save-excursion
  6316. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  6317. (org-cycle-show-empty-lines context))))
  6318. (defun org-files-list ()
  6319. "Return `org-agenda-files' list, plus all open org-mode files.
  6320. This is useful for operations that need to scan all of a user's
  6321. open and agenda-wise Org files."
  6322. (let ((files (mapcar 'expand-file-name (org-agenda-files))))
  6323. (dolist (buf (buffer-list))
  6324. (with-current-buffer buf
  6325. (when (and (derived-mode-p 'org-mode) (buffer-file-name))
  6326. (cl-pushnew (expand-file-name (buffer-file-name)) files))))
  6327. files))
  6328. (defsubst org-entry-beginning-position ()
  6329. "Return the beginning position of the current entry."
  6330. (save-excursion (outline-back-to-heading t) (point)))
  6331. (defsubst org-entry-end-position ()
  6332. "Return the end position of the current entry."
  6333. (save-excursion (outline-next-heading) (point)))
  6334. (defun org-cycle-hide-drawers (state &optional exceptions)
  6335. "Re-hide all drawers after a visibility state change.
  6336. When non-nil, optional argument EXCEPTIONS is a list of strings
  6337. specifying which drawers should not be hidden."
  6338. (when (and (derived-mode-p 'org-mode)
  6339. (not (memq state '(overview folded contents))))
  6340. (save-excursion
  6341. (let* ((globalp (memq state '(contents all)))
  6342. (beg (if globalp (point-min) (point)))
  6343. (end (if globalp (point-max)
  6344. (if (eq state 'children)
  6345. (save-excursion (outline-next-heading) (point))
  6346. (org-end-of-subtree t)))))
  6347. (goto-char beg)
  6348. (while (re-search-forward org-drawer-regexp (max end (point)) t)
  6349. (unless (member-ignore-case (match-string 1) exceptions)
  6350. (let ((drawer (org-element-at-point)))
  6351. (when (memq (org-element-type drawer) '(drawer property-drawer))
  6352. (org-flag-drawer t drawer)
  6353. ;; Make sure to skip drawer entirely or we might flag
  6354. ;; it another time when matching its ending line with
  6355. ;; `org-drawer-regexp'.
  6356. (goto-char (org-element-property :end drawer))))))))))
  6357. (defun org-flag-drawer (flag &optional element)
  6358. "When FLAG is non-nil, hide the drawer we are at.
  6359. Otherwise make it visible. When optional argument ELEMENT is
  6360. a parsed drawer, as returned by `org-element-at-point', hide or
  6361. show that drawer instead."
  6362. (when (save-excursion
  6363. (beginning-of-line)
  6364. (org-looking-at-p org-drawer-regexp))
  6365. (let ((drawer (or element (org-element-at-point))))
  6366. (when (memq (org-element-type drawer) '(drawer property-drawer))
  6367. (let ((post (org-element-property :post-affiliated drawer)))
  6368. (save-excursion
  6369. (outline-flag-region
  6370. (progn (goto-char post) (line-end-position))
  6371. (progn (goto-char (org-element-property :end drawer))
  6372. (skip-chars-backward " \r\t\n")
  6373. (line-end-position))
  6374. flag))
  6375. ;; When the drawer is hidden away, make sure point lies in
  6376. ;; a visible part of the buffer.
  6377. (when (and flag (> (line-beginning-position) post))
  6378. (goto-char post)))))))
  6379. (defun org-subtree-end-visible-p ()
  6380. "Is the end of the current subtree visible?"
  6381. (pos-visible-in-window-p
  6382. (save-excursion (org-end-of-subtree t) (point))))
  6383. (defun org-first-headline-recenter ()
  6384. "Move cursor to the first headline and recenter the headline."
  6385. (goto-char (point-min))
  6386. (when (re-search-forward (concat "^\\(" org-outline-regexp "\\)") nil t)
  6387. (set-window-start (selected-window) (point-at-bol))))
  6388. ;;; Saving and restoring visibility
  6389. (defun org-outline-overlay-data (&optional use-markers)
  6390. "Return a list of the locations of all outline overlays.
  6391. These are overlays with the `invisible' property value `outline'.
  6392. The return value is a list of cons cells, with start and stop
  6393. positions for each overlay.
  6394. If USE-MARKERS is set, return the positions as markers."
  6395. (let (beg end)
  6396. (save-excursion
  6397. (save-restriction
  6398. (widen)
  6399. (delq nil
  6400. (mapcar (lambda (o)
  6401. (when (eq (overlay-get o 'invisible) 'outline)
  6402. (setq beg (overlay-start o)
  6403. end (overlay-end o))
  6404. (and beg end (> end beg)
  6405. (if use-markers
  6406. (cons (copy-marker beg)
  6407. (copy-marker end t))
  6408. (cons beg end)))))
  6409. (overlays-in (point-min) (point-max))))))))
  6410. (defun org-set-outline-overlay-data (data)
  6411. "Create visibility overlays for all positions in DATA.
  6412. DATA should have been made by `org-outline-overlay-data'."
  6413. (org-with-wide-buffer
  6414. (outline-show-all)
  6415. (dolist (c data) (outline-flag-region (car c) (cdr c) t))))
  6416. ;;; Folding of blocks
  6417. (defvar-local org-hide-block-overlays nil
  6418. "Overlays hiding blocks.")
  6419. (defun org-block-map (function &optional start end)
  6420. "Call FUNCTION at the head of all source blocks in the current buffer.
  6421. Optional arguments START and END can be used to limit the range."
  6422. (let ((start (or start (point-min)))
  6423. (end (or end (point-max))))
  6424. (save-excursion
  6425. (goto-char start)
  6426. (while (and (< (point) end) (re-search-forward org-block-regexp end t))
  6427. (save-excursion
  6428. (save-match-data
  6429. (goto-char (match-beginning 0))
  6430. (funcall function)))))))
  6431. (defun org-hide-block-toggle-all ()
  6432. "Toggle the visibility of all blocks in the current buffer."
  6433. (org-block-map 'org-hide-block-toggle))
  6434. (defun org-hide-block-all ()
  6435. "Fold all blocks in the current buffer."
  6436. (interactive)
  6437. (org-show-block-all)
  6438. (org-block-map 'org-hide-block-toggle-maybe))
  6439. (defun org-show-block-all ()
  6440. "Unfold all blocks in the current buffer."
  6441. (interactive)
  6442. (mapc #'delete-overlay org-hide-block-overlays)
  6443. (setq org-hide-block-overlays nil))
  6444. (defun org-hide-block-toggle-maybe ()
  6445. "Toggle visibility of block at point.
  6446. Unlike to `org-hide-block-toggle', this function does not throw
  6447. an error. Return a non-nil value when toggling is successful."
  6448. (interactive)
  6449. (ignore-errors (org-hide-block-toggle)))
  6450. (defun org-hide-block-toggle (&optional force)
  6451. "Toggle the visibility of the current block.
  6452. When optional argument FORCE is `off', make block visible. If it
  6453. is non-nil, hide it unconditionally. Throw an error when not at
  6454. a block. Return a non-nil value when toggling is successful."
  6455. (interactive)
  6456. (let ((element (org-element-at-point)))
  6457. (unless (memq (org-element-type element)
  6458. '(center-block comment-block dynamic-block example-block
  6459. export-block quote-block special-block
  6460. src-block verse-block))
  6461. (user-error "Not at a block"))
  6462. (let* ((start (save-excursion
  6463. (goto-char (org-element-property :post-affiliated element))
  6464. (line-end-position)))
  6465. (end (save-excursion
  6466. (goto-char (org-element-property :end element))
  6467. (skip-chars-backward " \r\t\n")
  6468. (line-end-position)))
  6469. (overlays (overlays-at start)))
  6470. (cond
  6471. ;; Do nothing when not before or at the block opening line or
  6472. ;; at the block closing line.
  6473. ((let ((eol (line-end-position))) (and (> eol start) (/= eol end))) nil)
  6474. ((and (not (eq force 'off))
  6475. (not (memq t (mapcar
  6476. (lambda (o)
  6477. (eq (overlay-get o 'invisible) 'org-hide-block))
  6478. overlays))))
  6479. (let ((ov (make-overlay start end)))
  6480. (overlay-put ov 'invisible 'org-hide-block)
  6481. ;; Make the block accessible to `isearch'.
  6482. (overlay-put
  6483. ov 'isearch-open-invisible
  6484. (lambda (ov)
  6485. (when (memq ov org-hide-block-overlays)
  6486. (setq org-hide-block-overlays (delq ov org-hide-block-overlays)))
  6487. (when (eq (overlay-get ov 'invisible) 'org-hide-block)
  6488. (delete-overlay ov))))
  6489. (push ov org-hide-block-overlays)
  6490. ;; When the block is hidden away, make sure point is left in
  6491. ;; a visible part of the buffer.
  6492. (when (> (line-beginning-position) start)
  6493. (goto-char start)
  6494. (beginning-of-line))
  6495. ;; Signal successful toggling.
  6496. t))
  6497. ((or (not force) (eq force 'off))
  6498. (dolist (ov overlays t)
  6499. (when (memq ov org-hide-block-overlays)
  6500. (setq org-hide-block-overlays (delq ov org-hide-block-overlays)))
  6501. (when (eq (overlay-get ov 'invisible) 'org-hide-block)
  6502. (delete-overlay ov))))))))
  6503. ;; org-tab-after-check-for-cycling-hook
  6504. (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
  6505. ;; Remove overlays when changing major mode
  6506. (add-hook 'org-mode-hook
  6507. (lambda () (org-add-hook 'change-major-mode-hook
  6508. 'org-show-block-all 'append 'local)))
  6509. ;;; Org-goto
  6510. (defvar org-goto-window-configuration nil)
  6511. (defvar org-goto-marker nil)
  6512. (defvar org-goto-map)
  6513. (defun org-goto-map ()
  6514. "Set the keymap `org-goto'."
  6515. (setq org-goto-map
  6516. (let ((map (make-sparse-keymap)))
  6517. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command
  6518. mouse-drag-region universal-argument org-occur)))
  6519. (dolist (cmd cmds)
  6520. (substitute-key-definition cmd cmd map global-map)))
  6521. (suppress-keymap map)
  6522. (org-defkey map "\C-m" 'org-goto-ret)
  6523. (org-defkey map [(return)] 'org-goto-ret)
  6524. (org-defkey map [(left)] 'org-goto-left)
  6525. (org-defkey map [(right)] 'org-goto-right)
  6526. (org-defkey map [(control ?g)] 'org-goto-quit)
  6527. (org-defkey map "\C-i" 'org-cycle)
  6528. (org-defkey map [(tab)] 'org-cycle)
  6529. (org-defkey map [(down)] 'outline-next-visible-heading)
  6530. (org-defkey map [(up)] 'outline-previous-visible-heading)
  6531. (if org-goto-auto-isearch
  6532. (if (fboundp 'define-key-after)
  6533. (define-key-after map [t] 'org-goto-local-auto-isearch)
  6534. nil)
  6535. (org-defkey map "q" 'org-goto-quit)
  6536. (org-defkey map "n" 'outline-next-visible-heading)
  6537. (org-defkey map "p" 'outline-previous-visible-heading)
  6538. (org-defkey map "f" 'outline-forward-same-level)
  6539. (org-defkey map "b" 'outline-backward-same-level)
  6540. (org-defkey map "u" 'outline-up-heading))
  6541. (org-defkey map "/" 'org-occur)
  6542. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  6543. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  6544. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  6545. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  6546. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  6547. map)))
  6548. (defconst org-goto-help
  6549. "Browse buffer copy, to find location or copy text.%s
  6550. RET=jump to location C-g=quit and return to previous location
  6551. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  6552. (defvar org-goto-start-pos) ; dynamically scoped parameter
  6553. (defun org-goto (&optional alternative-interface)
  6554. "Look up a different location in the current file, keeping current visibility.
  6555. When you want look-up or go to a different location in a
  6556. document, the fastest way is often to fold the entire buffer and
  6557. then dive into the tree. This method has the disadvantage, that
  6558. the previous location will be folded, which may not be what you
  6559. want.
  6560. This command works around this by showing a copy of the current
  6561. buffer in an indirect buffer, in overview mode. You can dive
  6562. into the tree in that copy, use org-occur and incremental search
  6563. to find a location. When pressing RET or `Q', the command
  6564. returns to the original buffer in which the visibility is still
  6565. unchanged. After RET it will also jump to the location selected
  6566. in the indirect buffer and expose the headline hierarchy above.
  6567. With a prefix argument, use the alternative interface: e.g., if
  6568. `org-goto-interface' is `outline' use `outline-path-completion'."
  6569. (interactive "P")
  6570. (org-goto-map)
  6571. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  6572. (org-refile-use-outline-path t)
  6573. (org-refile-target-verify-function nil)
  6574. (interface
  6575. (if (not alternative-interface)
  6576. org-goto-interface
  6577. (if (eq org-goto-interface 'outline)
  6578. 'outline-path-completion
  6579. 'outline)))
  6580. (org-goto-start-pos (point))
  6581. (selected-point
  6582. (if (eq interface 'outline)
  6583. (car (org-get-location (current-buffer) org-goto-help))
  6584. (let ((pa (org-refile-get-location "Goto" nil nil t)))
  6585. (org-refile-check-position pa)
  6586. (nth 3 pa)))))
  6587. (if selected-point
  6588. (progn
  6589. (org-mark-ring-push org-goto-start-pos)
  6590. (goto-char selected-point)
  6591. (when (or (outline-invisible-p) (org-invisible-p2))
  6592. (org-show-context 'org-goto)))
  6593. (message "Quit"))))
  6594. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  6595. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  6596. (defvar org-goto-local-auto-isearch-map) ; defined below
  6597. (defun org-get-location (_buf help)
  6598. "Let the user select a location in current buffer.
  6599. This function uses a recursive edit. It returns the selected position
  6600. or nil."
  6601. (org-no-popups
  6602. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  6603. (isearch-hide-immediately nil)
  6604. (isearch-search-fun-function
  6605. (lambda () 'org-goto-local-search-headings))
  6606. (org-goto-selected-point org-goto-exit-command))
  6607. (save-excursion
  6608. (save-window-excursion
  6609. (delete-other-windows)
  6610. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  6611. (org-pop-to-buffer-same-window
  6612. (condition-case nil
  6613. (make-indirect-buffer (current-buffer) "*org-goto*")
  6614. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  6615. (with-output-to-temp-buffer "*Org Help*"
  6616. (princ (format help (if org-goto-auto-isearch
  6617. " Just type for auto-isearch."
  6618. " n/p/f/b/u to navigate, q to quit."))))
  6619. (org-fit-window-to-buffer (get-buffer-window "*Org Help*"))
  6620. (setq buffer-read-only nil)
  6621. (let ((org-startup-truncated t)
  6622. (org-startup-folded nil)
  6623. (org-startup-align-all-tables nil))
  6624. (org-mode)
  6625. (org-overview))
  6626. (setq buffer-read-only t)
  6627. (if (and (boundp 'org-goto-start-pos)
  6628. (integer-or-marker-p org-goto-start-pos))
  6629. (progn (goto-char org-goto-start-pos)
  6630. (when (outline-invisible-p)
  6631. (org-show-set-visibility 'lineage)))
  6632. (goto-char (point-min)))
  6633. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  6634. (message "Select location and press RET")
  6635. (use-local-map org-goto-map)
  6636. (recursive-edit)))
  6637. (kill-buffer "*org-goto*")
  6638. (cons org-goto-selected-point org-goto-exit-command))))
  6639. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  6640. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  6641. ;; `isearch-other-control-char' was removed in Emacs 24.4.
  6642. (if (fboundp 'isearch-other-control-char)
  6643. (progn
  6644. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  6645. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char))
  6646. (define-key org-goto-local-auto-isearch-map "\C-i" nil)
  6647. (define-key org-goto-local-auto-isearch-map "\C-m" nil)
  6648. (define-key org-goto-local-auto-isearch-map [return] nil))
  6649. (defun org-goto-local-search-headings (string bound noerror)
  6650. "Search and make sure that any matches are in headlines."
  6651. (catch 'return
  6652. (while (if isearch-forward
  6653. (search-forward string bound noerror)
  6654. (search-backward string bound noerror))
  6655. (when (save-match-data
  6656. (and (save-excursion
  6657. (beginning-of-line)
  6658. (looking-at org-complex-heading-regexp))
  6659. (or (not (match-beginning 5))
  6660. (< (point) (match-beginning 5)))))
  6661. (throw 'return (point))))))
  6662. (defun org-goto-local-auto-isearch ()
  6663. "Start isearch."
  6664. (interactive)
  6665. (goto-char (point-min))
  6666. (let ((keys (this-command-keys)))
  6667. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  6668. (isearch-mode t)
  6669. (isearch-process-search-char (string-to-char keys)))))
  6670. (defun org-goto-ret (&optional _arg)
  6671. "Finish `org-goto' by going to the new location."
  6672. (interactive "P")
  6673. (setq org-goto-selected-point (point))
  6674. (setq org-goto-exit-command 'return)
  6675. (throw 'exit nil))
  6676. (defun org-goto-left ()
  6677. "Finish `org-goto' by going to the new location."
  6678. (interactive)
  6679. (if (org-at-heading-p)
  6680. (progn
  6681. (beginning-of-line 1)
  6682. (setq org-goto-selected-point (point)
  6683. org-goto-exit-command 'left)
  6684. (throw 'exit nil))
  6685. (user-error "Not on a heading")))
  6686. (defun org-goto-right ()
  6687. "Finish `org-goto' by going to the new location."
  6688. (interactive)
  6689. (if (org-at-heading-p)
  6690. (progn
  6691. (setq org-goto-selected-point (point)
  6692. org-goto-exit-command 'right)
  6693. (throw 'exit nil))
  6694. (user-error "Not on a heading")))
  6695. (defun org-goto-quit ()
  6696. "Finish `org-goto' without cursor motion."
  6697. (interactive)
  6698. (setq org-goto-selected-point nil)
  6699. (setq org-goto-exit-command 'quit)
  6700. (throw 'exit nil))
  6701. ;;; Indirect buffer display of subtrees
  6702. (defvar org-indirect-dedicated-frame nil
  6703. "This is the frame being used for indirect tree display.")
  6704. (defvar org-last-indirect-buffer nil)
  6705. (defun org-tree-to-indirect-buffer (&optional arg)
  6706. "Create indirect buffer and narrow it to current subtree.
  6707. With a numerical prefix ARG, go up to this level and then take that tree.
  6708. If ARG is negative, go up that many levels.
  6709. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  6710. indirect buffer previously made with this command, to avoid proliferation of
  6711. indirect buffers. However, when you call the command with a \
  6712. \\[universal-argument] prefix, or
  6713. when `org-indirect-buffer-display' is `new-frame', the last buffer
  6714. is kept so that you can work with several indirect buffers at the same time.
  6715. If `org-indirect-buffer-display' is `dedicated-frame', the \
  6716. \\[universal-argument] prefix also
  6717. requests that a new frame be made for the new buffer, so that the dedicated
  6718. frame is not changed."
  6719. (interactive "P")
  6720. (let ((cbuf (current-buffer))
  6721. (cwin (selected-window))
  6722. (pos (point))
  6723. beg end level heading ibuf)
  6724. (save-excursion
  6725. (org-back-to-heading t)
  6726. (when (numberp arg)
  6727. (setq level (org-outline-level))
  6728. (when (< arg 0) (setq arg (+ level arg)))
  6729. (while (> (setq level (org-outline-level)) arg)
  6730. (org-up-heading-safe)))
  6731. (setq beg (point)
  6732. heading (org-get-heading 'no-tags))
  6733. (org-end-of-subtree t t)
  6734. (when (org-at-heading-p) (backward-char 1))
  6735. (setq end (point)))
  6736. (when (and (buffer-live-p org-last-indirect-buffer)
  6737. (not (eq org-indirect-buffer-display 'new-frame))
  6738. (not arg))
  6739. (kill-buffer org-last-indirect-buffer))
  6740. (setq ibuf (org-get-indirect-buffer cbuf heading)
  6741. org-last-indirect-buffer ibuf)
  6742. (cond
  6743. ((or (eq org-indirect-buffer-display 'new-frame)
  6744. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  6745. (select-frame (make-frame))
  6746. (delete-other-windows)
  6747. (org-pop-to-buffer-same-window ibuf)
  6748. (org-set-frame-title heading))
  6749. ((eq org-indirect-buffer-display 'dedicated-frame)
  6750. (raise-frame
  6751. (select-frame (or (and org-indirect-dedicated-frame
  6752. (frame-live-p org-indirect-dedicated-frame)
  6753. org-indirect-dedicated-frame)
  6754. (setq org-indirect-dedicated-frame (make-frame)))))
  6755. (delete-other-windows)
  6756. (org-pop-to-buffer-same-window ibuf)
  6757. (org-set-frame-title (concat "Indirect: " heading)))
  6758. ((eq org-indirect-buffer-display 'current-window)
  6759. (org-pop-to-buffer-same-window ibuf))
  6760. ((eq org-indirect-buffer-display 'other-window)
  6761. (pop-to-buffer ibuf))
  6762. (t (error "Invalid value")))
  6763. (when (featurep 'xemacs)
  6764. (save-excursion (org-mode) (turn-on-font-lock)))
  6765. (narrow-to-region beg end)
  6766. (outline-show-all)
  6767. (goto-char pos)
  6768. (run-hook-with-args 'org-cycle-hook 'all)
  6769. (and (window-live-p cwin) (select-window cwin))))
  6770. (defun org-get-indirect-buffer (&optional buffer heading)
  6771. (setq buffer (or buffer (current-buffer)))
  6772. (let ((n 1) (base (buffer-name buffer)) bname)
  6773. (while (buffer-live-p
  6774. (get-buffer
  6775. (setq bname
  6776. (concat base "-"
  6777. (if heading (concat heading "-" (number-to-string n))
  6778. (number-to-string n))))))
  6779. (setq n (1+ n)))
  6780. (condition-case nil
  6781. (make-indirect-buffer buffer bname 'clone)
  6782. (error (make-indirect-buffer buffer bname)))))
  6783. (defun org-set-frame-title (title)
  6784. "Set the title of the current frame to the string TITLE."
  6785. ;; FIXME: how to name a single frame in XEmacs???
  6786. (unless (featurep 'xemacs)
  6787. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  6788. ;;;; Structure editing
  6789. ;;; Inserting headlines
  6790. (defun org--line-empty-p (n)
  6791. "Is the Nth next line empty?
  6792. Counts the current line as N = 1 and the previous line as N = 0;
  6793. see `beginning-of-line'."
  6794. (save-excursion
  6795. (and (not (bobp))
  6796. (or (beginning-of-line n) t)
  6797. (save-match-data
  6798. (looking-at "[ \t]*$")))))
  6799. (defun org-previous-line-empty-p ()
  6800. "Is the previous line a blank line?
  6801. When NEXT is non-nil, check the next line instead."
  6802. (org--line-empty-p 0))
  6803. (defun org-next-line-empty-p ()
  6804. "Is the previous line a blank line?
  6805. When NEXT is non-nil, check the next line instead."
  6806. (org--line-empty-p 2))
  6807. (defun org-insert-heading (&optional arg invisible-ok top)
  6808. "Insert a new heading or an item with the same depth at point.
  6809. If point is at the beginning of a heading or a list item, insert
  6810. a new heading or a new item above the current one. If point is
  6811. at the beginning of a normal line, turn the line into a heading.
  6812. If point is in the middle of a headline or a list item, split the
  6813. headline or the item and create a new headline/item with the text
  6814. in the current line after point \(see `org-M-RET-may-split-line'
  6815. on how to modify this behavior).
  6816. With one universal prefix argument, set the user option
  6817. `org-insert-heading-respect-content' to t for the duration of
  6818. the command. This modifies the behavior described above in this
  6819. ways: on list items and at the beginning of normal lines, force
  6820. the insertion of a heading after the current subtree.
  6821. With two universal prefix arguments, insert the heading at the
  6822. end of the grandparent subtree. For example, if point is within
  6823. a 2nd-level heading, then it will insert a 2nd-level heading at
  6824. the end of the 1st-level parent heading.
  6825. If point is at the beginning of a headline, insert a sibling
  6826. before the current headline. If point is not at the beginning,
  6827. split the line and create a new headline with the text in the
  6828. current line after point \(see `org-M-RET-may-split-line' on how
  6829. to modify this behavior).
  6830. If point is at the beginning of a normal line, turn this line
  6831. into a heading.
  6832. When INVISIBLE-OK is set, stop at invisible headlines when going
  6833. back. This is important for non-interactive uses of the
  6834. command.
  6835. When optional argument TOP is non-nil, insert a level 1 heading,
  6836. unconditionally."
  6837. (interactive "P")
  6838. (when (org-called-interactively-p 'any) (org-reveal))
  6839. (let ((itemp (and (not top) (org-in-item-p)))
  6840. (may-split (org-get-alist-option org-M-RET-may-split-line 'headline))
  6841. (respect-content (or org-insert-heading-respect-content
  6842. (equal arg '(4))))
  6843. (initial-content ""))
  6844. (cond
  6845. ((or (= (buffer-size) 0)
  6846. (and (not (save-excursion
  6847. (and (ignore-errors (org-back-to-heading invisible-ok))
  6848. (org-at-heading-p))))
  6849. (or arg (not itemp))))
  6850. ;; At beginning of buffer or so high up that only a heading
  6851. ;; makes sense.
  6852. (cond ((and (bolp) (not respect-content)) (insert "* "))
  6853. ((not respect-content)
  6854. (unless may-split (end-of-line))
  6855. (insert "\n* "))
  6856. ((re-search-forward org-outline-regexp-bol nil t)
  6857. (beginning-of-line)
  6858. (insert "* \n")
  6859. (backward-char))
  6860. (t (goto-char (point-max))
  6861. (insert "\n* ")))
  6862. (run-hooks 'org-insert-heading-hook))
  6863. ((and itemp (not (member arg '((4) (16)))) (org-insert-item)))
  6864. (t
  6865. ;; Maybe move at the end of the subtree
  6866. (when (equal arg '(16))
  6867. (org-up-heading-safe)
  6868. (org-end-of-subtree t))
  6869. ;; Insert a heading
  6870. (save-restriction
  6871. (widen)
  6872. (let* ((level nil)
  6873. (on-heading (org-at-heading-p))
  6874. (empty-line-p (if on-heading
  6875. (org-previous-line-empty-p)
  6876. ;; We will decide later
  6877. nil))
  6878. ;; Get a level string to fall back on.
  6879. (fix-level
  6880. (if (org-before-first-heading-p) "*"
  6881. (save-excursion
  6882. (org-back-to-heading t)
  6883. (when (org-previous-line-empty-p) (setq empty-line-p t))
  6884. (looking-at org-outline-regexp)
  6885. (make-string (1- (length (match-string 0))) ?*))))
  6886. (stars
  6887. (save-excursion
  6888. (condition-case nil
  6889. (if top "* "
  6890. (org-back-to-heading invisible-ok)
  6891. (when (and (not on-heading)
  6892. (featurep 'org-inlinetask)
  6893. (integerp org-inlinetask-min-level)
  6894. (>= (length (match-string 0))
  6895. org-inlinetask-min-level))
  6896. ;; Find a heading level before the inline
  6897. ;; task.
  6898. (while (and (setq level (org-up-heading-safe))
  6899. (>= level org-inlinetask-min-level)))
  6900. (if (org-at-heading-p)
  6901. (org-back-to-heading invisible-ok)
  6902. (error "This should not happen")))
  6903. (unless (and (save-excursion
  6904. (save-match-data
  6905. (org-backward-heading-same-level
  6906. 1 invisible-ok))
  6907. (= (point) (match-beginning 0)))
  6908. (not (org-next-line-empty-p)))
  6909. (setq empty-line-p (or empty-line-p
  6910. (org-previous-line-empty-p))))
  6911. (match-string 0))
  6912. (error (or fix-level "* ")))))
  6913. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  6914. (blank (if (eq blank-a 'auto) empty-line-p blank-a)))
  6915. ;; If we insert after content, move there and clean up
  6916. ;; whitespace.
  6917. (when (and respect-content
  6918. (not (org-looking-at-p org-outline-regexp-bol)))
  6919. (if (not (org-before-first-heading-p))
  6920. (org-end-of-subtree nil t)
  6921. (re-search-forward org-outline-regexp-bol)
  6922. (beginning-of-line 0))
  6923. (skip-chars-backward " \r\t\n")
  6924. (and (not (org-looking-back "^\\*+" (line-beginning-position)))
  6925. (looking-at "[ \t]+") (replace-match ""))
  6926. (unless (eobp) (forward-char 1))
  6927. (when (looking-at "^\\*")
  6928. (unless (bobp) (backward-char 1))
  6929. (insert "\n")))
  6930. ;; If we are splitting, grab the text that should be moved
  6931. ;; to the new headline.
  6932. (when may-split
  6933. (if (org-at-heading-p)
  6934. ;; This is a heading: split intelligently (keeping
  6935. ;; tags).
  6936. (let ((pos (point)))
  6937. (beginning-of-line)
  6938. (unless (looking-at org-complex-heading-regexp)
  6939. (error "This should not happen"))
  6940. (when (and (match-beginning 4)
  6941. (> pos (match-beginning 4))
  6942. (< pos (match-end 4)))
  6943. (setq initial-content (buffer-substring pos (match-end 4)))
  6944. (goto-char pos)
  6945. (delete-region (point) (match-end 4))
  6946. (if (looking-at "[ \t]*$")
  6947. (replace-match "")
  6948. (insert (make-string (length initial-content) ?\s)))
  6949. (setq initial-content (org-trim initial-content)))
  6950. (goto-char pos))
  6951. ;; A normal line.
  6952. (setq initial-content
  6953. (org-trim
  6954. (delete-and-extract-region (point) (line-end-position))))))
  6955. ;; If we are at the beginning of the line, insert before it.
  6956. ;; Otherwise, after it.
  6957. (cond
  6958. ((and (bolp) (looking-at "[ \t]*$")))
  6959. ((bolp) (save-excursion (insert "\n")))
  6960. (t (end-of-line)
  6961. (insert "\n")))
  6962. ;; Insert the new heading
  6963. (insert stars)
  6964. (just-one-space)
  6965. (insert initial-content)
  6966. (unless (and blank (org-previous-line-empty-p))
  6967. (org-N-empty-lines-before-current (if blank 1 0)))
  6968. ;; Adjust visibility, which may be messed up if we removed
  6969. ;; blank lines while previous entry was hidden.
  6970. (let ((bol (line-beginning-position)))
  6971. (dolist (o (overlays-at (1- bol)))
  6972. (when (and (eq (overlay-get o 'invisible) 'outline)
  6973. (eq (overlay-end o) bol))
  6974. (move-overlay o (overlay-start o) (1- bol)))))
  6975. (run-hooks 'org-insert-heading-hook)))))))
  6976. (defun org-N-empty-lines-before-current (N)
  6977. "Make the number of empty lines before current exactly N.
  6978. So this will delete or add empty lines."
  6979. (save-excursion
  6980. (beginning-of-line)
  6981. (let ((p (point)))
  6982. (skip-chars-backward " \r\t\n")
  6983. (unless (bolp) (forward-line))
  6984. (delete-region (point) p))
  6985. (when (> N 0) (insert (make-string N ?\n)))))
  6986. (defun org-get-heading (&optional no-tags no-todo)
  6987. "Return the heading of the current entry, without the stars.
  6988. When NO-TAGS is non-nil, don't include tags.
  6989. When NO-TODO is non-nil, don't include TODO keywords."
  6990. (save-excursion
  6991. (org-back-to-heading t)
  6992. (cond
  6993. ((and no-tags no-todo)
  6994. (looking-at org-complex-heading-regexp)
  6995. (match-string 4))
  6996. (no-tags
  6997. (looking-at (concat org-outline-regexp
  6998. "\\(.*?\\)"
  6999. "\\(?:[ \t]+:[[:alnum:]:_@#%]+:\\)?[ \t]*$"))
  7000. (match-string 1))
  7001. (no-todo
  7002. (looking-at org-todo-line-regexp)
  7003. (match-string 3))
  7004. (t (looking-at org-heading-regexp)
  7005. (match-string 2)))))
  7006. (defvar orgstruct-mode) ; defined below
  7007. (defun org-heading-components ()
  7008. "Return the components of the current heading.
  7009. This is a list with the following elements:
  7010. - the level as an integer
  7011. - the reduced level, different if `org-odd-levels-only' is set.
  7012. - the TODO keyword, or nil
  7013. - the priority character, like ?A, or nil if no priority is given
  7014. - the headline text itself, or the tags string if no headline text
  7015. - the tags string, or nil."
  7016. (save-excursion
  7017. (org-back-to-heading t)
  7018. (when (let (case-fold-search)
  7019. (looking-at
  7020. (if orgstruct-mode
  7021. org-heading-regexp
  7022. org-complex-heading-regexp)))
  7023. (if orgstruct-mode
  7024. (list (length (match-string 1))
  7025. (org-reduced-level (length (match-string 1)))
  7026. nil
  7027. nil
  7028. (match-string 2)
  7029. nil)
  7030. (list (length (match-string 1))
  7031. (org-reduced-level (length (match-string 1)))
  7032. (org-match-string-no-properties 2)
  7033. (and (match-end 3) (aref (match-string 3) 2))
  7034. (org-match-string-no-properties 4)
  7035. (org-match-string-no-properties 5))))))
  7036. (defun org-get-entry ()
  7037. "Get the entry text, after heading, entire subtree."
  7038. (save-excursion
  7039. (org-back-to-heading t)
  7040. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  7041. (defun org-insert-heading-after-current ()
  7042. "Insert a new heading with same level as current, after current subtree."
  7043. (interactive)
  7044. (org-back-to-heading)
  7045. (org-insert-heading)
  7046. (org-move-subtree-down)
  7047. (end-of-line 1))
  7048. (defun org-insert-heading-respect-content (&optional invisible-ok)
  7049. "Insert heading with `org-insert-heading-respect-content' set to t."
  7050. (interactive)
  7051. (org-insert-heading '(4) invisible-ok))
  7052. (defun org-insert-todo-heading-respect-content (&optional force-state)
  7053. "Insert TODO heading with `org-insert-heading-respect-content' set to t."
  7054. (interactive)
  7055. (org-insert-todo-heading force-state '(4)))
  7056. (defun org-insert-todo-heading (arg &optional force-heading)
  7057. "Insert a new heading with the same level and TODO state as current heading.
  7058. If the heading has no TODO state, or if the state is DONE, use the first
  7059. state (TODO by default). Also with one prefix arg, force first state. With
  7060. two prefix args, force inserting at the end of the parent subtree."
  7061. (interactive "P")
  7062. (when (or force-heading (not (org-insert-item 'checkbox)))
  7063. (org-insert-heading (or (and (equal arg '(16)) '(16))
  7064. force-heading))
  7065. (save-excursion
  7066. (org-back-to-heading)
  7067. (outline-previous-heading)
  7068. (looking-at org-todo-line-regexp))
  7069. (let*
  7070. ((new-mark-x
  7071. (if (or (equal arg '(4))
  7072. (not (match-beginning 2))
  7073. (member (match-string 2) org-done-keywords))
  7074. (car org-todo-keywords-1)
  7075. (match-string 2)))
  7076. (new-mark
  7077. (or
  7078. (run-hook-with-args-until-success
  7079. 'org-todo-get-default-hook new-mark-x nil)
  7080. new-mark-x)))
  7081. (beginning-of-line 1)
  7082. (and (looking-at org-outline-regexp) (goto-char (match-end 0))
  7083. (if org-treat-insert-todo-heading-as-state-change
  7084. (org-todo new-mark)
  7085. (insert new-mark " "))))
  7086. (when org-provide-todo-statistics
  7087. (org-update-parent-todo-statistics))))
  7088. (defun org-insert-subheading (arg)
  7089. "Insert a new subheading and demote it.
  7090. Works for outline headings and for plain lists alike."
  7091. (interactive "P")
  7092. (org-insert-heading arg)
  7093. (cond
  7094. ((org-at-heading-p) (org-do-demote))
  7095. ((org-at-item-p) (org-indent-item))))
  7096. (defun org-insert-todo-subheading (arg)
  7097. "Insert a new subheading with TODO keyword or checkbox and demote it.
  7098. Works for outline headings and for plain lists alike."
  7099. (interactive "P")
  7100. (org-insert-todo-heading arg)
  7101. (cond
  7102. ((org-at-heading-p) (org-do-demote))
  7103. ((org-at-item-p) (org-indent-item))))
  7104. ;;; Promotion and Demotion
  7105. (defvar org-after-demote-entry-hook nil
  7106. "Hook run after an entry has been demoted.
  7107. The cursor will be at the beginning of the entry.
  7108. When a subtree is being demoted, the hook will be called for each node.")
  7109. (defvar org-after-promote-entry-hook nil
  7110. "Hook run after an entry has been promoted.
  7111. The cursor will be at the beginning of the entry.
  7112. When a subtree is being promoted, the hook will be called for each node.")
  7113. (defun org-promote-subtree ()
  7114. "Promote the entire subtree.
  7115. See also `org-promote'."
  7116. (interactive)
  7117. (save-excursion
  7118. (org-with-limited-levels (org-map-tree 'org-promote)))
  7119. (org-fix-position-after-promote))
  7120. (defun org-demote-subtree ()
  7121. "Demote the entire subtree.
  7122. See `org-demote' and `org-promote'."
  7123. (interactive)
  7124. (save-excursion
  7125. (org-with-limited-levels (org-map-tree 'org-demote)))
  7126. (org-fix-position-after-promote))
  7127. (defun org-do-promote ()
  7128. "Promote the current heading higher up the tree.
  7129. If the region is active in `transient-mark-mode', promote all
  7130. headings in the region."
  7131. (interactive)
  7132. (save-excursion
  7133. (if (org-region-active-p)
  7134. (org-map-region 'org-promote (region-beginning) (region-end))
  7135. (org-promote)))
  7136. (org-fix-position-after-promote))
  7137. (defun org-do-demote ()
  7138. "Demote the current heading lower down the tree.
  7139. If the region is active in `transient-mark-mode', demote all
  7140. headings in the region."
  7141. (interactive)
  7142. (save-excursion
  7143. (if (org-region-active-p)
  7144. (org-map-region 'org-demote (region-beginning) (region-end))
  7145. (org-demote)))
  7146. (org-fix-position-after-promote))
  7147. (defun org-fix-position-after-promote ()
  7148. "Fix cursor position and indentation after demoting/promoting."
  7149. (let ((pos (point)))
  7150. (when (save-excursion
  7151. (beginning-of-line 1)
  7152. (looking-at org-todo-line-regexp)
  7153. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  7154. (cond ((eobp) (insert " "))
  7155. ((eolp) (insert " "))
  7156. ((equal (char-after) ?\ ) (forward-char 1))))))
  7157. (defun org-current-level ()
  7158. "Return the level of the current entry, or nil if before the first headline.
  7159. The level is the number of stars at the beginning of the
  7160. headline. Use `org-reduced-level' to remove the effect of
  7161. `org-odd-levels'. Unlike to `org-outline-level', this function
  7162. ignores inlinetasks."
  7163. (let ((level (org-with-limited-levels (org-outline-level))))
  7164. (and (> level 0) level)))
  7165. (defun org-get-previous-line-level ()
  7166. "Return the outline depth of the last headline before the current line.
  7167. Returns 0 for the first headline in the buffer, and nil if before the
  7168. first headline."
  7169. (and (org-current-level)
  7170. (or (and (/= (line-beginning-position) (point-min))
  7171. (save-excursion (beginning-of-line 0) (org-current-level)))
  7172. 0)))
  7173. (defun org-reduced-level (l)
  7174. "Compute the effective level of a heading.
  7175. This takes into account the setting of `org-odd-levels-only'."
  7176. (cond
  7177. ((zerop l) 0)
  7178. (org-odd-levels-only (1+ (floor (/ l 2))))
  7179. (t l)))
  7180. (defun org-level-increment ()
  7181. "Return the number of stars that will be added or removed at a
  7182. time to headlines when structure editing, based on the value of
  7183. `org-odd-levels-only'."
  7184. (if org-odd-levels-only 2 1))
  7185. (defun org-get-valid-level (level &optional change)
  7186. "Rectify a level change under the influence of `org-odd-levels-only'
  7187. LEVEL is a current level, CHANGE is by how much the level should be
  7188. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  7189. even level numbers will become the next higher odd number."
  7190. (if org-odd-levels-only
  7191. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  7192. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  7193. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  7194. (max 1 (+ level (or change 0)))))
  7195. (define-obsolete-function-alias 'org-get-legal-level 'org-get-valid-level "23.1")
  7196. (defun org-promote ()
  7197. "Promote the current heading higher up the tree."
  7198. (org-with-wide-buffer
  7199. (org-back-to-heading t)
  7200. (let* ((after-change-functions (remq 'flyspell-after-change-function
  7201. after-change-functions))
  7202. (level (save-match-data (funcall outline-level)))
  7203. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  7204. (diff (abs (- level (length up-head) -1))))
  7205. (cond
  7206. ((and (= level 1) org-allow-promoting-top-level-subtree)
  7207. (replace-match "# " nil t))
  7208. ((= level 1)
  7209. (user-error "Cannot promote to level 0. UNDO to recover if necessary"))
  7210. (t (replace-match up-head nil t)))
  7211. (unless (= level 1)
  7212. (when org-auto-align-tags (org-set-tags nil 'ignore-column))
  7213. (when org-adapt-indentation (org-fixup-indentation (- diff))))
  7214. (run-hooks 'org-after-promote-entry-hook))))
  7215. (defun org-demote ()
  7216. "Demote the current heading lower down the tree."
  7217. (org-with-wide-buffer
  7218. (org-back-to-heading t)
  7219. (let* ((after-change-functions (remq 'flyspell-after-change-function
  7220. after-change-functions))
  7221. (level (save-match-data (funcall outline-level)))
  7222. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  7223. (diff (abs (- level (length down-head) -1))))
  7224. (replace-match down-head nil t)
  7225. (when org-auto-align-tags (org-set-tags nil 'ignore-column))
  7226. (when org-adapt-indentation (org-fixup-indentation diff))
  7227. (run-hooks 'org-after-demote-entry-hook))))
  7228. (defun org-cycle-level ()
  7229. "Cycle the level of an empty headline through possible states.
  7230. This goes first to child, then to parent, level, then up the hierarchy.
  7231. After top level, it switches back to sibling level."
  7232. (interactive)
  7233. (let ((org-adapt-indentation nil))
  7234. (when (org-point-at-end-of-empty-headline)
  7235. (setq this-command 'org-cycle-level) ; Only needed for caching
  7236. (let ((cur-level (org-current-level))
  7237. (prev-level (org-get-previous-line-level)))
  7238. (cond
  7239. ;; If first headline in file, promote to top-level.
  7240. ((= prev-level 0)
  7241. (cl-loop repeat (/ (- cur-level 1) (org-level-increment))
  7242. do (org-do-promote)))
  7243. ;; If same level as prev, demote one.
  7244. ((= prev-level cur-level)
  7245. (org-do-demote))
  7246. ;; If parent is top-level, promote to top level if not already.
  7247. ((= prev-level 1)
  7248. (cl-loop repeat (/ (- cur-level 1) (org-level-increment))
  7249. do (org-do-promote)))
  7250. ;; If top-level, return to prev-level.
  7251. ((= cur-level 1)
  7252. (cl-loop repeat (/ (- prev-level 1) (org-level-increment))
  7253. do (org-do-demote)))
  7254. ;; If less than prev-level, promote one.
  7255. ((< cur-level prev-level)
  7256. (org-do-promote))
  7257. ;; If deeper than prev-level, promote until higher than
  7258. ;; prev-level.
  7259. ((> cur-level prev-level)
  7260. (cl-loop repeat (+ 1 (/ (- cur-level prev-level) (org-level-increment)))
  7261. do (org-do-promote))))
  7262. t))))
  7263. (defun org-map-tree (fun)
  7264. "Call FUN for every heading underneath the current one."
  7265. (org-back-to-heading)
  7266. (let ((level (funcall outline-level)))
  7267. (save-excursion
  7268. (funcall fun)
  7269. (while (and (progn
  7270. (outline-next-heading)
  7271. (> (funcall outline-level) level))
  7272. (not (eobp)))
  7273. (funcall fun)))))
  7274. (defun org-map-region (fun beg end)
  7275. "Call FUN for every heading between BEG and END."
  7276. (let ((org-ignore-region t))
  7277. (save-excursion
  7278. (setq end (copy-marker end))
  7279. (goto-char beg)
  7280. (when (and (re-search-forward org-outline-regexp-bol nil t)
  7281. (< (point) end))
  7282. (funcall fun))
  7283. (while (and (progn
  7284. (outline-next-heading)
  7285. (< (point) end))
  7286. (not (eobp)))
  7287. (funcall fun)))))
  7288. (defun org-fixup-indentation (diff)
  7289. "Change the indentation in the current entry by DIFF.
  7290. DIFF is an integer. Indentation is done according to the
  7291. following rules:
  7292. - Planning information and property drawers are always indented
  7293. according to the new level of the headline;
  7294. - Footnote definitions and their contents are ignored;
  7295. - Inlinetasks' boundaries are not shifted;
  7296. - Empty lines are ignored;
  7297. - Other lines' indentation are shifted by DIFF columns, unless
  7298. it would introduce a structural change in the document, in
  7299. which case no shifting is done at all.
  7300. Assume point is at a heading or an inlinetask beginning."
  7301. (org-with-wide-buffer
  7302. (narrow-to-region (line-beginning-position)
  7303. (save-excursion
  7304. (if (org-with-limited-levels (org-at-heading-p))
  7305. (org-with-limited-levels (outline-next-heading))
  7306. (org-inlinetask-goto-end))
  7307. (point)))
  7308. (forward-line)
  7309. ;; Indent properly planning info and property drawer.
  7310. (when (org-looking-at-p org-planning-line-re)
  7311. (org-indent-line)
  7312. (forward-line))
  7313. (when (looking-at org-property-drawer-re)
  7314. (goto-char (match-end 0))
  7315. (forward-line)
  7316. (save-excursion (org-indent-region (match-beginning 0) (match-end 0))))
  7317. (catch 'no-shift
  7318. (when (zerop diff) (throw 'no-shift nil))
  7319. ;; If DIFF is negative, first check if a shift is possible at all
  7320. ;; (e.g., it doesn't break structure). This can only happen if
  7321. ;; some contents are not properly indented.
  7322. (let ((case-fold-search t))
  7323. (when (< diff 0)
  7324. (let ((diff (- diff))
  7325. (forbidden-re (concat org-outline-regexp
  7326. "\\|"
  7327. (substring org-footnote-definition-re 1))))
  7328. (save-excursion
  7329. (while (not (eobp))
  7330. (cond
  7331. ((org-looking-at-p "[ \t]*$") (forward-line))
  7332. ((and (org-looking-at-p org-footnote-definition-re)
  7333. (let ((e (org-element-at-point)))
  7334. (and (eq (org-element-type e) 'footnote-definition)
  7335. (goto-char (org-element-property :end e))))))
  7336. ((org-looking-at-p org-outline-regexp) (forward-line))
  7337. ;; Give up if shifting would move before column 0 or
  7338. ;; if it would introduce a headline or a footnote
  7339. ;; definition.
  7340. (t
  7341. (skip-chars-forward " \t")
  7342. (let ((ind (current-column)))
  7343. (when (or (< ind diff)
  7344. (and (= ind diff) (org-looking-at-p forbidden-re)))
  7345. (throw 'no-shift nil)))
  7346. ;; Ignore contents of example blocks and source
  7347. ;; blocks if their indentation is meant to be
  7348. ;; preserved. Jump to block's closing line.
  7349. (beginning-of-line)
  7350. (or (and (org-looking-at-p "[ \t]*#\\+BEGIN_\\(EXAMPLE\\|SRC\\)")
  7351. (let ((e (org-element-at-point)))
  7352. (and (memq (org-element-type e)
  7353. '(example-block src-block))
  7354. (or org-src-preserve-indentation
  7355. (org-element-property :preserve-indent e))
  7356. (goto-char (org-element-property :end e))
  7357. (progn (skip-chars-backward " \r\t\n")
  7358. (beginning-of-line)
  7359. t))))
  7360. (forward-line))))))))
  7361. ;; Shift lines but footnote definitions, inlinetasks boundaries
  7362. ;; by DIFF. Also skip contents of source or example blocks
  7363. ;; when indentation is meant to be preserved.
  7364. (while (not (eobp))
  7365. (cond
  7366. ((and (org-looking-at-p org-footnote-definition-re)
  7367. (let ((e (org-element-at-point)))
  7368. (and (eq (org-element-type e) 'footnote-definition)
  7369. (goto-char (org-element-property :end e))))))
  7370. ((org-looking-at-p org-outline-regexp) (forward-line))
  7371. ((org-looking-at-p "[ \t]*$") (forward-line))
  7372. (t
  7373. (org-indent-line-to (+ (org-get-indentation) diff))
  7374. (beginning-of-line)
  7375. (or (and (org-looking-at-p "[ \t]*#\\+BEGIN_\\(EXAMPLE\\|SRC\\)")
  7376. (let ((e (org-element-at-point)))
  7377. (and (memq (org-element-type e)
  7378. '(example-block src-block))
  7379. (or org-src-preserve-indentation
  7380. (org-element-property :preserve-indent e))
  7381. (goto-char (org-element-property :end e))
  7382. (progn (skip-chars-backward " \r\t\n")
  7383. (beginning-of-line)
  7384. t))))
  7385. (forward-line)))))))))
  7386. (defun org-convert-to-odd-levels ()
  7387. "Convert an org-mode file with all levels allowed to one with odd levels.
  7388. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  7389. level 5 etc."
  7390. (interactive)
  7391. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  7392. (let ((outline-level 'org-outline-level)
  7393. (org-odd-levels-only nil) n)
  7394. (save-excursion
  7395. (goto-char (point-min))
  7396. (while (re-search-forward "^\\*\\*+ " nil t)
  7397. (setq n (- (length (match-string 0)) 2))
  7398. (while (>= (setq n (1- n)) 0)
  7399. (org-demote))
  7400. (end-of-line 1))))))
  7401. (defun org-convert-to-oddeven-levels ()
  7402. "Convert an org-mode file with only odd levels to one with odd/even levels.
  7403. This promotes level 3 to level 2, level 5 to level 3 etc. If the
  7404. file contains a section with an even level, conversion would
  7405. destroy the structure of the file. An error is signaled in this
  7406. case."
  7407. (interactive)
  7408. (goto-char (point-min))
  7409. ;; First check if there are no even levels
  7410. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  7411. (org-show-set-visibility 'canonical)
  7412. (error "Not all levels are odd in this file. Conversion not possible"))
  7413. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  7414. (let ((outline-regexp org-outline-regexp)
  7415. (outline-level 'org-outline-level)
  7416. (org-odd-levels-only nil) n)
  7417. (save-excursion
  7418. (goto-char (point-min))
  7419. (while (re-search-forward "^\\*\\*+ " nil t)
  7420. (setq n (/ (1- (length (match-string 0))) 2))
  7421. (while (>= (setq n (1- n)) 0)
  7422. (org-promote))
  7423. (end-of-line 1))))))
  7424. (defun org-tr-level (n)
  7425. "Make N odd if required."
  7426. (if org-odd-levels-only (1+ (/ n 2)) n))
  7427. ;;; Vertical tree motion, cutting and pasting of subtrees
  7428. (defun org-move-subtree-up (&optional arg)
  7429. "Move the current subtree up past ARG headlines of the same level."
  7430. (interactive "p")
  7431. (org-move-subtree-down (- (prefix-numeric-value arg))))
  7432. (defun org-move-subtree-down (&optional arg)
  7433. "Move the current subtree down past ARG headlines of the same level."
  7434. (interactive "p")
  7435. (setq arg (prefix-numeric-value arg))
  7436. (let ((movfunc (if (> arg 0) 'org-get-next-sibling
  7437. 'org-get-last-sibling))
  7438. (ins-point (make-marker))
  7439. (cnt (abs arg))
  7440. (col (current-column))
  7441. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  7442. ;; Select the tree
  7443. (org-back-to-heading)
  7444. (setq beg0 (point))
  7445. (save-excursion
  7446. (setq ne-beg (org-back-over-empty-lines))
  7447. (setq beg (point)))
  7448. (save-match-data
  7449. (save-excursion (outline-end-of-heading)
  7450. (setq folded (outline-invisible-p)))
  7451. (progn (org-end-of-subtree nil t)
  7452. (unless (eobp) (backward-char))))
  7453. (outline-next-heading)
  7454. (setq ne-end (org-back-over-empty-lines))
  7455. (setq end (point))
  7456. (goto-char beg0)
  7457. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  7458. ;; include less whitespace
  7459. (save-excursion
  7460. (goto-char beg)
  7461. (forward-line (- ne-beg ne-end))
  7462. (setq beg (point))))
  7463. ;; Find insertion point, with error handling
  7464. (while (> cnt 0)
  7465. (or (and (funcall movfunc) (looking-at org-outline-regexp))
  7466. (progn (goto-char beg0)
  7467. (user-error "Cannot move past superior level or buffer limit")))
  7468. (setq cnt (1- cnt)))
  7469. (when (> arg 0)
  7470. ;; Moving forward - still need to move over subtree
  7471. (org-end-of-subtree t t)
  7472. (save-excursion
  7473. (org-back-over-empty-lines)
  7474. (or (bolp) (newline))))
  7475. (setq ne-ins (org-back-over-empty-lines))
  7476. (move-marker ins-point (point))
  7477. (setq txt (buffer-substring beg end))
  7478. (org-save-markers-in-region beg end)
  7479. (delete-region beg end)
  7480. (org-remove-empty-overlays-at beg)
  7481. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  7482. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  7483. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  7484. (let ((bbb (point)))
  7485. (insert-before-markers txt)
  7486. (org-reinstall-markers-in-region bbb)
  7487. (move-marker ins-point bbb))
  7488. (or (bolp) (insert "\n"))
  7489. (setq ins-end (point))
  7490. (goto-char ins-point)
  7491. (org-skip-whitespace)
  7492. (when (and (< arg 0)
  7493. (org-first-sibling-p)
  7494. (> ne-ins ne-beg))
  7495. ;; Move whitespace back to beginning
  7496. (save-excursion
  7497. (goto-char ins-end)
  7498. (let ((kill-whole-line t))
  7499. (kill-line (- ne-ins ne-beg)) (point)))
  7500. (insert (make-string (- ne-ins ne-beg) ?\n)))
  7501. (move-marker ins-point nil)
  7502. (if folded
  7503. (outline-hide-subtree)
  7504. (org-show-entry)
  7505. (org-show-children)
  7506. (org-cycle-hide-drawers 'children))
  7507. (org-clean-visibility-after-subtree-move)
  7508. ;; move back to the initial column we were at
  7509. (move-to-column col)))
  7510. (defvar org-subtree-clip ""
  7511. "Clipboard for cut and paste of subtrees.
  7512. This is actually only a copy of the kill, because we use the normal kill
  7513. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  7514. (defvar org-subtree-clip-folded nil
  7515. "Was the last copied subtree folded?
  7516. This is used to fold the tree back after pasting.")
  7517. (defun org-cut-subtree (&optional n)
  7518. "Cut the current subtree into the clipboard.
  7519. With prefix arg N, cut this many sequential subtrees.
  7520. This is a short-hand for marking the subtree and then cutting it."
  7521. (interactive "p")
  7522. (org-copy-subtree n 'cut))
  7523. (defun org-copy-subtree (&optional n cut force-store-markers nosubtrees)
  7524. "Copy the current subtree it in the clipboard.
  7525. With prefix arg N, copy this many sequential subtrees.
  7526. This is a short-hand for marking the subtree and then copying it.
  7527. If CUT is non-nil, actually cut the subtree.
  7528. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  7529. of some markers in the region, even if CUT is non-nil. This is
  7530. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  7531. (interactive "p")
  7532. (let (beg end folded (beg0 (point)))
  7533. (if (org-called-interactively-p 'any)
  7534. (org-back-to-heading nil) ; take what looks like a subtree
  7535. (org-back-to-heading t)) ; take what is really there
  7536. (setq beg (point))
  7537. (skip-chars-forward " \t\r\n")
  7538. (save-match-data
  7539. (if nosubtrees
  7540. (outline-next-heading)
  7541. (save-excursion (outline-end-of-heading)
  7542. (setq folded (outline-invisible-p)))
  7543. (ignore-errors (org-forward-heading-same-level (1- n) t))
  7544. (org-end-of-subtree t t)))
  7545. ;; Include the end of an inlinetask
  7546. (when (and (featurep 'org-inlinetask)
  7547. (looking-at-p (concat (org-inlinetask-outline-regexp)
  7548. "END[ \t]*$")))
  7549. (end-of-line))
  7550. (setq end (point))
  7551. (goto-char beg0)
  7552. (when (> end beg)
  7553. (setq org-subtree-clip-folded folded)
  7554. (when (or cut force-store-markers)
  7555. (org-save-markers-in-region beg end))
  7556. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  7557. (setq org-subtree-clip (current-kill 0))
  7558. (message "%s: Subtree(s) with %d characters"
  7559. (if cut "Cut" "Copied")
  7560. (length org-subtree-clip)))))
  7561. (defun org-paste-subtree (&optional level tree for-yank remove)
  7562. "Paste the clipboard as a subtree, with modification of headline level.
  7563. The entire subtree is promoted or demoted in order to match a new headline
  7564. level.
  7565. If the cursor is at the beginning of a headline, the same level as
  7566. that headline is used to paste the tree.
  7567. If not, the new level is derived from the *visible* headings
  7568. before and after the insertion point, and taken to be the inferior headline
  7569. level of the two. So if the previous visible heading is level 3 and the
  7570. next is level 4 (or vice versa), level 4 will be used for insertion.
  7571. This makes sure that the subtree remains an independent subtree and does
  7572. not swallow low level entries.
  7573. You can also force a different level, either by using a numeric prefix
  7574. argument, or by inserting the heading marker by hand. For example, if the
  7575. cursor is after \"*****\", then the tree will be shifted to level 5.
  7576. If optional TREE is given, use this text instead of the kill ring.
  7577. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  7578. move back over whitespace before inserting, and move point to the end of
  7579. the inserted text when done.
  7580. When REMOVE is non-nil, remove the subtree from the clipboard."
  7581. (interactive "P")
  7582. (setq tree (or tree (and kill-ring (current-kill 0))))
  7583. (unless (org-kill-is-subtree-p tree)
  7584. (user-error "%s"
  7585. (substitute-command-keys
  7586. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  7587. (org-with-limited-levels
  7588. (let* ((visp (not (outline-invisible-p)))
  7589. (txt tree)
  7590. (^re_ "\\(\\*+\\)[ \t]*")
  7591. (old-level (if (string-match org-outline-regexp-bol txt)
  7592. (- (match-end 0) (match-beginning 0) 1)
  7593. -1))
  7594. (force-level (cond (level (prefix-numeric-value level))
  7595. ((and (looking-at "[ \t]*$")
  7596. (string-match
  7597. "^\\*+$" (buffer-substring
  7598. (point-at-bol) (point))))
  7599. (- (match-end 0) (match-beginning 0)))
  7600. ((and (bolp)
  7601. (looking-at org-outline-regexp))
  7602. (- (match-end 0) (point) 1))))
  7603. (previous-level (save-excursion
  7604. (condition-case nil
  7605. (progn
  7606. (outline-previous-visible-heading 1)
  7607. (if (looking-at ^re_)
  7608. (- (match-end 0) (match-beginning 0) 1)
  7609. 1))
  7610. (error 1))))
  7611. (next-level (save-excursion
  7612. (condition-case nil
  7613. (progn
  7614. (or (looking-at org-outline-regexp)
  7615. (outline-next-visible-heading 1))
  7616. (if (looking-at ^re_)
  7617. (- (match-end 0) (match-beginning 0) 1)
  7618. 1))
  7619. (error 1))))
  7620. (new-level (or force-level (max previous-level next-level)))
  7621. (shift (if (or (= old-level -1)
  7622. (= new-level -1)
  7623. (= old-level new-level))
  7624. 0
  7625. (- new-level old-level)))
  7626. (delta (if (> shift 0) -1 1))
  7627. (func (if (> shift 0) 'org-demote 'org-promote))
  7628. (org-odd-levels-only nil)
  7629. beg end newend)
  7630. ;; Remove the forced level indicator
  7631. (when force-level
  7632. (delete-region (point-at-bol) (point)))
  7633. ;; Paste
  7634. (beginning-of-line (if (bolp) 1 2))
  7635. (setq beg (point))
  7636. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  7637. (insert-before-markers txt)
  7638. (unless (string-match "\n\\'" txt) (insert "\n"))
  7639. (setq newend (point))
  7640. (org-reinstall-markers-in-region beg)
  7641. (setq end (point))
  7642. (goto-char beg)
  7643. (skip-chars-forward " \t\n\r")
  7644. (setq beg (point))
  7645. (when (and (outline-invisible-p) visp)
  7646. (save-excursion (outline-show-heading)))
  7647. ;; Shift if necessary
  7648. (unless (= shift 0)
  7649. (save-restriction
  7650. (narrow-to-region beg end)
  7651. (while (not (= shift 0))
  7652. (org-map-region func (point-min) (point-max))
  7653. (setq shift (+ delta shift)))
  7654. (goto-char (point-min))
  7655. (setq newend (point-max))))
  7656. (when (or (org-called-interactively-p 'interactive) for-yank)
  7657. (message "Clipboard pasted as level %d subtree" new-level))
  7658. (when (and (not for-yank) ; in this case, org-yank will decide about folding
  7659. kill-ring
  7660. (eq org-subtree-clip (current-kill 0))
  7661. org-subtree-clip-folded)
  7662. ;; The tree was folded before it was killed/copied
  7663. (outline-hide-subtree))
  7664. (and for-yank (goto-char newend))
  7665. (and remove (setq kill-ring (cdr kill-ring))))))
  7666. (defun org-kill-is-subtree-p (&optional txt)
  7667. "Check if the current kill is an outline subtree, or a set of trees.
  7668. Returns nil if kill does not start with a headline, or if the first
  7669. headline level is not the largest headline level in the tree.
  7670. So this will actually accept several entries of equal levels as well,
  7671. which is OK for `org-paste-subtree'.
  7672. If optional TXT is given, check this string instead of the current kill."
  7673. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  7674. (re (org-get-limited-outline-regexp))
  7675. (^re (concat "^" re))
  7676. (start-level (and kill
  7677. (string-match
  7678. (concat "\\`\\([ \t\n\r]*?\n\\)?\\(" re "\\)")
  7679. kill)
  7680. (- (match-end 2) (match-beginning 2) 1)))
  7681. (start (1+ (or (match-beginning 2) -1))))
  7682. (if (not start-level)
  7683. (progn
  7684. nil) ;; does not even start with a heading
  7685. (catch 'exit
  7686. (while (setq start (string-match ^re kill (1+ start)))
  7687. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  7688. (throw 'exit nil)))
  7689. t))))
  7690. (defvar org-markers-to-move nil
  7691. "Markers that should be moved with a cut-and-paste operation.
  7692. Those markers are stored together with their positions relative to
  7693. the start of the region.")
  7694. (defun org-save-markers-in-region (beg end)
  7695. "Check markers in region.
  7696. If these markers are between BEG and END, record their position relative
  7697. to BEG, so that after moving the block of text, we can put the markers back
  7698. into place.
  7699. This function gets called just before an entry or tree gets cut from the
  7700. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  7701. called immediately, to move the markers with the entries."
  7702. (setq org-markers-to-move nil)
  7703. (when (featurep 'org-clock)
  7704. (org-clock-save-markers-for-cut-and-paste beg end))
  7705. (when (featurep 'org-agenda)
  7706. (org-agenda-save-markers-for-cut-and-paste beg end)))
  7707. (defun org-check-and-save-marker (marker beg end)
  7708. "Check if MARKER is between BEG and END.
  7709. If yes, remember the marker and the distance to BEG."
  7710. (when (and (marker-buffer marker)
  7711. (equal (marker-buffer marker) (current-buffer))
  7712. (>= marker beg) (< marker end))
  7713. (push (cons marker (- marker beg)) org-markers-to-move)))
  7714. (defun org-reinstall-markers-in-region (beg)
  7715. "Move all remembered markers to their position relative to BEG."
  7716. (dolist (x org-markers-to-move)
  7717. (move-marker (car x) (+ beg (cdr x))))
  7718. (setq org-markers-to-move nil))
  7719. (defun org-narrow-to-subtree ()
  7720. "Narrow buffer to the current subtree."
  7721. (interactive)
  7722. (save-excursion
  7723. (save-match-data
  7724. (org-with-limited-levels
  7725. (narrow-to-region
  7726. (progn (org-back-to-heading t) (point))
  7727. (progn (org-end-of-subtree t t)
  7728. (when (and (org-at-heading-p) (not (eobp))) (backward-char 1))
  7729. (point)))))))
  7730. (defun org-narrow-to-block ()
  7731. "Narrow buffer to the current block."
  7732. (interactive)
  7733. (let* ((case-fold-search t)
  7734. (blockp (org-between-regexps-p "^[ \t]*#\\+begin_.*"
  7735. "^[ \t]*#\\+end_.*")))
  7736. (if blockp
  7737. (narrow-to-region (car blockp) (cdr blockp))
  7738. (user-error "Not in a block"))))
  7739. (defun org-clone-subtree-with-time-shift (n &optional shift)
  7740. "Clone the task (subtree) at point N times.
  7741. The clones will be inserted as siblings.
  7742. In interactive use, the user will be prompted for the number of
  7743. clones to be produced. If the entry has a timestamp, the user
  7744. will also be prompted for a time shift, which may be a repeater
  7745. as used in time stamps, for example `+3d'. To disable this,
  7746. you can call the function with a universal prefix argument.
  7747. When a valid repeater is given and the entry contains any time
  7748. stamps, the clones will become a sequence in time, with time
  7749. stamps in the subtree shifted for each clone produced. If SHIFT
  7750. is nil or the empty string, time stamps will be left alone. The
  7751. ID property of the original subtree is removed.
  7752. If the original subtree did contain time stamps with a repeater,
  7753. the following will happen:
  7754. - the repeater will be removed in each clone
  7755. - an additional clone will be produced, with the current, unshifted
  7756. date(s) in the entry.
  7757. - the original entry will be placed *after* all the clones, with
  7758. repeater intact.
  7759. - the start days in the repeater in the original entry will be shifted
  7760. to past the last clone.
  7761. In this way you can spell out a number of instances of a repeating task,
  7762. and still retain the repeater to cover future instances of the task.
  7763. As described above, N+1 clones are produced when the original
  7764. subtree has a repeater. Setting N to 0, then, can be used to
  7765. remove the repeater from a subtree and create a shifted clone
  7766. with the original repeater."
  7767. (interactive "nNumber of clones to produce: ")
  7768. (let ((shift
  7769. (or shift
  7770. (if (and (not (equal current-prefix-arg '(4)))
  7771. (save-excursion
  7772. (re-search-forward org-ts-regexp-both
  7773. (save-excursion
  7774. (org-end-of-subtree t)
  7775. (point)) t)))
  7776. (read-from-minibuffer
  7777. "Date shift per clone (e.g. +1w, empty to copy unchanged): ")
  7778. ""))) ;; No time shift
  7779. (n-no-remove -1)
  7780. (drawer-re org-drawer-regexp)
  7781. (org-clock-re (format "^[ \t]*%s.*$" org-clock-string))
  7782. beg end template task idprop
  7783. shift-n shift-what doshift nmin nmax)
  7784. (unless (wholenump n)
  7785. (user-error "Invalid number of replications %s" n))
  7786. (when (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  7787. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([hdwmy]\\)[ \t]*\\'"
  7788. shift)))
  7789. (user-error "Invalid shift specification %s" shift))
  7790. (when doshift
  7791. (setq shift-n (string-to-number (match-string 1 shift))
  7792. shift-what (cdr (assoc (match-string 2 shift)
  7793. '(("d" . day) ("w" . week)
  7794. ("m" . month) ("y" . year))))))
  7795. (when (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  7796. (setq nmin 1 nmax n)
  7797. (org-back-to-heading t)
  7798. (setq beg (point))
  7799. (setq idprop (org-entry-get nil "ID"))
  7800. (org-end-of-subtree t t)
  7801. (or (bolp) (insert "\n"))
  7802. (setq end (point))
  7803. (setq template (buffer-substring beg end))
  7804. (when (and doshift
  7805. (string-match "<[^<>\n]+ [.+]?\\+[0-9]+[hdwmy][^<>\n]*>" template))
  7806. (delete-region beg end)
  7807. (setq end beg)
  7808. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  7809. (goto-char end)
  7810. (cl-loop for n from nmin to nmax do
  7811. ;; prepare clone
  7812. (with-temp-buffer
  7813. (insert template)
  7814. (org-mode)
  7815. (goto-char (point-min))
  7816. (org-show-subtree)
  7817. (and idprop (if org-clone-delete-id
  7818. (org-entry-delete nil "ID")
  7819. (org-id-get-create t)))
  7820. (unless (= n 0)
  7821. (while (re-search-forward org-clock-re nil t)
  7822. (kill-whole-line))
  7823. (goto-char (point-min))
  7824. (while (re-search-forward drawer-re nil t)
  7825. (org-remove-empty-drawer-at (point))))
  7826. (goto-char (point-min))
  7827. (when doshift
  7828. (while (re-search-forward org-ts-regexp-both nil t)
  7829. (org-timestamp-change (* n shift-n) shift-what))
  7830. (unless (= n n-no-remove)
  7831. (goto-char (point-min))
  7832. (while (re-search-forward org-ts-regexp nil t)
  7833. (save-excursion
  7834. (goto-char (match-beginning 0))
  7835. (when (looking-at "<[^<>\n]+\\( +[.+]?\\+[0-9]+[hdwmy]\\)")
  7836. (delete-region (match-beginning 1) (match-end 1)))))))
  7837. (setq task (buffer-string)))
  7838. (insert task))
  7839. (goto-char beg)))
  7840. ;;; Outline Sorting
  7841. (defun org-sort (with-case)
  7842. "Call `org-sort-entries', `org-table-sort-lines' or `org-sort-list'.
  7843. Optional argument WITH-CASE means sort case-sensitively."
  7844. (interactive "P")
  7845. (cond
  7846. ((org-at-table-p) (org-call-with-arg 'org-table-sort-lines with-case))
  7847. ((org-at-item-p) (org-call-with-arg 'org-sort-list with-case))
  7848. (t
  7849. (org-call-with-arg 'org-sort-entries with-case))))
  7850. (defun org-sort-remove-invisible (s)
  7851. "Remove invisible links from string S."
  7852. (remove-text-properties 0 (length s) org-rm-props s)
  7853. (while (string-match org-bracket-link-regexp s)
  7854. (setq s (replace-match (if (match-end 2)
  7855. (match-string 3 s)
  7856. (match-string 1 s))
  7857. t t s)))
  7858. (let ((st (format " %s " s)))
  7859. (while (string-match org-emph-re st)
  7860. (setq st (replace-match (format " %s " (match-string 4 st)) t t st)))
  7861. (setq s (substring st 1 -1)))
  7862. s)
  7863. (defvar org-priority-regexp) ; defined later in the file
  7864. (defvar org-after-sorting-entries-or-items-hook nil
  7865. "Hook that is run after a bunch of entries or items have been sorted.
  7866. When children are sorted, the cursor is in the parent line when this
  7867. hook gets called. When a region or a plain list is sorted, the cursor
  7868. will be in the first entry of the sorted region/list.")
  7869. (defun org-sort-entries
  7870. (&optional with-case sorting-type getkey-func compare-func property)
  7871. "Sort entries on a certain level of an outline tree.
  7872. If there is an active region, the entries in the region are sorted.
  7873. Else, if the cursor is before the first entry, sort the top-level items.
  7874. Else, the children of the entry at point are sorted.
  7875. Sorting can be alphabetically, numerically, by date/time as given by
  7876. a time stamp, by a property, by priority order, or by a custom function.
  7877. The command prompts for the sorting type unless it has been given to the
  7878. function through the SORTING-TYPE argument, which needs to be a character,
  7879. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?o ?O ?r ?R ?f ?F ?k ?K). Here is
  7880. the precise meaning of each character:
  7881. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  7882. c By creation time, which is assumed to be the first inactive time stamp
  7883. at the beginning of a line.
  7884. d By deadline date/time.
  7885. k By clocking time.
  7886. n Numerically, by converting the beginning of the entry/item to a number.
  7887. o By order of TODO keywords.
  7888. p By priority according to the cookie.
  7889. r By the value of a property.
  7890. s By scheduled date/time.
  7891. t By date/time, either the first active time stamp in the entry, or, if
  7892. none exist, by the first inactive one.
  7893. Capital letters will reverse the sort order.
  7894. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  7895. called with point at the beginning of the record. It must return either
  7896. a string or a number that should serve as the sorting key for that record.
  7897. Comparing entries ignores case by default. However, with an optional argument
  7898. WITH-CASE, the sorting considers case as well.
  7899. Sorting is done against the visible part of the headlines, it ignores hidden
  7900. links.
  7901. When sorting is done, call `org-after-sorting-entries-or-items-hook'."
  7902. (interactive "P")
  7903. (let ((case-func (if with-case 'identity 'downcase))
  7904. (cmstr
  7905. ;; The clock marker is lost when using `sort-subr', let's
  7906. ;; store the clocking string.
  7907. (when (equal (marker-buffer org-clock-marker) (current-buffer))
  7908. (save-excursion
  7909. (goto-char org-clock-marker)
  7910. (buffer-substring-no-properties (line-beginning-position)
  7911. (point)))))
  7912. start beg end stars re re2
  7913. txt what tmp)
  7914. ;; Find beginning and end of region to sort
  7915. (cond
  7916. ((org-region-active-p)
  7917. ;; we will sort the region
  7918. (setq end (region-end)
  7919. what "region")
  7920. (goto-char (region-beginning))
  7921. (unless (org-at-heading-p) (outline-next-heading))
  7922. (setq start (point)))
  7923. ((or (org-at-heading-p)
  7924. (ignore-errors (progn (org-back-to-heading) t)))
  7925. ;; we will sort the children of the current headline
  7926. (org-back-to-heading)
  7927. (setq start (point)
  7928. end (progn (org-end-of-subtree t t)
  7929. (or (bolp) (insert "\n"))
  7930. (when (>= (org-back-over-empty-lines) 1)
  7931. (forward-line 1))
  7932. (point))
  7933. what "children")
  7934. (goto-char start)
  7935. (outline-show-subtree)
  7936. (outline-next-heading))
  7937. (t
  7938. ;; we will sort the top-level entries in this file
  7939. (goto-char (point-min))
  7940. (or (org-at-heading-p) (outline-next-heading))
  7941. (setq start (point))
  7942. (goto-char (point-max))
  7943. (beginning-of-line 1)
  7944. (when (looking-at ".*?\\S-")
  7945. ;; File ends in a non-white line
  7946. (end-of-line 1)
  7947. (insert "\n"))
  7948. (setq end (point-max))
  7949. (setq what "top-level")
  7950. (goto-char start)
  7951. (outline-show-all)))
  7952. (setq beg (point))
  7953. (when (>= beg end) (goto-char start) (user-error "Nothing to sort"))
  7954. (looking-at "\\(\\*+\\)")
  7955. (setq stars (match-string 1)
  7956. re (concat "^" (regexp-quote stars) " +")
  7957. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[ \t\n]")
  7958. txt (buffer-substring beg end))
  7959. (unless (equal (substring txt -1) "\n") (setq txt (concat txt "\n")))
  7960. (when (and (not (equal stars "*")) (string-match re2 txt))
  7961. (user-error "Region to sort contains a level above the first entry"))
  7962. (unless sorting-type
  7963. (message
  7964. "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
  7965. [t]ime [s]cheduled [d]eadline [c]reated cloc[k]ing
  7966. A/N/P/R/O/F/T/S/D/C/K means reversed:"
  7967. what)
  7968. (setq sorting-type (read-char-exclusive))
  7969. (unless getkey-func
  7970. (and (= (downcase sorting-type) ?f)
  7971. (setq getkey-func
  7972. (completing-read "Sort using function: "
  7973. obarray 'fboundp t nil nil))
  7974. (setq getkey-func (intern getkey-func))))
  7975. (and (= (downcase sorting-type) ?r)
  7976. (not property)
  7977. (setq property
  7978. (completing-read "Property: "
  7979. (mapcar #'list (org-buffer-property-keys t))
  7980. nil t))))
  7981. (when (member sorting-type '(?k ?K)) (org-clock-sum))
  7982. (message "Sorting entries...")
  7983. (save-restriction
  7984. (narrow-to-region start end)
  7985. (let ((dcst (downcase sorting-type))
  7986. (case-fold-search nil)
  7987. (now (current-time)))
  7988. (sort-subr
  7989. (/= dcst sorting-type)
  7990. ;; This function moves to the beginning character of the "record" to
  7991. ;; be sorted.
  7992. (lambda nil
  7993. (if (re-search-forward re nil t)
  7994. (goto-char (match-beginning 0))
  7995. (goto-char (point-max))))
  7996. ;; This function moves to the last character of the "record" being
  7997. ;; sorted.
  7998. (lambda nil
  7999. (save-match-data
  8000. (condition-case nil
  8001. (outline-forward-same-level 1)
  8002. (error
  8003. (goto-char (point-max))))))
  8004. ;; This function returns the value that gets sorted against.
  8005. (lambda nil
  8006. (cond
  8007. ((= dcst ?n)
  8008. (if (looking-at org-complex-heading-regexp)
  8009. (string-to-number (org-sort-remove-invisible (match-string 4)))
  8010. nil))
  8011. ((= dcst ?a)
  8012. (if (looking-at org-complex-heading-regexp)
  8013. (funcall case-func (org-sort-remove-invisible (match-string 4)))
  8014. nil))
  8015. ((= dcst ?k)
  8016. (or (get-text-property (point) :org-clock-minutes) 0))
  8017. ((= dcst ?t)
  8018. (let ((end (save-excursion (outline-next-heading) (point))))
  8019. (if (or (re-search-forward org-ts-regexp end t)
  8020. (re-search-forward org-ts-regexp-both end t))
  8021. (org-time-string-to-seconds (match-string 0))
  8022. (org-float-time now))))
  8023. ((= dcst ?c)
  8024. (let ((end (save-excursion (outline-next-heading) (point))))
  8025. (if (re-search-forward
  8026. (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
  8027. end t)
  8028. (org-time-string-to-seconds (match-string 0))
  8029. (org-float-time now))))
  8030. ((= dcst ?s)
  8031. (let ((end (save-excursion (outline-next-heading) (point))))
  8032. (if (re-search-forward org-scheduled-time-regexp end t)
  8033. (org-time-string-to-seconds (match-string 1))
  8034. (org-float-time now))))
  8035. ((= dcst ?d)
  8036. (let ((end (save-excursion (outline-next-heading) (point))))
  8037. (if (re-search-forward org-deadline-time-regexp end t)
  8038. (org-time-string-to-seconds (match-string 1))
  8039. (org-float-time now))))
  8040. ((= dcst ?p)
  8041. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  8042. (string-to-char (match-string 2))
  8043. org-default-priority))
  8044. ((= dcst ?r)
  8045. (or (org-entry-get nil property) ""))
  8046. ((= dcst ?o)
  8047. (when (looking-at org-complex-heading-regexp)
  8048. (let* ((m (match-string 2))
  8049. (s (if (member m org-done-keywords) '- '+)))
  8050. (- 99 (funcall s (length (member m org-todo-keywords-1)))))))
  8051. ((= dcst ?f)
  8052. (if getkey-func
  8053. (progn
  8054. (setq tmp (funcall getkey-func))
  8055. (when (stringp tmp) (setq tmp (funcall case-func tmp)))
  8056. tmp)
  8057. (error "Invalid key function `%s'" getkey-func)))
  8058. (t (error "Invalid sorting type `%c'" sorting-type))))
  8059. nil
  8060. (cond
  8061. ((= dcst ?a) 'string<)
  8062. ((= dcst ?f) compare-func)
  8063. ((member dcst '(?p ?t ?s ?d ?c ?k)) '<)))))
  8064. (run-hooks 'org-after-sorting-entries-or-items-hook)
  8065. ;; Reset the clock marker if needed
  8066. (when cmstr
  8067. (save-excursion
  8068. (goto-char start)
  8069. (search-forward cmstr nil t)
  8070. (move-marker org-clock-marker (point))))
  8071. (message "Sorting entries...done")))
  8072. ;;; The orgstruct minor mode
  8073. ;; Define a minor mode which can be used in other modes in order to
  8074. ;; integrate the org-mode structure editing commands.
  8075. ;; This is really a hack, because the org-mode structure commands use
  8076. ;; keys which normally belong to the major mode. Here is how it
  8077. ;; works: The minor mode defines all the keys necessary to operate the
  8078. ;; structure commands, but wraps the commands into a function which
  8079. ;; tests if the cursor is currently at a headline or a plain list
  8080. ;; item. If that is the case, the structure command is used,
  8081. ;; temporarily setting many Org-mode variables like regular
  8082. ;; expressions for filling etc. However, when any of those keys is
  8083. ;; used at a different location, function uses `key-binding' to look
  8084. ;; up if the key has an associated command in another currently active
  8085. ;; keymap (minor modes, major mode, global), and executes that
  8086. ;; command. There might be problems if any of the keys is otherwise
  8087. ;; used as a prefix key.
  8088. (defcustom orgstruct-heading-prefix-regexp ""
  8089. "Regexp that matches the custom prefix of Org headlines in
  8090. orgstruct(++)-mode."
  8091. :group 'org
  8092. :version "24.4"
  8093. :package-version '(Org . "8.3")
  8094. :type 'regexp)
  8095. ;;;###autoload(put 'orgstruct-heading-prefix-regexp 'safe-local-variable 'stringp)
  8096. (defcustom orgstruct-setup-hook nil
  8097. "Hook run after orgstruct-mode-map is filled."
  8098. :group 'org
  8099. :version "24.4"
  8100. :package-version '(Org . "8.0")
  8101. :type 'hook)
  8102. (defvar orgstruct-initialized nil)
  8103. (defvar org-local-vars nil
  8104. "List of local variables, for use by `orgstruct-mode'.")
  8105. ;;;###autoload
  8106. (define-minor-mode orgstruct-mode
  8107. "Toggle the minor mode `orgstruct-mode'.
  8108. This mode is for using Org-mode structure commands in other
  8109. modes. The following keys behave as if Org-mode were active, if
  8110. the cursor is on a headline, or on a plain list item (both as
  8111. defined by Org-mode)."
  8112. nil " OrgStruct" (make-sparse-keymap)
  8113. (funcall (if orgstruct-mode
  8114. 'add-to-invisibility-spec
  8115. 'remove-from-invisibility-spec)
  8116. '(outline . t))
  8117. (when orgstruct-mode
  8118. (org-load-modules-maybe)
  8119. (unless orgstruct-initialized
  8120. (orgstruct-setup)
  8121. (setq orgstruct-initialized t))))
  8122. ;;;###autoload
  8123. (defun turn-on-orgstruct ()
  8124. "Unconditionally turn on `orgstruct-mode'."
  8125. (orgstruct-mode 1))
  8126. (defvar-local orgstruct-is-++ nil
  8127. "Is `orgstruct-mode' in ++ version in the current-buffer?")
  8128. (defvar-local org-fb-vars nil)
  8129. (defun orgstruct++-mode (&optional arg)
  8130. "Toggle `orgstruct-mode', the enhanced version of it.
  8131. In addition to setting orgstruct-mode, this also exports all
  8132. indentation and autofilling variables from org-mode into the
  8133. buffer. It will also recognize item context in multiline items."
  8134. (interactive "P")
  8135. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  8136. (if (< arg 1)
  8137. (progn (orgstruct-mode -1)
  8138. (dolist (v org-fb-vars)
  8139. (set (make-local-variable (car v))
  8140. (if (eq (car-safe (cadr v)) 'quote)
  8141. (cl-cadadr v)
  8142. (nth 1 v)))))
  8143. (orgstruct-mode 1)
  8144. (setq org-fb-vars nil)
  8145. (unless org-local-vars
  8146. (setq org-local-vars (org-get-local-variables)))
  8147. (let (var val)
  8148. (dolist (x org-local-vars)
  8149. (when (string-match
  8150. "^\\(paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\
  8151. \\|fill-prefix\\|indent-\\)"
  8152. (symbol-name (car x)))
  8153. (setq var (car x) val (nth 1 x))
  8154. (push (list var `(quote ,(eval var))) org-fb-vars)
  8155. (set (make-local-variable var)
  8156. (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  8157. (setq-local orgstruct-is-++ t))))
  8158. ;;;###autoload
  8159. (defun turn-on-orgstruct++ ()
  8160. "Unconditionally turn on `orgstruct++-mode'."
  8161. (orgstruct++-mode 1))
  8162. (defun orgstruct-error ()
  8163. "Error when there is no default binding for a structure key."
  8164. (interactive)
  8165. (funcall (if (fboundp 'user-error)
  8166. 'user-error
  8167. 'error)
  8168. "This key has no function outside structure elements"))
  8169. (defun orgstruct-setup ()
  8170. "Setup orgstruct keymap."
  8171. (dolist (cell '((org-demote . t)
  8172. (org-metaleft . t)
  8173. (org-metaright . t)
  8174. (org-promote . t)
  8175. (org-shiftmetaleft . t)
  8176. (org-shiftmetaright . t)
  8177. org-backward-element
  8178. org-backward-heading-same-level
  8179. org-ctrl-c-ret
  8180. org-ctrl-c-minus
  8181. org-ctrl-c-star
  8182. org-cycle
  8183. org-forward-heading-same-level
  8184. org-insert-heading
  8185. org-insert-heading-respect-content
  8186. org-kill-note-or-show-branches
  8187. org-mark-subtree
  8188. org-meta-return
  8189. org-metadown
  8190. org-metaup
  8191. org-narrow-to-subtree
  8192. org-promote-subtree
  8193. org-reveal
  8194. org-shiftdown
  8195. org-shiftleft
  8196. org-shiftmetadown
  8197. org-shiftmetaup
  8198. org-shiftright
  8199. org-shifttab
  8200. org-shifttab
  8201. org-shiftup
  8202. org-show-children
  8203. org-show-subtree
  8204. org-sort
  8205. org-up-element
  8206. outline-demote
  8207. outline-next-visible-heading
  8208. outline-previous-visible-heading
  8209. outline-promote
  8210. outline-up-heading))
  8211. (let ((f (or (car-safe cell) cell))
  8212. (disable-when-heading-prefix (cdr-safe cell)))
  8213. (when (fboundp f)
  8214. (let ((new-bindings))
  8215. (dolist (binding (nconc (where-is-internal f org-mode-map)
  8216. (where-is-internal f outline-mode-map)))
  8217. (push binding new-bindings)
  8218. ;; TODO use local-function-key-map
  8219. (dolist (rep '(("<tab>" . "TAB")
  8220. ("<return>" . "RET")
  8221. ("<escape>" . "ESC")
  8222. ("<delete>" . "DEL")))
  8223. (setq binding (read-kbd-macro
  8224. (let ((case-fold-search))
  8225. (replace-regexp-in-string
  8226. (regexp-quote (cdr rep))
  8227. (car rep)
  8228. (key-description binding)))))
  8229. (cl-pushnew binding new-bindings :test 'equal)))
  8230. (dolist (binding new-bindings)
  8231. (let ((key (lookup-key orgstruct-mode-map binding)))
  8232. (when (or (not key) (numberp key))
  8233. (ignore-errors
  8234. (org-defkey orgstruct-mode-map
  8235. binding
  8236. (orgstruct-make-binding
  8237. f binding disable-when-heading-prefix))))))))))
  8238. (run-hooks 'orgstruct-setup-hook))
  8239. (defun orgstruct-make-binding (fun key disable-when-heading-prefix)
  8240. "Create a function for binding in the structure minor mode.
  8241. FUN is the command to call inside a table. KEY is the key that
  8242. should be checked in for a command to execute outside of tables.
  8243. Non-nil `disable-when-heading-prefix' means to disable the command
  8244. if `orgstruct-heading-prefix-regexp' is not empty."
  8245. (let ((name (concat "orgstruct-hijacker-" (symbol-name fun))))
  8246. (let ((nname name)
  8247. (i 0))
  8248. (while (fboundp (intern nname))
  8249. (setq nname (format "%s-%d" name (setq i (1+ i)))))
  8250. (setq name (intern nname)))
  8251. (eval
  8252. (let ((bindings '((org-heading-regexp
  8253. (concat "^"
  8254. orgstruct-heading-prefix-regexp
  8255. "\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ ]*$"))
  8256. (org-outline-regexp
  8257. (concat orgstruct-heading-prefix-regexp "\\*+ "))
  8258. (org-outline-regexp-bol
  8259. (concat "^" org-outline-regexp))
  8260. (outline-regexp org-outline-regexp)
  8261. (outline-heading-end-regexp "\n")
  8262. (outline-level 'org-outline-level)
  8263. (outline-heading-alist))))
  8264. `(defun ,name (arg)
  8265. ,(concat "In Structure, run `" (symbol-name fun) "'.\n"
  8266. "Outside of structure, run the binding of `"
  8267. (key-description key) "'."
  8268. (when disable-when-heading-prefix
  8269. (concat
  8270. "\nIf `orgstruct-heading-prefix-regexp' is not empty, this command will always fall\n"
  8271. "back to the default binding due to limitations of Org's implementation of\n"
  8272. "`" (symbol-name fun) "'.")))
  8273. (interactive "p")
  8274. (let* ((disable
  8275. ,(and disable-when-heading-prefix
  8276. '(not (string= orgstruct-heading-prefix-regexp ""))))
  8277. (fallback
  8278. (or disable
  8279. (not
  8280. (let* ,bindings
  8281. (org-context-p 'headline 'item
  8282. ,(when (memq fun
  8283. '(org-insert-heading
  8284. org-insert-heading-respect-content
  8285. org-meta-return))
  8286. '(when orgstruct-is-++
  8287. 'item-body))))))))
  8288. (if fallback
  8289. (let* ((orgstruct-mode)
  8290. (binding
  8291. (let ((key ,key))
  8292. (catch 'exit
  8293. (dolist
  8294. (rep
  8295. '(nil
  8296. ("<\\([^>]*\\)tab>" . "\\1TAB")
  8297. ("<\\([^>]*\\)return>" . "\\1RET")
  8298. ("<\\([^>]*\\)escape>" . "\\1ESC")
  8299. ("<\\([^>]*\\)delete>" . "\\1DEL"))
  8300. nil)
  8301. (when rep
  8302. (setq key (read-kbd-macro
  8303. (let ((case-fold-search))
  8304. (replace-regexp-in-string
  8305. (car rep)
  8306. (cdr rep)
  8307. (key-description key))))))
  8308. (when (key-binding key)
  8309. (throw 'exit (key-binding key))))))))
  8310. (if (keymapp binding)
  8311. (org-set-transient-map binding)
  8312. (let ((func (or binding
  8313. (unless disable
  8314. 'orgstruct-error))))
  8315. (when func
  8316. (call-interactively func)))))
  8317. (org-run-like-in-org-mode
  8318. (lambda ()
  8319. (interactive)
  8320. (let* ,bindings
  8321. (call-interactively ',fun)))))))))
  8322. name))
  8323. (defun org-contextualize-keys (alist contexts)
  8324. "Return valid elements in ALIST depending on CONTEXTS.
  8325. `org-agenda-custom-commands' or `org-capture-templates' are the
  8326. values used for ALIST, and `org-agenda-custom-commands-contexts'
  8327. or `org-capture-templates-contexts' are the associated contexts
  8328. definitions."
  8329. (let ((contexts
  8330. ;; normalize contexts
  8331. (mapcar
  8332. (lambda(c) (cond ((listp (cadr c))
  8333. (list (car c) (car c) (nth 1 c)))
  8334. ((string= "" (cadr c))
  8335. (list (car c) (car c) (nth 2 c)))
  8336. (t c)))
  8337. contexts))
  8338. (a alist) r s)
  8339. ;; loop over all commands or templates
  8340. (dolist (c a)
  8341. (let (vrules repl)
  8342. (cond
  8343. ((not (assoc (car c) contexts))
  8344. (push c r))
  8345. ((and (assoc (car c) contexts)
  8346. (setq vrules (org-contextualize-validate-key
  8347. (car c) contexts)))
  8348. (mapc (lambda (vr)
  8349. (unless (equal (car vr) (cadr vr))
  8350. (setq repl vr)))
  8351. vrules)
  8352. (if (not repl) (push c r)
  8353. (push (cadr repl) s)
  8354. (push
  8355. (cons (car c)
  8356. (cdr (or (assoc (cadr repl) alist)
  8357. (error "Undefined key `%s' as contextual replacement for `%s'"
  8358. (cadr repl) (car c)))))
  8359. r))))))
  8360. ;; Return limited ALIST, possibly with keys modified, and deduplicated
  8361. (delq
  8362. nil
  8363. (delete-dups
  8364. (mapcar (lambda (x)
  8365. (let ((tpl (car x)))
  8366. (unless (delq
  8367. nil
  8368. (mapcar (lambda (y)
  8369. (equal y tpl))
  8370. s))
  8371. x)))
  8372. (reverse r))))))
  8373. (defun org-contextualize-validate-key (key contexts)
  8374. "Check CONTEXTS for agenda or capture KEY."
  8375. (let (res)
  8376. (dolist (r contexts)
  8377. (dolist (rr (car (last r)))
  8378. (when
  8379. (and (equal key (car r))
  8380. (if (functionp rr) (funcall rr)
  8381. (or (and (eq (car rr) 'in-file)
  8382. (buffer-file-name)
  8383. (string-match (cdr rr) (buffer-file-name)))
  8384. (and (eq (car rr) 'in-mode)
  8385. (string-match (cdr rr) (symbol-name major-mode)))
  8386. (and (eq (car rr) 'in-buffer)
  8387. (string-match (cdr rr) (buffer-name)))
  8388. (when (and (eq (car rr) 'not-in-file)
  8389. (buffer-file-name))
  8390. (not (string-match (cdr rr) (buffer-file-name))))
  8391. (when (eq (car rr) 'not-in-mode)
  8392. (not (string-match (cdr rr) (symbol-name major-mode))))
  8393. (when (eq (car rr) 'not-in-buffer)
  8394. (not (string-match (cdr rr) (buffer-name)))))))
  8395. (push r res))))
  8396. (delete-dups (delq nil res))))
  8397. (defun org-context-p (&rest contexts)
  8398. "Check if local context is any of CONTEXTS.
  8399. Possible values in the list of contexts are `table', `headline', and `item'."
  8400. (let ((pos (point)))
  8401. (goto-char (point-at-bol))
  8402. (prog1 (or (and (memq 'table contexts)
  8403. (looking-at "[ \t]*|"))
  8404. (and (memq 'headline contexts)
  8405. (looking-at org-outline-regexp))
  8406. (and (memq 'item contexts)
  8407. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  8408. (and (memq 'item-body contexts)
  8409. (org-in-item-p)))
  8410. (goto-char pos))))
  8411. (defconst org-unique-local-variables
  8412. '(org-element--cache
  8413. org-element--cache-objects
  8414. org-element--cache-sync-keys
  8415. org-element--cache-sync-requests
  8416. org-element--cache-sync-timer)
  8417. "List of local variables that cannot be transferred to another buffer.")
  8418. (defun org-get-local-variables ()
  8419. "Return a list of all local variables in an Org mode buffer."
  8420. (delq nil
  8421. (mapcar
  8422. (lambda (x)
  8423. (let* ((binding (if (symbolp x) (list x) (list (car x) (cdr x))))
  8424. (name (car binding)))
  8425. (and (not (get name 'org-state))
  8426. (not (memq name org-unique-local-variables))
  8427. (string-match-p
  8428. "\\`\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|\
  8429. auto-fill\\|normal-auto-fill\\|fill-paragraph\\|indent-\\)"
  8430. (symbol-name name))
  8431. binding)))
  8432. (with-temp-buffer
  8433. (org-mode)
  8434. (buffer-local-variables)))))
  8435. (defun org-clone-local-variables (from-buffer &optional regexp)
  8436. "Clone local variables from FROM-BUFFER.
  8437. Optional argument REGEXP selects variables to clone."
  8438. (dolist (pair (buffer-local-variables from-buffer))
  8439. (let ((name (car pair)))
  8440. (when (and (symbolp name)
  8441. (not (memq name org-unique-local-variables))
  8442. (or (null regexp) (string-match regexp (symbol-name name))))
  8443. (set (make-local-variable name) (cdr pair))))))
  8444. ;;;###autoload
  8445. (defun org-run-like-in-org-mode (cmd)
  8446. "Run a command, pretending that the current buffer is in Org-mode.
  8447. This will temporarily bind local variables that are typically bound in
  8448. Org-mode to the values they have in Org-mode, and then interactively
  8449. call CMD."
  8450. (org-load-modules-maybe)
  8451. (unless org-local-vars
  8452. (setq org-local-vars (org-get-local-variables)))
  8453. (let (binds)
  8454. (dolist (var org-local-vars)
  8455. (when (or (not (boundp (car var)))
  8456. (eq (symbol-value (car var))
  8457. (default-value (car var))))
  8458. (push (list (car var) `(quote ,(cadr var))) binds)))
  8459. (eval `(let ,binds
  8460. (call-interactively (quote ,cmd))))))
  8461. (defun org-get-category (&optional pos force-refresh)
  8462. "Get the category applying to position POS."
  8463. (save-match-data
  8464. (when force-refresh (org-refresh-category-properties))
  8465. (let ((pos (or pos (point))))
  8466. (or (get-text-property pos 'org-category)
  8467. (progn (org-refresh-category-properties)
  8468. (get-text-property pos 'org-category))))))
  8469. ;;; Refresh properties
  8470. (defun org-refresh-properties (dprop tprop)
  8471. "Refresh buffer text properties.
  8472. DPROP is the drawer property and TPROP is either the
  8473. corresponding text property to set, or an alist with each element
  8474. being a text property (as a symbol) and a function to apply to
  8475. the value of the drawer property."
  8476. (let ((case-fold-search t)
  8477. (inhibit-read-only t))
  8478. (org-with-silent-modifications
  8479. (save-excursion
  8480. (save-restriction
  8481. (widen)
  8482. (goto-char (point-min))
  8483. (while (re-search-forward (concat "^[ \t]*:" dprop ": +\\(.*\\)[ \t]*$") nil t)
  8484. (org-refresh-property tprop (org-match-string-no-properties 1))))))))
  8485. (defun org-refresh-property (tprop p)
  8486. "Refresh the buffer text property TPROP from the drawer property P.
  8487. The refresh happens only for the current tree (not subtree)."
  8488. (unless (org-before-first-heading-p)
  8489. (save-excursion
  8490. (org-back-to-heading t)
  8491. (if (symbolp tprop)
  8492. ;; TPROP is a text property symbol
  8493. (put-text-property
  8494. (point) (or (outline-next-heading) (point-max)) tprop p)
  8495. ;; TPROP is an alist with (properties . function) elements
  8496. (dolist (al tprop)
  8497. (save-excursion
  8498. (put-text-property
  8499. (line-beginning-position) (or (outline-next-heading) (point-max))
  8500. (car al)
  8501. (funcall (cdr al) p))))))))
  8502. (defun org-refresh-category-properties ()
  8503. "Refresh category text properties in the buffer."
  8504. (let ((case-fold-search t)
  8505. (inhibit-read-only t)
  8506. (default-category
  8507. (cond ((null org-category)
  8508. (if buffer-file-name
  8509. (file-name-sans-extension
  8510. (file-name-nondirectory buffer-file-name))
  8511. "???"))
  8512. ((symbolp org-category) (symbol-name org-category))
  8513. (t org-category))))
  8514. (org-with-silent-modifications
  8515. (org-with-wide-buffer
  8516. ;; Set buffer-wide category. Search last #+CATEGORY keyword.
  8517. ;; This is the default category for the buffer. If none is
  8518. ;; found, fall-back to `org-category' or buffer file name.
  8519. (put-text-property
  8520. (point-min) (point-max)
  8521. 'org-category
  8522. (catch 'buffer-category
  8523. (goto-char (point-max))
  8524. (while (re-search-backward "^[ \t]*#\\+CATEGORY:" (point-min) t)
  8525. (let ((element (org-element-at-point)))
  8526. (when (eq (org-element-type element) 'keyword)
  8527. (throw 'buffer-category
  8528. (org-element-property :value element)))))
  8529. default-category))
  8530. ;; Set sub-tree specific categories.
  8531. (goto-char (point-min))
  8532. (let ((regexp (org-re-property "CATEGORY")))
  8533. (while (re-search-forward regexp nil t)
  8534. (let ((value (org-match-string-no-properties 3)))
  8535. (when (org-at-property-p)
  8536. (put-text-property
  8537. (save-excursion (org-back-to-heading t) (point))
  8538. (save-excursion (org-end-of-subtree t t) (point))
  8539. 'org-category
  8540. value)))))))))
  8541. (defun org-refresh-stats-properties ()
  8542. "Refresh stats text properties in the buffer."
  8543. (let (stats)
  8544. (org-with-silent-modifications
  8545. (save-excursion
  8546. (save-restriction
  8547. (widen)
  8548. (goto-char (point-min))
  8549. (while (re-search-forward
  8550. (concat org-outline-regexp-bol ".*"
  8551. "\\(?:\\[\\([0-9]+\\)%\\|\\([0-9]+\\)/\\([0-9]+\\)\\]\\)")
  8552. nil t)
  8553. (setq stats (cond ((equal (match-string 3) "0") 0)
  8554. ((match-string 2)
  8555. (/ (* (string-to-number (match-string 2)) 100)
  8556. (string-to-number (match-string 3))))
  8557. (t (string-to-number (match-string 1)))))
  8558. (org-back-to-heading t)
  8559. (put-text-property (point) (progn (org-end-of-subtree t t) (point))
  8560. 'org-stats stats)))))))
  8561. (defun org-refresh-effort-properties ()
  8562. "Refresh effort properties"
  8563. (org-refresh-properties
  8564. org-effort-property
  8565. '((effort . identity)
  8566. (effort-minutes . org-duration-string-to-minutes))))
  8567. ;;;; Link Stuff
  8568. ;;; Link abbreviations
  8569. (defun org-link-expand-abbrev (link)
  8570. "Apply replacements as defined in `org-link-abbrev-alist'."
  8571. (if (string-match "^\\([^:]*\\)\\(::?\\(.*\\)\\)?$" link)
  8572. (let* ((key (match-string 1 link))
  8573. (as (or (assoc key org-link-abbrev-alist-local)
  8574. (assoc key org-link-abbrev-alist)))
  8575. (tag (and (match-end 2) (match-string 3 link)))
  8576. rpl)
  8577. (if (not as)
  8578. link
  8579. (setq rpl (cdr as))
  8580. (cond
  8581. ((symbolp rpl) (funcall rpl tag))
  8582. ((string-match "%(\\([^)]+\\))" rpl)
  8583. (replace-match
  8584. (save-match-data
  8585. (funcall (intern-soft (match-string 1 rpl)) tag)) t t rpl))
  8586. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  8587. ((string-match "%h" rpl)
  8588. (replace-match (url-hexify-string (or tag "")) t t rpl))
  8589. (t (concat rpl tag)))))
  8590. link))
  8591. ;;; Storing and inserting links
  8592. (defvar org-insert-link-history nil
  8593. "Minibuffer history for links inserted with `org-insert-link'.")
  8594. (defvar org-stored-links nil
  8595. "Contains the links stored with `org-store-link'.")
  8596. (defvar org-store-link-plist nil
  8597. "Plist with info about the most recently link created with `org-store-link'.")
  8598. (defvar org-link-protocols nil
  8599. "Link protocols added to Org-mode using `org-add-link-type'.")
  8600. (defvar org-store-link-functions nil
  8601. "List of functions that are called to create and store a link.
  8602. Each function will be called in turn until one returns a non-nil
  8603. value. Each function should check if it is responsible for creating
  8604. this link (for example by looking at the major mode).
  8605. If not, it must exit and return nil.
  8606. If yes, it should return a non-nil value after a calling
  8607. `org-store-link-props' with a list of properties and values.
  8608. Special properties are:
  8609. :type The link prefix, like \"http\". This must be given.
  8610. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  8611. This is obligatory as well.
  8612. :description Optional default description for the second pair
  8613. of brackets in an Org-mode link. The user can still change
  8614. this when inserting this link into an Org-mode buffer.
  8615. In addition to these, any additional properties can be specified
  8616. and then used in capture templates.")
  8617. (defun org-add-link-type (type &optional follow export)
  8618. "Add TYPE to the list of `org-link-types'.
  8619. Re-compute all regular expressions depending on `org-link-types'
  8620. FOLLOW and EXPORT are two functions.
  8621. FOLLOW should take the link path as the single argument and do whatever
  8622. is necessary to follow the link, for example find a file or display
  8623. a mail message.
  8624. EXPORT should format the link path for export to one of the export formats.
  8625. It should be a function accepting three arguments:
  8626. path the path of the link, the text after the prefix (like \"http:\")
  8627. desc the description of the link, if any
  8628. format the export format, a symbol like `html' or `latex' or `ascii'.
  8629. The function may use the FORMAT information to return different values
  8630. depending on the format. The return value will be put literally into
  8631. the exported file. If the return value is nil, this means Org should
  8632. do what it normally does with links which do not have EXPORT defined.
  8633. Org mode has a built-in default for exporting links. If you are happy with
  8634. this default, there is no need to define an export function for the link
  8635. type. For a simple example of an export function, see `org-bbdb.el'."
  8636. (add-to-list 'org-link-types type t)
  8637. (org-make-link-regexps)
  8638. (org-element-update-syntax)
  8639. (if (assoc type org-link-protocols)
  8640. (setcdr (assoc type org-link-protocols) (list follow export))
  8641. (push (list type follow export) org-link-protocols)))
  8642. (defvar org-agenda-buffer-name) ; Defined in org-agenda.el
  8643. (defvar org-id-link-to-org-use-id) ; Defined in org-id.el
  8644. ;;;###autoload
  8645. (defun org-store-link (arg)
  8646. "\\<org-mode-map>Store an org-link to the current location.
  8647. This link is added to `org-stored-links' and can later be inserted
  8648. into an Org buffer with \\[org-insert-link].
  8649. For some link types, a prefix ARG is interpreted.
  8650. For links to Usenet articles, ARG negates `org-gnus-prefer-web-links'.
  8651. For file links, ARG negates `org-context-in-file-links'.
  8652. A double prefix ARG force skipping storing functions that are not
  8653. part of Org's core.
  8654. A triple prefix ARG force storing a link for each line in the
  8655. active region."
  8656. (interactive "P")
  8657. (org-load-modules-maybe)
  8658. (if (and (equal arg '(64)) (org-region-active-p))
  8659. (save-excursion
  8660. (let ((end (region-end)))
  8661. (goto-char (region-beginning))
  8662. (set-mark (point))
  8663. (while (< (point-at-eol) end)
  8664. (move-end-of-line 1) (activate-mark)
  8665. (let (current-prefix-arg)
  8666. (call-interactively 'org-store-link))
  8667. (move-beginning-of-line 2)
  8668. (set-mark (point)))))
  8669. (org-with-limited-levels
  8670. (setq org-store-link-plist nil)
  8671. (let (link cpltxt desc description search
  8672. txt custom-id agenda-link sfuns sfunsn)
  8673. (cond
  8674. ;; Store a link using an external link type
  8675. ((and (not (equal arg '(16)))
  8676. (setq sfuns
  8677. (delq
  8678. nil (mapcar (lambda (f)
  8679. (let (fs) (if (funcall f) (push f fs))))
  8680. org-store-link-functions))
  8681. sfunsn (mapcar (lambda (fu) (symbol-name (car fu))) sfuns))
  8682. (or (and (cdr sfuns)
  8683. (funcall (intern
  8684. (completing-read
  8685. "Which function for creating the link? "
  8686. sfunsn nil t (car sfunsn)))))
  8687. (funcall (caar sfuns)))
  8688. (setq link (plist-get org-store-link-plist :link)
  8689. desc (or (plist-get org-store-link-plist
  8690. :description)
  8691. link))))
  8692. ;; Store a link from a source code buffer.
  8693. ((org-src-edit-buffer-p)
  8694. (cond
  8695. ((save-excursion
  8696. (beginning-of-line)
  8697. (looking-at (concat (format org-coderef-label-format "\\(.*?\\)")
  8698. "[ \t]*$")))
  8699. (setq link (format "(%s)" (org-match-string-no-properties 1))))
  8700. ((org-called-interactively-p 'any)
  8701. (let (label)
  8702. (while (or (not label)
  8703. (org-with-wide-buffer
  8704. (goto-char (point-min))
  8705. (re-search-forward
  8706. (regexp-quote (format org-coderef-label-format label))
  8707. nil t)))
  8708. (when label (message "Label exists already") (sit-for 2))
  8709. (setq label (read-string "Code line label: " label)))
  8710. (end-of-line)
  8711. (setq link (format org-coderef-label-format label))
  8712. (let ((gc (- 79 (length link))))
  8713. (if (< (current-column) gc) (org-move-to-column gc t)
  8714. (insert " ")))
  8715. (insert link)
  8716. (setq link (concat "(" label ")") desc nil)))
  8717. (t (setq link nil))))
  8718. ;; We are in the agenda, link to referenced location
  8719. ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
  8720. (let ((m (or (get-text-property (point) 'org-hd-marker)
  8721. (get-text-property (point) 'org-marker))))
  8722. (when m
  8723. (org-with-point-at m
  8724. (setq agenda-link
  8725. (if (org-called-interactively-p 'any)
  8726. (call-interactively 'org-store-link)
  8727. (org-store-link nil)))))))
  8728. ((eq major-mode 'calendar-mode)
  8729. (let ((cd (calendar-cursor-to-date)))
  8730. (setq link
  8731. (format-time-string
  8732. (car org-time-stamp-formats)
  8733. (apply 'encode-time
  8734. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  8735. nil nil nil))))
  8736. (org-store-link-props :type "calendar" :date cd)))
  8737. ((eq major-mode 'help-mode)
  8738. (setq link (concat "help:" (save-excursion
  8739. (goto-char (point-min))
  8740. (looking-at "^[^ ]+")
  8741. (match-string 0))))
  8742. (org-store-link-props :type "help"))
  8743. ((eq major-mode 'w3-mode)
  8744. (setq cpltxt (if (and (buffer-name)
  8745. (not (string-match "Untitled" (buffer-name))))
  8746. (buffer-name)
  8747. (url-view-url t))
  8748. link (url-view-url t))
  8749. (org-store-link-props :type "w3" :url (url-view-url t)))
  8750. ((eq major-mode 'image-mode)
  8751. (setq cpltxt (concat "file:"
  8752. (abbreviate-file-name buffer-file-name))
  8753. link cpltxt)
  8754. (org-store-link-props :type "image" :file buffer-file-name))
  8755. ;; In dired, store a link to the file of the current line
  8756. ((derived-mode-p 'dired-mode)
  8757. (let ((file (dired-get-filename nil t)))
  8758. (setq file (if file
  8759. (abbreviate-file-name
  8760. (expand-file-name (dired-get-filename nil t)))
  8761. ;; otherwise, no file so use current directory.
  8762. default-directory))
  8763. (setq cpltxt (concat "file:" file)
  8764. link cpltxt)))
  8765. ((setq search (run-hook-with-args-until-success
  8766. 'org-create-file-search-functions))
  8767. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  8768. "::" search))
  8769. (setq cpltxt (or description link)))
  8770. ((and (buffer-file-name (buffer-base-buffer)) (derived-mode-p 'org-mode))
  8771. (setq custom-id (org-entry-get nil "CUSTOM_ID"))
  8772. (cond
  8773. ;; Store a link using the target at point
  8774. ((org-in-regexp "[^<]<<\\([^<>]+\\)>>[^>]" 1)
  8775. (setq cpltxt
  8776. (concat "file:"
  8777. (abbreviate-file-name
  8778. (buffer-file-name (buffer-base-buffer)))
  8779. "::" (match-string 1))
  8780. link cpltxt))
  8781. ((and (featurep 'org-id)
  8782. (or (eq org-id-link-to-org-use-id t)
  8783. (and (org-called-interactively-p 'any)
  8784. (or (eq org-id-link-to-org-use-id 'create-if-interactive)
  8785. (and (eq org-id-link-to-org-use-id
  8786. 'create-if-interactive-and-no-custom-id)
  8787. (not custom-id))))
  8788. (and org-id-link-to-org-use-id (org-entry-get nil "ID"))))
  8789. ;; Store a link using the ID at point
  8790. (setq link (condition-case nil
  8791. (prog1 (org-id-store-link)
  8792. (setq desc (or (plist-get org-store-link-plist
  8793. :description)
  8794. "")))
  8795. (error
  8796. ;; Probably before first headline, link only to file
  8797. (concat "file:"
  8798. (abbreviate-file-name
  8799. (buffer-file-name (buffer-base-buffer))))))))
  8800. (t
  8801. ;; Just link to current headline
  8802. (setq cpltxt (concat "file:"
  8803. (abbreviate-file-name
  8804. (buffer-file-name (buffer-base-buffer)))))
  8805. ;; Add a context search string
  8806. (when (org-xor org-context-in-file-links arg)
  8807. (let* ((ee (org-element-at-point))
  8808. (et (org-element-type ee))
  8809. (ev (plist-get (cadr ee) :value))
  8810. (ek (plist-get (cadr ee) :key))
  8811. (eok (and (stringp ek) (string-match "name" ek))))
  8812. (setq txt (cond
  8813. ((org-at-heading-p) nil)
  8814. ((and (eq et 'keyword) eok) ev)
  8815. ((org-region-active-p)
  8816. (buffer-substring (region-beginning) (region-end)))))
  8817. (when (or (null txt) (string-match "\\S-" txt))
  8818. (setq cpltxt
  8819. (concat cpltxt "::"
  8820. (condition-case nil
  8821. (org-make-org-heading-search-string txt)
  8822. (error "")))
  8823. desc (or (and (eq et 'keyword) eok ev)
  8824. (nth 4 (ignore-errors (org-heading-components)))
  8825. "NONE")))))
  8826. (when (string-match "::\\'" cpltxt)
  8827. (setq cpltxt (substring cpltxt 0 -2)))
  8828. (setq link cpltxt))))
  8829. ((buffer-file-name (buffer-base-buffer))
  8830. ;; Just link to this file here.
  8831. (setq cpltxt (concat "file:"
  8832. (abbreviate-file-name
  8833. (buffer-file-name (buffer-base-buffer)))))
  8834. ;; Add a context string.
  8835. (when (org-xor org-context-in-file-links arg)
  8836. (setq txt (if (org-region-active-p)
  8837. (buffer-substring (region-beginning) (region-end))
  8838. (buffer-substring (point-at-bol) (point-at-eol))))
  8839. ;; Only use search option if there is some text.
  8840. (when (string-match "\\S-" txt)
  8841. (setq cpltxt
  8842. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  8843. desc "NONE")))
  8844. (setq link cpltxt))
  8845. ((org-called-interactively-p 'interactive)
  8846. (user-error "No method for storing a link from this buffer"))
  8847. (t (setq link nil)))
  8848. ;; We're done setting link and desc, clean up
  8849. (when (consp link) (setq cpltxt (car link) link (cdr link)))
  8850. (setq link (or link cpltxt)
  8851. desc (or desc cpltxt))
  8852. (cond ((equal desc "NONE") (setq desc nil))
  8853. ((and desc (string-match org-bracket-link-analytic-regexp desc))
  8854. (let ((d0 (match-string 3 desc))
  8855. (p0 (match-string 5 desc)))
  8856. (setq desc
  8857. (replace-regexp-in-string
  8858. org-bracket-link-regexp
  8859. (concat (or p0 d0)
  8860. (if (equal (length (match-string 0 desc))
  8861. (length desc)) "*" "")) desc)))))
  8862. ;; Return the link
  8863. (if (not (and (or (org-called-interactively-p 'any)
  8864. executing-kbd-macro)
  8865. link))
  8866. (or agenda-link (and link (org-make-link-string link desc)))
  8867. (push (list link desc) org-stored-links)
  8868. (message "Stored: %s" (or desc link))
  8869. (when custom-id
  8870. (setq link (concat "file:" (abbreviate-file-name
  8871. (buffer-file-name)) "::#" custom-id))
  8872. (push (list link desc) org-stored-links))
  8873. (car org-stored-links))))))
  8874. (defun org-store-link-props (&rest plist)
  8875. "Store link properties, extract names, addresses and dates."
  8876. (let ((x (plist-get plist :from)))
  8877. (when x
  8878. (let ((adr (mail-extract-address-components x)))
  8879. (setq plist (plist-put plist :fromname (car adr)))
  8880. (setq plist (plist-put plist :fromaddress (nth 1 adr))))))
  8881. (let ((x (plist-get plist :to)))
  8882. (when x
  8883. (let ((adr (mail-extract-address-components x)))
  8884. (setq plist (plist-put plist :toname (car adr)))
  8885. (setq plist (plist-put plist :toaddress (nth 1 adr))))))
  8886. (let ((x (ignore-errors (date-to-time (plist-get plist :date)))))
  8887. (when x
  8888. (setq plist (plist-put plist :date-timestamp
  8889. (format-time-string
  8890. (org-time-stamp-format t) x)))
  8891. (setq plist (plist-put plist :date-timestamp-inactive
  8892. (format-time-string
  8893. (org-time-stamp-format t t) x)))))
  8894. (let ((from (plist-get plist :from))
  8895. (to (plist-get plist :to)))
  8896. (when (and from to org-from-is-user-regexp)
  8897. (setq plist
  8898. (plist-put plist :fromto
  8899. (if (string-match org-from-is-user-regexp from)
  8900. (concat "to %t")
  8901. (concat "from %f"))))))
  8902. (setq org-store-link-plist plist))
  8903. (defun org-add-link-props (&rest plist)
  8904. "Add these properties to the link property list."
  8905. (let (key value)
  8906. (while plist
  8907. (setq key (pop plist) value (pop plist))
  8908. (setq org-store-link-plist
  8909. (plist-put org-store-link-plist key value)))))
  8910. (defun org-email-link-description (&optional fmt)
  8911. "Return the description part of an email link.
  8912. This takes information from `org-store-link-plist' and formats it
  8913. according to FMT (default from `org-email-link-description-format')."
  8914. (setq fmt (or fmt org-email-link-description-format))
  8915. (let* ((p org-store-link-plist)
  8916. (to (plist-get p :toaddress))
  8917. (from (plist-get p :fromaddress))
  8918. (table
  8919. (list
  8920. (cons "%c" (plist-get p :fromto))
  8921. (cons "%F" (plist-get p :from))
  8922. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  8923. (cons "%T" (plist-get p :to))
  8924. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  8925. (cons "%s" (plist-get p :subject))
  8926. (cons "%d" (plist-get p :date))
  8927. (cons "%m" (plist-get p :message-id)))))
  8928. (when (string-match "%c" fmt)
  8929. ;; Check if the user wrote this message
  8930. (if (and org-from-is-user-regexp from to
  8931. (save-match-data (string-match org-from-is-user-regexp from)))
  8932. (setq fmt (replace-match "to %t" t t fmt))
  8933. (setq fmt (replace-match "from %f" t t fmt))))
  8934. (org-replace-escapes fmt table)))
  8935. (defun org-make-org-heading-search-string (&optional string)
  8936. "Make search string for the current headline or STRING."
  8937. (let ((s (or string
  8938. (and (derived-mode-p 'org-mode)
  8939. (save-excursion
  8940. (org-back-to-heading t)
  8941. (org-element-property :raw-value (org-element-at-point))))))
  8942. (lines org-context-in-file-links))
  8943. (or string (setq s (concat "*" s))) ; Add * for headlines
  8944. (setq s (replace-regexp-in-string "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" "" s))
  8945. (when (and string (integerp lines) (> lines 0))
  8946. (let ((slines (org-split-string s "\n")))
  8947. (when (< lines (length slines))
  8948. (setq s (mapconcat
  8949. 'identity
  8950. (reverse (nthcdr (- (length slines) lines)
  8951. (reverse slines))) "\n")))))
  8952. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  8953. (defun org-make-link-string (link &optional description)
  8954. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  8955. (unless (org-string-nw-p link) (error "Empty link"))
  8956. (let ((uri (cond ((string-match org-link-types-re link)
  8957. (concat (match-string 1 link)
  8958. (org-link-escape (substring link (match-end 1)))))
  8959. ;; For readability, url-encode internal links only
  8960. ;; when absolutely needed (i.e, when they contain
  8961. ;; square brackets). File links however, are
  8962. ;; encoded since, e.g., spaces are significant.
  8963. ((or (file-name-absolute-p link)
  8964. (org-string-match-p "\\`\\.\\.?/\\|[][]" link))
  8965. (org-link-escape link))
  8966. (t link)))
  8967. (description
  8968. (and (org-string-nw-p description)
  8969. ;; Remove brackets from description, as they are fatal.
  8970. (replace-regexp-in-string
  8971. "[][]" (lambda (m) (if (equal "[" m) "{" "}"))
  8972. (org-trim description)))))
  8973. (format "[[%s]%s]"
  8974. uri
  8975. (if description (format "[%s]" description) ""))))
  8976. (defconst org-link-escape-chars
  8977. ;;%20 %5B %5D %25
  8978. '(?\s ?\[ ?\] ?%)
  8979. "List of characters that should be escaped in a link when stored to Org.
  8980. This is the list that is used for internal purposes.")
  8981. (defconst org-link-escape-chars-browser
  8982. ;;%20 %22
  8983. '(?\s ?\")
  8984. "List of characters to be escaped before handing over to the browser.
  8985. If you consider using this constant then you probably want to use
  8986. the function `org-link-escape-browser' instead. See there why
  8987. this constant is a candidate to be removed once Org drops support
  8988. for Emacs 24.1 and 24.2.")
  8989. (defun org-link-escape (text &optional table merge)
  8990. "Return percent escaped representation of TEXT.
  8991. TEXT is a string with the text to escape.
  8992. Optional argument TABLE is a list with characters that should be
  8993. escaped. When nil, `org-link-escape-chars' is used.
  8994. If optional argument MERGE is set, merge TABLE into
  8995. `org-link-escape-chars'."
  8996. (let ((characters-to-encode
  8997. (cond ((null table) org-link-escape-chars)
  8998. (merge (append org-link-escape-chars table))
  8999. (t table))))
  9000. (mapconcat
  9001. (lambda (c)
  9002. (if (or (memq c characters-to-encode)
  9003. (and org-url-hexify-p (or (< c 32) (> c 126))))
  9004. (mapconcat (lambda (e) (format "%%%.2X" e))
  9005. (or (encode-coding-char c 'utf-8)
  9006. (error "Unable to percent escape character: %c" c))
  9007. "")
  9008. (char-to-string c)))
  9009. text "")))
  9010. (defun org-link-escape-browser (text)
  9011. "Escape some characters before handing over to the browser.
  9012. This function is a candidate to be removed together with the
  9013. constant `org-link-escape-chars-browser' once Org drops support
  9014. for Emacs 24.1 and 24.2. All calls to this function will have to
  9015. be replaced with `url-encode-url' which is available since Emacs
  9016. 24.3.1."
  9017. ;; Example with the Org link
  9018. ;; [[http://lists.gnu.org/archive/cgi-bin/namazu.cgi?idxname=emacs-orgmode&query=%252Bsubject:"Release+8.2"]]
  9019. ;; to open the browser with +subject:"Release 8.2" filled into the
  9020. ;; query field: In this case the variable TEXT contains the
  9021. ;; unescaped [...]=%2Bsubject:"Release+8.2". Then `url-encode-url'
  9022. ;; converts correctly to [...]=%2Bsubject:%22Release+8.2%22 or
  9023. ;; `org-link-escape' with `org-link-escape-chars-browser' converts
  9024. ;; wrongly to [...]=%252Bsubject:%22Release+8.2%22.
  9025. (if (fboundp 'url-encode-url)
  9026. (url-encode-url text)
  9027. (if (org-string-match-p
  9028. (concat "[[:nonascii:]" org-link-escape-chars-browser "]")
  9029. text)
  9030. (org-link-escape text org-link-escape-chars-browser)
  9031. text)))
  9032. (defun org-link-unescape (str)
  9033. "Unhex hexified Unicode parts in string STR.
  9034. E.g. `%C3%B6' becomes the german o-Umlaut. This is the
  9035. reciprocal of `org-link-escape', which see."
  9036. (if (org-string-nw-p str)
  9037. (replace-regexp-in-string
  9038. "\\(%[0-9A-Za-z]\\{2\\}\\)+" #'org-link-unescape-compound str t t)
  9039. str))
  9040. (defun org-link-unescape-compound (hex)
  9041. "Unhexify Unicode hex-chars. E.g. `%C3%B6' is the German o-Umlaut.
  9042. Note: this function also decodes single byte encodings like
  9043. `%E1' (a-acute) if not followed by another `%[A-F0-9]{2}' group."
  9044. (save-match-data
  9045. (let* ((bytes (cdr (split-string hex "%")))
  9046. (ret "")
  9047. (eat 0)
  9048. (sum 0))
  9049. (while bytes
  9050. (let* ((val (string-to-number (pop bytes) 16))
  9051. (shift-xor
  9052. (if (= 0 eat)
  9053. (cond
  9054. ((>= val 252) (cons 6 252))
  9055. ((>= val 248) (cons 5 248))
  9056. ((>= val 240) (cons 4 240))
  9057. ((>= val 224) (cons 3 224))
  9058. ((>= val 192) (cons 2 192))
  9059. (t (cons 0 0)))
  9060. (cons 6 128))))
  9061. (when (>= val 192) (setq eat (car shift-xor)))
  9062. (setq val (logxor val (cdr shift-xor)))
  9063. (setq sum (+ (lsh sum (car shift-xor)) val))
  9064. (when (> eat 0) (setq eat (- eat 1)))
  9065. (cond
  9066. ((= 0 eat) ;multi byte
  9067. (setq ret (concat ret (org-char-to-string sum)))
  9068. (setq sum 0))
  9069. ((not bytes) ; single byte(s)
  9070. (setq ret (org-link-unescape-single-byte-sequence hex))))))
  9071. ret)))
  9072. (defun org-link-unescape-single-byte-sequence (hex)
  9073. "Unhexify hex-encoded single byte character sequences."
  9074. (mapconcat (lambda (byte)
  9075. (char-to-string (string-to-number byte 16)))
  9076. (cdr (split-string hex "%")) ""))
  9077. (defun org-xor (a b)
  9078. "Exclusive or."
  9079. (if a (not b) b))
  9080. (defun org-fixup-message-id-for-http (s)
  9081. "Replace special characters in a message id, so it can be used in an http query."
  9082. (when (string-match "%" s)
  9083. (setq s (mapconcat (lambda (c)
  9084. (if (eq c ?%)
  9085. "%25"
  9086. (char-to-string c)))
  9087. s "")))
  9088. (while (string-match "<" s)
  9089. (setq s (replace-match "%3C" t t s)))
  9090. (while (string-match ">" s)
  9091. (setq s (replace-match "%3E" t t s)))
  9092. (while (string-match "@" s)
  9093. (setq s (replace-match "%40" t t s)))
  9094. s)
  9095. (defun org-link-prettify (link)
  9096. "Return a human-readable representation of LINK.
  9097. The car of LINK must be a raw link.
  9098. The cdr of LINK must be either a link description or nil."
  9099. (let ((desc (or (cadr link) "<no description>")))
  9100. (concat (format "%-45s" (substring desc 0 (min (length desc) 40)))
  9101. "<" (car link) ">")))
  9102. ;;;###autoload
  9103. (defun org-insert-link-global ()
  9104. "Insert a link like Org-mode does.
  9105. This command can be called in any mode to insert a link in Org-mode syntax."
  9106. (interactive)
  9107. (org-load-modules-maybe)
  9108. (org-run-like-in-org-mode 'org-insert-link))
  9109. (defun org-insert-all-links (arg &optional pre post)
  9110. "Insert all links in `org-stored-links'.
  9111. When a universal prefix, do not delete the links from `org-stored-links'.
  9112. When `ARG' is a number, insert the last N link(s).
  9113. `PRE' and `POST' are optional arguments to define a string to
  9114. prepend or to append."
  9115. (interactive "P")
  9116. (let ((org-keep-stored-link-after-insertion (equal arg '(4)))
  9117. (links (copy-sequence org-stored-links))
  9118. (pr (or pre "- "))
  9119. (po (or post "\n"))
  9120. (cnt 1) l)
  9121. (if (null org-stored-links)
  9122. (message "No link to insert")
  9123. (while (and (or (listp arg) (>= arg cnt))
  9124. (setq l (if (listp arg)
  9125. (pop links)
  9126. (pop org-stored-links))))
  9127. (setq cnt (1+ cnt))
  9128. (insert pr)
  9129. (org-insert-link nil (car l) (or (cadr l) "<no description>"))
  9130. (insert po)))))
  9131. (defun org-insert-last-stored-link (arg)
  9132. "Insert the last link stored in `org-stored-links'."
  9133. (interactive "p")
  9134. (org-insert-all-links arg "" "\n"))
  9135. (defun org-link-fontify-links-to-this-file ()
  9136. "Fontify links to the current file in `org-stored-links'."
  9137. (let ((f (buffer-file-name)) a b)
  9138. (setq a (mapcar (lambda(l)
  9139. (let ((ll (car l)))
  9140. (when (and (string-match "^file:\\(.+\\)::" ll)
  9141. (equal f (expand-file-name (match-string 1 ll))))
  9142. ll)))
  9143. org-stored-links))
  9144. (when (featurep 'org-id)
  9145. (setq b (mapcar (lambda(l)
  9146. (let ((ll (car l)))
  9147. (when (and (string-match "^id:\\(.+\\)$" ll)
  9148. (equal f (expand-file-name
  9149. (or (org-id-find-id-file
  9150. (match-string 1 ll)) ""))))
  9151. ll)))
  9152. org-stored-links)))
  9153. (mapcar (lambda(l)
  9154. (put-text-property 0 (length l) 'face 'font-lock-comment-face l))
  9155. (delq nil (append a b)))))
  9156. (defvar org--links-history nil)
  9157. (defun org-insert-link (&optional complete-file link-location default-description)
  9158. "Insert a link. At the prompt, enter the link.
  9159. Completion can be used to insert any of the link protocol prefixes like
  9160. http or ftp in use.
  9161. The history can be used to select a link previously stored with
  9162. `org-store-link'. When the empty string is entered (i.e. if you just
  9163. press RET at the prompt), the link defaults to the most recently
  9164. stored link. As SPC triggers completion in the minibuffer, you need to
  9165. use M-SPC or C-q SPC to force the insertion of a space character.
  9166. You will also be prompted for a description, and if one is given, it will
  9167. be displayed in the buffer instead of the link.
  9168. If there is already a link at point, this command will allow you to edit link
  9169. and description parts.
  9170. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  9171. be selected using completion. The path to the file will be relative to the
  9172. current directory if the file is in the current directory or a subdirectory.
  9173. Otherwise, the link will be the absolute path as completed in the minibuffer
  9174. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  9175. option `org-link-file-path-type'.
  9176. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  9177. the current directory or below.
  9178. With three \\[universal-argument] prefixes, negate the meaning of
  9179. `org-keep-stored-link-after-insertion'.
  9180. If `org-make-link-description-function' is non-nil, this function will be
  9181. called with the link target, and the result will be the default
  9182. link description.
  9183. If the LINK-LOCATION parameter is non-nil, this value will be
  9184. used as the link location instead of reading one interactively.
  9185. If the DEFAULT-DESCRIPTION parameter is non-nil, this value will
  9186. be used as the default description."
  9187. (interactive "P")
  9188. (let* ((wcf (current-window-configuration))
  9189. (origbuf (current-buffer))
  9190. (region (when (org-region-active-p)
  9191. (buffer-substring (region-beginning) (region-end))))
  9192. (remove (and region (list (region-beginning) (region-end))))
  9193. (desc region)
  9194. (link link-location)
  9195. (abbrevs org-link-abbrev-alist-local)
  9196. entry all-prefixes auto-desc)
  9197. (cond
  9198. (link-location) ; specified by arg, just use it.
  9199. ((org-in-regexp org-bracket-link-regexp 1)
  9200. ;; We do have a link at point, and we are going to edit it.
  9201. (setq remove (list (match-beginning 0) (match-end 0)))
  9202. (setq desc (when (match-end 3) (org-match-string-no-properties 3)))
  9203. (setq link (read-string "Link: "
  9204. (org-link-unescape
  9205. (org-match-string-no-properties 1)))))
  9206. ((or (org-in-regexp org-angle-link-re)
  9207. (org-in-regexp org-plain-link-re))
  9208. ;; Convert to bracket link
  9209. (setq remove (list (match-beginning 0) (match-end 0))
  9210. link (read-string "Link: "
  9211. (org-remove-angle-brackets (match-string 0)))))
  9212. ((member complete-file '((4) (16)))
  9213. ;; Completing read for file names.
  9214. (setq link (org-file-complete-link complete-file)))
  9215. (t
  9216. ;; Read link, with completion for stored links.
  9217. (org-link-fontify-links-to-this-file)
  9218. (org-switch-to-buffer-other-window "*Org Links*")
  9219. (with-current-buffer "*Org Links*"
  9220. (erase-buffer)
  9221. (insert "Insert a link.
  9222. Use TAB to complete link prefixes, then RET for type-specific completion support\n")
  9223. (when org-stored-links
  9224. (insert "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  9225. (insert (mapconcat 'org-link-prettify
  9226. (reverse org-stored-links) "\n")))
  9227. (goto-char (point-min)))
  9228. (let ((cw (selected-window)))
  9229. (select-window (get-buffer-window "*Org Links*" 'visible))
  9230. (with-current-buffer "*Org Links*" (setq truncate-lines t))
  9231. (unless (pos-visible-in-window-p (point-max))
  9232. (org-fit-window-to-buffer))
  9233. (and (window-live-p cw) (select-window cw)))
  9234. (setq all-prefixes (append (mapcar 'car abbrevs)
  9235. (mapcar 'car org-link-abbrev-alist)
  9236. org-link-types))
  9237. (unwind-protect
  9238. ;; Fake a link history, containing the stored links.
  9239. (let ((org--links-history
  9240. (append (mapcar #'car org-stored-links)
  9241. org-insert-link-history)))
  9242. (setq link
  9243. (org-completing-read
  9244. "Link: "
  9245. (append
  9246. (mapcar (lambda (x) (concat x ":")) all-prefixes)
  9247. (mapcar #'car org-stored-links))
  9248. nil nil nil
  9249. 'org--links-history
  9250. (caar org-stored-links)))
  9251. (unless (org-string-nw-p link) (user-error "No link selected"))
  9252. (dolist (l org-stored-links)
  9253. (when (equal link (cadr l))
  9254. (setq link (car l))
  9255. (setq auto-desc t)))
  9256. (when (or (member link all-prefixes)
  9257. (and (equal ":" (substring link -1))
  9258. (member (substring link 0 -1) all-prefixes)
  9259. (setq link (substring link 0 -1))))
  9260. (setq link (with-current-buffer origbuf
  9261. (org-link-try-special-completion link)))))
  9262. (set-window-configuration wcf)
  9263. (kill-buffer "*Org Links*"))
  9264. (setq entry (assoc link org-stored-links))
  9265. (or entry (push link org-insert-link-history))
  9266. (setq desc (or desc (nth 1 entry)))))
  9267. (when (funcall (if (equal complete-file '(64)) 'not 'identity)
  9268. (not org-keep-stored-link-after-insertion))
  9269. (setq org-stored-links (delq (assoc link org-stored-links)
  9270. org-stored-links)))
  9271. (when (and (string-match org-plain-link-re link)
  9272. (not (string-match org-ts-regexp link)))
  9273. ;; URL-like link, normalize the use of angular brackets.
  9274. (setq link (org-remove-angle-brackets link)))
  9275. ;; Check if we are linking to the current file with a search
  9276. ;; option If yes, simplify the link by using only the search
  9277. ;; option.
  9278. (when (and buffer-file-name
  9279. (string-match "^file:\\(.+?\\)::\\(.+\\)" link))
  9280. (let* ((path (match-string 1 link))
  9281. (case-fold-search nil)
  9282. (search (match-string 2 link)))
  9283. (save-match-data
  9284. (when (equal (file-truename buffer-file-name) (file-truename path))
  9285. ;; We are linking to this same file, with a search option
  9286. (setq link search)))))
  9287. ;; Check if we can/should use a relative path. If yes, simplify the link
  9288. (when (string-match "^\\(file:\\|docview:\\)\\(.*\\)" link)
  9289. (let* ((type (match-string 1 link))
  9290. (path (match-string 2 link))
  9291. (origpath path)
  9292. (case-fold-search nil))
  9293. (cond
  9294. ((or (eq org-link-file-path-type 'absolute)
  9295. (equal complete-file '(16)))
  9296. (setq path (abbreviate-file-name (expand-file-name path))))
  9297. ((eq org-link-file-path-type 'noabbrev)
  9298. (setq path (expand-file-name path)))
  9299. ((eq org-link-file-path-type 'relative)
  9300. (setq path (file-relative-name path)))
  9301. (t
  9302. (save-match-data
  9303. (if (string-match (concat "^" (regexp-quote
  9304. (expand-file-name
  9305. (file-name-as-directory
  9306. default-directory))))
  9307. (expand-file-name path))
  9308. ;; We are linking a file with relative path name.
  9309. (setq path (substring (expand-file-name path)
  9310. (match-end 0)))
  9311. (setq path (abbreviate-file-name (expand-file-name path)))))))
  9312. (setq link (concat type path))
  9313. (when (equal desc origpath)
  9314. (setq desc path))))
  9315. (if org-make-link-description-function
  9316. (setq desc
  9317. (or (condition-case nil
  9318. (funcall org-make-link-description-function link desc)
  9319. (error (progn (message "Can't get link description from `%s'"
  9320. (symbol-name org-make-link-description-function))
  9321. (sit-for 2) nil)))
  9322. (read-string "Description: " default-description)))
  9323. (if default-description (setq desc default-description)
  9324. (setq desc (or (and auto-desc desc)
  9325. (read-string "Description: " desc)))))
  9326. (unless (string-match "\\S-" desc) (setq desc nil))
  9327. (when remove (apply 'delete-region remove))
  9328. (insert (org-make-link-string link desc))
  9329. ;; Redisplay so as the new link has proper invisible characters.
  9330. (sit-for 0)))
  9331. (defun org-link-try-special-completion (type)
  9332. "If there is completion support for link type TYPE, offer it."
  9333. (let ((fun (intern (concat "org-" type "-complete-link"))))
  9334. (if (functionp fun)
  9335. (funcall fun)
  9336. (read-string "Link (no completion support): " (concat type ":")))))
  9337. (defun org-file-complete-link (&optional arg)
  9338. "Create a file link using completion."
  9339. (let ((file (org-iread-file-name "File: "))
  9340. (pwd (file-name-as-directory (expand-file-name ".")))
  9341. (pwd1 (file-name-as-directory (abbreviate-file-name
  9342. (expand-file-name ".")))))
  9343. (cond ((equal arg '(16))
  9344. (concat "file:"
  9345. (abbreviate-file-name (expand-file-name file))))
  9346. ((string-match
  9347. (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  9348. (concat "file:" (match-string 1 file)))
  9349. ((string-match
  9350. (concat "^" (regexp-quote pwd) "\\(.+\\)")
  9351. (expand-file-name file))
  9352. (concat "file:"
  9353. (match-string 1 (expand-file-name file))))
  9354. (t (concat "file:" file)))))
  9355. (defvar ido-enter-matching-directory)
  9356. (defun org-iread-file-name (&rest args)
  9357. "Read-file-name using `ido-mode' speedup if available.
  9358. ARGS are arguments that may be passed to `ido-read-file-name' or `read-file-name'.
  9359. See `read-file-name' for a description of parameters."
  9360. (org-without-partial-completion
  9361. (if (and org-completion-use-ido
  9362. (fboundp 'ido-read-file-name)
  9363. (org-bound-and-true-p ido-mode)
  9364. (listp (nth 1 args)))
  9365. (let ((ido-enter-matching-directory nil))
  9366. (apply #'ido-read-file-name args))
  9367. (apply #'read-file-name args))))
  9368. (defun org-completing-read (&rest args)
  9369. "Completing-read with SPACE being a normal character."
  9370. (let ((enable-recursive-minibuffers t)
  9371. (minibuffer-local-completion-map
  9372. (copy-keymap minibuffer-local-completion-map)))
  9373. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  9374. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  9375. (org-defkey minibuffer-local-completion-map (kbd "C-c !")
  9376. 'org-time-stamp-inactive)
  9377. (apply #'completing-read args)))
  9378. (define-obsolete-function-alias
  9379. 'org-completing-read-no-i 'completing-read "Org 9.0")
  9380. (define-obsolete-function-alias
  9381. 'org-icompleting-read 'completing-read "Org 9.0")
  9382. ;;; Opening/following a link
  9383. (defvar org-link-search-failed nil)
  9384. (defvar org-open-link-functions nil
  9385. "Hook for functions finding a plain text link.
  9386. These functions must take a single argument, the link content.
  9387. They will be called for links that look like [[link text][description]]
  9388. when LINK TEXT does not have a protocol like \"http:\" and does not look
  9389. like a filename (e.g. \"./blue.png\").
  9390. These functions will be called *before* Org attempts to resolve the
  9391. link by doing text searches in the current buffer - so if you want a
  9392. link \"[[target]]\" to still find \"<<target>>\", your function should
  9393. handle this as a special case.
  9394. When the function does handle the link, it must return a non-nil value.
  9395. If it decides that it is not responsible for this link, it must return
  9396. nil to indicate that that Org-mode can continue with other options
  9397. like exact and fuzzy text search.")
  9398. (defun org-next-link (&optional search-backward)
  9399. "Move forward to the next link.
  9400. If the link is in hidden text, expose it."
  9401. (interactive "P")
  9402. (when (and org-link-search-failed (eq this-command last-command))
  9403. (goto-char (point-min))
  9404. (message "Link search wrapped back to beginning of buffer"))
  9405. (setq org-link-search-failed nil)
  9406. (let* ((pos (point))
  9407. (ct (org-context))
  9408. (a (assoc :link ct))
  9409. (srch-fun (if search-backward 're-search-backward 're-search-forward)))
  9410. (cond (a (goto-char (nth (if search-backward 1 2) a)))
  9411. ((looking-at org-any-link-re)
  9412. ;; Don't stay stuck at link without an org-link face
  9413. (forward-char (if search-backward -1 1))))
  9414. (if (funcall srch-fun org-any-link-re nil t)
  9415. (progn
  9416. (goto-char (match-beginning 0))
  9417. (when (outline-invisible-p) (org-show-context)))
  9418. (goto-char pos)
  9419. (setq org-link-search-failed t)
  9420. (message "No further link found"))))
  9421. (defun org-previous-link ()
  9422. "Move backward to the previous link.
  9423. If the link is in hidden text, expose it."
  9424. (interactive)
  9425. (funcall 'org-next-link t))
  9426. (defun org-translate-link (s)
  9427. "Translate a link string if a translation function has been defined."
  9428. (with-temp-buffer
  9429. (insert (org-trim s))
  9430. (org-trim (org-element-interpret-data (org-element-context)))))
  9431. (defun org-translate-link-from-planner (type path)
  9432. "Translate a link from Emacs Planner syntax so that Org can follow it.
  9433. This is still an experimental function, your mileage may vary."
  9434. (cond
  9435. ((member type '("http" "https" "news" "ftp"))
  9436. ;; standard Internet links are the same.
  9437. nil)
  9438. ((and (equal type "irc") (string-match "^//" path))
  9439. ;; Planner has two / at the beginning of an irc link, we have 1.
  9440. ;; We should have zero, actually....
  9441. (setq path (substring path 1)))
  9442. ((and (equal type "lisp") (string-match "^/" path))
  9443. ;; Planner has a slash, we do not.
  9444. (setq type "elisp" path (substring path 1)))
  9445. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  9446. ;; A typical message link. Planner has the id after the final slash,
  9447. ;; we separate it with a hash mark
  9448. (setq path (concat (match-string 1 path) "#"
  9449. (org-remove-angle-brackets (match-string 2 path))))))
  9450. (cons type path))
  9451. (defun org-find-file-at-mouse (ev)
  9452. "Open file link or URL at mouse."
  9453. (interactive "e")
  9454. (mouse-set-point ev)
  9455. (org-open-at-point 'in-emacs))
  9456. (defun org-open-at-mouse (ev)
  9457. "Open file link or URL at mouse.
  9458. See the docstring of `org-open-file' for details."
  9459. (interactive "e")
  9460. (mouse-set-point ev)
  9461. (when (eq major-mode 'org-agenda-mode)
  9462. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  9463. (org-open-at-point))
  9464. (defvar org-window-config-before-follow-link nil
  9465. "The window configuration before following a link.
  9466. This is saved in case the need arises to restore it.")
  9467. ;;;###autoload
  9468. (defun org-open-at-point-global ()
  9469. "Follow a link like Org-mode does.
  9470. This command can be called in any mode to follow a link that has
  9471. Org-mode syntax."
  9472. (interactive)
  9473. (org-run-like-in-org-mode 'org-open-at-point))
  9474. ;;;###autoload
  9475. (defun org-open-link-from-string (s &optional arg reference-buffer)
  9476. "Open a link in the string S, as if it was in Org-mode."
  9477. (interactive "sLink: \nP")
  9478. (let ((reference-buffer (or reference-buffer (current-buffer))))
  9479. (with-temp-buffer
  9480. (let ((org-inhibit-startup (not reference-buffer)))
  9481. (org-mode)
  9482. (insert s)
  9483. (goto-char (point-min))
  9484. (when reference-buffer
  9485. (setq org-link-abbrev-alist-local
  9486. (with-current-buffer reference-buffer
  9487. org-link-abbrev-alist-local)))
  9488. (org-open-at-point arg reference-buffer)))))
  9489. (defvar org-open-at-point-functions nil
  9490. "Hook that is run when following a link at point.
  9491. Functions in this hook must return t if they identify and follow
  9492. a link at point. If they don't find anything interesting at point,
  9493. they must return nil.")
  9494. (defvar org-link-search-inhibit-query nil) ;; dynamically scoped
  9495. (defvar clean-buffer-list-kill-buffer-names) ; Defined in midnight.el
  9496. (defun org-open-at-point (&optional arg reference-buffer)
  9497. "Open link, timestamp, footnote or tags at point.
  9498. When point is on a link, follow it. Normally, files will be
  9499. opened by an appropriate application. If the optional prefix
  9500. argument ARG is non-nil, Emacs will visit the file. With
  9501. a double prefix argument, try to open outside of Emacs, in the
  9502. application the system uses for this file type.
  9503. When point is on a timestamp, open the agenda at the day
  9504. specified.
  9505. When point is a footnote definition, move to the first reference
  9506. found. If it is on a reference, move to the associated
  9507. definition.
  9508. When point is on a headline, display a list of every link in the
  9509. entry, so it is possible to pick one, or all, of them. If point
  9510. is on a tag, call `org-tags-view' instead.
  9511. When optional argument REFERENCE-BUFFER is non-nil, it should
  9512. specify a buffer from where the link search should happen. This
  9513. is used internally by `org-open-link-from-string'.
  9514. On top of syntactically correct links, this function will open
  9515. the link at point in comments or comment blocks and the first
  9516. link in a property drawer line."
  9517. (interactive "P")
  9518. ;; On a code block, open block's results.
  9519. (unless (call-interactively 'org-babel-open-src-block-result)
  9520. (org-load-modules-maybe)
  9521. (setq org-window-config-before-follow-link (current-window-configuration))
  9522. (org-remove-occur-highlights nil nil t)
  9523. (unless (run-hook-with-args-until-success 'org-open-at-point-functions)
  9524. (let* ((context
  9525. ;; Only consider supported types, even if they are not
  9526. ;; the closest one.
  9527. (org-element-lineage
  9528. (org-element-context)
  9529. '(clock comment comment-block footnote-definition
  9530. footnote-reference headline inlinetask keyword link
  9531. node-property timestamp)
  9532. t))
  9533. (type (org-element-type context))
  9534. (value (org-element-property :value context)))
  9535. (cond
  9536. ((not context) (user-error "No link found"))
  9537. ;; Exception: open timestamps and links in properties
  9538. ;; drawers, keywords and comments.
  9539. ((memq type '(comment comment-block keyword node-property))
  9540. (cond ((org-in-regexp org-any-link-re)
  9541. (org-open-link-from-string (match-string-no-properties 0)))
  9542. ((or (org-in-regexp org-ts-regexp-both nil t)
  9543. (org-in-regexp org-tsr-regexp-both nil t))
  9544. (org-follow-timestamp-link))
  9545. (t (user-error "No link found"))))
  9546. ;; On a headline or an inlinetask, but not on a timestamp,
  9547. ;; a link, a footnote reference or on tags.
  9548. ((and (memq type '(headline inlinetask))
  9549. ;; Not on tags.
  9550. (progn (save-excursion (beginning-of-line)
  9551. (looking-at org-complex-heading-regexp))
  9552. (or (not (match-beginning 5))
  9553. (< (point) (match-beginning 5)))))
  9554. (let* ((data (org-offer-links-in-entry (current-buffer) (point) arg))
  9555. (links (car data))
  9556. (links-end (cdr data)))
  9557. (if links
  9558. (dolist (link (if (stringp links) (list links) links))
  9559. (search-forward link nil links-end)
  9560. (goto-char (match-beginning 0))
  9561. (org-open-at-point))
  9562. (require 'org-attach)
  9563. (org-attach-reveal 'if-exists))))
  9564. ;; On a clock line, make sure point is on the timestamp
  9565. ;; before opening it.
  9566. ((and (eq type 'clock)
  9567. value
  9568. (>= (point) (org-element-property :begin value))
  9569. (<= (point) (org-element-property :end value)))
  9570. (org-follow-timestamp-link))
  9571. ;; Do nothing on white spaces after an object.
  9572. ((>= (point)
  9573. (save-excursion
  9574. (goto-char (org-element-property :end context))
  9575. (skip-chars-backward " \t")
  9576. (point)))
  9577. (user-error "No link found"))
  9578. ((eq type 'timestamp) (org-follow-timestamp-link))
  9579. ;; On tags within a headline or an inlinetask.
  9580. ((and (memq type '(headline inlinetask))
  9581. (progn (save-excursion (beginning-of-line)
  9582. (looking-at org-complex-heading-regexp))
  9583. (and (match-beginning 5)
  9584. (>= (point) (match-beginning 5)))))
  9585. (org-tags-view arg (substring (match-string 5) 0 -1)))
  9586. ((eq type 'link)
  9587. ;; When link is located within the description of another
  9588. ;; link (e.g., an inline image), always open the parent
  9589. ;; link.
  9590. (let*((link (let ((up (org-element-property :parent context)))
  9591. (if (eq (org-element-type up) 'link) up context)))
  9592. (type (org-element-property :type link))
  9593. (path (org-link-unescape (org-element-property :path link))))
  9594. ;; Switch back to REFERENCE-BUFFER needed when called in
  9595. ;; a temporary buffer through `org-open-link-from-string'.
  9596. (with-current-buffer (or reference-buffer (current-buffer))
  9597. (cond
  9598. ((equal type "file")
  9599. (if (string-match "[*?{]" (file-name-nondirectory path))
  9600. (dired path)
  9601. ;; Look into `org-link-protocols' in order to find
  9602. ;; a DEDICATED-FUNCTION to open file. The function
  9603. ;; will be applied on raw link instead of parsed
  9604. ;; link due to the limitation in `org-add-link-type'
  9605. ;; ("open" function called with a single argument).
  9606. ;; If no such function is found, fallback to
  9607. ;; `org-open-file'.
  9608. ;;
  9609. ;; Note : "file+emacs" and "file+sys" types are
  9610. ;; hard-coded in order to escape the previous
  9611. ;; limitation.
  9612. (let* ((option (org-element-property :search-option link))
  9613. (app (org-element-property :application link))
  9614. (dedicated-function
  9615. (nth 1 (assoc app org-link-protocols))))
  9616. (if dedicated-function
  9617. (funcall dedicated-function
  9618. (concat path
  9619. (and option (concat "::" option))))
  9620. (apply #'org-open-file
  9621. path
  9622. (cond (arg)
  9623. ((equal app "emacs") 'emacs)
  9624. ((equal app "sys") 'system))
  9625. (cond ((not option) nil)
  9626. ((org-string-match-p "\\`[0-9]+\\'" option)
  9627. (list (string-to-number option)))
  9628. (t (list nil
  9629. (org-link-unescape option)))))))))
  9630. ((assoc type org-link-protocols)
  9631. (funcall (nth 1 (assoc type org-link-protocols)) path))
  9632. ((equal type "help")
  9633. (let ((f-or-v (intern path)))
  9634. (cond ((fboundp f-or-v) (describe-function f-or-v))
  9635. ((boundp f-or-v) (describe-variable f-or-v))
  9636. (t (error "Not a known function or variable")))))
  9637. ((member type '("http" "https" "ftp" "mailto" "news"))
  9638. (browse-url (org-link-escape-browser (concat type ":" path))))
  9639. ((equal type "doi")
  9640. (browse-url
  9641. (org-link-escape-browser (concat org-doi-server-url path))))
  9642. ((equal type "message") (browse-url (concat type ":" path)))
  9643. ((equal type "shell")
  9644. (let ((buf (generate-new-buffer "*Org Shell Output*"))
  9645. (cmd path))
  9646. (if (or (and (org-string-nw-p
  9647. org-confirm-shell-link-not-regexp)
  9648. (string-match
  9649. org-confirm-shell-link-not-regexp cmd))
  9650. (not org-confirm-shell-link-function)
  9651. (funcall org-confirm-shell-link-function
  9652. (format "Execute \"%s\" in shell? "
  9653. (org-add-props cmd nil
  9654. 'face 'org-warning))))
  9655. (progn
  9656. (message "Executing %s" cmd)
  9657. (shell-command cmd buf)
  9658. (when (featurep 'midnight)
  9659. (setq clean-buffer-list-kill-buffer-names
  9660. (cons (buffer-name buf)
  9661. clean-buffer-list-kill-buffer-names))))
  9662. (user-error "Abort"))))
  9663. ((equal type "elisp")
  9664. (let ((cmd path))
  9665. (if (or (and (org-string-nw-p
  9666. org-confirm-elisp-link-not-regexp)
  9667. (org-string-match-p
  9668. org-confirm-elisp-link-not-regexp cmd))
  9669. (not org-confirm-elisp-link-function)
  9670. (funcall org-confirm-elisp-link-function
  9671. (format "Execute \"%s\" as elisp? "
  9672. (org-add-props cmd nil
  9673. 'face 'org-warning))))
  9674. (message "%s => %s" cmd
  9675. (if (eq (string-to-char cmd) ?\()
  9676. (eval (read cmd))
  9677. (call-interactively (read cmd))))
  9678. (user-error "Abort"))))
  9679. ((equal type "id")
  9680. (require 'org-id)
  9681. (funcall (nth 1 (assoc "id" org-link-protocols)) path))
  9682. ((member type '("coderef" "custom-id" "fuzzy" "radio"))
  9683. (unless (run-hook-with-args-until-success
  9684. 'org-open-link-functions path)
  9685. (if (not arg) (org-mark-ring-push)
  9686. (switch-to-buffer-other-window
  9687. (org-get-buffer-for-internal-link (current-buffer))))
  9688. (let ((destination
  9689. (org-with-wide-buffer
  9690. (if (equal type "radio")
  9691. (org-search-radio-target
  9692. (org-element-property :path link))
  9693. (org-link-search
  9694. (if (member type '("custom-id" "coderef"))
  9695. (org-element-property :raw-link link)
  9696. path)
  9697. ;; Prevent fuzzy links from matching
  9698. ;; themselves.
  9699. (and (equal type "fuzzy")
  9700. (+ 2 (org-element-property :begin link)))))
  9701. (point))))
  9702. (unless (and (<= (point-min) destination)
  9703. (>= (point-max) destination))
  9704. (widen))
  9705. (goto-char destination))))
  9706. (t (browse-url-at-point))))))
  9707. ;; On a footnote reference or at a footnote definition's label.
  9708. ((or (eq type 'footnote-reference)
  9709. (and (eq type 'footnote-definition)
  9710. (save-excursion
  9711. ;; Do not validate action when point is on the
  9712. ;; spaces right after the footnote label, in
  9713. ;; order to be on par with behaviour on links.
  9714. (skip-chars-forward " \t")
  9715. (let ((begin
  9716. (org-element-property :contents-begin context)))
  9717. (if begin (< (point) begin)
  9718. (= (org-element-property :post-affiliated context)
  9719. (line-beginning-position)))))))
  9720. (org-footnote-action))
  9721. (t (user-error "No link found")))))
  9722. (run-hook-with-args 'org-follow-link-hook)))
  9723. (defun org-offer-links-in-entry (buffer marker &optional nth zero)
  9724. "Offer links in the current entry and return the selected link.
  9725. If there is only one link, return it.
  9726. If NTH is an integer, return the NTH link found.
  9727. If ZERO is a string, check also this string for a link, and if
  9728. there is one, return it."
  9729. (with-current-buffer buffer
  9730. (save-excursion
  9731. (save-restriction
  9732. (widen)
  9733. (goto-char marker)
  9734. (let ((cnt ?0)
  9735. have-zero end links link c)
  9736. (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
  9737. (push (match-string 0 zero) links)
  9738. (setq cnt (1- cnt) have-zero t))
  9739. (save-excursion
  9740. (org-back-to-heading t)
  9741. (setq end (save-excursion (outline-next-heading) (point)))
  9742. (while (re-search-forward org-any-link-re end t)
  9743. (push (match-string 0) links))
  9744. (setq links (org-uniquify (reverse links))))
  9745. (cond
  9746. ((null links)
  9747. (message "No links"))
  9748. ((equal (length links) 1)
  9749. (setq link (car links)))
  9750. ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
  9751. (setq link (nth (if have-zero nth (1- nth)) links)))
  9752. (t ; we have to select a link
  9753. (save-excursion
  9754. (save-window-excursion
  9755. (delete-other-windows)
  9756. (with-output-to-temp-buffer "*Select Link*"
  9757. (dolist (l links)
  9758. (cond
  9759. ((not (string-match org-bracket-link-regexp l))
  9760. (princ (format "[%c] %s\n" (cl-incf cnt)
  9761. (org-remove-angle-brackets l))))
  9762. ((match-end 3)
  9763. (princ (format "[%c] %s (%s)\n" (cl-incf cnt)
  9764. (match-string 3 l) (match-string 1 l))))
  9765. (t (princ (format "[%c] %s\n" (cl-incf cnt)
  9766. (match-string 1 l)))))))
  9767. (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
  9768. (message "Select link to open, RET to open all:")
  9769. (setq c (read-char-exclusive))
  9770. (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
  9771. (when (equal c ?q) (user-error "Abort"))
  9772. (if (equal c ?\C-m)
  9773. (setq link links)
  9774. (setq nth (- c ?0))
  9775. (when have-zero (setq nth (1+ nth)))
  9776. (unless (and (integerp nth) (>= (length links) nth))
  9777. (user-error "Invalid link selection"))
  9778. (setq link (nth (1- nth) links)))))
  9779. (cons link end))))))
  9780. ;; TODO: These functions are deprecated since `org-open-at-point'
  9781. ;; hard-codes behaviour for "file+emacs" and "file+sys" types.
  9782. (defun org-open-file-with-system (path)
  9783. "Open file at PATH using the system way of opening it."
  9784. (org-open-file path 'system))
  9785. (defun org-open-file-with-emacs (path)
  9786. "Open file at PATH in Emacs."
  9787. (org-open-file path 'emacs))
  9788. ;;; File search
  9789. (defvar org-create-file-search-functions nil
  9790. "List of functions to construct the right search string for a file link.
  9791. These functions are called in turn with point at the location to
  9792. which the link should point.
  9793. A function in the hook should first test if it would like to
  9794. handle this file type, for example by checking the `major-mode'
  9795. or the file extension. If it decides not to handle this file, it
  9796. should just return nil to give other functions a chance. If it
  9797. does handle the file, it must return the search string to be used
  9798. when following the link. The search string will be part of the
  9799. file link, given after a double colon, and `org-open-at-point'
  9800. will automatically search for it. If special measures must be
  9801. taken to make the search successful, another function should be
  9802. added to the companion hook `org-execute-file-search-functions',
  9803. which see.
  9804. A function in this hook may also use `setq' to set the variable
  9805. `description' to provide a suggestion for the descriptive text to
  9806. be used for this link when it gets inserted into an Org-mode
  9807. buffer with \\[org-insert-link].")
  9808. (defvar org-execute-file-search-functions nil
  9809. "List of functions to execute a file search triggered by a link.
  9810. Functions added to this hook must accept a single argument, the
  9811. search string that was part of the file link, the part after the
  9812. double colon. The function must first check if it would like to
  9813. handle this search, for example by checking the `major-mode' or
  9814. the file extension. If it decides not to handle this search, it
  9815. should just return nil to give other functions a chance. If it
  9816. does handle the search, it must return a non-nil value to keep
  9817. other functions from trying.
  9818. Each function can access the current prefix argument through the
  9819. variable `current-prefix-arg'. Note that a single prefix is used
  9820. to force opening a link in Emacs, so it may be good to only use a
  9821. numeric or double prefix to guide the search function.
  9822. In case this is needed, a function in this hook can also restore
  9823. the window configuration before `org-open-at-point' was called using:
  9824. \(set-window-configuration org-window-config-before-follow-link)")
  9825. (defun org-search-radio-target (target)
  9826. "Search a radio target matching TARGET in current buffer.
  9827. White spaces are not significant."
  9828. (let ((re (format "<<<%s>>>"
  9829. (mapconcat #'regexp-quote
  9830. (org-split-string target "[ \t\n]+")
  9831. "[ \t]+\\(?:\n[ \t]*\\)?")))
  9832. (origin (point)))
  9833. (goto-char (point-min))
  9834. (catch :radio-match
  9835. (while (re-search-forward re nil t)
  9836. (backward-char)
  9837. (let ((object (org-element-context)))
  9838. (when (eq (org-element-type object) 'radio-target)
  9839. (goto-char (org-element-property :begin object))
  9840. (org-show-context 'link-search)
  9841. (throw :radio-match nil))))
  9842. (goto-char origin)
  9843. (user-error "No match for radio target: %s" target))))
  9844. (defun org-link-search (s &optional avoid-pos stealth)
  9845. "Search for a search string S.
  9846. If S starts with \"#\", it triggers a custom ID search.
  9847. If S is enclosed within parenthesis, it initiates a coderef
  9848. search.
  9849. If S is surrounded by forward slashes, it is interpreted as
  9850. a regular expression. In Org mode files, this will create an
  9851. `org-occur' sparse tree. In ordinary files, `occur' will be used
  9852. to list matches. If the current buffer is in `dired-mode', grep
  9853. will be used to search in all files.
  9854. When AVOID-POS is given, ignore matches near that position.
  9855. When optional argument STEALTH is non-nil, do not modify
  9856. visibility around point, thus ignoring `org-show-context-detail'
  9857. variable.
  9858. Search is case-insensitive and ignores white spaces. Return type
  9859. of matched result, with is either `dedicated' or `fuzzy'."
  9860. (unless (org-string-nw-p s) (error "Invalid search string \"%s\"" s))
  9861. (let* ((case-fold-search t)
  9862. (origin (point))
  9863. (normalized (replace-regexp-in-string "\n[ \t]*" " " s))
  9864. (words (org-split-string s "[ \t\n]+"))
  9865. (s-multi-re (mapconcat #'regexp-quote words "[ \t]+\\(?:\n[ \t]*\\)?"))
  9866. (s-single-re (mapconcat #'regexp-quote words "[ \t]+"))
  9867. type)
  9868. (cond
  9869. ;; Check if there are any special search functions.
  9870. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  9871. ((eq (string-to-char s) ?#)
  9872. ;; Look for a custom ID S if S starts with "#".
  9873. (let* ((id (substring normalized 1))
  9874. (match (org-find-property "CUSTOM_ID" id)))
  9875. (if match (progn (goto-char match) (setf type 'dedicated))
  9876. (error "No match for custom ID: %s" id))))
  9877. ((string-match "\\`(\\(.*\\))\\'" normalized)
  9878. ;; Look for coderef targets if S is enclosed within parenthesis.
  9879. (let ((coderef (match-string-no-properties 1 normalized))
  9880. (re (substring s-single-re 1 -1)))
  9881. (goto-char (point-min))
  9882. (catch :coderef-match
  9883. (while (re-search-forward re nil t)
  9884. (let ((element (org-element-at-point)))
  9885. (when (and (memq (org-element-type element)
  9886. '(example-block src-block))
  9887. ;; Build proper regexp according to current
  9888. ;; block's label format.
  9889. (let ((label-fmt
  9890. (regexp-quote
  9891. (or (org-element-property :label-fmt element)
  9892. org-coderef-label-format))))
  9893. (save-excursion
  9894. (beginning-of-line)
  9895. (looking-at (format ".*?\\(%s\\)[ \t]*$"
  9896. (format label-fmt coderef))))))
  9897. (setq type 'dedicated)
  9898. (goto-char (match-beginning 1))
  9899. (throw :coderef-match nil))))
  9900. (goto-char origin)
  9901. (error "No match for coderef: %s" coderef))))
  9902. ((string-match "\\`/\\(.*\\)/\\'" normalized)
  9903. ;; Look for a regular expression.
  9904. (funcall (if (derived-mode-p 'org-mode) #'org-occur #'org-do-occur)
  9905. (match-string 1 s)))
  9906. ;; Fuzzy links.
  9907. (t
  9908. (let ((starred (eq (string-to-char normalized) ?*)))
  9909. (cond
  9910. ;; Look for targets, only if not in a headline search.
  9911. ((and (not starred)
  9912. (let ((target (format "<<%s>>" s-multi-re)))
  9913. (catch :target-match
  9914. (goto-char (point-min))
  9915. (while (re-search-forward target nil t)
  9916. (backward-char)
  9917. (let ((context (org-element-context)))
  9918. (when (eq (org-element-type context) 'target)
  9919. (setq type 'dedicated)
  9920. (goto-char (org-element-property :begin context))
  9921. (throw :target-match t))))
  9922. nil))))
  9923. ;; Look for elements named after S, only if not in a headline
  9924. ;; search.
  9925. ((and (not starred)
  9926. (let ((name (format "^[ \t]*#\\+NAME: +%s[ \t]*$" s-single-re)))
  9927. (catch :name-match
  9928. (goto-char (point-min))
  9929. (while (re-search-forward name nil t)
  9930. (let ((element (org-element-at-point)))
  9931. (when (equal (org-split-string
  9932. (org-element-property :name element)
  9933. "[ \t]+")
  9934. words)
  9935. (setq type 'dedicated)
  9936. (beginning-of-line)
  9937. (throw :name-match t))))
  9938. nil))))
  9939. ;; Regular text search. Prefer headlines in Org mode
  9940. ;; buffers.
  9941. ((and (derived-mode-p 'org-mode)
  9942. (let* ((wspace "[ \t]")
  9943. (wspaceopt (concat wspace "*"))
  9944. (cookie (concat "\\(?:"
  9945. wspaceopt
  9946. "\\[[0-9]*\\(?:%\\|/[0-9]*\\)\\]"
  9947. wspaceopt
  9948. "\\)"))
  9949. (sep (concat "\\(?:\\(?:" wspace "\\|" cookie "\\)+\\)"))
  9950. (re (concat
  9951. org-outline-regexp-bol
  9952. "\\(?:" org-todo-regexp "[ \t]+\\)?"
  9953. "\\(?:\\[#.\\][ \t]+\\)?"
  9954. "\\(?:" org-comment-string "[ \t]+\\)?"
  9955. sep "?"
  9956. (let ((title (mapconcat #'regexp-quote
  9957. words
  9958. sep)))
  9959. (if starred (substring title 1) title))
  9960. sep "?"
  9961. (org-re "\\(?:[ \t]+:[[:alnum:]_@#%%:]+:\\)?")
  9962. "[ \t]*$")))
  9963. (goto-char (point-min))
  9964. (re-search-forward re nil t)))
  9965. (goto-char (match-beginning 0))
  9966. (setq type 'dedicated))
  9967. ;; Offer to create non-existent headline depending on
  9968. ;; `org-link-search-must-match-exact-headline'.
  9969. ((and (derived-mode-p 'org-mode)
  9970. (not org-link-search-inhibit-query)
  9971. (eq org-link-search-must-match-exact-headline 'query-to-create)
  9972. (yes-or-no-p "No match - create this as a new heading? "))
  9973. (goto-char (point-max))
  9974. (unless (bolp) (newline))
  9975. (org-insert-heading nil t t)
  9976. (insert s "\n")
  9977. (beginning-of-line 0))
  9978. ;; Only headlines are looked after. No need to process
  9979. ;; further: throw an error.
  9980. ((and (derived-mode-p 'org-mode)
  9981. (or starred org-link-search-must-match-exact-headline))
  9982. (goto-char origin)
  9983. (error "No match for fuzzy expression: %s" normalized))
  9984. ;; Regular text search.
  9985. ((catch :fuzzy-match
  9986. (goto-char (point-min))
  9987. (while (re-search-forward s-multi-re nil t)
  9988. ;; Skip match if it contains AVOID-POS or it is included
  9989. ;; in a link with a description but outside the
  9990. ;; description.
  9991. (unless (or (and avoid-pos
  9992. (<= (match-beginning 0) avoid-pos)
  9993. (> (match-end 0) avoid-pos))
  9994. (and (save-match-data
  9995. (org-in-regexp org-bracket-link-regexp))
  9996. (match-beginning 3)
  9997. (or (> (match-beginning 3) (point))
  9998. (<= (match-end 3) (point)))
  9999. (org-element-lineage
  10000. (save-match-data (org-element-context))
  10001. '(link) t)))
  10002. (goto-char (match-beginning 0))
  10003. (setq type 'fuzzy)
  10004. (throw :fuzzy-match t)))
  10005. nil))
  10006. ;; All failed. Throw an error.
  10007. (t (goto-char origin)
  10008. (error "No match for fuzzy expression: %s" normalized))))))
  10009. ;; Disclose surroundings of match, if appropriate.
  10010. (when (and (derived-mode-p 'org-mode) (not stealth))
  10011. (org-show-context 'link-search))
  10012. type))
  10013. (defun org-get-buffer-for-internal-link (buffer)
  10014. "Return a buffer to be used for displaying the link target of internal links."
  10015. (cond
  10016. ((not org-display-internal-link-with-indirect-buffer)
  10017. buffer)
  10018. ((string-match "(Clone)$" (buffer-name buffer))
  10019. (message "Buffer is already a clone, not making another one")
  10020. ;; we also do not modify visibility in this case
  10021. buffer)
  10022. (t ; make a new indirect buffer for displaying the link
  10023. (let* ((bn (buffer-name buffer))
  10024. (ibn (concat bn "(Clone)"))
  10025. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  10026. (with-current-buffer ib (org-overview))
  10027. ib))))
  10028. (defun org-do-occur (regexp &optional cleanup)
  10029. "Call the Emacs command `occur'.
  10030. If CLEANUP is non-nil, remove the printout of the regular expression
  10031. in the *Occur* buffer. This is useful if the regex is long and not useful
  10032. to read."
  10033. (occur regexp)
  10034. (when cleanup
  10035. (let ((cwin (selected-window)) win beg end)
  10036. (when (setq win (get-buffer-window "*Occur*"))
  10037. (select-window win))
  10038. (goto-char (point-min))
  10039. (when (re-search-forward "match[a-z]+" nil t)
  10040. (setq beg (match-end 0))
  10041. (when (re-search-forward "^[ \t]*[0-9]+" nil t)
  10042. (setq end (1- (match-beginning 0)))))
  10043. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  10044. (goto-char (point-min))
  10045. (select-window cwin))))
  10046. ;;; The mark ring for links jumps
  10047. (defvar org-mark-ring nil
  10048. "Mark ring for positions before jumps in Org-mode.")
  10049. (defvar org-mark-ring-last-goto nil
  10050. "Last position in the mark ring used to go back.")
  10051. ;; Fill and close the ring
  10052. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  10053. (dotimes (_ org-mark-ring-length)
  10054. (push (make-marker) org-mark-ring))
  10055. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  10056. org-mark-ring)
  10057. (defun org-mark-ring-push (&optional pos buffer)
  10058. "Put the current position or POS into the mark ring and rotate it."
  10059. (interactive)
  10060. (setq pos (or pos (point)))
  10061. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  10062. (move-marker (car org-mark-ring)
  10063. (or pos (point))
  10064. (or buffer (current-buffer)))
  10065. (message "%s"
  10066. (substitute-command-keys
  10067. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  10068. (defun org-mark-ring-goto (&optional n)
  10069. "Jump to the previous position in the mark ring.
  10070. With prefix arg N, jump back that many stored positions. When
  10071. called several times in succession, walk through the entire ring.
  10072. Org-mode commands jumping to a different position in the current file,
  10073. or to another Org-mode file, automatically push the old position
  10074. onto the ring."
  10075. (interactive "p")
  10076. (let (p m)
  10077. (if (eq last-command this-command)
  10078. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  10079. (setq p org-mark-ring))
  10080. (setq org-mark-ring-last-goto p)
  10081. (setq m (car p))
  10082. (org-pop-to-buffer-same-window (marker-buffer m))
  10083. (goto-char m)
  10084. (when (or (outline-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  10085. ;;; TODO: Use string-remove-prefix and -suffix once we only support
  10086. ;;; Emacs 24.4+
  10087. (defun org-remove-angle-brackets (s)
  10088. (when (equal (substring s 0 1) "<") (setq s (substring s 1)))
  10089. (when (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  10090. s)
  10091. (defun org-add-angle-brackets (s)
  10092. (unless (equal (substring s 0 1) "<") (setq s (concat "<" s)))
  10093. (unless (equal (substring s -1) ">") (setq s (concat s ">")))
  10094. s)
  10095. (defun org-remove-double-quotes (s)
  10096. (when (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  10097. (when (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  10098. s)
  10099. ;;; Following specific links
  10100. (defvar org-agenda-buffer-tmp-name)
  10101. (defvar org-agenda-start-on-weekday)
  10102. (defun org-follow-timestamp-link ()
  10103. "Open an agenda view for the time-stamp date/range at point."
  10104. (cond
  10105. ((org-at-date-range-p t)
  10106. (let ((org-agenda-start-on-weekday)
  10107. (t1 (match-string 1))
  10108. (t2 (match-string 2)) tt1 tt2)
  10109. (setq tt1 (time-to-days (org-time-string-to-time t1))
  10110. tt2 (time-to-days (org-time-string-to-time t2)))
  10111. (let ((org-agenda-buffer-tmp-name
  10112. (format "*Org Agenda(a:%s)"
  10113. (concat (substring t1 0 10) "--" (substring t2 0 10)))))
  10114. (org-agenda-list nil tt1 (1+ (- tt2 tt1))))))
  10115. ((org-at-timestamp-p t)
  10116. (let ((org-agenda-buffer-tmp-name
  10117. (format "*Org Agenda(a:%s)" (substring (match-string 1) 0 10))))
  10118. (org-agenda-list nil (time-to-days (org-time-string-to-time
  10119. (substring (match-string 1) 0 10)))
  10120. 1)))
  10121. (t (error "This should not happen"))))
  10122. ;;; Following file links
  10123. (declare-function mailcap-parse-mailcaps "mailcap" (&optional path force))
  10124. (declare-function mailcap-extension-to-mime "mailcap" (extn))
  10125. (declare-function mailcap-mime-info
  10126. "mailcap" (string &optional request no-decode))
  10127. (defvar org-wait nil)
  10128. (defun org-open-file (path &optional in-emacs line search)
  10129. "Open the file at PATH.
  10130. First, this expands any special file name abbreviations. Then the
  10131. configuration variable `org-file-apps' is checked if it contains an
  10132. entry for this file type, and if yes, the corresponding command is launched.
  10133. If no application is found, Emacs simply visits the file.
  10134. With optional prefix argument IN-EMACS, Emacs will visit the file.
  10135. With a double \\[universal-argument] \\[universal-argument] \
  10136. prefix arg, Org tries to avoid opening in Emacs
  10137. and to use an external application to visit the file.
  10138. Optional LINE specifies a line to go to, optional SEARCH a string
  10139. to search for. If LINE or SEARCH is given, the file will be
  10140. opened in Emacs, unless an entry from org-file-apps that makes
  10141. use of groups in a regexp matches.
  10142. If you want to change the way frames are used when following a
  10143. link, please customize `org-link-frame-setup'.
  10144. If the file does not exist, an error is thrown."
  10145. (let* ((file (if (equal path "")
  10146. buffer-file-name
  10147. (substitute-in-file-name (expand-file-name path))))
  10148. (file-apps (append org-file-apps (org-default-apps)))
  10149. (apps (cl-remove-if
  10150. 'org-file-apps-entry-match-against-dlink-p file-apps))
  10151. (apps-dlink (cl-remove-if-not
  10152. 'org-file-apps-entry-match-against-dlink-p file-apps))
  10153. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  10154. (dirp (unless remp (file-directory-p file)))
  10155. (file (if (and dirp org-open-directory-means-index-dot-org)
  10156. (concat (file-name-as-directory file) "index.org")
  10157. file))
  10158. (a-m-a-p (assq 'auto-mode apps))
  10159. (dfile (downcase file))
  10160. ;; reconstruct the original file: link from the PATH, LINE and SEARCH args
  10161. (link (cond ((and (eq line nil)
  10162. (eq search nil))
  10163. file)
  10164. (line
  10165. (concat file "::" (number-to-string line)))
  10166. (search
  10167. (concat file "::" search))))
  10168. (dlink (downcase link))
  10169. (old-buffer (current-buffer))
  10170. (old-pos (point))
  10171. (old-mode major-mode)
  10172. ext cmd link-match-data)
  10173. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  10174. (setq ext (match-string 1 dfile))
  10175. (when (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  10176. (setq ext (match-string 1 dfile))))
  10177. (cond
  10178. ((member in-emacs '((16) system))
  10179. (setq cmd (cdr (assoc 'system apps))))
  10180. (in-emacs (setq cmd 'emacs))
  10181. (t
  10182. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  10183. (and dirp (cdr (assoc 'directory apps)))
  10184. ; first, try matching against apps-dlink
  10185. ; if we get a match here, store the match data for later
  10186. (let ((match (assoc-default dlink apps-dlink
  10187. 'string-match)))
  10188. (if match
  10189. (progn (setq link-match-data (match-data))
  10190. match)
  10191. (progn (setq in-emacs (or in-emacs line search))
  10192. nil))) ; if we have no match in apps-dlink,
  10193. ; always open the file in emacs if line or search
  10194. ; is given (for backwards compatibility)
  10195. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  10196. 'string-match)
  10197. (cdr (assoc ext apps))
  10198. (cdr (assoc t apps))))))
  10199. (when (eq cmd 'system)
  10200. (setq cmd (cdr (assoc 'system apps))))
  10201. (when (eq cmd 'default)
  10202. (setq cmd (cdr (assoc t apps))))
  10203. (when (eq cmd 'mailcap)
  10204. (require 'mailcap)
  10205. (mailcap-parse-mailcaps)
  10206. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  10207. (command (mailcap-mime-info mime-type)))
  10208. (if (stringp command)
  10209. (setq cmd command)
  10210. (setq cmd 'emacs))))
  10211. (when (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  10212. (not (file-exists-p file))
  10213. (not org-open-non-existing-files))
  10214. (user-error "No such file: %s" file))
  10215. (cond
  10216. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  10217. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  10218. (while (string-match "['\"]%s['\"]" cmd)
  10219. (setq cmd (replace-match "%s" t t cmd)))
  10220. (while (string-match "%s" cmd)
  10221. (setq cmd (replace-match
  10222. (save-match-data
  10223. (shell-quote-argument
  10224. (convert-standard-filename file)))
  10225. t t cmd)))
  10226. ;; Replace "%1", "%2" etc. in command with group matches from regex
  10227. (save-match-data
  10228. (let ((match-index 1)
  10229. (number-of-groups (- (/ (length link-match-data) 2) 1)))
  10230. (set-match-data link-match-data)
  10231. (while (<= match-index number-of-groups)
  10232. (let ((regex (concat "%" (number-to-string match-index)))
  10233. (replace-with (match-string match-index dlink)))
  10234. (while (string-match regex cmd)
  10235. (setq cmd (replace-match replace-with t t cmd))))
  10236. (setq match-index (+ match-index 1)))))
  10237. (save-window-excursion
  10238. (message "Running %s...done" cmd)
  10239. (start-process-shell-command cmd nil cmd)
  10240. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))))
  10241. ((or (stringp cmd)
  10242. (eq cmd 'emacs))
  10243. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  10244. (widen)
  10245. (if line (progn (org-goto-line line)
  10246. (when (derived-mode-p 'org-mode)
  10247. (org-reveal)))
  10248. (when search (org-link-search search))))
  10249. ((consp cmd)
  10250. (save-match-data
  10251. (set-match-data link-match-data)
  10252. (eval cmd `((file . ,(convert-standard-filename file))))))
  10253. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  10254. (and (derived-mode-p 'org-mode) (eq old-mode 'org-mode)
  10255. (or (not (equal old-buffer (current-buffer)))
  10256. (not (equal old-pos (point))))
  10257. (org-mark-ring-push old-pos old-buffer))))
  10258. (defun org-file-apps-entry-match-against-dlink-p (entry)
  10259. "This function returns non-nil if `entry' uses a regular
  10260. expression which should be matched against the whole link by
  10261. org-open-file.
  10262. It assumes that is the case when the entry uses a regular
  10263. expression which has at least one grouping construct and the
  10264. action is either a lisp form or a command string containing
  10265. `%1', i.e. using at least one subexpression match as a
  10266. parameter."
  10267. (let ((selector (car entry))
  10268. (action (cdr entry)))
  10269. (if (stringp selector)
  10270. (and (> (regexp-opt-depth selector) 0)
  10271. (or (and (stringp action)
  10272. (string-match "%[0-9]" action))
  10273. (consp action)))
  10274. nil)))
  10275. (defun org-default-apps ()
  10276. "Return the default applications for this operating system."
  10277. (cond
  10278. ((eq system-type 'darwin)
  10279. org-file-apps-defaults-macosx)
  10280. ((eq system-type 'windows-nt)
  10281. org-file-apps-defaults-windowsnt)
  10282. (t org-file-apps-defaults-gnu)))
  10283. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  10284. "Convert extensions to regular expressions in the cars of LIST.
  10285. Also, weed out any non-string entries, because the return value is used
  10286. only for regexp matching.
  10287. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  10288. point to the symbol `emacs', indicating that the file should
  10289. be opened in Emacs."
  10290. (append
  10291. (delq nil
  10292. (mapcar (lambda (x)
  10293. (unless (not (stringp (car x)))
  10294. (if (string-match "\\W" (car x))
  10295. x
  10296. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  10297. list))
  10298. (when add-auto-mode
  10299. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  10300. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  10301. (defun org-file-remote-p (file)
  10302. "Test whether FILE specifies a location on a remote system.
  10303. Return non-nil if the location is indeed remote.
  10304. For example, the filename \"/user@host:/foo\" specifies a location
  10305. on the system \"/user@host:\"."
  10306. (cond ((fboundp 'file-remote-p)
  10307. (file-remote-p file))
  10308. ((fboundp 'tramp-handle-file-remote-p)
  10309. (tramp-handle-file-remote-p file))
  10310. ((and (boundp 'ange-ftp-name-format)
  10311. (string-match (car ange-ftp-name-format) file))
  10312. t)))
  10313. ;;;; Refiling
  10314. (defun org-get-org-file ()
  10315. "Read a filename, with default directory `org-directory'."
  10316. (let ((default (or org-default-notes-file remember-data-file)))
  10317. (read-file-name (format "File name [%s]: " default)
  10318. (file-name-as-directory org-directory)
  10319. default)))
  10320. (defun org-notes-order-reversed-p ()
  10321. "Check if the current file should receive notes in reversed order."
  10322. (cond
  10323. ((not org-reverse-note-order) nil)
  10324. ((eq t org-reverse-note-order) t)
  10325. ((not (listp org-reverse-note-order)) nil)
  10326. (t (catch 'exit
  10327. (dolist (entry org-reverse-note-order)
  10328. (when (string-match (car entry) buffer-file-name)
  10329. (throw 'exit (cdr entry))))))))
  10330. (defvar org-refile-target-table nil
  10331. "The list of refile targets, created by `org-refile'.")
  10332. (defvar org-agenda-new-buffers nil
  10333. "Buffers created to visit agenda files.")
  10334. (defvar org-refile-cache nil
  10335. "Cache for refile targets.")
  10336. (defvar org-refile-markers nil
  10337. "All the markers used for caching refile locations.")
  10338. (defun org-refile-marker (pos)
  10339. "Get a new refile marker, but only if caching is in use."
  10340. (if (not org-refile-use-cache)
  10341. pos
  10342. (let ((m (make-marker)))
  10343. (move-marker m pos)
  10344. (push m org-refile-markers)
  10345. m)))
  10346. (defun org-refile-cache-clear ()
  10347. "Clear the refile cache and disable all the markers."
  10348. (dolist (m org-refile-markers) (move-marker m nil))
  10349. (setq org-refile-markers nil)
  10350. (setq org-refile-cache nil)
  10351. (message "Refile cache has been cleared"))
  10352. (defun org-refile-cache-check-set (set)
  10353. "Check if all the markers in the cache still have live buffers."
  10354. (let (marker)
  10355. (catch 'exit
  10356. (while (and set (setq marker (nth 3 (pop set))))
  10357. ;; If `org-refile-use-outline-path' is 'file, marker may be nil
  10358. (when (and marker (null (marker-buffer marker)))
  10359. (message "Please regenerate the refile cache with `C-0 C-c C-w'")
  10360. (sit-for 3)
  10361. (throw 'exit nil)))
  10362. t)))
  10363. (defun org-refile-cache-put (set &rest identifiers)
  10364. "Push the refile targets SET into the cache, under IDENTIFIERS."
  10365. (let* ((key (sha1 (prin1-to-string identifiers)))
  10366. (entry (assoc key org-refile-cache)))
  10367. (if entry
  10368. (setcdr entry set)
  10369. (push (cons key set) org-refile-cache))))
  10370. (defun org-refile-cache-get (&rest identifiers)
  10371. "Retrieve the cached value for refile targets given by IDENTIFIERS."
  10372. (cond
  10373. ((not org-refile-cache) nil)
  10374. ((not org-refile-use-cache) (org-refile-cache-clear) nil)
  10375. (t
  10376. (let ((set (cdr (assoc (sha1 (prin1-to-string identifiers))
  10377. org-refile-cache))))
  10378. (and set (org-refile-cache-check-set set) set)))))
  10379. (defun org-refile-get-targets (&optional default-buffer excluded-entries)
  10380. "Produce a table with refile targets."
  10381. (let ((case-fold-search nil)
  10382. ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
  10383. (entries (or org-refile-targets '((nil . (:level . 1)))))
  10384. targets tgs txt re files desc descre fast-path-p level pos0)
  10385. (message "Getting targets...")
  10386. (with-current-buffer (or default-buffer (current-buffer))
  10387. (dolist (entry entries)
  10388. (setq files (car entry) desc (cdr entry))
  10389. (setq fast-path-p nil)
  10390. (cond
  10391. ((null files) (setq files (list (current-buffer))))
  10392. ((eq files 'org-agenda-files)
  10393. (setq files (org-agenda-files 'unrestricted)))
  10394. ((and (symbolp files) (fboundp files))
  10395. (setq files (funcall files)))
  10396. ((and (symbolp files) (boundp files))
  10397. (setq files (symbol-value files))))
  10398. (when (stringp files) (setq files (list files)))
  10399. (cond
  10400. ((eq (car desc) :tag)
  10401. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  10402. ((eq (car desc) :todo)
  10403. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  10404. ((eq (car desc) :regexp)
  10405. (setq descre (cdr desc)))
  10406. ((eq (car desc) :level)
  10407. (setq descre (concat "^\\*\\{" (number-to-string
  10408. (if org-odd-levels-only
  10409. (1- (* 2 (cdr desc)))
  10410. (cdr desc)))
  10411. "\\}[ \t]")))
  10412. ((eq (car desc) :maxlevel)
  10413. (setq fast-path-p t)
  10414. (setq descre (concat "^\\*\\{1," (number-to-string
  10415. (if org-odd-levels-only
  10416. (1- (* 2 (cdr desc)))
  10417. (cdr desc)))
  10418. "\\}[ \t]")))
  10419. (t (error "Bad refiling target description %s" desc)))
  10420. (dolist (f files)
  10421. (with-current-buffer
  10422. (if (bufferp f) f (org-get-agenda-file-buffer f))
  10423. (or
  10424. (setq tgs (org-refile-cache-get (buffer-file-name) descre))
  10425. (progn
  10426. (when (bufferp f) (setq f (buffer-file-name
  10427. (buffer-base-buffer f))))
  10428. (setq f (and f (expand-file-name f)))
  10429. (when (eq org-refile-use-outline-path 'file)
  10430. (push (list (file-name-nondirectory f) f nil nil) tgs))
  10431. (save-excursion
  10432. (save-restriction
  10433. (widen)
  10434. (goto-char (point-min))
  10435. (while (re-search-forward descre nil t)
  10436. (goto-char (setq pos0 (point-at-bol)))
  10437. (catch 'next
  10438. (when org-refile-target-verify-function
  10439. (save-match-data
  10440. (or (funcall org-refile-target-verify-function)
  10441. (throw 'next t))))
  10442. (when (and (looking-at org-complex-heading-regexp)
  10443. (not (member (match-string 4) excluded-entries))
  10444. (match-string 4))
  10445. (setq level (org-reduced-level
  10446. (- (match-end 1) (match-beginning 1)))
  10447. txt (org-link-display-format (match-string 4))
  10448. txt (replace-regexp-in-string "\\( *[[0-9]+/?[0-9]*%?]\\)+$" "" txt)
  10449. re (format org-complex-heading-regexp-format
  10450. (regexp-quote (match-string 4))))
  10451. (when org-refile-use-outline-path
  10452. (setq txt (mapconcat
  10453. 'org-protect-slash
  10454. (append
  10455. (if (eq org-refile-use-outline-path
  10456. 'file)
  10457. (list (file-name-nondirectory
  10458. (buffer-file-name
  10459. (buffer-base-buffer))))
  10460. (when (eq org-refile-use-outline-path
  10461. 'full-file-path)
  10462. (list (buffer-file-name
  10463. (buffer-base-buffer)))))
  10464. (org-get-outline-path fast-path-p
  10465. level txt)
  10466. (list txt))
  10467. "/")))
  10468. (push (list txt f re (org-refile-marker (point)))
  10469. tgs)))
  10470. (when (= (point) pos0)
  10471. ;; verification function has not moved point
  10472. (goto-char (point-at-eol))))))))
  10473. (when org-refile-use-cache
  10474. (org-refile-cache-put tgs (buffer-file-name) descre))
  10475. (setq targets (append tgs targets))))))
  10476. (message "Getting targets...done")
  10477. (nreverse targets)))
  10478. (defun org-protect-slash (s)
  10479. (while (string-match "/" s)
  10480. (setq s (replace-match "\\" t t s)))
  10481. s)
  10482. (defvar org-olpa (make-vector 20 nil))
  10483. (defun org-get-outline-path (&optional fastp level heading)
  10484. "Return the outline path to the current entry, as a list.
  10485. The parameters FASTP, LEVEL, and HEADING are for use by a scanner
  10486. routine which makes outline path derivations for an entire file,
  10487. avoiding backtracing. Refile target collection makes use of that."
  10488. (if fastp
  10489. (progn
  10490. (when (> level 19)
  10491. (error "Outline path failure, more than 19 levels"))
  10492. (cl-loop for i from level upto 19 do
  10493. (aset org-olpa i nil))
  10494. (prog1
  10495. (delq nil (append org-olpa nil))
  10496. (aset org-olpa level heading)))
  10497. (let (rtn case-fold-search)
  10498. (save-excursion
  10499. (save-restriction
  10500. (widen)
  10501. (while (org-up-heading-safe)
  10502. (when (looking-at org-complex-heading-regexp)
  10503. (push (org-trim
  10504. (replace-regexp-in-string
  10505. ;; Remove statistical/checkboxes cookies
  10506. "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" ""
  10507. (org-match-string-no-properties 4)))
  10508. rtn)))
  10509. rtn)))))
  10510. (defun org-format-outline-path (path &optional width prefix separator)
  10511. "Format the outline path PATH for display.
  10512. WIDTH is the maximum number of characters that is available.
  10513. PREFIX is a prefix to be included in the returned string,
  10514. such as the file name.
  10515. SEPARATOR is inserted between the different parts of the path,
  10516. the default is \"/\"."
  10517. (setq width (or width 79))
  10518. (setq path (delq nil path))
  10519. (unless (> width 0)
  10520. (user-error "Argument `width' must be positive"))
  10521. (setq separator (or separator "/"))
  10522. (let* ((org-odd-levels-only nil)
  10523. (fpath (concat
  10524. prefix (and prefix path separator)
  10525. (mapconcat
  10526. (lambda (s) (replace-regexp-in-string "[ \t]+\\'" "" s))
  10527. (cl-loop for head in path
  10528. for n from 0
  10529. collect (org-add-props
  10530. head nil 'face
  10531. (nth (% n org-n-level-faces) org-level-faces)))
  10532. separator))))
  10533. (when (> (length fpath) width)
  10534. (if (< width 7)
  10535. ;; It's unlikely that `width' will be this small, but don't
  10536. ;; waste characters by adding ".." if it is.
  10537. (setq fpath (substring fpath 0 width))
  10538. (setf (substring fpath (- width 2)) "..")))
  10539. fpath))
  10540. (defun org-display-outline-path (&optional file current separator just-return-string)
  10541. "Display the current outline path in the echo area.
  10542. If FILE is non-nil, prepend the output with the file name.
  10543. If CURRENT is non-nil, append the current heading to the output.
  10544. SEPARATOR is passed through to `org-format-outline-path'. It separates
  10545. the different parts of the path and defaults to \"/\".
  10546. If JUST-RETURN-STRING is non-nil, return a string, don't display a message."
  10547. (interactive "P")
  10548. (let* (case-fold-search
  10549. (bfn (buffer-file-name (buffer-base-buffer)))
  10550. (path (and (derived-mode-p 'org-mode) (org-get-outline-path)))
  10551. res)
  10552. (when current (setq path (append path
  10553. (save-excursion
  10554. (org-back-to-heading t)
  10555. (when (looking-at org-complex-heading-regexp)
  10556. (list (match-string 4)))))))
  10557. (setq res
  10558. (org-format-outline-path
  10559. path
  10560. (1- (frame-width))
  10561. (and file bfn (concat (file-name-nondirectory bfn) separator))
  10562. separator))
  10563. (if just-return-string
  10564. (org-no-properties res)
  10565. (org-unlogged-message "%s" res))))
  10566. (defvar org-refile-history nil
  10567. "History for refiling operations.")
  10568. (defvar org-after-refile-insert-hook nil
  10569. "Hook run after `org-refile' has inserted its stuff at the new location.
  10570. Note that this is still *before* the stuff will be removed from
  10571. the *old* location.")
  10572. (defvar org-capture-last-stored-marker)
  10573. (defvar org-refile-keep nil
  10574. "Non-nil means `org-refile' will copy instead of refile.")
  10575. (defun org-copy ()
  10576. "Like `org-refile', but copy."
  10577. (interactive)
  10578. (let ((org-refile-keep t))
  10579. (funcall 'org-refile nil nil nil "Copy")))
  10580. (defun org-refile (&optional arg default-buffer rfloc msg)
  10581. "Move the entry or entries at point to another heading.
  10582. The list of target headings is compiled using the information in
  10583. `org-refile-targets', which see.
  10584. At the target location, the entry is filed as a subitem of the
  10585. target heading. Depending on `org-reverse-note-order', the new
  10586. subitem will either be the first or the last subitem.
  10587. If there is an active region, all entries in that region will be
  10588. refiled. However, the region must fulfill the requirement that
  10589. the first heading sets the top-level of the moved text.
  10590. With prefix arg ARG, the command will only visit the target
  10591. location and not actually move anything.
  10592. With a double prefix arg \\[universal-argument] \\[universal-argument], go to the location where the last
  10593. refiling operation has put the subtree.
  10594. With a numeric prefix argument of `2', refile to the running clock.
  10595. With a numeric prefix argument of `3', emulate `org-refile-keep'
  10596. being set to t and copy to the target location, don't move it.
  10597. Beware that keeping refiled entries may result in duplicated ID
  10598. properties.
  10599. RFLOC can be a refile location obtained in a different way.
  10600. MSG is a string to replace \"Refile\" in the default prompt with
  10601. another verb. E.g. `org-copy' sets this parameter to \"Copy\".
  10602. See also `org-refile-use-outline-path' and `org-completion-use-ido'.
  10603. If you are using target caching (see `org-refile-use-cache'), you
  10604. have to clear the target cache in order to find new targets.
  10605. This can be done with a 0 prefix (`C-0 C-c C-w') or a triple
  10606. prefix argument (`C-u C-u C-u C-c C-w')."
  10607. (interactive "P")
  10608. (if (member arg '(0 (64)))
  10609. (org-refile-cache-clear)
  10610. (let* ((actionmsg (cond (msg msg)
  10611. ((equal arg 3) "Refile (and keep)")
  10612. (t "Refile")))
  10613. (regionp (org-region-active-p))
  10614. (region-start (and regionp (region-beginning)))
  10615. (region-end (and regionp (region-end)))
  10616. (org-refile-keep (if (equal arg 3) t org-refile-keep))
  10617. pos it nbuf file level reversed)
  10618. (setq last-command nil)
  10619. (when regionp
  10620. (goto-char region-start)
  10621. (or (bolp) (goto-char (point-at-bol)))
  10622. (setq region-start (point))
  10623. (unless (or (org-kill-is-subtree-p
  10624. (buffer-substring region-start region-end))
  10625. (prog1 org-refile-active-region-within-subtree
  10626. (let ((s (point-at-eol)))
  10627. (org-toggle-heading)
  10628. (setq region-end (+ (- (point-at-eol) s) region-end)))))
  10629. (user-error "The region is not a (sequence of) subtree(s)")))
  10630. (if (equal arg '(16))
  10631. (org-refile-goto-last-stored)
  10632. (when (or
  10633. (and (equal arg 2)
  10634. org-clock-hd-marker (marker-buffer org-clock-hd-marker)
  10635. (prog1
  10636. (setq it (list (or org-clock-heading "running clock")
  10637. (buffer-file-name
  10638. (marker-buffer org-clock-hd-marker))
  10639. ""
  10640. (marker-position org-clock-hd-marker)))
  10641. (setq arg nil)))
  10642. (setq it (or rfloc
  10643. (let (heading-text)
  10644. (save-excursion
  10645. (unless (and arg (listp arg))
  10646. (org-back-to-heading t)
  10647. (setq heading-text
  10648. (replace-regexp-in-string
  10649. org-bracket-link-regexp
  10650. "\\3"
  10651. (nth 4 (org-heading-components)))))
  10652. (org-refile-get-location
  10653. (cond ((and arg (listp arg)) "Goto")
  10654. (regionp (concat actionmsg " region to"))
  10655. (t (concat actionmsg " subtree \""
  10656. heading-text "\" to")))
  10657. default-buffer
  10658. (and (not (equal '(4) arg))
  10659. org-refile-allow-creating-parent-nodes)
  10660. arg))))))
  10661. (setq file (nth 1 it)
  10662. pos (nth 3 it))
  10663. (when (and (not arg)
  10664. pos
  10665. (equal (buffer-file-name) file)
  10666. (if regionp
  10667. (and (>= pos region-start)
  10668. (<= pos region-end))
  10669. (and (>= pos (point))
  10670. (< pos (save-excursion
  10671. (org-end-of-subtree t t))))))
  10672. (error "Cannot refile to position inside the tree or region"))
  10673. (setq nbuf (or (find-buffer-visiting file)
  10674. (find-file-noselect file)))
  10675. (if (and arg (not (equal arg 3)))
  10676. (progn
  10677. (org-pop-to-buffer-same-window nbuf)
  10678. (goto-char pos)
  10679. (org-show-context 'org-goto))
  10680. (if regionp
  10681. (progn
  10682. (org-kill-new (buffer-substring region-start region-end))
  10683. (org-save-markers-in-region region-start region-end))
  10684. (org-copy-subtree 1 nil t))
  10685. (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
  10686. (find-file-noselect file)))
  10687. (setq reversed (org-notes-order-reversed-p))
  10688. (save-excursion
  10689. (save-restriction
  10690. (widen)
  10691. (if pos
  10692. (progn
  10693. (goto-char pos)
  10694. (looking-at org-outline-regexp)
  10695. (setq level (org-get-valid-level (funcall outline-level) 1))
  10696. (goto-char
  10697. (if reversed
  10698. (or (outline-next-heading) (point-max))
  10699. (or (save-excursion (org-get-next-sibling))
  10700. (org-end-of-subtree t t)
  10701. (point-max)))))
  10702. (setq level 1)
  10703. (if (not reversed)
  10704. (goto-char (point-max))
  10705. (goto-char (point-min))
  10706. (or (outline-next-heading) (goto-char (point-max)))))
  10707. (unless (bolp) (newline))
  10708. (org-paste-subtree level nil nil t)
  10709. (when org-log-refile
  10710. (org-add-log-setup 'refile nil nil org-log-refile)
  10711. (unless (eq org-log-refile 'note)
  10712. (save-excursion (org-add-log-note))))
  10713. (and org-auto-align-tags
  10714. (let ((org-loop-over-headlines-in-active-region nil))
  10715. (org-set-tags nil t)))
  10716. (let ((bookmark-name (plist-get org-bookmark-names-plist
  10717. :last-refile)))
  10718. (when bookmark-name
  10719. (with-demoted-errors
  10720. (bookmark-set bookmark-name))))
  10721. ;; If we are refiling for capture, make sure that the
  10722. ;; last-capture pointers point here
  10723. (when (org-bound-and-true-p org-capture-is-refiling)
  10724. (let ((bookmark-name (plist-get org-bookmark-names-plist
  10725. :last-capture-marker)))
  10726. (when bookmark-name
  10727. (with-demoted-errors
  10728. (bookmark-set bookmark-name))))
  10729. (move-marker org-capture-last-stored-marker (point)))
  10730. (when (fboundp 'deactivate-mark) (deactivate-mark))
  10731. (run-hooks 'org-after-refile-insert-hook))))
  10732. (unless org-refile-keep
  10733. (if regionp
  10734. (delete-region (point) (+ (point) (- region-end region-start)))
  10735. (delete-region
  10736. (and (org-back-to-heading t) (point))
  10737. (min (1+ (buffer-size)) (org-end-of-subtree t t) (point)))))
  10738. (when (featurep 'org-inlinetask)
  10739. (org-inlinetask-remove-END-maybe))
  10740. (setq org-markers-to-move nil)
  10741. (message (concat actionmsg " to \"%s\" in file %s: done") (car it) file)))))))
  10742. (defun org-refile-goto-last-stored ()
  10743. "Go to the location where the last refile was stored."
  10744. (interactive)
  10745. (bookmark-jump (plist-get org-bookmark-names-plist :last-refile))
  10746. (message "This is the location of the last refile"))
  10747. (defun org-refile--get-location (refloc tbl)
  10748. "When user refile to REFLOC, find the associated target in TBL.
  10749. Also check `org-refile-target-table'."
  10750. (car (delq
  10751. nil
  10752. (mapcar
  10753. (lambda (r) (or (assoc r tbl)
  10754. (assoc r org-refile-target-table)))
  10755. (list (replace-regexp-in-string "/$" "" refloc)
  10756. (replace-regexp-in-string "\\([^/]\\)$" "\\1/" refloc))))))
  10757. (defun org-refile-get-location (&optional prompt default-buffer new-nodes
  10758. no-exclude)
  10759. "Prompt the user for a refile location, using PROMPT.
  10760. PROMPT should not be suffixed with a colon and a space, because
  10761. this function appends the default value from
  10762. `org-refile-history' automatically, if that is not empty.
  10763. When NO-EXCLUDE is set, do not exclude headlines in the current subtree,
  10764. this is used for the GOTO interface."
  10765. (let ((org-refile-targets org-refile-targets)
  10766. (org-refile-use-outline-path org-refile-use-outline-path)
  10767. excluded-entries)
  10768. (when (and (derived-mode-p 'org-mode)
  10769. (not org-refile-use-cache)
  10770. (not no-exclude))
  10771. (org-map-tree
  10772. (lambda()
  10773. (setq excluded-entries
  10774. (append excluded-entries (list (org-get-heading t t)))))))
  10775. (setq org-refile-target-table
  10776. (org-refile-get-targets default-buffer excluded-entries)))
  10777. (unless org-refile-target-table
  10778. (user-error "No refile targets"))
  10779. (let* ((cbuf (current-buffer))
  10780. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  10781. (cfunc (if (and org-refile-use-outline-path
  10782. org-outline-path-complete-in-steps)
  10783. #'org-olpath-completing-read
  10784. #'completing-read))
  10785. (extra (if org-refile-use-outline-path "/" ""))
  10786. (cbnex (concat (buffer-name) extra))
  10787. (filename (and cfn (expand-file-name cfn)))
  10788. (tbl (mapcar
  10789. (lambda (x)
  10790. (if (and (not (member org-refile-use-outline-path
  10791. '(file full-file-path)))
  10792. (not (equal filename (nth 1 x))))
  10793. (cons (concat (car x) extra " ("
  10794. (file-name-nondirectory (nth 1 x)) ")")
  10795. (cdr x))
  10796. (cons (concat (car x) extra) (cdr x))))
  10797. org-refile-target-table))
  10798. (completion-ignore-case t)
  10799. cdef
  10800. (prompt (concat prompt
  10801. (or (and (car org-refile-history)
  10802. (concat " (default " (car org-refile-history) ")"))
  10803. (and (assoc cbnex tbl) (setq cdef cbnex)
  10804. (concat " (default " cbnex ")"))) ": "))
  10805. pa answ parent-target child parent old-hist)
  10806. (setq old-hist org-refile-history)
  10807. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  10808. nil 'org-refile-history (or cdef (car org-refile-history))))
  10809. (if (setq pa (org-refile--get-location answ tbl))
  10810. (progn
  10811. (org-refile-check-position pa)
  10812. (when (or (not org-refile-history)
  10813. (not (eq old-hist org-refile-history))
  10814. (not (equal (car pa) (car org-refile-history))))
  10815. (setq org-refile-history
  10816. (cons (car pa) (if (assoc (car org-refile-history) tbl)
  10817. org-refile-history
  10818. (cdr org-refile-history))))
  10819. (when (equal (car org-refile-history) (nth 1 org-refile-history))
  10820. (pop org-refile-history)))
  10821. pa)
  10822. (if (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  10823. (progn
  10824. (setq parent (match-string 1 answ)
  10825. child (match-string 2 answ))
  10826. (setq parent-target (org-refile--get-location parent tbl))
  10827. (when (and parent-target
  10828. (or (eq new-nodes t)
  10829. (and (eq new-nodes 'confirm)
  10830. (y-or-n-p (format "Create new node \"%s\"? "
  10831. child)))))
  10832. (org-refile-new-child parent-target child)))
  10833. (user-error "Invalid target location")))))
  10834. (declare-function org-string-nw-p "org-macs" (s))
  10835. (defun org-refile-check-position (refile-pointer)
  10836. "Check if the refile pointer matches the headline to which it points."
  10837. (let* ((file (nth 1 refile-pointer))
  10838. (re (nth 2 refile-pointer))
  10839. (pos (nth 3 refile-pointer))
  10840. buffer)
  10841. (if (and (not (markerp pos)) (not file))
  10842. (user-error "Please indicate a target file in the refile path")
  10843. (when (org-string-nw-p re)
  10844. (setq buffer (if (markerp pos)
  10845. (marker-buffer pos)
  10846. (or (find-buffer-visiting file)
  10847. (find-file-noselect file))))
  10848. (with-current-buffer buffer
  10849. (save-excursion
  10850. (save-restriction
  10851. (widen)
  10852. (goto-char pos)
  10853. (beginning-of-line 1)
  10854. (unless (org-looking-at-p re)
  10855. (user-error "Invalid refile position, please clear the cache with `C-0 C-c C-w' before refiling")))))))))
  10856. (defun org-refile-new-child (parent-target child)
  10857. "Use refile target PARENT-TARGET to add new CHILD below it."
  10858. (unless parent-target
  10859. (error "Cannot find parent for new node"))
  10860. (let ((file (nth 1 parent-target))
  10861. (pos (nth 3 parent-target))
  10862. level)
  10863. (with-current-buffer (or (find-buffer-visiting file)
  10864. (find-file-noselect file))
  10865. (save-excursion
  10866. (save-restriction
  10867. (widen)
  10868. (if pos
  10869. (goto-char pos)
  10870. (goto-char (point-max))
  10871. (unless (bolp) (newline)))
  10872. (when (looking-at org-outline-regexp)
  10873. (setq level (funcall outline-level))
  10874. (org-end-of-subtree t t))
  10875. (org-back-over-empty-lines)
  10876. (insert "\n" (make-string
  10877. (if pos (org-get-valid-level level 1) 1) ?*)
  10878. " " child "\n")
  10879. (beginning-of-line 0)
  10880. (list (concat (car parent-target) "/" child) file "" (point)))))))
  10881. (defun org-olpath-completing-read (prompt collection &rest args)
  10882. "Read an outline path like a file name."
  10883. (let ((thetable collection))
  10884. (apply #'completing-read
  10885. prompt
  10886. (lambda (string predicate &optional flag)
  10887. (cond
  10888. ((eq flag nil) (try-completion string thetable))
  10889. ((eq flag t)
  10890. (let ((l (length string)))
  10891. (mapcar (lambda (x)
  10892. (let ((r (substring x l))
  10893. (f (if (string-match " ([^)]*)$" x)
  10894. (match-string 0 x)
  10895. "")))
  10896. (if (string-match "/" r)
  10897. (concat string (substring r 0 (match-end 0)) f)
  10898. x)))
  10899. (all-completions string thetable predicate))))
  10900. ;; Exact match?
  10901. ((eq flag 'lambda) (assoc string thetable))))
  10902. args)))
  10903. ;;;; Dynamic blocks
  10904. (defun org-find-dblock (name)
  10905. "Find the first dynamic block with name NAME in the buffer.
  10906. If not found, stay at current position and return nil."
  10907. (let ((case-fold-search t) pos)
  10908. (save-excursion
  10909. (goto-char (point-min))
  10910. (setq pos (and (re-search-forward
  10911. (concat "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+" name "\\>") nil t)
  10912. (match-beginning 0))))
  10913. (when pos (goto-char pos))
  10914. pos))
  10915. (defun org-create-dblock (plist)
  10916. "Create a dynamic block section, with parameters taken from PLIST.
  10917. PLIST must contain a :name entry which is used as the name of the block."
  10918. (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
  10919. (end-of-line 1)
  10920. (newline))
  10921. (let ((col (current-column))
  10922. (name (plist-get plist :name)))
  10923. (insert "#+BEGIN: " name)
  10924. (while plist
  10925. (if (eq (car plist) :name)
  10926. (setq plist (cddr plist))
  10927. (insert " " (prin1-to-string (pop plist)))))
  10928. (insert "\n\n" (make-string col ?\ ) "#+END:\n")
  10929. (beginning-of-line -2)))
  10930. (defun org-prepare-dblock ()
  10931. "Prepare dynamic block for refresh.
  10932. This empties the block, puts the cursor at the insert position and returns
  10933. the property list including an extra property :name with the block name."
  10934. (unless (looking-at org-dblock-start-re)
  10935. (user-error "Not at a dynamic block"))
  10936. (let* ((begdel (1+ (match-end 0)))
  10937. (name (org-no-properties (match-string 1)))
  10938. (params (append (list :name name)
  10939. (read (concat "(" (match-string 3) ")")))))
  10940. (save-excursion
  10941. (beginning-of-line 1)
  10942. (skip-chars-forward " \t")
  10943. (setq params (plist-put params :indentation-column (current-column))))
  10944. (unless (re-search-forward org-dblock-end-re nil t)
  10945. (error "Dynamic block not terminated"))
  10946. (setq params
  10947. (append params
  10948. (list :content (buffer-substring
  10949. begdel (match-beginning 0)))))
  10950. (delete-region begdel (match-beginning 0))
  10951. (goto-char begdel)
  10952. (open-line 1)
  10953. params))
  10954. (defun org-map-dblocks (&optional command)
  10955. "Apply COMMAND to all dynamic blocks in the current buffer.
  10956. If COMMAND is not given, use `org-update-dblock'."
  10957. (let ((cmd (or command 'org-update-dblock)))
  10958. (save-excursion
  10959. (goto-char (point-min))
  10960. (while (re-search-forward org-dblock-start-re nil t)
  10961. (goto-char (match-beginning 0))
  10962. (save-excursion
  10963. (condition-case nil
  10964. (funcall cmd)
  10965. (error (message "Error during update of dynamic block"))))
  10966. (unless (re-search-forward org-dblock-end-re nil t)
  10967. (error "Dynamic block not terminated"))))))
  10968. (defun org-dblock-update (&optional arg)
  10969. "User command for updating dynamic blocks.
  10970. Update the dynamic block at point. With prefix ARG, update all dynamic
  10971. blocks in the buffer."
  10972. (interactive "P")
  10973. (if arg
  10974. (org-update-all-dblocks)
  10975. (or (looking-at org-dblock-start-re)
  10976. (org-beginning-of-dblock))
  10977. (org-update-dblock)))
  10978. (defun org-update-dblock ()
  10979. "Update the dynamic block at point.
  10980. This means to empty the block, parse for parameters and then call
  10981. the correct writing function."
  10982. (interactive)
  10983. (save-excursion
  10984. (let* ((win (selected-window))
  10985. (pos (point))
  10986. (line (org-current-line))
  10987. (params (org-prepare-dblock))
  10988. (name (plist-get params :name))
  10989. (indent (plist-get params :indentation-column))
  10990. (cmd (intern (concat "org-dblock-write:" name))))
  10991. (message "Updating dynamic block `%s' at line %d..." name line)
  10992. (funcall cmd params)
  10993. (message "Updating dynamic block `%s' at line %d...done" name line)
  10994. (goto-char pos)
  10995. (when (and indent (> indent 0))
  10996. (setq indent (make-string indent ?\ ))
  10997. (save-excursion
  10998. (select-window win)
  10999. (org-beginning-of-dblock)
  11000. (forward-line 1)
  11001. (while (not (looking-at org-dblock-end-re))
  11002. (insert indent)
  11003. (beginning-of-line 2))
  11004. (when (looking-at org-dblock-end-re)
  11005. (and (looking-at "[ \t]+")
  11006. (replace-match ""))
  11007. (insert indent)))))))
  11008. (defun org-beginning-of-dblock ()
  11009. "Find the beginning of the dynamic block at point.
  11010. Error if there is no such block at point."
  11011. (let ((pos (point))
  11012. beg)
  11013. (end-of-line 1)
  11014. (if (and (re-search-backward org-dblock-start-re nil t)
  11015. (setq beg (match-beginning 0))
  11016. (re-search-forward org-dblock-end-re nil t)
  11017. (> (match-end 0) pos))
  11018. (goto-char beg)
  11019. (goto-char pos)
  11020. (error "Not in a dynamic block"))))
  11021. (defun org-update-all-dblocks ()
  11022. "Update all dynamic blocks in the buffer.
  11023. This function can be used in a hook."
  11024. (interactive)
  11025. (when (derived-mode-p 'org-mode)
  11026. (org-map-dblocks 'org-update-dblock)))
  11027. ;;;; Completion
  11028. (declare-function org-export-backend-name "org-export" (cl-x))
  11029. (declare-function org-export-backend-options "org-export" (cl-x))
  11030. (defun org-get-export-keywords ()
  11031. "Return a list of all currently understood export keywords.
  11032. Export keywords include options, block names, attributes and
  11033. keywords relative to each registered export back-end."
  11034. (let (keywords)
  11035. (dolist (backend
  11036. (org-bound-and-true-p org-export-registered-backends)
  11037. (delq nil keywords))
  11038. ;; Back-end name (for keywords, like #+LATEX:)
  11039. (push (upcase (symbol-name (org-export-backend-name backend))) keywords)
  11040. (dolist (option-entry (org-export-backend-options backend))
  11041. ;; Back-end options.
  11042. (push (nth 1 option-entry) keywords)))))
  11043. (defconst org-options-keywords
  11044. '("ARCHIVE:" "AUTHOR:" "BIND:" "CATEGORY:" "COLUMNS:" "CREATOR:" "DATE:"
  11045. "DESCRIPTION:" "DRAWERS:" "EMAIL:" "EXCLUDE_TAGS:" "FILETAGS:" "INCLUDE:"
  11046. "INDEX:" "KEYWORDS:" "LANGUAGE:" "MACRO:" "OPTIONS:" "PROPERTY:"
  11047. "PRIORITIES:" "SELECT_TAGS:" "SEQ_TODO:" "SETUPFILE:" "STARTUP:" "TAGS:"
  11048. "TITLE:" "TODO:" "TYP_TODO:" "SELECT_TAGS:" "EXCLUDE_TAGS:"))
  11049. (defcustom org-structure-template-alist
  11050. '(("s" "#+BEGIN_SRC ?\n\n#+END_SRC")
  11051. ("e" "#+BEGIN_EXAMPLE\n?\n#+END_EXAMPLE")
  11052. ("q" "#+BEGIN_QUOTE\n?\n#+END_QUOTE")
  11053. ("v" "#+BEGIN_VERSE\n?\n#+END_VERSE")
  11054. ("V" "#+BEGIN_VERBATIM\n?\n#+END_VERBATIM")
  11055. ("c" "#+BEGIN_CENTER\n?\n#+END_CENTER")
  11056. ("l" "#+BEGIN_EXPORT latex\n?\n#+END_EXPORT")
  11057. ("L" "#+LaTeX: ")
  11058. ("h" "#+BEGIN_EXPORT html\n?\n#+END_EXPORT")
  11059. ("H" "#+HTML: ")
  11060. ("a" "#+BEGIN_EXPORT ascii\n?\n#+END_EXPORT")
  11061. ("A" "#+ASCII: ")
  11062. ("i" "#+INDEX: ?")
  11063. ("I" "#+INCLUDE: %file ?"))
  11064. "Structure completion elements.
  11065. This is a list of abbreviation keys and values. The value gets inserted
  11066. if you type `<' followed by the key and then press the completion key,
  11067. usually `TAB'. %file will be replaced by a file name after prompting
  11068. for the file using completion. The cursor will be placed at the position
  11069. of the `?' in the template.
  11070. There are two templates for each key, the first uses the original Org syntax,
  11071. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  11072. the default when the /org-mtags.el/ module has been loaded. See also the
  11073. variable `org-mtags-prefer-muse-templates'."
  11074. :group 'org-completion
  11075. :type '(repeat
  11076. (list
  11077. (string :tag "Key")
  11078. (string :tag "Template")))
  11079. :version "25.1"
  11080. :package-version '(Org . "8.3"))
  11081. (defun org-try-structure-completion ()
  11082. "Try to complete a structure template before point.
  11083. This looks for strings like \"<e\" on an otherwise empty line and
  11084. expands them."
  11085. (let ((l (buffer-substring (point-at-bol) (point)))
  11086. a)
  11087. (when (and (looking-at "[ \t]*$")
  11088. (string-match "^[ \t]*<\\([a-zA-Z]+\\)$" l)
  11089. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  11090. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  11091. (match-beginning 1)) a)
  11092. t)))
  11093. (defun org-complete-expand-structure-template (start cell)
  11094. "Expand a structure template."
  11095. (let ((rpl (nth 1 cell))
  11096. (ind ""))
  11097. (delete-region start (point))
  11098. (when (string-match "\\`[ \t]*#\\+" rpl)
  11099. (cond
  11100. ((bolp))
  11101. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  11102. (setq ind (buffer-substring (point-at-bol) (point))))
  11103. (t (newline))))
  11104. (setq start (point))
  11105. (when (string-match "%file" rpl)
  11106. (setq rpl (replace-match
  11107. (concat
  11108. "\""
  11109. (save-match-data
  11110. (abbreviate-file-name (read-file-name "Include file: ")))
  11111. "\"")
  11112. t t rpl)))
  11113. (setq rpl (mapconcat 'identity (split-string rpl "\n")
  11114. (concat "\n" ind)))
  11115. (insert rpl)
  11116. (when (re-search-backward "\\?" start t) (delete-char 1))))
  11117. ;;;; TODO, DEADLINE, Comments
  11118. (defun org-toggle-comment ()
  11119. "Change the COMMENT state of an entry."
  11120. (interactive)
  11121. (save-excursion
  11122. (org-back-to-heading)
  11123. (looking-at org-complex-heading-regexp)
  11124. (goto-char (or (match-end 3) (match-end 2) (match-end 1)))
  11125. (skip-chars-forward " \t")
  11126. (unless (memq (char-before) '(?\s ?\t)) (insert " "))
  11127. (if (org-in-commented-heading-p t)
  11128. (delete-region (point)
  11129. (progn (search-forward " " (line-end-position) 'move)
  11130. (skip-chars-forward " \t")
  11131. (point)))
  11132. (insert org-comment-string)
  11133. (unless (eolp) (insert " ")))))
  11134. (defvar org-last-todo-state-is-todo nil
  11135. "This is non-nil when the last TODO state change led to a TODO state.
  11136. If the last change removed the TODO tag or switched to DONE, then
  11137. this is nil.")
  11138. (defvar org-setting-tags nil) ; dynamically skipped
  11139. (defvar org-todo-setup-filter-hook nil
  11140. "Hook for functions that pre-filter todo specs.
  11141. Each function takes a todo spec and returns either nil or the spec
  11142. transformed into canonical form." )
  11143. (defvar org-todo-get-default-hook nil
  11144. "Hook for functions that get a default item for todo.
  11145. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  11146. nil or a string to be used for the todo mark." )
  11147. (defvar org-agenda-headline-snapshot-before-repeat)
  11148. (defun org-current-effective-time ()
  11149. "Return current time adjusted for `org-extend-today-until' variable."
  11150. (let* ((ct (org-current-time))
  11151. (dct (decode-time ct))
  11152. (ct1
  11153. (cond
  11154. (org-use-last-clock-out-time-as-effective-time
  11155. (or (org-clock-get-last-clock-out-time) ct))
  11156. ((and org-use-effective-time (< (nth 2 dct) org-extend-today-until))
  11157. (encode-time 0 59 23 (1- (nth 3 dct)) (nth 4 dct) (nth 5 dct)))
  11158. (t ct))))
  11159. ct1))
  11160. (defun org-todo-yesterday (&optional arg)
  11161. "Like `org-todo' but the time of change will be 23:59 of yesterday."
  11162. (interactive "P")
  11163. (if (eq major-mode 'org-agenda-mode)
  11164. (apply 'org-agenda-todo-yesterday arg)
  11165. (let* ((org-use-effective-time t)
  11166. (hour (nth 2 (decode-time (org-current-time))))
  11167. (org-extend-today-until (1+ hour)))
  11168. (org-todo arg))))
  11169. (defvar org-block-entry-blocking ""
  11170. "First entry preventing the TODO state change.")
  11171. (defun org-cancel-repeater ()
  11172. "Cancel a repeater by setting its numeric value to zero."
  11173. (interactive)
  11174. (save-excursion
  11175. (org-back-to-heading t)
  11176. (let ((bound1 (point))
  11177. (bound0 (save-excursion (outline-next-heading) (point))))
  11178. (when (and (re-search-forward
  11179. (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  11180. org-deadline-time-regexp "\\)\\|\\("
  11181. org-ts-regexp "\\)")
  11182. bound0 t)
  11183. (re-search-backward "[ \t]+\\(?:[.+]\\)?\\+\\([0-9]+\\)[hdwmy]"
  11184. bound1 t))
  11185. (replace-match "0" t nil nil 1)))))
  11186. (defun org-todo (&optional arg)
  11187. "Change the TODO state of an item.
  11188. The state of an item is given by a keyword at the start of the heading,
  11189. like
  11190. *** TODO Write paper
  11191. *** DONE Call mom
  11192. The different keywords are specified in the variable `org-todo-keywords'.
  11193. By default the available states are \"TODO\" and \"DONE\".
  11194. So for this example: when the item starts with TODO, it is changed to DONE.
  11195. When it starts with DONE, the DONE is removed. And when neither TODO nor
  11196. DONE are present, add TODO at the beginning of the heading.
  11197. With \\[universal-argument] prefix arg, use completion to determine the new \
  11198. state.
  11199. With numeric prefix arg, switch to that state.
  11200. With a double \\[universal-argument] prefix, switch to the next set of TODO \
  11201. keywords (nextset).
  11202. With a triple \\[universal-argument] prefix, circumvent any state blocking.
  11203. With a numeric prefix arg of 0, inhibit note taking for the change.
  11204. With a numeric prefix arg of -1, cancel repeater to allow marking as DONE.
  11205. When called through ELisp, arg is also interpreted in the following way:
  11206. `none' -> empty state
  11207. \"\"(empty string) -> switch to empty state
  11208. `done' -> switch to DONE
  11209. `nextset' -> switch to the next set of keywords
  11210. `previousset' -> switch to the previous set of keywords
  11211. \"WAITING\" -> switch to the specified keyword, but only if it
  11212. really is a member of `org-todo-keywords'."
  11213. (interactive "P")
  11214. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  11215. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11216. 'region-start-level 'region))
  11217. org-loop-over-headlines-in-active-region)
  11218. (org-map-entries
  11219. `(org-todo ,arg)
  11220. org-loop-over-headlines-in-active-region
  11221. cl (when (outline-invisible-p) (org-end-of-subtree nil t))))
  11222. (when (equal arg '(16)) (setq arg 'nextset))
  11223. (when (equal arg -1) (org-cancel-repeater) (setq arg nil))
  11224. (let ((org-blocker-hook org-blocker-hook)
  11225. commentp
  11226. case-fold-search)
  11227. (when (equal arg '(64))
  11228. (setq arg nil org-blocker-hook nil))
  11229. (when (and org-blocker-hook
  11230. (or org-inhibit-blocking
  11231. (org-entry-get nil "NOBLOCKING")))
  11232. (setq org-blocker-hook nil))
  11233. (save-excursion
  11234. (catch 'exit
  11235. (org-back-to-heading t)
  11236. (when (org-in-commented-heading-p t)
  11237. (org-toggle-comment)
  11238. (setq commentp t))
  11239. (when (looking-at org-outline-regexp) (goto-char (1- (match-end 0))))
  11240. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|[ \t]*$\\)"))
  11241. (looking-at "\\(?: *\\|[ \t]*$\\)"))
  11242. (let* ((match-data (match-data))
  11243. (startpos (point-at-bol))
  11244. (logging (save-match-data (org-entry-get nil "LOGGING" t t)))
  11245. (org-log-done org-log-done)
  11246. (org-log-repeat org-log-repeat)
  11247. (org-todo-log-states org-todo-log-states)
  11248. (org-inhibit-logging
  11249. (if (equal arg 0)
  11250. (progn (setq arg nil) 'note) org-inhibit-logging))
  11251. (this (match-string 1))
  11252. (hl-pos (match-beginning 0))
  11253. (head (org-get-todo-sequence-head this))
  11254. (ass (assoc head org-todo-kwd-alist))
  11255. (interpret (nth 1 ass))
  11256. (done-word (nth 3 ass))
  11257. (final-done-word (nth 4 ass))
  11258. (org-last-state (or this ""))
  11259. (completion-ignore-case t)
  11260. (member (member this org-todo-keywords-1))
  11261. (tail (cdr member))
  11262. (org-state (cond
  11263. ((and org-todo-key-trigger
  11264. (or (and (equal arg '(4))
  11265. (eq org-use-fast-todo-selection 'prefix))
  11266. (and (not arg) org-use-fast-todo-selection
  11267. (not (eq org-use-fast-todo-selection
  11268. 'prefix)))))
  11269. ;; Use fast selection
  11270. (org-fast-todo-selection))
  11271. ((and (equal arg '(4))
  11272. (or (not org-use-fast-todo-selection)
  11273. (not org-todo-key-trigger)))
  11274. ;; Read a state with completion
  11275. (completing-read
  11276. "State: " (mapcar #'list org-todo-keywords-1)
  11277. nil t))
  11278. ((eq arg 'right)
  11279. (if this
  11280. (if tail (car tail) nil)
  11281. (car org-todo-keywords-1)))
  11282. ((eq arg 'left)
  11283. (unless (equal member org-todo-keywords-1)
  11284. (if this
  11285. (nth (- (length org-todo-keywords-1)
  11286. (length tail) 2)
  11287. org-todo-keywords-1)
  11288. (org-last org-todo-keywords-1))))
  11289. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  11290. (setq arg nil))) ; hack to fall back to cycling
  11291. (arg
  11292. ;; user or caller requests a specific state
  11293. (cond
  11294. ((equal arg "") nil)
  11295. ((eq arg 'none) nil)
  11296. ((eq arg 'done) (or done-word (car org-done-keywords)))
  11297. ((eq arg 'nextset)
  11298. (or (car (cdr (member head org-todo-heads)))
  11299. (car org-todo-heads)))
  11300. ((eq arg 'previousset)
  11301. (let ((org-todo-heads (reverse org-todo-heads)))
  11302. (or (car (cdr (member head org-todo-heads)))
  11303. (car org-todo-heads))))
  11304. ((car (member arg org-todo-keywords-1)))
  11305. ((stringp arg)
  11306. (user-error "State `%s' not valid in this file" arg))
  11307. ((nth (1- (prefix-numeric-value arg))
  11308. org-todo-keywords-1))))
  11309. ((null member) (or head (car org-todo-keywords-1)))
  11310. ((equal this final-done-word) nil) ;; -> make empty
  11311. ((null tail) nil) ;; -> first entry
  11312. ((memq interpret '(type priority))
  11313. (if (eq this-command last-command)
  11314. (car tail)
  11315. (if (> (length tail) 0)
  11316. (or done-word (car org-done-keywords))
  11317. nil)))
  11318. (t
  11319. (car tail))))
  11320. (org-state (or
  11321. (run-hook-with-args-until-success
  11322. 'org-todo-get-default-hook org-state org-last-state)
  11323. org-state))
  11324. (next (if org-state (concat " " org-state " ") " "))
  11325. (change-plist (list :type 'todo-state-change :from this :to org-state
  11326. :position startpos))
  11327. dolog now-done-p)
  11328. (when org-blocker-hook
  11329. (setq org-last-todo-state-is-todo
  11330. (not (member this org-done-keywords)))
  11331. (unless (save-excursion
  11332. (save-match-data
  11333. (org-with-wide-buffer
  11334. (run-hook-with-args-until-failure
  11335. 'org-blocker-hook change-plist))))
  11336. (if (org-called-interactively-p 'interactive)
  11337. (user-error "TODO state change from %s to %s blocked (by \"%s\")"
  11338. this org-state org-block-entry-blocking)
  11339. ;; fail silently
  11340. (message "TODO state change from %s to %s blocked (by \"%s\")"
  11341. this org-state org-block-entry-blocking)
  11342. (throw 'exit nil))))
  11343. (store-match-data match-data)
  11344. (replace-match next t t)
  11345. (cond ((equal this org-state)
  11346. (message "TODO state was already %s" (org-trim next)))
  11347. ((pos-visible-in-window-p hl-pos)
  11348. (message "TODO state changed to %s" (org-trim next))))
  11349. (unless head
  11350. (setq head (org-get-todo-sequence-head org-state)
  11351. ass (assoc head org-todo-kwd-alist)
  11352. interpret (nth 1 ass)
  11353. done-word (nth 3 ass)
  11354. final-done-word (nth 4 ass)))
  11355. (when (memq arg '(nextset previousset))
  11356. (message "Keyword-Set %d/%d: %s"
  11357. (- (length org-todo-sets) -1
  11358. (length (memq (assoc org-state org-todo-sets) org-todo-sets)))
  11359. (length org-todo-sets)
  11360. (mapconcat 'identity (assoc org-state org-todo-sets) " ")))
  11361. (setq org-last-todo-state-is-todo
  11362. (not (member org-state org-done-keywords)))
  11363. (setq now-done-p (and (member org-state org-done-keywords)
  11364. (not (member this org-done-keywords))))
  11365. (and logging (org-local-logging logging))
  11366. (when (and (or org-todo-log-states org-log-done)
  11367. (not (eq org-inhibit-logging t))
  11368. (not (memq arg '(nextset previousset))))
  11369. ;; we need to look at recording a time and note
  11370. (setq dolog (or (nth 1 (assoc org-state org-todo-log-states))
  11371. (nth 2 (assoc this org-todo-log-states))))
  11372. (when (and (eq dolog 'note) (eq org-inhibit-logging 'note))
  11373. (setq dolog 'time))
  11374. (when (or (and (not org-state) (not org-closed-keep-when-no-todo))
  11375. (and org-state
  11376. (member org-state org-not-done-keywords)
  11377. (not (member this org-not-done-keywords))))
  11378. ;; This is now a todo state and was not one before
  11379. ;; If there was a CLOSED time stamp, get rid of it.
  11380. (org-add-planning-info nil nil 'closed))
  11381. (when (and now-done-p org-log-done)
  11382. ;; It is now done, and it was not done before
  11383. (org-add-planning-info 'closed (org-current-effective-time))
  11384. (when (and (not dolog) (eq 'note org-log-done))
  11385. (org-add-log-setup 'done org-state this 'note)))
  11386. (when (and org-state dolog)
  11387. ;; This is a non-nil state, and we need to log it
  11388. (org-add-log-setup 'state org-state this dolog)))
  11389. ;; Fixup tag positioning
  11390. (org-todo-trigger-tag-changes org-state)
  11391. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  11392. (when org-provide-todo-statistics
  11393. (org-update-parent-todo-statistics))
  11394. (run-hooks 'org-after-todo-state-change-hook)
  11395. (when (and arg (not (member org-state org-done-keywords)))
  11396. (setq head (org-get-todo-sequence-head org-state)))
  11397. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  11398. ;; Do we need to trigger a repeat?
  11399. (when now-done-p
  11400. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  11401. ;; This is for the agenda, take a snapshot of the headline.
  11402. (save-match-data
  11403. (setq org-agenda-headline-snapshot-before-repeat
  11404. (org-get-heading))))
  11405. (org-auto-repeat-maybe org-state))
  11406. ;; Fixup cursor location if close to the keyword
  11407. (when (and (outline-on-heading-p)
  11408. (not (bolp))
  11409. (save-excursion (beginning-of-line 1)
  11410. (looking-at org-todo-line-regexp))
  11411. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  11412. (goto-char (or (match-end 2) (match-end 1)))
  11413. (and (looking-at " ") (just-one-space)))
  11414. (when org-trigger-hook
  11415. (save-excursion
  11416. (run-hook-with-args 'org-trigger-hook change-plist)))
  11417. (when commentp (org-toggle-comment))))))))
  11418. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  11419. "Block turning an entry into a TODO, using the hierarchy.
  11420. This checks whether the current task should be blocked from state
  11421. changes. Such blocking occurs when:
  11422. 1. The task has children which are not all in a completed state.
  11423. 2. A task has a parent with the property :ORDERED:, and there
  11424. are siblings prior to the current task with incomplete
  11425. status.
  11426. 3. The parent of the task is blocked because it has siblings that should
  11427. be done first, or is child of a block grandparent TODO entry."
  11428. (if (not org-enforce-todo-dependencies)
  11429. t ; if locally turned off don't block
  11430. (catch 'dont-block
  11431. ;; If this is not a todo state change, or if this entry is already DONE,
  11432. ;; do not block
  11433. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  11434. (member (plist-get change-plist :from)
  11435. (cons 'done org-done-keywords))
  11436. (member (plist-get change-plist :to)
  11437. (cons 'todo org-not-done-keywords))
  11438. (not (plist-get change-plist :to)))
  11439. (throw 'dont-block t))
  11440. ;; If this task has children, and any are undone, it's blocked
  11441. (save-excursion
  11442. (org-back-to-heading t)
  11443. (let ((this-level (funcall outline-level)))
  11444. (outline-next-heading)
  11445. (let ((child-level (funcall outline-level)))
  11446. (while (and (not (eobp))
  11447. (> child-level this-level))
  11448. ;; this todo has children, check whether they are all
  11449. ;; completed
  11450. (when (and (not (org-entry-is-done-p))
  11451. (org-entry-is-todo-p))
  11452. (setq org-block-entry-blocking (org-get-heading))
  11453. (throw 'dont-block nil))
  11454. (outline-next-heading)
  11455. (setq child-level (funcall outline-level))))))
  11456. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  11457. ;; any previous siblings are undone, it's blocked
  11458. (save-excursion
  11459. (org-back-to-heading t)
  11460. (let* ((pos (point))
  11461. (parent-pos (and (org-up-heading-safe) (point))))
  11462. (unless parent-pos (throw 'dont-block t)) ; no parent
  11463. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  11464. (forward-line 1)
  11465. (re-search-forward org-not-done-heading-regexp pos t))
  11466. (setq org-block-entry-blocking (match-string 0))
  11467. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  11468. ;; Search further up the hierarchy, to see if an ancestor is blocked
  11469. (while t
  11470. (goto-char parent-pos)
  11471. (unless (looking-at org-not-done-heading-regexp)
  11472. (throw 'dont-block t)) ; do not block, parent is not a TODO
  11473. (setq pos (point))
  11474. (setq parent-pos (and (org-up-heading-safe) (point)))
  11475. (unless parent-pos (throw 'dont-block t)) ; no parent
  11476. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  11477. (forward-line 1)
  11478. (re-search-forward org-not-done-heading-regexp pos t)
  11479. (setq org-block-entry-blocking (org-get-heading)))
  11480. (throw 'dont-block nil)))))))) ; block, older sibling not done.
  11481. (defcustom org-track-ordered-property-with-tag nil
  11482. "Should the ORDERED property also be shown as a tag?
  11483. The ORDERED property decides if an entry should require subtasks to be
  11484. completed in sequence. Since a property is not very visible, setting
  11485. this option means that toggling the ORDERED property with the command
  11486. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  11487. not relevant for the behavior, but it makes things more visible.
  11488. Note that toggling the tag with tags commands will not change the property
  11489. and therefore not influence behavior!
  11490. This can be t, meaning the tag ORDERED should be used, It can also be a
  11491. string to select a different tag for this task."
  11492. :group 'org-todo
  11493. :type '(choice
  11494. (const :tag "No tracking" nil)
  11495. (const :tag "Track with ORDERED tag" t)
  11496. (string :tag "Use other tag")))
  11497. (defun org-toggle-ordered-property ()
  11498. "Toggle the ORDERED property of the current entry.
  11499. For better visibility, you can track the value of this property with a tag.
  11500. See variable `org-track-ordered-property-with-tag'."
  11501. (interactive)
  11502. (let* ((t1 org-track-ordered-property-with-tag)
  11503. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  11504. (save-excursion
  11505. (org-back-to-heading)
  11506. (if (org-entry-get nil "ORDERED")
  11507. (progn
  11508. (org-delete-property "ORDERED")
  11509. (and tag (org-toggle-tag tag 'off))
  11510. (message "Subtasks can be completed in arbitrary order"))
  11511. (org-entry-put nil "ORDERED" "t")
  11512. (and tag (org-toggle-tag tag 'on))
  11513. (message "Subtasks must be completed in sequence")))))
  11514. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  11515. (defun org-block-todo-from-checkboxes (change-plist)
  11516. "Block turning an entry into a TODO, using checkboxes.
  11517. This checks whether the current task should be blocked from state
  11518. changes because there are unchecked boxes in this entry."
  11519. (if (not org-enforce-todo-checkbox-dependencies)
  11520. t ; if locally turned off don't block
  11521. (catch 'dont-block
  11522. ;; If this is not a todo state change, or if this entry is already DONE,
  11523. ;; do not block
  11524. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  11525. (member (plist-get change-plist :from)
  11526. (cons 'done org-done-keywords))
  11527. (member (plist-get change-plist :to)
  11528. (cons 'todo org-not-done-keywords))
  11529. (not (plist-get change-plist :to)))
  11530. (throw 'dont-block t))
  11531. ;; If this task has checkboxes that are not checked, it's blocked
  11532. (save-excursion
  11533. (org-back-to-heading t)
  11534. (let ((beg (point)) end)
  11535. (outline-next-heading)
  11536. (setq end (point))
  11537. (goto-char beg)
  11538. (when (org-list-search-forward
  11539. (concat (org-item-beginning-re)
  11540. "\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?"
  11541. "\\[[- ]\\]")
  11542. end t)
  11543. (when (boundp 'org-blocked-by-checkboxes)
  11544. (setq org-blocked-by-checkboxes t))
  11545. (throw 'dont-block nil))))
  11546. t))) ; do not block
  11547. (defun org-entry-blocked-p ()
  11548. "Non-nil if entry at point is blocked."
  11549. (and (not (org-entry-get nil "NOBLOCKING"))
  11550. (member (org-entry-get nil "TODO") org-not-done-keywords)
  11551. (not (run-hook-with-args-until-failure
  11552. 'org-blocker-hook
  11553. (list :type 'todo-state-change
  11554. :position (point)
  11555. :from 'todo
  11556. :to 'done)))))
  11557. (defun org-update-statistics-cookies (all)
  11558. "Update the statistics cookie, either from TODO or from checkboxes.
  11559. This should be called with the cursor in a line with a statistics cookie."
  11560. (interactive "P")
  11561. (if all
  11562. (progn
  11563. (org-update-checkbox-count 'all)
  11564. (org-map-entries 'org-update-parent-todo-statistics))
  11565. (if (not (org-at-heading-p))
  11566. (org-update-checkbox-count)
  11567. (let ((pos (point-marker))
  11568. end l1 l2)
  11569. (ignore-errors (org-back-to-heading t))
  11570. (if (not (org-at-heading-p))
  11571. (org-update-checkbox-count)
  11572. (setq l1 (org-outline-level))
  11573. (setq end (save-excursion
  11574. (outline-next-heading)
  11575. (when (org-at-heading-p) (setq l2 (org-outline-level)))
  11576. (point)))
  11577. (if (and (save-excursion
  11578. (re-search-forward
  11579. "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) \\[[- X]\\]" end t))
  11580. (not (save-excursion (re-search-forward
  11581. ":COOKIE_DATA:.*\\<todo\\>" end t))))
  11582. (org-update-checkbox-count)
  11583. (if (and l2 (> l2 l1))
  11584. (progn
  11585. (goto-char end)
  11586. (org-update-parent-todo-statistics))
  11587. (goto-char pos)
  11588. (beginning-of-line 1)
  11589. (while (re-search-forward
  11590. "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)"
  11591. (point-at-eol) t)
  11592. (replace-match (if (match-end 2) "[100%]" "[0/0]") t t)))))
  11593. (goto-char pos)
  11594. (move-marker pos nil)))))
  11595. (defvar org-entry-property-inherited-from) ;; defined below
  11596. (defun org-update-parent-todo-statistics ()
  11597. "Update any statistics cookie in the parent of the current headline.
  11598. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  11599. the entire subtree and this will travel up the hierarchy and update
  11600. statistics everywhere."
  11601. (let* ((prop (save-excursion (org-up-heading-safe)
  11602. (org-entry-get nil "COOKIE_DATA" 'inherit)))
  11603. (recursive (or (not org-hierarchical-todo-statistics)
  11604. (and prop (string-match "\\<recursive\\>" prop))))
  11605. (lim (or (and prop (marker-position org-entry-property-inherited-from))
  11606. 0))
  11607. (first t)
  11608. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  11609. level ltoggle l1 new ndel
  11610. (cnt-all 0) (cnt-done 0) is-percent kwd
  11611. checkbox-beg ov ovs ove cookie-present)
  11612. (catch 'exit
  11613. (save-excursion
  11614. (beginning-of-line 1)
  11615. (setq ltoggle (funcall outline-level))
  11616. ;; Three situations are to consider:
  11617. ;; 1. if `org-hierarchical-todo-statistics' is nil, repeat up
  11618. ;; to the top-level ancestor on the headline;
  11619. ;; 2. If parent has "recursive" property, repeat up to the
  11620. ;; headline setting that property, taking inheritance into
  11621. ;; account;
  11622. ;; 3. Else, move up to direct parent and proceed only once.
  11623. (while (and (setq level (org-up-heading-safe))
  11624. (or recursive first)
  11625. (>= (point) lim))
  11626. (setq first nil cookie-present nil)
  11627. (unless (and level
  11628. (not (string-match
  11629. "\\<checkbox\\>"
  11630. (downcase (or (org-entry-get nil "COOKIE_DATA")
  11631. "")))))
  11632. (throw 'exit nil))
  11633. (while (re-search-forward box-re (point-at-eol) t)
  11634. (setq cnt-all 0 cnt-done 0 cookie-present t)
  11635. (setq is-percent (match-end 2) checkbox-beg (match-beginning 0))
  11636. (save-match-data
  11637. (unless (outline-next-heading) (throw 'exit nil))
  11638. (while (and (looking-at org-complex-heading-regexp)
  11639. (> (setq l1 (length (match-string 1))) level))
  11640. (setq kwd (and (or recursive (= l1 ltoggle))
  11641. (match-string 2)))
  11642. (if (or (eq org-provide-todo-statistics 'all-headlines)
  11643. (and (eq org-provide-todo-statistics t)
  11644. (or (member kwd org-done-keywords)))
  11645. (and (listp org-provide-todo-statistics)
  11646. (stringp (car org-provide-todo-statistics))
  11647. (or (member kwd org-provide-todo-statistics)
  11648. (member kwd org-done-keywords)))
  11649. (and (listp org-provide-todo-statistics)
  11650. (listp (car org-provide-todo-statistics))
  11651. (or (member kwd (car org-provide-todo-statistics))
  11652. (and (member kwd org-done-keywords)
  11653. (member kwd (cadr org-provide-todo-statistics))))))
  11654. (setq cnt-all (1+ cnt-all))
  11655. (and (eq org-provide-todo-statistics t)
  11656. kwd
  11657. (setq cnt-all (1+ cnt-all))))
  11658. (when (or (and (member org-provide-todo-statistics '(t all-headlines))
  11659. (member kwd org-done-keywords))
  11660. (and (listp org-provide-todo-statistics)
  11661. (listp (car org-provide-todo-statistics))
  11662. (member kwd org-done-keywords)
  11663. (member kwd (cadr org-provide-todo-statistics)))
  11664. (and (listp org-provide-todo-statistics)
  11665. (stringp (car org-provide-todo-statistics))
  11666. (member kwd org-done-keywords)))
  11667. (setq cnt-done (1+ cnt-done)))
  11668. (outline-next-heading)))
  11669. (setq new
  11670. (if is-percent
  11671. (format "[%d%%]" (floor (* 100.0 cnt-done)
  11672. (max 1 cnt-all)))
  11673. (format "[%d/%d]" cnt-done cnt-all))
  11674. ndel (- (match-end 0) checkbox-beg))
  11675. ;; handle overlays when updating cookie from column view
  11676. (when (setq ov (car (overlays-at checkbox-beg)))
  11677. (setq ovs (overlay-start ov) ove (overlay-end ov))
  11678. (delete-overlay ov))
  11679. (goto-char checkbox-beg)
  11680. (insert new)
  11681. (delete-region (point) (+ (point) ndel))
  11682. (when org-auto-align-tags (org-fix-tags-on-the-fly))
  11683. (when ov (move-overlay ov ovs ove)))
  11684. (when cookie-present
  11685. (run-hook-with-args 'org-after-todo-statistics-hook
  11686. cnt-done (- cnt-all cnt-done))))))
  11687. (run-hooks 'org-todo-statistics-hook)))
  11688. (defvar org-after-todo-statistics-hook nil
  11689. "Hook that is called after a TODO statistics cookie has been updated.
  11690. Each function is called with two arguments: the number of not-done entries
  11691. and the number of done entries.
  11692. For example, the following function, when added to this hook, will switch
  11693. an entry to DONE when all children are done, and back to TODO when new
  11694. entries are set to a TODO status. Note that this hook is only called
  11695. when there is a statistics cookie in the headline!
  11696. \(defun org-summary-todo (n-done n-not-done)
  11697. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  11698. \(let (org-log-done org-log-states) ; turn off logging
  11699. \(org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  11700. ")
  11701. (defvar org-todo-statistics-hook nil
  11702. "Hook that is run whenever Org thinks TODO statistics should be updated.
  11703. This hook runs even if there is no statistics cookie present, in which case
  11704. `org-after-todo-statistics-hook' would not run.")
  11705. (defun org-todo-trigger-tag-changes (state)
  11706. "Apply the changes defined in `org-todo-state-tags-triggers'."
  11707. (let ((l org-todo-state-tags-triggers)
  11708. changes)
  11709. (when (or (not state) (equal state ""))
  11710. (setq changes (append changes (cdr (assoc "" l)))))
  11711. (when (and (stringp state) (> (length state) 0))
  11712. (setq changes (append changes (cdr (assoc state l)))))
  11713. (when (member state org-not-done-keywords)
  11714. (setq changes (append changes (cdr (assoc 'todo l)))))
  11715. (when (member state org-done-keywords)
  11716. (setq changes (append changes (cdr (assoc 'done l)))))
  11717. (dolist (c changes)
  11718. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  11719. (defun org-local-logging (value)
  11720. "Get logging settings from a property VALUE."
  11721. ;; Directly set the variables, they are already local.
  11722. (setq org-log-done nil
  11723. org-log-repeat nil
  11724. org-todo-log-states nil)
  11725. (dolist (w (org-split-string value))
  11726. (let (a)
  11727. (cond
  11728. ((setq a (assoc w org-startup-options))
  11729. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  11730. (set (nth 1 a) (nth 2 a))))
  11731. ((setq a (org-extract-log-state-settings w))
  11732. (and (member (car a) org-todo-keywords-1)
  11733. (push a org-todo-log-states)))))))
  11734. (defun org-get-todo-sequence-head (kwd)
  11735. "Return the head of the TODO sequence to which KWD belongs.
  11736. If KWD is not set, check if there is a text property remembering the
  11737. right sequence."
  11738. (let (p)
  11739. (cond
  11740. ((not kwd)
  11741. (or (get-text-property (point-at-bol) 'org-todo-head)
  11742. (progn
  11743. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  11744. nil (point-at-eol)))
  11745. (get-text-property p 'org-todo-head))))
  11746. ((not (member kwd org-todo-keywords-1))
  11747. (car org-todo-keywords-1))
  11748. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  11749. (defun org-fast-todo-selection ()
  11750. "Fast TODO keyword selection with single keys.
  11751. Returns the new TODO keyword, or nil if no state change should occur."
  11752. (let* ((fulltable org-todo-key-alist)
  11753. (done-keywords org-done-keywords) ;; needed for the faces.
  11754. (maxlen (apply 'max (mapcar
  11755. (lambda (x)
  11756. (if (stringp (car x)) (string-width (car x)) 0))
  11757. fulltable)))
  11758. (expert nil)
  11759. (fwidth (+ maxlen 3 1 3))
  11760. (ncol (/ (- (window-width) 4) fwidth))
  11761. tg cnt e c tbl
  11762. groups ingroup)
  11763. (save-excursion
  11764. (save-window-excursion
  11765. (if expert
  11766. (set-buffer (get-buffer-create " *Org todo*"))
  11767. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  11768. (erase-buffer)
  11769. (setq-local org-done-keywords done-keywords)
  11770. (setq tbl fulltable cnt 0)
  11771. (while (setq e (pop tbl))
  11772. (cond
  11773. ((equal e '(:startgroup))
  11774. (push '() groups) (setq ingroup t)
  11775. (unless (= cnt 0)
  11776. (setq cnt 0)
  11777. (insert "\n"))
  11778. (insert "{ "))
  11779. ((equal e '(:endgroup))
  11780. (setq ingroup nil cnt 0)
  11781. (insert "}\n"))
  11782. ((equal e '(:newline))
  11783. (unless (= cnt 0)
  11784. (setq cnt 0)
  11785. (insert "\n")
  11786. (setq e (car tbl))
  11787. (while (equal (car tbl) '(:newline))
  11788. (insert "\n")
  11789. (setq tbl (cdr tbl)))))
  11790. (t
  11791. (setq tg (car e) c (cdr e))
  11792. (when ingroup (push tg (car groups)))
  11793. (setq tg (org-add-props tg nil 'face
  11794. (org-get-todo-face tg)))
  11795. (when (and (= cnt 0) (not ingroup)) (insert " "))
  11796. (insert "[" c "] " tg (make-string
  11797. (- fwidth 4 (length tg)) ?\ ))
  11798. (when (= (setq cnt (1+ cnt)) ncol)
  11799. (insert "\n")
  11800. (when ingroup (insert " "))
  11801. (setq cnt 0)))))
  11802. (insert "\n")
  11803. (goto-char (point-min))
  11804. (unless expert (org-fit-window-to-buffer))
  11805. (message "[a-z..]:Set [SPC]:clear")
  11806. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  11807. (cond
  11808. ((or (= c ?\C-g)
  11809. (and (= c ?q) (not (rassoc c fulltable))))
  11810. (setq quit-flag t))
  11811. ((= c ?\ ) nil)
  11812. ((setq e (rassoc c fulltable) tg (car e))
  11813. tg)
  11814. (t (setq quit-flag t)))))))
  11815. (defun org-entry-is-todo-p ()
  11816. (member (org-get-todo-state) org-not-done-keywords))
  11817. (defun org-entry-is-done-p ()
  11818. (member (org-get-todo-state) org-done-keywords))
  11819. (defun org-get-todo-state ()
  11820. "Return the TODO keyword of the current subtree."
  11821. (save-excursion
  11822. (org-back-to-heading t)
  11823. (and (looking-at org-todo-line-regexp)
  11824. (match-end 2)
  11825. (match-string 2))))
  11826. (defun org-at-date-range-p (&optional inactive-ok)
  11827. "Non-nil if point is inside a date range.
  11828. When optional argument INACTIVE-OK is non-nil, also consider
  11829. inactive time ranges.
  11830. When this function returns a non-nil value, match data is set
  11831. according to `org-tr-regexp-both' or `org-tr-regexp', depending
  11832. on INACTIVE-OK."
  11833. (interactive)
  11834. (save-excursion
  11835. (catch 'exit
  11836. (let ((pos (point)))
  11837. (skip-chars-backward "^[<\r\n")
  11838. (skip-chars-backward "<[")
  11839. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  11840. (>= (match-end 0) pos)
  11841. (throw 'exit t))
  11842. (skip-chars-backward "^<[\r\n")
  11843. (skip-chars-backward "<[")
  11844. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  11845. (>= (match-end 0) pos)
  11846. (throw 'exit t)))
  11847. nil)))
  11848. (defun org-get-repeat (&optional tagline)
  11849. "Check if there is a deadline/schedule with repeater in this entry."
  11850. (save-match-data
  11851. (save-excursion
  11852. (org-back-to-heading t)
  11853. (and (re-search-forward (if tagline
  11854. (concat tagline "\\s-*" org-repeat-re)
  11855. org-repeat-re)
  11856. (org-entry-end-position) t)
  11857. (match-string-no-properties 1)))))
  11858. (defvar org-last-changed-timestamp)
  11859. (defvar org-last-inserted-timestamp)
  11860. (defvar org-log-post-message)
  11861. (defvar org-log-note-purpose)
  11862. (defvar org-log-note-how nil)
  11863. (defvar org-log-note-extra)
  11864. (defun org-auto-repeat-maybe (done-word)
  11865. "Check if the current headline contains a repeated time-stamp.
  11866. If yes, set TODO state back to what it was and change the base date
  11867. of repeating deadline/scheduled time stamps to new date.
  11868. This function is run automatically after each state change to a DONE state."
  11869. (let* ((repeat (org-get-repeat))
  11870. (aa (assoc org-last-state org-todo-kwd-alist))
  11871. (interpret (nth 1 aa))
  11872. (head (nth 2 aa))
  11873. (whata '(("h" . hour) ("d" . day) ("m" . month) ("y" . year)))
  11874. (msg "Entry repeats: ")
  11875. (org-log-done nil)
  11876. (org-todo-log-states nil))
  11877. (when (and repeat (not (zerop (string-to-number (substring repeat 1)))))
  11878. (when (eq org-log-repeat t) (setq org-log-repeat 'state))
  11879. (let ((to-state (or (org-entry-get nil "REPEAT_TO_STATE" 'selective)
  11880. org-todo-repeat-to-state)))
  11881. (unless (and to-state (member to-state org-todo-keywords-1))
  11882. (setq to-state (if (eq interpret 'type) org-last-state head)))
  11883. (org-todo to-state))
  11884. (when (or org-log-repeat (org-entry-get nil "CLOCK"))
  11885. (org-entry-put nil "LAST_REPEAT" (format-time-string
  11886. (org-time-stamp-format t t))))
  11887. (when org-log-repeat
  11888. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  11889. (memq 'org-add-log-note post-command-hook))
  11890. ;; We are already setup for some record.
  11891. (when (eq org-log-repeat 'note)
  11892. ;; Make sure we take a note, not only a time stamp.
  11893. (setq org-log-note-how 'note))
  11894. ;; Set up for taking a record.
  11895. (org-add-log-setup 'state
  11896. (or done-word (car org-done-keywords))
  11897. org-last-state
  11898. org-log-repeat)))
  11899. (org-back-to-heading t)
  11900. (org-add-planning-info nil nil 'closed)
  11901. (let ((regexp (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  11902. org-deadline-time-regexp "\\)\\|\\("
  11903. org-ts-regexp "\\)"))
  11904. (end (save-excursion (outline-next-heading) (point))))
  11905. (while (re-search-forward regexp end t)
  11906. (let ((type (cond ((match-end 1) org-scheduled-string)
  11907. ((match-end 3) org-deadline-string)
  11908. (t "Plain:")))
  11909. (ts (or (match-string 2) (match-string 4) (match-string 0))))
  11910. (cond
  11911. ((not (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts))
  11912. ;; Time-stamps without a repeater are usually skipped.
  11913. ;; However, a SCHEDULED time-stamp without one is
  11914. ;; removed, as it is considered as no longer relevant.
  11915. (when (equal type org-scheduled-string)
  11916. (org-remove-timestamp-with-keyword type)))
  11917. (t
  11918. (let ((n (string-to-number (match-string 2 ts)))
  11919. (what (match-string 3 ts)))
  11920. (when (equal what "w") (setq n (* n 7) what "d"))
  11921. (when (and (equal what "h")
  11922. (not (string-match-p "[0-9]\\{1,2\\}:[0-9]\\{2\\}"
  11923. ts)))
  11924. (user-error
  11925. "Cannot repeat in Repeat in %d hour(s) because no hour has \
  11926. been set"
  11927. n))
  11928. ;; Preparation, see if we need to modify the start
  11929. ;; date for the change.
  11930. (when (match-end 1)
  11931. (let ((time (save-match-data (org-time-string-to-time ts))))
  11932. (cond
  11933. ((equal (match-string 1 ts) ".")
  11934. ;; Shift starting date to today
  11935. (org-timestamp-change
  11936. (- (org-today) (time-to-days time))
  11937. 'day))
  11938. ((equal (match-string 1 ts) "+")
  11939. (let ((nshiftmax 10)
  11940. (nshift 0))
  11941. (while (or (= nshift 0)
  11942. (<= (time-to-days time)
  11943. (time-to-days (current-time))))
  11944. (when (= (cl-incf nshift) nshiftmax)
  11945. (or (y-or-n-p
  11946. (format "%d repeater intervals were not \
  11947. enough to shift date past today. Continue? "
  11948. nshift))
  11949. (user-error "Abort")))
  11950. (org-timestamp-change n (cdr (assoc what whata)))
  11951. (org-at-timestamp-p t)
  11952. (setq ts (match-string 1))
  11953. (setq time
  11954. (save-match-data
  11955. (org-time-string-to-time ts)))))
  11956. (org-timestamp-change (- n) (cdr (assoc what whata)))
  11957. ;; Rematch, so that we have everything in place
  11958. ;; for the real shift.
  11959. (org-at-timestamp-p t)
  11960. (setq ts (match-string 1))
  11961. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)"
  11962. ts)))))
  11963. (save-excursion
  11964. (org-timestamp-change n (cdr (assoc what whata)) nil t))
  11965. (setq msg
  11966. (concat
  11967. msg type " " org-last-changed-timestamp " "))))))))
  11968. (setq org-log-post-message msg)
  11969. (message "%s" msg))))
  11970. (defun org-show-todo-tree (arg)
  11971. "Make a compact tree which shows all headlines marked with TODO.
  11972. The tree will show the lines where the regexp matches, and all higher
  11973. headlines above the match.
  11974. With a \\[universal-argument] prefix, prompt for a regexp to match.
  11975. With a numeric prefix N, construct a sparse tree for the Nth element
  11976. of `org-todo-keywords-1'."
  11977. (interactive "P")
  11978. (let ((case-fold-search nil)
  11979. (kwd-re
  11980. (cond ((null arg) org-not-done-regexp)
  11981. ((equal arg '(4))
  11982. (let ((kwd
  11983. (completing-read "Keyword (or KWD1|KWD2|...): "
  11984. (mapcar #'list org-todo-keywords-1))))
  11985. (concat "\\("
  11986. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  11987. "\\)\\>")))
  11988. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  11989. (regexp-quote (nth (1- (prefix-numeric-value arg))
  11990. org-todo-keywords-1)))
  11991. (t (user-error "Invalid prefix argument: %s" arg)))))
  11992. (message "%d TODO entries found"
  11993. (org-occur (concat "^" org-outline-regexp " *" kwd-re )))))
  11994. (defun org-deadline (arg &optional time)
  11995. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  11996. With one universal prefix argument, remove any deadline from the item.
  11997. With two universal prefix arguments, prompt for a warning delay.
  11998. With argument TIME, set the deadline at the corresponding date. TIME
  11999. can either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  12000. (interactive "P")
  12001. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  12002. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  12003. 'region-start-level 'region))
  12004. org-loop-over-headlines-in-active-region)
  12005. (org-map-entries
  12006. `(org-deadline ',arg ,time)
  12007. org-loop-over-headlines-in-active-region
  12008. cl (when (outline-invisible-p) (org-end-of-subtree nil t))))
  12009. (let* ((old-date (org-entry-get nil "DEADLINE"))
  12010. (old-date-time (when old-date (org-time-string-to-time old-date)))
  12011. (repeater (and old-date
  12012. (string-match
  12013. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  12014. old-date)
  12015. (match-string 1 old-date))))
  12016. (cond
  12017. ((equal arg '(4))
  12018. (when (and old-date org-log-redeadline)
  12019. (org-add-log-setup 'deldeadline nil old-date org-log-redeadline))
  12020. (org-remove-timestamp-with-keyword org-deadline-string)
  12021. (message "Item no longer has a deadline."))
  12022. ((equal arg '(16))
  12023. (save-excursion
  12024. (org-back-to-heading t)
  12025. (if (re-search-forward
  12026. org-deadline-time-regexp
  12027. (save-excursion (outline-next-heading) (point)) t)
  12028. (let* ((rpl0 (match-string 1))
  12029. (rpl (replace-regexp-in-string " -[0-9]+[hdwmy]" "" rpl0)))
  12030. (replace-match
  12031. (concat org-deadline-string
  12032. " <" rpl
  12033. (format " -%dd"
  12034. (abs
  12035. (- (time-to-days
  12036. (save-match-data
  12037. (org-read-date nil t nil "Warn starting from" old-date-time)))
  12038. (time-to-days old-date-time))))
  12039. ">") t t))
  12040. (user-error "No deadline information to update"))))
  12041. (t
  12042. (org-add-planning-info 'deadline time 'closed)
  12043. (when (and old-date
  12044. org-log-redeadline
  12045. (not (equal old-date org-last-inserted-timestamp)))
  12046. (org-add-log-setup
  12047. 'redeadline org-last-inserted-timestamp old-date org-log-redeadline))
  12048. (when repeater
  12049. (save-excursion
  12050. (org-back-to-heading t)
  12051. (when (re-search-forward (concat org-deadline-string " "
  12052. org-last-inserted-timestamp)
  12053. (save-excursion
  12054. (outline-next-heading) (point)) t)
  12055. (goto-char (1- (match-end 0)))
  12056. (insert " " repeater)
  12057. (setq org-last-inserted-timestamp
  12058. (concat (substring org-last-inserted-timestamp 0 -1)
  12059. " " repeater
  12060. (substring org-last-inserted-timestamp -1))))))
  12061. (message "Deadline on %s" org-last-inserted-timestamp))))))
  12062. (defun org-schedule (arg &optional time)
  12063. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  12064. With one universal prefix argument, remove any scheduling date from the item.
  12065. With two universal prefix arguments, prompt for a delay cookie.
  12066. With argument TIME, scheduled at the corresponding date. TIME can
  12067. either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  12068. (interactive "P")
  12069. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  12070. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  12071. 'region-start-level 'region))
  12072. org-loop-over-headlines-in-active-region)
  12073. (org-map-entries
  12074. `(org-schedule ',arg ,time)
  12075. org-loop-over-headlines-in-active-region
  12076. cl (when (outline-invisible-p) (org-end-of-subtree nil t))))
  12077. (let* ((old-date (org-entry-get nil "SCHEDULED"))
  12078. (old-date-time (when old-date (org-time-string-to-time old-date)))
  12079. (repeater (and old-date
  12080. (string-match
  12081. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  12082. old-date)
  12083. (match-string 1 old-date))))
  12084. (cond
  12085. ((equal arg '(4))
  12086. (progn
  12087. (when (and old-date org-log-reschedule)
  12088. (org-add-log-setup 'delschedule nil old-date org-log-reschedule))
  12089. (org-remove-timestamp-with-keyword org-scheduled-string)
  12090. (message "Item is no longer scheduled.")))
  12091. ((equal arg '(16))
  12092. (save-excursion
  12093. (org-back-to-heading t)
  12094. (if (re-search-forward
  12095. org-scheduled-time-regexp
  12096. (save-excursion (outline-next-heading) (point)) t)
  12097. (let* ((rpl0 (match-string 1))
  12098. (rpl (replace-regexp-in-string " -[0-9]+[hdwmy]" "" rpl0)))
  12099. (replace-match
  12100. (concat org-scheduled-string
  12101. " <" rpl
  12102. (format " -%dd"
  12103. (abs
  12104. (- (time-to-days
  12105. (save-match-data
  12106. (org-read-date nil t nil "Delay until" old-date-time)))
  12107. (time-to-days old-date-time))))
  12108. ">") t t))
  12109. (user-error "No scheduled information to update"))))
  12110. (t
  12111. (org-add-planning-info 'scheduled time 'closed)
  12112. (when (and old-date
  12113. org-log-reschedule
  12114. (not (equal old-date org-last-inserted-timestamp)))
  12115. (org-add-log-setup
  12116. 'reschedule org-last-inserted-timestamp old-date org-log-reschedule))
  12117. (when repeater
  12118. (save-excursion
  12119. (org-back-to-heading t)
  12120. (when (re-search-forward (concat org-scheduled-string " "
  12121. org-last-inserted-timestamp)
  12122. (save-excursion
  12123. (outline-next-heading) (point)) t)
  12124. (goto-char (1- (match-end 0)))
  12125. (insert " " repeater)
  12126. (setq org-last-inserted-timestamp
  12127. (concat (substring org-last-inserted-timestamp 0 -1)
  12128. " " repeater
  12129. (substring org-last-inserted-timestamp -1))))))
  12130. (message "Scheduled to %s" org-last-inserted-timestamp))))))
  12131. (defun org-get-scheduled-time (pom &optional inherit)
  12132. "Get the scheduled time as a time tuple, of a format suitable
  12133. for calling org-schedule with, or if there is no scheduling,
  12134. returns nil."
  12135. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  12136. (when time
  12137. (apply 'encode-time (org-parse-time-string time)))))
  12138. (defun org-get-deadline-time (pom &optional inherit)
  12139. "Get the deadline as a time tuple, of a format suitable for
  12140. calling org-deadline with, or if there is no scheduling, returns
  12141. nil."
  12142. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  12143. (when time
  12144. (apply 'encode-time (org-parse-time-string time)))))
  12145. (defun org-remove-timestamp-with-keyword (keyword)
  12146. "Remove all time stamps with KEYWORD in the current entry."
  12147. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  12148. beg)
  12149. (save-excursion
  12150. (org-back-to-heading t)
  12151. (setq beg (point))
  12152. (outline-next-heading)
  12153. (while (re-search-backward re beg t)
  12154. (replace-match "")
  12155. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  12156. (equal (char-before) ?\ ))
  12157. (backward-delete-char 1)
  12158. (when (string-match "^[ \t]*$" (buffer-substring
  12159. (point-at-bol) (point-at-eol)))
  12160. (delete-region (point-at-bol)
  12161. (min (point-max) (1+ (point-at-eol))))))))))
  12162. (defvar org-time-was-given) ; dynamically scoped parameter
  12163. (defvar org-end-time-was-given) ; dynamically scoped parameter
  12164. (defun org-at-planning-p ()
  12165. "Non-nil when point is on a planning info line."
  12166. ;; This is as accurate and faster than `org-element-at-point' since
  12167. ;; planning info location is fixed in the section.
  12168. (org-with-wide-buffer
  12169. (beginning-of-line)
  12170. (and (org-looking-at-p org-planning-line-re)
  12171. (eq (point)
  12172. (ignore-errors
  12173. (if (and (featurep 'org-inlinetask) (org-inlinetask-in-task-p))
  12174. (org-back-to-heading t)
  12175. (org-with-limited-levels (org-back-to-heading t)))
  12176. (line-beginning-position 2))))))
  12177. (defun org-add-planning-info (what &optional time &rest remove)
  12178. "Insert new timestamp with keyword in the planning line.
  12179. WHAT indicates what kind of time stamp to add. It is a symbol
  12180. among `closed', `deadline', `scheduled' and nil. TIME indicates
  12181. the time to use. If none is given, the user is prompted for
  12182. a date. REMOVE indicates what kind of entries to remove. An old
  12183. WHAT entry will also be removed."
  12184. (let (org-time-was-given org-end-time-was-given default-time default-input)
  12185. (catch 'exit
  12186. (when (and (memq what '(scheduled deadline))
  12187. (or (not time)
  12188. (and (stringp time)
  12189. (string-match "^[-+]+[0-9]" time))))
  12190. ;; Try to get a default date/time from existing timestamp
  12191. (save-excursion
  12192. (org-back-to-heading t)
  12193. (let ((end (save-excursion (outline-next-heading) (point))) ts)
  12194. (when (re-search-forward (if (eq what 'scheduled)
  12195. org-scheduled-time-regexp
  12196. org-deadline-time-regexp)
  12197. end t)
  12198. (setq ts (match-string 1)
  12199. default-time (apply 'encode-time (org-parse-time-string ts))
  12200. default-input (and ts (org-get-compact-tod ts)))))))
  12201. (when what
  12202. (setq time
  12203. (if (stringp time)
  12204. ;; This is a string (relative or absolute), set
  12205. ;; proper date.
  12206. (apply #'encode-time
  12207. (org-read-date-analyze
  12208. time default-time (decode-time default-time)))
  12209. ;; If necessary, get the time from the user
  12210. (or time (org-read-date nil 'to-time nil nil
  12211. default-time default-input)))))
  12212. (org-with-wide-buffer
  12213. (org-back-to-heading t)
  12214. (forward-line)
  12215. (unless (bolp) (insert "\n"))
  12216. (cond ((org-looking-at-p org-planning-line-re)
  12217. ;; Move to current indentation.
  12218. (skip-chars-forward " \t")
  12219. ;; Check if we have to remove something.
  12220. (dolist (type (if what (cons what remove) remove))
  12221. (save-excursion
  12222. (when (re-search-forward
  12223. (cl-case type
  12224. (closed org-closed-time-regexp)
  12225. (deadline org-deadline-time-regexp)
  12226. (scheduled org-scheduled-time-regexp)
  12227. (otherwise
  12228. (error "Invalid planning type: %s" type)))
  12229. (line-end-position) t)
  12230. ;; Delete until next keyword or end of line.
  12231. (delete-region
  12232. (match-beginning 0)
  12233. (if (re-search-forward org-keyword-time-not-clock-regexp
  12234. (line-end-position)
  12235. t)
  12236. (match-beginning 0)
  12237. (line-end-position))))))
  12238. ;; If there is nothing more to add and no more keyword
  12239. ;; is left, remove the line completely.
  12240. (if (and (org-looking-at-p "[ \t]*$") (not what))
  12241. (delete-region (line-beginning-position)
  12242. (line-beginning-position 2))
  12243. ;; If we removed last keyword, do not leave trailing
  12244. ;; white space at the end of line.
  12245. (let ((p (point)))
  12246. (save-excursion
  12247. (end-of-line)
  12248. (unless (= (skip-chars-backward " \t" p) 0)
  12249. (delete-region (point) (line-end-position)))))))
  12250. ((not what) (throw 'exit nil)) ; Nothing to do.
  12251. (t (insert-before-markers "\n")
  12252. (backward-char 1)
  12253. (when org-adapt-indentation
  12254. (org-indent-to-column (1+ (org-outline-level))))))
  12255. (when what
  12256. ;; Insert planning keyword.
  12257. (insert (cl-case what
  12258. (closed org-closed-string)
  12259. (deadline org-deadline-string)
  12260. (scheduled org-scheduled-string)
  12261. (otherwise (error "Invalid planning type: %s" what)))
  12262. " ")
  12263. ;; Insert associated timestamp.
  12264. (let ((ts (org-insert-time-stamp
  12265. time
  12266. (or org-time-was-given
  12267. (and (eq what 'closed) org-log-done-with-time))
  12268. (eq what 'closed)
  12269. nil nil (list org-end-time-was-given))))
  12270. (unless (eolp) (insert " "))
  12271. ts))))))
  12272. (defvar org-log-note-marker (make-marker)
  12273. "Marker pointing at the entry where the note is to be inserted.")
  12274. (defvar org-log-note-purpose nil)
  12275. (defvar org-log-note-state nil)
  12276. (defvar org-log-note-previous-state nil)
  12277. (defvar org-log-note-extra nil)
  12278. (defvar org-log-note-window-configuration nil)
  12279. (defvar org-log-note-return-to (make-marker))
  12280. (defvar org-log-note-effective-time nil
  12281. "Remembered current time so that dynamically scoped
  12282. `org-extend-today-until' affects timestamps in state change log")
  12283. (defvar org-log-post-message nil
  12284. "Message to be displayed after a log note has been stored.
  12285. The auto-repeater uses this.")
  12286. (defun org-add-note ()
  12287. "Add a note to the current entry.
  12288. This is done in the same way as adding a state change note."
  12289. (interactive)
  12290. (org-add-log-setup 'note))
  12291. (defun org-log-beginning (&optional create)
  12292. "Return expected start of log notes in current entry.
  12293. When optional argument CREATE is non-nil, the function creates
  12294. a drawer to store notes, if necessary. Returned position ignores
  12295. narrowing."
  12296. (org-with-wide-buffer
  12297. (let ((drawer (org-log-into-drawer)))
  12298. (cond
  12299. (drawer
  12300. (org-end-of-meta-data)
  12301. (let ((regexp (concat "^[ \t]*:" (regexp-quote drawer) ":[ \t]*$"))
  12302. (end (if (org-at-heading-p) (point)
  12303. (save-excursion (outline-next-heading) (point))))
  12304. (case-fold-search t))
  12305. (catch 'exit
  12306. ;; Try to find existing drawer.
  12307. (while (re-search-forward regexp end t)
  12308. (let ((element (org-element-at-point)))
  12309. (when (eq (org-element-type element) 'drawer)
  12310. (let ((cend (org-element-property :contents-end element)))
  12311. (when (and (not org-log-states-order-reversed) cend)
  12312. (goto-char cend)))
  12313. (throw 'exit nil))))
  12314. ;; No drawer found. Create one, if permitted.
  12315. (when create
  12316. (unless (bolp) (insert "\n"))
  12317. (let ((beg (point)))
  12318. (insert ":" drawer ":\n:END:\n")
  12319. (org-indent-region beg (point)))
  12320. (end-of-line -1)))))
  12321. (t
  12322. (org-end-of-meta-data org-log-state-notes-insert-after-drawers)
  12323. (skip-chars-forward " \t\n")
  12324. (beginning-of-line)
  12325. (unless org-log-states-order-reversed
  12326. (org-skip-over-state-notes)
  12327. (skip-chars-backward " \t\n")
  12328. (forward-line)))))
  12329. (if (bolp) (point) (line-beginning-position 2))))
  12330. (defun org-add-log-setup (&optional purpose state prev-state how extra)
  12331. "Set up the post command hook to take a note.
  12332. If this is about to TODO state change, the new state is expected in STATE.
  12333. HOW is an indicator what kind of note should be created.
  12334. EXTRA is additional text that will be inserted into the notes buffer."
  12335. (move-marker org-log-note-marker (point))
  12336. (setq org-log-note-purpose purpose
  12337. org-log-note-state state
  12338. org-log-note-previous-state prev-state
  12339. org-log-note-how how
  12340. org-log-note-extra extra
  12341. org-log-note-effective-time (org-current-effective-time))
  12342. (add-hook 'post-command-hook 'org-add-log-note 'append))
  12343. (defun org-skip-over-state-notes ()
  12344. "Skip past the list of State notes in an entry."
  12345. (when (ignore-errors (goto-char (org-in-item-p)))
  12346. (let* ((struct (org-list-struct))
  12347. (prevs (org-list-prevs-alist struct))
  12348. (regexp
  12349. (concat "[ \t]*- +"
  12350. (replace-regexp-in-string
  12351. " +" " +"
  12352. (org-replace-escapes
  12353. (regexp-quote (cdr (assq 'state org-log-note-headings)))
  12354. `(("%d" . ,org-ts-regexp-inactive)
  12355. ("%D" . ,org-ts-regexp)
  12356. ("%s" . "\"\\S-+\"")
  12357. ("%S" . "\"\\S-+\"")
  12358. ("%t" . ,org-ts-regexp-inactive)
  12359. ("%T" . ,org-ts-regexp)
  12360. ("%u" . ".*?")
  12361. ("%U" . ".*?")))))))
  12362. (while (org-looking-at-p regexp)
  12363. (goto-char (or (org-list-get-next-item (point) struct prevs)
  12364. (org-list-get-item-end (point) struct)))))))
  12365. (defun org-add-log-note (&optional _purpose)
  12366. "Pop up a window for taking a note, and add this note later."
  12367. (remove-hook 'post-command-hook 'org-add-log-note)
  12368. (setq org-log-note-window-configuration (current-window-configuration))
  12369. (delete-other-windows)
  12370. (move-marker org-log-note-return-to (point))
  12371. (org-pop-to-buffer-same-window (marker-buffer org-log-note-marker))
  12372. (goto-char org-log-note-marker)
  12373. (org-switch-to-buffer-other-window "*Org Note*")
  12374. (erase-buffer)
  12375. (if (memq org-log-note-how '(time state))
  12376. (let (current-prefix-arg) (org-store-log-note))
  12377. (let ((org-inhibit-startup t)) (org-mode))
  12378. (insert (format "# Insert note for %s.
  12379. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  12380. (cond
  12381. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  12382. ((eq org-log-note-purpose 'done) "closed todo item")
  12383. ((eq org-log-note-purpose 'state)
  12384. (format "state change from \"%s\" to \"%s\""
  12385. (or org-log-note-previous-state "")
  12386. (or org-log-note-state "")))
  12387. ((eq org-log-note-purpose 'reschedule)
  12388. "rescheduling")
  12389. ((eq org-log-note-purpose 'delschedule)
  12390. "no longer scheduled")
  12391. ((eq org-log-note-purpose 'redeadline)
  12392. "changing deadline")
  12393. ((eq org-log-note-purpose 'deldeadline)
  12394. "removing deadline")
  12395. ((eq org-log-note-purpose 'refile)
  12396. "refiling")
  12397. ((eq org-log-note-purpose 'note)
  12398. "this entry")
  12399. (t (error "This should not happen")))))
  12400. (when org-log-note-extra (insert org-log-note-extra))
  12401. (setq-local org-finish-function 'org-store-log-note)
  12402. (run-hooks 'org-log-buffer-setup-hook)))
  12403. (defvar org-note-abort nil) ; dynamically scoped
  12404. (defun org-store-log-note ()
  12405. "Finish taking a log note, and insert it to where it belongs."
  12406. (let ((txt (prog1 (buffer-string)
  12407. (kill-buffer)))
  12408. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  12409. lines)
  12410. (while (string-match "\\`# .*\n[ \t\n]*" txt)
  12411. (setq txt (replace-match "" t t txt)))
  12412. (when (string-match "\\s-+\\'" txt)
  12413. (setq txt (replace-match "" t t txt)))
  12414. (setq lines (org-split-string txt "\n"))
  12415. (when (org-string-nw-p note)
  12416. (setq note
  12417. (org-replace-escapes
  12418. note
  12419. (list (cons "%u" (user-login-name))
  12420. (cons "%U" user-full-name)
  12421. (cons "%t" (format-time-string
  12422. (org-time-stamp-format 'long 'inactive)
  12423. org-log-note-effective-time))
  12424. (cons "%T" (format-time-string
  12425. (org-time-stamp-format 'long nil)
  12426. org-log-note-effective-time))
  12427. (cons "%d" (format-time-string
  12428. (org-time-stamp-format nil 'inactive)
  12429. org-log-note-effective-time))
  12430. (cons "%D" (format-time-string
  12431. (org-time-stamp-format nil nil)
  12432. org-log-note-effective-time))
  12433. (cons "%s" (cond
  12434. ((not org-log-note-state) "")
  12435. ((org-string-match-p org-ts-regexp
  12436. org-log-note-state)
  12437. (format "\"[%s]\""
  12438. (substring org-log-note-state 1 -1)))
  12439. (t (format "\"%s\"" org-log-note-state))))
  12440. (cons "%S"
  12441. (cond
  12442. ((not org-log-note-previous-state) "")
  12443. ((org-string-match-p org-ts-regexp
  12444. org-log-note-previous-state)
  12445. (format "\"[%s]\""
  12446. (substring
  12447. org-log-note-previous-state 1 -1)))
  12448. (t (format "\"%s\""
  12449. org-log-note-previous-state)))))))
  12450. (when lines (setq note (concat note " \\\\")))
  12451. (push note lines))
  12452. (when (and lines (not (or current-prefix-arg org-note-abort)))
  12453. (with-current-buffer (marker-buffer org-log-note-marker)
  12454. (org-with-wide-buffer
  12455. ;; Find location for the new note.
  12456. (goto-char org-log-note-marker)
  12457. (set-marker org-log-note-marker nil)
  12458. (goto-char (org-log-beginning t))
  12459. ;; Make sure point is at the beginning of an empty line.
  12460. (cond ((not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  12461. ((looking-at "[ \t]*\\S-") (save-excursion (insert "\n"))))
  12462. ;; In an existing list, add a new item at the top level.
  12463. ;; Otherwise, indent line like a regular one.
  12464. (let ((itemp (org-in-item-p)))
  12465. (if itemp
  12466. (org-indent-line-to
  12467. (let ((struct (save-excursion
  12468. (goto-char itemp) (org-list-struct))))
  12469. (org-list-get-ind (org-list-get-top-point struct) struct)))
  12470. (org-indent-line)))
  12471. (insert (org-list-bullet-string "-") (pop lines))
  12472. (let ((ind (org-list-item-body-column (line-beginning-position))))
  12473. (dolist (line lines)
  12474. (insert "\n")
  12475. (org-indent-line-to ind)
  12476. (insert line)))
  12477. (message "Note stored")
  12478. (org-back-to-heading t)
  12479. (org-cycle-hide-drawers 'children))
  12480. ;; Fix `buffer-undo-list' when `org-store-log-note' is called
  12481. ;; from within `org-add-log-note' because `buffer-undo-list'
  12482. ;; is then modified outside of `org-with-remote-undo'.
  12483. (when (eq this-command 'org-agenda-todo)
  12484. (setcdr buffer-undo-list (cddr buffer-undo-list))))))
  12485. ;; Don't add undo information when called from `org-agenda-todo'.
  12486. (let ((buffer-undo-list (eq this-command 'org-agenda-todo)))
  12487. (set-window-configuration org-log-note-window-configuration)
  12488. (with-current-buffer (marker-buffer org-log-note-return-to)
  12489. (goto-char org-log-note-return-to))
  12490. (move-marker org-log-note-return-to nil)
  12491. (when org-log-post-message (message "%s" org-log-post-message))))
  12492. (defun org-remove-empty-drawer-at (pos)
  12493. "Remove an empty drawer at position POS.
  12494. POS may also be a marker."
  12495. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  12496. (org-with-wide-buffer
  12497. (goto-char pos)
  12498. (let ((drawer (org-element-at-point)))
  12499. (when (and (memq (org-element-type drawer) '(drawer property-drawer))
  12500. (not (org-element-property :contents-begin drawer)))
  12501. (delete-region (org-element-property :begin drawer)
  12502. (progn (goto-char (org-element-property :end drawer))
  12503. (skip-chars-backward " \r\t\n")
  12504. (forward-line)
  12505. (point))))))))
  12506. (defvar org-ts-type nil)
  12507. (defun org-sparse-tree (&optional arg type)
  12508. "Create a sparse tree, prompt for the details.
  12509. This command can create sparse trees. You first need to select the type
  12510. of match used to create the tree:
  12511. t Show all TODO entries.
  12512. T Show entries with a specific TODO keyword.
  12513. m Show entries selected by a tags/property match.
  12514. p Enter a property name and its value (both with completion on existing
  12515. names/values) and show entries with that property.
  12516. r Show entries matching a regular expression (`/' can be used as well).
  12517. b Show deadlines and scheduled items before a date.
  12518. a Show deadlines and scheduled items after a date.
  12519. d Show deadlines due within `org-deadline-warning-days'.
  12520. D Show deadlines and scheduled items between a date range."
  12521. (interactive "P")
  12522. (setq type (or type org-sparse-tree-default-date-type))
  12523. (setq org-ts-type type)
  12524. (message "Sparse tree: [/]regexp [t]odo [T]odo-kwd [m]atch [p]roperty
  12525. \[d]eadlines [b]efore-date [a]fter-date [D]ates range
  12526. \[c]ycle through date types: %s"
  12527. (cl-case type
  12528. (all "all timestamps")
  12529. (scheduled "only scheduled")
  12530. (deadline "only deadline")
  12531. (active "only active timestamps")
  12532. (inactive "only inactive timestamps")
  12533. (closed "with a closed time-stamp")
  12534. (otherwise "scheduled/deadline")))
  12535. (let ((answer (read-char-exclusive)))
  12536. (cl-case answer
  12537. (?c
  12538. (org-sparse-tree
  12539. arg
  12540. (cadr
  12541. (memq type '(nil all scheduled deadline active inactive closed)))))
  12542. (?d (call-interactively 'org-check-deadlines))
  12543. (?b (call-interactively 'org-check-before-date))
  12544. (?a (call-interactively 'org-check-after-date))
  12545. (?D (call-interactively 'org-check-dates-range))
  12546. (?t (call-interactively 'org-show-todo-tree))
  12547. (?T (org-show-todo-tree '(4)))
  12548. (?m (call-interactively 'org-match-sparse-tree))
  12549. ((?p ?P)
  12550. (let* ((kwd (completing-read
  12551. "Property: " (mapcar #'list (org-buffer-property-keys))))
  12552. (value (completing-read
  12553. "Value: " (mapcar #'list (org-property-values kwd)))))
  12554. (unless (string-match "\\`{.*}\\'" value)
  12555. (setq value (concat "\"" value "\"")))
  12556. (org-match-sparse-tree arg (concat kwd "=" value))))
  12557. ((?r ?R ?/) (call-interactively 'org-occur))
  12558. (otherwise (user-error "No such sparse tree command \"%c\"" answer)))))
  12559. (defvar-local org-occur-highlights nil
  12560. "List of overlays used for occur matches.")
  12561. (defvar-local org-occur-parameters nil
  12562. "Parameters of the active org-occur calls.
  12563. This is a list, each call to org-occur pushes as cons cell,
  12564. containing the regular expression and the callback, onto the list.
  12565. The list can contain several entries if `org-occur' has been called
  12566. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  12567. will only contain one set of parameters. When the highlights are
  12568. removed (for example with `C-c C-c', or with the next edit (depending
  12569. on `org-remove-highlights-with-change'), this variable is emptied
  12570. as well.")
  12571. (defun org-occur (regexp &optional keep-previous callback)
  12572. "Make a compact tree which shows all matches of REGEXP.
  12573. The tree will show the lines where the regexp matches, and all higher
  12574. headlines above the match. It will also show the heading after the match,
  12575. to make sure editing the matching entry is easy.
  12576. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  12577. call to `org-occur' will be kept, to allow stacking of calls to this
  12578. command.
  12579. If CALLBACK is non-nil, it is a function which is called to confirm
  12580. that the match should indeed be shown."
  12581. (interactive "sRegexp: \nP")
  12582. (when (equal regexp "")
  12583. (user-error "Regexp cannot be empty"))
  12584. (unless keep-previous
  12585. (org-remove-occur-highlights nil nil t))
  12586. (push (cons regexp callback) org-occur-parameters)
  12587. (let ((cnt 0))
  12588. (save-excursion
  12589. (goto-char (point-min))
  12590. (when (or (not keep-previous) ; do not want to keep
  12591. (not org-occur-highlights)) ; no previous matches
  12592. ;; hide everything
  12593. (org-overview))
  12594. (while (re-search-forward regexp nil t)
  12595. (backward-char) ;; FIXME: Match timestamps at the end of a headline
  12596. (when (or (not callback)
  12597. (save-match-data (funcall callback)))
  12598. (setq cnt (1+ cnt))
  12599. (when org-highlight-sparse-tree-matches
  12600. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  12601. (org-show-context 'occur-tree))))
  12602. (when org-remove-highlights-with-change
  12603. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  12604. nil 'local))
  12605. (unless org-sparse-tree-open-archived-trees
  12606. (org-hide-archived-subtrees (point-min) (point-max)))
  12607. (run-hooks 'org-occur-hook)
  12608. (when (org-called-interactively-p 'interactive)
  12609. (message "%d match(es) for regexp %s" cnt regexp))
  12610. cnt))
  12611. (defun org-occur-next-match (&optional n _reset)
  12612. "Function for `next-error-function' to find sparse tree matches.
  12613. N is the number of matches to move, when negative move backwards.
  12614. This function always goes back to the starting point when no
  12615. match is found."
  12616. (let* ((limit (if (< n 0) (point-min) (point-max)))
  12617. (search-func (if (< n 0)
  12618. 'previous-single-char-property-change
  12619. 'next-single-char-property-change))
  12620. (n (abs n))
  12621. (pos (point))
  12622. p1)
  12623. (catch 'exit
  12624. (while (setq p1 (funcall search-func (point) 'org-type))
  12625. (when (equal p1 limit)
  12626. (goto-char pos)
  12627. (user-error "No more matches"))
  12628. (when (equal (get-char-property p1 'org-type) 'org-occur)
  12629. (setq n (1- n))
  12630. (when (= n 0)
  12631. (goto-char p1)
  12632. (throw 'exit (point))))
  12633. (goto-char p1))
  12634. (goto-char p1)
  12635. (user-error "No more matches"))))
  12636. (defun org-show-context (&optional key)
  12637. "Make sure point and context are visible.
  12638. Optional argument KEY, when non-nil, is a symbol. See
  12639. `org-show-context-detail' for allowed values and how much is to
  12640. be shown."
  12641. (org-show-set-visibility
  12642. (cond ((symbolp org-show-context-detail) org-show-context-detail)
  12643. ((cdr (assq key org-show-context-detail)))
  12644. (t (cdr (assq 'default org-show-context-detail))))))
  12645. (defun org-show-set-visibility (detail)
  12646. "Set visibility around point according to DETAIL.
  12647. DETAIL is either nil, `minimal', `local', `ancestors', `lineage',
  12648. `tree', `canonical' or t. See `org-show-context-detail' for more
  12649. information."
  12650. (unless (org-before-first-heading-p)
  12651. ;; Show current heading and possibly its entry, following headline
  12652. ;; or all children.
  12653. (if (and (org-at-heading-p) (not (eq detail 'local)))
  12654. (org-flag-heading nil)
  12655. (org-show-entry)
  12656. (org-with-limited-levels
  12657. (cl-case detail
  12658. ((tree canonical t) (org-show-children))
  12659. ((nil minimal ancestors))
  12660. (t (save-excursion
  12661. (outline-next-heading)
  12662. (org-flag-heading nil))))))
  12663. ;; Show all siblings.
  12664. (when (eq detail 'lineage) (org-show-siblings))
  12665. ;; Show ancestors, possibly with their children.
  12666. (when (memq detail '(ancestors lineage tree canonical t))
  12667. (save-excursion
  12668. (while (org-up-heading-safe)
  12669. (org-flag-heading nil)
  12670. (when (memq detail '(canonical t)) (org-show-entry))
  12671. (when (memq detail '(tree canonical t)) (org-show-children)))))))
  12672. (defvar org-reveal-start-hook nil
  12673. "Hook run before revealing a location.")
  12674. (defun org-reveal (&optional siblings)
  12675. "Show current entry, hierarchy above it, and the following headline.
  12676. This can be used to show a consistent set of context around
  12677. locations exposed with `org-show-context'.
  12678. With optional argument SIBLINGS, on each level of the hierarchy all
  12679. siblings are shown. This repairs the tree structure to what it would
  12680. look like when opened with hierarchical calls to `org-cycle'.
  12681. With double optional argument \\[universal-argument] \\[universal-argument], \
  12682. go to the parent and show the
  12683. entire tree."
  12684. (interactive "P")
  12685. (run-hooks 'org-reveal-start-hook)
  12686. (cond ((equal siblings '(4)) (org-show-set-visibility 'canonical))
  12687. ((equal siblings '(16))
  12688. (save-excursion
  12689. (when (org-up-heading-safe)
  12690. (org-show-subtree)
  12691. (run-hook-with-args 'org-cycle-hook 'subtree))))
  12692. (t (org-show-set-visibility 'lineage))))
  12693. (defun org-highlight-new-match (beg end)
  12694. "Highlight from BEG to END and mark the highlight is an occur headline."
  12695. (let ((ov (make-overlay beg end)))
  12696. (overlay-put ov 'face 'secondary-selection)
  12697. (overlay-put ov 'org-type 'org-occur)
  12698. (push ov org-occur-highlights)))
  12699. (defun org-remove-occur-highlights (&optional _beg _end noremove)
  12700. "Remove the occur highlights from the buffer.
  12701. BEG and END are ignored. If NOREMOVE is nil, remove this function
  12702. from the `before-change-functions' in the current buffer."
  12703. (interactive)
  12704. (unless org-inhibit-highlight-removal
  12705. (mapc #'delete-overlay org-occur-highlights)
  12706. (setq org-occur-highlights nil)
  12707. (setq org-occur-parameters nil)
  12708. (unless noremove
  12709. (remove-hook 'before-change-functions
  12710. 'org-remove-occur-highlights 'local))))
  12711. ;;;; Priorities
  12712. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  12713. "Regular expression matching the priority indicator.")
  12714. (defvar org-remove-priority-next-time nil)
  12715. (defun org-priority-up ()
  12716. "Increase the priority of the current item."
  12717. (interactive)
  12718. (org-priority 'up))
  12719. (defun org-priority-down ()
  12720. "Decrease the priority of the current item."
  12721. (interactive)
  12722. (org-priority 'down))
  12723. (defun org-priority (&optional action _show)
  12724. "Change the priority of an item.
  12725. ACTION can be `set', `up', `down', or a character."
  12726. (interactive "P")
  12727. (if (equal action '(4))
  12728. (org-show-priority)
  12729. (unless org-enable-priority-commands
  12730. (user-error "Priority commands are disabled"))
  12731. (setq action (or action 'set))
  12732. (let (current new news have remove)
  12733. (save-excursion
  12734. (org-back-to-heading t)
  12735. (when (looking-at org-priority-regexp)
  12736. (setq current (string-to-char (match-string 2))
  12737. have t))
  12738. (cond
  12739. ((eq action 'remove)
  12740. (setq remove t new ?\ ))
  12741. ((or (eq action 'set)
  12742. (if (featurep 'xemacs) (characterp action) (integerp action)))
  12743. (if (not (eq action 'set))
  12744. (setq new action)
  12745. (message "Priority %c-%c, SPC to remove: "
  12746. org-highest-priority org-lowest-priority)
  12747. (save-match-data
  12748. (setq new (read-char-exclusive))))
  12749. (when (and (= (upcase org-highest-priority) org-highest-priority)
  12750. (= (upcase org-lowest-priority) org-lowest-priority))
  12751. (setq new (upcase new)))
  12752. (cond ((equal new ?\ ) (setq remove t))
  12753. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  12754. (user-error "Priority must be between `%c' and `%c'"
  12755. org-highest-priority org-lowest-priority))))
  12756. ((eq action 'up)
  12757. (setq new (if have
  12758. (1- current) ; normal cycling
  12759. ;; last priority was empty
  12760. (if (eq last-command this-command)
  12761. org-lowest-priority ; wrap around empty to lowest
  12762. ;; default
  12763. (if org-priority-start-cycle-with-default
  12764. org-default-priority
  12765. (1- org-default-priority))))))
  12766. ((eq action 'down)
  12767. (setq new (if have
  12768. (1+ current) ; normal cycling
  12769. ;; last priority was empty
  12770. (if (eq last-command this-command)
  12771. org-highest-priority ; wrap around empty to highest
  12772. ;; default
  12773. (if org-priority-start-cycle-with-default
  12774. org-default-priority
  12775. (1+ org-default-priority))))))
  12776. (t (user-error "Invalid action")))
  12777. (when (or (< (upcase new) org-highest-priority)
  12778. (> (upcase new) org-lowest-priority))
  12779. (if (and (memq action '(up down))
  12780. (not have) (not (eq last-command this-command)))
  12781. ;; `new' is from default priority
  12782. (error
  12783. "The default can not be set, see `org-default-priority' why")
  12784. ;; normal cycling: `new' is beyond highest/lowest priority
  12785. ;; and is wrapped around to the empty priority
  12786. (setq remove t)))
  12787. (setq news (format "%c" new))
  12788. (if have
  12789. (if remove
  12790. (replace-match "" t t nil 1)
  12791. (replace-match news t t nil 2))
  12792. (if remove
  12793. (user-error "No priority cookie found in line")
  12794. (let ((case-fold-search nil))
  12795. (looking-at org-todo-line-regexp))
  12796. (if (match-end 2)
  12797. (progn
  12798. (goto-char (match-end 2))
  12799. (insert " [#" news "]"))
  12800. (goto-char (match-beginning 3))
  12801. (insert "[#" news "] "))))
  12802. (org-set-tags nil 'align))
  12803. (if remove
  12804. (message "Priority removed")
  12805. (message "Priority of current item set to %s" news)))))
  12806. (defun org-show-priority ()
  12807. "Show the priority of the current item.
  12808. This priority is composed of the main priority given with the [#A] cookies,
  12809. and by additional input from the age of a schedules or deadline entry."
  12810. (interactive)
  12811. (let ((pri (if (eq major-mode 'org-agenda-mode)
  12812. (org-get-at-bol 'priority)
  12813. (save-excursion
  12814. (save-match-data
  12815. (beginning-of-line)
  12816. (and (looking-at org-heading-regexp)
  12817. (org-get-priority (match-string 0))))))))
  12818. (message "Priority is %d" (if pri pri -1000))))
  12819. (defun org-get-priority (s)
  12820. "Find priority cookie and return priority."
  12821. (save-match-data
  12822. (if (functionp org-get-priority-function)
  12823. (funcall org-get-priority-function)
  12824. (if (not (string-match org-priority-regexp s))
  12825. (* 1000 (- org-lowest-priority org-default-priority))
  12826. (* 1000 (- org-lowest-priority
  12827. (string-to-char (match-string 2 s))))))))
  12828. ;;;; Tags
  12829. (defvar org-agenda-archives-mode)
  12830. (defvar org-map-continue-from nil
  12831. "Position from where mapping should continue.
  12832. Can be set by the action argument to `org-scan-tags' and `org-map-entries'.")
  12833. (defvar org-scanner-tags nil
  12834. "The current tag list while the tags scanner is running.")
  12835. (defvar org-trust-scanner-tags nil
  12836. "Should `org-get-tags-at' use the tags for the scanner.
  12837. This is for internal dynamical scoping only.
  12838. When this is non-nil, the function `org-get-tags-at' will return the value
  12839. of `org-scanner-tags' instead of building the list by itself. This
  12840. can lead to large speed-ups when the tags scanner is used in a file with
  12841. many entries, and when the list of tags is retrieved, for example to
  12842. obtain a list of properties. Building the tags list for each entry in such
  12843. a file becomes an N^2 operation - but with this variable set, it scales
  12844. as N.")
  12845. (defvar org--matcher-tags-todo-only nil)
  12846. (defun org-scan-tags (action matcher todo-only &optional start-level)
  12847. "Scan headline tags with inheritance and produce output ACTION.
  12848. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  12849. or `agenda' to produce an entry list for an agenda view. It can also be
  12850. a Lisp form or a function that should be called at each matched headline, in
  12851. this case the return value is a list of all return values from these calls.
  12852. MATCHER is a function accepting three arguments, returning
  12853. a non-nil value whenever a given set of tags qualifies a headline
  12854. for inclusion. See `org-make-tags-matcher' for more information.
  12855. As a special case, it can also be set to t (respectively nil) in
  12856. order to match all (respectively none) headline.
  12857. When TODO-ONLY is non-nil, only lines with a not-done TODO
  12858. keyword are included in the output.
  12859. START-LEVEL can be a string with asterisks, reducing the scope to
  12860. headlines matching this string."
  12861. (require 'org-agenda)
  12862. (let* ((re (concat "^"
  12863. (if start-level
  12864. ;; Get the correct level to match
  12865. (concat "\\*\\{" (number-to-string start-level) "\\} ")
  12866. org-outline-regexp)
  12867. " *\\(\\<\\("
  12868. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  12869. (org-re "\\)\\>\\)? *\\(.*?\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*$")))
  12870. (props (list 'face 'default
  12871. 'done-face 'org-agenda-done
  12872. 'undone-face 'default
  12873. 'mouse-face 'highlight
  12874. 'org-not-done-regexp org-not-done-regexp
  12875. 'org-todo-regexp org-todo-regexp
  12876. 'org-complex-heading-regexp org-complex-heading-regexp
  12877. 'help-echo
  12878. (format "mouse-2 or RET jump to org file %s"
  12879. (abbreviate-file-name
  12880. (or (buffer-file-name (buffer-base-buffer))
  12881. (buffer-name (buffer-base-buffer)))))))
  12882. (org-map-continue-from nil)
  12883. lspos tags tags-list
  12884. (tags-alist (list (cons 0 org-file-tags)))
  12885. (llast 0) rtn rtn1 level category i txt
  12886. todo marker entry priority
  12887. ts-date ts-date-type ts-date-pair)
  12888. (unless (or (member action '(agenda sparse-tree)) (functionp action))
  12889. (setq action (list 'lambda nil action)))
  12890. (save-excursion
  12891. (goto-char (point-min))
  12892. (when (eq action 'sparse-tree)
  12893. (org-overview)
  12894. (org-remove-occur-highlights))
  12895. (while (let (case-fold-search)
  12896. (re-search-forward re nil t))
  12897. (setq org-map-continue-from nil)
  12898. (catch :skip
  12899. (setq todo
  12900. ;; TODO: is the 1-2 difference a bug?
  12901. (when (match-end 1) (org-match-string-no-properties 2))
  12902. tags (when (match-end 4) (org-match-string-no-properties 4)))
  12903. (goto-char (setq lspos (match-beginning 0)))
  12904. (setq level (org-reduced-level (org-outline-level))
  12905. category (org-get-category))
  12906. (when (eq action 'agenda)
  12907. (setq ts-date-pair (org-agenda-entry-get-agenda-timestamp (point))
  12908. ts-date (car ts-date-pair)
  12909. ts-date-type (cdr ts-date-pair)))
  12910. (setq i llast llast level)
  12911. ;; remove tag lists from same and sublevels
  12912. (while (>= i level)
  12913. (when (setq entry (assoc i tags-alist))
  12914. (setq tags-alist (delete entry tags-alist)))
  12915. (setq i (1- i)))
  12916. ;; add the next tags
  12917. (when tags
  12918. (setq tags (org-split-string tags ":")
  12919. tags-alist
  12920. (cons (cons level tags) tags-alist)))
  12921. ;; compile tags for current headline
  12922. (setq tags-list
  12923. (if org-use-tag-inheritance
  12924. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  12925. tags)
  12926. org-scanner-tags tags-list)
  12927. (when org-use-tag-inheritance
  12928. (setcdr (car tags-alist)
  12929. (mapcar (lambda (x)
  12930. (setq x (copy-sequence x))
  12931. (org-add-prop-inherited x))
  12932. (cdar tags-alist))))
  12933. (when (and tags org-use-tag-inheritance
  12934. (or (not (eq t org-use-tag-inheritance))
  12935. org-tags-exclude-from-inheritance))
  12936. ;; Selective inheritance, remove uninherited ones.
  12937. (setcdr (car tags-alist)
  12938. (org-remove-uninherited-tags (cdar tags-alist))))
  12939. (when (and
  12940. ;; eval matcher only when the todo condition is OK
  12941. (and (or (not todo-only) (member todo org-not-done-keywords))
  12942. (if (functionp matcher)
  12943. (let ((case-fold-search t) (org-trust-scanner-tags t))
  12944. (funcall matcher todo tags-list level))
  12945. matcher))
  12946. ;; Call the skipper, but return t if it does not
  12947. ;; skip, so that the `and' form continues evaluating.
  12948. (progn
  12949. (unless (eq action 'sparse-tree) (org-agenda-skip))
  12950. t)
  12951. ;; Check if timestamps are deselecting this entry
  12952. (or (not todo-only)
  12953. (and (member todo org-not-done-keywords)
  12954. (or (not org-agenda-tags-todo-honor-ignore-options)
  12955. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item))))))
  12956. ;; select this headline
  12957. (cond
  12958. ((eq action 'sparse-tree)
  12959. (and org-highlight-sparse-tree-matches
  12960. (org-get-heading) (match-end 0)
  12961. (org-highlight-new-match
  12962. (match-beginning 1) (match-end 1)))
  12963. (org-show-context 'tags-tree))
  12964. ((eq action 'agenda)
  12965. (setq txt (org-agenda-format-item
  12966. ""
  12967. (concat
  12968. (if (eq org-tags-match-list-sublevels 'indented)
  12969. (make-string (1- level) ?.) "")
  12970. (org-get-heading))
  12971. (make-string level ?\s)
  12972. category
  12973. tags-list)
  12974. priority (org-get-priority txt))
  12975. (goto-char lspos)
  12976. (setq marker (org-agenda-new-marker))
  12977. (org-add-props txt props
  12978. 'org-marker marker 'org-hd-marker marker 'org-category category
  12979. 'todo-state todo
  12980. 'ts-date ts-date
  12981. 'priority priority
  12982. 'type (concat "tagsmatch" ts-date-type))
  12983. (push txt rtn))
  12984. ((functionp action)
  12985. (setq org-map-continue-from nil)
  12986. (save-excursion
  12987. (setq rtn1 (funcall action))
  12988. (push rtn1 rtn)))
  12989. (t (user-error "Invalid action")))
  12990. ;; if we are to skip sublevels, jump to end of subtree
  12991. (unless org-tags-match-list-sublevels
  12992. (org-end-of-subtree t)
  12993. (backward-char 1))))
  12994. ;; Get the correct position from where to continue
  12995. (if org-map-continue-from
  12996. (goto-char org-map-continue-from)
  12997. (and (= (point) lspos) (end-of-line 1)))))
  12998. (when (and (eq action 'sparse-tree)
  12999. (not org-sparse-tree-open-archived-trees))
  13000. (org-hide-archived-subtrees (point-min) (point-max)))
  13001. (nreverse rtn)))
  13002. (defun org-remove-uninherited-tags (tags)
  13003. "Remove all tags that are not inherited from the list TAGS."
  13004. (cond
  13005. ((eq org-use-tag-inheritance t)
  13006. (if org-tags-exclude-from-inheritance
  13007. (org-delete-all org-tags-exclude-from-inheritance tags)
  13008. tags))
  13009. ((not org-use-tag-inheritance) nil)
  13010. ((stringp org-use-tag-inheritance)
  13011. (delq nil (mapcar
  13012. (lambda (x)
  13013. (if (and (string-match org-use-tag-inheritance x)
  13014. (not (member x org-tags-exclude-from-inheritance)))
  13015. x nil))
  13016. tags)))
  13017. ((listp org-use-tag-inheritance)
  13018. (delq nil (mapcar
  13019. (lambda (x)
  13020. (if (member x org-use-tag-inheritance) x nil))
  13021. tags)))))
  13022. (defun org-match-sparse-tree (&optional todo-only match)
  13023. "Create a sparse tree according to tags string MATCH.
  13024. MATCH can contain positive and negative selection of tags, like
  13025. \"+WORK+URGENT-WITHBOSS\".
  13026. If optional argument TODO-ONLY is non-nil, only select lines that are
  13027. also TODO lines."
  13028. (interactive "P")
  13029. (org-agenda-prepare-buffers (list (current-buffer)))
  13030. (let ((org--matcher-tags-todo-only todo-only))
  13031. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match))
  13032. org--matcher-tags-todo-only)))
  13033. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  13034. (defvar org-cached-props nil)
  13035. (defun org-cached-entry-get (pom property)
  13036. (if (or (eq t org-use-property-inheritance)
  13037. (and (stringp org-use-property-inheritance)
  13038. (let ((case-fold-search t))
  13039. (org-string-match-p org-use-property-inheritance property)))
  13040. (and (listp org-use-property-inheritance)
  13041. (member-ignore-case property org-use-property-inheritance)))
  13042. ;; Caching is not possible, check it directly.
  13043. (org-entry-get pom property 'inherit)
  13044. ;; Get all properties, so we can do complicated checks easily.
  13045. (cdr (assoc-string property
  13046. (or org-cached-props
  13047. (setq org-cached-props (org-entry-properties pom)))
  13048. t))))
  13049. (defun org-global-tags-completion-table (&optional files)
  13050. "Return the list of all tags in all agenda buffer/files.
  13051. Optional FILES argument is a list of files which can be used
  13052. instead of the agenda files."
  13053. (save-excursion
  13054. (org-uniquify
  13055. (delq nil
  13056. (apply 'append
  13057. (mapcar
  13058. (lambda (file)
  13059. (set-buffer (find-file-noselect file))
  13060. (append (org-get-buffer-tags)
  13061. (mapcar (lambda (x) (if (stringp (car-safe x))
  13062. (list (car-safe x)) nil))
  13063. org-tag-alist)))
  13064. (if (and files (car files))
  13065. files
  13066. (org-agenda-files))))))))
  13067. (defun org-make-tags-matcher (match)
  13068. "Create the TAGS/TODO matcher form for the selection string MATCH.
  13069. Returns a cons of the selection string MATCH and a function
  13070. implementing the matcher.
  13071. The matcher is to be called at an Org entry, with point on the
  13072. headline, and returns non-nil if the entry matches the selection
  13073. string MATCH. It must be called with three arguments: the TODO
  13074. keyword at the entry (or nil if none), the list of all tags at
  13075. the entry including inherited ones and the reduced level of the
  13076. headline. Additionally, the category of the entry, if any, must
  13077. be specified as the text property `org-category' on the headline.
  13078. This function sets the variable `org--matcher-tags-todo-only' to
  13079. a non-nil value if the matcher restricts matching to TODO
  13080. entries, otherwise it is not touched.
  13081. See also `org-scan-tags'."
  13082. (unless match
  13083. ;; Get a new match request, with completion against the global
  13084. ;; tags table and the local tags in current buffer.
  13085. (let ((org-last-tags-completion-table
  13086. (org-uniquify
  13087. (delq nil (append (org-get-buffer-tags)
  13088. (org-global-tags-completion-table))))))
  13089. (setq match
  13090. (completing-read
  13091. "Match: "
  13092. 'org-tags-completion-function nil nil nil 'org-tags-history))))
  13093. (let ((match0 match)
  13094. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\(\\(?:[[:alnum:]_]+\\(?:\\\\-\\)*\\)+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@#%]+\\)"))
  13095. (start 0)
  13096. tagsmatch todomatch tagsmatcher todomatcher)
  13097. ;; Expand group tags.
  13098. (setq match (org-tags-expand match))
  13099. ;; Check if there is a TODO part of this match, which would be the
  13100. ;; part after a "/". To make sure that this slash is not part of
  13101. ;; a property value to be matched against, we also check that
  13102. ;; there is no / after that slash. First, find the last slash.
  13103. (let ((s 0))
  13104. (while (string-match "/+" match s)
  13105. (setq start (match-beginning 0))
  13106. (setq s (match-end 0))))
  13107. (if (and (string-match "/+" match start)
  13108. (not (string-match-p "\"" match start)))
  13109. ;; Match contains also a TODO-matching request.
  13110. (progn
  13111. (setq tagsmatch (substring match 0 (match-beginning 0)))
  13112. (setq todomatch (substring match (match-end 0)))
  13113. (when (string-match "\\`!" todomatch)
  13114. (setq org--matcher-tags-todo-only t)
  13115. (setq todomatch (substring todomatch 1)))
  13116. (when (string-match "\\`\\s-*\\'" todomatch)
  13117. (setq todomatch nil)))
  13118. ;; Only matching tags.
  13119. (setq tagsmatch match)
  13120. (setq todomatch nil))
  13121. ;; Make the tags matcher.
  13122. (if (not (org-string-nw-p tagsmatch))
  13123. (setq tagsmatcher t)
  13124. (let ((orlist nil)
  13125. (orterms (org-split-string tagsmatch "|"))
  13126. term)
  13127. (while (setq term (pop orterms))
  13128. (while (and (equal (substring term -1) "\\") orterms)
  13129. (setq term (concat term "|" (pop orterms)))) ;repair bad split.
  13130. (while (string-match re term)
  13131. (let* ((rest (substring term (match-end 0)))
  13132. (minus (and (match-end 1)
  13133. (equal (match-string 1 term) "-")))
  13134. (tag (save-match-data
  13135. (replace-regexp-in-string
  13136. "\\\\-" "-" (match-string 2 term))))
  13137. (regexp (eq (string-to-char tag) ?{))
  13138. (levelp (match-end 4))
  13139. (propp (match-end 5))
  13140. (mm
  13141. (cond
  13142. (regexp `(org-match-any-p ,(substring tag 1 -1) tags-list))
  13143. (levelp
  13144. `(,(org-op-to-function (match-string 3 term))
  13145. level
  13146. ,(string-to-number (match-string 4 term))))
  13147. (propp
  13148. (let* ((gv (pcase (upcase (match-string 5 term))
  13149. ("CATEGORY"
  13150. '(get-text-property (point) 'org-category))
  13151. ("TODO" 'todo)
  13152. (p `(org-cached-entry-get nil ,p))))
  13153. (pv (match-string 7 term))
  13154. (regexp (eq (string-to-char pv) ?{))
  13155. (strp (eq (string-to-char pv) ?\"))
  13156. (timep (string-match-p "^\"[[<].*[]>]\"$" pv))
  13157. (po (org-op-to-function (match-string 6 term)
  13158. (if timep 'time strp))))
  13159. (setq pv (if (or regexp strp) (substring pv 1 -1) pv))
  13160. (when timep (setq pv (org-matcher-time pv)))
  13161. (cond ((and regexp (eq po 'org<>))
  13162. `(not (string-match ,pv (or ,gv ""))))
  13163. (regexp `(string-match ,pv (or ,gv "")))
  13164. (strp `(,po (or ,gv "") ,pv))
  13165. (t
  13166. `(,po
  13167. (string-to-number (or ,gv ""))
  13168. ,(string-to-number pv))))))
  13169. (t `(member ,tag tags-list)))))
  13170. (push (if minus `(not ,mm) mm) tagsmatcher)
  13171. (setq term rest)))
  13172. (push (if (> (length tagsmatcher) 1)
  13173. (cons 'and tagsmatcher)
  13174. (car tagsmatcher))
  13175. orlist)
  13176. (setq tagsmatcher nil))
  13177. (setq tagsmatcher
  13178. `(progn (setq org-cached-props nil) ,(cons 'or orlist)))))
  13179. ;; Make the TODO matcher.
  13180. (if (not (org-string-nw-p todomatch))
  13181. (setq todomatcher t)
  13182. (let ((orlist nil))
  13183. (dolist (term (org-split-string todomatch "|"))
  13184. (while (string-match re term)
  13185. (let* ((minus (and (match-end 1)
  13186. (equal (match-string 1 term) "-")))
  13187. (kwd (match-string 2 term))
  13188. (regexp (eq (string-to-char kwd) ?{))
  13189. (mm (if regexp `(string-match ,(substring kwd 1 -1) todo)
  13190. `(equal todo ,kwd))))
  13191. (push (if minus `(not ,mm) mm) todomatcher))
  13192. (setq term (substring term (match-end 0))))
  13193. (push (if (> (length todomatcher) 1)
  13194. (cons 'and todomatcher)
  13195. (car todomatcher))
  13196. orlist)
  13197. (setq todomatcher nil))
  13198. (setq todomatcher (cons 'or orlist))))
  13199. ;; Return the string and function of the matcher.
  13200. (let ((matcher (if todomatcher `(and ,tagsmatcher ,todomatcher)
  13201. tagsmatcher)))
  13202. (when org--matcher-tags-todo-only
  13203. (setq matcher `(and (member todo org-not-done-keywords) ,matcher)))
  13204. (cons match0 `(lambda (todo tags-list level) ,matcher)))))
  13205. (defun org-tags-expand (match &optional single-as-list downcased tags-already-expanded)
  13206. "Expand group tags in MATCH.
  13207. This replaces every group tag in MATCH with a regexp tag search.
  13208. For example, a group tag \"Work\" defined as { Work : Lab Conf }
  13209. will be replaced like this:
  13210. Work => {\\<\\(?:Work\\|Lab\\|Conf\\)\\>}
  13211. +Work => +{\\<\\(?:Work\\|Lab\\|Conf\\)\\>}
  13212. -Work => -{\\<\\(?:Work\\|Lab\\|Conf\\)\\>}
  13213. Replacing by a regexp preserves the structure of the match.
  13214. E.g., this expansion
  13215. Work|Home => {\\(?:Work\\|Lab\\|Conf\\}|Home
  13216. will match anything tagged with \"Lab\" and \"Home\", or tagged
  13217. with \"Conf\" and \"Home\" or tagged with \"Work\" and \"home\".
  13218. A group tag in MATCH can contain regular expressions of its own.
  13219. For example, a group tag \"Proj\" defined as { Proj : {P@.+} }
  13220. will be replaced like this:
  13221. Proj => {\\<\\(?:Proj\\)\\>\\|P@.+}
  13222. When the optional argument SINGLE-AS-LIST is non-nil, MATCH is
  13223. assumed to be a single group tag, and the function will return
  13224. the list of tags in this group.
  13225. When DOWNCASE is non-nil, expand downcased TAGS."
  13226. (if org-group-tags
  13227. (let* ((case-fold-search t)
  13228. (stable org-mode-syntax-table)
  13229. (taggroups (or org-tag-groups-alist-for-agenda org-tag-groups-alist))
  13230. (taggroups (if downcased
  13231. (mapcar (lambda (tg) (mapcar #'downcase tg))
  13232. taggroups)
  13233. taggroups))
  13234. (taggroups-keys (mapcar #'car taggroups))
  13235. (return-match (if downcased (downcase match) match))
  13236. (count 0)
  13237. (work-already-expanded tags-already-expanded)
  13238. regexps-in-match tags-in-group regexp-in-group regexp-in-group-escaped)
  13239. ;; @ and _ are allowed as word-components in tags.
  13240. (modify-syntax-entry ?@ "w" stable)
  13241. (modify-syntax-entry ?_ "w" stable)
  13242. ;; Temporarily replace regexp-expressions in the match-expression.
  13243. (while (string-match "{.+?}" return-match)
  13244. (cl-incf count)
  13245. (push (match-string 0 return-match) regexps-in-match)
  13246. (setq return-match (replace-match (format "<%d>" count) t nil return-match)))
  13247. (while (and taggroups-keys
  13248. (with-syntax-table stable
  13249. (string-match
  13250. (concat "\\(?1:[+-]?\\)\\(?2:\\<"
  13251. (regexp-opt taggroups-keys) "\\>\\)")
  13252. return-match)))
  13253. (let* ((dir (match-string 1 return-match))
  13254. (tag (match-string 2 return-match))
  13255. (tag (if downcased (downcase tag) tag)))
  13256. (unless (or (get-text-property 0 'grouptag (match-string 2 return-match))
  13257. (member tag work-already-expanded))
  13258. (setq tags-in-group (assoc tag taggroups))
  13259. (push tag work-already-expanded)
  13260. ;; Recursively expand each tag in the group, if the tag hasn't
  13261. ;; already been expanded. Restore the match-data after all recursive calls.
  13262. (save-match-data
  13263. (let (tags-expanded)
  13264. (dolist (x (cdr tags-in-group))
  13265. (if (and (member x taggroups-keys)
  13266. (not (member x work-already-expanded)))
  13267. (setq tags-expanded
  13268. (delete-dups
  13269. (append
  13270. (org-tags-expand x t downcased
  13271. work-already-expanded)
  13272. tags-expanded)))
  13273. (setq tags-expanded
  13274. (append (list x) tags-expanded)))
  13275. (setq work-already-expanded
  13276. (delete-dups
  13277. (append tags-expanded
  13278. work-already-expanded))))
  13279. (setq tags-in-group
  13280. (delete-dups (cons (car tags-in-group)
  13281. tags-expanded)))))
  13282. ;; Filter tag-regexps from tags.
  13283. (setq regexp-in-group-escaped
  13284. (delq nil (mapcar (lambda (x)
  13285. (if (stringp x)
  13286. (and (equal "{" (substring x 0 1))
  13287. (equal "}" (substring x -1))
  13288. x)
  13289. x))
  13290. tags-in-group))
  13291. regexp-in-group
  13292. (mapcar (lambda (x)
  13293. (substring x 1 -1))
  13294. regexp-in-group-escaped)
  13295. tags-in-group
  13296. (delq nil (mapcar (lambda (x)
  13297. (if (stringp x)
  13298. (and (not (equal "{" (substring x 0 1)))
  13299. (not (equal "}" (substring x -1)))
  13300. x)
  13301. x))
  13302. tags-in-group)))
  13303. ;; If single-as-list, do no more in the while-loop.
  13304. (if (not single-as-list)
  13305. (progn
  13306. (when regexp-in-group
  13307. (setq regexp-in-group
  13308. (concat "\\|"
  13309. (mapconcat 'identity regexp-in-group
  13310. "\\|"))))
  13311. (setq tags-in-group
  13312. (concat dir
  13313. "{\\<"
  13314. (regexp-opt tags-in-group)
  13315. "\\>"
  13316. regexp-in-group
  13317. "}"))
  13318. (when (stringp tags-in-group)
  13319. (org-add-props tags-in-group '(grouptag t)))
  13320. (setq return-match
  13321. (replace-match tags-in-group t t return-match)))
  13322. (setq tags-in-group
  13323. (append regexp-in-group-escaped tags-in-group))))
  13324. (setq taggroups-keys (delete tag taggroups-keys))))
  13325. ;; Add the regular expressions back into the match-expression again.
  13326. (while regexps-in-match
  13327. (setq return-match (replace-regexp-in-string (format "<%d>" count)
  13328. (pop regexps-in-match)
  13329. return-match t t))
  13330. (cl-decf count))
  13331. (if single-as-list
  13332. (if tags-in-group tags-in-group (list return-match))
  13333. return-match))
  13334. (if single-as-list
  13335. (list (if downcased (downcase match) match))
  13336. match)))
  13337. (defun org-op-to-function (op &optional stringp)
  13338. "Turn an operator into the appropriate function."
  13339. (setq op
  13340. (cond
  13341. ((equal op "<" ) '(< string< org-time<))
  13342. ((equal op ">" ) '(> org-string> org-time>))
  13343. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  13344. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  13345. ((member op '("=" "==")) '(= string= org-time=))
  13346. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  13347. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  13348. (defun org<> (a b) (not (= a b)))
  13349. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  13350. (defun org-string>= (a b) (not (string< a b)))
  13351. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  13352. (defun org-string<> (a b) (not (string= a b)))
  13353. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  13354. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  13355. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  13356. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  13357. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  13358. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  13359. (defun org-2ft (s)
  13360. "Convert S to a floating point time.
  13361. If S is already a number, just return it. If it is a string, parse
  13362. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  13363. (cond
  13364. ((numberp s) s)
  13365. ((stringp s)
  13366. (condition-case nil
  13367. (float-time (apply 'encode-time (org-parse-time-string s)))
  13368. (error 0.)))
  13369. (t 0.)))
  13370. (defun org-time-today ()
  13371. "Time in seconds today at 0:00.
  13372. Returns the float number of seconds since the beginning of the
  13373. epoch to the beginning of today (00:00)."
  13374. (float-time (apply 'encode-time
  13375. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  13376. (defun org-matcher-time (s)
  13377. "Interpret a time comparison value."
  13378. (save-match-data
  13379. (cond
  13380. ((string= s "<now>") (float-time))
  13381. ((string= s "<today>") (org-time-today))
  13382. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  13383. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  13384. ((string-match "^<\\([-+][0-9]+\\)\\([hdwmy]\\)>$" s)
  13385. (+ (org-time-today)
  13386. (* (string-to-number (match-string 1 s))
  13387. (cdr (assoc (match-string 2 s)
  13388. '(("d" . 86400.0) ("w" . 604800.0)
  13389. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  13390. (t (org-2ft s)))))
  13391. (defun org-match-any-p (re list)
  13392. "Does re match any element of list?"
  13393. (setq list (mapcar (lambda (x) (string-match re x)) list))
  13394. (delq nil list))
  13395. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  13396. (defvar org-tags-overlay (make-overlay 1 1))
  13397. (org-detach-overlay org-tags-overlay)
  13398. (defun org-get-local-tags-at (&optional pos)
  13399. "Get a list of tags defined in the current headline."
  13400. (org-get-tags-at pos 'local))
  13401. (defun org-get-local-tags ()
  13402. "Get a list of tags defined in the current headline."
  13403. (org-get-tags-at nil 'local))
  13404. (defun org-get-tags-at (&optional pos local)
  13405. "Get a list of all headline tags applicable at POS.
  13406. POS defaults to point. If tags are inherited, the list contains
  13407. the targets in the same sequence as the headlines appear, i.e.
  13408. the tags of the current headline come last.
  13409. When LOCAL is non-nil, only return tags from the current headline,
  13410. ignore inherited ones."
  13411. (interactive)
  13412. (if (and org-trust-scanner-tags
  13413. (or (not pos) (equal pos (point)))
  13414. (not local))
  13415. org-scanner-tags
  13416. (let (tags ltags lastpos parent)
  13417. (save-excursion
  13418. (save-restriction
  13419. (widen)
  13420. (goto-char (or pos (point)))
  13421. (save-match-data
  13422. (catch 'done
  13423. (condition-case nil
  13424. (progn
  13425. (org-back-to-heading t)
  13426. (while (not (equal lastpos (point)))
  13427. (setq lastpos (point))
  13428. (when (looking-at
  13429. (org-re "[^\r\n]+?:\\([[:alnum:]_@#%:]+\\):[ \t]*$"))
  13430. (setq ltags (org-split-string
  13431. (org-match-string-no-properties 1) ":"))
  13432. (when parent
  13433. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  13434. (setq tags (append
  13435. (if parent
  13436. (org-remove-uninherited-tags ltags)
  13437. ltags)
  13438. tags)))
  13439. (or org-use-tag-inheritance (throw 'done t))
  13440. (when local (throw 'done t))
  13441. (or (org-up-heading-safe) (error nil))
  13442. (setq parent t)))
  13443. (error nil)))))
  13444. (if local
  13445. tags
  13446. (reverse (delete-dups
  13447. (reverse (append
  13448. (org-remove-uninherited-tags
  13449. org-file-tags)
  13450. tags)))))))))
  13451. (defun org-add-prop-inherited (s)
  13452. (add-text-properties 0 (length s) '(inherited t) s)
  13453. s)
  13454. (defun org-toggle-tag (tag &optional onoff)
  13455. "Toggle the tag TAG for the current line.
  13456. If ONOFF is `on' or `off', don't toggle but set to this state."
  13457. (let (res current)
  13458. (save-excursion
  13459. (org-back-to-heading t)
  13460. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t]*$")
  13461. (point-at-eol) t)
  13462. (progn
  13463. (setq current (match-string 1))
  13464. (replace-match ""))
  13465. (setq current ""))
  13466. (setq current (nreverse (org-split-string current ":")))
  13467. (cond
  13468. ((eq onoff 'on)
  13469. (setq res t)
  13470. (or (member tag current) (push tag current)))
  13471. ((eq onoff 'off)
  13472. (or (not (member tag current)) (setq current (delete tag current))))
  13473. (t (if (member tag current)
  13474. (setq current (delete tag current))
  13475. (setq res t)
  13476. (push tag current))))
  13477. (end-of-line 1)
  13478. (if current
  13479. (progn
  13480. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  13481. (org-set-tags nil t))
  13482. (delete-horizontal-space))
  13483. (run-hooks 'org-after-tags-change-hook))
  13484. res))
  13485. (defun org-align-tags-here (to-col)
  13486. ;; Assumes that this is a headline
  13487. "Align tags on the current headline to TO-COL."
  13488. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  13489. (beginning-of-line 1)
  13490. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13491. (< pos (match-beginning 2)))
  13492. (progn
  13493. (setq tags-l (- (match-end 2) (match-beginning 2)))
  13494. (goto-char (match-beginning 1))
  13495. (insert " ")
  13496. (delete-region (point) (1+ (match-beginning 2)))
  13497. (setq ncol (max (current-column)
  13498. (1+ col)
  13499. (if (> to-col 0)
  13500. to-col
  13501. (- (abs to-col) tags-l))))
  13502. (setq p (point))
  13503. (insert (make-string (- ncol (current-column)) ?\ ))
  13504. (setq ncol (current-column))
  13505. (when indent-tabs-mode (tabify p (point-at-eol)))
  13506. (org-move-to-column (min ncol col)))
  13507. (goto-char pos))))
  13508. (defun org-set-tags-command (&optional arg just-align)
  13509. "Call the set-tags command for the current entry."
  13510. (interactive "P")
  13511. (if (or (org-at-heading-p) (and arg (org-before-first-heading-p)))
  13512. (org-set-tags arg just-align)
  13513. (save-excursion
  13514. (unless (and (org-region-active-p)
  13515. org-loop-over-headlines-in-active-region)
  13516. (org-back-to-heading t))
  13517. (org-set-tags arg just-align))))
  13518. (defun org-set-tags-to (data)
  13519. "Set the tags of the current entry to DATA, replacing the current tags.
  13520. DATA may be a tags string like :aa:bb:cc:, or a list of tags.
  13521. If DATA is nil or the empty string, any tags will be removed."
  13522. (interactive "sTags: ")
  13523. (setq data
  13524. (cond
  13525. ((eq data nil) "")
  13526. ((equal data "") "")
  13527. ((stringp data)
  13528. (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
  13529. ":"))
  13530. ((listp data)
  13531. (concat ":" (mapconcat 'identity data ":") ":"))))
  13532. (when data
  13533. (save-excursion
  13534. (org-back-to-heading t)
  13535. (when (looking-at org-complex-heading-regexp)
  13536. (if (match-end 5)
  13537. (progn
  13538. (goto-char (match-beginning 5))
  13539. (insert data)
  13540. (delete-region (point) (point-at-eol))
  13541. (org-set-tags nil 'align))
  13542. (goto-char (point-at-eol))
  13543. (insert " " data)
  13544. (org-set-tags nil 'align)))
  13545. (beginning-of-line 1)
  13546. (when (looking-at ".*?\\([ \t]+\\)$")
  13547. (delete-region (match-beginning 1) (match-end 1))))))
  13548. (defun org-align-all-tags ()
  13549. "Align the tags i all headings."
  13550. (interactive)
  13551. (save-excursion
  13552. (or (ignore-errors (org-back-to-heading t))
  13553. (outline-next-heading))
  13554. (if (org-at-heading-p)
  13555. (org-set-tags t)
  13556. (message "No headings"))))
  13557. (defvar org-indent-indentation-per-level)
  13558. (defun org-set-tags (&optional arg just-align)
  13559. "Set the tags for the current headline.
  13560. With prefix ARG, realign all tags in headings in the current buffer.
  13561. When JUST-ALIGN is non-nil, only align tags."
  13562. (interactive "P")
  13563. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  13564. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  13565. 'region-start-level
  13566. 'region))
  13567. org-loop-over-headlines-in-active-region)
  13568. (org-map-entries
  13569. ;; We don't use ARG and JUST-ALIGN here because these args
  13570. ;; are not useful when looping over headlines.
  13571. #'org-set-tags
  13572. org-loop-over-headlines-in-active-region
  13573. cl
  13574. '(when (outline-invisible-p) (org-end-of-subtree nil t))))
  13575. (let ((org-setting-tags t))
  13576. (if arg
  13577. (save-excursion
  13578. (goto-char (point-min))
  13579. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  13580. (while (re-search-forward org-outline-regexp-bol nil t)
  13581. (org-set-tags nil t)
  13582. (end-of-line)))
  13583. (message "All tags realigned to column %d" org-tags-column))
  13584. (let* ((current (org-get-tags-string))
  13585. (col (current-column))
  13586. (tags
  13587. (if just-align current
  13588. ;; Get a new set of tags from the user.
  13589. (save-excursion
  13590. (let* ((table
  13591. (setq
  13592. org-last-tags-completion-table
  13593. (append
  13594. org-tag-persistent-alist
  13595. (or org-tag-alist (org-get-buffer-tags))
  13596. (and
  13597. org-complete-tags-always-offer-all-agenda-tags
  13598. (org-global-tags-completion-table
  13599. (org-agenda-files))))))
  13600. (current-tags (org-split-string current ":"))
  13601. (inherited-tags
  13602. (nreverse (nthcdr (length current-tags)
  13603. (nreverse (org-get-tags-at))))))
  13604. (replace-regexp-in-string
  13605. "\\([-+&]+\\|,\\)"
  13606. ":"
  13607. (if (or (eq t org-use-fast-tag-selection)
  13608. (and org-use-fast-tag-selection
  13609. (delq nil (mapcar #'cdr table))))
  13610. (org-fast-tag-selection
  13611. current-tags inherited-tags table
  13612. (and org-fast-tag-selection-include-todo
  13613. org-todo-key-alist))
  13614. (let ((org-add-colon-after-tag-completion
  13615. (< 1 (length table))))
  13616. (org-trim
  13617. (completing-read
  13618. "Tags: "
  13619. #'org-tags-completion-function
  13620. nil nil current 'org-tags-history))))))))))
  13621. (when org-tags-sort-function
  13622. (setq tags
  13623. (mapconcat
  13624. #'identity
  13625. (sort (org-split-string tags (org-re "[^[:alnum:]_@#%]+"))
  13626. org-tags-sort-function)
  13627. ":")))
  13628. (if (not (org-string-nw-p tags)) (setq tags "")
  13629. (unless (string-match ":\\'" tags) (setq tags (concat tags ":")))
  13630. (unless (string-match "\\`:" tags) (setq tags (concat ":" tags))))
  13631. ;; Insert new tags at the correct column
  13632. (beginning-of-line)
  13633. (let ((level (if (looking-at org-outline-regexp)
  13634. (- (match-end 0) (point) 1)
  13635. 1)))
  13636. (cond
  13637. ((and (equal current "") (equal tags "")))
  13638. ((re-search-forward
  13639. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  13640. (line-end-position)
  13641. t)
  13642. (if (equal tags "") (replace-match "" t t)
  13643. (goto-char (match-beginning 0))
  13644. (let* ((c0 (current-column))
  13645. ;; Compute offset for the case of org-indent-mode
  13646. ;; active.
  13647. (di (if (org-bound-and-true-p org-indent-mode)
  13648. (* (1- org-indent-indentation-per-level)
  13649. (1- level))
  13650. 0))
  13651. (p0 (if (eq (char-before) ?*) (1+ (point)) (point)))
  13652. (tc (+ org-tags-column
  13653. (if (> org-tags-column 0) (- di) di)))
  13654. (c1 (max (1+ c0)
  13655. (if (> tc 0) tc
  13656. (- (- tc) (string-width tags)))))
  13657. (rpl (concat (make-string (max 0 (- c1 c0)) ?\s) tags)))
  13658. (replace-match rpl t t)
  13659. (when (and (not (featurep 'xemacs)) indent-tabs-mode)
  13660. (tabify p0 (point))))))
  13661. (t (error "Tags alignment failed"))))
  13662. (org-move-to-column col))
  13663. (unless just-align (run-hooks 'org-after-tags-change-hook))))))
  13664. (defun org-change-tag-in-region (beg end tag off)
  13665. "Add or remove TAG for each entry in the region.
  13666. This works in the agenda, and also in an org-mode buffer."
  13667. (interactive
  13668. (list (region-beginning) (region-end)
  13669. (let ((org-last-tags-completion-table
  13670. (if (derived-mode-p 'org-mode)
  13671. (org-uniquify
  13672. (delq nil (append (org-get-buffer-tags)
  13673. (org-global-tags-completion-table))))
  13674. (org-global-tags-completion-table))))
  13675. (completing-read
  13676. "Tag: " 'org-tags-completion-function nil nil nil
  13677. 'org-tags-history))
  13678. (progn
  13679. (message "[s]et or [r]emove? ")
  13680. (equal (read-char-exclusive) ?r))))
  13681. (when (fboundp 'deactivate-mark) (deactivate-mark))
  13682. (let ((agendap (equal major-mode 'org-agenda-mode))
  13683. l1 l2 m buf pos newhead (cnt 0))
  13684. (goto-char end)
  13685. (setq l2 (1- (org-current-line)))
  13686. (goto-char beg)
  13687. (setq l1 (org-current-line))
  13688. (cl-loop for l from l1 to l2 do
  13689. (org-goto-line l)
  13690. (setq m (get-text-property (point) 'org-hd-marker))
  13691. (when (or (and (derived-mode-p 'org-mode) (org-at-heading-p))
  13692. (and agendap m))
  13693. (setq buf (if agendap (marker-buffer m) (current-buffer))
  13694. pos (if agendap m (point)))
  13695. (with-current-buffer buf
  13696. (save-excursion
  13697. (save-restriction
  13698. (goto-char pos)
  13699. (setq cnt (1+ cnt))
  13700. (org-toggle-tag tag (if off 'off 'on))
  13701. (setq newhead (org-get-heading)))))
  13702. (and agendap (org-agenda-change-all-lines newhead m))))
  13703. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  13704. (defun org-tags-completion-function (string _predicate &optional flag)
  13705. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  13706. (confirm (lambda (x) (stringp (car x)))))
  13707. (if (string-match "^\\(.*[-+:&,|]\\)\\([^-+:&,|]*\\)$" string)
  13708. (setq s1 (match-string 1 string)
  13709. s2 (match-string 2 string))
  13710. (setq s1 "" s2 string))
  13711. (cond
  13712. ((eq flag nil)
  13713. ;; try completion
  13714. (setq rtn (try-completion s2 ctable confirm))
  13715. (when (stringp rtn)
  13716. (setq rtn
  13717. (concat s1 s2 (substring rtn (length s2))
  13718. (if (and org-add-colon-after-tag-completion
  13719. (assoc rtn ctable))
  13720. ":" ""))))
  13721. rtn)
  13722. ((eq flag t)
  13723. ;; all-completions
  13724. (all-completions s2 ctable confirm))
  13725. ((eq flag 'lambda)
  13726. ;; exact match?
  13727. (assoc s2 ctable)))))
  13728. (defun org-fast-tag-insert (kwd tags face &optional end)
  13729. "Insert KDW, and the TAGS, the latter with face FACE.
  13730. Also insert END."
  13731. (insert (format "%-12s" (concat kwd ":"))
  13732. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  13733. (or end "")))
  13734. (defun org-fast-tag-show-exit (flag)
  13735. (save-excursion
  13736. (org-goto-line 3)
  13737. (when (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  13738. (replace-match ""))
  13739. (when flag
  13740. (end-of-line 1)
  13741. (org-move-to-column (- (window-width) 19) t)
  13742. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  13743. (defun org-set-current-tags-overlay (current prefix)
  13744. "Add an overlay to CURRENT tag with PREFIX."
  13745. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  13746. (if (featurep 'xemacs)
  13747. (org-overlay-display org-tags-overlay (concat prefix s)
  13748. 'secondary-selection)
  13749. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  13750. (org-overlay-display org-tags-overlay (concat prefix s)))))
  13751. (defvar org-last-tag-selection-key nil)
  13752. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  13753. "Fast tag selection with single keys.
  13754. CURRENT is the current list of tags in the headline, INHERITED is the
  13755. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  13756. possibly with grouping information. TODO-TABLE is a similar table with
  13757. TODO keywords, should these have keys assigned to them.
  13758. If the keys are nil, a-z are automatically assigned.
  13759. Returns the new tags string, or nil to not change the current settings."
  13760. (let* ((fulltable (append table todo-table))
  13761. (maxlen (apply 'max (mapcar
  13762. (lambda (x)
  13763. (if (stringp (car x)) (string-width (car x)) 0))
  13764. fulltable)))
  13765. (buf (current-buffer))
  13766. (expert (eq org-fast-tag-selection-single-key 'expert))
  13767. (buffer-tags nil)
  13768. (fwidth (+ maxlen 3 1 3))
  13769. (ncol (/ (- (window-width) 4) fwidth))
  13770. (i-face 'org-done)
  13771. (c-face 'org-todo)
  13772. tg cnt e c char c1 c2 ntable tbl rtn
  13773. ov-start ov-end ov-prefix
  13774. (exit-after-next org-fast-tag-selection-single-key)
  13775. (done-keywords org-done-keywords)
  13776. groups ingroup intaggroup)
  13777. (save-excursion
  13778. (beginning-of-line 1)
  13779. (if (looking-at
  13780. (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13781. (setq ov-start (match-beginning 1)
  13782. ov-end (match-end 1)
  13783. ov-prefix "")
  13784. (setq ov-start (1- (point-at-eol))
  13785. ov-end (1+ ov-start))
  13786. (skip-chars-forward "^\n\r")
  13787. (setq ov-prefix
  13788. (concat
  13789. (buffer-substring (1- (point)) (point))
  13790. (if (> (current-column) org-tags-column)
  13791. " "
  13792. (make-string (- org-tags-column (current-column)) ?\ ))))))
  13793. (move-overlay org-tags-overlay ov-start ov-end)
  13794. (save-window-excursion
  13795. (if expert
  13796. (set-buffer (get-buffer-create " *Org tags*"))
  13797. (delete-other-windows)
  13798. (set-window-buffer (split-window-vertically) (get-buffer-create " *Org tags*"))
  13799. (org-switch-to-buffer-other-window " *Org tags*"))
  13800. (erase-buffer)
  13801. (setq-local org-done-keywords done-keywords)
  13802. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  13803. (org-fast-tag-insert "Current" current c-face "\n\n")
  13804. (org-fast-tag-show-exit exit-after-next)
  13805. (org-set-current-tags-overlay current ov-prefix)
  13806. (setq tbl fulltable char ?a cnt 0)
  13807. (while (setq e (pop tbl))
  13808. (cond
  13809. ((eq (car e) :startgroup)
  13810. (push '() groups) (setq ingroup t)
  13811. (unless (zerop cnt)
  13812. (setq cnt 0)
  13813. (insert "\n"))
  13814. (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
  13815. ((eq (car e) :endgroup)
  13816. (setq ingroup nil cnt 0)
  13817. (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
  13818. ((eq (car e) :startgrouptag)
  13819. (setq intaggroup t)
  13820. (unless (zerop cnt)
  13821. (setq cnt 0)
  13822. (insert "\n"))
  13823. (insert "[ "))
  13824. ((eq (car e) :endgrouptag)
  13825. (setq intaggroup nil cnt 0)
  13826. (insert "]\n"))
  13827. ((equal e '(:newline))
  13828. (unless (zerop cnt)
  13829. (setq cnt 0)
  13830. (insert "\n")
  13831. (setq e (car tbl))
  13832. (while (equal (car tbl) '(:newline))
  13833. (insert "\n")
  13834. (setq tbl (cdr tbl)))))
  13835. ((equal e '(:grouptags)) (insert " : "))
  13836. (t
  13837. (setq tg (copy-sequence (car e)) c2 nil)
  13838. (if (cdr e)
  13839. (setq c (cdr e))
  13840. ;; automatically assign a character.
  13841. (setq c1 (string-to-char
  13842. (downcase (substring
  13843. tg (if (= (string-to-char tg) ?@) 1 0)))))
  13844. (if (or (rassoc c1 ntable) (rassoc c1 table))
  13845. (while (or (rassoc char ntable) (rassoc char table))
  13846. (setq char (1+ char)))
  13847. (setq c2 c1))
  13848. (setq c (or c2 char)))
  13849. (when ingroup (push tg (car groups)))
  13850. (setq tg (org-add-props tg nil 'face
  13851. (cond
  13852. ((not (assoc tg table))
  13853. (org-get-todo-face tg))
  13854. ((member tg current) c-face)
  13855. ((member tg inherited) i-face))))
  13856. (when (equal (caar tbl) :grouptags)
  13857. (org-add-props tg nil 'face 'org-tag-group))
  13858. (when (and (zerop cnt) (not ingroup) (not intaggroup)) (insert " "))
  13859. (insert "[" c "] " tg (make-string
  13860. (- fwidth 4 (length tg)) ?\ ))
  13861. (push (cons tg c) ntable)
  13862. (when (= (cl-incf cnt) ncol)
  13863. (insert "\n")
  13864. (when (or ingroup intaggroup) (insert " "))
  13865. (setq cnt 0)))))
  13866. (setq ntable (nreverse ntable))
  13867. (insert "\n")
  13868. (goto-char (point-min))
  13869. (unless expert (org-fit-window-to-buffer))
  13870. (setq rtn
  13871. (catch 'exit
  13872. (while t
  13873. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
  13874. (if (not groups) "no " "")
  13875. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  13876. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  13877. (setq org-last-tag-selection-key c)
  13878. (cond
  13879. ((= c ?\r) (throw 'exit t))
  13880. ((= c ?!)
  13881. (setq groups (not groups))
  13882. (goto-char (point-min))
  13883. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  13884. ((= c ?\C-c)
  13885. (if (not expert)
  13886. (org-fast-tag-show-exit
  13887. (setq exit-after-next (not exit-after-next)))
  13888. (setq expert nil)
  13889. (delete-other-windows)
  13890. (set-window-buffer (split-window-vertically) " *Org tags*")
  13891. (org-switch-to-buffer-other-window " *Org tags*")
  13892. (org-fit-window-to-buffer)))
  13893. ((or (= c ?\C-g)
  13894. (and (= c ?q) (not (rassoc c ntable))))
  13895. (org-detach-overlay org-tags-overlay)
  13896. (setq quit-flag t))
  13897. ((= c ?\ )
  13898. (setq current nil)
  13899. (when exit-after-next (setq exit-after-next 'now)))
  13900. ((= c ?\t)
  13901. (condition-case nil
  13902. (setq tg (completing-read
  13903. "Tag: "
  13904. (or buffer-tags
  13905. (with-current-buffer buf
  13906. (setq buffer-tags
  13907. (org-get-buffer-tags))))))
  13908. (quit (setq tg "")))
  13909. (when (string-match "\\S-" tg)
  13910. (add-to-list 'buffer-tags (list tg))
  13911. (if (member tg current)
  13912. (setq current (delete tg current))
  13913. (push tg current)))
  13914. (when exit-after-next (setq exit-after-next 'now)))
  13915. ((setq e (rassoc c todo-table) tg (car e))
  13916. (with-current-buffer buf
  13917. (save-excursion (org-todo tg)))
  13918. (when exit-after-next (setq exit-after-next 'now)))
  13919. ((setq e (rassoc c ntable) tg (car e))
  13920. (if (member tg current)
  13921. (setq current (delete tg current))
  13922. (cl-loop for g in groups do
  13923. (when (member tg g)
  13924. (dolist (x g) (setq current (delete x current)))))
  13925. (push tg current))
  13926. (when exit-after-next (setq exit-after-next 'now))))
  13927. ;; Create a sorted list
  13928. (setq current
  13929. (sort current
  13930. (lambda (a b)
  13931. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  13932. (when (eq exit-after-next 'now) (throw 'exit t))
  13933. (goto-char (point-min))
  13934. (beginning-of-line 2)
  13935. (delete-region (point) (point-at-eol))
  13936. (org-fast-tag-insert "Current" current c-face)
  13937. (org-set-current-tags-overlay current ov-prefix)
  13938. (while (re-search-forward
  13939. (org-re "\\[.\\] \\([[:alnum:]_@#%]+\\)") nil t)
  13940. (setq tg (match-string 1))
  13941. (add-text-properties
  13942. (match-beginning 1) (match-end 1)
  13943. (list 'face
  13944. (cond
  13945. ((member tg current) c-face)
  13946. ((member tg inherited) i-face)
  13947. (t (get-text-property (match-beginning 1) 'face))))))
  13948. (goto-char (point-min)))))
  13949. (org-detach-overlay org-tags-overlay)
  13950. (if rtn
  13951. (mapconcat 'identity current ":")
  13952. nil))))
  13953. (defun org-get-tags-string ()
  13954. "Get the TAGS string in the current headline."
  13955. (unless (org-at-heading-p t)
  13956. (user-error "Not on a heading"))
  13957. (save-excursion
  13958. (beginning-of-line 1)
  13959. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13960. (org-match-string-no-properties 1)
  13961. "")))
  13962. (defun org-get-tags ()
  13963. "Get the list of tags specified in the current headline."
  13964. (org-split-string (org-get-tags-string) ":"))
  13965. (defun org-get-buffer-tags ()
  13966. "Get a table of all tags used in the buffer, for completion."
  13967. (org-with-wide-buffer
  13968. (goto-char (point-min))
  13969. (let ((tag-re (concat org-outline-regexp-bol
  13970. "\\(?:.*?[ \t]\\)?"
  13971. (org-re ":\\([[:alnum:]_@#%:]+\\):[ \t]*$")))
  13972. tags)
  13973. (while (re-search-forward tag-re nil t)
  13974. (dolist (tag (org-split-string (org-match-string-no-properties 1) ":"))
  13975. (push tag tags)))
  13976. (mapcar #'list (append org-file-tags (org-uniquify tags))))))
  13977. ;;;; The mapping API
  13978. (defvar org-agenda-skip-comment-trees)
  13979. (defvar org-agenda-skip-function)
  13980. (defun org-map-entries (func &optional match scope &rest skip)
  13981. "Call FUNC at each headline selected by MATCH in SCOPE.
  13982. FUNC is a function or a lisp form. The function will be called without
  13983. arguments, with the cursor positioned at the beginning of the headline.
  13984. The return values of all calls to the function will be collected and
  13985. returned as a list.
  13986. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  13987. does not need to preserve point. After evaluation, the cursor will be
  13988. moved to the end of the line (presumably of the headline of the
  13989. processed entry) and search continues from there. Under some
  13990. circumstances, this may not produce the wanted results. For example,
  13991. if you have removed (e.g. archived) the current (sub)tree it could
  13992. mean that the next entry will be skipped entirely. In such cases, you
  13993. can specify the position from where search should continue by making
  13994. FUNC set the variable `org-map-continue-from' to the desired buffer
  13995. position.
  13996. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  13997. Only headlines that are matched by this query will be considered during
  13998. the iteration. When MATCH is nil or t, all headlines will be
  13999. visited by the iteration.
  14000. SCOPE determines the scope of this command. It can be any of:
  14001. nil The current buffer, respecting the restriction if any
  14002. tree The subtree started with the entry at point
  14003. region The entries within the active region, if any
  14004. region-start-level
  14005. The entries within the active region, but only those at
  14006. the same level than the first one.
  14007. file The current buffer, without restriction
  14008. file-with-archives
  14009. The current buffer, and any archives associated with it
  14010. agenda All agenda files
  14011. agenda-with-archives
  14012. All agenda files with any archive files associated with them
  14013. \(file1 file2 ...)
  14014. If this is a list, all files in the list will be scanned
  14015. The remaining args are treated as settings for the skipping facilities of
  14016. the scanner. The following items can be given here:
  14017. archive skip trees with the archive tag
  14018. comment skip trees with the COMMENT keyword
  14019. function or Emacs Lisp form:
  14020. will be used as value for `org-agenda-skip-function', so
  14021. whenever the function returns a position, FUNC will not be
  14022. called for that entry and search will continue from the
  14023. position returned
  14024. If your function needs to retrieve the tags including inherited tags
  14025. at the *current* entry, you can use the value of the variable
  14026. `org-scanner-tags' which will be much faster than getting the value
  14027. with `org-get-tags-at'. If your function gets properties with
  14028. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  14029. to t around the call to `org-entry-properties' to get the same speedup.
  14030. Note that if your function moves around to retrieve tags and properties at
  14031. a *different* entry, you cannot use these techniques."
  14032. (unless (and (or (eq scope 'region) (eq scope 'region-start-level))
  14033. (not (org-region-active-p)))
  14034. (let* ((org-agenda-archives-mode nil) ; just to make sure
  14035. (org-agenda-skip-archived-trees (memq 'archive skip))
  14036. (org-agenda-skip-comment-trees (memq 'comment skip))
  14037. (org-agenda-skip-function
  14038. (car (org-delete-all '(comment archive) skip)))
  14039. (org-tags-match-list-sublevels t)
  14040. (start-level (eq scope 'region-start-level))
  14041. matcher res
  14042. org-todo-keywords-for-agenda
  14043. org-done-keywords-for-agenda
  14044. org-todo-keyword-alist-for-agenda
  14045. org-tag-alist-for-agenda
  14046. org--matcher-tags-todo-only)
  14047. (cond
  14048. ((eq match t) (setq matcher t))
  14049. ((eq match nil) (setq matcher t))
  14050. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  14051. (save-excursion
  14052. (save-restriction
  14053. (cond ((eq scope 'tree)
  14054. (org-back-to-heading t)
  14055. (org-narrow-to-subtree)
  14056. (setq scope nil))
  14057. ((and (or (eq scope 'region) (eq scope 'region-start-level))
  14058. (org-region-active-p))
  14059. ;; If needed, set start-level to a string like "2"
  14060. (when start-level
  14061. (save-excursion
  14062. (goto-char (region-beginning))
  14063. (unless (org-at-heading-p) (outline-next-heading))
  14064. (setq start-level (org-current-level))))
  14065. (narrow-to-region (region-beginning)
  14066. (save-excursion
  14067. (goto-char (region-end))
  14068. (unless (and (bolp) (org-at-heading-p))
  14069. (outline-next-heading))
  14070. (point)))
  14071. (setq scope nil)))
  14072. (if (not scope)
  14073. (progn
  14074. (org-agenda-prepare-buffers
  14075. (and buffer-file-name (list buffer-file-name)))
  14076. (setq res
  14077. (org-scan-tags
  14078. func matcher org--matcher-tags-todo-only start-level)))
  14079. ;; Get the right scope
  14080. (cond
  14081. ((and scope (listp scope) (symbolp (car scope)))
  14082. (setq scope (eval scope)))
  14083. ((eq scope 'agenda)
  14084. (setq scope (org-agenda-files t)))
  14085. ((eq scope 'agenda-with-archives)
  14086. (setq scope (org-agenda-files t))
  14087. (setq scope (org-add-archive-files scope)))
  14088. ((eq scope 'file)
  14089. (setq scope (and buffer-file-name (list buffer-file-name))))
  14090. ((eq scope 'file-with-archives)
  14091. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  14092. (org-agenda-prepare-buffers scope)
  14093. (dolist (file scope)
  14094. (with-current-buffer (org-find-base-buffer-visiting file)
  14095. (save-excursion
  14096. (save-restriction
  14097. (widen)
  14098. (goto-char (point-min))
  14099. (setq res
  14100. (append
  14101. res
  14102. (org-scan-tags
  14103. func matcher org--matcher-tags-todo-only))))))))))
  14104. res)))
  14105. ;;; Properties API
  14106. (defconst org-special-properties
  14107. '("ALLTAGS" "BLOCKED" "CLOCKSUM" "CLOCKSUM_T" "CLOSED" "DEADLINE" "FILE"
  14108. "ITEM" "PRIORITY" "SCHEDULED" "TAGS" "TIMESTAMP" "TIMESTAMP_IA" "TODO")
  14109. "The special properties valid in Org mode.
  14110. These are properties that are not defined in the property drawer,
  14111. but in some other way.")
  14112. (defconst org-default-properties
  14113. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  14114. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  14115. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  14116. "EXPORT_OPTIONS" "EXPORT_TEXT" "EXPORT_FILE_NAME"
  14117. "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE" "UNNUMBERED"
  14118. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER" "REPEAT_TO_STATE"
  14119. "CLOCK_MODELINE_TOTAL" "STYLE" "HTML_CONTAINER_CLASS")
  14120. "Some properties that are used by Org mode for various purposes.
  14121. Being in this list makes sure that they are offered for completion.")
  14122. (defun org--valid-property-p (property)
  14123. "Non nil when string PROPERTY is a valid property name."
  14124. (not
  14125. (or (equal property "")
  14126. (org-string-match-p "\\s-" property))))
  14127. (defun org--update-property-plist (key val props)
  14128. "Associate KEY to VAL in alist PROPS.
  14129. Modifications are made by side-effect. Return new alist."
  14130. (let* ((appending (string= (substring key -1) "+"))
  14131. (key (if appending (substring key 0 -1) key))
  14132. (old (assoc-string key props t)))
  14133. (if (not old) (cons (cons key val) props)
  14134. (setcdr old (if appending (concat (cdr old) " " val) val))
  14135. props)))
  14136. (defun org-get-property-block (&optional beg force)
  14137. "Return the (beg . end) range of the body of the property drawer.
  14138. BEG is the beginning of the current subtree, or of the part
  14139. before the first headline. If it is not given, it will be found.
  14140. If the drawer does not exist, create it if FORCE is non-nil, or
  14141. return nil."
  14142. (org-with-wide-buffer
  14143. (when beg (goto-char beg))
  14144. (unless (org-before-first-heading-p)
  14145. (let ((beg (cond (beg)
  14146. ((or (not (featurep 'org-inlinetask))
  14147. (org-inlinetask-in-task-p))
  14148. (org-back-to-heading t))
  14149. (t (org-with-limited-levels (org-back-to-heading t))))))
  14150. (forward-line)
  14151. (when (org-looking-at-p org-planning-line-re) (forward-line))
  14152. (cond ((looking-at org-property-drawer-re)
  14153. (forward-line)
  14154. (cons (point) (progn (goto-char (match-end 0))
  14155. (line-beginning-position))))
  14156. (force
  14157. (goto-char beg)
  14158. (org-insert-property-drawer)
  14159. (let ((pos (save-excursion (search-forward ":END:")
  14160. (line-beginning-position))))
  14161. (cons pos pos))))))))
  14162. (defun org-at-property-p ()
  14163. "Non-nil when point is inside a property drawer.
  14164. See `org-property-re' for match data, if applicable."
  14165. (save-excursion
  14166. (beginning-of-line)
  14167. (and (looking-at org-property-re)
  14168. (let ((property-drawer (save-match-data (org-get-property-block))))
  14169. (and property-drawer
  14170. (>= (point) (car property-drawer))
  14171. (< (point) (cdr property-drawer)))))))
  14172. (defun org-property-action ()
  14173. "Do an action on properties."
  14174. (interactive)
  14175. (unless (org-at-property-p) (user-error "Not at a property"))
  14176. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  14177. (let ((c (read-char-exclusive)))
  14178. (cl-case c
  14179. (?s (call-interactively #'org-set-property))
  14180. (?d (call-interactively #'org-delete-property))
  14181. (?D (call-interactively #'org-delete-property-globally))
  14182. (?c (call-interactively #'org-compute-property-at-point))
  14183. (otherwise (user-error "No such property action %c" c)))))
  14184. (defun org-inc-effort ()
  14185. "Increment the value of the effort property in the current entry."
  14186. (interactive)
  14187. (org-set-effort nil t))
  14188. (defvar org-clock-effort) ; Defined in org-clock.el.
  14189. (defvar org-clock-current-task) ; Defined in org-clock.el.
  14190. (defun org-set-effort (&optional value increment)
  14191. "Set the effort property of the current entry.
  14192. With numerical prefix arg, use the nth allowed value, 0 stands for the
  14193. 10th allowed value.
  14194. When INCREMENT is non-nil, set the property to the next allowed value."
  14195. (interactive "P")
  14196. (when (equal value 0) (setq value 10))
  14197. (let* ((completion-ignore-case t)
  14198. (prop org-effort-property)
  14199. (cur (org-entry-get nil prop))
  14200. (allowed (org-property-get-allowed-values nil prop 'table))
  14201. (existing (mapcar 'list (org-property-values prop)))
  14202. (heading (nth 4 (org-heading-components)))
  14203. rpl
  14204. (val (cond
  14205. ((stringp value) value)
  14206. ((and allowed (integerp value))
  14207. (or (car (nth (1- value) allowed))
  14208. (car (org-last allowed))))
  14209. ((and allowed increment)
  14210. (or (cl-caadr (member (list cur) allowed))
  14211. (user-error "Allowed effort values are not set")))
  14212. (allowed
  14213. (message "Select 1-9,0, [RET%s]: %s"
  14214. (if cur (concat "=" cur) "")
  14215. (mapconcat 'car allowed " "))
  14216. (setq rpl (read-char-exclusive))
  14217. (if (equal rpl ?\r)
  14218. cur
  14219. (setq rpl (- rpl ?0))
  14220. (when (equal rpl 0) (setq rpl 10))
  14221. (if (and (> rpl 0) (<= rpl (length allowed)))
  14222. (car (nth (1- rpl) allowed))
  14223. (org-completing-read "Effort: " allowed nil))))
  14224. (t
  14225. (let (org-completion-use-ido org-completion-use-iswitchb)
  14226. (org-completing-read
  14227. (concat "Effort " (if (and cur (string-match "\\S-" cur))
  14228. (concat "[" cur "]") "")
  14229. ": ")
  14230. existing nil nil "" nil cur))))))
  14231. (unless (equal (org-entry-get nil prop) val)
  14232. (org-entry-put nil prop val))
  14233. (org-refresh-property
  14234. '((effort . identity)
  14235. (effort-minutes . org-duration-string-to-minutes))
  14236. val)
  14237. (when (string= heading org-clock-current-task)
  14238. (setq org-clock-effort (get-text-property (point-at-bol) 'effort))
  14239. (org-clock-update-mode-line))
  14240. (message "%s is now %s" prop val)))
  14241. (defun org-entry-properties (&optional pom which)
  14242. "Get all properties of the current entry.
  14243. When POM is a buffer position, get all properties from the entry
  14244. there instead.
  14245. This includes the TODO keyword, the tags, time strings for
  14246. deadline, scheduled, and clocking, and any additional properties
  14247. defined in the entry.
  14248. If WHICH is nil or `all', get all properties. If WHICH is
  14249. `special' or `standard', only get that subclass. If WHICH is
  14250. a string, only get that property.
  14251. Return value is an alist. Keys are properties, as upcased
  14252. strings."
  14253. (org-with-point-at pom
  14254. (when (and (derived-mode-p 'org-mode)
  14255. (ignore-errors (org-back-to-heading t)))
  14256. (catch 'exit
  14257. (let* ((beg (point))
  14258. (specific (and (stringp which) (upcase which)))
  14259. (which (cond ((not specific) which)
  14260. ((member specific org-special-properties) 'special)
  14261. (t 'standard)))
  14262. props)
  14263. ;; Get the special properties, like TODO and TAGS.
  14264. (when (memq which '(nil all special))
  14265. (when (or (not specific) (string= specific "CLOCKSUM"))
  14266. (let ((clocksum (get-text-property (point) :org-clock-minutes)))
  14267. (when clocksum
  14268. (push (cons "CLOCKSUM"
  14269. (org-columns-number-to-string
  14270. (/ (float clocksum) 60.) 'add_times))
  14271. props)))
  14272. (when specific (throw 'exit props)))
  14273. (when (or (not specific) (string= specific "CLOCKSUM_T"))
  14274. (let ((clocksumt (get-text-property (point)
  14275. :org-clock-minutes-today)))
  14276. (when clocksumt
  14277. (push (cons "CLOCKSUM_T"
  14278. (org-columns-number-to-string
  14279. (/ (float clocksumt) 60.) 'add_times))
  14280. props)))
  14281. (when specific (throw 'exit props)))
  14282. (when (or (not specific) (string= specific "ITEM"))
  14283. (when (looking-at org-complex-heading-regexp)
  14284. (push (cons "ITEM"
  14285. (concat
  14286. (org-match-string-no-properties 1)
  14287. (let ((title (org-match-string-no-properties 4)))
  14288. (when (org-string-nw-p title)
  14289. (concat " " (org-remove-tabs title))))))
  14290. props))
  14291. (when specific (throw 'exit props)))
  14292. (when (or (not specific) (string= specific "TODO"))
  14293. (let ((case-fold-search nil))
  14294. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  14295. (push (cons "TODO" (org-match-string-no-properties 2)) props)))
  14296. (when specific (throw 'exit props)))
  14297. (when (or (not specific) (string= specific "PRIORITY"))
  14298. (push (cons "PRIORITY"
  14299. (if (looking-at org-priority-regexp)
  14300. (org-match-string-no-properties 2)
  14301. (char-to-string org-default-priority)))
  14302. props)
  14303. (when specific (throw 'exit props)))
  14304. (when (or (not specific) (string= specific "FILE"))
  14305. (push (cons "FILE" (buffer-file-name (buffer-base-buffer)))
  14306. props)
  14307. (when specific (throw 'exit props)))
  14308. (when (or (not specific) (string= specific "TAGS"))
  14309. (let ((value (org-string-nw-p (org-get-tags-string))))
  14310. (when value (push (cons "TAGS" value) props)))
  14311. (when specific (throw 'exit props)))
  14312. (when (or (not specific) (string= specific "ALLTAGS"))
  14313. (let ((value (org-get-tags-at)))
  14314. (when value
  14315. (push (cons "ALLTAGS"
  14316. (format ":%s:" (mapconcat #'identity value ":")))
  14317. props)))
  14318. (when specific (throw 'exit props)))
  14319. (when (or (not specific) (string= specific "BLOCKED"))
  14320. (push (cons "BLOCKED" (if (org-entry-blocked-p) "t" "")) props)
  14321. (when specific (throw 'exit props)))
  14322. (when (or (not specific)
  14323. (member specific '("CLOSED" "DEADLINE" "SCHEDULED")))
  14324. (forward-line)
  14325. (when (org-looking-at-p org-planning-line-re)
  14326. (end-of-line)
  14327. (let ((bol (line-beginning-position))
  14328. ;; Backward compatibility: time keywords used to
  14329. ;; be configurable (before 8.3). Make sure we
  14330. ;; get the correct keyword.
  14331. (key-assoc `(("CLOSED" . ,org-closed-string)
  14332. ("DEADLINE" . ,org-deadline-string)
  14333. ("SCHEDULED" . ,org-scheduled-string))))
  14334. (dolist (pair (if specific (list (assoc specific key-assoc))
  14335. key-assoc))
  14336. (save-excursion
  14337. (when (search-backward (cdr pair) bol t)
  14338. (goto-char (match-end 0))
  14339. (skip-chars-forward " \t")
  14340. (and (looking-at org-ts-regexp-both)
  14341. (push (cons (car pair)
  14342. (org-match-string-no-properties 0))
  14343. props)))))))
  14344. (when specific (throw 'exit props)))
  14345. (when (or (not specific)
  14346. (member specific '("TIMESTAMP" "TIMESTAMP_IA")))
  14347. (let ((find-ts
  14348. (lambda (end ts)
  14349. ;; Fix next time-stamp before END. TS is the
  14350. ;; list of time-stamps found so far.
  14351. (let ((ts ts)
  14352. (regexp (cond
  14353. ((string= specific "TIMESTAMP")
  14354. org-ts-regexp)
  14355. ((string= specific "TIMESTAMP_IA")
  14356. org-ts-regexp-inactive)
  14357. ((assoc "TIMESTAMP_IA" ts)
  14358. org-ts-regexp)
  14359. ((assoc "TIMESTAMP" ts)
  14360. org-ts-regexp-inactive)
  14361. (t org-ts-regexp-both))))
  14362. (catch 'next
  14363. (while (re-search-forward regexp end t)
  14364. (backward-char)
  14365. (let ((object (org-element-context)))
  14366. ;; Accept to match timestamps in node
  14367. ;; properties, too.
  14368. (when (memq (org-element-type object)
  14369. '(node-property timestamp))
  14370. (let ((type
  14371. (org-element-property :type object)))
  14372. (cond
  14373. ((and (memq type '(active active-range))
  14374. (not (equal specific "TIMESTAMP_IA")))
  14375. (unless (assoc "TIMESTAMP" ts)
  14376. (push (cons "TIMESTAMP"
  14377. (org-element-property
  14378. :raw-value object))
  14379. ts)
  14380. (when specific (throw 'exit ts))))
  14381. ((and (memq type '(inactive inactive-range))
  14382. (not (string= specific "TIMESTAMP")))
  14383. (unless (assoc "TIMESTAMP_IA" ts)
  14384. (push (cons "TIMESTAMP_IA"
  14385. (org-element-property
  14386. :raw-value object))
  14387. ts)
  14388. (when specific (throw 'exit ts))))))
  14389. ;; Both timestamp types are found,
  14390. ;; move to next part.
  14391. (when (= (length ts) 2) (throw 'next ts)))))
  14392. ts)))))
  14393. (goto-char beg)
  14394. ;; First look for timestamps within headline.
  14395. (let ((ts (funcall find-ts (line-end-position) nil)))
  14396. (if (= (length ts) 2) (setq props (nconc ts props))
  14397. (forward-line)
  14398. ;; Then find timestamps in the section, skipping
  14399. ;; planning line.
  14400. (when (org-looking-at-p org-planning-line-re)
  14401. (forward-line))
  14402. (let ((end (save-excursion (outline-next-heading))))
  14403. (setq props (nconc (funcall find-ts end ts) props))))))))
  14404. ;; Get the standard properties, like :PROP:.
  14405. (when (memq which '(nil all standard))
  14406. ;; If we are looking after a specific property, delegate
  14407. ;; to `org-entry-get', which is faster. However, make an
  14408. ;; exception for "CATEGORY", since it can be also set
  14409. ;; through keywords (i.e. #+CATEGORY).
  14410. (if (and specific (not (equal specific "CATEGORY")))
  14411. (let ((value (org-entry-get beg specific nil t)))
  14412. (throw 'exit (and value (list (cons specific value)))))
  14413. (let ((range (org-get-property-block beg)))
  14414. (when range
  14415. (let ((end (cdr range)) seen-base)
  14416. (goto-char (car range))
  14417. ;; Unlike to `org--update-property-plist', we
  14418. ;; handle the case where base values is found
  14419. ;; after its extension. We also forbid standard
  14420. ;; properties to be named as special properties.
  14421. (while (re-search-forward org-property-re end t)
  14422. (let* ((key (upcase (org-match-string-no-properties 2)))
  14423. (extendp (org-string-match-p "\\+\\'" key))
  14424. (key-base (if extendp (substring key 0 -1) key))
  14425. (value (org-match-string-no-properties 3)))
  14426. (cond
  14427. ((member-ignore-case key-base org-special-properties))
  14428. (extendp
  14429. (setq props
  14430. (org--update-property-plist key value props)))
  14431. ((member key seen-base))
  14432. (t (push key seen-base)
  14433. (let ((p (assoc-string key props t)))
  14434. (if p (setcdr p (concat value " " (cdr p)))
  14435. (push (cons key value) props))))))))))))
  14436. (unless (assoc "CATEGORY" props)
  14437. (push (cons "CATEGORY" (org-get-category beg)) props)
  14438. (when (string= specific "CATEGORY") (throw 'exit props)))
  14439. ;; Return value.
  14440. props)))))
  14441. (defun org-property--local-values (property literal-nil)
  14442. "Return value for PROPERTY in current entry.
  14443. Value is a list whose care is the base value for PROPERTY and cdr
  14444. a list of accumulated values. Return nil if neither is found in
  14445. the entry. Also return nil when PROPERTY is set to \"nil\",
  14446. unless LITERAL-NIL is non-nil."
  14447. (let ((range (org-get-property-block)))
  14448. (when range
  14449. (goto-char (car range))
  14450. (let* ((case-fold-search t)
  14451. (end (cdr range))
  14452. (value
  14453. ;; Base value.
  14454. (save-excursion
  14455. (let ((v (and (re-search-forward
  14456. (org-re-property property nil t) end t)
  14457. (org-match-string-no-properties 3))))
  14458. (list (if literal-nil v (org-not-nil v)))))))
  14459. ;; Find additional values.
  14460. (let* ((property+ (org-re-property (concat property "+") nil t)))
  14461. (while (re-search-forward property+ end t)
  14462. (push (org-match-string-no-properties 3) value)))
  14463. ;; Return final values.
  14464. (and (not (equal value '(nil))) (nreverse value))))))
  14465. (defun org-entry-get (pom property &optional inherit literal-nil)
  14466. "Get value of PROPERTY for entry or content at point-or-marker POM.
  14467. If INHERIT is non-nil and the entry does not have the property,
  14468. then also check higher levels of the hierarchy. If INHERIT is
  14469. the symbol `selective', use inheritance only if the setting in
  14470. `org-use-property-inheritance' selects PROPERTY for inheritance.
  14471. If the property is present but empty, the return value is the
  14472. empty string. If the property is not present at all, nil is
  14473. returned. In any other case, return the value as a string.
  14474. Search is case-insensitive.
  14475. If LITERAL-NIL is set, return the string value \"nil\" as
  14476. a string, do not interpret it as the list atom nil. This is used
  14477. for inheritance when a \"nil\" value can supersede a non-nil
  14478. value higher up the hierarchy."
  14479. (org-with-point-at pom
  14480. (cond
  14481. ((member-ignore-case property (cons "CATEGORY" org-special-properties))
  14482. ;; We need a special property. Use `org-entry-properties' to
  14483. ;; retrieve it, but specify the wanted property.
  14484. (cdr (assoc-string property (org-entry-properties nil property))))
  14485. ((and inherit
  14486. (or (not (eq inherit 'selective)) (org-property-inherit-p property)))
  14487. (org-entry-get-with-inheritance property literal-nil))
  14488. (t
  14489. (let* ((local (org-property--local-values property literal-nil))
  14490. (value (and local (mapconcat #'identity (delq nil local) " "))))
  14491. (if literal-nil value (org-not-nil value)))))))
  14492. (defun org-property-or-variable-value (var &optional inherit)
  14493. "Check if there is a property fixing the value of VAR.
  14494. If yes, return this value. If not, return the current value of the variable."
  14495. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  14496. (if (and prop (stringp prop) (string-match "\\S-" prop))
  14497. (read prop)
  14498. (symbol-value var))))
  14499. (defun org-entry-delete (pom property)
  14500. "Delete PROPERTY from entry at point-or-marker POM.
  14501. Accumulated properties, i.e. PROPERTY+, are also removed. Return
  14502. non-nil when a property was removed."
  14503. (unless (member property org-special-properties)
  14504. (org-with-point-at pom
  14505. (let ((range (org-get-property-block)))
  14506. (when range
  14507. (let* ((begin (car range))
  14508. (origin (cdr range))
  14509. (end (copy-marker origin))
  14510. (re (org-re-property
  14511. (concat (regexp-quote property) "\\+?") t t)))
  14512. (goto-char begin)
  14513. (while (re-search-forward re end t)
  14514. (delete-region (match-beginning 0) (line-beginning-position 2)))
  14515. ;; If drawer is empty, remove it altogether.
  14516. (when (= begin end)
  14517. (delete-region (line-beginning-position 0)
  14518. (line-beginning-position 2)))
  14519. ;; Return non-nil if some property was removed.
  14520. (prog1 (/= end origin) (set-marker end nil))))))))
  14521. ;; Multi-values properties are properties that contain multiple values
  14522. ;; These values are assumed to be single words, separated by whitespace.
  14523. (defun org-entry-add-to-multivalued-property (pom property value)
  14524. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  14525. (let* ((old (org-entry-get pom property))
  14526. (values (and old (org-split-string old "[ \t]"))))
  14527. (setq value (org-entry-protect-space value))
  14528. (unless (member value values)
  14529. (setq values (append values (list value)))
  14530. (org-entry-put pom property
  14531. (mapconcat 'identity values " ")))))
  14532. (defun org-entry-remove-from-multivalued-property (pom property value)
  14533. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  14534. (let* ((old (org-entry-get pom property))
  14535. (values (and old (org-split-string old "[ \t]"))))
  14536. (setq value (org-entry-protect-space value))
  14537. (when (member value values)
  14538. (setq values (delete value values))
  14539. (org-entry-put pom property
  14540. (mapconcat 'identity values " ")))))
  14541. (defun org-entry-member-in-multivalued-property (pom property value)
  14542. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  14543. (let* ((old (org-entry-get pom property))
  14544. (values (and old (org-split-string old "[ \t]"))))
  14545. (setq value (org-entry-protect-space value))
  14546. (member value values)))
  14547. (defun org-entry-get-multivalued-property (pom property)
  14548. "Return a list of values in a multivalued property."
  14549. (let* ((value (org-entry-get pom property))
  14550. (values (and value (org-split-string value "[ \t]"))))
  14551. (mapcar 'org-entry-restore-space values)))
  14552. (defun org-entry-put-multivalued-property (pom property &rest values)
  14553. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  14554. VALUES should be a list of strings. Spaces will be protected."
  14555. (org-entry-put pom property
  14556. (mapconcat 'org-entry-protect-space values " "))
  14557. (let* ((value (org-entry-get pom property))
  14558. (values (and value (org-split-string value "[ \t]"))))
  14559. (mapcar 'org-entry-restore-space values)))
  14560. (defun org-entry-protect-space (s)
  14561. "Protect spaces and newline in string S."
  14562. (while (string-match " " s)
  14563. (setq s (replace-match "%20" t t s)))
  14564. (while (string-match "\n" s)
  14565. (setq s (replace-match "%0A" t t s)))
  14566. s)
  14567. (defun org-entry-restore-space (s)
  14568. "Restore spaces and newline in string S."
  14569. (while (string-match "%20" s)
  14570. (setq s (replace-match " " t t s)))
  14571. (while (string-match "%0A" s)
  14572. (setq s (replace-match "\n" t t s)))
  14573. s)
  14574. (defvar org-entry-property-inherited-from (make-marker)
  14575. "Marker pointing to the entry from where a property was inherited.
  14576. Each call to `org-entry-get-with-inheritance' will set this marker to the
  14577. location of the entry where the inheritance search matched. If there was
  14578. no match, the marker will point nowhere.
  14579. Note that also `org-entry-get' calls this function, if the INHERIT flag
  14580. is set.")
  14581. (defun org-entry-get-with-inheritance (property &optional literal-nil)
  14582. "Get PROPERTY of entry or content at point, search higher levels if needed.
  14583. The search will stop at the first ancestor which has the property defined.
  14584. If the value found is \"nil\", return nil to show that the property
  14585. should be considered as undefined (this is the meaning of nil here).
  14586. However, if LITERAL-NIL is set, return the string value \"nil\" instead."
  14587. (move-marker org-entry-property-inherited-from nil)
  14588. (org-with-wide-buffer
  14589. (let (value)
  14590. (catch 'exit
  14591. (while t
  14592. (let ((v (org-property--local-values property literal-nil)))
  14593. (when v
  14594. (setq value
  14595. (concat (mapconcat #'identity (delq nil v) " ")
  14596. (and value " ")
  14597. value)))
  14598. (cond
  14599. ((car v)
  14600. (org-back-to-heading t)
  14601. (move-marker org-entry-property-inherited-from (point))
  14602. (throw 'exit nil))
  14603. ((org-up-heading-safe))
  14604. (t
  14605. (let ((global
  14606. (cdr (or (assoc-string property org-file-properties t)
  14607. (assoc-string property org-global-properties t)
  14608. (assoc-string property org-global-properties-fixed t)))))
  14609. (cond ((not global))
  14610. (value (setq value (concat global " " value)))
  14611. (t (setq value global))))
  14612. (throw 'exit nil))))))
  14613. (if literal-nil value (org-not-nil value)))))
  14614. (defvar org-property-changed-functions nil
  14615. "Hook called when the value of a property has changed.
  14616. Each hook function should accept two arguments, the name of the property
  14617. and the new value.")
  14618. (defun org-entry-put (pom property value)
  14619. "Set PROPERTY to VALUE for entry at point-or-marker POM.
  14620. If the value is nil, it is converted to the empty string. If it
  14621. is not a string, an error is raised. Also raise an error on
  14622. invalid property names.
  14623. PROPERTY can be any regular property (see
  14624. `org-special-properties'). It can also be \"TODO\",
  14625. \"PRIORITY\", \"SCHEDULED\" and \"DEADLINE\".
  14626. For the last two properties, VALUE may have any of the special
  14627. values \"earlier\" and \"later\". The function then increases or
  14628. decreases scheduled or deadline date by one day."
  14629. (cond ((null value) (setq value ""))
  14630. ((not (stringp value)) (error "Properties values should be strings"))
  14631. ((not (org--valid-property-p property))
  14632. (user-error "Invalid property name: \"%s\"" property)))
  14633. (org-with-point-at pom
  14634. (if (or (not (featurep 'org-inlinetask)) (org-inlinetask-in-task-p))
  14635. (org-back-to-heading t)
  14636. (org-with-limited-levels (org-back-to-heading t)))
  14637. (let ((beg (point)))
  14638. (cond
  14639. ((equal property "TODO")
  14640. (cond ((not (org-string-nw-p value)) (setq value 'none))
  14641. ((not (member value org-todo-keywords-1))
  14642. (user-error "\"%s\" is not a valid TODO state" value)))
  14643. (org-todo value)
  14644. (org-set-tags nil 'align))
  14645. ((equal property "PRIORITY")
  14646. (org-priority (if (org-string-nw-p value) (string-to-char value) ?\s))
  14647. (org-set-tags nil 'align))
  14648. ((equal property "SCHEDULED")
  14649. (forward-line)
  14650. (if (and (org-looking-at-p org-planning-line-re)
  14651. (re-search-forward
  14652. org-scheduled-time-regexp (line-end-position) t))
  14653. (cond ((string= value "earlier") (org-timestamp-change -1 'day))
  14654. ((string= value "later") (org-timestamp-change 1 'day))
  14655. ((string= value "") (org-schedule '(4)))
  14656. (t (org-schedule nil value)))
  14657. (if (member value '("earlier" "later" ""))
  14658. (call-interactively #'org-schedule)
  14659. (org-schedule nil value))))
  14660. ((equal property "DEADLINE")
  14661. (forward-line)
  14662. (if (and (org-looking-at-p org-planning-line-re)
  14663. (re-search-forward
  14664. org-deadline-time-regexp (line-end-position) t))
  14665. (cond ((string= value "earlier") (org-timestamp-change -1 'day))
  14666. ((string= value "later") (org-timestamp-change 1 'day))
  14667. ((string= value "") (org-deadline '(4)))
  14668. (t (org-deadline nil value)))
  14669. (if (member value '("earlier" "later" ""))
  14670. (call-interactively #'org-deadline)
  14671. (org-deadline nil value))))
  14672. ((member property org-special-properties)
  14673. (error "The %s property cannot be set with `org-entry-put'" property))
  14674. (t
  14675. (let* ((range (org-get-property-block beg 'force))
  14676. (end (cdr range))
  14677. (case-fold-search t))
  14678. (goto-char (car range))
  14679. (if (re-search-forward (org-re-property property nil t) end t)
  14680. (progn (delete-region (match-beginning 0) (match-end 0))
  14681. (goto-char (match-beginning 0)))
  14682. (goto-char end)
  14683. (insert "\n")
  14684. (backward-char))
  14685. (insert ":" property ":")
  14686. (when value (insert " " value))
  14687. (org-indent-line)))))
  14688. (run-hook-with-args 'org-property-changed-functions property value)))
  14689. (defun org-buffer-property-keys
  14690. (&optional specials defaults columns ignore-malformed)
  14691. "Get all property keys in the current buffer.
  14692. When SPECIALS is non-nil, also list the special properties that
  14693. reflect things like tags and TODO state.
  14694. When DEFAULTS is non-nil, also include properties that has
  14695. special meaning internally: ARCHIVE, CATEGORY, SUMMARY,
  14696. DESCRIPTION, LOCATION, and LOGGING and others.
  14697. When COLUMNS in non-nil, also include property names given in
  14698. COLUMN formats in the current buffer.
  14699. When IGNORE-MALFORMED is non-nil, malformed drawer repair will not be
  14700. automatically performed, such drawers will be silently ignored."
  14701. (let ((case-fold-search t)
  14702. (props (append
  14703. (and specials org-special-properties)
  14704. (and defaults (cons org-effort-property org-default-properties))
  14705. nil)))
  14706. (org-with-wide-buffer
  14707. (goto-char (point-min))
  14708. (while (re-search-forward org-property-start-re nil t)
  14709. (let ((range (org-get-property-block)))
  14710. (catch 'skip
  14711. (unless range
  14712. (when (and (not ignore-malformed)
  14713. (not (org-before-first-heading-p))
  14714. (y-or-n-p (format "Malformed drawer at %d, repair?"
  14715. (line-beginning-position))))
  14716. (org-get-property-block nil t))
  14717. (throw 'skip nil))
  14718. (goto-char (car range))
  14719. (let ((begin (car range))
  14720. (end (cdr range)))
  14721. ;; Make sure that found property block is not located
  14722. ;; before current point, as it would generate an infloop.
  14723. ;; It can happen, for example, in the following
  14724. ;; situation:
  14725. ;;
  14726. ;; * Headline
  14727. ;; :PROPERTIES:
  14728. ;; ...
  14729. ;; :END:
  14730. ;; *************** Inlinetask
  14731. ;; #+BEGIN_EXAMPLE
  14732. ;; :PROPERTIES:
  14733. ;; #+END_EXAMPLE
  14734. ;;
  14735. (if (< begin (point)) (throw 'skip nil) (goto-char begin))
  14736. (while (< (point) end)
  14737. (let ((p (progn (looking-at org-property-re)
  14738. (org-match-string-no-properties 2))))
  14739. ;; Only add true property name, not extension symbol.
  14740. (push (if (not (org-string-match-p "\\+\\'" p)) p
  14741. (substring p 0 -1))
  14742. props))
  14743. (forward-line))))
  14744. (outline-next-heading)))
  14745. (when columns
  14746. (goto-char (point-min))
  14747. (while (re-search-forward "^[ \t]*\\(?:#\\+\\|:\\)COLUMNS:" nil t)
  14748. (let ((element (org-element-at-point)))
  14749. (when (memq (org-element-type element) '(keyword node-property))
  14750. (let ((value (org-element-property :value element))
  14751. (start 0))
  14752. (while (string-match "%[0-9]*\\(\\S-+\\)" value start)
  14753. (setq start (match-end 0))
  14754. (let ((p (org-match-string-no-properties 1 value)))
  14755. (unless (member-ignore-case p org-special-properties)
  14756. (push p props))))))))))
  14757. (sort (delete-dups props) (lambda (a b) (string< (upcase a) (upcase b))))))
  14758. (defun org-property-values (key)
  14759. "List all non-nil values of property KEY in current buffer."
  14760. (org-with-wide-buffer
  14761. (goto-char (point-min))
  14762. (let ((case-fold-search t)
  14763. (re (org-re-property key))
  14764. values)
  14765. (while (re-search-forward re nil t)
  14766. (push (org-entry-get (point) key) values))
  14767. (delete-dups values))))
  14768. (defun org-insert-property-drawer ()
  14769. "Insert a property drawer into the current entry."
  14770. (org-with-wide-buffer
  14771. (if (or (not (featurep 'org-inlinetask)) (org-inlinetask-in-task-p))
  14772. (org-back-to-heading t)
  14773. (org-with-limited-levels (org-back-to-heading t)))
  14774. (forward-line)
  14775. (when (org-looking-at-p org-planning-line-re) (forward-line))
  14776. (unless (org-looking-at-p org-property-drawer-re)
  14777. ;; Make sure we start editing a line from current entry, not from
  14778. ;; next one. It prevents extending text properties or overlays
  14779. ;; belonging to the latter.
  14780. (when (bolp) (backward-char))
  14781. (let ((begin (1+ (point)))
  14782. (inhibit-read-only t))
  14783. (insert "\n:PROPERTIES:\n:END:")
  14784. (when (eobp) (insert "\n"))
  14785. (org-indent-region begin (point))))))
  14786. (defun org-insert-drawer (&optional arg drawer)
  14787. "Insert a drawer at point.
  14788. When optional argument ARG is non-nil, insert a property drawer.
  14789. Optional argument DRAWER, when non-nil, is a string representing
  14790. drawer's name. Otherwise, the user is prompted for a name.
  14791. If a region is active, insert the drawer around that region
  14792. instead.
  14793. Point is left between drawer's boundaries."
  14794. (interactive "P")
  14795. (let* ((drawer (if arg "PROPERTIES"
  14796. (or drawer (read-from-minibuffer "Drawer: ")))))
  14797. (cond
  14798. ;; With C-u, fall back on `org-insert-property-drawer'
  14799. (arg (org-insert-property-drawer))
  14800. ;; Check validity of suggested drawer's name.
  14801. ((not (org-string-match-p org-drawer-regexp (format ":%s:" drawer)))
  14802. (user-error "Invalid drawer name"))
  14803. ;; With an active region, insert a drawer at point.
  14804. ((not (org-region-active-p))
  14805. (progn
  14806. (unless (bolp) (insert "\n"))
  14807. (insert (format ":%s:\n\n:END:\n" drawer))
  14808. (forward-line -2)))
  14809. ;; Otherwise, insert the drawer at point
  14810. (t
  14811. (let ((rbeg (region-beginning))
  14812. (rend (copy-marker (region-end))))
  14813. (unwind-protect
  14814. (progn
  14815. (goto-char rbeg)
  14816. (beginning-of-line)
  14817. (when (save-excursion
  14818. (re-search-forward org-outline-regexp-bol rend t))
  14819. (user-error "Drawers cannot contain headlines"))
  14820. ;; Position point at the beginning of the first
  14821. ;; non-blank line in region. Insert drawer's opening
  14822. ;; there, then indent it.
  14823. (org-skip-whitespace)
  14824. (beginning-of-line)
  14825. (insert ":" drawer ":\n")
  14826. (forward-line -1)
  14827. (indent-for-tab-command)
  14828. ;; Move point to the beginning of the first blank line
  14829. ;; after the last non-blank line in region. Insert
  14830. ;; drawer's closing, then indent it.
  14831. (goto-char rend)
  14832. (skip-chars-backward " \r\t\n")
  14833. (insert "\n:END:")
  14834. (deactivate-mark t)
  14835. (indent-for-tab-command)
  14836. (unless (eolp) (insert "\n")))
  14837. ;; Clear marker, whatever the outcome of insertion is.
  14838. (set-marker rend nil)))))))
  14839. (defvar org-property-set-functions-alist nil
  14840. "Property set function alist.
  14841. Each entry should have the following format:
  14842. (PROPERTY . READ-FUNCTION)
  14843. The read function will be called with the same argument as
  14844. `org-completing-read'.")
  14845. (defun org-set-property-function (property)
  14846. "Get the function that should be used to set PROPERTY.
  14847. This is computed according to `org-property-set-functions-alist'."
  14848. (or (cdr (assoc property org-property-set-functions-alist))
  14849. 'org-completing-read))
  14850. (defun org-read-property-value (property)
  14851. "Read PROPERTY value from user."
  14852. (let* ((completion-ignore-case t)
  14853. (allowed (org-property-get-allowed-values nil property 'table))
  14854. (cur (org-entry-get nil property))
  14855. (prompt (concat property " value"
  14856. (if (and cur (string-match "\\S-" cur))
  14857. (concat " [" cur "]") "") ": "))
  14858. (set-function (org-set-property-function property))
  14859. (val (if allowed
  14860. (funcall set-function prompt allowed nil
  14861. (not (get-text-property 0 'org-unrestricted
  14862. (caar allowed))))
  14863. (let (org-completion-use-ido org-completion-use-iswitchb)
  14864. (funcall set-function prompt
  14865. (mapcar 'list (org-property-values property))
  14866. nil nil "" nil cur)))))
  14867. (org-trim val)))
  14868. (defvar org-last-set-property nil)
  14869. (defvar org-last-set-property-value nil)
  14870. (defun org-read-property-name ()
  14871. "Read a property name."
  14872. (let ((completion-ignore-case t)
  14873. (default-prop (or (and (org-at-property-p)
  14874. (org-match-string-no-properties 2))
  14875. org-last-set-property)))
  14876. (org-completing-read
  14877. (concat "Property"
  14878. (if default-prop (concat " [" default-prop "]") "")
  14879. ": ")
  14880. (mapcar #'list (org-buffer-property-keys nil t t))
  14881. nil nil nil nil default-prop)))
  14882. (defun org-set-property-and-value (use-last)
  14883. "Allow to set [PROPERTY]: [value] direction from prompt.
  14884. When use-default, don't even ask, just use the last
  14885. \"[PROPERTY]: [value]\" string from the history."
  14886. (interactive "P")
  14887. (let* ((completion-ignore-case t)
  14888. (pv (or (and use-last org-last-set-property-value)
  14889. (org-completing-read
  14890. "Enter a \"[Property]: [value]\" pair: "
  14891. nil nil nil nil nil
  14892. org-last-set-property-value)))
  14893. prop val)
  14894. (when (string-match "^[ \t]*\\([^:]+\\):[ \t]*\\(.*\\)[ \t]*$" pv)
  14895. (setq prop (match-string 1 pv)
  14896. val (match-string 2 pv))
  14897. (org-set-property prop val))))
  14898. (defun org-set-property (property value)
  14899. "In the current entry, set PROPERTY to VALUE.
  14900. When called interactively, this will prompt for a property name, offering
  14901. completion on existing and default properties. And then it will prompt
  14902. for a value, offering completion either on allowed values (via an inherited
  14903. xxx_ALL property) or on existing values in other instances of this property
  14904. in the current file.
  14905. Throw an error when trying to set a property with an invalid name."
  14906. (interactive (list nil nil))
  14907. (let ((property (or property (org-read-property-name))))
  14908. ;; `org-entry-put' also makes the following check, but this one
  14909. ;; avoids polluting `org-last-set-property' and
  14910. ;; `org-last-set-property-value' needlessly.
  14911. (unless (org--valid-property-p property)
  14912. (user-error "Invalid property name: \"%s\"" property))
  14913. (let ((value (or value (org-read-property-value property)))
  14914. (fn (cdr (assoc-string property org-properties-postprocess-alist t))))
  14915. (setq org-last-set-property property)
  14916. (setq org-last-set-property-value (concat property ": " value))
  14917. ;; Possibly postprocess the inserted value:
  14918. (when fn (setq value (funcall fn value)))
  14919. (unless (equal (org-entry-get nil property) value)
  14920. (org-entry-put nil property value)))))
  14921. (defun org-find-property (property &optional value)
  14922. "Find first entry in buffer that sets PROPERTY.
  14923. When optional argument VALUE is non-nil, only consider an entry
  14924. if it contains PROPERTY set to this value. If PROPERTY should be
  14925. explicitly set to nil, use string \"nil\" for VALUE.
  14926. Return position where the entry begins, or nil if there is no
  14927. such entry. If narrowing is in effect, only search the visible
  14928. part of the buffer."
  14929. (save-excursion
  14930. (goto-char (point-min))
  14931. (let ((case-fold-search t)
  14932. (re (org-re-property property nil (not value) value)))
  14933. (catch 'exit
  14934. (while (re-search-forward re nil t)
  14935. (when (if value (org-at-property-p)
  14936. (org-entry-get (point) property nil t))
  14937. (throw 'exit (progn (org-back-to-heading t) (point)))))))))
  14938. (defun org-delete-property (property)
  14939. "In the current entry, delete PROPERTY."
  14940. (interactive
  14941. (let* ((completion-ignore-case t)
  14942. (cat (org-entry-get (point) "CATEGORY"))
  14943. (props0 (org-entry-properties nil 'standard))
  14944. (props (if cat props0
  14945. (delete `("CATEGORY" . ,(org-get-category)) props0)))
  14946. (prop (if (< 1 (length props))
  14947. (completing-read "Property: " props nil t)
  14948. (caar props))))
  14949. (list prop)))
  14950. (if (not property)
  14951. (message "No property to delete in this entry")
  14952. (org-entry-delete nil property)
  14953. (message "Property \"%s\" deleted" property)))
  14954. (defun org-delete-property-globally (property)
  14955. "Remove PROPERTY globally, from all entries.
  14956. This function ignores narrowing, if any."
  14957. (interactive
  14958. (let* ((completion-ignore-case t)
  14959. (prop (completing-read
  14960. "Globally remove property: "
  14961. (mapcar #'list (org-buffer-property-keys)))))
  14962. (list prop)))
  14963. (org-with-wide-buffer
  14964. (goto-char (point-min))
  14965. (let ((count 0)
  14966. (re (org-re-property (concat (regexp-quote property) "\\+?") t t)))
  14967. (while (re-search-forward re nil t)
  14968. (when (org-entry-delete (point) property) (cl-incf count)))
  14969. (message "Property \"%s\" removed from %d entries" property count))))
  14970. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  14971. (defun org-compute-property-at-point ()
  14972. "Compute the property at point.
  14973. This looks for an enclosing column format, extracts the operator and
  14974. then applies it to the property in the column format's scope."
  14975. (interactive)
  14976. (unless (org-at-property-p)
  14977. (user-error "Not at a property"))
  14978. (let ((prop (org-match-string-no-properties 2)))
  14979. (org-columns-get-format-and-top-level)
  14980. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  14981. (user-error "No operator defined for property %s" prop))
  14982. (org-columns-compute prop)))
  14983. (defvar org-property-allowed-value-functions nil
  14984. "Hook for functions supplying allowed values for a specific property.
  14985. The functions must take a single argument, the name of the property, and
  14986. return a flat list of allowed values. If \":ETC\" is one of
  14987. the values, this means that these values are intended as defaults for
  14988. completion, but that other values should be allowed too.
  14989. The functions must return nil if they are not responsible for this
  14990. property.")
  14991. (defun org-property-get-allowed-values (pom property &optional table)
  14992. "Get allowed values for the property PROPERTY.
  14993. When TABLE is non-nil, return an alist that can directly be used for
  14994. completion."
  14995. (let (vals)
  14996. (cond
  14997. ((equal property "TODO")
  14998. (setq vals (org-with-point-at pom
  14999. (append org-todo-keywords-1 '("")))))
  15000. ((equal property "PRIORITY")
  15001. (let ((n org-lowest-priority))
  15002. (while (>= n org-highest-priority)
  15003. (push (char-to-string n) vals)
  15004. (setq n (1- n)))))
  15005. ((equal property "CATEGORY"))
  15006. ((member property org-special-properties))
  15007. ((setq vals (run-hook-with-args-until-success
  15008. 'org-property-allowed-value-functions property)))
  15009. (t
  15010. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  15011. (when (and vals (string-match "\\S-" vals))
  15012. (setq vals (car (read-from-string (concat "(" vals ")"))))
  15013. (setq vals (mapcar (lambda (x)
  15014. (cond ((stringp x) x)
  15015. ((numberp x) (number-to-string x))
  15016. ((symbolp x) (symbol-name x))
  15017. (t "???")))
  15018. vals)))))
  15019. (when (member ":ETC" vals)
  15020. (setq vals (remove ":ETC" vals))
  15021. (org-add-props (car vals) '(org-unrestricted t)))
  15022. (if table (mapcar 'list vals) vals)))
  15023. (defun org-property-previous-allowed-value (&optional _previous)
  15024. "Switch to the next allowed value for this property."
  15025. (interactive)
  15026. (org-property-next-allowed-value t))
  15027. (defun org-property-next-allowed-value (&optional previous)
  15028. "Switch to the next allowed value for this property."
  15029. (interactive)
  15030. (unless (org-at-property-p)
  15031. (user-error "Not at a property"))
  15032. (let* ((prop (car (save-match-data (org-split-string (match-string 1) ":"))))
  15033. (key (match-string 2))
  15034. (value (match-string 3))
  15035. (allowed (or (org-property-get-allowed-values (point) key)
  15036. (and (member value '("[ ]" "[-]" "[X]"))
  15037. '("[ ]" "[X]"))))
  15038. (heading (save-match-data (nth 4 (org-heading-components))))
  15039. nval)
  15040. (unless allowed
  15041. (user-error "Allowed values for this property have not been defined"))
  15042. (when previous (setq allowed (reverse allowed)))
  15043. (when (member value allowed)
  15044. (setq nval (car (cdr (member value allowed)))))
  15045. (setq nval (or nval (car allowed)))
  15046. (when (equal nval value)
  15047. (user-error "Only one allowed value for this property"))
  15048. (org-at-property-p)
  15049. (replace-match (concat " :" key ": " nval) t t)
  15050. (org-indent-line)
  15051. (beginning-of-line 1)
  15052. (skip-chars-forward " \t")
  15053. (when (equal prop org-effort-property)
  15054. (org-refresh-property
  15055. '((effort . identity)
  15056. (effort-minutes . org-duration-string-to-minutes))
  15057. nval)
  15058. (when (string= org-clock-current-task heading)
  15059. (setq org-clock-effort nval)
  15060. (org-clock-update-mode-line)))
  15061. (run-hook-with-args 'org-property-changed-functions key nval)))
  15062. (defun org-find-olp (path &optional this-buffer)
  15063. "Return a marker pointing to the entry at outline path OLP.
  15064. If anything goes wrong, throw an error.
  15065. You can wrap this call to catch the error like this:
  15066. \(condition-case msg
  15067. \(org-mobile-locate-entry (match-string 4))
  15068. \(error (nth 1 msg)))
  15069. The return value will then be either a string with the error message,
  15070. or a marker if everything is OK.
  15071. If THIS-BUFFER is set, the outline path does not contain a file,
  15072. only headings."
  15073. (let* ((file (if this-buffer buffer-file-name (pop path)))
  15074. (buffer (if this-buffer (current-buffer) (find-file-noselect file)))
  15075. (level 1)
  15076. (lmin 1)
  15077. (lmax 1)
  15078. end found flevel)
  15079. (unless buffer (error "File not found :%s" file))
  15080. (with-current-buffer buffer
  15081. (org-with-wide-buffer
  15082. (goto-char (point-min))
  15083. (dolist (heading path)
  15084. (let ((re (format org-complex-heading-regexp-format
  15085. (regexp-quote heading)))
  15086. (cnt 0))
  15087. (while (re-search-forward re end t)
  15088. (setq level (- (match-end 1) (match-beginning 1)))
  15089. (when (and (>= level lmin) (<= level lmax))
  15090. (setq found (match-beginning 0) flevel level cnt (1+ cnt))))
  15091. (when (= cnt 0)
  15092. (error "Heading not found on level %d: %s" lmax heading))
  15093. (when (> cnt 1)
  15094. (error "Heading not unique on level %d: %s" lmax heading))
  15095. (goto-char found)
  15096. (setq lmin (1+ flevel) lmax (+ lmin (if org-odd-levels-only 1 0)))
  15097. (setq end (save-excursion (org-end-of-subtree t t)))))
  15098. (when (org-at-heading-p)
  15099. (point-marker))))))
  15100. (defun org-find-exact-headline-in-buffer (heading &optional buffer pos-only)
  15101. "Find node HEADING in BUFFER.
  15102. Return a marker to the heading if it was found, or nil if not.
  15103. If POS-ONLY is set, return just the position instead of a marker.
  15104. The heading text must match exact, but it may have a TODO keyword,
  15105. a priority cookie and tags in the standard locations."
  15106. (with-current-buffer (or buffer (current-buffer))
  15107. (save-excursion
  15108. (save-restriction
  15109. (widen)
  15110. (goto-char (point-min))
  15111. (let (case-fold-search)
  15112. (when (re-search-forward
  15113. (format org-complex-heading-regexp-format
  15114. (regexp-quote heading)) nil t)
  15115. (if pos-only
  15116. (match-beginning 0)
  15117. (move-marker (make-marker) (match-beginning 0)))))))))
  15118. (defun org-find-exact-heading-in-directory (heading &optional dir)
  15119. "Find Org node headline HEADING in all .org files in directory DIR.
  15120. When the target headline is found, return a marker to this location."
  15121. (let ((files (directory-files (or dir default-directory)
  15122. t "\\`[^.#].*\\.org\\'"))
  15123. visiting m buffer)
  15124. (catch 'found
  15125. (dolist (file files)
  15126. (message "trying %s" file)
  15127. (setq visiting (org-find-base-buffer-visiting file))
  15128. (setq buffer (or visiting (find-file-noselect file)))
  15129. (setq m (org-find-exact-headline-in-buffer
  15130. heading buffer))
  15131. (when (and (not m) (not visiting)) (kill-buffer buffer))
  15132. (and m (throw 'found m))))))
  15133. (defun org-find-entry-with-id (ident)
  15134. "Locate the entry that contains the ID property with exact value IDENT.
  15135. IDENT can be a string, a symbol or a number, this function will search for
  15136. the string representation of it.
  15137. Return the position where this entry starts, or nil if there is no such entry."
  15138. (interactive "sID: ")
  15139. (let ((id (cond
  15140. ((stringp ident) ident)
  15141. ((symbolp ident) (symbol-name ident))
  15142. ((numberp ident) (number-to-string ident))
  15143. (t (error "IDENT %s must be a string, symbol or number" ident)))))
  15144. (org-with-wide-buffer (org-find-property "ID" id))))
  15145. ;;;; Timestamps
  15146. (defvar org-last-changed-timestamp nil)
  15147. (defvar org-last-inserted-timestamp nil
  15148. "The last time stamp inserted with `org-insert-time-stamp'.")
  15149. (defvar org-ts-what) ; dynamically scoped parameter
  15150. (defun org-time-stamp (arg &optional inactive)
  15151. "Prompt for a date/time and insert a time stamp.
  15152. If the user specifies a time like HH:MM or if this command is
  15153. called with at least one prefix argument, the time stamp contains
  15154. the date and the time. Otherwise, only the date is included.
  15155. All parts of a date not specified by the user are filled in from
  15156. the timestamp at point, if any, or the current date/time
  15157. otherwise.
  15158. If there is already a timestamp at the cursor, it is replaced.
  15159. With two universal prefix arguments, insert an active timestamp
  15160. with the current time without prompting the user.
  15161. When called from lisp, the timestamp is inactive if INACTIVE is
  15162. non-nil."
  15163. (interactive "P")
  15164. (let* ((ts (cond
  15165. ((org-at-date-range-p t)
  15166. (match-string (if (< (point) (- (match-beginning 2) 2)) 1 2)))
  15167. ((org-at-timestamp-p t) (match-string 0))))
  15168. ;; Default time is either the timestamp at point or today.
  15169. ;; When entering a range, only the range start is considered.
  15170. (default-time (if (not ts) (current-time)
  15171. (apply #'encode-time (org-parse-time-string ts))))
  15172. (default-input (and ts (org-get-compact-tod ts)))
  15173. (repeater (and ts
  15174. (string-match "\\([.+-]+[0-9]+[hdwmy] ?\\)+" ts)
  15175. (match-string 0 ts)))
  15176. org-time-was-given
  15177. org-end-time-was-given
  15178. (time
  15179. (and (if (equal arg '(16)) (current-time)
  15180. ;; Preserve `this-command' and `last-command'.
  15181. (let ((this-command this-command)
  15182. (last-command last-command))
  15183. (org-read-date
  15184. arg 'totime nil nil default-time default-input
  15185. inactive))))))
  15186. (cond
  15187. ((and ts
  15188. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  15189. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  15190. (insert "--")
  15191. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  15192. (ts
  15193. ;; Make sure we're on a timestamp. When in the middle of a date
  15194. ;; range, move arbitrarily to range end.
  15195. (unless (org-at-timestamp-p t)
  15196. (skip-chars-forward "-")
  15197. (org-at-timestamp-p t))
  15198. (replace-match "")
  15199. (setq org-last-changed-timestamp
  15200. (org-insert-time-stamp
  15201. time (or org-time-was-given arg)
  15202. inactive nil nil (list org-end-time-was-given)))
  15203. (when repeater
  15204. (backward-char)
  15205. (insert " " repeater)
  15206. (setq org-last-changed-timestamp
  15207. (concat (substring org-last-inserted-timestamp 0 -1)
  15208. " " repeater ">")))
  15209. (message "Timestamp updated"))
  15210. ((equal arg '(16)) (org-insert-time-stamp time t inactive))
  15211. (t (org-insert-time-stamp
  15212. time (or org-time-was-given arg) inactive nil nil
  15213. (list org-end-time-was-given))))))
  15214. ;; FIXME: can we use this for something else, like computing time differences?
  15215. (defun org-get-compact-tod (s)
  15216. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  15217. (let* ((t1 (match-string 1 s))
  15218. (h1 (string-to-number (match-string 2 s)))
  15219. (m1 (string-to-number (match-string 3 s)))
  15220. (t2 (and (match-end 4) (match-string 5 s)))
  15221. (h2 (and t2 (string-to-number (match-string 6 s))))
  15222. (m2 (and t2 (string-to-number (match-string 7 s))))
  15223. dh dm)
  15224. (if (not t2)
  15225. t1
  15226. (setq dh (- h2 h1) dm (- m2 m1))
  15227. (when (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  15228. (concat t1 "+" (number-to-string dh)
  15229. (and (/= 0 dm) (format ":%02d" dm)))))))
  15230. (defun org-time-stamp-inactive (&optional arg)
  15231. "Insert an inactive time stamp.
  15232. An inactive time stamp is enclosed in square brackets instead of angle
  15233. brackets. It is inactive in the sense that it does not trigger agenda entries,
  15234. does not link to the calendar and cannot be changed with the S-cursor keys.
  15235. So these are more for recording a certain time/date."
  15236. (interactive "P")
  15237. (org-time-stamp arg 'inactive))
  15238. (defvar org-date-ovl (make-overlay 1 1))
  15239. (overlay-put org-date-ovl 'face 'org-date-selected)
  15240. (org-detach-overlay org-date-ovl)
  15241. (defvar org-ans1) ; dynamically scoped parameter
  15242. (defvar org-ans2) ; dynamically scoped parameter
  15243. (defvar org-plain-time-of-day-regexp) ; defined below
  15244. (defvar org-overriding-default-time nil) ; dynamically scoped
  15245. (defvar org-read-date-overlay nil)
  15246. (defvar org-dcst nil) ; dynamically scoped
  15247. (defvar org-read-date-history nil)
  15248. (defvar org-read-date-final-answer nil)
  15249. (defvar org-read-date-analyze-futurep nil)
  15250. (defvar org-read-date-analyze-forced-year nil)
  15251. (defvar org-read-date-inactive)
  15252. (defvar org-read-date-minibuffer-local-map
  15253. (let* ((map (make-sparse-keymap)))
  15254. (set-keymap-parent map minibuffer-local-map)
  15255. (org-defkey map (kbd ".")
  15256. (lambda () (interactive)
  15257. ;; Are we at the beginning of the prompt?
  15258. (if (org-looking-back "^[^:]+: "
  15259. (let ((inhibit-field-text-motion t))
  15260. (line-beginning-position)))
  15261. (org-eval-in-calendar '(calendar-goto-today))
  15262. (insert "."))))
  15263. (org-defkey map (kbd "C-.")
  15264. (lambda () (interactive)
  15265. (org-eval-in-calendar '(calendar-goto-today))))
  15266. (org-defkey map [(meta shift left)]
  15267. (lambda () (interactive)
  15268. (org-eval-in-calendar '(calendar-backward-month 1))))
  15269. (org-defkey map [(meta shift right)]
  15270. (lambda () (interactive)
  15271. (org-eval-in-calendar '(calendar-forward-month 1))))
  15272. (org-defkey map [(meta shift up)]
  15273. (lambda () (interactive)
  15274. (org-eval-in-calendar '(calendar-backward-year 1))))
  15275. (org-defkey map [(meta shift down)]
  15276. (lambda () (interactive)
  15277. (org-eval-in-calendar '(calendar-forward-year 1))))
  15278. (org-defkey map [?\e (shift left)]
  15279. (lambda () (interactive)
  15280. (org-eval-in-calendar '(calendar-backward-month 1))))
  15281. (org-defkey map [?\e (shift right)]
  15282. (lambda () (interactive)
  15283. (org-eval-in-calendar '(calendar-forward-month 1))))
  15284. (org-defkey map [?\e (shift up)]
  15285. (lambda () (interactive)
  15286. (org-eval-in-calendar '(calendar-backward-year 1))))
  15287. (org-defkey map [?\e (shift down)]
  15288. (lambda () (interactive)
  15289. (org-eval-in-calendar '(calendar-forward-year 1))))
  15290. (org-defkey map [(shift up)]
  15291. (lambda () (interactive)
  15292. (org-eval-in-calendar '(calendar-backward-week 1))))
  15293. (org-defkey map [(shift down)]
  15294. (lambda () (interactive)
  15295. (org-eval-in-calendar '(calendar-forward-week 1))))
  15296. (org-defkey map [(shift left)]
  15297. (lambda () (interactive)
  15298. (org-eval-in-calendar '(calendar-backward-day 1))))
  15299. (org-defkey map [(shift right)]
  15300. (lambda () (interactive)
  15301. (org-eval-in-calendar '(calendar-forward-day 1))))
  15302. (org-defkey map "!"
  15303. (lambda () (interactive)
  15304. (org-eval-in-calendar '(diary-view-entries))
  15305. (message "")))
  15306. (org-defkey map ">"
  15307. (lambda () (interactive)
  15308. (org-eval-in-calendar '(calendar-scroll-left 1))))
  15309. (org-defkey map "<"
  15310. (lambda () (interactive)
  15311. (org-eval-in-calendar '(calendar-scroll-right 1))))
  15312. (org-defkey map "\C-v"
  15313. (lambda () (interactive)
  15314. (org-eval-in-calendar
  15315. '(calendar-scroll-left-three-months 1))))
  15316. (org-defkey map "\M-v"
  15317. (lambda () (interactive)
  15318. (org-eval-in-calendar
  15319. '(calendar-scroll-right-three-months 1))))
  15320. map)
  15321. "Keymap for minibuffer commands when using `org-read-date'.")
  15322. (defvar org-def)
  15323. (defvar org-defdecode)
  15324. (defvar org-with-time)
  15325. (defvar calendar-setup) ; Dynamically scoped.
  15326. (defun org-read-date (&optional with-time to-time from-string prompt
  15327. default-time default-input inactive)
  15328. "Read a date, possibly a time, and make things smooth for the user.
  15329. The prompt will suggest to enter an ISO date, but you can also enter anything
  15330. which will at least partially be understood by `parse-time-string'.
  15331. Unrecognized parts of the date will default to the current day, month, year,
  15332. hour and minute. If this command is called to replace a timestamp at point,
  15333. or to enter the second timestamp of a range, the default time is taken
  15334. from the existing stamp. Furthermore, the command prefers the future,
  15335. so if you are giving a date where the year is not given, and the day-month
  15336. combination is already past in the current year, it will assume you
  15337. mean next year. For details, see the manual. A few examples:
  15338. 3-2-5 --> 2003-02-05
  15339. feb 15 --> currentyear-02-15
  15340. 2/15 --> currentyear-02-15
  15341. sep 12 9 --> 2009-09-12
  15342. 12:45 --> today 12:45
  15343. 22 sept 0:34 --> currentyear-09-22 0:34
  15344. 12 --> currentyear-currentmonth-12
  15345. Fri --> nearest Friday after today
  15346. -Tue --> last Tuesday
  15347. etc.
  15348. Furthermore you can specify a relative date by giving, as the *first* thing
  15349. in the input: a plus/minus sign, a number and a letter [hdwmy] to indicate
  15350. change in days weeks, months, years.
  15351. With a single plus or minus, the date is relative to today. With a double
  15352. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  15353. +4d --> four days from today
  15354. +4 --> same as above
  15355. +2w --> two weeks from today
  15356. ++5 --> five days from default date
  15357. The function understands only English month and weekday abbreviations.
  15358. While prompting, a calendar is popped up - you can also select the
  15359. date with the mouse (button 1). The calendar shows a period of three
  15360. months. To scroll it to other months, use the keys `>' and `<'.
  15361. If you don't like the calendar, turn it off with
  15362. (setq org-read-date-popup-calendar nil)
  15363. With optional argument TO-TIME, the date will immediately be converted
  15364. to an internal time.
  15365. With an optional argument WITH-TIME, the prompt will suggest to
  15366. also insert a time. Note that when WITH-TIME is not set, you can
  15367. still enter a time, and this function will inform the calling routine
  15368. about this change. The calling routine may then choose to change the
  15369. format used to insert the time stamp into the buffer to include the time.
  15370. With optional argument FROM-STRING, read from this string instead from
  15371. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  15372. the time/date that is used for everything that is not specified by the
  15373. user."
  15374. (require 'parse-time)
  15375. (let* ((org-with-time with-time)
  15376. (org-time-stamp-rounding-minutes
  15377. (if (equal org-with-time '(16))
  15378. '(0 0)
  15379. org-time-stamp-rounding-minutes))
  15380. (org-dcst org-display-custom-times)
  15381. (ct (org-current-time))
  15382. (org-def (or org-overriding-default-time default-time ct))
  15383. (org-defdecode (decode-time org-def))
  15384. (cur-frame (selected-frame))
  15385. (mouse-autoselect-window nil) ; Don't let the mouse jump
  15386. (calendar-setup
  15387. (and (eq calendar-setup 'calendar-only) 'calendar-only))
  15388. (calendar-move-hook nil)
  15389. (calendar-view-diary-initially-flag nil)
  15390. (calendar-view-holidays-initially-flag nil)
  15391. ans (org-ans0 "") org-ans1 org-ans2 final cal-frame)
  15392. ;; Rationalize `org-def' and `org-defdecode', if required.
  15393. (when (< (nth 2 org-defdecode) org-extend-today-until)
  15394. (setf (nth 2 org-defdecode) -1)
  15395. (setf (nth 1 org-defdecode) 59)
  15396. (setq org-def (apply #'encode-time org-defdecode))
  15397. (setq org-defdecode (decode-time org-def)))
  15398. (let* ((timestr (format-time-string
  15399. (if org-with-time "%Y-%m-%d %H:%M" "%Y-%m-%d")
  15400. org-def))
  15401. (prompt (concat (if prompt (concat prompt " ") "")
  15402. (format "Date+time [%s]: " timestr))))
  15403. (cond
  15404. (from-string (setq ans from-string))
  15405. (org-read-date-popup-calendar
  15406. (save-excursion
  15407. (save-window-excursion
  15408. (calendar)
  15409. (when (eq calendar-setup 'calendar-only)
  15410. (setq cal-frame
  15411. (window-frame (get-buffer-window "*Calendar*" 'visible)))
  15412. (select-frame cal-frame))
  15413. (org-eval-in-calendar '(setq cursor-type nil) t)
  15414. (unwind-protect
  15415. (progn
  15416. (calendar-forward-day (- (time-to-days org-def)
  15417. (calendar-absolute-from-gregorian
  15418. (calendar-current-date))))
  15419. (org-eval-in-calendar nil t)
  15420. (let* ((old-map (current-local-map))
  15421. (map (copy-keymap calendar-mode-map))
  15422. (minibuffer-local-map
  15423. (copy-keymap org-read-date-minibuffer-local-map)))
  15424. (org-defkey map (kbd "RET") 'org-calendar-select)
  15425. (org-defkey map [mouse-1] 'org-calendar-select-mouse)
  15426. (org-defkey map [mouse-2] 'org-calendar-select-mouse)
  15427. (unwind-protect
  15428. (progn
  15429. (use-local-map map)
  15430. (setq org-read-date-inactive inactive)
  15431. (add-hook 'post-command-hook 'org-read-date-display)
  15432. (setq org-ans0
  15433. (read-string prompt
  15434. default-input
  15435. 'org-read-date-history
  15436. nil))
  15437. ;; org-ans0: from prompt
  15438. ;; org-ans1: from mouse click
  15439. ;; org-ans2: from calendar motion
  15440. (setq ans
  15441. (concat org-ans0 " " (or org-ans1 org-ans2))))
  15442. (remove-hook 'post-command-hook 'org-read-date-display)
  15443. (use-local-map old-map)
  15444. (when org-read-date-overlay
  15445. (delete-overlay org-read-date-overlay)
  15446. (setq org-read-date-overlay nil)))))
  15447. (bury-buffer "*Calendar*")
  15448. (when cal-frame
  15449. (delete-frame cal-frame)
  15450. (select-frame-set-input-focus cur-frame))))))
  15451. (t ; Naked prompt only
  15452. (unwind-protect
  15453. (setq ans (read-string prompt default-input
  15454. 'org-read-date-history timestr))
  15455. (when org-read-date-overlay
  15456. (delete-overlay org-read-date-overlay)
  15457. (setq org-read-date-overlay nil))))))
  15458. (setq final (org-read-date-analyze ans org-def org-defdecode))
  15459. (when org-read-date-analyze-forced-year
  15460. (message "Year was forced into %s"
  15461. (if org-read-date-force-compatible-dates
  15462. "compatible range (1970-2037)"
  15463. "range representable on this machine"))
  15464. (ding))
  15465. ;; One round trip to get rid of 34th of August and stuff like that....
  15466. (setq final (decode-time (apply 'encode-time final)))
  15467. (setq org-read-date-final-answer ans)
  15468. (if to-time
  15469. (apply 'encode-time final)
  15470. (if (and (boundp 'org-time-was-given) org-time-was-given)
  15471. (format "%04d-%02d-%02d %02d:%02d"
  15472. (nth 5 final) (nth 4 final) (nth 3 final)
  15473. (nth 2 final) (nth 1 final))
  15474. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  15475. (defun org-read-date-display ()
  15476. "Display the current date prompt interpretation in the minibuffer."
  15477. (when org-read-date-display-live
  15478. (when org-read-date-overlay
  15479. (delete-overlay org-read-date-overlay))
  15480. (when (minibufferp (current-buffer))
  15481. (save-excursion
  15482. (end-of-line 1)
  15483. (while (not (equal (buffer-substring
  15484. (max (point-min) (- (point) 4)) (point))
  15485. " "))
  15486. (insert " ")))
  15487. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  15488. " " (or org-ans1 org-ans2)))
  15489. (org-end-time-was-given nil)
  15490. (f (org-read-date-analyze ans org-def org-defdecode))
  15491. (fmts (if org-dcst
  15492. org-time-stamp-custom-formats
  15493. org-time-stamp-formats))
  15494. (fmt (if (or org-with-time
  15495. (and (boundp 'org-time-was-given) org-time-was-given))
  15496. (cdr fmts)
  15497. (car fmts)))
  15498. (txt (format-time-string fmt (apply 'encode-time f)))
  15499. (txt (if org-read-date-inactive (concat "[" (substring txt 1 -1) "]") txt))
  15500. (txt (concat "=> " txt)))
  15501. (when (and org-end-time-was-given
  15502. (string-match org-plain-time-of-day-regexp txt))
  15503. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  15504. org-end-time-was-given
  15505. (substring txt (match-end 0)))))
  15506. (when org-read-date-analyze-futurep
  15507. (setq txt (concat txt " (=>F)")))
  15508. (setq org-read-date-overlay
  15509. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  15510. (org-overlay-display org-read-date-overlay txt 'secondary-selection)))))
  15511. (defun org-read-date-analyze (ans def defdecode)
  15512. "Analyze the combined answer of the date prompt."
  15513. ;; FIXME: cleanup and comment
  15514. ;; Pass `current-time' result to `decode-time' (instead of calling
  15515. ;; without arguments) so that only `current-time' has to be
  15516. ;; overriden in tests.
  15517. (let ((org-def def)
  15518. (org-defdecode defdecode)
  15519. (nowdecode (decode-time (current-time)))
  15520. delta deltan deltaw deltadef year month day
  15521. hour minute second wday pm h2 m2 tl wday1
  15522. iso-year iso-weekday iso-week iso-date futurep kill-year)
  15523. (setq org-read-date-analyze-futurep nil
  15524. org-read-date-analyze-forced-year nil)
  15525. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  15526. (setq ans "+0"))
  15527. (when (setq delta (org-read-date-get-relative ans (current-time) org-def))
  15528. (setq ans (replace-match "" t t ans)
  15529. deltan (car delta)
  15530. deltaw (nth 1 delta)
  15531. deltadef (nth 2 delta)))
  15532. ;; Check if there is an iso week date in there. If yes, store the
  15533. ;; info and postpone interpreting it until the rest of the parsing
  15534. ;; is done.
  15535. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  15536. (setq iso-year (when (match-end 1)
  15537. (org-small-year-to-year
  15538. (string-to-number (match-string 1 ans))))
  15539. iso-weekday (when (match-end 3)
  15540. (string-to-number (match-string 3 ans)))
  15541. iso-week (string-to-number (match-string 2 ans)))
  15542. (setq ans (replace-match "" t t ans)))
  15543. ;; Help matching ISO dates with single digit month or day, like 2006-8-11.
  15544. (when (string-match
  15545. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  15546. (setq year (if (match-end 2)
  15547. (string-to-number (match-string 2 ans))
  15548. (progn (setq kill-year t)
  15549. (string-to-number (format-time-string "%Y"))))
  15550. month (string-to-number (match-string 3 ans))
  15551. day (string-to-number (match-string 4 ans)))
  15552. (when (< year 100) (setq year (+ 2000 year)))
  15553. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  15554. t nil ans)))
  15555. ;; Help matching dotted european dates
  15556. (when (string-match
  15557. "^ *\\(3[01]\\|0?[1-9]\\|[12][0-9]\\)\\. ?\\(0?[1-9]\\|1[012]\\)\\.\\( ?[1-9][0-9]\\{3\\}\\)?" ans)
  15558. (setq year (if (match-end 3) (string-to-number (match-string 3 ans))
  15559. (setq kill-year t)
  15560. (string-to-number (format-time-string "%Y")))
  15561. day (string-to-number (match-string 1 ans))
  15562. month (string-to-number (match-string 2 ans))
  15563. ans (replace-match (format "%04d-%02d-%02d" year month day)
  15564. t nil ans)))
  15565. ;; Help matching american dates, like 5/30 or 5/30/7
  15566. (when (string-match
  15567. "^ *\\(0?[1-9]\\|1[012]\\)/\\(0?[1-9]\\|[12][0-9]\\|3[01]\\)\\(/\\([0-9]+\\)\\)?\\([^/0-9]\\|$\\)" ans)
  15568. (setq year (if (match-end 4)
  15569. (string-to-number (match-string 4 ans))
  15570. (progn (setq kill-year t)
  15571. (string-to-number (format-time-string "%Y"))))
  15572. month (string-to-number (match-string 1 ans))
  15573. day (string-to-number (match-string 2 ans)))
  15574. (when (< year 100) (setq year (+ 2000 year)))
  15575. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  15576. t nil ans)))
  15577. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  15578. ;; If there is a time with am/pm, and *no* time without it, we convert
  15579. ;; so that matching will be successful.
  15580. (cl-loop for i from 1 to 2 do ; twice, for end time as well
  15581. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  15582. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  15583. (setq hour (string-to-number (match-string 1 ans))
  15584. minute (if (match-end 3)
  15585. (string-to-number (match-string 3 ans))
  15586. 0)
  15587. pm (equal ?p
  15588. (string-to-char (downcase (match-string 4 ans)))))
  15589. (if (and (= hour 12) (not pm))
  15590. (setq hour 0)
  15591. (when (and pm (< hour 12)) (setq hour (+ 12 hour))))
  15592. (setq ans (replace-match (format "%02d:%02d" hour minute)
  15593. t t ans))))
  15594. ;; Check if a time range is given as a duration
  15595. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  15596. (setq hour (string-to-number (match-string 1 ans))
  15597. h2 (+ hour (string-to-number (match-string 3 ans)))
  15598. minute (string-to-number (match-string 2 ans))
  15599. m2 (+ minute (if (match-end 5) (string-to-number
  15600. (match-string 5 ans))0)))
  15601. (when (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  15602. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  15603. t t ans)))
  15604. ;; Check if there is a time range
  15605. (when (boundp 'org-end-time-was-given)
  15606. (setq org-time-was-given nil)
  15607. (when (and (string-match org-plain-time-of-day-regexp ans)
  15608. (match-end 8))
  15609. (setq org-end-time-was-given (match-string 8 ans))
  15610. (setq ans (concat (substring ans 0 (match-beginning 7))
  15611. (substring ans (match-end 7))))))
  15612. (setq tl (parse-time-string ans)
  15613. day (or (nth 3 tl) (nth 3 org-defdecode))
  15614. month
  15615. (cond ((nth 4 tl))
  15616. ((not org-read-date-prefer-future) (nth 4 org-defdecode))
  15617. ;; Day was specified. Make sure DAY+MONTH
  15618. ;; combination happens in the future.
  15619. ((nth 3 tl)
  15620. (setq futurep t)
  15621. (if (< day (nth 3 nowdecode)) (1+ (nth 4 nowdecode))
  15622. (nth 4 nowdecode)))
  15623. (t (nth 4 org-defdecode)))
  15624. year
  15625. (cond ((and (not kill-year) (nth 5 tl)))
  15626. ((not org-read-date-prefer-future) (nth 5 org-defdecode))
  15627. ;; Month was guessed in the future and is at least
  15628. ;; equal to NOWDECODE's. Fix year accordingly.
  15629. (futurep
  15630. (if (or (> month (nth 4 nowdecode))
  15631. (>= day (nth 3 nowdecode)))
  15632. (nth 5 nowdecode)
  15633. (1+ (nth 5 nowdecode))))
  15634. ;; Month was specified. Make sure MONTH+YEAR
  15635. ;; combination happens in the future.
  15636. ((nth 4 tl)
  15637. (setq futurep t)
  15638. (cond ((> month (nth 4 nowdecode)) (nth 5 nowdecode))
  15639. ((< month (nth 4 nowdecode)) (1+ (nth 5 nowdecode)))
  15640. ((< day (nth 3 nowdecode)) (1+ (nth 5 nowdecode)))
  15641. (t (nth 5 nowdecode))))
  15642. (t (nth 5 org-defdecode)))
  15643. hour (or (nth 2 tl) (nth 2 org-defdecode))
  15644. minute (or (nth 1 tl) (nth 1 org-defdecode))
  15645. second (or (nth 0 tl) 0)
  15646. wday (nth 6 tl))
  15647. (when (and (eq org-read-date-prefer-future 'time)
  15648. (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
  15649. (equal day (nth 3 nowdecode))
  15650. (equal month (nth 4 nowdecode))
  15651. (equal year (nth 5 nowdecode))
  15652. (nth 2 tl)
  15653. (or (< (nth 2 tl) (nth 2 nowdecode))
  15654. (and (= (nth 2 tl) (nth 2 nowdecode))
  15655. (nth 1 tl)
  15656. (< (nth 1 tl) (nth 1 nowdecode)))))
  15657. (setq day (1+ day)
  15658. futurep t))
  15659. ;; Special date definitions below
  15660. (cond
  15661. (iso-week
  15662. ;; There was an iso week
  15663. (require 'cal-iso)
  15664. (setq futurep nil)
  15665. (setq year (or iso-year year)
  15666. day (or iso-weekday wday 1)
  15667. wday nil ; to make sure that the trigger below does not match
  15668. iso-date (calendar-gregorian-from-absolute
  15669. (calendar-iso-to-absolute
  15670. (list iso-week day year))))
  15671. ; FIXME: Should we also push ISO weeks into the future?
  15672. ; (when (and org-read-date-prefer-future
  15673. ; (not iso-year)
  15674. ; (< (calendar-absolute-from-gregorian iso-date)
  15675. ; (time-to-days (current-time))))
  15676. ; (setq year (1+ year)
  15677. ; iso-date (calendar-gregorian-from-absolute
  15678. ; (calendar-iso-to-absolute
  15679. ; (list iso-week day year)))))
  15680. (setq month (car iso-date)
  15681. year (nth 2 iso-date)
  15682. day (nth 1 iso-date)))
  15683. (deltan
  15684. (setq futurep nil)
  15685. (unless deltadef
  15686. ;; Pass `current-time' result to `decode-time' (instead of
  15687. ;; calling without arguments) so that only `current-time' has
  15688. ;; to be overriden in tests.
  15689. (let ((now (decode-time (current-time))))
  15690. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  15691. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  15692. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  15693. ((equal deltaw "m") (setq month (+ month deltan)))
  15694. ((equal deltaw "y") (setq year (+ year deltan)))))
  15695. ((and wday (not (nth 3 tl)))
  15696. ;; Weekday was given, but no day, so pick that day in the week
  15697. ;; on or after the derived date.
  15698. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  15699. (unless (equal wday wday1)
  15700. (setq day (+ day (% (- wday wday1 -7) 7))))))
  15701. (when (and (boundp 'org-time-was-given)
  15702. (nth 2 tl))
  15703. (setq org-time-was-given t))
  15704. (when (< year 100) (setq year (+ 2000 year)))
  15705. ;; Check of the date is representable
  15706. (if org-read-date-force-compatible-dates
  15707. (progn
  15708. (when (< year 1970)
  15709. (setq year 1970 org-read-date-analyze-forced-year t))
  15710. (when (> year 2037)
  15711. (setq year 2037 org-read-date-analyze-forced-year t)))
  15712. (condition-case nil
  15713. (ignore (encode-time second minute hour day month year))
  15714. (error
  15715. (setq year (nth 5 org-defdecode))
  15716. (setq org-read-date-analyze-forced-year t))))
  15717. (setq org-read-date-analyze-futurep futurep)
  15718. (list second minute hour day month year)))
  15719. (defvar parse-time-weekdays)
  15720. (defun org-read-date-get-relative (s today default)
  15721. "Check string S for special relative date string.
  15722. TODAY and DEFAULT are internal times, for today and for a default.
  15723. Return shift list (N what def-flag)
  15724. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  15725. N is the number of WHATs to shift.
  15726. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  15727. the DEFAULT date rather than TODAY."
  15728. (require 'parse-time)
  15729. (when (and
  15730. (string-match
  15731. (concat
  15732. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  15733. "\\([0-9]+\\)?"
  15734. "\\([hdwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  15735. "\\([ \t]\\|$\\)") s)
  15736. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  15737. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  15738. (string-to-char (substring (match-string 1 s) -1))
  15739. ?+))
  15740. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  15741. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  15742. (what (if (match-end 3) (match-string 3 s) "d"))
  15743. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  15744. (date (if rel default today))
  15745. (wday (nth 6 (decode-time date)))
  15746. delta)
  15747. (if wday1
  15748. (progn
  15749. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  15750. (when (= delta 0) (setq delta 7))
  15751. (when (= dir ?-)
  15752. (setq delta (- delta 7))
  15753. (when (= delta 0) (setq delta -7)))
  15754. (when (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  15755. (list delta "d" rel))
  15756. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  15757. (defun org-order-calendar-date-args (arg1 arg2 arg3)
  15758. "Turn a user-specified date into the internal representation.
  15759. The internal representation needed by the calendar is (month day year).
  15760. This is a wrapper to handle the brain-dead convention in calendar that
  15761. user function argument order change dependent on argument order."
  15762. (if (boundp 'calendar-date-style)
  15763. (cond
  15764. ((eq calendar-date-style 'american)
  15765. (list arg1 arg2 arg3))
  15766. ((eq calendar-date-style 'european)
  15767. (list arg2 arg1 arg3))
  15768. ((eq calendar-date-style 'iso)
  15769. (list arg2 arg3 arg1)))
  15770. (org-no-warnings ;; european-calendar-style is obsolete as of version 23.1
  15771. (if (org-bound-and-true-p european-calendar-style)
  15772. (list arg2 arg1 arg3)
  15773. (list arg1 arg2 arg3)))))
  15774. (defun org-eval-in-calendar (form &optional keepdate)
  15775. "Eval FORM in the calendar window and return to current window.
  15776. When KEEPDATE is non-nil, update `org-ans2' from the cursor date,
  15777. otherwise stick to the current value of `org-ans2'."
  15778. (let ((sf (selected-frame))
  15779. (sw (selected-window)))
  15780. (select-window (get-buffer-window "*Calendar*" t))
  15781. (eval form)
  15782. (when (and (not keepdate) (calendar-cursor-to-date))
  15783. (let* ((date (calendar-cursor-to-date))
  15784. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15785. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  15786. (move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  15787. (select-window sw)
  15788. (org-select-frame-set-input-focus sf)))
  15789. (defun org-calendar-select ()
  15790. "Return to `org-read-date' with the date currently selected.
  15791. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  15792. (interactive)
  15793. (when (calendar-cursor-to-date)
  15794. (let* ((date (calendar-cursor-to-date))
  15795. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15796. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  15797. (when (active-minibuffer-window) (exit-minibuffer))))
  15798. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  15799. "Insert a date stamp for the date given by the internal TIME.
  15800. See `format-time-string' for the format of TIME.
  15801. WITH-HM means use the stamp format that includes the time of the day.
  15802. INACTIVE means use square brackets instead of angular ones, so that the
  15803. stamp will not contribute to the agenda.
  15804. PRE and POST are optional strings to be inserted before and after the
  15805. stamp.
  15806. The command returns the inserted time stamp."
  15807. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  15808. stamp)
  15809. (when inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  15810. (insert-before-markers (or pre ""))
  15811. (when (listp extra)
  15812. (setq extra (car extra))
  15813. (if (and (stringp extra)
  15814. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  15815. (setq extra (format "-%02d:%02d"
  15816. (string-to-number (match-string 1 extra))
  15817. (string-to-number (match-string 2 extra))))
  15818. (setq extra nil)))
  15819. (when extra
  15820. (setq fmt (concat (substring fmt 0 -1) extra (substring fmt -1))))
  15821. (insert-before-markers (setq stamp (format-time-string fmt time)))
  15822. (insert-before-markers (or post ""))
  15823. (setq org-last-inserted-timestamp stamp)))
  15824. (defun org-toggle-time-stamp-overlays ()
  15825. "Toggle the use of custom time stamp formats."
  15826. (interactive)
  15827. (setq org-display-custom-times (not org-display-custom-times))
  15828. (unless org-display-custom-times
  15829. (let ((p (point-min)) (bmp (buffer-modified-p)))
  15830. (while (setq p (next-single-property-change p 'display))
  15831. (when (and (get-text-property p 'display)
  15832. (eq (get-text-property p 'face) 'org-date))
  15833. (remove-text-properties
  15834. p (setq p (next-single-property-change p 'display))
  15835. '(display t))))
  15836. (set-buffer-modified-p bmp)))
  15837. (when (featurep 'xemacs)
  15838. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  15839. (org-restart-font-lock)
  15840. (setq org-table-may-need-update t)
  15841. (if org-display-custom-times
  15842. (message "Time stamps are overlaid with custom format")
  15843. (message "Time stamp overlays removed")))
  15844. (defun org-display-custom-time (beg end)
  15845. "Overlay modified time stamp format over timestamp between BEG and END."
  15846. (let* ((ts (buffer-substring beg end))
  15847. t1 w1 with-hm tf time str w2 (off 0))
  15848. (save-match-data
  15849. (setq t1 (org-parse-time-string ts t))
  15850. (when (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)?\\'" ts)
  15851. (setq off (- (match-end 0) (match-beginning 0)))))
  15852. (setq end (- end off))
  15853. (setq w1 (- end beg)
  15854. with-hm (and (nth 1 t1) (nth 2 t1))
  15855. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  15856. time (org-fix-decoded-time t1)
  15857. str (org-add-props
  15858. (format-time-string
  15859. (substring tf 1 -1) (apply 'encode-time time))
  15860. nil 'mouse-face 'highlight)
  15861. w2 (length str))
  15862. (unless (= w2 w1)
  15863. (add-text-properties (1+ beg) (+ 2 beg)
  15864. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  15865. (if (featurep 'xemacs)
  15866. (progn
  15867. (put-text-property beg end 'invisible t)
  15868. (put-text-property beg end 'end-glyph (make-glyph str)))
  15869. (put-text-property beg end 'display str))))
  15870. (defun org-fix-decoded-time (time)
  15871. "Set 0 instead of nil for the first 6 elements of time.
  15872. Don't touch the rest."
  15873. (let ((n 0))
  15874. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  15875. (define-obsolete-function-alias 'org-days-to-time 'org-time-stamp-to-now "24.4")
  15876. (defun org-time-stamp-to-now (timestamp-string &optional seconds)
  15877. "Difference between TIMESTAMP-STRING and now in days.
  15878. If SECONDS is non-nil, return the difference in seconds."
  15879. (let ((fdiff (if seconds 'org-float-time 'time-to-days)))
  15880. (- (funcall fdiff (org-time-string-to-time timestamp-string))
  15881. (funcall fdiff (current-time)))))
  15882. (defun org-deadline-close (timestamp-string &optional ndays)
  15883. "Is the time in TIMESTAMP-STRING close to the current date?"
  15884. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  15885. (and (< (org-time-stamp-to-now timestamp-string) ndays)
  15886. (not (org-entry-is-done-p))))
  15887. (defun org-get-wdays (ts &optional delay zero-delay)
  15888. "Get the deadline lead time appropriate for timestring TS.
  15889. When DELAY is non-nil, get the delay time for scheduled items
  15890. instead of the deadline lead time. When ZERO-DELAY is non-nil
  15891. and `org-scheduled-delay-days' is 0, enforce 0 as the delay,
  15892. don't try to find the delay cookie in the scheduled timestamp."
  15893. (let ((tv (if delay org-scheduled-delay-days
  15894. org-deadline-warning-days)))
  15895. (cond
  15896. ((or (and delay (< tv 0))
  15897. (and delay zero-delay (<= tv 0))
  15898. (and (not delay) (<= tv 0)))
  15899. ;; Enforce this value no matter what
  15900. (- tv))
  15901. ((string-match "-\\([0-9]+\\)\\([hdwmy]\\)\\(\\'\\|>\\| \\)" ts)
  15902. ;; lead time is specified.
  15903. (floor (* (string-to-number (match-string 1 ts))
  15904. (cdr (assoc (match-string 2 ts)
  15905. '(("d" . 1) ("w" . 7)
  15906. ("m" . 30.4) ("y" . 365.25)
  15907. ("h" . 0.041667)))))))
  15908. ;; go for the default.
  15909. (t tv))))
  15910. (defun org-calendar-select-mouse (ev)
  15911. "Return to `org-read-date' with the date currently selected.
  15912. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  15913. (interactive "e")
  15914. (mouse-set-point ev)
  15915. (when (calendar-cursor-to-date)
  15916. (let* ((date (calendar-cursor-to-date))
  15917. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15918. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  15919. (when (active-minibuffer-window) (exit-minibuffer))))
  15920. (defun org-check-deadlines (ndays)
  15921. "Check if there are any deadlines due or past due.
  15922. A deadline is considered due if it happens within `org-deadline-warning-days'
  15923. days from today's date. If the deadline appears in an entry marked DONE,
  15924. it is not shown. The prefix arg NDAYS can be used to test that many
  15925. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  15926. (interactive "P")
  15927. (let* ((org-warn-days
  15928. (cond
  15929. ((equal ndays '(4)) 100000)
  15930. (ndays (prefix-numeric-value ndays))
  15931. (t (abs org-deadline-warning-days))))
  15932. (case-fold-search nil)
  15933. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  15934. (callback
  15935. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  15936. (message "%d deadlines past-due or due within %d days"
  15937. (org-occur regexp nil callback)
  15938. org-warn-days)))
  15939. (defsubst org-re-timestamp (type)
  15940. "Return a regexp for timestamp TYPE.
  15941. Allowed values for TYPE are:
  15942. all: all timestamps
  15943. active: only active timestamps (<...>)
  15944. inactive: only inactive timestamps ([...])
  15945. scheduled: only scheduled timestamps
  15946. deadline: only deadline timestamps
  15947. closed: only closed time-stamps
  15948. When TYPE is nil, fall back on returning a regexp that matches
  15949. both scheduled and deadline timestamps."
  15950. (cl-case type
  15951. (all org-ts-regexp-both)
  15952. (active org-ts-regexp)
  15953. (inactive org-ts-regexp-inactive)
  15954. (scheduled org-scheduled-time-regexp)
  15955. (deadline org-deadline-time-regexp)
  15956. (closed org-closed-time-regexp)
  15957. (otherwise
  15958. (concat "\\<"
  15959. (regexp-opt (list org-deadline-string org-scheduled-string))
  15960. " *<\\([^>]+\\)>"))))
  15961. (defun org-check-before-date (d)
  15962. "Check if there are deadlines or scheduled entries before date D."
  15963. (interactive (list (org-read-date)))
  15964. (let ((case-fold-search nil)
  15965. (regexp (org-re-timestamp org-ts-type))
  15966. (callback
  15967. `(lambda ()
  15968. (let ((match (match-string 1)))
  15969. (and ,(if (memq org-ts-type '(active inactive all))
  15970. '(eq (org-element-type (org-element-context)) 'timestamp)
  15971. '(org-at-planning-p))
  15972. (time-less-p
  15973. (org-time-string-to-time match)
  15974. (org-time-string-to-time d)))))))
  15975. (message "%d entries before %s"
  15976. (org-occur regexp nil callback)
  15977. d)))
  15978. (defun org-check-after-date (d)
  15979. "Check if there are deadlines or scheduled entries after date D."
  15980. (interactive (list (org-read-date)))
  15981. (let ((case-fold-search nil)
  15982. (regexp (org-re-timestamp org-ts-type))
  15983. (callback
  15984. `(lambda ()
  15985. (let ((match (match-string 1)))
  15986. (and ,(if (memq org-ts-type '(active inactive all))
  15987. '(eq (org-element-type (org-element-context)) 'timestamp)
  15988. '(org-at-planning-p))
  15989. (not (time-less-p
  15990. (org-time-string-to-time match)
  15991. (org-time-string-to-time d))))))))
  15992. (message "%d entries after %s"
  15993. (org-occur regexp nil callback)
  15994. d)))
  15995. (defun org-check-dates-range (start-date end-date)
  15996. "Check for deadlines/scheduled entries between START-DATE and END-DATE."
  15997. (interactive (list (org-read-date nil nil nil "Range starts")
  15998. (org-read-date nil nil nil "Range end")))
  15999. (let ((case-fold-search nil)
  16000. (regexp (org-re-timestamp org-ts-type))
  16001. (callback
  16002. `(lambda ()
  16003. (let ((match (match-string 1)))
  16004. (and
  16005. ,(if (memq org-ts-type '(active inactive all))
  16006. '(eq (org-element-type (org-element-context)) 'timestamp)
  16007. '(org-at-planning-p))
  16008. (not (time-less-p
  16009. (org-time-string-to-time match)
  16010. (org-time-string-to-time start-date)))
  16011. (time-less-p
  16012. (org-time-string-to-time match)
  16013. (org-time-string-to-time end-date)))))))
  16014. (message "%d entries between %s and %s"
  16015. (org-occur regexp nil callback) start-date end-date)))
  16016. (defun org-evaluate-time-range (&optional to-buffer)
  16017. "Evaluate a time range by computing the difference between start and end.
  16018. Normally the result is just printed in the echo area, but with prefix arg
  16019. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  16020. If the time range is actually in a table, the result is inserted into the
  16021. next column.
  16022. For time difference computation, a year is assumed to be exactly 365
  16023. days in order to avoid rounding problems."
  16024. (interactive "P")
  16025. (or
  16026. (org-clock-update-time-maybe)
  16027. (save-excursion
  16028. (unless (org-at-date-range-p t)
  16029. (goto-char (point-at-bol))
  16030. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  16031. (unless (org-at-date-range-p t)
  16032. (user-error "Not at a time-stamp range, and none found in current line")))
  16033. (let* ((ts1 (match-string 1))
  16034. (ts2 (match-string 2))
  16035. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  16036. (match-end (match-end 0))
  16037. (time1 (org-time-string-to-time ts1))
  16038. (time2 (org-time-string-to-time ts2))
  16039. (t1 (org-float-time time1))
  16040. (t2 (org-float-time time2))
  16041. (diff (abs (- t2 t1)))
  16042. (negative (< (- t2 t1) 0))
  16043. ;; (ys (floor (* 365 24 60 60)))
  16044. (ds (* 24 60 60))
  16045. (hs (* 60 60))
  16046. (fy "%dy %dd %02d:%02d")
  16047. (fy1 "%dy %dd")
  16048. (fd "%dd %02d:%02d")
  16049. (fd1 "%dd")
  16050. (fh "%02d:%02d")
  16051. y d h m align)
  16052. (if havetime
  16053. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  16054. y 0
  16055. d (floor (/ diff ds)) diff (mod diff ds)
  16056. h (floor (/ diff hs)) diff (mod diff hs)
  16057. m (floor (/ diff 60)))
  16058. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  16059. y 0
  16060. d (floor (+ (/ diff ds) 0.5))
  16061. h 0 m 0))
  16062. (if (not to-buffer)
  16063. (message "%s" (org-make-tdiff-string y d h m))
  16064. (if (org-at-table-p)
  16065. (progn
  16066. (goto-char match-end)
  16067. (setq align t)
  16068. (and (looking-at " *|") (goto-char (match-end 0))))
  16069. (goto-char match-end))
  16070. (when (looking-at
  16071. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  16072. (replace-match ""))
  16073. (when negative (insert " -"))
  16074. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  16075. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  16076. (insert " " (format fh h m))))
  16077. (when align (org-table-align))
  16078. (message "Time difference inserted")))))
  16079. (defun org-make-tdiff-string (y d h m)
  16080. (let ((fmt "")
  16081. (l nil))
  16082. (when (> y 0)
  16083. (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " "))
  16084. (push y l))
  16085. (when (> d 0)
  16086. (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " "))
  16087. (push d l))
  16088. (when (> h 0)
  16089. (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " "))
  16090. (push h l))
  16091. (when (> m 0)
  16092. (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " "))
  16093. (push m l))
  16094. (apply 'format fmt (nreverse l))))
  16095. (defun org-time-string-to-time (s &optional buffer pos)
  16096. "Convert a timestamp string into internal time."
  16097. (condition-case errdata
  16098. (apply 'encode-time (org-parse-time-string s))
  16099. (error (error "Bad timestamp `%s'%s\nError was: %s"
  16100. s (if (not (and buffer pos))
  16101. ""
  16102. (format-message " at %d in buffer `%s'" pos buffer))
  16103. (cdr errdata)))))
  16104. (defun org-time-string-to-seconds (s)
  16105. "Convert a timestamp string to a number of seconds."
  16106. (org-float-time (org-time-string-to-time s)))
  16107. (org-define-error 'org-diary-sexp-no-match "Unable to match diary sexp")
  16108. (defun org-time-string-to-absolute (s &optional daynr prefer show-all buffer pos)
  16109. "Convert time stamp S to an absolute day number.
  16110. If DAYNR in non-nil, and there is a specifier for a cyclic time
  16111. stamp, get the closest date to DAYNR. If PREFER is
  16112. `past' (respectively `future') return a date past (respectively
  16113. after) or equal to DAYNR.
  16114. POS is the location of time stamp S, as a buffer position in
  16115. BUFFER.
  16116. Diary sexp timestamps are matched against DAYNR, when non-nil.
  16117. If matching fails or DAYNR is nil, `org-diary-sexp-no-match' is
  16118. signalled."
  16119. (cond
  16120. ((string-match "\\`%%\\((.*)\\)" s)
  16121. ;; Sexp timestamp: try to match DAYNR, if available, since we're
  16122. ;; only able to match individual dates. If it fails, raise an
  16123. ;; error.
  16124. (if (and daynr
  16125. (org-diary-sexp-entry
  16126. (match-string 1 s) "" (calendar-gregorian-from-absolute daynr)))
  16127. daynr
  16128. (signal 'org-diary-sexp-no-match (list s))))
  16129. ((and daynr show-all) (org-closest-date s daynr prefer))
  16130. (t (time-to-days
  16131. (condition-case errdata
  16132. (apply #'encode-time (org-parse-time-string s))
  16133. (error (error "Bad timestamp `%s'%s\nError was: %s"
  16134. s
  16135. (if (not (and buffer pos)) ""
  16136. (format-message " at %d in buffer `%s'" pos buffer))
  16137. (cdr errdata))))))))
  16138. (defun org-days-to-iso-week (days)
  16139. "Return the iso week number."
  16140. (require 'cal-iso)
  16141. (car (calendar-iso-from-absolute days)))
  16142. (defun org-small-year-to-year (year)
  16143. "Convert 2-digit years into 4-digit years.
  16144. YEAR is expanded into one of the 30 next years, if possible, or
  16145. into a past one. Any year larger than 99 is returned unchanged."
  16146. (if (>= year 100) year
  16147. (let* ((current (string-to-number (format-time-string "%Y" (current-time))))
  16148. (century (/ current 100))
  16149. (offset (- year (% current 100))))
  16150. (cond ((> offset 30) (+ (* (1- century) 100) year))
  16151. ((> offset -70) (+ (* century 100) year))
  16152. (t (+ (* (1+ century) 100) year))))))
  16153. (defun org-time-from-absolute (d)
  16154. "Return the time corresponding to date D.
  16155. D may be an absolute day number, or a calendar-type list (month day year)."
  16156. (when (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  16157. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  16158. (defun org-calendar-holiday ()
  16159. "List of holidays, for Diary display in Org mode."
  16160. (declare (special date))
  16161. (require 'holidays)
  16162. (let ((hl (calendar-check-holidays date)))
  16163. (and hl (mapconcat #'identity hl "; "))))
  16164. (defun org-diary-sexp-entry (sexp entry d)
  16165. "Process a SEXP diary ENTRY for date D."
  16166. (require 'diary-lib)
  16167. ;; `org-anniversary' and alike expect ENTRY and DATE to be bound
  16168. ;; dynamically.
  16169. (let* ((sexp `(let ((entry ,entry)
  16170. (date ',d))
  16171. ,(car (read-from-string sexp))))
  16172. (result (if calendar-debug-sexp (eval sexp)
  16173. (condition-case nil
  16174. (eval sexp)
  16175. (error
  16176. (beep)
  16177. (message "Bad sexp at line %d in %s: %s"
  16178. (org-current-line)
  16179. (buffer-file-name) sexp)
  16180. (sleep-for 2))))))
  16181. (cond ((stringp result) (split-string result "; "))
  16182. ((and (consp result)
  16183. (not (consp (cdr result)))
  16184. (stringp (cdr result))) (cdr result))
  16185. ((and (consp result)
  16186. (stringp (car result))) result)
  16187. (result entry))))
  16188. (defun org-diary-to-ical-string (frombuf)
  16189. "Get iCalendar entries from diary entries in buffer FROMBUF.
  16190. This uses the icalendar.el library."
  16191. (let* ((tmpdir (if (featurep 'xemacs)
  16192. (temp-directory)
  16193. temporary-file-directory))
  16194. (tmpfile (make-temp-name
  16195. (expand-file-name "orgics" tmpdir)))
  16196. buf rtn b e)
  16197. (with-current-buffer frombuf
  16198. (icalendar-export-region (point-min) (point-max) tmpfile)
  16199. (setq buf (find-buffer-visiting tmpfile))
  16200. (set-buffer buf)
  16201. (goto-char (point-min))
  16202. (when (re-search-forward "^BEGIN:VEVENT" nil t)
  16203. (setq b (match-beginning 0)))
  16204. (goto-char (point-max))
  16205. (when (re-search-backward "^END:VEVENT" nil t)
  16206. (setq e (match-end 0)))
  16207. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  16208. (kill-buffer buf)
  16209. (delete-file tmpfile)
  16210. rtn))
  16211. (defun org-closest-date (start current prefer)
  16212. "Return closest date to CURRENT starting from START.
  16213. CURRENT and START are both time stamps.
  16214. When PREFER is `past', return a date that is either CURRENT or
  16215. past. When PREFER is `future', return a date that is either
  16216. CURRENT or future.
  16217. Only time stamps with a repeater are modified. Any other time
  16218. stamp stay unchanged. In any case, return value is an absolute
  16219. day number."
  16220. (if (not (string-match "\\+\\([0-9]+\\)\\([hdwmy]\\)" start))
  16221. ;; No repeater. Do not shift time stamp.
  16222. (time-to-days (apply #'encode-time (org-parse-time-string start)))
  16223. (let ((value (string-to-number (match-string 1 start)))
  16224. (type (match-string 2 start)))
  16225. (if (= 0 value)
  16226. ;; Repeater with a 0-value is considered as void.
  16227. (time-to-days (apply #'encode-time (org-parse-time-string start)))
  16228. (let* ((base (org-date-to-gregorian start))
  16229. (target (org-date-to-gregorian current))
  16230. (sday (calendar-absolute-from-gregorian base))
  16231. (cday (calendar-absolute-from-gregorian target))
  16232. n1 n2)
  16233. ;; If START is already past CURRENT, just return START.
  16234. (if (<= cday sday) sday
  16235. ;; Compute closest date before (N1) and closest date past
  16236. ;; (N2) CURRENT.
  16237. (pcase type
  16238. ("h"
  16239. (let ((missing-hours
  16240. (mod (+ (- (* 24 (- cday sday))
  16241. (nth 2 (org-parse-time-string start)))
  16242. org-extend-today-until)
  16243. value)))
  16244. (setf n1 (if (= missing-hours 0) cday
  16245. (- cday (1+ (/ missing-hours 24)))))
  16246. (setf n2 (+ cday (/ (- value missing-hours) 24)))))
  16247. ((or "d" "w")
  16248. (let ((value (if (equal type "w") (* 7 value) value)))
  16249. (setf n1 (+ sday (* value (/ (- cday sday) value))))
  16250. (setf n2 (+ n1 value))))
  16251. ("m"
  16252. (let* ((add-months
  16253. (lambda (d n)
  16254. ;; Add N months to gregorian date D, i.e.,
  16255. ;; a list (MONTH DAY YEAR). Return a valid
  16256. ;; gregorian date.
  16257. (let ((m (+ (nth 0 d) n)))
  16258. (list (mod m 12)
  16259. (nth 1 d)
  16260. (+ (/ m 12) (nth 2 d))))))
  16261. (months ; Complete months to TARGET.
  16262. (* (/ (+ (* 12 (- (nth 2 target) (nth 2 base)))
  16263. (- (nth 0 target) (nth 0 base))
  16264. ;; If START's day is greater than
  16265. ;; TARGET's, remove incomplete month.
  16266. (if (> (nth 1 target) (nth 1 base)) 0 -1))
  16267. value)
  16268. value))
  16269. (before (funcall add-months base months)))
  16270. (setf n1 (calendar-absolute-from-gregorian before))
  16271. (setf n2
  16272. (calendar-absolute-from-gregorian
  16273. (funcall add-months before value)))))
  16274. (_
  16275. (let* ((d (nth 1 base))
  16276. (m (nth 0 base))
  16277. (y (nth 2 base))
  16278. (years ; Complete years to TARGET.
  16279. (* (/ (- (nth 2 target)
  16280. y
  16281. ;; If START's month and day are
  16282. ;; greater than TARGET's, remove
  16283. ;; incomplete year.
  16284. (if (or (> (nth 0 target) m)
  16285. (and (= (nth 0 target) m)
  16286. (> (nth 1 target) d)))
  16287. 0
  16288. 1))
  16289. value)
  16290. value))
  16291. (before (list m d (+ y years))))
  16292. (setf n1 (calendar-absolute-from-gregorian before))
  16293. (setf n2 (calendar-absolute-from-gregorian
  16294. (list m d (+ (nth 2 before) value)))))))
  16295. ;; Handle PREFER parameter, if any.
  16296. (cond
  16297. ((eq prefer 'past) (if (= cday n2) n2 n1))
  16298. ((eq prefer 'future) (if (= cday n1) n1 n2))
  16299. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))))))))
  16300. (defun org-date-to-gregorian (d)
  16301. "Turn any specification of date D into a Gregorian date for the calendar."
  16302. (cond ((integerp d) (calendar-gregorian-from-absolute d))
  16303. ((and (listp d) (= (length d) 3)) d)
  16304. ((stringp d)
  16305. (let ((d (org-parse-time-string d)))
  16306. (list (nth 4 d) (nth 3 d) (nth 5 d))))
  16307. ((listp d) (list (nth 4 d) (nth 3 d) (nth 5 d)))))
  16308. (defun org-parse-time-string (s &optional nodefault)
  16309. "Parse the standard Org-mode time string.
  16310. This should be a lot faster than the normal `parse-time-string'.
  16311. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  16312. hour and minute fields will be nil if not given."
  16313. (cond ((string-match org-ts-regexp0 s)
  16314. (list 0
  16315. (when (or (match-beginning 8) (not nodefault))
  16316. (string-to-number (or (match-string 8 s) "0")))
  16317. (when (or (match-beginning 7) (not nodefault))
  16318. (string-to-number (or (match-string 7 s) "0")))
  16319. (string-to-number (match-string 4 s))
  16320. (string-to-number (match-string 3 s))
  16321. (string-to-number (match-string 2 s))
  16322. nil nil nil))
  16323. ((string-match "^<[^>]+>$" s)
  16324. (decode-time (seconds-to-time (org-matcher-time s))))
  16325. (t (error "Not a standard Org-mode time string: %s" s))))
  16326. (defun org-timestamp-up (&optional arg)
  16327. "Increase the date item at the cursor by one.
  16328. If the cursor is on the year, change the year. If it is on the month,
  16329. the day or the time, change that.
  16330. With prefix ARG, change by that many units."
  16331. (interactive "p")
  16332. (org-timestamp-change (prefix-numeric-value arg) nil 'updown))
  16333. (defun org-timestamp-down (&optional arg)
  16334. "Decrease the date item at the cursor by one.
  16335. If the cursor is on the year, change the year. If it is on the month,
  16336. the day or the time, change that.
  16337. With prefix ARG, change by that many units."
  16338. (interactive "p")
  16339. (org-timestamp-change (- (prefix-numeric-value arg)) nil 'updown))
  16340. (defun org-timestamp-up-day (&optional arg)
  16341. "Increase the date in the time stamp by one day.
  16342. With prefix ARG, change that many days."
  16343. (interactive "p")
  16344. (if (and (not (org-at-timestamp-p t))
  16345. (org-at-heading-p))
  16346. (org-todo 'up)
  16347. (org-timestamp-change (prefix-numeric-value arg) 'day 'updown)))
  16348. (defun org-timestamp-down-day (&optional arg)
  16349. "Decrease the date in the time stamp by one day.
  16350. With prefix ARG, change that many days."
  16351. (interactive "p")
  16352. (if (and (not (org-at-timestamp-p t))
  16353. (org-at-heading-p))
  16354. (org-todo 'down)
  16355. (org-timestamp-change (- (prefix-numeric-value arg)) 'day) 'updown))
  16356. (defun org-at-timestamp-p (&optional inactive-ok)
  16357. "Non-nil if point is inside a timestamp.
  16358. When optional argument INACTIVE-OK is non-nil, also consider
  16359. inactive timestamps.
  16360. When this function returns a non-nil value, match data is set
  16361. according to `org-ts-regexp3' or `org-ts-regexp2', depending on
  16362. INACTIVE-OK."
  16363. (interactive)
  16364. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  16365. (pos (point))
  16366. (ans (or (looking-at tsr)
  16367. (save-excursion
  16368. (skip-chars-backward "^[<\n\r\t")
  16369. (when (> (point) (point-min)) (backward-char 1))
  16370. (and (looking-at tsr)
  16371. (> (- (match-end 0) pos) -1))))))
  16372. (and ans
  16373. (boundp 'org-ts-what)
  16374. (setq org-ts-what
  16375. (cond
  16376. ((= pos (match-beginning 0)) 'bracket)
  16377. ;; Point is considered to be "on the bracket" whether
  16378. ;; it's really on it or right after it.
  16379. ((= pos (1- (match-end 0))) 'bracket)
  16380. ((= pos (match-end 0)) 'after)
  16381. ((org-pos-in-match-range pos 2) 'year)
  16382. ((org-pos-in-match-range pos 3) 'month)
  16383. ((org-pos-in-match-range pos 7) 'hour)
  16384. ((org-pos-in-match-range pos 8) 'minute)
  16385. ((or (org-pos-in-match-range pos 4)
  16386. (org-pos-in-match-range pos 5)) 'day)
  16387. ((and (> pos (or (match-end 8) (match-end 5)))
  16388. (< pos (match-end 0)))
  16389. (- pos (or (match-end 8) (match-end 5))))
  16390. (t 'day))))
  16391. ans))
  16392. (defun org-toggle-timestamp-type ()
  16393. "Toggle the type (<active> or [inactive]) of a time stamp."
  16394. (interactive)
  16395. (when (org-at-timestamp-p t)
  16396. (let ((beg (match-beginning 0)) (end (match-end 0))
  16397. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  16398. (save-excursion
  16399. (goto-char beg)
  16400. (while (re-search-forward "[][<>]" end t)
  16401. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  16402. t t)))
  16403. (message "Timestamp is now %sactive"
  16404. (if (equal (char-after beg) ?<) "" "in")))))
  16405. (defun org-at-clock-log-p nil
  16406. "Is the cursor on the clock log line?"
  16407. (save-excursion
  16408. (move-beginning-of-line 1)
  16409. (looking-at org-clock-line-re)))
  16410. (defvar org-clock-history) ; defined in org-clock.el
  16411. (defvar org-clock-adjust-closest nil) ; defined in org-clock.el
  16412. (defun org-timestamp-change (n &optional what updown suppress-tmp-delay)
  16413. "Change the date in the time stamp at point.
  16414. The date will be changed by N times WHAT. WHAT can be `day', `month',
  16415. `year', `minute', `second'. If WHAT is not given, the cursor position
  16416. in the timestamp determines what will be changed.
  16417. When SUPPRESS-TMP-DELAY is non-nil, suppress delays like \"--2d\"."
  16418. (let ((origin (point)) origin-cat
  16419. with-hm inactive
  16420. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  16421. org-ts-what
  16422. extra rem
  16423. ts time time0 fixnext clrgx)
  16424. (unless (org-at-timestamp-p t)
  16425. (user-error "Not at a timestamp"))
  16426. (if (and (not what) (eq org-ts-what 'bracket))
  16427. (org-toggle-timestamp-type)
  16428. ;; Point isn't on brackets. Remember the part of the time-stamp
  16429. ;; the point was in. Indeed, size of time-stamps may change,
  16430. ;; but point must be kept in the same category nonetheless.
  16431. (setq origin-cat org-ts-what)
  16432. (when (and (not what) (not (eq org-ts-what 'day))
  16433. org-display-custom-times
  16434. (get-text-property (point) 'display)
  16435. (not (get-text-property (1- (point)) 'display)))
  16436. (setq org-ts-what 'day))
  16437. (setq org-ts-what (or what org-ts-what)
  16438. inactive (= (char-after (match-beginning 0)) ?\[)
  16439. ts (match-string 0))
  16440. (replace-match "")
  16441. (when (string-match
  16442. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?-?[-+][0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)*\\)[]>]"
  16443. ts)
  16444. (setq extra (match-string 1 ts))
  16445. (when suppress-tmp-delay
  16446. (setq extra (replace-regexp-in-string " --[0-9]+[hdwmy]" "" extra))))
  16447. (when (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  16448. (setq with-hm t))
  16449. (setq time0 (org-parse-time-string ts))
  16450. (when (and updown
  16451. (eq org-ts-what 'minute)
  16452. (not current-prefix-arg))
  16453. ;; This looks like s-up and s-down. Change by one rounding step.
  16454. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  16455. (unless (= 0 (setq rem (% (nth 1 time0) dm)))
  16456. (setcar (cdr time0) (+ (nth 1 time0)
  16457. (if (> n 0) (- rem) (- dm rem))))))
  16458. (setq time
  16459. (encode-time (or (car time0) 0)
  16460. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  16461. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  16462. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  16463. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  16464. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  16465. (nthcdr 6 time0)))
  16466. (when (and (member org-ts-what '(hour minute))
  16467. extra
  16468. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  16469. (setq extra (org-modify-ts-extra
  16470. extra
  16471. (if (eq org-ts-what 'hour) 2 5)
  16472. n dm)))
  16473. (when (integerp org-ts-what)
  16474. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  16475. (when (eq what 'calendar)
  16476. (let ((cal-date (org-get-date-from-calendar)))
  16477. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  16478. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  16479. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  16480. (setcar time0 (or (car time0) 0))
  16481. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  16482. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  16483. (setq time (apply 'encode-time time0))))
  16484. ;; Insert the new time-stamp, and ensure point stays in the same
  16485. ;; category as before (i.e. not after the last position in that
  16486. ;; category).
  16487. (let ((pos (point)))
  16488. ;; Stay before inserted string. `save-excursion' is of no use.
  16489. (setq org-last-changed-timestamp
  16490. (org-insert-time-stamp time with-hm inactive nil nil extra))
  16491. (goto-char pos))
  16492. (save-match-data
  16493. (looking-at org-ts-regexp3)
  16494. (goto-char (cond
  16495. ;; `day' category ends before `hour' if any, or at
  16496. ;; the end of the day name.
  16497. ((eq origin-cat 'day)
  16498. (min (or (match-beginning 7) (1- (match-end 5))) origin))
  16499. ((eq origin-cat 'hour) (min (match-end 7) origin))
  16500. ((eq origin-cat 'minute) (min (1- (match-end 8)) origin))
  16501. ((integerp origin-cat) (min (1- (match-end 0)) origin))
  16502. ;; `year' and `month' have both fixed size: point
  16503. ;; couldn't have moved into another part.
  16504. (t origin))))
  16505. ;; Update clock if on a CLOCK line.
  16506. (org-clock-update-time-maybe)
  16507. ;; Maybe adjust the closest clock in `org-clock-history'
  16508. (when org-clock-adjust-closest
  16509. (if (not (and (org-at-clock-log-p)
  16510. (< 1 (length (delq nil (mapcar 'marker-position
  16511. org-clock-history))))))
  16512. (message "No clock to adjust")
  16513. (cond ((save-excursion ; fix previous clock?
  16514. (re-search-backward org-ts-regexp0 nil t)
  16515. (org-looking-back (concat org-clock-string " \\[")
  16516. (line-beginning-position)))
  16517. (setq fixnext 1 clrgx (concat org-ts-regexp0 "\\] =>.*$")))
  16518. ((save-excursion ; fix next clock?
  16519. (re-search-backward org-ts-regexp0 nil t)
  16520. (looking-at (concat org-ts-regexp0 "\\] =>")))
  16521. (setq fixnext -1 clrgx (concat org-clock-string " \\[" org-ts-regexp0))))
  16522. (save-window-excursion
  16523. ;; Find closest clock to point, adjust the previous/next one in history
  16524. (let* ((p (save-excursion (org-back-to-heading t)))
  16525. (cl (mapcar (lambda(c) (abs (- (marker-position c) p))) org-clock-history))
  16526. (clfixnth
  16527. (+ fixnext (- (length cl) (or (length (member (apply 'min cl) cl)) 100))))
  16528. (clfixpos (unless (> 0 clfixnth) (nth clfixnth org-clock-history))))
  16529. (if (not clfixpos)
  16530. (message "No clock to adjust")
  16531. (save-excursion
  16532. (org-goto-marker-or-bmk clfixpos)
  16533. (org-show-subtree)
  16534. (when (re-search-forward clrgx nil t)
  16535. (goto-char (match-beginning 1))
  16536. (let (org-clock-adjust-closest)
  16537. (org-timestamp-change n org-ts-what updown))
  16538. (message "Clock adjusted in %s for heading: %s"
  16539. (file-name-nondirectory (buffer-file-name))
  16540. (org-get-heading t t)))))))))
  16541. ;; Try to recenter the calendar window, if any.
  16542. (when (and org-calendar-follow-timestamp-change
  16543. (get-buffer-window "*Calendar*" t)
  16544. (memq org-ts-what '(day month year)))
  16545. (org-recenter-calendar (time-to-days time))))))
  16546. (defun org-modify-ts-extra (s pos n dm)
  16547. "Change the different parts of the lead-time and repeat fields in timestamp."
  16548. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  16549. ng h m new rem)
  16550. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  16551. (cond
  16552. ((or (org-pos-in-match-range pos 2)
  16553. (org-pos-in-match-range pos 3))
  16554. (setq m (string-to-number (match-string 3 s))
  16555. h (string-to-number (match-string 2 s)))
  16556. (if (org-pos-in-match-range pos 2)
  16557. (setq h (+ h n))
  16558. (setq n (* dm (org-no-warnings (signum n))))
  16559. (unless (= 0 (setq rem (% m dm)))
  16560. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  16561. (setq m (+ m n)))
  16562. (when (< m 0) (setq m (+ m 60) h (1- h)))
  16563. (when (> m 59) (setq m (- m 60) h (1+ h)))
  16564. (setq h (mod h 24))
  16565. (setq ng 1 new (format "-%02d:%02d" h m)))
  16566. ((org-pos-in-match-range pos 6)
  16567. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  16568. ((org-pos-in-match-range pos 5)
  16569. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  16570. ((org-pos-in-match-range pos 9)
  16571. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  16572. ((org-pos-in-match-range pos 8)
  16573. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  16574. (when ng
  16575. (setq s (concat
  16576. (substring s 0 (match-beginning ng))
  16577. new
  16578. (substring s (match-end ng))))))
  16579. s))
  16580. (defun org-recenter-calendar (d)
  16581. "If the calendar is visible, recenter it to date D."
  16582. (let ((cwin (get-buffer-window "*Calendar*" t)))
  16583. (when cwin
  16584. (let ((calendar-move-hook nil))
  16585. (with-selected-window cwin
  16586. (calendar-goto-date
  16587. (if (listp d) d (calendar-gregorian-from-absolute d))))))))
  16588. (defun org-goto-calendar (&optional arg)
  16589. "Go to the Emacs calendar at the current date.
  16590. If there is a time stamp in the current line, go to that date.
  16591. A prefix ARG can be used to force the current date."
  16592. (interactive "P")
  16593. (let ((tsr org-ts-regexp) diff
  16594. (calendar-move-hook nil)
  16595. (calendar-view-holidays-initially-flag nil)
  16596. (calendar-view-diary-initially-flag nil))
  16597. (when (or (org-at-timestamp-p)
  16598. (save-excursion
  16599. (beginning-of-line 1)
  16600. (looking-at (concat ".*" tsr))))
  16601. (let ((d1 (time-to-days (current-time)))
  16602. (d2 (time-to-days
  16603. (org-time-string-to-time (match-string 1)))))
  16604. (setq diff (- d2 d1))))
  16605. (calendar)
  16606. (calendar-goto-today)
  16607. (when (and diff (not arg)) (calendar-forward-day diff))))
  16608. (defun org-get-date-from-calendar ()
  16609. "Return a list (month day year) of date at point in calendar."
  16610. (with-current-buffer "*Calendar*"
  16611. (save-match-data
  16612. (calendar-cursor-to-date))))
  16613. (defun org-date-from-calendar ()
  16614. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  16615. If there is already a time stamp at the cursor position, update it."
  16616. (interactive)
  16617. (if (org-at-timestamp-p t)
  16618. (org-timestamp-change 0 'calendar)
  16619. (let ((cal-date (org-get-date-from-calendar)))
  16620. (org-insert-time-stamp
  16621. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  16622. (defcustom org-effort-durations
  16623. `(("min" . 1)
  16624. ("h" . 60)
  16625. ("d" . ,(* 60 8))
  16626. ("w" . ,(* 60 8 5))
  16627. ("m" . ,(* 60 8 5 4))
  16628. ("y" . ,(* 60 8 5 40)))
  16629. "Conversion factor to minutes for an effort modifier.
  16630. Each entry has the form (MODIFIER . MINUTES).
  16631. In an effort string, a number followed by MODIFIER is multiplied
  16632. by the specified number of MINUTES to obtain an effort in
  16633. minutes.
  16634. For example, if the value of this variable is ((\"hours\" . 60)), then an
  16635. effort string \"2hours\" is equivalent to 120 minutes."
  16636. :group 'org-agenda
  16637. :version "25.1"
  16638. :package-version '(Org . "8.3")
  16639. :type '(alist :key-type (string :tag "Modifier")
  16640. :value-type (number :tag "Minutes")))
  16641. (defun org-minutes-to-clocksum-string (m)
  16642. "Format number of minutes as a clocksum string.
  16643. The format is determined by `org-time-clocksum-format',
  16644. `org-time-clocksum-use-fractional' and
  16645. `org-time-clocksum-fractional-format' and
  16646. `org-time-clocksum-use-effort-durations'."
  16647. (let ((clocksum "")
  16648. (m (round m)) ; Don't allow fractions of minutes
  16649. h d w mo y fmt n)
  16650. (setq h (if org-time-clocksum-use-effort-durations
  16651. (cdr (assoc "h" org-effort-durations)) 60)
  16652. d (if org-time-clocksum-use-effort-durations
  16653. (/ (cdr (assoc "d" org-effort-durations)) h) 24)
  16654. w (if org-time-clocksum-use-effort-durations
  16655. (/ (cdr (assoc "w" org-effort-durations)) (* d h)) 7)
  16656. mo (if org-time-clocksum-use-effort-durations
  16657. (/ (cdr (assoc "m" org-effort-durations)) (* d h)) 30)
  16658. y (if org-time-clocksum-use-effort-durations
  16659. (/ (cdr (assoc "y" org-effort-durations)) (* d h)) 365))
  16660. ;; fractional format
  16661. (if org-time-clocksum-use-fractional
  16662. (cond
  16663. ;; single format string
  16664. ((stringp org-time-clocksum-fractional-format)
  16665. (format org-time-clocksum-fractional-format (/ m (float h))))
  16666. ;; choice of fractional formats for different time units
  16667. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :years))
  16668. (> (/ (truncate m) (* y d h)) 0))
  16669. (format fmt (/ m (* y d (float h)))))
  16670. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :months))
  16671. (> (/ (truncate m) (* mo d h)) 0))
  16672. (format fmt (/ m (* mo d (float h)))))
  16673. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :weeks))
  16674. (> (/ (truncate m) (* w d h)) 0))
  16675. (format fmt (/ m (* w d (float h)))))
  16676. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :days))
  16677. (> (/ (truncate m) (* d h)) 0))
  16678. (format fmt (/ m (* d (float h)))))
  16679. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :hours))
  16680. (> (/ (truncate m) h) 0))
  16681. (format fmt (/ m (float h))))
  16682. ((setq fmt (plist-get org-time-clocksum-fractional-format :minutes))
  16683. (format fmt m))
  16684. ;; fall back to smallest time unit with a format
  16685. ((setq fmt (plist-get org-time-clocksum-fractional-format :hours))
  16686. (format fmt (/ m (float h))))
  16687. ((setq fmt (plist-get org-time-clocksum-fractional-format :days))
  16688. (format fmt (/ m (* d (float h)))))
  16689. ((setq fmt (plist-get org-time-clocksum-fractional-format :weeks))
  16690. (format fmt (/ m (* w d (float h)))))
  16691. ((setq fmt (plist-get org-time-clocksum-fractional-format :months))
  16692. (format fmt (/ m (* mo d (float h)))))
  16693. ((setq fmt (plist-get org-time-clocksum-fractional-format :years))
  16694. (format fmt (/ m (* y d (float h))))))
  16695. ;; standard (non-fractional) format, with single format string
  16696. (if (stringp org-time-clocksum-format)
  16697. (format org-time-clocksum-format (setq n (/ m h)) (- m (* h n)))
  16698. ;; separate formats components
  16699. (and (setq fmt (plist-get org-time-clocksum-format :years))
  16700. (or (> (setq n (/ (truncate m) (* y d h))) 0)
  16701. (plist-get org-time-clocksum-format :require-years))
  16702. (setq clocksum (concat clocksum (format fmt n))
  16703. m (- m (* n y d h))))
  16704. (and (setq fmt (plist-get org-time-clocksum-format :months))
  16705. (or (> (setq n (/ (truncate m) (* mo d h))) 0)
  16706. (plist-get org-time-clocksum-format :require-months))
  16707. (setq clocksum (concat clocksum (format fmt n))
  16708. m (- m (* n mo d h))))
  16709. (and (setq fmt (plist-get org-time-clocksum-format :weeks))
  16710. (or (> (setq n (/ (truncate m) (* w d h))) 0)
  16711. (plist-get org-time-clocksum-format :require-weeks))
  16712. (setq clocksum (concat clocksum (format fmt n))
  16713. m (- m (* n w d h))))
  16714. (and (setq fmt (plist-get org-time-clocksum-format :days))
  16715. (or (> (setq n (/ (truncate m) (* d h))) 0)
  16716. (plist-get org-time-clocksum-format :require-days))
  16717. (setq clocksum (concat clocksum (format fmt n))
  16718. m (- m (* n d h))))
  16719. (and (setq fmt (plist-get org-time-clocksum-format :hours))
  16720. (or (> (setq n (/ (truncate m) h)) 0)
  16721. (plist-get org-time-clocksum-format :require-hours))
  16722. (setq clocksum (concat clocksum (format fmt n))
  16723. m (- m (* n h))))
  16724. (and (setq fmt (plist-get org-time-clocksum-format :minutes))
  16725. (or (> m 0) (plist-get org-time-clocksum-format :require-minutes))
  16726. (setq clocksum (concat clocksum (format fmt m))))
  16727. ;; return formatted time duration
  16728. clocksum))))
  16729. (defalias 'org-minutes-to-hh:mm-string 'org-minutes-to-clocksum-string)
  16730. (make-obsolete 'org-minutes-to-hh:mm-string 'org-minutes-to-clocksum-string
  16731. "Org mode version 8.0")
  16732. (defun org-hours-to-clocksum-string (n)
  16733. (org-minutes-to-clocksum-string (* n 60)))
  16734. (defun org-hh:mm-string-to-minutes (s)
  16735. "Convert a string H:MM to a number of minutes.
  16736. If the string is just a number, interpret it as minutes.
  16737. In fact, the first hh:mm or number in the string will be taken,
  16738. there can be extra stuff in the string.
  16739. If no number is found, the return value is 0."
  16740. (cond
  16741. ((integerp s) s)
  16742. ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  16743. (+ (* (string-to-number (match-string 1 s)) 60)
  16744. (string-to-number (match-string 2 s))))
  16745. ((string-match "\\([0-9]+\\)" s)
  16746. (string-to-number (match-string 1 s)))
  16747. (t 0)))
  16748. (defcustom org-image-actual-width t
  16749. "Should we use the actual width of images when inlining them?
  16750. When set to t, always use the image width.
  16751. When set to a number, use imagemagick (when available) to set
  16752. the image's width to this value.
  16753. When set to a number in a list, try to get the width from any
  16754. #+ATTR.* keyword if it matches a width specification like
  16755. #+ATTR_HTML: :width 300px
  16756. and fall back on that number if none is found.
  16757. When set to nil, try to get the width from an #+ATTR.* keyword
  16758. and fall back on the original width if none is found.
  16759. This requires Emacs >= 24.1, build with imagemagick support."
  16760. :group 'org-appearance
  16761. :version "24.4"
  16762. :package-version '(Org . "8.0")
  16763. :type '(choice
  16764. (const :tag "Use the image width" t)
  16765. (integer :tag "Use a number of pixels")
  16766. (list :tag "Use #+ATTR* or a number of pixels" (integer))
  16767. (const :tag "Use #+ATTR* or don't resize" nil)))
  16768. (defcustom org-agenda-inhibit-startup nil
  16769. "Inhibit startup when preparing agenda buffers.
  16770. When this variable is t, the initialization of the Org agenda
  16771. buffers is inhibited: e.g. the visibility state is not set, the
  16772. tables are not re-aligned, etc."
  16773. :type 'boolean
  16774. :version "24.3"
  16775. :group 'org-agenda)
  16776. (define-obsolete-variable-alias
  16777. 'org-agenda-ignore-drawer-properties
  16778. 'org-agenda-ignore-properties "25.1")
  16779. (defcustom org-agenda-ignore-properties nil
  16780. "Avoid updating text properties when building the agenda.
  16781. Properties are used to prepare buffers for effort estimates,
  16782. appointments, statistics and subtree-local categories.
  16783. If you don't use these in the agenda, you can add them to this
  16784. list and agenda building will be a bit faster.
  16785. The value is a list, with zero or more of the symbols `effort', `appt',
  16786. `stats' or `category'."
  16787. :type '(set :greedy t
  16788. (const effort)
  16789. (const appt)
  16790. (const stats)
  16791. (const category))
  16792. :version "25.1"
  16793. :package-version '(Org . "8.3")
  16794. :group 'org-agenda)
  16795. (defun org-duration-string-to-minutes (s &optional output-to-string)
  16796. "Convert a duration string S to minutes.
  16797. A bare number is interpreted as minutes, modifiers can be set by
  16798. customizing `org-effort-durations' (which see).
  16799. Entries containing a colon are interpreted as H:MM by
  16800. `org-hh:mm-string-to-minutes'."
  16801. (let ((result 0)
  16802. (re (concat "\\([0-9.]+\\) *\\("
  16803. (regexp-opt (mapcar 'car org-effort-durations))
  16804. "\\)")))
  16805. (while (string-match re s)
  16806. (cl-incf result (* (cdr (assoc (match-string 2 s) org-effort-durations))
  16807. (string-to-number (match-string 1 s))))
  16808. (setq s (replace-match "" nil t s)))
  16809. (setq result (floor result))
  16810. (cl-incf result (org-hh:mm-string-to-minutes s))
  16811. (if output-to-string (number-to-string result) result)))
  16812. ;;;; Files
  16813. (defun org-save-all-org-buffers ()
  16814. "Save all Org-mode buffers without user confirmation."
  16815. (interactive)
  16816. (message "Saving all Org-mode buffers...")
  16817. (save-some-buffers t (lambda () (derived-mode-p 'org-mode)))
  16818. (when (featurep 'org-id) (org-id-locations-save))
  16819. (message "Saving all Org-mode buffers... done"))
  16820. (defun org-revert-all-org-buffers ()
  16821. "Revert all Org-mode buffers.
  16822. Prompt for confirmation when there are unsaved changes.
  16823. Be sure you know what you are doing before letting this function
  16824. overwrite your changes.
  16825. This function is useful in a setup where one tracks org files
  16826. with a version control system, to revert on one machine after pulling
  16827. changes from another. I believe the procedure must be like this:
  16828. 1. M-x org-save-all-org-buffers
  16829. 2. Pull changes from the other machine, resolve conflicts
  16830. 3. M-x org-revert-all-org-buffers"
  16831. (interactive)
  16832. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  16833. (user-error "Abort"))
  16834. (save-excursion
  16835. (save-window-excursion
  16836. (dolist (b (buffer-list))
  16837. (when (and (with-current-buffer b (derived-mode-p 'org-mode))
  16838. (with-current-buffer b buffer-file-name))
  16839. (org-pop-to-buffer-same-window b)
  16840. (revert-buffer t 'no-confirm)))
  16841. (when (and (featurep 'org-id) org-id-track-globally)
  16842. (org-id-locations-load)))))
  16843. ;;;; Agenda files
  16844. ;;;###autoload
  16845. (defun org-switchb (&optional arg)
  16846. "Switch between Org buffers.
  16847. With one prefix argument, restrict available buffers to files.
  16848. With two prefix arguments, restrict available buffers to agenda files.
  16849. Defaults to `iswitchb' for buffer name completion.
  16850. Set `org-completion-use-ido' to make it use ido instead."
  16851. (interactive "P")
  16852. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  16853. ((equal arg '(16)) (org-buffer-list 'agenda))
  16854. (t (org-buffer-list))))
  16855. (org-completion-use-iswitchb org-completion-use-iswitchb)
  16856. (org-completion-use-ido org-completion-use-ido))
  16857. (unless (or org-completion-use-ido org-completion-use-iswitchb)
  16858. (setq org-completion-use-iswitchb t))
  16859. (org-pop-to-buffer-same-window
  16860. (completing-read "Org buffer: "
  16861. (mapcar #'list (mapcar #'buffer-name blist))
  16862. nil t))))
  16863. ;;; Define some older names previously used for this functionality
  16864. ;;;###autoload
  16865. (defalias 'org-ido-switchb 'org-switchb)
  16866. ;;;###autoload
  16867. (defalias 'org-iswitchb 'org-switchb)
  16868. (defun org-buffer-list (&optional predicate exclude-tmp)
  16869. "Return a list of Org buffers.
  16870. PREDICATE can be `export', `files' or `agenda'.
  16871. export restrict the list to Export buffers.
  16872. files restrict the list to buffers visiting Org files.
  16873. agenda restrict the list to buffers visiting agenda files.
  16874. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  16875. (let* ((bfn nil)
  16876. (agenda-files (and (eq predicate 'agenda)
  16877. (mapcar 'file-truename (org-agenda-files t))))
  16878. (filter
  16879. (cond
  16880. ((eq predicate 'files)
  16881. (lambda (b) (with-current-buffer b (derived-mode-p 'org-mode))))
  16882. ((eq predicate 'export)
  16883. (lambda (b) (string-match "\\*Org .*Export" (buffer-name b))))
  16884. ((eq predicate 'agenda)
  16885. (lambda (b)
  16886. (with-current-buffer b
  16887. (and (derived-mode-p 'org-mode)
  16888. (setq bfn (buffer-file-name b))
  16889. (member (file-truename bfn) agenda-files)))))
  16890. (t (lambda (b) (with-current-buffer b
  16891. (or (derived-mode-p 'org-mode)
  16892. (string-match "\\*Org .*Export"
  16893. (buffer-name b)))))))))
  16894. (delq nil
  16895. (mapcar
  16896. (lambda(b)
  16897. (if (and (funcall filter b)
  16898. (or (not exclude-tmp)
  16899. (not (string-match "tmp" (buffer-name b)))))
  16900. b
  16901. nil))
  16902. (buffer-list)))))
  16903. (defun org-agenda-files (&optional unrestricted archives)
  16904. "Get the list of agenda files.
  16905. Optional UNRESTRICTED means return the full list even if a restriction
  16906. is currently in place.
  16907. When ARCHIVES is t, include all archive files that are really being
  16908. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  16909. `org-agenda-archives-mode' is t."
  16910. (let ((files
  16911. (cond
  16912. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  16913. ((stringp org-agenda-files) (org-read-agenda-file-list))
  16914. ((listp org-agenda-files) org-agenda-files)
  16915. (t (error "Invalid value of `org-agenda-files'")))))
  16916. (setq files (apply 'append
  16917. (mapcar (lambda (f)
  16918. (if (file-directory-p f)
  16919. (directory-files
  16920. f t org-agenda-file-regexp)
  16921. (list f)))
  16922. files)))
  16923. (when org-agenda-skip-unavailable-files
  16924. (setq files (delq nil
  16925. (mapcar (function
  16926. (lambda (file)
  16927. (and (file-readable-p file) file)))
  16928. files))))
  16929. (when (or (eq archives t)
  16930. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  16931. (setq files (org-add-archive-files files)))
  16932. files))
  16933. (defun org-agenda-file-p (&optional file)
  16934. "Return non-nil, if FILE is an agenda file.
  16935. If FILE is omitted, use the file associated with the current
  16936. buffer."
  16937. (let ((fname (or file (buffer-file-name))))
  16938. (and fname
  16939. (member (file-truename fname)
  16940. (mapcar #'file-truename (org-agenda-files t))))))
  16941. (defun org-edit-agenda-file-list ()
  16942. "Edit the list of agenda files.
  16943. Depending on setup, this either uses customize to edit the variable
  16944. `org-agenda-files', or it visits the file that is holding the list. In the
  16945. latter case, the buffer is set up in a way that saving it automatically kills
  16946. the buffer and restores the previous window configuration."
  16947. (interactive)
  16948. (if (stringp org-agenda-files)
  16949. (let ((cw (current-window-configuration)))
  16950. (find-file org-agenda-files)
  16951. (setq-local org-window-configuration cw)
  16952. (org-add-hook 'after-save-hook
  16953. (lambda ()
  16954. (set-window-configuration
  16955. (prog1 org-window-configuration
  16956. (kill-buffer (current-buffer))))
  16957. (org-install-agenda-files-menu)
  16958. (message "New agenda file list installed"))
  16959. nil 'local)
  16960. (message "%s" (substitute-command-keys
  16961. "Edit list and finish with \\[save-buffer]")))
  16962. (customize-variable 'org-agenda-files)))
  16963. (defun org-store-new-agenda-file-list (list)
  16964. "Set new value for the agenda file list and save it correctly."
  16965. (if (stringp org-agenda-files)
  16966. (let ((fe (org-read-agenda-file-list t)) b u)
  16967. (while (setq b (find-buffer-visiting org-agenda-files))
  16968. (kill-buffer b))
  16969. (with-temp-file org-agenda-files
  16970. (insert
  16971. (mapconcat
  16972. (lambda (f) ;; Keep un-expanded entries.
  16973. (if (setq u (assoc f fe))
  16974. (cdr u)
  16975. f))
  16976. list "\n")
  16977. "\n")))
  16978. (let ((org-mode-hook nil) (org-inhibit-startup t)
  16979. (org-insert-mode-line-in-empty-file nil))
  16980. (setq org-agenda-files list)
  16981. (customize-save-variable 'org-agenda-files org-agenda-files))))
  16982. (defun org-read-agenda-file-list (&optional pair-with-expansion)
  16983. "Read the list of agenda files from a file.
  16984. If PAIR-WITH-EXPANSION is t return pairs with un-expanded
  16985. filenames, used by `org-store-new-agenda-file-list' to write back
  16986. un-expanded file names."
  16987. (when (file-directory-p org-agenda-files)
  16988. (error "`org-agenda-files' cannot be a single directory"))
  16989. (when (stringp org-agenda-files)
  16990. (with-temp-buffer
  16991. (insert-file-contents org-agenda-files)
  16992. (mapcar
  16993. (lambda (f)
  16994. (let ((e (expand-file-name (substitute-in-file-name f)
  16995. org-directory)))
  16996. (if pair-with-expansion
  16997. (cons e f)
  16998. e)))
  16999. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*")))))
  17000. ;;;###autoload
  17001. (defun org-cycle-agenda-files ()
  17002. "Cycle through the files in `org-agenda-files'.
  17003. If the current buffer visits an agenda file, find the next one in the list.
  17004. If the current buffer does not, find the first agenda file."
  17005. (interactive)
  17006. (let* ((fs (or (org-agenda-files t)
  17007. (user-error "No agenda files")))
  17008. (files (copy-sequence fs))
  17009. (tcf (and buffer-file-name (file-truename buffer-file-name)))
  17010. file)
  17011. (when tcf
  17012. (while (and (setq file (pop files))
  17013. (not (equal (file-truename file) tcf)))))
  17014. (find-file (car (or files fs)))
  17015. (when (buffer-base-buffer) (org-pop-to-buffer-same-window (buffer-base-buffer)))))
  17016. (defun org-agenda-file-to-front (&optional to-end)
  17017. "Move/add the current file to the top of the agenda file list.
  17018. If the file is not present in the list, it is added to the front. If it is
  17019. present, it is moved there. With optional argument TO-END, add/move to the
  17020. end of the list."
  17021. (interactive "P")
  17022. (let ((org-agenda-skip-unavailable-files nil)
  17023. (file-alist (mapcar (lambda (x)
  17024. (cons (file-truename x) x))
  17025. (org-agenda-files t)))
  17026. (ctf (file-truename
  17027. (or buffer-file-name
  17028. (user-error "Please save the current buffer to a file"))))
  17029. x had)
  17030. (setq x (assoc ctf file-alist) had x)
  17031. (unless x (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  17032. (if to-end
  17033. (setq file-alist (append (delq x file-alist) (list x)))
  17034. (setq file-alist (cons x (delq x file-alist))))
  17035. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  17036. (org-install-agenda-files-menu)
  17037. (message "File %s to %s of agenda file list"
  17038. (if had "moved" "added") (if to-end "end" "front"))))
  17039. (defun org-remove-file (&optional file)
  17040. "Remove current file from the list of files in variable `org-agenda-files'.
  17041. These are the files which are being checked for agenda entries.
  17042. Optional argument FILE means use this file instead of the current."
  17043. (interactive)
  17044. (let* ((org-agenda-skip-unavailable-files nil)
  17045. (file (or file buffer-file-name
  17046. (user-error "Current buffer does not visit a file")))
  17047. (true-file (file-truename file))
  17048. (afile (abbreviate-file-name file))
  17049. (files (delq nil (mapcar
  17050. (lambda (x)
  17051. (unless (equal true-file
  17052. (file-truename x))
  17053. x))
  17054. (org-agenda-files t)))))
  17055. (if (not (= (length files) (length (org-agenda-files t))))
  17056. (progn
  17057. (org-store-new-agenda-file-list files)
  17058. (org-install-agenda-files-menu)
  17059. (message "Removed from Org Agenda list: %s" afile))
  17060. (message "File was not in list: %s (not removed)" afile))))
  17061. (defun org-file-menu-entry (file)
  17062. (vector file (list 'find-file file) t))
  17063. (defun org-check-agenda-file (file)
  17064. "Make sure FILE exists. If not, ask user what to do."
  17065. (unless (file-exists-p file)
  17066. (message "Non-existent agenda file %s. [R]emove from list or [A]bort?"
  17067. (abbreviate-file-name file))
  17068. (let ((r (downcase (read-char-exclusive))))
  17069. (cond
  17070. ((equal r ?r)
  17071. (org-remove-file file)
  17072. (throw 'nextfile t))
  17073. (t (user-error "Abort"))))))
  17074. (defun org-get-agenda-file-buffer (file)
  17075. "Get an agenda buffer visiting FILE.
  17076. If the buffer needs to be created, add it to the list of buffers
  17077. which might be released later."
  17078. (let ((buf (org-find-base-buffer-visiting file)))
  17079. (if buf
  17080. buf ; just return it
  17081. ;; Make a new buffer and remember it
  17082. (setq buf (find-file-noselect file))
  17083. (when buf (push buf org-agenda-new-buffers))
  17084. buf)))
  17085. (defun org-release-buffers (blist)
  17086. "Release all buffers in list, asking the user for confirmation when needed.
  17087. When a buffer is unmodified, it is just killed. When modified, it is saved
  17088. \(if the user agrees) and then killed."
  17089. (let (file)
  17090. (dolist (buf blist)
  17091. (setq file (buffer-file-name buf))
  17092. (when (and (buffer-modified-p buf)
  17093. file
  17094. (y-or-n-p (format "Save file %s? " file)))
  17095. (with-current-buffer buf (save-buffer)))
  17096. (kill-buffer buf))))
  17097. (defun org-agenda-prepare-buffers (files)
  17098. "Create buffers for all agenda files, protect archived trees and comments."
  17099. (interactive)
  17100. (let ((pa '(:org-archived t))
  17101. (pc '(:org-comment t))
  17102. (pall '(:org-archived t :org-comment t))
  17103. (inhibit-read-only t)
  17104. (org-inhibit-startup org-agenda-inhibit-startup)
  17105. (rea (concat ":" org-archive-tag ":"))
  17106. re pos)
  17107. (setq org-tag-alist-for-agenda nil
  17108. org-tag-groups-alist-for-agenda nil)
  17109. (save-excursion
  17110. (save-restriction
  17111. (dolist (file files)
  17112. (catch 'nextfile
  17113. (if (bufferp file)
  17114. (set-buffer file)
  17115. (org-check-agenda-file file)
  17116. (set-buffer (org-get-agenda-file-buffer file)))
  17117. (widen)
  17118. (org-set-regexps-and-options 'tags-only)
  17119. (setq pos (point))
  17120. (or (memq 'category org-agenda-ignore-properties)
  17121. (org-refresh-category-properties))
  17122. (or (memq 'stats org-agenda-ignore-properties)
  17123. (org-refresh-stats-properties))
  17124. (or (memq 'effort org-agenda-ignore-properties)
  17125. (org-refresh-effort-properties))
  17126. (or (memq 'appt org-agenda-ignore-properties)
  17127. (org-refresh-properties "APPT_WARNTIME" 'org-appt-warntime))
  17128. (setq org-todo-keywords-for-agenda
  17129. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  17130. (setq org-done-keywords-for-agenda
  17131. (append org-done-keywords-for-agenda org-done-keywords))
  17132. (setq org-todo-keyword-alist-for-agenda
  17133. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  17134. (setq org-tag-alist-for-agenda
  17135. (org-uniquify
  17136. (append org-tag-alist-for-agenda
  17137. org-tag-alist
  17138. org-tag-persistent-alist)))
  17139. (when org-group-tags
  17140. (setq org-tag-groups-alist-for-agenda
  17141. (org-uniquify-alist
  17142. (append org-tag-groups-alist-for-agenda org-tag-groups-alist))))
  17143. (org-with-silent-modifications
  17144. (save-excursion
  17145. (remove-text-properties (point-min) (point-max) pall)
  17146. (when org-agenda-skip-archived-trees
  17147. (goto-char (point-min))
  17148. (while (re-search-forward rea nil t)
  17149. (when (org-at-heading-p t)
  17150. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  17151. (goto-char (point-min))
  17152. (setq re (format "^\\*+ .*\\<%s\\>" org-comment-string))
  17153. (while (re-search-forward re nil t)
  17154. (when (save-match-data (org-in-commented-heading-p t))
  17155. (add-text-properties
  17156. (match-beginning 0) (org-end-of-subtree t) pc)))))
  17157. (goto-char pos)))))
  17158. (setq org-todo-keywords-for-agenda
  17159. (org-uniquify org-todo-keywords-for-agenda))
  17160. (setq org-todo-keyword-alist-for-agenda
  17161. (org-uniquify org-todo-keyword-alist-for-agenda))))
  17162. ;;;; CDLaTeX minor mode
  17163. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  17164. "Keymap for the minor `org-cdlatex-mode'.")
  17165. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  17166. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  17167. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  17168. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  17169. (org-defkey org-cdlatex-mode-map "\C-c{" 'org-cdlatex-environment-indent)
  17170. (defvar org-cdlatex-texmathp-advice-is-done nil
  17171. "Flag remembering if we have applied the advice to texmathp already.")
  17172. (define-minor-mode org-cdlatex-mode
  17173. "Toggle the minor `org-cdlatex-mode'.
  17174. This mode supports entering LaTeX environment and math in LaTeX fragments
  17175. in Org-mode.
  17176. \\{org-cdlatex-mode-map}"
  17177. nil " OCDL" nil
  17178. (when org-cdlatex-mode
  17179. (require 'cdlatex)
  17180. (run-hooks 'cdlatex-mode-hook)
  17181. (cdlatex-compute-tables))
  17182. (unless org-cdlatex-texmathp-advice-is-done
  17183. (setq org-cdlatex-texmathp-advice-is-done t)
  17184. (defadvice texmathp (around org-math-always-on activate)
  17185. "Always return t in org-mode buffers.
  17186. This is because we want to insert math symbols without dollars even outside
  17187. the LaTeX math segments. If Orgmode thinks that point is actually inside
  17188. an embedded LaTeX fragment, let texmathp do its job.
  17189. \\[org-cdlatex-mode-map]"
  17190. (interactive)
  17191. (let (p)
  17192. (cond
  17193. ((not (derived-mode-p 'org-mode)) ad-do-it)
  17194. ((eq this-command 'cdlatex-math-symbol)
  17195. (setq ad-return-value t
  17196. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  17197. (t
  17198. (let ((p (org-inside-LaTeX-fragment-p)))
  17199. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  17200. (setq ad-return-value t
  17201. texmathp-why '("Org-mode embedded math" . 0))
  17202. (when p ad-do-it)))))))))
  17203. (defun turn-on-org-cdlatex ()
  17204. "Unconditionally turn on `org-cdlatex-mode'."
  17205. (org-cdlatex-mode 1))
  17206. (defun org-try-cdlatex-tab ()
  17207. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  17208. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  17209. - inside a LaTeX fragment, or
  17210. - after the first word in a line, where an abbreviation expansion could
  17211. insert a LaTeX environment."
  17212. (when org-cdlatex-mode
  17213. (cond
  17214. ;; Before any word on the line: No expansion possible.
  17215. ((save-excursion (skip-chars-backward " \t") (bolp)) nil)
  17216. ;; Just after first word on the line: Expand it. Make sure it
  17217. ;; cannot happen on headlines, though.
  17218. ((save-excursion
  17219. (skip-chars-backward "a-zA-Z0-9*")
  17220. (skip-chars-backward " \t")
  17221. (and (bolp) (not (org-at-heading-p))))
  17222. (cdlatex-tab) t)
  17223. ((org-inside-LaTeX-fragment-p) (cdlatex-tab) t))))
  17224. (defun org-cdlatex-underscore-caret (&optional _arg)
  17225. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  17226. Revert to the normal definition outside of these fragments."
  17227. (interactive "P")
  17228. (if (org-inside-LaTeX-fragment-p)
  17229. (call-interactively 'cdlatex-sub-superscript)
  17230. (let (org-cdlatex-mode)
  17231. (call-interactively (key-binding (vector last-input-event))))))
  17232. (defun org-cdlatex-math-modify (&optional _arg)
  17233. "Execute `cdlatex-math-modify' in LaTeX fragments.
  17234. Revert to the normal definition outside of these fragments."
  17235. (interactive "P")
  17236. (if (org-inside-LaTeX-fragment-p)
  17237. (call-interactively 'cdlatex-math-modify)
  17238. (let (org-cdlatex-mode)
  17239. (call-interactively (key-binding (vector last-input-event))))))
  17240. (defun org-cdlatex-environment-indent (&optional environment item)
  17241. "Execute `cdlatex-environment' and indent the inserted environment.
  17242. ENVIRONMENT and ITEM are passed to `cdlatex-environment'.
  17243. The inserted environment is indented to current indentation
  17244. unless point is at the beginning of the line, in which the
  17245. environment remains unintended."
  17246. (interactive)
  17247. ;; cdlatex-environment always return nil. Therefore, capture output
  17248. ;; first and determine if an environment was selected.
  17249. (let* ((beg (point-marker))
  17250. (end (copy-marker (point) t))
  17251. (inserted (progn
  17252. (ignore-errors (cdlatex-environment environment item))
  17253. (< beg end)))
  17254. ;; Figure out how many lines to move forward after the
  17255. ;; environment has been inserted.
  17256. (lines (when inserted
  17257. (save-excursion
  17258. (- (cl-loop while (< beg (point))
  17259. with x = 0
  17260. do (forward-line -1)
  17261. (cl-incf x)
  17262. finally return x)
  17263. (if (progn (goto-char beg)
  17264. (and (progn (skip-chars-forward " \t") (eolp))
  17265. (progn (skip-chars-backward " \t") (bolp))))
  17266. 1 0)))))
  17267. (env (org-trim (delete-and-extract-region beg end))))
  17268. (when inserted
  17269. ;; Get indentation of next line unless at column 0.
  17270. (let ((ind (if (bolp) 0
  17271. (save-excursion
  17272. (org-return-indent)
  17273. (prog1 (org-get-indentation)
  17274. (when (progn (skip-chars-forward " \t") (eolp))
  17275. (delete-region beg (point)))))))
  17276. (bol (progn (skip-chars-backward " \t") (bolp))))
  17277. ;; Insert a newline before environment unless at column zero
  17278. ;; to "escape" the current line. Insert a newline if
  17279. ;; something is one the same line as \end{ENVIRONMENT}.
  17280. (insert
  17281. (concat (unless bol "\n") env
  17282. (when (and (skip-chars-forward " \t") (not (eolp))) "\n")))
  17283. (unless (zerop ind)
  17284. (save-excursion
  17285. (goto-char beg)
  17286. (while (< (point) end)
  17287. (unless (eolp) (org-indent-line-to ind))
  17288. (forward-line))))
  17289. (goto-char beg)
  17290. (forward-line lines)
  17291. (org-indent-line-to ind)))
  17292. (set-marker beg nil)
  17293. (set-marker end nil)))
  17294. ;;;; LaTeX fragments
  17295. (defun org-inside-LaTeX-fragment-p ()
  17296. "Test if point is inside a LaTeX fragment.
  17297. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  17298. sequence appearing also before point.
  17299. Even though the matchers for math are configurable, this function assumes
  17300. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  17301. delimiters are skipped when they have been removed by customization.
  17302. The return value is nil, or a cons cell with the delimiter and the
  17303. position of this delimiter.
  17304. This function does a reasonably good job, but can locally be fooled by
  17305. for example currency specifications. For example it will assume being in
  17306. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  17307. fragments that are properly closed, but during editing, we have to live
  17308. with the uncertainty caused by missing closing delimiters. This function
  17309. looks only before point, not after."
  17310. (catch 'exit
  17311. (let ((pos (point))
  17312. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  17313. (lim (progn
  17314. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  17315. (point)))
  17316. dd-on str (start 0) m re)
  17317. (goto-char pos)
  17318. (when dodollar
  17319. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  17320. re (nth 1 (assoc "$" org-latex-regexps)))
  17321. (while (string-match re str start)
  17322. (cond
  17323. ((= (match-end 0) (length str))
  17324. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  17325. ((= (match-end 0) (- (length str) 5))
  17326. (throw 'exit nil))
  17327. (t (setq start (match-end 0))))))
  17328. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  17329. (goto-char pos)
  17330. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  17331. (and (match-beginning 2) (throw 'exit nil))
  17332. ;; count $$
  17333. (while (re-search-backward "\\$\\$" lim t)
  17334. (setq dd-on (not dd-on)))
  17335. (goto-char pos)
  17336. (when dd-on (cons "$$" m))))))
  17337. (defun org-inside-latex-macro-p ()
  17338. "Is point inside a LaTeX macro or its arguments?"
  17339. (save-match-data
  17340. (org-in-regexp
  17341. "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")))
  17342. (defvar-local org-latex-fragment-image-overlays nil
  17343. "List of overlays carrying the images of latex fragments.")
  17344. (defun org-remove-latex-fragment-image-overlays (&optional beg end)
  17345. "Remove all overlays with LaTeX fragment images in current buffer.
  17346. When optional arguments BEG and END are non-nil, remove all
  17347. overlays between them instead. Return t when some overlays were
  17348. removed, nil otherwise."
  17349. (let (removedp)
  17350. (setq org-latex-fragment-image-overlays
  17351. (let ((beg (or beg (point-min)))
  17352. (end (or end (point-max))))
  17353. (cl-remove-if
  17354. (lambda (o)
  17355. (cond ((not (overlay-buffer o)) (delete-overlay o) t)
  17356. ((and (>= (overlay-start o) beg)
  17357. (<= (overlay-end o) end))
  17358. (delete-overlay o)
  17359. (unless removedp (setq removedp t)))
  17360. (t nil)))
  17361. org-latex-fragment-image-overlays)))
  17362. removedp))
  17363. (define-obsolete-function-alias
  17364. 'org-preview-latex-fragment 'org-toggle-latex-fragment "24.4")
  17365. (defun org-toggle-latex-fragment (&optional arg)
  17366. "Preview the LaTeX fragment at point, or all locally or globally.
  17367. If the cursor is on a LaTeX fragment, create the image and overlay
  17368. it over the source code, if there is none. Remove it otherwise.
  17369. If there is no fragment at point, display all fragments in the
  17370. current section.
  17371. With prefix ARG, preview or clear image for all fragments in the
  17372. current subtree or in the whole buffer when used before the first
  17373. headline. With a double prefix ARG \\[universal-argument] \
  17374. \\[universal-argument] preview or clear images
  17375. for all fragments in the buffer."
  17376. (interactive "P")
  17377. (unless (buffer-file-name (buffer-base-buffer))
  17378. (user-error "Can't preview LaTeX fragment in a non-file buffer"))
  17379. (when (display-graphic-p)
  17380. (catch 'exit
  17381. (save-excursion
  17382. (let ((window-start (window-start)) msg)
  17383. (save-restriction
  17384. (cond
  17385. ((or (equal arg '(16))
  17386. (and (equal arg '(4))
  17387. (org-with-limited-levels (org-before-first-heading-p))))
  17388. (if (org-remove-latex-fragment-image-overlays)
  17389. (progn (message "LaTeX fragments images removed from buffer")
  17390. (throw 'exit nil))
  17391. (setq msg "Creating images for buffer...")))
  17392. ((equal arg '(4))
  17393. (org-with-limited-levels (org-back-to-heading t))
  17394. (let ((beg (point))
  17395. (end (progn (org-end-of-subtree t) (point))))
  17396. (if (org-remove-latex-fragment-image-overlays beg end)
  17397. (progn
  17398. (message "LaTeX fragment images removed from subtree")
  17399. (throw 'exit nil))
  17400. (setq msg "Creating images for subtree...")
  17401. (narrow-to-region beg end))))
  17402. ((let ((datum (org-element-context)))
  17403. (when (memq (org-element-type datum)
  17404. '(latex-environment latex-fragment))
  17405. (let* ((beg (org-element-property :begin datum))
  17406. (end (org-element-property :end datum)))
  17407. (if (org-remove-latex-fragment-image-overlays beg end)
  17408. (progn (message "LaTeX fragment image removed")
  17409. (throw 'exit nil))
  17410. (narrow-to-region beg end)
  17411. (setq msg "Creating image..."))))))
  17412. (t
  17413. (org-with-limited-levels
  17414. (let ((beg (if (org-at-heading-p) (line-beginning-position)
  17415. (outline-previous-heading)
  17416. (point)))
  17417. (end (progn (outline-next-heading) (point))))
  17418. (if (org-remove-latex-fragment-image-overlays beg end)
  17419. (progn
  17420. (message "LaTeX fragment images removed from section")
  17421. (throw 'exit nil))
  17422. (setq msg "Creating images for section...")
  17423. (narrow-to-region beg end))))))
  17424. (let ((file (buffer-file-name (buffer-base-buffer))))
  17425. (org-format-latex
  17426. (concat org-latex-preview-ltxpng-directory
  17427. (file-name-sans-extension (file-name-nondirectory file)))
  17428. ;; Emacs cannot overlay images from remote hosts.
  17429. ;; Create it in `temporary-file-directory' instead.
  17430. (if (file-remote-p file) temporary-file-directory
  17431. default-directory)
  17432. 'overlays msg 'forbuffer
  17433. org-latex-create-formula-image-program)))
  17434. ;; Work around a bug that doesn't restore window's start
  17435. ;; when widening back the buffer.
  17436. (set-window-start nil window-start)
  17437. (message (concat msg "done")))))))
  17438. (defun org-format-latex
  17439. (prefix &optional dir overlays msg forbuffer processing-type)
  17440. "Replace LaTeX fragments with links to an image, and produce images.
  17441. When optional argument OVERLAYS is non-nil, display the image on
  17442. top of the fragment instead of replacing it.
  17443. PROCESSING-TYPE is the conversion method to use, as a symbol.
  17444. Some of the options can be changed using the variable
  17445. `org-format-latex-options', which see."
  17446. (when (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  17447. (unless (eq processing-type 'verbatim)
  17448. (let* ((math-regexp "\\$\\|\\\\[([]\\|^[ \t]*\\\\begin{[A-Za-z0-9*]+}")
  17449. (cnt 0)
  17450. checkdir-flag)
  17451. (goto-char (point-min))
  17452. ;; Optimize overlay creation: (info "(elisp) Managing Overlays").
  17453. (when (and overlays (memq processing-type '(dvipng imagemagick)))
  17454. (overlay-recenter (point-max)))
  17455. (while (re-search-forward math-regexp nil t)
  17456. (unless (and overlays
  17457. (eq (get-char-property (point) 'org-overlay-type)
  17458. 'org-latex-overlay))
  17459. (let* ((context (org-element-context))
  17460. (type (org-element-type context)))
  17461. (when (memq type '(latex-environment latex-fragment))
  17462. (let ((block-type (eq type 'latex-environment))
  17463. (value (org-element-property :value context))
  17464. (beg (org-element-property :begin context))
  17465. (end (save-excursion
  17466. (goto-char (org-element-property :end context))
  17467. (skip-chars-backward " \r\t\n")
  17468. (point))))
  17469. (cl-case processing-type
  17470. (mathjax
  17471. ;; Prepare for MathJax processing.
  17472. (if (eq (char-after beg) ?$)
  17473. (save-excursion
  17474. (delete-region beg end)
  17475. (insert "\\(" (substring value 1 -1) "\\)"))
  17476. (goto-char end)))
  17477. ((dvipng imagemagick)
  17478. ;; Process to an image.
  17479. (cl-incf cnt)
  17480. (goto-char beg)
  17481. (let* ((face (face-at-point))
  17482. ;; Get the colors from the face at point.
  17483. (fg
  17484. (let ((color (plist-get org-format-latex-options
  17485. :foreground)))
  17486. (if (and forbuffer (eq color 'auto))
  17487. (face-attribute face :foreground nil 'default)
  17488. color)))
  17489. (bg
  17490. (let ((color (plist-get org-format-latex-options
  17491. :background)))
  17492. (if (and forbuffer (eq color 'auto))
  17493. (face-attribute face :background nil 'default)
  17494. color)))
  17495. (hash (sha1 (prin1-to-string
  17496. (list org-format-latex-header
  17497. org-latex-default-packages-alist
  17498. org-latex-packages-alist
  17499. org-format-latex-options
  17500. forbuffer value fg bg))))
  17501. (absprefix (expand-file-name prefix dir))
  17502. (linkfile (format "%s_%s.png" prefix hash))
  17503. (movefile (format "%s_%s.png" absprefix hash))
  17504. (sep (and block-type "\n\n"))
  17505. (link (concat sep "[[file:" linkfile "]]" sep))
  17506. (options
  17507. (org-combine-plists
  17508. org-format-latex-options
  17509. `(:foreground ,fg :background ,bg))))
  17510. (when msg (message msg cnt))
  17511. (unless checkdir-flag ; Ensure the directory exists.
  17512. (setq checkdir-flag t)
  17513. (let ((todir (file-name-directory absprefix)))
  17514. (unless (file-directory-p todir)
  17515. (make-directory todir t))))
  17516. (unless (file-exists-p movefile)
  17517. (org-create-formula-image
  17518. value movefile options forbuffer processing-type))
  17519. (if overlays
  17520. (progn
  17521. (dolist (o (overlays-in beg end))
  17522. (when (eq (overlay-get o 'org-overlay-type)
  17523. 'org-latex-overlay)
  17524. (delete-overlay o)))
  17525. (let ((ov (make-overlay beg end)))
  17526. (overlay-put ov
  17527. 'org-overlay-type
  17528. 'org-latex-overlay)
  17529. (overlay-put ov 'evaporate t)
  17530. (if (featurep 'xemacs)
  17531. (progn
  17532. (overlay-put ov 'invisible t)
  17533. (overlay-put
  17534. ov 'end-glyph
  17535. (make-glyph
  17536. (vector 'png :file movefile))))
  17537. (overlay-put
  17538. ov 'display
  17539. (list 'image
  17540. :type 'png
  17541. :file movefile
  17542. :ascent 'center)))
  17543. (push ov org-latex-fragment-image-overlays))
  17544. (goto-char end))
  17545. (delete-region beg end)
  17546. (insert
  17547. (org-add-props link
  17548. (list 'org-latex-src
  17549. (replace-regexp-in-string "\"" "" value)
  17550. 'org-latex-src-embed-type
  17551. (if block-type 'paragraph 'character)))))))
  17552. (mathml
  17553. ;; Process to MathML.
  17554. (unless (org-format-latex-mathml-available-p)
  17555. (user-error "LaTeX to MathML converter not configured"))
  17556. (cl-incf cnt)
  17557. (when msg (message msg cnt))
  17558. (goto-char beg)
  17559. (delete-region beg end)
  17560. (insert (org-format-latex-as-mathml
  17561. value block-type prefix dir)))
  17562. (otherwise
  17563. (error "Unknown conversion type %s for LaTeX fragments"
  17564. processing-type)))))))))))
  17565. (defun org-create-math-formula (latex-frag &optional mathml-file)
  17566. "Convert LATEX-FRAG to MathML and store it in MATHML-FILE.
  17567. Use `org-latex-to-mathml-convert-command'. If the conversion is
  17568. sucessful, return the portion between \"<math...> </math>\"
  17569. elements otherwise return nil. When MATHML-FILE is specified,
  17570. write the results in to that file. When invoked as an
  17571. interactive command, prompt for LATEX-FRAG, with initial value
  17572. set to the current active region and echo the results for user
  17573. inspection."
  17574. (interactive (list (let ((frag (when (org-region-active-p)
  17575. (buffer-substring-no-properties
  17576. (region-beginning) (region-end)))))
  17577. (read-string "LaTeX Fragment: " frag nil frag))))
  17578. (unless latex-frag (user-error "Invalid LaTeX fragment"))
  17579. (let* ((tmp-in-file
  17580. (let ((file (file-relative-name
  17581. (make-temp-name (expand-file-name "ltxmathml-in")))))
  17582. (write-region latex-frag nil file)
  17583. file))
  17584. (tmp-out-file (file-relative-name
  17585. (make-temp-name (expand-file-name "ltxmathml-out"))))
  17586. (cmd (format-spec
  17587. org-latex-to-mathml-convert-command
  17588. `((?j . ,(and org-latex-to-mathml-jar-file
  17589. (shell-quote-argument
  17590. (expand-file-name
  17591. org-latex-to-mathml-jar-file))))
  17592. (?I . ,(shell-quote-argument tmp-in-file))
  17593. (?i . ,latex-frag)
  17594. (?o . ,(shell-quote-argument tmp-out-file)))))
  17595. mathml shell-command-output)
  17596. (when (org-called-interactively-p 'any)
  17597. (unless (org-format-latex-mathml-available-p)
  17598. (user-error "LaTeX to MathML converter not configured")))
  17599. (message "Running %s" cmd)
  17600. (setq shell-command-output (shell-command-to-string cmd))
  17601. (setq mathml
  17602. (when (file-readable-p tmp-out-file)
  17603. (with-current-buffer (find-file-noselect tmp-out-file t)
  17604. (goto-char (point-min))
  17605. (when (re-search-forward
  17606. (concat
  17607. (regexp-quote
  17608. "<math xmlns=\"http://www.w3.org/1998/Math/MathML\"")
  17609. "[^>]*?>"
  17610. "\\(.\\|\n\\)*"
  17611. "</math>")
  17612. nil t)
  17613. (prog1 (match-string 0) (kill-buffer))))))
  17614. (cond
  17615. (mathml
  17616. (setq mathml
  17617. (concat "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" mathml))
  17618. (when mathml-file
  17619. (write-region mathml nil mathml-file))
  17620. (when (org-called-interactively-p 'any)
  17621. (message mathml)))
  17622. ((message "LaTeX to MathML conversion failed")
  17623. (message shell-command-output)))
  17624. (delete-file tmp-in-file)
  17625. (when (file-exists-p tmp-out-file)
  17626. (delete-file tmp-out-file))
  17627. mathml))
  17628. (defun org-format-latex-as-mathml (latex-frag latex-frag-type
  17629. prefix &optional dir)
  17630. "Use `org-create-math-formula' but check local cache first."
  17631. (let* ((absprefix (expand-file-name prefix dir))
  17632. (print-length nil) (print-level nil)
  17633. (formula-id (concat
  17634. "formula-"
  17635. (sha1
  17636. (prin1-to-string
  17637. (list latex-frag
  17638. org-latex-to-mathml-convert-command)))))
  17639. (formula-cache (format "%s-%s.mathml" absprefix formula-id))
  17640. (formula-cache-dir (file-name-directory formula-cache)))
  17641. (unless (file-directory-p formula-cache-dir)
  17642. (make-directory formula-cache-dir t))
  17643. (unless (file-exists-p formula-cache)
  17644. (org-create-math-formula latex-frag formula-cache))
  17645. (if (file-exists-p formula-cache)
  17646. ;; Successful conversion. Return the link to MathML file.
  17647. (org-add-props
  17648. (format "[[file:%s]]" (file-relative-name formula-cache dir))
  17649. (list 'org-latex-src (replace-regexp-in-string "\"" "" latex-frag)
  17650. 'org-latex-src-embed-type (if latex-frag-type
  17651. 'paragraph 'character)))
  17652. ;; Failed conversion. Return the LaTeX fragment verbatim
  17653. latex-frag)))
  17654. (defun org-create-formula-image (string tofile options buffer &optional type)
  17655. "Create an image from LaTeX source using dvipng or convert.
  17656. This function calls either `org-create-formula-image-with-dvipng'
  17657. or `org-create-formula-image-with-imagemagick' depending on the
  17658. value of `org-latex-create-formula-image-program' or on the value
  17659. of the optional TYPE variable.
  17660. Note: ultimately these two function should be combined as they
  17661. share a good deal of logic."
  17662. (org-check-external-command
  17663. "latex" "needed to convert LaTeX fragments to images")
  17664. (funcall
  17665. (cl-case (or type org-latex-create-formula-image-program)
  17666. (dvipng
  17667. (org-check-external-command
  17668. "dvipng" "needed to convert LaTeX fragments to images")
  17669. #'org-create-formula-image-with-dvipng)
  17670. (imagemagick
  17671. (org-check-external-command
  17672. "convert" "you need to install imagemagick")
  17673. #'org-create-formula-image-with-imagemagick)
  17674. (t (error
  17675. "Invalid value of `org-latex-create-formula-image-program'")))
  17676. string tofile options buffer))
  17677. (declare-function org-export-get-backend "ox" (name))
  17678. (declare-function org-export--get-global-options "ox" (&optional backend))
  17679. (declare-function org-export--get-inbuffer-options "ox" (&optional backend))
  17680. (declare-function org-latex-guess-inputenc "ox-latex" (header))
  17681. (declare-function org-latex-guess-babel-language "ox-latex" (header info))
  17682. (defun org-create-formula--latex-header ()
  17683. "Return LaTeX header appropriate for previewing a LaTeX snippet."
  17684. (let ((info (org-combine-plists (org-export--get-global-options
  17685. (org-export-get-backend 'latex))
  17686. (org-export--get-inbuffer-options
  17687. (org-export-get-backend 'latex)))))
  17688. (org-latex-guess-babel-language
  17689. (org-latex-guess-inputenc
  17690. (org-splice-latex-header
  17691. org-format-latex-header
  17692. org-latex-default-packages-alist
  17693. org-latex-packages-alist t
  17694. (plist-get info :latex-header)))
  17695. info)))
  17696. (defun org--get-display-dpi ()
  17697. "Get the DPI of the display.
  17698. Assumes that the display has the same pixel width in the
  17699. horizontal and vertical directions."
  17700. (if (display-graphic-p)
  17701. (round (/ (display-pixel-height)
  17702. (/ (display-mm-height) 25.4)))
  17703. (error "Attempt to calculate the dpi of a non-graphic display")))
  17704. ;; This function borrows from Ganesh Swami's latex2png.el
  17705. (defun org-create-formula-image-with-dvipng (string tofile options buffer)
  17706. "This calls dvipng."
  17707. (require 'ox-latex)
  17708. (let* ((tmpdir (if (featurep 'xemacs)
  17709. (temp-directory)
  17710. temporary-file-directory))
  17711. (texfilebase (make-temp-name
  17712. (expand-file-name "orgtex" tmpdir)))
  17713. (texfile (concat texfilebase ".tex"))
  17714. (dvifile (concat texfilebase ".dvi"))
  17715. (pngfile (concat texfilebase ".png"))
  17716. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  17717. ;; This assumes that the display has the same pixel width in
  17718. ;; the horizontal and vertical directions
  17719. (dpi (number-to-string (* scale (if buffer (org--get-display-dpi) 120))))
  17720. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  17721. "Black"))
  17722. (bg (or (plist-get options (if buffer :background :html-background))
  17723. "Transparent")))
  17724. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground))
  17725. (unless (string= fg "Transparent") (setq fg (org-dvipng-color-format fg))))
  17726. (if (eq bg 'default) (setq bg (org-dvipng-color :background))
  17727. (unless (string= bg "Transparent") (setq bg (org-dvipng-color-format bg))))
  17728. (let ((latex-header (org-create-formula--latex-header)))
  17729. (with-temp-file texfile
  17730. (insert latex-header)
  17731. (insert "\n\\begin{document}\n" string "\n\\end{document}\n")))
  17732. (let ((dir default-directory))
  17733. (ignore-errors
  17734. (cd tmpdir)
  17735. (call-process "latex" nil nil nil texfile))
  17736. (cd dir))
  17737. (if (not (file-exists-p dvifile))
  17738. (progn (message "Failed to create dvi file from %s" texfile) nil)
  17739. (ignore-errors
  17740. (if (featurep 'xemacs)
  17741. (call-process "dvipng" nil nil nil
  17742. "-fg" fg "-bg" bg
  17743. "-T" "tight"
  17744. "-o" pngfile
  17745. dvifile)
  17746. (call-process "dvipng" nil nil nil
  17747. "-fg" fg "-bg" bg
  17748. "-D" dpi
  17749. ;;"-x" scale "-y" scale
  17750. "-T" "tight"
  17751. "-o" pngfile
  17752. dvifile)))
  17753. (if (not (file-exists-p pngfile))
  17754. (if org-format-latex-signal-error
  17755. (error "Failed to create png file from %s" texfile)
  17756. (message "Failed to create png file from %s" texfile)
  17757. nil)
  17758. ;; Use the requested file name and clean up
  17759. (copy-file pngfile tofile 'replace)
  17760. (dolist (e '(".dvi" ".tex" ".aux" ".log" ".png" ".out"))
  17761. (when (file-exists-p (concat texfilebase e))
  17762. (delete-file (concat texfilebase e))))
  17763. pngfile))))
  17764. (declare-function org-latex-compile "ox-latex" (texfile &optional snippet))
  17765. (defun org-create-formula-image-with-imagemagick (string tofile options buffer)
  17766. "This calls convert, which is included into imagemagick."
  17767. (require 'ox-latex)
  17768. (let* ((tmpdir (if (featurep 'xemacs)
  17769. (temp-directory)
  17770. temporary-file-directory))
  17771. (texfilebase (make-temp-name
  17772. (expand-file-name "orgtex" tmpdir)))
  17773. (texfile (concat texfilebase ".tex"))
  17774. (pdffile (concat texfilebase ".pdf"))
  17775. (pngfile (concat texfilebase ".png"))
  17776. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  17777. (dpi (number-to-string (* scale (if buffer (org--get-display-dpi) 120))))
  17778. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  17779. "black"))
  17780. (bg (or (plist-get options (if buffer :background :html-background))
  17781. "white")))
  17782. (if (eq fg 'default) (setq fg (org-latex-color :foreground))
  17783. (setq fg (org-latex-color-format fg)))
  17784. (if (eq bg 'default) (setq bg (org-latex-color :background))
  17785. (setq bg (org-latex-color-format
  17786. (if (string= bg "Transparent") "white" bg))))
  17787. (let ((latex-header (org-create-formula--latex-header)))
  17788. (with-temp-file texfile
  17789. (insert latex-header)
  17790. (insert "\n\\begin{document}\n"
  17791. "\\definecolor{fg}{rgb}{" fg "}\n"
  17792. "\\definecolor{bg}{rgb}{" bg "}\n"
  17793. "\n\\pagecolor{bg}\n"
  17794. "\n{\\color{fg}\n"
  17795. string
  17796. "\n}\n"
  17797. "\n\\end{document}\n")))
  17798. (org-latex-compile texfile t)
  17799. (if (not (file-exists-p pdffile))
  17800. (progn (message "Failed to create pdf file from %s" texfile) nil)
  17801. (ignore-errors
  17802. (if (featurep 'xemacs)
  17803. (call-process "convert" nil nil nil
  17804. "-density" "96"
  17805. "-trim"
  17806. "-antialias"
  17807. pdffile
  17808. "-quality" "100"
  17809. ;; "-sharpen" "0x1.0"
  17810. pngfile)
  17811. (call-process "convert" nil nil nil
  17812. "-density" dpi
  17813. "-trim"
  17814. "-antialias"
  17815. pdffile
  17816. "-quality" "100"
  17817. ;; "-sharpen" "0x1.0"
  17818. pngfile)))
  17819. (if (not (file-exists-p pngfile))
  17820. (if org-format-latex-signal-error
  17821. (error "Failed to create png file from %s" texfile)
  17822. (message "Failed to create png file from %s" texfile)
  17823. nil)
  17824. ;; Use the requested file name and clean up
  17825. (copy-file pngfile tofile 'replace)
  17826. (dolist (e '(".pdf" ".tex" ".aux" ".log" ".png"))
  17827. (when (file-exists-p (concat texfilebase e))
  17828. (delete-file (concat texfilebase e))))
  17829. pngfile))))
  17830. (defun org-splice-latex-header (tpl def-pkg pkg snippets-p &optional extra)
  17831. "Fill a LaTeX header template TPL.
  17832. In the template, the following place holders will be recognized:
  17833. [DEFAULT-PACKAGES] \\usepackage statements for DEF-PKG
  17834. [NO-DEFAULT-PACKAGES] do not include DEF-PKG
  17835. [PACKAGES] \\usepackage statements for PKG
  17836. [NO-PACKAGES] do not include PKG
  17837. [EXTRA] the string EXTRA
  17838. [NO-EXTRA] do not include EXTRA
  17839. For backward compatibility, if both the positive and the negative place
  17840. holder is missing, the positive one (without the \"NO-\") will be
  17841. assumed to be present at the end of the template.
  17842. DEF-PKG and PKG are assumed to be alists of options/packagename lists.
  17843. EXTRA is a string.
  17844. SNIPPETS-P indicates if this is run to create snippet images for HTML."
  17845. (let (rpl (end ""))
  17846. (if (string-match "^[ \t]*\\[\\(NO-\\)?DEFAULT-PACKAGES\\][ \t]*\n?" tpl)
  17847. (setq rpl (if (or (match-end 1) (not def-pkg))
  17848. "" (org-latex-packages-to-string def-pkg snippets-p t))
  17849. tpl (replace-match rpl t t tpl))
  17850. (when def-pkg (setq end (org-latex-packages-to-string def-pkg snippets-p))))
  17851. (if (string-match "\\[\\(NO-\\)?PACKAGES\\][ \t]*\n?" tpl)
  17852. (setq rpl (if (or (match-end 1) (not pkg))
  17853. "" (org-latex-packages-to-string pkg snippets-p t))
  17854. tpl (replace-match rpl t t tpl))
  17855. (when pkg (setq end
  17856. (concat end "\n"
  17857. (org-latex-packages-to-string pkg snippets-p)))))
  17858. (if (string-match "\\[\\(NO-\\)?EXTRA\\][ \t]*\n?" tpl)
  17859. (setq rpl (if (or (match-end 1) (not extra))
  17860. "" (concat extra "\n"))
  17861. tpl (replace-match rpl t t tpl))
  17862. (when (and extra (string-match "\\S-" extra))
  17863. (setq end (concat end "\n" extra))))
  17864. (if (string-match "\\S-" end)
  17865. (concat tpl "\n" end)
  17866. tpl)))
  17867. (defun org-latex-packages-to-string (pkg &optional snippets-p newline)
  17868. "Turn an alist of packages into a string with the \\usepackage macros."
  17869. (setq pkg (mapconcat (lambda(p)
  17870. (cond
  17871. ((stringp p) p)
  17872. ((and snippets-p (>= (length p) 3) (not (nth 2 p)))
  17873. (format "%% Package %s omitted" (cadr p)))
  17874. ((equal "" (car p))
  17875. (format "\\usepackage{%s}" (cadr p)))
  17876. (t
  17877. (format "\\usepackage[%s]{%s}"
  17878. (car p) (cadr p)))))
  17879. pkg
  17880. "\n"))
  17881. (if newline (concat pkg "\n") pkg))
  17882. (defun org-dvipng-color (attr)
  17883. "Return a RGB color specification for dvipng."
  17884. (apply 'format "rgb %s %s %s"
  17885. (mapcar 'org-normalize-color
  17886. (if (featurep 'xemacs)
  17887. (color-rgb-components
  17888. (face-property 'default
  17889. (cond ((eq attr :foreground) 'foreground)
  17890. ((eq attr :background) 'background))))
  17891. (color-values (face-attribute 'default attr nil))))))
  17892. (defun org-dvipng-color-format (color-name)
  17893. "Convert COLOR-NAME to a RGB color value for dvipng."
  17894. (apply 'format "rgb %s %s %s"
  17895. (mapcar 'org-normalize-color
  17896. (color-values color-name))))
  17897. (defun org-latex-color (attr)
  17898. "Return a RGB color for the LaTeX color package."
  17899. (apply 'format "%s,%s,%s"
  17900. (mapcar 'org-normalize-color
  17901. (if (featurep 'xemacs)
  17902. (color-rgb-components
  17903. (face-property 'default
  17904. (cond ((eq attr :foreground) 'foreground)
  17905. ((eq attr :background) 'background))))
  17906. (color-values (face-attribute 'default attr nil))))))
  17907. (defun org-latex-color-format (color-name)
  17908. "Convert COLOR-NAME to a RGB color value."
  17909. (apply 'format "%s,%s,%s"
  17910. (mapcar 'org-normalize-color
  17911. (color-values color-name))))
  17912. (defun org-normalize-color (value)
  17913. "Return string to be used as color value for an RGB component."
  17914. (format "%g" (/ value 65535.0)))
  17915. ;; Image display
  17916. (defvar-local org-inline-image-overlays nil)
  17917. (defun org-toggle-inline-images (&optional include-linked)
  17918. "Toggle the display of inline images.
  17919. INCLUDE-LINKED is passed to `org-display-inline-images'."
  17920. (interactive "P")
  17921. (if org-inline-image-overlays
  17922. (progn
  17923. (org-remove-inline-images)
  17924. (when (org-called-interactively-p 'interactive)
  17925. (message "Inline image display turned off")))
  17926. (org-display-inline-images include-linked)
  17927. (when (org-called-interactively-p 'interactive)
  17928. (message (if org-inline-image-overlays
  17929. (format "%d images displayed inline"
  17930. (length org-inline-image-overlays))
  17931. "No images to display inline")))))
  17932. (defun org-redisplay-inline-images ()
  17933. "Refresh the display of inline images."
  17934. (interactive)
  17935. (if (not org-inline-image-overlays)
  17936. (org-toggle-inline-images)
  17937. (org-toggle-inline-images)
  17938. (org-toggle-inline-images)))
  17939. (defun org-display-inline-images (&optional include-linked refresh beg end)
  17940. "Display inline images.
  17941. An inline image is a link which follows either of these
  17942. conventions:
  17943. 1. Its path is a file with an extension matching return value
  17944. from `image-file-name-regexp' and it has no contents.
  17945. 2. Its description consists in a single link of the previous
  17946. type.
  17947. When optional argument INCLUDE-LINKED is non-nil, also links with
  17948. a text description part will be inlined. This can be nice for
  17949. a quick look at those images, but it does not reflect what
  17950. exported files will look like.
  17951. When optional argument REFRESH is non-nil, refresh existing
  17952. images between BEG and END. This will create new image displays
  17953. only if necessary. BEG and END default to the buffer
  17954. boundaries."
  17955. (interactive "P")
  17956. (when (display-graphic-p)
  17957. (unless refresh
  17958. (org-remove-inline-images)
  17959. (when (fboundp 'clear-image-cache) (clear-image-cache)))
  17960. (org-with-wide-buffer
  17961. (goto-char (or beg (point-min)))
  17962. (let ((case-fold-search t)
  17963. (file-extension-re (image-file-name-regexp)))
  17964. (while (re-search-forward "[][]\\[\\(?:file\\|[./~]\\)" end t)
  17965. (let ((link (save-match-data (org-element-context))))
  17966. ;; Check if we're at an inline image.
  17967. (when (and (equal (org-element-property :type link) "file")
  17968. (or include-linked
  17969. (not (org-element-property :contents-begin link)))
  17970. (let ((parent (org-element-property :parent link)))
  17971. (or (not (eq (org-element-type parent) 'link))
  17972. (not (cdr (org-element-contents parent)))))
  17973. (org-string-match-p file-extension-re
  17974. (org-element-property :path link)))
  17975. (let ((file (expand-file-name
  17976. (org-link-unescape
  17977. (org-element-property :path link)))))
  17978. (when (file-exists-p file)
  17979. (let ((width
  17980. ;; Apply `org-image-actual-width' specifications.
  17981. (cond
  17982. ((not (image-type-available-p 'imagemagick)) nil)
  17983. ((eq org-image-actual-width t) nil)
  17984. ((listp org-image-actual-width)
  17985. (or
  17986. ;; First try to find a width among
  17987. ;; attributes associated to the paragraph
  17988. ;; containing link.
  17989. (let ((paragraph
  17990. (let ((e link))
  17991. (while (and (setq e (org-element-property
  17992. :parent e))
  17993. (not (eq (org-element-type e)
  17994. 'paragraph))))
  17995. e)))
  17996. (when paragraph
  17997. (save-excursion
  17998. (goto-char (org-element-property :begin paragraph))
  17999. (when
  18000. (re-search-forward
  18001. "^[ \t]*#\\+attr_.*?: +.*?:width +\\(\\S-+\\)"
  18002. (org-element-property
  18003. :post-affiliated paragraph)
  18004. t)
  18005. (string-to-number (match-string 1))))))
  18006. ;; Otherwise, fall-back to provided number.
  18007. (car org-image-actual-width)))
  18008. ((numberp org-image-actual-width)
  18009. org-image-actual-width)))
  18010. (old (get-char-property-and-overlay
  18011. (org-element-property :begin link)
  18012. 'org-image-overlay)))
  18013. (if (and (car-safe old) refresh)
  18014. (image-refresh (overlay-get (cdr old) 'display))
  18015. (let ((image (create-image file
  18016. (and width 'imagemagick)
  18017. nil
  18018. :width width)))
  18019. (when image
  18020. (let* ((link
  18021. ;; If inline image is the description
  18022. ;; of another link, be sure to
  18023. ;; consider the latter as the one to
  18024. ;; apply the overlay on.
  18025. (let ((parent
  18026. (org-element-property :parent link)))
  18027. (if (eq (org-element-type parent) 'link)
  18028. parent
  18029. link)))
  18030. (ov (make-overlay
  18031. (org-element-property :begin link)
  18032. (progn
  18033. (goto-char
  18034. (org-element-property :end link))
  18035. (skip-chars-backward " \t")
  18036. (point)))))
  18037. (overlay-put ov 'display image)
  18038. (overlay-put ov 'face 'default)
  18039. (overlay-put ov 'org-image-overlay t)
  18040. (overlay-put
  18041. ov 'modification-hooks
  18042. (list 'org-display-inline-remove-overlay))
  18043. (push ov org-inline-image-overlays)))))))))))))))
  18044. (define-obsolete-function-alias
  18045. 'org-display-inline-modification-hook 'org-display-inline-remove-overlay "24.3")
  18046. (defun org-display-inline-remove-overlay (ov after _beg _end &optional _len)
  18047. "Remove inline-display overlay if a corresponding region is modified."
  18048. (let ((inhibit-modification-hooks t))
  18049. (when (and ov after)
  18050. (delete ov org-inline-image-overlays)
  18051. (delete-overlay ov))))
  18052. (defun org-remove-inline-images ()
  18053. "Remove inline display of images."
  18054. (interactive)
  18055. (mapc #'delete-overlay org-inline-image-overlays)
  18056. (setq org-inline-image-overlays nil))
  18057. ;;;; Key bindings
  18058. ;; Outline functions from `outline-mode-prefix-map'
  18059. ;; that can be remapped in Org:
  18060. (define-key org-mode-map [remap outline-mark-subtree] 'org-mark-subtree)
  18061. (define-key org-mode-map [remap outline-show-subtree] 'org-show-subtree)
  18062. (define-key org-mode-map [remap outline-forward-same-level]
  18063. 'org-forward-heading-same-level)
  18064. (define-key org-mode-map [remap outline-backward-same-level]
  18065. 'org-backward-heading-same-level)
  18066. (define-key org-mode-map [remap outline-show-branches]
  18067. 'org-kill-note-or-show-branches)
  18068. (define-key org-mode-map [remap outline-promote] 'org-promote-subtree)
  18069. (define-key org-mode-map [remap outline-demote] 'org-demote-subtree)
  18070. (define-key org-mode-map [remap outline-insert-heading] 'org-ctrl-c-ret)
  18071. (define-key org-mode-map [remap outline-next-visible-heading]
  18072. 'org-next-visible-heading)
  18073. (define-key org-mode-map [remap outline-previous-visible-heading]
  18074. 'org-previous-visible-heading)
  18075. (define-key org-mode-map [remap show-children] 'org-show-children)
  18076. ;; Outline functions from `outline-mode-prefix-map' that can not
  18077. ;; be remapped in Org:
  18078. ;; - the column "key binding" shows whether the Outline function is still
  18079. ;; available in Org mode on the same key that it has been bound to in
  18080. ;; Outline mode:
  18081. ;; - "overridden": key used for a different functionality in Org mode
  18082. ;; - else: key still bound to the same Outline function in Org mode
  18083. ;; | Outline function | key binding | Org replacement |
  18084. ;; |------------------------------------+-------------+--------------------------|
  18085. ;; | `outline-up-heading' | `C-c C-u' | still same function |
  18086. ;; | `outline-move-subtree-up' | overridden | better: org-shiftup |
  18087. ;; | `outline-move-subtree-down' | overridden | better: org-shiftdown |
  18088. ;; | `show-entry' | overridden | no replacement |
  18089. ;; | `show-branches' | `C-c C-k' | still same function |
  18090. ;; | `show-subtree' | overridden | visibility cycling |
  18091. ;; | `show-all' | overridden | no replacement |
  18092. ;; | `hide-subtree' | overridden | visibility cycling |
  18093. ;; | `hide-body' | overridden | no replacement |
  18094. ;; | `hide-entry' | overridden | visibility cycling |
  18095. ;; | `hide-leaves' | overridden | no replacement |
  18096. ;; | `hide-sublevels' | overridden | no replacement |
  18097. ;; | `hide-other' | overridden | no replacement |
  18098. ;; Make `C-c C-x' a prefix key
  18099. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  18100. ;; TAB key with modifiers
  18101. (org-defkey org-mode-map "\C-i" 'org-cycle)
  18102. (org-defkey org-mode-map [(tab)] 'org-cycle)
  18103. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  18104. (org-defkey org-mode-map "\M-\t" #'pcomplete)
  18105. ;; The following line is necessary under Suse GNU/Linux
  18106. (unless (featurep 'xemacs)
  18107. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  18108. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  18109. (define-key org-mode-map [backtab] 'org-shifttab)
  18110. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  18111. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  18112. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  18113. ;; Cursor keys with modifiers
  18114. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  18115. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  18116. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  18117. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  18118. (org-defkey org-mode-map [(control meta shift right)] 'org-increase-number-at-point)
  18119. (org-defkey org-mode-map [(control meta shift left)] 'org-decrease-number-at-point)
  18120. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  18121. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  18122. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  18123. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  18124. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  18125. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  18126. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  18127. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  18128. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  18129. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  18130. (org-defkey org-mode-map [(control shift up)] 'org-shiftcontrolup)
  18131. (org-defkey org-mode-map [(control shift down)] 'org-shiftcontroldown)
  18132. ;; Babel keys
  18133. (define-key org-mode-map org-babel-key-prefix org-babel-map)
  18134. (dolist (pair org-babel-key-bindings)
  18135. (define-key org-babel-map (car pair) (cdr pair)))
  18136. ;;; Extra keys for tty access.
  18137. ;; We only set them when really needed because otherwise the
  18138. ;; menus don't show the simple keys
  18139. (when (or org-use-extra-keys
  18140. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  18141. (not window-system))
  18142. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  18143. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  18144. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  18145. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  18146. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  18147. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  18148. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  18149. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  18150. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  18151. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  18152. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  18153. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  18154. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  18155. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  18156. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  18157. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  18158. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  18159. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  18160. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  18161. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  18162. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  18163. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  18164. (org-defkey org-mode-map [?\e (tab)] #'pcomplete)
  18165. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  18166. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  18167. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  18168. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  18169. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  18170. ;; All the other keys
  18171. (org-defkey org-mode-map "\C-c\C-a" 'outline-show-all) ; in case allout messed up.
  18172. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  18173. (if (boundp 'narrow-map)
  18174. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  18175. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  18176. (if (boundp 'narrow-map)
  18177. (org-defkey narrow-map "b" 'org-narrow-to-block)
  18178. (org-defkey org-mode-map "\C-xnb" 'org-narrow-to-block))
  18179. (if (boundp 'narrow-map)
  18180. (org-defkey narrow-map "e" 'org-narrow-to-element)
  18181. (org-defkey org-mode-map "\C-xne" 'org-narrow-to-element))
  18182. (org-defkey org-mode-map "\C-\M-t" 'org-transpose-element)
  18183. (org-defkey org-mode-map "\M-}" 'org-forward-element)
  18184. (org-defkey org-mode-map "\M-{" 'org-backward-element)
  18185. (org-defkey org-mode-map "\C-c\C-^" 'org-up-element)
  18186. (org-defkey org-mode-map "\C-c\C-_" 'org-down-element)
  18187. (org-defkey org-mode-map "\C-c\C-f" 'org-forward-heading-same-level)
  18188. (org-defkey org-mode-map "\C-c\C-b" 'org-backward-heading-same-level)
  18189. (org-defkey org-mode-map "\C-c\M-f" 'org-next-block)
  18190. (org-defkey org-mode-map "\C-c\M-b" 'org-previous-block)
  18191. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  18192. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-archive-subtree)
  18193. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
  18194. (org-defkey org-mode-map "\C-c\C-xd" 'org-insert-drawer)
  18195. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  18196. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  18197. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  18198. (org-defkey org-mode-map "\C-c\C-xq" 'org-toggle-tags-groups)
  18199. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  18200. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  18201. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  18202. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  18203. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  18204. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  18205. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  18206. (org-defkey org-mode-map "\C-c\M-w" 'org-copy)
  18207. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  18208. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  18209. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  18210. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  18211. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  18212. (org-defkey org-mode-map "\C-c\C-xv" 'org-copy-visible)
  18213. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  18214. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  18215. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  18216. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  18217. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  18218. (org-defkey org-mode-map "\C-c\M-l" 'org-insert-last-stored-link)
  18219. (org-defkey org-mode-map "\C-c\C-\M-l" 'org-insert-all-links)
  18220. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  18221. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  18222. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  18223. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  18224. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  18225. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  18226. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  18227. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  18228. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  18229. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  18230. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  18231. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  18232. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  18233. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  18234. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  18235. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  18236. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  18237. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  18238. (org-defkey org-mode-map "\C-c^" 'org-sort)
  18239. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  18240. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  18241. (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
  18242. (org-defkey org-mode-map [remap open-line] 'org-open-line)
  18243. (org-defkey org-mode-map [remap comment-dwim] 'org-comment-dwim)
  18244. (org-defkey org-mode-map [remap forward-paragraph] 'org-forward-paragraph)
  18245. (org-defkey org-mode-map [remap backward-paragraph] 'org-backward-paragraph)
  18246. (org-defkey org-mode-map "\M-^" 'org-delete-indentation)
  18247. (org-defkey org-mode-map "\C-m" 'org-return)
  18248. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  18249. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  18250. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  18251. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  18252. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  18253. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  18254. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  18255. (org-defkey org-mode-map "\C-c\"a" 'orgtbl-ascii-plot)
  18256. (org-defkey org-mode-map "\C-c\"g" 'org-plot/gnuplot)
  18257. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  18258. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  18259. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  18260. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  18261. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  18262. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  18263. (org-defkey org-mode-map "\C-c\C-e" 'org-export-dispatch)
  18264. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width)
  18265. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  18266. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  18267. (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
  18268. (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
  18269. (org-defkey org-mode-map "\C-c@" 'org-mark-subtree)
  18270. (org-defkey org-mode-map "\M-h" 'org-mark-element)
  18271. (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
  18272. ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
  18273. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  18274. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  18275. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  18276. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  18277. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  18278. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-in-last)
  18279. (org-defkey org-mode-map "\C-c\C-x\C-z" 'org-resolve-clocks)
  18280. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  18281. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  18282. (org-defkey org-mode-map "\C-c\C-x\C-q" 'org-clock-cancel)
  18283. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  18284. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  18285. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  18286. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-toggle-latex-fragment)
  18287. (org-defkey org-mode-map "\C-c\C-x\C-v" 'org-toggle-inline-images)
  18288. (org-defkey org-mode-map "\C-c\C-x\C-\M-v" 'org-redisplay-inline-images)
  18289. (org-defkey org-mode-map "\C-c\C-x\\" 'org-toggle-pretty-entities)
  18290. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  18291. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  18292. (org-defkey org-mode-map "\C-c\C-xP" 'org-set-property-and-value)
  18293. (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
  18294. (org-defkey org-mode-map "\C-c\C-xE" 'org-inc-effort)
  18295. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  18296. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  18297. (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
  18298. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  18299. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  18300. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  18301. (org-defkey org-mode-map "\C-c\C-x_" 'org-timer-stop)
  18302. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  18303. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  18304. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  18305. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  18306. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  18307. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  18308. (when (featurep 'xemacs)
  18309. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  18310. (defconst org-speed-commands-default
  18311. '(
  18312. ("Outline Navigation")
  18313. ("n" . (org-speed-move-safe 'org-next-visible-heading))
  18314. ("p" . (org-speed-move-safe 'org-previous-visible-heading))
  18315. ("f" . (org-speed-move-safe 'org-forward-heading-same-level))
  18316. ("b" . (org-speed-move-safe 'org-backward-heading-same-level))
  18317. ("F" . org-next-block)
  18318. ("B" . org-previous-block)
  18319. ("u" . (org-speed-move-safe 'outline-up-heading))
  18320. ("j" . org-goto)
  18321. ("g" . (org-refile t))
  18322. ("Outline Visibility")
  18323. ("c" . org-cycle)
  18324. ("C" . org-shifttab)
  18325. (" " . org-display-outline-path)
  18326. ("s" . org-narrow-to-subtree)
  18327. ("=" . org-columns)
  18328. ("Outline Structure Editing")
  18329. ("U" . org-metaup)
  18330. ("D" . org-metadown)
  18331. ("r" . org-metaright)
  18332. ("l" . org-metaleft)
  18333. ("R" . org-shiftmetaright)
  18334. ("L" . org-shiftmetaleft)
  18335. ("i" . (progn (forward-char 1) (call-interactively
  18336. 'org-insert-heading-respect-content)))
  18337. ("^" . org-sort)
  18338. ("w" . org-refile)
  18339. ("a" . org-archive-subtree-default-with-confirmation)
  18340. ("@" . org-mark-subtree)
  18341. ("#" . org-toggle-comment)
  18342. ("Clock Commands")
  18343. ("I" . org-clock-in)
  18344. ("O" . org-clock-out)
  18345. ("Meta Data Editing")
  18346. ("t" . org-todo)
  18347. ("," . (org-priority))
  18348. ("0" . (org-priority ?\ ))
  18349. ("1" . (org-priority ?A))
  18350. ("2" . (org-priority ?B))
  18351. ("3" . (org-priority ?C))
  18352. (":" . org-set-tags-command)
  18353. ("e" . org-set-effort)
  18354. ("E" . org-inc-effort)
  18355. ("W" . (lambda(m) (interactive "sMinutes before warning: ")
  18356. (org-entry-put (point) "APPT_WARNTIME" m)))
  18357. ("Agenda Views etc")
  18358. ("v" . org-agenda)
  18359. ("/" . org-sparse-tree)
  18360. ("Misc")
  18361. ("o" . org-open-at-point)
  18362. ("?" . org-speed-command-help)
  18363. ("<" . (org-agenda-set-restriction-lock 'subtree))
  18364. (">" . (org-agenda-remove-restriction-lock))
  18365. )
  18366. "The default speed commands.")
  18367. (defun org-print-speed-command (e)
  18368. (if (> (length (car e)) 1)
  18369. (progn
  18370. (princ "\n")
  18371. (princ (car e))
  18372. (princ "\n")
  18373. (princ (make-string (length (car e)) ?-))
  18374. (princ "\n"))
  18375. (princ (car e))
  18376. (princ " ")
  18377. (if (symbolp (cdr e))
  18378. (princ (symbol-name (cdr e)))
  18379. (prin1 (cdr e)))
  18380. (princ "\n")))
  18381. (defun org-speed-command-help ()
  18382. "Show the available speed commands."
  18383. (interactive)
  18384. (if (not org-use-speed-commands)
  18385. (user-error "Speed commands are not activated, customize `org-use-speed-commands'")
  18386. (with-output-to-temp-buffer "*Help*"
  18387. (princ "User-defined Speed commands\n===========================\n")
  18388. (mapc #'org-print-speed-command org-speed-commands-user)
  18389. (princ "\n")
  18390. (princ "Built-in Speed commands\n=======================\n")
  18391. (mapc #'org-print-speed-command org-speed-commands-default))
  18392. (with-current-buffer "*Help*"
  18393. (setq truncate-lines t))))
  18394. (defun org-speed-move-safe (cmd)
  18395. "Execute CMD, but make sure that the cursor always ends up in a headline.
  18396. If not, return to the original position and throw an error."
  18397. (interactive)
  18398. (let ((pos (point)))
  18399. (call-interactively cmd)
  18400. (unless (and (bolp) (org-at-heading-p))
  18401. (goto-char pos)
  18402. (error "Boundary reached while executing %s" cmd))))
  18403. (defvar org-self-insert-command-undo-counter 0)
  18404. (defvar org-table-auto-blank-field) ; defined in org-table.el
  18405. (defvar org-speed-command nil)
  18406. (define-obsolete-function-alias
  18407. 'org-speed-command-default-hook 'org-speed-command-activate "24.3")
  18408. (defun org-speed-command-activate (keys)
  18409. "Hook for activating single-letter speed commands.
  18410. `org-speed-commands-default' specifies a minimal command set.
  18411. Use `org-speed-commands-user' for further customization."
  18412. (when (or (and (bolp) (looking-at org-outline-regexp))
  18413. (and (functionp org-use-speed-commands)
  18414. (funcall org-use-speed-commands)))
  18415. (cdr (assoc keys (append org-speed-commands-user
  18416. org-speed-commands-default)))))
  18417. (define-obsolete-function-alias
  18418. 'org-babel-speed-command-hook 'org-babel-speed-command-activate "24.3")
  18419. (defun org-babel-speed-command-activate (keys)
  18420. "Hook for activating single-letter code block commands."
  18421. (when (and (bolp) (looking-at org-babel-src-block-regexp))
  18422. (cdr (assoc keys org-babel-key-bindings))))
  18423. (defcustom org-speed-command-hook
  18424. '(org-speed-command-default-hook org-babel-speed-command-hook)
  18425. "Hook for activating speed commands at strategic locations.
  18426. Hook functions are called in sequence until a valid handler is
  18427. found.
  18428. Each hook takes a single argument, a user-pressed command key
  18429. which is also a `self-insert-command' from the global map.
  18430. Within the hook, examine the cursor position and the command key
  18431. and return nil or a valid handler as appropriate. Handler could
  18432. be one of an interactive command, a function, or a form.
  18433. Set `org-use-speed-commands' to non-nil value to enable this
  18434. hook. The default setting is `org-speed-command-activate'."
  18435. :group 'org-structure
  18436. :version "24.1"
  18437. :type 'hook)
  18438. (defun org-self-insert-command (N)
  18439. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  18440. If the cursor is in a table looking at whitespace, the whitespace is
  18441. overwritten, and the table is not marked as requiring realignment."
  18442. (interactive "p")
  18443. (org-check-before-invisible-edit 'insert)
  18444. (cond
  18445. ((and org-use-speed-commands
  18446. (let ((kv (this-command-keys-vector)))
  18447. (setq org-speed-command
  18448. (run-hook-with-args-until-success
  18449. 'org-speed-command-hook
  18450. (make-string 1 (aref kv (1- (length kv))))))))
  18451. (cond
  18452. ((commandp org-speed-command)
  18453. (setq this-command org-speed-command)
  18454. (call-interactively org-speed-command))
  18455. ((functionp org-speed-command)
  18456. (funcall org-speed-command))
  18457. ((and org-speed-command (listp org-speed-command))
  18458. (eval org-speed-command))
  18459. (t (let (org-use-speed-commands)
  18460. (call-interactively 'org-self-insert-command)))))
  18461. ((and
  18462. (org-at-table-p)
  18463. (progn
  18464. ;; Check if we blank the field, and if that triggers align.
  18465. (and (featurep 'org-table) org-table-auto-blank-field
  18466. (memq last-command
  18467. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
  18468. (if (or (eq (char-after) ?\s) (looking-at "[^|\n]* |"))
  18469. ;; Got extra space, this field does not determine
  18470. ;; column width.
  18471. (let (org-table-may-need-update) (org-table-blank-field))
  18472. ;; No extra space, this field may determine column
  18473. ;; width.
  18474. (org-table-blank-field)))
  18475. t)
  18476. (eq N 1)
  18477. (looking-at "[^|\n]* \\( \\)|"))
  18478. ;; There is room for insertion without re-aligning the table.
  18479. (delete-region (match-beginning 1) (match-end 1))
  18480. (self-insert-command N))
  18481. (t
  18482. (setq org-table-may-need-update t)
  18483. (self-insert-command N)
  18484. (org-fix-tags-on-the-fly)
  18485. (when org-self-insert-cluster-for-undo
  18486. (if (not (eq last-command 'org-self-insert-command))
  18487. (setq org-self-insert-command-undo-counter 1)
  18488. (if (>= org-self-insert-command-undo-counter 20)
  18489. (setq org-self-insert-command-undo-counter 1)
  18490. (and (> org-self-insert-command-undo-counter 0)
  18491. buffer-undo-list (listp buffer-undo-list)
  18492. (not (cadr buffer-undo-list)) ; remove nil entry
  18493. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  18494. (setq org-self-insert-command-undo-counter
  18495. (1+ org-self-insert-command-undo-counter))))))))
  18496. (defun org-check-before-invisible-edit (kind)
  18497. "Check is editing if kind KIND would be dangerous with invisible text around.
  18498. The detailed reaction depends on the user option `org-catch-invisible-edits'."
  18499. ;; First, try to get out of here as quickly as possible, to reduce overhead
  18500. (when (and org-catch-invisible-edits
  18501. (or (not (boundp 'visible-mode)) (not visible-mode))
  18502. (or (get-char-property (point) 'invisible)
  18503. (get-char-property (max (point-min) (1- (point))) 'invisible)))
  18504. ;; OK, we need to take a closer look
  18505. (let* ((invisible-at-point (get-char-property (point) 'invisible))
  18506. (invisible-before-point (unless (bobp) (get-char-property
  18507. (1- (point)) 'invisible)))
  18508. (border-and-ok-direction
  18509. (or
  18510. ;; Check if we are acting predictably before invisible text
  18511. (and invisible-at-point (not invisible-before-point)
  18512. (memq kind '(insert delete-backward)))
  18513. ;; Check if we are acting predictably after invisible text
  18514. ;; This works not well, and I have turned it off. It seems
  18515. ;; better to always show and stop after invisible text.
  18516. ;; (and (not invisible-at-point) invisible-before-point
  18517. ;; (memq kind '(insert delete)))
  18518. )))
  18519. (when (or (memq invisible-at-point '(outline org-hide-block t))
  18520. (memq invisible-before-point '(outline org-hide-block t)))
  18521. (when (eq org-catch-invisible-edits 'error)
  18522. (user-error "Editing in invisible areas is prohibited, make them visible first"))
  18523. (if (and org-custom-properties-overlays
  18524. (y-or-n-p "Display invisible properties in this buffer? "))
  18525. (org-toggle-custom-properties-visibility)
  18526. ;; Make the area visible
  18527. (save-excursion
  18528. (when invisible-before-point
  18529. (goto-char (previous-single-char-property-change
  18530. (point) 'invisible)))
  18531. (outline-show-subtree))
  18532. (cond
  18533. ((eq org-catch-invisible-edits 'show)
  18534. ;; That's it, we do the edit after showing
  18535. (message
  18536. "Unfolding invisible region around point before editing")
  18537. (sit-for 1))
  18538. ((and (eq org-catch-invisible-edits 'smart)
  18539. border-and-ok-direction)
  18540. (message "Unfolding invisible region around point before editing"))
  18541. (t
  18542. ;; Don't do the edit, make the user repeat it in full visibility
  18543. (user-error "Edit in invisible region aborted, repeat to confirm with text visible"))))))))
  18544. (defun org-fix-tags-on-the-fly ()
  18545. (when (and (equal (char-after (point-at-bol)) ?*)
  18546. (org-at-heading-p))
  18547. (org-align-tags-here org-tags-column)))
  18548. (defun org-delete-backward-char (N)
  18549. "Like `delete-backward-char', insert whitespace at field end in tables.
  18550. When deleting backwards, in tables this function will insert whitespace in
  18551. front of the next \"|\" separator, to keep the table aligned. The table will
  18552. still be marked for re-alignment if the field did fill the entire column,
  18553. because, in this case the deletion might narrow the column."
  18554. (interactive "p")
  18555. (save-match-data
  18556. (org-check-before-invisible-edit 'delete-backward)
  18557. (if (and (org-at-table-p)
  18558. (eq N 1)
  18559. (string-match "|" (buffer-substring (point-at-bol) (point)))
  18560. (looking-at ".*?|"))
  18561. (let ((pos (point))
  18562. (noalign (looking-at "[^|\n\r]* |"))
  18563. (c org-table-may-need-update))
  18564. (backward-delete-char N)
  18565. (unless overwrite-mode
  18566. (skip-chars-forward "^|")
  18567. (insert " ")
  18568. (goto-char (1- pos)))
  18569. ;; noalign: if there were two spaces at the end, this field
  18570. ;; does not determine the width of the column.
  18571. (when noalign (setq org-table-may-need-update c)))
  18572. (backward-delete-char N)
  18573. (org-fix-tags-on-the-fly))))
  18574. (defun org-delete-char (N)
  18575. "Like `delete-char', but insert whitespace at field end in tables.
  18576. When deleting characters, in tables this function will insert whitespace in
  18577. front of the next \"|\" separator, to keep the table aligned. The table will
  18578. still be marked for re-alignment if the field did fill the entire column,
  18579. because, in this case the deletion might narrow the column."
  18580. (interactive "p")
  18581. (save-match-data
  18582. (org-check-before-invisible-edit 'delete)
  18583. (if (and (org-at-table-p)
  18584. (not (bolp))
  18585. (not (= (char-after) ?|))
  18586. (eq N 1))
  18587. (if (looking-at ".*?|")
  18588. (let ((pos (point))
  18589. (noalign (looking-at "[^|\n\r]* |"))
  18590. (c org-table-may-need-update))
  18591. (replace-match
  18592. (concat (substring (match-string 0) 1 -1) " |") nil t)
  18593. (goto-char pos)
  18594. ;; noalign: if there were two spaces at the end, this field
  18595. ;; does not determine the width of the column.
  18596. (when noalign (setq org-table-may-need-update c)))
  18597. (delete-char N))
  18598. (delete-char N)
  18599. (org-fix-tags-on-the-fly))))
  18600. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  18601. (put 'org-self-insert-command 'delete-selection
  18602. (lambda ()
  18603. (not (run-hook-with-args-until-success
  18604. 'self-insert-uses-region-functions))))
  18605. (put 'orgtbl-self-insert-command 'delete-selection
  18606. (lambda ()
  18607. (not (run-hook-with-args-until-success
  18608. 'self-insert-uses-region-functions))))
  18609. (put 'org-delete-char 'delete-selection 'supersede)
  18610. (put 'org-delete-backward-char 'delete-selection 'supersede)
  18611. (put 'org-yank 'delete-selection 'yank)
  18612. ;; Make `flyspell-mode' delay after some commands
  18613. (put 'org-self-insert-command 'flyspell-delayed t)
  18614. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  18615. (put 'org-delete-char 'flyspell-delayed t)
  18616. (put 'org-delete-backward-char 'flyspell-delayed t)
  18617. ;; Make pabbrev-mode expand after org-mode commands
  18618. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  18619. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  18620. (defun org-remap (map &rest commands)
  18621. "In MAP, remap the functions given in COMMANDS.
  18622. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  18623. (let (new old)
  18624. (while commands
  18625. (setq old (pop commands) new (pop commands))
  18626. (if (fboundp 'command-remapping)
  18627. (org-defkey map (vector 'remap old) new)
  18628. (substitute-key-definition old new map global-map)))))
  18629. (defun org-transpose-words ()
  18630. "Transpose words for Org.
  18631. This uses the `org-mode-transpose-word-syntax-table' syntax
  18632. table, which interprets characters in `org-emphasis-alist' as
  18633. word constituents."
  18634. (interactive)
  18635. (with-syntax-table org-mode-transpose-word-syntax-table
  18636. (call-interactively 'transpose-words)))
  18637. (org-remap org-mode-map 'transpose-words 'org-transpose-words)
  18638. (when (eq org-enable-table-editor 'optimized)
  18639. ;; If the user wants maximum table support, we need to hijack
  18640. ;; some standard editing functions
  18641. (org-remap org-mode-map
  18642. 'self-insert-command 'org-self-insert-command
  18643. 'delete-char 'org-delete-char
  18644. 'delete-backward-char 'org-delete-backward-char)
  18645. (org-defkey org-mode-map "|" 'org-force-self-insert))
  18646. (defvar org-ctrl-c-ctrl-c-hook nil
  18647. "Hook for functions attaching themselves to `C-c C-c'.
  18648. This can be used to add additional functionality to the C-c C-c
  18649. key which executes context-dependent commands. This hook is run
  18650. before any other test, while `org-ctrl-c-ctrl-c-final-hook' is
  18651. run after the last test.
  18652. Each function will be called with no arguments. The function
  18653. must check if the context is appropriate for it to act. If yes,
  18654. it should do its thing and then return a non-nil value. If the
  18655. context is wrong, just do nothing and return nil.")
  18656. (defvar org-ctrl-c-ctrl-c-final-hook nil
  18657. "Hook for functions attaching themselves to `C-c C-c'.
  18658. This can be used to add additional functionality to the C-c C-c
  18659. key which executes context-dependent commands. This hook is run
  18660. after any other test, while `org-ctrl-c-ctrl-c-hook' is run
  18661. before the first test.
  18662. Each function will be called with no arguments. The function
  18663. must check if the context is appropriate for it to act. If yes,
  18664. it should do its thing and then return a non-nil value. If the
  18665. context is wrong, just do nothing and return nil.")
  18666. (defvar org-tab-first-hook nil
  18667. "Hook for functions to attach themselves to TAB.
  18668. See `org-ctrl-c-ctrl-c-hook' for more information.
  18669. This hook runs as the first action when TAB is pressed, even before
  18670. `org-cycle' messes around with the `outline-regexp' to cater for
  18671. inline tasks and plain list item folding.
  18672. If any function in this hook returns t, any other actions that
  18673. would have been caused by TAB (such as table field motion or visibility
  18674. cycling) will not occur.")
  18675. (defvar org-tab-after-check-for-table-hook nil
  18676. "Hook for functions to attach themselves to TAB.
  18677. See `org-ctrl-c-ctrl-c-hook' for more information.
  18678. This hook runs after it has been established that the cursor is not in a
  18679. table, but before checking if the cursor is in a headline or if global cycling
  18680. should be done.
  18681. If any function in this hook returns t, not other actions like visibility
  18682. cycling will be done.")
  18683. (defvar org-tab-after-check-for-cycling-hook nil
  18684. "Hook for functions to attach themselves to TAB.
  18685. See `org-ctrl-c-ctrl-c-hook' for more information.
  18686. This hook runs after it has been established that not table field motion and
  18687. not visibility should be done because of current context. This is probably
  18688. the place where a package like yasnippets can hook in.")
  18689. (defvar org-tab-before-tab-emulation-hook nil
  18690. "Hook for functions to attach themselves to TAB.
  18691. See `org-ctrl-c-ctrl-c-hook' for more information.
  18692. This hook runs after every other options for TAB have been exhausted, but
  18693. before indentation and \t insertion takes place.")
  18694. (defvar org-metaleft-hook nil
  18695. "Hook for functions attaching themselves to `M-left'.
  18696. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18697. (defvar org-metaright-hook nil
  18698. "Hook for functions attaching themselves to `M-right'.
  18699. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18700. (defvar org-metaup-hook nil
  18701. "Hook for functions attaching themselves to `M-up'.
  18702. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18703. (defvar org-metadown-hook nil
  18704. "Hook for functions attaching themselves to `M-down'.
  18705. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18706. (defvar org-shiftmetaleft-hook nil
  18707. "Hook for functions attaching themselves to `M-S-left'.
  18708. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18709. (defvar org-shiftmetaright-hook nil
  18710. "Hook for functions attaching themselves to `M-S-right'.
  18711. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18712. (defvar org-shiftmetaup-hook nil
  18713. "Hook for functions attaching themselves to `M-S-up'.
  18714. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18715. (defvar org-shiftmetadown-hook nil
  18716. "Hook for functions attaching themselves to `M-S-down'.
  18717. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18718. (defvar org-metareturn-hook nil
  18719. "Hook for functions attaching themselves to `M-RET'.
  18720. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18721. (defvar org-shiftup-hook nil
  18722. "Hook for functions attaching themselves to `S-up'.
  18723. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18724. (defvar org-shiftup-final-hook nil
  18725. "Hook for functions attaching themselves to `S-up'.
  18726. This one runs after all other options except shift-select have been excluded.
  18727. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18728. (defvar org-shiftdown-hook nil
  18729. "Hook for functions attaching themselves to `S-down'.
  18730. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18731. (defvar org-shiftdown-final-hook nil
  18732. "Hook for functions attaching themselves to `S-down'.
  18733. This one runs after all other options except shift-select have been excluded.
  18734. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18735. (defvar org-shiftleft-hook nil
  18736. "Hook for functions attaching themselves to `S-left'.
  18737. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18738. (defvar org-shiftleft-final-hook nil
  18739. "Hook for functions attaching themselves to `S-left'.
  18740. This one runs after all other options except shift-select have been excluded.
  18741. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18742. (defvar org-shiftright-hook nil
  18743. "Hook for functions attaching themselves to `S-right'.
  18744. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18745. (defvar org-shiftright-final-hook nil
  18746. "Hook for functions attaching themselves to `S-right'.
  18747. This one runs after all other options except shift-select have been excluded.
  18748. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18749. (defun org-modifier-cursor-error ()
  18750. "Throw an error, a modified cursor command was applied in wrong context."
  18751. (user-error "This command is active in special context like tables, headlines or items"))
  18752. (defun org-shiftselect-error ()
  18753. "Throw an error because Shift-Cursor command was applied in wrong context."
  18754. (if (and (boundp 'shift-select-mode) shift-select-mode)
  18755. (user-error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
  18756. (user-error "This command works only in special context like headlines or timestamps")))
  18757. (defun org-call-for-shift-select (cmd)
  18758. (let ((this-command-keys-shift-translated t))
  18759. (call-interactively cmd)))
  18760. (defun org-shifttab (&optional arg)
  18761. "Global visibility cycling or move to previous table field.
  18762. Call `org-table-previous-field' within a table.
  18763. When ARG is nil, cycle globally through visibility states.
  18764. When ARG is a numeric prefix, show contents of this level."
  18765. (interactive "P")
  18766. (cond
  18767. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  18768. ((integerp arg)
  18769. (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
  18770. (message "Content view to level: %d" arg)
  18771. (org-content (prefix-numeric-value arg2))
  18772. (org-cycle-show-empty-lines t)
  18773. (setq org-cycle-global-status 'overview)))
  18774. (t (call-interactively 'org-global-cycle))))
  18775. (defun org-shiftmetaleft ()
  18776. "Promote subtree or delete table column.
  18777. Calls `org-promote-subtree', `org-outdent-item-tree', or
  18778. `org-table-delete-column', depending on context. See the
  18779. individual commands for more information."
  18780. (interactive)
  18781. (cond
  18782. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  18783. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  18784. ((org-at-heading-p) (call-interactively 'org-promote-subtree))
  18785. ((if (not (org-region-active-p)) (org-at-item-p)
  18786. (save-excursion (goto-char (region-beginning))
  18787. (org-at-item-p)))
  18788. (call-interactively 'org-outdent-item-tree))
  18789. (t (org-modifier-cursor-error))))
  18790. (defun org-shiftmetaright ()
  18791. "Demote subtree or insert table column.
  18792. Calls `org-demote-subtree', `org-indent-item-tree', or
  18793. `org-table-insert-column', depending on context. See the
  18794. individual commands for more information."
  18795. (interactive)
  18796. (cond
  18797. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  18798. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  18799. ((org-at-heading-p) (call-interactively 'org-demote-subtree))
  18800. ((if (not (org-region-active-p)) (org-at-item-p)
  18801. (save-excursion (goto-char (region-beginning))
  18802. (org-at-item-p)))
  18803. (call-interactively 'org-indent-item-tree))
  18804. (t (org-modifier-cursor-error))))
  18805. (defun org-shiftmetaup (&optional _arg)
  18806. "Drag the line at point up.
  18807. In a table, kill the current row.
  18808. On a clock timestamp, update the value of the timestamp like `S-<up>'
  18809. but also adjust the previous clocked item in the clock history.
  18810. Everywhere else, drag the line at point up."
  18811. (interactive "P")
  18812. (cond
  18813. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  18814. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  18815. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  18816. (call-interactively 'org-timestamp-up)))
  18817. (t (call-interactively 'org-drag-line-backward))))
  18818. (defun org-shiftmetadown (&optional _arg)
  18819. "Drag the line at point down.
  18820. In a table, insert an empty row at the current line.
  18821. On a clock timestamp, update the value of the timestamp like `S-<down>'
  18822. but also adjust the previous clocked item in the clock history.
  18823. Everywhere else, drag the line at point down."
  18824. (interactive "P")
  18825. (cond
  18826. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  18827. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  18828. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  18829. (call-interactively 'org-timestamp-down)))
  18830. (t (call-interactively 'org-drag-line-forward))))
  18831. (defsubst org-hidden-tree-error ()
  18832. (user-error
  18833. "Hidden subtree, open with TAB or use subtree command M-S-<left>/<right>"))
  18834. (defun org-metaleft (&optional _arg)
  18835. "Promote heading, list item at point or move table column left.
  18836. Calls `org-do-promote', `org-outdent-item' or `org-table-move-column',
  18837. depending on context. With no specific context, calls the Emacs
  18838. default `backward-word'. See the individual commands for more
  18839. information.
  18840. This function runs the hook `org-metaleft-hook' as a first step,
  18841. and returns at first non-nil value."
  18842. (interactive "P")
  18843. (cond
  18844. ((run-hook-with-args-until-success 'org-metaleft-hook))
  18845. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  18846. ((org-with-limited-levels
  18847. (or (org-at-heading-p)
  18848. (and (org-region-active-p)
  18849. (save-excursion
  18850. (goto-char (region-beginning))
  18851. (org-at-heading-p)))))
  18852. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  18853. (call-interactively 'org-do-promote))
  18854. ;; At an inline task.
  18855. ((org-at-heading-p)
  18856. (call-interactively 'org-inlinetask-promote))
  18857. ((or (org-at-item-p)
  18858. (and (org-region-active-p)
  18859. (save-excursion
  18860. (goto-char (region-beginning))
  18861. (org-at-item-p))))
  18862. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  18863. (call-interactively 'org-outdent-item))
  18864. (t (call-interactively 'backward-word))))
  18865. (defun org-metaright (&optional _arg)
  18866. "Demote heading, list item at point or move table column right.
  18867. In front of a drawer or a block keyword, indent it correctly.
  18868. Calls `org-do-demote', `org-indent-item', `org-table-move-column',
  18869. `org-indnet-drawer' or `org-indent-block' depending on context.
  18870. With no specific context, calls the Emacs default `forward-word'.
  18871. See the individual commands for more information.
  18872. This function runs the hook `org-metaright-hook' as a first step,
  18873. and returns at first non-nil value."
  18874. (interactive "P")
  18875. (cond
  18876. ((run-hook-with-args-until-success 'org-metaright-hook))
  18877. ((org-at-table-p) (call-interactively 'org-table-move-column))
  18878. ((org-at-drawer-p) (call-interactively 'org-indent-drawer))
  18879. ((org-at-block-p) (call-interactively 'org-indent-block))
  18880. ((org-with-limited-levels
  18881. (or (org-at-heading-p)
  18882. (and (org-region-active-p)
  18883. (save-excursion
  18884. (goto-char (region-beginning))
  18885. (org-at-heading-p)))))
  18886. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  18887. (call-interactively 'org-do-demote))
  18888. ;; At an inline task.
  18889. ((org-at-heading-p)
  18890. (call-interactively 'org-inlinetask-demote))
  18891. ((or (org-at-item-p)
  18892. (and (org-region-active-p)
  18893. (save-excursion
  18894. (goto-char (region-beginning))
  18895. (org-at-item-p))))
  18896. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  18897. (call-interactively 'org-indent-item))
  18898. (t (call-interactively 'forward-word))))
  18899. (defun org-check-for-hidden (what)
  18900. "Check if there are hidden headlines/items in the current visual line.
  18901. WHAT can be either `headlines' or `items'. If the current line is
  18902. an outline or item heading and it has a folded subtree below it,
  18903. this function returns t, nil otherwise."
  18904. (let ((re (cond
  18905. ((eq what 'headlines) org-outline-regexp-bol)
  18906. ((eq what 'items) (org-item-beginning-re))
  18907. (t (error "This should not happen"))))
  18908. beg end)
  18909. (save-excursion
  18910. (catch 'exit
  18911. (unless (org-region-active-p)
  18912. (setq beg (point-at-bol))
  18913. (beginning-of-line 2)
  18914. (while (and (not (eobp)) ;; this is like `next-line'
  18915. (get-char-property (1- (point)) 'invisible))
  18916. (beginning-of-line 2))
  18917. (setq end (point))
  18918. (goto-char beg)
  18919. (goto-char (point-at-eol))
  18920. (setq end (max end (point)))
  18921. (while (re-search-forward re end t)
  18922. (when (get-char-property (match-beginning 0) 'invisible)
  18923. (throw 'exit t))))
  18924. nil))))
  18925. (defun org-metaup (&optional _arg)
  18926. "Move subtree up or move table row up.
  18927. Calls `org-move-subtree-up' or `org-table-move-row' or
  18928. `org-move-item-up', depending on context. See the individual commands
  18929. for more information."
  18930. (interactive "P")
  18931. (cond
  18932. ((run-hook-with-args-until-success 'org-metaup-hook))
  18933. ((org-region-active-p)
  18934. (let* ((a (min (region-beginning) (region-end)))
  18935. (b (1- (max (region-beginning) (region-end))))
  18936. (c (save-excursion (goto-char a)
  18937. (move-beginning-of-line 0)))
  18938. (d (save-excursion (goto-char a)
  18939. (move-end-of-line 0) (point))))
  18940. (transpose-regions a b c d)
  18941. (goto-char c)))
  18942. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  18943. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  18944. ((org-at-item-p) (call-interactively 'org-move-item-up))
  18945. (t (org-drag-element-backward))))
  18946. (defun org-metadown (&optional _arg)
  18947. "Move subtree down or move table row down.
  18948. Calls `org-move-subtree-down' or `org-table-move-row' or
  18949. `org-move-item-down', depending on context. See the individual
  18950. commands for more information."
  18951. (interactive "P")
  18952. (cond
  18953. ((run-hook-with-args-until-success 'org-metadown-hook))
  18954. ((org-region-active-p)
  18955. (let* ((a (min (region-beginning) (region-end)))
  18956. (b (max (region-beginning) (region-end)))
  18957. (c (save-excursion (goto-char b)
  18958. (move-beginning-of-line 1)))
  18959. (d (save-excursion (goto-char b)
  18960. (move-end-of-line 1) (1+ (point)))))
  18961. (transpose-regions a b c d)
  18962. (goto-char d)))
  18963. ((org-at-table-p) (call-interactively 'org-table-move-row))
  18964. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  18965. ((org-at-item-p) (call-interactively 'org-move-item-down))
  18966. (t (org-drag-element-forward))))
  18967. (defun org-shiftup (&optional arg)
  18968. "Increase item in timestamp or increase priority of current headline.
  18969. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  18970. depending on context. See the individual commands for more information."
  18971. (interactive "P")
  18972. (cond
  18973. ((run-hook-with-args-until-success 'org-shiftup-hook))
  18974. ((and org-support-shift-select (org-region-active-p))
  18975. (org-call-for-shift-select 'previous-line))
  18976. ((org-at-timestamp-p t)
  18977. (call-interactively (if org-edit-timestamp-down-means-later
  18978. 'org-timestamp-down 'org-timestamp-up)))
  18979. ((and (not (eq org-support-shift-select 'always))
  18980. org-enable-priority-commands
  18981. (org-at-heading-p))
  18982. (call-interactively 'org-priority-up))
  18983. ((and (not org-support-shift-select) (org-at-item-p))
  18984. (call-interactively 'org-previous-item))
  18985. ((org-clocktable-try-shift 'up arg))
  18986. ((run-hook-with-args-until-success 'org-shiftup-final-hook))
  18987. (org-support-shift-select
  18988. (org-call-for-shift-select 'previous-line))
  18989. (t (org-shiftselect-error))))
  18990. (defun org-shiftdown (&optional arg)
  18991. "Decrease item in timestamp or decrease priority of current headline.
  18992. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  18993. depending on context. See the individual commands for more information."
  18994. (interactive "P")
  18995. (cond
  18996. ((run-hook-with-args-until-success 'org-shiftdown-hook))
  18997. ((and org-support-shift-select (org-region-active-p))
  18998. (org-call-for-shift-select 'next-line))
  18999. ((org-at-timestamp-p t)
  19000. (call-interactively (if org-edit-timestamp-down-means-later
  19001. 'org-timestamp-up 'org-timestamp-down)))
  19002. ((and (not (eq org-support-shift-select 'always))
  19003. org-enable-priority-commands
  19004. (org-at-heading-p))
  19005. (call-interactively 'org-priority-down))
  19006. ((and (not org-support-shift-select) (org-at-item-p))
  19007. (call-interactively 'org-next-item))
  19008. ((org-clocktable-try-shift 'down arg))
  19009. ((run-hook-with-args-until-success 'org-shiftdown-final-hook))
  19010. (org-support-shift-select
  19011. (org-call-for-shift-select 'next-line))
  19012. (t (org-shiftselect-error))))
  19013. (defun org-shiftright (&optional arg)
  19014. "Cycle the thing at point or in the current line, depending on context.
  19015. Depending on context, this does one of the following:
  19016. - switch a timestamp at point one day into the future
  19017. - on a headline, switch to the next TODO keyword.
  19018. - on an item, switch entire list to the next bullet type
  19019. - on a property line, switch to the next allowed value
  19020. - on a clocktable definition line, move time block into the future"
  19021. (interactive "P")
  19022. (cond
  19023. ((run-hook-with-args-until-success 'org-shiftright-hook))
  19024. ((and org-support-shift-select (org-region-active-p))
  19025. (org-call-for-shift-select 'forward-char))
  19026. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  19027. ((and (not (eq org-support-shift-select 'always))
  19028. (org-at-heading-p))
  19029. (let ((org-inhibit-logging
  19030. (not org-treat-S-cursor-todo-selection-as-state-change))
  19031. (org-inhibit-blocking
  19032. (not org-treat-S-cursor-todo-selection-as-state-change)))
  19033. (org-call-with-arg 'org-todo 'right)))
  19034. ((or (and org-support-shift-select
  19035. (not (eq org-support-shift-select 'always))
  19036. (org-at-item-bullet-p))
  19037. (and (not org-support-shift-select) (org-at-item-p)))
  19038. (org-call-with-arg 'org-cycle-list-bullet nil))
  19039. ((and (not (eq org-support-shift-select 'always))
  19040. (org-at-property-p))
  19041. (call-interactively 'org-property-next-allowed-value))
  19042. ((org-clocktable-try-shift 'right arg))
  19043. ((run-hook-with-args-until-success 'org-shiftright-final-hook))
  19044. (org-support-shift-select
  19045. (org-call-for-shift-select 'forward-char))
  19046. (t (org-shiftselect-error))))
  19047. (defun org-shiftleft (&optional arg)
  19048. "Cycle the thing at point or in the current line, depending on context.
  19049. Depending on context, this does one of the following:
  19050. - switch a timestamp at point one day into the past
  19051. - on a headline, switch to the previous TODO keyword.
  19052. - on an item, switch entire list to the previous bullet type
  19053. - on a property line, switch to the previous allowed value
  19054. - on a clocktable definition line, move time block into the past"
  19055. (interactive "P")
  19056. (cond
  19057. ((run-hook-with-args-until-success 'org-shiftleft-hook))
  19058. ((and org-support-shift-select (org-region-active-p))
  19059. (org-call-for-shift-select 'backward-char))
  19060. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  19061. ((and (not (eq org-support-shift-select 'always))
  19062. (org-at-heading-p))
  19063. (let ((org-inhibit-logging
  19064. (not org-treat-S-cursor-todo-selection-as-state-change))
  19065. (org-inhibit-blocking
  19066. (not org-treat-S-cursor-todo-selection-as-state-change)))
  19067. (org-call-with-arg 'org-todo 'left)))
  19068. ((or (and org-support-shift-select
  19069. (not (eq org-support-shift-select 'always))
  19070. (org-at-item-bullet-p))
  19071. (and (not org-support-shift-select) (org-at-item-p)))
  19072. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  19073. ((and (not (eq org-support-shift-select 'always))
  19074. (org-at-property-p))
  19075. (call-interactively 'org-property-previous-allowed-value))
  19076. ((org-clocktable-try-shift 'left arg))
  19077. ((run-hook-with-args-until-success 'org-shiftleft-final-hook))
  19078. (org-support-shift-select
  19079. (org-call-for-shift-select 'backward-char))
  19080. (t (org-shiftselect-error))))
  19081. (defun org-shiftcontrolright ()
  19082. "Switch to next TODO set."
  19083. (interactive)
  19084. (cond
  19085. ((and org-support-shift-select (org-region-active-p))
  19086. (org-call-for-shift-select 'forward-word))
  19087. ((and (not (eq org-support-shift-select 'always))
  19088. (org-at-heading-p))
  19089. (org-call-with-arg 'org-todo 'nextset))
  19090. (org-support-shift-select
  19091. (org-call-for-shift-select 'forward-word))
  19092. (t (org-shiftselect-error))))
  19093. (defun org-shiftcontrolleft ()
  19094. "Switch to previous TODO set."
  19095. (interactive)
  19096. (cond
  19097. ((and org-support-shift-select (org-region-active-p))
  19098. (org-call-for-shift-select 'backward-word))
  19099. ((and (not (eq org-support-shift-select 'always))
  19100. (org-at-heading-p))
  19101. (org-call-with-arg 'org-todo 'previousset))
  19102. (org-support-shift-select
  19103. (org-call-for-shift-select 'backward-word))
  19104. (t (org-shiftselect-error))))
  19105. (defun org-shiftcontrolup (&optional n)
  19106. "Change timestamps synchronously up in CLOCK log lines.
  19107. Optional argument N tells to change by that many units."
  19108. (interactive "P")
  19109. (if (and (org-at-clock-log-p) (org-at-timestamp-p t))
  19110. (let (org-support-shift-select)
  19111. (org-clock-timestamps-up n))
  19112. (user-error "Not at a clock log")))
  19113. (defun org-shiftcontroldown (&optional n)
  19114. "Change timestamps synchronously down in CLOCK log lines.
  19115. Optional argument N tells to change by that many units."
  19116. (interactive "P")
  19117. (if (and (org-at-clock-log-p) (org-at-timestamp-p t))
  19118. (let (org-support-shift-select)
  19119. (org-clock-timestamps-down n))
  19120. (user-error "Not at a clock log")))
  19121. (defun org-increase-number-at-point (&optional inc)
  19122. "Increment the number at point.
  19123. With an optional prefix numeric argument INC, increment using
  19124. this numeric value."
  19125. (interactive "p")
  19126. (if (not (number-at-point))
  19127. (user-error "Not on a number")
  19128. (unless inc (setq inc 1))
  19129. (let ((pos (point))
  19130. (beg (skip-chars-backward "-+^/*0-9eE."))
  19131. (end (skip-chars-forward "-+^/*0-9eE^.")) nap)
  19132. (setq nap (buffer-substring-no-properties
  19133. (+ pos beg) (+ pos beg end)))
  19134. (delete-region (+ pos beg) (+ pos beg end))
  19135. (insert (calc-eval (concat (number-to-string inc) "+" nap))))
  19136. (when (org-at-table-p)
  19137. (org-table-align)
  19138. (org-table-end-of-field 1))))
  19139. (defun org-decrease-number-at-point (&optional inc)
  19140. "Decrement the number at point.
  19141. With an optional prefix numeric argument INC, decrement using
  19142. this numeric value."
  19143. (interactive "p")
  19144. (org-increase-number-at-point (- (or inc 1))))
  19145. (defun org-ctrl-c-ret ()
  19146. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  19147. (interactive)
  19148. (cond
  19149. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  19150. (t (call-interactively 'org-insert-heading))))
  19151. (defun org-find-visible ()
  19152. (let ((s (point)))
  19153. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  19154. (get-char-property s 'invisible)))
  19155. s))
  19156. (defun org-find-invisible ()
  19157. (let ((s (point)))
  19158. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  19159. (not (get-char-property s 'invisible))))
  19160. s))
  19161. (defun org-copy-visible (beg end)
  19162. "Copy the visible parts of the region."
  19163. (interactive "r")
  19164. (let (snippets s)
  19165. (save-excursion
  19166. (save-restriction
  19167. (narrow-to-region beg end)
  19168. (setq s (goto-char (point-min)))
  19169. (while (not (= (point) (point-max)))
  19170. (goto-char (org-find-invisible))
  19171. (push (buffer-substring s (point)) snippets)
  19172. (setq s (goto-char (org-find-visible))))))
  19173. (kill-new (apply 'concat (nreverse snippets)))))
  19174. (defun org-copy-special ()
  19175. "Copy region in table or copy current subtree.
  19176. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  19177. See the individual commands for more information."
  19178. (interactive)
  19179. (call-interactively
  19180. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  19181. (defun org-cut-special ()
  19182. "Cut region in table or cut current subtree.
  19183. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  19184. See the individual commands for more information."
  19185. (interactive)
  19186. (call-interactively
  19187. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  19188. (defun org-paste-special (arg)
  19189. "Paste rectangular region into table, or past subtree relative to level.
  19190. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  19191. See the individual commands for more information."
  19192. (interactive "P")
  19193. (if (org-at-table-p)
  19194. (org-table-paste-rectangle)
  19195. (org-paste-subtree arg)))
  19196. (defsubst org-in-fixed-width-region-p ()
  19197. "Is point in a fixed-width region?"
  19198. (save-match-data
  19199. (eq 'fixed-width (org-element-type (org-element-at-point)))))
  19200. (defun org-edit-special (&optional arg)
  19201. "Call a special editor for the element at point.
  19202. When at a table, call the formula editor with `org-table-edit-formulas'.
  19203. When in a source code block, call `org-edit-src-code'.
  19204. When in a fixed-width region, call `org-edit-fixed-width-region'.
  19205. When in an export block, call `org-edit-export-block'.
  19206. When at an #+INCLUDE keyword, visit the included file.
  19207. When at a footnote reference, call `org-edit-footnote-reference'
  19208. On a link, call `ffap' to visit the link at point.
  19209. Otherwise, return a user error."
  19210. (interactive "P")
  19211. (let ((element (org-element-at-point)))
  19212. (cl-assert (not buffer-read-only) nil
  19213. "Buffer is read-only: %s" (buffer-name))
  19214. (pcase (org-element-type element)
  19215. (`src-block
  19216. (if (not arg) (org-edit-src-code)
  19217. (let* ((info (org-babel-get-src-block-info))
  19218. (lang (nth 0 info))
  19219. (params (nth 2 info))
  19220. (session (cdr (assq :session params))))
  19221. (if (not session) (org-edit-src-code)
  19222. ;; At a src-block with a session and function called with
  19223. ;; an ARG: switch to the buffer related to the inferior
  19224. ;; process.
  19225. (switch-to-buffer
  19226. (funcall (intern (concat "org-babel-prep-session:" lang))
  19227. session params))))))
  19228. (`keyword
  19229. (if (member (org-element-property :key element) '("INCLUDE" "SETUPFILE"))
  19230. (org-open-link-from-string
  19231. (format "[[%s]]"
  19232. (expand-file-name
  19233. (let ((value (org-element-property :value element)))
  19234. (cond ((not (org-string-nw-p value))
  19235. (user-error "No file to edit"))
  19236. ((string-match "\\`\"\\(.*?\\)\"" value)
  19237. (match-string 1 value))
  19238. ((string-match "\\`[^ \t\"]\\S-*" value)
  19239. (match-string 0 value))
  19240. (t (user-error "No valid file specified")))))))
  19241. (user-error "No special environment to edit here")))
  19242. (`table
  19243. (if (eq (org-element-property :type element) 'table.el)
  19244. (org-edit-table.el)
  19245. (call-interactively 'org-table-edit-formulas)))
  19246. ;; Only Org tables contain `table-row' type elements.
  19247. (`table-row (call-interactively 'org-table-edit-formulas))
  19248. (`example-block (org-edit-src-code))
  19249. (`export-block (org-edit-export-block))
  19250. (`fixed-width (org-edit-fixed-width-region))
  19251. (_
  19252. ;; No notable element at point. Though, we may be at a link or
  19253. ;; a footnote reference, which are objects. Thus, scan deeper.
  19254. (let ((context (org-element-context element)))
  19255. (pcase (org-element-type context)
  19256. (`footnote-reference (org-edit-footnote-reference))
  19257. (`inline-src-block (org-edit-inline-src-code))
  19258. (`link (call-interactively #'ffap))
  19259. (_ (user-error "No special environment to edit here"))))))))
  19260. (defvar org-table-coordinate-overlays) ; defined in org-table.el
  19261. (defun org-ctrl-c-ctrl-c (&optional arg)
  19262. "Set tags in headline, or update according to changed information at point.
  19263. This command does many different things, depending on context:
  19264. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  19265. this is what we do.
  19266. - If the cursor is on a statistics cookie, update it.
  19267. - If the cursor is in a headline, prompt for tags and insert them
  19268. into the current line, aligned to `org-tags-column'. When called
  19269. with prefix arg, realign all tags in the current buffer.
  19270. - If the cursor is in one of the special #+KEYWORD lines, this
  19271. triggers scanning the buffer for these lines and updating the
  19272. information.
  19273. - If the cursor is inside a table, realign the table. This command
  19274. works even if the automatic table editor has been turned off.
  19275. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  19276. the entire table.
  19277. - If the cursor is at a footnote reference or definition, jump to
  19278. the corresponding definition or references, respectively.
  19279. - If the cursor is a the beginning of a dynamic block, update it.
  19280. - If the current buffer is a capture buffer, close note and file it.
  19281. - If the cursor is on a <<<target>>>, update radio targets and
  19282. corresponding links in this buffer.
  19283. - If the cursor is on a numbered item in a plain list, renumber the
  19284. ordered list.
  19285. - If the cursor is on a checkbox, toggle it.
  19286. - If the cursor is on a code block, evaluate it. The variable
  19287. `org-confirm-babel-evaluate' can be used to control prompting
  19288. before code block evaluation, by default every code block
  19289. evaluation requires confirmation. Code block evaluation can be
  19290. inhibited by setting `org-babel-no-eval-on-ctrl-c-ctrl-c'."
  19291. (interactive "P")
  19292. (cond
  19293. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  19294. org-occur-highlights)
  19295. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  19296. (org-remove-occur-highlights)
  19297. (message "Temporary highlights/overlays removed from current buffer"))
  19298. ((and (local-variable-p 'org-finish-function (current-buffer))
  19299. (fboundp org-finish-function))
  19300. (funcall org-finish-function))
  19301. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  19302. (t
  19303. (if (save-excursion (beginning-of-line) (looking-at "[ \t]*$"))
  19304. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  19305. (user-error "C-c C-c can do nothing useful at this location"))
  19306. (let* ((context (org-element-context))
  19307. (type (org-element-type context)))
  19308. (cl-case type
  19309. ;; When at a link, act according to the parent instead.
  19310. (link (setq context (org-element-property :parent context))
  19311. (setq type (org-element-type context)))
  19312. ;; Unsupported object types: refer to the first supported
  19313. ;; element or object containing it.
  19314. ((bold code entity export-snippet inline-babel-call inline-src-block
  19315. italic latex-fragment line-break macro strike-through subscript
  19316. superscript underline verbatim)
  19317. (setq context
  19318. (org-element-lineage
  19319. context '(radio-target paragraph verse-block table-cell)))))
  19320. ;; For convenience: at the first line of a paragraph on the
  19321. ;; same line as an item, apply function on that item instead.
  19322. (when (eq type 'paragraph)
  19323. (let ((parent (org-element-property :parent context)))
  19324. (when (and (eq (org-element-type parent) 'item)
  19325. (= (line-beginning-position)
  19326. (org-element-property :begin parent)))
  19327. (setq context parent type 'item))))
  19328. ;; Act according to type of element or object at point.
  19329. (cl-case type
  19330. (clock (org-clock-update-time-maybe))
  19331. (dynamic-block
  19332. (save-excursion
  19333. (goto-char (org-element-property :post-affiliated context))
  19334. (org-update-dblock)))
  19335. (footnote-definition
  19336. (goto-char (org-element-property :post-affiliated context))
  19337. (call-interactively 'org-footnote-action))
  19338. (footnote-reference (call-interactively 'org-footnote-action))
  19339. ((headline inlinetask)
  19340. (save-excursion (goto-char (org-element-property :begin context))
  19341. (call-interactively 'org-set-tags)))
  19342. (item
  19343. ;; At an item: a double C-u set checkbox to "[-]"
  19344. ;; unconditionally, whereas a single one will toggle its
  19345. ;; presence. Without a universal argument, if the item
  19346. ;; has a checkbox, toggle it. Otherwise repair the list.
  19347. (let* ((box (org-element-property :checkbox context))
  19348. (struct (org-element-property :structure context))
  19349. (old-struct (copy-tree struct))
  19350. (parents (org-list-parents-alist struct))
  19351. (prevs (org-list-prevs-alist struct))
  19352. (orderedp (org-not-nil (org-entry-get nil "ORDERED"))))
  19353. (org-list-set-checkbox
  19354. (org-element-property :begin context) struct
  19355. (cond ((equal arg '(16)) "[-]")
  19356. ((and (not box) (equal arg '(4))) "[ ]")
  19357. ((or (not box) (equal arg '(4))) nil)
  19358. ((eq box 'on) "[ ]")
  19359. (t "[X]")))
  19360. ;; Mimic `org-list-write-struct' but with grabbing
  19361. ;; a return value from `org-list-struct-fix-box'.
  19362. (org-list-struct-fix-ind struct parents 2)
  19363. (org-list-struct-fix-item-end struct)
  19364. (org-list-struct-fix-bul struct prevs)
  19365. (org-list-struct-fix-ind struct parents)
  19366. (let ((block-item
  19367. (org-list-struct-fix-box struct parents prevs orderedp)))
  19368. (if (and box (equal struct old-struct))
  19369. (if (equal arg '(16))
  19370. (message "Checkboxes already reset")
  19371. (user-error "Cannot toggle this checkbox: %s"
  19372. (if (eq box 'on)
  19373. "all subitems checked"
  19374. "unchecked subitems")))
  19375. (org-list-struct-apply-struct struct old-struct)
  19376. (org-update-checkbox-count-maybe))
  19377. (when block-item
  19378. (message "Checkboxes were removed due to empty box at line %d"
  19379. (org-current-line block-item))))))
  19380. (keyword
  19381. (let ((org-inhibit-startup-visibility-stuff t)
  19382. (org-startup-align-all-tables nil))
  19383. (when (boundp 'org-table-coordinate-overlays)
  19384. (mapc #'delete-overlay org-table-coordinate-overlays)
  19385. (setq org-table-coordinate-overlays nil))
  19386. (org-save-outline-visibility 'use-markers (org-mode-restart)))
  19387. (message "Local setup has been refreshed"))
  19388. (plain-list
  19389. ;; At a plain list, with a double C-u argument, set
  19390. ;; checkboxes of each item to "[-]", whereas a single one
  19391. ;; will toggle their presence according to the state of the
  19392. ;; first item in the list. Without an argument, repair the
  19393. ;; list.
  19394. (let* ((begin (org-element-property :contents-begin context))
  19395. (beginm (move-marker (make-marker) begin))
  19396. (struct (org-element-property :structure context))
  19397. (old-struct (copy-tree struct))
  19398. (first-box (save-excursion
  19399. (goto-char begin)
  19400. (looking-at org-list-full-item-re)
  19401. (match-string-no-properties 3)))
  19402. (new-box (cond ((equal arg '(16)) "[-]")
  19403. ((equal arg '(4)) (unless first-box "[ ]"))
  19404. ((equal first-box "[X]") "[ ]")
  19405. (t "[X]"))))
  19406. (cond
  19407. (arg
  19408. (dolist (pos
  19409. (org-list-get-all-items
  19410. begin struct (org-list-prevs-alist struct)))
  19411. (org-list-set-checkbox pos struct new-box)))
  19412. ((and first-box (eq (point) begin))
  19413. ;; For convenience, when point is at bol on the first
  19414. ;; item of the list and no argument is provided, simply
  19415. ;; toggle checkbox of that item, if any.
  19416. (org-list-set-checkbox begin struct new-box)))
  19417. (org-list-write-struct
  19418. struct (org-list-parents-alist struct) old-struct)
  19419. (org-update-checkbox-count-maybe)
  19420. (save-excursion (goto-char beginm) (org-list-send-list 'maybe))))
  19421. ((property-drawer node-property)
  19422. (call-interactively 'org-property-action))
  19423. ((radio-target target)
  19424. (call-interactively 'org-update-radio-target-regexp))
  19425. (statistics-cookie
  19426. (call-interactively 'org-update-statistics-cookies))
  19427. ((table table-cell table-row)
  19428. ;; At a table, recalculate every field and align it. Also
  19429. ;; send the table if necessary. If the table has
  19430. ;; a `table.el' type, just give up. At a table row or
  19431. ;; cell, maybe recalculate line but always align table.
  19432. (if (eq (org-element-property :type context) 'table.el)
  19433. (message "%s" (substitute-command-keys "\\<org-mode-map>\
  19434. Use \\[org-edit-special] to edit table.el tables"))
  19435. (let ((org-enable-table-editor t))
  19436. (if (or (eq type 'table)
  19437. ;; Check if point is at a TBLFM line.
  19438. (and (eq type 'table-row)
  19439. (= (point) (org-element-property :end context))))
  19440. (save-excursion
  19441. (if (org-at-TBLFM-p)
  19442. (progn (require 'org-table)
  19443. (org-table-calc-current-TBLFM))
  19444. (goto-char (org-element-property :contents-begin context))
  19445. (org-call-with-arg 'org-table-recalculate (or arg t))
  19446. (orgtbl-send-table 'maybe)))
  19447. (org-table-maybe-eval-formula)
  19448. (cond (arg (call-interactively 'org-table-recalculate))
  19449. ((org-table-maybe-recalculate-line))
  19450. (t (org-table-align)))))))
  19451. (timestamp (org-timestamp-change 0 'day))
  19452. (otherwise
  19453. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  19454. (user-error
  19455. "C-c C-c can do nothing useful at this location")))))))))
  19456. (defun org-mode-restart ()
  19457. (interactive)
  19458. (let ((indent-status (org-bound-and-true-p org-indent-mode)))
  19459. (funcall major-mode)
  19460. (hack-local-variables)
  19461. (when (and indent-status (not (org-bound-and-true-p org-indent-mode)))
  19462. (org-indent-mode -1)))
  19463. (message "%s restarted" major-mode))
  19464. (defun org-kill-note-or-show-branches ()
  19465. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  19466. (interactive)
  19467. (if (not org-finish-function)
  19468. (progn
  19469. (outline-hide-subtree)
  19470. (call-interactively 'outline-show-branches))
  19471. (let ((org-note-abort t))
  19472. (funcall org-finish-function))))
  19473. (defun org-delete-indentation (&optional ARG)
  19474. "Join current line to previous and fix whitespace at join.
  19475. If previous line is a headline add to headline title. Otherwise
  19476. the function calls `delete-indentation'.
  19477. With argument, join this line to following line."
  19478. (interactive "*P")
  19479. (if (save-excursion
  19480. (if ARG (beginning-of-line)
  19481. (forward-line -1))
  19482. (looking-at org-complex-heading-regexp))
  19483. ;; At headline.
  19484. (let ((tags-column (when (match-beginning 5)
  19485. (save-excursion (goto-char (match-beginning 5))
  19486. (current-column))))
  19487. (string (concat " " (progn (when ARG (forward-line 1))
  19488. (org-trim (delete-and-extract-region
  19489. (line-beginning-position)
  19490. (line-end-position)))))))
  19491. (unless (bobp) (delete-region (point) (1- (point))))
  19492. (goto-char (or (match-end 4)
  19493. (match-beginning 5)
  19494. (match-end 0)))
  19495. (skip-chars-backward " \t")
  19496. (save-excursion (insert string))
  19497. ;; Adjust alignment of tags.
  19498. (when tags-column
  19499. (org-align-tags-here (if org-auto-align-tags
  19500. org-tags-column
  19501. tags-column))))
  19502. (delete-indentation ARG)))
  19503. (defun org-open-line (n)
  19504. "Insert a new row in tables, call `open-line' elsewhere.
  19505. If `org-special-ctrl-o' is nil, just call `open-line' everywhere."
  19506. (interactive "*p")
  19507. (cond
  19508. ((not org-special-ctrl-o)
  19509. (open-line n))
  19510. ((org-at-table-p)
  19511. (org-table-insert-row))
  19512. (t
  19513. (open-line n))))
  19514. (defun org-return (&optional indent)
  19515. "Goto next table row or insert a newline.
  19516. Calls `org-table-next-row' or `newline', depending on context.
  19517. When optional INDENT argument is non-nil, call
  19518. `newline-and-indent' instead of `newline'.
  19519. When `org-return-follows-link' is non-nil and point is on
  19520. a timestamp or a link, call `org-open-at-point'. However, it
  19521. will not happen if point is in a table or on a \"dead\"
  19522. object (e.g., within a comment). In these case, you need to use
  19523. `org-open-at-point' directly."
  19524. (interactive)
  19525. (let ((context (if org-return-follows-link (org-element-context)
  19526. (org-element-at-point))))
  19527. (cond
  19528. ;; In a table, call `org-table-next-row'.
  19529. ((or (and (eq (org-element-type context) 'table)
  19530. (>= (point) (org-element-property :contents-begin context))
  19531. (< (point) (org-element-property :contents-end context)))
  19532. (org-element-lineage context '(table-row table-cell) t))
  19533. (org-table-justify-field-maybe)
  19534. (call-interactively #'org-table-next-row))
  19535. ;; On a link or a timestamp, call `org-open-at-point' if
  19536. ;; `org-return-follows-link' allows it. Tolerate fuzzy
  19537. ;; locations, e.g., in a comment, as `org-open-at-point'.
  19538. ((and org-return-follows-link
  19539. (or (org-in-regexp org-ts-regexp-both nil t)
  19540. (org-in-regexp org-tsr-regexp-both nil t)
  19541. (org-in-regexp org-any-link-re nil t)))
  19542. (call-interactively #'org-open-at-point))
  19543. ;; Insert newline in heading, but preserve tags.
  19544. ((and (not (bolp))
  19545. (save-excursion (beginning-of-line)
  19546. (looking-at org-complex-heading-regexp)))
  19547. ;; At headline. Split line. However, if point is on keyword,
  19548. ;; priority cookie or tags, do not break any of them: add
  19549. ;; a newline after the headline instead.
  19550. (let ((tags-column (and (match-beginning 5)
  19551. (save-excursion (goto-char (match-beginning 5))
  19552. (current-column))))
  19553. (string
  19554. (when (and (match-end 4)
  19555. (>= (point)
  19556. (or (match-end 3) (match-end 2) (1+ (match-end 1))))
  19557. (<= (point) (match-end 4)))
  19558. (delete-and-extract-region (point) (match-end 4)))))
  19559. (when (and tags-column string) ; Adjust tag alignment.
  19560. (org-align-tags-here
  19561. (if org-auto-align-tags org-tags-column tags-column)))
  19562. (end-of-line)
  19563. (org-show-entry)
  19564. (if indent (newline-and-indent) (newline))
  19565. (when string (save-excursion (insert (org-trim string))))))
  19566. ;; In a list, make sure indenting keeps trailing text within.
  19567. ((and indent
  19568. (not (eolp))
  19569. (org-element-lineage context '(item)))
  19570. (let ((trailing-data
  19571. (delete-and-extract-region (point) (line-end-position))))
  19572. (newline-and-indent)
  19573. (save-excursion (insert trailing-data))))
  19574. (t (if indent (newline-and-indent) (newline))))))
  19575. (defun org-return-indent ()
  19576. "Goto next table row or insert a newline and indent.
  19577. Calls `org-table-next-row' or `newline-and-indent', depending on
  19578. context. See the individual commands for more information."
  19579. (interactive)
  19580. (org-return t))
  19581. (defun org-ctrl-c-star ()
  19582. "Compute table, or change heading status of lines.
  19583. Calls `org-table-recalculate' or `org-toggle-heading',
  19584. depending on context."
  19585. (interactive)
  19586. (cond
  19587. ((org-at-table-p)
  19588. (call-interactively 'org-table-recalculate))
  19589. (t
  19590. ;; Convert all lines in region to list items
  19591. (call-interactively 'org-toggle-heading))))
  19592. (defun org-ctrl-c-minus ()
  19593. "Insert separator line in table or modify bullet status of line.
  19594. Also turns a plain line or a region of lines into list items.
  19595. Calls `org-table-insert-hline', `org-toggle-item', or
  19596. `org-cycle-list-bullet', depending on context."
  19597. (interactive)
  19598. (cond
  19599. ((org-at-table-p)
  19600. (call-interactively 'org-table-insert-hline))
  19601. ((org-region-active-p)
  19602. (call-interactively 'org-toggle-item))
  19603. ((org-in-item-p)
  19604. (call-interactively 'org-cycle-list-bullet))
  19605. (t
  19606. (call-interactively 'org-toggle-item))))
  19607. (defun org-toggle-item (arg)
  19608. "Convert headings or normal lines to items, items to normal lines.
  19609. If there is no active region, only the current line is considered.
  19610. If the first non blank line in the region is a headline, convert
  19611. all headlines to items, shifting text accordingly.
  19612. If it is an item, convert all items to normal lines.
  19613. If it is normal text, change region into a list of items.
  19614. With a prefix argument ARG, change the region in a single item."
  19615. (interactive "P")
  19616. (let ((shift-text
  19617. (function
  19618. ;; Shift text in current section to IND, from point to END.
  19619. ;; The function leaves point to END line.
  19620. (lambda (ind end)
  19621. (let ((min-i 1000) (end (copy-marker end)))
  19622. ;; First determine the minimum indentation (MIN-I) of
  19623. ;; the text.
  19624. (save-excursion
  19625. (catch 'exit
  19626. (while (< (point) end)
  19627. (let ((i (org-get-indentation)))
  19628. (cond
  19629. ;; Skip blank lines and inline tasks.
  19630. ((looking-at "^[ \t]*$"))
  19631. ((looking-at org-outline-regexp-bol))
  19632. ;; We can't find less than 0 indentation.
  19633. ((zerop i) (throw 'exit (setq min-i 0)))
  19634. ((< i min-i) (setq min-i i))))
  19635. (forward-line))))
  19636. ;; Then indent each line so that a line indented to
  19637. ;; MIN-I becomes indented to IND. Ignore blank lines
  19638. ;; and inline tasks in the process.
  19639. (let ((delta (- ind min-i)))
  19640. (while (< (point) end)
  19641. (unless (or (looking-at "^[ \t]*$")
  19642. (looking-at org-outline-regexp-bol))
  19643. (org-indent-line-to (+ (org-get-indentation) delta)))
  19644. (forward-line)))))))
  19645. (skip-blanks
  19646. (function
  19647. ;; Return beginning of first non-blank line, starting from
  19648. ;; line at POS.
  19649. (lambda (pos)
  19650. (save-excursion
  19651. (goto-char pos)
  19652. (skip-chars-forward " \r\t\n")
  19653. (point-at-bol)))))
  19654. beg end)
  19655. ;; Determine boundaries of changes.
  19656. (if (org-region-active-p)
  19657. (setq beg (funcall skip-blanks (region-beginning))
  19658. end (copy-marker (region-end)))
  19659. (setq beg (funcall skip-blanks (point-at-bol))
  19660. end (copy-marker (point-at-eol))))
  19661. ;; Depending on the starting line, choose an action on the text
  19662. ;; between BEG and END.
  19663. (org-with-limited-levels
  19664. (save-excursion
  19665. (goto-char beg)
  19666. (cond
  19667. ;; Case 1. Start at an item: de-itemize. Note that it only
  19668. ;; happens when a region is active: `org-ctrl-c-minus'
  19669. ;; would call `org-cycle-list-bullet' otherwise.
  19670. ((org-at-item-p)
  19671. (while (< (point) end)
  19672. (when (org-at-item-p)
  19673. (skip-chars-forward " \t")
  19674. (delete-region (point) (match-end 0)))
  19675. (forward-line)))
  19676. ;; Case 2. Start at an heading: convert to items.
  19677. ((org-at-heading-p)
  19678. (let* ((bul (org-list-bullet-string "-"))
  19679. (bul-len (length bul))
  19680. (done (org-entry-is-done-p))
  19681. (todo (org-entry-is-todo-p))
  19682. ;; Indentation of the first heading. It should be
  19683. ;; relative to the indentation of its parent, if any.
  19684. (start-ind (save-excursion
  19685. (cond
  19686. ((not org-adapt-indentation) 0)
  19687. ((not (outline-previous-heading)) 0)
  19688. (t (length (match-string 0))))))
  19689. ;; Level of first heading. Further headings will be
  19690. ;; compared to it to determine hierarchy in the list.
  19691. (ref-level (org-reduced-level (org-outline-level))))
  19692. (when (or done todo) (org-todo ""))
  19693. (while (< (point) end)
  19694. (let* ((level (org-reduced-level (org-outline-level)))
  19695. (delta (max 0 (- level ref-level))))
  19696. ;; If current headline is less indented than the first
  19697. ;; one, set it as reference, in order to preserve
  19698. ;; subtrees.
  19699. (when (< level ref-level) (setq ref-level level))
  19700. (replace-match bul t t)
  19701. (org-indent-line-to (+ start-ind (* delta bul-len)))
  19702. (when (or done todo)
  19703. (let* ((struct (org-list-struct))
  19704. (old (copy-tree struct)))
  19705. (org-list-set-checkbox (line-beginning-position)
  19706. struct
  19707. (if done "[X]" "[ ]"))
  19708. (org-list-write-struct struct
  19709. (org-list-parents-alist struct)
  19710. old)))
  19711. ;; Ensure all text down to END (or SECTION-END) belongs
  19712. ;; to the newly created item.
  19713. (let ((section-end (save-excursion
  19714. (or (outline-next-heading) (point)))))
  19715. (forward-line)
  19716. (funcall shift-text
  19717. (+ start-ind (* (1+ delta) bul-len))
  19718. (min end section-end)))))))
  19719. ;; Case 3. Normal line with ARG: make the first line of region
  19720. ;; an item, and shift indentation of others lines to
  19721. ;; set them as item's body.
  19722. (arg (let* ((bul (org-list-bullet-string "-"))
  19723. (bul-len (length bul))
  19724. (ref-ind (org-get-indentation)))
  19725. (skip-chars-forward " \t")
  19726. (insert bul)
  19727. (forward-line)
  19728. (while (< (point) end)
  19729. ;; Ensure that lines less indented than first one
  19730. ;; still get included in item body.
  19731. (funcall shift-text
  19732. (+ ref-ind bul-len)
  19733. (min end (save-excursion (or (outline-next-heading)
  19734. (point)))))
  19735. (forward-line))))
  19736. ;; Case 4. Normal line without ARG: turn each non-item line
  19737. ;; into an item.
  19738. (t
  19739. (while (< (point) end)
  19740. (unless (or (org-at-heading-p) (org-at-item-p))
  19741. (when (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  19742. (replace-match
  19743. (concat "\\1" (org-list-bullet-string "-") "\\2"))))
  19744. (forward-line))))))))
  19745. (defun org-toggle-heading (&optional nstars)
  19746. "Convert headings to normal text, or items or text to headings.
  19747. If there is no active region, only convert the current line.
  19748. With a \\[universal-argument] prefix, convert the whole list at
  19749. point into heading.
  19750. In a region:
  19751. - If the first non blank line is a headline, remove the stars
  19752. from all headlines in the region.
  19753. - If it is a normal line, turn each and every normal line (i.e.,
  19754. not an heading or an item) in the region into headings. If you
  19755. want to convert only the first line of this region, use one
  19756. universal prefix argument.
  19757. - If it is a plain list item, turn all plain list items into headings.
  19758. When converting a line into a heading, the number of stars is chosen
  19759. such that the lines become children of the current entry. However,
  19760. when a numeric prefix argument is given, its value determines the
  19761. number of stars to add."
  19762. (interactive "P")
  19763. (let ((skip-blanks
  19764. (function
  19765. ;; Return beginning of first non-blank line, starting from
  19766. ;; line at POS.
  19767. (lambda (pos)
  19768. (save-excursion
  19769. (goto-char pos)
  19770. (while (org-at-comment-p) (forward-line))
  19771. (skip-chars-forward " \r\t\n")
  19772. (point-at-bol)))))
  19773. beg end toggled)
  19774. ;; Determine boundaries of changes. If a universal prefix has
  19775. ;; been given, put the list in a region. If region ends at a bol,
  19776. ;; do not consider the last line to be in the region.
  19777. (when (and current-prefix-arg (org-at-item-p))
  19778. (when (listp current-prefix-arg) (setq current-prefix-arg 1))
  19779. (org-mark-element))
  19780. (if (org-region-active-p)
  19781. (setq beg (funcall skip-blanks (region-beginning))
  19782. end (copy-marker (save-excursion
  19783. (goto-char (region-end))
  19784. (if (bolp) (point) (point-at-eol)))))
  19785. (setq beg (funcall skip-blanks (point-at-bol))
  19786. end (copy-marker (point-at-eol))))
  19787. ;; Ensure inline tasks don't count as headings.
  19788. (org-with-limited-levels
  19789. (save-excursion
  19790. (goto-char beg)
  19791. (cond
  19792. ;; Case 1. Started at an heading: de-star headings.
  19793. ((org-at-heading-p)
  19794. (while (< (point) end)
  19795. (when (org-at-heading-p t)
  19796. (looking-at org-outline-regexp) (replace-match "")
  19797. (setq toggled t))
  19798. (forward-line)))
  19799. ;; Case 2. Started at an item: change items into headlines.
  19800. ;; One star will be added by `org-list-to-subtree'.
  19801. ((org-at-item-p)
  19802. (while (< (point) end)
  19803. (when (org-at-item-p)
  19804. ;; Pay attention to cases when region ends before list.
  19805. (let* ((struct (org-list-struct))
  19806. (list-end
  19807. (min (org-list-get-bottom-point struct) (1+ end))))
  19808. (save-restriction
  19809. (narrow-to-region (point) list-end)
  19810. (insert (org-list-to-subtree (org-list-to-lisp t)) "\n")))
  19811. (setq toggled t))
  19812. (forward-line)))
  19813. ;; Case 3. Started at normal text: make every line an heading,
  19814. ;; skipping headlines and items.
  19815. (t (let* ((stars
  19816. (make-string
  19817. (if (numberp nstars) nstars (or (org-current-level) 0)) ?*))
  19818. (add-stars
  19819. (cond (nstars "") ; stars from prefix only
  19820. ((equal stars "") "*") ; before first heading
  19821. (org-odd-levels-only "**") ; inside heading, odd
  19822. (t "*"))) ; inside heading, oddeven
  19823. (rpl (concat stars add-stars " "))
  19824. (lend (when (listp nstars) (save-excursion (end-of-line) (point)))))
  19825. (while (< (point) (if (equal nstars '(4)) lend end))
  19826. (when (and (not (or (org-at-heading-p) (org-at-item-p) (org-at-comment-p)))
  19827. (looking-at "\\([ \t]*\\)\\(\\S-\\)"))
  19828. (replace-match (concat rpl (match-string 2))) (setq toggled t))
  19829. (forward-line)))))))
  19830. (unless toggled (message "Cannot toggle heading from here"))))
  19831. (defun org-meta-return (&optional _arg)
  19832. "Insert a new heading or wrap a region in a table.
  19833. Calls `org-insert-heading' or `org-table-wrap-region', depending
  19834. on context. See the individual commands for more information."
  19835. (interactive)
  19836. (org-check-before-invisible-edit 'insert)
  19837. (or (run-hook-with-args-until-success 'org-metareturn-hook)
  19838. (call-interactively (if (org-at-table-p) #'org-table-wrap-region
  19839. #'org-insert-heading))))
  19840. ;;; Menu entries
  19841. (defsubst org-in-subtree-not-table-p ()
  19842. "Are we in a subtree and not in a table?"
  19843. (and (not (org-before-first-heading-p))
  19844. (not (org-at-table-p))))
  19845. ;; Define the Org-mode menus
  19846. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  19847. '("Tbl"
  19848. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  19849. ["Next Field" org-cycle (org-at-table-p)]
  19850. ["Previous Field" org-shifttab (org-at-table-p)]
  19851. ["Next Row" org-return (org-at-table-p)]
  19852. "--"
  19853. ["Blank Field" org-table-blank-field (org-at-table-p)]
  19854. ["Edit Field" org-table-edit-field (org-at-table-p)]
  19855. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  19856. "--"
  19857. ("Column"
  19858. ["Move Column Left" org-metaleft (org-at-table-p)]
  19859. ["Move Column Right" org-metaright (org-at-table-p)]
  19860. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  19861. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  19862. ("Row"
  19863. ["Move Row Up" org-metaup (org-at-table-p)]
  19864. ["Move Row Down" org-metadown (org-at-table-p)]
  19865. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  19866. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  19867. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  19868. "--"
  19869. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  19870. ("Rectangle"
  19871. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  19872. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  19873. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  19874. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  19875. "--"
  19876. ("Calculate"
  19877. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  19878. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  19879. ["Edit Formulas" org-edit-special (org-at-table-p)]
  19880. "--"
  19881. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  19882. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  19883. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  19884. "--"
  19885. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  19886. "--"
  19887. ["Sum Column/Rectangle" org-table-sum
  19888. (or (org-at-table-p) (org-region-active-p))]
  19889. ["Which Column?" org-table-current-column (org-at-table-p)])
  19890. ["Debug Formulas"
  19891. org-table-toggle-formula-debugger
  19892. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  19893. ["Show Col/Row Numbers"
  19894. org-table-toggle-coordinate-overlays
  19895. :style toggle
  19896. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  19897. "--"
  19898. ["Create" org-table-create (and (not (org-at-table-p))
  19899. org-enable-table-editor)]
  19900. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  19901. ["Import from File" org-table-import (not (org-at-table-p))]
  19902. ["Export to File" org-table-export (org-at-table-p)]
  19903. "--"
  19904. ["Create/Convert from/to table.el" org-table-create-with-table.el t]
  19905. "--"
  19906. ("Plot"
  19907. ["Ascii plot" orgtbl-ascii-plot :active (org-at-table-p) :keys "C-c \" a"]
  19908. ["Gnuplot" org-plot/gnuplot :active (org-at-table-p) :keys "C-c \" g"])))
  19909. (easy-menu-define org-org-menu org-mode-map "Org menu"
  19910. '("Org"
  19911. ("Show/Hide"
  19912. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  19913. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  19914. ["Sparse Tree..." org-sparse-tree t]
  19915. ["Reveal Context" org-reveal t]
  19916. ["Show All" outline-show-all t]
  19917. "--"
  19918. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  19919. "--"
  19920. ["New Heading" org-insert-heading t]
  19921. ("Navigate Headings"
  19922. ["Up" outline-up-heading t]
  19923. ["Next" outline-next-visible-heading t]
  19924. ["Previous" outline-previous-visible-heading t]
  19925. ["Next Same Level" outline-forward-same-level t]
  19926. ["Previous Same Level" outline-backward-same-level t]
  19927. "--"
  19928. ["Jump" org-goto t])
  19929. ("Edit Structure"
  19930. ["Refile Subtree" org-refile (org-in-subtree-not-table-p)]
  19931. "--"
  19932. ["Move Subtree Up" org-metaup (org-at-heading-p)]
  19933. ["Move Subtree Down" org-metadown (org-at-heading-p)]
  19934. "--"
  19935. ["Copy Subtree" org-copy-special (org-in-subtree-not-table-p)]
  19936. ["Cut Subtree" org-cut-special (org-in-subtree-not-table-p)]
  19937. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  19938. "--"
  19939. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  19940. "--"
  19941. ["Copy visible text" org-copy-visible t]
  19942. "--"
  19943. ["Promote Heading" org-metaleft (org-in-subtree-not-table-p)]
  19944. ["Promote Subtree" org-shiftmetaleft (org-in-subtree-not-table-p)]
  19945. ["Demote Heading" org-metaright (org-in-subtree-not-table-p)]
  19946. ["Demote Subtree" org-shiftmetaright (org-in-subtree-not-table-p)]
  19947. "--"
  19948. ["Sort Region/Children" org-sort t]
  19949. "--"
  19950. ["Convert to odd levels" org-convert-to-odd-levels t]
  19951. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  19952. ("Editing"
  19953. ["Emphasis..." org-emphasize t]
  19954. ["Edit Source Example" org-edit-special t]
  19955. "--"
  19956. ["Footnote new/jump" org-footnote-action t]
  19957. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  19958. ("Archive"
  19959. ["Archive (default method)" org-archive-subtree-default (org-in-subtree-not-table-p)]
  19960. "--"
  19961. ["Move Subtree to Archive file" org-archive-subtree (org-in-subtree-not-table-p)]
  19962. ["Toggle ARCHIVE tag" org-toggle-archive-tag (org-in-subtree-not-table-p)]
  19963. ["Move subtree to Archive sibling" org-archive-to-archive-sibling (org-in-subtree-not-table-p)]
  19964. )
  19965. "--"
  19966. ("Hyperlinks"
  19967. ["Store Link (Global)" org-store-link t]
  19968. ["Find existing link to here" org-occur-link-in-agenda-files t]
  19969. ["Insert Link" org-insert-link t]
  19970. ["Follow Link" org-open-at-point t]
  19971. "--"
  19972. ["Next link" org-next-link t]
  19973. ["Previous link" org-previous-link t]
  19974. "--"
  19975. ["Descriptive Links"
  19976. org-toggle-link-display
  19977. :style radio
  19978. :selected org-descriptive-links
  19979. ]
  19980. ["Literal Links"
  19981. org-toggle-link-display
  19982. :style radio
  19983. :selected (not org-descriptive-links)])
  19984. "--"
  19985. ("TODO Lists"
  19986. ["TODO/DONE/-" org-todo t]
  19987. ("Select keyword"
  19988. ["Next keyword" org-shiftright (org-at-heading-p)]
  19989. ["Previous keyword" org-shiftleft (org-at-heading-p)]
  19990. ["Complete Keyword" pcomplete (assq :todo-keyword (org-context))]
  19991. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))]
  19992. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))])
  19993. ["Show TODO Tree" org-show-todo-tree :active t :keys "C-c / t"]
  19994. ["Global TODO list" org-todo-list :active t :keys "C-c a t"]
  19995. "--"
  19996. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  19997. :selected org-enforce-todo-dependencies :style toggle :active t]
  19998. "Settings for tree at point"
  19999. ["Do Children sequentially" org-toggle-ordered-property :style radio
  20000. :selected (org-entry-get nil "ORDERED")
  20001. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  20002. ["Do Children parallel" org-toggle-ordered-property :style radio
  20003. :selected (not (org-entry-get nil "ORDERED"))
  20004. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  20005. "--"
  20006. ["Set Priority" org-priority t]
  20007. ["Priority Up" org-shiftup t]
  20008. ["Priority Down" org-shiftdown t]
  20009. "--"
  20010. ["Get news from all feeds" org-feed-update-all t]
  20011. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  20012. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  20013. ("TAGS and Properties"
  20014. ["Set Tags" org-set-tags-command (not (org-before-first-heading-p))]
  20015. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  20016. "--"
  20017. ["Set property" org-set-property (not (org-before-first-heading-p))]
  20018. ["Column view of properties" org-columns t]
  20019. ["Insert Column View DBlock" org-insert-columns-dblock t])
  20020. ("Dates and Scheduling"
  20021. ["Timestamp" org-time-stamp (not (org-before-first-heading-p))]
  20022. ["Timestamp (inactive)" org-time-stamp-inactive (not (org-before-first-heading-p))]
  20023. ("Change Date"
  20024. ["1 Day Later" org-shiftright (org-at-timestamp-p)]
  20025. ["1 Day Earlier" org-shiftleft (org-at-timestamp-p)]
  20026. ["1 ... Later" org-shiftup (org-at-timestamp-p)]
  20027. ["1 ... Earlier" org-shiftdown (org-at-timestamp-p)])
  20028. ["Compute Time Range" org-evaluate-time-range t]
  20029. ["Schedule Item" org-schedule (not (org-before-first-heading-p))]
  20030. ["Deadline" org-deadline (not (org-before-first-heading-p))]
  20031. "--"
  20032. ["Custom time format" org-toggle-time-stamp-overlays
  20033. :style radio :selected org-display-custom-times]
  20034. "--"
  20035. ["Goto Calendar" org-goto-calendar t]
  20036. ["Date from Calendar" org-date-from-calendar t]
  20037. "--"
  20038. ["Start/Restart Timer" org-timer-start t]
  20039. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  20040. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  20041. ["Insert Timer String" org-timer t]
  20042. ["Insert Timer Item" org-timer-item t])
  20043. ("Logging work"
  20044. ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
  20045. ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
  20046. ["Clock out" org-clock-out t]
  20047. ["Clock cancel" org-clock-cancel t]
  20048. "--"
  20049. ["Mark as default task" org-clock-mark-default-task t]
  20050. ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
  20051. ["Goto running clock" org-clock-goto t]
  20052. "--"
  20053. ["Display times" org-clock-display t]
  20054. ["Create clock table" org-clock-report t]
  20055. "--"
  20056. ["Record DONE time"
  20057. (progn (setq org-log-done (not org-log-done))
  20058. (message "Switching to %s will %s record a timestamp"
  20059. (car org-done-keywords)
  20060. (if org-log-done "automatically" "not")))
  20061. :style toggle :selected org-log-done])
  20062. "--"
  20063. ["Agenda Command..." org-agenda t]
  20064. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  20065. ("File List for Agenda")
  20066. ("Special views current file"
  20067. ["TODO Tree" org-show-todo-tree t]
  20068. ["Check Deadlines" org-check-deadlines t]
  20069. ["Timeline" org-timeline t]
  20070. ["Tags/Property tree" org-match-sparse-tree t])
  20071. "--"
  20072. ["Export/Publish..." org-export-dispatch t]
  20073. ("LaTeX"
  20074. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  20075. :selected org-cdlatex-mode]
  20076. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  20077. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  20078. ["Modify math symbol" org-cdlatex-math-modify
  20079. (org-inside-LaTeX-fragment-p)]
  20080. ["Insert citation" org-reftex-citation t]
  20081. "--"
  20082. ["Template for BEAMER" (org-beamer-insert-options-template) t])
  20083. "--"
  20084. ("MobileOrg"
  20085. ["Push Files and Views" org-mobile-push t]
  20086. ["Get Captured and Flagged" org-mobile-pull t]
  20087. ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
  20088. "--"
  20089. ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
  20090. "--"
  20091. ("Documentation"
  20092. ["Show Version" org-version t]
  20093. ["Info Documentation" org-info t])
  20094. ("Customize"
  20095. ["Browse Org Group" org-customize t]
  20096. "--"
  20097. ["Expand This Menu" org-create-customize-menu
  20098. (fboundp 'customize-menu-create)])
  20099. ["Send bug report" org-submit-bug-report t]
  20100. "--"
  20101. ("Refresh/Reload"
  20102. ["Refresh setup current buffer" org-mode-restart t]
  20103. ["Reload Org (after update)" org-reload t]
  20104. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x !"])
  20105. ))
  20106. (defun org-info (&optional node)
  20107. "Read documentation for Org-mode in the info system.
  20108. With optional NODE, go directly to that node."
  20109. (interactive)
  20110. (info (format "(org)%s" (or node ""))))
  20111. ;;;###autoload
  20112. (defun org-submit-bug-report ()
  20113. "Submit a bug report on Org-mode via mail.
  20114. Don't hesitate to report any problems or inaccurate documentation.
  20115. If you don't have setup sending mail from (X)Emacs, please copy the
  20116. output buffer into your mail program, as it gives us important
  20117. information about your Org-mode version and configuration."
  20118. (interactive)
  20119. (require 'reporter)
  20120. (defvar reporter-prompt-for-summary-p)
  20121. (org-load-modules-maybe)
  20122. (org-require-autoloaded-modules)
  20123. (let ((reporter-prompt-for-summary-p "Bug report subject: "))
  20124. (reporter-submit-bug-report
  20125. "emacs-orgmode@gnu.org"
  20126. (org-version nil 'full)
  20127. (let (list)
  20128. (save-window-excursion
  20129. (org-pop-to-buffer-same-window (get-buffer-create "*Warn about privacy*"))
  20130. (delete-other-windows)
  20131. (erase-buffer)
  20132. (insert "You are about to submit a bug report to the Org-mode mailing list.
  20133. We would like to add your full Org-mode and Outline configuration to the
  20134. bug report. This greatly simplifies the work of the maintainer and
  20135. other experts on the mailing list.
  20136. HOWEVER, some variables you have customized may contain private
  20137. information. The names of customers, colleagues, or friends, might
  20138. appear in the form of file names, tags, todo states, or search strings.
  20139. If you answer yes to the prompt, you might want to check and remove
  20140. such private information before sending the email.")
  20141. (add-text-properties (point-min) (point-max) '(face org-warning))
  20142. (when (yes-or-no-p "Include your Org-mode configuration ")
  20143. (mapatoms
  20144. (lambda (v)
  20145. (and (boundp v)
  20146. (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
  20147. (or (and (symbol-value v)
  20148. (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
  20149. (and
  20150. (get v 'custom-type) (get v 'standard-value)
  20151. (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
  20152. (push v list)))))
  20153. (kill-buffer (get-buffer "*Warn about privacy*"))
  20154. list))
  20155. nil nil
  20156. "Remember to cover the basics, that is, what you expected to happen and
  20157. what in fact did happen. You don't know how to make a good report? See
  20158. http://orgmode.org/manual/Feedback.html#Feedback
  20159. Your bug report will be posted to the Org-mode mailing list.
  20160. ------------------------------------------------------------------------")
  20161. (save-excursion
  20162. (when (re-search-backward "^\\(Subject: \\)Org-mode version \\(.*?\\);[ \t]*\\(.*\\)" nil t)
  20163. (replace-match "\\1Bug: \\3 [\\2]")))))
  20164. (defun org-install-agenda-files-menu ()
  20165. (let ((bl (buffer-list)))
  20166. (save-excursion
  20167. (while bl
  20168. (set-buffer (pop bl))
  20169. (when (derived-mode-p 'org-mode) (setq bl nil)))
  20170. (when (derived-mode-p 'org-mode)
  20171. (easy-menu-change
  20172. '("Org") "File List for Agenda"
  20173. (append
  20174. (list
  20175. ["Edit File List" (org-edit-agenda-file-list) t]
  20176. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  20177. ["Remove Current File from List" org-remove-file t]
  20178. ["Cycle through agenda files" org-cycle-agenda-files t]
  20179. ["Occur in all agenda files" org-occur-in-agenda-files t]
  20180. "--")
  20181. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  20182. ;;;; Documentation
  20183. (defun org-require-autoloaded-modules ()
  20184. (interactive)
  20185. (mapc #'require
  20186. '(org-agenda org-archive org-attach org-clock org-colview org-id
  20187. org-table org-timer)))
  20188. ;;;###autoload
  20189. (defun org-reload (&optional uncompiled)
  20190. "Reload all org lisp files.
  20191. With prefix arg UNCOMPILED, load the uncompiled versions."
  20192. (interactive "P")
  20193. (require 'loadhist)
  20194. (let* ((org-dir (org-find-library-dir "org"))
  20195. (contrib-dir (or (org-find-library-dir "org-contribdir") org-dir))
  20196. (feature-re "^\\(org\\|ob\\|ox\\)\\(-.*\\)?")
  20197. (remove-re (mapconcat 'identity
  20198. (mapcar (lambda (f) (concat "^" f "$"))
  20199. (list (if (featurep 'xemacs)
  20200. "org-colview"
  20201. "org-colview-xemacs")
  20202. "org" "org-loaddefs" "org-version"))
  20203. "\\|"))
  20204. (feats (delete-dups
  20205. (mapcar 'file-name-sans-extension
  20206. (mapcar 'file-name-nondirectory
  20207. (delq nil
  20208. (mapcar 'feature-file
  20209. features))))))
  20210. (lfeat (append
  20211. (sort
  20212. (setq feats
  20213. (delq nil (mapcar
  20214. (lambda (f)
  20215. (if (and (string-match feature-re f)
  20216. (not (string-match remove-re f)))
  20217. f nil))
  20218. feats)))
  20219. 'string-lessp)
  20220. (list "org-version" "org")))
  20221. (load-suffixes (when (boundp 'load-suffixes) load-suffixes))
  20222. (load-suffixes (if uncompiled (reverse load-suffixes) load-suffixes))
  20223. load-uncore load-misses)
  20224. (setq load-misses
  20225. (delq 't
  20226. (mapcar (lambda (f)
  20227. (or (org-load-noerror-mustsuffix (concat org-dir f))
  20228. (and (string= org-dir contrib-dir)
  20229. (org-load-noerror-mustsuffix (concat contrib-dir f)))
  20230. (and (org-load-noerror-mustsuffix (concat (org-find-library-dir f) f))
  20231. (add-to-list 'load-uncore f 'append)
  20232. 't)
  20233. f))
  20234. lfeat)))
  20235. (when load-uncore
  20236. (message "The following feature%s found in load-path, please check if that's correct:\n%s"
  20237. (if (> (length load-uncore) 1) "s were" " was") load-uncore))
  20238. (if load-misses
  20239. (message "Some error occurred while reloading Org feature%s\n%s\nPlease check *Messages*!\n%s"
  20240. (if (> (length load-misses) 1) "s" "") load-misses (org-version nil 'full))
  20241. (message "Successfully reloaded Org\n%s" (org-version nil 'full)))))
  20242. ;;;###autoload
  20243. (defun org-customize ()
  20244. "Call the customize function with org as argument."
  20245. (interactive)
  20246. (org-load-modules-maybe)
  20247. (org-require-autoloaded-modules)
  20248. (customize-browse 'org))
  20249. (defun org-create-customize-menu ()
  20250. "Create a full customization menu for Org-mode, insert it into the menu."
  20251. (interactive)
  20252. (org-load-modules-maybe)
  20253. (org-require-autoloaded-modules)
  20254. (if (fboundp 'customize-menu-create)
  20255. (progn
  20256. (easy-menu-change
  20257. '("Org") "Customize"
  20258. `(["Browse Org group" org-customize t]
  20259. "--"
  20260. ,(customize-menu-create 'org)
  20261. ["Set" Custom-set t]
  20262. ["Save" Custom-save t]
  20263. ["Reset to Current" Custom-reset-current t]
  20264. ["Reset to Saved" Custom-reset-saved t]
  20265. ["Reset to Standard Settings" Custom-reset-standard t]))
  20266. (message "\"Org\"-menu now contains full customization menu"))
  20267. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  20268. ;;;; Miscellaneous stuff
  20269. ;;; Generally useful functions
  20270. (defun org-get-at-eol (property n)
  20271. "Get text property PROPERTY at the end of line less N characters."
  20272. (get-text-property (- (point-at-eol) n) property))
  20273. (defun org-find-text-property-in-string (prop s)
  20274. "Return the first non-nil value of property PROP in string S."
  20275. (or (get-text-property 0 prop s)
  20276. (get-text-property (or (next-single-property-change 0 prop s) 0)
  20277. prop s)))
  20278. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  20279. "Display the given MESSAGE as a warning."
  20280. (if (fboundp 'display-warning)
  20281. (display-warning 'org message
  20282. (if (featurep 'xemacs) 'warning :warning))
  20283. (let ((buf (get-buffer-create "*Org warnings*")))
  20284. (with-current-buffer buf
  20285. (goto-char (point-max))
  20286. (insert "Warning (Org): " message)
  20287. (unless (bolp)
  20288. (newline)))
  20289. (display-buffer buf)
  20290. (sit-for 0))))
  20291. (defun org-eval (form)
  20292. "Eval FORM and return result."
  20293. (condition-case error
  20294. (eval form)
  20295. (error (format "%%![Error: %s]" error))))
  20296. (defun org-in-clocktable-p ()
  20297. "Check if the cursor is in a clocktable."
  20298. (let ((pos (point)) start)
  20299. (save-excursion
  20300. (end-of-line 1)
  20301. (and (re-search-backward "^[ \t]*#\\+BEGIN:[ \t]+clocktable" nil t)
  20302. (setq start (match-beginning 0))
  20303. (re-search-forward "^[ \t]*#\\+END:.*" nil t)
  20304. (>= (match-end 0) pos)
  20305. start))))
  20306. (defun org-in-verbatim-emphasis ()
  20307. (save-match-data
  20308. (and (org-in-regexp org-emph-re 2)
  20309. (>= (point) (match-beginning 3))
  20310. (<= (point) (match-end 4))
  20311. (member (match-string 3) '("=" "~")))))
  20312. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  20313. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  20314. (if (and marker (marker-buffer marker)
  20315. (buffer-live-p (marker-buffer marker)))
  20316. (progn
  20317. (org-pop-to-buffer-same-window (marker-buffer marker))
  20318. (when (or (> marker (point-max)) (< marker (point-min)))
  20319. (widen))
  20320. (goto-char marker)
  20321. (org-show-context 'org-goto))
  20322. (if bookmark
  20323. (bookmark-jump bookmark)
  20324. (error "Cannot find location"))))
  20325. (defun org-quote-csv-field (s)
  20326. "Quote field for inclusion in CSV material."
  20327. (if (string-match "[\",]" s)
  20328. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  20329. s))
  20330. (defun org-force-self-insert (N)
  20331. "Needed to enforce self-insert under remapping."
  20332. (interactive "p")
  20333. (self-insert-command N))
  20334. (defun org-string-width (s)
  20335. "Compute width of string, ignoring invisible characters.
  20336. This ignores character with invisibility property `org-link', and also
  20337. characters with property `org-cwidth', because these will become invisible
  20338. upon the next fontification round."
  20339. (let (b l)
  20340. (when (or (eq t buffer-invisibility-spec)
  20341. (assq 'org-link buffer-invisibility-spec))
  20342. (while (setq b (text-property-any 0 (length s)
  20343. 'invisible 'org-link s))
  20344. (setq s (concat (substring s 0 b)
  20345. (substring s (or (next-single-property-change
  20346. b 'invisible s)
  20347. (length s)))))))
  20348. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  20349. (setq s (concat (substring s 0 b)
  20350. (substring s (or (next-single-property-change
  20351. b 'org-cwidth s)
  20352. (length s))))))
  20353. (setq l (string-width s) b -1)
  20354. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  20355. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  20356. l))
  20357. (defun org-shorten-string (s maxlength)
  20358. "Shorten string S so tht it is no longer than MAXLENGTH characters.
  20359. If the string is shorter or has length MAXLENGTH, just return the
  20360. original string. If it is longer, the functions finds a space in the
  20361. string, breaks this string off at that locations and adds three dots
  20362. as ellipsis. Including the ellipsis, the string will not be longer
  20363. than MAXLENGTH. If finding a good breaking point in the string does
  20364. not work, the string is just chopped off in the middle of a word
  20365. if necessary."
  20366. (if (<= (length s) maxlength)
  20367. s
  20368. (let* ((n (max (- maxlength 4) 1))
  20369. (re (concat "\\`\\(.\\{1," (int-to-string n) "\\}[^ ]\\)\\([ ]\\|\\'\\)")))
  20370. (if (string-match re s)
  20371. (concat (match-string 1 s) "...")
  20372. (concat (substring s 0 (max (- maxlength 3) 0)) "...")))))
  20373. (defun org-get-indentation (&optional line)
  20374. "Get the indentation of the current line, interpreting tabs.
  20375. When LINE is given, assume it represents a line and compute its indentation."
  20376. (if line
  20377. (when (string-match "^ *" (org-remove-tabs line))
  20378. (match-end 0))
  20379. (save-excursion
  20380. (beginning-of-line 1)
  20381. (skip-chars-forward " \t")
  20382. (current-column))))
  20383. (defun org-get-string-indentation (s)
  20384. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  20385. (let ((n -1) (i 0) (w tab-width) c)
  20386. (catch 'exit
  20387. (while (< (setq n (1+ n)) (length s))
  20388. (setq c (aref s n))
  20389. (cond ((= c ?\ ) (setq i (1+ i)))
  20390. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  20391. (t (throw 'exit t)))))
  20392. i))
  20393. (defun org-remove-tabs (s &optional width)
  20394. "Replace tabulators in S with spaces.
  20395. Assumes that s is a single line, starting in column 0."
  20396. (setq width (or width tab-width))
  20397. (while (string-match "\t" s)
  20398. (setq s (replace-match
  20399. (make-string
  20400. (- (* width (/ (+ (match-beginning 0) width) width))
  20401. (match-beginning 0)) ?\ )
  20402. t t s)))
  20403. s)
  20404. (defun org-fix-indentation (line ind)
  20405. "Fix indentation in LINE.
  20406. IND is a cons cell with target and minimum indentation.
  20407. If the current indentation in LINE is smaller than the minimum,
  20408. leave it alone. If it is larger than ind, set it to the target."
  20409. (let* ((l (org-remove-tabs line))
  20410. (i (org-get-indentation l))
  20411. (i1 (car ind)) (i2 (cdr ind)))
  20412. (when (>= i i2) (setq l (substring line i2)))
  20413. (if (> i1 0)
  20414. (concat (make-string i1 ?\ ) l)
  20415. l)))
  20416. (defun org-remove-indentation (code &optional n)
  20417. "Remove maximum common indentation in string CODE and return it.
  20418. N may optionally be the number of columns to remove. Return CODE
  20419. as-is if removal failed."
  20420. (with-temp-buffer
  20421. (insert code)
  20422. (if (org-do-remove-indentation n) (buffer-string) code)))
  20423. (defun org-do-remove-indentation (&optional n)
  20424. "Remove the maximum common indentation from the buffer.
  20425. When optional argument N is a positive integer, remove exactly
  20426. that much characters from indentation, if possible. Return nil
  20427. if it fails."
  20428. (catch :exit
  20429. (goto-char (point-min))
  20430. ;; Find maximum common indentation, if not specified.
  20431. (let ((n (or n
  20432. (let ((min-ind (point-max)))
  20433. (save-excursion
  20434. (while (re-search-forward "^[ \t]*\\S-" nil t)
  20435. (let ((ind (1- (current-column))))
  20436. (if (zerop ind) (throw :exit nil)
  20437. (setq min-ind (min min-ind ind))))))
  20438. min-ind))))
  20439. (if (zerop n) (throw :exit nil)
  20440. ;; Remove exactly N indentation, but give up if not possible.
  20441. (while (not (eobp))
  20442. (let ((ind (progn (skip-chars-forward " \t") (current-column))))
  20443. (cond ((eolp) (delete-region (line-beginning-position) (point)))
  20444. ((< ind n) (throw :exit nil))
  20445. (t (org-indent-line-to (- ind n))))
  20446. (forward-line)))
  20447. ;; Signal success.
  20448. t))))
  20449. (defun org-fill-template (template alist)
  20450. "Find each %key of ALIST in TEMPLATE and replace it."
  20451. (let ((case-fold-search nil))
  20452. (dolist (entry (sort (copy-sequence alist)
  20453. (lambda (a b) (< (length (car a)) (length (car b))))))
  20454. (setq template
  20455. (replace-regexp-in-string
  20456. (concat "%" (regexp-quote (car entry)))
  20457. (or (cdr entry) "") template t t)))
  20458. template))
  20459. (defun org-base-buffer (buffer)
  20460. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  20461. (if (not buffer)
  20462. buffer
  20463. (or (buffer-base-buffer buffer)
  20464. buffer)))
  20465. (defun org-wrap (string &optional width lines)
  20466. "Wrap string to either a number of lines, or a width in characters.
  20467. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  20468. that costs. If there is a word longer than WIDTH, the text is actually
  20469. wrapped to the length of that word.
  20470. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  20471. many lines, whatever width that takes.
  20472. The return value is a list of lines, without newlines at the end."
  20473. (let* ((words (org-split-string string "[ \t\n]+"))
  20474. (maxword (apply 'max (mapcar 'org-string-width words)))
  20475. w ll)
  20476. (cond (width
  20477. (org-do-wrap words (max maxword width)))
  20478. (lines
  20479. (setq w maxword)
  20480. (setq ll (org-do-wrap words maxword))
  20481. (if (<= (length ll) lines)
  20482. ll
  20483. (setq ll words)
  20484. (while (> (length ll) lines)
  20485. (setq w (1+ w))
  20486. (setq ll (org-do-wrap words w)))
  20487. ll))
  20488. (t (error "Cannot wrap this")))))
  20489. (defun org-do-wrap (words width)
  20490. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  20491. (let (lines line)
  20492. (while words
  20493. (setq line (pop words))
  20494. (while (and words (< (+ (length line) (length (car words))) width))
  20495. (setq line (concat line " " (pop words))))
  20496. (setq lines (push line lines)))
  20497. (nreverse lines)))
  20498. (defun org-split-string (string &optional separators)
  20499. "Splits STRING into substrings at SEPARATORS.
  20500. SEPARATORS is a regular expression.
  20501. No empty strings are returned if there are matches at the beginning
  20502. and end of string."
  20503. ;; FIXME: why not use (split-string STRING SEPARATORS t)?
  20504. (let ((start 0) notfirst list)
  20505. (while (and (string-match (or separators "[ \f\t\n\r\v]+") string
  20506. (if (and notfirst
  20507. (= start (match-beginning 0))
  20508. (< start (length string)))
  20509. (1+ start) start))
  20510. (< (match-beginning 0) (length string)))
  20511. (setq notfirst t)
  20512. (or (eq (match-beginning 0) 0)
  20513. (and (eq (match-beginning 0) (match-end 0))
  20514. (eq (match-beginning 0) start))
  20515. (push (substring string start (match-beginning 0)) list))
  20516. (setq start (match-end 0)))
  20517. (or (eq start (length string))
  20518. (push (substring string start) list))
  20519. (nreverse list)))
  20520. (defun org-quote-vert (s)
  20521. "Replace \"|\" with \"\\vert\"."
  20522. (while (string-match "|" s)
  20523. (setq s (replace-match "\\vert" t t s)))
  20524. s)
  20525. (defun org-uuidgen-p (s)
  20526. "Is S an ID created by UUIDGEN?"
  20527. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  20528. (defun org-in-src-block-p (&optional inside)
  20529. "Whether point is in a code source block.
  20530. When INSIDE is non-nil, don't consider we are within a src block
  20531. when point is at #+BEGIN_SRC or #+END_SRC."
  20532. (let ((case-fold-search t))
  20533. (or (and (eq (get-char-property (point) 'src-block) t))
  20534. (and (not inside)
  20535. (save-match-data
  20536. (save-excursion
  20537. (beginning-of-line)
  20538. (looking-at ".*#\\+\\(begin\\|end\\)_src")))))))
  20539. (defun org-context ()
  20540. "Return a list of contexts of the current cursor position.
  20541. If several contexts apply, all are returned.
  20542. Each context entry is a list with a symbol naming the context, and
  20543. two positions indicating start and end of the context. Possible
  20544. contexts are:
  20545. :headline anywhere in a headline
  20546. :headline-stars on the leading stars in a headline
  20547. :todo-keyword on a TODO keyword (including DONE) in a headline
  20548. :tags on the TAGS in a headline
  20549. :priority on the priority cookie in a headline
  20550. :item on the first line of a plain list item
  20551. :item-bullet on the bullet/number of a plain list item
  20552. :checkbox on the checkbox in a plain list item
  20553. :table in an org-mode table
  20554. :table-special on a special filed in a table
  20555. :table-table in a table.el table
  20556. :clocktable in a clocktable
  20557. :src-block in a source block
  20558. :link on a hyperlink
  20559. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE, COMMENT.
  20560. :target on a <<target>>
  20561. :radio-target on a <<<radio-target>>>
  20562. :latex-fragment on a LaTeX fragment
  20563. :latex-preview on a LaTeX fragment with overlaid preview image
  20564. This function expects the position to be visible because it uses font-lock
  20565. faces as a help to recognize the following contexts: :table-special, :link,
  20566. and :keyword."
  20567. (let* ((f (get-text-property (point) 'face))
  20568. (faces (if (listp f) f (list f)))
  20569. (case-fold-search t)
  20570. (p (point)) clist o)
  20571. ;; First the large context
  20572. (cond
  20573. ((org-at-heading-p t)
  20574. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  20575. (when (progn
  20576. (beginning-of-line 1)
  20577. (looking-at org-todo-line-tags-regexp))
  20578. (push (org-point-in-group p 1 :headline-stars) clist)
  20579. (push (org-point-in-group p 2 :todo-keyword) clist)
  20580. (push (org-point-in-group p 4 :tags) clist))
  20581. (goto-char p)
  20582. (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
  20583. (when (looking-at "\\[#[A-Z0-9]\\]")
  20584. (push (org-point-in-group p 0 :priority) clist)))
  20585. ((org-at-item-p)
  20586. (push (org-point-in-group p 2 :item-bullet) clist)
  20587. (push (list :item (point-at-bol)
  20588. (save-excursion (org-end-of-item) (point)))
  20589. clist)
  20590. (and (org-at-item-checkbox-p)
  20591. (push (org-point-in-group p 0 :checkbox) clist)))
  20592. ((org-at-table-p)
  20593. (push (list :table (org-table-begin) (org-table-end)) clist)
  20594. (when (memq 'org-formula faces)
  20595. (push (list :table-special
  20596. (previous-single-property-change p 'face)
  20597. (next-single-property-change p 'face)) clist)))
  20598. ((org-at-table-p 'any)
  20599. (push (list :table-table) clist)))
  20600. (goto-char p)
  20601. (let ((case-fold-search t))
  20602. ;; New the "medium" contexts: clocktables, source blocks
  20603. (cond ((org-in-clocktable-p)
  20604. (push (list :clocktable
  20605. (and (or (looking-at "[ \t]*\\(#\\+BEGIN: clocktable\\)")
  20606. (re-search-backward "[ \t]*\\(#+BEGIN: clocktable\\)" nil t))
  20607. (match-beginning 1))
  20608. (and (re-search-forward "[ \t]*#\\+END:?" nil t)
  20609. (match-end 0))) clist))
  20610. ((org-in-src-block-p)
  20611. (push (list :src-block
  20612. (and (or (looking-at "[ \t]*\\(#\\+BEGIN_SRC\\)")
  20613. (re-search-backward "[ \t]*\\(#+BEGIN_SRC\\)" nil t))
  20614. (match-beginning 1))
  20615. (and (search-forward "#+END_SRC" nil t)
  20616. (match-beginning 0))) clist))))
  20617. (goto-char p)
  20618. ;; Now the small context
  20619. (cond
  20620. ((org-at-timestamp-p)
  20621. (push (org-point-in-group p 0 :timestamp) clist))
  20622. ((memq 'org-link faces)
  20623. (push (list :link
  20624. (previous-single-property-change p 'face)
  20625. (next-single-property-change p 'face)) clist))
  20626. ((memq 'org-special-keyword faces)
  20627. (push (list :keyword
  20628. (previous-single-property-change p 'face)
  20629. (next-single-property-change p 'face)) clist))
  20630. ((org-at-target-p)
  20631. (push (org-point-in-group p 0 :target) clist)
  20632. (goto-char (1- (match-beginning 0)))
  20633. (when (looking-at org-radio-target-regexp)
  20634. (push (org-point-in-group p 0 :radio-target) clist))
  20635. (goto-char p))
  20636. ((setq o (car (delq nil
  20637. (mapcar
  20638. (lambda (x)
  20639. (when (memq x org-latex-fragment-image-overlays) x))
  20640. (overlays-at (point))))))
  20641. (push (list :latex-fragment
  20642. (overlay-start o) (overlay-end o)) clist)
  20643. (push (list :latex-preview
  20644. (overlay-start o) (overlay-end o)) clist))
  20645. ((org-inside-LaTeX-fragment-p)
  20646. ;; FIXME: positions wrong.
  20647. (push (list :latex-fragment (point) (point)) clist)))
  20648. (setq clist (nreverse (delq nil clist)))
  20649. clist))
  20650. (defun org-in-regexp (regexp &optional nlines visually)
  20651. "Check if point is inside a match of REGEXP.
  20652. Normally only the current line is checked, but you can include
  20653. NLINES extra lines around point into the search. If VISUALLY is
  20654. set, require that the cursor is not after the match but really
  20655. on, so that the block visually is on the match.
  20656. Return nil or a cons cell (BEG . END) where BEG and END are,
  20657. respectively, the positions at the beginning and the end of the
  20658. match."
  20659. (catch :exit
  20660. (let ((pos (point))
  20661. (eol (line-end-position (if nlines (1+ nlines) 1))))
  20662. (save-excursion
  20663. (beginning-of-line (- 1 (or nlines 0)))
  20664. (while (and (re-search-forward regexp eol t)
  20665. (<= (match-beginning 0) pos))
  20666. (let ((end (match-end 0)))
  20667. (when (or (> end pos) (and (= end pos) (not visually)))
  20668. (throw :exit (cons (match-beginning 0) (match-end 0))))))))))
  20669. (define-obsolete-function-alias 'org-at-regexp-p 'org-in-regexp
  20670. "Org mode 8.3")
  20671. (defun org-between-regexps-p (start-re end-re &optional lim-up lim-down)
  20672. "Non-nil when point is between matches of START-RE and END-RE.
  20673. Also return a non-nil value when point is on one of the matches.
  20674. Optional arguments LIM-UP and LIM-DOWN bound the search; they are
  20675. buffer positions. Default values are the positions of headlines
  20676. surrounding the point.
  20677. The functions returns a cons cell whose car (resp. cdr) is the
  20678. position before START-RE (resp. after END-RE)."
  20679. (save-match-data
  20680. (let ((pos (point))
  20681. (limit-up (or lim-up (save-excursion (outline-previous-heading))))
  20682. (limit-down (or lim-down (save-excursion (outline-next-heading))))
  20683. beg end)
  20684. (save-excursion
  20685. ;; Point is on a block when on START-RE or if START-RE can be
  20686. ;; found before it...
  20687. (and (or (org-in-regexp start-re)
  20688. (re-search-backward start-re limit-up t))
  20689. (setq beg (match-beginning 0))
  20690. ;; ... and END-RE after it...
  20691. (goto-char (match-end 0))
  20692. (re-search-forward end-re limit-down t)
  20693. (> (setq end (match-end 0)) pos)
  20694. ;; ... without another START-RE in-between.
  20695. (goto-char (match-beginning 0))
  20696. (not (re-search-backward start-re (1+ beg) t))
  20697. ;; Return value.
  20698. (cons beg end))))))
  20699. (defun org-in-block-p (names)
  20700. "Non-nil when point belongs to a block whose name belongs to NAMES.
  20701. NAMES is a list of strings containing names of blocks.
  20702. Return first block name matched, or nil. Beware that in case of
  20703. nested blocks, the returned name may not belong to the closest
  20704. block from point."
  20705. (save-match-data
  20706. (catch 'exit
  20707. (let ((case-fold-search t)
  20708. (lim-up (save-excursion (outline-previous-heading)))
  20709. (lim-down (save-excursion (outline-next-heading))))
  20710. (dolist (name names)
  20711. (let ((n (regexp-quote name)))
  20712. (when (org-between-regexps-p
  20713. (concat "^[ \t]*#\\+begin_" n)
  20714. (concat "^[ \t]*#\\+end_" n)
  20715. lim-up lim-down)
  20716. (throw 'exit n)))))
  20717. nil)))
  20718. (defun org-occur-in-agenda-files (regexp &optional _nlines)
  20719. "Call `multi-occur' with buffers for all agenda files."
  20720. (interactive "sOrg-files matching: ")
  20721. (let* ((files (org-agenda-files))
  20722. (tnames (mapcar #'file-truename files))
  20723. (extra org-agenda-text-search-extra-files))
  20724. (when (eq (car extra) 'agenda-archives)
  20725. (setq extra (cdr extra))
  20726. (setq files (org-add-archive-files files)))
  20727. (dolist (f extra)
  20728. (unless (member (file-truename f) tnames)
  20729. (unless (member f files) (setq files (append files (list f))))
  20730. (setq tnames (append tnames (list (file-truename f))))))
  20731. (multi-occur
  20732. (mapcar (lambda (x)
  20733. (with-current-buffer
  20734. ;; FIXME: Why not just (find-file-noselect x)?
  20735. ;; Is it to avoid the "revert buffer" prompt?
  20736. (or (get-file-buffer x) (find-file-noselect x))
  20737. (widen)
  20738. (current-buffer)))
  20739. files)
  20740. regexp)))
  20741. (if (boundp 'occur-mode-find-occurrence-hook)
  20742. ;; Emacs 23
  20743. (add-hook 'occur-mode-find-occurrence-hook
  20744. (lambda ()
  20745. (when (derived-mode-p 'org-mode)
  20746. (org-reveal))))
  20747. ;; Emacs 22
  20748. (defadvice occur-mode-goto-occurrence
  20749. (after org-occur-reveal activate)
  20750. (and (derived-mode-p 'org-mode) (org-reveal)))
  20751. (defadvice occur-mode-goto-occurrence-other-window
  20752. (after org-occur-reveal activate)
  20753. (and (derived-mode-p 'org-mode) (org-reveal)))
  20754. (defadvice occur-mode-display-occurrence
  20755. (after org-occur-reveal activate)
  20756. (when (derived-mode-p 'org-mode)
  20757. (let ((pos (occur-mode-find-occurrence)))
  20758. (with-current-buffer (marker-buffer pos)
  20759. (save-excursion
  20760. (goto-char pos)
  20761. (org-reveal)))))))
  20762. (defun org-occur-link-in-agenda-files ()
  20763. "Create a link and search for it in the agendas.
  20764. The link is not stored in `org-stored-links', it is just created
  20765. for the search purpose."
  20766. (interactive)
  20767. (let ((link (condition-case nil
  20768. (org-store-link nil)
  20769. (error "Unable to create a link to here"))))
  20770. (org-occur-in-agenda-files (regexp-quote link))))
  20771. (defun org-reverse-string (string)
  20772. "Return the reverse of STRING."
  20773. (apply 'string (reverse (string-to-list string))))
  20774. ;; defsubst org-uniquify must be defined before first use
  20775. (defun org-uniquify-alist (alist)
  20776. "Merge elements of ALIST with the same key.
  20777. For example, in this alist:
  20778. \(org-uniquify-alist \\='((a 1) (b 2) (a 3)))
  20779. => \\='((a 1 3) (b 2))
  20780. merge (a 1) and (a 3) into (a 1 3).
  20781. The function returns the new ALIST."
  20782. (let (rtn)
  20783. (dolist (e alist rtn)
  20784. (let (n)
  20785. (if (not (assoc (car e) rtn))
  20786. (push e rtn)
  20787. (setq n (cons (car e) (append (cdr (assoc (car e) rtn)) (cdr e))))
  20788. (setq rtn (assq-delete-all (car e) rtn))
  20789. (push n rtn))))))
  20790. (defun org-delete-all (elts list)
  20791. "Remove all elements in ELTS from LIST."
  20792. (while elts
  20793. (setq list (delete (pop elts) list)))
  20794. list)
  20795. (defun org-back-over-empty-lines ()
  20796. "Move backwards over whitespace, to the beginning of the first empty line.
  20797. Returns the number of empty lines passed."
  20798. (let ((pos (point)))
  20799. (if (cdr (assoc 'heading org-blank-before-new-entry))
  20800. (skip-chars-backward " \t\n\r")
  20801. (unless (eobp)
  20802. (forward-line -1)))
  20803. (beginning-of-line 2)
  20804. (goto-char (min (point) pos))
  20805. (count-lines (point) pos)))
  20806. (defun org-skip-whitespace ()
  20807. (skip-chars-forward " \t\n\r"))
  20808. (defun org-point-in-group (point group &optional context)
  20809. "Check if POINT is in match-group GROUP.
  20810. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  20811. match. If the match group does not exist or point is not inside it,
  20812. return nil."
  20813. (and (match-beginning group)
  20814. (>= point (match-beginning group))
  20815. (<= point (match-end group))
  20816. (if context
  20817. (list context (match-beginning group) (match-end group))
  20818. t)))
  20819. (defun org-switch-to-buffer-other-window (&rest args)
  20820. "Switch to buffer in a second window on the current frame.
  20821. In particular, do not allow pop-up frames.
  20822. Returns the newly created buffer."
  20823. (org-no-popups
  20824. (apply 'switch-to-buffer-other-window args)))
  20825. (defun org-combine-plists (&rest plists)
  20826. "Create a single property list from all plists in PLISTS.
  20827. The process starts by copying the first list, and then setting properties
  20828. from the other lists. Settings in the last list are the most significant
  20829. ones and overrule settings in the other lists."
  20830. (let ((rtn (copy-sequence (pop plists)))
  20831. p v ls)
  20832. (while plists
  20833. (setq ls (pop plists))
  20834. (while ls
  20835. (setq p (pop ls) v (pop ls))
  20836. (setq rtn (plist-put rtn p v))))
  20837. rtn))
  20838. (defun org-replace-escapes (string table)
  20839. "Replace %-escapes in STRING with values in TABLE.
  20840. TABLE is an association list with keys like \"%a\" and string values.
  20841. The sequences in STRING may contain normal field width and padding information,
  20842. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  20843. so values can contain further %-escapes if they are define later in TABLE."
  20844. (let ((tbl (copy-alist table))
  20845. (case-fold-search nil)
  20846. (pchg 0)
  20847. re rpl)
  20848. (dolist (e tbl)
  20849. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  20850. (when (and (cdr e) (string-match re (cdr e)))
  20851. (let ((sref (substring (cdr e) (match-beginning 0) (match-end 0)))
  20852. (safe "SREF"))
  20853. (add-text-properties 0 3 (list 'sref sref) safe)
  20854. (setcdr e (replace-match safe t t (cdr e)))))
  20855. (while (string-match re string)
  20856. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  20857. (cdr e)))
  20858. (setq string (replace-match rpl t t string))))
  20859. (while (setq pchg (next-property-change pchg string))
  20860. (let ((sref (get-text-property pchg 'sref string)))
  20861. (when (and sref (string-match "SREF" string pchg))
  20862. (setq string (replace-match sref t t string)))))
  20863. string))
  20864. (defun org-sublist (list start end)
  20865. "Return a section of LIST, from START to END.
  20866. Counting starts at 1."
  20867. (cl-subseq list (1- start) end))
  20868. (make-obsolete 'org-sublist "cl-subseq (note the 0-based counting)." "Org 9.0")
  20869. (defun org-find-base-buffer-visiting (file)
  20870. "Like `find-buffer-visiting' but always return the base buffer and
  20871. not an indirect buffer."
  20872. (let ((buf (or (get-file-buffer file)
  20873. (find-buffer-visiting file))))
  20874. (if buf
  20875. (or (buffer-base-buffer buf) buf)
  20876. nil)))
  20877. (define-obsolete-function-alias 'org-image-file-name-regexp 'image-file-name-regexp
  20878. "Org 9.0")
  20879. ;;; TODO: Only called once, from ox-odt which should probably use
  20880. ;;; org-export-inline-image-p or something.
  20881. (defun org-file-image-p (file)
  20882. "Return non-nil if FILE is an image."
  20883. (save-match-data
  20884. (string-match (image-file-name-regexp) file)))
  20885. (defun org-get-cursor-date (&optional with-time)
  20886. "Return the date at cursor in as a time.
  20887. This works in the calendar and in the agenda, anywhere else it just
  20888. returns the current time.
  20889. If WITH-TIME is non-nil, returns the time of the event at point (in
  20890. the agenda) or the current time of the day."
  20891. (let (date day defd tp hod mod)
  20892. (when with-time
  20893. (setq tp (get-text-property (point) 'time))
  20894. (when (and tp (string-match "\\([0-9][0-9]\\):\\([0-9][0-9]\\)" tp))
  20895. (setq hod (string-to-number (match-string 1 tp))
  20896. mod (string-to-number (match-string 2 tp))))
  20897. (or tp (let ((now (decode-time)))
  20898. (setq hod (nth 2 now)
  20899. mod (nth 1 now)))))
  20900. (cond
  20901. ((eq major-mode 'calendar-mode)
  20902. (setq date (calendar-cursor-to-date)
  20903. defd (encode-time 0 (or mod 0) (or hod 0)
  20904. (nth 1 date) (nth 0 date) (nth 2 date))))
  20905. ((eq major-mode 'org-agenda-mode)
  20906. (setq day (get-text-property (point) 'day))
  20907. (when day
  20908. (setq date (calendar-gregorian-from-absolute day)
  20909. defd (encode-time 0 (or mod 0) (or hod 0)
  20910. (nth 1 date) (nth 0 date) (nth 2 date))))))
  20911. (or defd (current-time))))
  20912. (defun org-mark-subtree (&optional up)
  20913. "Mark the current subtree.
  20914. This puts point at the start of the current subtree, and mark at
  20915. the end. If a numeric prefix UP is given, move up into the
  20916. hierarchy of headlines by UP levels before marking the subtree."
  20917. (interactive "P")
  20918. (org-with-limited-levels
  20919. (cond ((org-at-heading-p) (beginning-of-line))
  20920. ((org-before-first-heading-p) (user-error "Not in a subtree"))
  20921. (t (outline-previous-visible-heading 1))))
  20922. (when up (while (and (> up 0) (org-up-heading-safe)) (cl-decf up)))
  20923. (if (org-called-interactively-p 'any)
  20924. (call-interactively 'org-mark-element)
  20925. (org-mark-element)))
  20926. ;;; Indentation
  20927. (defun org--get-expected-indentation (element contentsp)
  20928. "Expected indentation column for current line, according to ELEMENT.
  20929. ELEMENT is an element containing point. CONTENTSP is non-nil
  20930. when indentation is to be computed according to contents of
  20931. ELEMENT."
  20932. (let ((type (org-element-type element))
  20933. (start (org-element-property :begin element))
  20934. (post-affiliated (org-element-property :post-affiliated element)))
  20935. (org-with-wide-buffer
  20936. (cond
  20937. (contentsp
  20938. (cl-case type
  20939. ((diary-sexp footnote-definition) 0)
  20940. ((headline inlinetask nil)
  20941. (if (not org-adapt-indentation) 0
  20942. (let ((level (org-current-level)))
  20943. (if level (1+ level) 0))))
  20944. ((item plain-list) (org-list-item-body-column post-affiliated))
  20945. (t
  20946. (goto-char start)
  20947. (org-get-indentation))))
  20948. ((memq type '(headline inlinetask nil))
  20949. (if (save-excursion (beginning-of-line) (looking-at "[ \t]*$"))
  20950. (org--get-expected-indentation element t)
  20951. 0))
  20952. ((memq type '(diary-sexp footnote-definition)) 0)
  20953. ;; First paragraph of a footnote definition or an item.
  20954. ;; Indent like parent.
  20955. ((< (line-beginning-position) start)
  20956. (org--get-expected-indentation
  20957. (org-element-property :parent element) t))
  20958. ;; At first line: indent according to previous sibling, if any,
  20959. ;; ignoring footnote definitions and inline tasks, or parent's
  20960. ;; contents.
  20961. ((= (line-beginning-position) start)
  20962. (catch 'exit
  20963. (while t
  20964. (if (= (point-min) start) (throw 'exit 0)
  20965. (goto-char (1- start))
  20966. (let* ((previous (org-element-at-point))
  20967. (parent previous))
  20968. (while (and parent (<= (org-element-property :end parent) start))
  20969. (setq previous parent
  20970. parent (org-element-property :parent parent)))
  20971. (cond
  20972. ((not previous) (throw 'exit 0))
  20973. ((> (org-element-property :end previous) start)
  20974. (throw 'exit (org--get-expected-indentation previous t)))
  20975. ((memq (org-element-type previous)
  20976. '(footnote-definition inlinetask))
  20977. (setq start (org-element-property :begin previous)))
  20978. (t (goto-char (org-element-property :begin previous))
  20979. (throw 'exit
  20980. (if (bolp) (org-get-indentation)
  20981. ;; At first paragraph in an item or
  20982. ;; a footnote definition.
  20983. (org--get-expected-indentation
  20984. (org-element-property :parent previous) t))))))))))
  20985. ;; Otherwise, move to the first non-blank line above.
  20986. (t
  20987. (beginning-of-line)
  20988. (let ((pos (point)))
  20989. (skip-chars-backward " \r\t\n")
  20990. (cond
  20991. ;; Two blank lines end a footnote definition or a plain
  20992. ;; list. When we indent an empty line after them, the
  20993. ;; containing list or footnote definition is over, so it
  20994. ;; qualifies as a previous sibling. Therefore, we indent
  20995. ;; like its first line.
  20996. ((and (memq type '(footnote-definition plain-list))
  20997. (> (count-lines (point) pos) 2))
  20998. (goto-char start)
  20999. (org-get-indentation))
  21000. ;; Line above is the first one of a paragraph at the
  21001. ;; beginning of an item or a footnote definition. Indent
  21002. ;; like parent.
  21003. ((< (line-beginning-position) start)
  21004. (org--get-expected-indentation
  21005. (org-element-property :parent element) t))
  21006. ;; Line above is the beginning of an element, i.e., point
  21007. ;; was originally on the blank lines between element's start
  21008. ;; and contents.
  21009. ((= (line-beginning-position) post-affiliated)
  21010. (org--get-expected-indentation element t))
  21011. ;; POS is after contents in a greater element. Indent like
  21012. ;; the beginning of the element.
  21013. ;;
  21014. ;; As a special case, if point is at the end of a footnote
  21015. ;; definition or an item, indent like the very last element
  21016. ;; within.
  21017. ((and (not (eq type 'paragraph))
  21018. (let ((cend (org-element-property :contents-end element)))
  21019. (and cend (<= cend pos))))
  21020. (if (memq type '(footnote-definition item plain-list))
  21021. (let ((last (org-element-at-point)))
  21022. (org--get-expected-indentation
  21023. last
  21024. (memq (org-element-type last)
  21025. '(footnote-definition item plain-list))))
  21026. (goto-char start)
  21027. (org-get-indentation)))
  21028. ;; In any other case, indent like the current line.
  21029. (t (org-get-indentation)))))))))
  21030. (defun org--align-node-property ()
  21031. "Align node property at point.
  21032. Alignment is done according to `org-property-format', which see."
  21033. (when (save-excursion
  21034. (beginning-of-line)
  21035. (looking-at org-property-re))
  21036. (replace-match
  21037. (concat (match-string 4)
  21038. (org-trim
  21039. (format org-property-format (match-string 1) (match-string 3))))
  21040. t t)))
  21041. (defun org-indent-line ()
  21042. "Indent line depending on context.
  21043. Indentation is done according to the following rules:
  21044. - Footnote definitions, diary sexps, headlines and inline tasks
  21045. have to start at column 0.
  21046. - On the very first line of an element, consider, in order, the
  21047. next rules until one matches:
  21048. 1. If there's a sibling element before, ignoring footnote
  21049. definitions and inline tasks, indent like its first line.
  21050. 2. If element has a parent, indent like its contents. More
  21051. precisely, if parent is an item, indent after the
  21052. description part, if any, or the bullet (see
  21053. `org-list-description-max-indent'). Else, indent like
  21054. parent's first line.
  21055. 3. Otherwise, indent relatively to current level, if
  21056. `org-adapt-indentation' is non-nil, or to left margin.
  21057. - On a blank line at the end of an element, indent according to
  21058. the type of the element. More precisely
  21059. 1. If element is a plain list, an item, or a footnote
  21060. definition, indent like the very last element within.
  21061. 2. If element is a paragraph, indent like its last non blank
  21062. line.
  21063. 3. Otherwise, indent like its very first line.
  21064. - In the code part of a source block, use language major mode
  21065. to indent current line if `org-src-tab-acts-natively' is
  21066. non-nil. If it is nil, do nothing.
  21067. - Otherwise, indent like the first non-blank line above.
  21068. The function doesn't indent an item as it could break the whole
  21069. list structure. Instead, use \\<org-mode-map>\\[org-shiftmetaleft] or \
  21070. \\[org-shiftmetaright].
  21071. Also align node properties according to `org-property-format'."
  21072. (interactive)
  21073. (cond
  21074. (orgstruct-is-++
  21075. (let ((indent-line-function
  21076. (cl-cadadr (assq 'indent-line-function org-fb-vars))))
  21077. (indent-according-to-mode)))
  21078. ((org-at-heading-p) 'noindent)
  21079. (t
  21080. (let* ((element (save-excursion (beginning-of-line) (org-element-at-point)))
  21081. (type (org-element-type element)))
  21082. (cond ((and (memq type '(plain-list item))
  21083. (= (line-beginning-position)
  21084. (org-element-property :post-affiliated element)))
  21085. 'noindent)
  21086. ((and (eq type 'src-block)
  21087. org-src-tab-acts-natively
  21088. (> (line-beginning-position)
  21089. (org-element-property :post-affiliated element))
  21090. (< (line-beginning-position)
  21091. (org-with-wide-buffer
  21092. (goto-char (org-element-property :end element))
  21093. (skip-chars-backward " \r\t\n")
  21094. (line-beginning-position))))
  21095. (org-babel-do-key-sequence-in-edit-buffer (kbd "TAB")))
  21096. (t
  21097. (let ((column (org--get-expected-indentation element nil)))
  21098. ;; Preserve current column.
  21099. (if (<= (current-column) (current-indentation))
  21100. (org-indent-line-to column)
  21101. (save-excursion (org-indent-line-to column))))
  21102. ;; Align node property. Also preserve current column.
  21103. (when (eq type 'node-property)
  21104. (let ((column (current-column)))
  21105. (org--align-node-property)
  21106. (org-move-to-column column)))))))))
  21107. (defun org-indent-region (start end)
  21108. "Indent each non-blank line in the region.
  21109. Called from a program, START and END specify the region to
  21110. indent. The function will not indent contents of example blocks,
  21111. verse blocks and export blocks as leading white spaces are
  21112. assumed to be significant there."
  21113. (interactive "r")
  21114. (save-excursion
  21115. (goto-char start)
  21116. (skip-chars-forward " \r\t\n")
  21117. (unless (eobp) (beginning-of-line))
  21118. (let ((indent-to
  21119. (lambda (ind pos)
  21120. ;; Set IND as indentation for all lines between point and
  21121. ;; POS. Blank lines are ignored. Leave point after POS
  21122. ;; once done.
  21123. (let ((limit (copy-marker pos)))
  21124. (while (< (point) limit)
  21125. (unless (org-looking-at-p "[ \t]*$") (org-indent-line-to ind))
  21126. (forward-line))
  21127. (set-marker limit nil))))
  21128. (end (copy-marker end)))
  21129. (while (< (point) end)
  21130. (if (or (org-looking-at-p " \r\t\n") (org-at-heading-p)) (forward-line)
  21131. (let* ((element (org-element-at-point))
  21132. (type (org-element-type element))
  21133. (element-end (copy-marker (org-element-property :end element)))
  21134. (ind (org--get-expected-indentation element nil)))
  21135. (cond
  21136. ((or (memq type '(paragraph table table-row))
  21137. (not (or (org-element-property :contents-begin element)
  21138. (memq type
  21139. '(example-block export-block src-block)))))
  21140. ;; Elements here are indented as a single block. Also
  21141. ;; align node properties.
  21142. (when (eq type 'node-property)
  21143. (org--align-node-property)
  21144. (beginning-of-line))
  21145. (funcall indent-to ind (min element-end end)))
  21146. (t
  21147. ;; Elements in this category consist of three parts:
  21148. ;; before the contents, the contents, and after the
  21149. ;; contents. The contents are treated specially,
  21150. ;; according to the element type, or not indented at
  21151. ;; all. Other parts are indented as a single block.
  21152. (let* ((post (copy-marker
  21153. (org-element-property :post-affiliated element)))
  21154. (cbeg
  21155. (copy-marker
  21156. (cond
  21157. ((not (org-element-property :contents-begin element))
  21158. ;; Fake contents for source blocks.
  21159. (org-with-wide-buffer
  21160. (goto-char post)
  21161. (forward-line)
  21162. (point)))
  21163. ((memq type '(footnote-definition item plain-list))
  21164. ;; Contents in these elements could start on
  21165. ;; the same line as the beginning of the
  21166. ;; element. Make sure we start indenting
  21167. ;; from the second line.
  21168. (org-with-wide-buffer
  21169. (goto-char post)
  21170. (end-of-line)
  21171. (skip-chars-forward " \r\t\n")
  21172. (if (eobp) (point) (line-beginning-position))))
  21173. (t (org-element-property :contents-begin element)))))
  21174. (cend (copy-marker
  21175. (or (org-element-property :contents-end element)
  21176. ;; Fake contents for source blocks.
  21177. (org-with-wide-buffer
  21178. (goto-char element-end)
  21179. (skip-chars-backward " \r\t\n")
  21180. (line-beginning-position)))
  21181. t)))
  21182. ;; Do not change items indentation individually as it
  21183. ;; might break the list as a whole. On the other
  21184. ;; hand, when at a plain list, indent it as a whole.
  21185. (cond ((eq type 'plain-list)
  21186. (let ((offset (- ind (org-get-indentation))))
  21187. (unless (zerop offset)
  21188. (indent-rigidly (org-element-property :begin element)
  21189. (org-element-property :end element)
  21190. offset))
  21191. (goto-char cbeg)))
  21192. ((eq type 'item) (goto-char cbeg))
  21193. (t (funcall indent-to ind (min cbeg end))))
  21194. (when (< (point) end)
  21195. (cl-case type
  21196. ((example-block export-block verse-block))
  21197. (src-block
  21198. ;; In a source block, indent source code
  21199. ;; according to language major mode, but only if
  21200. ;; `org-src-tab-acts-natively' is non-nil.
  21201. (when (and (< (point) end) org-src-tab-acts-natively)
  21202. (ignore-errors
  21203. (org-babel-do-in-edit-buffer
  21204. (indent-region (point-min) (point-max))))))
  21205. (t (org-indent-region (point) (min cend end))))
  21206. (goto-char (min cend end))
  21207. (when (< (point) end)
  21208. (funcall indent-to ind (min element-end end))))
  21209. (set-marker post nil)
  21210. (set-marker cbeg nil)
  21211. (set-marker cend nil))))
  21212. (set-marker element-end nil))))
  21213. (set-marker end nil))))
  21214. (defun org-indent-drawer ()
  21215. "Indent the drawer at point."
  21216. (interactive)
  21217. (unless (save-excursion
  21218. (beginning-of-line)
  21219. (org-looking-at-p org-drawer-regexp))
  21220. (user-error "Not at a drawer"))
  21221. (let ((element (org-element-at-point)))
  21222. (unless (memq (org-element-type element) '(drawer property-drawer))
  21223. (user-error "Not at a drawer"))
  21224. (org-with-wide-buffer
  21225. (org-indent-region (org-element-property :begin element)
  21226. (org-element-property :end element))))
  21227. (message "Drawer at point indented"))
  21228. (defun org-indent-block ()
  21229. "Indent the block at point."
  21230. (interactive)
  21231. (unless (save-excursion
  21232. (beginning-of-line)
  21233. (let ((case-fold-search t))
  21234. (org-looking-at-p "[ \t]*#\\+\\(begin\\|end\\)_")))
  21235. (user-error "Not at a block"))
  21236. (let ((element (org-element-at-point)))
  21237. (unless (memq (org-element-type element)
  21238. '(comment-block center-block dynamic-block example-block
  21239. export-block quote-block special-block
  21240. src-block verse-block))
  21241. (user-error "Not at a block"))
  21242. (org-with-wide-buffer
  21243. (org-indent-region (org-element-property :begin element)
  21244. (org-element-property :end element))))
  21245. (message "Block at point indented"))
  21246. ;;; Filling
  21247. ;; We use our own fill-paragraph and auto-fill functions.
  21248. ;; `org-fill-paragraph' relies on adaptive filling and context
  21249. ;; checking. Appropriate `fill-prefix' is computed with
  21250. ;; `org-adaptive-fill-function'.
  21251. ;; `org-auto-fill-function' takes care of auto-filling. It calls
  21252. ;; `do-auto-fill' only on valid areas with `fill-prefix' shadowed with
  21253. ;; `org-adaptive-fill-function' value. Internally,
  21254. ;; `org-comment-line-break-function' breaks the line.
  21255. ;; `org-setup-filling' installs filling and auto-filling related
  21256. ;; variables during `org-mode' initialization.
  21257. (defvar org-element-paragraph-separate) ; org-element.el
  21258. (defun org-setup-filling ()
  21259. (require 'org-element)
  21260. ;; Prevent auto-fill from inserting unwanted new items.
  21261. (when (boundp 'fill-nobreak-predicate)
  21262. (setq-local
  21263. fill-nobreak-predicate
  21264. (org-uniquify
  21265. (append fill-nobreak-predicate
  21266. '(org-fill-line-break-nobreak-p
  21267. org-fill-paragraph-with-timestamp-nobreak-p)))))
  21268. (let ((paragraph-ending (substring org-element-paragraph-separate 1)))
  21269. (setq-local paragraph-start paragraph-ending)
  21270. (setq-local paragraph-separate paragraph-ending))
  21271. (setq-local fill-paragraph-function 'org-fill-paragraph)
  21272. (setq-local auto-fill-inhibit-regexp nil)
  21273. (setq-local adaptive-fill-function 'org-adaptive-fill-function)
  21274. (setq-local normal-auto-fill-function 'org-auto-fill-function)
  21275. (setq-local comment-line-break-function 'org-comment-line-break-function))
  21276. (defun org-fill-line-break-nobreak-p ()
  21277. "Non-nil when a new line at point would create an Org line break."
  21278. (save-excursion
  21279. (skip-chars-backward "[ \t]")
  21280. (skip-chars-backward "\\\\")
  21281. (looking-at "\\\\\\\\\\($\\|[^\\\\]\\)")))
  21282. (defun org-fill-paragraph-with-timestamp-nobreak-p ()
  21283. "Non-nil when a new line at point would split a timestamp."
  21284. (and (org-at-timestamp-p t)
  21285. (not (looking-at org-ts-regexp-both))))
  21286. (declare-function message-in-body-p "message" ())
  21287. (defvar orgtbl-line-start-regexp) ; From org-table.el
  21288. (defun org-adaptive-fill-function ()
  21289. "Compute a fill prefix for the current line.
  21290. Return fill prefix, as a string, or nil if current line isn't
  21291. meant to be filled. For convenience, if `adaptive-fill-regexp'
  21292. matches in paragraphs or comments, use it."
  21293. (catch 'exit
  21294. (when (derived-mode-p 'message-mode)
  21295. (save-excursion
  21296. (beginning-of-line)
  21297. (cond ((not (message-in-body-p)) (throw 'exit nil))
  21298. ((org-looking-at-p org-table-line-regexp) (throw 'exit nil))
  21299. ((looking-at message-cite-prefix-regexp)
  21300. (throw 'exit (match-string-no-properties 0)))
  21301. ((looking-at org-outline-regexp)
  21302. (throw 'exit (make-string (length (match-string 0)) ?\s))))))
  21303. (org-with-wide-buffer
  21304. (unless (org-at-heading-p)
  21305. (let* ((p (line-beginning-position))
  21306. (element (save-excursion
  21307. (beginning-of-line)
  21308. (org-element-at-point)))
  21309. (type (org-element-type element))
  21310. (post-affiliated (org-element-property :post-affiliated element)))
  21311. (unless (< p post-affiliated)
  21312. (cl-case type
  21313. (comment
  21314. (save-excursion
  21315. (beginning-of-line)
  21316. (looking-at "[ \t]*")
  21317. (concat (match-string 0) "# ")))
  21318. (footnote-definition "")
  21319. ((item plain-list)
  21320. (make-string (org-list-item-body-column post-affiliated) ?\s))
  21321. (paragraph
  21322. ;; Fill prefix is usually the same as the current line,
  21323. ;; unless the paragraph is at the beginning of an item.
  21324. (let ((parent (org-element-property :parent element)))
  21325. (save-excursion
  21326. (beginning-of-line)
  21327. (cond ((eq (org-element-type parent) 'item)
  21328. (make-string (org-list-item-body-column
  21329. (org-element-property :begin parent))
  21330. ?\s))
  21331. ((and adaptive-fill-regexp
  21332. ;; Locally disable
  21333. ;; `adaptive-fill-function' to let
  21334. ;; `fill-context-prefix' handle
  21335. ;; `adaptive-fill-regexp' variable.
  21336. (let (adaptive-fill-function)
  21337. (fill-context-prefix
  21338. post-affiliated
  21339. (org-element-property :end element)))))
  21340. ((looking-at "[ \t]+") (match-string 0))
  21341. (t "")))))
  21342. (comment-block
  21343. ;; Only fill contents if P is within block boundaries.
  21344. (let* ((cbeg (save-excursion (goto-char post-affiliated)
  21345. (forward-line)
  21346. (point)))
  21347. (cend (save-excursion
  21348. (goto-char (org-element-property :end element))
  21349. (skip-chars-backward " \r\t\n")
  21350. (line-beginning-position))))
  21351. (when (and (>= p cbeg) (< p cend))
  21352. (if (save-excursion (beginning-of-line) (looking-at "[ \t]+"))
  21353. (match-string 0)
  21354. "")))))))))))
  21355. (declare-function message-goto-body "message" ())
  21356. (defvar message-cite-prefix-regexp) ; From message.el
  21357. (defun org-fill-paragraph (&optional justify)
  21358. "Fill element at point, when applicable.
  21359. This function only applies to comment blocks, comments, example
  21360. blocks and paragraphs. Also, as a special case, re-align table
  21361. when point is at one.
  21362. If JUSTIFY is non-nil (interactively, with prefix argument),
  21363. justify as well. If `sentence-end-double-space' is non-nil, then
  21364. period followed by one space does not end a sentence, so don't
  21365. break a line there. The variable `fill-column' controls the
  21366. width for filling.
  21367. For convenience, when point is at a plain list, an item or
  21368. a footnote definition, try to fill the first paragraph within."
  21369. (interactive)
  21370. (if (and (derived-mode-p 'message-mode)
  21371. (or (not (message-in-body-p))
  21372. (save-excursion (move-beginning-of-line 1)
  21373. (looking-at message-cite-prefix-regexp))))
  21374. ;; First ensure filling is correct in message-mode.
  21375. (let ((fill-paragraph-function
  21376. (cl-cadadr (assq 'fill-paragraph-function org-fb-vars)))
  21377. (fill-prefix (cl-cadadr (assq 'fill-prefix org-fb-vars)))
  21378. (paragraph-start (cl-cadadr (assq 'paragraph-start org-fb-vars)))
  21379. (paragraph-separate
  21380. (cl-cadadr (assq 'paragraph-separate org-fb-vars))))
  21381. (fill-paragraph nil))
  21382. (with-syntax-table org-mode-transpose-word-syntax-table
  21383. ;; Move to end of line in order to get the first paragraph
  21384. ;; within a plain list or a footnote definition.
  21385. (let ((element (save-excursion
  21386. (end-of-line)
  21387. (or (ignore-errors (org-element-at-point))
  21388. (user-error "An element cannot be parsed line %d"
  21389. (line-number-at-pos (point)))))))
  21390. ;; First check if point is in a blank line at the beginning of
  21391. ;; the buffer. In that case, ignore filling.
  21392. (cl-case (org-element-type element)
  21393. ;; Use major mode filling function is src blocks.
  21394. (src-block (org-babel-do-key-sequence-in-edit-buffer (kbd "M-q")))
  21395. ;; Align Org tables, leave table.el tables as-is.
  21396. (table-row (org-table-align) t)
  21397. (table
  21398. (when (eq (org-element-property :type element) 'org)
  21399. (save-excursion
  21400. (goto-char (org-element-property :post-affiliated element))
  21401. (org-table-align)))
  21402. t)
  21403. (paragraph
  21404. ;; Paragraphs may contain `line-break' type objects.
  21405. (let ((beg (max (point-min)
  21406. (org-element-property :contents-begin element)))
  21407. (end (min (point-max)
  21408. (org-element-property :contents-end element))))
  21409. ;; Do nothing if point is at an affiliated keyword.
  21410. (if (< (line-end-position) beg) t
  21411. (when (derived-mode-p 'message-mode)
  21412. ;; In `message-mode', do not fill following citation
  21413. ;; in current paragraph nor text before message body.
  21414. (let ((body-start (save-excursion (message-goto-body))))
  21415. (when body-start (setq beg (max body-start beg))))
  21416. (when (save-excursion
  21417. (re-search-forward
  21418. (concat "^" message-cite-prefix-regexp) end t))
  21419. (setq end (match-beginning 0))))
  21420. ;; Fill paragraph, taking line breaks into account.
  21421. (save-excursion
  21422. (goto-char beg)
  21423. (let ((cuts (list beg)))
  21424. (while (re-search-forward "\\\\\\\\[ \t]*\n" end t)
  21425. (when (eq 'line-break
  21426. (org-element-type
  21427. (save-excursion (backward-char)
  21428. (org-element-context))))
  21429. (push (point) cuts)))
  21430. (dolist (c (delq end cuts))
  21431. (fill-region-as-paragraph c end justify)
  21432. (setq end c))))
  21433. t)))
  21434. ;; Contents of `comment-block' type elements should be
  21435. ;; filled as plain text, but only if point is within block
  21436. ;; markers.
  21437. (comment-block
  21438. (let* ((case-fold-search t)
  21439. (beg (save-excursion
  21440. (goto-char (org-element-property :begin element))
  21441. (re-search-forward "^[ \t]*#\\+begin_comment" nil t)
  21442. (forward-line)
  21443. (point)))
  21444. (end (save-excursion
  21445. (goto-char (org-element-property :end element))
  21446. (re-search-backward "^[ \t]*#\\+end_comment" nil t)
  21447. (line-beginning-position))))
  21448. (if (or (< (point) beg) (> (point) end)) t
  21449. (fill-region-as-paragraph
  21450. (save-excursion (end-of-line)
  21451. (re-search-backward "^[ \t]*$" beg 'move)
  21452. (line-beginning-position))
  21453. (save-excursion (beginning-of-line)
  21454. (re-search-forward "^[ \t]*$" end 'move)
  21455. (line-beginning-position))
  21456. justify))))
  21457. ;; Fill comments.
  21458. (comment
  21459. (let ((begin (org-element-property :post-affiliated element))
  21460. (end (org-element-property :end element)))
  21461. (when (and (>= (point) begin) (<= (point) end))
  21462. (let ((begin (save-excursion
  21463. (end-of-line)
  21464. (if (re-search-backward "^[ \t]*#[ \t]*$" begin t)
  21465. (progn (forward-line) (point))
  21466. begin)))
  21467. (end (save-excursion
  21468. (end-of-line)
  21469. (if (re-search-forward "^[ \t]*#[ \t]*$" end 'move)
  21470. (1- (line-beginning-position))
  21471. (skip-chars-backward " \r\t\n")
  21472. (line-end-position)))))
  21473. ;; Do not fill comments when at a blank line.
  21474. (when (> end begin)
  21475. (let ((fill-prefix
  21476. (save-excursion
  21477. (beginning-of-line)
  21478. (looking-at "[ \t]*#")
  21479. (let ((comment-prefix (match-string 0)))
  21480. (goto-char (match-end 0))
  21481. (if (looking-at adaptive-fill-regexp)
  21482. (concat comment-prefix (match-string 0))
  21483. (concat comment-prefix " "))))))
  21484. (save-excursion
  21485. (fill-region-as-paragraph begin end justify))))))
  21486. t))
  21487. ;; Ignore every other element.
  21488. (otherwise t))))))
  21489. (defun org-auto-fill-function ()
  21490. "Auto-fill function."
  21491. ;; Check if auto-filling is meaningful.
  21492. (let ((fc (current-fill-column)))
  21493. (when (and fc (> (current-column) fc))
  21494. (let* ((fill-prefix (org-adaptive-fill-function))
  21495. ;; Enforce empty fill prefix, if required. Otherwise, it
  21496. ;; will be computed again.
  21497. (adaptive-fill-mode (not (equal fill-prefix ""))))
  21498. (when fill-prefix (do-auto-fill))))))
  21499. (defun org-comment-line-break-function (&optional soft)
  21500. "Break line at point and indent, continuing comment if within one.
  21501. The inserted newline is marked hard if variable
  21502. `use-hard-newlines' is true, unless optional argument SOFT is
  21503. non-nil."
  21504. (if soft (insert-and-inherit ?\n) (newline 1))
  21505. (save-excursion (forward-char -1) (delete-horizontal-space))
  21506. (delete-horizontal-space)
  21507. (indent-to-left-margin)
  21508. (insert-before-markers-and-inherit fill-prefix))
  21509. ;;; Fixed Width Areas
  21510. (defun org-toggle-fixed-width ()
  21511. "Toggle fixed-width markup.
  21512. Add or remove fixed-width markup on current line, whenever it
  21513. makes sense. Return an error otherwise.
  21514. If a region is active and if it contains only fixed-width areas
  21515. or blank lines, remove all fixed-width markup in it. If the
  21516. region contains anything else, convert all non-fixed-width lines
  21517. to fixed-width ones.
  21518. Blank lines at the end of the region are ignored unless the
  21519. region only contains such lines."
  21520. (interactive)
  21521. (if (not (org-region-active-p))
  21522. ;; No region:
  21523. ;;
  21524. ;; Remove fixed width marker only in a fixed-with element.
  21525. ;;
  21526. ;; Add fixed width maker in paragraphs, in blank lines after
  21527. ;; elements or at the beginning of a headline or an inlinetask,
  21528. ;; and before any one-line elements (e.g., a clock).
  21529. (progn
  21530. (beginning-of-line)
  21531. (let* ((element (org-element-at-point))
  21532. (type (org-element-type element)))
  21533. (cond
  21534. ((and (eq type 'fixed-width)
  21535. (looking-at "[ \t]*\\(:\\(?: \\|$\\)\\)"))
  21536. (replace-match
  21537. "" nil nil nil (if (= (line-end-position) (match-end 0)) 0 1)))
  21538. ((and (memq type '(babel-call clock comment diary-sexp headline
  21539. horizontal-rule keyword paragraph
  21540. planning))
  21541. (<= (org-element-property :post-affiliated element) (point)))
  21542. (skip-chars-forward " \t")
  21543. (insert ": "))
  21544. ((and (org-looking-at-p "[ \t]*$")
  21545. (or (eq type 'inlinetask)
  21546. (save-excursion
  21547. (skip-chars-forward " \r\t\n")
  21548. (<= (org-element-property :end element) (point)))))
  21549. (delete-region (point) (line-end-position))
  21550. (org-indent-line)
  21551. (insert ": "))
  21552. (t (user-error "Cannot insert a fixed-width line here")))))
  21553. ;; Region active.
  21554. (let* ((begin (save-excursion
  21555. (goto-char (region-beginning))
  21556. (line-beginning-position)))
  21557. (end (copy-marker
  21558. (save-excursion
  21559. (goto-char (region-end))
  21560. (unless (eolp) (beginning-of-line))
  21561. (if (save-excursion (re-search-backward "\\S-" begin t))
  21562. (progn (skip-chars-backward " \r\t\n") (point))
  21563. (point)))))
  21564. (all-fixed-width-p
  21565. (catch 'not-all-p
  21566. (save-excursion
  21567. (goto-char begin)
  21568. (skip-chars-forward " \r\t\n")
  21569. (when (eobp) (throw 'not-all-p nil))
  21570. (while (< (point) end)
  21571. (let ((element (org-element-at-point)))
  21572. (if (eq (org-element-type element) 'fixed-width)
  21573. (goto-char (org-element-property :end element))
  21574. (throw 'not-all-p nil))))
  21575. t))))
  21576. (if all-fixed-width-p
  21577. (save-excursion
  21578. (goto-char begin)
  21579. (while (< (point) end)
  21580. (when (looking-at "[ \t]*\\(:\\(?: \\|$\\)\\)")
  21581. (replace-match
  21582. "" nil nil nil
  21583. (if (= (line-end-position) (match-end 0)) 0 1)))
  21584. (forward-line)))
  21585. (let ((min-ind (point-max)))
  21586. ;; Find minimum indentation across all lines.
  21587. (save-excursion
  21588. (goto-char begin)
  21589. (if (not (save-excursion (re-search-forward "\\S-" end t)))
  21590. (setq min-ind 0)
  21591. (catch 'zerop
  21592. (while (< (point) end)
  21593. (unless (org-looking-at-p "[ \t]*$")
  21594. (let ((ind (org-get-indentation)))
  21595. (setq min-ind (min min-ind ind))
  21596. (when (zerop ind) (throw 'zerop t))))
  21597. (forward-line)))))
  21598. ;; Loop over all lines and add fixed-width markup everywhere
  21599. ;; but in fixed-width lines.
  21600. (save-excursion
  21601. (goto-char begin)
  21602. (while (< (point) end)
  21603. (cond
  21604. ((org-at-heading-p)
  21605. (insert ": ")
  21606. (forward-line)
  21607. (while (and (< (point) end) (org-looking-at-p "[ \t]*$"))
  21608. (insert ":")
  21609. (forward-line)))
  21610. ((org-looking-at-p "[ \t]*:\\( \\|$\\)")
  21611. (let* ((element (org-element-at-point))
  21612. (element-end (org-element-property :end element)))
  21613. (if (eq (org-element-type element) 'fixed-width)
  21614. (progn (goto-char element-end)
  21615. (skip-chars-backward " \r\t\n")
  21616. (forward-line))
  21617. (let ((limit (min end element-end)))
  21618. (while (< (point) limit)
  21619. (org-move-to-column min-ind t)
  21620. (insert ": ")
  21621. (forward-line))))))
  21622. (t
  21623. (org-move-to-column min-ind t)
  21624. (insert ": ")
  21625. (forward-line)))))))
  21626. (set-marker end nil))))
  21627. ;;; Comments
  21628. ;; Org comments syntax is quite complex. It requires the entire line
  21629. ;; to be just a comment. Also, even with the right syntax at the
  21630. ;; beginning of line, some some elements (i.e. verse-block or
  21631. ;; example-block) don't accept comments. Usual Emacs comment commands
  21632. ;; cannot cope with those requirements. Therefore, Org replaces them.
  21633. ;; Org still relies on `comment-dwim', but cannot trust
  21634. ;; `comment-only-p'. So, `comment-region-function' and
  21635. ;; `uncomment-region-function' both point
  21636. ;; to`org-comment-or-uncomment-region'. Eventually,
  21637. ;; `org-insert-comment' takes care of insertion of comments at the
  21638. ;; beginning of line.
  21639. ;; `org-setup-comments-handling' install comments related variables
  21640. ;; during `org-mode' initialization.
  21641. (defun org-setup-comments-handling ()
  21642. (interactive)
  21643. (setq-local comment-use-syntax nil)
  21644. (setq-local comment-start "# ")
  21645. (setq-local comment-start-skip "^\\s-*#\\(?: \\|$\\)")
  21646. (setq-local comment-insert-comment-function 'org-insert-comment)
  21647. (setq-local comment-region-function 'org-comment-or-uncomment-region)
  21648. (setq-local uncomment-region-function 'org-comment-or-uncomment-region))
  21649. (defun org-insert-comment ()
  21650. "Insert an empty comment above current line.
  21651. If the line is empty, insert comment at its beginning. When
  21652. point is within a source block, comment according to the related
  21653. major mode."
  21654. (if (let ((element (org-element-at-point)))
  21655. (and (eq (org-element-type element) 'src-block)
  21656. (< (save-excursion
  21657. (goto-char (org-element-property :post-affiliated element))
  21658. (line-end-position))
  21659. (point))
  21660. (> (save-excursion
  21661. (goto-char (org-element-property :end element))
  21662. (skip-chars-backward " \r\t\n")
  21663. (line-beginning-position))
  21664. (point))))
  21665. (org-babel-do-in-edit-buffer (call-interactively 'comment-dwim))
  21666. (beginning-of-line)
  21667. (if (looking-at "\\s-*$") (delete-region (point) (point-at-eol))
  21668. (open-line 1))
  21669. (org-indent-line)
  21670. (insert "# ")))
  21671. (defvar comment-empty-lines) ; From newcomment.el.
  21672. (defun org-comment-or-uncomment-region (beg end &rest _)
  21673. "Comment or uncomment each non-blank line in the region.
  21674. Uncomment each non-blank line between BEG and END if it only
  21675. contains commented lines. Otherwise, comment them. If region is
  21676. strictly within a source block, use appropriate comment syntax."
  21677. (if (let ((element (org-element-at-point)))
  21678. (and (eq (org-element-type element) 'src-block)
  21679. (< (save-excursion
  21680. (goto-char (org-element-property :post-affiliated element))
  21681. (line-end-position))
  21682. beg)
  21683. (>= (save-excursion
  21684. (goto-char (org-element-property :end element))
  21685. (skip-chars-backward " \r\t\n")
  21686. (line-beginning-position))
  21687. end)))
  21688. (org-babel-do-in-edit-buffer (call-interactively 'comment-dwim))
  21689. (save-restriction
  21690. ;; Restrict region
  21691. (narrow-to-region (save-excursion (goto-char beg)
  21692. (skip-chars-forward " \r\t\n" end)
  21693. (line-beginning-position))
  21694. (save-excursion (goto-char end)
  21695. (skip-chars-backward " \r\t\n" beg)
  21696. (line-end-position)))
  21697. (let ((uncommentp
  21698. ;; UNCOMMENTP is non-nil when every non blank line between
  21699. ;; BEG and END is a comment.
  21700. (save-excursion
  21701. (goto-char (point-min))
  21702. (while (and (not (eobp))
  21703. (let ((element (org-element-at-point)))
  21704. (and (eq (org-element-type element) 'comment)
  21705. (goto-char (min (point-max)
  21706. (org-element-property
  21707. :end element)))))))
  21708. (eobp))))
  21709. (if uncommentp
  21710. ;; Only blank lines and comments in region: uncomment it.
  21711. (save-excursion
  21712. (goto-char (point-min))
  21713. (while (not (eobp))
  21714. (when (looking-at "[ \t]*\\(#\\(?: \\|$\\)\\)")
  21715. (replace-match "" nil nil nil 1))
  21716. (forward-line)))
  21717. ;; Comment each line in region.
  21718. (let ((min-indent (point-max)))
  21719. ;; First find the minimum indentation across all lines.
  21720. (save-excursion
  21721. (goto-char (point-min))
  21722. (while (and (not (eobp)) (not (zerop min-indent)))
  21723. (unless (looking-at "[ \t]*$")
  21724. (setq min-indent (min min-indent (current-indentation))))
  21725. (forward-line)))
  21726. ;; Then loop over all lines.
  21727. (save-excursion
  21728. (goto-char (point-min))
  21729. (while (not (eobp))
  21730. (unless (and (not comment-empty-lines) (looking-at "[ \t]*$"))
  21731. ;; Don't get fooled by invisible text (e.g. link path)
  21732. ;; when moving to column MIN-INDENT.
  21733. (let ((buffer-invisibility-spec nil))
  21734. (org-move-to-column min-indent t))
  21735. (insert comment-start))
  21736. (forward-line)))))))))
  21737. (defun org-comment-dwim (_arg)
  21738. "Call `comment-dwim' within a source edit buffer if needed."
  21739. (interactive "*P")
  21740. (if (org-in-src-block-p)
  21741. (org-babel-do-in-edit-buffer (call-interactively 'comment-dwim))
  21742. (call-interactively 'comment-dwim)))
  21743. ;;; Timestamps API
  21744. ;; This section contains tools to operate on timestamp objects, as
  21745. ;; returned by, e.g. `org-element-context'.
  21746. (defun org-timestamp--to-internal-time (timestamp &optional end)
  21747. "Encode TIMESTAMP object into Emacs internal time.
  21748. Use end of date range or time range when END is non-nil."
  21749. (apply #'encode-time
  21750. (cons 0
  21751. (mapcar
  21752. (lambda (prop) (or (org-element-property prop timestamp) 0))
  21753. (if end '(:minute-end :hour-end :day-end :month-end :year-end)
  21754. '(:minute-start :hour-start :day-start :month-start
  21755. :year-start))))))
  21756. (defun org-timestamp-has-time-p (timestamp)
  21757. "Non-nil when TIMESTAMP has a time specified."
  21758. (org-element-property :hour-start timestamp))
  21759. (defun org-timestamp-format (timestamp format &optional end utc)
  21760. "Format a TIMESTAMP object into a string.
  21761. FORMAT is a format specifier to be passed to
  21762. `format-time-string'.
  21763. When optional argument END is non-nil, use end of date-range or
  21764. time-range, if possible.
  21765. When optional argument UTC is non-nil, time will be expressed as
  21766. Universal Time."
  21767. (format-time-string
  21768. format (org-timestamp--to-internal-time timestamp end) utc))
  21769. (defun org-timestamp-split-range (timestamp &optional end)
  21770. "Extract a TIMESTAMP object from a date or time range.
  21771. END, when non-nil, means extract the end of the range.
  21772. Otherwise, extract its start.
  21773. Return a new timestamp object."
  21774. (let ((type (org-element-property :type timestamp)))
  21775. (if (memq type '(active inactive diary)) timestamp
  21776. (let ((split-ts (org-element-copy timestamp)))
  21777. ;; Set new type.
  21778. (org-element-put-property
  21779. split-ts :type (if (eq type 'active-range) 'active 'inactive))
  21780. ;; Copy start properties over end properties if END is
  21781. ;; non-nil. Otherwise, copy end properties over `start' ones.
  21782. (let ((p-alist '((:minute-start . :minute-end)
  21783. (:hour-start . :hour-end)
  21784. (:day-start . :day-end)
  21785. (:month-start . :month-end)
  21786. (:year-start . :year-end))))
  21787. (dolist (p-cell p-alist)
  21788. (org-element-put-property
  21789. split-ts
  21790. (funcall (if end #'car #'cdr) p-cell)
  21791. (org-element-property
  21792. (funcall (if end #'cdr #'car) p-cell) split-ts)))
  21793. ;; Eventually refresh `:raw-value'.
  21794. (org-element-put-property split-ts :raw-value nil)
  21795. (org-element-put-property
  21796. split-ts :raw-value (org-element-interpret-data split-ts)))))))
  21797. (defun org-timestamp-translate (timestamp &optional boundary)
  21798. "Translate TIMESTAMP object to custom format.
  21799. Format string is defined in `org-time-stamp-custom-formats',
  21800. which see.
  21801. When optional argument BOUNDARY is non-nil, it is either the
  21802. symbol `start' or `end'. In this case, only translate the
  21803. starting or ending part of TIMESTAMP if it is a date or time
  21804. range. Otherwise, translate both parts.
  21805. Return timestamp as-is if `org-display-custom-times' is nil or if
  21806. it has a `diary' type."
  21807. (let ((type (org-element-property :type timestamp)))
  21808. (if (or (not org-display-custom-times) (eq type 'diary))
  21809. (org-element-interpret-data timestamp)
  21810. (let ((fmt (funcall (if (org-timestamp-has-time-p timestamp) #'cdr #'car)
  21811. org-time-stamp-custom-formats)))
  21812. (if (and (not boundary) (memq type '(active-range inactive-range)))
  21813. (concat (org-timestamp-format timestamp fmt)
  21814. "--"
  21815. (org-timestamp-format timestamp fmt t))
  21816. (org-timestamp-format timestamp fmt (eq boundary 'end)))))))
  21817. ;;; Other stuff.
  21818. (defvar reftex-docstruct-symbol)
  21819. (defvar reftex-cite-format)
  21820. (defvar org--rds)
  21821. (defun org-reftex-citation ()
  21822. "Use reftex-citation to insert a citation into the buffer.
  21823. This looks for a line like
  21824. #+BIBLIOGRAPHY: foo plain option:-d
  21825. and derives from it that foo.bib is the bibliography file relevant
  21826. for this document. It then installs the necessary environment for RefTeX
  21827. to work in this buffer and calls `reftex-citation' to insert a citation
  21828. into the buffer.
  21829. Export of such citations to both LaTeX and HTML is handled by the contributed
  21830. package ox-bibtex by Taru Karttunen."
  21831. (interactive)
  21832. (let ((reftex-docstruct-symbol 'org--rds)
  21833. (reftex-cite-format "\\cite{%l}")
  21834. org--rds bib)
  21835. (save-excursion
  21836. (save-restriction
  21837. (widen)
  21838. (let ((case-fold-search t)
  21839. (re "^[ \t]*#\\+BIBLIOGRAPHY:[ \t]+\\([^ \t\n]+\\)"))
  21840. (if (not (save-excursion
  21841. (or (re-search-forward re nil t)
  21842. (re-search-backward re nil t))))
  21843. (user-error "No bibliography defined in file")
  21844. (setq bib (concat (match-string 1) ".bib")
  21845. org--rds (list (list 'bib bib)))))))
  21846. (call-interactively 'reftex-citation)))
  21847. ;;;; Functions extending outline functionality
  21848. (defun org-beginning-of-line (&optional arg)
  21849. "Go to the beginning of the current line. If that is invisible, continue
  21850. to a visible line beginning. This makes the function of C-a more intuitive.
  21851. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  21852. first attempt, and only move to after the tags when the cursor is already
  21853. beyond the end of the headline."
  21854. (interactive "P")
  21855. (let ((pos (point))
  21856. (special (if (consp org-special-ctrl-a/e)
  21857. (car org-special-ctrl-a/e)
  21858. org-special-ctrl-a/e))
  21859. deactivate-mark refpos)
  21860. (if (org-bound-and-true-p visual-line-mode)
  21861. (beginning-of-visual-line 1)
  21862. (beginning-of-line 1))
  21863. (if (and arg (fboundp 'move-beginning-of-line))
  21864. (call-interactively 'move-beginning-of-line)
  21865. (unless (bobp)
  21866. (backward-char 1)
  21867. (if (org-truely-invisible-p)
  21868. (while (and (not (bobp)) (org-truely-invisible-p))
  21869. (backward-char 1)
  21870. (beginning-of-line 1))
  21871. (forward-char 1))))
  21872. (when special
  21873. (cond
  21874. ((and (looking-at org-complex-heading-regexp)
  21875. (eq (char-after (match-end 1)) ?\s))
  21876. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  21877. (point-at-eol)))
  21878. (goto-char
  21879. (if (eq special t)
  21880. (cond ((> pos refpos) refpos)
  21881. ((= pos (point)) refpos)
  21882. (t (point)))
  21883. (cond ((> pos (point)) (point))
  21884. ((not (eq last-command this-command)) (point))
  21885. (t refpos)))))
  21886. ((org-at-item-p)
  21887. ;; Being at an item and not looking at an the item means point
  21888. ;; was previously moved to beginning of a visual line, which
  21889. ;; doesn't contain the item. Therefore, do nothing special,
  21890. ;; just stay here.
  21891. (when (looking-at org-list-full-item-re)
  21892. ;; Set special position at first white space character after
  21893. ;; bullet, and check-box, if any.
  21894. (let ((after-bullet
  21895. (let ((box (match-end 3)))
  21896. (if (not box) (match-end 1)
  21897. (let ((after (char-after box)))
  21898. (if (and after (= after ? )) (1+ box) box))))))
  21899. ;; Special case: Move point to special position when
  21900. ;; currently after it or at beginning of line.
  21901. (if (eq special t)
  21902. (when (or (> pos after-bullet) (= (point) pos))
  21903. (goto-char after-bullet))
  21904. ;; Reversed case: Move point to special position when
  21905. ;; point was already at beginning of line and command is
  21906. ;; repeated.
  21907. (when (and (= (point) pos) (eq last-command this-command))
  21908. (goto-char after-bullet))))))))
  21909. (org-no-warnings
  21910. (and (featurep 'xemacs) (setq zmacs-region-stays t))))
  21911. (setq disable-point-adjustment
  21912. (or (not (invisible-p (point)))
  21913. (not (invisible-p (max (point-min) (1- (point))))))))
  21914. (defun org-end-of-line (&optional arg)
  21915. "Go to the end of the line.
  21916. If this is a headline, and `org-special-ctrl-a/e' is set, ignore
  21917. tags on the first attempt, and only move to after the tags when
  21918. the cursor is already beyond the end of the headline."
  21919. (interactive "P")
  21920. (let ((special (if (consp org-special-ctrl-a/e) (cdr org-special-ctrl-a/e)
  21921. org-special-ctrl-a/e))
  21922. (move-fun (cond ((org-bound-and-true-p visual-line-mode)
  21923. 'end-of-visual-line)
  21924. ((fboundp 'move-end-of-line) 'move-end-of-line)
  21925. (t 'end-of-line)))
  21926. deactivate-mark)
  21927. (if (or (not special) arg) (call-interactively move-fun)
  21928. (let* ((element (save-excursion (beginning-of-line)
  21929. (org-element-at-point)))
  21930. (type (org-element-type element)))
  21931. (cond
  21932. ((memq type '(headline inlinetask))
  21933. (let ((pos (point)))
  21934. (beginning-of-line 1)
  21935. (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*\\)?$"))
  21936. (if (eq special t)
  21937. (if (or (< pos (match-beginning 1)) (= pos (match-end 0)))
  21938. (goto-char (match-beginning 1))
  21939. (goto-char (match-end 0)))
  21940. (if (or (< pos (match-end 0))
  21941. (not (eq this-command last-command)))
  21942. (goto-char (match-end 0))
  21943. (goto-char (match-beginning 1))))
  21944. (call-interactively move-fun))))
  21945. ((outline-invisible-p (line-end-position))
  21946. ;; If element is hidden, `move-end-of-line' would put point
  21947. ;; after it. Use `end-of-line' to stay on current line.
  21948. (call-interactively 'end-of-line))
  21949. (t (call-interactively move-fun)))))
  21950. (org-no-warnings (and (featurep 'xemacs) (setq zmacs-region-stays t))))
  21951. (setq disable-point-adjustment
  21952. (or (not (invisible-p (point)))
  21953. (not (invisible-p (max (point-min) (1- (point))))))))
  21954. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  21955. (define-key org-mode-map "\C-e" 'org-end-of-line)
  21956. (defun org-backward-sentence (&optional _arg)
  21957. "Go to beginning of sentence, or beginning of table field.
  21958. This will call `backward-sentence' or `org-table-beginning-of-field',
  21959. depending on context."
  21960. (interactive)
  21961. (let* ((element (org-element-at-point))
  21962. (contents-begin (org-element-property :contents-begin element))
  21963. (table (org-element-lineage element '(table) t)))
  21964. (if (and table
  21965. (> (point) contents-begin)
  21966. (<= (point) (org-element-property :contents-end table)))
  21967. (call-interactively #'org-table-beginning-of-field)
  21968. (save-restriction
  21969. (when (and contents-begin
  21970. (< (point-min) contents-begin)
  21971. (> (point) contents-begin))
  21972. (narrow-to-region contents-begin
  21973. (org-element-property :contents-end element)))
  21974. (call-interactively #'backward-sentence)))))
  21975. (defun org-forward-sentence (&optional _arg)
  21976. "Go to end of sentence, or end of table field.
  21977. This will call `forward-sentence' or `org-table-end-of-field',
  21978. depending on context."
  21979. (interactive)
  21980. (let* ((element (org-element-at-point))
  21981. (contents-end (org-element-property :contents-end element))
  21982. (table (org-element-lineage element '(table) t)))
  21983. (if (and table
  21984. (>= (point) (org-element-property :contents-begin table))
  21985. (< (point) contents-end))
  21986. (call-interactively #'org-table-end-of-field)
  21987. (save-restriction
  21988. (when (and contents-end
  21989. (> (point-max) contents-end)
  21990. ;; Skip blank lines between elements.
  21991. (< (org-element-property :end element)
  21992. (save-excursion (goto-char contents-end)
  21993. (skip-chars-forward " \r\t\n"))))
  21994. (narrow-to-region (org-element-property :contents-begin element)
  21995. contents-end))
  21996. (call-interactively #'forward-sentence)))))
  21997. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  21998. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  21999. (defun org-kill-line (&optional _arg)
  22000. "Kill line, to tags or end of line."
  22001. (interactive)
  22002. (cond
  22003. ((or (not org-special-ctrl-k)
  22004. (bolp)
  22005. (not (org-at-heading-p)))
  22006. (when (and (get-char-property (min (point-max) (point-at-eol)) 'invisible)
  22007. org-ctrl-k-protect-subtree
  22008. (or (eq org-ctrl-k-protect-subtree 'error)
  22009. (not (y-or-n-p "Kill hidden subtree along with headline? "))))
  22010. (user-error "C-k aborted as it would kill a hidden subtree"))
  22011. (call-interactively
  22012. (if (org-bound-and-true-p visual-line-mode) 'kill-visual-line 'kill-line)))
  22013. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$"))
  22014. (kill-region (point) (match-beginning 1))
  22015. (org-set-tags nil t))
  22016. (t (kill-region (point) (point-at-eol)))))
  22017. (define-key org-mode-map "\C-k" 'org-kill-line)
  22018. (defun org-yank (&optional arg)
  22019. "Yank. If the kill is a subtree, treat it specially.
  22020. This command will look at the current kill and check if is a single
  22021. subtree, or a series of subtrees[1]. If it passes the test, and if the
  22022. cursor is at the beginning of a line or after the stars of a currently
  22023. empty headline, then the yank is handled specially. How exactly depends
  22024. on the value of the following variables, both set by default.
  22025. `org-yank-folded-subtrees'
  22026. When set, the subtree(s) will be folded after insertion, but only
  22027. if doing so would now swallow text after the yanked text.
  22028. `org-yank-adjusted-subtrees'
  22029. When set, the subtree will be promoted or demoted in order to
  22030. fit into the local outline tree structure, which means that the
  22031. level will be adjusted so that it becomes the smaller one of the
  22032. two *visible* surrounding headings.
  22033. Any prefix to this command will cause `yank' to be called directly with
  22034. no special treatment. In particular, a simple \\[universal-argument] prefix \
  22035. will just
  22036. plainly yank the text as it is.
  22037. \[1] The test checks if the first non-white line is a heading
  22038. and if there are no other headings with fewer stars."
  22039. (interactive "P")
  22040. (org-yank-generic 'yank arg))
  22041. (defun org-yank-generic (command arg)
  22042. "Perform some yank-like command.
  22043. This function implements the behavior described in the `org-yank'
  22044. documentation. However, it has been generalized to work for any
  22045. interactive command with similar behavior."
  22046. ;; pretend to be command COMMAND
  22047. (setq this-command command)
  22048. (if arg
  22049. (call-interactively command)
  22050. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  22051. (and (org-kill-is-subtree-p)
  22052. (or (bolp)
  22053. (and (looking-at "[ \t]*$")
  22054. (string-match
  22055. "\\`\\*+\\'"
  22056. (buffer-substring (point-at-bol) (point)))))))
  22057. swallowp)
  22058. (cond
  22059. ((and subtreep org-yank-folded-subtrees)
  22060. (let ((beg (point))
  22061. end)
  22062. (if (and subtreep org-yank-adjusted-subtrees)
  22063. (org-paste-subtree nil nil 'for-yank)
  22064. (call-interactively command))
  22065. (setq end (point))
  22066. (goto-char beg)
  22067. (when (and (bolp) subtreep
  22068. (not (setq swallowp
  22069. (org-yank-folding-would-swallow-text beg end))))
  22070. (org-with-limited-levels
  22071. (or (looking-at org-outline-regexp)
  22072. (re-search-forward org-outline-regexp-bol end t))
  22073. (while (and (< (point) end) (looking-at org-outline-regexp))
  22074. (outline-hide-subtree)
  22075. (org-cycle-show-empty-lines 'folded)
  22076. (condition-case nil
  22077. (outline-forward-same-level 1)
  22078. (error (goto-char end))))))
  22079. (when swallowp
  22080. (message
  22081. "Inserted text not folded because that would swallow text"))
  22082. (goto-char end)
  22083. (skip-chars-forward " \t\n\r")
  22084. (beginning-of-line 1)
  22085. (push-mark beg 'nomsg)))
  22086. ((and subtreep org-yank-adjusted-subtrees)
  22087. (let ((beg (point-at-bol)))
  22088. (org-paste-subtree nil nil 'for-yank)
  22089. (push-mark beg 'nomsg)))
  22090. (t
  22091. (call-interactively command))))))
  22092. (defun org-yank-folding-would-swallow-text (beg end)
  22093. "Would hide-subtree at BEG swallow any text after END?"
  22094. (let (level)
  22095. (org-with-limited-levels
  22096. (save-excursion
  22097. (goto-char beg)
  22098. (when (or (looking-at org-outline-regexp)
  22099. (re-search-forward org-outline-regexp-bol end t))
  22100. (setq level (org-outline-level)))
  22101. (goto-char end)
  22102. (skip-chars-forward " \t\r\n\v\f")
  22103. (not (or (eobp)
  22104. (and (bolp) (looking-at-p org-outline-regexp)
  22105. (<= (org-outline-level) level))))))))
  22106. (define-key org-mode-map "\C-y" 'org-yank)
  22107. (defun org-truely-invisible-p ()
  22108. "Check if point is at a character currently not visible.
  22109. This version does not only check the character property, but also
  22110. `visible-mode'."
  22111. ;; Early versions of noutline don't have `outline-invisible-p'.
  22112. (unless (org-bound-and-true-p visible-mode)
  22113. (outline-invisible-p)))
  22114. (defun org-invisible-p2 ()
  22115. "Check if point is at a character currently not visible."
  22116. (save-excursion
  22117. (when (and (eolp) (not (bobp))) (backward-char 1))
  22118. ;; Early versions of noutline don't have `outline-invisible-p'.
  22119. (outline-invisible-p)))
  22120. (defun org-back-to-heading (&optional invisible-ok)
  22121. "Call `outline-back-to-heading', but provide a better error message."
  22122. (condition-case nil
  22123. (outline-back-to-heading invisible-ok)
  22124. (error (error "Before first headline at position %d in buffer %s"
  22125. (point) (current-buffer)))))
  22126. (defun org-before-first-heading-p ()
  22127. "Before first heading?"
  22128. (save-excursion
  22129. (end-of-line)
  22130. (null (re-search-backward org-outline-regexp-bol nil t))))
  22131. (defun org-at-heading-p (&optional ignored)
  22132. (outline-on-heading-p t))
  22133. ;;; Though the function was obsoleted in 7.8.03, the byte-compiler
  22134. ;;; warning was only added in Org 9.0, which should be taken into
  22135. ;;; account when deciding when to remove the alias.
  22136. (define-obsolete-function-alias 'org-on-heading-p 'org-at-heading-p "Org 7.8.03")
  22137. (defun org-in-commented-heading-p (&optional no-inheritance)
  22138. "Non-nil if point is under a commented heading.
  22139. This function also checks ancestors of the current headline,
  22140. unless optional argument NO-INHERITANCE is non-nil."
  22141. (cond
  22142. ((org-before-first-heading-p) nil)
  22143. ((let ((headline (nth 4 (org-heading-components))))
  22144. (and headline
  22145. (let ((case-fold-search nil))
  22146. (org-string-match-p (concat "^" org-comment-string "\\(?: \\|$\\)")
  22147. headline)))))
  22148. (no-inheritance nil)
  22149. (t
  22150. (save-excursion (and (org-up-heading-safe) (org-in-commented-heading-p))))))
  22151. (defun org-at-comment-p nil
  22152. "Is cursor in a commented line?"
  22153. (save-excursion
  22154. (save-match-data
  22155. (beginning-of-line)
  22156. (looking-at "^[ \t]*# "))))
  22157. (defun org-at-drawer-p nil
  22158. "Is cursor at a drawer keyword?"
  22159. (save-excursion
  22160. (move-beginning-of-line 1)
  22161. (looking-at org-drawer-regexp)))
  22162. (defun org-at-block-p nil
  22163. "Is cursor at a block keyword?"
  22164. (save-excursion
  22165. (move-beginning-of-line 1)
  22166. (looking-at org-block-regexp)))
  22167. (defun org-point-at-end-of-empty-headline ()
  22168. "If point is at the end of an empty headline, return t, else nil.
  22169. If the heading only contains a TODO keyword, it is still still considered
  22170. empty."
  22171. (and (looking-at "[ \t]*$")
  22172. (when org-todo-line-regexp
  22173. (save-excursion
  22174. (beginning-of-line 1)
  22175. (let ((case-fold-search nil))
  22176. (looking-at org-todo-line-regexp)
  22177. (string= (match-string 3) ""))))))
  22178. (defun org-at-heading-or-item-p ()
  22179. (or (org-at-heading-p) (org-at-item-p)))
  22180. (defun org-at-target-p ()
  22181. (or (org-in-regexp org-radio-target-regexp)
  22182. (org-in-regexp org-target-regexp)))
  22183. ;; Compatibility alias with Org versions < 7.8.03
  22184. (defalias 'org-on-target-p 'org-at-target-p)
  22185. (defun org-up-heading-all (arg)
  22186. "Move to the heading line of which the present line is a subheading.
  22187. This function considers both visible and invisible heading lines.
  22188. With argument, move up ARG levels."
  22189. (if (fboundp 'outline-up-heading-all)
  22190. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  22191. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  22192. (defun org-up-heading-safe ()
  22193. "Move to the heading line of which the present line is a subheading.
  22194. This version will not throw an error. It will return the level of the
  22195. headline found, or nil if no higher level is found.
  22196. Also, this function will be a lot faster than `outline-up-heading',
  22197. because it relies on stars being the outline starters. This can really
  22198. make a significant difference in outlines with very many siblings."
  22199. (when (ignore-errors (org-back-to-heading t))
  22200. (let ((level-up (1- (funcall outline-level))))
  22201. (and (> level-up 0)
  22202. (re-search-backward (format "^\\*\\{1,%d\\} " level-up) nil t)
  22203. (funcall outline-level)))))
  22204. (defun org-first-sibling-p ()
  22205. "Is this heading the first child of its parents?"
  22206. (interactive)
  22207. (let ((re org-outline-regexp-bol)
  22208. level l)
  22209. (unless (org-at-heading-p t)
  22210. (user-error "Not at a heading"))
  22211. (setq level (funcall outline-level))
  22212. (save-excursion
  22213. (if (not (re-search-backward re nil t))
  22214. t
  22215. (setq l (funcall outline-level))
  22216. (< l level)))))
  22217. (defun org-goto-sibling (&optional previous)
  22218. "Goto the next sibling, even if it is invisible.
  22219. When PREVIOUS is set, go to the previous sibling instead. Returns t
  22220. when a sibling was found. When none is found, return nil and don't
  22221. move point."
  22222. (let ((fun (if previous 're-search-backward 're-search-forward))
  22223. (pos (point))
  22224. (re org-outline-regexp-bol)
  22225. level l)
  22226. (when (ignore-errors (org-back-to-heading t))
  22227. (setq level (funcall outline-level))
  22228. (catch 'exit
  22229. (or previous (forward-char 1))
  22230. (while (funcall fun re nil t)
  22231. (setq l (funcall outline-level))
  22232. (when (< l level) (goto-char pos) (throw 'exit nil))
  22233. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  22234. (goto-char pos)
  22235. nil))))
  22236. (defun org-show-siblings ()
  22237. "Show all siblings of the current headline."
  22238. (save-excursion
  22239. (while (org-goto-sibling) (org-flag-heading nil)))
  22240. (save-excursion
  22241. (while (org-goto-sibling 'previous)
  22242. (org-flag-heading nil))))
  22243. (defun org-goto-first-child ()
  22244. "Goto the first child, even if it is invisible.
  22245. Return t when a child was found. Otherwise don't move point and
  22246. return nil."
  22247. (let (level (pos (point)) (re org-outline-regexp-bol))
  22248. (when (ignore-errors (org-back-to-heading t))
  22249. (setq level (outline-level))
  22250. (forward-char 1)
  22251. (if (and (re-search-forward re nil t) (> (outline-level) level))
  22252. (progn (goto-char (match-beginning 0)) t)
  22253. (goto-char pos) nil))))
  22254. (defun org-show-hidden-entry ()
  22255. "Show an entry where even the heading is hidden."
  22256. (save-excursion
  22257. (org-show-entry)))
  22258. (defun org-flag-heading (flag &optional entry)
  22259. "Flag the current heading. FLAG non-nil means make invisible.
  22260. When ENTRY is non-nil, show the entire entry."
  22261. (save-excursion
  22262. (org-back-to-heading t)
  22263. ;; Check if we should show the entire entry
  22264. (if entry
  22265. (progn
  22266. (org-show-entry)
  22267. (save-excursion
  22268. (and (outline-next-heading)
  22269. (org-flag-heading nil))))
  22270. (outline-flag-region (max (point-min) (1- (point)))
  22271. (save-excursion (outline-end-of-heading) (point))
  22272. flag))))
  22273. (defun org-get-next-sibling ()
  22274. "Move to next heading of the same level, and return point.
  22275. If there is no such heading, return nil.
  22276. This is like outline-next-sibling, but invisible headings are ok."
  22277. (let ((level (funcall outline-level)))
  22278. (outline-next-heading)
  22279. (while (and (not (eobp)) (> (funcall outline-level) level))
  22280. (outline-next-heading))
  22281. (unless (or (eobp) (< (funcall outline-level) level))
  22282. (point))))
  22283. (defun org-get-last-sibling ()
  22284. "Move to previous heading of the same level, and return point.
  22285. If there is no such heading, return nil."
  22286. (let ((opoint (point))
  22287. (level (funcall outline-level)))
  22288. (outline-previous-heading)
  22289. (when (and (/= (point) opoint) (outline-on-heading-p t))
  22290. (while (and (> (funcall outline-level) level)
  22291. (not (bobp)))
  22292. (outline-previous-heading))
  22293. (unless (< (funcall outline-level) level)
  22294. (point)))))
  22295. (defun org-end-of-subtree (&optional invisible-ok to-heading)
  22296. "Goto to the end of a subtree."
  22297. ;; This contains an exact copy of the original function, but it uses
  22298. ;; `org-back-to-heading', to make it work also in invisible
  22299. ;; trees. And is uses an invisible-ok argument.
  22300. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  22301. ;; Furthermore, when used inside Org, finding the end of a large subtree
  22302. ;; with many children and grandchildren etc, this can be much faster
  22303. ;; than the outline version.
  22304. (org-back-to-heading invisible-ok)
  22305. (let ((first t)
  22306. (level (funcall outline-level)))
  22307. (if (and (derived-mode-p 'org-mode) (< level 1000))
  22308. ;; A true heading (not a plain list item), in Org-mode
  22309. ;; This means we can easily find the end by looking
  22310. ;; only for the right number of stars. Using a regexp to do
  22311. ;; this is so much faster than using a Lisp loop.
  22312. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  22313. (forward-char 1)
  22314. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  22315. ;; something else, do it the slow way
  22316. (while (and (not (eobp))
  22317. (or first (> (funcall outline-level) level)))
  22318. (setq first nil)
  22319. (outline-next-heading)))
  22320. (unless to-heading
  22321. (when (memq (preceding-char) '(?\n ?\^M))
  22322. ;; Go to end of line before heading
  22323. (forward-char -1)
  22324. (when (memq (preceding-char) '(?\n ?\^M))
  22325. ;; leave blank line before heading
  22326. (forward-char -1)))))
  22327. (point))
  22328. (defun org-end-of-meta-data (&optional full)
  22329. "Skip planning line and properties drawer in current entry.
  22330. When optional argument FULL is non-nil, also skip empty lines,
  22331. clocking lines and regular drawers at the beginning of the
  22332. entry."
  22333. (org-back-to-heading t)
  22334. (forward-line)
  22335. (when (org-looking-at-p org-planning-line-re) (forward-line))
  22336. (when (looking-at org-property-drawer-re)
  22337. (goto-char (match-end 0))
  22338. (forward-line))
  22339. (when (and full (not (org-at-heading-p)))
  22340. (catch 'exit
  22341. (let ((end (save-excursion (outline-next-heading) (point)))
  22342. (re (concat "[ \t]*$" "\\|" org-clock-line-re)))
  22343. (while (not (eobp))
  22344. (cond ((org-looking-at-p org-drawer-regexp)
  22345. (if (re-search-forward "^[ \t]*:END:[ \t]*$" end t)
  22346. (forward-line)
  22347. (throw 'exit t)))
  22348. ((org-looking-at-p re) (forward-line))
  22349. (t (throw 'exit t))))))))
  22350. (defun org-forward-heading-same-level (arg &optional invisible-ok)
  22351. "Move forward to the ARG'th subheading at same level as this one.
  22352. Stop at the first and last subheadings of a superior heading.
  22353. Normally this only looks at visible headings, but when INVISIBLE-OK is
  22354. non-nil it will also look at invisible ones."
  22355. (interactive "p")
  22356. (if (not (ignore-errors (org-back-to-heading invisible-ok)))
  22357. (if (and arg (< arg 0))
  22358. (goto-char (point-min))
  22359. (outline-next-heading))
  22360. (org-at-heading-p)
  22361. (let ((level (- (match-end 0) (match-beginning 0) 1))
  22362. (f (if (and arg (< arg 0))
  22363. 're-search-backward
  22364. 're-search-forward))
  22365. (count (if arg (abs arg) 1))
  22366. (result (point)))
  22367. (while (and (prog1 (> count 0)
  22368. (forward-char (if (and arg (< arg 0)) -1 1)))
  22369. (funcall f org-outline-regexp-bol nil 'move))
  22370. (let ((l (- (match-end 0) (match-beginning 0) 1)))
  22371. (cond ((< l level) (setq count 0))
  22372. ((and (= l level)
  22373. (or invisible-ok
  22374. (progn
  22375. (goto-char (line-beginning-position))
  22376. (not (outline-invisible-p)))))
  22377. (setq count (1- count))
  22378. (when (eq l level)
  22379. (setq result (point)))))))
  22380. (goto-char result))
  22381. (beginning-of-line 1)))
  22382. (defun org-backward-heading-same-level (arg &optional invisible-ok)
  22383. "Move backward to the ARG'th subheading at same level as this one.
  22384. Stop at the first and last subheadings of a superior heading."
  22385. (interactive "p")
  22386. (org-forward-heading-same-level (if arg (- arg) -1) invisible-ok))
  22387. (defun org-next-visible-heading (arg)
  22388. "Move to the next visible heading.
  22389. This function wraps `outline-next-visible-heading' with
  22390. `org-with-limited-levels' in order to skip over inline tasks and
  22391. respect customization of `org-odd-levels-only'."
  22392. (interactive "p")
  22393. (org-with-limited-levels
  22394. (outline-next-visible-heading arg)))
  22395. (defun org-previous-visible-heading (arg)
  22396. "Move to the next visible heading.
  22397. This function wraps `outline-previous-visible-heading' with
  22398. `org-with-limited-levels' in order to skip over inline tasks and
  22399. respect customization of `org-odd-levels-only'."
  22400. (interactive "p")
  22401. (org-with-limited-levels
  22402. (outline-previous-visible-heading arg)))
  22403. (defun org-next-block (arg &optional backward block-regexp)
  22404. "Jump to the next block.
  22405. With a prefix argument ARG, jump forward ARG many blocks.
  22406. When BACKWARD is non-nil, jump to the previous block.
  22407. When BLOCK-REGEXP is non-nil, use this regexp to find blocks.
  22408. Match data is set according to this regexp when the function
  22409. returns.
  22410. Return point at beginning of the opening line of found block.
  22411. Throw an error if no block is found."
  22412. (interactive "p")
  22413. (let ((re (or block-regexp "^[ \t]*#\\+BEGIN"))
  22414. (case-fold-search t)
  22415. (search-fn (if backward #'re-search-backward #'re-search-forward))
  22416. (count (or arg 1))
  22417. (origin (point))
  22418. last-element)
  22419. (if backward (beginning-of-line) (end-of-line))
  22420. (while (and (> count 0) (funcall search-fn re nil t))
  22421. (let ((element (save-excursion
  22422. (goto-char (match-beginning 0))
  22423. (save-match-data (org-element-at-point)))))
  22424. (when (and (memq (org-element-type element)
  22425. '(center-block comment-block dynamic-block
  22426. example-block export-block quote-block
  22427. special-block src-block verse-block))
  22428. (<= (match-beginning 0)
  22429. (org-element-property :post-affiliated element)))
  22430. (setq last-element element)
  22431. (cl-decf count))))
  22432. (if (= count 0)
  22433. (prog1 (goto-char (org-element-property :post-affiliated last-element))
  22434. (save-match-data (org-show-context)))
  22435. (goto-char origin)
  22436. (user-error "No %s code blocks" (if backward "previous" "further")))))
  22437. (defun org-previous-block (arg &optional block-regexp)
  22438. "Jump to the previous block.
  22439. With a prefix argument ARG, jump backward ARG many source blocks.
  22440. When BLOCK-REGEXP is non-nil, use this regexp to find blocks."
  22441. (interactive "p")
  22442. (org-next-block arg t block-regexp))
  22443. (defun org-forward-paragraph ()
  22444. "Move forward to beginning of next paragraph or equivalent.
  22445. The function moves point to the beginning of the next visible
  22446. structural element, which can be a paragraph, a table, a list
  22447. item, etc. It also provides some special moves for convenience:
  22448. - On an affiliated keyword, jump to the beginning of the
  22449. relative element.
  22450. - On an item or a footnote definition, move to the second
  22451. element inside, if any.
  22452. - On a table or a property drawer, jump after it.
  22453. - On a verse or source block, stop after blank lines."
  22454. (interactive)
  22455. (when (eobp) (user-error "Cannot move further down"))
  22456. (let* ((deactivate-mark nil)
  22457. (element (org-element-at-point))
  22458. (type (org-element-type element))
  22459. (post-affiliated (org-element-property :post-affiliated element))
  22460. (contents-begin (org-element-property :contents-begin element))
  22461. (contents-end (org-element-property :contents-end element))
  22462. (end (let ((end (org-element-property :end element)) (parent element))
  22463. (while (and (setq parent (org-element-property :parent parent))
  22464. (= (org-element-property :contents-end parent) end))
  22465. (setq end (org-element-property :end parent)))
  22466. end)))
  22467. (cond ((not element)
  22468. (skip-chars-forward " \r\t\n")
  22469. (or (eobp) (beginning-of-line)))
  22470. ;; On affiliated keywords, move to element's beginning.
  22471. ((< (point) post-affiliated)
  22472. (goto-char post-affiliated))
  22473. ;; At a table row, move to the end of the table. Similarly,
  22474. ;; at a node property, move to the end of the property
  22475. ;; drawer.
  22476. ((memq type '(node-property table-row))
  22477. (goto-char (org-element-property
  22478. :end (org-element-property :parent element))))
  22479. ((memq type '(property-drawer table)) (goto-char end))
  22480. ;; Consider blank lines as separators in verse and source
  22481. ;; blocks to ease editing.
  22482. ((memq type '(src-block verse-block))
  22483. (when (eq type 'src-block)
  22484. (setq contents-end
  22485. (save-excursion (goto-char end)
  22486. (skip-chars-backward " \r\t\n")
  22487. (line-beginning-position))))
  22488. (beginning-of-line)
  22489. (when (looking-at "[ \t]*$") (skip-chars-forward " \r\t\n"))
  22490. (if (not (re-search-forward "^[ \t]*$" contents-end t))
  22491. (goto-char end)
  22492. (skip-chars-forward " \r\t\n")
  22493. (if (= (point) contents-end) (goto-char end)
  22494. (beginning-of-line))))
  22495. ;; With no contents, just skip element.
  22496. ((not contents-begin) (goto-char end))
  22497. ;; If contents are invisible, skip the element altogether.
  22498. ((outline-invisible-p (line-end-position))
  22499. (cl-case type
  22500. (headline
  22501. (org-with-limited-levels (outline-next-visible-heading 1)))
  22502. ;; At a plain list, make sure we move to the next item
  22503. ;; instead of skipping the whole list.
  22504. (plain-list (forward-char)
  22505. (org-forward-paragraph))
  22506. (otherwise (goto-char end))))
  22507. ((>= (point) contents-end) (goto-char end))
  22508. ((>= (point) contents-begin)
  22509. ;; This can only happen on paragraphs and plain lists.
  22510. (cl-case type
  22511. (paragraph (goto-char end))
  22512. ;; At a plain list, try to move to second element in
  22513. ;; first item, if possible.
  22514. (plain-list (end-of-line)
  22515. (org-forward-paragraph))))
  22516. ;; When contents start on the middle of a line (e.g. in
  22517. ;; items and footnote definitions), try to reach first
  22518. ;; element starting after current line.
  22519. ((> (line-end-position) contents-begin)
  22520. (end-of-line)
  22521. (org-forward-paragraph))
  22522. (t (goto-char contents-begin)))))
  22523. (defun org-backward-paragraph ()
  22524. "Move backward to start of previous paragraph or equivalent.
  22525. The function moves point to the beginning of the current
  22526. structural element, which can be a paragraph, a table, a list
  22527. item, etc., or to the beginning of the previous visible one if
  22528. point is already there. It also provides some special moves for
  22529. convenience:
  22530. - On an affiliated keyword, jump to the first one.
  22531. - On a table or a property drawer, move to its beginning.
  22532. - On a verse or source block, stop before blank lines."
  22533. (interactive)
  22534. (when (bobp) (user-error "Cannot move further up"))
  22535. (let* ((deactivate-mark nil)
  22536. (element (org-element-at-point))
  22537. (type (org-element-type element))
  22538. (contents-begin (org-element-property :contents-begin element))
  22539. (contents-end (org-element-property :contents-end element))
  22540. (post-affiliated (org-element-property :post-affiliated element))
  22541. (begin (org-element-property :begin element)))
  22542. (cond
  22543. ((not element) (goto-char (point-min)))
  22544. ((= (point) begin)
  22545. (backward-char)
  22546. (org-backward-paragraph))
  22547. ((<= (point) post-affiliated) (goto-char begin))
  22548. ((memq type '(node-property table-row))
  22549. (goto-char (org-element-property
  22550. :post-affiliated (org-element-property :parent element))))
  22551. ((memq type '(property-drawer table)) (goto-char begin))
  22552. ((memq type '(src-block verse-block))
  22553. (when (eq type 'src-block)
  22554. (setq contents-begin
  22555. (save-excursion (goto-char begin) (forward-line) (point))))
  22556. (if (= (point) contents-begin) (goto-char post-affiliated)
  22557. ;; Inside a verse block, see blank lines as paragraph
  22558. ;; separators.
  22559. (let ((origin (point)))
  22560. (skip-chars-backward " \r\t\n" contents-begin)
  22561. (when (re-search-backward "^[ \t]*$" contents-begin 'move)
  22562. (skip-chars-forward " \r\t\n" origin)
  22563. (if (= (point) origin) (goto-char contents-begin)
  22564. (beginning-of-line))))))
  22565. ((not contents-begin) (goto-char (or post-affiliated begin)))
  22566. ((eq type 'paragraph)
  22567. (goto-char contents-begin)
  22568. ;; When at first paragraph in an item or a footnote definition,
  22569. ;; move directly to beginning of line.
  22570. (let ((parent-contents
  22571. (org-element-property
  22572. :contents-begin (org-element-property :parent element))))
  22573. (when (and parent-contents (= parent-contents contents-begin))
  22574. (beginning-of-line))))
  22575. ;; At the end of a greater element, move to the beginning of the
  22576. ;; last element within.
  22577. ((>= (point) contents-end)
  22578. (goto-char (1- contents-end))
  22579. (org-backward-paragraph))
  22580. (t (goto-char (or post-affiliated begin))))
  22581. ;; Ensure we never leave point invisible.
  22582. (when (outline-invisible-p (point)) (beginning-of-visual-line))))
  22583. (defun org-forward-element ()
  22584. "Move forward by one element.
  22585. Move to the next element at the same level, when possible."
  22586. (interactive)
  22587. (cond ((eobp) (user-error "Cannot move further down"))
  22588. ((org-with-limited-levels (org-at-heading-p))
  22589. (let ((origin (point)))
  22590. (goto-char (org-end-of-subtree nil t))
  22591. (unless (org-with-limited-levels (org-at-heading-p))
  22592. (goto-char origin)
  22593. (user-error "Cannot move further down"))))
  22594. (t
  22595. (let* ((elem (org-element-at-point))
  22596. (end (org-element-property :end elem))
  22597. (parent (org-element-property :parent elem)))
  22598. (cond ((and parent (= (org-element-property :contents-end parent) end))
  22599. (goto-char (org-element-property :end parent)))
  22600. ((integer-or-marker-p end) (goto-char end))
  22601. (t (message "No element at point")))))))
  22602. (defun org-backward-element ()
  22603. "Move backward by one element.
  22604. Move to the previous element at the same level, when possible."
  22605. (interactive)
  22606. (cond ((bobp) (user-error "Cannot move further up"))
  22607. ((org-with-limited-levels (org-at-heading-p))
  22608. ;; At a headline, move to the previous one, if any, or stay
  22609. ;; here.
  22610. (let ((origin (point)))
  22611. (org-with-limited-levels (org-backward-heading-same-level 1))
  22612. ;; When current headline has no sibling above, move to its
  22613. ;; parent.
  22614. (when (= (point) origin)
  22615. (or (org-with-limited-levels (org-up-heading-safe))
  22616. (progn (goto-char origin)
  22617. (user-error "Cannot move further up"))))))
  22618. (t
  22619. (let* ((elem (org-element-at-point))
  22620. (beg (org-element-property :begin elem)))
  22621. (cond
  22622. ;; Move to beginning of current element if point isn't
  22623. ;; there already.
  22624. ((null beg) (message "No element at point"))
  22625. ((/= (point) beg) (goto-char beg))
  22626. (t (goto-char beg)
  22627. (skip-chars-backward " \r\t\n")
  22628. (unless (bobp)
  22629. (let ((prev (org-element-at-point)))
  22630. (goto-char (org-element-property :begin prev))
  22631. (while (and (setq prev (org-element-property :parent prev))
  22632. (<= (org-element-property :end prev) beg))
  22633. (goto-char (org-element-property :begin prev)))))))))))
  22634. (defun org-up-element ()
  22635. "Move to upper element."
  22636. (interactive)
  22637. (if (org-with-limited-levels (org-at-heading-p))
  22638. (unless (org-up-heading-safe) (user-error "No surrounding element"))
  22639. (let* ((elem (org-element-at-point))
  22640. (parent (org-element-property :parent elem)))
  22641. (if parent (goto-char (org-element-property :begin parent))
  22642. (if (org-with-limited-levels (org-before-first-heading-p))
  22643. (user-error "No surrounding element")
  22644. (org-with-limited-levels (org-back-to-heading)))))))
  22645. (defvar org-element-greater-elements)
  22646. (defun org-down-element ()
  22647. "Move to inner element."
  22648. (interactive)
  22649. (let ((element (org-element-at-point)))
  22650. (cond
  22651. ((memq (org-element-type element) '(plain-list table))
  22652. (goto-char (org-element-property :contents-begin element))
  22653. (forward-char))
  22654. ((memq (org-element-type element) org-element-greater-elements)
  22655. ;; If contents are hidden, first disclose them.
  22656. (when (outline-invisible-p (line-end-position)) (org-cycle))
  22657. (goto-char (or (org-element-property :contents-begin element)
  22658. (user-error "No content for this element"))))
  22659. (t (user-error "No inner element")))))
  22660. (defun org-drag-element-backward ()
  22661. "Move backward element at point."
  22662. (interactive)
  22663. (if (org-with-limited-levels (org-at-heading-p)) (org-move-subtree-up)
  22664. (let* ((elem (org-element-at-point))
  22665. (prev-elem
  22666. (save-excursion
  22667. (goto-char (org-element-property :begin elem))
  22668. (skip-chars-backward " \r\t\n")
  22669. (unless (bobp)
  22670. (let* ((beg (org-element-property :begin elem))
  22671. (prev (org-element-at-point))
  22672. (up prev))
  22673. (while (and (setq up (org-element-property :parent up))
  22674. (<= (org-element-property :end up) beg))
  22675. (setq prev up))
  22676. prev)))))
  22677. ;; Error out if no previous element or previous element is
  22678. ;; a parent of the current one.
  22679. (if (or (not prev-elem) (org-element-nested-p elem prev-elem))
  22680. (user-error "Cannot drag element backward")
  22681. (let ((pos (point)))
  22682. (org-element-swap-A-B prev-elem elem)
  22683. (goto-char (+ (org-element-property :begin prev-elem)
  22684. (- pos (org-element-property :begin elem)))))))))
  22685. (defun org-drag-element-forward ()
  22686. "Move forward element at point."
  22687. (interactive)
  22688. (let* ((pos (point))
  22689. (elem (org-element-at-point)))
  22690. (when (= (point-max) (org-element-property :end elem))
  22691. (user-error "Cannot drag element forward"))
  22692. (goto-char (org-element-property :end elem))
  22693. (let ((next-elem (org-element-at-point)))
  22694. (when (or (org-element-nested-p elem next-elem)
  22695. (and (eq (org-element-type next-elem) 'headline)
  22696. (not (eq (org-element-type elem) 'headline))))
  22697. (goto-char pos)
  22698. (user-error "Cannot drag element forward"))
  22699. ;; Compute new position of point: it's shifted by NEXT-ELEM
  22700. ;; body's length (without final blanks) and by the length of
  22701. ;; blanks between ELEM and NEXT-ELEM.
  22702. (let ((size-next (- (save-excursion
  22703. (goto-char (org-element-property :end next-elem))
  22704. (skip-chars-backward " \r\t\n")
  22705. (forward-line)
  22706. ;; Small correction if buffer doesn't end
  22707. ;; with a newline character.
  22708. (if (and (eolp) (not (bolp))) (1+ (point)) (point)))
  22709. (org-element-property :begin next-elem)))
  22710. (size-blank (- (org-element-property :end elem)
  22711. (save-excursion
  22712. (goto-char (org-element-property :end elem))
  22713. (skip-chars-backward " \r\t\n")
  22714. (forward-line)
  22715. (point)))))
  22716. (org-element-swap-A-B elem next-elem)
  22717. (goto-char (+ pos size-next size-blank))))))
  22718. (defun org-drag-line-forward (arg)
  22719. "Drag the line at point ARG lines forward."
  22720. (interactive "p")
  22721. (dotimes (_ (abs arg))
  22722. (let ((c (current-column)))
  22723. (if (< 0 arg)
  22724. (progn
  22725. (beginning-of-line 2)
  22726. (transpose-lines 1)
  22727. (beginning-of-line 0))
  22728. (transpose-lines 1)
  22729. (beginning-of-line -1))
  22730. (org-move-to-column c))))
  22731. (defun org-drag-line-backward (arg)
  22732. "Drag the line at point ARG lines backward."
  22733. (interactive "p")
  22734. (org-drag-line-forward (- arg)))
  22735. (defun org-mark-element ()
  22736. "Put point at beginning of this element, mark at end.
  22737. Interactively, if this command is repeated or (in Transient Mark
  22738. mode) if the mark is active, it marks the next element after the
  22739. ones already marked."
  22740. (interactive)
  22741. (let (deactivate-mark)
  22742. (if (and (org-called-interactively-p 'any)
  22743. (or (and (eq last-command this-command) (mark t))
  22744. (and transient-mark-mode mark-active)))
  22745. (set-mark
  22746. (save-excursion
  22747. (goto-char (mark))
  22748. (goto-char (org-element-property :end (org-element-at-point)))))
  22749. (let ((element (org-element-at-point)))
  22750. (end-of-line)
  22751. (push-mark (org-element-property :end element) t t)
  22752. (goto-char (org-element-property :begin element))))))
  22753. (defun org-narrow-to-element ()
  22754. "Narrow buffer to current element."
  22755. (interactive)
  22756. (let ((elem (org-element-at-point)))
  22757. (cond
  22758. ((eq (car elem) 'headline)
  22759. (narrow-to-region
  22760. (org-element-property :begin elem)
  22761. (org-element-property :end elem)))
  22762. ((memq (car elem) org-element-greater-elements)
  22763. (narrow-to-region
  22764. (org-element-property :contents-begin elem)
  22765. (org-element-property :contents-end elem)))
  22766. (t
  22767. (narrow-to-region
  22768. (org-element-property :begin elem)
  22769. (org-element-property :end elem))))))
  22770. (defun org-transpose-element ()
  22771. "Transpose current and previous elements, keeping blank lines between.
  22772. Point is moved after both elements."
  22773. (interactive)
  22774. (org-skip-whitespace)
  22775. (let ((end (org-element-property :end (org-element-at-point))))
  22776. (org-drag-element-backward)
  22777. (goto-char end)))
  22778. (defun org-unindent-buffer ()
  22779. "Un-indent the visible part of the buffer.
  22780. Relative indentation (between items, inside blocks, etc.) isn't
  22781. modified."
  22782. (interactive)
  22783. (unless (eq major-mode 'org-mode)
  22784. (user-error "Cannot un-indent a buffer not in Org mode"))
  22785. (letrec ((parse-tree (org-element-parse-buffer 'greater-element))
  22786. (unindent-tree
  22787. (lambda (contents)
  22788. (dolist (element (reverse contents))
  22789. (if (memq (org-element-type element) '(headline section))
  22790. (funcall unindent-tree (org-element-contents element))
  22791. (save-excursion
  22792. (save-restriction
  22793. (narrow-to-region
  22794. (org-element-property :begin element)
  22795. (org-element-property :end element))
  22796. (org-do-remove-indentation))))))))
  22797. (funcall unindent-tree (org-element-contents parse-tree))))
  22798. (defun org-show-children (&optional level)
  22799. "Show all direct subheadings of this heading.
  22800. Prefix arg LEVEL is how many levels below the current level
  22801. should be shown. Default is enough to cause the following
  22802. heading to appear."
  22803. (interactive "p")
  22804. ;; If `orgstruct-mode' is active, use the slower version.
  22805. (if orgstruct-mode (call-interactively #'outline-show-children)
  22806. (save-excursion
  22807. (org-back-to-heading t)
  22808. (let* ((current-level (funcall outline-level))
  22809. (max-level (org-get-valid-level
  22810. current-level
  22811. (if level (prefix-numeric-value level) 1)))
  22812. (end (save-excursion (org-end-of-subtree t t)))
  22813. (regexp-fmt "^\\*\\{%d,%s\\}\\(?: \\|$\\)")
  22814. (past-first-child nil)
  22815. ;; Make sure to skip inlinetasks.
  22816. (re (format regexp-fmt
  22817. current-level
  22818. (cond
  22819. ((not (featurep 'org-inlinetask)) "")
  22820. (org-odd-levels-only (- (* 2 org-inlinetask-min-level)
  22821. 3))
  22822. (t (1- org-inlinetask-min-level))))))
  22823. ;; Display parent heading.
  22824. (outline-flag-region (line-end-position 0) (line-end-position) nil)
  22825. (forward-line)
  22826. ;; Display children. First child may be deeper than expected
  22827. ;; MAX-LEVEL. Since we want to display it anyway, adjust
  22828. ;; MAX-LEVEL accordingly.
  22829. (while (re-search-forward re end t)
  22830. (unless past-first-child
  22831. (setq re (format regexp-fmt
  22832. current-level
  22833. (max (funcall outline-level) max-level)))
  22834. (setq past-first-child t))
  22835. (outline-flag-region
  22836. (line-end-position 0) (line-end-position) nil))))))
  22837. (defun org-show-subtree ()
  22838. "Show everything after this heading at deeper levels."
  22839. (interactive)
  22840. (outline-flag-region
  22841. (point)
  22842. (save-excursion
  22843. (org-end-of-subtree t t))
  22844. nil))
  22845. (defun org-show-entry ()
  22846. "Show the body directly following this heading.
  22847. Show the heading too, if it is currently invisible."
  22848. (interactive)
  22849. (save-excursion
  22850. (ignore-errors
  22851. (org-back-to-heading t)
  22852. (outline-flag-region
  22853. (max (point-min) (1- (point)))
  22854. (save-excursion
  22855. (if (re-search-forward
  22856. (concat "[\r\n]\\(" org-outline-regexp "\\)") nil t)
  22857. (match-beginning 1)
  22858. (point-max)))
  22859. nil)
  22860. (org-cycle-hide-drawers 'children))))
  22861. (defun org-make-options-regexp (kwds &optional extra)
  22862. "Make a regular expression for keyword lines.
  22863. KWDS is a list of keywords, as strings. Optional argument EXTRA,
  22864. when non-nil, is a regexp matching keywords names."
  22865. (concat "^[ \t]*#\\+\\("
  22866. (regexp-opt kwds)
  22867. (and extra (concat (and kwds "\\|") extra))
  22868. "\\):[ \t]*\\(.*\\)"))
  22869. ;; Make isearch reveal the necessary context
  22870. (defun org-isearch-end ()
  22871. "Reveal context after isearch exits."
  22872. (when isearch-success ; only if search was successful
  22873. (if (featurep 'xemacs)
  22874. ;; Under XEmacs, the hook is run in the correct place,
  22875. ;; we directly show the context.
  22876. (org-show-context 'isearch)
  22877. ;; In Emacs the hook runs *before* restoring the overlays.
  22878. ;; So we have to use a one-time post-command-hook to do this.
  22879. ;; (Emacs 22 has a special variable, see function `org-mode')
  22880. (unless (and (boundp 'isearch-mode-end-hook-quit)
  22881. isearch-mode-end-hook-quit)
  22882. ;; Only when the isearch was not quitted.
  22883. (org-add-hook 'post-command-hook 'org-isearch-post-command
  22884. 'append 'local)))))
  22885. (defun org-isearch-post-command ()
  22886. "Remove self from hook, and show context."
  22887. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  22888. (org-show-context 'isearch))
  22889. ;;;; Integration with and fixes for other packages
  22890. ;;; Imenu support
  22891. (defvar-local org-imenu-markers nil
  22892. "All markers currently used by Imenu.")
  22893. (defun org-imenu-new-marker (&optional pos)
  22894. "Return a new marker for use by Imenu, and remember the marker."
  22895. (let ((m (make-marker)))
  22896. (move-marker m (or pos (point)))
  22897. (push m org-imenu-markers)
  22898. m))
  22899. (defun org-imenu-get-tree ()
  22900. "Produce the index for Imenu."
  22901. (dolist (x org-imenu-markers) (move-marker x nil))
  22902. (setq org-imenu-markers nil)
  22903. (let* ((n org-imenu-depth)
  22904. (re (concat "^" (org-get-limited-outline-regexp)))
  22905. (subs (make-vector (1+ n) nil))
  22906. (last-level 0)
  22907. m level head0 head)
  22908. (save-excursion
  22909. (save-restriction
  22910. (widen)
  22911. (goto-char (point-max))
  22912. (while (re-search-backward re nil t)
  22913. (setq level (org-reduced-level (funcall outline-level)))
  22914. (when (and (<= level n)
  22915. (looking-at org-complex-heading-regexp)
  22916. (setq head0 (org-match-string-no-properties 4)))
  22917. (setq head (org-link-display-format head0)
  22918. m (org-imenu-new-marker))
  22919. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  22920. (if (>= level last-level)
  22921. (push (cons head m) (aref subs level))
  22922. (push (cons head (aref subs (1+ level))) (aref subs level))
  22923. (cl-loop for i from (1+ level) to n do (aset subs i nil)))
  22924. (setq last-level level)))))
  22925. (aref subs 1)))
  22926. (eval-after-load "imenu"
  22927. '(progn
  22928. (add-hook 'imenu-after-jump-hook
  22929. (lambda ()
  22930. (when (derived-mode-p 'org-mode)
  22931. (org-show-context 'org-goto))))))
  22932. (defun org-link-display-format (link)
  22933. "Replace a link with its the description.
  22934. If there is no description, use the link target."
  22935. (save-match-data
  22936. (if (string-match org-bracket-link-analytic-regexp link)
  22937. (replace-match (if (match-end 5)
  22938. (match-string 5 link)
  22939. (concat (match-string 1 link)
  22940. (match-string 3 link)))
  22941. nil t link)
  22942. link)))
  22943. (defun org-toggle-link-display ()
  22944. "Toggle the literal or descriptive display of links."
  22945. (interactive)
  22946. (if org-descriptive-links
  22947. (progn (org-remove-from-invisibility-spec '(org-link))
  22948. (org-restart-font-lock)
  22949. (setq org-descriptive-links nil))
  22950. (progn (add-to-invisibility-spec '(org-link))
  22951. (org-restart-font-lock)
  22952. (setq org-descriptive-links t))))
  22953. ;; Speedbar support
  22954. (defvar org-speedbar-restriction-lock-overlay (make-overlay 1 1)
  22955. "Overlay marking the agenda restriction line in speedbar.")
  22956. (overlay-put org-speedbar-restriction-lock-overlay
  22957. 'face 'org-agenda-restriction-lock)
  22958. (overlay-put org-speedbar-restriction-lock-overlay
  22959. 'help-echo "Agendas are currently limited to this item.")
  22960. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  22961. (defun org-speedbar-set-agenda-restriction ()
  22962. "Restrict future agenda commands to the location at point in speedbar.
  22963. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  22964. (interactive)
  22965. (require 'org-agenda)
  22966. (let (p m tp np dir txt)
  22967. (cond
  22968. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  22969. 'org-imenu t))
  22970. (setq m (get-text-property p 'org-imenu-marker))
  22971. (with-current-buffer (marker-buffer m)
  22972. (goto-char m)
  22973. (org-agenda-set-restriction-lock 'subtree)))
  22974. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  22975. 'speedbar-function 'speedbar-find-file))
  22976. (setq tp (previous-single-property-change
  22977. (1+ p) 'speedbar-function)
  22978. np (next-single-property-change
  22979. tp 'speedbar-function)
  22980. dir (speedbar-line-directory)
  22981. txt (buffer-substring-no-properties (or tp (point-min))
  22982. (or np (point-max))))
  22983. (with-current-buffer (find-file-noselect
  22984. (let ((default-directory dir))
  22985. (expand-file-name txt)))
  22986. (unless (derived-mode-p 'org-mode)
  22987. (user-error "Cannot restrict to non-Org-mode file"))
  22988. (org-agenda-set-restriction-lock 'file)))
  22989. (t (user-error "Don't know how to restrict Org-mode's agenda")))
  22990. (move-overlay org-speedbar-restriction-lock-overlay
  22991. (point-at-bol) (point-at-eol))
  22992. (setq current-prefix-arg nil)
  22993. (org-agenda-maybe-redo)))
  22994. (defvar speedbar-file-key-map)
  22995. (declare-function speedbar-add-supported-extension "speedbar" (extension))
  22996. (eval-after-load "speedbar"
  22997. '(progn
  22998. (speedbar-add-supported-extension ".org")
  22999. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  23000. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  23001. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  23002. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  23003. (add-hook 'speedbar-visiting-tag-hook
  23004. (lambda () (and (derived-mode-p 'org-mode) (org-show-context 'org-goto))))))
  23005. ;;; Fixes and Hacks for problems with other packages
  23006. (defun org--flyspell-object-check-p (element)
  23007. "Non-nil when Flyspell can check object at point.
  23008. ELEMENT is the element at point."
  23009. (let ((object (save-excursion
  23010. (when (org-looking-at-p "\\>") (backward-char))
  23011. (org-element-context element))))
  23012. (cl-case (org-element-type object)
  23013. ;; Prevent checks in links due to keybinding conflict with
  23014. ;; Flyspell.
  23015. ((code entity export-snippet inline-babel-call
  23016. inline-src-block line-break latex-fragment link macro
  23017. statistics-cookie target timestamp verbatim)
  23018. nil)
  23019. (footnote-reference
  23020. ;; Only in inline footnotes, within the definition.
  23021. (and (eq (org-element-property :type object) 'inline)
  23022. (< (save-excursion
  23023. (goto-char (org-element-property :begin object))
  23024. (search-forward ":" nil t 2))
  23025. (point))))
  23026. (otherwise t))))
  23027. (defun org-mode-flyspell-verify ()
  23028. "Function used for `flyspell-generic-check-word-predicate'."
  23029. (if (org-at-heading-p)
  23030. ;; At a headline or an inlinetask, check title only. This is
  23031. ;; faster than relying on `org-element-at-point'.
  23032. (and (save-excursion (beginning-of-line)
  23033. (and (let ((case-fold-search t))
  23034. (not (looking-at "\\*+ END[ \t]*$")))
  23035. (looking-at org-complex-heading-regexp)))
  23036. (match-beginning 4)
  23037. (>= (point) (match-beginning 4))
  23038. (or (not (match-beginning 5))
  23039. (< (point) (match-beginning 5))))
  23040. (let* ((element (org-element-at-point))
  23041. (post-affiliated (org-element-property :post-affiliated element)))
  23042. (cond
  23043. ;; Ignore checks in all affiliated keywords but captions.
  23044. ((< (point) post-affiliated)
  23045. (and (save-excursion
  23046. (beginning-of-line)
  23047. (let ((case-fold-search t)) (looking-at "[ \t]*#\\+CAPTION:")))
  23048. (> (point) (match-end 0))
  23049. (org--flyspell-object-check-p element)))
  23050. ;; Ignore checks in LOGBOOK (or equivalent) drawer.
  23051. ((let ((log (org-log-into-drawer)))
  23052. (and log
  23053. (let ((drawer (org-element-lineage element '(drawer))))
  23054. (and drawer
  23055. (eq (compare-strings
  23056. log nil nil
  23057. (org-element-property :drawer-name drawer) nil nil t)
  23058. t)))))
  23059. nil)
  23060. (t
  23061. (cl-case (org-element-type element)
  23062. ((comment quote-section) t)
  23063. (comment-block
  23064. ;; Allow checks between block markers, not on them.
  23065. (and (> (line-beginning-position) post-affiliated)
  23066. (save-excursion
  23067. (end-of-line)
  23068. (skip-chars-forward " \r\t\n")
  23069. (< (point) (org-element-property :end element)))))
  23070. ;; Arbitrary list of keywords where checks are meaningful.
  23071. ;; Make sure point is on the value part of the element.
  23072. (keyword
  23073. (and (member (org-element-property :key element)
  23074. '("DESCRIPTION" "TITLE"))
  23075. (save-excursion
  23076. (search-backward ":" (line-beginning-position) t))))
  23077. ;; Check is globally allowed in paragraphs verse blocks and
  23078. ;; table rows (after affiliated keywords) but some objects
  23079. ;; must not be affected.
  23080. ((paragraph table-row verse-block)
  23081. (let ((cbeg (org-element-property :contents-begin element))
  23082. (cend (org-element-property :contents-end element)))
  23083. (and cbeg (>= (point) cbeg) (< (point) cend)
  23084. (org--flyspell-object-check-p element))))))))))
  23085. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  23086. (defun org-remove-flyspell-overlays-in (beg end)
  23087. "Remove flyspell overlays in region."
  23088. (and (org-bound-and-true-p flyspell-mode)
  23089. (fboundp 'flyspell-delete-region-overlays)
  23090. (flyspell-delete-region-overlays beg end)))
  23091. (defvar flyspell-delayed-commands)
  23092. (eval-after-load "flyspell"
  23093. '(add-to-list 'flyspell-delayed-commands 'org-self-insert-command))
  23094. ;; Make `bookmark-jump' shows the jump location if it was hidden.
  23095. (eval-after-load "bookmark"
  23096. '(if (boundp 'bookmark-after-jump-hook)
  23097. ;; We can use the hook
  23098. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  23099. ;; Hook not available, use advice
  23100. (defadvice bookmark-jump (after org-make-visible activate)
  23101. "Make the position visible."
  23102. (org-bookmark-jump-unhide))))
  23103. ;; Make sure saveplace shows the location if it was hidden
  23104. (eval-after-load "saveplace"
  23105. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  23106. "Make the position visible."
  23107. (org-bookmark-jump-unhide)))
  23108. ;; Make sure ecb shows the location if it was hidden
  23109. (eval-after-load "ecb"
  23110. '(defadvice ecb-method-clicked (after esf/org-show-context activate)
  23111. "Make hierarchy visible when jumping into location from ECB tree buffer."
  23112. (when (derived-mode-p 'org-mode)
  23113. (org-show-context))))
  23114. (defun org-bookmark-jump-unhide ()
  23115. "Unhide the current position, to show the bookmark location."
  23116. (and (derived-mode-p 'org-mode)
  23117. (or (outline-invisible-p)
  23118. (save-excursion (goto-char (max (point-min) (1- (point))))
  23119. (outline-invisible-p)))
  23120. (org-show-context 'bookmark-jump)))
  23121. (defun org-mark-jump-unhide ()
  23122. "Make the point visible with `org-show-context' after jumping to the mark."
  23123. (when (and (derived-mode-p 'org-mode)
  23124. (outline-invisible-p))
  23125. (org-show-context 'mark-goto)))
  23126. (eval-after-load "simple"
  23127. '(defadvice pop-to-mark-command (after org-make-visible activate)
  23128. "Make the point visible with `org-show-context'."
  23129. (org-mark-jump-unhide)))
  23130. (eval-after-load "simple"
  23131. '(defadvice exchange-point-and-mark (after org-make-visible activate)
  23132. "Make the point visible with `org-show-context'."
  23133. (org-mark-jump-unhide)))
  23134. (eval-after-load "simple"
  23135. '(defadvice pop-global-mark (after org-make-visible activate)
  23136. "Make the point visible with `org-show-context'."
  23137. (org-mark-jump-unhide)))
  23138. ;; Make session.el ignore our circular variable
  23139. (defvar session-globals-exclude)
  23140. (eval-after-load "session"
  23141. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  23142. ;;;; Finish up
  23143. (provide 'org)
  23144. (run-hooks 'org-load-hook)
  23145. ;;; org.el ends here