org.el 956 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106171071710817109171101711117112171131711417115171161711717118171191712017121171221712317124171251712617127171281712917130171311713217133171341713517136171371713817139171401714117142171431714417145171461714717148171491715017151171521715317154171551715617157171581715917160171611716217163171641716517166171671716817169171701717117172171731717417175171761717717178171791718017181171821718317184171851718617187171881718917190171911719217193171941719517196171971719817199172001720117202172031720417205172061720717208172091721017211172121721317214172151721617217172181721917220172211722217223172241722517226172271722817229172301723117232172331723417235172361723717238172391724017241172421724317244172451724617247172481724917250172511725217253172541725517256172571725817259172601726117262172631726417265172661726717268172691727017271172721727317274172751727617277172781727917280172811728217283172841728517286172871728817289172901729117292172931729417295172961729717298172991730017301173021730317304173051730617307173081730917310173111731217313173141731517316173171731817319173201732117322173231732417325173261732717328173291733017331173321733317334173351733617337173381733917340173411734217343173441734517346173471734817349173501735117352173531735417355173561735717358173591736017361173621736317364173651736617367173681736917370173711737217373173741737517376173771737817379173801738117382173831738417385173861738717388173891739017391173921739317394173951739617397173981739917400174011740217403174041740517406174071740817409174101741117412174131741417415174161741717418174191742017421174221742317424174251742617427174281742917430174311743217433174341743517436174371743817439174401744117442174431744417445174461744717448174491745017451174521745317454174551745617457174581745917460174611746217463174641746517466174671746817469174701747117472174731747417475174761747717478174791748017481174821748317484174851748617487174881748917490174911749217493174941749517496174971749817499175001750117502175031750417505175061750717508175091751017511175121751317514175151751617517175181751917520175211752217523175241752517526175271752817529175301753117532175331753417535175361753717538175391754017541175421754317544175451754617547175481754917550175511755217553175541755517556175571755817559175601756117562175631756417565175661756717568175691757017571175721757317574175751757617577175781757917580175811758217583175841758517586175871758817589175901759117592175931759417595175961759717598175991760017601176021760317604176051760617607176081760917610176111761217613176141761517616176171761817619176201762117622176231762417625176261762717628176291763017631176321763317634176351763617637176381763917640176411764217643176441764517646176471764817649176501765117652176531765417655176561765717658176591766017661176621766317664176651766617667176681766917670176711767217673176741767517676176771767817679176801768117682176831768417685176861768717688176891769017691176921769317694176951769617697176981769917700177011770217703177041770517706177071770817709177101771117712177131771417715177161771717718177191772017721177221772317724177251772617727177281772917730177311773217733177341773517736177371773817739177401774117742177431774417745177461774717748177491775017751177521775317754177551775617757177581775917760177611776217763177641776517766177671776817769177701777117772177731777417775177761777717778177791778017781177821778317784177851778617787177881778917790177911779217793177941779517796177971779817799178001780117802178031780417805178061780717808178091781017811178121781317814178151781617817178181781917820178211782217823178241782517826178271782817829178301783117832178331783417835178361783717838178391784017841178421784317844178451784617847178481784917850178511785217853178541785517856178571785817859178601786117862178631786417865178661786717868178691787017871178721787317874178751787617877178781787917880178811788217883178841788517886178871788817889178901789117892178931789417895178961789717898178991790017901179021790317904179051790617907179081790917910179111791217913179141791517916179171791817919179201792117922179231792417925179261792717928179291793017931179321793317934179351793617937179381793917940179411794217943179441794517946179471794817949179501795117952179531795417955179561795717958179591796017961179621796317964179651796617967179681796917970179711797217973179741797517976179771797817979179801798117982179831798417985179861798717988179891799017991179921799317994179951799617997179981799918000180011800218003180041800518006180071800818009180101801118012180131801418015180161801718018180191802018021180221802318024180251802618027180281802918030180311803218033180341803518036180371803818039180401804118042180431804418045180461804718048180491805018051180521805318054180551805618057180581805918060180611806218063180641806518066180671806818069180701807118072180731807418075180761807718078180791808018081180821808318084180851808618087180881808918090180911809218093180941809518096180971809818099181001810118102181031810418105181061810718108181091811018111181121811318114181151811618117181181811918120181211812218123181241812518126181271812818129181301813118132181331813418135181361813718138181391814018141181421814318144181451814618147181481814918150181511815218153181541815518156181571815818159181601816118162181631816418165181661816718168181691817018171181721817318174181751817618177181781817918180181811818218183181841818518186181871818818189181901819118192181931819418195181961819718198181991820018201182021820318204182051820618207182081820918210182111821218213182141821518216182171821818219182201822118222182231822418225182261822718228182291823018231182321823318234182351823618237182381823918240182411824218243182441824518246182471824818249182501825118252182531825418255182561825718258182591826018261182621826318264182651826618267182681826918270182711827218273182741827518276182771827818279182801828118282182831828418285182861828718288182891829018291182921829318294182951829618297182981829918300183011830218303183041830518306183071830818309183101831118312183131831418315183161831718318183191832018321183221832318324183251832618327183281832918330183311833218333183341833518336183371833818339183401834118342183431834418345183461834718348183491835018351183521835318354183551835618357183581835918360183611836218363183641836518366183671836818369183701837118372183731837418375183761837718378183791838018381183821838318384183851838618387183881838918390183911839218393183941839518396183971839818399184001840118402184031840418405184061840718408184091841018411184121841318414184151841618417184181841918420184211842218423184241842518426184271842818429184301843118432184331843418435184361843718438184391844018441184421844318444184451844618447184481844918450184511845218453184541845518456184571845818459184601846118462184631846418465184661846718468184691847018471184721847318474184751847618477184781847918480184811848218483184841848518486184871848818489184901849118492184931849418495184961849718498184991850018501185021850318504185051850618507185081850918510185111851218513185141851518516185171851818519185201852118522185231852418525185261852718528185291853018531185321853318534185351853618537185381853918540185411854218543185441854518546185471854818549185501855118552185531855418555185561855718558185591856018561185621856318564185651856618567185681856918570185711857218573185741857518576185771857818579185801858118582185831858418585185861858718588185891859018591185921859318594185951859618597185981859918600186011860218603186041860518606186071860818609186101861118612186131861418615186161861718618186191862018621186221862318624186251862618627186281862918630186311863218633186341863518636186371863818639186401864118642186431864418645186461864718648186491865018651186521865318654186551865618657186581865918660186611866218663186641866518666186671866818669186701867118672186731867418675186761867718678186791868018681186821868318684186851868618687186881868918690186911869218693186941869518696186971869818699187001870118702187031870418705187061870718708187091871018711187121871318714187151871618717187181871918720187211872218723187241872518726187271872818729187301873118732187331873418735187361873718738187391874018741187421874318744187451874618747187481874918750187511875218753187541875518756187571875818759187601876118762187631876418765187661876718768187691877018771187721877318774187751877618777187781877918780187811878218783187841878518786187871878818789187901879118792187931879418795187961879718798187991880018801188021880318804188051880618807188081880918810188111881218813188141881518816188171881818819188201882118822188231882418825188261882718828188291883018831188321883318834188351883618837188381883918840188411884218843188441884518846188471884818849188501885118852188531885418855188561885718858188591886018861188621886318864188651886618867188681886918870188711887218873188741887518876188771887818879188801888118882188831888418885188861888718888188891889018891188921889318894188951889618897188981889918900189011890218903189041890518906189071890818909189101891118912189131891418915189161891718918189191892018921189221892318924189251892618927189281892918930189311893218933189341893518936189371893818939189401894118942189431894418945189461894718948189491895018951189521895318954189551895618957189581895918960189611896218963189641896518966189671896818969189701897118972189731897418975189761897718978189791898018981189821898318984189851898618987189881898918990189911899218993189941899518996189971899818999190001900119002190031900419005190061900719008190091901019011190121901319014190151901619017190181901919020190211902219023190241902519026190271902819029190301903119032190331903419035190361903719038190391904019041190421904319044190451904619047190481904919050190511905219053190541905519056190571905819059190601906119062190631906419065190661906719068190691907019071190721907319074190751907619077190781907919080190811908219083190841908519086190871908819089190901909119092190931909419095190961909719098190991910019101191021910319104191051910619107191081910919110191111911219113191141911519116191171911819119191201912119122191231912419125191261912719128191291913019131191321913319134191351913619137191381913919140191411914219143191441914519146191471914819149191501915119152191531915419155191561915719158191591916019161191621916319164191651916619167191681916919170191711917219173191741917519176191771917819179191801918119182191831918419185191861918719188191891919019191191921919319194191951919619197191981919919200192011920219203192041920519206192071920819209192101921119212192131921419215192161921719218192191922019221192221922319224192251922619227192281922919230192311923219233192341923519236192371923819239192401924119242192431924419245192461924719248192491925019251192521925319254192551925619257192581925919260192611926219263192641926519266192671926819269192701927119272192731927419275192761927719278192791928019281192821928319284192851928619287192881928919290192911929219293192941929519296192971929819299193001930119302193031930419305193061930719308193091931019311193121931319314193151931619317193181931919320193211932219323193241932519326193271932819329193301933119332193331933419335193361933719338193391934019341193421934319344193451934619347193481934919350193511935219353193541935519356193571935819359193601936119362193631936419365193661936719368193691937019371193721937319374193751937619377193781937919380193811938219383193841938519386193871938819389193901939119392193931939419395193961939719398193991940019401194021940319404194051940619407194081940919410194111941219413194141941519416194171941819419194201942119422194231942419425194261942719428194291943019431194321943319434194351943619437194381943919440194411944219443194441944519446194471944819449194501945119452194531945419455194561945719458194591946019461194621946319464194651946619467194681946919470194711947219473194741947519476194771947819479194801948119482194831948419485194861948719488194891949019491194921949319494194951949619497194981949919500195011950219503195041950519506195071950819509195101951119512195131951419515195161951719518195191952019521195221952319524195251952619527195281952919530195311953219533195341953519536195371953819539195401954119542195431954419545195461954719548195491955019551195521955319554195551955619557195581955919560195611956219563195641956519566195671956819569195701957119572195731957419575195761957719578195791958019581195821958319584195851958619587195881958919590195911959219593195941959519596195971959819599196001960119602196031960419605196061960719608196091961019611196121961319614196151961619617196181961919620196211962219623196241962519626196271962819629196301963119632196331963419635196361963719638196391964019641196421964319644196451964619647196481964919650196511965219653196541965519656196571965819659196601966119662196631966419665196661966719668196691967019671196721967319674196751967619677196781967919680196811968219683196841968519686196871968819689196901969119692196931969419695196961969719698196991970019701197021970319704197051970619707197081970919710197111971219713197141971519716197171971819719197201972119722197231972419725197261972719728197291973019731197321973319734197351973619737197381973919740197411974219743197441974519746197471974819749197501975119752197531975419755197561975719758197591976019761197621976319764197651976619767197681976919770197711977219773197741977519776197771977819779197801978119782197831978419785197861978719788197891979019791197921979319794197951979619797197981979919800198011980219803198041980519806198071980819809198101981119812198131981419815198161981719818198191982019821198221982319824198251982619827198281982919830198311983219833198341983519836198371983819839198401984119842198431984419845198461984719848198491985019851198521985319854198551985619857198581985919860198611986219863198641986519866198671986819869198701987119872198731987419875198761987719878198791988019881198821988319884198851988619887198881988919890198911989219893198941989519896198971989819899199001990119902199031990419905199061990719908199091991019911199121991319914199151991619917199181991919920199211992219923199241992519926199271992819929199301993119932199331993419935199361993719938199391994019941199421994319944199451994619947199481994919950199511995219953199541995519956199571995819959199601996119962199631996419965199661996719968199691997019971199721997319974199751997619977199781997919980199811998219983199841998519986199871998819989199901999119992199931999419995199961999719998199992000020001200022000320004200052000620007200082000920010200112001220013200142001520016200172001820019200202002120022200232002420025200262002720028200292003020031200322003320034200352003620037200382003920040200412004220043200442004520046200472004820049200502005120052200532005420055200562005720058200592006020061200622006320064200652006620067200682006920070200712007220073200742007520076200772007820079200802008120082200832008420085200862008720088200892009020091200922009320094200952009620097200982009920100201012010220103201042010520106201072010820109201102011120112201132011420115201162011720118201192012020121201222012320124201252012620127201282012920130201312013220133201342013520136201372013820139201402014120142201432014420145201462014720148201492015020151201522015320154201552015620157201582015920160201612016220163201642016520166201672016820169201702017120172201732017420175201762017720178201792018020181201822018320184201852018620187201882018920190201912019220193201942019520196201972019820199202002020120202202032020420205202062020720208202092021020211202122021320214202152021620217202182021920220202212022220223202242022520226202272022820229202302023120232202332023420235202362023720238202392024020241202422024320244202452024620247202482024920250202512025220253202542025520256202572025820259202602026120262202632026420265202662026720268202692027020271202722027320274202752027620277202782027920280202812028220283202842028520286202872028820289202902029120292202932029420295202962029720298202992030020301203022030320304203052030620307203082030920310203112031220313203142031520316203172031820319203202032120322203232032420325203262032720328203292033020331203322033320334203352033620337203382033920340203412034220343203442034520346203472034820349203502035120352203532035420355203562035720358203592036020361203622036320364203652036620367203682036920370203712037220373203742037520376203772037820379203802038120382203832038420385203862038720388203892039020391203922039320394203952039620397203982039920400204012040220403204042040520406204072040820409204102041120412204132041420415204162041720418204192042020421204222042320424204252042620427204282042920430204312043220433204342043520436204372043820439204402044120442204432044420445204462044720448204492045020451204522045320454204552045620457204582045920460204612046220463204642046520466204672046820469204702047120472204732047420475204762047720478204792048020481204822048320484204852048620487204882048920490204912049220493204942049520496204972049820499205002050120502205032050420505205062050720508205092051020511205122051320514205152051620517205182051920520205212052220523205242052520526205272052820529205302053120532205332053420535205362053720538205392054020541205422054320544205452054620547205482054920550205512055220553205542055520556205572055820559205602056120562205632056420565205662056720568205692057020571205722057320574205752057620577205782057920580205812058220583205842058520586205872058820589205902059120592205932059420595205962059720598205992060020601206022060320604206052060620607206082060920610206112061220613206142061520616206172061820619206202062120622206232062420625206262062720628206292063020631206322063320634206352063620637206382063920640206412064220643206442064520646206472064820649206502065120652206532065420655206562065720658206592066020661206622066320664206652066620667206682066920670206712067220673206742067520676206772067820679206802068120682206832068420685206862068720688206892069020691206922069320694206952069620697206982069920700207012070220703207042070520706207072070820709207102071120712207132071420715207162071720718207192072020721207222072320724207252072620727207282072920730207312073220733207342073520736207372073820739207402074120742207432074420745207462074720748207492075020751207522075320754207552075620757207582075920760207612076220763207642076520766207672076820769207702077120772207732077420775207762077720778207792078020781207822078320784207852078620787207882078920790207912079220793207942079520796207972079820799208002080120802208032080420805208062080720808208092081020811208122081320814208152081620817208182081920820208212082220823208242082520826208272082820829208302083120832208332083420835208362083720838208392084020841208422084320844208452084620847208482084920850208512085220853208542085520856208572085820859208602086120862208632086420865208662086720868208692087020871208722087320874208752087620877208782087920880208812088220883208842088520886208872088820889208902089120892208932089420895208962089720898208992090020901209022090320904209052090620907209082090920910209112091220913209142091520916209172091820919209202092120922209232092420925209262092720928209292093020931209322093320934209352093620937209382093920940209412094220943209442094520946209472094820949209502095120952209532095420955209562095720958209592096020961209622096320964209652096620967209682096920970209712097220973209742097520976209772097820979209802098120982209832098420985209862098720988209892099020991209922099320994209952099620997209982099921000210012100221003210042100521006210072100821009210102101121012210132101421015210162101721018210192102021021210222102321024210252102621027210282102921030210312103221033210342103521036210372103821039210402104121042210432104421045210462104721048210492105021051210522105321054210552105621057210582105921060210612106221063210642106521066210672106821069210702107121072210732107421075210762107721078210792108021081210822108321084210852108621087210882108921090210912109221093210942109521096210972109821099211002110121102211032110421105211062110721108211092111021111211122111321114211152111621117211182111921120211212112221123211242112521126211272112821129211302113121132211332113421135211362113721138211392114021141211422114321144211452114621147211482114921150211512115221153211542115521156211572115821159211602116121162211632116421165211662116721168211692117021171211722117321174211752117621177211782117921180211812118221183211842118521186211872118821189211902119121192211932119421195211962119721198211992120021201212022120321204212052120621207212082120921210212112121221213212142121521216212172121821219212202122121222212232122421225212262122721228212292123021231212322123321234212352123621237212382123921240212412124221243212442124521246212472124821249212502125121252212532125421255212562125721258212592126021261212622126321264212652126621267212682126921270212712127221273212742127521276212772127821279212802128121282212832128421285212862128721288212892129021291212922129321294212952129621297212982129921300213012130221303213042130521306213072130821309213102131121312213132131421315213162131721318213192132021321213222132321324213252132621327213282132921330213312133221333213342133521336213372133821339213402134121342213432134421345213462134721348213492135021351213522135321354213552135621357213582135921360213612136221363213642136521366213672136821369213702137121372213732137421375213762137721378213792138021381213822138321384213852138621387213882138921390213912139221393213942139521396213972139821399214002140121402214032140421405214062140721408214092141021411214122141321414214152141621417214182141921420214212142221423214242142521426214272142821429214302143121432214332143421435214362143721438214392144021441214422144321444214452144621447214482144921450214512145221453214542145521456214572145821459214602146121462214632146421465214662146721468214692147021471214722147321474214752147621477214782147921480214812148221483214842148521486214872148821489214902149121492214932149421495214962149721498214992150021501215022150321504215052150621507215082150921510215112151221513215142151521516215172151821519215202152121522215232152421525215262152721528215292153021531215322153321534215352153621537215382153921540215412154221543215442154521546215472154821549215502155121552215532155421555215562155721558215592156021561215622156321564215652156621567215682156921570215712157221573215742157521576215772157821579215802158121582215832158421585215862158721588215892159021591215922159321594215952159621597215982159921600216012160221603216042160521606216072160821609216102161121612216132161421615216162161721618216192162021621216222162321624216252162621627216282162921630216312163221633216342163521636216372163821639216402164121642216432164421645216462164721648216492165021651216522165321654216552165621657216582165921660216612166221663216642166521666216672166821669216702167121672216732167421675216762167721678216792168021681216822168321684216852168621687216882168921690216912169221693216942169521696216972169821699217002170121702217032170421705217062170721708217092171021711217122171321714217152171621717217182171921720217212172221723217242172521726217272172821729217302173121732217332173421735217362173721738217392174021741217422174321744217452174621747217482174921750217512175221753217542175521756217572175821759217602176121762217632176421765217662176721768217692177021771217722177321774217752177621777217782177921780217812178221783217842178521786217872178821789217902179121792217932179421795217962179721798217992180021801218022180321804218052180621807218082180921810218112181221813218142181521816218172181821819218202182121822218232182421825218262182721828218292183021831218322183321834218352183621837218382183921840218412184221843218442184521846218472184821849218502185121852218532185421855218562185721858218592186021861218622186321864218652186621867218682186921870218712187221873218742187521876218772187821879218802188121882218832188421885218862188721888218892189021891218922189321894218952189621897218982189921900219012190221903219042190521906219072190821909219102191121912219132191421915219162191721918219192192021921219222192321924219252192621927219282192921930219312193221933219342193521936219372193821939219402194121942219432194421945219462194721948219492195021951219522195321954219552195621957219582195921960219612196221963219642196521966219672196821969219702197121972219732197421975219762197721978219792198021981219822198321984219852198621987219882198921990219912199221993219942199521996219972199821999220002200122002220032200422005220062200722008220092201022011220122201322014220152201622017220182201922020220212202222023220242202522026220272202822029220302203122032220332203422035220362203722038220392204022041220422204322044220452204622047220482204922050220512205222053220542205522056220572205822059220602206122062220632206422065220662206722068220692207022071220722207322074220752207622077220782207922080220812208222083220842208522086220872208822089220902209122092220932209422095220962209722098220992210022101221022210322104221052210622107221082210922110221112211222113221142211522116221172211822119221202212122122221232212422125221262212722128221292213022131221322213322134221352213622137221382213922140221412214222143221442214522146221472214822149221502215122152221532215422155221562215722158221592216022161221622216322164221652216622167221682216922170221712217222173221742217522176221772217822179221802218122182221832218422185221862218722188221892219022191221922219322194221952219622197221982219922200222012220222203222042220522206222072220822209222102221122212222132221422215222162221722218222192222022221222222222322224222252222622227222282222922230222312223222233222342223522236222372223822239222402224122242222432224422245222462224722248222492225022251222522225322254222552225622257222582225922260222612226222263222642226522266222672226822269222702227122272222732227422275222762227722278222792228022281222822228322284222852228622287222882228922290222912229222293222942229522296222972229822299223002230122302223032230422305223062230722308223092231022311223122231322314223152231622317223182231922320223212232222323223242232522326223272232822329223302233122332223332233422335223362233722338223392234022341223422234322344223452234622347223482234922350223512235222353223542235522356223572235822359223602236122362223632236422365223662236722368223692237022371223722237322374223752237622377223782237922380223812238222383223842238522386223872238822389223902239122392223932239422395223962239722398223992240022401224022240322404224052240622407224082240922410224112241222413224142241522416224172241822419224202242122422224232242422425224262242722428224292243022431224322243322434224352243622437224382243922440224412244222443224442244522446224472244822449224502245122452224532245422455224562245722458224592246022461224622246322464224652246622467224682246922470224712247222473224742247522476224772247822479224802248122482224832248422485224862248722488224892249022491224922249322494224952249622497224982249922500225012250222503225042250522506225072250822509225102251122512225132251422515225162251722518225192252022521225222252322524225252252622527225282252922530225312253222533225342253522536225372253822539225402254122542225432254422545225462254722548225492255022551225522255322554225552255622557225582255922560225612256222563225642256522566225672256822569225702257122572225732257422575225762257722578225792258022581225822258322584225852258622587225882258922590225912259222593225942259522596225972259822599226002260122602226032260422605226062260722608226092261022611226122261322614226152261622617226182261922620226212262222623226242262522626226272262822629226302263122632226332263422635226362263722638226392264022641226422264322644226452264622647226482264922650226512265222653226542265522656226572265822659226602266122662226632266422665226662266722668226692267022671226722267322674226752267622677226782267922680226812268222683226842268522686226872268822689226902269122692226932269422695226962269722698226992270022701227022270322704227052270622707227082270922710227112271222713227142271522716227172271822719227202272122722227232272422725227262272722728227292273022731227322273322734227352273622737227382273922740227412274222743227442274522746227472274822749227502275122752227532275422755227562275722758227592276022761227622276322764227652276622767227682276922770227712277222773227742277522776227772277822779227802278122782227832278422785227862278722788227892279022791227922279322794227952279622797227982279922800228012280222803228042280522806228072280822809228102281122812228132281422815228162281722818228192282022821228222282322824228252282622827228282282922830228312283222833228342283522836228372283822839228402284122842228432284422845228462284722848228492285022851228522285322854228552285622857228582285922860228612286222863228642286522866228672286822869228702287122872228732287422875228762287722878228792288022881228822288322884228852288622887228882288922890228912289222893228942289522896228972289822899229002290122902229032290422905229062290722908229092291022911229122291322914229152291622917229182291922920229212292222923229242292522926229272292822929229302293122932229332293422935229362293722938229392294022941229422294322944229452294622947229482294922950229512295222953229542295522956229572295822959229602296122962229632296422965229662296722968229692297022971229722297322974229752297622977229782297922980229812298222983229842298522986229872298822989229902299122992229932299422995229962299722998229992300023001230022300323004230052300623007230082300923010230112301223013230142301523016230172301823019230202302123022230232302423025230262302723028230292303023031230322303323034230352303623037230382303923040230412304223043230442304523046230472304823049230502305123052230532305423055230562305723058230592306023061230622306323064230652306623067230682306923070230712307223073230742307523076230772307823079230802308123082230832308423085230862308723088230892309023091230922309323094230952309623097230982309923100231012310223103231042310523106231072310823109231102311123112231132311423115231162311723118231192312023121231222312323124231252312623127231282312923130231312313223133231342313523136231372313823139231402314123142231432314423145231462314723148231492315023151231522315323154231552315623157231582315923160231612316223163231642316523166231672316823169231702317123172231732317423175231762317723178231792318023181231822318323184231852318623187231882318923190231912319223193231942319523196231972319823199232002320123202232032320423205232062320723208232092321023211232122321323214232152321623217232182321923220232212322223223232242322523226232272322823229232302323123232232332323423235232362323723238232392324023241232422324323244232452324623247232482324923250232512325223253232542325523256232572325823259232602326123262232632326423265232662326723268232692327023271232722327323274232752327623277232782327923280232812328223283232842328523286232872328823289232902329123292232932329423295232962329723298232992330023301233022330323304233052330623307233082330923310233112331223313233142331523316233172331823319233202332123322233232332423325233262332723328233292333023331233322333323334233352333623337233382333923340233412334223343233442334523346233472334823349233502335123352233532335423355233562335723358233592336023361233622336323364233652336623367233682336923370233712337223373233742337523376233772337823379233802338123382233832338423385233862338723388233892339023391233922339323394233952339623397233982339923400234012340223403234042340523406234072340823409234102341123412234132341423415234162341723418234192342023421234222342323424234252342623427234282342923430234312343223433234342343523436234372343823439234402344123442234432344423445234462344723448234492345023451234522345323454234552345623457234582345923460234612346223463234642346523466234672346823469234702347123472234732347423475234762347723478234792348023481234822348323484234852348623487234882348923490234912349223493234942349523496234972349823499235002350123502235032350423505235062350723508235092351023511235122351323514235152351623517235182351923520235212352223523235242352523526235272352823529235302353123532235332353423535235362353723538235392354023541235422354323544235452354623547235482354923550235512355223553235542355523556235572355823559235602356123562235632356423565235662356723568235692357023571235722357323574235752357623577235782357923580235812358223583235842358523586235872358823589235902359123592235932359423595235962359723598235992360023601236022360323604236052360623607236082360923610236112361223613236142361523616236172361823619236202362123622236232362423625236262362723628236292363023631236322363323634236352363623637236382363923640236412364223643236442364523646236472364823649236502365123652236532365423655236562365723658236592366023661236622366323664236652366623667236682366923670236712367223673236742367523676236772367823679236802368123682236832368423685236862368723688236892369023691236922369323694236952369623697236982369923700237012370223703237042370523706237072370823709237102371123712237132371423715237162371723718237192372023721237222372323724237252372623727237282372923730237312373223733237342373523736237372373823739237402374123742237432374423745237462374723748237492375023751237522375323754237552375623757237582375923760237612376223763237642376523766237672376823769237702377123772237732377423775237762377723778237792378023781237822378323784237852378623787237882378923790237912379223793237942379523796237972379823799238002380123802238032380423805238062380723808238092381023811238122381323814238152381623817238182381923820238212382223823238242382523826238272382823829238302383123832238332383423835238362383723838238392384023841238422384323844238452384623847238482384923850238512385223853238542385523856238572385823859238602386123862238632386423865238662386723868238692387023871238722387323874238752387623877238782387923880238812388223883238842388523886238872388823889238902389123892238932389423895238962389723898238992390023901239022390323904239052390623907239082390923910239112391223913239142391523916239172391823919239202392123922239232392423925239262392723928239292393023931239322393323934239352393623937239382393923940239412394223943239442394523946239472394823949239502395123952239532395423955239562395723958239592396023961239622396323964239652396623967239682396923970239712397223973239742397523976239772397823979239802398123982239832398423985239862398723988239892399023991239922399323994239952399623997239982399924000240012400224003240042400524006240072400824009240102401124012240132401424015240162401724018240192402024021240222402324024240252402624027240282402924030240312403224033240342403524036240372403824039240402404124042240432404424045240462404724048240492405024051240522405324054240552405624057240582405924060240612406224063240642406524066240672406824069240702407124072240732407424075240762407724078240792408024081240822408324084240852408624087240882408924090240912409224093240942409524096240972409824099241002410124102241032410424105241062410724108241092411024111241122411324114241152411624117241182411924120241212412224123241242412524126241272412824129241302413124132241332413424135241362413724138241392414024141241422414324144241452414624147241482414924150241512415224153241542415524156241572415824159241602416124162241632416424165241662416724168241692417024171241722417324174241752417624177241782417924180241812418224183241842418524186241872418824189241902419124192241932419424195241962419724198241992420024201242022420324204242052420624207242082420924210242112421224213242142421524216242172421824219242202422124222242232422424225242262422724228242292423024231242322423324234242352423624237242382423924240242412424224243242442424524246242472424824249242502425124252242532425424255242562425724258242592426024261242622426324264242652426624267242682426924270242712427224273242742427524276242772427824279242802428124282242832428424285242862428724288242892429024291242922429324294242952429624297242982429924300243012430224303243042430524306243072430824309243102431124312243132431424315243162431724318243192432024321243222432324324243252432624327243282432924330243312433224333243342433524336243372433824339243402434124342243432434424345243462434724348243492435024351243522435324354243552435624357243582435924360243612436224363243642436524366243672436824369243702437124372243732437424375243762437724378243792438024381243822438324384243852438624387243882438924390243912439224393243942439524396243972439824399244002440124402244032440424405244062440724408244092441024411244122441324414244152441624417244182441924420244212442224423244242442524426244272442824429244302443124432244332443424435244362443724438244392444024441244422444324444244452444624447244482444924450244512445224453244542445524456244572445824459244602446124462244632446424465244662446724468244692447024471244722447324474244752447624477244782447924480244812448224483244842448524486244872448824489244902449124492244932449424495244962449724498244992450024501245022450324504245052450624507245082450924510245112451224513245142451524516245172451824519245202452124522245232452424525245262452724528245292453024531245322453324534245352453624537245382453924540245412454224543245442454524546245472454824549245502455124552245532455424555245562455724558245592456024561245622456324564245652456624567245682456924570245712457224573245742457524576245772457824579245802458124582245832458424585245862458724588245892459024591245922459324594245952459624597245982459924600246012460224603246042460524606246072460824609246102461124612246132461424615246162461724618246192462024621246222462324624246252462624627246282462924630246312463224633246342463524636246372463824639246402464124642246432464424645246462464724648246492465024651246522465324654246552465624657246582465924660246612466224663246642466524666246672466824669246702467124672246732467424675246762467724678246792468024681246822468324684246852468624687246882468924690246912469224693246942469524696246972469824699247002470124702247032470424705247062470724708247092471024711247122471324714247152471624717247182471924720247212472224723247242472524726247272472824729247302473124732247332473424735247362473724738247392474024741247422474324744247452474624747247482474924750247512475224753247542475524756247572475824759247602476124762247632476424765247662476724768247692477024771247722477324774247752477624777247782477924780247812478224783247842478524786247872478824789247902479124792247932479424795247962479724798247992480024801248022480324804248052480624807248082480924810248112481224813248142481524816248172481824819248202482124822248232482424825248262482724828248292483024831248322483324834248352483624837248382483924840248412484224843248442484524846248472484824849248502485124852248532485424855248562485724858248592486024861248622486324864248652486624867248682486924870248712487224873248742487524876248772487824879248802488124882248832488424885248862488724888248892489024891248922489324894248952489624897248982489924900249012490224903249042490524906249072490824909249102491124912249132491424915249162491724918249192492024921249222492324924249252492624927249282492924930249312493224933249342493524936249372493824939249402494124942249432494424945249462494724948249492495024951249522495324954249552495624957249582495924960249612496224963249642496524966249672496824969249702497124972249732497424975249762497724978249792498024981249822498324984249852498624987249882498924990249912499224993249942499524996249972499824999250002500125002250032500425005250062500725008250092501025011250122501325014250152501625017250182501925020250212502225023250242502525026250272502825029250302503125032250332503425035250362503725038250392504025041250422504325044250452504625047250482504925050250512505225053250542505525056250572505825059250602506125062250632506425065250662506725068250692507025071250722507325074250752507625077250782507925080250812508225083250842508525086250872508825089250902509125092250932509425095250962509725098250992510025101251022510325104251052510625107251082510925110251112511225113251142511525116251172511825119251202512125122251232512425125251262512725128251292513025131251322513325134251352513625137251382513925140251412514225143251442514525146251472514825149251502515125152251532515425155251562515725158251592516025161251622516325164251652516625167251682516925170251712517225173251742517525176251772517825179251802518125182251832518425185251862518725188251892519025191251922519325194251952519625197251982519925200252012520225203252042520525206252072520825209252102521125212252132521425215252162521725218252192522025221252222522325224252252522625227252282522925230252312523225233252342523525236252372523825239252402524125242252432524425245
  1. ;;; org.el --- Outline-based notes management and organizer
  2. ;; Carstens outline-mode for keeping track of everything.
  3. ;; Copyright (C) 2004-2015 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
  67. (require 'cl)
  68. (require 'gnus-sum))
  69. (require 'calendar)
  70. (require 'find-func)
  71. (require 'format-spec)
  72. (or (equal this-command 'eval-buffer)
  73. (condition-case nil
  74. (load (concat (file-name-directory load-file-name)
  75. "org-loaddefs.el")
  76. nil t t t)
  77. (error
  78. (message "WARNING: No org-loaddefs.el file could be found from where org.el is loaded.")
  79. (sit-for 3)
  80. (message "You need to run \"make\" or \"make autoloads\" from Org lisp directory")
  81. (sit-for 3))))
  82. (require 'org-macs)
  83. (require 'org-compat)
  84. ;; `org-outline-regexp' ought to be a defconst but is let-bound in
  85. ;; some places -- e.g. see the macro `org-with-limited-levels'.
  86. ;;
  87. ;; In Org buffers, the value of `outline-regexp' is that of
  88. ;; `org-outline-regexp'. The only function still directly relying on
  89. ;; `outline-regexp' is `org-overview' so that `org-cycle' can do its
  90. ;; job when `orgstruct-mode' is active.
  91. (defvar org-outline-regexp "\\*+ "
  92. "Regexp to match Org headlines.")
  93. (defvar org-outline-regexp-bol "^\\*+ "
  94. "Regexp to match Org headlines.
  95. This is similar to `org-outline-regexp' but additionally makes
  96. sure that we are at the beginning of the line.")
  97. (defvar org-heading-regexp "^\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  98. "Matches a headline, putting stars and text into groups.
  99. Stars are put in group 1 and the trimmed body in group 2.")
  100. (declare-function cdlatex-environment "ext:cdlatex" (environment item))
  101. (declare-function org-add-archive-files "org-archive" (files))
  102. (declare-function org-agenda-entry-get-agenda-timestamp "org-agenda" (pom))
  103. (declare-function org-agenda-list "org-agenda" (&optional arg start-day span))
  104. (declare-function org-agenda-redo "org-agenda" (&optional all))
  105. (declare-function org-babel-do-in-edit-buffer "ob-core" (&rest body))
  106. (declare-function org-babel-tangle-file "ob-tangle" (file &optional target-file lang))
  107. (declare-function org-beamer-mode "ox-beamer" ())
  108. (declare-function org-clock-get-last-clock-out-time "org-clock" ())
  109. (declare-function org-clock-out "org-clock" (&optional switch-to-state fail-quietly at-time))
  110. (declare-function org-clock-remove-overlays "org-clock" (&optional beg end noremove))
  111. (declare-function org-clock-sum "org-clock" (&optional tstart tend headline-filter propname))
  112. (declare-function org-clock-sum-current-item "org-clock" (&optional tstart))
  113. (declare-function org-clock-timestamps-down "org-clock" (&optional n))
  114. (declare-function org-clock-timestamps-up "org-clock" (&optional n))
  115. (declare-function org-clock-update-time-maybe "org-clock" ())
  116. (declare-function org-clocktable-shift "org-clock" (dir n))
  117. (declare-function org-element-at-point "org-element" ())
  118. (declare-function org-element-cache-refresh "org-element" (pos))
  119. (declare-function org-element-cache-reset "org-element" (&optional all))
  120. (declare-function org-element-contents "org-element" (element))
  121. (declare-function org-element-context "org-element" (&optional element))
  122. (declare-function org-element-copy "org-element" (datum))
  123. (declare-function org-element-interpret-data "org-element" (data &optional parent))
  124. (declare-function org-element-lineage "org-element" (blob &optional types with-self))
  125. (declare-function org-element-nested-p "org-element" (elem-a elem-b))
  126. (declare-function org-element-parse-buffer "org-element" (&optional granularity visible-only))
  127. (declare-function org-element-property "org-element" (property element))
  128. (declare-function org-element-put-property "org-element" (element property value))
  129. (declare-function org-element-swap-A-B "org-element" (elem-a elem-b))
  130. (declare-function org-element-type "org-element" (element))
  131. (declare-function org-element-update-syntax "org-element" ())
  132. (declare-function org-id-find-id-file "org-id" (id))
  133. (declare-function org-id-get-create "org-id" (&optional force))
  134. (declare-function org-inlinetask-at-task-p "org-inlinetask" ())
  135. (declare-function org-inlinetask-outline-regexp "org-inlinetask" ())
  136. (declare-function org-inlinetask-toggle-visibility "org-inlinetask" ())
  137. (declare-function org-plot/gnuplot "org-plot" (&optional params))
  138. (declare-function org-pop-to-buffer-same-window "org-compat" (&optional buffer-or-name norecord label))
  139. (declare-function org-table-align "org-table" ())
  140. (declare-function org-table-begin "org-table" (&optional table-type))
  141. (declare-function org-table-beginning-of-field "org-table" (&optional n))
  142. (declare-function org-table-blank-field "org-table" ())
  143. (declare-function org-table-calc-current-TBLFM "org-table" (&optional arg))
  144. (declare-function org-table-edit-field "org-table" (arg))
  145. (declare-function org-table-end "org-table" (&optional table-type))
  146. (declare-function org-table-end-of-field "org-table" (&optional n))
  147. (declare-function org-table-insert-row "org-table" (&optional arg))
  148. (declare-function org-table-justify-field-maybe "org-table" (&optional new))
  149. (declare-function org-table-maybe-eval-formula "org-table" ())
  150. (declare-function org-table-maybe-recalculate-line "org-table" ())
  151. (declare-function org-table-next-row "org-table" ())
  152. (declare-function org-table-paste-rectangle "org-table" ())
  153. (declare-function org-table-wrap-region "org-table" (arg))
  154. (declare-function org-tags-view "org-agenda" (&optional todo-only match))
  155. (declare-function orgtbl-ascii-plot "org-table" (&optional ask))
  156. (declare-function orgtbl-mode "org-table" (&optional arg))
  157. (defsubst org-uniquify (list)
  158. "Non-destructively remove duplicate elements from LIST."
  159. (let ((res (copy-sequence list))) (delete-dups res)))
  160. (defsubst org-get-at-bol (property)
  161. "Get text property PROPERTY at the beginning of line."
  162. (get-text-property (point-at-bol) property))
  163. (defsubst org-trim (s)
  164. "Remove whitespace at the beginning and the end of string S."
  165. (replace-regexp-in-string
  166. "\\`[ \t\n\r]+" ""
  167. (replace-regexp-in-string "[ \t\n\r]+\\'" "" s)))
  168. ;; load languages based on value of `org-babel-load-languages'
  169. (defvar org-babel-load-languages)
  170. ;;;###autoload
  171. (defun org-babel-do-load-languages (sym value)
  172. "Load the languages defined in `org-babel-load-languages'."
  173. (set-default sym value)
  174. (mapc (lambda (pair)
  175. (let ((active (cdr pair)) (lang (symbol-name (car pair))))
  176. (if active
  177. (progn
  178. (require (intern (concat "ob-" lang))))
  179. (progn
  180. (funcall 'fmakunbound
  181. (intern (concat "org-babel-execute:" lang)))
  182. (funcall 'fmakunbound
  183. (intern (concat "org-babel-expand-body:" lang)))))))
  184. org-babel-load-languages))
  185. (declare-function org-babel-tangle-file "ob-tangle" (file &optional target-file lang))
  186. ;;;###autoload
  187. (defun org-babel-load-file (file &optional compile)
  188. "Load Emacs Lisp source code blocks in the Org-mode FILE.
  189. This function exports the source code using `org-babel-tangle'
  190. and then loads the resulting file using `load-file'. With prefix
  191. arg (noninteractively: 2nd arg) COMPILE the tangled Emacs Lisp
  192. file to byte-code before it is loaded."
  193. (interactive "fFile to load: \nP")
  194. (let* ((age (lambda (file)
  195. (float-time
  196. (time-subtract (current-time)
  197. (nth 5 (or (file-attributes (file-truename file))
  198. (file-attributes file)))))))
  199. (base-name (file-name-sans-extension file))
  200. (exported-file (concat base-name ".el")))
  201. ;; tangle if the org-mode file is newer than the elisp file
  202. (unless (and (file-exists-p exported-file)
  203. (> (funcall age file) (funcall age exported-file)))
  204. ;; Tangle-file traversal returns reversed list of tangled files
  205. ;; and we want to evaluate the first target.
  206. (setq exported-file
  207. (car (last (org-babel-tangle-file file exported-file "emacs-lisp")))))
  208. (message "%s %s"
  209. (if compile
  210. (progn (byte-compile-file exported-file 'load)
  211. "Compiled and loaded")
  212. (progn (load-file exported-file) "Loaded"))
  213. exported-file)))
  214. (defcustom org-babel-load-languages '((emacs-lisp . t))
  215. "Languages which can be evaluated in Org-mode buffers.
  216. This list can be used to load support for any of the languages
  217. below, note that each language will depend on a different set of
  218. system executables and/or Emacs modes. When a language is
  219. \"loaded\", then code blocks in that language can be evaluated
  220. with `org-babel-execute-src-block' bound by default to C-c
  221. C-c (note the `org-babel-no-eval-on-ctrl-c-ctrl-c' variable can
  222. be set to remove code block evaluation from the C-c C-c
  223. keybinding. By default only Emacs Lisp (which has no
  224. requirements) is loaded."
  225. :group 'org-babel
  226. :set 'org-babel-do-load-languages
  227. :version "24.1"
  228. :type '(alist :tag "Babel Languages"
  229. :key-type
  230. (choice
  231. (const :tag "Awk" awk)
  232. (const :tag "C" C)
  233. (const :tag "R" R)
  234. (const :tag "Asymptote" asymptote)
  235. (const :tag "Calc" calc)
  236. (const :tag "Clojure" clojure)
  237. (const :tag "CSS" css)
  238. (const :tag "Ditaa" ditaa)
  239. (const :tag "Dot" dot)
  240. (const :tag "Emacs Lisp" emacs-lisp)
  241. (const :tag "Forth" forth)
  242. (const :tag "Fortran" fortran)
  243. (const :tag "Gnuplot" gnuplot)
  244. (const :tag "Haskell" haskell)
  245. (const :tag "IO" io)
  246. (const :tag "J" J)
  247. (const :tag "Java" java)
  248. (const :tag "Javascript" js)
  249. (const :tag "LaTeX" latex)
  250. (const :tag "Ledger" ledger)
  251. (const :tag "Lilypond" lilypond)
  252. (const :tag "Lisp" lisp)
  253. (const :tag "Makefile" makefile)
  254. (const :tag "Maxima" maxima)
  255. (const :tag "Matlab" matlab)
  256. (const :tag "Mscgen" mscgen)
  257. (const :tag "Ocaml" ocaml)
  258. (const :tag "Octave" octave)
  259. (const :tag "Org" org)
  260. (const :tag "Perl" perl)
  261. (const :tag "Pico Lisp" picolisp)
  262. (const :tag "PlantUML" plantuml)
  263. (const :tag "Python" python)
  264. (const :tag "Ruby" ruby)
  265. (const :tag "Sass" sass)
  266. (const :tag "Scala" scala)
  267. (const :tag "Scheme" scheme)
  268. (const :tag "Screen" screen)
  269. (const :tag "Shell Script" shell)
  270. (const :tag "Shen" shen)
  271. (const :tag "Sql" sql)
  272. (const :tag "Sqlite" sqlite)
  273. (const :tag "Stan" stan)
  274. (const :tag "ebnf2ps" ebnf2ps))
  275. :value-type (boolean :tag "Activate" :value t)))
  276. ;;;; Customization variables
  277. (defcustom org-clone-delete-id nil
  278. "Remove ID property of clones of a subtree.
  279. When non-nil, clones of a subtree don't inherit the ID property.
  280. Otherwise they inherit the ID property with a new unique
  281. identifier."
  282. :type 'boolean
  283. :version "24.1"
  284. :group 'org-id)
  285. ;;; Version
  286. (org-check-version)
  287. ;;;###autoload
  288. (defun org-version (&optional here full message)
  289. "Show the org-mode version.
  290. Interactively, or when MESSAGE is non-nil, show it in echo area.
  291. With prefix argument, or when HERE is non-nil, insert it at point.
  292. In non-interactive uses, a reduced version string is output unless
  293. FULL is given."
  294. (interactive (list current-prefix-arg t (not current-prefix-arg)))
  295. (let* ((org-dir (ignore-errors (org-find-library-dir "org")))
  296. (save-load-suffixes (when (boundp 'load-suffixes) load-suffixes))
  297. (load-suffixes (list ".el"))
  298. (org-install-dir (ignore-errors (org-find-library-dir "org-loaddefs")))
  299. (org-trash (or
  300. (and (fboundp 'org-release) (fboundp 'org-git-version))
  301. (org-load-noerror-mustsuffix (concat org-dir "org-version"))))
  302. (load-suffixes save-load-suffixes)
  303. (org-version (org-release))
  304. (git-version (org-git-version))
  305. (version (format "Org-mode version %s (%s @ %s)"
  306. org-version
  307. git-version
  308. (if org-install-dir
  309. (if (string= org-dir org-install-dir)
  310. org-install-dir
  311. (concat "mixed installation! " org-install-dir " and " org-dir))
  312. "org-loaddefs.el can not be found!")))
  313. (version1 (if full version org-version)))
  314. (when here (insert version1))
  315. (when message (message "%s" version1))
  316. version1))
  317. (defconst org-version (org-version))
  318. ;;; Syntax Constants
  319. ;;;; Block
  320. (defconst org-block-regexp
  321. "^[ \t]*#\\+begin_?\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_?\\1[ \t]*$"
  322. "Regular expression for hiding blocks.")
  323. (defconst org-dblock-start-re
  324. "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  325. "Matches the start line of a dynamic block, with parameters.")
  326. (defconst org-dblock-end-re "^[ \t]*#\\+\\(?:END\\|end\\)\\([: \t\r\n]\\|$\\)"
  327. "Matches the end of a dynamic block.")
  328. ;;;; Clock and Planning
  329. (defconst org-clock-string "CLOCK:"
  330. "String used as prefix for timestamps clocking work hours on an item.")
  331. (defvar org-closed-string "CLOSED:"
  332. "String used as the prefix for timestamps logging closing a TODO entry.")
  333. (defvar org-deadline-string "DEADLINE:"
  334. "String to mark deadline entries.
  335. A deadline is this string, followed by a time stamp. Should be a word,
  336. terminated by a colon. You can insert a schedule keyword and
  337. a timestamp with \\[org-deadline].")
  338. (defvar org-scheduled-string "SCHEDULED:"
  339. "String to mark scheduled TODO entries.
  340. A schedule is this string, followed by a time stamp. Should be a word,
  341. terminated by a colon. You can insert a schedule keyword and
  342. a timestamp with \\[org-schedule].")
  343. (defconst org-ds-keyword-length
  344. (+ 2
  345. (apply #'max
  346. (mapcar #'length
  347. (list org-deadline-string org-scheduled-string
  348. org-clock-string org-closed-string))))
  349. "Maximum length of the DEADLINE and SCHEDULED keywords.")
  350. (defconst org-planning-line-re
  351. (concat "^[ \t]*"
  352. (regexp-opt
  353. (list org-closed-string org-deadline-string org-scheduled-string)
  354. t))
  355. "Matches a line with planning info.
  356. Matched keyword is in group 1.")
  357. (defconst org-clock-line-re
  358. (concat "^[ \t]*" org-clock-string)
  359. "Matches a line with clock info.")
  360. (defconst org-deadline-regexp (concat "\\<" org-deadline-string)
  361. "Matches the DEADLINE keyword.")
  362. (defconst org-deadline-time-regexp
  363. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  364. "Matches the DEADLINE keyword together with a time stamp.")
  365. (defconst org-deadline-time-hour-regexp
  366. (concat "\\<" org-deadline-string
  367. " *<\\([^>]+[0-9]\\{1,2\\}:[0-9]\\{2\\}[0-9-+:hdwmy \t.]*\\)>")
  368. "Matches the DEADLINE keyword together with a time-and-hour stamp.")
  369. (defconst org-deadline-line-regexp
  370. (concat "\\<\\(" org-deadline-string "\\).*")
  371. "Matches the DEADLINE keyword and the rest of the line.")
  372. (defconst org-scheduled-regexp (concat "\\<" org-scheduled-string)
  373. "Matches the SCHEDULED keyword.")
  374. (defconst org-scheduled-time-regexp
  375. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  376. "Matches the SCHEDULED keyword together with a time stamp.")
  377. (defconst org-scheduled-time-hour-regexp
  378. (concat "\\<" org-scheduled-string
  379. " *<\\([^>]+[0-9]\\{1,2\\}:[0-9]\\{2\\}[0-9-+:hdwmy \t.]*\\)>")
  380. "Matches the SCHEDULED keyword together with a time-and-hour stamp.")
  381. (defconst org-closed-time-regexp
  382. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  383. "Matches the CLOSED keyword together with a time stamp.")
  384. (defconst org-keyword-time-regexp
  385. (concat "\\<"
  386. (regexp-opt
  387. (list org-scheduled-string org-deadline-string org-closed-string
  388. org-clock-string)
  389. t)
  390. " *[[<]\\([^]>]+\\)[]>]")
  391. "Matches any of the 4 keywords, together with the time stamp.")
  392. (defconst org-keyword-time-not-clock-regexp
  393. (concat
  394. "\\<"
  395. (regexp-opt
  396. (list org-scheduled-string org-deadline-string org-closed-string) t)
  397. " *[[<]\\([^]>]+\\)[]>]")
  398. "Matches any of the 3 keywords, together with the time stamp.")
  399. (defconst org-maybe-keyword-time-regexp
  400. (concat "\\(\\<"
  401. (regexp-opt
  402. (list org-scheduled-string org-deadline-string org-closed-string
  403. org-clock-string)
  404. t)
  405. "\\)?"
  406. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?[]>]"
  407. "\\|"
  408. "<%%([^\r\n>]*>\\)")
  409. "Matches a timestamp, possibly preceded by a keyword.")
  410. (defconst org-all-time-keywords
  411. (mapcar (lambda (w) (substring w 0 -1))
  412. (list org-scheduled-string org-deadline-string
  413. org-clock-string org-closed-string))
  414. "List of time keywords.")
  415. ;;;; Drawer
  416. (defconst org-drawer-regexp "^[ \t]*:\\(\\(?:\\w\\|[-_]\\)+\\):[ \t]*$"
  417. "Matches first or last line of a hidden block.
  418. Group 1 contains drawer's name or \"END\".")
  419. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  420. "Regular expression matching the first line of a property drawer.")
  421. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  422. "Regular expression matching the last line of a property drawer.")
  423. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  424. "Regular expression matching the first line of a clock drawer.")
  425. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  426. "Regular expression matching the last line of a clock drawer.")
  427. (defconst org-property-drawer-re
  428. (concat "^[ \t]*:PROPERTIES:[ \t]*\n"
  429. "\\(?:[ \t]*:\\S-+:\\(?: .*\\)?[ \t]*\n\\)*?"
  430. "[ \t]*:END:[ \t]*$")
  431. "Matches an entire property drawer.")
  432. (defconst org-clock-drawer-re
  433. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*?\\("
  434. org-clock-drawer-end-re "\\)\n?")
  435. "Matches an entire clock drawer.")
  436. ;;;; Headline
  437. (defconst org-heading-keyword-regexp-format
  438. "^\\(\\*+\\)\\(?: +%s\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  439. "Printf format for a regexp matching a headline with some keyword.
  440. This regexp will match the headline of any node which has the
  441. exact keyword that is put into the format. The keyword isn't in
  442. any group by default, but the stars and the body are.")
  443. (defconst org-heading-keyword-maybe-regexp-format
  444. "^\\(\\*+\\)\\(?: +%s\\)?\\(?: +\\(.*?\\)\\)?[ \t]*$"
  445. "Printf format for a regexp matching a headline, possibly with some keyword.
  446. This regexp can match any headline with the specified keyword, or
  447. without a keyword. The keyword isn't in any group by default,
  448. but the stars and the body are.")
  449. (defconst org-archive-tag "ARCHIVE"
  450. "The tag that marks a subtree as archived.
  451. An archived subtree does not open during visibility cycling, and does
  452. not contribute to the agenda listings.")
  453. (defconst org-comment-string "COMMENT"
  454. "Entries starting with this keyword will never be exported.
  455. An entry can be toggled between COMMENT and normal with
  456. \\[org-toggle-comment].")
  457. ;;;; LaTeX Environments and Fragments
  458. (defconst org-latex-regexps
  459. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  460. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  461. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  462. ("$1" "\\([^$]\\|^\\)\\(\\$[^ \r\n,;.$]\\$\\)\\(\\s.\\|\\s-\\|\\s(\\|\\s)\\|\\s\"\\|\000\\|$\\)" 2 nil)
  463. ("$" "\\([^$]\\|^\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\(\\s.\\|\\s-\\|\\s(\\|\\s)\\|\\s\"\\|\000\\|$\\)" 2 nil)
  464. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  465. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
  466. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
  467. "Regular expressions for matching embedded LaTeX.")
  468. ;;;; Node Property
  469. (defconst org-effort-property "Effort"
  470. "The property that is being used to keep track of effort estimates.
  471. Effort estimates given in this property need to have the format H:MM.")
  472. ;;;; Table
  473. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  474. "Detect an org-type or table-type table.")
  475. (defconst org-table-line-regexp "^[ \t]*|"
  476. "Detect an org-type table line.")
  477. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  478. "Detect an org-type table line.")
  479. (defconst org-table-hline-regexp "^[ \t]*|-"
  480. "Detect an org-type table hline.")
  481. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  482. "Detect a table-type table hline.")
  483. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  484. "Detect the first line outside a table when searching from within it.
  485. This works for both table types.")
  486. (defconst org-TBLFM-regexp "^[ \t]*#\\+TBLFM: "
  487. "Detect a #+TBLFM line.")
  488. ;;;; Timestamp
  489. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^\r\n>]*?\\)>"
  490. "Regular expression for fast time stamp matching.")
  491. (defconst org-ts-regexp-inactive
  492. "\\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^\r\n>]*?\\)\\]"
  493. "Regular expression for fast inactive time stamp matching.")
  494. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?\\)[]>]"
  495. "Regular expression for fast time stamp matching.")
  496. (defconst org-ts-regexp0
  497. "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\)\\( +[^]+0-9>\r\n -]+\\)?\\( +\\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  498. "Regular expression matching time strings for analysis.
  499. This one does not require the space after the date, so it can be used
  500. on a string that terminates immediately after the date.")
  501. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]+0-9>\r\n -]*\\)\\( \\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  502. "Regular expression matching time strings for analysis.")
  503. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  504. "Regular expression matching time stamps, with groups.")
  505. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  506. "Regular expression matching time stamps (also [..]), with groups.")
  507. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  508. "Regular expression matching a time stamp range.")
  509. (defconst org-tr-regexp-both
  510. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  511. "Regular expression matching a time stamp range.")
  512. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  513. org-ts-regexp "\\)?")
  514. "Regular expression matching a time stamp or time stamp range.")
  515. (defconst org-tsr-regexp-both
  516. (concat org-ts-regexp-both "\\(--?-?"
  517. org-ts-regexp-both "\\)?")
  518. "Regular expression matching a time stamp or time stamp range.
  519. The time stamps may be either active or inactive.")
  520. (defconst org-repeat-re
  521. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*?\\([.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)"
  522. "Regular expression for specifying repeated events.
  523. After a match, group 1 contains the repeat expression.")
  524. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  525. "Formats for `format-time-string' which are used for time stamps.")
  526. ;;; The custom variables
  527. (defgroup org nil
  528. "Outline-based notes management and organizer."
  529. :tag "Org"
  530. :group 'outlines
  531. :group 'calendar)
  532. (defcustom org-mode-hook nil
  533. "Mode hook for Org-mode, run after the mode was turned on."
  534. :group 'org
  535. :type 'hook)
  536. (defcustom org-load-hook nil
  537. "Hook that is run after org.el has been loaded."
  538. :group 'org
  539. :type 'hook)
  540. (defcustom org-log-buffer-setup-hook nil
  541. "Hook that is run after an Org log buffer is created."
  542. :group 'org
  543. :version "24.1"
  544. :type 'hook)
  545. (defvar org-modules) ; defined below
  546. (defvar org-modules-loaded nil
  547. "Have the modules been loaded already?")
  548. (defun org-load-modules-maybe (&optional force)
  549. "Load all extensions listed in `org-modules'."
  550. (when (or force (not org-modules-loaded))
  551. (mapc (lambda (ext)
  552. (condition-case nil (require ext)
  553. (error (message "Problems while trying to load feature `%s'" ext))))
  554. org-modules)
  555. (setq org-modules-loaded t)))
  556. (defun org-set-modules (var value)
  557. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  558. (set var value)
  559. (when (featurep 'org)
  560. (org-load-modules-maybe 'force)
  561. (org-element-cache-reset 'all)))
  562. (defcustom org-modules '(org-w3m org-bbdb org-bibtex org-docview org-gnus org-info org-irc org-mhe org-rmail)
  563. "Modules that should always be loaded together with org.el.
  564. If a description starts with <C>, the file is not part of Emacs
  565. and loading it will require that you have downloaded and properly
  566. installed the Org mode distribution.
  567. You can also use this system to load external packages (i.e. neither Org
  568. core modules, nor modules from the CONTRIB directory). Just add symbols
  569. to the end of the list. If the package is called org-xyz.el, then you need
  570. to add the symbol `xyz', and the package must have a call to:
  571. (provide \\='org-xyz)
  572. For export specific modules, see also `org-export-backends'."
  573. :group 'org
  574. :set 'org-set-modules
  575. :version "24.4"
  576. :package-version '(Org . "8.0")
  577. :type
  578. '(set :greedy t
  579. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  580. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  581. (const :tag " crypt: Encryption of subtrees" org-crypt)
  582. (const :tag " ctags: Access to Emacs tags with links" org-ctags)
  583. (const :tag " docview: Links to doc-view buffers" org-docview)
  584. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  585. (const :tag " habit: Track your consistency with habits" org-habit)
  586. (const :tag " id: Global IDs for identifying entries" org-id)
  587. (const :tag " info: Links to Info nodes" org-info)
  588. (const :tag " inlinetask: Tasks independent of outline hierarchy" org-inlinetask)
  589. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  590. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  591. (const :tag " mouse: Additional mouse support" org-mouse)
  592. (const :tag " protocol: Intercept calls from emacsclient" org-protocol)
  593. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  594. (const :tag " w3m: Special cut/paste from w3m to Org-mode." org-w3m)
  595. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  596. (const :tag "C bookmark: Org-mode links to bookmarks" org-bookmark)
  597. (const :tag "C bullets: Add overlays to headlines stars" org-bullets)
  598. (const :tag "C checklist: Extra functions for checklists in repeated tasks" org-checklist)
  599. (const :tag "C choose: Use TODO keywords to mark decisions states" org-choose)
  600. (const :tag "C collector: Collect properties into tables" org-collector)
  601. (const :tag "C depend: TODO dependencies for Org-mode\n\t\t\t(PARTIALLY OBSOLETE, see built-in dependency support))" org-depend)
  602. (const :tag "C drill: Flashcards and spaced repetition for Org-mode" org-drill)
  603. (const :tag "C elisp-symbol: Org-mode links to emacs-lisp symbols" org-elisp-symbol)
  604. (const :tag "C eshell Support for links to working directories in eshell" org-eshell)
  605. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  606. (const :tag "C eval: Include command output as text" org-eval)
  607. (const :tag "C eww: Store link to url of eww" org-eww)
  608. (const :tag "C expiry: Expiry mechanism for Org-mode entries" org-expiry)
  609. (const :tag "C favtable: Lookup table of favorite references and links" org-favtable)
  610. (const :tag "C git-link: Provide org links to specific file version" org-git-link)
  611. (const :tag "C interactive-query: Interactive modification of tags query\n\t\t\t(PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query)
  612. (const :tag "C invoice: Help manage client invoices in Org-mode" org-invoice)
  613. (const :tag "C learn: SuperMemo's incremental learning algorithm" org-learn)
  614. (const :tag "C mac-iCal Imports events from iCal.app to the Emacs diary" org-mac-iCal)
  615. (const :tag "C mac-link: Grab links and url from various mac Applications" org-mac-link)
  616. (const :tag "C mairix: Hook mairix search into Org-mode for different MUAs" org-mairix)
  617. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  618. (const :tag "C mew: Links to Mew folders/messages" org-mew)
  619. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  620. (const :tag "C notmuch: Provide org links to notmuch searches or messages" org-notmuch)
  621. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  622. (const :tag "C registry: A registry for Org-mode links" org-registry)
  623. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  624. (const :tag "C secretary: Team management with org-mode" org-secretary)
  625. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  626. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  627. (const :tag "C track: Keep up with Org-mode development" org-track)
  628. (const :tag "C velocity Something like Notational Velocity for Org" org-velocity)
  629. (const :tag "C vm: Links to VM folders/messages" org-vm)
  630. (const :tag "C wikinodes: CamelCase wiki-like links" org-wikinodes)
  631. (const :tag "C wl: Links to Wanderlust folders/messages" org-wl)
  632. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  633. (defvar org-export-registered-backends) ; From ox.el.
  634. (declare-function org-export-derived-backend-p "ox" (backend &rest backends))
  635. (declare-function org-export-backend-name "ox" (backend))
  636. (defcustom org-export-backends '(ascii html icalendar latex)
  637. "List of export back-ends that should be always available.
  638. If a description starts with <C>, the file is not part of Emacs
  639. and loading it will require that you have downloaded and properly
  640. installed the Org mode distribution.
  641. Unlike to `org-modules', libraries in this list will not be
  642. loaded along with Org, but only once the export framework is
  643. needed.
  644. This variable needs to be set before org.el is loaded. If you
  645. need to make a change while Emacs is running, use the customize
  646. interface or run the following code, where VAL stands for the new
  647. value of the variable, after updating it:
  648. (progn
  649. (setq org-export-registered-backends
  650. (cl-remove-if-not
  651. (lambda (backend)
  652. (let ((name (org-export-backend-name backend)))
  653. (or (memq name val)
  654. (catch \\='parentp
  655. (dolist (b val)
  656. (and (org-export-derived-backend-p b name)
  657. (throw \\='parentp t)))))))
  658. org-export-registered-backends))
  659. (let ((new-list (mapcar #\\='org-export-backend-name
  660. org-export-registered-backends)))
  661. (dolist (backend val)
  662. (cond
  663. ((not (load (format \"ox-%s\" backend) t t))
  664. (message \"Problems while trying to load export back-end \\=`%s\\='\"
  665. backend))
  666. ((not (memq backend new-list)) (push backend new-list))))
  667. (set-default \\='org-export-backends new-list)))
  668. Adding a back-end to this list will also pull the back-end it
  669. depends on, if any."
  670. :group 'org
  671. :group 'org-export
  672. :version "24.4"
  673. :package-version '(Org . "8.0")
  674. :initialize 'custom-initialize-set
  675. :set (lambda (var val)
  676. (if (not (featurep 'ox)) (set-default var val)
  677. ;; Any back-end not required anymore (not present in VAL and not
  678. ;; a parent of any back-end in the new value) is removed from the
  679. ;; list of registered back-ends.
  680. (setq org-export-registered-backends
  681. (cl-remove-if-not
  682. (lambda (backend)
  683. (let ((name (org-export-backend-name backend)))
  684. (or (memq name val)
  685. (catch 'parentp
  686. (dolist (b val)
  687. (and (org-export-derived-backend-p b name)
  688. (throw 'parentp t)))))))
  689. org-export-registered-backends))
  690. ;; Now build NEW-LIST of both new back-ends and required
  691. ;; parents.
  692. (let ((new-list (mapcar #'org-export-backend-name
  693. org-export-registered-backends)))
  694. (dolist (backend val)
  695. (cond
  696. ((not (load (format "ox-%s" backend) t t))
  697. (message "Problems while trying to load export back-end `%s'"
  698. backend))
  699. ((not (memq backend new-list)) (push backend new-list))))
  700. ;; Set VAR to that list with fixed dependencies.
  701. (set-default var new-list))))
  702. :type '(set :greedy t
  703. (const :tag " ascii Export buffer to ASCII format" ascii)
  704. (const :tag " beamer Export buffer to Beamer presentation" beamer)
  705. (const :tag " html Export buffer to HTML format" html)
  706. (const :tag " icalendar Export buffer to iCalendar format" icalendar)
  707. (const :tag " latex Export buffer to LaTeX format" latex)
  708. (const :tag " man Export buffer to MAN format" man)
  709. (const :tag " md Export buffer to Markdown format" md)
  710. (const :tag " odt Export buffer to ODT format" odt)
  711. (const :tag " org Export buffer to Org format" org)
  712. (const :tag " texinfo Export buffer to Texinfo format" texinfo)
  713. (const :tag "C confluence Export buffer to Confluence Wiki format" confluence)
  714. (const :tag "C deck Export buffer to deck.js presentations" deck)
  715. (const :tag "C freemind Export buffer to Freemind mindmap format" freemind)
  716. (const :tag "C groff Export buffer to Groff format" groff)
  717. (const :tag "C koma-letter Export buffer to KOMA Scrlttrl2 format" koma-letter)
  718. (const :tag "C RSS 2.0 Export buffer to RSS 2.0 format" rss)
  719. (const :tag "C s5 Export buffer to s5 presentations" s5)
  720. (const :tag "C taskjuggler Export buffer to TaskJuggler format" taskjuggler)))
  721. (eval-after-load 'ox
  722. '(mapc
  723. (lambda (backend)
  724. (condition-case nil (require (intern (format "ox-%s" backend)))
  725. (error (message "Problems while trying to load export back-end `%s'"
  726. backend))))
  727. org-export-backends))
  728. (defcustom org-support-shift-select nil
  729. "Non-nil means make shift-cursor commands select text when possible.
  730. \\<org-mode-map>\
  731. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys
  732. start selecting a region, or enlarge regions started in this way.
  733. In Org-mode, in special contexts, these same keys are used for
  734. other purposes, important enough to compete with shift selection.
  735. Org tries to balance these needs by supporting `shift-select-mode'
  736. outside these special contexts, under control of this variable.
  737. The default of this variable is nil, to avoid confusing behavior. Shifted
  738. cursor keys will then execute Org commands in the following contexts:
  739. - on a headline, changing TODO state (left/right) and priority (up/down)
  740. - on a time stamp, changing the time
  741. - in a plain list item, changing the bullet type
  742. - in a property definition line, switching between allowed values
  743. - in the BEGIN line of a clock table (changing the time block).
  744. Outside these contexts, the commands will throw an error.
  745. When this variable is t and the cursor is not in a special
  746. context, Org-mode will support shift-selection for making and
  747. enlarging regions. To make this more effective, the bullet
  748. cycling will no longer happen anywhere in an item line, but only
  749. if the cursor is exactly on the bullet.
  750. If you set this variable to the symbol `always', then the keys
  751. will not be special in headlines, property lines, and item lines,
  752. to make shift selection work there as well. If this is what you
  753. want, you can use the following alternative commands:
  754. `\\[org-todo]' and `\\[org-priority]' \
  755. to change TODO state and priority,
  756. `\\[universal-argument] \\[universal-argument] \\[org-todo]' \
  757. can be used to switch TODO sets,
  758. `\\[org-ctrl-c-minus]' to cycle item bullet types,
  759. and properties can be edited by hand or in column view.
  760. However, when the cursor is on a timestamp, shift-cursor commands
  761. will still edit the time stamp - this is just too good to give up.
  762. XEmacs user should have this variable set to nil, because
  763. `shift-select-mode' is in Emacs 23 or later only."
  764. :group 'org
  765. :type '(choice
  766. (const :tag "Never" nil)
  767. (const :tag "When outside special context" t)
  768. (const :tag "Everywhere except timestamps" always)))
  769. (defcustom org-loop-over-headlines-in-active-region nil
  770. "Shall some commands act upon headlines in the active region?
  771. When set to t, some commands will be performed in all headlines
  772. within the active region.
  773. When set to `start-level', some commands will be performed in all
  774. headlines within the active region, provided that these headlines
  775. are of the same level than the first one.
  776. When set to a string, those commands will be performed on the
  777. matching headlines within the active region. Such string must be
  778. a tags/property/todo match as it is used in the agenda tags view.
  779. The list of commands is: `org-schedule', `org-deadline',
  780. `org-todo', `org-archive-subtree', `org-archive-set-tag' and
  781. `org-archive-to-archive-sibling'. The archiving commands skip
  782. already archived entries."
  783. :type '(choice (const :tag "Don't loop" nil)
  784. (const :tag "All headlines in active region" t)
  785. (const :tag "In active region, headlines at the same level than the first one" start-level)
  786. (string :tag "Tags/Property/Todo matcher"))
  787. :version "24.1"
  788. :group 'org-todo
  789. :group 'org-archive)
  790. (defgroup org-startup nil
  791. "Options concerning startup of Org-mode."
  792. :tag "Org Startup"
  793. :group 'org)
  794. (defcustom org-startup-folded t
  795. "Non-nil means entering Org-mode will switch to OVERVIEW.
  796. This can also be configured on a per-file basis by adding one of
  797. the following lines anywhere in the buffer:
  798. #+STARTUP: fold (or `overview', this is equivalent)
  799. #+STARTUP: nofold (or `showall', this is equivalent)
  800. #+STARTUP: content
  801. #+STARTUP: showeverything
  802. By default, this option is ignored when Org opens agenda files
  803. for the first time. If you want the agenda to honor the startup
  804. option, set `org-agenda-inhibit-startup' to nil."
  805. :group 'org-startup
  806. :type '(choice
  807. (const :tag "nofold: show all" nil)
  808. (const :tag "fold: overview" t)
  809. (const :tag "content: all headlines" content)
  810. (const :tag "show everything, even drawers" showeverything)))
  811. (defcustom org-startup-truncated t
  812. "Non-nil means entering Org-mode will set `truncate-lines'.
  813. This is useful since some lines containing links can be very long and
  814. uninteresting. Also tables look terrible when wrapped."
  815. :group 'org-startup
  816. :type 'boolean)
  817. (defcustom org-startup-indented nil
  818. "Non-nil means turn on `org-indent-mode' on startup.
  819. This can also be configured on a per-file basis by adding one of
  820. the following lines anywhere in the buffer:
  821. #+STARTUP: indent
  822. #+STARTUP: noindent"
  823. :group 'org-structure
  824. :type '(choice
  825. (const :tag "Not" nil)
  826. (const :tag "Globally (slow on startup in large files)" t)))
  827. (defcustom org-use-sub-superscripts t
  828. "Non-nil means interpret \"_\" and \"^\" for display.
  829. If you want to control how Org exports those characters, see
  830. `org-export-with-sub-superscripts'. `org-use-sub-superscripts'
  831. used to be an alias for `org-export-with-sub-superscripts' in
  832. Org <8.0, it is not anymore.
  833. When this option is turned on, you can use TeX-like syntax for
  834. sub- and superscripts within the buffer. Several characters after
  835. \"_\" or \"^\" will be considered as a single item - so grouping
  836. with {} is normally not needed. For example, the following things
  837. will be parsed as single sub- or superscripts:
  838. 10^24 or 10^tau several digits will be considered 1 item.
  839. 10^-12 or 10^-tau a leading sign with digits or a word
  840. x^2-y^3 will be read as x^2 - y^3, because items are
  841. terminated by almost any nonword/nondigit char.
  842. x_{i^2} or x^(2-i) braces or parenthesis do grouping.
  843. Still, ambiguity is possible. So when in doubt, use {} to enclose
  844. the sub/superscript. If you set this variable to the symbol `{}',
  845. the braces are *required* in order to trigger interpretations as
  846. sub/superscript. This can be helpful in documents that need \"_\"
  847. frequently in plain text."
  848. :group 'org-startup
  849. :version "24.4"
  850. :package-version '(Org . "8.0")
  851. :type '(choice
  852. (const :tag "Always interpret" t)
  853. (const :tag "Only with braces" {})
  854. (const :tag "Never interpret" nil)))
  855. (defcustom org-startup-with-beamer-mode nil
  856. "Non-nil means turn on `org-beamer-mode' on startup.
  857. This can also be configured on a per-file basis by adding one of
  858. the following lines anywhere in the buffer:
  859. #+STARTUP: beamer"
  860. :group 'org-startup
  861. :version "24.1"
  862. :type 'boolean)
  863. (defcustom org-startup-align-all-tables nil
  864. "Non-nil means align all tables when visiting a file.
  865. This is useful when the column width in tables is forced with <N> cookies
  866. in table fields. Such tables will look correct only after the first re-align.
  867. This can also be configured on a per-file basis by adding one of
  868. the following lines anywhere in the buffer:
  869. #+STARTUP: align
  870. #+STARTUP: noalign"
  871. :group 'org-startup
  872. :type 'boolean)
  873. (defcustom org-startup-with-inline-images nil
  874. "Non-nil means show inline images when loading a new Org file.
  875. This can also be configured on a per-file basis by adding one of
  876. the following lines anywhere in the buffer:
  877. #+STARTUP: inlineimages
  878. #+STARTUP: noinlineimages"
  879. :group 'org-startup
  880. :version "24.1"
  881. :type 'boolean)
  882. (defcustom org-startup-with-latex-preview nil
  883. "Non-nil means preview LaTeX fragments when loading a new Org file.
  884. This can also be configured on a per-file basis by adding one of
  885. the following lines anywhere in the buffer:
  886. #+STARTUP: latexpreview
  887. #+STARTUP: nolatexpreview"
  888. :group 'org-startup
  889. :version "24.4"
  890. :package-version '(Org . "8.0")
  891. :type 'boolean)
  892. (defcustom org-insert-mode-line-in-empty-file nil
  893. "Non-nil means insert the first line setting Org-mode in empty files.
  894. When the function `org-mode' is called interactively in an empty file, this
  895. normally means that the file name does not automatically trigger Org-mode.
  896. To ensure that the file will always be in Org-mode in the future, a
  897. line enforcing Org-mode will be inserted into the buffer, if this option
  898. has been set."
  899. :group 'org-startup
  900. :type 'boolean)
  901. (defcustom org-replace-disputed-keys nil
  902. "Non-nil means use alternative key bindings for some keys.
  903. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  904. These keys are also used by other packages like shift-selection-mode'
  905. \(built into Emacs 23), `CUA-mode' or `windmove.el'.
  906. If you want to use Org-mode together with one of these other modes,
  907. or more generally if you would like to move some Org-mode commands to
  908. other keys, set this variable and configure the keys with the variable
  909. `org-disputed-keys'.
  910. This option is only relevant at load-time of Org-mode, and must be set
  911. *before* org.el is loaded. Changing it requires a restart of Emacs to
  912. become effective."
  913. :group 'org-startup
  914. :type 'boolean)
  915. (defcustom org-use-extra-keys nil
  916. "Non-nil means use extra key sequence definitions for certain commands.
  917. This happens automatically if you run XEmacs or if `window-system'
  918. is nil. This variable lets you do the same manually. You must
  919. set it before loading org.
  920. Example: on Carbon Emacs 22 running graphically, with an external
  921. keyboard on a Powerbook, the default way of setting M-left might
  922. not work for either Alt or ESC. Setting this variable will make
  923. it work for ESC."
  924. :group 'org-startup
  925. :type 'boolean)
  926. (org-defvaralias 'org-CUA-compatible 'org-replace-disputed-keys)
  927. (defcustom org-disputed-keys
  928. '(([(shift up)] . [(meta p)])
  929. ([(shift down)] . [(meta n)])
  930. ([(shift left)] . [(meta -)])
  931. ([(shift right)] . [(meta +)])
  932. ([(control shift right)] . [(meta shift +)])
  933. ([(control shift left)] . [(meta shift -)]))
  934. "Keys for which Org-mode and other modes compete.
  935. This is an alist, cars are the default keys, second element specifies
  936. the alternative to use when `org-replace-disputed-keys' is t.
  937. Keys can be specified in any syntax supported by `define-key'.
  938. The value of this option takes effect only at Org-mode's startup,
  939. therefore you'll have to restart Emacs to apply it after changing."
  940. :group 'org-startup
  941. :type 'alist)
  942. (defun org-key (key)
  943. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  944. Or return the original if not disputed.
  945. Also apply the translations defined in `org-xemacs-key-equivalents'."
  946. (when org-replace-disputed-keys
  947. (let* ((nkey (key-description key))
  948. (x (org-find-if (lambda (x)
  949. (equal (key-description (car x)) nkey))
  950. org-disputed-keys)))
  951. (setq key (if x (cdr x) key))))
  952. (when (featurep 'xemacs)
  953. (setq key (or (cdr (assoc key org-xemacs-key-equivalents)) key)))
  954. key)
  955. (defun org-find-if (predicate seq)
  956. (catch 'exit
  957. (while seq
  958. (if (funcall predicate (car seq))
  959. (throw 'exit (car seq))
  960. (pop seq)))))
  961. (defun org-defkey (keymap key def)
  962. "Define a key, possibly translated, as returned by `org-key'."
  963. (define-key keymap (org-key key) def))
  964. (defcustom org-ellipsis nil
  965. "The ellipsis to use in the Org-mode outline.
  966. When nil, just use the standard three dots.
  967. When a string, use that string instead.
  968. When a face, use the standard 3 dots, but with the specified face.
  969. The change affects only Org-mode (which will then use its own display table).
  970. Changing this requires executing \\[org-mode] in a buffer to become
  971. effective."
  972. :group 'org-startup
  973. :type '(choice (const :tag "Default" nil)
  974. (face :tag "Face" :value org-warning)
  975. (string :tag "String" :value "...#")))
  976. (defvar org-display-table nil
  977. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  978. (defgroup org-keywords nil
  979. "Keywords in Org-mode."
  980. :tag "Org Keywords"
  981. :group 'org)
  982. (defcustom org-closed-keep-when-no-todo nil
  983. "Remove CLOSED: time-stamp when switching back to a non-todo state?"
  984. :group 'org-todo
  985. :group 'org-keywords
  986. :version "24.4"
  987. :package-version '(Org . "8.0")
  988. :type 'boolean)
  989. (defgroup org-structure nil
  990. "Options concerning the general structure of Org-mode files."
  991. :tag "Org Structure"
  992. :group 'org)
  993. (defgroup org-reveal-location nil
  994. "Options about how to make context of a location visible."
  995. :tag "Org Reveal Location"
  996. :group 'org-structure)
  997. (defcustom org-show-context-detail '((isearch . lineage)
  998. (bookmark-jump . lineage)
  999. (default . ancestors))
  1000. "Alist between context and visibility span when revealing a location.
  1001. \\<org-mode-map>Some actions may move point into invisible
  1002. locations. As a consequence, Org always expose a neighborhood
  1003. around point. How much is shown depends on the initial action,
  1004. or context. Valid contexts are
  1005. agenda when exposing an entry from the agenda
  1006. org-goto when using the command `org-goto' (\\[org-goto])
  1007. occur-tree when using the command `org-occur' (\\[org-sparse-tree] /)
  1008. tags-tree when constructing a sparse tree based on tags matches
  1009. link-search when exposing search matches associated with a link
  1010. mark-goto when exposing the jump goal of a mark
  1011. bookmark-jump when exposing a bookmark location
  1012. isearch when exiting from an incremental search
  1013. default default for all contexts not set explicitly
  1014. Allowed visibility spans are
  1015. minimal show current headline; if point is not on headline,
  1016. also show entry
  1017. local show current headline, entry and next headline
  1018. ancestors show current headline and its direct ancestors; if
  1019. point is not on headline, also show entry
  1020. lineage show current headline, its direct ancestors and all
  1021. their children; if point is not on headline, also show
  1022. entry and first child
  1023. tree show current headline, its direct ancestors and all
  1024. their children; if point is not on headline, also show
  1025. entry and all children
  1026. canonical show current headline, its direct ancestors along with
  1027. their entries and children; if point is not located on
  1028. the headline, also show current entry and all children
  1029. As special cases, a nil or t value means show all contexts in
  1030. `minimal' or `canonical' view, respectively.
  1031. Some views can make displayed information very compact, but also
  1032. make it harder to edit the location of the match. In such
  1033. a case, use the command `org-reveal' (\\[org-reveal]) to show
  1034. more context."
  1035. :group 'org-reveal-location
  1036. :version "25.1"
  1037. :package-version '(Org . "8.3")
  1038. :type '(choice
  1039. (const :tag "Canonical" t)
  1040. (const :tag "Minimal" nil)
  1041. (repeat :greedy t :tag "Individual contexts"
  1042. (cons
  1043. (choice :tag "Context"
  1044. (const agenda)
  1045. (const org-goto)
  1046. (const occur-tree)
  1047. (const tags-tree)
  1048. (const link-search)
  1049. (const mark-goto)
  1050. (const bookmark-jump)
  1051. (const isearch)
  1052. (const default))
  1053. (choice :tag "Detail level"
  1054. (const minimal)
  1055. (const local)
  1056. (const ancestors)
  1057. (const lineage)
  1058. (const tree)
  1059. (const canonical))))))
  1060. (defcustom org-indirect-buffer-display 'other-window
  1061. "How should indirect tree buffers be displayed?
  1062. This applies to indirect buffers created with the commands
  1063. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  1064. Valid values are:
  1065. current-window Display in the current window
  1066. other-window Just display in another window.
  1067. dedicated-frame Create one new frame, and re-use it each time.
  1068. new-frame Make a new frame each time. Note that in this case
  1069. previously-made indirect buffers are kept, and you need to
  1070. kill these buffers yourself."
  1071. :group 'org-structure
  1072. :group 'org-agenda-windows
  1073. :type '(choice
  1074. (const :tag "In current window" current-window)
  1075. (const :tag "In current frame, other window" other-window)
  1076. (const :tag "Each time a new frame" new-frame)
  1077. (const :tag "One dedicated frame" dedicated-frame)))
  1078. (defcustom org-use-speed-commands nil
  1079. "Non-nil means activate single letter commands at beginning of a headline.
  1080. This may also be a function to test for appropriate locations where speed
  1081. commands should be active.
  1082. For example, to activate speed commands when the point is on any
  1083. star at the beginning of the headline, you can do this:
  1084. (setq org-use-speed-commands
  1085. (lambda () (and (looking-at org-outline-regexp) (looking-back \"^\\**\"))))"
  1086. :group 'org-structure
  1087. :type '(choice
  1088. (const :tag "Never" nil)
  1089. (const :tag "At beginning of headline stars" t)
  1090. (function)))
  1091. (defcustom org-speed-commands-user nil
  1092. "Alist of additional speed commands.
  1093. This list will be checked before `org-speed-commands-default'
  1094. when the variable `org-use-speed-commands' is non-nil
  1095. and when the cursor is at the beginning of a headline.
  1096. The car if each entry is a string with a single letter, which must
  1097. be assigned to `self-insert-command' in the global map.
  1098. The cdr is either a command to be called interactively, a function
  1099. to be called, or a form to be evaluated.
  1100. An entry that is just a list with a single string will be interpreted
  1101. as a descriptive headline that will be added when listing the speed
  1102. commands in the Help buffer using the `?' speed command."
  1103. :group 'org-structure
  1104. :type '(repeat :value ("k" . ignore)
  1105. (choice :value ("k" . ignore)
  1106. (list :tag "Descriptive Headline" (string :tag "Headline"))
  1107. (cons :tag "Letter and Command"
  1108. (string :tag "Command letter")
  1109. (choice
  1110. (function)
  1111. (sexp))))))
  1112. (defcustom org-bookmark-names-plist
  1113. '(:last-capture "org-capture-last-stored"
  1114. :last-refile "org-refile-last-stored"
  1115. :last-capture-marker "org-capture-last-stored-marker")
  1116. "Names for bookmarks automatically set by some Org commands.
  1117. This can provide strings as names for a number of bookmarks Org sets
  1118. automatically. The following keys are currently implemented:
  1119. :last-capture
  1120. :last-capture-marker
  1121. :last-refile
  1122. When a key does not show up in the property list, the corresponding bookmark
  1123. is not set."
  1124. :group 'org-structure
  1125. :type 'plist)
  1126. (defgroup org-cycle nil
  1127. "Options concerning visibility cycling in Org-mode."
  1128. :tag "Org Cycle"
  1129. :group 'org-structure)
  1130. (defcustom org-cycle-skip-children-state-if-no-children t
  1131. "Non-nil means skip CHILDREN state in entries that don't have any."
  1132. :group 'org-cycle
  1133. :type 'boolean)
  1134. (defcustom org-cycle-max-level nil
  1135. "Maximum level which should still be subject to visibility cycling.
  1136. Levels higher than this will, for cycling, be treated as text, not a headline.
  1137. When `org-odd-levels-only' is set, a value of N in this variable actually
  1138. means 2N-1 stars as the limiting headline.
  1139. When nil, cycle all levels.
  1140. Note that the limiting level of cycling is also influenced by
  1141. `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
  1142. `org-inlinetask-min-level' is, cycling will be limited to levels one less
  1143. than its value."
  1144. :group 'org-cycle
  1145. :type '(choice
  1146. (const :tag "No limit" nil)
  1147. (integer :tag "Maximum level")))
  1148. (defcustom org-hide-block-startup nil
  1149. "Non-nil means entering Org-mode will fold all blocks.
  1150. This can also be set in on a per-file basis with
  1151. #+STARTUP: hideblocks
  1152. #+STARTUP: showblocks"
  1153. :group 'org-startup
  1154. :group 'org-cycle
  1155. :type 'boolean)
  1156. (defcustom org-cycle-global-at-bob nil
  1157. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  1158. This makes it possible to do global cycling without having to use S-TAB or
  1159. \\[universal-argument] TAB. For this special case to work, the first line
  1160. of the buffer must not be a headline -- it may be empty or some other text.
  1161. When used in this way, `org-cycle-hook' is disabled temporarily to make
  1162. sure the cursor stays at the beginning of the buffer. When this option is
  1163. nil, don't do anything special at the beginning of the buffer."
  1164. :group 'org-cycle
  1165. :type 'boolean)
  1166. (defcustom org-cycle-level-after-item/entry-creation t
  1167. "Non-nil means cycle entry level or item indentation in new empty entries.
  1168. When the cursor is at the end of an empty headline, i.e., with only stars
  1169. and maybe a TODO keyword, TAB will then switch the entry to become a child,
  1170. and then all possible ancestor states, before returning to the original state.
  1171. This makes data entry extremely fast: M-RET to create a new headline,
  1172. on TAB to make it a child, two or more tabs to make it a (grand-)uncle.
  1173. When the cursor is at the end of an empty plain list item, one TAB will
  1174. make it a subitem, two or more tabs will back up to make this an item
  1175. higher up in the item hierarchy."
  1176. :group 'org-cycle
  1177. :type 'boolean)
  1178. (defcustom org-cycle-emulate-tab t
  1179. "Where should `org-cycle' emulate TAB.
  1180. nil Never
  1181. white Only in completely white lines
  1182. whitestart Only at the beginning of lines, before the first non-white char
  1183. t Everywhere except in headlines
  1184. exc-hl-bol Everywhere except at the start of a headline
  1185. If TAB is used in a place where it does not emulate TAB, the current subtree
  1186. visibility is cycled."
  1187. :group 'org-cycle
  1188. :type '(choice (const :tag "Never" nil)
  1189. (const :tag "Only in completely white lines" white)
  1190. (const :tag "Before first char in a line" whitestart)
  1191. (const :tag "Everywhere except in headlines" t)
  1192. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)))
  1193. (defcustom org-cycle-separator-lines 2
  1194. "Number of empty lines needed to keep an empty line between collapsed trees.
  1195. If you leave an empty line between the end of a subtree and the following
  1196. headline, this empty line is hidden when the subtree is folded.
  1197. Org-mode will leave (exactly) one empty line visible if the number of
  1198. empty lines is equal or larger to the number given in this variable.
  1199. So the default 2 means at least 2 empty lines after the end of a subtree
  1200. are needed to produce free space between a collapsed subtree and the
  1201. following headline.
  1202. If the number is negative, and the number of empty lines is at least -N,
  1203. all empty lines are shown.
  1204. Special case: when 0, never leave empty lines in collapsed view."
  1205. :group 'org-cycle
  1206. :type 'integer)
  1207. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  1208. (defcustom org-pre-cycle-hook nil
  1209. "Hook that is run before visibility cycling is happening.
  1210. The function(s) in this hook must accept a single argument which indicates
  1211. the new state that will be set right after running this hook. The
  1212. argument is a symbol. Before a global state change, it can have the values
  1213. `overview', `content', or `all'. Before a local state change, it can have
  1214. the values `folded', `children', or `subtree'."
  1215. :group 'org-cycle
  1216. :type 'hook)
  1217. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  1218. org-cycle-hide-drawers
  1219. org-cycle-show-empty-lines
  1220. org-optimize-window-after-visibility-change)
  1221. "Hook that is run after `org-cycle' has changed the buffer visibility.
  1222. The function(s) in this hook must accept a single argument which indicates
  1223. the new state that was set by the most recent `org-cycle' command. The
  1224. argument is a symbol. After a global state change, it can have the values
  1225. `overview', `contents', or `all'. After a local state change, it can have
  1226. the values `folded', `children', or `subtree'."
  1227. :group 'org-cycle
  1228. :type 'hook
  1229. :version "25.1"
  1230. :package-version '(Org . "8.3"))
  1231. (defgroup org-edit-structure nil
  1232. "Options concerning structure editing in Org-mode."
  1233. :tag "Org Edit Structure"
  1234. :group 'org-structure)
  1235. (defcustom org-odd-levels-only nil
  1236. "Non-nil means skip even levels and only use odd levels for the outline.
  1237. This has the effect that two stars are being added/taken away in
  1238. promotion/demotion commands. It also influences how levels are
  1239. handled by the exporters.
  1240. Changing it requires restart of `font-lock-mode' to become effective
  1241. for fontification also in regions already fontified.
  1242. You may also set this on a per-file basis by adding one of the following
  1243. lines to the buffer:
  1244. #+STARTUP: odd
  1245. #+STARTUP: oddeven"
  1246. :group 'org-edit-structure
  1247. :group 'org-appearance
  1248. :type 'boolean)
  1249. (defcustom org-adapt-indentation t
  1250. "Non-nil means adapt indentation to outline node level.
  1251. When this variable is set, Org assumes that you write outlines by
  1252. indenting text in each node to align with the headline (after the
  1253. stars). The following issues are influenced by this variable:
  1254. - The indentation is increased by one space in a demotion
  1255. command, and decreased by one in a promotion command. However,
  1256. in the latter case, if shifting some line in the entry body
  1257. would alter document structure (e.g., insert a new headline),
  1258. indentation is not changed at all.
  1259. - Property drawers and planning information is inserted indented
  1260. when this variable is set. When nil, they will not be indented.
  1261. - TAB indents a line relative to current level. The lines below
  1262. a headline will be indented when this variable is set.
  1263. Note that this is all about true indentation, by adding and
  1264. removing space characters. See also `org-indent.el' which does
  1265. level-dependent indentation in a virtual way, i.e. at display
  1266. time in Emacs."
  1267. :group 'org-edit-structure
  1268. :type 'boolean)
  1269. (defcustom org-special-ctrl-a/e nil
  1270. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  1271. When t, `C-a' will bring back the cursor to the beginning of the
  1272. headline text, i.e. after the stars and after a possible TODO
  1273. keyword. In an item, this will be the position after bullet and
  1274. check-box, if any. When the cursor is already at that position,
  1275. another `C-a' will bring it to the beginning of the line.
  1276. `C-e' will jump to the end of the headline, ignoring the presence
  1277. of tags in the headline. A second `C-e' will then jump to the
  1278. true end of the line, after any tags. This also means that, when
  1279. this variable is non-nil, `C-e' also will never jump beyond the
  1280. end of the heading of a folded section, i.e. not after the
  1281. ellipses.
  1282. When set to the symbol `reversed', the first `C-a' or `C-e' works
  1283. normally, going to the true line boundary first. Only a directly
  1284. following, identical keypress will bring the cursor to the
  1285. special positions.
  1286. This may also be a cons cell where the behavior for `C-a' and
  1287. `C-e' is set separately."
  1288. :group 'org-edit-structure
  1289. :type '(choice
  1290. (const :tag "off" nil)
  1291. (const :tag "on: after stars/bullet and before tags first" t)
  1292. (const :tag "reversed: true line boundary first" reversed)
  1293. (cons :tag "Set C-a and C-e separately"
  1294. (choice :tag "Special C-a"
  1295. (const :tag "off" nil)
  1296. (const :tag "on: after stars/bullet first" t)
  1297. (const :tag "reversed: before stars/bullet first" reversed))
  1298. (choice :tag "Special C-e"
  1299. (const :tag "off" nil)
  1300. (const :tag "on: before tags first" t)
  1301. (const :tag "reversed: after tags first" reversed)))))
  1302. (org-defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e)
  1303. (defcustom org-special-ctrl-k nil
  1304. "Non-nil means `C-k' will behave specially in headlines.
  1305. When nil, `C-k' will call the default `kill-line' command.
  1306. When t, the following will happen while the cursor is in the headline:
  1307. - When the cursor is at the beginning of a headline, kill the entire
  1308. line and possible the folded subtree below the line.
  1309. - When in the middle of the headline text, kill the headline up to the tags.
  1310. - When after the headline text, kill the tags."
  1311. :group 'org-edit-structure
  1312. :type 'boolean)
  1313. (defcustom org-ctrl-k-protect-subtree nil
  1314. "Non-nil means, do not delete a hidden subtree with C-k.
  1315. When set to the symbol `error', simply throw an error when C-k is
  1316. used to kill (part-of) a headline that has hidden text behind it.
  1317. Any other non-nil value will result in a query to the user, if it is
  1318. OK to kill that hidden subtree. When nil, kill without remorse."
  1319. :group 'org-edit-structure
  1320. :version "24.1"
  1321. :type '(choice
  1322. (const :tag "Do not protect hidden subtrees" nil)
  1323. (const :tag "Protect hidden subtrees with a security query" t)
  1324. (const :tag "Never kill a hidden subtree with C-k" error)))
  1325. (defcustom org-special-ctrl-o t
  1326. "Non-nil means, make `C-o' insert a row in tables."
  1327. :group 'org-edit-structure
  1328. :type 'boolean)
  1329. (defcustom org-catch-invisible-edits nil
  1330. "Check if in invisible region before inserting or deleting a character.
  1331. Valid values are:
  1332. nil Do not check, so just do invisible edits.
  1333. error Throw an error and do nothing.
  1334. show Make point visible, and do the requested edit.
  1335. show-and-error Make point visible, then throw an error and abort the edit.
  1336. smart Make point visible, and do insertion/deletion if it is
  1337. adjacent to visible text and the change feels predictable.
  1338. Never delete a previously invisible character or add in the
  1339. middle or right after an invisible region. Basically, this
  1340. allows insertion and backward-delete right before ellipses.
  1341. FIXME: maybe in this case we should not even show?"
  1342. :group 'org-edit-structure
  1343. :version "24.1"
  1344. :type '(choice
  1345. (const :tag "Do not check" nil)
  1346. (const :tag "Throw error when trying to edit" error)
  1347. (const :tag "Unhide, but do not do the edit" show-and-error)
  1348. (const :tag "Show invisible part and do the edit" show)
  1349. (const :tag "Be smart and do the right thing" smart)))
  1350. (defcustom org-yank-folded-subtrees t
  1351. "Non-nil means when yanking subtrees, fold them.
  1352. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  1353. it starts with a heading and all other headings in it are either children
  1354. or siblings, then fold all the subtrees. However, do this only if no
  1355. text after the yank would be swallowed into a folded tree by this action."
  1356. :group 'org-edit-structure
  1357. :type 'boolean)
  1358. (defcustom org-yank-adjusted-subtrees nil
  1359. "Non-nil means when yanking subtrees, adjust the level.
  1360. With this setting, `org-paste-subtree' is used to insert the subtree, see
  1361. this function for details."
  1362. :group 'org-edit-structure
  1363. :type 'boolean)
  1364. (defcustom org-M-RET-may-split-line '((default . t))
  1365. "Non-nil means M-RET will split the line at the cursor position.
  1366. When nil, it will go to the end of the line before making a
  1367. new line.
  1368. You may also set this option in a different way for different
  1369. contexts. Valid contexts are:
  1370. headline when creating a new headline
  1371. item when creating a new item
  1372. table in a table field
  1373. default the value to be used for all contexts not explicitly
  1374. customized"
  1375. :group 'org-structure
  1376. :group 'org-table
  1377. :type '(choice
  1378. (const :tag "Always" t)
  1379. (const :tag "Never" nil)
  1380. (repeat :greedy t :tag "Individual contexts"
  1381. (cons
  1382. (choice :tag "Context"
  1383. (const headline)
  1384. (const item)
  1385. (const table)
  1386. (const default))
  1387. (boolean)))))
  1388. (defcustom org-insert-heading-respect-content nil
  1389. "Non-nil means insert new headings after the current subtree.
  1390. When nil, the new heading is created directly after the current line.
  1391. The commands \\[org-insert-heading-respect-content] and \\[org-insert-todo-heading-respect-content] turn
  1392. this variable on for the duration of the command."
  1393. :group 'org-structure
  1394. :type 'boolean)
  1395. (defcustom org-blank-before-new-entry '((heading . auto)
  1396. (plain-list-item . auto))
  1397. "Should `org-insert-heading' leave a blank line before new heading/item?
  1398. The value is an alist, with `heading' and `plain-list-item' as CAR,
  1399. and a boolean flag as CDR. The cdr may also be the symbol `auto', in
  1400. which case Org will look at the surrounding headings/items and try to
  1401. make an intelligent decision whether to insert a blank line or not."
  1402. :group 'org-edit-structure
  1403. :type '(list
  1404. (cons (const heading)
  1405. (choice (const :tag "Never" nil)
  1406. (const :tag "Always" t)
  1407. (const :tag "Auto" auto)))
  1408. (cons (const plain-list-item)
  1409. (choice (const :tag "Never" nil)
  1410. (const :tag "Always" t)
  1411. (const :tag "Auto" auto)))))
  1412. (defcustom org-insert-heading-hook nil
  1413. "Hook being run after inserting a new heading."
  1414. :group 'org-edit-structure
  1415. :type 'hook)
  1416. (defcustom org-enable-fixed-width-editor t
  1417. "Non-nil means lines starting with \":\" are treated as fixed-width.
  1418. This currently only means they are never auto-wrapped.
  1419. When nil, such lines will be treated like ordinary lines."
  1420. :group 'org-edit-structure
  1421. :type 'boolean)
  1422. (defcustom org-goto-auto-isearch t
  1423. "Non-nil means typing characters in `org-goto' starts incremental search.
  1424. When nil, you can use these keybindings to navigate the buffer:
  1425. q Quit the org-goto interface
  1426. n Go to the next visible heading
  1427. p Go to the previous visible heading
  1428. f Go one heading forward on same level
  1429. b Go one heading backward on same level
  1430. u Go one heading up"
  1431. :group 'org-edit-structure
  1432. :type 'boolean)
  1433. (defgroup org-sparse-trees nil
  1434. "Options concerning sparse trees in Org-mode."
  1435. :tag "Org Sparse Trees"
  1436. :group 'org-structure)
  1437. (defcustom org-highlight-sparse-tree-matches t
  1438. "Non-nil means highlight all matches that define a sparse tree.
  1439. The highlights will automatically disappear the next time the buffer is
  1440. changed by an edit command."
  1441. :group 'org-sparse-trees
  1442. :type 'boolean)
  1443. (defcustom org-remove-highlights-with-change t
  1444. "Non-nil means any change to the buffer will remove temporary highlights.
  1445. \\<org-mode-map>\
  1446. Such highlights are created by `org-occur' and `org-clock-display'.
  1447. When nil, `\\[org-ctrl-c-ctrl-c]' needs to be used \
  1448. to get rid of the highlights.
  1449. The highlights created by `org-toggle-latex-fragment' always need
  1450. `\\[org-toggle-latex-fragment]' to be removed."
  1451. :group 'org-sparse-trees
  1452. :group 'org-time
  1453. :type 'boolean)
  1454. (defcustom org-occur-hook '(org-first-headline-recenter)
  1455. "Hook that is run after `org-occur' has constructed a sparse tree.
  1456. This can be used to recenter the window to show as much of the structure
  1457. as possible."
  1458. :group 'org-sparse-trees
  1459. :type 'hook)
  1460. (defgroup org-imenu-and-speedbar nil
  1461. "Options concerning imenu and speedbar in Org-mode."
  1462. :tag "Org Imenu and Speedbar"
  1463. :group 'org-structure)
  1464. (defcustom org-imenu-depth 2
  1465. "The maximum level for Imenu access to Org-mode headlines.
  1466. This also applied for speedbar access."
  1467. :group 'org-imenu-and-speedbar
  1468. :type 'integer)
  1469. (defgroup org-table nil
  1470. "Options concerning tables in Org-mode."
  1471. :tag "Org Table"
  1472. :group 'org)
  1473. (defcustom org-enable-table-editor 'optimized
  1474. "Non-nil means lines starting with \"|\" are handled by the table editor.
  1475. When nil, such lines will be treated like ordinary lines.
  1476. When equal to the symbol `optimized', the table editor will be optimized to
  1477. do the following:
  1478. - Automatic overwrite mode in front of whitespace in table fields.
  1479. This makes the structure of the table stay in tact as long as the edited
  1480. field does not exceed the column width.
  1481. - Minimize the number of realigns. Normally, the table is aligned each time
  1482. TAB or RET are pressed to move to another field. With optimization this
  1483. happens only if changes to a field might have changed the column width.
  1484. Optimization requires replacing the functions `self-insert-command',
  1485. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  1486. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  1487. very good at guessing when a re-align will be necessary, but you can always
  1488. force one with \\[org-ctrl-c-ctrl-c].
  1489. If you would like to use the optimized version in Org-mode, but the
  1490. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  1491. This variable can be used to turn on and off the table editor during a session,
  1492. but in order to toggle optimization, a restart is required.
  1493. See also the variable `org-table-auto-blank-field'."
  1494. :group 'org-table
  1495. :type '(choice
  1496. (const :tag "off" nil)
  1497. (const :tag "on" t)
  1498. (const :tag "on, optimized" optimized)))
  1499. (defcustom org-self-insert-cluster-for-undo (or (featurep 'xemacs)
  1500. (version<= emacs-version "24.1"))
  1501. "Non-nil means cluster self-insert commands for undo when possible.
  1502. If this is set, then, like in the Emacs command loop, 20 consecutive
  1503. characters will be undone together.
  1504. This is configurable, because there is some impact on typing performance."
  1505. :group 'org-table
  1506. :type 'boolean)
  1507. (defcustom org-table-tab-recognizes-table.el t
  1508. "Non-nil means TAB will automatically notice a table.el table.
  1509. When it sees such a table, it moves point into it and - if necessary -
  1510. calls `table-recognize-table'."
  1511. :group 'org-table-editing
  1512. :type 'boolean)
  1513. (defgroup org-link nil
  1514. "Options concerning links in Org-mode."
  1515. :tag "Org Link"
  1516. :group 'org)
  1517. (defvar-local org-link-abbrev-alist-local nil
  1518. "Buffer-local version of `org-link-abbrev-alist', which see.
  1519. The value of this is taken from the #+LINK lines.")
  1520. (defcustom org-link-abbrev-alist nil
  1521. "Alist of link abbreviations.
  1522. The car of each element is a string, to be replaced at the start of a link.
  1523. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  1524. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  1525. [[linkkey:tag][description]]
  1526. The `linkkey' must be a word word, starting with a letter, followed
  1527. by letters, numbers, `-' or `_'.
  1528. If REPLACE is a string, the tag will simply be appended to create the link.
  1529. If the string contains \"%s\", the tag will be inserted there. If the string
  1530. contains \"%h\", it will cause a url-encoded version of the tag to be inserted
  1531. at that point (see the function `url-hexify-string'). If the string contains
  1532. the specifier \"%(my-function)\", then the custom function `my-function' will
  1533. be invoked: this function takes the tag as its only argument and must return
  1534. a string.
  1535. REPLACE may also be a function that will be called with the tag as the
  1536. only argument to create the link, which should be returned as a string.
  1537. See the manual for examples."
  1538. :group 'org-link
  1539. :type '(repeat
  1540. (cons
  1541. (string :tag "Protocol")
  1542. (choice
  1543. (string :tag "Format")
  1544. (function)))))
  1545. (defcustom org-descriptive-links t
  1546. "Non-nil means Org will display descriptive links.
  1547. E.g. [[http://orgmode.org][Org website]] will be displayed as
  1548. \"Org Website\", hiding the link itself and just displaying its
  1549. description. When set to nil, Org will display the full links
  1550. literally.
  1551. You can interactively set the value of this variable by calling
  1552. `org-toggle-link-display' or from the menu Org>Hyperlinks menu."
  1553. :group 'org-link
  1554. :type 'boolean)
  1555. (defcustom org-link-file-path-type 'adaptive
  1556. "How the path name in file links should be stored.
  1557. Valid values are:
  1558. relative Relative to the current directory, i.e. the directory of the file
  1559. into which the link is being inserted.
  1560. absolute Absolute path, if possible with ~ for home directory.
  1561. noabbrev Absolute path, no abbreviation of home directory.
  1562. adaptive Use relative path for files in the current directory and sub-
  1563. directories of it. For other files, use an absolute path."
  1564. :group 'org-link
  1565. :type '(choice
  1566. (const relative)
  1567. (const absolute)
  1568. (const noabbrev)
  1569. (const adaptive)))
  1570. (defvaralias 'org-activate-links 'org-highlight-links)
  1571. (defcustom org-highlight-links '(bracket angle plain radio tag date footnote)
  1572. "Types of links that should be highlighted in Org-mode files.
  1573. This is a list of symbols, each one of them leading to the
  1574. highlighting of a certain link type.
  1575. You can still open links that are not highlighted.
  1576. In principle, it does not hurt to turn on highlighting for all
  1577. link types. There may be a small gain when turning off unused
  1578. link types. The types are:
  1579. bracket The recommended [[link][description]] or [[link]] links with hiding.
  1580. angle Links in angular brackets that may contain whitespace like
  1581. <bbdb:Carsten Dominik>.
  1582. plain Plain links in normal text, no whitespace, like http://google.com.
  1583. radio Text that is matched by a radio target, see manual for details.
  1584. tag Tag settings in a headline (link to tag search).
  1585. date Time stamps (link to calendar).
  1586. footnote Footnote labels.
  1587. If you set this variable during an Emacs session, use `org-mode-restart'
  1588. in the Org buffer so that the change takes effect."
  1589. :group 'org-link
  1590. :group 'org-appearance
  1591. :type '(set :greedy t
  1592. (const :tag "Double bracket links" bracket)
  1593. (const :tag "Angular bracket links" angle)
  1594. (const :tag "Plain text links" plain)
  1595. (const :tag "Radio target matches" radio)
  1596. (const :tag "Tags" tag)
  1597. (const :tag "Timestamps" date)
  1598. (const :tag "Footnotes" footnote)))
  1599. (defcustom org-make-link-description-function nil
  1600. "Function to use for generating link descriptions from links.
  1601. When nil, the link location will be used. This function must take
  1602. two parameters: the first one is the link, the second one is the
  1603. description generated by `org-insert-link'. The function should
  1604. return the description to use."
  1605. :group 'org-link
  1606. :type '(choice (const nil) (function)))
  1607. (defgroup org-link-store nil
  1608. "Options concerning storing links in Org-mode."
  1609. :tag "Org Store Link"
  1610. :group 'org-link)
  1611. (defcustom org-url-hexify-p t
  1612. "When non-nil, hexify URL when creating a link."
  1613. :type 'boolean
  1614. :version "24.3"
  1615. :group 'org-link-store)
  1616. (defcustom org-email-link-description-format "Email %c: %.30s"
  1617. "Format of the description part of a link to an email or usenet message.
  1618. The following %-escapes will be replaced by corresponding information:
  1619. %F full \"From\" field
  1620. %f name, taken from \"From\" field, address if no name
  1621. %T full \"To\" field
  1622. %t first name in \"To\" field, address if no name
  1623. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  1624. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  1625. %s subject
  1626. %d date
  1627. %m message-id.
  1628. You may use normal field width specification between the % and the letter.
  1629. This is for example useful to limit the length of the subject.
  1630. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  1631. :group 'org-link-store
  1632. :type 'string)
  1633. (defcustom org-from-is-user-regexp
  1634. (let (r1 r2)
  1635. (when (and user-mail-address (not (string= user-mail-address "")))
  1636. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  1637. (when (and user-full-name (not (string= user-full-name "")))
  1638. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  1639. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  1640. "Regexp matched against the \"From:\" header of an email or usenet message.
  1641. It should match if the message is from the user him/herself."
  1642. :group 'org-link-store
  1643. :type 'regexp)
  1644. (defcustom org-context-in-file-links t
  1645. "Non-nil means file links from `org-store-link' contain context.
  1646. A search string will be added to the file name with :: as separator and
  1647. used to find the context when the link is activated by the command
  1648. `org-open-at-point'. When this option is t, the entire active region
  1649. will be placed in the search string of the file link. If set to a
  1650. positive integer, only the first n lines of context will be stored.
  1651. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1652. negates this setting for the duration of the command."
  1653. :group 'org-link-store
  1654. :type '(choice boolean integer))
  1655. (defcustom org-keep-stored-link-after-insertion nil
  1656. "Non-nil means keep link in list for entire session.
  1657. The command `org-store-link' adds a link pointing to the current
  1658. location to an internal list. These links accumulate during a session.
  1659. The command `org-insert-link' can be used to insert links into any
  1660. Org-mode file (offering completion for all stored links). When this
  1661. option is nil, every link which has been inserted once using \\[org-insert-link]
  1662. will be removed from the list, to make completing the unused links
  1663. more efficient."
  1664. :group 'org-link-store
  1665. :type 'boolean)
  1666. (defgroup org-link-follow nil
  1667. "Options concerning following links in Org-mode."
  1668. :tag "Org Follow Link"
  1669. :group 'org-link)
  1670. (defcustom org-link-translation-function nil
  1671. "Function to translate links with different syntax to Org syntax.
  1672. This can be used to translate links created for example by the Planner
  1673. or emacs-wiki packages to Org syntax.
  1674. The function must accept two parameters, a TYPE containing the link
  1675. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  1676. which is everything after the link protocol. It should return a cons
  1677. with possibly modified values of type and path.
  1678. Org contains a function for this, so if you set this variable to
  1679. `org-translate-link-from-planner', you should be able follow many
  1680. links created by planner."
  1681. :group 'org-link-follow
  1682. :type '(choice (const nil) (function)))
  1683. (defcustom org-follow-link-hook nil
  1684. "Hook that is run after a link has been followed."
  1685. :group 'org-link-follow
  1686. :type 'hook)
  1687. (defcustom org-tab-follows-link nil
  1688. "Non-nil means on links TAB will follow the link.
  1689. Needs to be set before org.el is loaded.
  1690. This really should not be used, it does not make sense, and the
  1691. implementation is bad."
  1692. :group 'org-link-follow
  1693. :type 'boolean)
  1694. (defcustom org-return-follows-link nil
  1695. "Non-nil means on links RET will follow the link.
  1696. In tables, the special behavior of RET has precedence."
  1697. :group 'org-link-follow
  1698. :type 'boolean)
  1699. (defcustom org-mouse-1-follows-link
  1700. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1701. "Non-nil means mouse-1 on a link will follow the link.
  1702. A longer mouse click will still set point. Does not work on XEmacs.
  1703. Needs to be set before org.el is loaded."
  1704. :group 'org-link-follow
  1705. :version "24.4"
  1706. :package-version '(Org . "8.3")
  1707. :type '(choice
  1708. (const :tag "A double click follows the link" double)
  1709. (const :tag "Unconditionally follow the link with mouse-1" t)
  1710. (integer :tag "mouse-1 click does not follow the link if longer than N ms" 450)))
  1711. (defcustom org-mark-ring-length 4
  1712. "Number of different positions to be recorded in the ring.
  1713. Changing this requires a restart of Emacs to work correctly."
  1714. :group 'org-link-follow
  1715. :type 'integer)
  1716. (defcustom org-link-search-must-match-exact-headline 'query-to-create
  1717. "Non-nil means internal links in Org files must exactly match a headline.
  1718. When nil, the link search tries to match a phrase with all words
  1719. in the search text."
  1720. :group 'org-link-follow
  1721. :version "24.1"
  1722. :type '(choice
  1723. (const :tag "Use fuzzy text search" nil)
  1724. (const :tag "Match only exact headline" t)
  1725. (const :tag "Match exact headline or query to create it"
  1726. query-to-create)))
  1727. (defcustom org-link-frame-setup
  1728. '((vm . vm-visit-folder-other-frame)
  1729. (vm-imap . vm-visit-imap-folder-other-frame)
  1730. (gnus . org-gnus-no-new-news)
  1731. (file . find-file-other-window)
  1732. (wl . wl-other-frame))
  1733. "Setup the frame configuration for following links.
  1734. When following a link with Emacs, it may often be useful to display
  1735. this link in another window or frame. This variable can be used to
  1736. set this up for the different types of links.
  1737. For VM, use any of
  1738. `vm-visit-folder'
  1739. `vm-visit-folder-other-window'
  1740. `vm-visit-folder-other-frame'
  1741. For Gnus, use any of
  1742. `gnus'
  1743. `gnus-other-frame'
  1744. `org-gnus-no-new-news'
  1745. For FILE, use any of
  1746. `find-file'
  1747. `find-file-other-window'
  1748. `find-file-other-frame'
  1749. For Wanderlust use any of
  1750. `wl'
  1751. `wl-other-frame'
  1752. For the calendar, use the variable `calendar-setup'.
  1753. For BBDB, it is currently only possible to display the matches in
  1754. another window."
  1755. :group 'org-link-follow
  1756. :type '(list
  1757. (cons (const vm)
  1758. (choice
  1759. (const vm-visit-folder)
  1760. (const vm-visit-folder-other-window)
  1761. (const vm-visit-folder-other-frame)))
  1762. (cons (const vm-imap)
  1763. (choice
  1764. (const vm-visit-imap-folder)
  1765. (const vm-visit-imap-folder-other-window)
  1766. (const vm-visit-imap-folder-other-frame)))
  1767. (cons (const gnus)
  1768. (choice
  1769. (const gnus)
  1770. (const gnus-other-frame)
  1771. (const org-gnus-no-new-news)))
  1772. (cons (const file)
  1773. (choice
  1774. (const find-file)
  1775. (const find-file-other-window)
  1776. (const find-file-other-frame)))
  1777. (cons (const wl)
  1778. (choice
  1779. (const wl)
  1780. (const wl-other-frame)))))
  1781. (defcustom org-display-internal-link-with-indirect-buffer nil
  1782. "Non-nil means use indirect buffer to display infile links.
  1783. Activating internal links (from one location in a file to another location
  1784. in the same file) normally just jumps to the location. When the link is
  1785. activated with a \\[universal-argument] prefix (or with mouse-3), the link \
  1786. is displayed in
  1787. another window. When this option is set, the other window actually displays
  1788. an indirect buffer clone of the current buffer, to avoid any visibility
  1789. changes to the current buffer."
  1790. :group 'org-link-follow
  1791. :type 'boolean)
  1792. (defcustom org-open-non-existing-files nil
  1793. "Non-nil means `org-open-file' will open non-existing files.
  1794. When nil, an error will be generated.
  1795. This variable applies only to external applications because they
  1796. might choke on non-existing files. If the link is to a file that
  1797. will be opened in Emacs, the variable is ignored."
  1798. :group 'org-link-follow
  1799. :type 'boolean)
  1800. (defcustom org-open-directory-means-index-dot-org nil
  1801. "Non-nil means a link to a directory really means to index.org.
  1802. When nil, following a directory link will run dired or open a finder/explorer
  1803. window on that directory."
  1804. :group 'org-link-follow
  1805. :type 'boolean)
  1806. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1807. "Non-nil means ask for confirmation before executing shell links.
  1808. Shell links can be dangerous: just think about a link
  1809. [[shell:rm -rf ~/*][Google Search]]
  1810. This link would show up in your Org-mode document as \"Google Search\",
  1811. but really it would remove your entire home directory.
  1812. Therefore we advise against setting this variable to nil.
  1813. Just change it to `y-or-n-p' if you want to confirm with a
  1814. single keystroke rather than having to type \"yes\"."
  1815. :group 'org-link-follow
  1816. :type '(choice
  1817. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1818. (const :tag "with y-or-n (faster)" y-or-n-p)
  1819. (const :tag "no confirmation (dangerous)" nil)))
  1820. (put 'org-confirm-shell-link-function
  1821. 'safe-local-variable
  1822. (lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1823. (defcustom org-confirm-shell-link-not-regexp ""
  1824. "A regexp to skip confirmation for shell links."
  1825. :group 'org-link-follow
  1826. :version "24.1"
  1827. :type 'regexp)
  1828. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1829. "Non-nil means ask for confirmation before executing Emacs Lisp links.
  1830. Elisp links can be dangerous: just think about a link
  1831. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1832. This link would show up in your Org-mode document as \"Google Search\",
  1833. but really it would remove your entire home directory.
  1834. Therefore we advise against setting this variable to nil.
  1835. Just change it to `y-or-n-p' if you want to confirm with a
  1836. single keystroke rather than having to type \"yes\"."
  1837. :group 'org-link-follow
  1838. :type '(choice
  1839. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1840. (const :tag "with y-or-n (faster)" y-or-n-p)
  1841. (const :tag "no confirmation (dangerous)" nil)))
  1842. (put 'org-confirm-shell-link-function
  1843. 'safe-local-variable
  1844. (lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1845. (defcustom org-confirm-elisp-link-not-regexp ""
  1846. "A regexp to skip confirmation for Elisp links."
  1847. :group 'org-link-follow
  1848. :version "24.1"
  1849. :type 'regexp)
  1850. (defconst org-file-apps-defaults-gnu
  1851. '((remote . emacs)
  1852. (system . mailcap)
  1853. (t . mailcap))
  1854. "Default file applications on a UNIX or GNU/Linux system.
  1855. See `org-file-apps'.")
  1856. (defconst org-file-apps-defaults-macosx
  1857. '((remote . emacs)
  1858. (t . "open %s")
  1859. (system . "open %s")
  1860. ("ps.gz" . "gv %s")
  1861. ("eps.gz" . "gv %s")
  1862. ("dvi" . "xdvi %s")
  1863. ("fig" . "xfig %s"))
  1864. "Default file applications on a MacOS X system.
  1865. The system \"open\" is known as a default, but we use X11 applications
  1866. for some files for which the OS does not have a good default.
  1867. See `org-file-apps'.")
  1868. (defconst org-file-apps-defaults-windowsnt
  1869. (list
  1870. '(remote . emacs)
  1871. (cons t
  1872. (list (if (featurep 'xemacs)
  1873. 'mswindows-shell-execute
  1874. 'w32-shell-execute)
  1875. "open" 'file))
  1876. (cons 'system
  1877. (list (if (featurep 'xemacs)
  1878. 'mswindows-shell-execute
  1879. 'w32-shell-execute)
  1880. "open" 'file)))
  1881. "Default file applications on a Windows NT system.
  1882. The system \"open\" is used for most files.
  1883. See `org-file-apps'.")
  1884. (defcustom org-file-apps
  1885. '((auto-mode . emacs)
  1886. ("\\.mm\\'" . default)
  1887. ("\\.x?html?\\'" . default)
  1888. ("\\.pdf\\'" . default))
  1889. "External applications for opening `file:path' items in a document.
  1890. Org-mode uses system defaults for different file types, but
  1891. you can use this variable to set the application for a given file
  1892. extension. The entries in this list are cons cells where the car identifies
  1893. files and the cdr the corresponding command. Possible values for the
  1894. file identifier are
  1895. \"string\" A string as a file identifier can be interpreted in different
  1896. ways, depending on its contents:
  1897. - Alphanumeric characters only:
  1898. Match links with this file extension.
  1899. Example: (\"pdf\" . \"evince %s\")
  1900. to open PDFs with evince.
  1901. - Regular expression: Match links where the
  1902. filename matches the regexp. If you want to
  1903. use groups here, use shy groups.
  1904. Example: (\"\\.x?html\\\\='\" . \"firefox %s\")
  1905. (\"\\(?:xhtml\\|html\\)\" . \"firefox %s\")
  1906. to open *.html and *.xhtml with firefox.
  1907. - Regular expression which contains (non-shy) groups:
  1908. Match links where the whole link, including \"::\", and
  1909. anything after that, matches the regexp.
  1910. In a custom command string, %1, %2, etc. are replaced with
  1911. the parts of the link that were matched by the groups.
  1912. For backwards compatibility, if a command string is given
  1913. that does not use any of the group matches, this case is
  1914. handled identically to the second one (i.e. match against
  1915. file name only).
  1916. In a custom lisp form, you can access the group matches with
  1917. (match-string n link).
  1918. Example: (\"\\.pdf::\\(\\d+\\)\\\\='\" . \"evince -p %1 %s\")
  1919. to open [[file:document.pdf::5]] with evince at page 5.
  1920. `directory' Matches a directory
  1921. `remote' Matches a remote file, accessible through tramp or efs.
  1922. Remote files most likely should be visited through Emacs
  1923. because external applications cannot handle such paths.
  1924. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1925. so all files Emacs knows how to handle. Using this with
  1926. command `emacs' will open most files in Emacs. Beware that this
  1927. will also open html files inside Emacs, unless you add
  1928. (\"html\" . default) to the list as well.
  1929. t Default for files not matched by any of the other options.
  1930. `system' The system command to open files, like `open' on Windows
  1931. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1932. that will be selected if you call `C-c C-o' with a double
  1933. \\[universal-argument] \\[universal-argument] prefix.
  1934. Possible values for the command are:
  1935. `emacs' The file will be visited by the current Emacs process.
  1936. `default' Use the default application for this file type, which is the
  1937. association for t in the list, most likely in the system-specific
  1938. part.
  1939. This can be used to overrule an unwanted setting in the
  1940. system-specific variable.
  1941. `system' Use the system command for opening files, like \"open\".
  1942. This command is specified by the entry whose car is `system'.
  1943. Most likely, the system-specific version of this variable
  1944. does define this command, but you can overrule/replace it
  1945. here.
  1946. string A command to be executed by a shell; %s will be replaced
  1947. by the path to the file.
  1948. sexp A Lisp form which will be evaluated. The file path will
  1949. be available in the Lisp variable `file'.
  1950. For more examples, see the system specific constants
  1951. `org-file-apps-defaults-macosx'
  1952. `org-file-apps-defaults-windowsnt'
  1953. `org-file-apps-defaults-gnu'."
  1954. :group 'org-link-follow
  1955. :type '(repeat
  1956. (cons (choice :value ""
  1957. (string :tag "Extension")
  1958. (const :tag "System command to open files" system)
  1959. (const :tag "Default for unrecognized files" t)
  1960. (const :tag "Remote file" remote)
  1961. (const :tag "Links to a directory" directory)
  1962. (const :tag "Any files that have Emacs modes"
  1963. auto-mode))
  1964. (choice :value ""
  1965. (const :tag "Visit with Emacs" emacs)
  1966. (const :tag "Use default" default)
  1967. (const :tag "Use the system command" system)
  1968. (string :tag "Command")
  1969. (sexp :tag "Lisp form")))))
  1970. (defcustom org-doi-server-url "http://dx.doi.org/"
  1971. "The URL of the DOI server."
  1972. :type 'string
  1973. :version "24.3"
  1974. :group 'org-link-follow)
  1975. (defgroup org-refile nil
  1976. "Options concerning refiling entries in Org-mode."
  1977. :tag "Org Refile"
  1978. :group 'org)
  1979. (defcustom org-directory "~/org"
  1980. "Directory with org files.
  1981. This is just a default location to look for Org files. There is no need
  1982. at all to put your files into this directory. It is only used in the
  1983. following situations:
  1984. 1. When a capture template specifies a target file that is not an
  1985. absolute path. The path will then be interpreted relative to
  1986. `org-directory'
  1987. 2. When a capture note is filed away in an interactive way (when exiting the
  1988. note buffer with `C-1 C-c C-c'. The user is prompted for an org file,
  1989. with `org-directory' as the default path."
  1990. :group 'org-refile
  1991. :group 'org-capture
  1992. :type 'directory)
  1993. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1994. "Default target for storing notes.
  1995. Used as a fall back file for org-capture.el, for templates that
  1996. do not specify a target file."
  1997. :group 'org-refile
  1998. :group 'org-capture
  1999. :type 'file)
  2000. (defcustom org-goto-interface 'outline
  2001. "The default interface to be used for `org-goto'.
  2002. Allowed values are:
  2003. outline The interface shows an outline of the relevant file
  2004. and the correct heading is found by moving through
  2005. the outline or by searching with incremental search.
  2006. outline-path-completion Headlines in the current buffer are offered via
  2007. completion. This is the interface also used by
  2008. the refile command."
  2009. :group 'org-refile
  2010. :type '(choice
  2011. (const :tag "Outline" outline)
  2012. (const :tag "Outline-path-completion" outline-path-completion)))
  2013. (defcustom org-goto-max-level 5
  2014. "Maximum target level when running `org-goto' with refile interface."
  2015. :group 'org-refile
  2016. :type 'integer)
  2017. (defcustom org-reverse-note-order nil
  2018. "Non-nil means store new notes at the beginning of a file or entry.
  2019. When nil, new notes will be filed to the end of a file or entry.
  2020. This can also be a list with cons cells of regular expressions that
  2021. are matched against file names, and values."
  2022. :group 'org-capture
  2023. :group 'org-refile
  2024. :type '(choice
  2025. (const :tag "Reverse always" t)
  2026. (const :tag "Reverse never" nil)
  2027. (repeat :tag "By file name regexp"
  2028. (cons regexp boolean))))
  2029. (defcustom org-log-refile nil
  2030. "Information to record when a task is refiled.
  2031. Possible values are:
  2032. nil Don't add anything
  2033. time Add a time stamp to the task
  2034. note Prompt for a note and add it with template `org-log-note-headings'
  2035. This option can also be set with on a per-file-basis with
  2036. #+STARTUP: nologrefile
  2037. #+STARTUP: logrefile
  2038. #+STARTUP: lognoterefile
  2039. You can have local logging settings for a subtree by setting the LOGGING
  2040. property to one or more of these keywords.
  2041. When bulk-refiling from the agenda, the value `note' is forbidden and
  2042. will temporarily be changed to `time'."
  2043. :group 'org-refile
  2044. :group 'org-progress
  2045. :version "24.1"
  2046. :type '(choice
  2047. (const :tag "No logging" nil)
  2048. (const :tag "Record timestamp" time)
  2049. (const :tag "Record timestamp with note." note)))
  2050. (defcustom org-refile-targets nil
  2051. "Targets for refiling entries with \\[org-refile].
  2052. This is a list of cons cells. Each cell contains:
  2053. - a specification of the files to be considered, either a list of files,
  2054. or a symbol whose function or variable value will be used to retrieve
  2055. a file name or a list of file names. If you use `org-agenda-files' for
  2056. that, all agenda files will be scanned for targets. Nil means consider
  2057. headings in the current buffer.
  2058. - A specification of how to find candidate refile targets. This may be
  2059. any of:
  2060. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  2061. This tag has to be present in all target headlines, inheritance will
  2062. not be considered.
  2063. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  2064. todo keyword.
  2065. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  2066. headlines that are refiling targets.
  2067. - a cons cell (:level . N). Any headline of level N is considered a target.
  2068. Note that, when `org-odd-levels-only' is set, level corresponds to
  2069. order in hierarchy, not to the number of stars.
  2070. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  2071. Note that, when `org-odd-levels-only' is set, level corresponds to
  2072. order in hierarchy, not to the number of stars.
  2073. Each element of this list generates a set of possible targets.
  2074. The union of these sets is presented (with completion) to
  2075. the user by `org-refile'.
  2076. You can set the variable `org-refile-target-verify-function' to a function
  2077. to verify each headline found by the simple criteria above.
  2078. When this variable is nil, all top-level headlines in the current buffer
  2079. are used, equivalent to the value `((nil . (:level . 1))'."
  2080. :group 'org-refile
  2081. :type '(repeat
  2082. (cons
  2083. (choice :value org-agenda-files
  2084. (const :tag "All agenda files" org-agenda-files)
  2085. (const :tag "Current buffer" nil)
  2086. (function) (variable) (file))
  2087. (choice :tag "Identify target headline by"
  2088. (cons :tag "Specific tag" (const :value :tag) (string))
  2089. (cons :tag "TODO keyword" (const :value :todo) (string))
  2090. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  2091. (cons :tag "Level number" (const :value :level) (integer))
  2092. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  2093. (defcustom org-refile-target-verify-function nil
  2094. "Function to verify if the headline at point should be a refile target.
  2095. The function will be called without arguments, with point at the
  2096. beginning of the headline. It should return t and leave point
  2097. where it is if the headline is a valid target for refiling.
  2098. If the target should not be selected, the function must return nil.
  2099. In addition to this, it may move point to a place from where the search
  2100. should be continued. For example, the function may decide that the entire
  2101. subtree of the current entry should be excluded and move point to the end
  2102. of the subtree."
  2103. :group 'org-refile
  2104. :type '(choice
  2105. (const nil)
  2106. (function)))
  2107. (defcustom org-refile-use-cache nil
  2108. "Non-nil means cache refile targets to speed up the process.
  2109. \\<org-mode-map>\
  2110. The cache for a particular file will be updated automatically when
  2111. the buffer has been killed, or when any of the marker used for flagging
  2112. refile targets no longer points at a live buffer.
  2113. If you have added new entries to a buffer that might themselves be targets,
  2114. you need to clear the cache manually by pressing `C-0 \\[org-refile]' or,
  2115. if you find that easier, \
  2116. `\\[universal-argument] \\[universal-argument] \\[universal-argument] \
  2117. \\[org-refile]'."
  2118. :group 'org-refile
  2119. :version "24.1"
  2120. :type 'boolean)
  2121. (defcustom org-refile-use-outline-path nil
  2122. "Non-nil means provide refile targets as paths.
  2123. So a level 3 headline will be available as level1/level2/level3.
  2124. When the value is `file', also include the file name (without directory)
  2125. into the path. In this case, you can also stop the completion after
  2126. the file name, to get entries inserted as top level in the file.
  2127. When `full-file-path', include the full file path."
  2128. :group 'org-refile
  2129. :type '(choice
  2130. (const :tag "Not" nil)
  2131. (const :tag "Yes" t)
  2132. (const :tag "Start with file name" file)
  2133. (const :tag "Start with full file path" full-file-path)))
  2134. (defcustom org-outline-path-complete-in-steps t
  2135. "Non-nil means complete the outline path in hierarchical steps.
  2136. When Org-mode uses the refile interface to select an outline path
  2137. \(see variable `org-refile-use-outline-path'), the completion of
  2138. the path can be done in a single go, or it can be done in steps down
  2139. the headline hierarchy. Going in steps is probably the best if you
  2140. do not use a special completion package like `ido' or `icicles'.
  2141. However, when using these packages, going in one step can be very
  2142. fast, while still showing the whole path to the entry."
  2143. :group 'org-refile
  2144. :type 'boolean)
  2145. (defcustom org-refile-allow-creating-parent-nodes nil
  2146. "Non-nil means allow to create new nodes as refile targets.
  2147. New nodes are then created by adding \"/new node name\" to the completion
  2148. of an existing node. When the value of this variable is `confirm',
  2149. new node creation must be confirmed by the user (recommended).
  2150. When nil, the completion must match an existing entry.
  2151. Note that, if the new heading is not seen by the criteria
  2152. listed in `org-refile-targets', multiple instances of the same
  2153. heading would be created by trying again to file under the new
  2154. heading."
  2155. :group 'org-refile
  2156. :type '(choice
  2157. (const :tag "Never" nil)
  2158. (const :tag "Always" t)
  2159. (const :tag "Prompt for confirmation" confirm)))
  2160. (defcustom org-refile-active-region-within-subtree nil
  2161. "Non-nil means also refile active region within a subtree.
  2162. By default `org-refile' doesn't allow refiling regions if they
  2163. don't contain a set of subtrees, but it might be convenient to
  2164. do so sometimes: in that case, the first line of the region is
  2165. converted to a headline before refiling."
  2166. :group 'org-refile
  2167. :version "24.1"
  2168. :type 'boolean)
  2169. (defgroup org-todo nil
  2170. "Options concerning TODO items in Org-mode."
  2171. :tag "Org TODO"
  2172. :group 'org)
  2173. (defgroup org-progress nil
  2174. "Options concerning Progress logging in Org-mode."
  2175. :tag "Org Progress"
  2176. :group 'org-time)
  2177. (defvar org-todo-interpretation-widgets
  2178. '((:tag "Sequence (cycling hits every state)" sequence)
  2179. (:tag "Type (cycling directly to DONE)" type))
  2180. "The available interpretation symbols for customizing `org-todo-keywords'.
  2181. Interested libraries should add to this list.")
  2182. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  2183. "List of TODO entry keyword sequences and their interpretation.
  2184. \\<org-mode-map>This is a list of sequences.
  2185. Each sequence starts with a symbol, either `sequence' or `type',
  2186. indicating if the keywords should be interpreted as a sequence of
  2187. action steps, or as different types of TODO items. The first
  2188. keywords are states requiring action - these states will select a headline
  2189. for inclusion into the global TODO list Org-mode produces. If one of
  2190. the \"keywords\" is the vertical bar, \"|\", the remaining keywords
  2191. signify that no further action is necessary. If \"|\" is not found,
  2192. the last keyword is treated as the only DONE state of the sequence.
  2193. The command \\[org-todo] cycles an entry through these states, and one
  2194. additional state where no keyword is present. For details about this
  2195. cycling, see the manual.
  2196. TODO keywords and interpretation can also be set on a per-file basis with
  2197. the special #+SEQ_TODO and #+TYP_TODO lines.
  2198. Each keyword can optionally specify a character for fast state selection
  2199. \(in combination with the variable `org-use-fast-todo-selection')
  2200. and specifiers for state change logging, using the same syntax that
  2201. is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says that
  2202. the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  2203. indicates to record a time stamp each time this state is selected.
  2204. Each keyword may also specify if a timestamp or a note should be
  2205. recorded when entering or leaving the state, by adding additional
  2206. characters in the parenthesis after the keyword. This looks like this:
  2207. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  2208. record only the time of the state change. With X and Y being either
  2209. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  2210. Y when leaving the state if and only if the *target* state does not
  2211. define X. You may omit any of the fast-selection key or X or /Y,
  2212. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  2213. For backward compatibility, this variable may also be just a list
  2214. of keywords. In this case the interpretation (sequence or type) will be
  2215. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  2216. :group 'org-todo
  2217. :group 'org-keywords
  2218. :type '(choice
  2219. (repeat :tag "Old syntax, just keywords"
  2220. (string :tag "Keyword"))
  2221. (repeat :tag "New syntax"
  2222. (cons
  2223. (choice
  2224. :tag "Interpretation"
  2225. ;;Quick and dirty way to see
  2226. ;;`org-todo-interpretations'. This takes the
  2227. ;;place of item arguments
  2228. :convert-widget
  2229. (lambda (widget)
  2230. (widget-put widget
  2231. :args (mapcar
  2232. (lambda (x)
  2233. (widget-convert
  2234. (cons 'const x)))
  2235. org-todo-interpretation-widgets))
  2236. widget))
  2237. (repeat
  2238. (string :tag "Keyword"))))))
  2239. (defvar-local org-todo-keywords-1 nil
  2240. "All TODO and DONE keywords active in a buffer.")
  2241. (defvar org-todo-keywords-for-agenda nil)
  2242. (defvar org-done-keywords-for-agenda nil)
  2243. (defvar org-todo-keyword-alist-for-agenda nil)
  2244. (defvar org-tag-alist-for-agenda nil
  2245. "Alist of all tags from all agenda files.")
  2246. (defvar org-tag-groups-alist-for-agenda nil
  2247. "Alist of all groups tags from all current agenda files.")
  2248. (defvar-local org-tag-groups-alist nil)
  2249. (defvar org-agenda-contributing-files nil)
  2250. (defvar-local org-not-done-keywords nil)
  2251. (defvar-local org-done-keywords nil)
  2252. (defvar-local org-todo-heads nil)
  2253. (defvar-local org-todo-sets nil)
  2254. (defvar-local org-todo-log-states nil)
  2255. (defvar-local org-todo-kwd-alist nil)
  2256. (defvar-local org-todo-key-alist nil)
  2257. (defvar-local org-todo-key-trigger nil)
  2258. (defcustom org-todo-interpretation 'sequence
  2259. "Controls how TODO keywords are interpreted.
  2260. This variable is in principle obsolete and is only used for
  2261. backward compatibility, if the interpretation of todo keywords is
  2262. not given already in `org-todo-keywords'. See that variable for
  2263. more information."
  2264. :group 'org-todo
  2265. :group 'org-keywords
  2266. :type '(choice (const sequence)
  2267. (const type)))
  2268. (defcustom org-use-fast-todo-selection t
  2269. "\\<org-mode-map>\
  2270. Non-nil means use the fast todo selection scheme with \\[org-todo].
  2271. This variable describes if and under what circumstances the cycling
  2272. mechanism for TODO keywords will be replaced by a single-key, direct
  2273. selection scheme.
  2274. When nil, fast selection is never used.
  2275. When the symbol `prefix', it will be used when `org-todo' is called
  2276. with a prefix argument, i.e. `\\[universal-argument] \\[org-todo]' \
  2277. in an Org-mode buffer, and
  2278. `\\[universal-argument] t' in an agenda buffer.
  2279. When t, fast selection is used by default. In this case, the prefix
  2280. argument forces cycling instead.
  2281. In all cases, the special interface is only used if access keys have
  2282. actually been assigned by the user, i.e. if keywords in the configuration
  2283. are followed by a letter in parenthesis, like TODO(t)."
  2284. :group 'org-todo
  2285. :type '(choice
  2286. (const :tag "Never" nil)
  2287. (const :tag "By default" t)
  2288. (const :tag "Only with C-u C-c C-t" prefix)))
  2289. (defcustom org-provide-todo-statistics t
  2290. "Non-nil means update todo statistics after insert and toggle.
  2291. ALL-HEADLINES means update todo statistics by including headlines
  2292. with no TODO keyword as well, counting them as not done.
  2293. A list of TODO keywords means the same, but skip keywords that are
  2294. not in this list.
  2295. When set to a list of two lists, the first list contains keywords
  2296. to consider as TODO keywords, the second list contains keywords
  2297. to consider as DONE keywords.
  2298. When this is set, todo statistics is updated in the parent of the
  2299. current entry each time a todo state is changed."
  2300. :group 'org-todo
  2301. :type '(choice
  2302. (const :tag "Yes, only for TODO entries" t)
  2303. (const :tag "Yes, including all entries" all-headlines)
  2304. (repeat :tag "Yes, for TODOs in this list"
  2305. (string :tag "TODO keyword"))
  2306. (list :tag "Yes, for TODOs and DONEs in these lists"
  2307. (repeat (string :tag "TODO keyword"))
  2308. (repeat (string :tag "DONE keyword")))
  2309. (other :tag "No TODO statistics" nil)))
  2310. (defcustom org-hierarchical-todo-statistics t
  2311. "Non-nil means TODO statistics covers just direct children.
  2312. When nil, all entries in the subtree are considered.
  2313. This has only an effect if `org-provide-todo-statistics' is set.
  2314. To set this to nil for only a single subtree, use a COOKIE_DATA
  2315. property and include the word \"recursive\" into the value."
  2316. :group 'org-todo
  2317. :type 'boolean)
  2318. (defcustom org-after-todo-state-change-hook nil
  2319. "Hook which is run after the state of a TODO item was changed.
  2320. The new state (a string with a TODO keyword, or nil) is available in the
  2321. Lisp variable `org-state'."
  2322. :group 'org-todo
  2323. :type 'hook)
  2324. (defvar org-blocker-hook nil
  2325. "Hook for functions that are allowed to block a state change.
  2326. Functions in this hook should not modify the buffer.
  2327. Each function gets as its single argument a property list,
  2328. see `org-trigger-hook' for more information about this list.
  2329. If any of the functions in this hook returns nil, the state change
  2330. is blocked.")
  2331. (defvar org-trigger-hook nil
  2332. "Hook for functions that are triggered by a state change.
  2333. Each function gets as its single argument a property list with at
  2334. least the following elements:
  2335. (:type type-of-change :position pos-at-entry-start
  2336. :from old-state :to new-state)
  2337. Depending on the type, more properties may be present.
  2338. This mechanism is currently implemented for:
  2339. TODO state changes
  2340. ------------------
  2341. :type todo-state-change
  2342. :from previous state (keyword as a string), or nil, or a symbol
  2343. `todo' or `done', to indicate the general type of state.
  2344. :to new state, like in :from")
  2345. (defcustom org-enforce-todo-dependencies nil
  2346. "Non-nil means undone TODO entries will block switching the parent to DONE.
  2347. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  2348. be blocked if any prior sibling is not yet done.
  2349. Finally, if the parent is blocked because of ordered siblings of its own,
  2350. the child will also be blocked."
  2351. :set (lambda (var val)
  2352. (set var val)
  2353. (if val
  2354. (add-hook 'org-blocker-hook
  2355. 'org-block-todo-from-children-or-siblings-or-parent)
  2356. (remove-hook 'org-blocker-hook
  2357. 'org-block-todo-from-children-or-siblings-or-parent)))
  2358. :group 'org-todo
  2359. :type 'boolean)
  2360. (defcustom org-enforce-todo-checkbox-dependencies nil
  2361. "Non-nil means unchecked boxes will block switching the parent to DONE.
  2362. When this is nil, checkboxes have no influence on switching TODO states.
  2363. When non-nil, you first need to check off all check boxes before the TODO
  2364. entry can be switched to DONE.
  2365. This variable needs to be set before org.el is loaded, and you need to
  2366. restart Emacs after a change to make the change effective. The only way
  2367. to change is while Emacs is running is through the customize interface."
  2368. :set (lambda (var val)
  2369. (set var val)
  2370. (if val
  2371. (add-hook 'org-blocker-hook
  2372. 'org-block-todo-from-checkboxes)
  2373. (remove-hook 'org-blocker-hook
  2374. 'org-block-todo-from-checkboxes)))
  2375. :group 'org-todo
  2376. :type 'boolean)
  2377. (defcustom org-treat-insert-todo-heading-as-state-change nil
  2378. "Non-nil means inserting a TODO heading is treated as state change.
  2379. So when the command \\[org-insert-todo-heading] is used, state change
  2380. logging will apply if appropriate. When nil, the new TODO item will
  2381. be inserted directly, and no logging will take place."
  2382. :group 'org-todo
  2383. :type 'boolean)
  2384. (defcustom org-treat-S-cursor-todo-selection-as-state-change t
  2385. "Non-nil means switching TODO states with S-cursor counts as state change.
  2386. This is the default behavior. However, setting this to nil allows a
  2387. convenient way to select a TODO state and bypass any logging associated
  2388. with that."
  2389. :group 'org-todo
  2390. :type 'boolean)
  2391. (defcustom org-todo-state-tags-triggers nil
  2392. "Tag changes that should be triggered by TODO state changes.
  2393. This is a list. Each entry is
  2394. (state-change (tag . flag) .......)
  2395. State-change can be a string with a state, and empty string to indicate the
  2396. state that has no TODO keyword, or it can be one of the symbols `todo'
  2397. or `done', meaning any not-done or done state, respectively."
  2398. :group 'org-todo
  2399. :group 'org-tags
  2400. :type '(repeat
  2401. (cons (choice :tag "When changing to"
  2402. (const :tag "Not-done state" todo)
  2403. (const :tag "Done state" done)
  2404. (string :tag "State"))
  2405. (repeat
  2406. (cons :tag "Tag action"
  2407. (string :tag "Tag")
  2408. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  2409. (defcustom org-log-done nil
  2410. "Information to record when a task moves to the DONE state.
  2411. Possible values are:
  2412. nil Don't add anything, just change the keyword
  2413. time Add a time stamp to the task
  2414. note Prompt for a note and add it with template `org-log-note-headings'
  2415. This option can also be set with on a per-file-basis with
  2416. #+STARTUP: nologdone
  2417. #+STARTUP: logdone
  2418. #+STARTUP: lognotedone
  2419. You can have local logging settings for a subtree by setting the LOGGING
  2420. property to one or more of these keywords."
  2421. :group 'org-todo
  2422. :group 'org-progress
  2423. :type '(choice
  2424. (const :tag "No logging" nil)
  2425. (const :tag "Record CLOSED timestamp" time)
  2426. (const :tag "Record CLOSED timestamp with note." note)))
  2427. ;; Normalize old uses of org-log-done.
  2428. (cond
  2429. ((eq org-log-done t) (setq org-log-done 'time))
  2430. ((and (listp org-log-done) (memq 'done org-log-done))
  2431. (setq org-log-done 'note)))
  2432. (defcustom org-log-reschedule nil
  2433. "Information to record when the scheduling date of a tasks is modified.
  2434. Possible values are:
  2435. nil Don't add anything, just change the date
  2436. time Add a time stamp to the task
  2437. note Prompt for a note and add it with template `org-log-note-headings'
  2438. This option can also be set with on a per-file-basis with
  2439. #+STARTUP: nologreschedule
  2440. #+STARTUP: logreschedule
  2441. #+STARTUP: lognotereschedule"
  2442. :group 'org-todo
  2443. :group 'org-progress
  2444. :type '(choice
  2445. (const :tag "No logging" nil)
  2446. (const :tag "Record timestamp" time)
  2447. (const :tag "Record timestamp with note." note)))
  2448. (defcustom org-log-redeadline nil
  2449. "Information to record when the deadline date of a tasks is modified.
  2450. Possible values are:
  2451. nil Don't add anything, just change the date
  2452. time Add a time stamp to the task
  2453. note Prompt for a note and add it with template `org-log-note-headings'
  2454. This option can also be set with on a per-file-basis with
  2455. #+STARTUP: nologredeadline
  2456. #+STARTUP: logredeadline
  2457. #+STARTUP: lognoteredeadline
  2458. You can have local logging settings for a subtree by setting the LOGGING
  2459. property to one or more of these keywords."
  2460. :group 'org-todo
  2461. :group 'org-progress
  2462. :type '(choice
  2463. (const :tag "No logging" nil)
  2464. (const :tag "Record timestamp" time)
  2465. (const :tag "Record timestamp with note." note)))
  2466. (defcustom org-log-note-clock-out nil
  2467. "Non-nil means record a note when clocking out of an item.
  2468. This can also be configured on a per-file basis by adding one of
  2469. the following lines anywhere in the buffer:
  2470. #+STARTUP: lognoteclock-out
  2471. #+STARTUP: nolognoteclock-out"
  2472. :group 'org-todo
  2473. :group 'org-progress
  2474. :type 'boolean)
  2475. (defcustom org-log-done-with-time t
  2476. "Non-nil means the CLOSED time stamp will contain date and time.
  2477. When nil, only the date will be recorded."
  2478. :group 'org-progress
  2479. :type 'boolean)
  2480. (defcustom org-log-note-headings
  2481. '((done . "CLOSING NOTE %t")
  2482. (state . "State %-12s from %-12S %t")
  2483. (note . "Note taken on %t")
  2484. (reschedule . "Rescheduled from %S on %t")
  2485. (delschedule . "Not scheduled, was %S on %t")
  2486. (redeadline . "New deadline from %S on %t")
  2487. (deldeadline . "Removed deadline, was %S on %t")
  2488. (refile . "Refiled on %t")
  2489. (clock-out . ""))
  2490. "Headings for notes added to entries.
  2491. The value is an alist, with the car being a symbol indicating the
  2492. note context, and the cdr is the heading to be used. The heading
  2493. may also be the empty string. The following placeholders can be
  2494. used:
  2495. %t a time stamp.
  2496. %T an active time stamp instead the default inactive one
  2497. %d a short-format time stamp.
  2498. %D an active short-format time stamp.
  2499. %s the new TODO state or time stamp (inactive), in double quotes.
  2500. %S the old TODO state or time stamp (inactive), in double quotes.
  2501. %u the user name.
  2502. %U full user name.
  2503. In fact, it is not a good idea to change the `state' entry,
  2504. because Agenda Log mode depends on the format of these entries."
  2505. :group 'org-todo
  2506. :group 'org-progress
  2507. :type '(list :greedy t
  2508. (cons (const :tag "Heading when closing an item" done) string)
  2509. (cons (const :tag
  2510. "Heading when changing todo state (todo sequence only)"
  2511. state) string)
  2512. (cons (const :tag "Heading when just taking a note" note) string)
  2513. (cons (const :tag "Heading when rescheduling" reschedule) string)
  2514. (cons (const :tag "Heading when an item is no longer scheduled" delschedule) string)
  2515. (cons (const :tag "Heading when changing deadline" redeadline) string)
  2516. (cons (const :tag "Heading when deleting a deadline" deldeadline) string)
  2517. (cons (const :tag "Heading when refiling" refile) string)
  2518. (cons (const :tag "Heading when clocking out" clock-out) string)))
  2519. (unless (assq 'note org-log-note-headings)
  2520. (push '(note . "%t") org-log-note-headings))
  2521. (defcustom org-log-into-drawer nil
  2522. "Non-nil means insert state change notes and time stamps into a drawer.
  2523. When nil, state changes notes will be inserted after the headline and
  2524. any scheduling and clock lines, but not inside a drawer.
  2525. The value of this variable should be the name of the drawer to use.
  2526. LOGBOOK is proposed as the default drawer for this purpose, you can
  2527. also set this to a string to define the drawer of your choice.
  2528. A value of t is also allowed, representing \"LOGBOOK\".
  2529. A value of t or nil can also be set with on a per-file-basis with
  2530. #+STARTUP: logdrawer
  2531. #+STARTUP: nologdrawer
  2532. If this variable is set, `org-log-state-notes-insert-after-drawers'
  2533. will be ignored.
  2534. You can set the property LOG_INTO_DRAWER to overrule this setting for
  2535. a subtree.
  2536. Do not check directly this variable in a Lisp program. Call
  2537. function `org-log-into-drawer' instead."
  2538. :group 'org-todo
  2539. :group 'org-progress
  2540. :type '(choice
  2541. (const :tag "Not into a drawer" nil)
  2542. (const :tag "LOGBOOK" t)
  2543. (string :tag "Other")))
  2544. (org-defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer)
  2545. (defun org-log-into-drawer ()
  2546. "Name of the log drawer, as a string, or nil.
  2547. This is the value of `org-log-into-drawer'. However, if the
  2548. current entry has or inherits a LOG_INTO_DRAWER property, it will
  2549. be used instead of the default value."
  2550. (let ((p (org-entry-get nil "LOG_INTO_DRAWER" 'inherit t)))
  2551. (cond ((equal p "nil") nil)
  2552. ((equal p "t") "LOGBOOK")
  2553. ((stringp p) p)
  2554. (p "LOGBOOK")
  2555. ((stringp org-log-into-drawer) org-log-into-drawer)
  2556. (org-log-into-drawer "LOGBOOK"))))
  2557. (defcustom org-log-state-notes-insert-after-drawers nil
  2558. "Non-nil means insert state change notes after any drawers in entry.
  2559. Only the drawers that *immediately* follow the headline and the
  2560. deadline/scheduled line are skipped.
  2561. When nil, insert notes right after the heading and perhaps the line
  2562. with deadline/scheduling if present.
  2563. This variable will have no effect if `org-log-into-drawer' is
  2564. set."
  2565. :group 'org-todo
  2566. :group 'org-progress
  2567. :type 'boolean)
  2568. (defcustom org-log-states-order-reversed t
  2569. "Non-nil means the latest state note will be directly after heading.
  2570. When nil, the state change notes will be ordered according to time.
  2571. This option can also be set with on a per-file-basis with
  2572. #+STARTUP: logstatesreversed
  2573. #+STARTUP: nologstatesreversed"
  2574. :group 'org-todo
  2575. :group 'org-progress
  2576. :type 'boolean)
  2577. (defcustom org-todo-repeat-to-state nil
  2578. "The TODO state to which a repeater should return the repeating task.
  2579. By default this is the first task in a TODO sequence, or the previous state
  2580. in a TODO_TYP set. But you can specify another task here.
  2581. alternatively, set the :REPEAT_TO_STATE: property of the entry."
  2582. :group 'org-todo
  2583. :version "24.1"
  2584. :type '(choice (const :tag "Head of sequence" nil)
  2585. (string :tag "Specific state")))
  2586. (defcustom org-log-repeat 'time
  2587. "Non-nil means record moving through the DONE state when triggering repeat.
  2588. An auto-repeating task is immediately switched back to TODO when
  2589. marked DONE. If you are not logging state changes (by adding \"@\"
  2590. or \"!\" to the TODO keyword definition), or set `org-log-done' to
  2591. record a closing note, there will be no record of the task moving
  2592. through DONE. This variable forces taking a note anyway.
  2593. nil Don't force a record
  2594. time Record a time stamp
  2595. note Prompt for a note and add it with template `org-log-note-headings'
  2596. This option can also be set with on a per-file-basis with
  2597. #+STARTUP: nologrepeat
  2598. #+STARTUP: logrepeat
  2599. #+STARTUP: lognoterepeat
  2600. You can have local logging settings for a subtree by setting the LOGGING
  2601. property to one or more of these keywords."
  2602. :group 'org-todo
  2603. :group 'org-progress
  2604. :type '(choice
  2605. (const :tag "Don't force a record" nil)
  2606. (const :tag "Force recording the DONE state" time)
  2607. (const :tag "Force recording a note with the DONE state" note)))
  2608. (defgroup org-priorities nil
  2609. "Priorities in Org-mode."
  2610. :tag "Org Priorities"
  2611. :group 'org-todo)
  2612. (defcustom org-enable-priority-commands t
  2613. "Non-nil means priority commands are active.
  2614. When nil, these commands will be disabled, so that you never accidentally
  2615. set a priority."
  2616. :group 'org-priorities
  2617. :type 'boolean)
  2618. (defcustom org-highest-priority ?A
  2619. "The highest priority of TODO items. A character like ?A, ?B etc.
  2620. Must have a smaller ASCII number than `org-lowest-priority'."
  2621. :group 'org-priorities
  2622. :type 'character)
  2623. (defcustom org-lowest-priority ?C
  2624. "The lowest priority of TODO items. A character like ?A, ?B etc.
  2625. Must have a larger ASCII number than `org-highest-priority'."
  2626. :group 'org-priorities
  2627. :type 'character)
  2628. (defcustom org-default-priority ?B
  2629. "The default priority of TODO items.
  2630. This is the priority an item gets if no explicit priority is given.
  2631. When starting to cycle on an empty priority the first step in the cycle
  2632. depends on `org-priority-start-cycle-with-default'. The resulting first
  2633. step priority must not exceed the range from `org-highest-priority' to
  2634. `org-lowest-priority' which means that `org-default-priority' has to be
  2635. in this range exclusive or inclusive the range boundaries. Else the
  2636. first step refuses to set the default and the second will fall back
  2637. to (depending on the command used) the highest or lowest priority."
  2638. :group 'org-priorities
  2639. :type 'character)
  2640. (defcustom org-priority-start-cycle-with-default t
  2641. "Non-nil means start with default priority when starting to cycle.
  2642. When this is nil, the first step in the cycle will be (depending on the
  2643. command used) one higher or lower than the default priority.
  2644. See also `org-default-priority'."
  2645. :group 'org-priorities
  2646. :type 'boolean)
  2647. (defcustom org-get-priority-function nil
  2648. "Function to extract the priority from a string.
  2649. The string is normally the headline. If this is nil Org computes the
  2650. priority from the priority cookie like [#A] in the headline. It returns
  2651. an integer, increasing by 1000 for each priority level.
  2652. The user can set a different function here, which should take a string
  2653. as an argument and return the numeric priority."
  2654. :group 'org-priorities
  2655. :version "24.1"
  2656. :type '(choice
  2657. (const nil)
  2658. (function)))
  2659. (defgroup org-time nil
  2660. "Options concerning time stamps and deadlines in Org-mode."
  2661. :tag "Org Time"
  2662. :group 'org)
  2663. (defcustom org-time-stamp-rounding-minutes '(0 5)
  2664. "Number of minutes to round time stamps to.
  2665. \\<org-mode-map>\
  2666. These are two values, the first applies when first creating a time stamp.
  2667. The second applies when changing it with the commands `S-up' and `S-down'.
  2668. When changing the time stamp, this means that it will change in steps
  2669. of N minutes, as given by the second value.
  2670. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  2671. numbers should be factors of 60, so for example 5, 10, 15.
  2672. When this is larger than 1, you can still force an exact time stamp by using
  2673. a double prefix argument to a time stamp command like \
  2674. `\\[org-time-stamp]' or `\\[org-time-stamp-inactive],
  2675. and by using a prefix arg to `S-up/down' to specify the exact number
  2676. of minutes to shift."
  2677. :group 'org-time
  2678. :get (lambda (var) ; Make sure both elements are there
  2679. (if (integerp (default-value var))
  2680. (list (default-value var) 5)
  2681. (default-value var)))
  2682. :type '(list
  2683. (integer :tag "when inserting times")
  2684. (integer :tag "when modifying times")))
  2685. ;; Normalize old customizations of this variable.
  2686. (when (integerp org-time-stamp-rounding-minutes)
  2687. (setq org-time-stamp-rounding-minutes
  2688. (list org-time-stamp-rounding-minutes
  2689. org-time-stamp-rounding-minutes)))
  2690. (defcustom org-display-custom-times nil
  2691. "Non-nil means overlay custom formats over all time stamps.
  2692. The formats are defined through the variable `org-time-stamp-custom-formats'.
  2693. To turn this on on a per-file basis, insert anywhere in the file:
  2694. #+STARTUP: customtime"
  2695. :group 'org-time
  2696. :set 'set-default
  2697. :type 'sexp)
  2698. (make-variable-buffer-local 'org-display-custom-times)
  2699. (defcustom org-time-stamp-custom-formats
  2700. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  2701. "Custom formats for time stamps. See `format-time-string' for the syntax.
  2702. These are overlaid over the default ISO format if the variable
  2703. `org-display-custom-times' is set. Time like %H:%M should be at the
  2704. end of the second format. The custom formats are also honored by export
  2705. commands, if custom time display is turned on at the time of export."
  2706. :group 'org-time
  2707. :type 'sexp)
  2708. (defun org-time-stamp-format (&optional long inactive)
  2709. "Get the right format for a time string."
  2710. (let ((f (if long (cdr org-time-stamp-formats)
  2711. (car org-time-stamp-formats))))
  2712. (if inactive
  2713. (concat "[" (substring f 1 -1) "]")
  2714. f)))
  2715. (defcustom org-time-clocksum-format
  2716. '(:days "%dd " :hours "%d" :require-hours t :minutes ":%02d" :require-minutes t)
  2717. "The format string used when creating CLOCKSUM lines.
  2718. This is also used when Org mode generates a time duration.
  2719. The value can be a single format string containing two
  2720. %-sequences, which will be filled with the number of hours and
  2721. minutes in that order.
  2722. Alternatively, the value can be a plist associating any of the
  2723. keys :years, :months, :weeks, :days, :hours or :minutes with
  2724. format strings. The time duration is formatted using only the
  2725. time components that are needed and concatenating the results.
  2726. If a time unit in absent, it falls back to the next smallest
  2727. unit.
  2728. The keys :require-years, :require-months, :require-days,
  2729. :require-weeks, :require-hours, :require-minutes are also
  2730. meaningful. A non-nil value for these keys indicates that the
  2731. corresponding time component should always be included, even if
  2732. its value is 0.
  2733. For example,
  2734. (:days \"%dd\" :hours \"%d\" :require-hours t :minutes \":%02d\"
  2735. :require-minutes t)
  2736. means durations longer than a day will be expressed in days,
  2737. hours and minutes, and durations less than a day will always be
  2738. expressed in hours and minutes (even for durations less than an
  2739. hour).
  2740. The value
  2741. (:days \"%dd\" :minutes \"%dm\")
  2742. means durations longer than a day will be expressed in days and
  2743. minutes, and durations less than a day will be expressed entirely
  2744. in minutes (even for durations longer than an hour)."
  2745. :group 'org-time
  2746. :group 'org-clock
  2747. :version "24.4"
  2748. :package-version '(Org . "8.0")
  2749. :type '(choice (string :tag "Format string")
  2750. (set :tag "Plist"
  2751. (group :inline t (const :tag "Years" :years)
  2752. (string :tag "Format string"))
  2753. (group :inline t
  2754. (const :tag "Always show years" :require-years)
  2755. (const t))
  2756. (group :inline t (const :tag "Months" :months)
  2757. (string :tag "Format string"))
  2758. (group :inline t
  2759. (const :tag "Always show months" :require-months)
  2760. (const t))
  2761. (group :inline t (const :tag "Weeks" :weeks)
  2762. (string :tag "Format string"))
  2763. (group :inline t
  2764. (const :tag "Always show weeks" :require-weeks)
  2765. (const t))
  2766. (group :inline t (const :tag "Days" :days)
  2767. (string :tag "Format string"))
  2768. (group :inline t
  2769. (const :tag "Always show days" :require-days)
  2770. (const t))
  2771. (group :inline t (const :tag "Hours" :hours)
  2772. (string :tag "Format string"))
  2773. (group :inline t
  2774. (const :tag "Always show hours" :require-hours)
  2775. (const t))
  2776. (group :inline t (const :tag "Minutes" :minutes)
  2777. (string :tag "Format string"))
  2778. (group :inline t
  2779. (const :tag "Always show minutes" :require-minutes)
  2780. (const t)))))
  2781. (defcustom org-time-clocksum-use-fractional nil
  2782. "When non-nil, \\[org-clock-display] uses fractional times.
  2783. See `org-time-clocksum-format' for more on time clock formats."
  2784. :group 'org-time
  2785. :group 'org-clock
  2786. :version "24.3"
  2787. :type 'boolean)
  2788. (defcustom org-time-clocksum-use-effort-durations nil
  2789. "When non-nil, \\[org-clock-display] uses effort durations.
  2790. E.g. by default, one day is considered to be a 8 hours effort,
  2791. so a task that has been clocked for 16 hours will be displayed
  2792. as during 2 days in the clock display or in the clocktable.
  2793. See `org-effort-durations' on how to set effort durations
  2794. and `org-time-clocksum-format' for more on time clock formats."
  2795. :group 'org-time
  2796. :group 'org-clock
  2797. :version "24.4"
  2798. :package-version '(Org . "8.0")
  2799. :type 'boolean)
  2800. (defcustom org-time-clocksum-fractional-format "%.2f"
  2801. "The format string used when creating CLOCKSUM lines,
  2802. or when Org mode generates a time duration, if
  2803. `org-time-clocksum-use-fractional' is enabled.
  2804. The value can be a single format string containing one
  2805. %-sequence, which will be filled with the number of hours as
  2806. a float.
  2807. Alternatively, the value can be a plist associating any of the
  2808. keys :years, :months, :weeks, :days, :hours or :minutes with
  2809. a format string. The time duration is formatted using the
  2810. largest time unit which gives a non-zero integer part. If all
  2811. specified formats have zero integer part, the smallest time unit
  2812. is used."
  2813. :group 'org-time
  2814. :type '(choice (string :tag "Format string")
  2815. (set (group :inline t (const :tag "Years" :years)
  2816. (string :tag "Format string"))
  2817. (group :inline t (const :tag "Months" :months)
  2818. (string :tag "Format string"))
  2819. (group :inline t (const :tag "Weeks" :weeks)
  2820. (string :tag "Format string"))
  2821. (group :inline t (const :tag "Days" :days)
  2822. (string :tag "Format string"))
  2823. (group :inline t (const :tag "Hours" :hours)
  2824. (string :tag "Format string"))
  2825. (group :inline t (const :tag "Minutes" :minutes)
  2826. (string :tag "Format string")))))
  2827. (defcustom org-deadline-warning-days 14
  2828. "Number of days before expiration during which a deadline becomes active.
  2829. This variable governs the display in sparse trees and in the agenda.
  2830. When 0 or negative, it means use this number (the absolute value of it)
  2831. even if a deadline has a different individual lead time specified.
  2832. Custom commands can set this variable in the options section."
  2833. :group 'org-time
  2834. :group 'org-agenda-daily/weekly
  2835. :type 'integer)
  2836. (defcustom org-scheduled-delay-days 0
  2837. "Number of days before a scheduled item becomes active.
  2838. This variable governs the display in sparse trees and in the agenda.
  2839. The default value (i.e. 0) means: don't delay scheduled item.
  2840. When negative, it means use this number (the absolute value of it)
  2841. even if a scheduled item has a different individual delay time
  2842. specified.
  2843. Custom commands can set this variable in the options section."
  2844. :group 'org-time
  2845. :group 'org-agenda-daily/weekly
  2846. :version "24.4"
  2847. :package-version '(Org . "8.0")
  2848. :type 'integer)
  2849. (defcustom org-read-date-prefer-future t
  2850. "Non-nil means assume future for incomplete date input from user.
  2851. This affects the following situations:
  2852. 1. The user gives a month but not a year.
  2853. For example, if it is April and you enter \"feb 2\", this will be read
  2854. as Feb 2, *next* year. \"May 5\", however, will be this year.
  2855. 2. The user gives a day, but no month.
  2856. For example, if today is the 15th, and you enter \"3\", Org-mode will
  2857. read this as the third of *next* month. However, if you enter \"17\",
  2858. it will be considered as *this* month.
  2859. If you set this variable to the symbol `time', then also the following
  2860. will work:
  2861. 3. If the user gives a time.
  2862. If the time is before now, it will be interpreted as tomorrow.
  2863. Currently none of this works for ISO week specifications.
  2864. When this option is nil, the current day, month and year will always be
  2865. used as defaults.
  2866. See also `org-agenda-jump-prefer-future'."
  2867. :group 'org-time
  2868. :type '(choice
  2869. (const :tag "Never" nil)
  2870. (const :tag "Check month and day" t)
  2871. (const :tag "Check month, day, and time" time)))
  2872. (defcustom org-agenda-jump-prefer-future 'org-read-date-prefer-future
  2873. "Should the agenda jump command prefer the future for incomplete dates?
  2874. The default is to do the same as configured in `org-read-date-prefer-future'.
  2875. But you can also set a deviating value here.
  2876. This may t or nil, or the symbol `org-read-date-prefer-future'."
  2877. :group 'org-agenda
  2878. :group 'org-time
  2879. :version "24.1"
  2880. :type '(choice
  2881. (const :tag "Use org-read-date-prefer-future"
  2882. org-read-date-prefer-future)
  2883. (const :tag "Never" nil)
  2884. (const :tag "Always" t)))
  2885. (defcustom org-read-date-force-compatible-dates t
  2886. "Should date/time prompt force dates that are guaranteed to work in Emacs?
  2887. Depending on the system Emacs is running on, certain dates cannot
  2888. be represented with the type used internally to represent time.
  2889. Dates between 1970-1-1 and 2038-1-1 can always be represented
  2890. correctly. Some systems allow for earlier dates, some for later,
  2891. some for both. One way to find out it to insert any date into an
  2892. Org buffer, putting the cursor on the year and hitting S-up and
  2893. S-down to test the range.
  2894. When this variable is set to t, the date/time prompt will not let
  2895. you specify dates outside the 1970-2037 range, so it is certain that
  2896. these dates will work in whatever version of Emacs you are
  2897. running, and also that you can move a file from one Emacs implementation
  2898. to another. WHenever Org is forcing the year for you, it will display
  2899. a message and beep.
  2900. When this variable is nil, Org will check if the date is
  2901. representable in the specific Emacs implementation you are using.
  2902. If not, it will force a year, usually the current year, and beep
  2903. to remind you. Currently this setting is not recommended because
  2904. the likelihood that you will open your Org files in an Emacs that
  2905. has limited date range is not negligible.
  2906. A workaround for this problem is to use diary sexp dates for time
  2907. stamps outside of this range."
  2908. :group 'org-time
  2909. :version "24.1"
  2910. :type 'boolean)
  2911. (defcustom org-read-date-display-live t
  2912. "Non-nil means display current interpretation of date prompt live.
  2913. This display will be in an overlay, in the minibuffer."
  2914. :group 'org-time
  2915. :type 'boolean)
  2916. (defcustom org-read-date-popup-calendar t
  2917. "Non-nil means pop up a calendar when prompting for a date.
  2918. In the calendar, the date can be selected with mouse-1. However, the
  2919. minibuffer will also be active, and you can simply enter the date as well.
  2920. When nil, only the minibuffer will be available."
  2921. :group 'org-time
  2922. :type 'boolean)
  2923. (org-defvaralias 'org-popup-calendar-for-date-prompt
  2924. 'org-read-date-popup-calendar)
  2925. (make-obsolete-variable
  2926. 'org-read-date-minibuffer-setup-hook
  2927. "Set `org-read-date-minibuffer-local-map' instead." "24.4")
  2928. (defcustom org-read-date-minibuffer-setup-hook nil
  2929. "Hook to be used to set up keys for the date/time interface.
  2930. Add key definitions to `minibuffer-local-map', which will be a
  2931. temporary copy.
  2932. WARNING: This option is obsolete, you should use
  2933. `org-read-date-minibuffer-local-map' to set up keys."
  2934. :group 'org-time
  2935. :type 'hook)
  2936. (defcustom org-extend-today-until 0
  2937. "The hour when your day really ends. Must be an integer.
  2938. This has influence for the following applications:
  2939. - When switching the agenda to \"today\". It it is still earlier than
  2940. the time given here, the day recognized as TODAY is actually yesterday.
  2941. - When a date is read from the user and it is still before the time given
  2942. here, the current date and time will be assumed to be yesterday, 23:59.
  2943. Also, timestamps inserted in capture templates follow this rule.
  2944. IMPORTANT: This is a feature whose implementation is and likely will
  2945. remain incomplete. Really, it is only here because past midnight seems to
  2946. be the favorite working time of John Wiegley :-)"
  2947. :group 'org-time
  2948. :type 'integer)
  2949. (defcustom org-use-effective-time nil
  2950. "If non-nil, consider `org-extend-today-until' when creating timestamps.
  2951. For example, if `org-extend-today-until' is 8, and it's 4am, then the
  2952. \"effective time\" of any timestamps between midnight and 8am will be
  2953. 23:59 of the previous day."
  2954. :group 'org-time
  2955. :version "24.1"
  2956. :type 'boolean)
  2957. (defcustom org-use-last-clock-out-time-as-effective-time nil
  2958. "When non-nil, use the last clock out time for `org-todo'.
  2959. Note that this option has precedence over the combined use of
  2960. `org-use-effective-time' and `org-extend-today-until'."
  2961. :group 'org-time
  2962. :version "24.4"
  2963. :package-version '(Org . "8.0")
  2964. :type 'boolean)
  2965. (defcustom org-edit-timestamp-down-means-later nil
  2966. "Non-nil means S-down will increase the time in a time stamp.
  2967. When nil, S-up will increase."
  2968. :group 'org-time
  2969. :type 'boolean)
  2970. (defcustom org-calendar-follow-timestamp-change t
  2971. "Non-nil means make the calendar window follow timestamp changes.
  2972. When a timestamp is modified and the calendar window is visible, it will be
  2973. moved to the new date."
  2974. :group 'org-time
  2975. :type 'boolean)
  2976. (defgroup org-tags nil
  2977. "Options concerning tags in Org-mode."
  2978. :tag "Org Tags"
  2979. :group 'org)
  2980. (defcustom org-tag-alist nil
  2981. "List of tags allowed in Org-mode files.
  2982. When this list is nil, Org-mode will base TAG input on what is already in the
  2983. buffer.
  2984. The value of this variable is an alist, the car of each entry must be a
  2985. keyword as a string, the cdr may be a character that is used to select
  2986. that tag through the fast-tag-selection interface.
  2987. See the manual for details."
  2988. :group 'org-tags
  2989. :type '(repeat
  2990. (choice
  2991. (cons (string :tag "Tag name")
  2992. (character :tag "Access char"))
  2993. (list :tag "Start radio group"
  2994. (const :startgroup)
  2995. (option (string :tag "Group description")))
  2996. (list :tag "Start tag group, non distinct"
  2997. (const :startgrouptag)
  2998. (option (string :tag "Group description")))
  2999. (list :tag "Group tags delimiter"
  3000. (const :grouptags))
  3001. (list :tag "End radio group"
  3002. (const :endgroup)
  3003. (option (string :tag "Group description")))
  3004. (list :tag "End tag group, non distinct"
  3005. (const :endgrouptag)
  3006. (option (string :tag "Group description")))
  3007. (const :tag "New line" (:newline)))))
  3008. (defcustom org-tag-persistent-alist nil
  3009. "List of tags that will always appear in all Org-mode files.
  3010. This is in addition to any in buffer settings or customizations
  3011. of `org-tag-alist'.
  3012. When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
  3013. The value of this variable is an alist, the car of each entry must be a
  3014. keyword as a string, the cdr may be a character that is used to select
  3015. that tag through the fast-tag-selection interface.
  3016. See the manual for details.
  3017. To disable these tags on a per-file basis, insert anywhere in the file:
  3018. #+STARTUP: noptag"
  3019. :group 'org-tags
  3020. :type '(repeat
  3021. (choice
  3022. (cons (string :tag "Tag name")
  3023. (character :tag "Access char"))
  3024. (const :tag "Start radio group" (:startgroup))
  3025. (const :tag "Group tags delimiter" (:grouptags))
  3026. (const :tag "End radio group" (:endgroup))
  3027. (const :tag "New line" (:newline)))))
  3028. (defcustom org-complete-tags-always-offer-all-agenda-tags nil
  3029. "If non-nil, always offer completion for all tags of all agenda files.
  3030. Instead of customizing this variable directly, you might want to
  3031. set it locally for capture buffers, because there no list of
  3032. tags in that file can be created dynamically (there are none).
  3033. (add-hook \\='org-capture-mode-hook
  3034. (lambda ()
  3035. (setq-local org-complete-tags-always-offer-all-agenda-tags t)))"
  3036. :group 'org-tags
  3037. :version "24.1"
  3038. :type 'boolean)
  3039. (defvar org-file-tags nil
  3040. "List of tags that can be inherited by all entries in the file.
  3041. The tags will be inherited if the variable `org-use-tag-inheritance'
  3042. says they should be.
  3043. This variable is populated from #+FILETAGS lines.")
  3044. (defcustom org-use-fast-tag-selection 'auto
  3045. "Non-nil means use fast tag selection scheme.
  3046. This is a special interface to select and deselect tags with single keys.
  3047. When nil, fast selection is never used.
  3048. When the symbol `auto', fast selection is used if and only if selection
  3049. characters for tags have been configured, either through the variable
  3050. `org-tag-alist' or through a #+TAGS line in the buffer.
  3051. When t, fast selection is always used and selection keys are assigned
  3052. automatically if necessary."
  3053. :group 'org-tags
  3054. :type '(choice
  3055. (const :tag "Always" t)
  3056. (const :tag "Never" nil)
  3057. (const :tag "When selection characters are configured" auto)))
  3058. (defcustom org-fast-tag-selection-single-key nil
  3059. "Non-nil means fast tag selection exits after first change.
  3060. When nil, you have to press RET to exit it.
  3061. During fast tag selection, you can toggle this flag with `C-c'.
  3062. This variable can also have the value `expert'. In this case, the window
  3063. displaying the tags menu is not even shown, until you press C-c again."
  3064. :group 'org-tags
  3065. :type '(choice
  3066. (const :tag "No" nil)
  3067. (const :tag "Yes" t)
  3068. (const :tag "Expert" expert)))
  3069. (defvar org-fast-tag-selection-include-todo nil
  3070. "Non-nil means fast tags selection interface will also offer TODO states.
  3071. This is an undocumented feature, you should not rely on it.")
  3072. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  3073. "The column to which tags should be indented in a headline.
  3074. If this number is positive, it specifies the column. If it is negative,
  3075. it means that the tags should be flushright to that column. For example,
  3076. -80 works well for a normal 80 character screen.
  3077. When 0, place tags directly after headline text, with only one space in
  3078. between."
  3079. :group 'org-tags
  3080. :type 'integer)
  3081. (defcustom org-auto-align-tags t
  3082. "Non-nil keeps tags aligned when modifying headlines.
  3083. Some operations (i.e. demoting) change the length of a headline and
  3084. therefore shift the tags around. With this option turned on, after
  3085. each such operation the tags are again aligned to `org-tags-column'."
  3086. :group 'org-tags
  3087. :type 'boolean)
  3088. (defcustom org-use-tag-inheritance t
  3089. "Non-nil means tags in levels apply also for sublevels.
  3090. When nil, only the tags directly given in a specific line apply there.
  3091. This may also be a list of tags that should be inherited, or a regexp that
  3092. matches tags that should be inherited. Additional control is possible
  3093. with the variable `org-tags-exclude-from-inheritance' which gives an
  3094. explicit list of tags to be excluded from inheritance, even if the value of
  3095. `org-use-tag-inheritance' would select it for inheritance.
  3096. If this option is t, a match early-on in a tree can lead to a large
  3097. number of matches in the subtree when constructing the agenda or creating
  3098. a sparse tree. If you only want to see the first match in a tree during
  3099. a search, check out the variable `org-tags-match-list-sublevels'."
  3100. :group 'org-tags
  3101. :type '(choice
  3102. (const :tag "Not" nil)
  3103. (const :tag "Always" t)
  3104. (repeat :tag "Specific tags" (string :tag "Tag"))
  3105. (regexp :tag "Tags matched by regexp")))
  3106. (defcustom org-tags-exclude-from-inheritance nil
  3107. "List of tags that should never be inherited.
  3108. This is a way to exclude a few tags from inheritance. For way to do
  3109. the opposite, to actively allow inheritance for selected tags,
  3110. see the variable `org-use-tag-inheritance'."
  3111. :group 'org-tags
  3112. :type '(repeat (string :tag "Tag")))
  3113. (defun org-tag-inherit-p (tag)
  3114. "Check if TAG is one that should be inherited."
  3115. (cond
  3116. ((member tag org-tags-exclude-from-inheritance) nil)
  3117. ((eq org-use-tag-inheritance t) t)
  3118. ((not org-use-tag-inheritance) nil)
  3119. ((stringp org-use-tag-inheritance)
  3120. (string-match org-use-tag-inheritance tag))
  3121. ((listp org-use-tag-inheritance)
  3122. (member tag org-use-tag-inheritance))
  3123. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  3124. (defcustom org-tags-match-list-sublevels t
  3125. "Non-nil means list also sublevels of headlines matching a search.
  3126. This variable applies to tags/property searches, and also to stuck
  3127. projects because this search is based on a tags match as well.
  3128. When set to the symbol `indented', sublevels are indented with
  3129. leading dots.
  3130. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  3131. the sublevels of a headline matching a tag search often also match
  3132. the same search. Listing all of them can create very long lists.
  3133. Setting this variable to nil causes subtrees of a match to be skipped.
  3134. This variable is semi-obsolete and probably should always be true. It
  3135. is better to limit inheritance to certain tags using the variables
  3136. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  3137. :group 'org-tags
  3138. :type '(choice
  3139. (const :tag "No, don't list them" nil)
  3140. (const :tag "Yes, do list them" t)
  3141. (const :tag "List them, indented with leading dots" indented)))
  3142. (defcustom org-tags-sort-function nil
  3143. "When set, tags are sorted using this function as a comparator."
  3144. :group 'org-tags
  3145. :type '(choice
  3146. (const :tag "No sorting" nil)
  3147. (const :tag "Alphabetical" string<)
  3148. (const :tag "Reverse alphabetical" string>)
  3149. (function :tag "Custom function" nil)))
  3150. (defvar org-tags-history nil
  3151. "History of minibuffer reads for tags.")
  3152. (defvar org-last-tags-completion-table nil
  3153. "The last used completion table for tags.")
  3154. (defvar org-after-tags-change-hook nil
  3155. "Hook that is run after the tags in a line have changed.")
  3156. (defgroup org-properties nil
  3157. "Options concerning properties in Org-mode."
  3158. :tag "Org Properties"
  3159. :group 'org)
  3160. (defcustom org-property-format "%-10s %s"
  3161. "How property key/value pairs should be formatted by `indent-line'.
  3162. When `indent-line' hits a property definition, it will format the line
  3163. according to this format, mainly to make sure that the values are
  3164. lined-up with respect to each other."
  3165. :group 'org-properties
  3166. :type 'string)
  3167. (defcustom org-properties-postprocess-alist nil
  3168. "Alist of properties and functions to adjust inserted values.
  3169. Elements of this alist must be of the form
  3170. ([string] [function])
  3171. where [string] must be a property name and [function] must be a
  3172. lambda expression: this lambda expression must take one argument,
  3173. the value to adjust, and return the new value as a string.
  3174. For example, this element will allow the property \"Remaining\"
  3175. to be updated wrt the relation between the \"Effort\" property
  3176. and the clock summary:
  3177. ((\"Remaining\" (lambda(value)
  3178. (let ((clocksum (org-clock-sum-current-item))
  3179. (effort (org-duration-string-to-minutes
  3180. (org-entry-get (point) \"Effort\"))))
  3181. (org-minutes-to-clocksum-string (- effort clocksum))))))"
  3182. :group 'org-properties
  3183. :version "24.1"
  3184. :type '(alist :key-type (string :tag "Property")
  3185. :value-type (function :tag "Function")))
  3186. (defcustom org-use-property-inheritance nil
  3187. "Non-nil means properties apply also for sublevels.
  3188. This setting is chiefly used during property searches. Turning it on can
  3189. cause significant overhead when doing a search, which is why it is not
  3190. on by default.
  3191. When nil, only the properties directly given in the current entry count.
  3192. When t, every property is inherited. The value may also be a list of
  3193. properties that should have inheritance, or a regular expression matching
  3194. properties that should be inherited.
  3195. However, note that some special properties use inheritance under special
  3196. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  3197. and the properties ending in \"_ALL\" when they are used as descriptor
  3198. for valid values of a property.
  3199. Note for programmers:
  3200. When querying an entry with `org-entry-get', you can control if inheritance
  3201. should be used. By default, `org-entry-get' looks only at the local
  3202. properties. You can request inheritance by setting the inherit argument
  3203. to t (to force inheritance) or to `selective' (to respect the setting
  3204. in this variable)."
  3205. :group 'org-properties
  3206. :type '(choice
  3207. (const :tag "Not" nil)
  3208. (const :tag "Always" t)
  3209. (repeat :tag "Specific properties" (string :tag "Property"))
  3210. (regexp :tag "Properties matched by regexp")))
  3211. (defun org-property-inherit-p (property)
  3212. "Check if PROPERTY is one that should be inherited."
  3213. (cond
  3214. ((eq org-use-property-inheritance t) t)
  3215. ((not org-use-property-inheritance) nil)
  3216. ((stringp org-use-property-inheritance)
  3217. (string-match org-use-property-inheritance property))
  3218. ((listp org-use-property-inheritance)
  3219. (member property org-use-property-inheritance))
  3220. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  3221. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  3222. "The default column format, if no other format has been defined.
  3223. This variable can be set on the per-file basis by inserting a line
  3224. #+COLUMNS: %25ITEM ....."
  3225. :group 'org-properties
  3226. :type 'string)
  3227. (defcustom org-columns-ellipses ".."
  3228. "The ellipses to be used when a field in column view is truncated.
  3229. When this is the empty string, as many characters as possible are shown,
  3230. but then there will be no visual indication that the field has been truncated.
  3231. When this is a string of length N, the last N characters of a truncated
  3232. field are replaced by this string. If the column is narrower than the
  3233. ellipses string, only part of the ellipses string will be shown."
  3234. :group 'org-properties
  3235. :type 'string)
  3236. (defcustom org-columns-modify-value-for-display-function nil
  3237. "Function that modifies values for display in column view.
  3238. For example, it can be used to cut out a certain part from a time stamp.
  3239. The function must take 2 arguments:
  3240. column-title The title of the column (*not* the property name)
  3241. value The value that should be modified.
  3242. The function should return the value that should be displayed,
  3243. or nil if the normal value should be used."
  3244. :group 'org-properties
  3245. :type '(choice (const nil) (function)))
  3246. (defconst org-global-properties-fixed
  3247. '(("VISIBILITY_ALL" . "folded children content all")
  3248. ("CLOCK_MODELINE_TOTAL_ALL" . "current today repeat all auto"))
  3249. "List of property/value pairs that can be inherited by any entry.
  3250. These are fixed values, for the preset properties. The user variable
  3251. that can be used to add to this list is `org-global-properties'.
  3252. The entries in this list are cons cells where the car is a property
  3253. name and cdr is a string with the value. If the value represents
  3254. multiple items like an \"_ALL\" property, separate the items by
  3255. spaces.")
  3256. (defcustom org-global-properties nil
  3257. "List of property/value pairs that can be inherited by any entry.
  3258. This list will be combined with the constant `org-global-properties-fixed'.
  3259. The entries in this list are cons cells where the car is a property
  3260. name and cdr is a string with the value.
  3261. You can set buffer-local values for the same purpose in the variable
  3262. `org-file-properties' this by adding lines like
  3263. #+PROPERTY: NAME VALUE"
  3264. :group 'org-properties
  3265. :type '(repeat
  3266. (cons (string :tag "Property")
  3267. (string :tag "Value"))))
  3268. (defvar-local org-file-properties nil
  3269. "List of property/value pairs that can be inherited by any entry.
  3270. Valid for the current buffer.
  3271. This variable is populated from #+PROPERTY lines.")
  3272. (defgroup org-agenda nil
  3273. "Options concerning agenda views in Org-mode."
  3274. :tag "Org Agenda"
  3275. :group 'org)
  3276. (defvar-local org-category nil
  3277. "Variable used by org files to set a category for agenda display.
  3278. Such files should use a file variable to set it, for example
  3279. # -*- mode: org; org-category: \"ELisp\"
  3280. or contain a special line
  3281. #+CATEGORY: ELisp
  3282. If the file does not specify a category, then file's base name
  3283. is used instead.")
  3284. (put 'org-category 'safe-local-variable (lambda (x) (or (symbolp x) (stringp x))))
  3285. (defcustom org-agenda-files nil
  3286. "The files to be used for agenda display.
  3287. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  3288. \\[org-remove-file]. You can also use customize to edit the list.
  3289. If an entry is a directory, all files in that directory that are matched by
  3290. `org-agenda-file-regexp' will be part of the file list.
  3291. If the value of the variable is not a list but a single file name, then
  3292. the list of agenda files is actually stored and maintained in that file, one
  3293. agenda file per line. In this file paths can be given relative to
  3294. `org-directory'. Tilde expansion and environment variable substitution
  3295. are also made."
  3296. :group 'org-agenda
  3297. :type '(choice
  3298. (repeat :tag "List of files and directories" file)
  3299. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  3300. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  3301. "Regular expression to match files for `org-agenda-files'.
  3302. If any element in the list in that variable contains a directory instead
  3303. of a normal file, all files in that directory that are matched by this
  3304. regular expression will be included."
  3305. :group 'org-agenda
  3306. :type 'regexp)
  3307. (defcustom org-agenda-text-search-extra-files nil
  3308. "List of extra files to be searched by text search commands.
  3309. These files will be searched in addition to the agenda files by the
  3310. commands `org-search-view' (`\\[org-agenda] s') \
  3311. and `org-occur-in-agenda-files'.
  3312. Note that these files will only be searched for text search commands,
  3313. not for the other agenda views like todo lists, tag searches or the weekly
  3314. agenda. This variable is intended to list notes and possibly archive files
  3315. that should also be searched by these two commands.
  3316. In fact, if the first element in the list is the symbol `agenda-archives',
  3317. then all archive files of all agenda files will be added to the search
  3318. scope."
  3319. :group 'org-agenda
  3320. :type '(set :greedy t
  3321. (const :tag "Agenda Archives" agenda-archives)
  3322. (repeat :inline t (file))))
  3323. (org-defvaralias 'org-agenda-multi-occur-extra-files
  3324. 'org-agenda-text-search-extra-files)
  3325. (defcustom org-agenda-skip-unavailable-files nil
  3326. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  3327. A nil value means to remove them, after a query, from the list."
  3328. :group 'org-agenda
  3329. :type 'boolean)
  3330. (defcustom org-calendar-to-agenda-key [?c]
  3331. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  3332. The command `org-calendar-goto-agenda' will be bound to this key. The
  3333. default is the character `c' because then `c' can be used to switch back and
  3334. forth between agenda and calendar."
  3335. :group 'org-agenda
  3336. :type 'sexp)
  3337. (defcustom org-calendar-insert-diary-entry-key [?i]
  3338. "The key to be installed in `calendar-mode-map' for adding diary entries.
  3339. This option is irrelevant until `org-agenda-diary-file' has been configured
  3340. to point to an Org-mode file. When that is the case, the command
  3341. `org-agenda-diary-entry' will be bound to the key given here, by default
  3342. `i'. In the calendar, `i' normally adds entries to `diary-file'. So
  3343. if you want to continue doing this, you need to change this to a different
  3344. key."
  3345. :group 'org-agenda
  3346. :type 'sexp)
  3347. (defcustom org-agenda-diary-file 'diary-file
  3348. "File to which to add new entries with the `i' key in agenda and calendar.
  3349. When this is the symbol `diary-file', the functionality in the Emacs
  3350. calendar will be used to add entries to the `diary-file'. But when this
  3351. points to a file, `org-agenda-diary-entry' will be used instead."
  3352. :group 'org-agenda
  3353. :type '(choice
  3354. (const :tag "The standard Emacs diary file" diary-file)
  3355. (file :tag "Special Org file diary entries")))
  3356. (eval-after-load "calendar"
  3357. '(progn
  3358. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  3359. 'org-calendar-goto-agenda)
  3360. (add-hook 'calendar-mode-hook
  3361. (lambda ()
  3362. (unless (eq org-agenda-diary-file 'diary-file)
  3363. (define-key calendar-mode-map
  3364. org-calendar-insert-diary-entry-key
  3365. 'org-agenda-diary-entry))))))
  3366. (defgroup org-latex nil
  3367. "Options for embedding LaTeX code into Org-mode."
  3368. :tag "Org LaTeX"
  3369. :group 'org)
  3370. (defcustom org-format-latex-options
  3371. '(:foreground default :background default :scale 1.0
  3372. :html-foreground "Black" :html-background "Transparent"
  3373. :html-scale 1.0 :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  3374. "Options for creating images from LaTeX fragments.
  3375. This is a property list with the following properties:
  3376. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  3377. `default' means use the foreground of the default face.
  3378. `auto' means use the foreground from the text face.
  3379. :background the background color, or \"Transparent\".
  3380. `default' means use the background of the default face.
  3381. `auto' means use the background from the text face.
  3382. :scale a scaling factor for the size of the images, to get more pixels
  3383. :html-foreground, :html-background, :html-scale
  3384. the same numbers for HTML export.
  3385. :matchers a list indicating which matchers should be used to
  3386. find LaTeX fragments. Valid members of this list are:
  3387. \"begin\" find environments
  3388. \"$1\" find single characters surrounded by $.$
  3389. \"$\" find math expressions surrounded by $...$
  3390. \"$$\" find math expressions surrounded by $$....$$
  3391. \"\\(\" find math expressions surrounded by \\(...\\)
  3392. \"\\=\\[\" find math expressions surrounded by \\=\\[...\\]"
  3393. :group 'org-latex
  3394. :type 'plist)
  3395. (defcustom org-format-latex-signal-error t
  3396. "Non-nil means signal an error when image creation of LaTeX snippets fails.
  3397. When nil, just push out a message."
  3398. :group 'org-latex
  3399. :version "24.1"
  3400. :type 'boolean)
  3401. (defcustom org-latex-to-mathml-jar-file nil
  3402. "Value of\"%j\" in `org-latex-to-mathml-convert-command'.
  3403. Use this to specify additional executable file say a jar file.
  3404. When using MathToWeb as the converter, specify the full-path to
  3405. your mathtoweb.jar file."
  3406. :group 'org-latex
  3407. :version "24.1"
  3408. :type '(choice
  3409. (const :tag "None" nil)
  3410. (file :tag "JAR file" :must-match t)))
  3411. (defcustom org-latex-to-mathml-convert-command nil
  3412. "Command to convert LaTeX fragments to MathML.
  3413. Replace format-specifiers in the command as noted below and use
  3414. `shell-command' to convert LaTeX to MathML.
  3415. %j: Executable file in fully expanded form as specified by
  3416. `org-latex-to-mathml-jar-file'.
  3417. %I: Input LaTeX file in fully expanded form.
  3418. %i: The latex fragment to be converted.
  3419. %o: Output MathML file.
  3420. This command is used by `org-create-math-formula'.
  3421. When using MathToWeb as the converter, set this option to
  3422. \"java -jar %j -unicode -force -df %o %I\".
  3423. When using LaTeXML set this option to
  3424. \"latexmlmath \"%i\" --presentationmathml=%o\"."
  3425. :group 'org-latex
  3426. :version "24.1"
  3427. :type '(choice
  3428. (const :tag "None" nil)
  3429. (string :tag "\nShell command")))
  3430. (defcustom org-latex-create-formula-image-program 'dvipng
  3431. "Program to convert LaTeX fragments with.
  3432. dvipng Process the LaTeX fragments to dvi file, then convert
  3433. dvi files to png files using dvipng.
  3434. This will also include processing of non-math environments.
  3435. imagemagick Convert the LaTeX fragments to pdf files and use imagemagick
  3436. to convert pdf files to png files"
  3437. :group 'org-latex
  3438. :version "24.1"
  3439. :type '(choice
  3440. (const :tag "dvipng" dvipng)
  3441. (const :tag "imagemagick" imagemagick)))
  3442. (defcustom org-latex-preview-ltxpng-directory "ltxpng/"
  3443. "Path to store latex preview images.
  3444. A relative path here creates many directories relative to the
  3445. processed org files paths. An absolute path puts all preview
  3446. images at the same place."
  3447. :group 'org-latex
  3448. :version "24.3"
  3449. :type 'string)
  3450. (defun org-format-latex-mathml-available-p ()
  3451. "Return t if `org-latex-to-mathml-convert-command' is usable."
  3452. (save-match-data
  3453. (when (and (boundp 'org-latex-to-mathml-convert-command)
  3454. org-latex-to-mathml-convert-command)
  3455. (let ((executable (car (split-string
  3456. org-latex-to-mathml-convert-command))))
  3457. (when (executable-find executable)
  3458. (if (string-match
  3459. "%j" org-latex-to-mathml-convert-command)
  3460. (file-readable-p org-latex-to-mathml-jar-file)
  3461. t))))))
  3462. (defcustom org-format-latex-header "\\documentclass{article}
  3463. \\usepackage[usenames]{color}
  3464. \[PACKAGES]
  3465. \[DEFAULT-PACKAGES]
  3466. \\pagestyle{empty} % do not remove
  3467. % The settings below are copied from fullpage.sty
  3468. \\setlength{\\textwidth}{\\paperwidth}
  3469. \\addtolength{\\textwidth}{-3cm}
  3470. \\setlength{\\oddsidemargin}{1.5cm}
  3471. \\addtolength{\\oddsidemargin}{-2.54cm}
  3472. \\setlength{\\evensidemargin}{\\oddsidemargin}
  3473. \\setlength{\\textheight}{\\paperheight}
  3474. \\addtolength{\\textheight}{-\\headheight}
  3475. \\addtolength{\\textheight}{-\\headsep}
  3476. \\addtolength{\\textheight}{-\\footskip}
  3477. \\addtolength{\\textheight}{-3cm}
  3478. \\setlength{\\topmargin}{1.5cm}
  3479. \\addtolength{\\topmargin}{-2.54cm}"
  3480. "The document header used for processing LaTeX fragments.
  3481. It is imperative that this header make sure that no page number
  3482. appears on the page. The package defined in the variables
  3483. `org-latex-default-packages-alist' and `org-latex-packages-alist'
  3484. will either replace the placeholder \"[PACKAGES]\" in this
  3485. header, or they will be appended."
  3486. :group 'org-latex
  3487. :type 'string)
  3488. (defun org-set-packages-alist (var val)
  3489. "Set the packages alist and make sure it has 3 elements per entry."
  3490. (set var (mapcar (lambda (x)
  3491. (if (and (consp x) (= (length x) 2))
  3492. (list (car x) (nth 1 x) t)
  3493. x))
  3494. val)))
  3495. (defun org-get-packages-alist (var)
  3496. "Get the packages alist and make sure it has 3 elements per entry."
  3497. (mapcar (lambda (x)
  3498. (if (and (consp x) (= (length x) 2))
  3499. (list (car x) (nth 1 x) t)
  3500. x))
  3501. (default-value var)))
  3502. (defcustom org-latex-default-packages-alist
  3503. '(("AUTO" "inputenc" t ("pdflatex"))
  3504. ("T1" "fontenc" t ("pdflatex"))
  3505. ("" "graphicx" t)
  3506. ("" "grffile" t)
  3507. ("" "longtable" nil)
  3508. ("" "wrapfig" nil)
  3509. ("" "rotating" nil)
  3510. ("normalem" "ulem" t)
  3511. ("" "amsmath" t)
  3512. ("" "textcomp" t)
  3513. ("" "amssymb" t)
  3514. ("" "capt-of" nil)
  3515. ("" "hyperref" nil))
  3516. "Alist of default packages to be inserted in the header.
  3517. Change this only if one of the packages here causes an
  3518. incompatibility with another package you are using.
  3519. The packages in this list are needed by one part or another of
  3520. Org mode to function properly:
  3521. - inputenc, fontenc: for basic font and character selection
  3522. - graphicx: for including images
  3523. - grffile: allow periods and spaces in graphics file names
  3524. - longtable: For multipage tables
  3525. - wrapfig: for figure placement
  3526. - rotating: for sideways figures and tables
  3527. - ulem: for underline and strike-through
  3528. - amsmath: for subscript and superscript and math environments
  3529. - textcomp, amssymb: for various symbols used
  3530. for interpreting the entities in `org-entities'. You can skip
  3531. some of these packages if you don't use any of their symbols.
  3532. - capt-of: for captions outside of floats
  3533. - hyperref: for cross references
  3534. Therefore you should not modify this variable unless you know
  3535. what you are doing. The one reason to change it anyway is that
  3536. you might be loading some other package that conflicts with one
  3537. of the default packages. Each element is either a cell or
  3538. a string.
  3539. A cell is of the format
  3540. (\"options\" \"package\" SNIPPET-FLAG COMPILERS)
  3541. If SNIPPET-FLAG is non-nil, the package also needs to be included
  3542. when compiling LaTeX snippets into images for inclusion into
  3543. non-LaTeX output. COMPILERS is a list of compilers that should
  3544. include the package, see `org-latex-compiler'. If the document
  3545. compiler is not in the list, and the list is non-nil, the package
  3546. will not be inserted in the final document.
  3547. A string will be inserted as-is in the header of the document."
  3548. :group 'org-latex
  3549. :group 'org-export-latex
  3550. :set 'org-set-packages-alist
  3551. :get 'org-get-packages-alist
  3552. :version "25.1"
  3553. :package-version '(Org . "8.3")
  3554. :type '(repeat
  3555. (choice
  3556. (list :tag "options/package pair"
  3557. (string :tag "options")
  3558. (string :tag "package")
  3559. (boolean :tag "Snippet"))
  3560. (string :tag "A line of LaTeX"))))
  3561. (defcustom org-latex-packages-alist nil
  3562. "Alist of packages to be inserted in every LaTeX header.
  3563. These will be inserted after `org-latex-default-packages-alist'.
  3564. Each element is either a cell or a string.
  3565. A cell is of the format:
  3566. (\"options\" \"package\" SNIPPET-FLAG)
  3567. SNIPPET-FLAG, when non-nil, indicates that this package is also
  3568. needed when turning LaTeX snippets into images for inclusion into
  3569. non-LaTeX output.
  3570. A string will be inserted as-is in the header of the document.
  3571. Make sure that you only list packages here which:
  3572. - you want in every file;
  3573. - do not conflict with the setup in `org-format-latex-header';
  3574. - do not conflict with the default packages in
  3575. `org-latex-default-packages-alist'."
  3576. :group 'org-latex
  3577. :group 'org-export-latex
  3578. :set 'org-set-packages-alist
  3579. :get 'org-get-packages-alist
  3580. :type '(repeat
  3581. (choice
  3582. (list :tag "options/package pair"
  3583. (string :tag "options")
  3584. (string :tag "package")
  3585. (boolean :tag "Snippet"))
  3586. (string :tag "A line of LaTeX"))))
  3587. (defgroup org-appearance nil
  3588. "Settings for Org-mode appearance."
  3589. :tag "Org Appearance"
  3590. :group 'org)
  3591. (defcustom org-level-color-stars-only nil
  3592. "Non-nil means fontify only the stars in each headline.
  3593. When nil, the entire headline is fontified.
  3594. Changing it requires restart of `font-lock-mode' to become effective
  3595. also in regions already fontified."
  3596. :group 'org-appearance
  3597. :type 'boolean)
  3598. (defcustom org-hide-leading-stars nil
  3599. "Non-nil means hide the first N-1 stars in a headline.
  3600. This works by using the face `org-hide' for these stars. This
  3601. face is white for a light background, and black for a dark
  3602. background. You may have to customize the face `org-hide' to
  3603. make this work.
  3604. Changing it requires restart of `font-lock-mode' to become effective
  3605. also in regions already fontified.
  3606. You may also set this on a per-file basis by adding one of the following
  3607. lines to the buffer:
  3608. #+STARTUP: hidestars
  3609. #+STARTUP: showstars"
  3610. :group 'org-appearance
  3611. :type 'boolean)
  3612. (defcustom org-hidden-keywords nil
  3613. "List of symbols corresponding to keywords to be hidden the org buffer.
  3614. For example, a value \\='(title) for this list will make the document's title
  3615. appear in the buffer without the initial #+TITLE: keyword."
  3616. :group 'org-appearance
  3617. :version "24.1"
  3618. :type '(set (const :tag "#+AUTHOR" author)
  3619. (const :tag "#+DATE" date)
  3620. (const :tag "#+EMAIL" email)
  3621. (const :tag "#+TITLE" title)))
  3622. (defcustom org-custom-properties nil
  3623. "List of properties (as strings) with a special meaning.
  3624. The default use of these custom properties is to let the user
  3625. hide them with `org-toggle-custom-properties-visibility'."
  3626. :group 'org-properties
  3627. :group 'org-appearance
  3628. :version "24.3"
  3629. :type '(repeat (string :tag "Property Name")))
  3630. (defcustom org-fontify-done-headline nil
  3631. "Non-nil means change the face of a headline if it is marked DONE.
  3632. Normally, only the TODO/DONE keyword indicates the state of a headline.
  3633. When this is non-nil, the headline after the keyword is set to the
  3634. `org-headline-done' as an additional indication."
  3635. :group 'org-appearance
  3636. :type 'boolean)
  3637. (defcustom org-fontify-emphasized-text t
  3638. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  3639. Changing this variable requires a restart of Emacs to take effect."
  3640. :group 'org-appearance
  3641. :type 'boolean)
  3642. (defcustom org-fontify-whole-heading-line nil
  3643. "Non-nil means fontify the whole line for headings.
  3644. This is useful when setting a background color for the
  3645. org-level-* faces."
  3646. :group 'org-appearance
  3647. :type 'boolean)
  3648. (defcustom org-highlight-latex-and-related nil
  3649. "Non-nil means highlight LaTeX related syntax in the buffer.
  3650. When non nil, the value should be a list containing any of the
  3651. following symbols:
  3652. `latex' Highlight LaTeX snippets and environments.
  3653. `script' Highlight subscript and superscript.
  3654. `entities' Highlight entities."
  3655. :group 'org-appearance
  3656. :version "24.4"
  3657. :package-version '(Org . "8.0")
  3658. :type '(choice
  3659. (const :tag "No highlighting" nil)
  3660. (set :greedy t :tag "Highlight"
  3661. (const :tag "LaTeX snippets and environments" latex)
  3662. (const :tag "Subscript and superscript" script)
  3663. (const :tag "Entities" entities))))
  3664. (defcustom org-hide-emphasis-markers nil
  3665. "Non-nil mean font-lock should hide the emphasis marker characters."
  3666. :group 'org-appearance
  3667. :type 'boolean)
  3668. (defcustom org-hide-macro-markers nil
  3669. "Non-nil mean font-lock should hide the brackets marking macro calls."
  3670. :group 'org-appearance
  3671. :type 'boolean)
  3672. (defcustom org-pretty-entities nil
  3673. "Non-nil means show entities as UTF8 characters.
  3674. When nil, the \\name form remains in the buffer."
  3675. :group 'org-appearance
  3676. :version "24.1"
  3677. :type 'boolean)
  3678. (defcustom org-pretty-entities-include-sub-superscripts t
  3679. "Non-nil means, pretty entity display includes formatting sub/superscripts."
  3680. :group 'org-appearance
  3681. :version "24.1"
  3682. :type 'boolean)
  3683. (defvar org-emph-re nil
  3684. "Regular expression for matching emphasis.
  3685. After a match, the match groups contain these elements:
  3686. 0 The match of the full regular expression, including the characters
  3687. before and after the proper match
  3688. 1 The character before the proper match, or empty at beginning of line
  3689. 2 The proper match, including the leading and trailing markers
  3690. 3 The leading marker like * or /, indicating the type of highlighting
  3691. 4 The text between the emphasis markers, not including the markers
  3692. 5 The character after the match, empty at the end of a line")
  3693. (defvar org-verbatim-re nil
  3694. "Regular expression for matching verbatim text.")
  3695. (defvar org-emphasis-regexp-components) ; defined just below
  3696. (defvar org-emphasis-alist) ; defined just below
  3697. (defun org-set-emph-re (var val)
  3698. "Set variable and compute the emphasis regular expression."
  3699. (set var val)
  3700. (when (and (boundp 'org-emphasis-alist)
  3701. (boundp 'org-emphasis-regexp-components)
  3702. org-emphasis-alist org-emphasis-regexp-components)
  3703. (let* ((e org-emphasis-regexp-components)
  3704. (pre (car e))
  3705. (post (nth 1 e))
  3706. (border (nth 2 e))
  3707. (body (nth 3 e))
  3708. (nl (nth 4 e))
  3709. (body1 (concat body "*?"))
  3710. (markers (mapconcat 'car org-emphasis-alist ""))
  3711. (vmarkers (mapconcat
  3712. (lambda (x) (if (eq (nth 2 x) 'verbatim) (car x) ""))
  3713. org-emphasis-alist "")))
  3714. ;; make sure special characters appear at the right position in the class
  3715. (if (string-match "\\^" markers)
  3716. (setq markers (concat (replace-match "" t t markers) "^")))
  3717. (if (string-match "-" markers)
  3718. (setq markers (concat (replace-match "" t t markers) "-")))
  3719. (if (string-match "\\^" vmarkers)
  3720. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  3721. (if (string-match "-" vmarkers)
  3722. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  3723. (if (> nl 0)
  3724. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  3725. (int-to-string nl) "\\}")))
  3726. ;; Make the regexp
  3727. (setq org-emph-re
  3728. (concat "\\([" pre "]\\|^\\)"
  3729. "\\("
  3730. "\\([" markers "]\\)"
  3731. "\\("
  3732. "[^" border "]\\|"
  3733. "[^" border "]"
  3734. body1
  3735. "[^" border "]"
  3736. "\\)"
  3737. "\\3\\)"
  3738. "\\([" post "]\\|$\\)"))
  3739. (setq org-verbatim-re
  3740. (concat "\\([" pre "]\\|^\\)"
  3741. "\\("
  3742. "\\([" vmarkers "]\\)"
  3743. "\\("
  3744. "[^" border "]\\|"
  3745. "[^" border "]"
  3746. body1
  3747. "[^" border "]"
  3748. "\\)"
  3749. "\\3\\)"
  3750. "\\([" post "]\\|$\\)")))))
  3751. ;; This used to be a defcustom (Org <8.0) but allowing the users to
  3752. ;; set this option proved cumbersome. See this message/thread:
  3753. ;; http://article.gmane.org/gmane.emacs.orgmode/68681
  3754. (defvar org-emphasis-regexp-components
  3755. '(" \t('\"{" "- \t.,:!?;'\")}\\[" " \t\r\n" "." 1)
  3756. "Components used to build the regular expression for emphasis.
  3757. This is a list with five entries. Terminology: In an emphasis string
  3758. like \" *strong word* \", we call the initial space PREMATCH, the final
  3759. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  3760. and \"trong wor\" is the body. The different components in this variable
  3761. specify what is allowed/forbidden in each part:
  3762. pre Chars allowed as prematch. Beginning of line will be allowed too.
  3763. post Chars allowed as postmatch. End of line will be allowed too.
  3764. border The chars *forbidden* as border characters.
  3765. body-regexp A regexp like \".\" to match a body character. Don't use
  3766. non-shy groups here, and don't allow newline here.
  3767. newline The maximum number of newlines allowed in an emphasis exp.
  3768. You need to reload Org or to restart Emacs after customizing this.")
  3769. (defcustom org-emphasis-alist
  3770. `(("*" bold)
  3771. ("/" italic)
  3772. ("_" underline)
  3773. ("=" org-verbatim verbatim)
  3774. ("~" org-code verbatim)
  3775. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))))
  3776. "Alist of characters and faces to emphasize text.
  3777. Text starting and ending with a special character will be emphasized,
  3778. for example *bold*, _underlined_ and /italic/. This variable sets the
  3779. marker characters and the face to be used by font-lock for highlighting
  3780. in Org-mode Emacs buffers.
  3781. You need to reload Org or to restart Emacs after customizing this."
  3782. :group 'org-appearance
  3783. :set 'org-set-emph-re
  3784. :version "24.4"
  3785. :package-version '(Org . "8.0")
  3786. :type '(repeat
  3787. (list
  3788. (string :tag "Marker character")
  3789. (choice
  3790. (face :tag "Font-lock-face")
  3791. (plist :tag "Face property list"))
  3792. (option (const verbatim)))))
  3793. (defvar org-protecting-blocks '("src" "example" "export")
  3794. "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
  3795. This is needed for font-lock setup.")
  3796. ;;; Miscellaneous options
  3797. (defgroup org-completion nil
  3798. "Completion in Org-mode."
  3799. :tag "Org Completion"
  3800. :group 'org)
  3801. (defcustom org-completion-use-ido nil
  3802. "Non-nil means use ido completion wherever possible.
  3803. Note that `ido-mode' must be active for this variable to be relevant.
  3804. If you decide to turn this variable on, you might well want to turn off
  3805. `org-outline-path-complete-in-steps'.
  3806. See also `org-completion-use-iswitchb'."
  3807. :group 'org-completion
  3808. :type 'boolean)
  3809. (defcustom org-completion-use-iswitchb nil
  3810. "Non-nil means use iswitchb completion wherever possible.
  3811. Note that `iswitchb-mode' must be active for this variable to be relevant.
  3812. If you decide to turn this variable on, you might well want to turn off
  3813. `org-outline-path-complete-in-steps'.
  3814. Note that this variable has only an effect if `org-completion-use-ido' is nil."
  3815. :group 'org-completion
  3816. :type 'boolean)
  3817. (defcustom org-completion-fallback-command 'hippie-expand
  3818. "The expansion command called by \\[pcomplete] in normal context.
  3819. Normal means, no org-mode-specific context."
  3820. :group 'org-completion
  3821. :type 'function)
  3822. ;;; Functions and variables from their packages
  3823. ;; Declared here to avoid compiler warnings
  3824. ;; XEmacs only
  3825. (defvar outline-mode-menu-heading)
  3826. (defvar outline-mode-menu-show)
  3827. (defvar outline-mode-menu-hide)
  3828. (defvar zmacs-regions) ; XEmacs regions
  3829. ;; Emacs only
  3830. (defvar mark-active)
  3831. ;; Various packages
  3832. (declare-function calc-eval "calc" (str &optional separator &rest args))
  3833. (declare-function calendar-forward-day "cal-move" (arg))
  3834. (declare-function calendar-goto-date "cal-move" (date))
  3835. (declare-function calendar-goto-today "cal-move" ())
  3836. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  3837. (declare-function calendar-iso-to-absolute "cal-iso" (date))
  3838. (declare-function cdlatex-compute-tables "ext:cdlatex" ())
  3839. (declare-function cdlatex-tab "ext:cdlatex" ())
  3840. (declare-function dired-get-filename
  3841. "dired"
  3842. (&optional localp no-error-if-not-filep))
  3843. (declare-function iswitchb-read-buffer
  3844. "iswitchb"
  3845. (prompt &optional default require-match start matches-set))
  3846. (declare-function org-agenda-change-all-lines
  3847. "org-agenda"
  3848. (newhead hdmarker &optional fixface just-this))
  3849. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  3850. "org-agenda"
  3851. (&optional end))
  3852. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  3853. (declare-function org-agenda-format-item
  3854. "org-agenda"
  3855. (extra txt &optional level category tags dotime noprefix
  3856. remove-re habitp))
  3857. (declare-function org-agenda-maybe-redo "org-agenda" ())
  3858. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  3859. (declare-function org-agenda-save-markers-for-cut-and-paste
  3860. "org-agenda"
  3861. (beg end))
  3862. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  3863. (declare-function org-agenda-skip "org-agenda" ())
  3864. (declare-function org-attach-reveal "org-attach" (&optional if-exists))
  3865. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  3866. (declare-function org-indent-mode "org-indent" (&optional arg))
  3867. (declare-function org-inlinetask-goto-beginning "org-inlinetask" ())
  3868. (declare-function org-inlinetask-goto-end "org-inlinetask" ())
  3869. (declare-function org-inlinetask-in-task-p "org-inlinetask" ())
  3870. (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
  3871. (declare-function orgtbl-send-table "org-table" (&optional maybe))
  3872. (declare-function parse-time-string "parse-time" (string))
  3873. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  3874. (declare-function table--at-cell-p
  3875. "table"
  3876. (position &optional object at-column))
  3877. (defvar align-mode-rules-list)
  3878. (defvar calc-embedded-close-formula)
  3879. (defvar calc-embedded-open-formula)
  3880. (defvar calc-embedded-open-mode)
  3881. (defvar font-lock-unfontify-region-function)
  3882. (defvar iswitchb-temp-buflist)
  3883. (defvar org-agenda-tags-todo-honor-ignore-options)
  3884. (defvar remember-data-file)
  3885. (defvar texmathp-why)
  3886. ;;;###autoload
  3887. (defun turn-on-orgtbl ()
  3888. "Unconditionally turn on `orgtbl-mode'."
  3889. (require 'org-table)
  3890. (orgtbl-mode 1))
  3891. (defun org-at-table-p (&optional table-type)
  3892. "Non-nil if the cursor is inside an Org table.
  3893. If TABLE-TYPE is non-nil, also check for table.el-type tables.
  3894. If `org-enable-table-editor' is nil, return nil unconditionally."
  3895. (and
  3896. org-enable-table-editor
  3897. (save-excursion
  3898. (beginning-of-line)
  3899. (org-looking-at-p (if table-type "[ \t]*[|+]" "[ \t]*|")))
  3900. (or (not (derived-mode-p 'org-mode))
  3901. (let ((e (org-element-lineage (org-element-at-point) '(table) t)))
  3902. (and e (or table-type (eq (org-element-property :type e) 'org)))))))
  3903. (define-obsolete-function-alias 'org-table-p 'org-at-table-p "Org 9.0")
  3904. (defun org-at-table.el-p ()
  3905. "Non-nil when point is at a table.el table."
  3906. (and (save-excursion (beginning-of-line) (looking-at "[ \t]*[|+]"))
  3907. (let ((element (org-element-at-point)))
  3908. (and (eq (org-element-type element) 'table)
  3909. (eq (org-element-property :type element) 'table.el)))))
  3910. (defun org-table-recognize-table.el ()
  3911. "If there is a table.el table nearby, recognize it and move into it."
  3912. (when (and org-table-tab-recognizes-table.el (org-at-table.el-p))
  3913. (beginning-of-line)
  3914. (unless (or (looking-at org-table-dataline-regexp)
  3915. (not (looking-at org-table1-hline-regexp)))
  3916. (forward-line)
  3917. (when (looking-at org-table-any-border-regexp)
  3918. (forward-line -2)))
  3919. (if (re-search-forward "|" (org-table-end t) t)
  3920. (progn
  3921. (require 'table)
  3922. (if (table--at-cell-p (point)) t
  3923. (message "recognizing table.el table...")
  3924. (table-recognize-table)
  3925. (message "recognizing table.el table...done")))
  3926. (error "This should not happen"))))
  3927. ;;; This function is not used by org core since commit 6d1e3082, Feb 2010
  3928. (make-obsolete 'org-table-recognize-table.el
  3929. "please notify the org mailing list if you use this function."
  3930. "Org 9.0")
  3931. (defun org-at-table-hline-p ()
  3932. "Non-nil when point is inside a hline in a table.
  3933. Assume point is already in a table. If `org-enable-table-editor'
  3934. is nil, return nil unconditionally."
  3935. (and org-enable-table-editor
  3936. (save-excursion
  3937. (beginning-of-line)
  3938. (looking-at org-table-hline-regexp))))
  3939. (defun org-table-map-tables (function &optional quietly)
  3940. "Apply FUNCTION to the start of all tables in the buffer."
  3941. (save-excursion
  3942. (save-restriction
  3943. (widen)
  3944. (goto-char (point-min))
  3945. (while (re-search-forward org-table-any-line-regexp nil t)
  3946. (unless quietly
  3947. (message "Mapping tables: %d%%"
  3948. (floor (* 100.0 (point)) (buffer-size))))
  3949. (beginning-of-line 1)
  3950. (when (and (looking-at org-table-line-regexp)
  3951. ;; Exclude tables in src/example/verbatim/clocktable blocks
  3952. (not (org-in-block-p '("src" "example" "verbatim" "clocktable"))))
  3953. (save-excursion (funcall function))
  3954. (or (looking-at org-table-line-regexp)
  3955. (forward-char 1)))
  3956. (re-search-forward org-table-any-border-regexp nil 1))))
  3957. (unless quietly (message "Mapping tables: done")))
  3958. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock" (beg end))
  3959. (declare-function org-clock-update-mode-line "org-clock" ())
  3960. (declare-function org-resolve-clocks "org-clock"
  3961. (&optional also-non-dangling-p prompt last-valid))
  3962. (defun org-at-TBLFM-p (&optional pos)
  3963. "Non-nil when point (or POS) is in #+TBLFM line."
  3964. (save-excursion
  3965. (goto-char (or pos (point)))
  3966. (beginning-of-line)
  3967. (and (let ((case-fold-search t)) (looking-at org-TBLFM-regexp))
  3968. (eq (org-element-type (org-element-at-point)) 'table))))
  3969. (defvar org-clock-start-time)
  3970. (defvar org-clock-marker (make-marker)
  3971. "Marker recording the last clock-in.")
  3972. (defvar org-clock-hd-marker (make-marker)
  3973. "Marker recording the last clock-in, but the headline position.")
  3974. (defvar org-clock-heading ""
  3975. "The heading of the current clock entry.")
  3976. (defun org-clock-is-active ()
  3977. "Return the buffer where the clock is currently running.
  3978. Return nil if no clock is running."
  3979. (marker-buffer org-clock-marker))
  3980. (defun org-check-running-clock ()
  3981. "Check if the current buffer contains the running clock.
  3982. If yes, offer to stop it and to save the buffer with the changes."
  3983. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3984. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  3985. (buffer-name))))
  3986. (org-clock-out)
  3987. (when (y-or-n-p "Save changed buffer?")
  3988. (save-buffer))))
  3989. (defun org-clocktable-try-shift (dir n)
  3990. "Check if this line starts a clock table, if yes, shift the time block."
  3991. (when (org-match-line "^[ \t]*#\\+BEGIN:[ \t]+clocktable\\>")
  3992. (org-clocktable-shift dir n)))
  3993. ;;;###autoload
  3994. (defun org-clock-persistence-insinuate ()
  3995. "Set up hooks for clock persistence."
  3996. (require 'org-clock)
  3997. (add-hook 'org-mode-hook 'org-clock-load)
  3998. (add-hook 'kill-emacs-hook 'org-clock-save))
  3999. (defgroup org-archive nil
  4000. "Options concerning archiving in Org-mode."
  4001. :tag "Org Archive"
  4002. :group 'org-structure)
  4003. (defcustom org-archive-location "%s_archive::"
  4004. "The location where subtrees should be archived.
  4005. The value of this variable is a string, consisting of two parts,
  4006. separated by a double-colon. The first part is a filename and
  4007. the second part is a headline.
  4008. When the filename is omitted, archiving happens in the same file.
  4009. %s in the filename will be replaced by the current file
  4010. name (without the directory part). Archiving to a different file
  4011. is useful to keep archived entries from contributing to the
  4012. Org-mode Agenda.
  4013. The archived entries will be filed as subtrees of the specified
  4014. headline. When the headline is omitted, the subtrees are simply
  4015. filed away at the end of the file, as top-level entries. Also in
  4016. the heading you can use %s to represent the file name, this can be
  4017. useful when using the same archive for a number of different files.
  4018. Here are a few examples:
  4019. \"%s_archive::\"
  4020. If the current file is Projects.org, archive in file
  4021. Projects.org_archive, as top-level trees. This is the default.
  4022. \"::* Archived Tasks\"
  4023. Archive in the current file, under the top-level headline
  4024. \"* Archived Tasks\".
  4025. \"~/org/archive.org::\"
  4026. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  4027. \"~/org/archive.org::* From %s\"
  4028. Archive in file ~/org/archive.org (absolute path), under headlines
  4029. \"From FILENAME\" where file name is the current file name.
  4030. \"~/org/datetree.org::datetree/* Finished Tasks\"
  4031. The \"datetree/\" string is special, signifying to archive
  4032. items to the datetree. Items are placed in either the CLOSED
  4033. date of the item, or the current date if there is no CLOSED date.
  4034. The heading will be a subentry to the current date. There doesn't
  4035. need to be a heading, but there always needs to be a slash after
  4036. datetree. For example, to store archived items directly in the
  4037. datetree, use \"~/org/datetree.org::datetree/\".
  4038. \"basement::** Finished Tasks\"
  4039. Archive in file ./basement (relative path), as level 3 trees
  4040. below the level 2 heading \"** Finished Tasks\".
  4041. You may set this option on a per-file basis by adding to the buffer a
  4042. line like
  4043. #+ARCHIVE: basement::** Finished Tasks
  4044. You may also define it locally for a subtree by setting an ARCHIVE property
  4045. in the entry. If such a property is found in an entry, or anywhere up
  4046. the hierarchy, it will be used."
  4047. :group 'org-archive
  4048. :type 'string)
  4049. (defcustom org-agenda-skip-archived-trees t
  4050. "Non-nil means the agenda will skip any items located in archived trees.
  4051. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  4052. variable is no longer recommended, you should leave it at the value t.
  4053. Instead, use the key `v' to cycle the archives-mode in the agenda."
  4054. :group 'org-archive
  4055. :group 'org-agenda-skip
  4056. :type 'boolean)
  4057. (defcustom org-columns-skip-archived-trees t
  4058. "Non-nil means ignore archived trees when creating column view."
  4059. :group 'org-archive
  4060. :group 'org-properties
  4061. :type 'boolean)
  4062. (defcustom org-cycle-open-archived-trees nil
  4063. "Non-nil means `org-cycle' will open archived trees.
  4064. An archived tree is a tree marked with the tag ARCHIVE.
  4065. When nil, archived trees will stay folded. You can still open them with
  4066. normal outline commands like `show-all', but not with the cycling commands."
  4067. :group 'org-archive
  4068. :group 'org-cycle
  4069. :type 'boolean)
  4070. (defcustom org-sparse-tree-open-archived-trees nil
  4071. "Non-nil means sparse tree construction shows matches in archived trees.
  4072. When nil, matches in these trees are highlighted, but the trees are kept in
  4073. collapsed state."
  4074. :group 'org-archive
  4075. :group 'org-sparse-trees
  4076. :type 'boolean)
  4077. (defcustom org-sparse-tree-default-date-type nil
  4078. "The default date type when building a sparse tree.
  4079. When this is nil, a date is a scheduled or a deadline timestamp.
  4080. Otherwise, these types are allowed:
  4081. all: all timestamps
  4082. active: only active timestamps (<...>)
  4083. inactive: only inactive timestamps ([...])
  4084. scheduled: only scheduled timestamps
  4085. deadline: only deadline timestamps"
  4086. :type '(choice (const :tag "Scheduled or deadline" nil)
  4087. (const :tag "All timestamps" all)
  4088. (const :tag "Only active timestamps" active)
  4089. (const :tag "Only inactive timestamps" inactive)
  4090. (const :tag "Only scheduled timestamps" scheduled)
  4091. (const :tag "Only deadline timestamps" deadline)
  4092. (const :tag "Only closed timestamps" closed))
  4093. :version "25.1"
  4094. :package-version '(Org . "8.3")
  4095. :group 'org-sparse-trees)
  4096. (defun org-cycle-hide-archived-subtrees (state)
  4097. "Re-hide all archived subtrees after a visibility state change."
  4098. (when (and (not org-cycle-open-archived-trees)
  4099. (not (memq state '(overview folded))))
  4100. (save-excursion
  4101. (let* ((globalp (memq state '(contents all)))
  4102. (beg (if globalp (point-min) (point)))
  4103. (end (if globalp (point-max) (org-end-of-subtree t))))
  4104. (org-hide-archived-subtrees beg end)
  4105. (goto-char beg)
  4106. (when (looking-at-p (concat ".*:" org-archive-tag ":"))
  4107. (message "%s" (substitute-command-keys
  4108. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  4109. (defun org-force-cycle-archived ()
  4110. "Cycle subtree even if it is archived."
  4111. (interactive)
  4112. (setq this-command 'org-cycle)
  4113. (let ((org-cycle-open-archived-trees t))
  4114. (call-interactively 'org-cycle)))
  4115. (defun org-hide-archived-subtrees (beg end)
  4116. "Re-hide all archived subtrees after a visibility state change."
  4117. (org-with-wide-buffer
  4118. (let ((case-fold-search nil)
  4119. (re (concat org-outline-regexp-bol ".*:" org-archive-tag ":")))
  4120. (goto-char beg)
  4121. ;; Include headline point is currently on.
  4122. (beginning-of-line)
  4123. (while (and (< (point) end) (re-search-forward re end t))
  4124. (when (member org-archive-tag (org-get-tags))
  4125. (org-flag-subtree t)
  4126. (org-end-of-subtree t))))))
  4127. (declare-function outline-end-of-heading "outline" ())
  4128. (declare-function outline-flag-region "outline" (from to flag))
  4129. (defun org-flag-subtree (flag)
  4130. (save-excursion
  4131. (org-back-to-heading t)
  4132. (outline-end-of-heading)
  4133. (outline-flag-region (point)
  4134. (progn (org-end-of-subtree t) (point))
  4135. flag)))
  4136. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  4137. ;; Declare Column View Code
  4138. (declare-function org-columns-number-to-string "org-colview" (n fmt &optional printf))
  4139. (declare-function org-columns-get-format-and-top-level "org-colview" ())
  4140. (declare-function org-columns-compute "org-colview" (property))
  4141. ;; Declare ID code
  4142. (declare-function org-id-store-link "org-id")
  4143. (declare-function org-id-locations-load "org-id")
  4144. (declare-function org-id-locations-save "org-id")
  4145. (defvar org-id-track-globally)
  4146. ;;; Variables for pre-computed regular expressions, all buffer local
  4147. (defvar-local org-todo-regexp nil
  4148. "Matches any of the TODO state keywords.")
  4149. (defvar-local org-not-done-regexp nil
  4150. "Matches any of the TODO state keywords except the last one.")
  4151. (defvar-local org-not-done-heading-regexp nil
  4152. "Matches a TODO headline that is not done.")
  4153. (defvar-local org-todo-line-regexp nil
  4154. "Matches a headline and puts TODO state into group 2 if present.")
  4155. (defvar-local org-complex-heading-regexp nil
  4156. "Matches a headline and puts everything into groups:
  4157. group 1: the stars
  4158. group 2: The todo keyword, maybe
  4159. group 3: Priority cookie
  4160. group 4: True headline
  4161. group 5: Tags")
  4162. (defvar-local org-complex-heading-regexp-format nil
  4163. "Printf format to make regexp to match an exact headline.
  4164. This regexp will match the headline of any node which has the
  4165. exact headline text that is put into the format, but may have any
  4166. TODO state, priority and tags.")
  4167. (defvar-local org-todo-line-tags-regexp nil
  4168. "Matches a headline and puts TODO state into group 2 if present.
  4169. Also put tags into group 4 if tags are present.")
  4170. (defconst org-plain-time-of-day-regexp
  4171. (concat
  4172. "\\(\\<[012]?[0-9]"
  4173. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4174. "\\(--?"
  4175. "\\(\\<[012]?[0-9]"
  4176. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4177. "\\)?")
  4178. "Regular expression to match a plain time or time range.
  4179. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  4180. groups carry important information:
  4181. 0 the full match
  4182. 1 the first time, range or not
  4183. 8 the second time, if it is a range.")
  4184. (defconst org-plain-time-extension-regexp
  4185. (concat
  4186. "\\(\\<[012]?[0-9]"
  4187. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4188. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  4189. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  4190. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  4191. groups carry important information:
  4192. 0 the full match
  4193. 7 hours of duration
  4194. 9 minutes of duration")
  4195. (defconst org-stamp-time-of-day-regexp
  4196. (concat
  4197. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  4198. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  4199. "\\(--?"
  4200. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  4201. "Regular expression to match a timestamp time or time range.
  4202. After a match, the following groups carry important information:
  4203. 0 the full match
  4204. 1 date plus weekday, for back referencing to make sure both times are on the same day
  4205. 2 the first time, range or not
  4206. 4 the second time, if it is a range.")
  4207. (defconst org-startup-options
  4208. '(("fold" org-startup-folded t)
  4209. ("overview" org-startup-folded t)
  4210. ("nofold" org-startup-folded nil)
  4211. ("showall" org-startup-folded nil)
  4212. ("showeverything" org-startup-folded showeverything)
  4213. ("content" org-startup-folded content)
  4214. ("indent" org-startup-indented t)
  4215. ("noindent" org-startup-indented nil)
  4216. ("hidestars" org-hide-leading-stars t)
  4217. ("showstars" org-hide-leading-stars nil)
  4218. ("odd" org-odd-levels-only t)
  4219. ("oddeven" org-odd-levels-only nil)
  4220. ("align" org-startup-align-all-tables t)
  4221. ("noalign" org-startup-align-all-tables nil)
  4222. ("inlineimages" org-startup-with-inline-images t)
  4223. ("noinlineimages" org-startup-with-inline-images nil)
  4224. ("latexpreview" org-startup-with-latex-preview t)
  4225. ("nolatexpreview" org-startup-with-latex-preview nil)
  4226. ("customtime" org-display-custom-times t)
  4227. ("logdone" org-log-done time)
  4228. ("lognotedone" org-log-done note)
  4229. ("nologdone" org-log-done nil)
  4230. ("lognoteclock-out" org-log-note-clock-out t)
  4231. ("nolognoteclock-out" org-log-note-clock-out nil)
  4232. ("logrepeat" org-log-repeat state)
  4233. ("lognoterepeat" org-log-repeat note)
  4234. ("logdrawer" org-log-into-drawer t)
  4235. ("nologdrawer" org-log-into-drawer nil)
  4236. ("logstatesreversed" org-log-states-order-reversed t)
  4237. ("nologstatesreversed" org-log-states-order-reversed nil)
  4238. ("nologrepeat" org-log-repeat nil)
  4239. ("logreschedule" org-log-reschedule time)
  4240. ("lognotereschedule" org-log-reschedule note)
  4241. ("nologreschedule" org-log-reschedule nil)
  4242. ("logredeadline" org-log-redeadline time)
  4243. ("lognoteredeadline" org-log-redeadline note)
  4244. ("nologredeadline" org-log-redeadline nil)
  4245. ("logrefile" org-log-refile time)
  4246. ("lognoterefile" org-log-refile note)
  4247. ("nologrefile" org-log-refile nil)
  4248. ("fninline" org-footnote-define-inline t)
  4249. ("nofninline" org-footnote-define-inline nil)
  4250. ("fnlocal" org-footnote-section nil)
  4251. ("fnauto" org-footnote-auto-label t)
  4252. ("fnprompt" org-footnote-auto-label nil)
  4253. ("fnconfirm" org-footnote-auto-label confirm)
  4254. ("fnplain" org-footnote-auto-label plain)
  4255. ("fnadjust" org-footnote-auto-adjust t)
  4256. ("nofnadjust" org-footnote-auto-adjust nil)
  4257. ("constcgs" constants-unit-system cgs)
  4258. ("constSI" constants-unit-system SI)
  4259. ("noptag" org-tag-persistent-alist nil)
  4260. ("hideblocks" org-hide-block-startup t)
  4261. ("nohideblocks" org-hide-block-startup nil)
  4262. ("beamer" org-startup-with-beamer-mode t)
  4263. ("entitiespretty" org-pretty-entities t)
  4264. ("entitiesplain" org-pretty-entities nil))
  4265. "Variable associated with STARTUP options for org-mode.
  4266. Each element is a list of three items: the startup options (as written
  4267. in the #+STARTUP line), the corresponding variable, and the value to set
  4268. this variable to if the option is found. An optional forth element PUSH
  4269. means to push this value onto the list in the variable.")
  4270. (defcustom org-group-tags t
  4271. "When non-nil (the default), use group tags.
  4272. This can be turned on/off through `org-toggle-tags-groups'."
  4273. :group 'org-tags
  4274. :group 'org-startup
  4275. :type 'boolean)
  4276. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  4277. (defun org-toggle-tags-groups ()
  4278. "Toggle support for group tags.
  4279. Support for group tags is controlled by the option
  4280. `org-group-tags', which is non-nil by default."
  4281. (interactive)
  4282. (setq org-group-tags (not org-group-tags))
  4283. (cond ((and (derived-mode-p 'org-agenda-mode)
  4284. org-group-tags)
  4285. (org-agenda-redo))
  4286. ((derived-mode-p 'org-mode)
  4287. (let ((org-inhibit-startup t)) (org-mode))))
  4288. (message "Groups tags support has been turned %s"
  4289. (if org-group-tags "on" "off")))
  4290. (defun org-set-regexps-and-options (&optional tags-only)
  4291. "Precompute regular expressions used in the current buffer.
  4292. When optional argument TAGS-ONLY is non-nil, only compute tags
  4293. related expressions."
  4294. (when (derived-mode-p 'org-mode)
  4295. (let ((alist (org--setup-collect-keywords
  4296. (org-make-options-regexp
  4297. (append '("FILETAGS" "TAGS" "SETUPFILE")
  4298. (and (not tags-only)
  4299. '("ARCHIVE" "CATEGORY" "COLUMNS" "CONSTANTS"
  4300. "LINK" "OPTIONS" "PRIORITIES" "PROPERTY"
  4301. "SEQ_TODO" "STARTUP" "TODO" "TYP_TODO")))))))
  4302. (org--setup-process-tags
  4303. (cdr (assq 'tags alist)) (cdr (assq 'filetags alist)))
  4304. (unless tags-only
  4305. ;; File properties.
  4306. (setq-local org-file-properties (cdr (assq 'property alist)))
  4307. ;; Archive location.
  4308. (let ((archive (cdr (assq 'archive alist))))
  4309. (when archive (setq-local org-archive-location archive)))
  4310. ;; Category.
  4311. (let ((cat (org-string-nw-p (cdr (assq 'category alist)))))
  4312. (when cat
  4313. (setq-local org-category (intern cat))
  4314. (setq-local org-file-properties
  4315. (org--update-property-plist
  4316. "CATEGORY" cat org-file-properties))))
  4317. ;; Columns.
  4318. (let ((column (cdr (assq 'columns alist))))
  4319. (when column (setq-local org-columns-default-format column)))
  4320. ;; Constants.
  4321. (setq org-table-formula-constants-local (cdr (assq 'constants alist)))
  4322. ;; Link abbreviations.
  4323. (let ((links (cdr (assq 'link alist))))
  4324. (when links (setq org-link-abbrev-alist-local (nreverse links))))
  4325. ;; Priorities.
  4326. (let ((priorities (cdr (assq 'priorities alist))))
  4327. (when priorities
  4328. (setq-local org-highest-priority (nth 0 priorities))
  4329. (setq-local org-lowest-priority (nth 1 priorities))
  4330. (setq-local org-default-priority (nth 2 priorities))))
  4331. ;; Scripts.
  4332. (let ((scripts (assq 'scripts alist)))
  4333. (when scripts
  4334. (setq-local org-use-sub-superscripts (cdr scripts))))
  4335. ;; Startup options.
  4336. (let ((startup (cdr (assq 'startup alist))))
  4337. (dolist (option startup)
  4338. (let ((entry (assoc-string option org-startup-options t)))
  4339. (when entry
  4340. (let ((var (nth 1 entry))
  4341. (val (nth 2 entry)))
  4342. (if (not (nth 3 entry)) (set (make-local-variable var) val)
  4343. (unless (listp (symbol-value var))
  4344. (set (make-local-variable var) nil))
  4345. (add-to-list var val)))))))
  4346. ;; TODO keywords.
  4347. (setq-local org-todo-kwd-alist nil)
  4348. (setq-local org-todo-key-alist nil)
  4349. (setq-local org-todo-key-trigger nil)
  4350. (setq-local org-todo-keywords-1 nil)
  4351. (setq-local org-done-keywords nil)
  4352. (setq-local org-todo-heads nil)
  4353. (setq-local org-todo-sets nil)
  4354. (setq-local org-todo-log-states nil)
  4355. (let ((todo-sequences
  4356. (or (nreverse (cdr (assq 'todo alist)))
  4357. (let ((d (default-value 'org-todo-keywords)))
  4358. (if (not (stringp (car d))) d
  4359. ;; XXX: Backward compatibility code.
  4360. (list (cons org-todo-interpretation d)))))))
  4361. (dolist (sequence todo-sequences)
  4362. (let* ((sequence (or (run-hook-with-args-until-success
  4363. 'org-todo-setup-filter-hook sequence)
  4364. sequence))
  4365. (sequence-type (car sequence))
  4366. (keywords (cdr sequence))
  4367. (sep (member "|" keywords))
  4368. names alist)
  4369. (dolist (k (remove "|" keywords))
  4370. (unless (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$"
  4371. k)
  4372. (error "Invalid TODO keyword %s" k))
  4373. (let ((name (match-string 1 k))
  4374. (key (match-string 2 k))
  4375. (log (org-extract-log-state-settings k)))
  4376. (push name names)
  4377. (push (cons name (and key (string-to-char key))) alist)
  4378. (when log (push log org-todo-log-states))))
  4379. (let* ((names (nreverse names))
  4380. (done (if sep (org-remove-keyword-keys (cdr sep))
  4381. (last names)))
  4382. (head (car names))
  4383. (tail (list sequence-type head (car done) (org-last done))))
  4384. (add-to-list 'org-todo-heads head 'append)
  4385. (push names org-todo-sets)
  4386. (setq org-done-keywords (append org-done-keywords done nil))
  4387. (setq org-todo-keywords-1 (append org-todo-keywords-1 names nil))
  4388. (setq org-todo-key-alist
  4389. (append org-todo-key-alist
  4390. (and alist
  4391. (append '((:startgroup))
  4392. (nreverse alist)
  4393. '((:endgroup))))))
  4394. (dolist (k names) (push (cons k tail) org-todo-kwd-alist))))))
  4395. (setq org-todo-sets (nreverse org-todo-sets)
  4396. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  4397. org-todo-key-trigger (delq nil (mapcar #'cdr org-todo-key-alist))
  4398. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist))
  4399. ;; Compute the regular expressions and other local variables.
  4400. ;; Using `org-outline-regexp-bol' would complicate them much,
  4401. ;; because of the fixed white space at the end of that string.
  4402. (unless org-done-keywords
  4403. (setq org-done-keywords
  4404. (and org-todo-keywords-1 (last org-todo-keywords-1))))
  4405. (setq org-not-done-keywords
  4406. (org-delete-all org-done-keywords
  4407. (copy-sequence org-todo-keywords-1))
  4408. org-todo-regexp (regexp-opt org-todo-keywords-1 t)
  4409. org-not-done-regexp (regexp-opt org-not-done-keywords t)
  4410. org-not-done-heading-regexp
  4411. (format org-heading-keyword-regexp-format org-not-done-regexp)
  4412. org-todo-line-regexp
  4413. (format org-heading-keyword-maybe-regexp-format org-todo-regexp)
  4414. org-complex-heading-regexp
  4415. (concat "^\\(\\*+\\)"
  4416. "\\(?: +" org-todo-regexp "\\)?"
  4417. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4418. "\\(?: +\\(.*?\\)\\)??"
  4419. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?")
  4420. "[ \t]*$")
  4421. org-complex-heading-regexp-format
  4422. (concat "^\\(\\*+\\)"
  4423. "\\(?: +" org-todo-regexp "\\)?"
  4424. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4425. "\\(?: +"
  4426. ;; Stats cookies can be stuck to body.
  4427. "\\(?:\\[[0-9%%/]+\\] *\\)*"
  4428. "\\(%s\\)"
  4429. "\\(?: *\\[[0-9%%/]+\\]\\)*"
  4430. "\\)"
  4431. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%%:]+:\\)\\)?")
  4432. "[ \t]*$")
  4433. org-todo-line-tags-regexp
  4434. (concat "^\\(\\*+\\)"
  4435. "\\(?: +" org-todo-regexp "\\)?"
  4436. "\\(?: +\\(.*?\\)\\)??"
  4437. (org-re "\\(?:[ \t]+\\(:[[:alnum:]:_@#%]+:\\)\\)?")
  4438. "[ \t]*$"))
  4439. (org-compute-latex-and-related-regexp)))))
  4440. (defun org--setup-collect-keywords (regexp &optional files alist)
  4441. "Return setup keywords values as an alist.
  4442. REGEXP matches a subset of setup keywords. FILES is a list of
  4443. file names already visited. It is used to avoid circular setup
  4444. files. ALIST, when non-nil, is the alist computed so far.
  4445. Return value contains the following keys: `archive', `category',
  4446. `columns', `constants', `filetags', `link', `priorities',
  4447. `property', `scripts', `startup', `tags' and `todo'."
  4448. (org-with-wide-buffer
  4449. (goto-char (point-min))
  4450. (let ((case-fold-search t))
  4451. (while (re-search-forward regexp nil t)
  4452. (let ((element (org-element-at-point)))
  4453. (when (eq (org-element-type element) 'keyword)
  4454. (let ((key (org-element-property :key element))
  4455. (value (org-element-property :value element)))
  4456. (cond
  4457. ((equal key "ARCHIVE")
  4458. (when (org-string-nw-p value)
  4459. (push (cons 'archive value) alist)))
  4460. ((equal key "CATEGORY") (push (cons 'category value) alist))
  4461. ((equal key "COLUMNS") (push (cons 'columns value) alist))
  4462. ((equal key "CONSTANTS")
  4463. (let* ((constants (assq 'constants alist))
  4464. (store (cdr constants)))
  4465. (dolist (pair (org-split-string value))
  4466. (when (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)"
  4467. pair)
  4468. (let* ((name (match-string 1 pair))
  4469. (value (match-string 2 pair))
  4470. (old (assoc name store)))
  4471. (if old (setcdr old value)
  4472. (push (cons name value) store)))))
  4473. (if constants (setcdr constants store)
  4474. (push (cons 'constants store) alist))))
  4475. ((equal key "FILETAGS")
  4476. (when (org-string-nw-p value)
  4477. (let ((old (assq 'filetags alist))
  4478. (new (apply #'nconc
  4479. (mapcar (lambda (x) (org-split-string x ":"))
  4480. (org-split-string value)))))
  4481. (if old (setcdr old (append new (cdr old)))
  4482. (push (cons 'filetags new) alist)))))
  4483. ((equal key "LINK")
  4484. (when (string-match "\\`\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  4485. (let ((links (assq 'link alist))
  4486. (pair (cons (org-match-string-no-properties 1 value)
  4487. (org-match-string-no-properties 2 value))))
  4488. (if links (push pair (cdr links))
  4489. (push (list 'link pair) alist)))))
  4490. ((equal key "OPTIONS")
  4491. (when (and (org-string-nw-p value)
  4492. (string-match "\\^:\\(t\\|nil\\|{}\\)" value))
  4493. (push (cons 'scripts (read (match-string 1 value))) alist)))
  4494. ((equal key "PRIORITIES")
  4495. (push (cons 'priorities
  4496. (let ((prio (org-split-string value)))
  4497. (if (< (length prio) 3) '(?A ?C ?B)
  4498. (mapcar #'string-to-char prio))))
  4499. alist))
  4500. ((equal key "PROPERTY")
  4501. (when (string-match "\\(\\S-+\\)[ \t]+\\(.*\\)" value)
  4502. (let* ((property (assq 'property alist))
  4503. (value (org--update-property-plist
  4504. (org-match-string-no-properties 1 value)
  4505. (org-match-string-no-properties 2 value)
  4506. (cdr property))))
  4507. (if property (setcdr property value)
  4508. (push (cons 'property value) alist)))))
  4509. ((equal key "STARTUP")
  4510. (let ((startup (assq 'startup alist)))
  4511. (if startup
  4512. (setcdr startup
  4513. (append (cdr startup) (org-split-string value)))
  4514. (push (cons 'startup (org-split-string value)) alist))))
  4515. ((equal key "TAGS")
  4516. (let ((tag-cell (assq 'tags alist)))
  4517. (if tag-cell
  4518. (setcdr tag-cell
  4519. (append (cdr tag-cell)
  4520. '("\\n")
  4521. (org-split-string value)))
  4522. (push (cons 'tags (org-split-string value)) alist))))
  4523. ((member key '("TODO" "SEQ_TODO" "TYP_TODO"))
  4524. (let ((todo (assq 'todo alist))
  4525. (value (cons (if (equal key "TYP_TODO") 'type 'sequence)
  4526. (org-split-string value))))
  4527. (if todo (push value (cdr todo))
  4528. (push (list 'todo value) alist))))
  4529. ((equal key "SETUPFILE")
  4530. (unless buffer-read-only ; Do not check in Gnus messages.
  4531. (let ((f (and (org-string-nw-p value)
  4532. (expand-file-name
  4533. (org-remove-double-quotes value)))))
  4534. (when (and f (file-readable-p f) (not (member f files)))
  4535. (with-temp-buffer
  4536. (setq default-directory (file-name-directory f))
  4537. (insert-file-contents f)
  4538. (setq alist
  4539. ;; Fake Org mode to benefit from cache
  4540. ;; without recurring needlessly.
  4541. (let ((major-mode 'org-mode))
  4542. (org--setup-collect-keywords
  4543. regexp (cons f files) alist)))))))))))))))
  4544. alist)
  4545. (defun org--setup-process-tags (tags filetags)
  4546. "Precompute variables used for tags.
  4547. TAGS is a list of tags and tag group symbols, as strings.
  4548. FILETAGS is a list of tags, as strings."
  4549. ;; Process the file tags.
  4550. (setq-local org-file-tags
  4551. (mapcar #'org-add-prop-inherited filetags))
  4552. ;; Provide default tags if no local tags are found.
  4553. (when (and (not tags) org-tag-alist)
  4554. (setq tags
  4555. (mapcar (lambda (tag)
  4556. (case (car tag)
  4557. (:startgroup "{")
  4558. (:endgroup "}")
  4559. (:startgrouptag "[")
  4560. (:endgrouptag "]")
  4561. (:grouptags ":")
  4562. (:newline "\\n")
  4563. (otherwise (concat (car tag)
  4564. (and (characterp (cdr tag))
  4565. (format "(%c)" (cdr tag)))))))
  4566. org-tag-alist)))
  4567. ;; Process the tags.
  4568. (setq-local org-tag-groups-alist nil)
  4569. (setq-local org-tag-alist nil)
  4570. (let (group-flag)
  4571. (while tags
  4572. (let ((e (car tags)))
  4573. (setq tags (cdr tags))
  4574. (cond
  4575. ((equal e "{")
  4576. (push '(:startgroup) org-tag-alist)
  4577. (when (equal (nth 1 tags) ":") (setq group-flag t)))
  4578. ((equal e "}")
  4579. (push '(:endgroup) org-tag-alist)
  4580. (setq group-flag nil))
  4581. ((equal e "[")
  4582. (push '(:startgrouptag) org-tag-alist)
  4583. (when (equal (nth 1 tags) ":") (setq group-flag t)))
  4584. ((equal e "]")
  4585. (push '(:endgrouptag) org-tag-alist)
  4586. (setq group-flag nil))
  4587. ((equal e ":")
  4588. (push '(:grouptags) org-tag-alist)
  4589. (setq group-flag 'append))
  4590. ((equal e "\\n") (push '(:newline) org-tag-alist))
  4591. ((string-match
  4592. (org-re (concat "\\`\\([[:alnum:]_@#%]+"
  4593. "\\|{.+?}\\)" ; regular expression
  4594. "\\(?:(\\(.\\))\\)?\\'")) e)
  4595. (let ((tag (match-string 1 e))
  4596. (key (and (match-beginning 2)
  4597. (string-to-char (match-string 2 e)))))
  4598. (cond ((eq group-flag 'append)
  4599. (setcar org-tag-groups-alist
  4600. (append (car org-tag-groups-alist) (list tag))))
  4601. (group-flag (push (list tag) org-tag-groups-alist)))
  4602. ;; Push all tags in groups, no matter if they already exist.
  4603. (unless (and (not group-flag) (assoc tag org-tag-alist))
  4604. (push (cons tag key) org-tag-alist))))))))
  4605. (setq org-tag-alist (nreverse org-tag-alist)))
  4606. (defun org-file-contents (file &optional noerror)
  4607. "Return the contents of FILE, as a string."
  4608. (if (and file (file-readable-p file))
  4609. (with-temp-buffer
  4610. (insert-file-contents file)
  4611. (buffer-string))
  4612. (funcall (if noerror 'message 'error)
  4613. "Cannot read file \"%s\"%s"
  4614. file
  4615. (let ((from (buffer-file-name (buffer-base-buffer))))
  4616. (if from (concat " (referenced in file \"" from "\")") "")))))
  4617. (defun org-extract-log-state-settings (x)
  4618. "Extract the log state setting from a TODO keyword string.
  4619. This will extract info from a string like \"WAIT(w@/!)\"."
  4620. (let (kw key log1 log2)
  4621. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  4622. (setq kw (match-string 1 x)
  4623. key (and (match-end 2) (match-string 2 x))
  4624. log1 (and (match-end 3) (match-string 3 x))
  4625. log2 (and (match-end 4) (match-string 4 x)))
  4626. (and (or log1 log2)
  4627. (list kw
  4628. (and log1 (if (equal log1 "!") 'time 'note))
  4629. (and log2 (if (equal log2 "!") 'time 'note)))))))
  4630. (defun org-remove-keyword-keys (list)
  4631. "Remove a pair of parenthesis at the end of each string in LIST."
  4632. (mapcar (lambda (x)
  4633. (if (string-match "(.*)$" x)
  4634. (substring x 0 (match-beginning 0))
  4635. x))
  4636. list))
  4637. (defun org-assign-fast-keys (alist)
  4638. "Assign fast keys to a keyword-key alist.
  4639. Respect keys that are already there."
  4640. (let (new e (alt ?0))
  4641. (while (setq e (pop alist))
  4642. (if (or (memq (car e) '(:newline :grouptags :endgroup :startgroup))
  4643. (cdr e)) ;; Key already assigned.
  4644. (push e new)
  4645. (let ((clist (string-to-list (downcase (car e))))
  4646. (used (append new alist)))
  4647. (when (= (car clist) ?@)
  4648. (pop clist))
  4649. (while (and clist (rassoc (car clist) used))
  4650. (pop clist))
  4651. (unless clist
  4652. (while (rassoc alt used)
  4653. (incf alt)))
  4654. (push (cons (car e) (or (car clist) alt)) new))))
  4655. (nreverse new)))
  4656. ;;; Some variables used in various places
  4657. (defvar org-window-configuration nil
  4658. "Used in various places to store a window configuration.")
  4659. (defvar org-selected-window nil
  4660. "Used in various places to store a window configuration.")
  4661. (defvar org-finish-function nil
  4662. "Function to be called when `C-c C-c' is used.
  4663. This is for getting out of special buffers like capture.")
  4664. ;; FIXME: Occasionally check by commenting these, to make sure
  4665. ;; no other functions uses these, forgetting to let-bind them.
  4666. (org-no-warnings (defvar entry)) ;; unprefixed, from calendar.el
  4667. (defvar org-last-state)
  4668. (org-no-warnings (defvar date)) ;; unprefixed, from calendar.el
  4669. ;; Defined somewhere in this file, but used before definition.
  4670. (defvar org-entities) ;; defined in org-entities.el
  4671. (defvar org-struct-menu)
  4672. (defvar org-org-menu)
  4673. (defvar org-tbl-menu)
  4674. ;;;; Define the Org-mode
  4675. ;; We use a before-change function to check if a table might need
  4676. ;; an update.
  4677. (defvar org-table-may-need-update t
  4678. "Indicates that a table might need an update.
  4679. This variable is set by `org-before-change-function'.
  4680. `org-table-align' sets it back to nil.")
  4681. (defun org-before-change-function (beg end)
  4682. "Every change indicates that a table might need an update."
  4683. (setq org-table-may-need-update t))
  4684. (defvar org-mode-map)
  4685. (defvar org-inhibit-startup-visibility-stuff nil) ; Dynamically-scoped param.
  4686. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  4687. (defvar org-inhibit-logging nil) ; Dynamically-scoped param.
  4688. (defvar org-inhibit-blocking nil) ; Dynamically-scoped param.
  4689. (defvar org-table-buffer-is-an nil)
  4690. (defvar bidi-paragraph-direction)
  4691. (defvar buffer-face-mode-face)
  4692. (require 'outline)
  4693. (when (and (not (keymapp outline-mode-map)) (featurep 'allout))
  4694. (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"))
  4695. (require 'noutline "noutline" 'noerror) ;; stock XEmacs does not have it
  4696. ;; Other stuff we need.
  4697. (require 'time-date)
  4698. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  4699. (require 'easymenu)
  4700. (autoload 'easy-menu-add "easymenu")
  4701. (require 'overlay)
  4702. ;; (require 'org-macs) moved higher up in the file before it is first used
  4703. (require 'org-entities)
  4704. ;; (require 'org-compat) moved higher up in the file before it is first used
  4705. (require 'org-faces)
  4706. (require 'org-list)
  4707. (require 'org-pcomplete)
  4708. (require 'org-src)
  4709. (require 'org-footnote)
  4710. (require 'org-macro)
  4711. ;; babel
  4712. (require 'ob)
  4713. ;;;###autoload
  4714. (define-derived-mode org-mode outline-mode "Org"
  4715. "Outline-based notes management and organizer, alias
  4716. \"Carsten's outline-mode for keeping track of everything.\"
  4717. Org-mode develops organizational tasks around a NOTES file which
  4718. contains information about projects as plain text. Org-mode is
  4719. implemented on top of outline-mode, which is ideal to keep the content
  4720. of large files well structured. It supports ToDo items, deadlines and
  4721. time stamps, which magically appear in the diary listing of the Emacs
  4722. calendar. Tables are easily created with a built-in table editor.
  4723. Plain text URL-like links connect to websites, emails (VM), Usenet
  4724. messages (Gnus), BBDB entries, and any files related to the project.
  4725. For printing and sharing of notes, an Org-mode file (or a part of it)
  4726. can be exported as a structured ASCII or HTML file.
  4727. The following commands are available:
  4728. \\{org-mode-map}"
  4729. ;; Get rid of Outline menus, they are not needed
  4730. ;; Need to do this here because define-derived-mode sets up
  4731. ;; the keymap so late. Still, it is a waste to call this each time
  4732. ;; we switch another buffer into org-mode.
  4733. (if (featurep 'xemacs)
  4734. (when (boundp 'outline-mode-menu-heading)
  4735. ;; Assume this is Greg's port, it uses easymenu
  4736. (easy-menu-remove outline-mode-menu-heading)
  4737. (easy-menu-remove outline-mode-menu-show)
  4738. (easy-menu-remove outline-mode-menu-hide))
  4739. (define-key org-mode-map [menu-bar headings] 'undefined)
  4740. (define-key org-mode-map [menu-bar hide] 'undefined)
  4741. (define-key org-mode-map [menu-bar show] 'undefined))
  4742. (org-load-modules-maybe)
  4743. (when (featurep 'xemacs)
  4744. (easy-menu-add org-org-menu)
  4745. (easy-menu-add org-tbl-menu))
  4746. (org-install-agenda-files-menu)
  4747. (when org-descriptive-links (add-to-invisibility-spec '(org-link)))
  4748. (add-to-invisibility-spec '(org-cwidth))
  4749. (add-to-invisibility-spec '(org-hide-block . t))
  4750. (when (featurep 'xemacs)
  4751. (setq-local line-move-ignore-invisible t))
  4752. (setq-local outline-regexp org-outline-regexp)
  4753. (setq-local outline-level 'org-outline-level)
  4754. (setq bidi-paragraph-direction 'left-to-right)
  4755. (when (and org-ellipsis
  4756. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  4757. (fboundp 'make-glyph-code))
  4758. (unless org-display-table
  4759. (setq org-display-table (make-display-table)))
  4760. (set-display-table-slot
  4761. org-display-table 4
  4762. (vconcat (mapcar
  4763. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  4764. org-ellipsis)))
  4765. (if (stringp org-ellipsis) org-ellipsis "..."))))
  4766. (setq buffer-display-table org-display-table))
  4767. (org-set-regexps-and-options)
  4768. (org-set-font-lock-defaults)
  4769. (when (and org-tag-faces (not org-tags-special-faces-re))
  4770. ;; tag faces set outside customize.... force initialization.
  4771. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  4772. ;; Calc embedded
  4773. (setq-local calc-embedded-open-mode "# ")
  4774. ;; Modify a few syntax entries
  4775. (modify-syntax-entry ?@ "w")
  4776. (modify-syntax-entry ?\" "\"")
  4777. (modify-syntax-entry ?\\ "_")
  4778. (modify-syntax-entry ?~ "_")
  4779. (setq-local font-lock-unfontify-region-function 'org-unfontify-region)
  4780. ;; Activate before-change-function
  4781. (setq-local org-table-may-need-update t)
  4782. (org-add-hook 'before-change-functions 'org-before-change-function nil 'local)
  4783. ;; Check for running clock before killing a buffer
  4784. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  4785. ;; Initialize macros templates.
  4786. (org-macro-initialize-templates)
  4787. ;; Initialize radio targets.
  4788. (org-update-radio-target-regexp)
  4789. ;; Indentation.
  4790. (setq-local indent-line-function 'org-indent-line)
  4791. (setq-local indent-region-function 'org-indent-region)
  4792. ;; Filling and auto-filling.
  4793. (org-setup-filling)
  4794. ;; Comments.
  4795. (org-setup-comments-handling)
  4796. ;; Initialize cache.
  4797. (org-element-cache-reset)
  4798. ;; Beginning/end of defun
  4799. (setq-local beginning-of-defun-function 'org-backward-element)
  4800. (setq-local end-of-defun-function
  4801. (lambda ()
  4802. (if (not (org-at-heading-p))
  4803. (org-forward-element)
  4804. (org-forward-element)
  4805. (forward-char -1))))
  4806. ;; Next error for sparse trees
  4807. (setq-local next-error-function 'org-occur-next-match)
  4808. ;; Make sure dependence stuff works reliably, even for users who set it
  4809. ;; too late :-(
  4810. (if org-enforce-todo-dependencies
  4811. (add-hook 'org-blocker-hook
  4812. 'org-block-todo-from-children-or-siblings-or-parent)
  4813. (remove-hook 'org-blocker-hook
  4814. 'org-block-todo-from-children-or-siblings-or-parent))
  4815. (if org-enforce-todo-checkbox-dependencies
  4816. (add-hook 'org-blocker-hook
  4817. 'org-block-todo-from-checkboxes)
  4818. (remove-hook 'org-blocker-hook
  4819. 'org-block-todo-from-checkboxes))
  4820. ;; Align options lines
  4821. (setq-local
  4822. align-mode-rules-list
  4823. '((org-in-buffer-settings
  4824. (regexp . "^[ \t]*#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  4825. (modes . '(org-mode)))))
  4826. ;; Imenu
  4827. (setq-local imenu-create-index-function 'org-imenu-get-tree)
  4828. ;; Make isearch reveal context
  4829. (if (or (featurep 'xemacs)
  4830. (not (boundp 'outline-isearch-open-invisible-function)))
  4831. ;; Emacs 21 and XEmacs make use of the hook
  4832. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  4833. ;; Emacs 22 deals with this through a special variable
  4834. (setq-local outline-isearch-open-invisible-function
  4835. (lambda (&rest ignore) (org-show-context 'isearch))))
  4836. ;; Setup the pcomplete hooks
  4837. (setq-local pcomplete-command-completion-function 'org-pcomplete-initial)
  4838. (setq-local pcomplete-command-name-function 'org-command-at-point)
  4839. (setq-local pcomplete-default-completion-function 'ignore)
  4840. (setq-local pcomplete-parse-arguments-function 'org-parse-arguments)
  4841. (setq-local pcomplete-termination-string "")
  4842. (setq-local buffer-face-mode-face 'org-default)
  4843. ;; If empty file that did not turn on Org mode automatically, make
  4844. ;; it to.
  4845. (when (and org-insert-mode-line-in-empty-file
  4846. (org-called-interactively-p 'any)
  4847. (= (point-min) (point-max)))
  4848. (insert "# -*- mode: org -*-\n\n"))
  4849. (unless org-inhibit-startup
  4850. (org-unmodified
  4851. (when org-startup-with-beamer-mode (org-beamer-mode))
  4852. (when org-startup-align-all-tables
  4853. (org-table-map-tables #'org-table-align t))
  4854. (when org-startup-with-inline-images (org-display-inline-images))
  4855. (when org-startup-with-latex-preview (org-toggle-latex-fragment))
  4856. (unless org-inhibit-startup-visibility-stuff (org-set-startup-visibility))
  4857. (when org-startup-truncated (setq truncate-lines t))
  4858. (when org-startup-indented (require 'org-indent) (org-indent-mode 1))
  4859. (org-refresh-effort-properties)))
  4860. ;; Try to set `org-hide' face correctly.
  4861. (let ((foreground (org-find-invisible-foreground)))
  4862. (when foreground
  4863. (set-face-foreground 'org-hide foreground))))
  4864. ;; Update `customize-package-emacs-version-alist'
  4865. (add-to-list 'customize-package-emacs-version-alist
  4866. '(Org ("6.21b" . "23.1") ("6.33x" . "23.2")
  4867. ("7.8.11" . "24.1") ("7.9.4" . "24.3")
  4868. ("8.2.6" . "24.4") ("8.3" . "25.1")))
  4869. (defvar org-mode-transpose-word-syntax-table
  4870. (let ((st (make-syntax-table text-mode-syntax-table)))
  4871. (mapc (lambda(c) (modify-syntax-entry
  4872. (string-to-char (car c)) "w p" st))
  4873. org-emphasis-alist)
  4874. st))
  4875. (when (fboundp 'abbrev-table-put)
  4876. (abbrev-table-put org-mode-abbrev-table
  4877. :parents (list text-mode-abbrev-table)))
  4878. (defun org-find-invisible-foreground ()
  4879. (let ((candidates (remove
  4880. "unspecified-bg"
  4881. (nconc
  4882. (list (face-background 'default)
  4883. (face-background 'org-default))
  4884. (mapcar
  4885. (lambda (alist)
  4886. (when (boundp alist)
  4887. (cdr (assoc 'background-color (symbol-value alist)))))
  4888. '(default-frame-alist initial-frame-alist window-system-default-frame-alist))
  4889. (list (face-foreground 'org-hide))))))
  4890. (car (remove nil candidates))))
  4891. (defun org-current-time (&optional rounding-minutes past)
  4892. "Current time, possibly rounded to ROUNDING-MINUTES.
  4893. When ROUNDING-MINUTES is not an integer, fall back on the car of
  4894. `org-time-stamp-rounding-minutes'. When PAST is non-nil, ensure
  4895. the rounding returns a past time."
  4896. (let ((r (or (and (integerp rounding-minutes) rounding-minutes)
  4897. (car org-time-stamp-rounding-minutes)))
  4898. (time (decode-time)) res)
  4899. (if (< r 1)
  4900. (current-time)
  4901. (setq res
  4902. (apply 'encode-time
  4903. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  4904. (nthcdr 2 time))))
  4905. (if (and past (< (org-float-time (time-subtract (current-time) res)) 0))
  4906. (seconds-to-time (- (org-float-time res) (* r 60)))
  4907. res))))
  4908. (defun org-today ()
  4909. "Return today date, considering `org-extend-today-until'."
  4910. (time-to-days
  4911. (time-subtract (current-time)
  4912. (list 0 (* 3600 org-extend-today-until) 0))))
  4913. ;;;; Font-Lock stuff, including the activators
  4914. (defvar org-mouse-map (make-sparse-keymap))
  4915. (org-defkey org-mouse-map [mouse-2] 'org-open-at-mouse)
  4916. (org-defkey org-mouse-map [mouse-3] 'org-find-file-at-mouse)
  4917. (when org-mouse-1-follows-link
  4918. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  4919. (when org-tab-follows-link
  4920. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  4921. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  4922. (require 'font-lock)
  4923. (defconst org-non-link-chars "]\t\n\r<>")
  4924. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "file+emacs"
  4925. "file+sys" "news" "shell" "elisp" "doi" "message"
  4926. "help"))
  4927. (defvar org-link-types-re nil
  4928. "Matches a link that has a url-like prefix like \"http:\"")
  4929. (defvar org-link-re-with-space nil
  4930. "Matches a link with spaces, optional angular brackets around it.")
  4931. (defvar org-link-re-with-space2 nil
  4932. "Matches a link with spaces, optional angular brackets around it.")
  4933. (defvar org-link-re-with-space3 nil
  4934. "Matches a link with spaces, only for internal part in bracket links.")
  4935. (defvar org-angle-link-re nil
  4936. "Matches link with angular brackets, spaces are allowed.")
  4937. (defvar org-plain-link-re nil
  4938. "Matches plain link, without spaces.")
  4939. (defvar org-bracket-link-regexp nil
  4940. "Matches a link in double brackets.")
  4941. (defvar org-bracket-link-analytic-regexp nil
  4942. "Regular expression used to analyze links.
  4943. Here is what the match groups contain after a match:
  4944. 1: http:
  4945. 2: http
  4946. 3: path
  4947. 4: [desc]
  4948. 5: desc")
  4949. (defvar org-bracket-link-analytic-regexp++ nil
  4950. "Like `org-bracket-link-analytic-regexp', but include coderef internal type.")
  4951. (defvar org-any-link-re nil
  4952. "Regular expression matching any link.")
  4953. (defconst org-match-sexp-depth 3
  4954. "Number of stacked braces for sub/superscript matching.")
  4955. (defun org-create-multibrace-regexp (left right n)
  4956. "Create a regular expression which will match a balanced sexp.
  4957. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  4958. as single character strings.
  4959. The regexp returned will match the entire expression including the
  4960. delimiters. It will also define a single group which contains the
  4961. match except for the outermost delimiters. The maximum depth of
  4962. stacked delimiters is N. Escaping delimiters is not possible."
  4963. (let* ((nothing (concat "[^" left right "]*?"))
  4964. (or "\\|")
  4965. (re nothing)
  4966. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  4967. (while (> n 1)
  4968. (setq n (1- n)
  4969. re (concat re or next)
  4970. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  4971. (concat left "\\(" re "\\)" right)))
  4972. (defconst org-match-substring-regexp
  4973. (concat
  4974. "\\(\\S-\\)\\([_^]\\)\\("
  4975. "\\(?:" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4976. "\\|"
  4977. "\\(?:" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  4978. "\\|"
  4979. "\\(?:\\*\\|[+-]?[[:alnum:].,\\]*[[:alnum:]]\\)\\)")
  4980. "The regular expression matching a sub- or superscript.")
  4981. (defconst org-match-substring-with-braces-regexp
  4982. (concat
  4983. "\\(\\S-\\)\\([_^]\\)"
  4984. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)")
  4985. "The regular expression matching a sub- or superscript, forcing braces.")
  4986. (defun org-make-link-regexps ()
  4987. "Update the link regular expressions.
  4988. This should be called after the variable `org-link-types' has changed."
  4989. (let ((types-re (regexp-opt org-link-types t)))
  4990. (setq org-link-types-re
  4991. (concat "\\`" types-re ":")
  4992. org-link-re-with-space
  4993. (concat "<?" types-re ":"
  4994. "\\([^" org-non-link-chars " ]"
  4995. "[^" org-non-link-chars "]*"
  4996. "[^" org-non-link-chars " ]\\)>?")
  4997. org-link-re-with-space2
  4998. (concat "<?" types-re ":"
  4999. "\\([^" org-non-link-chars " ]"
  5000. "[^\t\n\r]*"
  5001. "[^" org-non-link-chars " ]\\)>?")
  5002. org-link-re-with-space3
  5003. (concat "<?" types-re ":"
  5004. "\\([^" org-non-link-chars " ]"
  5005. "[^\t\n\r]*\\)")
  5006. org-angle-link-re
  5007. (format "<%s:\\([^>\n]*\\(?:\n[ \t]*[^> \t\n][^>\n]*\\)*\\)>"
  5008. types-re)
  5009. org-plain-link-re
  5010. (concat
  5011. "\\<" types-re ":"
  5012. (org-re "\\([^ \t\n()<>]+\\(?:([[:word:]0-9_]+)\\|\\([^[:punct:] \t\n]\\|/\\)\\)\\)"))
  5013. ;; "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  5014. org-bracket-link-regexp
  5015. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  5016. org-bracket-link-analytic-regexp
  5017. (concat
  5018. "\\[\\["
  5019. "\\(" types-re ":\\)?"
  5020. "\\([^]]+\\)"
  5021. "\\]"
  5022. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  5023. "\\]")
  5024. org-bracket-link-analytic-regexp++
  5025. (concat
  5026. "\\[\\["
  5027. "\\(" (regexp-opt (cons "coderef" org-link-types) t) ":\\)?"
  5028. "\\([^]]+\\)"
  5029. "\\]"
  5030. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  5031. "\\]")
  5032. org-any-link-re
  5033. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  5034. org-angle-link-re "\\)\\|\\("
  5035. org-plain-link-re "\\)"))))
  5036. (org-make-link-regexps)
  5037. (defvar org-emph-face nil)
  5038. (defun org-do-emphasis-faces (limit)
  5039. "Run through the buffer and emphasize strings."
  5040. (let (rtn a)
  5041. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  5042. (let* ((border (char-after (match-beginning 3)))
  5043. (bre (regexp-quote (char-to-string border))))
  5044. (when (and (not (= border (char-after (match-beginning 4))))
  5045. (not (string-match-p (concat bre ".*" bre)
  5046. (replace-regexp-in-string
  5047. "\n" " "
  5048. (substring (match-string 2) 1 -1)))))
  5049. (setq rtn t)
  5050. (setq a (assoc (match-string 3) org-emphasis-alist))
  5051. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  5052. 'face
  5053. (nth 1 a))
  5054. (and (nth 2 a)
  5055. (org-remove-flyspell-overlays-in
  5056. (match-beginning 0) (match-end 0)))
  5057. (add-text-properties (match-beginning 2) (match-end 2)
  5058. '(font-lock-multiline t org-emphasis t))
  5059. (when org-hide-emphasis-markers
  5060. (add-text-properties (match-end 4) (match-beginning 5)
  5061. '(invisible org-link))
  5062. (add-text-properties (match-beginning 3) (match-end 3)
  5063. '(invisible org-link)))))
  5064. (goto-char (1+ (match-beginning 0))))
  5065. rtn))
  5066. (defun org-emphasize (&optional char)
  5067. "Insert or change an emphasis, i.e. a font like bold or italic.
  5068. If there is an active region, change that region to a new emphasis.
  5069. If there is no region, just insert the marker characters and position
  5070. the cursor between them.
  5071. CHAR should be the marker character. If it is a space, it means to
  5072. remove the emphasis of the selected region.
  5073. If CHAR is not given (for example in an interactive call) it will be
  5074. prompted for."
  5075. (interactive)
  5076. (let ((erc org-emphasis-regexp-components)
  5077. (prompt "")
  5078. (string "") beg end move c s)
  5079. (if (org-region-active-p)
  5080. (setq beg (region-beginning) end (region-end)
  5081. string (buffer-substring beg end))
  5082. (setq move t))
  5083. (unless char
  5084. (message "Emphasis marker or tag: [%s]"
  5085. (mapconcat (lambda(e) (car e)) org-emphasis-alist ""))
  5086. (setq char (read-char-exclusive)))
  5087. (if (equal char ?\ )
  5088. (setq s "" move nil)
  5089. (unless (assoc (char-to-string char) org-emphasis-alist)
  5090. (user-error "No such emphasis marker: \"%c\"" char))
  5091. (setq s (char-to-string char)))
  5092. (while (and (> (length string) 1)
  5093. (equal (substring string 0 1) (substring string -1))
  5094. (assoc (substring string 0 1) org-emphasis-alist))
  5095. (setq string (substring string 1 -1)))
  5096. (setq string (concat s string s))
  5097. (when beg (delete-region beg end))
  5098. (unless (or (bolp)
  5099. (string-match (concat "[" (nth 0 erc) "\n]")
  5100. (char-to-string (char-before (point)))))
  5101. (insert " "))
  5102. (unless (or (eobp)
  5103. (string-match (concat "[" (nth 1 erc) "\n]")
  5104. (char-to-string (char-after (point)))))
  5105. (insert " ") (backward-char 1))
  5106. (insert string)
  5107. (and move (backward-char 1))))
  5108. (defconst org-nonsticky-props
  5109. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text htmlize-link))
  5110. (defsubst org-rear-nonsticky-at (pos)
  5111. (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
  5112. (defun org-activate-plain-links (limit)
  5113. "Add link properties for plain links."
  5114. (let (f hl)
  5115. (when (and (re-search-forward (concat org-plain-link-re) limit t)
  5116. (not (member 'org-tag
  5117. (get-text-property (1- (match-beginning 0)) 'face)))
  5118. (not (org-in-src-block-p)))
  5119. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5120. (setq f (get-text-property (match-beginning 0) 'face))
  5121. (setq hl (org-match-string-no-properties 0))
  5122. (unless (or (eq f 'org-tag)
  5123. (and (listp f) (memq 'org-tag f)))
  5124. (add-text-properties (match-beginning 0) (match-end 0)
  5125. (list 'mouse-face 'highlight
  5126. 'face 'org-link
  5127. 'htmlize-link `(:uri ,hl)
  5128. 'keymap org-mouse-map))
  5129. (org-rear-nonsticky-at (match-end 0)))
  5130. t)))
  5131. (defun org-activate-code (limit)
  5132. (when (re-search-forward "^[ \t]*\\(:\\(?: .*\\|$\\)\n?\\)" limit t)
  5133. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5134. (remove-text-properties (match-beginning 0) (match-end 0)
  5135. '(display t invisible t intangible t))
  5136. t))
  5137. (defcustom org-src-fontify-natively t
  5138. "When non-nil, fontify code in code blocks."
  5139. :type 'boolean
  5140. :version "24.4"
  5141. :package-version '(Org . "8.3")
  5142. :group 'org-appearance
  5143. :group 'org-babel)
  5144. (defcustom org-allow-promoting-top-level-subtree nil
  5145. "When non-nil, allow promoting a top level subtree.
  5146. The leading star of the top level headline will be replaced
  5147. by a #."
  5148. :type 'boolean
  5149. :version "24.1"
  5150. :group 'org-appearance)
  5151. (defun org-fontify-meta-lines-and-blocks (limit)
  5152. (condition-case nil
  5153. (org-fontify-meta-lines-and-blocks-1 limit)
  5154. (error (message "org-mode fontification error"))))
  5155. (defun org-fontify-meta-lines-and-blocks-1 (limit)
  5156. "Fontify #+ lines and blocks."
  5157. (let ((case-fold-search t))
  5158. (when (re-search-forward
  5159. "^\\([ \t]*#\\(\\(\\+[a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)[ \t]*\\(\\([^ \t\n]*\\)[ \t]*\\(.*\\)\\)\\)"
  5160. limit t)
  5161. (let ((beg (match-beginning 0))
  5162. (block-start (match-end 0))
  5163. (block-end nil)
  5164. (lang (match-string 7))
  5165. (beg1 (line-beginning-position 2))
  5166. (dc1 (downcase (match-string 2)))
  5167. (dc3 (downcase (match-string 3)))
  5168. end end1 quoting block-type ovl)
  5169. (cond
  5170. ((and (match-end 4) (equal dc3 "+begin"))
  5171. ;; Truly a block
  5172. (setq block-type (downcase (match-string 5))
  5173. quoting (member block-type org-protecting-blocks))
  5174. (when (re-search-forward
  5175. (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
  5176. nil t) ;; on purpose, we look further than LIMIT
  5177. (setq end (min (point-max) (match-end 0))
  5178. end1 (min (point-max) (1- (match-beginning 0))))
  5179. (setq block-end (match-beginning 0))
  5180. (when quoting
  5181. (org-remove-flyspell-overlays-in beg1 end1)
  5182. (remove-text-properties beg end
  5183. '(display t invisible t intangible t)))
  5184. (add-text-properties
  5185. beg end '(font-lock-fontified t font-lock-multiline t))
  5186. (add-text-properties beg beg1 '(face org-meta-line))
  5187. (org-remove-flyspell-overlays-in beg beg1)
  5188. (add-text-properties ; For end_src
  5189. end1 (min (point-max) (1+ end)) '(face org-meta-line))
  5190. (org-remove-flyspell-overlays-in end1 end)
  5191. (cond
  5192. ((and lang (not (string= lang "")) org-src-fontify-natively)
  5193. (org-src-font-lock-fontify-block lang block-start block-end)
  5194. (add-text-properties beg1 block-end '(src-block t)))
  5195. (quoting
  5196. (add-text-properties beg1 (min (point-max) (1+ end1))
  5197. '(face org-block))) ; end of source block
  5198. ((not org-fontify-quote-and-verse-blocks))
  5199. ((string= block-type "quote")
  5200. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-quote)))
  5201. ((string= block-type "verse")
  5202. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-verse))))
  5203. (add-text-properties beg beg1 '(face org-block-begin-line))
  5204. (add-text-properties (min (point-max) (1+ end)) (min (point-max) (1+ end1))
  5205. '(face org-block-end-line))
  5206. t))
  5207. ((member dc1 '("+title:" "+author:" "+email:" "+date:"))
  5208. (org-remove-flyspell-overlays-in
  5209. (match-beginning 0)
  5210. (if (equal "+title:" dc1) (match-end 2) (match-end 0)))
  5211. (add-text-properties
  5212. beg (match-end 3)
  5213. (if (member (intern (substring dc1 1 -1)) org-hidden-keywords)
  5214. '(font-lock-fontified t invisible t)
  5215. '(font-lock-fontified t face org-document-info-keyword)))
  5216. (add-text-properties
  5217. (match-beginning 6) (min (point-max) (1+ (match-end 6)))
  5218. (if (string-equal dc1 "+title:")
  5219. '(font-lock-fontified t face org-document-title)
  5220. '(font-lock-fontified t face org-document-info))))
  5221. ((equal dc1 "+caption:")
  5222. (org-remove-flyspell-overlays-in (match-end 2) (match-end 0))
  5223. (remove-text-properties (match-beginning 0) (match-end 0)
  5224. '(display t invisible t intangible t))
  5225. (add-text-properties (match-beginning 1) (match-end 3)
  5226. '(font-lock-fontified t face org-meta-line))
  5227. (add-text-properties (match-beginning 6) (+ (match-end 6) 1)
  5228. '(font-lock-fontified t face org-block))
  5229. t)
  5230. ((member dc3 '(" " ""))
  5231. (org-remove-flyspell-overlays-in beg (match-end 0))
  5232. (add-text-properties
  5233. beg (match-end 0)
  5234. '(font-lock-fontified t face font-lock-comment-face)))
  5235. (t ;; just any other in-buffer setting, but not indented
  5236. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5237. (remove-text-properties (match-beginning 0) (match-end 0)
  5238. '(display t invisible t intangible t))
  5239. (add-text-properties beg (match-end 0)
  5240. '(font-lock-fontified t face org-meta-line))
  5241. t))))))
  5242. (defun org-fontify-drawers (limit)
  5243. "Fontify drawers."
  5244. (when (re-search-forward org-drawer-regexp limit t)
  5245. (add-text-properties
  5246. (match-beginning 0) (match-end 0)
  5247. '(font-lock-fontified t face org-special-keyword))
  5248. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5249. t))
  5250. (defun org-fontify-macros (limit)
  5251. "Fontify macros."
  5252. (when (re-search-forward "\\({{{\\).+?\\(}}}\\)" limit t)
  5253. (add-text-properties
  5254. (match-beginning 0) (match-end 0)
  5255. '(font-lock-fontified t face org-macro))
  5256. (when org-hide-macro-markers
  5257. (add-text-properties (match-end 2) (match-beginning 2)
  5258. '(invisible t))
  5259. (add-text-properties (match-beginning 1) (match-end 1)
  5260. '(invisible t)))
  5261. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5262. t))
  5263. (defun org-activate-angle-links (limit)
  5264. "Add text properties for angle links."
  5265. (when (and (re-search-forward org-angle-link-re limit t)
  5266. (not (org-in-src-block-p)))
  5267. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5268. (add-text-properties (match-beginning 0) (match-end 0)
  5269. (list 'mouse-face 'highlight
  5270. 'keymap org-mouse-map
  5271. 'font-lock-multiline t))
  5272. (org-rear-nonsticky-at (match-end 0))
  5273. t))
  5274. (defun org-activate-footnote-links (limit)
  5275. "Add text properties for footnotes."
  5276. (let ((fn (org-footnote-next-reference-or-definition limit)))
  5277. (when fn
  5278. (let* ((beg (nth 1 fn))
  5279. (end (nth 2 fn))
  5280. (label (car fn))
  5281. (referencep (/= (line-beginning-position) beg)))
  5282. (when (and referencep (nth 3 fn))
  5283. (save-excursion
  5284. (goto-char beg)
  5285. (search-forward (or label "fn:"))
  5286. (org-remove-flyspell-overlays-in beg (match-end 0))))
  5287. (add-text-properties beg end
  5288. (list 'mouse-face 'highlight
  5289. 'keymap org-mouse-map
  5290. 'help-echo
  5291. (if referencep "Footnote reference"
  5292. "Footnote definition")
  5293. 'font-lock-fontified t
  5294. 'font-lock-multiline t
  5295. 'face 'org-footnote))))))
  5296. (defun org-activate-bracket-links (limit)
  5297. "Add text properties for bracketed links."
  5298. (when (and (re-search-forward org-bracket-link-regexp limit t)
  5299. (not (org-in-src-block-p)))
  5300. (let* ((hl (org-match-string-no-properties 1))
  5301. (help (concat "LINK: " (save-match-data (org-link-unescape hl))))
  5302. (ip (org-maybe-intangible
  5303. (list 'invisible 'org-link
  5304. 'keymap org-mouse-map 'mouse-face 'highlight
  5305. 'font-lock-multiline t 'help-echo help
  5306. 'htmlize-link `(:uri ,hl))))
  5307. (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
  5308. 'font-lock-multiline t 'help-echo help
  5309. 'htmlize-link `(:uri ,hl))))
  5310. ;; We need to remove the invisible property here. Table narrowing
  5311. ;; may have made some of this invisible.
  5312. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5313. (remove-text-properties (match-beginning 0) (match-end 0)
  5314. '(invisible nil))
  5315. (if (match-end 3)
  5316. (progn
  5317. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  5318. (org-rear-nonsticky-at (match-beginning 3))
  5319. (add-text-properties (match-beginning 3) (match-end 3) vp)
  5320. (org-rear-nonsticky-at (match-end 3))
  5321. (add-text-properties (match-end 3) (match-end 0) ip)
  5322. (org-rear-nonsticky-at (match-end 0)))
  5323. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  5324. (org-rear-nonsticky-at (match-beginning 1))
  5325. (add-text-properties (match-beginning 1) (match-end 1) vp)
  5326. (org-rear-nonsticky-at (match-end 1))
  5327. (add-text-properties (match-end 1) (match-end 0) ip)
  5328. (org-rear-nonsticky-at (match-end 0)))
  5329. t)))
  5330. (defun org-activate-dates (limit)
  5331. "Add text properties for dates."
  5332. (when (and (re-search-forward org-tsr-regexp-both limit t)
  5333. (not (equal (char-before (match-beginning 0)) 91)))
  5334. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5335. (add-text-properties (match-beginning 0) (match-end 0)
  5336. (list 'mouse-face 'highlight
  5337. 'keymap org-mouse-map))
  5338. (org-rear-nonsticky-at (match-end 0))
  5339. (when org-display-custom-times
  5340. (if (match-end 3)
  5341. (org-display-custom-time (match-beginning 3) (match-end 3))
  5342. (org-display-custom-time (match-beginning 1) (match-end 1))))
  5343. t))
  5344. (defvar-local org-target-link-regexp nil
  5345. "Regular expression matching radio targets in plain text.")
  5346. (defconst org-target-regexp (let ((border "[^<>\n\r \t]"))
  5347. (format "<<\\(%s\\|%s[^<>\n\r]*%s\\)>>"
  5348. border border border))
  5349. "Regular expression matching a link target.")
  5350. (defconst org-radio-target-regexp (format "<%s>" org-target-regexp)
  5351. "Regular expression matching a radio target.")
  5352. (defconst org-any-target-regexp
  5353. (format "%s\\|%s" org-radio-target-regexp org-target-regexp)
  5354. "Regular expression matching any target.")
  5355. (defun org-activate-target-links (limit)
  5356. "Add text properties for target matches."
  5357. (when org-target-link-regexp
  5358. (let ((case-fold-search t))
  5359. (when (re-search-forward org-target-link-regexp limit t)
  5360. (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
  5361. (add-text-properties (match-beginning 1) (match-end 1)
  5362. (list 'mouse-face 'highlight
  5363. 'keymap org-mouse-map
  5364. 'help-echo "Radio target link"
  5365. 'org-linked-text t))
  5366. (org-rear-nonsticky-at (match-end 1))
  5367. t))))
  5368. (defun org-update-radio-target-regexp ()
  5369. "Find all radio targets in this file and update the regular expression.
  5370. Also refresh fontification if needed."
  5371. (interactive)
  5372. (let ((old-regexp org-target-link-regexp)
  5373. (before-re "\\(?:^\\|[^[:alnum:]]\\)\\(")
  5374. (after-re "\\)\\(?:$\\|[^[:alnum:]]\\)")
  5375. (targets
  5376. (org-with-wide-buffer
  5377. (goto-char (point-min))
  5378. (let (rtn)
  5379. (while (re-search-forward org-radio-target-regexp nil t)
  5380. ;; Make sure point is really within the object.
  5381. (backward-char)
  5382. (let ((obj (org-element-context)))
  5383. (when (eq (org-element-type obj) 'radio-target)
  5384. (add-to-list 'rtn (org-element-property :value obj)))))
  5385. rtn))))
  5386. (setq org-target-link-regexp
  5387. (and targets
  5388. (concat before-re
  5389. (mapconcat
  5390. (lambda (x)
  5391. (replace-regexp-in-string
  5392. " +" "\\s-+" (regexp-quote x) t t))
  5393. targets
  5394. "\\|")
  5395. after-re)))
  5396. (unless (equal old-regexp org-target-link-regexp)
  5397. ;; Clean-up cache.
  5398. (let ((regexp (cond ((not old-regexp) org-target-link-regexp)
  5399. ((not org-target-link-regexp) old-regexp)
  5400. (t
  5401. (concat before-re
  5402. (mapconcat
  5403. (lambda (re)
  5404. (substring re (length before-re)
  5405. (- (length after-re))))
  5406. (list old-regexp org-target-link-regexp)
  5407. "\\|")
  5408. after-re)))))
  5409. (org-with-wide-buffer
  5410. (goto-char (point-min))
  5411. (while (re-search-forward regexp nil t)
  5412. (org-element-cache-refresh (match-beginning 1)))))
  5413. ;; Re fontify buffer.
  5414. (when (memq 'radio org-highlight-links)
  5415. (org-restart-font-lock)))))
  5416. (defun org-hide-wide-columns (limit)
  5417. (let (s e)
  5418. (setq s (text-property-any (point) (or limit (point-max))
  5419. 'org-cwidth t))
  5420. (when s
  5421. (setq e (next-single-property-change s 'org-cwidth))
  5422. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  5423. (goto-char e)
  5424. t)))
  5425. (defvar org-latex-and-related-regexp nil
  5426. "Regular expression for highlighting LaTeX, entities and sub/superscript.")
  5427. (defun org-compute-latex-and-related-regexp ()
  5428. "Compute regular expression for LaTeX, entities and sub/superscript.
  5429. Result depends on variable `org-highlight-latex-and-related'."
  5430. (setq-local
  5431. org-latex-and-related-regexp
  5432. (let* ((re-sub
  5433. (cond ((not (memq 'script org-highlight-latex-and-related)) nil)
  5434. ((eq org-use-sub-superscripts '{})
  5435. (list org-match-substring-with-braces-regexp))
  5436. (org-use-sub-superscripts (list org-match-substring-regexp))))
  5437. (re-latex
  5438. (when (memq 'latex org-highlight-latex-and-related)
  5439. (let ((matchers (plist-get org-format-latex-options :matchers)))
  5440. (delq nil
  5441. (mapcar (lambda (x)
  5442. (and (member (car x) matchers) (nth 1 x)))
  5443. org-latex-regexps)))))
  5444. (re-entities
  5445. (when (memq 'entities org-highlight-latex-and-related)
  5446. (list "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)"))))
  5447. (mapconcat 'identity (append re-latex re-entities re-sub) "\\|"))))
  5448. (defun org-do-latex-and-related (limit)
  5449. "Highlight LaTeX snippets and environments, entities and sub/superscript.
  5450. LIMIT bounds the search for syntax to highlight. Stop at first
  5451. highlighted object, if any. Return t if some highlighting was
  5452. done, nil otherwise."
  5453. (when (org-string-nw-p org-latex-and-related-regexp)
  5454. (catch 'found
  5455. (while (re-search-forward org-latex-and-related-regexp limit t)
  5456. (unless (memq (car-safe (get-text-property (1+ (match-beginning 0))
  5457. 'face))
  5458. '(org-code org-verbatim underline))
  5459. (let ((offset (if (memq (char-after (1+ (match-beginning 0)))
  5460. '(?_ ?^))
  5461. 1
  5462. 0)))
  5463. (font-lock-prepend-text-property
  5464. (+ offset (match-beginning 0)) (match-end 0)
  5465. 'face 'org-latex-and-related)
  5466. (add-text-properties (+ offset (match-beginning 0)) (match-end 0)
  5467. '(font-lock-multiline t)))
  5468. (throw 'found t)))
  5469. nil)))
  5470. (defun org-restart-font-lock ()
  5471. "Restart `font-lock-mode', to force refontification."
  5472. (when (and (boundp 'font-lock-mode) font-lock-mode)
  5473. (font-lock-mode -1)
  5474. (font-lock-mode 1)))
  5475. (defun org-activate-tags (limit)
  5476. (when (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \r\n]") limit t)
  5477. (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
  5478. (add-text-properties (match-beginning 1) (match-end 1)
  5479. (list 'mouse-face 'highlight
  5480. 'keymap org-mouse-map))
  5481. (org-rear-nonsticky-at (match-end 1))
  5482. t))
  5483. (defun org-outline-level ()
  5484. "Compute the outline level of the heading at point.
  5485. If this is called at a normal headline, the level is the number
  5486. of stars. Use `org-reduced-level' to remove the effect of
  5487. `org-odd-levels'. Unlike to `org-current-level', this function
  5488. takes into consideration inlinetasks."
  5489. (org-with-wide-buffer
  5490. (end-of-line)
  5491. (if (re-search-backward org-outline-regexp-bol nil t)
  5492. (1- (- (match-end 0) (match-beginning 0)))
  5493. 0)))
  5494. (defvar org-font-lock-keywords nil)
  5495. (defsubst org-re-property (property &optional literal allow-null value)
  5496. "Return a regexp matching a PROPERTY line.
  5497. When optional argument LITERAL is non-nil, do not quote PROPERTY.
  5498. This is useful when PROPERTY is a regexp. When ALLOW-NULL is
  5499. non-nil, match properties even without a value.
  5500. Match group 3 is set to the value when it exists. If there is no
  5501. value and ALLOW-NULL is non-nil, it is set to the empty string.
  5502. With optional argument VALUE, match only property lines with
  5503. that value; in this case, ALLOW-NULL is ignored. VALUE is quoted
  5504. unless LITERAL is non-nil."
  5505. (concat
  5506. "^\\(?4:[ \t]*\\)"
  5507. (format "\\(?1::\\(?2:%s\\):\\)"
  5508. (if literal property (regexp-quote property)))
  5509. (cond (value
  5510. (format "[ \t]+\\(?3:%s\\)\\(?5:[ \t]*\\)$"
  5511. (if literal value (regexp-quote value))))
  5512. (allow-null
  5513. "\\(?:\\(?3:$\\)\\|[ \t]+\\(?3:.*?\\)\\)\\(?5:[ \t]*\\)$")
  5514. (t
  5515. "[ \t]+\\(?3:[^ \r\t\n]+.*?\\)\\(?5:[ \t]*\\)$"))))
  5516. (defconst org-property-re
  5517. (org-re-property "\\S-+" 'literal t)
  5518. "Regular expression matching a property line.
  5519. There are four matching groups:
  5520. 1: :PROPKEY: including the leading and trailing colon,
  5521. 2: PROPKEY without the leading and trailing colon,
  5522. 3: PROPVAL without leading or trailing spaces,
  5523. 4: the indentation of the current line,
  5524. 5: trailing whitespace.")
  5525. (defvar org-font-lock-hook nil
  5526. "Functions to be called for special font lock stuff.")
  5527. (defvar org-font-lock-set-keywords-hook nil
  5528. "Functions that can manipulate `org-font-lock-extra-keywords'.
  5529. This is called after `org-font-lock-extra-keywords' is defined, but before
  5530. it is installed to be used by font lock. This can be useful if something
  5531. needs to be inserted at a specific position in the font-lock sequence.")
  5532. (defun org-font-lock-hook (limit)
  5533. "Run `org-font-lock-hook' within LIMIT."
  5534. (run-hook-with-args 'org-font-lock-hook limit))
  5535. (defun org-set-font-lock-defaults ()
  5536. "Set font lock defaults for the current buffer."
  5537. (let* ((em org-fontify-emphasized-text)
  5538. (lk org-highlight-links)
  5539. (org-font-lock-extra-keywords
  5540. (list
  5541. ;; Call the hook
  5542. '(org-font-lock-hook)
  5543. ;; Headlines
  5544. `(,(if org-fontify-whole-heading-line
  5545. "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
  5546. "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
  5547. (1 (org-get-level-face 1))
  5548. (2 (org-get-level-face 2))
  5549. (3 (org-get-level-face 3)))
  5550. ;; Table lines
  5551. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  5552. (1 'org-table t))
  5553. ;; Table internals
  5554. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  5555. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  5556. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  5557. '("| *\\(<[lrc]?[0-9]*>\\)" (1 'org-formula t))
  5558. ;; Drawers
  5559. '(org-fontify-drawers)
  5560. ;; Properties
  5561. (list org-property-re
  5562. '(1 'org-special-keyword t)
  5563. '(3 'org-property-value t))
  5564. ;; Links
  5565. (when (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  5566. (when (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  5567. (when (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  5568. (when (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  5569. (when (memq 'radio lk) '(org-activate-target-links (1 'org-link t)))
  5570. (when (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  5571. (when (memq 'footnote lk) '(org-activate-footnote-links))
  5572. ;; Targets.
  5573. (list org-any-target-regexp '(0 'org-target t))
  5574. ;; Diary sexps.
  5575. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  5576. ;; Macro
  5577. '(org-fontify-macros)
  5578. '(org-hide-wide-columns (0 nil append))
  5579. ;; TODO keyword
  5580. (list (format org-heading-keyword-regexp-format
  5581. org-todo-regexp)
  5582. '(2 (org-get-todo-face 2) t))
  5583. ;; DONE
  5584. (if org-fontify-done-headline
  5585. (list (format org-heading-keyword-regexp-format
  5586. (concat
  5587. "\\(?:"
  5588. (mapconcat 'regexp-quote org-done-keywords "\\|")
  5589. "\\)"))
  5590. '(2 'org-headline-done t))
  5591. nil)
  5592. ;; Priorities
  5593. '(org-font-lock-add-priority-faces)
  5594. ;; Tags
  5595. '(org-font-lock-add-tag-faces)
  5596. ;; Tags groups
  5597. (when (and org-group-tags org-tag-groups-alist)
  5598. (list (concat org-outline-regexp-bol ".+\\(:"
  5599. (regexp-opt (mapcar 'car org-tag-groups-alist))
  5600. ":\\).*$")
  5601. '(1 'org-tag-group prepend)))
  5602. ;; Special keywords
  5603. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  5604. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  5605. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  5606. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  5607. ;; Emphasis
  5608. (when em
  5609. (if (featurep 'xemacs)
  5610. '(org-do-emphasis-faces (0 nil append))
  5611. '(org-do-emphasis-faces)))
  5612. ;; Checkboxes
  5613. '("^[ \t]*\\(?:[-+*]\\|[0-9]+[.)]\\)[ \t]+\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?\\(\\[[- X]\\]\\)"
  5614. 1 'org-checkbox prepend)
  5615. (when (cdr (assq 'checkbox org-list-automatic-rules))
  5616. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  5617. (0 (org-get-checkbox-statistics-face) t)))
  5618. ;; Description list items
  5619. '("^[ \t]*[-+*][ \t]+\\(.*?[ \t]+::\\)\\([ \t]+\\|$\\)"
  5620. 1 'org-list-dt prepend)
  5621. ;; ARCHIVEd headings
  5622. (list (concat
  5623. org-outline-regexp-bol
  5624. "\\(.*:" org-archive-tag ":.*\\)")
  5625. '(1 'org-archived prepend))
  5626. ;; Specials
  5627. '(org-do-latex-and-related)
  5628. '(org-fontify-entities)
  5629. '(org-raise-scripts)
  5630. ;; Code
  5631. '(org-activate-code (1 'org-code t))
  5632. ;; COMMENT
  5633. (list (format
  5634. "^\\*+\\(?: +%s\\)?\\(?: +\\[#[A-Z0-9]\\]\\)? +\\(?9:%s\\)\\(?: \\|$\\)"
  5635. org-todo-regexp
  5636. org-comment-string)
  5637. '(9 'org-special-keyword t))
  5638. ;; Blocks and meta lines
  5639. '(org-fontify-meta-lines-and-blocks))))
  5640. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  5641. (run-hooks 'org-font-lock-set-keywords-hook)
  5642. ;; Now set the full font-lock-keywords
  5643. (setq-local org-font-lock-keywords org-font-lock-extra-keywords)
  5644. (setq-local font-lock-defaults
  5645. '(org-font-lock-keywords t nil nil backward-paragraph))
  5646. (kill-local-variable 'font-lock-keywords) nil))
  5647. (defun org-toggle-pretty-entities ()
  5648. "Toggle the composition display of entities as UTF8 characters."
  5649. (interactive)
  5650. (setq-local org-pretty-entities (not org-pretty-entities))
  5651. (org-restart-font-lock)
  5652. (if org-pretty-entities
  5653. (message "Entities are now displayed as UTF8 characters")
  5654. (save-restriction
  5655. (widen)
  5656. (org-decompose-region (point-min) (point-max))
  5657. (message "Entities are now displayed as plain text"))))
  5658. (defvar-local org-custom-properties-overlays nil
  5659. "List of overlays used for custom properties.")
  5660. (defun org-toggle-custom-properties-visibility ()
  5661. "Display or hide properties in `org-custom-properties'."
  5662. (interactive)
  5663. (if org-custom-properties-overlays
  5664. (progn (mapc #'delete-overlay org-custom-properties-overlays)
  5665. (setq org-custom-properties-overlays nil))
  5666. (when org-custom-properties
  5667. (org-with-wide-buffer
  5668. (goto-char (point-min))
  5669. (let ((regexp (org-re-property (regexp-opt org-custom-properties) t t)))
  5670. (while (re-search-forward regexp nil t)
  5671. (let ((end (cdr (save-match-data (org-get-property-block)))))
  5672. (when (and end (< (point) end))
  5673. ;; Hide first custom property in current drawer.
  5674. (let ((o (make-overlay (match-beginning 0) (1+ (match-end 0)))))
  5675. (overlay-put o 'invisible t)
  5676. (overlay-put o 'org-custom-property t)
  5677. (push o org-custom-properties-overlays))
  5678. ;; Hide additional custom properties in the same drawer.
  5679. (while (re-search-forward regexp end t)
  5680. (let ((o (make-overlay (match-beginning 0) (1+ (match-end 0)))))
  5681. (overlay-put o 'invisible t)
  5682. (overlay-put o 'org-custom-property t)
  5683. (push o org-custom-properties-overlays)))))
  5684. ;; Each entry is limited to a single property drawer.
  5685. (outline-next-heading)))))))
  5686. (defun org-fontify-entities (limit)
  5687. "Find an entity to fontify."
  5688. (let (ee)
  5689. (when org-pretty-entities
  5690. (catch 'match
  5691. ;; "\_ "-family is left out on purpose. Only the first one,
  5692. ;; i.e., "\_ ", could be fontified anyway, and it would be
  5693. ;; confusing when adding a second white space character.
  5694. (while (re-search-forward
  5695. "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]\n]\\)"
  5696. limit t)
  5697. (when (and (not (org-at-comment-p))
  5698. (setq ee (org-entity-get (match-string 1)))
  5699. (= (length (nth 6 ee)) 1))
  5700. (let* ((end (if (equal (match-string 2) "{}")
  5701. (match-end 2)
  5702. (match-end 1))))
  5703. (add-text-properties
  5704. (match-beginning 0) end
  5705. (list 'font-lock-fontified t))
  5706. (compose-region (match-beginning 0) end
  5707. (nth 6 ee) nil)
  5708. (backward-char 1)
  5709. (throw 'match t))))
  5710. nil))))
  5711. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  5712. "Fontify string S like in Org-mode."
  5713. (with-temp-buffer
  5714. (insert s)
  5715. (let ((org-odd-levels-only odd-levels))
  5716. (org-mode)
  5717. (font-lock-ensure)
  5718. (buffer-string))))
  5719. (defvar org-m nil)
  5720. (defvar org-l nil)
  5721. (defvar org-f nil)
  5722. (defun org-get-level-face (n)
  5723. "Get the right face for match N in font-lock matching of headlines."
  5724. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  5725. (when org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  5726. (if org-cycle-level-faces
  5727. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  5728. (setq org-f (nth (1- (min org-l org-n-level-faces)) org-level-faces)))
  5729. (cond
  5730. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  5731. ((eq n 2) org-f)
  5732. (t (unless org-level-color-stars-only org-f))))
  5733. (defun org-face-from-face-or-color (context inherit face-or-color)
  5734. "Create a face list that inherits INHERIT, but sets the foreground color.
  5735. When FACE-OR-COLOR is not a string, just return it."
  5736. (if (stringp face-or-color)
  5737. (list :inherit inherit
  5738. (cdr (assoc context org-faces-easy-properties))
  5739. face-or-color)
  5740. face-or-color))
  5741. (defun org-get-todo-face (kwd)
  5742. "Get the right face for a TODO keyword KWD.
  5743. If KWD is a number, get the corresponding match group."
  5744. (when (numberp kwd) (setq kwd (match-string kwd)))
  5745. (or (org-face-from-face-or-color
  5746. 'todo 'org-todo (cdr (assoc kwd org-todo-keyword-faces)))
  5747. (and (member kwd org-done-keywords) 'org-done)
  5748. 'org-todo))
  5749. (defun org-get-priority-face (priority)
  5750. "Get the right face for PRIORITY.
  5751. PRIORITY is a character."
  5752. (or (org-face-from-face-or-color
  5753. 'priority 'org-priority (cdr (assq priority org-priority-faces)))
  5754. 'org-priority))
  5755. (defun org-get-tag-face (tag)
  5756. "Get the right face for TAG.
  5757. If TAG is a number, get the corresponding match group."
  5758. (let ((tag (if (wholenump tag) (match-string tag) tag)))
  5759. (or (org-face-from-face-or-color
  5760. 'tag 'org-tag (cdr (assoc tag org-tag-faces)))
  5761. 'org-tag)))
  5762. (defun org-font-lock-add-priority-faces (limit)
  5763. "Add the special priority faces."
  5764. (while (re-search-forward "^\\*+ .*?\\(\\[#\\(.\\)\\]\\)" limit t)
  5765. (add-text-properties
  5766. (match-beginning 1) (match-end 1)
  5767. (list 'face (org-get-priority-face (string-to-char (match-string 2)))
  5768. 'font-lock-fontified t))))
  5769. (defun org-font-lock-add-tag-faces (limit)
  5770. "Add the special tag faces."
  5771. (when (and org-tag-faces org-tags-special-faces-re)
  5772. (while (re-search-forward org-tags-special-faces-re limit t)
  5773. (add-text-properties (match-beginning 1) (match-end 1)
  5774. (list 'face (org-get-tag-face 1)
  5775. 'font-lock-fontified t))
  5776. (backward-char 1))))
  5777. (defun org-unfontify-region (beg end &optional maybe_loudly)
  5778. "Remove fontification and activation overlays from links."
  5779. (font-lock-default-unfontify-region beg end)
  5780. (let* ((buffer-undo-list t)
  5781. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  5782. (inhibit-modification-hooks t)
  5783. deactivate-mark buffer-file-name buffer-file-truename)
  5784. (org-decompose-region beg end)
  5785. (remove-text-properties beg end
  5786. '(mouse-face t keymap t org-linked-text t
  5787. invisible t intangible t
  5788. org-emphasis t))
  5789. (org-remove-font-lock-display-properties beg end)))
  5790. (defconst org-script-display '(((raise -0.3) (height 0.7))
  5791. ((raise 0.3) (height 0.7))
  5792. ((raise -0.5))
  5793. ((raise 0.5)))
  5794. "Display properties for showing superscripts and subscripts.")
  5795. (defun org-remove-font-lock-display-properties (beg end)
  5796. "Remove specific display properties that have been added by font lock.
  5797. The will remove the raise properties that are used to show superscripts
  5798. and subscripts."
  5799. (let (next prop)
  5800. (while (< beg end)
  5801. (setq next (next-single-property-change beg 'display nil end)
  5802. prop (get-text-property beg 'display))
  5803. (when (member prop org-script-display)
  5804. (put-text-property beg next 'display nil))
  5805. (setq beg next))))
  5806. (defun org-raise-scripts (limit)
  5807. "Add raise properties to sub/superscripts."
  5808. (when (and org-pretty-entities org-pretty-entities-include-sub-superscripts
  5809. (re-search-forward
  5810. (if (eq org-use-sub-superscripts t)
  5811. org-match-substring-regexp
  5812. org-match-substring-with-braces-regexp)
  5813. limit t))
  5814. (let* ((pos (point)) table-p comment-p
  5815. (mpos (match-beginning 3))
  5816. (emph-p (get-text-property mpos 'org-emphasis))
  5817. (link-p (get-text-property mpos 'mouse-face))
  5818. (keyw-p (eq 'org-special-keyword (get-text-property mpos 'face))))
  5819. (goto-char (point-at-bol))
  5820. (setq table-p (org-looking-at-p org-table-dataline-regexp)
  5821. comment-p (org-looking-at-p "^[ \t]*#[ +]"))
  5822. (goto-char pos)
  5823. ;; Handle a_b^c
  5824. (when (member (char-after) '(?_ ?^)) (goto-char (1- pos)))
  5825. (unless (or comment-p emph-p link-p keyw-p)
  5826. (put-text-property (match-beginning 3) (match-end 0)
  5827. 'display
  5828. (if (equal (char-after (match-beginning 2)) ?^)
  5829. (nth (if table-p 3 1) org-script-display)
  5830. (nth (if table-p 2 0) org-script-display)))
  5831. (add-text-properties (match-beginning 2) (match-end 2)
  5832. (list 'invisible t
  5833. 'org-dwidth t 'org-dwidth-n 1))
  5834. (if (and (eq (char-after (match-beginning 3)) ?{)
  5835. (eq (char-before (match-end 3)) ?}))
  5836. (progn
  5837. (add-text-properties
  5838. (match-beginning 3) (1+ (match-beginning 3))
  5839. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))
  5840. (add-text-properties
  5841. (1- (match-end 3)) (match-end 3)
  5842. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1)))))
  5843. t)))
  5844. ;;;; Visibility cycling, including org-goto and indirect buffer
  5845. ;;; Cycling
  5846. (defvar-local org-cycle-global-status nil)
  5847. (put 'org-cycle-global-status 'org-state t)
  5848. (defvar-local org-cycle-subtree-status nil)
  5849. (put 'org-cycle-subtree-status 'org-state t)
  5850. (defvar org-inlinetask-min-level)
  5851. (defun org-unlogged-message (&rest args)
  5852. "Display a message, but avoid logging it in the *Messages* buffer."
  5853. (let ((message-log-max nil))
  5854. (apply 'message args)))
  5855. ;;;###autoload
  5856. (defun org-cycle (&optional arg)
  5857. "TAB-action and visibility cycling for Org-mode.
  5858. This is the command invoked in Org-mode by the TAB key. Its main purpose
  5859. is outline visibility cycling, but it also invokes other actions
  5860. in special contexts.
  5861. - When this function is called with a prefix argument, rotate the entire
  5862. buffer through 3 states (global cycling)
  5863. 1. OVERVIEW: Show only top-level headlines.
  5864. 2. CONTENTS: Show all headlines of all levels, but no body text.
  5865. 3. SHOW ALL: Show everything.
  5866. With a double \\[universal-argument] prefix argument, \
  5867. switch to the startup visibility,
  5868. determined by the variable `org-startup-folded', and by any VISIBILITY
  5869. properties in the buffer.
  5870. With a triple \\[universal-argument] prefix argument, \
  5871. show the entire buffer, including any drawers.
  5872. - When inside a table, re-align the table and move to the next field.
  5873. - When point is at the beginning of a headline, rotate the subtree started
  5874. by this line through 3 different states (local cycling)
  5875. 1. FOLDED: Only the main headline is shown.
  5876. 2. CHILDREN: The main headline and the direct children are shown.
  5877. From this state, you can move to one of the children
  5878. and zoom in further.
  5879. 3. SUBTREE: Show the entire subtree, including body text.
  5880. If there is no subtree, switch directly from CHILDREN to FOLDED.
  5881. - When point is at the beginning of an empty headline and the variable
  5882. `org-cycle-level-after-item/entry-creation' is set, cycle the level
  5883. of the headline by demoting and promoting it to likely levels. This
  5884. speeds up creation document structure by pressing TAB once or several
  5885. times right after creating a new headline.
  5886. - When there is a numeric prefix, go up to a heading with level ARG, do
  5887. a `show-subtree' and return to the previous cursor position. If ARG
  5888. is negative, go up that many levels.
  5889. - When point is not at the beginning of a headline, execute the global
  5890. binding for TAB, which is re-indenting the line. See the option
  5891. `org-cycle-emulate-tab' for details.
  5892. - Special case: if point is at the beginning of the buffer and there is
  5893. no headline in line 1, this function will act as if called with prefix arg
  5894. (\\[universal-argument] TAB, same as S-TAB) also when called without prefix arg.
  5895. But only if also the variable `org-cycle-global-at-bob' is t."
  5896. (interactive "P")
  5897. (org-load-modules-maybe)
  5898. (unless (or (run-hook-with-args-until-success 'org-tab-first-hook)
  5899. (and org-cycle-level-after-item/entry-creation
  5900. (or (org-cycle-level)
  5901. (org-cycle-item-indentation))))
  5902. (let* ((limit-level
  5903. (or org-cycle-max-level
  5904. (and (boundp 'org-inlinetask-min-level)
  5905. org-inlinetask-min-level
  5906. (1- org-inlinetask-min-level))))
  5907. (nstars (and limit-level
  5908. (if org-odd-levels-only
  5909. (and limit-level (1- (* limit-level 2)))
  5910. limit-level)))
  5911. (org-outline-regexp
  5912. (if (not (derived-mode-p 'org-mode))
  5913. outline-regexp
  5914. (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ "))))
  5915. (bob-special (and org-cycle-global-at-bob (not arg) (bobp)
  5916. (not (looking-at org-outline-regexp))))
  5917. (org-cycle-hook
  5918. (if bob-special
  5919. (delq 'org-optimize-window-after-visibility-change
  5920. (copy-sequence org-cycle-hook))
  5921. org-cycle-hook))
  5922. (pos (point)))
  5923. (when (or bob-special (equal arg '(4)))
  5924. ;; special case: use global cycling
  5925. (setq arg t))
  5926. (cond
  5927. ((equal arg '(16))
  5928. (setq last-command 'dummy)
  5929. (org-set-startup-visibility)
  5930. (org-unlogged-message "Startup visibility, plus VISIBILITY properties"))
  5931. ((equal arg '(64))
  5932. (outline-show-all)
  5933. (org-unlogged-message "Entire buffer visible, including drawers"))
  5934. ;; Try cdlatex TAB completion
  5935. ((org-try-cdlatex-tab))
  5936. ;; Table: enter it or move to the next field.
  5937. ((org-at-table-p 'any)
  5938. (if (org-at-table.el-p)
  5939. (message "%s" (substitute-command-keys "\\<org-mode-map>\
  5940. Use \\[org-edit-special] to edit table.el tables"))
  5941. (if arg (org-table-edit-field t)
  5942. (org-table-justify-field-maybe)
  5943. (call-interactively 'org-table-next-field))))
  5944. ((run-hook-with-args-until-success
  5945. 'org-tab-after-check-for-table-hook))
  5946. ;; Global cycling: delegate to `org-cycle-internal-global'.
  5947. ((eq arg t) (org-cycle-internal-global))
  5948. ;; Drawers: delegate to `org-flag-drawer'.
  5949. ((save-excursion
  5950. (beginning-of-line 1)
  5951. (looking-at org-drawer-regexp))
  5952. (org-flag-drawer ; toggle block visibility
  5953. (not (get-char-property (match-end 0) 'invisible))))
  5954. ;; Show-subtree, ARG levels up from here.
  5955. ((integerp arg)
  5956. (save-excursion
  5957. (org-back-to-heading)
  5958. (outline-up-heading (if (< arg 0) (- arg)
  5959. (- (funcall outline-level) arg)))
  5960. (org-show-subtree)))
  5961. ;; Inline task: delegate to `org-inlinetask-toggle-visibility'.
  5962. ((and (featurep 'org-inlinetask)
  5963. (org-inlinetask-at-task-p)
  5964. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5965. (org-inlinetask-toggle-visibility))
  5966. ;; At an item/headline: delegate to `org-cycle-internal-local'.
  5967. ((and (or (and org-cycle-include-plain-lists (org-at-item-p))
  5968. (save-excursion (move-beginning-of-line 1)
  5969. (looking-at org-outline-regexp)))
  5970. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5971. (org-cycle-internal-local))
  5972. ;; From there: TAB emulation and template completion.
  5973. (buffer-read-only (org-back-to-heading))
  5974. ((run-hook-with-args-until-success
  5975. 'org-tab-after-check-for-cycling-hook))
  5976. ((org-try-structure-completion))
  5977. ((run-hook-with-args-until-success
  5978. 'org-tab-before-tab-emulation-hook))
  5979. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  5980. (or (not (bolp))
  5981. (not (looking-at org-outline-regexp))))
  5982. (call-interactively (global-key-binding "\t")))
  5983. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  5984. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  5985. (or (and (eq org-cycle-emulate-tab 'white)
  5986. (= (match-end 0) (point-at-eol)))
  5987. (and (eq org-cycle-emulate-tab 'whitestart)
  5988. (>= (match-end 0) pos))))
  5989. t
  5990. (eq org-cycle-emulate-tab t))
  5991. (call-interactively (global-key-binding "\t")))
  5992. (t (save-excursion
  5993. (org-back-to-heading)
  5994. (org-cycle)))))))
  5995. (defun org-cycle-internal-global ()
  5996. "Do the global cycling action."
  5997. ;; Hack to avoid display of messages for .org attachments in Gnus
  5998. (let ((ga (string-match "\\*fontification" (buffer-name))))
  5999. (cond
  6000. ((and (eq last-command this-command)
  6001. (eq org-cycle-global-status 'overview))
  6002. ;; We just created the overview - now do table of contents
  6003. ;; This can be slow in very large buffers, so indicate action
  6004. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  6005. (unless ga (org-unlogged-message "CONTENTS..."))
  6006. (org-content)
  6007. (unless ga (org-unlogged-message "CONTENTS...done"))
  6008. (setq org-cycle-global-status 'contents)
  6009. (run-hook-with-args 'org-cycle-hook 'contents))
  6010. ((and (eq last-command this-command)
  6011. (eq org-cycle-global-status 'contents))
  6012. ;; We just showed the table of contents - now show everything
  6013. (run-hook-with-args 'org-pre-cycle-hook 'all)
  6014. (outline-show-all)
  6015. (unless ga (org-unlogged-message "SHOW ALL"))
  6016. (setq org-cycle-global-status 'all)
  6017. (run-hook-with-args 'org-cycle-hook 'all))
  6018. (t
  6019. ;; Default action: go to overview
  6020. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  6021. (org-overview)
  6022. (unless ga (org-unlogged-message "OVERVIEW"))
  6023. (setq org-cycle-global-status 'overview)
  6024. (run-hook-with-args 'org-cycle-hook 'overview)))))
  6025. (defvar org-called-with-limited-levels nil
  6026. "Non-nil when `org-with-limited-levels' is currently active.")
  6027. (defun org-cycle-internal-local ()
  6028. "Do the local cycling action."
  6029. (let ((goal-column 0) eoh eol eos has-children children-skipped struct)
  6030. ;; First, determine end of headline (EOH), end of subtree or item
  6031. ;; (EOS), and if item or heading has children (HAS-CHILDREN).
  6032. (save-excursion
  6033. (if (org-at-item-p)
  6034. (progn
  6035. (beginning-of-line)
  6036. (setq struct (org-list-struct))
  6037. (setq eoh (point-at-eol))
  6038. (setq eos (org-list-get-item-end-before-blank (point) struct))
  6039. (setq has-children (org-list-has-child-p (point) struct)))
  6040. (org-back-to-heading)
  6041. (setq eoh (save-excursion (outline-end-of-heading) (point)))
  6042. (setq eos (save-excursion (org-end-of-subtree t t)
  6043. (when (bolp) (backward-char)) (point)))
  6044. (setq has-children
  6045. (or (save-excursion
  6046. (let ((level (funcall outline-level)))
  6047. (outline-next-heading)
  6048. (and (org-at-heading-p t)
  6049. (> (funcall outline-level) level))))
  6050. (save-excursion
  6051. (org-list-search-forward (org-item-beginning-re) eos t)))))
  6052. ;; Determine end invisible part of buffer (EOL)
  6053. (beginning-of-line 2)
  6054. ;; XEmacs doesn't have `next-single-char-property-change'
  6055. (if (featurep 'xemacs)
  6056. (while (and (not (eobp)) ;; this is like `next-line'
  6057. (get-char-property (1- (point)) 'invisible))
  6058. (beginning-of-line 2))
  6059. (while (and (not (eobp)) ;; this is like `next-line'
  6060. (get-char-property (1- (point)) 'invisible))
  6061. (goto-char (next-single-char-property-change (point) 'invisible))
  6062. (and (eolp) (beginning-of-line 2))))
  6063. (setq eol (point)))
  6064. ;; Find out what to do next and set `this-command'
  6065. (cond
  6066. ((= eos eoh)
  6067. ;; Nothing is hidden behind this heading
  6068. (unless (org-before-first-heading-p)
  6069. (run-hook-with-args 'org-pre-cycle-hook 'empty))
  6070. (org-unlogged-message "EMPTY ENTRY")
  6071. (setq org-cycle-subtree-status nil)
  6072. (save-excursion
  6073. (goto-char eos)
  6074. (outline-next-heading)
  6075. (when (outline-invisible-p) (org-flag-heading nil))))
  6076. ((and (or (>= eol eos)
  6077. (not (string-match "\\S-" (buffer-substring eol eos))))
  6078. (or has-children
  6079. (not (setq children-skipped
  6080. org-cycle-skip-children-state-if-no-children))))
  6081. ;; Entire subtree is hidden in one line: children view
  6082. (unless (org-before-first-heading-p)
  6083. (run-hook-with-args 'org-pre-cycle-hook 'children))
  6084. (if (org-at-item-p)
  6085. (org-list-set-item-visibility (point-at-bol) struct 'children)
  6086. (org-show-entry)
  6087. (org-with-limited-levels (org-show-children))
  6088. ;; FIXME: This slows down the func way too much.
  6089. ;; How keep drawers hidden in subtree anyway?
  6090. ;; (when (memq 'org-cycle-hide-drawers org-cycle-hook)
  6091. ;; (org-cycle-hide-drawers 'subtree))
  6092. ;; Fold every list in subtree to top-level items.
  6093. (when (eq org-cycle-include-plain-lists 'integrate)
  6094. (save-excursion
  6095. (org-back-to-heading)
  6096. (while (org-list-search-forward (org-item-beginning-re) eos t)
  6097. (beginning-of-line 1)
  6098. (let* ((struct (org-list-struct))
  6099. (prevs (org-list-prevs-alist struct))
  6100. (end (org-list-get-bottom-point struct)))
  6101. (mapc (lambda (e) (org-list-set-item-visibility e struct 'folded))
  6102. (org-list-get-all-items (point) struct prevs))
  6103. (goto-char (if (< end eos) end eos)))))))
  6104. (org-unlogged-message "CHILDREN")
  6105. (save-excursion
  6106. (goto-char eos)
  6107. (outline-next-heading)
  6108. (when (outline-invisible-p) (org-flag-heading nil)))
  6109. (setq org-cycle-subtree-status 'children)
  6110. (unless (org-before-first-heading-p)
  6111. (run-hook-with-args 'org-cycle-hook 'children)))
  6112. ((or children-skipped
  6113. (and (eq last-command this-command)
  6114. (eq org-cycle-subtree-status 'children)))
  6115. ;; We just showed the children, or no children are there,
  6116. ;; now show everything.
  6117. (unless (org-before-first-heading-p)
  6118. (run-hook-with-args 'org-pre-cycle-hook 'subtree))
  6119. (outline-flag-region eoh eos nil)
  6120. (org-unlogged-message
  6121. (if children-skipped "SUBTREE (NO CHILDREN)" "SUBTREE"))
  6122. (setq org-cycle-subtree-status 'subtree)
  6123. (unless (org-before-first-heading-p)
  6124. (run-hook-with-args 'org-cycle-hook 'subtree)))
  6125. (t
  6126. ;; Default action: hide the subtree.
  6127. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  6128. (outline-flag-region eoh eos t)
  6129. (org-unlogged-message "FOLDED")
  6130. (setq org-cycle-subtree-status 'folded)
  6131. (unless (org-before-first-heading-p)
  6132. (run-hook-with-args 'org-cycle-hook 'folded))))))
  6133. ;;;###autoload
  6134. (defun org-global-cycle (&optional arg)
  6135. "Cycle the global visibility. For details see `org-cycle'.
  6136. With \\[universal-argument] prefix arg, switch to startup visibility.
  6137. With a numeric prefix, show all headlines up to that level."
  6138. (interactive "P")
  6139. (let ((org-cycle-include-plain-lists
  6140. (if (derived-mode-p 'org-mode) org-cycle-include-plain-lists nil)))
  6141. (cond
  6142. ((integerp arg)
  6143. (outline-show-all)
  6144. (outline-hide-sublevels arg)
  6145. (setq org-cycle-global-status 'contents))
  6146. ((equal arg '(4))
  6147. (org-set-startup-visibility)
  6148. (org-unlogged-message "Startup visibility, plus VISIBILITY properties."))
  6149. (t
  6150. (org-cycle '(4))))))
  6151. (defun org-set-startup-visibility ()
  6152. "Set the visibility required by startup options and properties."
  6153. (cond
  6154. ((eq org-startup-folded t)
  6155. (org-overview))
  6156. ((eq org-startup-folded 'content)
  6157. (org-content))
  6158. ((or (eq org-startup-folded 'showeverything)
  6159. (eq org-startup-folded nil))
  6160. (outline-show-all)))
  6161. (unless (eq org-startup-folded 'showeverything)
  6162. (when org-hide-block-startup (org-hide-block-all))
  6163. (org-set-visibility-according-to-property 'no-cleanup)
  6164. (org-cycle-hide-archived-subtrees 'all)
  6165. (org-cycle-hide-drawers 'all)
  6166. (org-cycle-show-empty-lines t)))
  6167. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  6168. "Switch subtree visibilities according to :VISIBILITY: property."
  6169. (interactive)
  6170. (let (org-show-entry-below)
  6171. (org-with-wide-buffer
  6172. (goto-char (point-min))
  6173. (while (re-search-forward "^[ \t]*:VISIBILITY:" nil t)
  6174. (if (not (org-at-property-p)) (outline-next-heading)
  6175. (let ((state (match-string 3)))
  6176. (save-excursion
  6177. (org-back-to-heading t)
  6178. (outline-hide-subtree)
  6179. (org-reveal)
  6180. (cond
  6181. ((equal state "folded")
  6182. (outline-hide-subtree))
  6183. ((equal state "children")
  6184. (org-show-hidden-entry)
  6185. (org-show-children))
  6186. ((equal state "content")
  6187. (save-excursion
  6188. (save-restriction
  6189. (org-narrow-to-subtree)
  6190. (org-content))))
  6191. ((member state '("all" "showall"))
  6192. (outline-show-subtree)))))))
  6193. (unless no-cleanup
  6194. (org-cycle-hide-archived-subtrees 'all)
  6195. (org-cycle-hide-drawers 'all)
  6196. (org-cycle-show-empty-lines 'all)))))
  6197. ;; This function uses outline-regexp instead of the more fundamental
  6198. ;; org-outline-regexp so that org-cycle-global works outside of Org
  6199. ;; buffers, where outline-regexp is needed.
  6200. (defun org-overview ()
  6201. "Switch to overview mode, showing only top-level headlines.
  6202. This shows all headlines with a level equal or greater than the level
  6203. of the first headline in the buffer. This is important, because if the
  6204. first headline is not level one, then (hide-sublevels 1) gives confusing
  6205. results."
  6206. (interactive)
  6207. (save-excursion
  6208. (let ((level
  6209. (save-excursion
  6210. (goto-char (point-min))
  6211. (when (re-search-forward (concat "^" outline-regexp) nil t)
  6212. (goto-char (match-beginning 0))
  6213. (funcall outline-level)))))
  6214. (and level (outline-hide-sublevels level)))))
  6215. (defun org-content (&optional arg)
  6216. "Show all headlines in the buffer, like a table of contents.
  6217. With numerical argument N, show content up to level N."
  6218. (interactive "P")
  6219. (org-overview)
  6220. (save-excursion
  6221. ;; Visit all headings and show their offspring
  6222. (and (integerp arg) (org-overview))
  6223. (goto-char (point-max))
  6224. (catch 'exit
  6225. (while (and (progn (condition-case nil
  6226. (outline-previous-visible-heading 1)
  6227. (error (goto-char (point-min))))
  6228. t)
  6229. (looking-at org-outline-regexp))
  6230. (if (integerp arg)
  6231. (org-show-children (1- arg))
  6232. (outline-show-branches))
  6233. (when (bobp) (throw 'exit nil))))))
  6234. (defun org-optimize-window-after-visibility-change (state)
  6235. "Adjust the window after a change in outline visibility.
  6236. This function is the default value of the hook `org-cycle-hook'."
  6237. (when (get-buffer-window (current-buffer))
  6238. (cond
  6239. ((eq state 'content) nil)
  6240. ((eq state 'all) nil)
  6241. ((eq state 'folded) nil)
  6242. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  6243. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  6244. (defun org-remove-empty-overlays-at (pos)
  6245. "Remove outline overlays that do not contain non-white stuff."
  6246. (mapc
  6247. (lambda (o)
  6248. (and (eq 'outline (overlay-get o 'invisible))
  6249. (not (string-match "\\S-" (buffer-substring (overlay-start o)
  6250. (overlay-end o))))
  6251. (delete-overlay o)))
  6252. (overlays-at pos)))
  6253. (defun org-clean-visibility-after-subtree-move ()
  6254. "Fix visibility issues after moving a subtree."
  6255. ;; First, find a reasonable region to look at:
  6256. ;; Start two siblings above, end three below
  6257. (let* ((beg (save-excursion
  6258. (and (org-get-last-sibling)
  6259. (org-get-last-sibling))
  6260. (point)))
  6261. (end (save-excursion
  6262. (and (org-get-next-sibling)
  6263. (org-get-next-sibling)
  6264. (org-get-next-sibling))
  6265. (if (org-at-heading-p)
  6266. (point-at-eol)
  6267. (point))))
  6268. (level (looking-at "\\*+"))
  6269. (re (when level (concat "^" (regexp-quote (match-string 0)) " "))))
  6270. (save-excursion
  6271. (save-restriction
  6272. (narrow-to-region beg end)
  6273. (when re
  6274. ;; Properly fold already folded siblings
  6275. (goto-char (point-min))
  6276. (while (re-search-forward re nil t)
  6277. (when (and (not (outline-invisible-p))
  6278. (save-excursion
  6279. (goto-char (point-at-eol)) (outline-invisible-p)))
  6280. (outline-hide-entry))))
  6281. (org-cycle-show-empty-lines 'overview)
  6282. (org-cycle-hide-drawers 'overview)))))
  6283. (defun org-cycle-show-empty-lines (state)
  6284. "Show empty lines above all visible headlines.
  6285. The region to be covered depends on STATE when called through
  6286. `org-cycle-hook'. Lisp program can use t for STATE to get the
  6287. entire buffer covered. Note that an empty line is only shown if there
  6288. are at least `org-cycle-separator-lines' empty lines before the headline."
  6289. (when (/= org-cycle-separator-lines 0)
  6290. (save-excursion
  6291. (let* ((n (abs org-cycle-separator-lines))
  6292. (re (cond
  6293. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  6294. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  6295. (t (let ((ns (number-to-string (- n 2))))
  6296. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  6297. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  6298. beg end)
  6299. (cond
  6300. ((memq state '(overview contents t))
  6301. (setq beg (point-min) end (point-max)))
  6302. ((memq state '(children folded))
  6303. (setq beg (point)
  6304. end (progn (org-end-of-subtree t t)
  6305. (line-beginning-position 2)))))
  6306. (when beg
  6307. (goto-char beg)
  6308. (while (re-search-forward re end t)
  6309. (unless (get-char-property (match-end 1) 'invisible)
  6310. (let ((e (match-end 1))
  6311. (b (if (>= org-cycle-separator-lines 0)
  6312. (match-beginning 1)
  6313. (save-excursion
  6314. (goto-char (match-beginning 0))
  6315. (skip-chars-backward " \t\n")
  6316. (line-end-position)))))
  6317. (outline-flag-region b e nil))))))))
  6318. ;; Never hide empty lines at the end of the file.
  6319. (save-excursion
  6320. (goto-char (point-max))
  6321. (outline-previous-heading)
  6322. (outline-end-of-heading)
  6323. (when (and (looking-at "[ \t\n]+")
  6324. (= (match-end 0) (point-max)))
  6325. (outline-flag-region (point) (match-end 0) nil))))
  6326. (defun org-show-empty-lines-in-parent ()
  6327. "Move to the parent and re-show empty lines before visible headlines."
  6328. (save-excursion
  6329. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  6330. (org-cycle-show-empty-lines context))))
  6331. (defun org-files-list ()
  6332. "Return `org-agenda-files' list, plus all open org-mode files.
  6333. This is useful for operations that need to scan all of a user's
  6334. open and agenda-wise Org files."
  6335. (let ((files (mapcar 'expand-file-name (org-agenda-files))))
  6336. (dolist (buf (buffer-list))
  6337. (with-current-buffer buf
  6338. (when (and (derived-mode-p 'org-mode) (buffer-file-name))
  6339. (cl-pushnew (expand-file-name (buffer-file-name)) files))))
  6340. files))
  6341. (defsubst org-entry-beginning-position ()
  6342. "Return the beginning position of the current entry."
  6343. (save-excursion (outline-back-to-heading t) (point)))
  6344. (defsubst org-entry-end-position ()
  6345. "Return the end position of the current entry."
  6346. (save-excursion (outline-next-heading) (point)))
  6347. (defun org-cycle-hide-drawers (state &optional exceptions)
  6348. "Re-hide all drawers after a visibility state change.
  6349. When non-nil, optional argument EXCEPTIONS is a list of strings
  6350. specifying which drawers should not be hidden."
  6351. (when (and (derived-mode-p 'org-mode)
  6352. (not (memq state '(overview folded contents))))
  6353. (save-excursion
  6354. (let* ((globalp (memq state '(contents all)))
  6355. (beg (if globalp (point-min) (point)))
  6356. (end (if globalp (point-max)
  6357. (if (eq state 'children)
  6358. (save-excursion (outline-next-heading) (point))
  6359. (org-end-of-subtree t)))))
  6360. (goto-char beg)
  6361. (while (re-search-forward org-drawer-regexp (max end (point)) t)
  6362. (unless (member-ignore-case (match-string 1) exceptions)
  6363. (let ((drawer (org-element-at-point)))
  6364. (when (memq (org-element-type drawer) '(drawer property-drawer))
  6365. (org-flag-drawer t drawer)
  6366. ;; Make sure to skip drawer entirely or we might flag
  6367. ;; it another time when matching its ending line with
  6368. ;; `org-drawer-regexp'.
  6369. (goto-char (org-element-property :end drawer))))))))))
  6370. (defun org-flag-drawer (flag &optional element)
  6371. "When FLAG is non-nil, hide the drawer we are at.
  6372. Otherwise make it visible. When optional argument ELEMENT is
  6373. a parsed drawer, as returned by `org-element-at-point', hide or
  6374. show that drawer instead."
  6375. (when (save-excursion
  6376. (beginning-of-line)
  6377. (org-looking-at-p org-drawer-regexp))
  6378. (let ((drawer (or element (org-element-at-point))))
  6379. (when (memq (org-element-type drawer) '(drawer property-drawer))
  6380. (let ((post (org-element-property :post-affiliated drawer)))
  6381. (save-excursion
  6382. (outline-flag-region
  6383. (progn (goto-char post) (line-end-position))
  6384. (progn (goto-char (org-element-property :end drawer))
  6385. (skip-chars-backward " \r\t\n")
  6386. (line-end-position))
  6387. flag))
  6388. ;; When the drawer is hidden away, make sure point lies in
  6389. ;; a visible part of the buffer.
  6390. (when (and flag (> (line-beginning-position) post))
  6391. (goto-char post)))))))
  6392. (defun org-subtree-end-visible-p ()
  6393. "Is the end of the current subtree visible?"
  6394. (pos-visible-in-window-p
  6395. (save-excursion (org-end-of-subtree t) (point))))
  6396. (defun org-first-headline-recenter ()
  6397. "Move cursor to the first headline and recenter the headline."
  6398. (goto-char (point-min))
  6399. (when (re-search-forward (concat "^\\(" org-outline-regexp "\\)") nil t)
  6400. (set-window-start (selected-window) (point-at-bol))))
  6401. ;;; Saving and restoring visibility
  6402. (defun org-outline-overlay-data (&optional use-markers)
  6403. "Return a list of the locations of all outline overlays.
  6404. These are overlays with the `invisible' property value `outline'.
  6405. The return value is a list of cons cells, with start and stop
  6406. positions for each overlay.
  6407. If USE-MARKERS is set, return the positions as markers."
  6408. (let (beg end)
  6409. (save-excursion
  6410. (save-restriction
  6411. (widen)
  6412. (delq nil
  6413. (mapcar (lambda (o)
  6414. (when (eq (overlay-get o 'invisible) 'outline)
  6415. (setq beg (overlay-start o)
  6416. end (overlay-end o))
  6417. (and beg end (> end beg)
  6418. (if use-markers
  6419. (cons (copy-marker beg)
  6420. (copy-marker end t))
  6421. (cons beg end)))))
  6422. (overlays-in (point-min) (point-max))))))))
  6423. (defun org-set-outline-overlay-data (data)
  6424. "Create visibility overlays for all positions in DATA.
  6425. DATA should have been made by `org-outline-overlay-data'."
  6426. (let (o)
  6427. (save-excursion
  6428. (save-restriction
  6429. (widen)
  6430. (outline-show-all)
  6431. (mapc (lambda (c)
  6432. (outline-flag-region (car c) (cdr c) t))
  6433. data)))))
  6434. ;;; Folding of blocks
  6435. (defvar-local org-hide-block-overlays nil
  6436. "Overlays hiding blocks.")
  6437. (defun org-block-map (function &optional start end)
  6438. "Call FUNCTION at the head of all source blocks in the current buffer.
  6439. Optional arguments START and END can be used to limit the range."
  6440. (let ((start (or start (point-min)))
  6441. (end (or end (point-max))))
  6442. (save-excursion
  6443. (goto-char start)
  6444. (while (and (< (point) end) (re-search-forward org-block-regexp end t))
  6445. (save-excursion
  6446. (save-match-data
  6447. (goto-char (match-beginning 0))
  6448. (funcall function)))))))
  6449. (defun org-hide-block-toggle-all ()
  6450. "Toggle the visibility of all blocks in the current buffer."
  6451. (org-block-map 'org-hide-block-toggle))
  6452. (defun org-hide-block-all ()
  6453. "Fold all blocks in the current buffer."
  6454. (interactive)
  6455. (org-show-block-all)
  6456. (org-block-map 'org-hide-block-toggle-maybe))
  6457. (defun org-show-block-all ()
  6458. "Unfold all blocks in the current buffer."
  6459. (interactive)
  6460. (mapc 'delete-overlay org-hide-block-overlays)
  6461. (setq org-hide-block-overlays nil))
  6462. (defun org-hide-block-toggle-maybe ()
  6463. "Toggle visibility of block at point.
  6464. Unlike to `org-hide-block-toggle', this function does not throw
  6465. an error. Return a non-nil value when toggling is successful."
  6466. (interactive)
  6467. (ignore-errors (org-hide-block-toggle)))
  6468. (defun org-hide-block-toggle (&optional force)
  6469. "Toggle the visibility of the current block.
  6470. When optional argument FORCE is `off', make block visible. If it
  6471. is non-nil, hide it unconditionally. Throw an error when not at
  6472. a block. Return a non-nil value when toggling is successful."
  6473. (interactive)
  6474. (let ((element (org-element-at-point)))
  6475. (unless (memq (org-element-type element)
  6476. '(center-block comment-block dynamic-block example-block
  6477. export-block quote-block special-block
  6478. src-block verse-block))
  6479. (user-error "Not at a block"))
  6480. (let* ((start (save-excursion
  6481. (goto-char (org-element-property :post-affiliated element))
  6482. (line-end-position)))
  6483. (end (save-excursion
  6484. (goto-char (org-element-property :end element))
  6485. (skip-chars-backward " \r\t\n")
  6486. (line-end-position)))
  6487. (overlays (overlays-at start)))
  6488. (cond
  6489. ;; Do nothing when not before or at the block opening line or
  6490. ;; at the block closing line.
  6491. ((let ((eol (line-end-position))) (and (> eol start) (/= eol end))) nil)
  6492. ((and (not (eq force 'off))
  6493. (not (memq t (mapcar
  6494. (lambda (o)
  6495. (eq (overlay-get o 'invisible) 'org-hide-block))
  6496. overlays))))
  6497. (let ((ov (make-overlay start end)))
  6498. (overlay-put ov 'invisible 'org-hide-block)
  6499. ;; Make the block accessible to `isearch'.
  6500. (overlay-put
  6501. ov 'isearch-open-invisible
  6502. (lambda (ov)
  6503. (when (memq ov org-hide-block-overlays)
  6504. (setq org-hide-block-overlays (delq ov org-hide-block-overlays)))
  6505. (when (eq (overlay-get ov 'invisible) 'org-hide-block)
  6506. (delete-overlay ov))))
  6507. (push ov org-hide-block-overlays)
  6508. ;; When the block is hidden away, make sure point is left in
  6509. ;; a visible part of the buffer.
  6510. (when (> (line-beginning-position) start)
  6511. (goto-char start)
  6512. (beginning-of-line))
  6513. ;; Signal successful toggling.
  6514. t))
  6515. ((or (not force) (eq force 'off))
  6516. (dolist (ov overlays t)
  6517. (when (memq ov org-hide-block-overlays)
  6518. (setq org-hide-block-overlays (delq ov org-hide-block-overlays)))
  6519. (when (eq (overlay-get ov 'invisible) 'org-hide-block)
  6520. (delete-overlay ov))))))))
  6521. ;; org-tab-after-check-for-cycling-hook
  6522. (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
  6523. ;; Remove overlays when changing major mode
  6524. (add-hook 'org-mode-hook
  6525. (lambda () (org-add-hook 'change-major-mode-hook
  6526. 'org-show-block-all 'append 'local)))
  6527. ;;; Org-goto
  6528. (defvar org-goto-window-configuration nil)
  6529. (defvar org-goto-marker nil)
  6530. (defvar org-goto-map)
  6531. (defun org-goto-map ()
  6532. "Set the keymap `org-goto'."
  6533. (setq org-goto-map
  6534. (let ((map (make-sparse-keymap)))
  6535. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command
  6536. mouse-drag-region universal-argument org-occur)))
  6537. (dolist (cmd cmds)
  6538. (substitute-key-definition cmd cmd map global-map)))
  6539. (suppress-keymap map)
  6540. (org-defkey map "\C-m" 'org-goto-ret)
  6541. (org-defkey map [(return)] 'org-goto-ret)
  6542. (org-defkey map [(left)] 'org-goto-left)
  6543. (org-defkey map [(right)] 'org-goto-right)
  6544. (org-defkey map [(control ?g)] 'org-goto-quit)
  6545. (org-defkey map "\C-i" 'org-cycle)
  6546. (org-defkey map [(tab)] 'org-cycle)
  6547. (org-defkey map [(down)] 'outline-next-visible-heading)
  6548. (org-defkey map [(up)] 'outline-previous-visible-heading)
  6549. (if org-goto-auto-isearch
  6550. (if (fboundp 'define-key-after)
  6551. (define-key-after map [t] 'org-goto-local-auto-isearch)
  6552. nil)
  6553. (org-defkey map "q" 'org-goto-quit)
  6554. (org-defkey map "n" 'outline-next-visible-heading)
  6555. (org-defkey map "p" 'outline-previous-visible-heading)
  6556. (org-defkey map "f" 'outline-forward-same-level)
  6557. (org-defkey map "b" 'outline-backward-same-level)
  6558. (org-defkey map "u" 'outline-up-heading))
  6559. (org-defkey map "/" 'org-occur)
  6560. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  6561. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  6562. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  6563. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  6564. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  6565. map)))
  6566. (defconst org-goto-help
  6567. "Browse buffer copy, to find location or copy text.%s
  6568. RET=jump to location C-g=quit and return to previous location
  6569. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  6570. (defvar org-goto-start-pos) ; dynamically scoped parameter
  6571. (defun org-goto (&optional alternative-interface)
  6572. "Look up a different location in the current file, keeping current visibility.
  6573. When you want look-up or go to a different location in a
  6574. document, the fastest way is often to fold the entire buffer and
  6575. then dive into the tree. This method has the disadvantage, that
  6576. the previous location will be folded, which may not be what you
  6577. want.
  6578. This command works around this by showing a copy of the current
  6579. buffer in an indirect buffer, in overview mode. You can dive
  6580. into the tree in that copy, use org-occur and incremental search
  6581. to find a location. When pressing RET or `Q', the command
  6582. returns to the original buffer in which the visibility is still
  6583. unchanged. After RET it will also jump to the location selected
  6584. in the indirect buffer and expose the headline hierarchy above.
  6585. With a prefix argument, use the alternative interface: e.g., if
  6586. `org-goto-interface' is `outline' use `outline-path-completion'."
  6587. (interactive "P")
  6588. (org-goto-map)
  6589. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  6590. (org-refile-use-outline-path t)
  6591. (org-refile-target-verify-function nil)
  6592. (interface
  6593. (if (not alternative-interface)
  6594. org-goto-interface
  6595. (if (eq org-goto-interface 'outline)
  6596. 'outline-path-completion
  6597. 'outline)))
  6598. (org-goto-start-pos (point))
  6599. (selected-point
  6600. (if (eq interface 'outline)
  6601. (car (org-get-location (current-buffer) org-goto-help))
  6602. (let ((pa (org-refile-get-location "Goto" nil nil t)))
  6603. (org-refile-check-position pa)
  6604. (nth 3 pa)))))
  6605. (if selected-point
  6606. (progn
  6607. (org-mark-ring-push org-goto-start-pos)
  6608. (goto-char selected-point)
  6609. (when (or (outline-invisible-p) (org-invisible-p2))
  6610. (org-show-context 'org-goto)))
  6611. (message "Quit"))))
  6612. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  6613. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  6614. (defvar org-goto-local-auto-isearch-map) ; defined below
  6615. (defun org-get-location (buf help)
  6616. "Let the user select a location in the Org-mode buffer BUF.
  6617. This function uses a recursive edit. It returns the selected position
  6618. or nil."
  6619. (org-no-popups
  6620. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  6621. (isearch-hide-immediately nil)
  6622. (isearch-search-fun-function
  6623. (lambda () 'org-goto-local-search-headings))
  6624. (org-goto-selected-point org-goto-exit-command))
  6625. (save-excursion
  6626. (save-window-excursion
  6627. (delete-other-windows)
  6628. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  6629. (org-pop-to-buffer-same-window
  6630. (condition-case nil
  6631. (make-indirect-buffer (current-buffer) "*org-goto*")
  6632. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  6633. (with-output-to-temp-buffer "*Org Help*"
  6634. (princ (format help (if org-goto-auto-isearch
  6635. " Just type for auto-isearch."
  6636. " n/p/f/b/u to navigate, q to quit."))))
  6637. (org-fit-window-to-buffer (get-buffer-window "*Org Help*"))
  6638. (setq buffer-read-only nil)
  6639. (let ((org-startup-truncated t)
  6640. (org-startup-folded nil)
  6641. (org-startup-align-all-tables nil))
  6642. (org-mode)
  6643. (org-overview))
  6644. (setq buffer-read-only t)
  6645. (if (and (boundp 'org-goto-start-pos)
  6646. (integer-or-marker-p org-goto-start-pos))
  6647. (progn (goto-char org-goto-start-pos)
  6648. (when (outline-invisible-p)
  6649. (org-show-set-visibility 'lineage)))
  6650. (goto-char (point-min)))
  6651. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  6652. (message "Select location and press RET")
  6653. (use-local-map org-goto-map)
  6654. (recursive-edit)))
  6655. (kill-buffer "*org-goto*")
  6656. (cons org-goto-selected-point org-goto-exit-command))))
  6657. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  6658. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  6659. ;; `isearch-other-control-char' was removed in Emacs 24.4.
  6660. (if (fboundp 'isearch-other-control-char)
  6661. (progn
  6662. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  6663. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char))
  6664. (define-key org-goto-local-auto-isearch-map "\C-i" nil)
  6665. (define-key org-goto-local-auto-isearch-map "\C-m" nil)
  6666. (define-key org-goto-local-auto-isearch-map [return] nil))
  6667. (defun org-goto-local-search-headings (string bound noerror)
  6668. "Search and make sure that any matches are in headlines."
  6669. (catch 'return
  6670. (while (if isearch-forward
  6671. (search-forward string bound noerror)
  6672. (search-backward string bound noerror))
  6673. (when (save-match-data
  6674. (and (save-excursion
  6675. (beginning-of-line)
  6676. (looking-at org-complex-heading-regexp))
  6677. (or (not (match-beginning 5))
  6678. (< (point) (match-beginning 5)))))
  6679. (throw 'return (point))))))
  6680. (defun org-goto-local-auto-isearch ()
  6681. "Start isearch."
  6682. (interactive)
  6683. (goto-char (point-min))
  6684. (let ((keys (this-command-keys)))
  6685. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  6686. (isearch-mode t)
  6687. (isearch-process-search-char (string-to-char keys)))))
  6688. (defun org-goto-ret (&optional arg)
  6689. "Finish `org-goto' by going to the new location."
  6690. (interactive "P")
  6691. (setq org-goto-selected-point (point)
  6692. org-goto-exit-command 'return)
  6693. (throw 'exit nil))
  6694. (defun org-goto-left ()
  6695. "Finish `org-goto' by going to the new location."
  6696. (interactive)
  6697. (if (org-at-heading-p)
  6698. (progn
  6699. (beginning-of-line 1)
  6700. (setq org-goto-selected-point (point)
  6701. org-goto-exit-command 'left)
  6702. (throw 'exit nil))
  6703. (user-error "Not on a heading")))
  6704. (defun org-goto-right ()
  6705. "Finish `org-goto' by going to the new location."
  6706. (interactive)
  6707. (if (org-at-heading-p)
  6708. (progn
  6709. (setq org-goto-selected-point (point)
  6710. org-goto-exit-command 'right)
  6711. (throw 'exit nil))
  6712. (user-error "Not on a heading")))
  6713. (defun org-goto-quit ()
  6714. "Finish `org-goto' without cursor motion."
  6715. (interactive)
  6716. (setq org-goto-selected-point nil)
  6717. (setq org-goto-exit-command 'quit)
  6718. (throw 'exit nil))
  6719. ;;; Indirect buffer display of subtrees
  6720. (defvar org-indirect-dedicated-frame nil
  6721. "This is the frame being used for indirect tree display.")
  6722. (defvar org-last-indirect-buffer nil)
  6723. (defun org-tree-to-indirect-buffer (&optional arg)
  6724. "Create indirect buffer and narrow it to current subtree.
  6725. With a numerical prefix ARG, go up to this level and then take that tree.
  6726. If ARG is negative, go up that many levels.
  6727. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  6728. indirect buffer previously made with this command, to avoid proliferation of
  6729. indirect buffers. However, when you call the command with a \
  6730. \\[universal-argument] prefix, or
  6731. when `org-indirect-buffer-display' is `new-frame', the last buffer
  6732. is kept so that you can work with several indirect buffers at the same time.
  6733. If `org-indirect-buffer-display' is `dedicated-frame', the \
  6734. \\[universal-argument] prefix also
  6735. requests that a new frame be made for the new buffer, so that the dedicated
  6736. frame is not changed."
  6737. (interactive "P")
  6738. (let ((cbuf (current-buffer))
  6739. (cwin (selected-window))
  6740. (pos (point))
  6741. beg end level heading ibuf)
  6742. (save-excursion
  6743. (org-back-to-heading t)
  6744. (when (numberp arg)
  6745. (setq level (org-outline-level))
  6746. (when (< arg 0) (setq arg (+ level arg)))
  6747. (while (> (setq level (org-outline-level)) arg)
  6748. (org-up-heading-safe)))
  6749. (setq beg (point)
  6750. heading (org-get-heading))
  6751. (org-end-of-subtree t t)
  6752. (when (org-at-heading-p) (backward-char 1))
  6753. (setq end (point)))
  6754. (when (and (buffer-live-p org-last-indirect-buffer)
  6755. (not (eq org-indirect-buffer-display 'new-frame))
  6756. (not arg))
  6757. (kill-buffer org-last-indirect-buffer))
  6758. (setq ibuf (org-get-indirect-buffer cbuf heading)
  6759. org-last-indirect-buffer ibuf)
  6760. (cond
  6761. ((or (eq org-indirect-buffer-display 'new-frame)
  6762. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  6763. (select-frame (make-frame))
  6764. (delete-other-windows)
  6765. (org-pop-to-buffer-same-window ibuf)
  6766. (org-set-frame-title heading))
  6767. ((eq org-indirect-buffer-display 'dedicated-frame)
  6768. (raise-frame
  6769. (select-frame (or (and org-indirect-dedicated-frame
  6770. (frame-live-p org-indirect-dedicated-frame)
  6771. org-indirect-dedicated-frame)
  6772. (setq org-indirect-dedicated-frame (make-frame)))))
  6773. (delete-other-windows)
  6774. (org-pop-to-buffer-same-window ibuf)
  6775. (org-set-frame-title (concat "Indirect: " heading)))
  6776. ((eq org-indirect-buffer-display 'current-window)
  6777. (org-pop-to-buffer-same-window ibuf))
  6778. ((eq org-indirect-buffer-display 'other-window)
  6779. (pop-to-buffer ibuf))
  6780. (t (error "Invalid value")))
  6781. (when (featurep 'xemacs)
  6782. (save-excursion (org-mode) (turn-on-font-lock)))
  6783. (narrow-to-region beg end)
  6784. (outline-show-all)
  6785. (goto-char pos)
  6786. (run-hook-with-args 'org-cycle-hook 'all)
  6787. (and (window-live-p cwin) (select-window cwin))))
  6788. (defun org-get-indirect-buffer (&optional buffer heading)
  6789. (setq buffer (or buffer (current-buffer)))
  6790. (let ((n 1) (base (buffer-name buffer)) bname)
  6791. (while (buffer-live-p
  6792. (get-buffer
  6793. (setq bname
  6794. (concat base "-"
  6795. (if heading (concat heading "-" (number-to-string n))
  6796. (number-to-string n))))))
  6797. (setq n (1+ n)))
  6798. (condition-case nil
  6799. (make-indirect-buffer buffer bname 'clone)
  6800. (error (make-indirect-buffer buffer bname)))))
  6801. (defun org-set-frame-title (title)
  6802. "Set the title of the current frame to the string TITLE."
  6803. ;; FIXME: how to name a single frame in XEmacs???
  6804. (unless (featurep 'xemacs)
  6805. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  6806. ;;;; Structure editing
  6807. ;;; Inserting headlines
  6808. (defun org--line-empty-p (n)
  6809. "Is the Nth next line empty?
  6810. Counts the current line as N = 1 and the previous line as N = 0;
  6811. see `beginning-of-line'."
  6812. (save-excursion
  6813. (and (not (bobp))
  6814. (or (beginning-of-line n) t)
  6815. (save-match-data
  6816. (looking-at "[ \t]*$")))))
  6817. (defun org-previous-line-empty-p ()
  6818. "Is the previous line a blank line?
  6819. When NEXT is non-nil, check the next line instead."
  6820. (org--line-empty-p 0))
  6821. (defun org-next-line-empty-p ()
  6822. "Is the previous line a blank line?
  6823. When NEXT is non-nil, check the next line instead."
  6824. (org--line-empty-p 2))
  6825. (defun org-insert-heading (&optional arg invisible-ok top-level)
  6826. "Insert a new heading or an item with the same depth at point.
  6827. If point is at the beginning of a heading or a list item, insert
  6828. a new heading or a new item above the current one. If point is
  6829. at the beginning of a normal line, turn the line into a heading.
  6830. If point is in the middle of a headline or a list item, split the
  6831. headline or the item and create a new headline/item with the text
  6832. in the current line after point \(see `org-M-RET-may-split-line'
  6833. on how to modify this behavior).
  6834. With one universal prefix argument, set the user option
  6835. `org-insert-heading-respect-content' to t for the duration of
  6836. the command. This modifies the behavior described above in this
  6837. ways: on list items and at the beginning of normal lines, force
  6838. the insertion of a heading after the current subtree.
  6839. With two universal prefix arguments, insert the heading at the
  6840. end of the grandparent subtree. For example, if point is within
  6841. a 2nd-level heading, then it will insert a 2nd-level heading at
  6842. the end of the 1st-level parent heading.
  6843. If point is at the beginning of a headline, insert a sibling
  6844. before the current headline. If point is not at the beginning,
  6845. split the line and create a new headline with the text in the
  6846. current line after point \(see `org-M-RET-may-split-line' on how
  6847. to modify this behavior).
  6848. If point is at the beginning of a normal line, turn this line
  6849. into a heading.
  6850. When INVISIBLE-OK is set, stop at invisible headlines when going
  6851. back. This is important for non-interactive uses of the
  6852. command.
  6853. When optional argument TOP-LEVEL is non-nil, insert a level 1
  6854. heading, unconditionally."
  6855. (interactive "P")
  6856. (when (org-called-interactively-p 'any) (org-reveal))
  6857. (let ((itemp (and (not top-level) (org-in-item-p)))
  6858. (may-split (org-get-alist-option org-M-RET-may-split-line 'headline))
  6859. (respect-content (or org-insert-heading-respect-content
  6860. (equal arg '(4))))
  6861. (initial-content ""))
  6862. (cond
  6863. ((or (= (buffer-size) 0)
  6864. (and (not (save-excursion
  6865. (and (ignore-errors (org-back-to-heading invisible-ok))
  6866. (org-at-heading-p))))
  6867. (or arg (not itemp))))
  6868. ;; At beginning of buffer or so high up that only a heading
  6869. ;; makes sense.
  6870. (cond ((and (bolp) (not respect-content)) (insert "* "))
  6871. ((not respect-content)
  6872. (unless may-split (end-of-line))
  6873. (insert "\n* "))
  6874. ((re-search-forward org-outline-regexp-bol nil t)
  6875. (beginning-of-line)
  6876. (insert "* \n")
  6877. (backward-char))
  6878. (t (goto-char (point-max))
  6879. (insert "\n* ")))
  6880. (run-hooks 'org-insert-heading-hook))
  6881. ((and itemp (not (member arg '((4) (16)))) (org-insert-item)))
  6882. (t
  6883. ;; Maybe move at the end of the subtree
  6884. (when (equal arg '(16))
  6885. (org-up-heading-safe)
  6886. (org-end-of-subtree t))
  6887. ;; Insert a heading
  6888. (save-restriction
  6889. (widen)
  6890. (let* ((level nil)
  6891. (on-heading (org-at-heading-p))
  6892. (empty-line-p (if on-heading
  6893. (org-previous-line-empty-p)
  6894. ;; We will decide later
  6895. nil))
  6896. ;; Get a level string to fall back on.
  6897. (fix-level
  6898. (if (org-before-first-heading-p) "*"
  6899. (save-excursion
  6900. (org-back-to-heading t)
  6901. (when (org-previous-line-empty-p) (setq empty-line-p t))
  6902. (looking-at org-outline-regexp)
  6903. (make-string (1- (length (match-string 0))) ?*))))
  6904. (stars
  6905. (save-excursion
  6906. (condition-case nil
  6907. (if top-level "* "
  6908. (org-back-to-heading invisible-ok)
  6909. (when (and (not on-heading)
  6910. (featurep 'org-inlinetask)
  6911. (integerp org-inlinetask-min-level)
  6912. (>= (length (match-string 0))
  6913. org-inlinetask-min-level))
  6914. ;; Find a heading level before the inline
  6915. ;; task.
  6916. (while (and (setq level (org-up-heading-safe))
  6917. (>= level org-inlinetask-min-level)))
  6918. (if (org-at-heading-p)
  6919. (org-back-to-heading invisible-ok)
  6920. (error "This should not happen")))
  6921. (unless (and (save-excursion
  6922. (save-match-data
  6923. (org-backward-heading-same-level
  6924. 1 invisible-ok))
  6925. (= (point) (match-beginning 0)))
  6926. (not (org-next-line-empty-p)))
  6927. (setq empty-line-p (or empty-line-p
  6928. (org-previous-line-empty-p))))
  6929. (match-string 0))
  6930. (error (or fix-level "* ")))))
  6931. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  6932. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  6933. pos hide-previous previous-pos)
  6934. ;; If we insert after content, move there and clean up
  6935. ;; whitespace.
  6936. (when (and respect-content
  6937. (not (org-looking-at-p org-outline-regexp-bol)))
  6938. (if (not (org-before-first-heading-p))
  6939. (org-end-of-subtree nil t)
  6940. (re-search-forward org-outline-regexp-bol)
  6941. (beginning-of-line 0))
  6942. (skip-chars-backward " \r\t\n")
  6943. (and (not (org-looking-back "^\\*+" (line-beginning-position)))
  6944. (looking-at "[ \t]+") (replace-match ""))
  6945. (unless (eobp) (forward-char 1))
  6946. (when (looking-at "^\\*")
  6947. (unless (bobp) (backward-char 1))
  6948. (insert "\n")))
  6949. ;; If we are splitting, grab the text that should be moved
  6950. ;; to the new headline.
  6951. (when may-split
  6952. (if (org-at-heading-p)
  6953. ;; This is a heading: split intelligently (keeping
  6954. ;; tags).
  6955. (let ((pos (point)))
  6956. (beginning-of-line)
  6957. (unless (looking-at org-complex-heading-regexp)
  6958. (error "This should not happen"))
  6959. (when (and (match-beginning 4)
  6960. (> pos (match-beginning 4))
  6961. (< pos (match-end 4)))
  6962. (setq initial-content (buffer-substring pos (match-end 4)))
  6963. (goto-char pos)
  6964. (delete-region (point) (match-end 4))
  6965. (if (looking-at "[ \t]*$")
  6966. (replace-match "")
  6967. (insert (make-string (length initial-content) ?\s)))
  6968. (setq initial-content (org-trim initial-content)))
  6969. (goto-char pos))
  6970. ;; A normal line.
  6971. (setq initial-content
  6972. (org-trim
  6973. (delete-and-extract-region (point) (line-end-position))))))
  6974. ;; If we are at the beginning of the line, insert before it.
  6975. ;; Otherwise, after it.
  6976. (cond
  6977. ((and (bolp) (looking-at "[ \t]*$")))
  6978. ((bolp) (save-excursion (insert "\n")))
  6979. (t (end-of-line)
  6980. (insert "\n")))
  6981. ;; Insert the new heading
  6982. (insert stars)
  6983. (just-one-space)
  6984. (insert initial-content)
  6985. (unless (and blank (org-previous-line-empty-p))
  6986. (org-N-empty-lines-before-current (if blank 1 0)))
  6987. ;; Adjust visibility, which may be messed up if we removed
  6988. ;; blank lines while previous entry was hidden.
  6989. (let ((bol (line-beginning-position)))
  6990. (dolist (o (overlays-at (1- bol)))
  6991. (when (and (eq (overlay-get o 'invisible) 'outline)
  6992. (eq (overlay-end o) bol))
  6993. (move-overlay o (overlay-start o) (1- bol)))))
  6994. (run-hooks 'org-insert-heading-hook)))))))
  6995. (defun org-N-empty-lines-before-current (N)
  6996. "Make the number of empty lines before current exactly N.
  6997. So this will delete or add empty lines."
  6998. (save-excursion
  6999. (beginning-of-line)
  7000. (let ((p (point)))
  7001. (skip-chars-backward " \r\t\n")
  7002. (unless (bolp) (forward-line))
  7003. (delete-region (point) p))
  7004. (when (> N 0) (insert (make-string N ?\n)))))
  7005. (defun org-get-heading (&optional no-tags no-todo)
  7006. "Return the heading of the current entry, without the stars.
  7007. When NO-TAGS is non-nil, don't include tags.
  7008. When NO-TODO is non-nil, don't include TODO keywords."
  7009. (save-excursion
  7010. (org-back-to-heading t)
  7011. (cond
  7012. ((and no-tags no-todo)
  7013. (looking-at org-complex-heading-regexp)
  7014. (match-string 4))
  7015. (no-tags
  7016. (looking-at (concat org-outline-regexp
  7017. "\\(.*?\\)"
  7018. "\\(?:[ \t]+:[[:alnum:]:_@#%]+:\\)?[ \t]*$"))
  7019. (match-string 1))
  7020. (no-todo
  7021. (looking-at org-todo-line-regexp)
  7022. (match-string 3))
  7023. (t (looking-at org-heading-regexp)
  7024. (match-string 2)))))
  7025. (defvar orgstruct-mode) ; defined below
  7026. (defun org-heading-components ()
  7027. "Return the components of the current heading.
  7028. This is a list with the following elements:
  7029. - the level as an integer
  7030. - the reduced level, different if `org-odd-levels-only' is set.
  7031. - the TODO keyword, or nil
  7032. - the priority character, like ?A, or nil if no priority is given
  7033. - the headline text itself, or the tags string if no headline text
  7034. - the tags string, or nil."
  7035. (save-excursion
  7036. (org-back-to-heading t)
  7037. (when (let (case-fold-search)
  7038. (looking-at
  7039. (if orgstruct-mode
  7040. org-heading-regexp
  7041. org-complex-heading-regexp)))
  7042. (if orgstruct-mode
  7043. (list (length (match-string 1))
  7044. (org-reduced-level (length (match-string 1)))
  7045. nil
  7046. nil
  7047. (match-string 2)
  7048. nil)
  7049. (list (length (match-string 1))
  7050. (org-reduced-level (length (match-string 1)))
  7051. (org-match-string-no-properties 2)
  7052. (and (match-end 3) (aref (match-string 3) 2))
  7053. (org-match-string-no-properties 4)
  7054. (org-match-string-no-properties 5))))))
  7055. (defun org-get-entry ()
  7056. "Get the entry text, after heading, entire subtree."
  7057. (save-excursion
  7058. (org-back-to-heading t)
  7059. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  7060. (defun org-insert-heading-after-current ()
  7061. "Insert a new heading with same level as current, after current subtree."
  7062. (interactive)
  7063. (org-back-to-heading)
  7064. (org-insert-heading)
  7065. (org-move-subtree-down)
  7066. (end-of-line 1))
  7067. (defun org-insert-heading-respect-content (&optional invisible-ok)
  7068. "Insert heading with `org-insert-heading-respect-content' set to t."
  7069. (interactive)
  7070. (org-insert-heading '(4) invisible-ok))
  7071. (defun org-insert-todo-heading-respect-content (&optional force-state)
  7072. "Insert TODO heading with `org-insert-heading-respect-content' set to t."
  7073. (interactive)
  7074. (org-insert-todo-heading force-state '(4)))
  7075. (defun org-insert-todo-heading (arg &optional force-heading)
  7076. "Insert a new heading with the same level and TODO state as current heading.
  7077. If the heading has no TODO state, or if the state is DONE, use the first
  7078. state (TODO by default). Also with one prefix arg, force first state. With
  7079. two prefix args, force inserting at the end of the parent subtree."
  7080. (interactive "P")
  7081. (when (or force-heading (not (org-insert-item 'checkbox)))
  7082. (org-insert-heading (or (and (equal arg '(16)) '(16))
  7083. force-heading))
  7084. (save-excursion
  7085. (org-back-to-heading)
  7086. (outline-previous-heading)
  7087. (looking-at org-todo-line-regexp))
  7088. (let*
  7089. ((new-mark-x
  7090. (if (or (equal arg '(4))
  7091. (not (match-beginning 2))
  7092. (member (match-string 2) org-done-keywords))
  7093. (car org-todo-keywords-1)
  7094. (match-string 2)))
  7095. (new-mark
  7096. (or
  7097. (run-hook-with-args-until-success
  7098. 'org-todo-get-default-hook new-mark-x nil)
  7099. new-mark-x)))
  7100. (beginning-of-line 1)
  7101. (and (looking-at org-outline-regexp) (goto-char (match-end 0))
  7102. (if org-treat-insert-todo-heading-as-state-change
  7103. (org-todo new-mark)
  7104. (insert new-mark " "))))
  7105. (when org-provide-todo-statistics
  7106. (org-update-parent-todo-statistics))))
  7107. (defun org-insert-subheading (arg)
  7108. "Insert a new subheading and demote it.
  7109. Works for outline headings and for plain lists alike."
  7110. (interactive "P")
  7111. (org-insert-heading arg)
  7112. (cond
  7113. ((org-at-heading-p) (org-do-demote))
  7114. ((org-at-item-p) (org-indent-item))))
  7115. (defun org-insert-todo-subheading (arg)
  7116. "Insert a new subheading with TODO keyword or checkbox and demote it.
  7117. Works for outline headings and for plain lists alike."
  7118. (interactive "P")
  7119. (org-insert-todo-heading arg)
  7120. (cond
  7121. ((org-at-heading-p) (org-do-demote))
  7122. ((org-at-item-p) (org-indent-item))))
  7123. ;;; Promotion and Demotion
  7124. (defvar org-after-demote-entry-hook nil
  7125. "Hook run after an entry has been demoted.
  7126. The cursor will be at the beginning of the entry.
  7127. When a subtree is being demoted, the hook will be called for each node.")
  7128. (defvar org-after-promote-entry-hook nil
  7129. "Hook run after an entry has been promoted.
  7130. The cursor will be at the beginning of the entry.
  7131. When a subtree is being promoted, the hook will be called for each node.")
  7132. (defun org-promote-subtree ()
  7133. "Promote the entire subtree.
  7134. See also `org-promote'."
  7135. (interactive)
  7136. (save-excursion
  7137. (org-with-limited-levels (org-map-tree 'org-promote)))
  7138. (org-fix-position-after-promote))
  7139. (defun org-demote-subtree ()
  7140. "Demote the entire subtree.
  7141. See `org-demote' and `org-promote'."
  7142. (interactive)
  7143. (save-excursion
  7144. (org-with-limited-levels (org-map-tree 'org-demote)))
  7145. (org-fix-position-after-promote))
  7146. (defun org-do-promote ()
  7147. "Promote the current heading higher up the tree.
  7148. If the region is active in `transient-mark-mode', promote all
  7149. headings in the region."
  7150. (interactive)
  7151. (save-excursion
  7152. (if (org-region-active-p)
  7153. (org-map-region 'org-promote (region-beginning) (region-end))
  7154. (org-promote)))
  7155. (org-fix-position-after-promote))
  7156. (defun org-do-demote ()
  7157. "Demote the current heading lower down the tree.
  7158. If the region is active in `transient-mark-mode', demote all
  7159. headings in the region."
  7160. (interactive)
  7161. (save-excursion
  7162. (if (org-region-active-p)
  7163. (org-map-region 'org-demote (region-beginning) (region-end))
  7164. (org-demote)))
  7165. (org-fix-position-after-promote))
  7166. (defun org-fix-position-after-promote ()
  7167. "Fix cursor position and indentation after demoting/promoting."
  7168. (let ((pos (point)))
  7169. (when (save-excursion
  7170. (beginning-of-line 1)
  7171. (looking-at org-todo-line-regexp)
  7172. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  7173. (cond ((eobp) (insert " "))
  7174. ((eolp) (insert " "))
  7175. ((equal (char-after) ?\ ) (forward-char 1))))))
  7176. (defun org-current-level ()
  7177. "Return the level of the current entry, or nil if before the first headline.
  7178. The level is the number of stars at the beginning of the
  7179. headline. Use `org-reduced-level' to remove the effect of
  7180. `org-odd-levels'. Unlike to `org-outline-level', this function
  7181. ignores inlinetasks."
  7182. (let ((level (org-with-limited-levels (org-outline-level))))
  7183. (and (> level 0) level)))
  7184. (defun org-get-previous-line-level ()
  7185. "Return the outline depth of the last headline before the current line.
  7186. Returns 0 for the first headline in the buffer, and nil if before the
  7187. first headline."
  7188. (and (org-current-level)
  7189. (or (and (/= (line-beginning-position) (point-min))
  7190. (save-excursion (beginning-of-line 0) (org-current-level)))
  7191. 0)))
  7192. (defun org-reduced-level (l)
  7193. "Compute the effective level of a heading.
  7194. This takes into account the setting of `org-odd-levels-only'."
  7195. (cond
  7196. ((zerop l) 0)
  7197. (org-odd-levels-only (1+ (floor (/ l 2))))
  7198. (t l)))
  7199. (defun org-level-increment ()
  7200. "Return the number of stars that will be added or removed at a
  7201. time to headlines when structure editing, based on the value of
  7202. `org-odd-levels-only'."
  7203. (if org-odd-levels-only 2 1))
  7204. (defun org-get-valid-level (level &optional change)
  7205. "Rectify a level change under the influence of `org-odd-levels-only'
  7206. LEVEL is a current level, CHANGE is by how much the level should be
  7207. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  7208. even level numbers will become the next higher odd number."
  7209. (if org-odd-levels-only
  7210. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  7211. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  7212. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  7213. (max 1 (+ level (or change 0)))))
  7214. (define-obsolete-function-alias 'org-get-legal-level 'org-get-valid-level "23.1")
  7215. (defun org-promote ()
  7216. "Promote the current heading higher up 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. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  7223. (diff (abs (- level (length up-head) -1))))
  7224. (cond
  7225. ((and (= level 1) org-allow-promoting-top-level-subtree)
  7226. (replace-match "# " nil t))
  7227. ((= level 1)
  7228. (user-error "Cannot promote to level 0. UNDO to recover if necessary"))
  7229. (t (replace-match up-head nil t)))
  7230. (unless (= level 1)
  7231. (when org-auto-align-tags (org-set-tags nil 'ignore-column))
  7232. (when org-adapt-indentation (org-fixup-indentation (- diff))))
  7233. (run-hooks 'org-after-promote-entry-hook))))
  7234. (defun org-demote ()
  7235. "Demote the current heading lower down the tree."
  7236. (org-with-wide-buffer
  7237. (org-back-to-heading t)
  7238. (let* ((after-change-functions (remq 'flyspell-after-change-function
  7239. after-change-functions))
  7240. (level (save-match-data (funcall outline-level)))
  7241. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  7242. (diff (abs (- level (length down-head) -1))))
  7243. (replace-match down-head nil t)
  7244. (when org-auto-align-tags (org-set-tags nil 'ignore-column))
  7245. (when org-adapt-indentation (org-fixup-indentation diff))
  7246. (run-hooks 'org-after-demote-entry-hook))))
  7247. (defun org-cycle-level ()
  7248. "Cycle the level of an empty headline through possible states.
  7249. This goes first to child, then to parent, level, then up the hierarchy.
  7250. After top level, it switches back to sibling level."
  7251. (interactive)
  7252. (let ((org-adapt-indentation nil))
  7253. (when (org-point-at-end-of-empty-headline)
  7254. (setq this-command 'org-cycle-level) ; Only needed for caching
  7255. (let ((cur-level (org-current-level))
  7256. (prev-level (org-get-previous-line-level)))
  7257. (cond
  7258. ;; If first headline in file, promote to top-level.
  7259. ((= prev-level 0)
  7260. (loop repeat (/ (- cur-level 1) (org-level-increment))
  7261. do (org-do-promote)))
  7262. ;; If same level as prev, demote one.
  7263. ((= prev-level cur-level)
  7264. (org-do-demote))
  7265. ;; If parent is top-level, promote to top level if not already.
  7266. ((= prev-level 1)
  7267. (loop repeat (/ (- cur-level 1) (org-level-increment))
  7268. do (org-do-promote)))
  7269. ;; If top-level, return to prev-level.
  7270. ((= cur-level 1)
  7271. (loop repeat (/ (- prev-level 1) (org-level-increment))
  7272. do (org-do-demote)))
  7273. ;; If less than prev-level, promote one.
  7274. ((< cur-level prev-level)
  7275. (org-do-promote))
  7276. ;; If deeper than prev-level, promote until higher than
  7277. ;; prev-level.
  7278. ((> cur-level prev-level)
  7279. (loop repeat (+ 1 (/ (- cur-level prev-level) (org-level-increment)))
  7280. do (org-do-promote))))
  7281. t))))
  7282. (defun org-map-tree (fun)
  7283. "Call FUN for every heading underneath the current one."
  7284. (org-back-to-heading)
  7285. (let ((level (funcall outline-level)))
  7286. (save-excursion
  7287. (funcall fun)
  7288. (while (and (progn
  7289. (outline-next-heading)
  7290. (> (funcall outline-level) level))
  7291. (not (eobp)))
  7292. (funcall fun)))))
  7293. (defun org-map-region (fun beg end)
  7294. "Call FUN for every heading between BEG and END."
  7295. (let ((org-ignore-region t))
  7296. (save-excursion
  7297. (setq end (copy-marker end))
  7298. (goto-char beg)
  7299. (when (and (re-search-forward org-outline-regexp-bol nil t)
  7300. (< (point) end))
  7301. (funcall fun))
  7302. (while (and (progn
  7303. (outline-next-heading)
  7304. (< (point) end))
  7305. (not (eobp)))
  7306. (funcall fun)))))
  7307. (defun org-fixup-indentation (diff)
  7308. "Change the indentation in the current entry by DIFF.
  7309. DIFF is an integer. Indentation is done according to the
  7310. following rules:
  7311. - Planning information and property drawers are always indented
  7312. according to the new level of the headline;
  7313. - Footnote definitions and their contents are ignored;
  7314. - Inlinetasks' boundaries are not shifted;
  7315. - Empty lines are ignored;
  7316. - Other lines' indentation are shifted by DIFF columns, unless
  7317. it would introduce a structural change in the document, in
  7318. which case no shifting is done at all.
  7319. Assume point is at a heading or an inlinetask beginning."
  7320. (org-with-wide-buffer
  7321. (narrow-to-region (line-beginning-position)
  7322. (save-excursion
  7323. (if (org-with-limited-levels (org-at-heading-p))
  7324. (org-with-limited-levels (outline-next-heading))
  7325. (org-inlinetask-goto-end))
  7326. (point)))
  7327. (forward-line)
  7328. ;; Indent properly planning info and property drawer.
  7329. (when (org-looking-at-p org-planning-line-re)
  7330. (org-indent-line)
  7331. (forward-line))
  7332. (when (looking-at org-property-drawer-re)
  7333. (goto-char (match-end 0))
  7334. (forward-line)
  7335. (save-excursion (org-indent-region (match-beginning 0) (match-end 0))))
  7336. (catch 'no-shift
  7337. (when (zerop diff) (throw 'no-shift nil))
  7338. ;; If DIFF is negative, first check if a shift is possible at all
  7339. ;; (e.g., it doesn't break structure). This can only happen if
  7340. ;; some contents are not properly indented.
  7341. (let ((case-fold-search t))
  7342. (when (< diff 0)
  7343. (let ((diff (- diff))
  7344. (forbidden-re (concat org-outline-regexp
  7345. "\\|"
  7346. (substring org-footnote-definition-re 1))))
  7347. (save-excursion
  7348. (while (not (eobp))
  7349. (cond
  7350. ((org-looking-at-p "[ \t]*$") (forward-line))
  7351. ((and (org-looking-at-p org-footnote-definition-re)
  7352. (let ((e (org-element-at-point)))
  7353. (and (eq (org-element-type e) 'footnote-definition)
  7354. (goto-char (org-element-property :end e))))))
  7355. ((org-looking-at-p org-outline-regexp) (forward-line))
  7356. ;; Give up if shifting would move before column 0 or
  7357. ;; if it would introduce a headline or a footnote
  7358. ;; definition.
  7359. (t
  7360. (skip-chars-forward " \t")
  7361. (let ((ind (current-column)))
  7362. (when (or (< ind diff)
  7363. (and (= ind diff) (org-looking-at-p forbidden-re)))
  7364. (throw 'no-shift nil)))
  7365. ;; Ignore contents of example blocks and source
  7366. ;; blocks if their indentation is meant to be
  7367. ;; preserved. Jump to block's closing line.
  7368. (beginning-of-line)
  7369. (or (and (org-looking-at-p "[ \t]*#\\+BEGIN_\\(EXAMPLE\\|SRC\\)")
  7370. (let ((e (org-element-at-point)))
  7371. (and (memq (org-element-type e)
  7372. '(example-block src-block))
  7373. (or org-src-preserve-indentation
  7374. (org-element-property :preserve-indent e))
  7375. (goto-char (org-element-property :end e))
  7376. (progn (skip-chars-backward " \r\t\n")
  7377. (beginning-of-line)
  7378. t))))
  7379. (forward-line))))))))
  7380. ;; Shift lines but footnote definitions, inlinetasks boundaries
  7381. ;; by DIFF. Also skip contents of source or example blocks
  7382. ;; when indentation is meant to be preserved.
  7383. (while (not (eobp))
  7384. (cond
  7385. ((and (org-looking-at-p org-footnote-definition-re)
  7386. (let ((e (org-element-at-point)))
  7387. (and (eq (org-element-type e) 'footnote-definition)
  7388. (goto-char (org-element-property :end e))))))
  7389. ((org-looking-at-p org-outline-regexp) (forward-line))
  7390. ((org-looking-at-p "[ \t]*$") (forward-line))
  7391. (t
  7392. (org-indent-line-to (+ (org-get-indentation) diff))
  7393. (beginning-of-line)
  7394. (or (and (org-looking-at-p "[ \t]*#\\+BEGIN_\\(EXAMPLE\\|SRC\\)")
  7395. (let ((e (org-element-at-point)))
  7396. (and (memq (org-element-type e)
  7397. '(example-block src-block))
  7398. (or org-src-preserve-indentation
  7399. (org-element-property :preserve-indent e))
  7400. (goto-char (org-element-property :end e))
  7401. (progn (skip-chars-backward " \r\t\n")
  7402. (beginning-of-line)
  7403. t))))
  7404. (forward-line)))))))))
  7405. (defun org-convert-to-odd-levels ()
  7406. "Convert an org-mode file with all levels allowed to one with odd levels.
  7407. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  7408. level 5 etc."
  7409. (interactive)
  7410. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  7411. (let ((outline-level 'org-outline-level)
  7412. (org-odd-levels-only nil) n)
  7413. (save-excursion
  7414. (goto-char (point-min))
  7415. (while (re-search-forward "^\\*\\*+ " nil t)
  7416. (setq n (- (length (match-string 0)) 2))
  7417. (while (>= (setq n (1- n)) 0)
  7418. (org-demote))
  7419. (end-of-line 1))))))
  7420. (defun org-convert-to-oddeven-levels ()
  7421. "Convert an org-mode file with only odd levels to one with odd/even levels.
  7422. This promotes level 3 to level 2, level 5 to level 3 etc. If the
  7423. file contains a section with an even level, conversion would
  7424. destroy the structure of the file. An error is signaled in this
  7425. case."
  7426. (interactive)
  7427. (goto-char (point-min))
  7428. ;; First check if there are no even levels
  7429. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  7430. (org-show-set-visibility 'canonical)
  7431. (error "Not all levels are odd in this file. Conversion not possible"))
  7432. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  7433. (let ((outline-regexp org-outline-regexp)
  7434. (outline-level 'org-outline-level)
  7435. (org-odd-levels-only nil) n)
  7436. (save-excursion
  7437. (goto-char (point-min))
  7438. (while (re-search-forward "^\\*\\*+ " nil t)
  7439. (setq n (/ (1- (length (match-string 0))) 2))
  7440. (while (>= (setq n (1- n)) 0)
  7441. (org-promote))
  7442. (end-of-line 1))))))
  7443. (defun org-tr-level (n)
  7444. "Make N odd if required."
  7445. (if org-odd-levels-only (1+ (/ n 2)) n))
  7446. ;;; Vertical tree motion, cutting and pasting of subtrees
  7447. (defun org-move-subtree-up (&optional arg)
  7448. "Move the current subtree up past ARG headlines of the same level."
  7449. (interactive "p")
  7450. (org-move-subtree-down (- (prefix-numeric-value arg))))
  7451. (defun org-move-subtree-down (&optional arg)
  7452. "Move the current subtree down past ARG headlines of the same level."
  7453. (interactive "p")
  7454. (setq arg (prefix-numeric-value arg))
  7455. (let ((movfunc (if (> arg 0) 'org-get-next-sibling
  7456. 'org-get-last-sibling))
  7457. (ins-point (make-marker))
  7458. (cnt (abs arg))
  7459. (col (current-column))
  7460. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  7461. ;; Select the tree
  7462. (org-back-to-heading)
  7463. (setq beg0 (point))
  7464. (save-excursion
  7465. (setq ne-beg (org-back-over-empty-lines))
  7466. (setq beg (point)))
  7467. (save-match-data
  7468. (save-excursion (outline-end-of-heading)
  7469. (setq folded (outline-invisible-p)))
  7470. (progn (org-end-of-subtree nil t)
  7471. (unless (eobp) (backward-char))))
  7472. (outline-next-heading)
  7473. (setq ne-end (org-back-over-empty-lines))
  7474. (setq end (point))
  7475. (goto-char beg0)
  7476. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  7477. ;; include less whitespace
  7478. (save-excursion
  7479. (goto-char beg)
  7480. (forward-line (- ne-beg ne-end))
  7481. (setq beg (point))))
  7482. ;; Find insertion point, with error handling
  7483. (while (> cnt 0)
  7484. (or (and (funcall movfunc) (looking-at org-outline-regexp))
  7485. (progn (goto-char beg0)
  7486. (user-error "Cannot move past superior level or buffer limit")))
  7487. (setq cnt (1- cnt)))
  7488. (when (> arg 0)
  7489. ;; Moving forward - still need to move over subtree
  7490. (org-end-of-subtree t t)
  7491. (save-excursion
  7492. (org-back-over-empty-lines)
  7493. (or (bolp) (newline))))
  7494. (setq ne-ins (org-back-over-empty-lines))
  7495. (move-marker ins-point (point))
  7496. (setq txt (buffer-substring beg end))
  7497. (org-save-markers-in-region beg end)
  7498. (delete-region beg end)
  7499. (org-remove-empty-overlays-at beg)
  7500. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  7501. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  7502. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  7503. (let ((bbb (point)))
  7504. (insert-before-markers txt)
  7505. (org-reinstall-markers-in-region bbb)
  7506. (move-marker ins-point bbb))
  7507. (or (bolp) (insert "\n"))
  7508. (setq ins-end (point))
  7509. (goto-char ins-point)
  7510. (org-skip-whitespace)
  7511. (when (and (< arg 0)
  7512. (org-first-sibling-p)
  7513. (> ne-ins ne-beg))
  7514. ;; Move whitespace back to beginning
  7515. (save-excursion
  7516. (goto-char ins-end)
  7517. (let ((kill-whole-line t))
  7518. (kill-line (- ne-ins ne-beg)) (point)))
  7519. (insert (make-string (- ne-ins ne-beg) ?\n)))
  7520. (move-marker ins-point nil)
  7521. (if folded
  7522. (outline-hide-subtree)
  7523. (org-show-entry)
  7524. (org-show-children)
  7525. (org-cycle-hide-drawers 'children))
  7526. (org-clean-visibility-after-subtree-move)
  7527. ;; move back to the initial column we were at
  7528. (move-to-column col)))
  7529. (defvar org-subtree-clip ""
  7530. "Clipboard for cut and paste of subtrees.
  7531. This is actually only a copy of the kill, because we use the normal kill
  7532. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  7533. (defvar org-subtree-clip-folded nil
  7534. "Was the last copied subtree folded?
  7535. This is used to fold the tree back after pasting.")
  7536. (defun org-cut-subtree (&optional n)
  7537. "Cut the current subtree into the clipboard.
  7538. With prefix arg N, cut this many sequential subtrees.
  7539. This is a short-hand for marking the subtree and then cutting it."
  7540. (interactive "p")
  7541. (org-copy-subtree n 'cut))
  7542. (defun org-copy-subtree (&optional n cut force-store-markers nosubtrees)
  7543. "Copy the current subtree it in the clipboard.
  7544. With prefix arg N, copy this many sequential subtrees.
  7545. This is a short-hand for marking the subtree and then copying it.
  7546. If CUT is non-nil, actually cut the subtree.
  7547. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  7548. of some markers in the region, even if CUT is non-nil. This is
  7549. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  7550. (interactive "p")
  7551. (let (beg end folded (beg0 (point)))
  7552. (if (org-called-interactively-p 'any)
  7553. (org-back-to-heading nil) ; take what looks like a subtree
  7554. (org-back-to-heading t)) ; take what is really there
  7555. (setq beg (point))
  7556. (skip-chars-forward " \t\r\n")
  7557. (save-match-data
  7558. (if nosubtrees
  7559. (outline-next-heading)
  7560. (save-excursion (outline-end-of-heading)
  7561. (setq folded (outline-invisible-p)))
  7562. (ignore-errors (org-forward-heading-same-level (1- n) t))
  7563. (org-end-of-subtree t t)))
  7564. ;; Include the end of an inlinetask
  7565. (when (and (featurep 'org-inlinetask)
  7566. (looking-at-p (concat (org-inlinetask-outline-regexp)
  7567. "END[ \t]*$")))
  7568. (end-of-line))
  7569. (setq end (point))
  7570. (goto-char beg0)
  7571. (when (> end beg)
  7572. (setq org-subtree-clip-folded folded)
  7573. (when (or cut force-store-markers)
  7574. (org-save-markers-in-region beg end))
  7575. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  7576. (setq org-subtree-clip (current-kill 0))
  7577. (message "%s: Subtree(s) with %d characters"
  7578. (if cut "Cut" "Copied")
  7579. (length org-subtree-clip)))))
  7580. (defun org-paste-subtree (&optional level tree for-yank remove)
  7581. "Paste the clipboard as a subtree, with modification of headline level.
  7582. The entire subtree is promoted or demoted in order to match a new headline
  7583. level.
  7584. If the cursor is at the beginning of a headline, the same level as
  7585. that headline is used to paste the tree.
  7586. If not, the new level is derived from the *visible* headings
  7587. before and after the insertion point, and taken to be the inferior headline
  7588. level of the two. So if the previous visible heading is level 3 and the
  7589. next is level 4 (or vice versa), level 4 will be used for insertion.
  7590. This makes sure that the subtree remains an independent subtree and does
  7591. not swallow low level entries.
  7592. You can also force a different level, either by using a numeric prefix
  7593. argument, or by inserting the heading marker by hand. For example, if the
  7594. cursor is after \"*****\", then the tree will be shifted to level 5.
  7595. If optional TREE is given, use this text instead of the kill ring.
  7596. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  7597. move back over whitespace before inserting, and move point to the end of
  7598. the inserted text when done.
  7599. When REMOVE is non-nil, remove the subtree from the clipboard."
  7600. (interactive "P")
  7601. (setq tree (or tree (and kill-ring (current-kill 0))))
  7602. (unless (org-kill-is-subtree-p tree)
  7603. (user-error "%s"
  7604. (substitute-command-keys
  7605. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  7606. (org-with-limited-levels
  7607. (let* ((visp (not (outline-invisible-p)))
  7608. (txt tree)
  7609. (^re_ "\\(\\*+\\)[ \t]*")
  7610. (old-level (if (string-match org-outline-regexp-bol txt)
  7611. (- (match-end 0) (match-beginning 0) 1)
  7612. -1))
  7613. (force-level (cond (level (prefix-numeric-value level))
  7614. ((and (looking-at "[ \t]*$")
  7615. (string-match
  7616. "^\\*+$" (buffer-substring
  7617. (point-at-bol) (point))))
  7618. (- (match-end 0) (match-beginning 0)))
  7619. ((and (bolp)
  7620. (looking-at org-outline-regexp))
  7621. (- (match-end 0) (point) 1))))
  7622. (previous-level (save-excursion
  7623. (condition-case nil
  7624. (progn
  7625. (outline-previous-visible-heading 1)
  7626. (if (looking-at ^re_)
  7627. (- (match-end 0) (match-beginning 0) 1)
  7628. 1))
  7629. (error 1))))
  7630. (next-level (save-excursion
  7631. (condition-case nil
  7632. (progn
  7633. (or (looking-at org-outline-regexp)
  7634. (outline-next-visible-heading 1))
  7635. (if (looking-at ^re_)
  7636. (- (match-end 0) (match-beginning 0) 1)
  7637. 1))
  7638. (error 1))))
  7639. (new-level (or force-level (max previous-level next-level)))
  7640. (shift (if (or (= old-level -1)
  7641. (= new-level -1)
  7642. (= old-level new-level))
  7643. 0
  7644. (- new-level old-level)))
  7645. (delta (if (> shift 0) -1 1))
  7646. (func (if (> shift 0) 'org-demote 'org-promote))
  7647. (org-odd-levels-only nil)
  7648. beg end newend)
  7649. ;; Remove the forced level indicator
  7650. (when force-level
  7651. (delete-region (point-at-bol) (point)))
  7652. ;; Paste
  7653. (beginning-of-line (if (bolp) 1 2))
  7654. (setq beg (point))
  7655. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  7656. (insert-before-markers txt)
  7657. (unless (string-match "\n\\'" txt) (insert "\n"))
  7658. (setq newend (point))
  7659. (org-reinstall-markers-in-region beg)
  7660. (setq end (point))
  7661. (goto-char beg)
  7662. (skip-chars-forward " \t\n\r")
  7663. (setq beg (point))
  7664. (when (and (outline-invisible-p) visp)
  7665. (save-excursion (outline-show-heading)))
  7666. ;; Shift if necessary
  7667. (unless (= shift 0)
  7668. (save-restriction
  7669. (narrow-to-region beg end)
  7670. (while (not (= shift 0))
  7671. (org-map-region func (point-min) (point-max))
  7672. (setq shift (+ delta shift)))
  7673. (goto-char (point-min))
  7674. (setq newend (point-max))))
  7675. (when (or (org-called-interactively-p 'interactive) for-yank)
  7676. (message "Clipboard pasted as level %d subtree" new-level))
  7677. (when (and (not for-yank) ; in this case, org-yank will decide about folding
  7678. kill-ring
  7679. (eq org-subtree-clip (current-kill 0))
  7680. org-subtree-clip-folded)
  7681. ;; The tree was folded before it was killed/copied
  7682. (outline-hide-subtree))
  7683. (and for-yank (goto-char newend))
  7684. (and remove (setq kill-ring (cdr kill-ring))))))
  7685. (defun org-kill-is-subtree-p (&optional txt)
  7686. "Check if the current kill is an outline subtree, or a set of trees.
  7687. Returns nil if kill does not start with a headline, or if the first
  7688. headline level is not the largest headline level in the tree.
  7689. So this will actually accept several entries of equal levels as well,
  7690. which is OK for `org-paste-subtree'.
  7691. If optional TXT is given, check this string instead of the current kill."
  7692. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  7693. (re (org-get-limited-outline-regexp))
  7694. (^re (concat "^" re))
  7695. (start-level (and kill
  7696. (string-match
  7697. (concat "\\`\\([ \t\n\r]*?\n\\)?\\(" re "\\)")
  7698. kill)
  7699. (- (match-end 2) (match-beginning 2) 1)))
  7700. (start (1+ (or (match-beginning 2) -1))))
  7701. (if (not start-level)
  7702. (progn
  7703. nil) ;; does not even start with a heading
  7704. (catch 'exit
  7705. (while (setq start (string-match ^re kill (1+ start)))
  7706. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  7707. (throw 'exit nil)))
  7708. t))))
  7709. (defvar org-markers-to-move nil
  7710. "Markers that should be moved with a cut-and-paste operation.
  7711. Those markers are stored together with their positions relative to
  7712. the start of the region.")
  7713. (defun org-save-markers-in-region (beg end)
  7714. "Check markers in region.
  7715. If these markers are between BEG and END, record their position relative
  7716. to BEG, so that after moving the block of text, we can put the markers back
  7717. into place.
  7718. This function gets called just before an entry or tree gets cut from the
  7719. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  7720. called immediately, to move the markers with the entries."
  7721. (setq org-markers-to-move nil)
  7722. (when (featurep 'org-clock)
  7723. (org-clock-save-markers-for-cut-and-paste beg end))
  7724. (when (featurep 'org-agenda)
  7725. (org-agenda-save-markers-for-cut-and-paste beg end)))
  7726. (defun org-check-and-save-marker (marker beg end)
  7727. "Check if MARKER is between BEG and END.
  7728. If yes, remember the marker and the distance to BEG."
  7729. (when (and (marker-buffer marker)
  7730. (equal (marker-buffer marker) (current-buffer))
  7731. (>= marker beg) (< marker end))
  7732. (push (cons marker (- marker beg)) org-markers-to-move)))
  7733. (defun org-reinstall-markers-in-region (beg)
  7734. "Move all remembered markers to their position relative to BEG."
  7735. (mapc (lambda (x)
  7736. (move-marker (car x) (+ beg (cdr x))))
  7737. org-markers-to-move)
  7738. (setq org-markers-to-move nil))
  7739. (defun org-narrow-to-subtree ()
  7740. "Narrow buffer to the current subtree."
  7741. (interactive)
  7742. (save-excursion
  7743. (save-match-data
  7744. (org-with-limited-levels
  7745. (narrow-to-region
  7746. (progn (org-back-to-heading t) (point))
  7747. (progn (org-end-of-subtree t t)
  7748. (when (and (org-at-heading-p) (not (eobp))) (backward-char 1))
  7749. (point)))))))
  7750. (defun org-narrow-to-block ()
  7751. "Narrow buffer to the current block."
  7752. (interactive)
  7753. (let* ((case-fold-search t)
  7754. (blockp (org-between-regexps-p "^[ \t]*#\\+begin_.*"
  7755. "^[ \t]*#\\+end_.*")))
  7756. (if blockp
  7757. (narrow-to-region (car blockp) (cdr blockp))
  7758. (user-error "Not in a block"))))
  7759. (defun org-clone-subtree-with-time-shift (n &optional shift)
  7760. "Clone the task (subtree) at point N times.
  7761. The clones will be inserted as siblings.
  7762. In interactive use, the user will be prompted for the number of
  7763. clones to be produced. If the entry has a timestamp, the user
  7764. will also be prompted for a time shift, which may be a repeater
  7765. as used in time stamps, for example `+3d'. To disable this,
  7766. you can call the function with a universal prefix argument.
  7767. When a valid repeater is given and the entry contains any time
  7768. stamps, the clones will become a sequence in time, with time
  7769. stamps in the subtree shifted for each clone produced. If SHIFT
  7770. is nil or the empty string, time stamps will be left alone. The
  7771. ID property of the original subtree is removed.
  7772. If the original subtree did contain time stamps with a repeater,
  7773. the following will happen:
  7774. - the repeater will be removed in each clone
  7775. - an additional clone will be produced, with the current, unshifted
  7776. date(s) in the entry.
  7777. - the original entry will be placed *after* all the clones, with
  7778. repeater intact.
  7779. - the start days in the repeater in the original entry will be shifted
  7780. to past the last clone.
  7781. In this way you can spell out a number of instances of a repeating task,
  7782. and still retain the repeater to cover future instances of the task.
  7783. As described above, N+1 clones are produced when the original
  7784. subtree has a repeater. Setting N to 0, then, can be used to
  7785. remove the repeater from a subtree and create a shifted clone
  7786. with the original repeater."
  7787. (interactive "nNumber of clones to produce: ")
  7788. (let ((shift
  7789. (or shift
  7790. (if (and (not (equal current-prefix-arg '(4)))
  7791. (save-excursion
  7792. (re-search-forward org-ts-regexp-both
  7793. (save-excursion
  7794. (org-end-of-subtree t)
  7795. (point)) t)))
  7796. (read-from-minibuffer
  7797. "Date shift per clone (e.g. +1w, empty to copy unchanged): ")
  7798. ""))) ;; No time shift
  7799. (n-no-remove -1)
  7800. (drawer-re org-drawer-regexp)
  7801. (org-clock-re (format "^[ \t]*%s.*$" org-clock-string))
  7802. beg end template task idprop
  7803. shift-n shift-what doshift nmin nmax)
  7804. (unless (wholenump n)
  7805. (user-error "Invalid number of replications %s" n))
  7806. (when (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  7807. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([hdwmy]\\)[ \t]*\\'"
  7808. shift)))
  7809. (user-error "Invalid shift specification %s" shift))
  7810. (when doshift
  7811. (setq shift-n (string-to-number (match-string 1 shift))
  7812. shift-what (cdr (assoc (match-string 2 shift)
  7813. '(("d" . day) ("w" . week)
  7814. ("m" . month) ("y" . year))))))
  7815. (when (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  7816. (setq nmin 1 nmax n)
  7817. (org-back-to-heading t)
  7818. (setq beg (point))
  7819. (setq idprop (org-entry-get nil "ID"))
  7820. (org-end-of-subtree t t)
  7821. (or (bolp) (insert "\n"))
  7822. (setq end (point))
  7823. (setq template (buffer-substring beg end))
  7824. (when (and doshift
  7825. (string-match "<[^<>\n]+ [.+]?\\+[0-9]+[hdwmy][^<>\n]*>" template))
  7826. (delete-region beg end)
  7827. (setq end beg)
  7828. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  7829. (goto-char end)
  7830. (loop for n from nmin to nmax do
  7831. ;; prepare clone
  7832. (with-temp-buffer
  7833. (insert template)
  7834. (org-mode)
  7835. (goto-char (point-min))
  7836. (org-show-subtree)
  7837. (and idprop (if org-clone-delete-id
  7838. (org-entry-delete nil "ID")
  7839. (org-id-get-create t)))
  7840. (unless (= n 0)
  7841. (while (re-search-forward org-clock-re nil t)
  7842. (kill-whole-line))
  7843. (goto-char (point-min))
  7844. (while (re-search-forward drawer-re nil t)
  7845. (org-remove-empty-drawer-at (point))))
  7846. (goto-char (point-min))
  7847. (when doshift
  7848. (while (re-search-forward org-ts-regexp-both nil t)
  7849. (org-timestamp-change (* n shift-n) shift-what))
  7850. (unless (= n n-no-remove)
  7851. (goto-char (point-min))
  7852. (while (re-search-forward org-ts-regexp nil t)
  7853. (save-excursion
  7854. (goto-char (match-beginning 0))
  7855. (when (looking-at "<[^<>\n]+\\( +[.+]?\\+[0-9]+[hdwmy]\\)")
  7856. (delete-region (match-beginning 1) (match-end 1)))))))
  7857. (setq task (buffer-string)))
  7858. (insert task))
  7859. (goto-char beg)))
  7860. ;;; Outline Sorting
  7861. (defun org-sort (with-case)
  7862. "Call `org-sort-entries', `org-table-sort-lines' or `org-sort-list'.
  7863. Optional argument WITH-CASE means sort case-sensitively."
  7864. (interactive "P")
  7865. (cond
  7866. ((org-at-table-p) (org-call-with-arg 'org-table-sort-lines with-case))
  7867. ((org-at-item-p) (org-call-with-arg 'org-sort-list with-case))
  7868. (t
  7869. (org-call-with-arg 'org-sort-entries with-case))))
  7870. (defun org-sort-remove-invisible (s)
  7871. "Remove invisible links from string S."
  7872. (remove-text-properties 0 (length s) org-rm-props s)
  7873. (while (string-match org-bracket-link-regexp s)
  7874. (setq s (replace-match (if (match-end 2)
  7875. (match-string 3 s)
  7876. (match-string 1 s))
  7877. t t s)))
  7878. (let ((st (format " %s " s)))
  7879. (while (string-match org-emph-re st)
  7880. (setq st (replace-match (format " %s " (match-string 4 st)) t t st)))
  7881. (setq s (substring st 1 -1)))
  7882. s)
  7883. (defvar org-priority-regexp) ; defined later in the file
  7884. (defvar org-after-sorting-entries-or-items-hook nil
  7885. "Hook that is run after a bunch of entries or items have been sorted.
  7886. When children are sorted, the cursor is in the parent line when this
  7887. hook gets called. When a region or a plain list is sorted, the cursor
  7888. will be in the first entry of the sorted region/list.")
  7889. (defun org-sort-entries
  7890. (&optional with-case sorting-type getkey-func compare-func property)
  7891. "Sort entries on a certain level of an outline tree.
  7892. If there is an active region, the entries in the region are sorted.
  7893. Else, if the cursor is before the first entry, sort the top-level items.
  7894. Else, the children of the entry at point are sorted.
  7895. Sorting can be alphabetically, numerically, by date/time as given by
  7896. a time stamp, by a property, by priority order, or by a custom function.
  7897. The command prompts for the sorting type unless it has been given to the
  7898. function through the SORTING-TYPE argument, which needs to be a character,
  7899. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?o ?O ?r ?R ?f ?F ?k ?K). Here is
  7900. the precise meaning of each character:
  7901. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  7902. c By creation time, which is assumed to be the first inactive time stamp
  7903. at the beginning of a line.
  7904. d By deadline date/time.
  7905. k By clocking time.
  7906. n Numerically, by converting the beginning of the entry/item to a number.
  7907. o By order of TODO keywords.
  7908. p By priority according to the cookie.
  7909. r By the value of a property.
  7910. s By scheduled date/time.
  7911. t By date/time, either the first active time stamp in the entry, or, if
  7912. none exist, by the first inactive one.
  7913. Capital letters will reverse the sort order.
  7914. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  7915. called with point at the beginning of the record. It must return either
  7916. a string or a number that should serve as the sorting key for that record.
  7917. Comparing entries ignores case by default. However, with an optional argument
  7918. WITH-CASE, the sorting considers case as well.
  7919. Sorting is done against the visible part of the headlines, it ignores hidden
  7920. links.
  7921. When sorting is done, call `org-after-sorting-entries-or-items-hook'."
  7922. (interactive "P")
  7923. (let ((case-func (if with-case 'identity 'downcase))
  7924. (cmstr
  7925. ;; The clock marker is lost when using `sort-subr', let's
  7926. ;; store the clocking string.
  7927. (when (equal (marker-buffer org-clock-marker) (current-buffer))
  7928. (save-excursion
  7929. (goto-char org-clock-marker)
  7930. (buffer-substring-no-properties (line-beginning-position)
  7931. (point)))))
  7932. start beg end stars re re2
  7933. txt what tmp)
  7934. ;; Find beginning and end of region to sort
  7935. (cond
  7936. ((org-region-active-p)
  7937. ;; we will sort the region
  7938. (setq end (region-end)
  7939. what "region")
  7940. (goto-char (region-beginning))
  7941. (unless (org-at-heading-p) (outline-next-heading))
  7942. (setq start (point)))
  7943. ((or (org-at-heading-p)
  7944. (ignore-errors (progn (org-back-to-heading) t)))
  7945. ;; we will sort the children of the current headline
  7946. (org-back-to-heading)
  7947. (setq start (point)
  7948. end (progn (org-end-of-subtree t t)
  7949. (or (bolp) (insert "\n"))
  7950. (when (>= (org-back-over-empty-lines) 1)
  7951. (forward-line 1))
  7952. (point))
  7953. what "children")
  7954. (goto-char start)
  7955. (outline-show-subtree)
  7956. (outline-next-heading))
  7957. (t
  7958. ;; we will sort the top-level entries in this file
  7959. (goto-char (point-min))
  7960. (or (org-at-heading-p) (outline-next-heading))
  7961. (setq start (point))
  7962. (goto-char (point-max))
  7963. (beginning-of-line 1)
  7964. (when (looking-at ".*?\\S-")
  7965. ;; File ends in a non-white line
  7966. (end-of-line 1)
  7967. (insert "\n"))
  7968. (setq end (point-max))
  7969. (setq what "top-level")
  7970. (goto-char start)
  7971. (outline-show-all)))
  7972. (setq beg (point))
  7973. (when (>= beg end) (goto-char start) (user-error "Nothing to sort"))
  7974. (looking-at "\\(\\*+\\)")
  7975. (setq stars (match-string 1)
  7976. re (concat "^" (regexp-quote stars) " +")
  7977. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[ \t\n]")
  7978. txt (buffer-substring beg end))
  7979. (unless (equal (substring txt -1) "\n") (setq txt (concat txt "\n")))
  7980. (when (and (not (equal stars "*")) (string-match re2 txt))
  7981. (user-error "Region to sort contains a level above the first entry"))
  7982. (unless sorting-type
  7983. (message
  7984. "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
  7985. [t]ime [s]cheduled [d]eadline [c]reated cloc[k]ing
  7986. A/N/P/R/O/F/T/S/D/C/K means reversed:"
  7987. what)
  7988. (setq sorting-type (read-char-exclusive))
  7989. (unless getkey-func
  7990. (and (= (downcase sorting-type) ?f)
  7991. (setq getkey-func
  7992. (org-icompleting-read "Sort using function: "
  7993. obarray 'fboundp t nil nil))
  7994. (setq getkey-func (intern getkey-func))))
  7995. (and (= (downcase sorting-type) ?r)
  7996. (not property)
  7997. (setq property
  7998. (org-icompleting-read "Property: "
  7999. (mapcar 'list (org-buffer-property-keys t))
  8000. nil t))))
  8001. (when (member sorting-type '(?k ?K)) (org-clock-sum))
  8002. (message "Sorting entries...")
  8003. (save-restriction
  8004. (narrow-to-region start end)
  8005. (let ((dcst (downcase sorting-type))
  8006. (case-fold-search nil)
  8007. (now (current-time)))
  8008. (sort-subr
  8009. (/= dcst sorting-type)
  8010. ;; This function moves to the beginning character of the "record" to
  8011. ;; be sorted.
  8012. (lambda nil
  8013. (if (re-search-forward re nil t)
  8014. (goto-char (match-beginning 0))
  8015. (goto-char (point-max))))
  8016. ;; This function moves to the last character of the "record" being
  8017. ;; sorted.
  8018. (lambda nil
  8019. (save-match-data
  8020. (condition-case nil
  8021. (outline-forward-same-level 1)
  8022. (error
  8023. (goto-char (point-max))))))
  8024. ;; This function returns the value that gets sorted against.
  8025. (lambda nil
  8026. (cond
  8027. ((= dcst ?n)
  8028. (if (looking-at org-complex-heading-regexp)
  8029. (string-to-number (org-sort-remove-invisible (match-string 4)))
  8030. nil))
  8031. ((= dcst ?a)
  8032. (if (looking-at org-complex-heading-regexp)
  8033. (funcall case-func (org-sort-remove-invisible (match-string 4)))
  8034. nil))
  8035. ((= dcst ?k)
  8036. (or (get-text-property (point) :org-clock-minutes) 0))
  8037. ((= dcst ?t)
  8038. (let ((end (save-excursion (outline-next-heading) (point))))
  8039. (if (or (re-search-forward org-ts-regexp end t)
  8040. (re-search-forward org-ts-regexp-both end t))
  8041. (org-time-string-to-seconds (match-string 0))
  8042. (org-float-time now))))
  8043. ((= dcst ?c)
  8044. (let ((end (save-excursion (outline-next-heading) (point))))
  8045. (if (re-search-forward
  8046. (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
  8047. end t)
  8048. (org-time-string-to-seconds (match-string 0))
  8049. (org-float-time now))))
  8050. ((= dcst ?s)
  8051. (let ((end (save-excursion (outline-next-heading) (point))))
  8052. (if (re-search-forward org-scheduled-time-regexp end t)
  8053. (org-time-string-to-seconds (match-string 1))
  8054. (org-float-time now))))
  8055. ((= dcst ?d)
  8056. (let ((end (save-excursion (outline-next-heading) (point))))
  8057. (if (re-search-forward org-deadline-time-regexp end t)
  8058. (org-time-string-to-seconds (match-string 1))
  8059. (org-float-time now))))
  8060. ((= dcst ?p)
  8061. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  8062. (string-to-char (match-string 2))
  8063. org-default-priority))
  8064. ((= dcst ?r)
  8065. (or (org-entry-get nil property) ""))
  8066. ((= dcst ?o)
  8067. (when (looking-at org-complex-heading-regexp)
  8068. (let* ((m (match-string 2))
  8069. (s (if (member m org-done-keywords) '- '+)))
  8070. (- 99 (funcall s (length (member m org-todo-keywords-1)))))))
  8071. ((= dcst ?f)
  8072. (if getkey-func
  8073. (progn
  8074. (setq tmp (funcall getkey-func))
  8075. (when (stringp tmp) (setq tmp (funcall case-func tmp)))
  8076. tmp)
  8077. (error "Invalid key function `%s'" getkey-func)))
  8078. (t (error "Invalid sorting type `%c'" sorting-type))))
  8079. nil
  8080. (cond
  8081. ((= dcst ?a) 'string<)
  8082. ((= dcst ?f) compare-func)
  8083. ((member dcst '(?p ?t ?s ?d ?c ?k)) '<)))))
  8084. (run-hooks 'org-after-sorting-entries-or-items-hook)
  8085. ;; Reset the clock marker if needed
  8086. (when cmstr
  8087. (save-excursion
  8088. (goto-char start)
  8089. (search-forward cmstr nil t)
  8090. (move-marker org-clock-marker (point))))
  8091. (message "Sorting entries...done")))
  8092. ;;; The orgstruct minor mode
  8093. ;; Define a minor mode which can be used in other modes in order to
  8094. ;; integrate the org-mode structure editing commands.
  8095. ;; This is really a hack, because the org-mode structure commands use
  8096. ;; keys which normally belong to the major mode. Here is how it
  8097. ;; works: The minor mode defines all the keys necessary to operate the
  8098. ;; structure commands, but wraps the commands into a function which
  8099. ;; tests if the cursor is currently at a headline or a plain list
  8100. ;; item. If that is the case, the structure command is used,
  8101. ;; temporarily setting many Org-mode variables like regular
  8102. ;; expressions for filling etc. However, when any of those keys is
  8103. ;; used at a different location, function uses `key-binding' to look
  8104. ;; up if the key has an associated command in another currently active
  8105. ;; keymap (minor modes, major mode, global), and executes that
  8106. ;; command. There might be problems if any of the keys is otherwise
  8107. ;; used as a prefix key.
  8108. (defcustom orgstruct-heading-prefix-regexp ""
  8109. "Regexp that matches the custom prefix of Org headlines in
  8110. orgstruct(++)-mode."
  8111. :group 'org
  8112. :version "24.4"
  8113. :package-version '(Org . "8.3")
  8114. :type 'regexp)
  8115. ;;;###autoload(put 'orgstruct-heading-prefix-regexp 'safe-local-variable 'stringp)
  8116. (defcustom orgstruct-setup-hook nil
  8117. "Hook run after orgstruct-mode-map is filled."
  8118. :group 'org
  8119. :version "24.4"
  8120. :package-version '(Org . "8.0")
  8121. :type 'hook)
  8122. (defvar orgstruct-initialized nil)
  8123. (defvar org-local-vars nil
  8124. "List of local variables, for use by `orgstruct-mode'.")
  8125. ;;;###autoload
  8126. (define-minor-mode orgstruct-mode
  8127. "Toggle the minor mode `orgstruct-mode'.
  8128. This mode is for using Org-mode structure commands in other
  8129. modes. The following keys behave as if Org-mode were active, if
  8130. the cursor is on a headline, or on a plain list item (both as
  8131. defined by Org-mode)."
  8132. nil " OrgStruct" (make-sparse-keymap)
  8133. (funcall (if orgstruct-mode
  8134. 'add-to-invisibility-spec
  8135. 'remove-from-invisibility-spec)
  8136. '(outline . t))
  8137. (when orgstruct-mode
  8138. (org-load-modules-maybe)
  8139. (unless orgstruct-initialized
  8140. (orgstruct-setup)
  8141. (setq orgstruct-initialized t))))
  8142. ;;;###autoload
  8143. (defun turn-on-orgstruct ()
  8144. "Unconditionally turn on `orgstruct-mode'."
  8145. (orgstruct-mode 1))
  8146. (defvar-local orgstruct-is-++ nil
  8147. "Is `orgstruct-mode' in ++ version in the current-buffer?")
  8148. (defvar-local org-fb-vars nil)
  8149. (defun orgstruct++-mode (&optional arg)
  8150. "Toggle `orgstruct-mode', the enhanced version of it.
  8151. In addition to setting orgstruct-mode, this also exports all
  8152. indentation and autofilling variables from org-mode into the
  8153. buffer. It will also recognize item context in multiline items."
  8154. (interactive "P")
  8155. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  8156. (if (< arg 1)
  8157. (progn (orgstruct-mode -1)
  8158. (mapc (lambda(v)
  8159. (set (make-local-variable (car v))
  8160. (if (eq (car-safe (cadr v)) 'quote) (cadadr v) (cadr v))))
  8161. org-fb-vars))
  8162. (orgstruct-mode 1)
  8163. (setq org-fb-vars nil)
  8164. (unless org-local-vars
  8165. (setq org-local-vars (org-get-local-variables)))
  8166. (let (var val)
  8167. (mapc
  8168. (lambda (x)
  8169. (when (string-match
  8170. "^\\(paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|fill-prefix\\|indent-\\)"
  8171. (symbol-name (car x)))
  8172. (setq var (car x) val (nth 1 x))
  8173. (push (list var `(quote ,(eval var))) org-fb-vars)
  8174. (set (make-local-variable var)
  8175. (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  8176. org-local-vars)
  8177. (setq-local orgstruct-is-++ t))))
  8178. ;;;###autoload
  8179. (defun turn-on-orgstruct++ ()
  8180. "Unconditionally turn on `orgstruct++-mode'."
  8181. (orgstruct++-mode 1))
  8182. (defun orgstruct-error ()
  8183. "Error when there is no default binding for a structure key."
  8184. (interactive)
  8185. (funcall (if (fboundp 'user-error)
  8186. 'user-error
  8187. 'error)
  8188. "This key has no function outside structure elements"))
  8189. (defun orgstruct-setup ()
  8190. "Setup orgstruct keymap."
  8191. (dolist (cell '((org-demote . t)
  8192. (org-metaleft . t)
  8193. (org-metaright . t)
  8194. (org-promote . t)
  8195. (org-shiftmetaleft . t)
  8196. (org-shiftmetaright . t)
  8197. org-backward-element
  8198. org-backward-heading-same-level
  8199. org-ctrl-c-ret
  8200. org-ctrl-c-minus
  8201. org-ctrl-c-star
  8202. org-cycle
  8203. org-forward-heading-same-level
  8204. org-insert-heading
  8205. org-insert-heading-respect-content
  8206. org-kill-note-or-show-branches
  8207. org-mark-subtree
  8208. org-meta-return
  8209. org-metadown
  8210. org-metaup
  8211. org-narrow-to-subtree
  8212. org-promote-subtree
  8213. org-reveal
  8214. org-shiftdown
  8215. org-shiftleft
  8216. org-shiftmetadown
  8217. org-shiftmetaup
  8218. org-shiftright
  8219. org-shifttab
  8220. org-shifttab
  8221. org-shiftup
  8222. org-show-children
  8223. org-show-subtree
  8224. org-sort
  8225. org-up-element
  8226. outline-demote
  8227. outline-next-visible-heading
  8228. outline-previous-visible-heading
  8229. outline-promote
  8230. outline-up-heading))
  8231. (let ((f (or (car-safe cell) cell))
  8232. (disable-when-heading-prefix (cdr-safe cell)))
  8233. (when (fboundp f)
  8234. (let ((new-bindings))
  8235. (dolist (binding (nconc (where-is-internal f org-mode-map)
  8236. (where-is-internal f outline-mode-map)))
  8237. (push binding new-bindings)
  8238. ;; TODO use local-function-key-map
  8239. (dolist (rep '(("<tab>" . "TAB")
  8240. ("<return>" . "RET")
  8241. ("<escape>" . "ESC")
  8242. ("<delete>" . "DEL")))
  8243. (setq binding (read-kbd-macro
  8244. (let ((case-fold-search))
  8245. (replace-regexp-in-string
  8246. (regexp-quote (cdr rep))
  8247. (car rep)
  8248. (key-description binding)))))
  8249. (pushnew binding new-bindings :test 'equal)))
  8250. (dolist (binding new-bindings)
  8251. (let ((key (lookup-key orgstruct-mode-map binding)))
  8252. (when (or (not key) (numberp key))
  8253. (ignore-errors
  8254. (org-defkey orgstruct-mode-map
  8255. binding
  8256. (orgstruct-make-binding
  8257. f binding disable-when-heading-prefix))))))))))
  8258. (run-hooks 'orgstruct-setup-hook))
  8259. (defun orgstruct-make-binding (fun key disable-when-heading-prefix)
  8260. "Create a function for binding in the structure minor mode.
  8261. FUN is the command to call inside a table. KEY is the key that
  8262. should be checked in for a command to execute outside of tables.
  8263. Non-nil `disable-when-heading-prefix' means to disable the command
  8264. if `orgstruct-heading-prefix-regexp' is not empty."
  8265. (let ((name (concat "orgstruct-hijacker-" (symbol-name fun))))
  8266. (let ((nname name)
  8267. (i 0))
  8268. (while (fboundp (intern nname))
  8269. (setq nname (format "%s-%d" name (setq i (1+ i)))))
  8270. (setq name (intern nname)))
  8271. (eval
  8272. (let ((bindings '((org-heading-regexp
  8273. (concat "^"
  8274. orgstruct-heading-prefix-regexp
  8275. "\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ ]*$"))
  8276. (org-outline-regexp
  8277. (concat orgstruct-heading-prefix-regexp "\\*+ "))
  8278. (org-outline-regexp-bol
  8279. (concat "^" org-outline-regexp))
  8280. (outline-regexp org-outline-regexp)
  8281. (outline-heading-end-regexp "\n")
  8282. (outline-level 'org-outline-level)
  8283. (outline-heading-alist))))
  8284. `(defun ,name (arg)
  8285. ,(concat "In Structure, run `" (symbol-name fun) "'.\n"
  8286. "Outside of structure, run the binding of `"
  8287. (key-description key) "'."
  8288. (when disable-when-heading-prefix
  8289. (concat
  8290. "\nIf `orgstruct-heading-prefix-regexp' is not empty, this command will always fall\n"
  8291. "back to the default binding due to limitations of Org's implementation of\n"
  8292. "`" (symbol-name fun) "'.")))
  8293. (interactive "p")
  8294. (let* ((disable
  8295. ,(and disable-when-heading-prefix
  8296. '(not (string= orgstruct-heading-prefix-regexp ""))))
  8297. (fallback
  8298. (or disable
  8299. (not
  8300. (let* ,bindings
  8301. (org-context-p 'headline 'item
  8302. ,(when (memq fun
  8303. '(org-insert-heading
  8304. org-insert-heading-respect-content
  8305. org-meta-return))
  8306. '(when orgstruct-is-++
  8307. 'item-body))))))))
  8308. (if fallback
  8309. (let* ((orgstruct-mode)
  8310. (binding
  8311. (let ((key ,key))
  8312. (catch 'exit
  8313. (dolist
  8314. (rep
  8315. '(nil
  8316. ("<\\([^>]*\\)tab>" . "\\1TAB")
  8317. ("<\\([^>]*\\)return>" . "\\1RET")
  8318. ("<\\([^>]*\\)escape>" . "\\1ESC")
  8319. ("<\\([^>]*\\)delete>" . "\\1DEL"))
  8320. nil)
  8321. (when rep
  8322. (setq key (read-kbd-macro
  8323. (let ((case-fold-search))
  8324. (replace-regexp-in-string
  8325. (car rep)
  8326. (cdr rep)
  8327. (key-description key))))))
  8328. (when (key-binding key)
  8329. (throw 'exit (key-binding key))))))))
  8330. (if (keymapp binding)
  8331. (org-set-transient-map binding)
  8332. (let ((func (or binding
  8333. (unless disable
  8334. 'orgstruct-error))))
  8335. (when func
  8336. (call-interactively func)))))
  8337. (org-run-like-in-org-mode
  8338. (lambda ()
  8339. (interactive)
  8340. (let* ,bindings
  8341. (call-interactively ',fun)))))))))
  8342. name))
  8343. (defun org-contextualize-keys (alist contexts)
  8344. "Return valid elements in ALIST depending on CONTEXTS.
  8345. `org-agenda-custom-commands' or `org-capture-templates' are the
  8346. values used for ALIST, and `org-agenda-custom-commands-contexts'
  8347. or `org-capture-templates-contexts' are the associated contexts
  8348. definitions."
  8349. (let ((contexts
  8350. ;; normalize contexts
  8351. (mapcar
  8352. (lambda(c) (cond ((listp (cadr c))
  8353. (list (car c) (car c) (cadr c)))
  8354. ((string= "" (cadr c))
  8355. (list (car c) (car c) (caddr c)))
  8356. (t c)))
  8357. contexts))
  8358. (a alist) r s)
  8359. ;; loop over all commands or templates
  8360. (dolist (c a)
  8361. (let (vrules repl)
  8362. (cond
  8363. ((not (assoc (car c) contexts))
  8364. (push c r))
  8365. ((and (assoc (car c) contexts)
  8366. (setq vrules (org-contextualize-validate-key
  8367. (car c) contexts)))
  8368. (mapc (lambda (vr)
  8369. (unless (equal (car vr) (cadr vr))
  8370. (setq repl vr)))
  8371. vrules)
  8372. (if (not repl) (push c r)
  8373. (push (cadr repl) s)
  8374. (push
  8375. (cons (car c)
  8376. (cdr (or (assoc (cadr repl) alist)
  8377. (error "Undefined key `%s' as contextual replacement for `%s'"
  8378. (cadr repl) (car c)))))
  8379. r))))))
  8380. ;; Return limited ALIST, possibly with keys modified, and deduplicated
  8381. (delq
  8382. nil
  8383. (delete-dups
  8384. (mapcar (lambda (x)
  8385. (let ((tpl (car x)))
  8386. (unless (delq
  8387. nil
  8388. (mapcar (lambda (y)
  8389. (equal y tpl))
  8390. s))
  8391. x)))
  8392. (reverse r))))))
  8393. (defun org-contextualize-validate-key (key contexts)
  8394. "Check CONTEXTS for agenda or capture KEY."
  8395. (let (rr res)
  8396. (dolist (r contexts)
  8397. (mapc
  8398. (lambda (rr)
  8399. (when
  8400. (and (equal key (car r))
  8401. (if (functionp rr) (funcall rr)
  8402. (or (and (eq (car rr) 'in-file)
  8403. (buffer-file-name)
  8404. (string-match (cdr rr) (buffer-file-name)))
  8405. (and (eq (car rr) 'in-mode)
  8406. (string-match (cdr rr) (symbol-name major-mode)))
  8407. (and (eq (car rr) 'in-buffer)
  8408. (string-match (cdr rr) (buffer-name)))
  8409. (when (and (eq (car rr) 'not-in-file)
  8410. (buffer-file-name))
  8411. (not (string-match (cdr rr) (buffer-file-name))))
  8412. (when (eq (car rr) 'not-in-mode)
  8413. (not (string-match (cdr rr) (symbol-name major-mode))))
  8414. (when (eq (car rr) 'not-in-buffer)
  8415. (not (string-match (cdr rr) (buffer-name)))))))
  8416. (push r res)))
  8417. (car (last r))))
  8418. (delete-dups (delq nil res))))
  8419. (defun org-context-p (&rest contexts)
  8420. "Check if local context is any of CONTEXTS.
  8421. Possible values in the list of contexts are `table', `headline', and `item'."
  8422. (let ((pos (point)))
  8423. (goto-char (point-at-bol))
  8424. (prog1 (or (and (memq 'table contexts)
  8425. (looking-at "[ \t]*|"))
  8426. (and (memq 'headline contexts)
  8427. (looking-at org-outline-regexp))
  8428. (and (memq 'item contexts)
  8429. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  8430. (and (memq 'item-body contexts)
  8431. (org-in-item-p)))
  8432. (goto-char pos))))
  8433. (defconst org-unique-local-variables
  8434. '(org-element--cache
  8435. org-element--cache-objects
  8436. org-element--cache-sync-keys
  8437. org-element--cache-sync-requests
  8438. org-element--cache-sync-timer)
  8439. "List of local variables that cannot be transferred to another buffer.")
  8440. (defun org-get-local-variables ()
  8441. "Return a list of all local variables in an Org mode buffer."
  8442. (delq nil
  8443. (mapcar
  8444. (lambda (x)
  8445. (let* ((binding (if (symbolp x) (list x) (list (car x) (cdr x))))
  8446. (name (car binding)))
  8447. (and (not (get name 'org-state))
  8448. (not (memq name org-unique-local-variables))
  8449. (string-match-p
  8450. "\\`\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|\
  8451. auto-fill\\|normal-auto-fill\\|fill-paragraph\\|indent-\\)"
  8452. (symbol-name name))
  8453. binding)))
  8454. (with-temp-buffer
  8455. (org-mode)
  8456. (buffer-local-variables)))))
  8457. (defun org-clone-local-variables (from-buffer &optional regexp)
  8458. "Clone local variables from FROM-BUFFER.
  8459. Optional argument REGEXP selects variables to clone."
  8460. (dolist (pair (buffer-local-variables from-buffer))
  8461. (let ((name (car pair)))
  8462. (when (and (symbolp name)
  8463. (not (memq name org-unique-local-variables))
  8464. (or (null regexp) (string-match regexp (symbol-name name))))
  8465. (set (make-local-variable name) (cdr pair))))))
  8466. ;;;###autoload
  8467. (defun org-run-like-in-org-mode (cmd)
  8468. "Run a command, pretending that the current buffer is in Org-mode.
  8469. This will temporarily bind local variables that are typically bound in
  8470. Org-mode to the values they have in Org-mode, and then interactively
  8471. call CMD."
  8472. (org-load-modules-maybe)
  8473. (unless org-local-vars
  8474. (setq org-local-vars (org-get-local-variables)))
  8475. (let (binds)
  8476. (dolist (var org-local-vars)
  8477. (when (or (not (boundp (car var)))
  8478. (eq (symbol-value (car var))
  8479. (default-value (car var))))
  8480. (push (list (car var) `(quote ,(cadr var))) binds)))
  8481. (eval `(let ,binds
  8482. (call-interactively (quote ,cmd))))))
  8483. (defun org-get-category (&optional pos force-refresh)
  8484. "Get the category applying to position POS."
  8485. (save-match-data
  8486. (when force-refresh (org-refresh-category-properties))
  8487. (let ((pos (or pos (point))))
  8488. (or (get-text-property pos 'org-category)
  8489. (progn (org-refresh-category-properties)
  8490. (get-text-property pos 'org-category))))))
  8491. ;;; Refresh properties
  8492. (defun org-refresh-properties (dprop tprop)
  8493. "Refresh buffer text properties.
  8494. DPROP is the drawer property and TPROP is either the
  8495. corresponding text property to set, or an alist with each element
  8496. being a text property (as a symbol) and a function to apply to
  8497. the value of the drawer property."
  8498. (let ((case-fold-search t)
  8499. (inhibit-read-only t))
  8500. (org-with-silent-modifications
  8501. (save-excursion
  8502. (save-restriction
  8503. (widen)
  8504. (goto-char (point-min))
  8505. (while (re-search-forward (concat "^[ \t]*:" dprop ": +\\(.*\\)[ \t]*$") nil t)
  8506. (org-refresh-property tprop (org-match-string-no-properties 1))))))))
  8507. (defun org-refresh-property (tprop p)
  8508. "Refresh the buffer text property TPROP from the drawer property P.
  8509. The refresh happens only for the current tree (not subtree)."
  8510. (unless (org-before-first-heading-p)
  8511. (save-excursion
  8512. (org-back-to-heading t)
  8513. (if (symbolp tprop)
  8514. ;; TPROP is a text property symbol
  8515. (put-text-property
  8516. (point) (or (outline-next-heading) (point-max)) tprop p)
  8517. ;; TPROP is an alist with (properties . function) elements
  8518. (dolist (al tprop)
  8519. (save-excursion
  8520. (put-text-property
  8521. (line-beginning-position) (or (outline-next-heading) (point-max))
  8522. (car al)
  8523. (funcall (cdr al) p))))))))
  8524. (defun org-refresh-category-properties ()
  8525. "Refresh category text properties in the buffer."
  8526. (let ((case-fold-search t)
  8527. (inhibit-read-only t)
  8528. (default-category
  8529. (cond ((null org-category)
  8530. (if buffer-file-name
  8531. (file-name-sans-extension
  8532. (file-name-nondirectory buffer-file-name))
  8533. "???"))
  8534. ((symbolp org-category) (symbol-name org-category))
  8535. (t org-category))))
  8536. (org-with-silent-modifications
  8537. (org-with-wide-buffer
  8538. ;; Set buffer-wide category. Search last #+CATEGORY keyword.
  8539. ;; This is the default category for the buffer. If none is
  8540. ;; found, fall-back to `org-category' or buffer file name.
  8541. (put-text-property
  8542. (point-min) (point-max)
  8543. 'org-category
  8544. (catch 'buffer-category
  8545. (goto-char (point-max))
  8546. (while (re-search-backward "^[ \t]*#\\+CATEGORY:" (point-min) t)
  8547. (let ((element (org-element-at-point)))
  8548. (when (eq (org-element-type element) 'keyword)
  8549. (throw 'buffer-category
  8550. (org-element-property :value element)))))
  8551. default-category))
  8552. ;; Set sub-tree specific categories.
  8553. (goto-char (point-min))
  8554. (let ((regexp (org-re-property "CATEGORY")))
  8555. (while (re-search-forward regexp nil t)
  8556. (let ((value (org-match-string-no-properties 3)))
  8557. (when (org-at-property-p)
  8558. (put-text-property
  8559. (save-excursion (org-back-to-heading t) (point))
  8560. (save-excursion (org-end-of-subtree t t) (point))
  8561. 'org-category
  8562. value)))))))))
  8563. (defun org-refresh-stats-properties ()
  8564. "Refresh stats text properties in the buffer."
  8565. (let (stats)
  8566. (org-with-silent-modifications
  8567. (save-excursion
  8568. (save-restriction
  8569. (widen)
  8570. (goto-char (point-min))
  8571. (while (re-search-forward
  8572. (concat org-outline-regexp-bol ".*"
  8573. "\\(?:\\[\\([0-9]+\\)%\\|\\([0-9]+\\)/\\([0-9]+\\)\\]\\)")
  8574. nil t)
  8575. (setq stats (cond ((equal (match-string 3) "0") 0)
  8576. ((match-string 2)
  8577. (/ (* (string-to-number (match-string 2)) 100)
  8578. (string-to-number (match-string 3))))
  8579. (t (string-to-number (match-string 1)))))
  8580. (org-back-to-heading t)
  8581. (put-text-property (point) (progn (org-end-of-subtree t t) (point))
  8582. 'org-stats stats)))))))
  8583. (defun org-refresh-effort-properties ()
  8584. "Refresh effort properties"
  8585. (org-refresh-properties
  8586. org-effort-property
  8587. '((effort . identity)
  8588. (effort-minutes . org-duration-string-to-minutes))))
  8589. ;;;; Link Stuff
  8590. ;;; Link abbreviations
  8591. (defun org-link-expand-abbrev (link)
  8592. "Apply replacements as defined in `org-link-abbrev-alist'."
  8593. (if (string-match "^\\([^:]*\\)\\(::?\\(.*\\)\\)?$" link)
  8594. (let* ((key (match-string 1 link))
  8595. (as (or (assoc key org-link-abbrev-alist-local)
  8596. (assoc key org-link-abbrev-alist)))
  8597. (tag (and (match-end 2) (match-string 3 link)))
  8598. rpl)
  8599. (if (not as)
  8600. link
  8601. (setq rpl (cdr as))
  8602. (cond
  8603. ((symbolp rpl) (funcall rpl tag))
  8604. ((string-match "%(\\([^)]+\\))" rpl)
  8605. (replace-match
  8606. (save-match-data
  8607. (funcall (intern-soft (match-string 1 rpl)) tag)) t t rpl))
  8608. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  8609. ((string-match "%h" rpl)
  8610. (replace-match (url-hexify-string (or tag "")) t t rpl))
  8611. (t (concat rpl tag)))))
  8612. link))
  8613. ;;; Storing and inserting links
  8614. (defvar org-insert-link-history nil
  8615. "Minibuffer history for links inserted with `org-insert-link'.")
  8616. (defvar org-stored-links nil
  8617. "Contains the links stored with `org-store-link'.")
  8618. (defvar org-store-link-plist nil
  8619. "Plist with info about the most recently link created with `org-store-link'.")
  8620. (defvar org-link-protocols nil
  8621. "Link protocols added to Org-mode using `org-add-link-type'.")
  8622. (defvar org-store-link-functions nil
  8623. "List of functions that are called to create and store a link.
  8624. Each function will be called in turn until one returns a non-nil
  8625. value. Each function should check if it is responsible for creating
  8626. this link (for example by looking at the major mode).
  8627. If not, it must exit and return nil.
  8628. If yes, it should return a non-nil value after a calling
  8629. `org-store-link-props' with a list of properties and values.
  8630. Special properties are:
  8631. :type The link prefix, like \"http\". This must be given.
  8632. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  8633. This is obligatory as well.
  8634. :description Optional default description for the second pair
  8635. of brackets in an Org-mode link. The user can still change
  8636. this when inserting this link into an Org-mode buffer.
  8637. In addition to these, any additional properties can be specified
  8638. and then used in capture templates.")
  8639. (defun org-add-link-type (type &optional follow export)
  8640. "Add TYPE to the list of `org-link-types'.
  8641. Re-compute all regular expressions depending on `org-link-types'
  8642. FOLLOW and EXPORT are two functions.
  8643. FOLLOW should take the link path as the single argument and do whatever
  8644. is necessary to follow the link, for example find a file or display
  8645. a mail message.
  8646. EXPORT should format the link path for export to one of the export formats.
  8647. It should be a function accepting three arguments:
  8648. path the path of the link, the text after the prefix (like \"http:\")
  8649. desc the description of the link, if any
  8650. format the export format, a symbol like `html' or `latex' or `ascii'.
  8651. The function may use the FORMAT information to return different values
  8652. depending on the format. The return value will be put literally into
  8653. the exported file. If the return value is nil, this means Org should
  8654. do what it normally does with links which do not have EXPORT defined.
  8655. Org mode has a built-in default for exporting links. If you are happy with
  8656. this default, there is no need to define an export function for the link
  8657. type. For a simple example of an export function, see `org-bbdb.el'."
  8658. (add-to-list 'org-link-types type t)
  8659. (org-make-link-regexps)
  8660. (org-element-update-syntax)
  8661. (if (assoc type org-link-protocols)
  8662. (setcdr (assoc type org-link-protocols) (list follow export))
  8663. (push (list type follow export) org-link-protocols)))
  8664. (defvar org-agenda-buffer-name) ; Defined in org-agenda.el
  8665. (defvar org-id-link-to-org-use-id) ; Defined in org-id.el
  8666. ;;;###autoload
  8667. (defun org-store-link (arg)
  8668. "\\<org-mode-map>Store an org-link to the current location.
  8669. This link is added to `org-stored-links' and can later be inserted
  8670. into an Org buffer with \\[org-insert-link].
  8671. For some link types, a prefix ARG is interpreted.
  8672. For links to Usenet articles, ARG negates `org-gnus-prefer-web-links'.
  8673. For file links, ARG negates `org-context-in-file-links'.
  8674. A double prefix ARG force skipping storing functions that are not
  8675. part of Org's core.
  8676. A triple prefix ARG force storing a link for each line in the
  8677. active region."
  8678. (interactive "P")
  8679. (org-load-modules-maybe)
  8680. (if (and (equal arg '(64)) (org-region-active-p))
  8681. (save-excursion
  8682. (let ((end (region-end)))
  8683. (goto-char (region-beginning))
  8684. (set-mark (point))
  8685. (while (< (point-at-eol) end)
  8686. (move-end-of-line 1) (activate-mark)
  8687. (let (current-prefix-arg)
  8688. (call-interactively 'org-store-link))
  8689. (move-beginning-of-line 2)
  8690. (set-mark (point)))))
  8691. (org-with-limited-levels
  8692. (setq org-store-link-plist nil)
  8693. (let (link cpltxt desc description search
  8694. txt custom-id agenda-link sfuns sfunsn)
  8695. (cond
  8696. ;; Store a link using an external link type
  8697. ((and (not (equal arg '(16)))
  8698. (setq sfuns
  8699. (delq
  8700. nil (mapcar (lambda (f)
  8701. (let (fs) (if (funcall f) (push f fs))))
  8702. org-store-link-functions))
  8703. sfunsn (mapcar (lambda (fu) (symbol-name (car fu))) sfuns))
  8704. (or (and (cdr sfuns)
  8705. (funcall (intern
  8706. (completing-read
  8707. "Which function for creating the link? "
  8708. sfunsn nil t (car sfunsn)))))
  8709. (funcall (caar sfuns)))
  8710. (setq link (plist-get org-store-link-plist :link)
  8711. desc (or (plist-get org-store-link-plist
  8712. :description)
  8713. link))))
  8714. ;; Store a link from a source code buffer.
  8715. ((org-src-edit-buffer-p)
  8716. (cond
  8717. ((save-excursion
  8718. (beginning-of-line)
  8719. (looking-at (concat (format org-coderef-label-format "\\(.*?\\)")
  8720. "[ \t]*$")))
  8721. (setq link (format "(%s)" (org-match-string-no-properties 1))))
  8722. ((org-called-interactively-p 'any)
  8723. (let (label)
  8724. (while (or (not label)
  8725. (org-with-wide-buffer
  8726. (goto-char (point-min))
  8727. (re-search-forward
  8728. (regexp-quote (format org-coderef-label-format label))
  8729. nil t)))
  8730. (when label (message "Label exists already") (sit-for 2))
  8731. (setq label (read-string "Code line label: " label)))
  8732. (end-of-line)
  8733. (setq link (format org-coderef-label-format label))
  8734. (let ((gc (- 79 (length link))))
  8735. (if (< (current-column) gc) (org-move-to-column gc t)
  8736. (insert " ")))
  8737. (insert link)
  8738. (setq link (concat "(" label ")") desc nil)))
  8739. (t (setq link nil))))
  8740. ;; We are in the agenda, link to referenced location
  8741. ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
  8742. (let ((m (or (get-text-property (point) 'org-hd-marker)
  8743. (get-text-property (point) 'org-marker))))
  8744. (when m
  8745. (org-with-point-at m
  8746. (setq agenda-link
  8747. (if (org-called-interactively-p 'any)
  8748. (call-interactively 'org-store-link)
  8749. (org-store-link nil)))))))
  8750. ((eq major-mode 'calendar-mode)
  8751. (let ((cd (calendar-cursor-to-date)))
  8752. (setq link
  8753. (format-time-string
  8754. (car org-time-stamp-formats)
  8755. (apply 'encode-time
  8756. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  8757. nil nil nil))))
  8758. (org-store-link-props :type "calendar" :date cd)))
  8759. ((eq major-mode 'help-mode)
  8760. (setq link (concat "help:" (save-excursion
  8761. (goto-char (point-min))
  8762. (looking-at "^[^ ]+")
  8763. (match-string 0))))
  8764. (org-store-link-props :type "help"))
  8765. ((eq major-mode 'w3-mode)
  8766. (setq cpltxt (if (and (buffer-name)
  8767. (not (string-match "Untitled" (buffer-name))))
  8768. (buffer-name)
  8769. (url-view-url t))
  8770. link (url-view-url t))
  8771. (org-store-link-props :type "w3" :url (url-view-url t)))
  8772. ((eq major-mode 'image-mode)
  8773. (setq cpltxt (concat "file:"
  8774. (abbreviate-file-name buffer-file-name))
  8775. link cpltxt)
  8776. (org-store-link-props :type "image" :file buffer-file-name))
  8777. ;; In dired, store a link to the file of the current line
  8778. ((derived-mode-p 'dired-mode)
  8779. (let ((file (dired-get-filename nil t)))
  8780. (setq file (if file
  8781. (abbreviate-file-name
  8782. (expand-file-name (dired-get-filename nil t)))
  8783. ;; otherwise, no file so use current directory.
  8784. default-directory))
  8785. (setq cpltxt (concat "file:" file)
  8786. link cpltxt)))
  8787. ((setq search (run-hook-with-args-until-success
  8788. 'org-create-file-search-functions))
  8789. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  8790. "::" search))
  8791. (setq cpltxt (or description link)))
  8792. ((and (buffer-file-name (buffer-base-buffer)) (derived-mode-p 'org-mode))
  8793. (setq custom-id (org-entry-get nil "CUSTOM_ID"))
  8794. (cond
  8795. ;; Store a link using the target at point
  8796. ((org-in-regexp "[^<]<<\\([^<>]+\\)>>[^>]" 1)
  8797. (setq cpltxt
  8798. (concat "file:"
  8799. (abbreviate-file-name
  8800. (buffer-file-name (buffer-base-buffer)))
  8801. "::" (match-string 1))
  8802. link cpltxt))
  8803. ((and (featurep 'org-id)
  8804. (or (eq org-id-link-to-org-use-id t)
  8805. (and (org-called-interactively-p 'any)
  8806. (or (eq org-id-link-to-org-use-id 'create-if-interactive)
  8807. (and (eq org-id-link-to-org-use-id
  8808. 'create-if-interactive-and-no-custom-id)
  8809. (not custom-id))))
  8810. (and org-id-link-to-org-use-id (org-entry-get nil "ID"))))
  8811. ;; Store a link using the ID at point
  8812. (setq link (condition-case nil
  8813. (prog1 (org-id-store-link)
  8814. (setq desc (or (plist-get org-store-link-plist
  8815. :description)
  8816. "")))
  8817. (error
  8818. ;; Probably before first headline, link only to file
  8819. (concat "file:"
  8820. (abbreviate-file-name
  8821. (buffer-file-name (buffer-base-buffer))))))))
  8822. (t
  8823. ;; Just link to current headline
  8824. (setq cpltxt (concat "file:"
  8825. (abbreviate-file-name
  8826. (buffer-file-name (buffer-base-buffer)))))
  8827. ;; Add a context search string
  8828. (when (org-xor org-context-in-file-links arg)
  8829. (let* ((ee (org-element-at-point))
  8830. (et (org-element-type ee))
  8831. (ev (plist-get (cadr ee) :value))
  8832. (ek (plist-get (cadr ee) :key))
  8833. (eok (and (stringp ek) (string-match "name" ek))))
  8834. (setq txt (cond
  8835. ((org-at-heading-p) nil)
  8836. ((and (eq et 'keyword) eok) ev)
  8837. ((org-region-active-p)
  8838. (buffer-substring (region-beginning) (region-end)))))
  8839. (when (or (null txt) (string-match "\\S-" txt))
  8840. (setq cpltxt
  8841. (concat cpltxt "::"
  8842. (condition-case nil
  8843. (org-make-org-heading-search-string txt)
  8844. (error "")))
  8845. desc (or (and (eq et 'keyword) eok ev)
  8846. (nth 4 (ignore-errors (org-heading-components)))
  8847. "NONE")))))
  8848. (when (string-match "::\\'" cpltxt)
  8849. (setq cpltxt (substring cpltxt 0 -2)))
  8850. (setq link cpltxt))))
  8851. ((buffer-file-name (buffer-base-buffer))
  8852. ;; Just link to this file here.
  8853. (setq cpltxt (concat "file:"
  8854. (abbreviate-file-name
  8855. (buffer-file-name (buffer-base-buffer)))))
  8856. ;; Add a context string.
  8857. (when (org-xor org-context-in-file-links arg)
  8858. (setq txt (if (org-region-active-p)
  8859. (buffer-substring (region-beginning) (region-end))
  8860. (buffer-substring (point-at-bol) (point-at-eol))))
  8861. ;; Only use search option if there is some text.
  8862. (when (string-match "\\S-" txt)
  8863. (setq cpltxt
  8864. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  8865. desc "NONE")))
  8866. (setq link cpltxt))
  8867. ((org-called-interactively-p 'interactive)
  8868. (user-error "No method for storing a link from this buffer"))
  8869. (t (setq link nil)))
  8870. ;; We're done setting link and desc, clean up
  8871. (when (consp link) (setq cpltxt (car link) link (cdr link)))
  8872. (setq link (or link cpltxt)
  8873. desc (or desc cpltxt))
  8874. (cond ((equal desc "NONE") (setq desc nil))
  8875. ((and desc (string-match org-bracket-link-analytic-regexp desc))
  8876. (let ((d0 (match-string 3 desc))
  8877. (p0 (match-string 5 desc)))
  8878. (setq desc
  8879. (replace-regexp-in-string
  8880. org-bracket-link-regexp
  8881. (concat (or p0 d0)
  8882. (if (equal (length (match-string 0 desc))
  8883. (length desc)) "*" "")) desc)))))
  8884. ;; Return the link
  8885. (if (not (and (or (org-called-interactively-p 'any)
  8886. executing-kbd-macro)
  8887. link))
  8888. (or agenda-link (and link (org-make-link-string link desc)))
  8889. (push (list link desc) org-stored-links)
  8890. (message "Stored: %s" (or desc link))
  8891. (when custom-id
  8892. (setq link (concat "file:" (abbreviate-file-name
  8893. (buffer-file-name)) "::#" custom-id))
  8894. (push (list link desc) org-stored-links))
  8895. (car org-stored-links))))))
  8896. (defun org-store-link-props (&rest plist)
  8897. "Store link properties, extract names, addresses and dates."
  8898. (let ((x (plist-get plist :from)))
  8899. (when x
  8900. (let ((adr (mail-extract-address-components x)))
  8901. (setq plist (plist-put plist :fromname (car adr)))
  8902. (setq plist (plist-put plist :fromaddress (nth 1 adr))))))
  8903. (let ((x (plist-get plist :to)))
  8904. (when x
  8905. (let ((adr (mail-extract-address-components x)))
  8906. (setq plist (plist-put plist :toname (car adr)))
  8907. (setq plist (plist-put plist :toaddress (nth 1 adr))))))
  8908. (let ((x (ignore-errors (date-to-time (plist-get plist :date)))))
  8909. (when x
  8910. (setq plist (plist-put plist :date-timestamp
  8911. (format-time-string
  8912. (org-time-stamp-format t) x)))
  8913. (setq plist (plist-put plist :date-timestamp-inactive
  8914. (format-time-string
  8915. (org-time-stamp-format t t) x)))))
  8916. (let ((from (plist-get plist :from))
  8917. (to (plist-get plist :to)))
  8918. (when (and from to org-from-is-user-regexp)
  8919. (setq plist
  8920. (plist-put plist :fromto
  8921. (if (string-match org-from-is-user-regexp from)
  8922. (concat "to %t")
  8923. (concat "from %f"))))))
  8924. (setq org-store-link-plist plist))
  8925. (defun org-add-link-props (&rest plist)
  8926. "Add these properties to the link property list."
  8927. (let (key value)
  8928. (while plist
  8929. (setq key (pop plist) value (pop plist))
  8930. (setq org-store-link-plist
  8931. (plist-put org-store-link-plist key value)))))
  8932. (defun org-email-link-description (&optional fmt)
  8933. "Return the description part of an email link.
  8934. This takes information from `org-store-link-plist' and formats it
  8935. according to FMT (default from `org-email-link-description-format')."
  8936. (setq fmt (or fmt org-email-link-description-format))
  8937. (let* ((p org-store-link-plist)
  8938. (to (plist-get p :toaddress))
  8939. (from (plist-get p :fromaddress))
  8940. (table
  8941. (list
  8942. (cons "%c" (plist-get p :fromto))
  8943. (cons "%F" (plist-get p :from))
  8944. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  8945. (cons "%T" (plist-get p :to))
  8946. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  8947. (cons "%s" (plist-get p :subject))
  8948. (cons "%d" (plist-get p :date))
  8949. (cons "%m" (plist-get p :message-id)))))
  8950. (when (string-match "%c" fmt)
  8951. ;; Check if the user wrote this message
  8952. (if (and org-from-is-user-regexp from to
  8953. (save-match-data (string-match org-from-is-user-regexp from)))
  8954. (setq fmt (replace-match "to %t" t t fmt))
  8955. (setq fmt (replace-match "from %f" t t fmt))))
  8956. (org-replace-escapes fmt table)))
  8957. (defun org-make-org-heading-search-string (&optional string)
  8958. "Make search string for the current headline or STRING."
  8959. (let ((s (or string
  8960. (and (derived-mode-p 'org-mode)
  8961. (save-excursion
  8962. (org-back-to-heading t)
  8963. (org-element-property :raw-value (org-element-at-point))))))
  8964. (lines org-context-in-file-links))
  8965. (or string (setq s (concat "*" s))) ; Add * for headlines
  8966. (setq s (replace-regexp-in-string "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" "" s))
  8967. (when (and string (integerp lines) (> lines 0))
  8968. (let ((slines (org-split-string s "\n")))
  8969. (when (< lines (length slines))
  8970. (setq s (mapconcat
  8971. 'identity
  8972. (reverse (nthcdr (- (length slines) lines)
  8973. (reverse slines))) "\n")))))
  8974. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  8975. (defun org-make-link-string (link &optional description)
  8976. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  8977. (unless (org-string-nw-p link) (error "Empty link"))
  8978. (let ((uri (cond ((string-match org-link-types-re link)
  8979. (concat (match-string 1 link)
  8980. (org-link-escape (substring link (match-end 1)))))
  8981. ;; For readability, url-encode internal links only
  8982. ;; when absolutely needed (i.e, when they contain
  8983. ;; square brackets). File links however, are
  8984. ;; encoded since, e.g., spaces are significant.
  8985. ((or (file-name-absolute-p link)
  8986. (org-string-match-p "\\`\\.\\.?/\\|[][]" link))
  8987. (org-link-escape link))
  8988. (t link)))
  8989. (description
  8990. (and (org-string-nw-p description)
  8991. ;; Remove brackets from description, as they are fatal.
  8992. (replace-regexp-in-string
  8993. "[][]" (lambda (m) (if (equal "[" m) "{" "}"))
  8994. (org-trim description)))))
  8995. (format "[[%s]%s]"
  8996. uri
  8997. (if description (format "[%s]" description) ""))))
  8998. (defconst org-link-escape-chars
  8999. ;;%20 %5B %5D %25
  9000. '(?\s ?\[ ?\] ?%)
  9001. "List of characters that should be escaped in a link when stored to Org.
  9002. This is the list that is used for internal purposes.")
  9003. (defconst org-link-escape-chars-browser
  9004. ;;%20 %22
  9005. '(?\s ?\")
  9006. "List of characters to be escaped before handing over to the browser.
  9007. If you consider using this constant then you probably want to use
  9008. the function `org-link-escape-browser' instead. See there why
  9009. this constant is a candidate to be removed once Org drops support
  9010. for Emacs 24.1 and 24.2.")
  9011. (defun org-link-escape (text &optional table merge)
  9012. "Return percent escaped representation of TEXT.
  9013. TEXT is a string with the text to escape.
  9014. Optional argument TABLE is a list with characters that should be
  9015. escaped. When nil, `org-link-escape-chars' is used.
  9016. If optional argument MERGE is set, merge TABLE into
  9017. `org-link-escape-chars'."
  9018. (let ((characters-to-encode
  9019. (cond ((null table) org-link-escape-chars)
  9020. (merge (append org-link-escape-chars table))
  9021. (t table))))
  9022. (mapconcat
  9023. (lambda (c)
  9024. (if (or (memq c characters-to-encode)
  9025. (and org-url-hexify-p (or (< c 32) (> c 126))))
  9026. (mapconcat (lambda (e) (format "%%%.2X" e))
  9027. (or (encode-coding-char c 'utf-8)
  9028. (error "Unable to percent escape character: %c" c))
  9029. "")
  9030. (char-to-string c)))
  9031. text "")))
  9032. (defun org-link-escape-browser (text)
  9033. "Escape some characters before handing over to the browser.
  9034. This function is a candidate to be removed together with the
  9035. constant `org-link-escape-chars-browser' once Org drops support
  9036. for Emacs 24.1 and 24.2. All calls to this function will have to
  9037. be replaced with `url-encode-url' which is available since Emacs
  9038. 24.3.1."
  9039. ;; Example with the Org link
  9040. ;; [[http://lists.gnu.org/archive/cgi-bin/namazu.cgi?idxname=emacs-orgmode&query=%252Bsubject:"Release+8.2"]]
  9041. ;; to open the browser with +subject:"Release 8.2" filled into the
  9042. ;; query field: In this case the variable TEXT contains the
  9043. ;; unescaped [...]=%2Bsubject:"Release+8.2". Then `url-encode-url'
  9044. ;; converts correctly to [...]=%2Bsubject:%22Release+8.2%22 or
  9045. ;; `org-link-escape' with `org-link-escape-chars-browser' converts
  9046. ;; wrongly to [...]=%252Bsubject:%22Release+8.2%22.
  9047. (if (fboundp 'url-encode-url)
  9048. (url-encode-url text)
  9049. (if (org-string-match-p
  9050. (concat "[[:nonascii:]" org-link-escape-chars-browser "]")
  9051. text)
  9052. (org-link-escape text org-link-escape-chars-browser)
  9053. text)))
  9054. (defun org-link-unescape (str)
  9055. "Unhex hexified Unicode parts in string STR.
  9056. E.g. `%C3%B6' becomes the german o-Umlaut. This is the
  9057. reciprocal of `org-link-escape', which see."
  9058. (if (org-string-nw-p str)
  9059. (replace-regexp-in-string
  9060. "\\(%[0-9A-Za-z]\\{2\\}\\)+" #'org-link-unescape-compound str t t)
  9061. str))
  9062. (defun org-link-unescape-compound (hex)
  9063. "Unhexify Unicode hex-chars. E.g. `%C3%B6' is the German o-Umlaut.
  9064. Note: this function also decodes single byte encodings like
  9065. `%E1' (a-acute) if not followed by another `%[A-F0-9]{2}' group."
  9066. (save-match-data
  9067. (let* ((bytes (cdr (split-string hex "%")))
  9068. (ret "")
  9069. (eat 0)
  9070. (sum 0))
  9071. (while bytes
  9072. (let* ((val (string-to-number (pop bytes) 16))
  9073. (shift-xor
  9074. (if (= 0 eat)
  9075. (cond
  9076. ((>= val 252) (cons 6 252))
  9077. ((>= val 248) (cons 5 248))
  9078. ((>= val 240) (cons 4 240))
  9079. ((>= val 224) (cons 3 224))
  9080. ((>= val 192) (cons 2 192))
  9081. (t (cons 0 0)))
  9082. (cons 6 128))))
  9083. (when (>= val 192) (setq eat (car shift-xor)))
  9084. (setq val (logxor val (cdr shift-xor)))
  9085. (setq sum (+ (lsh sum (car shift-xor)) val))
  9086. (when (> eat 0) (setq eat (- eat 1)))
  9087. (cond
  9088. ((= 0 eat) ;multi byte
  9089. (setq ret (concat ret (org-char-to-string sum)))
  9090. (setq sum 0))
  9091. ((not bytes) ; single byte(s)
  9092. (setq ret (org-link-unescape-single-byte-sequence hex))))))
  9093. ret)))
  9094. (defun org-link-unescape-single-byte-sequence (hex)
  9095. "Unhexify hex-encoded single byte character sequences."
  9096. (mapconcat (lambda (byte)
  9097. (char-to-string (string-to-number byte 16)))
  9098. (cdr (split-string hex "%")) ""))
  9099. (defun org-xor (a b)
  9100. "Exclusive or."
  9101. (if a (not b) b))
  9102. (defun org-fixup-message-id-for-http (s)
  9103. "Replace special characters in a message id, so it can be used in an http query."
  9104. (when (string-match "%" s)
  9105. (setq s (mapconcat (lambda (c)
  9106. (if (eq c ?%)
  9107. "%25"
  9108. (char-to-string c)))
  9109. s "")))
  9110. (while (string-match "<" s)
  9111. (setq s (replace-match "%3C" t t s)))
  9112. (while (string-match ">" s)
  9113. (setq s (replace-match "%3E" t t s)))
  9114. (while (string-match "@" s)
  9115. (setq s (replace-match "%40" t t s)))
  9116. s)
  9117. (defun org-link-prettify (link)
  9118. "Return a human-readable representation of LINK.
  9119. The car of LINK must be a raw link.
  9120. The cdr of LINK must be either a link description or nil."
  9121. (let ((desc (or (cadr link) "<no description>")))
  9122. (concat (format "%-45s" (substring desc 0 (min (length desc) 40)))
  9123. "<" (car link) ">")))
  9124. ;;;###autoload
  9125. (defun org-insert-link-global ()
  9126. "Insert a link like Org-mode does.
  9127. This command can be called in any mode to insert a link in Org-mode syntax."
  9128. (interactive)
  9129. (org-load-modules-maybe)
  9130. (org-run-like-in-org-mode 'org-insert-link))
  9131. (defun org-insert-all-links (arg &optional pre post)
  9132. "Insert all links in `org-stored-links'.
  9133. When a universal prefix, do not delete the links from `org-stored-links'.
  9134. When `ARG' is a number, insert the last N link(s).
  9135. `PRE' and `POST' are optional arguments to define a string to
  9136. prepend or to append."
  9137. (interactive "P")
  9138. (let ((org-keep-stored-link-after-insertion (equal arg '(4)))
  9139. (links (copy-seq org-stored-links))
  9140. (pr (or pre "- "))
  9141. (po (or post "\n"))
  9142. (cnt 1) l)
  9143. (if (null org-stored-links)
  9144. (message "No link to insert")
  9145. (while (and (or (listp arg) (>= arg cnt))
  9146. (setq l (if (listp arg)
  9147. (pop links)
  9148. (pop org-stored-links))))
  9149. (setq cnt (1+ cnt))
  9150. (insert pr)
  9151. (org-insert-link nil (car l) (or (cadr l) "<no description>"))
  9152. (insert po)))))
  9153. (defun org-insert-last-stored-link (arg)
  9154. "Insert the last link stored in `org-stored-links'."
  9155. (interactive "p")
  9156. (org-insert-all-links arg "" "\n"))
  9157. (defun org-link-fontify-links-to-this-file ()
  9158. "Fontify links to the current file in `org-stored-links'."
  9159. (let ((f (buffer-file-name)) a b)
  9160. (setq a (mapcar (lambda(l)
  9161. (let ((ll (car l)))
  9162. (when (and (string-match "^file:\\(.+\\)::" ll)
  9163. (equal f (expand-file-name (match-string 1 ll))))
  9164. ll)))
  9165. org-stored-links))
  9166. (when (featurep 'org-id)
  9167. (setq b (mapcar (lambda(l)
  9168. (let ((ll (car l)))
  9169. (when (and (string-match "^id:\\(.+\\)$" ll)
  9170. (equal f (expand-file-name
  9171. (or (org-id-find-id-file
  9172. (match-string 1 ll)) ""))))
  9173. ll)))
  9174. org-stored-links)))
  9175. (mapcar (lambda(l)
  9176. (put-text-property 0 (length l) 'face 'font-lock-comment-face l))
  9177. (delq nil (append a b)))))
  9178. (defvar org-link-links-in-this-file nil)
  9179. (defun org-insert-link (&optional complete-file link-location default-description)
  9180. "Insert a link. At the prompt, enter the link.
  9181. Completion can be used to insert any of the link protocol prefixes like
  9182. http or ftp in use.
  9183. The history can be used to select a link previously stored with
  9184. `org-store-link'. When the empty string is entered (i.e. if you just
  9185. press RET at the prompt), the link defaults to the most recently
  9186. stored link. As SPC triggers completion in the minibuffer, you need to
  9187. use M-SPC or C-q SPC to force the insertion of a space character.
  9188. You will also be prompted for a description, and if one is given, it will
  9189. be displayed in the buffer instead of the link.
  9190. If there is already a link at point, this command will allow you to edit link
  9191. and description parts.
  9192. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  9193. be selected using completion. The path to the file will be relative to the
  9194. current directory if the file is in the current directory or a subdirectory.
  9195. Otherwise, the link will be the absolute path as completed in the minibuffer
  9196. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  9197. option `org-link-file-path-type'.
  9198. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  9199. the current directory or below.
  9200. With three \\[universal-argument] prefixes, negate the meaning of
  9201. `org-keep-stored-link-after-insertion'.
  9202. If `org-make-link-description-function' is non-nil, this function will be
  9203. called with the link target, and the result will be the default
  9204. link description.
  9205. If the LINK-LOCATION parameter is non-nil, this value will be
  9206. used as the link location instead of reading one interactively.
  9207. If the DEFAULT-DESCRIPTION parameter is non-nil, this value will
  9208. be used as the default description."
  9209. (interactive "P")
  9210. (let* ((wcf (current-window-configuration))
  9211. (origbuf (current-buffer))
  9212. (region (when (org-region-active-p)
  9213. (buffer-substring (region-beginning) (region-end))))
  9214. (remove (and region (list (region-beginning) (region-end))))
  9215. (desc region)
  9216. tmphist ; byte-compile incorrectly complains about this
  9217. (link link-location)
  9218. (abbrevs org-link-abbrev-alist-local)
  9219. entry file all-prefixes auto-desc)
  9220. (cond
  9221. (link-location) ; specified by arg, just use it.
  9222. ((org-in-regexp org-bracket-link-regexp 1)
  9223. ;; We do have a link at point, and we are going to edit it.
  9224. (setq remove (list (match-beginning 0) (match-end 0)))
  9225. (setq desc (when (match-end 3) (org-match-string-no-properties 3)))
  9226. (setq link (read-string "Link: "
  9227. (org-link-unescape
  9228. (org-match-string-no-properties 1)))))
  9229. ((or (org-in-regexp org-angle-link-re)
  9230. (org-in-regexp org-plain-link-re))
  9231. ;; Convert to bracket link
  9232. (setq remove (list (match-beginning 0) (match-end 0))
  9233. link (read-string "Link: "
  9234. (org-remove-angle-brackets (match-string 0)))))
  9235. ((member complete-file '((4) (16)))
  9236. ;; Completing read for file names.
  9237. (setq link (org-file-complete-link complete-file)))
  9238. (t
  9239. ;; Read link, with completion for stored links.
  9240. (org-link-fontify-links-to-this-file)
  9241. (org-switch-to-buffer-other-window "*Org Links*")
  9242. (with-current-buffer "*Org Links*"
  9243. (erase-buffer)
  9244. (insert "Insert a link.
  9245. Use TAB to complete link prefixes, then RET for type-specific completion support\n")
  9246. (when org-stored-links
  9247. (insert "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  9248. (insert (mapconcat 'org-link-prettify
  9249. (reverse org-stored-links) "\n")))
  9250. (goto-char (point-min)))
  9251. (let ((cw (selected-window)))
  9252. (select-window (get-buffer-window "*Org Links*" 'visible))
  9253. (with-current-buffer "*Org Links*" (setq truncate-lines t))
  9254. (unless (pos-visible-in-window-p (point-max))
  9255. (org-fit-window-to-buffer))
  9256. (and (window-live-p cw) (select-window cw)))
  9257. ;; Fake a link history, containing the stored links.
  9258. (setq tmphist (append (mapcar 'car org-stored-links)
  9259. org-insert-link-history))
  9260. (setq all-prefixes (append (mapcar 'car abbrevs)
  9261. (mapcar 'car org-link-abbrev-alist)
  9262. org-link-types))
  9263. (unwind-protect
  9264. (progn
  9265. (setq link
  9266. (org-completing-read
  9267. "Link: "
  9268. (append
  9269. (mapcar (lambda (x) (concat x ":"))
  9270. all-prefixes)
  9271. (mapcar 'car org-stored-links))
  9272. nil nil nil
  9273. 'tmphist
  9274. (caar org-stored-links)))
  9275. (unless (string-match "\\S-" link)
  9276. (user-error "No link selected"))
  9277. (mapc (lambda(l)
  9278. (when (equal link (cadr l)) (setq link (car l) auto-desc t)))
  9279. org-stored-links)
  9280. (when (or (member link all-prefixes)
  9281. (and (equal ":" (substring link -1))
  9282. (member (substring link 0 -1) all-prefixes)
  9283. (setq link (substring link 0 -1))))
  9284. (setq link (with-current-buffer origbuf
  9285. (org-link-try-special-completion link)))))
  9286. (set-window-configuration wcf)
  9287. (kill-buffer "*Org Links*"))
  9288. (setq entry (assoc link org-stored-links))
  9289. (or entry (push link org-insert-link-history))
  9290. (setq desc (or desc (nth 1 entry)))))
  9291. (when (funcall (if (equal complete-file '(64)) 'not 'identity)
  9292. (not org-keep-stored-link-after-insertion))
  9293. (setq org-stored-links (delq (assoc link org-stored-links)
  9294. org-stored-links)))
  9295. (when (and (string-match org-plain-link-re link)
  9296. (not (string-match org-ts-regexp link)))
  9297. ;; URL-like link, normalize the use of angular brackets.
  9298. (setq link (org-remove-angle-brackets link)))
  9299. ;; Check if we are linking to the current file with a search
  9300. ;; option If yes, simplify the link by using only the search
  9301. ;; option.
  9302. (when (and buffer-file-name
  9303. (string-match "^file:\\(.+?\\)::\\(.+\\)" link))
  9304. (let* ((path (match-string 1 link))
  9305. (case-fold-search nil)
  9306. (search (match-string 2 link)))
  9307. (save-match-data
  9308. (when (equal (file-truename buffer-file-name) (file-truename path))
  9309. ;; We are linking to this same file, with a search option
  9310. (setq link search)))))
  9311. ;; Check if we can/should use a relative path. If yes, simplify the link
  9312. (when (string-match "^\\(file:\\|docview:\\)\\(.*\\)" link)
  9313. (let* ((type (match-string 1 link))
  9314. (path (match-string 2 link))
  9315. (origpath path)
  9316. (case-fold-search nil))
  9317. (cond
  9318. ((or (eq org-link-file-path-type 'absolute)
  9319. (equal complete-file '(16)))
  9320. (setq path (abbreviate-file-name (expand-file-name path))))
  9321. ((eq org-link-file-path-type 'noabbrev)
  9322. (setq path (expand-file-name path)))
  9323. ((eq org-link-file-path-type 'relative)
  9324. (setq path (file-relative-name path)))
  9325. (t
  9326. (save-match-data
  9327. (if (string-match (concat "^" (regexp-quote
  9328. (expand-file-name
  9329. (file-name-as-directory
  9330. default-directory))))
  9331. (expand-file-name path))
  9332. ;; We are linking a file with relative path name.
  9333. (setq path (substring (expand-file-name path)
  9334. (match-end 0)))
  9335. (setq path (abbreviate-file-name (expand-file-name path)))))))
  9336. (setq link (concat type path))
  9337. (when (equal desc origpath)
  9338. (setq desc path))))
  9339. (if org-make-link-description-function
  9340. (setq desc
  9341. (or (condition-case nil
  9342. (funcall org-make-link-description-function link desc)
  9343. (error (progn (message "Can't get link description from `%s'"
  9344. (symbol-name org-make-link-description-function))
  9345. (sit-for 2) nil)))
  9346. (read-string "Description: " default-description)))
  9347. (if default-description (setq desc default-description)
  9348. (setq desc (or (and auto-desc desc)
  9349. (read-string "Description: " desc)))))
  9350. (unless (string-match "\\S-" desc) (setq desc nil))
  9351. (when remove (apply 'delete-region remove))
  9352. (insert (org-make-link-string link desc))
  9353. ;; Redisplay so as the new link has proper invisible characters.
  9354. (sit-for 0)))
  9355. (defun org-link-try-special-completion (type)
  9356. "If there is completion support for link type TYPE, offer it."
  9357. (let ((fun (intern (concat "org-" type "-complete-link"))))
  9358. (if (functionp fun)
  9359. (funcall fun)
  9360. (read-string "Link (no completion support): " (concat type ":")))))
  9361. (defun org-file-complete-link (&optional arg)
  9362. "Create a file link using completion."
  9363. (let ((file (org-iread-file-name "File: "))
  9364. (pwd (file-name-as-directory (expand-file-name ".")))
  9365. (pwd1 (file-name-as-directory (abbreviate-file-name
  9366. (expand-file-name ".")))))
  9367. (cond ((equal arg '(16))
  9368. (concat "file:"
  9369. (abbreviate-file-name (expand-file-name file))))
  9370. ((string-match
  9371. (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  9372. (concat "file:" (match-string 1 file)))
  9373. ((string-match
  9374. (concat "^" (regexp-quote pwd) "\\(.+\\)")
  9375. (expand-file-name file))
  9376. (concat "file:"
  9377. (match-string 1 (expand-file-name file))))
  9378. (t (concat "file:" file)))))
  9379. (defun org-iread-file-name (&rest args)
  9380. "Read-file-name using `ido-mode' speedup if available.
  9381. ARGS are arguments that may be passed to `ido-read-file-name' or `read-file-name'.
  9382. See `read-file-name' for a description of parameters."
  9383. (org-without-partial-completion
  9384. (if (and org-completion-use-ido
  9385. (fboundp 'ido-read-file-name)
  9386. (org-bound-and-true-p ido-mode)
  9387. (listp (nth 1 args)))
  9388. (let ((ido-enter-matching-directory nil))
  9389. (apply #'ido-read-file-name args))
  9390. (apply #'read-file-name args))))
  9391. (defun org-completing-read (&rest args)
  9392. "Completing-read with SPACE being a normal character."
  9393. (let ((enable-recursive-minibuffers t)
  9394. (minibuffer-local-completion-map
  9395. (copy-keymap minibuffer-local-completion-map)))
  9396. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  9397. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  9398. (org-defkey minibuffer-local-completion-map (kbd "C-c !") 'org-time-stamp-inactive)
  9399. (apply 'org-icompleting-read args)))
  9400. (defun org-completing-read-no-i (&rest args)
  9401. (let (org-completion-use-ido org-completion-use-iswitchb)
  9402. (apply 'org-completing-read args)))
  9403. (defun org-iswitchb-completing-read (prompt choices &rest args)
  9404. "Use iswitch as a completing-read replacement to choose from choices.
  9405. PROMPT is a string to prompt with. CHOICES is a list of strings to choose
  9406. from."
  9407. (let* ((iswitchb-use-virtual-buffers nil)
  9408. (iswitchb-make-buflist-hook
  9409. (lambda ()
  9410. (setq iswitchb-temp-buflist choices))))
  9411. (iswitchb-read-buffer prompt)))
  9412. (defun org-icompleting-read (&rest args)
  9413. "Completing-read using `ido-mode' or `iswitchb' speedups if available.
  9414. Should be called like `completing-read'."
  9415. (org-without-partial-completion
  9416. (if (not (listp (nth 1 args)))
  9417. ;; Ido only supports lists as the COLLECTION argument. Use
  9418. ;; default completion function when second argument is not
  9419. ;; a list.
  9420. (apply #'completing-read args)
  9421. (let ((ido-enter-matching-directory nil))
  9422. (apply (cond ((and org-completion-use-ido
  9423. (fboundp 'ido-completing-read)
  9424. (org-bound-and-true-p ido-mode))
  9425. #'ido-completing-read)
  9426. ((and org-completion-use-iswitchb
  9427. (org-bound-and-true-p iswitchb-mode))
  9428. #'org-iswitchb-completing-read)
  9429. (t #'completing-read))
  9430. args)))))
  9431. ;;; Opening/following a link
  9432. (defvar org-link-search-failed nil)
  9433. (defvar org-open-link-functions nil
  9434. "Hook for functions finding a plain text link.
  9435. These functions must take a single argument, the link content.
  9436. They will be called for links that look like [[link text][description]]
  9437. when LINK TEXT does not have a protocol like \"http:\" and does not look
  9438. like a filename (e.g. \"./blue.png\").
  9439. These functions will be called *before* Org attempts to resolve the
  9440. link by doing text searches in the current buffer - so if you want a
  9441. link \"[[target]]\" to still find \"<<target>>\", your function should
  9442. handle this as a special case.
  9443. When the function does handle the link, it must return a non-nil value.
  9444. If it decides that it is not responsible for this link, it must return
  9445. nil to indicate that that Org-mode can continue with other options
  9446. like exact and fuzzy text search.")
  9447. (defun org-next-link (&optional search-backward)
  9448. "Move forward to the next link.
  9449. If the link is in hidden text, expose it."
  9450. (interactive "P")
  9451. (when (and org-link-search-failed (eq this-command last-command))
  9452. (goto-char (point-min))
  9453. (message "Link search wrapped back to beginning of buffer"))
  9454. (setq org-link-search-failed nil)
  9455. (let* ((pos (point))
  9456. (ct (org-context))
  9457. (a (assoc :link ct))
  9458. (srch-fun (if search-backward 're-search-backward 're-search-forward)))
  9459. (cond (a (goto-char (nth (if search-backward 1 2) a)))
  9460. ((looking-at org-any-link-re)
  9461. ;; Don't stay stuck at link without an org-link face
  9462. (forward-char (if search-backward -1 1))))
  9463. (if (funcall srch-fun org-any-link-re nil t)
  9464. (progn
  9465. (goto-char (match-beginning 0))
  9466. (when (outline-invisible-p) (org-show-context)))
  9467. (goto-char pos)
  9468. (setq org-link-search-failed t)
  9469. (message "No further link found"))))
  9470. (defun org-previous-link ()
  9471. "Move backward to the previous link.
  9472. If the link is in hidden text, expose it."
  9473. (interactive)
  9474. (funcall 'org-next-link t))
  9475. (defun org-translate-link (s)
  9476. "Translate a link string if a translation function has been defined."
  9477. (with-temp-buffer
  9478. (insert (org-trim s))
  9479. (org-trim (org-element-interpret-data (org-element-context)))))
  9480. (defun org-translate-link-from-planner (type path)
  9481. "Translate a link from Emacs Planner syntax so that Org can follow it.
  9482. This is still an experimental function, your mileage may vary."
  9483. (cond
  9484. ((member type '("http" "https" "news" "ftp"))
  9485. ;; standard Internet links are the same.
  9486. nil)
  9487. ((and (equal type "irc") (string-match "^//" path))
  9488. ;; Planner has two / at the beginning of an irc link, we have 1.
  9489. ;; We should have zero, actually....
  9490. (setq path (substring path 1)))
  9491. ((and (equal type "lisp") (string-match "^/" path))
  9492. ;; Planner has a slash, we do not.
  9493. (setq type "elisp" path (substring path 1)))
  9494. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  9495. ;; A typical message link. Planner has the id after the final slash,
  9496. ;; we separate it with a hash mark
  9497. (setq path (concat (match-string 1 path) "#"
  9498. (org-remove-angle-brackets (match-string 2 path))))))
  9499. (cons type path))
  9500. (defun org-find-file-at-mouse (ev)
  9501. "Open file link or URL at mouse."
  9502. (interactive "e")
  9503. (mouse-set-point ev)
  9504. (org-open-at-point 'in-emacs))
  9505. (defun org-open-at-mouse (ev)
  9506. "Open file link or URL at mouse.
  9507. See the docstring of `org-open-file' for details."
  9508. (interactive "e")
  9509. (mouse-set-point ev)
  9510. (when (eq major-mode 'org-agenda-mode)
  9511. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  9512. (org-open-at-point))
  9513. (defvar org-window-config-before-follow-link nil
  9514. "The window configuration before following a link.
  9515. This is saved in case the need arises to restore it.")
  9516. ;;;###autoload
  9517. (defun org-open-at-point-global ()
  9518. "Follow a link like Org-mode does.
  9519. This command can be called in any mode to follow a link that has
  9520. Org-mode syntax."
  9521. (interactive)
  9522. (org-run-like-in-org-mode 'org-open-at-point))
  9523. ;;;###autoload
  9524. (defun org-open-link-from-string (s &optional arg reference-buffer)
  9525. "Open a link in the string S, as if it was in Org-mode."
  9526. (interactive "sLink: \nP")
  9527. (let ((reference-buffer (or reference-buffer (current-buffer))))
  9528. (with-temp-buffer
  9529. (let ((org-inhibit-startup (not reference-buffer)))
  9530. (org-mode)
  9531. (insert s)
  9532. (goto-char (point-min))
  9533. (when reference-buffer
  9534. (setq org-link-abbrev-alist-local
  9535. (with-current-buffer reference-buffer
  9536. org-link-abbrev-alist-local)))
  9537. (org-open-at-point arg reference-buffer)))))
  9538. (defvar org-open-at-point-functions nil
  9539. "Hook that is run when following a link at point.
  9540. Functions in this hook must return t if they identify and follow
  9541. a link at point. If they don't find anything interesting at point,
  9542. they must return nil.")
  9543. (defvar org-link-search-inhibit-query nil) ;; dynamically scoped
  9544. (defvar clean-buffer-list-kill-buffer-names) ; Defined in midnight.el
  9545. (defun org-open-at-point (&optional arg reference-buffer)
  9546. "Open link, timestamp, footnote or tags at point.
  9547. When point is on a link, follow it. Normally, files will be
  9548. opened by an appropriate application. If the optional prefix
  9549. argument ARG is non-nil, Emacs will visit the file. With
  9550. a double prefix argument, try to open outside of Emacs, in the
  9551. application the system uses for this file type.
  9552. When point is on a timestamp, open the agenda at the day
  9553. specified.
  9554. When point is a footnote definition, move to the first reference
  9555. found. If it is on a reference, move to the associated
  9556. definition.
  9557. When point is on a headline, display a list of every link in the
  9558. entry, so it is possible to pick one, or all, of them. If point
  9559. is on a tag, call `org-tags-view' instead.
  9560. When optional argument REFERENCE-BUFFER is non-nil, it should
  9561. specify a buffer from where the link search should happen. This
  9562. is used internally by `org-open-link-from-string'.
  9563. On top of syntactically correct links, this function will open
  9564. the link at point in comments or comment blocks and the first
  9565. link in a property drawer line."
  9566. (interactive "P")
  9567. ;; On a code block, open block's results.
  9568. (unless (call-interactively 'org-babel-open-src-block-result)
  9569. (org-load-modules-maybe)
  9570. (setq org-window-config-before-follow-link (current-window-configuration))
  9571. (org-remove-occur-highlights nil nil t)
  9572. (unless (run-hook-with-args-until-success 'org-open-at-point-functions)
  9573. (let* ((context
  9574. ;; Only consider supported types, even if they are not
  9575. ;; the closest one.
  9576. (org-element-lineage
  9577. (org-element-context)
  9578. '(clock comment comment-block footnote-definition
  9579. footnote-reference headline inlinetask keyword link
  9580. node-property timestamp)
  9581. t))
  9582. (type (org-element-type context))
  9583. (value (org-element-property :value context)))
  9584. (cond
  9585. ((not context) (user-error "No link found"))
  9586. ;; Exception: open timestamps and links in properties
  9587. ;; drawers, keywords and comments.
  9588. ((memq type '(comment comment-block keyword node-property))
  9589. (cond ((org-in-regexp org-any-link-re)
  9590. (org-open-link-from-string (match-string-no-properties 0)))
  9591. ((or (org-in-regexp org-ts-regexp-both nil t)
  9592. (org-in-regexp org-tsr-regexp-both nil t))
  9593. (org-follow-timestamp-link))
  9594. (t (user-error "No link found"))))
  9595. ;; On a headline or an inlinetask, but not on a timestamp,
  9596. ;; a link, a footnote reference or on tags.
  9597. ((and (memq type '(headline inlinetask))
  9598. ;; Not on tags.
  9599. (progn (save-excursion (beginning-of-line)
  9600. (looking-at org-complex-heading-regexp))
  9601. (or (not (match-beginning 5))
  9602. (< (point) (match-beginning 5)))))
  9603. (let* ((data (org-offer-links-in-entry (current-buffer) (point) arg))
  9604. (links (car data))
  9605. (links-end (cdr data)))
  9606. (if links
  9607. (dolist (link (if (stringp links) (list links) links))
  9608. (search-forward link nil links-end)
  9609. (goto-char (match-beginning 0))
  9610. (org-open-at-point))
  9611. (require 'org-attach)
  9612. (org-attach-reveal 'if-exists))))
  9613. ;; On a clock line, make sure point is on the timestamp
  9614. ;; before opening it.
  9615. ((and (eq type 'clock)
  9616. value
  9617. (>= (point) (org-element-property :begin value))
  9618. (<= (point) (org-element-property :end value)))
  9619. (org-follow-timestamp-link))
  9620. ;; Do nothing on white spaces after an object.
  9621. ((>= (point)
  9622. (save-excursion
  9623. (goto-char (org-element-property :end context))
  9624. (skip-chars-backward " \t")
  9625. (point)))
  9626. (user-error "No link found"))
  9627. ((eq type 'timestamp) (org-follow-timestamp-link))
  9628. ;; On tags within a headline or an inlinetask.
  9629. ((and (memq type '(headline inlinetask))
  9630. (progn (save-excursion (beginning-of-line)
  9631. (looking-at org-complex-heading-regexp))
  9632. (and (match-beginning 5)
  9633. (>= (point) (match-beginning 5)))))
  9634. (org-tags-view arg (substring (match-string 5) 0 -1)))
  9635. ((eq type 'link)
  9636. ;; When link is located within the description of another
  9637. ;; link (e.g., an inline image), always open the parent
  9638. ;; link.
  9639. (let*((link (let ((up (org-element-property :parent context)))
  9640. (if (eq (org-element-type up) 'link) up context)))
  9641. (type (org-element-property :type link))
  9642. (path (org-link-unescape (org-element-property :path link))))
  9643. ;; Switch back to REFERENCE-BUFFER needed when called in
  9644. ;; a temporary buffer through `org-open-link-from-string'.
  9645. (with-current-buffer (or reference-buffer (current-buffer))
  9646. (cond
  9647. ((equal type "file")
  9648. (if (string-match "[*?{]" (file-name-nondirectory path))
  9649. (dired path)
  9650. ;; Look into `org-link-protocols' in order to find
  9651. ;; a DEDICATED-FUNCTION to open file. The function
  9652. ;; will be applied on raw link instead of parsed
  9653. ;; link due to the limitation in `org-add-link-type'
  9654. ;; ("open" function called with a single argument).
  9655. ;; If no such function is found, fallback to
  9656. ;; `org-open-file'.
  9657. ;;
  9658. ;; Note : "file+emacs" and "file+sys" types are
  9659. ;; hard-coded in order to escape the previous
  9660. ;; limitation.
  9661. (let* ((option (org-element-property :search-option link))
  9662. (app (org-element-property :application link))
  9663. (dedicated-function
  9664. (nth 1 (assoc app org-link-protocols))))
  9665. (if dedicated-function
  9666. (funcall dedicated-function
  9667. (concat path
  9668. (and option (concat "::" option))))
  9669. (apply #'org-open-file
  9670. path
  9671. (cond (arg)
  9672. ((equal app "emacs") 'emacs)
  9673. ((equal app "sys") 'system))
  9674. (cond ((not option) nil)
  9675. ((org-string-match-p "\\`[0-9]+\\'" option)
  9676. (list (string-to-number option)))
  9677. (t (list nil
  9678. (org-link-unescape option)))))))))
  9679. ((assoc type org-link-protocols)
  9680. (funcall (nth 1 (assoc type org-link-protocols)) path))
  9681. ((equal type "help")
  9682. (let ((f-or-v (intern path)))
  9683. (cond ((fboundp f-or-v) (describe-function f-or-v))
  9684. ((boundp f-or-v) (describe-variable f-or-v))
  9685. (t (error "Not a known function or variable")))))
  9686. ((member type '("http" "https" "ftp" "mailto" "news"))
  9687. (browse-url (org-link-escape-browser (concat type ":" path))))
  9688. ((equal type "doi")
  9689. (browse-url
  9690. (org-link-escape-browser (concat org-doi-server-url path))))
  9691. ((equal type "message") (browse-url (concat type ":" path)))
  9692. ((equal type "shell")
  9693. (let ((buf (generate-new-buffer "*Org Shell Output*"))
  9694. (cmd path))
  9695. (if (or (and (org-string-nw-p
  9696. org-confirm-shell-link-not-regexp)
  9697. (string-match
  9698. org-confirm-shell-link-not-regexp cmd))
  9699. (not org-confirm-shell-link-function)
  9700. (funcall org-confirm-shell-link-function
  9701. (format "Execute \"%s\" in shell? "
  9702. (org-add-props cmd nil
  9703. 'face 'org-warning))))
  9704. (progn
  9705. (message "Executing %s" cmd)
  9706. (shell-command cmd buf)
  9707. (when (featurep 'midnight)
  9708. (setq clean-buffer-list-kill-buffer-names
  9709. (cons (buffer-name buf)
  9710. clean-buffer-list-kill-buffer-names))))
  9711. (user-error "Abort"))))
  9712. ((equal type "elisp")
  9713. (let ((cmd path))
  9714. (if (or (and (org-string-nw-p
  9715. org-confirm-elisp-link-not-regexp)
  9716. (org-string-match-p
  9717. org-confirm-elisp-link-not-regexp cmd))
  9718. (not org-confirm-elisp-link-function)
  9719. (funcall org-confirm-elisp-link-function
  9720. (format "Execute \"%s\" as elisp? "
  9721. (org-add-props cmd nil
  9722. 'face 'org-warning))))
  9723. (message "%s => %s" cmd
  9724. (if (eq (string-to-char cmd) ?\()
  9725. (eval (read cmd))
  9726. (call-interactively (read cmd))))
  9727. (user-error "Abort"))))
  9728. ((equal type "id")
  9729. (require 'org-id)
  9730. (funcall (nth 1 (assoc "id" org-link-protocols)) path))
  9731. ((member type '("coderef" "custom-id" "fuzzy" "radio"))
  9732. (unless (run-hook-with-args-until-success
  9733. 'org-open-link-functions path)
  9734. (if (not arg) (org-mark-ring-push)
  9735. (switch-to-buffer-other-window
  9736. (org-get-buffer-for-internal-link (current-buffer))))
  9737. (let ((destination
  9738. (org-with-wide-buffer
  9739. (if (equal type "radio")
  9740. (org-search-radio-target
  9741. (org-element-property :path link))
  9742. (org-link-search
  9743. (if (member type '("custom-id" "coderef"))
  9744. (org-element-property :raw-link link)
  9745. path)
  9746. ;; Prevent fuzzy links from matching
  9747. ;; themselves.
  9748. (and (equal type "fuzzy")
  9749. (+ 2 (org-element-property :begin link)))))
  9750. (point))))
  9751. (unless (and (<= (point-min) destination)
  9752. (>= (point-max) destination))
  9753. (widen))
  9754. (goto-char destination))))
  9755. (t (browse-url-at-point))))))
  9756. ;; On a footnote reference or at a footnote definition's label.
  9757. ((or (eq type 'footnote-reference)
  9758. (and (eq type 'footnote-definition)
  9759. (save-excursion
  9760. ;; Do not validate action when point is on the
  9761. ;; spaces right after the footnote label, in
  9762. ;; order to be on par with behaviour on links.
  9763. (skip-chars-forward " \t")
  9764. (let ((begin
  9765. (org-element-property :contents-begin context)))
  9766. (if begin (< (point) begin)
  9767. (= (org-element-property :post-affiliated context)
  9768. (line-beginning-position)))))))
  9769. (org-footnote-action))
  9770. (t (user-error "No link found")))))
  9771. (run-hook-with-args 'org-follow-link-hook)))
  9772. (defun org-offer-links-in-entry (buffer marker &optional nth zero)
  9773. "Offer links in the current entry and return the selected link.
  9774. If there is only one link, return it.
  9775. If NTH is an integer, return the NTH link found.
  9776. If ZERO is a string, check also this string for a link, and if
  9777. there is one, return it."
  9778. (with-current-buffer buffer
  9779. (save-excursion
  9780. (save-restriction
  9781. (widen)
  9782. (goto-char marker)
  9783. (let ((cnt ?0)
  9784. (in-emacs (unless (integerp nth) nth))
  9785. have-zero end links link c)
  9786. (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
  9787. (push (match-string 0 zero) links)
  9788. (setq cnt (1- cnt) have-zero t))
  9789. (save-excursion
  9790. (org-back-to-heading t)
  9791. (setq end (save-excursion (outline-next-heading) (point)))
  9792. (while (re-search-forward org-any-link-re end t)
  9793. (push (match-string 0) links))
  9794. (setq links (org-uniquify (reverse links))))
  9795. (cond
  9796. ((null links)
  9797. (message "No links"))
  9798. ((equal (length links) 1)
  9799. (setq link (car links)))
  9800. ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
  9801. (setq link (nth (if have-zero nth (1- nth)) links)))
  9802. (t ; we have to select a link
  9803. (save-excursion
  9804. (save-window-excursion
  9805. (delete-other-windows)
  9806. (with-output-to-temp-buffer "*Select Link*"
  9807. (mapc (lambda (l)
  9808. (if (not (string-match org-bracket-link-regexp l))
  9809. (princ (format "[%c] %s\n" (incf cnt)
  9810. (org-remove-angle-brackets l)))
  9811. (if (match-end 3)
  9812. (princ (format "[%c] %s (%s)\n" (incf cnt)
  9813. (match-string 3 l) (match-string 1 l)))
  9814. (princ (format "[%c] %s\n" (incf cnt)
  9815. (match-string 1 l))))))
  9816. links))
  9817. (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
  9818. (message "Select link to open, RET to open all:")
  9819. (setq c (read-char-exclusive))
  9820. (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
  9821. (when (equal c ?q) (user-error "Abort"))
  9822. (if (equal c ?\C-m)
  9823. (setq link links)
  9824. (setq nth (- c ?0))
  9825. (when have-zero (setq nth (1+ nth)))
  9826. (unless (and (integerp nth) (>= (length links) nth))
  9827. (user-error "Invalid link selection"))
  9828. (setq link (nth (1- nth) links)))))
  9829. (cons link end))))))
  9830. ;; TODO: These functions are deprecated since `org-open-at-point'
  9831. ;; hard-codes behaviour for "file+emacs" and "file+sys" types.
  9832. (defun org-open-file-with-system (path)
  9833. "Open file at PATH using the system way of opening it."
  9834. (org-open-file path 'system))
  9835. (defun org-open-file-with-emacs (path)
  9836. "Open file at PATH in Emacs."
  9837. (org-open-file path 'emacs))
  9838. ;;; File search
  9839. (defvar org-create-file-search-functions nil
  9840. "List of functions to construct the right search string for a file link.
  9841. These functions are called in turn with point at the location to
  9842. which the link should point.
  9843. A function in the hook should first test if it would like to
  9844. handle this file type, for example by checking the `major-mode'
  9845. or the file extension. If it decides not to handle this file, it
  9846. should just return nil to give other functions a chance. If it
  9847. does handle the file, it must return the search string to be used
  9848. when following the link. The search string will be part of the
  9849. file link, given after a double colon, and `org-open-at-point'
  9850. will automatically search for it. If special measures must be
  9851. taken to make the search successful, another function should be
  9852. added to the companion hook `org-execute-file-search-functions',
  9853. which see.
  9854. A function in this hook may also use `setq' to set the variable
  9855. `description' to provide a suggestion for the descriptive text to
  9856. be used for this link when it gets inserted into an Org-mode
  9857. buffer with \\[org-insert-link].")
  9858. (defvar org-execute-file-search-functions nil
  9859. "List of functions to execute a file search triggered by a link.
  9860. Functions added to this hook must accept a single argument, the
  9861. search string that was part of the file link, the part after the
  9862. double colon. The function must first check if it would like to
  9863. handle this search, for example by checking the `major-mode' or
  9864. the file extension. If it decides not to handle this search, it
  9865. should just return nil to give other functions a chance. If it
  9866. does handle the search, it must return a non-nil value to keep
  9867. other functions from trying.
  9868. Each function can access the current prefix argument through the
  9869. variable `current-prefix-arg'. Note that a single prefix is used
  9870. to force opening a link in Emacs, so it may be good to only use a
  9871. numeric or double prefix to guide the search function.
  9872. In case this is needed, a function in this hook can also restore
  9873. the window configuration before `org-open-at-point' was called using:
  9874. (set-window-configuration org-window-config-before-follow-link)")
  9875. (defun org-search-radio-target (target)
  9876. "Search a radio target matching TARGET in current buffer.
  9877. White spaces are not significant."
  9878. (let ((re (format "<<<%s>>>"
  9879. (mapconcat #'regexp-quote
  9880. (org-split-string target "[ \t\n]+")
  9881. "[ \t]+\\(?:\n[ \t]*\\)?")))
  9882. (origin (point)))
  9883. (goto-char (point-min))
  9884. (catch :radio-match
  9885. (while (re-search-forward re nil t)
  9886. (backward-char)
  9887. (let ((object (org-element-context)))
  9888. (when (eq (org-element-type object) 'radio-target)
  9889. (goto-char (org-element-property :begin object))
  9890. (org-show-context 'link-search)
  9891. (throw :radio-match nil))))
  9892. (goto-char origin)
  9893. (user-error "No match for radio target: %s" target))))
  9894. (defun org-link-search (s &optional avoid-pos stealth)
  9895. "Search for a search string S.
  9896. If S starts with \"#\", it triggers a custom ID search.
  9897. If S is enclosed within parenthesis, it initiates a coderef
  9898. search.
  9899. If S is surrounded by forward slashes, it is interpreted as
  9900. a regular expression. In Org mode files, this will create an
  9901. `org-occur' sparse tree. In ordinary files, `occur' will be used
  9902. to list matches. If the current buffer is in `dired-mode', grep
  9903. will be used to search in all files.
  9904. When AVOID-POS is given, ignore matches near that position.
  9905. When optional argument STEALTH is non-nil, do not modify
  9906. visibility around point, thus ignoring `org-show-context-detail'
  9907. variable.
  9908. Search is case-insensitive and ignores white spaces. Return type
  9909. of matched result, with is either `dedicated' or `fuzzy'."
  9910. (unless (org-string-nw-p s) (error "Invalid search string \"%s\"" s))
  9911. (let* ((case-fold-search t)
  9912. (origin (point))
  9913. (normalized (replace-regexp-in-string "\n[ \t]*" " " s))
  9914. (words (org-split-string s "[ \t\n]+"))
  9915. (s-multi-re (mapconcat #'regexp-quote words "[ \t]+\\(?:\n[ \t]*\\)?"))
  9916. (s-single-re (mapconcat #'regexp-quote words "[ \t]+"))
  9917. type)
  9918. (cond
  9919. ;; Check if there are any special search functions.
  9920. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  9921. ((eq (string-to-char s) ?#)
  9922. ;; Look for a custom ID S if S starts with "#".
  9923. (let* ((id (substring normalized 1))
  9924. (match (org-find-property "CUSTOM_ID" id)))
  9925. (if match (progn (goto-char match) (setf type 'dedicated))
  9926. (error "No match for custom ID: %s" id))))
  9927. ((string-match "\\`(\\(.*\\))\\'" normalized)
  9928. ;; Look for coderef targets if S is enclosed within parenthesis.
  9929. (let ((coderef (match-string-no-properties 1 normalized))
  9930. (re (substring s-single-re 1 -1)))
  9931. (goto-char (point-min))
  9932. (catch :coderef-match
  9933. (while (re-search-forward re nil t)
  9934. (let ((element (org-element-at-point)))
  9935. (when (and (memq (org-element-type element)
  9936. '(example-block src-block))
  9937. ;; Build proper regexp according to current
  9938. ;; block's label format.
  9939. (let ((label-fmt
  9940. (regexp-quote
  9941. (or (org-element-property :label-fmt element)
  9942. org-coderef-label-format))))
  9943. (save-excursion
  9944. (beginning-of-line)
  9945. (looking-at (format ".*?\\(%s\\)[ \t]*$"
  9946. (format label-fmt coderef))))))
  9947. (setq type 'dedicated)
  9948. (goto-char (match-beginning 1))
  9949. (throw :coderef-match nil))))
  9950. (goto-char origin)
  9951. (error "No match for coderef: %s" coderef))))
  9952. ((string-match "\\`/\\(.*\\)/\\'" normalized)
  9953. ;; Look for a regular expression.
  9954. (funcall (if (derived-mode-p 'org-mode) #'org-occur #'org-do-occur)
  9955. (match-string 1 s)))
  9956. ;; Fuzzy links.
  9957. (t
  9958. (let* ((starred (eq (string-to-char normalized) ?*))
  9959. (headline-search (and (derived-mode-p 'org-mode)
  9960. (or org-link-search-must-match-exact-headline
  9961. starred))))
  9962. (cond
  9963. ;; Look for targets, only if not in a headline search.
  9964. ((and (not starred)
  9965. (let ((target (format "<<%s>>" s-multi-re)))
  9966. (catch :target-match
  9967. (goto-char (point-min))
  9968. (while (re-search-forward target nil t)
  9969. (backward-char)
  9970. (let ((context (org-element-context)))
  9971. (when (eq (org-element-type context) 'target)
  9972. (setq type 'dedicated)
  9973. (goto-char (org-element-property :begin context))
  9974. (throw :target-match t))))
  9975. nil))))
  9976. ;; Look for elements named after S, only if not in a headline
  9977. ;; search.
  9978. ((and (not starred)
  9979. (let ((name (format "^[ \t]*#\\+NAME: +%s[ \t]*$" s-single-re)))
  9980. (catch :name-match
  9981. (goto-char (point-min))
  9982. (while (re-search-forward name nil t)
  9983. (let ((element (org-element-at-point)))
  9984. (when (equal (org-split-string
  9985. (org-element-property :name element)
  9986. "[ \t]+")
  9987. words)
  9988. (setq type 'dedicated)
  9989. (beginning-of-line)
  9990. (throw :name-match t))))
  9991. nil))))
  9992. ;; Regular text search. Prefer headlines in Org mode
  9993. ;; buffers.
  9994. ((and (derived-mode-p 'org-mode)
  9995. (let* ((wspace "[ \t]")
  9996. (wspaceopt (concat wspace "*"))
  9997. (cookie (concat "\\(?:"
  9998. wspaceopt
  9999. "\\[[0-9]*\\(?:%\\|/[0-9]*\\)\\]"
  10000. wspaceopt
  10001. "\\)"))
  10002. (sep (concat "\\(?:\\(?:" wspace "\\|" cookie "\\)+\\)"))
  10003. (re (concat
  10004. org-outline-regexp-bol
  10005. "\\(?:" org-todo-regexp "[ \t]+\\)?"
  10006. "\\(?:\\[#.\\][ \t]+\\)?"
  10007. "\\(?:" org-comment-string "[ \t]+\\)?"
  10008. sep "?"
  10009. (let ((title (mapconcat #'regexp-quote
  10010. words
  10011. sep)))
  10012. (if starred (substring title 1) title))
  10013. sep "?"
  10014. (org-re "\\(?:[ \t]+:[[:alnum:]_@#%%:]+:\\)?")
  10015. "[ \t]*$")))
  10016. (goto-char (point-min))
  10017. (re-search-forward re nil t)))
  10018. (goto-char (match-beginning 0))
  10019. (setq type 'dedicated))
  10020. ;; Offer to create non-existent headline depending on
  10021. ;; `org-link-search-must-match-exact-headline'.
  10022. ((and (derived-mode-p 'org-mode)
  10023. (not org-link-search-inhibit-query)
  10024. (eq org-link-search-must-match-exact-headline 'query-to-create)
  10025. (yes-or-no-p "No match - create this as a new heading? "))
  10026. (goto-char (point-max))
  10027. (unless (bolp) (newline))
  10028. (org-insert-heading nil t t)
  10029. (insert s "\n")
  10030. (beginning-of-line 0))
  10031. ;; Only headlines are looked after. No need to process
  10032. ;; further: throw an error.
  10033. ((and (derived-mode-p 'org-mode)
  10034. (or starred org-link-search-must-match-exact-headline))
  10035. (goto-char origin)
  10036. (error "No match for fuzzy expression: %s" normalized))
  10037. ;; Regular text search.
  10038. ((catch :fuzzy-match
  10039. (goto-char (point-min))
  10040. (while (re-search-forward s-multi-re nil t)
  10041. ;; Skip match if it contains AVOID-POS or it is included
  10042. ;; in a link with a description but outside the
  10043. ;; description.
  10044. (unless (or (and avoid-pos
  10045. (<= (match-beginning 0) avoid-pos)
  10046. (> (match-end 0) avoid-pos))
  10047. (and (save-match-data
  10048. (org-in-regexp org-bracket-link-regexp))
  10049. (match-beginning 3)
  10050. (or (> (match-beginning 3) (point))
  10051. (<= (match-end 3) (point)))
  10052. (org-element-lineage
  10053. (save-match-data (org-element-context))
  10054. '(link) t)))
  10055. (goto-char (match-beginning 0))
  10056. (setq type 'fuzzy)
  10057. (throw :fuzzy-match t)))
  10058. nil))
  10059. ;; All failed. Throw an error.
  10060. (t (goto-char origin)
  10061. (error "No match for fuzzy expression: %s" normalized))))))
  10062. ;; Disclose surroundings of match, if appropriate.
  10063. (when (and (derived-mode-p 'org-mode) (not stealth))
  10064. (org-show-context 'link-search))
  10065. type))
  10066. (defun org-get-buffer-for-internal-link (buffer)
  10067. "Return a buffer to be used for displaying the link target of internal links."
  10068. (cond
  10069. ((not org-display-internal-link-with-indirect-buffer)
  10070. buffer)
  10071. ((string-match "(Clone)$" (buffer-name buffer))
  10072. (message "Buffer is already a clone, not making another one")
  10073. ;; we also do not modify visibility in this case
  10074. buffer)
  10075. (t ; make a new indirect buffer for displaying the link
  10076. (let* ((bn (buffer-name buffer))
  10077. (ibn (concat bn "(Clone)"))
  10078. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  10079. (with-current-buffer ib (org-overview))
  10080. ib))))
  10081. (defun org-do-occur (regexp &optional cleanup)
  10082. "Call the Emacs command `occur'.
  10083. If CLEANUP is non-nil, remove the printout of the regular expression
  10084. in the *Occur* buffer. This is useful if the regex is long and not useful
  10085. to read."
  10086. (occur regexp)
  10087. (when cleanup
  10088. (let ((cwin (selected-window)) win beg end)
  10089. (when (setq win (get-buffer-window "*Occur*"))
  10090. (select-window win))
  10091. (goto-char (point-min))
  10092. (when (re-search-forward "match[a-z]+" nil t)
  10093. (setq beg (match-end 0))
  10094. (when (re-search-forward "^[ \t]*[0-9]+" nil t)
  10095. (setq end (1- (match-beginning 0)))))
  10096. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  10097. (goto-char (point-min))
  10098. (select-window cwin))))
  10099. ;;; The mark ring for links jumps
  10100. (defvar org-mark-ring nil
  10101. "Mark ring for positions before jumps in Org-mode.")
  10102. (defvar org-mark-ring-last-goto nil
  10103. "Last position in the mark ring used to go back.")
  10104. ;; Fill and close the ring
  10105. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  10106. (loop for i from 1 to org-mark-ring-length do
  10107. (push (make-marker) org-mark-ring))
  10108. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  10109. org-mark-ring)
  10110. (defun org-mark-ring-push (&optional pos buffer)
  10111. "Put the current position or POS into the mark ring and rotate it."
  10112. (interactive)
  10113. (setq pos (or pos (point)))
  10114. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  10115. (move-marker (car org-mark-ring)
  10116. (or pos (point))
  10117. (or buffer (current-buffer)))
  10118. (message "%s"
  10119. (substitute-command-keys
  10120. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  10121. (defun org-mark-ring-goto (&optional n)
  10122. "Jump to the previous position in the mark ring.
  10123. With prefix arg N, jump back that many stored positions. When
  10124. called several times in succession, walk through the entire ring.
  10125. Org-mode commands jumping to a different position in the current file,
  10126. or to another Org-mode file, automatically push the old position
  10127. onto the ring."
  10128. (interactive "p")
  10129. (let (p m)
  10130. (if (eq last-command this-command)
  10131. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  10132. (setq p org-mark-ring))
  10133. (setq org-mark-ring-last-goto p)
  10134. (setq m (car p))
  10135. (org-pop-to-buffer-same-window (marker-buffer m))
  10136. (goto-char m)
  10137. (when (or (outline-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  10138. ;;; TODO: Use string-remove-prefix and -suffix once we only support
  10139. ;;; Emacs 24.4+
  10140. (defun org-remove-angle-brackets (s)
  10141. (when (equal (substring s 0 1) "<") (setq s (substring s 1)))
  10142. (when (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  10143. s)
  10144. (defun org-add-angle-brackets (s)
  10145. (unless (equal (substring s 0 1) "<") (setq s (concat "<" s)))
  10146. (unless (equal (substring s -1) ">") (setq s (concat s ">")))
  10147. s)
  10148. (defun org-remove-double-quotes (s)
  10149. (when (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  10150. (when (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  10151. s)
  10152. ;;; Following specific links
  10153. (defun org-follow-timestamp-link ()
  10154. "Open an agenda view for the time-stamp date/range at point."
  10155. (cond
  10156. ((org-at-date-range-p t)
  10157. (let ((org-agenda-start-on-weekday)
  10158. (t1 (match-string 1))
  10159. (t2 (match-string 2)) tt1 tt2)
  10160. (setq tt1 (time-to-days (org-time-string-to-time t1))
  10161. tt2 (time-to-days (org-time-string-to-time t2)))
  10162. (let ((org-agenda-buffer-tmp-name
  10163. (format "*Org Agenda(a:%s)"
  10164. (concat (substring t1 0 10) "--" (substring t2 0 10)))))
  10165. (org-agenda-list nil tt1 (1+ (- tt2 tt1))))))
  10166. ((org-at-timestamp-p t)
  10167. (let ((org-agenda-buffer-tmp-name
  10168. (format "*Org Agenda(a:%s)" (substring (match-string 1) 0 10))))
  10169. (org-agenda-list nil (time-to-days (org-time-string-to-time
  10170. (substring (match-string 1) 0 10)))
  10171. 1)))
  10172. (t (error "This should not happen"))))
  10173. ;;; Following file links
  10174. (declare-function mailcap-parse-mailcaps "mailcap" (&optional path force))
  10175. (declare-function mailcap-extension-to-mime "mailcap" (extn))
  10176. (declare-function mailcap-mime-info
  10177. "mailcap" (string &optional request no-decode))
  10178. (defvar org-wait nil)
  10179. (defun org-open-file (path &optional in-emacs line search)
  10180. "Open the file at PATH.
  10181. First, this expands any special file name abbreviations. Then the
  10182. configuration variable `org-file-apps' is checked if it contains an
  10183. entry for this file type, and if yes, the corresponding command is launched.
  10184. If no application is found, Emacs simply visits the file.
  10185. With optional prefix argument IN-EMACS, Emacs will visit the file.
  10186. With a double \\[universal-argument] \\[universal-argument] \
  10187. prefix arg, Org tries to avoid opening in Emacs
  10188. and to use an external application to visit the file.
  10189. Optional LINE specifies a line to go to, optional SEARCH a string
  10190. to search for. If LINE or SEARCH is given, the file will be
  10191. opened in Emacs, unless an entry from org-file-apps that makes
  10192. use of groups in a regexp matches.
  10193. If you want to change the way frames are used when following a
  10194. link, please customize `org-link-frame-setup'.
  10195. If the file does not exist, an error is thrown."
  10196. (let* ((file (if (equal path "")
  10197. buffer-file-name
  10198. (substitute-in-file-name (expand-file-name path))))
  10199. (file-apps (append org-file-apps (org-default-apps)))
  10200. (apps (cl-remove-if
  10201. 'org-file-apps-entry-match-against-dlink-p file-apps))
  10202. (apps-dlink (cl-remove-if-not
  10203. 'org-file-apps-entry-match-against-dlink-p file-apps))
  10204. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  10205. (dirp (unless remp (file-directory-p file)))
  10206. (file (if (and dirp org-open-directory-means-index-dot-org)
  10207. (concat (file-name-as-directory file) "index.org")
  10208. file))
  10209. (a-m-a-p (assq 'auto-mode apps))
  10210. (dfile (downcase file))
  10211. ;; reconstruct the original file: link from the PATH, LINE and SEARCH args
  10212. (link (cond ((and (eq line nil)
  10213. (eq search nil))
  10214. file)
  10215. (line
  10216. (concat file "::" (number-to-string line)))
  10217. (search
  10218. (concat file "::" search))))
  10219. (dlink (downcase link))
  10220. (old-buffer (current-buffer))
  10221. (old-pos (point))
  10222. (old-mode major-mode)
  10223. ext cmd link-match-data)
  10224. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  10225. (setq ext (match-string 1 dfile))
  10226. (when (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  10227. (setq ext (match-string 1 dfile))))
  10228. (cond
  10229. ((member in-emacs '((16) system))
  10230. (setq cmd (cdr (assoc 'system apps))))
  10231. (in-emacs (setq cmd 'emacs))
  10232. (t
  10233. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  10234. (and dirp (cdr (assoc 'directory apps)))
  10235. ; first, try matching against apps-dlink
  10236. ; if we get a match here, store the match data for later
  10237. (let ((match (assoc-default dlink apps-dlink
  10238. 'string-match)))
  10239. (if match
  10240. (progn (setq link-match-data (match-data))
  10241. match)
  10242. (progn (setq in-emacs (or in-emacs line search))
  10243. nil))) ; if we have no match in apps-dlink,
  10244. ; always open the file in emacs if line or search
  10245. ; is given (for backwards compatibility)
  10246. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  10247. 'string-match)
  10248. (cdr (assoc ext apps))
  10249. (cdr (assoc t apps))))))
  10250. (when (eq cmd 'system)
  10251. (setq cmd (cdr (assoc 'system apps))))
  10252. (when (eq cmd 'default)
  10253. (setq cmd (cdr (assoc t apps))))
  10254. (when (eq cmd 'mailcap)
  10255. (require 'mailcap)
  10256. (mailcap-parse-mailcaps)
  10257. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  10258. (command (mailcap-mime-info mime-type)))
  10259. (if (stringp command)
  10260. (setq cmd command)
  10261. (setq cmd 'emacs))))
  10262. (when (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  10263. (not (file-exists-p file))
  10264. (not org-open-non-existing-files))
  10265. (user-error "No such file: %s" file))
  10266. (cond
  10267. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  10268. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  10269. (while (string-match "['\"]%s['\"]" cmd)
  10270. (setq cmd (replace-match "%s" t t cmd)))
  10271. (while (string-match "%s" cmd)
  10272. (setq cmd (replace-match
  10273. (save-match-data
  10274. (shell-quote-argument
  10275. (convert-standard-filename file)))
  10276. t t cmd)))
  10277. ;; Replace "%1", "%2" etc. in command with group matches from regex
  10278. (save-match-data
  10279. (let ((match-index 1)
  10280. (number-of-groups (- (/ (length link-match-data) 2) 1)))
  10281. (set-match-data link-match-data)
  10282. (while (<= match-index number-of-groups)
  10283. (let ((regex (concat "%" (number-to-string match-index)))
  10284. (replace-with (match-string match-index dlink)))
  10285. (while (string-match regex cmd)
  10286. (setq cmd (replace-match replace-with t t cmd))))
  10287. (setq match-index (+ match-index 1)))))
  10288. (save-window-excursion
  10289. (message "Running %s...done" cmd)
  10290. (start-process-shell-command cmd nil cmd)
  10291. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))))
  10292. ((or (stringp cmd)
  10293. (eq cmd 'emacs))
  10294. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  10295. (widen)
  10296. (if line (progn (org-goto-line line)
  10297. (when (derived-mode-p 'org-mode)
  10298. (org-reveal)))
  10299. (when search (org-link-search search))))
  10300. ((consp cmd)
  10301. (let ((file (convert-standard-filename file)))
  10302. (save-match-data
  10303. (set-match-data link-match-data)
  10304. (eval cmd))))
  10305. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  10306. (and (derived-mode-p 'org-mode) (eq old-mode 'org-mode)
  10307. (or (not (equal old-buffer (current-buffer)))
  10308. (not (equal old-pos (point))))
  10309. (org-mark-ring-push old-pos old-buffer))))
  10310. (defun org-file-apps-entry-match-against-dlink-p (entry)
  10311. "This function returns non-nil if `entry' uses a regular
  10312. expression which should be matched against the whole link by
  10313. org-open-file.
  10314. It assumes that is the case when the entry uses a regular
  10315. expression which has at least one grouping construct and the
  10316. action is either a lisp form or a command string containing
  10317. `%1', i.e. using at least one subexpression match as a
  10318. parameter."
  10319. (let ((selector (car entry))
  10320. (action (cdr entry)))
  10321. (if (stringp selector)
  10322. (and (> (regexp-opt-depth selector) 0)
  10323. (or (and (stringp action)
  10324. (string-match "%[0-9]" action))
  10325. (consp action)))
  10326. nil)))
  10327. (defun org-default-apps ()
  10328. "Return the default applications for this operating system."
  10329. (cond
  10330. ((eq system-type 'darwin)
  10331. org-file-apps-defaults-macosx)
  10332. ((eq system-type 'windows-nt)
  10333. org-file-apps-defaults-windowsnt)
  10334. (t org-file-apps-defaults-gnu)))
  10335. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  10336. "Convert extensions to regular expressions in the cars of LIST.
  10337. Also, weed out any non-string entries, because the return value is used
  10338. only for regexp matching.
  10339. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  10340. point to the symbol `emacs', indicating that the file should
  10341. be opened in Emacs."
  10342. (append
  10343. (delq nil
  10344. (mapcar (lambda (x)
  10345. (unless (not (stringp (car x)))
  10346. (if (string-match "\\W" (car x))
  10347. x
  10348. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  10349. list))
  10350. (when add-auto-mode
  10351. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  10352. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  10353. (defun org-file-remote-p (file)
  10354. "Test whether FILE specifies a location on a remote system.
  10355. Return non-nil if the location is indeed remote.
  10356. For example, the filename \"/user@host:/foo\" specifies a location
  10357. on the system \"/user@host:\"."
  10358. (cond ((fboundp 'file-remote-p)
  10359. (file-remote-p file))
  10360. ((fboundp 'tramp-handle-file-remote-p)
  10361. (tramp-handle-file-remote-p file))
  10362. ((and (boundp 'ange-ftp-name-format)
  10363. (string-match (car ange-ftp-name-format) file))
  10364. t)))
  10365. ;;;; Refiling
  10366. (defun org-get-org-file ()
  10367. "Read a filename, with default directory `org-directory'."
  10368. (let ((default (or org-default-notes-file remember-data-file)))
  10369. (read-file-name (format "File name [%s]: " default)
  10370. (file-name-as-directory org-directory)
  10371. default)))
  10372. (defun org-notes-order-reversed-p ()
  10373. "Check if the current file should receive notes in reversed order."
  10374. (cond
  10375. ((not org-reverse-note-order) nil)
  10376. ((eq t org-reverse-note-order) t)
  10377. ((not (listp org-reverse-note-order)) nil)
  10378. (t (catch 'exit
  10379. (dolist (entry org-reverse-note-order)
  10380. (when (string-match (car entry) buffer-file-name)
  10381. (throw 'exit (cdr entry))))))))
  10382. (defvar org-refile-target-table nil
  10383. "The list of refile targets, created by `org-refile'.")
  10384. (defvar org-agenda-new-buffers nil
  10385. "Buffers created to visit agenda files.")
  10386. (defvar org-refile-cache nil
  10387. "Cache for refile targets.")
  10388. (defvar org-refile-markers nil
  10389. "All the markers used for caching refile locations.")
  10390. (defun org-refile-marker (pos)
  10391. "Get a new refile marker, but only if caching is in use."
  10392. (if (not org-refile-use-cache)
  10393. pos
  10394. (let ((m (make-marker)))
  10395. (move-marker m pos)
  10396. (push m org-refile-markers)
  10397. m)))
  10398. (defun org-refile-cache-clear ()
  10399. "Clear the refile cache and disable all the markers."
  10400. (mapc (lambda (m) (move-marker m nil)) org-refile-markers)
  10401. (setq org-refile-markers nil)
  10402. (setq org-refile-cache nil)
  10403. (message "Refile cache has been cleared"))
  10404. (defun org-refile-cache-check-set (set)
  10405. "Check if all the markers in the cache still have live buffers."
  10406. (let (marker)
  10407. (catch 'exit
  10408. (while (and set (setq marker (nth 3 (pop set))))
  10409. ;; If `org-refile-use-outline-path' is 'file, marker may be nil
  10410. (when (and marker (null (marker-buffer marker)))
  10411. (message "Please regenerate the refile cache with `C-0 C-c C-w'")
  10412. (sit-for 3)
  10413. (throw 'exit nil)))
  10414. t)))
  10415. (defun org-refile-cache-put (set &rest identifiers)
  10416. "Push the refile targets SET into the cache, under IDENTIFIERS."
  10417. (let* ((key (sha1 (prin1-to-string identifiers)))
  10418. (entry (assoc key org-refile-cache)))
  10419. (if entry
  10420. (setcdr entry set)
  10421. (push (cons key set) org-refile-cache))))
  10422. (defun org-refile-cache-get (&rest identifiers)
  10423. "Retrieve the cached value for refile targets given by IDENTIFIERS."
  10424. (cond
  10425. ((not org-refile-cache) nil)
  10426. ((not org-refile-use-cache) (org-refile-cache-clear) nil)
  10427. (t
  10428. (let ((set (cdr (assoc (sha1 (prin1-to-string identifiers))
  10429. org-refile-cache))))
  10430. (and set (org-refile-cache-check-set set) set)))))
  10431. (defun org-refile-get-targets (&optional default-buffer excluded-entries)
  10432. "Produce a table with refile targets."
  10433. (let ((case-fold-search nil)
  10434. ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
  10435. (entries (or org-refile-targets '((nil . (:level . 1)))))
  10436. targets tgs txt re files desc descre fast-path-p level pos0)
  10437. (message "Getting targets...")
  10438. (with-current-buffer (or default-buffer (current-buffer))
  10439. (dolist (entry entries)
  10440. (setq files (car entry) desc (cdr entry))
  10441. (setq fast-path-p nil)
  10442. (cond
  10443. ((null files) (setq files (list (current-buffer))))
  10444. ((eq files 'org-agenda-files)
  10445. (setq files (org-agenda-files 'unrestricted)))
  10446. ((and (symbolp files) (fboundp files))
  10447. (setq files (funcall files)))
  10448. ((and (symbolp files) (boundp files))
  10449. (setq files (symbol-value files))))
  10450. (when (stringp files) (setq files (list files)))
  10451. (cond
  10452. ((eq (car desc) :tag)
  10453. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  10454. ((eq (car desc) :todo)
  10455. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  10456. ((eq (car desc) :regexp)
  10457. (setq descre (cdr desc)))
  10458. ((eq (car desc) :level)
  10459. (setq descre (concat "^\\*\\{" (number-to-string
  10460. (if org-odd-levels-only
  10461. (1- (* 2 (cdr desc)))
  10462. (cdr desc)))
  10463. "\\}[ \t]")))
  10464. ((eq (car desc) :maxlevel)
  10465. (setq fast-path-p t)
  10466. (setq descre (concat "^\\*\\{1," (number-to-string
  10467. (if org-odd-levels-only
  10468. (1- (* 2 (cdr desc)))
  10469. (cdr desc)))
  10470. "\\}[ \t]")))
  10471. (t (error "Bad refiling target description %s" desc)))
  10472. (dolist (f files)
  10473. (with-current-buffer
  10474. (if (bufferp f) f (org-get-agenda-file-buffer f))
  10475. (or
  10476. (setq tgs (org-refile-cache-get (buffer-file-name) descre))
  10477. (progn
  10478. (when (bufferp f) (setq f (buffer-file-name
  10479. (buffer-base-buffer f))))
  10480. (setq f (and f (expand-file-name f)))
  10481. (when (eq org-refile-use-outline-path 'file)
  10482. (push (list (file-name-nondirectory f) f nil nil) tgs))
  10483. (save-excursion
  10484. (save-restriction
  10485. (widen)
  10486. (goto-char (point-min))
  10487. (while (re-search-forward descre nil t)
  10488. (goto-char (setq pos0 (point-at-bol)))
  10489. (catch 'next
  10490. (when org-refile-target-verify-function
  10491. (save-match-data
  10492. (or (funcall org-refile-target-verify-function)
  10493. (throw 'next t))))
  10494. (when (and (looking-at org-complex-heading-regexp)
  10495. (not (member (match-string 4) excluded-entries))
  10496. (match-string 4))
  10497. (setq level (org-reduced-level
  10498. (- (match-end 1) (match-beginning 1)))
  10499. txt (org-link-display-format (match-string 4))
  10500. txt (replace-regexp-in-string "\\( *[[0-9]+/?[0-9]*%?]\\)+$" "" txt)
  10501. re (format org-complex-heading-regexp-format
  10502. (regexp-quote (match-string 4))))
  10503. (when org-refile-use-outline-path
  10504. (setq txt (mapconcat
  10505. 'org-protect-slash
  10506. (append
  10507. (if (eq org-refile-use-outline-path
  10508. 'file)
  10509. (list (file-name-nondirectory
  10510. (buffer-file-name
  10511. (buffer-base-buffer))))
  10512. (when (eq org-refile-use-outline-path
  10513. 'full-file-path)
  10514. (list (buffer-file-name
  10515. (buffer-base-buffer)))))
  10516. (org-get-outline-path fast-path-p
  10517. level txt)
  10518. (list txt))
  10519. "/")))
  10520. (push (list txt f re (org-refile-marker (point)))
  10521. tgs)))
  10522. (when (= (point) pos0)
  10523. ;; verification function has not moved point
  10524. (goto-char (point-at-eol))))))))
  10525. (when org-refile-use-cache
  10526. (org-refile-cache-put tgs (buffer-file-name) descre))
  10527. (setq targets (append tgs targets))))))
  10528. (message "Getting targets...done")
  10529. (nreverse targets)))
  10530. (defun org-protect-slash (s)
  10531. (while (string-match "/" s)
  10532. (setq s (replace-match "\\" t t s)))
  10533. s)
  10534. (defvar org-olpa (make-vector 20 nil))
  10535. (defun org-get-outline-path (&optional fastp level heading)
  10536. "Return the outline path to the current entry, as a list.
  10537. The parameters FASTP, LEVEL, and HEADING are for use by a scanner
  10538. routine which makes outline path derivations for an entire file,
  10539. avoiding backtracing. Refile target collection makes use of that."
  10540. (if fastp
  10541. (progn
  10542. (when (> level 19)
  10543. (error "Outline path failure, more than 19 levels"))
  10544. (loop for i from level upto 19 do
  10545. (aset org-olpa i nil))
  10546. (prog1
  10547. (delq nil (append org-olpa nil))
  10548. (aset org-olpa level heading)))
  10549. (let (rtn case-fold-search)
  10550. (save-excursion
  10551. (save-restriction
  10552. (widen)
  10553. (while (org-up-heading-safe)
  10554. (when (looking-at org-complex-heading-regexp)
  10555. (push (org-trim
  10556. (replace-regexp-in-string
  10557. ;; Remove statistical/checkboxes cookies
  10558. "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" ""
  10559. (org-match-string-no-properties 4)))
  10560. rtn)))
  10561. rtn)))))
  10562. (defun org-format-outline-path (path &optional width prefix separator)
  10563. "Format the outline path PATH for display.
  10564. WIDTH is the maximum number of characters that is available.
  10565. PREFIX is a prefix to be included in the returned string,
  10566. such as the file name.
  10567. SEPARATOR is inserted between the different parts of the path,
  10568. the default is \"/\"."
  10569. (setq width (or width 79))
  10570. (setq path (delq nil path))
  10571. (unless (> width 0)
  10572. (user-error "Argument `width' must be positive"))
  10573. (setq separator (or separator "/"))
  10574. (let* ((org-odd-levels-only nil)
  10575. (fpath (concat
  10576. prefix (and prefix path separator)
  10577. (mapconcat
  10578. (lambda (s) (replace-regexp-in-string "[ \t]+\\'" "" s))
  10579. (loop for head in path
  10580. for n from 0
  10581. collect (org-add-props
  10582. head nil 'face
  10583. (nth (% n org-n-level-faces) org-level-faces)))
  10584. separator))))
  10585. (when (> (length fpath) width)
  10586. (if (< width 7)
  10587. ;; It's unlikely that `width' will be this small, but don't
  10588. ;; waste characters by adding ".." if it is.
  10589. (setq fpath (substring fpath 0 width))
  10590. (setf (substring fpath (- width 2)) "..")))
  10591. fpath))
  10592. (defun org-display-outline-path (&optional file current separator just-return-string)
  10593. "Display the current outline path in the echo area.
  10594. If FILE is non-nil, prepend the output with the file name.
  10595. If CURRENT is non-nil, append the current heading to the output.
  10596. SEPARATOR is passed through to `org-format-outline-path'. It separates
  10597. the different parts of the path and defaults to \"/\".
  10598. If JUST-RETURN-STRING is non-nil, return a string, don't display a message."
  10599. (interactive "P")
  10600. (let* (case-fold-search
  10601. (bfn (buffer-file-name (buffer-base-buffer)))
  10602. (path (and (derived-mode-p 'org-mode) (org-get-outline-path)))
  10603. res)
  10604. (when current (setq path (append path
  10605. (save-excursion
  10606. (org-back-to-heading t)
  10607. (when (looking-at org-complex-heading-regexp)
  10608. (list (match-string 4)))))))
  10609. (setq res
  10610. (org-format-outline-path
  10611. path
  10612. (1- (frame-width))
  10613. (and file bfn (concat (file-name-nondirectory bfn) separator))
  10614. separator))
  10615. (if just-return-string
  10616. (org-no-properties res)
  10617. (org-unlogged-message "%s" res))))
  10618. (defvar org-refile-history nil
  10619. "History for refiling operations.")
  10620. (defvar org-after-refile-insert-hook nil
  10621. "Hook run after `org-refile' has inserted its stuff at the new location.
  10622. Note that this is still *before* the stuff will be removed from
  10623. the *old* location.")
  10624. (defvar org-capture-last-stored-marker)
  10625. (defvar org-refile-keep nil
  10626. "Non-nil means `org-refile' will copy instead of refile.")
  10627. (defun org-copy ()
  10628. "Like `org-refile', but copy."
  10629. (interactive)
  10630. (let ((org-refile-keep t))
  10631. (funcall 'org-refile nil nil nil "Copy")))
  10632. (defun org-refile (&optional arg default-buffer rfloc msg)
  10633. "Move the entry or entries at point to another heading.
  10634. The list of target headings is compiled using the information in
  10635. `org-refile-targets', which see.
  10636. At the target location, the entry is filed as a subitem of the
  10637. target heading. Depending on `org-reverse-note-order', the new
  10638. subitem will either be the first or the last subitem.
  10639. If there is an active region, all entries in that region will be
  10640. refiled. However, the region must fulfill the requirement that
  10641. the first heading sets the top-level of the moved text.
  10642. With prefix arg ARG, the command will only visit the target
  10643. location and not actually move anything.
  10644. With a double prefix arg \\[universal-argument] \\[universal-argument], go to the location where the last
  10645. refiling operation has put the subtree.
  10646. With a numeric prefix argument of `2', refile to the running clock.
  10647. With a numeric prefix argument of `3', emulate `org-refile-keep'
  10648. being set to t and copy to the target location, don't move it.
  10649. Beware that keeping refiled entries may result in duplicated ID
  10650. properties.
  10651. RFLOC can be a refile location obtained in a different way.
  10652. MSG is a string to replace \"Refile\" in the default prompt with
  10653. another verb. E.g. `org-copy' sets this parameter to \"Copy\".
  10654. See also `org-refile-use-outline-path' and `org-completion-use-ido'.
  10655. If you are using target caching (see `org-refile-use-cache'), you
  10656. have to clear the target cache in order to find new targets.
  10657. This can be done with a 0 prefix (`C-0 C-c C-w') or a triple
  10658. prefix argument (`C-u C-u C-u C-c C-w')."
  10659. (interactive "P")
  10660. (if (member arg '(0 (64)))
  10661. (org-refile-cache-clear)
  10662. (let* ((actionmsg (cond (msg msg)
  10663. ((equal arg 3) "Refile (and keep)")
  10664. (t "Refile")))
  10665. (cbuf (current-buffer))
  10666. (regionp (org-region-active-p))
  10667. (region-start (and regionp (region-beginning)))
  10668. (region-end (and regionp (region-end)))
  10669. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  10670. (org-refile-keep (if (equal arg 3) t org-refile-keep))
  10671. pos it nbuf file re level reversed)
  10672. (setq last-command nil)
  10673. (when regionp
  10674. (goto-char region-start)
  10675. (or (bolp) (goto-char (point-at-bol)))
  10676. (setq region-start (point))
  10677. (unless (or (org-kill-is-subtree-p
  10678. (buffer-substring region-start region-end))
  10679. (prog1 org-refile-active-region-within-subtree
  10680. (let ((s (point-at-eol)))
  10681. (org-toggle-heading)
  10682. (setq region-end (+ (- (point-at-eol) s) region-end)))))
  10683. (user-error "The region is not a (sequence of) subtree(s)")))
  10684. (if (equal arg '(16))
  10685. (org-refile-goto-last-stored)
  10686. (when (or
  10687. (and (equal arg 2)
  10688. org-clock-hd-marker (marker-buffer org-clock-hd-marker)
  10689. (prog1
  10690. (setq it (list (or org-clock-heading "running clock")
  10691. (buffer-file-name
  10692. (marker-buffer org-clock-hd-marker))
  10693. ""
  10694. (marker-position org-clock-hd-marker)))
  10695. (setq arg nil)))
  10696. (setq it (or rfloc
  10697. (let (heading-text)
  10698. (save-excursion
  10699. (unless (and arg (listp arg))
  10700. (org-back-to-heading t)
  10701. (setq heading-text
  10702. (replace-regexp-in-string
  10703. org-bracket-link-regexp
  10704. "\\3"
  10705. (nth 4 (org-heading-components)))))
  10706. (org-refile-get-location
  10707. (cond ((and arg (listp arg)) "Goto")
  10708. (regionp (concat actionmsg " region to"))
  10709. (t (concat actionmsg " subtree \""
  10710. heading-text "\" to")))
  10711. default-buffer
  10712. (and (not (equal '(4) arg))
  10713. org-refile-allow-creating-parent-nodes)
  10714. arg))))))
  10715. (setq file (nth 1 it)
  10716. re (nth 2 it)
  10717. pos (nth 3 it))
  10718. (when (and (not arg)
  10719. pos
  10720. (equal (buffer-file-name) file)
  10721. (if regionp
  10722. (and (>= pos region-start)
  10723. (<= pos region-end))
  10724. (and (>= pos (point))
  10725. (< pos (save-excursion
  10726. (org-end-of-subtree t t))))))
  10727. (error "Cannot refile to position inside the tree or region"))
  10728. (setq nbuf (or (find-buffer-visiting file)
  10729. (find-file-noselect file)))
  10730. (if (and arg (not (equal arg 3)))
  10731. (progn
  10732. (org-pop-to-buffer-same-window nbuf)
  10733. (goto-char pos)
  10734. (org-show-context 'org-goto))
  10735. (if regionp
  10736. (progn
  10737. (org-kill-new (buffer-substring region-start region-end))
  10738. (org-save-markers-in-region region-start region-end))
  10739. (org-copy-subtree 1 nil t))
  10740. (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
  10741. (find-file-noselect file)))
  10742. (setq reversed (org-notes-order-reversed-p))
  10743. (save-excursion
  10744. (save-restriction
  10745. (widen)
  10746. (if pos
  10747. (progn
  10748. (goto-char pos)
  10749. (looking-at org-outline-regexp)
  10750. (setq level (org-get-valid-level (funcall outline-level) 1))
  10751. (goto-char
  10752. (if reversed
  10753. (or (outline-next-heading) (point-max))
  10754. (or (save-excursion (org-get-next-sibling))
  10755. (org-end-of-subtree t t)
  10756. (point-max)))))
  10757. (setq level 1)
  10758. (if (not reversed)
  10759. (goto-char (point-max))
  10760. (goto-char (point-min))
  10761. (or (outline-next-heading) (goto-char (point-max)))))
  10762. (unless (bolp) (newline))
  10763. (org-paste-subtree level nil nil t)
  10764. (when org-log-refile
  10765. (org-add-log-setup 'refile nil nil 'findpos org-log-refile)
  10766. (unless (eq org-log-refile 'note)
  10767. (save-excursion (org-add-log-note))))
  10768. (and org-auto-align-tags
  10769. (let ((org-loop-over-headlines-in-active-region nil))
  10770. (org-set-tags nil t)))
  10771. (let ((bookmark-name (plist-get org-bookmark-names-plist
  10772. :last-refile)))
  10773. (when bookmark-name
  10774. (with-demoted-errors
  10775. (bookmark-set bookmark-name))))
  10776. ;; If we are refiling for capture, make sure that the
  10777. ;; last-capture pointers point here
  10778. (when (org-bound-and-true-p org-capture-is-refiling)
  10779. (let ((bookmark-name (plist-get org-bookmark-names-plist
  10780. :last-capture-marker)))
  10781. (when bookmark-name
  10782. (with-demoted-errors
  10783. (bookmark-set bookmark-name))))
  10784. (move-marker org-capture-last-stored-marker (point)))
  10785. (when (fboundp 'deactivate-mark) (deactivate-mark))
  10786. (run-hooks 'org-after-refile-insert-hook))))
  10787. (unless org-refile-keep
  10788. (if regionp
  10789. (delete-region (point) (+ (point) (- region-end region-start)))
  10790. (delete-region
  10791. (and (org-back-to-heading t) (point))
  10792. (min (1+ (buffer-size)) (org-end-of-subtree t t) (point)))))
  10793. (when (featurep 'org-inlinetask)
  10794. (org-inlinetask-remove-END-maybe))
  10795. (setq org-markers-to-move nil)
  10796. (message (concat actionmsg " to \"%s\" in file %s: done") (car it) file)))))))
  10797. (defun org-refile-goto-last-stored ()
  10798. "Go to the location where the last refile was stored."
  10799. (interactive)
  10800. (bookmark-jump (plist-get org-bookmark-names-plist :last-refile))
  10801. (message "This is the location of the last refile"))
  10802. (defun org-refile--get-location (refloc tbl)
  10803. "When user refile to REFLOC, find the associated target in TBL.
  10804. Also check `org-refile-target-table'."
  10805. (car (delq
  10806. nil
  10807. (mapcar
  10808. (lambda (r) (or (assoc r tbl)
  10809. (assoc r org-refile-target-table)))
  10810. (list (replace-regexp-in-string "/$" "" refloc)
  10811. (replace-regexp-in-string "\\([^/]\\)$" "\\1/" refloc))))))
  10812. (defun org-refile-get-location (&optional prompt default-buffer new-nodes
  10813. no-exclude)
  10814. "Prompt the user for a refile location, using PROMPT.
  10815. PROMPT should not be suffixed with a colon and a space, because
  10816. this function appends the default value from
  10817. `org-refile-history' automatically, if that is not empty.
  10818. When NO-EXCLUDE is set, do not exclude headlines in the current subtree,
  10819. this is used for the GOTO interface."
  10820. (let ((org-refile-targets org-refile-targets)
  10821. (org-refile-use-outline-path org-refile-use-outline-path)
  10822. excluded-entries)
  10823. (when (and (derived-mode-p 'org-mode)
  10824. (not org-refile-use-cache)
  10825. (not no-exclude))
  10826. (org-map-tree
  10827. (lambda()
  10828. (setq excluded-entries
  10829. (append excluded-entries (list (org-get-heading t t)))))))
  10830. (setq org-refile-target-table
  10831. (org-refile-get-targets default-buffer excluded-entries)))
  10832. (unless org-refile-target-table
  10833. (user-error "No refile targets"))
  10834. (let* ((cbuf (current-buffer))
  10835. (partial-completion-mode nil)
  10836. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  10837. (cfunc (if (and org-refile-use-outline-path
  10838. org-outline-path-complete-in-steps)
  10839. 'org-olpath-completing-read
  10840. 'org-icompleting-read))
  10841. (extra (if org-refile-use-outline-path "/" ""))
  10842. (cbnex (concat (buffer-name) extra))
  10843. (filename (and cfn (expand-file-name cfn)))
  10844. (tbl (mapcar
  10845. (lambda (x)
  10846. (if (and (not (member org-refile-use-outline-path
  10847. '(file full-file-path)))
  10848. (not (equal filename (nth 1 x))))
  10849. (cons (concat (car x) extra " ("
  10850. (file-name-nondirectory (nth 1 x)) ")")
  10851. (cdr x))
  10852. (cons (concat (car x) extra) (cdr x))))
  10853. org-refile-target-table))
  10854. (completion-ignore-case t)
  10855. cdef
  10856. (prompt (concat prompt
  10857. (or (and (car org-refile-history)
  10858. (concat " (default " (car org-refile-history) ")"))
  10859. (and (assoc cbnex tbl) (setq cdef cbnex)
  10860. (concat " (default " cbnex ")"))) ": "))
  10861. pa answ parent-target child parent old-hist)
  10862. (setq old-hist org-refile-history)
  10863. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  10864. nil 'org-refile-history (or cdef (car org-refile-history))))
  10865. (if (setq pa (org-refile--get-location answ tbl))
  10866. (progn
  10867. (org-refile-check-position pa)
  10868. (when (or (not org-refile-history)
  10869. (not (eq old-hist org-refile-history))
  10870. (not (equal (car pa) (car org-refile-history))))
  10871. (setq org-refile-history
  10872. (cons (car pa) (if (assoc (car org-refile-history) tbl)
  10873. org-refile-history
  10874. (cdr org-refile-history))))
  10875. (when (equal (car org-refile-history) (nth 1 org-refile-history))
  10876. (pop org-refile-history)))
  10877. pa)
  10878. (if (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  10879. (progn
  10880. (setq parent (match-string 1 answ)
  10881. child (match-string 2 answ))
  10882. (setq parent-target (org-refile--get-location parent tbl))
  10883. (when (and parent-target
  10884. (or (eq new-nodes t)
  10885. (and (eq new-nodes 'confirm)
  10886. (y-or-n-p (format "Create new node \"%s\"? "
  10887. child)))))
  10888. (org-refile-new-child parent-target child)))
  10889. (user-error "Invalid target location")))))
  10890. (declare-function org-string-nw-p "org-macs" (s))
  10891. (defun org-refile-check-position (refile-pointer)
  10892. "Check if the refile pointer matches the headline to which it points."
  10893. (let* ((file (nth 1 refile-pointer))
  10894. (re (nth 2 refile-pointer))
  10895. (pos (nth 3 refile-pointer))
  10896. buffer)
  10897. (if (and (not (markerp pos)) (not file))
  10898. (user-error "Please indicate a target file in the refile path")
  10899. (when (org-string-nw-p re)
  10900. (setq buffer (if (markerp pos)
  10901. (marker-buffer pos)
  10902. (or (find-buffer-visiting file)
  10903. (find-file-noselect file))))
  10904. (with-current-buffer buffer
  10905. (save-excursion
  10906. (save-restriction
  10907. (widen)
  10908. (goto-char pos)
  10909. (beginning-of-line 1)
  10910. (unless (org-looking-at-p re)
  10911. (user-error "Invalid refile position, please clear the cache with `C-0 C-c C-w' before refiling")))))))))
  10912. (defun org-refile-new-child (parent-target child)
  10913. "Use refile target PARENT-TARGET to add new CHILD below it."
  10914. (unless parent-target
  10915. (error "Cannot find parent for new node"))
  10916. (let ((file (nth 1 parent-target))
  10917. (pos (nth 3 parent-target))
  10918. level)
  10919. (with-current-buffer (or (find-buffer-visiting file)
  10920. (find-file-noselect file))
  10921. (save-excursion
  10922. (save-restriction
  10923. (widen)
  10924. (if pos
  10925. (goto-char pos)
  10926. (goto-char (point-max))
  10927. (unless (bolp) (newline)))
  10928. (when (looking-at org-outline-regexp)
  10929. (setq level (funcall outline-level))
  10930. (org-end-of-subtree t t))
  10931. (org-back-over-empty-lines)
  10932. (insert "\n" (make-string
  10933. (if pos (org-get-valid-level level 1) 1) ?*)
  10934. " " child "\n")
  10935. (beginning-of-line 0)
  10936. (list (concat (car parent-target) "/" child) file "" (point)))))))
  10937. (defun org-olpath-completing-read (prompt collection &rest args)
  10938. "Read an outline path like a file name."
  10939. (let ((thetable collection)
  10940. (org-completion-use-ido nil) ; does not work with ido.
  10941. (org-completion-use-iswitchb nil)) ; or iswitchb
  10942. (apply #'org-icompleting-read
  10943. prompt
  10944. (lambda (string predicate &optional flag)
  10945. (cond
  10946. ((eq flag nil) (try-completion string thetable))
  10947. ((eq flag t)
  10948. (let ((l (length string)))
  10949. (mapcar (lambda (x)
  10950. (let ((r (substring x l))
  10951. (f (if (string-match " ([^)]*)$" x)
  10952. (match-string 0 x)
  10953. "")))
  10954. (if (string-match "/" r)
  10955. (concat string (substring r 0 (match-end 0)) f)
  10956. x)))
  10957. (all-completions string thetable predicate))))
  10958. ;; Exact match?
  10959. ((eq flag 'lambda) (assoc string thetable))))
  10960. args)))
  10961. ;;;; Dynamic blocks
  10962. (defun org-find-dblock (name)
  10963. "Find the first dynamic block with name NAME in the buffer.
  10964. If not found, stay at current position and return nil."
  10965. (let ((case-fold-search t) pos)
  10966. (save-excursion
  10967. (goto-char (point-min))
  10968. (setq pos (and (re-search-forward
  10969. (concat "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+" name "\\>") nil t)
  10970. (match-beginning 0))))
  10971. (when pos (goto-char pos))
  10972. pos))
  10973. (defun org-create-dblock (plist)
  10974. "Create a dynamic block section, with parameters taken from PLIST.
  10975. PLIST must contain a :name entry which is used as the name of the block."
  10976. (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
  10977. (end-of-line 1)
  10978. (newline))
  10979. (let ((col (current-column))
  10980. (name (plist-get plist :name)))
  10981. (insert "#+BEGIN: " name)
  10982. (while plist
  10983. (if (eq (car plist) :name)
  10984. (setq plist (cddr plist))
  10985. (insert " " (prin1-to-string (pop plist)))))
  10986. (insert "\n\n" (make-string col ?\ ) "#+END:\n")
  10987. (beginning-of-line -2)))
  10988. (defun org-prepare-dblock ()
  10989. "Prepare dynamic block for refresh.
  10990. This empties the block, puts the cursor at the insert position and returns
  10991. the property list including an extra property :name with the block name."
  10992. (unless (looking-at org-dblock-start-re)
  10993. (user-error "Not at a dynamic block"))
  10994. (let* ((begdel (1+ (match-end 0)))
  10995. (name (org-no-properties (match-string 1)))
  10996. (params (append (list :name name)
  10997. (read (concat "(" (match-string 3) ")")))))
  10998. (save-excursion
  10999. (beginning-of-line 1)
  11000. (skip-chars-forward " \t")
  11001. (setq params (plist-put params :indentation-column (current-column))))
  11002. (unless (re-search-forward org-dblock-end-re nil t)
  11003. (error "Dynamic block not terminated"))
  11004. (setq params
  11005. (append params
  11006. (list :content (buffer-substring
  11007. begdel (match-beginning 0)))))
  11008. (delete-region begdel (match-beginning 0))
  11009. (goto-char begdel)
  11010. (open-line 1)
  11011. params))
  11012. (defun org-map-dblocks (&optional command)
  11013. "Apply COMMAND to all dynamic blocks in the current buffer.
  11014. If COMMAND is not given, use `org-update-dblock'."
  11015. (let ((cmd (or command 'org-update-dblock)))
  11016. (save-excursion
  11017. (goto-char (point-min))
  11018. (while (re-search-forward org-dblock-start-re nil t)
  11019. (goto-char (match-beginning 0))
  11020. (save-excursion
  11021. (condition-case nil
  11022. (funcall cmd)
  11023. (error (message "Error during update of dynamic block"))))
  11024. (unless (re-search-forward org-dblock-end-re nil t)
  11025. (error "Dynamic block not terminated"))))))
  11026. (defun org-dblock-update (&optional arg)
  11027. "User command for updating dynamic blocks.
  11028. Update the dynamic block at point. With prefix ARG, update all dynamic
  11029. blocks in the buffer."
  11030. (interactive "P")
  11031. (if arg
  11032. (org-update-all-dblocks)
  11033. (or (looking-at org-dblock-start-re)
  11034. (org-beginning-of-dblock))
  11035. (org-update-dblock)))
  11036. (defun org-update-dblock ()
  11037. "Update the dynamic block at point.
  11038. This means to empty the block, parse for parameters and then call
  11039. the correct writing function."
  11040. (interactive)
  11041. (save-excursion
  11042. (let* ((win (selected-window))
  11043. (pos (point))
  11044. (line (org-current-line))
  11045. (params (org-prepare-dblock))
  11046. (name (plist-get params :name))
  11047. (indent (plist-get params :indentation-column))
  11048. (cmd (intern (concat "org-dblock-write:" name))))
  11049. (message "Updating dynamic block `%s' at line %d..." name line)
  11050. (funcall cmd params)
  11051. (message "Updating dynamic block `%s' at line %d...done" name line)
  11052. (goto-char pos)
  11053. (when (and indent (> indent 0))
  11054. (setq indent (make-string indent ?\ ))
  11055. (save-excursion
  11056. (select-window win)
  11057. (org-beginning-of-dblock)
  11058. (forward-line 1)
  11059. (while (not (looking-at org-dblock-end-re))
  11060. (insert indent)
  11061. (beginning-of-line 2))
  11062. (when (looking-at org-dblock-end-re)
  11063. (and (looking-at "[ \t]+")
  11064. (replace-match ""))
  11065. (insert indent)))))))
  11066. (defun org-beginning-of-dblock ()
  11067. "Find the beginning of the dynamic block at point.
  11068. Error if there is no such block at point."
  11069. (let ((pos (point))
  11070. beg)
  11071. (end-of-line 1)
  11072. (if (and (re-search-backward org-dblock-start-re nil t)
  11073. (setq beg (match-beginning 0))
  11074. (re-search-forward org-dblock-end-re nil t)
  11075. (> (match-end 0) pos))
  11076. (goto-char beg)
  11077. (goto-char pos)
  11078. (error "Not in a dynamic block"))))
  11079. (defun org-update-all-dblocks ()
  11080. "Update all dynamic blocks in the buffer.
  11081. This function can be used in a hook."
  11082. (interactive)
  11083. (when (derived-mode-p 'org-mode)
  11084. (org-map-dblocks 'org-update-dblock)))
  11085. ;;;; Completion
  11086. (declare-function org-export-backend-name "org-export" (cl-x))
  11087. (declare-function org-export-backend-options "org-export" (cl-x))
  11088. (defun org-get-export-keywords ()
  11089. "Return a list of all currently understood export keywords.
  11090. Export keywords include options, block names, attributes and
  11091. keywords relative to each registered export back-end."
  11092. (let (keywords)
  11093. (dolist (backend
  11094. (org-bound-and-true-p org-export-registered-backends)
  11095. (delq nil keywords))
  11096. ;; Back-end name (for keywords, like #+LATEX:)
  11097. (push (upcase (symbol-name (org-export-backend-name backend))) keywords)
  11098. (dolist (option-entry (org-export-backend-options backend))
  11099. ;; Back-end options.
  11100. (push (nth 1 option-entry) keywords)))))
  11101. (defconst org-options-keywords
  11102. '("ARCHIVE:" "AUTHOR:" "BIND:" "CATEGORY:" "COLUMNS:" "CREATOR:" "DATE:"
  11103. "DESCRIPTION:" "DRAWERS:" "EMAIL:" "EXCLUDE_TAGS:" "FILETAGS:" "INCLUDE:"
  11104. "INDEX:" "KEYWORDS:" "LANGUAGE:" "MACRO:" "OPTIONS:" "PROPERTY:"
  11105. "PRIORITIES:" "SELECT_TAGS:" "SEQ_TODO:" "SETUPFILE:" "STARTUP:" "TAGS:"
  11106. "TITLE:" "TODO:" "TYP_TODO:" "SELECT_TAGS:" "EXCLUDE_TAGS:"))
  11107. (defcustom org-structure-template-alist
  11108. '(("s" "#+BEGIN_SRC ?\n\n#+END_SRC")
  11109. ("e" "#+BEGIN_EXAMPLE\n?\n#+END_EXAMPLE")
  11110. ("q" "#+BEGIN_QUOTE\n?\n#+END_QUOTE")
  11111. ("v" "#+BEGIN_VERSE\n?\n#+END_VERSE")
  11112. ("V" "#+BEGIN_VERBATIM\n?\n#+END_VERBATIM")
  11113. ("c" "#+BEGIN_CENTER\n?\n#+END_CENTER")
  11114. ("l" "#+BEGIN_EXPORT latex\n?\n#+END_EXPORT")
  11115. ("L" "#+LaTeX: ")
  11116. ("h" "#+BEGIN_EXPORT html\n?\n#+END_EXPORT")
  11117. ("H" "#+HTML: ")
  11118. ("a" "#+BEGIN_EXPORT ascii\n?\n#+END_EXPORT")
  11119. ("A" "#+ASCII: ")
  11120. ("i" "#+INDEX: ?")
  11121. ("I" "#+INCLUDE: %file ?"))
  11122. "Structure completion elements.
  11123. This is a list of abbreviation keys and values. The value gets inserted
  11124. if you type `<' followed by the key and then press the completion key,
  11125. usually `TAB'. %file will be replaced by a file name after prompting
  11126. for the file using completion. The cursor will be placed at the position
  11127. of the `?' in the template.
  11128. There are two templates for each key, the first uses the original Org syntax,
  11129. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  11130. the default when the /org-mtags.el/ module has been loaded. See also the
  11131. variable `org-mtags-prefer-muse-templates'."
  11132. :group 'org-completion
  11133. :type '(repeat
  11134. (list
  11135. (string :tag "Key")
  11136. (string :tag "Template")))
  11137. :version "25.1"
  11138. :package-version '(Org . "8.3"))
  11139. (defun org-try-structure-completion ()
  11140. "Try to complete a structure template before point.
  11141. This looks for strings like \"<e\" on an otherwise empty line and
  11142. expands them."
  11143. (let ((l (buffer-substring (point-at-bol) (point)))
  11144. a)
  11145. (when (and (looking-at "[ \t]*$")
  11146. (string-match "^[ \t]*<\\([a-zA-Z]+\\)$" l)
  11147. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  11148. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  11149. (match-beginning 1)) a)
  11150. t)))
  11151. (defun org-complete-expand-structure-template (start cell)
  11152. "Expand a structure template."
  11153. (let ((rpl (nth 1 cell))
  11154. (ind ""))
  11155. (delete-region start (point))
  11156. (when (string-match "\\`[ \t]*#\\+" rpl)
  11157. (cond
  11158. ((bolp))
  11159. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  11160. (setq ind (buffer-substring (point-at-bol) (point))))
  11161. (t (newline))))
  11162. (setq start (point))
  11163. (when (string-match "%file" rpl)
  11164. (setq rpl (replace-match
  11165. (concat
  11166. "\""
  11167. (save-match-data
  11168. (abbreviate-file-name (read-file-name "Include file: ")))
  11169. "\"")
  11170. t t rpl)))
  11171. (setq rpl (mapconcat 'identity (split-string rpl "\n")
  11172. (concat "\n" ind)))
  11173. (insert rpl)
  11174. (when (re-search-backward "\\?" start t) (delete-char 1))))
  11175. ;;;; TODO, DEADLINE, Comments
  11176. (defun org-toggle-comment ()
  11177. "Change the COMMENT state of an entry."
  11178. (interactive)
  11179. (save-excursion
  11180. (org-back-to-heading)
  11181. (looking-at org-complex-heading-regexp)
  11182. (goto-char (or (match-end 3) (match-end 2) (match-end 1)))
  11183. (skip-chars-forward " \t")
  11184. (unless (memq (char-before) '(?\s ?\t)) (insert " "))
  11185. (if (org-in-commented-heading-p t)
  11186. (delete-region (point)
  11187. (progn (search-forward " " (line-end-position) 'move)
  11188. (skip-chars-forward " \t")
  11189. (point)))
  11190. (insert org-comment-string)
  11191. (unless (eolp) (insert " ")))))
  11192. (defvar org-last-todo-state-is-todo nil
  11193. "This is non-nil when the last TODO state change led to a TODO state.
  11194. If the last change removed the TODO tag or switched to DONE, then
  11195. this is nil.")
  11196. (defvar org-setting-tags nil) ; dynamically skipped
  11197. (defvar org-todo-setup-filter-hook nil
  11198. "Hook for functions that pre-filter todo specs.
  11199. Each function takes a todo spec and returns either nil or the spec
  11200. transformed into canonical form." )
  11201. (defvar org-todo-get-default-hook nil
  11202. "Hook for functions that get a default item for todo.
  11203. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  11204. nil or a string to be used for the todo mark." )
  11205. (defvar org-agenda-headline-snapshot-before-repeat)
  11206. (defun org-current-effective-time ()
  11207. "Return current time adjusted for `org-extend-today-until' variable."
  11208. (let* ((ct (org-current-time))
  11209. (dct (decode-time ct))
  11210. (ct1
  11211. (cond
  11212. (org-use-last-clock-out-time-as-effective-time
  11213. (or (org-clock-get-last-clock-out-time) ct))
  11214. ((and org-use-effective-time (< (nth 2 dct) org-extend-today-until))
  11215. (encode-time 0 59 23 (1- (nth 3 dct)) (nth 4 dct) (nth 5 dct)))
  11216. (t ct))))
  11217. ct1))
  11218. (defun org-todo-yesterday (&optional arg)
  11219. "Like `org-todo' but the time of change will be 23:59 of yesterday."
  11220. (interactive "P")
  11221. (if (eq major-mode 'org-agenda-mode)
  11222. (apply 'org-agenda-todo-yesterday arg)
  11223. (let* ((org-use-effective-time t)
  11224. (hour (third (decode-time
  11225. (org-current-time))))
  11226. (org-extend-today-until (1+ hour)))
  11227. (org-todo arg))))
  11228. (defvar org-block-entry-blocking ""
  11229. "First entry preventing the TODO state change.")
  11230. (defun org-cancel-repeater ()
  11231. "Cancel a repeater by setting its numeric value to zero."
  11232. (interactive)
  11233. (save-excursion
  11234. (org-back-to-heading t)
  11235. (let ((bound1 (point))
  11236. (bound0 (save-excursion (outline-next-heading) (point))))
  11237. (when (and (re-search-forward
  11238. (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  11239. org-deadline-time-regexp "\\)\\|\\("
  11240. org-ts-regexp "\\)")
  11241. bound0 t)
  11242. (re-search-backward "[ \t]+\\(?:[.+]\\)?\\+\\([0-9]+\\)[hdwmy]"
  11243. bound1 t))
  11244. (replace-match "0" t nil nil 1)))))
  11245. (defun org-todo (&optional arg)
  11246. "Change the TODO state of an item.
  11247. The state of an item is given by a keyword at the start of the heading,
  11248. like
  11249. *** TODO Write paper
  11250. *** DONE Call mom
  11251. The different keywords are specified in the variable `org-todo-keywords'.
  11252. By default the available states are \"TODO\" and \"DONE\".
  11253. So for this example: when the item starts with TODO, it is changed to DONE.
  11254. When it starts with DONE, the DONE is removed. And when neither TODO nor
  11255. DONE are present, add TODO at the beginning of the heading.
  11256. With \\[universal-argument] prefix arg, use completion to determine the new \
  11257. state.
  11258. With numeric prefix arg, switch to that state.
  11259. With a double \\[universal-argument] prefix, switch to the next set of TODO \
  11260. keywords (nextset).
  11261. With a triple \\[universal-argument] prefix, circumvent any state blocking.
  11262. With a numeric prefix arg of 0, inhibit note taking for the change.
  11263. With a numeric prefix arg of -1, cancel repeater to allow marking as DONE.
  11264. When called through ELisp, arg is also interpreted in the following way:
  11265. `none' -> empty state
  11266. \"\"(empty string) -> switch to empty state
  11267. `done' -> switch to DONE
  11268. `nextset' -> switch to the next set of keywords
  11269. `previousset' -> switch to the previous set of keywords
  11270. \"WAITING\" -> switch to the specified keyword, but only if it
  11271. really is a member of `org-todo-keywords'."
  11272. (interactive "P")
  11273. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  11274. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11275. 'region-start-level 'region))
  11276. org-loop-over-headlines-in-active-region)
  11277. (org-map-entries
  11278. `(org-todo ,arg)
  11279. org-loop-over-headlines-in-active-region
  11280. cl (when (outline-invisible-p) (org-end-of-subtree nil t))))
  11281. (when (equal arg '(16)) (setq arg 'nextset))
  11282. (when (equal arg -1) (org-cancel-repeater) (setq arg nil))
  11283. (let ((org-blocker-hook org-blocker-hook)
  11284. commentp
  11285. case-fold-search)
  11286. (when (equal arg '(64))
  11287. (setq arg nil org-blocker-hook nil))
  11288. (when (and org-blocker-hook
  11289. (or org-inhibit-blocking
  11290. (org-entry-get nil "NOBLOCKING")))
  11291. (setq org-blocker-hook nil))
  11292. (save-excursion
  11293. (catch 'exit
  11294. (org-back-to-heading t)
  11295. (when (org-in-commented-heading-p t)
  11296. (org-toggle-comment)
  11297. (setq commentp t))
  11298. (when (looking-at org-outline-regexp) (goto-char (1- (match-end 0))))
  11299. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|[ \t]*$\\)"))
  11300. (looking-at "\\(?: *\\|[ \t]*$\\)"))
  11301. (let* ((match-data (match-data))
  11302. (startpos (point-at-bol))
  11303. (logging (save-match-data (org-entry-get nil "LOGGING" t t)))
  11304. (org-log-done org-log-done)
  11305. (org-log-repeat org-log-repeat)
  11306. (org-todo-log-states org-todo-log-states)
  11307. (org-inhibit-logging
  11308. (if (equal arg 0)
  11309. (progn (setq arg nil) 'note) org-inhibit-logging))
  11310. (this (match-string 1))
  11311. (hl-pos (match-beginning 0))
  11312. (head (org-get-todo-sequence-head this))
  11313. (ass (assoc head org-todo-kwd-alist))
  11314. (interpret (nth 1 ass))
  11315. (done-word (nth 3 ass))
  11316. (final-done-word (nth 4 ass))
  11317. (org-last-state (or this ""))
  11318. (completion-ignore-case t)
  11319. (member (member this org-todo-keywords-1))
  11320. (tail (cdr member))
  11321. (org-state (cond
  11322. ((and org-todo-key-trigger
  11323. (or (and (equal arg '(4))
  11324. (eq org-use-fast-todo-selection 'prefix))
  11325. (and (not arg) org-use-fast-todo-selection
  11326. (not (eq org-use-fast-todo-selection
  11327. 'prefix)))))
  11328. ;; Use fast selection
  11329. (org-fast-todo-selection))
  11330. ((and (equal arg '(4))
  11331. (or (not org-use-fast-todo-selection)
  11332. (not org-todo-key-trigger)))
  11333. ;; Read a state with completion
  11334. (org-icompleting-read
  11335. "State: " (mapcar 'list org-todo-keywords-1)
  11336. nil t))
  11337. ((eq arg 'right)
  11338. (if this
  11339. (if tail (car tail) nil)
  11340. (car org-todo-keywords-1)))
  11341. ((eq arg 'left)
  11342. (unless (equal member org-todo-keywords-1)
  11343. (if this
  11344. (nth (- (length org-todo-keywords-1)
  11345. (length tail) 2)
  11346. org-todo-keywords-1)
  11347. (org-last org-todo-keywords-1))))
  11348. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  11349. (setq arg nil))) ; hack to fall back to cycling
  11350. (arg
  11351. ;; user or caller requests a specific state
  11352. (cond
  11353. ((equal arg "") nil)
  11354. ((eq arg 'none) nil)
  11355. ((eq arg 'done) (or done-word (car org-done-keywords)))
  11356. ((eq arg 'nextset)
  11357. (or (car (cdr (member head org-todo-heads)))
  11358. (car org-todo-heads)))
  11359. ((eq arg 'previousset)
  11360. (let ((org-todo-heads (reverse org-todo-heads)))
  11361. (or (car (cdr (member head org-todo-heads)))
  11362. (car org-todo-heads))))
  11363. ((car (member arg org-todo-keywords-1)))
  11364. ((stringp arg)
  11365. (user-error "State `%s' not valid in this file" arg))
  11366. ((nth (1- (prefix-numeric-value arg))
  11367. org-todo-keywords-1))))
  11368. ((null member) (or head (car org-todo-keywords-1)))
  11369. ((equal this final-done-word) nil) ;; -> make empty
  11370. ((null tail) nil) ;; -> first entry
  11371. ((memq interpret '(type priority))
  11372. (if (eq this-command last-command)
  11373. (car tail)
  11374. (if (> (length tail) 0)
  11375. (or done-word (car org-done-keywords))
  11376. nil)))
  11377. (t
  11378. (car tail))))
  11379. (org-state (or
  11380. (run-hook-with-args-until-success
  11381. 'org-todo-get-default-hook org-state org-last-state)
  11382. org-state))
  11383. (next (if org-state (concat " " org-state " ") " "))
  11384. (change-plist (list :type 'todo-state-change :from this :to org-state
  11385. :position startpos))
  11386. dolog now-done-p)
  11387. (when org-blocker-hook
  11388. (setq org-last-todo-state-is-todo
  11389. (not (member this org-done-keywords)))
  11390. (unless (save-excursion
  11391. (save-match-data
  11392. (org-with-wide-buffer
  11393. (run-hook-with-args-until-failure
  11394. 'org-blocker-hook change-plist))))
  11395. (if (org-called-interactively-p 'interactive)
  11396. (user-error "TODO state change from %s to %s blocked (by \"%s\")"
  11397. this org-state org-block-entry-blocking)
  11398. ;; fail silently
  11399. (message "TODO state change from %s to %s blocked (by \"%s\")"
  11400. this org-state org-block-entry-blocking)
  11401. (throw 'exit nil))))
  11402. (store-match-data match-data)
  11403. (replace-match next t t)
  11404. (cond ((equal this org-state)
  11405. (message "TODO state was already %s" (org-trim next)))
  11406. ((pos-visible-in-window-p hl-pos)
  11407. (message "TODO state changed to %s" (org-trim next))))
  11408. (unless head
  11409. (setq head (org-get-todo-sequence-head org-state)
  11410. ass (assoc head org-todo-kwd-alist)
  11411. interpret (nth 1 ass)
  11412. done-word (nth 3 ass)
  11413. final-done-word (nth 4 ass)))
  11414. (when (memq arg '(nextset previousset))
  11415. (message "Keyword-Set %d/%d: %s"
  11416. (- (length org-todo-sets) -1
  11417. (length (memq (assoc org-state org-todo-sets) org-todo-sets)))
  11418. (length org-todo-sets)
  11419. (mapconcat 'identity (assoc org-state org-todo-sets) " ")))
  11420. (setq org-last-todo-state-is-todo
  11421. (not (member org-state org-done-keywords)))
  11422. (setq now-done-p (and (member org-state org-done-keywords)
  11423. (not (member this org-done-keywords))))
  11424. (and logging (org-local-logging logging))
  11425. (when (and (or org-todo-log-states org-log-done)
  11426. (not (eq org-inhibit-logging t))
  11427. (not (memq arg '(nextset previousset))))
  11428. ;; we need to look at recording a time and note
  11429. (setq dolog (or (nth 1 (assoc org-state org-todo-log-states))
  11430. (nth 2 (assoc this org-todo-log-states))))
  11431. (when (and (eq dolog 'note) (eq org-inhibit-logging 'note))
  11432. (setq dolog 'time))
  11433. (when (or (and (not org-state) (not org-closed-keep-when-no-todo))
  11434. (and org-state
  11435. (member org-state org-not-done-keywords)
  11436. (not (member this org-not-done-keywords))))
  11437. ;; This is now a todo state and was not one before
  11438. ;; If there was a CLOSED time stamp, get rid of it.
  11439. (org-add-planning-info nil nil 'closed))
  11440. (when (and now-done-p org-log-done)
  11441. ;; It is now done, and it was not done before
  11442. (org-add-planning-info 'closed (org-current-effective-time))
  11443. (when (and (not dolog) (eq 'note org-log-done))
  11444. (org-add-log-setup 'done org-state this 'findpos 'note)))
  11445. (when (and org-state dolog)
  11446. ;; This is a non-nil state, and we need to log it
  11447. (org-add-log-setup 'state org-state this 'findpos dolog)))
  11448. ;; Fixup tag positioning
  11449. (org-todo-trigger-tag-changes org-state)
  11450. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  11451. (when org-provide-todo-statistics
  11452. (org-update-parent-todo-statistics))
  11453. (run-hooks 'org-after-todo-state-change-hook)
  11454. (when (and arg (not (member org-state org-done-keywords)))
  11455. (setq head (org-get-todo-sequence-head org-state)))
  11456. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  11457. ;; Do we need to trigger a repeat?
  11458. (when now-done-p
  11459. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  11460. ;; This is for the agenda, take a snapshot of the headline.
  11461. (save-match-data
  11462. (setq org-agenda-headline-snapshot-before-repeat
  11463. (org-get-heading))))
  11464. (org-auto-repeat-maybe org-state))
  11465. ;; Fixup cursor location if close to the keyword
  11466. (when (and (outline-on-heading-p)
  11467. (not (bolp))
  11468. (save-excursion (beginning-of-line 1)
  11469. (looking-at org-todo-line-regexp))
  11470. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  11471. (goto-char (or (match-end 2) (match-end 1)))
  11472. (and (looking-at " ") (just-one-space)))
  11473. (when org-trigger-hook
  11474. (save-excursion
  11475. (run-hook-with-args 'org-trigger-hook change-plist)))
  11476. (when commentp (org-toggle-comment))))))))
  11477. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  11478. "Block turning an entry into a TODO, using the hierarchy.
  11479. This checks whether the current task should be blocked from state
  11480. changes. Such blocking occurs when:
  11481. 1. The task has children which are not all in a completed state.
  11482. 2. A task has a parent with the property :ORDERED:, and there
  11483. are siblings prior to the current task with incomplete
  11484. status.
  11485. 3. The parent of the task is blocked because it has siblings that should
  11486. be done first, or is child of a block grandparent TODO entry."
  11487. (if (not org-enforce-todo-dependencies)
  11488. t ; if locally turned off don't block
  11489. (catch 'dont-block
  11490. ;; If this is not a todo state change, or if this entry is already DONE,
  11491. ;; do not block
  11492. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  11493. (member (plist-get change-plist :from)
  11494. (cons 'done org-done-keywords))
  11495. (member (plist-get change-plist :to)
  11496. (cons 'todo org-not-done-keywords))
  11497. (not (plist-get change-plist :to)))
  11498. (throw 'dont-block t))
  11499. ;; If this task has children, and any are undone, it's blocked
  11500. (save-excursion
  11501. (org-back-to-heading t)
  11502. (let ((this-level (funcall outline-level)))
  11503. (outline-next-heading)
  11504. (let ((child-level (funcall outline-level)))
  11505. (while (and (not (eobp))
  11506. (> child-level this-level))
  11507. ;; this todo has children, check whether they are all
  11508. ;; completed
  11509. (when (and (not (org-entry-is-done-p))
  11510. (org-entry-is-todo-p))
  11511. (setq org-block-entry-blocking (org-get-heading))
  11512. (throw 'dont-block nil))
  11513. (outline-next-heading)
  11514. (setq child-level (funcall outline-level))))))
  11515. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  11516. ;; any previous siblings are undone, it's blocked
  11517. (save-excursion
  11518. (org-back-to-heading t)
  11519. (let* ((pos (point))
  11520. (parent-pos (and (org-up-heading-safe) (point))))
  11521. (unless parent-pos (throw 'dont-block t)) ; no parent
  11522. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  11523. (forward-line 1)
  11524. (re-search-forward org-not-done-heading-regexp pos t))
  11525. (setq org-block-entry-blocking (match-string 0))
  11526. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  11527. ;; Search further up the hierarchy, to see if an ancestor is blocked
  11528. (while t
  11529. (goto-char parent-pos)
  11530. (unless (looking-at org-not-done-heading-regexp)
  11531. (throw 'dont-block t)) ; do not block, parent is not a TODO
  11532. (setq pos (point))
  11533. (setq parent-pos (and (org-up-heading-safe) (point)))
  11534. (unless parent-pos (throw 'dont-block t)) ; no parent
  11535. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  11536. (forward-line 1)
  11537. (re-search-forward org-not-done-heading-regexp pos t)
  11538. (setq org-block-entry-blocking (org-get-heading)))
  11539. (throw 'dont-block nil)))))))) ; block, older sibling not done.
  11540. (defcustom org-track-ordered-property-with-tag nil
  11541. "Should the ORDERED property also be shown as a tag?
  11542. The ORDERED property decides if an entry should require subtasks to be
  11543. completed in sequence. Since a property is not very visible, setting
  11544. this option means that toggling the ORDERED property with the command
  11545. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  11546. not relevant for the behavior, but it makes things more visible.
  11547. Note that toggling the tag with tags commands will not change the property
  11548. and therefore not influence behavior!
  11549. This can be t, meaning the tag ORDERED should be used, It can also be a
  11550. string to select a different tag for this task."
  11551. :group 'org-todo
  11552. :type '(choice
  11553. (const :tag "No tracking" nil)
  11554. (const :tag "Track with ORDERED tag" t)
  11555. (string :tag "Use other tag")))
  11556. (defun org-toggle-ordered-property ()
  11557. "Toggle the ORDERED property of the current entry.
  11558. For better visibility, you can track the value of this property with a tag.
  11559. See variable `org-track-ordered-property-with-tag'."
  11560. (interactive)
  11561. (let* ((t1 org-track-ordered-property-with-tag)
  11562. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  11563. (save-excursion
  11564. (org-back-to-heading)
  11565. (if (org-entry-get nil "ORDERED")
  11566. (progn
  11567. (org-delete-property "ORDERED")
  11568. (and tag (org-toggle-tag tag 'off))
  11569. (message "Subtasks can be completed in arbitrary order"))
  11570. (org-entry-put nil "ORDERED" "t")
  11571. (and tag (org-toggle-tag tag 'on))
  11572. (message "Subtasks must be completed in sequence")))))
  11573. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  11574. (defun org-block-todo-from-checkboxes (change-plist)
  11575. "Block turning an entry into a TODO, using checkboxes.
  11576. This checks whether the current task should be blocked from state
  11577. changes because there are unchecked boxes in this entry."
  11578. (if (not org-enforce-todo-checkbox-dependencies)
  11579. t ; if locally turned off don't block
  11580. (catch 'dont-block
  11581. ;; If this is not a todo state change, or if this entry is already DONE,
  11582. ;; do not block
  11583. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  11584. (member (plist-get change-plist :from)
  11585. (cons 'done org-done-keywords))
  11586. (member (plist-get change-plist :to)
  11587. (cons 'todo org-not-done-keywords))
  11588. (not (plist-get change-plist :to)))
  11589. (throw 'dont-block t))
  11590. ;; If this task has checkboxes that are not checked, it's blocked
  11591. (save-excursion
  11592. (org-back-to-heading t)
  11593. (let ((beg (point)) end)
  11594. (outline-next-heading)
  11595. (setq end (point))
  11596. (goto-char beg)
  11597. (when (org-list-search-forward
  11598. (concat (org-item-beginning-re)
  11599. "\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?"
  11600. "\\[[- ]\\]")
  11601. end t)
  11602. (when (boundp 'org-blocked-by-checkboxes)
  11603. (setq org-blocked-by-checkboxes t))
  11604. (throw 'dont-block nil))))
  11605. t))) ; do not block
  11606. (defun org-entry-blocked-p ()
  11607. "Non-nil if entry at point is blocked."
  11608. (and (not (org-entry-get nil "NOBLOCKING"))
  11609. (member (org-entry-get nil "TODO") org-not-done-keywords)
  11610. (not (run-hook-with-args-until-failure
  11611. 'org-blocker-hook
  11612. (list :type 'todo-state-change
  11613. :position (point)
  11614. :from 'todo
  11615. :to 'done)))))
  11616. (defun org-update-statistics-cookies (all)
  11617. "Update the statistics cookie, either from TODO or from checkboxes.
  11618. This should be called with the cursor in a line with a statistics cookie."
  11619. (interactive "P")
  11620. (if all
  11621. (progn
  11622. (org-update-checkbox-count 'all)
  11623. (org-map-entries 'org-update-parent-todo-statistics))
  11624. (if (not (org-at-heading-p))
  11625. (org-update-checkbox-count)
  11626. (let ((pos (point-marker))
  11627. end l1 l2)
  11628. (ignore-errors (org-back-to-heading t))
  11629. (if (not (org-at-heading-p))
  11630. (org-update-checkbox-count)
  11631. (setq l1 (org-outline-level))
  11632. (setq end (save-excursion
  11633. (outline-next-heading)
  11634. (when (org-at-heading-p) (setq l2 (org-outline-level)))
  11635. (point)))
  11636. (if (and (save-excursion
  11637. (re-search-forward
  11638. "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) \\[[- X]\\]" end t))
  11639. (not (save-excursion (re-search-forward
  11640. ":COOKIE_DATA:.*\\<todo\\>" end t))))
  11641. (org-update-checkbox-count)
  11642. (if (and l2 (> l2 l1))
  11643. (progn
  11644. (goto-char end)
  11645. (org-update-parent-todo-statistics))
  11646. (goto-char pos)
  11647. (beginning-of-line 1)
  11648. (while (re-search-forward
  11649. "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)"
  11650. (point-at-eol) t)
  11651. (replace-match (if (match-end 2) "[100%]" "[0/0]") t t)))))
  11652. (goto-char pos)
  11653. (move-marker pos nil)))))
  11654. (defvar org-entry-property-inherited-from) ;; defined below
  11655. (defun org-update-parent-todo-statistics ()
  11656. "Update any statistics cookie in the parent of the current headline.
  11657. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  11658. the entire subtree and this will travel up the hierarchy and update
  11659. statistics everywhere."
  11660. (let* ((prop (save-excursion (org-up-heading-safe)
  11661. (org-entry-get nil "COOKIE_DATA" 'inherit)))
  11662. (recursive (or (not org-hierarchical-todo-statistics)
  11663. (and prop (string-match "\\<recursive\\>" prop))))
  11664. (lim (or (and prop (marker-position org-entry-property-inherited-from))
  11665. 0))
  11666. (first t)
  11667. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  11668. level ltoggle l1 new ndel
  11669. (cnt-all 0) (cnt-done 0) is-percent kwd
  11670. checkbox-beg ov ovs ove cookie-present)
  11671. (catch 'exit
  11672. (save-excursion
  11673. (beginning-of-line 1)
  11674. (setq ltoggle (funcall outline-level))
  11675. ;; Three situations are to consider:
  11676. ;; 1. if `org-hierarchical-todo-statistics' is nil, repeat up
  11677. ;; to the top-level ancestor on the headline;
  11678. ;; 2. If parent has "recursive" property, repeat up to the
  11679. ;; headline setting that property, taking inheritance into
  11680. ;; account;
  11681. ;; 3. Else, move up to direct parent and proceed only once.
  11682. (while (and (setq level (org-up-heading-safe))
  11683. (or recursive first)
  11684. (>= (point) lim))
  11685. (setq first nil cookie-present nil)
  11686. (unless (and level
  11687. (not (string-match
  11688. "\\<checkbox\\>"
  11689. (downcase (or (org-entry-get nil "COOKIE_DATA")
  11690. "")))))
  11691. (throw 'exit nil))
  11692. (while (re-search-forward box-re (point-at-eol) t)
  11693. (setq cnt-all 0 cnt-done 0 cookie-present t)
  11694. (setq is-percent (match-end 2) checkbox-beg (match-beginning 0))
  11695. (save-match-data
  11696. (unless (outline-next-heading) (throw 'exit nil))
  11697. (while (and (looking-at org-complex-heading-regexp)
  11698. (> (setq l1 (length (match-string 1))) level))
  11699. (setq kwd (and (or recursive (= l1 ltoggle))
  11700. (match-string 2)))
  11701. (if (or (eq org-provide-todo-statistics 'all-headlines)
  11702. (and (eq org-provide-todo-statistics t)
  11703. (or (member kwd org-done-keywords)))
  11704. (and (listp org-provide-todo-statistics)
  11705. (stringp (car org-provide-todo-statistics))
  11706. (or (member kwd org-provide-todo-statistics)
  11707. (member kwd org-done-keywords)))
  11708. (and (listp org-provide-todo-statistics)
  11709. (listp (car org-provide-todo-statistics))
  11710. (or (member kwd (car org-provide-todo-statistics))
  11711. (and (member kwd org-done-keywords)
  11712. (member kwd (cadr org-provide-todo-statistics))))))
  11713. (setq cnt-all (1+ cnt-all))
  11714. (and (eq org-provide-todo-statistics t)
  11715. kwd
  11716. (setq cnt-all (1+ cnt-all))))
  11717. (when (or (and (member org-provide-todo-statistics '(t all-headlines))
  11718. (member kwd org-done-keywords))
  11719. (and (listp org-provide-todo-statistics)
  11720. (listp (car org-provide-todo-statistics))
  11721. (member kwd org-done-keywords)
  11722. (member kwd (cadr org-provide-todo-statistics)))
  11723. (and (listp org-provide-todo-statistics)
  11724. (stringp (car org-provide-todo-statistics))
  11725. (member kwd org-done-keywords)))
  11726. (setq cnt-done (1+ cnt-done)))
  11727. (outline-next-heading)))
  11728. (setq new
  11729. (if is-percent
  11730. (format "[%d%%]" (floor (* 100.0 cnt-done)
  11731. (max 1 cnt-all)))
  11732. (format "[%d/%d]" cnt-done cnt-all))
  11733. ndel (- (match-end 0) checkbox-beg))
  11734. ;; handle overlays when updating cookie from column view
  11735. (when (setq ov (car (overlays-at checkbox-beg)))
  11736. (setq ovs (overlay-start ov) ove (overlay-end ov))
  11737. (delete-overlay ov))
  11738. (goto-char checkbox-beg)
  11739. (insert new)
  11740. (delete-region (point) (+ (point) ndel))
  11741. (when org-auto-align-tags (org-fix-tags-on-the-fly))
  11742. (when ov (move-overlay ov ovs ove)))
  11743. (when cookie-present
  11744. (run-hook-with-args 'org-after-todo-statistics-hook
  11745. cnt-done (- cnt-all cnt-done))))))
  11746. (run-hooks 'org-todo-statistics-hook)))
  11747. (defvar org-after-todo-statistics-hook nil
  11748. "Hook that is called after a TODO statistics cookie has been updated.
  11749. Each function is called with two arguments: the number of not-done entries
  11750. and the number of done entries.
  11751. For example, the following function, when added to this hook, will switch
  11752. an entry to DONE when all children are done, and back to TODO when new
  11753. entries are set to a TODO status. Note that this hook is only called
  11754. when there is a statistics cookie in the headline!
  11755. (defun org-summary-todo (n-done n-not-done)
  11756. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  11757. (let (org-log-done org-log-states) ; turn off logging
  11758. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  11759. ")
  11760. (defvar org-todo-statistics-hook nil
  11761. "Hook that is run whenever Org thinks TODO statistics should be updated.
  11762. This hook runs even if there is no statistics cookie present, in which case
  11763. `org-after-todo-statistics-hook' would not run.")
  11764. (defun org-todo-trigger-tag-changes (state)
  11765. "Apply the changes defined in `org-todo-state-tags-triggers'."
  11766. (let ((l org-todo-state-tags-triggers)
  11767. changes)
  11768. (when (or (not state) (equal state ""))
  11769. (setq changes (append changes (cdr (assoc "" l)))))
  11770. (when (and (stringp state) (> (length state) 0))
  11771. (setq changes (append changes (cdr (assoc state l)))))
  11772. (when (member state org-not-done-keywords)
  11773. (setq changes (append changes (cdr (assoc 'todo l)))))
  11774. (when (member state org-done-keywords)
  11775. (setq changes (append changes (cdr (assoc 'done l)))))
  11776. (dolist (c changes)
  11777. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  11778. (defun org-local-logging (value)
  11779. "Get logging settings from a property VALUE."
  11780. ;; Directly set the variables, they are already local.
  11781. (setq org-log-done nil
  11782. org-log-repeat nil
  11783. org-todo-log-states nil)
  11784. (dolist (w (org-split-string value))
  11785. (let (a)
  11786. (cond
  11787. ((setq a (assoc w org-startup-options))
  11788. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  11789. (set (nth 1 a) (nth 2 a))))
  11790. ((setq a (org-extract-log-state-settings w))
  11791. (and (member (car a) org-todo-keywords-1)
  11792. (push a org-todo-log-states)))))))
  11793. (defun org-get-todo-sequence-head (kwd)
  11794. "Return the head of the TODO sequence to which KWD belongs.
  11795. If KWD is not set, check if there is a text property remembering the
  11796. right sequence."
  11797. (let (p)
  11798. (cond
  11799. ((not kwd)
  11800. (or (get-text-property (point-at-bol) 'org-todo-head)
  11801. (progn
  11802. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  11803. nil (point-at-eol)))
  11804. (get-text-property p 'org-todo-head))))
  11805. ((not (member kwd org-todo-keywords-1))
  11806. (car org-todo-keywords-1))
  11807. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  11808. (defun org-fast-todo-selection ()
  11809. "Fast TODO keyword selection with single keys.
  11810. Returns the new TODO keyword, or nil if no state change should occur."
  11811. (let* ((fulltable org-todo-key-alist)
  11812. (done-keywords org-done-keywords) ;; needed for the faces.
  11813. (maxlen (apply 'max (mapcar
  11814. (lambda (x)
  11815. (if (stringp (car x)) (string-width (car x)) 0))
  11816. fulltable)))
  11817. (expert nil)
  11818. (fwidth (+ maxlen 3 1 3))
  11819. (ncol (/ (- (window-width) 4) fwidth))
  11820. tg cnt e c tbl
  11821. groups ingroup)
  11822. (save-excursion
  11823. (save-window-excursion
  11824. (if expert
  11825. (set-buffer (get-buffer-create " *Org todo*"))
  11826. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  11827. (erase-buffer)
  11828. (setq-local org-done-keywords done-keywords)
  11829. (setq tbl fulltable cnt 0)
  11830. (while (setq e (pop tbl))
  11831. (cond
  11832. ((equal e '(:startgroup))
  11833. (push '() groups) (setq ingroup t)
  11834. (unless (= cnt 0)
  11835. (setq cnt 0)
  11836. (insert "\n"))
  11837. (insert "{ "))
  11838. ((equal e '(:endgroup))
  11839. (setq ingroup nil cnt 0)
  11840. (insert "}\n"))
  11841. ((equal e '(:newline))
  11842. (unless (= cnt 0)
  11843. (setq cnt 0)
  11844. (insert "\n")
  11845. (setq e (car tbl))
  11846. (while (equal (car tbl) '(:newline))
  11847. (insert "\n")
  11848. (setq tbl (cdr tbl)))))
  11849. (t
  11850. (setq tg (car e) c (cdr e))
  11851. (when ingroup (push tg (car groups)))
  11852. (setq tg (org-add-props tg nil 'face
  11853. (org-get-todo-face tg)))
  11854. (when (and (= cnt 0) (not ingroup)) (insert " "))
  11855. (insert "[" c "] " tg (make-string
  11856. (- fwidth 4 (length tg)) ?\ ))
  11857. (when (= (setq cnt (1+ cnt)) ncol)
  11858. (insert "\n")
  11859. (when ingroup (insert " "))
  11860. (setq cnt 0)))))
  11861. (insert "\n")
  11862. (goto-char (point-min))
  11863. (unless expert (org-fit-window-to-buffer))
  11864. (message "[a-z..]:Set [SPC]:clear")
  11865. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  11866. (cond
  11867. ((or (= c ?\C-g)
  11868. (and (= c ?q) (not (rassoc c fulltable))))
  11869. (setq quit-flag t))
  11870. ((= c ?\ ) nil)
  11871. ((setq e (rassoc c fulltable) tg (car e))
  11872. tg)
  11873. (t (setq quit-flag t)))))))
  11874. (defun org-entry-is-todo-p ()
  11875. (member (org-get-todo-state) org-not-done-keywords))
  11876. (defun org-entry-is-done-p ()
  11877. (member (org-get-todo-state) org-done-keywords))
  11878. (defun org-get-todo-state ()
  11879. "Return the TODO keyword of the current subtree."
  11880. (save-excursion
  11881. (org-back-to-heading t)
  11882. (and (looking-at org-todo-line-regexp)
  11883. (match-end 2)
  11884. (match-string 2))))
  11885. (defun org-at-date-range-p (&optional inactive-ok)
  11886. "Non-nil if point is inside a date range.
  11887. When optional argument INACTIVE-OK is non-nil, also consider
  11888. inactive time ranges.
  11889. When this function returns a non-nil value, match data is set
  11890. according to `org-tr-regexp-both' or `org-tr-regexp', depending
  11891. on INACTIVE-OK."
  11892. (interactive)
  11893. (save-excursion
  11894. (catch 'exit
  11895. (let ((pos (point)))
  11896. (skip-chars-backward "^[<\r\n")
  11897. (skip-chars-backward "<[")
  11898. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  11899. (>= (match-end 0) pos)
  11900. (throw 'exit t))
  11901. (skip-chars-backward "^<[\r\n")
  11902. (skip-chars-backward "<[")
  11903. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  11904. (>= (match-end 0) pos)
  11905. (throw 'exit t)))
  11906. nil)))
  11907. (defun org-get-repeat (&optional tagline)
  11908. "Check if there is a deadline/schedule with repeater in this entry."
  11909. (save-match-data
  11910. (save-excursion
  11911. (org-back-to-heading t)
  11912. (and (re-search-forward (if tagline
  11913. (concat tagline "\\s-*" org-repeat-re)
  11914. org-repeat-re)
  11915. (org-entry-end-position) t)
  11916. (match-string-no-properties 1)))))
  11917. (defvar org-last-changed-timestamp)
  11918. (defvar org-last-inserted-timestamp)
  11919. (defvar org-log-post-message)
  11920. (defvar org-log-note-purpose)
  11921. (defvar org-log-note-how nil)
  11922. (defvar org-log-note-extra)
  11923. (defun org-auto-repeat-maybe (done-word)
  11924. "Check if the current headline contains a repeated deadline/schedule.
  11925. If yes, set TODO state back to what it was and change the base date
  11926. of repeating deadline/scheduled time stamps to new date.
  11927. This function is run automatically after each state change to a DONE state."
  11928. ;; last-state is dynamically scoped into this function
  11929. (let* ((repeat (org-get-repeat))
  11930. (aa (assoc org-last-state org-todo-kwd-alist))
  11931. (interpret (nth 1 aa))
  11932. (head (nth 2 aa))
  11933. (whata '(("h" . hour) ("d" . day) ("m" . month) ("y" . year)))
  11934. (msg "Entry repeats: ")
  11935. (org-log-done nil)
  11936. (org-todo-log-states nil)
  11937. re type n what ts time to-state)
  11938. (when (and repeat (not (zerop (string-to-number (substring repeat 1)))))
  11939. (when (eq org-log-repeat t) (setq org-log-repeat 'state))
  11940. (setq to-state (or (org-entry-get nil "REPEAT_TO_STATE")
  11941. org-todo-repeat-to-state))
  11942. (unless (and to-state (member to-state org-todo-keywords-1))
  11943. (setq to-state (if (eq interpret 'type) org-last-state head)))
  11944. (org-todo to-state)
  11945. (when (or org-log-repeat (org-entry-get nil "CLOCK"))
  11946. (org-entry-put nil "LAST_REPEAT" (format-time-string
  11947. (org-time-stamp-format t t))))
  11948. (when org-log-repeat
  11949. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  11950. (memq 'org-add-log-note post-command-hook))
  11951. ;; OK, we are already setup for some record
  11952. (when (eq org-log-repeat 'note)
  11953. ;; make sure we take a note, not only a time stamp
  11954. (setq org-log-note-how 'note))
  11955. ;; Set up for taking a record
  11956. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  11957. org-last-state
  11958. 'findpos org-log-repeat)))
  11959. (org-back-to-heading t)
  11960. (org-add-planning-info nil nil 'closed)
  11961. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  11962. org-deadline-time-regexp "\\)\\|\\("
  11963. org-ts-regexp "\\)"))
  11964. (while (re-search-forward
  11965. re (save-excursion (outline-next-heading) (point)) t)
  11966. (setq type (if (match-end 1) org-scheduled-string
  11967. (if (match-end 3) org-deadline-string "Plain:"))
  11968. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  11969. (if (not (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts))
  11970. (org-remove-timestamp-with-keyword org-scheduled-string)
  11971. (setq n (string-to-number (match-string 2 ts))
  11972. what (match-string 3 ts))
  11973. (when (equal what "w") (setq n (* n 7) what "d"))
  11974. (when (and (equal what "h")
  11975. (not (string-match "[0-9]\\{1,2\\}:[0-9]\\{2\\}" ts)))
  11976. (user-error
  11977. "Cannot repeat in Repeat in %d hour(s) because no hour has been set" n))
  11978. ;; Preparation, see if we need to modify the start date for the change
  11979. (when (match-end 1)
  11980. (setq time (save-match-data (org-time-string-to-time ts)))
  11981. (cond
  11982. ((equal (match-string 1 ts) ".")
  11983. ;; Shift starting date to today
  11984. (org-timestamp-change
  11985. (- (org-today) (time-to-days time))
  11986. 'day))
  11987. ((equal (match-string 1 ts) "+")
  11988. (let ((nshiftmax 10) (nshift 0))
  11989. (while (or (= nshift 0)
  11990. (<= (time-to-days time)
  11991. (time-to-days (current-time))))
  11992. (when (= (incf nshift) nshiftmax)
  11993. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  11994. (user-error "Abort")))
  11995. (org-timestamp-change n (cdr (assoc what whata)))
  11996. (org-at-timestamp-p t)
  11997. (setq ts (match-string 1))
  11998. (setq time (save-match-data (org-time-string-to-time ts)))))
  11999. (org-timestamp-change (- n) (cdr (assoc what whata)))
  12000. ;; rematch, so that we have everything in place for the real shift
  12001. (org-at-timestamp-p t)
  12002. (setq ts (match-string 1))
  12003. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts))))
  12004. (save-excursion
  12005. (org-timestamp-change n (cdr (assoc what whata)) nil t))
  12006. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  12007. (setq org-log-post-message msg)
  12008. (message "%s" msg))))
  12009. (defun org-show-todo-tree (arg)
  12010. "Make a compact tree which shows all headlines marked with TODO.
  12011. The tree will show the lines where the regexp matches, and all higher
  12012. headlines above the match.
  12013. With a \\[universal-argument] prefix, prompt for a regexp to match.
  12014. With a numeric prefix N, construct a sparse tree for the Nth element
  12015. of `org-todo-keywords-1'."
  12016. (interactive "P")
  12017. (let ((case-fold-search nil)
  12018. (kwd-re
  12019. (cond ((null arg) org-not-done-regexp)
  12020. ((equal arg '(4))
  12021. (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
  12022. (mapcar 'list org-todo-keywords-1))))
  12023. (concat "\\("
  12024. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  12025. "\\)\\>")))
  12026. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  12027. (regexp-quote (nth (1- (prefix-numeric-value arg))
  12028. org-todo-keywords-1)))
  12029. (t (user-error "Invalid prefix argument: %s" arg)))))
  12030. (message "%d TODO entries found"
  12031. (org-occur (concat "^" org-outline-regexp " *" kwd-re )))))
  12032. (defun org-deadline (arg &optional time)
  12033. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  12034. With one universal prefix argument, remove any deadline from the item.
  12035. With two universal prefix arguments, prompt for a warning delay.
  12036. With argument TIME, set the deadline at the corresponding date. TIME
  12037. can either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  12038. (interactive "P")
  12039. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  12040. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  12041. 'region-start-level 'region))
  12042. org-loop-over-headlines-in-active-region)
  12043. (org-map-entries
  12044. `(org-deadline ',arg ,time)
  12045. org-loop-over-headlines-in-active-region
  12046. cl (when (outline-invisible-p) (org-end-of-subtree nil t))))
  12047. (let* ((old-date (org-entry-get nil "DEADLINE"))
  12048. (old-date-time (when old-date (org-time-string-to-time old-date)))
  12049. (repeater (and old-date
  12050. (string-match
  12051. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  12052. old-date)
  12053. (match-string 1 old-date))))
  12054. (cond
  12055. ((equal arg '(4))
  12056. (when (and old-date org-log-redeadline)
  12057. (org-add-log-setup 'deldeadline nil old-date 'findpos
  12058. org-log-redeadline))
  12059. (org-remove-timestamp-with-keyword org-deadline-string)
  12060. (message "Item no longer has a deadline."))
  12061. ((equal arg '(16))
  12062. (save-excursion
  12063. (org-back-to-heading t)
  12064. (if (re-search-forward
  12065. org-deadline-time-regexp
  12066. (save-excursion (outline-next-heading) (point)) t)
  12067. (let* ((rpl0 (match-string 1))
  12068. (rpl (replace-regexp-in-string " -[0-9]+[hdwmy]" "" rpl0)))
  12069. (replace-match
  12070. (concat org-deadline-string
  12071. " <" rpl
  12072. (format " -%dd"
  12073. (abs
  12074. (- (time-to-days
  12075. (save-match-data
  12076. (org-read-date nil t nil "Warn starting from" old-date-time)))
  12077. (time-to-days old-date-time))))
  12078. ">") t t))
  12079. (user-error "No deadline information to update"))))
  12080. (t
  12081. (org-add-planning-info 'deadline time 'closed)
  12082. (when (and old-date
  12083. org-log-redeadline
  12084. (not (equal old-date org-last-inserted-timestamp)))
  12085. (org-add-log-setup
  12086. 'redeadline org-last-inserted-timestamp old-date 'findpos
  12087. org-log-redeadline))
  12088. (when repeater
  12089. (save-excursion
  12090. (org-back-to-heading t)
  12091. (when (re-search-forward (concat org-deadline-string " "
  12092. org-last-inserted-timestamp)
  12093. (save-excursion
  12094. (outline-next-heading) (point)) t)
  12095. (goto-char (1- (match-end 0)))
  12096. (insert " " repeater)
  12097. (setq org-last-inserted-timestamp
  12098. (concat (substring org-last-inserted-timestamp 0 -1)
  12099. " " repeater
  12100. (substring org-last-inserted-timestamp -1))))))
  12101. (message "Deadline on %s" org-last-inserted-timestamp))))))
  12102. (defun org-schedule (arg &optional time)
  12103. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  12104. With one universal prefix argument, remove any scheduling date from the item.
  12105. With two universal prefix arguments, prompt for a delay cookie.
  12106. With argument TIME, scheduled at the corresponding date. TIME can
  12107. either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  12108. (interactive "P")
  12109. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  12110. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  12111. 'region-start-level 'region))
  12112. org-loop-over-headlines-in-active-region)
  12113. (org-map-entries
  12114. `(org-schedule ',arg ,time)
  12115. org-loop-over-headlines-in-active-region
  12116. cl (when (outline-invisible-p) (org-end-of-subtree nil t))))
  12117. (let* ((old-date (org-entry-get nil "SCHEDULED"))
  12118. (old-date-time (when old-date (org-time-string-to-time old-date)))
  12119. (repeater (and old-date
  12120. (string-match
  12121. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  12122. old-date)
  12123. (match-string 1 old-date))))
  12124. (cond
  12125. ((equal arg '(4))
  12126. (progn
  12127. (when (and old-date org-log-reschedule)
  12128. (org-add-log-setup 'delschedule nil old-date 'findpos
  12129. org-log-reschedule))
  12130. (org-remove-timestamp-with-keyword org-scheduled-string)
  12131. (message "Item is no longer scheduled.")))
  12132. ((equal arg '(16))
  12133. (save-excursion
  12134. (org-back-to-heading t)
  12135. (if (re-search-forward
  12136. org-scheduled-time-regexp
  12137. (save-excursion (outline-next-heading) (point)) t)
  12138. (let* ((rpl0 (match-string 1))
  12139. (rpl (replace-regexp-in-string " -[0-9]+[hdwmy]" "" rpl0)))
  12140. (replace-match
  12141. (concat org-scheduled-string
  12142. " <" rpl
  12143. (format " -%dd"
  12144. (abs
  12145. (- (time-to-days
  12146. (save-match-data
  12147. (org-read-date nil t nil "Delay until" old-date-time)))
  12148. (time-to-days old-date-time))))
  12149. ">") t t))
  12150. (user-error "No scheduled information to update"))))
  12151. (t
  12152. (org-add-planning-info 'scheduled time 'closed)
  12153. (when (and old-date
  12154. org-log-reschedule
  12155. (not (equal old-date org-last-inserted-timestamp)))
  12156. (org-add-log-setup
  12157. 'reschedule org-last-inserted-timestamp old-date 'findpos
  12158. org-log-reschedule))
  12159. (when repeater
  12160. (save-excursion
  12161. (org-back-to-heading t)
  12162. (when (re-search-forward (concat org-scheduled-string " "
  12163. org-last-inserted-timestamp)
  12164. (save-excursion
  12165. (outline-next-heading) (point)) t)
  12166. (goto-char (1- (match-end 0)))
  12167. (insert " " repeater)
  12168. (setq org-last-inserted-timestamp
  12169. (concat (substring org-last-inserted-timestamp 0 -1)
  12170. " " repeater
  12171. (substring org-last-inserted-timestamp -1))))))
  12172. (message "Scheduled to %s" org-last-inserted-timestamp))))))
  12173. (defun org-get-scheduled-time (pom &optional inherit)
  12174. "Get the scheduled time as a time tuple, of a format suitable
  12175. for calling org-schedule with, or if there is no scheduling,
  12176. returns nil."
  12177. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  12178. (when time
  12179. (apply 'encode-time (org-parse-time-string time)))))
  12180. (defun org-get-deadline-time (pom &optional inherit)
  12181. "Get the deadline as a time tuple, of a format suitable for
  12182. calling org-deadline with, or if there is no scheduling, returns
  12183. nil."
  12184. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  12185. (when time
  12186. (apply 'encode-time (org-parse-time-string time)))))
  12187. (defun org-remove-timestamp-with-keyword (keyword)
  12188. "Remove all time stamps with KEYWORD in the current entry."
  12189. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  12190. beg)
  12191. (save-excursion
  12192. (org-back-to-heading t)
  12193. (setq beg (point))
  12194. (outline-next-heading)
  12195. (while (re-search-backward re beg t)
  12196. (replace-match "")
  12197. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  12198. (equal (char-before) ?\ ))
  12199. (backward-delete-char 1)
  12200. (when (string-match "^[ \t]*$" (buffer-substring
  12201. (point-at-bol) (point-at-eol)))
  12202. (delete-region (point-at-bol)
  12203. (min (point-max) (1+ (point-at-eol))))))))))
  12204. (defvar org-time-was-given) ; dynamically scoped parameter
  12205. (defvar org-end-time-was-given) ; dynamically scoped parameter
  12206. (defun org-at-planning-p ()
  12207. "Non-nil when point is on a planning info line."
  12208. ;; This is as accurate and faster than `org-element-at-point' since
  12209. ;; planning info location is fixed in the section.
  12210. (org-with-wide-buffer
  12211. (beginning-of-line)
  12212. (and (org-looking-at-p org-planning-line-re)
  12213. (eq (point)
  12214. (ignore-errors
  12215. (if (and (featurep 'org-inlinetask) (org-inlinetask-in-task-p))
  12216. (org-back-to-heading t)
  12217. (org-with-limited-levels (org-back-to-heading t)))
  12218. (line-beginning-position 2))))))
  12219. (defun org-add-planning-info (what &optional time &rest remove)
  12220. "Insert new timestamp with keyword in the planning line.
  12221. WHAT indicates what kind of time stamp to add. It is a symbol
  12222. among `closed', `deadline', `scheduled' and nil. TIME indicates
  12223. the time to use. If none is given, the user is prompted for
  12224. a date. REMOVE indicates what kind of entries to remove. An old
  12225. WHAT entry will also be removed."
  12226. (let (org-time-was-given org-end-time-was-given default-time default-input)
  12227. (catch 'exit
  12228. (when (and (memq what '(scheduled deadline))
  12229. (or (not time)
  12230. (and (stringp time)
  12231. (string-match "^[-+]+[0-9]" time))))
  12232. ;; Try to get a default date/time from existing timestamp
  12233. (save-excursion
  12234. (org-back-to-heading t)
  12235. (let ((end (save-excursion (outline-next-heading) (point))) ts)
  12236. (when (re-search-forward (if (eq what 'scheduled)
  12237. org-scheduled-time-regexp
  12238. org-deadline-time-regexp)
  12239. end t)
  12240. (setq ts (match-string 1)
  12241. default-time (apply 'encode-time (org-parse-time-string ts))
  12242. default-input (and ts (org-get-compact-tod ts)))))))
  12243. (when what
  12244. (setq time
  12245. (if (stringp time)
  12246. ;; This is a string (relative or absolute), set
  12247. ;; proper date.
  12248. (apply #'encode-time
  12249. (org-read-date-analyze
  12250. time default-time (decode-time default-time)))
  12251. ;; If necessary, get the time from the user
  12252. (or time (org-read-date nil 'to-time nil nil
  12253. default-time default-input)))))
  12254. (org-with-wide-buffer
  12255. (org-back-to-heading t)
  12256. (forward-line)
  12257. (unless (bolp) (insert "\n"))
  12258. (cond ((org-looking-at-p org-planning-line-re)
  12259. ;; Move to current indentation.
  12260. (skip-chars-forward " \t")
  12261. ;; Check if we have to remove something.
  12262. (dolist (type (if what (cons what remove) remove))
  12263. (save-excursion
  12264. (when (re-search-forward
  12265. (case type
  12266. (closed org-closed-time-regexp)
  12267. (deadline org-deadline-time-regexp)
  12268. (scheduled org-scheduled-time-regexp)
  12269. (otherwise
  12270. (error "Invalid planning type: %s" type)))
  12271. (line-end-position) t)
  12272. ;; Delete until next keyword or end of line.
  12273. (delete-region
  12274. (match-beginning 0)
  12275. (if (re-search-forward org-keyword-time-not-clock-regexp
  12276. (line-end-position)
  12277. t)
  12278. (match-beginning 0)
  12279. (line-end-position))))))
  12280. ;; If there is nothing more to add and no more keyword
  12281. ;; is left, remove the line completely.
  12282. (if (and (org-looking-at-p "[ \t]*$") (not what))
  12283. (delete-region (line-beginning-position)
  12284. (line-beginning-position 2))
  12285. ;; If we removed last keyword, do not leave trailing
  12286. ;; white space at the end of line.
  12287. (let ((p (point)))
  12288. (save-excursion
  12289. (end-of-line)
  12290. (unless (= (skip-chars-backward " \t" p) 0)
  12291. (delete-region (point) (line-end-position)))))))
  12292. ((not what) (throw 'exit nil)) ; Nothing to do.
  12293. (t (insert-before-markers "\n")
  12294. (backward-char 1)
  12295. (when org-adapt-indentation
  12296. (org-indent-to-column (1+ (org-outline-level))))))
  12297. (when what
  12298. ;; Insert planning keyword.
  12299. (insert (case what
  12300. (closed org-closed-string)
  12301. (deadline org-deadline-string)
  12302. (scheduled org-scheduled-string)
  12303. (otherwise (error "Invalid planning type: %s" what)))
  12304. " ")
  12305. ;; Insert associated timestamp.
  12306. (let ((ts (org-insert-time-stamp
  12307. time
  12308. (or org-time-was-given
  12309. (and (eq what 'closed) org-log-done-with-time))
  12310. (eq what 'closed)
  12311. nil nil (list org-end-time-was-given))))
  12312. (unless (eolp) (insert " "))
  12313. ts))))))
  12314. (defvar org-log-note-marker (make-marker))
  12315. (defvar org-log-note-purpose nil)
  12316. (defvar org-log-note-state nil)
  12317. (defvar org-log-note-previous-state nil)
  12318. (defvar org-log-note-extra nil)
  12319. (defvar org-log-note-window-configuration nil)
  12320. (defvar org-log-note-return-to (make-marker))
  12321. (defvar org-log-note-effective-time nil
  12322. "Remembered current time so that dynamically scoped
  12323. `org-extend-today-until' affects tha timestamps in state change
  12324. log")
  12325. (defvar org-log-post-message nil
  12326. "Message to be displayed after a log note has been stored.
  12327. The auto-repeater uses this.")
  12328. (defun org-add-note ()
  12329. "Add a note to the current entry.
  12330. This is done in the same way as adding a state change note."
  12331. (interactive)
  12332. (org-add-log-setup 'note nil nil 'findpos nil))
  12333. (defun org-log-beginning (&optional create)
  12334. "Return expected start of log notes in current entry.
  12335. When optional argument CREATE is non-nil, the function creates
  12336. a drawer to store notes, if necessary. Returned position ignores
  12337. narrowing."
  12338. (org-with-wide-buffer
  12339. (org-end-of-meta-data)
  12340. (let ((end (if (org-at-heading-p) (point)
  12341. (save-excursion (outline-next-heading) (point))))
  12342. (drawer (org-log-into-drawer)))
  12343. (cond
  12344. (drawer
  12345. (let ((regexp (concat "^[ \t]*:" (regexp-quote drawer) ":[ \t]*$"))
  12346. (case-fold-search t))
  12347. (catch 'exit
  12348. ;; Try to find existing drawer.
  12349. (while (re-search-forward regexp end t)
  12350. (let ((element (org-element-at-point)))
  12351. (when (eq (org-element-type element) 'drawer)
  12352. (let ((cend (org-element-property :contents-end element)))
  12353. (when (and (not org-log-states-order-reversed) cend)
  12354. (goto-char cend)))
  12355. (throw 'exit nil))))
  12356. ;; No drawer found. Create one, if permitted.
  12357. (when create
  12358. (unless (bolp) (insert "\n"))
  12359. (let ((beg (point)))
  12360. (insert ":" drawer ":\n:END:\n")
  12361. (org-indent-region beg (point)))
  12362. (end-of-line -1)))))
  12363. (org-log-state-notes-insert-after-drawers
  12364. (while (and (looking-at org-drawer-regexp)
  12365. (progn (goto-char (match-end 0))
  12366. (re-search-forward org-property-end-re end t)))
  12367. (forward-line)))))
  12368. (if (bolp) (point) (line-beginning-position 2))))
  12369. (defun org-add-log-setup (&optional purpose state prev-state findpos how extra)
  12370. "Set up the post command hook to take a note.
  12371. If this is about to TODO state change, the new state is expected in STATE.
  12372. When FINDPOS is non-nil, find the correct position for the note in
  12373. the current entry. If not, assume that it can be inserted at point.
  12374. HOW is an indicator what kind of note should be created.
  12375. EXTRA is additional text that will be inserted into the notes buffer."
  12376. (org-with-wide-buffer
  12377. (when findpos
  12378. (goto-char (org-log-beginning t))
  12379. (unless org-log-states-order-reversed
  12380. (org-skip-over-state-notes)
  12381. (skip-chars-backward " \t\n\r")
  12382. (forward-line)))
  12383. (move-marker org-log-note-marker (point))
  12384. ;; Preserve position even if a property drawer is inserted in the
  12385. ;; process.
  12386. (set-marker-insertion-type org-log-note-marker t)
  12387. (setq org-log-note-purpose purpose
  12388. org-log-note-state state
  12389. org-log-note-previous-state prev-state
  12390. org-log-note-how how
  12391. org-log-note-extra extra
  12392. org-log-note-effective-time (org-current-effective-time))
  12393. (add-hook 'post-command-hook 'org-add-log-note 'append)))
  12394. (defun org-skip-over-state-notes ()
  12395. "Skip past the list of State notes in an entry."
  12396. (when (ignore-errors (goto-char (org-in-item-p)))
  12397. (let* ((struct (org-list-struct))
  12398. (prevs (org-list-prevs-alist struct))
  12399. (regexp
  12400. (concat "[ \t]*- +"
  12401. (replace-regexp-in-string
  12402. " +" " +"
  12403. (org-replace-escapes
  12404. (regexp-quote (cdr (assq 'state org-log-note-headings)))
  12405. `(("%d" . ,org-ts-regexp-inactive)
  12406. ("%D" . ,org-ts-regexp)
  12407. ("%s" . "\"\\S-+\"")
  12408. ("%S" . "\"\\S-+\"")
  12409. ("%t" . ,org-ts-regexp-inactive)
  12410. ("%T" . ,org-ts-regexp)
  12411. ("%u" . ".*?")
  12412. ("%U" . ".*?")))))))
  12413. (while (org-looking-at-p regexp)
  12414. (goto-char (or (org-list-get-next-item (point) struct prevs)
  12415. (org-list-get-item-end (point) struct)))))))
  12416. (defun org-add-log-note (&optional purpose)
  12417. "Pop up a window for taking a note, and add this note later at point."
  12418. (remove-hook 'post-command-hook 'org-add-log-note)
  12419. (setq org-log-note-window-configuration (current-window-configuration))
  12420. (delete-other-windows)
  12421. (move-marker org-log-note-return-to (point))
  12422. (org-pop-to-buffer-same-window (marker-buffer org-log-note-marker))
  12423. (goto-char org-log-note-marker)
  12424. (org-switch-to-buffer-other-window "*Org Note*")
  12425. (erase-buffer)
  12426. (if (memq org-log-note-how '(time state))
  12427. (let (current-prefix-arg) (org-store-log-note))
  12428. (let ((org-inhibit-startup t)) (org-mode))
  12429. (insert (format "# Insert note for %s.
  12430. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  12431. (cond
  12432. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  12433. ((eq org-log-note-purpose 'done) "closed todo item")
  12434. ((eq org-log-note-purpose 'state)
  12435. (format "state change from \"%s\" to \"%s\""
  12436. (or org-log-note-previous-state "")
  12437. (or org-log-note-state "")))
  12438. ((eq org-log-note-purpose 'reschedule)
  12439. "rescheduling")
  12440. ((eq org-log-note-purpose 'delschedule)
  12441. "no longer scheduled")
  12442. ((eq org-log-note-purpose 'redeadline)
  12443. "changing deadline")
  12444. ((eq org-log-note-purpose 'deldeadline)
  12445. "removing deadline")
  12446. ((eq org-log-note-purpose 'refile)
  12447. "refiling")
  12448. ((eq org-log-note-purpose 'note)
  12449. "this entry")
  12450. (t (error "This should not happen")))))
  12451. (when org-log-note-extra (insert org-log-note-extra))
  12452. (setq-local org-finish-function 'org-store-log-note)
  12453. (run-hooks 'org-log-buffer-setup-hook)))
  12454. (defvar org-note-abort nil) ; dynamically scoped
  12455. (defun org-store-log-note ()
  12456. "Finish taking a log note, and insert it to where it belongs."
  12457. (let ((txt (buffer-string)))
  12458. (kill-buffer (current-buffer))
  12459. (let ((note (cdr (assq org-log-note-purpose org-log-note-headings))) lines)
  12460. (while (string-match "\\`# .*\n[ \t\n]*" txt)
  12461. (setq txt (replace-match "" t t txt)))
  12462. (when (string-match "\\s-+\\'" txt)
  12463. (setq txt (replace-match "" t t txt)))
  12464. (setq lines (org-split-string txt "\n"))
  12465. (when (and note (string-match "\\S-" note))
  12466. (setq note
  12467. (org-replace-escapes
  12468. note
  12469. (list (cons "%u" (user-login-name))
  12470. (cons "%U" user-full-name)
  12471. (cons "%t" (format-time-string
  12472. (org-time-stamp-format 'long 'inactive)
  12473. org-log-note-effective-time))
  12474. (cons "%T" (format-time-string
  12475. (org-time-stamp-format 'long nil)
  12476. org-log-note-effective-time))
  12477. (cons "%d" (format-time-string
  12478. (org-time-stamp-format nil 'inactive)
  12479. org-log-note-effective-time))
  12480. (cons "%D" (format-time-string
  12481. (org-time-stamp-format nil nil)
  12482. org-log-note-effective-time))
  12483. (cons "%s" (cond
  12484. ((not org-log-note-state) "")
  12485. ((org-string-match-p org-ts-regexp
  12486. org-log-note-state)
  12487. (format "\"[%s]\""
  12488. (substring org-log-note-state 1 -1)))
  12489. (t (format "\"%s\"" org-log-note-state))))
  12490. (cons "%S"
  12491. (cond
  12492. ((not org-log-note-previous-state) "")
  12493. ((org-string-match-p org-ts-regexp
  12494. org-log-note-previous-state)
  12495. (format "\"[%s]\""
  12496. (substring
  12497. org-log-note-previous-state 1 -1)))
  12498. (t (format "\"%s\""
  12499. org-log-note-previous-state)))))))
  12500. (when lines (setq note (concat note " \\\\")))
  12501. (push note lines))
  12502. (when (or current-prefix-arg org-note-abort)
  12503. (when (org-log-into-drawer)
  12504. (org-remove-empty-drawer-at org-log-note-marker))
  12505. (setq lines nil))
  12506. (when lines
  12507. (with-current-buffer (marker-buffer org-log-note-marker)
  12508. (org-with-wide-buffer
  12509. (goto-char org-log-note-marker)
  12510. (move-marker org-log-note-marker nil)
  12511. ;; Make sure point is at the beginning of an empty line.
  12512. (cond ((not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  12513. ((looking-at "[ \t]*\\S-") (save-excursion (insert "\n"))))
  12514. ;; In an existing list, add a new item at the top level.
  12515. ;; Otherwise, indent line like a regular one.
  12516. (let ((itemp (org-in-item-p)))
  12517. (if itemp
  12518. (org-indent-line-to
  12519. (let ((struct (save-excursion
  12520. (goto-char itemp) (org-list-struct))))
  12521. (org-list-get-ind (org-list-get-top-point struct) struct)))
  12522. (org-indent-line)))
  12523. (insert (org-list-bullet-string "-") (pop lines))
  12524. (let ((ind (org-list-item-body-column (line-beginning-position))))
  12525. (dolist (line lines)
  12526. (insert "\n")
  12527. (org-indent-line-to ind)
  12528. (insert line)))
  12529. (message "Note stored")
  12530. (org-back-to-heading t)
  12531. (org-cycle-hide-drawers 'children))
  12532. ;; Fix `buffer-undo-list' when `org-store-log-note' is called
  12533. ;; from within `org-add-log-note' because `buffer-undo-list'
  12534. ;; is then modified outside of `org-with-remote-undo'.
  12535. (when (eq this-command 'org-agenda-todo)
  12536. (setcdr buffer-undo-list (cddr buffer-undo-list)))))))
  12537. ;; Don't add undo information when called from `org-agenda-todo'
  12538. (let ((buffer-undo-list (eq this-command 'org-agenda-todo)))
  12539. (set-window-configuration org-log-note-window-configuration)
  12540. (with-current-buffer (marker-buffer org-log-note-return-to)
  12541. (goto-char org-log-note-return-to))
  12542. (move-marker org-log-note-return-to nil)
  12543. (and org-log-post-message (message "%s" org-log-post-message))))
  12544. (defun org-remove-empty-drawer-at (pos)
  12545. "Remove an empty drawer at position POS.
  12546. POS may also be a marker."
  12547. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  12548. (org-with-wide-buffer
  12549. (goto-char pos)
  12550. (let ((drawer (org-element-at-point)))
  12551. (when (and (memq (org-element-type drawer) '(drawer property-drawer))
  12552. (not (org-element-property :contents-begin drawer)))
  12553. (delete-region (org-element-property :begin drawer)
  12554. (progn (goto-char (org-element-property :end drawer))
  12555. (skip-chars-backward " \r\t\n")
  12556. (forward-line)
  12557. (point))))))))
  12558. (defvar org-ts-type nil)
  12559. (defun org-sparse-tree (&optional arg type)
  12560. "Create a sparse tree, prompt for the details.
  12561. This command can create sparse trees. You first need to select the type
  12562. of match used to create the tree:
  12563. t Show all TODO entries.
  12564. T Show entries with a specific TODO keyword.
  12565. m Show entries selected by a tags/property match.
  12566. p Enter a property name and its value (both with completion on existing
  12567. names/values) and show entries with that property.
  12568. r Show entries matching a regular expression (`/' can be used as well).
  12569. b Show deadlines and scheduled items before a date.
  12570. a Show deadlines and scheduled items after a date.
  12571. d Show deadlines due within `org-deadline-warning-days'.
  12572. D Show deadlines and scheduled items between a date range."
  12573. (interactive "P")
  12574. (setq type (or type org-sparse-tree-default-date-type))
  12575. (setq org-ts-type type)
  12576. (message "Sparse tree: [/]regexp [t]odo [T]odo-kwd [m]atch [p]roperty
  12577. [d]eadlines [b]efore-date [a]fter-date [D]ates range
  12578. [c]ycle through date types: %s"
  12579. (case type
  12580. (all "all timestamps")
  12581. (scheduled "only scheduled")
  12582. (deadline "only deadline")
  12583. (active "only active timestamps")
  12584. (inactive "only inactive timestamps")
  12585. (closed "with a closed time-stamp")
  12586. (otherwise "scheduled/deadline")))
  12587. (let ((answer (read-char-exclusive)))
  12588. (case answer
  12589. (?c
  12590. (org-sparse-tree
  12591. arg
  12592. (cadr
  12593. (memq type '(nil all scheduled deadline active inactive closed)))))
  12594. (?d (call-interactively 'org-check-deadlines))
  12595. (?b (call-interactively 'org-check-before-date))
  12596. (?a (call-interactively 'org-check-after-date))
  12597. (?D (call-interactively 'org-check-dates-range))
  12598. (?t (call-interactively 'org-show-todo-tree))
  12599. (?T (org-show-todo-tree '(4)))
  12600. (?m (call-interactively 'org-match-sparse-tree))
  12601. ((?p ?P)
  12602. (let* ((kwd (org-icompleting-read
  12603. "Property: " (mapcar 'list (org-buffer-property-keys))))
  12604. (value (org-icompleting-read
  12605. "Value: " (mapcar 'list (org-property-values kwd)))))
  12606. (unless (string-match "\\`{.*}\\'" value)
  12607. (setq value (concat "\"" value "\"")))
  12608. (org-match-sparse-tree arg (concat kwd "=" value))))
  12609. ((?r ?R ?/) (call-interactively 'org-occur))
  12610. (otherwise (user-error "No such sparse tree command \"%c\"" answer)))))
  12611. (defvar-local org-occur-highlights nil
  12612. "List of overlays used for occur matches.")
  12613. (defvar-local org-occur-parameters nil
  12614. "Parameters of the active org-occur calls.
  12615. This is a list, each call to org-occur pushes as cons cell,
  12616. containing the regular expression and the callback, onto the list.
  12617. The list can contain several entries if `org-occur' has been called
  12618. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  12619. will only contain one set of parameters. When the highlights are
  12620. removed (for example with `C-c C-c', or with the next edit (depending
  12621. on `org-remove-highlights-with-change'), this variable is emptied
  12622. as well.")
  12623. (defun org-occur (regexp &optional keep-previous callback)
  12624. "Make a compact tree which shows all matches of REGEXP.
  12625. The tree will show the lines where the regexp matches, and all higher
  12626. headlines above the match. It will also show the heading after the match,
  12627. to make sure editing the matching entry is easy.
  12628. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  12629. call to `org-occur' will be kept, to allow stacking of calls to this
  12630. command.
  12631. If CALLBACK is non-nil, it is a function which is called to confirm
  12632. that the match should indeed be shown."
  12633. (interactive "sRegexp: \nP")
  12634. (when (equal regexp "")
  12635. (user-error "Regexp cannot be empty"))
  12636. (unless keep-previous
  12637. (org-remove-occur-highlights nil nil t))
  12638. (push (cons regexp callback) org-occur-parameters)
  12639. (let ((cnt 0))
  12640. (save-excursion
  12641. (goto-char (point-min))
  12642. (when (or (not keep-previous) ; do not want to keep
  12643. (not org-occur-highlights)) ; no previous matches
  12644. ;; hide everything
  12645. (org-overview))
  12646. (while (re-search-forward regexp nil t)
  12647. (backward-char) ;; FIXME: Match timestamps at the end of a headline
  12648. (when (or (not callback)
  12649. (save-match-data (funcall callback)))
  12650. (setq cnt (1+ cnt))
  12651. (when org-highlight-sparse-tree-matches
  12652. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  12653. (org-show-context 'occur-tree))))
  12654. (when org-remove-highlights-with-change
  12655. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  12656. nil 'local))
  12657. (unless org-sparse-tree-open-archived-trees
  12658. (org-hide-archived-subtrees (point-min) (point-max)))
  12659. (run-hooks 'org-occur-hook)
  12660. (when (org-called-interactively-p 'interactive)
  12661. (message "%d match(es) for regexp %s" cnt regexp))
  12662. cnt))
  12663. (defun org-occur-next-match (&optional n reset)
  12664. "Function for `next-error-function' to find sparse tree matches.
  12665. N is the number of matches to move, when negative move backwards.
  12666. RESET is entirely ignored - this function always goes back to the
  12667. starting point when no match is found."
  12668. (let* ((limit (if (< n 0) (point-min) (point-max)))
  12669. (search-func (if (< n 0)
  12670. 'previous-single-char-property-change
  12671. 'next-single-char-property-change))
  12672. (n (abs n))
  12673. (pos (point))
  12674. p1)
  12675. (catch 'exit
  12676. (while (setq p1 (funcall search-func (point) 'org-type))
  12677. (when (equal p1 limit)
  12678. (goto-char pos)
  12679. (user-error "No more matches"))
  12680. (when (equal (get-char-property p1 'org-type) 'org-occur)
  12681. (setq n (1- n))
  12682. (when (= n 0)
  12683. (goto-char p1)
  12684. (throw 'exit (point))))
  12685. (goto-char p1))
  12686. (goto-char p1)
  12687. (user-error "No more matches"))))
  12688. (defun org-show-context (&optional key)
  12689. "Make sure point and context are visible.
  12690. Optional argument KEY, when non-nil, is a symbol. See
  12691. `org-show-context-detail' for allowed values and how much is to
  12692. be shown."
  12693. (org-show-set-visibility
  12694. (cond ((symbolp org-show-context-detail) org-show-context-detail)
  12695. ((cdr (assq key org-show-context-detail)))
  12696. (t (cdr (assq 'default org-show-context-detail))))))
  12697. (defun org-show-set-visibility (detail)
  12698. "Set visibility around point according to DETAIL.
  12699. DETAIL is either nil, `minimal', `local', `ancestors', `lineage',
  12700. `tree', `canonical' or t. See `org-show-context-detail' for more
  12701. information."
  12702. (unless (org-before-first-heading-p)
  12703. ;; Show current heading and possibly its entry, following headline
  12704. ;; or all children.
  12705. (if (and (org-at-heading-p) (not (eq detail 'local)))
  12706. (org-flag-heading nil)
  12707. (org-show-entry)
  12708. (org-with-limited-levels
  12709. (case detail
  12710. ((tree canonical t) (org-show-children))
  12711. ((nil minimal ancestors))
  12712. (t (save-excursion
  12713. (outline-next-heading)
  12714. (org-flag-heading nil))))))
  12715. ;; Show all siblings.
  12716. (when (eq detail 'lineage) (org-show-siblings))
  12717. ;; Show ancestors, possibly with their children.
  12718. (when (memq detail '(ancestors lineage tree canonical t))
  12719. (save-excursion
  12720. (while (org-up-heading-safe)
  12721. (org-flag-heading nil)
  12722. (when (memq detail '(canonical t)) (org-show-entry))
  12723. (when (memq detail '(tree canonical t)) (org-show-children)))))))
  12724. (defvar org-reveal-start-hook nil
  12725. "Hook run before revealing a location.")
  12726. (defun org-reveal (&optional siblings)
  12727. "Show current entry, hierarchy above it, and the following headline.
  12728. This can be used to show a consistent set of context around
  12729. locations exposed with `org-show-context'.
  12730. With optional argument SIBLINGS, on each level of the hierarchy all
  12731. siblings are shown. This repairs the tree structure to what it would
  12732. look like when opened with hierarchical calls to `org-cycle'.
  12733. With double optional argument \\[universal-argument] \\[universal-argument], \
  12734. go to the parent and show the
  12735. entire tree."
  12736. (interactive "P")
  12737. (run-hooks 'org-reveal-start-hook)
  12738. (cond ((equal siblings '(4)) (org-show-set-visibility 'canonical))
  12739. ((equal siblings '(16))
  12740. (save-excursion
  12741. (when (org-up-heading-safe)
  12742. (org-show-subtree)
  12743. (run-hook-with-args 'org-cycle-hook 'subtree))))
  12744. (t (org-show-set-visibility 'lineage))))
  12745. (defun org-highlight-new-match (beg end)
  12746. "Highlight from BEG to END and mark the highlight is an occur headline."
  12747. (let ((ov (make-overlay beg end)))
  12748. (overlay-put ov 'face 'secondary-selection)
  12749. (overlay-put ov 'org-type 'org-occur)
  12750. (push ov org-occur-highlights)))
  12751. (defun org-remove-occur-highlights (&optional beg end noremove)
  12752. "Remove the occur highlights from the buffer.
  12753. BEG and END are ignored. If NOREMOVE is nil, remove this function
  12754. from the `before-change-functions' in the current buffer."
  12755. (interactive)
  12756. (unless org-inhibit-highlight-removal
  12757. (mapc 'delete-overlay org-occur-highlights)
  12758. (setq org-occur-highlights nil)
  12759. (setq org-occur-parameters nil)
  12760. (unless noremove
  12761. (remove-hook 'before-change-functions
  12762. 'org-remove-occur-highlights 'local))))
  12763. ;;;; Priorities
  12764. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  12765. "Regular expression matching the priority indicator.")
  12766. (defvar org-remove-priority-next-time nil)
  12767. (defun org-priority-up ()
  12768. "Increase the priority of the current item."
  12769. (interactive)
  12770. (org-priority 'up))
  12771. (defun org-priority-down ()
  12772. "Decrease the priority of the current item."
  12773. (interactive)
  12774. (org-priority 'down))
  12775. (defun org-priority (&optional action show)
  12776. "Change the priority of an item.
  12777. ACTION can be `set', `up', `down', or a character."
  12778. (interactive "P")
  12779. (if (equal action '(4))
  12780. (org-show-priority)
  12781. (unless org-enable-priority-commands
  12782. (user-error "Priority commands are disabled"))
  12783. (setq action (or action 'set))
  12784. (let (current new news have remove)
  12785. (save-excursion
  12786. (org-back-to-heading t)
  12787. (when (looking-at org-priority-regexp)
  12788. (setq current (string-to-char (match-string 2))
  12789. have t))
  12790. (cond
  12791. ((eq action 'remove)
  12792. (setq remove t new ?\ ))
  12793. ((or (eq action 'set)
  12794. (if (featurep 'xemacs) (characterp action) (integerp action)))
  12795. (if (not (eq action 'set))
  12796. (setq new action)
  12797. (message "Priority %c-%c, SPC to remove: "
  12798. org-highest-priority org-lowest-priority)
  12799. (save-match-data
  12800. (setq new (read-char-exclusive))))
  12801. (when (and (= (upcase org-highest-priority) org-highest-priority)
  12802. (= (upcase org-lowest-priority) org-lowest-priority))
  12803. (setq new (upcase new)))
  12804. (cond ((equal new ?\ ) (setq remove t))
  12805. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  12806. (user-error "Priority must be between `%c' and `%c'"
  12807. org-highest-priority org-lowest-priority))))
  12808. ((eq action 'up)
  12809. (setq new (if have
  12810. (1- current) ; normal cycling
  12811. ;; last priority was empty
  12812. (if (eq last-command this-command)
  12813. org-lowest-priority ; wrap around empty to lowest
  12814. ;; default
  12815. (if org-priority-start-cycle-with-default
  12816. org-default-priority
  12817. (1- org-default-priority))))))
  12818. ((eq action 'down)
  12819. (setq new (if have
  12820. (1+ current) ; normal cycling
  12821. ;; last priority was empty
  12822. (if (eq last-command this-command)
  12823. org-highest-priority ; wrap around empty to highest
  12824. ;; default
  12825. (if org-priority-start-cycle-with-default
  12826. org-default-priority
  12827. (1+ org-default-priority))))))
  12828. (t (user-error "Invalid action")))
  12829. (when (or (< (upcase new) org-highest-priority)
  12830. (> (upcase new) org-lowest-priority))
  12831. (if (and (memq action '(up down))
  12832. (not have) (not (eq last-command this-command)))
  12833. ;; `new' is from default priority
  12834. (error
  12835. "The default can not be set, see `org-default-priority' why")
  12836. ;; normal cycling: `new' is beyond highest/lowest priority
  12837. ;; and is wrapped around to the empty priority
  12838. (setq remove t)))
  12839. (setq news (format "%c" new))
  12840. (if have
  12841. (if remove
  12842. (replace-match "" t t nil 1)
  12843. (replace-match news t t nil 2))
  12844. (if remove
  12845. (user-error "No priority cookie found in line")
  12846. (let ((case-fold-search nil))
  12847. (looking-at org-todo-line-regexp))
  12848. (if (match-end 2)
  12849. (progn
  12850. (goto-char (match-end 2))
  12851. (insert " [#" news "]"))
  12852. (goto-char (match-beginning 3))
  12853. (insert "[#" news "] "))))
  12854. (org-set-tags nil 'align))
  12855. (if remove
  12856. (message "Priority removed")
  12857. (message "Priority of current item set to %s" news)))))
  12858. (defun org-show-priority ()
  12859. "Show the priority of the current item.
  12860. This priority is composed of the main priority given with the [#A] cookies,
  12861. and by additional input from the age of a schedules or deadline entry."
  12862. (interactive)
  12863. (let ((pri (if (eq major-mode 'org-agenda-mode)
  12864. (org-get-at-bol 'priority)
  12865. (save-excursion
  12866. (save-match-data
  12867. (beginning-of-line)
  12868. (and (looking-at org-heading-regexp)
  12869. (org-get-priority (match-string 0))))))))
  12870. (message "Priority is %d" (if pri pri -1000))))
  12871. (defun org-get-priority (s)
  12872. "Find priority cookie and return priority."
  12873. (save-match-data
  12874. (if (functionp org-get-priority-function)
  12875. (funcall org-get-priority-function)
  12876. (if (not (string-match org-priority-regexp s))
  12877. (* 1000 (- org-lowest-priority org-default-priority))
  12878. (* 1000 (- org-lowest-priority
  12879. (string-to-char (match-string 2 s))))))))
  12880. ;;;; Tags
  12881. (defvar org-agenda-archives-mode)
  12882. (defvar org-map-continue-from nil
  12883. "Position from where mapping should continue.
  12884. Can be set by the action argument to `org-scan-tags' and `org-map-entries'.")
  12885. (defvar org-scanner-tags nil
  12886. "The current tag list while the tags scanner is running.")
  12887. (defvar org-trust-scanner-tags nil
  12888. "Should `org-get-tags-at' use the tags for the scanner.
  12889. This is for internal dynamical scoping only.
  12890. When this is non-nil, the function `org-get-tags-at' will return the value
  12891. of `org-scanner-tags' instead of building the list by itself. This
  12892. can lead to large speed-ups when the tags scanner is used in a file with
  12893. many entries, and when the list of tags is retrieved, for example to
  12894. obtain a list of properties. Building the tags list for each entry in such
  12895. a file becomes an N^2 operation - but with this variable set, it scales
  12896. as N.")
  12897. (defun org-scan-tags (action matcher todo-only &optional start-level)
  12898. "Scan headline tags with inheritance and produce output ACTION.
  12899. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  12900. or `agenda' to produce an entry list for an agenda view. It can also be
  12901. a Lisp form or a function that should be called at each matched headline, in
  12902. this case the return value is a list of all return values from these calls.
  12903. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  12904. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  12905. only lines with a not-done TODO keyword are included in the output.
  12906. This should be the same variable that was scoped into
  12907. and set by `org-make-tags-matcher' when it constructed MATCHER.
  12908. START-LEVEL can be a string with asterisks, reducing the scope to
  12909. headlines matching this string."
  12910. (require 'org-agenda)
  12911. (let* ((re (concat "^"
  12912. (if start-level
  12913. ;; Get the correct level to match
  12914. (concat "\\*\\{" (number-to-string start-level) "\\} ")
  12915. org-outline-regexp)
  12916. " *\\(\\<\\("
  12917. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  12918. (org-re "\\)\\>\\)? *\\(.*?\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*$")))
  12919. (props (list 'face 'default
  12920. 'done-face 'org-agenda-done
  12921. 'undone-face 'default
  12922. 'mouse-face 'highlight
  12923. 'org-not-done-regexp org-not-done-regexp
  12924. 'org-todo-regexp org-todo-regexp
  12925. 'org-complex-heading-regexp org-complex-heading-regexp
  12926. 'help-echo
  12927. (format "mouse-2 or RET jump to org file %s"
  12928. (abbreviate-file-name
  12929. (or (buffer-file-name (buffer-base-buffer))
  12930. (buffer-name (buffer-base-buffer)))))))
  12931. (org-map-continue-from nil)
  12932. lspos tags tags-list
  12933. (tags-alist (list (cons 0 org-file-tags)))
  12934. (llast 0) rtn rtn1 level category i txt
  12935. todo marker entry priority
  12936. ts-date ts-date-type ts-date-pair)
  12937. (unless (or (member action '(agenda sparse-tree)) (functionp action))
  12938. (setq action (list 'lambda nil action)))
  12939. (save-excursion
  12940. (goto-char (point-min))
  12941. (when (eq action 'sparse-tree)
  12942. (org-overview)
  12943. (org-remove-occur-highlights))
  12944. (while (let (case-fold-search)
  12945. (re-search-forward re nil t))
  12946. (setq org-map-continue-from nil)
  12947. (catch :skip
  12948. (setq todo
  12949. ;; TODO: is the 1-2 difference a bug?
  12950. (when (match-end 1) (org-match-string-no-properties 2))
  12951. tags (when (match-end 4) (org-match-string-no-properties 4)))
  12952. (goto-char (setq lspos (match-beginning 0)))
  12953. (setq level (org-reduced-level (org-outline-level))
  12954. category (org-get-category))
  12955. (when (eq action 'agenda)
  12956. (setq ts-date-pair (org-agenda-entry-get-agenda-timestamp (point))
  12957. ts-date (car ts-date-pair)
  12958. ts-date-type (cdr ts-date-pair)))
  12959. (setq i llast llast level)
  12960. ;; remove tag lists from same and sublevels
  12961. (while (>= i level)
  12962. (when (setq entry (assoc i tags-alist))
  12963. (setq tags-alist (delete entry tags-alist)))
  12964. (setq i (1- i)))
  12965. ;; add the next tags
  12966. (when tags
  12967. (setq tags (org-split-string tags ":")
  12968. tags-alist
  12969. (cons (cons level tags) tags-alist)))
  12970. ;; compile tags for current headline
  12971. (setq tags-list
  12972. (if org-use-tag-inheritance
  12973. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  12974. tags)
  12975. org-scanner-tags tags-list)
  12976. (when org-use-tag-inheritance
  12977. (setcdr (car tags-alist)
  12978. (mapcar (lambda (x)
  12979. (setq x (copy-sequence x))
  12980. (org-add-prop-inherited x))
  12981. (cdar tags-alist))))
  12982. (when (and tags org-use-tag-inheritance
  12983. (or (not (eq t org-use-tag-inheritance))
  12984. org-tags-exclude-from-inheritance))
  12985. ;; selective inheritance, remove uninherited ones
  12986. (setcdr (car tags-alist)
  12987. (org-remove-uninherited-tags (cdar tags-alist))))
  12988. (when (and
  12989. ;; eval matcher only when the todo condition is OK
  12990. (and (or (not todo-only) (member todo org-not-done-keywords))
  12991. (let ((case-fold-search t) (org-trust-scanner-tags t))
  12992. (eval matcher)))
  12993. ;; Call the skipper, but return t if it does not skip,
  12994. ;; so that the `and' form continues evaluating
  12995. (progn
  12996. (unless (eq action 'sparse-tree) (org-agenda-skip))
  12997. t)
  12998. ;; Check if timestamps are deselecting this entry
  12999. (or (not todo-only)
  13000. (and (member todo org-not-done-keywords)
  13001. (or (not org-agenda-tags-todo-honor-ignore-options)
  13002. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item))))))
  13003. ;; select this headline
  13004. (cond
  13005. ((eq action 'sparse-tree)
  13006. (and org-highlight-sparse-tree-matches
  13007. (org-get-heading) (match-end 0)
  13008. (org-highlight-new-match
  13009. (match-beginning 1) (match-end 1)))
  13010. (org-show-context 'tags-tree))
  13011. ((eq action 'agenda)
  13012. (setq txt (org-agenda-format-item
  13013. ""
  13014. (concat
  13015. (if (eq org-tags-match-list-sublevels 'indented)
  13016. (make-string (1- level) ?.) "")
  13017. (org-get-heading))
  13018. (make-string level ?\s)
  13019. category
  13020. tags-list)
  13021. priority (org-get-priority txt))
  13022. (goto-char lspos)
  13023. (setq marker (org-agenda-new-marker))
  13024. (org-add-props txt props
  13025. 'org-marker marker 'org-hd-marker marker 'org-category category
  13026. 'todo-state todo
  13027. 'ts-date ts-date
  13028. 'priority priority
  13029. 'type (concat "tagsmatch" ts-date-type))
  13030. (push txt rtn))
  13031. ((functionp action)
  13032. (setq org-map-continue-from nil)
  13033. (save-excursion
  13034. (setq rtn1 (funcall action))
  13035. (push rtn1 rtn)))
  13036. (t (user-error "Invalid action")))
  13037. ;; if we are to skip sublevels, jump to end of subtree
  13038. (unless org-tags-match-list-sublevels
  13039. (org-end-of-subtree t)
  13040. (backward-char 1))))
  13041. ;; Get the correct position from where to continue
  13042. (if org-map-continue-from
  13043. (goto-char org-map-continue-from)
  13044. (and (= (point) lspos) (end-of-line 1)))))
  13045. (when (and (eq action 'sparse-tree)
  13046. (not org-sparse-tree-open-archived-trees))
  13047. (org-hide-archived-subtrees (point-min) (point-max)))
  13048. (nreverse rtn)))
  13049. (defun org-remove-uninherited-tags (tags)
  13050. "Remove all tags that are not inherited from the list TAGS."
  13051. (cond
  13052. ((eq org-use-tag-inheritance t)
  13053. (if org-tags-exclude-from-inheritance
  13054. (org-delete-all org-tags-exclude-from-inheritance tags)
  13055. tags))
  13056. ((not org-use-tag-inheritance) nil)
  13057. ((stringp org-use-tag-inheritance)
  13058. (delq nil (mapcar
  13059. (lambda (x)
  13060. (if (and (string-match org-use-tag-inheritance x)
  13061. (not (member x org-tags-exclude-from-inheritance)))
  13062. x nil))
  13063. tags)))
  13064. ((listp org-use-tag-inheritance)
  13065. (delq nil (mapcar
  13066. (lambda (x)
  13067. (if (member x org-use-tag-inheritance) x nil))
  13068. tags)))))
  13069. (defun org-match-sparse-tree (&optional todo-only match)
  13070. "Create a sparse tree according to tags string MATCH.
  13071. MATCH can contain positive and negative selection of tags, like
  13072. \"+WORK+URGENT-WITHBOSS\".
  13073. If optional argument TODO-ONLY is non-nil, only select lines that are
  13074. also TODO lines."
  13075. (interactive "P")
  13076. (org-agenda-prepare-buffers (list (current-buffer)))
  13077. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  13078. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  13079. (defvar org-cached-props nil)
  13080. (defun org-cached-entry-get (pom property)
  13081. (if (or (eq t org-use-property-inheritance)
  13082. (and (stringp org-use-property-inheritance)
  13083. (let ((case-fold-search t))
  13084. (org-string-match-p org-use-property-inheritance property)))
  13085. (and (listp org-use-property-inheritance)
  13086. (member-ignore-case property org-use-property-inheritance)))
  13087. ;; Caching is not possible, check it directly.
  13088. (org-entry-get pom property 'inherit)
  13089. ;; Get all properties, so we can do complicated checks easily.
  13090. (cdr (assoc-string property
  13091. (or org-cached-props
  13092. (setq org-cached-props (org-entry-properties pom)))
  13093. t))))
  13094. (defun org-global-tags-completion-table (&optional files)
  13095. "Return the list of all tags in all agenda buffer/files.
  13096. Optional FILES argument is a list of files which can be used
  13097. instead of the agenda files."
  13098. (save-excursion
  13099. (org-uniquify
  13100. (delq nil
  13101. (apply 'append
  13102. (mapcar
  13103. (lambda (file)
  13104. (set-buffer (find-file-noselect file))
  13105. (append (org-get-buffer-tags)
  13106. (mapcar (lambda (x) (if (stringp (car-safe x))
  13107. (list (car-safe x)) nil))
  13108. org-tag-alist)))
  13109. (if (and files (car files))
  13110. files
  13111. (org-agenda-files))))))))
  13112. (defun org-make-tags-matcher (match)
  13113. "Create the TAGS/TODO matcher form for the selection string MATCH.
  13114. The variable `todo-only' is scoped dynamically into this function.
  13115. It will be set to t if the matcher restricts matching to TODO entries,
  13116. otherwise will not be touched.
  13117. Returns a cons of the selection string MATCH and the constructed
  13118. lisp form implementing the matcher. The matcher is to be evaluated
  13119. at an Org entry, with point on the headline, and returns t if the
  13120. entry matches the selection string MATCH. The returned lisp form
  13121. references two variables with information about the entry, which
  13122. must be bound around the form's evaluation: todo, the TODO keyword
  13123. at the entry (or nil of none); and tags-list, the list of all tags
  13124. at the entry including inherited ones. Additionally, the category
  13125. of the entry (if any) must be specified as the text property
  13126. `org-category' on the headline.
  13127. See also `org-scan-tags'.
  13128. "
  13129. (declare (special todo-only))
  13130. (unless (boundp 'todo-only)
  13131. (error "`org-make-tags-matcher' expects todo-only to be scoped in"))
  13132. (unless match
  13133. ;; Get a new match request, with completion against the global
  13134. ;; tags table and the local tags in current buffer
  13135. (let ((org-last-tags-completion-table
  13136. (org-uniquify
  13137. (delq nil (append (org-get-buffer-tags)
  13138. (org-global-tags-completion-table))))))
  13139. (setq match (org-completing-read-no-i
  13140. "Match: " 'org-tags-completion-function nil nil nil
  13141. 'org-tags-history))))
  13142. ;; Parse the string and create a lisp form
  13143. (let ((match0 match)
  13144. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\(\\(?:[[:alnum:]_]+\\(?:\\\\-\\)*\\)+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@#%]+\\)"))
  13145. minus tag mm
  13146. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  13147. orterms term orlist re-p str-p level-p level-op time-p
  13148. prop-p pn pv po gv rest (start 0) (ss 0))
  13149. ;; Expand group tags
  13150. (setq match (org-tags-expand match))
  13151. ;; Check if there is a TODO part of this match, which would be the
  13152. ;; part after a "/". TO make sure that this slash is not part of
  13153. ;; a property value to be matched against, we also check that there
  13154. ;; is no " after that slash.
  13155. ;; First, find the last slash
  13156. (while (string-match "/+" match ss)
  13157. (setq start (match-beginning 0) ss (match-end 0)))
  13158. (if (and (string-match "/+" match start)
  13159. (not (save-match-data (string-match "\"" match start))))
  13160. ;; match contains also a todo-matching request
  13161. (progn
  13162. (setq tagsmatch (substring match 0 (match-beginning 0))
  13163. todomatch (substring match (match-end 0)))
  13164. (when (string-match "^!" todomatch)
  13165. (setq todo-only t todomatch (substring todomatch 1)))
  13166. (when (string-match "^\\s-*$" todomatch)
  13167. (setq todomatch nil)))
  13168. ;; only matching tags
  13169. (setq tagsmatch match todomatch nil))
  13170. ;; Make the tags matcher
  13171. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  13172. (setq tagsmatcher t)
  13173. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  13174. (while (setq term (pop orterms))
  13175. (while (and (equal (substring term -1) "\\") orterms)
  13176. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  13177. (while (string-match re term)
  13178. (setq rest (substring term (match-end 0))
  13179. minus (and (match-end 1)
  13180. (equal (match-string 1 term) "-"))
  13181. tag (save-match-data (replace-regexp-in-string
  13182. "\\\\-" "-"
  13183. (match-string 2 term)))
  13184. re-p (equal (string-to-char tag) ?{)
  13185. level-p (match-end 4)
  13186. prop-p (match-end 5)
  13187. mm (cond
  13188. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  13189. (level-p
  13190. (setq level-op (org-op-to-function (match-string 3 term)))
  13191. `(,level-op level ,(string-to-number
  13192. (match-string 4 term))))
  13193. (prop-p
  13194. (setq pn (match-string 5 term)
  13195. po (match-string 6 term)
  13196. pv (match-string 7 term)
  13197. re-p (equal (string-to-char pv) ?{)
  13198. str-p (equal (string-to-char pv) ?\")
  13199. time-p (save-match-data
  13200. (string-match "^\"[[<].*[]>]\"$" pv))
  13201. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  13202. (when time-p (setq pv (org-matcher-time pv)))
  13203. (setq po (org-op-to-function po (if time-p 'time str-p)))
  13204. (cond
  13205. ((equal pn "CATEGORY")
  13206. (setq gv '(get-text-property (point) 'org-category)))
  13207. ((equal pn "TODO")
  13208. (setq gv 'todo))
  13209. (t
  13210. (setq gv `(org-cached-entry-get nil ,pn))))
  13211. (if re-p
  13212. (if (eq po 'org<>)
  13213. `(not (string-match ,pv (or ,gv "")))
  13214. `(string-match ,pv (or ,gv "")))
  13215. (if str-p
  13216. `(,po (or ,gv "") ,pv)
  13217. `(,po (string-to-number (or ,gv ""))
  13218. ,(string-to-number pv) ))))
  13219. (t `(member ,tag tags-list)))
  13220. mm (if minus (list 'not mm) mm)
  13221. term rest)
  13222. (push mm tagsmatcher))
  13223. (push (if (> (length tagsmatcher) 1)
  13224. (cons 'and tagsmatcher)
  13225. (car tagsmatcher))
  13226. orlist)
  13227. (setq tagsmatcher nil))
  13228. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  13229. (setq tagsmatcher
  13230. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  13231. ;; Make the todo matcher
  13232. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  13233. (setq todomatcher t)
  13234. (setq orterms (org-split-string todomatch "|") orlist nil)
  13235. (dolist (term orterms)
  13236. (while (string-match re term)
  13237. (setq minus (and (match-end 1)
  13238. (equal (match-string 1 term) "-"))
  13239. kwd (match-string 2 term)
  13240. re-p (equal (string-to-char kwd) ?{)
  13241. term (substring term (match-end 0))
  13242. mm (if re-p
  13243. `(string-match ,(substring kwd 1 -1) todo)
  13244. (list 'equal 'todo kwd))
  13245. mm (if minus (list 'not mm) mm))
  13246. (push mm todomatcher))
  13247. (push (if (> (length todomatcher) 1)
  13248. (cons 'and todomatcher)
  13249. (car todomatcher))
  13250. orlist)
  13251. (setq todomatcher nil))
  13252. (setq todomatcher (if (> (length orlist) 1)
  13253. (cons 'or orlist) (car orlist))))
  13254. ;; Return the string and lisp forms of the matcher
  13255. (setq matcher (if todomatcher
  13256. (list 'and tagsmatcher todomatcher)
  13257. tagsmatcher))
  13258. (when todo-only
  13259. (setq matcher (list 'and '(member todo org-not-done-keywords)
  13260. matcher)))
  13261. (cons match0 matcher)))
  13262. (defun org-tags-expand (match &optional single-as-list downcased tags-already-expanded)
  13263. "Expand group tags in MATCH.
  13264. This replaces every group tag in MATCH with a regexp tag search.
  13265. For example, a group tag \"Work\" defined as { Work : Lab Conf }
  13266. will be replaced like this:
  13267. Work => {\\<\\(?:Work\\|Lab\\|Conf\\)\\>}
  13268. +Work => +{\\<\\(?:Work\\|Lab\\|Conf\\)\\>}
  13269. -Work => -{\\<\\(?:Work\\|Lab\\|Conf\\)\\>}
  13270. Replacing by a regexp preserves the structure of the match.
  13271. E.g., this expansion
  13272. Work|Home => {\\(?:Work\\|Lab\\|Conf\\}|Home
  13273. will match anything tagged with \"Lab\" and \"Home\", or tagged
  13274. with \"Conf\" and \"Home\" or tagged with \"Work\" and \"home\".
  13275. A group tag in MATCH can contain regular expressions of its own.
  13276. For example, a group tag \"Proj\" defined as { Proj : {P@.+} }
  13277. will be replaced like this:
  13278. Proj => {\\<\\(?:Proj\\)\\>\\|P@.+}
  13279. When the optional argument SINGLE-AS-LIST is non-nil, MATCH is
  13280. assumed to be a single group tag, and the function will return
  13281. the list of tags in this group.
  13282. When DOWNCASE is non-nil, expand downcased TAGS."
  13283. (if org-group-tags
  13284. (let* ((case-fold-search t)
  13285. (stable org-mode-syntax-table)
  13286. (taggroups (or org-tag-groups-alist-for-agenda org-tag-groups-alist))
  13287. (taggroups (if downcased
  13288. (mapcar (lambda (tg) (mapcar #'downcase tg))
  13289. taggroups)
  13290. taggroups))
  13291. (taggroups-keys (mapcar #'car taggroups))
  13292. (return-match (if downcased (downcase match) match))
  13293. (count 0)
  13294. (work-already-expanded tags-already-expanded)
  13295. regexps-in-match tags-in-group regexp-in-group regexp-in-group-escaped)
  13296. ;; @ and _ are allowed as word-components in tags.
  13297. (modify-syntax-entry ?@ "w" stable)
  13298. (modify-syntax-entry ?_ "w" stable)
  13299. ;; Temporarily replace regexp-expressions in the match-expression.
  13300. (while (string-match "{.+?}" return-match)
  13301. (incf count)
  13302. (push (match-string 0 return-match) regexps-in-match)
  13303. (setq return-match (replace-match (format "<%d>" count) t nil return-match)))
  13304. (while (and taggroups-keys
  13305. (with-syntax-table stable
  13306. (string-match
  13307. (concat "\\(?1:[+-]?\\)\\(?2:\\<"
  13308. (regexp-opt taggroups-keys) "\\>\\)")
  13309. return-match)))
  13310. (let* ((dir (match-string 1 return-match))
  13311. (tag (match-string 2 return-match))
  13312. (tag (if downcased (downcase tag) tag)))
  13313. (unless (or (get-text-property 0 'grouptag (match-string 2 return-match))
  13314. (member tag work-already-expanded))
  13315. (setq tags-in-group (assoc tag taggroups))
  13316. (push tag work-already-expanded)
  13317. ;; Recursively expand each tag in the group, if the tag hasn't
  13318. ;; already been expanded. Restore the match-data after all recursive calls.
  13319. (save-match-data
  13320. (let (tags-expanded)
  13321. (dolist (x (cdr tags-in-group))
  13322. (if (and (member x taggroups-keys)
  13323. (not (member x work-already-expanded)))
  13324. (setq tags-expanded
  13325. (delete-dups
  13326. (append
  13327. (org-tags-expand x t downcased
  13328. work-already-expanded)
  13329. tags-expanded)))
  13330. (setq tags-expanded
  13331. (append (list x) tags-expanded)))
  13332. (setq work-already-expanded
  13333. (delete-dups
  13334. (append tags-expanded
  13335. work-already-expanded))))
  13336. (setq tags-in-group
  13337. (delete-dups (cons (car tags-in-group)
  13338. tags-expanded)))))
  13339. ;; Filter tag-regexps from tags.
  13340. (setq regexp-in-group-escaped
  13341. (delq nil (mapcar (lambda (x)
  13342. (if (stringp x)
  13343. (and (equal "{" (substring x 0 1))
  13344. (equal "}" (substring x -1))
  13345. x)
  13346. x))
  13347. tags-in-group))
  13348. regexp-in-group
  13349. (mapcar (lambda (x)
  13350. (substring x 1 -1))
  13351. regexp-in-group-escaped)
  13352. tags-in-group
  13353. (delq nil (mapcar (lambda (x)
  13354. (if (stringp x)
  13355. (and (not (equal "{" (substring x 0 1)))
  13356. (not (equal "}" (substring x -1)))
  13357. x)
  13358. x))
  13359. tags-in-group)))
  13360. ;; If single-as-list, do no more in the while-loop.
  13361. (if (not single-as-list)
  13362. (progn
  13363. (when regexp-in-group
  13364. (setq regexp-in-group
  13365. (concat "\\|"
  13366. (mapconcat 'identity regexp-in-group
  13367. "\\|"))))
  13368. (setq tags-in-group
  13369. (concat dir
  13370. "{\\<"
  13371. (regexp-opt tags-in-group)
  13372. "\\>"
  13373. regexp-in-group
  13374. "}"))
  13375. (when (stringp tags-in-group)
  13376. (org-add-props tags-in-group '(grouptag t)))
  13377. (setq return-match
  13378. (replace-match tags-in-group t t return-match)))
  13379. (setq tags-in-group
  13380. (append regexp-in-group-escaped tags-in-group))))
  13381. (setq taggroups-keys (delete tag taggroups-keys))))
  13382. ;; Add the regular expressions back into the match-expression again.
  13383. (while regexps-in-match
  13384. (setq return-match (replace-regexp-in-string (format "<%d>" count)
  13385. (pop regexps-in-match)
  13386. return-match t t))
  13387. (decf count))
  13388. (if single-as-list
  13389. (if tags-in-group tags-in-group (list return-match))
  13390. return-match))
  13391. (if single-as-list
  13392. (list (if downcased (downcase match) match))
  13393. match)))
  13394. (defun org-op-to-function (op &optional stringp)
  13395. "Turn an operator into the appropriate function."
  13396. (setq op
  13397. (cond
  13398. ((equal op "<" ) '(< string< org-time<))
  13399. ((equal op ">" ) '(> org-string> org-time>))
  13400. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  13401. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  13402. ((member op '("=" "==")) '(= string= org-time=))
  13403. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  13404. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  13405. (defun org<> (a b) (not (= a b)))
  13406. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  13407. (defun org-string>= (a b) (not (string< a b)))
  13408. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  13409. (defun org-string<> (a b) (not (string= a b)))
  13410. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  13411. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  13412. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  13413. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  13414. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  13415. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  13416. (defun org-2ft (s)
  13417. "Convert S to a floating point time.
  13418. If S is already a number, just return it. If it is a string, parse
  13419. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  13420. (cond
  13421. ((numberp s) s)
  13422. ((stringp s)
  13423. (condition-case nil
  13424. (float-time (apply 'encode-time (org-parse-time-string s)))
  13425. (error 0.)))
  13426. (t 0.)))
  13427. (defun org-time-today ()
  13428. "Time in seconds today at 0:00.
  13429. Returns the float number of seconds since the beginning of the
  13430. epoch to the beginning of today (00:00)."
  13431. (float-time (apply 'encode-time
  13432. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  13433. (defun org-matcher-time (s)
  13434. "Interpret a time comparison value."
  13435. (save-match-data
  13436. (cond
  13437. ((string= s "<now>") (float-time))
  13438. ((string= s "<today>") (org-time-today))
  13439. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  13440. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  13441. ((string-match "^<\\([-+][0-9]+\\)\\([hdwmy]\\)>$" s)
  13442. (+ (org-time-today)
  13443. (* (string-to-number (match-string 1 s))
  13444. (cdr (assoc (match-string 2 s)
  13445. '(("d" . 86400.0) ("w" . 604800.0)
  13446. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  13447. (t (org-2ft s)))))
  13448. (defun org-match-any-p (re list)
  13449. "Does re match any element of list?"
  13450. (setq list (mapcar (lambda (x) (string-match re x)) list))
  13451. (delq nil list))
  13452. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  13453. (defvar org-tags-overlay (make-overlay 1 1))
  13454. (org-detach-overlay org-tags-overlay)
  13455. (defun org-get-local-tags-at (&optional pos)
  13456. "Get a list of tags defined in the current headline."
  13457. (org-get-tags-at pos 'local))
  13458. (defun org-get-local-tags ()
  13459. "Get a list of tags defined in the current headline."
  13460. (org-get-tags-at nil 'local))
  13461. (defun org-get-tags-at (&optional pos local)
  13462. "Get a list of all headline tags applicable at POS.
  13463. POS defaults to point. If tags are inherited, the list contains
  13464. the targets in the same sequence as the headlines appear, i.e.
  13465. the tags of the current headline come last.
  13466. When LOCAL is non-nil, only return tags from the current headline,
  13467. ignore inherited ones."
  13468. (interactive)
  13469. (if (and org-trust-scanner-tags
  13470. (or (not pos) (equal pos (point)))
  13471. (not local))
  13472. org-scanner-tags
  13473. (let (tags ltags lastpos parent)
  13474. (save-excursion
  13475. (save-restriction
  13476. (widen)
  13477. (goto-char (or pos (point)))
  13478. (save-match-data
  13479. (catch 'done
  13480. (condition-case nil
  13481. (progn
  13482. (org-back-to-heading t)
  13483. (while (not (equal lastpos (point)))
  13484. (setq lastpos (point))
  13485. (when (looking-at
  13486. (org-re "[^\r\n]+?:\\([[:alnum:]_@#%:]+\\):[ \t]*$"))
  13487. (setq ltags (org-split-string
  13488. (org-match-string-no-properties 1) ":"))
  13489. (when parent
  13490. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  13491. (setq tags (append
  13492. (if parent
  13493. (org-remove-uninherited-tags ltags)
  13494. ltags)
  13495. tags)))
  13496. (or org-use-tag-inheritance (throw 'done t))
  13497. (when local (throw 'done t))
  13498. (or (org-up-heading-safe) (error nil))
  13499. (setq parent t)))
  13500. (error nil)))))
  13501. (if local
  13502. tags
  13503. (reverse (delete-dups
  13504. (reverse (append
  13505. (org-remove-uninherited-tags
  13506. org-file-tags)
  13507. tags)))))))))
  13508. (defun org-add-prop-inherited (s)
  13509. (add-text-properties 0 (length s) '(inherited t) s)
  13510. s)
  13511. (defun org-toggle-tag (tag &optional onoff)
  13512. "Toggle the tag TAG for the current line.
  13513. If ONOFF is `on' or `off', don't toggle but set to this state."
  13514. (let (res current)
  13515. (save-excursion
  13516. (org-back-to-heading t)
  13517. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t]*$")
  13518. (point-at-eol) t)
  13519. (progn
  13520. (setq current (match-string 1))
  13521. (replace-match ""))
  13522. (setq current ""))
  13523. (setq current (nreverse (org-split-string current ":")))
  13524. (cond
  13525. ((eq onoff 'on)
  13526. (setq res t)
  13527. (or (member tag current) (push tag current)))
  13528. ((eq onoff 'off)
  13529. (or (not (member tag current)) (setq current (delete tag current))))
  13530. (t (if (member tag current)
  13531. (setq current (delete tag current))
  13532. (setq res t)
  13533. (push tag current))))
  13534. (end-of-line 1)
  13535. (if current
  13536. (progn
  13537. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  13538. (org-set-tags nil t))
  13539. (delete-horizontal-space))
  13540. (run-hooks 'org-after-tags-change-hook))
  13541. res))
  13542. (defun org-align-tags-here (to-col)
  13543. ;; Assumes that this is a headline
  13544. "Align tags on the current headline to TO-COL."
  13545. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  13546. (beginning-of-line 1)
  13547. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13548. (< pos (match-beginning 2)))
  13549. (progn
  13550. (setq tags-l (- (match-end 2) (match-beginning 2)))
  13551. (goto-char (match-beginning 1))
  13552. (insert " ")
  13553. (delete-region (point) (1+ (match-beginning 2)))
  13554. (setq ncol (max (current-column)
  13555. (1+ col)
  13556. (if (> to-col 0)
  13557. to-col
  13558. (- (abs to-col) tags-l))))
  13559. (setq p (point))
  13560. (insert (make-string (- ncol (current-column)) ?\ ))
  13561. (setq ncol (current-column))
  13562. (when indent-tabs-mode (tabify p (point-at-eol)))
  13563. (org-move-to-column (min ncol col)))
  13564. (goto-char pos))))
  13565. (defun org-set-tags-command (&optional arg just-align)
  13566. "Call the set-tags command for the current entry."
  13567. (interactive "P")
  13568. (if (or (org-at-heading-p) (and arg (org-before-first-heading-p)))
  13569. (org-set-tags arg just-align)
  13570. (save-excursion
  13571. (unless (and (org-region-active-p)
  13572. org-loop-over-headlines-in-active-region)
  13573. (org-back-to-heading t))
  13574. (org-set-tags arg just-align))))
  13575. (defun org-set-tags-to (data)
  13576. "Set the tags of the current entry to DATA, replacing the current tags.
  13577. DATA may be a tags string like :aa:bb:cc:, or a list of tags.
  13578. If DATA is nil or the empty string, any tags will be removed."
  13579. (interactive "sTags: ")
  13580. (setq data
  13581. (cond
  13582. ((eq data nil) "")
  13583. ((equal data "") "")
  13584. ((stringp data)
  13585. (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
  13586. ":"))
  13587. ((listp data)
  13588. (concat ":" (mapconcat 'identity data ":") ":"))))
  13589. (when data
  13590. (save-excursion
  13591. (org-back-to-heading t)
  13592. (when (looking-at org-complex-heading-regexp)
  13593. (if (match-end 5)
  13594. (progn
  13595. (goto-char (match-beginning 5))
  13596. (insert data)
  13597. (delete-region (point) (point-at-eol))
  13598. (org-set-tags nil 'align))
  13599. (goto-char (point-at-eol))
  13600. (insert " " data)
  13601. (org-set-tags nil 'align)))
  13602. (beginning-of-line 1)
  13603. (when (looking-at ".*?\\([ \t]+\\)$")
  13604. (delete-region (match-beginning 1) (match-end 1))))))
  13605. (defun org-align-all-tags ()
  13606. "Align the tags i all headings."
  13607. (interactive)
  13608. (save-excursion
  13609. (or (ignore-errors (org-back-to-heading t))
  13610. (outline-next-heading))
  13611. (if (org-at-heading-p)
  13612. (org-set-tags t)
  13613. (message "No headings"))))
  13614. (defvar org-indent-indentation-per-level)
  13615. (defun org-set-tags (&optional arg just-align)
  13616. "Set the tags for the current headline.
  13617. With prefix ARG, realign all tags in headings in the current buffer.
  13618. When JUST-ALIGN is non-nil, only align tags."
  13619. (interactive "P")
  13620. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  13621. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  13622. 'region-start-level
  13623. 'region))
  13624. org-loop-over-headlines-in-active-region)
  13625. (org-map-entries
  13626. ;; We don't use ARG and JUST-ALIGN here because these args
  13627. ;; are not useful when looping over headlines.
  13628. #'org-set-tags
  13629. org-loop-over-headlines-in-active-region
  13630. cl
  13631. '(when (outline-invisible-p) (org-end-of-subtree nil t))))
  13632. (let ((org-setting-tags t))
  13633. (if arg
  13634. (save-excursion
  13635. (goto-char (point-min))
  13636. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  13637. (while (re-search-forward org-outline-regexp-bol nil t)
  13638. (org-set-tags nil t)
  13639. (end-of-line)))
  13640. (message "All tags realigned to column %d" org-tags-column))
  13641. (let* ((current (org-get-tags-string))
  13642. (col (current-column))
  13643. (tags
  13644. (if just-align current
  13645. ;; Get a new set of tags from the user.
  13646. (save-excursion
  13647. (let* ((table
  13648. (setq
  13649. org-last-tags-completion-table
  13650. (append
  13651. org-tag-persistent-alist
  13652. (or org-tag-alist (org-get-buffer-tags))
  13653. (and
  13654. org-complete-tags-always-offer-all-agenda-tags
  13655. (org-global-tags-completion-table
  13656. (org-agenda-files))))))
  13657. (current-tags (org-split-string current ":"))
  13658. (inherited-tags
  13659. (nreverse (nthcdr (length current-tags)
  13660. (nreverse (org-get-tags-at))))))
  13661. (replace-regexp-in-string
  13662. "\\([-+&]+\\|,\\)"
  13663. ":"
  13664. (if (or (eq t org-use-fast-tag-selection)
  13665. (and org-use-fast-tag-selection
  13666. (delq nil (mapcar #'cdr table))))
  13667. (org-fast-tag-selection
  13668. current-tags inherited-tags table
  13669. (and org-fast-tag-selection-include-todo
  13670. org-todo-key-alist))
  13671. (let ((org-add-colon-after-tag-completion
  13672. (< 1 (length table))))
  13673. (org-trim
  13674. (completing-read
  13675. "Tags: "
  13676. #'org-tags-completion-function
  13677. nil nil current 'org-tags-history))))))))))
  13678. (when org-tags-sort-function
  13679. (setq tags
  13680. (mapconcat
  13681. #'identity
  13682. (sort (org-split-string tags (org-re "[^[:alnum:]_@#%]+"))
  13683. org-tags-sort-function)
  13684. ":")))
  13685. (if (not (org-string-nw-p tags)) (setq tags "")
  13686. (unless (string-match ":\\'" tags) (setq tags (concat tags ":")))
  13687. (unless (string-match "\\`:" tags) (setq tags (concat ":" tags))))
  13688. ;; Insert new tags at the correct column
  13689. (beginning-of-line)
  13690. (let ((level (if (looking-at org-outline-regexp)
  13691. (- (match-end 0) (point) 1)
  13692. 1)))
  13693. (cond
  13694. ((and (equal current "") (equal tags "")))
  13695. ((re-search-forward
  13696. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  13697. (line-end-position)
  13698. t)
  13699. (if (equal tags "") (replace-match "" t t)
  13700. (goto-char (match-beginning 0))
  13701. (let* ((c0 (current-column))
  13702. ;; Compute offset for the case of org-indent-mode
  13703. ;; active.
  13704. (di (if (org-bound-and-true-p org-indent-mode)
  13705. (* (1- org-indent-indentation-per-level)
  13706. (1- level))
  13707. 0))
  13708. (p0 (if (eq (char-before) ?*) (1+ (point)) (point)))
  13709. (tc (+ org-tags-column
  13710. (if (> org-tags-column 0) (- di) di)))
  13711. (c1 (max (1+ c0)
  13712. (if (> tc 0) tc
  13713. (- (- tc) (string-width tags)))))
  13714. (rpl (concat (make-string (max 0 (- c1 c0)) ?\s) tags)))
  13715. (replace-match rpl t t)
  13716. (when (and (not (featurep 'xemacs)) indent-tabs-mode)
  13717. (tabify p0 (point))))))
  13718. (t (error "Tags alignment failed"))))
  13719. (org-move-to-column col))
  13720. (unless just-align (run-hooks 'org-after-tags-change-hook))))))
  13721. (defun org-change-tag-in-region (beg end tag off)
  13722. "Add or remove TAG for each entry in the region.
  13723. This works in the agenda, and also in an org-mode buffer."
  13724. (interactive
  13725. (list (region-beginning) (region-end)
  13726. (let ((org-last-tags-completion-table
  13727. (if (derived-mode-p 'org-mode)
  13728. (org-uniquify
  13729. (delq nil (append (org-get-buffer-tags)
  13730. (org-global-tags-completion-table))))
  13731. (org-global-tags-completion-table))))
  13732. (org-icompleting-read
  13733. "Tag: " 'org-tags-completion-function nil nil nil
  13734. 'org-tags-history))
  13735. (progn
  13736. (message "[s]et or [r]emove? ")
  13737. (equal (read-char-exclusive) ?r))))
  13738. (when (fboundp 'deactivate-mark) (deactivate-mark))
  13739. (let ((agendap (equal major-mode 'org-agenda-mode))
  13740. l1 l2 m buf pos newhead (cnt 0))
  13741. (goto-char end)
  13742. (setq l2 (1- (org-current-line)))
  13743. (goto-char beg)
  13744. (setq l1 (org-current-line))
  13745. (loop for l from l1 to l2 do
  13746. (org-goto-line l)
  13747. (setq m (get-text-property (point) 'org-hd-marker))
  13748. (when (or (and (derived-mode-p 'org-mode) (org-at-heading-p))
  13749. (and agendap m))
  13750. (setq buf (if agendap (marker-buffer m) (current-buffer))
  13751. pos (if agendap m (point)))
  13752. (with-current-buffer buf
  13753. (save-excursion
  13754. (save-restriction
  13755. (goto-char pos)
  13756. (setq cnt (1+ cnt))
  13757. (org-toggle-tag tag (if off 'off 'on))
  13758. (setq newhead (org-get-heading)))))
  13759. (and agendap (org-agenda-change-all-lines newhead m))))
  13760. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  13761. (defun org-tags-completion-function (string predicate &optional flag)
  13762. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  13763. (confirm (lambda (x) (stringp (car x)))))
  13764. (if (string-match "^\\(.*[-+:&,|]\\)\\([^-+:&,|]*\\)$" string)
  13765. (setq s1 (match-string 1 string)
  13766. s2 (match-string 2 string))
  13767. (setq s1 "" s2 string))
  13768. (cond
  13769. ((eq flag nil)
  13770. ;; try completion
  13771. (setq rtn (try-completion s2 ctable confirm))
  13772. (when (stringp rtn)
  13773. (setq rtn
  13774. (concat s1 s2 (substring rtn (length s2))
  13775. (if (and org-add-colon-after-tag-completion
  13776. (assoc rtn ctable))
  13777. ":" ""))))
  13778. rtn)
  13779. ((eq flag t)
  13780. ;; all-completions
  13781. (all-completions s2 ctable confirm))
  13782. ((eq flag 'lambda)
  13783. ;; exact match?
  13784. (assoc s2 ctable)))))
  13785. (defun org-fast-tag-insert (kwd tags face &optional end)
  13786. "Insert KDW, and the TAGS, the latter with face FACE.
  13787. Also insert END."
  13788. (insert (format "%-12s" (concat kwd ":"))
  13789. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  13790. (or end "")))
  13791. (defun org-fast-tag-show-exit (flag)
  13792. (save-excursion
  13793. (org-goto-line 3)
  13794. (when (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  13795. (replace-match ""))
  13796. (when flag
  13797. (end-of-line 1)
  13798. (org-move-to-column (- (window-width) 19) t)
  13799. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  13800. (defun org-set-current-tags-overlay (current prefix)
  13801. "Add an overlay to CURRENT tag with PREFIX."
  13802. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  13803. (if (featurep 'xemacs)
  13804. (org-overlay-display org-tags-overlay (concat prefix s)
  13805. 'secondary-selection)
  13806. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  13807. (org-overlay-display org-tags-overlay (concat prefix s)))))
  13808. (defvar org-last-tag-selection-key nil)
  13809. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  13810. "Fast tag selection with single keys.
  13811. CURRENT is the current list of tags in the headline, INHERITED is the
  13812. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  13813. possibly with grouping information. TODO-TABLE is a similar table with
  13814. TODO keywords, should these have keys assigned to them.
  13815. If the keys are nil, a-z are automatically assigned.
  13816. Returns the new tags string, or nil to not change the current settings."
  13817. (let* ((fulltable (append table todo-table))
  13818. (maxlen (apply 'max (mapcar
  13819. (lambda (x)
  13820. (if (stringp (car x)) (string-width (car x)) 0))
  13821. fulltable)))
  13822. (buf (current-buffer))
  13823. (expert (eq org-fast-tag-selection-single-key 'expert))
  13824. (buffer-tags nil)
  13825. (fwidth (+ maxlen 3 1 3))
  13826. (ncol (/ (- (window-width) 4) fwidth))
  13827. (i-face 'org-done)
  13828. (c-face 'org-todo)
  13829. tg cnt e c char c1 c2 ntable tbl rtn
  13830. ov-start ov-end ov-prefix
  13831. (exit-after-next org-fast-tag-selection-single-key)
  13832. (done-keywords org-done-keywords)
  13833. groups ingroup intaggroup)
  13834. (save-excursion
  13835. (beginning-of-line 1)
  13836. (if (looking-at
  13837. (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13838. (setq ov-start (match-beginning 1)
  13839. ov-end (match-end 1)
  13840. ov-prefix "")
  13841. (setq ov-start (1- (point-at-eol))
  13842. ov-end (1+ ov-start))
  13843. (skip-chars-forward "^\n\r")
  13844. (setq ov-prefix
  13845. (concat
  13846. (buffer-substring (1- (point)) (point))
  13847. (if (> (current-column) org-tags-column)
  13848. " "
  13849. (make-string (- org-tags-column (current-column)) ?\ ))))))
  13850. (move-overlay org-tags-overlay ov-start ov-end)
  13851. (save-window-excursion
  13852. (if expert
  13853. (set-buffer (get-buffer-create " *Org tags*"))
  13854. (delete-other-windows)
  13855. (set-window-buffer (split-window-vertically) (get-buffer-create " *Org tags*"))
  13856. (org-switch-to-buffer-other-window " *Org tags*"))
  13857. (erase-buffer)
  13858. (setq-local org-done-keywords done-keywords)
  13859. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  13860. (org-fast-tag-insert "Current" current c-face "\n\n")
  13861. (org-fast-tag-show-exit exit-after-next)
  13862. (org-set-current-tags-overlay current ov-prefix)
  13863. (setq tbl fulltable char ?a cnt 0)
  13864. (while (setq e (pop tbl))
  13865. (cond
  13866. ((eq (car e) :startgroup)
  13867. (push '() groups) (setq ingroup t)
  13868. (unless (zerop cnt)
  13869. (setq cnt 0)
  13870. (insert "\n"))
  13871. (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
  13872. ((eq (car e) :endgroup)
  13873. (setq ingroup nil cnt 0)
  13874. (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
  13875. ((eq (car e) :startgrouptag)
  13876. (setq intaggroup t)
  13877. (unless (zerop cnt)
  13878. (setq cnt 0)
  13879. (insert "\n"))
  13880. (insert "[ "))
  13881. ((eq (car e) :endgrouptag)
  13882. (setq intaggroup nil cnt 0)
  13883. (insert "]\n"))
  13884. ((equal e '(:newline))
  13885. (unless (zerop cnt)
  13886. (setq cnt 0)
  13887. (insert "\n")
  13888. (setq e (car tbl))
  13889. (while (equal (car tbl) '(:newline))
  13890. (insert "\n")
  13891. (setq tbl (cdr tbl)))))
  13892. ((equal e '(:grouptags)) (insert " : "))
  13893. (t
  13894. (setq tg (copy-sequence (car e)) c2 nil)
  13895. (if (cdr e)
  13896. (setq c (cdr e))
  13897. ;; automatically assign a character.
  13898. (setq c1 (string-to-char
  13899. (downcase (substring
  13900. tg (if (= (string-to-char tg) ?@) 1 0)))))
  13901. (if (or (rassoc c1 ntable) (rassoc c1 table))
  13902. (while (or (rassoc char ntable) (rassoc char table))
  13903. (setq char (1+ char)))
  13904. (setq c2 c1))
  13905. (setq c (or c2 char)))
  13906. (when ingroup (push tg (car groups)))
  13907. (setq tg (org-add-props tg nil 'face
  13908. (cond
  13909. ((not (assoc tg table))
  13910. (org-get-todo-face tg))
  13911. ((member tg current) c-face)
  13912. ((member tg inherited) i-face))))
  13913. (when (equal (caar tbl) :grouptags)
  13914. (org-add-props tg nil 'face 'org-tag-group))
  13915. (when (and (zerop cnt) (not ingroup) (not intaggroup)) (insert " "))
  13916. (insert "[" c "] " tg (make-string
  13917. (- fwidth 4 (length tg)) ?\ ))
  13918. (push (cons tg c) ntable)
  13919. (when (= (incf cnt) ncol)
  13920. (insert "\n")
  13921. (when (or ingroup intaggroup) (insert " "))
  13922. (setq cnt 0)))))
  13923. (setq ntable (nreverse ntable))
  13924. (insert "\n")
  13925. (goto-char (point-min))
  13926. (unless expert (org-fit-window-to-buffer))
  13927. (setq rtn
  13928. (catch 'exit
  13929. (while t
  13930. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
  13931. (if (not groups) "no " "")
  13932. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  13933. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  13934. (setq org-last-tag-selection-key c)
  13935. (cond
  13936. ((= c ?\r) (throw 'exit t))
  13937. ((= c ?!)
  13938. (setq groups (not groups))
  13939. (goto-char (point-min))
  13940. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  13941. ((= c ?\C-c)
  13942. (if (not expert)
  13943. (org-fast-tag-show-exit
  13944. (setq exit-after-next (not exit-after-next)))
  13945. (setq expert nil)
  13946. (delete-other-windows)
  13947. (set-window-buffer (split-window-vertically) " *Org tags*")
  13948. (org-switch-to-buffer-other-window " *Org tags*")
  13949. (org-fit-window-to-buffer)))
  13950. ((or (= c ?\C-g)
  13951. (and (= c ?q) (not (rassoc c ntable))))
  13952. (org-detach-overlay org-tags-overlay)
  13953. (setq quit-flag t))
  13954. ((= c ?\ )
  13955. (setq current nil)
  13956. (when exit-after-next (setq exit-after-next 'now)))
  13957. ((= c ?\t)
  13958. (condition-case nil
  13959. (setq tg (org-icompleting-read
  13960. "Tag: "
  13961. (or buffer-tags
  13962. (with-current-buffer buf
  13963. (setq buffer-tags
  13964. (org-get-buffer-tags))))))
  13965. (quit (setq tg "")))
  13966. (when (string-match "\\S-" tg)
  13967. (add-to-list 'buffer-tags (list tg))
  13968. (if (member tg current)
  13969. (setq current (delete tg current))
  13970. (push tg current)))
  13971. (when exit-after-next (setq exit-after-next 'now)))
  13972. ((setq e (rassoc c todo-table) tg (car e))
  13973. (with-current-buffer buf
  13974. (save-excursion (org-todo tg)))
  13975. (when exit-after-next (setq exit-after-next 'now)))
  13976. ((setq e (rassoc c ntable) tg (car e))
  13977. (if (member tg current)
  13978. (setq current (delete tg current))
  13979. (loop for g in groups do
  13980. (when (member tg g)
  13981. (dolist (x g) (setq current (delete x current)))))
  13982. (push tg current))
  13983. (when exit-after-next (setq exit-after-next 'now))))
  13984. ;; Create a sorted list
  13985. (setq current
  13986. (sort current
  13987. (lambda (a b)
  13988. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  13989. (when (eq exit-after-next 'now) (throw 'exit t))
  13990. (goto-char (point-min))
  13991. (beginning-of-line 2)
  13992. (delete-region (point) (point-at-eol))
  13993. (org-fast-tag-insert "Current" current c-face)
  13994. (org-set-current-tags-overlay current ov-prefix)
  13995. (while (re-search-forward
  13996. (org-re "\\[.\\] \\([[:alnum:]_@#%]+\\)") nil t)
  13997. (setq tg (match-string 1))
  13998. (add-text-properties
  13999. (match-beginning 1) (match-end 1)
  14000. (list 'face
  14001. (cond
  14002. ((member tg current) c-face)
  14003. ((member tg inherited) i-face)
  14004. (t (get-text-property (match-beginning 1) 'face))))))
  14005. (goto-char (point-min)))))
  14006. (org-detach-overlay org-tags-overlay)
  14007. (if rtn
  14008. (mapconcat 'identity current ":")
  14009. nil))))
  14010. (defun org-get-tags-string ()
  14011. "Get the TAGS string in the current headline."
  14012. (unless (org-at-heading-p t)
  14013. (user-error "Not on a heading"))
  14014. (save-excursion
  14015. (beginning-of-line 1)
  14016. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  14017. (org-match-string-no-properties 1)
  14018. "")))
  14019. (defun org-get-tags ()
  14020. "Get the list of tags specified in the current headline."
  14021. (org-split-string (org-get-tags-string) ":"))
  14022. (defun org-get-buffer-tags ()
  14023. "Get a table of all tags used in the buffer, for completion."
  14024. (org-with-wide-buffer
  14025. (goto-char (point-min))
  14026. (let ((tag-re (concat org-outline-regexp-bol
  14027. "\\(?:.*?[ \t]\\)?"
  14028. (org-re ":\\([[:alnum:]_@#%:]+\\):[ \t]*$")))
  14029. tags)
  14030. (while (re-search-forward tag-re nil t)
  14031. (dolist (tag (org-split-string (org-match-string-no-properties 1) ":"))
  14032. (push tag tags)))
  14033. (mapcar #'list (append org-file-tags (org-uniquify tags))))))
  14034. ;;;; The mapping API
  14035. (defun org-map-entries (func &optional match scope &rest skip)
  14036. "Call FUNC at each headline selected by MATCH in SCOPE.
  14037. FUNC is a function or a lisp form. The function will be called without
  14038. arguments, with the cursor positioned at the beginning of the headline.
  14039. The return values of all calls to the function will be collected and
  14040. returned as a list.
  14041. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  14042. does not need to preserve point. After evaluation, the cursor will be
  14043. moved to the end of the line (presumably of the headline of the
  14044. processed entry) and search continues from there. Under some
  14045. circumstances, this may not produce the wanted results. For example,
  14046. if you have removed (e.g. archived) the current (sub)tree it could
  14047. mean that the next entry will be skipped entirely. In such cases, you
  14048. can specify the position from where search should continue by making
  14049. FUNC set the variable `org-map-continue-from' to the desired buffer
  14050. position.
  14051. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  14052. Only headlines that are matched by this query will be considered during
  14053. the iteration. When MATCH is nil or t, all headlines will be
  14054. visited by the iteration.
  14055. SCOPE determines the scope of this command. It can be any of:
  14056. nil The current buffer, respecting the restriction if any
  14057. tree The subtree started with the entry at point
  14058. region The entries within the active region, if any
  14059. region-start-level
  14060. The entries within the active region, but only those at
  14061. the same level than the first one.
  14062. file The current buffer, without restriction
  14063. file-with-archives
  14064. The current buffer, and any archives associated with it
  14065. agenda All agenda files
  14066. agenda-with-archives
  14067. All agenda files with any archive files associated with them
  14068. \(file1 file2 ...)
  14069. If this is a list, all files in the list will be scanned
  14070. The remaining args are treated as settings for the skipping facilities of
  14071. the scanner. The following items can be given here:
  14072. archive skip trees with the archive tag
  14073. comment skip trees with the COMMENT keyword
  14074. function or Emacs Lisp form:
  14075. will be used as value for `org-agenda-skip-function', so
  14076. whenever the function returns a position, FUNC will not be
  14077. called for that entry and search will continue from the
  14078. position returned
  14079. If your function needs to retrieve the tags including inherited tags
  14080. at the *current* entry, you can use the value of the variable
  14081. `org-scanner-tags' which will be much faster than getting the value
  14082. with `org-get-tags-at'. If your function gets properties with
  14083. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  14084. to t around the call to `org-entry-properties' to get the same speedup.
  14085. Note that if your function moves around to retrieve tags and properties at
  14086. a *different* entry, you cannot use these techniques."
  14087. (unless (and (or (eq scope 'region) (eq scope 'region-start-level))
  14088. (not (org-region-active-p)))
  14089. (let* ((org-agenda-archives-mode nil) ; just to make sure
  14090. (org-agenda-skip-archived-trees (memq 'archive skip))
  14091. (org-agenda-skip-comment-trees (memq 'comment skip))
  14092. (org-agenda-skip-function
  14093. (car (org-delete-all '(comment archive) skip)))
  14094. (org-tags-match-list-sublevels t)
  14095. (start-level (eq scope 'region-start-level))
  14096. matcher file res
  14097. org-todo-keywords-for-agenda
  14098. org-done-keywords-for-agenda
  14099. org-todo-keyword-alist-for-agenda
  14100. org-tag-alist-for-agenda
  14101. todo-only)
  14102. (cond
  14103. ((eq match t) (setq matcher t))
  14104. ((eq match nil) (setq matcher t))
  14105. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  14106. (save-excursion
  14107. (save-restriction
  14108. (cond ((eq scope 'tree)
  14109. (org-back-to-heading t)
  14110. (org-narrow-to-subtree)
  14111. (setq scope nil))
  14112. ((and (or (eq scope 'region) (eq scope 'region-start-level))
  14113. (org-region-active-p))
  14114. ;; If needed, set start-level to a string like "2"
  14115. (when start-level
  14116. (save-excursion
  14117. (goto-char (region-beginning))
  14118. (unless (org-at-heading-p) (outline-next-heading))
  14119. (setq start-level (org-current-level))))
  14120. (narrow-to-region (region-beginning)
  14121. (save-excursion
  14122. (goto-char (region-end))
  14123. (unless (and (bolp) (org-at-heading-p))
  14124. (outline-next-heading))
  14125. (point)))
  14126. (setq scope nil)))
  14127. (if (not scope)
  14128. (progn
  14129. (org-agenda-prepare-buffers
  14130. (and buffer-file-name (list buffer-file-name)))
  14131. (setq res (org-scan-tags func matcher todo-only start-level)))
  14132. ;; Get the right scope
  14133. (cond
  14134. ((and scope (listp scope) (symbolp (car scope)))
  14135. (setq scope (eval scope)))
  14136. ((eq scope 'agenda)
  14137. (setq scope (org-agenda-files t)))
  14138. ((eq scope 'agenda-with-archives)
  14139. (setq scope (org-agenda-files t))
  14140. (setq scope (org-add-archive-files scope)))
  14141. ((eq scope 'file)
  14142. (setq scope (and buffer-file-name (list buffer-file-name))))
  14143. ((eq scope 'file-with-archives)
  14144. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  14145. (org-agenda-prepare-buffers scope)
  14146. (dolist (file scope)
  14147. (with-current-buffer (org-find-base-buffer-visiting file)
  14148. (save-excursion
  14149. (save-restriction
  14150. (widen)
  14151. (goto-char (point-min))
  14152. (setq res (append res (org-scan-tags func matcher todo-only))))))))))
  14153. res)))
  14154. ;;; Properties API
  14155. (defconst org-special-properties
  14156. '("ALLTAGS" "BLOCKED" "CLOCKSUM" "CLOCKSUM_T" "CLOSED" "DEADLINE" "FILE"
  14157. "ITEM" "PRIORITY" "SCHEDULED" "TAGS" "TIMESTAMP" "TIMESTAMP_IA" "TODO")
  14158. "The special properties valid in Org mode.
  14159. These are properties that are not defined in the property drawer,
  14160. but in some other way.")
  14161. (defconst org-default-properties
  14162. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  14163. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  14164. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  14165. "EXPORT_OPTIONS" "EXPORT_TEXT" "EXPORT_FILE_NAME"
  14166. "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE" "UNNUMBERED"
  14167. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER" "REPEAT_TO_STATE"
  14168. "CLOCK_MODELINE_TOTAL" "STYLE" "HTML_CONTAINER_CLASS")
  14169. "Some properties that are used by Org mode for various purposes.
  14170. Being in this list makes sure that they are offered for completion.")
  14171. (defun org--update-property-plist (key val props)
  14172. "Associate KEY to VAL in alist PROPS.
  14173. Modifications are made by side-effect. Return new alist."
  14174. (let* ((appending (string= (substring key -1) "+"))
  14175. (key (if appending (substring key 0 -1) key))
  14176. (old (assoc-string key props t)))
  14177. (if (not old) (cons (cons key val) props)
  14178. (setcdr old (if appending (concat (cdr old) " " val) val))
  14179. props)))
  14180. (defun org-get-property-block (&optional beg force)
  14181. "Return the (beg . end) range of the body of the property drawer.
  14182. BEG is the beginning of the current subtree, or of the part
  14183. before the first headline. If it is not given, it will be found.
  14184. If the drawer does not exist, create it if FORCE is non-nil, or
  14185. return nil."
  14186. (org-with-wide-buffer
  14187. (when beg (goto-char beg))
  14188. (unless (org-before-first-heading-p)
  14189. (let ((beg (cond (beg)
  14190. ((or (not (featurep 'org-inlinetask))
  14191. (org-inlinetask-in-task-p))
  14192. (org-back-to-heading t))
  14193. (t (org-with-limited-levels (org-back-to-heading t))))))
  14194. (forward-line)
  14195. (when (org-looking-at-p org-planning-line-re) (forward-line))
  14196. (cond ((looking-at org-property-drawer-re)
  14197. (forward-line)
  14198. (cons (point) (progn (goto-char (match-end 0))
  14199. (line-beginning-position))))
  14200. (force
  14201. (goto-char beg)
  14202. (org-insert-property-drawer)
  14203. (let ((pos (save-excursion (search-forward ":END:")
  14204. (line-beginning-position))))
  14205. (cons pos pos))))))))
  14206. (defun org-at-property-p ()
  14207. "Non-nil when point is inside a property drawer.
  14208. See `org-property-re' for match data, if applicable."
  14209. (save-excursion
  14210. (beginning-of-line)
  14211. (and (looking-at org-property-re)
  14212. (let ((property-drawer (save-match-data (org-get-property-block))))
  14213. (and property-drawer
  14214. (>= (point) (car property-drawer))
  14215. (< (point) (cdr property-drawer)))))))
  14216. (defun org-property-action ()
  14217. "Do an action on properties."
  14218. (interactive)
  14219. (unless (org-at-property-p) (user-error "Not at a property"))
  14220. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  14221. (let ((c (read-char-exclusive)))
  14222. (case c
  14223. (?s (call-interactively #'org-set-property))
  14224. (?d (call-interactively #'org-delete-property))
  14225. (?D (call-interactively #'org-delete-property-globally))
  14226. (?c (call-interactively #'org-compute-property-at-point))
  14227. (otherwise (user-error "No such property action %c" c)))))
  14228. (defun org-inc-effort ()
  14229. "Increment the value of the effort property in the current entry."
  14230. (interactive)
  14231. (org-set-effort nil t))
  14232. (defvar org-clock-effort) ; Defined in org-clock.el.
  14233. (defvar org-clock-current-task) ; Defined in org-clock.el.
  14234. (defun org-set-effort (&optional value increment)
  14235. "Set the effort property of the current entry.
  14236. With numerical prefix arg, use the nth allowed value, 0 stands for the
  14237. 10th allowed value.
  14238. When INCREMENT is non-nil, set the property to the next allowed value."
  14239. (interactive "P")
  14240. (when (equal value 0) (setq value 10))
  14241. (let* ((completion-ignore-case t)
  14242. (prop org-effort-property)
  14243. (cur (org-entry-get nil prop))
  14244. (allowed (org-property-get-allowed-values nil prop 'table))
  14245. (existing (mapcar 'list (org-property-values prop)))
  14246. (heading (nth 4 (org-heading-components)))
  14247. rpl
  14248. (val (cond
  14249. ((stringp value) value)
  14250. ((and allowed (integerp value))
  14251. (or (car (nth (1- value) allowed))
  14252. (car (org-last allowed))))
  14253. ((and allowed increment)
  14254. (or (caadr (member (list cur) allowed))
  14255. (user-error "Allowed effort values are not set")))
  14256. (allowed
  14257. (message "Select 1-9,0, [RET%s]: %s"
  14258. (if cur (concat "=" cur) "")
  14259. (mapconcat 'car allowed " "))
  14260. (setq rpl (read-char-exclusive))
  14261. (if (equal rpl ?\r)
  14262. cur
  14263. (setq rpl (- rpl ?0))
  14264. (when (equal rpl 0) (setq rpl 10))
  14265. (if (and (> rpl 0) (<= rpl (length allowed)))
  14266. (car (nth (1- rpl) allowed))
  14267. (org-completing-read "Effort: " allowed nil))))
  14268. (t
  14269. (let (org-completion-use-ido org-completion-use-iswitchb)
  14270. (org-completing-read
  14271. (concat "Effort " (if (and cur (string-match "\\S-" cur))
  14272. (concat "[" cur "]") "")
  14273. ": ")
  14274. existing nil nil "" nil cur))))))
  14275. (unless (equal (org-entry-get nil prop) val)
  14276. (org-entry-put nil prop val))
  14277. (org-refresh-property
  14278. '((effort . identity)
  14279. (effort-minutes . org-duration-string-to-minutes))
  14280. val)
  14281. (when (string= heading org-clock-current-task)
  14282. (setq org-clock-effort (get-text-property (point-at-bol) 'effort))
  14283. (org-clock-update-mode-line))
  14284. (message "%s is now %s" prop val)))
  14285. (defun org-entry-properties (&optional pom which)
  14286. "Get all properties of the current entry.
  14287. When POM is a buffer position, get all properties from the entry
  14288. there instead.
  14289. This includes the TODO keyword, the tags, time strings for
  14290. deadline, scheduled, and clocking, and any additional properties
  14291. defined in the entry.
  14292. If WHICH is nil or `all', get all properties. If WHICH is
  14293. `special' or `standard', only get that subclass. If WHICH is
  14294. a string, only get that property.
  14295. Return value is an alist. Keys are properties, as upcased
  14296. strings."
  14297. (org-with-point-at pom
  14298. (when (and (derived-mode-p 'org-mode)
  14299. (ignore-errors (org-back-to-heading t)))
  14300. (catch 'exit
  14301. (let* ((beg (point))
  14302. (specific (and (stringp which) (upcase which)))
  14303. (which (cond ((not specific) which)
  14304. ((member specific org-special-properties) 'special)
  14305. (t 'standard)))
  14306. props)
  14307. ;; Get the special properties, like TODO and TAGS.
  14308. (when (memq which '(nil all special))
  14309. (when (or (not specific) (string= specific "CLOCKSUM"))
  14310. (let ((clocksum (get-text-property (point) :org-clock-minutes)))
  14311. (when clocksum
  14312. (push (cons "CLOCKSUM"
  14313. (org-columns-number-to-string
  14314. (/ (float clocksum) 60.) 'add_times))
  14315. props)))
  14316. (when specific (throw 'exit props)))
  14317. (when (or (not specific) (string= specific "CLOCKSUM_T"))
  14318. (let ((clocksumt (get-text-property (point)
  14319. :org-clock-minutes-today)))
  14320. (when clocksumt
  14321. (push (cons "CLOCKSUM_T"
  14322. (org-columns-number-to-string
  14323. (/ (float clocksumt) 60.) 'add_times))
  14324. props)))
  14325. (when specific (throw 'exit props)))
  14326. (when (or (not specific) (string= specific "ITEM"))
  14327. (when (looking-at org-complex-heading-regexp)
  14328. (push (cons "ITEM"
  14329. (concat
  14330. (org-match-string-no-properties 1)
  14331. (let ((title (org-match-string-no-properties 4)))
  14332. (when (org-string-nw-p title)
  14333. (concat " " (org-remove-tabs title))))))
  14334. props))
  14335. (when specific (throw 'exit props)))
  14336. (when (or (not specific) (string= specific "TODO"))
  14337. (let ((case-fold-search nil))
  14338. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  14339. (push (cons "TODO" (org-match-string-no-properties 2)) props)))
  14340. (when specific (throw 'exit props)))
  14341. (when (or (not specific) (string= specific "PRIORITY"))
  14342. (push (cons "PRIORITY"
  14343. (if (looking-at org-priority-regexp)
  14344. (org-match-string-no-properties 2)
  14345. (char-to-string org-default-priority)))
  14346. props)
  14347. (when specific (throw 'exit props)))
  14348. (when (or (not specific) (string= specific "FILE"))
  14349. (push (cons "FILE" (buffer-file-name (buffer-base-buffer)))
  14350. props)
  14351. (when specific (throw 'exit props)))
  14352. (when (or (not specific) (string= specific "TAGS"))
  14353. (let ((value (org-string-nw-p (org-get-tags-string))))
  14354. (when value (push (cons "TAGS" value) props)))
  14355. (when specific (throw 'exit props)))
  14356. (when (or (not specific) (string= specific "ALLTAGS"))
  14357. (let ((value (org-get-tags-at)))
  14358. (when value
  14359. (push (cons "ALLTAGS"
  14360. (format ":%s:" (mapconcat #'identity value ":")))
  14361. props)))
  14362. (when specific (throw 'exit props)))
  14363. (when (or (not specific) (string= specific "BLOCKED"))
  14364. (push (cons "BLOCKED" (if (org-entry-blocked-p) "t" "")) props)
  14365. (when specific (throw 'exit props)))
  14366. (when (or (not specific)
  14367. (member specific '("CLOSED" "DEADLINE" "SCHEDULED")))
  14368. (forward-line)
  14369. (when (org-looking-at-p org-planning-line-re)
  14370. (end-of-line)
  14371. (let ((bol (line-beginning-position))
  14372. ;; Backward compatibility: time keywords used to
  14373. ;; be configurable (before 8.3). Make sure we
  14374. ;; get the correct keyword.
  14375. (key-assoc `(("CLOSED" . ,org-closed-string)
  14376. ("DEADLINE" . ,org-deadline-string)
  14377. ("SCHEDULED" . ,org-scheduled-string))))
  14378. (dolist (pair (if specific (list (assoc specific key-assoc))
  14379. key-assoc))
  14380. (save-excursion
  14381. (when (search-backward (cdr pair) bol t)
  14382. (goto-char (match-end 0))
  14383. (skip-chars-forward " \t")
  14384. (and (looking-at org-ts-regexp-both)
  14385. (push (cons (car pair)
  14386. (org-match-string-no-properties 0))
  14387. props)))))))
  14388. (when specific (throw 'exit props)))
  14389. (when (or (not specific)
  14390. (member specific '("TIMESTAMP" "TIMESTAMP_IA")))
  14391. (let ((find-ts
  14392. (lambda (end ts)
  14393. (let ((regexp (cond
  14394. ((string= specific "TIMESTAMP")
  14395. org-ts-regexp)
  14396. ((string= specific "TIMESTAMP_IA")
  14397. org-ts-regexp-inactive)
  14398. ((assoc "TIMESTAMP_IA" ts)
  14399. org-ts-regexp)
  14400. ((assoc "TIMESTAMP" ts)
  14401. org-ts-regexp-inactive)
  14402. (t org-ts-regexp-both))))
  14403. (catch 'next
  14404. (while (re-search-forward regexp end t)
  14405. (backward-char)
  14406. (let ((object (org-element-context)))
  14407. ;; Accept to match timestamps in node
  14408. ;; properties, too.
  14409. (when (memq (org-element-type object)
  14410. '(node-property timestamp))
  14411. (let ((type
  14412. (org-element-property :type object)))
  14413. (cond
  14414. ((and (memq type '(active active-range))
  14415. (not (equal specific "TIMESTAMP_IA")))
  14416. (unless (assoc "TIMESTAMP" ts)
  14417. (push (cons "TIMESTAMP"
  14418. (org-element-property
  14419. :raw-value object))
  14420. ts)
  14421. (when specific (throw 'exit ts))))
  14422. ((and (memq type '(inactive inactive-range))
  14423. (not (string= specific "TIMESTAMP")))
  14424. (unless (assoc "TIMESTAMP_IA" ts)
  14425. (push (cons "TIMESTAMP_IA"
  14426. (org-element-property
  14427. :raw-value object))
  14428. ts)
  14429. (when specific (throw 'exit ts))))))
  14430. ;; Both timestamp types are found,
  14431. ;; move to next part.
  14432. (when (= (length ts) 2) (throw 'next ts)))))
  14433. ts)))))
  14434. (goto-char beg)
  14435. ;; First look for timestamps within headline.
  14436. (let ((ts (funcall find-ts (line-end-position) nil)))
  14437. (if (= (length ts) 2) (setq props (nconc ts props))
  14438. (forward-line)
  14439. ;; Then find timestamps in the section, skipping
  14440. ;; planning line.
  14441. (when (org-looking-at-p org-planning-line-re)
  14442. (forward-line))
  14443. (let ((end (save-excursion (outline-next-heading))))
  14444. (setq props (nconc (funcall find-ts end ts) props))))))))
  14445. ;; Get the standard properties, like :PROP:.
  14446. (when (memq which '(nil all standard))
  14447. ;; If we are looking after a specific property, delegate
  14448. ;; to `org-entry-get', which is faster. However, make an
  14449. ;; exception for "CATEGORY", since it can be also set
  14450. ;; through keywords (i.e. #+CATEGORY).
  14451. (if (and specific (not (equal specific "CATEGORY")))
  14452. (let ((value (org-entry-get beg specific nil t)))
  14453. (throw 'exit (and value (list (cons specific value)))))
  14454. (let ((range (org-get-property-block beg)))
  14455. (when range
  14456. (let ((end (cdr range)) seen-base)
  14457. (goto-char (car range))
  14458. ;; Unlike to `org--update-property-plist', we
  14459. ;; handle the case where base values is found
  14460. ;; after its extension. We also forbid standard
  14461. ;; properties to be named as special properties.
  14462. (while (re-search-forward org-property-re end t)
  14463. (let* ((key (upcase (org-match-string-no-properties 2)))
  14464. (extendp (org-string-match-p "\\+\\'" key))
  14465. (key-base (if extendp (substring key 0 -1) key))
  14466. (value (org-match-string-no-properties 3)))
  14467. (cond
  14468. ((member-ignore-case key-base org-special-properties))
  14469. (extendp
  14470. (setq props
  14471. (org--update-property-plist key value props)))
  14472. ((member key seen-base))
  14473. (t (push key seen-base)
  14474. (let ((p (assoc-string key props t)))
  14475. (if p (setcdr p (concat value " " (cdr p)))
  14476. (push (cons key value) props))))))))))))
  14477. (unless (assoc "CATEGORY" props)
  14478. (push (cons "CATEGORY" (org-get-category beg)) props)
  14479. (when (string= specific "CATEGORY") (throw 'exit props)))
  14480. ;; Return value.
  14481. props)))))
  14482. (defun org-property--local-values (property literal-nil)
  14483. "Return value for PROPERTY in current entry.
  14484. Value is a list whose care is the base value for PROPERTY and cdr
  14485. a list of accumulated values. Return nil if neither is found in
  14486. the entry. Also return nil when PROPERTY is set to \"nil\",
  14487. unless LITERAL-NIL is non-nil."
  14488. (let ((range (org-get-property-block)))
  14489. (when range
  14490. (goto-char (car range))
  14491. (let* ((case-fold-search t)
  14492. (end (cdr range))
  14493. (value
  14494. ;; Base value.
  14495. (save-excursion
  14496. (let ((v (and (re-search-forward
  14497. (org-re-property property nil t) end t)
  14498. (org-match-string-no-properties 3))))
  14499. (list (if literal-nil v (org-not-nil v)))))))
  14500. ;; Find additional values.
  14501. (let* ((property+ (org-re-property (concat property "+") nil t)))
  14502. (while (re-search-forward property+ end t)
  14503. (push (org-match-string-no-properties 3) value)))
  14504. ;; Return final values.
  14505. (and (not (equal value '(nil))) (nreverse value))))))
  14506. (defun org-entry-get (pom property &optional inherit literal-nil)
  14507. "Get value of PROPERTY for entry or content at point-or-marker POM.
  14508. If INHERIT is non-nil and the entry does not have the property,
  14509. then also check higher levels of the hierarchy. If INHERIT is
  14510. the symbol `selective', use inheritance only if the setting in
  14511. `org-use-property-inheritance' selects PROPERTY for inheritance.
  14512. If the property is present but empty, the return value is the
  14513. empty string. If the property is not present at all, nil is
  14514. returned. In any other case, return the value as a string.
  14515. Search is case-insensitive.
  14516. If LITERAL-NIL is set, return the string value \"nil\" as
  14517. a string, do not interpret it as the list atom nil. This is used
  14518. for inheritance when a \"nil\" value can supersede a non-nil
  14519. value higher up the hierarchy."
  14520. (org-with-point-at pom
  14521. (cond
  14522. ((member-ignore-case property (cons "CATEGORY" org-special-properties))
  14523. ;; We need a special property. Use `org-entry-properties' to
  14524. ;; retrieve it, but specify the wanted property.
  14525. (cdr (assoc-string property (org-entry-properties nil property))))
  14526. ((and inherit
  14527. (or (not (eq inherit 'selective)) (org-property-inherit-p property)))
  14528. (org-entry-get-with-inheritance property literal-nil))
  14529. (t
  14530. (let* ((local (org-property--local-values property literal-nil))
  14531. (value (and local (mapconcat #'identity (delq nil local) " "))))
  14532. (if literal-nil value (org-not-nil value)))))))
  14533. (defun org-property-or-variable-value (var &optional inherit)
  14534. "Check if there is a property fixing the value of VAR.
  14535. If yes, return this value. If not, return the current value of the variable."
  14536. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  14537. (if (and prop (stringp prop) (string-match "\\S-" prop))
  14538. (read prop)
  14539. (symbol-value var))))
  14540. (defun org-entry-delete (pom property)
  14541. "Delete PROPERTY from entry at point-or-marker POM.
  14542. Accumulated properties, i.e. PROPERTY+, are also removed. Return
  14543. non-nil when a property was removed."
  14544. (unless (member property org-special-properties)
  14545. (org-with-point-at pom
  14546. (let ((range (org-get-property-block)))
  14547. (when range
  14548. (let* ((begin (car range))
  14549. (origin (cdr range))
  14550. (end (copy-marker origin))
  14551. (re (org-re-property
  14552. (concat (regexp-quote property) "\\+?") t t)))
  14553. (goto-char begin)
  14554. (while (re-search-forward re end t)
  14555. (delete-region (match-beginning 0) (line-beginning-position 2)))
  14556. ;; If drawer is empty, remove it altogether.
  14557. (when (= begin end)
  14558. (delete-region (line-beginning-position 0)
  14559. (line-beginning-position 2)))
  14560. ;; Return non-nil if some property was removed.
  14561. (prog1 (/= end origin) (set-marker end nil))))))))
  14562. ;; Multi-values properties are properties that contain multiple values
  14563. ;; These values are assumed to be single words, separated by whitespace.
  14564. (defun org-entry-add-to-multivalued-property (pom property value)
  14565. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  14566. (let* ((old (org-entry-get pom property))
  14567. (values (and old (org-split-string old "[ \t]"))))
  14568. (setq value (org-entry-protect-space value))
  14569. (unless (member value values)
  14570. (setq values (append values (list value)))
  14571. (org-entry-put pom property
  14572. (mapconcat 'identity values " ")))))
  14573. (defun org-entry-remove-from-multivalued-property (pom property value)
  14574. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  14575. (let* ((old (org-entry-get pom property))
  14576. (values (and old (org-split-string old "[ \t]"))))
  14577. (setq value (org-entry-protect-space value))
  14578. (when (member value values)
  14579. (setq values (delete value values))
  14580. (org-entry-put pom property
  14581. (mapconcat 'identity values " ")))))
  14582. (defun org-entry-member-in-multivalued-property (pom property value)
  14583. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  14584. (let* ((old (org-entry-get pom property))
  14585. (values (and old (org-split-string old "[ \t]"))))
  14586. (setq value (org-entry-protect-space value))
  14587. (member value values)))
  14588. (defun org-entry-get-multivalued-property (pom property)
  14589. "Return a list of values in a multivalued property."
  14590. (let* ((value (org-entry-get pom property))
  14591. (values (and value (org-split-string value "[ \t]"))))
  14592. (mapcar 'org-entry-restore-space values)))
  14593. (defun org-entry-put-multivalued-property (pom property &rest values)
  14594. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  14595. VALUES should be a list of strings. Spaces will be protected."
  14596. (org-entry-put pom property
  14597. (mapconcat 'org-entry-protect-space values " "))
  14598. (let* ((value (org-entry-get pom property))
  14599. (values (and value (org-split-string value "[ \t]"))))
  14600. (mapcar 'org-entry-restore-space values)))
  14601. (defun org-entry-protect-space (s)
  14602. "Protect spaces and newline in string S."
  14603. (while (string-match " " s)
  14604. (setq s (replace-match "%20" t t s)))
  14605. (while (string-match "\n" s)
  14606. (setq s (replace-match "%0A" t t s)))
  14607. s)
  14608. (defun org-entry-restore-space (s)
  14609. "Restore spaces and newline in string S."
  14610. (while (string-match "%20" s)
  14611. (setq s (replace-match " " t t s)))
  14612. (while (string-match "%0A" s)
  14613. (setq s (replace-match "\n" t t s)))
  14614. s)
  14615. (defvar org-entry-property-inherited-from (make-marker)
  14616. "Marker pointing to the entry from where a property was inherited.
  14617. Each call to `org-entry-get-with-inheritance' will set this marker to the
  14618. location of the entry where the inheritance search matched. If there was
  14619. no match, the marker will point nowhere.
  14620. Note that also `org-entry-get' calls this function, if the INHERIT flag
  14621. is set.")
  14622. (defun org-entry-get-with-inheritance (property &optional literal-nil)
  14623. "Get PROPERTY of entry or content at point, search higher levels if needed.
  14624. The search will stop at the first ancestor which has the property defined.
  14625. If the value found is \"nil\", return nil to show that the property
  14626. should be considered as undefined (this is the meaning of nil here).
  14627. However, if LITERAL-NIL is set, return the string value \"nil\" instead."
  14628. (move-marker org-entry-property-inherited-from nil)
  14629. (org-with-wide-buffer
  14630. (let (value)
  14631. (catch 'exit
  14632. (while t
  14633. (let ((v (org-property--local-values property literal-nil)))
  14634. (when v
  14635. (setq value
  14636. (concat (mapconcat #'identity (delq nil v) " ")
  14637. (and value " ")
  14638. value)))
  14639. (cond
  14640. ((car v)
  14641. (org-back-to-heading t)
  14642. (move-marker org-entry-property-inherited-from (point))
  14643. (throw 'exit nil))
  14644. ((org-up-heading-safe))
  14645. (t
  14646. (let ((global
  14647. (cdr (or (assoc-string property org-file-properties t)
  14648. (assoc-string property org-global-properties t)
  14649. (assoc-string property org-global-properties-fixed t)))))
  14650. (cond ((not global))
  14651. (value (setq value (concat global " " value)))
  14652. (t (setq value global))))
  14653. (throw 'exit nil))))))
  14654. (if literal-nil value (org-not-nil value)))))
  14655. (defvar org-property-changed-functions nil
  14656. "Hook called when the value of a property has changed.
  14657. Each hook function should accept two arguments, the name of the property
  14658. and the new value.")
  14659. (defun org-entry-put (pom property value)
  14660. "Set PROPERTY to VALUE for entry at point-or-marker POM.
  14661. If the value is nil, it is converted to the empty string. If
  14662. it is not a string, an error is raised.
  14663. PROPERTY can be any regular property (see
  14664. `org-special-properties'). It can also be \"TODO\",
  14665. \"PRIORITY\", \"SCHEDULED\" and \"DEADLINE\".
  14666. For the last two properties, VALUE may have any of the special
  14667. values \"earlier\" and \"later\". The function then increases or
  14668. decreases scheduled or deadline date by one day."
  14669. (cond ((null value) (setq value ""))
  14670. ((not (stringp value)) (error "Properties values should be strings")))
  14671. (org-with-point-at pom
  14672. (if (or (not (featurep 'org-inlinetask)) (org-inlinetask-in-task-p))
  14673. (org-back-to-heading t)
  14674. (org-with-limited-levels (org-back-to-heading t)))
  14675. (let ((beg (point)))
  14676. (cond
  14677. ((equal property "TODO")
  14678. (cond ((not (org-string-nw-p value)) (setq value 'none))
  14679. ((not (member value org-todo-keywords-1))
  14680. (user-error "\"%s\" is not a valid TODO state" value)))
  14681. (org-todo value)
  14682. (org-set-tags nil 'align))
  14683. ((equal property "PRIORITY")
  14684. (org-priority (if (org-string-nw-p value) (string-to-char value) ?\s))
  14685. (org-set-tags nil 'align))
  14686. ((equal property "SCHEDULED")
  14687. (forward-line)
  14688. (if (and (org-looking-at-p org-planning-line-re)
  14689. (re-search-forward
  14690. org-scheduled-time-regexp (line-end-position) t))
  14691. (cond ((string= value "earlier") (org-timestamp-change -1 'day))
  14692. ((string= value "later") (org-timestamp-change 1 'day))
  14693. ((string= value "") (org-schedule '(4)))
  14694. (t (org-schedule nil value)))
  14695. (if (member value '("earlier" "later" ""))
  14696. (call-interactively #'org-schedule)
  14697. (org-schedule nil value))))
  14698. ((equal property "DEADLINE")
  14699. (forward-line)
  14700. (if (and (org-looking-at-p org-planning-line-re)
  14701. (re-search-forward
  14702. org-deadline-time-regexp (line-end-position) t))
  14703. (cond ((string= value "earlier") (org-timestamp-change -1 'day))
  14704. ((string= value "later") (org-timestamp-change 1 'day))
  14705. ((string= value "") (org-deadline '(4)))
  14706. (t (org-deadline nil value)))
  14707. (if (member value '("earlier" "later" ""))
  14708. (call-interactively #'org-deadline)
  14709. (org-deadline nil value))))
  14710. ((member property org-special-properties)
  14711. (error "The %s property cannot be set with `org-entry-put'" property))
  14712. (t
  14713. (let* ((range (org-get-property-block beg 'force))
  14714. (end (cdr range))
  14715. (case-fold-search t))
  14716. (goto-char (car range))
  14717. (if (re-search-forward (org-re-property property nil t) end t)
  14718. (progn (delete-region (match-beginning 0) (match-end 0))
  14719. (goto-char (match-beginning 0)))
  14720. (goto-char end)
  14721. (insert "\n")
  14722. (backward-char))
  14723. (insert ":" property ":")
  14724. (when value (insert " " value))
  14725. (org-indent-line)))))
  14726. (run-hook-with-args 'org-property-changed-functions property value)))
  14727. (defun org-buffer-property-keys
  14728. (&optional specials defaults columns ignore-malformed)
  14729. "Get all property keys in the current buffer.
  14730. When SPECIALS is non-nil, also list the special properties that
  14731. reflect things like tags and TODO state.
  14732. When DEFAULTS is non-nil, also include properties that has
  14733. special meaning internally: ARCHIVE, CATEGORY, SUMMARY,
  14734. DESCRIPTION, LOCATION, and LOGGING and others.
  14735. When COLUMNS in non-nil, also include property names given in
  14736. COLUMN formats in the current buffer.
  14737. When IGNORE-MALFORMED is non-nil, malformed drawer repair will not be
  14738. automatically performed, such drawers will be silently ignored."
  14739. (let ((case-fold-search t)
  14740. (props (append
  14741. (and specials org-special-properties)
  14742. (and defaults (cons org-effort-property org-default-properties))
  14743. nil)))
  14744. (org-with-wide-buffer
  14745. (goto-char (point-min))
  14746. (while (re-search-forward org-property-start-re nil t)
  14747. (let ((range (org-get-property-block)))
  14748. (catch 'skip
  14749. (unless range
  14750. (when (and (not ignore-malformed)
  14751. (not (org-before-first-heading-p))
  14752. (y-or-n-p (format "Malformed drawer at %d, repair?"
  14753. (line-beginning-position))))
  14754. (org-get-property-block nil t))
  14755. (throw 'skip nil))
  14756. (goto-char (car range))
  14757. (let ((begin (car range))
  14758. (end (cdr range)))
  14759. ;; Make sure that found property block is not located
  14760. ;; before current point, as it would generate an infloop.
  14761. ;; It can happen, for example, in the following
  14762. ;; situation:
  14763. ;;
  14764. ;; * Headline
  14765. ;; :PROPERTIES:
  14766. ;; ...
  14767. ;; :END:
  14768. ;; *************** Inlinetask
  14769. ;; #+BEGIN_EXAMPLE
  14770. ;; :PROPERTIES:
  14771. ;; #+END_EXAMPLE
  14772. ;;
  14773. (if (< begin (point)) (throw 'skip nil) (goto-char begin))
  14774. (while (< (point) end)
  14775. (let ((p (progn (looking-at org-property-re)
  14776. (org-match-string-no-properties 2))))
  14777. ;; Only add true property name, not extension symbol.
  14778. (add-to-list 'props
  14779. (if (not (org-string-match-p "\\+\\'" p)) p
  14780. (substring p 0 -1))))
  14781. (forward-line))))
  14782. (outline-next-heading)))
  14783. (when columns
  14784. (goto-char (point-min))
  14785. (while (re-search-forward "^[ \t]*\\(?:#\\+\\|:\\)COLUMNS:" nil t)
  14786. (let ((element (org-element-at-point)))
  14787. (when (memq (org-element-type element) '(keyword node-property))
  14788. (let ((value (org-element-property :value element))
  14789. (start 0))
  14790. (while (string-match "%[0-9]*\\(\\S-+\\)" value start)
  14791. (setq start (match-end 0))
  14792. (let ((p (org-match-string-no-properties 1 value)))
  14793. (unless (member-ignore-case p org-special-properties)
  14794. (add-to-list 'props p))))))))))
  14795. (sort props (lambda (a b) (string< (upcase a) (upcase b))))))
  14796. (defun org-property-values (key)
  14797. "List all non-nil values of property KEY in current buffer."
  14798. (org-with-wide-buffer
  14799. (goto-char (point-min))
  14800. (let ((case-fold-search t)
  14801. (re (org-re-property key))
  14802. values)
  14803. (while (re-search-forward re nil t)
  14804. (add-to-list 'values (org-entry-get (point) key)))
  14805. values)))
  14806. (defun org-insert-property-drawer ()
  14807. "Insert a property drawer into the current entry."
  14808. (org-with-wide-buffer
  14809. (if (or (not (featurep 'org-inlinetask)) (org-inlinetask-in-task-p))
  14810. (org-back-to-heading t)
  14811. (org-with-limited-levels (org-back-to-heading t)))
  14812. (forward-line)
  14813. (when (org-looking-at-p org-planning-line-re) (forward-line))
  14814. (unless (org-looking-at-p org-property-drawer-re)
  14815. ;; Make sure we start editing a line from current entry, not from
  14816. ;; next one. It prevents extending text properties or overlays
  14817. ;; belonging to the latter.
  14818. (when (bolp) (backward-char))
  14819. (let ((begin (1+ (point)))
  14820. (inhibit-read-only t))
  14821. (insert "\n:PROPERTIES:\n:END:")
  14822. (when (eobp) (insert "\n"))
  14823. (org-indent-region begin (point))))))
  14824. (defun org-insert-drawer (&optional arg drawer)
  14825. "Insert a drawer at point.
  14826. When optional argument ARG is non-nil, insert a property drawer.
  14827. Optional argument DRAWER, when non-nil, is a string representing
  14828. drawer's name. Otherwise, the user is prompted for a name.
  14829. If a region is active, insert the drawer around that region
  14830. instead.
  14831. Point is left between drawer's boundaries."
  14832. (interactive "P")
  14833. (let* ((drawer (if arg "PROPERTIES"
  14834. (or drawer (read-from-minibuffer "Drawer: ")))))
  14835. (cond
  14836. ;; With C-u, fall back on `org-insert-property-drawer'
  14837. (arg (org-insert-property-drawer))
  14838. ;; Check validity of suggested drawer's name.
  14839. ((not (org-string-match-p org-drawer-regexp (format ":%s:" drawer)))
  14840. (user-error "Invalid drawer name"))
  14841. ;; With an active region, insert a drawer at point.
  14842. ((not (org-region-active-p))
  14843. (progn
  14844. (unless (bolp) (insert "\n"))
  14845. (insert (format ":%s:\n\n:END:\n" drawer))
  14846. (forward-line -2)))
  14847. ;; Otherwise, insert the drawer at point
  14848. (t
  14849. (let ((rbeg (region-beginning))
  14850. (rend (copy-marker (region-end))))
  14851. (unwind-protect
  14852. (progn
  14853. (goto-char rbeg)
  14854. (beginning-of-line)
  14855. (when (save-excursion
  14856. (re-search-forward org-outline-regexp-bol rend t))
  14857. (user-error "Drawers cannot contain headlines"))
  14858. ;; Position point at the beginning of the first
  14859. ;; non-blank line in region. Insert drawer's opening
  14860. ;; there, then indent it.
  14861. (org-skip-whitespace)
  14862. (beginning-of-line)
  14863. (insert ":" drawer ":\n")
  14864. (forward-line -1)
  14865. (indent-for-tab-command)
  14866. ;; Move point to the beginning of the first blank line
  14867. ;; after the last non-blank line in region. Insert
  14868. ;; drawer's closing, then indent it.
  14869. (goto-char rend)
  14870. (skip-chars-backward " \r\t\n")
  14871. (insert "\n:END:")
  14872. (deactivate-mark t)
  14873. (indent-for-tab-command)
  14874. (unless (eolp) (insert "\n")))
  14875. ;; Clear marker, whatever the outcome of insertion is.
  14876. (set-marker rend nil)))))))
  14877. (defvar org-property-set-functions-alist nil
  14878. "Property set function alist.
  14879. Each entry should have the following format:
  14880. (PROPERTY . READ-FUNCTION)
  14881. The read function will be called with the same argument as
  14882. `org-completing-read'.")
  14883. (defun org-set-property-function (property)
  14884. "Get the function that should be used to set PROPERTY.
  14885. This is computed according to `org-property-set-functions-alist'."
  14886. (or (cdr (assoc property org-property-set-functions-alist))
  14887. 'org-completing-read))
  14888. (defun org-read-property-value (property)
  14889. "Read PROPERTY value from user."
  14890. (let* ((completion-ignore-case t)
  14891. (allowed (org-property-get-allowed-values nil property 'table))
  14892. (cur (org-entry-get nil property))
  14893. (prompt (concat property " value"
  14894. (if (and cur (string-match "\\S-" cur))
  14895. (concat " [" cur "]") "") ": "))
  14896. (set-function (org-set-property-function property))
  14897. (val (if allowed
  14898. (funcall set-function prompt allowed nil
  14899. (not (get-text-property 0 'org-unrestricted
  14900. (caar allowed))))
  14901. (let (org-completion-use-ido org-completion-use-iswitchb)
  14902. (funcall set-function prompt
  14903. (mapcar 'list (org-property-values property))
  14904. nil nil "" nil cur)))))
  14905. (org-trim val)))
  14906. (defvar org-last-set-property nil)
  14907. (defvar org-last-set-property-value nil)
  14908. (defun org-read-property-name ()
  14909. "Read a property name."
  14910. (let ((completion-ignore-case t)
  14911. (default-prop (or (and (org-at-property-p)
  14912. (org-match-string-no-properties 2))
  14913. org-last-set-property)))
  14914. (org-completing-read
  14915. (concat "Property"
  14916. (if default-prop (concat " [" default-prop "]") "")
  14917. ": ")
  14918. (mapcar #'list (org-buffer-property-keys nil t t))
  14919. nil nil nil nil default-prop)))
  14920. (defun org-set-property-and-value (use-last)
  14921. "Allow to set [PROPERTY]: [value] direction from prompt.
  14922. When use-default, don't even ask, just use the last
  14923. \"[PROPERTY]: [value]\" string from the history."
  14924. (interactive "P")
  14925. (let* ((completion-ignore-case t)
  14926. (pv (or (and use-last org-last-set-property-value)
  14927. (org-completing-read
  14928. "Enter a \"[Property]: [value]\" pair: "
  14929. nil nil nil nil nil
  14930. org-last-set-property-value)))
  14931. prop val)
  14932. (when (string-match "^[ \t]*\\([^:]+\\):[ \t]*\\(.*\\)[ \t]*$" pv)
  14933. (setq prop (match-string 1 pv)
  14934. val (match-string 2 pv))
  14935. (org-set-property prop val))))
  14936. (defun org-set-property (property value)
  14937. "In the current entry, set PROPERTY to VALUE.
  14938. When called interactively, this will prompt for a property name, offering
  14939. completion on existing and default properties. And then it will prompt
  14940. for a value, offering completion either on allowed values (via an inherited
  14941. xxx_ALL property) or on existing values in other instances of this property
  14942. in the current file."
  14943. (interactive (list nil nil))
  14944. (let* ((property (or property (org-read-property-name)))
  14945. (value (or value (org-read-property-value property)))
  14946. (fn (cdr (assoc property org-properties-postprocess-alist))))
  14947. (setq org-last-set-property property)
  14948. (setq org-last-set-property-value (concat property ": " value))
  14949. ;; Possibly postprocess the inserted value:
  14950. (when fn (setq value (funcall fn value)))
  14951. (unless (equal (org-entry-get nil property) value)
  14952. (org-entry-put nil property value))))
  14953. (defun org-find-property (property &optional value)
  14954. "Find first entry in buffer that sets PROPERTY.
  14955. When optional argument VALUE is non-nil, only consider an entry
  14956. if it contains PROPERTY set to this value. If PROPERTY should be
  14957. explicitly set to nil, use string \"nil\" for VALUE.
  14958. Return position where the entry begins, or nil if there is no
  14959. such entry. If narrowing is in effect, only search the visible
  14960. part of the buffer."
  14961. (save-excursion
  14962. (goto-char (point-min))
  14963. (let ((case-fold-search t)
  14964. (re (org-re-property property nil (not value) value)))
  14965. (catch 'exit
  14966. (while (re-search-forward re nil t)
  14967. (when (if value (org-at-property-p)
  14968. (org-entry-get (point) property nil t))
  14969. (throw 'exit (progn (org-back-to-heading t) (point)))))))))
  14970. (defun org-delete-property (property)
  14971. "In the current entry, delete PROPERTY."
  14972. (interactive
  14973. (let* ((completion-ignore-case t)
  14974. (cat (org-entry-get (point) "CATEGORY"))
  14975. (props0 (org-entry-properties nil 'standard))
  14976. (props (if cat props0
  14977. (delete `("CATEGORY" . ,(org-get-category)) props0)))
  14978. (prop (if (< 1 (length props))
  14979. (org-icompleting-read "Property: " props nil t)
  14980. (caar props))))
  14981. (list prop)))
  14982. (if (not property)
  14983. (message "No property to delete in this entry")
  14984. (org-entry-delete nil property)
  14985. (message "Property \"%s\" deleted" property)))
  14986. (defun org-delete-property-globally (property)
  14987. "Remove PROPERTY globally, from all entries.
  14988. This function ignores narrowing, if any."
  14989. (interactive
  14990. (let* ((completion-ignore-case t)
  14991. (prop (org-icompleting-read
  14992. "Globally remove property: "
  14993. (mapcar #'list (org-buffer-property-keys)))))
  14994. (list prop)))
  14995. (org-with-wide-buffer
  14996. (goto-char (point-min))
  14997. (let ((count 0)
  14998. (re (org-re-property (concat (regexp-quote property) "\\+?") t t)))
  14999. (while (re-search-forward re nil t)
  15000. (when (org-entry-delete (point) property) (incf count)))
  15001. (message "Property \"%s\" removed from %d entries" property count))))
  15002. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  15003. (defun org-compute-property-at-point ()
  15004. "Compute the property at point.
  15005. This looks for an enclosing column format, extracts the operator and
  15006. then applies it to the property in the column format's scope."
  15007. (interactive)
  15008. (unless (org-at-property-p)
  15009. (user-error "Not at a property"))
  15010. (let ((prop (org-match-string-no-properties 2)))
  15011. (org-columns-get-format-and-top-level)
  15012. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  15013. (user-error "No operator defined for property %s" prop))
  15014. (org-columns-compute prop)))
  15015. (defvar org-property-allowed-value-functions nil
  15016. "Hook for functions supplying allowed values for a specific property.
  15017. The functions must take a single argument, the name of the property, and
  15018. return a flat list of allowed values. If \":ETC\" is one of
  15019. the values, this means that these values are intended as defaults for
  15020. completion, but that other values should be allowed too.
  15021. The functions must return nil if they are not responsible for this
  15022. property.")
  15023. (defun org-property-get-allowed-values (pom property &optional table)
  15024. "Get allowed values for the property PROPERTY.
  15025. When TABLE is non-nil, return an alist that can directly be used for
  15026. completion."
  15027. (let (vals)
  15028. (cond
  15029. ((equal property "TODO")
  15030. (setq vals (org-with-point-at pom
  15031. (append org-todo-keywords-1 '("")))))
  15032. ((equal property "PRIORITY")
  15033. (let ((n org-lowest-priority))
  15034. (while (>= n org-highest-priority)
  15035. (push (char-to-string n) vals)
  15036. (setq n (1- n)))))
  15037. ((equal property "CATEGORY"))
  15038. ((member property org-special-properties))
  15039. ((setq vals (run-hook-with-args-until-success
  15040. 'org-property-allowed-value-functions property)))
  15041. (t
  15042. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  15043. (when (and vals (string-match "\\S-" vals))
  15044. (setq vals (car (read-from-string (concat "(" vals ")"))))
  15045. (setq vals (mapcar (lambda (x)
  15046. (cond ((stringp x) x)
  15047. ((numberp x) (number-to-string x))
  15048. ((symbolp x) (symbol-name x))
  15049. (t "???")))
  15050. vals)))))
  15051. (when (member ":ETC" vals)
  15052. (setq vals (remove ":ETC" vals))
  15053. (org-add-props (car vals) '(org-unrestricted t)))
  15054. (if table (mapcar 'list vals) vals)))
  15055. (defun org-property-previous-allowed-value (&optional previous)
  15056. "Switch to the next allowed value for this property."
  15057. (interactive)
  15058. (org-property-next-allowed-value t))
  15059. (defun org-property-next-allowed-value (&optional previous)
  15060. "Switch to the next allowed value for this property."
  15061. (interactive)
  15062. (unless (org-at-property-p)
  15063. (user-error "Not at a property"))
  15064. (let* ((prop (car (save-match-data (org-split-string (match-string 1) ":"))))
  15065. (key (match-string 2))
  15066. (value (match-string 3))
  15067. (allowed (or (org-property-get-allowed-values (point) key)
  15068. (and (member value '("[ ]" "[-]" "[X]"))
  15069. '("[ ]" "[X]"))))
  15070. (heading (save-match-data (nth 4 (org-heading-components))))
  15071. nval)
  15072. (unless allowed
  15073. (user-error "Allowed values for this property have not been defined"))
  15074. (when previous (setq allowed (reverse allowed)))
  15075. (when (member value allowed)
  15076. (setq nval (car (cdr (member value allowed)))))
  15077. (setq nval (or nval (car allowed)))
  15078. (when (equal nval value)
  15079. (user-error "Only one allowed value for this property"))
  15080. (org-at-property-p)
  15081. (replace-match (concat " :" key ": " nval) t t)
  15082. (org-indent-line)
  15083. (beginning-of-line 1)
  15084. (skip-chars-forward " \t")
  15085. (when (equal prop org-effort-property)
  15086. (org-refresh-property
  15087. '((effort . identity)
  15088. (effort-minutes . org-duration-string-to-minutes))
  15089. nval)
  15090. (when (string= org-clock-current-task heading)
  15091. (setq org-clock-effort nval)
  15092. (org-clock-update-mode-line)))
  15093. (run-hook-with-args 'org-property-changed-functions key nval)))
  15094. (defun org-find-olp (path &optional this-buffer)
  15095. "Return a marker pointing to the entry at outline path OLP.
  15096. If anything goes wrong, throw an error.
  15097. You can wrap this call to catch the error like this:
  15098. (condition-case msg
  15099. (org-mobile-locate-entry (match-string 4))
  15100. (error (nth 1 msg)))
  15101. The return value will then be either a string with the error message,
  15102. or a marker if everything is OK.
  15103. If THIS-BUFFER is set, the outline path does not contain a file,
  15104. only headings."
  15105. (let* ((file (if this-buffer buffer-file-name (pop path)))
  15106. (buffer (if this-buffer (current-buffer) (find-file-noselect file)))
  15107. (level 1)
  15108. (lmin 1)
  15109. (lmax 1)
  15110. limit re end found pos heading cnt flevel)
  15111. (unless buffer (error "File not found :%s" file))
  15112. (with-current-buffer buffer
  15113. (save-excursion
  15114. (save-restriction
  15115. (widen)
  15116. (setq limit (point-max))
  15117. (goto-char (point-min))
  15118. (dolist (heading path)
  15119. (setq re (format org-complex-heading-regexp-format
  15120. (regexp-quote heading)))
  15121. (setq cnt 0 pos (point))
  15122. (while (re-search-forward re end t)
  15123. (setq level (- (match-end 1) (match-beginning 1)))
  15124. (when (and (>= level lmin) (<= level lmax))
  15125. (setq found (match-beginning 0) flevel level cnt (1+ cnt))))
  15126. (when (= cnt 0) (error "Heading not found on level %d: %s"
  15127. lmax heading))
  15128. (when (> cnt 1) (error "Heading not unique on level %d: %s"
  15129. lmax heading))
  15130. (goto-char found)
  15131. (setq lmin (1+ flevel) lmax (+ lmin (if org-odd-levels-only 1 0)))
  15132. (setq end (save-excursion (org-end-of-subtree t t))))
  15133. (when (org-at-heading-p)
  15134. (point-marker)))))))
  15135. (defun org-find-exact-headline-in-buffer (heading &optional buffer pos-only)
  15136. "Find node HEADING in BUFFER.
  15137. Return a marker to the heading if it was found, or nil if not.
  15138. If POS-ONLY is set, return just the position instead of a marker.
  15139. The heading text must match exact, but it may have a TODO keyword,
  15140. a priority cookie and tags in the standard locations."
  15141. (with-current-buffer (or buffer (current-buffer))
  15142. (save-excursion
  15143. (save-restriction
  15144. (widen)
  15145. (goto-char (point-min))
  15146. (let (case-fold-search)
  15147. (when (re-search-forward
  15148. (format org-complex-heading-regexp-format
  15149. (regexp-quote heading)) nil t)
  15150. (if pos-only
  15151. (match-beginning 0)
  15152. (move-marker (make-marker) (match-beginning 0)))))))))
  15153. (defun org-find-exact-heading-in-directory (heading &optional dir)
  15154. "Find Org node headline HEADING in all .org files in directory DIR.
  15155. When the target headline is found, return a marker to this location."
  15156. (let ((files (directory-files (or dir default-directory)
  15157. t "\\`[^.#].*\\.org\\'"))
  15158. visiting m buffer)
  15159. (catch 'found
  15160. (dolist (file files)
  15161. (message "trying %s" file)
  15162. (setq visiting (org-find-base-buffer-visiting file))
  15163. (setq buffer (or visiting (find-file-noselect file)))
  15164. (setq m (org-find-exact-headline-in-buffer
  15165. heading buffer))
  15166. (when (and (not m) (not visiting)) (kill-buffer buffer))
  15167. (and m (throw 'found m))))))
  15168. (defun org-find-entry-with-id (ident)
  15169. "Locate the entry that contains the ID property with exact value IDENT.
  15170. IDENT can be a string, a symbol or a number, this function will search for
  15171. the string representation of it.
  15172. Return the position where this entry starts, or nil if there is no such entry."
  15173. (interactive "sID: ")
  15174. (let ((id (cond
  15175. ((stringp ident) ident)
  15176. ((symbolp ident) (symbol-name ident))
  15177. ((numberp ident) (number-to-string ident))
  15178. (t (error "IDENT %s must be a string, symbol or number" ident)))))
  15179. (org-with-wide-buffer (org-find-property "ID" id))))
  15180. ;;;; Timestamps
  15181. (defvar org-last-changed-timestamp nil)
  15182. (defvar org-last-inserted-timestamp nil
  15183. "The last time stamp inserted with `org-insert-time-stamp'.")
  15184. (defvar org-ts-what) ; dynamically scoped parameter
  15185. (defun org-time-stamp (arg &optional inactive)
  15186. "Prompt for a date/time and insert a time stamp.
  15187. If the user specifies a time like HH:MM or if this command is
  15188. called with at least one prefix argument, the time stamp contains
  15189. the date and the time. Otherwise, only the date is included.
  15190. All parts of a date not specified by the user are filled in from
  15191. the timestamp at point, if any, or the current date/time
  15192. otherwise.
  15193. If there is already a timestamp at the cursor, it is replaced.
  15194. With two universal prefix arguments, insert an active timestamp
  15195. with the current time without prompting the user.
  15196. When called from lisp, the timestamp is inactive if INACTIVE is
  15197. non-nil."
  15198. (interactive "P")
  15199. (let* ((ts (cond
  15200. ((org-at-date-range-p t)
  15201. (match-string (if (< (point) (- (match-beginning 2) 2)) 1 2)))
  15202. ((org-at-timestamp-p t) (match-string 0))))
  15203. ;; Default time is either the timestamp at point or today.
  15204. ;; When entering a range, only the range start is considered.
  15205. (default-time (if (not ts) (current-time)
  15206. (apply #'encode-time (org-parse-time-string ts))))
  15207. (default-input (and ts (org-get-compact-tod ts)))
  15208. (repeater (and ts
  15209. (string-match "\\([.+-]+[0-9]+[hdwmy] ?\\)+" ts)
  15210. (match-string 0 ts)))
  15211. org-time-was-given
  15212. org-end-time-was-given
  15213. (time
  15214. (and (if (equal arg '(16)) (current-time)
  15215. ;; Preserve `this-command' and `last-command'.
  15216. (let ((this-command this-command)
  15217. (last-command last-command))
  15218. (org-read-date
  15219. arg 'totime nil nil default-time default-input
  15220. inactive))))))
  15221. (cond
  15222. ((and ts
  15223. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  15224. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  15225. (insert "--")
  15226. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  15227. (ts
  15228. ;; Make sure we're on a timestamp. When in the middle of a date
  15229. ;; range, move arbitrarily to range end.
  15230. (unless (org-at-timestamp-p t)
  15231. (skip-chars-forward "-")
  15232. (org-at-timestamp-p t))
  15233. (replace-match "")
  15234. (setq org-last-changed-timestamp
  15235. (org-insert-time-stamp
  15236. time (or org-time-was-given arg)
  15237. inactive nil nil (list org-end-time-was-given)))
  15238. (when repeater
  15239. (backward-char)
  15240. (insert " " repeater)
  15241. (setq org-last-changed-timestamp
  15242. (concat (substring org-last-inserted-timestamp 0 -1)
  15243. " " repeater ">")))
  15244. (message "Timestamp updated"))
  15245. ((equal arg '(16)) (org-insert-time-stamp time t inactive))
  15246. (t (org-insert-time-stamp
  15247. time (or org-time-was-given arg) inactive nil nil
  15248. (list org-end-time-was-given))))))
  15249. ;; FIXME: can we use this for something else, like computing time differences?
  15250. (defun org-get-compact-tod (s)
  15251. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  15252. (let* ((t1 (match-string 1 s))
  15253. (h1 (string-to-number (match-string 2 s)))
  15254. (m1 (string-to-number (match-string 3 s)))
  15255. (t2 (and (match-end 4) (match-string 5 s)))
  15256. (h2 (and t2 (string-to-number (match-string 6 s))))
  15257. (m2 (and t2 (string-to-number (match-string 7 s))))
  15258. dh dm)
  15259. (if (not t2)
  15260. t1
  15261. (setq dh (- h2 h1) dm (- m2 m1))
  15262. (when (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  15263. (concat t1 "+" (number-to-string dh)
  15264. (and (/= 0 dm) (format ":%02d" dm)))))))
  15265. (defun org-time-stamp-inactive (&optional arg)
  15266. "Insert an inactive time stamp.
  15267. An inactive time stamp is enclosed in square brackets instead of angle
  15268. brackets. It is inactive in the sense that it does not trigger agenda entries,
  15269. does not link to the calendar and cannot be changed with the S-cursor keys.
  15270. So these are more for recording a certain time/date."
  15271. (interactive "P")
  15272. (org-time-stamp arg 'inactive))
  15273. (defvar org-date-ovl (make-overlay 1 1))
  15274. (overlay-put org-date-ovl 'face 'org-date-selected)
  15275. (org-detach-overlay org-date-ovl)
  15276. (defvar org-ans1) ; dynamically scoped parameter
  15277. (defvar org-ans2) ; dynamically scoped parameter
  15278. (defvar org-plain-time-of-day-regexp) ; defined below
  15279. (defvar org-overriding-default-time nil) ; dynamically scoped
  15280. (defvar org-read-date-overlay nil)
  15281. (defvar org-dcst nil) ; dynamically scoped
  15282. (defvar org-read-date-history nil)
  15283. (defvar org-read-date-final-answer nil)
  15284. (defvar org-read-date-analyze-futurep nil)
  15285. (defvar org-read-date-analyze-forced-year nil)
  15286. (defvar org-read-date-inactive)
  15287. (defvar org-read-date-minibuffer-local-map
  15288. (let* ((map (make-sparse-keymap)))
  15289. (set-keymap-parent map minibuffer-local-map)
  15290. (org-defkey map (kbd ".")
  15291. (lambda () (interactive)
  15292. ;; Are we at the beginning of the prompt?
  15293. (if (org-looking-back "^[^:]+: "
  15294. (let ((inhibit-field-text-motion t))
  15295. (line-beginning-position)))
  15296. (org-eval-in-calendar '(calendar-goto-today))
  15297. (insert "."))))
  15298. (org-defkey map (kbd "C-.")
  15299. (lambda () (interactive)
  15300. (org-eval-in-calendar '(calendar-goto-today))))
  15301. (org-defkey map [(meta shift left)]
  15302. (lambda () (interactive)
  15303. (org-eval-in-calendar '(calendar-backward-month 1))))
  15304. (org-defkey map [(meta shift right)]
  15305. (lambda () (interactive)
  15306. (org-eval-in-calendar '(calendar-forward-month 1))))
  15307. (org-defkey map [(meta shift up)]
  15308. (lambda () (interactive)
  15309. (org-eval-in-calendar '(calendar-backward-year 1))))
  15310. (org-defkey map [(meta shift down)]
  15311. (lambda () (interactive)
  15312. (org-eval-in-calendar '(calendar-forward-year 1))))
  15313. (org-defkey map [?\e (shift left)]
  15314. (lambda () (interactive)
  15315. (org-eval-in-calendar '(calendar-backward-month 1))))
  15316. (org-defkey map [?\e (shift right)]
  15317. (lambda () (interactive)
  15318. (org-eval-in-calendar '(calendar-forward-month 1))))
  15319. (org-defkey map [?\e (shift up)]
  15320. (lambda () (interactive)
  15321. (org-eval-in-calendar '(calendar-backward-year 1))))
  15322. (org-defkey map [?\e (shift down)]
  15323. (lambda () (interactive)
  15324. (org-eval-in-calendar '(calendar-forward-year 1))))
  15325. (org-defkey map [(shift up)]
  15326. (lambda () (interactive)
  15327. (org-eval-in-calendar '(calendar-backward-week 1))))
  15328. (org-defkey map [(shift down)]
  15329. (lambda () (interactive)
  15330. (org-eval-in-calendar '(calendar-forward-week 1))))
  15331. (org-defkey map [(shift left)]
  15332. (lambda () (interactive)
  15333. (org-eval-in-calendar '(calendar-backward-day 1))))
  15334. (org-defkey map [(shift right)]
  15335. (lambda () (interactive)
  15336. (org-eval-in-calendar '(calendar-forward-day 1))))
  15337. (org-defkey map "!"
  15338. (lambda () (interactive)
  15339. (org-eval-in-calendar '(diary-view-entries))
  15340. (message "")))
  15341. (org-defkey map ">"
  15342. (lambda () (interactive)
  15343. (org-eval-in-calendar '(calendar-scroll-left 1))))
  15344. (org-defkey map "<"
  15345. (lambda () (interactive)
  15346. (org-eval-in-calendar '(calendar-scroll-right 1))))
  15347. (org-defkey map "\C-v"
  15348. (lambda () (interactive)
  15349. (org-eval-in-calendar
  15350. '(calendar-scroll-left-three-months 1))))
  15351. (org-defkey map "\M-v"
  15352. (lambda () (interactive)
  15353. (org-eval-in-calendar
  15354. '(calendar-scroll-right-three-months 1))))
  15355. map)
  15356. "Keymap for minibuffer commands when using `org-read-date'.")
  15357. (defvar org-def)
  15358. (defvar org-defdecode)
  15359. (defvar org-with-time)
  15360. (defun org-read-date (&optional org-with-time to-time from-string prompt
  15361. default-time default-input inactive)
  15362. "Read a date, possibly a time, and make things smooth for the user.
  15363. The prompt will suggest to enter an ISO date, but you can also enter anything
  15364. which will at least partially be understood by `parse-time-string'.
  15365. Unrecognized parts of the date will default to the current day, month, year,
  15366. hour and minute. If this command is called to replace a timestamp at point,
  15367. or to enter the second timestamp of a range, the default time is taken
  15368. from the existing stamp. Furthermore, the command prefers the future,
  15369. so if you are giving a date where the year is not given, and the day-month
  15370. combination is already past in the current year, it will assume you
  15371. mean next year. For details, see the manual. A few examples:
  15372. 3-2-5 --> 2003-02-05
  15373. feb 15 --> currentyear-02-15
  15374. 2/15 --> currentyear-02-15
  15375. sep 12 9 --> 2009-09-12
  15376. 12:45 --> today 12:45
  15377. 22 sept 0:34 --> currentyear-09-22 0:34
  15378. 12 --> currentyear-currentmonth-12
  15379. Fri --> nearest Friday after today
  15380. -Tue --> last Tuesday
  15381. etc.
  15382. Furthermore you can specify a relative date by giving, as the *first* thing
  15383. in the input: a plus/minus sign, a number and a letter [hdwmy] to indicate
  15384. change in days weeks, months, years.
  15385. With a single plus or minus, the date is relative to today. With a double
  15386. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  15387. +4d --> four days from today
  15388. +4 --> same as above
  15389. +2w --> two weeks from today
  15390. ++5 --> five days from default date
  15391. The function understands only English month and weekday abbreviations.
  15392. While prompting, a calendar is popped up - you can also select the
  15393. date with the mouse (button 1). The calendar shows a period of three
  15394. months. To scroll it to other months, use the keys `>' and `<'.
  15395. If you don't like the calendar, turn it off with
  15396. (setq org-read-date-popup-calendar nil)
  15397. With optional argument TO-TIME, the date will immediately be converted
  15398. to an internal time.
  15399. With an optional argument ORG-WITH-TIME, the prompt will suggest to
  15400. also insert a time. Note that when ORG-WITH-TIME is not set, you can
  15401. still enter a time, and this function will inform the calling routine
  15402. about this change. The calling routine may then choose to change the
  15403. format used to insert the time stamp into the buffer to include the time.
  15404. With optional argument FROM-STRING, read from this string instead from
  15405. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  15406. the time/date that is used for everything that is not specified by the
  15407. user."
  15408. (require 'parse-time)
  15409. (let* ((org-time-stamp-rounding-minutes
  15410. (if (equal org-with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  15411. (org-dcst org-display-custom-times)
  15412. (ct (org-current-time))
  15413. (org-def (or org-overriding-default-time default-time ct))
  15414. (org-defdecode (decode-time org-def))
  15415. (dummy (progn
  15416. (when (< (nth 2 org-defdecode) org-extend-today-until)
  15417. (setcar (nthcdr 2 org-defdecode) -1)
  15418. (setcar (nthcdr 1 org-defdecode) 59)
  15419. (setq org-def (apply 'encode-time org-defdecode)
  15420. org-defdecode (decode-time org-def)))))
  15421. (cur-frame (selected-frame))
  15422. (mouse-autoselect-window nil) ; Don't let the mouse jump
  15423. (calendar-frame-setup nil)
  15424. (calendar-setup (when (eq calendar-setup 'calendar-only) 'calendar-only))
  15425. (calendar-move-hook nil)
  15426. (calendar-view-diary-initially-flag nil)
  15427. (calendar-view-holidays-initially-flag nil)
  15428. (timestr (format-time-string
  15429. (if org-with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") org-def))
  15430. (prompt (concat (if prompt (concat prompt " ") "")
  15431. (format "Date+time [%s]: " timestr)))
  15432. ans (org-ans0 "") org-ans1 org-ans2 final cal-frame)
  15433. (cond
  15434. (from-string (setq ans from-string))
  15435. (org-read-date-popup-calendar
  15436. (save-excursion
  15437. (save-window-excursion
  15438. (calendar)
  15439. (when (eq calendar-setup 'calendar-only)
  15440. (setq cal-frame
  15441. (window-frame (get-buffer-window "*Calendar*" 'visible)))
  15442. (select-frame cal-frame))
  15443. (org-eval-in-calendar '(setq cursor-type nil) t)
  15444. (unwind-protect
  15445. (progn
  15446. (calendar-forward-day (- (time-to-days org-def)
  15447. (calendar-absolute-from-gregorian
  15448. (calendar-current-date))))
  15449. (org-eval-in-calendar nil t)
  15450. (let* ((old-map (current-local-map))
  15451. (map (copy-keymap calendar-mode-map))
  15452. (minibuffer-local-map
  15453. (copy-keymap org-read-date-minibuffer-local-map)))
  15454. (org-defkey map (kbd "RET") 'org-calendar-select)
  15455. (org-defkey map [mouse-1] 'org-calendar-select-mouse)
  15456. (org-defkey map [mouse-2] 'org-calendar-select-mouse)
  15457. (unwind-protect
  15458. (progn
  15459. (use-local-map map)
  15460. (setq org-read-date-inactive inactive)
  15461. (add-hook 'post-command-hook 'org-read-date-display)
  15462. (setq org-ans0 (read-string prompt default-input
  15463. 'org-read-date-history nil))
  15464. ;; org-ans0: from prompt
  15465. ;; org-ans1: from mouse click
  15466. ;; org-ans2: from calendar motion
  15467. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  15468. (remove-hook 'post-command-hook 'org-read-date-display)
  15469. (use-local-map old-map)
  15470. (when org-read-date-overlay
  15471. (delete-overlay org-read-date-overlay)
  15472. (setq org-read-date-overlay nil)))))
  15473. (bury-buffer "*Calendar*")
  15474. (when cal-frame
  15475. (delete-frame cal-frame)
  15476. (select-frame-set-input-focus cur-frame))))))
  15477. (t ; Naked prompt only
  15478. (unwind-protect
  15479. (setq ans (read-string prompt default-input
  15480. 'org-read-date-history timestr))
  15481. (when org-read-date-overlay
  15482. (delete-overlay org-read-date-overlay)
  15483. (setq org-read-date-overlay nil)))))
  15484. (setq final (org-read-date-analyze ans org-def org-defdecode))
  15485. (when org-read-date-analyze-forced-year
  15486. (message "Year was forced into %s"
  15487. (if org-read-date-force-compatible-dates
  15488. "compatible range (1970-2037)"
  15489. "range representable on this machine"))
  15490. (ding))
  15491. ;; One round trip to get rid of 34th of August and stuff like that....
  15492. (setq final (decode-time (apply 'encode-time final)))
  15493. (setq org-read-date-final-answer ans)
  15494. (if to-time
  15495. (apply 'encode-time final)
  15496. (if (and (boundp 'org-time-was-given) org-time-was-given)
  15497. (format "%04d-%02d-%02d %02d:%02d"
  15498. (nth 5 final) (nth 4 final) (nth 3 final)
  15499. (nth 2 final) (nth 1 final))
  15500. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  15501. (defun org-read-date-display ()
  15502. "Display the current date prompt interpretation in the minibuffer."
  15503. (when org-read-date-display-live
  15504. (when org-read-date-overlay
  15505. (delete-overlay org-read-date-overlay))
  15506. (when (minibufferp (current-buffer))
  15507. (save-excursion
  15508. (end-of-line 1)
  15509. (while (not (equal (buffer-substring
  15510. (max (point-min) (- (point) 4)) (point))
  15511. " "))
  15512. (insert " ")))
  15513. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  15514. " " (or org-ans1 org-ans2)))
  15515. (org-end-time-was-given nil)
  15516. (f (org-read-date-analyze ans org-def org-defdecode))
  15517. (fmts (if org-dcst
  15518. org-time-stamp-custom-formats
  15519. org-time-stamp-formats))
  15520. (fmt (if (or org-with-time
  15521. (and (boundp 'org-time-was-given) org-time-was-given))
  15522. (cdr fmts)
  15523. (car fmts)))
  15524. (txt (format-time-string fmt (apply 'encode-time f)))
  15525. (txt (if org-read-date-inactive (concat "[" (substring txt 1 -1) "]") txt))
  15526. (txt (concat "=> " txt)))
  15527. (when (and org-end-time-was-given
  15528. (string-match org-plain-time-of-day-regexp txt))
  15529. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  15530. org-end-time-was-given
  15531. (substring txt (match-end 0)))))
  15532. (when org-read-date-analyze-futurep
  15533. (setq txt (concat txt " (=>F)")))
  15534. (setq org-read-date-overlay
  15535. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  15536. (org-overlay-display org-read-date-overlay txt 'secondary-selection)))))
  15537. (defun org-read-date-analyze (ans org-def org-defdecode)
  15538. "Analyze the combined answer of the date prompt."
  15539. ;; FIXME: cleanup and comment
  15540. ;; Pass `current-time' result to `decode-time' (instead of calling
  15541. ;; without arguments) so that only `current-time' has to be
  15542. ;; overriden in tests.
  15543. (let ((nowdecode (decode-time (current-time)))
  15544. delta deltan deltaw deltadef year month day
  15545. hour minute second wday pm h2 m2 tl wday1
  15546. iso-year iso-weekday iso-week iso-year iso-date futurep kill-year)
  15547. (setq org-read-date-analyze-futurep nil
  15548. org-read-date-analyze-forced-year nil)
  15549. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  15550. (setq ans "+0"))
  15551. (when (setq delta (org-read-date-get-relative ans (current-time) org-def))
  15552. (setq ans (replace-match "" t t ans)
  15553. deltan (car delta)
  15554. deltaw (nth 1 delta)
  15555. deltadef (nth 2 delta)))
  15556. ;; Check if there is an iso week date in there. If yes, store the
  15557. ;; info and postpone interpreting it until the rest of the parsing
  15558. ;; is done.
  15559. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  15560. (setq iso-year (when (match-end 1)
  15561. (org-small-year-to-year
  15562. (string-to-number (match-string 1 ans))))
  15563. iso-weekday (when (match-end 3)
  15564. (string-to-number (match-string 3 ans)))
  15565. iso-week (string-to-number (match-string 2 ans)))
  15566. (setq ans (replace-match "" t t ans)))
  15567. ;; Help matching ISO dates with single digit month or day, like 2006-8-11.
  15568. (when (string-match
  15569. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  15570. (setq year (if (match-end 2)
  15571. (string-to-number (match-string 2 ans))
  15572. (progn (setq kill-year t)
  15573. (string-to-number (format-time-string "%Y"))))
  15574. month (string-to-number (match-string 3 ans))
  15575. day (string-to-number (match-string 4 ans)))
  15576. (when (< year 100) (setq year (+ 2000 year)))
  15577. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  15578. t nil ans)))
  15579. ;; Help matching dotted european dates
  15580. (when (string-match
  15581. "^ *\\(3[01]\\|0?[1-9]\\|[12][0-9]\\)\\. ?\\(0?[1-9]\\|1[012]\\)\\.\\( ?[1-9][0-9]\\{3\\}\\)?" ans)
  15582. (setq year (if (match-end 3) (string-to-number (match-string 3 ans))
  15583. (setq kill-year t)
  15584. (string-to-number (format-time-string "%Y")))
  15585. day (string-to-number (match-string 1 ans))
  15586. month (string-to-number (match-string 2 ans))
  15587. ans (replace-match (format "%04d-%02d-%02d" year month day)
  15588. t nil ans)))
  15589. ;; Help matching american dates, like 5/30 or 5/30/7
  15590. (when (string-match
  15591. "^ *\\(0?[1-9]\\|1[012]\\)/\\(0?[1-9]\\|[12][0-9]\\|3[01]\\)\\(/\\([0-9]+\\)\\)?\\([^/0-9]\\|$\\)" ans)
  15592. (setq year (if (match-end 4)
  15593. (string-to-number (match-string 4 ans))
  15594. (progn (setq kill-year t)
  15595. (string-to-number (format-time-string "%Y"))))
  15596. month (string-to-number (match-string 1 ans))
  15597. day (string-to-number (match-string 2 ans)))
  15598. (when (< year 100) (setq year (+ 2000 year)))
  15599. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  15600. t nil ans)))
  15601. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  15602. ;; If there is a time with am/pm, and *no* time without it, we convert
  15603. ;; so that matching will be successful.
  15604. (loop for i from 1 to 2 do ; twice, for end time as well
  15605. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  15606. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  15607. (setq hour (string-to-number (match-string 1 ans))
  15608. minute (if (match-end 3)
  15609. (string-to-number (match-string 3 ans))
  15610. 0)
  15611. pm (equal ?p
  15612. (string-to-char (downcase (match-string 4 ans)))))
  15613. (if (and (= hour 12) (not pm))
  15614. (setq hour 0)
  15615. (when (and pm (< hour 12)) (setq hour (+ 12 hour))))
  15616. (setq ans (replace-match (format "%02d:%02d" hour minute)
  15617. t t ans))))
  15618. ;; Check if a time range is given as a duration
  15619. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  15620. (setq hour (string-to-number (match-string 1 ans))
  15621. h2 (+ hour (string-to-number (match-string 3 ans)))
  15622. minute (string-to-number (match-string 2 ans))
  15623. m2 (+ minute (if (match-end 5) (string-to-number
  15624. (match-string 5 ans))0)))
  15625. (when (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  15626. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  15627. t t ans)))
  15628. ;; Check if there is a time range
  15629. (when (boundp 'org-end-time-was-given)
  15630. (setq org-time-was-given nil)
  15631. (when (and (string-match org-plain-time-of-day-regexp ans)
  15632. (match-end 8))
  15633. (setq org-end-time-was-given (match-string 8 ans))
  15634. (setq ans (concat (substring ans 0 (match-beginning 7))
  15635. (substring ans (match-end 7))))))
  15636. (setq tl (parse-time-string ans)
  15637. day (or (nth 3 tl) (nth 3 org-defdecode))
  15638. month
  15639. (cond ((nth 4 tl))
  15640. ((not org-read-date-prefer-future) (nth 4 org-defdecode))
  15641. ;; Day was specified. Make sure DAY+MONTH
  15642. ;; combination happens in the future.
  15643. ((nth 3 tl)
  15644. (setq futurep t)
  15645. (if (< day (nth 3 nowdecode)) (1+ (nth 4 nowdecode))
  15646. (nth 4 nowdecode)))
  15647. (t (nth 4 org-defdecode)))
  15648. year
  15649. (cond ((and (not kill-year) (nth 5 tl)))
  15650. ((not org-read-date-prefer-future) (nth 5 org-defdecode))
  15651. ;; Month was guessed in the future and is at least
  15652. ;; equal to NOWDECODE's. Fix year accordingly.
  15653. (futurep
  15654. (if (or (> month (nth 4 nowdecode))
  15655. (>= day (nth 3 nowdecode)))
  15656. (nth 5 nowdecode)
  15657. (1+ (nth 5 nowdecode))))
  15658. ;; Month was specified. Make sure MONTH+YEAR
  15659. ;; combination happens in the future.
  15660. ((nth 4 tl)
  15661. (setq futurep t)
  15662. (cond ((> month (nth 4 nowdecode)) (nth 5 nowdecode))
  15663. ((< month (nth 4 nowdecode)) (1+ (nth 5 nowdecode)))
  15664. ((< day (nth 3 nowdecode)) (1+ (nth 5 nowdecode)))
  15665. (t (nth 5 nowdecode))))
  15666. (t (nth 5 org-defdecode)))
  15667. hour (or (nth 2 tl) (nth 2 org-defdecode))
  15668. minute (or (nth 1 tl) (nth 1 org-defdecode))
  15669. second (or (nth 0 tl) 0)
  15670. wday (nth 6 tl))
  15671. (when (and (eq org-read-date-prefer-future 'time)
  15672. (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
  15673. (equal day (nth 3 nowdecode))
  15674. (equal month (nth 4 nowdecode))
  15675. (equal year (nth 5 nowdecode))
  15676. (nth 2 tl)
  15677. (or (< (nth 2 tl) (nth 2 nowdecode))
  15678. (and (= (nth 2 tl) (nth 2 nowdecode))
  15679. (nth 1 tl)
  15680. (< (nth 1 tl) (nth 1 nowdecode)))))
  15681. (setq day (1+ day)
  15682. futurep t))
  15683. ;; Special date definitions below
  15684. (cond
  15685. (iso-week
  15686. ;; There was an iso week
  15687. (require 'cal-iso)
  15688. (setq futurep nil)
  15689. (setq year (or iso-year year)
  15690. day (or iso-weekday wday 1)
  15691. wday nil ; to make sure that the trigger below does not match
  15692. iso-date (calendar-gregorian-from-absolute
  15693. (calendar-iso-to-absolute
  15694. (list iso-week day year))))
  15695. ; FIXME: Should we also push ISO weeks into the future?
  15696. ; (when (and org-read-date-prefer-future
  15697. ; (not iso-year)
  15698. ; (< (calendar-absolute-from-gregorian iso-date)
  15699. ; (time-to-days (current-time))))
  15700. ; (setq year (1+ year)
  15701. ; iso-date (calendar-gregorian-from-absolute
  15702. ; (calendar-iso-to-absolute
  15703. ; (list iso-week day year)))))
  15704. (setq month (car iso-date)
  15705. year (nth 2 iso-date)
  15706. day (nth 1 iso-date)))
  15707. (deltan
  15708. (setq futurep nil)
  15709. (unless deltadef
  15710. ;; Pass `current-time' result to `decode-time' (instead of
  15711. ;; calling without arguments) so that only `current-time' has
  15712. ;; to be overriden in tests.
  15713. (let ((now (decode-time (current-time))))
  15714. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  15715. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  15716. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  15717. ((equal deltaw "m") (setq month (+ month deltan)))
  15718. ((equal deltaw "y") (setq year (+ year deltan)))))
  15719. ((and wday (not (nth 3 tl)))
  15720. ;; Weekday was given, but no day, so pick that day in the week
  15721. ;; on or after the derived date.
  15722. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  15723. (unless (equal wday wday1)
  15724. (setq day (+ day (% (- wday wday1 -7) 7))))))
  15725. (when (and (boundp 'org-time-was-given)
  15726. (nth 2 tl))
  15727. (setq org-time-was-given t))
  15728. (when (< year 100) (setq year (+ 2000 year)))
  15729. ;; Check of the date is representable
  15730. (if org-read-date-force-compatible-dates
  15731. (progn
  15732. (when (< year 1970)
  15733. (setq year 1970 org-read-date-analyze-forced-year t))
  15734. (when (> year 2037)
  15735. (setq year 2037 org-read-date-analyze-forced-year t)))
  15736. (condition-case nil
  15737. (ignore (encode-time second minute hour day month year))
  15738. (error
  15739. (setq year (nth 5 org-defdecode))
  15740. (setq org-read-date-analyze-forced-year t))))
  15741. (setq org-read-date-analyze-futurep futurep)
  15742. (list second minute hour day month year)))
  15743. (defvar parse-time-weekdays)
  15744. (defun org-read-date-get-relative (s today default)
  15745. "Check string S for special relative date string.
  15746. TODAY and DEFAULT are internal times, for today and for a default.
  15747. Return shift list (N what def-flag)
  15748. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  15749. N is the number of WHATs to shift.
  15750. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  15751. the DEFAULT date rather than TODAY."
  15752. (require 'parse-time)
  15753. (when (and
  15754. (string-match
  15755. (concat
  15756. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  15757. "\\([0-9]+\\)?"
  15758. "\\([hdwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  15759. "\\([ \t]\\|$\\)") s)
  15760. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  15761. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  15762. (string-to-char (substring (match-string 1 s) -1))
  15763. ?+))
  15764. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  15765. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  15766. (what (if (match-end 3) (match-string 3 s) "d"))
  15767. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  15768. (date (if rel default today))
  15769. (wday (nth 6 (decode-time date)))
  15770. delta)
  15771. (if wday1
  15772. (progn
  15773. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  15774. (when (= delta 0) (setq delta 7))
  15775. (when (= dir ?-)
  15776. (setq delta (- delta 7))
  15777. (when (= delta 0) (setq delta -7)))
  15778. (when (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  15779. (list delta "d" rel))
  15780. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  15781. (defun org-order-calendar-date-args (arg1 arg2 arg3)
  15782. "Turn a user-specified date into the internal representation.
  15783. The internal representation needed by the calendar is (month day year).
  15784. This is a wrapper to handle the brain-dead convention in calendar that
  15785. user function argument order change dependent on argument order."
  15786. (if (boundp 'calendar-date-style)
  15787. (cond
  15788. ((eq calendar-date-style 'american)
  15789. (list arg1 arg2 arg3))
  15790. ((eq calendar-date-style 'european)
  15791. (list arg2 arg1 arg3))
  15792. ((eq calendar-date-style 'iso)
  15793. (list arg2 arg3 arg1)))
  15794. (org-no-warnings ;; european-calendar-style is obsolete as of version 23.1
  15795. (if (org-bound-and-true-p european-calendar-style)
  15796. (list arg2 arg1 arg3)
  15797. (list arg1 arg2 arg3)))))
  15798. (defun org-eval-in-calendar (form &optional keepdate)
  15799. "Eval FORM in the calendar window and return to current window.
  15800. When KEEPDATE is non-nil, update `org-ans2' from the cursor date,
  15801. otherwise stick to the current value of `org-ans2'."
  15802. (let ((sf (selected-frame))
  15803. (sw (selected-window)))
  15804. (select-window (get-buffer-window "*Calendar*" t))
  15805. (eval form)
  15806. (when (and (not keepdate) (calendar-cursor-to-date))
  15807. (let* ((date (calendar-cursor-to-date))
  15808. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15809. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  15810. (move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  15811. (select-window sw)
  15812. (org-select-frame-set-input-focus sf)))
  15813. (defun org-calendar-select ()
  15814. "Return to `org-read-date' with the date currently selected.
  15815. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  15816. (interactive)
  15817. (when (calendar-cursor-to-date)
  15818. (let* ((date (calendar-cursor-to-date))
  15819. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15820. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  15821. (when (active-minibuffer-window) (exit-minibuffer))))
  15822. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  15823. "Insert a date stamp for the date given by the internal TIME.
  15824. See `format-time-string' for the format of TIME.
  15825. WITH-HM means use the stamp format that includes the time of the day.
  15826. INACTIVE means use square brackets instead of angular ones, so that the
  15827. stamp will not contribute to the agenda.
  15828. PRE and POST are optional strings to be inserted before and after the
  15829. stamp.
  15830. The command returns the inserted time stamp."
  15831. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  15832. stamp)
  15833. (when inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  15834. (insert-before-markers (or pre ""))
  15835. (when (listp extra)
  15836. (setq extra (car extra))
  15837. (if (and (stringp extra)
  15838. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  15839. (setq extra (format "-%02d:%02d"
  15840. (string-to-number (match-string 1 extra))
  15841. (string-to-number (match-string 2 extra))))
  15842. (setq extra nil)))
  15843. (when extra
  15844. (setq fmt (concat (substring fmt 0 -1) extra (substring fmt -1))))
  15845. (insert-before-markers (setq stamp (format-time-string fmt time)))
  15846. (insert-before-markers (or post ""))
  15847. (setq org-last-inserted-timestamp stamp)))
  15848. (defun org-toggle-time-stamp-overlays ()
  15849. "Toggle the use of custom time stamp formats."
  15850. (interactive)
  15851. (setq org-display-custom-times (not org-display-custom-times))
  15852. (unless org-display-custom-times
  15853. (let ((p (point-min)) (bmp (buffer-modified-p)))
  15854. (while (setq p (next-single-property-change p 'display))
  15855. (when (and (get-text-property p 'display)
  15856. (eq (get-text-property p 'face) 'org-date))
  15857. (remove-text-properties
  15858. p (setq p (next-single-property-change p 'display))
  15859. '(display t))))
  15860. (set-buffer-modified-p bmp)))
  15861. (when (featurep 'xemacs)
  15862. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  15863. (org-restart-font-lock)
  15864. (setq org-table-may-need-update t)
  15865. (if org-display-custom-times
  15866. (message "Time stamps are overlaid with custom format")
  15867. (message "Time stamp overlays removed")))
  15868. (defun org-display-custom-time (beg end)
  15869. "Overlay modified time stamp format over timestamp between BEG and END."
  15870. (let* ((ts (buffer-substring beg end))
  15871. t1 w1 with-hm tf time str w2 (off 0))
  15872. (save-match-data
  15873. (setq t1 (org-parse-time-string ts t))
  15874. (when (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)?\\'" ts)
  15875. (setq off (- (match-end 0) (match-beginning 0)))))
  15876. (setq end (- end off))
  15877. (setq w1 (- end beg)
  15878. with-hm (and (nth 1 t1) (nth 2 t1))
  15879. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  15880. time (org-fix-decoded-time t1)
  15881. str (org-add-props
  15882. (format-time-string
  15883. (substring tf 1 -1) (apply 'encode-time time))
  15884. nil 'mouse-face 'highlight)
  15885. w2 (length str))
  15886. (unless (= w2 w1)
  15887. (add-text-properties (1+ beg) (+ 2 beg)
  15888. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  15889. (if (featurep 'xemacs)
  15890. (progn
  15891. (put-text-property beg end 'invisible t)
  15892. (put-text-property beg end 'end-glyph (make-glyph str)))
  15893. (put-text-property beg end 'display str))))
  15894. (defun org-fix-decoded-time (time)
  15895. "Set 0 instead of nil for the first 6 elements of time.
  15896. Don't touch the rest."
  15897. (let ((n 0))
  15898. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  15899. (define-obsolete-function-alias 'org-days-to-time 'org-time-stamp-to-now "24.4")
  15900. (defun org-time-stamp-to-now (timestamp-string &optional seconds)
  15901. "Difference between TIMESTAMP-STRING and now in days.
  15902. If SECONDS is non-nil, return the difference in seconds."
  15903. (let ((fdiff (if seconds 'org-float-time 'time-to-days)))
  15904. (- (funcall fdiff (org-time-string-to-time timestamp-string))
  15905. (funcall fdiff (current-time)))))
  15906. (defun org-deadline-close (timestamp-string &optional ndays)
  15907. "Is the time in TIMESTAMP-STRING close to the current date?"
  15908. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  15909. (and (< (org-time-stamp-to-now timestamp-string) ndays)
  15910. (not (org-entry-is-done-p))))
  15911. (defun org-get-wdays (ts &optional delay zero-delay)
  15912. "Get the deadline lead time appropriate for timestring TS.
  15913. When DELAY is non-nil, get the delay time for scheduled items
  15914. instead of the deadline lead time. When ZERO-DELAY is non-nil
  15915. and `org-scheduled-delay-days' is 0, enforce 0 as the delay,
  15916. don't try to find the delay cookie in the scheduled timestamp."
  15917. (let ((tv (if delay org-scheduled-delay-days
  15918. org-deadline-warning-days)))
  15919. (cond
  15920. ((or (and delay (< tv 0))
  15921. (and delay zero-delay (<= tv 0))
  15922. (and (not delay) (<= tv 0)))
  15923. ;; Enforce this value no matter what
  15924. (- tv))
  15925. ((string-match "-\\([0-9]+\\)\\([hdwmy]\\)\\(\\'\\|>\\| \\)" ts)
  15926. ;; lead time is specified.
  15927. (floor (* (string-to-number (match-string 1 ts))
  15928. (cdr (assoc (match-string 2 ts)
  15929. '(("d" . 1) ("w" . 7)
  15930. ("m" . 30.4) ("y" . 365.25)
  15931. ("h" . 0.041667)))))))
  15932. ;; go for the default.
  15933. (t tv))))
  15934. (defun org-calendar-select-mouse (ev)
  15935. "Return to `org-read-date' with the date currently selected.
  15936. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  15937. (interactive "e")
  15938. (mouse-set-point ev)
  15939. (when (calendar-cursor-to-date)
  15940. (let* ((date (calendar-cursor-to-date))
  15941. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15942. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  15943. (when (active-minibuffer-window) (exit-minibuffer))))
  15944. (defun org-check-deadlines (ndays)
  15945. "Check if there are any deadlines due or past due.
  15946. A deadline is considered due if it happens within `org-deadline-warning-days'
  15947. days from today's date. If the deadline appears in an entry marked DONE,
  15948. it is not shown. The prefix arg NDAYS can be used to test that many
  15949. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  15950. (interactive "P")
  15951. (let* ((org-warn-days
  15952. (cond
  15953. ((equal ndays '(4)) 100000)
  15954. (ndays (prefix-numeric-value ndays))
  15955. (t (abs org-deadline-warning-days))))
  15956. (case-fold-search nil)
  15957. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  15958. (callback
  15959. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  15960. (message "%d deadlines past-due or due within %d days"
  15961. (org-occur regexp nil callback)
  15962. org-warn-days)))
  15963. (defsubst org-re-timestamp (type)
  15964. "Return a regexp for timestamp TYPE.
  15965. Allowed values for TYPE are:
  15966. all: all timestamps
  15967. active: only active timestamps (<...>)
  15968. inactive: only inactive timestamps ([...])
  15969. scheduled: only scheduled timestamps
  15970. deadline: only deadline timestamps
  15971. closed: only closed time-stamps
  15972. When TYPE is nil, fall back on returning a regexp that matches
  15973. both scheduled and deadline timestamps."
  15974. (case type
  15975. (all org-ts-regexp-both)
  15976. (active org-ts-regexp)
  15977. (inactive org-ts-regexp-inactive)
  15978. (scheduled org-scheduled-time-regexp)
  15979. (deadline org-deadline-time-regexp)
  15980. (closed org-closed-time-regexp)
  15981. (otherwise
  15982. (concat "\\<"
  15983. (regexp-opt (list org-deadline-string org-scheduled-string))
  15984. " *<\\([^>]+\\)>"))))
  15985. (defun org-check-before-date (date)
  15986. "Check if there are deadlines or scheduled entries before DATE."
  15987. (interactive (list (org-read-date)))
  15988. (let ((case-fold-search nil)
  15989. (regexp (org-re-timestamp org-ts-type))
  15990. (callback
  15991. `(lambda ()
  15992. (let ((match (match-string 1)))
  15993. (and ,(if (memq org-ts-type '(active inactive all))
  15994. '(eq (org-element-type (org-element-context)) 'timestamp)
  15995. '(org-at-planning-p))
  15996. (time-less-p
  15997. (org-time-string-to-time match)
  15998. (org-time-string-to-time date)))))))
  15999. (message "%d entries before %s"
  16000. (org-occur regexp nil callback) date)))
  16001. (defun org-check-after-date (date)
  16002. "Check if there are deadlines or scheduled entries after DATE."
  16003. (interactive (list (org-read-date)))
  16004. (let ((case-fold-search nil)
  16005. (regexp (org-re-timestamp org-ts-type))
  16006. (callback
  16007. `(lambda ()
  16008. (let ((match (match-string 1)))
  16009. (and ,(if (memq org-ts-type '(active inactive all))
  16010. '(eq (org-element-type (org-element-context)) 'timestamp)
  16011. '(org-at-planning-p))
  16012. (not (time-less-p
  16013. (org-time-string-to-time match)
  16014. (org-time-string-to-time date))))))))
  16015. (message "%d entries after %s"
  16016. (org-occur regexp nil callback) date)))
  16017. (defun org-check-dates-range (start-date end-date)
  16018. "Check for deadlines/scheduled entries between START-DATE and END-DATE."
  16019. (interactive (list (org-read-date nil nil nil "Range starts")
  16020. (org-read-date nil nil nil "Range end")))
  16021. (let ((case-fold-search nil)
  16022. (regexp (org-re-timestamp org-ts-type))
  16023. (callback
  16024. `(lambda ()
  16025. (let ((match (match-string 1)))
  16026. (and
  16027. ,(if (memq org-ts-type '(active inactive all))
  16028. '(eq (org-element-type (org-element-context)) 'timestamp)
  16029. '(org-at-planning-p))
  16030. (not (time-less-p
  16031. (org-time-string-to-time match)
  16032. (org-time-string-to-time start-date)))
  16033. (time-less-p
  16034. (org-time-string-to-time match)
  16035. (org-time-string-to-time end-date)))))))
  16036. (message "%d entries between %s and %s"
  16037. (org-occur regexp nil callback) start-date end-date)))
  16038. (defun org-evaluate-time-range (&optional to-buffer)
  16039. "Evaluate a time range by computing the difference between start and end.
  16040. Normally the result is just printed in the echo area, but with prefix arg
  16041. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  16042. If the time range is actually in a table, the result is inserted into the
  16043. next column.
  16044. For time difference computation, a year is assumed to be exactly 365
  16045. days in order to avoid rounding problems."
  16046. (interactive "P")
  16047. (or
  16048. (org-clock-update-time-maybe)
  16049. (save-excursion
  16050. (unless (org-at-date-range-p t)
  16051. (goto-char (point-at-bol))
  16052. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  16053. (unless (org-at-date-range-p t)
  16054. (user-error "Not at a time-stamp range, and none found in current line")))
  16055. (let* ((ts1 (match-string 1))
  16056. (ts2 (match-string 2))
  16057. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  16058. (match-end (match-end 0))
  16059. (time1 (org-time-string-to-time ts1))
  16060. (time2 (org-time-string-to-time ts2))
  16061. (t1 (org-float-time time1))
  16062. (t2 (org-float-time time2))
  16063. (diff (abs (- t2 t1)))
  16064. (negative (< (- t2 t1) 0))
  16065. ;; (ys (floor (* 365 24 60 60)))
  16066. (ds (* 24 60 60))
  16067. (hs (* 60 60))
  16068. (fy "%dy %dd %02d:%02d")
  16069. (fy1 "%dy %dd")
  16070. (fd "%dd %02d:%02d")
  16071. (fd1 "%dd")
  16072. (fh "%02d:%02d")
  16073. y d h m align)
  16074. (if havetime
  16075. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  16076. y 0
  16077. d (floor (/ diff ds)) diff (mod diff ds)
  16078. h (floor (/ diff hs)) diff (mod diff hs)
  16079. m (floor (/ diff 60)))
  16080. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  16081. y 0
  16082. d (floor (+ (/ diff ds) 0.5))
  16083. h 0 m 0))
  16084. (if (not to-buffer)
  16085. (message "%s" (org-make-tdiff-string y d h m))
  16086. (if (org-at-table-p)
  16087. (progn
  16088. (goto-char match-end)
  16089. (setq align t)
  16090. (and (looking-at " *|") (goto-char (match-end 0))))
  16091. (goto-char match-end))
  16092. (when (looking-at
  16093. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  16094. (replace-match ""))
  16095. (when negative (insert " -"))
  16096. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  16097. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  16098. (insert " " (format fh h m))))
  16099. (when align (org-table-align))
  16100. (message "Time difference inserted")))))
  16101. (defun org-make-tdiff-string (y d h m)
  16102. (let ((fmt "")
  16103. (l nil))
  16104. (when (> y 0)
  16105. (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " "))
  16106. (push y l))
  16107. (when (> d 0)
  16108. (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " "))
  16109. (push d l))
  16110. (when (> h 0)
  16111. (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " "))
  16112. (push h l))
  16113. (when (> m 0)
  16114. (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " "))
  16115. (push m l))
  16116. (apply 'format fmt (nreverse l))))
  16117. (defun org-time-string-to-time (s &optional buffer pos)
  16118. "Convert a timestamp string into internal time."
  16119. (condition-case errdata
  16120. (apply 'encode-time (org-parse-time-string s))
  16121. (error (error "Bad timestamp `%s'%s\nError was: %s"
  16122. s (if (not (and buffer pos))
  16123. ""
  16124. (format-message " at %d in buffer `%s'" pos buffer))
  16125. (cdr errdata)))))
  16126. (defun org-time-string-to-seconds (s)
  16127. "Convert a timestamp string to a number of seconds."
  16128. (org-float-time (org-time-string-to-time s)))
  16129. (org-define-error 'org-diary-sexp-no-match "Unable to match diary sexp")
  16130. (defun org-time-string-to-absolute (s &optional daynr prefer show-all buffer pos)
  16131. "Convert time stamp S to an absolute day number.
  16132. If DAYNR in non-nil, and there is a specifier for a cyclic time
  16133. stamp, get the closest date to DAYNR. If PREFER is
  16134. `past' (respectively `future') return a date past (respectively
  16135. after) or equal to DAYNR.
  16136. POS is the location of time stamp S, as a buffer position in
  16137. BUFFER.
  16138. Diary sexp timestamps are matched against DAYNR, when non-nil.
  16139. If matching fails or DAYNR is nil, `org-diary-sexp-no-match' is
  16140. signalled."
  16141. (cond
  16142. ((string-match "\\`%%\\((.*)\\)" s)
  16143. ;; Sexp timestamp: try to match DAYNR, if available, since we're
  16144. ;; only able to match individual dates. If it fails, raise an
  16145. ;; error.
  16146. (if (and daynr
  16147. (org-diary-sexp-entry
  16148. (match-string 1 s) "" (calendar-gregorian-from-absolute daynr)))
  16149. daynr
  16150. (signal 'org-diary-sexp-no-match (list s))))
  16151. ((and daynr show-all) (org-closest-date s daynr prefer))
  16152. (t (time-to-days
  16153. (condition-case errdata
  16154. (apply #'encode-time (org-parse-time-string s))
  16155. (error (error "Bad timestamp `%s'%s\nError was: %s"
  16156. s
  16157. (if (not (and buffer pos)) ""
  16158. (format-message " at %d in buffer `%s'" pos buffer))
  16159. (cdr errdata))))))))
  16160. (defun org-days-to-iso-week (days)
  16161. "Return the iso week number."
  16162. (require 'cal-iso)
  16163. (car (calendar-iso-from-absolute days)))
  16164. (defun org-small-year-to-year (year)
  16165. "Convert 2-digit years into 4-digit years.
  16166. YEAR is expanded into one of the 30 next years, if possible, or
  16167. into a past one. Any year larger than 99 is returned unchanged."
  16168. (if (>= year 100) year
  16169. (let* ((current (string-to-number (format-time-string "%Y" (current-time))))
  16170. (century (/ current 100))
  16171. (offset (- year (% current 100))))
  16172. (cond ((> offset 30) (+ (* (1- century) 100) year))
  16173. ((> offset -70) (+ (* century 100) year))
  16174. (t (+ (* (1+ century) 100) year))))))
  16175. (defun org-time-from-absolute (d)
  16176. "Return the time corresponding to date D.
  16177. D may be an absolute day number, or a calendar-type list (month day year)."
  16178. (when (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  16179. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  16180. (defun org-calendar-holiday ()
  16181. "List of holidays, for Diary display in Org-mode."
  16182. (require 'holidays)
  16183. (let ((hl (funcall
  16184. (if (fboundp 'calendar-check-holidays)
  16185. 'calendar-check-holidays 'check-calendar-holidays) date)))
  16186. (when hl (mapconcat 'identity hl "; "))))
  16187. (defun org-diary-sexp-entry (sexp entry date)
  16188. "Process a SEXP diary ENTRY for DATE."
  16189. (require 'diary-lib)
  16190. (let ((result (if calendar-debug-sexp
  16191. (let ((stack-trace-on-error t))
  16192. (eval (car (read-from-string sexp))))
  16193. (condition-case nil
  16194. (eval (car (read-from-string sexp)))
  16195. (error
  16196. (beep)
  16197. (message "Bad sexp at line %d in %s: %s"
  16198. (org-current-line)
  16199. (buffer-file-name) sexp)
  16200. (sleep-for 2))))))
  16201. (cond ((stringp result) (split-string result "; "))
  16202. ((and (consp result)
  16203. (not (consp (cdr result)))
  16204. (stringp (cdr result))) (cdr result))
  16205. ((and (consp result)
  16206. (stringp (car result))) result)
  16207. (result entry))))
  16208. (defun org-diary-to-ical-string (frombuf)
  16209. "Get iCalendar entries from diary entries in buffer FROMBUF.
  16210. This uses the icalendar.el library."
  16211. (let* ((tmpdir (if (featurep 'xemacs)
  16212. (temp-directory)
  16213. temporary-file-directory))
  16214. (tmpfile (make-temp-name
  16215. (expand-file-name "orgics" tmpdir)))
  16216. buf rtn b e)
  16217. (with-current-buffer frombuf
  16218. (icalendar-export-region (point-min) (point-max) tmpfile)
  16219. (setq buf (find-buffer-visiting tmpfile))
  16220. (set-buffer buf)
  16221. (goto-char (point-min))
  16222. (when (re-search-forward "^BEGIN:VEVENT" nil t)
  16223. (setq b (match-beginning 0)))
  16224. (goto-char (point-max))
  16225. (when (re-search-backward "^END:VEVENT" nil t)
  16226. (setq e (match-end 0)))
  16227. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  16228. (kill-buffer buf)
  16229. (delete-file tmpfile)
  16230. rtn))
  16231. (defun org-closest-date (start current prefer)
  16232. "Return closest date to CURRENT starting from START.
  16233. CURRENT and START are both time stamps.
  16234. When PREFER is `past', return a date that is either CURRENT or
  16235. past. When PREFER is `future', return a date that is either
  16236. CURRENT or future.
  16237. Only time stamps with a repeater are modified. Any other time
  16238. stamp stay unchanged. In any case, return value is an absolute
  16239. day number."
  16240. (if (not (string-match "\\+\\([0-9]+\\)\\([hdwmy]\\)" start))
  16241. ;; No repeater. Do not shift time stamp.
  16242. (time-to-days (apply #'encode-time (org-parse-time-string start)))
  16243. (let ((value (string-to-number (match-string 1 start)))
  16244. (type (match-string 2 start)))
  16245. (if (= 0 value)
  16246. ;; Repeater with a 0-value is considered as void.
  16247. (time-to-days (apply #'encode-time (org-parse-time-string start)))
  16248. (let* ((base (org-date-to-gregorian start))
  16249. (target (org-date-to-gregorian current))
  16250. (sday (calendar-absolute-from-gregorian base))
  16251. (cday (calendar-absolute-from-gregorian target))
  16252. n1 n2)
  16253. ;; If START is already past CURRENT, just return START.
  16254. (if (<= cday sday) sday
  16255. ;; Compute closest date before (N1) and closest date past
  16256. ;; (N2) CURRENT.
  16257. (pcase type
  16258. ("h"
  16259. (let ((missing-hours
  16260. (mod (+ (- (* 24 (- cday sday))
  16261. (nth 2 (org-parse-time-string start)))
  16262. org-extend-today-until)
  16263. value)))
  16264. (setf n1 (if (= missing-hours 0) cday
  16265. (- cday (1+ (/ missing-hours 24)))))
  16266. (setf n2 (+ cday (/ (- value missing-hours) 24)))))
  16267. ((or "d" "w")
  16268. (let ((value (if (equal type "w") (* 7 value) value)))
  16269. (setf n1 (+ sday (* value (/ (- cday sday) value))))
  16270. (setf n2 (+ n1 value))))
  16271. ("m"
  16272. (let* ((add-months
  16273. (lambda (date n)
  16274. ;; Add N months to gregorian DATE, i.e.,
  16275. ;; a list (MONTH DAY YEAR). Return a valid
  16276. ;; gregorian date.
  16277. (let ((m (+ (nth 0 date) n)))
  16278. (list (mod m 12)
  16279. (nth 1 date)
  16280. (+ (/ m 12) (nth 2 date))))))
  16281. (months ; Complete months to TARGET.
  16282. (* (/ (+ (* 12 (- (nth 2 target) (nth 2 base)))
  16283. (- (nth 0 target) (nth 0 base))
  16284. ;; If START's day is greater than
  16285. ;; TARGET's, remove incomplete month.
  16286. (if (> (nth 1 target) (nth 1 base)) 0 -1))
  16287. value)
  16288. value))
  16289. (before (funcall add-months base months)))
  16290. (setf n1 (calendar-absolute-from-gregorian before))
  16291. (setf n2
  16292. (calendar-absolute-from-gregorian
  16293. (funcall add-months before value)))))
  16294. (_
  16295. (let* ((d (nth 1 base))
  16296. (m (nth 0 base))
  16297. (y (nth 2 base))
  16298. (years ; Complete years to TARGET.
  16299. (* (/ (- (nth 2 target)
  16300. y
  16301. ;; If START's month and day are
  16302. ;; greater than TARGET's, remove
  16303. ;; incomplete year.
  16304. (if (or (> (nth 0 target) m)
  16305. (and (= (nth 0 target) m)
  16306. (> (nth 1 target) d)))
  16307. 0
  16308. 1))
  16309. value)
  16310. value))
  16311. (before (list m d (+ y years))))
  16312. (setf n1 (calendar-absolute-from-gregorian before))
  16313. (setf n2 (calendar-absolute-from-gregorian
  16314. (list m d (+ (nth 2 before) value)))))))
  16315. ;; Handle PREFER parameter, if any.
  16316. (cond
  16317. ((eq prefer 'past) (if (= cday n2) n2 n1))
  16318. ((eq prefer 'future) (if (= cday n1) n1 n2))
  16319. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))))))))
  16320. (defun org-date-to-gregorian (date)
  16321. "Turn any specification of DATE into a Gregorian date for the calendar."
  16322. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  16323. ((and (listp date) (= (length date) 3)) date)
  16324. ((stringp date)
  16325. (setq date (org-parse-time-string date))
  16326. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  16327. ((listp date)
  16328. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  16329. (defun org-parse-time-string (s &optional nodefault)
  16330. "Parse the standard Org-mode time string.
  16331. This should be a lot faster than the normal `parse-time-string'.
  16332. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  16333. hour and minute fields will be nil if not given."
  16334. (cond ((string-match org-ts-regexp0 s)
  16335. (list 0
  16336. (when (or (match-beginning 8) (not nodefault))
  16337. (string-to-number (or (match-string 8 s) "0")))
  16338. (when (or (match-beginning 7) (not nodefault))
  16339. (string-to-number (or (match-string 7 s) "0")))
  16340. (string-to-number (match-string 4 s))
  16341. (string-to-number (match-string 3 s))
  16342. (string-to-number (match-string 2 s))
  16343. nil nil nil))
  16344. ((string-match "^<[^>]+>$" s)
  16345. (decode-time (seconds-to-time (org-matcher-time s))))
  16346. (t (error "Not a standard Org-mode time string: %s" s))))
  16347. (defun org-timestamp-up (&optional arg)
  16348. "Increase the date item at the cursor by one.
  16349. If the cursor is on the year, change the year. If it is on the month,
  16350. the day or the time, change that.
  16351. With prefix ARG, change by that many units."
  16352. (interactive "p")
  16353. (org-timestamp-change (prefix-numeric-value arg) nil 'updown))
  16354. (defun org-timestamp-down (&optional arg)
  16355. "Decrease the date item at the cursor by one.
  16356. If the cursor is on the year, change the year. If it is on the month,
  16357. the day or the time, change that.
  16358. With prefix ARG, change by that many units."
  16359. (interactive "p")
  16360. (org-timestamp-change (- (prefix-numeric-value arg)) nil 'updown))
  16361. (defun org-timestamp-up-day (&optional arg)
  16362. "Increase the date in the time stamp by one day.
  16363. With prefix ARG, change that many days."
  16364. (interactive "p")
  16365. (if (and (not (org-at-timestamp-p t))
  16366. (org-at-heading-p))
  16367. (org-todo 'up)
  16368. (org-timestamp-change (prefix-numeric-value arg) 'day 'updown)))
  16369. (defun org-timestamp-down-day (&optional arg)
  16370. "Decrease the date in the time stamp by one day.
  16371. With prefix ARG, change that many days."
  16372. (interactive "p")
  16373. (if (and (not (org-at-timestamp-p t))
  16374. (org-at-heading-p))
  16375. (org-todo 'down)
  16376. (org-timestamp-change (- (prefix-numeric-value arg)) 'day) 'updown))
  16377. (defun org-at-timestamp-p (&optional inactive-ok)
  16378. "Non-nil if point is inside a timestamp.
  16379. When optional argument INACTIVE-OK is non-nil, also consider
  16380. inactive timestamps.
  16381. When this function returns a non-nil value, match data is set
  16382. according to `org-ts-regexp3' or `org-ts-regexp2', depending on
  16383. INACTIVE-OK."
  16384. (interactive)
  16385. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  16386. (pos (point))
  16387. (ans (or (looking-at tsr)
  16388. (save-excursion
  16389. (skip-chars-backward "^[<\n\r\t")
  16390. (when (> (point) (point-min)) (backward-char 1))
  16391. (and (looking-at tsr)
  16392. (> (- (match-end 0) pos) -1))))))
  16393. (and ans
  16394. (boundp 'org-ts-what)
  16395. (setq org-ts-what
  16396. (cond
  16397. ((= pos (match-beginning 0)) 'bracket)
  16398. ;; Point is considered to be "on the bracket" whether
  16399. ;; it's really on it or right after it.
  16400. ((= pos (1- (match-end 0))) 'bracket)
  16401. ((= pos (match-end 0)) 'after)
  16402. ((org-pos-in-match-range pos 2) 'year)
  16403. ((org-pos-in-match-range pos 3) 'month)
  16404. ((org-pos-in-match-range pos 7) 'hour)
  16405. ((org-pos-in-match-range pos 8) 'minute)
  16406. ((or (org-pos-in-match-range pos 4)
  16407. (org-pos-in-match-range pos 5)) 'day)
  16408. ((and (> pos (or (match-end 8) (match-end 5)))
  16409. (< pos (match-end 0)))
  16410. (- pos (or (match-end 8) (match-end 5))))
  16411. (t 'day))))
  16412. ans))
  16413. (defun org-toggle-timestamp-type ()
  16414. "Toggle the type (<active> or [inactive]) of a time stamp."
  16415. (interactive)
  16416. (when (org-at-timestamp-p t)
  16417. (let ((beg (match-beginning 0)) (end (match-end 0))
  16418. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  16419. (save-excursion
  16420. (goto-char beg)
  16421. (while (re-search-forward "[][<>]" end t)
  16422. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  16423. t t)))
  16424. (message "Timestamp is now %sactive"
  16425. (if (equal (char-after beg) ?<) "" "in")))))
  16426. (defun org-at-clock-log-p nil
  16427. "Is the cursor on the clock log line?"
  16428. (save-excursion
  16429. (move-beginning-of-line 1)
  16430. (looking-at org-clock-line-re)))
  16431. (defvar org-clock-history) ; defined in org-clock.el
  16432. (defvar org-clock-adjust-closest nil) ; defined in org-clock.el
  16433. (defun org-timestamp-change (n &optional what updown suppress-tmp-delay)
  16434. "Change the date in the time stamp at point.
  16435. The date will be changed by N times WHAT. WHAT can be `day', `month',
  16436. `year', `minute', `second'. If WHAT is not given, the cursor position
  16437. in the timestamp determines what will be changed.
  16438. When SUPPRESS-TMP-DELAY is non-nil, suppress delays like \"--2d\"."
  16439. (let ((origin (point)) origin-cat
  16440. with-hm inactive
  16441. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  16442. org-ts-what
  16443. extra rem
  16444. ts time time0 fixnext clrgx)
  16445. (unless (org-at-timestamp-p t)
  16446. (user-error "Not at a timestamp"))
  16447. (if (and (not what) (eq org-ts-what 'bracket))
  16448. (org-toggle-timestamp-type)
  16449. ;; Point isn't on brackets. Remember the part of the time-stamp
  16450. ;; the point was in. Indeed, size of time-stamps may change,
  16451. ;; but point must be kept in the same category nonetheless.
  16452. (setq origin-cat org-ts-what)
  16453. (when (and (not what) (not (eq org-ts-what 'day))
  16454. org-display-custom-times
  16455. (get-text-property (point) 'display)
  16456. (not (get-text-property (1- (point)) 'display)))
  16457. (setq org-ts-what 'day))
  16458. (setq org-ts-what (or what org-ts-what)
  16459. inactive (= (char-after (match-beginning 0)) ?\[)
  16460. ts (match-string 0))
  16461. (replace-match "")
  16462. (when (string-match
  16463. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?-?[-+][0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)*\\)[]>]"
  16464. ts)
  16465. (setq extra (match-string 1 ts))
  16466. (when suppress-tmp-delay
  16467. (setq extra (replace-regexp-in-string " --[0-9]+[hdwmy]" "" extra))))
  16468. (when (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  16469. (setq with-hm t))
  16470. (setq time0 (org-parse-time-string ts))
  16471. (when (and updown
  16472. (eq org-ts-what 'minute)
  16473. (not current-prefix-arg))
  16474. ;; This looks like s-up and s-down. Change by one rounding step.
  16475. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  16476. (unless (= 0 (setq rem (% (nth 1 time0) dm)))
  16477. (setcar (cdr time0) (+ (nth 1 time0)
  16478. (if (> n 0) (- rem) (- dm rem))))))
  16479. (setq time
  16480. (encode-time (or (car time0) 0)
  16481. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  16482. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  16483. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  16484. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  16485. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  16486. (nthcdr 6 time0)))
  16487. (when (and (member org-ts-what '(hour minute))
  16488. extra
  16489. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  16490. (setq extra (org-modify-ts-extra
  16491. extra
  16492. (if (eq org-ts-what 'hour) 2 5)
  16493. n dm)))
  16494. (when (integerp org-ts-what)
  16495. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  16496. (when (eq what 'calendar)
  16497. (let ((cal-date (org-get-date-from-calendar)))
  16498. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  16499. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  16500. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  16501. (setcar time0 (or (car time0) 0))
  16502. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  16503. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  16504. (setq time (apply 'encode-time time0))))
  16505. ;; Insert the new time-stamp, and ensure point stays in the same
  16506. ;; category as before (i.e. not after the last position in that
  16507. ;; category).
  16508. (let ((pos (point)))
  16509. ;; Stay before inserted string. `save-excursion' is of no use.
  16510. (setq org-last-changed-timestamp
  16511. (org-insert-time-stamp time with-hm inactive nil nil extra))
  16512. (goto-char pos))
  16513. (save-match-data
  16514. (looking-at org-ts-regexp3)
  16515. (goto-char (cond
  16516. ;; `day' category ends before `hour' if any, or at
  16517. ;; the end of the day name.
  16518. ((eq origin-cat 'day)
  16519. (min (or (match-beginning 7) (1- (match-end 5))) origin))
  16520. ((eq origin-cat 'hour) (min (match-end 7) origin))
  16521. ((eq origin-cat 'minute) (min (1- (match-end 8)) origin))
  16522. ((integerp origin-cat) (min (1- (match-end 0)) origin))
  16523. ;; `year' and `month' have both fixed size: point
  16524. ;; couldn't have moved into another part.
  16525. (t origin))))
  16526. ;; Update clock if on a CLOCK line.
  16527. (org-clock-update-time-maybe)
  16528. ;; Maybe adjust the closest clock in `org-clock-history'
  16529. (when org-clock-adjust-closest
  16530. (if (not (and (org-at-clock-log-p)
  16531. (< 1 (length (delq nil (mapcar 'marker-position
  16532. org-clock-history))))))
  16533. (message "No clock to adjust")
  16534. (cond ((save-excursion ; fix previous clock?
  16535. (re-search-backward org-ts-regexp0 nil t)
  16536. (org-looking-back (concat org-clock-string " \\[")
  16537. (line-beginning-position)))
  16538. (setq fixnext 1 clrgx (concat org-ts-regexp0 "\\] =>.*$")))
  16539. ((save-excursion ; fix next clock?
  16540. (re-search-backward org-ts-regexp0 nil t)
  16541. (looking-at (concat org-ts-regexp0 "\\] =>")))
  16542. (setq fixnext -1 clrgx (concat org-clock-string " \\[" org-ts-regexp0))))
  16543. (save-window-excursion
  16544. ;; Find closest clock to point, adjust the previous/next one in history
  16545. (let* ((p (save-excursion (org-back-to-heading t)))
  16546. (cl (mapcar (lambda(c) (abs (- (marker-position c) p))) org-clock-history))
  16547. (clfixnth
  16548. (+ fixnext (- (length cl) (or (length (member (apply 'min cl) cl)) 100))))
  16549. (clfixpos (unless (> 0 clfixnth) (nth clfixnth org-clock-history))))
  16550. (if (not clfixpos)
  16551. (message "No clock to adjust")
  16552. (save-excursion
  16553. (org-goto-marker-or-bmk clfixpos)
  16554. (org-show-subtree)
  16555. (when (re-search-forward clrgx nil t)
  16556. (goto-char (match-beginning 1))
  16557. (let (org-clock-adjust-closest)
  16558. (org-timestamp-change n org-ts-what updown))
  16559. (message "Clock adjusted in %s for heading: %s"
  16560. (file-name-nondirectory (buffer-file-name))
  16561. (org-get-heading t t)))))))))
  16562. ;; Try to recenter the calendar window, if any.
  16563. (when (and org-calendar-follow-timestamp-change
  16564. (get-buffer-window "*Calendar*" t)
  16565. (memq org-ts-what '(day month year)))
  16566. (org-recenter-calendar (time-to-days time))))))
  16567. (defun org-modify-ts-extra (s pos n dm)
  16568. "Change the different parts of the lead-time and repeat fields in timestamp."
  16569. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  16570. ng h m new rem)
  16571. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  16572. (cond
  16573. ((or (org-pos-in-match-range pos 2)
  16574. (org-pos-in-match-range pos 3))
  16575. (setq m (string-to-number (match-string 3 s))
  16576. h (string-to-number (match-string 2 s)))
  16577. (if (org-pos-in-match-range pos 2)
  16578. (setq h (+ h n))
  16579. (setq n (* dm (org-no-warnings (signum n))))
  16580. (unless (= 0 (setq rem (% m dm)))
  16581. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  16582. (setq m (+ m n)))
  16583. (when (< m 0) (setq m (+ m 60) h (1- h)))
  16584. (when (> m 59) (setq m (- m 60) h (1+ h)))
  16585. (setq h (mod h 24))
  16586. (setq ng 1 new (format "-%02d:%02d" h m)))
  16587. ((org-pos-in-match-range pos 6)
  16588. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  16589. ((org-pos-in-match-range pos 5)
  16590. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  16591. ((org-pos-in-match-range pos 9)
  16592. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  16593. ((org-pos-in-match-range pos 8)
  16594. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  16595. (when ng
  16596. (setq s (concat
  16597. (substring s 0 (match-beginning ng))
  16598. new
  16599. (substring s (match-end ng))))))
  16600. s))
  16601. (defun org-recenter-calendar (date)
  16602. "If the calendar is visible, recenter it to DATE."
  16603. (let ((cwin (get-buffer-window "*Calendar*" t)))
  16604. (when cwin
  16605. (let ((calendar-move-hook nil))
  16606. (with-selected-window cwin
  16607. (calendar-goto-date (if (listp date) date
  16608. (calendar-gregorian-from-absolute date))))))))
  16609. (defun org-goto-calendar (&optional arg)
  16610. "Go to the Emacs calendar at the current date.
  16611. If there is a time stamp in the current line, go to that date.
  16612. A prefix ARG can be used to force the current date."
  16613. (interactive "P")
  16614. (let ((tsr org-ts-regexp) diff
  16615. (calendar-move-hook nil)
  16616. (calendar-view-holidays-initially-flag nil)
  16617. (calendar-view-diary-initially-flag nil))
  16618. (when (or (org-at-timestamp-p)
  16619. (save-excursion
  16620. (beginning-of-line 1)
  16621. (looking-at (concat ".*" tsr))))
  16622. (let ((d1 (time-to-days (current-time)))
  16623. (d2 (time-to-days
  16624. (org-time-string-to-time (match-string 1)))))
  16625. (setq diff (- d2 d1))))
  16626. (calendar)
  16627. (calendar-goto-today)
  16628. (when (and diff (not arg)) (calendar-forward-day diff))))
  16629. (defun org-get-date-from-calendar ()
  16630. "Return a list (month day year) of date at point in calendar."
  16631. (with-current-buffer "*Calendar*"
  16632. (save-match-data
  16633. (calendar-cursor-to-date))))
  16634. (defun org-date-from-calendar ()
  16635. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  16636. If there is already a time stamp at the cursor position, update it."
  16637. (interactive)
  16638. (if (org-at-timestamp-p t)
  16639. (org-timestamp-change 0 'calendar)
  16640. (let ((cal-date (org-get-date-from-calendar)))
  16641. (org-insert-time-stamp
  16642. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  16643. (defcustom org-effort-durations
  16644. `(("min" . 1)
  16645. ("h" . 60)
  16646. ("d" . ,(* 60 8))
  16647. ("w" . ,(* 60 8 5))
  16648. ("m" . ,(* 60 8 5 4))
  16649. ("y" . ,(* 60 8 5 40)))
  16650. "Conversion factor to minutes for an effort modifier.
  16651. Each entry has the form (MODIFIER . MINUTES).
  16652. In an effort string, a number followed by MODIFIER is multiplied
  16653. by the specified number of MINUTES to obtain an effort in
  16654. minutes.
  16655. For example, if the value of this variable is ((\"hours\" . 60)), then an
  16656. effort string \"2hours\" is equivalent to 120 minutes."
  16657. :group 'org-agenda
  16658. :version "25.1"
  16659. :package-version '(Org . "8.3")
  16660. :type '(alist :key-type (string :tag "Modifier")
  16661. :value-type (number :tag "Minutes")))
  16662. (defun org-minutes-to-clocksum-string (m)
  16663. "Format number of minutes as a clocksum string.
  16664. The format is determined by `org-time-clocksum-format',
  16665. `org-time-clocksum-use-fractional' and
  16666. `org-time-clocksum-fractional-format' and
  16667. `org-time-clocksum-use-effort-durations'."
  16668. (let ((clocksum "")
  16669. (m (round m)) ; Don't allow fractions of minutes
  16670. h d w mo y fmt n)
  16671. (setq h (if org-time-clocksum-use-effort-durations
  16672. (cdr (assoc "h" org-effort-durations)) 60)
  16673. d (if org-time-clocksum-use-effort-durations
  16674. (/ (cdr (assoc "d" org-effort-durations)) h) 24)
  16675. w (if org-time-clocksum-use-effort-durations
  16676. (/ (cdr (assoc "w" org-effort-durations)) (* d h)) 7)
  16677. mo (if org-time-clocksum-use-effort-durations
  16678. (/ (cdr (assoc "m" org-effort-durations)) (* d h)) 30)
  16679. y (if org-time-clocksum-use-effort-durations
  16680. (/ (cdr (assoc "y" org-effort-durations)) (* d h)) 365))
  16681. ;; fractional format
  16682. (if org-time-clocksum-use-fractional
  16683. (cond
  16684. ;; single format string
  16685. ((stringp org-time-clocksum-fractional-format)
  16686. (format org-time-clocksum-fractional-format (/ m (float h))))
  16687. ;; choice of fractional formats for different time units
  16688. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :years))
  16689. (> (/ (truncate m) (* y d h)) 0))
  16690. (format fmt (/ m (* y d (float h)))))
  16691. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :months))
  16692. (> (/ (truncate m) (* mo d h)) 0))
  16693. (format fmt (/ m (* mo d (float h)))))
  16694. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :weeks))
  16695. (> (/ (truncate m) (* w d h)) 0))
  16696. (format fmt (/ m (* w d (float h)))))
  16697. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :days))
  16698. (> (/ (truncate m) (* d h)) 0))
  16699. (format fmt (/ m (* d (float h)))))
  16700. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :hours))
  16701. (> (/ (truncate m) h) 0))
  16702. (format fmt (/ m (float h))))
  16703. ((setq fmt (plist-get org-time-clocksum-fractional-format :minutes))
  16704. (format fmt m))
  16705. ;; fall back to smallest time unit with a format
  16706. ((setq fmt (plist-get org-time-clocksum-fractional-format :hours))
  16707. (format fmt (/ m (float h))))
  16708. ((setq fmt (plist-get org-time-clocksum-fractional-format :days))
  16709. (format fmt (/ m (* d (float h)))))
  16710. ((setq fmt (plist-get org-time-clocksum-fractional-format :weeks))
  16711. (format fmt (/ m (* w d (float h)))))
  16712. ((setq fmt (plist-get org-time-clocksum-fractional-format :months))
  16713. (format fmt (/ m (* mo d (float h)))))
  16714. ((setq fmt (plist-get org-time-clocksum-fractional-format :years))
  16715. (format fmt (/ m (* y d (float h))))))
  16716. ;; standard (non-fractional) format, with single format string
  16717. (if (stringp org-time-clocksum-format)
  16718. (format org-time-clocksum-format (setq n (/ m h)) (- m (* h n)))
  16719. ;; separate formats components
  16720. (and (setq fmt (plist-get org-time-clocksum-format :years))
  16721. (or (> (setq n (/ (truncate m) (* y d h))) 0)
  16722. (plist-get org-time-clocksum-format :require-years))
  16723. (setq clocksum (concat clocksum (format fmt n))
  16724. m (- m (* n y d h))))
  16725. (and (setq fmt (plist-get org-time-clocksum-format :months))
  16726. (or (> (setq n (/ (truncate m) (* mo d h))) 0)
  16727. (plist-get org-time-clocksum-format :require-months))
  16728. (setq clocksum (concat clocksum (format fmt n))
  16729. m (- m (* n mo d h))))
  16730. (and (setq fmt (plist-get org-time-clocksum-format :weeks))
  16731. (or (> (setq n (/ (truncate m) (* w d h))) 0)
  16732. (plist-get org-time-clocksum-format :require-weeks))
  16733. (setq clocksum (concat clocksum (format fmt n))
  16734. m (- m (* n w d h))))
  16735. (and (setq fmt (plist-get org-time-clocksum-format :days))
  16736. (or (> (setq n (/ (truncate m) (* d h))) 0)
  16737. (plist-get org-time-clocksum-format :require-days))
  16738. (setq clocksum (concat clocksum (format fmt n))
  16739. m (- m (* n d h))))
  16740. (and (setq fmt (plist-get org-time-clocksum-format :hours))
  16741. (or (> (setq n (/ (truncate m) h)) 0)
  16742. (plist-get org-time-clocksum-format :require-hours))
  16743. (setq clocksum (concat clocksum (format fmt n))
  16744. m (- m (* n h))))
  16745. (and (setq fmt (plist-get org-time-clocksum-format :minutes))
  16746. (or (> m 0) (plist-get org-time-clocksum-format :require-minutes))
  16747. (setq clocksum (concat clocksum (format fmt m))))
  16748. ;; return formatted time duration
  16749. clocksum))))
  16750. (defalias 'org-minutes-to-hh:mm-string 'org-minutes-to-clocksum-string)
  16751. (make-obsolete 'org-minutes-to-hh:mm-string 'org-minutes-to-clocksum-string
  16752. "Org mode version 8.0")
  16753. (defun org-hours-to-clocksum-string (n)
  16754. (org-minutes-to-clocksum-string (* n 60)))
  16755. (defun org-hh:mm-string-to-minutes (s)
  16756. "Convert a string H:MM to a number of minutes.
  16757. If the string is just a number, interpret it as minutes.
  16758. In fact, the first hh:mm or number in the string will be taken,
  16759. there can be extra stuff in the string.
  16760. If no number is found, the return value is 0."
  16761. (cond
  16762. ((integerp s) s)
  16763. ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  16764. (+ (* (string-to-number (match-string 1 s)) 60)
  16765. (string-to-number (match-string 2 s))))
  16766. ((string-match "\\([0-9]+\\)" s)
  16767. (string-to-number (match-string 1 s)))
  16768. (t 0)))
  16769. (defcustom org-image-actual-width t
  16770. "Should we use the actual width of images when inlining them?
  16771. When set to t, always use the image width.
  16772. When set to a number, use imagemagick (when available) to set
  16773. the image's width to this value.
  16774. When set to a number in a list, try to get the width from any
  16775. #+ATTR.* keyword if it matches a width specification like
  16776. #+ATTR_HTML: :width 300px
  16777. and fall back on that number if none is found.
  16778. When set to nil, try to get the width from an #+ATTR.* keyword
  16779. and fall back on the original width if none is found.
  16780. This requires Emacs >= 24.1, build with imagemagick support."
  16781. :group 'org-appearance
  16782. :version "24.4"
  16783. :package-version '(Org . "8.0")
  16784. :type '(choice
  16785. (const :tag "Use the image width" t)
  16786. (integer :tag "Use a number of pixels")
  16787. (list :tag "Use #+ATTR* or a number of pixels" (integer))
  16788. (const :tag "Use #+ATTR* or don't resize" nil)))
  16789. (defcustom org-agenda-inhibit-startup nil
  16790. "Inhibit startup when preparing agenda buffers.
  16791. When this variable is t, the initialization of the Org agenda
  16792. buffers is inhibited: e.g. the visibility state is not set, the
  16793. tables are not re-aligned, etc."
  16794. :type 'boolean
  16795. :version "24.3"
  16796. :group 'org-agenda)
  16797. (define-obsolete-variable-alias
  16798. 'org-agenda-ignore-drawer-properties
  16799. 'org-agenda-ignore-properties "25.1")
  16800. (defcustom org-agenda-ignore-properties nil
  16801. "Avoid updating text properties when building the agenda.
  16802. Properties are used to prepare buffers for effort estimates,
  16803. appointments, statistics and subtree-local categories.
  16804. If you don't use these in the agenda, you can add them to this
  16805. list and agenda building will be a bit faster.
  16806. The value is a list, with zero or more of the symbols `effort', `appt',
  16807. `stats' or `category'."
  16808. :type '(set :greedy t
  16809. (const effort)
  16810. (const appt)
  16811. (const stats)
  16812. (const category))
  16813. :version "25.1"
  16814. :package-version '(Org . "8.3")
  16815. :group 'org-agenda)
  16816. (defun org-duration-string-to-minutes (s &optional output-to-string)
  16817. "Convert a duration string S to minutes.
  16818. A bare number is interpreted as minutes, modifiers can be set by
  16819. customizing `org-effort-durations' (which see).
  16820. Entries containing a colon are interpreted as H:MM by
  16821. `org-hh:mm-string-to-minutes'."
  16822. (let ((result 0)
  16823. (re (concat "\\([0-9.]+\\) *\\("
  16824. (regexp-opt (mapcar 'car org-effort-durations))
  16825. "\\)")))
  16826. (while (string-match re s)
  16827. (incf result (* (cdr (assoc (match-string 2 s) org-effort-durations))
  16828. (string-to-number (match-string 1 s))))
  16829. (setq s (replace-match "" nil t s)))
  16830. (setq result (floor result))
  16831. (incf result (org-hh:mm-string-to-minutes s))
  16832. (if output-to-string (number-to-string result) result)))
  16833. ;;;; Files
  16834. (defun org-save-all-org-buffers ()
  16835. "Save all Org-mode buffers without user confirmation."
  16836. (interactive)
  16837. (message "Saving all Org-mode buffers...")
  16838. (save-some-buffers t (lambda () (derived-mode-p 'org-mode)))
  16839. (when (featurep 'org-id) (org-id-locations-save))
  16840. (message "Saving all Org-mode buffers... done"))
  16841. (defun org-revert-all-org-buffers ()
  16842. "Revert all Org-mode buffers.
  16843. Prompt for confirmation when there are unsaved changes.
  16844. Be sure you know what you are doing before letting this function
  16845. overwrite your changes.
  16846. This function is useful in a setup where one tracks org files
  16847. with a version control system, to revert on one machine after pulling
  16848. changes from another. I believe the procedure must be like this:
  16849. 1. M-x org-save-all-org-buffers
  16850. 2. Pull changes from the other machine, resolve conflicts
  16851. 3. M-x org-revert-all-org-buffers"
  16852. (interactive)
  16853. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  16854. (user-error "Abort"))
  16855. (save-excursion
  16856. (save-window-excursion
  16857. (mapc
  16858. (lambda (b)
  16859. (when (and (with-current-buffer b (derived-mode-p 'org-mode))
  16860. (with-current-buffer b buffer-file-name))
  16861. (org-pop-to-buffer-same-window b)
  16862. (revert-buffer t 'no-confirm)))
  16863. (buffer-list))
  16864. (when (and (featurep 'org-id) org-id-track-globally)
  16865. (org-id-locations-load)))))
  16866. ;;;; Agenda files
  16867. ;;;###autoload
  16868. (defun org-switchb (&optional arg)
  16869. "Switch between Org buffers.
  16870. With one prefix argument, restrict available buffers to files.
  16871. With two prefix arguments, restrict available buffers to agenda files.
  16872. Defaults to `iswitchb' for buffer name completion.
  16873. Set `org-completion-use-ido' to make it use ido instead."
  16874. (interactive "P")
  16875. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  16876. ((equal arg '(16)) (org-buffer-list 'agenda))
  16877. (t (org-buffer-list))))
  16878. (org-completion-use-iswitchb org-completion-use-iswitchb)
  16879. (org-completion-use-ido org-completion-use-ido))
  16880. (unless (or org-completion-use-ido org-completion-use-iswitchb)
  16881. (setq org-completion-use-iswitchb t))
  16882. (org-pop-to-buffer-same-window
  16883. (org-icompleting-read "Org buffer: "
  16884. (mapcar 'list (mapcar 'buffer-name blist))
  16885. nil t))))
  16886. ;;; Define some older names previously used for this functionality
  16887. ;;;###autoload
  16888. (defalias 'org-ido-switchb 'org-switchb)
  16889. ;;;###autoload
  16890. (defalias 'org-iswitchb 'org-switchb)
  16891. (defun org-buffer-list (&optional predicate exclude-tmp)
  16892. "Return a list of Org buffers.
  16893. PREDICATE can be `export', `files' or `agenda'.
  16894. export restrict the list to Export buffers.
  16895. files restrict the list to buffers visiting Org files.
  16896. agenda restrict the list to buffers visiting agenda files.
  16897. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  16898. (let* ((bfn nil)
  16899. (agenda-files (and (eq predicate 'agenda)
  16900. (mapcar 'file-truename (org-agenda-files t))))
  16901. (filter
  16902. (cond
  16903. ((eq predicate 'files)
  16904. (lambda (b) (with-current-buffer b (derived-mode-p 'org-mode))))
  16905. ((eq predicate 'export)
  16906. (lambda (b) (string-match "\\*Org .*Export" (buffer-name b))))
  16907. ((eq predicate 'agenda)
  16908. (lambda (b)
  16909. (with-current-buffer b
  16910. (and (derived-mode-p 'org-mode)
  16911. (setq bfn (buffer-file-name b))
  16912. (member (file-truename bfn) agenda-files)))))
  16913. (t (lambda (b) (with-current-buffer b
  16914. (or (derived-mode-p 'org-mode)
  16915. (string-match "\\*Org .*Export"
  16916. (buffer-name b)))))))))
  16917. (delq nil
  16918. (mapcar
  16919. (lambda(b)
  16920. (if (and (funcall filter b)
  16921. (or (not exclude-tmp)
  16922. (not (string-match "tmp" (buffer-name b)))))
  16923. b
  16924. nil))
  16925. (buffer-list)))))
  16926. (defun org-agenda-files (&optional unrestricted archives)
  16927. "Get the list of agenda files.
  16928. Optional UNRESTRICTED means return the full list even if a restriction
  16929. is currently in place.
  16930. When ARCHIVES is t, include all archive files that are really being
  16931. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  16932. `org-agenda-archives-mode' is t."
  16933. (let ((files
  16934. (cond
  16935. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  16936. ((stringp org-agenda-files) (org-read-agenda-file-list))
  16937. ((listp org-agenda-files) org-agenda-files)
  16938. (t (error "Invalid value of `org-agenda-files'")))))
  16939. (setq files (apply 'append
  16940. (mapcar (lambda (f)
  16941. (if (file-directory-p f)
  16942. (directory-files
  16943. f t org-agenda-file-regexp)
  16944. (list f)))
  16945. files)))
  16946. (when org-agenda-skip-unavailable-files
  16947. (setq files (delq nil
  16948. (mapcar (function
  16949. (lambda (file)
  16950. (and (file-readable-p file) file)))
  16951. files))))
  16952. (when (or (eq archives t)
  16953. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  16954. (setq files (org-add-archive-files files)))
  16955. files))
  16956. (defun org-agenda-file-p (&optional file)
  16957. "Return non-nil, if FILE is an agenda file.
  16958. If FILE is omitted, use the file associated with the current
  16959. buffer."
  16960. (let ((fname (or file (buffer-file-name))))
  16961. (and fname
  16962. (member (file-truename fname)
  16963. (mapcar #'file-truename (org-agenda-files t))))))
  16964. (defun org-edit-agenda-file-list ()
  16965. "Edit the list of agenda files.
  16966. Depending on setup, this either uses customize to edit the variable
  16967. `org-agenda-files', or it visits the file that is holding the list. In the
  16968. latter case, the buffer is set up in a way that saving it automatically kills
  16969. the buffer and restores the previous window configuration."
  16970. (interactive)
  16971. (if (stringp org-agenda-files)
  16972. (let ((cw (current-window-configuration)))
  16973. (find-file org-agenda-files)
  16974. (setq-local org-window-configuration cw)
  16975. (org-add-hook 'after-save-hook
  16976. (lambda ()
  16977. (set-window-configuration
  16978. (prog1 org-window-configuration
  16979. (kill-buffer (current-buffer))))
  16980. (org-install-agenda-files-menu)
  16981. (message "New agenda file list installed"))
  16982. nil 'local)
  16983. (message "%s" (substitute-command-keys
  16984. "Edit list and finish with \\[save-buffer]")))
  16985. (customize-variable 'org-agenda-files)))
  16986. (defun org-store-new-agenda-file-list (list)
  16987. "Set new value for the agenda file list and save it correctly."
  16988. (if (stringp org-agenda-files)
  16989. (let ((fe (org-read-agenda-file-list t)) b u)
  16990. (while (setq b (find-buffer-visiting org-agenda-files))
  16991. (kill-buffer b))
  16992. (with-temp-file org-agenda-files
  16993. (insert
  16994. (mapconcat
  16995. (lambda (f) ;; Keep un-expanded entries.
  16996. (if (setq u (assoc f fe))
  16997. (cdr u)
  16998. f))
  16999. list "\n")
  17000. "\n")))
  17001. (let ((org-mode-hook nil) (org-inhibit-startup t)
  17002. (org-insert-mode-line-in-empty-file nil))
  17003. (setq org-agenda-files list)
  17004. (customize-save-variable 'org-agenda-files org-agenda-files))))
  17005. (defun org-read-agenda-file-list (&optional pair-with-expansion)
  17006. "Read the list of agenda files from a file.
  17007. If PAIR-WITH-EXPANSION is t return pairs with un-expanded
  17008. filenames, used by `org-store-new-agenda-file-list' to write back
  17009. un-expanded file names."
  17010. (when (file-directory-p org-agenda-files)
  17011. (error "`org-agenda-files' cannot be a single directory"))
  17012. (when (stringp org-agenda-files)
  17013. (with-temp-buffer
  17014. (insert-file-contents org-agenda-files)
  17015. (mapcar
  17016. (lambda (f)
  17017. (let ((e (expand-file-name (substitute-in-file-name f)
  17018. org-directory)))
  17019. (if pair-with-expansion
  17020. (cons e f)
  17021. e)))
  17022. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*")))))
  17023. ;;;###autoload
  17024. (defun org-cycle-agenda-files ()
  17025. "Cycle through the files in `org-agenda-files'.
  17026. If the current buffer visits an agenda file, find the next one in the list.
  17027. If the current buffer does not, find the first agenda file."
  17028. (interactive)
  17029. (let* ((fs (or (org-agenda-files t)
  17030. (user-error "No agenda files")))
  17031. (files (copy-sequence fs))
  17032. (tcf (and buffer-file-name (file-truename buffer-file-name)))
  17033. file)
  17034. (when tcf
  17035. (while (and (setq file (pop files))
  17036. (not (equal (file-truename file) tcf)))))
  17037. (find-file (car (or files fs)))
  17038. (when (buffer-base-buffer) (org-pop-to-buffer-same-window (buffer-base-buffer)))))
  17039. (defun org-agenda-file-to-front (&optional to-end)
  17040. "Move/add the current file to the top of the agenda file list.
  17041. If the file is not present in the list, it is added to the front. If it is
  17042. present, it is moved there. With optional argument TO-END, add/move to the
  17043. end of the list."
  17044. (interactive "P")
  17045. (let ((org-agenda-skip-unavailable-files nil)
  17046. (file-alist (mapcar (lambda (x)
  17047. (cons (file-truename x) x))
  17048. (org-agenda-files t)))
  17049. (ctf (file-truename
  17050. (or buffer-file-name
  17051. (user-error "Please save the current buffer to a file"))))
  17052. x had)
  17053. (setq x (assoc ctf file-alist) had x)
  17054. (unless x (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  17055. (if to-end
  17056. (setq file-alist (append (delq x file-alist) (list x)))
  17057. (setq file-alist (cons x (delq x file-alist))))
  17058. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  17059. (org-install-agenda-files-menu)
  17060. (message "File %s to %s of agenda file list"
  17061. (if had "moved" "added") (if to-end "end" "front"))))
  17062. (defun org-remove-file (&optional file)
  17063. "Remove current file from the list of files in variable `org-agenda-files'.
  17064. These are the files which are being checked for agenda entries.
  17065. Optional argument FILE means use this file instead of the current."
  17066. (interactive)
  17067. (let* ((org-agenda-skip-unavailable-files nil)
  17068. (file (or file buffer-file-name
  17069. (user-error "Current buffer does not visit a file")))
  17070. (true-file (file-truename file))
  17071. (afile (abbreviate-file-name file))
  17072. (files (delq nil (mapcar
  17073. (lambda (x)
  17074. (unless (equal true-file
  17075. (file-truename x))
  17076. x))
  17077. (org-agenda-files t)))))
  17078. (if (not (= (length files) (length (org-agenda-files t))))
  17079. (progn
  17080. (org-store-new-agenda-file-list files)
  17081. (org-install-agenda-files-menu)
  17082. (message "Removed from Org Agenda list: %s" afile))
  17083. (message "File was not in list: %s (not removed)" afile))))
  17084. (defun org-file-menu-entry (file)
  17085. (vector file (list 'find-file file) t))
  17086. (defun org-check-agenda-file (file)
  17087. "Make sure FILE exists. If not, ask user what to do."
  17088. (unless (file-exists-p file)
  17089. (message "Non-existent agenda file %s. [R]emove from list or [A]bort?"
  17090. (abbreviate-file-name file))
  17091. (let ((r (downcase (read-char-exclusive))))
  17092. (cond
  17093. ((equal r ?r)
  17094. (org-remove-file file)
  17095. (throw 'nextfile t))
  17096. (t (user-error "Abort"))))))
  17097. (defun org-get-agenda-file-buffer (file)
  17098. "Get an agenda buffer visiting FILE.
  17099. If the buffer needs to be created, add it to the list of buffers
  17100. which might be released later."
  17101. (let ((buf (org-find-base-buffer-visiting file)))
  17102. (if buf
  17103. buf ; just return it
  17104. ;; Make a new buffer and remember it
  17105. (setq buf (find-file-noselect file))
  17106. (when buf (push buf org-agenda-new-buffers))
  17107. buf)))
  17108. (defun org-release-buffers (blist)
  17109. "Release all buffers in list, asking the user for confirmation when needed.
  17110. When a buffer is unmodified, it is just killed. When modified, it is saved
  17111. \(if the user agrees) and then killed."
  17112. (let (file)
  17113. (dolist (buf blist)
  17114. (setq file (buffer-file-name buf))
  17115. (when (and (buffer-modified-p buf)
  17116. file
  17117. (y-or-n-p (format "Save file %s? " file)))
  17118. (with-current-buffer buf (save-buffer)))
  17119. (kill-buffer buf))))
  17120. (defun org-agenda-prepare-buffers (files)
  17121. "Create buffers for all agenda files, protect archived trees and comments."
  17122. (interactive)
  17123. (let ((pa '(:org-archived t))
  17124. (pc '(:org-comment t))
  17125. (pall '(:org-archived t :org-comment t))
  17126. (inhibit-read-only t)
  17127. (org-inhibit-startup org-agenda-inhibit-startup)
  17128. (rea (concat ":" org-archive-tag ":"))
  17129. file re pos)
  17130. (setq org-tag-alist-for-agenda nil
  17131. org-tag-groups-alist-for-agenda nil)
  17132. (save-excursion
  17133. (save-restriction
  17134. (dolist (file files)
  17135. (catch 'nextfile
  17136. (if (bufferp file)
  17137. (set-buffer file)
  17138. (org-check-agenda-file file)
  17139. (set-buffer (org-get-agenda-file-buffer file)))
  17140. (widen)
  17141. (org-set-regexps-and-options 'tags-only)
  17142. (setq pos (point))
  17143. (or (memq 'category org-agenda-ignore-properties)
  17144. (org-refresh-category-properties))
  17145. (or (memq 'stats org-agenda-ignore-properties)
  17146. (org-refresh-stats-properties))
  17147. (or (memq 'effort org-agenda-ignore-properties)
  17148. (org-refresh-effort-properties))
  17149. (or (memq 'appt org-agenda-ignore-properties)
  17150. (org-refresh-properties "APPT_WARNTIME" 'org-appt-warntime))
  17151. (setq org-todo-keywords-for-agenda
  17152. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  17153. (setq org-done-keywords-for-agenda
  17154. (append org-done-keywords-for-agenda org-done-keywords))
  17155. (setq org-todo-keyword-alist-for-agenda
  17156. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  17157. (setq org-tag-alist-for-agenda
  17158. (org-uniquify
  17159. (append org-tag-alist-for-agenda
  17160. org-tag-alist
  17161. org-tag-persistent-alist)))
  17162. (when org-group-tags
  17163. (setq org-tag-groups-alist-for-agenda
  17164. (org-uniquify-alist
  17165. (append org-tag-groups-alist-for-agenda org-tag-groups-alist))))
  17166. (org-with-silent-modifications
  17167. (save-excursion
  17168. (remove-text-properties (point-min) (point-max) pall)
  17169. (when org-agenda-skip-archived-trees
  17170. (goto-char (point-min))
  17171. (while (re-search-forward rea nil t)
  17172. (when (org-at-heading-p t)
  17173. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  17174. (goto-char (point-min))
  17175. (setq re (format "^\\*+ .*\\<%s\\>" org-comment-string))
  17176. (while (re-search-forward re nil t)
  17177. (when (save-match-data (org-in-commented-heading-p t))
  17178. (add-text-properties
  17179. (match-beginning 0) (org-end-of-subtree t) pc)))))
  17180. (goto-char pos)))))
  17181. (setq org-todo-keywords-for-agenda
  17182. (org-uniquify org-todo-keywords-for-agenda))
  17183. (setq org-todo-keyword-alist-for-agenda
  17184. (org-uniquify org-todo-keyword-alist-for-agenda))))
  17185. ;;;; CDLaTeX minor mode
  17186. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  17187. "Keymap for the minor `org-cdlatex-mode'.")
  17188. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  17189. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  17190. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  17191. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  17192. (org-defkey org-cdlatex-mode-map "\C-c{" 'org-cdlatex-environment-indent)
  17193. (defvar org-cdlatex-texmathp-advice-is-done nil
  17194. "Flag remembering if we have applied the advice to texmathp already.")
  17195. (define-minor-mode org-cdlatex-mode
  17196. "Toggle the minor `org-cdlatex-mode'.
  17197. This mode supports entering LaTeX environment and math in LaTeX fragments
  17198. in Org-mode.
  17199. \\{org-cdlatex-mode-map}"
  17200. nil " OCDL" nil
  17201. (when org-cdlatex-mode
  17202. (require 'cdlatex)
  17203. (run-hooks 'cdlatex-mode-hook)
  17204. (cdlatex-compute-tables))
  17205. (unless org-cdlatex-texmathp-advice-is-done
  17206. (setq org-cdlatex-texmathp-advice-is-done t)
  17207. (defadvice texmathp (around org-math-always-on activate)
  17208. "Always return t in org-mode buffers.
  17209. This is because we want to insert math symbols without dollars even outside
  17210. the LaTeX math segments. If Orgmode thinks that point is actually inside
  17211. an embedded LaTeX fragment, let texmathp do its job.
  17212. \\[org-cdlatex-mode-map]"
  17213. (interactive)
  17214. (let (p)
  17215. (cond
  17216. ((not (derived-mode-p 'org-mode)) ad-do-it)
  17217. ((eq this-command 'cdlatex-math-symbol)
  17218. (setq ad-return-value t
  17219. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  17220. (t
  17221. (let ((p (org-inside-LaTeX-fragment-p)))
  17222. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  17223. (setq ad-return-value t
  17224. texmathp-why '("Org-mode embedded math" . 0))
  17225. (when p ad-do-it)))))))))
  17226. (defun turn-on-org-cdlatex ()
  17227. "Unconditionally turn on `org-cdlatex-mode'."
  17228. (org-cdlatex-mode 1))
  17229. (defun org-try-cdlatex-tab ()
  17230. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  17231. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  17232. - inside a LaTeX fragment, or
  17233. - after the first word in a line, where an abbreviation expansion could
  17234. insert a LaTeX environment."
  17235. (when org-cdlatex-mode
  17236. (cond
  17237. ;; Before any word on the line: No expansion possible.
  17238. ((save-excursion (skip-chars-backward " \t") (bolp)) nil)
  17239. ;; Just after first word on the line: Expand it. Make sure it
  17240. ;; cannot happen on headlines, though.
  17241. ((save-excursion
  17242. (skip-chars-backward "a-zA-Z0-9*")
  17243. (skip-chars-backward " \t")
  17244. (and (bolp) (not (org-at-heading-p))))
  17245. (cdlatex-tab) t)
  17246. ((org-inside-LaTeX-fragment-p) (cdlatex-tab) t))))
  17247. (defun org-cdlatex-underscore-caret (&optional arg)
  17248. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  17249. Revert to the normal definition outside of these fragments."
  17250. (interactive "P")
  17251. (if (org-inside-LaTeX-fragment-p)
  17252. (call-interactively 'cdlatex-sub-superscript)
  17253. (let (org-cdlatex-mode)
  17254. (call-interactively (key-binding (vector last-input-event))))))
  17255. (defun org-cdlatex-math-modify (&optional arg)
  17256. "Execute `cdlatex-math-modify' in LaTeX fragments.
  17257. Revert to the normal definition outside of these fragments."
  17258. (interactive "P")
  17259. (if (org-inside-LaTeX-fragment-p)
  17260. (call-interactively 'cdlatex-math-modify)
  17261. (let (org-cdlatex-mode)
  17262. (call-interactively (key-binding (vector last-input-event))))))
  17263. (defun org-cdlatex-environment-indent (&optional environment item)
  17264. "Execute `cdlatex-environment' and indent the inserted environment.
  17265. ENVIRONMENT and ITEM are passed to `cdlatex-environment'.
  17266. The inserted environment is indented to current indentation
  17267. unless point is at the beginning of the line, in which the
  17268. environment remains unintended."
  17269. (interactive)
  17270. ;; cdlatex-environment always return nil. Therefore, capture output
  17271. ;; first and determine if an environment was selected.
  17272. (let* ((beg (point-marker))
  17273. (end (copy-marker (point) t))
  17274. (inserted (progn
  17275. (ignore-errors (cdlatex-environment environment item))
  17276. (< beg end)))
  17277. ;; Figure out how many lines to move forward after the
  17278. ;; environment has been inserted.
  17279. (lines (when inserted
  17280. (save-excursion
  17281. (- (loop while (< beg (point))
  17282. with x = 0
  17283. do (forward-line -1)
  17284. (incf x)
  17285. finally return x)
  17286. (if (progn (goto-char beg)
  17287. (and (progn (skip-chars-forward " \t") (eolp))
  17288. (progn (skip-chars-backward " \t") (bolp))))
  17289. 1 0)))))
  17290. (env (org-trim (delete-and-extract-region beg end))))
  17291. (when inserted
  17292. ;; Get indentation of next line unless at column 0.
  17293. (let ((ind (if (bolp) 0
  17294. (save-excursion
  17295. (org-return-indent)
  17296. (prog1 (org-get-indentation)
  17297. (when (progn (skip-chars-forward " \t") (eolp))
  17298. (delete-region beg (point)))))))
  17299. (bol (progn (skip-chars-backward " \t") (bolp))))
  17300. ;; Insert a newline before environment unless at column zero
  17301. ;; to "escape" the current line. Insert a newline if
  17302. ;; something is one the same line as \end{ENVIRONMENT}.
  17303. (insert
  17304. (concat (unless bol "\n") env
  17305. (when (and (skip-chars-forward " \t") (not (eolp))) "\n")))
  17306. (unless (zerop ind)
  17307. (save-excursion
  17308. (goto-char beg)
  17309. (while (< (point) end)
  17310. (unless (eolp) (org-indent-line-to ind))
  17311. (forward-line))))
  17312. (goto-char beg)
  17313. (forward-line lines)
  17314. (org-indent-line-to ind)))
  17315. (set-marker beg nil)
  17316. (set-marker end nil)))
  17317. ;;;; LaTeX fragments
  17318. (defun org-inside-LaTeX-fragment-p ()
  17319. "Test if point is inside a LaTeX fragment.
  17320. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  17321. sequence appearing also before point.
  17322. Even though the matchers for math are configurable, this function assumes
  17323. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  17324. delimiters are skipped when they have been removed by customization.
  17325. The return value is nil, or a cons cell with the delimiter and the
  17326. position of this delimiter.
  17327. This function does a reasonably good job, but can locally be fooled by
  17328. for example currency specifications. For example it will assume being in
  17329. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  17330. fragments that are properly closed, but during editing, we have to live
  17331. with the uncertainty caused by missing closing delimiters. This function
  17332. looks only before point, not after."
  17333. (catch 'exit
  17334. (let ((pos (point))
  17335. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  17336. (lim (progn
  17337. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  17338. (point)))
  17339. dd-on str (start 0) m re)
  17340. (goto-char pos)
  17341. (when dodollar
  17342. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  17343. re (nth 1 (assoc "$" org-latex-regexps)))
  17344. (while (string-match re str start)
  17345. (cond
  17346. ((= (match-end 0) (length str))
  17347. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  17348. ((= (match-end 0) (- (length str) 5))
  17349. (throw 'exit nil))
  17350. (t (setq start (match-end 0))))))
  17351. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  17352. (goto-char pos)
  17353. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  17354. (and (match-beginning 2) (throw 'exit nil))
  17355. ;; count $$
  17356. (while (re-search-backward "\\$\\$" lim t)
  17357. (setq dd-on (not dd-on)))
  17358. (goto-char pos)
  17359. (when dd-on (cons "$$" m))))))
  17360. (defun org-inside-latex-macro-p ()
  17361. "Is point inside a LaTeX macro or its arguments?"
  17362. (save-match-data
  17363. (org-in-regexp
  17364. "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")))
  17365. (defvar-local org-latex-fragment-image-overlays nil
  17366. "List of overlays carrying the images of latex fragments.")
  17367. (defun org-remove-latex-fragment-image-overlays (&optional beg end)
  17368. "Remove all overlays with LaTeX fragment images in current buffer.
  17369. When optional arguments BEG and END are non-nil, remove all
  17370. overlays between them instead. Return t when some overlays were
  17371. removed, nil otherwise."
  17372. (let (removedp)
  17373. (setq org-latex-fragment-image-overlays
  17374. (let ((beg (or beg (point-min)))
  17375. (end (or end (point-max))))
  17376. (cl-remove-if
  17377. (lambda (o)
  17378. (cond ((not (overlay-buffer o)) (delete-overlay o) t)
  17379. ((and (>= (overlay-start o) beg)
  17380. (<= (overlay-end o) end))
  17381. (delete-overlay o)
  17382. (unless removedp (setq removedp t)))
  17383. (t nil)))
  17384. org-latex-fragment-image-overlays)))
  17385. removedp))
  17386. (define-obsolete-function-alias
  17387. 'org-preview-latex-fragment 'org-toggle-latex-fragment "24.4")
  17388. (defun org-toggle-latex-fragment (&optional arg)
  17389. "Preview the LaTeX fragment at point, or all locally or globally.
  17390. If the cursor is on a LaTeX fragment, create the image and overlay
  17391. it over the source code, if there is none. Remove it otherwise.
  17392. If there is no fragment at point, display all fragments in the
  17393. current section.
  17394. With prefix ARG, preview or clear image for all fragments in the
  17395. current subtree or in the whole buffer when used before the first
  17396. headline. With a double prefix ARG \\[universal-argument] \
  17397. \\[universal-argument] preview or clear images
  17398. for all fragments in the buffer."
  17399. (interactive "P")
  17400. (unless (buffer-file-name (buffer-base-buffer))
  17401. (user-error "Can't preview LaTeX fragment in a non-file buffer"))
  17402. (when (display-graphic-p)
  17403. (catch 'exit
  17404. (save-excursion
  17405. (let ((window-start (window-start)) msg)
  17406. (save-restriction
  17407. (cond
  17408. ((or (equal arg '(16))
  17409. (and (equal arg '(4))
  17410. (org-with-limited-levels (org-before-first-heading-p))))
  17411. (if (org-remove-latex-fragment-image-overlays)
  17412. (progn (message "LaTeX fragments images removed from buffer")
  17413. (throw 'exit nil))
  17414. (setq msg "Creating images for buffer...")))
  17415. ((equal arg '(4))
  17416. (org-with-limited-levels (org-back-to-heading t))
  17417. (let ((beg (point))
  17418. (end (progn (org-end-of-subtree t) (point))))
  17419. (if (org-remove-latex-fragment-image-overlays beg end)
  17420. (progn
  17421. (message "LaTeX fragment images removed from subtree")
  17422. (throw 'exit nil))
  17423. (setq msg "Creating images for subtree...")
  17424. (narrow-to-region beg end))))
  17425. ((let ((datum (org-element-context)))
  17426. (when (memq (org-element-type datum)
  17427. '(latex-environment latex-fragment))
  17428. (let* ((beg (org-element-property :begin datum))
  17429. (end (org-element-property :end datum)))
  17430. (if (org-remove-latex-fragment-image-overlays beg end)
  17431. (progn (message "LaTeX fragment image removed")
  17432. (throw 'exit nil))
  17433. (narrow-to-region beg end)
  17434. (setq msg "Creating image..."))))))
  17435. (t
  17436. (org-with-limited-levels
  17437. (let ((beg (if (org-at-heading-p) (line-beginning-position)
  17438. (outline-previous-heading)
  17439. (point)))
  17440. (end (progn (outline-next-heading) (point))))
  17441. (if (org-remove-latex-fragment-image-overlays beg end)
  17442. (progn
  17443. (message "LaTeX fragment images removed from section")
  17444. (throw 'exit nil))
  17445. (setq msg "Creating images for section...")
  17446. (narrow-to-region beg end))))))
  17447. (let ((file (buffer-file-name (buffer-base-buffer))))
  17448. (org-format-latex
  17449. (concat org-latex-preview-ltxpng-directory
  17450. (file-name-sans-extension (file-name-nondirectory file)))
  17451. ;; Emacs cannot overlay images from remote hosts.
  17452. ;; Create it in `temporary-file-directory' instead.
  17453. (if (file-remote-p file) temporary-file-directory
  17454. default-directory)
  17455. 'overlays msg 'forbuffer
  17456. org-latex-create-formula-image-program)))
  17457. ;; Work around a bug that doesn't restore window's start
  17458. ;; when widening back the buffer.
  17459. (set-window-start nil window-start)
  17460. (message (concat msg "done")))))))
  17461. (defun org-format-latex
  17462. (prefix &optional dir overlays msg forbuffer processing-type)
  17463. "Replace LaTeX fragments with links to an image, and produce images.
  17464. When optional argument OVERLAYS is non-nil, display the image on
  17465. top of the fragment instead of replacing it.
  17466. PROCESSING-TYPE is the conversion method to use, as a symbol.
  17467. Some of the options can be changed using the variable
  17468. `org-format-latex-options', which see."
  17469. (when (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  17470. (unless (eq processing-type 'verbatim)
  17471. (let* ((math-regexp "\\$\\|\\\\[([]\\|^[ \t]*\\\\begin{[A-Za-z0-9*]+}")
  17472. (cnt 0)
  17473. checkdir-flag)
  17474. (goto-char (point-min))
  17475. ;; Optimize overlay creation: (info "(elisp) Managing Overlays").
  17476. (when (and overlays (memq processing-type '(dvipng imagemagick)))
  17477. (overlay-recenter (point-max)))
  17478. (while (re-search-forward math-regexp nil t)
  17479. (unless (and overlays
  17480. (eq (get-char-property (point) 'org-overlay-type)
  17481. 'org-latex-overlay))
  17482. (let* ((context (org-element-context))
  17483. (type (org-element-type context)))
  17484. (when (memq type '(latex-environment latex-fragment))
  17485. (let ((block-type (eq type 'latex-environment))
  17486. (value (org-element-property :value context))
  17487. (beg (org-element-property :begin context))
  17488. (end (save-excursion
  17489. (goto-char (org-element-property :end context))
  17490. (skip-chars-backward " \r\t\n")
  17491. (point))))
  17492. (case processing-type
  17493. (mathjax
  17494. ;; Prepare for MathJax processing.
  17495. (if (eq (char-after beg) ?$)
  17496. (save-excursion
  17497. (delete-region beg end)
  17498. (insert "\\(" (substring value 1 -1) "\\)"))
  17499. (goto-char end)))
  17500. ((dvipng imagemagick)
  17501. ;; Process to an image.
  17502. (incf cnt)
  17503. (goto-char beg)
  17504. (let* ((face (face-at-point))
  17505. ;; Get the colors from the face at point.
  17506. (fg
  17507. (let ((color (plist-get org-format-latex-options
  17508. :foreground)))
  17509. (if (and forbuffer (eq color 'auto))
  17510. (face-attribute face :foreground nil 'default)
  17511. color)))
  17512. (bg
  17513. (let ((color (plist-get org-format-latex-options
  17514. :background)))
  17515. (if (and forbuffer (eq color 'auto))
  17516. (face-attribute face :background nil 'default)
  17517. color)))
  17518. (hash (sha1 (prin1-to-string
  17519. (list org-format-latex-header
  17520. org-latex-default-packages-alist
  17521. org-latex-packages-alist
  17522. org-format-latex-options
  17523. forbuffer value fg bg))))
  17524. (absprefix (expand-file-name prefix dir))
  17525. (linkfile (format "%s_%s.png" prefix hash))
  17526. (movefile (format "%s_%s.png" absprefix hash))
  17527. (sep (and block-type "\n\n"))
  17528. (link (concat sep "[[file:" linkfile "]]" sep))
  17529. (options
  17530. (org-combine-plists
  17531. org-format-latex-options
  17532. `(:foreground ,fg :background ,bg))))
  17533. (when msg (message msg cnt))
  17534. (unless checkdir-flag ; Ensure the directory exists.
  17535. (setq checkdir-flag t)
  17536. (let ((todir (file-name-directory absprefix)))
  17537. (unless (file-directory-p todir)
  17538. (make-directory todir t))))
  17539. (unless (file-exists-p movefile)
  17540. (org-create-formula-image
  17541. value movefile options forbuffer processing-type))
  17542. (if overlays
  17543. (progn
  17544. (dolist (o (overlays-in beg end))
  17545. (when (eq (overlay-get o 'org-overlay-type)
  17546. 'org-latex-overlay)
  17547. (delete-overlay o)))
  17548. (let ((ov (make-overlay beg end)))
  17549. (overlay-put ov
  17550. 'org-overlay-type
  17551. 'org-latex-overlay)
  17552. (overlay-put ov 'evaporate t)
  17553. (if (featurep 'xemacs)
  17554. (progn
  17555. (overlay-put ov 'invisible t)
  17556. (overlay-put
  17557. ov 'end-glyph
  17558. (make-glyph
  17559. (vector 'png :file movefile))))
  17560. (overlay-put
  17561. ov 'display
  17562. (list 'image
  17563. :type 'png
  17564. :file movefile
  17565. :ascent 'center)))
  17566. (push ov org-latex-fragment-image-overlays))
  17567. (goto-char end))
  17568. (delete-region beg end)
  17569. (insert
  17570. (org-add-props link
  17571. (list 'org-latex-src
  17572. (replace-regexp-in-string "\"" "" value)
  17573. 'org-latex-src-embed-type
  17574. (if block-type 'paragraph 'character)))))))
  17575. (mathml
  17576. ;; Process to MathML.
  17577. (unless (org-format-latex-mathml-available-p)
  17578. (user-error "LaTeX to MathML converter not configured"))
  17579. (incf cnt)
  17580. (when msg (message msg cnt))
  17581. (goto-char beg)
  17582. (delete-region beg end)
  17583. (insert (org-format-latex-as-mathml
  17584. value block-type prefix dir)))
  17585. (otherwise
  17586. (error "Unknown conversion type %s for LaTeX fragments"
  17587. processing-type)))))))))))
  17588. (defun org-create-math-formula (latex-frag &optional mathml-file)
  17589. "Convert LATEX-FRAG to MathML and store it in MATHML-FILE.
  17590. Use `org-latex-to-mathml-convert-command'. If the conversion is
  17591. sucessful, return the portion between \"<math...> </math>\"
  17592. elements otherwise return nil. When MATHML-FILE is specified,
  17593. write the results in to that file. When invoked as an
  17594. interactive command, prompt for LATEX-FRAG, with initial value
  17595. set to the current active region and echo the results for user
  17596. inspection."
  17597. (interactive (list (let ((frag (when (org-region-active-p)
  17598. (buffer-substring-no-properties
  17599. (region-beginning) (region-end)))))
  17600. (read-string "LaTeX Fragment: " frag nil frag))))
  17601. (unless latex-frag (user-error "Invalid LaTeX fragment"))
  17602. (let* ((tmp-in-file (file-relative-name
  17603. (make-temp-name (expand-file-name "ltxmathml-in"))))
  17604. (ignore (write-region latex-frag nil tmp-in-file))
  17605. (tmp-out-file (file-relative-name
  17606. (make-temp-name (expand-file-name "ltxmathml-out"))))
  17607. (cmd (format-spec
  17608. org-latex-to-mathml-convert-command
  17609. `((?j . ,(and org-latex-to-mathml-jar-file
  17610. (shell-quote-argument
  17611. (expand-file-name
  17612. org-latex-to-mathml-jar-file))))
  17613. (?I . ,(shell-quote-argument tmp-in-file))
  17614. (?i . ,latex-frag)
  17615. (?o . ,(shell-quote-argument tmp-out-file)))))
  17616. mathml shell-command-output)
  17617. (when (org-called-interactively-p 'any)
  17618. (unless (org-format-latex-mathml-available-p)
  17619. (user-error "LaTeX to MathML converter not configured")))
  17620. (message "Running %s" cmd)
  17621. (setq shell-command-output (shell-command-to-string cmd))
  17622. (setq mathml
  17623. (when (file-readable-p tmp-out-file)
  17624. (with-current-buffer (find-file-noselect tmp-out-file t)
  17625. (goto-char (point-min))
  17626. (when (re-search-forward
  17627. (concat
  17628. (regexp-quote
  17629. "<math xmlns=\"http://www.w3.org/1998/Math/MathML\"")
  17630. "[^>]*?>"
  17631. "\\(.\\|\n\\)*"
  17632. "</math>")
  17633. nil t)
  17634. (prog1 (match-string 0) (kill-buffer))))))
  17635. (cond
  17636. (mathml
  17637. (setq mathml
  17638. (concat "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" mathml))
  17639. (when mathml-file
  17640. (write-region mathml nil mathml-file))
  17641. (when (org-called-interactively-p 'any)
  17642. (message mathml)))
  17643. ((message "LaTeX to MathML conversion failed")
  17644. (message shell-command-output)))
  17645. (delete-file tmp-in-file)
  17646. (when (file-exists-p tmp-out-file)
  17647. (delete-file tmp-out-file))
  17648. mathml))
  17649. (defun org-format-latex-as-mathml (latex-frag latex-frag-type
  17650. prefix &optional dir)
  17651. "Use `org-create-math-formula' but check local cache first."
  17652. (let* ((absprefix (expand-file-name prefix dir))
  17653. (print-length nil) (print-level nil)
  17654. (formula-id (concat
  17655. "formula-"
  17656. (sha1
  17657. (prin1-to-string
  17658. (list latex-frag
  17659. org-latex-to-mathml-convert-command)))))
  17660. (formula-cache (format "%s-%s.mathml" absprefix formula-id))
  17661. (formula-cache-dir (file-name-directory formula-cache)))
  17662. (unless (file-directory-p formula-cache-dir)
  17663. (make-directory formula-cache-dir t))
  17664. (unless (file-exists-p formula-cache)
  17665. (org-create-math-formula latex-frag formula-cache))
  17666. (if (file-exists-p formula-cache)
  17667. ;; Successful conversion. Return the link to MathML file.
  17668. (org-add-props
  17669. (format "[[file:%s]]" (file-relative-name formula-cache dir))
  17670. (list 'org-latex-src (replace-regexp-in-string "\"" "" latex-frag)
  17671. 'org-latex-src-embed-type (if latex-frag-type
  17672. 'paragraph 'character)))
  17673. ;; Failed conversion. Return the LaTeX fragment verbatim
  17674. latex-frag)))
  17675. (defun org-create-formula-image (string tofile options buffer &optional type)
  17676. "Create an image from LaTeX source using dvipng or convert.
  17677. This function calls either `org-create-formula-image-with-dvipng'
  17678. or `org-create-formula-image-with-imagemagick' depending on the
  17679. value of `org-latex-create-formula-image-program' or on the value
  17680. of the optional TYPE variable.
  17681. Note: ultimately these two function should be combined as they
  17682. share a good deal of logic."
  17683. (org-check-external-command
  17684. "latex" "needed to convert LaTeX fragments to images")
  17685. (funcall
  17686. (case (or type org-latex-create-formula-image-program)
  17687. (dvipng
  17688. (org-check-external-command
  17689. "dvipng" "needed to convert LaTeX fragments to images")
  17690. #'org-create-formula-image-with-dvipng)
  17691. (imagemagick
  17692. (org-check-external-command
  17693. "convert" "you need to install imagemagick")
  17694. #'org-create-formula-image-with-imagemagick)
  17695. (t (error
  17696. "Invalid value of `org-latex-create-formula-image-program'")))
  17697. string tofile options buffer))
  17698. (declare-function org-export-get-backend "ox" (name))
  17699. (declare-function org-export--get-global-options "ox" (&optional backend))
  17700. (declare-function org-export--get-inbuffer-options "ox" (&optional backend))
  17701. (declare-function org-latex-guess-inputenc "ox-latex" (header))
  17702. (declare-function org-latex-guess-babel-language "ox-latex" (header info))
  17703. (defun org-create-formula--latex-header ()
  17704. "Return LaTeX header appropriate for previewing a LaTeX snippet."
  17705. (let ((info (org-combine-plists (org-export--get-global-options
  17706. (org-export-get-backend 'latex))
  17707. (org-export--get-inbuffer-options
  17708. (org-export-get-backend 'latex)))))
  17709. (org-latex-guess-babel-language
  17710. (org-latex-guess-inputenc
  17711. (org-splice-latex-header
  17712. org-format-latex-header
  17713. org-latex-default-packages-alist
  17714. org-latex-packages-alist t
  17715. (plist-get info :latex-header)))
  17716. info)))
  17717. (defun org--get-display-dpi ()
  17718. "Get the DPI of the display.
  17719. Assumes that the display has the same pixel width in the
  17720. horizontal and vertical directions."
  17721. (if (display-graphic-p)
  17722. (round (/ (display-pixel-height)
  17723. (/ (display-mm-height) 25.4)))
  17724. (error "Attempt to calculate the dpi of a non-graphic display")))
  17725. ;; This function borrows from Ganesh Swami's latex2png.el
  17726. (defun org-create-formula-image-with-dvipng (string tofile options buffer)
  17727. "This calls dvipng."
  17728. (require 'ox-latex)
  17729. (let* ((tmpdir (if (featurep 'xemacs)
  17730. (temp-directory)
  17731. temporary-file-directory))
  17732. (texfilebase (make-temp-name
  17733. (expand-file-name "orgtex" tmpdir)))
  17734. (texfile (concat texfilebase ".tex"))
  17735. (dvifile (concat texfilebase ".dvi"))
  17736. (pngfile (concat texfilebase ".png"))
  17737. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  17738. ;; This assumes that the display has the same pixel width in
  17739. ;; the horizontal and vertical directions
  17740. (dpi (number-to-string (* scale (if buffer (org--get-display-dpi) 120))))
  17741. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  17742. "Black"))
  17743. (bg (or (plist-get options (if buffer :background :html-background))
  17744. "Transparent")))
  17745. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground))
  17746. (unless (string= fg "Transparent") (setq fg (org-dvipng-color-format fg))))
  17747. (if (eq bg 'default) (setq bg (org-dvipng-color :background))
  17748. (unless (string= bg "Transparent") (setq bg (org-dvipng-color-format bg))))
  17749. (let ((latex-header (org-create-formula--latex-header)))
  17750. (with-temp-file texfile
  17751. (insert latex-header)
  17752. (insert "\n\\begin{document}\n" string "\n\\end{document}\n")))
  17753. (let ((dir default-directory))
  17754. (ignore-errors
  17755. (cd tmpdir)
  17756. (call-process "latex" nil nil nil texfile))
  17757. (cd dir))
  17758. (if (not (file-exists-p dvifile))
  17759. (progn (message "Failed to create dvi file from %s" texfile) nil)
  17760. (ignore-errors
  17761. (if (featurep 'xemacs)
  17762. (call-process "dvipng" nil nil nil
  17763. "-fg" fg "-bg" bg
  17764. "-T" "tight"
  17765. "-o" pngfile
  17766. dvifile)
  17767. (call-process "dvipng" nil nil nil
  17768. "-fg" fg "-bg" bg
  17769. "-D" dpi
  17770. ;;"-x" scale "-y" scale
  17771. "-T" "tight"
  17772. "-o" pngfile
  17773. dvifile)))
  17774. (if (not (file-exists-p pngfile))
  17775. (if org-format-latex-signal-error
  17776. (error "Failed to create png file from %s" texfile)
  17777. (message "Failed to create png file from %s" texfile)
  17778. nil)
  17779. ;; Use the requested file name and clean up
  17780. (copy-file pngfile tofile 'replace)
  17781. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png" ".out") do
  17782. (when (file-exists-p (concat texfilebase e))
  17783. (delete-file (concat texfilebase e))))
  17784. pngfile))))
  17785. (declare-function org-latex-compile "ox-latex" (texfile &optional snippet))
  17786. (defun org-create-formula-image-with-imagemagick (string tofile options buffer)
  17787. "This calls convert, which is included into imagemagick."
  17788. (require 'ox-latex)
  17789. (let* ((tmpdir (if (featurep 'xemacs)
  17790. (temp-directory)
  17791. temporary-file-directory))
  17792. (texfilebase (make-temp-name
  17793. (expand-file-name "orgtex" tmpdir)))
  17794. (texfile (concat texfilebase ".tex"))
  17795. (pdffile (concat texfilebase ".pdf"))
  17796. (pngfile (concat texfilebase ".png"))
  17797. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  17798. (dpi (number-to-string (* scale (if buffer (org--get-display-dpi) 120))))
  17799. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  17800. "black"))
  17801. (bg (or (plist-get options (if buffer :background :html-background))
  17802. "white")))
  17803. (if (eq fg 'default) (setq fg (org-latex-color :foreground))
  17804. (setq fg (org-latex-color-format fg)))
  17805. (if (eq bg 'default) (setq bg (org-latex-color :background))
  17806. (setq bg (org-latex-color-format
  17807. (if (string= bg "Transparent") "white" bg))))
  17808. (let ((latex-header (org-create-formula--latex-header)))
  17809. (with-temp-file texfile
  17810. (insert latex-header)
  17811. (insert "\n\\begin{document}\n"
  17812. "\\definecolor{fg}{rgb}{" fg "}\n"
  17813. "\\definecolor{bg}{rgb}{" bg "}\n"
  17814. "\n\\pagecolor{bg}\n"
  17815. "\n{\\color{fg}\n"
  17816. string
  17817. "\n}\n"
  17818. "\n\\end{document}\n")))
  17819. (org-latex-compile texfile t)
  17820. (if (not (file-exists-p pdffile))
  17821. (progn (message "Failed to create pdf file from %s" texfile) nil)
  17822. (ignore-errors
  17823. (if (featurep 'xemacs)
  17824. (call-process "convert" nil nil nil
  17825. "-density" "96"
  17826. "-trim"
  17827. "-antialias"
  17828. pdffile
  17829. "-quality" "100"
  17830. ;; "-sharpen" "0x1.0"
  17831. pngfile)
  17832. (call-process "convert" nil nil nil
  17833. "-density" dpi
  17834. "-trim"
  17835. "-antialias"
  17836. pdffile
  17837. "-quality" "100"
  17838. ;; "-sharpen" "0x1.0"
  17839. pngfile)))
  17840. (if (not (file-exists-p pngfile))
  17841. (if org-format-latex-signal-error
  17842. (error "Failed to create png file from %s" texfile)
  17843. (message "Failed to create png file from %s" texfile)
  17844. nil)
  17845. ;; Use the requested file name and clean up
  17846. (copy-file pngfile tofile 'replace)
  17847. (loop for e in '(".pdf" ".tex" ".aux" ".log" ".png") do
  17848. (when (file-exists-p (concat texfilebase e))
  17849. (delete-file (concat texfilebase e))))
  17850. pngfile))))
  17851. (defun org-splice-latex-header (tpl def-pkg pkg snippets-p &optional extra)
  17852. "Fill a LaTeX header template TPL.
  17853. In the template, the following place holders will be recognized:
  17854. [DEFAULT-PACKAGES] \\usepackage statements for DEF-PKG
  17855. [NO-DEFAULT-PACKAGES] do not include DEF-PKG
  17856. [PACKAGES] \\usepackage statements for PKG
  17857. [NO-PACKAGES] do not include PKG
  17858. [EXTRA] the string EXTRA
  17859. [NO-EXTRA] do not include EXTRA
  17860. For backward compatibility, if both the positive and the negative place
  17861. holder is missing, the positive one (without the \"NO-\") will be
  17862. assumed to be present at the end of the template.
  17863. DEF-PKG and PKG are assumed to be alists of options/packagename lists.
  17864. EXTRA is a string.
  17865. SNIPPETS-P indicates if this is run to create snippet images for HTML."
  17866. (let (rpl (end ""))
  17867. (if (string-match "^[ \t]*\\[\\(NO-\\)?DEFAULT-PACKAGES\\][ \t]*\n?" tpl)
  17868. (setq rpl (if (or (match-end 1) (not def-pkg))
  17869. "" (org-latex-packages-to-string def-pkg snippets-p t))
  17870. tpl (replace-match rpl t t tpl))
  17871. (when def-pkg (setq end (org-latex-packages-to-string def-pkg snippets-p))))
  17872. (if (string-match "\\[\\(NO-\\)?PACKAGES\\][ \t]*\n?" tpl)
  17873. (setq rpl (if (or (match-end 1) (not pkg))
  17874. "" (org-latex-packages-to-string pkg snippets-p t))
  17875. tpl (replace-match rpl t t tpl))
  17876. (when pkg (setq end
  17877. (concat end "\n"
  17878. (org-latex-packages-to-string pkg snippets-p)))))
  17879. (if (string-match "\\[\\(NO-\\)?EXTRA\\][ \t]*\n?" tpl)
  17880. (setq rpl (if (or (match-end 1) (not extra))
  17881. "" (concat extra "\n"))
  17882. tpl (replace-match rpl t t tpl))
  17883. (when (and extra (string-match "\\S-" extra))
  17884. (setq end (concat end "\n" extra))))
  17885. (if (string-match "\\S-" end)
  17886. (concat tpl "\n" end)
  17887. tpl)))
  17888. (defun org-latex-packages-to-string (pkg &optional snippets-p newline)
  17889. "Turn an alist of packages into a string with the \\usepackage macros."
  17890. (setq pkg (mapconcat (lambda(p)
  17891. (cond
  17892. ((stringp p) p)
  17893. ((and snippets-p (>= (length p) 3) (not (nth 2 p)))
  17894. (format "%% Package %s omitted" (cadr p)))
  17895. ((equal "" (car p))
  17896. (format "\\usepackage{%s}" (cadr p)))
  17897. (t
  17898. (format "\\usepackage[%s]{%s}"
  17899. (car p) (cadr p)))))
  17900. pkg
  17901. "\n"))
  17902. (if newline (concat pkg "\n") pkg))
  17903. (defun org-dvipng-color (attr)
  17904. "Return a RGB color specification for dvipng."
  17905. (apply 'format "rgb %s %s %s"
  17906. (mapcar 'org-normalize-color
  17907. (if (featurep 'xemacs)
  17908. (color-rgb-components
  17909. (face-property 'default
  17910. (cond ((eq attr :foreground) 'foreground)
  17911. ((eq attr :background) 'background))))
  17912. (color-values (face-attribute 'default attr nil))))))
  17913. (defun org-dvipng-color-format (color-name)
  17914. "Convert COLOR-NAME to a RGB color value for dvipng."
  17915. (apply 'format "rgb %s %s %s"
  17916. (mapcar 'org-normalize-color
  17917. (color-values color-name))))
  17918. (defun org-latex-color (attr)
  17919. "Return a RGB color for the LaTeX color package."
  17920. (apply 'format "%s,%s,%s"
  17921. (mapcar 'org-normalize-color
  17922. (if (featurep 'xemacs)
  17923. (color-rgb-components
  17924. (face-property 'default
  17925. (cond ((eq attr :foreground) 'foreground)
  17926. ((eq attr :background) 'background))))
  17927. (color-values (face-attribute 'default attr nil))))))
  17928. (defun org-latex-color-format (color-name)
  17929. "Convert COLOR-NAME to a RGB color value."
  17930. (apply 'format "%s,%s,%s"
  17931. (mapcar 'org-normalize-color
  17932. (color-values color-name))))
  17933. (defun org-normalize-color (value)
  17934. "Return string to be used as color value for an RGB component."
  17935. (format "%g" (/ value 65535.0)))
  17936. ;; Image display
  17937. (defvar-local org-inline-image-overlays nil)
  17938. (defun org-toggle-inline-images (&optional include-linked)
  17939. "Toggle the display of inline images.
  17940. INCLUDE-LINKED is passed to `org-display-inline-images'."
  17941. (interactive "P")
  17942. (if org-inline-image-overlays
  17943. (progn
  17944. (org-remove-inline-images)
  17945. (when (org-called-interactively-p 'interactive)
  17946. (message "Inline image display turned off")))
  17947. (org-display-inline-images include-linked)
  17948. (when (org-called-interactively-p 'interactive)
  17949. (message (if org-inline-image-overlays
  17950. (format "%d images displayed inline"
  17951. (length org-inline-image-overlays))
  17952. "No images to display inline")))))
  17953. (defun org-redisplay-inline-images ()
  17954. "Refresh the display of inline images."
  17955. (interactive)
  17956. (if (not org-inline-image-overlays)
  17957. (org-toggle-inline-images)
  17958. (org-toggle-inline-images)
  17959. (org-toggle-inline-images)))
  17960. (defun org-display-inline-images (&optional include-linked refresh beg end)
  17961. "Display inline images.
  17962. An inline image is a link which follows either of these
  17963. conventions:
  17964. 1. Its path is a file with an extension matching return value
  17965. from `image-file-name-regexp' and it has no contents.
  17966. 2. Its description consists in a single link of the previous
  17967. type.
  17968. When optional argument INCLUDE-LINKED is non-nil, also links with
  17969. a text description part will be inlined. This can be nice for
  17970. a quick look at those images, but it does not reflect what
  17971. exported files will look like.
  17972. When optional argument REFRESH is non-nil, refresh existing
  17973. images between BEG and END. This will create new image displays
  17974. only if necessary. BEG and END default to the buffer
  17975. boundaries."
  17976. (interactive "P")
  17977. (when (display-graphic-p)
  17978. (unless refresh
  17979. (org-remove-inline-images)
  17980. (when (fboundp 'clear-image-cache) (clear-image-cache)))
  17981. (org-with-wide-buffer
  17982. (goto-char (or beg (point-min)))
  17983. (let ((case-fold-search t)
  17984. (file-extension-re (image-file-name-regexp)))
  17985. (while (re-search-forward "[][]\\[\\(?:file\\|[./~]\\)" end t)
  17986. (let ((link (save-match-data (org-element-context))))
  17987. ;; Check if we're at an inline image.
  17988. (when (and (equal (org-element-property :type link) "file")
  17989. (or include-linked
  17990. (not (org-element-property :contents-begin link)))
  17991. (let ((parent (org-element-property :parent link)))
  17992. (or (not (eq (org-element-type parent) 'link))
  17993. (not (cdr (org-element-contents parent)))))
  17994. (org-string-match-p file-extension-re
  17995. (org-element-property :path link)))
  17996. (let ((file (expand-file-name
  17997. (org-link-unescape
  17998. (org-element-property :path link)))))
  17999. (when (file-exists-p file)
  18000. (let ((width
  18001. ;; Apply `org-image-actual-width' specifications.
  18002. (cond
  18003. ((not (image-type-available-p 'imagemagick)) nil)
  18004. ((eq org-image-actual-width t) nil)
  18005. ((listp org-image-actual-width)
  18006. (or
  18007. ;; First try to find a width among
  18008. ;; attributes associated to the paragraph
  18009. ;; containing link.
  18010. (let ((paragraph
  18011. (let ((e link))
  18012. (while (and (setq e (org-element-property
  18013. :parent e))
  18014. (not (eq (org-element-type e)
  18015. 'paragraph))))
  18016. e)))
  18017. (when paragraph
  18018. (save-excursion
  18019. (goto-char (org-element-property :begin paragraph))
  18020. (when
  18021. (re-search-forward
  18022. "^[ \t]*#\\+attr_.*?: +.*?:width +\\(\\S-+\\)"
  18023. (org-element-property
  18024. :post-affiliated paragraph)
  18025. t)
  18026. (string-to-number (match-string 1))))))
  18027. ;; Otherwise, fall-back to provided number.
  18028. (car org-image-actual-width)))
  18029. ((numberp org-image-actual-width)
  18030. org-image-actual-width)))
  18031. (old (get-char-property-and-overlay
  18032. (org-element-property :begin link)
  18033. 'org-image-overlay)))
  18034. (if (and (car-safe old) refresh)
  18035. (image-refresh (overlay-get (cdr old) 'display))
  18036. (let ((image (create-image file
  18037. (and width 'imagemagick)
  18038. nil
  18039. :width width)))
  18040. (when image
  18041. (let* ((link
  18042. ;; If inline image is the description
  18043. ;; of another link, be sure to
  18044. ;; consider the latter as the one to
  18045. ;; apply the overlay on.
  18046. (let ((parent
  18047. (org-element-property :parent link)))
  18048. (if (eq (org-element-type parent) 'link)
  18049. parent
  18050. link)))
  18051. (ov (make-overlay
  18052. (org-element-property :begin link)
  18053. (progn
  18054. (goto-char
  18055. (org-element-property :end link))
  18056. (skip-chars-backward " \t")
  18057. (point)))))
  18058. (overlay-put ov 'display image)
  18059. (overlay-put ov 'face 'default)
  18060. (overlay-put ov 'org-image-overlay t)
  18061. (overlay-put
  18062. ov 'modification-hooks
  18063. (list 'org-display-inline-remove-overlay))
  18064. (push ov org-inline-image-overlays)))))))))))))))
  18065. (define-obsolete-function-alias
  18066. 'org-display-inline-modification-hook 'org-display-inline-remove-overlay "24.3")
  18067. (defun org-display-inline-remove-overlay (ov after beg end &optional len)
  18068. "Remove inline-display overlay if a corresponding region is modified."
  18069. (let ((inhibit-modification-hooks t))
  18070. (when (and ov after)
  18071. (delete ov org-inline-image-overlays)
  18072. (delete-overlay ov))))
  18073. (defun org-remove-inline-images ()
  18074. "Remove inline display of images."
  18075. (interactive)
  18076. (mapc 'delete-overlay org-inline-image-overlays)
  18077. (setq org-inline-image-overlays nil))
  18078. ;;;; Key bindings
  18079. ;; Outline functions from `outline-mode-prefix-map'
  18080. ;; that can be remapped in Org:
  18081. (define-key org-mode-map [remap outline-mark-subtree] 'org-mark-subtree)
  18082. (define-key org-mode-map [remap outline-show-subtree] 'org-show-subtree)
  18083. (define-key org-mode-map [remap outline-forward-same-level]
  18084. 'org-forward-heading-same-level)
  18085. (define-key org-mode-map [remap outline-backward-same-level]
  18086. 'org-backward-heading-same-level)
  18087. (define-key org-mode-map [remap outline-show-branches]
  18088. 'org-kill-note-or-show-branches)
  18089. (define-key org-mode-map [remap outline-promote] 'org-promote-subtree)
  18090. (define-key org-mode-map [remap outline-demote] 'org-demote-subtree)
  18091. (define-key org-mode-map [remap outline-insert-heading] 'org-ctrl-c-ret)
  18092. (define-key org-mode-map [remap outline-next-visible-heading]
  18093. 'org-next-visible-heading)
  18094. (define-key org-mode-map [remap outline-previous-visible-heading]
  18095. 'org-previous-visible-heading)
  18096. (define-key org-mode-map [remap show-children] 'org-show-children)
  18097. ;; Outline functions from `outline-mode-prefix-map' that can not
  18098. ;; be remapped in Org:
  18099. ;; - the column "key binding" shows whether the Outline function is still
  18100. ;; available in Org mode on the same key that it has been bound to in
  18101. ;; Outline mode:
  18102. ;; - "overridden": key used for a different functionality in Org mode
  18103. ;; - else: key still bound to the same Outline function in Org mode
  18104. ;; | Outline function | key binding | Org replacement |
  18105. ;; |------------------------------------+-------------+--------------------------|
  18106. ;; | `outline-up-heading' | `C-c C-u' | still same function |
  18107. ;; | `outline-move-subtree-up' | overridden | better: org-shiftup |
  18108. ;; | `outline-move-subtree-down' | overridden | better: org-shiftdown |
  18109. ;; | `show-entry' | overridden | no replacement |
  18110. ;; | `show-branches' | `C-c C-k' | still same function |
  18111. ;; | `show-subtree' | overridden | visibility cycling |
  18112. ;; | `show-all' | overridden | no replacement |
  18113. ;; | `hide-subtree' | overridden | visibility cycling |
  18114. ;; | `hide-body' | overridden | no replacement |
  18115. ;; | `hide-entry' | overridden | visibility cycling |
  18116. ;; | `hide-leaves' | overridden | no replacement |
  18117. ;; | `hide-sublevels' | overridden | no replacement |
  18118. ;; | `hide-other' | overridden | no replacement |
  18119. ;; Make `C-c C-x' a prefix key
  18120. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  18121. ;; TAB key with modifiers
  18122. (org-defkey org-mode-map "\C-i" 'org-cycle)
  18123. (org-defkey org-mode-map [(tab)] 'org-cycle)
  18124. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  18125. (org-defkey org-mode-map "\M-\t" #'pcomplete)
  18126. ;; The following line is necessary under Suse GNU/Linux
  18127. (unless (featurep 'xemacs)
  18128. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  18129. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  18130. (define-key org-mode-map [backtab] 'org-shifttab)
  18131. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  18132. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  18133. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  18134. ;; Cursor keys with modifiers
  18135. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  18136. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  18137. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  18138. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  18139. (org-defkey org-mode-map [(control meta shift right)] 'org-increase-number-at-point)
  18140. (org-defkey org-mode-map [(control meta shift left)] 'org-decrease-number-at-point)
  18141. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  18142. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  18143. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  18144. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  18145. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  18146. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  18147. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  18148. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  18149. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  18150. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  18151. (org-defkey org-mode-map [(control shift up)] 'org-shiftcontrolup)
  18152. (org-defkey org-mode-map [(control shift down)] 'org-shiftcontroldown)
  18153. ;; Babel keys
  18154. (define-key org-mode-map org-babel-key-prefix org-babel-map)
  18155. (mapc (lambda (pair)
  18156. (define-key org-babel-map (car pair) (cdr pair)))
  18157. org-babel-key-bindings)
  18158. ;;; Extra keys for tty access.
  18159. ;; We only set them when really needed because otherwise the
  18160. ;; menus don't show the simple keys
  18161. (when (or org-use-extra-keys
  18162. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  18163. (not window-system))
  18164. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  18165. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  18166. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  18167. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  18168. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  18169. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  18170. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  18171. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  18172. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  18173. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  18174. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  18175. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  18176. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  18177. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  18178. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  18179. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  18180. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  18181. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  18182. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  18183. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  18184. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  18185. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  18186. (org-defkey org-mode-map [?\e (tab)] #'pcomplete)
  18187. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  18188. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  18189. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  18190. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  18191. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  18192. ;; All the other keys
  18193. (org-defkey org-mode-map "\C-c\C-a" 'outline-show-all) ; in case allout messed up.
  18194. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  18195. (if (boundp 'narrow-map)
  18196. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  18197. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  18198. (if (boundp 'narrow-map)
  18199. (org-defkey narrow-map "b" 'org-narrow-to-block)
  18200. (org-defkey org-mode-map "\C-xnb" 'org-narrow-to-block))
  18201. (if (boundp 'narrow-map)
  18202. (org-defkey narrow-map "e" 'org-narrow-to-element)
  18203. (org-defkey org-mode-map "\C-xne" 'org-narrow-to-element))
  18204. (org-defkey org-mode-map "\C-\M-t" 'org-transpose-element)
  18205. (org-defkey org-mode-map "\M-}" 'org-forward-element)
  18206. (org-defkey org-mode-map "\M-{" 'org-backward-element)
  18207. (org-defkey org-mode-map "\C-c\C-^" 'org-up-element)
  18208. (org-defkey org-mode-map "\C-c\C-_" 'org-down-element)
  18209. (org-defkey org-mode-map "\C-c\C-f" 'org-forward-heading-same-level)
  18210. (org-defkey org-mode-map "\C-c\C-b" 'org-backward-heading-same-level)
  18211. (org-defkey org-mode-map "\C-c\M-f" 'org-next-block)
  18212. (org-defkey org-mode-map "\C-c\M-b" 'org-previous-block)
  18213. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  18214. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-archive-subtree)
  18215. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
  18216. (org-defkey org-mode-map "\C-c\C-xd" 'org-insert-drawer)
  18217. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  18218. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  18219. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  18220. (org-defkey org-mode-map "\C-c\C-xq" 'org-toggle-tags-groups)
  18221. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  18222. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  18223. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  18224. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  18225. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  18226. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  18227. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  18228. (org-defkey org-mode-map "\C-c\M-w" 'org-copy)
  18229. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  18230. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  18231. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  18232. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  18233. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  18234. (org-defkey org-mode-map "\C-c\C-xv" 'org-copy-visible)
  18235. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  18236. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  18237. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  18238. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  18239. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  18240. (org-defkey org-mode-map "\C-c\M-l" 'org-insert-last-stored-link)
  18241. (org-defkey org-mode-map "\C-c\C-\M-l" 'org-insert-all-links)
  18242. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  18243. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  18244. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  18245. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  18246. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  18247. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  18248. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  18249. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  18250. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  18251. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  18252. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  18253. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  18254. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  18255. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  18256. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  18257. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  18258. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  18259. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  18260. (org-defkey org-mode-map "\C-c^" 'org-sort)
  18261. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  18262. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  18263. (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
  18264. (org-defkey org-mode-map [remap open-line] 'org-open-line)
  18265. (org-defkey org-mode-map [remap comment-dwim] 'org-comment-dwim)
  18266. (org-defkey org-mode-map [remap forward-paragraph] 'org-forward-paragraph)
  18267. (org-defkey org-mode-map [remap backward-paragraph] 'org-backward-paragraph)
  18268. (org-defkey org-mode-map "\M-^" 'org-delete-indentation)
  18269. (org-defkey org-mode-map "\C-m" 'org-return)
  18270. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  18271. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  18272. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  18273. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  18274. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  18275. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  18276. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  18277. (org-defkey org-mode-map "\C-c\"a" 'orgtbl-ascii-plot)
  18278. (org-defkey org-mode-map "\C-c\"g" 'org-plot/gnuplot)
  18279. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  18280. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  18281. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  18282. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  18283. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  18284. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  18285. (org-defkey org-mode-map "\C-c\C-e" 'org-export-dispatch)
  18286. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width)
  18287. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  18288. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  18289. (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
  18290. (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
  18291. (org-defkey org-mode-map "\C-c@" 'org-mark-subtree)
  18292. (org-defkey org-mode-map "\M-h" 'org-mark-element)
  18293. (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
  18294. ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
  18295. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  18296. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  18297. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  18298. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  18299. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  18300. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-in-last)
  18301. (org-defkey org-mode-map "\C-c\C-x\C-z" 'org-resolve-clocks)
  18302. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  18303. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  18304. (org-defkey org-mode-map "\C-c\C-x\C-q" 'org-clock-cancel)
  18305. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  18306. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  18307. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  18308. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-toggle-latex-fragment)
  18309. (org-defkey org-mode-map "\C-c\C-x\C-v" 'org-toggle-inline-images)
  18310. (org-defkey org-mode-map "\C-c\C-x\C-\M-v" 'org-redisplay-inline-images)
  18311. (org-defkey org-mode-map "\C-c\C-x\\" 'org-toggle-pretty-entities)
  18312. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  18313. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  18314. (org-defkey org-mode-map "\C-c\C-xP" 'org-set-property-and-value)
  18315. (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
  18316. (org-defkey org-mode-map "\C-c\C-xE" 'org-inc-effort)
  18317. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  18318. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  18319. (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
  18320. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  18321. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  18322. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  18323. (org-defkey org-mode-map "\C-c\C-x_" 'org-timer-stop)
  18324. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  18325. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  18326. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  18327. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  18328. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  18329. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  18330. (when (featurep 'xemacs)
  18331. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  18332. (defconst org-speed-commands-default
  18333. '(
  18334. ("Outline Navigation")
  18335. ("n" . (org-speed-move-safe 'org-next-visible-heading))
  18336. ("p" . (org-speed-move-safe 'org-previous-visible-heading))
  18337. ("f" . (org-speed-move-safe 'org-forward-heading-same-level))
  18338. ("b" . (org-speed-move-safe 'org-backward-heading-same-level))
  18339. ("F" . org-next-block)
  18340. ("B" . org-previous-block)
  18341. ("u" . (org-speed-move-safe 'outline-up-heading))
  18342. ("j" . org-goto)
  18343. ("g" . (org-refile t))
  18344. ("Outline Visibility")
  18345. ("c" . org-cycle)
  18346. ("C" . org-shifttab)
  18347. (" " . org-display-outline-path)
  18348. ("s" . org-narrow-to-subtree)
  18349. ("=" . org-columns)
  18350. ("Outline Structure Editing")
  18351. ("U" . org-metaup)
  18352. ("D" . org-metadown)
  18353. ("r" . org-metaright)
  18354. ("l" . org-metaleft)
  18355. ("R" . org-shiftmetaright)
  18356. ("L" . org-shiftmetaleft)
  18357. ("i" . (progn (forward-char 1) (call-interactively
  18358. 'org-insert-heading-respect-content)))
  18359. ("^" . org-sort)
  18360. ("w" . org-refile)
  18361. ("a" . org-archive-subtree-default-with-confirmation)
  18362. ("@" . org-mark-subtree)
  18363. ("#" . org-toggle-comment)
  18364. ("Clock Commands")
  18365. ("I" . org-clock-in)
  18366. ("O" . org-clock-out)
  18367. ("Meta Data Editing")
  18368. ("t" . org-todo)
  18369. ("," . (org-priority))
  18370. ("0" . (org-priority ?\ ))
  18371. ("1" . (org-priority ?A))
  18372. ("2" . (org-priority ?B))
  18373. ("3" . (org-priority ?C))
  18374. (":" . org-set-tags-command)
  18375. ("e" . org-set-effort)
  18376. ("E" . org-inc-effort)
  18377. ("W" . (lambda(m) (interactive "sMinutes before warning: ")
  18378. (org-entry-put (point) "APPT_WARNTIME" m)))
  18379. ("Agenda Views etc")
  18380. ("v" . org-agenda)
  18381. ("/" . org-sparse-tree)
  18382. ("Misc")
  18383. ("o" . org-open-at-point)
  18384. ("?" . org-speed-command-help)
  18385. ("<" . (org-agenda-set-restriction-lock 'subtree))
  18386. (">" . (org-agenda-remove-restriction-lock))
  18387. )
  18388. "The default speed commands.")
  18389. (defun org-print-speed-command (e)
  18390. (if (> (length (car e)) 1)
  18391. (progn
  18392. (princ "\n")
  18393. (princ (car e))
  18394. (princ "\n")
  18395. (princ (make-string (length (car e)) ?-))
  18396. (princ "\n"))
  18397. (princ (car e))
  18398. (princ " ")
  18399. (if (symbolp (cdr e))
  18400. (princ (symbol-name (cdr e)))
  18401. (prin1 (cdr e)))
  18402. (princ "\n")))
  18403. (defun org-speed-command-help ()
  18404. "Show the available speed commands."
  18405. (interactive)
  18406. (if (not org-use-speed-commands)
  18407. (user-error "Speed commands are not activated, customize `org-use-speed-commands'")
  18408. (with-output-to-temp-buffer "*Help*"
  18409. (princ "User-defined Speed commands\n===========================\n")
  18410. (mapc 'org-print-speed-command org-speed-commands-user)
  18411. (princ "\n")
  18412. (princ "Built-in Speed commands\n=======================\n")
  18413. (mapc 'org-print-speed-command org-speed-commands-default))
  18414. (with-current-buffer "*Help*"
  18415. (setq truncate-lines t))))
  18416. (defun org-speed-move-safe (cmd)
  18417. "Execute CMD, but make sure that the cursor always ends up in a headline.
  18418. If not, return to the original position and throw an error."
  18419. (interactive)
  18420. (let ((pos (point)))
  18421. (call-interactively cmd)
  18422. (unless (and (bolp) (org-at-heading-p))
  18423. (goto-char pos)
  18424. (error "Boundary reached while executing %s" cmd))))
  18425. (defvar org-self-insert-command-undo-counter 0)
  18426. (defvar org-table-auto-blank-field) ; defined in org-table.el
  18427. (defvar org-speed-command nil)
  18428. (define-obsolete-function-alias
  18429. 'org-speed-command-default-hook 'org-speed-command-activate "24.3")
  18430. (defun org-speed-command-activate (keys)
  18431. "Hook for activating single-letter speed commands.
  18432. `org-speed-commands-default' specifies a minimal command set.
  18433. Use `org-speed-commands-user' for further customization."
  18434. (when (or (and (bolp) (looking-at org-outline-regexp))
  18435. (and (functionp org-use-speed-commands)
  18436. (funcall org-use-speed-commands)))
  18437. (cdr (assoc keys (append org-speed-commands-user
  18438. org-speed-commands-default)))))
  18439. (define-obsolete-function-alias
  18440. 'org-babel-speed-command-hook 'org-babel-speed-command-activate "24.3")
  18441. (defun org-babel-speed-command-activate (keys)
  18442. "Hook for activating single-letter code block commands."
  18443. (when (and (bolp) (looking-at org-babel-src-block-regexp))
  18444. (cdr (assoc keys org-babel-key-bindings))))
  18445. (defcustom org-speed-command-hook
  18446. '(org-speed-command-default-hook org-babel-speed-command-hook)
  18447. "Hook for activating speed commands at strategic locations.
  18448. Hook functions are called in sequence until a valid handler is
  18449. found.
  18450. Each hook takes a single argument, a user-pressed command key
  18451. which is also a `self-insert-command' from the global map.
  18452. Within the hook, examine the cursor position and the command key
  18453. and return nil or a valid handler as appropriate. Handler could
  18454. be one of an interactive command, a function, or a form.
  18455. Set `org-use-speed-commands' to non-nil value to enable this
  18456. hook. The default setting is `org-speed-command-activate'."
  18457. :group 'org-structure
  18458. :version "24.1"
  18459. :type 'hook)
  18460. (defun org-self-insert-command (N)
  18461. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  18462. If the cursor is in a table looking at whitespace, the whitespace is
  18463. overwritten, and the table is not marked as requiring realignment."
  18464. (interactive "p")
  18465. (org-check-before-invisible-edit 'insert)
  18466. (cond
  18467. ((and org-use-speed-commands
  18468. (let ((kv (this-command-keys-vector)))
  18469. (setq org-speed-command
  18470. (run-hook-with-args-until-success
  18471. 'org-speed-command-hook
  18472. (make-string 1 (aref kv (1- (length kv))))))))
  18473. (cond
  18474. ((commandp org-speed-command)
  18475. (setq this-command org-speed-command)
  18476. (call-interactively org-speed-command))
  18477. ((functionp org-speed-command)
  18478. (funcall org-speed-command))
  18479. ((and org-speed-command (listp org-speed-command))
  18480. (eval org-speed-command))
  18481. (t (let (org-use-speed-commands)
  18482. (call-interactively 'org-self-insert-command)))))
  18483. ((and
  18484. (org-at-table-p)
  18485. (progn
  18486. ;; Check if we blank the field, and if that triggers align.
  18487. (and (featurep 'org-table) org-table-auto-blank-field
  18488. (memq last-command
  18489. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
  18490. (if (or (eq (char-after) ?\s) (looking-at "[^|\n]* |"))
  18491. ;; Got extra space, this field does not determine
  18492. ;; column width.
  18493. (let (org-table-may-need-update) (org-table-blank-field))
  18494. ;; No extra space, this field may determine column
  18495. ;; width.
  18496. (org-table-blank-field)))
  18497. t)
  18498. (eq N 1)
  18499. (looking-at "[^|\n]* \\( \\)|"))
  18500. ;; There is room for insertion without re-aligning the table.
  18501. (delete-region (match-beginning 1) (match-end 1))
  18502. (self-insert-command N))
  18503. (t
  18504. (setq org-table-may-need-update t)
  18505. (self-insert-command N)
  18506. (org-fix-tags-on-the-fly)
  18507. (when org-self-insert-cluster-for-undo
  18508. (if (not (eq last-command 'org-self-insert-command))
  18509. (setq org-self-insert-command-undo-counter 1)
  18510. (if (>= org-self-insert-command-undo-counter 20)
  18511. (setq org-self-insert-command-undo-counter 1)
  18512. (and (> org-self-insert-command-undo-counter 0)
  18513. buffer-undo-list (listp buffer-undo-list)
  18514. (not (cadr buffer-undo-list)) ; remove nil entry
  18515. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  18516. (setq org-self-insert-command-undo-counter
  18517. (1+ org-self-insert-command-undo-counter))))))))
  18518. (defun org-check-before-invisible-edit (kind)
  18519. "Check is editing if kind KIND would be dangerous with invisible text around.
  18520. The detailed reaction depends on the user option `org-catch-invisible-edits'."
  18521. ;; First, try to get out of here as quickly as possible, to reduce overhead
  18522. (when (and org-catch-invisible-edits
  18523. (or (not (boundp 'visible-mode)) (not visible-mode))
  18524. (or (get-char-property (point) 'invisible)
  18525. (get-char-property (max (point-min) (1- (point))) 'invisible)))
  18526. ;; OK, we need to take a closer look
  18527. (let* ((invisible-at-point (get-char-property (point) 'invisible))
  18528. (invisible-before-point (unless (bobp) (get-char-property
  18529. (1- (point)) 'invisible)))
  18530. (border-and-ok-direction
  18531. (or
  18532. ;; Check if we are acting predictably before invisible text
  18533. (and invisible-at-point (not invisible-before-point)
  18534. (memq kind '(insert delete-backward)))
  18535. ;; Check if we are acting predictably after invisible text
  18536. ;; This works not well, and I have turned it off. It seems
  18537. ;; better to always show and stop after invisible text.
  18538. ;; (and (not invisible-at-point) invisible-before-point
  18539. ;; (memq kind '(insert delete)))
  18540. )))
  18541. (when (or (memq invisible-at-point '(outline org-hide-block t))
  18542. (memq invisible-before-point '(outline org-hide-block t)))
  18543. (when (eq org-catch-invisible-edits 'error)
  18544. (user-error "Editing in invisible areas is prohibited, make them visible first"))
  18545. (if (and org-custom-properties-overlays
  18546. (y-or-n-p "Display invisible properties in this buffer? "))
  18547. (org-toggle-custom-properties-visibility)
  18548. ;; Make the area visible
  18549. (save-excursion
  18550. (when invisible-before-point
  18551. (goto-char (previous-single-char-property-change
  18552. (point) 'invisible)))
  18553. (outline-show-subtree))
  18554. (cond
  18555. ((eq org-catch-invisible-edits 'show)
  18556. ;; That's it, we do the edit after showing
  18557. (message
  18558. "Unfolding invisible region around point before editing")
  18559. (sit-for 1))
  18560. ((and (eq org-catch-invisible-edits 'smart)
  18561. border-and-ok-direction)
  18562. (message "Unfolding invisible region around point before editing"))
  18563. (t
  18564. ;; Don't do the edit, make the user repeat it in full visibility
  18565. (user-error "Edit in invisible region aborted, repeat to confirm with text visible"))))))))
  18566. (defun org-fix-tags-on-the-fly ()
  18567. (when (and (equal (char-after (point-at-bol)) ?*)
  18568. (org-at-heading-p))
  18569. (org-align-tags-here org-tags-column)))
  18570. (defun org-delete-backward-char (N)
  18571. "Like `delete-backward-char', insert whitespace at field end in tables.
  18572. When deleting backwards, in tables this function will insert whitespace in
  18573. front of the next \"|\" separator, to keep the table aligned. The table will
  18574. still be marked for re-alignment if the field did fill the entire column,
  18575. because, in this case the deletion might narrow the column."
  18576. (interactive "p")
  18577. (save-match-data
  18578. (org-check-before-invisible-edit 'delete-backward)
  18579. (if (and (org-at-table-p)
  18580. (eq N 1)
  18581. (string-match "|" (buffer-substring (point-at-bol) (point)))
  18582. (looking-at ".*?|"))
  18583. (let ((pos (point))
  18584. (noalign (looking-at "[^|\n\r]* |"))
  18585. (c org-table-may-need-update))
  18586. (backward-delete-char N)
  18587. (unless overwrite-mode
  18588. (skip-chars-forward "^|")
  18589. (insert " ")
  18590. (goto-char (1- pos)))
  18591. ;; noalign: if there were two spaces at the end, this field
  18592. ;; does not determine the width of the column.
  18593. (when noalign (setq org-table-may-need-update c)))
  18594. (backward-delete-char N)
  18595. (org-fix-tags-on-the-fly))))
  18596. (defun org-delete-char (N)
  18597. "Like `delete-char', but insert whitespace at field end in tables.
  18598. When deleting characters, in tables this function will insert whitespace in
  18599. front of the next \"|\" separator, to keep the table aligned. The table will
  18600. still be marked for re-alignment if the field did fill the entire column,
  18601. because, in this case the deletion might narrow the column."
  18602. (interactive "p")
  18603. (save-match-data
  18604. (org-check-before-invisible-edit 'delete)
  18605. (if (and (org-at-table-p)
  18606. (not (bolp))
  18607. (not (= (char-after) ?|))
  18608. (eq N 1))
  18609. (if (looking-at ".*?|")
  18610. (let ((pos (point))
  18611. (noalign (looking-at "[^|\n\r]* |"))
  18612. (c org-table-may-need-update))
  18613. (replace-match
  18614. (concat (substring (match-string 0) 1 -1) " |") nil t)
  18615. (goto-char pos)
  18616. ;; noalign: if there were two spaces at the end, this field
  18617. ;; does not determine the width of the column.
  18618. (when noalign (setq org-table-may-need-update c)))
  18619. (delete-char N))
  18620. (delete-char N)
  18621. (org-fix-tags-on-the-fly))))
  18622. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  18623. (put 'org-self-insert-command 'delete-selection
  18624. (lambda ()
  18625. (not (run-hook-with-args-until-success
  18626. 'self-insert-uses-region-functions))))
  18627. (put 'orgtbl-self-insert-command 'delete-selection
  18628. (lambda ()
  18629. (not (run-hook-with-args-until-success
  18630. 'self-insert-uses-region-functions))))
  18631. (put 'org-delete-char 'delete-selection 'supersede)
  18632. (put 'org-delete-backward-char 'delete-selection 'supersede)
  18633. (put 'org-yank 'delete-selection 'yank)
  18634. ;; Make `flyspell-mode' delay after some commands
  18635. (put 'org-self-insert-command 'flyspell-delayed t)
  18636. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  18637. (put 'org-delete-char 'flyspell-delayed t)
  18638. (put 'org-delete-backward-char 'flyspell-delayed t)
  18639. ;; Make pabbrev-mode expand after org-mode commands
  18640. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  18641. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  18642. (defun org-remap (map &rest commands)
  18643. "In MAP, remap the functions given in COMMANDS.
  18644. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  18645. (let (new old)
  18646. (while commands
  18647. (setq old (pop commands) new (pop commands))
  18648. (if (fboundp 'command-remapping)
  18649. (org-defkey map (vector 'remap old) new)
  18650. (substitute-key-definition old new map global-map)))))
  18651. (defun org-transpose-words ()
  18652. "Transpose words for Org.
  18653. This uses the `org-mode-transpose-word-syntax-table' syntax
  18654. table, which interprets characters in `org-emphasis-alist' as
  18655. word constituents."
  18656. (interactive)
  18657. (with-syntax-table org-mode-transpose-word-syntax-table
  18658. (call-interactively 'transpose-words)))
  18659. (org-remap org-mode-map 'transpose-words 'org-transpose-words)
  18660. (when (eq org-enable-table-editor 'optimized)
  18661. ;; If the user wants maximum table support, we need to hijack
  18662. ;; some standard editing functions
  18663. (org-remap org-mode-map
  18664. 'self-insert-command 'org-self-insert-command
  18665. 'delete-char 'org-delete-char
  18666. 'delete-backward-char 'org-delete-backward-char)
  18667. (org-defkey org-mode-map "|" 'org-force-self-insert))
  18668. (defvar org-ctrl-c-ctrl-c-hook nil
  18669. "Hook for functions attaching themselves to `C-c C-c'.
  18670. This can be used to add additional functionality to the C-c C-c
  18671. key which executes context-dependent commands. This hook is run
  18672. before any other test, while `org-ctrl-c-ctrl-c-final-hook' is
  18673. run after the last test.
  18674. Each function will be called with no arguments. The function
  18675. must check if the context is appropriate for it to act. If yes,
  18676. it should do its thing and then return a non-nil value. If the
  18677. context is wrong, just do nothing and return nil.")
  18678. (defvar org-ctrl-c-ctrl-c-final-hook nil
  18679. "Hook for functions attaching themselves to `C-c C-c'.
  18680. This can be used to add additional functionality to the C-c C-c
  18681. key which executes context-dependent commands. This hook is run
  18682. after any other test, while `org-ctrl-c-ctrl-c-hook' is run
  18683. before the first test.
  18684. Each function will be called with no arguments. The function
  18685. must check if the context is appropriate for it to act. If yes,
  18686. it should do its thing and then return a non-nil value. If the
  18687. context is wrong, just do nothing and return nil.")
  18688. (defvar org-tab-first-hook nil
  18689. "Hook for functions to attach themselves to TAB.
  18690. See `org-ctrl-c-ctrl-c-hook' for more information.
  18691. This hook runs as the first action when TAB is pressed, even before
  18692. `org-cycle' messes around with the `outline-regexp' to cater for
  18693. inline tasks and plain list item folding.
  18694. If any function in this hook returns t, any other actions that
  18695. would have been caused by TAB (such as table field motion or visibility
  18696. cycling) will not occur.")
  18697. (defvar org-tab-after-check-for-table-hook nil
  18698. "Hook for functions to attach themselves to TAB.
  18699. See `org-ctrl-c-ctrl-c-hook' for more information.
  18700. This hook runs after it has been established that the cursor is not in a
  18701. table, but before checking if the cursor is in a headline or if global cycling
  18702. should be done.
  18703. If any function in this hook returns t, not other actions like visibility
  18704. cycling will be done.")
  18705. (defvar org-tab-after-check-for-cycling-hook nil
  18706. "Hook for functions to attach themselves to TAB.
  18707. See `org-ctrl-c-ctrl-c-hook' for more information.
  18708. This hook runs after it has been established that not table field motion and
  18709. not visibility should be done because of current context. This is probably
  18710. the place where a package like yasnippets can hook in.")
  18711. (defvar org-tab-before-tab-emulation-hook nil
  18712. "Hook for functions to attach themselves to TAB.
  18713. See `org-ctrl-c-ctrl-c-hook' for more information.
  18714. This hook runs after every other options for TAB have been exhausted, but
  18715. before indentation and \t insertion takes place.")
  18716. (defvar org-metaleft-hook nil
  18717. "Hook for functions attaching themselves to `M-left'.
  18718. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18719. (defvar org-metaright-hook nil
  18720. "Hook for functions attaching themselves to `M-right'.
  18721. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18722. (defvar org-metaup-hook nil
  18723. "Hook for functions attaching themselves to `M-up'.
  18724. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18725. (defvar org-metadown-hook nil
  18726. "Hook for functions attaching themselves to `M-down'.
  18727. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18728. (defvar org-shiftmetaleft-hook nil
  18729. "Hook for functions attaching themselves to `M-S-left'.
  18730. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18731. (defvar org-shiftmetaright-hook nil
  18732. "Hook for functions attaching themselves to `M-S-right'.
  18733. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18734. (defvar org-shiftmetaup-hook nil
  18735. "Hook for functions attaching themselves to `M-S-up'.
  18736. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18737. (defvar org-shiftmetadown-hook nil
  18738. "Hook for functions attaching themselves to `M-S-down'.
  18739. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18740. (defvar org-metareturn-hook nil
  18741. "Hook for functions attaching themselves to `M-RET'.
  18742. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18743. (defvar org-shiftup-hook nil
  18744. "Hook for functions attaching themselves to `S-up'.
  18745. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18746. (defvar org-shiftup-final-hook nil
  18747. "Hook for functions attaching themselves to `S-up'.
  18748. This one runs after all other options except shift-select have been excluded.
  18749. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18750. (defvar org-shiftdown-hook nil
  18751. "Hook for functions attaching themselves to `S-down'.
  18752. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18753. (defvar org-shiftdown-final-hook nil
  18754. "Hook for functions attaching themselves to `S-down'.
  18755. This one runs after all other options except shift-select have been excluded.
  18756. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18757. (defvar org-shiftleft-hook nil
  18758. "Hook for functions attaching themselves to `S-left'.
  18759. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18760. (defvar org-shiftleft-final-hook nil
  18761. "Hook for functions attaching themselves to `S-left'.
  18762. This one runs after all other options except shift-select have been excluded.
  18763. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18764. (defvar org-shiftright-hook nil
  18765. "Hook for functions attaching themselves to `S-right'.
  18766. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18767. (defvar org-shiftright-final-hook nil
  18768. "Hook for functions attaching themselves to `S-right'.
  18769. This one runs after all other options except shift-select have been excluded.
  18770. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18771. (defun org-modifier-cursor-error ()
  18772. "Throw an error, a modified cursor command was applied in wrong context."
  18773. (user-error "This command is active in special context like tables, headlines or items"))
  18774. (defun org-shiftselect-error ()
  18775. "Throw an error because Shift-Cursor command was applied in wrong context."
  18776. (if (and (boundp 'shift-select-mode) shift-select-mode)
  18777. (user-error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
  18778. (user-error "This command works only in special context like headlines or timestamps")))
  18779. (defun org-call-for-shift-select (cmd)
  18780. (let ((this-command-keys-shift-translated t))
  18781. (call-interactively cmd)))
  18782. (defun org-shifttab (&optional arg)
  18783. "Global visibility cycling or move to previous table field.
  18784. Call `org-table-previous-field' within a table.
  18785. When ARG is nil, cycle globally through visibility states.
  18786. When ARG is a numeric prefix, show contents of this level."
  18787. (interactive "P")
  18788. (cond
  18789. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  18790. ((integerp arg)
  18791. (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
  18792. (message "Content view to level: %d" arg)
  18793. (org-content (prefix-numeric-value arg2))
  18794. (org-cycle-show-empty-lines t)
  18795. (setq org-cycle-global-status 'overview)))
  18796. (t (call-interactively 'org-global-cycle))))
  18797. (defun org-shiftmetaleft ()
  18798. "Promote subtree or delete table column.
  18799. Calls `org-promote-subtree', `org-outdent-item-tree', or
  18800. `org-table-delete-column', depending on context. See the
  18801. individual commands for more information."
  18802. (interactive)
  18803. (cond
  18804. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  18805. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  18806. ((org-at-heading-p) (call-interactively 'org-promote-subtree))
  18807. ((if (not (org-region-active-p)) (org-at-item-p)
  18808. (save-excursion (goto-char (region-beginning))
  18809. (org-at-item-p)))
  18810. (call-interactively 'org-outdent-item-tree))
  18811. (t (org-modifier-cursor-error))))
  18812. (defun org-shiftmetaright ()
  18813. "Demote subtree or insert table column.
  18814. Calls `org-demote-subtree', `org-indent-item-tree', or
  18815. `org-table-insert-column', depending on context. See the
  18816. individual commands for more information."
  18817. (interactive)
  18818. (cond
  18819. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  18820. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  18821. ((org-at-heading-p) (call-interactively 'org-demote-subtree))
  18822. ((if (not (org-region-active-p)) (org-at-item-p)
  18823. (save-excursion (goto-char (region-beginning))
  18824. (org-at-item-p)))
  18825. (call-interactively 'org-indent-item-tree))
  18826. (t (org-modifier-cursor-error))))
  18827. (defun org-shiftmetaup (&optional arg)
  18828. "Drag the line at point up.
  18829. In a table, kill the current row.
  18830. On a clock timestamp, update the value of the timestamp like `S-<up>'
  18831. but also adjust the previous clocked item in the clock history.
  18832. Everywhere else, drag the line at point up."
  18833. (interactive "P")
  18834. (cond
  18835. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  18836. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  18837. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  18838. (call-interactively 'org-timestamp-up)))
  18839. (t (call-interactively 'org-drag-line-backward))))
  18840. (defun org-shiftmetadown (&optional arg)
  18841. "Drag the line at point down.
  18842. In a table, insert an empty row at the current line.
  18843. On a clock timestamp, update the value of the timestamp like `S-<down>'
  18844. but also adjust the previous clocked item in the clock history.
  18845. Everywhere else, drag the line at point down."
  18846. (interactive "P")
  18847. (cond
  18848. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  18849. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  18850. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  18851. (call-interactively 'org-timestamp-down)))
  18852. (t (call-interactively 'org-drag-line-forward))))
  18853. (defsubst org-hidden-tree-error ()
  18854. (user-error
  18855. "Hidden subtree, open with TAB or use subtree command M-S-<left>/<right>"))
  18856. (defun org-metaleft (&optional arg)
  18857. "Promote heading, list item at point or move table column left.
  18858. Calls `org-do-promote', `org-outdent-item' or `org-table-move-column',
  18859. depending on context. With no specific context, calls the Emacs
  18860. default `backward-word'. See the individual commands for more
  18861. information.
  18862. This function runs the hook `org-metaleft-hook' as a first step,
  18863. and returns at first non-nil value."
  18864. (interactive "P")
  18865. (cond
  18866. ((run-hook-with-args-until-success 'org-metaleft-hook))
  18867. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  18868. ((org-with-limited-levels
  18869. (or (org-at-heading-p)
  18870. (and (org-region-active-p)
  18871. (save-excursion
  18872. (goto-char (region-beginning))
  18873. (org-at-heading-p)))))
  18874. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  18875. (call-interactively 'org-do-promote))
  18876. ;; At an inline task.
  18877. ((org-at-heading-p)
  18878. (call-interactively 'org-inlinetask-promote))
  18879. ((or (org-at-item-p)
  18880. (and (org-region-active-p)
  18881. (save-excursion
  18882. (goto-char (region-beginning))
  18883. (org-at-item-p))))
  18884. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  18885. (call-interactively 'org-outdent-item))
  18886. (t (call-interactively 'backward-word))))
  18887. (defun org-metaright (&optional arg)
  18888. "Demote heading, list item at point or move table column right.
  18889. In front of a drawer or a block keyword, indent it correctly.
  18890. Calls `org-do-demote', `org-indent-item', `org-table-move-column',
  18891. `org-indnet-drawer' or `org-indent-block' depending on context.
  18892. With no specific context, calls the Emacs default `forward-word'.
  18893. See the individual commands for more information.
  18894. This function runs the hook `org-metaright-hook' as a first step,
  18895. and returns at first non-nil value."
  18896. (interactive "P")
  18897. (cond
  18898. ((run-hook-with-args-until-success 'org-metaright-hook))
  18899. ((org-at-table-p) (call-interactively 'org-table-move-column))
  18900. ((org-at-drawer-p) (call-interactively 'org-indent-drawer))
  18901. ((org-at-block-p) (call-interactively 'org-indent-block))
  18902. ((org-with-limited-levels
  18903. (or (org-at-heading-p)
  18904. (and (org-region-active-p)
  18905. (save-excursion
  18906. (goto-char (region-beginning))
  18907. (org-at-heading-p)))))
  18908. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  18909. (call-interactively 'org-do-demote))
  18910. ;; At an inline task.
  18911. ((org-at-heading-p)
  18912. (call-interactively 'org-inlinetask-demote))
  18913. ((or (org-at-item-p)
  18914. (and (org-region-active-p)
  18915. (save-excursion
  18916. (goto-char (region-beginning))
  18917. (org-at-item-p))))
  18918. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  18919. (call-interactively 'org-indent-item))
  18920. (t (call-interactively 'forward-word))))
  18921. (defun org-check-for-hidden (what)
  18922. "Check if there are hidden headlines/items in the current visual line.
  18923. WHAT can be either `headlines' or `items'. If the current line is
  18924. an outline or item heading and it has a folded subtree below it,
  18925. this function returns t, nil otherwise."
  18926. (let ((re (cond
  18927. ((eq what 'headlines) org-outline-regexp-bol)
  18928. ((eq what 'items) (org-item-beginning-re))
  18929. (t (error "This should not happen"))))
  18930. beg end)
  18931. (save-excursion
  18932. (catch 'exit
  18933. (unless (org-region-active-p)
  18934. (setq beg (point-at-bol))
  18935. (beginning-of-line 2)
  18936. (while (and (not (eobp)) ;; this is like `next-line'
  18937. (get-char-property (1- (point)) 'invisible))
  18938. (beginning-of-line 2))
  18939. (setq end (point))
  18940. (goto-char beg)
  18941. (goto-char (point-at-eol))
  18942. (setq end (max end (point)))
  18943. (while (re-search-forward re end t)
  18944. (when (get-char-property (match-beginning 0) 'invisible)
  18945. (throw 'exit t))))
  18946. nil))))
  18947. (defun org-metaup (&optional arg)
  18948. "Move subtree up or move table row up.
  18949. Calls `org-move-subtree-up' or `org-table-move-row' or
  18950. `org-move-item-up', depending on context. See the individual commands
  18951. for more information."
  18952. (interactive "P")
  18953. (cond
  18954. ((run-hook-with-args-until-success 'org-metaup-hook))
  18955. ((org-region-active-p)
  18956. (let* ((a (min (region-beginning) (region-end)))
  18957. (b (1- (max (region-beginning) (region-end))))
  18958. (c (save-excursion (goto-char a)
  18959. (move-beginning-of-line 0)))
  18960. (d (save-excursion (goto-char a)
  18961. (move-end-of-line 0) (point))))
  18962. (transpose-regions a b c d)
  18963. (goto-char c)))
  18964. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  18965. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  18966. ((org-at-item-p) (call-interactively 'org-move-item-up))
  18967. (t (org-drag-element-backward))))
  18968. (defun org-metadown (&optional arg)
  18969. "Move subtree down or move table row down.
  18970. Calls `org-move-subtree-down' or `org-table-move-row' or
  18971. `org-move-item-down', depending on context. See the individual
  18972. commands for more information."
  18973. (interactive "P")
  18974. (cond
  18975. ((run-hook-with-args-until-success 'org-metadown-hook))
  18976. ((org-region-active-p)
  18977. (let* ((a (min (region-beginning) (region-end)))
  18978. (b (max (region-beginning) (region-end)))
  18979. (c (save-excursion (goto-char b)
  18980. (move-beginning-of-line 1)))
  18981. (d (save-excursion (goto-char b)
  18982. (move-end-of-line 1) (1+ (point)))))
  18983. (transpose-regions a b c d)
  18984. (goto-char d)))
  18985. ((org-at-table-p) (call-interactively 'org-table-move-row))
  18986. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  18987. ((org-at-item-p) (call-interactively 'org-move-item-down))
  18988. (t (org-drag-element-forward))))
  18989. (defun org-shiftup (&optional arg)
  18990. "Increase item in timestamp or increase priority of current headline.
  18991. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  18992. depending on context. See the individual commands for more information."
  18993. (interactive "P")
  18994. (cond
  18995. ((run-hook-with-args-until-success 'org-shiftup-hook))
  18996. ((and org-support-shift-select (org-region-active-p))
  18997. (org-call-for-shift-select 'previous-line))
  18998. ((org-at-timestamp-p t)
  18999. (call-interactively (if org-edit-timestamp-down-means-later
  19000. 'org-timestamp-down 'org-timestamp-up)))
  19001. ((and (not (eq org-support-shift-select 'always))
  19002. org-enable-priority-commands
  19003. (org-at-heading-p))
  19004. (call-interactively 'org-priority-up))
  19005. ((and (not org-support-shift-select) (org-at-item-p))
  19006. (call-interactively 'org-previous-item))
  19007. ((org-clocktable-try-shift 'up arg))
  19008. ((run-hook-with-args-until-success 'org-shiftup-final-hook))
  19009. (org-support-shift-select
  19010. (org-call-for-shift-select 'previous-line))
  19011. (t (org-shiftselect-error))))
  19012. (defun org-shiftdown (&optional arg)
  19013. "Decrease item in timestamp or decrease priority of current headline.
  19014. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  19015. depending on context. See the individual commands for more information."
  19016. (interactive "P")
  19017. (cond
  19018. ((run-hook-with-args-until-success 'org-shiftdown-hook))
  19019. ((and org-support-shift-select (org-region-active-p))
  19020. (org-call-for-shift-select 'next-line))
  19021. ((org-at-timestamp-p t)
  19022. (call-interactively (if org-edit-timestamp-down-means-later
  19023. 'org-timestamp-up 'org-timestamp-down)))
  19024. ((and (not (eq org-support-shift-select 'always))
  19025. org-enable-priority-commands
  19026. (org-at-heading-p))
  19027. (call-interactively 'org-priority-down))
  19028. ((and (not org-support-shift-select) (org-at-item-p))
  19029. (call-interactively 'org-next-item))
  19030. ((org-clocktable-try-shift 'down arg))
  19031. ((run-hook-with-args-until-success 'org-shiftdown-final-hook))
  19032. (org-support-shift-select
  19033. (org-call-for-shift-select 'next-line))
  19034. (t (org-shiftselect-error))))
  19035. (defun org-shiftright (&optional arg)
  19036. "Cycle the thing at point or in the current line, depending on context.
  19037. Depending on context, this does one of the following:
  19038. - switch a timestamp at point one day into the future
  19039. - on a headline, switch to the next TODO keyword.
  19040. - on an item, switch entire list to the next bullet type
  19041. - on a property line, switch to the next allowed value
  19042. - on a clocktable definition line, move time block into the future"
  19043. (interactive "P")
  19044. (cond
  19045. ((run-hook-with-args-until-success 'org-shiftright-hook))
  19046. ((and org-support-shift-select (org-region-active-p))
  19047. (org-call-for-shift-select 'forward-char))
  19048. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  19049. ((and (not (eq org-support-shift-select 'always))
  19050. (org-at-heading-p))
  19051. (let ((org-inhibit-logging
  19052. (not org-treat-S-cursor-todo-selection-as-state-change))
  19053. (org-inhibit-blocking
  19054. (not org-treat-S-cursor-todo-selection-as-state-change)))
  19055. (org-call-with-arg 'org-todo 'right)))
  19056. ((or (and org-support-shift-select
  19057. (not (eq org-support-shift-select 'always))
  19058. (org-at-item-bullet-p))
  19059. (and (not org-support-shift-select) (org-at-item-p)))
  19060. (org-call-with-arg 'org-cycle-list-bullet nil))
  19061. ((and (not (eq org-support-shift-select 'always))
  19062. (org-at-property-p))
  19063. (call-interactively 'org-property-next-allowed-value))
  19064. ((org-clocktable-try-shift 'right arg))
  19065. ((run-hook-with-args-until-success 'org-shiftright-final-hook))
  19066. (org-support-shift-select
  19067. (org-call-for-shift-select 'forward-char))
  19068. (t (org-shiftselect-error))))
  19069. (defun org-shiftleft (&optional arg)
  19070. "Cycle the thing at point or in the current line, depending on context.
  19071. Depending on context, this does one of the following:
  19072. - switch a timestamp at point one day into the past
  19073. - on a headline, switch to the previous TODO keyword.
  19074. - on an item, switch entire list to the previous bullet type
  19075. - on a property line, switch to the previous allowed value
  19076. - on a clocktable definition line, move time block into the past"
  19077. (interactive "P")
  19078. (cond
  19079. ((run-hook-with-args-until-success 'org-shiftleft-hook))
  19080. ((and org-support-shift-select (org-region-active-p))
  19081. (org-call-for-shift-select 'backward-char))
  19082. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  19083. ((and (not (eq org-support-shift-select 'always))
  19084. (org-at-heading-p))
  19085. (let ((org-inhibit-logging
  19086. (not org-treat-S-cursor-todo-selection-as-state-change))
  19087. (org-inhibit-blocking
  19088. (not org-treat-S-cursor-todo-selection-as-state-change)))
  19089. (org-call-with-arg 'org-todo 'left)))
  19090. ((or (and org-support-shift-select
  19091. (not (eq org-support-shift-select 'always))
  19092. (org-at-item-bullet-p))
  19093. (and (not org-support-shift-select) (org-at-item-p)))
  19094. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  19095. ((and (not (eq org-support-shift-select 'always))
  19096. (org-at-property-p))
  19097. (call-interactively 'org-property-previous-allowed-value))
  19098. ((org-clocktable-try-shift 'left arg))
  19099. ((run-hook-with-args-until-success 'org-shiftleft-final-hook))
  19100. (org-support-shift-select
  19101. (org-call-for-shift-select 'backward-char))
  19102. (t (org-shiftselect-error))))
  19103. (defun org-shiftcontrolright ()
  19104. "Switch to next TODO set."
  19105. (interactive)
  19106. (cond
  19107. ((and org-support-shift-select (org-region-active-p))
  19108. (org-call-for-shift-select 'forward-word))
  19109. ((and (not (eq org-support-shift-select 'always))
  19110. (org-at-heading-p))
  19111. (org-call-with-arg 'org-todo 'nextset))
  19112. (org-support-shift-select
  19113. (org-call-for-shift-select 'forward-word))
  19114. (t (org-shiftselect-error))))
  19115. (defun org-shiftcontrolleft ()
  19116. "Switch to previous TODO set."
  19117. (interactive)
  19118. (cond
  19119. ((and org-support-shift-select (org-region-active-p))
  19120. (org-call-for-shift-select 'backward-word))
  19121. ((and (not (eq org-support-shift-select 'always))
  19122. (org-at-heading-p))
  19123. (org-call-with-arg 'org-todo 'previousset))
  19124. (org-support-shift-select
  19125. (org-call-for-shift-select 'backward-word))
  19126. (t (org-shiftselect-error))))
  19127. (defun org-shiftcontrolup (&optional n)
  19128. "Change timestamps synchronously up in CLOCK log lines.
  19129. Optional argument N tells to change by that many units."
  19130. (interactive "P")
  19131. (if (and (org-at-clock-log-p) (org-at-timestamp-p t))
  19132. (let (org-support-shift-select)
  19133. (org-clock-timestamps-up n))
  19134. (user-error "Not at a clock log")))
  19135. (defun org-shiftcontroldown (&optional n)
  19136. "Change timestamps synchronously down in CLOCK log lines.
  19137. Optional argument N tells to change by that many units."
  19138. (interactive "P")
  19139. (if (and (org-at-clock-log-p) (org-at-timestamp-p t))
  19140. (let (org-support-shift-select)
  19141. (org-clock-timestamps-down n))
  19142. (user-error "Not at a clock log")))
  19143. (defun org-increase-number-at-point (&optional inc)
  19144. "Increment the number at point.
  19145. With an optional prefix numeric argument INC, increment using
  19146. this numeric value."
  19147. (interactive "p")
  19148. (if (not (number-at-point))
  19149. (user-error "Not on a number")
  19150. (unless inc (setq inc 1))
  19151. (let ((pos (point))
  19152. (beg (skip-chars-backward "-+^/*0-9eE."))
  19153. (end (skip-chars-forward "-+^/*0-9eE^.")) nap)
  19154. (setq nap (buffer-substring-no-properties
  19155. (+ pos beg) (+ pos beg end)))
  19156. (delete-region (+ pos beg) (+ pos beg end))
  19157. (insert (calc-eval (concat (number-to-string inc) "+" nap))))
  19158. (when (org-at-table-p)
  19159. (org-table-align)
  19160. (org-table-end-of-field 1))))
  19161. (defun org-decrease-number-at-point (&optional inc)
  19162. "Decrement the number at point.
  19163. With an optional prefix numeric argument INC, decrement using
  19164. this numeric value."
  19165. (interactive "p")
  19166. (org-increase-number-at-point (- (or inc 1))))
  19167. (defun org-ctrl-c-ret ()
  19168. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  19169. (interactive)
  19170. (cond
  19171. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  19172. (t (call-interactively 'org-insert-heading))))
  19173. (defun org-find-visible ()
  19174. (let ((s (point)))
  19175. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  19176. (get-char-property s 'invisible)))
  19177. s))
  19178. (defun org-find-invisible ()
  19179. (let ((s (point)))
  19180. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  19181. (not (get-char-property s 'invisible))))
  19182. s))
  19183. (defun org-copy-visible (beg end)
  19184. "Copy the visible parts of the region."
  19185. (interactive "r")
  19186. (let (snippets s)
  19187. (save-excursion
  19188. (save-restriction
  19189. (narrow-to-region beg end)
  19190. (setq s (goto-char (point-min)))
  19191. (while (not (= (point) (point-max)))
  19192. (goto-char (org-find-invisible))
  19193. (push (buffer-substring s (point)) snippets)
  19194. (setq s (goto-char (org-find-visible))))))
  19195. (kill-new (apply 'concat (nreverse snippets)))))
  19196. (defun org-copy-special ()
  19197. "Copy region in table or copy current subtree.
  19198. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  19199. See the individual commands for more information."
  19200. (interactive)
  19201. (call-interactively
  19202. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  19203. (defun org-cut-special ()
  19204. "Cut region in table or cut current subtree.
  19205. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  19206. See the individual commands for more information."
  19207. (interactive)
  19208. (call-interactively
  19209. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  19210. (defun org-paste-special (arg)
  19211. "Paste rectangular region into table, or past subtree relative to level.
  19212. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  19213. See the individual commands for more information."
  19214. (interactive "P")
  19215. (if (org-at-table-p)
  19216. (org-table-paste-rectangle)
  19217. (org-paste-subtree arg)))
  19218. (defsubst org-in-fixed-width-region-p ()
  19219. "Is point in a fixed-width region?"
  19220. (save-match-data
  19221. (eq 'fixed-width (org-element-type (org-element-at-point)))))
  19222. (defun org-edit-special (&optional arg)
  19223. "Call a special editor for the element at point.
  19224. When at a table, call the formula editor with `org-table-edit-formulas'.
  19225. When in a source code block, call `org-edit-src-code'.
  19226. When in a fixed-width region, call `org-edit-fixed-width-region'.
  19227. When in an export block, call `org-edit-export-block'.
  19228. When at an #+INCLUDE keyword, visit the included file.
  19229. When at a footnote reference, call `org-edit-footnote-reference'
  19230. On a link, call `ffap' to visit the link at point.
  19231. Otherwise, return a user error."
  19232. (interactive "P")
  19233. (let ((element (org-element-at-point)))
  19234. (assert (not buffer-read-only) nil
  19235. "Buffer is read-only: %s" (buffer-name))
  19236. (pcase (org-element-type element)
  19237. (`src-block
  19238. (if (not arg) (org-edit-src-code)
  19239. (let* ((info (org-babel-get-src-block-info))
  19240. (lang (nth 0 info))
  19241. (params (nth 2 info))
  19242. (session (cdr (assq :session params))))
  19243. (if (not session) (org-edit-src-code)
  19244. ;; At a src-block with a session and function called with
  19245. ;; an ARG: switch to the buffer related to the inferior
  19246. ;; process.
  19247. (switch-to-buffer
  19248. (funcall (intern (concat "org-babel-prep-session:" lang))
  19249. session params))))))
  19250. (`keyword
  19251. (if (member (org-element-property :key element) '("INCLUDE" "SETUPFILE"))
  19252. (org-open-link-from-string
  19253. (format "[[%s]]"
  19254. (expand-file-name
  19255. (let ((value (org-element-property :value element)))
  19256. (cond ((not (org-string-nw-p value))
  19257. (user-error "No file to edit"))
  19258. ((string-match "\\`\"\\(.*?\\)\"" value)
  19259. (match-string 1 value))
  19260. ((string-match "\\`[^ \t\"]\\S-*" value)
  19261. (match-string 0 value))
  19262. (t (user-error "No valid file specified")))))))
  19263. (user-error "No special environment to edit here")))
  19264. (`table
  19265. (if (eq (org-element-property :type element) 'table.el)
  19266. (org-edit-table.el)
  19267. (call-interactively 'org-table-edit-formulas)))
  19268. ;; Only Org tables contain `table-row' type elements.
  19269. (`table-row (call-interactively 'org-table-edit-formulas))
  19270. (`example-block (org-edit-src-code))
  19271. (`export-block (org-edit-export-block))
  19272. (`fixed-width (org-edit-fixed-width-region))
  19273. (_
  19274. ;; No notable element at point. Though, we may be at a link or
  19275. ;; a footnote reference, which are objects. Thus, scan deeper.
  19276. (let ((context (org-element-context element)))
  19277. (pcase (org-element-type context)
  19278. (`footnote-reference (org-edit-footnote-reference))
  19279. (`inline-src-block (org-edit-inline-src-code))
  19280. (`link (call-interactively #'ffap))
  19281. (_ (user-error "No special environment to edit here"))))))))
  19282. (defvar org-table-coordinate-overlays) ; defined in org-table.el
  19283. (defun org-ctrl-c-ctrl-c (&optional arg)
  19284. "Set tags in headline, or update according to changed information at point.
  19285. This command does many different things, depending on context:
  19286. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  19287. this is what we do.
  19288. - If the cursor is on a statistics cookie, update it.
  19289. - If the cursor is in a headline, prompt for tags and insert them
  19290. into the current line, aligned to `org-tags-column'. When called
  19291. with prefix arg, realign all tags in the current buffer.
  19292. - If the cursor is in one of the special #+KEYWORD lines, this
  19293. triggers scanning the buffer for these lines and updating the
  19294. information.
  19295. - If the cursor is inside a table, realign the table. This command
  19296. works even if the automatic table editor has been turned off.
  19297. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  19298. the entire table.
  19299. - If the cursor is at a footnote reference or definition, jump to
  19300. the corresponding definition or references, respectively.
  19301. - If the cursor is a the beginning of a dynamic block, update it.
  19302. - If the current buffer is a capture buffer, close note and file it.
  19303. - If the cursor is on a <<<target>>>, update radio targets and
  19304. corresponding links in this buffer.
  19305. - If the cursor is on a numbered item in a plain list, renumber the
  19306. ordered list.
  19307. - If the cursor is on a checkbox, toggle it.
  19308. - If the cursor is on a code block, evaluate it. The variable
  19309. `org-confirm-babel-evaluate' can be used to control prompting
  19310. before code block evaluation, by default every code block
  19311. evaluation requires confirmation. Code block evaluation can be
  19312. inhibited by setting `org-babel-no-eval-on-ctrl-c-ctrl-c'."
  19313. (interactive "P")
  19314. (cond
  19315. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  19316. org-occur-highlights)
  19317. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  19318. (org-remove-occur-highlights)
  19319. (message "Temporary highlights/overlays removed from current buffer"))
  19320. ((and (local-variable-p 'org-finish-function (current-buffer))
  19321. (fboundp org-finish-function))
  19322. (funcall org-finish-function))
  19323. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  19324. (t
  19325. (if (save-excursion (beginning-of-line) (looking-at "[ \t]*$"))
  19326. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  19327. (user-error "C-c C-c can do nothing useful at this location"))
  19328. (let* ((context (org-element-context))
  19329. (type (org-element-type context)))
  19330. (case type
  19331. ;; When at a link, act according to the parent instead.
  19332. (link (setq context (org-element-property :parent context))
  19333. (setq type (org-element-type context)))
  19334. ;; Unsupported object types: refer to the first supported
  19335. ;; element or object containing it.
  19336. ((bold code entity export-snippet inline-babel-call inline-src-block
  19337. italic latex-fragment line-break macro strike-through subscript
  19338. superscript underline verbatim)
  19339. (setq context
  19340. (org-element-lineage
  19341. context '(radio-target paragraph verse-block table-cell)))))
  19342. ;; For convenience: at the first line of a paragraph on the
  19343. ;; same line as an item, apply function on that item instead.
  19344. (when (eq type 'paragraph)
  19345. (let ((parent (org-element-property :parent context)))
  19346. (when (and (eq (org-element-type parent) 'item)
  19347. (= (line-beginning-position)
  19348. (org-element-property :begin parent)))
  19349. (setq context parent type 'item))))
  19350. ;; Act according to type of element or object at point.
  19351. (case type
  19352. (clock (org-clock-update-time-maybe))
  19353. (dynamic-block
  19354. (save-excursion
  19355. (goto-char (org-element-property :post-affiliated context))
  19356. (org-update-dblock)))
  19357. (footnote-definition
  19358. (goto-char (org-element-property :post-affiliated context))
  19359. (call-interactively 'org-footnote-action))
  19360. (footnote-reference (call-interactively 'org-footnote-action))
  19361. ((headline inlinetask)
  19362. (save-excursion (goto-char (org-element-property :begin context))
  19363. (call-interactively 'org-set-tags)))
  19364. (item
  19365. ;; At an item: a double C-u set checkbox to "[-]"
  19366. ;; unconditionally, whereas a single one will toggle its
  19367. ;; presence. Without a universal argument, if the item
  19368. ;; has a checkbox, toggle it. Otherwise repair the list.
  19369. (let* ((box (org-element-property :checkbox context))
  19370. (struct (org-element-property :structure context))
  19371. (old-struct (copy-tree struct))
  19372. (parents (org-list-parents-alist struct))
  19373. (prevs (org-list-prevs-alist struct))
  19374. (orderedp (org-not-nil (org-entry-get nil "ORDERED"))))
  19375. (org-list-set-checkbox
  19376. (org-element-property :begin context) struct
  19377. (cond ((equal arg '(16)) "[-]")
  19378. ((and (not box) (equal arg '(4))) "[ ]")
  19379. ((or (not box) (equal arg '(4))) nil)
  19380. ((eq box 'on) "[ ]")
  19381. (t "[X]")))
  19382. ;; Mimic `org-list-write-struct' but with grabbing
  19383. ;; a return value from `org-list-struct-fix-box'.
  19384. (org-list-struct-fix-ind struct parents 2)
  19385. (org-list-struct-fix-item-end struct)
  19386. (org-list-struct-fix-bul struct prevs)
  19387. (org-list-struct-fix-ind struct parents)
  19388. (let ((block-item
  19389. (org-list-struct-fix-box struct parents prevs orderedp)))
  19390. (if (and box (equal struct old-struct))
  19391. (if (equal arg '(16))
  19392. (message "Checkboxes already reset")
  19393. (user-error "Cannot toggle this checkbox: %s"
  19394. (if (eq box 'on)
  19395. "all subitems checked"
  19396. "unchecked subitems")))
  19397. (org-list-struct-apply-struct struct old-struct)
  19398. (org-update-checkbox-count-maybe))
  19399. (when block-item
  19400. (message "Checkboxes were removed due to empty box at line %d"
  19401. (org-current-line block-item))))))
  19402. (keyword
  19403. (let ((org-inhibit-startup-visibility-stuff t)
  19404. (org-startup-align-all-tables nil))
  19405. (when (boundp 'org-table-coordinate-overlays)
  19406. (mapc 'delete-overlay org-table-coordinate-overlays)
  19407. (setq org-table-coordinate-overlays nil))
  19408. (org-save-outline-visibility 'use-markers (org-mode-restart)))
  19409. (message "Local setup has been refreshed"))
  19410. (plain-list
  19411. ;; At a plain list, with a double C-u argument, set
  19412. ;; checkboxes of each item to "[-]", whereas a single one
  19413. ;; will toggle their presence according to the state of the
  19414. ;; first item in the list. Without an argument, repair the
  19415. ;; list.
  19416. (let* ((begin (org-element-property :contents-begin context))
  19417. (beginm (move-marker (make-marker) begin))
  19418. (struct (org-element-property :structure context))
  19419. (old-struct (copy-tree struct))
  19420. (first-box (save-excursion
  19421. (goto-char begin)
  19422. (looking-at org-list-full-item-re)
  19423. (match-string-no-properties 3)))
  19424. (new-box (cond ((equal arg '(16)) "[-]")
  19425. ((equal arg '(4)) (unless first-box "[ ]"))
  19426. ((equal first-box "[X]") "[ ]")
  19427. (t "[X]"))))
  19428. (cond
  19429. (arg
  19430. (mapc (lambda (pos) (org-list-set-checkbox pos struct new-box))
  19431. (org-list-get-all-items
  19432. begin struct (org-list-prevs-alist struct))))
  19433. ((and first-box (eq (point) begin))
  19434. ;; For convenience, when point is at bol on the first
  19435. ;; item of the list and no argument is provided, simply
  19436. ;; toggle checkbox of that item, if any.
  19437. (org-list-set-checkbox begin struct new-box)))
  19438. (org-list-write-struct
  19439. struct (org-list-parents-alist struct) old-struct)
  19440. (org-update-checkbox-count-maybe)
  19441. (save-excursion (goto-char beginm) (org-list-send-list 'maybe))))
  19442. ((property-drawer node-property)
  19443. (call-interactively 'org-property-action))
  19444. ((radio-target target)
  19445. (call-interactively 'org-update-radio-target-regexp))
  19446. (statistics-cookie
  19447. (call-interactively 'org-update-statistics-cookies))
  19448. ((table table-cell table-row)
  19449. ;; At a table, recalculate every field and align it. Also
  19450. ;; send the table if necessary. If the table has
  19451. ;; a `table.el' type, just give up. At a table row or
  19452. ;; cell, maybe recalculate line but always align table.
  19453. (if (eq (org-element-property :type context) 'table.el)
  19454. (message "%s" (substitute-command-keys "\\<org-mode-map>\
  19455. Use \\[org-edit-special] to edit table.el tables"))
  19456. (let ((org-enable-table-editor t))
  19457. (if (or (eq type 'table)
  19458. ;; Check if point is at a TBLFM line.
  19459. (and (eq type 'table-row)
  19460. (= (point) (org-element-property :end context))))
  19461. (save-excursion
  19462. (if (org-at-TBLFM-p)
  19463. (progn (require 'org-table)
  19464. (org-table-calc-current-TBLFM))
  19465. (goto-char (org-element-property :contents-begin context))
  19466. (org-call-with-arg 'org-table-recalculate (or arg t))
  19467. (orgtbl-send-table 'maybe)))
  19468. (org-table-maybe-eval-formula)
  19469. (cond (arg (call-interactively 'org-table-recalculate))
  19470. ((org-table-maybe-recalculate-line))
  19471. (t (org-table-align)))))))
  19472. (timestamp (org-timestamp-change 0 'day))
  19473. (otherwise
  19474. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  19475. (user-error
  19476. "C-c C-c can do nothing useful at this location")))))))))
  19477. (defun org-mode-restart ()
  19478. (interactive)
  19479. (let ((indent-status (org-bound-and-true-p org-indent-mode)))
  19480. (funcall major-mode)
  19481. (hack-local-variables)
  19482. (when (and indent-status (not (org-bound-and-true-p org-indent-mode)))
  19483. (org-indent-mode -1)))
  19484. (message "%s restarted" major-mode))
  19485. (defun org-kill-note-or-show-branches ()
  19486. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  19487. (interactive)
  19488. (if (not org-finish-function)
  19489. (progn
  19490. (outline-hide-subtree)
  19491. (call-interactively 'outline-show-branches))
  19492. (let ((org-note-abort t))
  19493. (funcall org-finish-function))))
  19494. (defun org-delete-indentation (&optional ARG)
  19495. "Join current line to previous and fix whitespace at join.
  19496. If previous line is a headline add to headline title. Otherwise
  19497. the function calls `delete-indentation'.
  19498. With argument, join this line to following line."
  19499. (interactive "*P")
  19500. (if (save-excursion
  19501. (if ARG (beginning-of-line)
  19502. (forward-line -1))
  19503. (looking-at org-complex-heading-regexp))
  19504. ;; At headline.
  19505. (let ((tags-column (when (match-beginning 5)
  19506. (save-excursion (goto-char (match-beginning 5))
  19507. (current-column))))
  19508. (string (concat " " (progn (when ARG (forward-line 1))
  19509. (org-trim (delete-and-extract-region
  19510. (line-beginning-position)
  19511. (line-end-position)))))))
  19512. (unless (bobp) (delete-region (point) (1- (point))))
  19513. (goto-char (or (match-end 4)
  19514. (match-beginning 5)
  19515. (match-end 0)))
  19516. (skip-chars-backward " \t")
  19517. (save-excursion (insert string))
  19518. ;; Adjust alignment of tags.
  19519. (when tags-column
  19520. (org-align-tags-here (if org-auto-align-tags
  19521. org-tags-column
  19522. tags-column))))
  19523. (delete-indentation ARG)))
  19524. (defun org-open-line (n)
  19525. "Insert a new row in tables, call `open-line' elsewhere.
  19526. If `org-special-ctrl-o' is nil, just call `open-line' everywhere."
  19527. (interactive "*p")
  19528. (cond
  19529. ((not org-special-ctrl-o)
  19530. (open-line n))
  19531. ((org-at-table-p)
  19532. (org-table-insert-row))
  19533. (t
  19534. (open-line n))))
  19535. (defun org-return (&optional indent)
  19536. "Goto next table row or insert a newline.
  19537. Calls `org-table-next-row' or `newline', depending on context.
  19538. When optional INDENT argument is non-nil, call
  19539. `newline-and-indent' instead of `newline'.
  19540. When `org-return-follows-link' is non-nil and point is on
  19541. a timestamp or a link, call `org-open-at-point'. However, it
  19542. will not happen if point is in a table or on a \"dead\"
  19543. object (e.g., within a comment). In these case, you need to use
  19544. `org-open-at-point' directly."
  19545. (interactive)
  19546. (let ((context (if org-return-follows-link (org-element-context)
  19547. (org-element-at-point))))
  19548. (cond
  19549. ;; In a table, call `org-table-next-row'.
  19550. ((or (and (eq (org-element-type context) 'table)
  19551. (>= (point) (org-element-property :contents-begin context))
  19552. (< (point) (org-element-property :contents-end context)))
  19553. (org-element-lineage context '(table-row table-cell) t))
  19554. (org-table-justify-field-maybe)
  19555. (call-interactively #'org-table-next-row))
  19556. ;; On a link or a timestamp, call `org-open-at-point' if
  19557. ;; `org-return-follows-link' allows it. Tolerate fuzzy
  19558. ;; locations, e.g., in a comment, as `org-open-at-point'.
  19559. ((and org-return-follows-link
  19560. (or (org-in-regexp org-ts-regexp-both nil t)
  19561. (org-in-regexp org-tsr-regexp-both nil t)
  19562. (org-in-regexp org-any-link-re nil t)))
  19563. (call-interactively #'org-open-at-point))
  19564. ;; Insert newline in heading, but preserve tags.
  19565. ((and (not (bolp))
  19566. (save-excursion (beginning-of-line)
  19567. (looking-at org-complex-heading-regexp)))
  19568. ;; At headline. Split line. However, if point is on keyword,
  19569. ;; priority cookie or tags, do not break any of them: add
  19570. ;; a newline after the headline instead.
  19571. (let ((tags-column (and (match-beginning 5)
  19572. (save-excursion (goto-char (match-beginning 5))
  19573. (current-column))))
  19574. (string
  19575. (when (and (match-end 4)
  19576. (>= (point)
  19577. (or (match-end 3) (match-end 2) (1+ (match-end 1))))
  19578. (<= (point) (match-end 4)))
  19579. (delete-and-extract-region (point) (match-end 4)))))
  19580. (when (and tags-column string) ; Adjust tag alignment.
  19581. (org-align-tags-here
  19582. (if org-auto-align-tags org-tags-column tags-column)))
  19583. (end-of-line)
  19584. (org-show-entry)
  19585. (if indent (newline-and-indent) (newline))
  19586. (when string (save-excursion (insert (org-trim string))))))
  19587. ;; In a list, make sure indenting keeps trailing text within.
  19588. ((and indent
  19589. (not (eolp))
  19590. (org-element-lineage context '(item)))
  19591. (let ((trailing-data
  19592. (delete-and-extract-region (point) (line-end-position))))
  19593. (newline-and-indent)
  19594. (save-excursion (insert trailing-data))))
  19595. (t (if indent (newline-and-indent) (newline))))))
  19596. (defun org-return-indent ()
  19597. "Goto next table row or insert a newline and indent.
  19598. Calls `org-table-next-row' or `newline-and-indent', depending on
  19599. context. See the individual commands for more information."
  19600. (interactive)
  19601. (org-return t))
  19602. (defun org-ctrl-c-star ()
  19603. "Compute table, or change heading status of lines.
  19604. Calls `org-table-recalculate' or `org-toggle-heading',
  19605. depending on context."
  19606. (interactive)
  19607. (cond
  19608. ((org-at-table-p)
  19609. (call-interactively 'org-table-recalculate))
  19610. (t
  19611. ;; Convert all lines in region to list items
  19612. (call-interactively 'org-toggle-heading))))
  19613. (defun org-ctrl-c-minus ()
  19614. "Insert separator line in table or modify bullet status of line.
  19615. Also turns a plain line or a region of lines into list items.
  19616. Calls `org-table-insert-hline', `org-toggle-item', or
  19617. `org-cycle-list-bullet', depending on context."
  19618. (interactive)
  19619. (cond
  19620. ((org-at-table-p)
  19621. (call-interactively 'org-table-insert-hline))
  19622. ((org-region-active-p)
  19623. (call-interactively 'org-toggle-item))
  19624. ((org-in-item-p)
  19625. (call-interactively 'org-cycle-list-bullet))
  19626. (t
  19627. (call-interactively 'org-toggle-item))))
  19628. (defun org-toggle-item (arg)
  19629. "Convert headings or normal lines to items, items to normal lines.
  19630. If there is no active region, only the current line is considered.
  19631. If the first non blank line in the region is a headline, convert
  19632. all headlines to items, shifting text accordingly.
  19633. If it is an item, convert all items to normal lines.
  19634. If it is normal text, change region into a list of items.
  19635. With a prefix argument ARG, change the region in a single item."
  19636. (interactive "P")
  19637. (let ((shift-text
  19638. (function
  19639. ;; Shift text in current section to IND, from point to END.
  19640. ;; The function leaves point to END line.
  19641. (lambda (ind end)
  19642. (let ((min-i 1000) (end (copy-marker end)))
  19643. ;; First determine the minimum indentation (MIN-I) of
  19644. ;; the text.
  19645. (save-excursion
  19646. (catch 'exit
  19647. (while (< (point) end)
  19648. (let ((i (org-get-indentation)))
  19649. (cond
  19650. ;; Skip blank lines and inline tasks.
  19651. ((looking-at "^[ \t]*$"))
  19652. ((looking-at org-outline-regexp-bol))
  19653. ;; We can't find less than 0 indentation.
  19654. ((zerop i) (throw 'exit (setq min-i 0)))
  19655. ((< i min-i) (setq min-i i))))
  19656. (forward-line))))
  19657. ;; Then indent each line so that a line indented to
  19658. ;; MIN-I becomes indented to IND. Ignore blank lines
  19659. ;; and inline tasks in the process.
  19660. (let ((delta (- ind min-i)))
  19661. (while (< (point) end)
  19662. (unless (or (looking-at "^[ \t]*$")
  19663. (looking-at org-outline-regexp-bol))
  19664. (org-indent-line-to (+ (org-get-indentation) delta)))
  19665. (forward-line)))))))
  19666. (skip-blanks
  19667. (function
  19668. ;; Return beginning of first non-blank line, starting from
  19669. ;; line at POS.
  19670. (lambda (pos)
  19671. (save-excursion
  19672. (goto-char pos)
  19673. (skip-chars-forward " \r\t\n")
  19674. (point-at-bol)))))
  19675. beg end)
  19676. ;; Determine boundaries of changes.
  19677. (if (org-region-active-p)
  19678. (setq beg (funcall skip-blanks (region-beginning))
  19679. end (copy-marker (region-end)))
  19680. (setq beg (funcall skip-blanks (point-at-bol))
  19681. end (copy-marker (point-at-eol))))
  19682. ;; Depending on the starting line, choose an action on the text
  19683. ;; between BEG and END.
  19684. (org-with-limited-levels
  19685. (save-excursion
  19686. (goto-char beg)
  19687. (cond
  19688. ;; Case 1. Start at an item: de-itemize. Note that it only
  19689. ;; happens when a region is active: `org-ctrl-c-minus'
  19690. ;; would call `org-cycle-list-bullet' otherwise.
  19691. ((org-at-item-p)
  19692. (while (< (point) end)
  19693. (when (org-at-item-p)
  19694. (skip-chars-forward " \t")
  19695. (delete-region (point) (match-end 0)))
  19696. (forward-line)))
  19697. ;; Case 2. Start at an heading: convert to items.
  19698. ((org-at-heading-p)
  19699. (let* ((bul (org-list-bullet-string "-"))
  19700. (bul-len (length bul))
  19701. (done (org-entry-is-done-p))
  19702. (todo (org-entry-is-todo-p))
  19703. ;; Indentation of the first heading. It should be
  19704. ;; relative to the indentation of its parent, if any.
  19705. (start-ind (save-excursion
  19706. (cond
  19707. ((not org-adapt-indentation) 0)
  19708. ((not (outline-previous-heading)) 0)
  19709. (t (length (match-string 0))))))
  19710. ;; Level of first heading. Further headings will be
  19711. ;; compared to it to determine hierarchy in the list.
  19712. (ref-level (org-reduced-level (org-outline-level))))
  19713. (when (or done todo) (org-todo ""))
  19714. (while (< (point) end)
  19715. (let* ((level (org-reduced-level (org-outline-level)))
  19716. (delta (max 0 (- level ref-level))))
  19717. ;; If current headline is less indented than the first
  19718. ;; one, set it as reference, in order to preserve
  19719. ;; subtrees.
  19720. (when (< level ref-level) (setq ref-level level))
  19721. (replace-match bul t t)
  19722. (org-indent-line-to (+ start-ind (* delta bul-len)))
  19723. (when (or done todo)
  19724. (let* ((struct (org-list-struct))
  19725. (old (copy-tree struct)))
  19726. (org-list-set-checkbox (line-beginning-position)
  19727. struct
  19728. (if done "[X]" "[ ]"))
  19729. (org-list-write-struct struct
  19730. (org-list-parents-alist struct)
  19731. old)))
  19732. ;; Ensure all text down to END (or SECTION-END) belongs
  19733. ;; to the newly created item.
  19734. (let ((section-end (save-excursion
  19735. (or (outline-next-heading) (point)))))
  19736. (forward-line)
  19737. (funcall shift-text
  19738. (+ start-ind (* (1+ delta) bul-len))
  19739. (min end section-end)))))))
  19740. ;; Case 3. Normal line with ARG: make the first line of region
  19741. ;; an item, and shift indentation of others lines to
  19742. ;; set them as item's body.
  19743. (arg (let* ((bul (org-list-bullet-string "-"))
  19744. (bul-len (length bul))
  19745. (ref-ind (org-get-indentation)))
  19746. (skip-chars-forward " \t")
  19747. (insert bul)
  19748. (forward-line)
  19749. (while (< (point) end)
  19750. ;; Ensure that lines less indented than first one
  19751. ;; still get included in item body.
  19752. (funcall shift-text
  19753. (+ ref-ind bul-len)
  19754. (min end (save-excursion (or (outline-next-heading)
  19755. (point)))))
  19756. (forward-line))))
  19757. ;; Case 4. Normal line without ARG: turn each non-item line
  19758. ;; into an item.
  19759. (t
  19760. (while (< (point) end)
  19761. (unless (or (org-at-heading-p) (org-at-item-p))
  19762. (when (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  19763. (replace-match
  19764. (concat "\\1" (org-list-bullet-string "-") "\\2"))))
  19765. (forward-line))))))))
  19766. (defun org-toggle-heading (&optional nstars)
  19767. "Convert headings to normal text, or items or text to headings.
  19768. If there is no active region, only convert the current line.
  19769. With a \\[universal-argument] prefix, convert the whole list at
  19770. point into heading.
  19771. In a region:
  19772. - If the first non blank line is a headline, remove the stars
  19773. from all headlines in the region.
  19774. - If it is a normal line, turn each and every normal line (i.e.,
  19775. not an heading or an item) in the region into headings. If you
  19776. want to convert only the first line of this region, use one
  19777. universal prefix argument.
  19778. - If it is a plain list item, turn all plain list items into headings.
  19779. When converting a line into a heading, the number of stars is chosen
  19780. such that the lines become children of the current entry. However,
  19781. when a numeric prefix argument is given, its value determines the
  19782. number of stars to add."
  19783. (interactive "P")
  19784. (let ((skip-blanks
  19785. (function
  19786. ;; Return beginning of first non-blank line, starting from
  19787. ;; line at POS.
  19788. (lambda (pos)
  19789. (save-excursion
  19790. (goto-char pos)
  19791. (while (org-at-comment-p) (forward-line))
  19792. (skip-chars-forward " \r\t\n")
  19793. (point-at-bol)))))
  19794. beg end toggled)
  19795. ;; Determine boundaries of changes. If a universal prefix has
  19796. ;; been given, put the list in a region. If region ends at a bol,
  19797. ;; do not consider the last line to be in the region.
  19798. (when (and current-prefix-arg (org-at-item-p))
  19799. (when (listp current-prefix-arg) (setq current-prefix-arg 1))
  19800. (org-mark-element))
  19801. (if (org-region-active-p)
  19802. (setq beg (funcall skip-blanks (region-beginning))
  19803. end (copy-marker (save-excursion
  19804. (goto-char (region-end))
  19805. (if (bolp) (point) (point-at-eol)))))
  19806. (setq beg (funcall skip-blanks (point-at-bol))
  19807. end (copy-marker (point-at-eol))))
  19808. ;; Ensure inline tasks don't count as headings.
  19809. (org-with-limited-levels
  19810. (save-excursion
  19811. (goto-char beg)
  19812. (cond
  19813. ;; Case 1. Started at an heading: de-star headings.
  19814. ((org-at-heading-p)
  19815. (while (< (point) end)
  19816. (when (org-at-heading-p t)
  19817. (looking-at org-outline-regexp) (replace-match "")
  19818. (setq toggled t))
  19819. (forward-line)))
  19820. ;; Case 2. Started at an item: change items into headlines.
  19821. ;; One star will be added by `org-list-to-subtree'.
  19822. ((org-at-item-p)
  19823. (while (< (point) end)
  19824. (when (org-at-item-p)
  19825. ;; Pay attention to cases when region ends before list.
  19826. (let* ((struct (org-list-struct))
  19827. (list-end (min (org-list-get-bottom-point struct) (1+ end))))
  19828. (save-restriction
  19829. (narrow-to-region (point) list-end)
  19830. (insert (org-list-to-subtree (org-list-to-lisp t)))))
  19831. (setq toggled t))
  19832. (forward-line)))
  19833. ;; Case 3. Started at normal text: make every line an heading,
  19834. ;; skipping headlines and items.
  19835. (t (let* ((stars
  19836. (make-string
  19837. (if (numberp nstars) nstars (or (org-current-level) 0)) ?*))
  19838. (add-stars
  19839. (cond (nstars "") ; stars from prefix only
  19840. ((equal stars "") "*") ; before first heading
  19841. (org-odd-levels-only "**") ; inside heading, odd
  19842. (t "*"))) ; inside heading, oddeven
  19843. (rpl (concat stars add-stars " "))
  19844. (lend (when (listp nstars) (save-excursion (end-of-line) (point)))))
  19845. (while (< (point) (if (equal nstars '(4)) lend end))
  19846. (when (and (not (or (org-at-heading-p) (org-at-item-p) (org-at-comment-p)))
  19847. (looking-at "\\([ \t]*\\)\\(\\S-\\)"))
  19848. (replace-match (concat rpl (match-string 2))) (setq toggled t))
  19849. (forward-line)))))))
  19850. (unless toggled (message "Cannot toggle heading from here"))))
  19851. (defun org-meta-return (&optional _arg)
  19852. "Insert a new heading or wrap a region in a table.
  19853. Calls `org-insert-heading' or `org-table-wrap-region', depending
  19854. on context. See the individual commands for more information."
  19855. (interactive)
  19856. (org-check-before-invisible-edit 'insert)
  19857. (or (run-hook-with-args-until-success 'org-metareturn-hook)
  19858. (call-interactively (if (org-at-table-p) #'org-table-wrap-region
  19859. #'org-insert-heading))))
  19860. ;;; Menu entries
  19861. (defsubst org-in-subtree-not-table-p ()
  19862. "Are we in a subtree and not in a table?"
  19863. (and (not (org-before-first-heading-p))
  19864. (not (org-at-table-p))))
  19865. ;; Define the Org-mode menus
  19866. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  19867. '("Tbl"
  19868. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  19869. ["Next Field" org-cycle (org-at-table-p)]
  19870. ["Previous Field" org-shifttab (org-at-table-p)]
  19871. ["Next Row" org-return (org-at-table-p)]
  19872. "--"
  19873. ["Blank Field" org-table-blank-field (org-at-table-p)]
  19874. ["Edit Field" org-table-edit-field (org-at-table-p)]
  19875. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  19876. "--"
  19877. ("Column"
  19878. ["Move Column Left" org-metaleft (org-at-table-p)]
  19879. ["Move Column Right" org-metaright (org-at-table-p)]
  19880. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  19881. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  19882. ("Row"
  19883. ["Move Row Up" org-metaup (org-at-table-p)]
  19884. ["Move Row Down" org-metadown (org-at-table-p)]
  19885. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  19886. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  19887. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  19888. "--"
  19889. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  19890. ("Rectangle"
  19891. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  19892. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  19893. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  19894. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  19895. "--"
  19896. ("Calculate"
  19897. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  19898. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  19899. ["Edit Formulas" org-edit-special (org-at-table-p)]
  19900. "--"
  19901. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  19902. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  19903. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  19904. "--"
  19905. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  19906. "--"
  19907. ["Sum Column/Rectangle" org-table-sum
  19908. (or (org-at-table-p) (org-region-active-p))]
  19909. ["Which Column?" org-table-current-column (org-at-table-p)])
  19910. ["Debug Formulas"
  19911. org-table-toggle-formula-debugger
  19912. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  19913. ["Show Col/Row Numbers"
  19914. org-table-toggle-coordinate-overlays
  19915. :style toggle
  19916. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  19917. "--"
  19918. ["Create" org-table-create (and (not (org-at-table-p))
  19919. org-enable-table-editor)]
  19920. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  19921. ["Import from File" org-table-import (not (org-at-table-p))]
  19922. ["Export to File" org-table-export (org-at-table-p)]
  19923. "--"
  19924. ["Create/Convert from/to table.el" org-table-create-with-table.el t]
  19925. "--"
  19926. ("Plot"
  19927. ["Ascii plot" orgtbl-ascii-plot :active (org-at-table-p) :keys "C-c \" a"]
  19928. ["Gnuplot" org-plot/gnuplot :active (org-at-table-p) :keys "C-c \" g"])))
  19929. (easy-menu-define org-org-menu org-mode-map "Org menu"
  19930. '("Org"
  19931. ("Show/Hide"
  19932. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  19933. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  19934. ["Sparse Tree..." org-sparse-tree t]
  19935. ["Reveal Context" org-reveal t]
  19936. ["Show All" outline-show-all t]
  19937. "--"
  19938. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  19939. "--"
  19940. ["New Heading" org-insert-heading t]
  19941. ("Navigate Headings"
  19942. ["Up" outline-up-heading t]
  19943. ["Next" outline-next-visible-heading t]
  19944. ["Previous" outline-previous-visible-heading t]
  19945. ["Next Same Level" outline-forward-same-level t]
  19946. ["Previous Same Level" outline-backward-same-level t]
  19947. "--"
  19948. ["Jump" org-goto t])
  19949. ("Edit Structure"
  19950. ["Refile Subtree" org-refile (org-in-subtree-not-table-p)]
  19951. "--"
  19952. ["Move Subtree Up" org-metaup (org-at-heading-p)]
  19953. ["Move Subtree Down" org-metadown (org-at-heading-p)]
  19954. "--"
  19955. ["Copy Subtree" org-copy-special (org-in-subtree-not-table-p)]
  19956. ["Cut Subtree" org-cut-special (org-in-subtree-not-table-p)]
  19957. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  19958. "--"
  19959. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  19960. "--"
  19961. ["Copy visible text" org-copy-visible t]
  19962. "--"
  19963. ["Promote Heading" org-metaleft (org-in-subtree-not-table-p)]
  19964. ["Promote Subtree" org-shiftmetaleft (org-in-subtree-not-table-p)]
  19965. ["Demote Heading" org-metaright (org-in-subtree-not-table-p)]
  19966. ["Demote Subtree" org-shiftmetaright (org-in-subtree-not-table-p)]
  19967. "--"
  19968. ["Sort Region/Children" org-sort t]
  19969. "--"
  19970. ["Convert to odd levels" org-convert-to-odd-levels t]
  19971. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  19972. ("Editing"
  19973. ["Emphasis..." org-emphasize t]
  19974. ["Edit Source Example" org-edit-special t]
  19975. "--"
  19976. ["Footnote new/jump" org-footnote-action t]
  19977. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  19978. ("Archive"
  19979. ["Archive (default method)" org-archive-subtree-default (org-in-subtree-not-table-p)]
  19980. "--"
  19981. ["Move Subtree to Archive file" org-archive-subtree (org-in-subtree-not-table-p)]
  19982. ["Toggle ARCHIVE tag" org-toggle-archive-tag (org-in-subtree-not-table-p)]
  19983. ["Move subtree to Archive sibling" org-archive-to-archive-sibling (org-in-subtree-not-table-p)]
  19984. )
  19985. "--"
  19986. ("Hyperlinks"
  19987. ["Store Link (Global)" org-store-link t]
  19988. ["Find existing link to here" org-occur-link-in-agenda-files t]
  19989. ["Insert Link" org-insert-link t]
  19990. ["Follow Link" org-open-at-point t]
  19991. "--"
  19992. ["Next link" org-next-link t]
  19993. ["Previous link" org-previous-link t]
  19994. "--"
  19995. ["Descriptive Links"
  19996. org-toggle-link-display
  19997. :style radio
  19998. :selected org-descriptive-links
  19999. ]
  20000. ["Literal Links"
  20001. org-toggle-link-display
  20002. :style radio
  20003. :selected (not org-descriptive-links)])
  20004. "--"
  20005. ("TODO Lists"
  20006. ["TODO/DONE/-" org-todo t]
  20007. ("Select keyword"
  20008. ["Next keyword" org-shiftright (org-at-heading-p)]
  20009. ["Previous keyword" org-shiftleft (org-at-heading-p)]
  20010. ["Complete Keyword" pcomplete (assq :todo-keyword (org-context))]
  20011. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))]
  20012. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))])
  20013. ["Show TODO Tree" org-show-todo-tree :active t :keys "C-c / t"]
  20014. ["Global TODO list" org-todo-list :active t :keys "C-c a t"]
  20015. "--"
  20016. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  20017. :selected org-enforce-todo-dependencies :style toggle :active t]
  20018. "Settings for tree at point"
  20019. ["Do Children sequentially" org-toggle-ordered-property :style radio
  20020. :selected (org-entry-get nil "ORDERED")
  20021. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  20022. ["Do Children parallel" org-toggle-ordered-property :style radio
  20023. :selected (not (org-entry-get nil "ORDERED"))
  20024. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  20025. "--"
  20026. ["Set Priority" org-priority t]
  20027. ["Priority Up" org-shiftup t]
  20028. ["Priority Down" org-shiftdown t]
  20029. "--"
  20030. ["Get news from all feeds" org-feed-update-all t]
  20031. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  20032. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  20033. ("TAGS and Properties"
  20034. ["Set Tags" org-set-tags-command (not (org-before-first-heading-p))]
  20035. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  20036. "--"
  20037. ["Set property" org-set-property (not (org-before-first-heading-p))]
  20038. ["Column view of properties" org-columns t]
  20039. ["Insert Column View DBlock" org-insert-columns-dblock t])
  20040. ("Dates and Scheduling"
  20041. ["Timestamp" org-time-stamp (not (org-before-first-heading-p))]
  20042. ["Timestamp (inactive)" org-time-stamp-inactive (not (org-before-first-heading-p))]
  20043. ("Change Date"
  20044. ["1 Day Later" org-shiftright (org-at-timestamp-p)]
  20045. ["1 Day Earlier" org-shiftleft (org-at-timestamp-p)]
  20046. ["1 ... Later" org-shiftup (org-at-timestamp-p)]
  20047. ["1 ... Earlier" org-shiftdown (org-at-timestamp-p)])
  20048. ["Compute Time Range" org-evaluate-time-range t]
  20049. ["Schedule Item" org-schedule (not (org-before-first-heading-p))]
  20050. ["Deadline" org-deadline (not (org-before-first-heading-p))]
  20051. "--"
  20052. ["Custom time format" org-toggle-time-stamp-overlays
  20053. :style radio :selected org-display-custom-times]
  20054. "--"
  20055. ["Goto Calendar" org-goto-calendar t]
  20056. ["Date from Calendar" org-date-from-calendar t]
  20057. "--"
  20058. ["Start/Restart Timer" org-timer-start t]
  20059. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  20060. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  20061. ["Insert Timer String" org-timer t]
  20062. ["Insert Timer Item" org-timer-item t])
  20063. ("Logging work"
  20064. ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
  20065. ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
  20066. ["Clock out" org-clock-out t]
  20067. ["Clock cancel" org-clock-cancel t]
  20068. "--"
  20069. ["Mark as default task" org-clock-mark-default-task t]
  20070. ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
  20071. ["Goto running clock" org-clock-goto t]
  20072. "--"
  20073. ["Display times" org-clock-display t]
  20074. ["Create clock table" org-clock-report t]
  20075. "--"
  20076. ["Record DONE time"
  20077. (progn (setq org-log-done (not org-log-done))
  20078. (message "Switching to %s will %s record a timestamp"
  20079. (car org-done-keywords)
  20080. (if org-log-done "automatically" "not")))
  20081. :style toggle :selected org-log-done])
  20082. "--"
  20083. ["Agenda Command..." org-agenda t]
  20084. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  20085. ("File List for Agenda")
  20086. ("Special views current file"
  20087. ["TODO Tree" org-show-todo-tree t]
  20088. ["Check Deadlines" org-check-deadlines t]
  20089. ["Timeline" org-timeline t]
  20090. ["Tags/Property tree" org-match-sparse-tree t])
  20091. "--"
  20092. ["Export/Publish..." org-export-dispatch t]
  20093. ("LaTeX"
  20094. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  20095. :selected org-cdlatex-mode]
  20096. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  20097. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  20098. ["Modify math symbol" org-cdlatex-math-modify
  20099. (org-inside-LaTeX-fragment-p)]
  20100. ["Insert citation" org-reftex-citation t]
  20101. "--"
  20102. ["Template for BEAMER" (org-beamer-insert-options-template) t])
  20103. "--"
  20104. ("MobileOrg"
  20105. ["Push Files and Views" org-mobile-push t]
  20106. ["Get Captured and Flagged" org-mobile-pull t]
  20107. ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
  20108. "--"
  20109. ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
  20110. "--"
  20111. ("Documentation"
  20112. ["Show Version" org-version t]
  20113. ["Info Documentation" org-info t])
  20114. ("Customize"
  20115. ["Browse Org Group" org-customize t]
  20116. "--"
  20117. ["Expand This Menu" org-create-customize-menu
  20118. (fboundp 'customize-menu-create)])
  20119. ["Send bug report" org-submit-bug-report t]
  20120. "--"
  20121. ("Refresh/Reload"
  20122. ["Refresh setup current buffer" org-mode-restart t]
  20123. ["Reload Org (after update)" org-reload t]
  20124. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x !"])
  20125. ))
  20126. (defun org-info (&optional node)
  20127. "Read documentation for Org-mode in the info system.
  20128. With optional NODE, go directly to that node."
  20129. (interactive)
  20130. (info (format "(org)%s" (or node ""))))
  20131. ;;;###autoload
  20132. (defun org-submit-bug-report ()
  20133. "Submit a bug report on Org-mode via mail.
  20134. Don't hesitate to report any problems or inaccurate documentation.
  20135. If you don't have setup sending mail from (X)Emacs, please copy the
  20136. output buffer into your mail program, as it gives us important
  20137. information about your Org-mode version and configuration."
  20138. (interactive)
  20139. (require 'reporter)
  20140. (defvar reporter-prompt-for-summary-p)
  20141. (org-load-modules-maybe)
  20142. (org-require-autoloaded-modules)
  20143. (let ((reporter-prompt-for-summary-p "Bug report subject: "))
  20144. (reporter-submit-bug-report
  20145. "emacs-orgmode@gnu.org"
  20146. (org-version nil 'full)
  20147. (let (list)
  20148. (save-window-excursion
  20149. (org-pop-to-buffer-same-window (get-buffer-create "*Warn about privacy*"))
  20150. (delete-other-windows)
  20151. (erase-buffer)
  20152. (insert "You are about to submit a bug report to the Org-mode mailing list.
  20153. We would like to add your full Org-mode and Outline configuration to the
  20154. bug report. This greatly simplifies the work of the maintainer and
  20155. other experts on the mailing list.
  20156. HOWEVER, some variables you have customized may contain private
  20157. information. The names of customers, colleagues, or friends, might
  20158. appear in the form of file names, tags, todo states, or search strings.
  20159. If you answer yes to the prompt, you might want to check and remove
  20160. such private information before sending the email.")
  20161. (add-text-properties (point-min) (point-max) '(face org-warning))
  20162. (when (yes-or-no-p "Include your Org-mode configuration ")
  20163. (mapatoms
  20164. (lambda (v)
  20165. (and (boundp v)
  20166. (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
  20167. (or (and (symbol-value v)
  20168. (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
  20169. (and
  20170. (get v 'custom-type) (get v 'standard-value)
  20171. (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
  20172. (push v list)))))
  20173. (kill-buffer (get-buffer "*Warn about privacy*"))
  20174. list))
  20175. nil nil
  20176. "Remember to cover the basics, that is, what you expected to happen and
  20177. what in fact did happen. You don't know how to make a good report? See
  20178. http://orgmode.org/manual/Feedback.html#Feedback
  20179. Your bug report will be posted to the Org-mode mailing list.
  20180. ------------------------------------------------------------------------")
  20181. (save-excursion
  20182. (when (re-search-backward "^\\(Subject: \\)Org-mode version \\(.*?\\);[ \t]*\\(.*\\)" nil t)
  20183. (replace-match "\\1Bug: \\3 [\\2]")))))
  20184. (defun org-install-agenda-files-menu ()
  20185. (let ((bl (buffer-list)))
  20186. (save-excursion
  20187. (while bl
  20188. (set-buffer (pop bl))
  20189. (when (derived-mode-p 'org-mode) (setq bl nil)))
  20190. (when (derived-mode-p 'org-mode)
  20191. (easy-menu-change
  20192. '("Org") "File List for Agenda"
  20193. (append
  20194. (list
  20195. ["Edit File List" (org-edit-agenda-file-list) t]
  20196. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  20197. ["Remove Current File from List" org-remove-file t]
  20198. ["Cycle through agenda files" org-cycle-agenda-files t]
  20199. ["Occur in all agenda files" org-occur-in-agenda-files t]
  20200. "--")
  20201. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  20202. ;;;; Documentation
  20203. (defun org-require-autoloaded-modules ()
  20204. (interactive)
  20205. (mapc 'require
  20206. '(org-agenda org-archive org-attach org-clock org-colview org-id
  20207. org-table org-timer)))
  20208. ;;;###autoload
  20209. (defun org-reload (&optional uncompiled)
  20210. "Reload all org lisp files.
  20211. With prefix arg UNCOMPILED, load the uncompiled versions."
  20212. (interactive "P")
  20213. (require 'loadhist)
  20214. (let* ((org-dir (org-find-library-dir "org"))
  20215. (contrib-dir (or (org-find-library-dir "org-contribdir") org-dir))
  20216. (feature-re "^\\(org\\|ob\\|ox\\)\\(-.*\\)?")
  20217. (remove-re (mapconcat 'identity
  20218. (mapcar (lambda (f) (concat "^" f "$"))
  20219. (list (if (featurep 'xemacs)
  20220. "org-colview"
  20221. "org-colview-xemacs")
  20222. "org" "org-loaddefs" "org-version"))
  20223. "\\|"))
  20224. (feats (delete-dups
  20225. (mapcar 'file-name-sans-extension
  20226. (mapcar 'file-name-nondirectory
  20227. (delq nil
  20228. (mapcar 'feature-file
  20229. features))))))
  20230. (lfeat (append
  20231. (sort
  20232. (setq feats
  20233. (delq nil (mapcar
  20234. (lambda (f)
  20235. (if (and (string-match feature-re f)
  20236. (not (string-match remove-re f)))
  20237. f nil))
  20238. feats)))
  20239. 'string-lessp)
  20240. (list "org-version" "org")))
  20241. (load-suffixes (when (boundp 'load-suffixes) load-suffixes))
  20242. (load-suffixes (if uncompiled (reverse load-suffixes) load-suffixes))
  20243. load-uncore load-misses)
  20244. (setq load-misses
  20245. (delq 't
  20246. (mapcar (lambda (f)
  20247. (or (org-load-noerror-mustsuffix (concat org-dir f))
  20248. (and (string= org-dir contrib-dir)
  20249. (org-load-noerror-mustsuffix (concat contrib-dir f)))
  20250. (and (org-load-noerror-mustsuffix (concat (org-find-library-dir f) f))
  20251. (add-to-list 'load-uncore f 'append)
  20252. 't)
  20253. f))
  20254. lfeat)))
  20255. (when load-uncore
  20256. (message "The following feature%s found in load-path, please check if that's correct:\n%s"
  20257. (if (> (length load-uncore) 1) "s were" " was") load-uncore))
  20258. (if load-misses
  20259. (message "Some error occurred while reloading Org feature%s\n%s\nPlease check *Messages*!\n%s"
  20260. (if (> (length load-misses) 1) "s" "") load-misses (org-version nil 'full))
  20261. (message "Successfully reloaded Org\n%s" (org-version nil 'full)))))
  20262. ;;;###autoload
  20263. (defun org-customize ()
  20264. "Call the customize function with org as argument."
  20265. (interactive)
  20266. (org-load-modules-maybe)
  20267. (org-require-autoloaded-modules)
  20268. (customize-browse 'org))
  20269. (defun org-create-customize-menu ()
  20270. "Create a full customization menu for Org-mode, insert it into the menu."
  20271. (interactive)
  20272. (org-load-modules-maybe)
  20273. (org-require-autoloaded-modules)
  20274. (if (fboundp 'customize-menu-create)
  20275. (progn
  20276. (easy-menu-change
  20277. '("Org") "Customize"
  20278. `(["Browse Org group" org-customize t]
  20279. "--"
  20280. ,(customize-menu-create 'org)
  20281. ["Set" Custom-set t]
  20282. ["Save" Custom-save t]
  20283. ["Reset to Current" Custom-reset-current t]
  20284. ["Reset to Saved" Custom-reset-saved t]
  20285. ["Reset to Standard Settings" Custom-reset-standard t]))
  20286. (message "\"Org\"-menu now contains full customization menu"))
  20287. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  20288. ;;;; Miscellaneous stuff
  20289. ;;; Generally useful functions
  20290. (defsubst org-get-at-eol (property n)
  20291. "Get text property PROPERTY at the end of line less N characters."
  20292. (get-text-property (- (point-at-eol) n) property))
  20293. (defun org-find-text-property-in-string (prop s)
  20294. "Return the first non-nil value of property PROP in string S."
  20295. (or (get-text-property 0 prop s)
  20296. (get-text-property (or (next-single-property-change 0 prop s) 0)
  20297. prop s)))
  20298. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  20299. "Display the given MESSAGE as a warning."
  20300. (if (fboundp 'display-warning)
  20301. (display-warning 'org message
  20302. (if (featurep 'xemacs) 'warning :warning))
  20303. (let ((buf (get-buffer-create "*Org warnings*")))
  20304. (with-current-buffer buf
  20305. (goto-char (point-max))
  20306. (insert "Warning (Org): " message)
  20307. (unless (bolp)
  20308. (newline)))
  20309. (display-buffer buf)
  20310. (sit-for 0))))
  20311. (defun org-eval (form)
  20312. "Eval FORM and return result."
  20313. (condition-case error
  20314. (eval form)
  20315. (error (format "%%![Error: %s]" error))))
  20316. (defun org-in-clocktable-p ()
  20317. "Check if the cursor is in a clocktable."
  20318. (let ((pos (point)) start)
  20319. (save-excursion
  20320. (end-of-line 1)
  20321. (and (re-search-backward "^[ \t]*#\\+BEGIN:[ \t]+clocktable" nil t)
  20322. (setq start (match-beginning 0))
  20323. (re-search-forward "^[ \t]*#\\+END:.*" nil t)
  20324. (>= (match-end 0) pos)
  20325. start))))
  20326. (defun org-in-verbatim-emphasis ()
  20327. (save-match-data
  20328. (and (org-in-regexp org-emph-re 2)
  20329. (>= (point) (match-beginning 3))
  20330. (<= (point) (match-end 4))
  20331. (member (match-string 3) '("=" "~")))))
  20332. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  20333. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  20334. (if (and marker (marker-buffer marker)
  20335. (buffer-live-p (marker-buffer marker)))
  20336. (progn
  20337. (org-pop-to-buffer-same-window (marker-buffer marker))
  20338. (when (or (> marker (point-max)) (< marker (point-min)))
  20339. (widen))
  20340. (goto-char marker)
  20341. (org-show-context 'org-goto))
  20342. (if bookmark
  20343. (bookmark-jump bookmark)
  20344. (error "Cannot find location"))))
  20345. (defun org-quote-csv-field (s)
  20346. "Quote field for inclusion in CSV material."
  20347. (if (string-match "[\",]" s)
  20348. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  20349. s))
  20350. (defun org-force-self-insert (N)
  20351. "Needed to enforce self-insert under remapping."
  20352. (interactive "p")
  20353. (self-insert-command N))
  20354. (defun org-string-width (s)
  20355. "Compute width of string, ignoring invisible characters.
  20356. This ignores character with invisibility property `org-link', and also
  20357. characters with property `org-cwidth', because these will become invisible
  20358. upon the next fontification round."
  20359. (let (b l)
  20360. (when (or (eq t buffer-invisibility-spec)
  20361. (assq 'org-link buffer-invisibility-spec))
  20362. (while (setq b (text-property-any 0 (length s)
  20363. 'invisible 'org-link s))
  20364. (setq s (concat (substring s 0 b)
  20365. (substring s (or (next-single-property-change
  20366. b 'invisible s)
  20367. (length s)))))))
  20368. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  20369. (setq s (concat (substring s 0 b)
  20370. (substring s (or (next-single-property-change
  20371. b 'org-cwidth s)
  20372. (length s))))))
  20373. (setq l (string-width s) b -1)
  20374. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  20375. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  20376. l))
  20377. (defun org-shorten-string (s maxlength)
  20378. "Shorten string S so tht it is no longer than MAXLENGTH characters.
  20379. If the string is shorter or has length MAXLENGTH, just return the
  20380. original string. If it is longer, the functions finds a space in the
  20381. string, breaks this string off at that locations and adds three dots
  20382. as ellipsis. Including the ellipsis, the string will not be longer
  20383. than MAXLENGTH. If finding a good breaking point in the string does
  20384. not work, the string is just chopped off in the middle of a word
  20385. if necessary."
  20386. (if (<= (length s) maxlength)
  20387. s
  20388. (let* ((n (max (- maxlength 4) 1))
  20389. (re (concat "\\`\\(.\\{1," (int-to-string n) "\\}[^ ]\\)\\([ ]\\|\\'\\)")))
  20390. (if (string-match re s)
  20391. (concat (match-string 1 s) "...")
  20392. (concat (substring s 0 (max (- maxlength 3) 0)) "...")))))
  20393. (defun org-get-indentation (&optional line)
  20394. "Get the indentation of the current line, interpreting tabs.
  20395. When LINE is given, assume it represents a line and compute its indentation."
  20396. (if line
  20397. (when (string-match "^ *" (org-remove-tabs line))
  20398. (match-end 0))
  20399. (save-excursion
  20400. (beginning-of-line 1)
  20401. (skip-chars-forward " \t")
  20402. (current-column))))
  20403. (defun org-get-string-indentation (s)
  20404. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  20405. (let ((n -1) (i 0) (w tab-width) c)
  20406. (catch 'exit
  20407. (while (< (setq n (1+ n)) (length s))
  20408. (setq c (aref s n))
  20409. (cond ((= c ?\ ) (setq i (1+ i)))
  20410. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  20411. (t (throw 'exit t)))))
  20412. i))
  20413. (defun org-remove-tabs (s &optional width)
  20414. "Replace tabulators in S with spaces.
  20415. Assumes that s is a single line, starting in column 0."
  20416. (setq width (or width tab-width))
  20417. (while (string-match "\t" s)
  20418. (setq s (replace-match
  20419. (make-string
  20420. (- (* width (/ (+ (match-beginning 0) width) width))
  20421. (match-beginning 0)) ?\ )
  20422. t t s)))
  20423. s)
  20424. (defun org-fix-indentation (line ind)
  20425. "Fix indentation in LINE.
  20426. IND is a cons cell with target and minimum indentation.
  20427. If the current indentation in LINE is smaller than the minimum,
  20428. leave it alone. If it is larger than ind, set it to the target."
  20429. (let* ((l (org-remove-tabs line))
  20430. (i (org-get-indentation l))
  20431. (i1 (car ind)) (i2 (cdr ind)))
  20432. (when (>= i i2) (setq l (substring line i2)))
  20433. (if (> i1 0)
  20434. (concat (make-string i1 ?\ ) l)
  20435. l)))
  20436. (defun org-remove-indentation (code &optional n)
  20437. "Remove maximum common indentation in string CODE and return it.
  20438. N may optionally be the number of columns to remove. Return CODE
  20439. as-is if removal failed."
  20440. (with-temp-buffer
  20441. (insert code)
  20442. (if (org-do-remove-indentation n) (buffer-string) code)))
  20443. (defun org-do-remove-indentation (&optional n)
  20444. "Remove the maximum common indentation from the buffer.
  20445. When optional argument N is a positive integer, remove exactly
  20446. that much characters from indentation, if possible. Return nil
  20447. if it fails."
  20448. (catch :exit
  20449. (goto-char (point-min))
  20450. ;; Find maximum common indentation, if not specified.
  20451. (let ((n (or n
  20452. (let ((min-ind (point-max)))
  20453. (save-excursion
  20454. (while (re-search-forward "^[ \t]*\\S-" nil t)
  20455. (let ((ind (1- (current-column))))
  20456. (if (zerop ind) (throw :exit nil)
  20457. (setq min-ind (min min-ind ind))))))
  20458. min-ind))))
  20459. (if (zerop n) (throw :exit nil)
  20460. ;; Remove exactly N indentation, but give up if not possible.
  20461. (while (not (eobp))
  20462. (let ((ind (progn (skip-chars-forward " \t") (current-column))))
  20463. (cond ((eolp) (delete-region (line-beginning-position) (point)))
  20464. ((< ind n) (throw :exit nil))
  20465. (t (org-indent-line-to (- ind n))))
  20466. (forward-line)))
  20467. ;; Signal success.
  20468. t))))
  20469. (defun org-fill-template (template alist)
  20470. "Find each %key of ALIST in TEMPLATE and replace it."
  20471. (let ((case-fold-search nil))
  20472. (dolist (entry (sort (copy-sequence alist)
  20473. (lambda (a b) (< (length (car a)) (length (car b))))))
  20474. (setq template
  20475. (replace-regexp-in-string
  20476. (concat "%" (regexp-quote (car entry)))
  20477. (or (cdr entry) "") template t t)))
  20478. template))
  20479. (defun org-base-buffer (buffer)
  20480. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  20481. (if (not buffer)
  20482. buffer
  20483. (or (buffer-base-buffer buffer)
  20484. buffer)))
  20485. (defun org-wrap (string &optional width lines)
  20486. "Wrap string to either a number of lines, or a width in characters.
  20487. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  20488. that costs. If there is a word longer than WIDTH, the text is actually
  20489. wrapped to the length of that word.
  20490. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  20491. many lines, whatever width that takes.
  20492. The return value is a list of lines, without newlines at the end."
  20493. (let* ((words (org-split-string string "[ \t\n]+"))
  20494. (maxword (apply 'max (mapcar 'org-string-width words)))
  20495. w ll)
  20496. (cond (width
  20497. (org-do-wrap words (max maxword width)))
  20498. (lines
  20499. (setq w maxword)
  20500. (setq ll (org-do-wrap words maxword))
  20501. (if (<= (length ll) lines)
  20502. ll
  20503. (setq ll words)
  20504. (while (> (length ll) lines)
  20505. (setq w (1+ w))
  20506. (setq ll (org-do-wrap words w)))
  20507. ll))
  20508. (t (error "Cannot wrap this")))))
  20509. (defun org-do-wrap (words width)
  20510. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  20511. (let (lines line)
  20512. (while words
  20513. (setq line (pop words))
  20514. (while (and words (< (+ (length line) (length (car words))) width))
  20515. (setq line (concat line " " (pop words))))
  20516. (setq lines (push line lines)))
  20517. (nreverse lines)))
  20518. (defun org-split-string (string &optional separators)
  20519. "Splits STRING into substrings at SEPARATORS.
  20520. SEPARATORS is a regular expression.
  20521. No empty strings are returned if there are matches at the beginning
  20522. and end of string."
  20523. ;; FIXME: why not use (split-string STRING SEPARATORS t)?
  20524. (let ((start 0) notfirst list)
  20525. (while (and (string-match (or separators "[ \f\t\n\r\v]+") string
  20526. (if (and notfirst
  20527. (= start (match-beginning 0))
  20528. (< start (length string)))
  20529. (1+ start) start))
  20530. (< (match-beginning 0) (length string)))
  20531. (setq notfirst t)
  20532. (or (eq (match-beginning 0) 0)
  20533. (and (eq (match-beginning 0) (match-end 0))
  20534. (eq (match-beginning 0) start))
  20535. (push (substring string start (match-beginning 0)) list))
  20536. (setq start (match-end 0)))
  20537. (or (eq start (length string))
  20538. (push (substring string start) list))
  20539. (nreverse list)))
  20540. (defun org-quote-vert (s)
  20541. "Replace \"|\" with \"\\vert\"."
  20542. (while (string-match "|" s)
  20543. (setq s (replace-match "\\vert" t t s)))
  20544. s)
  20545. (defun org-uuidgen-p (s)
  20546. "Is S an ID created by UUIDGEN?"
  20547. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  20548. (defun org-in-src-block-p (&optional inside)
  20549. "Whether point is in a code source block.
  20550. When INSIDE is non-nil, don't consider we are within a src block
  20551. when point is at #+BEGIN_SRC or #+END_SRC."
  20552. (let ((case-fold-search t) ov)
  20553. (or (and (eq (get-char-property (point) 'src-block) t))
  20554. (and (not inside)
  20555. (save-match-data
  20556. (save-excursion
  20557. (beginning-of-line)
  20558. (looking-at ".*#\\+\\(begin\\|end\\)_src")))))))
  20559. (defun org-context ()
  20560. "Return a list of contexts of the current cursor position.
  20561. If several contexts apply, all are returned.
  20562. Each context entry is a list with a symbol naming the context, and
  20563. two positions indicating start and end of the context. Possible
  20564. contexts are:
  20565. :headline anywhere in a headline
  20566. :headline-stars on the leading stars in a headline
  20567. :todo-keyword on a TODO keyword (including DONE) in a headline
  20568. :tags on the TAGS in a headline
  20569. :priority on the priority cookie in a headline
  20570. :item on the first line of a plain list item
  20571. :item-bullet on the bullet/number of a plain list item
  20572. :checkbox on the checkbox in a plain list item
  20573. :table in an org-mode table
  20574. :table-special on a special filed in a table
  20575. :table-table in a table.el table
  20576. :clocktable in a clocktable
  20577. :src-block in a source block
  20578. :link on a hyperlink
  20579. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE, COMMENT.
  20580. :target on a <<target>>
  20581. :radio-target on a <<<radio-target>>>
  20582. :latex-fragment on a LaTeX fragment
  20583. :latex-preview on a LaTeX fragment with overlaid preview image
  20584. This function expects the position to be visible because it uses font-lock
  20585. faces as a help to recognize the following contexts: :table-special, :link,
  20586. and :keyword."
  20587. (let* ((f (get-text-property (point) 'face))
  20588. (faces (if (listp f) f (list f)))
  20589. (case-fold-search t)
  20590. (p (point)) clist o)
  20591. ;; First the large context
  20592. (cond
  20593. ((org-at-heading-p t)
  20594. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  20595. (when (progn
  20596. (beginning-of-line 1)
  20597. (looking-at org-todo-line-tags-regexp))
  20598. (push (org-point-in-group p 1 :headline-stars) clist)
  20599. (push (org-point-in-group p 2 :todo-keyword) clist)
  20600. (push (org-point-in-group p 4 :tags) clist))
  20601. (goto-char p)
  20602. (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
  20603. (when (looking-at "\\[#[A-Z0-9]\\]")
  20604. (push (org-point-in-group p 0 :priority) clist)))
  20605. ((org-at-item-p)
  20606. (push (org-point-in-group p 2 :item-bullet) clist)
  20607. (push (list :item (point-at-bol)
  20608. (save-excursion (org-end-of-item) (point)))
  20609. clist)
  20610. (and (org-at-item-checkbox-p)
  20611. (push (org-point-in-group p 0 :checkbox) clist)))
  20612. ((org-at-table-p)
  20613. (push (list :table (org-table-begin) (org-table-end)) clist)
  20614. (when (memq 'org-formula faces)
  20615. (push (list :table-special
  20616. (previous-single-property-change p 'face)
  20617. (next-single-property-change p 'face)) clist)))
  20618. ((org-at-table-p 'any)
  20619. (push (list :table-table) clist)))
  20620. (goto-char p)
  20621. (let ((case-fold-search t))
  20622. ;; New the "medium" contexts: clocktables, source blocks
  20623. (cond ((org-in-clocktable-p)
  20624. (push (list :clocktable
  20625. (and (or (looking-at "[ \t]*\\(#\\+BEGIN: clocktable\\)")
  20626. (re-search-backward "[ \t]*\\(#+BEGIN: clocktable\\)" nil t))
  20627. (match-beginning 1))
  20628. (and (re-search-forward "[ \t]*#\\+END:?" nil t)
  20629. (match-end 0))) clist))
  20630. ((org-in-src-block-p)
  20631. (push (list :src-block
  20632. (and (or (looking-at "[ \t]*\\(#\\+BEGIN_SRC\\)")
  20633. (re-search-backward "[ \t]*\\(#+BEGIN_SRC\\)" nil t))
  20634. (match-beginning 1))
  20635. (and (search-forward "#+END_SRC" nil t)
  20636. (match-beginning 0))) clist))))
  20637. (goto-char p)
  20638. ;; Now the small context
  20639. (cond
  20640. ((org-at-timestamp-p)
  20641. (push (org-point-in-group p 0 :timestamp) clist))
  20642. ((memq 'org-link faces)
  20643. (push (list :link
  20644. (previous-single-property-change p 'face)
  20645. (next-single-property-change p 'face)) clist))
  20646. ((memq 'org-special-keyword faces)
  20647. (push (list :keyword
  20648. (previous-single-property-change p 'face)
  20649. (next-single-property-change p 'face)) clist))
  20650. ((org-at-target-p)
  20651. (push (org-point-in-group p 0 :target) clist)
  20652. (goto-char (1- (match-beginning 0)))
  20653. (when (looking-at org-radio-target-regexp)
  20654. (push (org-point-in-group p 0 :radio-target) clist))
  20655. (goto-char p))
  20656. ((setq o (car (delq nil
  20657. (mapcar
  20658. (lambda (x)
  20659. (when (memq x org-latex-fragment-image-overlays) x))
  20660. (overlays-at (point))))))
  20661. (push (list :latex-fragment
  20662. (overlay-start o) (overlay-end o)) clist)
  20663. (push (list :latex-preview
  20664. (overlay-start o) (overlay-end o)) clist))
  20665. ((org-inside-LaTeX-fragment-p)
  20666. ;; FIXME: positions wrong.
  20667. (push (list :latex-fragment (point) (point)) clist)))
  20668. (setq clist (nreverse (delq nil clist)))
  20669. clist))
  20670. (defun org-in-regexp (regexp &optional nlines visually)
  20671. "Check if point is inside a match of REGEXP.
  20672. Normally only the current line is checked, but you can include
  20673. NLINES extra lines around point into the search. If VISUALLY is
  20674. set, require that the cursor is not after the match but really
  20675. on, so that the block visually is on the match.
  20676. Return nil or a cons cell (BEG . END) where BEG and END are,
  20677. respectively, the positions at the beginning and the end of the
  20678. match."
  20679. (catch :exit
  20680. (let ((pos (point))
  20681. (eol (line-end-position (if nlines (1+ nlines) 1))))
  20682. (save-excursion
  20683. (beginning-of-line (- 1 (or nlines 0)))
  20684. (while (and (re-search-forward regexp eol t)
  20685. (<= (match-beginning 0) pos))
  20686. (let ((end (match-end 0)))
  20687. (when (or (> end pos) (and (= end pos) (not visually)))
  20688. (throw :exit (cons (match-beginning 0) (match-end 0))))))))))
  20689. (define-obsolete-function-alias 'org-at-regexp-p 'org-in-regexp
  20690. "Org mode 8.3")
  20691. (defun org-between-regexps-p (start-re end-re &optional lim-up lim-down)
  20692. "Non-nil when point is between matches of START-RE and END-RE.
  20693. Also return a non-nil value when point is on one of the matches.
  20694. Optional arguments LIM-UP and LIM-DOWN bound the search; they are
  20695. buffer positions. Default values are the positions of headlines
  20696. surrounding the point.
  20697. The functions returns a cons cell whose car (resp. cdr) is the
  20698. position before START-RE (resp. after END-RE)."
  20699. (save-match-data
  20700. (let ((pos (point))
  20701. (limit-up (or lim-up (save-excursion (outline-previous-heading))))
  20702. (limit-down (or lim-down (save-excursion (outline-next-heading))))
  20703. beg end)
  20704. (save-excursion
  20705. ;; Point is on a block when on START-RE or if START-RE can be
  20706. ;; found before it...
  20707. (and (or (org-in-regexp start-re)
  20708. (re-search-backward start-re limit-up t))
  20709. (setq beg (match-beginning 0))
  20710. ;; ... and END-RE after it...
  20711. (goto-char (match-end 0))
  20712. (re-search-forward end-re limit-down t)
  20713. (> (setq end (match-end 0)) pos)
  20714. ;; ... without another START-RE in-between.
  20715. (goto-char (match-beginning 0))
  20716. (not (re-search-backward start-re (1+ beg) t))
  20717. ;; Return value.
  20718. (cons beg end))))))
  20719. (defun org-in-block-p (names)
  20720. "Non-nil when point belongs to a block whose name belongs to NAMES.
  20721. NAMES is a list of strings containing names of blocks.
  20722. Return first block name matched, or nil. Beware that in case of
  20723. nested blocks, the returned name may not belong to the closest
  20724. block from point."
  20725. (save-match-data
  20726. (catch 'exit
  20727. (let ((case-fold-search t)
  20728. (lim-up (save-excursion (outline-previous-heading)))
  20729. (lim-down (save-excursion (outline-next-heading))))
  20730. (mapc (lambda (name)
  20731. (let ((n (regexp-quote name)))
  20732. (when (org-between-regexps-p
  20733. (concat "^[ \t]*#\\+begin_" n)
  20734. (concat "^[ \t]*#\\+end_" n)
  20735. lim-up lim-down)
  20736. (throw 'exit n))))
  20737. names))
  20738. nil)))
  20739. (defun org-occur-in-agenda-files (regexp &optional _nlines)
  20740. "Call `multi-occur' with buffers for all agenda files."
  20741. (interactive "sOrg-files matching: ")
  20742. (let* ((files (org-agenda-files))
  20743. (tnames (mapcar #'file-truename files))
  20744. (extra org-agenda-text-search-extra-files))
  20745. (when (eq (car extra) 'agenda-archives)
  20746. (setq extra (cdr extra))
  20747. (setq files (org-add-archive-files files)))
  20748. (dolist (f extra)
  20749. (unless (member (file-truename f) tnames)
  20750. (unless (member f files) (setq files (append files (list f))))
  20751. (setq tnames (append tnames (list (file-truename f))))))
  20752. (multi-occur
  20753. (mapcar (lambda (x)
  20754. (with-current-buffer
  20755. ;; FIXME: Why not just (find-file-noselect x)?
  20756. ;; Is it to avoid the "revert buffer" prompt?
  20757. (or (get-file-buffer x) (find-file-noselect x))
  20758. (widen)
  20759. (current-buffer)))
  20760. files)
  20761. regexp)))
  20762. (if (boundp 'occur-mode-find-occurrence-hook)
  20763. ;; Emacs 23
  20764. (add-hook 'occur-mode-find-occurrence-hook
  20765. (lambda ()
  20766. (when (derived-mode-p 'org-mode)
  20767. (org-reveal))))
  20768. ;; Emacs 22
  20769. (defadvice occur-mode-goto-occurrence
  20770. (after org-occur-reveal activate)
  20771. (and (derived-mode-p 'org-mode) (org-reveal)))
  20772. (defadvice occur-mode-goto-occurrence-other-window
  20773. (after org-occur-reveal activate)
  20774. (and (derived-mode-p 'org-mode) (org-reveal)))
  20775. (defadvice occur-mode-display-occurrence
  20776. (after org-occur-reveal activate)
  20777. (when (derived-mode-p 'org-mode)
  20778. (let ((pos (occur-mode-find-occurrence)))
  20779. (with-current-buffer (marker-buffer pos)
  20780. (save-excursion
  20781. (goto-char pos)
  20782. (org-reveal)))))))
  20783. (defun org-occur-link-in-agenda-files ()
  20784. "Create a link and search for it in the agendas.
  20785. The link is not stored in `org-stored-links', it is just created
  20786. for the search purpose."
  20787. (interactive)
  20788. (let ((link (condition-case nil
  20789. (org-store-link nil)
  20790. (error "Unable to create a link to here"))))
  20791. (org-occur-in-agenda-files (regexp-quote link))))
  20792. (defun org-reverse-string (string)
  20793. "Return the reverse of STRING."
  20794. (apply 'string (reverse (string-to-list string))))
  20795. ;; defsubst org-uniquify must be defined before first use
  20796. (defun org-uniquify-alist (alist)
  20797. "Merge elements of ALIST with the same key.
  20798. For example, in this alist:
  20799. \(org-uniquify-alist \\='((a 1) (b 2) (a 3)))
  20800. => \\='((a 1 3) (b 2))
  20801. merge (a 1) and (a 3) into (a 1 3).
  20802. The function returns the new ALIST."
  20803. (let (rtn)
  20804. (mapc
  20805. (lambda (e)
  20806. (let (n)
  20807. (if (not (assoc (car e) rtn))
  20808. (push e rtn)
  20809. (setq n (cons (car e) (append (cdr (assoc (car e) rtn)) (cdr e))))
  20810. (setq rtn (assq-delete-all (car e) rtn))
  20811. (push n rtn))))
  20812. alist)
  20813. rtn))
  20814. (defun org-delete-all (elts list)
  20815. "Remove all elements in ELTS from LIST."
  20816. (while elts
  20817. (setq list (delete (pop elts) list)))
  20818. list)
  20819. (defun org-back-over-empty-lines ()
  20820. "Move backwards over whitespace, to the beginning of the first empty line.
  20821. Returns the number of empty lines passed."
  20822. (let ((pos (point)))
  20823. (if (cdr (assoc 'heading org-blank-before-new-entry))
  20824. (skip-chars-backward " \t\n\r")
  20825. (unless (eobp)
  20826. (forward-line -1)))
  20827. (beginning-of-line 2)
  20828. (goto-char (min (point) pos))
  20829. (count-lines (point) pos)))
  20830. (defun org-skip-whitespace ()
  20831. (skip-chars-forward " \t\n\r"))
  20832. (defun org-point-in-group (point group &optional context)
  20833. "Check if POINT is in match-group GROUP.
  20834. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  20835. match. If the match group does not exist or point is not inside it,
  20836. return nil."
  20837. (and (match-beginning group)
  20838. (>= point (match-beginning group))
  20839. (<= point (match-end group))
  20840. (if context
  20841. (list context (match-beginning group) (match-end group))
  20842. t)))
  20843. (defun org-switch-to-buffer-other-window (&rest args)
  20844. "Switch to buffer in a second window on the current frame.
  20845. In particular, do not allow pop-up frames.
  20846. Returns the newly created buffer."
  20847. (org-no-popups
  20848. (apply 'switch-to-buffer-other-window args)))
  20849. (defun org-combine-plists (&rest plists)
  20850. "Create a single property list from all plists in PLISTS.
  20851. The process starts by copying the first list, and then setting properties
  20852. from the other lists. Settings in the last list are the most significant
  20853. ones and overrule settings in the other lists."
  20854. (let ((rtn (copy-sequence (pop plists)))
  20855. p v ls)
  20856. (while plists
  20857. (setq ls (pop plists))
  20858. (while ls
  20859. (setq p (pop ls) v (pop ls))
  20860. (setq rtn (plist-put rtn p v))))
  20861. rtn))
  20862. (defun org-replace-escapes (string table)
  20863. "Replace %-escapes in STRING with values in TABLE.
  20864. TABLE is an association list with keys like \"%a\" and string values.
  20865. The sequences in STRING may contain normal field width and padding information,
  20866. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  20867. so values can contain further %-escapes if they are define later in TABLE."
  20868. (let ((tbl (copy-alist table))
  20869. (case-fold-search nil)
  20870. (pchg 0)
  20871. e re rpl)
  20872. (dolist (e tbl)
  20873. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  20874. (when (and (cdr e) (string-match re (cdr e)))
  20875. (let ((sref (substring (cdr e) (match-beginning 0) (match-end 0)))
  20876. (safe "SREF"))
  20877. (add-text-properties 0 3 (list 'sref sref) safe)
  20878. (setcdr e (replace-match safe t t (cdr e)))))
  20879. (while (string-match re string)
  20880. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  20881. (cdr e)))
  20882. (setq string (replace-match rpl t t string))))
  20883. (while (setq pchg (next-property-change pchg string))
  20884. (let ((sref (get-text-property pchg 'sref string)))
  20885. (when (and sref (string-match "SREF" string pchg))
  20886. (setq string (replace-match sref t t string)))))
  20887. string))
  20888. (defun org-sublist (list start end)
  20889. "Return a section of LIST, from START to END.
  20890. Counting starts at 1."
  20891. (cl-subseq list (1- start) end))
  20892. (make-obsolete 'org-sublist "cl-subseq (note the 0-based counting)." "Org 9.0")
  20893. (defun org-find-base-buffer-visiting (file)
  20894. "Like `find-buffer-visiting' but always return the base buffer and
  20895. not an indirect buffer."
  20896. (let ((buf (or (get-file-buffer file)
  20897. (find-buffer-visiting file))))
  20898. (if buf
  20899. (or (buffer-base-buffer buf) buf)
  20900. nil)))
  20901. (define-obsolete-function-alias 'org-image-file-name-regexp 'image-file-name-regexp
  20902. "Org 9.0")
  20903. ;;; TODO: Only called once, from ox-odt which should probably use
  20904. ;;; org-export-inline-image-p or something.
  20905. (defun org-file-image-p (file)
  20906. "Return non-nil if FILE is an image."
  20907. (save-match-data
  20908. (string-match (image-file-name-regexp) file)))
  20909. (defun org-get-cursor-date (&optional with-time)
  20910. "Return the date at cursor in as a time.
  20911. This works in the calendar and in the agenda, anywhere else it just
  20912. returns the current time.
  20913. If WITH-TIME is non-nil, returns the time of the event at point (in
  20914. the agenda) or the current time of the day."
  20915. (let (date day defd tp hod mod)
  20916. (when with-time
  20917. (setq tp (get-text-property (point) 'time))
  20918. (when (and tp (string-match "\\([0-9][0-9]\\):\\([0-9][0-9]\\)" tp))
  20919. (setq hod (string-to-number (match-string 1 tp))
  20920. mod (string-to-number (match-string 2 tp))))
  20921. (or tp (let ((now (decode-time)))
  20922. (setq hod (nth 2 now)
  20923. mod (nth 1 now)))))
  20924. (cond
  20925. ((eq major-mode 'calendar-mode)
  20926. (setq date (calendar-cursor-to-date)
  20927. defd (encode-time 0 (or mod 0) (or hod 0)
  20928. (nth 1 date) (nth 0 date) (nth 2 date))))
  20929. ((eq major-mode 'org-agenda-mode)
  20930. (setq day (get-text-property (point) 'day))
  20931. (when day
  20932. (setq date (calendar-gregorian-from-absolute day)
  20933. defd (encode-time 0 (or mod 0) (or hod 0)
  20934. (nth 1 date) (nth 0 date) (nth 2 date))))))
  20935. (or defd (current-time))))
  20936. (defun org-mark-subtree (&optional up)
  20937. "Mark the current subtree.
  20938. This puts point at the start of the current subtree, and mark at
  20939. the end. If a numeric prefix UP is given, move up into the
  20940. hierarchy of headlines by UP levels before marking the subtree."
  20941. (interactive "P")
  20942. (org-with-limited-levels
  20943. (cond ((org-at-heading-p) (beginning-of-line))
  20944. ((org-before-first-heading-p) (user-error "Not in a subtree"))
  20945. (t (outline-previous-visible-heading 1))))
  20946. (when up (while (and (> up 0) (org-up-heading-safe)) (decf up)))
  20947. (if (org-called-interactively-p 'any)
  20948. (call-interactively 'org-mark-element)
  20949. (org-mark-element)))
  20950. ;;; Indentation
  20951. (defun org--get-expected-indentation (element contentsp)
  20952. "Expected indentation column for current line, according to ELEMENT.
  20953. ELEMENT is an element containing point. CONTENTSP is non-nil
  20954. when indentation is to be computed according to contents of
  20955. ELEMENT."
  20956. (let ((type (org-element-type element))
  20957. (start (org-element-property :begin element))
  20958. (post-affiliated (org-element-property :post-affiliated element)))
  20959. (org-with-wide-buffer
  20960. (cond
  20961. (contentsp
  20962. (case type
  20963. ((diary-sexp footnote-definition) 0)
  20964. ((headline inlinetask nil)
  20965. (if (not org-adapt-indentation) 0
  20966. (let ((level (org-current-level)))
  20967. (if level (1+ level) 0))))
  20968. ((item plain-list) (org-list-item-body-column post-affiliated))
  20969. (t
  20970. (goto-char start)
  20971. (org-get-indentation))))
  20972. ((memq type '(headline inlinetask nil))
  20973. (if (save-excursion (beginning-of-line) (looking-at "[ \t]*$"))
  20974. (org--get-expected-indentation element t)
  20975. 0))
  20976. ((memq type '(diary-sexp footnote-definition)) 0)
  20977. ;; First paragraph of a footnote definition or an item.
  20978. ;; Indent like parent.
  20979. ((< (line-beginning-position) start)
  20980. (org--get-expected-indentation
  20981. (org-element-property :parent element) t))
  20982. ;; At first line: indent according to previous sibling, if any,
  20983. ;; ignoring footnote definitions and inline tasks, or parent's
  20984. ;; contents.
  20985. ((= (line-beginning-position) start)
  20986. (catch 'exit
  20987. (while t
  20988. (if (= (point-min) start) (throw 'exit 0)
  20989. (goto-char (1- start))
  20990. (let* ((previous (org-element-at-point))
  20991. (parent previous))
  20992. (while (and parent (<= (org-element-property :end parent) start))
  20993. (setq previous parent
  20994. parent (org-element-property :parent parent)))
  20995. (cond
  20996. ((not previous) (throw 'exit 0))
  20997. ((> (org-element-property :end previous) start)
  20998. (throw 'exit (org--get-expected-indentation previous t)))
  20999. ((memq (org-element-type previous)
  21000. '(footnote-definition inlinetask))
  21001. (setq start (org-element-property :begin previous)))
  21002. (t (goto-char (org-element-property :begin previous))
  21003. (throw 'exit
  21004. (if (bolp) (org-get-indentation)
  21005. ;; At first paragraph in an item or
  21006. ;; a footnote definition.
  21007. (org--get-expected-indentation
  21008. (org-element-property :parent previous) t))))))))))
  21009. ;; Otherwise, move to the first non-blank line above.
  21010. (t
  21011. (beginning-of-line)
  21012. (let ((pos (point)))
  21013. (skip-chars-backward " \r\t\n")
  21014. (cond
  21015. ;; Two blank lines end a footnote definition or a plain
  21016. ;; list. When we indent an empty line after them, the
  21017. ;; containing list or footnote definition is over, so it
  21018. ;; qualifies as a previous sibling. Therefore, we indent
  21019. ;; like its first line.
  21020. ((and (memq type '(footnote-definition plain-list))
  21021. (> (count-lines (point) pos) 2))
  21022. (goto-char start)
  21023. (org-get-indentation))
  21024. ;; Line above is the first one of a paragraph at the
  21025. ;; beginning of an item or a footnote definition. Indent
  21026. ;; like parent.
  21027. ((< (line-beginning-position) start)
  21028. (org--get-expected-indentation
  21029. (org-element-property :parent element) t))
  21030. ;; Line above is the beginning of an element, i.e., point
  21031. ;; was originally on the blank lines between element's start
  21032. ;; and contents.
  21033. ((= (line-beginning-position) post-affiliated)
  21034. (org--get-expected-indentation element t))
  21035. ;; POS is after contents in a greater element. Indent like
  21036. ;; the beginning of the element.
  21037. ;;
  21038. ;; As a special case, if point is at the end of a footnote
  21039. ;; definition or an item, indent like the very last element
  21040. ;; within.
  21041. ((and (not (eq type 'paragraph))
  21042. (let ((cend (org-element-property :contents-end element)))
  21043. (and cend (<= cend pos))))
  21044. (if (memq type '(footnote-definition item plain-list))
  21045. (let ((last (org-element-at-point)))
  21046. (org--get-expected-indentation
  21047. last
  21048. (memq (org-element-type last)
  21049. '(footnote-definition item plain-list))))
  21050. (goto-char start)
  21051. (org-get-indentation)))
  21052. ;; In any other case, indent like the current line.
  21053. (t (org-get-indentation)))))))))
  21054. (defun org--align-node-property ()
  21055. "Align node property at point.
  21056. Alignment is done according to `org-property-format', which see."
  21057. (when (save-excursion
  21058. (beginning-of-line)
  21059. (looking-at org-property-re))
  21060. (replace-match
  21061. (concat (match-string 4)
  21062. (org-trim
  21063. (format org-property-format (match-string 1) (match-string 3))))
  21064. t t)))
  21065. (defun org-indent-line ()
  21066. "Indent line depending on context.
  21067. Indentation is done according to the following rules:
  21068. - Footnote definitions, diary sexps, headlines and inline tasks
  21069. have to start at column 0.
  21070. - On the very first line of an element, consider, in order, the
  21071. next rules until one matches:
  21072. 1. If there's a sibling element before, ignoring footnote
  21073. definitions and inline tasks, indent like its first line.
  21074. 2. If element has a parent, indent like its contents. More
  21075. precisely, if parent is an item, indent after the
  21076. description part, if any, or the bullet (see
  21077. `org-list-description-max-indent'). Else, indent like
  21078. parent's first line.
  21079. 3. Otherwise, indent relatively to current level, if
  21080. `org-adapt-indentation' is non-nil, or to left margin.
  21081. - On a blank line at the end of an element, indent according to
  21082. the type of the element. More precisely
  21083. 1. If element is a plain list, an item, or a footnote
  21084. definition, indent like the very last element within.
  21085. 2. If element is a paragraph, indent like its last non blank
  21086. line.
  21087. 3. Otherwise, indent like its very first line.
  21088. - In the code part of a source block, use language major mode
  21089. to indent current line if `org-src-tab-acts-natively' is
  21090. non-nil. If it is nil, do nothing.
  21091. - Otherwise, indent like the first non-blank line above.
  21092. The function doesn't indent an item as it could break the whole
  21093. list structure. Instead, use \\<org-mode-map>\\[org-shiftmetaleft] or \
  21094. \\[org-shiftmetaright].
  21095. Also align node properties according to `org-property-format'."
  21096. (interactive)
  21097. (cond
  21098. (orgstruct-is-++
  21099. (let ((indent-line-function
  21100. (cadadr (assq 'indent-line-function org-fb-vars))))
  21101. (indent-according-to-mode)))
  21102. ((org-at-heading-p) 'noindent)
  21103. (t
  21104. (let* ((element (save-excursion (beginning-of-line) (org-element-at-point)))
  21105. (type (org-element-type element)))
  21106. (cond ((and (memq type '(plain-list item))
  21107. (= (line-beginning-position)
  21108. (org-element-property :post-affiliated element)))
  21109. 'noindent)
  21110. ((and (eq type 'src-block)
  21111. org-src-tab-acts-natively
  21112. (> (line-beginning-position)
  21113. (org-element-property :post-affiliated element))
  21114. (< (line-beginning-position)
  21115. (org-with-wide-buffer
  21116. (goto-char (org-element-property :end element))
  21117. (skip-chars-backward " \r\t\n")
  21118. (line-beginning-position))))
  21119. (org-babel-do-key-sequence-in-edit-buffer (kbd "TAB")))
  21120. (t
  21121. (let ((column (org--get-expected-indentation element nil)))
  21122. ;; Preserve current column.
  21123. (if (<= (current-column) (current-indentation))
  21124. (org-indent-line-to column)
  21125. (save-excursion (org-indent-line-to column))))
  21126. ;; Align node property. Also preserve current column.
  21127. (when (eq type 'node-property)
  21128. (let ((column (current-column)))
  21129. (org--align-node-property)
  21130. (org-move-to-column column)))))))))
  21131. (defun org-indent-region (start end)
  21132. "Indent each non-blank line in the region.
  21133. Called from a program, START and END specify the region to
  21134. indent. The function will not indent contents of example blocks,
  21135. verse blocks and export blocks as leading white spaces are
  21136. assumed to be significant there."
  21137. (interactive "r")
  21138. (save-excursion
  21139. (goto-char start)
  21140. (skip-chars-forward " \r\t\n")
  21141. (unless (eobp) (beginning-of-line))
  21142. (let ((indent-to
  21143. (lambda (ind pos)
  21144. ;; Set IND as indentation for all lines between point and
  21145. ;; POS or END, whichever comes first. Blank lines are
  21146. ;; ignored. Leave point after POS once done.
  21147. (let ((limit (copy-marker (min end pos))))
  21148. (while (< (point) limit)
  21149. (unless (org-looking-at-p "[ \t]*$") (org-indent-line-to ind))
  21150. (forward-line))
  21151. (set-marker limit nil))))
  21152. (end (copy-marker end)))
  21153. (while (< (point) end)
  21154. (if (or (org-looking-at-p " \r\t\n") (org-at-heading-p)) (forward-line)
  21155. (let* ((element (org-element-at-point))
  21156. (type (org-element-type element))
  21157. (element-end (copy-marker (org-element-property :end element)))
  21158. (ind (org--get-expected-indentation element nil)))
  21159. (cond
  21160. ((or (memq type '(paragraph table table-row))
  21161. (not (or (org-element-property :contents-begin element)
  21162. (memq type
  21163. '(example-block export-block src-block)))))
  21164. ;; Elements here are indented as a single block. Also
  21165. ;; align node properties.
  21166. (when (eq type 'node-property)
  21167. (org--align-node-property)
  21168. (beginning-of-line))
  21169. (funcall indent-to ind element-end))
  21170. (t
  21171. ;; Elements in this category consist of three parts:
  21172. ;; before the contents, the contents, and after the
  21173. ;; contents. The contents are treated specially,
  21174. ;; according to the element type, or not indented at
  21175. ;; all. Other parts are indented as a single block.
  21176. (let* ((post (copy-marker
  21177. (org-element-property :post-affiliated element)))
  21178. (cbeg
  21179. (copy-marker
  21180. (cond
  21181. ((not (org-element-property :contents-begin element))
  21182. ;; Fake contents for source blocks.
  21183. (org-with-wide-buffer
  21184. (goto-char post)
  21185. (forward-line)
  21186. (point)))
  21187. ((memq type '(footnote-definition item plain-list))
  21188. ;; Contents in these elements could start on
  21189. ;; the same line as the beginning of the
  21190. ;; element. Make sure we start indenting
  21191. ;; from the second line.
  21192. (org-with-wide-buffer
  21193. (goto-char post)
  21194. (end-of-line)
  21195. (skip-chars-forward " \r\t\n")
  21196. (if (eobp) (point) (line-beginning-position))))
  21197. (t (org-element-property :contents-begin element)))))
  21198. (cend (copy-marker
  21199. (or (org-element-property :contents-end element)
  21200. ;; Fake contents for source blocks.
  21201. (org-with-wide-buffer
  21202. (goto-char element-end)
  21203. (skip-chars-backward " \r\t\n")
  21204. (line-beginning-position)))
  21205. t)))
  21206. ;; Do not change items indentation individually as it
  21207. ;; might break the list as a whole. On the other
  21208. ;; hand, when at a plain list, indent it as a whole.
  21209. (cond ((eq type 'plain-list)
  21210. (let ((offset (- ind (org-get-indentation))))
  21211. (unless (zerop offset)
  21212. (indent-rigidly (org-element-property :begin element)
  21213. (org-element-property :end element)
  21214. offset))
  21215. (goto-char cbeg)))
  21216. ((eq type 'item) (goto-char cbeg))
  21217. (t (funcall indent-to ind cbeg)))
  21218. (when (< (point) end)
  21219. (case type
  21220. ((example-block export-block verse-block))
  21221. (src-block
  21222. ;; In a source block, indent source code
  21223. ;; according to language major mode, but only if
  21224. ;; `org-src-tab-acts-natively' is non-nil.
  21225. (when (and (< (point) end) org-src-tab-acts-natively)
  21226. (ignore-errors
  21227. (org-babel-do-in-edit-buffer
  21228. (indent-region (point-min) (point-max))))))
  21229. (t (org-indent-region (point) (min cend end))))
  21230. (goto-char (min cend end))
  21231. (when (< (point) end) (funcall indent-to ind element-end)))
  21232. (set-marker post nil)
  21233. (set-marker cbeg nil)
  21234. (set-marker cend nil))))
  21235. (set-marker element-end nil))))
  21236. (set-marker end nil))))
  21237. (defun org-indent-drawer ()
  21238. "Indent the drawer at point."
  21239. (interactive)
  21240. (unless (save-excursion
  21241. (beginning-of-line)
  21242. (org-looking-at-p org-drawer-regexp))
  21243. (user-error "Not at a drawer"))
  21244. (let ((element (org-element-at-point)))
  21245. (unless (memq (org-element-type element) '(drawer property-drawer))
  21246. (user-error "Not at a drawer"))
  21247. (org-with-wide-buffer
  21248. (org-indent-region (org-element-property :begin element)
  21249. (org-element-property :end element))))
  21250. (message "Drawer at point indented"))
  21251. (defun org-indent-block ()
  21252. "Indent the block at point."
  21253. (interactive)
  21254. (unless (save-excursion
  21255. (beginning-of-line)
  21256. (let ((case-fold-search t))
  21257. (org-looking-at-p "[ \t]*#\\+\\(begin\\|end\\)_")))
  21258. (user-error "Not at a block"))
  21259. (let ((element (org-element-at-point)))
  21260. (unless (memq (org-element-type element)
  21261. '(comment-block center-block dynamic-block example-block
  21262. export-block quote-block special-block
  21263. src-block verse-block))
  21264. (user-error "Not at a block"))
  21265. (org-with-wide-buffer
  21266. (org-indent-region (org-element-property :begin element)
  21267. (org-element-property :end element))))
  21268. (message "Block at point indented"))
  21269. ;;; Filling
  21270. ;; We use our own fill-paragraph and auto-fill functions.
  21271. ;; `org-fill-paragraph' relies on adaptive filling and context
  21272. ;; checking. Appropriate `fill-prefix' is computed with
  21273. ;; `org-adaptive-fill-function'.
  21274. ;; `org-auto-fill-function' takes care of auto-filling. It calls
  21275. ;; `do-auto-fill' only on valid areas with `fill-prefix' shadowed with
  21276. ;; `org-adaptive-fill-function' value. Internally,
  21277. ;; `org-comment-line-break-function' breaks the line.
  21278. ;; `org-setup-filling' installs filling and auto-filling related
  21279. ;; variables during `org-mode' initialization.
  21280. (defvar org-element-paragraph-separate) ; org-element.el
  21281. (defun org-setup-filling ()
  21282. (require 'org-element)
  21283. ;; Prevent auto-fill from inserting unwanted new items.
  21284. (when (boundp 'fill-nobreak-predicate)
  21285. (setq-local
  21286. fill-nobreak-predicate
  21287. (org-uniquify
  21288. (append fill-nobreak-predicate
  21289. '(org-fill-line-break-nobreak-p
  21290. org-fill-paragraph-with-timestamp-nobreak-p)))))
  21291. (let ((paragraph-ending (substring org-element-paragraph-separate 1)))
  21292. (setq-local paragraph-start paragraph-ending)
  21293. (setq-local paragraph-separate paragraph-ending))
  21294. (setq-local fill-paragraph-function 'org-fill-paragraph)
  21295. (setq-local auto-fill-inhibit-regexp nil)
  21296. (setq-local adaptive-fill-function 'org-adaptive-fill-function)
  21297. (setq-local normal-auto-fill-function 'org-auto-fill-function)
  21298. (setq-local comment-line-break-function 'org-comment-line-break-function))
  21299. (defun org-fill-line-break-nobreak-p ()
  21300. "Non-nil when a new line at point would create an Org line break."
  21301. (save-excursion
  21302. (skip-chars-backward "[ \t]")
  21303. (skip-chars-backward "\\\\")
  21304. (looking-at "\\\\\\\\\\($\\|[^\\\\]\\)")))
  21305. (defun org-fill-paragraph-with-timestamp-nobreak-p ()
  21306. "Non-nil when a new line at point would split a timestamp."
  21307. (and (org-at-timestamp-p t)
  21308. (not (looking-at org-ts-regexp-both))))
  21309. (declare-function message-in-body-p "message" ())
  21310. (defvar orgtbl-line-start-regexp) ; From org-table.el
  21311. (defun org-adaptive-fill-function ()
  21312. "Compute a fill prefix for the current line.
  21313. Return fill prefix, as a string, or nil if current line isn't
  21314. meant to be filled. For convenience, if `adaptive-fill-regexp'
  21315. matches in paragraphs or comments, use it."
  21316. (catch 'exit
  21317. (when (derived-mode-p 'message-mode)
  21318. (save-excursion
  21319. (beginning-of-line)
  21320. (cond ((not (message-in-body-p)) (throw 'exit nil))
  21321. ((org-looking-at-p org-table-line-regexp) (throw 'exit nil))
  21322. ((looking-at message-cite-prefix-regexp)
  21323. (throw 'exit (match-string-no-properties 0)))
  21324. ((looking-at org-outline-regexp)
  21325. (throw 'exit (make-string (length (match-string 0)) ?\s))))))
  21326. (org-with-wide-buffer
  21327. (unless (org-at-heading-p)
  21328. (let* ((p (line-beginning-position))
  21329. (element (save-excursion
  21330. (beginning-of-line)
  21331. (org-element-at-point)))
  21332. (type (org-element-type element))
  21333. (post-affiliated (org-element-property :post-affiliated element)))
  21334. (unless (< p post-affiliated)
  21335. (case type
  21336. (comment
  21337. (save-excursion
  21338. (beginning-of-line)
  21339. (looking-at "[ \t]*")
  21340. (concat (match-string 0) "# ")))
  21341. (footnote-definition "")
  21342. ((item plain-list)
  21343. (make-string (org-list-item-body-column post-affiliated) ?\s))
  21344. (paragraph
  21345. ;; Fill prefix is usually the same as the current line,
  21346. ;; unless the paragraph is at the beginning of an item.
  21347. (let ((parent (org-element-property :parent element)))
  21348. (save-excursion
  21349. (beginning-of-line)
  21350. (cond ((eq (org-element-type parent) 'item)
  21351. (make-string (org-list-item-body-column
  21352. (org-element-property :begin parent))
  21353. ?\s))
  21354. ((and adaptive-fill-regexp
  21355. ;; Locally disable
  21356. ;; `adaptive-fill-function' to let
  21357. ;; `fill-context-prefix' handle
  21358. ;; `adaptive-fill-regexp' variable.
  21359. (let (adaptive-fill-function)
  21360. (fill-context-prefix
  21361. post-affiliated
  21362. (org-element-property :end element)))))
  21363. ((looking-at "[ \t]+") (match-string 0))
  21364. (t "")))))
  21365. (comment-block
  21366. ;; Only fill contents if P is within block boundaries.
  21367. (let* ((cbeg (save-excursion (goto-char post-affiliated)
  21368. (forward-line)
  21369. (point)))
  21370. (cend (save-excursion
  21371. (goto-char (org-element-property :end element))
  21372. (skip-chars-backward " \r\t\n")
  21373. (line-beginning-position))))
  21374. (when (and (>= p cbeg) (< p cend))
  21375. (if (save-excursion (beginning-of-line) (looking-at "[ \t]+"))
  21376. (match-string 0)
  21377. "")))))))))))
  21378. (declare-function message-goto-body "message" ())
  21379. (defvar message-cite-prefix-regexp) ; From message.el
  21380. (defun org-fill-paragraph (&optional justify)
  21381. "Fill element at point, when applicable.
  21382. This function only applies to comment blocks, comments, example
  21383. blocks and paragraphs. Also, as a special case, re-align table
  21384. when point is at one.
  21385. If JUSTIFY is non-nil (interactively, with prefix argument),
  21386. justify as well. If `sentence-end-double-space' is non-nil, then
  21387. period followed by one space does not end a sentence, so don't
  21388. break a line there. The variable `fill-column' controls the
  21389. width for filling.
  21390. For convenience, when point is at a plain list, an item or
  21391. a footnote definition, try to fill the first paragraph within."
  21392. (interactive)
  21393. (if (and (derived-mode-p 'message-mode)
  21394. (or (not (message-in-body-p))
  21395. (save-excursion (move-beginning-of-line 1)
  21396. (looking-at message-cite-prefix-regexp))))
  21397. ;; First ensure filling is correct in message-mode.
  21398. (let ((fill-paragraph-function
  21399. (cadadr (assoc 'fill-paragraph-function org-fb-vars)))
  21400. (fill-prefix (cadadr (assoc 'fill-prefix org-fb-vars)))
  21401. (paragraph-start (cadadr (assoc 'paragraph-start org-fb-vars)))
  21402. (paragraph-separate
  21403. (cadadr (assoc 'paragraph-separate org-fb-vars))))
  21404. (fill-paragraph nil))
  21405. (with-syntax-table org-mode-transpose-word-syntax-table
  21406. ;; Move to end of line in order to get the first paragraph
  21407. ;; within a plain list or a footnote definition.
  21408. (let ((element (save-excursion
  21409. (end-of-line)
  21410. (or (ignore-errors (org-element-at-point))
  21411. (user-error "An element cannot be parsed line %d"
  21412. (line-number-at-pos (point)))))))
  21413. ;; First check if point is in a blank line at the beginning of
  21414. ;; the buffer. In that case, ignore filling.
  21415. (case (org-element-type element)
  21416. ;; Use major mode filling function is src blocks.
  21417. (src-block (org-babel-do-key-sequence-in-edit-buffer (kbd "M-q")))
  21418. ;; Align Org tables, leave table.el tables as-is.
  21419. (table-row (org-table-align) t)
  21420. (table
  21421. (when (eq (org-element-property :type element) 'org)
  21422. (save-excursion
  21423. (goto-char (org-element-property :post-affiliated element))
  21424. (org-table-align)))
  21425. t)
  21426. (paragraph
  21427. ;; Paragraphs may contain `line-break' type objects.
  21428. (let ((beg (max (point-min)
  21429. (org-element-property :contents-begin element)))
  21430. (end (min (point-max)
  21431. (org-element-property :contents-end element))))
  21432. ;; Do nothing if point is at an affiliated keyword.
  21433. (if (< (line-end-position) beg) t
  21434. (when (derived-mode-p 'message-mode)
  21435. ;; In `message-mode', do not fill following citation
  21436. ;; in current paragraph nor text before message body.
  21437. (let ((body-start (save-excursion (message-goto-body))))
  21438. (when body-start (setq beg (max body-start beg))))
  21439. (when (save-excursion
  21440. (re-search-forward
  21441. (concat "^" message-cite-prefix-regexp) end t))
  21442. (setq end (match-beginning 0))))
  21443. ;; Fill paragraph, taking line breaks into account.
  21444. (save-excursion
  21445. (goto-char beg)
  21446. (let ((cuts (list beg)))
  21447. (while (re-search-forward "\\\\\\\\[ \t]*\n" end t)
  21448. (when (eq 'line-break
  21449. (org-element-type
  21450. (save-excursion (backward-char)
  21451. (org-element-context))))
  21452. (push (point) cuts)))
  21453. (dolist (c (delq end cuts))
  21454. (fill-region-as-paragraph c end justify)
  21455. (setq end c))))
  21456. t)))
  21457. ;; Contents of `comment-block' type elements should be
  21458. ;; filled as plain text, but only if point is within block
  21459. ;; markers.
  21460. (comment-block
  21461. (let* ((case-fold-search t)
  21462. (beg (save-excursion
  21463. (goto-char (org-element-property :begin element))
  21464. (re-search-forward "^[ \t]*#\\+begin_comment" nil t)
  21465. (forward-line)
  21466. (point)))
  21467. (end (save-excursion
  21468. (goto-char (org-element-property :end element))
  21469. (re-search-backward "^[ \t]*#\\+end_comment" nil t)
  21470. (line-beginning-position))))
  21471. (if (or (< (point) beg) (> (point) end)) t
  21472. (fill-region-as-paragraph
  21473. (save-excursion (end-of-line)
  21474. (re-search-backward "^[ \t]*$" beg 'move)
  21475. (line-beginning-position))
  21476. (save-excursion (beginning-of-line)
  21477. (re-search-forward "^[ \t]*$" end 'move)
  21478. (line-beginning-position))
  21479. justify))))
  21480. ;; Fill comments.
  21481. (comment
  21482. (let ((begin (org-element-property :post-affiliated element))
  21483. (end (org-element-property :end element)))
  21484. (when (and (>= (point) begin) (<= (point) end))
  21485. (let ((begin (save-excursion
  21486. (end-of-line)
  21487. (if (re-search-backward "^[ \t]*#[ \t]*$" begin t)
  21488. (progn (forward-line) (point))
  21489. begin)))
  21490. (end (save-excursion
  21491. (end-of-line)
  21492. (if (re-search-forward "^[ \t]*#[ \t]*$" end 'move)
  21493. (1- (line-beginning-position))
  21494. (skip-chars-backward " \r\t\n")
  21495. (line-end-position)))))
  21496. ;; Do not fill comments when at a blank line.
  21497. (when (> end begin)
  21498. (let ((fill-prefix
  21499. (save-excursion
  21500. (beginning-of-line)
  21501. (looking-at "[ \t]*#")
  21502. (let ((comment-prefix (match-string 0)))
  21503. (goto-char (match-end 0))
  21504. (if (looking-at adaptive-fill-regexp)
  21505. (concat comment-prefix (match-string 0))
  21506. (concat comment-prefix " "))))))
  21507. (save-excursion
  21508. (fill-region-as-paragraph begin end justify))))))
  21509. t))
  21510. ;; Ignore every other element.
  21511. (otherwise t))))))
  21512. (defun org-auto-fill-function ()
  21513. "Auto-fill function."
  21514. ;; Check if auto-filling is meaningful.
  21515. (let ((fc (current-fill-column)))
  21516. (when (and fc (> (current-column) fc))
  21517. (let* ((fill-prefix (org-adaptive-fill-function))
  21518. ;; Enforce empty fill prefix, if required. Otherwise, it
  21519. ;; will be computed again.
  21520. (adaptive-fill-mode (not (equal fill-prefix ""))))
  21521. (when fill-prefix (do-auto-fill))))))
  21522. (defun org-comment-line-break-function (&optional soft)
  21523. "Break line at point and indent, continuing comment if within one.
  21524. The inserted newline is marked hard if variable
  21525. `use-hard-newlines' is true, unless optional argument SOFT is
  21526. non-nil."
  21527. (if soft (insert-and-inherit ?\n) (newline 1))
  21528. (save-excursion (forward-char -1) (delete-horizontal-space))
  21529. (delete-horizontal-space)
  21530. (indent-to-left-margin)
  21531. (insert-before-markers-and-inherit fill-prefix))
  21532. ;;; Fixed Width Areas
  21533. (defun org-toggle-fixed-width ()
  21534. "Toggle fixed-width markup.
  21535. Add or remove fixed-width markup on current line, whenever it
  21536. makes sense. Return an error otherwise.
  21537. If a region is active and if it contains only fixed-width areas
  21538. or blank lines, remove all fixed-width markup in it. If the
  21539. region contains anything else, convert all non-fixed-width lines
  21540. to fixed-width ones.
  21541. Blank lines at the end of the region are ignored unless the
  21542. region only contains such lines."
  21543. (interactive)
  21544. (if (not (org-region-active-p))
  21545. ;; No region:
  21546. ;;
  21547. ;; Remove fixed width marker only in a fixed-with element.
  21548. ;;
  21549. ;; Add fixed width maker in paragraphs, in blank lines after
  21550. ;; elements or at the beginning of a headline or an inlinetask,
  21551. ;; and before any one-line elements (e.g., a clock).
  21552. (progn
  21553. (beginning-of-line)
  21554. (let* ((element (org-element-at-point))
  21555. (type (org-element-type element)))
  21556. (cond
  21557. ((and (eq type 'fixed-width)
  21558. (looking-at "[ \t]*\\(:\\(?: \\|$\\)\\)"))
  21559. (replace-match
  21560. "" nil nil nil (if (= (line-end-position) (match-end 0)) 0 1)))
  21561. ((and (memq type '(babel-call clock comment diary-sexp headline
  21562. horizontal-rule keyword paragraph
  21563. planning))
  21564. (<= (org-element-property :post-affiliated element) (point)))
  21565. (skip-chars-forward " \t")
  21566. (insert ": "))
  21567. ((and (org-looking-at-p "[ \t]*$")
  21568. (or (eq type 'inlinetask)
  21569. (save-excursion
  21570. (skip-chars-forward " \r\t\n")
  21571. (<= (org-element-property :end element) (point)))))
  21572. (delete-region (point) (line-end-position))
  21573. (org-indent-line)
  21574. (insert ": "))
  21575. (t (user-error "Cannot insert a fixed-width line here")))))
  21576. ;; Region active.
  21577. (let* ((begin (save-excursion
  21578. (goto-char (region-beginning))
  21579. (line-beginning-position)))
  21580. (end (copy-marker
  21581. (save-excursion
  21582. (goto-char (region-end))
  21583. (unless (eolp) (beginning-of-line))
  21584. (if (save-excursion (re-search-backward "\\S-" begin t))
  21585. (progn (skip-chars-backward " \r\t\n") (point))
  21586. (point)))))
  21587. (all-fixed-width-p
  21588. (catch 'not-all-p
  21589. (save-excursion
  21590. (goto-char begin)
  21591. (skip-chars-forward " \r\t\n")
  21592. (when (eobp) (throw 'not-all-p nil))
  21593. (while (< (point) end)
  21594. (let ((element (org-element-at-point)))
  21595. (if (eq (org-element-type element) 'fixed-width)
  21596. (goto-char (org-element-property :end element))
  21597. (throw 'not-all-p nil))))
  21598. t))))
  21599. (if all-fixed-width-p
  21600. (save-excursion
  21601. (goto-char begin)
  21602. (while (< (point) end)
  21603. (when (looking-at "[ \t]*\\(:\\(?: \\|$\\)\\)")
  21604. (replace-match
  21605. "" nil nil nil
  21606. (if (= (line-end-position) (match-end 0)) 0 1)))
  21607. (forward-line)))
  21608. (let ((min-ind (point-max)))
  21609. ;; Find minimum indentation across all lines.
  21610. (save-excursion
  21611. (goto-char begin)
  21612. (if (not (save-excursion (re-search-forward "\\S-" end t)))
  21613. (setq min-ind 0)
  21614. (catch 'zerop
  21615. (while (< (point) end)
  21616. (unless (org-looking-at-p "[ \t]*$")
  21617. (let ((ind (org-get-indentation)))
  21618. (setq min-ind (min min-ind ind))
  21619. (when (zerop ind) (throw 'zerop t))))
  21620. (forward-line)))))
  21621. ;; Loop over all lines and add fixed-width markup everywhere
  21622. ;; but in fixed-width lines.
  21623. (save-excursion
  21624. (goto-char begin)
  21625. (while (< (point) end)
  21626. (cond
  21627. ((org-at-heading-p)
  21628. (insert ": ")
  21629. (forward-line)
  21630. (while (and (< (point) end) (org-looking-at-p "[ \t]*$"))
  21631. (insert ":")
  21632. (forward-line)))
  21633. ((org-looking-at-p "[ \t]*:\\( \\|$\\)")
  21634. (let* ((element (org-element-at-point))
  21635. (element-end (org-element-property :end element)))
  21636. (if (eq (org-element-type element) 'fixed-width)
  21637. (progn (goto-char element-end)
  21638. (skip-chars-backward " \r\t\n")
  21639. (forward-line))
  21640. (let ((limit (min end element-end)))
  21641. (while (< (point) limit)
  21642. (org-move-to-column min-ind t)
  21643. (insert ": ")
  21644. (forward-line))))))
  21645. (t
  21646. (org-move-to-column min-ind t)
  21647. (insert ": ")
  21648. (forward-line)))))))
  21649. (set-marker end nil))))
  21650. ;;; Comments
  21651. ;; Org comments syntax is quite complex. It requires the entire line
  21652. ;; to be just a comment. Also, even with the right syntax at the
  21653. ;; beginning of line, some some elements (i.e. verse-block or
  21654. ;; example-block) don't accept comments. Usual Emacs comment commands
  21655. ;; cannot cope with those requirements. Therefore, Org replaces them.
  21656. ;; Org still relies on `comment-dwim', but cannot trust
  21657. ;; `comment-only-p'. So, `comment-region-function' and
  21658. ;; `uncomment-region-function' both point
  21659. ;; to`org-comment-or-uncomment-region'. Eventually,
  21660. ;; `org-insert-comment' takes care of insertion of comments at the
  21661. ;; beginning of line.
  21662. ;; `org-setup-comments-handling' install comments related variables
  21663. ;; during `org-mode' initialization.
  21664. (defun org-setup-comments-handling ()
  21665. (interactive)
  21666. (setq-local comment-use-syntax nil)
  21667. (setq-local comment-start "# ")
  21668. (setq-local comment-start-skip "^\\s-*#\\(?: \\|$\\)")
  21669. (setq-local comment-insert-comment-function 'org-insert-comment)
  21670. (setq-local comment-region-function 'org-comment-or-uncomment-region)
  21671. (setq-local uncomment-region-function 'org-comment-or-uncomment-region))
  21672. (defun org-insert-comment ()
  21673. "Insert an empty comment above current line.
  21674. If the line is empty, insert comment at its beginning. When
  21675. point is within a source block, comment according to the related
  21676. major mode."
  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. (point))
  21683. (> (save-excursion
  21684. (goto-char (org-element-property :end element))
  21685. (skip-chars-backward " \r\t\n")
  21686. (line-beginning-position))
  21687. (point))))
  21688. (org-babel-do-in-edit-buffer (call-interactively 'comment-dwim))
  21689. (beginning-of-line)
  21690. (if (looking-at "\\s-*$") (delete-region (point) (point-at-eol))
  21691. (open-line 1))
  21692. (org-indent-line)
  21693. (insert "# ")))
  21694. (defvar comment-empty-lines) ; From newcomment.el.
  21695. (defun org-comment-or-uncomment-region (beg end &rest _)
  21696. "Comment or uncomment each non-blank line in the region.
  21697. Uncomment each non-blank line between BEG and END if it only
  21698. contains commented lines. Otherwise, comment them. If region is
  21699. strictly within a source block, use appropriate comment syntax."
  21700. (if (let ((element (org-element-at-point)))
  21701. (and (eq (org-element-type element) 'src-block)
  21702. (< (save-excursion
  21703. (goto-char (org-element-property :post-affiliated element))
  21704. (line-end-position))
  21705. beg)
  21706. (>= (save-excursion
  21707. (goto-char (org-element-property :end element))
  21708. (skip-chars-backward " \r\t\n")
  21709. (line-beginning-position))
  21710. end)))
  21711. (org-babel-do-in-edit-buffer (call-interactively 'comment-dwim))
  21712. (save-restriction
  21713. ;; Restrict region
  21714. (narrow-to-region (save-excursion (goto-char beg)
  21715. (skip-chars-forward " \r\t\n" end)
  21716. (line-beginning-position))
  21717. (save-excursion (goto-char end)
  21718. (skip-chars-backward " \r\t\n" beg)
  21719. (line-end-position)))
  21720. (let ((uncommentp
  21721. ;; UNCOMMENTP is non-nil when every non blank line between
  21722. ;; BEG and END is a comment.
  21723. (save-excursion
  21724. (goto-char (point-min))
  21725. (while (and (not (eobp))
  21726. (let ((element (org-element-at-point)))
  21727. (and (eq (org-element-type element) 'comment)
  21728. (goto-char (min (point-max)
  21729. (org-element-property
  21730. :end element)))))))
  21731. (eobp))))
  21732. (if uncommentp
  21733. ;; Only blank lines and comments in region: uncomment it.
  21734. (save-excursion
  21735. (goto-char (point-min))
  21736. (while (not (eobp))
  21737. (when (looking-at "[ \t]*\\(#\\(?: \\|$\\)\\)")
  21738. (replace-match "" nil nil nil 1))
  21739. (forward-line)))
  21740. ;; Comment each line in region.
  21741. (let ((min-indent (point-max)))
  21742. ;; First find the minimum indentation across all lines.
  21743. (save-excursion
  21744. (goto-char (point-min))
  21745. (while (and (not (eobp)) (not (zerop min-indent)))
  21746. (unless (looking-at "[ \t]*$")
  21747. (setq min-indent (min min-indent (current-indentation))))
  21748. (forward-line)))
  21749. ;; Then loop over all lines.
  21750. (save-excursion
  21751. (goto-char (point-min))
  21752. (while (not (eobp))
  21753. (unless (and (not comment-empty-lines) (looking-at "[ \t]*$"))
  21754. ;; Don't get fooled by invisible text (e.g. link path)
  21755. ;; when moving to column MIN-INDENT.
  21756. (let ((buffer-invisibility-spec nil))
  21757. (org-move-to-column min-indent t))
  21758. (insert comment-start))
  21759. (forward-line)))))))))
  21760. (defun org-comment-dwim (arg)
  21761. "Call `comment-dwim' within a source edit buffer if needed."
  21762. (interactive "*P")
  21763. (if (org-in-src-block-p)
  21764. (org-babel-do-in-edit-buffer (call-interactively 'comment-dwim))
  21765. (call-interactively 'comment-dwim)))
  21766. ;;; Timestamps API
  21767. ;; This section contains tools to operate on timestamp objects, as
  21768. ;; returned by, e.g. `org-element-context'.
  21769. (defun org-timestamp--to-internal-time (timestamp &optional end)
  21770. "Encode TIMESTAMP object into Emacs internal time.
  21771. Use end of date range or time range when END is non-nil."
  21772. (apply #'encode-time
  21773. (cons 0
  21774. (mapcar
  21775. (lambda (prop) (or (org-element-property prop timestamp) 0))
  21776. (if end '(:minute-end :hour-end :day-end :month-end :year-end)
  21777. '(:minute-start :hour-start :day-start :month-start
  21778. :year-start))))))
  21779. (defun org-timestamp-has-time-p (timestamp)
  21780. "Non-nil when TIMESTAMP has a time specified."
  21781. (org-element-property :hour-start timestamp))
  21782. (defun org-timestamp-format (timestamp format &optional end utc)
  21783. "Format a TIMESTAMP object into a string.
  21784. FORMAT is a format specifier to be passed to
  21785. `format-time-string'.
  21786. When optional argument END is non-nil, use end of date-range or
  21787. time-range, if possible.
  21788. When optional argument UTC is non-nil, time will be expressed as
  21789. Universal Time."
  21790. (format-time-string
  21791. format (org-timestamp--to-internal-time timestamp end) utc))
  21792. (defun org-timestamp-split-range (timestamp &optional end)
  21793. "Extract a TIMESTAMP object from a date or time range.
  21794. END, when non-nil, means extract the end of the range.
  21795. Otherwise, extract its start.
  21796. Return a new timestamp object."
  21797. (let ((type (org-element-property :type timestamp)))
  21798. (if (memq type '(active inactive diary)) timestamp
  21799. (let ((split-ts (org-element-copy timestamp)))
  21800. ;; Set new type.
  21801. (org-element-put-property
  21802. split-ts :type (if (eq type 'active-range) 'active 'inactive))
  21803. ;; Copy start properties over end properties if END is
  21804. ;; non-nil. Otherwise, copy end properties over `start' ones.
  21805. (let ((p-alist '((:minute-start . :minute-end)
  21806. (:hour-start . :hour-end)
  21807. (:day-start . :day-end)
  21808. (:month-start . :month-end)
  21809. (:year-start . :year-end))))
  21810. (dolist (p-cell p-alist)
  21811. (org-element-put-property
  21812. split-ts
  21813. (funcall (if end #'car #'cdr) p-cell)
  21814. (org-element-property
  21815. (funcall (if end #'cdr #'car) p-cell) split-ts)))
  21816. ;; Eventually refresh `:raw-value'.
  21817. (org-element-put-property split-ts :raw-value nil)
  21818. (org-element-put-property
  21819. split-ts :raw-value (org-element-interpret-data split-ts)))))))
  21820. (defun org-timestamp-translate (timestamp &optional boundary)
  21821. "Translate TIMESTAMP object to custom format.
  21822. Format string is defined in `org-time-stamp-custom-formats',
  21823. which see.
  21824. When optional argument BOUNDARY is non-nil, it is either the
  21825. symbol `start' or `end'. In this case, only translate the
  21826. starting or ending part of TIMESTAMP if it is a date or time
  21827. range. Otherwise, translate both parts.
  21828. Return timestamp as-is if `org-display-custom-times' is nil or if
  21829. it has a `diary' type."
  21830. (let ((type (org-element-property :type timestamp)))
  21831. (if (or (not org-display-custom-times) (eq type 'diary))
  21832. (org-element-interpret-data timestamp)
  21833. (let ((fmt (funcall (if (org-timestamp-has-time-p timestamp) #'cdr #'car)
  21834. org-time-stamp-custom-formats)))
  21835. (if (and (not boundary) (memq type '(active-range inactive-range)))
  21836. (concat (org-timestamp-format timestamp fmt)
  21837. "--"
  21838. (org-timestamp-format timestamp fmt t))
  21839. (org-timestamp-format timestamp fmt (eq boundary 'end)))))))
  21840. ;;; Other stuff.
  21841. (defvar reftex-docstruct-symbol)
  21842. (defvar reftex-cite-format)
  21843. (defvar org--rds)
  21844. (defun org-reftex-citation ()
  21845. "Use reftex-citation to insert a citation into the buffer.
  21846. This looks for a line like
  21847. #+BIBLIOGRAPHY: foo plain option:-d
  21848. and derives from it that foo.bib is the bibliography file relevant
  21849. for this document. It then installs the necessary environment for RefTeX
  21850. to work in this buffer and calls `reftex-citation' to insert a citation
  21851. into the buffer.
  21852. Export of such citations to both LaTeX and HTML is handled by the contributed
  21853. package ox-bibtex by Taru Karttunen."
  21854. (interactive)
  21855. (let ((reftex-docstruct-symbol 'org--rds)
  21856. (reftex-cite-format "\\cite{%l}")
  21857. org--rds bib)
  21858. (save-excursion
  21859. (save-restriction
  21860. (widen)
  21861. (let ((case-fold-search t)
  21862. (re "^[ \t]*#\\+BIBLIOGRAPHY:[ \t]+\\([^ \t\n]+\\)"))
  21863. (if (not (save-excursion
  21864. (or (re-search-forward re nil t)
  21865. (re-search-backward re nil t))))
  21866. (user-error "No bibliography defined in file")
  21867. (setq bib (concat (match-string 1) ".bib")
  21868. org--rds (list (list 'bib bib)))))))
  21869. (call-interactively 'reftex-citation)))
  21870. ;;;; Functions extending outline functionality
  21871. (defun org-beginning-of-line (&optional arg)
  21872. "Go to the beginning of the current line. If that is invisible, continue
  21873. to a visible line beginning. This makes the function of C-a more intuitive.
  21874. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  21875. first attempt, and only move to after the tags when the cursor is already
  21876. beyond the end of the headline."
  21877. (interactive "P")
  21878. (let ((pos (point))
  21879. (special (if (consp org-special-ctrl-a/e)
  21880. (car org-special-ctrl-a/e)
  21881. org-special-ctrl-a/e))
  21882. deactivate-mark refpos)
  21883. (if (org-bound-and-true-p visual-line-mode)
  21884. (beginning-of-visual-line 1)
  21885. (beginning-of-line 1))
  21886. (if (and arg (fboundp 'move-beginning-of-line))
  21887. (call-interactively 'move-beginning-of-line)
  21888. (unless (bobp)
  21889. (backward-char 1)
  21890. (if (org-truely-invisible-p)
  21891. (while (and (not (bobp)) (org-truely-invisible-p))
  21892. (backward-char 1)
  21893. (beginning-of-line 1))
  21894. (forward-char 1))))
  21895. (when special
  21896. (cond
  21897. ((and (looking-at org-complex-heading-regexp)
  21898. (eq (char-after (match-end 1)) ?\s))
  21899. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  21900. (point-at-eol)))
  21901. (goto-char
  21902. (if (eq special t)
  21903. (cond ((> pos refpos) refpos)
  21904. ((= pos (point)) refpos)
  21905. (t (point)))
  21906. (cond ((> pos (point)) (point))
  21907. ((not (eq last-command this-command)) (point))
  21908. (t refpos)))))
  21909. ((org-at-item-p)
  21910. ;; Being at an item and not looking at an the item means point
  21911. ;; was previously moved to beginning of a visual line, which
  21912. ;; doesn't contain the item. Therefore, do nothing special,
  21913. ;; just stay here.
  21914. (when (looking-at org-list-full-item-re)
  21915. ;; Set special position at first white space character after
  21916. ;; bullet, and check-box, if any.
  21917. (let ((after-bullet
  21918. (let ((box (match-end 3)))
  21919. (if (not box) (match-end 1)
  21920. (let ((after (char-after box)))
  21921. (if (and after (= after ? )) (1+ box) box))))))
  21922. ;; Special case: Move point to special position when
  21923. ;; currently after it or at beginning of line.
  21924. (if (eq special t)
  21925. (when (or (> pos after-bullet) (= (point) pos))
  21926. (goto-char after-bullet))
  21927. ;; Reversed case: Move point to special position when
  21928. ;; point was already at beginning of line and command is
  21929. ;; repeated.
  21930. (when (and (= (point) pos) (eq last-command this-command))
  21931. (goto-char after-bullet))))))))
  21932. (org-no-warnings
  21933. (and (featurep 'xemacs) (setq zmacs-region-stays t))))
  21934. (setq disable-point-adjustment
  21935. (or (not (invisible-p (point)))
  21936. (not (invisible-p (max (point-min) (1- (point))))))))
  21937. (defun org-end-of-line (&optional arg)
  21938. "Go to the end of the line.
  21939. If this is a headline, and `org-special-ctrl-a/e' is set, ignore
  21940. tags on the first attempt, and only move to after the tags when
  21941. the cursor is already beyond the end of the headline."
  21942. (interactive "P")
  21943. (let ((special (if (consp org-special-ctrl-a/e) (cdr org-special-ctrl-a/e)
  21944. org-special-ctrl-a/e))
  21945. (move-fun (cond ((org-bound-and-true-p visual-line-mode)
  21946. 'end-of-visual-line)
  21947. ((fboundp 'move-end-of-line) 'move-end-of-line)
  21948. (t 'end-of-line)))
  21949. deactivate-mark)
  21950. (if (or (not special) arg) (call-interactively move-fun)
  21951. (let* ((element (save-excursion (beginning-of-line)
  21952. (org-element-at-point)))
  21953. (type (org-element-type element)))
  21954. (cond
  21955. ((memq type '(headline inlinetask))
  21956. (let ((pos (point)))
  21957. (beginning-of-line 1)
  21958. (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*\\)?$"))
  21959. (if (eq special t)
  21960. (if (or (< pos (match-beginning 1)) (= pos (match-end 0)))
  21961. (goto-char (match-beginning 1))
  21962. (goto-char (match-end 0)))
  21963. (if (or (< pos (match-end 0))
  21964. (not (eq this-command last-command)))
  21965. (goto-char (match-end 0))
  21966. (goto-char (match-beginning 1))))
  21967. (call-interactively move-fun))))
  21968. ((outline-invisible-p (line-end-position))
  21969. ;; If element is hidden, `move-end-of-line' would put point
  21970. ;; after it. Use `end-of-line' to stay on current line.
  21971. (call-interactively 'end-of-line))
  21972. (t (call-interactively move-fun)))))
  21973. (org-no-warnings (and (featurep 'xemacs) (setq zmacs-region-stays t))))
  21974. (setq disable-point-adjustment
  21975. (or (not (invisible-p (point)))
  21976. (not (invisible-p (max (point-min) (1- (point))))))))
  21977. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  21978. (define-key org-mode-map "\C-e" 'org-end-of-line)
  21979. (defun org-backward-sentence (&optional _arg)
  21980. "Go to beginning of sentence, or beginning of table field.
  21981. This will call `backward-sentence' or `org-table-beginning-of-field',
  21982. depending on context."
  21983. (interactive)
  21984. (let* ((element (org-element-at-point))
  21985. (contents-begin (org-element-property :contents-begin element))
  21986. (table (org-element-lineage element '(table) t)))
  21987. (if (and table
  21988. (> (point) contents-begin)
  21989. (<= (point) (org-element-property :contents-end table)))
  21990. (call-interactively #'org-table-beginning-of-field)
  21991. (save-restriction
  21992. (when (and contents-begin
  21993. (< (point-min) contents-begin)
  21994. (> (point) contents-begin))
  21995. (narrow-to-region contents-begin
  21996. (org-element-property :contents-end element)))
  21997. (call-interactively #'backward-sentence)))))
  21998. (defun org-forward-sentence (&optional _arg)
  21999. "Go to end of sentence, or end of table field.
  22000. This will call `forward-sentence' or `org-table-end-of-field',
  22001. depending on context."
  22002. (interactive)
  22003. (let* ((element (org-element-at-point))
  22004. (contents-end (org-element-property :contents-end element))
  22005. (table (org-element-lineage element '(table) t)))
  22006. (if (and table
  22007. (>= (point) (org-element-property :contents-begin table))
  22008. (< (point) contents-end))
  22009. (call-interactively #'org-table-end-of-field)
  22010. (save-restriction
  22011. (when (and contents-end
  22012. (> (point-max) contents-end)
  22013. ;; Skip blank lines between elements.
  22014. (< (org-element-property :end element)
  22015. (save-excursion (goto-char contents-end)
  22016. (skip-chars-forward " \r\t\n"))))
  22017. (narrow-to-region (org-element-property :contents-begin element)
  22018. contents-end))
  22019. (call-interactively #'forward-sentence)))))
  22020. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  22021. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  22022. (defun org-kill-line (&optional _arg)
  22023. "Kill line, to tags or end of line."
  22024. (interactive)
  22025. (cond
  22026. ((or (not org-special-ctrl-k)
  22027. (bolp)
  22028. (not (org-at-heading-p)))
  22029. (when (and (get-char-property (min (point-max) (point-at-eol)) 'invisible)
  22030. org-ctrl-k-protect-subtree
  22031. (or (eq org-ctrl-k-protect-subtree 'error)
  22032. (not (y-or-n-p "Kill hidden subtree along with headline? "))))
  22033. (user-error "C-k aborted as it would kill a hidden subtree"))
  22034. (call-interactively
  22035. (if (org-bound-and-true-p visual-line-mode) 'kill-visual-line 'kill-line)))
  22036. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$"))
  22037. (kill-region (point) (match-beginning 1))
  22038. (org-set-tags nil t))
  22039. (t (kill-region (point) (point-at-eol)))))
  22040. (define-key org-mode-map "\C-k" 'org-kill-line)
  22041. (defun org-yank (&optional arg)
  22042. "Yank. If the kill is a subtree, treat it specially.
  22043. This command will look at the current kill and check if is a single
  22044. subtree, or a series of subtrees[1]. If it passes the test, and if the
  22045. cursor is at the beginning of a line or after the stars of a currently
  22046. empty headline, then the yank is handled specially. How exactly depends
  22047. on the value of the following variables, both set by default.
  22048. `org-yank-folded-subtrees'
  22049. When set, the subtree(s) will be folded after insertion, but only
  22050. if doing so would now swallow text after the yanked text.
  22051. `org-yank-adjusted-subtrees'
  22052. When set, the subtree will be promoted or demoted in order to
  22053. fit into the local outline tree structure, which means that the
  22054. level will be adjusted so that it becomes the smaller one of the
  22055. two *visible* surrounding headings.
  22056. Any prefix to this command will cause `yank' to be called directly with
  22057. no special treatment. In particular, a simple \\[universal-argument] prefix \
  22058. will just
  22059. plainly yank the text as it is.
  22060. \[1] The test checks if the first non-white line is a heading
  22061. and if there are no other headings with fewer stars."
  22062. (interactive "P")
  22063. (org-yank-generic 'yank arg))
  22064. (defun org-yank-generic (command arg)
  22065. "Perform some yank-like command.
  22066. This function implements the behavior described in the `org-yank'
  22067. documentation. However, it has been generalized to work for any
  22068. interactive command with similar behavior."
  22069. ;; pretend to be command COMMAND
  22070. (setq this-command command)
  22071. (if arg
  22072. (call-interactively command)
  22073. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  22074. (and (org-kill-is-subtree-p)
  22075. (or (bolp)
  22076. (and (looking-at "[ \t]*$")
  22077. (string-match
  22078. "\\`\\*+\\'"
  22079. (buffer-substring (point-at-bol) (point)))))))
  22080. swallowp)
  22081. (cond
  22082. ((and subtreep org-yank-folded-subtrees)
  22083. (let ((beg (point))
  22084. end)
  22085. (if (and subtreep org-yank-adjusted-subtrees)
  22086. (org-paste-subtree nil nil 'for-yank)
  22087. (call-interactively command))
  22088. (setq end (point))
  22089. (goto-char beg)
  22090. (when (and (bolp) subtreep
  22091. (not (setq swallowp
  22092. (org-yank-folding-would-swallow-text beg end))))
  22093. (org-with-limited-levels
  22094. (or (looking-at org-outline-regexp)
  22095. (re-search-forward org-outline-regexp-bol end t))
  22096. (while (and (< (point) end) (looking-at org-outline-regexp))
  22097. (outline-hide-subtree)
  22098. (org-cycle-show-empty-lines 'folded)
  22099. (condition-case nil
  22100. (outline-forward-same-level 1)
  22101. (error (goto-char end))))))
  22102. (when swallowp
  22103. (message
  22104. "Inserted text not folded because that would swallow text"))
  22105. (goto-char end)
  22106. (skip-chars-forward " \t\n\r")
  22107. (beginning-of-line 1)
  22108. (push-mark beg 'nomsg)))
  22109. ((and subtreep org-yank-adjusted-subtrees)
  22110. (let ((beg (point-at-bol)))
  22111. (org-paste-subtree nil nil 'for-yank)
  22112. (push-mark beg 'nomsg)))
  22113. (t
  22114. (call-interactively command))))))
  22115. (defun org-yank-folding-would-swallow-text (beg end)
  22116. "Would hide-subtree at BEG swallow any text after END?"
  22117. (let (level)
  22118. (org-with-limited-levels
  22119. (save-excursion
  22120. (goto-char beg)
  22121. (when (or (looking-at org-outline-regexp)
  22122. (re-search-forward org-outline-regexp-bol end t))
  22123. (setq level (org-outline-level)))
  22124. (goto-char end)
  22125. (skip-chars-forward " \t\r\n\v\f")
  22126. (not (or (eobp)
  22127. (and (bolp) (looking-at-p org-outline-regexp)
  22128. (<= (org-outline-level) level))))))))
  22129. (define-key org-mode-map "\C-y" 'org-yank)
  22130. (defun org-truely-invisible-p ()
  22131. "Check if point is at a character currently not visible.
  22132. This version does not only check the character property, but also
  22133. `visible-mode'."
  22134. ;; Early versions of noutline don't have `outline-invisible-p'.
  22135. (unless (org-bound-and-true-p visible-mode)
  22136. (outline-invisible-p)))
  22137. (defun org-invisible-p2 ()
  22138. "Check if point is at a character currently not visible."
  22139. (save-excursion
  22140. (when (and (eolp) (not (bobp))) (backward-char 1))
  22141. ;; Early versions of noutline don't have `outline-invisible-p'.
  22142. (outline-invisible-p)))
  22143. (defun org-back-to-heading (&optional invisible-ok)
  22144. "Call `outline-back-to-heading', but provide a better error message."
  22145. (condition-case nil
  22146. (outline-back-to-heading invisible-ok)
  22147. (error (error "Before first headline at position %d in buffer %s"
  22148. (point) (current-buffer)))))
  22149. (defun org-before-first-heading-p ()
  22150. "Before first heading?"
  22151. (save-excursion
  22152. (end-of-line)
  22153. (null (re-search-backward org-outline-regexp-bol nil t))))
  22154. (defun org-at-heading-p (&optional ignored)
  22155. (outline-on-heading-p t))
  22156. ;;; Though the function was obsoleted in 7.8.03, the byte-compiler
  22157. ;;; warning was only added in Org 9.0, which should be taken into
  22158. ;;; account when deciding when to remove the alias.
  22159. (define-obsolete-function-alias 'org-on-heading-p 'org-at-heading-p "Org 7.8.03")
  22160. (defun org-in-commented-heading-p (&optional no-inheritance)
  22161. "Non-nil if point is under a commented heading.
  22162. This function also checks ancestors of the current headline,
  22163. unless optional argument NO-INHERITANCE is non-nil."
  22164. (cond
  22165. ((org-before-first-heading-p) nil)
  22166. ((let ((headline (nth 4 (org-heading-components))))
  22167. (and headline
  22168. (let ((case-fold-search nil))
  22169. (org-string-match-p (concat "^" org-comment-string "\\(?: \\|$\\)")
  22170. headline)))))
  22171. (no-inheritance nil)
  22172. (t
  22173. (save-excursion (and (org-up-heading-safe) (org-in-commented-heading-p))))))
  22174. (defun org-at-comment-p nil
  22175. "Is cursor in a commented line?"
  22176. (save-excursion
  22177. (save-match-data
  22178. (beginning-of-line)
  22179. (looking-at "^[ \t]*# "))))
  22180. (defun org-at-drawer-p nil
  22181. "Is cursor at a drawer keyword?"
  22182. (save-excursion
  22183. (move-beginning-of-line 1)
  22184. (looking-at org-drawer-regexp)))
  22185. (defun org-at-block-p nil
  22186. "Is cursor at a block keyword?"
  22187. (save-excursion
  22188. (move-beginning-of-line 1)
  22189. (looking-at org-block-regexp)))
  22190. (defun org-point-at-end-of-empty-headline ()
  22191. "If point is at the end of an empty headline, return t, else nil.
  22192. If the heading only contains a TODO keyword, it is still still considered
  22193. empty."
  22194. (and (looking-at "[ \t]*$")
  22195. (when org-todo-line-regexp
  22196. (save-excursion
  22197. (beginning-of-line 1)
  22198. (let ((case-fold-search nil))
  22199. (looking-at org-todo-line-regexp)
  22200. (string= (match-string 3) ""))))))
  22201. (defun org-at-heading-or-item-p ()
  22202. (or (org-at-heading-p) (org-at-item-p)))
  22203. (defun org-at-target-p ()
  22204. (or (org-in-regexp org-radio-target-regexp)
  22205. (org-in-regexp org-target-regexp)))
  22206. ;; Compatibility alias with Org versions < 7.8.03
  22207. (defalias 'org-on-target-p 'org-at-target-p)
  22208. (defun org-up-heading-all (arg)
  22209. "Move to the heading line of which the present line is a subheading.
  22210. This function considers both visible and invisible heading lines.
  22211. With argument, move up ARG levels."
  22212. (if (fboundp 'outline-up-heading-all)
  22213. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  22214. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  22215. (defun org-up-heading-safe ()
  22216. "Move to the heading line of which the present line is a subheading.
  22217. This version will not throw an error. It will return the level of the
  22218. headline found, or nil if no higher level is found.
  22219. Also, this function will be a lot faster than `outline-up-heading',
  22220. because it relies on stars being the outline starters. This can really
  22221. make a significant difference in outlines with very many siblings."
  22222. (when (ignore-errors (org-back-to-heading t))
  22223. (let ((level-up (1- (funcall outline-level))))
  22224. (and (> level-up 0)
  22225. (re-search-backward (format "^\\*\\{1,%d\\} " level-up) nil t)
  22226. (funcall outline-level)))))
  22227. (defun org-first-sibling-p ()
  22228. "Is this heading the first child of its parents?"
  22229. (interactive)
  22230. (let ((re org-outline-regexp-bol)
  22231. level l)
  22232. (unless (org-at-heading-p t)
  22233. (user-error "Not at a heading"))
  22234. (setq level (funcall outline-level))
  22235. (save-excursion
  22236. (if (not (re-search-backward re nil t))
  22237. t
  22238. (setq l (funcall outline-level))
  22239. (< l level)))))
  22240. (defun org-goto-sibling (&optional previous)
  22241. "Goto the next sibling, even if it is invisible.
  22242. When PREVIOUS is set, go to the previous sibling instead. Returns t
  22243. when a sibling was found. When none is found, return nil and don't
  22244. move point."
  22245. (let ((fun (if previous 're-search-backward 're-search-forward))
  22246. (pos (point))
  22247. (re org-outline-regexp-bol)
  22248. level l)
  22249. (when (ignore-errors (org-back-to-heading t))
  22250. (setq level (funcall outline-level))
  22251. (catch 'exit
  22252. (or previous (forward-char 1))
  22253. (while (funcall fun re nil t)
  22254. (setq l (funcall outline-level))
  22255. (when (< l level) (goto-char pos) (throw 'exit nil))
  22256. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  22257. (goto-char pos)
  22258. nil))))
  22259. (defun org-show-siblings ()
  22260. "Show all siblings of the current headline."
  22261. (save-excursion
  22262. (while (org-goto-sibling) (org-flag-heading nil)))
  22263. (save-excursion
  22264. (while (org-goto-sibling 'previous)
  22265. (org-flag-heading nil))))
  22266. (defun org-goto-first-child ()
  22267. "Goto the first child, even if it is invisible.
  22268. Return t when a child was found. Otherwise don't move point and
  22269. return nil."
  22270. (let (level (pos (point)) (re org-outline-regexp-bol))
  22271. (when (ignore-errors (org-back-to-heading t))
  22272. (setq level (outline-level))
  22273. (forward-char 1)
  22274. (if (and (re-search-forward re nil t) (> (outline-level) level))
  22275. (progn (goto-char (match-beginning 0)) t)
  22276. (goto-char pos) nil))))
  22277. (defun org-show-hidden-entry ()
  22278. "Show an entry where even the heading is hidden."
  22279. (save-excursion
  22280. (org-show-entry)))
  22281. (defun org-flag-heading (flag &optional entry)
  22282. "Flag the current heading. FLAG non-nil means make invisible.
  22283. When ENTRY is non-nil, show the entire entry."
  22284. (save-excursion
  22285. (org-back-to-heading t)
  22286. ;; Check if we should show the entire entry
  22287. (if entry
  22288. (progn
  22289. (org-show-entry)
  22290. (save-excursion
  22291. (and (outline-next-heading)
  22292. (org-flag-heading nil))))
  22293. (outline-flag-region (max (point-min) (1- (point)))
  22294. (save-excursion (outline-end-of-heading) (point))
  22295. flag))))
  22296. (defun org-get-next-sibling ()
  22297. "Move to next heading of the same level, and return point.
  22298. If there is no such heading, return nil.
  22299. This is like outline-next-sibling, but invisible headings are ok."
  22300. (let ((level (funcall outline-level)))
  22301. (outline-next-heading)
  22302. (while (and (not (eobp)) (> (funcall outline-level) level))
  22303. (outline-next-heading))
  22304. (unless (or (eobp) (< (funcall outline-level) level))
  22305. (point))))
  22306. (defun org-get-last-sibling ()
  22307. "Move to previous heading of the same level, and return point.
  22308. If there is no such heading, return nil."
  22309. (let ((opoint (point))
  22310. (level (funcall outline-level)))
  22311. (outline-previous-heading)
  22312. (when (and (/= (point) opoint) (outline-on-heading-p t))
  22313. (while (and (> (funcall outline-level) level)
  22314. (not (bobp)))
  22315. (outline-previous-heading))
  22316. (unless (< (funcall outline-level) level)
  22317. (point)))))
  22318. (defun org-end-of-subtree (&optional invisible-ok to-heading)
  22319. "Goto to the end of a subtree."
  22320. ;; This contains an exact copy of the original function, but it uses
  22321. ;; `org-back-to-heading', to make it work also in invisible
  22322. ;; trees. And is uses an invisible-ok argument.
  22323. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  22324. ;; Furthermore, when used inside Org, finding the end of a large subtree
  22325. ;; with many children and grandchildren etc, this can be much faster
  22326. ;; than the outline version.
  22327. (org-back-to-heading invisible-ok)
  22328. (let ((first t)
  22329. (level (funcall outline-level)))
  22330. (if (and (derived-mode-p 'org-mode) (< level 1000))
  22331. ;; A true heading (not a plain list item), in Org-mode
  22332. ;; This means we can easily find the end by looking
  22333. ;; only for the right number of stars. Using a regexp to do
  22334. ;; this is so much faster than using a Lisp loop.
  22335. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  22336. (forward-char 1)
  22337. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  22338. ;; something else, do it the slow way
  22339. (while (and (not (eobp))
  22340. (or first (> (funcall outline-level) level)))
  22341. (setq first nil)
  22342. (outline-next-heading)))
  22343. (unless to-heading
  22344. (when (memq (preceding-char) '(?\n ?\^M))
  22345. ;; Go to end of line before heading
  22346. (forward-char -1)
  22347. (when (memq (preceding-char) '(?\n ?\^M))
  22348. ;; leave blank line before heading
  22349. (forward-char -1)))))
  22350. (point))
  22351. (defun org-end-of-meta-data (&optional full)
  22352. "Skip planning line and properties drawer in current entry.
  22353. When optional argument FULL is non-nil, also skip empty lines,
  22354. clocking lines and regular drawers at the beginning of the
  22355. entry."
  22356. (org-back-to-heading t)
  22357. (forward-line)
  22358. (when (org-looking-at-p org-planning-line-re) (forward-line))
  22359. (when (looking-at org-property-drawer-re)
  22360. (goto-char (match-end 0))
  22361. (forward-line))
  22362. (when (and full (not (org-at-heading-p)))
  22363. (catch 'exit
  22364. (let ((end (save-excursion (outline-next-heading) (point)))
  22365. (re (concat "[ \t]*$" "\\|" org-clock-line-re)))
  22366. (while (not (eobp))
  22367. (cond ((org-looking-at-p org-drawer-regexp)
  22368. (if (re-search-forward "^[ \t]*:END:[ \t]*$" end t)
  22369. (forward-line)
  22370. (throw 'exit t)))
  22371. ((org-looking-at-p re) (forward-line))
  22372. (t (throw 'exit t))))))))
  22373. (defun org-forward-heading-same-level (arg &optional invisible-ok)
  22374. "Move forward to the ARG'th subheading at same level as this one.
  22375. Stop at the first and last subheadings of a superior heading.
  22376. Normally this only looks at visible headings, but when INVISIBLE-OK is
  22377. non-nil it will also look at invisible ones."
  22378. (interactive "p")
  22379. (if (not (ignore-errors (org-back-to-heading invisible-ok)))
  22380. (if (and arg (< arg 0))
  22381. (goto-char (point-min))
  22382. (outline-next-heading))
  22383. (org-at-heading-p)
  22384. (let ((level (- (match-end 0) (match-beginning 0) 1))
  22385. (f (if (and arg (< arg 0))
  22386. 're-search-backward
  22387. 're-search-forward))
  22388. (count (if arg (abs arg) 1))
  22389. (result (point)))
  22390. (while (and (prog1 (> count 0)
  22391. (forward-char (if (and arg (< arg 0)) -1 1)))
  22392. (funcall f org-outline-regexp-bol nil 'move))
  22393. (let ((l (- (match-end 0) (match-beginning 0) 1)))
  22394. (cond ((< l level) (setq count 0))
  22395. ((and (= l level)
  22396. (or invisible-ok
  22397. (progn
  22398. (goto-char (line-beginning-position))
  22399. (not (outline-invisible-p)))))
  22400. (setq count (1- count))
  22401. (when (eq l level)
  22402. (setq result (point)))))))
  22403. (goto-char result))
  22404. (beginning-of-line 1)))
  22405. (defun org-backward-heading-same-level (arg &optional invisible-ok)
  22406. "Move backward to the ARG'th subheading at same level as this one.
  22407. Stop at the first and last subheadings of a superior heading."
  22408. (interactive "p")
  22409. (org-forward-heading-same-level (if arg (- arg) -1) invisible-ok))
  22410. (defun org-next-visible-heading (arg)
  22411. "Move to the next visible heading.
  22412. This function wraps `outline-next-visible-heading' with
  22413. `org-with-limited-levels' in order to skip over inline tasks and
  22414. respect customization of `org-odd-levels-only'."
  22415. (interactive "p")
  22416. (org-with-limited-levels
  22417. (outline-next-visible-heading arg)))
  22418. (defun org-previous-visible-heading (arg)
  22419. "Move to the next visible heading.
  22420. This function wraps `outline-previous-visible-heading' with
  22421. `org-with-limited-levels' in order to skip over inline tasks and
  22422. respect customization of `org-odd-levels-only'."
  22423. (interactive "p")
  22424. (org-with-limited-levels
  22425. (outline-previous-visible-heading arg)))
  22426. (defun org-next-block (arg &optional backward block-regexp)
  22427. "Jump to the next block.
  22428. With a prefix argument ARG, jump forward ARG many blocks.
  22429. When BACKWARD is non-nil, jump to the previous block.
  22430. When BLOCK-REGEXP is non-nil, use this regexp to find blocks.
  22431. Match data is set according to this regexp when the function
  22432. returns.
  22433. Return point at beginning of the opening line of found block.
  22434. Throw an error if no block is found."
  22435. (interactive "p")
  22436. (let ((re (or block-regexp "^[ \t]*#\\+BEGIN"))
  22437. (case-fold-search t)
  22438. (search-fn (if backward #'re-search-backward #'re-search-forward))
  22439. (count (or arg 1))
  22440. (origin (point))
  22441. last-element)
  22442. (if backward (beginning-of-line) (end-of-line))
  22443. (while (and (> count 0) (funcall search-fn re nil t))
  22444. (let ((element (save-excursion
  22445. (goto-char (match-beginning 0))
  22446. (save-match-data (org-element-at-point)))))
  22447. (when (and (memq (org-element-type element)
  22448. '(center-block comment-block dynamic-block
  22449. example-block export-block quote-block
  22450. special-block src-block verse-block))
  22451. (<= (match-beginning 0)
  22452. (org-element-property :post-affiliated element)))
  22453. (setq last-element element)
  22454. (decf count))))
  22455. (if (= count 0)
  22456. (prog1 (goto-char (org-element-property :post-affiliated last-element))
  22457. (save-match-data (org-show-context)))
  22458. (goto-char origin)
  22459. (user-error "No %s code blocks" (if backward "previous" "further")))))
  22460. (defun org-previous-block (arg &optional block-regexp)
  22461. "Jump to the previous block.
  22462. With a prefix argument ARG, jump backward ARG many source blocks.
  22463. When BLOCK-REGEXP is non-nil, use this regexp to find blocks."
  22464. (interactive "p")
  22465. (org-next-block arg t block-regexp))
  22466. (defun org-forward-paragraph ()
  22467. "Move forward to beginning of next paragraph or equivalent.
  22468. The function moves point to the beginning of the next visible
  22469. structural element, which can be a paragraph, a table, a list
  22470. item, etc. It also provides some special moves for convenience:
  22471. - On an affiliated keyword, jump to the beginning of the
  22472. relative element.
  22473. - On an item or a footnote definition, move to the second
  22474. element inside, if any.
  22475. - On a table or a property drawer, jump after it.
  22476. - On a verse or source block, stop after blank lines."
  22477. (interactive)
  22478. (when (eobp) (user-error "Cannot move further down"))
  22479. (let* ((deactivate-mark nil)
  22480. (element (org-element-at-point))
  22481. (type (org-element-type element))
  22482. (post-affiliated (org-element-property :post-affiliated element))
  22483. (contents-begin (org-element-property :contents-begin element))
  22484. (contents-end (org-element-property :contents-end element))
  22485. (end (let ((end (org-element-property :end element)) (parent element))
  22486. (while (and (setq parent (org-element-property :parent parent))
  22487. (= (org-element-property :contents-end parent) end))
  22488. (setq end (org-element-property :end parent)))
  22489. end)))
  22490. (cond ((not element)
  22491. (skip-chars-forward " \r\t\n")
  22492. (or (eobp) (beginning-of-line)))
  22493. ;; On affiliated keywords, move to element's beginning.
  22494. ((< (point) post-affiliated)
  22495. (goto-char post-affiliated))
  22496. ;; At a table row, move to the end of the table. Similarly,
  22497. ;; at a node property, move to the end of the property
  22498. ;; drawer.
  22499. ((memq type '(node-property table-row))
  22500. (goto-char (org-element-property
  22501. :end (org-element-property :parent element))))
  22502. ((memq type '(property-drawer table)) (goto-char end))
  22503. ;; Consider blank lines as separators in verse and source
  22504. ;; blocks to ease editing.
  22505. ((memq type '(src-block verse-block))
  22506. (when (eq type 'src-block)
  22507. (setq contents-end
  22508. (save-excursion (goto-char end)
  22509. (skip-chars-backward " \r\t\n")
  22510. (line-beginning-position))))
  22511. (beginning-of-line)
  22512. (when (looking-at "[ \t]*$") (skip-chars-forward " \r\t\n"))
  22513. (if (not (re-search-forward "^[ \t]*$" contents-end t))
  22514. (goto-char end)
  22515. (skip-chars-forward " \r\t\n")
  22516. (if (= (point) contents-end) (goto-char end)
  22517. (beginning-of-line))))
  22518. ;; With no contents, just skip element.
  22519. ((not contents-begin) (goto-char end))
  22520. ;; If contents are invisible, skip the element altogether.
  22521. ((outline-invisible-p (line-end-position))
  22522. (case type
  22523. (headline
  22524. (org-with-limited-levels (outline-next-visible-heading 1)))
  22525. ;; At a plain list, make sure we move to the next item
  22526. ;; instead of skipping the whole list.
  22527. (plain-list (forward-char)
  22528. (org-forward-paragraph))
  22529. (otherwise (goto-char end))))
  22530. ((>= (point) contents-end) (goto-char end))
  22531. ((>= (point) contents-begin)
  22532. ;; This can only happen on paragraphs and plain lists.
  22533. (case type
  22534. (paragraph (goto-char end))
  22535. ;; At a plain list, try to move to second element in
  22536. ;; first item, if possible.
  22537. (plain-list (end-of-line)
  22538. (org-forward-paragraph))))
  22539. ;; When contents start on the middle of a line (e.g. in
  22540. ;; items and footnote definitions), try to reach first
  22541. ;; element starting after current line.
  22542. ((> (line-end-position) contents-begin)
  22543. (end-of-line)
  22544. (org-forward-paragraph))
  22545. (t (goto-char contents-begin)))))
  22546. (defun org-backward-paragraph ()
  22547. "Move backward to start of previous paragraph or equivalent.
  22548. The function moves point to the beginning of the current
  22549. structural element, which can be a paragraph, a table, a list
  22550. item, etc., or to the beginning of the previous visible one if
  22551. point is already there. It also provides some special moves for
  22552. convenience:
  22553. - On an affiliated keyword, jump to the first one.
  22554. - On a table or a property drawer, move to its beginning.
  22555. - On a verse or source block, stop before blank lines."
  22556. (interactive)
  22557. (when (bobp) (user-error "Cannot move further up"))
  22558. (let* ((deactivate-mark nil)
  22559. (element (org-element-at-point))
  22560. (type (org-element-type element))
  22561. (contents-begin (org-element-property :contents-begin element))
  22562. (contents-end (org-element-property :contents-end element))
  22563. (post-affiliated (org-element-property :post-affiliated element))
  22564. (begin (org-element-property :begin element)))
  22565. (cond
  22566. ((not element) (goto-char (point-min)))
  22567. ((= (point) begin)
  22568. (backward-char)
  22569. (org-backward-paragraph))
  22570. ((<= (point) post-affiliated) (goto-char begin))
  22571. ((memq type '(node-property table-row))
  22572. (goto-char (org-element-property
  22573. :post-affiliated (org-element-property :parent element))))
  22574. ((memq type '(property-drawer table)) (goto-char begin))
  22575. ((memq type '(src-block verse-block))
  22576. (when (eq type 'src-block)
  22577. (setq contents-begin
  22578. (save-excursion (goto-char begin) (forward-line) (point))))
  22579. (if (= (point) contents-begin) (goto-char post-affiliated)
  22580. ;; Inside a verse block, see blank lines as paragraph
  22581. ;; separators.
  22582. (let ((origin (point)))
  22583. (skip-chars-backward " \r\t\n" contents-begin)
  22584. (when (re-search-backward "^[ \t]*$" contents-begin 'move)
  22585. (skip-chars-forward " \r\t\n" origin)
  22586. (if (= (point) origin) (goto-char contents-begin)
  22587. (beginning-of-line))))))
  22588. ((not contents-begin) (goto-char (or post-affiliated begin)))
  22589. ((eq type 'paragraph)
  22590. (goto-char contents-begin)
  22591. ;; When at first paragraph in an item or a footnote definition,
  22592. ;; move directly to beginning of line.
  22593. (let ((parent-contents
  22594. (org-element-property
  22595. :contents-begin (org-element-property :parent element))))
  22596. (when (and parent-contents (= parent-contents contents-begin))
  22597. (beginning-of-line))))
  22598. ;; At the end of a greater element, move to the beginning of the
  22599. ;; last element within.
  22600. ((>= (point) contents-end)
  22601. (goto-char (1- contents-end))
  22602. (org-backward-paragraph))
  22603. (t (goto-char (or post-affiliated begin))))
  22604. ;; Ensure we never leave point invisible.
  22605. (when (outline-invisible-p (point)) (beginning-of-visual-line))))
  22606. (defun org-forward-element ()
  22607. "Move forward by one element.
  22608. Move to the next element at the same level, when possible."
  22609. (interactive)
  22610. (cond ((eobp) (user-error "Cannot move further down"))
  22611. ((org-with-limited-levels (org-at-heading-p))
  22612. (let ((origin (point)))
  22613. (goto-char (org-end-of-subtree nil t))
  22614. (unless (org-with-limited-levels (org-at-heading-p))
  22615. (goto-char origin)
  22616. (user-error "Cannot move further down"))))
  22617. (t
  22618. (let* ((elem (org-element-at-point))
  22619. (end (org-element-property :end elem))
  22620. (parent (org-element-property :parent elem)))
  22621. (cond ((and parent (= (org-element-property :contents-end parent) end))
  22622. (goto-char (org-element-property :end parent)))
  22623. ((integer-or-marker-p end) (goto-char end))
  22624. (t (message "No element at point")))))))
  22625. (defun org-backward-element ()
  22626. "Move backward by one element.
  22627. Move to the previous element at the same level, when possible."
  22628. (interactive)
  22629. (cond ((bobp) (user-error "Cannot move further up"))
  22630. ((org-with-limited-levels (org-at-heading-p))
  22631. ;; At a headline, move to the previous one, if any, or stay
  22632. ;; here.
  22633. (let ((origin (point)))
  22634. (org-with-limited-levels (org-backward-heading-same-level 1))
  22635. ;; When current headline has no sibling above, move to its
  22636. ;; parent.
  22637. (when (= (point) origin)
  22638. (or (org-with-limited-levels (org-up-heading-safe))
  22639. (progn (goto-char origin)
  22640. (user-error "Cannot move further up"))))))
  22641. (t
  22642. (let* ((elem (org-element-at-point))
  22643. (beg (org-element-property :begin elem)))
  22644. (cond
  22645. ;; Move to beginning of current element if point isn't
  22646. ;; there already.
  22647. ((null beg) (message "No element at point"))
  22648. ((/= (point) beg) (goto-char beg))
  22649. (t (goto-char beg)
  22650. (skip-chars-backward " \r\t\n")
  22651. (unless (bobp)
  22652. (let ((prev (org-element-at-point)))
  22653. (goto-char (org-element-property :begin prev))
  22654. (while (and (setq prev (org-element-property :parent prev))
  22655. (<= (org-element-property :end prev) beg))
  22656. (goto-char (org-element-property :begin prev)))))))))))
  22657. (defun org-up-element ()
  22658. "Move to upper element."
  22659. (interactive)
  22660. (if (org-with-limited-levels (org-at-heading-p))
  22661. (unless (org-up-heading-safe) (user-error "No surrounding element"))
  22662. (let* ((elem (org-element-at-point))
  22663. (parent (org-element-property :parent elem)))
  22664. (if parent (goto-char (org-element-property :begin parent))
  22665. (if (org-with-limited-levels (org-before-first-heading-p))
  22666. (user-error "No surrounding element")
  22667. (org-with-limited-levels (org-back-to-heading)))))))
  22668. (defvar org-element-greater-elements)
  22669. (defun org-down-element ()
  22670. "Move to inner element."
  22671. (interactive)
  22672. (let ((element (org-element-at-point)))
  22673. (cond
  22674. ((memq (org-element-type element) '(plain-list table))
  22675. (goto-char (org-element-property :contents-begin element))
  22676. (forward-char))
  22677. ((memq (org-element-type element) org-element-greater-elements)
  22678. ;; If contents are hidden, first disclose them.
  22679. (when (outline-invisible-p (line-end-position)) (org-cycle))
  22680. (goto-char (or (org-element-property :contents-begin element)
  22681. (user-error "No content for this element"))))
  22682. (t (user-error "No inner element")))))
  22683. (defun org-drag-element-backward ()
  22684. "Move backward element at point."
  22685. (interactive)
  22686. (if (org-with-limited-levels (org-at-heading-p)) (org-move-subtree-up)
  22687. (let* ((elem (org-element-at-point))
  22688. (prev-elem
  22689. (save-excursion
  22690. (goto-char (org-element-property :begin elem))
  22691. (skip-chars-backward " \r\t\n")
  22692. (unless (bobp)
  22693. (let* ((beg (org-element-property :begin elem))
  22694. (prev (org-element-at-point))
  22695. (up prev))
  22696. (while (and (setq up (org-element-property :parent up))
  22697. (<= (org-element-property :end up) beg))
  22698. (setq prev up))
  22699. prev)))))
  22700. ;; Error out if no previous element or previous element is
  22701. ;; a parent of the current one.
  22702. (if (or (not prev-elem) (org-element-nested-p elem prev-elem))
  22703. (user-error "Cannot drag element backward")
  22704. (let ((pos (point)))
  22705. (org-element-swap-A-B prev-elem elem)
  22706. (goto-char (+ (org-element-property :begin prev-elem)
  22707. (- pos (org-element-property :begin elem)))))))))
  22708. (defun org-drag-element-forward ()
  22709. "Move forward element at point."
  22710. (interactive)
  22711. (let* ((pos (point))
  22712. (elem (org-element-at-point)))
  22713. (when (= (point-max) (org-element-property :end elem))
  22714. (user-error "Cannot drag element forward"))
  22715. (goto-char (org-element-property :end elem))
  22716. (let ((next-elem (org-element-at-point)))
  22717. (when (or (org-element-nested-p elem next-elem)
  22718. (and (eq (org-element-type next-elem) 'headline)
  22719. (not (eq (org-element-type elem) 'headline))))
  22720. (goto-char pos)
  22721. (user-error "Cannot drag element forward"))
  22722. ;; Compute new position of point: it's shifted by NEXT-ELEM
  22723. ;; body's length (without final blanks) and by the length of
  22724. ;; blanks between ELEM and NEXT-ELEM.
  22725. (let ((size-next (- (save-excursion
  22726. (goto-char (org-element-property :end next-elem))
  22727. (skip-chars-backward " \r\t\n")
  22728. (forward-line)
  22729. ;; Small correction if buffer doesn't end
  22730. ;; with a newline character.
  22731. (if (and (eolp) (not (bolp))) (1+ (point)) (point)))
  22732. (org-element-property :begin next-elem)))
  22733. (size-blank (- (org-element-property :end elem)
  22734. (save-excursion
  22735. (goto-char (org-element-property :end elem))
  22736. (skip-chars-backward " \r\t\n")
  22737. (forward-line)
  22738. (point)))))
  22739. (org-element-swap-A-B elem next-elem)
  22740. (goto-char (+ pos size-next size-blank))))))
  22741. (defun org-drag-line-forward (arg)
  22742. "Drag the line at point ARG lines forward."
  22743. (interactive "p")
  22744. (dotimes (n (abs arg))
  22745. (let ((c (current-column)))
  22746. (if (< 0 arg)
  22747. (progn
  22748. (beginning-of-line 2)
  22749. (transpose-lines 1)
  22750. (beginning-of-line 0))
  22751. (transpose-lines 1)
  22752. (beginning-of-line -1))
  22753. (org-move-to-column c))))
  22754. (defun org-drag-line-backward (arg)
  22755. "Drag the line at point ARG lines backward."
  22756. (interactive "p")
  22757. (org-drag-line-forward (- arg)))
  22758. (defun org-mark-element ()
  22759. "Put point at beginning of this element, mark at end.
  22760. Interactively, if this command is repeated or (in Transient Mark
  22761. mode) if the mark is active, it marks the next element after the
  22762. ones already marked."
  22763. (interactive)
  22764. (let (deactivate-mark)
  22765. (if (and (org-called-interactively-p 'any)
  22766. (or (and (eq last-command this-command) (mark t))
  22767. (and transient-mark-mode mark-active)))
  22768. (set-mark
  22769. (save-excursion
  22770. (goto-char (mark))
  22771. (goto-char (org-element-property :end (org-element-at-point)))))
  22772. (let ((element (org-element-at-point)))
  22773. (end-of-line)
  22774. (push-mark (org-element-property :end element) t t)
  22775. (goto-char (org-element-property :begin element))))))
  22776. (defun org-narrow-to-element ()
  22777. "Narrow buffer to current element."
  22778. (interactive)
  22779. (let ((elem (org-element-at-point)))
  22780. (cond
  22781. ((eq (car elem) 'headline)
  22782. (narrow-to-region
  22783. (org-element-property :begin elem)
  22784. (org-element-property :end elem)))
  22785. ((memq (car elem) org-element-greater-elements)
  22786. (narrow-to-region
  22787. (org-element-property :contents-begin elem)
  22788. (org-element-property :contents-end elem)))
  22789. (t
  22790. (narrow-to-region
  22791. (org-element-property :begin elem)
  22792. (org-element-property :end elem))))))
  22793. (defun org-transpose-element ()
  22794. "Transpose current and previous elements, keeping blank lines between.
  22795. Point is moved after both elements."
  22796. (interactive)
  22797. (org-skip-whitespace)
  22798. (let ((end (org-element-property :end (org-element-at-point))))
  22799. (org-drag-element-backward)
  22800. (goto-char end)))
  22801. (defun org-unindent-buffer ()
  22802. "Un-indent the visible part of the buffer.
  22803. Relative indentation (between items, inside blocks, etc.) isn't
  22804. modified."
  22805. (interactive)
  22806. (unless (eq major-mode 'org-mode)
  22807. (user-error "Cannot un-indent a buffer not in Org mode"))
  22808. (let* ((parse-tree (org-element-parse-buffer 'greater-element))
  22809. unindent-tree ; For byte-compiler.
  22810. (unindent-tree
  22811. (function
  22812. (lambda (contents)
  22813. (mapc
  22814. (lambda (element)
  22815. (if (memq (org-element-type element) '(headline section))
  22816. (funcall unindent-tree (org-element-contents element))
  22817. (save-excursion
  22818. (save-restriction
  22819. (narrow-to-region
  22820. (org-element-property :begin element)
  22821. (org-element-property :end element))
  22822. (org-do-remove-indentation)))))
  22823. (reverse contents))))))
  22824. (funcall unindent-tree (org-element-contents parse-tree))))
  22825. (defun org-show-children (&optional level)
  22826. "Show all direct subheadings of this heading.
  22827. Prefix arg LEVEL is how many levels below the current level
  22828. should be shown. Default is enough to cause the following
  22829. heading to appear."
  22830. (interactive "p")
  22831. ;; If `orgstruct-mode' is active, use the slower version.
  22832. (if orgstruct-mode (call-interactively #'outline-show-children)
  22833. (save-excursion
  22834. (org-back-to-heading t)
  22835. (let* ((current-level (funcall outline-level))
  22836. (max-level (org-get-valid-level
  22837. current-level
  22838. (if level (prefix-numeric-value level) 1)))
  22839. (end (save-excursion (org-end-of-subtree t t)))
  22840. (regexp-fmt "^\\*\\{%d,%s\\}\\(?: \\|$\\)")
  22841. (past-first-child nil)
  22842. ;; Make sure to skip inlinetasks.
  22843. (re (format regexp-fmt
  22844. current-level
  22845. (cond
  22846. ((not (featurep 'org-inlinetask)) "")
  22847. (org-odd-levels-only (- (* 2 org-inlinetask-min-level)
  22848. 3))
  22849. (t (1- org-inlinetask-min-level))))))
  22850. ;; Display parent heading.
  22851. (outline-flag-region (line-end-position 0) (line-end-position) nil)
  22852. (forward-line)
  22853. ;; Display children. First child may be deeper than expected
  22854. ;; MAX-LEVEL. Since we want to display it anyway, adjust
  22855. ;; MAX-LEVEL accordingly.
  22856. (while (re-search-forward re end t)
  22857. (unless past-first-child
  22858. (setq re (format regexp-fmt
  22859. current-level
  22860. (max (funcall outline-level) max-level)))
  22861. (setq past-first-child t))
  22862. (outline-flag-region
  22863. (line-end-position 0) (line-end-position) nil))))))
  22864. (defun org-show-subtree ()
  22865. "Show everything after this heading at deeper levels."
  22866. (interactive)
  22867. (outline-flag-region
  22868. (point)
  22869. (save-excursion
  22870. (org-end-of-subtree t t))
  22871. nil))
  22872. (defun org-show-entry ()
  22873. "Show the body directly following this heading.
  22874. Show the heading too, if it is currently invisible."
  22875. (interactive)
  22876. (save-excursion
  22877. (ignore-errors
  22878. (org-back-to-heading t)
  22879. (outline-flag-region
  22880. (max (point-min) (1- (point)))
  22881. (save-excursion
  22882. (if (re-search-forward
  22883. (concat "[\r\n]\\(" org-outline-regexp "\\)") nil t)
  22884. (match-beginning 1)
  22885. (point-max)))
  22886. nil)
  22887. (org-cycle-hide-drawers 'children))))
  22888. (defun org-make-options-regexp (kwds &optional extra)
  22889. "Make a regular expression for keyword lines.
  22890. KWDS is a list of keywords, as strings. Optional argument EXTRA,
  22891. when non-nil, is a regexp matching keywords names."
  22892. (concat "^[ \t]*#\\+\\("
  22893. (regexp-opt kwds)
  22894. (and extra (concat (and kwds "\\|") extra))
  22895. "\\):[ \t]*\\(.*\\)"))
  22896. ;; Make isearch reveal the necessary context
  22897. (defun org-isearch-end ()
  22898. "Reveal context after isearch exits."
  22899. (when isearch-success ; only if search was successful
  22900. (if (featurep 'xemacs)
  22901. ;; Under XEmacs, the hook is run in the correct place,
  22902. ;; we directly show the context.
  22903. (org-show-context 'isearch)
  22904. ;; In Emacs the hook runs *before* restoring the overlays.
  22905. ;; So we have to use a one-time post-command-hook to do this.
  22906. ;; (Emacs 22 has a special variable, see function `org-mode')
  22907. (unless (and (boundp 'isearch-mode-end-hook-quit)
  22908. isearch-mode-end-hook-quit)
  22909. ;; Only when the isearch was not quitted.
  22910. (org-add-hook 'post-command-hook 'org-isearch-post-command
  22911. 'append 'local)))))
  22912. (defun org-isearch-post-command ()
  22913. "Remove self from hook, and show context."
  22914. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  22915. (org-show-context 'isearch))
  22916. ;;;; Integration with and fixes for other packages
  22917. ;;; Imenu support
  22918. (defvar-local org-imenu-markers nil
  22919. "All markers currently used by Imenu.")
  22920. (defun org-imenu-new-marker (&optional pos)
  22921. "Return a new marker for use by Imenu, and remember the marker."
  22922. (let ((m (make-marker)))
  22923. (move-marker m (or pos (point)))
  22924. (push m org-imenu-markers)
  22925. m))
  22926. (defun org-imenu-get-tree ()
  22927. "Produce the index for Imenu."
  22928. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  22929. (setq org-imenu-markers nil)
  22930. (let* ((n org-imenu-depth)
  22931. (re (concat "^" (org-get-limited-outline-regexp)))
  22932. (subs (make-vector (1+ n) nil))
  22933. (last-level 0)
  22934. m level head0 head)
  22935. (save-excursion
  22936. (save-restriction
  22937. (widen)
  22938. (goto-char (point-max))
  22939. (while (re-search-backward re nil t)
  22940. (setq level (org-reduced-level (funcall outline-level)))
  22941. (when (and (<= level n)
  22942. (looking-at org-complex-heading-regexp)
  22943. (setq head0 (org-match-string-no-properties 4)))
  22944. (setq head (org-link-display-format head0)
  22945. m (org-imenu-new-marker))
  22946. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  22947. (if (>= level last-level)
  22948. (push (cons head m) (aref subs level))
  22949. (push (cons head (aref subs (1+ level))) (aref subs level))
  22950. (loop for i from (1+ level) to n do (aset subs i nil)))
  22951. (setq last-level level)))))
  22952. (aref subs 1)))
  22953. (eval-after-load "imenu"
  22954. '(progn
  22955. (add-hook 'imenu-after-jump-hook
  22956. (lambda ()
  22957. (when (derived-mode-p 'org-mode)
  22958. (org-show-context 'org-goto))))))
  22959. (defun org-link-display-format (link)
  22960. "Replace a link with its the description.
  22961. If there is no description, use the link target."
  22962. (save-match-data
  22963. (if (string-match org-bracket-link-analytic-regexp link)
  22964. (replace-match (if (match-end 5)
  22965. (match-string 5 link)
  22966. (concat (match-string 1 link)
  22967. (match-string 3 link)))
  22968. nil t link)
  22969. link)))
  22970. (defun org-toggle-link-display ()
  22971. "Toggle the literal or descriptive display of links."
  22972. (interactive)
  22973. (if org-descriptive-links
  22974. (progn (org-remove-from-invisibility-spec '(org-link))
  22975. (org-restart-font-lock)
  22976. (setq org-descriptive-links nil))
  22977. (progn (add-to-invisibility-spec '(org-link))
  22978. (org-restart-font-lock)
  22979. (setq org-descriptive-links t))))
  22980. ;; Speedbar support
  22981. (defvar org-speedbar-restriction-lock-overlay (make-overlay 1 1)
  22982. "Overlay marking the agenda restriction line in speedbar.")
  22983. (overlay-put org-speedbar-restriction-lock-overlay
  22984. 'face 'org-agenda-restriction-lock)
  22985. (overlay-put org-speedbar-restriction-lock-overlay
  22986. 'help-echo "Agendas are currently limited to this item.")
  22987. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  22988. (defun org-speedbar-set-agenda-restriction ()
  22989. "Restrict future agenda commands to the location at point in speedbar.
  22990. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  22991. (interactive)
  22992. (require 'org-agenda)
  22993. (let (p m tp np dir txt)
  22994. (cond
  22995. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  22996. 'org-imenu t))
  22997. (setq m (get-text-property p 'org-imenu-marker))
  22998. (with-current-buffer (marker-buffer m)
  22999. (goto-char m)
  23000. (org-agenda-set-restriction-lock 'subtree)))
  23001. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  23002. 'speedbar-function 'speedbar-find-file))
  23003. (setq tp (previous-single-property-change
  23004. (1+ p) 'speedbar-function)
  23005. np (next-single-property-change
  23006. tp 'speedbar-function)
  23007. dir (speedbar-line-directory)
  23008. txt (buffer-substring-no-properties (or tp (point-min))
  23009. (or np (point-max))))
  23010. (with-current-buffer (find-file-noselect
  23011. (let ((default-directory dir))
  23012. (expand-file-name txt)))
  23013. (unless (derived-mode-p 'org-mode)
  23014. (user-error "Cannot restrict to non-Org-mode file"))
  23015. (org-agenda-set-restriction-lock 'file)))
  23016. (t (user-error "Don't know how to restrict Org-mode's agenda")))
  23017. (move-overlay org-speedbar-restriction-lock-overlay
  23018. (point-at-bol) (point-at-eol))
  23019. (setq current-prefix-arg nil)
  23020. (org-agenda-maybe-redo)))
  23021. (defvar speedbar-file-key-map)
  23022. (declare-function speedbar-add-supported-extension "speedbar" (extension))
  23023. (eval-after-load "speedbar"
  23024. '(progn
  23025. (speedbar-add-supported-extension ".org")
  23026. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  23027. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  23028. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  23029. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  23030. (add-hook 'speedbar-visiting-tag-hook
  23031. (lambda () (and (derived-mode-p 'org-mode) (org-show-context 'org-goto))))))
  23032. ;;; Fixes and Hacks for problems with other packages
  23033. (defun org--flyspell-object-check-p (element)
  23034. "Non-nil when Flyspell can check object at point.
  23035. ELEMENT is the element at point."
  23036. (let ((object (save-excursion
  23037. (when (org-looking-at-p "\\>") (backward-char))
  23038. (org-element-context element))))
  23039. (case (org-element-type object)
  23040. ;; Prevent checks in links due to keybinding conflict with
  23041. ;; Flyspell.
  23042. ((code entity export-snippet inline-babel-call
  23043. inline-src-block line-break latex-fragment link macro
  23044. statistics-cookie target timestamp verbatim)
  23045. nil)
  23046. (footnote-reference
  23047. ;; Only in inline footnotes, within the definition.
  23048. (and (eq (org-element-property :type object) 'inline)
  23049. (< (save-excursion
  23050. (goto-char (org-element-property :begin object))
  23051. (search-forward ":" nil t 2))
  23052. (point))))
  23053. (otherwise t))))
  23054. (defun org-mode-flyspell-verify ()
  23055. "Function used for `flyspell-generic-check-word-predicate'."
  23056. (if (org-at-heading-p)
  23057. ;; At a headline or an inlinetask, check title only. This is
  23058. ;; faster than relying on `org-element-at-point'.
  23059. (and (save-excursion (beginning-of-line)
  23060. (and (let ((case-fold-search t))
  23061. (not (looking-at "\\*+ END[ \t]*$")))
  23062. (looking-at org-complex-heading-regexp)))
  23063. (match-beginning 4)
  23064. (>= (point) (match-beginning 4))
  23065. (or (not (match-beginning 5))
  23066. (< (point) (match-beginning 5))))
  23067. (let* ((element (org-element-at-point))
  23068. (post-affiliated (org-element-property :post-affiliated element)))
  23069. (cond
  23070. ;; Ignore checks in all affiliated keywords but captions.
  23071. ((< (point) post-affiliated)
  23072. (and (save-excursion
  23073. (beginning-of-line)
  23074. (let ((case-fold-search t)) (looking-at "[ \t]*#\\+CAPTION:")))
  23075. (> (point) (match-end 0))
  23076. (org--flyspell-object-check-p element)))
  23077. ;; Ignore checks in LOGBOOK (or equivalent) drawer.
  23078. ((let ((log (org-log-into-drawer)))
  23079. (and log
  23080. (let ((drawer (org-element-lineage element '(drawer))))
  23081. (and drawer
  23082. (eq (compare-strings
  23083. log nil nil
  23084. (org-element-property :drawer-name drawer) nil nil t)
  23085. t)))))
  23086. nil)
  23087. (t
  23088. (case (org-element-type element)
  23089. ((comment quote-section) t)
  23090. (comment-block
  23091. ;; Allow checks between block markers, not on them.
  23092. (and (> (line-beginning-position) post-affiliated)
  23093. (save-excursion
  23094. (end-of-line)
  23095. (skip-chars-forward " \r\t\n")
  23096. (< (point) (org-element-property :end element)))))
  23097. ;; Arbitrary list of keywords where checks are meaningful.
  23098. ;; Make sure point is on the value part of the element.
  23099. (keyword
  23100. (and (member (org-element-property :key element)
  23101. '("DESCRIPTION" "TITLE"))
  23102. (save-excursion
  23103. (search-backward ":" (line-beginning-position) t))))
  23104. ;; Check is globally allowed in paragraphs verse blocks and
  23105. ;; table rows (after affiliated keywords) but some objects
  23106. ;; must not be affected.
  23107. ((paragraph table-row verse-block)
  23108. (let ((cbeg (org-element-property :contents-begin element))
  23109. (cend (org-element-property :contents-end element)))
  23110. (and cbeg (>= (point) cbeg) (< (point) cend)
  23111. (org--flyspell-object-check-p element))))))))))
  23112. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  23113. (defun org-remove-flyspell-overlays-in (beg end)
  23114. "Remove flyspell overlays in region."
  23115. (and (org-bound-and-true-p flyspell-mode)
  23116. (fboundp 'flyspell-delete-region-overlays)
  23117. (flyspell-delete-region-overlays beg end)))
  23118. (defvar flyspell-delayed-commands)
  23119. (eval-after-load "flyspell"
  23120. '(add-to-list 'flyspell-delayed-commands 'org-self-insert-command))
  23121. ;; Make `bookmark-jump' shows the jump location if it was hidden.
  23122. (eval-after-load "bookmark"
  23123. '(if (boundp 'bookmark-after-jump-hook)
  23124. ;; We can use the hook
  23125. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  23126. ;; Hook not available, use advice
  23127. (defadvice bookmark-jump (after org-make-visible activate)
  23128. "Make the position visible."
  23129. (org-bookmark-jump-unhide))))
  23130. ;; Make sure saveplace shows the location if it was hidden
  23131. (eval-after-load "saveplace"
  23132. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  23133. "Make the position visible."
  23134. (org-bookmark-jump-unhide)))
  23135. ;; Make sure ecb shows the location if it was hidden
  23136. (eval-after-load "ecb"
  23137. '(defadvice ecb-method-clicked (after esf/org-show-context activate)
  23138. "Make hierarchy visible when jumping into location from ECB tree buffer."
  23139. (when (derived-mode-p 'org-mode)
  23140. (org-show-context))))
  23141. (defun org-bookmark-jump-unhide ()
  23142. "Unhide the current position, to show the bookmark location."
  23143. (and (derived-mode-p 'org-mode)
  23144. (or (outline-invisible-p)
  23145. (save-excursion (goto-char (max (point-min) (1- (point))))
  23146. (outline-invisible-p)))
  23147. (org-show-context 'bookmark-jump)))
  23148. (defun org-mark-jump-unhide ()
  23149. "Make the point visible with `org-show-context' after jumping to the mark."
  23150. (when (and (derived-mode-p 'org-mode)
  23151. (outline-invisible-p))
  23152. (org-show-context 'mark-goto)))
  23153. (eval-after-load "simple"
  23154. '(defadvice pop-to-mark-command (after org-make-visible activate)
  23155. "Make the point visible with `org-show-context'."
  23156. (org-mark-jump-unhide)))
  23157. (eval-after-load "simple"
  23158. '(defadvice exchange-point-and-mark (after org-make-visible activate)
  23159. "Make the point visible with `org-show-context'."
  23160. (org-mark-jump-unhide)))
  23161. (eval-after-load "simple"
  23162. '(defadvice pop-global-mark (after org-make-visible activate)
  23163. "Make the point visible with `org-show-context'."
  23164. (org-mark-jump-unhide)))
  23165. ;; Make session.el ignore our circular variable
  23166. (defvar session-globals-exclude)
  23167. (eval-after-load "session"
  23168. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  23169. ;;;; Finish up
  23170. (provide 'org)
  23171. (run-hooks 'org-load-hook)
  23172. ;;; org.el ends here