org.el 956 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106171071710817109171101711117112171131711417115171161711717118171191712017121171221712317124171251712617127171281712917130171311713217133171341713517136171371713817139171401714117142171431714417145171461714717148171491715017151171521715317154171551715617157171581715917160171611716217163171641716517166171671716817169171701717117172171731717417175171761717717178171791718017181171821718317184171851718617187171881718917190171911719217193171941719517196171971719817199172001720117202172031720417205172061720717208172091721017211172121721317214172151721617217172181721917220172211722217223172241722517226172271722817229172301723117232172331723417235172361723717238172391724017241172421724317244172451724617247172481724917250172511725217253172541725517256172571725817259172601726117262172631726417265172661726717268172691727017271172721727317274172751727617277172781727917280172811728217283172841728517286172871728817289172901729117292172931729417295172961729717298172991730017301173021730317304173051730617307173081730917310173111731217313173141731517316173171731817319173201732117322173231732417325173261732717328173291733017331173321733317334173351733617337173381733917340173411734217343173441734517346173471734817349173501735117352173531735417355173561735717358173591736017361173621736317364173651736617367173681736917370173711737217373173741737517376173771737817379173801738117382173831738417385173861738717388173891739017391173921739317394173951739617397173981739917400174011740217403174041740517406174071740817409174101741117412174131741417415174161741717418174191742017421174221742317424174251742617427174281742917430174311743217433174341743517436174371743817439174401744117442174431744417445174461744717448174491745017451174521745317454174551745617457174581745917460174611746217463174641746517466174671746817469174701747117472174731747417475174761747717478174791748017481174821748317484174851748617487174881748917490174911749217493174941749517496174971749817499175001750117502175031750417505175061750717508175091751017511175121751317514175151751617517175181751917520175211752217523175241752517526175271752817529175301753117532175331753417535175361753717538175391754017541175421754317544175451754617547175481754917550175511755217553175541755517556175571755817559175601756117562175631756417565175661756717568175691757017571175721757317574175751757617577175781757917580175811758217583175841758517586175871758817589175901759117592175931759417595175961759717598175991760017601176021760317604176051760617607176081760917610176111761217613176141761517616176171761817619176201762117622176231762417625176261762717628176291763017631176321763317634176351763617637176381763917640176411764217643176441764517646176471764817649176501765117652176531765417655176561765717658176591766017661176621766317664176651766617667176681766917670176711767217673176741767517676176771767817679176801768117682176831768417685176861768717688176891769017691176921769317694176951769617697176981769917700177011770217703177041770517706177071770817709177101771117712177131771417715177161771717718177191772017721177221772317724177251772617727177281772917730177311773217733177341773517736177371773817739177401774117742177431774417745177461774717748177491775017751177521775317754177551775617757177581775917760177611776217763177641776517766177671776817769177701777117772177731777417775177761777717778177791778017781177821778317784177851778617787177881778917790177911779217793177941779517796177971779817799178001780117802178031780417805178061780717808178091781017811178121781317814178151781617817178181781917820178211782217823178241782517826178271782817829178301783117832178331783417835178361783717838178391784017841178421784317844178451784617847178481784917850178511785217853178541785517856178571785817859178601786117862178631786417865178661786717868178691787017871178721787317874178751787617877178781787917880178811788217883178841788517886178871788817889178901789117892178931789417895178961789717898178991790017901179021790317904179051790617907179081790917910179111791217913179141791517916179171791817919179201792117922179231792417925179261792717928179291793017931179321793317934179351793617937179381793917940179411794217943179441794517946179471794817949179501795117952179531795417955179561795717958179591796017961179621796317964179651796617967179681796917970179711797217973179741797517976179771797817979179801798117982179831798417985179861798717988179891799017991179921799317994179951799617997179981799918000180011800218003180041800518006180071800818009180101801118012180131801418015180161801718018180191802018021180221802318024180251802618027180281802918030180311803218033180341803518036180371803818039180401804118042180431804418045180461804718048180491805018051180521805318054180551805618057180581805918060180611806218063180641806518066180671806818069180701807118072180731807418075180761807718078180791808018081180821808318084180851808618087180881808918090180911809218093180941809518096180971809818099181001810118102181031810418105181061810718108181091811018111181121811318114181151811618117181181811918120181211812218123181241812518126181271812818129181301813118132181331813418135181361813718138181391814018141181421814318144181451814618147181481814918150181511815218153181541815518156181571815818159181601816118162181631816418165181661816718168181691817018171181721817318174181751817618177181781817918180181811818218183181841818518186181871818818189181901819118192181931819418195181961819718198181991820018201182021820318204182051820618207182081820918210182111821218213182141821518216182171821818219182201822118222182231822418225182261822718228182291823018231182321823318234182351823618237182381823918240182411824218243182441824518246182471824818249182501825118252182531825418255182561825718258182591826018261182621826318264182651826618267182681826918270182711827218273182741827518276182771827818279182801828118282182831828418285182861828718288182891829018291182921829318294182951829618297182981829918300183011830218303183041830518306183071830818309183101831118312183131831418315183161831718318183191832018321183221832318324183251832618327183281832918330183311833218333183341833518336183371833818339183401834118342183431834418345183461834718348183491835018351183521835318354183551835618357183581835918360183611836218363183641836518366183671836818369183701837118372183731837418375183761837718378183791838018381183821838318384183851838618387183881838918390183911839218393183941839518396183971839818399184001840118402184031840418405184061840718408184091841018411184121841318414184151841618417184181841918420184211842218423184241842518426184271842818429184301843118432184331843418435184361843718438184391844018441184421844318444184451844618447184481844918450184511845218453184541845518456184571845818459184601846118462184631846418465184661846718468184691847018471184721847318474184751847618477184781847918480184811848218483184841848518486184871848818489184901849118492184931849418495184961849718498184991850018501185021850318504185051850618507185081850918510185111851218513185141851518516185171851818519185201852118522185231852418525185261852718528185291853018531185321853318534185351853618537185381853918540185411854218543185441854518546185471854818549185501855118552185531855418555185561855718558185591856018561185621856318564185651856618567185681856918570185711857218573185741857518576185771857818579185801858118582185831858418585185861858718588185891859018591185921859318594185951859618597185981859918600186011860218603186041860518606186071860818609186101861118612186131861418615186161861718618186191862018621186221862318624186251862618627186281862918630186311863218633186341863518636186371863818639186401864118642186431864418645186461864718648186491865018651186521865318654186551865618657186581865918660186611866218663186641866518666186671866818669186701867118672186731867418675186761867718678186791868018681186821868318684186851868618687186881868918690186911869218693186941869518696186971869818699187001870118702187031870418705187061870718708187091871018711187121871318714187151871618717187181871918720187211872218723187241872518726187271872818729187301873118732187331873418735187361873718738187391874018741187421874318744187451874618747187481874918750187511875218753187541875518756187571875818759187601876118762187631876418765187661876718768187691877018771187721877318774187751877618777187781877918780187811878218783187841878518786187871878818789187901879118792187931879418795187961879718798187991880018801188021880318804188051880618807188081880918810188111881218813188141881518816188171881818819188201882118822188231882418825188261882718828188291883018831188321883318834188351883618837188381883918840188411884218843188441884518846188471884818849188501885118852188531885418855188561885718858188591886018861188621886318864188651886618867188681886918870188711887218873188741887518876188771887818879188801888118882188831888418885188861888718888188891889018891188921889318894188951889618897188981889918900189011890218903189041890518906189071890818909189101891118912189131891418915189161891718918189191892018921189221892318924189251892618927189281892918930189311893218933189341893518936189371893818939189401894118942189431894418945189461894718948189491895018951189521895318954189551895618957189581895918960189611896218963189641896518966189671896818969189701897118972189731897418975189761897718978189791898018981189821898318984189851898618987189881898918990189911899218993189941899518996189971899818999190001900119002190031900419005190061900719008190091901019011190121901319014190151901619017190181901919020190211902219023190241902519026190271902819029190301903119032190331903419035190361903719038190391904019041190421904319044190451904619047190481904919050190511905219053190541905519056190571905819059190601906119062190631906419065190661906719068190691907019071190721907319074190751907619077190781907919080190811908219083190841908519086190871908819089190901909119092190931909419095190961909719098190991910019101191021910319104191051910619107191081910919110191111911219113191141911519116191171911819119191201912119122191231912419125191261912719128191291913019131191321913319134191351913619137191381913919140191411914219143191441914519146191471914819149191501915119152191531915419155191561915719158191591916019161191621916319164191651916619167191681916919170191711917219173191741917519176191771917819179191801918119182191831918419185191861918719188191891919019191191921919319194191951919619197191981919919200192011920219203192041920519206192071920819209192101921119212192131921419215192161921719218192191922019221192221922319224192251922619227192281922919230192311923219233192341923519236192371923819239192401924119242192431924419245192461924719248192491925019251192521925319254192551925619257192581925919260192611926219263192641926519266192671926819269192701927119272192731927419275192761927719278192791928019281192821928319284192851928619287192881928919290192911929219293192941929519296192971929819299193001930119302193031930419305193061930719308193091931019311193121931319314193151931619317193181931919320193211932219323193241932519326193271932819329193301933119332193331933419335193361933719338193391934019341193421934319344193451934619347193481934919350193511935219353193541935519356193571935819359193601936119362193631936419365193661936719368193691937019371193721937319374193751937619377193781937919380193811938219383193841938519386193871938819389193901939119392193931939419395193961939719398193991940019401194021940319404194051940619407194081940919410194111941219413194141941519416194171941819419194201942119422194231942419425194261942719428194291943019431194321943319434194351943619437194381943919440194411944219443194441944519446194471944819449194501945119452194531945419455194561945719458194591946019461194621946319464194651946619467194681946919470194711947219473194741947519476194771947819479194801948119482194831948419485194861948719488194891949019491194921949319494194951949619497194981949919500195011950219503195041950519506195071950819509195101951119512195131951419515195161951719518195191952019521195221952319524195251952619527195281952919530195311953219533195341953519536195371953819539195401954119542195431954419545195461954719548195491955019551195521955319554195551955619557195581955919560195611956219563195641956519566195671956819569195701957119572195731957419575195761957719578195791958019581195821958319584195851958619587195881958919590195911959219593195941959519596195971959819599196001960119602196031960419605196061960719608196091961019611196121961319614196151961619617196181961919620196211962219623196241962519626196271962819629196301963119632196331963419635196361963719638196391964019641196421964319644196451964619647196481964919650196511965219653196541965519656196571965819659196601966119662196631966419665196661966719668196691967019671196721967319674196751967619677196781967919680196811968219683196841968519686196871968819689196901969119692196931969419695196961969719698196991970019701197021970319704197051970619707197081970919710197111971219713197141971519716197171971819719197201972119722197231972419725197261972719728197291973019731197321973319734197351973619737197381973919740197411974219743197441974519746197471974819749197501975119752197531975419755197561975719758197591976019761197621976319764197651976619767197681976919770197711977219773197741977519776197771977819779197801978119782197831978419785197861978719788197891979019791197921979319794197951979619797197981979919800198011980219803198041980519806198071980819809198101981119812198131981419815198161981719818198191982019821198221982319824198251982619827198281982919830198311983219833198341983519836198371983819839198401984119842198431984419845198461984719848198491985019851198521985319854198551985619857198581985919860198611986219863198641986519866198671986819869198701987119872198731987419875198761987719878198791988019881198821988319884198851988619887198881988919890198911989219893198941989519896198971989819899199001990119902199031990419905199061990719908199091991019911199121991319914199151991619917199181991919920199211992219923199241992519926199271992819929199301993119932199331993419935199361993719938199391994019941199421994319944199451994619947199481994919950199511995219953199541995519956199571995819959199601996119962199631996419965199661996719968199691997019971199721997319974199751997619977199781997919980199811998219983199841998519986199871998819989199901999119992199931999419995199961999719998199992000020001200022000320004200052000620007200082000920010200112001220013200142001520016200172001820019200202002120022200232002420025200262002720028200292003020031200322003320034200352003620037200382003920040200412004220043200442004520046200472004820049200502005120052200532005420055200562005720058200592006020061200622006320064200652006620067200682006920070200712007220073200742007520076200772007820079200802008120082200832008420085200862008720088200892009020091200922009320094200952009620097200982009920100201012010220103201042010520106201072010820109201102011120112201132011420115201162011720118201192012020121201222012320124201252012620127201282012920130201312013220133201342013520136201372013820139201402014120142201432014420145201462014720148201492015020151201522015320154201552015620157201582015920160201612016220163201642016520166201672016820169201702017120172201732017420175201762017720178201792018020181201822018320184201852018620187201882018920190201912019220193201942019520196201972019820199202002020120202202032020420205202062020720208202092021020211202122021320214202152021620217202182021920220202212022220223202242022520226202272022820229202302023120232202332023420235202362023720238202392024020241202422024320244202452024620247202482024920250202512025220253202542025520256202572025820259202602026120262202632026420265202662026720268202692027020271202722027320274202752027620277202782027920280202812028220283202842028520286202872028820289202902029120292202932029420295202962029720298202992030020301203022030320304203052030620307203082030920310203112031220313203142031520316203172031820319203202032120322203232032420325203262032720328203292033020331203322033320334203352033620337203382033920340203412034220343203442034520346203472034820349203502035120352203532035420355203562035720358203592036020361203622036320364203652036620367203682036920370203712037220373203742037520376203772037820379203802038120382203832038420385203862038720388203892039020391203922039320394203952039620397203982039920400204012040220403204042040520406204072040820409204102041120412204132041420415204162041720418204192042020421204222042320424204252042620427204282042920430204312043220433204342043520436204372043820439204402044120442204432044420445204462044720448204492045020451204522045320454204552045620457204582045920460204612046220463204642046520466204672046820469204702047120472204732047420475204762047720478204792048020481204822048320484204852048620487204882048920490204912049220493204942049520496204972049820499205002050120502205032050420505205062050720508205092051020511205122051320514205152051620517205182051920520205212052220523205242052520526205272052820529205302053120532205332053420535205362053720538205392054020541205422054320544205452054620547205482054920550205512055220553205542055520556205572055820559205602056120562205632056420565205662056720568205692057020571205722057320574205752057620577205782057920580205812058220583205842058520586205872058820589205902059120592205932059420595205962059720598205992060020601206022060320604206052060620607206082060920610206112061220613206142061520616206172061820619206202062120622206232062420625206262062720628206292063020631206322063320634206352063620637206382063920640206412064220643206442064520646206472064820649206502065120652206532065420655206562065720658206592066020661206622066320664206652066620667206682066920670206712067220673206742067520676206772067820679206802068120682206832068420685206862068720688206892069020691206922069320694206952069620697206982069920700207012070220703207042070520706207072070820709207102071120712207132071420715207162071720718207192072020721207222072320724207252072620727207282072920730207312073220733207342073520736207372073820739207402074120742207432074420745207462074720748207492075020751207522075320754207552075620757207582075920760207612076220763207642076520766207672076820769207702077120772207732077420775207762077720778207792078020781207822078320784207852078620787207882078920790207912079220793207942079520796207972079820799208002080120802208032080420805208062080720808208092081020811208122081320814208152081620817208182081920820208212082220823208242082520826208272082820829208302083120832208332083420835208362083720838208392084020841208422084320844208452084620847208482084920850208512085220853208542085520856208572085820859208602086120862208632086420865208662086720868208692087020871208722087320874208752087620877208782087920880208812088220883208842088520886208872088820889208902089120892208932089420895208962089720898208992090020901209022090320904209052090620907209082090920910209112091220913209142091520916209172091820919209202092120922209232092420925209262092720928209292093020931209322093320934209352093620937209382093920940209412094220943209442094520946209472094820949209502095120952209532095420955209562095720958209592096020961209622096320964209652096620967209682096920970209712097220973209742097520976209772097820979209802098120982209832098420985209862098720988209892099020991209922099320994209952099620997209982099921000210012100221003210042100521006210072100821009210102101121012210132101421015210162101721018210192102021021210222102321024210252102621027210282102921030210312103221033210342103521036210372103821039210402104121042210432104421045210462104721048210492105021051210522105321054210552105621057210582105921060210612106221063210642106521066210672106821069210702107121072210732107421075210762107721078210792108021081210822108321084210852108621087210882108921090210912109221093210942109521096210972109821099211002110121102211032110421105211062110721108211092111021111211122111321114211152111621117211182111921120211212112221123211242112521126211272112821129211302113121132211332113421135211362113721138211392114021141211422114321144211452114621147211482114921150211512115221153211542115521156211572115821159211602116121162211632116421165211662116721168211692117021171211722117321174211752117621177211782117921180211812118221183211842118521186211872118821189211902119121192211932119421195211962119721198211992120021201212022120321204212052120621207212082120921210212112121221213212142121521216212172121821219212202122121222212232122421225212262122721228212292123021231212322123321234212352123621237212382123921240212412124221243212442124521246212472124821249212502125121252212532125421255212562125721258212592126021261212622126321264212652126621267212682126921270212712127221273212742127521276212772127821279212802128121282212832128421285212862128721288212892129021291212922129321294212952129621297212982129921300213012130221303213042130521306213072130821309213102131121312213132131421315213162131721318213192132021321213222132321324213252132621327213282132921330213312133221333213342133521336213372133821339213402134121342213432134421345213462134721348213492135021351213522135321354213552135621357213582135921360213612136221363213642136521366213672136821369213702137121372213732137421375213762137721378213792138021381213822138321384213852138621387213882138921390213912139221393213942139521396213972139821399214002140121402214032140421405214062140721408214092141021411214122141321414214152141621417214182141921420214212142221423214242142521426214272142821429214302143121432214332143421435214362143721438214392144021441214422144321444214452144621447214482144921450214512145221453214542145521456214572145821459214602146121462214632146421465214662146721468214692147021471214722147321474214752147621477214782147921480214812148221483214842148521486214872148821489214902149121492214932149421495214962149721498214992150021501215022150321504215052150621507215082150921510215112151221513215142151521516215172151821519215202152121522215232152421525215262152721528215292153021531215322153321534215352153621537215382153921540215412154221543215442154521546215472154821549215502155121552215532155421555215562155721558215592156021561215622156321564215652156621567215682156921570215712157221573215742157521576215772157821579215802158121582215832158421585215862158721588215892159021591215922159321594215952159621597215982159921600216012160221603216042160521606216072160821609216102161121612216132161421615216162161721618216192162021621216222162321624216252162621627216282162921630216312163221633216342163521636216372163821639216402164121642216432164421645216462164721648216492165021651216522165321654216552165621657216582165921660216612166221663216642166521666216672166821669216702167121672216732167421675216762167721678216792168021681216822168321684216852168621687216882168921690216912169221693216942169521696216972169821699217002170121702217032170421705217062170721708217092171021711217122171321714217152171621717217182171921720217212172221723217242172521726217272172821729217302173121732217332173421735217362173721738217392174021741217422174321744217452174621747217482174921750217512175221753217542175521756217572175821759217602176121762217632176421765217662176721768217692177021771217722177321774217752177621777217782177921780217812178221783217842178521786217872178821789217902179121792217932179421795217962179721798217992180021801218022180321804218052180621807218082180921810218112181221813218142181521816218172181821819218202182121822218232182421825218262182721828218292183021831218322183321834218352183621837218382183921840218412184221843218442184521846218472184821849218502185121852218532185421855218562185721858218592186021861218622186321864218652186621867218682186921870218712187221873218742187521876218772187821879218802188121882218832188421885218862188721888218892189021891218922189321894218952189621897218982189921900219012190221903219042190521906219072190821909219102191121912219132191421915219162191721918219192192021921219222192321924219252192621927219282192921930219312193221933219342193521936219372193821939219402194121942219432194421945219462194721948219492195021951219522195321954219552195621957219582195921960219612196221963219642196521966219672196821969219702197121972219732197421975219762197721978219792198021981219822198321984219852198621987219882198921990219912199221993219942199521996219972199821999220002200122002220032200422005220062200722008220092201022011220122201322014220152201622017220182201922020220212202222023220242202522026220272202822029220302203122032220332203422035220362203722038220392204022041220422204322044220452204622047220482204922050220512205222053220542205522056220572205822059220602206122062220632206422065220662206722068220692207022071220722207322074220752207622077220782207922080220812208222083220842208522086220872208822089220902209122092220932209422095220962209722098220992210022101221022210322104221052210622107221082210922110221112211222113221142211522116221172211822119221202212122122221232212422125221262212722128221292213022131221322213322134221352213622137221382213922140221412214222143221442214522146221472214822149221502215122152221532215422155221562215722158221592216022161221622216322164221652216622167221682216922170221712217222173221742217522176221772217822179221802218122182221832218422185221862218722188221892219022191221922219322194221952219622197221982219922200222012220222203222042220522206222072220822209222102221122212222132221422215222162221722218222192222022221222222222322224222252222622227222282222922230222312223222233222342223522236222372223822239222402224122242222432224422245222462224722248222492225022251222522225322254222552225622257222582225922260222612226222263222642226522266222672226822269222702227122272222732227422275222762227722278222792228022281222822228322284222852228622287222882228922290222912229222293222942229522296222972229822299223002230122302223032230422305223062230722308223092231022311223122231322314223152231622317223182231922320223212232222323223242232522326223272232822329223302233122332223332233422335223362233722338223392234022341223422234322344223452234622347223482234922350223512235222353223542235522356223572235822359223602236122362223632236422365223662236722368223692237022371223722237322374223752237622377223782237922380223812238222383223842238522386223872238822389223902239122392223932239422395223962239722398223992240022401224022240322404224052240622407224082240922410224112241222413224142241522416224172241822419224202242122422224232242422425224262242722428224292243022431224322243322434224352243622437224382243922440224412244222443224442244522446224472244822449224502245122452224532245422455224562245722458224592246022461224622246322464224652246622467224682246922470224712247222473224742247522476224772247822479224802248122482224832248422485224862248722488224892249022491224922249322494224952249622497224982249922500225012250222503225042250522506225072250822509225102251122512225132251422515225162251722518225192252022521225222252322524225252252622527225282252922530225312253222533225342253522536225372253822539225402254122542225432254422545225462254722548225492255022551225522255322554225552255622557225582255922560225612256222563225642256522566225672256822569225702257122572225732257422575225762257722578225792258022581225822258322584225852258622587225882258922590225912259222593225942259522596225972259822599226002260122602226032260422605226062260722608226092261022611226122261322614226152261622617226182261922620226212262222623226242262522626226272262822629226302263122632226332263422635226362263722638226392264022641226422264322644226452264622647226482264922650226512265222653226542265522656226572265822659226602266122662226632266422665226662266722668226692267022671226722267322674226752267622677226782267922680226812268222683226842268522686226872268822689226902269122692226932269422695226962269722698226992270022701227022270322704227052270622707227082270922710227112271222713227142271522716227172271822719227202272122722227232272422725227262272722728227292273022731227322273322734227352273622737227382273922740227412274222743227442274522746227472274822749227502275122752227532275422755227562275722758227592276022761227622276322764227652276622767227682276922770227712277222773227742277522776227772277822779227802278122782227832278422785227862278722788227892279022791227922279322794227952279622797227982279922800228012280222803228042280522806228072280822809228102281122812228132281422815228162281722818228192282022821228222282322824228252282622827228282282922830228312283222833228342283522836228372283822839228402284122842228432284422845228462284722848228492285022851228522285322854228552285622857228582285922860228612286222863228642286522866228672286822869228702287122872228732287422875228762287722878228792288022881228822288322884228852288622887228882288922890228912289222893228942289522896228972289822899229002290122902229032290422905229062290722908229092291022911229122291322914229152291622917229182291922920229212292222923229242292522926229272292822929229302293122932229332293422935229362293722938229392294022941229422294322944229452294622947229482294922950229512295222953229542295522956229572295822959229602296122962229632296422965229662296722968229692297022971229722297322974229752297622977229782297922980229812298222983229842298522986229872298822989229902299122992229932299422995229962299722998229992300023001230022300323004230052300623007230082300923010230112301223013230142301523016230172301823019230202302123022230232302423025230262302723028230292303023031230322303323034230352303623037230382303923040230412304223043230442304523046230472304823049230502305123052230532305423055230562305723058230592306023061230622306323064230652306623067230682306923070230712307223073230742307523076230772307823079230802308123082230832308423085230862308723088230892309023091230922309323094230952309623097230982309923100231012310223103231042310523106231072310823109231102311123112231132311423115231162311723118231192312023121231222312323124231252312623127231282312923130231312313223133231342313523136231372313823139231402314123142231432314423145231462314723148231492315023151231522315323154231552315623157231582315923160231612316223163231642316523166231672316823169231702317123172231732317423175231762317723178231792318023181231822318323184231852318623187231882318923190231912319223193231942319523196231972319823199232002320123202232032320423205232062320723208232092321023211232122321323214232152321623217232182321923220232212322223223232242322523226232272322823229232302323123232232332323423235232362323723238232392324023241232422324323244232452324623247232482324923250232512325223253232542325523256232572325823259232602326123262232632326423265232662326723268232692327023271232722327323274232752327623277232782327923280232812328223283232842328523286232872328823289232902329123292232932329423295232962329723298232992330023301233022330323304233052330623307233082330923310233112331223313233142331523316233172331823319233202332123322233232332423325233262332723328233292333023331233322333323334233352333623337233382333923340233412334223343233442334523346233472334823349233502335123352233532335423355233562335723358233592336023361233622336323364233652336623367233682336923370233712337223373233742337523376233772337823379233802338123382233832338423385233862338723388233892339023391233922339323394233952339623397233982339923400234012340223403234042340523406234072340823409234102341123412234132341423415234162341723418234192342023421234222342323424234252342623427234282342923430234312343223433234342343523436234372343823439234402344123442234432344423445234462344723448234492345023451234522345323454234552345623457234582345923460234612346223463234642346523466234672346823469234702347123472234732347423475234762347723478234792348023481234822348323484234852348623487234882348923490234912349223493234942349523496234972349823499235002350123502235032350423505235062350723508235092351023511235122351323514235152351623517235182351923520235212352223523235242352523526235272352823529235302353123532235332353423535235362353723538235392354023541235422354323544235452354623547235482354923550235512355223553235542355523556235572355823559235602356123562235632356423565235662356723568235692357023571235722357323574235752357623577235782357923580235812358223583235842358523586235872358823589235902359123592235932359423595235962359723598235992360023601236022360323604236052360623607236082360923610236112361223613236142361523616236172361823619236202362123622236232362423625236262362723628236292363023631236322363323634236352363623637236382363923640236412364223643236442364523646236472364823649236502365123652236532365423655236562365723658236592366023661236622366323664236652366623667236682366923670236712367223673236742367523676236772367823679236802368123682236832368423685236862368723688236892369023691236922369323694236952369623697236982369923700237012370223703237042370523706237072370823709237102371123712237132371423715237162371723718237192372023721237222372323724237252372623727237282372923730237312373223733237342373523736237372373823739237402374123742237432374423745237462374723748237492375023751237522375323754237552375623757237582375923760237612376223763237642376523766237672376823769237702377123772237732377423775237762377723778237792378023781237822378323784237852378623787237882378923790237912379223793237942379523796237972379823799238002380123802238032380423805238062380723808238092381023811238122381323814238152381623817238182381923820238212382223823238242382523826238272382823829238302383123832238332383423835238362383723838238392384023841238422384323844238452384623847238482384923850238512385223853238542385523856238572385823859238602386123862238632386423865238662386723868238692387023871238722387323874238752387623877238782387923880238812388223883238842388523886238872388823889238902389123892238932389423895238962389723898238992390023901239022390323904239052390623907239082390923910239112391223913239142391523916239172391823919239202392123922239232392423925239262392723928239292393023931239322393323934239352393623937239382393923940239412394223943239442394523946239472394823949239502395123952239532395423955239562395723958239592396023961239622396323964239652396623967239682396923970239712397223973239742397523976239772397823979239802398123982239832398423985239862398723988239892399023991239922399323994239952399623997239982399924000240012400224003240042400524006240072400824009240102401124012240132401424015240162401724018240192402024021240222402324024240252402624027240282402924030240312403224033240342403524036240372403824039240402404124042240432404424045240462404724048240492405024051240522405324054240552405624057240582405924060240612406224063240642406524066240672406824069240702407124072240732407424075240762407724078240792408024081240822408324084240852408624087240882408924090240912409224093240942409524096240972409824099241002410124102241032410424105241062410724108241092411024111241122411324114241152411624117241182411924120241212412224123241242412524126241272412824129241302413124132241332413424135241362413724138241392414024141241422414324144241452414624147241482414924150241512415224153241542415524156241572415824159241602416124162241632416424165241662416724168241692417024171241722417324174241752417624177241782417924180241812418224183241842418524186241872418824189241902419124192241932419424195241962419724198241992420024201242022420324204242052420624207242082420924210242112421224213242142421524216242172421824219242202422124222242232422424225242262422724228242292423024231242322423324234242352423624237242382423924240242412424224243242442424524246242472424824249242502425124252242532425424255242562425724258242592426024261242622426324264242652426624267242682426924270242712427224273242742427524276242772427824279242802428124282242832428424285242862428724288242892429024291242922429324294242952429624297242982429924300243012430224303243042430524306243072430824309243102431124312243132431424315243162431724318243192432024321243222432324324243252432624327243282432924330243312433224333243342433524336243372433824339243402434124342243432434424345243462434724348243492435024351243522435324354243552435624357243582435924360243612436224363243642436524366243672436824369243702437124372243732437424375243762437724378243792438024381243822438324384243852438624387243882438924390243912439224393243942439524396243972439824399244002440124402244032440424405244062440724408244092441024411244122441324414244152441624417244182441924420244212442224423244242442524426244272442824429244302443124432244332443424435244362443724438244392444024441244422444324444244452444624447244482444924450244512445224453244542445524456244572445824459244602446124462244632446424465244662446724468244692447024471244722447324474244752447624477244782447924480244812448224483244842448524486244872448824489244902449124492244932449424495244962449724498244992450024501245022450324504245052450624507245082450924510245112451224513245142451524516245172451824519245202452124522245232452424525245262452724528245292453024531245322453324534245352453624537245382453924540245412454224543245442454524546245472454824549245502455124552245532455424555245562455724558245592456024561245622456324564245652456624567245682456924570245712457224573245742457524576245772457824579245802458124582245832458424585245862458724588245892459024591245922459324594245952459624597245982459924600246012460224603246042460524606246072460824609246102461124612246132461424615246162461724618246192462024621246222462324624246252462624627246282462924630246312463224633246342463524636246372463824639246402464124642246432464424645246462464724648246492465024651246522465324654246552465624657246582465924660246612466224663246642466524666246672466824669246702467124672246732467424675246762467724678246792468024681246822468324684246852468624687246882468924690246912469224693246942469524696246972469824699247002470124702247032470424705247062470724708247092471024711247122471324714247152471624717247182471924720247212472224723247242472524726247272472824729247302473124732247332473424735247362473724738247392474024741247422474324744247452474624747247482474924750247512475224753247542475524756247572475824759247602476124762247632476424765247662476724768247692477024771247722477324774247752477624777247782477924780247812478224783247842478524786247872478824789247902479124792247932479424795247962479724798247992480024801248022480324804248052480624807248082480924810248112481224813248142481524816248172481824819248202482124822248232482424825248262482724828248292483024831248322483324834248352483624837248382483924840248412484224843248442484524846248472484824849248502485124852248532485424855248562485724858248592486024861248622486324864248652486624867248682486924870248712487224873248742487524876248772487824879248802488124882248832488424885248862488724888248892489024891248922489324894248952489624897248982489924900249012490224903249042490524906249072490824909249102491124912249132491424915249162491724918249192492024921249222492324924249252492624927249282492924930249312493224933249342493524936249372493824939249402494124942249432494424945249462494724948249492495024951249522495324954249552495624957249582495924960249612496224963249642496524966249672496824969249702497124972249732497424975249762497724978249792498024981249822498324984249852498624987249882498924990249912499224993249942499524996249972499824999250002500125002250032500425005250062500725008250092501025011250122501325014250152501625017250182501925020250212502225023250242502525026250272502825029250302503125032250332503425035250362503725038250392504025041250422504325044250452504625047250482504925050250512505225053250542505525056250572505825059250602506125062250632506425065250662506725068250692507025071250722507325074250752507625077250782507925080250812508225083250842508525086250872508825089250902509125092250932509425095250962509725098250992510025101251022510325104251052510625107251082510925110251112511225113251142511525116251172511825119251202512125122251232512425125251262512725128251292513025131251322513325134251352513625137251382513925140251412514225143251442514525146251472514825149251502515125152251532515425155251562515725158251592516025161251622516325164251652516625167251682516925170251712517225173251742517525176251772517825179251802518125182251832518425185251862518725188251892519025191251922519325194251952519625197251982519925200252012520225203252042520525206252072520825209252102521125212252132521425215252162521725218252192522025221252222522325224252252522625227252282522925230252312523225233
  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
  3794. '("src" "example" "latex" "ascii" "html" "ditaa" "dot" "r" "R")
  3795. "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
  3796. This is needed for font-lock setup.")
  3797. ;;; Miscellaneous options
  3798. (defgroup org-completion nil
  3799. "Completion in Org-mode."
  3800. :tag "Org Completion"
  3801. :group 'org)
  3802. (defcustom org-completion-use-ido nil
  3803. "Non-nil means use ido completion wherever possible.
  3804. Note that `ido-mode' must be active for this variable to be relevant.
  3805. If you decide to turn this variable on, you might well want to turn off
  3806. `org-outline-path-complete-in-steps'.
  3807. See also `org-completion-use-iswitchb'."
  3808. :group 'org-completion
  3809. :type 'boolean)
  3810. (defcustom org-completion-use-iswitchb nil
  3811. "Non-nil means use iswitchb completion wherever possible.
  3812. Note that `iswitchb-mode' must be active for this variable to be relevant.
  3813. If you decide to turn this variable on, you might well want to turn off
  3814. `org-outline-path-complete-in-steps'.
  3815. Note that this variable has only an effect if `org-completion-use-ido' is nil."
  3816. :group 'org-completion
  3817. :type 'boolean)
  3818. (defcustom org-completion-fallback-command 'hippie-expand
  3819. "The expansion command called by \\[pcomplete] in normal context.
  3820. Normal means, no org-mode-specific context."
  3821. :group 'org-completion
  3822. :type 'function)
  3823. ;;; Functions and variables from their packages
  3824. ;; Declared here to avoid compiler warnings
  3825. ;; XEmacs only
  3826. (defvar outline-mode-menu-heading)
  3827. (defvar outline-mode-menu-show)
  3828. (defvar outline-mode-menu-hide)
  3829. (defvar zmacs-regions) ; XEmacs regions
  3830. ;; Emacs only
  3831. (defvar mark-active)
  3832. ;; Various packages
  3833. (declare-function calc-eval "calc" (str &optional separator &rest args))
  3834. (declare-function calendar-forward-day "cal-move" (arg))
  3835. (declare-function calendar-goto-date "cal-move" (date))
  3836. (declare-function calendar-goto-today "cal-move" ())
  3837. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  3838. (declare-function calendar-iso-to-absolute "cal-iso" (date))
  3839. (declare-function cdlatex-compute-tables "ext:cdlatex" ())
  3840. (declare-function cdlatex-tab "ext:cdlatex" ())
  3841. (declare-function dired-get-filename
  3842. "dired"
  3843. (&optional localp no-error-if-not-filep))
  3844. (declare-function iswitchb-read-buffer
  3845. "iswitchb"
  3846. (prompt &optional default require-match start matches-set))
  3847. (declare-function org-agenda-change-all-lines
  3848. "org-agenda"
  3849. (newhead hdmarker &optional fixface just-this))
  3850. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  3851. "org-agenda"
  3852. (&optional end))
  3853. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  3854. (declare-function org-agenda-format-item
  3855. "org-agenda"
  3856. (extra txt &optional level category tags dotime noprefix
  3857. remove-re habitp))
  3858. (declare-function org-agenda-maybe-redo "org-agenda" ())
  3859. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  3860. (declare-function org-agenda-save-markers-for-cut-and-paste
  3861. "org-agenda"
  3862. (beg end))
  3863. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  3864. (declare-function org-agenda-skip "org-agenda" ())
  3865. (declare-function org-attach-reveal "org-attach" (&optional if-exists))
  3866. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  3867. (declare-function org-indent-mode "org-indent" (&optional arg))
  3868. (declare-function org-inlinetask-goto-beginning "org-inlinetask" ())
  3869. (declare-function org-inlinetask-goto-end "org-inlinetask" ())
  3870. (declare-function org-inlinetask-in-task-p "org-inlinetask" ())
  3871. (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
  3872. (declare-function orgtbl-send-table "org-table" (&optional maybe))
  3873. (declare-function parse-time-string "parse-time" (string))
  3874. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  3875. (declare-function table--at-cell-p
  3876. "table"
  3877. (position &optional object at-column))
  3878. (defvar align-mode-rules-list)
  3879. (defvar calc-embedded-close-formula)
  3880. (defvar calc-embedded-open-formula)
  3881. (defvar calc-embedded-open-mode)
  3882. (defvar font-lock-unfontify-region-function)
  3883. (defvar iswitchb-temp-buflist)
  3884. (defvar org-agenda-tags-todo-honor-ignore-options)
  3885. (defvar remember-data-file)
  3886. (defvar texmathp-why)
  3887. ;;;###autoload
  3888. (defun turn-on-orgtbl ()
  3889. "Unconditionally turn on `orgtbl-mode'."
  3890. (require 'org-table)
  3891. (orgtbl-mode 1))
  3892. (defun org-at-table-p (&optional table-type)
  3893. "Non-nil if the cursor is inside an Org table.
  3894. If TABLE-TYPE is non-nil, also check for table.el-type tables.
  3895. If `org-enable-table-editor' is nil, return nil unconditionally."
  3896. (and
  3897. org-enable-table-editor
  3898. (save-excursion
  3899. (beginning-of-line)
  3900. (org-looking-at-p (if table-type "[ \t]*[|+]" "[ \t]*|")))
  3901. (or (not (derived-mode-p 'org-mode))
  3902. (let ((e (org-element-lineage (org-element-at-point) '(table) t)))
  3903. (and e (or table-type (eq (org-element-property :type e) 'org)))))))
  3904. (define-obsolete-function-alias 'org-table-p 'org-at-table-p "Org 9.0")
  3905. (defun org-at-table.el-p ()
  3906. "Non-nil when point is at a table.el table."
  3907. (and (save-excursion (beginning-of-line) (looking-at "[ \t]*[|+]"))
  3908. (let ((element (org-element-at-point)))
  3909. (and (eq (org-element-type element) 'table)
  3910. (eq (org-element-property :type element) 'table.el)))))
  3911. (defun org-table-recognize-table.el ()
  3912. "If there is a table.el table nearby, recognize it and move into it."
  3913. (when (and org-table-tab-recognizes-table.el (org-at-table.el-p))
  3914. (beginning-of-line)
  3915. (unless (or (looking-at org-table-dataline-regexp)
  3916. (not (looking-at org-table1-hline-regexp)))
  3917. (forward-line)
  3918. (when (looking-at org-table-any-border-regexp)
  3919. (forward-line -2)))
  3920. (if (re-search-forward "|" (org-table-end t) t)
  3921. (progn
  3922. (require 'table)
  3923. (if (table--at-cell-p (point)) t
  3924. (message "recognizing table.el table...")
  3925. (table-recognize-table)
  3926. (message "recognizing table.el table...done")))
  3927. (error "This should not happen"))))
  3928. ;;; This function is not used by org core since commit 6d1e3082, Feb 2010
  3929. (make-obsolete 'org-table-recognize-table.el
  3930. "please notify the org mailing list if you use this function."
  3931. "Org 9.0")
  3932. (defun org-at-table-hline-p ()
  3933. "Non-nil when point is inside a hline in a table.
  3934. Assume point is already in a table. If `org-enable-table-editor'
  3935. is nil, return nil unconditionally."
  3936. (and org-enable-table-editor
  3937. (save-excursion
  3938. (beginning-of-line)
  3939. (looking-at org-table-hline-regexp))))
  3940. (defun org-table-map-tables (function &optional quietly)
  3941. "Apply FUNCTION to the start of all tables in the buffer."
  3942. (save-excursion
  3943. (save-restriction
  3944. (widen)
  3945. (goto-char (point-min))
  3946. (while (re-search-forward org-table-any-line-regexp nil t)
  3947. (unless quietly
  3948. (message "Mapping tables: %d%%"
  3949. (floor (* 100.0 (point)) (buffer-size))))
  3950. (beginning-of-line 1)
  3951. (when (and (looking-at org-table-line-regexp)
  3952. ;; Exclude tables in src/example/verbatim/clocktable blocks
  3953. (not (org-in-block-p '("src" "example" "verbatim" "clocktable"))))
  3954. (save-excursion (funcall function))
  3955. (or (looking-at org-table-line-regexp)
  3956. (forward-char 1)))
  3957. (re-search-forward org-table-any-border-regexp nil 1))))
  3958. (unless quietly (message "Mapping tables: done")))
  3959. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock" (beg end))
  3960. (declare-function org-clock-update-mode-line "org-clock" ())
  3961. (declare-function org-resolve-clocks "org-clock"
  3962. (&optional also-non-dangling-p prompt last-valid))
  3963. (defun org-at-TBLFM-p (&optional pos)
  3964. "Non-nil when point (or POS) is in #+TBLFM line."
  3965. (save-excursion
  3966. (goto-char (or pos (point)))
  3967. (beginning-of-line)
  3968. (and (let ((case-fold-search t)) (looking-at org-TBLFM-regexp))
  3969. (eq (org-element-type (org-element-at-point)) 'table))))
  3970. (defvar org-clock-start-time)
  3971. (defvar org-clock-marker (make-marker)
  3972. "Marker recording the last clock-in.")
  3973. (defvar org-clock-hd-marker (make-marker)
  3974. "Marker recording the last clock-in, but the headline position.")
  3975. (defvar org-clock-heading ""
  3976. "The heading of the current clock entry.")
  3977. (defun org-clock-is-active ()
  3978. "Return the buffer where the clock is currently running.
  3979. Return nil if no clock is running."
  3980. (marker-buffer org-clock-marker))
  3981. (defun org-check-running-clock ()
  3982. "Check if the current buffer contains the running clock.
  3983. If yes, offer to stop it and to save the buffer with the changes."
  3984. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3985. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  3986. (buffer-name))))
  3987. (org-clock-out)
  3988. (when (y-or-n-p "Save changed buffer?")
  3989. (save-buffer))))
  3990. (defun org-clocktable-try-shift (dir n)
  3991. "Check if this line starts a clock table, if yes, shift the time block."
  3992. (when (org-match-line "^[ \t]*#\\+BEGIN:[ \t]+clocktable\\>")
  3993. (org-clocktable-shift dir n)))
  3994. ;;;###autoload
  3995. (defun org-clock-persistence-insinuate ()
  3996. "Set up hooks for clock persistence."
  3997. (require 'org-clock)
  3998. (add-hook 'org-mode-hook 'org-clock-load)
  3999. (add-hook 'kill-emacs-hook 'org-clock-save))
  4000. (defgroup org-archive nil
  4001. "Options concerning archiving in Org-mode."
  4002. :tag "Org Archive"
  4003. :group 'org-structure)
  4004. (defcustom org-archive-location "%s_archive::"
  4005. "The location where subtrees should be archived.
  4006. The value of this variable is a string, consisting of two parts,
  4007. separated by a double-colon. The first part is a filename and
  4008. the second part is a headline.
  4009. When the filename is omitted, archiving happens in the same file.
  4010. %s in the filename will be replaced by the current file
  4011. name (without the directory part). Archiving to a different file
  4012. is useful to keep archived entries from contributing to the
  4013. Org-mode Agenda.
  4014. The archived entries will be filed as subtrees of the specified
  4015. headline. When the headline is omitted, the subtrees are simply
  4016. filed away at the end of the file, as top-level entries. Also in
  4017. the heading you can use %s to represent the file name, this can be
  4018. useful when using the same archive for a number of different files.
  4019. Here are a few examples:
  4020. \"%s_archive::\"
  4021. If the current file is Projects.org, archive in file
  4022. Projects.org_archive, as top-level trees. This is the default.
  4023. \"::* Archived Tasks\"
  4024. Archive in the current file, under the top-level headline
  4025. \"* Archived Tasks\".
  4026. \"~/org/archive.org::\"
  4027. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  4028. \"~/org/archive.org::* From %s\"
  4029. Archive in file ~/org/archive.org (absolute path), under headlines
  4030. \"From FILENAME\" where file name is the current file name.
  4031. \"~/org/datetree.org::datetree/* Finished Tasks\"
  4032. The \"datetree/\" string is special, signifying to archive
  4033. items to the datetree. Items are placed in either the CLOSED
  4034. date of the item, or the current date if there is no CLOSED date.
  4035. The heading will be a subentry to the current date. There doesn't
  4036. need to be a heading, but there always needs to be a slash after
  4037. datetree. For example, to store archived items directly in the
  4038. datetree, use \"~/org/datetree.org::datetree/\".
  4039. \"basement::** Finished Tasks\"
  4040. Archive in file ./basement (relative path), as level 3 trees
  4041. below the level 2 heading \"** Finished Tasks\".
  4042. You may set this option on a per-file basis by adding to the buffer a
  4043. line like
  4044. #+ARCHIVE: basement::** Finished Tasks
  4045. You may also define it locally for a subtree by setting an ARCHIVE property
  4046. in the entry. If such a property is found in an entry, or anywhere up
  4047. the hierarchy, it will be used."
  4048. :group 'org-archive
  4049. :type 'string)
  4050. (defcustom org-agenda-skip-archived-trees t
  4051. "Non-nil means the agenda will skip any items located in archived trees.
  4052. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  4053. variable is no longer recommended, you should leave it at the value t.
  4054. Instead, use the key `v' to cycle the archives-mode in the agenda."
  4055. :group 'org-archive
  4056. :group 'org-agenda-skip
  4057. :type 'boolean)
  4058. (defcustom org-columns-skip-archived-trees t
  4059. "Non-nil means ignore archived trees when creating column view."
  4060. :group 'org-archive
  4061. :group 'org-properties
  4062. :type 'boolean)
  4063. (defcustom org-cycle-open-archived-trees nil
  4064. "Non-nil means `org-cycle' will open archived trees.
  4065. An archived tree is a tree marked with the tag ARCHIVE.
  4066. When nil, archived trees will stay folded. You can still open them with
  4067. normal outline commands like `show-all', but not with the cycling commands."
  4068. :group 'org-archive
  4069. :group 'org-cycle
  4070. :type 'boolean)
  4071. (defcustom org-sparse-tree-open-archived-trees nil
  4072. "Non-nil means sparse tree construction shows matches in archived trees.
  4073. When nil, matches in these trees are highlighted, but the trees are kept in
  4074. collapsed state."
  4075. :group 'org-archive
  4076. :group 'org-sparse-trees
  4077. :type 'boolean)
  4078. (defcustom org-sparse-tree-default-date-type nil
  4079. "The default date type when building a sparse tree.
  4080. When this is nil, a date is a scheduled or a deadline timestamp.
  4081. Otherwise, these types are allowed:
  4082. all: all timestamps
  4083. active: only active timestamps (<...>)
  4084. inactive: only inactive timestamps ([...])
  4085. scheduled: only scheduled timestamps
  4086. deadline: only deadline timestamps"
  4087. :type '(choice (const :tag "Scheduled or deadline" nil)
  4088. (const :tag "All timestamps" all)
  4089. (const :tag "Only active timestamps" active)
  4090. (const :tag "Only inactive timestamps" inactive)
  4091. (const :tag "Only scheduled timestamps" scheduled)
  4092. (const :tag "Only deadline timestamps" deadline)
  4093. (const :tag "Only closed timestamps" closed))
  4094. :version "25.1"
  4095. :package-version '(Org . "8.3")
  4096. :group 'org-sparse-trees)
  4097. (defun org-cycle-hide-archived-subtrees (state)
  4098. "Re-hide all archived subtrees after a visibility state change."
  4099. (when (and (not org-cycle-open-archived-trees)
  4100. (not (memq state '(overview folded))))
  4101. (save-excursion
  4102. (let* ((globalp (memq state '(contents all)))
  4103. (beg (if globalp (point-min) (point)))
  4104. (end (if globalp (point-max) (org-end-of-subtree t))))
  4105. (org-hide-archived-subtrees beg end)
  4106. (goto-char beg)
  4107. (when (looking-at-p (concat ".*:" org-archive-tag ":"))
  4108. (message "%s" (substitute-command-keys
  4109. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  4110. (defun org-force-cycle-archived ()
  4111. "Cycle subtree even if it is archived."
  4112. (interactive)
  4113. (setq this-command 'org-cycle)
  4114. (let ((org-cycle-open-archived-trees t))
  4115. (call-interactively 'org-cycle)))
  4116. (defun org-hide-archived-subtrees (beg end)
  4117. "Re-hide all archived subtrees after a visibility state change."
  4118. (org-with-wide-buffer
  4119. (let ((case-fold-search nil)
  4120. (re (concat org-outline-regexp-bol ".*:" org-archive-tag ":")))
  4121. (goto-char beg)
  4122. ;; Include headline point is currently on.
  4123. (beginning-of-line)
  4124. (while (and (< (point) end) (re-search-forward re end t))
  4125. (when (member org-archive-tag (org-get-tags))
  4126. (org-flag-subtree t)
  4127. (org-end-of-subtree t))))))
  4128. (declare-function outline-end-of-heading "outline" ())
  4129. (declare-function outline-flag-region "outline" (from to flag))
  4130. (defun org-flag-subtree (flag)
  4131. (save-excursion
  4132. (org-back-to-heading t)
  4133. (outline-end-of-heading)
  4134. (outline-flag-region (point)
  4135. (progn (org-end-of-subtree t) (point))
  4136. flag)))
  4137. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  4138. ;; Declare Column View Code
  4139. (declare-function org-columns-number-to-string "org-colview" (n fmt &optional printf))
  4140. (declare-function org-columns-get-format-and-top-level "org-colview" ())
  4141. (declare-function org-columns-compute "org-colview" (property))
  4142. ;; Declare ID code
  4143. (declare-function org-id-store-link "org-id")
  4144. (declare-function org-id-locations-load "org-id")
  4145. (declare-function org-id-locations-save "org-id")
  4146. (defvar org-id-track-globally)
  4147. ;;; Variables for pre-computed regular expressions, all buffer local
  4148. (defvar-local org-todo-regexp nil
  4149. "Matches any of the TODO state keywords.")
  4150. (defvar-local org-not-done-regexp nil
  4151. "Matches any of the TODO state keywords except the last one.")
  4152. (defvar-local org-not-done-heading-regexp nil
  4153. "Matches a TODO headline that is not done.")
  4154. (defvar-local org-todo-line-regexp nil
  4155. "Matches a headline and puts TODO state into group 2 if present.")
  4156. (defvar-local org-complex-heading-regexp nil
  4157. "Matches a headline and puts everything into groups:
  4158. group 1: the stars
  4159. group 2: The todo keyword, maybe
  4160. group 3: Priority cookie
  4161. group 4: True headline
  4162. group 5: Tags")
  4163. (defvar-local org-complex-heading-regexp-format nil
  4164. "Printf format to make regexp to match an exact headline.
  4165. This regexp will match the headline of any node which has the
  4166. exact headline text that is put into the format, but may have any
  4167. TODO state, priority and tags.")
  4168. (defvar-local org-todo-line-tags-regexp nil
  4169. "Matches a headline and puts TODO state into group 2 if present.
  4170. Also put tags into group 4 if tags are present.")
  4171. (defconst org-plain-time-of-day-regexp
  4172. (concat
  4173. "\\(\\<[012]?[0-9]"
  4174. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4175. "\\(--?"
  4176. "\\(\\<[012]?[0-9]"
  4177. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4178. "\\)?")
  4179. "Regular expression to match a plain time or time range.
  4180. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  4181. groups carry important information:
  4182. 0 the full match
  4183. 1 the first time, range or not
  4184. 8 the second time, if it is a range.")
  4185. (defconst org-plain-time-extension-regexp
  4186. (concat
  4187. "\\(\\<[012]?[0-9]"
  4188. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  4189. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  4190. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  4191. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  4192. groups carry important information:
  4193. 0 the full match
  4194. 7 hours of duration
  4195. 9 minutes of duration")
  4196. (defconst org-stamp-time-of-day-regexp
  4197. (concat
  4198. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  4199. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  4200. "\\(--?"
  4201. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  4202. "Regular expression to match a timestamp time or time range.
  4203. After a match, the following groups carry important information:
  4204. 0 the full match
  4205. 1 date plus weekday, for back referencing to make sure both times are on the same day
  4206. 2 the first time, range or not
  4207. 4 the second time, if it is a range.")
  4208. (defconst org-startup-options
  4209. '(("fold" org-startup-folded t)
  4210. ("overview" org-startup-folded t)
  4211. ("nofold" org-startup-folded nil)
  4212. ("showall" org-startup-folded nil)
  4213. ("showeverything" org-startup-folded showeverything)
  4214. ("content" org-startup-folded content)
  4215. ("indent" org-startup-indented t)
  4216. ("noindent" org-startup-indented nil)
  4217. ("hidestars" org-hide-leading-stars t)
  4218. ("showstars" org-hide-leading-stars nil)
  4219. ("odd" org-odd-levels-only t)
  4220. ("oddeven" org-odd-levels-only nil)
  4221. ("align" org-startup-align-all-tables t)
  4222. ("noalign" org-startup-align-all-tables nil)
  4223. ("inlineimages" org-startup-with-inline-images t)
  4224. ("noinlineimages" org-startup-with-inline-images nil)
  4225. ("latexpreview" org-startup-with-latex-preview t)
  4226. ("nolatexpreview" org-startup-with-latex-preview nil)
  4227. ("customtime" org-display-custom-times t)
  4228. ("logdone" org-log-done time)
  4229. ("lognotedone" org-log-done note)
  4230. ("nologdone" org-log-done nil)
  4231. ("lognoteclock-out" org-log-note-clock-out t)
  4232. ("nolognoteclock-out" org-log-note-clock-out nil)
  4233. ("logrepeat" org-log-repeat state)
  4234. ("lognoterepeat" org-log-repeat note)
  4235. ("logdrawer" org-log-into-drawer t)
  4236. ("nologdrawer" org-log-into-drawer nil)
  4237. ("logstatesreversed" org-log-states-order-reversed t)
  4238. ("nologstatesreversed" org-log-states-order-reversed nil)
  4239. ("nologrepeat" org-log-repeat nil)
  4240. ("logreschedule" org-log-reschedule time)
  4241. ("lognotereschedule" org-log-reschedule note)
  4242. ("nologreschedule" org-log-reschedule nil)
  4243. ("logredeadline" org-log-redeadline time)
  4244. ("lognoteredeadline" org-log-redeadline note)
  4245. ("nologredeadline" org-log-redeadline nil)
  4246. ("logrefile" org-log-refile time)
  4247. ("lognoterefile" org-log-refile note)
  4248. ("nologrefile" org-log-refile nil)
  4249. ("fninline" org-footnote-define-inline t)
  4250. ("nofninline" org-footnote-define-inline nil)
  4251. ("fnlocal" org-footnote-section nil)
  4252. ("fnauto" org-footnote-auto-label t)
  4253. ("fnprompt" org-footnote-auto-label nil)
  4254. ("fnconfirm" org-footnote-auto-label confirm)
  4255. ("fnplain" org-footnote-auto-label plain)
  4256. ("fnadjust" org-footnote-auto-adjust t)
  4257. ("nofnadjust" org-footnote-auto-adjust nil)
  4258. ("constcgs" constants-unit-system cgs)
  4259. ("constSI" constants-unit-system SI)
  4260. ("noptag" org-tag-persistent-alist nil)
  4261. ("hideblocks" org-hide-block-startup t)
  4262. ("nohideblocks" org-hide-block-startup nil)
  4263. ("beamer" org-startup-with-beamer-mode t)
  4264. ("entitiespretty" org-pretty-entities t)
  4265. ("entitiesplain" org-pretty-entities nil))
  4266. "Variable associated with STARTUP options for org-mode.
  4267. Each element is a list of three items: the startup options (as written
  4268. in the #+STARTUP line), the corresponding variable, and the value to set
  4269. this variable to if the option is found. An optional forth element PUSH
  4270. means to push this value onto the list in the variable.")
  4271. (defcustom org-group-tags t
  4272. "When non-nil (the default), use group tags.
  4273. This can be turned on/off through `org-toggle-tags-groups'."
  4274. :group 'org-tags
  4275. :group 'org-startup
  4276. :type 'boolean)
  4277. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  4278. (defun org-toggle-tags-groups ()
  4279. "Toggle support for group tags.
  4280. Support for group tags is controlled by the option
  4281. `org-group-tags', which is non-nil by default."
  4282. (interactive)
  4283. (setq org-group-tags (not org-group-tags))
  4284. (cond ((and (derived-mode-p 'org-agenda-mode)
  4285. org-group-tags)
  4286. (org-agenda-redo))
  4287. ((derived-mode-p 'org-mode)
  4288. (let ((org-inhibit-startup t)) (org-mode))))
  4289. (message "Groups tags support has been turned %s"
  4290. (if org-group-tags "on" "off")))
  4291. (defun org-set-regexps-and-options (&optional tags-only)
  4292. "Precompute regular expressions used in the current buffer.
  4293. When optional argument TAGS-ONLY is non-nil, only compute tags
  4294. related expressions."
  4295. (when (derived-mode-p 'org-mode)
  4296. (let ((alist (org--setup-collect-keywords
  4297. (org-make-options-regexp
  4298. (append '("FILETAGS" "TAGS" "SETUPFILE")
  4299. (and (not tags-only)
  4300. '("ARCHIVE" "CATEGORY" "COLUMNS" "CONSTANTS"
  4301. "LINK" "OPTIONS" "PRIORITIES" "PROPERTY"
  4302. "SEQ_TODO" "STARTUP" "TODO" "TYP_TODO")))))))
  4303. (org--setup-process-tags
  4304. (cdr (assq 'tags alist)) (cdr (assq 'filetags alist)))
  4305. (unless tags-only
  4306. ;; File properties.
  4307. (setq-local org-file-properties (cdr (assq 'property alist)))
  4308. ;; Archive location.
  4309. (let ((archive (cdr (assq 'archive alist))))
  4310. (when archive (setq-local org-archive-location archive)))
  4311. ;; Category.
  4312. (let ((cat (org-string-nw-p (cdr (assq 'category alist)))))
  4313. (when cat
  4314. (setq-local org-category (intern cat))
  4315. (setq-local org-file-properties
  4316. (org--update-property-plist
  4317. "CATEGORY" cat org-file-properties))))
  4318. ;; Columns.
  4319. (let ((column (cdr (assq 'columns alist))))
  4320. (when column (setq-local org-columns-default-format column)))
  4321. ;; Constants.
  4322. (setq org-table-formula-constants-local (cdr (assq 'constants alist)))
  4323. ;; Link abbreviations.
  4324. (let ((links (cdr (assq 'link alist))))
  4325. (when links (setq org-link-abbrev-alist-local (nreverse links))))
  4326. ;; Priorities.
  4327. (let ((priorities (cdr (assq 'priorities alist))))
  4328. (when priorities
  4329. (setq-local org-highest-priority (nth 0 priorities))
  4330. (setq-local org-lowest-priority (nth 1 priorities))
  4331. (setq-local org-default-priority (nth 2 priorities))))
  4332. ;; Scripts.
  4333. (let ((scripts (assq 'scripts alist)))
  4334. (when scripts
  4335. (setq-local org-use-sub-superscripts (cdr scripts))))
  4336. ;; Startup options.
  4337. (let ((startup (cdr (assq 'startup alist))))
  4338. (dolist (option startup)
  4339. (let ((entry (assoc-string option org-startup-options t)))
  4340. (when entry
  4341. (let ((var (nth 1 entry))
  4342. (val (nth 2 entry)))
  4343. (if (not (nth 3 entry)) (set (make-local-variable var) val)
  4344. (unless (listp (symbol-value var))
  4345. (set (make-local-variable var) nil))
  4346. (add-to-list var val)))))))
  4347. ;; TODO keywords.
  4348. (setq-local org-todo-kwd-alist nil)
  4349. (setq-local org-todo-key-alist nil)
  4350. (setq-local org-todo-key-trigger nil)
  4351. (setq-local org-todo-keywords-1 nil)
  4352. (setq-local org-done-keywords nil)
  4353. (setq-local org-todo-heads nil)
  4354. (setq-local org-todo-sets nil)
  4355. (setq-local org-todo-log-states nil)
  4356. (let ((todo-sequences
  4357. (or (nreverse (cdr (assq 'todo alist)))
  4358. (let ((d (default-value 'org-todo-keywords)))
  4359. (if (not (stringp (car d))) d
  4360. ;; XXX: Backward compatibility code.
  4361. (list (cons org-todo-interpretation d)))))))
  4362. (dolist (sequence todo-sequences)
  4363. (let* ((sequence (or (run-hook-with-args-until-success
  4364. 'org-todo-setup-filter-hook sequence)
  4365. sequence))
  4366. (sequence-type (car sequence))
  4367. (keywords (cdr sequence))
  4368. (sep (member "|" keywords))
  4369. names alist)
  4370. (dolist (k (remove "|" keywords))
  4371. (unless (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$"
  4372. k)
  4373. (error "Invalid TODO keyword %s" k))
  4374. (let ((name (match-string 1 k))
  4375. (key (match-string 2 k))
  4376. (log (org-extract-log-state-settings k)))
  4377. (push name names)
  4378. (push (cons name (and key (string-to-char key))) alist)
  4379. (when log (push log org-todo-log-states))))
  4380. (let* ((names (nreverse names))
  4381. (done (if sep (org-remove-keyword-keys (cdr sep))
  4382. (last names)))
  4383. (head (car names))
  4384. (tail (list sequence-type head (car done) (org-last done))))
  4385. (add-to-list 'org-todo-heads head 'append)
  4386. (push names org-todo-sets)
  4387. (setq org-done-keywords (append org-done-keywords done nil))
  4388. (setq org-todo-keywords-1 (append org-todo-keywords-1 names nil))
  4389. (setq org-todo-key-alist
  4390. (append org-todo-key-alist
  4391. (and alist
  4392. (append '((:startgroup))
  4393. (nreverse alist)
  4394. '((:endgroup))))))
  4395. (dolist (k names) (push (cons k tail) org-todo-kwd-alist))))))
  4396. (setq org-todo-sets (nreverse org-todo-sets)
  4397. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  4398. org-todo-key-trigger (delq nil (mapcar #'cdr org-todo-key-alist))
  4399. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist))
  4400. ;; Compute the regular expressions and other local variables.
  4401. ;; Using `org-outline-regexp-bol' would complicate them much,
  4402. ;; because of the fixed white space at the end of that string.
  4403. (unless org-done-keywords
  4404. (setq org-done-keywords
  4405. (and org-todo-keywords-1 (last org-todo-keywords-1))))
  4406. (setq org-not-done-keywords
  4407. (org-delete-all org-done-keywords
  4408. (copy-sequence org-todo-keywords-1))
  4409. org-todo-regexp (regexp-opt org-todo-keywords-1 t)
  4410. org-not-done-regexp (regexp-opt org-not-done-keywords t)
  4411. org-not-done-heading-regexp
  4412. (format org-heading-keyword-regexp-format org-not-done-regexp)
  4413. org-todo-line-regexp
  4414. (format org-heading-keyword-maybe-regexp-format org-todo-regexp)
  4415. org-complex-heading-regexp
  4416. (concat "^\\(\\*+\\)"
  4417. "\\(?: +" org-todo-regexp "\\)?"
  4418. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4419. "\\(?: +\\(.*?\\)\\)??"
  4420. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?")
  4421. "[ \t]*$")
  4422. org-complex-heading-regexp-format
  4423. (concat "^\\(\\*+\\)"
  4424. "\\(?: +" org-todo-regexp "\\)?"
  4425. "\\(?: +\\(\\[#.\\]\\)\\)?"
  4426. "\\(?: +"
  4427. ;; Stats cookies can be stuck to body.
  4428. "\\(?:\\[[0-9%%/]+\\] *\\)*"
  4429. "\\(%s\\)"
  4430. "\\(?: *\\[[0-9%%/]+\\]\\)*"
  4431. "\\)"
  4432. (org-re "\\(?:[ \t]+\\(:[[:alnum:]_@#%%:]+:\\)\\)?")
  4433. "[ \t]*$")
  4434. org-todo-line-tags-regexp
  4435. (concat "^\\(\\*+\\)"
  4436. "\\(?: +" org-todo-regexp "\\)?"
  4437. "\\(?: +\\(.*?\\)\\)??"
  4438. (org-re "\\(?:[ \t]+\\(:[[:alnum:]:_@#%]+:\\)\\)?")
  4439. "[ \t]*$"))
  4440. (org-compute-latex-and-related-regexp)))))
  4441. (defun org--setup-collect-keywords (regexp &optional files alist)
  4442. "Return setup keywords values as an alist.
  4443. REGEXP matches a subset of setup keywords. FILES is a list of
  4444. file names already visited. It is used to avoid circular setup
  4445. files. ALIST, when non-nil, is the alist computed so far.
  4446. Return value contains the following keys: `archive', `category',
  4447. `columns', `constants', `filetags', `link', `priorities',
  4448. `property', `scripts', `startup', `tags' and `todo'."
  4449. (org-with-wide-buffer
  4450. (goto-char (point-min))
  4451. (let ((case-fold-search t))
  4452. (while (re-search-forward regexp nil t)
  4453. (let ((element (org-element-at-point)))
  4454. (when (eq (org-element-type element) 'keyword)
  4455. (let ((key (org-element-property :key element))
  4456. (value (org-element-property :value element)))
  4457. (cond
  4458. ((equal key "ARCHIVE")
  4459. (when (org-string-nw-p value)
  4460. (push (cons 'archive value) alist)))
  4461. ((equal key "CATEGORY") (push (cons 'category value) alist))
  4462. ((equal key "COLUMNS") (push (cons 'columns value) alist))
  4463. ((equal key "CONSTANTS")
  4464. (let* ((constants (assq 'constants alist))
  4465. (store (cdr constants)))
  4466. (dolist (pair (org-split-string value))
  4467. (when (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)"
  4468. pair)
  4469. (let* ((name (match-string 1 pair))
  4470. (value (match-string 2 pair))
  4471. (old (assoc name store)))
  4472. (if old (setcdr old value)
  4473. (push (cons name value) store)))))
  4474. (if constants (setcdr constants store)
  4475. (push (cons 'constants store) alist))))
  4476. ((equal key "FILETAGS")
  4477. (when (org-string-nw-p value)
  4478. (let ((old (assq 'filetags alist))
  4479. (new (apply #'nconc
  4480. (mapcar (lambda (x) (org-split-string x ":"))
  4481. (org-split-string value)))))
  4482. (if old (setcdr old (append new (cdr old)))
  4483. (push (cons 'filetags new) alist)))))
  4484. ((equal key "LINK")
  4485. (when (string-match "\\`\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  4486. (let ((links (assq 'link alist))
  4487. (pair (cons (org-match-string-no-properties 1 value)
  4488. (org-match-string-no-properties 2 value))))
  4489. (if links (push pair (cdr links))
  4490. (push (list 'link pair) alist)))))
  4491. ((equal key "OPTIONS")
  4492. (when (and (org-string-nw-p value)
  4493. (string-match "\\^:\\(t\\|nil\\|{}\\)" value))
  4494. (push (cons 'scripts (read (match-string 1 value))) alist)))
  4495. ((equal key "PRIORITIES")
  4496. (push (cons 'priorities
  4497. (let ((prio (org-split-string value)))
  4498. (if (< (length prio) 3) '(?A ?C ?B)
  4499. (mapcar #'string-to-char prio))))
  4500. alist))
  4501. ((equal key "PROPERTY")
  4502. (when (string-match "\\(\\S-+\\)[ \t]+\\(.*\\)" value)
  4503. (let* ((property (assq 'property alist))
  4504. (value (org--update-property-plist
  4505. (org-match-string-no-properties 1 value)
  4506. (org-match-string-no-properties 2 value)
  4507. (cdr property))))
  4508. (if property (setcdr property value)
  4509. (push (cons 'property value) alist)))))
  4510. ((equal key "STARTUP")
  4511. (let ((startup (assq 'startup alist)))
  4512. (if startup
  4513. (setcdr startup
  4514. (append (cdr startup) (org-split-string value)))
  4515. (push (cons 'startup (org-split-string value)) alist))))
  4516. ((equal key "TAGS")
  4517. (let ((tag-cell (assq 'tags alist)))
  4518. (if tag-cell
  4519. (setcdr tag-cell
  4520. (append (cdr tag-cell)
  4521. '("\\n")
  4522. (org-split-string value)))
  4523. (push (cons 'tags (org-split-string value)) alist))))
  4524. ((member key '("TODO" "SEQ_TODO" "TYP_TODO"))
  4525. (let ((todo (assq 'todo alist))
  4526. (value (cons (if (equal key "TYP_TODO") 'type 'sequence)
  4527. (org-split-string value))))
  4528. (if todo (push value (cdr todo))
  4529. (push (list 'todo value) alist))))
  4530. ((equal key "SETUPFILE")
  4531. (unless buffer-read-only ; Do not check in Gnus messages.
  4532. (let ((f (and (org-string-nw-p value)
  4533. (expand-file-name
  4534. (org-remove-double-quotes value)))))
  4535. (when (and f (file-readable-p f) (not (member f files)))
  4536. (with-temp-buffer
  4537. (setq default-directory (file-name-directory f))
  4538. (insert-file-contents f)
  4539. (setq alist
  4540. ;; Fake Org mode to benefit from cache
  4541. ;; without recurring needlessly.
  4542. (let ((major-mode 'org-mode))
  4543. (org--setup-collect-keywords
  4544. regexp (cons f files) alist)))))))))))))))
  4545. alist)
  4546. (defun org--setup-process-tags (tags filetags)
  4547. "Precompute variables used for tags.
  4548. TAGS is a list of tags and tag group symbols, as strings.
  4549. FILETAGS is a list of tags, as strings."
  4550. ;; Process the file tags.
  4551. (setq-local org-file-tags
  4552. (mapcar #'org-add-prop-inherited filetags))
  4553. ;; Provide default tags if no local tags are found.
  4554. (when (and (not tags) org-tag-alist)
  4555. (setq tags
  4556. (mapcar (lambda (tag)
  4557. (case (car tag)
  4558. (:startgroup "{")
  4559. (:endgroup "}")
  4560. (:startgrouptag "[")
  4561. (:endgrouptag "]")
  4562. (:grouptags ":")
  4563. (:newline "\\n")
  4564. (otherwise (concat (car tag)
  4565. (and (characterp (cdr tag))
  4566. (format "(%c)" (cdr tag)))))))
  4567. org-tag-alist)))
  4568. ;; Process the tags.
  4569. (setq-local org-tag-groups-alist nil)
  4570. (setq-local org-tag-alist nil)
  4571. (let (group-flag)
  4572. (while tags
  4573. (let ((e (car tags)))
  4574. (setq tags (cdr tags))
  4575. (cond
  4576. ((equal e "{")
  4577. (push '(:startgroup) org-tag-alist)
  4578. (when (equal (nth 1 tags) ":") (setq group-flag t)))
  4579. ((equal e "}")
  4580. (push '(:endgroup) org-tag-alist)
  4581. (setq group-flag nil))
  4582. ((equal e "[")
  4583. (push '(:startgrouptag) org-tag-alist)
  4584. (when (equal (nth 1 tags) ":") (setq group-flag t)))
  4585. ((equal e "]")
  4586. (push '(:endgrouptag) org-tag-alist)
  4587. (setq group-flag nil))
  4588. ((equal e ":")
  4589. (push '(:grouptags) org-tag-alist)
  4590. (setq group-flag 'append))
  4591. ((equal e "\\n") (push '(:newline) org-tag-alist))
  4592. ((string-match
  4593. (org-re (concat "\\`\\([[:alnum:]_@#%]+"
  4594. "\\|{.+?}\\)" ; regular expression
  4595. "\\(?:(\\(.\\))\\)?\\'")) e)
  4596. (let ((tag (match-string 1 e))
  4597. (key (and (match-beginning 2)
  4598. (string-to-char (match-string 2 e)))))
  4599. (cond ((eq group-flag 'append)
  4600. (setcar org-tag-groups-alist
  4601. (append (car org-tag-groups-alist) (list tag))))
  4602. (group-flag (push (list tag) org-tag-groups-alist)))
  4603. ;; Push all tags in groups, no matter if they already exist.
  4604. (unless (and (not group-flag) (assoc tag org-tag-alist))
  4605. (push (cons tag key) org-tag-alist))))))))
  4606. (setq org-tag-alist (nreverse org-tag-alist)))
  4607. (defun org-file-contents (file &optional noerror)
  4608. "Return the contents of FILE, as a string."
  4609. (if (and file (file-readable-p file))
  4610. (with-temp-buffer
  4611. (insert-file-contents file)
  4612. (buffer-string))
  4613. (funcall (if noerror 'message 'error)
  4614. "Cannot read file \"%s\"%s"
  4615. file
  4616. (let ((from (buffer-file-name (buffer-base-buffer))))
  4617. (if from (concat " (referenced in file \"" from "\")") "")))))
  4618. (defun org-extract-log-state-settings (x)
  4619. "Extract the log state setting from a TODO keyword string.
  4620. This will extract info from a string like \"WAIT(w@/!)\"."
  4621. (let (kw key log1 log2)
  4622. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  4623. (setq kw (match-string 1 x)
  4624. key (and (match-end 2) (match-string 2 x))
  4625. log1 (and (match-end 3) (match-string 3 x))
  4626. log2 (and (match-end 4) (match-string 4 x)))
  4627. (and (or log1 log2)
  4628. (list kw
  4629. (and log1 (if (equal log1 "!") 'time 'note))
  4630. (and log2 (if (equal log2 "!") 'time 'note)))))))
  4631. (defun org-remove-keyword-keys (list)
  4632. "Remove a pair of parenthesis at the end of each string in LIST."
  4633. (mapcar (lambda (x)
  4634. (if (string-match "(.*)$" x)
  4635. (substring x 0 (match-beginning 0))
  4636. x))
  4637. list))
  4638. (defun org-assign-fast-keys (alist)
  4639. "Assign fast keys to a keyword-key alist.
  4640. Respect keys that are already there."
  4641. (let (new e (alt ?0))
  4642. (while (setq e (pop alist))
  4643. (if (or (memq (car e) '(:newline :grouptags :endgroup :startgroup))
  4644. (cdr e)) ;; Key already assigned.
  4645. (push e new)
  4646. (let ((clist (string-to-list (downcase (car e))))
  4647. (used (append new alist)))
  4648. (when (= (car clist) ?@)
  4649. (pop clist))
  4650. (while (and clist (rassoc (car clist) used))
  4651. (pop clist))
  4652. (unless clist
  4653. (while (rassoc alt used)
  4654. (incf alt)))
  4655. (push (cons (car e) (or (car clist) alt)) new))))
  4656. (nreverse new)))
  4657. ;;; Some variables used in various places
  4658. (defvar org-window-configuration nil
  4659. "Used in various places to store a window configuration.")
  4660. (defvar org-selected-window nil
  4661. "Used in various places to store a window configuration.")
  4662. (defvar org-finish-function nil
  4663. "Function to be called when `C-c C-c' is used.
  4664. This is for getting out of special buffers like capture.")
  4665. ;; FIXME: Occasionally check by commenting these, to make sure
  4666. ;; no other functions uses these, forgetting to let-bind them.
  4667. (org-no-warnings (defvar entry)) ;; unprefixed, from calendar.el
  4668. (defvar org-last-state)
  4669. (org-no-warnings (defvar date)) ;; unprefixed, from calendar.el
  4670. ;; Defined somewhere in this file, but used before definition.
  4671. (defvar org-entities) ;; defined in org-entities.el
  4672. (defvar org-struct-menu)
  4673. (defvar org-org-menu)
  4674. (defvar org-tbl-menu)
  4675. ;;;; Define the Org-mode
  4676. ;; We use a before-change function to check if a table might need
  4677. ;; an update.
  4678. (defvar org-table-may-need-update t
  4679. "Indicates that a table might need an update.
  4680. This variable is set by `org-before-change-function'.
  4681. `org-table-align' sets it back to nil.")
  4682. (defun org-before-change-function (beg end)
  4683. "Every change indicates that a table might need an update."
  4684. (setq org-table-may-need-update t))
  4685. (defvar org-mode-map)
  4686. (defvar org-inhibit-startup-visibility-stuff nil) ; Dynamically-scoped param.
  4687. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  4688. (defvar org-inhibit-logging nil) ; Dynamically-scoped param.
  4689. (defvar org-inhibit-blocking nil) ; Dynamically-scoped param.
  4690. (defvar org-table-buffer-is-an nil)
  4691. (defvar bidi-paragraph-direction)
  4692. (defvar buffer-face-mode-face)
  4693. (require 'outline)
  4694. (when (and (not (keymapp outline-mode-map)) (featurep 'allout))
  4695. (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"))
  4696. (require 'noutline "noutline" 'noerror) ;; stock XEmacs does not have it
  4697. ;; Other stuff we need.
  4698. (require 'time-date)
  4699. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  4700. (require 'easymenu)
  4701. (autoload 'easy-menu-add "easymenu")
  4702. (require 'overlay)
  4703. ;; (require 'org-macs) moved higher up in the file before it is first used
  4704. (require 'org-entities)
  4705. ;; (require 'org-compat) moved higher up in the file before it is first used
  4706. (require 'org-faces)
  4707. (require 'org-list)
  4708. (require 'org-pcomplete)
  4709. (require 'org-src)
  4710. (require 'org-footnote)
  4711. (require 'org-macro)
  4712. ;; babel
  4713. (require 'ob)
  4714. ;;;###autoload
  4715. (define-derived-mode org-mode outline-mode "Org"
  4716. "Outline-based notes management and organizer, alias
  4717. \"Carsten's outline-mode for keeping track of everything.\"
  4718. Org-mode develops organizational tasks around a NOTES file which
  4719. contains information about projects as plain text. Org-mode is
  4720. implemented on top of outline-mode, which is ideal to keep the content
  4721. of large files well structured. It supports ToDo items, deadlines and
  4722. time stamps, which magically appear in the diary listing of the Emacs
  4723. calendar. Tables are easily created with a built-in table editor.
  4724. Plain text URL-like links connect to websites, emails (VM), Usenet
  4725. messages (Gnus), BBDB entries, and any files related to the project.
  4726. For printing and sharing of notes, an Org-mode file (or a part of it)
  4727. can be exported as a structured ASCII or HTML file.
  4728. The following commands are available:
  4729. \\{org-mode-map}"
  4730. ;; Get rid of Outline menus, they are not needed
  4731. ;; Need to do this here because define-derived-mode sets up
  4732. ;; the keymap so late. Still, it is a waste to call this each time
  4733. ;; we switch another buffer into org-mode.
  4734. (if (featurep 'xemacs)
  4735. (when (boundp 'outline-mode-menu-heading)
  4736. ;; Assume this is Greg's port, it uses easymenu
  4737. (easy-menu-remove outline-mode-menu-heading)
  4738. (easy-menu-remove outline-mode-menu-show)
  4739. (easy-menu-remove outline-mode-menu-hide))
  4740. (define-key org-mode-map [menu-bar headings] 'undefined)
  4741. (define-key org-mode-map [menu-bar hide] 'undefined)
  4742. (define-key org-mode-map [menu-bar show] 'undefined))
  4743. (org-load-modules-maybe)
  4744. (when (featurep 'xemacs)
  4745. (easy-menu-add org-org-menu)
  4746. (easy-menu-add org-tbl-menu))
  4747. (org-install-agenda-files-menu)
  4748. (when org-descriptive-links (add-to-invisibility-spec '(org-link)))
  4749. (add-to-invisibility-spec '(org-cwidth))
  4750. (add-to-invisibility-spec '(org-hide-block . t))
  4751. (when (featurep 'xemacs)
  4752. (setq-local line-move-ignore-invisible t))
  4753. (setq-local outline-regexp org-outline-regexp)
  4754. (setq-local outline-level 'org-outline-level)
  4755. (setq bidi-paragraph-direction 'left-to-right)
  4756. (when (and org-ellipsis
  4757. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  4758. (fboundp 'make-glyph-code))
  4759. (unless org-display-table
  4760. (setq org-display-table (make-display-table)))
  4761. (set-display-table-slot
  4762. org-display-table 4
  4763. (vconcat (mapcar
  4764. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  4765. org-ellipsis)))
  4766. (if (stringp org-ellipsis) org-ellipsis "..."))))
  4767. (setq buffer-display-table org-display-table))
  4768. (org-set-regexps-and-options)
  4769. (org-set-font-lock-defaults)
  4770. (when (and org-tag-faces (not org-tags-special-faces-re))
  4771. ;; tag faces set outside customize.... force initialization.
  4772. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  4773. ;; Calc embedded
  4774. (setq-local calc-embedded-open-mode "# ")
  4775. ;; Modify a few syntax entries
  4776. (modify-syntax-entry ?@ "w")
  4777. (modify-syntax-entry ?\" "\"")
  4778. (modify-syntax-entry ?\\ "_")
  4779. (modify-syntax-entry ?~ "_")
  4780. (setq-local font-lock-unfontify-region-function 'org-unfontify-region)
  4781. ;; Activate before-change-function
  4782. (setq-local org-table-may-need-update t)
  4783. (org-add-hook 'before-change-functions 'org-before-change-function nil 'local)
  4784. ;; Check for running clock before killing a buffer
  4785. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  4786. ;; Initialize macros templates.
  4787. (org-macro-initialize-templates)
  4788. ;; Initialize radio targets.
  4789. (org-update-radio-target-regexp)
  4790. ;; Indentation.
  4791. (setq-local indent-line-function 'org-indent-line)
  4792. (setq-local indent-region-function 'org-indent-region)
  4793. ;; Filling and auto-filling.
  4794. (org-setup-filling)
  4795. ;; Comments.
  4796. (org-setup-comments-handling)
  4797. ;; Initialize cache.
  4798. (org-element-cache-reset)
  4799. ;; Beginning/end of defun
  4800. (setq-local beginning-of-defun-function 'org-backward-element)
  4801. (setq-local end-of-defun-function
  4802. (lambda ()
  4803. (if (not (org-at-heading-p))
  4804. (org-forward-element)
  4805. (org-forward-element)
  4806. (forward-char -1))))
  4807. ;; Next error for sparse trees
  4808. (setq-local next-error-function 'org-occur-next-match)
  4809. ;; Make sure dependence stuff works reliably, even for users who set it
  4810. ;; too late :-(
  4811. (if org-enforce-todo-dependencies
  4812. (add-hook 'org-blocker-hook
  4813. 'org-block-todo-from-children-or-siblings-or-parent)
  4814. (remove-hook 'org-blocker-hook
  4815. 'org-block-todo-from-children-or-siblings-or-parent))
  4816. (if org-enforce-todo-checkbox-dependencies
  4817. (add-hook 'org-blocker-hook
  4818. 'org-block-todo-from-checkboxes)
  4819. (remove-hook 'org-blocker-hook
  4820. 'org-block-todo-from-checkboxes))
  4821. ;; Align options lines
  4822. (setq-local
  4823. align-mode-rules-list
  4824. '((org-in-buffer-settings
  4825. (regexp . "^[ \t]*#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  4826. (modes . '(org-mode)))))
  4827. ;; Imenu
  4828. (setq-local imenu-create-index-function 'org-imenu-get-tree)
  4829. ;; Make isearch reveal context
  4830. (if (or (featurep 'xemacs)
  4831. (not (boundp 'outline-isearch-open-invisible-function)))
  4832. ;; Emacs 21 and XEmacs make use of the hook
  4833. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  4834. ;; Emacs 22 deals with this through a special variable
  4835. (setq-local outline-isearch-open-invisible-function
  4836. (lambda (&rest ignore) (org-show-context 'isearch))))
  4837. ;; Setup the pcomplete hooks
  4838. (setq-local pcomplete-command-completion-function 'org-pcomplete-initial)
  4839. (setq-local pcomplete-command-name-function 'org-command-at-point)
  4840. (setq-local pcomplete-default-completion-function 'ignore)
  4841. (setq-local pcomplete-parse-arguments-function 'org-parse-arguments)
  4842. (setq-local pcomplete-termination-string "")
  4843. (setq-local buffer-face-mode-face 'org-default)
  4844. ;; If empty file that did not turn on Org mode automatically, make
  4845. ;; it to.
  4846. (when (and org-insert-mode-line-in-empty-file
  4847. (org-called-interactively-p 'any)
  4848. (= (point-min) (point-max)))
  4849. (insert "# -*- mode: org -*-\n\n"))
  4850. (unless org-inhibit-startup
  4851. (org-unmodified
  4852. (when org-startup-with-beamer-mode (org-beamer-mode))
  4853. (when org-startup-align-all-tables
  4854. (org-table-map-tables #'org-table-align t))
  4855. (when org-startup-with-inline-images (org-display-inline-images))
  4856. (when org-startup-with-latex-preview (org-toggle-latex-fragment))
  4857. (unless org-inhibit-startup-visibility-stuff (org-set-startup-visibility))
  4858. (when org-startup-truncated (setq truncate-lines t))
  4859. (when org-startup-indented (require 'org-indent) (org-indent-mode 1))
  4860. (org-refresh-effort-properties)))
  4861. ;; Try to set `org-hide' face correctly.
  4862. (let ((foreground (org-find-invisible-foreground)))
  4863. (when foreground
  4864. (set-face-foreground 'org-hide foreground))))
  4865. ;; Update `customize-package-emacs-version-alist'
  4866. (add-to-list 'customize-package-emacs-version-alist
  4867. '(Org ("6.21b" . "23.1") ("6.33x" . "23.2")
  4868. ("7.8.11" . "24.1") ("7.9.4" . "24.3")
  4869. ("8.2.6" . "24.4") ("8.3" . "25.1")))
  4870. (defvar org-mode-transpose-word-syntax-table
  4871. (let ((st (make-syntax-table text-mode-syntax-table)))
  4872. (mapc (lambda(c) (modify-syntax-entry
  4873. (string-to-char (car c)) "w p" st))
  4874. org-emphasis-alist)
  4875. st))
  4876. (when (fboundp 'abbrev-table-put)
  4877. (abbrev-table-put org-mode-abbrev-table
  4878. :parents (list text-mode-abbrev-table)))
  4879. (defun org-find-invisible-foreground ()
  4880. (let ((candidates (remove
  4881. "unspecified-bg"
  4882. (nconc
  4883. (list (face-background 'default)
  4884. (face-background 'org-default))
  4885. (mapcar
  4886. (lambda (alist)
  4887. (when (boundp alist)
  4888. (cdr (assoc 'background-color (symbol-value alist)))))
  4889. '(default-frame-alist initial-frame-alist window-system-default-frame-alist))
  4890. (list (face-foreground 'org-hide))))))
  4891. (car (remove nil candidates))))
  4892. (defun org-current-time (&optional rounding-minutes past)
  4893. "Current time, possibly rounded to ROUNDING-MINUTES.
  4894. When ROUNDING-MINUTES is not an integer, fall back on the car of
  4895. `org-time-stamp-rounding-minutes'. When PAST is non-nil, ensure
  4896. the rounding returns a past time."
  4897. (let ((r (or (and (integerp rounding-minutes) rounding-minutes)
  4898. (car org-time-stamp-rounding-minutes)))
  4899. (time (decode-time)) res)
  4900. (if (< r 1)
  4901. (current-time)
  4902. (setq res
  4903. (apply 'encode-time
  4904. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  4905. (nthcdr 2 time))))
  4906. (if (and past (< (org-float-time (time-subtract (current-time) res)) 0))
  4907. (seconds-to-time (- (org-float-time res) (* r 60)))
  4908. res))))
  4909. (defun org-today ()
  4910. "Return today date, considering `org-extend-today-until'."
  4911. (time-to-days
  4912. (time-subtract (current-time)
  4913. (list 0 (* 3600 org-extend-today-until) 0))))
  4914. ;;;; Font-Lock stuff, including the activators
  4915. (defvar org-mouse-map (make-sparse-keymap))
  4916. (org-defkey org-mouse-map [mouse-2] 'org-open-at-mouse)
  4917. (org-defkey org-mouse-map [mouse-3] 'org-find-file-at-mouse)
  4918. (when org-mouse-1-follows-link
  4919. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  4920. (when org-tab-follows-link
  4921. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  4922. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  4923. (require 'font-lock)
  4924. (defconst org-non-link-chars "]\t\n\r<>")
  4925. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "file+emacs"
  4926. "file+sys" "news" "shell" "elisp" "doi" "message"
  4927. "help"))
  4928. (defvar org-link-types-re nil
  4929. "Matches a link that has a url-like prefix like \"http:\"")
  4930. (defvar org-link-re-with-space nil
  4931. "Matches a link with spaces, optional angular brackets around it.")
  4932. (defvar org-link-re-with-space2 nil
  4933. "Matches a link with spaces, optional angular brackets around it.")
  4934. (defvar org-link-re-with-space3 nil
  4935. "Matches a link with spaces, only for internal part in bracket links.")
  4936. (defvar org-angle-link-re nil
  4937. "Matches link with angular brackets, spaces are allowed.")
  4938. (defvar org-plain-link-re nil
  4939. "Matches plain link, without spaces.")
  4940. (defvar org-bracket-link-regexp nil
  4941. "Matches a link in double brackets.")
  4942. (defvar org-bracket-link-analytic-regexp nil
  4943. "Regular expression used to analyze links.
  4944. Here is what the match groups contain after a match:
  4945. 1: http:
  4946. 2: http
  4947. 3: path
  4948. 4: [desc]
  4949. 5: desc")
  4950. (defvar org-bracket-link-analytic-regexp++ nil
  4951. "Like `org-bracket-link-analytic-regexp', but include coderef internal type.")
  4952. (defvar org-any-link-re nil
  4953. "Regular expression matching any link.")
  4954. (defconst org-match-sexp-depth 3
  4955. "Number of stacked braces for sub/superscript matching.")
  4956. (defun org-create-multibrace-regexp (left right n)
  4957. "Create a regular expression which will match a balanced sexp.
  4958. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  4959. as single character strings.
  4960. The regexp returned will match the entire expression including the
  4961. delimiters. It will also define a single group which contains the
  4962. match except for the outermost delimiters. The maximum depth of
  4963. stacked delimiters is N. Escaping delimiters is not possible."
  4964. (let* ((nothing (concat "[^" left right "]*?"))
  4965. (or "\\|")
  4966. (re nothing)
  4967. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  4968. (while (> n 1)
  4969. (setq n (1- n)
  4970. re (concat re or next)
  4971. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  4972. (concat left "\\(" re "\\)" right)))
  4973. (defconst org-match-substring-regexp
  4974. (concat
  4975. "\\(\\S-\\)\\([_^]\\)\\("
  4976. "\\(?:" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4977. "\\|"
  4978. "\\(?:" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  4979. "\\|"
  4980. "\\(?:\\*\\|[+-]?[[:alnum:].,\\]*[[:alnum:]]\\)\\)")
  4981. "The regular expression matching a sub- or superscript.")
  4982. (defconst org-match-substring-with-braces-regexp
  4983. (concat
  4984. "\\(\\S-\\)\\([_^]\\)"
  4985. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)")
  4986. "The regular expression matching a sub- or superscript, forcing braces.")
  4987. (defun org-make-link-regexps ()
  4988. "Update the link regular expressions.
  4989. This should be called after the variable `org-link-types' has changed."
  4990. (let ((types-re (regexp-opt org-link-types t)))
  4991. (setq org-link-types-re
  4992. (concat "\\`" types-re ":")
  4993. org-link-re-with-space
  4994. (concat "<?" types-re ":"
  4995. "\\([^" org-non-link-chars " ]"
  4996. "[^" org-non-link-chars "]*"
  4997. "[^" org-non-link-chars " ]\\)>?")
  4998. org-link-re-with-space2
  4999. (concat "<?" types-re ":"
  5000. "\\([^" org-non-link-chars " ]"
  5001. "[^\t\n\r]*"
  5002. "[^" org-non-link-chars " ]\\)>?")
  5003. org-link-re-with-space3
  5004. (concat "<?" types-re ":"
  5005. "\\([^" org-non-link-chars " ]"
  5006. "[^\t\n\r]*\\)")
  5007. org-angle-link-re
  5008. (format "<%s:\\([^>\n]*\\(?:\n[ \t]*[^> \t\n][^>\n]*\\)*\\)>"
  5009. types-re)
  5010. org-plain-link-re
  5011. (concat
  5012. "\\<" types-re ":"
  5013. (org-re "\\([^ \t\n()<>]+\\(?:([[:word:]0-9_]+)\\|\\([^[:punct:] \t\n]\\|/\\)\\)\\)"))
  5014. ;; "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  5015. org-bracket-link-regexp
  5016. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  5017. org-bracket-link-analytic-regexp
  5018. (concat
  5019. "\\[\\["
  5020. "\\(" types-re ":\\)?"
  5021. "\\([^]]+\\)"
  5022. "\\]"
  5023. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  5024. "\\]")
  5025. org-bracket-link-analytic-regexp++
  5026. (concat
  5027. "\\[\\["
  5028. "\\(" (regexp-opt (cons "coderef" org-link-types) t) ":\\)?"
  5029. "\\([^]]+\\)"
  5030. "\\]"
  5031. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  5032. "\\]")
  5033. org-any-link-re
  5034. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  5035. org-angle-link-re "\\)\\|\\("
  5036. org-plain-link-re "\\)"))))
  5037. (org-make-link-regexps)
  5038. (defvar org-emph-face nil)
  5039. (defun org-do-emphasis-faces (limit)
  5040. "Run through the buffer and emphasize strings."
  5041. (let (rtn a)
  5042. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  5043. (let* ((border (char-after (match-beginning 3)))
  5044. (bre (regexp-quote (char-to-string border))))
  5045. (when (and (not (= border (char-after (match-beginning 4))))
  5046. (not (string-match-p (concat bre ".*" bre)
  5047. (replace-regexp-in-string
  5048. "\n" " "
  5049. (substring (match-string 2) 1 -1)))))
  5050. (setq rtn t)
  5051. (setq a (assoc (match-string 3) org-emphasis-alist))
  5052. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  5053. 'face
  5054. (nth 1 a))
  5055. (and (nth 2 a)
  5056. (org-remove-flyspell-overlays-in
  5057. (match-beginning 0) (match-end 0)))
  5058. (add-text-properties (match-beginning 2) (match-end 2)
  5059. '(font-lock-multiline t org-emphasis t))
  5060. (when org-hide-emphasis-markers
  5061. (add-text-properties (match-end 4) (match-beginning 5)
  5062. '(invisible org-link))
  5063. (add-text-properties (match-beginning 3) (match-end 3)
  5064. '(invisible org-link)))))
  5065. (goto-char (1+ (match-beginning 0))))
  5066. rtn))
  5067. (defun org-emphasize (&optional char)
  5068. "Insert or change an emphasis, i.e. a font like bold or italic.
  5069. If there is an active region, change that region to a new emphasis.
  5070. If there is no region, just insert the marker characters and position
  5071. the cursor between them.
  5072. CHAR should be the marker character. If it is a space, it means to
  5073. remove the emphasis of the selected region.
  5074. If CHAR is not given (for example in an interactive call) it will be
  5075. prompted for."
  5076. (interactive)
  5077. (let ((erc org-emphasis-regexp-components)
  5078. (prompt "")
  5079. (string "") beg end move c s)
  5080. (if (org-region-active-p)
  5081. (setq beg (region-beginning) end (region-end)
  5082. string (buffer-substring beg end))
  5083. (setq move t))
  5084. (unless char
  5085. (message "Emphasis marker or tag: [%s]"
  5086. (mapconcat (lambda(e) (car e)) org-emphasis-alist ""))
  5087. (setq char (read-char-exclusive)))
  5088. (if (equal char ?\ )
  5089. (setq s "" move nil)
  5090. (unless (assoc (char-to-string char) org-emphasis-alist)
  5091. (user-error "No such emphasis marker: \"%c\"" char))
  5092. (setq s (char-to-string char)))
  5093. (while (and (> (length string) 1)
  5094. (equal (substring string 0 1) (substring string -1))
  5095. (assoc (substring string 0 1) org-emphasis-alist))
  5096. (setq string (substring string 1 -1)))
  5097. (setq string (concat s string s))
  5098. (when beg (delete-region beg end))
  5099. (unless (or (bolp)
  5100. (string-match (concat "[" (nth 0 erc) "\n]")
  5101. (char-to-string (char-before (point)))))
  5102. (insert " "))
  5103. (unless (or (eobp)
  5104. (string-match (concat "[" (nth 1 erc) "\n]")
  5105. (char-to-string (char-after (point)))))
  5106. (insert " ") (backward-char 1))
  5107. (insert string)
  5108. (and move (backward-char 1))))
  5109. (defconst org-nonsticky-props
  5110. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text htmlize-link))
  5111. (defsubst org-rear-nonsticky-at (pos)
  5112. (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
  5113. (defun org-activate-plain-links (limit)
  5114. "Add link properties for plain links."
  5115. (let (f hl)
  5116. (when (and (re-search-forward (concat org-plain-link-re) limit t)
  5117. (not (member 'org-tag
  5118. (get-text-property (1- (match-beginning 0)) 'face)))
  5119. (not (org-in-src-block-p)))
  5120. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5121. (setq f (get-text-property (match-beginning 0) 'face))
  5122. (setq hl (org-match-string-no-properties 0))
  5123. (unless (or (eq f 'org-tag)
  5124. (and (listp f) (memq 'org-tag f)))
  5125. (add-text-properties (match-beginning 0) (match-end 0)
  5126. (list 'mouse-face 'highlight
  5127. 'face 'org-link
  5128. 'htmlize-link `(:uri ,hl)
  5129. 'keymap org-mouse-map))
  5130. (org-rear-nonsticky-at (match-end 0)))
  5131. t)))
  5132. (defun org-activate-code (limit)
  5133. (when (re-search-forward "^[ \t]*\\(:\\(?: .*\\|$\\)\n?\\)" limit t)
  5134. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5135. (remove-text-properties (match-beginning 0) (match-end 0)
  5136. '(display t invisible t intangible t))
  5137. t))
  5138. (defcustom org-src-fontify-natively t
  5139. "When non-nil, fontify code in code blocks."
  5140. :type 'boolean
  5141. :version "24.4"
  5142. :package-version '(Org . "8.3")
  5143. :group 'org-appearance
  5144. :group 'org-babel)
  5145. (defcustom org-allow-promoting-top-level-subtree nil
  5146. "When non-nil, allow promoting a top level subtree.
  5147. The leading star of the top level headline will be replaced
  5148. by a #."
  5149. :type 'boolean
  5150. :version "24.1"
  5151. :group 'org-appearance)
  5152. (defun org-fontify-meta-lines-and-blocks (limit)
  5153. (condition-case nil
  5154. (org-fontify-meta-lines-and-blocks-1 limit)
  5155. (error (message "org-mode fontification error"))))
  5156. (defun org-fontify-meta-lines-and-blocks-1 (limit)
  5157. "Fontify #+ lines and blocks."
  5158. (let ((case-fold-search t))
  5159. (when (re-search-forward
  5160. "^\\([ \t]*#\\(\\(\\+[a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)[ \t]*\\(\\([^ \t\n]*\\)[ \t]*\\(.*\\)\\)\\)"
  5161. limit t)
  5162. (let ((beg (match-beginning 0))
  5163. (block-start (match-end 0))
  5164. (block-end nil)
  5165. (lang (match-string 7))
  5166. (beg1 (line-beginning-position 2))
  5167. (dc1 (downcase (match-string 2)))
  5168. (dc3 (downcase (match-string 3)))
  5169. end end1 quoting block-type ovl)
  5170. (cond
  5171. ((and (match-end 4) (equal dc3 "+begin"))
  5172. ;; Truly a block
  5173. (setq block-type (downcase (match-string 5))
  5174. quoting (member block-type org-protecting-blocks))
  5175. (when (re-search-forward
  5176. (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
  5177. nil t) ;; on purpose, we look further than LIMIT
  5178. (setq end (min (point-max) (match-end 0))
  5179. end1 (min (point-max) (1- (match-beginning 0))))
  5180. (setq block-end (match-beginning 0))
  5181. (when quoting
  5182. (org-remove-flyspell-overlays-in beg1 end1)
  5183. (remove-text-properties beg end
  5184. '(display t invisible t intangible t)))
  5185. (add-text-properties
  5186. beg end '(font-lock-fontified t font-lock-multiline t))
  5187. (add-text-properties beg beg1 '(face org-meta-line))
  5188. (org-remove-flyspell-overlays-in beg beg1)
  5189. (add-text-properties ; For end_src
  5190. end1 (min (point-max) (1+ end)) '(face org-meta-line))
  5191. (org-remove-flyspell-overlays-in end1 end)
  5192. (cond
  5193. ((and lang (not (string= lang "")) org-src-fontify-natively)
  5194. (org-src-font-lock-fontify-block lang block-start block-end)
  5195. (add-text-properties beg1 block-end '(src-block t)))
  5196. (quoting
  5197. (add-text-properties beg1 (min (point-max) (1+ end1))
  5198. '(face org-block))) ; end of source block
  5199. ((not org-fontify-quote-and-verse-blocks))
  5200. ((string= block-type "quote")
  5201. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-quote)))
  5202. ((string= block-type "verse")
  5203. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-verse))))
  5204. (add-text-properties beg beg1 '(face org-block-begin-line))
  5205. (add-text-properties (min (point-max) (1+ end)) (min (point-max) (1+ end1))
  5206. '(face org-block-end-line))
  5207. t))
  5208. ((member dc1 '("+title:" "+author:" "+email:" "+date:"))
  5209. (org-remove-flyspell-overlays-in
  5210. (match-beginning 0)
  5211. (if (equal "+title:" dc1) (match-end 2) (match-end 0)))
  5212. (add-text-properties
  5213. beg (match-end 3)
  5214. (if (member (intern (substring dc1 1 -1)) org-hidden-keywords)
  5215. '(font-lock-fontified t invisible t)
  5216. '(font-lock-fontified t face org-document-info-keyword)))
  5217. (add-text-properties
  5218. (match-beginning 6) (min (point-max) (1+ (match-end 6)))
  5219. (if (string-equal dc1 "+title:")
  5220. '(font-lock-fontified t face org-document-title)
  5221. '(font-lock-fontified t face org-document-info))))
  5222. ((equal dc1 "+caption:")
  5223. (org-remove-flyspell-overlays-in (match-end 2) (match-end 0))
  5224. (remove-text-properties (match-beginning 0) (match-end 0)
  5225. '(display t invisible t intangible t))
  5226. (add-text-properties (match-beginning 1) (match-end 3)
  5227. '(font-lock-fontified t face org-meta-line))
  5228. (add-text-properties (match-beginning 6) (+ (match-end 6) 1)
  5229. '(font-lock-fontified t face org-block))
  5230. t)
  5231. ((member dc3 '(" " ""))
  5232. (org-remove-flyspell-overlays-in beg (match-end 0))
  5233. (add-text-properties
  5234. beg (match-end 0)
  5235. '(font-lock-fontified t face font-lock-comment-face)))
  5236. (t ;; just any other in-buffer setting, but not indented
  5237. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5238. (remove-text-properties (match-beginning 0) (match-end 0)
  5239. '(display t invisible t intangible t))
  5240. (add-text-properties beg (match-end 0)
  5241. '(font-lock-fontified t face org-meta-line))
  5242. t))))))
  5243. (defun org-fontify-drawers (limit)
  5244. "Fontify drawers."
  5245. (when (re-search-forward org-drawer-regexp limit t)
  5246. (add-text-properties
  5247. (match-beginning 0) (match-end 0)
  5248. '(font-lock-fontified t face org-special-keyword))
  5249. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5250. t))
  5251. (defun org-fontify-macros (limit)
  5252. "Fontify macros."
  5253. (when (re-search-forward "\\({{{\\).+?\\(}}}\\)" limit t)
  5254. (add-text-properties
  5255. (match-beginning 0) (match-end 0)
  5256. '(font-lock-fontified t face org-macro))
  5257. (when org-hide-macro-markers
  5258. (add-text-properties (match-end 2) (match-beginning 2)
  5259. '(invisible t))
  5260. (add-text-properties (match-beginning 1) (match-end 1)
  5261. '(invisible t)))
  5262. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5263. t))
  5264. (defun org-activate-angle-links (limit)
  5265. "Add text properties for angle links."
  5266. (when (and (re-search-forward org-angle-link-re limit t)
  5267. (not (org-in-src-block-p)))
  5268. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5269. (add-text-properties (match-beginning 0) (match-end 0)
  5270. (list 'mouse-face 'highlight
  5271. 'keymap org-mouse-map
  5272. 'font-lock-multiline t))
  5273. (org-rear-nonsticky-at (match-end 0))
  5274. t))
  5275. (defun org-activate-footnote-links (limit)
  5276. "Add text properties for footnotes."
  5277. (let ((fn (org-footnote-next-reference-or-definition limit)))
  5278. (when fn
  5279. (let* ((beg (nth 1 fn))
  5280. (end (nth 2 fn))
  5281. (label (car fn))
  5282. (referencep (/= (line-beginning-position) beg)))
  5283. (when (and referencep (nth 3 fn))
  5284. (save-excursion
  5285. (goto-char beg)
  5286. (search-forward (or label "fn:"))
  5287. (org-remove-flyspell-overlays-in beg (match-end 0))))
  5288. (add-text-properties beg end
  5289. (list 'mouse-face 'highlight
  5290. 'keymap org-mouse-map
  5291. 'help-echo
  5292. (if referencep "Footnote reference"
  5293. "Footnote definition")
  5294. 'font-lock-fontified t
  5295. 'font-lock-multiline t
  5296. 'face 'org-footnote))))))
  5297. (defun org-activate-bracket-links (limit)
  5298. "Add text properties for bracketed links."
  5299. (when (and (re-search-forward org-bracket-link-regexp limit t)
  5300. (not (org-in-src-block-p)))
  5301. (let* ((hl (org-match-string-no-properties 1))
  5302. (help (concat "LINK: " (save-match-data (org-link-unescape hl))))
  5303. (ip (org-maybe-intangible
  5304. (list 'invisible 'org-link
  5305. 'keymap org-mouse-map 'mouse-face 'highlight
  5306. 'font-lock-multiline t 'help-echo help
  5307. 'htmlize-link `(:uri ,hl))))
  5308. (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
  5309. 'font-lock-multiline t 'help-echo help
  5310. 'htmlize-link `(:uri ,hl))))
  5311. ;; We need to remove the invisible property here. Table narrowing
  5312. ;; may have made some of this invisible.
  5313. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5314. (remove-text-properties (match-beginning 0) (match-end 0)
  5315. '(invisible nil))
  5316. (if (match-end 3)
  5317. (progn
  5318. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  5319. (org-rear-nonsticky-at (match-beginning 3))
  5320. (add-text-properties (match-beginning 3) (match-end 3) vp)
  5321. (org-rear-nonsticky-at (match-end 3))
  5322. (add-text-properties (match-end 3) (match-end 0) ip)
  5323. (org-rear-nonsticky-at (match-end 0)))
  5324. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  5325. (org-rear-nonsticky-at (match-beginning 1))
  5326. (add-text-properties (match-beginning 1) (match-end 1) vp)
  5327. (org-rear-nonsticky-at (match-end 1))
  5328. (add-text-properties (match-end 1) (match-end 0) ip)
  5329. (org-rear-nonsticky-at (match-end 0)))
  5330. t)))
  5331. (defun org-activate-dates (limit)
  5332. "Add text properties for dates."
  5333. (when (and (re-search-forward org-tsr-regexp-both limit t)
  5334. (not (equal (char-before (match-beginning 0)) 91)))
  5335. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5336. (add-text-properties (match-beginning 0) (match-end 0)
  5337. (list 'mouse-face 'highlight
  5338. 'keymap org-mouse-map))
  5339. (org-rear-nonsticky-at (match-end 0))
  5340. (when org-display-custom-times
  5341. (if (match-end 3)
  5342. (org-display-custom-time (match-beginning 3) (match-end 3))
  5343. (org-display-custom-time (match-beginning 1) (match-end 1))))
  5344. t))
  5345. (defvar-local org-target-link-regexp nil
  5346. "Regular expression matching radio targets in plain text.")
  5347. (defconst org-target-regexp (let ((border "[^<>\n\r \t]"))
  5348. (format "<<\\(%s\\|%s[^<>\n\r]*%s\\)>>"
  5349. border border border))
  5350. "Regular expression matching a link target.")
  5351. (defconst org-radio-target-regexp (format "<%s>" org-target-regexp)
  5352. "Regular expression matching a radio target.")
  5353. (defconst org-any-target-regexp
  5354. (format "%s\\|%s" org-radio-target-regexp org-target-regexp)
  5355. "Regular expression matching any target.")
  5356. (defun org-activate-target-links (limit)
  5357. "Add text properties for target matches."
  5358. (when org-target-link-regexp
  5359. (let ((case-fold-search t))
  5360. (when (re-search-forward org-target-link-regexp limit t)
  5361. (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
  5362. (add-text-properties (match-beginning 1) (match-end 1)
  5363. (list 'mouse-face 'highlight
  5364. 'keymap org-mouse-map
  5365. 'help-echo "Radio target link"
  5366. 'org-linked-text t))
  5367. (org-rear-nonsticky-at (match-end 1))
  5368. t))))
  5369. (defun org-update-radio-target-regexp ()
  5370. "Find all radio targets in this file and update the regular expression.
  5371. Also refresh fontification if needed."
  5372. (interactive)
  5373. (let ((old-regexp org-target-link-regexp)
  5374. (before-re "\\(?:^\\|[^[:alnum:]]\\)\\(")
  5375. (after-re "\\)\\(?:$\\|[^[:alnum:]]\\)")
  5376. (targets
  5377. (org-with-wide-buffer
  5378. (goto-char (point-min))
  5379. (let (rtn)
  5380. (while (re-search-forward org-radio-target-regexp nil t)
  5381. ;; Make sure point is really within the object.
  5382. (backward-char)
  5383. (let ((obj (org-element-context)))
  5384. (when (eq (org-element-type obj) 'radio-target)
  5385. (add-to-list 'rtn (org-element-property :value obj)))))
  5386. rtn))))
  5387. (setq org-target-link-regexp
  5388. (and targets
  5389. (concat before-re
  5390. (mapconcat
  5391. (lambda (x)
  5392. (replace-regexp-in-string
  5393. " +" "\\s-+" (regexp-quote x) t t))
  5394. targets
  5395. "\\|")
  5396. after-re)))
  5397. (unless (equal old-regexp org-target-link-regexp)
  5398. ;; Clean-up cache.
  5399. (let ((regexp (cond ((not old-regexp) org-target-link-regexp)
  5400. ((not org-target-link-regexp) old-regexp)
  5401. (t
  5402. (concat before-re
  5403. (mapconcat
  5404. (lambda (re)
  5405. (substring re (length before-re)
  5406. (- (length after-re))))
  5407. (list old-regexp org-target-link-regexp)
  5408. "\\|")
  5409. after-re)))))
  5410. (org-with-wide-buffer
  5411. (goto-char (point-min))
  5412. (while (re-search-forward regexp nil t)
  5413. (org-element-cache-refresh (match-beginning 1)))))
  5414. ;; Re fontify buffer.
  5415. (when (memq 'radio org-highlight-links)
  5416. (org-restart-font-lock)))))
  5417. (defun org-hide-wide-columns (limit)
  5418. (let (s e)
  5419. (setq s (text-property-any (point) (or limit (point-max))
  5420. 'org-cwidth t))
  5421. (when s
  5422. (setq e (next-single-property-change s 'org-cwidth))
  5423. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  5424. (goto-char e)
  5425. t)))
  5426. (defvar org-latex-and-related-regexp nil
  5427. "Regular expression for highlighting LaTeX, entities and sub/superscript.")
  5428. (defun org-compute-latex-and-related-regexp ()
  5429. "Compute regular expression for LaTeX, entities and sub/superscript.
  5430. Result depends on variable `org-highlight-latex-and-related'."
  5431. (setq-local
  5432. org-latex-and-related-regexp
  5433. (let* ((re-sub
  5434. (cond ((not (memq 'script org-highlight-latex-and-related)) nil)
  5435. ((eq org-use-sub-superscripts '{})
  5436. (list org-match-substring-with-braces-regexp))
  5437. (org-use-sub-superscripts (list org-match-substring-regexp))))
  5438. (re-latex
  5439. (when (memq 'latex org-highlight-latex-and-related)
  5440. (let ((matchers (plist-get org-format-latex-options :matchers)))
  5441. (delq nil
  5442. (mapcar (lambda (x)
  5443. (and (member (car x) matchers) (nth 1 x)))
  5444. org-latex-regexps)))))
  5445. (re-entities
  5446. (when (memq 'entities org-highlight-latex-and-related)
  5447. (list "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)"))))
  5448. (mapconcat 'identity (append re-latex re-entities re-sub) "\\|"))))
  5449. (defun org-do-latex-and-related (limit)
  5450. "Highlight LaTeX snippets and environments, entities and sub/superscript.
  5451. LIMIT bounds the search for syntax to highlight. Stop at first
  5452. highlighted object, if any. Return t if some highlighting was
  5453. done, nil otherwise."
  5454. (when (org-string-nw-p org-latex-and-related-regexp)
  5455. (catch 'found
  5456. (while (re-search-forward org-latex-and-related-regexp limit t)
  5457. (unless (memq (car-safe (get-text-property (1+ (match-beginning 0))
  5458. 'face))
  5459. '(org-code org-verbatim underline))
  5460. (let ((offset (if (memq (char-after (1+ (match-beginning 0)))
  5461. '(?_ ?^))
  5462. 1
  5463. 0)))
  5464. (font-lock-prepend-text-property
  5465. (+ offset (match-beginning 0)) (match-end 0)
  5466. 'face 'org-latex-and-related)
  5467. (add-text-properties (+ offset (match-beginning 0)) (match-end 0)
  5468. '(font-lock-multiline t)))
  5469. (throw 'found t)))
  5470. nil)))
  5471. (defun org-restart-font-lock ()
  5472. "Restart `font-lock-mode', to force refontification."
  5473. (when (and (boundp 'font-lock-mode) font-lock-mode)
  5474. (font-lock-mode -1)
  5475. (font-lock-mode 1)))
  5476. (defun org-activate-tags (limit)
  5477. (when (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \r\n]") limit t)
  5478. (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
  5479. (add-text-properties (match-beginning 1) (match-end 1)
  5480. (list 'mouse-face 'highlight
  5481. 'keymap org-mouse-map))
  5482. (org-rear-nonsticky-at (match-end 1))
  5483. t))
  5484. (defun org-outline-level ()
  5485. "Compute the outline level of the heading at point.
  5486. If this is called at a normal headline, the level is the number
  5487. of stars. Use `org-reduced-level' to remove the effect of
  5488. `org-odd-levels'. Unlike to `org-current-level', this function
  5489. takes into consideration inlinetasks."
  5490. (org-with-wide-buffer
  5491. (end-of-line)
  5492. (if (re-search-backward org-outline-regexp-bol nil t)
  5493. (1- (- (match-end 0) (match-beginning 0)))
  5494. 0)))
  5495. (defvar org-font-lock-keywords nil)
  5496. (defsubst org-re-property (property &optional literal allow-null value)
  5497. "Return a regexp matching a PROPERTY line.
  5498. When optional argument LITERAL is non-nil, do not quote PROPERTY.
  5499. This is useful when PROPERTY is a regexp. When ALLOW-NULL is
  5500. non-nil, match properties even without a value.
  5501. Match group 3 is set to the value when it exists. If there is no
  5502. value and ALLOW-NULL is non-nil, it is set to the empty string.
  5503. With optional argument VALUE, match only property lines with
  5504. that value; in this case, ALLOW-NULL is ignored. VALUE is quoted
  5505. unless LITERAL is non-nil."
  5506. (concat
  5507. "^\\(?4:[ \t]*\\)"
  5508. (format "\\(?1::\\(?2:%s\\):\\)"
  5509. (if literal property (regexp-quote property)))
  5510. (cond (value
  5511. (format "[ \t]+\\(?3:%s\\)\\(?5:[ \t]*\\)$"
  5512. (if literal value (regexp-quote value))))
  5513. (allow-null
  5514. "\\(?:\\(?3:$\\)\\|[ \t]+\\(?3:.*?\\)\\)\\(?5:[ \t]*\\)$")
  5515. (t
  5516. "[ \t]+\\(?3:[^ \r\t\n]+.*?\\)\\(?5:[ \t]*\\)$"))))
  5517. (defconst org-property-re
  5518. (org-re-property "\\S-+" 'literal t)
  5519. "Regular expression matching a property line.
  5520. There are four matching groups:
  5521. 1: :PROPKEY: including the leading and trailing colon,
  5522. 2: PROPKEY without the leading and trailing colon,
  5523. 3: PROPVAL without leading or trailing spaces,
  5524. 4: the indentation of the current line,
  5525. 5: trailing whitespace.")
  5526. (defvar org-font-lock-hook nil
  5527. "Functions to be called for special font lock stuff.")
  5528. (defvar org-font-lock-set-keywords-hook nil
  5529. "Functions that can manipulate `org-font-lock-extra-keywords'.
  5530. This is called after `org-font-lock-extra-keywords' is defined, but before
  5531. it is installed to be used by font lock. This can be useful if something
  5532. needs to be inserted at a specific position in the font-lock sequence.")
  5533. (defun org-font-lock-hook (limit)
  5534. "Run `org-font-lock-hook' within LIMIT."
  5535. (run-hook-with-args 'org-font-lock-hook limit))
  5536. (defun org-set-font-lock-defaults ()
  5537. "Set font lock defaults for the current buffer."
  5538. (let* ((em org-fontify-emphasized-text)
  5539. (lk org-highlight-links)
  5540. (org-font-lock-extra-keywords
  5541. (list
  5542. ;; Call the hook
  5543. '(org-font-lock-hook)
  5544. ;; Headlines
  5545. `(,(if org-fontify-whole-heading-line
  5546. "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
  5547. "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
  5548. (1 (org-get-level-face 1))
  5549. (2 (org-get-level-face 2))
  5550. (3 (org-get-level-face 3)))
  5551. ;; Table lines
  5552. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  5553. (1 'org-table t))
  5554. ;; Table internals
  5555. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  5556. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  5557. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  5558. '("| *\\(<[lrc]?[0-9]*>\\)" (1 'org-formula t))
  5559. ;; Drawers
  5560. '(org-fontify-drawers)
  5561. ;; Properties
  5562. (list org-property-re
  5563. '(1 'org-special-keyword t)
  5564. '(3 'org-property-value t))
  5565. ;; Links
  5566. (when (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  5567. (when (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  5568. (when (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  5569. (when (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  5570. (when (memq 'radio lk) '(org-activate-target-links (1 'org-link t)))
  5571. (when (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  5572. (when (memq 'footnote lk) '(org-activate-footnote-links))
  5573. ;; Targets.
  5574. (list org-any-target-regexp '(0 'org-target t))
  5575. ;; Diary sexps.
  5576. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  5577. ;; Macro
  5578. '(org-fontify-macros)
  5579. '(org-hide-wide-columns (0 nil append))
  5580. ;; TODO keyword
  5581. (list (format org-heading-keyword-regexp-format
  5582. org-todo-regexp)
  5583. '(2 (org-get-todo-face 2) t))
  5584. ;; DONE
  5585. (if org-fontify-done-headline
  5586. (list (format org-heading-keyword-regexp-format
  5587. (concat
  5588. "\\(?:"
  5589. (mapconcat 'regexp-quote org-done-keywords "\\|")
  5590. "\\)"))
  5591. '(2 'org-headline-done t))
  5592. nil)
  5593. ;; Priorities
  5594. '(org-font-lock-add-priority-faces)
  5595. ;; Tags
  5596. '(org-font-lock-add-tag-faces)
  5597. ;; Tags groups
  5598. (when (and org-group-tags org-tag-groups-alist)
  5599. (list (concat org-outline-regexp-bol ".+\\(:"
  5600. (regexp-opt (mapcar 'car org-tag-groups-alist))
  5601. ":\\).*$")
  5602. '(1 'org-tag-group prepend)))
  5603. ;; Special keywords
  5604. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  5605. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  5606. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  5607. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  5608. ;; Emphasis
  5609. (when em
  5610. (if (featurep 'xemacs)
  5611. '(org-do-emphasis-faces (0 nil append))
  5612. '(org-do-emphasis-faces)))
  5613. ;; Checkboxes
  5614. '("^[ \t]*\\(?:[-+*]\\|[0-9]+[.)]\\)[ \t]+\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?\\(\\[[- X]\\]\\)"
  5615. 1 'org-checkbox prepend)
  5616. (when (cdr (assq 'checkbox org-list-automatic-rules))
  5617. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  5618. (0 (org-get-checkbox-statistics-face) t)))
  5619. ;; Description list items
  5620. '("^[ \t]*[-+*][ \t]+\\(.*?[ \t]+::\\)\\([ \t]+\\|$\\)"
  5621. 1 'org-list-dt prepend)
  5622. ;; ARCHIVEd headings
  5623. (list (concat
  5624. org-outline-regexp-bol
  5625. "\\(.*:" org-archive-tag ":.*\\)")
  5626. '(1 'org-archived prepend))
  5627. ;; Specials
  5628. '(org-do-latex-and-related)
  5629. '(org-fontify-entities)
  5630. '(org-raise-scripts)
  5631. ;; Code
  5632. '(org-activate-code (1 'org-code t))
  5633. ;; COMMENT
  5634. (list (format
  5635. "^\\*+\\(?: +%s\\)?\\(?: +\\[#[A-Z0-9]\\]\\)? +\\(?9:%s\\)\\(?: \\|$\\)"
  5636. org-todo-regexp
  5637. org-comment-string)
  5638. '(9 'org-special-keyword t))
  5639. ;; Blocks and meta lines
  5640. '(org-fontify-meta-lines-and-blocks))))
  5641. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  5642. (run-hooks 'org-font-lock-set-keywords-hook)
  5643. ;; Now set the full font-lock-keywords
  5644. (setq-local org-font-lock-keywords org-font-lock-extra-keywords)
  5645. (setq-local font-lock-defaults
  5646. '(org-font-lock-keywords t nil nil backward-paragraph))
  5647. (kill-local-variable 'font-lock-keywords) nil))
  5648. (defun org-toggle-pretty-entities ()
  5649. "Toggle the composition display of entities as UTF8 characters."
  5650. (interactive)
  5651. (setq-local org-pretty-entities (not org-pretty-entities))
  5652. (org-restart-font-lock)
  5653. (if org-pretty-entities
  5654. (message "Entities are now displayed as UTF8 characters")
  5655. (save-restriction
  5656. (widen)
  5657. (org-decompose-region (point-min) (point-max))
  5658. (message "Entities are now displayed as plain text"))))
  5659. (defvar-local org-custom-properties-overlays nil
  5660. "List of overlays used for custom properties.")
  5661. (defun org-toggle-custom-properties-visibility ()
  5662. "Display or hide properties in `org-custom-properties'."
  5663. (interactive)
  5664. (if org-custom-properties-overlays
  5665. (progn (mapc #'delete-overlay org-custom-properties-overlays)
  5666. (setq org-custom-properties-overlays nil))
  5667. (when org-custom-properties
  5668. (org-with-wide-buffer
  5669. (goto-char (point-min))
  5670. (let ((regexp (org-re-property (regexp-opt org-custom-properties) t t)))
  5671. (while (re-search-forward regexp nil t)
  5672. (let ((end (cdr (save-match-data (org-get-property-block)))))
  5673. (when (and end (< (point) end))
  5674. ;; Hide first custom property in current drawer.
  5675. (let ((o (make-overlay (match-beginning 0) (1+ (match-end 0)))))
  5676. (overlay-put o 'invisible t)
  5677. (overlay-put o 'org-custom-property t)
  5678. (push o org-custom-properties-overlays))
  5679. ;; Hide additional custom properties in the same drawer.
  5680. (while (re-search-forward regexp end t)
  5681. (let ((o (make-overlay (match-beginning 0) (1+ (match-end 0)))))
  5682. (overlay-put o 'invisible t)
  5683. (overlay-put o 'org-custom-property t)
  5684. (push o org-custom-properties-overlays)))))
  5685. ;; Each entry is limited to a single property drawer.
  5686. (outline-next-heading)))))))
  5687. (defun org-fontify-entities (limit)
  5688. "Find an entity to fontify."
  5689. (let (ee)
  5690. (when org-pretty-entities
  5691. (catch 'match
  5692. ;; "\_ "-family is left out on purpose. Only the first one,
  5693. ;; i.e., "\_ ", could be fontified anyway, and it would be
  5694. ;; confusing when adding a second white space character.
  5695. (while (re-search-forward
  5696. "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]\n]\\)"
  5697. limit t)
  5698. (when (and (not (org-at-comment-p))
  5699. (setq ee (org-entity-get (match-string 1)))
  5700. (= (length (nth 6 ee)) 1))
  5701. (let* ((end (if (equal (match-string 2) "{}")
  5702. (match-end 2)
  5703. (match-end 1))))
  5704. (add-text-properties
  5705. (match-beginning 0) end
  5706. (list 'font-lock-fontified t))
  5707. (compose-region (match-beginning 0) end
  5708. (nth 6 ee) nil)
  5709. (backward-char 1)
  5710. (throw 'match t))))
  5711. nil))))
  5712. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  5713. "Fontify string S like in Org-mode."
  5714. (with-temp-buffer
  5715. (insert s)
  5716. (let ((org-odd-levels-only odd-levels))
  5717. (org-mode)
  5718. (font-lock-ensure)
  5719. (buffer-string))))
  5720. (defvar org-m nil)
  5721. (defvar org-l nil)
  5722. (defvar org-f nil)
  5723. (defun org-get-level-face (n)
  5724. "Get the right face for match N in font-lock matching of headlines."
  5725. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  5726. (when org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  5727. (if org-cycle-level-faces
  5728. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  5729. (setq org-f (nth (1- (min org-l org-n-level-faces)) org-level-faces)))
  5730. (cond
  5731. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  5732. ((eq n 2) org-f)
  5733. (t (unless org-level-color-stars-only org-f))))
  5734. (defun org-face-from-face-or-color (context inherit face-or-color)
  5735. "Create a face list that inherits INHERIT, but sets the foreground color.
  5736. When FACE-OR-COLOR is not a string, just return it."
  5737. (if (stringp face-or-color)
  5738. (list :inherit inherit
  5739. (cdr (assoc context org-faces-easy-properties))
  5740. face-or-color)
  5741. face-or-color))
  5742. (defun org-get-todo-face (kwd)
  5743. "Get the right face for a TODO keyword KWD.
  5744. If KWD is a number, get the corresponding match group."
  5745. (when (numberp kwd) (setq kwd (match-string kwd)))
  5746. (or (org-face-from-face-or-color
  5747. 'todo 'org-todo (cdr (assoc kwd org-todo-keyword-faces)))
  5748. (and (member kwd org-done-keywords) 'org-done)
  5749. 'org-todo))
  5750. (defun org-get-priority-face (priority)
  5751. "Get the right face for PRIORITY.
  5752. PRIORITY is a character."
  5753. (or (org-face-from-face-or-color
  5754. 'priority 'org-priority (cdr (assq priority org-priority-faces)))
  5755. 'org-priority))
  5756. (defun org-get-tag-face (tag)
  5757. "Get the right face for TAG.
  5758. If TAG is a number, get the corresponding match group."
  5759. (let ((tag (if (wholenump tag) (match-string tag) tag)))
  5760. (or (org-face-from-face-or-color
  5761. 'tag 'org-tag (cdr (assoc tag org-tag-faces)))
  5762. 'org-tag)))
  5763. (defun org-font-lock-add-priority-faces (limit)
  5764. "Add the special priority faces."
  5765. (while (re-search-forward "^\\*+ .*?\\(\\[#\\(.\\)\\]\\)" limit t)
  5766. (add-text-properties
  5767. (match-beginning 1) (match-end 1)
  5768. (list 'face (org-get-priority-face (string-to-char (match-string 2)))
  5769. 'font-lock-fontified t))))
  5770. (defun org-font-lock-add-tag-faces (limit)
  5771. "Add the special tag faces."
  5772. (when (and org-tag-faces org-tags-special-faces-re)
  5773. (while (re-search-forward org-tags-special-faces-re limit t)
  5774. (add-text-properties (match-beginning 1) (match-end 1)
  5775. (list 'face (org-get-tag-face 1)
  5776. 'font-lock-fontified t))
  5777. (backward-char 1))))
  5778. (defun org-unfontify-region (beg end &optional maybe_loudly)
  5779. "Remove fontification and activation overlays from links."
  5780. (font-lock-default-unfontify-region beg end)
  5781. (let* ((buffer-undo-list t)
  5782. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  5783. (inhibit-modification-hooks t)
  5784. deactivate-mark buffer-file-name buffer-file-truename)
  5785. (org-decompose-region beg end)
  5786. (remove-text-properties beg end
  5787. '(mouse-face t keymap t org-linked-text t
  5788. invisible t intangible t
  5789. org-emphasis t))
  5790. (org-remove-font-lock-display-properties beg end)))
  5791. (defconst org-script-display '(((raise -0.3) (height 0.7))
  5792. ((raise 0.3) (height 0.7))
  5793. ((raise -0.5))
  5794. ((raise 0.5)))
  5795. "Display properties for showing superscripts and subscripts.")
  5796. (defun org-remove-font-lock-display-properties (beg end)
  5797. "Remove specific display properties that have been added by font lock.
  5798. The will remove the raise properties that are used to show superscripts
  5799. and subscripts."
  5800. (let (next prop)
  5801. (while (< beg end)
  5802. (setq next (next-single-property-change beg 'display nil end)
  5803. prop (get-text-property beg 'display))
  5804. (when (member prop org-script-display)
  5805. (put-text-property beg next 'display nil))
  5806. (setq beg next))))
  5807. (defun org-raise-scripts (limit)
  5808. "Add raise properties to sub/superscripts."
  5809. (when (and org-pretty-entities org-pretty-entities-include-sub-superscripts
  5810. (re-search-forward
  5811. (if (eq org-use-sub-superscripts t)
  5812. org-match-substring-regexp
  5813. org-match-substring-with-braces-regexp)
  5814. limit t))
  5815. (let* ((pos (point)) table-p comment-p
  5816. (mpos (match-beginning 3))
  5817. (emph-p (get-text-property mpos 'org-emphasis))
  5818. (link-p (get-text-property mpos 'mouse-face))
  5819. (keyw-p (eq 'org-special-keyword (get-text-property mpos 'face))))
  5820. (goto-char (point-at-bol))
  5821. (setq table-p (org-looking-at-p org-table-dataline-regexp)
  5822. comment-p (org-looking-at-p "^[ \t]*#[ +]"))
  5823. (goto-char pos)
  5824. ;; Handle a_b^c
  5825. (when (member (char-after) '(?_ ?^)) (goto-char (1- pos)))
  5826. (unless (or comment-p emph-p link-p keyw-p)
  5827. (put-text-property (match-beginning 3) (match-end 0)
  5828. 'display
  5829. (if (equal (char-after (match-beginning 2)) ?^)
  5830. (nth (if table-p 3 1) org-script-display)
  5831. (nth (if table-p 2 0) org-script-display)))
  5832. (add-text-properties (match-beginning 2) (match-end 2)
  5833. (list 'invisible t
  5834. 'org-dwidth t 'org-dwidth-n 1))
  5835. (if (and (eq (char-after (match-beginning 3)) ?{)
  5836. (eq (char-before (match-end 3)) ?}))
  5837. (progn
  5838. (add-text-properties
  5839. (match-beginning 3) (1+ (match-beginning 3))
  5840. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))
  5841. (add-text-properties
  5842. (1- (match-end 3)) (match-end 3)
  5843. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1)))))
  5844. t)))
  5845. ;;;; Visibility cycling, including org-goto and indirect buffer
  5846. ;;; Cycling
  5847. (defvar-local org-cycle-global-status nil)
  5848. (put 'org-cycle-global-status 'org-state t)
  5849. (defvar-local org-cycle-subtree-status nil)
  5850. (put 'org-cycle-subtree-status 'org-state t)
  5851. (defvar org-inlinetask-min-level)
  5852. (defun org-unlogged-message (&rest args)
  5853. "Display a message, but avoid logging it in the *Messages* buffer."
  5854. (let ((message-log-max nil))
  5855. (apply 'message args)))
  5856. ;;;###autoload
  5857. (defun org-cycle (&optional arg)
  5858. "TAB-action and visibility cycling for Org-mode.
  5859. This is the command invoked in Org-mode by the TAB key. Its main purpose
  5860. is outline visibility cycling, but it also invokes other actions
  5861. in special contexts.
  5862. - When this function is called with a prefix argument, rotate the entire
  5863. buffer through 3 states (global cycling)
  5864. 1. OVERVIEW: Show only top-level headlines.
  5865. 2. CONTENTS: Show all headlines of all levels, but no body text.
  5866. 3. SHOW ALL: Show everything.
  5867. With a double \\[universal-argument] prefix argument, \
  5868. switch to the startup visibility,
  5869. determined by the variable `org-startup-folded', and by any VISIBILITY
  5870. properties in the buffer.
  5871. With a triple \\[universal-argument] prefix argument, \
  5872. show the entire buffer, including any drawers.
  5873. - When inside a table, re-align the table and move to the next field.
  5874. - When point is at the beginning of a headline, rotate the subtree started
  5875. by this line through 3 different states (local cycling)
  5876. 1. FOLDED: Only the main headline is shown.
  5877. 2. CHILDREN: The main headline and the direct children are shown.
  5878. From this state, you can move to one of the children
  5879. and zoom in further.
  5880. 3. SUBTREE: Show the entire subtree, including body text.
  5881. If there is no subtree, switch directly from CHILDREN to FOLDED.
  5882. - When point is at the beginning of an empty headline and the variable
  5883. `org-cycle-level-after-item/entry-creation' is set, cycle the level
  5884. of the headline by demoting and promoting it to likely levels. This
  5885. speeds up creation document structure by pressing TAB once or several
  5886. times right after creating a new headline.
  5887. - When there is a numeric prefix, go up to a heading with level ARG, do
  5888. a `show-subtree' and return to the previous cursor position. If ARG
  5889. is negative, go up that many levels.
  5890. - When point is not at the beginning of a headline, execute the global
  5891. binding for TAB, which is re-indenting the line. See the option
  5892. `org-cycle-emulate-tab' for details.
  5893. - Special case: if point is at the beginning of the buffer and there is
  5894. no headline in line 1, this function will act as if called with prefix arg
  5895. (\\[universal-argument] TAB, same as S-TAB) also when called without prefix arg.
  5896. But only if also the variable `org-cycle-global-at-bob' is t."
  5897. (interactive "P")
  5898. (org-load-modules-maybe)
  5899. (unless (or (run-hook-with-args-until-success 'org-tab-first-hook)
  5900. (and org-cycle-level-after-item/entry-creation
  5901. (or (org-cycle-level)
  5902. (org-cycle-item-indentation))))
  5903. (let* ((limit-level
  5904. (or org-cycle-max-level
  5905. (and (boundp 'org-inlinetask-min-level)
  5906. org-inlinetask-min-level
  5907. (1- org-inlinetask-min-level))))
  5908. (nstars (and limit-level
  5909. (if org-odd-levels-only
  5910. (and limit-level (1- (* limit-level 2)))
  5911. limit-level)))
  5912. (org-outline-regexp
  5913. (if (not (derived-mode-p 'org-mode))
  5914. outline-regexp
  5915. (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ "))))
  5916. (bob-special (and org-cycle-global-at-bob (not arg) (bobp)
  5917. (not (looking-at org-outline-regexp))))
  5918. (org-cycle-hook
  5919. (if bob-special
  5920. (delq 'org-optimize-window-after-visibility-change
  5921. (copy-sequence org-cycle-hook))
  5922. org-cycle-hook))
  5923. (pos (point)))
  5924. (when (or bob-special (equal arg '(4)))
  5925. ;; special case: use global cycling
  5926. (setq arg t))
  5927. (cond
  5928. ((equal arg '(16))
  5929. (setq last-command 'dummy)
  5930. (org-set-startup-visibility)
  5931. (org-unlogged-message "Startup visibility, plus VISIBILITY properties"))
  5932. ((equal arg '(64))
  5933. (outline-show-all)
  5934. (org-unlogged-message "Entire buffer visible, including drawers"))
  5935. ;; Try cdlatex TAB completion
  5936. ((org-try-cdlatex-tab))
  5937. ;; Table: enter it or move to the next field.
  5938. ((org-at-table-p 'any)
  5939. (if (org-at-table.el-p)
  5940. (message "%s" (substitute-command-keys "\\<org-mode-map>\
  5941. Use \\[org-edit-special] to edit table.el tables"))
  5942. (if arg (org-table-edit-field t)
  5943. (org-table-justify-field-maybe)
  5944. (call-interactively 'org-table-next-field))))
  5945. ((run-hook-with-args-until-success
  5946. 'org-tab-after-check-for-table-hook))
  5947. ;; Global cycling: delegate to `org-cycle-internal-global'.
  5948. ((eq arg t) (org-cycle-internal-global))
  5949. ;; Drawers: delegate to `org-flag-drawer'.
  5950. ((save-excursion
  5951. (beginning-of-line 1)
  5952. (looking-at org-drawer-regexp))
  5953. (org-flag-drawer ; toggle block visibility
  5954. (not (get-char-property (match-end 0) 'invisible))))
  5955. ;; Show-subtree, ARG levels up from here.
  5956. ((integerp arg)
  5957. (save-excursion
  5958. (org-back-to-heading)
  5959. (outline-up-heading (if (< arg 0) (- arg)
  5960. (- (funcall outline-level) arg)))
  5961. (org-show-subtree)))
  5962. ;; Inline task: delegate to `org-inlinetask-toggle-visibility'.
  5963. ((and (featurep 'org-inlinetask)
  5964. (org-inlinetask-at-task-p)
  5965. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5966. (org-inlinetask-toggle-visibility))
  5967. ;; At an item/headline: delegate to `org-cycle-internal-local'.
  5968. ((and (or (and org-cycle-include-plain-lists (org-at-item-p))
  5969. (save-excursion (move-beginning-of-line 1)
  5970. (looking-at org-outline-regexp)))
  5971. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5972. (org-cycle-internal-local))
  5973. ;; From there: TAB emulation and template completion.
  5974. (buffer-read-only (org-back-to-heading))
  5975. ((run-hook-with-args-until-success
  5976. 'org-tab-after-check-for-cycling-hook))
  5977. ((org-try-structure-completion))
  5978. ((run-hook-with-args-until-success
  5979. 'org-tab-before-tab-emulation-hook))
  5980. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  5981. (or (not (bolp))
  5982. (not (looking-at org-outline-regexp))))
  5983. (call-interactively (global-key-binding "\t")))
  5984. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  5985. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  5986. (or (and (eq org-cycle-emulate-tab 'white)
  5987. (= (match-end 0) (point-at-eol)))
  5988. (and (eq org-cycle-emulate-tab 'whitestart)
  5989. (>= (match-end 0) pos))))
  5990. t
  5991. (eq org-cycle-emulate-tab t))
  5992. (call-interactively (global-key-binding "\t")))
  5993. (t (save-excursion
  5994. (org-back-to-heading)
  5995. (org-cycle)))))))
  5996. (defun org-cycle-internal-global ()
  5997. "Do the global cycling action."
  5998. ;; Hack to avoid display of messages for .org attachments in Gnus
  5999. (let ((ga (string-match "\\*fontification" (buffer-name))))
  6000. (cond
  6001. ((and (eq last-command this-command)
  6002. (eq org-cycle-global-status 'overview))
  6003. ;; We just created the overview - now do table of contents
  6004. ;; This can be slow in very large buffers, so indicate action
  6005. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  6006. (unless ga (org-unlogged-message "CONTENTS..."))
  6007. (org-content)
  6008. (unless ga (org-unlogged-message "CONTENTS...done"))
  6009. (setq org-cycle-global-status 'contents)
  6010. (run-hook-with-args 'org-cycle-hook 'contents))
  6011. ((and (eq last-command this-command)
  6012. (eq org-cycle-global-status 'contents))
  6013. ;; We just showed the table of contents - now show everything
  6014. (run-hook-with-args 'org-pre-cycle-hook 'all)
  6015. (outline-show-all)
  6016. (unless ga (org-unlogged-message "SHOW ALL"))
  6017. (setq org-cycle-global-status 'all)
  6018. (run-hook-with-args 'org-cycle-hook 'all))
  6019. (t
  6020. ;; Default action: go to overview
  6021. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  6022. (org-overview)
  6023. (unless ga (org-unlogged-message "OVERVIEW"))
  6024. (setq org-cycle-global-status 'overview)
  6025. (run-hook-with-args 'org-cycle-hook 'overview)))))
  6026. (defvar org-called-with-limited-levels nil
  6027. "Non-nil when `org-with-limited-levels' is currently active.")
  6028. (defun org-cycle-internal-local ()
  6029. "Do the local cycling action."
  6030. (let ((goal-column 0) eoh eol eos has-children children-skipped struct)
  6031. ;; First, determine end of headline (EOH), end of subtree or item
  6032. ;; (EOS), and if item or heading has children (HAS-CHILDREN).
  6033. (save-excursion
  6034. (if (org-at-item-p)
  6035. (progn
  6036. (beginning-of-line)
  6037. (setq struct (org-list-struct))
  6038. (setq eoh (point-at-eol))
  6039. (setq eos (org-list-get-item-end-before-blank (point) struct))
  6040. (setq has-children (org-list-has-child-p (point) struct)))
  6041. (org-back-to-heading)
  6042. (setq eoh (save-excursion (outline-end-of-heading) (point)))
  6043. (setq eos (save-excursion (org-end-of-subtree t t)
  6044. (when (bolp) (backward-char)) (point)))
  6045. (setq has-children
  6046. (or (save-excursion
  6047. (let ((level (funcall outline-level)))
  6048. (outline-next-heading)
  6049. (and (org-at-heading-p t)
  6050. (> (funcall outline-level) level))))
  6051. (save-excursion
  6052. (org-list-search-forward (org-item-beginning-re) eos t)))))
  6053. ;; Determine end invisible part of buffer (EOL)
  6054. (beginning-of-line 2)
  6055. ;; XEmacs doesn't have `next-single-char-property-change'
  6056. (if (featurep 'xemacs)
  6057. (while (and (not (eobp)) ;; this is like `next-line'
  6058. (get-char-property (1- (point)) 'invisible))
  6059. (beginning-of-line 2))
  6060. (while (and (not (eobp)) ;; this is like `next-line'
  6061. (get-char-property (1- (point)) 'invisible))
  6062. (goto-char (next-single-char-property-change (point) 'invisible))
  6063. (and (eolp) (beginning-of-line 2))))
  6064. (setq eol (point)))
  6065. ;; Find out what to do next and set `this-command'
  6066. (cond
  6067. ((= eos eoh)
  6068. ;; Nothing is hidden behind this heading
  6069. (unless (org-before-first-heading-p)
  6070. (run-hook-with-args 'org-pre-cycle-hook 'empty))
  6071. (org-unlogged-message "EMPTY ENTRY")
  6072. (setq org-cycle-subtree-status nil)
  6073. (save-excursion
  6074. (goto-char eos)
  6075. (outline-next-heading)
  6076. (when (outline-invisible-p) (org-flag-heading nil))))
  6077. ((and (or (>= eol eos)
  6078. (not (string-match "\\S-" (buffer-substring eol eos))))
  6079. (or has-children
  6080. (not (setq children-skipped
  6081. org-cycle-skip-children-state-if-no-children))))
  6082. ;; Entire subtree is hidden in one line: children view
  6083. (unless (org-before-first-heading-p)
  6084. (run-hook-with-args 'org-pre-cycle-hook 'children))
  6085. (if (org-at-item-p)
  6086. (org-list-set-item-visibility (point-at-bol) struct 'children)
  6087. (org-show-entry)
  6088. (org-with-limited-levels (org-show-children))
  6089. ;; FIXME: This slows down the func way too much.
  6090. ;; How keep drawers hidden in subtree anyway?
  6091. ;; (when (memq 'org-cycle-hide-drawers org-cycle-hook)
  6092. ;; (org-cycle-hide-drawers 'subtree))
  6093. ;; Fold every list in subtree to top-level items.
  6094. (when (eq org-cycle-include-plain-lists 'integrate)
  6095. (save-excursion
  6096. (org-back-to-heading)
  6097. (while (org-list-search-forward (org-item-beginning-re) eos t)
  6098. (beginning-of-line 1)
  6099. (let* ((struct (org-list-struct))
  6100. (prevs (org-list-prevs-alist struct))
  6101. (end (org-list-get-bottom-point struct)))
  6102. (mapc (lambda (e) (org-list-set-item-visibility e struct 'folded))
  6103. (org-list-get-all-items (point) struct prevs))
  6104. (goto-char (if (< end eos) end eos)))))))
  6105. (org-unlogged-message "CHILDREN")
  6106. (save-excursion
  6107. (goto-char eos)
  6108. (outline-next-heading)
  6109. (when (outline-invisible-p) (org-flag-heading nil)))
  6110. (setq org-cycle-subtree-status 'children)
  6111. (unless (org-before-first-heading-p)
  6112. (run-hook-with-args 'org-cycle-hook 'children)))
  6113. ((or children-skipped
  6114. (and (eq last-command this-command)
  6115. (eq org-cycle-subtree-status 'children)))
  6116. ;; We just showed the children, or no children are there,
  6117. ;; now show everything.
  6118. (unless (org-before-first-heading-p)
  6119. (run-hook-with-args 'org-pre-cycle-hook 'subtree))
  6120. (outline-flag-region eoh eos nil)
  6121. (org-unlogged-message
  6122. (if children-skipped "SUBTREE (NO CHILDREN)" "SUBTREE"))
  6123. (setq org-cycle-subtree-status 'subtree)
  6124. (unless (org-before-first-heading-p)
  6125. (run-hook-with-args 'org-cycle-hook 'subtree)))
  6126. (t
  6127. ;; Default action: hide the subtree.
  6128. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  6129. (outline-flag-region eoh eos t)
  6130. (org-unlogged-message "FOLDED")
  6131. (setq org-cycle-subtree-status 'folded)
  6132. (unless (org-before-first-heading-p)
  6133. (run-hook-with-args 'org-cycle-hook 'folded))))))
  6134. ;;;###autoload
  6135. (defun org-global-cycle (&optional arg)
  6136. "Cycle the global visibility. For details see `org-cycle'.
  6137. With \\[universal-argument] prefix arg, switch to startup visibility.
  6138. With a numeric prefix, show all headlines up to that level."
  6139. (interactive "P")
  6140. (let ((org-cycle-include-plain-lists
  6141. (if (derived-mode-p 'org-mode) org-cycle-include-plain-lists nil)))
  6142. (cond
  6143. ((integerp arg)
  6144. (outline-show-all)
  6145. (outline-hide-sublevels arg)
  6146. (setq org-cycle-global-status 'contents))
  6147. ((equal arg '(4))
  6148. (org-set-startup-visibility)
  6149. (org-unlogged-message "Startup visibility, plus VISIBILITY properties."))
  6150. (t
  6151. (org-cycle '(4))))))
  6152. (defun org-set-startup-visibility ()
  6153. "Set the visibility required by startup options and properties."
  6154. (cond
  6155. ((eq org-startup-folded t)
  6156. (org-overview))
  6157. ((eq org-startup-folded 'content)
  6158. (org-content))
  6159. ((or (eq org-startup-folded 'showeverything)
  6160. (eq org-startup-folded nil))
  6161. (outline-show-all)))
  6162. (unless (eq org-startup-folded 'showeverything)
  6163. (when org-hide-block-startup (org-hide-block-all))
  6164. (org-set-visibility-according-to-property 'no-cleanup)
  6165. (org-cycle-hide-archived-subtrees 'all)
  6166. (org-cycle-hide-drawers 'all)
  6167. (org-cycle-show-empty-lines t)))
  6168. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  6169. "Switch subtree visibilities according to :VISIBILITY: property."
  6170. (interactive)
  6171. (let (org-show-entry-below)
  6172. (org-with-wide-buffer
  6173. (goto-char (point-min))
  6174. (while (re-search-forward "^[ \t]*:VISIBILITY:" nil t)
  6175. (if (not (org-at-property-p)) (outline-next-heading)
  6176. (let ((state (match-string 3)))
  6177. (save-excursion
  6178. (org-back-to-heading t)
  6179. (outline-hide-subtree)
  6180. (org-reveal)
  6181. (cond
  6182. ((equal state "folded")
  6183. (outline-hide-subtree))
  6184. ((equal state "children")
  6185. (org-show-hidden-entry)
  6186. (org-show-children))
  6187. ((equal state "content")
  6188. (save-excursion
  6189. (save-restriction
  6190. (org-narrow-to-subtree)
  6191. (org-content))))
  6192. ((member state '("all" "showall"))
  6193. (outline-show-subtree)))))))
  6194. (unless no-cleanup
  6195. (org-cycle-hide-archived-subtrees 'all)
  6196. (org-cycle-hide-drawers 'all)
  6197. (org-cycle-show-empty-lines 'all)))))
  6198. ;; This function uses outline-regexp instead of the more fundamental
  6199. ;; org-outline-regexp so that org-cycle-global works outside of Org
  6200. ;; buffers, where outline-regexp is needed.
  6201. (defun org-overview ()
  6202. "Switch to overview mode, showing only top-level headlines.
  6203. This shows all headlines with a level equal or greater than the level
  6204. of the first headline in the buffer. This is important, because if the
  6205. first headline is not level one, then (hide-sublevels 1) gives confusing
  6206. results."
  6207. (interactive)
  6208. (save-excursion
  6209. (let ((level
  6210. (save-excursion
  6211. (goto-char (point-min))
  6212. (when (re-search-forward (concat "^" outline-regexp) nil t)
  6213. (goto-char (match-beginning 0))
  6214. (funcall outline-level)))))
  6215. (and level (outline-hide-sublevels level)))))
  6216. (defun org-content (&optional arg)
  6217. "Show all headlines in the buffer, like a table of contents.
  6218. With numerical argument N, show content up to level N."
  6219. (interactive "P")
  6220. (org-overview)
  6221. (save-excursion
  6222. ;; Visit all headings and show their offspring
  6223. (and (integerp arg) (org-overview))
  6224. (goto-char (point-max))
  6225. (catch 'exit
  6226. (while (and (progn (condition-case nil
  6227. (outline-previous-visible-heading 1)
  6228. (error (goto-char (point-min))))
  6229. t)
  6230. (looking-at org-outline-regexp))
  6231. (if (integerp arg)
  6232. (org-show-children (1- arg))
  6233. (outline-show-branches))
  6234. (when (bobp) (throw 'exit nil))))))
  6235. (defun org-optimize-window-after-visibility-change (state)
  6236. "Adjust the window after a change in outline visibility.
  6237. This function is the default value of the hook `org-cycle-hook'."
  6238. (when (get-buffer-window (current-buffer))
  6239. (cond
  6240. ((eq state 'content) nil)
  6241. ((eq state 'all) nil)
  6242. ((eq state 'folded) nil)
  6243. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  6244. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  6245. (defun org-remove-empty-overlays-at (pos)
  6246. "Remove outline overlays that do not contain non-white stuff."
  6247. (mapc
  6248. (lambda (o)
  6249. (and (eq 'outline (overlay-get o 'invisible))
  6250. (not (string-match "\\S-" (buffer-substring (overlay-start o)
  6251. (overlay-end o))))
  6252. (delete-overlay o)))
  6253. (overlays-at pos)))
  6254. (defun org-clean-visibility-after-subtree-move ()
  6255. "Fix visibility issues after moving a subtree."
  6256. ;; First, find a reasonable region to look at:
  6257. ;; Start two siblings above, end three below
  6258. (let* ((beg (save-excursion
  6259. (and (org-get-last-sibling)
  6260. (org-get-last-sibling))
  6261. (point)))
  6262. (end (save-excursion
  6263. (and (org-get-next-sibling)
  6264. (org-get-next-sibling)
  6265. (org-get-next-sibling))
  6266. (if (org-at-heading-p)
  6267. (point-at-eol)
  6268. (point))))
  6269. (level (looking-at "\\*+"))
  6270. (re (when level (concat "^" (regexp-quote (match-string 0)) " "))))
  6271. (save-excursion
  6272. (save-restriction
  6273. (narrow-to-region beg end)
  6274. (when re
  6275. ;; Properly fold already folded siblings
  6276. (goto-char (point-min))
  6277. (while (re-search-forward re nil t)
  6278. (when (and (not (outline-invisible-p))
  6279. (save-excursion
  6280. (goto-char (point-at-eol)) (outline-invisible-p)))
  6281. (outline-hide-entry))))
  6282. (org-cycle-show-empty-lines 'overview)
  6283. (org-cycle-hide-drawers 'overview)))))
  6284. (defun org-cycle-show-empty-lines (state)
  6285. "Show empty lines above all visible headlines.
  6286. The region to be covered depends on STATE when called through
  6287. `org-cycle-hook'. Lisp program can use t for STATE to get the
  6288. entire buffer covered. Note that an empty line is only shown if there
  6289. are at least `org-cycle-separator-lines' empty lines before the headline."
  6290. (when (/= org-cycle-separator-lines 0)
  6291. (save-excursion
  6292. (let* ((n (abs org-cycle-separator-lines))
  6293. (re (cond
  6294. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  6295. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  6296. (t (let ((ns (number-to-string (- n 2))))
  6297. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  6298. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  6299. beg end)
  6300. (cond
  6301. ((memq state '(overview contents t))
  6302. (setq beg (point-min) end (point-max)))
  6303. ((memq state '(children folded))
  6304. (setq beg (point)
  6305. end (progn (org-end-of-subtree t t)
  6306. (line-beginning-position 2)))))
  6307. (when beg
  6308. (goto-char beg)
  6309. (while (re-search-forward re end t)
  6310. (unless (get-char-property (match-end 1) 'invisible)
  6311. (let ((e (match-end 1))
  6312. (b (if (>= org-cycle-separator-lines 0)
  6313. (match-beginning 1)
  6314. (save-excursion
  6315. (goto-char (match-beginning 0))
  6316. (skip-chars-backward " \t\n")
  6317. (line-end-position)))))
  6318. (outline-flag-region b e nil))))))))
  6319. ;; Never hide empty lines at the end of the file.
  6320. (save-excursion
  6321. (goto-char (point-max))
  6322. (outline-previous-heading)
  6323. (outline-end-of-heading)
  6324. (when (and (looking-at "[ \t\n]+")
  6325. (= (match-end 0) (point-max)))
  6326. (outline-flag-region (point) (match-end 0) nil))))
  6327. (defun org-show-empty-lines-in-parent ()
  6328. "Move to the parent and re-show empty lines before visible headlines."
  6329. (save-excursion
  6330. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  6331. (org-cycle-show-empty-lines context))))
  6332. (defun org-files-list ()
  6333. "Return `org-agenda-files' list, plus all open org-mode files.
  6334. This is useful for operations that need to scan all of a user's
  6335. open and agenda-wise Org files."
  6336. (let ((files (mapcar 'expand-file-name (org-agenda-files))))
  6337. (dolist (buf (buffer-list))
  6338. (with-current-buffer buf
  6339. (when (and (derived-mode-p 'org-mode) (buffer-file-name))
  6340. (cl-pushnew (expand-file-name (buffer-file-name)) files))))
  6341. files))
  6342. (defsubst org-entry-beginning-position ()
  6343. "Return the beginning position of the current entry."
  6344. (save-excursion (outline-back-to-heading t) (point)))
  6345. (defsubst org-entry-end-position ()
  6346. "Return the end position of the current entry."
  6347. (save-excursion (outline-next-heading) (point)))
  6348. (defun org-cycle-hide-drawers (state &optional exceptions)
  6349. "Re-hide all drawers after a visibility state change.
  6350. When non-nil, optional argument EXCEPTIONS is a list of strings
  6351. specifying which drawers should not be hidden."
  6352. (when (and (derived-mode-p 'org-mode)
  6353. (not (memq state '(overview folded contents))))
  6354. (save-excursion
  6355. (let* ((globalp (memq state '(contents all)))
  6356. (beg (if globalp (point-min) (point)))
  6357. (end (if globalp (point-max)
  6358. (if (eq state 'children)
  6359. (save-excursion (outline-next-heading) (point))
  6360. (org-end-of-subtree t)))))
  6361. (goto-char beg)
  6362. (while (re-search-forward org-drawer-regexp (max end (point)) t)
  6363. (unless (member-ignore-case (match-string 1) exceptions)
  6364. (let ((drawer (org-element-at-point)))
  6365. (when (memq (org-element-type drawer) '(drawer property-drawer))
  6366. (org-flag-drawer t drawer)
  6367. ;; Make sure to skip drawer entirely or we might flag
  6368. ;; it another time when matching its ending line with
  6369. ;; `org-drawer-regexp'.
  6370. (goto-char (org-element-property :end drawer))))))))))
  6371. (defun org-flag-drawer (flag &optional element)
  6372. "When FLAG is non-nil, hide the drawer we are at.
  6373. Otherwise make it visible. When optional argument ELEMENT is
  6374. a parsed drawer, as returned by `org-element-at-point', hide or
  6375. show that drawer instead."
  6376. (when (save-excursion
  6377. (beginning-of-line)
  6378. (org-looking-at-p org-drawer-regexp))
  6379. (let ((drawer (or element (org-element-at-point))))
  6380. (when (memq (org-element-type drawer) '(drawer property-drawer))
  6381. (let ((post (org-element-property :post-affiliated drawer)))
  6382. (save-excursion
  6383. (outline-flag-region
  6384. (progn (goto-char post) (line-end-position))
  6385. (progn (goto-char (org-element-property :end drawer))
  6386. (skip-chars-backward " \r\t\n")
  6387. (line-end-position))
  6388. flag))
  6389. ;; When the drawer is hidden away, make sure point lies in
  6390. ;; a visible part of the buffer.
  6391. (when (and flag (> (line-beginning-position) post))
  6392. (goto-char post)))))))
  6393. (defun org-subtree-end-visible-p ()
  6394. "Is the end of the current subtree visible?"
  6395. (pos-visible-in-window-p
  6396. (save-excursion (org-end-of-subtree t) (point))))
  6397. (defun org-first-headline-recenter ()
  6398. "Move cursor to the first headline and recenter the headline."
  6399. (goto-char (point-min))
  6400. (when (re-search-forward (concat "^\\(" org-outline-regexp "\\)") nil t)
  6401. (set-window-start (selected-window) (point-at-bol))))
  6402. ;;; Saving and restoring visibility
  6403. (defun org-outline-overlay-data (&optional use-markers)
  6404. "Return a list of the locations of all outline overlays.
  6405. These are overlays with the `invisible' property value `outline'.
  6406. The return value is a list of cons cells, with start and stop
  6407. positions for each overlay.
  6408. If USE-MARKERS is set, return the positions as markers."
  6409. (let (beg end)
  6410. (save-excursion
  6411. (save-restriction
  6412. (widen)
  6413. (delq nil
  6414. (mapcar (lambda (o)
  6415. (when (eq (overlay-get o 'invisible) 'outline)
  6416. (setq beg (overlay-start o)
  6417. end (overlay-end o))
  6418. (and beg end (> end beg)
  6419. (if use-markers
  6420. (cons (copy-marker beg)
  6421. (copy-marker end t))
  6422. (cons beg end)))))
  6423. (overlays-in (point-min) (point-max))))))))
  6424. (defun org-set-outline-overlay-data (data)
  6425. "Create visibility overlays for all positions in DATA.
  6426. DATA should have been made by `org-outline-overlay-data'."
  6427. (let (o)
  6428. (save-excursion
  6429. (save-restriction
  6430. (widen)
  6431. (outline-show-all)
  6432. (mapc (lambda (c)
  6433. (outline-flag-region (car c) (cdr c) t))
  6434. data)))))
  6435. ;;; Folding of blocks
  6436. (defvar-local org-hide-block-overlays nil
  6437. "Overlays hiding blocks.")
  6438. (defun org-block-map (function &optional start end)
  6439. "Call FUNCTION at the head of all source blocks in the current buffer.
  6440. Optional arguments START and END can be used to limit the range."
  6441. (let ((start (or start (point-min)))
  6442. (end (or end (point-max))))
  6443. (save-excursion
  6444. (goto-char start)
  6445. (while (and (< (point) end) (re-search-forward org-block-regexp end t))
  6446. (save-excursion
  6447. (save-match-data
  6448. (goto-char (match-beginning 0))
  6449. (funcall function)))))))
  6450. (defun org-hide-block-toggle-all ()
  6451. "Toggle the visibility of all blocks in the current buffer."
  6452. (org-block-map 'org-hide-block-toggle))
  6453. (defun org-hide-block-all ()
  6454. "Fold all blocks in the current buffer."
  6455. (interactive)
  6456. (org-show-block-all)
  6457. (org-block-map 'org-hide-block-toggle-maybe))
  6458. (defun org-show-block-all ()
  6459. "Unfold all blocks in the current buffer."
  6460. (interactive)
  6461. (mapc 'delete-overlay org-hide-block-overlays)
  6462. (setq org-hide-block-overlays nil))
  6463. (defun org-hide-block-toggle-maybe ()
  6464. "Toggle visibility of block at point.
  6465. Unlike to `org-hide-block-toggle', this function does not throw
  6466. an error. Return a non-nil value when toggling is successful."
  6467. (interactive)
  6468. (ignore-errors (org-hide-block-toggle)))
  6469. (defun org-hide-block-toggle (&optional force)
  6470. "Toggle the visibility of the current block.
  6471. When optional argument FORCE is `off', make block visible. If it
  6472. is non-nil, hide it unconditionally. Throw an error when not at
  6473. a block. Return a non-nil value when toggling is successful."
  6474. (interactive)
  6475. (let ((element (org-element-at-point)))
  6476. (unless (memq (org-element-type element)
  6477. '(center-block comment-block dynamic-block example-block
  6478. export-block quote-block special-block
  6479. src-block verse-block))
  6480. (user-error "Not at a block"))
  6481. (let* ((start (save-excursion
  6482. (goto-char (org-element-property :post-affiliated element))
  6483. (line-end-position)))
  6484. (end (save-excursion
  6485. (goto-char (org-element-property :end element))
  6486. (skip-chars-backward " \r\t\n")
  6487. (line-end-position)))
  6488. (overlays (overlays-at start)))
  6489. (cond
  6490. ;; Do nothing when not before or at the block opening line or
  6491. ;; at the block closing line.
  6492. ((let ((eol (line-end-position))) (and (> eol start) (/= eol end))) nil)
  6493. ((and (not (eq force 'off))
  6494. (not (memq t (mapcar
  6495. (lambda (o)
  6496. (eq (overlay-get o 'invisible) 'org-hide-block))
  6497. overlays))))
  6498. (let ((ov (make-overlay start end)))
  6499. (overlay-put ov 'invisible 'org-hide-block)
  6500. ;; Make the block accessible to `isearch'.
  6501. (overlay-put
  6502. ov 'isearch-open-invisible
  6503. (lambda (ov)
  6504. (when (memq ov org-hide-block-overlays)
  6505. (setq org-hide-block-overlays (delq ov org-hide-block-overlays)))
  6506. (when (eq (overlay-get ov 'invisible) 'org-hide-block)
  6507. (delete-overlay ov))))
  6508. (push ov org-hide-block-overlays)
  6509. ;; When the block is hidden away, make sure point is left in
  6510. ;; a visible part of the buffer.
  6511. (when (> (line-beginning-position) start)
  6512. (goto-char start)
  6513. (beginning-of-line))
  6514. ;; Signal successful toggling.
  6515. t))
  6516. ((or (not force) (eq force 'off))
  6517. (dolist (ov overlays t)
  6518. (when (memq ov org-hide-block-overlays)
  6519. (setq org-hide-block-overlays (delq ov org-hide-block-overlays)))
  6520. (when (eq (overlay-get ov 'invisible) 'org-hide-block)
  6521. (delete-overlay ov))))))))
  6522. ;; org-tab-after-check-for-cycling-hook
  6523. (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
  6524. ;; Remove overlays when changing major mode
  6525. (add-hook 'org-mode-hook
  6526. (lambda () (org-add-hook 'change-major-mode-hook
  6527. 'org-show-block-all 'append 'local)))
  6528. ;;; Org-goto
  6529. (defvar org-goto-window-configuration nil)
  6530. (defvar org-goto-marker nil)
  6531. (defvar org-goto-map)
  6532. (defun org-goto-map ()
  6533. "Set the keymap `org-goto'."
  6534. (setq org-goto-map
  6535. (let ((map (make-sparse-keymap)))
  6536. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command
  6537. mouse-drag-region universal-argument org-occur)))
  6538. (dolist (cmd cmds)
  6539. (substitute-key-definition cmd cmd map global-map)))
  6540. (suppress-keymap map)
  6541. (org-defkey map "\C-m" 'org-goto-ret)
  6542. (org-defkey map [(return)] 'org-goto-ret)
  6543. (org-defkey map [(left)] 'org-goto-left)
  6544. (org-defkey map [(right)] 'org-goto-right)
  6545. (org-defkey map [(control ?g)] 'org-goto-quit)
  6546. (org-defkey map "\C-i" 'org-cycle)
  6547. (org-defkey map [(tab)] 'org-cycle)
  6548. (org-defkey map [(down)] 'outline-next-visible-heading)
  6549. (org-defkey map [(up)] 'outline-previous-visible-heading)
  6550. (if org-goto-auto-isearch
  6551. (if (fboundp 'define-key-after)
  6552. (define-key-after map [t] 'org-goto-local-auto-isearch)
  6553. nil)
  6554. (org-defkey map "q" 'org-goto-quit)
  6555. (org-defkey map "n" 'outline-next-visible-heading)
  6556. (org-defkey map "p" 'outline-previous-visible-heading)
  6557. (org-defkey map "f" 'outline-forward-same-level)
  6558. (org-defkey map "b" 'outline-backward-same-level)
  6559. (org-defkey map "u" 'outline-up-heading))
  6560. (org-defkey map "/" 'org-occur)
  6561. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  6562. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  6563. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  6564. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  6565. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  6566. map)))
  6567. (defconst org-goto-help
  6568. "Browse buffer copy, to find location or copy text.%s
  6569. RET=jump to location C-g=quit and return to previous location
  6570. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  6571. (defvar org-goto-start-pos) ; dynamically scoped parameter
  6572. (defun org-goto (&optional alternative-interface)
  6573. "Look up a different location in the current file, keeping current visibility.
  6574. When you want look-up or go to a different location in a
  6575. document, the fastest way is often to fold the entire buffer and
  6576. then dive into the tree. This method has the disadvantage, that
  6577. the previous location will be folded, which may not be what you
  6578. want.
  6579. This command works around this by showing a copy of the current
  6580. buffer in an indirect buffer, in overview mode. You can dive
  6581. into the tree in that copy, use org-occur and incremental search
  6582. to find a location. When pressing RET or `Q', the command
  6583. returns to the original buffer in which the visibility is still
  6584. unchanged. After RET it will also jump to the location selected
  6585. in the indirect buffer and expose the headline hierarchy above.
  6586. With a prefix argument, use the alternative interface: e.g., if
  6587. `org-goto-interface' is `outline' use `outline-path-completion'."
  6588. (interactive "P")
  6589. (org-goto-map)
  6590. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  6591. (org-refile-use-outline-path t)
  6592. (org-refile-target-verify-function nil)
  6593. (interface
  6594. (if (not alternative-interface)
  6595. org-goto-interface
  6596. (if (eq org-goto-interface 'outline)
  6597. 'outline-path-completion
  6598. 'outline)))
  6599. (org-goto-start-pos (point))
  6600. (selected-point
  6601. (if (eq interface 'outline)
  6602. (car (org-get-location (current-buffer) org-goto-help))
  6603. (let ((pa (org-refile-get-location "Goto" nil nil t)))
  6604. (org-refile-check-position pa)
  6605. (nth 3 pa)))))
  6606. (if selected-point
  6607. (progn
  6608. (org-mark-ring-push org-goto-start-pos)
  6609. (goto-char selected-point)
  6610. (when (or (outline-invisible-p) (org-invisible-p2))
  6611. (org-show-context 'org-goto)))
  6612. (message "Quit"))))
  6613. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  6614. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  6615. (defvar org-goto-local-auto-isearch-map) ; defined below
  6616. (defun org-get-location (buf help)
  6617. "Let the user select a location in the Org-mode buffer BUF.
  6618. This function uses a recursive edit. It returns the selected position
  6619. or nil."
  6620. (org-no-popups
  6621. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  6622. (isearch-hide-immediately nil)
  6623. (isearch-search-fun-function
  6624. (lambda () 'org-goto-local-search-headings))
  6625. (org-goto-selected-point org-goto-exit-command))
  6626. (save-excursion
  6627. (save-window-excursion
  6628. (delete-other-windows)
  6629. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  6630. (org-pop-to-buffer-same-window
  6631. (condition-case nil
  6632. (make-indirect-buffer (current-buffer) "*org-goto*")
  6633. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  6634. (with-output-to-temp-buffer "*Org Help*"
  6635. (princ (format help (if org-goto-auto-isearch
  6636. " Just type for auto-isearch."
  6637. " n/p/f/b/u to navigate, q to quit."))))
  6638. (org-fit-window-to-buffer (get-buffer-window "*Org Help*"))
  6639. (setq buffer-read-only nil)
  6640. (let ((org-startup-truncated t)
  6641. (org-startup-folded nil)
  6642. (org-startup-align-all-tables nil))
  6643. (org-mode)
  6644. (org-overview))
  6645. (setq buffer-read-only t)
  6646. (if (and (boundp 'org-goto-start-pos)
  6647. (integer-or-marker-p org-goto-start-pos))
  6648. (progn (goto-char org-goto-start-pos)
  6649. (when (outline-invisible-p)
  6650. (org-show-set-visibility 'lineage)))
  6651. (goto-char (point-min)))
  6652. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  6653. (message "Select location and press RET")
  6654. (use-local-map org-goto-map)
  6655. (recursive-edit)))
  6656. (kill-buffer "*org-goto*")
  6657. (cons org-goto-selected-point org-goto-exit-command))))
  6658. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  6659. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  6660. ;; `isearch-other-control-char' was removed in Emacs 24.4.
  6661. (if (fboundp 'isearch-other-control-char)
  6662. (progn
  6663. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  6664. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char))
  6665. (define-key org-goto-local-auto-isearch-map "\C-i" nil)
  6666. (define-key org-goto-local-auto-isearch-map "\C-m" nil)
  6667. (define-key org-goto-local-auto-isearch-map [return] nil))
  6668. (defun org-goto-local-search-headings (string bound noerror)
  6669. "Search and make sure that any matches are in headlines."
  6670. (catch 'return
  6671. (while (if isearch-forward
  6672. (search-forward string bound noerror)
  6673. (search-backward string bound noerror))
  6674. (when (save-match-data
  6675. (and (save-excursion
  6676. (beginning-of-line)
  6677. (looking-at org-complex-heading-regexp))
  6678. (or (not (match-beginning 5))
  6679. (< (point) (match-beginning 5)))))
  6680. (throw 'return (point))))))
  6681. (defun org-goto-local-auto-isearch ()
  6682. "Start isearch."
  6683. (interactive)
  6684. (goto-char (point-min))
  6685. (let ((keys (this-command-keys)))
  6686. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  6687. (isearch-mode t)
  6688. (isearch-process-search-char (string-to-char keys)))))
  6689. (defun org-goto-ret (&optional arg)
  6690. "Finish `org-goto' by going to the new location."
  6691. (interactive "P")
  6692. (setq org-goto-selected-point (point)
  6693. org-goto-exit-command 'return)
  6694. (throw 'exit nil))
  6695. (defun org-goto-left ()
  6696. "Finish `org-goto' by going to the new location."
  6697. (interactive)
  6698. (if (org-at-heading-p)
  6699. (progn
  6700. (beginning-of-line 1)
  6701. (setq org-goto-selected-point (point)
  6702. org-goto-exit-command 'left)
  6703. (throw 'exit nil))
  6704. (user-error "Not on a heading")))
  6705. (defun org-goto-right ()
  6706. "Finish `org-goto' by going to the new location."
  6707. (interactive)
  6708. (if (org-at-heading-p)
  6709. (progn
  6710. (setq org-goto-selected-point (point)
  6711. org-goto-exit-command 'right)
  6712. (throw 'exit nil))
  6713. (user-error "Not on a heading")))
  6714. (defun org-goto-quit ()
  6715. "Finish `org-goto' without cursor motion."
  6716. (interactive)
  6717. (setq org-goto-selected-point nil)
  6718. (setq org-goto-exit-command 'quit)
  6719. (throw 'exit nil))
  6720. ;;; Indirect buffer display of subtrees
  6721. (defvar org-indirect-dedicated-frame nil
  6722. "This is the frame being used for indirect tree display.")
  6723. (defvar org-last-indirect-buffer nil)
  6724. (defun org-tree-to-indirect-buffer (&optional arg)
  6725. "Create indirect buffer and narrow it to current subtree.
  6726. With a numerical prefix ARG, go up to this level and then take that tree.
  6727. If ARG is negative, go up that many levels.
  6728. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  6729. indirect buffer previously made with this command, to avoid proliferation of
  6730. indirect buffers. However, when you call the command with a \
  6731. \\[universal-argument] prefix, or
  6732. when `org-indirect-buffer-display' is `new-frame', the last buffer
  6733. is kept so that you can work with several indirect buffers at the same time.
  6734. If `org-indirect-buffer-display' is `dedicated-frame', the \
  6735. \\[universal-argument] prefix also
  6736. requests that a new frame be made for the new buffer, so that the dedicated
  6737. frame is not changed."
  6738. (interactive "P")
  6739. (let ((cbuf (current-buffer))
  6740. (cwin (selected-window))
  6741. (pos (point))
  6742. beg end level heading ibuf)
  6743. (save-excursion
  6744. (org-back-to-heading t)
  6745. (when (numberp arg)
  6746. (setq level (org-outline-level))
  6747. (when (< arg 0) (setq arg (+ level arg)))
  6748. (while (> (setq level (org-outline-level)) arg)
  6749. (org-up-heading-safe)))
  6750. (setq beg (point)
  6751. heading (org-get-heading))
  6752. (org-end-of-subtree t t)
  6753. (when (org-at-heading-p) (backward-char 1))
  6754. (setq end (point)))
  6755. (when (and (buffer-live-p org-last-indirect-buffer)
  6756. (not (eq org-indirect-buffer-display 'new-frame))
  6757. (not arg))
  6758. (kill-buffer org-last-indirect-buffer))
  6759. (setq ibuf (org-get-indirect-buffer cbuf heading)
  6760. org-last-indirect-buffer ibuf)
  6761. (cond
  6762. ((or (eq org-indirect-buffer-display 'new-frame)
  6763. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  6764. (select-frame (make-frame))
  6765. (delete-other-windows)
  6766. (org-pop-to-buffer-same-window ibuf)
  6767. (org-set-frame-title heading))
  6768. ((eq org-indirect-buffer-display 'dedicated-frame)
  6769. (raise-frame
  6770. (select-frame (or (and org-indirect-dedicated-frame
  6771. (frame-live-p org-indirect-dedicated-frame)
  6772. org-indirect-dedicated-frame)
  6773. (setq org-indirect-dedicated-frame (make-frame)))))
  6774. (delete-other-windows)
  6775. (org-pop-to-buffer-same-window ibuf)
  6776. (org-set-frame-title (concat "Indirect: " heading)))
  6777. ((eq org-indirect-buffer-display 'current-window)
  6778. (org-pop-to-buffer-same-window ibuf))
  6779. ((eq org-indirect-buffer-display 'other-window)
  6780. (pop-to-buffer ibuf))
  6781. (t (error "Invalid value")))
  6782. (when (featurep 'xemacs)
  6783. (save-excursion (org-mode) (turn-on-font-lock)))
  6784. (narrow-to-region beg end)
  6785. (outline-show-all)
  6786. (goto-char pos)
  6787. (run-hook-with-args 'org-cycle-hook 'all)
  6788. (and (window-live-p cwin) (select-window cwin))))
  6789. (defun org-get-indirect-buffer (&optional buffer heading)
  6790. (setq buffer (or buffer (current-buffer)))
  6791. (let ((n 1) (base (buffer-name buffer)) bname)
  6792. (while (buffer-live-p
  6793. (get-buffer
  6794. (setq bname
  6795. (concat base "-"
  6796. (if heading (concat heading "-" (number-to-string n))
  6797. (number-to-string n))))))
  6798. (setq n (1+ n)))
  6799. (condition-case nil
  6800. (make-indirect-buffer buffer bname 'clone)
  6801. (error (make-indirect-buffer buffer bname)))))
  6802. (defun org-set-frame-title (title)
  6803. "Set the title of the current frame to the string TITLE."
  6804. ;; FIXME: how to name a single frame in XEmacs???
  6805. (unless (featurep 'xemacs)
  6806. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  6807. ;;;; Structure editing
  6808. ;;; Inserting headlines
  6809. (defun org--line-empty-p (n)
  6810. "Is the Nth next line empty?
  6811. Counts the current line as N = 1 and the previous line as N = 0;
  6812. see `beginning-of-line'."
  6813. (save-excursion
  6814. (and (not (bobp))
  6815. (or (beginning-of-line n) t)
  6816. (save-match-data
  6817. (looking-at "[ \t]*$")))))
  6818. (defun org-previous-line-empty-p ()
  6819. "Is the previous line a blank line?
  6820. When NEXT is non-nil, check the next line instead."
  6821. (org--line-empty-p 0))
  6822. (defun org-next-line-empty-p ()
  6823. "Is the previous line a blank line?
  6824. When NEXT is non-nil, check the next line instead."
  6825. (org--line-empty-p 2))
  6826. (defun org-insert-heading (&optional arg invisible-ok top-level)
  6827. "Insert a new heading or an item with the same depth at point.
  6828. If point is at the beginning of a heading or a list item, insert
  6829. a new heading or a new item above the current one. If point is
  6830. at the beginning of a normal line, turn the line into a heading.
  6831. If point is in the middle of a headline or a list item, split the
  6832. headline or the item and create a new headline/item with the text
  6833. in the current line after point \(see `org-M-RET-may-split-line'
  6834. on how to modify this behavior).
  6835. With one universal prefix argument, set the user option
  6836. `org-insert-heading-respect-content' to t for the duration of
  6837. the command. This modifies the behavior described above in this
  6838. ways: on list items and at the beginning of normal lines, force
  6839. the insertion of a heading after the current subtree.
  6840. With two universal prefix arguments, insert the heading at the
  6841. end of the grandparent subtree. For example, if point is within
  6842. a 2nd-level heading, then it will insert a 2nd-level heading at
  6843. the end of the 1st-level parent heading.
  6844. If point is at the beginning of a headline, insert a sibling
  6845. before the current headline. If point is not at the beginning,
  6846. split the line and create a new headline with the text in the
  6847. current line after point \(see `org-M-RET-may-split-line' on how
  6848. to modify this behavior).
  6849. If point is at the beginning of a normal line, turn this line
  6850. into a heading.
  6851. When INVISIBLE-OK is set, stop at invisible headlines when going
  6852. back. This is important for non-interactive uses of the
  6853. command.
  6854. When optional argument TOP-LEVEL is non-nil, insert a level 1
  6855. heading, unconditionally."
  6856. (interactive "P")
  6857. (when (org-called-interactively-p 'any) (org-reveal))
  6858. (let ((itemp (and (not top-level) (org-in-item-p)))
  6859. (may-split (org-get-alist-option org-M-RET-may-split-line 'headline))
  6860. (respect-content (or org-insert-heading-respect-content
  6861. (equal arg '(4))))
  6862. (initial-content ""))
  6863. (cond
  6864. ((or (= (buffer-size) 0)
  6865. (and (not (save-excursion
  6866. (and (ignore-errors (org-back-to-heading invisible-ok))
  6867. (org-at-heading-p))))
  6868. (or arg (not itemp))))
  6869. ;; At beginning of buffer or so high up that only a heading
  6870. ;; makes sense.
  6871. (cond ((and (bolp) (not respect-content)) (insert "* "))
  6872. ((not respect-content)
  6873. (unless may-split (end-of-line))
  6874. (insert "\n* "))
  6875. ((re-search-forward org-outline-regexp-bol nil t)
  6876. (beginning-of-line)
  6877. (insert "* \n")
  6878. (backward-char))
  6879. (t (goto-char (point-max))
  6880. (insert "\n* ")))
  6881. (run-hooks 'org-insert-heading-hook))
  6882. ((and itemp (not (member arg '((4) (16)))) (org-insert-item)))
  6883. (t
  6884. ;; Maybe move at the end of the subtree
  6885. (when (equal arg '(16))
  6886. (org-up-heading-safe)
  6887. (org-end-of-subtree t))
  6888. ;; Insert a heading
  6889. (save-restriction
  6890. (widen)
  6891. (let* ((level nil)
  6892. (on-heading (org-at-heading-p))
  6893. (empty-line-p (if on-heading
  6894. (org-previous-line-empty-p)
  6895. ;; We will decide later
  6896. nil))
  6897. ;; Get a level string to fall back on.
  6898. (fix-level
  6899. (if (org-before-first-heading-p) "*"
  6900. (save-excursion
  6901. (org-back-to-heading t)
  6902. (when (org-previous-line-empty-p) (setq empty-line-p t))
  6903. (looking-at org-outline-regexp)
  6904. (make-string (1- (length (match-string 0))) ?*))))
  6905. (stars
  6906. (save-excursion
  6907. (condition-case nil
  6908. (if top-level "* "
  6909. (org-back-to-heading invisible-ok)
  6910. (when (and (not on-heading)
  6911. (featurep 'org-inlinetask)
  6912. (integerp org-inlinetask-min-level)
  6913. (>= (length (match-string 0))
  6914. org-inlinetask-min-level))
  6915. ;; Find a heading level before the inline
  6916. ;; task.
  6917. (while (and (setq level (org-up-heading-safe))
  6918. (>= level org-inlinetask-min-level)))
  6919. (if (org-at-heading-p)
  6920. (org-back-to-heading invisible-ok)
  6921. (error "This should not happen")))
  6922. (unless (and (save-excursion
  6923. (save-match-data
  6924. (org-backward-heading-same-level
  6925. 1 invisible-ok))
  6926. (= (point) (match-beginning 0)))
  6927. (not (org-next-line-empty-p)))
  6928. (setq empty-line-p (or empty-line-p
  6929. (org-previous-line-empty-p))))
  6930. (match-string 0))
  6931. (error (or fix-level "* ")))))
  6932. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  6933. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  6934. pos hide-previous previous-pos)
  6935. ;; If we insert after content, move there and clean up
  6936. ;; whitespace.
  6937. (when (and respect-content
  6938. (not (org-looking-at-p org-outline-regexp-bol)))
  6939. (if (not (org-before-first-heading-p))
  6940. (org-end-of-subtree nil t)
  6941. (re-search-forward org-outline-regexp-bol)
  6942. (beginning-of-line 0))
  6943. (skip-chars-backward " \r\t\n")
  6944. (and (not (org-looking-back "^\\*+" (line-beginning-position)))
  6945. (looking-at "[ \t]+") (replace-match ""))
  6946. (unless (eobp) (forward-char 1))
  6947. (when (looking-at "^\\*")
  6948. (unless (bobp) (backward-char 1))
  6949. (insert "\n")))
  6950. ;; If we are splitting, grab the text that should be moved
  6951. ;; to the new headline.
  6952. (when may-split
  6953. (if (org-at-heading-p)
  6954. ;; This is a heading: split intelligently (keeping
  6955. ;; tags).
  6956. (let ((pos (point)))
  6957. (beginning-of-line)
  6958. (unless (looking-at org-complex-heading-regexp)
  6959. (error "This should not happen"))
  6960. (when (and (match-beginning 4)
  6961. (> pos (match-beginning 4))
  6962. (< pos (match-end 4)))
  6963. (setq initial-content (buffer-substring pos (match-end 4)))
  6964. (goto-char pos)
  6965. (delete-region (point) (match-end 4))
  6966. (if (looking-at "[ \t]*$")
  6967. (replace-match "")
  6968. (insert (make-string (length initial-content) ?\s)))
  6969. (setq initial-content (org-trim initial-content)))
  6970. (goto-char pos))
  6971. ;; A normal line.
  6972. (setq initial-content
  6973. (org-trim
  6974. (delete-and-extract-region (point) (line-end-position))))))
  6975. ;; If we are at the beginning of the line, insert before it.
  6976. ;; Otherwise, after it.
  6977. (cond
  6978. ((and (bolp) (looking-at "[ \t]*$")))
  6979. ((bolp) (save-excursion (insert "\n")))
  6980. (t (end-of-line)
  6981. (insert "\n")))
  6982. ;; Insert the new heading
  6983. (insert stars)
  6984. (just-one-space)
  6985. (insert initial-content)
  6986. (unless (and blank (org-previous-line-empty-p))
  6987. (org-N-empty-lines-before-current (if blank 1 0)))
  6988. ;; Adjust visibility, which may be messed up if we removed
  6989. ;; blank lines while previous entry was hidden.
  6990. (let ((bol (line-beginning-position)))
  6991. (dolist (o (overlays-at (1- bol)))
  6992. (when (and (eq (overlay-get o 'invisible) 'outline)
  6993. (eq (overlay-end o) bol))
  6994. (move-overlay o (overlay-start o) (1- bol)))))
  6995. (run-hooks 'org-insert-heading-hook)))))))
  6996. (defun org-N-empty-lines-before-current (N)
  6997. "Make the number of empty lines before current exactly N.
  6998. So this will delete or add empty lines."
  6999. (save-excursion
  7000. (beginning-of-line)
  7001. (let ((p (point)))
  7002. (skip-chars-backward " \r\t\n")
  7003. (unless (bolp) (forward-line))
  7004. (delete-region (point) p))
  7005. (when (> N 0) (insert (make-string N ?\n)))))
  7006. (defun org-get-heading (&optional no-tags no-todo)
  7007. "Return the heading of the current entry, without the stars.
  7008. When NO-TAGS is non-nil, don't include tags.
  7009. When NO-TODO is non-nil, don't include TODO keywords."
  7010. (save-excursion
  7011. (org-back-to-heading t)
  7012. (cond
  7013. ((and no-tags no-todo)
  7014. (looking-at org-complex-heading-regexp)
  7015. (match-string 4))
  7016. (no-tags
  7017. (looking-at (concat org-outline-regexp
  7018. "\\(.*?\\)"
  7019. "\\(?:[ \t]+:[[:alnum:]:_@#%]+:\\)?[ \t]*$"))
  7020. (match-string 1))
  7021. (no-todo
  7022. (looking-at org-todo-line-regexp)
  7023. (match-string 3))
  7024. (t (looking-at org-heading-regexp)
  7025. (match-string 2)))))
  7026. (defvar orgstruct-mode) ; defined below
  7027. (defun org-heading-components ()
  7028. "Return the components of the current heading.
  7029. This is a list with the following elements:
  7030. - the level as an integer
  7031. - the reduced level, different if `org-odd-levels-only' is set.
  7032. - the TODO keyword, or nil
  7033. - the priority character, like ?A, or nil if no priority is given
  7034. - the headline text itself, or the tags string if no headline text
  7035. - the tags string, or nil."
  7036. (save-excursion
  7037. (org-back-to-heading t)
  7038. (when (let (case-fold-search)
  7039. (looking-at
  7040. (if orgstruct-mode
  7041. org-heading-regexp
  7042. org-complex-heading-regexp)))
  7043. (if orgstruct-mode
  7044. (list (length (match-string 1))
  7045. (org-reduced-level (length (match-string 1)))
  7046. nil
  7047. nil
  7048. (match-string 2)
  7049. nil)
  7050. (list (length (match-string 1))
  7051. (org-reduced-level (length (match-string 1)))
  7052. (org-match-string-no-properties 2)
  7053. (and (match-end 3) (aref (match-string 3) 2))
  7054. (org-match-string-no-properties 4)
  7055. (org-match-string-no-properties 5))))))
  7056. (defun org-get-entry ()
  7057. "Get the entry text, after heading, entire subtree."
  7058. (save-excursion
  7059. (org-back-to-heading t)
  7060. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  7061. (defun org-insert-heading-after-current ()
  7062. "Insert a new heading with same level as current, after current subtree."
  7063. (interactive)
  7064. (org-back-to-heading)
  7065. (org-insert-heading)
  7066. (org-move-subtree-down)
  7067. (end-of-line 1))
  7068. (defun org-insert-heading-respect-content (&optional invisible-ok)
  7069. "Insert heading with `org-insert-heading-respect-content' set to t."
  7070. (interactive)
  7071. (org-insert-heading '(4) invisible-ok))
  7072. (defun org-insert-todo-heading-respect-content (&optional force-state)
  7073. "Insert TODO heading with `org-insert-heading-respect-content' set to t."
  7074. (interactive)
  7075. (org-insert-todo-heading force-state '(4)))
  7076. (defun org-insert-todo-heading (arg &optional force-heading)
  7077. "Insert a new heading with the same level and TODO state as current heading.
  7078. If the heading has no TODO state, or if the state is DONE, use the first
  7079. state (TODO by default). Also with one prefix arg, force first state. With
  7080. two prefix args, force inserting at the end of the parent subtree."
  7081. (interactive "P")
  7082. (when (or force-heading (not (org-insert-item 'checkbox)))
  7083. (org-insert-heading (or (and (equal arg '(16)) '(16))
  7084. force-heading))
  7085. (save-excursion
  7086. (org-back-to-heading)
  7087. (outline-previous-heading)
  7088. (looking-at org-todo-line-regexp))
  7089. (let*
  7090. ((new-mark-x
  7091. (if (or (equal arg '(4))
  7092. (not (match-beginning 2))
  7093. (member (match-string 2) org-done-keywords))
  7094. (car org-todo-keywords-1)
  7095. (match-string 2)))
  7096. (new-mark
  7097. (or
  7098. (run-hook-with-args-until-success
  7099. 'org-todo-get-default-hook new-mark-x nil)
  7100. new-mark-x)))
  7101. (beginning-of-line 1)
  7102. (and (looking-at org-outline-regexp) (goto-char (match-end 0))
  7103. (if org-treat-insert-todo-heading-as-state-change
  7104. (org-todo new-mark)
  7105. (insert new-mark " "))))
  7106. (when org-provide-todo-statistics
  7107. (org-update-parent-todo-statistics))))
  7108. (defun org-insert-subheading (arg)
  7109. "Insert a new subheading and demote it.
  7110. Works for outline headings and for plain lists alike."
  7111. (interactive "P")
  7112. (org-insert-heading arg)
  7113. (cond
  7114. ((org-at-heading-p) (org-do-demote))
  7115. ((org-at-item-p) (org-indent-item))))
  7116. (defun org-insert-todo-subheading (arg)
  7117. "Insert a new subheading with TODO keyword or checkbox and demote it.
  7118. Works for outline headings and for plain lists alike."
  7119. (interactive "P")
  7120. (org-insert-todo-heading arg)
  7121. (cond
  7122. ((org-at-heading-p) (org-do-demote))
  7123. ((org-at-item-p) (org-indent-item))))
  7124. ;;; Promotion and Demotion
  7125. (defvar org-after-demote-entry-hook nil
  7126. "Hook run after an entry has been demoted.
  7127. The cursor will be at the beginning of the entry.
  7128. When a subtree is being demoted, the hook will be called for each node.")
  7129. (defvar org-after-promote-entry-hook nil
  7130. "Hook run after an entry has been promoted.
  7131. The cursor will be at the beginning of the entry.
  7132. When a subtree is being promoted, the hook will be called for each node.")
  7133. (defun org-promote-subtree ()
  7134. "Promote the entire subtree.
  7135. See also `org-promote'."
  7136. (interactive)
  7137. (save-excursion
  7138. (org-with-limited-levels (org-map-tree 'org-promote)))
  7139. (org-fix-position-after-promote))
  7140. (defun org-demote-subtree ()
  7141. "Demote the entire subtree.
  7142. See `org-demote' and `org-promote'."
  7143. (interactive)
  7144. (save-excursion
  7145. (org-with-limited-levels (org-map-tree 'org-demote)))
  7146. (org-fix-position-after-promote))
  7147. (defun org-do-promote ()
  7148. "Promote the current heading higher up the tree.
  7149. If the region is active in `transient-mark-mode', promote all
  7150. headings in the region."
  7151. (interactive)
  7152. (save-excursion
  7153. (if (org-region-active-p)
  7154. (org-map-region 'org-promote (region-beginning) (region-end))
  7155. (org-promote)))
  7156. (org-fix-position-after-promote))
  7157. (defun org-do-demote ()
  7158. "Demote the current heading lower down the tree.
  7159. If the region is active in `transient-mark-mode', demote all
  7160. headings in the region."
  7161. (interactive)
  7162. (save-excursion
  7163. (if (org-region-active-p)
  7164. (org-map-region 'org-demote (region-beginning) (region-end))
  7165. (org-demote)))
  7166. (org-fix-position-after-promote))
  7167. (defun org-fix-position-after-promote ()
  7168. "Fix cursor position and indentation after demoting/promoting."
  7169. (let ((pos (point)))
  7170. (when (save-excursion
  7171. (beginning-of-line 1)
  7172. (looking-at org-todo-line-regexp)
  7173. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  7174. (cond ((eobp) (insert " "))
  7175. ((eolp) (insert " "))
  7176. ((equal (char-after) ?\ ) (forward-char 1))))))
  7177. (defun org-current-level ()
  7178. "Return the level of the current entry, or nil if before the first headline.
  7179. The level is the number of stars at the beginning of the
  7180. headline. Use `org-reduced-level' to remove the effect of
  7181. `org-odd-levels'. Unlike to `org-outline-level', this function
  7182. ignores inlinetasks."
  7183. (let ((level (org-with-limited-levels (org-outline-level))))
  7184. (and (> level 0) level)))
  7185. (defun org-get-previous-line-level ()
  7186. "Return the outline depth of the last headline before the current line.
  7187. Returns 0 for the first headline in the buffer, and nil if before the
  7188. first headline."
  7189. (and (org-current-level)
  7190. (or (and (/= (line-beginning-position) (point-min))
  7191. (save-excursion (beginning-of-line 0) (org-current-level)))
  7192. 0)))
  7193. (defun org-reduced-level (l)
  7194. "Compute the effective level of a heading.
  7195. This takes into account the setting of `org-odd-levels-only'."
  7196. (cond
  7197. ((zerop l) 0)
  7198. (org-odd-levels-only (1+ (floor (/ l 2))))
  7199. (t l)))
  7200. (defun org-level-increment ()
  7201. "Return the number of stars that will be added or removed at a
  7202. time to headlines when structure editing, based on the value of
  7203. `org-odd-levels-only'."
  7204. (if org-odd-levels-only 2 1))
  7205. (defun org-get-valid-level (level &optional change)
  7206. "Rectify a level change under the influence of `org-odd-levels-only'
  7207. LEVEL is a current level, CHANGE is by how much the level should be
  7208. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  7209. even level numbers will become the next higher odd number."
  7210. (if org-odd-levels-only
  7211. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  7212. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  7213. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  7214. (max 1 (+ level (or change 0)))))
  7215. (define-obsolete-function-alias 'org-get-legal-level 'org-get-valid-level "23.1")
  7216. (defun org-promote ()
  7217. "Promote the current heading higher up the tree."
  7218. (org-with-wide-buffer
  7219. (org-back-to-heading t)
  7220. (let* ((after-change-functions (remq 'flyspell-after-change-function
  7221. after-change-functions))
  7222. (level (save-match-data (funcall outline-level)))
  7223. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  7224. (diff (abs (- level (length up-head) -1))))
  7225. (cond
  7226. ((and (= level 1) org-allow-promoting-top-level-subtree)
  7227. (replace-match "# " nil t))
  7228. ((= level 1)
  7229. (user-error "Cannot promote to level 0. UNDO to recover if necessary"))
  7230. (t (replace-match up-head nil t)))
  7231. (unless (= level 1)
  7232. (when org-auto-align-tags (org-set-tags nil 'ignore-column))
  7233. (when org-adapt-indentation (org-fixup-indentation (- diff))))
  7234. (run-hooks 'org-after-promote-entry-hook))))
  7235. (defun org-demote ()
  7236. "Demote the current heading lower down the tree."
  7237. (org-with-wide-buffer
  7238. (org-back-to-heading t)
  7239. (let* ((after-change-functions (remq 'flyspell-after-change-function
  7240. after-change-functions))
  7241. (level (save-match-data (funcall outline-level)))
  7242. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  7243. (diff (abs (- level (length down-head) -1))))
  7244. (replace-match down-head nil t)
  7245. (when org-auto-align-tags (org-set-tags nil 'ignore-column))
  7246. (when org-adapt-indentation (org-fixup-indentation diff))
  7247. (run-hooks 'org-after-demote-entry-hook))))
  7248. (defun org-cycle-level ()
  7249. "Cycle the level of an empty headline through possible states.
  7250. This goes first to child, then to parent, level, then up the hierarchy.
  7251. After top level, it switches back to sibling level."
  7252. (interactive)
  7253. (let ((org-adapt-indentation nil))
  7254. (when (org-point-at-end-of-empty-headline)
  7255. (setq this-command 'org-cycle-level) ; Only needed for caching
  7256. (let ((cur-level (org-current-level))
  7257. (prev-level (org-get-previous-line-level)))
  7258. (cond
  7259. ;; If first headline in file, promote to top-level.
  7260. ((= prev-level 0)
  7261. (loop repeat (/ (- cur-level 1) (org-level-increment))
  7262. do (org-do-promote)))
  7263. ;; If same level as prev, demote one.
  7264. ((= prev-level cur-level)
  7265. (org-do-demote))
  7266. ;; If parent is top-level, promote to top level if not already.
  7267. ((= prev-level 1)
  7268. (loop repeat (/ (- cur-level 1) (org-level-increment))
  7269. do (org-do-promote)))
  7270. ;; If top-level, return to prev-level.
  7271. ((= cur-level 1)
  7272. (loop repeat (/ (- prev-level 1) (org-level-increment))
  7273. do (org-do-demote)))
  7274. ;; If less than prev-level, promote one.
  7275. ((< cur-level prev-level)
  7276. (org-do-promote))
  7277. ;; If deeper than prev-level, promote until higher than
  7278. ;; prev-level.
  7279. ((> cur-level prev-level)
  7280. (loop repeat (+ 1 (/ (- cur-level prev-level) (org-level-increment)))
  7281. do (org-do-promote))))
  7282. t))))
  7283. (defun org-map-tree (fun)
  7284. "Call FUN for every heading underneath the current one."
  7285. (org-back-to-heading)
  7286. (let ((level (funcall outline-level)))
  7287. (save-excursion
  7288. (funcall fun)
  7289. (while (and (progn
  7290. (outline-next-heading)
  7291. (> (funcall outline-level) level))
  7292. (not (eobp)))
  7293. (funcall fun)))))
  7294. (defun org-map-region (fun beg end)
  7295. "Call FUN for every heading between BEG and END."
  7296. (let ((org-ignore-region t))
  7297. (save-excursion
  7298. (setq end (copy-marker end))
  7299. (goto-char beg)
  7300. (when (and (re-search-forward org-outline-regexp-bol nil t)
  7301. (< (point) end))
  7302. (funcall fun))
  7303. (while (and (progn
  7304. (outline-next-heading)
  7305. (< (point) end))
  7306. (not (eobp)))
  7307. (funcall fun)))))
  7308. (defun org-fixup-indentation (diff)
  7309. "Change the indentation in the current entry by DIFF.
  7310. DIFF is an integer. Indentation is done according to the
  7311. following rules:
  7312. - Planning information and property drawers are always indented
  7313. according to the new level of the headline;
  7314. - Footnote definitions and their contents are ignored;
  7315. - Inlinetasks' boundaries are not shifted;
  7316. - Empty lines are ignored;
  7317. - Other lines' indentation are shifted by DIFF columns, unless
  7318. it would introduce a structural change in the document, in
  7319. which case no shifting is done at all.
  7320. Assume point is at a heading or an inlinetask beginning."
  7321. (org-with-wide-buffer
  7322. (narrow-to-region (line-beginning-position)
  7323. (save-excursion
  7324. (if (org-with-limited-levels (org-at-heading-p))
  7325. (org-with-limited-levels (outline-next-heading))
  7326. (org-inlinetask-goto-end))
  7327. (point)))
  7328. (forward-line)
  7329. ;; Indent properly planning info and property drawer.
  7330. (when (org-looking-at-p org-planning-line-re)
  7331. (org-indent-line)
  7332. (forward-line))
  7333. (when (looking-at org-property-drawer-re)
  7334. (goto-char (match-end 0))
  7335. (forward-line)
  7336. (save-excursion (org-indent-region (match-beginning 0) (match-end 0))))
  7337. (catch 'no-shift
  7338. (when (zerop diff) (throw 'no-shift nil))
  7339. ;; If DIFF is negative, first check if a shift is possible at all
  7340. ;; (e.g., it doesn't break structure). This can only happen if
  7341. ;; some contents are not properly indented.
  7342. (let ((case-fold-search t))
  7343. (when (< diff 0)
  7344. (let ((diff (- diff))
  7345. (forbidden-re (concat org-outline-regexp
  7346. "\\|"
  7347. (substring org-footnote-definition-re 1))))
  7348. (save-excursion
  7349. (while (not (eobp))
  7350. (cond
  7351. ((org-looking-at-p "[ \t]*$") (forward-line))
  7352. ((and (org-looking-at-p org-footnote-definition-re)
  7353. (let ((e (org-element-at-point)))
  7354. (and (eq (org-element-type e) 'footnote-definition)
  7355. (goto-char (org-element-property :end e))))))
  7356. ((org-looking-at-p org-outline-regexp) (forward-line))
  7357. ;; Give up if shifting would move before column 0 or
  7358. ;; if it would introduce a headline or a footnote
  7359. ;; definition.
  7360. (t
  7361. (skip-chars-forward " \t")
  7362. (let ((ind (current-column)))
  7363. (when (or (< ind diff)
  7364. (and (= ind diff) (org-looking-at-p forbidden-re)))
  7365. (throw 'no-shift nil)))
  7366. ;; Ignore contents of example blocks and source
  7367. ;; blocks if their indentation is meant to be
  7368. ;; preserved. Jump to block's closing line.
  7369. (beginning-of-line)
  7370. (or (and (org-looking-at-p "[ \t]*#\\+BEGIN_\\(EXAMPLE\\|SRC\\)")
  7371. (let ((e (org-element-at-point)))
  7372. (and (memq (org-element-type e)
  7373. '(example-block src-block))
  7374. (or org-src-preserve-indentation
  7375. (org-element-property :preserve-indent e))
  7376. (goto-char (org-element-property :end e))
  7377. (progn (skip-chars-backward " \r\t\n")
  7378. (beginning-of-line)
  7379. t))))
  7380. (forward-line))))))))
  7381. ;; Shift lines but footnote definitions, inlinetasks boundaries
  7382. ;; by DIFF. Also skip contents of source or example blocks
  7383. ;; when indentation is meant to be preserved.
  7384. (while (not (eobp))
  7385. (cond
  7386. ((and (org-looking-at-p org-footnote-definition-re)
  7387. (let ((e (org-element-at-point)))
  7388. (and (eq (org-element-type e) 'footnote-definition)
  7389. (goto-char (org-element-property :end e))))))
  7390. ((org-looking-at-p org-outline-regexp) (forward-line))
  7391. ((org-looking-at-p "[ \t]*$") (forward-line))
  7392. (t
  7393. (org-indent-line-to (+ (org-get-indentation) diff))
  7394. (beginning-of-line)
  7395. (or (and (org-looking-at-p "[ \t]*#\\+BEGIN_\\(EXAMPLE\\|SRC\\)")
  7396. (let ((e (org-element-at-point)))
  7397. (and (memq (org-element-type e)
  7398. '(example-block src-block))
  7399. (or org-src-preserve-indentation
  7400. (org-element-property :preserve-indent e))
  7401. (goto-char (org-element-property :end e))
  7402. (progn (skip-chars-backward " \r\t\n")
  7403. (beginning-of-line)
  7404. t))))
  7405. (forward-line)))))))))
  7406. (defun org-convert-to-odd-levels ()
  7407. "Convert an org-mode file with all levels allowed to one with odd levels.
  7408. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  7409. level 5 etc."
  7410. (interactive)
  7411. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  7412. (let ((outline-level 'org-outline-level)
  7413. (org-odd-levels-only nil) n)
  7414. (save-excursion
  7415. (goto-char (point-min))
  7416. (while (re-search-forward "^\\*\\*+ " nil t)
  7417. (setq n (- (length (match-string 0)) 2))
  7418. (while (>= (setq n (1- n)) 0)
  7419. (org-demote))
  7420. (end-of-line 1))))))
  7421. (defun org-convert-to-oddeven-levels ()
  7422. "Convert an org-mode file with only odd levels to one with odd/even levels.
  7423. This promotes level 3 to level 2, level 5 to level 3 etc. If the
  7424. file contains a section with an even level, conversion would
  7425. destroy the structure of the file. An error is signaled in this
  7426. case."
  7427. (interactive)
  7428. (goto-char (point-min))
  7429. ;; First check if there are no even levels
  7430. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  7431. (org-show-set-visibility 'canonical)
  7432. (error "Not all levels are odd in this file. Conversion not possible"))
  7433. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  7434. (let ((outline-regexp org-outline-regexp)
  7435. (outline-level 'org-outline-level)
  7436. (org-odd-levels-only nil) n)
  7437. (save-excursion
  7438. (goto-char (point-min))
  7439. (while (re-search-forward "^\\*\\*+ " nil t)
  7440. (setq n (/ (1- (length (match-string 0))) 2))
  7441. (while (>= (setq n (1- n)) 0)
  7442. (org-promote))
  7443. (end-of-line 1))))))
  7444. (defun org-tr-level (n)
  7445. "Make N odd if required."
  7446. (if org-odd-levels-only (1+ (/ n 2)) n))
  7447. ;;; Vertical tree motion, cutting and pasting of subtrees
  7448. (defun org-move-subtree-up (&optional arg)
  7449. "Move the current subtree up past ARG headlines of the same level."
  7450. (interactive "p")
  7451. (org-move-subtree-down (- (prefix-numeric-value arg))))
  7452. (defun org-move-subtree-down (&optional arg)
  7453. "Move the current subtree down past ARG headlines of the same level."
  7454. (interactive "p")
  7455. (setq arg (prefix-numeric-value arg))
  7456. (let ((movfunc (if (> arg 0) 'org-get-next-sibling
  7457. 'org-get-last-sibling))
  7458. (ins-point (make-marker))
  7459. (cnt (abs arg))
  7460. (col (current-column))
  7461. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  7462. ;; Select the tree
  7463. (org-back-to-heading)
  7464. (setq beg0 (point))
  7465. (save-excursion
  7466. (setq ne-beg (org-back-over-empty-lines))
  7467. (setq beg (point)))
  7468. (save-match-data
  7469. (save-excursion (outline-end-of-heading)
  7470. (setq folded (outline-invisible-p)))
  7471. (progn (org-end-of-subtree nil t)
  7472. (unless (eobp) (backward-char))))
  7473. (outline-next-heading)
  7474. (setq ne-end (org-back-over-empty-lines))
  7475. (setq end (point))
  7476. (goto-char beg0)
  7477. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  7478. ;; include less whitespace
  7479. (save-excursion
  7480. (goto-char beg)
  7481. (forward-line (- ne-beg ne-end))
  7482. (setq beg (point))))
  7483. ;; Find insertion point, with error handling
  7484. (while (> cnt 0)
  7485. (or (and (funcall movfunc) (looking-at org-outline-regexp))
  7486. (progn (goto-char beg0)
  7487. (user-error "Cannot move past superior level or buffer limit")))
  7488. (setq cnt (1- cnt)))
  7489. (when (> arg 0)
  7490. ;; Moving forward - still need to move over subtree
  7491. (org-end-of-subtree t t)
  7492. (save-excursion
  7493. (org-back-over-empty-lines)
  7494. (or (bolp) (newline))))
  7495. (setq ne-ins (org-back-over-empty-lines))
  7496. (move-marker ins-point (point))
  7497. (setq txt (buffer-substring beg end))
  7498. (org-save-markers-in-region beg end)
  7499. (delete-region beg end)
  7500. (org-remove-empty-overlays-at beg)
  7501. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  7502. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  7503. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  7504. (let ((bbb (point)))
  7505. (insert-before-markers txt)
  7506. (org-reinstall-markers-in-region bbb)
  7507. (move-marker ins-point bbb))
  7508. (or (bolp) (insert "\n"))
  7509. (setq ins-end (point))
  7510. (goto-char ins-point)
  7511. (org-skip-whitespace)
  7512. (when (and (< arg 0)
  7513. (org-first-sibling-p)
  7514. (> ne-ins ne-beg))
  7515. ;; Move whitespace back to beginning
  7516. (save-excursion
  7517. (goto-char ins-end)
  7518. (let ((kill-whole-line t))
  7519. (kill-line (- ne-ins ne-beg)) (point)))
  7520. (insert (make-string (- ne-ins ne-beg) ?\n)))
  7521. (move-marker ins-point nil)
  7522. (if folded
  7523. (outline-hide-subtree)
  7524. (org-show-entry)
  7525. (org-show-children)
  7526. (org-cycle-hide-drawers 'children))
  7527. (org-clean-visibility-after-subtree-move)
  7528. ;; move back to the initial column we were at
  7529. (move-to-column col)))
  7530. (defvar org-subtree-clip ""
  7531. "Clipboard for cut and paste of subtrees.
  7532. This is actually only a copy of the kill, because we use the normal kill
  7533. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  7534. (defvar org-subtree-clip-folded nil
  7535. "Was the last copied subtree folded?
  7536. This is used to fold the tree back after pasting.")
  7537. (defun org-cut-subtree (&optional n)
  7538. "Cut the current subtree into the clipboard.
  7539. With prefix arg N, cut this many sequential subtrees.
  7540. This is a short-hand for marking the subtree and then cutting it."
  7541. (interactive "p")
  7542. (org-copy-subtree n 'cut))
  7543. (defun org-copy-subtree (&optional n cut force-store-markers nosubtrees)
  7544. "Copy the current subtree it in the clipboard.
  7545. With prefix arg N, copy this many sequential subtrees.
  7546. This is a short-hand for marking the subtree and then copying it.
  7547. If CUT is non-nil, actually cut the subtree.
  7548. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  7549. of some markers in the region, even if CUT is non-nil. This is
  7550. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  7551. (interactive "p")
  7552. (let (beg end folded (beg0 (point)))
  7553. (if (org-called-interactively-p 'any)
  7554. (org-back-to-heading nil) ; take what looks like a subtree
  7555. (org-back-to-heading t)) ; take what is really there
  7556. (setq beg (point))
  7557. (skip-chars-forward " \t\r\n")
  7558. (save-match-data
  7559. (if nosubtrees
  7560. (outline-next-heading)
  7561. (save-excursion (outline-end-of-heading)
  7562. (setq folded (outline-invisible-p)))
  7563. (ignore-errors (org-forward-heading-same-level (1- n) t))
  7564. (org-end-of-subtree t t)))
  7565. ;; Include the end of an inlinetask
  7566. (when (and (featurep 'org-inlinetask)
  7567. (looking-at-p (concat (org-inlinetask-outline-regexp)
  7568. "END[ \t]*$")))
  7569. (end-of-line))
  7570. (setq end (point))
  7571. (goto-char beg0)
  7572. (when (> end beg)
  7573. (setq org-subtree-clip-folded folded)
  7574. (when (or cut force-store-markers)
  7575. (org-save-markers-in-region beg end))
  7576. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  7577. (setq org-subtree-clip (current-kill 0))
  7578. (message "%s: Subtree(s) with %d characters"
  7579. (if cut "Cut" "Copied")
  7580. (length org-subtree-clip)))))
  7581. (defun org-paste-subtree (&optional level tree for-yank remove)
  7582. "Paste the clipboard as a subtree, with modification of headline level.
  7583. The entire subtree is promoted or demoted in order to match a new headline
  7584. level.
  7585. If the cursor is at the beginning of a headline, the same level as
  7586. that headline is used to paste the tree.
  7587. If not, the new level is derived from the *visible* headings
  7588. before and after the insertion point, and taken to be the inferior headline
  7589. level of the two. So if the previous visible heading is level 3 and the
  7590. next is level 4 (or vice versa), level 4 will be used for insertion.
  7591. This makes sure that the subtree remains an independent subtree and does
  7592. not swallow low level entries.
  7593. You can also force a different level, either by using a numeric prefix
  7594. argument, or by inserting the heading marker by hand. For example, if the
  7595. cursor is after \"*****\", then the tree will be shifted to level 5.
  7596. If optional TREE is given, use this text instead of the kill ring.
  7597. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  7598. move back over whitespace before inserting, and move point to the end of
  7599. the inserted text when done.
  7600. When REMOVE is non-nil, remove the subtree from the clipboard."
  7601. (interactive "P")
  7602. (setq tree (or tree (and kill-ring (current-kill 0))))
  7603. (unless (org-kill-is-subtree-p tree)
  7604. (user-error "%s"
  7605. (substitute-command-keys
  7606. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  7607. (org-with-limited-levels
  7608. (let* ((visp (not (outline-invisible-p)))
  7609. (txt tree)
  7610. (^re_ "\\(\\*+\\)[ \t]*")
  7611. (old-level (if (string-match org-outline-regexp-bol txt)
  7612. (- (match-end 0) (match-beginning 0) 1)
  7613. -1))
  7614. (force-level (cond (level (prefix-numeric-value level))
  7615. ((and (looking-at "[ \t]*$")
  7616. (string-match
  7617. "^\\*+$" (buffer-substring
  7618. (point-at-bol) (point))))
  7619. (- (match-end 0) (match-beginning 0)))
  7620. ((and (bolp)
  7621. (looking-at org-outline-regexp))
  7622. (- (match-end 0) (point) 1))))
  7623. (previous-level (save-excursion
  7624. (condition-case nil
  7625. (progn
  7626. (outline-previous-visible-heading 1)
  7627. (if (looking-at ^re_)
  7628. (- (match-end 0) (match-beginning 0) 1)
  7629. 1))
  7630. (error 1))))
  7631. (next-level (save-excursion
  7632. (condition-case nil
  7633. (progn
  7634. (or (looking-at org-outline-regexp)
  7635. (outline-next-visible-heading 1))
  7636. (if (looking-at ^re_)
  7637. (- (match-end 0) (match-beginning 0) 1)
  7638. 1))
  7639. (error 1))))
  7640. (new-level (or force-level (max previous-level next-level)))
  7641. (shift (if (or (= old-level -1)
  7642. (= new-level -1)
  7643. (= old-level new-level))
  7644. 0
  7645. (- new-level old-level)))
  7646. (delta (if (> shift 0) -1 1))
  7647. (func (if (> shift 0) 'org-demote 'org-promote))
  7648. (org-odd-levels-only nil)
  7649. beg end newend)
  7650. ;; Remove the forced level indicator
  7651. (when force-level
  7652. (delete-region (point-at-bol) (point)))
  7653. ;; Paste
  7654. (beginning-of-line (if (bolp) 1 2))
  7655. (setq beg (point))
  7656. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  7657. (insert-before-markers txt)
  7658. (unless (string-match "\n\\'" txt) (insert "\n"))
  7659. (setq newend (point))
  7660. (org-reinstall-markers-in-region beg)
  7661. (setq end (point))
  7662. (goto-char beg)
  7663. (skip-chars-forward " \t\n\r")
  7664. (setq beg (point))
  7665. (when (and (outline-invisible-p) visp)
  7666. (save-excursion (outline-show-heading)))
  7667. ;; Shift if necessary
  7668. (unless (= shift 0)
  7669. (save-restriction
  7670. (narrow-to-region beg end)
  7671. (while (not (= shift 0))
  7672. (org-map-region func (point-min) (point-max))
  7673. (setq shift (+ delta shift)))
  7674. (goto-char (point-min))
  7675. (setq newend (point-max))))
  7676. (when (or (org-called-interactively-p 'interactive) for-yank)
  7677. (message "Clipboard pasted as level %d subtree" new-level))
  7678. (when (and (not for-yank) ; in this case, org-yank will decide about folding
  7679. kill-ring
  7680. (eq org-subtree-clip (current-kill 0))
  7681. org-subtree-clip-folded)
  7682. ;; The tree was folded before it was killed/copied
  7683. (outline-hide-subtree))
  7684. (and for-yank (goto-char newend))
  7685. (and remove (setq kill-ring (cdr kill-ring))))))
  7686. (defun org-kill-is-subtree-p (&optional txt)
  7687. "Check if the current kill is an outline subtree, or a set of trees.
  7688. Returns nil if kill does not start with a headline, or if the first
  7689. headline level is not the largest headline level in the tree.
  7690. So this will actually accept several entries of equal levels as well,
  7691. which is OK for `org-paste-subtree'.
  7692. If optional TXT is given, check this string instead of the current kill."
  7693. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  7694. (re (org-get-limited-outline-regexp))
  7695. (^re (concat "^" re))
  7696. (start-level (and kill
  7697. (string-match
  7698. (concat "\\`\\([ \t\n\r]*?\n\\)?\\(" re "\\)")
  7699. kill)
  7700. (- (match-end 2) (match-beginning 2) 1)))
  7701. (start (1+ (or (match-beginning 2) -1))))
  7702. (if (not start-level)
  7703. (progn
  7704. nil) ;; does not even start with a heading
  7705. (catch 'exit
  7706. (while (setq start (string-match ^re kill (1+ start)))
  7707. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  7708. (throw 'exit nil)))
  7709. t))))
  7710. (defvar org-markers-to-move nil
  7711. "Markers that should be moved with a cut-and-paste operation.
  7712. Those markers are stored together with their positions relative to
  7713. the start of the region.")
  7714. (defun org-save-markers-in-region (beg end)
  7715. "Check markers in region.
  7716. If these markers are between BEG and END, record their position relative
  7717. to BEG, so that after moving the block of text, we can put the markers back
  7718. into place.
  7719. This function gets called just before an entry or tree gets cut from the
  7720. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  7721. called immediately, to move the markers with the entries."
  7722. (setq org-markers-to-move nil)
  7723. (when (featurep 'org-clock)
  7724. (org-clock-save-markers-for-cut-and-paste beg end))
  7725. (when (featurep 'org-agenda)
  7726. (org-agenda-save-markers-for-cut-and-paste beg end)))
  7727. (defun org-check-and-save-marker (marker beg end)
  7728. "Check if MARKER is between BEG and END.
  7729. If yes, remember the marker and the distance to BEG."
  7730. (when (and (marker-buffer marker)
  7731. (equal (marker-buffer marker) (current-buffer))
  7732. (>= marker beg) (< marker end))
  7733. (push (cons marker (- marker beg)) org-markers-to-move)))
  7734. (defun org-reinstall-markers-in-region (beg)
  7735. "Move all remembered markers to their position relative to BEG."
  7736. (mapc (lambda (x)
  7737. (move-marker (car x) (+ beg (cdr x))))
  7738. org-markers-to-move)
  7739. (setq org-markers-to-move nil))
  7740. (defun org-narrow-to-subtree ()
  7741. "Narrow buffer to the current subtree."
  7742. (interactive)
  7743. (save-excursion
  7744. (save-match-data
  7745. (org-with-limited-levels
  7746. (narrow-to-region
  7747. (progn (org-back-to-heading t) (point))
  7748. (progn (org-end-of-subtree t t)
  7749. (when (and (org-at-heading-p) (not (eobp))) (backward-char 1))
  7750. (point)))))))
  7751. (defun org-narrow-to-block ()
  7752. "Narrow buffer to the current block."
  7753. (interactive)
  7754. (let* ((case-fold-search t)
  7755. (blockp (org-between-regexps-p "^[ \t]*#\\+begin_.*"
  7756. "^[ \t]*#\\+end_.*")))
  7757. (if blockp
  7758. (narrow-to-region (car blockp) (cdr blockp))
  7759. (user-error "Not in a block"))))
  7760. (defun org-clone-subtree-with-time-shift (n &optional shift)
  7761. "Clone the task (subtree) at point N times.
  7762. The clones will be inserted as siblings.
  7763. In interactive use, the user will be prompted for the number of
  7764. clones to be produced. If the entry has a timestamp, the user
  7765. will also be prompted for a time shift, which may be a repeater
  7766. as used in time stamps, for example `+3d'. To disable this,
  7767. you can call the function with a universal prefix argument.
  7768. When a valid repeater is given and the entry contains any time
  7769. stamps, the clones will become a sequence in time, with time
  7770. stamps in the subtree shifted for each clone produced. If SHIFT
  7771. is nil or the empty string, time stamps will be left alone. The
  7772. ID property of the original subtree is removed.
  7773. If the original subtree did contain time stamps with a repeater,
  7774. the following will happen:
  7775. - the repeater will be removed in each clone
  7776. - an additional clone will be produced, with the current, unshifted
  7777. date(s) in the entry.
  7778. - the original entry will be placed *after* all the clones, with
  7779. repeater intact.
  7780. - the start days in the repeater in the original entry will be shifted
  7781. to past the last clone.
  7782. In this way you can spell out a number of instances of a repeating task,
  7783. and still retain the repeater to cover future instances of the task.
  7784. As described above, N+1 clones are produced when the original
  7785. subtree has a repeater. Setting N to 0, then, can be used to
  7786. remove the repeater from a subtree and create a shifted clone
  7787. with the original repeater."
  7788. (interactive "nNumber of clones to produce: ")
  7789. (let ((shift
  7790. (or shift
  7791. (if (and (not (equal current-prefix-arg '(4)))
  7792. (save-excursion
  7793. (re-search-forward org-ts-regexp-both
  7794. (save-excursion
  7795. (org-end-of-subtree t)
  7796. (point)) t)))
  7797. (read-from-minibuffer
  7798. "Date shift per clone (e.g. +1w, empty to copy unchanged): ")
  7799. ""))) ;; No time shift
  7800. (n-no-remove -1)
  7801. (drawer-re org-drawer-regexp)
  7802. (org-clock-re (format "^[ \t]*%s.*$" org-clock-string))
  7803. beg end template task idprop
  7804. shift-n shift-what doshift nmin nmax)
  7805. (unless (wholenump n)
  7806. (user-error "Invalid number of replications %s" n))
  7807. (when (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  7808. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([hdwmy]\\)[ \t]*\\'"
  7809. shift)))
  7810. (user-error "Invalid shift specification %s" shift))
  7811. (when doshift
  7812. (setq shift-n (string-to-number (match-string 1 shift))
  7813. shift-what (cdr (assoc (match-string 2 shift)
  7814. '(("d" . day) ("w" . week)
  7815. ("m" . month) ("y" . year))))))
  7816. (when (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  7817. (setq nmin 1 nmax n)
  7818. (org-back-to-heading t)
  7819. (setq beg (point))
  7820. (setq idprop (org-entry-get nil "ID"))
  7821. (org-end-of-subtree t t)
  7822. (or (bolp) (insert "\n"))
  7823. (setq end (point))
  7824. (setq template (buffer-substring beg end))
  7825. (when (and doshift
  7826. (string-match "<[^<>\n]+ [.+]?\\+[0-9]+[hdwmy][^<>\n]*>" template))
  7827. (delete-region beg end)
  7828. (setq end beg)
  7829. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  7830. (goto-char end)
  7831. (loop for n from nmin to nmax do
  7832. ;; prepare clone
  7833. (with-temp-buffer
  7834. (insert template)
  7835. (org-mode)
  7836. (goto-char (point-min))
  7837. (org-show-subtree)
  7838. (and idprop (if org-clone-delete-id
  7839. (org-entry-delete nil "ID")
  7840. (org-id-get-create t)))
  7841. (unless (= n 0)
  7842. (while (re-search-forward org-clock-re nil t)
  7843. (kill-whole-line))
  7844. (goto-char (point-min))
  7845. (while (re-search-forward drawer-re nil t)
  7846. (org-remove-empty-drawer-at (point))))
  7847. (goto-char (point-min))
  7848. (when doshift
  7849. (while (re-search-forward org-ts-regexp-both nil t)
  7850. (org-timestamp-change (* n shift-n) shift-what))
  7851. (unless (= n n-no-remove)
  7852. (goto-char (point-min))
  7853. (while (re-search-forward org-ts-regexp nil t)
  7854. (save-excursion
  7855. (goto-char (match-beginning 0))
  7856. (when (looking-at "<[^<>\n]+\\( +[.+]?\\+[0-9]+[hdwmy]\\)")
  7857. (delete-region (match-beginning 1) (match-end 1)))))))
  7858. (setq task (buffer-string)))
  7859. (insert task))
  7860. (goto-char beg)))
  7861. ;;; Outline Sorting
  7862. (defun org-sort (with-case)
  7863. "Call `org-sort-entries', `org-table-sort-lines' or `org-sort-list'.
  7864. Optional argument WITH-CASE means sort case-sensitively."
  7865. (interactive "P")
  7866. (cond
  7867. ((org-at-table-p) (org-call-with-arg 'org-table-sort-lines with-case))
  7868. ((org-at-item-p) (org-call-with-arg 'org-sort-list with-case))
  7869. (t
  7870. (org-call-with-arg 'org-sort-entries with-case))))
  7871. (defun org-sort-remove-invisible (s)
  7872. "Remove invisible links from string S."
  7873. (remove-text-properties 0 (length s) org-rm-props s)
  7874. (while (string-match org-bracket-link-regexp s)
  7875. (setq s (replace-match (if (match-end 2)
  7876. (match-string 3 s)
  7877. (match-string 1 s))
  7878. t t s)))
  7879. (let ((st (format " %s " s)))
  7880. (while (string-match org-emph-re st)
  7881. (setq st (replace-match (format " %s " (match-string 4 st)) t t st)))
  7882. (setq s (substring st 1 -1)))
  7883. s)
  7884. (defvar org-priority-regexp) ; defined later in the file
  7885. (defvar org-after-sorting-entries-or-items-hook nil
  7886. "Hook that is run after a bunch of entries or items have been sorted.
  7887. When children are sorted, the cursor is in the parent line when this
  7888. hook gets called. When a region or a plain list is sorted, the cursor
  7889. will be in the first entry of the sorted region/list.")
  7890. (defun org-sort-entries
  7891. (&optional with-case sorting-type getkey-func compare-func property)
  7892. "Sort entries on a certain level of an outline tree.
  7893. If there is an active region, the entries in the region are sorted.
  7894. Else, if the cursor is before the first entry, sort the top-level items.
  7895. Else, the children of the entry at point are sorted.
  7896. Sorting can be alphabetically, numerically, by date/time as given by
  7897. a time stamp, by a property, by priority order, or by a custom function.
  7898. The command prompts for the sorting type unless it has been given to the
  7899. function through the SORTING-TYPE argument, which needs to be a character,
  7900. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?o ?O ?r ?R ?f ?F ?k ?K). Here is
  7901. the precise meaning of each character:
  7902. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  7903. c By creation time, which is assumed to be the first inactive time stamp
  7904. at the beginning of a line.
  7905. d By deadline date/time.
  7906. k By clocking time.
  7907. n Numerically, by converting the beginning of the entry/item to a number.
  7908. o By order of TODO keywords.
  7909. p By priority according to the cookie.
  7910. r By the value of a property.
  7911. s By scheduled date/time.
  7912. t By date/time, either the first active time stamp in the entry, or, if
  7913. none exist, by the first inactive one.
  7914. Capital letters will reverse the sort order.
  7915. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  7916. called with point at the beginning of the record. It must return either
  7917. a string or a number that should serve as the sorting key for that record.
  7918. Comparing entries ignores case by default. However, with an optional argument
  7919. WITH-CASE, the sorting considers case as well.
  7920. Sorting is done against the visible part of the headlines, it ignores hidden
  7921. links.
  7922. When sorting is done, call `org-after-sorting-entries-or-items-hook'."
  7923. (interactive "P")
  7924. (let ((case-func (if with-case 'identity 'downcase))
  7925. (cmstr
  7926. ;; The clock marker is lost when using `sort-subr', let's
  7927. ;; store the clocking string.
  7928. (when (equal (marker-buffer org-clock-marker) (current-buffer))
  7929. (save-excursion
  7930. (goto-char org-clock-marker)
  7931. (buffer-substring-no-properties (line-beginning-position)
  7932. (point)))))
  7933. start beg end stars re re2
  7934. txt what tmp)
  7935. ;; Find beginning and end of region to sort
  7936. (cond
  7937. ((org-region-active-p)
  7938. ;; we will sort the region
  7939. (setq end (region-end)
  7940. what "region")
  7941. (goto-char (region-beginning))
  7942. (unless (org-at-heading-p) (outline-next-heading))
  7943. (setq start (point)))
  7944. ((or (org-at-heading-p)
  7945. (ignore-errors (progn (org-back-to-heading) t)))
  7946. ;; we will sort the children of the current headline
  7947. (org-back-to-heading)
  7948. (setq start (point)
  7949. end (progn (org-end-of-subtree t t)
  7950. (or (bolp) (insert "\n"))
  7951. (when (>= (org-back-over-empty-lines) 1)
  7952. (forward-line 1))
  7953. (point))
  7954. what "children")
  7955. (goto-char start)
  7956. (outline-show-subtree)
  7957. (outline-next-heading))
  7958. (t
  7959. ;; we will sort the top-level entries in this file
  7960. (goto-char (point-min))
  7961. (or (org-at-heading-p) (outline-next-heading))
  7962. (setq start (point))
  7963. (goto-char (point-max))
  7964. (beginning-of-line 1)
  7965. (when (looking-at ".*?\\S-")
  7966. ;; File ends in a non-white line
  7967. (end-of-line 1)
  7968. (insert "\n"))
  7969. (setq end (point-max))
  7970. (setq what "top-level")
  7971. (goto-char start)
  7972. (outline-show-all)))
  7973. (setq beg (point))
  7974. (when (>= beg end) (goto-char start) (user-error "Nothing to sort"))
  7975. (looking-at "\\(\\*+\\)")
  7976. (setq stars (match-string 1)
  7977. re (concat "^" (regexp-quote stars) " +")
  7978. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[ \t\n]")
  7979. txt (buffer-substring beg end))
  7980. (unless (equal (substring txt -1) "\n") (setq txt (concat txt "\n")))
  7981. (when (and (not (equal stars "*")) (string-match re2 txt))
  7982. (user-error "Region to sort contains a level above the first entry"))
  7983. (unless sorting-type
  7984. (message
  7985. "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
  7986. [t]ime [s]cheduled [d]eadline [c]reated cloc[k]ing
  7987. A/N/P/R/O/F/T/S/D/C/K means reversed:"
  7988. what)
  7989. (setq sorting-type (read-char-exclusive))
  7990. (unless getkey-func
  7991. (and (= (downcase sorting-type) ?f)
  7992. (setq getkey-func
  7993. (org-icompleting-read "Sort using function: "
  7994. obarray 'fboundp t nil nil))
  7995. (setq getkey-func (intern getkey-func))))
  7996. (and (= (downcase sorting-type) ?r)
  7997. (not property)
  7998. (setq property
  7999. (org-icompleting-read "Property: "
  8000. (mapcar 'list (org-buffer-property-keys t))
  8001. nil t))))
  8002. (when (member sorting-type '(?k ?K)) (org-clock-sum))
  8003. (message "Sorting entries...")
  8004. (save-restriction
  8005. (narrow-to-region start end)
  8006. (let ((dcst (downcase sorting-type))
  8007. (case-fold-search nil)
  8008. (now (current-time)))
  8009. (sort-subr
  8010. (/= dcst sorting-type)
  8011. ;; This function moves to the beginning character of the "record" to
  8012. ;; be sorted.
  8013. (lambda nil
  8014. (if (re-search-forward re nil t)
  8015. (goto-char (match-beginning 0))
  8016. (goto-char (point-max))))
  8017. ;; This function moves to the last character of the "record" being
  8018. ;; sorted.
  8019. (lambda nil
  8020. (save-match-data
  8021. (condition-case nil
  8022. (outline-forward-same-level 1)
  8023. (error
  8024. (goto-char (point-max))))))
  8025. ;; This function returns the value that gets sorted against.
  8026. (lambda nil
  8027. (cond
  8028. ((= dcst ?n)
  8029. (if (looking-at org-complex-heading-regexp)
  8030. (string-to-number (org-sort-remove-invisible (match-string 4)))
  8031. nil))
  8032. ((= dcst ?a)
  8033. (if (looking-at org-complex-heading-regexp)
  8034. (funcall case-func (org-sort-remove-invisible (match-string 4)))
  8035. nil))
  8036. ((= dcst ?k)
  8037. (or (get-text-property (point) :org-clock-minutes) 0))
  8038. ((= dcst ?t)
  8039. (let ((end (save-excursion (outline-next-heading) (point))))
  8040. (if (or (re-search-forward org-ts-regexp end t)
  8041. (re-search-forward org-ts-regexp-both end t))
  8042. (org-time-string-to-seconds (match-string 0))
  8043. (org-float-time now))))
  8044. ((= dcst ?c)
  8045. (let ((end (save-excursion (outline-next-heading) (point))))
  8046. (if (re-search-forward
  8047. (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
  8048. end t)
  8049. (org-time-string-to-seconds (match-string 0))
  8050. (org-float-time now))))
  8051. ((= dcst ?s)
  8052. (let ((end (save-excursion (outline-next-heading) (point))))
  8053. (if (re-search-forward org-scheduled-time-regexp end t)
  8054. (org-time-string-to-seconds (match-string 1))
  8055. (org-float-time now))))
  8056. ((= dcst ?d)
  8057. (let ((end (save-excursion (outline-next-heading) (point))))
  8058. (if (re-search-forward org-deadline-time-regexp end t)
  8059. (org-time-string-to-seconds (match-string 1))
  8060. (org-float-time now))))
  8061. ((= dcst ?p)
  8062. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  8063. (string-to-char (match-string 2))
  8064. org-default-priority))
  8065. ((= dcst ?r)
  8066. (or (org-entry-get nil property) ""))
  8067. ((= dcst ?o)
  8068. (when (looking-at org-complex-heading-regexp)
  8069. (let* ((m (match-string 2))
  8070. (s (if (member m org-done-keywords) '- '+)))
  8071. (- 99 (funcall s (length (member m org-todo-keywords-1)))))))
  8072. ((= dcst ?f)
  8073. (if getkey-func
  8074. (progn
  8075. (setq tmp (funcall getkey-func))
  8076. (when (stringp tmp) (setq tmp (funcall case-func tmp)))
  8077. tmp)
  8078. (error "Invalid key function `%s'" getkey-func)))
  8079. (t (error "Invalid sorting type `%c'" sorting-type))))
  8080. nil
  8081. (cond
  8082. ((= dcst ?a) 'string<)
  8083. ((= dcst ?f) compare-func)
  8084. ((member dcst '(?p ?t ?s ?d ?c ?k)) '<)))))
  8085. (run-hooks 'org-after-sorting-entries-or-items-hook)
  8086. ;; Reset the clock marker if needed
  8087. (when cmstr
  8088. (save-excursion
  8089. (goto-char start)
  8090. (search-forward cmstr nil t)
  8091. (move-marker org-clock-marker (point))))
  8092. (message "Sorting entries...done")))
  8093. ;;; The orgstruct minor mode
  8094. ;; Define a minor mode which can be used in other modes in order to
  8095. ;; integrate the org-mode structure editing commands.
  8096. ;; This is really a hack, because the org-mode structure commands use
  8097. ;; keys which normally belong to the major mode. Here is how it
  8098. ;; works: The minor mode defines all the keys necessary to operate the
  8099. ;; structure commands, but wraps the commands into a function which
  8100. ;; tests if the cursor is currently at a headline or a plain list
  8101. ;; item. If that is the case, the structure command is used,
  8102. ;; temporarily setting many Org-mode variables like regular
  8103. ;; expressions for filling etc. However, when any of those keys is
  8104. ;; used at a different location, function uses `key-binding' to look
  8105. ;; up if the key has an associated command in another currently active
  8106. ;; keymap (minor modes, major mode, global), and executes that
  8107. ;; command. There might be problems if any of the keys is otherwise
  8108. ;; used as a prefix key.
  8109. (defcustom orgstruct-heading-prefix-regexp ""
  8110. "Regexp that matches the custom prefix of Org headlines in
  8111. orgstruct(++)-mode."
  8112. :group 'org
  8113. :version "24.4"
  8114. :package-version '(Org . "8.3")
  8115. :type 'regexp)
  8116. ;;;###autoload(put 'orgstruct-heading-prefix-regexp 'safe-local-variable 'stringp)
  8117. (defcustom orgstruct-setup-hook nil
  8118. "Hook run after orgstruct-mode-map is filled."
  8119. :group 'org
  8120. :version "24.4"
  8121. :package-version '(Org . "8.0")
  8122. :type 'hook)
  8123. (defvar orgstruct-initialized nil)
  8124. (defvar org-local-vars nil
  8125. "List of local variables, for use by `orgstruct-mode'.")
  8126. ;;;###autoload
  8127. (define-minor-mode orgstruct-mode
  8128. "Toggle the minor mode `orgstruct-mode'.
  8129. This mode is for using Org-mode structure commands in other
  8130. modes. The following keys behave as if Org-mode were active, if
  8131. the cursor is on a headline, or on a plain list item (both as
  8132. defined by Org-mode)."
  8133. nil " OrgStruct" (make-sparse-keymap)
  8134. (funcall (if orgstruct-mode
  8135. 'add-to-invisibility-spec
  8136. 'remove-from-invisibility-spec)
  8137. '(outline . t))
  8138. (when orgstruct-mode
  8139. (org-load-modules-maybe)
  8140. (unless orgstruct-initialized
  8141. (orgstruct-setup)
  8142. (setq orgstruct-initialized t))))
  8143. ;;;###autoload
  8144. (defun turn-on-orgstruct ()
  8145. "Unconditionally turn on `orgstruct-mode'."
  8146. (orgstruct-mode 1))
  8147. (defvar-local orgstruct-is-++ nil
  8148. "Is `orgstruct-mode' in ++ version in the current-buffer?")
  8149. (defvar-local org-fb-vars nil)
  8150. (defun orgstruct++-mode (&optional arg)
  8151. "Toggle `orgstruct-mode', the enhanced version of it.
  8152. In addition to setting orgstruct-mode, this also exports all
  8153. indentation and autofilling variables from org-mode into the
  8154. buffer. It will also recognize item context in multiline items."
  8155. (interactive "P")
  8156. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  8157. (if (< arg 1)
  8158. (progn (orgstruct-mode -1)
  8159. (mapc (lambda(v)
  8160. (set (make-local-variable (car v))
  8161. (if (eq (car-safe (cadr v)) 'quote) (cadadr v) (cadr v))))
  8162. org-fb-vars))
  8163. (orgstruct-mode 1)
  8164. (setq org-fb-vars nil)
  8165. (unless org-local-vars
  8166. (setq org-local-vars (org-get-local-variables)))
  8167. (let (var val)
  8168. (mapc
  8169. (lambda (x)
  8170. (when (string-match
  8171. "^\\(paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|fill-prefix\\|indent-\\)"
  8172. (symbol-name (car x)))
  8173. (setq var (car x) val (nth 1 x))
  8174. (push (list var `(quote ,(eval var))) org-fb-vars)
  8175. (set (make-local-variable var)
  8176. (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  8177. org-local-vars)
  8178. (setq-local orgstruct-is-++ t))))
  8179. ;;;###autoload
  8180. (defun turn-on-orgstruct++ ()
  8181. "Unconditionally turn on `orgstruct++-mode'."
  8182. (orgstruct++-mode 1))
  8183. (defun orgstruct-error ()
  8184. "Error when there is no default binding for a structure key."
  8185. (interactive)
  8186. (funcall (if (fboundp 'user-error)
  8187. 'user-error
  8188. 'error)
  8189. "This key has no function outside structure elements"))
  8190. (defun orgstruct-setup ()
  8191. "Setup orgstruct keymap."
  8192. (dolist (cell '((org-demote . t)
  8193. (org-metaleft . t)
  8194. (org-metaright . t)
  8195. (org-promote . t)
  8196. (org-shiftmetaleft . t)
  8197. (org-shiftmetaright . t)
  8198. org-backward-element
  8199. org-backward-heading-same-level
  8200. org-ctrl-c-ret
  8201. org-ctrl-c-minus
  8202. org-ctrl-c-star
  8203. org-cycle
  8204. org-forward-heading-same-level
  8205. org-insert-heading
  8206. org-insert-heading-respect-content
  8207. org-kill-note-or-show-branches
  8208. org-mark-subtree
  8209. org-meta-return
  8210. org-metadown
  8211. org-metaup
  8212. org-narrow-to-subtree
  8213. org-promote-subtree
  8214. org-reveal
  8215. org-shiftdown
  8216. org-shiftleft
  8217. org-shiftmetadown
  8218. org-shiftmetaup
  8219. org-shiftright
  8220. org-shifttab
  8221. org-shifttab
  8222. org-shiftup
  8223. org-show-children
  8224. org-show-subtree
  8225. org-sort
  8226. org-up-element
  8227. outline-demote
  8228. outline-next-visible-heading
  8229. outline-previous-visible-heading
  8230. outline-promote
  8231. outline-up-heading))
  8232. (let ((f (or (car-safe cell) cell))
  8233. (disable-when-heading-prefix (cdr-safe cell)))
  8234. (when (fboundp f)
  8235. (let ((new-bindings))
  8236. (dolist (binding (nconc (where-is-internal f org-mode-map)
  8237. (where-is-internal f outline-mode-map)))
  8238. (push binding new-bindings)
  8239. ;; TODO use local-function-key-map
  8240. (dolist (rep '(("<tab>" . "TAB")
  8241. ("<return>" . "RET")
  8242. ("<escape>" . "ESC")
  8243. ("<delete>" . "DEL")))
  8244. (setq binding (read-kbd-macro
  8245. (let ((case-fold-search))
  8246. (replace-regexp-in-string
  8247. (regexp-quote (cdr rep))
  8248. (car rep)
  8249. (key-description binding)))))
  8250. (pushnew binding new-bindings :test 'equal)))
  8251. (dolist (binding new-bindings)
  8252. (let ((key (lookup-key orgstruct-mode-map binding)))
  8253. (when (or (not key) (numberp key))
  8254. (ignore-errors
  8255. (org-defkey orgstruct-mode-map
  8256. binding
  8257. (orgstruct-make-binding
  8258. f binding disable-when-heading-prefix))))))))))
  8259. (run-hooks 'orgstruct-setup-hook))
  8260. (defun orgstruct-make-binding (fun key disable-when-heading-prefix)
  8261. "Create a function for binding in the structure minor mode.
  8262. FUN is the command to call inside a table. KEY is the key that
  8263. should be checked in for a command to execute outside of tables.
  8264. Non-nil `disable-when-heading-prefix' means to disable the command
  8265. if `orgstruct-heading-prefix-regexp' is not empty."
  8266. (let ((name (concat "orgstruct-hijacker-" (symbol-name fun))))
  8267. (let ((nname name)
  8268. (i 0))
  8269. (while (fboundp (intern nname))
  8270. (setq nname (format "%s-%d" name (setq i (1+ i)))))
  8271. (setq name (intern nname)))
  8272. (eval
  8273. (let ((bindings '((org-heading-regexp
  8274. (concat "^"
  8275. orgstruct-heading-prefix-regexp
  8276. "\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ ]*$"))
  8277. (org-outline-regexp
  8278. (concat orgstruct-heading-prefix-regexp "\\*+ "))
  8279. (org-outline-regexp-bol
  8280. (concat "^" org-outline-regexp))
  8281. (outline-regexp org-outline-regexp)
  8282. (outline-heading-end-regexp "\n")
  8283. (outline-level 'org-outline-level)
  8284. (outline-heading-alist))))
  8285. `(defun ,name (arg)
  8286. ,(concat "In Structure, run `" (symbol-name fun) "'.\n"
  8287. "Outside of structure, run the binding of `"
  8288. (key-description key) "'."
  8289. (when disable-when-heading-prefix
  8290. (concat
  8291. "\nIf `orgstruct-heading-prefix-regexp' is not empty, this command will always fall\n"
  8292. "back to the default binding due to limitations of Org's implementation of\n"
  8293. "`" (symbol-name fun) "'.")))
  8294. (interactive "p")
  8295. (let* ((disable
  8296. ,(and disable-when-heading-prefix
  8297. '(not (string= orgstruct-heading-prefix-regexp ""))))
  8298. (fallback
  8299. (or disable
  8300. (not
  8301. (let* ,bindings
  8302. (org-context-p 'headline 'item
  8303. ,(when (memq fun
  8304. '(org-insert-heading
  8305. org-insert-heading-respect-content
  8306. org-meta-return))
  8307. '(when orgstruct-is-++
  8308. 'item-body))))))))
  8309. (if fallback
  8310. (let* ((orgstruct-mode)
  8311. (binding
  8312. (let ((key ,key))
  8313. (catch 'exit
  8314. (dolist
  8315. (rep
  8316. '(nil
  8317. ("<\\([^>]*\\)tab>" . "\\1TAB")
  8318. ("<\\([^>]*\\)return>" . "\\1RET")
  8319. ("<\\([^>]*\\)escape>" . "\\1ESC")
  8320. ("<\\([^>]*\\)delete>" . "\\1DEL"))
  8321. nil)
  8322. (when rep
  8323. (setq key (read-kbd-macro
  8324. (let ((case-fold-search))
  8325. (replace-regexp-in-string
  8326. (car rep)
  8327. (cdr rep)
  8328. (key-description key))))))
  8329. (when (key-binding key)
  8330. (throw 'exit (key-binding key))))))))
  8331. (if (keymapp binding)
  8332. (org-set-transient-map binding)
  8333. (let ((func (or binding
  8334. (unless disable
  8335. 'orgstruct-error))))
  8336. (when func
  8337. (call-interactively func)))))
  8338. (org-run-like-in-org-mode
  8339. (lambda ()
  8340. (interactive)
  8341. (let* ,bindings
  8342. (call-interactively ',fun)))))))))
  8343. name))
  8344. (defun org-contextualize-keys (alist contexts)
  8345. "Return valid elements in ALIST depending on CONTEXTS.
  8346. `org-agenda-custom-commands' or `org-capture-templates' are the
  8347. values used for ALIST, and `org-agenda-custom-commands-contexts'
  8348. or `org-capture-templates-contexts' are the associated contexts
  8349. definitions."
  8350. (let ((contexts
  8351. ;; normalize contexts
  8352. (mapcar
  8353. (lambda(c) (cond ((listp (cadr c))
  8354. (list (car c) (car c) (cadr c)))
  8355. ((string= "" (cadr c))
  8356. (list (car c) (car c) (caddr c)))
  8357. (t c)))
  8358. contexts))
  8359. (a alist) r s)
  8360. ;; loop over all commands or templates
  8361. (dolist (c a)
  8362. (let (vrules repl)
  8363. (cond
  8364. ((not (assoc (car c) contexts))
  8365. (push c r))
  8366. ((and (assoc (car c) contexts)
  8367. (setq vrules (org-contextualize-validate-key
  8368. (car c) contexts)))
  8369. (mapc (lambda (vr)
  8370. (unless (equal (car vr) (cadr vr))
  8371. (setq repl vr)))
  8372. vrules)
  8373. (if (not repl) (push c r)
  8374. (push (cadr repl) s)
  8375. (push
  8376. (cons (car c)
  8377. (cdr (or (assoc (cadr repl) alist)
  8378. (error "Undefined key `%s' as contextual replacement for `%s'"
  8379. (cadr repl) (car c)))))
  8380. r))))))
  8381. ;; Return limited ALIST, possibly with keys modified, and deduplicated
  8382. (delq
  8383. nil
  8384. (delete-dups
  8385. (mapcar (lambda (x)
  8386. (let ((tpl (car x)))
  8387. (unless (delq
  8388. nil
  8389. (mapcar (lambda (y)
  8390. (equal y tpl))
  8391. s))
  8392. x)))
  8393. (reverse r))))))
  8394. (defun org-contextualize-validate-key (key contexts)
  8395. "Check CONTEXTS for agenda or capture KEY."
  8396. (let (rr res)
  8397. (dolist (r contexts)
  8398. (mapc
  8399. (lambda (rr)
  8400. (when
  8401. (and (equal key (car r))
  8402. (if (functionp rr) (funcall rr)
  8403. (or (and (eq (car rr) 'in-file)
  8404. (buffer-file-name)
  8405. (string-match (cdr rr) (buffer-file-name)))
  8406. (and (eq (car rr) 'in-mode)
  8407. (string-match (cdr rr) (symbol-name major-mode)))
  8408. (and (eq (car rr) 'in-buffer)
  8409. (string-match (cdr rr) (buffer-name)))
  8410. (when (and (eq (car rr) 'not-in-file)
  8411. (buffer-file-name))
  8412. (not (string-match (cdr rr) (buffer-file-name))))
  8413. (when (eq (car rr) 'not-in-mode)
  8414. (not (string-match (cdr rr) (symbol-name major-mode))))
  8415. (when (eq (car rr) 'not-in-buffer)
  8416. (not (string-match (cdr rr) (buffer-name)))))))
  8417. (push r res)))
  8418. (car (last r))))
  8419. (delete-dups (delq nil res))))
  8420. (defun org-context-p (&rest contexts)
  8421. "Check if local context is any of CONTEXTS.
  8422. Possible values in the list of contexts are `table', `headline', and `item'."
  8423. (let ((pos (point)))
  8424. (goto-char (point-at-bol))
  8425. (prog1 (or (and (memq 'table contexts)
  8426. (looking-at "[ \t]*|"))
  8427. (and (memq 'headline contexts)
  8428. (looking-at org-outline-regexp))
  8429. (and (memq 'item contexts)
  8430. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  8431. (and (memq 'item-body contexts)
  8432. (org-in-item-p)))
  8433. (goto-char pos))))
  8434. (defun org-get-local-variables ()
  8435. "Return a list of all local variables in an Org mode buffer."
  8436. (let ((varlist
  8437. (prog1 (with-current-buffer (get-buffer-create "*Org tmp*")
  8438. (erase-buffer)
  8439. (org-mode)
  8440. (buffer-local-variables))
  8441. (kill-buffer "*Org tmp*"))))
  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. ;; Ignore internal local variables, since those
  8449. ;; are likely variables that are not meant to be
  8450. ;; copied.
  8451. (not (string-match-p "--" (symbol-name name)))
  8452. (string-match-p
  8453. "\\`\\(org-\\|orgtbl-\\|outline-\\|comment-\\|\
  8454. paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|indent-\\)"
  8455. (symbol-name name))
  8456. x)))
  8457. varlist))))
  8458. (defun org-clone-local-variables (from-buffer &optional regexp)
  8459. "Clone local variables from FROM-BUFFER.
  8460. Optional argument REGEXP selects variables to clone."
  8461. (dolist (pair (buffer-local-variables from-buffer))
  8462. (let ((name (car pair)))
  8463. (when (and (symbolp name)
  8464. ;; Ignore internal local variables, since those are
  8465. ;; likely variables that are not meant to be copied.
  8466. (not (string-match-p "--" (symbol-name name)))
  8467. (or (null regexp) (string-match regexp (symbol-name name))))
  8468. (set (make-local-variable name) (cdr pair))))))
  8469. ;;;###autoload
  8470. (defun org-run-like-in-org-mode (cmd)
  8471. "Run a command, pretending that the current buffer is in Org-mode.
  8472. This will temporarily bind local variables that are typically bound in
  8473. Org-mode to the values they have in Org-mode, and then interactively
  8474. call CMD."
  8475. (org-load-modules-maybe)
  8476. (unless org-local-vars
  8477. (setq org-local-vars (org-get-local-variables)))
  8478. (let (binds)
  8479. (dolist (var org-local-vars)
  8480. (when (or (not (boundp (car var)))
  8481. (eq (symbol-value (car var))
  8482. (default-value (car var))))
  8483. (push (list (car var) `(quote ,(cadr var))) binds)))
  8484. (eval `(let ,binds
  8485. (call-interactively (quote ,cmd))))))
  8486. (defun org-get-category (&optional pos force-refresh)
  8487. "Get the category applying to position POS."
  8488. (save-match-data
  8489. (when force-refresh (org-refresh-category-properties))
  8490. (let ((pos (or pos (point))))
  8491. (or (get-text-property pos 'org-category)
  8492. (progn (org-refresh-category-properties)
  8493. (get-text-property pos 'org-category))))))
  8494. ;;; Refresh properties
  8495. (defun org-refresh-properties (dprop tprop)
  8496. "Refresh buffer text properties.
  8497. DPROP is the drawer property and TPROP is either the
  8498. corresponding text property to set, or an alist with each element
  8499. being a text property (as a symbol) and a function to apply to
  8500. the value of the drawer property."
  8501. (let ((case-fold-search t)
  8502. (inhibit-read-only t))
  8503. (org-with-silent-modifications
  8504. (save-excursion
  8505. (save-restriction
  8506. (widen)
  8507. (goto-char (point-min))
  8508. (while (re-search-forward (concat "^[ \t]*:" dprop ": +\\(.*\\)[ \t]*$") nil t)
  8509. (org-refresh-property tprop (org-match-string-no-properties 1))))))))
  8510. (defun org-refresh-property (tprop p)
  8511. "Refresh the buffer text property TPROP from the drawer property P.
  8512. The refresh happens only for the current tree (not subtree)."
  8513. (unless (org-before-first-heading-p)
  8514. (save-excursion
  8515. (org-back-to-heading t)
  8516. (if (symbolp tprop)
  8517. ;; TPROP is a text property symbol
  8518. (put-text-property
  8519. (point) (or (outline-next-heading) (point-max)) tprop p)
  8520. ;; TPROP is an alist with (properties . function) elements
  8521. (dolist (al tprop)
  8522. (save-excursion
  8523. (put-text-property
  8524. (line-beginning-position) (or (outline-next-heading) (point-max))
  8525. (car al)
  8526. (funcall (cdr al) p))))))))
  8527. (defun org-refresh-category-properties ()
  8528. "Refresh category text properties in the buffer."
  8529. (let ((case-fold-search t)
  8530. (inhibit-read-only t)
  8531. (default-category
  8532. (cond ((null org-category)
  8533. (if buffer-file-name
  8534. (file-name-sans-extension
  8535. (file-name-nondirectory buffer-file-name))
  8536. "???"))
  8537. ((symbolp org-category) (symbol-name org-category))
  8538. (t org-category))))
  8539. (org-with-silent-modifications
  8540. (org-with-wide-buffer
  8541. ;; Set buffer-wide category. Search last #+CATEGORY keyword.
  8542. ;; This is the default category for the buffer. If none is
  8543. ;; found, fall-back to `org-category' or buffer file name.
  8544. (put-text-property
  8545. (point-min) (point-max)
  8546. 'org-category
  8547. (catch 'buffer-category
  8548. (goto-char (point-max))
  8549. (while (re-search-backward "^[ \t]*#\\+CATEGORY:" (point-min) t)
  8550. (let ((element (org-element-at-point)))
  8551. (when (eq (org-element-type element) 'keyword)
  8552. (throw 'buffer-category
  8553. (org-element-property :value element)))))
  8554. default-category))
  8555. ;; Set sub-tree specific categories.
  8556. (goto-char (point-min))
  8557. (let ((regexp (org-re-property "CATEGORY")))
  8558. (while (re-search-forward regexp nil t)
  8559. (let ((value (org-match-string-no-properties 3)))
  8560. (when (org-at-property-p)
  8561. (put-text-property
  8562. (save-excursion (org-back-to-heading t) (point))
  8563. (save-excursion (org-end-of-subtree t t) (point))
  8564. 'org-category
  8565. value)))))))))
  8566. (defun org-refresh-stats-properties ()
  8567. "Refresh stats text properties in the buffer."
  8568. (let (stats)
  8569. (org-with-silent-modifications
  8570. (save-excursion
  8571. (save-restriction
  8572. (widen)
  8573. (goto-char (point-min))
  8574. (while (re-search-forward
  8575. (concat org-outline-regexp-bol ".*"
  8576. "\\(?:\\[\\([0-9]+\\)%\\|\\([0-9]+\\)/\\([0-9]+\\)\\]\\)")
  8577. nil t)
  8578. (setq stats (cond ((equal (match-string 3) "0") 0)
  8579. ((match-string 2)
  8580. (/ (* (string-to-number (match-string 2)) 100)
  8581. (string-to-number (match-string 3))))
  8582. (t (string-to-number (match-string 1)))))
  8583. (org-back-to-heading t)
  8584. (put-text-property (point) (progn (org-end-of-subtree t t) (point))
  8585. 'org-stats stats)))))))
  8586. (defun org-refresh-effort-properties ()
  8587. "Refresh effort properties"
  8588. (org-refresh-properties
  8589. org-effort-property
  8590. '((effort . identity)
  8591. (effort-minutes . org-duration-string-to-minutes))))
  8592. ;;;; Link Stuff
  8593. ;;; Link abbreviations
  8594. (defun org-link-expand-abbrev (link)
  8595. "Apply replacements as defined in `org-link-abbrev-alist'."
  8596. (if (string-match "^\\([^:]*\\)\\(::?\\(.*\\)\\)?$" link)
  8597. (let* ((key (match-string 1 link))
  8598. (as (or (assoc key org-link-abbrev-alist-local)
  8599. (assoc key org-link-abbrev-alist)))
  8600. (tag (and (match-end 2) (match-string 3 link)))
  8601. rpl)
  8602. (if (not as)
  8603. link
  8604. (setq rpl (cdr as))
  8605. (cond
  8606. ((symbolp rpl) (funcall rpl tag))
  8607. ((string-match "%(\\([^)]+\\))" rpl)
  8608. (replace-match
  8609. (save-match-data
  8610. (funcall (intern-soft (match-string 1 rpl)) tag)) t t rpl))
  8611. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  8612. ((string-match "%h" rpl)
  8613. (replace-match (url-hexify-string (or tag "")) t t rpl))
  8614. (t (concat rpl tag)))))
  8615. link))
  8616. ;;; Storing and inserting links
  8617. (defvar org-insert-link-history nil
  8618. "Minibuffer history for links inserted with `org-insert-link'.")
  8619. (defvar org-stored-links nil
  8620. "Contains the links stored with `org-store-link'.")
  8621. (defvar org-store-link-plist nil
  8622. "Plist with info about the most recently link created with `org-store-link'.")
  8623. (defvar org-link-protocols nil
  8624. "Link protocols added to Org-mode using `org-add-link-type'.")
  8625. (defvar org-store-link-functions nil
  8626. "List of functions that are called to create and store a link.
  8627. Each function will be called in turn until one returns a non-nil
  8628. value. Each function should check if it is responsible for creating
  8629. this link (for example by looking at the major mode).
  8630. If not, it must exit and return nil.
  8631. If yes, it should return a non-nil value after a calling
  8632. `org-store-link-props' with a list of properties and values.
  8633. Special properties are:
  8634. :type The link prefix, like \"http\". This must be given.
  8635. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  8636. This is obligatory as well.
  8637. :description Optional default description for the second pair
  8638. of brackets in an Org-mode link. The user can still change
  8639. this when inserting this link into an Org-mode buffer.
  8640. In addition to these, any additional properties can be specified
  8641. and then used in capture templates.")
  8642. (defun org-add-link-type (type &optional follow export)
  8643. "Add TYPE to the list of `org-link-types'.
  8644. Re-compute all regular expressions depending on `org-link-types'
  8645. FOLLOW and EXPORT are two functions.
  8646. FOLLOW should take the link path as the single argument and do whatever
  8647. is necessary to follow the link, for example find a file or display
  8648. a mail message.
  8649. EXPORT should format the link path for export to one of the export formats.
  8650. It should be a function accepting three arguments:
  8651. path the path of the link, the text after the prefix (like \"http:\")
  8652. desc the description of the link, if any
  8653. format the export format, a symbol like `html' or `latex' or `ascii'.
  8654. The function may use the FORMAT information to return different values
  8655. depending on the format. The return value will be put literally into
  8656. the exported file. If the return value is nil, this means Org should
  8657. do what it normally does with links which do not have EXPORT defined.
  8658. Org mode has a built-in default for exporting links. If you are happy with
  8659. this default, there is no need to define an export function for the link
  8660. type. For a simple example of an export function, see `org-bbdb.el'."
  8661. (add-to-list 'org-link-types type t)
  8662. (org-make-link-regexps)
  8663. (org-element-update-syntax)
  8664. (if (assoc type org-link-protocols)
  8665. (setcdr (assoc type org-link-protocols) (list follow export))
  8666. (push (list type follow export) org-link-protocols)))
  8667. (defvar org-agenda-buffer-name) ; Defined in org-agenda.el
  8668. (defvar org-id-link-to-org-use-id) ; Defined in org-id.el
  8669. ;;;###autoload
  8670. (defun org-store-link (arg)
  8671. "\\<org-mode-map>Store an org-link to the current location.
  8672. This link is added to `org-stored-links' and can later be inserted
  8673. into an Org buffer with \\[org-insert-link].
  8674. For some link types, a prefix ARG is interpreted.
  8675. For links to Usenet articles, ARG negates `org-gnus-prefer-web-links'.
  8676. For file links, ARG negates `org-context-in-file-links'.
  8677. A double prefix ARG force skipping storing functions that are not
  8678. part of Org's core.
  8679. A triple prefix ARG force storing a link for each line in the
  8680. active region."
  8681. (interactive "P")
  8682. (org-load-modules-maybe)
  8683. (if (and (equal arg '(64)) (org-region-active-p))
  8684. (save-excursion
  8685. (let ((end (region-end)))
  8686. (goto-char (region-beginning))
  8687. (set-mark (point))
  8688. (while (< (point-at-eol) end)
  8689. (move-end-of-line 1) (activate-mark)
  8690. (let (current-prefix-arg)
  8691. (call-interactively 'org-store-link))
  8692. (move-beginning-of-line 2)
  8693. (set-mark (point)))))
  8694. (org-with-limited-levels
  8695. (setq org-store-link-plist nil)
  8696. (let (link cpltxt desc description search
  8697. txt custom-id agenda-link sfuns sfunsn)
  8698. (cond
  8699. ;; Store a link using an external link type
  8700. ((and (not (equal arg '(16)))
  8701. (setq sfuns
  8702. (delq
  8703. nil (mapcar (lambda (f)
  8704. (let (fs) (if (funcall f) (push f fs))))
  8705. org-store-link-functions))
  8706. sfunsn (mapcar (lambda (fu) (symbol-name (car fu))) sfuns))
  8707. (or (and (cdr sfuns)
  8708. (funcall (intern
  8709. (completing-read
  8710. "Which function for creating the link? "
  8711. sfunsn nil t (car sfunsn)))))
  8712. (funcall (caar sfuns)))
  8713. (setq link (plist-get org-store-link-plist :link)
  8714. desc (or (plist-get org-store-link-plist
  8715. :description)
  8716. link))))
  8717. ;; Store a link from a source code buffer.
  8718. ((org-src-edit-buffer-p)
  8719. (cond
  8720. ((save-excursion
  8721. (beginning-of-line)
  8722. (looking-at (concat (format org-coderef-label-format "\\(.*?\\)")
  8723. "[ \t]*$")))
  8724. (setq link (format "(%s)" (org-match-string-no-properties 1))))
  8725. ((org-called-interactively-p 'any)
  8726. (let (label)
  8727. (while (or (not label)
  8728. (org-with-wide-buffer
  8729. (goto-char (point-min))
  8730. (re-search-forward
  8731. (regexp-quote (format org-coderef-label-format label))
  8732. nil t)))
  8733. (when label (message "Label exists already") (sit-for 2))
  8734. (setq label (read-string "Code line label: " label)))
  8735. (end-of-line)
  8736. (setq link (format org-coderef-label-format label))
  8737. (let ((gc (- 79 (length link))))
  8738. (if (< (current-column) gc) (org-move-to-column gc t)
  8739. (insert " ")))
  8740. (insert link)
  8741. (setq link (concat "(" label ")") desc nil)))
  8742. (t (setq link nil))))
  8743. ;; We are in the agenda, link to referenced location
  8744. ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
  8745. (let ((m (or (get-text-property (point) 'org-hd-marker)
  8746. (get-text-property (point) 'org-marker))))
  8747. (when m
  8748. (org-with-point-at m
  8749. (setq agenda-link
  8750. (if (org-called-interactively-p 'any)
  8751. (call-interactively 'org-store-link)
  8752. (org-store-link nil)))))))
  8753. ((eq major-mode 'calendar-mode)
  8754. (let ((cd (calendar-cursor-to-date)))
  8755. (setq link
  8756. (format-time-string
  8757. (car org-time-stamp-formats)
  8758. (apply 'encode-time
  8759. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  8760. nil nil nil))))
  8761. (org-store-link-props :type "calendar" :date cd)))
  8762. ((eq major-mode 'help-mode)
  8763. (setq link (concat "help:" (save-excursion
  8764. (goto-char (point-min))
  8765. (looking-at "^[^ ]+")
  8766. (match-string 0))))
  8767. (org-store-link-props :type "help"))
  8768. ((eq major-mode 'w3-mode)
  8769. (setq cpltxt (if (and (buffer-name)
  8770. (not (string-match "Untitled" (buffer-name))))
  8771. (buffer-name)
  8772. (url-view-url t))
  8773. link (url-view-url t))
  8774. (org-store-link-props :type "w3" :url (url-view-url t)))
  8775. ((eq major-mode 'image-mode)
  8776. (setq cpltxt (concat "file:"
  8777. (abbreviate-file-name buffer-file-name))
  8778. link cpltxt)
  8779. (org-store-link-props :type "image" :file buffer-file-name))
  8780. ;; In dired, store a link to the file of the current line
  8781. ((derived-mode-p 'dired-mode)
  8782. (let ((file (dired-get-filename nil t)))
  8783. (setq file (if file
  8784. (abbreviate-file-name
  8785. (expand-file-name (dired-get-filename nil t)))
  8786. ;; otherwise, no file so use current directory.
  8787. default-directory))
  8788. (setq cpltxt (concat "file:" file)
  8789. link cpltxt)))
  8790. ((setq search (run-hook-with-args-until-success
  8791. 'org-create-file-search-functions))
  8792. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  8793. "::" search))
  8794. (setq cpltxt (or description link)))
  8795. ((and (buffer-file-name (buffer-base-buffer)) (derived-mode-p 'org-mode))
  8796. (setq custom-id (org-entry-get nil "CUSTOM_ID"))
  8797. (cond
  8798. ;; Store a link using the target at point
  8799. ((org-in-regexp "[^<]<<\\([^<>]+\\)>>[^>]" 1)
  8800. (setq cpltxt
  8801. (concat "file:"
  8802. (abbreviate-file-name
  8803. (buffer-file-name (buffer-base-buffer)))
  8804. "::" (match-string 1))
  8805. link cpltxt))
  8806. ((and (featurep 'org-id)
  8807. (or (eq org-id-link-to-org-use-id t)
  8808. (and (org-called-interactively-p 'any)
  8809. (or (eq org-id-link-to-org-use-id 'create-if-interactive)
  8810. (and (eq org-id-link-to-org-use-id
  8811. 'create-if-interactive-and-no-custom-id)
  8812. (not custom-id))))
  8813. (and org-id-link-to-org-use-id (org-entry-get nil "ID"))))
  8814. ;; Store a link using the ID at point
  8815. (setq link (condition-case nil
  8816. (prog1 (org-id-store-link)
  8817. (setq desc (or (plist-get org-store-link-plist
  8818. :description)
  8819. "")))
  8820. (error
  8821. ;; Probably before first headline, link only to file
  8822. (concat "file:"
  8823. (abbreviate-file-name
  8824. (buffer-file-name (buffer-base-buffer))))))))
  8825. (t
  8826. ;; Just link to current headline
  8827. (setq cpltxt (concat "file:"
  8828. (abbreviate-file-name
  8829. (buffer-file-name (buffer-base-buffer)))))
  8830. ;; Add a context search string
  8831. (when (org-xor org-context-in-file-links arg)
  8832. (let* ((ee (org-element-at-point))
  8833. (et (org-element-type ee))
  8834. (ev (plist-get (cadr ee) :value))
  8835. (ek (plist-get (cadr ee) :key))
  8836. (eok (and (stringp ek) (string-match "name" ek))))
  8837. (setq txt (cond
  8838. ((org-at-heading-p) nil)
  8839. ((and (eq et 'keyword) eok) ev)
  8840. ((org-region-active-p)
  8841. (buffer-substring (region-beginning) (region-end)))))
  8842. (when (or (null txt) (string-match "\\S-" txt))
  8843. (setq cpltxt
  8844. (concat cpltxt "::"
  8845. (condition-case nil
  8846. (org-make-org-heading-search-string txt)
  8847. (error "")))
  8848. desc (or (and (eq et 'keyword) eok ev)
  8849. (nth 4 (ignore-errors (org-heading-components)))
  8850. "NONE")))))
  8851. (when (string-match "::\\'" cpltxt)
  8852. (setq cpltxt (substring cpltxt 0 -2)))
  8853. (setq link cpltxt))))
  8854. ((buffer-file-name (buffer-base-buffer))
  8855. ;; Just link to this file here.
  8856. (setq cpltxt (concat "file:"
  8857. (abbreviate-file-name
  8858. (buffer-file-name (buffer-base-buffer)))))
  8859. ;; Add a context string.
  8860. (when (org-xor org-context-in-file-links arg)
  8861. (setq txt (if (org-region-active-p)
  8862. (buffer-substring (region-beginning) (region-end))
  8863. (buffer-substring (point-at-bol) (point-at-eol))))
  8864. ;; Only use search option if there is some text.
  8865. (when (string-match "\\S-" txt)
  8866. (setq cpltxt
  8867. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  8868. desc "NONE")))
  8869. (setq link cpltxt))
  8870. ((org-called-interactively-p 'interactive)
  8871. (user-error "No method for storing a link from this buffer"))
  8872. (t (setq link nil)))
  8873. ;; We're done setting link and desc, clean up
  8874. (when (consp link) (setq cpltxt (car link) link (cdr link)))
  8875. (setq link (or link cpltxt)
  8876. desc (or desc cpltxt))
  8877. (cond ((equal desc "NONE") (setq desc nil))
  8878. ((and desc (string-match org-bracket-link-analytic-regexp desc))
  8879. (let ((d0 (match-string 3 desc))
  8880. (p0 (match-string 5 desc)))
  8881. (setq desc
  8882. (replace-regexp-in-string
  8883. org-bracket-link-regexp
  8884. (concat (or p0 d0)
  8885. (if (equal (length (match-string 0 desc))
  8886. (length desc)) "*" "")) desc)))))
  8887. ;; Return the link
  8888. (if (not (and (or (org-called-interactively-p 'any)
  8889. executing-kbd-macro)
  8890. link))
  8891. (or agenda-link (and link (org-make-link-string link desc)))
  8892. (push (list link desc) org-stored-links)
  8893. (message "Stored: %s" (or desc link))
  8894. (when custom-id
  8895. (setq link (concat "file:" (abbreviate-file-name
  8896. (buffer-file-name)) "::#" custom-id))
  8897. (push (list link desc) org-stored-links))
  8898. (car org-stored-links))))))
  8899. (defun org-store-link-props (&rest plist)
  8900. "Store link properties, extract names and addresses."
  8901. (let (x adr)
  8902. (when (setq x (plist-get plist :from))
  8903. (setq adr (mail-extract-address-components x))
  8904. (setq plist (plist-put plist :fromname (car adr)))
  8905. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  8906. (when (setq x (plist-get plist :to))
  8907. (setq adr (mail-extract-address-components x))
  8908. (setq plist (plist-put plist :toname (car adr)))
  8909. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  8910. (let ((from (plist-get plist :from))
  8911. (to (plist-get plist :to)))
  8912. (when (and from to org-from-is-user-regexp)
  8913. (setq plist
  8914. (plist-put plist :fromto
  8915. (if (string-match org-from-is-user-regexp from)
  8916. (concat "to %t")
  8917. (concat "from %f"))))))
  8918. (setq org-store-link-plist plist))
  8919. (defun org-add-link-props (&rest plist)
  8920. "Add these properties to the link property list."
  8921. (let (key value)
  8922. (while plist
  8923. (setq key (pop plist) value (pop plist))
  8924. (setq org-store-link-plist
  8925. (plist-put org-store-link-plist key value)))))
  8926. (defun org-email-link-description (&optional fmt)
  8927. "Return the description part of an email link.
  8928. This takes information from `org-store-link-plist' and formats it
  8929. according to FMT (default from `org-email-link-description-format')."
  8930. (setq fmt (or fmt org-email-link-description-format))
  8931. (let* ((p org-store-link-plist)
  8932. (to (plist-get p :toaddress))
  8933. (from (plist-get p :fromaddress))
  8934. (table
  8935. (list
  8936. (cons "%c" (plist-get p :fromto))
  8937. (cons "%F" (plist-get p :from))
  8938. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  8939. (cons "%T" (plist-get p :to))
  8940. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  8941. (cons "%s" (plist-get p :subject))
  8942. (cons "%d" (plist-get p :date))
  8943. (cons "%m" (plist-get p :message-id)))))
  8944. (when (string-match "%c" fmt)
  8945. ;; Check if the user wrote this message
  8946. (if (and org-from-is-user-regexp from to
  8947. (save-match-data (string-match org-from-is-user-regexp from)))
  8948. (setq fmt (replace-match "to %t" t t fmt))
  8949. (setq fmt (replace-match "from %f" t t fmt))))
  8950. (org-replace-escapes fmt table)))
  8951. (defun org-make-org-heading-search-string (&optional string)
  8952. "Make search string for the current headline or STRING."
  8953. (let ((s (or string
  8954. (and (derived-mode-p 'org-mode)
  8955. (save-excursion
  8956. (org-back-to-heading t)
  8957. (org-element-property :raw-value (org-element-at-point))))))
  8958. (lines org-context-in-file-links))
  8959. (or string (setq s (concat "*" s))) ; Add * for headlines
  8960. (setq s (replace-regexp-in-string "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" "" s))
  8961. (when (and string (integerp lines) (> lines 0))
  8962. (let ((slines (org-split-string s "\n")))
  8963. (when (< lines (length slines))
  8964. (setq s (mapconcat
  8965. 'identity
  8966. (reverse (nthcdr (- (length slines) lines)
  8967. (reverse slines))) "\n")))))
  8968. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  8969. (defun org-make-link-string (link &optional description)
  8970. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  8971. (unless (org-string-nw-p link) (error "Empty link"))
  8972. (let ((uri (cond ((string-match org-link-types-re link)
  8973. (concat (match-string 1 link)
  8974. (org-link-escape (substring link (match-end 1)))))
  8975. ;; For readability, url-encode internal links only
  8976. ;; when absolutely needed (i.e, when they contain
  8977. ;; square brackets). File links however, are
  8978. ;; encoded since, e.g., spaces are significant.
  8979. ((or (file-name-absolute-p link)
  8980. (org-string-match-p "\\`\\.\\.?/\\|[][]" link))
  8981. (org-link-escape link))
  8982. (t link)))
  8983. (description
  8984. (and (org-string-nw-p description)
  8985. ;; Remove brackets from description, as they are fatal.
  8986. (replace-regexp-in-string
  8987. "[][]" (lambda (m) (if (equal "[" m) "{" "}"))
  8988. (org-trim description)))))
  8989. (format "[[%s]%s]"
  8990. uri
  8991. (if description (format "[%s]" description) ""))))
  8992. (defconst org-link-escape-chars
  8993. ;;%20 %5B %5D %25
  8994. '(?\s ?\[ ?\] ?%)
  8995. "List of characters that should be escaped in a link when stored to Org.
  8996. This is the list that is used for internal purposes.")
  8997. (defconst org-link-escape-chars-browser
  8998. ;;%20 %22
  8999. '(?\s ?\")
  9000. "List of characters to be escaped before handing over to the browser.
  9001. If you consider using this constant then you probably want to use
  9002. the function `org-link-escape-browser' instead. See there why
  9003. this constant is a candidate to be removed once Org drops support
  9004. for Emacs 24.1 and 24.2.")
  9005. (defun org-link-escape (text &optional table merge)
  9006. "Return percent escaped representation of TEXT.
  9007. TEXT is a string with the text to escape.
  9008. Optional argument TABLE is a list with characters that should be
  9009. escaped. When nil, `org-link-escape-chars' is used.
  9010. If optional argument MERGE is set, merge TABLE into
  9011. `org-link-escape-chars'."
  9012. (let ((characters-to-encode
  9013. (cond ((null table) org-link-escape-chars)
  9014. (merge (append org-link-escape-chars table))
  9015. (t table))))
  9016. (mapconcat
  9017. (lambda (c)
  9018. (if (or (memq c characters-to-encode)
  9019. (and org-url-hexify-p (or (< c 32) (> c 126))))
  9020. (mapconcat (lambda (e) (format "%%%.2X" e))
  9021. (or (encode-coding-char c 'utf-8)
  9022. (error "Unable to percent escape character: %c" c))
  9023. "")
  9024. (char-to-string c)))
  9025. text "")))
  9026. (defun org-link-escape-browser (text)
  9027. "Escape some characters before handing over to the browser.
  9028. This function is a candidate to be removed together with the
  9029. constant `org-link-escape-chars-browser' once Org drops support
  9030. for Emacs 24.1 and 24.2. All calls to this function will have to
  9031. be replaced with `url-encode-url' which is available since Emacs
  9032. 24.3.1."
  9033. ;; Example with the Org link
  9034. ;; [[http://lists.gnu.org/archive/cgi-bin/namazu.cgi?idxname=emacs-orgmode&query=%252Bsubject:"Release+8.2"]]
  9035. ;; to open the browser with +subject:"Release 8.2" filled into the
  9036. ;; query field: In this case the variable TEXT contains the
  9037. ;; unescaped [...]=%2Bsubject:"Release+8.2". Then `url-encode-url'
  9038. ;; converts correctly to [...]=%2Bsubject:%22Release+8.2%22 or
  9039. ;; `org-link-escape' with `org-link-escape-chars-browser' converts
  9040. ;; wrongly to [...]=%252Bsubject:%22Release+8.2%22.
  9041. (if (fboundp 'url-encode-url)
  9042. (url-encode-url text)
  9043. (if (org-string-match-p
  9044. (concat "[[:nonascii:]" org-link-escape-chars-browser "]")
  9045. text)
  9046. (org-link-escape text org-link-escape-chars-browser)
  9047. text)))
  9048. (defun org-link-unescape (str)
  9049. "Unhex hexified Unicode parts in string STR.
  9050. E.g. `%C3%B6' becomes the german o-Umlaut. This is the
  9051. reciprocal of `org-link-escape', which see."
  9052. (if (org-string-nw-p str)
  9053. (replace-regexp-in-string
  9054. "\\(%[0-9A-Za-z]\\{2\\}\\)+" #'org-link-unescape-compound str t t)
  9055. str))
  9056. (defun org-link-unescape-compound (hex)
  9057. "Unhexify Unicode hex-chars. E.g. `%C3%B6' is the German o-Umlaut.
  9058. Note: this function also decodes single byte encodings like
  9059. `%E1' (a-acute) if not followed by another `%[A-F0-9]{2}' group."
  9060. (save-match-data
  9061. (let* ((bytes (cdr (split-string hex "%")))
  9062. (ret "")
  9063. (eat 0)
  9064. (sum 0))
  9065. (while bytes
  9066. (let* ((val (string-to-number (pop bytes) 16))
  9067. (shift-xor
  9068. (if (= 0 eat)
  9069. (cond
  9070. ((>= val 252) (cons 6 252))
  9071. ((>= val 248) (cons 5 248))
  9072. ((>= val 240) (cons 4 240))
  9073. ((>= val 224) (cons 3 224))
  9074. ((>= val 192) (cons 2 192))
  9075. (t (cons 0 0)))
  9076. (cons 6 128))))
  9077. (when (>= val 192) (setq eat (car shift-xor)))
  9078. (setq val (logxor val (cdr shift-xor)))
  9079. (setq sum (+ (lsh sum (car shift-xor)) val))
  9080. (when (> eat 0) (setq eat (- eat 1)))
  9081. (cond
  9082. ((= 0 eat) ;multi byte
  9083. (setq ret (concat ret (org-char-to-string sum)))
  9084. (setq sum 0))
  9085. ((not bytes) ; single byte(s)
  9086. (setq ret (org-link-unescape-single-byte-sequence hex))))))
  9087. ret)))
  9088. (defun org-link-unescape-single-byte-sequence (hex)
  9089. "Unhexify hex-encoded single byte character sequences."
  9090. (mapconcat (lambda (byte)
  9091. (char-to-string (string-to-number byte 16)))
  9092. (cdr (split-string hex "%")) ""))
  9093. (defun org-xor (a b)
  9094. "Exclusive or."
  9095. (if a (not b) b))
  9096. (defun org-fixup-message-id-for-http (s)
  9097. "Replace special characters in a message id, so it can be used in an http query."
  9098. (when (string-match "%" s)
  9099. (setq s (mapconcat (lambda (c)
  9100. (if (eq c ?%)
  9101. "%25"
  9102. (char-to-string c)))
  9103. s "")))
  9104. (while (string-match "<" s)
  9105. (setq s (replace-match "%3C" t t s)))
  9106. (while (string-match ">" s)
  9107. (setq s (replace-match "%3E" t t s)))
  9108. (while (string-match "@" s)
  9109. (setq s (replace-match "%40" t t s)))
  9110. s)
  9111. (defun org-link-prettify (link)
  9112. "Return a human-readable representation of LINK.
  9113. The car of LINK must be a raw link.
  9114. The cdr of LINK must be either a link description or nil."
  9115. (let ((desc (or (cadr link) "<no description>")))
  9116. (concat (format "%-45s" (substring desc 0 (min (length desc) 40)))
  9117. "<" (car link) ">")))
  9118. ;;;###autoload
  9119. (defun org-insert-link-global ()
  9120. "Insert a link like Org-mode does.
  9121. This command can be called in any mode to insert a link in Org-mode syntax."
  9122. (interactive)
  9123. (org-load-modules-maybe)
  9124. (org-run-like-in-org-mode 'org-insert-link))
  9125. (defun org-insert-all-links (arg &optional pre post)
  9126. "Insert all links in `org-stored-links'.
  9127. When a universal prefix, do not delete the links from `org-stored-links'.
  9128. When `ARG' is a number, insert the last N link(s).
  9129. `PRE' and `POST' are optional arguments to define a string to
  9130. prepend or to append."
  9131. (interactive "P")
  9132. (let ((org-keep-stored-link-after-insertion (equal arg '(4)))
  9133. (links (copy-seq org-stored-links))
  9134. (pr (or pre "- "))
  9135. (po (or post "\n"))
  9136. (cnt 1) l)
  9137. (if (null org-stored-links)
  9138. (message "No link to insert")
  9139. (while (and (or (listp arg) (>= arg cnt))
  9140. (setq l (if (listp arg)
  9141. (pop links)
  9142. (pop org-stored-links))))
  9143. (setq cnt (1+ cnt))
  9144. (insert pr)
  9145. (org-insert-link nil (car l) (or (cadr l) "<no description>"))
  9146. (insert po)))))
  9147. (defun org-insert-last-stored-link (arg)
  9148. "Insert the last link stored in `org-stored-links'."
  9149. (interactive "p")
  9150. (org-insert-all-links arg "" "\n"))
  9151. (defun org-link-fontify-links-to-this-file ()
  9152. "Fontify links to the current file in `org-stored-links'."
  9153. (let ((f (buffer-file-name)) a b)
  9154. (setq a (mapcar (lambda(l)
  9155. (let ((ll (car l)))
  9156. (when (and (string-match "^file:\\(.+\\)::" ll)
  9157. (equal f (expand-file-name (match-string 1 ll))))
  9158. ll)))
  9159. org-stored-links))
  9160. (when (featurep 'org-id)
  9161. (setq b (mapcar (lambda(l)
  9162. (let ((ll (car l)))
  9163. (when (and (string-match "^id:\\(.+\\)$" ll)
  9164. (equal f (expand-file-name
  9165. (or (org-id-find-id-file
  9166. (match-string 1 ll)) ""))))
  9167. ll)))
  9168. org-stored-links)))
  9169. (mapcar (lambda(l)
  9170. (put-text-property 0 (length l) 'face 'font-lock-comment-face l))
  9171. (delq nil (append a b)))))
  9172. (defvar org-link-links-in-this-file nil)
  9173. (defun org-insert-link (&optional complete-file link-location default-description)
  9174. "Insert a link. At the prompt, enter the link.
  9175. Completion can be used to insert any of the link protocol prefixes like
  9176. http or ftp in use.
  9177. The history can be used to select a link previously stored with
  9178. `org-store-link'. When the empty string is entered (i.e. if you just
  9179. press RET at the prompt), the link defaults to the most recently
  9180. stored link. As SPC triggers completion in the minibuffer, you need to
  9181. use M-SPC or C-q SPC to force the insertion of a space character.
  9182. You will also be prompted for a description, and if one is given, it will
  9183. be displayed in the buffer instead of the link.
  9184. If there is already a link at point, this command will allow you to edit link
  9185. and description parts.
  9186. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  9187. be selected using completion. The path to the file will be relative to the
  9188. current directory if the file is in the current directory or a subdirectory.
  9189. Otherwise, the link will be the absolute path as completed in the minibuffer
  9190. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  9191. option `org-link-file-path-type'.
  9192. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  9193. the current directory or below.
  9194. With three \\[universal-argument] prefixes, negate the meaning of
  9195. `org-keep-stored-link-after-insertion'.
  9196. If `org-make-link-description-function' is non-nil, this function will be
  9197. called with the link target, and the result will be the default
  9198. link description.
  9199. If the LINK-LOCATION parameter is non-nil, this value will be
  9200. used as the link location instead of reading one interactively.
  9201. If the DEFAULT-DESCRIPTION parameter is non-nil, this value will
  9202. be used as the default description."
  9203. (interactive "P")
  9204. (let* ((wcf (current-window-configuration))
  9205. (origbuf (current-buffer))
  9206. (region (when (org-region-active-p)
  9207. (buffer-substring (region-beginning) (region-end))))
  9208. (remove (and region (list (region-beginning) (region-end))))
  9209. (desc region)
  9210. tmphist ; byte-compile incorrectly complains about this
  9211. (link link-location)
  9212. (abbrevs org-link-abbrev-alist-local)
  9213. entry file all-prefixes auto-desc)
  9214. (cond
  9215. (link-location) ; specified by arg, just use it.
  9216. ((org-in-regexp org-bracket-link-regexp 1)
  9217. ;; We do have a link at point, and we are going to edit it.
  9218. (setq remove (list (match-beginning 0) (match-end 0)))
  9219. (setq desc (when (match-end 3) (org-match-string-no-properties 3)))
  9220. (setq link (read-string "Link: "
  9221. (org-link-unescape
  9222. (org-match-string-no-properties 1)))))
  9223. ((or (org-in-regexp org-angle-link-re)
  9224. (org-in-regexp org-plain-link-re))
  9225. ;; Convert to bracket link
  9226. (setq remove (list (match-beginning 0) (match-end 0))
  9227. link (read-string "Link: "
  9228. (org-remove-angle-brackets (match-string 0)))))
  9229. ((member complete-file '((4) (16)))
  9230. ;; Completing read for file names.
  9231. (setq link (org-file-complete-link complete-file)))
  9232. (t
  9233. ;; Read link, with completion for stored links.
  9234. (org-link-fontify-links-to-this-file)
  9235. (org-switch-to-buffer-other-window "*Org Links*")
  9236. (with-current-buffer "*Org Links*"
  9237. (erase-buffer)
  9238. (insert "Insert a link.
  9239. Use TAB to complete link prefixes, then RET for type-specific completion support\n")
  9240. (when org-stored-links
  9241. (insert "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  9242. (insert (mapconcat 'org-link-prettify
  9243. (reverse org-stored-links) "\n")))
  9244. (goto-char (point-min)))
  9245. (let ((cw (selected-window)))
  9246. (select-window (get-buffer-window "*Org Links*" 'visible))
  9247. (with-current-buffer "*Org Links*" (setq truncate-lines t))
  9248. (unless (pos-visible-in-window-p (point-max))
  9249. (org-fit-window-to-buffer))
  9250. (and (window-live-p cw) (select-window cw)))
  9251. ;; Fake a link history, containing the stored links.
  9252. (setq tmphist (append (mapcar 'car org-stored-links)
  9253. org-insert-link-history))
  9254. (setq all-prefixes (append (mapcar 'car abbrevs)
  9255. (mapcar 'car org-link-abbrev-alist)
  9256. org-link-types))
  9257. (unwind-protect
  9258. (progn
  9259. (setq link
  9260. (org-completing-read
  9261. "Link: "
  9262. (append
  9263. (mapcar (lambda (x) (concat x ":"))
  9264. all-prefixes)
  9265. (mapcar 'car org-stored-links))
  9266. nil nil nil
  9267. 'tmphist
  9268. (caar org-stored-links)))
  9269. (unless (string-match "\\S-" link)
  9270. (user-error "No link selected"))
  9271. (mapc (lambda(l)
  9272. (when (equal link (cadr l)) (setq link (car l) auto-desc t)))
  9273. org-stored-links)
  9274. (when (or (member link all-prefixes)
  9275. (and (equal ":" (substring link -1))
  9276. (member (substring link 0 -1) all-prefixes)
  9277. (setq link (substring link 0 -1))))
  9278. (setq link (with-current-buffer origbuf
  9279. (org-link-try-special-completion link)))))
  9280. (set-window-configuration wcf)
  9281. (kill-buffer "*Org Links*"))
  9282. (setq entry (assoc link org-stored-links))
  9283. (or entry (push link org-insert-link-history))
  9284. (setq desc (or desc (nth 1 entry)))))
  9285. (when (funcall (if (equal complete-file '(64)) 'not 'identity)
  9286. (not org-keep-stored-link-after-insertion))
  9287. (setq org-stored-links (delq (assoc link org-stored-links)
  9288. org-stored-links)))
  9289. (when (and (string-match org-plain-link-re link)
  9290. (not (string-match org-ts-regexp link)))
  9291. ;; URL-like link, normalize the use of angular brackets.
  9292. (setq link (org-remove-angle-brackets link)))
  9293. ;; Check if we are linking to the current file with a search
  9294. ;; option If yes, simplify the link by using only the search
  9295. ;; option.
  9296. (when (and buffer-file-name
  9297. (string-match "^file:\\(.+?\\)::\\(.+\\)" link))
  9298. (let* ((path (match-string 1 link))
  9299. (case-fold-search nil)
  9300. (search (match-string 2 link)))
  9301. (save-match-data
  9302. (when (equal (file-truename buffer-file-name) (file-truename path))
  9303. ;; We are linking to this same file, with a search option
  9304. (setq link search)))))
  9305. ;; Check if we can/should use a relative path. If yes, simplify the link
  9306. (when (string-match "^\\(file:\\|docview:\\)\\(.*\\)" link)
  9307. (let* ((type (match-string 1 link))
  9308. (path (match-string 2 link))
  9309. (origpath path)
  9310. (case-fold-search nil))
  9311. (cond
  9312. ((or (eq org-link-file-path-type 'absolute)
  9313. (equal complete-file '(16)))
  9314. (setq path (abbreviate-file-name (expand-file-name path))))
  9315. ((eq org-link-file-path-type 'noabbrev)
  9316. (setq path (expand-file-name path)))
  9317. ((eq org-link-file-path-type 'relative)
  9318. (setq path (file-relative-name path)))
  9319. (t
  9320. (save-match-data
  9321. (if (string-match (concat "^" (regexp-quote
  9322. (expand-file-name
  9323. (file-name-as-directory
  9324. default-directory))))
  9325. (expand-file-name path))
  9326. ;; We are linking a file with relative path name.
  9327. (setq path (substring (expand-file-name path)
  9328. (match-end 0)))
  9329. (setq path (abbreviate-file-name (expand-file-name path)))))))
  9330. (setq link (concat type path))
  9331. (when (equal desc origpath)
  9332. (setq desc path))))
  9333. (if org-make-link-description-function
  9334. (setq desc
  9335. (or (condition-case nil
  9336. (funcall org-make-link-description-function link desc)
  9337. (error (progn (message "Can't get link description from `%s'"
  9338. (symbol-name org-make-link-description-function))
  9339. (sit-for 2) nil)))
  9340. (read-string "Description: " default-description)))
  9341. (if default-description (setq desc default-description)
  9342. (setq desc (or (and auto-desc desc)
  9343. (read-string "Description: " desc)))))
  9344. (unless (string-match "\\S-" desc) (setq desc nil))
  9345. (when remove (apply 'delete-region remove))
  9346. (insert (org-make-link-string link desc))
  9347. ;; Redisplay so as the new link has proper invisible characters.
  9348. (sit-for 0)))
  9349. (defun org-link-try-special-completion (type)
  9350. "If there is completion support for link type TYPE, offer it."
  9351. (let ((fun (intern (concat "org-" type "-complete-link"))))
  9352. (if (functionp fun)
  9353. (funcall fun)
  9354. (read-string "Link (no completion support): " (concat type ":")))))
  9355. (defun org-file-complete-link (&optional arg)
  9356. "Create a file link using completion."
  9357. (let ((file (org-iread-file-name "File: "))
  9358. (pwd (file-name-as-directory (expand-file-name ".")))
  9359. (pwd1 (file-name-as-directory (abbreviate-file-name
  9360. (expand-file-name ".")))))
  9361. (cond ((equal arg '(16))
  9362. (concat "file:"
  9363. (abbreviate-file-name (expand-file-name file))))
  9364. ((string-match
  9365. (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  9366. (concat "file:" (match-string 1 file)))
  9367. ((string-match
  9368. (concat "^" (regexp-quote pwd) "\\(.+\\)")
  9369. (expand-file-name file))
  9370. (concat "file:"
  9371. (match-string 1 (expand-file-name file))))
  9372. (t (concat "file:" file)))))
  9373. (defun org-iread-file-name (&rest args)
  9374. "Read-file-name using `ido-mode' speedup if available.
  9375. ARGS are arguments that may be passed to `ido-read-file-name' or `read-file-name'.
  9376. See `read-file-name' for a description of parameters."
  9377. (org-without-partial-completion
  9378. (if (and org-completion-use-ido
  9379. (fboundp 'ido-read-file-name)
  9380. (org-bound-and-true-p ido-mode)
  9381. (listp (nth 1 args)))
  9382. (let ((ido-enter-matching-directory nil))
  9383. (apply #'ido-read-file-name args))
  9384. (apply #'read-file-name args))))
  9385. (defun org-completing-read (&rest args)
  9386. "Completing-read with SPACE being a normal character."
  9387. (let ((enable-recursive-minibuffers t)
  9388. (minibuffer-local-completion-map
  9389. (copy-keymap minibuffer-local-completion-map)))
  9390. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  9391. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  9392. (org-defkey minibuffer-local-completion-map (kbd "C-c !") 'org-time-stamp-inactive)
  9393. (apply 'org-icompleting-read args)))
  9394. (defun org-completing-read-no-i (&rest args)
  9395. (let (org-completion-use-ido org-completion-use-iswitchb)
  9396. (apply 'org-completing-read args)))
  9397. (defun org-iswitchb-completing-read (prompt choices &rest args)
  9398. "Use iswitch as a completing-read replacement to choose from choices.
  9399. PROMPT is a string to prompt with. CHOICES is a list of strings to choose
  9400. from."
  9401. (let* ((iswitchb-use-virtual-buffers nil)
  9402. (iswitchb-make-buflist-hook
  9403. (lambda ()
  9404. (setq iswitchb-temp-buflist choices))))
  9405. (iswitchb-read-buffer prompt)))
  9406. (defun org-icompleting-read (&rest args)
  9407. "Completing-read using `ido-mode' or `iswitchb' speedups if available.
  9408. Should be called like `completing-read'."
  9409. (org-without-partial-completion
  9410. (if (not (listp (nth 1 args)))
  9411. ;; Ido only supports lists as the COLLECTION argument. Use
  9412. ;; default completion function when second argument is not
  9413. ;; a list.
  9414. (apply #'completing-read args)
  9415. (let ((ido-enter-matching-directory nil))
  9416. (apply (cond ((and org-completion-use-ido
  9417. (fboundp 'ido-completing-read)
  9418. (org-bound-and-true-p ido-mode))
  9419. #'ido-completing-read)
  9420. ((and org-completion-use-iswitchb
  9421. (org-bound-and-true-p iswitchb-mode))
  9422. #'org-iswitchb-completing-read)
  9423. (t #'completing-read))
  9424. args)))))
  9425. ;;; Opening/following a link
  9426. (defvar org-link-search-failed nil)
  9427. (defvar org-open-link-functions nil
  9428. "Hook for functions finding a plain text link.
  9429. These functions must take a single argument, the link content.
  9430. They will be called for links that look like [[link text][description]]
  9431. when LINK TEXT does not have a protocol like \"http:\" and does not look
  9432. like a filename (e.g. \"./blue.png\").
  9433. These functions will be called *before* Org attempts to resolve the
  9434. link by doing text searches in the current buffer - so if you want a
  9435. link \"[[target]]\" to still find \"<<target>>\", your function should
  9436. handle this as a special case.
  9437. When the function does handle the link, it must return a non-nil value.
  9438. If it decides that it is not responsible for this link, it must return
  9439. nil to indicate that that Org-mode can continue with other options
  9440. like exact and fuzzy text search.")
  9441. (defun org-next-link (&optional search-backward)
  9442. "Move forward to the next link.
  9443. If the link is in hidden text, expose it."
  9444. (interactive "P")
  9445. (when (and org-link-search-failed (eq this-command last-command))
  9446. (goto-char (point-min))
  9447. (message "Link search wrapped back to beginning of buffer"))
  9448. (setq org-link-search-failed nil)
  9449. (let* ((pos (point))
  9450. (ct (org-context))
  9451. (a (assoc :link ct))
  9452. (srch-fun (if search-backward 're-search-backward 're-search-forward)))
  9453. (cond (a (goto-char (nth (if search-backward 1 2) a)))
  9454. ((looking-at org-any-link-re)
  9455. ;; Don't stay stuck at link without an org-link face
  9456. (forward-char (if search-backward -1 1))))
  9457. (if (funcall srch-fun org-any-link-re nil t)
  9458. (progn
  9459. (goto-char (match-beginning 0))
  9460. (when (outline-invisible-p) (org-show-context)))
  9461. (goto-char pos)
  9462. (setq org-link-search-failed t)
  9463. (message "No further link found"))))
  9464. (defun org-previous-link ()
  9465. "Move backward to the previous link.
  9466. If the link is in hidden text, expose it."
  9467. (interactive)
  9468. (funcall 'org-next-link t))
  9469. (defun org-translate-link (s)
  9470. "Translate a link string if a translation function has been defined."
  9471. (with-temp-buffer
  9472. (insert (org-trim s))
  9473. (org-trim (org-element-interpret-data (org-element-context)))))
  9474. (defun org-translate-link-from-planner (type path)
  9475. "Translate a link from Emacs Planner syntax so that Org can follow it.
  9476. This is still an experimental function, your mileage may vary."
  9477. (cond
  9478. ((member type '("http" "https" "news" "ftp"))
  9479. ;; standard Internet links are the same.
  9480. nil)
  9481. ((and (equal type "irc") (string-match "^//" path))
  9482. ;; Planner has two / at the beginning of an irc link, we have 1.
  9483. ;; We should have zero, actually....
  9484. (setq path (substring path 1)))
  9485. ((and (equal type "lisp") (string-match "^/" path))
  9486. ;; Planner has a slash, we do not.
  9487. (setq type "elisp" path (substring path 1)))
  9488. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  9489. ;; A typical message link. Planner has the id after the final slash,
  9490. ;; we separate it with a hash mark
  9491. (setq path (concat (match-string 1 path) "#"
  9492. (org-remove-angle-brackets (match-string 2 path))))))
  9493. (cons type path))
  9494. (defun org-find-file-at-mouse (ev)
  9495. "Open file link or URL at mouse."
  9496. (interactive "e")
  9497. (mouse-set-point ev)
  9498. (org-open-at-point 'in-emacs))
  9499. (defun org-open-at-mouse (ev)
  9500. "Open file link or URL at mouse.
  9501. See the docstring of `org-open-file' for details."
  9502. (interactive "e")
  9503. (mouse-set-point ev)
  9504. (when (eq major-mode 'org-agenda-mode)
  9505. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  9506. (org-open-at-point))
  9507. (defvar org-window-config-before-follow-link nil
  9508. "The window configuration before following a link.
  9509. This is saved in case the need arises to restore it.")
  9510. ;;;###autoload
  9511. (defun org-open-at-point-global ()
  9512. "Follow a link like Org-mode does.
  9513. This command can be called in any mode to follow a link that has
  9514. Org-mode syntax."
  9515. (interactive)
  9516. (org-run-like-in-org-mode 'org-open-at-point))
  9517. ;;;###autoload
  9518. (defun org-open-link-from-string (s &optional arg reference-buffer)
  9519. "Open a link in the string S, as if it was in Org-mode."
  9520. (interactive "sLink: \nP")
  9521. (let ((reference-buffer (or reference-buffer (current-buffer))))
  9522. (with-temp-buffer
  9523. (let ((org-inhibit-startup (not reference-buffer)))
  9524. (org-mode)
  9525. (insert s)
  9526. (goto-char (point-min))
  9527. (when reference-buffer
  9528. (setq org-link-abbrev-alist-local
  9529. (with-current-buffer reference-buffer
  9530. org-link-abbrev-alist-local)))
  9531. (org-open-at-point arg reference-buffer)))))
  9532. (defvar org-open-at-point-functions nil
  9533. "Hook that is run when following a link at point.
  9534. Functions in this hook must return t if they identify and follow
  9535. a link at point. If they don't find anything interesting at point,
  9536. they must return nil.")
  9537. (defvar org-link-search-inhibit-query nil) ;; dynamically scoped
  9538. (defvar clean-buffer-list-kill-buffer-names) ; Defined in midnight.el
  9539. (defun org-open-at-point (&optional arg reference-buffer)
  9540. "Open link, timestamp, footnote or tags at point.
  9541. When point is on a link, follow it. Normally, files will be
  9542. opened by an appropriate application. If the optional prefix
  9543. argument ARG is non-nil, Emacs will visit the file. With
  9544. a double prefix argument, try to open outside of Emacs, in the
  9545. application the system uses for this file type.
  9546. When point is on a timestamp, open the agenda at the day
  9547. specified.
  9548. When point is a footnote definition, move to the first reference
  9549. found. If it is on a reference, move to the associated
  9550. definition.
  9551. When point is on a headline, display a list of every link in the
  9552. entry, so it is possible to pick one, or all, of them. If point
  9553. is on a tag, call `org-tags-view' instead.
  9554. When optional argument REFERENCE-BUFFER is non-nil, it should
  9555. specify a buffer from where the link search should happen. This
  9556. is used internally by `org-open-link-from-string'.
  9557. On top of syntactically correct links, this function will open
  9558. the link at point in comments or comment blocks and the first
  9559. link in a property drawer line."
  9560. (interactive "P")
  9561. ;; On a code block, open block's results.
  9562. (unless (call-interactively 'org-babel-open-src-block-result)
  9563. (org-load-modules-maybe)
  9564. (setq org-window-config-before-follow-link (current-window-configuration))
  9565. (org-remove-occur-highlights nil nil t)
  9566. (unless (run-hook-with-args-until-success 'org-open-at-point-functions)
  9567. (let* ((context
  9568. ;; Only consider supported types, even if they are not
  9569. ;; the closest one.
  9570. (org-element-lineage
  9571. (org-element-context)
  9572. '(clock comment comment-block footnote-definition
  9573. footnote-reference headline inlinetask keyword link
  9574. node-property timestamp)
  9575. t))
  9576. (type (org-element-type context))
  9577. (value (org-element-property :value context)))
  9578. (cond
  9579. ((not context) (user-error "No link found"))
  9580. ;; Exception: open timestamps and links in properties
  9581. ;; drawers, keywords and comments.
  9582. ((memq type '(comment comment-block keyword node-property))
  9583. (cond ((org-in-regexp org-any-link-re)
  9584. (org-open-link-from-string (match-string-no-properties 0)))
  9585. ((or (org-in-regexp org-ts-regexp-both nil t)
  9586. (org-in-regexp org-tsr-regexp-both nil t))
  9587. (org-follow-timestamp-link))
  9588. (t (user-error "No link found"))))
  9589. ;; On a headline or an inlinetask, but not on a timestamp,
  9590. ;; a link, a footnote reference or on tags.
  9591. ((and (memq type '(headline inlinetask))
  9592. ;; Not on tags.
  9593. (progn (save-excursion (beginning-of-line)
  9594. (looking-at org-complex-heading-regexp))
  9595. (or (not (match-beginning 5))
  9596. (< (point) (match-beginning 5)))))
  9597. (let* ((data (org-offer-links-in-entry (current-buffer) (point) arg))
  9598. (links (car data))
  9599. (links-end (cdr data)))
  9600. (if links
  9601. (dolist (link (if (stringp links) (list links) links))
  9602. (search-forward link nil links-end)
  9603. (goto-char (match-beginning 0))
  9604. (org-open-at-point))
  9605. (require 'org-attach)
  9606. (org-attach-reveal 'if-exists))))
  9607. ;; On a clock line, make sure point is on the timestamp
  9608. ;; before opening it.
  9609. ((and (eq type 'clock)
  9610. value
  9611. (>= (point) (org-element-property :begin value))
  9612. (<= (point) (org-element-property :end value)))
  9613. (org-follow-timestamp-link))
  9614. ;; Do nothing on white spaces after an object.
  9615. ((>= (point)
  9616. (save-excursion
  9617. (goto-char (org-element-property :end context))
  9618. (skip-chars-backward " \t")
  9619. (point)))
  9620. (user-error "No link found"))
  9621. ((eq type 'timestamp) (org-follow-timestamp-link))
  9622. ;; On tags within a headline or an inlinetask.
  9623. ((and (memq type '(headline inlinetask))
  9624. (progn (save-excursion (beginning-of-line)
  9625. (looking-at org-complex-heading-regexp))
  9626. (and (match-beginning 5)
  9627. (>= (point) (match-beginning 5)))))
  9628. (org-tags-view arg (substring (match-string 5) 0 -1)))
  9629. ((eq type 'link)
  9630. ;; When link is located within the description of another
  9631. ;; link (e.g., an inline image), always open the parent
  9632. ;; link.
  9633. (let*((link (let ((up (org-element-property :parent context)))
  9634. (if (eq (org-element-type up) 'link) up context)))
  9635. (type (org-element-property :type link))
  9636. (path (org-link-unescape (org-element-property :path link))))
  9637. ;; Switch back to REFERENCE-BUFFER needed when called in
  9638. ;; a temporary buffer through `org-open-link-from-string'.
  9639. (with-current-buffer (or reference-buffer (current-buffer))
  9640. (cond
  9641. ((equal type "file")
  9642. (if (string-match "[*?{]" (file-name-nondirectory path))
  9643. (dired path)
  9644. ;; Look into `org-link-protocols' in order to find
  9645. ;; a DEDICATED-FUNCTION to open file. The function
  9646. ;; will be applied on raw link instead of parsed
  9647. ;; link due to the limitation in `org-add-link-type'
  9648. ;; ("open" function called with a single argument).
  9649. ;; If no such function is found, fallback to
  9650. ;; `org-open-file'.
  9651. ;;
  9652. ;; Note : "file+emacs" and "file+sys" types are
  9653. ;; hard-coded in order to escape the previous
  9654. ;; limitation.
  9655. (let* ((option (org-element-property :search-option link))
  9656. (app (org-element-property :application link))
  9657. (dedicated-function
  9658. (nth 1 (assoc app org-link-protocols))))
  9659. (if dedicated-function
  9660. (funcall dedicated-function
  9661. (concat path
  9662. (and option (concat "::" option))))
  9663. (apply #'org-open-file
  9664. path
  9665. (cond (arg)
  9666. ((equal app "emacs") 'emacs)
  9667. ((equal app "sys") 'system))
  9668. (cond ((not option) nil)
  9669. ((org-string-match-p "\\`[0-9]+\\'" option)
  9670. (list (string-to-number option)))
  9671. (t (list nil
  9672. (org-link-unescape option)))))))))
  9673. ((assoc type org-link-protocols)
  9674. (funcall (nth 1 (assoc type org-link-protocols)) path))
  9675. ((equal type "help")
  9676. (let ((f-or-v (intern path)))
  9677. (cond ((fboundp f-or-v) (describe-function f-or-v))
  9678. ((boundp f-or-v) (describe-variable f-or-v))
  9679. (t (error "Not a known function or variable")))))
  9680. ((member type '("http" "https" "ftp" "mailto" "news"))
  9681. (browse-url (org-link-escape-browser (concat type ":" path))))
  9682. ((equal type "doi")
  9683. (browse-url
  9684. (org-link-escape-browser (concat org-doi-server-url path))))
  9685. ((equal type "message") (browse-url (concat type ":" path)))
  9686. ((equal type "shell")
  9687. (let ((buf (generate-new-buffer "*Org Shell Output*"))
  9688. (cmd path))
  9689. (if (or (and (org-string-nw-p
  9690. org-confirm-shell-link-not-regexp)
  9691. (string-match
  9692. org-confirm-shell-link-not-regexp cmd))
  9693. (not org-confirm-shell-link-function)
  9694. (funcall org-confirm-shell-link-function
  9695. (format "Execute \"%s\" in shell? "
  9696. (org-add-props cmd nil
  9697. 'face 'org-warning))))
  9698. (progn
  9699. (message "Executing %s" cmd)
  9700. (shell-command cmd buf)
  9701. (when (featurep 'midnight)
  9702. (setq clean-buffer-list-kill-buffer-names
  9703. (cons (buffer-name buf)
  9704. clean-buffer-list-kill-buffer-names))))
  9705. (user-error "Abort"))))
  9706. ((equal type "elisp")
  9707. (let ((cmd path))
  9708. (if (or (and (org-string-nw-p
  9709. org-confirm-elisp-link-not-regexp)
  9710. (org-string-match-p
  9711. org-confirm-elisp-link-not-regexp cmd))
  9712. (not org-confirm-elisp-link-function)
  9713. (funcall org-confirm-elisp-link-function
  9714. (format "Execute \"%s\" as elisp? "
  9715. (org-add-props cmd nil
  9716. 'face 'org-warning))))
  9717. (message "%s => %s" cmd
  9718. (if (eq (string-to-char cmd) ?\()
  9719. (eval (read cmd))
  9720. (call-interactively (read cmd))))
  9721. (user-error "Abort"))))
  9722. ((equal type "id")
  9723. (require 'org-id)
  9724. (funcall (nth 1 (assoc "id" org-link-protocols)) path))
  9725. ((member type '("coderef" "custom-id" "fuzzy" "radio"))
  9726. (unless (run-hook-with-args-until-success
  9727. 'org-open-link-functions path)
  9728. (if (not arg) (org-mark-ring-push)
  9729. (switch-to-buffer-other-window
  9730. (org-get-buffer-for-internal-link (current-buffer))))
  9731. (let ((destination
  9732. (org-with-wide-buffer
  9733. (if (equal type "radio")
  9734. (org-search-radio-target
  9735. (org-element-property :path link))
  9736. (org-link-search
  9737. (if (member type '("custom-id" "coderef"))
  9738. (org-element-property :raw-link link)
  9739. path)
  9740. ;; Prevent fuzzy links from matching
  9741. ;; themselves.
  9742. (and (equal type "fuzzy")
  9743. (+ 2 (org-element-property :begin link)))))
  9744. (point))))
  9745. (unless (and (<= (point-min) destination)
  9746. (>= (point-max) destination))
  9747. (widen))
  9748. (goto-char destination))))
  9749. (t (browse-url-at-point))))))
  9750. ;; On a footnote reference or at a footnote definition's label.
  9751. ((or (eq type 'footnote-reference)
  9752. (and (eq type 'footnote-definition)
  9753. (save-excursion
  9754. ;; Do not validate action when point is on the
  9755. ;; spaces right after the footnote label, in
  9756. ;; order to be on par with behaviour on links.
  9757. (skip-chars-forward " \t")
  9758. (let ((begin
  9759. (org-element-property :contents-begin context)))
  9760. (if begin (< (point) begin)
  9761. (= (org-element-property :post-affiliated context)
  9762. (line-beginning-position)))))))
  9763. (org-footnote-action))
  9764. (t (user-error "No link found")))))
  9765. (run-hook-with-args 'org-follow-link-hook)))
  9766. (defun org-offer-links-in-entry (buffer marker &optional nth zero)
  9767. "Offer links in the current entry and return the selected link.
  9768. If there is only one link, return it.
  9769. If NTH is an integer, return the NTH link found.
  9770. If ZERO is a string, check also this string for a link, and if
  9771. there is one, return it."
  9772. (with-current-buffer buffer
  9773. (save-excursion
  9774. (save-restriction
  9775. (widen)
  9776. (goto-char marker)
  9777. (let ((cnt ?0)
  9778. (in-emacs (unless (integerp nth) nth))
  9779. have-zero end links link c)
  9780. (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
  9781. (push (match-string 0 zero) links)
  9782. (setq cnt (1- cnt) have-zero t))
  9783. (save-excursion
  9784. (org-back-to-heading t)
  9785. (setq end (save-excursion (outline-next-heading) (point)))
  9786. (while (re-search-forward org-any-link-re end t)
  9787. (push (match-string 0) links))
  9788. (setq links (org-uniquify (reverse links))))
  9789. (cond
  9790. ((null links)
  9791. (message "No links"))
  9792. ((equal (length links) 1)
  9793. (setq link (car links)))
  9794. ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
  9795. (setq link (nth (if have-zero nth (1- nth)) links)))
  9796. (t ; we have to select a link
  9797. (save-excursion
  9798. (save-window-excursion
  9799. (delete-other-windows)
  9800. (with-output-to-temp-buffer "*Select Link*"
  9801. (mapc (lambda (l)
  9802. (if (not (string-match org-bracket-link-regexp l))
  9803. (princ (format "[%c] %s\n" (incf cnt)
  9804. (org-remove-angle-brackets l)))
  9805. (if (match-end 3)
  9806. (princ (format "[%c] %s (%s)\n" (incf cnt)
  9807. (match-string 3 l) (match-string 1 l)))
  9808. (princ (format "[%c] %s\n" (incf cnt)
  9809. (match-string 1 l))))))
  9810. links))
  9811. (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
  9812. (message "Select link to open, RET to open all:")
  9813. (setq c (read-char-exclusive))
  9814. (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
  9815. (when (equal c ?q) (user-error "Abort"))
  9816. (if (equal c ?\C-m)
  9817. (setq link links)
  9818. (setq nth (- c ?0))
  9819. (when have-zero (setq nth (1+ nth)))
  9820. (unless (and (integerp nth) (>= (length links) nth))
  9821. (user-error "Invalid link selection"))
  9822. (setq link (nth (1- nth) links)))))
  9823. (cons link end))))))
  9824. ;; TODO: These functions are deprecated since `org-open-at-point'
  9825. ;; hard-codes behaviour for "file+emacs" and "file+sys" types.
  9826. (defun org-open-file-with-system (path)
  9827. "Open file at PATH using the system way of opening it."
  9828. (org-open-file path 'system))
  9829. (defun org-open-file-with-emacs (path)
  9830. "Open file at PATH in Emacs."
  9831. (org-open-file path 'emacs))
  9832. ;;; File search
  9833. (defvar org-create-file-search-functions nil
  9834. "List of functions to construct the right search string for a file link.
  9835. These functions are called in turn with point at the location to
  9836. which the link should point.
  9837. A function in the hook should first test if it would like to
  9838. handle this file type, for example by checking the `major-mode'
  9839. or the file extension. If it decides not to handle this file, it
  9840. should just return nil to give other functions a chance. If it
  9841. does handle the file, it must return the search string to be used
  9842. when following the link. The search string will be part of the
  9843. file link, given after a double colon, and `org-open-at-point'
  9844. will automatically search for it. If special measures must be
  9845. taken to make the search successful, another function should be
  9846. added to the companion hook `org-execute-file-search-functions',
  9847. which see.
  9848. A function in this hook may also use `setq' to set the variable
  9849. `description' to provide a suggestion for the descriptive text to
  9850. be used for this link when it gets inserted into an Org-mode
  9851. buffer with \\[org-insert-link].")
  9852. (defvar org-execute-file-search-functions nil
  9853. "List of functions to execute a file search triggered by a link.
  9854. Functions added to this hook must accept a single argument, the
  9855. search string that was part of the file link, the part after the
  9856. double colon. The function must first check if it would like to
  9857. handle this search, for example by checking the `major-mode' or
  9858. the file extension. If it decides not to handle this search, it
  9859. should just return nil to give other functions a chance. If it
  9860. does handle the search, it must return a non-nil value to keep
  9861. other functions from trying.
  9862. Each function can access the current prefix argument through the
  9863. variable `current-prefix-arg'. Note that a single prefix is used
  9864. to force opening a link in Emacs, so it may be good to only use a
  9865. numeric or double prefix to guide the search function.
  9866. In case this is needed, a function in this hook can also restore
  9867. the window configuration before `org-open-at-point' was called using:
  9868. (set-window-configuration org-window-config-before-follow-link)")
  9869. (defun org-search-radio-target (target)
  9870. "Search a radio target matching TARGET in current buffer.
  9871. White spaces are not significant."
  9872. (let ((re (format "<<<%s>>>"
  9873. (mapconcat #'regexp-quote
  9874. (org-split-string target "[ \t\n]+")
  9875. "[ \t]+\\(?:\n[ \t]*\\)?")))
  9876. (origin (point)))
  9877. (goto-char (point-min))
  9878. (catch :radio-match
  9879. (while (re-search-forward re nil t)
  9880. (backward-char)
  9881. (let ((object (org-element-context)))
  9882. (when (eq (org-element-type object) 'radio-target)
  9883. (goto-char (org-element-property :begin object))
  9884. (org-show-context 'link-search)
  9885. (throw :radio-match nil))))
  9886. (goto-char origin)
  9887. (user-error "No match for radio target: %s" target))))
  9888. (defun org-link-search (s &optional avoid-pos stealth)
  9889. "Search for a search string S.
  9890. If S starts with \"#\", it triggers a custom ID search.
  9891. If S is enclosed within parenthesis, it initiates a coderef
  9892. search.
  9893. If S is surrounded by forward slashes, it is interpreted as
  9894. a regular expression. In Org mode files, this will create an
  9895. `org-occur' sparse tree. In ordinary files, `occur' will be used
  9896. to list matches. If the current buffer is in `dired-mode', grep
  9897. will be used to search in all files.
  9898. When AVOID-POS is given, ignore matches near that position.
  9899. When optional argument STEALTH is non-nil, do not modify
  9900. visibility around point, thus ignoring `org-show-context-detail'
  9901. variable.
  9902. Search is case-insensitive and ignores white spaces. Return type
  9903. of matched result, with is either `dedicated' or `fuzzy'."
  9904. (unless (org-string-nw-p s) (error "Invalid search string \"%s\"" s))
  9905. (let* ((case-fold-search t)
  9906. (origin (point))
  9907. (normalized (replace-regexp-in-string "\n[ \t]*" " " s))
  9908. (words (org-split-string s "[ \t\n]+"))
  9909. (s-multi-re (mapconcat #'regexp-quote words "[ \t]+\\(?:\n[ \t]*\\)?"))
  9910. (s-single-re (mapconcat #'regexp-quote words "[ \t]+"))
  9911. type)
  9912. (cond
  9913. ;; Check if there are any special search functions.
  9914. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  9915. ((eq (string-to-char s) ?#)
  9916. ;; Look for a custom ID S if S starts with "#".
  9917. (let* ((id (substring normalized 1))
  9918. (match (org-find-property "CUSTOM_ID" id)))
  9919. (if match (progn (goto-char match) (setf type 'dedicated))
  9920. (error "No match for custom ID: %s" id))))
  9921. ((string-match "\\`(\\(.*\\))\\'" normalized)
  9922. ;; Look for coderef targets if S is enclosed within parenthesis.
  9923. (let ((coderef (match-string-no-properties 1 normalized))
  9924. (re (substring s-single-re 1 -1)))
  9925. (goto-char (point-min))
  9926. (catch :coderef-match
  9927. (while (re-search-forward re nil t)
  9928. (let ((element (org-element-at-point)))
  9929. (when (and (memq (org-element-type element)
  9930. '(example-block src-block))
  9931. ;; Build proper regexp according to current
  9932. ;; block's label format.
  9933. (let ((label-fmt
  9934. (regexp-quote
  9935. (or (org-element-property :label-fmt element)
  9936. org-coderef-label-format))))
  9937. (save-excursion
  9938. (beginning-of-line)
  9939. (looking-at (format ".*?\\(%s\\)[ \t]*$"
  9940. (format label-fmt coderef))))))
  9941. (setq type 'dedicated)
  9942. (goto-char (match-beginning 1))
  9943. (throw :coderef-match nil))))
  9944. (goto-char origin)
  9945. (error "No match for coderef: %s" coderef))))
  9946. ((string-match "\\`/\\(.*\\)/\\'" normalized)
  9947. ;; Look for a regular expression.
  9948. (funcall (if (derived-mode-p 'org-mode) #'org-occur #'org-do-occur)
  9949. (match-string 1 s)))
  9950. ;; Fuzzy links.
  9951. (t
  9952. (let* ((starred (eq (string-to-char normalized) ?*))
  9953. (headline-search (and (derived-mode-p 'org-mode)
  9954. (or org-link-search-must-match-exact-headline
  9955. starred))))
  9956. (cond
  9957. ;; Look for targets, only if not in a headline search.
  9958. ((and (not starred)
  9959. (let ((target (format "<<%s>>" s-multi-re)))
  9960. (catch :target-match
  9961. (goto-char (point-min))
  9962. (while (re-search-forward target nil t)
  9963. (backward-char)
  9964. (let ((context (org-element-context)))
  9965. (when (eq (org-element-type context) 'target)
  9966. (setq type 'dedicated)
  9967. (goto-char (org-element-property :begin context))
  9968. (throw :target-match t))))
  9969. nil))))
  9970. ;; Look for elements named after S, only if not in a headline
  9971. ;; search.
  9972. ((and (not starred)
  9973. (let ((name (format "^[ \t]*#\\+NAME: +%s[ \t]*$" s-single-re)))
  9974. (catch :name-match
  9975. (goto-char (point-min))
  9976. (while (re-search-forward name nil t)
  9977. (let ((element (org-element-at-point)))
  9978. (when (equal (org-split-string
  9979. (org-element-property :name element)
  9980. "[ \t]+")
  9981. words)
  9982. (setq type 'dedicated)
  9983. (beginning-of-line)
  9984. (throw :name-match t))))
  9985. nil))))
  9986. ;; Regular text search. Prefer headlines in Org mode
  9987. ;; buffers.
  9988. ((and (derived-mode-p 'org-mode)
  9989. (let* ((wspace "[ \t]")
  9990. (wspaceopt (concat wspace "*"))
  9991. (cookie (concat "\\(?:"
  9992. wspaceopt
  9993. "\\[[0-9]*\\(?:%\\|/[0-9]*\\)\\]"
  9994. wspaceopt
  9995. "\\)"))
  9996. (sep (concat "\\(?:\\(?:" wspace "\\|" cookie "\\)+\\)"))
  9997. (re (concat
  9998. org-outline-regexp-bol
  9999. "\\(?:" org-todo-regexp "[ \t]+\\)?"
  10000. "\\(?:\\[#.\\][ \t]+\\)?"
  10001. "\\(?:" org-comment-string "[ \t]+\\)?"
  10002. sep "?"
  10003. (let ((title (mapconcat #'regexp-quote
  10004. words
  10005. sep)))
  10006. (if starred (substring title 1) title))
  10007. sep "?"
  10008. (org-re "\\(?:[ \t]+:[[:alnum:]_@#%%:]+:\\)?")
  10009. "[ \t]*$")))
  10010. (goto-char (point-min))
  10011. (re-search-forward re nil t)))
  10012. (goto-char (match-beginning 0))
  10013. (setq type 'dedicated))
  10014. ;; Offer to create non-existent headline depending on
  10015. ;; `org-link-search-must-match-exact-headline'.
  10016. ((and (derived-mode-p 'org-mode)
  10017. (not org-link-search-inhibit-query)
  10018. (eq org-link-search-must-match-exact-headline 'query-to-create)
  10019. (yes-or-no-p "No match - create this as a new heading? "))
  10020. (goto-char (point-max))
  10021. (unless (bolp) (newline))
  10022. (org-insert-heading nil t t)
  10023. (insert s "\n")
  10024. (beginning-of-line 0))
  10025. ;; Only headlines are looked after. No need to process
  10026. ;; further: throw an error.
  10027. ((and (derived-mode-p 'org-mode)
  10028. (or starred org-link-search-must-match-exact-headline))
  10029. (goto-char origin)
  10030. (error "No match for fuzzy expression: %s" normalized))
  10031. ;; Regular text search.
  10032. ((catch :fuzzy-match
  10033. (goto-char (point-min))
  10034. (while (re-search-forward s-multi-re nil t)
  10035. ;; Skip match if it contains AVOID-POS or it is included
  10036. ;; in a link with a description but outside the
  10037. ;; description.
  10038. (unless (or (and avoid-pos
  10039. (<= (match-beginning 0) avoid-pos)
  10040. (> (match-end 0) avoid-pos))
  10041. (and (save-match-data
  10042. (org-in-regexp org-bracket-link-regexp))
  10043. (match-beginning 3)
  10044. (or (> (match-beginning 3) (point))
  10045. (<= (match-end 3) (point)))
  10046. (org-element-lineage
  10047. (save-match-data (org-element-context))
  10048. '(link) t)))
  10049. (goto-char (match-beginning 0))
  10050. (setq type 'fuzzy)
  10051. (throw :fuzzy-match t)))
  10052. nil))
  10053. ;; All failed. Throw an error.
  10054. (t (goto-char origin)
  10055. (error "No match for fuzzy expression: %s" normalized))))))
  10056. ;; Disclose surroundings of match, if appropriate.
  10057. (when (and (derived-mode-p 'org-mode) (not stealth))
  10058. (org-show-context 'link-search))
  10059. type))
  10060. (defun org-get-buffer-for-internal-link (buffer)
  10061. "Return a buffer to be used for displaying the link target of internal links."
  10062. (cond
  10063. ((not org-display-internal-link-with-indirect-buffer)
  10064. buffer)
  10065. ((string-match "(Clone)$" (buffer-name buffer))
  10066. (message "Buffer is already a clone, not making another one")
  10067. ;; we also do not modify visibility in this case
  10068. buffer)
  10069. (t ; make a new indirect buffer for displaying the link
  10070. (let* ((bn (buffer-name buffer))
  10071. (ibn (concat bn "(Clone)"))
  10072. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  10073. (with-current-buffer ib (org-overview))
  10074. ib))))
  10075. (defun org-do-occur (regexp &optional cleanup)
  10076. "Call the Emacs command `occur'.
  10077. If CLEANUP is non-nil, remove the printout of the regular expression
  10078. in the *Occur* buffer. This is useful if the regex is long and not useful
  10079. to read."
  10080. (occur regexp)
  10081. (when cleanup
  10082. (let ((cwin (selected-window)) win beg end)
  10083. (when (setq win (get-buffer-window "*Occur*"))
  10084. (select-window win))
  10085. (goto-char (point-min))
  10086. (when (re-search-forward "match[a-z]+" nil t)
  10087. (setq beg (match-end 0))
  10088. (when (re-search-forward "^[ \t]*[0-9]+" nil t)
  10089. (setq end (1- (match-beginning 0)))))
  10090. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  10091. (goto-char (point-min))
  10092. (select-window cwin))))
  10093. ;;; The mark ring for links jumps
  10094. (defvar org-mark-ring nil
  10095. "Mark ring for positions before jumps in Org-mode.")
  10096. (defvar org-mark-ring-last-goto nil
  10097. "Last position in the mark ring used to go back.")
  10098. ;; Fill and close the ring
  10099. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  10100. (loop for i from 1 to org-mark-ring-length do
  10101. (push (make-marker) org-mark-ring))
  10102. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  10103. org-mark-ring)
  10104. (defun org-mark-ring-push (&optional pos buffer)
  10105. "Put the current position or POS into the mark ring and rotate it."
  10106. (interactive)
  10107. (setq pos (or pos (point)))
  10108. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  10109. (move-marker (car org-mark-ring)
  10110. (or pos (point))
  10111. (or buffer (current-buffer)))
  10112. (message "%s"
  10113. (substitute-command-keys
  10114. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  10115. (defun org-mark-ring-goto (&optional n)
  10116. "Jump to the previous position in the mark ring.
  10117. With prefix arg N, jump back that many stored positions. When
  10118. called several times in succession, walk through the entire ring.
  10119. Org-mode commands jumping to a different position in the current file,
  10120. or to another Org-mode file, automatically push the old position
  10121. onto the ring."
  10122. (interactive "p")
  10123. (let (p m)
  10124. (if (eq last-command this-command)
  10125. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  10126. (setq p org-mark-ring))
  10127. (setq org-mark-ring-last-goto p)
  10128. (setq m (car p))
  10129. (org-pop-to-buffer-same-window (marker-buffer m))
  10130. (goto-char m)
  10131. (when (or (outline-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  10132. ;;; TODO: Use string-remove-prefix and -suffix once we only support
  10133. ;;; Emacs 24.4+
  10134. (defun org-remove-angle-brackets (s)
  10135. (when (equal (substring s 0 1) "<") (setq s (substring s 1)))
  10136. (when (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  10137. s)
  10138. (defun org-add-angle-brackets (s)
  10139. (unless (equal (substring s 0 1) "<") (setq s (concat "<" s)))
  10140. (unless (equal (substring s -1) ">") (setq s (concat s ">")))
  10141. s)
  10142. (defun org-remove-double-quotes (s)
  10143. (when (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  10144. (when (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  10145. s)
  10146. ;;; Following specific links
  10147. (defun org-follow-timestamp-link ()
  10148. "Open an agenda view for the time-stamp date/range at point."
  10149. (cond
  10150. ((org-at-date-range-p t)
  10151. (let ((org-agenda-start-on-weekday)
  10152. (t1 (match-string 1))
  10153. (t2 (match-string 2)) tt1 tt2)
  10154. (setq tt1 (time-to-days (org-time-string-to-time t1))
  10155. tt2 (time-to-days (org-time-string-to-time t2)))
  10156. (let ((org-agenda-buffer-tmp-name
  10157. (format "*Org Agenda(a:%s)"
  10158. (concat (substring t1 0 10) "--" (substring t2 0 10)))))
  10159. (org-agenda-list nil tt1 (1+ (- tt2 tt1))))))
  10160. ((org-at-timestamp-p t)
  10161. (let ((org-agenda-buffer-tmp-name
  10162. (format "*Org Agenda(a:%s)" (substring (match-string 1) 0 10))))
  10163. (org-agenda-list nil (time-to-days (org-time-string-to-time
  10164. (substring (match-string 1) 0 10)))
  10165. 1)))
  10166. (t (error "This should not happen"))))
  10167. ;;; Following file links
  10168. (declare-function mailcap-parse-mailcaps "mailcap" (&optional path force))
  10169. (declare-function mailcap-extension-to-mime "mailcap" (extn))
  10170. (declare-function mailcap-mime-info
  10171. "mailcap" (string &optional request no-decode))
  10172. (defvar org-wait nil)
  10173. (defun org-open-file (path &optional in-emacs line search)
  10174. "Open the file at PATH.
  10175. First, this expands any special file name abbreviations. Then the
  10176. configuration variable `org-file-apps' is checked if it contains an
  10177. entry for this file type, and if yes, the corresponding command is launched.
  10178. If no application is found, Emacs simply visits the file.
  10179. With optional prefix argument IN-EMACS, Emacs will visit the file.
  10180. With a double \\[universal-argument] \\[universal-argument] \
  10181. prefix arg, Org tries to avoid opening in Emacs
  10182. and to use an external application to visit the file.
  10183. Optional LINE specifies a line to go to, optional SEARCH a string
  10184. to search for. If LINE or SEARCH is given, the file will be
  10185. opened in Emacs, unless an entry from org-file-apps that makes
  10186. use of groups in a regexp matches.
  10187. If you want to change the way frames are used when following a
  10188. link, please customize `org-link-frame-setup'.
  10189. If the file does not exist, an error is thrown."
  10190. (let* ((file (if (equal path "")
  10191. buffer-file-name
  10192. (substitute-in-file-name (expand-file-name path))))
  10193. (file-apps (append org-file-apps (org-default-apps)))
  10194. (apps (cl-remove-if
  10195. 'org-file-apps-entry-match-against-dlink-p file-apps))
  10196. (apps-dlink (cl-remove-if-not
  10197. 'org-file-apps-entry-match-against-dlink-p file-apps))
  10198. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  10199. (dirp (unless remp (file-directory-p file)))
  10200. (file (if (and dirp org-open-directory-means-index-dot-org)
  10201. (concat (file-name-as-directory file) "index.org")
  10202. file))
  10203. (a-m-a-p (assq 'auto-mode apps))
  10204. (dfile (downcase file))
  10205. ;; reconstruct the original file: link from the PATH, LINE and SEARCH args
  10206. (link (cond ((and (eq line nil)
  10207. (eq search nil))
  10208. file)
  10209. (line
  10210. (concat file "::" (number-to-string line)))
  10211. (search
  10212. (concat file "::" search))))
  10213. (dlink (downcase link))
  10214. (old-buffer (current-buffer))
  10215. (old-pos (point))
  10216. (old-mode major-mode)
  10217. ext cmd link-match-data)
  10218. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  10219. (setq ext (match-string 1 dfile))
  10220. (when (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  10221. (setq ext (match-string 1 dfile))))
  10222. (cond
  10223. ((member in-emacs '((16) system))
  10224. (setq cmd (cdr (assoc 'system apps))))
  10225. (in-emacs (setq cmd 'emacs))
  10226. (t
  10227. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  10228. (and dirp (cdr (assoc 'directory apps)))
  10229. ; first, try matching against apps-dlink
  10230. ; if we get a match here, store the match data for later
  10231. (let ((match (assoc-default dlink apps-dlink
  10232. 'string-match)))
  10233. (if match
  10234. (progn (setq link-match-data (match-data))
  10235. match)
  10236. (progn (setq in-emacs (or in-emacs line search))
  10237. nil))) ; if we have no match in apps-dlink,
  10238. ; always open the file in emacs if line or search
  10239. ; is given (for backwards compatibility)
  10240. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  10241. 'string-match)
  10242. (cdr (assoc ext apps))
  10243. (cdr (assoc t apps))))))
  10244. (when (eq cmd 'system)
  10245. (setq cmd (cdr (assoc 'system apps))))
  10246. (when (eq cmd 'default)
  10247. (setq cmd (cdr (assoc t apps))))
  10248. (when (eq cmd 'mailcap)
  10249. (require 'mailcap)
  10250. (mailcap-parse-mailcaps)
  10251. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  10252. (command (mailcap-mime-info mime-type)))
  10253. (if (stringp command)
  10254. (setq cmd command)
  10255. (setq cmd 'emacs))))
  10256. (when (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  10257. (not (file-exists-p file))
  10258. (not org-open-non-existing-files))
  10259. (user-error "No such file: %s" file))
  10260. (cond
  10261. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  10262. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  10263. (while (string-match "['\"]%s['\"]" cmd)
  10264. (setq cmd (replace-match "%s" t t cmd)))
  10265. (while (string-match "%s" cmd)
  10266. (setq cmd (replace-match
  10267. (save-match-data
  10268. (shell-quote-argument
  10269. (convert-standard-filename file)))
  10270. t t cmd)))
  10271. ;; Replace "%1", "%2" etc. in command with group matches from regex
  10272. (save-match-data
  10273. (let ((match-index 1)
  10274. (number-of-groups (- (/ (length link-match-data) 2) 1)))
  10275. (set-match-data link-match-data)
  10276. (while (<= match-index number-of-groups)
  10277. (let ((regex (concat "%" (number-to-string match-index)))
  10278. (replace-with (match-string match-index dlink)))
  10279. (while (string-match regex cmd)
  10280. (setq cmd (replace-match replace-with t t cmd))))
  10281. (setq match-index (+ match-index 1)))))
  10282. (save-window-excursion
  10283. (message "Running %s...done" cmd)
  10284. (start-process-shell-command cmd nil cmd)
  10285. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))))
  10286. ((or (stringp cmd)
  10287. (eq cmd 'emacs))
  10288. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  10289. (widen)
  10290. (if line (progn (org-goto-line line)
  10291. (when (derived-mode-p 'org-mode)
  10292. (org-reveal)))
  10293. (when search (org-link-search search))))
  10294. ((consp cmd)
  10295. (let ((file (convert-standard-filename file)))
  10296. (save-match-data
  10297. (set-match-data link-match-data)
  10298. (eval cmd))))
  10299. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  10300. (and (derived-mode-p 'org-mode) (eq old-mode 'org-mode)
  10301. (or (not (equal old-buffer (current-buffer)))
  10302. (not (equal old-pos (point))))
  10303. (org-mark-ring-push old-pos old-buffer))))
  10304. (defun org-file-apps-entry-match-against-dlink-p (entry)
  10305. "This function returns non-nil if `entry' uses a regular
  10306. expression which should be matched against the whole link by
  10307. org-open-file.
  10308. It assumes that is the case when the entry uses a regular
  10309. expression which has at least one grouping construct and the
  10310. action is either a lisp form or a command string containing
  10311. `%1', i.e. using at least one subexpression match as a
  10312. parameter."
  10313. (let ((selector (car entry))
  10314. (action (cdr entry)))
  10315. (if (stringp selector)
  10316. (and (> (regexp-opt-depth selector) 0)
  10317. (or (and (stringp action)
  10318. (string-match "%[0-9]" action))
  10319. (consp action)))
  10320. nil)))
  10321. (defun org-default-apps ()
  10322. "Return the default applications for this operating system."
  10323. (cond
  10324. ((eq system-type 'darwin)
  10325. org-file-apps-defaults-macosx)
  10326. ((eq system-type 'windows-nt)
  10327. org-file-apps-defaults-windowsnt)
  10328. (t org-file-apps-defaults-gnu)))
  10329. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  10330. "Convert extensions to regular expressions in the cars of LIST.
  10331. Also, weed out any non-string entries, because the return value is used
  10332. only for regexp matching.
  10333. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  10334. point to the symbol `emacs', indicating that the file should
  10335. be opened in Emacs."
  10336. (append
  10337. (delq nil
  10338. (mapcar (lambda (x)
  10339. (unless (not (stringp (car x)))
  10340. (if (string-match "\\W" (car x))
  10341. x
  10342. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  10343. list))
  10344. (when add-auto-mode
  10345. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  10346. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  10347. (defun org-file-remote-p (file)
  10348. "Test whether FILE specifies a location on a remote system.
  10349. Return non-nil if the location is indeed remote.
  10350. For example, the filename \"/user@host:/foo\" specifies a location
  10351. on the system \"/user@host:\"."
  10352. (cond ((fboundp 'file-remote-p)
  10353. (file-remote-p file))
  10354. ((fboundp 'tramp-handle-file-remote-p)
  10355. (tramp-handle-file-remote-p file))
  10356. ((and (boundp 'ange-ftp-name-format)
  10357. (string-match (car ange-ftp-name-format) file))
  10358. t)))
  10359. ;;;; Refiling
  10360. (defun org-get-org-file ()
  10361. "Read a filename, with default directory `org-directory'."
  10362. (let ((default (or org-default-notes-file remember-data-file)))
  10363. (read-file-name (format "File name [%s]: " default)
  10364. (file-name-as-directory org-directory)
  10365. default)))
  10366. (defun org-notes-order-reversed-p ()
  10367. "Check if the current file should receive notes in reversed order."
  10368. (cond
  10369. ((not org-reverse-note-order) nil)
  10370. ((eq t org-reverse-note-order) t)
  10371. ((not (listp org-reverse-note-order)) nil)
  10372. (t (catch 'exit
  10373. (dolist (entry org-reverse-note-order)
  10374. (when (string-match (car entry) buffer-file-name)
  10375. (throw 'exit (cdr entry))))))))
  10376. (defvar org-refile-target-table nil
  10377. "The list of refile targets, created by `org-refile'.")
  10378. (defvar org-agenda-new-buffers nil
  10379. "Buffers created to visit agenda files.")
  10380. (defvar org-refile-cache nil
  10381. "Cache for refile targets.")
  10382. (defvar org-refile-markers nil
  10383. "All the markers used for caching refile locations.")
  10384. (defun org-refile-marker (pos)
  10385. "Get a new refile marker, but only if caching is in use."
  10386. (if (not org-refile-use-cache)
  10387. pos
  10388. (let ((m (make-marker)))
  10389. (move-marker m pos)
  10390. (push m org-refile-markers)
  10391. m)))
  10392. (defun org-refile-cache-clear ()
  10393. "Clear the refile cache and disable all the markers."
  10394. (mapc (lambda (m) (move-marker m nil)) org-refile-markers)
  10395. (setq org-refile-markers nil)
  10396. (setq org-refile-cache nil)
  10397. (message "Refile cache has been cleared"))
  10398. (defun org-refile-cache-check-set (set)
  10399. "Check if all the markers in the cache still have live buffers."
  10400. (let (marker)
  10401. (catch 'exit
  10402. (while (and set (setq marker (nth 3 (pop set))))
  10403. ;; If `org-refile-use-outline-path' is 'file, marker may be nil
  10404. (when (and marker (null (marker-buffer marker)))
  10405. (message "Please regenerate the refile cache with `C-0 C-c C-w'")
  10406. (sit-for 3)
  10407. (throw 'exit nil)))
  10408. t)))
  10409. (defun org-refile-cache-put (set &rest identifiers)
  10410. "Push the refile targets SET into the cache, under IDENTIFIERS."
  10411. (let* ((key (sha1 (prin1-to-string identifiers)))
  10412. (entry (assoc key org-refile-cache)))
  10413. (if entry
  10414. (setcdr entry set)
  10415. (push (cons key set) org-refile-cache))))
  10416. (defun org-refile-cache-get (&rest identifiers)
  10417. "Retrieve the cached value for refile targets given by IDENTIFIERS."
  10418. (cond
  10419. ((not org-refile-cache) nil)
  10420. ((not org-refile-use-cache) (org-refile-cache-clear) nil)
  10421. (t
  10422. (let ((set (cdr (assoc (sha1 (prin1-to-string identifiers))
  10423. org-refile-cache))))
  10424. (and set (org-refile-cache-check-set set) set)))))
  10425. (defun org-refile-get-targets (&optional default-buffer excluded-entries)
  10426. "Produce a table with refile targets."
  10427. (let ((case-fold-search nil)
  10428. ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
  10429. (entries (or org-refile-targets '((nil . (:level . 1)))))
  10430. targets tgs txt re files desc descre fast-path-p level pos0)
  10431. (message "Getting targets...")
  10432. (with-current-buffer (or default-buffer (current-buffer))
  10433. (dolist (entry entries)
  10434. (setq files (car entry) desc (cdr entry))
  10435. (setq fast-path-p nil)
  10436. (cond
  10437. ((null files) (setq files (list (current-buffer))))
  10438. ((eq files 'org-agenda-files)
  10439. (setq files (org-agenda-files 'unrestricted)))
  10440. ((and (symbolp files) (fboundp files))
  10441. (setq files (funcall files)))
  10442. ((and (symbolp files) (boundp files))
  10443. (setq files (symbol-value files))))
  10444. (when (stringp files) (setq files (list files)))
  10445. (cond
  10446. ((eq (car desc) :tag)
  10447. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  10448. ((eq (car desc) :todo)
  10449. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  10450. ((eq (car desc) :regexp)
  10451. (setq descre (cdr desc)))
  10452. ((eq (car desc) :level)
  10453. (setq descre (concat "^\\*\\{" (number-to-string
  10454. (if org-odd-levels-only
  10455. (1- (* 2 (cdr desc)))
  10456. (cdr desc)))
  10457. "\\}[ \t]")))
  10458. ((eq (car desc) :maxlevel)
  10459. (setq fast-path-p t)
  10460. (setq descre (concat "^\\*\\{1," (number-to-string
  10461. (if org-odd-levels-only
  10462. (1- (* 2 (cdr desc)))
  10463. (cdr desc)))
  10464. "\\}[ \t]")))
  10465. (t (error "Bad refiling target description %s" desc)))
  10466. (dolist (f files)
  10467. (with-current-buffer
  10468. (if (bufferp f) f (org-get-agenda-file-buffer f))
  10469. (or
  10470. (setq tgs (org-refile-cache-get (buffer-file-name) descre))
  10471. (progn
  10472. (when (bufferp f) (setq f (buffer-file-name
  10473. (buffer-base-buffer f))))
  10474. (setq f (and f (expand-file-name f)))
  10475. (when (eq org-refile-use-outline-path 'file)
  10476. (push (list (file-name-nondirectory f) f nil nil) tgs))
  10477. (save-excursion
  10478. (save-restriction
  10479. (widen)
  10480. (goto-char (point-min))
  10481. (while (re-search-forward descre nil t)
  10482. (goto-char (setq pos0 (point-at-bol)))
  10483. (catch 'next
  10484. (when org-refile-target-verify-function
  10485. (save-match-data
  10486. (or (funcall org-refile-target-verify-function)
  10487. (throw 'next t))))
  10488. (when (and (looking-at org-complex-heading-regexp)
  10489. (not (member (match-string 4) excluded-entries))
  10490. (match-string 4))
  10491. (setq level (org-reduced-level
  10492. (- (match-end 1) (match-beginning 1)))
  10493. txt (org-link-display-format (match-string 4))
  10494. txt (replace-regexp-in-string "\\( *[[0-9]+/?[0-9]*%?]\\)+$" "" txt)
  10495. re (format org-complex-heading-regexp-format
  10496. (regexp-quote (match-string 4))))
  10497. (when org-refile-use-outline-path
  10498. (setq txt (mapconcat
  10499. 'org-protect-slash
  10500. (append
  10501. (if (eq org-refile-use-outline-path
  10502. 'file)
  10503. (list (file-name-nondirectory
  10504. (buffer-file-name
  10505. (buffer-base-buffer))))
  10506. (when (eq org-refile-use-outline-path
  10507. 'full-file-path)
  10508. (list (buffer-file-name
  10509. (buffer-base-buffer)))))
  10510. (org-get-outline-path fast-path-p
  10511. level txt)
  10512. (list txt))
  10513. "/")))
  10514. (push (list txt f re (org-refile-marker (point)))
  10515. tgs)))
  10516. (when (= (point) pos0)
  10517. ;; verification function has not moved point
  10518. (goto-char (point-at-eol))))))))
  10519. (when org-refile-use-cache
  10520. (org-refile-cache-put tgs (buffer-file-name) descre))
  10521. (setq targets (append tgs targets))))))
  10522. (message "Getting targets...done")
  10523. (nreverse targets)))
  10524. (defun org-protect-slash (s)
  10525. (while (string-match "/" s)
  10526. (setq s (replace-match "\\" t t s)))
  10527. s)
  10528. (defvar org-olpa (make-vector 20 nil))
  10529. (defun org-get-outline-path (&optional fastp level heading)
  10530. "Return the outline path to the current entry, as a list.
  10531. The parameters FASTP, LEVEL, and HEADING are for use by a scanner
  10532. routine which makes outline path derivations for an entire file,
  10533. avoiding backtracing. Refile target collection makes use of that."
  10534. (if fastp
  10535. (progn
  10536. (when (> level 19)
  10537. (error "Outline path failure, more than 19 levels"))
  10538. (loop for i from level upto 19 do
  10539. (aset org-olpa i nil))
  10540. (prog1
  10541. (delq nil (append org-olpa nil))
  10542. (aset org-olpa level heading)))
  10543. (let (rtn case-fold-search)
  10544. (save-excursion
  10545. (save-restriction
  10546. (widen)
  10547. (while (org-up-heading-safe)
  10548. (when (looking-at org-complex-heading-regexp)
  10549. (push (org-trim
  10550. (replace-regexp-in-string
  10551. ;; Remove statistical/checkboxes cookies
  10552. "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" ""
  10553. (org-match-string-no-properties 4)))
  10554. rtn)))
  10555. rtn)))))
  10556. (defun org-format-outline-path (path &optional width prefix separator)
  10557. "Format the outline path PATH for display.
  10558. WIDTH is the maximum number of characters that is available.
  10559. PREFIX is a prefix to be included in the returned string,
  10560. such as the file name.
  10561. SEPARATOR is inserted between the different parts of the path,
  10562. the default is \"/\"."
  10563. (setq width (or width 79))
  10564. (setq path (delq nil path))
  10565. (unless (> width 0)
  10566. (user-error "Argument `width' must be positive"))
  10567. (setq separator (or separator "/"))
  10568. (let* ((org-odd-levels-only nil)
  10569. (fpath (concat
  10570. prefix (and prefix path separator)
  10571. (mapconcat
  10572. (lambda (s) (replace-regexp-in-string "[ \t]+\\'" "" s))
  10573. (loop for head in path
  10574. for n from 0
  10575. collect (org-add-props
  10576. head nil 'face
  10577. (nth (% n org-n-level-faces) org-level-faces)))
  10578. separator))))
  10579. (when (> (length fpath) width)
  10580. (if (< width 7)
  10581. ;; It's unlikely that `width' will be this small, but don't
  10582. ;; waste characters by adding ".." if it is.
  10583. (setq fpath (substring fpath 0 width))
  10584. (setf (substring fpath (- width 2)) "..")))
  10585. fpath))
  10586. (defun org-display-outline-path (&optional file current separator just-return-string)
  10587. "Display the current outline path in the echo area.
  10588. If FILE is non-nil, prepend the output with the file name.
  10589. If CURRENT is non-nil, append the current heading to the output.
  10590. SEPARATOR is passed through to `org-format-outline-path'. It separates
  10591. the different parts of the path and defaults to \"/\".
  10592. If JUST-RETURN-STRING is non-nil, return a string, don't display a message."
  10593. (interactive "P")
  10594. (let* (case-fold-search
  10595. (bfn (buffer-file-name (buffer-base-buffer)))
  10596. (path (and (derived-mode-p 'org-mode) (org-get-outline-path)))
  10597. res)
  10598. (when current (setq path (append path
  10599. (save-excursion
  10600. (org-back-to-heading t)
  10601. (when (looking-at org-complex-heading-regexp)
  10602. (list (match-string 4)))))))
  10603. (setq res
  10604. (org-format-outline-path
  10605. path
  10606. (1- (frame-width))
  10607. (and file bfn (concat (file-name-nondirectory bfn) separator))
  10608. separator))
  10609. (if just-return-string
  10610. (org-no-properties res)
  10611. (org-unlogged-message "%s" res))))
  10612. (defvar org-refile-history nil
  10613. "History for refiling operations.")
  10614. (defvar org-after-refile-insert-hook nil
  10615. "Hook run after `org-refile' has inserted its stuff at the new location.
  10616. Note that this is still *before* the stuff will be removed from
  10617. the *old* location.")
  10618. (defvar org-capture-last-stored-marker)
  10619. (defvar org-refile-keep nil
  10620. "Non-nil means `org-refile' will copy instead of refile.")
  10621. (defun org-copy ()
  10622. "Like `org-refile', but copy."
  10623. (interactive)
  10624. (let ((org-refile-keep t))
  10625. (funcall 'org-refile nil nil nil "Copy")))
  10626. (defun org-refile (&optional arg default-buffer rfloc msg)
  10627. "Move the entry or entries at point to another heading.
  10628. The list of target headings is compiled using the information in
  10629. `org-refile-targets', which see.
  10630. At the target location, the entry is filed as a subitem of the
  10631. target heading. Depending on `org-reverse-note-order', the new
  10632. subitem will either be the first or the last subitem.
  10633. If there is an active region, all entries in that region will be
  10634. refiled. However, the region must fulfill the requirement that
  10635. the first heading sets the top-level of the moved text.
  10636. With prefix arg ARG, the command will only visit the target
  10637. location and not actually move anything.
  10638. With a double prefix arg \\[universal-argument] \\[universal-argument], go to the location where the last
  10639. refiling operation has put the subtree.
  10640. With a numeric prefix argument of `2', refile to the running clock.
  10641. With a numeric prefix argument of `3', emulate `org-refile-keep'
  10642. being set to t and copy to the target location, don't move it.
  10643. Beware that keeping refiled entries may result in duplicated ID
  10644. properties.
  10645. RFLOC can be a refile location obtained in a different way.
  10646. MSG is a string to replace \"Refile\" in the default prompt with
  10647. another verb. E.g. `org-copy' sets this parameter to \"Copy\".
  10648. See also `org-refile-use-outline-path' and `org-completion-use-ido'.
  10649. If you are using target caching (see `org-refile-use-cache'), you
  10650. have to clear the target cache in order to find new targets.
  10651. This can be done with a 0 prefix (`C-0 C-c C-w') or a triple
  10652. prefix argument (`C-u C-u C-u C-c C-w')."
  10653. (interactive "P")
  10654. (if (member arg '(0 (64)))
  10655. (org-refile-cache-clear)
  10656. (let* ((actionmsg (cond (msg msg)
  10657. ((equal arg 3) "Refile (and keep)")
  10658. (t "Refile")))
  10659. (cbuf (current-buffer))
  10660. (regionp (org-region-active-p))
  10661. (region-start (and regionp (region-beginning)))
  10662. (region-end (and regionp (region-end)))
  10663. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  10664. (org-refile-keep (if (equal arg 3) t org-refile-keep))
  10665. pos it nbuf file re level reversed)
  10666. (setq last-command nil)
  10667. (when regionp
  10668. (goto-char region-start)
  10669. (or (bolp) (goto-char (point-at-bol)))
  10670. (setq region-start (point))
  10671. (unless (or (org-kill-is-subtree-p
  10672. (buffer-substring region-start region-end))
  10673. (prog1 org-refile-active-region-within-subtree
  10674. (let ((s (point-at-eol)))
  10675. (org-toggle-heading)
  10676. (setq region-end (+ (- (point-at-eol) s) region-end)))))
  10677. (user-error "The region is not a (sequence of) subtree(s)")))
  10678. (if (equal arg '(16))
  10679. (org-refile-goto-last-stored)
  10680. (when (or
  10681. (and (equal arg 2)
  10682. org-clock-hd-marker (marker-buffer org-clock-hd-marker)
  10683. (prog1
  10684. (setq it (list (or org-clock-heading "running clock")
  10685. (buffer-file-name
  10686. (marker-buffer org-clock-hd-marker))
  10687. ""
  10688. (marker-position org-clock-hd-marker)))
  10689. (setq arg nil)))
  10690. (setq it (or rfloc
  10691. (let (heading-text)
  10692. (save-excursion
  10693. (unless (and arg (listp arg))
  10694. (org-back-to-heading t)
  10695. (setq heading-text
  10696. (replace-regexp-in-string
  10697. org-bracket-link-regexp
  10698. "\\3"
  10699. (nth 4 (org-heading-components)))))
  10700. (org-refile-get-location
  10701. (cond ((and arg (listp arg)) "Goto")
  10702. (regionp (concat actionmsg " region to"))
  10703. (t (concat actionmsg " subtree \""
  10704. heading-text "\" to")))
  10705. default-buffer
  10706. (and (not (equal '(4) arg))
  10707. org-refile-allow-creating-parent-nodes)
  10708. arg))))))
  10709. (setq file (nth 1 it)
  10710. re (nth 2 it)
  10711. pos (nth 3 it))
  10712. (when (and (not arg)
  10713. pos
  10714. (equal (buffer-file-name) file)
  10715. (if regionp
  10716. (and (>= pos region-start)
  10717. (<= pos region-end))
  10718. (and (>= pos (point))
  10719. (< pos (save-excursion
  10720. (org-end-of-subtree t t))))))
  10721. (error "Cannot refile to position inside the tree or region"))
  10722. (setq nbuf (or (find-buffer-visiting file)
  10723. (find-file-noselect file)))
  10724. (if (and arg (not (equal arg 3)))
  10725. (progn
  10726. (org-pop-to-buffer-same-window nbuf)
  10727. (goto-char pos)
  10728. (org-show-context 'org-goto))
  10729. (if regionp
  10730. (progn
  10731. (org-kill-new (buffer-substring region-start region-end))
  10732. (org-save-markers-in-region region-start region-end))
  10733. (org-copy-subtree 1 nil t))
  10734. (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
  10735. (find-file-noselect file)))
  10736. (setq reversed (org-notes-order-reversed-p))
  10737. (save-excursion
  10738. (save-restriction
  10739. (widen)
  10740. (if pos
  10741. (progn
  10742. (goto-char pos)
  10743. (looking-at org-outline-regexp)
  10744. (setq level (org-get-valid-level (funcall outline-level) 1))
  10745. (goto-char
  10746. (if reversed
  10747. (or (outline-next-heading) (point-max))
  10748. (or (save-excursion (org-get-next-sibling))
  10749. (org-end-of-subtree t t)
  10750. (point-max)))))
  10751. (setq level 1)
  10752. (if (not reversed)
  10753. (goto-char (point-max))
  10754. (goto-char (point-min))
  10755. (or (outline-next-heading) (goto-char (point-max)))))
  10756. (unless (bolp) (newline))
  10757. (org-paste-subtree level nil nil t)
  10758. (when org-log-refile
  10759. (org-add-log-setup 'refile nil nil 'findpos org-log-refile)
  10760. (unless (eq org-log-refile 'note)
  10761. (save-excursion (org-add-log-note))))
  10762. (and org-auto-align-tags
  10763. (let ((org-loop-over-headlines-in-active-region nil))
  10764. (org-set-tags nil t)))
  10765. (let ((bookmark-name (plist-get org-bookmark-names-plist
  10766. :last-refile)))
  10767. (when bookmark-name
  10768. (with-demoted-errors
  10769. (bookmark-set bookmark-name))))
  10770. ;; If we are refiling for capture, make sure that the
  10771. ;; last-capture pointers point here
  10772. (when (org-bound-and-true-p org-capture-is-refiling)
  10773. (let ((bookmark-name (plist-get org-bookmark-names-plist
  10774. :last-capture-marker)))
  10775. (when bookmark-name
  10776. (with-demoted-errors
  10777. (bookmark-set bookmark-name))))
  10778. (move-marker org-capture-last-stored-marker (point)))
  10779. (when (fboundp 'deactivate-mark) (deactivate-mark))
  10780. (run-hooks 'org-after-refile-insert-hook))))
  10781. (unless org-refile-keep
  10782. (if regionp
  10783. (delete-region (point) (+ (point) (- region-end region-start)))
  10784. (delete-region
  10785. (and (org-back-to-heading t) (point))
  10786. (min (1+ (buffer-size)) (org-end-of-subtree t t) (point)))))
  10787. (when (featurep 'org-inlinetask)
  10788. (org-inlinetask-remove-END-maybe))
  10789. (setq org-markers-to-move nil)
  10790. (message (concat actionmsg " to \"%s\" in file %s: done") (car it) file)))))))
  10791. (defun org-refile-goto-last-stored ()
  10792. "Go to the location where the last refile was stored."
  10793. (interactive)
  10794. (bookmark-jump (plist-get org-bookmark-names-plist :last-refile))
  10795. (message "This is the location of the last refile"))
  10796. (defun org-refile--get-location (refloc tbl)
  10797. "When user refile to REFLOC, find the associated target in TBL.
  10798. Also check `org-refile-target-table'."
  10799. (car (delq
  10800. nil
  10801. (mapcar
  10802. (lambda (r) (or (assoc r tbl)
  10803. (assoc r org-refile-target-table)))
  10804. (list (replace-regexp-in-string "/$" "" refloc)
  10805. (replace-regexp-in-string "\\([^/]\\)$" "\\1/" refloc))))))
  10806. (defun org-refile-get-location (&optional prompt default-buffer new-nodes
  10807. no-exclude)
  10808. "Prompt the user for a refile location, using PROMPT.
  10809. PROMPT should not be suffixed with a colon and a space, because
  10810. this function appends the default value from
  10811. `org-refile-history' automatically, if that is not empty.
  10812. When NO-EXCLUDE is set, do not exclude headlines in the current subtree,
  10813. this is used for the GOTO interface."
  10814. (let ((org-refile-targets org-refile-targets)
  10815. (org-refile-use-outline-path org-refile-use-outline-path)
  10816. excluded-entries)
  10817. (when (and (derived-mode-p 'org-mode)
  10818. (not org-refile-use-cache)
  10819. (not no-exclude))
  10820. (org-map-tree
  10821. (lambda()
  10822. (setq excluded-entries
  10823. (append excluded-entries (list (org-get-heading t t)))))))
  10824. (setq org-refile-target-table
  10825. (org-refile-get-targets default-buffer excluded-entries)))
  10826. (unless org-refile-target-table
  10827. (user-error "No refile targets"))
  10828. (let* ((cbuf (current-buffer))
  10829. (partial-completion-mode nil)
  10830. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  10831. (cfunc (if (and org-refile-use-outline-path
  10832. org-outline-path-complete-in-steps)
  10833. 'org-olpath-completing-read
  10834. 'org-icompleting-read))
  10835. (extra (if org-refile-use-outline-path "/" ""))
  10836. (cbnex (concat (buffer-name) extra))
  10837. (filename (and cfn (expand-file-name cfn)))
  10838. (tbl (mapcar
  10839. (lambda (x)
  10840. (if (and (not (member org-refile-use-outline-path
  10841. '(file full-file-path)))
  10842. (not (equal filename (nth 1 x))))
  10843. (cons (concat (car x) extra " ("
  10844. (file-name-nondirectory (nth 1 x)) ")")
  10845. (cdr x))
  10846. (cons (concat (car x) extra) (cdr x))))
  10847. org-refile-target-table))
  10848. (completion-ignore-case t)
  10849. cdef
  10850. (prompt (concat prompt
  10851. (or (and (car org-refile-history)
  10852. (concat " (default " (car org-refile-history) ")"))
  10853. (and (assoc cbnex tbl) (setq cdef cbnex)
  10854. (concat " (default " cbnex ")"))) ": "))
  10855. pa answ parent-target child parent old-hist)
  10856. (setq old-hist org-refile-history)
  10857. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  10858. nil 'org-refile-history (or cdef (car org-refile-history))))
  10859. (if (setq pa (org-refile--get-location answ tbl))
  10860. (progn
  10861. (org-refile-check-position pa)
  10862. (when (or (not org-refile-history)
  10863. (not (eq old-hist org-refile-history))
  10864. (not (equal (car pa) (car org-refile-history))))
  10865. (setq org-refile-history
  10866. (cons (car pa) (if (assoc (car org-refile-history) tbl)
  10867. org-refile-history
  10868. (cdr org-refile-history))))
  10869. (when (equal (car org-refile-history) (nth 1 org-refile-history))
  10870. (pop org-refile-history)))
  10871. pa)
  10872. (if (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  10873. (progn
  10874. (setq parent (match-string 1 answ)
  10875. child (match-string 2 answ))
  10876. (setq parent-target (org-refile--get-location parent tbl))
  10877. (when (and parent-target
  10878. (or (eq new-nodes t)
  10879. (and (eq new-nodes 'confirm)
  10880. (y-or-n-p (format "Create new node \"%s\"? "
  10881. child)))))
  10882. (org-refile-new-child parent-target child)))
  10883. (user-error "Invalid target location")))))
  10884. (declare-function org-string-nw-p "org-macs" (s))
  10885. (defun org-refile-check-position (refile-pointer)
  10886. "Check if the refile pointer matches the headline to which it points."
  10887. (let* ((file (nth 1 refile-pointer))
  10888. (re (nth 2 refile-pointer))
  10889. (pos (nth 3 refile-pointer))
  10890. buffer)
  10891. (if (and (not (markerp pos)) (not file))
  10892. (user-error "Please indicate a target file in the refile path")
  10893. (when (org-string-nw-p re)
  10894. (setq buffer (if (markerp pos)
  10895. (marker-buffer pos)
  10896. (or (find-buffer-visiting file)
  10897. (find-file-noselect file))))
  10898. (with-current-buffer buffer
  10899. (save-excursion
  10900. (save-restriction
  10901. (widen)
  10902. (goto-char pos)
  10903. (beginning-of-line 1)
  10904. (unless (org-looking-at-p re)
  10905. (user-error "Invalid refile position, please clear the cache with `C-0 C-c C-w' before refiling")))))))))
  10906. (defun org-refile-new-child (parent-target child)
  10907. "Use refile target PARENT-TARGET to add new CHILD below it."
  10908. (unless parent-target
  10909. (error "Cannot find parent for new node"))
  10910. (let ((file (nth 1 parent-target))
  10911. (pos (nth 3 parent-target))
  10912. level)
  10913. (with-current-buffer (or (find-buffer-visiting file)
  10914. (find-file-noselect file))
  10915. (save-excursion
  10916. (save-restriction
  10917. (widen)
  10918. (if pos
  10919. (goto-char pos)
  10920. (goto-char (point-max))
  10921. (unless (bolp) (newline)))
  10922. (when (looking-at org-outline-regexp)
  10923. (setq level (funcall outline-level))
  10924. (org-end-of-subtree t t))
  10925. (org-back-over-empty-lines)
  10926. (insert "\n" (make-string
  10927. (if pos (org-get-valid-level level 1) 1) ?*)
  10928. " " child "\n")
  10929. (beginning-of-line 0)
  10930. (list (concat (car parent-target) "/" child) file "" (point)))))))
  10931. (defun org-olpath-completing-read (prompt collection &rest args)
  10932. "Read an outline path like a file name."
  10933. (let ((thetable collection)
  10934. (org-completion-use-ido nil) ; does not work with ido.
  10935. (org-completion-use-iswitchb nil)) ; or iswitchb
  10936. (apply #'org-icompleting-read
  10937. prompt
  10938. (lambda (string predicate &optional flag)
  10939. (cond
  10940. ((eq flag nil) (try-completion string thetable))
  10941. ((eq flag t)
  10942. (let ((l (length string)))
  10943. (mapcar (lambda (x)
  10944. (let ((r (substring x l))
  10945. (f (if (string-match " ([^)]*)$" x)
  10946. (match-string 0 x)
  10947. "")))
  10948. (if (string-match "/" r)
  10949. (concat string (substring r 0 (match-end 0)) f)
  10950. x)))
  10951. (all-completions string thetable predicate))))
  10952. ;; Exact match?
  10953. ((eq flag 'lambda) (assoc string thetable))))
  10954. args)))
  10955. ;;;; Dynamic blocks
  10956. (defun org-find-dblock (name)
  10957. "Find the first dynamic block with name NAME in the buffer.
  10958. If not found, stay at current position and return nil."
  10959. (let ((case-fold-search t) pos)
  10960. (save-excursion
  10961. (goto-char (point-min))
  10962. (setq pos (and (re-search-forward
  10963. (concat "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+" name "\\>") nil t)
  10964. (match-beginning 0))))
  10965. (when pos (goto-char pos))
  10966. pos))
  10967. (defun org-create-dblock (plist)
  10968. "Create a dynamic block section, with parameters taken from PLIST.
  10969. PLIST must contain a :name entry which is used as the name of the block."
  10970. (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
  10971. (end-of-line 1)
  10972. (newline))
  10973. (let ((col (current-column))
  10974. (name (plist-get plist :name)))
  10975. (insert "#+BEGIN: " name)
  10976. (while plist
  10977. (if (eq (car plist) :name)
  10978. (setq plist (cddr plist))
  10979. (insert " " (prin1-to-string (pop plist)))))
  10980. (insert "\n\n" (make-string col ?\ ) "#+END:\n")
  10981. (beginning-of-line -2)))
  10982. (defun org-prepare-dblock ()
  10983. "Prepare dynamic block for refresh.
  10984. This empties the block, puts the cursor at the insert position and returns
  10985. the property list including an extra property :name with the block name."
  10986. (unless (looking-at org-dblock-start-re)
  10987. (user-error "Not at a dynamic block"))
  10988. (let* ((begdel (1+ (match-end 0)))
  10989. (name (org-no-properties (match-string 1)))
  10990. (params (append (list :name name)
  10991. (read (concat "(" (match-string 3) ")")))))
  10992. (save-excursion
  10993. (beginning-of-line 1)
  10994. (skip-chars-forward " \t")
  10995. (setq params (plist-put params :indentation-column (current-column))))
  10996. (unless (re-search-forward org-dblock-end-re nil t)
  10997. (error "Dynamic block not terminated"))
  10998. (setq params
  10999. (append params
  11000. (list :content (buffer-substring
  11001. begdel (match-beginning 0)))))
  11002. (delete-region begdel (match-beginning 0))
  11003. (goto-char begdel)
  11004. (open-line 1)
  11005. params))
  11006. (defun org-map-dblocks (&optional command)
  11007. "Apply COMMAND to all dynamic blocks in the current buffer.
  11008. If COMMAND is not given, use `org-update-dblock'."
  11009. (let ((cmd (or command 'org-update-dblock)))
  11010. (save-excursion
  11011. (goto-char (point-min))
  11012. (while (re-search-forward org-dblock-start-re nil t)
  11013. (goto-char (match-beginning 0))
  11014. (save-excursion
  11015. (condition-case nil
  11016. (funcall cmd)
  11017. (error (message "Error during update of dynamic block"))))
  11018. (unless (re-search-forward org-dblock-end-re nil t)
  11019. (error "Dynamic block not terminated"))))))
  11020. (defun org-dblock-update (&optional arg)
  11021. "User command for updating dynamic blocks.
  11022. Update the dynamic block at point. With prefix ARG, update all dynamic
  11023. blocks in the buffer."
  11024. (interactive "P")
  11025. (if arg
  11026. (org-update-all-dblocks)
  11027. (or (looking-at org-dblock-start-re)
  11028. (org-beginning-of-dblock))
  11029. (org-update-dblock)))
  11030. (defun org-update-dblock ()
  11031. "Update the dynamic block at point.
  11032. This means to empty the block, parse for parameters and then call
  11033. the correct writing function."
  11034. (interactive)
  11035. (save-excursion
  11036. (let* ((win (selected-window))
  11037. (pos (point))
  11038. (line (org-current-line))
  11039. (params (org-prepare-dblock))
  11040. (name (plist-get params :name))
  11041. (indent (plist-get params :indentation-column))
  11042. (cmd (intern (concat "org-dblock-write:" name))))
  11043. (message "Updating dynamic block `%s' at line %d..." name line)
  11044. (funcall cmd params)
  11045. (message "Updating dynamic block `%s' at line %d...done" name line)
  11046. (goto-char pos)
  11047. (when (and indent (> indent 0))
  11048. (setq indent (make-string indent ?\ ))
  11049. (save-excursion
  11050. (select-window win)
  11051. (org-beginning-of-dblock)
  11052. (forward-line 1)
  11053. (while (not (looking-at org-dblock-end-re))
  11054. (insert indent)
  11055. (beginning-of-line 2))
  11056. (when (looking-at org-dblock-end-re)
  11057. (and (looking-at "[ \t]+")
  11058. (replace-match ""))
  11059. (insert indent)))))))
  11060. (defun org-beginning-of-dblock ()
  11061. "Find the beginning of the dynamic block at point.
  11062. Error if there is no such block at point."
  11063. (let ((pos (point))
  11064. beg)
  11065. (end-of-line 1)
  11066. (if (and (re-search-backward org-dblock-start-re nil t)
  11067. (setq beg (match-beginning 0))
  11068. (re-search-forward org-dblock-end-re nil t)
  11069. (> (match-end 0) pos))
  11070. (goto-char beg)
  11071. (goto-char pos)
  11072. (error "Not in a dynamic block"))))
  11073. (defun org-update-all-dblocks ()
  11074. "Update all dynamic blocks in the buffer.
  11075. This function can be used in a hook."
  11076. (interactive)
  11077. (when (derived-mode-p 'org-mode)
  11078. (org-map-dblocks 'org-update-dblock)))
  11079. ;;;; Completion
  11080. (declare-function org-export-backend-name "org-export" (cl-x))
  11081. (declare-function org-export-backend-options "org-export" (cl-x))
  11082. (defun org-get-export-keywords ()
  11083. "Return a list of all currently understood export keywords.
  11084. Export keywords include options, block names, attributes and
  11085. keywords relative to each registered export back-end."
  11086. (let (keywords)
  11087. (dolist (backend
  11088. (org-bound-and-true-p org-export-registered-backends)
  11089. (delq nil keywords))
  11090. ;; Back-end name (for keywords, like #+LATEX:)
  11091. (push (upcase (symbol-name (org-export-backend-name backend))) keywords)
  11092. (dolist (option-entry (org-export-backend-options backend))
  11093. ;; Back-end options.
  11094. (push (nth 1 option-entry) keywords)))))
  11095. (defconst org-options-keywords
  11096. '("ARCHIVE:" "AUTHOR:" "BIND:" "CATEGORY:" "COLUMNS:" "CREATOR:" "DATE:"
  11097. "DESCRIPTION:" "DRAWERS:" "EMAIL:" "EXCLUDE_TAGS:" "FILETAGS:" "INCLUDE:"
  11098. "INDEX:" "KEYWORDS:" "LANGUAGE:" "MACRO:" "OPTIONS:" "PROPERTY:"
  11099. "PRIORITIES:" "SELECT_TAGS:" "SEQ_TODO:" "SETUPFILE:" "STARTUP:" "TAGS:"
  11100. "TITLE:" "TODO:" "TYP_TODO:" "SELECT_TAGS:" "EXCLUDE_TAGS:"))
  11101. (defcustom org-structure-template-alist
  11102. '(("s" "#+BEGIN_SRC ?\n\n#+END_SRC")
  11103. ("e" "#+BEGIN_EXAMPLE\n?\n#+END_EXAMPLE")
  11104. ("q" "#+BEGIN_QUOTE\n?\n#+END_QUOTE")
  11105. ("v" "#+BEGIN_VERSE\n?\n#+END_VERSE")
  11106. ("V" "#+BEGIN_VERBATIM\n?\n#+END_VERBATIM")
  11107. ("c" "#+BEGIN_CENTER\n?\n#+END_CENTER")
  11108. ("l" "#+BEGIN_LaTeX\n?\n#+END_LaTeX")
  11109. ("L" "#+LaTeX: ")
  11110. ("h" "#+BEGIN_HTML\n?\n#+END_HTML")
  11111. ("H" "#+HTML: ")
  11112. ("a" "#+BEGIN_ASCII\n?\n#+END_ASCII")
  11113. ("A" "#+ASCII: ")
  11114. ("i" "#+INDEX: ?")
  11115. ("I" "#+INCLUDE: %file ?"))
  11116. "Structure completion elements.
  11117. This is a list of abbreviation keys and values. The value gets inserted
  11118. if you type `<' followed by the key and then press the completion key,
  11119. usually `TAB'. %file will be replaced by a file name after prompting
  11120. for the file using completion. The cursor will be placed at the position
  11121. of the `?' in the template.
  11122. There are two templates for each key, the first uses the original Org syntax,
  11123. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  11124. the default when the /org-mtags.el/ module has been loaded. See also the
  11125. variable `org-mtags-prefer-muse-templates'."
  11126. :group 'org-completion
  11127. :type '(repeat
  11128. (list
  11129. (string :tag "Key")
  11130. (string :tag "Template")))
  11131. :version "25.1"
  11132. :package-version '(Org . "8.3"))
  11133. (defun org-try-structure-completion ()
  11134. "Try to complete a structure template before point.
  11135. This looks for strings like \"<e\" on an otherwise empty line and
  11136. expands them."
  11137. (let ((l (buffer-substring (point-at-bol) (point)))
  11138. a)
  11139. (when (and (looking-at "[ \t]*$")
  11140. (string-match "^[ \t]*<\\([a-zA-Z]+\\)$" l)
  11141. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  11142. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  11143. (match-beginning 1)) a)
  11144. t)))
  11145. (defun org-complete-expand-structure-template (start cell)
  11146. "Expand a structure template."
  11147. (let ((rpl (nth 1 cell))
  11148. (ind ""))
  11149. (delete-region start (point))
  11150. (when (string-match "\\`[ \t]*#\\+" rpl)
  11151. (cond
  11152. ((bolp))
  11153. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  11154. (setq ind (buffer-substring (point-at-bol) (point))))
  11155. (t (newline))))
  11156. (setq start (point))
  11157. (when (string-match "%file" rpl)
  11158. (setq rpl (replace-match
  11159. (concat
  11160. "\""
  11161. (save-match-data
  11162. (abbreviate-file-name (read-file-name "Include file: ")))
  11163. "\"")
  11164. t t rpl)))
  11165. (setq rpl (mapconcat 'identity (split-string rpl "\n")
  11166. (concat "\n" ind)))
  11167. (insert rpl)
  11168. (when (re-search-backward "\\?" start t) (delete-char 1))))
  11169. ;;;; TODO, DEADLINE, Comments
  11170. (defun org-toggle-comment ()
  11171. "Change the COMMENT state of an entry."
  11172. (interactive)
  11173. (save-excursion
  11174. (org-back-to-heading)
  11175. (looking-at org-complex-heading-regexp)
  11176. (goto-char (or (match-end 3) (match-end 2) (match-end 1)))
  11177. (skip-chars-forward " \t")
  11178. (unless (memq (char-before) '(?\s ?\t)) (insert " "))
  11179. (if (org-in-commented-heading-p t)
  11180. (delete-region (point)
  11181. (progn (search-forward " " (line-end-position) 'move)
  11182. (skip-chars-forward " \t")
  11183. (point)))
  11184. (insert org-comment-string)
  11185. (unless (eolp) (insert " ")))))
  11186. (defvar org-last-todo-state-is-todo nil
  11187. "This is non-nil when the last TODO state change led to a TODO state.
  11188. If the last change removed the TODO tag or switched to DONE, then
  11189. this is nil.")
  11190. (defvar org-setting-tags nil) ; dynamically skipped
  11191. (defvar org-todo-setup-filter-hook nil
  11192. "Hook for functions that pre-filter todo specs.
  11193. Each function takes a todo spec and returns either nil or the spec
  11194. transformed into canonical form." )
  11195. (defvar org-todo-get-default-hook nil
  11196. "Hook for functions that get a default item for todo.
  11197. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  11198. nil or a string to be used for the todo mark." )
  11199. (defvar org-agenda-headline-snapshot-before-repeat)
  11200. (defun org-current-effective-time ()
  11201. "Return current time adjusted for `org-extend-today-until' variable."
  11202. (let* ((ct (org-current-time))
  11203. (dct (decode-time ct))
  11204. (ct1
  11205. (cond
  11206. (org-use-last-clock-out-time-as-effective-time
  11207. (or (org-clock-get-last-clock-out-time) ct))
  11208. ((and org-use-effective-time (< (nth 2 dct) org-extend-today-until))
  11209. (encode-time 0 59 23 (1- (nth 3 dct)) (nth 4 dct) (nth 5 dct)))
  11210. (t ct))))
  11211. ct1))
  11212. (defun org-todo-yesterday (&optional arg)
  11213. "Like `org-todo' but the time of change will be 23:59 of yesterday."
  11214. (interactive "P")
  11215. (if (eq major-mode 'org-agenda-mode)
  11216. (apply 'org-agenda-todo-yesterday arg)
  11217. (let* ((org-use-effective-time t)
  11218. (hour (third (decode-time
  11219. (org-current-time))))
  11220. (org-extend-today-until (1+ hour)))
  11221. (org-todo arg))))
  11222. (defvar org-block-entry-blocking ""
  11223. "First entry preventing the TODO state change.")
  11224. (defun org-cancel-repeater ()
  11225. "Cancel a repeater by setting its numeric value to zero."
  11226. (interactive)
  11227. (save-excursion
  11228. (org-back-to-heading t)
  11229. (let ((bound1 (point))
  11230. (bound0 (save-excursion (outline-next-heading) (point))))
  11231. (when (and (re-search-forward
  11232. (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  11233. org-deadline-time-regexp "\\)\\|\\("
  11234. org-ts-regexp "\\)")
  11235. bound0 t)
  11236. (re-search-backward "[ \t]+\\(?:[.+]\\)?\\+\\([0-9]+\\)[hdwmy]"
  11237. bound1 t))
  11238. (replace-match "0" t nil nil 1)))))
  11239. (defun org-todo (&optional arg)
  11240. "Change the TODO state of an item.
  11241. The state of an item is given by a keyword at the start of the heading,
  11242. like
  11243. *** TODO Write paper
  11244. *** DONE Call mom
  11245. The different keywords are specified in the variable `org-todo-keywords'.
  11246. By default the available states are \"TODO\" and \"DONE\".
  11247. So for this example: when the item starts with TODO, it is changed to DONE.
  11248. When it starts with DONE, the DONE is removed. And when neither TODO nor
  11249. DONE are present, add TODO at the beginning of the heading.
  11250. With \\[universal-argument] prefix arg, use completion to determine the new \
  11251. state.
  11252. With numeric prefix arg, switch to that state.
  11253. With a double \\[universal-argument] prefix, switch to the next set of TODO \
  11254. keywords (nextset).
  11255. With a triple \\[universal-argument] prefix, circumvent any state blocking.
  11256. With a numeric prefix arg of 0, inhibit note taking for the change.
  11257. With a numeric prefix arg of -1, cancel repeater to allow marking as DONE.
  11258. When called through ELisp, arg is also interpreted in the following way:
  11259. `none' -> empty state
  11260. \"\"(empty string) -> switch to empty state
  11261. `done' -> switch to DONE
  11262. `nextset' -> switch to the next set of keywords
  11263. `previousset' -> switch to the previous set of keywords
  11264. \"WAITING\" -> switch to the specified keyword, but only if it
  11265. really is a member of `org-todo-keywords'."
  11266. (interactive "P")
  11267. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  11268. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11269. 'region-start-level 'region))
  11270. org-loop-over-headlines-in-active-region)
  11271. (org-map-entries
  11272. `(org-todo ,arg)
  11273. org-loop-over-headlines-in-active-region
  11274. cl (when (outline-invisible-p) (org-end-of-subtree nil t))))
  11275. (when (equal arg '(16)) (setq arg 'nextset))
  11276. (when (equal arg -1) (org-cancel-repeater) (setq arg nil))
  11277. (let ((org-blocker-hook org-blocker-hook)
  11278. commentp
  11279. case-fold-search)
  11280. (when (equal arg '(64))
  11281. (setq arg nil org-blocker-hook nil))
  11282. (when (and org-blocker-hook
  11283. (or org-inhibit-blocking
  11284. (org-entry-get nil "NOBLOCKING")))
  11285. (setq org-blocker-hook nil))
  11286. (save-excursion
  11287. (catch 'exit
  11288. (org-back-to-heading t)
  11289. (when (org-in-commented-heading-p t)
  11290. (org-toggle-comment)
  11291. (setq commentp t))
  11292. (when (looking-at org-outline-regexp) (goto-char (1- (match-end 0))))
  11293. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|[ \t]*$\\)"))
  11294. (looking-at "\\(?: *\\|[ \t]*$\\)"))
  11295. (let* ((match-data (match-data))
  11296. (startpos (point-at-bol))
  11297. (logging (save-match-data (org-entry-get nil "LOGGING" t t)))
  11298. (org-log-done org-log-done)
  11299. (org-log-repeat org-log-repeat)
  11300. (org-todo-log-states org-todo-log-states)
  11301. (org-inhibit-logging
  11302. (if (equal arg 0)
  11303. (progn (setq arg nil) 'note) org-inhibit-logging))
  11304. (this (match-string 1))
  11305. (hl-pos (match-beginning 0))
  11306. (head (org-get-todo-sequence-head this))
  11307. (ass (assoc head org-todo-kwd-alist))
  11308. (interpret (nth 1 ass))
  11309. (done-word (nth 3 ass))
  11310. (final-done-word (nth 4 ass))
  11311. (org-last-state (or this ""))
  11312. (completion-ignore-case t)
  11313. (member (member this org-todo-keywords-1))
  11314. (tail (cdr member))
  11315. (org-state (cond
  11316. ((and org-todo-key-trigger
  11317. (or (and (equal arg '(4))
  11318. (eq org-use-fast-todo-selection 'prefix))
  11319. (and (not arg) org-use-fast-todo-selection
  11320. (not (eq org-use-fast-todo-selection
  11321. 'prefix)))))
  11322. ;; Use fast selection
  11323. (org-fast-todo-selection))
  11324. ((and (equal arg '(4))
  11325. (or (not org-use-fast-todo-selection)
  11326. (not org-todo-key-trigger)))
  11327. ;; Read a state with completion
  11328. (org-icompleting-read
  11329. "State: " (mapcar 'list org-todo-keywords-1)
  11330. nil t))
  11331. ((eq arg 'right)
  11332. (if this
  11333. (if tail (car tail) nil)
  11334. (car org-todo-keywords-1)))
  11335. ((eq arg 'left)
  11336. (unless (equal member org-todo-keywords-1)
  11337. (if this
  11338. (nth (- (length org-todo-keywords-1)
  11339. (length tail) 2)
  11340. org-todo-keywords-1)
  11341. (org-last org-todo-keywords-1))))
  11342. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  11343. (setq arg nil))) ; hack to fall back to cycling
  11344. (arg
  11345. ;; user or caller requests a specific state
  11346. (cond
  11347. ((equal arg "") nil)
  11348. ((eq arg 'none) nil)
  11349. ((eq arg 'done) (or done-word (car org-done-keywords)))
  11350. ((eq arg 'nextset)
  11351. (or (car (cdr (member head org-todo-heads)))
  11352. (car org-todo-heads)))
  11353. ((eq arg 'previousset)
  11354. (let ((org-todo-heads (reverse org-todo-heads)))
  11355. (or (car (cdr (member head org-todo-heads)))
  11356. (car org-todo-heads))))
  11357. ((car (member arg org-todo-keywords-1)))
  11358. ((stringp arg)
  11359. (user-error "State `%s' not valid in this file" arg))
  11360. ((nth (1- (prefix-numeric-value arg))
  11361. org-todo-keywords-1))))
  11362. ((null member) (or head (car org-todo-keywords-1)))
  11363. ((equal this final-done-word) nil) ;; -> make empty
  11364. ((null tail) nil) ;; -> first entry
  11365. ((memq interpret '(type priority))
  11366. (if (eq this-command last-command)
  11367. (car tail)
  11368. (if (> (length tail) 0)
  11369. (or done-word (car org-done-keywords))
  11370. nil)))
  11371. (t
  11372. (car tail))))
  11373. (org-state (or
  11374. (run-hook-with-args-until-success
  11375. 'org-todo-get-default-hook org-state org-last-state)
  11376. org-state))
  11377. (next (if org-state (concat " " org-state " ") " "))
  11378. (change-plist (list :type 'todo-state-change :from this :to org-state
  11379. :position startpos))
  11380. dolog now-done-p)
  11381. (when org-blocker-hook
  11382. (setq org-last-todo-state-is-todo
  11383. (not (member this org-done-keywords)))
  11384. (unless (save-excursion
  11385. (save-match-data
  11386. (org-with-wide-buffer
  11387. (run-hook-with-args-until-failure
  11388. 'org-blocker-hook change-plist))))
  11389. (if (org-called-interactively-p 'interactive)
  11390. (user-error "TODO state change from %s to %s blocked (by \"%s\")"
  11391. this org-state org-block-entry-blocking)
  11392. ;; fail silently
  11393. (message "TODO state change from %s to %s blocked (by \"%s\")"
  11394. this org-state org-block-entry-blocking)
  11395. (throw 'exit nil))))
  11396. (store-match-data match-data)
  11397. (replace-match next t t)
  11398. (cond ((equal this org-state)
  11399. (message "TODO state was already %s" (org-trim next)))
  11400. ((pos-visible-in-window-p hl-pos)
  11401. (message "TODO state changed to %s" (org-trim next))))
  11402. (unless head
  11403. (setq head (org-get-todo-sequence-head org-state)
  11404. ass (assoc head org-todo-kwd-alist)
  11405. interpret (nth 1 ass)
  11406. done-word (nth 3 ass)
  11407. final-done-word (nth 4 ass)))
  11408. (when (memq arg '(nextset previousset))
  11409. (message "Keyword-Set %d/%d: %s"
  11410. (- (length org-todo-sets) -1
  11411. (length (memq (assoc org-state org-todo-sets) org-todo-sets)))
  11412. (length org-todo-sets)
  11413. (mapconcat 'identity (assoc org-state org-todo-sets) " ")))
  11414. (setq org-last-todo-state-is-todo
  11415. (not (member org-state org-done-keywords)))
  11416. (setq now-done-p (and (member org-state org-done-keywords)
  11417. (not (member this org-done-keywords))))
  11418. (and logging (org-local-logging logging))
  11419. (when (and (or org-todo-log-states org-log-done)
  11420. (not (eq org-inhibit-logging t))
  11421. (not (memq arg '(nextset previousset))))
  11422. ;; we need to look at recording a time and note
  11423. (setq dolog (or (nth 1 (assoc org-state org-todo-log-states))
  11424. (nth 2 (assoc this org-todo-log-states))))
  11425. (when (and (eq dolog 'note) (eq org-inhibit-logging 'note))
  11426. (setq dolog 'time))
  11427. (when (or (and (not org-state) (not org-closed-keep-when-no-todo))
  11428. (and org-state
  11429. (member org-state org-not-done-keywords)
  11430. (not (member this org-not-done-keywords))))
  11431. ;; This is now a todo state and was not one before
  11432. ;; If there was a CLOSED time stamp, get rid of it.
  11433. (org-add-planning-info nil nil 'closed))
  11434. (when (and now-done-p org-log-done)
  11435. ;; It is now done, and it was not done before
  11436. (org-add-planning-info 'closed (org-current-effective-time))
  11437. (when (and (not dolog) (eq 'note org-log-done))
  11438. (org-add-log-setup 'done org-state this 'findpos 'note)))
  11439. (when (and org-state dolog)
  11440. ;; This is a non-nil state, and we need to log it
  11441. (org-add-log-setup 'state org-state this 'findpos dolog)))
  11442. ;; Fixup tag positioning
  11443. (org-todo-trigger-tag-changes org-state)
  11444. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  11445. (when org-provide-todo-statistics
  11446. (org-update-parent-todo-statistics))
  11447. (run-hooks 'org-after-todo-state-change-hook)
  11448. (when (and arg (not (member org-state org-done-keywords)))
  11449. (setq head (org-get-todo-sequence-head org-state)))
  11450. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  11451. ;; Do we need to trigger a repeat?
  11452. (when now-done-p
  11453. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  11454. ;; This is for the agenda, take a snapshot of the headline.
  11455. (save-match-data
  11456. (setq org-agenda-headline-snapshot-before-repeat
  11457. (org-get-heading))))
  11458. (org-auto-repeat-maybe org-state))
  11459. ;; Fixup cursor location if close to the keyword
  11460. (when (and (outline-on-heading-p)
  11461. (not (bolp))
  11462. (save-excursion (beginning-of-line 1)
  11463. (looking-at org-todo-line-regexp))
  11464. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  11465. (goto-char (or (match-end 2) (match-end 1)))
  11466. (and (looking-at " ") (just-one-space)))
  11467. (when org-trigger-hook
  11468. (save-excursion
  11469. (run-hook-with-args 'org-trigger-hook change-plist)))
  11470. (when commentp (org-toggle-comment))))))))
  11471. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  11472. "Block turning an entry into a TODO, using the hierarchy.
  11473. This checks whether the current task should be blocked from state
  11474. changes. Such blocking occurs when:
  11475. 1. The task has children which are not all in a completed state.
  11476. 2. A task has a parent with the property :ORDERED:, and there
  11477. are siblings prior to the current task with incomplete
  11478. status.
  11479. 3. The parent of the task is blocked because it has siblings that should
  11480. be done first, or is child of a block grandparent TODO entry."
  11481. (if (not org-enforce-todo-dependencies)
  11482. t ; if locally turned off don't block
  11483. (catch 'dont-block
  11484. ;; If this is not a todo state change, or if this entry is already DONE,
  11485. ;; do not block
  11486. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  11487. (member (plist-get change-plist :from)
  11488. (cons 'done org-done-keywords))
  11489. (member (plist-get change-plist :to)
  11490. (cons 'todo org-not-done-keywords))
  11491. (not (plist-get change-plist :to)))
  11492. (throw 'dont-block t))
  11493. ;; If this task has children, and any are undone, it's blocked
  11494. (save-excursion
  11495. (org-back-to-heading t)
  11496. (let ((this-level (funcall outline-level)))
  11497. (outline-next-heading)
  11498. (let ((child-level (funcall outline-level)))
  11499. (while (and (not (eobp))
  11500. (> child-level this-level))
  11501. ;; this todo has children, check whether they are all
  11502. ;; completed
  11503. (when (and (not (org-entry-is-done-p))
  11504. (org-entry-is-todo-p))
  11505. (setq org-block-entry-blocking (org-get-heading))
  11506. (throw 'dont-block nil))
  11507. (outline-next-heading)
  11508. (setq child-level (funcall outline-level))))))
  11509. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  11510. ;; any previous siblings are undone, it's blocked
  11511. (save-excursion
  11512. (org-back-to-heading t)
  11513. (let* ((pos (point))
  11514. (parent-pos (and (org-up-heading-safe) (point))))
  11515. (unless parent-pos (throw 'dont-block t)) ; no parent
  11516. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  11517. (forward-line 1)
  11518. (re-search-forward org-not-done-heading-regexp pos t))
  11519. (setq org-block-entry-blocking (match-string 0))
  11520. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  11521. ;; Search further up the hierarchy, to see if an ancestor is blocked
  11522. (while t
  11523. (goto-char parent-pos)
  11524. (unless (looking-at org-not-done-heading-regexp)
  11525. (throw 'dont-block t)) ; do not block, parent is not a TODO
  11526. (setq pos (point))
  11527. (setq parent-pos (and (org-up-heading-safe) (point)))
  11528. (unless parent-pos (throw 'dont-block t)) ; no parent
  11529. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  11530. (forward-line 1)
  11531. (re-search-forward org-not-done-heading-regexp pos t)
  11532. (setq org-block-entry-blocking (org-get-heading)))
  11533. (throw 'dont-block nil)))))))) ; block, older sibling not done.
  11534. (defcustom org-track-ordered-property-with-tag nil
  11535. "Should the ORDERED property also be shown as a tag?
  11536. The ORDERED property decides if an entry should require subtasks to be
  11537. completed in sequence. Since a property is not very visible, setting
  11538. this option means that toggling the ORDERED property with the command
  11539. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  11540. not relevant for the behavior, but it makes things more visible.
  11541. Note that toggling the tag with tags commands will not change the property
  11542. and therefore not influence behavior!
  11543. This can be t, meaning the tag ORDERED should be used, It can also be a
  11544. string to select a different tag for this task."
  11545. :group 'org-todo
  11546. :type '(choice
  11547. (const :tag "No tracking" nil)
  11548. (const :tag "Track with ORDERED tag" t)
  11549. (string :tag "Use other tag")))
  11550. (defun org-toggle-ordered-property ()
  11551. "Toggle the ORDERED property of the current entry.
  11552. For better visibility, you can track the value of this property with a tag.
  11553. See variable `org-track-ordered-property-with-tag'."
  11554. (interactive)
  11555. (let* ((t1 org-track-ordered-property-with-tag)
  11556. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  11557. (save-excursion
  11558. (org-back-to-heading)
  11559. (if (org-entry-get nil "ORDERED")
  11560. (progn
  11561. (org-delete-property "ORDERED")
  11562. (and tag (org-toggle-tag tag 'off))
  11563. (message "Subtasks can be completed in arbitrary order"))
  11564. (org-entry-put nil "ORDERED" "t")
  11565. (and tag (org-toggle-tag tag 'on))
  11566. (message "Subtasks must be completed in sequence")))))
  11567. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  11568. (defun org-block-todo-from-checkboxes (change-plist)
  11569. "Block turning an entry into a TODO, using checkboxes.
  11570. This checks whether the current task should be blocked from state
  11571. changes because there are unchecked boxes in this entry."
  11572. (if (not org-enforce-todo-checkbox-dependencies)
  11573. t ; if locally turned off don't block
  11574. (catch 'dont-block
  11575. ;; If this is not a todo state change, or if this entry is already DONE,
  11576. ;; do not block
  11577. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  11578. (member (plist-get change-plist :from)
  11579. (cons 'done org-done-keywords))
  11580. (member (plist-get change-plist :to)
  11581. (cons 'todo org-not-done-keywords))
  11582. (not (plist-get change-plist :to)))
  11583. (throw 'dont-block t))
  11584. ;; If this task has checkboxes that are not checked, it's blocked
  11585. (save-excursion
  11586. (org-back-to-heading t)
  11587. (let ((beg (point)) end)
  11588. (outline-next-heading)
  11589. (setq end (point))
  11590. (goto-char beg)
  11591. (when (org-list-search-forward
  11592. (concat (org-item-beginning-re)
  11593. "\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?"
  11594. "\\[[- ]\\]")
  11595. end t)
  11596. (when (boundp 'org-blocked-by-checkboxes)
  11597. (setq org-blocked-by-checkboxes t))
  11598. (throw 'dont-block nil))))
  11599. t))) ; do not block
  11600. (defun org-entry-blocked-p ()
  11601. "Non-nil if entry at point is blocked."
  11602. (and (not (org-entry-get nil "NOBLOCKING"))
  11603. (member (org-entry-get nil "TODO") org-not-done-keywords)
  11604. (not (run-hook-with-args-until-failure
  11605. 'org-blocker-hook
  11606. (list :type 'todo-state-change
  11607. :position (point)
  11608. :from 'todo
  11609. :to 'done)))))
  11610. (defun org-update-statistics-cookies (all)
  11611. "Update the statistics cookie, either from TODO or from checkboxes.
  11612. This should be called with the cursor in a line with a statistics cookie."
  11613. (interactive "P")
  11614. (if all
  11615. (progn
  11616. (org-update-checkbox-count 'all)
  11617. (org-map-entries 'org-update-parent-todo-statistics))
  11618. (if (not (org-at-heading-p))
  11619. (org-update-checkbox-count)
  11620. (let ((pos (point-marker))
  11621. end l1 l2)
  11622. (ignore-errors (org-back-to-heading t))
  11623. (if (not (org-at-heading-p))
  11624. (org-update-checkbox-count)
  11625. (setq l1 (org-outline-level))
  11626. (setq end (save-excursion
  11627. (outline-next-heading)
  11628. (when (org-at-heading-p) (setq l2 (org-outline-level)))
  11629. (point)))
  11630. (if (and (save-excursion
  11631. (re-search-forward
  11632. "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) \\[[- X]\\]" end t))
  11633. (not (save-excursion (re-search-forward
  11634. ":COOKIE_DATA:.*\\<todo\\>" end t))))
  11635. (org-update-checkbox-count)
  11636. (if (and l2 (> l2 l1))
  11637. (progn
  11638. (goto-char end)
  11639. (org-update-parent-todo-statistics))
  11640. (goto-char pos)
  11641. (beginning-of-line 1)
  11642. (while (re-search-forward
  11643. "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)"
  11644. (point-at-eol) t)
  11645. (replace-match (if (match-end 2) "[100%]" "[0/0]") t t)))))
  11646. (goto-char pos)
  11647. (move-marker pos nil)))))
  11648. (defvar org-entry-property-inherited-from) ;; defined below
  11649. (defun org-update-parent-todo-statistics ()
  11650. "Update any statistics cookie in the parent of the current headline.
  11651. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  11652. the entire subtree and this will travel up the hierarchy and update
  11653. statistics everywhere."
  11654. (let* ((prop (save-excursion (org-up-heading-safe)
  11655. (org-entry-get nil "COOKIE_DATA" 'inherit)))
  11656. (recursive (or (not org-hierarchical-todo-statistics)
  11657. (and prop (string-match "\\<recursive\\>" prop))))
  11658. (lim (or (and prop (marker-position org-entry-property-inherited-from))
  11659. 0))
  11660. (first t)
  11661. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  11662. level ltoggle l1 new ndel
  11663. (cnt-all 0) (cnt-done 0) is-percent kwd
  11664. checkbox-beg ov ovs ove cookie-present)
  11665. (catch 'exit
  11666. (save-excursion
  11667. (beginning-of-line 1)
  11668. (setq ltoggle (funcall outline-level))
  11669. ;; Three situations are to consider:
  11670. ;; 1. if `org-hierarchical-todo-statistics' is nil, repeat up
  11671. ;; to the top-level ancestor on the headline;
  11672. ;; 2. If parent has "recursive" property, repeat up to the
  11673. ;; headline setting that property, taking inheritance into
  11674. ;; account;
  11675. ;; 3. Else, move up to direct parent and proceed only once.
  11676. (while (and (setq level (org-up-heading-safe))
  11677. (or recursive first)
  11678. (>= (point) lim))
  11679. (setq first nil cookie-present nil)
  11680. (unless (and level
  11681. (not (string-match
  11682. "\\<checkbox\\>"
  11683. (downcase (or (org-entry-get nil "COOKIE_DATA")
  11684. "")))))
  11685. (throw 'exit nil))
  11686. (while (re-search-forward box-re (point-at-eol) t)
  11687. (setq cnt-all 0 cnt-done 0 cookie-present t)
  11688. (setq is-percent (match-end 2) checkbox-beg (match-beginning 0))
  11689. (save-match-data
  11690. (unless (outline-next-heading) (throw 'exit nil))
  11691. (while (and (looking-at org-complex-heading-regexp)
  11692. (> (setq l1 (length (match-string 1))) level))
  11693. (setq kwd (and (or recursive (= l1 ltoggle))
  11694. (match-string 2)))
  11695. (if (or (eq org-provide-todo-statistics 'all-headlines)
  11696. (and (eq org-provide-todo-statistics t)
  11697. (or (member kwd org-done-keywords)))
  11698. (and (listp org-provide-todo-statistics)
  11699. (stringp (car org-provide-todo-statistics))
  11700. (or (member kwd org-provide-todo-statistics)
  11701. (member kwd org-done-keywords)))
  11702. (and (listp org-provide-todo-statistics)
  11703. (listp (car org-provide-todo-statistics))
  11704. (or (member kwd (car org-provide-todo-statistics))
  11705. (and (member kwd org-done-keywords)
  11706. (member kwd (cadr org-provide-todo-statistics))))))
  11707. (setq cnt-all (1+ cnt-all))
  11708. (and (eq org-provide-todo-statistics t)
  11709. kwd
  11710. (setq cnt-all (1+ cnt-all))))
  11711. (when (or (and (member org-provide-todo-statistics '(t all-headlines))
  11712. (member kwd org-done-keywords))
  11713. (and (listp org-provide-todo-statistics)
  11714. (listp (car org-provide-todo-statistics))
  11715. (member kwd org-done-keywords)
  11716. (member kwd (cadr org-provide-todo-statistics)))
  11717. (and (listp org-provide-todo-statistics)
  11718. (stringp (car org-provide-todo-statistics))
  11719. (member kwd org-done-keywords)))
  11720. (setq cnt-done (1+ cnt-done)))
  11721. (outline-next-heading)))
  11722. (setq new
  11723. (if is-percent
  11724. (format "[%d%%]" (floor (* 100.0 cnt-done)
  11725. (max 1 cnt-all)))
  11726. (format "[%d/%d]" cnt-done cnt-all))
  11727. ndel (- (match-end 0) checkbox-beg))
  11728. ;; handle overlays when updating cookie from column view
  11729. (when (setq ov (car (overlays-at checkbox-beg)))
  11730. (setq ovs (overlay-start ov) ove (overlay-end ov))
  11731. (delete-overlay ov))
  11732. (goto-char checkbox-beg)
  11733. (insert new)
  11734. (delete-region (point) (+ (point) ndel))
  11735. (when org-auto-align-tags (org-fix-tags-on-the-fly))
  11736. (when ov (move-overlay ov ovs ove)))
  11737. (when cookie-present
  11738. (run-hook-with-args 'org-after-todo-statistics-hook
  11739. cnt-done (- cnt-all cnt-done))))))
  11740. (run-hooks 'org-todo-statistics-hook)))
  11741. (defvar org-after-todo-statistics-hook nil
  11742. "Hook that is called after a TODO statistics cookie has been updated.
  11743. Each function is called with two arguments: the number of not-done entries
  11744. and the number of done entries.
  11745. For example, the following function, when added to this hook, will switch
  11746. an entry to DONE when all children are done, and back to TODO when new
  11747. entries are set to a TODO status. Note that this hook is only called
  11748. when there is a statistics cookie in the headline!
  11749. (defun org-summary-todo (n-done n-not-done)
  11750. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  11751. (let (org-log-done org-log-states) ; turn off logging
  11752. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  11753. ")
  11754. (defvar org-todo-statistics-hook nil
  11755. "Hook that is run whenever Org thinks TODO statistics should be updated.
  11756. This hook runs even if there is no statistics cookie present, in which case
  11757. `org-after-todo-statistics-hook' would not run.")
  11758. (defun org-todo-trigger-tag-changes (state)
  11759. "Apply the changes defined in `org-todo-state-tags-triggers'."
  11760. (let ((l org-todo-state-tags-triggers)
  11761. changes)
  11762. (when (or (not state) (equal state ""))
  11763. (setq changes (append changes (cdr (assoc "" l)))))
  11764. (when (and (stringp state) (> (length state) 0))
  11765. (setq changes (append changes (cdr (assoc state l)))))
  11766. (when (member state org-not-done-keywords)
  11767. (setq changes (append changes (cdr (assoc 'todo l)))))
  11768. (when (member state org-done-keywords)
  11769. (setq changes (append changes (cdr (assoc 'done l)))))
  11770. (dolist (c changes)
  11771. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  11772. (defun org-local-logging (value)
  11773. "Get logging settings from a property VALUE."
  11774. ;; Directly set the variables, they are already local.
  11775. (setq org-log-done nil
  11776. org-log-repeat nil
  11777. org-todo-log-states nil)
  11778. (dolist (w (org-split-string value))
  11779. (let (a)
  11780. (cond
  11781. ((setq a (assoc w org-startup-options))
  11782. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  11783. (set (nth 1 a) (nth 2 a))))
  11784. ((setq a (org-extract-log-state-settings w))
  11785. (and (member (car a) org-todo-keywords-1)
  11786. (push a org-todo-log-states)))))))
  11787. (defun org-get-todo-sequence-head (kwd)
  11788. "Return the head of the TODO sequence to which KWD belongs.
  11789. If KWD is not set, check if there is a text property remembering the
  11790. right sequence."
  11791. (let (p)
  11792. (cond
  11793. ((not kwd)
  11794. (or (get-text-property (point-at-bol) 'org-todo-head)
  11795. (progn
  11796. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  11797. nil (point-at-eol)))
  11798. (get-text-property p 'org-todo-head))))
  11799. ((not (member kwd org-todo-keywords-1))
  11800. (car org-todo-keywords-1))
  11801. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  11802. (defun org-fast-todo-selection ()
  11803. "Fast TODO keyword selection with single keys.
  11804. Returns the new TODO keyword, or nil if no state change should occur."
  11805. (let* ((fulltable org-todo-key-alist)
  11806. (done-keywords org-done-keywords) ;; needed for the faces.
  11807. (maxlen (apply 'max (mapcar
  11808. (lambda (x)
  11809. (if (stringp (car x)) (string-width (car x)) 0))
  11810. fulltable)))
  11811. (expert nil)
  11812. (fwidth (+ maxlen 3 1 3))
  11813. (ncol (/ (- (window-width) 4) fwidth))
  11814. tg cnt e c tbl
  11815. groups ingroup)
  11816. (save-excursion
  11817. (save-window-excursion
  11818. (if expert
  11819. (set-buffer (get-buffer-create " *Org todo*"))
  11820. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  11821. (erase-buffer)
  11822. (setq-local org-done-keywords done-keywords)
  11823. (setq tbl fulltable cnt 0)
  11824. (while (setq e (pop tbl))
  11825. (cond
  11826. ((equal e '(:startgroup))
  11827. (push '() groups) (setq ingroup t)
  11828. (unless (= cnt 0)
  11829. (setq cnt 0)
  11830. (insert "\n"))
  11831. (insert "{ "))
  11832. ((equal e '(:endgroup))
  11833. (setq ingroup nil cnt 0)
  11834. (insert "}\n"))
  11835. ((equal e '(:newline))
  11836. (unless (= cnt 0)
  11837. (setq cnt 0)
  11838. (insert "\n")
  11839. (setq e (car tbl))
  11840. (while (equal (car tbl) '(:newline))
  11841. (insert "\n")
  11842. (setq tbl (cdr tbl)))))
  11843. (t
  11844. (setq tg (car e) c (cdr e))
  11845. (when ingroup (push tg (car groups)))
  11846. (setq tg (org-add-props tg nil 'face
  11847. (org-get-todo-face tg)))
  11848. (when (and (= cnt 0) (not ingroup)) (insert " "))
  11849. (insert "[" c "] " tg (make-string
  11850. (- fwidth 4 (length tg)) ?\ ))
  11851. (when (= (setq cnt (1+ cnt)) ncol)
  11852. (insert "\n")
  11853. (when ingroup (insert " "))
  11854. (setq cnt 0)))))
  11855. (insert "\n")
  11856. (goto-char (point-min))
  11857. (unless expert (org-fit-window-to-buffer))
  11858. (message "[a-z..]:Set [SPC]:clear")
  11859. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  11860. (cond
  11861. ((or (= c ?\C-g)
  11862. (and (= c ?q) (not (rassoc c fulltable))))
  11863. (setq quit-flag t))
  11864. ((= c ?\ ) nil)
  11865. ((setq e (rassoc c fulltable) tg (car e))
  11866. tg)
  11867. (t (setq quit-flag t)))))))
  11868. (defun org-entry-is-todo-p ()
  11869. (member (org-get-todo-state) org-not-done-keywords))
  11870. (defun org-entry-is-done-p ()
  11871. (member (org-get-todo-state) org-done-keywords))
  11872. (defun org-get-todo-state ()
  11873. "Return the TODO keyword of the current subtree."
  11874. (save-excursion
  11875. (org-back-to-heading t)
  11876. (and (looking-at org-todo-line-regexp)
  11877. (match-end 2)
  11878. (match-string 2))))
  11879. (defun org-at-date-range-p (&optional inactive-ok)
  11880. "Non-nil if point is inside a date range.
  11881. When optional argument INACTIVE-OK is non-nil, also consider
  11882. inactive time ranges.
  11883. When this function returns a non-nil value, match data is set
  11884. according to `org-tr-regexp-both' or `org-tr-regexp', depending
  11885. on INACTIVE-OK."
  11886. (interactive)
  11887. (save-excursion
  11888. (catch 'exit
  11889. (let ((pos (point)))
  11890. (skip-chars-backward "^[<\r\n")
  11891. (skip-chars-backward "<[")
  11892. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  11893. (>= (match-end 0) pos)
  11894. (throw 'exit t))
  11895. (skip-chars-backward "^<[\r\n")
  11896. (skip-chars-backward "<[")
  11897. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  11898. (>= (match-end 0) pos)
  11899. (throw 'exit t)))
  11900. nil)))
  11901. (defun org-get-repeat (&optional tagline)
  11902. "Check if there is a deadline/schedule with repeater in this entry."
  11903. (save-match-data
  11904. (save-excursion
  11905. (org-back-to-heading t)
  11906. (and (re-search-forward (if tagline
  11907. (concat tagline "\\s-*" org-repeat-re)
  11908. org-repeat-re)
  11909. (org-entry-end-position) t)
  11910. (match-string-no-properties 1)))))
  11911. (defvar org-last-changed-timestamp)
  11912. (defvar org-last-inserted-timestamp)
  11913. (defvar org-log-post-message)
  11914. (defvar org-log-note-purpose)
  11915. (defvar org-log-note-how nil)
  11916. (defvar org-log-note-extra)
  11917. (defun org-auto-repeat-maybe (done-word)
  11918. "Check if the current headline contains a repeated deadline/schedule.
  11919. If yes, set TODO state back to what it was and change the base date
  11920. of repeating deadline/scheduled time stamps to new date.
  11921. This function is run automatically after each state change to a DONE state."
  11922. ;; last-state is dynamically scoped into this function
  11923. (let* ((repeat (org-get-repeat))
  11924. (aa (assoc org-last-state org-todo-kwd-alist))
  11925. (interpret (nth 1 aa))
  11926. (head (nth 2 aa))
  11927. (whata '(("h" . hour) ("d" . day) ("m" . month) ("y" . year)))
  11928. (msg "Entry repeats: ")
  11929. (org-log-done nil)
  11930. (org-todo-log-states nil)
  11931. re type n what ts time to-state)
  11932. (when (and repeat (not (zerop (string-to-number (substring repeat 1)))))
  11933. (when (eq org-log-repeat t) (setq org-log-repeat 'state))
  11934. (setq to-state (or (org-entry-get nil "REPEAT_TO_STATE")
  11935. org-todo-repeat-to-state))
  11936. (unless (and to-state (member to-state org-todo-keywords-1))
  11937. (setq to-state (if (eq interpret 'type) org-last-state head)))
  11938. (org-todo to-state)
  11939. (when (or org-log-repeat (org-entry-get nil "CLOCK"))
  11940. (org-entry-put nil "LAST_REPEAT" (format-time-string
  11941. (org-time-stamp-format t t))))
  11942. (when org-log-repeat
  11943. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  11944. (memq 'org-add-log-note post-command-hook))
  11945. ;; OK, we are already setup for some record
  11946. (when (eq org-log-repeat 'note)
  11947. ;; make sure we take a note, not only a time stamp
  11948. (setq org-log-note-how 'note))
  11949. ;; Set up for taking a record
  11950. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  11951. org-last-state
  11952. 'findpos org-log-repeat)))
  11953. (org-back-to-heading t)
  11954. (org-add-planning-info nil nil 'closed)
  11955. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  11956. org-deadline-time-regexp "\\)\\|\\("
  11957. org-ts-regexp "\\)"))
  11958. (while (re-search-forward
  11959. re (save-excursion (outline-next-heading) (point)) t)
  11960. (setq type (if (match-end 1) org-scheduled-string
  11961. (if (match-end 3) org-deadline-string "Plain:"))
  11962. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  11963. (if (not (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts))
  11964. (org-remove-timestamp-with-keyword org-scheduled-string)
  11965. (setq n (string-to-number (match-string 2 ts))
  11966. what (match-string 3 ts))
  11967. (when (equal what "w") (setq n (* n 7) what "d"))
  11968. (when (and (equal what "h")
  11969. (not (string-match "[0-9]\\{1,2\\}:[0-9]\\{2\\}" ts)))
  11970. (user-error
  11971. "Cannot repeat in Repeat in %d hour(s) because no hour has been set" n))
  11972. ;; Preparation, see if we need to modify the start date for the change
  11973. (when (match-end 1)
  11974. (setq time (save-match-data (org-time-string-to-time ts)))
  11975. (cond
  11976. ((equal (match-string 1 ts) ".")
  11977. ;; Shift starting date to today
  11978. (org-timestamp-change
  11979. (- (org-today) (time-to-days time))
  11980. 'day))
  11981. ((equal (match-string 1 ts) "+")
  11982. (let ((nshiftmax 10) (nshift 0))
  11983. (while (or (= nshift 0)
  11984. (<= (time-to-days time)
  11985. (time-to-days (current-time))))
  11986. (when (= (incf nshift) nshiftmax)
  11987. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  11988. (user-error "Abort")))
  11989. (org-timestamp-change n (cdr (assoc what whata)))
  11990. (org-at-timestamp-p t)
  11991. (setq ts (match-string 1))
  11992. (setq time (save-match-data (org-time-string-to-time ts)))))
  11993. (org-timestamp-change (- n) (cdr (assoc what whata)))
  11994. ;; rematch, so that we have everything in place for the real shift
  11995. (org-at-timestamp-p t)
  11996. (setq ts (match-string 1))
  11997. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts))))
  11998. (save-excursion
  11999. (org-timestamp-change n (cdr (assoc what whata)) nil t))
  12000. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  12001. (setq org-log-post-message msg)
  12002. (message "%s" msg))))
  12003. (defun org-show-todo-tree (arg)
  12004. "Make a compact tree which shows all headlines marked with TODO.
  12005. The tree will show the lines where the regexp matches, and all higher
  12006. headlines above the match.
  12007. With a \\[universal-argument] prefix, prompt for a regexp to match.
  12008. With a numeric prefix N, construct a sparse tree for the Nth element
  12009. of `org-todo-keywords-1'."
  12010. (interactive "P")
  12011. (let ((case-fold-search nil)
  12012. (kwd-re
  12013. (cond ((null arg) org-not-done-regexp)
  12014. ((equal arg '(4))
  12015. (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
  12016. (mapcar 'list org-todo-keywords-1))))
  12017. (concat "\\("
  12018. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  12019. "\\)\\>")))
  12020. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  12021. (regexp-quote (nth (1- (prefix-numeric-value arg))
  12022. org-todo-keywords-1)))
  12023. (t (user-error "Invalid prefix argument: %s" arg)))))
  12024. (message "%d TODO entries found"
  12025. (org-occur (concat "^" org-outline-regexp " *" kwd-re )))))
  12026. (defun org-deadline (arg &optional time)
  12027. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  12028. With one universal prefix argument, remove any deadline from the item.
  12029. With two universal prefix arguments, prompt for a warning delay.
  12030. With argument TIME, set the deadline at the corresponding date. TIME
  12031. can either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  12032. (interactive "P")
  12033. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  12034. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  12035. 'region-start-level 'region))
  12036. org-loop-over-headlines-in-active-region)
  12037. (org-map-entries
  12038. `(org-deadline ',arg ,time)
  12039. org-loop-over-headlines-in-active-region
  12040. cl (when (outline-invisible-p) (org-end-of-subtree nil t))))
  12041. (let* ((old-date (org-entry-get nil "DEADLINE"))
  12042. (old-date-time (when old-date (org-time-string-to-time old-date)))
  12043. (repeater (and old-date
  12044. (string-match
  12045. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  12046. old-date)
  12047. (match-string 1 old-date))))
  12048. (cond
  12049. ((equal arg '(4))
  12050. (when (and old-date org-log-redeadline)
  12051. (org-add-log-setup 'deldeadline nil old-date 'findpos
  12052. org-log-redeadline))
  12053. (org-remove-timestamp-with-keyword org-deadline-string)
  12054. (message "Item no longer has a deadline."))
  12055. ((equal arg '(16))
  12056. (save-excursion
  12057. (org-back-to-heading t)
  12058. (if (re-search-forward
  12059. org-deadline-time-regexp
  12060. (save-excursion (outline-next-heading) (point)) t)
  12061. (let* ((rpl0 (match-string 1))
  12062. (rpl (replace-regexp-in-string " -[0-9]+[hdwmy]" "" rpl0)))
  12063. (replace-match
  12064. (concat org-deadline-string
  12065. " <" rpl
  12066. (format " -%dd"
  12067. (abs
  12068. (- (time-to-days
  12069. (save-match-data
  12070. (org-read-date nil t nil "Warn starting from" old-date-time)))
  12071. (time-to-days old-date-time))))
  12072. ">") t t))
  12073. (user-error "No deadline information to update"))))
  12074. (t
  12075. (org-add-planning-info 'deadline time 'closed)
  12076. (when (and old-date
  12077. org-log-redeadline
  12078. (not (equal old-date org-last-inserted-timestamp)))
  12079. (org-add-log-setup
  12080. 'redeadline org-last-inserted-timestamp old-date 'findpos
  12081. org-log-redeadline))
  12082. (when repeater
  12083. (save-excursion
  12084. (org-back-to-heading t)
  12085. (when (re-search-forward (concat org-deadline-string " "
  12086. org-last-inserted-timestamp)
  12087. (save-excursion
  12088. (outline-next-heading) (point)) t)
  12089. (goto-char (1- (match-end 0)))
  12090. (insert " " repeater)
  12091. (setq org-last-inserted-timestamp
  12092. (concat (substring org-last-inserted-timestamp 0 -1)
  12093. " " repeater
  12094. (substring org-last-inserted-timestamp -1))))))
  12095. (message "Deadline on %s" org-last-inserted-timestamp))))))
  12096. (defun org-schedule (arg &optional time)
  12097. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  12098. With one universal prefix argument, remove any scheduling date from the item.
  12099. With two universal prefix arguments, prompt for a delay cookie.
  12100. With argument TIME, scheduled at the corresponding date. TIME can
  12101. either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  12102. (interactive "P")
  12103. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  12104. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  12105. 'region-start-level 'region))
  12106. org-loop-over-headlines-in-active-region)
  12107. (org-map-entries
  12108. `(org-schedule ',arg ,time)
  12109. org-loop-over-headlines-in-active-region
  12110. cl (when (outline-invisible-p) (org-end-of-subtree nil t))))
  12111. (let* ((old-date (org-entry-get nil "SCHEDULED"))
  12112. (old-date-time (when old-date (org-time-string-to-time old-date)))
  12113. (repeater (and old-date
  12114. (string-match
  12115. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  12116. old-date)
  12117. (match-string 1 old-date))))
  12118. (cond
  12119. ((equal arg '(4))
  12120. (progn
  12121. (when (and old-date org-log-reschedule)
  12122. (org-add-log-setup 'delschedule nil old-date 'findpos
  12123. org-log-reschedule))
  12124. (org-remove-timestamp-with-keyword org-scheduled-string)
  12125. (message "Item is no longer scheduled.")))
  12126. ((equal arg '(16))
  12127. (save-excursion
  12128. (org-back-to-heading t)
  12129. (if (re-search-forward
  12130. org-scheduled-time-regexp
  12131. (save-excursion (outline-next-heading) (point)) t)
  12132. (let* ((rpl0 (match-string 1))
  12133. (rpl (replace-regexp-in-string " -[0-9]+[hdwmy]" "" rpl0)))
  12134. (replace-match
  12135. (concat org-scheduled-string
  12136. " <" rpl
  12137. (format " -%dd"
  12138. (abs
  12139. (- (time-to-days
  12140. (save-match-data
  12141. (org-read-date nil t nil "Delay until" old-date-time)))
  12142. (time-to-days old-date-time))))
  12143. ">") t t))
  12144. (user-error "No scheduled information to update"))))
  12145. (t
  12146. (org-add-planning-info 'scheduled time 'closed)
  12147. (when (and old-date
  12148. org-log-reschedule
  12149. (not (equal old-date org-last-inserted-timestamp)))
  12150. (org-add-log-setup
  12151. 'reschedule org-last-inserted-timestamp old-date 'findpos
  12152. org-log-reschedule))
  12153. (when repeater
  12154. (save-excursion
  12155. (org-back-to-heading t)
  12156. (when (re-search-forward (concat org-scheduled-string " "
  12157. org-last-inserted-timestamp)
  12158. (save-excursion
  12159. (outline-next-heading) (point)) t)
  12160. (goto-char (1- (match-end 0)))
  12161. (insert " " repeater)
  12162. (setq org-last-inserted-timestamp
  12163. (concat (substring org-last-inserted-timestamp 0 -1)
  12164. " " repeater
  12165. (substring org-last-inserted-timestamp -1))))))
  12166. (message "Scheduled to %s" org-last-inserted-timestamp))))))
  12167. (defun org-get-scheduled-time (pom &optional inherit)
  12168. "Get the scheduled time as a time tuple, of a format suitable
  12169. for calling org-schedule with, or if there is no scheduling,
  12170. returns nil."
  12171. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  12172. (when time
  12173. (apply 'encode-time (org-parse-time-string time)))))
  12174. (defun org-get-deadline-time (pom &optional inherit)
  12175. "Get the deadline as a time tuple, of a format suitable for
  12176. calling org-deadline with, or if there is no scheduling, returns
  12177. nil."
  12178. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  12179. (when time
  12180. (apply 'encode-time (org-parse-time-string time)))))
  12181. (defun org-remove-timestamp-with-keyword (keyword)
  12182. "Remove all time stamps with KEYWORD in the current entry."
  12183. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  12184. beg)
  12185. (save-excursion
  12186. (org-back-to-heading t)
  12187. (setq beg (point))
  12188. (outline-next-heading)
  12189. (while (re-search-backward re beg t)
  12190. (replace-match "")
  12191. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  12192. (equal (char-before) ?\ ))
  12193. (backward-delete-char 1)
  12194. (when (string-match "^[ \t]*$" (buffer-substring
  12195. (point-at-bol) (point-at-eol)))
  12196. (delete-region (point-at-bol)
  12197. (min (point-max) (1+ (point-at-eol))))))))))
  12198. (defvar org-time-was-given) ; dynamically scoped parameter
  12199. (defvar org-end-time-was-given) ; dynamically scoped parameter
  12200. (defun org-at-planning-p ()
  12201. "Non-nil when point is on a planning info line."
  12202. ;; This is as accurate and faster than `org-element-at-point' since
  12203. ;; planning info location is fixed in the section.
  12204. (org-with-wide-buffer
  12205. (beginning-of-line)
  12206. (and (org-looking-at-p org-planning-line-re)
  12207. (eq (point)
  12208. (ignore-errors
  12209. (if (and (featurep 'org-inlinetask) (org-inlinetask-in-task-p))
  12210. (org-back-to-heading t)
  12211. (org-with-limited-levels (org-back-to-heading t)))
  12212. (line-beginning-position 2))))))
  12213. (defun org-add-planning-info (what &optional time &rest remove)
  12214. "Insert new timestamp with keyword in the planning line.
  12215. WHAT indicates what kind of time stamp to add. It is a symbol
  12216. among `closed', `deadline', `scheduled' and nil. TIME indicates
  12217. the time to use. If none is given, the user is prompted for
  12218. a date. REMOVE indicates what kind of entries to remove. An old
  12219. WHAT entry will also be removed."
  12220. (let (org-time-was-given org-end-time-was-given default-time default-input)
  12221. (catch 'exit
  12222. (when (and (memq what '(scheduled deadline))
  12223. (or (not time)
  12224. (and (stringp time)
  12225. (string-match "^[-+]+[0-9]" time))))
  12226. ;; Try to get a default date/time from existing timestamp
  12227. (save-excursion
  12228. (org-back-to-heading t)
  12229. (let ((end (save-excursion (outline-next-heading) (point))) ts)
  12230. (when (re-search-forward (if (eq what 'scheduled)
  12231. org-scheduled-time-regexp
  12232. org-deadline-time-regexp)
  12233. end t)
  12234. (setq ts (match-string 1)
  12235. default-time (apply 'encode-time (org-parse-time-string ts))
  12236. default-input (and ts (org-get-compact-tod ts)))))))
  12237. (when what
  12238. (setq time
  12239. (if (stringp time)
  12240. ;; This is a string (relative or absolute), set
  12241. ;; proper date.
  12242. (apply #'encode-time
  12243. (org-read-date-analyze
  12244. time default-time (decode-time default-time)))
  12245. ;; If necessary, get the time from the user
  12246. (or time (org-read-date nil 'to-time nil nil
  12247. default-time default-input)))))
  12248. (org-with-wide-buffer
  12249. (org-back-to-heading t)
  12250. (forward-line)
  12251. (unless (bolp) (insert "\n"))
  12252. (cond ((org-looking-at-p org-planning-line-re)
  12253. ;; Move to current indentation.
  12254. (skip-chars-forward " \t")
  12255. ;; Check if we have to remove something.
  12256. (dolist (type (if what (cons what remove) remove))
  12257. (save-excursion
  12258. (when (re-search-forward
  12259. (case type
  12260. (closed org-closed-time-regexp)
  12261. (deadline org-deadline-time-regexp)
  12262. (scheduled org-scheduled-time-regexp)
  12263. (otherwise
  12264. (error "Invalid planning type: %s" type)))
  12265. (line-end-position) t)
  12266. ;; Delete until next keyword or end of line.
  12267. (delete-region
  12268. (match-beginning 0)
  12269. (if (re-search-forward org-keyword-time-not-clock-regexp
  12270. (line-end-position)
  12271. t)
  12272. (match-beginning 0)
  12273. (line-end-position))))))
  12274. ;; If there is nothing more to add and no more keyword
  12275. ;; is left, remove the line completely.
  12276. (if (and (org-looking-at-p "[ \t]*$") (not what))
  12277. (delete-region (line-beginning-position)
  12278. (line-beginning-position 2))
  12279. ;; If we removed last keyword, do not leave trailing
  12280. ;; white space at the end of line.
  12281. (let ((p (point)))
  12282. (save-excursion
  12283. (end-of-line)
  12284. (unless (= (skip-chars-backward " \t" p) 0)
  12285. (delete-region (point) (line-end-position)))))))
  12286. ((not what) (throw 'exit nil)) ; Nothing to do.
  12287. (t (insert-before-markers "\n")
  12288. (backward-char 1)
  12289. (when org-adapt-indentation
  12290. (org-indent-to-column (1+ (org-outline-level))))))
  12291. (when what
  12292. ;; Insert planning keyword.
  12293. (insert (case what
  12294. (closed org-closed-string)
  12295. (deadline org-deadline-string)
  12296. (scheduled org-scheduled-string)
  12297. (otherwise (error "Invalid planning type: %s" what)))
  12298. " ")
  12299. ;; Insert associated timestamp.
  12300. (let ((ts (org-insert-time-stamp
  12301. time
  12302. (or org-time-was-given
  12303. (and (eq what 'closed) org-log-done-with-time))
  12304. (eq what 'closed)
  12305. nil nil (list org-end-time-was-given))))
  12306. (unless (eolp) (insert " "))
  12307. ts))))))
  12308. (defvar org-log-note-marker (make-marker))
  12309. (defvar org-log-note-purpose nil)
  12310. (defvar org-log-note-state nil)
  12311. (defvar org-log-note-previous-state nil)
  12312. (defvar org-log-note-extra nil)
  12313. (defvar org-log-note-window-configuration nil)
  12314. (defvar org-log-note-return-to (make-marker))
  12315. (defvar org-log-note-effective-time nil
  12316. "Remembered current time so that dynamically scoped
  12317. `org-extend-today-until' affects tha timestamps in state change
  12318. log")
  12319. (defvar org-log-post-message nil
  12320. "Message to be displayed after a log note has been stored.
  12321. The auto-repeater uses this.")
  12322. (defun org-add-note ()
  12323. "Add a note to the current entry.
  12324. This is done in the same way as adding a state change note."
  12325. (interactive)
  12326. (org-add-log-setup 'note nil nil 'findpos nil))
  12327. (defun org-log-beginning (&optional create)
  12328. "Return expected start of log notes in current entry.
  12329. When optional argument CREATE is non-nil, the function creates
  12330. a drawer to store notes, if necessary. Returned position ignores
  12331. narrowing."
  12332. (org-with-wide-buffer
  12333. (org-end-of-meta-data)
  12334. (let ((end (if (org-at-heading-p) (point)
  12335. (save-excursion (outline-next-heading) (point))))
  12336. (drawer (org-log-into-drawer)))
  12337. (cond
  12338. (drawer
  12339. (let ((regexp (concat "^[ \t]*:" (regexp-quote drawer) ":[ \t]*$"))
  12340. (case-fold-search t))
  12341. (catch 'exit
  12342. ;; Try to find existing drawer.
  12343. (while (re-search-forward regexp end t)
  12344. (let ((element (org-element-at-point)))
  12345. (when (eq (org-element-type element) 'drawer)
  12346. (let ((cend (org-element-property :contents-end element)))
  12347. (when (and (not org-log-states-order-reversed) cend)
  12348. (goto-char cend)))
  12349. (throw 'exit nil))))
  12350. ;; No drawer found. Create one, if permitted.
  12351. (when create
  12352. (unless (bolp) (insert "\n"))
  12353. (let ((beg (point)))
  12354. (insert ":" drawer ":\n:END:\n")
  12355. (org-indent-region beg (point)))
  12356. (end-of-line -1)))))
  12357. (org-log-state-notes-insert-after-drawers
  12358. (while (and (looking-at org-drawer-regexp)
  12359. (progn (goto-char (match-end 0))
  12360. (re-search-forward org-property-end-re end t)))
  12361. (forward-line)))))
  12362. (if (bolp) (point) (line-beginning-position 2))))
  12363. (defun org-add-log-setup (&optional purpose state prev-state findpos how extra)
  12364. "Set up the post command hook to take a note.
  12365. If this is about to TODO state change, the new state is expected in STATE.
  12366. When FINDPOS is non-nil, find the correct position for the note in
  12367. the current entry. If not, assume that it can be inserted at point.
  12368. HOW is an indicator what kind of note should be created.
  12369. EXTRA is additional text that will be inserted into the notes buffer."
  12370. (org-with-wide-buffer
  12371. (when findpos
  12372. (goto-char (org-log-beginning t))
  12373. (unless org-log-states-order-reversed
  12374. (org-skip-over-state-notes)
  12375. (skip-chars-backward " \t\n\r")
  12376. (forward-line)))
  12377. (move-marker org-log-note-marker (point))
  12378. ;; Preserve position even if a property drawer is inserted in the
  12379. ;; process.
  12380. (set-marker-insertion-type org-log-note-marker t)
  12381. (setq org-log-note-purpose purpose
  12382. org-log-note-state state
  12383. org-log-note-previous-state prev-state
  12384. org-log-note-how how
  12385. org-log-note-extra extra
  12386. org-log-note-effective-time (org-current-effective-time))
  12387. (add-hook 'post-command-hook 'org-add-log-note 'append)))
  12388. (defun org-skip-over-state-notes ()
  12389. "Skip past the list of State notes in an entry."
  12390. (when (ignore-errors (goto-char (org-in-item-p)))
  12391. (let* ((struct (org-list-struct))
  12392. (prevs (org-list-prevs-alist struct))
  12393. (regexp
  12394. (concat "[ \t]*- +"
  12395. (replace-regexp-in-string
  12396. " +" " +"
  12397. (org-replace-escapes
  12398. (regexp-quote (cdr (assq 'state org-log-note-headings)))
  12399. `(("%d" . ,org-ts-regexp-inactive)
  12400. ("%D" . ,org-ts-regexp)
  12401. ("%s" . "\"\\S-+\"")
  12402. ("%S" . "\"\\S-+\"")
  12403. ("%t" . ,org-ts-regexp-inactive)
  12404. ("%T" . ,org-ts-regexp)
  12405. ("%u" . ".*?")
  12406. ("%U" . ".*?")))))))
  12407. (while (org-looking-at-p regexp)
  12408. (goto-char (or (org-list-get-next-item (point) struct prevs)
  12409. (org-list-get-item-end (point) struct)))))))
  12410. (defun org-add-log-note (&optional purpose)
  12411. "Pop up a window for taking a note, and add this note later at point."
  12412. (remove-hook 'post-command-hook 'org-add-log-note)
  12413. (setq org-log-note-window-configuration (current-window-configuration))
  12414. (delete-other-windows)
  12415. (move-marker org-log-note-return-to (point))
  12416. (org-pop-to-buffer-same-window (marker-buffer org-log-note-marker))
  12417. (goto-char org-log-note-marker)
  12418. (org-switch-to-buffer-other-window "*Org Note*")
  12419. (erase-buffer)
  12420. (if (memq org-log-note-how '(time state))
  12421. (let (current-prefix-arg) (org-store-log-note))
  12422. (let ((org-inhibit-startup t)) (org-mode))
  12423. (insert (format "# Insert note for %s.
  12424. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  12425. (cond
  12426. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  12427. ((eq org-log-note-purpose 'done) "closed todo item")
  12428. ((eq org-log-note-purpose 'state)
  12429. (format "state change from \"%s\" to \"%s\""
  12430. (or org-log-note-previous-state "")
  12431. (or org-log-note-state "")))
  12432. ((eq org-log-note-purpose 'reschedule)
  12433. "rescheduling")
  12434. ((eq org-log-note-purpose 'delschedule)
  12435. "no longer scheduled")
  12436. ((eq org-log-note-purpose 'redeadline)
  12437. "changing deadline")
  12438. ((eq org-log-note-purpose 'deldeadline)
  12439. "removing deadline")
  12440. ((eq org-log-note-purpose 'refile)
  12441. "refiling")
  12442. ((eq org-log-note-purpose 'note)
  12443. "this entry")
  12444. (t (error "This should not happen")))))
  12445. (when org-log-note-extra (insert org-log-note-extra))
  12446. (setq-local org-finish-function 'org-store-log-note)
  12447. (run-hooks 'org-log-buffer-setup-hook)))
  12448. (defvar org-note-abort nil) ; dynamically scoped
  12449. (defun org-store-log-note ()
  12450. "Finish taking a log note, and insert it to where it belongs."
  12451. (let ((txt (buffer-string)))
  12452. (kill-buffer (current-buffer))
  12453. (let ((note (cdr (assq org-log-note-purpose org-log-note-headings))) lines)
  12454. (while (string-match "\\`# .*\n[ \t\n]*" txt)
  12455. (setq txt (replace-match "" t t txt)))
  12456. (when (string-match "\\s-+\\'" txt)
  12457. (setq txt (replace-match "" t t txt)))
  12458. (setq lines (org-split-string txt "\n"))
  12459. (when (and note (string-match "\\S-" note))
  12460. (setq note
  12461. (org-replace-escapes
  12462. note
  12463. (list (cons "%u" (user-login-name))
  12464. (cons "%U" user-full-name)
  12465. (cons "%t" (format-time-string
  12466. (org-time-stamp-format 'long 'inactive)
  12467. org-log-note-effective-time))
  12468. (cons "%T" (format-time-string
  12469. (org-time-stamp-format 'long nil)
  12470. org-log-note-effective-time))
  12471. (cons "%d" (format-time-string
  12472. (org-time-stamp-format nil 'inactive)
  12473. org-log-note-effective-time))
  12474. (cons "%D" (format-time-string
  12475. (org-time-stamp-format nil nil)
  12476. org-log-note-effective-time))
  12477. (cons "%s" (cond
  12478. ((not org-log-note-state) "")
  12479. ((org-string-match-p org-ts-regexp
  12480. org-log-note-state)
  12481. (format "\"[%s]\""
  12482. (substring org-log-note-state 1 -1)))
  12483. (t (format "\"%s\"" org-log-note-state))))
  12484. (cons "%S"
  12485. (cond
  12486. ((not org-log-note-previous-state) "")
  12487. ((org-string-match-p org-ts-regexp
  12488. org-log-note-previous-state)
  12489. (format "\"[%s]\""
  12490. (substring
  12491. org-log-note-previous-state 1 -1)))
  12492. (t (format "\"%s\""
  12493. org-log-note-previous-state)))))))
  12494. (when lines (setq note (concat note " \\\\")))
  12495. (push note lines))
  12496. (when (or current-prefix-arg org-note-abort)
  12497. (when (org-log-into-drawer)
  12498. (org-remove-empty-drawer-at org-log-note-marker))
  12499. (setq lines nil))
  12500. (when lines
  12501. (with-current-buffer (marker-buffer org-log-note-marker)
  12502. (org-with-wide-buffer
  12503. (goto-char org-log-note-marker)
  12504. (move-marker org-log-note-marker nil)
  12505. ;; Make sure point is at the beginning of an empty line.
  12506. (cond ((not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  12507. ((looking-at "[ \t]*\\S-") (save-excursion (insert "\n"))))
  12508. ;; In an existing list, add a new item at the top level.
  12509. ;; Otherwise, indent line like a regular one.
  12510. (let ((itemp (org-in-item-p)))
  12511. (if itemp
  12512. (org-indent-line-to
  12513. (let ((struct (save-excursion
  12514. (goto-char itemp) (org-list-struct))))
  12515. (org-list-get-ind (org-list-get-top-point struct) struct)))
  12516. (org-indent-line)))
  12517. (insert (org-list-bullet-string "-") (pop lines))
  12518. (let ((ind (org-list-item-body-column (line-beginning-position))))
  12519. (dolist (line lines)
  12520. (insert "\n")
  12521. (org-indent-line-to ind)
  12522. (insert line)))
  12523. (message "Note stored")
  12524. (org-back-to-heading t)
  12525. (org-cycle-hide-drawers 'children))
  12526. ;; Fix `buffer-undo-list' when `org-store-log-note' is called
  12527. ;; from within `org-add-log-note' because `buffer-undo-list'
  12528. ;; is then modified outside of `org-with-remote-undo'.
  12529. (when (eq this-command 'org-agenda-todo)
  12530. (setcdr buffer-undo-list (cddr buffer-undo-list)))))))
  12531. ;; Don't add undo information when called from `org-agenda-todo'
  12532. (let ((buffer-undo-list (eq this-command 'org-agenda-todo)))
  12533. (set-window-configuration org-log-note-window-configuration)
  12534. (with-current-buffer (marker-buffer org-log-note-return-to)
  12535. (goto-char org-log-note-return-to))
  12536. (move-marker org-log-note-return-to nil)
  12537. (and org-log-post-message (message "%s" org-log-post-message))))
  12538. (defun org-remove-empty-drawer-at (pos)
  12539. "Remove an empty drawer at position POS.
  12540. POS may also be a marker."
  12541. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  12542. (org-with-wide-buffer
  12543. (goto-char pos)
  12544. (let ((drawer (org-element-at-point)))
  12545. (when (and (memq (org-element-type drawer) '(drawer property-drawer))
  12546. (not (org-element-property :contents-begin drawer)))
  12547. (delete-region (org-element-property :begin drawer)
  12548. (progn (goto-char (org-element-property :end drawer))
  12549. (skip-chars-backward " \r\t\n")
  12550. (forward-line)
  12551. (point))))))))
  12552. (defvar org-ts-type nil)
  12553. (defun org-sparse-tree (&optional arg type)
  12554. "Create a sparse tree, prompt for the details.
  12555. This command can create sparse trees. You first need to select the type
  12556. of match used to create the tree:
  12557. t Show all TODO entries.
  12558. T Show entries with a specific TODO keyword.
  12559. m Show entries selected by a tags/property match.
  12560. p Enter a property name and its value (both with completion on existing
  12561. names/values) and show entries with that property.
  12562. r Show entries matching a regular expression (`/' can be used as well).
  12563. b Show deadlines and scheduled items before a date.
  12564. a Show deadlines and scheduled items after a date.
  12565. d Show deadlines due within `org-deadline-warning-days'.
  12566. D Show deadlines and scheduled items between a date range."
  12567. (interactive "P")
  12568. (setq type (or type org-sparse-tree-default-date-type))
  12569. (setq org-ts-type type)
  12570. (message "Sparse tree: [/]regexp [t]odo [T]odo-kwd [m]atch [p]roperty
  12571. [d]eadlines [b]efore-date [a]fter-date [D]ates range
  12572. [c]ycle through date types: %s"
  12573. (case type
  12574. (all "all timestamps")
  12575. (scheduled "only scheduled")
  12576. (deadline "only deadline")
  12577. (active "only active timestamps")
  12578. (inactive "only inactive timestamps")
  12579. (closed "with a closed time-stamp")
  12580. (otherwise "scheduled/deadline")))
  12581. (let ((answer (read-char-exclusive)))
  12582. (case answer
  12583. (?c
  12584. (org-sparse-tree
  12585. arg
  12586. (cadr
  12587. (memq type '(nil all scheduled deadline active inactive closed)))))
  12588. (?d (call-interactively 'org-check-deadlines))
  12589. (?b (call-interactively 'org-check-before-date))
  12590. (?a (call-interactively 'org-check-after-date))
  12591. (?D (call-interactively 'org-check-dates-range))
  12592. (?t (call-interactively 'org-show-todo-tree))
  12593. (?T (org-show-todo-tree '(4)))
  12594. (?m (call-interactively 'org-match-sparse-tree))
  12595. ((?p ?P)
  12596. (let* ((kwd (org-icompleting-read
  12597. "Property: " (mapcar 'list (org-buffer-property-keys))))
  12598. (value (org-icompleting-read
  12599. "Value: " (mapcar 'list (org-property-values kwd)))))
  12600. (unless (string-match "\\`{.*}\\'" value)
  12601. (setq value (concat "\"" value "\"")))
  12602. (org-match-sparse-tree arg (concat kwd "=" value))))
  12603. ((?r ?R ?/) (call-interactively 'org-occur))
  12604. (otherwise (user-error "No such sparse tree command \"%c\"" answer)))))
  12605. (defvar-local org-occur-highlights nil
  12606. "List of overlays used for occur matches.")
  12607. (defvar-local org-occur-parameters nil
  12608. "Parameters of the active org-occur calls.
  12609. This is a list, each call to org-occur pushes as cons cell,
  12610. containing the regular expression and the callback, onto the list.
  12611. The list can contain several entries if `org-occur' has been called
  12612. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  12613. will only contain one set of parameters. When the highlights are
  12614. removed (for example with `C-c C-c', or with the next edit (depending
  12615. on `org-remove-highlights-with-change'), this variable is emptied
  12616. as well.")
  12617. (defun org-occur (regexp &optional keep-previous callback)
  12618. "Make a compact tree which shows all matches of REGEXP.
  12619. The tree will show the lines where the regexp matches, and all higher
  12620. headlines above the match. It will also show the heading after the match,
  12621. to make sure editing the matching entry is easy.
  12622. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  12623. call to `org-occur' will be kept, to allow stacking of calls to this
  12624. command.
  12625. If CALLBACK is non-nil, it is a function which is called to confirm
  12626. that the match should indeed be shown."
  12627. (interactive "sRegexp: \nP")
  12628. (when (equal regexp "")
  12629. (user-error "Regexp cannot be empty"))
  12630. (unless keep-previous
  12631. (org-remove-occur-highlights nil nil t))
  12632. (push (cons regexp callback) org-occur-parameters)
  12633. (let ((cnt 0))
  12634. (save-excursion
  12635. (goto-char (point-min))
  12636. (when (or (not keep-previous) ; do not want to keep
  12637. (not org-occur-highlights)) ; no previous matches
  12638. ;; hide everything
  12639. (org-overview))
  12640. (while (re-search-forward regexp nil t)
  12641. (backward-char) ;; FIXME: Match timestamps at the end of a headline
  12642. (when (or (not callback)
  12643. (save-match-data (funcall callback)))
  12644. (setq cnt (1+ cnt))
  12645. (when org-highlight-sparse-tree-matches
  12646. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  12647. (org-show-context 'occur-tree))))
  12648. (when org-remove-highlights-with-change
  12649. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  12650. nil 'local))
  12651. (unless org-sparse-tree-open-archived-trees
  12652. (org-hide-archived-subtrees (point-min) (point-max)))
  12653. (run-hooks 'org-occur-hook)
  12654. (when (org-called-interactively-p 'interactive)
  12655. (message "%d match(es) for regexp %s" cnt regexp))
  12656. cnt))
  12657. (defun org-occur-next-match (&optional n reset)
  12658. "Function for `next-error-function' to find sparse tree matches.
  12659. N is the number of matches to move, when negative move backwards.
  12660. RESET is entirely ignored - this function always goes back to the
  12661. starting point when no match is found."
  12662. (let* ((limit (if (< n 0) (point-min) (point-max)))
  12663. (search-func (if (< n 0)
  12664. 'previous-single-char-property-change
  12665. 'next-single-char-property-change))
  12666. (n (abs n))
  12667. (pos (point))
  12668. p1)
  12669. (catch 'exit
  12670. (while (setq p1 (funcall search-func (point) 'org-type))
  12671. (when (equal p1 limit)
  12672. (goto-char pos)
  12673. (user-error "No more matches"))
  12674. (when (equal (get-char-property p1 'org-type) 'org-occur)
  12675. (setq n (1- n))
  12676. (when (= n 0)
  12677. (goto-char p1)
  12678. (throw 'exit (point))))
  12679. (goto-char p1))
  12680. (goto-char p1)
  12681. (user-error "No more matches"))))
  12682. (defun org-show-context (&optional key)
  12683. "Make sure point and context are visible.
  12684. Optional argument KEY, when non-nil, is a symbol. See
  12685. `org-show-context-detail' for allowed values and how much is to
  12686. be shown."
  12687. (org-show-set-visibility
  12688. (cond ((symbolp org-show-context-detail) org-show-context-detail)
  12689. ((cdr (assq key org-show-context-detail)))
  12690. (t (cdr (assq 'default org-show-context-detail))))))
  12691. (defun org-show-set-visibility (detail)
  12692. "Set visibility around point according to DETAIL.
  12693. DETAIL is either nil, `minimal', `local', `ancestors', `lineage',
  12694. `tree', `canonical' or t. See `org-show-context-detail' for more
  12695. information."
  12696. (unless (org-before-first-heading-p)
  12697. ;; Show current heading and possibly its entry, following headline
  12698. ;; or all children.
  12699. (if (and (org-at-heading-p) (not (eq detail 'local)))
  12700. (org-flag-heading nil)
  12701. (org-show-entry)
  12702. (org-with-limited-levels
  12703. (case detail
  12704. ((tree canonical t) (org-show-children))
  12705. ((nil minimal ancestors))
  12706. (t (save-excursion
  12707. (outline-next-heading)
  12708. (org-flag-heading nil))))))
  12709. ;; Show all siblings.
  12710. (when (eq detail 'lineage) (org-show-siblings))
  12711. ;; Show ancestors, possibly with their children.
  12712. (when (memq detail '(ancestors lineage tree canonical t))
  12713. (save-excursion
  12714. (while (org-up-heading-safe)
  12715. (org-flag-heading nil)
  12716. (when (memq detail '(canonical t)) (org-show-entry))
  12717. (when (memq detail '(tree canonical t)) (org-show-children)))))))
  12718. (defvar org-reveal-start-hook nil
  12719. "Hook run before revealing a location.")
  12720. (defun org-reveal (&optional siblings)
  12721. "Show current entry, hierarchy above it, and the following headline.
  12722. This can be used to show a consistent set of context around
  12723. locations exposed with `org-show-context'.
  12724. With optional argument SIBLINGS, on each level of the hierarchy all
  12725. siblings are shown. This repairs the tree structure to what it would
  12726. look like when opened with hierarchical calls to `org-cycle'.
  12727. With double optional argument \\[universal-argument] \\[universal-argument], \
  12728. go to the parent and show the
  12729. entire tree."
  12730. (interactive "P")
  12731. (run-hooks 'org-reveal-start-hook)
  12732. (cond ((equal siblings '(4)) (org-show-set-visibility 'canonical))
  12733. ((equal siblings '(16))
  12734. (save-excursion
  12735. (when (org-up-heading-safe)
  12736. (org-show-subtree)
  12737. (run-hook-with-args 'org-cycle-hook 'subtree))))
  12738. (t (org-show-set-visibility 'lineage))))
  12739. (defun org-highlight-new-match (beg end)
  12740. "Highlight from BEG to END and mark the highlight is an occur headline."
  12741. (let ((ov (make-overlay beg end)))
  12742. (overlay-put ov 'face 'secondary-selection)
  12743. (overlay-put ov 'org-type 'org-occur)
  12744. (push ov org-occur-highlights)))
  12745. (defun org-remove-occur-highlights (&optional beg end noremove)
  12746. "Remove the occur highlights from the buffer.
  12747. BEG and END are ignored. If NOREMOVE is nil, remove this function
  12748. from the `before-change-functions' in the current buffer."
  12749. (interactive)
  12750. (unless org-inhibit-highlight-removal
  12751. (mapc 'delete-overlay org-occur-highlights)
  12752. (setq org-occur-highlights nil)
  12753. (setq org-occur-parameters nil)
  12754. (unless noremove
  12755. (remove-hook 'before-change-functions
  12756. 'org-remove-occur-highlights 'local))))
  12757. ;;;; Priorities
  12758. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  12759. "Regular expression matching the priority indicator.")
  12760. (defvar org-remove-priority-next-time nil)
  12761. (defun org-priority-up ()
  12762. "Increase the priority of the current item."
  12763. (interactive)
  12764. (org-priority 'up))
  12765. (defun org-priority-down ()
  12766. "Decrease the priority of the current item."
  12767. (interactive)
  12768. (org-priority 'down))
  12769. (defun org-priority (&optional action show)
  12770. "Change the priority of an item.
  12771. ACTION can be `set', `up', `down', or a character."
  12772. (interactive "P")
  12773. (if (equal action '(4))
  12774. (org-show-priority)
  12775. (unless org-enable-priority-commands
  12776. (user-error "Priority commands are disabled"))
  12777. (setq action (or action 'set))
  12778. (let (current new news have remove)
  12779. (save-excursion
  12780. (org-back-to-heading t)
  12781. (when (looking-at org-priority-regexp)
  12782. (setq current (string-to-char (match-string 2))
  12783. have t))
  12784. (cond
  12785. ((eq action 'remove)
  12786. (setq remove t new ?\ ))
  12787. ((or (eq action 'set)
  12788. (if (featurep 'xemacs) (characterp action) (integerp action)))
  12789. (if (not (eq action 'set))
  12790. (setq new action)
  12791. (message "Priority %c-%c, SPC to remove: "
  12792. org-highest-priority org-lowest-priority)
  12793. (save-match-data
  12794. (setq new (read-char-exclusive))))
  12795. (when (and (= (upcase org-highest-priority) org-highest-priority)
  12796. (= (upcase org-lowest-priority) org-lowest-priority))
  12797. (setq new (upcase new)))
  12798. (cond ((equal new ?\ ) (setq remove t))
  12799. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  12800. (user-error "Priority must be between `%c' and `%c'"
  12801. org-highest-priority org-lowest-priority))))
  12802. ((eq action 'up)
  12803. (setq new (if have
  12804. (1- current) ; normal cycling
  12805. ;; last priority was empty
  12806. (if (eq last-command this-command)
  12807. org-lowest-priority ; wrap around empty to lowest
  12808. ;; default
  12809. (if org-priority-start-cycle-with-default
  12810. org-default-priority
  12811. (1- org-default-priority))))))
  12812. ((eq action 'down)
  12813. (setq new (if have
  12814. (1+ current) ; normal cycling
  12815. ;; last priority was empty
  12816. (if (eq last-command this-command)
  12817. org-highest-priority ; wrap around empty to highest
  12818. ;; default
  12819. (if org-priority-start-cycle-with-default
  12820. org-default-priority
  12821. (1+ org-default-priority))))))
  12822. (t (user-error "Invalid action")))
  12823. (when (or (< (upcase new) org-highest-priority)
  12824. (> (upcase new) org-lowest-priority))
  12825. (if (and (memq action '(up down))
  12826. (not have) (not (eq last-command this-command)))
  12827. ;; `new' is from default priority
  12828. (error
  12829. "The default can not be set, see `org-default-priority' why")
  12830. ;; normal cycling: `new' is beyond highest/lowest priority
  12831. ;; and is wrapped around to the empty priority
  12832. (setq remove t)))
  12833. (setq news (format "%c" new))
  12834. (if have
  12835. (if remove
  12836. (replace-match "" t t nil 1)
  12837. (replace-match news t t nil 2))
  12838. (if remove
  12839. (user-error "No priority cookie found in line")
  12840. (let ((case-fold-search nil))
  12841. (looking-at org-todo-line-regexp))
  12842. (if (match-end 2)
  12843. (progn
  12844. (goto-char (match-end 2))
  12845. (insert " [#" news "]"))
  12846. (goto-char (match-beginning 3))
  12847. (insert "[#" news "] "))))
  12848. (org-set-tags nil 'align))
  12849. (if remove
  12850. (message "Priority removed")
  12851. (message "Priority of current item set to %s" news)))))
  12852. (defun org-show-priority ()
  12853. "Show the priority of the current item.
  12854. This priority is composed of the main priority given with the [#A] cookies,
  12855. and by additional input from the age of a schedules or deadline entry."
  12856. (interactive)
  12857. (let ((pri (if (eq major-mode 'org-agenda-mode)
  12858. (org-get-at-bol 'priority)
  12859. (save-excursion
  12860. (save-match-data
  12861. (beginning-of-line)
  12862. (and (looking-at org-heading-regexp)
  12863. (org-get-priority (match-string 0))))))))
  12864. (message "Priority is %d" (if pri pri -1000))))
  12865. (defun org-get-priority (s)
  12866. "Find priority cookie and return priority."
  12867. (save-match-data
  12868. (if (functionp org-get-priority-function)
  12869. (funcall org-get-priority-function)
  12870. (if (not (string-match org-priority-regexp s))
  12871. (* 1000 (- org-lowest-priority org-default-priority))
  12872. (* 1000 (- org-lowest-priority
  12873. (string-to-char (match-string 2 s))))))))
  12874. ;;;; Tags
  12875. (defvar org-agenda-archives-mode)
  12876. (defvar org-map-continue-from nil
  12877. "Position from where mapping should continue.
  12878. Can be set by the action argument to `org-scan-tags' and `org-map-entries'.")
  12879. (defvar org-scanner-tags nil
  12880. "The current tag list while the tags scanner is running.")
  12881. (defvar org-trust-scanner-tags nil
  12882. "Should `org-get-tags-at' use the tags for the scanner.
  12883. This is for internal dynamical scoping only.
  12884. When this is non-nil, the function `org-get-tags-at' will return the value
  12885. of `org-scanner-tags' instead of building the list by itself. This
  12886. can lead to large speed-ups when the tags scanner is used in a file with
  12887. many entries, and when the list of tags is retrieved, for example to
  12888. obtain a list of properties. Building the tags list for each entry in such
  12889. a file becomes an N^2 operation - but with this variable set, it scales
  12890. as N.")
  12891. (defun org-scan-tags (action matcher todo-only &optional start-level)
  12892. "Scan headline tags with inheritance and produce output ACTION.
  12893. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  12894. or `agenda' to produce an entry list for an agenda view. It can also be
  12895. a Lisp form or a function that should be called at each matched headline, in
  12896. this case the return value is a list of all return values from these calls.
  12897. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  12898. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  12899. only lines with a not-done TODO keyword are included in the output.
  12900. This should be the same variable that was scoped into
  12901. and set by `org-make-tags-matcher' when it constructed MATCHER.
  12902. START-LEVEL can be a string with asterisks, reducing the scope to
  12903. headlines matching this string."
  12904. (require 'org-agenda)
  12905. (let* ((re (concat "^"
  12906. (if start-level
  12907. ;; Get the correct level to match
  12908. (concat "\\*\\{" (number-to-string start-level) "\\} ")
  12909. org-outline-regexp)
  12910. " *\\(\\<\\("
  12911. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  12912. (org-re "\\)\\>\\)? *\\(.*?\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*$")))
  12913. (props (list 'face 'default
  12914. 'done-face 'org-agenda-done
  12915. 'undone-face 'default
  12916. 'mouse-face 'highlight
  12917. 'org-not-done-regexp org-not-done-regexp
  12918. 'org-todo-regexp org-todo-regexp
  12919. 'org-complex-heading-regexp org-complex-heading-regexp
  12920. 'help-echo
  12921. (format "mouse-2 or RET jump to org file %s"
  12922. (abbreviate-file-name
  12923. (or (buffer-file-name (buffer-base-buffer))
  12924. (buffer-name (buffer-base-buffer)))))))
  12925. (org-map-continue-from nil)
  12926. lspos tags tags-list
  12927. (tags-alist (list (cons 0 org-file-tags)))
  12928. (llast 0) rtn rtn1 level category i txt
  12929. todo marker entry priority
  12930. ts-date ts-date-type ts-date-pair)
  12931. (unless (or (member action '(agenda sparse-tree)) (functionp action))
  12932. (setq action (list 'lambda nil action)))
  12933. (save-excursion
  12934. (goto-char (point-min))
  12935. (when (eq action 'sparse-tree)
  12936. (org-overview)
  12937. (org-remove-occur-highlights))
  12938. (while (let (case-fold-search)
  12939. (re-search-forward re nil t))
  12940. (setq org-map-continue-from nil)
  12941. (catch :skip
  12942. (setq todo
  12943. ;; TODO: is the 1-2 difference a bug?
  12944. (when (match-end 1) (org-match-string-no-properties 2))
  12945. tags (when (match-end 4) (org-match-string-no-properties 4)))
  12946. (goto-char (setq lspos (match-beginning 0)))
  12947. (setq level (org-reduced-level (org-outline-level))
  12948. category (org-get-category))
  12949. (when (eq action 'agenda)
  12950. (setq ts-date-pair (org-agenda-entry-get-agenda-timestamp (point))
  12951. ts-date (car ts-date-pair)
  12952. ts-date-type (cdr ts-date-pair)))
  12953. (setq i llast llast level)
  12954. ;; remove tag lists from same and sublevels
  12955. (while (>= i level)
  12956. (when (setq entry (assoc i tags-alist))
  12957. (setq tags-alist (delete entry tags-alist)))
  12958. (setq i (1- i)))
  12959. ;; add the next tags
  12960. (when tags
  12961. (setq tags (org-split-string tags ":")
  12962. tags-alist
  12963. (cons (cons level tags) tags-alist)))
  12964. ;; compile tags for current headline
  12965. (setq tags-list
  12966. (if org-use-tag-inheritance
  12967. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  12968. tags)
  12969. org-scanner-tags tags-list)
  12970. (when org-use-tag-inheritance
  12971. (setcdr (car tags-alist)
  12972. (mapcar (lambda (x)
  12973. (setq x (copy-sequence x))
  12974. (org-add-prop-inherited x))
  12975. (cdar tags-alist))))
  12976. (when (and tags org-use-tag-inheritance
  12977. (or (not (eq t org-use-tag-inheritance))
  12978. org-tags-exclude-from-inheritance))
  12979. ;; selective inheritance, remove uninherited ones
  12980. (setcdr (car tags-alist)
  12981. (org-remove-uninherited-tags (cdar tags-alist))))
  12982. (when (and
  12983. ;; eval matcher only when the todo condition is OK
  12984. (and (or (not todo-only) (member todo org-not-done-keywords))
  12985. (let ((case-fold-search t) (org-trust-scanner-tags t))
  12986. (eval matcher)))
  12987. ;; Call the skipper, but return t if it does not skip,
  12988. ;; so that the `and' form continues evaluating
  12989. (progn
  12990. (unless (eq action 'sparse-tree) (org-agenda-skip))
  12991. t)
  12992. ;; Check if timestamps are deselecting this entry
  12993. (or (not todo-only)
  12994. (and (member todo org-not-done-keywords)
  12995. (or (not org-agenda-tags-todo-honor-ignore-options)
  12996. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item))))))
  12997. ;; select this headline
  12998. (cond
  12999. ((eq action 'sparse-tree)
  13000. (and org-highlight-sparse-tree-matches
  13001. (org-get-heading) (match-end 0)
  13002. (org-highlight-new-match
  13003. (match-beginning 1) (match-end 1)))
  13004. (org-show-context 'tags-tree))
  13005. ((eq action 'agenda)
  13006. (setq txt (org-agenda-format-item
  13007. ""
  13008. (concat
  13009. (if (eq org-tags-match-list-sublevels 'indented)
  13010. (make-string (1- level) ?.) "")
  13011. (org-get-heading))
  13012. (make-string level ?\s)
  13013. category
  13014. tags-list)
  13015. priority (org-get-priority txt))
  13016. (goto-char lspos)
  13017. (setq marker (org-agenda-new-marker))
  13018. (org-add-props txt props
  13019. 'org-marker marker 'org-hd-marker marker 'org-category category
  13020. 'todo-state todo
  13021. 'ts-date ts-date
  13022. 'priority priority
  13023. 'type (concat "tagsmatch" ts-date-type))
  13024. (push txt rtn))
  13025. ((functionp action)
  13026. (setq org-map-continue-from nil)
  13027. (save-excursion
  13028. (setq rtn1 (funcall action))
  13029. (push rtn1 rtn)))
  13030. (t (user-error "Invalid action")))
  13031. ;; if we are to skip sublevels, jump to end of subtree
  13032. (unless org-tags-match-list-sublevels
  13033. (org-end-of-subtree t)
  13034. (backward-char 1))))
  13035. ;; Get the correct position from where to continue
  13036. (if org-map-continue-from
  13037. (goto-char org-map-continue-from)
  13038. (and (= (point) lspos) (end-of-line 1)))))
  13039. (when (and (eq action 'sparse-tree)
  13040. (not org-sparse-tree-open-archived-trees))
  13041. (org-hide-archived-subtrees (point-min) (point-max)))
  13042. (nreverse rtn)))
  13043. (defun org-remove-uninherited-tags (tags)
  13044. "Remove all tags that are not inherited from the list TAGS."
  13045. (cond
  13046. ((eq org-use-tag-inheritance t)
  13047. (if org-tags-exclude-from-inheritance
  13048. (org-delete-all org-tags-exclude-from-inheritance tags)
  13049. tags))
  13050. ((not org-use-tag-inheritance) nil)
  13051. ((stringp org-use-tag-inheritance)
  13052. (delq nil (mapcar
  13053. (lambda (x)
  13054. (if (and (string-match org-use-tag-inheritance x)
  13055. (not (member x org-tags-exclude-from-inheritance)))
  13056. x nil))
  13057. tags)))
  13058. ((listp org-use-tag-inheritance)
  13059. (delq nil (mapcar
  13060. (lambda (x)
  13061. (if (member x org-use-tag-inheritance) x nil))
  13062. tags)))))
  13063. (defun org-match-sparse-tree (&optional todo-only match)
  13064. "Create a sparse tree according to tags string MATCH.
  13065. MATCH can contain positive and negative selection of tags, like
  13066. \"+WORK+URGENT-WITHBOSS\".
  13067. If optional argument TODO-ONLY is non-nil, only select lines that are
  13068. also TODO lines."
  13069. (interactive "P")
  13070. (org-agenda-prepare-buffers (list (current-buffer)))
  13071. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  13072. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  13073. (defvar org-cached-props nil)
  13074. (defun org-cached-entry-get (pom property)
  13075. (if (or (eq t org-use-property-inheritance)
  13076. (and (stringp org-use-property-inheritance)
  13077. (let ((case-fold-search t))
  13078. (org-string-match-p org-use-property-inheritance property)))
  13079. (and (listp org-use-property-inheritance)
  13080. (member-ignore-case property org-use-property-inheritance)))
  13081. ;; Caching is not possible, check it directly.
  13082. (org-entry-get pom property 'inherit)
  13083. ;; Get all properties, so we can do complicated checks easily.
  13084. (cdr (assoc-string property
  13085. (or org-cached-props
  13086. (setq org-cached-props (org-entry-properties pom)))
  13087. t))))
  13088. (defun org-global-tags-completion-table (&optional files)
  13089. "Return the list of all tags in all agenda buffer/files.
  13090. Optional FILES argument is a list of files which can be used
  13091. instead of the agenda files."
  13092. (save-excursion
  13093. (org-uniquify
  13094. (delq nil
  13095. (apply 'append
  13096. (mapcar
  13097. (lambda (file)
  13098. (set-buffer (find-file-noselect file))
  13099. (append (org-get-buffer-tags)
  13100. (mapcar (lambda (x) (if (stringp (car-safe x))
  13101. (list (car-safe x)) nil))
  13102. org-tag-alist)))
  13103. (if (and files (car files))
  13104. files
  13105. (org-agenda-files))))))))
  13106. (defun org-make-tags-matcher (match)
  13107. "Create the TAGS/TODO matcher form for the selection string MATCH.
  13108. The variable `todo-only' is scoped dynamically into this function.
  13109. It will be set to t if the matcher restricts matching to TODO entries,
  13110. otherwise will not be touched.
  13111. Returns a cons of the selection string MATCH and the constructed
  13112. lisp form implementing the matcher. The matcher is to be evaluated
  13113. at an Org entry, with point on the headline, and returns t if the
  13114. entry matches the selection string MATCH. The returned lisp form
  13115. references two variables with information about the entry, which
  13116. must be bound around the form's evaluation: todo, the TODO keyword
  13117. at the entry (or nil of none); and tags-list, the list of all tags
  13118. at the entry including inherited ones. Additionally, the category
  13119. of the entry (if any) must be specified as the text property
  13120. `org-category' on the headline.
  13121. See also `org-scan-tags'.
  13122. "
  13123. (declare (special todo-only))
  13124. (unless (boundp 'todo-only)
  13125. (error "`org-make-tags-matcher' expects todo-only to be scoped in"))
  13126. (unless match
  13127. ;; Get a new match request, with completion against the global
  13128. ;; tags table and the local tags in current buffer
  13129. (let ((org-last-tags-completion-table
  13130. (org-uniquify
  13131. (delq nil (append (org-get-buffer-tags)
  13132. (org-global-tags-completion-table))))))
  13133. (setq match (org-completing-read-no-i
  13134. "Match: " 'org-tags-completion-function nil nil nil
  13135. 'org-tags-history))))
  13136. ;; Parse the string and create a lisp form
  13137. (let ((match0 match)
  13138. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\(\\(?:[[:alnum:]_]+\\(?:\\\\-\\)*\\)+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@#%]+\\)"))
  13139. minus tag mm
  13140. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  13141. orterms term orlist re-p str-p level-p level-op time-p
  13142. prop-p pn pv po gv rest (start 0) (ss 0))
  13143. ;; Expand group tags
  13144. (setq match (org-tags-expand match))
  13145. ;; Check if there is a TODO part of this match, which would be the
  13146. ;; part after a "/". TO make sure that this slash is not part of
  13147. ;; a property value to be matched against, we also check that there
  13148. ;; is no " after that slash.
  13149. ;; First, find the last slash
  13150. (while (string-match "/+" match ss)
  13151. (setq start (match-beginning 0) ss (match-end 0)))
  13152. (if (and (string-match "/+" match start)
  13153. (not (save-match-data (string-match "\"" match start))))
  13154. ;; match contains also a todo-matching request
  13155. (progn
  13156. (setq tagsmatch (substring match 0 (match-beginning 0))
  13157. todomatch (substring match (match-end 0)))
  13158. (when (string-match "^!" todomatch)
  13159. (setq todo-only t todomatch (substring todomatch 1)))
  13160. (when (string-match "^\\s-*$" todomatch)
  13161. (setq todomatch nil)))
  13162. ;; only matching tags
  13163. (setq tagsmatch match todomatch nil))
  13164. ;; Make the tags matcher
  13165. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  13166. (setq tagsmatcher t)
  13167. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  13168. (while (setq term (pop orterms))
  13169. (while (and (equal (substring term -1) "\\") orterms)
  13170. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  13171. (while (string-match re term)
  13172. (setq rest (substring term (match-end 0))
  13173. minus (and (match-end 1)
  13174. (equal (match-string 1 term) "-"))
  13175. tag (save-match-data (replace-regexp-in-string
  13176. "\\\\-" "-"
  13177. (match-string 2 term)))
  13178. re-p (equal (string-to-char tag) ?{)
  13179. level-p (match-end 4)
  13180. prop-p (match-end 5)
  13181. mm (cond
  13182. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  13183. (level-p
  13184. (setq level-op (org-op-to-function (match-string 3 term)))
  13185. `(,level-op level ,(string-to-number
  13186. (match-string 4 term))))
  13187. (prop-p
  13188. (setq pn (match-string 5 term)
  13189. po (match-string 6 term)
  13190. pv (match-string 7 term)
  13191. re-p (equal (string-to-char pv) ?{)
  13192. str-p (equal (string-to-char pv) ?\")
  13193. time-p (save-match-data
  13194. (string-match "^\"[[<].*[]>]\"$" pv))
  13195. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  13196. (when time-p (setq pv (org-matcher-time pv)))
  13197. (setq po (org-op-to-function po (if time-p 'time str-p)))
  13198. (cond
  13199. ((equal pn "CATEGORY")
  13200. (setq gv '(get-text-property (point) 'org-category)))
  13201. ((equal pn "TODO")
  13202. (setq gv 'todo))
  13203. (t
  13204. (setq gv `(org-cached-entry-get nil ,pn))))
  13205. (if re-p
  13206. (if (eq po 'org<>)
  13207. `(not (string-match ,pv (or ,gv "")))
  13208. `(string-match ,pv (or ,gv "")))
  13209. (if str-p
  13210. `(,po (or ,gv "") ,pv)
  13211. `(,po (string-to-number (or ,gv ""))
  13212. ,(string-to-number pv) ))))
  13213. (t `(member ,tag tags-list)))
  13214. mm (if minus (list 'not mm) mm)
  13215. term rest)
  13216. (push mm tagsmatcher))
  13217. (push (if (> (length tagsmatcher) 1)
  13218. (cons 'and tagsmatcher)
  13219. (car tagsmatcher))
  13220. orlist)
  13221. (setq tagsmatcher nil))
  13222. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  13223. (setq tagsmatcher
  13224. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  13225. ;; Make the todo matcher
  13226. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  13227. (setq todomatcher t)
  13228. (setq orterms (org-split-string todomatch "|") orlist nil)
  13229. (dolist (term orterms)
  13230. (while (string-match re term)
  13231. (setq minus (and (match-end 1)
  13232. (equal (match-string 1 term) "-"))
  13233. kwd (match-string 2 term)
  13234. re-p (equal (string-to-char kwd) ?{)
  13235. term (substring term (match-end 0))
  13236. mm (if re-p
  13237. `(string-match ,(substring kwd 1 -1) todo)
  13238. (list 'equal 'todo kwd))
  13239. mm (if minus (list 'not mm) mm))
  13240. (push mm todomatcher))
  13241. (push (if (> (length todomatcher) 1)
  13242. (cons 'and todomatcher)
  13243. (car todomatcher))
  13244. orlist)
  13245. (setq todomatcher nil))
  13246. (setq todomatcher (if (> (length orlist) 1)
  13247. (cons 'or orlist) (car orlist))))
  13248. ;; Return the string and lisp forms of the matcher
  13249. (setq matcher (if todomatcher
  13250. (list 'and tagsmatcher todomatcher)
  13251. tagsmatcher))
  13252. (when todo-only
  13253. (setq matcher (list 'and '(member todo org-not-done-keywords)
  13254. matcher)))
  13255. (cons match0 matcher)))
  13256. (defun org-tags-expand (match &optional single-as-list downcased tags-already-expanded)
  13257. "Expand group tags in MATCH.
  13258. This replaces every group tag in MATCH with a regexp tag search.
  13259. For example, a group tag \"Work\" defined as { Work : Lab Conf }
  13260. will be replaced like this:
  13261. Work => {\\<\\(?:Work\\|Lab\\|Conf\\)\\>}
  13262. +Work => +{\\<\\(?:Work\\|Lab\\|Conf\\)\\>}
  13263. -Work => -{\\<\\(?:Work\\|Lab\\|Conf\\)\\>}
  13264. Replacing by a regexp preserves the structure of the match.
  13265. E.g., this expansion
  13266. Work|Home => {\\(?:Work\\|Lab\\|Conf\\}|Home
  13267. will match anything tagged with \"Lab\" and \"Home\", or tagged
  13268. with \"Conf\" and \"Home\" or tagged with \"Work\" and \"home\".
  13269. A group tag in MATCH can contain regular expressions of its own.
  13270. For example, a group tag \"Proj\" defined as { Proj : {P@.+} }
  13271. will be replaced like this:
  13272. Proj => {\\<\\(?:Proj\\)\\>\\|P@.+}
  13273. When the optional argument SINGLE-AS-LIST is non-nil, MATCH is
  13274. assumed to be a single group tag, and the function will return
  13275. the list of tags in this group.
  13276. When DOWNCASE is non-nil, expand downcased TAGS."
  13277. (if org-group-tags
  13278. (let* ((case-fold-search t)
  13279. (stable org-mode-syntax-table)
  13280. (taggroups (or org-tag-groups-alist-for-agenda org-tag-groups-alist))
  13281. (taggroups (if downcased
  13282. (mapcar (lambda (tg) (mapcar #'downcase tg))
  13283. taggroups)
  13284. taggroups))
  13285. (taggroups-keys (mapcar #'car taggroups))
  13286. (return-match (if downcased (downcase match) match))
  13287. (count 0)
  13288. (work-already-expanded tags-already-expanded)
  13289. regexps-in-match tags-in-group regexp-in-group regexp-in-group-escaped)
  13290. ;; @ and _ are allowed as word-components in tags.
  13291. (modify-syntax-entry ?@ "w" stable)
  13292. (modify-syntax-entry ?_ "w" stable)
  13293. ;; Temporarily replace regexp-expressions in the match-expression.
  13294. (while (string-match "{.+?}" return-match)
  13295. (incf count)
  13296. (push (match-string 0 return-match) regexps-in-match)
  13297. (setq return-match (replace-match (format "<%d>" count) t nil return-match)))
  13298. (while (and taggroups-keys
  13299. (with-syntax-table stable
  13300. (string-match
  13301. (concat "\\(?1:[+-]?\\)\\(?2:\\<"
  13302. (regexp-opt taggroups-keys) "\\>\\)")
  13303. return-match)))
  13304. (let* ((dir (match-string 1 return-match))
  13305. (tag (match-string 2 return-match))
  13306. (tag (if downcased (downcase tag) tag)))
  13307. (unless (or (get-text-property 0 'grouptag (match-string 2 return-match))
  13308. (member tag work-already-expanded))
  13309. (setq tags-in-group (assoc tag taggroups))
  13310. (push tag work-already-expanded)
  13311. ;; Recursively expand each tag in the group, if the tag hasn't
  13312. ;; already been expanded. Restore the match-data after all recursive calls.
  13313. (save-match-data
  13314. (let (tags-expanded)
  13315. (dolist (x (cdr tags-in-group))
  13316. (if (and (member x taggroups-keys)
  13317. (not (member x work-already-expanded)))
  13318. (setq tags-expanded
  13319. (delete-dups
  13320. (append
  13321. (org-tags-expand x t downcased
  13322. work-already-expanded)
  13323. tags-expanded)))
  13324. (setq tags-expanded
  13325. (append (list x) tags-expanded)))
  13326. (setq work-already-expanded
  13327. (delete-dups
  13328. (append tags-expanded
  13329. work-already-expanded))))
  13330. (setq tags-in-group
  13331. (delete-dups (cons (car tags-in-group)
  13332. tags-expanded)))))
  13333. ;; Filter tag-regexps from tags.
  13334. (setq regexp-in-group-escaped
  13335. (delq nil (mapcar (lambda (x)
  13336. (if (stringp x)
  13337. (and (equal "{" (substring x 0 1))
  13338. (equal "}" (substring x -1))
  13339. x)
  13340. x))
  13341. tags-in-group))
  13342. regexp-in-group
  13343. (mapcar (lambda (x)
  13344. (substring x 1 -1))
  13345. regexp-in-group-escaped)
  13346. tags-in-group
  13347. (delq nil (mapcar (lambda (x)
  13348. (if (stringp x)
  13349. (and (not (equal "{" (substring x 0 1)))
  13350. (not (equal "}" (substring x -1)))
  13351. x)
  13352. x))
  13353. tags-in-group)))
  13354. ;; If single-as-list, do no more in the while-loop.
  13355. (if (not single-as-list)
  13356. (progn
  13357. (when regexp-in-group
  13358. (setq regexp-in-group
  13359. (concat "\\|"
  13360. (mapconcat 'identity regexp-in-group
  13361. "\\|"))))
  13362. (setq tags-in-group
  13363. (concat dir
  13364. "{\\<"
  13365. (regexp-opt tags-in-group)
  13366. "\\>"
  13367. regexp-in-group
  13368. "}"))
  13369. (when (stringp tags-in-group)
  13370. (org-add-props tags-in-group '(grouptag t)))
  13371. (setq return-match
  13372. (replace-match tags-in-group t t return-match)))
  13373. (setq tags-in-group
  13374. (append regexp-in-group-escaped tags-in-group))))
  13375. (setq taggroups-keys (delete tag taggroups-keys))))
  13376. ;; Add the regular expressions back into the match-expression again.
  13377. (while regexps-in-match
  13378. (setq return-match (replace-regexp-in-string (format "<%d>" count)
  13379. (pop regexps-in-match)
  13380. return-match t t))
  13381. (decf count))
  13382. (if single-as-list
  13383. (if tags-in-group tags-in-group (list return-match))
  13384. return-match))
  13385. (if single-as-list
  13386. (list (if downcased (downcase match) match))
  13387. match)))
  13388. (defun org-op-to-function (op &optional stringp)
  13389. "Turn an operator into the appropriate function."
  13390. (setq op
  13391. (cond
  13392. ((equal op "<" ) '(< string< org-time<))
  13393. ((equal op ">" ) '(> org-string> org-time>))
  13394. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  13395. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  13396. ((member op '("=" "==")) '(= string= org-time=))
  13397. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  13398. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  13399. (defun org<> (a b) (not (= a b)))
  13400. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  13401. (defun org-string>= (a b) (not (string< a b)))
  13402. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  13403. (defun org-string<> (a b) (not (string= a b)))
  13404. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  13405. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  13406. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  13407. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  13408. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  13409. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  13410. (defun org-2ft (s)
  13411. "Convert S to a floating point time.
  13412. If S is already a number, just return it. If it is a string, parse
  13413. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  13414. (cond
  13415. ((numberp s) s)
  13416. ((stringp s)
  13417. (condition-case nil
  13418. (float-time (apply 'encode-time (org-parse-time-string s)))
  13419. (error 0.)))
  13420. (t 0.)))
  13421. (defun org-time-today ()
  13422. "Time in seconds today at 0:00.
  13423. Returns the float number of seconds since the beginning of the
  13424. epoch to the beginning of today (00:00)."
  13425. (float-time (apply 'encode-time
  13426. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  13427. (defun org-matcher-time (s)
  13428. "Interpret a time comparison value."
  13429. (save-match-data
  13430. (cond
  13431. ((string= s "<now>") (float-time))
  13432. ((string= s "<today>") (org-time-today))
  13433. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  13434. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  13435. ((string-match "^<\\([-+][0-9]+\\)\\([hdwmy]\\)>$" s)
  13436. (+ (org-time-today)
  13437. (* (string-to-number (match-string 1 s))
  13438. (cdr (assoc (match-string 2 s)
  13439. '(("d" . 86400.0) ("w" . 604800.0)
  13440. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  13441. (t (org-2ft s)))))
  13442. (defun org-match-any-p (re list)
  13443. "Does re match any element of list?"
  13444. (setq list (mapcar (lambda (x) (string-match re x)) list))
  13445. (delq nil list))
  13446. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  13447. (defvar org-tags-overlay (make-overlay 1 1))
  13448. (org-detach-overlay org-tags-overlay)
  13449. (defun org-get-local-tags-at (&optional pos)
  13450. "Get a list of tags defined in the current headline."
  13451. (org-get-tags-at pos 'local))
  13452. (defun org-get-local-tags ()
  13453. "Get a list of tags defined in the current headline."
  13454. (org-get-tags-at nil 'local))
  13455. (defun org-get-tags-at (&optional pos local)
  13456. "Get a list of all headline tags applicable at POS.
  13457. POS defaults to point. If tags are inherited, the list contains
  13458. the targets in the same sequence as the headlines appear, i.e.
  13459. the tags of the current headline come last.
  13460. When LOCAL is non-nil, only return tags from the current headline,
  13461. ignore inherited ones."
  13462. (interactive)
  13463. (if (and org-trust-scanner-tags
  13464. (or (not pos) (equal pos (point)))
  13465. (not local))
  13466. org-scanner-tags
  13467. (let (tags ltags lastpos parent)
  13468. (save-excursion
  13469. (save-restriction
  13470. (widen)
  13471. (goto-char (or pos (point)))
  13472. (save-match-data
  13473. (catch 'done
  13474. (condition-case nil
  13475. (progn
  13476. (org-back-to-heading t)
  13477. (while (not (equal lastpos (point)))
  13478. (setq lastpos (point))
  13479. (when (looking-at
  13480. (org-re "[^\r\n]+?:\\([[:alnum:]_@#%:]+\\):[ \t]*$"))
  13481. (setq ltags (org-split-string
  13482. (org-match-string-no-properties 1) ":"))
  13483. (when parent
  13484. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  13485. (setq tags (append
  13486. (if parent
  13487. (org-remove-uninherited-tags ltags)
  13488. ltags)
  13489. tags)))
  13490. (or org-use-tag-inheritance (throw 'done t))
  13491. (when local (throw 'done t))
  13492. (or (org-up-heading-safe) (error nil))
  13493. (setq parent t)))
  13494. (error nil)))))
  13495. (if local
  13496. tags
  13497. (reverse (delete-dups
  13498. (reverse (append
  13499. (org-remove-uninherited-tags
  13500. org-file-tags)
  13501. tags)))))))))
  13502. (defun org-add-prop-inherited (s)
  13503. (add-text-properties 0 (length s) '(inherited t) s)
  13504. s)
  13505. (defun org-toggle-tag (tag &optional onoff)
  13506. "Toggle the tag TAG for the current line.
  13507. If ONOFF is `on' or `off', don't toggle but set to this state."
  13508. (let (res current)
  13509. (save-excursion
  13510. (org-back-to-heading t)
  13511. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t]*$")
  13512. (point-at-eol) t)
  13513. (progn
  13514. (setq current (match-string 1))
  13515. (replace-match ""))
  13516. (setq current ""))
  13517. (setq current (nreverse (org-split-string current ":")))
  13518. (cond
  13519. ((eq onoff 'on)
  13520. (setq res t)
  13521. (or (member tag current) (push tag current)))
  13522. ((eq onoff 'off)
  13523. (or (not (member tag current)) (setq current (delete tag current))))
  13524. (t (if (member tag current)
  13525. (setq current (delete tag current))
  13526. (setq res t)
  13527. (push tag current))))
  13528. (end-of-line 1)
  13529. (if current
  13530. (progn
  13531. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  13532. (org-set-tags nil t))
  13533. (delete-horizontal-space))
  13534. (run-hooks 'org-after-tags-change-hook))
  13535. res))
  13536. (defun org-align-tags-here (to-col)
  13537. ;; Assumes that this is a headline
  13538. "Align tags on the current headline to TO-COL."
  13539. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  13540. (beginning-of-line 1)
  13541. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13542. (< pos (match-beginning 2)))
  13543. (progn
  13544. (setq tags-l (- (match-end 2) (match-beginning 2)))
  13545. (goto-char (match-beginning 1))
  13546. (insert " ")
  13547. (delete-region (point) (1+ (match-beginning 2)))
  13548. (setq ncol (max (current-column)
  13549. (1+ col)
  13550. (if (> to-col 0)
  13551. to-col
  13552. (- (abs to-col) tags-l))))
  13553. (setq p (point))
  13554. (insert (make-string (- ncol (current-column)) ?\ ))
  13555. (setq ncol (current-column))
  13556. (when indent-tabs-mode (tabify p (point-at-eol)))
  13557. (org-move-to-column (min ncol col)))
  13558. (goto-char pos))))
  13559. (defun org-set-tags-command (&optional arg just-align)
  13560. "Call the set-tags command for the current entry."
  13561. (interactive "P")
  13562. (if (or (org-at-heading-p) (and arg (org-before-first-heading-p)))
  13563. (org-set-tags arg just-align)
  13564. (save-excursion
  13565. (unless (and (org-region-active-p)
  13566. org-loop-over-headlines-in-active-region)
  13567. (org-back-to-heading t))
  13568. (org-set-tags arg just-align))))
  13569. (defun org-set-tags-to (data)
  13570. "Set the tags of the current entry to DATA, replacing the current tags.
  13571. DATA may be a tags string like :aa:bb:cc:, or a list of tags.
  13572. If DATA is nil or the empty string, any tags will be removed."
  13573. (interactive "sTags: ")
  13574. (setq data
  13575. (cond
  13576. ((eq data nil) "")
  13577. ((equal data "") "")
  13578. ((stringp data)
  13579. (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
  13580. ":"))
  13581. ((listp data)
  13582. (concat ":" (mapconcat 'identity data ":") ":"))))
  13583. (when data
  13584. (save-excursion
  13585. (org-back-to-heading t)
  13586. (when (looking-at org-complex-heading-regexp)
  13587. (if (match-end 5)
  13588. (progn
  13589. (goto-char (match-beginning 5))
  13590. (insert data)
  13591. (delete-region (point) (point-at-eol))
  13592. (org-set-tags nil 'align))
  13593. (goto-char (point-at-eol))
  13594. (insert " " data)
  13595. (org-set-tags nil 'align)))
  13596. (beginning-of-line 1)
  13597. (when (looking-at ".*?\\([ \t]+\\)$")
  13598. (delete-region (match-beginning 1) (match-end 1))))))
  13599. (defun org-align-all-tags ()
  13600. "Align the tags i all headings."
  13601. (interactive)
  13602. (save-excursion
  13603. (or (ignore-errors (org-back-to-heading t))
  13604. (outline-next-heading))
  13605. (if (org-at-heading-p)
  13606. (org-set-tags t)
  13607. (message "No headings"))))
  13608. (defvar org-indent-indentation-per-level)
  13609. (defun org-set-tags (&optional arg just-align)
  13610. "Set the tags for the current headline.
  13611. With prefix ARG, realign all tags in headings in the current buffer.
  13612. When JUST-ALIGN is non-nil, only align tags."
  13613. (interactive "P")
  13614. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  13615. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  13616. 'region-start-level
  13617. 'region))
  13618. org-loop-over-headlines-in-active-region)
  13619. (org-map-entries
  13620. ;; We don't use ARG and JUST-ALIGN here because these args
  13621. ;; are not useful when looping over headlines.
  13622. #'org-set-tags
  13623. org-loop-over-headlines-in-active-region
  13624. cl
  13625. '(when (outline-invisible-p) (org-end-of-subtree nil t))))
  13626. (let ((org-setting-tags t))
  13627. (if arg
  13628. (save-excursion
  13629. (goto-char (point-min))
  13630. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  13631. (while (re-search-forward org-outline-regexp-bol nil t)
  13632. (org-set-tags nil t)
  13633. (end-of-line)))
  13634. (message "All tags realigned to column %d" org-tags-column))
  13635. (let* ((current (org-get-tags-string))
  13636. (col (current-column))
  13637. (tags
  13638. (if just-align current
  13639. ;; Get a new set of tags from the user.
  13640. (save-excursion
  13641. (let* ((table
  13642. (setq
  13643. org-last-tags-completion-table
  13644. (append
  13645. org-tag-persistent-alist
  13646. (or org-tag-alist (org-get-buffer-tags))
  13647. (and
  13648. org-complete-tags-always-offer-all-agenda-tags
  13649. (org-global-tags-completion-table
  13650. (org-agenda-files))))))
  13651. (current-tags (org-split-string current ":"))
  13652. (inherited-tags
  13653. (nreverse (nthcdr (length current-tags)
  13654. (nreverse (org-get-tags-at))))))
  13655. (replace-regexp-in-string
  13656. "\\([-+&]+\\|,\\)"
  13657. ":"
  13658. (if (or (eq t org-use-fast-tag-selection)
  13659. (and org-use-fast-tag-selection
  13660. (delq nil (mapcar #'cdr table))))
  13661. (org-fast-tag-selection
  13662. current-tags inherited-tags table
  13663. (and org-fast-tag-selection-include-todo
  13664. org-todo-key-alist))
  13665. (let ((org-add-colon-after-tag-completion
  13666. (< 1 (length table))))
  13667. (org-trim
  13668. (completing-read
  13669. "Tags: "
  13670. #'org-tags-completion-function
  13671. nil nil current 'org-tags-history))))))))))
  13672. (when org-tags-sort-function
  13673. (setq tags
  13674. (mapconcat
  13675. #'identity
  13676. (sort (org-split-string tags (org-re "[^[:alnum:]_@#%]+"))
  13677. org-tags-sort-function)
  13678. ":")))
  13679. (if (not (org-string-nw-p tags)) (setq tags "")
  13680. (unless (string-match ":\\'" tags) (setq tags (concat tags ":")))
  13681. (unless (string-match "\\`:" tags) (setq tags (concat ":" tags))))
  13682. ;; Insert new tags at the correct column
  13683. (beginning-of-line)
  13684. (let ((level (if (looking-at org-outline-regexp)
  13685. (- (match-end 0) (point) 1)
  13686. 1)))
  13687. (cond
  13688. ((and (equal current "") (equal tags "")))
  13689. ((re-search-forward
  13690. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  13691. (line-end-position)
  13692. t)
  13693. (if (equal tags "") (replace-match "" t t)
  13694. (goto-char (match-beginning 0))
  13695. (let* ((c0 (current-column))
  13696. ;; Compute offset for the case of org-indent-mode
  13697. ;; active.
  13698. (di (if (org-bound-and-true-p org-indent-mode)
  13699. (* (1- org-indent-indentation-per-level)
  13700. (1- level))
  13701. 0))
  13702. (p0 (if (eq (char-before) ?*) (1+ (point)) (point)))
  13703. (tc (+ org-tags-column
  13704. (if (> org-tags-column 0) (- di) di)))
  13705. (c1 (max (1+ c0)
  13706. (if (> tc 0) tc
  13707. (- (- tc) (string-width tags)))))
  13708. (rpl (concat (make-string (max 0 (- c1 c0)) ?\s) tags)))
  13709. (replace-match rpl t t)
  13710. (when (and (not (featurep 'xemacs)) indent-tabs-mode)
  13711. (tabify p0 (point))))))
  13712. (t (error "Tags alignment failed"))))
  13713. (org-move-to-column col))
  13714. (unless just-align (run-hooks 'org-after-tags-change-hook))))))
  13715. (defun org-change-tag-in-region (beg end tag off)
  13716. "Add or remove TAG for each entry in the region.
  13717. This works in the agenda, and also in an org-mode buffer."
  13718. (interactive
  13719. (list (region-beginning) (region-end)
  13720. (let ((org-last-tags-completion-table
  13721. (if (derived-mode-p 'org-mode)
  13722. (org-uniquify
  13723. (delq nil (append (org-get-buffer-tags)
  13724. (org-global-tags-completion-table))))
  13725. (org-global-tags-completion-table))))
  13726. (org-icompleting-read
  13727. "Tag: " 'org-tags-completion-function nil nil nil
  13728. 'org-tags-history))
  13729. (progn
  13730. (message "[s]et or [r]emove? ")
  13731. (equal (read-char-exclusive) ?r))))
  13732. (when (fboundp 'deactivate-mark) (deactivate-mark))
  13733. (let ((agendap (equal major-mode 'org-agenda-mode))
  13734. l1 l2 m buf pos newhead (cnt 0))
  13735. (goto-char end)
  13736. (setq l2 (1- (org-current-line)))
  13737. (goto-char beg)
  13738. (setq l1 (org-current-line))
  13739. (loop for l from l1 to l2 do
  13740. (org-goto-line l)
  13741. (setq m (get-text-property (point) 'org-hd-marker))
  13742. (when (or (and (derived-mode-p 'org-mode) (org-at-heading-p))
  13743. (and agendap m))
  13744. (setq buf (if agendap (marker-buffer m) (current-buffer))
  13745. pos (if agendap m (point)))
  13746. (with-current-buffer buf
  13747. (save-excursion
  13748. (save-restriction
  13749. (goto-char pos)
  13750. (setq cnt (1+ cnt))
  13751. (org-toggle-tag tag (if off 'off 'on))
  13752. (setq newhead (org-get-heading)))))
  13753. (and agendap (org-agenda-change-all-lines newhead m))))
  13754. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  13755. (defun org-tags-completion-function (string predicate &optional flag)
  13756. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  13757. (confirm (lambda (x) (stringp (car x)))))
  13758. (if (string-match "^\\(.*[-+:&,|]\\)\\([^-+:&,|]*\\)$" string)
  13759. (setq s1 (match-string 1 string)
  13760. s2 (match-string 2 string))
  13761. (setq s1 "" s2 string))
  13762. (cond
  13763. ((eq flag nil)
  13764. ;; try completion
  13765. (setq rtn (try-completion s2 ctable confirm))
  13766. (when (stringp rtn)
  13767. (setq rtn
  13768. (concat s1 s2 (substring rtn (length s2))
  13769. (if (and org-add-colon-after-tag-completion
  13770. (assoc rtn ctable))
  13771. ":" ""))))
  13772. rtn)
  13773. ((eq flag t)
  13774. ;; all-completions
  13775. (all-completions s2 ctable confirm))
  13776. ((eq flag 'lambda)
  13777. ;; exact match?
  13778. (assoc s2 ctable)))))
  13779. (defun org-fast-tag-insert (kwd tags face &optional end)
  13780. "Insert KDW, and the TAGS, the latter with face FACE.
  13781. Also insert END."
  13782. (insert (format "%-12s" (concat kwd ":"))
  13783. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  13784. (or end "")))
  13785. (defun org-fast-tag-show-exit (flag)
  13786. (save-excursion
  13787. (org-goto-line 3)
  13788. (when (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  13789. (replace-match ""))
  13790. (when flag
  13791. (end-of-line 1)
  13792. (org-move-to-column (- (window-width) 19) t)
  13793. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  13794. (defun org-set-current-tags-overlay (current prefix)
  13795. "Add an overlay to CURRENT tag with PREFIX."
  13796. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  13797. (if (featurep 'xemacs)
  13798. (org-overlay-display org-tags-overlay (concat prefix s)
  13799. 'secondary-selection)
  13800. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  13801. (org-overlay-display org-tags-overlay (concat prefix s)))))
  13802. (defvar org-last-tag-selection-key nil)
  13803. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  13804. "Fast tag selection with single keys.
  13805. CURRENT is the current list of tags in the headline, INHERITED is the
  13806. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  13807. possibly with grouping information. TODO-TABLE is a similar table with
  13808. TODO keywords, should these have keys assigned to them.
  13809. If the keys are nil, a-z are automatically assigned.
  13810. Returns the new tags string, or nil to not change the current settings."
  13811. (let* ((fulltable (append table todo-table))
  13812. (maxlen (apply 'max (mapcar
  13813. (lambda (x)
  13814. (if (stringp (car x)) (string-width (car x)) 0))
  13815. fulltable)))
  13816. (buf (current-buffer))
  13817. (expert (eq org-fast-tag-selection-single-key 'expert))
  13818. (buffer-tags nil)
  13819. (fwidth (+ maxlen 3 1 3))
  13820. (ncol (/ (- (window-width) 4) fwidth))
  13821. (i-face 'org-done)
  13822. (c-face 'org-todo)
  13823. tg cnt e c char c1 c2 ntable tbl rtn
  13824. ov-start ov-end ov-prefix
  13825. (exit-after-next org-fast-tag-selection-single-key)
  13826. (done-keywords org-done-keywords)
  13827. groups ingroup intaggroup)
  13828. (save-excursion
  13829. (beginning-of-line 1)
  13830. (if (looking-at
  13831. (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13832. (setq ov-start (match-beginning 1)
  13833. ov-end (match-end 1)
  13834. ov-prefix "")
  13835. (setq ov-start (1- (point-at-eol))
  13836. ov-end (1+ ov-start))
  13837. (skip-chars-forward "^\n\r")
  13838. (setq ov-prefix
  13839. (concat
  13840. (buffer-substring (1- (point)) (point))
  13841. (if (> (current-column) org-tags-column)
  13842. " "
  13843. (make-string (- org-tags-column (current-column)) ?\ ))))))
  13844. (move-overlay org-tags-overlay ov-start ov-end)
  13845. (save-window-excursion
  13846. (if expert
  13847. (set-buffer (get-buffer-create " *Org tags*"))
  13848. (delete-other-windows)
  13849. (set-window-buffer (split-window-vertically) (get-buffer-create " *Org tags*"))
  13850. (org-switch-to-buffer-other-window " *Org tags*"))
  13851. (erase-buffer)
  13852. (setq-local org-done-keywords done-keywords)
  13853. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  13854. (org-fast-tag-insert "Current" current c-face "\n\n")
  13855. (org-fast-tag-show-exit exit-after-next)
  13856. (org-set-current-tags-overlay current ov-prefix)
  13857. (setq tbl fulltable char ?a cnt 0)
  13858. (while (setq e (pop tbl))
  13859. (cond
  13860. ((eq (car e) :startgroup)
  13861. (push '() groups) (setq ingroup t)
  13862. (unless (zerop cnt)
  13863. (setq cnt 0)
  13864. (insert "\n"))
  13865. (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
  13866. ((eq (car e) :endgroup)
  13867. (setq ingroup nil cnt 0)
  13868. (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
  13869. ((eq (car e) :startgrouptag)
  13870. (setq intaggroup t)
  13871. (unless (zerop cnt)
  13872. (setq cnt 0)
  13873. (insert "\n"))
  13874. (insert "[ "))
  13875. ((eq (car e) :endgrouptag)
  13876. (setq intaggroup nil cnt 0)
  13877. (insert "]\n"))
  13878. ((equal e '(:newline))
  13879. (unless (zerop cnt)
  13880. (setq cnt 0)
  13881. (insert "\n")
  13882. (setq e (car tbl))
  13883. (while (equal (car tbl) '(:newline))
  13884. (insert "\n")
  13885. (setq tbl (cdr tbl)))))
  13886. ((equal e '(:grouptags)) (insert " : "))
  13887. (t
  13888. (setq tg (copy-sequence (car e)) c2 nil)
  13889. (if (cdr e)
  13890. (setq c (cdr e))
  13891. ;; automatically assign a character.
  13892. (setq c1 (string-to-char
  13893. (downcase (substring
  13894. tg (if (= (string-to-char tg) ?@) 1 0)))))
  13895. (if (or (rassoc c1 ntable) (rassoc c1 table))
  13896. (while (or (rassoc char ntable) (rassoc char table))
  13897. (setq char (1+ char)))
  13898. (setq c2 c1))
  13899. (setq c (or c2 char)))
  13900. (when ingroup (push tg (car groups)))
  13901. (setq tg (org-add-props tg nil 'face
  13902. (cond
  13903. ((not (assoc tg table))
  13904. (org-get-todo-face tg))
  13905. ((member tg current) c-face)
  13906. ((member tg inherited) i-face))))
  13907. (when (equal (caar tbl) :grouptags)
  13908. (org-add-props tg nil 'face 'org-tag-group))
  13909. (when (and (zerop cnt) (not ingroup) (not intaggroup)) (insert " "))
  13910. (insert "[" c "] " tg (make-string
  13911. (- fwidth 4 (length tg)) ?\ ))
  13912. (push (cons tg c) ntable)
  13913. (when (= (incf cnt) ncol)
  13914. (insert "\n")
  13915. (when (or ingroup intaggroup) (insert " "))
  13916. (setq cnt 0)))))
  13917. (setq ntable (nreverse ntable))
  13918. (insert "\n")
  13919. (goto-char (point-min))
  13920. (unless expert (org-fit-window-to-buffer))
  13921. (setq rtn
  13922. (catch 'exit
  13923. (while t
  13924. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
  13925. (if (not groups) "no " "")
  13926. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  13927. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  13928. (setq org-last-tag-selection-key c)
  13929. (cond
  13930. ((= c ?\r) (throw 'exit t))
  13931. ((= c ?!)
  13932. (setq groups (not groups))
  13933. (goto-char (point-min))
  13934. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  13935. ((= c ?\C-c)
  13936. (if (not expert)
  13937. (org-fast-tag-show-exit
  13938. (setq exit-after-next (not exit-after-next)))
  13939. (setq expert nil)
  13940. (delete-other-windows)
  13941. (set-window-buffer (split-window-vertically) " *Org tags*")
  13942. (org-switch-to-buffer-other-window " *Org tags*")
  13943. (org-fit-window-to-buffer)))
  13944. ((or (= c ?\C-g)
  13945. (and (= c ?q) (not (rassoc c ntable))))
  13946. (org-detach-overlay org-tags-overlay)
  13947. (setq quit-flag t))
  13948. ((= c ?\ )
  13949. (setq current nil)
  13950. (when exit-after-next (setq exit-after-next 'now)))
  13951. ((= c ?\t)
  13952. (condition-case nil
  13953. (setq tg (org-icompleting-read
  13954. "Tag: "
  13955. (or buffer-tags
  13956. (with-current-buffer buf
  13957. (setq buffer-tags
  13958. (org-get-buffer-tags))))))
  13959. (quit (setq tg "")))
  13960. (when (string-match "\\S-" tg)
  13961. (add-to-list 'buffer-tags (list tg))
  13962. (if (member tg current)
  13963. (setq current (delete tg current))
  13964. (push tg current)))
  13965. (when exit-after-next (setq exit-after-next 'now)))
  13966. ((setq e (rassoc c todo-table) tg (car e))
  13967. (with-current-buffer buf
  13968. (save-excursion (org-todo tg)))
  13969. (when exit-after-next (setq exit-after-next 'now)))
  13970. ((setq e (rassoc c ntable) tg (car e))
  13971. (if (member tg current)
  13972. (setq current (delete tg current))
  13973. (loop for g in groups do
  13974. (when (member tg g)
  13975. (dolist (x g) (setq current (delete x current)))))
  13976. (push tg current))
  13977. (when exit-after-next (setq exit-after-next 'now))))
  13978. ;; Create a sorted list
  13979. (setq current
  13980. (sort current
  13981. (lambda (a b)
  13982. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  13983. (when (eq exit-after-next 'now) (throw 'exit t))
  13984. (goto-char (point-min))
  13985. (beginning-of-line 2)
  13986. (delete-region (point) (point-at-eol))
  13987. (org-fast-tag-insert "Current" current c-face)
  13988. (org-set-current-tags-overlay current ov-prefix)
  13989. (while (re-search-forward
  13990. (org-re "\\[.\\] \\([[:alnum:]_@#%]+\\)") nil t)
  13991. (setq tg (match-string 1))
  13992. (add-text-properties
  13993. (match-beginning 1) (match-end 1)
  13994. (list 'face
  13995. (cond
  13996. ((member tg current) c-face)
  13997. ((member tg inherited) i-face)
  13998. (t (get-text-property (match-beginning 1) 'face))))))
  13999. (goto-char (point-min)))))
  14000. (org-detach-overlay org-tags-overlay)
  14001. (if rtn
  14002. (mapconcat 'identity current ":")
  14003. nil))))
  14004. (defun org-get-tags-string ()
  14005. "Get the TAGS string in the current headline."
  14006. (unless (org-at-heading-p t)
  14007. (user-error "Not on a heading"))
  14008. (save-excursion
  14009. (beginning-of-line 1)
  14010. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  14011. (org-match-string-no-properties 1)
  14012. "")))
  14013. (defun org-get-tags ()
  14014. "Get the list of tags specified in the current headline."
  14015. (org-split-string (org-get-tags-string) ":"))
  14016. (defun org-get-buffer-tags ()
  14017. "Get a table of all tags used in the buffer, for completion."
  14018. (org-with-wide-buffer
  14019. (goto-char (point-min))
  14020. (let ((tag-re (concat org-outline-regexp-bol
  14021. "\\(?:.*?[ \t]\\)?"
  14022. (org-re ":\\([[:alnum:]_@#%:]+\\):[ \t]*$")))
  14023. tags)
  14024. (while (re-search-forward tag-re nil t)
  14025. (dolist (tag (org-split-string (org-match-string-no-properties 1) ":"))
  14026. (push tag tags)))
  14027. (mapcar #'list (append org-file-tags (org-uniquify tags))))))
  14028. ;;;; The mapping API
  14029. (defun org-map-entries (func &optional match scope &rest skip)
  14030. "Call FUNC at each headline selected by MATCH in SCOPE.
  14031. FUNC is a function or a lisp form. The function will be called without
  14032. arguments, with the cursor positioned at the beginning of the headline.
  14033. The return values of all calls to the function will be collected and
  14034. returned as a list.
  14035. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  14036. does not need to preserve point. After evaluation, the cursor will be
  14037. moved to the end of the line (presumably of the headline of the
  14038. processed entry) and search continues from there. Under some
  14039. circumstances, this may not produce the wanted results. For example,
  14040. if you have removed (e.g. archived) the current (sub)tree it could
  14041. mean that the next entry will be skipped entirely. In such cases, you
  14042. can specify the position from where search should continue by making
  14043. FUNC set the variable `org-map-continue-from' to the desired buffer
  14044. position.
  14045. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  14046. Only headlines that are matched by this query will be considered during
  14047. the iteration. When MATCH is nil or t, all headlines will be
  14048. visited by the iteration.
  14049. SCOPE determines the scope of this command. It can be any of:
  14050. nil The current buffer, respecting the restriction if any
  14051. tree The subtree started with the entry at point
  14052. region The entries within the active region, if any
  14053. region-start-level
  14054. The entries within the active region, but only those at
  14055. the same level than the first one.
  14056. file The current buffer, without restriction
  14057. file-with-archives
  14058. The current buffer, and any archives associated with it
  14059. agenda All agenda files
  14060. agenda-with-archives
  14061. All agenda files with any archive files associated with them
  14062. \(file1 file2 ...)
  14063. If this is a list, all files in the list will be scanned
  14064. The remaining args are treated as settings for the skipping facilities of
  14065. the scanner. The following items can be given here:
  14066. archive skip trees with the archive tag
  14067. comment skip trees with the COMMENT keyword
  14068. function or Emacs Lisp form:
  14069. will be used as value for `org-agenda-skip-function', so
  14070. whenever the function returns a position, FUNC will not be
  14071. called for that entry and search will continue from the
  14072. position returned
  14073. If your function needs to retrieve the tags including inherited tags
  14074. at the *current* entry, you can use the value of the variable
  14075. `org-scanner-tags' which will be much faster than getting the value
  14076. with `org-get-tags-at'. If your function gets properties with
  14077. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  14078. to t around the call to `org-entry-properties' to get the same speedup.
  14079. Note that if your function moves around to retrieve tags and properties at
  14080. a *different* entry, you cannot use these techniques."
  14081. (unless (and (or (eq scope 'region) (eq scope 'region-start-level))
  14082. (not (org-region-active-p)))
  14083. (let* ((org-agenda-archives-mode nil) ; just to make sure
  14084. (org-agenda-skip-archived-trees (memq 'archive skip))
  14085. (org-agenda-skip-comment-trees (memq 'comment skip))
  14086. (org-agenda-skip-function
  14087. (car (org-delete-all '(comment archive) skip)))
  14088. (org-tags-match-list-sublevels t)
  14089. (start-level (eq scope 'region-start-level))
  14090. matcher file res
  14091. org-todo-keywords-for-agenda
  14092. org-done-keywords-for-agenda
  14093. org-todo-keyword-alist-for-agenda
  14094. org-tag-alist-for-agenda
  14095. todo-only)
  14096. (cond
  14097. ((eq match t) (setq matcher t))
  14098. ((eq match nil) (setq matcher t))
  14099. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  14100. (save-excursion
  14101. (save-restriction
  14102. (cond ((eq scope 'tree)
  14103. (org-back-to-heading t)
  14104. (org-narrow-to-subtree)
  14105. (setq scope nil))
  14106. ((and (or (eq scope 'region) (eq scope 'region-start-level))
  14107. (org-region-active-p))
  14108. ;; If needed, set start-level to a string like "2"
  14109. (when start-level
  14110. (save-excursion
  14111. (goto-char (region-beginning))
  14112. (unless (org-at-heading-p) (outline-next-heading))
  14113. (setq start-level (org-current-level))))
  14114. (narrow-to-region (region-beginning)
  14115. (save-excursion
  14116. (goto-char (region-end))
  14117. (unless (and (bolp) (org-at-heading-p))
  14118. (outline-next-heading))
  14119. (point)))
  14120. (setq scope nil)))
  14121. (if (not scope)
  14122. (progn
  14123. (org-agenda-prepare-buffers
  14124. (and buffer-file-name (list buffer-file-name)))
  14125. (setq res (org-scan-tags func matcher todo-only start-level)))
  14126. ;; Get the right scope
  14127. (cond
  14128. ((and scope (listp scope) (symbolp (car scope)))
  14129. (setq scope (eval scope)))
  14130. ((eq scope 'agenda)
  14131. (setq scope (org-agenda-files t)))
  14132. ((eq scope 'agenda-with-archives)
  14133. (setq scope (org-agenda-files t))
  14134. (setq scope (org-add-archive-files scope)))
  14135. ((eq scope 'file)
  14136. (setq scope (and buffer-file-name (list buffer-file-name))))
  14137. ((eq scope 'file-with-archives)
  14138. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  14139. (org-agenda-prepare-buffers scope)
  14140. (dolist (file scope)
  14141. (with-current-buffer (org-find-base-buffer-visiting file)
  14142. (save-excursion
  14143. (save-restriction
  14144. (widen)
  14145. (goto-char (point-min))
  14146. (setq res (append res (org-scan-tags func matcher todo-only))))))))))
  14147. res)))
  14148. ;;; Properties API
  14149. (defconst org-special-properties
  14150. '("ALLTAGS" "BLOCKED" "CLOCKSUM" "CLOCKSUM_T" "CLOSED" "DEADLINE" "FILE"
  14151. "ITEM" "PRIORITY" "SCHEDULED" "TAGS" "TIMESTAMP" "TIMESTAMP_IA" "TODO")
  14152. "The special properties valid in Org mode.
  14153. These are properties that are not defined in the property drawer,
  14154. but in some other way.")
  14155. (defconst org-default-properties
  14156. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  14157. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  14158. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  14159. "EXPORT_OPTIONS" "EXPORT_TEXT" "EXPORT_FILE_NAME"
  14160. "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE" "UNNUMBERED"
  14161. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER" "REPEAT_TO_STATE"
  14162. "CLOCK_MODELINE_TOTAL" "STYLE" "HTML_CONTAINER_CLASS")
  14163. "Some properties that are used by Org mode for various purposes.
  14164. Being in this list makes sure that they are offered for completion.")
  14165. (defun org--update-property-plist (key val props)
  14166. "Associate KEY to VAL in alist PROPS.
  14167. Modifications are made by side-effect. Return new alist."
  14168. (let* ((appending (string= (substring key -1) "+"))
  14169. (key (if appending (substring key 0 -1) key))
  14170. (old (assoc-string key props t)))
  14171. (if (not old) (cons (cons key val) props)
  14172. (setcdr old (if appending (concat (cdr old) " " val) val))
  14173. props)))
  14174. (defun org-get-property-block (&optional beg force)
  14175. "Return the (beg . end) range of the body of the property drawer.
  14176. BEG is the beginning of the current subtree, or of the part
  14177. before the first headline. If it is not given, it will be found.
  14178. If the drawer does not exist, create it if FORCE is non-nil, or
  14179. return nil."
  14180. (org-with-wide-buffer
  14181. (when beg (goto-char beg))
  14182. (unless (org-before-first-heading-p)
  14183. (let ((beg (cond (beg)
  14184. ((or (not (featurep 'org-inlinetask))
  14185. (org-inlinetask-in-task-p))
  14186. (org-back-to-heading t))
  14187. (t (org-with-limited-levels (org-back-to-heading t))))))
  14188. (forward-line)
  14189. (when (org-looking-at-p org-planning-line-re) (forward-line))
  14190. (cond ((looking-at org-property-drawer-re)
  14191. (forward-line)
  14192. (cons (point) (progn (goto-char (match-end 0))
  14193. (line-beginning-position))))
  14194. (force
  14195. (goto-char beg)
  14196. (org-insert-property-drawer)
  14197. (let ((pos (save-excursion (search-forward ":END:")
  14198. (line-beginning-position))))
  14199. (cons pos pos))))))))
  14200. (defun org-at-property-p ()
  14201. "Non-nil when point is inside a property drawer.
  14202. See `org-property-re' for match data, if applicable."
  14203. (save-excursion
  14204. (beginning-of-line)
  14205. (and (looking-at org-property-re)
  14206. (let ((property-drawer (save-match-data (org-get-property-block))))
  14207. (and property-drawer
  14208. (>= (point) (car property-drawer))
  14209. (< (point) (cdr property-drawer)))))))
  14210. (defun org-property-action ()
  14211. "Do an action on properties."
  14212. (interactive)
  14213. (unless (org-at-property-p) (user-error "Not at a property"))
  14214. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  14215. (let ((c (read-char-exclusive)))
  14216. (case c
  14217. (?s (call-interactively #'org-set-property))
  14218. (?d (call-interactively #'org-delete-property))
  14219. (?D (call-interactively #'org-delete-property-globally))
  14220. (?c (call-interactively #'org-compute-property-at-point))
  14221. (otherwise (user-error "No such property action %c" c)))))
  14222. (defun org-inc-effort ()
  14223. "Increment the value of the effort property in the current entry."
  14224. (interactive)
  14225. (org-set-effort nil t))
  14226. (defvar org-clock-effort) ; Defined in org-clock.el.
  14227. (defvar org-clock-current-task) ; Defined in org-clock.el.
  14228. (defun org-set-effort (&optional value increment)
  14229. "Set the effort property of the current entry.
  14230. With numerical prefix arg, use the nth allowed value, 0 stands for the
  14231. 10th allowed value.
  14232. When INCREMENT is non-nil, set the property to the next allowed value."
  14233. (interactive "P")
  14234. (when (equal value 0) (setq value 10))
  14235. (let* ((completion-ignore-case t)
  14236. (prop org-effort-property)
  14237. (cur (org-entry-get nil prop))
  14238. (allowed (org-property-get-allowed-values nil prop 'table))
  14239. (existing (mapcar 'list (org-property-values prop)))
  14240. (heading (nth 4 (org-heading-components)))
  14241. rpl
  14242. (val (cond
  14243. ((stringp value) value)
  14244. ((and allowed (integerp value))
  14245. (or (car (nth (1- value) allowed))
  14246. (car (org-last allowed))))
  14247. ((and allowed increment)
  14248. (or (caadr (member (list cur) allowed))
  14249. (user-error "Allowed effort values are not set")))
  14250. (allowed
  14251. (message "Select 1-9,0, [RET%s]: %s"
  14252. (if cur (concat "=" cur) "")
  14253. (mapconcat 'car allowed " "))
  14254. (setq rpl (read-char-exclusive))
  14255. (if (equal rpl ?\r)
  14256. cur
  14257. (setq rpl (- rpl ?0))
  14258. (when (equal rpl 0) (setq rpl 10))
  14259. (if (and (> rpl 0) (<= rpl (length allowed)))
  14260. (car (nth (1- rpl) allowed))
  14261. (org-completing-read "Effort: " allowed nil))))
  14262. (t
  14263. (let (org-completion-use-ido org-completion-use-iswitchb)
  14264. (org-completing-read
  14265. (concat "Effort " (if (and cur (string-match "\\S-" cur))
  14266. (concat "[" cur "]") "")
  14267. ": ")
  14268. existing nil nil "" nil cur))))))
  14269. (unless (equal (org-entry-get nil prop) val)
  14270. (org-entry-put nil prop val))
  14271. (org-refresh-property
  14272. '((effort . identity)
  14273. (effort-minutes . org-duration-string-to-minutes))
  14274. val)
  14275. (when (string= heading org-clock-current-task)
  14276. (setq org-clock-effort (get-text-property (point-at-bol) 'effort))
  14277. (org-clock-update-mode-line))
  14278. (message "%s is now %s" prop val)))
  14279. (defun org-entry-properties (&optional pom which)
  14280. "Get all properties of the current entry.
  14281. When POM is a buffer position, get all properties from the entry
  14282. there instead.
  14283. This includes the TODO keyword, the tags, time strings for
  14284. deadline, scheduled, and clocking, and any additional properties
  14285. defined in the entry.
  14286. If WHICH is nil or `all', get all properties. If WHICH is
  14287. `special' or `standard', only get that subclass. If WHICH is
  14288. a string, only get that property.
  14289. Return value is an alist. Keys are properties, as upcased
  14290. strings."
  14291. (org-with-point-at pom
  14292. (when (and (derived-mode-p 'org-mode)
  14293. (ignore-errors (org-back-to-heading t)))
  14294. (catch 'exit
  14295. (let* ((beg (point))
  14296. (specific (and (stringp which) (upcase which)))
  14297. (which (cond ((not specific) which)
  14298. ((member specific org-special-properties) 'special)
  14299. (t 'standard)))
  14300. props)
  14301. ;; Get the special properties, like TODO and TAGS.
  14302. (when (memq which '(nil all special))
  14303. (when (or (not specific) (string= specific "CLOCKSUM"))
  14304. (let ((clocksum (get-text-property (point) :org-clock-minutes)))
  14305. (when clocksum
  14306. (push (cons "CLOCKSUM"
  14307. (org-columns-number-to-string
  14308. (/ (float clocksum) 60.) 'add_times))
  14309. props)))
  14310. (when specific (throw 'exit props)))
  14311. (when (or (not specific) (string= specific "CLOCKSUM_T"))
  14312. (let ((clocksumt (get-text-property (point)
  14313. :org-clock-minutes-today)))
  14314. (when clocksumt
  14315. (push (cons "CLOCKSUM_T"
  14316. (org-columns-number-to-string
  14317. (/ (float clocksumt) 60.) 'add_times))
  14318. props)))
  14319. (when specific (throw 'exit props)))
  14320. (when (or (not specific) (string= specific "ITEM"))
  14321. (when (looking-at org-complex-heading-regexp)
  14322. (push (cons "ITEM"
  14323. (concat
  14324. (org-match-string-no-properties 1)
  14325. (let ((title (org-match-string-no-properties 4)))
  14326. (when (org-string-nw-p title)
  14327. (concat " " (org-remove-tabs title))))))
  14328. props))
  14329. (when specific (throw 'exit props)))
  14330. (when (or (not specific) (string= specific "TODO"))
  14331. (let ((case-fold-search nil))
  14332. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  14333. (push (cons "TODO" (org-match-string-no-properties 2)) props)))
  14334. (when specific (throw 'exit props)))
  14335. (when (or (not specific) (string= specific "PRIORITY"))
  14336. (push (cons "PRIORITY"
  14337. (if (looking-at org-priority-regexp)
  14338. (org-match-string-no-properties 2)
  14339. (char-to-string org-default-priority)))
  14340. props)
  14341. (when specific (throw 'exit props)))
  14342. (when (or (not specific) (string= specific "FILE"))
  14343. (push (cons "FILE" (buffer-file-name (buffer-base-buffer)))
  14344. props)
  14345. (when specific (throw 'exit props)))
  14346. (when (or (not specific) (string= specific "TAGS"))
  14347. (let ((value (org-string-nw-p (org-get-tags-string))))
  14348. (when value (push (cons "TAGS" value) props)))
  14349. (when specific (throw 'exit props)))
  14350. (when (or (not specific) (string= specific "ALLTAGS"))
  14351. (let ((value (org-get-tags-at)))
  14352. (when value
  14353. (push (cons "ALLTAGS"
  14354. (format ":%s:" (mapconcat #'identity value ":")))
  14355. props)))
  14356. (when specific (throw 'exit props)))
  14357. (when (or (not specific) (string= specific "BLOCKED"))
  14358. (push (cons "BLOCKED" (if (org-entry-blocked-p) "t" "")) props)
  14359. (when specific (throw 'exit props)))
  14360. (when (or (not specific)
  14361. (member specific '("CLOSED" "DEADLINE" "SCHEDULED")))
  14362. (forward-line)
  14363. (when (org-looking-at-p org-planning-line-re)
  14364. (end-of-line)
  14365. (let ((bol (line-beginning-position))
  14366. ;; Backward compatibility: time keywords used to
  14367. ;; be configurable (before 8.3). Make sure we
  14368. ;; get the correct keyword.
  14369. (key-assoc `(("CLOSED" . ,org-closed-string)
  14370. ("DEADLINE" . ,org-deadline-string)
  14371. ("SCHEDULED" . ,org-scheduled-string))))
  14372. (dolist (pair (if specific (list (assoc specific key-assoc))
  14373. key-assoc))
  14374. (save-excursion
  14375. (when (search-backward (cdr pair) bol t)
  14376. (goto-char (match-end 0))
  14377. (skip-chars-forward " \t")
  14378. (and (looking-at org-ts-regexp-both)
  14379. (push (cons (car pair)
  14380. (org-match-string-no-properties 0))
  14381. props)))))))
  14382. (when specific (throw 'exit props)))
  14383. (when (or (not specific)
  14384. (member specific '("TIMESTAMP" "TIMESTAMP_IA")))
  14385. (let ((find-ts
  14386. (lambda (end ts)
  14387. (let ((regexp (cond
  14388. ((string= specific "TIMESTAMP")
  14389. org-ts-regexp)
  14390. ((string= specific "TIMESTAMP_IA")
  14391. org-ts-regexp-inactive)
  14392. ((assoc "TIMESTAMP_IA" ts)
  14393. org-ts-regexp)
  14394. ((assoc "TIMESTAMP" ts)
  14395. org-ts-regexp-inactive)
  14396. (t org-ts-regexp-both))))
  14397. (catch 'next
  14398. (while (re-search-forward regexp end t)
  14399. (backward-char)
  14400. (let ((object (org-element-context)))
  14401. ;; Accept to match timestamps in node
  14402. ;; properties, too.
  14403. (when (memq (org-element-type object)
  14404. '(node-property timestamp))
  14405. (let ((type
  14406. (org-element-property :type object)))
  14407. (cond
  14408. ((and (memq type '(active active-range))
  14409. (not (equal specific "TIMESTAMP_IA")))
  14410. (unless (assoc "TIMESTAMP" ts)
  14411. (push (cons "TIMESTAMP"
  14412. (org-element-property
  14413. :raw-value object))
  14414. ts)
  14415. (when specific (throw 'exit ts))))
  14416. ((and (memq type '(inactive inactive-range))
  14417. (not (string= specific "TIMESTAMP")))
  14418. (unless (assoc "TIMESTAMP_IA" ts)
  14419. (push (cons "TIMESTAMP_IA"
  14420. (org-element-property
  14421. :raw-value object))
  14422. ts)
  14423. (when specific (throw 'exit ts))))))
  14424. ;; Both timestamp types are found,
  14425. ;; move to next part.
  14426. (when (= (length ts) 2) (throw 'next ts)))))
  14427. ts)))))
  14428. (goto-char beg)
  14429. ;; First look for timestamps within headline.
  14430. (let ((ts (funcall find-ts (line-end-position) nil)))
  14431. (if (= (length ts) 2) (setq props (nconc ts props))
  14432. (forward-line)
  14433. ;; Then find timestamps in the section, skipping
  14434. ;; planning line.
  14435. (when (org-looking-at-p org-planning-line-re)
  14436. (forward-line))
  14437. (let ((end (save-excursion (outline-next-heading))))
  14438. (setq props (nconc (funcall find-ts end ts) props))))))))
  14439. ;; Get the standard properties, like :PROP:.
  14440. (when (memq which '(nil all standard))
  14441. ;; If we are looking after a specific property, delegate
  14442. ;; to `org-entry-get', which is faster. However, make an
  14443. ;; exception for "CATEGORY", since it can be also set
  14444. ;; through keywords (i.e. #+CATEGORY).
  14445. (if (and specific (not (equal specific "CATEGORY")))
  14446. (let ((value (org-entry-get beg specific nil t)))
  14447. (throw 'exit (and value (list (cons specific value)))))
  14448. (let ((range (org-get-property-block beg)))
  14449. (when range
  14450. (let ((end (cdr range)) seen-base)
  14451. (goto-char (car range))
  14452. ;; Unlike to `org--update-property-plist', we
  14453. ;; handle the case where base values is found
  14454. ;; after its extension. We also forbid standard
  14455. ;; properties to be named as special properties.
  14456. (while (re-search-forward org-property-re end t)
  14457. (let* ((key (upcase (org-match-string-no-properties 2)))
  14458. (extendp (org-string-match-p "\\+\\'" key))
  14459. (key-base (if extendp (substring key 0 -1) key))
  14460. (value (org-match-string-no-properties 3)))
  14461. (cond
  14462. ((member-ignore-case key-base org-special-properties))
  14463. (extendp
  14464. (setq props
  14465. (org--update-property-plist key value props)))
  14466. ((member key seen-base))
  14467. (t (push key seen-base)
  14468. (let ((p (assoc-string key props t)))
  14469. (if p (setcdr p (concat value " " (cdr p)))
  14470. (push (cons key value) props))))))))))))
  14471. (unless (assoc "CATEGORY" props)
  14472. (push (cons "CATEGORY" (org-get-category beg)) props)
  14473. (when (string= specific "CATEGORY") (throw 'exit props)))
  14474. ;; Return value.
  14475. props)))))
  14476. (defun org-property--local-values (property literal-nil)
  14477. "Return value for PROPERTY in current entry.
  14478. Value is a list whose care is the base value for PROPERTY and cdr
  14479. a list of accumulated values. Return nil if neither is found in
  14480. the entry. Also return nil when PROPERTY is set to \"nil\",
  14481. unless LITERAL-NIL is non-nil."
  14482. (let ((range (org-get-property-block)))
  14483. (when range
  14484. (goto-char (car range))
  14485. (let* ((case-fold-search t)
  14486. (end (cdr range))
  14487. (value
  14488. ;; Base value.
  14489. (save-excursion
  14490. (let ((v (and (re-search-forward
  14491. (org-re-property property nil t) end t)
  14492. (org-match-string-no-properties 3))))
  14493. (list (if literal-nil v (org-not-nil v)))))))
  14494. ;; Find additional values.
  14495. (let* ((property+ (org-re-property (concat property "+") nil t)))
  14496. (while (re-search-forward property+ end t)
  14497. (push (org-match-string-no-properties 3) value)))
  14498. ;; Return final values.
  14499. (and (not (equal value '(nil))) (nreverse value))))))
  14500. (defun org-entry-get (pom property &optional inherit literal-nil)
  14501. "Get value of PROPERTY for entry or content at point-or-marker POM.
  14502. If INHERIT is non-nil and the entry does not have the property,
  14503. then also check higher levels of the hierarchy. If INHERIT is
  14504. the symbol `selective', use inheritance only if the setting in
  14505. `org-use-property-inheritance' selects PROPERTY for inheritance.
  14506. If the property is present but empty, the return value is the
  14507. empty string. If the property is not present at all, nil is
  14508. returned. In any other case, return the value as a string.
  14509. Search is case-insensitive.
  14510. If LITERAL-NIL is set, return the string value \"nil\" as
  14511. a string, do not interpret it as the list atom nil. This is used
  14512. for inheritance when a \"nil\" value can supersede a non-nil
  14513. value higher up the hierarchy."
  14514. (org-with-point-at pom
  14515. (cond
  14516. ((member-ignore-case property (cons "CATEGORY" org-special-properties))
  14517. ;; We need a special property. Use `org-entry-properties' to
  14518. ;; retrieve it, but specify the wanted property.
  14519. (cdr (assoc-string property (org-entry-properties nil property))))
  14520. ((and inherit
  14521. (or (not (eq inherit 'selective)) (org-property-inherit-p property)))
  14522. (org-entry-get-with-inheritance property literal-nil))
  14523. (t
  14524. (let* ((local (org-property--local-values property literal-nil))
  14525. (value (and local (mapconcat #'identity (delq nil local) " "))))
  14526. (if literal-nil value (org-not-nil value)))))))
  14527. (defun org-property-or-variable-value (var &optional inherit)
  14528. "Check if there is a property fixing the value of VAR.
  14529. If yes, return this value. If not, return the current value of the variable."
  14530. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  14531. (if (and prop (stringp prop) (string-match "\\S-" prop))
  14532. (read prop)
  14533. (symbol-value var))))
  14534. (defun org-entry-delete (pom property)
  14535. "Delete PROPERTY from entry at point-or-marker POM.
  14536. Accumulated properties, i.e. PROPERTY+, are also removed. Return
  14537. non-nil when a property was removed."
  14538. (unless (member property org-special-properties)
  14539. (org-with-point-at pom
  14540. (let ((range (org-get-property-block)))
  14541. (when range
  14542. (let* ((begin (car range))
  14543. (origin (cdr range))
  14544. (end (copy-marker origin))
  14545. (re (org-re-property
  14546. (concat (regexp-quote property) "\\+?") t t)))
  14547. (goto-char begin)
  14548. (while (re-search-forward re end t)
  14549. (delete-region (match-beginning 0) (line-beginning-position 2)))
  14550. ;; If drawer is empty, remove it altogether.
  14551. (when (= begin end)
  14552. (delete-region (line-beginning-position 0)
  14553. (line-beginning-position 2)))
  14554. ;; Return non-nil if some property was removed.
  14555. (prog1 (/= end origin) (set-marker end nil))))))))
  14556. ;; Multi-values properties are properties that contain multiple values
  14557. ;; These values are assumed to be single words, separated by whitespace.
  14558. (defun org-entry-add-to-multivalued-property (pom property value)
  14559. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  14560. (let* ((old (org-entry-get pom property))
  14561. (values (and old (org-split-string old "[ \t]"))))
  14562. (setq value (org-entry-protect-space value))
  14563. (unless (member value values)
  14564. (setq values (append values (list value)))
  14565. (org-entry-put pom property
  14566. (mapconcat 'identity values " ")))))
  14567. (defun org-entry-remove-from-multivalued-property (pom property value)
  14568. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  14569. (let* ((old (org-entry-get pom property))
  14570. (values (and old (org-split-string old "[ \t]"))))
  14571. (setq value (org-entry-protect-space value))
  14572. (when (member value values)
  14573. (setq values (delete value values))
  14574. (org-entry-put pom property
  14575. (mapconcat 'identity values " ")))))
  14576. (defun org-entry-member-in-multivalued-property (pom property value)
  14577. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  14578. (let* ((old (org-entry-get pom property))
  14579. (values (and old (org-split-string old "[ \t]"))))
  14580. (setq value (org-entry-protect-space value))
  14581. (member value values)))
  14582. (defun org-entry-get-multivalued-property (pom property)
  14583. "Return a list of values in a multivalued property."
  14584. (let* ((value (org-entry-get pom property))
  14585. (values (and value (org-split-string value "[ \t]"))))
  14586. (mapcar 'org-entry-restore-space values)))
  14587. (defun org-entry-put-multivalued-property (pom property &rest values)
  14588. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  14589. VALUES should be a list of strings. Spaces will be protected."
  14590. (org-entry-put pom property
  14591. (mapconcat 'org-entry-protect-space values " "))
  14592. (let* ((value (org-entry-get pom property))
  14593. (values (and value (org-split-string value "[ \t]"))))
  14594. (mapcar 'org-entry-restore-space values)))
  14595. (defun org-entry-protect-space (s)
  14596. "Protect spaces and newline in string S."
  14597. (while (string-match " " s)
  14598. (setq s (replace-match "%20" t t s)))
  14599. (while (string-match "\n" s)
  14600. (setq s (replace-match "%0A" t t s)))
  14601. s)
  14602. (defun org-entry-restore-space (s)
  14603. "Restore spaces and newline in string S."
  14604. (while (string-match "%20" s)
  14605. (setq s (replace-match " " t t s)))
  14606. (while (string-match "%0A" s)
  14607. (setq s (replace-match "\n" t t s)))
  14608. s)
  14609. (defvar org-entry-property-inherited-from (make-marker)
  14610. "Marker pointing to the entry from where a property was inherited.
  14611. Each call to `org-entry-get-with-inheritance' will set this marker to the
  14612. location of the entry where the inheritance search matched. If there was
  14613. no match, the marker will point nowhere.
  14614. Note that also `org-entry-get' calls this function, if the INHERIT flag
  14615. is set.")
  14616. (defun org-entry-get-with-inheritance (property &optional literal-nil)
  14617. "Get PROPERTY of entry or content at point, search higher levels if needed.
  14618. The search will stop at the first ancestor which has the property defined.
  14619. If the value found is \"nil\", return nil to show that the property
  14620. should be considered as undefined (this is the meaning of nil here).
  14621. However, if LITERAL-NIL is set, return the string value \"nil\" instead."
  14622. (move-marker org-entry-property-inherited-from nil)
  14623. (org-with-wide-buffer
  14624. (let (value)
  14625. (catch 'exit
  14626. (while t
  14627. (let ((v (org-property--local-values property literal-nil)))
  14628. (when v
  14629. (setq value
  14630. (concat (mapconcat #'identity (delq nil v) " ")
  14631. (and value " ")
  14632. value)))
  14633. (cond
  14634. ((car v)
  14635. (org-back-to-heading t)
  14636. (move-marker org-entry-property-inherited-from (point))
  14637. (throw 'exit nil))
  14638. ((org-up-heading-safe))
  14639. (t
  14640. (let ((global
  14641. (cdr (or (assoc-string property org-file-properties t)
  14642. (assoc-string property org-global-properties t)
  14643. (assoc-string property org-global-properties-fixed t)))))
  14644. (cond ((not global))
  14645. (value (setq value (concat global " " value)))
  14646. (t (setq value global))))
  14647. (throw 'exit nil))))))
  14648. (if literal-nil value (org-not-nil value)))))
  14649. (defvar org-property-changed-functions nil
  14650. "Hook called when the value of a property has changed.
  14651. Each hook function should accept two arguments, the name of the property
  14652. and the new value.")
  14653. (defun org-entry-put (pom property value)
  14654. "Set PROPERTY to VALUE for entry at point-or-marker POM.
  14655. If the value is nil, it is converted to the empty string. If
  14656. it is not a string, an error is raised.
  14657. PROPERTY can be any regular property (see
  14658. `org-special-properties'). It can also be \"TODO\",
  14659. \"PRIORITY\", \"SCHEDULED\" and \"DEADLINE\".
  14660. For the last two properties, VALUE may have any of the special
  14661. values \"earlier\" and \"later\". The function then increases or
  14662. decreases scheduled or deadline date by one day."
  14663. (cond ((null value) (setq value ""))
  14664. ((not (stringp value)) (error "Properties values should be strings")))
  14665. (org-with-point-at pom
  14666. (if (or (not (featurep 'org-inlinetask)) (org-inlinetask-in-task-p))
  14667. (org-back-to-heading t)
  14668. (org-with-limited-levels (org-back-to-heading t)))
  14669. (let ((beg (point)))
  14670. (cond
  14671. ((equal property "TODO")
  14672. (cond ((not (org-string-nw-p value)) (setq value 'none))
  14673. ((not (member value org-todo-keywords-1))
  14674. (user-error "\"%s\" is not a valid TODO state" value)))
  14675. (org-todo value)
  14676. (org-set-tags nil 'align))
  14677. ((equal property "PRIORITY")
  14678. (org-priority (if (org-string-nw-p value) (string-to-char value) ?\s))
  14679. (org-set-tags nil 'align))
  14680. ((equal property "SCHEDULED")
  14681. (forward-line)
  14682. (if (and (org-looking-at-p org-planning-line-re)
  14683. (re-search-forward
  14684. org-scheduled-time-regexp (line-end-position) t))
  14685. (cond ((string= value "earlier") (org-timestamp-change -1 'day))
  14686. ((string= value "later") (org-timestamp-change 1 'day))
  14687. ((string= value "") (org-schedule '(4)))
  14688. (t (org-schedule nil value)))
  14689. (if (member value '("earlier" "later" ""))
  14690. (call-interactively #'org-schedule)
  14691. (org-schedule nil value))))
  14692. ((equal property "DEADLINE")
  14693. (forward-line)
  14694. (if (and (org-looking-at-p org-planning-line-re)
  14695. (re-search-forward
  14696. org-deadline-time-regexp (line-end-position) t))
  14697. (cond ((string= value "earlier") (org-timestamp-change -1 'day))
  14698. ((string= value "later") (org-timestamp-change 1 'day))
  14699. ((string= value "") (org-deadline '(4)))
  14700. (t (org-deadline nil value)))
  14701. (if (member value '("earlier" "later" ""))
  14702. (call-interactively #'org-deadline)
  14703. (org-deadline nil value))))
  14704. ((member property org-special-properties)
  14705. (error "The %s property cannot be set with `org-entry-put'" property))
  14706. (t
  14707. (let* ((range (org-get-property-block beg 'force))
  14708. (end (cdr range))
  14709. (case-fold-search t))
  14710. (goto-char (car range))
  14711. (if (re-search-forward (org-re-property property nil t) end t)
  14712. (progn (delete-region (match-beginning 0) (match-end 0))
  14713. (goto-char (match-beginning 0)))
  14714. (goto-char end)
  14715. (insert "\n")
  14716. (backward-char))
  14717. (insert ":" property ":")
  14718. (when value (insert " " value))
  14719. (org-indent-line)))))
  14720. (run-hook-with-args 'org-property-changed-functions property value)))
  14721. (defun org-buffer-property-keys (&optional specials defaults columns)
  14722. "Get all property keys in the current buffer.
  14723. When SPECIALS is non-nil, also list the special properties that
  14724. reflect things like tags and TODO state.
  14725. When DEFAULTS is non-nil, also include properties that has
  14726. special meaning internally: ARCHIVE, CATEGORY, SUMMARY,
  14727. DESCRIPTION, LOCATION, and LOGGING and others.
  14728. When COLUMNS in non-nil, also include property names given in
  14729. COLUMN formats in the current buffer."
  14730. (let ((case-fold-search t)
  14731. (props (append
  14732. (and specials org-special-properties)
  14733. (and defaults (cons org-effort-property org-default-properties))
  14734. nil)))
  14735. (org-with-wide-buffer
  14736. (goto-char (point-min))
  14737. (while (re-search-forward org-property-start-re nil t)
  14738. (let ((range (org-get-property-block)))
  14739. (catch 'skip
  14740. (unless range
  14741. (when (and (not (org-before-first-heading-p))
  14742. (y-or-n-p (format "Malformed drawer at %d, repair?"
  14743. (line-beginning-position))))
  14744. (org-get-property-block nil t))
  14745. (throw 'skip nil))
  14746. (goto-char (car range))
  14747. (let ((begin (car range))
  14748. (end (cdr range)))
  14749. ;; Make sure that found property block is not located
  14750. ;; before current point, as it would generate an infloop.
  14751. ;; It can happen, for example, in the following
  14752. ;; situation:
  14753. ;;
  14754. ;; * Headline
  14755. ;; :PROPERTIES:
  14756. ;; ...
  14757. ;; :END:
  14758. ;; *************** Inlinetask
  14759. ;; #+BEGIN_EXAMPLE
  14760. ;; :PROPERTIES:
  14761. ;; #+END_EXAMPLE
  14762. ;;
  14763. (if (< begin (point)) (throw 'skip nil) (goto-char begin))
  14764. (while (< (point) end)
  14765. (let ((p (progn (looking-at org-property-re)
  14766. (org-match-string-no-properties 2))))
  14767. ;; Only add true property name, not extension symbol.
  14768. (add-to-list 'props
  14769. (if (not (org-string-match-p "\\+\\'" p)) p
  14770. (substring p 0 -1))))
  14771. (forward-line))))
  14772. (outline-next-heading)))
  14773. (when columns
  14774. (goto-char (point-min))
  14775. (while (re-search-forward "^[ \t]*\\(?:#\\+\\|:\\)COLUMNS:" nil t)
  14776. (let ((element (org-element-at-point)))
  14777. (when (memq (org-element-type element) '(keyword node-property))
  14778. (let ((value (org-element-property :value element))
  14779. (start 0))
  14780. (while (string-match "%[0-9]*\\(\\S-+\\)" value start)
  14781. (setq start (match-end 0))
  14782. (let ((p (org-match-string-no-properties 1 value)))
  14783. (unless (member-ignore-case p org-special-properties)
  14784. (add-to-list 'props p))))))))))
  14785. (sort props (lambda (a b) (string< (upcase a) (upcase b))))))
  14786. (defun org-property-values (key)
  14787. "List all non-nil values of property KEY in current buffer."
  14788. (org-with-wide-buffer
  14789. (goto-char (point-min))
  14790. (let ((case-fold-search t)
  14791. (re (org-re-property key))
  14792. values)
  14793. (while (re-search-forward re nil t)
  14794. (add-to-list 'values (org-entry-get (point) key)))
  14795. values)))
  14796. (defun org-insert-property-drawer ()
  14797. "Insert a property drawer into the current entry."
  14798. (org-with-wide-buffer
  14799. (if (or (not (featurep 'org-inlinetask)) (org-inlinetask-in-task-p))
  14800. (org-back-to-heading t)
  14801. (org-with-limited-levels (org-back-to-heading t)))
  14802. (forward-line)
  14803. (when (org-looking-at-p org-planning-line-re) (forward-line))
  14804. (unless (org-looking-at-p org-property-drawer-re)
  14805. ;; Make sure we start editing a line from current entry, not from
  14806. ;; next one. It prevents extending text properties or overlays
  14807. ;; belonging to the latter.
  14808. (when (bolp) (backward-char))
  14809. (let ((begin (1+ (point)))
  14810. (inhibit-read-only t))
  14811. (insert "\n:PROPERTIES:\n:END:")
  14812. (when (eobp) (insert "\n"))
  14813. (org-indent-region begin (point))))))
  14814. (defun org-insert-drawer (&optional arg drawer)
  14815. "Insert a drawer at point.
  14816. When optional argument ARG is non-nil, insert a property drawer.
  14817. Optional argument DRAWER, when non-nil, is a string representing
  14818. drawer's name. Otherwise, the user is prompted for a name.
  14819. If a region is active, insert the drawer around that region
  14820. instead.
  14821. Point is left between drawer's boundaries."
  14822. (interactive "P")
  14823. (let* ((drawer (if arg "PROPERTIES"
  14824. (or drawer (read-from-minibuffer "Drawer: ")))))
  14825. (cond
  14826. ;; With C-u, fall back on `org-insert-property-drawer'
  14827. (arg (org-insert-property-drawer))
  14828. ;; Check validity of suggested drawer's name.
  14829. ((not (org-string-match-p org-drawer-regexp (format ":%s:" drawer)))
  14830. (user-error "Invalid drawer name"))
  14831. ;; With an active region, insert a drawer at point.
  14832. ((not (org-region-active-p))
  14833. (progn
  14834. (unless (bolp) (insert "\n"))
  14835. (insert (format ":%s:\n\n:END:\n" drawer))
  14836. (forward-line -2)))
  14837. ;; Otherwise, insert the drawer at point
  14838. (t
  14839. (let ((rbeg (region-beginning))
  14840. (rend (copy-marker (region-end))))
  14841. (unwind-protect
  14842. (progn
  14843. (goto-char rbeg)
  14844. (beginning-of-line)
  14845. (when (save-excursion
  14846. (re-search-forward org-outline-regexp-bol rend t))
  14847. (user-error "Drawers cannot contain headlines"))
  14848. ;; Position point at the beginning of the first
  14849. ;; non-blank line in region. Insert drawer's opening
  14850. ;; there, then indent it.
  14851. (org-skip-whitespace)
  14852. (beginning-of-line)
  14853. (insert ":" drawer ":\n")
  14854. (forward-line -1)
  14855. (indent-for-tab-command)
  14856. ;; Move point to the beginning of the first blank line
  14857. ;; after the last non-blank line in region. Insert
  14858. ;; drawer's closing, then indent it.
  14859. (goto-char rend)
  14860. (skip-chars-backward " \r\t\n")
  14861. (insert "\n:END:")
  14862. (deactivate-mark t)
  14863. (indent-for-tab-command)
  14864. (unless (eolp) (insert "\n")))
  14865. ;; Clear marker, whatever the outcome of insertion is.
  14866. (set-marker rend nil)))))))
  14867. (defvar org-property-set-functions-alist nil
  14868. "Property set function alist.
  14869. Each entry should have the following format:
  14870. (PROPERTY . READ-FUNCTION)
  14871. The read function will be called with the same argument as
  14872. `org-completing-read'.")
  14873. (defun org-set-property-function (property)
  14874. "Get the function that should be used to set PROPERTY.
  14875. This is computed according to `org-property-set-functions-alist'."
  14876. (or (cdr (assoc property org-property-set-functions-alist))
  14877. 'org-completing-read))
  14878. (defun org-read-property-value (property)
  14879. "Read PROPERTY value from user."
  14880. (let* ((completion-ignore-case t)
  14881. (allowed (org-property-get-allowed-values nil property 'table))
  14882. (cur (org-entry-get nil property))
  14883. (prompt (concat property " value"
  14884. (if (and cur (string-match "\\S-" cur))
  14885. (concat " [" cur "]") "") ": "))
  14886. (set-function (org-set-property-function property))
  14887. (val (if allowed
  14888. (funcall set-function prompt allowed nil
  14889. (not (get-text-property 0 'org-unrestricted
  14890. (caar allowed))))
  14891. (let (org-completion-use-ido org-completion-use-iswitchb)
  14892. (funcall set-function prompt
  14893. (mapcar 'list (org-property-values property))
  14894. nil nil "" nil cur)))))
  14895. (org-trim val)))
  14896. (defvar org-last-set-property nil)
  14897. (defvar org-last-set-property-value nil)
  14898. (defun org-read-property-name ()
  14899. "Read a property name."
  14900. (let ((completion-ignore-case t)
  14901. (default-prop (or (and (org-at-property-p)
  14902. (org-match-string-no-properties 2))
  14903. org-last-set-property)))
  14904. (org-completing-read
  14905. (concat "Property"
  14906. (if default-prop (concat " [" default-prop "]") "")
  14907. ": ")
  14908. (mapcar #'list (org-buffer-property-keys nil t t))
  14909. nil nil nil nil default-prop)))
  14910. (defun org-set-property-and-value (use-last)
  14911. "Allow to set [PROPERTY]: [value] direction from prompt.
  14912. When use-default, don't even ask, just use the last
  14913. \"[PROPERTY]: [value]\" string from the history."
  14914. (interactive "P")
  14915. (let* ((completion-ignore-case t)
  14916. (pv (or (and use-last org-last-set-property-value)
  14917. (org-completing-read
  14918. "Enter a \"[Property]: [value]\" pair: "
  14919. nil nil nil nil nil
  14920. org-last-set-property-value)))
  14921. prop val)
  14922. (when (string-match "^[ \t]*\\([^:]+\\):[ \t]*\\(.*\\)[ \t]*$" pv)
  14923. (setq prop (match-string 1 pv)
  14924. val (match-string 2 pv))
  14925. (org-set-property prop val))))
  14926. (defun org-set-property (property value)
  14927. "In the current entry, set PROPERTY to VALUE.
  14928. When called interactively, this will prompt for a property name, offering
  14929. completion on existing and default properties. And then it will prompt
  14930. for a value, offering completion either on allowed values (via an inherited
  14931. xxx_ALL property) or on existing values in other instances of this property
  14932. in the current file."
  14933. (interactive (list nil nil))
  14934. (let* ((property (or property (org-read-property-name)))
  14935. (value (or value (org-read-property-value property)))
  14936. (fn (cdr (assoc property org-properties-postprocess-alist))))
  14937. (setq org-last-set-property property)
  14938. (setq org-last-set-property-value (concat property ": " value))
  14939. ;; Possibly postprocess the inserted value:
  14940. (when fn (setq value (funcall fn value)))
  14941. (unless (equal (org-entry-get nil property) value)
  14942. (org-entry-put nil property value))))
  14943. (defun org-find-property (property &optional value)
  14944. "Find first entry in buffer that sets PROPERTY.
  14945. When optional argument VALUE is non-nil, only consider an entry
  14946. if it contains PROPERTY set to this value. If PROPERTY should be
  14947. explicitly set to nil, use string \"nil\" for VALUE.
  14948. Return position where the entry begins, or nil if there is no
  14949. such entry. If narrowing is in effect, only search the visible
  14950. part of the buffer."
  14951. (save-excursion
  14952. (goto-char (point-min))
  14953. (let ((case-fold-search t)
  14954. (re (org-re-property property nil (not value) value)))
  14955. (catch 'exit
  14956. (while (re-search-forward re nil t)
  14957. (when (if value (org-at-property-p)
  14958. (org-entry-get (point) property nil t))
  14959. (throw 'exit (progn (org-back-to-heading t) (point)))))))))
  14960. (defun org-delete-property (property)
  14961. "In the current entry, delete PROPERTY."
  14962. (interactive
  14963. (let* ((completion-ignore-case t)
  14964. (cat (org-entry-get (point) "CATEGORY"))
  14965. (props0 (org-entry-properties nil 'standard))
  14966. (props (if cat props0
  14967. (delete `("CATEGORY" . ,(org-get-category)) props0)))
  14968. (prop (if (< 1 (length props))
  14969. (org-icompleting-read "Property: " props nil t)
  14970. (caar props))))
  14971. (list prop)))
  14972. (if (not property)
  14973. (message "No property to delete in this entry")
  14974. (org-entry-delete nil property)
  14975. (message "Property \"%s\" deleted" property)))
  14976. (defun org-delete-property-globally (property)
  14977. "Remove PROPERTY globally, from all entries.
  14978. This function ignores narrowing, if any."
  14979. (interactive
  14980. (let* ((completion-ignore-case t)
  14981. (prop (org-icompleting-read
  14982. "Globally remove property: "
  14983. (mapcar #'list (org-buffer-property-keys)))))
  14984. (list prop)))
  14985. (org-with-wide-buffer
  14986. (goto-char (point-min))
  14987. (let ((count 0)
  14988. (re (org-re-property (concat (regexp-quote property) "\\+?") t t)))
  14989. (while (re-search-forward re nil t)
  14990. (when (org-entry-delete (point) property) (incf count)))
  14991. (message "Property \"%s\" removed from %d entries" property count))))
  14992. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  14993. (defun org-compute-property-at-point ()
  14994. "Compute the property at point.
  14995. This looks for an enclosing column format, extracts the operator and
  14996. then applies it to the property in the column format's scope."
  14997. (interactive)
  14998. (unless (org-at-property-p)
  14999. (user-error "Not at a property"))
  15000. (let ((prop (org-match-string-no-properties 2)))
  15001. (org-columns-get-format-and-top-level)
  15002. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  15003. (user-error "No operator defined for property %s" prop))
  15004. (org-columns-compute prop)))
  15005. (defvar org-property-allowed-value-functions nil
  15006. "Hook for functions supplying allowed values for a specific property.
  15007. The functions must take a single argument, the name of the property, and
  15008. return a flat list of allowed values. If \":ETC\" is one of
  15009. the values, this means that these values are intended as defaults for
  15010. completion, but that other values should be allowed too.
  15011. The functions must return nil if they are not responsible for this
  15012. property.")
  15013. (defun org-property-get-allowed-values (pom property &optional table)
  15014. "Get allowed values for the property PROPERTY.
  15015. When TABLE is non-nil, return an alist that can directly be used for
  15016. completion."
  15017. (let (vals)
  15018. (cond
  15019. ((equal property "TODO")
  15020. (setq vals (org-with-point-at pom
  15021. (append org-todo-keywords-1 '("")))))
  15022. ((equal property "PRIORITY")
  15023. (let ((n org-lowest-priority))
  15024. (while (>= n org-highest-priority)
  15025. (push (char-to-string n) vals)
  15026. (setq n (1- n)))))
  15027. ((equal property "CATEGORY"))
  15028. ((member property org-special-properties))
  15029. ((setq vals (run-hook-with-args-until-success
  15030. 'org-property-allowed-value-functions property)))
  15031. (t
  15032. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  15033. (when (and vals (string-match "\\S-" vals))
  15034. (setq vals (car (read-from-string (concat "(" vals ")"))))
  15035. (setq vals (mapcar (lambda (x)
  15036. (cond ((stringp x) x)
  15037. ((numberp x) (number-to-string x))
  15038. ((symbolp x) (symbol-name x))
  15039. (t "???")))
  15040. vals)))))
  15041. (when (member ":ETC" vals)
  15042. (setq vals (remove ":ETC" vals))
  15043. (org-add-props (car vals) '(org-unrestricted t)))
  15044. (if table (mapcar 'list vals) vals)))
  15045. (defun org-property-previous-allowed-value (&optional previous)
  15046. "Switch to the next allowed value for this property."
  15047. (interactive)
  15048. (org-property-next-allowed-value t))
  15049. (defun org-property-next-allowed-value (&optional previous)
  15050. "Switch to the next allowed value for this property."
  15051. (interactive)
  15052. (unless (org-at-property-p)
  15053. (user-error "Not at a property"))
  15054. (let* ((prop (car (save-match-data (org-split-string (match-string 1) ":"))))
  15055. (key (match-string 2))
  15056. (value (match-string 3))
  15057. (allowed (or (org-property-get-allowed-values (point) key)
  15058. (and (member value '("[ ]" "[-]" "[X]"))
  15059. '("[ ]" "[X]"))))
  15060. (heading (save-match-data (nth 4 (org-heading-components))))
  15061. nval)
  15062. (unless allowed
  15063. (user-error "Allowed values for this property have not been defined"))
  15064. (when previous (setq allowed (reverse allowed)))
  15065. (when (member value allowed)
  15066. (setq nval (car (cdr (member value allowed)))))
  15067. (setq nval (or nval (car allowed)))
  15068. (when (equal nval value)
  15069. (user-error "Only one allowed value for this property"))
  15070. (org-at-property-p)
  15071. (replace-match (concat " :" key ": " nval) t t)
  15072. (org-indent-line)
  15073. (beginning-of-line 1)
  15074. (skip-chars-forward " \t")
  15075. (when (equal prop org-effort-property)
  15076. (org-refresh-property
  15077. '((effort . identity)
  15078. (effort-minutes . org-duration-string-to-minutes))
  15079. nval)
  15080. (when (string= org-clock-current-task heading)
  15081. (setq org-clock-effort nval)
  15082. (org-clock-update-mode-line)))
  15083. (run-hook-with-args 'org-property-changed-functions key nval)))
  15084. (defun org-find-olp (path &optional this-buffer)
  15085. "Return a marker pointing to the entry at outline path OLP.
  15086. If anything goes wrong, throw an error.
  15087. You can wrap this call to catch the error like this:
  15088. (condition-case msg
  15089. (org-mobile-locate-entry (match-string 4))
  15090. (error (nth 1 msg)))
  15091. The return value will then be either a string with the error message,
  15092. or a marker if everything is OK.
  15093. If THIS-BUFFER is set, the outline path does not contain a file,
  15094. only headings."
  15095. (let* ((file (if this-buffer buffer-file-name (pop path)))
  15096. (buffer (if this-buffer (current-buffer) (find-file-noselect file)))
  15097. (level 1)
  15098. (lmin 1)
  15099. (lmax 1)
  15100. limit re end found pos heading cnt flevel)
  15101. (unless buffer (error "File not found :%s" file))
  15102. (with-current-buffer buffer
  15103. (save-excursion
  15104. (save-restriction
  15105. (widen)
  15106. (setq limit (point-max))
  15107. (goto-char (point-min))
  15108. (dolist (heading path)
  15109. (setq re (format org-complex-heading-regexp-format
  15110. (regexp-quote heading)))
  15111. (setq cnt 0 pos (point))
  15112. (while (re-search-forward re end t)
  15113. (setq level (- (match-end 1) (match-beginning 1)))
  15114. (when (and (>= level lmin) (<= level lmax))
  15115. (setq found (match-beginning 0) flevel level cnt (1+ cnt))))
  15116. (when (= cnt 0) (error "Heading not found on level %d: %s"
  15117. lmax heading))
  15118. (when (> cnt 1) (error "Heading not unique on level %d: %s"
  15119. lmax heading))
  15120. (goto-char found)
  15121. (setq lmin (1+ flevel) lmax (+ lmin (if org-odd-levels-only 1 0)))
  15122. (setq end (save-excursion (org-end-of-subtree t t))))
  15123. (when (org-at-heading-p)
  15124. (point-marker)))))))
  15125. (defun org-find-exact-headline-in-buffer (heading &optional buffer pos-only)
  15126. "Find node HEADING in BUFFER.
  15127. Return a marker to the heading if it was found, or nil if not.
  15128. If POS-ONLY is set, return just the position instead of a marker.
  15129. The heading text must match exact, but it may have a TODO keyword,
  15130. a priority cookie and tags in the standard locations."
  15131. (with-current-buffer (or buffer (current-buffer))
  15132. (save-excursion
  15133. (save-restriction
  15134. (widen)
  15135. (goto-char (point-min))
  15136. (let (case-fold-search)
  15137. (when (re-search-forward
  15138. (format org-complex-heading-regexp-format
  15139. (regexp-quote heading)) nil t)
  15140. (if pos-only
  15141. (match-beginning 0)
  15142. (move-marker (make-marker) (match-beginning 0)))))))))
  15143. (defun org-find-exact-heading-in-directory (heading &optional dir)
  15144. "Find Org node headline HEADING in all .org files in directory DIR.
  15145. When the target headline is found, return a marker to this location."
  15146. (let ((files (directory-files (or dir default-directory)
  15147. t "\\`[^.#].*\\.org\\'"))
  15148. visiting m buffer)
  15149. (catch 'found
  15150. (dolist (file files)
  15151. (message "trying %s" file)
  15152. (setq visiting (org-find-base-buffer-visiting file))
  15153. (setq buffer (or visiting (find-file-noselect file)))
  15154. (setq m (org-find-exact-headline-in-buffer
  15155. heading buffer))
  15156. (when (and (not m) (not visiting)) (kill-buffer buffer))
  15157. (and m (throw 'found m))))))
  15158. (defun org-find-entry-with-id (ident)
  15159. "Locate the entry that contains the ID property with exact value IDENT.
  15160. IDENT can be a string, a symbol or a number, this function will search for
  15161. the string representation of it.
  15162. Return the position where this entry starts, or nil if there is no such entry."
  15163. (interactive "sID: ")
  15164. (let ((id (cond
  15165. ((stringp ident) ident)
  15166. ((symbolp ident) (symbol-name ident))
  15167. ((numberp ident) (number-to-string ident))
  15168. (t (error "IDENT %s must be a string, symbol or number" ident)))))
  15169. (org-with-wide-buffer (org-find-property "ID" id))))
  15170. ;;;; Timestamps
  15171. (defvar org-last-changed-timestamp nil)
  15172. (defvar org-last-inserted-timestamp nil
  15173. "The last time stamp inserted with `org-insert-time-stamp'.")
  15174. (defvar org-ts-what) ; dynamically scoped parameter
  15175. (defun org-time-stamp (arg &optional inactive)
  15176. "Prompt for a date/time and insert a time stamp.
  15177. If the user specifies a time like HH:MM or if this command is
  15178. called with at least one prefix argument, the time stamp contains
  15179. the date and the time. Otherwise, only the date is included.
  15180. All parts of a date not specified by the user are filled in from
  15181. the timestamp at point, if any, or the current date/time
  15182. otherwise.
  15183. If there is already a timestamp at the cursor, it is replaced.
  15184. With two universal prefix arguments, insert an active timestamp
  15185. with the current time without prompting the user.
  15186. When called from lisp, the timestamp is inactive if INACTIVE is
  15187. non-nil."
  15188. (interactive "P")
  15189. (let* ((ts (cond
  15190. ((org-at-date-range-p t)
  15191. (match-string (if (< (point) (- (match-beginning 2) 2)) 1 2)))
  15192. ((org-at-timestamp-p t) (match-string 0))))
  15193. ;; Default time is either the timestamp at point or today.
  15194. ;; When entering a range, only the range start is considered.
  15195. (default-time (if (not ts) (current-time)
  15196. (apply #'encode-time (org-parse-time-string ts))))
  15197. (default-input (and ts (org-get-compact-tod ts)))
  15198. (repeater (and ts
  15199. (string-match "\\([.+-]+[0-9]+[hdwmy] ?\\)+" ts)
  15200. (match-string 0 ts)))
  15201. org-time-was-given
  15202. org-end-time-was-given
  15203. (time
  15204. (and (if (equal arg '(16)) (current-time)
  15205. ;; Preserve `this-command' and `last-command'.
  15206. (let ((this-command this-command)
  15207. (last-command last-command))
  15208. (org-read-date
  15209. arg 'totime nil nil default-time default-input
  15210. inactive))))))
  15211. (cond
  15212. ((and ts
  15213. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  15214. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  15215. (insert "--")
  15216. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  15217. (ts
  15218. ;; Make sure we're on a timestamp. When in the middle of a date
  15219. ;; range, move arbitrarily to range end.
  15220. (unless (org-at-timestamp-p t)
  15221. (skip-chars-forward "-")
  15222. (org-at-timestamp-p t))
  15223. (replace-match "")
  15224. (setq org-last-changed-timestamp
  15225. (org-insert-time-stamp
  15226. time (or org-time-was-given arg)
  15227. inactive nil nil (list org-end-time-was-given)))
  15228. (when repeater
  15229. (backward-char)
  15230. (insert " " repeater)
  15231. (setq org-last-changed-timestamp
  15232. (concat (substring org-last-inserted-timestamp 0 -1)
  15233. " " repeater ">")))
  15234. (message "Timestamp updated"))
  15235. ((equal arg '(16)) (org-insert-time-stamp time t inactive))
  15236. (t (org-insert-time-stamp
  15237. time (or org-time-was-given arg) inactive nil nil
  15238. (list org-end-time-was-given))))))
  15239. ;; FIXME: can we use this for something else, like computing time differences?
  15240. (defun org-get-compact-tod (s)
  15241. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  15242. (let* ((t1 (match-string 1 s))
  15243. (h1 (string-to-number (match-string 2 s)))
  15244. (m1 (string-to-number (match-string 3 s)))
  15245. (t2 (and (match-end 4) (match-string 5 s)))
  15246. (h2 (and t2 (string-to-number (match-string 6 s))))
  15247. (m2 (and t2 (string-to-number (match-string 7 s))))
  15248. dh dm)
  15249. (if (not t2)
  15250. t1
  15251. (setq dh (- h2 h1) dm (- m2 m1))
  15252. (when (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  15253. (concat t1 "+" (number-to-string dh)
  15254. (and (/= 0 dm) (format ":%02d" dm)))))))
  15255. (defun org-time-stamp-inactive (&optional arg)
  15256. "Insert an inactive time stamp.
  15257. An inactive time stamp is enclosed in square brackets instead of angle
  15258. brackets. It is inactive in the sense that it does not trigger agenda entries,
  15259. does not link to the calendar and cannot be changed with the S-cursor keys.
  15260. So these are more for recording a certain time/date."
  15261. (interactive "P")
  15262. (org-time-stamp arg 'inactive))
  15263. (defvar org-date-ovl (make-overlay 1 1))
  15264. (overlay-put org-date-ovl 'face 'org-date-selected)
  15265. (org-detach-overlay org-date-ovl)
  15266. (defvar org-ans1) ; dynamically scoped parameter
  15267. (defvar org-ans2) ; dynamically scoped parameter
  15268. (defvar org-plain-time-of-day-regexp) ; defined below
  15269. (defvar org-overriding-default-time nil) ; dynamically scoped
  15270. (defvar org-read-date-overlay nil)
  15271. (defvar org-dcst nil) ; dynamically scoped
  15272. (defvar org-read-date-history nil)
  15273. (defvar org-read-date-final-answer nil)
  15274. (defvar org-read-date-analyze-futurep nil)
  15275. (defvar org-read-date-analyze-forced-year nil)
  15276. (defvar org-read-date-inactive)
  15277. (defvar org-read-date-minibuffer-local-map
  15278. (let* ((map (make-sparse-keymap)))
  15279. (set-keymap-parent map minibuffer-local-map)
  15280. (org-defkey map (kbd ".")
  15281. (lambda () (interactive)
  15282. ;; Are we at the beginning of the prompt?
  15283. (if (org-looking-back "^[^:]+: "
  15284. (let ((inhibit-field-text-motion t))
  15285. (line-beginning-position)))
  15286. (org-eval-in-calendar '(calendar-goto-today))
  15287. (insert "."))))
  15288. (org-defkey map (kbd "C-.")
  15289. (lambda () (interactive)
  15290. (org-eval-in-calendar '(calendar-goto-today))))
  15291. (org-defkey map [(meta shift left)]
  15292. (lambda () (interactive)
  15293. (org-eval-in-calendar '(calendar-backward-month 1))))
  15294. (org-defkey map [(meta shift right)]
  15295. (lambda () (interactive)
  15296. (org-eval-in-calendar '(calendar-forward-month 1))))
  15297. (org-defkey map [(meta shift up)]
  15298. (lambda () (interactive)
  15299. (org-eval-in-calendar '(calendar-backward-year 1))))
  15300. (org-defkey map [(meta shift down)]
  15301. (lambda () (interactive)
  15302. (org-eval-in-calendar '(calendar-forward-year 1))))
  15303. (org-defkey map [?\e (shift left)]
  15304. (lambda () (interactive)
  15305. (org-eval-in-calendar '(calendar-backward-month 1))))
  15306. (org-defkey map [?\e (shift right)]
  15307. (lambda () (interactive)
  15308. (org-eval-in-calendar '(calendar-forward-month 1))))
  15309. (org-defkey map [?\e (shift up)]
  15310. (lambda () (interactive)
  15311. (org-eval-in-calendar '(calendar-backward-year 1))))
  15312. (org-defkey map [?\e (shift down)]
  15313. (lambda () (interactive)
  15314. (org-eval-in-calendar '(calendar-forward-year 1))))
  15315. (org-defkey map [(shift up)]
  15316. (lambda () (interactive)
  15317. (org-eval-in-calendar '(calendar-backward-week 1))))
  15318. (org-defkey map [(shift down)]
  15319. (lambda () (interactive)
  15320. (org-eval-in-calendar '(calendar-forward-week 1))))
  15321. (org-defkey map [(shift left)]
  15322. (lambda () (interactive)
  15323. (org-eval-in-calendar '(calendar-backward-day 1))))
  15324. (org-defkey map [(shift right)]
  15325. (lambda () (interactive)
  15326. (org-eval-in-calendar '(calendar-forward-day 1))))
  15327. (org-defkey map "!"
  15328. (lambda () (interactive)
  15329. (org-eval-in-calendar '(diary-view-entries))
  15330. (message "")))
  15331. (org-defkey map ">"
  15332. (lambda () (interactive)
  15333. (org-eval-in-calendar '(calendar-scroll-left 1))))
  15334. (org-defkey map "<"
  15335. (lambda () (interactive)
  15336. (org-eval-in-calendar '(calendar-scroll-right 1))))
  15337. (org-defkey map "\C-v"
  15338. (lambda () (interactive)
  15339. (org-eval-in-calendar
  15340. '(calendar-scroll-left-three-months 1))))
  15341. (org-defkey map "\M-v"
  15342. (lambda () (interactive)
  15343. (org-eval-in-calendar
  15344. '(calendar-scroll-right-three-months 1))))
  15345. map)
  15346. "Keymap for minibuffer commands when using `org-read-date'.")
  15347. (defvar org-def)
  15348. (defvar org-defdecode)
  15349. (defvar org-with-time)
  15350. (defun org-read-date (&optional org-with-time to-time from-string prompt
  15351. default-time default-input inactive)
  15352. "Read a date, possibly a time, and make things smooth for the user.
  15353. The prompt will suggest to enter an ISO date, but you can also enter anything
  15354. which will at least partially be understood by `parse-time-string'.
  15355. Unrecognized parts of the date will default to the current day, month, year,
  15356. hour and minute. If this command is called to replace a timestamp at point,
  15357. or to enter the second timestamp of a range, the default time is taken
  15358. from the existing stamp. Furthermore, the command prefers the future,
  15359. so if you are giving a date where the year is not given, and the day-month
  15360. combination is already past in the current year, it will assume you
  15361. mean next year. For details, see the manual. A few examples:
  15362. 3-2-5 --> 2003-02-05
  15363. feb 15 --> currentyear-02-15
  15364. 2/15 --> currentyear-02-15
  15365. sep 12 9 --> 2009-09-12
  15366. 12:45 --> today 12:45
  15367. 22 sept 0:34 --> currentyear-09-22 0:34
  15368. 12 --> currentyear-currentmonth-12
  15369. Fri --> nearest Friday after today
  15370. -Tue --> last Tuesday
  15371. etc.
  15372. Furthermore you can specify a relative date by giving, as the *first* thing
  15373. in the input: a plus/minus sign, a number and a letter [hdwmy] to indicate
  15374. change in days weeks, months, years.
  15375. With a single plus or minus, the date is relative to today. With a double
  15376. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  15377. +4d --> four days from today
  15378. +4 --> same as above
  15379. +2w --> two weeks from today
  15380. ++5 --> five days from default date
  15381. The function understands only English month and weekday abbreviations.
  15382. While prompting, a calendar is popped up - you can also select the
  15383. date with the mouse (button 1). The calendar shows a period of three
  15384. months. To scroll it to other months, use the keys `>' and `<'.
  15385. If you don't like the calendar, turn it off with
  15386. (setq org-read-date-popup-calendar nil)
  15387. With optional argument TO-TIME, the date will immediately be converted
  15388. to an internal time.
  15389. With an optional argument ORG-WITH-TIME, the prompt will suggest to
  15390. also insert a time. Note that when ORG-WITH-TIME is not set, you can
  15391. still enter a time, and this function will inform the calling routine
  15392. about this change. The calling routine may then choose to change the
  15393. format used to insert the time stamp into the buffer to include the time.
  15394. With optional argument FROM-STRING, read from this string instead from
  15395. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  15396. the time/date that is used for everything that is not specified by the
  15397. user."
  15398. (require 'parse-time)
  15399. (let* ((org-time-stamp-rounding-minutes
  15400. (if (equal org-with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  15401. (org-dcst org-display-custom-times)
  15402. (ct (org-current-time))
  15403. (org-def (or org-overriding-default-time default-time ct))
  15404. (org-defdecode (decode-time org-def))
  15405. (dummy (progn
  15406. (when (< (nth 2 org-defdecode) org-extend-today-until)
  15407. (setcar (nthcdr 2 org-defdecode) -1)
  15408. (setcar (nthcdr 1 org-defdecode) 59)
  15409. (setq org-def (apply 'encode-time org-defdecode)
  15410. org-defdecode (decode-time org-def)))))
  15411. (cur-frame (selected-frame))
  15412. (mouse-autoselect-window nil) ; Don't let the mouse jump
  15413. (calendar-frame-setup nil)
  15414. (calendar-setup (when (eq calendar-setup 'calendar-only) 'calendar-only))
  15415. (calendar-move-hook nil)
  15416. (calendar-view-diary-initially-flag nil)
  15417. (calendar-view-holidays-initially-flag nil)
  15418. (timestr (format-time-string
  15419. (if org-with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") org-def))
  15420. (prompt (concat (if prompt (concat prompt " ") "")
  15421. (format "Date+time [%s]: " timestr)))
  15422. ans (org-ans0 "") org-ans1 org-ans2 final cal-frame)
  15423. (cond
  15424. (from-string (setq ans from-string))
  15425. (org-read-date-popup-calendar
  15426. (save-excursion
  15427. (save-window-excursion
  15428. (calendar)
  15429. (when (eq calendar-setup 'calendar-only)
  15430. (setq cal-frame
  15431. (window-frame (get-buffer-window "*Calendar*" 'visible)))
  15432. (select-frame cal-frame))
  15433. (org-eval-in-calendar '(setq cursor-type nil) t)
  15434. (unwind-protect
  15435. (progn
  15436. (calendar-forward-day (- (time-to-days org-def)
  15437. (calendar-absolute-from-gregorian
  15438. (calendar-current-date))))
  15439. (org-eval-in-calendar nil t)
  15440. (let* ((old-map (current-local-map))
  15441. (map (copy-keymap calendar-mode-map))
  15442. (minibuffer-local-map
  15443. (copy-keymap org-read-date-minibuffer-local-map)))
  15444. (org-defkey map (kbd "RET") 'org-calendar-select)
  15445. (org-defkey map [mouse-1] 'org-calendar-select-mouse)
  15446. (org-defkey map [mouse-2] 'org-calendar-select-mouse)
  15447. (unwind-protect
  15448. (progn
  15449. (use-local-map map)
  15450. (setq org-read-date-inactive inactive)
  15451. (add-hook 'post-command-hook 'org-read-date-display)
  15452. (setq org-ans0 (read-string prompt default-input
  15453. 'org-read-date-history nil))
  15454. ;; org-ans0: from prompt
  15455. ;; org-ans1: from mouse click
  15456. ;; org-ans2: from calendar motion
  15457. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  15458. (remove-hook 'post-command-hook 'org-read-date-display)
  15459. (use-local-map old-map)
  15460. (when org-read-date-overlay
  15461. (delete-overlay org-read-date-overlay)
  15462. (setq org-read-date-overlay nil)))))
  15463. (bury-buffer "*Calendar*")
  15464. (when cal-frame
  15465. (delete-frame cal-frame)
  15466. (select-frame-set-input-focus cur-frame))))))
  15467. (t ; Naked prompt only
  15468. (unwind-protect
  15469. (setq ans (read-string prompt default-input
  15470. 'org-read-date-history timestr))
  15471. (when org-read-date-overlay
  15472. (delete-overlay org-read-date-overlay)
  15473. (setq org-read-date-overlay nil)))))
  15474. (setq final (org-read-date-analyze ans org-def org-defdecode))
  15475. (when org-read-date-analyze-forced-year
  15476. (message "Year was forced into %s"
  15477. (if org-read-date-force-compatible-dates
  15478. "compatible range (1970-2037)"
  15479. "range representable on this machine"))
  15480. (ding))
  15481. ;; One round trip to get rid of 34th of August and stuff like that....
  15482. (setq final (decode-time (apply 'encode-time final)))
  15483. (setq org-read-date-final-answer ans)
  15484. (if to-time
  15485. (apply 'encode-time final)
  15486. (if (and (boundp 'org-time-was-given) org-time-was-given)
  15487. (format "%04d-%02d-%02d %02d:%02d"
  15488. (nth 5 final) (nth 4 final) (nth 3 final)
  15489. (nth 2 final) (nth 1 final))
  15490. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  15491. (defun org-read-date-display ()
  15492. "Display the current date prompt interpretation in the minibuffer."
  15493. (when org-read-date-display-live
  15494. (when org-read-date-overlay
  15495. (delete-overlay org-read-date-overlay))
  15496. (when (minibufferp (current-buffer))
  15497. (save-excursion
  15498. (end-of-line 1)
  15499. (while (not (equal (buffer-substring
  15500. (max (point-min) (- (point) 4)) (point))
  15501. " "))
  15502. (insert " ")))
  15503. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  15504. " " (or org-ans1 org-ans2)))
  15505. (org-end-time-was-given nil)
  15506. (f (org-read-date-analyze ans org-def org-defdecode))
  15507. (fmts (if org-dcst
  15508. org-time-stamp-custom-formats
  15509. org-time-stamp-formats))
  15510. (fmt (if (or org-with-time
  15511. (and (boundp 'org-time-was-given) org-time-was-given))
  15512. (cdr fmts)
  15513. (car fmts)))
  15514. (txt (format-time-string fmt (apply 'encode-time f)))
  15515. (txt (if org-read-date-inactive (concat "[" (substring txt 1 -1) "]") txt))
  15516. (txt (concat "=> " txt)))
  15517. (when (and org-end-time-was-given
  15518. (string-match org-plain-time-of-day-regexp txt))
  15519. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  15520. org-end-time-was-given
  15521. (substring txt (match-end 0)))))
  15522. (when org-read-date-analyze-futurep
  15523. (setq txt (concat txt " (=>F)")))
  15524. (setq org-read-date-overlay
  15525. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  15526. (org-overlay-display org-read-date-overlay txt 'secondary-selection)))))
  15527. (defun org-read-date-analyze (ans org-def org-defdecode)
  15528. "Analyze the combined answer of the date prompt."
  15529. ;; FIXME: cleanup and comment
  15530. ;; Pass `current-time' result to `decode-time' (instead of calling
  15531. ;; without arguments) so that only `current-time' has to be
  15532. ;; overriden in tests.
  15533. (let ((nowdecode (decode-time (current-time)))
  15534. delta deltan deltaw deltadef year month day
  15535. hour minute second wday pm h2 m2 tl wday1
  15536. iso-year iso-weekday iso-week iso-year iso-date futurep kill-year)
  15537. (setq org-read-date-analyze-futurep nil
  15538. org-read-date-analyze-forced-year nil)
  15539. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  15540. (setq ans "+0"))
  15541. (when (setq delta (org-read-date-get-relative ans (current-time) org-def))
  15542. (setq ans (replace-match "" t t ans)
  15543. deltan (car delta)
  15544. deltaw (nth 1 delta)
  15545. deltadef (nth 2 delta)))
  15546. ;; Check if there is an iso week date in there. If yes, store the
  15547. ;; info and postpone interpreting it until the rest of the parsing
  15548. ;; is done.
  15549. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  15550. (setq iso-year (when (match-end 1)
  15551. (org-small-year-to-year
  15552. (string-to-number (match-string 1 ans))))
  15553. iso-weekday (when (match-end 3)
  15554. (string-to-number (match-string 3 ans)))
  15555. iso-week (string-to-number (match-string 2 ans)))
  15556. (setq ans (replace-match "" t t ans)))
  15557. ;; Help matching ISO dates with single digit month or day, like 2006-8-11.
  15558. (when (string-match
  15559. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  15560. (setq year (if (match-end 2)
  15561. (string-to-number (match-string 2 ans))
  15562. (progn (setq kill-year t)
  15563. (string-to-number (format-time-string "%Y"))))
  15564. month (string-to-number (match-string 3 ans))
  15565. day (string-to-number (match-string 4 ans)))
  15566. (when (< year 100) (setq year (+ 2000 year)))
  15567. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  15568. t nil ans)))
  15569. ;; Help matching dotted european dates
  15570. (when (string-match
  15571. "^ *\\(3[01]\\|0?[1-9]\\|[12][0-9]\\)\\. ?\\(0?[1-9]\\|1[012]\\)\\.\\( ?[1-9][0-9]\\{3\\}\\)?" ans)
  15572. (setq year (if (match-end 3) (string-to-number (match-string 3 ans))
  15573. (setq kill-year t)
  15574. (string-to-number (format-time-string "%Y")))
  15575. day (string-to-number (match-string 1 ans))
  15576. month (string-to-number (match-string 2 ans))
  15577. ans (replace-match (format "%04d-%02d-%02d" year month day)
  15578. t nil ans)))
  15579. ;; Help matching american dates, like 5/30 or 5/30/7
  15580. (when (string-match
  15581. "^ *\\(0?[1-9]\\|1[012]\\)/\\(0?[1-9]\\|[12][0-9]\\|3[01]\\)\\(/\\([0-9]+\\)\\)?\\([^/0-9]\\|$\\)" ans)
  15582. (setq year (if (match-end 4)
  15583. (string-to-number (match-string 4 ans))
  15584. (progn (setq kill-year t)
  15585. (string-to-number (format-time-string "%Y"))))
  15586. month (string-to-number (match-string 1 ans))
  15587. day (string-to-number (match-string 2 ans)))
  15588. (when (< year 100) (setq year (+ 2000 year)))
  15589. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  15590. t nil ans)))
  15591. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  15592. ;; If there is a time with am/pm, and *no* time without it, we convert
  15593. ;; so that matching will be successful.
  15594. (loop for i from 1 to 2 do ; twice, for end time as well
  15595. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  15596. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  15597. (setq hour (string-to-number (match-string 1 ans))
  15598. minute (if (match-end 3)
  15599. (string-to-number (match-string 3 ans))
  15600. 0)
  15601. pm (equal ?p
  15602. (string-to-char (downcase (match-string 4 ans)))))
  15603. (if (and (= hour 12) (not pm))
  15604. (setq hour 0)
  15605. (when (and pm (< hour 12)) (setq hour (+ 12 hour))))
  15606. (setq ans (replace-match (format "%02d:%02d" hour minute)
  15607. t t ans))))
  15608. ;; Check if a time range is given as a duration
  15609. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  15610. (setq hour (string-to-number (match-string 1 ans))
  15611. h2 (+ hour (string-to-number (match-string 3 ans)))
  15612. minute (string-to-number (match-string 2 ans))
  15613. m2 (+ minute (if (match-end 5) (string-to-number
  15614. (match-string 5 ans))0)))
  15615. (when (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  15616. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  15617. t t ans)))
  15618. ;; Check if there is a time range
  15619. (when (boundp 'org-end-time-was-given)
  15620. (setq org-time-was-given nil)
  15621. (when (and (string-match org-plain-time-of-day-regexp ans)
  15622. (match-end 8))
  15623. (setq org-end-time-was-given (match-string 8 ans))
  15624. (setq ans (concat (substring ans 0 (match-beginning 7))
  15625. (substring ans (match-end 7))))))
  15626. (setq tl (parse-time-string ans)
  15627. day (or (nth 3 tl) (nth 3 org-defdecode))
  15628. month
  15629. (cond ((nth 4 tl))
  15630. ((not org-read-date-prefer-future) (nth 4 org-defdecode))
  15631. ;; Day was specified. Make sure DAY+MONTH
  15632. ;; combination happens in the future.
  15633. ((nth 3 tl)
  15634. (setq futurep t)
  15635. (if (< day (nth 3 nowdecode)) (1+ (nth 4 nowdecode))
  15636. (nth 4 nowdecode)))
  15637. (t (nth 4 org-defdecode)))
  15638. year
  15639. (cond ((and (not kill-year) (nth 5 tl)))
  15640. ((not org-read-date-prefer-future) (nth 5 org-defdecode))
  15641. ;; Month was guessed in the future and is at least
  15642. ;; equal to NOWDECODE's. Fix year accordingly.
  15643. (futurep
  15644. (if (or (> month (nth 4 nowdecode))
  15645. (>= day (nth 3 nowdecode)))
  15646. (nth 5 nowdecode)
  15647. (1+ (nth 5 nowdecode))))
  15648. ;; Month was specified. Make sure MONTH+YEAR
  15649. ;; combination happens in the future.
  15650. ((nth 4 tl)
  15651. (setq futurep t)
  15652. (cond ((> month (nth 4 nowdecode)) (nth 5 nowdecode))
  15653. ((< month (nth 4 nowdecode)) (1+ (nth 5 nowdecode)))
  15654. ((< day (nth 3 nowdecode)) (1+ (nth 5 nowdecode)))
  15655. (t (nth 5 nowdecode))))
  15656. (t (nth 5 org-defdecode)))
  15657. hour (or (nth 2 tl) (nth 2 org-defdecode))
  15658. minute (or (nth 1 tl) (nth 1 org-defdecode))
  15659. second (or (nth 0 tl) 0)
  15660. wday (nth 6 tl))
  15661. (when (and (eq org-read-date-prefer-future 'time)
  15662. (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
  15663. (equal day (nth 3 nowdecode))
  15664. (equal month (nth 4 nowdecode))
  15665. (equal year (nth 5 nowdecode))
  15666. (nth 2 tl)
  15667. (or (< (nth 2 tl) (nth 2 nowdecode))
  15668. (and (= (nth 2 tl) (nth 2 nowdecode))
  15669. (nth 1 tl)
  15670. (< (nth 1 tl) (nth 1 nowdecode)))))
  15671. (setq day (1+ day)
  15672. futurep t))
  15673. ;; Special date definitions below
  15674. (cond
  15675. (iso-week
  15676. ;; There was an iso week
  15677. (require 'cal-iso)
  15678. (setq futurep nil)
  15679. (setq year (or iso-year year)
  15680. day (or iso-weekday wday 1)
  15681. wday nil ; to make sure that the trigger below does not match
  15682. iso-date (calendar-gregorian-from-absolute
  15683. (calendar-iso-to-absolute
  15684. (list iso-week day year))))
  15685. ; FIXME: Should we also push ISO weeks into the future?
  15686. ; (when (and org-read-date-prefer-future
  15687. ; (not iso-year)
  15688. ; (< (calendar-absolute-from-gregorian iso-date)
  15689. ; (time-to-days (current-time))))
  15690. ; (setq year (1+ year)
  15691. ; iso-date (calendar-gregorian-from-absolute
  15692. ; (calendar-iso-to-absolute
  15693. ; (list iso-week day year)))))
  15694. (setq month (car iso-date)
  15695. year (nth 2 iso-date)
  15696. day (nth 1 iso-date)))
  15697. (deltan
  15698. (setq futurep nil)
  15699. (unless deltadef
  15700. ;; Pass `current-time' result to `decode-time' (instead of
  15701. ;; calling without arguments) so that only `current-time' has
  15702. ;; to be overriden in tests.
  15703. (let ((now (decode-time (current-time))))
  15704. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  15705. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  15706. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  15707. ((equal deltaw "m") (setq month (+ month deltan)))
  15708. ((equal deltaw "y") (setq year (+ year deltan)))))
  15709. ((and wday (not (nth 3 tl)))
  15710. ;; Weekday was given, but no day, so pick that day in the week
  15711. ;; on or after the derived date.
  15712. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  15713. (unless (equal wday wday1)
  15714. (setq day (+ day (% (- wday wday1 -7) 7))))))
  15715. (when (and (boundp 'org-time-was-given)
  15716. (nth 2 tl))
  15717. (setq org-time-was-given t))
  15718. (when (< year 100) (setq year (+ 2000 year)))
  15719. ;; Check of the date is representable
  15720. (if org-read-date-force-compatible-dates
  15721. (progn
  15722. (when (< year 1970)
  15723. (setq year 1970 org-read-date-analyze-forced-year t))
  15724. (when (> year 2037)
  15725. (setq year 2037 org-read-date-analyze-forced-year t)))
  15726. (condition-case nil
  15727. (ignore (encode-time second minute hour day month year))
  15728. (error
  15729. (setq year (nth 5 org-defdecode))
  15730. (setq org-read-date-analyze-forced-year t))))
  15731. (setq org-read-date-analyze-futurep futurep)
  15732. (list second minute hour day month year)))
  15733. (defvar parse-time-weekdays)
  15734. (defun org-read-date-get-relative (s today default)
  15735. "Check string S for special relative date string.
  15736. TODAY and DEFAULT are internal times, for today and for a default.
  15737. Return shift list (N what def-flag)
  15738. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  15739. N is the number of WHATs to shift.
  15740. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  15741. the DEFAULT date rather than TODAY."
  15742. (require 'parse-time)
  15743. (when (and
  15744. (string-match
  15745. (concat
  15746. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  15747. "\\([0-9]+\\)?"
  15748. "\\([hdwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  15749. "\\([ \t]\\|$\\)") s)
  15750. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  15751. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  15752. (string-to-char (substring (match-string 1 s) -1))
  15753. ?+))
  15754. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  15755. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  15756. (what (if (match-end 3) (match-string 3 s) "d"))
  15757. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  15758. (date (if rel default today))
  15759. (wday (nth 6 (decode-time date)))
  15760. delta)
  15761. (if wday1
  15762. (progn
  15763. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  15764. (when (= delta 0) (setq delta 7))
  15765. (when (= dir ?-)
  15766. (setq delta (- delta 7))
  15767. (when (= delta 0) (setq delta -7)))
  15768. (when (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  15769. (list delta "d" rel))
  15770. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  15771. (defun org-order-calendar-date-args (arg1 arg2 arg3)
  15772. "Turn a user-specified date into the internal representation.
  15773. The internal representation needed by the calendar is (month day year).
  15774. This is a wrapper to handle the brain-dead convention in calendar that
  15775. user function argument order change dependent on argument order."
  15776. (if (boundp 'calendar-date-style)
  15777. (cond
  15778. ((eq calendar-date-style 'american)
  15779. (list arg1 arg2 arg3))
  15780. ((eq calendar-date-style 'european)
  15781. (list arg2 arg1 arg3))
  15782. ((eq calendar-date-style 'iso)
  15783. (list arg2 arg3 arg1)))
  15784. (org-no-warnings ;; european-calendar-style is obsolete as of version 23.1
  15785. (if (org-bound-and-true-p european-calendar-style)
  15786. (list arg2 arg1 arg3)
  15787. (list arg1 arg2 arg3)))))
  15788. (defun org-eval-in-calendar (form &optional keepdate)
  15789. "Eval FORM in the calendar window and return to current window.
  15790. When KEEPDATE is non-nil, update `org-ans2' from the cursor date,
  15791. otherwise stick to the current value of `org-ans2'."
  15792. (let ((sf (selected-frame))
  15793. (sw (selected-window)))
  15794. (select-window (get-buffer-window "*Calendar*" t))
  15795. (eval form)
  15796. (when (and (not keepdate) (calendar-cursor-to-date))
  15797. (let* ((date (calendar-cursor-to-date))
  15798. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15799. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  15800. (move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  15801. (select-window sw)
  15802. (org-select-frame-set-input-focus sf)))
  15803. (defun org-calendar-select ()
  15804. "Return to `org-read-date' with the date currently selected.
  15805. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  15806. (interactive)
  15807. (when (calendar-cursor-to-date)
  15808. (let* ((date (calendar-cursor-to-date))
  15809. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15810. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  15811. (when (active-minibuffer-window) (exit-minibuffer))))
  15812. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  15813. "Insert a date stamp for the date given by the internal TIME.
  15814. See `format-time-string' for the format of TIME.
  15815. WITH-HM means use the stamp format that includes the time of the day.
  15816. INACTIVE means use square brackets instead of angular ones, so that the
  15817. stamp will not contribute to the agenda.
  15818. PRE and POST are optional strings to be inserted before and after the
  15819. stamp.
  15820. The command returns the inserted time stamp."
  15821. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  15822. stamp)
  15823. (when inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  15824. (insert-before-markers (or pre ""))
  15825. (when (listp extra)
  15826. (setq extra (car extra))
  15827. (if (and (stringp extra)
  15828. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  15829. (setq extra (format "-%02d:%02d"
  15830. (string-to-number (match-string 1 extra))
  15831. (string-to-number (match-string 2 extra))))
  15832. (setq extra nil)))
  15833. (when extra
  15834. (setq fmt (concat (substring fmt 0 -1) extra (substring fmt -1))))
  15835. (insert-before-markers (setq stamp (format-time-string fmt time)))
  15836. (insert-before-markers (or post ""))
  15837. (setq org-last-inserted-timestamp stamp)))
  15838. (defun org-toggle-time-stamp-overlays ()
  15839. "Toggle the use of custom time stamp formats."
  15840. (interactive)
  15841. (setq org-display-custom-times (not org-display-custom-times))
  15842. (unless org-display-custom-times
  15843. (let ((p (point-min)) (bmp (buffer-modified-p)))
  15844. (while (setq p (next-single-property-change p 'display))
  15845. (when (and (get-text-property p 'display)
  15846. (eq (get-text-property p 'face) 'org-date))
  15847. (remove-text-properties
  15848. p (setq p (next-single-property-change p 'display))
  15849. '(display t))))
  15850. (set-buffer-modified-p bmp)))
  15851. (when (featurep 'xemacs)
  15852. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  15853. (org-restart-font-lock)
  15854. (setq org-table-may-need-update t)
  15855. (if org-display-custom-times
  15856. (message "Time stamps are overlaid with custom format")
  15857. (message "Time stamp overlays removed")))
  15858. (defun org-display-custom-time (beg end)
  15859. "Overlay modified time stamp format over timestamp between BEG and END."
  15860. (let* ((ts (buffer-substring beg end))
  15861. t1 w1 with-hm tf time str w2 (off 0))
  15862. (save-match-data
  15863. (setq t1 (org-parse-time-string ts t))
  15864. (when (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)?\\'" ts)
  15865. (setq off (- (match-end 0) (match-beginning 0)))))
  15866. (setq end (- end off))
  15867. (setq w1 (- end beg)
  15868. with-hm (and (nth 1 t1) (nth 2 t1))
  15869. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  15870. time (org-fix-decoded-time t1)
  15871. str (org-add-props
  15872. (format-time-string
  15873. (substring tf 1 -1) (apply 'encode-time time))
  15874. nil 'mouse-face 'highlight)
  15875. w2 (length str))
  15876. (unless (= w2 w1)
  15877. (add-text-properties (1+ beg) (+ 2 beg)
  15878. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  15879. (if (featurep 'xemacs)
  15880. (progn
  15881. (put-text-property beg end 'invisible t)
  15882. (put-text-property beg end 'end-glyph (make-glyph str)))
  15883. (put-text-property beg end 'display str))))
  15884. (defun org-fix-decoded-time (time)
  15885. "Set 0 instead of nil for the first 6 elements of time.
  15886. Don't touch the rest."
  15887. (let ((n 0))
  15888. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  15889. (define-obsolete-function-alias 'org-days-to-time 'org-time-stamp-to-now "24.4")
  15890. (defun org-time-stamp-to-now (timestamp-string &optional seconds)
  15891. "Difference between TIMESTAMP-STRING and now in days.
  15892. If SECONDS is non-nil, return the difference in seconds."
  15893. (let ((fdiff (if seconds 'org-float-time 'time-to-days)))
  15894. (- (funcall fdiff (org-time-string-to-time timestamp-string))
  15895. (funcall fdiff (current-time)))))
  15896. (defun org-deadline-close (timestamp-string &optional ndays)
  15897. "Is the time in TIMESTAMP-STRING close to the current date?"
  15898. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  15899. (and (< (org-time-stamp-to-now timestamp-string) ndays)
  15900. (not (org-entry-is-done-p))))
  15901. (defun org-get-wdays (ts &optional delay zero-delay)
  15902. "Get the deadline lead time appropriate for timestring TS.
  15903. When DELAY is non-nil, get the delay time for scheduled items
  15904. instead of the deadline lead time. When ZERO-DELAY is non-nil
  15905. and `org-scheduled-delay-days' is 0, enforce 0 as the delay,
  15906. don't try to find the delay cookie in the scheduled timestamp."
  15907. (let ((tv (if delay org-scheduled-delay-days
  15908. org-deadline-warning-days)))
  15909. (cond
  15910. ((or (and delay (< tv 0))
  15911. (and delay zero-delay (<= tv 0))
  15912. (and (not delay) (<= tv 0)))
  15913. ;; Enforce this value no matter what
  15914. (- tv))
  15915. ((string-match "-\\([0-9]+\\)\\([hdwmy]\\)\\(\\'\\|>\\| \\)" ts)
  15916. ;; lead time is specified.
  15917. (floor (* (string-to-number (match-string 1 ts))
  15918. (cdr (assoc (match-string 2 ts)
  15919. '(("d" . 1) ("w" . 7)
  15920. ("m" . 30.4) ("y" . 365.25)
  15921. ("h" . 0.041667)))))))
  15922. ;; go for the default.
  15923. (t tv))))
  15924. (defun org-calendar-select-mouse (ev)
  15925. "Return to `org-read-date' with the date currently selected.
  15926. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  15927. (interactive "e")
  15928. (mouse-set-point ev)
  15929. (when (calendar-cursor-to-date)
  15930. (let* ((date (calendar-cursor-to-date))
  15931. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15932. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  15933. (when (active-minibuffer-window) (exit-minibuffer))))
  15934. (defun org-check-deadlines (ndays)
  15935. "Check if there are any deadlines due or past due.
  15936. A deadline is considered due if it happens within `org-deadline-warning-days'
  15937. days from today's date. If the deadline appears in an entry marked DONE,
  15938. it is not shown. The prefix arg NDAYS can be used to test that many
  15939. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  15940. (interactive "P")
  15941. (let* ((org-warn-days
  15942. (cond
  15943. ((equal ndays '(4)) 100000)
  15944. (ndays (prefix-numeric-value ndays))
  15945. (t (abs org-deadline-warning-days))))
  15946. (case-fold-search nil)
  15947. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  15948. (callback
  15949. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  15950. (message "%d deadlines past-due or due within %d days"
  15951. (org-occur regexp nil callback)
  15952. org-warn-days)))
  15953. (defsubst org-re-timestamp (type)
  15954. "Return a regexp for timestamp TYPE.
  15955. Allowed values for TYPE are:
  15956. all: all timestamps
  15957. active: only active timestamps (<...>)
  15958. inactive: only inactive timestamps ([...])
  15959. scheduled: only scheduled timestamps
  15960. deadline: only deadline timestamps
  15961. closed: only closed time-stamps
  15962. When TYPE is nil, fall back on returning a regexp that matches
  15963. both scheduled and deadline timestamps."
  15964. (case type
  15965. (all org-ts-regexp-both)
  15966. (active org-ts-regexp)
  15967. (inactive org-ts-regexp-inactive)
  15968. (scheduled org-scheduled-time-regexp)
  15969. (deadline org-deadline-time-regexp)
  15970. (closed org-closed-time-regexp)
  15971. (otherwise
  15972. (concat "\\<"
  15973. (regexp-opt (list org-deadline-string org-scheduled-string))
  15974. " *<\\([^>]+\\)>"))))
  15975. (defun org-check-before-date (date)
  15976. "Check if there are deadlines or scheduled entries before DATE."
  15977. (interactive (list (org-read-date)))
  15978. (let ((case-fold-search nil)
  15979. (regexp (org-re-timestamp org-ts-type))
  15980. (callback
  15981. `(lambda ()
  15982. (let ((match (match-string 1)))
  15983. (and ,(if (memq org-ts-type '(active inactive all))
  15984. '(eq (org-element-type (org-element-context)) 'timestamp)
  15985. '(org-at-planning-p))
  15986. (time-less-p
  15987. (org-time-string-to-time match)
  15988. (org-time-string-to-time date)))))))
  15989. (message "%d entries before %s"
  15990. (org-occur regexp nil callback) date)))
  15991. (defun org-check-after-date (date)
  15992. "Check if there are deadlines or scheduled entries after DATE."
  15993. (interactive (list (org-read-date)))
  15994. (let ((case-fold-search nil)
  15995. (regexp (org-re-timestamp org-ts-type))
  15996. (callback
  15997. `(lambda ()
  15998. (let ((match (match-string 1)))
  15999. (and ,(if (memq org-ts-type '(active inactive all))
  16000. '(eq (org-element-type (org-element-context)) 'timestamp)
  16001. '(org-at-planning-p))
  16002. (not (time-less-p
  16003. (org-time-string-to-time match)
  16004. (org-time-string-to-time date))))))))
  16005. (message "%d entries after %s"
  16006. (org-occur regexp nil callback) date)))
  16007. (defun org-check-dates-range (start-date end-date)
  16008. "Check for deadlines/scheduled entries between START-DATE and END-DATE."
  16009. (interactive (list (org-read-date nil nil nil "Range starts")
  16010. (org-read-date nil nil nil "Range end")))
  16011. (let ((case-fold-search nil)
  16012. (regexp (org-re-timestamp org-ts-type))
  16013. (callback
  16014. `(lambda ()
  16015. (let ((match (match-string 1)))
  16016. (and
  16017. ,(if (memq org-ts-type '(active inactive all))
  16018. '(eq (org-element-type (org-element-context)) 'timestamp)
  16019. '(org-at-planning-p))
  16020. (not (time-less-p
  16021. (org-time-string-to-time match)
  16022. (org-time-string-to-time start-date)))
  16023. (time-less-p
  16024. (org-time-string-to-time match)
  16025. (org-time-string-to-time end-date)))))))
  16026. (message "%d entries between %s and %s"
  16027. (org-occur regexp nil callback) start-date end-date)))
  16028. (defun org-evaluate-time-range (&optional to-buffer)
  16029. "Evaluate a time range by computing the difference between start and end.
  16030. Normally the result is just printed in the echo area, but with prefix arg
  16031. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  16032. If the time range is actually in a table, the result is inserted into the
  16033. next column.
  16034. For time difference computation, a year is assumed to be exactly 365
  16035. days in order to avoid rounding problems."
  16036. (interactive "P")
  16037. (or
  16038. (org-clock-update-time-maybe)
  16039. (save-excursion
  16040. (unless (org-at-date-range-p t)
  16041. (goto-char (point-at-bol))
  16042. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  16043. (unless (org-at-date-range-p t)
  16044. (user-error "Not at a time-stamp range, and none found in current line")))
  16045. (let* ((ts1 (match-string 1))
  16046. (ts2 (match-string 2))
  16047. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  16048. (match-end (match-end 0))
  16049. (time1 (org-time-string-to-time ts1))
  16050. (time2 (org-time-string-to-time ts2))
  16051. (t1 (org-float-time time1))
  16052. (t2 (org-float-time time2))
  16053. (diff (abs (- t2 t1)))
  16054. (negative (< (- t2 t1) 0))
  16055. ;; (ys (floor (* 365 24 60 60)))
  16056. (ds (* 24 60 60))
  16057. (hs (* 60 60))
  16058. (fy "%dy %dd %02d:%02d")
  16059. (fy1 "%dy %dd")
  16060. (fd "%dd %02d:%02d")
  16061. (fd1 "%dd")
  16062. (fh "%02d:%02d")
  16063. y d h m align)
  16064. (if havetime
  16065. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  16066. y 0
  16067. d (floor (/ diff ds)) diff (mod diff ds)
  16068. h (floor (/ diff hs)) diff (mod diff hs)
  16069. m (floor (/ diff 60)))
  16070. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  16071. y 0
  16072. d (floor (+ (/ diff ds) 0.5))
  16073. h 0 m 0))
  16074. (if (not to-buffer)
  16075. (message "%s" (org-make-tdiff-string y d h m))
  16076. (if (org-at-table-p)
  16077. (progn
  16078. (goto-char match-end)
  16079. (setq align t)
  16080. (and (looking-at " *|") (goto-char (match-end 0))))
  16081. (goto-char match-end))
  16082. (when (looking-at
  16083. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  16084. (replace-match ""))
  16085. (when negative (insert " -"))
  16086. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  16087. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  16088. (insert " " (format fh h m))))
  16089. (when align (org-table-align))
  16090. (message "Time difference inserted")))))
  16091. (defun org-make-tdiff-string (y d h m)
  16092. (let ((fmt "")
  16093. (l nil))
  16094. (when (> y 0)
  16095. (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " "))
  16096. (push y l))
  16097. (when (> d 0)
  16098. (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " "))
  16099. (push d l))
  16100. (when (> h 0)
  16101. (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " "))
  16102. (push h l))
  16103. (when (> m 0)
  16104. (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " "))
  16105. (push m l))
  16106. (apply 'format fmt (nreverse l))))
  16107. (defun org-time-string-to-time (s &optional buffer pos)
  16108. "Convert a timestamp string into internal time."
  16109. (condition-case errdata
  16110. (apply 'encode-time (org-parse-time-string s))
  16111. (error (error "Bad timestamp `%s'%s\nError was: %s"
  16112. s (if (not (and buffer pos))
  16113. ""
  16114. (format-message " at %d in buffer `%s'" pos buffer))
  16115. (cdr errdata)))))
  16116. (defun org-time-string-to-seconds (s)
  16117. "Convert a timestamp string to a number of seconds."
  16118. (org-float-time (org-time-string-to-time s)))
  16119. (org-define-error 'org-diary-sexp-no-match "Unable to match diary sexp")
  16120. (defun org-time-string-to-absolute (s &optional daynr prefer show-all buffer pos)
  16121. "Convert time stamp S to an absolute day number.
  16122. If DAYNR in non-nil, and there is a specifier for a cyclic time
  16123. stamp, get the closest date to DAYNR. If PREFER is
  16124. `past' (respectively `future') return a date past (respectively
  16125. after) or equal to DAYNR.
  16126. POS is the location of time stamp S, as a buffer position in
  16127. BUFFER.
  16128. Diary sexp timestamps are matched against DAYNR, when non-nil.
  16129. If matching fails or DAYNR is nil, `org-diary-sexp-no-match' is
  16130. signalled."
  16131. (cond
  16132. ((string-match "\\`%%\\((.*)\\)" s)
  16133. ;; Sexp timestamp: try to match DAYNR, if available, since we're
  16134. ;; only able to match individual dates. If it fails, raise an
  16135. ;; error.
  16136. (if (and daynr
  16137. (org-diary-sexp-entry
  16138. (match-string 1 s) "" (calendar-gregorian-from-absolute daynr)))
  16139. daynr
  16140. (signal 'org-diary-sexp-no-match (list s))))
  16141. ((and daynr show-all) (org-closest-date s daynr prefer))
  16142. (t (time-to-days
  16143. (condition-case errdata
  16144. (apply #'encode-time (org-parse-time-string s))
  16145. (error (error "Bad timestamp `%s'%s\nError was: %s"
  16146. s
  16147. (if (not (and buffer pos)) ""
  16148. (format-message " at %d in buffer `%s'" pos buffer))
  16149. (cdr errdata))))))))
  16150. (defun org-days-to-iso-week (days)
  16151. "Return the iso week number."
  16152. (require 'cal-iso)
  16153. (car (calendar-iso-from-absolute days)))
  16154. (defun org-small-year-to-year (year)
  16155. "Convert 2-digit years into 4-digit years.
  16156. YEAR is expanded into one of the 30 next years, if possible, or
  16157. into a past one. Any year larger than 99 is returned unchanged."
  16158. (if (>= year 100) year
  16159. (let* ((current (string-to-number (format-time-string "%Y" (current-time))))
  16160. (century (/ current 100))
  16161. (offset (- year (% current 100))))
  16162. (cond ((> offset 30) (+ (* (1- century) 100) year))
  16163. ((> offset -70) (+ (* century 100) year))
  16164. (t (+ (* (1+ century) 100) year))))))
  16165. (defun org-time-from-absolute (d)
  16166. "Return the time corresponding to date D.
  16167. D may be an absolute day number, or a calendar-type list (month day year)."
  16168. (when (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  16169. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  16170. (defun org-calendar-holiday ()
  16171. "List of holidays, for Diary display in Org-mode."
  16172. (require 'holidays)
  16173. (let ((hl (funcall
  16174. (if (fboundp 'calendar-check-holidays)
  16175. 'calendar-check-holidays 'check-calendar-holidays) date)))
  16176. (when hl (mapconcat 'identity hl "; "))))
  16177. (defun org-diary-sexp-entry (sexp entry date)
  16178. "Process a SEXP diary ENTRY for DATE."
  16179. (require 'diary-lib)
  16180. (let ((result (if calendar-debug-sexp
  16181. (let ((stack-trace-on-error t))
  16182. (eval (car (read-from-string sexp))))
  16183. (condition-case nil
  16184. (eval (car (read-from-string sexp)))
  16185. (error
  16186. (beep)
  16187. (message "Bad sexp at line %d in %s: %s"
  16188. (org-current-line)
  16189. (buffer-file-name) sexp)
  16190. (sleep-for 2))))))
  16191. (cond ((stringp result) (split-string result "; "))
  16192. ((and (consp result)
  16193. (not (consp (cdr result)))
  16194. (stringp (cdr result))) (cdr result))
  16195. ((and (consp result)
  16196. (stringp (car result))) result)
  16197. (result entry))))
  16198. (defun org-diary-to-ical-string (frombuf)
  16199. "Get iCalendar entries from diary entries in buffer FROMBUF.
  16200. This uses the icalendar.el library."
  16201. (let* ((tmpdir (if (featurep 'xemacs)
  16202. (temp-directory)
  16203. temporary-file-directory))
  16204. (tmpfile (make-temp-name
  16205. (expand-file-name "orgics" tmpdir)))
  16206. buf rtn b e)
  16207. (with-current-buffer frombuf
  16208. (icalendar-export-region (point-min) (point-max) tmpfile)
  16209. (setq buf (find-buffer-visiting tmpfile))
  16210. (set-buffer buf)
  16211. (goto-char (point-min))
  16212. (when (re-search-forward "^BEGIN:VEVENT" nil t)
  16213. (setq b (match-beginning 0)))
  16214. (goto-char (point-max))
  16215. (when (re-search-backward "^END:VEVENT" nil t)
  16216. (setq e (match-end 0)))
  16217. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  16218. (kill-buffer buf)
  16219. (delete-file tmpfile)
  16220. rtn))
  16221. (defun org-closest-date (start current prefer)
  16222. "Return closest date to CURRENT starting from START.
  16223. CURRENT and START are both time stamps.
  16224. When PREFER is `past', return a date that is either CURRENT or
  16225. past. When PREFER is `future', return a date that is either
  16226. CURRENT or future.
  16227. Only time stamps with a repeater are modified. Any other time
  16228. stamp stay unchanged. In any case, return value is an absolute
  16229. day number."
  16230. (if (not (string-match "\\+\\([0-9]+\\)\\([hdwmy]\\)" start))
  16231. ;; No repeater. Do not shift time stamp.
  16232. (time-to-days (apply #'encode-time (org-parse-time-string start)))
  16233. (let ((value (string-to-number (match-string 1 start)))
  16234. (type (match-string 2 start)))
  16235. (if (= 0 value)
  16236. ;; Repeater with a 0-value is considered as void.
  16237. (time-to-days (apply #'encode-time (org-parse-time-string start)))
  16238. (let* ((base (org-date-to-gregorian start))
  16239. (target (org-date-to-gregorian current))
  16240. (sday (calendar-absolute-from-gregorian base))
  16241. (cday (calendar-absolute-from-gregorian target))
  16242. n1 n2)
  16243. ;; If START is already past CURRENT, just return START.
  16244. (if (<= cday sday) sday
  16245. ;; Compute closest date before (N1) and closest date past
  16246. ;; (N2) CURRENT.
  16247. (pcase type
  16248. ("h"
  16249. (let ((missing-hours
  16250. (mod (+ (- (* 24 (- cday sday))
  16251. (nth 2 (org-parse-time-string start)))
  16252. org-extend-today-until)
  16253. value)))
  16254. (setf n1 (if (= missing-hours 0) cday
  16255. (- cday (1+ (/ missing-hours 24)))))
  16256. (setf n2 (+ cday (/ (- value missing-hours) 24)))))
  16257. ((or "d" "w")
  16258. (let ((value (if (equal type "w") (* 7 value) value)))
  16259. (setf n1 (+ sday (* value (/ (- cday sday) value))))
  16260. (setf n2 (+ n1 value))))
  16261. ("m"
  16262. (let* ((add-months
  16263. (lambda (date n)
  16264. ;; Add N months to gregorian DATE, i.e.,
  16265. ;; a list (MONTH DAY YEAR). Return a valid
  16266. ;; gregorian date.
  16267. (let ((m (+ (nth 0 date) n)))
  16268. (list (mod m 12)
  16269. (nth 1 date)
  16270. (+ (/ m 12) (nth 2 date))))))
  16271. (months ; Complete months to TARGET.
  16272. (* (/ (+ (* 12 (- (nth 2 target) (nth 2 base)))
  16273. (- (nth 0 target) (nth 0 base))
  16274. ;; If START's day is greater than
  16275. ;; TARGET's, remove incomplete month.
  16276. (if (> (nth 1 target) (nth 1 base)) 0 -1))
  16277. value)
  16278. value))
  16279. (before (funcall add-months base months)))
  16280. (setf n1 (calendar-absolute-from-gregorian before))
  16281. (setf n2
  16282. (calendar-absolute-from-gregorian
  16283. (funcall add-months before value)))))
  16284. (_
  16285. (let* ((d (nth 1 base))
  16286. (m (nth 0 base))
  16287. (y (nth 2 base))
  16288. (years ; Complete years to TARGET.
  16289. (* (/ (- (nth 2 target)
  16290. y
  16291. ;; If START's month and day are
  16292. ;; greater than TARGET's, remove
  16293. ;; incomplete year.
  16294. (if (or (> (nth 0 target) m)
  16295. (and (= (nth 0 target) m)
  16296. (> (nth 1 target) d)))
  16297. 0
  16298. 1))
  16299. value)
  16300. value))
  16301. (before (list m d (+ y years))))
  16302. (setf n1 (calendar-absolute-from-gregorian before))
  16303. (setf n2 (calendar-absolute-from-gregorian
  16304. (list m d (+ (nth 2 before) value)))))))
  16305. ;; Handle PREFER parameter, if any.
  16306. (cond
  16307. ((eq prefer 'past) (if (= cday n2) n2 n1))
  16308. ((eq prefer 'future) (if (= cday n1) n1 n2))
  16309. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))))))))
  16310. (defun org-date-to-gregorian (date)
  16311. "Turn any specification of DATE into a Gregorian date for the calendar."
  16312. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  16313. ((and (listp date) (= (length date) 3)) date)
  16314. ((stringp date)
  16315. (setq date (org-parse-time-string date))
  16316. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  16317. ((listp date)
  16318. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  16319. (defun org-parse-time-string (s &optional nodefault)
  16320. "Parse the standard Org-mode time string.
  16321. This should be a lot faster than the normal `parse-time-string'.
  16322. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  16323. hour and minute fields will be nil if not given."
  16324. (cond ((string-match org-ts-regexp0 s)
  16325. (list 0
  16326. (when (or (match-beginning 8) (not nodefault))
  16327. (string-to-number (or (match-string 8 s) "0")))
  16328. (when (or (match-beginning 7) (not nodefault))
  16329. (string-to-number (or (match-string 7 s) "0")))
  16330. (string-to-number (match-string 4 s))
  16331. (string-to-number (match-string 3 s))
  16332. (string-to-number (match-string 2 s))
  16333. nil nil nil))
  16334. ((string-match "^<[^>]+>$" s)
  16335. (decode-time (seconds-to-time (org-matcher-time s))))
  16336. (t (error "Not a standard Org-mode time string: %s" s))))
  16337. (defun org-timestamp-up (&optional arg)
  16338. "Increase the date item at the cursor by one.
  16339. If the cursor is on the year, change the year. If it is on the month,
  16340. the day or the time, change that.
  16341. With prefix ARG, change by that many units."
  16342. (interactive "p")
  16343. (org-timestamp-change (prefix-numeric-value arg) nil 'updown))
  16344. (defun org-timestamp-down (&optional arg)
  16345. "Decrease the date item at the cursor by one.
  16346. If the cursor is on the year, change the year. If it is on the month,
  16347. the day or the time, change that.
  16348. With prefix ARG, change by that many units."
  16349. (interactive "p")
  16350. (org-timestamp-change (- (prefix-numeric-value arg)) nil 'updown))
  16351. (defun org-timestamp-up-day (&optional arg)
  16352. "Increase the date in the time stamp by one day.
  16353. With prefix ARG, change that many days."
  16354. (interactive "p")
  16355. (if (and (not (org-at-timestamp-p t))
  16356. (org-at-heading-p))
  16357. (org-todo 'up)
  16358. (org-timestamp-change (prefix-numeric-value arg) 'day 'updown)))
  16359. (defun org-timestamp-down-day (&optional arg)
  16360. "Decrease the date in the time stamp by one day.
  16361. With prefix ARG, change that many days."
  16362. (interactive "p")
  16363. (if (and (not (org-at-timestamp-p t))
  16364. (org-at-heading-p))
  16365. (org-todo 'down)
  16366. (org-timestamp-change (- (prefix-numeric-value arg)) 'day) 'updown))
  16367. (defun org-at-timestamp-p (&optional inactive-ok)
  16368. "Non-nil if point is inside a timestamp.
  16369. When optional argument INACTIVE-OK is non-nil, also consider
  16370. inactive timestamps.
  16371. When this function returns a non-nil value, match data is set
  16372. according to `org-ts-regexp3' or `org-ts-regexp2', depending on
  16373. INACTIVE-OK."
  16374. (interactive)
  16375. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  16376. (pos (point))
  16377. (ans (or (looking-at tsr)
  16378. (save-excursion
  16379. (skip-chars-backward "^[<\n\r\t")
  16380. (when (> (point) (point-min)) (backward-char 1))
  16381. (and (looking-at tsr)
  16382. (> (- (match-end 0) pos) -1))))))
  16383. (and ans
  16384. (boundp 'org-ts-what)
  16385. (setq org-ts-what
  16386. (cond
  16387. ((= pos (match-beginning 0)) 'bracket)
  16388. ;; Point is considered to be "on the bracket" whether
  16389. ;; it's really on it or right after it.
  16390. ((= pos (1- (match-end 0))) 'bracket)
  16391. ((= pos (match-end 0)) 'after)
  16392. ((org-pos-in-match-range pos 2) 'year)
  16393. ((org-pos-in-match-range pos 3) 'month)
  16394. ((org-pos-in-match-range pos 7) 'hour)
  16395. ((org-pos-in-match-range pos 8) 'minute)
  16396. ((or (org-pos-in-match-range pos 4)
  16397. (org-pos-in-match-range pos 5)) 'day)
  16398. ((and (> pos (or (match-end 8) (match-end 5)))
  16399. (< pos (match-end 0)))
  16400. (- pos (or (match-end 8) (match-end 5))))
  16401. (t 'day))))
  16402. ans))
  16403. (defun org-toggle-timestamp-type ()
  16404. "Toggle the type (<active> or [inactive]) of a time stamp."
  16405. (interactive)
  16406. (when (org-at-timestamp-p t)
  16407. (let ((beg (match-beginning 0)) (end (match-end 0))
  16408. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  16409. (save-excursion
  16410. (goto-char beg)
  16411. (while (re-search-forward "[][<>]" end t)
  16412. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  16413. t t)))
  16414. (message "Timestamp is now %sactive"
  16415. (if (equal (char-after beg) ?<) "" "in")))))
  16416. (defun org-at-clock-log-p nil
  16417. "Is the cursor on the clock log line?"
  16418. (save-excursion
  16419. (move-beginning-of-line 1)
  16420. (looking-at org-clock-line-re)))
  16421. (defvar org-clock-history) ; defined in org-clock.el
  16422. (defvar org-clock-adjust-closest nil) ; defined in org-clock.el
  16423. (defun org-timestamp-change (n &optional what updown suppress-tmp-delay)
  16424. "Change the date in the time stamp at point.
  16425. The date will be changed by N times WHAT. WHAT can be `day', `month',
  16426. `year', `minute', `second'. If WHAT is not given, the cursor position
  16427. in the timestamp determines what will be changed.
  16428. When SUPPRESS-TMP-DELAY is non-nil, suppress delays like \"--2d\"."
  16429. (let ((origin (point)) origin-cat
  16430. with-hm inactive
  16431. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  16432. org-ts-what
  16433. extra rem
  16434. ts time time0 fixnext clrgx)
  16435. (unless (org-at-timestamp-p t)
  16436. (user-error "Not at a timestamp"))
  16437. (if (and (not what) (eq org-ts-what 'bracket))
  16438. (org-toggle-timestamp-type)
  16439. ;; Point isn't on brackets. Remember the part of the time-stamp
  16440. ;; the point was in. Indeed, size of time-stamps may change,
  16441. ;; but point must be kept in the same category nonetheless.
  16442. (setq origin-cat org-ts-what)
  16443. (when (and (not what) (not (eq org-ts-what 'day))
  16444. org-display-custom-times
  16445. (get-text-property (point) 'display)
  16446. (not (get-text-property (1- (point)) 'display)))
  16447. (setq org-ts-what 'day))
  16448. (setq org-ts-what (or what org-ts-what)
  16449. inactive (= (char-after (match-beginning 0)) ?\[)
  16450. ts (match-string 0))
  16451. (replace-match "")
  16452. (when (string-match
  16453. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?-?[-+][0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)*\\)[]>]"
  16454. ts)
  16455. (setq extra (match-string 1 ts))
  16456. (when suppress-tmp-delay
  16457. (setq extra (replace-regexp-in-string " --[0-9]+[hdwmy]" "" extra))))
  16458. (when (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  16459. (setq with-hm t))
  16460. (setq time0 (org-parse-time-string ts))
  16461. (when (and updown
  16462. (eq org-ts-what 'minute)
  16463. (not current-prefix-arg))
  16464. ;; This looks like s-up and s-down. Change by one rounding step.
  16465. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  16466. (unless (= 0 (setq rem (% (nth 1 time0) dm)))
  16467. (setcar (cdr time0) (+ (nth 1 time0)
  16468. (if (> n 0) (- rem) (- dm rem))))))
  16469. (setq time
  16470. (encode-time (or (car time0) 0)
  16471. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  16472. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  16473. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  16474. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  16475. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  16476. (nthcdr 6 time0)))
  16477. (when (and (member org-ts-what '(hour minute))
  16478. extra
  16479. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  16480. (setq extra (org-modify-ts-extra
  16481. extra
  16482. (if (eq org-ts-what 'hour) 2 5)
  16483. n dm)))
  16484. (when (integerp org-ts-what)
  16485. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  16486. (when (eq what 'calendar)
  16487. (let ((cal-date (org-get-date-from-calendar)))
  16488. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  16489. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  16490. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  16491. (setcar time0 (or (car time0) 0))
  16492. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  16493. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  16494. (setq time (apply 'encode-time time0))))
  16495. ;; Insert the new time-stamp, and ensure point stays in the same
  16496. ;; category as before (i.e. not after the last position in that
  16497. ;; category).
  16498. (let ((pos (point)))
  16499. ;; Stay before inserted string. `save-excursion' is of no use.
  16500. (setq org-last-changed-timestamp
  16501. (org-insert-time-stamp time with-hm inactive nil nil extra))
  16502. (goto-char pos))
  16503. (save-match-data
  16504. (looking-at org-ts-regexp3)
  16505. (goto-char (cond
  16506. ;; `day' category ends before `hour' if any, or at
  16507. ;; the end of the day name.
  16508. ((eq origin-cat 'day)
  16509. (min (or (match-beginning 7) (1- (match-end 5))) origin))
  16510. ((eq origin-cat 'hour) (min (match-end 7) origin))
  16511. ((eq origin-cat 'minute) (min (1- (match-end 8)) origin))
  16512. ((integerp origin-cat) (min (1- (match-end 0)) origin))
  16513. ;; `year' and `month' have both fixed size: point
  16514. ;; couldn't have moved into another part.
  16515. (t origin))))
  16516. ;; Update clock if on a CLOCK line.
  16517. (org-clock-update-time-maybe)
  16518. ;; Maybe adjust the closest clock in `org-clock-history'
  16519. (when org-clock-adjust-closest
  16520. (if (not (and (org-at-clock-log-p)
  16521. (< 1 (length (delq nil (mapcar 'marker-position
  16522. org-clock-history))))))
  16523. (message "No clock to adjust")
  16524. (cond ((save-excursion ; fix previous clock?
  16525. (re-search-backward org-ts-regexp0 nil t)
  16526. (org-looking-back (concat org-clock-string " \\[")
  16527. (line-beginning-position)))
  16528. (setq fixnext 1 clrgx (concat org-ts-regexp0 "\\] =>.*$")))
  16529. ((save-excursion ; fix next clock?
  16530. (re-search-backward org-ts-regexp0 nil t)
  16531. (looking-at (concat org-ts-regexp0 "\\] =>")))
  16532. (setq fixnext -1 clrgx (concat org-clock-string " \\[" org-ts-regexp0))))
  16533. (save-window-excursion
  16534. ;; Find closest clock to point, adjust the previous/next one in history
  16535. (let* ((p (save-excursion (org-back-to-heading t)))
  16536. (cl (mapcar (lambda(c) (abs (- (marker-position c) p))) org-clock-history))
  16537. (clfixnth
  16538. (+ fixnext (- (length cl) (or (length (member (apply 'min cl) cl)) 100))))
  16539. (clfixpos (unless (> 0 clfixnth) (nth clfixnth org-clock-history))))
  16540. (if (not clfixpos)
  16541. (message "No clock to adjust")
  16542. (save-excursion
  16543. (org-goto-marker-or-bmk clfixpos)
  16544. (org-show-subtree)
  16545. (when (re-search-forward clrgx nil t)
  16546. (goto-char (match-beginning 1))
  16547. (let (org-clock-adjust-closest)
  16548. (org-timestamp-change n org-ts-what updown))
  16549. (message "Clock adjusted in %s for heading: %s"
  16550. (file-name-nondirectory (buffer-file-name))
  16551. (org-get-heading t t)))))))))
  16552. ;; Try to recenter the calendar window, if any.
  16553. (when (and org-calendar-follow-timestamp-change
  16554. (get-buffer-window "*Calendar*" t)
  16555. (memq org-ts-what '(day month year)))
  16556. (org-recenter-calendar (time-to-days time))))))
  16557. (defun org-modify-ts-extra (s pos n dm)
  16558. "Change the different parts of the lead-time and repeat fields in timestamp."
  16559. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  16560. ng h m new rem)
  16561. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  16562. (cond
  16563. ((or (org-pos-in-match-range pos 2)
  16564. (org-pos-in-match-range pos 3))
  16565. (setq m (string-to-number (match-string 3 s))
  16566. h (string-to-number (match-string 2 s)))
  16567. (if (org-pos-in-match-range pos 2)
  16568. (setq h (+ h n))
  16569. (setq n (* dm (org-no-warnings (signum n))))
  16570. (unless (= 0 (setq rem (% m dm)))
  16571. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  16572. (setq m (+ m n)))
  16573. (when (< m 0) (setq m (+ m 60) h (1- h)))
  16574. (when (> m 59) (setq m (- m 60) h (1+ h)))
  16575. (setq h (mod h 24))
  16576. (setq ng 1 new (format "-%02d:%02d" h m)))
  16577. ((org-pos-in-match-range pos 6)
  16578. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  16579. ((org-pos-in-match-range pos 5)
  16580. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  16581. ((org-pos-in-match-range pos 9)
  16582. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  16583. ((org-pos-in-match-range pos 8)
  16584. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  16585. (when ng
  16586. (setq s (concat
  16587. (substring s 0 (match-beginning ng))
  16588. new
  16589. (substring s (match-end ng))))))
  16590. s))
  16591. (defun org-recenter-calendar (date)
  16592. "If the calendar is visible, recenter it to DATE."
  16593. (let ((cwin (get-buffer-window "*Calendar*" t)))
  16594. (when cwin
  16595. (let ((calendar-move-hook nil))
  16596. (with-selected-window cwin
  16597. (calendar-goto-date (if (listp date) date
  16598. (calendar-gregorian-from-absolute date))))))))
  16599. (defun org-goto-calendar (&optional arg)
  16600. "Go to the Emacs calendar at the current date.
  16601. If there is a time stamp in the current line, go to that date.
  16602. A prefix ARG can be used to force the current date."
  16603. (interactive "P")
  16604. (let ((tsr org-ts-regexp) diff
  16605. (calendar-move-hook nil)
  16606. (calendar-view-holidays-initially-flag nil)
  16607. (calendar-view-diary-initially-flag nil))
  16608. (when (or (org-at-timestamp-p)
  16609. (save-excursion
  16610. (beginning-of-line 1)
  16611. (looking-at (concat ".*" tsr))))
  16612. (let ((d1 (time-to-days (current-time)))
  16613. (d2 (time-to-days
  16614. (org-time-string-to-time (match-string 1)))))
  16615. (setq diff (- d2 d1))))
  16616. (calendar)
  16617. (calendar-goto-today)
  16618. (when (and diff (not arg)) (calendar-forward-day diff))))
  16619. (defun org-get-date-from-calendar ()
  16620. "Return a list (month day year) of date at point in calendar."
  16621. (with-current-buffer "*Calendar*"
  16622. (save-match-data
  16623. (calendar-cursor-to-date))))
  16624. (defun org-date-from-calendar ()
  16625. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  16626. If there is already a time stamp at the cursor position, update it."
  16627. (interactive)
  16628. (if (org-at-timestamp-p t)
  16629. (org-timestamp-change 0 'calendar)
  16630. (let ((cal-date (org-get-date-from-calendar)))
  16631. (org-insert-time-stamp
  16632. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  16633. (defcustom org-effort-durations
  16634. `(("min" . 1)
  16635. ("h" . 60)
  16636. ("d" . ,(* 60 8))
  16637. ("w" . ,(* 60 8 5))
  16638. ("m" . ,(* 60 8 5 4))
  16639. ("y" . ,(* 60 8 5 40)))
  16640. "Conversion factor to minutes for an effort modifier.
  16641. Each entry has the form (MODIFIER . MINUTES).
  16642. In an effort string, a number followed by MODIFIER is multiplied
  16643. by the specified number of MINUTES to obtain an effort in
  16644. minutes.
  16645. For example, if the value of this variable is ((\"hours\" . 60)), then an
  16646. effort string \"2hours\" is equivalent to 120 minutes."
  16647. :group 'org-agenda
  16648. :version "25.1"
  16649. :package-version '(Org . "8.3")
  16650. :type '(alist :key-type (string :tag "Modifier")
  16651. :value-type (number :tag "Minutes")))
  16652. (defun org-minutes-to-clocksum-string (m)
  16653. "Format number of minutes as a clocksum string.
  16654. The format is determined by `org-time-clocksum-format',
  16655. `org-time-clocksum-use-fractional' and
  16656. `org-time-clocksum-fractional-format' and
  16657. `org-time-clocksum-use-effort-durations'."
  16658. (let ((clocksum "")
  16659. (m (round m)) ; Don't allow fractions of minutes
  16660. h d w mo y fmt n)
  16661. (setq h (if org-time-clocksum-use-effort-durations
  16662. (cdr (assoc "h" org-effort-durations)) 60)
  16663. d (if org-time-clocksum-use-effort-durations
  16664. (/ (cdr (assoc "d" org-effort-durations)) h) 24)
  16665. w (if org-time-clocksum-use-effort-durations
  16666. (/ (cdr (assoc "w" org-effort-durations)) (* d h)) 7)
  16667. mo (if org-time-clocksum-use-effort-durations
  16668. (/ (cdr (assoc "m" org-effort-durations)) (* d h)) 30)
  16669. y (if org-time-clocksum-use-effort-durations
  16670. (/ (cdr (assoc "y" org-effort-durations)) (* d h)) 365))
  16671. ;; fractional format
  16672. (if org-time-clocksum-use-fractional
  16673. (cond
  16674. ;; single format string
  16675. ((stringp org-time-clocksum-fractional-format)
  16676. (format org-time-clocksum-fractional-format (/ m (float h))))
  16677. ;; choice of fractional formats for different time units
  16678. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :years))
  16679. (> (/ (truncate m) (* y d h)) 0))
  16680. (format fmt (/ m (* y d (float h)))))
  16681. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :months))
  16682. (> (/ (truncate m) (* mo d h)) 0))
  16683. (format fmt (/ m (* mo d (float h)))))
  16684. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :weeks))
  16685. (> (/ (truncate m) (* w d h)) 0))
  16686. (format fmt (/ m (* w d (float h)))))
  16687. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :days))
  16688. (> (/ (truncate m) (* d h)) 0))
  16689. (format fmt (/ m (* d (float h)))))
  16690. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :hours))
  16691. (> (/ (truncate m) h) 0))
  16692. (format fmt (/ m (float h))))
  16693. ((setq fmt (plist-get org-time-clocksum-fractional-format :minutes))
  16694. (format fmt m))
  16695. ;; fall back to smallest time unit with a format
  16696. ((setq fmt (plist-get org-time-clocksum-fractional-format :hours))
  16697. (format fmt (/ m (float h))))
  16698. ((setq fmt (plist-get org-time-clocksum-fractional-format :days))
  16699. (format fmt (/ m (* d (float h)))))
  16700. ((setq fmt (plist-get org-time-clocksum-fractional-format :weeks))
  16701. (format fmt (/ m (* w d (float h)))))
  16702. ((setq fmt (plist-get org-time-clocksum-fractional-format :months))
  16703. (format fmt (/ m (* mo d (float h)))))
  16704. ((setq fmt (plist-get org-time-clocksum-fractional-format :years))
  16705. (format fmt (/ m (* y d (float h))))))
  16706. ;; standard (non-fractional) format, with single format string
  16707. (if (stringp org-time-clocksum-format)
  16708. (format org-time-clocksum-format (setq n (/ m h)) (- m (* h n)))
  16709. ;; separate formats components
  16710. (and (setq fmt (plist-get org-time-clocksum-format :years))
  16711. (or (> (setq n (/ (truncate m) (* y d h))) 0)
  16712. (plist-get org-time-clocksum-format :require-years))
  16713. (setq clocksum (concat clocksum (format fmt n))
  16714. m (- m (* n y d h))))
  16715. (and (setq fmt (plist-get org-time-clocksum-format :months))
  16716. (or (> (setq n (/ (truncate m) (* mo d h))) 0)
  16717. (plist-get org-time-clocksum-format :require-months))
  16718. (setq clocksum (concat clocksum (format fmt n))
  16719. m (- m (* n mo d h))))
  16720. (and (setq fmt (plist-get org-time-clocksum-format :weeks))
  16721. (or (> (setq n (/ (truncate m) (* w d h))) 0)
  16722. (plist-get org-time-clocksum-format :require-weeks))
  16723. (setq clocksum (concat clocksum (format fmt n))
  16724. m (- m (* n w d h))))
  16725. (and (setq fmt (plist-get org-time-clocksum-format :days))
  16726. (or (> (setq n (/ (truncate m) (* d h))) 0)
  16727. (plist-get org-time-clocksum-format :require-days))
  16728. (setq clocksum (concat clocksum (format fmt n))
  16729. m (- m (* n d h))))
  16730. (and (setq fmt (plist-get org-time-clocksum-format :hours))
  16731. (or (> (setq n (/ (truncate m) h)) 0)
  16732. (plist-get org-time-clocksum-format :require-hours))
  16733. (setq clocksum (concat clocksum (format fmt n))
  16734. m (- m (* n h))))
  16735. (and (setq fmt (plist-get org-time-clocksum-format :minutes))
  16736. (or (> m 0) (plist-get org-time-clocksum-format :require-minutes))
  16737. (setq clocksum (concat clocksum (format fmt m))))
  16738. ;; return formatted time duration
  16739. clocksum))))
  16740. (defalias 'org-minutes-to-hh:mm-string 'org-minutes-to-clocksum-string)
  16741. (make-obsolete 'org-minutes-to-hh:mm-string 'org-minutes-to-clocksum-string
  16742. "Org mode version 8.0")
  16743. (defun org-hours-to-clocksum-string (n)
  16744. (org-minutes-to-clocksum-string (* n 60)))
  16745. (defun org-hh:mm-string-to-minutes (s)
  16746. "Convert a string H:MM to a number of minutes.
  16747. If the string is just a number, interpret it as minutes.
  16748. In fact, the first hh:mm or number in the string will be taken,
  16749. there can be extra stuff in the string.
  16750. If no number is found, the return value is 0."
  16751. (cond
  16752. ((integerp s) s)
  16753. ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  16754. (+ (* (string-to-number (match-string 1 s)) 60)
  16755. (string-to-number (match-string 2 s))))
  16756. ((string-match "\\([0-9]+\\)" s)
  16757. (string-to-number (match-string 1 s)))
  16758. (t 0)))
  16759. (defcustom org-image-actual-width t
  16760. "Should we use the actual width of images when inlining them?
  16761. When set to t, always use the image width.
  16762. When set to a number, use imagemagick (when available) to set
  16763. the image's width to this value.
  16764. When set to a number in a list, try to get the width from any
  16765. #+ATTR.* keyword if it matches a width specification like
  16766. #+ATTR_HTML: :width 300px
  16767. and fall back on that number if none is found.
  16768. When set to nil, try to get the width from an #+ATTR.* keyword
  16769. and fall back on the original width if none is found.
  16770. This requires Emacs >= 24.1, build with imagemagick support."
  16771. :group 'org-appearance
  16772. :version "24.4"
  16773. :package-version '(Org . "8.0")
  16774. :type '(choice
  16775. (const :tag "Use the image width" t)
  16776. (integer :tag "Use a number of pixels")
  16777. (list :tag "Use #+ATTR* or a number of pixels" (integer))
  16778. (const :tag "Use #+ATTR* or don't resize" nil)))
  16779. (defcustom org-agenda-inhibit-startup nil
  16780. "Inhibit startup when preparing agenda buffers.
  16781. When this variable is t, the initialization of the Org agenda
  16782. buffers is inhibited: e.g. the visibility state is not set, the
  16783. tables are not re-aligned, etc."
  16784. :type 'boolean
  16785. :version "24.3"
  16786. :group 'org-agenda)
  16787. (define-obsolete-variable-alias
  16788. 'org-agenda-ignore-drawer-properties
  16789. 'org-agenda-ignore-properties "25.1")
  16790. (defcustom org-agenda-ignore-properties nil
  16791. "Avoid updating text properties when building the agenda.
  16792. Properties are used to prepare buffers for effort estimates,
  16793. appointments, statistics and subtree-local categories.
  16794. If you don't use these in the agenda, you can add them to this
  16795. list and agenda building will be a bit faster.
  16796. The value is a list, with zero or more of the symbols `effort', `appt',
  16797. `stats' or `category'."
  16798. :type '(set :greedy t
  16799. (const effort)
  16800. (const appt)
  16801. (const stats)
  16802. (const category))
  16803. :version "25.1"
  16804. :package-version '(Org . "8.3")
  16805. :group 'org-agenda)
  16806. (defun org-duration-string-to-minutes (s &optional output-to-string)
  16807. "Convert a duration string S to minutes.
  16808. A bare number is interpreted as minutes, modifiers can be set by
  16809. customizing `org-effort-durations' (which see).
  16810. Entries containing a colon are interpreted as H:MM by
  16811. `org-hh:mm-string-to-minutes'."
  16812. (let ((result 0)
  16813. (re (concat "\\([0-9.]+\\) *\\("
  16814. (regexp-opt (mapcar 'car org-effort-durations))
  16815. "\\)")))
  16816. (while (string-match re s)
  16817. (incf result (* (cdr (assoc (match-string 2 s) org-effort-durations))
  16818. (string-to-number (match-string 1 s))))
  16819. (setq s (replace-match "" nil t s)))
  16820. (setq result (floor result))
  16821. (incf result (org-hh:mm-string-to-minutes s))
  16822. (if output-to-string (number-to-string result) result)))
  16823. ;;;; Files
  16824. (defun org-save-all-org-buffers ()
  16825. "Save all Org-mode buffers without user confirmation."
  16826. (interactive)
  16827. (message "Saving all Org-mode buffers...")
  16828. (save-some-buffers t (lambda () (derived-mode-p 'org-mode)))
  16829. (when (featurep 'org-id) (org-id-locations-save))
  16830. (message "Saving all Org-mode buffers... done"))
  16831. (defun org-revert-all-org-buffers ()
  16832. "Revert all Org-mode buffers.
  16833. Prompt for confirmation when there are unsaved changes.
  16834. Be sure you know what you are doing before letting this function
  16835. overwrite your changes.
  16836. This function is useful in a setup where one tracks org files
  16837. with a version control system, to revert on one machine after pulling
  16838. changes from another. I believe the procedure must be like this:
  16839. 1. M-x org-save-all-org-buffers
  16840. 2. Pull changes from the other machine, resolve conflicts
  16841. 3. M-x org-revert-all-org-buffers"
  16842. (interactive)
  16843. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  16844. (user-error "Abort"))
  16845. (save-excursion
  16846. (save-window-excursion
  16847. (mapc
  16848. (lambda (b)
  16849. (when (and (with-current-buffer b (derived-mode-p 'org-mode))
  16850. (with-current-buffer b buffer-file-name))
  16851. (org-pop-to-buffer-same-window b)
  16852. (revert-buffer t 'no-confirm)))
  16853. (buffer-list))
  16854. (when (and (featurep 'org-id) org-id-track-globally)
  16855. (org-id-locations-load)))))
  16856. ;;;; Agenda files
  16857. ;;;###autoload
  16858. (defun org-switchb (&optional arg)
  16859. "Switch between Org buffers.
  16860. With one prefix argument, restrict available buffers to files.
  16861. With two prefix arguments, restrict available buffers to agenda files.
  16862. Defaults to `iswitchb' for buffer name completion.
  16863. Set `org-completion-use-ido' to make it use ido instead."
  16864. (interactive "P")
  16865. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  16866. ((equal arg '(16)) (org-buffer-list 'agenda))
  16867. (t (org-buffer-list))))
  16868. (org-completion-use-iswitchb org-completion-use-iswitchb)
  16869. (org-completion-use-ido org-completion-use-ido))
  16870. (unless (or org-completion-use-ido org-completion-use-iswitchb)
  16871. (setq org-completion-use-iswitchb t))
  16872. (org-pop-to-buffer-same-window
  16873. (org-icompleting-read "Org buffer: "
  16874. (mapcar 'list (mapcar 'buffer-name blist))
  16875. nil t))))
  16876. ;;; Define some older names previously used for this functionality
  16877. ;;;###autoload
  16878. (defalias 'org-ido-switchb 'org-switchb)
  16879. ;;;###autoload
  16880. (defalias 'org-iswitchb 'org-switchb)
  16881. (defun org-buffer-list (&optional predicate exclude-tmp)
  16882. "Return a list of Org buffers.
  16883. PREDICATE can be `export', `files' or `agenda'.
  16884. export restrict the list to Export buffers.
  16885. files restrict the list to buffers visiting Org files.
  16886. agenda restrict the list to buffers visiting agenda files.
  16887. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  16888. (let* ((bfn nil)
  16889. (agenda-files (and (eq predicate 'agenda)
  16890. (mapcar 'file-truename (org-agenda-files t))))
  16891. (filter
  16892. (cond
  16893. ((eq predicate 'files)
  16894. (lambda (b) (with-current-buffer b (derived-mode-p 'org-mode))))
  16895. ((eq predicate 'export)
  16896. (lambda (b) (string-match "\\*Org .*Export" (buffer-name b))))
  16897. ((eq predicate 'agenda)
  16898. (lambda (b)
  16899. (with-current-buffer b
  16900. (and (derived-mode-p 'org-mode)
  16901. (setq bfn (buffer-file-name b))
  16902. (member (file-truename bfn) agenda-files)))))
  16903. (t (lambda (b) (with-current-buffer b
  16904. (or (derived-mode-p 'org-mode)
  16905. (string-match "\\*Org .*Export"
  16906. (buffer-name b)))))))))
  16907. (delq nil
  16908. (mapcar
  16909. (lambda(b)
  16910. (if (and (funcall filter b)
  16911. (or (not exclude-tmp)
  16912. (not (string-match "tmp" (buffer-name b)))))
  16913. b
  16914. nil))
  16915. (buffer-list)))))
  16916. (defun org-agenda-files (&optional unrestricted archives)
  16917. "Get the list of agenda files.
  16918. Optional UNRESTRICTED means return the full list even if a restriction
  16919. is currently in place.
  16920. When ARCHIVES is t, include all archive files that are really being
  16921. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  16922. `org-agenda-archives-mode' is t."
  16923. (let ((files
  16924. (cond
  16925. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  16926. ((stringp org-agenda-files) (org-read-agenda-file-list))
  16927. ((listp org-agenda-files) org-agenda-files)
  16928. (t (error "Invalid value of `org-agenda-files'")))))
  16929. (setq files (apply 'append
  16930. (mapcar (lambda (f)
  16931. (if (file-directory-p f)
  16932. (directory-files
  16933. f t org-agenda-file-regexp)
  16934. (list f)))
  16935. files)))
  16936. (when org-agenda-skip-unavailable-files
  16937. (setq files (delq nil
  16938. (mapcar (function
  16939. (lambda (file)
  16940. (and (file-readable-p file) file)))
  16941. files))))
  16942. (when (or (eq archives t)
  16943. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  16944. (setq files (org-add-archive-files files)))
  16945. files))
  16946. (defun org-agenda-file-p (&optional file)
  16947. "Return non-nil, if FILE is an agenda file.
  16948. If FILE is omitted, use the file associated with the current
  16949. buffer."
  16950. (let ((fname (or file (buffer-file-name))))
  16951. (and fname
  16952. (member (file-truename fname)
  16953. (mapcar #'file-truename (org-agenda-files t))))))
  16954. (defun org-edit-agenda-file-list ()
  16955. "Edit the list of agenda files.
  16956. Depending on setup, this either uses customize to edit the variable
  16957. `org-agenda-files', or it visits the file that is holding the list. In the
  16958. latter case, the buffer is set up in a way that saving it automatically kills
  16959. the buffer and restores the previous window configuration."
  16960. (interactive)
  16961. (if (stringp org-agenda-files)
  16962. (let ((cw (current-window-configuration)))
  16963. (find-file org-agenda-files)
  16964. (setq-local org-window-configuration cw)
  16965. (org-add-hook 'after-save-hook
  16966. (lambda ()
  16967. (set-window-configuration
  16968. (prog1 org-window-configuration
  16969. (kill-buffer (current-buffer))))
  16970. (org-install-agenda-files-menu)
  16971. (message "New agenda file list installed"))
  16972. nil 'local)
  16973. (message "%s" (substitute-command-keys
  16974. "Edit list and finish with \\[save-buffer]")))
  16975. (customize-variable 'org-agenda-files)))
  16976. (defun org-store-new-agenda-file-list (list)
  16977. "Set new value for the agenda file list and save it correctly."
  16978. (if (stringp org-agenda-files)
  16979. (let ((fe (org-read-agenda-file-list t)) b u)
  16980. (while (setq b (find-buffer-visiting org-agenda-files))
  16981. (kill-buffer b))
  16982. (with-temp-file org-agenda-files
  16983. (insert
  16984. (mapconcat
  16985. (lambda (f) ;; Keep un-expanded entries.
  16986. (if (setq u (assoc f fe))
  16987. (cdr u)
  16988. f))
  16989. list "\n")
  16990. "\n")))
  16991. (let ((org-mode-hook nil) (org-inhibit-startup t)
  16992. (org-insert-mode-line-in-empty-file nil))
  16993. (setq org-agenda-files list)
  16994. (customize-save-variable 'org-agenda-files org-agenda-files))))
  16995. (defun org-read-agenda-file-list (&optional pair-with-expansion)
  16996. "Read the list of agenda files from a file.
  16997. If PAIR-WITH-EXPANSION is t return pairs with un-expanded
  16998. filenames, used by `org-store-new-agenda-file-list' to write back
  16999. un-expanded file names."
  17000. (when (file-directory-p org-agenda-files)
  17001. (error "`org-agenda-files' cannot be a single directory"))
  17002. (when (stringp org-agenda-files)
  17003. (with-temp-buffer
  17004. (insert-file-contents org-agenda-files)
  17005. (mapcar
  17006. (lambda (f)
  17007. (let ((e (expand-file-name (substitute-in-file-name f)
  17008. org-directory)))
  17009. (if pair-with-expansion
  17010. (cons e f)
  17011. e)))
  17012. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*")))))
  17013. ;;;###autoload
  17014. (defun org-cycle-agenda-files ()
  17015. "Cycle through the files in `org-agenda-files'.
  17016. If the current buffer visits an agenda file, find the next one in the list.
  17017. If the current buffer does not, find the first agenda file."
  17018. (interactive)
  17019. (let* ((fs (or (org-agenda-files t)
  17020. (user-error "No agenda files")))
  17021. (files (copy-sequence fs))
  17022. (tcf (and buffer-file-name (file-truename buffer-file-name)))
  17023. file)
  17024. (when tcf
  17025. (while (and (setq file (pop files))
  17026. (not (equal (file-truename file) tcf)))))
  17027. (find-file (car (or files fs)))
  17028. (when (buffer-base-buffer) (org-pop-to-buffer-same-window (buffer-base-buffer)))))
  17029. (defun org-agenda-file-to-front (&optional to-end)
  17030. "Move/add the current file to the top of the agenda file list.
  17031. If the file is not present in the list, it is added to the front. If it is
  17032. present, it is moved there. With optional argument TO-END, add/move to the
  17033. end of the list."
  17034. (interactive "P")
  17035. (let ((org-agenda-skip-unavailable-files nil)
  17036. (file-alist (mapcar (lambda (x)
  17037. (cons (file-truename x) x))
  17038. (org-agenda-files t)))
  17039. (ctf (file-truename
  17040. (or buffer-file-name
  17041. (user-error "Please save the current buffer to a file"))))
  17042. x had)
  17043. (setq x (assoc ctf file-alist) had x)
  17044. (unless x (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  17045. (if to-end
  17046. (setq file-alist (append (delq x file-alist) (list x)))
  17047. (setq file-alist (cons x (delq x file-alist))))
  17048. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  17049. (org-install-agenda-files-menu)
  17050. (message "File %s to %s of agenda file list"
  17051. (if had "moved" "added") (if to-end "end" "front"))))
  17052. (defun org-remove-file (&optional file)
  17053. "Remove current file from the list of files in variable `org-agenda-files'.
  17054. These are the files which are being checked for agenda entries.
  17055. Optional argument FILE means use this file instead of the current."
  17056. (interactive)
  17057. (let* ((org-agenda-skip-unavailable-files nil)
  17058. (file (or file buffer-file-name
  17059. (user-error "Current buffer does not visit a file")))
  17060. (true-file (file-truename file))
  17061. (afile (abbreviate-file-name file))
  17062. (files (delq nil (mapcar
  17063. (lambda (x)
  17064. (unless (equal true-file
  17065. (file-truename x))
  17066. x))
  17067. (org-agenda-files t)))))
  17068. (if (not (= (length files) (length (org-agenda-files t))))
  17069. (progn
  17070. (org-store-new-agenda-file-list files)
  17071. (org-install-agenda-files-menu)
  17072. (message "Removed from Org Agenda list: %s" afile))
  17073. (message "File was not in list: %s (not removed)" afile))))
  17074. (defun org-file-menu-entry (file)
  17075. (vector file (list 'find-file file) t))
  17076. (defun org-check-agenda-file (file)
  17077. "Make sure FILE exists. If not, ask user what to do."
  17078. (unless (file-exists-p file)
  17079. (message "Non-existent agenda file %s. [R]emove from list or [A]bort?"
  17080. (abbreviate-file-name file))
  17081. (let ((r (downcase (read-char-exclusive))))
  17082. (cond
  17083. ((equal r ?r)
  17084. (org-remove-file file)
  17085. (throw 'nextfile t))
  17086. (t (user-error "Abort"))))))
  17087. (defun org-get-agenda-file-buffer (file)
  17088. "Get an agenda buffer visiting FILE.
  17089. If the buffer needs to be created, add it to the list of buffers
  17090. which might be released later."
  17091. (let ((buf (org-find-base-buffer-visiting file)))
  17092. (if buf
  17093. buf ; just return it
  17094. ;; Make a new buffer and remember it
  17095. (setq buf (find-file-noselect file))
  17096. (when buf (push buf org-agenda-new-buffers))
  17097. buf)))
  17098. (defun org-release-buffers (blist)
  17099. "Release all buffers in list, asking the user for confirmation when needed.
  17100. When a buffer is unmodified, it is just killed. When modified, it is saved
  17101. \(if the user agrees) and then killed."
  17102. (let (file)
  17103. (dolist (buf blist)
  17104. (setq file (buffer-file-name buf))
  17105. (when (and (buffer-modified-p buf)
  17106. file
  17107. (y-or-n-p (format "Save file %s? " file)))
  17108. (with-current-buffer buf (save-buffer)))
  17109. (kill-buffer buf))))
  17110. (defun org-agenda-prepare-buffers (files)
  17111. "Create buffers for all agenda files, protect archived trees and comments."
  17112. (interactive)
  17113. (let ((pa '(:org-archived t))
  17114. (pc '(:org-comment t))
  17115. (pall '(:org-archived t :org-comment t))
  17116. (inhibit-read-only t)
  17117. (org-inhibit-startup org-agenda-inhibit-startup)
  17118. (rea (concat ":" org-archive-tag ":"))
  17119. file re pos)
  17120. (setq org-tag-alist-for-agenda nil
  17121. org-tag-groups-alist-for-agenda nil)
  17122. (save-excursion
  17123. (save-restriction
  17124. (dolist (file files)
  17125. (catch 'nextfile
  17126. (if (bufferp file)
  17127. (set-buffer file)
  17128. (org-check-agenda-file file)
  17129. (set-buffer (org-get-agenda-file-buffer file)))
  17130. (widen)
  17131. (org-set-regexps-and-options 'tags-only)
  17132. (setq pos (point))
  17133. (or (memq 'category org-agenda-ignore-properties)
  17134. (org-refresh-category-properties))
  17135. (or (memq 'stats org-agenda-ignore-properties)
  17136. (org-refresh-stats-properties))
  17137. (or (memq 'effort org-agenda-ignore-properties)
  17138. (org-refresh-effort-properties))
  17139. (or (memq 'appt org-agenda-ignore-properties)
  17140. (org-refresh-properties "APPT_WARNTIME" 'org-appt-warntime))
  17141. (setq org-todo-keywords-for-agenda
  17142. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  17143. (setq org-done-keywords-for-agenda
  17144. (append org-done-keywords-for-agenda org-done-keywords))
  17145. (setq org-todo-keyword-alist-for-agenda
  17146. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  17147. (setq org-tag-alist-for-agenda
  17148. (org-uniquify
  17149. (append org-tag-alist-for-agenda
  17150. org-tag-alist
  17151. org-tag-persistent-alist)))
  17152. (when org-group-tags
  17153. (setq org-tag-groups-alist-for-agenda
  17154. (org-uniquify-alist
  17155. (append org-tag-groups-alist-for-agenda org-tag-groups-alist))))
  17156. (org-with-silent-modifications
  17157. (save-excursion
  17158. (remove-text-properties (point-min) (point-max) pall)
  17159. (when org-agenda-skip-archived-trees
  17160. (goto-char (point-min))
  17161. (while (re-search-forward rea nil t)
  17162. (when (org-at-heading-p t)
  17163. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  17164. (goto-char (point-min))
  17165. (setq re (format "^\\*+ .*\\<%s\\>" org-comment-string))
  17166. (while (re-search-forward re nil t)
  17167. (when (save-match-data (org-in-commented-heading-p t))
  17168. (add-text-properties
  17169. (match-beginning 0) (org-end-of-subtree t) pc)))))
  17170. (goto-char pos)))))
  17171. (setq org-todo-keywords-for-agenda
  17172. (org-uniquify org-todo-keywords-for-agenda))
  17173. (setq org-todo-keyword-alist-for-agenda
  17174. (org-uniquify org-todo-keyword-alist-for-agenda))))
  17175. ;;;; CDLaTeX minor mode
  17176. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  17177. "Keymap for the minor `org-cdlatex-mode'.")
  17178. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  17179. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  17180. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  17181. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  17182. (org-defkey org-cdlatex-mode-map "\C-c{" 'org-cdlatex-environment-indent)
  17183. (defvar org-cdlatex-texmathp-advice-is-done nil
  17184. "Flag remembering if we have applied the advice to texmathp already.")
  17185. (define-minor-mode org-cdlatex-mode
  17186. "Toggle the minor `org-cdlatex-mode'.
  17187. This mode supports entering LaTeX environment and math in LaTeX fragments
  17188. in Org-mode.
  17189. \\{org-cdlatex-mode-map}"
  17190. nil " OCDL" nil
  17191. (when org-cdlatex-mode
  17192. (require 'cdlatex)
  17193. (run-hooks 'cdlatex-mode-hook)
  17194. (cdlatex-compute-tables))
  17195. (unless org-cdlatex-texmathp-advice-is-done
  17196. (setq org-cdlatex-texmathp-advice-is-done t)
  17197. (defadvice texmathp (around org-math-always-on activate)
  17198. "Always return t in org-mode buffers.
  17199. This is because we want to insert math symbols without dollars even outside
  17200. the LaTeX math segments. If Orgmode thinks that point is actually inside
  17201. an embedded LaTeX fragment, let texmathp do its job.
  17202. \\[org-cdlatex-mode-map]"
  17203. (interactive)
  17204. (let (p)
  17205. (cond
  17206. ((not (derived-mode-p 'org-mode)) ad-do-it)
  17207. ((eq this-command 'cdlatex-math-symbol)
  17208. (setq ad-return-value t
  17209. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  17210. (t
  17211. (let ((p (org-inside-LaTeX-fragment-p)))
  17212. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  17213. (setq ad-return-value t
  17214. texmathp-why '("Org-mode embedded math" . 0))
  17215. (when p ad-do-it)))))))))
  17216. (defun turn-on-org-cdlatex ()
  17217. "Unconditionally turn on `org-cdlatex-mode'."
  17218. (org-cdlatex-mode 1))
  17219. (defun org-try-cdlatex-tab ()
  17220. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  17221. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  17222. - inside a LaTeX fragment, or
  17223. - after the first word in a line, where an abbreviation expansion could
  17224. insert a LaTeX environment."
  17225. (when org-cdlatex-mode
  17226. (cond
  17227. ;; Before any word on the line: No expansion possible.
  17228. ((save-excursion (skip-chars-backward " \t") (bolp)) nil)
  17229. ;; Just after first word on the line: Expand it. Make sure it
  17230. ;; cannot happen on headlines, though.
  17231. ((save-excursion
  17232. (skip-chars-backward "a-zA-Z0-9*")
  17233. (skip-chars-backward " \t")
  17234. (and (bolp) (not (org-at-heading-p))))
  17235. (cdlatex-tab) t)
  17236. ((org-inside-LaTeX-fragment-p) (cdlatex-tab) t))))
  17237. (defun org-cdlatex-underscore-caret (&optional arg)
  17238. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  17239. Revert to the normal definition outside of these fragments."
  17240. (interactive "P")
  17241. (if (org-inside-LaTeX-fragment-p)
  17242. (call-interactively 'cdlatex-sub-superscript)
  17243. (let (org-cdlatex-mode)
  17244. (call-interactively (key-binding (vector last-input-event))))))
  17245. (defun org-cdlatex-math-modify (&optional arg)
  17246. "Execute `cdlatex-math-modify' in LaTeX fragments.
  17247. Revert to the normal definition outside of these fragments."
  17248. (interactive "P")
  17249. (if (org-inside-LaTeX-fragment-p)
  17250. (call-interactively 'cdlatex-math-modify)
  17251. (let (org-cdlatex-mode)
  17252. (call-interactively (key-binding (vector last-input-event))))))
  17253. (defun org-cdlatex-environment-indent (&optional environment item)
  17254. "Execute `cdlatex-environment' and indent the inserted environment.
  17255. ENVIRONMENT and ITEM are passed to `cdlatex-environment'.
  17256. The inserted environment is indented to current indentation
  17257. unless point is at the beginning of the line, in which the
  17258. environment remains unintended."
  17259. (interactive)
  17260. ;; cdlatex-environment always return nil. Therefore, capture output
  17261. ;; first and determine if an environment was selected.
  17262. (let* ((beg (point-marker))
  17263. (end (copy-marker (point) t))
  17264. (inserted (progn
  17265. (ignore-errors (cdlatex-environment environment item))
  17266. (< beg end)))
  17267. ;; Figure out how many lines to move forward after the
  17268. ;; environment has been inserted.
  17269. (lines (when inserted
  17270. (save-excursion
  17271. (- (loop while (< beg (point))
  17272. with x = 0
  17273. do (forward-line -1)
  17274. (incf x)
  17275. finally return x)
  17276. (if (progn (goto-char beg)
  17277. (and (progn (skip-chars-forward " \t") (eolp))
  17278. (progn (skip-chars-backward " \t") (bolp))))
  17279. 1 0)))))
  17280. (env (org-trim (delete-and-extract-region beg end))))
  17281. (when inserted
  17282. ;; Get indentation of next line unless at column 0.
  17283. (let ((ind (if (bolp) 0
  17284. (save-excursion
  17285. (org-return-indent)
  17286. (prog1 (org-get-indentation)
  17287. (when (progn (skip-chars-forward " \t") (eolp))
  17288. (delete-region beg (point)))))))
  17289. (bol (progn (skip-chars-backward " \t") (bolp))))
  17290. ;; Insert a newline before environment unless at column zero
  17291. ;; to "escape" the current line. Insert a newline if
  17292. ;; something is one the same line as \end{ENVIRONMENT}.
  17293. (insert
  17294. (concat (unless bol "\n") env
  17295. (when (and (skip-chars-forward " \t") (not (eolp))) "\n")))
  17296. (unless (zerop ind)
  17297. (save-excursion
  17298. (goto-char beg)
  17299. (while (< (point) end)
  17300. (unless (eolp) (org-indent-line-to ind))
  17301. (forward-line))))
  17302. (goto-char beg)
  17303. (forward-line lines)
  17304. (org-indent-line-to ind)))
  17305. (set-marker beg nil)
  17306. (set-marker end nil)))
  17307. ;;;; LaTeX fragments
  17308. (defun org-inside-LaTeX-fragment-p ()
  17309. "Test if point is inside a LaTeX fragment.
  17310. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  17311. sequence appearing also before point.
  17312. Even though the matchers for math are configurable, this function assumes
  17313. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  17314. delimiters are skipped when they have been removed by customization.
  17315. The return value is nil, or a cons cell with the delimiter and the
  17316. position of this delimiter.
  17317. This function does a reasonably good job, but can locally be fooled by
  17318. for example currency specifications. For example it will assume being in
  17319. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  17320. fragments that are properly closed, but during editing, we have to live
  17321. with the uncertainty caused by missing closing delimiters. This function
  17322. looks only before point, not after."
  17323. (catch 'exit
  17324. (let ((pos (point))
  17325. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  17326. (lim (progn
  17327. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  17328. (point)))
  17329. dd-on str (start 0) m re)
  17330. (goto-char pos)
  17331. (when dodollar
  17332. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  17333. re (nth 1 (assoc "$" org-latex-regexps)))
  17334. (while (string-match re str start)
  17335. (cond
  17336. ((= (match-end 0) (length str))
  17337. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  17338. ((= (match-end 0) (- (length str) 5))
  17339. (throw 'exit nil))
  17340. (t (setq start (match-end 0))))))
  17341. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  17342. (goto-char pos)
  17343. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  17344. (and (match-beginning 2) (throw 'exit nil))
  17345. ;; count $$
  17346. (while (re-search-backward "\\$\\$" lim t)
  17347. (setq dd-on (not dd-on)))
  17348. (goto-char pos)
  17349. (when dd-on (cons "$$" m))))))
  17350. (defun org-inside-latex-macro-p ()
  17351. "Is point inside a LaTeX macro or its arguments?"
  17352. (save-match-data
  17353. (org-in-regexp
  17354. "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")))
  17355. (defvar-local org-latex-fragment-image-overlays nil
  17356. "List of overlays carrying the images of latex fragments.")
  17357. (defun org-remove-latex-fragment-image-overlays (&optional beg end)
  17358. "Remove all overlays with LaTeX fragment images in current buffer.
  17359. When optional arguments BEG and END are non-nil, remove all
  17360. overlays between them instead. Return t when some overlays were
  17361. removed, nil otherwise."
  17362. (let (removedp)
  17363. (setq org-latex-fragment-image-overlays
  17364. (let ((beg (or beg (point-min)))
  17365. (end (or end (point-max))))
  17366. (cl-remove-if
  17367. (lambda (o)
  17368. (cond ((not (overlay-buffer o)) (delete-overlay o) t)
  17369. ((and (>= (overlay-start o) beg)
  17370. (<= (overlay-end o) end))
  17371. (delete-overlay o)
  17372. (unless removedp (setq removedp t)))
  17373. (t nil)))
  17374. org-latex-fragment-image-overlays)))
  17375. removedp))
  17376. (define-obsolete-function-alias
  17377. 'org-preview-latex-fragment 'org-toggle-latex-fragment "24.4")
  17378. (defun org-toggle-latex-fragment (&optional arg)
  17379. "Preview the LaTeX fragment at point, or all locally or globally.
  17380. If the cursor is on a LaTeX fragment, create the image and overlay
  17381. it over the source code, if there is none. Remove it otherwise.
  17382. If there is no fragment at point, display all fragments in the
  17383. current section.
  17384. With prefix ARG, preview or clear image for all fragments in the
  17385. current subtree or in the whole buffer when used before the first
  17386. headline. With a double prefix ARG \\[universal-argument] \
  17387. \\[universal-argument] preview or clear images
  17388. for all fragments in the buffer."
  17389. (interactive "P")
  17390. (unless (buffer-file-name (buffer-base-buffer))
  17391. (user-error "Can't preview LaTeX fragment in a non-file buffer"))
  17392. (when (display-graphic-p)
  17393. (catch 'exit
  17394. (save-excursion
  17395. (let ((window-start (window-start)) msg)
  17396. (save-restriction
  17397. (cond
  17398. ((or (equal arg '(16))
  17399. (and (equal arg '(4))
  17400. (org-with-limited-levels (org-before-first-heading-p))))
  17401. (if (org-remove-latex-fragment-image-overlays)
  17402. (progn (message "LaTeX fragments images removed from buffer")
  17403. (throw 'exit nil))
  17404. (setq msg "Creating images for buffer...")))
  17405. ((equal arg '(4))
  17406. (org-with-limited-levels (org-back-to-heading t))
  17407. (let ((beg (point))
  17408. (end (progn (org-end-of-subtree t) (point))))
  17409. (if (org-remove-latex-fragment-image-overlays beg end)
  17410. (progn
  17411. (message "LaTeX fragment images removed from subtree")
  17412. (throw 'exit nil))
  17413. (setq msg "Creating images for subtree...")
  17414. (narrow-to-region beg end))))
  17415. ((let ((datum (org-element-context)))
  17416. (when (memq (org-element-type datum)
  17417. '(latex-environment latex-fragment))
  17418. (let* ((beg (org-element-property :begin datum))
  17419. (end (org-element-property :end datum)))
  17420. (if (org-remove-latex-fragment-image-overlays beg end)
  17421. (progn (message "LaTeX fragment image removed")
  17422. (throw 'exit nil))
  17423. (narrow-to-region beg end)
  17424. (setq msg "Creating image..."))))))
  17425. (t
  17426. (org-with-limited-levels
  17427. (let ((beg (if (org-at-heading-p) (line-beginning-position)
  17428. (outline-previous-heading)
  17429. (point)))
  17430. (end (progn (outline-next-heading) (point))))
  17431. (if (org-remove-latex-fragment-image-overlays beg end)
  17432. (progn
  17433. (message "LaTeX fragment images removed from section")
  17434. (throw 'exit nil))
  17435. (setq msg "Creating images for section...")
  17436. (narrow-to-region beg end))))))
  17437. (let ((file (buffer-file-name (buffer-base-buffer))))
  17438. (org-format-latex
  17439. (concat org-latex-preview-ltxpng-directory
  17440. (file-name-sans-extension (file-name-nondirectory file)))
  17441. ;; Emacs cannot overlay images from remote hosts.
  17442. ;; Create it in `temporary-file-directory' instead.
  17443. (if (file-remote-p file) temporary-file-directory
  17444. default-directory)
  17445. 'overlays msg 'forbuffer
  17446. org-latex-create-formula-image-program)))
  17447. ;; Work around a bug that doesn't restore window's start
  17448. ;; when widening back the buffer.
  17449. (set-window-start nil window-start)
  17450. (message (concat msg "done")))))))
  17451. (defun org-format-latex
  17452. (prefix &optional dir overlays msg forbuffer processing-type)
  17453. "Replace LaTeX fragments with links to an image, and produce images.
  17454. When optional argument OVERLAYS is non-nil, display the image on
  17455. top of the fragment instead of replacing it.
  17456. PROCESSING-TYPE is the conversion method to use, as a symbol.
  17457. Some of the options can be changed using the variable
  17458. `org-format-latex-options', which see."
  17459. (when (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  17460. (unless (eq processing-type 'verbatim)
  17461. (let* ((math-regexp "\\$\\|\\\\[([]\\|^[ \t]*\\\\begin{[A-Za-z0-9*]+}")
  17462. (cnt 0)
  17463. checkdir-flag)
  17464. (goto-char (point-min))
  17465. ;; Optimize overlay creation: (info "(elisp) Managing Overlays").
  17466. (when (and overlays (memq processing-type '(dvipng imagemagick)))
  17467. (overlay-recenter (point-max)))
  17468. (while (re-search-forward math-regexp nil t)
  17469. (unless (and overlays
  17470. (eq (get-char-property (point) 'org-overlay-type)
  17471. 'org-latex-overlay))
  17472. (let* ((context (org-element-context))
  17473. (type (org-element-type context)))
  17474. (when (memq type '(latex-environment latex-fragment))
  17475. (let ((block-type (eq type 'latex-environment))
  17476. (value (org-element-property :value context))
  17477. (beg (org-element-property :begin context))
  17478. (end (save-excursion
  17479. (goto-char (org-element-property :end context))
  17480. (skip-chars-backward " \r\t\n")
  17481. (point))))
  17482. (case processing-type
  17483. (mathjax
  17484. ;; Prepare for MathJax processing.
  17485. (if (eq (char-after beg) ?$)
  17486. (save-excursion
  17487. (delete-region beg end)
  17488. (insert "\\(" (substring value 1 -1) "\\)"))
  17489. (goto-char end)))
  17490. ((dvipng imagemagick)
  17491. ;; Process to an image.
  17492. (incf cnt)
  17493. (goto-char beg)
  17494. (let* ((face (face-at-point))
  17495. ;; Get the colors from the face at point.
  17496. (fg
  17497. (let ((color (plist-get org-format-latex-options
  17498. :foreground)))
  17499. (if (and forbuffer (eq color 'auto))
  17500. (face-attribute face :foreground nil 'default)
  17501. color)))
  17502. (bg
  17503. (let ((color (plist-get org-format-latex-options
  17504. :background)))
  17505. (if (and forbuffer (eq color 'auto))
  17506. (face-attribute face :background nil 'default)
  17507. color)))
  17508. (hash (sha1 (prin1-to-string
  17509. (list org-format-latex-header
  17510. org-latex-default-packages-alist
  17511. org-latex-packages-alist
  17512. org-format-latex-options
  17513. forbuffer value fg bg))))
  17514. (absprefix (expand-file-name prefix dir))
  17515. (linkfile (format "%s_%s.png" prefix hash))
  17516. (movefile (format "%s_%s.png" absprefix hash))
  17517. (sep (and block-type "\n\n"))
  17518. (link (concat sep "[[file:" linkfile "]]" sep))
  17519. (options
  17520. (org-combine-plists
  17521. org-format-latex-options
  17522. `(:foreground ,fg :background ,bg))))
  17523. (when msg (message msg cnt))
  17524. (unless checkdir-flag ; Ensure the directory exists.
  17525. (setq checkdir-flag t)
  17526. (let ((todir (file-name-directory absprefix)))
  17527. (unless (file-directory-p todir)
  17528. (make-directory todir t))))
  17529. (unless (file-exists-p movefile)
  17530. (org-create-formula-image
  17531. value movefile options forbuffer processing-type))
  17532. (if overlays
  17533. (progn
  17534. (dolist (o (overlays-in beg end))
  17535. (when (eq (overlay-get o 'org-overlay-type)
  17536. 'org-latex-overlay)
  17537. (delete-overlay o)))
  17538. (let ((ov (make-overlay beg end)))
  17539. (overlay-put ov
  17540. 'org-overlay-type
  17541. 'org-latex-overlay)
  17542. (overlay-put ov 'evaporate t)
  17543. (if (featurep 'xemacs)
  17544. (progn
  17545. (overlay-put ov 'invisible t)
  17546. (overlay-put
  17547. ov 'end-glyph
  17548. (make-glyph
  17549. (vector 'png :file movefile))))
  17550. (overlay-put
  17551. ov 'display
  17552. (list 'image
  17553. :type 'png
  17554. :file movefile
  17555. :ascent 'center)))
  17556. (push ov org-latex-fragment-image-overlays))
  17557. (goto-char end))
  17558. (delete-region beg end)
  17559. (insert
  17560. (org-add-props link
  17561. (list 'org-latex-src
  17562. (replace-regexp-in-string "\"" "" value)
  17563. 'org-latex-src-embed-type
  17564. (if block-type 'paragraph 'character)))))))
  17565. (mathml
  17566. ;; Process to MathML.
  17567. (unless (org-format-latex-mathml-available-p)
  17568. (user-error "LaTeX to MathML converter not configured"))
  17569. (incf cnt)
  17570. (when msg (message msg cnt))
  17571. (goto-char beg)
  17572. (delete-region beg end)
  17573. (insert (org-format-latex-as-mathml
  17574. value block-type prefix dir)))
  17575. (otherwise
  17576. (error "Unknown conversion type %s for LaTeX fragments"
  17577. processing-type)))))))))))
  17578. (defun org-create-math-formula (latex-frag &optional mathml-file)
  17579. "Convert LATEX-FRAG to MathML and store it in MATHML-FILE.
  17580. Use `org-latex-to-mathml-convert-command'. If the conversion is
  17581. sucessful, return the portion between \"<math...> </math>\"
  17582. elements otherwise return nil. When MATHML-FILE is specified,
  17583. write the results in to that file. When invoked as an
  17584. interactive command, prompt for LATEX-FRAG, with initial value
  17585. set to the current active region and echo the results for user
  17586. inspection."
  17587. (interactive (list (let ((frag (when (org-region-active-p)
  17588. (buffer-substring-no-properties
  17589. (region-beginning) (region-end)))))
  17590. (read-string "LaTeX Fragment: " frag nil frag))))
  17591. (unless latex-frag (user-error "Invalid LaTeX fragment"))
  17592. (let* ((tmp-in-file (file-relative-name
  17593. (make-temp-name (expand-file-name "ltxmathml-in"))))
  17594. (ignore (write-region latex-frag nil tmp-in-file))
  17595. (tmp-out-file (file-relative-name
  17596. (make-temp-name (expand-file-name "ltxmathml-out"))))
  17597. (cmd (format-spec
  17598. org-latex-to-mathml-convert-command
  17599. `((?j . ,(and org-latex-to-mathml-jar-file
  17600. (shell-quote-argument
  17601. (expand-file-name
  17602. org-latex-to-mathml-jar-file))))
  17603. (?I . ,(shell-quote-argument tmp-in-file))
  17604. (?i . ,latex-frag)
  17605. (?o . ,(shell-quote-argument tmp-out-file)))))
  17606. mathml shell-command-output)
  17607. (when (org-called-interactively-p 'any)
  17608. (unless (org-format-latex-mathml-available-p)
  17609. (user-error "LaTeX to MathML converter not configured")))
  17610. (message "Running %s" cmd)
  17611. (setq shell-command-output (shell-command-to-string cmd))
  17612. (setq mathml
  17613. (when (file-readable-p tmp-out-file)
  17614. (with-current-buffer (find-file-noselect tmp-out-file t)
  17615. (goto-char (point-min))
  17616. (when (re-search-forward
  17617. (concat
  17618. (regexp-quote
  17619. "<math xmlns=\"http://www.w3.org/1998/Math/MathML\"")
  17620. "[^>]*?>"
  17621. "\\(.\\|\n\\)*"
  17622. "</math>")
  17623. nil t)
  17624. (prog1 (match-string 0) (kill-buffer))))))
  17625. (cond
  17626. (mathml
  17627. (setq mathml
  17628. (concat "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" mathml))
  17629. (when mathml-file
  17630. (write-region mathml nil mathml-file))
  17631. (when (org-called-interactively-p 'any)
  17632. (message mathml)))
  17633. ((message "LaTeX to MathML conversion failed")
  17634. (message shell-command-output)))
  17635. (delete-file tmp-in-file)
  17636. (when (file-exists-p tmp-out-file)
  17637. (delete-file tmp-out-file))
  17638. mathml))
  17639. (defun org-format-latex-as-mathml (latex-frag latex-frag-type
  17640. prefix &optional dir)
  17641. "Use `org-create-math-formula' but check local cache first."
  17642. (let* ((absprefix (expand-file-name prefix dir))
  17643. (print-length nil) (print-level nil)
  17644. (formula-id (concat
  17645. "formula-"
  17646. (sha1
  17647. (prin1-to-string
  17648. (list latex-frag
  17649. org-latex-to-mathml-convert-command)))))
  17650. (formula-cache (format "%s-%s.mathml" absprefix formula-id))
  17651. (formula-cache-dir (file-name-directory formula-cache)))
  17652. (unless (file-directory-p formula-cache-dir)
  17653. (make-directory formula-cache-dir t))
  17654. (unless (file-exists-p formula-cache)
  17655. (org-create-math-formula latex-frag formula-cache))
  17656. (if (file-exists-p formula-cache)
  17657. ;; Successful conversion. Return the link to MathML file.
  17658. (org-add-props
  17659. (format "[[file:%s]]" (file-relative-name formula-cache dir))
  17660. (list 'org-latex-src (replace-regexp-in-string "\"" "" latex-frag)
  17661. 'org-latex-src-embed-type (if latex-frag-type
  17662. 'paragraph 'character)))
  17663. ;; Failed conversion. Return the LaTeX fragment verbatim
  17664. latex-frag)))
  17665. (defun org-create-formula-image (string tofile options buffer &optional type)
  17666. "Create an image from LaTeX source using dvipng or convert.
  17667. This function calls either `org-create-formula-image-with-dvipng'
  17668. or `org-create-formula-image-with-imagemagick' depending on the
  17669. value of `org-latex-create-formula-image-program' or on the value
  17670. of the optional TYPE variable.
  17671. Note: ultimately these two function should be combined as they
  17672. share a good deal of logic."
  17673. (org-check-external-command
  17674. "latex" "needed to convert LaTeX fragments to images")
  17675. (funcall
  17676. (case (or type org-latex-create-formula-image-program)
  17677. (dvipng
  17678. (org-check-external-command
  17679. "dvipng" "needed to convert LaTeX fragments to images")
  17680. #'org-create-formula-image-with-dvipng)
  17681. (imagemagick
  17682. (org-check-external-command
  17683. "convert" "you need to install imagemagick")
  17684. #'org-create-formula-image-with-imagemagick)
  17685. (t (error
  17686. "Invalid value of `org-latex-create-formula-image-program'")))
  17687. string tofile options buffer))
  17688. (declare-function org-export-get-backend "ox" (name))
  17689. (declare-function org-export--get-global-options "ox" (&optional backend))
  17690. (declare-function org-export--get-inbuffer-options "ox" (&optional backend))
  17691. (declare-function org-latex-guess-inputenc "ox-latex" (header))
  17692. (declare-function org-latex-guess-babel-language "ox-latex" (header info))
  17693. (defun org-create-formula--latex-header ()
  17694. "Return LaTeX header appropriate for previewing a LaTeX snippet."
  17695. (let ((info (org-combine-plists (org-export--get-global-options
  17696. (org-export-get-backend 'latex))
  17697. (org-export--get-inbuffer-options
  17698. (org-export-get-backend 'latex)))))
  17699. (org-latex-guess-babel-language
  17700. (org-latex-guess-inputenc
  17701. (org-splice-latex-header
  17702. org-format-latex-header
  17703. org-latex-default-packages-alist
  17704. org-latex-packages-alist t
  17705. (plist-get info :latex-header)))
  17706. info)))
  17707. (defun org--get-display-dpi ()
  17708. "Get the DPI of the display.
  17709. Assumes that the display has the same pixel width in the
  17710. horizontal and vertical directions."
  17711. (if (display-graphic-p)
  17712. (round (/ (display-pixel-height)
  17713. (/ (display-mm-height) 25.4)))
  17714. (error "Attempt to calculate the dpi of a non-graphic display")))
  17715. ;; This function borrows from Ganesh Swami's latex2png.el
  17716. (defun org-create-formula-image-with-dvipng (string tofile options buffer)
  17717. "This calls dvipng."
  17718. (require 'ox-latex)
  17719. (let* ((tmpdir (if (featurep 'xemacs)
  17720. (temp-directory)
  17721. temporary-file-directory))
  17722. (texfilebase (make-temp-name
  17723. (expand-file-name "orgtex" tmpdir)))
  17724. (texfile (concat texfilebase ".tex"))
  17725. (dvifile (concat texfilebase ".dvi"))
  17726. (pngfile (concat texfilebase ".png"))
  17727. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  17728. ;; This assumes that the display has the same pixel width in
  17729. ;; the horizontal and vertical directions
  17730. (dpi (number-to-string (* scale (if buffer (org--get-display-dpi) 120))))
  17731. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  17732. "Black"))
  17733. (bg (or (plist-get options (if buffer :background :html-background))
  17734. "Transparent")))
  17735. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground))
  17736. (unless (string= fg "Transparent") (setq fg (org-dvipng-color-format fg))))
  17737. (if (eq bg 'default) (setq bg (org-dvipng-color :background))
  17738. (unless (string= bg "Transparent") (setq bg (org-dvipng-color-format bg))))
  17739. (let ((latex-header (org-create-formula--latex-header)))
  17740. (with-temp-file texfile
  17741. (insert latex-header)
  17742. (insert "\n\\begin{document}\n" string "\n\\end{document}\n")))
  17743. (let ((dir default-directory))
  17744. (ignore-errors
  17745. (cd tmpdir)
  17746. (call-process "latex" nil nil nil texfile))
  17747. (cd dir))
  17748. (if (not (file-exists-p dvifile))
  17749. (progn (message "Failed to create dvi file from %s" texfile) nil)
  17750. (ignore-errors
  17751. (if (featurep 'xemacs)
  17752. (call-process "dvipng" nil nil nil
  17753. "-fg" fg "-bg" bg
  17754. "-T" "tight"
  17755. "-o" pngfile
  17756. dvifile)
  17757. (call-process "dvipng" nil nil nil
  17758. "-fg" fg "-bg" bg
  17759. "-D" dpi
  17760. ;;"-x" scale "-y" scale
  17761. "-T" "tight"
  17762. "-o" pngfile
  17763. dvifile)))
  17764. (if (not (file-exists-p pngfile))
  17765. (if org-format-latex-signal-error
  17766. (error "Failed to create png file from %s" texfile)
  17767. (message "Failed to create png file from %s" texfile)
  17768. nil)
  17769. ;; Use the requested file name and clean up
  17770. (copy-file pngfile tofile 'replace)
  17771. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png" ".out") do
  17772. (when (file-exists-p (concat texfilebase e))
  17773. (delete-file (concat texfilebase e))))
  17774. pngfile))))
  17775. (declare-function org-latex-compile "ox-latex" (texfile &optional snippet))
  17776. (defun org-create-formula-image-with-imagemagick (string tofile options buffer)
  17777. "This calls convert, which is included into imagemagick."
  17778. (require 'ox-latex)
  17779. (let* ((tmpdir (if (featurep 'xemacs)
  17780. (temp-directory)
  17781. temporary-file-directory))
  17782. (texfilebase (make-temp-name
  17783. (expand-file-name "orgtex" tmpdir)))
  17784. (texfile (concat texfilebase ".tex"))
  17785. (pdffile (concat texfilebase ".pdf"))
  17786. (pngfile (concat texfilebase ".png"))
  17787. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  17788. (dpi (number-to-string (* scale (if buffer (org--get-display-dpi) 120))))
  17789. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  17790. "black"))
  17791. (bg (or (plist-get options (if buffer :background :html-background))
  17792. "white")))
  17793. (if (eq fg 'default) (setq fg (org-latex-color :foreground))
  17794. (setq fg (org-latex-color-format fg)))
  17795. (if (eq bg 'default) (setq bg (org-latex-color :background))
  17796. (setq bg (org-latex-color-format
  17797. (if (string= bg "Transparent") "white" bg))))
  17798. (let ((latex-header (org-create-formula--latex-header)))
  17799. (with-temp-file texfile
  17800. (insert latex-header)
  17801. (insert "\n\\begin{document}\n"
  17802. "\\definecolor{fg}{rgb}{" fg "}\n"
  17803. "\\definecolor{bg}{rgb}{" bg "}\n"
  17804. "\n\\pagecolor{bg}\n"
  17805. "\n{\\color{fg}\n"
  17806. string
  17807. "\n}\n"
  17808. "\n\\end{document}\n")))
  17809. (org-latex-compile texfile t)
  17810. (if (not (file-exists-p pdffile))
  17811. (progn (message "Failed to create pdf file from %s" texfile) nil)
  17812. (ignore-errors
  17813. (if (featurep 'xemacs)
  17814. (call-process "convert" nil nil nil
  17815. "-density" "96"
  17816. "-trim"
  17817. "-antialias"
  17818. pdffile
  17819. "-quality" "100"
  17820. ;; "-sharpen" "0x1.0"
  17821. pngfile)
  17822. (call-process "convert" nil nil nil
  17823. "-density" dpi
  17824. "-trim"
  17825. "-antialias"
  17826. pdffile
  17827. "-quality" "100"
  17828. ;; "-sharpen" "0x1.0"
  17829. pngfile)))
  17830. (if (not (file-exists-p pngfile))
  17831. (if org-format-latex-signal-error
  17832. (error "Failed to create png file from %s" texfile)
  17833. (message "Failed to create png file from %s" texfile)
  17834. nil)
  17835. ;; Use the requested file name and clean up
  17836. (copy-file pngfile tofile 'replace)
  17837. (loop for e in '(".pdf" ".tex" ".aux" ".log" ".png") do
  17838. (when (file-exists-p (concat texfilebase e))
  17839. (delete-file (concat texfilebase e))))
  17840. pngfile))))
  17841. (defun org-splice-latex-header (tpl def-pkg pkg snippets-p &optional extra)
  17842. "Fill a LaTeX header template TPL.
  17843. In the template, the following place holders will be recognized:
  17844. [DEFAULT-PACKAGES] \\usepackage statements for DEF-PKG
  17845. [NO-DEFAULT-PACKAGES] do not include DEF-PKG
  17846. [PACKAGES] \\usepackage statements for PKG
  17847. [NO-PACKAGES] do not include PKG
  17848. [EXTRA] the string EXTRA
  17849. [NO-EXTRA] do not include EXTRA
  17850. For backward compatibility, if both the positive and the negative place
  17851. holder is missing, the positive one (without the \"NO-\") will be
  17852. assumed to be present at the end of the template.
  17853. DEF-PKG and PKG are assumed to be alists of options/packagename lists.
  17854. EXTRA is a string.
  17855. SNIPPETS-P indicates if this is run to create snippet images for HTML."
  17856. (let (rpl (end ""))
  17857. (if (string-match "^[ \t]*\\[\\(NO-\\)?DEFAULT-PACKAGES\\][ \t]*\n?" tpl)
  17858. (setq rpl (if (or (match-end 1) (not def-pkg))
  17859. "" (org-latex-packages-to-string def-pkg snippets-p t))
  17860. tpl (replace-match rpl t t tpl))
  17861. (when def-pkg (setq end (org-latex-packages-to-string def-pkg snippets-p))))
  17862. (if (string-match "\\[\\(NO-\\)?PACKAGES\\][ \t]*\n?" tpl)
  17863. (setq rpl (if (or (match-end 1) (not pkg))
  17864. "" (org-latex-packages-to-string pkg snippets-p t))
  17865. tpl (replace-match rpl t t tpl))
  17866. (when pkg (setq end
  17867. (concat end "\n"
  17868. (org-latex-packages-to-string pkg snippets-p)))))
  17869. (if (string-match "\\[\\(NO-\\)?EXTRA\\][ \t]*\n?" tpl)
  17870. (setq rpl (if (or (match-end 1) (not extra))
  17871. "" (concat extra "\n"))
  17872. tpl (replace-match rpl t t tpl))
  17873. (when (and extra (string-match "\\S-" extra))
  17874. (setq end (concat end "\n" extra))))
  17875. (if (string-match "\\S-" end)
  17876. (concat tpl "\n" end)
  17877. tpl)))
  17878. (defun org-latex-packages-to-string (pkg &optional snippets-p newline)
  17879. "Turn an alist of packages into a string with the \\usepackage macros."
  17880. (setq pkg (mapconcat (lambda(p)
  17881. (cond
  17882. ((stringp p) p)
  17883. ((and snippets-p (>= (length p) 3) (not (nth 2 p)))
  17884. (format "%% Package %s omitted" (cadr p)))
  17885. ((equal "" (car p))
  17886. (format "\\usepackage{%s}" (cadr p)))
  17887. (t
  17888. (format "\\usepackage[%s]{%s}"
  17889. (car p) (cadr p)))))
  17890. pkg
  17891. "\n"))
  17892. (if newline (concat pkg "\n") pkg))
  17893. (defun org-dvipng-color (attr)
  17894. "Return a RGB color specification for dvipng."
  17895. (apply 'format "rgb %s %s %s"
  17896. (mapcar 'org-normalize-color
  17897. (if (featurep 'xemacs)
  17898. (color-rgb-components
  17899. (face-property 'default
  17900. (cond ((eq attr :foreground) 'foreground)
  17901. ((eq attr :background) 'background))))
  17902. (color-values (face-attribute 'default attr nil))))))
  17903. (defun org-dvipng-color-format (color-name)
  17904. "Convert COLOR-NAME to a RGB color value for dvipng."
  17905. (apply 'format "rgb %s %s %s"
  17906. (mapcar 'org-normalize-color
  17907. (color-values color-name))))
  17908. (defun org-latex-color (attr)
  17909. "Return a RGB color for the LaTeX color package."
  17910. (apply 'format "%s,%s,%s"
  17911. (mapcar 'org-normalize-color
  17912. (if (featurep 'xemacs)
  17913. (color-rgb-components
  17914. (face-property 'default
  17915. (cond ((eq attr :foreground) 'foreground)
  17916. ((eq attr :background) 'background))))
  17917. (color-values (face-attribute 'default attr nil))))))
  17918. (defun org-latex-color-format (color-name)
  17919. "Convert COLOR-NAME to a RGB color value."
  17920. (apply 'format "%s,%s,%s"
  17921. (mapcar 'org-normalize-color
  17922. (color-values color-name))))
  17923. (defun org-normalize-color (value)
  17924. "Return string to be used as color value for an RGB component."
  17925. (format "%g" (/ value 65535.0)))
  17926. ;; Image display
  17927. (defvar-local org-inline-image-overlays nil)
  17928. (defun org-toggle-inline-images (&optional include-linked)
  17929. "Toggle the display of inline images.
  17930. INCLUDE-LINKED is passed to `org-display-inline-images'."
  17931. (interactive "P")
  17932. (if org-inline-image-overlays
  17933. (progn
  17934. (org-remove-inline-images)
  17935. (when (org-called-interactively-p 'interactive)
  17936. (message "Inline image display turned off")))
  17937. (org-display-inline-images include-linked)
  17938. (when (org-called-interactively-p 'interactive)
  17939. (message (if org-inline-image-overlays
  17940. (format "%d images displayed inline"
  17941. (length org-inline-image-overlays))
  17942. "No images to display inline")))))
  17943. (defun org-redisplay-inline-images ()
  17944. "Refresh the display of inline images."
  17945. (interactive)
  17946. (if (not org-inline-image-overlays)
  17947. (org-toggle-inline-images)
  17948. (org-toggle-inline-images)
  17949. (org-toggle-inline-images)))
  17950. (defun org-display-inline-images (&optional include-linked refresh beg end)
  17951. "Display inline images.
  17952. An inline image is a link which follows either of these
  17953. conventions:
  17954. 1. Its path is a file with an extension matching return value
  17955. from `image-file-name-regexp' and it has no contents.
  17956. 2. Its description consists in a single link of the previous
  17957. type.
  17958. When optional argument INCLUDE-LINKED is non-nil, also links with
  17959. a text description part will be inlined. This can be nice for
  17960. a quick look at those images, but it does not reflect what
  17961. exported files will look like.
  17962. When optional argument REFRESH is non-nil, refresh existing
  17963. images between BEG and END. This will create new image displays
  17964. only if necessary. BEG and END default to the buffer
  17965. boundaries."
  17966. (interactive "P")
  17967. (when (display-graphic-p)
  17968. (unless refresh
  17969. (org-remove-inline-images)
  17970. (when (fboundp 'clear-image-cache) (clear-image-cache)))
  17971. (org-with-wide-buffer
  17972. (goto-char (or beg (point-min)))
  17973. (let ((case-fold-search t)
  17974. (file-extension-re (image-file-name-regexp)))
  17975. (while (re-search-forward "[][]\\[\\(?:file\\|[./~]\\)" end t)
  17976. (let ((link (save-match-data (org-element-context))))
  17977. ;; Check if we're at an inline image.
  17978. (when (and (equal (org-element-property :type link) "file")
  17979. (or include-linked
  17980. (not (org-element-property :contents-begin link)))
  17981. (let ((parent (org-element-property :parent link)))
  17982. (or (not (eq (org-element-type parent) 'link))
  17983. (not (cdr (org-element-contents parent)))))
  17984. (org-string-match-p file-extension-re
  17985. (org-element-property :path link)))
  17986. (let ((file (expand-file-name
  17987. (org-link-unescape
  17988. (org-element-property :path link)))))
  17989. (when (file-exists-p file)
  17990. (let ((width
  17991. ;; Apply `org-image-actual-width' specifications.
  17992. (cond
  17993. ((not (image-type-available-p 'imagemagick)) nil)
  17994. ((eq org-image-actual-width t) nil)
  17995. ((listp org-image-actual-width)
  17996. (or
  17997. ;; First try to find a width among
  17998. ;; attributes associated to the paragraph
  17999. ;; containing link.
  18000. (let ((paragraph
  18001. (let ((e link))
  18002. (while (and (setq e (org-element-property
  18003. :parent e))
  18004. (not (eq (org-element-type e)
  18005. 'paragraph))))
  18006. e)))
  18007. (when paragraph
  18008. (save-excursion
  18009. (goto-char (org-element-property :begin paragraph))
  18010. (when
  18011. (re-search-forward
  18012. "^[ \t]*#\\+attr_.*?: +.*?:width +\\(\\S-+\\)"
  18013. (org-element-property
  18014. :post-affiliated paragraph)
  18015. t)
  18016. (string-to-number (match-string 1))))))
  18017. ;; Otherwise, fall-back to provided number.
  18018. (car org-image-actual-width)))
  18019. ((numberp org-image-actual-width)
  18020. org-image-actual-width)))
  18021. (old (get-char-property-and-overlay
  18022. (org-element-property :begin link)
  18023. 'org-image-overlay)))
  18024. (if (and (car-safe old) refresh)
  18025. (image-refresh (overlay-get (cdr old) 'display))
  18026. (let ((image (create-image file
  18027. (and width 'imagemagick)
  18028. nil
  18029. :width width)))
  18030. (when image
  18031. (let* ((link
  18032. ;; If inline image is the description
  18033. ;; of another link, be sure to
  18034. ;; consider the latter as the one to
  18035. ;; apply the overlay on.
  18036. (let ((parent
  18037. (org-element-property :parent link)))
  18038. (if (eq (org-element-type parent) 'link)
  18039. parent
  18040. link)))
  18041. (ov (make-overlay
  18042. (org-element-property :begin link)
  18043. (progn
  18044. (goto-char
  18045. (org-element-property :end link))
  18046. (skip-chars-backward " \t")
  18047. (point)))))
  18048. (overlay-put ov 'display image)
  18049. (overlay-put ov 'face 'default)
  18050. (overlay-put ov 'org-image-overlay t)
  18051. (overlay-put
  18052. ov 'modification-hooks
  18053. (list 'org-display-inline-remove-overlay))
  18054. (push ov org-inline-image-overlays)))))))))))))))
  18055. (define-obsolete-function-alias
  18056. 'org-display-inline-modification-hook 'org-display-inline-remove-overlay "24.3")
  18057. (defun org-display-inline-remove-overlay (ov after beg end &optional len)
  18058. "Remove inline-display overlay if a corresponding region is modified."
  18059. (let ((inhibit-modification-hooks t))
  18060. (when (and ov after)
  18061. (delete ov org-inline-image-overlays)
  18062. (delete-overlay ov))))
  18063. (defun org-remove-inline-images ()
  18064. "Remove inline display of images."
  18065. (interactive)
  18066. (mapc 'delete-overlay org-inline-image-overlays)
  18067. (setq org-inline-image-overlays nil))
  18068. ;;;; Key bindings
  18069. ;; Outline functions from `outline-mode-prefix-map'
  18070. ;; that can be remapped in Org:
  18071. (define-key org-mode-map [remap outline-mark-subtree] 'org-mark-subtree)
  18072. (define-key org-mode-map [remap outline-show-subtree] 'org-show-subtree)
  18073. (define-key org-mode-map [remap outline-forward-same-level]
  18074. 'org-forward-heading-same-level)
  18075. (define-key org-mode-map [remap outline-backward-same-level]
  18076. 'org-backward-heading-same-level)
  18077. (define-key org-mode-map [remap outline-show-branches]
  18078. 'org-kill-note-or-show-branches)
  18079. (define-key org-mode-map [remap outline-promote] 'org-promote-subtree)
  18080. (define-key org-mode-map [remap outline-demote] 'org-demote-subtree)
  18081. (define-key org-mode-map [remap outline-insert-heading] 'org-ctrl-c-ret)
  18082. (define-key org-mode-map [remap outline-next-visible-heading]
  18083. 'org-next-visible-heading)
  18084. (define-key org-mode-map [remap outline-previous-visible-heading]
  18085. 'org-previous-visible-heading)
  18086. (define-key org-mode-map [remap show-children] 'org-show-children)
  18087. ;; Outline functions from `outline-mode-prefix-map' that can not
  18088. ;; be remapped in Org:
  18089. ;; - the column "key binding" shows whether the Outline function is still
  18090. ;; available in Org mode on the same key that it has been bound to in
  18091. ;; Outline mode:
  18092. ;; - "overridden": key used for a different functionality in Org mode
  18093. ;; - else: key still bound to the same Outline function in Org mode
  18094. ;; | Outline function | key binding | Org replacement |
  18095. ;; |------------------------------------+-------------+--------------------------|
  18096. ;; | `outline-up-heading' | `C-c C-u' | still same function |
  18097. ;; | `outline-move-subtree-up' | overridden | better: org-shiftup |
  18098. ;; | `outline-move-subtree-down' | overridden | better: org-shiftdown |
  18099. ;; | `show-entry' | overridden | no replacement |
  18100. ;; | `show-branches' | `C-c C-k' | still same function |
  18101. ;; | `show-subtree' | overridden | visibility cycling |
  18102. ;; | `show-all' | overridden | no replacement |
  18103. ;; | `hide-subtree' | overridden | visibility cycling |
  18104. ;; | `hide-body' | overridden | no replacement |
  18105. ;; | `hide-entry' | overridden | visibility cycling |
  18106. ;; | `hide-leaves' | overridden | no replacement |
  18107. ;; | `hide-sublevels' | overridden | no replacement |
  18108. ;; | `hide-other' | overridden | no replacement |
  18109. ;; Make `C-c C-x' a prefix key
  18110. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  18111. ;; TAB key with modifiers
  18112. (org-defkey org-mode-map "\C-i" 'org-cycle)
  18113. (org-defkey org-mode-map [(tab)] 'org-cycle)
  18114. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  18115. (org-defkey org-mode-map "\M-\t" #'pcomplete)
  18116. ;; The following line is necessary under Suse GNU/Linux
  18117. (unless (featurep 'xemacs)
  18118. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  18119. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  18120. (define-key org-mode-map [backtab] 'org-shifttab)
  18121. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  18122. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  18123. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  18124. ;; Cursor keys with modifiers
  18125. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  18126. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  18127. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  18128. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  18129. (org-defkey org-mode-map [(control meta shift right)] 'org-increase-number-at-point)
  18130. (org-defkey org-mode-map [(control meta shift left)] 'org-decrease-number-at-point)
  18131. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  18132. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  18133. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  18134. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  18135. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  18136. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  18137. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  18138. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  18139. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  18140. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  18141. (org-defkey org-mode-map [(control shift up)] 'org-shiftcontrolup)
  18142. (org-defkey org-mode-map [(control shift down)] 'org-shiftcontroldown)
  18143. ;; Babel keys
  18144. (define-key org-mode-map org-babel-key-prefix org-babel-map)
  18145. (mapc (lambda (pair)
  18146. (define-key org-babel-map (car pair) (cdr pair)))
  18147. org-babel-key-bindings)
  18148. ;;; Extra keys for tty access.
  18149. ;; We only set them when really needed because otherwise the
  18150. ;; menus don't show the simple keys
  18151. (when (or org-use-extra-keys
  18152. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  18153. (not window-system))
  18154. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  18155. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  18156. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  18157. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  18158. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  18159. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  18160. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  18161. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  18162. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  18163. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  18164. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  18165. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  18166. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  18167. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  18168. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  18169. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  18170. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  18171. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  18172. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  18173. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  18174. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  18175. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  18176. (org-defkey org-mode-map [?\e (tab)] #'pcomplete)
  18177. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  18178. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  18179. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  18180. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  18181. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  18182. ;; All the other keys
  18183. (org-defkey org-mode-map "\C-c\C-a" 'outline-show-all) ; in case allout messed up.
  18184. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  18185. (if (boundp 'narrow-map)
  18186. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  18187. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  18188. (if (boundp 'narrow-map)
  18189. (org-defkey narrow-map "b" 'org-narrow-to-block)
  18190. (org-defkey org-mode-map "\C-xnb" 'org-narrow-to-block))
  18191. (if (boundp 'narrow-map)
  18192. (org-defkey narrow-map "e" 'org-narrow-to-element)
  18193. (org-defkey org-mode-map "\C-xne" 'org-narrow-to-element))
  18194. (org-defkey org-mode-map "\C-\M-t" 'org-transpose-element)
  18195. (org-defkey org-mode-map "\M-}" 'org-forward-element)
  18196. (org-defkey org-mode-map "\M-{" 'org-backward-element)
  18197. (org-defkey org-mode-map "\C-c\C-^" 'org-up-element)
  18198. (org-defkey org-mode-map "\C-c\C-_" 'org-down-element)
  18199. (org-defkey org-mode-map "\C-c\C-f" 'org-forward-heading-same-level)
  18200. (org-defkey org-mode-map "\C-c\C-b" 'org-backward-heading-same-level)
  18201. (org-defkey org-mode-map "\C-c\M-f" 'org-next-block)
  18202. (org-defkey org-mode-map "\C-c\M-b" 'org-previous-block)
  18203. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  18204. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-archive-subtree)
  18205. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
  18206. (org-defkey org-mode-map "\C-c\C-xd" 'org-insert-drawer)
  18207. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  18208. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  18209. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  18210. (org-defkey org-mode-map "\C-c\C-xq" 'org-toggle-tags-groups)
  18211. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  18212. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  18213. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  18214. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  18215. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  18216. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  18217. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  18218. (org-defkey org-mode-map "\C-c\M-w" 'org-copy)
  18219. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  18220. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  18221. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  18222. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  18223. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  18224. (org-defkey org-mode-map "\C-c\C-xv" 'org-copy-visible)
  18225. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  18226. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  18227. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  18228. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  18229. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  18230. (org-defkey org-mode-map "\C-c\M-l" 'org-insert-last-stored-link)
  18231. (org-defkey org-mode-map "\C-c\C-\M-l" 'org-insert-all-links)
  18232. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  18233. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  18234. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  18235. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  18236. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  18237. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  18238. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  18239. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  18240. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  18241. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  18242. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  18243. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  18244. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  18245. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  18246. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  18247. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  18248. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  18249. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  18250. (org-defkey org-mode-map "\C-c^" 'org-sort)
  18251. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  18252. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  18253. (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
  18254. (org-defkey org-mode-map [remap open-line] 'org-open-line)
  18255. (org-defkey org-mode-map [remap comment-dwim] 'org-comment-dwim)
  18256. (org-defkey org-mode-map [remap forward-paragraph] 'org-forward-paragraph)
  18257. (org-defkey org-mode-map [remap backward-paragraph] 'org-backward-paragraph)
  18258. (org-defkey org-mode-map "\M-^" 'org-delete-indentation)
  18259. (org-defkey org-mode-map "\C-m" 'org-return)
  18260. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  18261. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  18262. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  18263. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  18264. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  18265. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  18266. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  18267. (org-defkey org-mode-map "\C-c\"a" 'orgtbl-ascii-plot)
  18268. (org-defkey org-mode-map "\C-c\"g" 'org-plot/gnuplot)
  18269. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  18270. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  18271. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  18272. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  18273. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  18274. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  18275. (org-defkey org-mode-map "\C-c\C-e" 'org-export-dispatch)
  18276. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width)
  18277. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  18278. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  18279. (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
  18280. (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
  18281. (org-defkey org-mode-map "\C-c@" 'org-mark-subtree)
  18282. (org-defkey org-mode-map "\M-h" 'org-mark-element)
  18283. (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
  18284. ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
  18285. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  18286. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  18287. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  18288. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  18289. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  18290. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-in-last)
  18291. (org-defkey org-mode-map "\C-c\C-x\C-z" 'org-resolve-clocks)
  18292. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  18293. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  18294. (org-defkey org-mode-map "\C-c\C-x\C-q" 'org-clock-cancel)
  18295. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  18296. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  18297. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  18298. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-toggle-latex-fragment)
  18299. (org-defkey org-mode-map "\C-c\C-x\C-v" 'org-toggle-inline-images)
  18300. (org-defkey org-mode-map "\C-c\C-x\C-\M-v" 'org-redisplay-inline-images)
  18301. (org-defkey org-mode-map "\C-c\C-x\\" 'org-toggle-pretty-entities)
  18302. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  18303. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  18304. (org-defkey org-mode-map "\C-c\C-xP" 'org-set-property-and-value)
  18305. (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
  18306. (org-defkey org-mode-map "\C-c\C-xE" 'org-inc-effort)
  18307. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  18308. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  18309. (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
  18310. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  18311. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  18312. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  18313. (org-defkey org-mode-map "\C-c\C-x_" 'org-timer-stop)
  18314. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  18315. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  18316. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  18317. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  18318. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  18319. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  18320. (when (featurep 'xemacs)
  18321. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  18322. (defconst org-speed-commands-default
  18323. '(
  18324. ("Outline Navigation")
  18325. ("n" . (org-speed-move-safe 'org-next-visible-heading))
  18326. ("p" . (org-speed-move-safe 'org-previous-visible-heading))
  18327. ("f" . (org-speed-move-safe 'org-forward-heading-same-level))
  18328. ("b" . (org-speed-move-safe 'org-backward-heading-same-level))
  18329. ("F" . org-next-block)
  18330. ("B" . org-previous-block)
  18331. ("u" . (org-speed-move-safe 'outline-up-heading))
  18332. ("j" . org-goto)
  18333. ("g" . (org-refile t))
  18334. ("Outline Visibility")
  18335. ("c" . org-cycle)
  18336. ("C" . org-shifttab)
  18337. (" " . org-display-outline-path)
  18338. ("s" . org-narrow-to-subtree)
  18339. ("=" . org-columns)
  18340. ("Outline Structure Editing")
  18341. ("U" . org-metaup)
  18342. ("D" . org-metadown)
  18343. ("r" . org-metaright)
  18344. ("l" . org-metaleft)
  18345. ("R" . org-shiftmetaright)
  18346. ("L" . org-shiftmetaleft)
  18347. ("i" . (progn (forward-char 1) (call-interactively
  18348. 'org-insert-heading-respect-content)))
  18349. ("^" . org-sort)
  18350. ("w" . org-refile)
  18351. ("a" . org-archive-subtree-default-with-confirmation)
  18352. ("@" . org-mark-subtree)
  18353. ("#" . org-toggle-comment)
  18354. ("Clock Commands")
  18355. ("I" . org-clock-in)
  18356. ("O" . org-clock-out)
  18357. ("Meta Data Editing")
  18358. ("t" . org-todo)
  18359. ("," . (org-priority))
  18360. ("0" . (org-priority ?\ ))
  18361. ("1" . (org-priority ?A))
  18362. ("2" . (org-priority ?B))
  18363. ("3" . (org-priority ?C))
  18364. (":" . org-set-tags-command)
  18365. ("e" . org-set-effort)
  18366. ("E" . org-inc-effort)
  18367. ("W" . (lambda(m) (interactive "sMinutes before warning: ")
  18368. (org-entry-put (point) "APPT_WARNTIME" m)))
  18369. ("Agenda Views etc")
  18370. ("v" . org-agenda)
  18371. ("/" . org-sparse-tree)
  18372. ("Misc")
  18373. ("o" . org-open-at-point)
  18374. ("?" . org-speed-command-help)
  18375. ("<" . (org-agenda-set-restriction-lock 'subtree))
  18376. (">" . (org-agenda-remove-restriction-lock))
  18377. )
  18378. "The default speed commands.")
  18379. (defun org-print-speed-command (e)
  18380. (if (> (length (car e)) 1)
  18381. (progn
  18382. (princ "\n")
  18383. (princ (car e))
  18384. (princ "\n")
  18385. (princ (make-string (length (car e)) ?-))
  18386. (princ "\n"))
  18387. (princ (car e))
  18388. (princ " ")
  18389. (if (symbolp (cdr e))
  18390. (princ (symbol-name (cdr e)))
  18391. (prin1 (cdr e)))
  18392. (princ "\n")))
  18393. (defun org-speed-command-help ()
  18394. "Show the available speed commands."
  18395. (interactive)
  18396. (if (not org-use-speed-commands)
  18397. (user-error "Speed commands are not activated, customize `org-use-speed-commands'")
  18398. (with-output-to-temp-buffer "*Help*"
  18399. (princ "User-defined Speed commands\n===========================\n")
  18400. (mapc 'org-print-speed-command org-speed-commands-user)
  18401. (princ "\n")
  18402. (princ "Built-in Speed commands\n=======================\n")
  18403. (mapc 'org-print-speed-command org-speed-commands-default))
  18404. (with-current-buffer "*Help*"
  18405. (setq truncate-lines t))))
  18406. (defun org-speed-move-safe (cmd)
  18407. "Execute CMD, but make sure that the cursor always ends up in a headline.
  18408. If not, return to the original position and throw an error."
  18409. (interactive)
  18410. (let ((pos (point)))
  18411. (call-interactively cmd)
  18412. (unless (and (bolp) (org-at-heading-p))
  18413. (goto-char pos)
  18414. (error "Boundary reached while executing %s" cmd))))
  18415. (defvar org-self-insert-command-undo-counter 0)
  18416. (defvar org-table-auto-blank-field) ; defined in org-table.el
  18417. (defvar org-speed-command nil)
  18418. (define-obsolete-function-alias
  18419. 'org-speed-command-default-hook 'org-speed-command-activate "24.3")
  18420. (defun org-speed-command-activate (keys)
  18421. "Hook for activating single-letter speed commands.
  18422. `org-speed-commands-default' specifies a minimal command set.
  18423. Use `org-speed-commands-user' for further customization."
  18424. (when (or (and (bolp) (looking-at org-outline-regexp))
  18425. (and (functionp org-use-speed-commands)
  18426. (funcall org-use-speed-commands)))
  18427. (cdr (assoc keys (append org-speed-commands-user
  18428. org-speed-commands-default)))))
  18429. (define-obsolete-function-alias
  18430. 'org-babel-speed-command-hook 'org-babel-speed-command-activate "24.3")
  18431. (defun org-babel-speed-command-activate (keys)
  18432. "Hook for activating single-letter code block commands."
  18433. (when (and (bolp) (looking-at org-babel-src-block-regexp))
  18434. (cdr (assoc keys org-babel-key-bindings))))
  18435. (defcustom org-speed-command-hook
  18436. '(org-speed-command-default-hook org-babel-speed-command-hook)
  18437. "Hook for activating speed commands at strategic locations.
  18438. Hook functions are called in sequence until a valid handler is
  18439. found.
  18440. Each hook takes a single argument, a user-pressed command key
  18441. which is also a `self-insert-command' from the global map.
  18442. Within the hook, examine the cursor position and the command key
  18443. and return nil or a valid handler as appropriate. Handler could
  18444. be one of an interactive command, a function, or a form.
  18445. Set `org-use-speed-commands' to non-nil value to enable this
  18446. hook. The default setting is `org-speed-command-activate'."
  18447. :group 'org-structure
  18448. :version "24.1"
  18449. :type 'hook)
  18450. (defun org-self-insert-command (N)
  18451. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  18452. If the cursor is in a table looking at whitespace, the whitespace is
  18453. overwritten, and the table is not marked as requiring realignment."
  18454. (interactive "p")
  18455. (org-check-before-invisible-edit 'insert)
  18456. (cond
  18457. ((and org-use-speed-commands
  18458. (let ((kv (this-command-keys-vector)))
  18459. (setq org-speed-command
  18460. (run-hook-with-args-until-success
  18461. 'org-speed-command-hook
  18462. (make-string 1 (aref kv (1- (length kv))))))))
  18463. (cond
  18464. ((commandp org-speed-command)
  18465. (setq this-command org-speed-command)
  18466. (call-interactively org-speed-command))
  18467. ((functionp org-speed-command)
  18468. (funcall org-speed-command))
  18469. ((and org-speed-command (listp org-speed-command))
  18470. (eval org-speed-command))
  18471. (t (let (org-use-speed-commands)
  18472. (call-interactively 'org-self-insert-command)))))
  18473. ((and
  18474. (org-at-table-p)
  18475. (progn
  18476. ;; Check if we blank the field, and if that triggers align.
  18477. (and (featurep 'org-table) org-table-auto-blank-field
  18478. (memq last-command
  18479. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
  18480. (if (or (eq (char-after) ?\s) (looking-at "[^|\n]* |"))
  18481. ;; Got extra space, this field does not determine
  18482. ;; column width.
  18483. (let (org-table-may-need-update) (org-table-blank-field))
  18484. ;; No extra space, this field may determine column
  18485. ;; width.
  18486. (org-table-blank-field)))
  18487. t)
  18488. (eq N 1)
  18489. (looking-at "[^|\n]* \\( \\)|"))
  18490. ;; There is room for insertion without re-aligning the table.
  18491. (delete-region (match-beginning 1) (match-end 1))
  18492. (self-insert-command N))
  18493. (t
  18494. (setq org-table-may-need-update t)
  18495. (self-insert-command N)
  18496. (org-fix-tags-on-the-fly)
  18497. (when org-self-insert-cluster-for-undo
  18498. (if (not (eq last-command 'org-self-insert-command))
  18499. (setq org-self-insert-command-undo-counter 1)
  18500. (if (>= org-self-insert-command-undo-counter 20)
  18501. (setq org-self-insert-command-undo-counter 1)
  18502. (and (> org-self-insert-command-undo-counter 0)
  18503. buffer-undo-list (listp buffer-undo-list)
  18504. (not (cadr buffer-undo-list)) ; remove nil entry
  18505. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  18506. (setq org-self-insert-command-undo-counter
  18507. (1+ org-self-insert-command-undo-counter))))))))
  18508. (defun org-check-before-invisible-edit (kind)
  18509. "Check is editing if kind KIND would be dangerous with invisible text around.
  18510. The detailed reaction depends on the user option `org-catch-invisible-edits'."
  18511. ;; First, try to get out of here as quickly as possible, to reduce overhead
  18512. (when (and org-catch-invisible-edits
  18513. (or (not (boundp 'visible-mode)) (not visible-mode))
  18514. (or (get-char-property (point) 'invisible)
  18515. (get-char-property (max (point-min) (1- (point))) 'invisible)))
  18516. ;; OK, we need to take a closer look
  18517. (let* ((invisible-at-point (get-char-property (point) 'invisible))
  18518. (invisible-before-point (unless (bobp) (get-char-property
  18519. (1- (point)) 'invisible)))
  18520. (border-and-ok-direction
  18521. (or
  18522. ;; Check if we are acting predictably before invisible text
  18523. (and invisible-at-point (not invisible-before-point)
  18524. (memq kind '(insert delete-backward)))
  18525. ;; Check if we are acting predictably after invisible text
  18526. ;; This works not well, and I have turned it off. It seems
  18527. ;; better to always show and stop after invisible text.
  18528. ;; (and (not invisible-at-point) invisible-before-point
  18529. ;; (memq kind '(insert delete)))
  18530. )))
  18531. (when (or (memq invisible-at-point '(outline org-hide-block t))
  18532. (memq invisible-before-point '(outline org-hide-block t)))
  18533. (when (eq org-catch-invisible-edits 'error)
  18534. (user-error "Editing in invisible areas is prohibited, make them visible first"))
  18535. (if (and org-custom-properties-overlays
  18536. (y-or-n-p "Display invisible properties in this buffer? "))
  18537. (org-toggle-custom-properties-visibility)
  18538. ;; Make the area visible
  18539. (save-excursion
  18540. (when invisible-before-point
  18541. (goto-char (previous-single-char-property-change
  18542. (point) 'invisible)))
  18543. (outline-show-subtree))
  18544. (cond
  18545. ((eq org-catch-invisible-edits 'show)
  18546. ;; That's it, we do the edit after showing
  18547. (message
  18548. "Unfolding invisible region around point before editing")
  18549. (sit-for 1))
  18550. ((and (eq org-catch-invisible-edits 'smart)
  18551. border-and-ok-direction)
  18552. (message "Unfolding invisible region around point before editing"))
  18553. (t
  18554. ;; Don't do the edit, make the user repeat it in full visibility
  18555. (user-error "Edit in invisible region aborted, repeat to confirm with text visible"))))))))
  18556. (defun org-fix-tags-on-the-fly ()
  18557. (when (and (equal (char-after (point-at-bol)) ?*)
  18558. (org-at-heading-p))
  18559. (org-align-tags-here org-tags-column)))
  18560. (defun org-delete-backward-char (N)
  18561. "Like `delete-backward-char', insert whitespace at field end in tables.
  18562. When deleting backwards, in tables this function will insert whitespace in
  18563. front of the next \"|\" separator, to keep the table aligned. The table will
  18564. still be marked for re-alignment if the field did fill the entire column,
  18565. because, in this case the deletion might narrow the column."
  18566. (interactive "p")
  18567. (save-match-data
  18568. (org-check-before-invisible-edit 'delete-backward)
  18569. (if (and (org-at-table-p)
  18570. (eq N 1)
  18571. (string-match "|" (buffer-substring (point-at-bol) (point)))
  18572. (looking-at ".*?|"))
  18573. (let ((pos (point))
  18574. (noalign (looking-at "[^|\n\r]* |"))
  18575. (c org-table-may-need-update))
  18576. (backward-delete-char N)
  18577. (unless overwrite-mode
  18578. (skip-chars-forward "^|")
  18579. (insert " ")
  18580. (goto-char (1- pos)))
  18581. ;; noalign: if there were two spaces at the end, this field
  18582. ;; does not determine the width of the column.
  18583. (when noalign (setq org-table-may-need-update c)))
  18584. (backward-delete-char N)
  18585. (org-fix-tags-on-the-fly))))
  18586. (defun org-delete-char (N)
  18587. "Like `delete-char', but insert whitespace at field end in tables.
  18588. When deleting characters, in tables this function will insert whitespace in
  18589. front of the next \"|\" separator, to keep the table aligned. The table will
  18590. still be marked for re-alignment if the field did fill the entire column,
  18591. because, in this case the deletion might narrow the column."
  18592. (interactive "p")
  18593. (save-match-data
  18594. (org-check-before-invisible-edit 'delete)
  18595. (if (and (org-at-table-p)
  18596. (not (bolp))
  18597. (not (= (char-after) ?|))
  18598. (eq N 1))
  18599. (if (looking-at ".*?|")
  18600. (let ((pos (point))
  18601. (noalign (looking-at "[^|\n\r]* |"))
  18602. (c org-table-may-need-update))
  18603. (replace-match
  18604. (concat (substring (match-string 0) 1 -1) " |") nil t)
  18605. (goto-char pos)
  18606. ;; noalign: if there were two spaces at the end, this field
  18607. ;; does not determine the width of the column.
  18608. (when noalign (setq org-table-may-need-update c)))
  18609. (delete-char N))
  18610. (delete-char N)
  18611. (org-fix-tags-on-the-fly))))
  18612. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  18613. (put 'org-self-insert-command 'delete-selection
  18614. (lambda ()
  18615. (not (run-hook-with-args-until-success
  18616. 'self-insert-uses-region-functions))))
  18617. (put 'orgtbl-self-insert-command 'delete-selection
  18618. (lambda ()
  18619. (not (run-hook-with-args-until-success
  18620. 'self-insert-uses-region-functions))))
  18621. (put 'org-delete-char 'delete-selection 'supersede)
  18622. (put 'org-delete-backward-char 'delete-selection 'supersede)
  18623. (put 'org-yank 'delete-selection 'yank)
  18624. ;; Make `flyspell-mode' delay after some commands
  18625. (put 'org-self-insert-command 'flyspell-delayed t)
  18626. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  18627. (put 'org-delete-char 'flyspell-delayed t)
  18628. (put 'org-delete-backward-char 'flyspell-delayed t)
  18629. ;; Make pabbrev-mode expand after org-mode commands
  18630. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  18631. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  18632. (defun org-remap (map &rest commands)
  18633. "In MAP, remap the functions given in COMMANDS.
  18634. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  18635. (let (new old)
  18636. (while commands
  18637. (setq old (pop commands) new (pop commands))
  18638. (if (fboundp 'command-remapping)
  18639. (org-defkey map (vector 'remap old) new)
  18640. (substitute-key-definition old new map global-map)))))
  18641. (defun org-transpose-words ()
  18642. "Transpose words for Org.
  18643. This uses the `org-mode-transpose-word-syntax-table' syntax
  18644. table, which interprets characters in `org-emphasis-alist' as
  18645. word constituents."
  18646. (interactive)
  18647. (with-syntax-table org-mode-transpose-word-syntax-table
  18648. (call-interactively 'transpose-words)))
  18649. (org-remap org-mode-map 'transpose-words 'org-transpose-words)
  18650. (when (eq org-enable-table-editor 'optimized)
  18651. ;; If the user wants maximum table support, we need to hijack
  18652. ;; some standard editing functions
  18653. (org-remap org-mode-map
  18654. 'self-insert-command 'org-self-insert-command
  18655. 'delete-char 'org-delete-char
  18656. 'delete-backward-char 'org-delete-backward-char)
  18657. (org-defkey org-mode-map "|" 'org-force-self-insert))
  18658. (defvar org-ctrl-c-ctrl-c-hook nil
  18659. "Hook for functions attaching themselves to `C-c C-c'.
  18660. This can be used to add additional functionality to the C-c C-c
  18661. key which executes context-dependent commands. This hook is run
  18662. before any other test, while `org-ctrl-c-ctrl-c-final-hook' is
  18663. run after the last test.
  18664. Each function will be called with no arguments. The function
  18665. must check if the context is appropriate for it to act. If yes,
  18666. it should do its thing and then return a non-nil value. If the
  18667. context is wrong, just do nothing and return nil.")
  18668. (defvar org-ctrl-c-ctrl-c-final-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. after any other test, while `org-ctrl-c-ctrl-c-hook' is run
  18673. before the first 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-tab-first-hook nil
  18679. "Hook for functions to attach themselves to TAB.
  18680. See `org-ctrl-c-ctrl-c-hook' for more information.
  18681. This hook runs as the first action when TAB is pressed, even before
  18682. `org-cycle' messes around with the `outline-regexp' to cater for
  18683. inline tasks and plain list item folding.
  18684. If any function in this hook returns t, any other actions that
  18685. would have been caused by TAB (such as table field motion or visibility
  18686. cycling) will not occur.")
  18687. (defvar org-tab-after-check-for-table-hook nil
  18688. "Hook for functions to attach themselves to TAB.
  18689. See `org-ctrl-c-ctrl-c-hook' for more information.
  18690. This hook runs after it has been established that the cursor is not in a
  18691. table, but before checking if the cursor is in a headline or if global cycling
  18692. should be done.
  18693. If any function in this hook returns t, not other actions like visibility
  18694. cycling will be done.")
  18695. (defvar org-tab-after-check-for-cycling-hook nil
  18696. "Hook for functions to attach themselves to TAB.
  18697. See `org-ctrl-c-ctrl-c-hook' for more information.
  18698. This hook runs after it has been established that not table field motion and
  18699. not visibility should be done because of current context. This is probably
  18700. the place where a package like yasnippets can hook in.")
  18701. (defvar org-tab-before-tab-emulation-hook nil
  18702. "Hook for functions to attach themselves to TAB.
  18703. See `org-ctrl-c-ctrl-c-hook' for more information.
  18704. This hook runs after every other options for TAB have been exhausted, but
  18705. before indentation and \t insertion takes place.")
  18706. (defvar org-metaleft-hook nil
  18707. "Hook for functions attaching themselves to `M-left'.
  18708. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18709. (defvar org-metaright-hook nil
  18710. "Hook for functions attaching themselves to `M-right'.
  18711. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18712. (defvar org-metaup-hook nil
  18713. "Hook for functions attaching themselves to `M-up'.
  18714. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18715. (defvar org-metadown-hook nil
  18716. "Hook for functions attaching themselves to `M-down'.
  18717. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18718. (defvar org-shiftmetaleft-hook nil
  18719. "Hook for functions attaching themselves to `M-S-left'.
  18720. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18721. (defvar org-shiftmetaright-hook nil
  18722. "Hook for functions attaching themselves to `M-S-right'.
  18723. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18724. (defvar org-shiftmetaup-hook nil
  18725. "Hook for functions attaching themselves to `M-S-up'.
  18726. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18727. (defvar org-shiftmetadown-hook nil
  18728. "Hook for functions attaching themselves to `M-S-down'.
  18729. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18730. (defvar org-metareturn-hook nil
  18731. "Hook for functions attaching themselves to `M-RET'.
  18732. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18733. (defvar org-shiftup-hook nil
  18734. "Hook for functions attaching themselves to `S-up'.
  18735. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18736. (defvar org-shiftup-final-hook nil
  18737. "Hook for functions attaching themselves to `S-up'.
  18738. This one runs after all other options except shift-select have been excluded.
  18739. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18740. (defvar org-shiftdown-hook nil
  18741. "Hook for functions attaching themselves to `S-down'.
  18742. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18743. (defvar org-shiftdown-final-hook nil
  18744. "Hook for functions attaching themselves to `S-down'.
  18745. This one runs after all other options except shift-select have been excluded.
  18746. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18747. (defvar org-shiftleft-hook nil
  18748. "Hook for functions attaching themselves to `S-left'.
  18749. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18750. (defvar org-shiftleft-final-hook nil
  18751. "Hook for functions attaching themselves to `S-left'.
  18752. This one runs after all other options except shift-select have been excluded.
  18753. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18754. (defvar org-shiftright-hook nil
  18755. "Hook for functions attaching themselves to `S-right'.
  18756. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18757. (defvar org-shiftright-final-hook nil
  18758. "Hook for functions attaching themselves to `S-right'.
  18759. This one runs after all other options except shift-select have been excluded.
  18760. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18761. (defun org-modifier-cursor-error ()
  18762. "Throw an error, a modified cursor command was applied in wrong context."
  18763. (user-error "This command is active in special context like tables, headlines or items"))
  18764. (defun org-shiftselect-error ()
  18765. "Throw an error because Shift-Cursor command was applied in wrong context."
  18766. (if (and (boundp 'shift-select-mode) shift-select-mode)
  18767. (user-error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
  18768. (user-error "This command works only in special context like headlines or timestamps")))
  18769. (defun org-call-for-shift-select (cmd)
  18770. (let ((this-command-keys-shift-translated t))
  18771. (call-interactively cmd)))
  18772. (defun org-shifttab (&optional arg)
  18773. "Global visibility cycling or move to previous table field.
  18774. Call `org-table-previous-field' within a table.
  18775. When ARG is nil, cycle globally through visibility states.
  18776. When ARG is a numeric prefix, show contents of this level."
  18777. (interactive "P")
  18778. (cond
  18779. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  18780. ((integerp arg)
  18781. (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
  18782. (message "Content view to level: %d" arg)
  18783. (org-content (prefix-numeric-value arg2))
  18784. (org-cycle-show-empty-lines t)
  18785. (setq org-cycle-global-status 'overview)))
  18786. (t (call-interactively 'org-global-cycle))))
  18787. (defun org-shiftmetaleft ()
  18788. "Promote subtree or delete table column.
  18789. Calls `org-promote-subtree', `org-outdent-item-tree', or
  18790. `org-table-delete-column', depending on context. See the
  18791. individual commands for more information."
  18792. (interactive)
  18793. (cond
  18794. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  18795. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  18796. ((org-at-heading-p) (call-interactively 'org-promote-subtree))
  18797. ((if (not (org-region-active-p)) (org-at-item-p)
  18798. (save-excursion (goto-char (region-beginning))
  18799. (org-at-item-p)))
  18800. (call-interactively 'org-outdent-item-tree))
  18801. (t (org-modifier-cursor-error))))
  18802. (defun org-shiftmetaright ()
  18803. "Demote subtree or insert table column.
  18804. Calls `org-demote-subtree', `org-indent-item-tree', or
  18805. `org-table-insert-column', depending on context. See the
  18806. individual commands for more information."
  18807. (interactive)
  18808. (cond
  18809. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  18810. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  18811. ((org-at-heading-p) (call-interactively 'org-demote-subtree))
  18812. ((if (not (org-region-active-p)) (org-at-item-p)
  18813. (save-excursion (goto-char (region-beginning))
  18814. (org-at-item-p)))
  18815. (call-interactively 'org-indent-item-tree))
  18816. (t (org-modifier-cursor-error))))
  18817. (defun org-shiftmetaup (&optional arg)
  18818. "Drag the line at point up.
  18819. In a table, kill the current row.
  18820. On a clock timestamp, update the value of the timestamp like `S-<up>'
  18821. but also adjust the previous clocked item in the clock history.
  18822. Everywhere else, drag the line at point up."
  18823. (interactive "P")
  18824. (cond
  18825. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  18826. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  18827. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  18828. (call-interactively 'org-timestamp-up)))
  18829. (t (call-interactively 'org-drag-line-backward))))
  18830. (defun org-shiftmetadown (&optional arg)
  18831. "Drag the line at point down.
  18832. In a table, insert an empty row at the current line.
  18833. On a clock timestamp, update the value of the timestamp like `S-<down>'
  18834. but also adjust the previous clocked item in the clock history.
  18835. Everywhere else, drag the line at point down."
  18836. (interactive "P")
  18837. (cond
  18838. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  18839. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  18840. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  18841. (call-interactively 'org-timestamp-down)))
  18842. (t (call-interactively 'org-drag-line-forward))))
  18843. (defsubst org-hidden-tree-error ()
  18844. (user-error
  18845. "Hidden subtree, open with TAB or use subtree command M-S-<left>/<right>"))
  18846. (defun org-metaleft (&optional arg)
  18847. "Promote heading, list item at point or move table column left.
  18848. Calls `org-do-promote', `org-outdent-item' or `org-table-move-column',
  18849. depending on context. With no specific context, calls the Emacs
  18850. default `backward-word'. See the individual commands for more
  18851. information.
  18852. This function runs the hook `org-metaleft-hook' as a first step,
  18853. and returns at first non-nil value."
  18854. (interactive "P")
  18855. (cond
  18856. ((run-hook-with-args-until-success 'org-metaleft-hook))
  18857. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  18858. ((org-with-limited-levels
  18859. (or (org-at-heading-p)
  18860. (and (org-region-active-p)
  18861. (save-excursion
  18862. (goto-char (region-beginning))
  18863. (org-at-heading-p)))))
  18864. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  18865. (call-interactively 'org-do-promote))
  18866. ;; At an inline task.
  18867. ((org-at-heading-p)
  18868. (call-interactively 'org-inlinetask-promote))
  18869. ((or (org-at-item-p)
  18870. (and (org-region-active-p)
  18871. (save-excursion
  18872. (goto-char (region-beginning))
  18873. (org-at-item-p))))
  18874. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  18875. (call-interactively 'org-outdent-item))
  18876. (t (call-interactively 'backward-word))))
  18877. (defun org-metaright (&optional arg)
  18878. "Demote heading, list item at point or move table column right.
  18879. In front of a drawer or a block keyword, indent it correctly.
  18880. Calls `org-do-demote', `org-indent-item', `org-table-move-column',
  18881. `org-indnet-drawer' or `org-indent-block' depending on context.
  18882. With no specific context, calls the Emacs default `forward-word'.
  18883. See the individual commands for more information.
  18884. This function runs the hook `org-metaright-hook' as a first step,
  18885. and returns at first non-nil value."
  18886. (interactive "P")
  18887. (cond
  18888. ((run-hook-with-args-until-success 'org-metaright-hook))
  18889. ((org-at-table-p) (call-interactively 'org-table-move-column))
  18890. ((org-at-drawer-p) (call-interactively 'org-indent-drawer))
  18891. ((org-at-block-p) (call-interactively 'org-indent-block))
  18892. ((org-with-limited-levels
  18893. (or (org-at-heading-p)
  18894. (and (org-region-active-p)
  18895. (save-excursion
  18896. (goto-char (region-beginning))
  18897. (org-at-heading-p)))))
  18898. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  18899. (call-interactively 'org-do-demote))
  18900. ;; At an inline task.
  18901. ((org-at-heading-p)
  18902. (call-interactively 'org-inlinetask-demote))
  18903. ((or (org-at-item-p)
  18904. (and (org-region-active-p)
  18905. (save-excursion
  18906. (goto-char (region-beginning))
  18907. (org-at-item-p))))
  18908. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  18909. (call-interactively 'org-indent-item))
  18910. (t (call-interactively 'forward-word))))
  18911. (defun org-check-for-hidden (what)
  18912. "Check if there are hidden headlines/items in the current visual line.
  18913. WHAT can be either `headlines' or `items'. If the current line is
  18914. an outline or item heading and it has a folded subtree below it,
  18915. this function returns t, nil otherwise."
  18916. (let ((re (cond
  18917. ((eq what 'headlines) org-outline-regexp-bol)
  18918. ((eq what 'items) (org-item-beginning-re))
  18919. (t (error "This should not happen"))))
  18920. beg end)
  18921. (save-excursion
  18922. (catch 'exit
  18923. (unless (org-region-active-p)
  18924. (setq beg (point-at-bol))
  18925. (beginning-of-line 2)
  18926. (while (and (not (eobp)) ;; this is like `next-line'
  18927. (get-char-property (1- (point)) 'invisible))
  18928. (beginning-of-line 2))
  18929. (setq end (point))
  18930. (goto-char beg)
  18931. (goto-char (point-at-eol))
  18932. (setq end (max end (point)))
  18933. (while (re-search-forward re end t)
  18934. (when (get-char-property (match-beginning 0) 'invisible)
  18935. (throw 'exit t))))
  18936. nil))))
  18937. (defun org-metaup (&optional arg)
  18938. "Move subtree up or move table row up.
  18939. Calls `org-move-subtree-up' or `org-table-move-row' or
  18940. `org-move-item-up', depending on context. See the individual commands
  18941. for more information."
  18942. (interactive "P")
  18943. (cond
  18944. ((run-hook-with-args-until-success 'org-metaup-hook))
  18945. ((org-region-active-p)
  18946. (let* ((a (min (region-beginning) (region-end)))
  18947. (b (1- (max (region-beginning) (region-end))))
  18948. (c (save-excursion (goto-char a)
  18949. (move-beginning-of-line 0)))
  18950. (d (save-excursion (goto-char a)
  18951. (move-end-of-line 0) (point))))
  18952. (transpose-regions a b c d)
  18953. (goto-char c)))
  18954. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  18955. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  18956. ((org-at-item-p) (call-interactively 'org-move-item-up))
  18957. (t (org-drag-element-backward))))
  18958. (defun org-metadown (&optional arg)
  18959. "Move subtree down or move table row down.
  18960. Calls `org-move-subtree-down' or `org-table-move-row' or
  18961. `org-move-item-down', depending on context. See the individual
  18962. commands for more information."
  18963. (interactive "P")
  18964. (cond
  18965. ((run-hook-with-args-until-success 'org-metadown-hook))
  18966. ((org-region-active-p)
  18967. (let* ((a (min (region-beginning) (region-end)))
  18968. (b (max (region-beginning) (region-end)))
  18969. (c (save-excursion (goto-char b)
  18970. (move-beginning-of-line 1)))
  18971. (d (save-excursion (goto-char b)
  18972. (move-end-of-line 1) (1+ (point)))))
  18973. (transpose-regions a b c d)
  18974. (goto-char d)))
  18975. ((org-at-table-p) (call-interactively 'org-table-move-row))
  18976. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  18977. ((org-at-item-p) (call-interactively 'org-move-item-down))
  18978. (t (org-drag-element-forward))))
  18979. (defun org-shiftup (&optional arg)
  18980. "Increase item in timestamp or increase priority of current headline.
  18981. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  18982. depending on context. See the individual commands for more information."
  18983. (interactive "P")
  18984. (cond
  18985. ((run-hook-with-args-until-success 'org-shiftup-hook))
  18986. ((and org-support-shift-select (org-region-active-p))
  18987. (org-call-for-shift-select 'previous-line))
  18988. ((org-at-timestamp-p t)
  18989. (call-interactively (if org-edit-timestamp-down-means-later
  18990. 'org-timestamp-down 'org-timestamp-up)))
  18991. ((and (not (eq org-support-shift-select 'always))
  18992. org-enable-priority-commands
  18993. (org-at-heading-p))
  18994. (call-interactively 'org-priority-up))
  18995. ((and (not org-support-shift-select) (org-at-item-p))
  18996. (call-interactively 'org-previous-item))
  18997. ((org-clocktable-try-shift 'up arg))
  18998. ((run-hook-with-args-until-success 'org-shiftup-final-hook))
  18999. (org-support-shift-select
  19000. (org-call-for-shift-select 'previous-line))
  19001. (t (org-shiftselect-error))))
  19002. (defun org-shiftdown (&optional arg)
  19003. "Decrease item in timestamp or decrease priority of current headline.
  19004. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  19005. depending on context. See the individual commands for more information."
  19006. (interactive "P")
  19007. (cond
  19008. ((run-hook-with-args-until-success 'org-shiftdown-hook))
  19009. ((and org-support-shift-select (org-region-active-p))
  19010. (org-call-for-shift-select 'next-line))
  19011. ((org-at-timestamp-p t)
  19012. (call-interactively (if org-edit-timestamp-down-means-later
  19013. 'org-timestamp-up 'org-timestamp-down)))
  19014. ((and (not (eq org-support-shift-select 'always))
  19015. org-enable-priority-commands
  19016. (org-at-heading-p))
  19017. (call-interactively 'org-priority-down))
  19018. ((and (not org-support-shift-select) (org-at-item-p))
  19019. (call-interactively 'org-next-item))
  19020. ((org-clocktable-try-shift 'down arg))
  19021. ((run-hook-with-args-until-success 'org-shiftdown-final-hook))
  19022. (org-support-shift-select
  19023. (org-call-for-shift-select 'next-line))
  19024. (t (org-shiftselect-error))))
  19025. (defun org-shiftright (&optional arg)
  19026. "Cycle the thing at point or in the current line, depending on context.
  19027. Depending on context, this does one of the following:
  19028. - switch a timestamp at point one day into the future
  19029. - on a headline, switch to the next TODO keyword.
  19030. - on an item, switch entire list to the next bullet type
  19031. - on a property line, switch to the next allowed value
  19032. - on a clocktable definition line, move time block into the future"
  19033. (interactive "P")
  19034. (cond
  19035. ((run-hook-with-args-until-success 'org-shiftright-hook))
  19036. ((and org-support-shift-select (org-region-active-p))
  19037. (org-call-for-shift-select 'forward-char))
  19038. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  19039. ((and (not (eq org-support-shift-select 'always))
  19040. (org-at-heading-p))
  19041. (let ((org-inhibit-logging
  19042. (not org-treat-S-cursor-todo-selection-as-state-change))
  19043. (org-inhibit-blocking
  19044. (not org-treat-S-cursor-todo-selection-as-state-change)))
  19045. (org-call-with-arg 'org-todo 'right)))
  19046. ((or (and org-support-shift-select
  19047. (not (eq org-support-shift-select 'always))
  19048. (org-at-item-bullet-p))
  19049. (and (not org-support-shift-select) (org-at-item-p)))
  19050. (org-call-with-arg 'org-cycle-list-bullet nil))
  19051. ((and (not (eq org-support-shift-select 'always))
  19052. (org-at-property-p))
  19053. (call-interactively 'org-property-next-allowed-value))
  19054. ((org-clocktable-try-shift 'right arg))
  19055. ((run-hook-with-args-until-success 'org-shiftright-final-hook))
  19056. (org-support-shift-select
  19057. (org-call-for-shift-select 'forward-char))
  19058. (t (org-shiftselect-error))))
  19059. (defun org-shiftleft (&optional arg)
  19060. "Cycle the thing at point or in the current line, depending on context.
  19061. Depending on context, this does one of the following:
  19062. - switch a timestamp at point one day into the past
  19063. - on a headline, switch to the previous TODO keyword.
  19064. - on an item, switch entire list to the previous bullet type
  19065. - on a property line, switch to the previous allowed value
  19066. - on a clocktable definition line, move time block into the past"
  19067. (interactive "P")
  19068. (cond
  19069. ((run-hook-with-args-until-success 'org-shiftleft-hook))
  19070. ((and org-support-shift-select (org-region-active-p))
  19071. (org-call-for-shift-select 'backward-char))
  19072. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  19073. ((and (not (eq org-support-shift-select 'always))
  19074. (org-at-heading-p))
  19075. (let ((org-inhibit-logging
  19076. (not org-treat-S-cursor-todo-selection-as-state-change))
  19077. (org-inhibit-blocking
  19078. (not org-treat-S-cursor-todo-selection-as-state-change)))
  19079. (org-call-with-arg 'org-todo 'left)))
  19080. ((or (and org-support-shift-select
  19081. (not (eq org-support-shift-select 'always))
  19082. (org-at-item-bullet-p))
  19083. (and (not org-support-shift-select) (org-at-item-p)))
  19084. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  19085. ((and (not (eq org-support-shift-select 'always))
  19086. (org-at-property-p))
  19087. (call-interactively 'org-property-previous-allowed-value))
  19088. ((org-clocktable-try-shift 'left arg))
  19089. ((run-hook-with-args-until-success 'org-shiftleft-final-hook))
  19090. (org-support-shift-select
  19091. (org-call-for-shift-select 'backward-char))
  19092. (t (org-shiftselect-error))))
  19093. (defun org-shiftcontrolright ()
  19094. "Switch to next TODO set."
  19095. (interactive)
  19096. (cond
  19097. ((and org-support-shift-select (org-region-active-p))
  19098. (org-call-for-shift-select 'forward-word))
  19099. ((and (not (eq org-support-shift-select 'always))
  19100. (org-at-heading-p))
  19101. (org-call-with-arg 'org-todo 'nextset))
  19102. (org-support-shift-select
  19103. (org-call-for-shift-select 'forward-word))
  19104. (t (org-shiftselect-error))))
  19105. (defun org-shiftcontrolleft ()
  19106. "Switch to previous TODO set."
  19107. (interactive)
  19108. (cond
  19109. ((and org-support-shift-select (org-region-active-p))
  19110. (org-call-for-shift-select 'backward-word))
  19111. ((and (not (eq org-support-shift-select 'always))
  19112. (org-at-heading-p))
  19113. (org-call-with-arg 'org-todo 'previousset))
  19114. (org-support-shift-select
  19115. (org-call-for-shift-select 'backward-word))
  19116. (t (org-shiftselect-error))))
  19117. (defun org-shiftcontrolup (&optional n)
  19118. "Change timestamps synchronously up in CLOCK log lines.
  19119. Optional argument N tells to change by that many units."
  19120. (interactive "P")
  19121. (if (and (org-at-clock-log-p) (org-at-timestamp-p t))
  19122. (let (org-support-shift-select)
  19123. (org-clock-timestamps-up n))
  19124. (user-error "Not at a clock log")))
  19125. (defun org-shiftcontroldown (&optional n)
  19126. "Change timestamps synchronously down in CLOCK log lines.
  19127. Optional argument N tells to change by that many units."
  19128. (interactive "P")
  19129. (if (and (org-at-clock-log-p) (org-at-timestamp-p t))
  19130. (let (org-support-shift-select)
  19131. (org-clock-timestamps-down n))
  19132. (user-error "Not at a clock log")))
  19133. (defun org-increase-number-at-point (&optional inc)
  19134. "Increment the number at point.
  19135. With an optional prefix numeric argument INC, increment using
  19136. this numeric value."
  19137. (interactive "p")
  19138. (if (not (number-at-point))
  19139. (user-error "Not on a number")
  19140. (unless inc (setq inc 1))
  19141. (let ((pos (point))
  19142. (beg (skip-chars-backward "-+^/*0-9eE."))
  19143. (end (skip-chars-forward "-+^/*0-9eE^.")) nap)
  19144. (setq nap (buffer-substring-no-properties
  19145. (+ pos beg) (+ pos beg end)))
  19146. (delete-region (+ pos beg) (+ pos beg end))
  19147. (insert (calc-eval (concat (number-to-string inc) "+" nap))))
  19148. (when (org-at-table-p)
  19149. (org-table-align)
  19150. (org-table-end-of-field 1))))
  19151. (defun org-decrease-number-at-point (&optional inc)
  19152. "Decrement the number at point.
  19153. With an optional prefix numeric argument INC, decrement using
  19154. this numeric value."
  19155. (interactive "p")
  19156. (org-increase-number-at-point (- (or inc 1))))
  19157. (defun org-ctrl-c-ret ()
  19158. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  19159. (interactive)
  19160. (cond
  19161. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  19162. (t (call-interactively 'org-insert-heading))))
  19163. (defun org-find-visible ()
  19164. (let ((s (point)))
  19165. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  19166. (get-char-property s 'invisible)))
  19167. s))
  19168. (defun org-find-invisible ()
  19169. (let ((s (point)))
  19170. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  19171. (not (get-char-property s 'invisible))))
  19172. s))
  19173. (defun org-copy-visible (beg end)
  19174. "Copy the visible parts of the region."
  19175. (interactive "r")
  19176. (let (snippets s)
  19177. (save-excursion
  19178. (save-restriction
  19179. (narrow-to-region beg end)
  19180. (setq s (goto-char (point-min)))
  19181. (while (not (= (point) (point-max)))
  19182. (goto-char (org-find-invisible))
  19183. (push (buffer-substring s (point)) snippets)
  19184. (setq s (goto-char (org-find-visible))))))
  19185. (kill-new (apply 'concat (nreverse snippets)))))
  19186. (defun org-copy-special ()
  19187. "Copy region in table or copy current subtree.
  19188. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  19189. See the individual commands for more information."
  19190. (interactive)
  19191. (call-interactively
  19192. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  19193. (defun org-cut-special ()
  19194. "Cut region in table or cut current subtree.
  19195. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  19196. See the individual commands for more information."
  19197. (interactive)
  19198. (call-interactively
  19199. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  19200. (defun org-paste-special (arg)
  19201. "Paste rectangular region into table, or past subtree relative to level.
  19202. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  19203. See the individual commands for more information."
  19204. (interactive "P")
  19205. (if (org-at-table-p)
  19206. (org-table-paste-rectangle)
  19207. (org-paste-subtree arg)))
  19208. (defsubst org-in-fixed-width-region-p ()
  19209. "Is point in a fixed-width region?"
  19210. (save-match-data
  19211. (eq 'fixed-width (org-element-type (org-element-at-point)))))
  19212. (defun org-edit-special (&optional arg)
  19213. "Call a special editor for the element at point.
  19214. When at a table, call the formula editor with `org-table-edit-formulas'.
  19215. When in a source code block, call `org-edit-src-code'.
  19216. When in a fixed-width region, call `org-edit-fixed-width-region'.
  19217. When in an export block, call `org-edit-export-block'.
  19218. When at an #+INCLUDE keyword, visit the included file.
  19219. When at a footnote reference, call `org-edit-footnote-reference'
  19220. On a link, call `ffap' to visit the link at point.
  19221. Otherwise, return a user error."
  19222. (interactive "P")
  19223. (let ((element (org-element-at-point)))
  19224. (assert (not buffer-read-only) nil
  19225. "Buffer is read-only: %s" (buffer-name))
  19226. (pcase (org-element-type element)
  19227. (`src-block
  19228. (if (not arg) (org-edit-src-code)
  19229. (let* ((info (org-babel-get-src-block-info))
  19230. (lang (nth 0 info))
  19231. (params (nth 2 info))
  19232. (session (cdr (assq :session params))))
  19233. (if (not session) (org-edit-src-code)
  19234. ;; At a src-block with a session and function called with
  19235. ;; an ARG: switch to the buffer related to the inferior
  19236. ;; process.
  19237. (switch-to-buffer
  19238. (funcall (intern (concat "org-babel-prep-session:" lang))
  19239. session params))))))
  19240. (`keyword
  19241. (if (member (org-element-property :key element) '("INCLUDE" "SETUPFILE"))
  19242. (org-open-link-from-string
  19243. (format "[[%s]]"
  19244. (expand-file-name
  19245. (let ((value (org-element-property :value element)))
  19246. (cond ((not (org-string-nw-p value))
  19247. (user-error "No file to edit"))
  19248. ((string-match "\\`\"\\(.*?\\)\"" value)
  19249. (match-string 1 value))
  19250. ((string-match "\\`[^ \t\"]\\S-*" value)
  19251. (match-string 0 value))
  19252. (t (user-error "No valid file specified")))))))
  19253. (user-error "No special environment to edit here")))
  19254. (`table
  19255. (if (eq (org-element-property :type element) 'table.el)
  19256. (org-edit-table.el)
  19257. (call-interactively 'org-table-edit-formulas)))
  19258. ;; Only Org tables contain `table-row' type elements.
  19259. (`table-row (call-interactively 'org-table-edit-formulas))
  19260. (`example-block (org-edit-src-code))
  19261. (`export-block (org-edit-export-block))
  19262. (`fixed-width (org-edit-fixed-width-region))
  19263. (_
  19264. ;; No notable element at point. Though, we may be at a link or
  19265. ;; a footnote reference, which are objects. Thus, scan deeper.
  19266. (let ((context (org-element-context element)))
  19267. (pcase (org-element-type context)
  19268. (`footnote-reference (org-edit-footnote-reference))
  19269. (`inline-src-block (org-edit-inline-src-code))
  19270. (`link (call-interactively #'ffap))
  19271. (_ (user-error "No special environment to edit here"))))))))
  19272. (defvar org-table-coordinate-overlays) ; defined in org-table.el
  19273. (defun org-ctrl-c-ctrl-c (&optional arg)
  19274. "Set tags in headline, or update according to changed information at point.
  19275. This command does many different things, depending on context:
  19276. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  19277. this is what we do.
  19278. - If the cursor is on a statistics cookie, update it.
  19279. - If the cursor is in a headline, prompt for tags and insert them
  19280. into the current line, aligned to `org-tags-column'. When called
  19281. with prefix arg, realign all tags in the current buffer.
  19282. - If the cursor is in one of the special #+KEYWORD lines, this
  19283. triggers scanning the buffer for these lines and updating the
  19284. information.
  19285. - If the cursor is inside a table, realign the table. This command
  19286. works even if the automatic table editor has been turned off.
  19287. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  19288. the entire table.
  19289. - If the cursor is at a footnote reference or definition, jump to
  19290. the corresponding definition or references, respectively.
  19291. - If the cursor is a the beginning of a dynamic block, update it.
  19292. - If the current buffer is a capture buffer, close note and file it.
  19293. - If the cursor is on a <<<target>>>, update radio targets and
  19294. corresponding links in this buffer.
  19295. - If the cursor is on a numbered item in a plain list, renumber the
  19296. ordered list.
  19297. - If the cursor is on a checkbox, toggle it.
  19298. - If the cursor is on a code block, evaluate it. The variable
  19299. `org-confirm-babel-evaluate' can be used to control prompting
  19300. before code block evaluation, by default every code block
  19301. evaluation requires confirmation. Code block evaluation can be
  19302. inhibited by setting `org-babel-no-eval-on-ctrl-c-ctrl-c'."
  19303. (interactive "P")
  19304. (cond
  19305. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  19306. org-occur-highlights)
  19307. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  19308. (org-remove-occur-highlights)
  19309. (message "Temporary highlights/overlays removed from current buffer"))
  19310. ((and (local-variable-p 'org-finish-function (current-buffer))
  19311. (fboundp org-finish-function))
  19312. (funcall org-finish-function))
  19313. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  19314. (t
  19315. (if (save-excursion (beginning-of-line) (looking-at "[ \t]*$"))
  19316. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  19317. (user-error "C-c C-c can do nothing useful at this location"))
  19318. (let* ((context (org-element-context))
  19319. (type (org-element-type context)))
  19320. (case type
  19321. ;; When at a link, act according to the parent instead.
  19322. (link (setq context (org-element-property :parent context))
  19323. (setq type (org-element-type context)))
  19324. ;; Unsupported object types: refer to the first supported
  19325. ;; element or object containing it.
  19326. ((bold code entity export-snippet inline-babel-call inline-src-block
  19327. italic latex-fragment line-break macro strike-through subscript
  19328. superscript underline verbatim)
  19329. (setq context
  19330. (org-element-lineage
  19331. context '(radio-target paragraph verse-block table-cell)))))
  19332. ;; For convenience: at the first line of a paragraph on the
  19333. ;; same line as an item, apply function on that item instead.
  19334. (when (eq type 'paragraph)
  19335. (let ((parent (org-element-property :parent context)))
  19336. (when (and (eq (org-element-type parent) 'item)
  19337. (= (line-beginning-position)
  19338. (org-element-property :begin parent)))
  19339. (setq context parent type 'item))))
  19340. ;; Act according to type of element or object at point.
  19341. (case type
  19342. (clock (org-clock-update-time-maybe))
  19343. (dynamic-block
  19344. (save-excursion
  19345. (goto-char (org-element-property :post-affiliated context))
  19346. (org-update-dblock)))
  19347. (footnote-definition
  19348. (goto-char (org-element-property :post-affiliated context))
  19349. (call-interactively 'org-footnote-action))
  19350. (footnote-reference (call-interactively 'org-footnote-action))
  19351. ((headline inlinetask)
  19352. (save-excursion (goto-char (org-element-property :begin context))
  19353. (call-interactively 'org-set-tags)))
  19354. (item
  19355. ;; At an item: a double C-u set checkbox to "[-]"
  19356. ;; unconditionally, whereas a single one will toggle its
  19357. ;; presence. Without a universal argument, if the item
  19358. ;; has a checkbox, toggle it. Otherwise repair the list.
  19359. (let* ((box (org-element-property :checkbox context))
  19360. (struct (org-element-property :structure context))
  19361. (old-struct (copy-tree struct))
  19362. (parents (org-list-parents-alist struct))
  19363. (prevs (org-list-prevs-alist struct))
  19364. (orderedp (org-not-nil (org-entry-get nil "ORDERED"))))
  19365. (org-list-set-checkbox
  19366. (org-element-property :begin context) struct
  19367. (cond ((equal arg '(16)) "[-]")
  19368. ((and (not box) (equal arg '(4))) "[ ]")
  19369. ((or (not box) (equal arg '(4))) nil)
  19370. ((eq box 'on) "[ ]")
  19371. (t "[X]")))
  19372. ;; Mimic `org-list-write-struct' but with grabbing
  19373. ;; a return value from `org-list-struct-fix-box'.
  19374. (org-list-struct-fix-ind struct parents 2)
  19375. (org-list-struct-fix-item-end struct)
  19376. (org-list-struct-fix-bul struct prevs)
  19377. (org-list-struct-fix-ind struct parents)
  19378. (let ((block-item
  19379. (org-list-struct-fix-box struct parents prevs orderedp)))
  19380. (if (and box (equal struct old-struct))
  19381. (if (equal arg '(16))
  19382. (message "Checkboxes already reset")
  19383. (user-error "Cannot toggle this checkbox: %s"
  19384. (if (eq box 'on)
  19385. "all subitems checked"
  19386. "unchecked subitems")))
  19387. (org-list-struct-apply-struct struct old-struct)
  19388. (org-update-checkbox-count-maybe))
  19389. (when block-item
  19390. (message "Checkboxes were removed due to empty box at line %d"
  19391. (org-current-line block-item))))))
  19392. (keyword
  19393. (let ((org-inhibit-startup-visibility-stuff t)
  19394. (org-startup-align-all-tables nil))
  19395. (when (boundp 'org-table-coordinate-overlays)
  19396. (mapc 'delete-overlay org-table-coordinate-overlays)
  19397. (setq org-table-coordinate-overlays nil))
  19398. (org-save-outline-visibility 'use-markers (org-mode-restart)))
  19399. (message "Local setup has been refreshed"))
  19400. (plain-list
  19401. ;; At a plain list, with a double C-u argument, set
  19402. ;; checkboxes of each item to "[-]", whereas a single one
  19403. ;; will toggle their presence according to the state of the
  19404. ;; first item in the list. Without an argument, repair the
  19405. ;; list.
  19406. (let* ((begin (org-element-property :contents-begin context))
  19407. (beginm (move-marker (make-marker) begin))
  19408. (struct (org-element-property :structure context))
  19409. (old-struct (copy-tree struct))
  19410. (first-box (save-excursion
  19411. (goto-char begin)
  19412. (looking-at org-list-full-item-re)
  19413. (match-string-no-properties 3)))
  19414. (new-box (cond ((equal arg '(16)) "[-]")
  19415. ((equal arg '(4)) (unless first-box "[ ]"))
  19416. ((equal first-box "[X]") "[ ]")
  19417. (t "[X]"))))
  19418. (cond
  19419. (arg
  19420. (mapc (lambda (pos) (org-list-set-checkbox pos struct new-box))
  19421. (org-list-get-all-items
  19422. begin struct (org-list-prevs-alist struct))))
  19423. ((and first-box (eq (point) begin))
  19424. ;; For convenience, when point is at bol on the first
  19425. ;; item of the list and no argument is provided, simply
  19426. ;; toggle checkbox of that item, if any.
  19427. (org-list-set-checkbox begin struct new-box)))
  19428. (org-list-write-struct
  19429. struct (org-list-parents-alist struct) old-struct)
  19430. (org-update-checkbox-count-maybe)
  19431. (save-excursion (goto-char beginm) (org-list-send-list 'maybe))))
  19432. ((property-drawer node-property)
  19433. (call-interactively 'org-property-action))
  19434. ((radio-target target)
  19435. (call-interactively 'org-update-radio-target-regexp))
  19436. (statistics-cookie
  19437. (call-interactively 'org-update-statistics-cookies))
  19438. ((table table-cell table-row)
  19439. ;; At a table, recalculate every field and align it. Also
  19440. ;; send the table if necessary. If the table has
  19441. ;; a `table.el' type, just give up. At a table row or
  19442. ;; cell, maybe recalculate line but always align table.
  19443. (if (eq (org-element-property :type context) 'table.el)
  19444. (message "%s" (substitute-command-keys "\\<org-mode-map>\
  19445. Use \\[org-edit-special] to edit table.el tables"))
  19446. (let ((org-enable-table-editor t))
  19447. (if (or (eq type 'table)
  19448. ;; Check if point is at a TBLFM line.
  19449. (and (eq type 'table-row)
  19450. (= (point) (org-element-property :end context))))
  19451. (save-excursion
  19452. (if (org-at-TBLFM-p)
  19453. (progn (require 'org-table)
  19454. (org-table-calc-current-TBLFM))
  19455. (goto-char (org-element-property :contents-begin context))
  19456. (org-call-with-arg 'org-table-recalculate (or arg t))
  19457. (orgtbl-send-table 'maybe)))
  19458. (org-table-maybe-eval-formula)
  19459. (cond (arg (call-interactively 'org-table-recalculate))
  19460. ((org-table-maybe-recalculate-line))
  19461. (t (org-table-align)))))))
  19462. (timestamp (org-timestamp-change 0 'day))
  19463. (otherwise
  19464. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  19465. (user-error
  19466. "C-c C-c can do nothing useful at this location")))))))))
  19467. (defun org-mode-restart ()
  19468. (interactive)
  19469. (let ((indent-status (org-bound-and-true-p org-indent-mode)))
  19470. (funcall major-mode)
  19471. (hack-local-variables)
  19472. (when (and indent-status (not (org-bound-and-true-p org-indent-mode)))
  19473. (org-indent-mode -1)))
  19474. (message "%s restarted" major-mode))
  19475. (defun org-kill-note-or-show-branches ()
  19476. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  19477. (interactive)
  19478. (if (not org-finish-function)
  19479. (progn
  19480. (outline-hide-subtree)
  19481. (call-interactively 'outline-show-branches))
  19482. (let ((org-note-abort t))
  19483. (funcall org-finish-function))))
  19484. (defun org-delete-indentation (&optional ARG)
  19485. "Join current line to previous and fix whitespace at join.
  19486. If previous line is a headline add to headline title. Otherwise
  19487. the function calls `delete-indentation'.
  19488. With argument, join this line to following line."
  19489. (interactive "*P")
  19490. (if (save-excursion
  19491. (if ARG (beginning-of-line)
  19492. (forward-line -1))
  19493. (looking-at org-complex-heading-regexp))
  19494. ;; At headline.
  19495. (let ((tags-column (when (match-beginning 5)
  19496. (save-excursion (goto-char (match-beginning 5))
  19497. (current-column))))
  19498. (string (concat " " (progn (when ARG (forward-line 1))
  19499. (org-trim (delete-and-extract-region
  19500. (line-beginning-position)
  19501. (line-end-position)))))))
  19502. (unless (bobp) (delete-region (point) (1- (point))))
  19503. (goto-char (or (match-end 4)
  19504. (match-beginning 5)
  19505. (match-end 0)))
  19506. (skip-chars-backward " \t")
  19507. (save-excursion (insert string))
  19508. ;; Adjust alignment of tags.
  19509. (when tags-column
  19510. (org-align-tags-here (if org-auto-align-tags
  19511. org-tags-column
  19512. tags-column))))
  19513. (delete-indentation ARG)))
  19514. (defun org-open-line (n)
  19515. "Insert a new row in tables, call `open-line' elsewhere.
  19516. If `org-special-ctrl-o' is nil, just call `open-line' everywhere."
  19517. (interactive "*p")
  19518. (cond
  19519. ((not org-special-ctrl-o)
  19520. (open-line n))
  19521. ((org-at-table-p)
  19522. (org-table-insert-row))
  19523. (t
  19524. (open-line n))))
  19525. (defun org-return (&optional indent)
  19526. "Goto next table row or insert a newline.
  19527. Calls `org-table-next-row' or `newline', depending on context.
  19528. When optional INDENT argument is non-nil, call
  19529. `newline-and-indent' instead of `newline'.
  19530. When `org-return-follows-link' is non-nil and point is on
  19531. a timestamp or a link, call `org-open-at-point'. However, it
  19532. will not happen if point is in a table or on a \"dead\"
  19533. object (e.g., within a comment). In these case, you need to use
  19534. `org-open-at-point' directly."
  19535. (interactive)
  19536. (let ((context (if org-return-follows-link (org-element-context)
  19537. (org-element-at-point))))
  19538. (cond
  19539. ;; In a table, call `org-table-next-row'.
  19540. ((or (and (eq (org-element-type context) 'table)
  19541. (>= (point) (org-element-property :contents-begin context))
  19542. (< (point) (org-element-property :contents-end context)))
  19543. (org-element-lineage context '(table-row table-cell) t))
  19544. (org-table-justify-field-maybe)
  19545. (call-interactively #'org-table-next-row))
  19546. ;; On a link or a timestamp, call `org-open-at-point' if
  19547. ;; `org-return-follows-link' allows it. Tolerate fuzzy
  19548. ;; locations, e.g., in a comment, as `org-open-at-point'.
  19549. ((and org-return-follows-link
  19550. (or (org-in-regexp org-ts-regexp-both nil t)
  19551. (org-in-regexp org-tsr-regexp-both nil t)
  19552. (org-in-regexp org-any-link-re nil t)))
  19553. (call-interactively #'org-open-at-point))
  19554. ;; Insert newline in heading, but preserve tags.
  19555. ((and (not (bolp))
  19556. (save-excursion (beginning-of-line)
  19557. (looking-at org-complex-heading-regexp)))
  19558. ;; At headline. Split line. However, if point is on keyword,
  19559. ;; priority cookie or tags, do not break any of them: add
  19560. ;; a newline after the headline instead.
  19561. (let ((tags-column (and (match-beginning 5)
  19562. (save-excursion (goto-char (match-beginning 5))
  19563. (current-column))))
  19564. (string
  19565. (when (and (match-end 4)
  19566. (>= (point)
  19567. (or (match-end 3) (match-end 2) (1+ (match-end 1))))
  19568. (<= (point) (match-end 4)))
  19569. (delete-and-extract-region (point) (match-end 4)))))
  19570. (when (and tags-column string) ; Adjust tag alignment.
  19571. (org-align-tags-here
  19572. (if org-auto-align-tags org-tags-column tags-column)))
  19573. (end-of-line)
  19574. (org-show-entry)
  19575. (if indent (newline-and-indent) (newline))
  19576. (when string (save-excursion (insert (org-trim string))))))
  19577. ;; In a list, make sure indenting keeps trailing text within.
  19578. ((and indent
  19579. (not (eolp))
  19580. (org-element-lineage context '(item)))
  19581. (let ((trailing-data
  19582. (delete-and-extract-region (point) (line-end-position))))
  19583. (newline-and-indent)
  19584. (save-excursion (insert trailing-data))))
  19585. (t (if indent (newline-and-indent) (newline))))))
  19586. (defun org-return-indent ()
  19587. "Goto next table row or insert a newline and indent.
  19588. Calls `org-table-next-row' or `newline-and-indent', depending on
  19589. context. See the individual commands for more information."
  19590. (interactive)
  19591. (org-return t))
  19592. (defun org-ctrl-c-star ()
  19593. "Compute table, or change heading status of lines.
  19594. Calls `org-table-recalculate' or `org-toggle-heading',
  19595. depending on context."
  19596. (interactive)
  19597. (cond
  19598. ((org-at-table-p)
  19599. (call-interactively 'org-table-recalculate))
  19600. (t
  19601. ;; Convert all lines in region to list items
  19602. (call-interactively 'org-toggle-heading))))
  19603. (defun org-ctrl-c-minus ()
  19604. "Insert separator line in table or modify bullet status of line.
  19605. Also turns a plain line or a region of lines into list items.
  19606. Calls `org-table-insert-hline', `org-toggle-item', or
  19607. `org-cycle-list-bullet', depending on context."
  19608. (interactive)
  19609. (cond
  19610. ((org-at-table-p)
  19611. (call-interactively 'org-table-insert-hline))
  19612. ((org-region-active-p)
  19613. (call-interactively 'org-toggle-item))
  19614. ((org-in-item-p)
  19615. (call-interactively 'org-cycle-list-bullet))
  19616. (t
  19617. (call-interactively 'org-toggle-item))))
  19618. (defun org-toggle-item (arg)
  19619. "Convert headings or normal lines to items, items to normal lines.
  19620. If there is no active region, only the current line is considered.
  19621. If the first non blank line in the region is a headline, convert
  19622. all headlines to items, shifting text accordingly.
  19623. If it is an item, convert all items to normal lines.
  19624. If it is normal text, change region into a list of items.
  19625. With a prefix argument ARG, change the region in a single item."
  19626. (interactive "P")
  19627. (let ((shift-text
  19628. (function
  19629. ;; Shift text in current section to IND, from point to END.
  19630. ;; The function leaves point to END line.
  19631. (lambda (ind end)
  19632. (let ((min-i 1000) (end (copy-marker end)))
  19633. ;; First determine the minimum indentation (MIN-I) of
  19634. ;; the text.
  19635. (save-excursion
  19636. (catch 'exit
  19637. (while (< (point) end)
  19638. (let ((i (org-get-indentation)))
  19639. (cond
  19640. ;; Skip blank lines and inline tasks.
  19641. ((looking-at "^[ \t]*$"))
  19642. ((looking-at org-outline-regexp-bol))
  19643. ;; We can't find less than 0 indentation.
  19644. ((zerop i) (throw 'exit (setq min-i 0)))
  19645. ((< i min-i) (setq min-i i))))
  19646. (forward-line))))
  19647. ;; Then indent each line so that a line indented to
  19648. ;; MIN-I becomes indented to IND. Ignore blank lines
  19649. ;; and inline tasks in the process.
  19650. (let ((delta (- ind min-i)))
  19651. (while (< (point) end)
  19652. (unless (or (looking-at "^[ \t]*$")
  19653. (looking-at org-outline-regexp-bol))
  19654. (org-indent-line-to (+ (org-get-indentation) delta)))
  19655. (forward-line)))))))
  19656. (skip-blanks
  19657. (function
  19658. ;; Return beginning of first non-blank line, starting from
  19659. ;; line at POS.
  19660. (lambda (pos)
  19661. (save-excursion
  19662. (goto-char pos)
  19663. (skip-chars-forward " \r\t\n")
  19664. (point-at-bol)))))
  19665. beg end)
  19666. ;; Determine boundaries of changes.
  19667. (if (org-region-active-p)
  19668. (setq beg (funcall skip-blanks (region-beginning))
  19669. end (copy-marker (region-end)))
  19670. (setq beg (funcall skip-blanks (point-at-bol))
  19671. end (copy-marker (point-at-eol))))
  19672. ;; Depending on the starting line, choose an action on the text
  19673. ;; between BEG and END.
  19674. (org-with-limited-levels
  19675. (save-excursion
  19676. (goto-char beg)
  19677. (cond
  19678. ;; Case 1. Start at an item: de-itemize. Note that it only
  19679. ;; happens when a region is active: `org-ctrl-c-minus'
  19680. ;; would call `org-cycle-list-bullet' otherwise.
  19681. ((org-at-item-p)
  19682. (while (< (point) end)
  19683. (when (org-at-item-p)
  19684. (skip-chars-forward " \t")
  19685. (delete-region (point) (match-end 0)))
  19686. (forward-line)))
  19687. ;; Case 2. Start at an heading: convert to items.
  19688. ((org-at-heading-p)
  19689. (let* ((bul (org-list-bullet-string "-"))
  19690. (bul-len (length bul))
  19691. (done (org-entry-is-done-p))
  19692. (todo (org-entry-is-todo-p))
  19693. ;; Indentation of the first heading. It should be
  19694. ;; relative to the indentation of its parent, if any.
  19695. (start-ind (save-excursion
  19696. (cond
  19697. ((not org-adapt-indentation) 0)
  19698. ((not (outline-previous-heading)) 0)
  19699. (t (length (match-string 0))))))
  19700. ;; Level of first heading. Further headings will be
  19701. ;; compared to it to determine hierarchy in the list.
  19702. (ref-level (org-reduced-level (org-outline-level))))
  19703. (when (or done todo) (org-todo ""))
  19704. (while (< (point) end)
  19705. (let* ((level (org-reduced-level (org-outline-level)))
  19706. (delta (max 0 (- level ref-level))))
  19707. ;; If current headline is less indented than the first
  19708. ;; one, set it as reference, in order to preserve
  19709. ;; subtrees.
  19710. (when (< level ref-level) (setq ref-level level))
  19711. (replace-match bul t t)
  19712. (org-indent-line-to (+ start-ind (* delta bul-len)))
  19713. (when (or done todo)
  19714. (let* ((struct (org-list-struct))
  19715. (old (copy-tree struct)))
  19716. (org-list-set-checkbox (line-beginning-position)
  19717. struct
  19718. (if done "[X]" "[ ]"))
  19719. (org-list-write-struct struct
  19720. (org-list-parents-alist struct)
  19721. old)))
  19722. ;; Ensure all text down to END (or SECTION-END) belongs
  19723. ;; to the newly created item.
  19724. (let ((section-end (save-excursion
  19725. (or (outline-next-heading) (point)))))
  19726. (forward-line)
  19727. (funcall shift-text
  19728. (+ start-ind (* (1+ delta) bul-len))
  19729. (min end section-end)))))))
  19730. ;; Case 3. Normal line with ARG: make the first line of region
  19731. ;; an item, and shift indentation of others lines to
  19732. ;; set them as item's body.
  19733. (arg (let* ((bul (org-list-bullet-string "-"))
  19734. (bul-len (length bul))
  19735. (ref-ind (org-get-indentation)))
  19736. (skip-chars-forward " \t")
  19737. (insert bul)
  19738. (forward-line)
  19739. (while (< (point) end)
  19740. ;; Ensure that lines less indented than first one
  19741. ;; still get included in item body.
  19742. (funcall shift-text
  19743. (+ ref-ind bul-len)
  19744. (min end (save-excursion (or (outline-next-heading)
  19745. (point)))))
  19746. (forward-line))))
  19747. ;; Case 4. Normal line without ARG: turn each non-item line
  19748. ;; into an item.
  19749. (t
  19750. (while (< (point) end)
  19751. (unless (or (org-at-heading-p) (org-at-item-p))
  19752. (when (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  19753. (replace-match
  19754. (concat "\\1" (org-list-bullet-string "-") "\\2"))))
  19755. (forward-line))))))))
  19756. (defun org-toggle-heading (&optional nstars)
  19757. "Convert headings to normal text, or items or text to headings.
  19758. If there is no active region, only convert the current line.
  19759. With a \\[universal-argument] prefix, convert the whole list at
  19760. point into heading.
  19761. In a region:
  19762. - If the first non blank line is a headline, remove the stars
  19763. from all headlines in the region.
  19764. - If it is a normal line, turn each and every normal line (i.e.,
  19765. not an heading or an item) in the region into headings. If you
  19766. want to convert only the first line of this region, use one
  19767. universal prefix argument.
  19768. - If it is a plain list item, turn all plain list items into headings.
  19769. When converting a line into a heading, the number of stars is chosen
  19770. such that the lines become children of the current entry. However,
  19771. when a numeric prefix argument is given, its value determines the
  19772. number of stars to add."
  19773. (interactive "P")
  19774. (let ((skip-blanks
  19775. (function
  19776. ;; Return beginning of first non-blank line, starting from
  19777. ;; line at POS.
  19778. (lambda (pos)
  19779. (save-excursion
  19780. (goto-char pos)
  19781. (while (org-at-comment-p) (forward-line))
  19782. (skip-chars-forward " \r\t\n")
  19783. (point-at-bol)))))
  19784. beg end toggled)
  19785. ;; Determine boundaries of changes. If a universal prefix has
  19786. ;; been given, put the list in a region. If region ends at a bol,
  19787. ;; do not consider the last line to be in the region.
  19788. (when (and current-prefix-arg (org-at-item-p))
  19789. (when (listp current-prefix-arg) (setq current-prefix-arg 1))
  19790. (org-mark-element))
  19791. (if (org-region-active-p)
  19792. (setq beg (funcall skip-blanks (region-beginning))
  19793. end (copy-marker (save-excursion
  19794. (goto-char (region-end))
  19795. (if (bolp) (point) (point-at-eol)))))
  19796. (setq beg (funcall skip-blanks (point-at-bol))
  19797. end (copy-marker (point-at-eol))))
  19798. ;; Ensure inline tasks don't count as headings.
  19799. (org-with-limited-levels
  19800. (save-excursion
  19801. (goto-char beg)
  19802. (cond
  19803. ;; Case 1. Started at an heading: de-star headings.
  19804. ((org-at-heading-p)
  19805. (while (< (point) end)
  19806. (when (org-at-heading-p t)
  19807. (looking-at org-outline-regexp) (replace-match "")
  19808. (setq toggled t))
  19809. (forward-line)))
  19810. ;; Case 2. Started at an item: change items into headlines.
  19811. ;; One star will be added by `org-list-to-subtree'.
  19812. ((org-at-item-p)
  19813. (while (< (point) end)
  19814. (when (org-at-item-p)
  19815. ;; Pay attention to cases when region ends before list.
  19816. (let* ((struct (org-list-struct))
  19817. (list-end (min (org-list-get-bottom-point struct) (1+ end))))
  19818. (save-restriction
  19819. (narrow-to-region (point) list-end)
  19820. (insert (org-list-to-subtree (org-list-to-lisp t)))))
  19821. (setq toggled t))
  19822. (forward-line)))
  19823. ;; Case 3. Started at normal text: make every line an heading,
  19824. ;; skipping headlines and items.
  19825. (t (let* ((stars
  19826. (make-string
  19827. (if (numberp nstars) nstars (or (org-current-level) 0)) ?*))
  19828. (add-stars
  19829. (cond (nstars "") ; stars from prefix only
  19830. ((equal stars "") "*") ; before first heading
  19831. (org-odd-levels-only "**") ; inside heading, odd
  19832. (t "*"))) ; inside heading, oddeven
  19833. (rpl (concat stars add-stars " "))
  19834. (lend (when (listp nstars) (save-excursion (end-of-line) (point)))))
  19835. (while (< (point) (if (equal nstars '(4)) lend end))
  19836. (when (and (not (or (org-at-heading-p) (org-at-item-p) (org-at-comment-p)))
  19837. (looking-at "\\([ \t]*\\)\\(\\S-\\)"))
  19838. (replace-match (concat rpl (match-string 2))) (setq toggled t))
  19839. (forward-line)))))))
  19840. (unless toggled (message "Cannot toggle heading from here"))))
  19841. (defun org-meta-return (&optional _arg)
  19842. "Insert a new heading or wrap a region in a table.
  19843. Calls `org-insert-heading' or `org-table-wrap-region', depending
  19844. on context. See the individual commands for more information."
  19845. (interactive)
  19846. (org-check-before-invisible-edit 'insert)
  19847. (or (run-hook-with-args-until-success 'org-metareturn-hook)
  19848. (call-interactively (if (org-at-table-p) #'org-table-wrap-region
  19849. #'org-insert-heading))))
  19850. ;;; Menu entries
  19851. (defsubst org-in-subtree-not-table-p ()
  19852. "Are we in a subtree and not in a table?"
  19853. (and (not (org-before-first-heading-p))
  19854. (not (org-at-table-p))))
  19855. ;; Define the Org-mode menus
  19856. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  19857. '("Tbl"
  19858. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  19859. ["Next Field" org-cycle (org-at-table-p)]
  19860. ["Previous Field" org-shifttab (org-at-table-p)]
  19861. ["Next Row" org-return (org-at-table-p)]
  19862. "--"
  19863. ["Blank Field" org-table-blank-field (org-at-table-p)]
  19864. ["Edit Field" org-table-edit-field (org-at-table-p)]
  19865. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  19866. "--"
  19867. ("Column"
  19868. ["Move Column Left" org-metaleft (org-at-table-p)]
  19869. ["Move Column Right" org-metaright (org-at-table-p)]
  19870. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  19871. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  19872. ("Row"
  19873. ["Move Row Up" org-metaup (org-at-table-p)]
  19874. ["Move Row Down" org-metadown (org-at-table-p)]
  19875. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  19876. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  19877. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  19878. "--"
  19879. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  19880. ("Rectangle"
  19881. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  19882. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  19883. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  19884. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  19885. "--"
  19886. ("Calculate"
  19887. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  19888. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  19889. ["Edit Formulas" org-edit-special (org-at-table-p)]
  19890. "--"
  19891. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  19892. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  19893. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  19894. "--"
  19895. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  19896. "--"
  19897. ["Sum Column/Rectangle" org-table-sum
  19898. (or (org-at-table-p) (org-region-active-p))]
  19899. ["Which Column?" org-table-current-column (org-at-table-p)])
  19900. ["Debug Formulas"
  19901. org-table-toggle-formula-debugger
  19902. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  19903. ["Show Col/Row Numbers"
  19904. org-table-toggle-coordinate-overlays
  19905. :style toggle
  19906. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  19907. "--"
  19908. ["Create" org-table-create (and (not (org-at-table-p))
  19909. org-enable-table-editor)]
  19910. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  19911. ["Import from File" org-table-import (not (org-at-table-p))]
  19912. ["Export to File" org-table-export (org-at-table-p)]
  19913. "--"
  19914. ["Create/Convert from/to table.el" org-table-create-with-table.el t]
  19915. "--"
  19916. ("Plot"
  19917. ["Ascii plot" orgtbl-ascii-plot :active (org-at-table-p) :keys "C-c \" a"]
  19918. ["Gnuplot" org-plot/gnuplot :active (org-at-table-p) :keys "C-c \" g"])))
  19919. (easy-menu-define org-org-menu org-mode-map "Org menu"
  19920. '("Org"
  19921. ("Show/Hide"
  19922. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  19923. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  19924. ["Sparse Tree..." org-sparse-tree t]
  19925. ["Reveal Context" org-reveal t]
  19926. ["Show All" outline-show-all t]
  19927. "--"
  19928. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  19929. "--"
  19930. ["New Heading" org-insert-heading t]
  19931. ("Navigate Headings"
  19932. ["Up" outline-up-heading t]
  19933. ["Next" outline-next-visible-heading t]
  19934. ["Previous" outline-previous-visible-heading t]
  19935. ["Next Same Level" outline-forward-same-level t]
  19936. ["Previous Same Level" outline-backward-same-level t]
  19937. "--"
  19938. ["Jump" org-goto t])
  19939. ("Edit Structure"
  19940. ["Refile Subtree" org-refile (org-in-subtree-not-table-p)]
  19941. "--"
  19942. ["Move Subtree Up" org-metaup (org-at-heading-p)]
  19943. ["Move Subtree Down" org-metadown (org-at-heading-p)]
  19944. "--"
  19945. ["Copy Subtree" org-copy-special (org-in-subtree-not-table-p)]
  19946. ["Cut Subtree" org-cut-special (org-in-subtree-not-table-p)]
  19947. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  19948. "--"
  19949. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  19950. "--"
  19951. ["Copy visible text" org-copy-visible t]
  19952. "--"
  19953. ["Promote Heading" org-metaleft (org-in-subtree-not-table-p)]
  19954. ["Promote Subtree" org-shiftmetaleft (org-in-subtree-not-table-p)]
  19955. ["Demote Heading" org-metaright (org-in-subtree-not-table-p)]
  19956. ["Demote Subtree" org-shiftmetaright (org-in-subtree-not-table-p)]
  19957. "--"
  19958. ["Sort Region/Children" org-sort t]
  19959. "--"
  19960. ["Convert to odd levels" org-convert-to-odd-levels t]
  19961. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  19962. ("Editing"
  19963. ["Emphasis..." org-emphasize t]
  19964. ["Edit Source Example" org-edit-special t]
  19965. "--"
  19966. ["Footnote new/jump" org-footnote-action t]
  19967. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  19968. ("Archive"
  19969. ["Archive (default method)" org-archive-subtree-default (org-in-subtree-not-table-p)]
  19970. "--"
  19971. ["Move Subtree to Archive file" org-archive-subtree (org-in-subtree-not-table-p)]
  19972. ["Toggle ARCHIVE tag" org-toggle-archive-tag (org-in-subtree-not-table-p)]
  19973. ["Move subtree to Archive sibling" org-archive-to-archive-sibling (org-in-subtree-not-table-p)]
  19974. )
  19975. "--"
  19976. ("Hyperlinks"
  19977. ["Store Link (Global)" org-store-link t]
  19978. ["Find existing link to here" org-occur-link-in-agenda-files t]
  19979. ["Insert Link" org-insert-link t]
  19980. ["Follow Link" org-open-at-point t]
  19981. "--"
  19982. ["Next link" org-next-link t]
  19983. ["Previous link" org-previous-link t]
  19984. "--"
  19985. ["Descriptive Links"
  19986. org-toggle-link-display
  19987. :style radio
  19988. :selected org-descriptive-links
  19989. ]
  19990. ["Literal Links"
  19991. org-toggle-link-display
  19992. :style radio
  19993. :selected (not org-descriptive-links)])
  19994. "--"
  19995. ("TODO Lists"
  19996. ["TODO/DONE/-" org-todo t]
  19997. ("Select keyword"
  19998. ["Next keyword" org-shiftright (org-at-heading-p)]
  19999. ["Previous keyword" org-shiftleft (org-at-heading-p)]
  20000. ["Complete Keyword" pcomplete (assq :todo-keyword (org-context))]
  20001. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))]
  20002. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))])
  20003. ["Show TODO Tree" org-show-todo-tree :active t :keys "C-c / t"]
  20004. ["Global TODO list" org-todo-list :active t :keys "C-c a t"]
  20005. "--"
  20006. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  20007. :selected org-enforce-todo-dependencies :style toggle :active t]
  20008. "Settings for tree at point"
  20009. ["Do Children sequentially" org-toggle-ordered-property :style radio
  20010. :selected (org-entry-get nil "ORDERED")
  20011. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  20012. ["Do Children parallel" org-toggle-ordered-property :style radio
  20013. :selected (not (org-entry-get nil "ORDERED"))
  20014. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  20015. "--"
  20016. ["Set Priority" org-priority t]
  20017. ["Priority Up" org-shiftup t]
  20018. ["Priority Down" org-shiftdown t]
  20019. "--"
  20020. ["Get news from all feeds" org-feed-update-all t]
  20021. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  20022. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  20023. ("TAGS and Properties"
  20024. ["Set Tags" org-set-tags-command (not (org-before-first-heading-p))]
  20025. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  20026. "--"
  20027. ["Set property" org-set-property (not (org-before-first-heading-p))]
  20028. ["Column view of properties" org-columns t]
  20029. ["Insert Column View DBlock" org-insert-columns-dblock t])
  20030. ("Dates and Scheduling"
  20031. ["Timestamp" org-time-stamp (not (org-before-first-heading-p))]
  20032. ["Timestamp (inactive)" org-time-stamp-inactive (not (org-before-first-heading-p))]
  20033. ("Change Date"
  20034. ["1 Day Later" org-shiftright (org-at-timestamp-p)]
  20035. ["1 Day Earlier" org-shiftleft (org-at-timestamp-p)]
  20036. ["1 ... Later" org-shiftup (org-at-timestamp-p)]
  20037. ["1 ... Earlier" org-shiftdown (org-at-timestamp-p)])
  20038. ["Compute Time Range" org-evaluate-time-range t]
  20039. ["Schedule Item" org-schedule (not (org-before-first-heading-p))]
  20040. ["Deadline" org-deadline (not (org-before-first-heading-p))]
  20041. "--"
  20042. ["Custom time format" org-toggle-time-stamp-overlays
  20043. :style radio :selected org-display-custom-times]
  20044. "--"
  20045. ["Goto Calendar" org-goto-calendar t]
  20046. ["Date from Calendar" org-date-from-calendar t]
  20047. "--"
  20048. ["Start/Restart Timer" org-timer-start t]
  20049. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  20050. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  20051. ["Insert Timer String" org-timer t]
  20052. ["Insert Timer Item" org-timer-item t])
  20053. ("Logging work"
  20054. ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
  20055. ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
  20056. ["Clock out" org-clock-out t]
  20057. ["Clock cancel" org-clock-cancel t]
  20058. "--"
  20059. ["Mark as default task" org-clock-mark-default-task t]
  20060. ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
  20061. ["Goto running clock" org-clock-goto t]
  20062. "--"
  20063. ["Display times" org-clock-display t]
  20064. ["Create clock table" org-clock-report t]
  20065. "--"
  20066. ["Record DONE time"
  20067. (progn (setq org-log-done (not org-log-done))
  20068. (message "Switching to %s will %s record a timestamp"
  20069. (car org-done-keywords)
  20070. (if org-log-done "automatically" "not")))
  20071. :style toggle :selected org-log-done])
  20072. "--"
  20073. ["Agenda Command..." org-agenda t]
  20074. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  20075. ("File List for Agenda")
  20076. ("Special views current file"
  20077. ["TODO Tree" org-show-todo-tree t]
  20078. ["Check Deadlines" org-check-deadlines t]
  20079. ["Timeline" org-timeline t]
  20080. ["Tags/Property tree" org-match-sparse-tree t])
  20081. "--"
  20082. ["Export/Publish..." org-export-dispatch t]
  20083. ("LaTeX"
  20084. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  20085. :selected org-cdlatex-mode]
  20086. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  20087. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  20088. ["Modify math symbol" org-cdlatex-math-modify
  20089. (org-inside-LaTeX-fragment-p)]
  20090. ["Insert citation" org-reftex-citation t]
  20091. "--"
  20092. ["Template for BEAMER" (org-beamer-insert-options-template) t])
  20093. "--"
  20094. ("MobileOrg"
  20095. ["Push Files and Views" org-mobile-push t]
  20096. ["Get Captured and Flagged" org-mobile-pull t]
  20097. ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
  20098. "--"
  20099. ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
  20100. "--"
  20101. ("Documentation"
  20102. ["Show Version" org-version t]
  20103. ["Info Documentation" org-info t])
  20104. ("Customize"
  20105. ["Browse Org Group" org-customize t]
  20106. "--"
  20107. ["Expand This Menu" org-create-customize-menu
  20108. (fboundp 'customize-menu-create)])
  20109. ["Send bug report" org-submit-bug-report t]
  20110. "--"
  20111. ("Refresh/Reload"
  20112. ["Refresh setup current buffer" org-mode-restart t]
  20113. ["Reload Org (after update)" org-reload t]
  20114. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x !"])
  20115. ))
  20116. (defun org-info (&optional node)
  20117. "Read documentation for Org-mode in the info system.
  20118. With optional NODE, go directly to that node."
  20119. (interactive)
  20120. (info (format "(org)%s" (or node ""))))
  20121. ;;;###autoload
  20122. (defun org-submit-bug-report ()
  20123. "Submit a bug report on Org-mode via mail.
  20124. Don't hesitate to report any problems or inaccurate documentation.
  20125. If you don't have setup sending mail from (X)Emacs, please copy the
  20126. output buffer into your mail program, as it gives us important
  20127. information about your Org-mode version and configuration."
  20128. (interactive)
  20129. (require 'reporter)
  20130. (defvar reporter-prompt-for-summary-p)
  20131. (org-load-modules-maybe)
  20132. (org-require-autoloaded-modules)
  20133. (let ((reporter-prompt-for-summary-p "Bug report subject: "))
  20134. (reporter-submit-bug-report
  20135. "emacs-orgmode@gnu.org"
  20136. (org-version nil 'full)
  20137. (let (list)
  20138. (save-window-excursion
  20139. (org-pop-to-buffer-same-window (get-buffer-create "*Warn about privacy*"))
  20140. (delete-other-windows)
  20141. (erase-buffer)
  20142. (insert "You are about to submit a bug report to the Org-mode mailing list.
  20143. We would like to add your full Org-mode and Outline configuration to the
  20144. bug report. This greatly simplifies the work of the maintainer and
  20145. other experts on the mailing list.
  20146. HOWEVER, some variables you have customized may contain private
  20147. information. The names of customers, colleagues, or friends, might
  20148. appear in the form of file names, tags, todo states, or search strings.
  20149. If you answer yes to the prompt, you might want to check and remove
  20150. such private information before sending the email.")
  20151. (add-text-properties (point-min) (point-max) '(face org-warning))
  20152. (when (yes-or-no-p "Include your Org-mode configuration ")
  20153. (mapatoms
  20154. (lambda (v)
  20155. (and (boundp v)
  20156. (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
  20157. (or (and (symbol-value v)
  20158. (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
  20159. (and
  20160. (get v 'custom-type) (get v 'standard-value)
  20161. (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
  20162. (push v list)))))
  20163. (kill-buffer (get-buffer "*Warn about privacy*"))
  20164. list))
  20165. nil nil
  20166. "Remember to cover the basics, that is, what you expected to happen and
  20167. what in fact did happen. You don't know how to make a good report? See
  20168. http://orgmode.org/manual/Feedback.html#Feedback
  20169. Your bug report will be posted to the Org-mode mailing list.
  20170. ------------------------------------------------------------------------")
  20171. (save-excursion
  20172. (when (re-search-backward "^\\(Subject: \\)Org-mode version \\(.*?\\);[ \t]*\\(.*\\)" nil t)
  20173. (replace-match "\\1Bug: \\3 [\\2]")))))
  20174. (defun org-install-agenda-files-menu ()
  20175. (let ((bl (buffer-list)))
  20176. (save-excursion
  20177. (while bl
  20178. (set-buffer (pop bl))
  20179. (when (derived-mode-p 'org-mode) (setq bl nil)))
  20180. (when (derived-mode-p 'org-mode)
  20181. (easy-menu-change
  20182. '("Org") "File List for Agenda"
  20183. (append
  20184. (list
  20185. ["Edit File List" (org-edit-agenda-file-list) t]
  20186. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  20187. ["Remove Current File from List" org-remove-file t]
  20188. ["Cycle through agenda files" org-cycle-agenda-files t]
  20189. ["Occur in all agenda files" org-occur-in-agenda-files t]
  20190. "--")
  20191. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  20192. ;;;; Documentation
  20193. (defun org-require-autoloaded-modules ()
  20194. (interactive)
  20195. (mapc 'require
  20196. '(org-agenda org-archive org-attach org-clock org-colview org-id
  20197. org-table org-timer)))
  20198. ;;;###autoload
  20199. (defun org-reload (&optional uncompiled)
  20200. "Reload all org lisp files.
  20201. With prefix arg UNCOMPILED, load the uncompiled versions."
  20202. (interactive "P")
  20203. (require 'loadhist)
  20204. (let* ((org-dir (org-find-library-dir "org"))
  20205. (contrib-dir (or (org-find-library-dir "org-contribdir") org-dir))
  20206. (feature-re "^\\(org\\|ob\\|ox\\)\\(-.*\\)?")
  20207. (remove-re (mapconcat 'identity
  20208. (mapcar (lambda (f) (concat "^" f "$"))
  20209. (list (if (featurep 'xemacs)
  20210. "org-colview"
  20211. "org-colview-xemacs")
  20212. "org" "org-loaddefs" "org-version"))
  20213. "\\|"))
  20214. (feats (delete-dups
  20215. (mapcar 'file-name-sans-extension
  20216. (mapcar 'file-name-nondirectory
  20217. (delq nil
  20218. (mapcar 'feature-file
  20219. features))))))
  20220. (lfeat (append
  20221. (sort
  20222. (setq feats
  20223. (delq nil (mapcar
  20224. (lambda (f)
  20225. (if (and (string-match feature-re f)
  20226. (not (string-match remove-re f)))
  20227. f nil))
  20228. feats)))
  20229. 'string-lessp)
  20230. (list "org-version" "org")))
  20231. (load-suffixes (when (boundp 'load-suffixes) load-suffixes))
  20232. (load-suffixes (if uncompiled (reverse load-suffixes) load-suffixes))
  20233. load-uncore load-misses)
  20234. (setq load-misses
  20235. (delq 't
  20236. (mapcar (lambda (f)
  20237. (or (org-load-noerror-mustsuffix (concat org-dir f))
  20238. (and (string= org-dir contrib-dir)
  20239. (org-load-noerror-mustsuffix (concat contrib-dir f)))
  20240. (and (org-load-noerror-mustsuffix (concat (org-find-library-dir f) f))
  20241. (add-to-list 'load-uncore f 'append)
  20242. 't)
  20243. f))
  20244. lfeat)))
  20245. (when load-uncore
  20246. (message "The following feature%s found in load-path, please check if that's correct:\n%s"
  20247. (if (> (length load-uncore) 1) "s were" " was") load-uncore))
  20248. (if load-misses
  20249. (message "Some error occurred while reloading Org feature%s\n%s\nPlease check *Messages*!\n%s"
  20250. (if (> (length load-misses) 1) "s" "") load-misses (org-version nil 'full))
  20251. (message "Successfully reloaded Org\n%s" (org-version nil 'full)))))
  20252. ;;;###autoload
  20253. (defun org-customize ()
  20254. "Call the customize function with org as argument."
  20255. (interactive)
  20256. (org-load-modules-maybe)
  20257. (org-require-autoloaded-modules)
  20258. (customize-browse 'org))
  20259. (defun org-create-customize-menu ()
  20260. "Create a full customization menu for Org-mode, insert it into the menu."
  20261. (interactive)
  20262. (org-load-modules-maybe)
  20263. (org-require-autoloaded-modules)
  20264. (if (fboundp 'customize-menu-create)
  20265. (progn
  20266. (easy-menu-change
  20267. '("Org") "Customize"
  20268. `(["Browse Org group" org-customize t]
  20269. "--"
  20270. ,(customize-menu-create 'org)
  20271. ["Set" Custom-set t]
  20272. ["Save" Custom-save t]
  20273. ["Reset to Current" Custom-reset-current t]
  20274. ["Reset to Saved" Custom-reset-saved t]
  20275. ["Reset to Standard Settings" Custom-reset-standard t]))
  20276. (message "\"Org\"-menu now contains full customization menu"))
  20277. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  20278. ;;;; Miscellaneous stuff
  20279. ;;; Generally useful functions
  20280. (defsubst org-get-at-eol (property n)
  20281. "Get text property PROPERTY at the end of line less N characters."
  20282. (get-text-property (- (point-at-eol) n) property))
  20283. (defun org-find-text-property-in-string (prop s)
  20284. "Return the first non-nil value of property PROP in string S."
  20285. (or (get-text-property 0 prop s)
  20286. (get-text-property (or (next-single-property-change 0 prop s) 0)
  20287. prop s)))
  20288. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  20289. "Display the given MESSAGE as a warning."
  20290. (if (fboundp 'display-warning)
  20291. (display-warning 'org message
  20292. (if (featurep 'xemacs) 'warning :warning))
  20293. (let ((buf (get-buffer-create "*Org warnings*")))
  20294. (with-current-buffer buf
  20295. (goto-char (point-max))
  20296. (insert "Warning (Org): " message)
  20297. (unless (bolp)
  20298. (newline)))
  20299. (display-buffer buf)
  20300. (sit-for 0))))
  20301. (defun org-eval (form)
  20302. "Eval FORM and return result."
  20303. (condition-case error
  20304. (eval form)
  20305. (error (format "%%![Error: %s]" error))))
  20306. (defun org-in-clocktable-p ()
  20307. "Check if the cursor is in a clocktable."
  20308. (let ((pos (point)) start)
  20309. (save-excursion
  20310. (end-of-line 1)
  20311. (and (re-search-backward "^[ \t]*#\\+BEGIN:[ \t]+clocktable" nil t)
  20312. (setq start (match-beginning 0))
  20313. (re-search-forward "^[ \t]*#\\+END:.*" nil t)
  20314. (>= (match-end 0) pos)
  20315. start))))
  20316. (defun org-in-verbatim-emphasis ()
  20317. (save-match-data
  20318. (and (org-in-regexp org-emph-re 2)
  20319. (>= (point) (match-beginning 3))
  20320. (<= (point) (match-end 4))
  20321. (member (match-string 3) '("=" "~")))))
  20322. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  20323. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  20324. (if (and marker (marker-buffer marker)
  20325. (buffer-live-p (marker-buffer marker)))
  20326. (progn
  20327. (org-pop-to-buffer-same-window (marker-buffer marker))
  20328. (when (or (> marker (point-max)) (< marker (point-min)))
  20329. (widen))
  20330. (goto-char marker)
  20331. (org-show-context 'org-goto))
  20332. (if bookmark
  20333. (bookmark-jump bookmark)
  20334. (error "Cannot find location"))))
  20335. (defun org-quote-csv-field (s)
  20336. "Quote field for inclusion in CSV material."
  20337. (if (string-match "[\",]" s)
  20338. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  20339. s))
  20340. (defun org-force-self-insert (N)
  20341. "Needed to enforce self-insert under remapping."
  20342. (interactive "p")
  20343. (self-insert-command N))
  20344. (defun org-string-width (s)
  20345. "Compute width of string, ignoring invisible characters.
  20346. This ignores character with invisibility property `org-link', and also
  20347. characters with property `org-cwidth', because these will become invisible
  20348. upon the next fontification round."
  20349. (let (b l)
  20350. (when (or (eq t buffer-invisibility-spec)
  20351. (assq 'org-link buffer-invisibility-spec))
  20352. (while (setq b (text-property-any 0 (length s)
  20353. 'invisible 'org-link s))
  20354. (setq s (concat (substring s 0 b)
  20355. (substring s (or (next-single-property-change
  20356. b 'invisible s)
  20357. (length s)))))))
  20358. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  20359. (setq s (concat (substring s 0 b)
  20360. (substring s (or (next-single-property-change
  20361. b 'org-cwidth s)
  20362. (length s))))))
  20363. (setq l (string-width s) b -1)
  20364. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  20365. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  20366. l))
  20367. (defun org-shorten-string (s maxlength)
  20368. "Shorten string S so tht it is no longer than MAXLENGTH characters.
  20369. If the string is shorter or has length MAXLENGTH, just return the
  20370. original string. If it is longer, the functions finds a space in the
  20371. string, breaks this string off at that locations and adds three dots
  20372. as ellipsis. Including the ellipsis, the string will not be longer
  20373. than MAXLENGTH. If finding a good breaking point in the string does
  20374. not work, the string is just chopped off in the middle of a word
  20375. if necessary."
  20376. (if (<= (length s) maxlength)
  20377. s
  20378. (let* ((n (max (- maxlength 4) 1))
  20379. (re (concat "\\`\\(.\\{1," (int-to-string n) "\\}[^ ]\\)\\([ ]\\|\\'\\)")))
  20380. (if (string-match re s)
  20381. (concat (match-string 1 s) "...")
  20382. (concat (substring s 0 (max (- maxlength 3) 0)) "...")))))
  20383. (defun org-get-indentation (&optional line)
  20384. "Get the indentation of the current line, interpreting tabs.
  20385. When LINE is given, assume it represents a line and compute its indentation."
  20386. (if line
  20387. (when (string-match "^ *" (org-remove-tabs line))
  20388. (match-end 0))
  20389. (save-excursion
  20390. (beginning-of-line 1)
  20391. (skip-chars-forward " \t")
  20392. (current-column))))
  20393. (defun org-get-string-indentation (s)
  20394. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  20395. (let ((n -1) (i 0) (w tab-width) c)
  20396. (catch 'exit
  20397. (while (< (setq n (1+ n)) (length s))
  20398. (setq c (aref s n))
  20399. (cond ((= c ?\ ) (setq i (1+ i)))
  20400. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  20401. (t (throw 'exit t)))))
  20402. i))
  20403. (defun org-remove-tabs (s &optional width)
  20404. "Replace tabulators in S with spaces.
  20405. Assumes that s is a single line, starting in column 0."
  20406. (setq width (or width tab-width))
  20407. (while (string-match "\t" s)
  20408. (setq s (replace-match
  20409. (make-string
  20410. (- (* width (/ (+ (match-beginning 0) width) width))
  20411. (match-beginning 0)) ?\ )
  20412. t t s)))
  20413. s)
  20414. (defun org-fix-indentation (line ind)
  20415. "Fix indentation in LINE.
  20416. IND is a cons cell with target and minimum indentation.
  20417. If the current indentation in LINE is smaller than the minimum,
  20418. leave it alone. If it is larger than ind, set it to the target."
  20419. (let* ((l (org-remove-tabs line))
  20420. (i (org-get-indentation l))
  20421. (i1 (car ind)) (i2 (cdr ind)))
  20422. (when (>= i i2) (setq l (substring line i2)))
  20423. (if (> i1 0)
  20424. (concat (make-string i1 ?\ ) l)
  20425. l)))
  20426. (defun org-remove-indentation (code &optional n)
  20427. "Remove maximum common indentation in string CODE and return it.
  20428. N may optionally be the number of columns to remove. Return CODE
  20429. as-is if removal failed."
  20430. (with-temp-buffer
  20431. (insert code)
  20432. (if (org-do-remove-indentation n) (buffer-string) code)))
  20433. (defun org-do-remove-indentation (&optional n)
  20434. "Remove the maximum common indentation from the buffer.
  20435. When optional argument N is a positive integer, remove exactly
  20436. that much characters from indentation, if possible. Return nil
  20437. if it fails."
  20438. (catch :exit
  20439. (goto-char (point-min))
  20440. ;; Find maximum common indentation, if not specified.
  20441. (let ((n (or n
  20442. (let ((min-ind (point-max)))
  20443. (save-excursion
  20444. (while (re-search-forward "^[ \t]*\\S-" nil t)
  20445. (let ((ind (1- (current-column))))
  20446. (if (zerop ind) (throw :exit nil)
  20447. (setq min-ind (min min-ind ind))))))
  20448. min-ind))))
  20449. (if (zerop n) (throw :exit nil)
  20450. ;; Remove exactly N indentation, but give up if not possible.
  20451. (while (not (eobp))
  20452. (let ((ind (progn (skip-chars-forward " \t") (current-column))))
  20453. (cond ((eolp) (delete-region (line-beginning-position) (point)))
  20454. ((< ind n) (throw :exit nil))
  20455. (t (org-indent-line-to (- ind n))))
  20456. (forward-line)))
  20457. ;; Signal success.
  20458. t))))
  20459. (defun org-fill-template (template alist)
  20460. "Find each %key of ALIST in TEMPLATE and replace it."
  20461. (let ((case-fold-search nil))
  20462. (dolist (entry (sort (copy-sequence alist)
  20463. (lambda (a b) (< (length (car a)) (length (car b))))))
  20464. (setq template
  20465. (replace-regexp-in-string
  20466. (concat "%" (regexp-quote (car entry)))
  20467. (or (cdr entry) "") template t t)))
  20468. template))
  20469. (defun org-base-buffer (buffer)
  20470. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  20471. (if (not buffer)
  20472. buffer
  20473. (or (buffer-base-buffer buffer)
  20474. buffer)))
  20475. (defun org-wrap (string &optional width lines)
  20476. "Wrap string to either a number of lines, or a width in characters.
  20477. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  20478. that costs. If there is a word longer than WIDTH, the text is actually
  20479. wrapped to the length of that word.
  20480. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  20481. many lines, whatever width that takes.
  20482. The return value is a list of lines, without newlines at the end."
  20483. (let* ((words (org-split-string string "[ \t\n]+"))
  20484. (maxword (apply 'max (mapcar 'org-string-width words)))
  20485. w ll)
  20486. (cond (width
  20487. (org-do-wrap words (max maxword width)))
  20488. (lines
  20489. (setq w maxword)
  20490. (setq ll (org-do-wrap words maxword))
  20491. (if (<= (length ll) lines)
  20492. ll
  20493. (setq ll words)
  20494. (while (> (length ll) lines)
  20495. (setq w (1+ w))
  20496. (setq ll (org-do-wrap words w)))
  20497. ll))
  20498. (t (error "Cannot wrap this")))))
  20499. (defun org-do-wrap (words width)
  20500. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  20501. (let (lines line)
  20502. (while words
  20503. (setq line (pop words))
  20504. (while (and words (< (+ (length line) (length (car words))) width))
  20505. (setq line (concat line " " (pop words))))
  20506. (setq lines (push line lines)))
  20507. (nreverse lines)))
  20508. (defun org-split-string (string &optional separators)
  20509. "Splits STRING into substrings at SEPARATORS.
  20510. SEPARATORS is a regular expression.
  20511. No empty strings are returned if there are matches at the beginning
  20512. and end of string."
  20513. ;; FIXME: why not use (split-string STRING SEPARATORS t)?
  20514. (let ((start 0) notfirst list)
  20515. (while (and (string-match (or separators "[ \f\t\n\r\v]+") string
  20516. (if (and notfirst
  20517. (= start (match-beginning 0))
  20518. (< start (length string)))
  20519. (1+ start) start))
  20520. (< (match-beginning 0) (length string)))
  20521. (setq notfirst t)
  20522. (or (eq (match-beginning 0) 0)
  20523. (and (eq (match-beginning 0) (match-end 0))
  20524. (eq (match-beginning 0) start))
  20525. (push (substring string start (match-beginning 0)) list))
  20526. (setq start (match-end 0)))
  20527. (or (eq start (length string))
  20528. (push (substring string start) list))
  20529. (nreverse list)))
  20530. (defun org-quote-vert (s)
  20531. "Replace \"|\" with \"\\vert\"."
  20532. (while (string-match "|" s)
  20533. (setq s (replace-match "\\vert" t t s)))
  20534. s)
  20535. (defun org-uuidgen-p (s)
  20536. "Is S an ID created by UUIDGEN?"
  20537. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  20538. (defun org-in-src-block-p (&optional inside)
  20539. "Whether point is in a code source block.
  20540. When INSIDE is non-nil, don't consider we are within a src block
  20541. when point is at #+BEGIN_SRC or #+END_SRC."
  20542. (let ((case-fold-search t) ov)
  20543. (or (and (eq (get-char-property (point) 'src-block) t))
  20544. (and (not inside)
  20545. (save-match-data
  20546. (save-excursion
  20547. (beginning-of-line)
  20548. (looking-at ".*#\\+\\(begin\\|end\\)_src")))))))
  20549. (defun org-context ()
  20550. "Return a list of contexts of the current cursor position.
  20551. If several contexts apply, all are returned.
  20552. Each context entry is a list with a symbol naming the context, and
  20553. two positions indicating start and end of the context. Possible
  20554. contexts are:
  20555. :headline anywhere in a headline
  20556. :headline-stars on the leading stars in a headline
  20557. :todo-keyword on a TODO keyword (including DONE) in a headline
  20558. :tags on the TAGS in a headline
  20559. :priority on the priority cookie in a headline
  20560. :item on the first line of a plain list item
  20561. :item-bullet on the bullet/number of a plain list item
  20562. :checkbox on the checkbox in a plain list item
  20563. :table in an org-mode table
  20564. :table-special on a special filed in a table
  20565. :table-table in a table.el table
  20566. :clocktable in a clocktable
  20567. :src-block in a source block
  20568. :link on a hyperlink
  20569. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE, COMMENT.
  20570. :target on a <<target>>
  20571. :radio-target on a <<<radio-target>>>
  20572. :latex-fragment on a LaTeX fragment
  20573. :latex-preview on a LaTeX fragment with overlaid preview image
  20574. This function expects the position to be visible because it uses font-lock
  20575. faces as a help to recognize the following contexts: :table-special, :link,
  20576. and :keyword."
  20577. (let* ((f (get-text-property (point) 'face))
  20578. (faces (if (listp f) f (list f)))
  20579. (case-fold-search t)
  20580. (p (point)) clist o)
  20581. ;; First the large context
  20582. (cond
  20583. ((org-at-heading-p t)
  20584. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  20585. (when (progn
  20586. (beginning-of-line 1)
  20587. (looking-at org-todo-line-tags-regexp))
  20588. (push (org-point-in-group p 1 :headline-stars) clist)
  20589. (push (org-point-in-group p 2 :todo-keyword) clist)
  20590. (push (org-point-in-group p 4 :tags) clist))
  20591. (goto-char p)
  20592. (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
  20593. (when (looking-at "\\[#[A-Z0-9]\\]")
  20594. (push (org-point-in-group p 0 :priority) clist)))
  20595. ((org-at-item-p)
  20596. (push (org-point-in-group p 2 :item-bullet) clist)
  20597. (push (list :item (point-at-bol)
  20598. (save-excursion (org-end-of-item) (point)))
  20599. clist)
  20600. (and (org-at-item-checkbox-p)
  20601. (push (org-point-in-group p 0 :checkbox) clist)))
  20602. ((org-at-table-p)
  20603. (push (list :table (org-table-begin) (org-table-end)) clist)
  20604. (when (memq 'org-formula faces)
  20605. (push (list :table-special
  20606. (previous-single-property-change p 'face)
  20607. (next-single-property-change p 'face)) clist)))
  20608. ((org-at-table-p 'any)
  20609. (push (list :table-table) clist)))
  20610. (goto-char p)
  20611. (let ((case-fold-search t))
  20612. ;; New the "medium" contexts: clocktables, source blocks
  20613. (cond ((org-in-clocktable-p)
  20614. (push (list :clocktable
  20615. (and (or (looking-at "[ \t]*\\(#\\+BEGIN: clocktable\\)")
  20616. (re-search-backward "[ \t]*\\(#+BEGIN: clocktable\\)" nil t))
  20617. (match-beginning 1))
  20618. (and (re-search-forward "[ \t]*#\\+END:?" nil t)
  20619. (match-end 0))) clist))
  20620. ((org-in-src-block-p)
  20621. (push (list :src-block
  20622. (and (or (looking-at "[ \t]*\\(#\\+BEGIN_SRC\\)")
  20623. (re-search-backward "[ \t]*\\(#+BEGIN_SRC\\)" nil t))
  20624. (match-beginning 1))
  20625. (and (search-forward "#+END_SRC" nil t)
  20626. (match-beginning 0))) clist))))
  20627. (goto-char p)
  20628. ;; Now the small context
  20629. (cond
  20630. ((org-at-timestamp-p)
  20631. (push (org-point-in-group p 0 :timestamp) clist))
  20632. ((memq 'org-link faces)
  20633. (push (list :link
  20634. (previous-single-property-change p 'face)
  20635. (next-single-property-change p 'face)) clist))
  20636. ((memq 'org-special-keyword faces)
  20637. (push (list :keyword
  20638. (previous-single-property-change p 'face)
  20639. (next-single-property-change p 'face)) clist))
  20640. ((org-at-target-p)
  20641. (push (org-point-in-group p 0 :target) clist)
  20642. (goto-char (1- (match-beginning 0)))
  20643. (when (looking-at org-radio-target-regexp)
  20644. (push (org-point-in-group p 0 :radio-target) clist))
  20645. (goto-char p))
  20646. ((setq o (car (delq nil
  20647. (mapcar
  20648. (lambda (x)
  20649. (when (memq x org-latex-fragment-image-overlays) x))
  20650. (overlays-at (point))))))
  20651. (push (list :latex-fragment
  20652. (overlay-start o) (overlay-end o)) clist)
  20653. (push (list :latex-preview
  20654. (overlay-start o) (overlay-end o)) clist))
  20655. ((org-inside-LaTeX-fragment-p)
  20656. ;; FIXME: positions wrong.
  20657. (push (list :latex-fragment (point) (point)) clist)))
  20658. (setq clist (nreverse (delq nil clist)))
  20659. clist))
  20660. (defun org-in-regexp (regexp &optional nlines visually)
  20661. "Check if point is inside a match of REGEXP.
  20662. Normally only the current line is checked, but you can include
  20663. NLINES extra lines around point into the search. If VISUALLY is
  20664. set, require that the cursor is not after the match but really
  20665. on, so that the block visually is on the match.
  20666. Return nil or a cons cell (BEG . END) where BEG and END are,
  20667. respectively, the positions at the beginning and the end of the
  20668. match."
  20669. (catch :exit
  20670. (let ((pos (point))
  20671. (eol (line-end-position (if nlines (1+ nlines) 1))))
  20672. (save-excursion
  20673. (beginning-of-line (- 1 (or nlines 0)))
  20674. (while (and (re-search-forward regexp eol t)
  20675. (<= (match-beginning 0) pos))
  20676. (let ((end (match-end 0)))
  20677. (when (or (> end pos) (and (= end pos) (not visually)))
  20678. (throw :exit (cons (match-beginning 0) (match-end 0))))))))))
  20679. (define-obsolete-function-alias 'org-at-regexp-p 'org-in-regexp
  20680. "Org mode 8.3")
  20681. (defun org-between-regexps-p (start-re end-re &optional lim-up lim-down)
  20682. "Non-nil when point is between matches of START-RE and END-RE.
  20683. Also return a non-nil value when point is on one of the matches.
  20684. Optional arguments LIM-UP and LIM-DOWN bound the search; they are
  20685. buffer positions. Default values are the positions of headlines
  20686. surrounding the point.
  20687. The functions returns a cons cell whose car (resp. cdr) is the
  20688. position before START-RE (resp. after END-RE)."
  20689. (save-match-data
  20690. (let ((pos (point))
  20691. (limit-up (or lim-up (save-excursion (outline-previous-heading))))
  20692. (limit-down (or lim-down (save-excursion (outline-next-heading))))
  20693. beg end)
  20694. (save-excursion
  20695. ;; Point is on a block when on START-RE or if START-RE can be
  20696. ;; found before it...
  20697. (and (or (org-in-regexp start-re)
  20698. (re-search-backward start-re limit-up t))
  20699. (setq beg (match-beginning 0))
  20700. ;; ... and END-RE after it...
  20701. (goto-char (match-end 0))
  20702. (re-search-forward end-re limit-down t)
  20703. (> (setq end (match-end 0)) pos)
  20704. ;; ... without another START-RE in-between.
  20705. (goto-char (match-beginning 0))
  20706. (not (re-search-backward start-re (1+ beg) t))
  20707. ;; Return value.
  20708. (cons beg end))))))
  20709. (defun org-in-block-p (names)
  20710. "Non-nil when point belongs to a block whose name belongs to NAMES.
  20711. NAMES is a list of strings containing names of blocks.
  20712. Return first block name matched, or nil. Beware that in case of
  20713. nested blocks, the returned name may not belong to the closest
  20714. block from point."
  20715. (save-match-data
  20716. (catch 'exit
  20717. (let ((case-fold-search t)
  20718. (lim-up (save-excursion (outline-previous-heading)))
  20719. (lim-down (save-excursion (outline-next-heading))))
  20720. (mapc (lambda (name)
  20721. (let ((n (regexp-quote name)))
  20722. (when (org-between-regexps-p
  20723. (concat "^[ \t]*#\\+begin_" n)
  20724. (concat "^[ \t]*#\\+end_" n)
  20725. lim-up lim-down)
  20726. (throw 'exit n))))
  20727. names))
  20728. nil)))
  20729. (defun org-occur-in-agenda-files (regexp &optional _nlines)
  20730. "Call `multi-occur' with buffers for all agenda files."
  20731. (interactive "sOrg-files matching: ")
  20732. (let* ((files (org-agenda-files))
  20733. (tnames (mapcar #'file-truename files))
  20734. (extra org-agenda-text-search-extra-files))
  20735. (when (eq (car extra) 'agenda-archives)
  20736. (setq extra (cdr extra))
  20737. (setq files (org-add-archive-files files)))
  20738. (dolist (f extra)
  20739. (unless (member (file-truename f) tnames)
  20740. (unless (member f files) (setq files (append files (list f))))
  20741. (setq tnames (append tnames (list (file-truename f))))))
  20742. (multi-occur
  20743. (mapcar (lambda (x)
  20744. (with-current-buffer
  20745. ;; FIXME: Why not just (find-file-noselect x)?
  20746. ;; Is it to avoid the "revert buffer" prompt?
  20747. (or (get-file-buffer x) (find-file-noselect x))
  20748. (widen)
  20749. (current-buffer)))
  20750. files)
  20751. regexp)))
  20752. (if (boundp 'occur-mode-find-occurrence-hook)
  20753. ;; Emacs 23
  20754. (add-hook 'occur-mode-find-occurrence-hook
  20755. (lambda ()
  20756. (when (derived-mode-p 'org-mode)
  20757. (org-reveal))))
  20758. ;; Emacs 22
  20759. (defadvice occur-mode-goto-occurrence
  20760. (after org-occur-reveal activate)
  20761. (and (derived-mode-p 'org-mode) (org-reveal)))
  20762. (defadvice occur-mode-goto-occurrence-other-window
  20763. (after org-occur-reveal activate)
  20764. (and (derived-mode-p 'org-mode) (org-reveal)))
  20765. (defadvice occur-mode-display-occurrence
  20766. (after org-occur-reveal activate)
  20767. (when (derived-mode-p 'org-mode)
  20768. (let ((pos (occur-mode-find-occurrence)))
  20769. (with-current-buffer (marker-buffer pos)
  20770. (save-excursion
  20771. (goto-char pos)
  20772. (org-reveal)))))))
  20773. (defun org-occur-link-in-agenda-files ()
  20774. "Create a link and search for it in the agendas.
  20775. The link is not stored in `org-stored-links', it is just created
  20776. for the search purpose."
  20777. (interactive)
  20778. (let ((link (condition-case nil
  20779. (org-store-link nil)
  20780. (error "Unable to create a link to here"))))
  20781. (org-occur-in-agenda-files (regexp-quote link))))
  20782. (defun org-reverse-string (string)
  20783. "Return the reverse of STRING."
  20784. (apply 'string (reverse (string-to-list string))))
  20785. ;; defsubst org-uniquify must be defined before first use
  20786. (defun org-uniquify-alist (alist)
  20787. "Merge elements of ALIST with the same key.
  20788. For example, in this alist:
  20789. \(org-uniquify-alist \\='((a 1) (b 2) (a 3)))
  20790. => \\='((a 1 3) (b 2))
  20791. merge (a 1) and (a 3) into (a 1 3).
  20792. The function returns the new ALIST."
  20793. (let (rtn)
  20794. (mapc
  20795. (lambda (e)
  20796. (let (n)
  20797. (if (not (assoc (car e) rtn))
  20798. (push e rtn)
  20799. (setq n (cons (car e) (append (cdr (assoc (car e) rtn)) (cdr e))))
  20800. (setq rtn (assq-delete-all (car e) rtn))
  20801. (push n rtn))))
  20802. alist)
  20803. rtn))
  20804. (defun org-delete-all (elts list)
  20805. "Remove all elements in ELTS from LIST."
  20806. (while elts
  20807. (setq list (delete (pop elts) list)))
  20808. list)
  20809. (defun org-back-over-empty-lines ()
  20810. "Move backwards over whitespace, to the beginning of the first empty line.
  20811. Returns the number of empty lines passed."
  20812. (let ((pos (point)))
  20813. (if (cdr (assoc 'heading org-blank-before-new-entry))
  20814. (skip-chars-backward " \t\n\r")
  20815. (unless (eobp)
  20816. (forward-line -1)))
  20817. (beginning-of-line 2)
  20818. (goto-char (min (point) pos))
  20819. (count-lines (point) pos)))
  20820. (defun org-skip-whitespace ()
  20821. (skip-chars-forward " \t\n\r"))
  20822. (defun org-point-in-group (point group &optional context)
  20823. "Check if POINT is in match-group GROUP.
  20824. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  20825. match. If the match group does not exist or point is not inside it,
  20826. return nil."
  20827. (and (match-beginning group)
  20828. (>= point (match-beginning group))
  20829. (<= point (match-end group))
  20830. (if context
  20831. (list context (match-beginning group) (match-end group))
  20832. t)))
  20833. (defun org-switch-to-buffer-other-window (&rest args)
  20834. "Switch to buffer in a second window on the current frame.
  20835. In particular, do not allow pop-up frames.
  20836. Returns the newly created buffer."
  20837. (org-no-popups
  20838. (apply 'switch-to-buffer-other-window args)))
  20839. (defun org-combine-plists (&rest plists)
  20840. "Create a single property list from all plists in PLISTS.
  20841. The process starts by copying the first list, and then setting properties
  20842. from the other lists. Settings in the last list are the most significant
  20843. ones and overrule settings in the other lists."
  20844. (let ((rtn (copy-sequence (pop plists)))
  20845. p v ls)
  20846. (while plists
  20847. (setq ls (pop plists))
  20848. (while ls
  20849. (setq p (pop ls) v (pop ls))
  20850. (setq rtn (plist-put rtn p v))))
  20851. rtn))
  20852. (defun org-replace-escapes (string table)
  20853. "Replace %-escapes in STRING with values in TABLE.
  20854. TABLE is an association list with keys like \"%a\" and string values.
  20855. The sequences in STRING may contain normal field width and padding information,
  20856. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  20857. so values can contain further %-escapes if they are define later in TABLE."
  20858. (let ((tbl (copy-alist table))
  20859. (case-fold-search nil)
  20860. (pchg 0)
  20861. e re rpl)
  20862. (dolist (e tbl)
  20863. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  20864. (when (and (cdr e) (string-match re (cdr e)))
  20865. (let ((sref (substring (cdr e) (match-beginning 0) (match-end 0)))
  20866. (safe "SREF"))
  20867. (add-text-properties 0 3 (list 'sref sref) safe)
  20868. (setcdr e (replace-match safe t t (cdr e)))))
  20869. (while (string-match re string)
  20870. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  20871. (cdr e)))
  20872. (setq string (replace-match rpl t t string))))
  20873. (while (setq pchg (next-property-change pchg string))
  20874. (let ((sref (get-text-property pchg 'sref string)))
  20875. (when (and sref (string-match "SREF" string pchg))
  20876. (setq string (replace-match sref t t string)))))
  20877. string))
  20878. (defun org-sublist (list start end)
  20879. "Return a section of LIST, from START to END.
  20880. Counting starts at 1."
  20881. (cl-subseq list (1- start) end))
  20882. (make-obsolete 'org-sublist "cl-subseq (note the 0-based counting)." "Org 9.0")
  20883. (defun org-find-base-buffer-visiting (file)
  20884. "Like `find-buffer-visiting' but always return the base buffer and
  20885. not an indirect buffer."
  20886. (let ((buf (or (get-file-buffer file)
  20887. (find-buffer-visiting file))))
  20888. (if buf
  20889. (or (buffer-base-buffer buf) buf)
  20890. nil)))
  20891. (define-obsolete-function-alias 'org-image-file-name-regexp 'image-file-name-regexp
  20892. "Org 9.0")
  20893. ;;; TODO: Only called once, from ox-odt which should probably use
  20894. ;;; org-export-inline-image-p or something.
  20895. (defun org-file-image-p (file)
  20896. "Return non-nil if FILE is an image."
  20897. (save-match-data
  20898. (string-match (image-file-name-regexp) file)))
  20899. (defun org-get-cursor-date (&optional with-time)
  20900. "Return the date at cursor in as a time.
  20901. This works in the calendar and in the agenda, anywhere else it just
  20902. returns the current time.
  20903. If WITH-TIME is non-nil, returns the time of the event at point (in
  20904. the agenda) or the current time of the day."
  20905. (let (date day defd tp hod mod)
  20906. (when with-time
  20907. (setq tp (get-text-property (point) 'time))
  20908. (when (and tp (string-match "\\([0-9][0-9]\\):\\([0-9][0-9]\\)" tp))
  20909. (setq hod (string-to-number (match-string 1 tp))
  20910. mod (string-to-number (match-string 2 tp))))
  20911. (or tp (let ((now (decode-time)))
  20912. (setq hod (nth 2 now)
  20913. mod (nth 1 now)))))
  20914. (cond
  20915. ((eq major-mode 'calendar-mode)
  20916. (setq date (calendar-cursor-to-date)
  20917. defd (encode-time 0 (or mod 0) (or hod 0)
  20918. (nth 1 date) (nth 0 date) (nth 2 date))))
  20919. ((eq major-mode 'org-agenda-mode)
  20920. (setq day (get-text-property (point) 'day))
  20921. (when day
  20922. (setq date (calendar-gregorian-from-absolute day)
  20923. defd (encode-time 0 (or mod 0) (or hod 0)
  20924. (nth 1 date) (nth 0 date) (nth 2 date))))))
  20925. (or defd (current-time))))
  20926. (defun org-mark-subtree (&optional up)
  20927. "Mark the current subtree.
  20928. This puts point at the start of the current subtree, and mark at
  20929. the end. If a numeric prefix UP is given, move up into the
  20930. hierarchy of headlines by UP levels before marking the subtree."
  20931. (interactive "P")
  20932. (org-with-limited-levels
  20933. (cond ((org-at-heading-p) (beginning-of-line))
  20934. ((org-before-first-heading-p) (user-error "Not in a subtree"))
  20935. (t (outline-previous-visible-heading 1))))
  20936. (when up (while (and (> up 0) (org-up-heading-safe)) (decf up)))
  20937. (if (org-called-interactively-p 'any)
  20938. (call-interactively 'org-mark-element)
  20939. (org-mark-element)))
  20940. ;;; Indentation
  20941. (defun org--get-expected-indentation (element contentsp)
  20942. "Expected indentation column for current line, according to ELEMENT.
  20943. ELEMENT is an element containing point. CONTENTSP is non-nil
  20944. when indentation is to be computed according to contents of
  20945. ELEMENT."
  20946. (let ((type (org-element-type element))
  20947. (start (org-element-property :begin element))
  20948. (post-affiliated (org-element-property :post-affiliated element)))
  20949. (org-with-wide-buffer
  20950. (cond
  20951. (contentsp
  20952. (case type
  20953. ((diary-sexp footnote-definition) 0)
  20954. ((headline inlinetask nil)
  20955. (if (not org-adapt-indentation) 0
  20956. (let ((level (org-current-level)))
  20957. (if level (1+ level) 0))))
  20958. ((item plain-list) (org-list-item-body-column post-affiliated))
  20959. (t
  20960. (goto-char start)
  20961. (org-get-indentation))))
  20962. ((memq type '(headline inlinetask nil))
  20963. (if (save-excursion (beginning-of-line) (looking-at "[ \t]*$"))
  20964. (org--get-expected-indentation element t)
  20965. 0))
  20966. ((memq type '(diary-sexp footnote-definition)) 0)
  20967. ;; First paragraph of a footnote definition or an item.
  20968. ;; Indent like parent.
  20969. ((< (line-beginning-position) start)
  20970. (org--get-expected-indentation
  20971. (org-element-property :parent element) t))
  20972. ;; At first line: indent according to previous sibling, if any,
  20973. ;; ignoring footnote definitions and inline tasks, or parent's
  20974. ;; contents.
  20975. ((= (line-beginning-position) start)
  20976. (catch 'exit
  20977. (while t
  20978. (if (= (point-min) start) (throw 'exit 0)
  20979. (goto-char (1- start))
  20980. (let* ((previous (org-element-at-point))
  20981. (parent previous))
  20982. (while (and parent (<= (org-element-property :end parent) start))
  20983. (setq previous parent
  20984. parent (org-element-property :parent parent)))
  20985. (cond
  20986. ((not previous) (throw 'exit 0))
  20987. ((> (org-element-property :end previous) start)
  20988. (throw 'exit (org--get-expected-indentation previous t)))
  20989. ((memq (org-element-type previous)
  20990. '(footnote-definition inlinetask))
  20991. (setq start (org-element-property :begin previous)))
  20992. (t (goto-char (org-element-property :begin previous))
  20993. (throw 'exit
  20994. (if (bolp) (org-get-indentation)
  20995. ;; At first paragraph in an item or
  20996. ;; a footnote definition.
  20997. (org--get-expected-indentation
  20998. (org-element-property :parent previous) t))))))))))
  20999. ;; Otherwise, move to the first non-blank line above.
  21000. (t
  21001. (beginning-of-line)
  21002. (let ((pos (point)))
  21003. (skip-chars-backward " \r\t\n")
  21004. (cond
  21005. ;; Two blank lines end a footnote definition or a plain
  21006. ;; list. When we indent an empty line after them, the
  21007. ;; containing list or footnote definition is over, so it
  21008. ;; qualifies as a previous sibling. Therefore, we indent
  21009. ;; like its first line.
  21010. ((and (memq type '(footnote-definition plain-list))
  21011. (> (count-lines (point) pos) 2))
  21012. (goto-char start)
  21013. (org-get-indentation))
  21014. ;; Line above is the first one of a paragraph at the
  21015. ;; beginning of an item or a footnote definition. Indent
  21016. ;; like parent.
  21017. ((< (line-beginning-position) start)
  21018. (org--get-expected-indentation
  21019. (org-element-property :parent element) t))
  21020. ;; Line above is the beginning of an element, i.e., point
  21021. ;; was originally on the blank lines between element's start
  21022. ;; and contents.
  21023. ((= (line-beginning-position) post-affiliated)
  21024. (org--get-expected-indentation element t))
  21025. ;; POS is after contents in a greater element. Indent like
  21026. ;; the beginning of the element.
  21027. ;;
  21028. ;; As a special case, if point is at the end of a footnote
  21029. ;; definition or an item, indent like the very last element
  21030. ;; within.
  21031. ((and (not (eq type 'paragraph))
  21032. (let ((cend (org-element-property :contents-end element)))
  21033. (and cend (<= cend pos))))
  21034. (if (memq type '(footnote-definition item plain-list))
  21035. (let ((last (org-element-at-point)))
  21036. (org--get-expected-indentation
  21037. last
  21038. (memq (org-element-type last)
  21039. '(footnote-definition item plain-list))))
  21040. (goto-char start)
  21041. (org-get-indentation)))
  21042. ;; In any other case, indent like the current line.
  21043. (t (org-get-indentation)))))))))
  21044. (defun org--align-node-property ()
  21045. "Align node property at point.
  21046. Alignment is done according to `org-property-format', which see."
  21047. (when (save-excursion
  21048. (beginning-of-line)
  21049. (looking-at org-property-re))
  21050. (replace-match
  21051. (concat (match-string 4)
  21052. (org-trim
  21053. (format org-property-format (match-string 1) (match-string 3))))
  21054. t t)))
  21055. (defun org-indent-line ()
  21056. "Indent line depending on context.
  21057. Indentation is done according to the following rules:
  21058. - Footnote definitions, diary sexps, headlines and inline tasks
  21059. have to start at column 0.
  21060. - On the very first line of an element, consider, in order, the
  21061. next rules until one matches:
  21062. 1. If there's a sibling element before, ignoring footnote
  21063. definitions and inline tasks, indent like its first line.
  21064. 2. If element has a parent, indent like its contents. More
  21065. precisely, if parent is an item, indent after the
  21066. description part, if any, or the bullet (see
  21067. `org-list-description-max-indent'). Else, indent like
  21068. parent's first line.
  21069. 3. Otherwise, indent relatively to current level, if
  21070. `org-adapt-indentation' is non-nil, or to left margin.
  21071. - On a blank line at the end of an element, indent according to
  21072. the type of the element. More precisely
  21073. 1. If element is a plain list, an item, or a footnote
  21074. definition, indent like the very last element within.
  21075. 2. If element is a paragraph, indent like its last non blank
  21076. line.
  21077. 3. Otherwise, indent like its very first line.
  21078. - In the code part of a source block, use language major mode
  21079. to indent current line if `org-src-tab-acts-natively' is
  21080. non-nil. If it is nil, do nothing.
  21081. - Otherwise, indent like the first non-blank line above.
  21082. The function doesn't indent an item as it could break the whole
  21083. list structure. Instead, use \\<org-mode-map>\\[org-shiftmetaleft] or \
  21084. \\[org-shiftmetaright].
  21085. Also align node properties according to `org-property-format'."
  21086. (interactive)
  21087. (cond
  21088. (orgstruct-is-++
  21089. (let ((indent-line-function
  21090. (cadadr (assq 'indent-line-function org-fb-vars))))
  21091. (indent-according-to-mode)))
  21092. ((org-at-heading-p) 'noindent)
  21093. (t
  21094. (let* ((element (save-excursion (beginning-of-line) (org-element-at-point)))
  21095. (type (org-element-type element)))
  21096. (cond ((and (memq type '(plain-list item))
  21097. (= (line-beginning-position)
  21098. (org-element-property :post-affiliated element)))
  21099. 'noindent)
  21100. ((and (eq type 'src-block)
  21101. org-src-tab-acts-natively
  21102. (> (line-beginning-position)
  21103. (org-element-property :post-affiliated element))
  21104. (< (line-beginning-position)
  21105. (org-with-wide-buffer
  21106. (goto-char (org-element-property :end element))
  21107. (skip-chars-backward " \r\t\n")
  21108. (line-beginning-position))))
  21109. (org-babel-do-key-sequence-in-edit-buffer (kbd "TAB")))
  21110. (t
  21111. (let ((column (org--get-expected-indentation element nil)))
  21112. ;; Preserve current column.
  21113. (if (<= (current-column) (current-indentation))
  21114. (org-indent-line-to column)
  21115. (save-excursion (org-indent-line-to column))))
  21116. ;; Align node property. Also preserve current column.
  21117. (when (eq type 'node-property)
  21118. (let ((column (current-column)))
  21119. (org--align-node-property)
  21120. (org-move-to-column column)))))))))
  21121. (defun org-indent-region (start end)
  21122. "Indent each non-blank line in the region.
  21123. Called from a program, START and END specify the region to
  21124. indent. The function will not indent contents of example blocks,
  21125. verse blocks and export blocks as leading white spaces are
  21126. assumed to be significant there."
  21127. (interactive "r")
  21128. (save-excursion
  21129. (goto-char start)
  21130. (skip-chars-forward " \r\t\n")
  21131. (unless (eobp) (beginning-of-line))
  21132. (let ((indent-to
  21133. (lambda (ind pos)
  21134. ;; Set IND as indentation for all lines between point and
  21135. ;; POS or END, whichever comes first. Blank lines are
  21136. ;; ignored. Leave point after POS once done.
  21137. (let ((limit (copy-marker (min end pos))))
  21138. (while (< (point) limit)
  21139. (unless (org-looking-at-p "[ \t]*$") (org-indent-line-to ind))
  21140. (forward-line))
  21141. (set-marker limit nil))))
  21142. (end (copy-marker end)))
  21143. (while (< (point) end)
  21144. (if (or (org-looking-at-p " \r\t\n") (org-at-heading-p)) (forward-line)
  21145. (let* ((element (org-element-at-point))
  21146. (type (org-element-type element))
  21147. (element-end (copy-marker (org-element-property :end element)))
  21148. (ind (org--get-expected-indentation element nil)))
  21149. (cond
  21150. ((or (memq type '(paragraph table table-row))
  21151. (not (or (org-element-property :contents-begin element)
  21152. (memq type
  21153. '(example-block export-block src-block)))))
  21154. ;; Elements here are indented as a single block. Also
  21155. ;; align node properties.
  21156. (when (eq type 'node-property)
  21157. (org--align-node-property)
  21158. (beginning-of-line))
  21159. (funcall indent-to ind element-end))
  21160. (t
  21161. ;; Elements in this category consist of three parts:
  21162. ;; before the contents, the contents, and after the
  21163. ;; contents. The contents are treated specially,
  21164. ;; according to the element type, or not indented at
  21165. ;; all. Other parts are indented as a single block.
  21166. (let* ((post (copy-marker
  21167. (org-element-property :post-affiliated element)))
  21168. (cbeg
  21169. (copy-marker
  21170. (cond
  21171. ((not (org-element-property :contents-begin element))
  21172. ;; Fake contents for source blocks.
  21173. (org-with-wide-buffer
  21174. (goto-char post)
  21175. (forward-line)
  21176. (point)))
  21177. ((memq type '(footnote-definition item plain-list))
  21178. ;; Contents in these elements could start on
  21179. ;; the same line as the beginning of the
  21180. ;; element. Make sure we start indenting
  21181. ;; from the second line.
  21182. (org-with-wide-buffer
  21183. (goto-char post)
  21184. (end-of-line)
  21185. (skip-chars-forward " \r\t\n")
  21186. (if (eobp) (point) (line-beginning-position))))
  21187. (t (org-element-property :contents-begin element)))))
  21188. (cend (copy-marker
  21189. (or (org-element-property :contents-end element)
  21190. ;; Fake contents for source blocks.
  21191. (org-with-wide-buffer
  21192. (goto-char element-end)
  21193. (skip-chars-backward " \r\t\n")
  21194. (line-beginning-position)))
  21195. t)))
  21196. ;; Do not change items indentation individually as it
  21197. ;; might break the list as a whole. On the other
  21198. ;; hand, when at a plain list, indent it as a whole.
  21199. (cond ((eq type 'plain-list)
  21200. (let ((offset (- ind (org-get-indentation))))
  21201. (unless (zerop offset)
  21202. (indent-rigidly (org-element-property :begin element)
  21203. (org-element-property :end element)
  21204. offset))
  21205. (goto-char cbeg)))
  21206. ((eq type 'item) (goto-char cbeg))
  21207. (t (funcall indent-to ind cbeg)))
  21208. (when (< (point) end)
  21209. (case type
  21210. ((example-block export-block verse-block))
  21211. (src-block
  21212. ;; In a source block, indent source code
  21213. ;; according to language major mode, but only if
  21214. ;; `org-src-tab-acts-natively' is non-nil.
  21215. (when (and (< (point) end) org-src-tab-acts-natively)
  21216. (ignore-errors
  21217. (org-babel-do-in-edit-buffer
  21218. (indent-region (point-min) (point-max))))))
  21219. (t (org-indent-region (point) (min cend end))))
  21220. (goto-char (min cend end))
  21221. (when (< (point) end) (funcall indent-to ind element-end)))
  21222. (set-marker post nil)
  21223. (set-marker cbeg nil)
  21224. (set-marker cend nil))))
  21225. (set-marker element-end nil))))
  21226. (set-marker end nil))))
  21227. (defun org-indent-drawer ()
  21228. "Indent the drawer at point."
  21229. (interactive)
  21230. (unless (save-excursion
  21231. (beginning-of-line)
  21232. (org-looking-at-p org-drawer-regexp))
  21233. (user-error "Not at a drawer"))
  21234. (let ((element (org-element-at-point)))
  21235. (unless (memq (org-element-type element) '(drawer property-drawer))
  21236. (user-error "Not at a drawer"))
  21237. (org-with-wide-buffer
  21238. (org-indent-region (org-element-property :begin element)
  21239. (org-element-property :end element))))
  21240. (message "Drawer at point indented"))
  21241. (defun org-indent-block ()
  21242. "Indent the block at point."
  21243. (interactive)
  21244. (unless (save-excursion
  21245. (beginning-of-line)
  21246. (let ((case-fold-search t))
  21247. (org-looking-at-p "[ \t]*#\\+\\(begin\\|end\\)_")))
  21248. (user-error "Not at a block"))
  21249. (let ((element (org-element-at-point)))
  21250. (unless (memq (org-element-type element)
  21251. '(comment-block center-block dynamic-block example-block
  21252. export-block quote-block special-block
  21253. src-block verse-block))
  21254. (user-error "Not at a block"))
  21255. (org-with-wide-buffer
  21256. (org-indent-region (org-element-property :begin element)
  21257. (org-element-property :end element))))
  21258. (message "Block at point indented"))
  21259. ;;; Filling
  21260. ;; We use our own fill-paragraph and auto-fill functions.
  21261. ;; `org-fill-paragraph' relies on adaptive filling and context
  21262. ;; checking. Appropriate `fill-prefix' is computed with
  21263. ;; `org-adaptive-fill-function'.
  21264. ;; `org-auto-fill-function' takes care of auto-filling. It calls
  21265. ;; `do-auto-fill' only on valid areas with `fill-prefix' shadowed with
  21266. ;; `org-adaptive-fill-function' value. Internally,
  21267. ;; `org-comment-line-break-function' breaks the line.
  21268. ;; `org-setup-filling' installs filling and auto-filling related
  21269. ;; variables during `org-mode' initialization.
  21270. (defvar org-element-paragraph-separate) ; org-element.el
  21271. (defun org-setup-filling ()
  21272. (require 'org-element)
  21273. ;; Prevent auto-fill from inserting unwanted new items.
  21274. (when (boundp 'fill-nobreak-predicate)
  21275. (setq-local
  21276. fill-nobreak-predicate
  21277. (org-uniquify
  21278. (append fill-nobreak-predicate
  21279. '(org-fill-line-break-nobreak-p
  21280. org-fill-paragraph-with-timestamp-nobreak-p)))))
  21281. (let ((paragraph-ending (substring org-element-paragraph-separate 1)))
  21282. (setq-local paragraph-start paragraph-ending)
  21283. (setq-local paragraph-separate paragraph-ending))
  21284. (setq-local fill-paragraph-function 'org-fill-paragraph)
  21285. (setq-local auto-fill-inhibit-regexp nil)
  21286. (setq-local adaptive-fill-function 'org-adaptive-fill-function)
  21287. (setq-local normal-auto-fill-function 'org-auto-fill-function)
  21288. (setq-local comment-line-break-function 'org-comment-line-break-function))
  21289. (defun org-fill-line-break-nobreak-p ()
  21290. "Non-nil when a new line at point would create an Org line break."
  21291. (save-excursion
  21292. (skip-chars-backward "[ \t]")
  21293. (skip-chars-backward "\\\\")
  21294. (looking-at "\\\\\\\\\\($\\|[^\\\\]\\)")))
  21295. (defun org-fill-paragraph-with-timestamp-nobreak-p ()
  21296. "Non-nil when a new line at point would split a timestamp."
  21297. (and (org-at-timestamp-p t)
  21298. (not (looking-at org-ts-regexp-both))))
  21299. (declare-function message-in-body-p "message" ())
  21300. (defvar orgtbl-line-start-regexp) ; From org-table.el
  21301. (defun org-adaptive-fill-function ()
  21302. "Compute a fill prefix for the current line.
  21303. Return fill prefix, as a string, or nil if current line isn't
  21304. meant to be filled. For convenience, if `adaptive-fill-regexp'
  21305. matches in paragraphs or comments, use it."
  21306. (catch 'exit
  21307. (when (derived-mode-p 'message-mode)
  21308. (save-excursion
  21309. (beginning-of-line)
  21310. (cond ((not (message-in-body-p)) (throw 'exit nil))
  21311. ((org-looking-at-p org-table-line-regexp) (throw 'exit nil))
  21312. ((looking-at message-cite-prefix-regexp)
  21313. (throw 'exit (match-string-no-properties 0)))
  21314. ((looking-at org-outline-regexp)
  21315. (throw 'exit (make-string (length (match-string 0)) ?\s))))))
  21316. (org-with-wide-buffer
  21317. (unless (org-at-heading-p)
  21318. (let* ((p (line-beginning-position))
  21319. (element (save-excursion
  21320. (beginning-of-line)
  21321. (org-element-at-point)))
  21322. (type (org-element-type element))
  21323. (post-affiliated (org-element-property :post-affiliated element)))
  21324. (unless (< p post-affiliated)
  21325. (case type
  21326. (comment
  21327. (save-excursion
  21328. (beginning-of-line)
  21329. (looking-at "[ \t]*")
  21330. (concat (match-string 0) "# ")))
  21331. (footnote-definition "")
  21332. ((item plain-list)
  21333. (make-string (org-list-item-body-column post-affiliated) ?\s))
  21334. (paragraph
  21335. ;; Fill prefix is usually the same as the current line,
  21336. ;; unless the paragraph is at the beginning of an item.
  21337. (let ((parent (org-element-property :parent element)))
  21338. (save-excursion
  21339. (beginning-of-line)
  21340. (cond ((eq (org-element-type parent) 'item)
  21341. (make-string (org-list-item-body-column
  21342. (org-element-property :begin parent))
  21343. ?\s))
  21344. ((and adaptive-fill-regexp
  21345. ;; Locally disable
  21346. ;; `adaptive-fill-function' to let
  21347. ;; `fill-context-prefix' handle
  21348. ;; `adaptive-fill-regexp' variable.
  21349. (let (adaptive-fill-function)
  21350. (fill-context-prefix
  21351. post-affiliated
  21352. (org-element-property :end element)))))
  21353. ((looking-at "[ \t]+") (match-string 0))
  21354. (t "")))))
  21355. (comment-block
  21356. ;; Only fill contents if P is within block boundaries.
  21357. (let* ((cbeg (save-excursion (goto-char post-affiliated)
  21358. (forward-line)
  21359. (point)))
  21360. (cend (save-excursion
  21361. (goto-char (org-element-property :end element))
  21362. (skip-chars-backward " \r\t\n")
  21363. (line-beginning-position))))
  21364. (when (and (>= p cbeg) (< p cend))
  21365. (if (save-excursion (beginning-of-line) (looking-at "[ \t]+"))
  21366. (match-string 0)
  21367. "")))))))))))
  21368. (declare-function message-goto-body "message" ())
  21369. (defvar message-cite-prefix-regexp) ; From message.el
  21370. (defun org-fill-paragraph (&optional justify)
  21371. "Fill element at point, when applicable.
  21372. This function only applies to comment blocks, comments, example
  21373. blocks and paragraphs. Also, as a special case, re-align table
  21374. when point is at one.
  21375. If JUSTIFY is non-nil (interactively, with prefix argument),
  21376. justify as well. If `sentence-end-double-space' is non-nil, then
  21377. period followed by one space does not end a sentence, so don't
  21378. break a line there. The variable `fill-column' controls the
  21379. width for filling.
  21380. For convenience, when point is at a plain list, an item or
  21381. a footnote definition, try to fill the first paragraph within."
  21382. (interactive)
  21383. (if (and (derived-mode-p 'message-mode)
  21384. (or (not (message-in-body-p))
  21385. (save-excursion (move-beginning-of-line 1)
  21386. (looking-at message-cite-prefix-regexp))))
  21387. ;; First ensure filling is correct in message-mode.
  21388. (let ((fill-paragraph-function
  21389. (cadadr (assoc 'fill-paragraph-function org-fb-vars)))
  21390. (fill-prefix (cadadr (assoc 'fill-prefix org-fb-vars)))
  21391. (paragraph-start (cadadr (assoc 'paragraph-start org-fb-vars)))
  21392. (paragraph-separate
  21393. (cadadr (assoc 'paragraph-separate org-fb-vars))))
  21394. (fill-paragraph nil))
  21395. (with-syntax-table org-mode-transpose-word-syntax-table
  21396. ;; Move to end of line in order to get the first paragraph
  21397. ;; within a plain list or a footnote definition.
  21398. (let ((element (save-excursion
  21399. (end-of-line)
  21400. (or (ignore-errors (org-element-at-point))
  21401. (user-error "An element cannot be parsed line %d"
  21402. (line-number-at-pos (point)))))))
  21403. ;; First check if point is in a blank line at the beginning of
  21404. ;; the buffer. In that case, ignore filling.
  21405. (case (org-element-type element)
  21406. ;; Use major mode filling function is src blocks.
  21407. (src-block (org-babel-do-key-sequence-in-edit-buffer (kbd "M-q")))
  21408. ;; Align Org tables, leave table.el tables as-is.
  21409. (table-row (org-table-align) t)
  21410. (table
  21411. (when (eq (org-element-property :type element) 'org)
  21412. (save-excursion
  21413. (goto-char (org-element-property :post-affiliated element))
  21414. (org-table-align)))
  21415. t)
  21416. (paragraph
  21417. ;; Paragraphs may contain `line-break' type objects.
  21418. (let ((beg (max (point-min)
  21419. (org-element-property :contents-begin element)))
  21420. (end (min (point-max)
  21421. (org-element-property :contents-end element))))
  21422. ;; Do nothing if point is at an affiliated keyword.
  21423. (if (< (line-end-position) beg) t
  21424. (when (derived-mode-p 'message-mode)
  21425. ;; In `message-mode', do not fill following citation
  21426. ;; in current paragraph nor text before message body.
  21427. (let ((body-start (save-excursion (message-goto-body))))
  21428. (when body-start (setq beg (max body-start beg))))
  21429. (when (save-excursion
  21430. (re-search-forward
  21431. (concat "^" message-cite-prefix-regexp) end t))
  21432. (setq end (match-beginning 0))))
  21433. ;; Fill paragraph, taking line breaks into account.
  21434. (save-excursion
  21435. (goto-char beg)
  21436. (let ((cuts (list beg)))
  21437. (while (re-search-forward "\\\\\\\\[ \t]*\n" end t)
  21438. (when (eq 'line-break
  21439. (org-element-type
  21440. (save-excursion (backward-char)
  21441. (org-element-context))))
  21442. (push (point) cuts)))
  21443. (dolist (c (delq end cuts))
  21444. (fill-region-as-paragraph c end justify)
  21445. (setq end c))))
  21446. t)))
  21447. ;; Contents of `comment-block' type elements should be
  21448. ;; filled as plain text, but only if point is within block
  21449. ;; markers.
  21450. (comment-block
  21451. (let* ((case-fold-search t)
  21452. (beg (save-excursion
  21453. (goto-char (org-element-property :begin element))
  21454. (re-search-forward "^[ \t]*#\\+begin_comment" nil t)
  21455. (forward-line)
  21456. (point)))
  21457. (end (save-excursion
  21458. (goto-char (org-element-property :end element))
  21459. (re-search-backward "^[ \t]*#\\+end_comment" nil t)
  21460. (line-beginning-position))))
  21461. (if (or (< (point) beg) (> (point) end)) t
  21462. (fill-region-as-paragraph
  21463. (save-excursion (end-of-line)
  21464. (re-search-backward "^[ \t]*$" beg 'move)
  21465. (line-beginning-position))
  21466. (save-excursion (beginning-of-line)
  21467. (re-search-forward "^[ \t]*$" end 'move)
  21468. (line-beginning-position))
  21469. justify))))
  21470. ;; Fill comments.
  21471. (comment
  21472. (let ((begin (org-element-property :post-affiliated element))
  21473. (end (org-element-property :end element)))
  21474. (when (and (>= (point) begin) (<= (point) end))
  21475. (let ((begin (save-excursion
  21476. (end-of-line)
  21477. (if (re-search-backward "^[ \t]*#[ \t]*$" begin t)
  21478. (progn (forward-line) (point))
  21479. begin)))
  21480. (end (save-excursion
  21481. (end-of-line)
  21482. (if (re-search-forward "^[ \t]*#[ \t]*$" end 'move)
  21483. (1- (line-beginning-position))
  21484. (skip-chars-backward " \r\t\n")
  21485. (line-end-position)))))
  21486. ;; Do not fill comments when at a blank line.
  21487. (when (> end begin)
  21488. (let ((fill-prefix
  21489. (save-excursion
  21490. (beginning-of-line)
  21491. (looking-at "[ \t]*#")
  21492. (let ((comment-prefix (match-string 0)))
  21493. (goto-char (match-end 0))
  21494. (if (looking-at adaptive-fill-regexp)
  21495. (concat comment-prefix (match-string 0))
  21496. (concat comment-prefix " "))))))
  21497. (save-excursion
  21498. (fill-region-as-paragraph begin end justify))))))
  21499. t))
  21500. ;; Ignore every other element.
  21501. (otherwise t))))))
  21502. (defun org-auto-fill-function ()
  21503. "Auto-fill function."
  21504. ;; Check if auto-filling is meaningful.
  21505. (let ((fc (current-fill-column)))
  21506. (when (and fc (> (current-column) fc))
  21507. (let* ((fill-prefix (org-adaptive-fill-function))
  21508. ;; Enforce empty fill prefix, if required. Otherwise, it
  21509. ;; will be computed again.
  21510. (adaptive-fill-mode (not (equal fill-prefix ""))))
  21511. (when fill-prefix (do-auto-fill))))))
  21512. (defun org-comment-line-break-function (&optional soft)
  21513. "Break line at point and indent, continuing comment if within one.
  21514. The inserted newline is marked hard if variable
  21515. `use-hard-newlines' is true, unless optional argument SOFT is
  21516. non-nil."
  21517. (if soft (insert-and-inherit ?\n) (newline 1))
  21518. (save-excursion (forward-char -1) (delete-horizontal-space))
  21519. (delete-horizontal-space)
  21520. (indent-to-left-margin)
  21521. (insert-before-markers-and-inherit fill-prefix))
  21522. ;;; Fixed Width Areas
  21523. (defun org-toggle-fixed-width ()
  21524. "Toggle fixed-width markup.
  21525. Add or remove fixed-width markup on current line, whenever it
  21526. makes sense. Return an error otherwise.
  21527. If a region is active and if it contains only fixed-width areas
  21528. or blank lines, remove all fixed-width markup in it. If the
  21529. region contains anything else, convert all non-fixed-width lines
  21530. to fixed-width ones.
  21531. Blank lines at the end of the region are ignored unless the
  21532. region only contains such lines."
  21533. (interactive)
  21534. (if (not (org-region-active-p))
  21535. ;; No region:
  21536. ;;
  21537. ;; Remove fixed width marker only in a fixed-with element.
  21538. ;;
  21539. ;; Add fixed width maker in paragraphs, in blank lines after
  21540. ;; elements or at the beginning of a headline or an inlinetask,
  21541. ;; and before any one-line elements (e.g., a clock).
  21542. (progn
  21543. (beginning-of-line)
  21544. (let* ((element (org-element-at-point))
  21545. (type (org-element-type element)))
  21546. (cond
  21547. ((and (eq type 'fixed-width)
  21548. (looking-at "[ \t]*\\(:\\(?: \\|$\\)\\)"))
  21549. (replace-match
  21550. "" nil nil nil (if (= (line-end-position) (match-end 0)) 0 1)))
  21551. ((and (memq type '(babel-call clock comment diary-sexp headline
  21552. horizontal-rule keyword paragraph
  21553. planning))
  21554. (<= (org-element-property :post-affiliated element) (point)))
  21555. (skip-chars-forward " \t")
  21556. (insert ": "))
  21557. ((and (org-looking-at-p "[ \t]*$")
  21558. (or (eq type 'inlinetask)
  21559. (save-excursion
  21560. (skip-chars-forward " \r\t\n")
  21561. (<= (org-element-property :end element) (point)))))
  21562. (delete-region (point) (line-end-position))
  21563. (org-indent-line)
  21564. (insert ": "))
  21565. (t (user-error "Cannot insert a fixed-width line here")))))
  21566. ;; Region active.
  21567. (let* ((begin (save-excursion
  21568. (goto-char (region-beginning))
  21569. (line-beginning-position)))
  21570. (end (copy-marker
  21571. (save-excursion
  21572. (goto-char (region-end))
  21573. (unless (eolp) (beginning-of-line))
  21574. (if (save-excursion (re-search-backward "\\S-" begin t))
  21575. (progn (skip-chars-backward " \r\t\n") (point))
  21576. (point)))))
  21577. (all-fixed-width-p
  21578. (catch 'not-all-p
  21579. (save-excursion
  21580. (goto-char begin)
  21581. (skip-chars-forward " \r\t\n")
  21582. (when (eobp) (throw 'not-all-p nil))
  21583. (while (< (point) end)
  21584. (let ((element (org-element-at-point)))
  21585. (if (eq (org-element-type element) 'fixed-width)
  21586. (goto-char (org-element-property :end element))
  21587. (throw 'not-all-p nil))))
  21588. t))))
  21589. (if all-fixed-width-p
  21590. (save-excursion
  21591. (goto-char begin)
  21592. (while (< (point) end)
  21593. (when (looking-at "[ \t]*\\(:\\(?: \\|$\\)\\)")
  21594. (replace-match
  21595. "" nil nil nil
  21596. (if (= (line-end-position) (match-end 0)) 0 1)))
  21597. (forward-line)))
  21598. (let ((min-ind (point-max)))
  21599. ;; Find minimum indentation across all lines.
  21600. (save-excursion
  21601. (goto-char begin)
  21602. (if (not (save-excursion (re-search-forward "\\S-" end t)))
  21603. (setq min-ind 0)
  21604. (catch 'zerop
  21605. (while (< (point) end)
  21606. (unless (org-looking-at-p "[ \t]*$")
  21607. (let ((ind (org-get-indentation)))
  21608. (setq min-ind (min min-ind ind))
  21609. (when (zerop ind) (throw 'zerop t))))
  21610. (forward-line)))))
  21611. ;; Loop over all lines and add fixed-width markup everywhere
  21612. ;; but in fixed-width lines.
  21613. (save-excursion
  21614. (goto-char begin)
  21615. (while (< (point) end)
  21616. (cond
  21617. ((org-at-heading-p)
  21618. (insert ": ")
  21619. (forward-line)
  21620. (while (and (< (point) end) (org-looking-at-p "[ \t]*$"))
  21621. (insert ":")
  21622. (forward-line)))
  21623. ((org-looking-at-p "[ \t]*:\\( \\|$\\)")
  21624. (let* ((element (org-element-at-point))
  21625. (element-end (org-element-property :end element)))
  21626. (if (eq (org-element-type element) 'fixed-width)
  21627. (progn (goto-char element-end)
  21628. (skip-chars-backward " \r\t\n")
  21629. (forward-line))
  21630. (let ((limit (min end element-end)))
  21631. (while (< (point) limit)
  21632. (org-move-to-column min-ind t)
  21633. (insert ": ")
  21634. (forward-line))))))
  21635. (t
  21636. (org-move-to-column min-ind t)
  21637. (insert ": ")
  21638. (forward-line)))))))
  21639. (set-marker end nil))))
  21640. ;;; Comments
  21641. ;; Org comments syntax is quite complex. It requires the entire line
  21642. ;; to be just a comment. Also, even with the right syntax at the
  21643. ;; beginning of line, some some elements (i.e. verse-block or
  21644. ;; example-block) don't accept comments. Usual Emacs comment commands
  21645. ;; cannot cope with those requirements. Therefore, Org replaces them.
  21646. ;; Org still relies on `comment-dwim', but cannot trust
  21647. ;; `comment-only-p'. So, `comment-region-function' and
  21648. ;; `uncomment-region-function' both point
  21649. ;; to`org-comment-or-uncomment-region'. Eventually,
  21650. ;; `org-insert-comment' takes care of insertion of comments at the
  21651. ;; beginning of line.
  21652. ;; `org-setup-comments-handling' install comments related variables
  21653. ;; during `org-mode' initialization.
  21654. (defun org-setup-comments-handling ()
  21655. (interactive)
  21656. (setq-local comment-use-syntax nil)
  21657. (setq-local comment-start "# ")
  21658. (setq-local comment-start-skip "^\\s-*#\\(?: \\|$\\)")
  21659. (setq-local comment-insert-comment-function 'org-insert-comment)
  21660. (setq-local comment-region-function 'org-comment-or-uncomment-region)
  21661. (setq-local uncomment-region-function 'org-comment-or-uncomment-region))
  21662. (defun org-insert-comment ()
  21663. "Insert an empty comment above current line.
  21664. If the line is empty, insert comment at its beginning. When
  21665. point is within a source block, comment according to the related
  21666. major mode."
  21667. (if (let ((element (org-element-at-point)))
  21668. (and (eq (org-element-type element) 'src-block)
  21669. (< (save-excursion
  21670. (goto-char (org-element-property :post-affiliated element))
  21671. (line-end-position))
  21672. (point))
  21673. (> (save-excursion
  21674. (goto-char (org-element-property :end element))
  21675. (skip-chars-backward " \r\t\n")
  21676. (line-beginning-position))
  21677. (point))))
  21678. (org-babel-do-in-edit-buffer (call-interactively 'comment-dwim))
  21679. (beginning-of-line)
  21680. (if (looking-at "\\s-*$") (delete-region (point) (point-at-eol))
  21681. (open-line 1))
  21682. (org-indent-line)
  21683. (insert "# ")))
  21684. (defvar comment-empty-lines) ; From newcomment.el.
  21685. (defun org-comment-or-uncomment-region (beg end &rest _)
  21686. "Comment or uncomment each non-blank line in the region.
  21687. Uncomment each non-blank line between BEG and END if it only
  21688. contains commented lines. Otherwise, comment them. If region is
  21689. strictly within a source block, use appropriate comment syntax."
  21690. (if (let ((element (org-element-at-point)))
  21691. (and (eq (org-element-type element) 'src-block)
  21692. (< (save-excursion
  21693. (goto-char (org-element-property :post-affiliated element))
  21694. (line-end-position))
  21695. beg)
  21696. (>= (save-excursion
  21697. (goto-char (org-element-property :end element))
  21698. (skip-chars-backward " \r\t\n")
  21699. (line-beginning-position))
  21700. end)))
  21701. (org-babel-do-in-edit-buffer (call-interactively 'comment-dwim))
  21702. (save-restriction
  21703. ;; Restrict region
  21704. (narrow-to-region (save-excursion (goto-char beg)
  21705. (skip-chars-forward " \r\t\n" end)
  21706. (line-beginning-position))
  21707. (save-excursion (goto-char end)
  21708. (skip-chars-backward " \r\t\n" beg)
  21709. (line-end-position)))
  21710. (let ((uncommentp
  21711. ;; UNCOMMENTP is non-nil when every non blank line between
  21712. ;; BEG and END is a comment.
  21713. (save-excursion
  21714. (goto-char (point-min))
  21715. (while (and (not (eobp))
  21716. (let ((element (org-element-at-point)))
  21717. (and (eq (org-element-type element) 'comment)
  21718. (goto-char (min (point-max)
  21719. (org-element-property
  21720. :end element)))))))
  21721. (eobp))))
  21722. (if uncommentp
  21723. ;; Only blank lines and comments in region: uncomment it.
  21724. (save-excursion
  21725. (goto-char (point-min))
  21726. (while (not (eobp))
  21727. (when (looking-at "[ \t]*\\(#\\(?: \\|$\\)\\)")
  21728. (replace-match "" nil nil nil 1))
  21729. (forward-line)))
  21730. ;; Comment each line in region.
  21731. (let ((min-indent (point-max)))
  21732. ;; First find the minimum indentation across all lines.
  21733. (save-excursion
  21734. (goto-char (point-min))
  21735. (while (and (not (eobp)) (not (zerop min-indent)))
  21736. (unless (looking-at "[ \t]*$")
  21737. (setq min-indent (min min-indent (current-indentation))))
  21738. (forward-line)))
  21739. ;; Then loop over all lines.
  21740. (save-excursion
  21741. (goto-char (point-min))
  21742. (while (not (eobp))
  21743. (unless (and (not comment-empty-lines) (looking-at "[ \t]*$"))
  21744. ;; Don't get fooled by invisible text (e.g. link path)
  21745. ;; when moving to column MIN-INDENT.
  21746. (let ((buffer-invisibility-spec nil))
  21747. (org-move-to-column min-indent t))
  21748. (insert comment-start))
  21749. (forward-line)))))))))
  21750. (defun org-comment-dwim (arg)
  21751. "Call `comment-dwim' within a source edit buffer if needed."
  21752. (interactive "*P")
  21753. (if (org-in-src-block-p)
  21754. (org-babel-do-in-edit-buffer (call-interactively 'comment-dwim))
  21755. (call-interactively 'comment-dwim)))
  21756. ;;; Timestamps API
  21757. ;; This section contains tools to operate on timestamp objects, as
  21758. ;; returned by, e.g. `org-element-context'.
  21759. (defun org-timestamp--to-internal-time (timestamp &optional end)
  21760. "Encode TIMESTAMP object into Emacs internal time.
  21761. Use end of date range or time range when END is non-nil."
  21762. (apply #'encode-time
  21763. (cons 0
  21764. (mapcar
  21765. (lambda (prop) (or (org-element-property prop timestamp) 0))
  21766. (if end '(:minute-end :hour-end :day-end :month-end :year-end)
  21767. '(:minute-start :hour-start :day-start :month-start
  21768. :year-start))))))
  21769. (defun org-timestamp-has-time-p (timestamp)
  21770. "Non-nil when TIMESTAMP has a time specified."
  21771. (org-element-property :hour-start timestamp))
  21772. (defun org-timestamp-format (timestamp format &optional end utc)
  21773. "Format a TIMESTAMP object into a string.
  21774. FORMAT is a format specifier to be passed to
  21775. `format-time-string'.
  21776. When optional argument END is non-nil, use end of date-range or
  21777. time-range, if possible.
  21778. When optional argument UTC is non-nil, time will be expressed as
  21779. Universal Time."
  21780. (format-time-string
  21781. format (org-timestamp--to-internal-time timestamp end) utc))
  21782. (defun org-timestamp-split-range (timestamp &optional end)
  21783. "Extract a TIMESTAMP object from a date or time range.
  21784. END, when non-nil, means extract the end of the range.
  21785. Otherwise, extract its start.
  21786. Return a new timestamp object."
  21787. (let ((type (org-element-property :type timestamp)))
  21788. (if (memq type '(active inactive diary)) timestamp
  21789. (let ((split-ts (org-element-copy timestamp)))
  21790. ;; Set new type.
  21791. (org-element-put-property
  21792. split-ts :type (if (eq type 'active-range) 'active 'inactive))
  21793. ;; Copy start properties over end properties if END is
  21794. ;; non-nil. Otherwise, copy end properties over `start' ones.
  21795. (let ((p-alist '((:minute-start . :minute-end)
  21796. (:hour-start . :hour-end)
  21797. (:day-start . :day-end)
  21798. (:month-start . :month-end)
  21799. (:year-start . :year-end))))
  21800. (dolist (p-cell p-alist)
  21801. (org-element-put-property
  21802. split-ts
  21803. (funcall (if end #'car #'cdr) p-cell)
  21804. (org-element-property
  21805. (funcall (if end #'cdr #'car) p-cell) split-ts)))
  21806. ;; Eventually refresh `:raw-value'.
  21807. (org-element-put-property split-ts :raw-value nil)
  21808. (org-element-put-property
  21809. split-ts :raw-value (org-element-interpret-data split-ts)))))))
  21810. (defun org-timestamp-translate (timestamp &optional boundary)
  21811. "Translate TIMESTAMP object to custom format.
  21812. Format string is defined in `org-time-stamp-custom-formats',
  21813. which see.
  21814. When optional argument BOUNDARY is non-nil, it is either the
  21815. symbol `start' or `end'. In this case, only translate the
  21816. starting or ending part of TIMESTAMP if it is a date or time
  21817. range. Otherwise, translate both parts.
  21818. Return timestamp as-is if `org-display-custom-times' is nil or if
  21819. it has a `diary' type."
  21820. (let ((type (org-element-property :type timestamp)))
  21821. (if (or (not org-display-custom-times) (eq type 'diary))
  21822. (org-element-interpret-data timestamp)
  21823. (let ((fmt (funcall (if (org-timestamp-has-time-p timestamp) #'cdr #'car)
  21824. org-time-stamp-custom-formats)))
  21825. (if (and (not boundary) (memq type '(active-range inactive-range)))
  21826. (concat (org-timestamp-format timestamp fmt)
  21827. "--"
  21828. (org-timestamp-format timestamp fmt t))
  21829. (org-timestamp-format timestamp fmt (eq boundary 'end)))))))
  21830. ;;; Other stuff.
  21831. (defvar reftex-docstruct-symbol)
  21832. (defvar reftex-cite-format)
  21833. (defvar org--rds)
  21834. (defun org-reftex-citation ()
  21835. "Use reftex-citation to insert a citation into the buffer.
  21836. This looks for a line like
  21837. #+BIBLIOGRAPHY: foo plain option:-d
  21838. and derives from it that foo.bib is the bibliography file relevant
  21839. for this document. It then installs the necessary environment for RefTeX
  21840. to work in this buffer and calls `reftex-citation' to insert a citation
  21841. into the buffer.
  21842. Export of such citations to both LaTeX and HTML is handled by the contributed
  21843. package ox-bibtex by Taru Karttunen."
  21844. (interactive)
  21845. (let ((reftex-docstruct-symbol 'org--rds)
  21846. (reftex-cite-format "\\cite{%l}")
  21847. org--rds bib)
  21848. (save-excursion
  21849. (save-restriction
  21850. (widen)
  21851. (let ((case-fold-search t)
  21852. (re "^[ \t]*#\\+BIBLIOGRAPHY:[ \t]+\\([^ \t\n]+\\)"))
  21853. (if (not (save-excursion
  21854. (or (re-search-forward re nil t)
  21855. (re-search-backward re nil t))))
  21856. (user-error "No bibliography defined in file")
  21857. (setq bib (concat (match-string 1) ".bib")
  21858. org--rds (list (list 'bib bib)))))))
  21859. (call-interactively 'reftex-citation)))
  21860. ;;;; Functions extending outline functionality
  21861. (defun org-beginning-of-line (&optional arg)
  21862. "Go to the beginning of the current line. If that is invisible, continue
  21863. to a visible line beginning. This makes the function of C-a more intuitive.
  21864. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  21865. first attempt, and only move to after the tags when the cursor is already
  21866. beyond the end of the headline."
  21867. (interactive "P")
  21868. (let ((pos (point))
  21869. (special (if (consp org-special-ctrl-a/e)
  21870. (car org-special-ctrl-a/e)
  21871. org-special-ctrl-a/e))
  21872. deactivate-mark refpos)
  21873. (if (org-bound-and-true-p visual-line-mode)
  21874. (beginning-of-visual-line 1)
  21875. (beginning-of-line 1))
  21876. (if (and arg (fboundp 'move-beginning-of-line))
  21877. (call-interactively 'move-beginning-of-line)
  21878. (unless (bobp)
  21879. (backward-char 1)
  21880. (if (org-truely-invisible-p)
  21881. (while (and (not (bobp)) (org-truely-invisible-p))
  21882. (backward-char 1)
  21883. (beginning-of-line 1))
  21884. (forward-char 1))))
  21885. (when special
  21886. (cond
  21887. ((and (looking-at org-complex-heading-regexp)
  21888. (eq (char-after (match-end 1)) ?\s))
  21889. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  21890. (point-at-eol)))
  21891. (goto-char
  21892. (if (eq special t)
  21893. (cond ((> pos refpos) refpos)
  21894. ((= pos (point)) refpos)
  21895. (t (point)))
  21896. (cond ((> pos (point)) (point))
  21897. ((not (eq last-command this-command)) (point))
  21898. (t refpos)))))
  21899. ((org-at-item-p)
  21900. ;; Being at an item and not looking at an the item means point
  21901. ;; was previously moved to beginning of a visual line, which
  21902. ;; doesn't contain the item. Therefore, do nothing special,
  21903. ;; just stay here.
  21904. (when (looking-at org-list-full-item-re)
  21905. ;; Set special position at first white space character after
  21906. ;; bullet, and check-box, if any.
  21907. (let ((after-bullet
  21908. (let ((box (match-end 3)))
  21909. (if (not box) (match-end 1)
  21910. (let ((after (char-after box)))
  21911. (if (and after (= after ? )) (1+ box) box))))))
  21912. ;; Special case: Move point to special position when
  21913. ;; currently after it or at beginning of line.
  21914. (if (eq special t)
  21915. (when (or (> pos after-bullet) (= (point) pos))
  21916. (goto-char after-bullet))
  21917. ;; Reversed case: Move point to special position when
  21918. ;; point was already at beginning of line and command is
  21919. ;; repeated.
  21920. (when (and (= (point) pos) (eq last-command this-command))
  21921. (goto-char after-bullet))))))))
  21922. (org-no-warnings
  21923. (and (featurep 'xemacs) (setq zmacs-region-stays t))))
  21924. (setq disable-point-adjustment
  21925. (or (not (invisible-p (point)))
  21926. (not (invisible-p (max (point-min) (1- (point))))))))
  21927. (defun org-end-of-line (&optional arg)
  21928. "Go to the end of the line.
  21929. If this is a headline, and `org-special-ctrl-a/e' is set, ignore
  21930. tags on the first attempt, and only move to after the tags when
  21931. the cursor is already beyond the end of the headline."
  21932. (interactive "P")
  21933. (let ((special (if (consp org-special-ctrl-a/e) (cdr org-special-ctrl-a/e)
  21934. org-special-ctrl-a/e))
  21935. (move-fun (cond ((org-bound-and-true-p visual-line-mode)
  21936. 'end-of-visual-line)
  21937. ((fboundp 'move-end-of-line) 'move-end-of-line)
  21938. (t 'end-of-line)))
  21939. deactivate-mark)
  21940. (if (or (not special) arg) (call-interactively move-fun)
  21941. (let* ((element (save-excursion (beginning-of-line)
  21942. (org-element-at-point)))
  21943. (type (org-element-type element)))
  21944. (cond
  21945. ((memq type '(headline inlinetask))
  21946. (let ((pos (point)))
  21947. (beginning-of-line 1)
  21948. (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*\\)?$"))
  21949. (if (eq special t)
  21950. (if (or (< pos (match-beginning 1)) (= pos (match-end 0)))
  21951. (goto-char (match-beginning 1))
  21952. (goto-char (match-end 0)))
  21953. (if (or (< pos (match-end 0))
  21954. (not (eq this-command last-command)))
  21955. (goto-char (match-end 0))
  21956. (goto-char (match-beginning 1))))
  21957. (call-interactively move-fun))))
  21958. ((outline-invisible-p (line-end-position))
  21959. ;; If element is hidden, `move-end-of-line' would put point
  21960. ;; after it. Use `end-of-line' to stay on current line.
  21961. (call-interactively 'end-of-line))
  21962. (t (call-interactively move-fun)))))
  21963. (org-no-warnings (and (featurep 'xemacs) (setq zmacs-region-stays t))))
  21964. (setq disable-point-adjustment
  21965. (or (not (invisible-p (point)))
  21966. (not (invisible-p (max (point-min) (1- (point))))))))
  21967. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  21968. (define-key org-mode-map "\C-e" 'org-end-of-line)
  21969. (defun org-backward-sentence (&optional _arg)
  21970. "Go to beginning of sentence, or beginning of table field.
  21971. This will call `backward-sentence' or `org-table-beginning-of-field',
  21972. depending on context."
  21973. (interactive)
  21974. (let* ((element (org-element-at-point))
  21975. (contents-begin (org-element-property :contents-begin element))
  21976. (table (org-element-lineage element '(table) t)))
  21977. (if (and table
  21978. (> (point) contents-begin)
  21979. (<= (point) (org-element-property :contents-end table)))
  21980. (call-interactively #'org-table-beginning-of-field)
  21981. (save-restriction
  21982. (when (and contents-begin
  21983. (< (point-min) contents-begin)
  21984. (> (point) contents-begin))
  21985. (narrow-to-region contents-begin
  21986. (org-element-property :contents-end element)))
  21987. (call-interactively #'backward-sentence)))))
  21988. (defun org-forward-sentence (&optional _arg)
  21989. "Go to end of sentence, or end of table field.
  21990. This will call `forward-sentence' or `org-table-end-of-field',
  21991. depending on context."
  21992. (interactive)
  21993. (let* ((element (org-element-at-point))
  21994. (contents-end (org-element-property :contents-end element))
  21995. (table (org-element-lineage element '(table) t)))
  21996. (if (and table
  21997. (>= (point) (org-element-property :contents-begin table))
  21998. (< (point) contents-end))
  21999. (call-interactively #'org-table-end-of-field)
  22000. (save-restriction
  22001. (when (and contents-end
  22002. (> (point-max) contents-end)
  22003. ;; Skip blank lines between elements.
  22004. (< (org-element-property :end element)
  22005. (save-excursion (goto-char contents-end)
  22006. (skip-chars-forward " \r\t\n"))))
  22007. (narrow-to-region (org-element-property :contents-begin element)
  22008. contents-end))
  22009. (call-interactively #'forward-sentence)))))
  22010. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  22011. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  22012. (defun org-kill-line (&optional _arg)
  22013. "Kill line, to tags or end of line."
  22014. (interactive)
  22015. (cond
  22016. ((or (not org-special-ctrl-k)
  22017. (bolp)
  22018. (not (org-at-heading-p)))
  22019. (when (and (get-char-property (min (point-max) (point-at-eol)) 'invisible)
  22020. org-ctrl-k-protect-subtree
  22021. (or (eq org-ctrl-k-protect-subtree 'error)
  22022. (not (y-or-n-p "Kill hidden subtree along with headline? "))))
  22023. (user-error "C-k aborted as it would kill a hidden subtree"))
  22024. (call-interactively
  22025. (if (org-bound-and-true-p visual-line-mode) 'kill-visual-line 'kill-line)))
  22026. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$"))
  22027. (kill-region (point) (match-beginning 1))
  22028. (org-set-tags nil t))
  22029. (t (kill-region (point) (point-at-eol)))))
  22030. (define-key org-mode-map "\C-k" 'org-kill-line)
  22031. (defun org-yank (&optional arg)
  22032. "Yank. If the kill is a subtree, treat it specially.
  22033. This command will look at the current kill and check if is a single
  22034. subtree, or a series of subtrees[1]. If it passes the test, and if the
  22035. cursor is at the beginning of a line or after the stars of a currently
  22036. empty headline, then the yank is handled specially. How exactly depends
  22037. on the value of the following variables, both set by default.
  22038. `org-yank-folded-subtrees'
  22039. When set, the subtree(s) will be folded after insertion, but only
  22040. if doing so would now swallow text after the yanked text.
  22041. `org-yank-adjusted-subtrees'
  22042. When set, the subtree will be promoted or demoted in order to
  22043. fit into the local outline tree structure, which means that the
  22044. level will be adjusted so that it becomes the smaller one of the
  22045. two *visible* surrounding headings.
  22046. Any prefix to this command will cause `yank' to be called directly with
  22047. no special treatment. In particular, a simple \\[universal-argument] prefix \
  22048. will just
  22049. plainly yank the text as it is.
  22050. \[1] The test checks if the first non-white line is a heading
  22051. and if there are no other headings with fewer stars."
  22052. (interactive "P")
  22053. (org-yank-generic 'yank arg))
  22054. (defun org-yank-generic (command arg)
  22055. "Perform some yank-like command.
  22056. This function implements the behavior described in the `org-yank'
  22057. documentation. However, it has been generalized to work for any
  22058. interactive command with similar behavior."
  22059. ;; pretend to be command COMMAND
  22060. (setq this-command command)
  22061. (if arg
  22062. (call-interactively command)
  22063. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  22064. (and (org-kill-is-subtree-p)
  22065. (or (bolp)
  22066. (and (looking-at "[ \t]*$")
  22067. (string-match
  22068. "\\`\\*+\\'"
  22069. (buffer-substring (point-at-bol) (point)))))))
  22070. swallowp)
  22071. (cond
  22072. ((and subtreep org-yank-folded-subtrees)
  22073. (let ((beg (point))
  22074. end)
  22075. (if (and subtreep org-yank-adjusted-subtrees)
  22076. (org-paste-subtree nil nil 'for-yank)
  22077. (call-interactively command))
  22078. (setq end (point))
  22079. (goto-char beg)
  22080. (when (and (bolp) subtreep
  22081. (not (setq swallowp
  22082. (org-yank-folding-would-swallow-text beg end))))
  22083. (org-with-limited-levels
  22084. (or (looking-at org-outline-regexp)
  22085. (re-search-forward org-outline-regexp-bol end t))
  22086. (while (and (< (point) end) (looking-at org-outline-regexp))
  22087. (outline-hide-subtree)
  22088. (org-cycle-show-empty-lines 'folded)
  22089. (condition-case nil
  22090. (outline-forward-same-level 1)
  22091. (error (goto-char end))))))
  22092. (when swallowp
  22093. (message
  22094. "Inserted text not folded because that would swallow text"))
  22095. (goto-char end)
  22096. (skip-chars-forward " \t\n\r")
  22097. (beginning-of-line 1)
  22098. (push-mark beg 'nomsg)))
  22099. ((and subtreep org-yank-adjusted-subtrees)
  22100. (let ((beg (point-at-bol)))
  22101. (org-paste-subtree nil nil 'for-yank)
  22102. (push-mark beg 'nomsg)))
  22103. (t
  22104. (call-interactively command))))))
  22105. (defun org-yank-folding-would-swallow-text (beg end)
  22106. "Would hide-subtree at BEG swallow any text after END?"
  22107. (let (level)
  22108. (org-with-limited-levels
  22109. (save-excursion
  22110. (goto-char beg)
  22111. (when (or (looking-at org-outline-regexp)
  22112. (re-search-forward org-outline-regexp-bol end t))
  22113. (setq level (org-outline-level)))
  22114. (goto-char end)
  22115. (skip-chars-forward " \t\r\n\v\f")
  22116. (not (or (eobp)
  22117. (and (bolp) (looking-at-p org-outline-regexp)
  22118. (<= (org-outline-level) level))))))))
  22119. (define-key org-mode-map "\C-y" 'org-yank)
  22120. (defun org-truely-invisible-p ()
  22121. "Check if point is at a character currently not visible.
  22122. This version does not only check the character property, but also
  22123. `visible-mode'."
  22124. ;; Early versions of noutline don't have `outline-invisible-p'.
  22125. (unless (org-bound-and-true-p visible-mode)
  22126. (outline-invisible-p)))
  22127. (defun org-invisible-p2 ()
  22128. "Check if point is at a character currently not visible."
  22129. (save-excursion
  22130. (when (and (eolp) (not (bobp))) (backward-char 1))
  22131. ;; Early versions of noutline don't have `outline-invisible-p'.
  22132. (outline-invisible-p)))
  22133. (defun org-back-to-heading (&optional invisible-ok)
  22134. "Call `outline-back-to-heading', but provide a better error message."
  22135. (condition-case nil
  22136. (outline-back-to-heading invisible-ok)
  22137. (error (error "Before first headline at position %d in buffer %s"
  22138. (point) (current-buffer)))))
  22139. (defun org-before-first-heading-p ()
  22140. "Before first heading?"
  22141. (save-excursion
  22142. (end-of-line)
  22143. (null (re-search-backward org-outline-regexp-bol nil t))))
  22144. (defun org-at-heading-p (&optional ignored)
  22145. (outline-on-heading-p t))
  22146. ;;; Though the function was obsoleted in 7.8.03, the byte-compiler
  22147. ;;; warning was only added in Org 9.0, which should be taken into
  22148. ;;; account when deciding when to remove the alias.
  22149. (define-obsolete-function-alias 'org-on-heading-p 'org-at-heading-p "Org 7.8.03")
  22150. (defun org-in-commented-heading-p (&optional no-inheritance)
  22151. "Non-nil if point is under a commented heading.
  22152. This function also checks ancestors of the current headline,
  22153. unless optional argument NO-INHERITANCE is non-nil."
  22154. (cond
  22155. ((org-before-first-heading-p) nil)
  22156. ((let ((headline (nth 4 (org-heading-components))))
  22157. (and headline
  22158. (let ((case-fold-search nil))
  22159. (org-string-match-p (concat "^" org-comment-string "\\(?: \\|$\\)")
  22160. headline)))))
  22161. (no-inheritance nil)
  22162. (t
  22163. (save-excursion (and (org-up-heading-safe) (org-in-commented-heading-p))))))
  22164. (defun org-at-comment-p nil
  22165. "Is cursor in a commented line?"
  22166. (save-excursion
  22167. (save-match-data
  22168. (beginning-of-line)
  22169. (looking-at "^[ \t]*# "))))
  22170. (defun org-at-drawer-p nil
  22171. "Is cursor at a drawer keyword?"
  22172. (save-excursion
  22173. (move-beginning-of-line 1)
  22174. (looking-at org-drawer-regexp)))
  22175. (defun org-at-block-p nil
  22176. "Is cursor at a block keyword?"
  22177. (save-excursion
  22178. (move-beginning-of-line 1)
  22179. (looking-at org-block-regexp)))
  22180. (defun org-point-at-end-of-empty-headline ()
  22181. "If point is at the end of an empty headline, return t, else nil.
  22182. If the heading only contains a TODO keyword, it is still still considered
  22183. empty."
  22184. (and (looking-at "[ \t]*$")
  22185. (when org-todo-line-regexp
  22186. (save-excursion
  22187. (beginning-of-line 1)
  22188. (let ((case-fold-search nil))
  22189. (looking-at org-todo-line-regexp)
  22190. (string= (match-string 3) ""))))))
  22191. (defun org-at-heading-or-item-p ()
  22192. (or (org-at-heading-p) (org-at-item-p)))
  22193. (defun org-at-target-p ()
  22194. (or (org-in-regexp org-radio-target-regexp)
  22195. (org-in-regexp org-target-regexp)))
  22196. ;; Compatibility alias with Org versions < 7.8.03
  22197. (defalias 'org-on-target-p 'org-at-target-p)
  22198. (defun org-up-heading-all (arg)
  22199. "Move to the heading line of which the present line is a subheading.
  22200. This function considers both visible and invisible heading lines.
  22201. With argument, move up ARG levels."
  22202. (if (fboundp 'outline-up-heading-all)
  22203. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  22204. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  22205. (defun org-up-heading-safe ()
  22206. "Move to the heading line of which the present line is a subheading.
  22207. This version will not throw an error. It will return the level of the
  22208. headline found, or nil if no higher level is found.
  22209. Also, this function will be a lot faster than `outline-up-heading',
  22210. because it relies on stars being the outline starters. This can really
  22211. make a significant difference in outlines with very many siblings."
  22212. (when (ignore-errors (org-back-to-heading t))
  22213. (let ((level-up (1- (funcall outline-level))))
  22214. (and (> level-up 0)
  22215. (re-search-backward (format "^\\*\\{1,%d\\} " level-up) nil t)
  22216. (funcall outline-level)))))
  22217. (defun org-first-sibling-p ()
  22218. "Is this heading the first child of its parents?"
  22219. (interactive)
  22220. (let ((re org-outline-regexp-bol)
  22221. level l)
  22222. (unless (org-at-heading-p t)
  22223. (user-error "Not at a heading"))
  22224. (setq level (funcall outline-level))
  22225. (save-excursion
  22226. (if (not (re-search-backward re nil t))
  22227. t
  22228. (setq l (funcall outline-level))
  22229. (< l level)))))
  22230. (defun org-goto-sibling (&optional previous)
  22231. "Goto the next sibling, even if it is invisible.
  22232. When PREVIOUS is set, go to the previous sibling instead. Returns t
  22233. when a sibling was found. When none is found, return nil and don't
  22234. move point."
  22235. (let ((fun (if previous 're-search-backward 're-search-forward))
  22236. (pos (point))
  22237. (re org-outline-regexp-bol)
  22238. level l)
  22239. (when (ignore-errors (org-back-to-heading t))
  22240. (setq level (funcall outline-level))
  22241. (catch 'exit
  22242. (or previous (forward-char 1))
  22243. (while (funcall fun re nil t)
  22244. (setq l (funcall outline-level))
  22245. (when (< l level) (goto-char pos) (throw 'exit nil))
  22246. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  22247. (goto-char pos)
  22248. nil))))
  22249. (defun org-show-siblings ()
  22250. "Show all siblings of the current headline."
  22251. (save-excursion
  22252. (while (org-goto-sibling) (org-flag-heading nil)))
  22253. (save-excursion
  22254. (while (org-goto-sibling 'previous)
  22255. (org-flag-heading nil))))
  22256. (defun org-goto-first-child ()
  22257. "Goto the first child, even if it is invisible.
  22258. Return t when a child was found. Otherwise don't move point and
  22259. return nil."
  22260. (let (level (pos (point)) (re org-outline-regexp-bol))
  22261. (when (ignore-errors (org-back-to-heading t))
  22262. (setq level (outline-level))
  22263. (forward-char 1)
  22264. (if (and (re-search-forward re nil t) (> (outline-level) level))
  22265. (progn (goto-char (match-beginning 0)) t)
  22266. (goto-char pos) nil))))
  22267. (defun org-show-hidden-entry ()
  22268. "Show an entry where even the heading is hidden."
  22269. (save-excursion
  22270. (org-show-entry)))
  22271. (defun org-flag-heading (flag &optional entry)
  22272. "Flag the current heading. FLAG non-nil means make invisible.
  22273. When ENTRY is non-nil, show the entire entry."
  22274. (save-excursion
  22275. (org-back-to-heading t)
  22276. ;; Check if we should show the entire entry
  22277. (if entry
  22278. (progn
  22279. (org-show-entry)
  22280. (save-excursion
  22281. (and (outline-next-heading)
  22282. (org-flag-heading nil))))
  22283. (outline-flag-region (max (point-min) (1- (point)))
  22284. (save-excursion (outline-end-of-heading) (point))
  22285. flag))))
  22286. (defun org-get-next-sibling ()
  22287. "Move to next heading of the same level, and return point.
  22288. If there is no such heading, return nil.
  22289. This is like outline-next-sibling, but invisible headings are ok."
  22290. (let ((level (funcall outline-level)))
  22291. (outline-next-heading)
  22292. (while (and (not (eobp)) (> (funcall outline-level) level))
  22293. (outline-next-heading))
  22294. (unless (or (eobp) (< (funcall outline-level) level))
  22295. (point))))
  22296. (defun org-get-last-sibling ()
  22297. "Move to previous heading of the same level, and return point.
  22298. If there is no such heading, return nil."
  22299. (let ((opoint (point))
  22300. (level (funcall outline-level)))
  22301. (outline-previous-heading)
  22302. (when (and (/= (point) opoint) (outline-on-heading-p t))
  22303. (while (and (> (funcall outline-level) level)
  22304. (not (bobp)))
  22305. (outline-previous-heading))
  22306. (unless (< (funcall outline-level) level)
  22307. (point)))))
  22308. (defun org-end-of-subtree (&optional invisible-ok to-heading)
  22309. "Goto to the end of a subtree."
  22310. ;; This contains an exact copy of the original function, but it uses
  22311. ;; `org-back-to-heading', to make it work also in invisible
  22312. ;; trees. And is uses an invisible-ok argument.
  22313. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  22314. ;; Furthermore, when used inside Org, finding the end of a large subtree
  22315. ;; with many children and grandchildren etc, this can be much faster
  22316. ;; than the outline version.
  22317. (org-back-to-heading invisible-ok)
  22318. (let ((first t)
  22319. (level (funcall outline-level)))
  22320. (if (and (derived-mode-p 'org-mode) (< level 1000))
  22321. ;; A true heading (not a plain list item), in Org-mode
  22322. ;; This means we can easily find the end by looking
  22323. ;; only for the right number of stars. Using a regexp to do
  22324. ;; this is so much faster than using a Lisp loop.
  22325. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  22326. (forward-char 1)
  22327. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  22328. ;; something else, do it the slow way
  22329. (while (and (not (eobp))
  22330. (or first (> (funcall outline-level) level)))
  22331. (setq first nil)
  22332. (outline-next-heading)))
  22333. (unless to-heading
  22334. (when (memq (preceding-char) '(?\n ?\^M))
  22335. ;; Go to end of line before heading
  22336. (forward-char -1)
  22337. (when (memq (preceding-char) '(?\n ?\^M))
  22338. ;; leave blank line before heading
  22339. (forward-char -1)))))
  22340. (point))
  22341. (defun org-end-of-meta-data (&optional full)
  22342. "Skip planning line and properties drawer in current entry.
  22343. When optional argument FULL is non-nil, also skip empty lines,
  22344. clocking lines and regular drawers at the beginning of the
  22345. entry."
  22346. (org-back-to-heading t)
  22347. (forward-line)
  22348. (when (org-looking-at-p org-planning-line-re) (forward-line))
  22349. (when (looking-at org-property-drawer-re)
  22350. (goto-char (match-end 0))
  22351. (forward-line))
  22352. (when (and full (not (org-at-heading-p)))
  22353. (catch 'exit
  22354. (let ((end (save-excursion (outline-next-heading) (point)))
  22355. (re (concat "[ \t]*$" "\\|" org-clock-line-re)))
  22356. (while (not (eobp))
  22357. (cond ((org-looking-at-p org-drawer-regexp)
  22358. (if (re-search-forward "^[ \t]*:END:[ \t]*$" end t)
  22359. (forward-line)
  22360. (throw 'exit t)))
  22361. ((org-looking-at-p re) (forward-line))
  22362. (t (throw 'exit t))))))))
  22363. (defun org-forward-heading-same-level (arg &optional invisible-ok)
  22364. "Move forward to the ARG'th subheading at same level as this one.
  22365. Stop at the first and last subheadings of a superior heading.
  22366. Normally this only looks at visible headings, but when INVISIBLE-OK is
  22367. non-nil it will also look at invisible ones."
  22368. (interactive "p")
  22369. (if (not (ignore-errors (org-back-to-heading invisible-ok)))
  22370. (if (and arg (< arg 0))
  22371. (goto-char (point-min))
  22372. (outline-next-heading))
  22373. (org-at-heading-p)
  22374. (let ((level (- (match-end 0) (match-beginning 0) 1))
  22375. (f (if (and arg (< arg 0))
  22376. 're-search-backward
  22377. 're-search-forward))
  22378. (count (if arg (abs arg) 1))
  22379. (result (point)))
  22380. (while (and (prog1 (> count 0)
  22381. (forward-char (if (and arg (< arg 0)) -1 1)))
  22382. (funcall f org-outline-regexp-bol nil 'move))
  22383. (let ((l (- (match-end 0) (match-beginning 0) 1)))
  22384. (cond ((< l level) (setq count 0))
  22385. ((and (= l level)
  22386. (or invisible-ok
  22387. (progn
  22388. (goto-char (line-beginning-position))
  22389. (not (outline-invisible-p)))))
  22390. (setq count (1- count))
  22391. (when (eq l level)
  22392. (setq result (point)))))))
  22393. (goto-char result))
  22394. (beginning-of-line 1)))
  22395. (defun org-backward-heading-same-level (arg &optional invisible-ok)
  22396. "Move backward to the ARG'th subheading at same level as this one.
  22397. Stop at the first and last subheadings of a superior heading."
  22398. (interactive "p")
  22399. (org-forward-heading-same-level (if arg (- arg) -1) invisible-ok))
  22400. (defun org-next-visible-heading (arg)
  22401. "Move to the next visible heading.
  22402. This function wraps `outline-next-visible-heading' with
  22403. `org-with-limited-levels' in order to skip over inline tasks and
  22404. respect customization of `org-odd-levels-only'."
  22405. (interactive "p")
  22406. (org-with-limited-levels
  22407. (outline-next-visible-heading arg)))
  22408. (defun org-previous-visible-heading (arg)
  22409. "Move to the next visible heading.
  22410. This function wraps `outline-previous-visible-heading' with
  22411. `org-with-limited-levels' in order to skip over inline tasks and
  22412. respect customization of `org-odd-levels-only'."
  22413. (interactive "p")
  22414. (org-with-limited-levels
  22415. (outline-previous-visible-heading arg)))
  22416. (defun org-next-block (arg &optional backward block-regexp)
  22417. "Jump to the next block.
  22418. With a prefix argument ARG, jump forward ARG many blocks.
  22419. When BACKWARD is non-nil, jump to the previous block.
  22420. When BLOCK-REGEXP is non-nil, use this regexp to find blocks.
  22421. Match data is set according to this regexp when the function
  22422. returns.
  22423. Return point at beginning of the opening line of found block.
  22424. Throw an error if no block is found."
  22425. (interactive "p")
  22426. (let ((re (or block-regexp "^[ \t]*#\\+BEGIN"))
  22427. (case-fold-search t)
  22428. (search-fn (if backward #'re-search-backward #'re-search-forward))
  22429. (count (or arg 1))
  22430. (origin (point))
  22431. last-element)
  22432. (if backward (beginning-of-line) (end-of-line))
  22433. (while (and (> count 0) (funcall search-fn re nil t))
  22434. (let ((element (save-excursion
  22435. (goto-char (match-beginning 0))
  22436. (save-match-data (org-element-at-point)))))
  22437. (when (and (memq (org-element-type element)
  22438. '(center-block comment-block dynamic-block
  22439. example-block export-block quote-block
  22440. special-block src-block verse-block))
  22441. (<= (match-beginning 0)
  22442. (org-element-property :post-affiliated element)))
  22443. (setq last-element element)
  22444. (decf count))))
  22445. (if (= count 0)
  22446. (prog1 (goto-char (org-element-property :post-affiliated last-element))
  22447. (save-match-data (org-show-context)))
  22448. (goto-char origin)
  22449. (user-error "No %s code blocks" (if backward "previous" "further")))))
  22450. (defun org-previous-block (arg &optional block-regexp)
  22451. "Jump to the previous block.
  22452. With a prefix argument ARG, jump backward ARG many source blocks.
  22453. When BLOCK-REGEXP is non-nil, use this regexp to find blocks."
  22454. (interactive "p")
  22455. (org-next-block arg t block-regexp))
  22456. (defun org-forward-paragraph ()
  22457. "Move forward to beginning of next paragraph or equivalent.
  22458. The function moves point to the beginning of the next visible
  22459. structural element, which can be a paragraph, a table, a list
  22460. item, etc. It also provides some special moves for convenience:
  22461. - On an affiliated keyword, jump to the beginning of the
  22462. relative element.
  22463. - On an item or a footnote definition, move to the second
  22464. element inside, if any.
  22465. - On a table or a property drawer, jump after it.
  22466. - On a verse or source block, stop after blank lines."
  22467. (interactive)
  22468. (when (eobp) (user-error "Cannot move further down"))
  22469. (let* ((deactivate-mark nil)
  22470. (element (org-element-at-point))
  22471. (type (org-element-type element))
  22472. (post-affiliated (org-element-property :post-affiliated element))
  22473. (contents-begin (org-element-property :contents-begin element))
  22474. (contents-end (org-element-property :contents-end element))
  22475. (end (let ((end (org-element-property :end element)) (parent element))
  22476. (while (and (setq parent (org-element-property :parent parent))
  22477. (= (org-element-property :contents-end parent) end))
  22478. (setq end (org-element-property :end parent)))
  22479. end)))
  22480. (cond ((not element)
  22481. (skip-chars-forward " \r\t\n")
  22482. (or (eobp) (beginning-of-line)))
  22483. ;; On affiliated keywords, move to element's beginning.
  22484. ((< (point) post-affiliated)
  22485. (goto-char post-affiliated))
  22486. ;; At a table row, move to the end of the table. Similarly,
  22487. ;; at a node property, move to the end of the property
  22488. ;; drawer.
  22489. ((memq type '(node-property table-row))
  22490. (goto-char (org-element-property
  22491. :end (org-element-property :parent element))))
  22492. ((memq type '(property-drawer table)) (goto-char end))
  22493. ;; Consider blank lines as separators in verse and source
  22494. ;; blocks to ease editing.
  22495. ((memq type '(src-block verse-block))
  22496. (when (eq type 'src-block)
  22497. (setq contents-end
  22498. (save-excursion (goto-char end)
  22499. (skip-chars-backward " \r\t\n")
  22500. (line-beginning-position))))
  22501. (beginning-of-line)
  22502. (when (looking-at "[ \t]*$") (skip-chars-forward " \r\t\n"))
  22503. (if (not (re-search-forward "^[ \t]*$" contents-end t))
  22504. (goto-char end)
  22505. (skip-chars-forward " \r\t\n")
  22506. (if (= (point) contents-end) (goto-char end)
  22507. (beginning-of-line))))
  22508. ;; With no contents, just skip element.
  22509. ((not contents-begin) (goto-char end))
  22510. ;; If contents are invisible, skip the element altogether.
  22511. ((outline-invisible-p (line-end-position))
  22512. (case type
  22513. (headline
  22514. (org-with-limited-levels (outline-next-visible-heading 1)))
  22515. ;; At a plain list, make sure we move to the next item
  22516. ;; instead of skipping the whole list.
  22517. (plain-list (forward-char)
  22518. (org-forward-paragraph))
  22519. (otherwise (goto-char end))))
  22520. ((>= (point) contents-end) (goto-char end))
  22521. ((>= (point) contents-begin)
  22522. ;; This can only happen on paragraphs and plain lists.
  22523. (case type
  22524. (paragraph (goto-char end))
  22525. ;; At a plain list, try to move to second element in
  22526. ;; first item, if possible.
  22527. (plain-list (end-of-line)
  22528. (org-forward-paragraph))))
  22529. ;; When contents start on the middle of a line (e.g. in
  22530. ;; items and footnote definitions), try to reach first
  22531. ;; element starting after current line.
  22532. ((> (line-end-position) contents-begin)
  22533. (end-of-line)
  22534. (org-forward-paragraph))
  22535. (t (goto-char contents-begin)))))
  22536. (defun org-backward-paragraph ()
  22537. "Move backward to start of previous paragraph or equivalent.
  22538. The function moves point to the beginning of the current
  22539. structural element, which can be a paragraph, a table, a list
  22540. item, etc., or to the beginning of the previous visible one if
  22541. point is already there. It also provides some special moves for
  22542. convenience:
  22543. - On an affiliated keyword, jump to the first one.
  22544. - On a table or a property drawer, move to its beginning.
  22545. - On a verse or source block, stop before blank lines."
  22546. (interactive)
  22547. (when (bobp) (user-error "Cannot move further up"))
  22548. (let* ((deactivate-mark nil)
  22549. (element (org-element-at-point))
  22550. (type (org-element-type element))
  22551. (contents-begin (org-element-property :contents-begin element))
  22552. (contents-end (org-element-property :contents-end element))
  22553. (post-affiliated (org-element-property :post-affiliated element))
  22554. (begin (org-element-property :begin element)))
  22555. (cond
  22556. ((not element) (goto-char (point-min)))
  22557. ((= (point) begin)
  22558. (backward-char)
  22559. (org-backward-paragraph))
  22560. ((<= (point) post-affiliated) (goto-char begin))
  22561. ((memq type '(node-property table-row))
  22562. (goto-char (org-element-property
  22563. :post-affiliated (org-element-property :parent element))))
  22564. ((memq type '(property-drawer table)) (goto-char begin))
  22565. ((memq type '(src-block verse-block))
  22566. (when (eq type 'src-block)
  22567. (setq contents-begin
  22568. (save-excursion (goto-char begin) (forward-line) (point))))
  22569. (if (= (point) contents-begin) (goto-char post-affiliated)
  22570. ;; Inside a verse block, see blank lines as paragraph
  22571. ;; separators.
  22572. (let ((origin (point)))
  22573. (skip-chars-backward " \r\t\n" contents-begin)
  22574. (when (re-search-backward "^[ \t]*$" contents-begin 'move)
  22575. (skip-chars-forward " \r\t\n" origin)
  22576. (if (= (point) origin) (goto-char contents-begin)
  22577. (beginning-of-line))))))
  22578. ((not contents-begin) (goto-char (or post-affiliated begin)))
  22579. ((eq type 'paragraph)
  22580. (goto-char contents-begin)
  22581. ;; When at first paragraph in an item or a footnote definition,
  22582. ;; move directly to beginning of line.
  22583. (let ((parent-contents
  22584. (org-element-property
  22585. :contents-begin (org-element-property :parent element))))
  22586. (when (and parent-contents (= parent-contents contents-begin))
  22587. (beginning-of-line))))
  22588. ;; At the end of a greater element, move to the beginning of the
  22589. ;; last element within.
  22590. ((>= (point) contents-end)
  22591. (goto-char (1- contents-end))
  22592. (org-backward-paragraph))
  22593. (t (goto-char (or post-affiliated begin))))
  22594. ;; Ensure we never leave point invisible.
  22595. (when (outline-invisible-p (point)) (beginning-of-visual-line))))
  22596. (defun org-forward-element ()
  22597. "Move forward by one element.
  22598. Move to the next element at the same level, when possible."
  22599. (interactive)
  22600. (cond ((eobp) (user-error "Cannot move further down"))
  22601. ((org-with-limited-levels (org-at-heading-p))
  22602. (let ((origin (point)))
  22603. (goto-char (org-end-of-subtree nil t))
  22604. (unless (org-with-limited-levels (org-at-heading-p))
  22605. (goto-char origin)
  22606. (user-error "Cannot move further down"))))
  22607. (t
  22608. (let* ((elem (org-element-at-point))
  22609. (end (org-element-property :end elem))
  22610. (parent (org-element-property :parent elem)))
  22611. (cond ((and parent (= (org-element-property :contents-end parent) end))
  22612. (goto-char (org-element-property :end parent)))
  22613. ((integer-or-marker-p end) (goto-char end))
  22614. (t (message "No element at point")))))))
  22615. (defun org-backward-element ()
  22616. "Move backward by one element.
  22617. Move to the previous element at the same level, when possible."
  22618. (interactive)
  22619. (cond ((bobp) (user-error "Cannot move further up"))
  22620. ((org-with-limited-levels (org-at-heading-p))
  22621. ;; At a headline, move to the previous one, if any, or stay
  22622. ;; here.
  22623. (let ((origin (point)))
  22624. (org-with-limited-levels (org-backward-heading-same-level 1))
  22625. ;; When current headline has no sibling above, move to its
  22626. ;; parent.
  22627. (when (= (point) origin)
  22628. (or (org-with-limited-levels (org-up-heading-safe))
  22629. (progn (goto-char origin)
  22630. (user-error "Cannot move further up"))))))
  22631. (t
  22632. (let* ((elem (org-element-at-point))
  22633. (beg (org-element-property :begin elem)))
  22634. (cond
  22635. ;; Move to beginning of current element if point isn't
  22636. ;; there already.
  22637. ((null beg) (message "No element at point"))
  22638. ((/= (point) beg) (goto-char beg))
  22639. (t (goto-char beg)
  22640. (skip-chars-backward " \r\t\n")
  22641. (unless (bobp)
  22642. (let ((prev (org-element-at-point)))
  22643. (goto-char (org-element-property :begin prev))
  22644. (while (and (setq prev (org-element-property :parent prev))
  22645. (<= (org-element-property :end prev) beg))
  22646. (goto-char (org-element-property :begin prev)))))))))))
  22647. (defun org-up-element ()
  22648. "Move to upper element."
  22649. (interactive)
  22650. (if (org-with-limited-levels (org-at-heading-p))
  22651. (unless (org-up-heading-safe) (user-error "No surrounding element"))
  22652. (let* ((elem (org-element-at-point))
  22653. (parent (org-element-property :parent elem)))
  22654. (if parent (goto-char (org-element-property :begin parent))
  22655. (if (org-with-limited-levels (org-before-first-heading-p))
  22656. (user-error "No surrounding element")
  22657. (org-with-limited-levels (org-back-to-heading)))))))
  22658. (defvar org-element-greater-elements)
  22659. (defun org-down-element ()
  22660. "Move to inner element."
  22661. (interactive)
  22662. (let ((element (org-element-at-point)))
  22663. (cond
  22664. ((memq (org-element-type element) '(plain-list table))
  22665. (goto-char (org-element-property :contents-begin element))
  22666. (forward-char))
  22667. ((memq (org-element-type element) org-element-greater-elements)
  22668. ;; If contents are hidden, first disclose them.
  22669. (when (outline-invisible-p (line-end-position)) (org-cycle))
  22670. (goto-char (or (org-element-property :contents-begin element)
  22671. (user-error "No content for this element"))))
  22672. (t (user-error "No inner element")))))
  22673. (defun org-drag-element-backward ()
  22674. "Move backward element at point."
  22675. (interactive)
  22676. (if (org-with-limited-levels (org-at-heading-p)) (org-move-subtree-up)
  22677. (let* ((elem (org-element-at-point))
  22678. (prev-elem
  22679. (save-excursion
  22680. (goto-char (org-element-property :begin elem))
  22681. (skip-chars-backward " \r\t\n")
  22682. (unless (bobp)
  22683. (let* ((beg (org-element-property :begin elem))
  22684. (prev (org-element-at-point))
  22685. (up prev))
  22686. (while (and (setq up (org-element-property :parent up))
  22687. (<= (org-element-property :end up) beg))
  22688. (setq prev up))
  22689. prev)))))
  22690. ;; Error out if no previous element or previous element is
  22691. ;; a parent of the current one.
  22692. (if (or (not prev-elem) (org-element-nested-p elem prev-elem))
  22693. (user-error "Cannot drag element backward")
  22694. (let ((pos (point)))
  22695. (org-element-swap-A-B prev-elem elem)
  22696. (goto-char (+ (org-element-property :begin prev-elem)
  22697. (- pos (org-element-property :begin elem)))))))))
  22698. (defun org-drag-element-forward ()
  22699. "Move forward element at point."
  22700. (interactive)
  22701. (let* ((pos (point))
  22702. (elem (org-element-at-point)))
  22703. (when (= (point-max) (org-element-property :end elem))
  22704. (user-error "Cannot drag element forward"))
  22705. (goto-char (org-element-property :end elem))
  22706. (let ((next-elem (org-element-at-point)))
  22707. (when (or (org-element-nested-p elem next-elem)
  22708. (and (eq (org-element-type next-elem) 'headline)
  22709. (not (eq (org-element-type elem) 'headline))))
  22710. (goto-char pos)
  22711. (user-error "Cannot drag element forward"))
  22712. ;; Compute new position of point: it's shifted by NEXT-ELEM
  22713. ;; body's length (without final blanks) and by the length of
  22714. ;; blanks between ELEM and NEXT-ELEM.
  22715. (let ((size-next (- (save-excursion
  22716. (goto-char (org-element-property :end next-elem))
  22717. (skip-chars-backward " \r\t\n")
  22718. (forward-line)
  22719. ;; Small correction if buffer doesn't end
  22720. ;; with a newline character.
  22721. (if (and (eolp) (not (bolp))) (1+ (point)) (point)))
  22722. (org-element-property :begin next-elem)))
  22723. (size-blank (- (org-element-property :end elem)
  22724. (save-excursion
  22725. (goto-char (org-element-property :end elem))
  22726. (skip-chars-backward " \r\t\n")
  22727. (forward-line)
  22728. (point)))))
  22729. (org-element-swap-A-B elem next-elem)
  22730. (goto-char (+ pos size-next size-blank))))))
  22731. (defun org-drag-line-forward (arg)
  22732. "Drag the line at point ARG lines forward."
  22733. (interactive "p")
  22734. (dotimes (n (abs arg))
  22735. (let ((c (current-column)))
  22736. (if (< 0 arg)
  22737. (progn
  22738. (beginning-of-line 2)
  22739. (transpose-lines 1)
  22740. (beginning-of-line 0))
  22741. (transpose-lines 1)
  22742. (beginning-of-line -1))
  22743. (org-move-to-column c))))
  22744. (defun org-drag-line-backward (arg)
  22745. "Drag the line at point ARG lines backward."
  22746. (interactive "p")
  22747. (org-drag-line-forward (- arg)))
  22748. (defun org-mark-element ()
  22749. "Put point at beginning of this element, mark at end.
  22750. Interactively, if this command is repeated or (in Transient Mark
  22751. mode) if the mark is active, it marks the next element after the
  22752. ones already marked."
  22753. (interactive)
  22754. (let (deactivate-mark)
  22755. (if (and (org-called-interactively-p 'any)
  22756. (or (and (eq last-command this-command) (mark t))
  22757. (and transient-mark-mode mark-active)))
  22758. (set-mark
  22759. (save-excursion
  22760. (goto-char (mark))
  22761. (goto-char (org-element-property :end (org-element-at-point)))))
  22762. (let ((element (org-element-at-point)))
  22763. (end-of-line)
  22764. (push-mark (org-element-property :end element) t t)
  22765. (goto-char (org-element-property :begin element))))))
  22766. (defun org-narrow-to-element ()
  22767. "Narrow buffer to current element."
  22768. (interactive)
  22769. (let ((elem (org-element-at-point)))
  22770. (cond
  22771. ((eq (car elem) 'headline)
  22772. (narrow-to-region
  22773. (org-element-property :begin elem)
  22774. (org-element-property :end elem)))
  22775. ((memq (car elem) org-element-greater-elements)
  22776. (narrow-to-region
  22777. (org-element-property :contents-begin elem)
  22778. (org-element-property :contents-end elem)))
  22779. (t
  22780. (narrow-to-region
  22781. (org-element-property :begin elem)
  22782. (org-element-property :end elem))))))
  22783. (defun org-transpose-element ()
  22784. "Transpose current and previous elements, keeping blank lines between.
  22785. Point is moved after both elements."
  22786. (interactive)
  22787. (org-skip-whitespace)
  22788. (let ((end (org-element-property :end (org-element-at-point))))
  22789. (org-drag-element-backward)
  22790. (goto-char end)))
  22791. (defun org-unindent-buffer ()
  22792. "Un-indent the visible part of the buffer.
  22793. Relative indentation (between items, inside blocks, etc.) isn't
  22794. modified."
  22795. (interactive)
  22796. (unless (eq major-mode 'org-mode)
  22797. (user-error "Cannot un-indent a buffer not in Org mode"))
  22798. (let* ((parse-tree (org-element-parse-buffer 'greater-element))
  22799. unindent-tree ; For byte-compiler.
  22800. (unindent-tree
  22801. (function
  22802. (lambda (contents)
  22803. (mapc
  22804. (lambda (element)
  22805. (if (memq (org-element-type element) '(headline section))
  22806. (funcall unindent-tree (org-element-contents element))
  22807. (save-excursion
  22808. (save-restriction
  22809. (narrow-to-region
  22810. (org-element-property :begin element)
  22811. (org-element-property :end element))
  22812. (org-do-remove-indentation)))))
  22813. (reverse contents))))))
  22814. (funcall unindent-tree (org-element-contents parse-tree))))
  22815. (defun org-show-children (&optional level)
  22816. "Show all direct subheadings of this heading.
  22817. Prefix arg LEVEL is how many levels below the current level
  22818. should be shown. Default is enough to cause the following
  22819. heading to appear."
  22820. (interactive "p")
  22821. ;; If `orgstruct-mode' is active, use the slower version.
  22822. (if orgstruct-mode (call-interactively #'outline-show-children)
  22823. (save-excursion
  22824. (org-back-to-heading t)
  22825. (let* ((current-level (funcall outline-level))
  22826. (max-level (org-get-valid-level
  22827. current-level
  22828. (if level (prefix-numeric-value level) 1)))
  22829. (end (save-excursion (org-end-of-subtree t t)))
  22830. (regexp-fmt "^\\*\\{%d,%s\\}\\(?: \\|$\\)")
  22831. (past-first-child nil)
  22832. ;; Make sure to skip inlinetasks.
  22833. (re (format regexp-fmt
  22834. current-level
  22835. (cond
  22836. ((not (featurep 'org-inlinetask)) "")
  22837. (org-odd-levels-only (- (* 2 org-inlinetask-min-level)
  22838. 3))
  22839. (t (1- org-inlinetask-min-level))))))
  22840. ;; Display parent heading.
  22841. (outline-flag-region (line-end-position 0) (line-end-position) nil)
  22842. (forward-line)
  22843. ;; Display children. First child may be deeper than expected
  22844. ;; MAX-LEVEL. Since we want to display it anyway, adjust
  22845. ;; MAX-LEVEL accordingly.
  22846. (while (re-search-forward re end t)
  22847. (unless past-first-child
  22848. (setq re (format regexp-fmt
  22849. current-level
  22850. (max (funcall outline-level) max-level)))
  22851. (setq past-first-child t))
  22852. (outline-flag-region
  22853. (line-end-position 0) (line-end-position) nil))))))
  22854. (defun org-show-subtree ()
  22855. "Show everything after this heading at deeper levels."
  22856. (interactive)
  22857. (outline-flag-region
  22858. (point)
  22859. (save-excursion
  22860. (org-end-of-subtree t t))
  22861. nil))
  22862. (defun org-show-entry ()
  22863. "Show the body directly following this heading.
  22864. Show the heading too, if it is currently invisible."
  22865. (interactive)
  22866. (save-excursion
  22867. (ignore-errors
  22868. (org-back-to-heading t)
  22869. (outline-flag-region
  22870. (max (point-min) (1- (point)))
  22871. (save-excursion
  22872. (if (re-search-forward
  22873. (concat "[\r\n]\\(" org-outline-regexp "\\)") nil t)
  22874. (match-beginning 1)
  22875. (point-max)))
  22876. nil)
  22877. (org-cycle-hide-drawers 'children))))
  22878. (defun org-make-options-regexp (kwds &optional extra)
  22879. "Make a regular expression for keyword lines.
  22880. KWDS is a list of keywords, as strings. Optional argument EXTRA,
  22881. when non-nil, is a regexp matching keywords names."
  22882. (concat "^[ \t]*#\\+\\("
  22883. (regexp-opt kwds)
  22884. (and extra (concat (and kwds "\\|") extra))
  22885. "\\):[ \t]*\\(.*\\)"))
  22886. ;; Make isearch reveal the necessary context
  22887. (defun org-isearch-end ()
  22888. "Reveal context after isearch exits."
  22889. (when isearch-success ; only if search was successful
  22890. (if (featurep 'xemacs)
  22891. ;; Under XEmacs, the hook is run in the correct place,
  22892. ;; we directly show the context.
  22893. (org-show-context 'isearch)
  22894. ;; In Emacs the hook runs *before* restoring the overlays.
  22895. ;; So we have to use a one-time post-command-hook to do this.
  22896. ;; (Emacs 22 has a special variable, see function `org-mode')
  22897. (unless (and (boundp 'isearch-mode-end-hook-quit)
  22898. isearch-mode-end-hook-quit)
  22899. ;; Only when the isearch was not quitted.
  22900. (org-add-hook 'post-command-hook 'org-isearch-post-command
  22901. 'append 'local)))))
  22902. (defun org-isearch-post-command ()
  22903. "Remove self from hook, and show context."
  22904. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  22905. (org-show-context 'isearch))
  22906. ;;;; Integration with and fixes for other packages
  22907. ;;; Imenu support
  22908. (defvar-local org-imenu-markers nil
  22909. "All markers currently used by Imenu.")
  22910. (defun org-imenu-new-marker (&optional pos)
  22911. "Return a new marker for use by Imenu, and remember the marker."
  22912. (let ((m (make-marker)))
  22913. (move-marker m (or pos (point)))
  22914. (push m org-imenu-markers)
  22915. m))
  22916. (defun org-imenu-get-tree ()
  22917. "Produce the index for Imenu."
  22918. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  22919. (setq org-imenu-markers nil)
  22920. (let* ((n org-imenu-depth)
  22921. (re (concat "^" (org-get-limited-outline-regexp)))
  22922. (subs (make-vector (1+ n) nil))
  22923. (last-level 0)
  22924. m level head0 head)
  22925. (save-excursion
  22926. (save-restriction
  22927. (widen)
  22928. (goto-char (point-max))
  22929. (while (re-search-backward re nil t)
  22930. (setq level (org-reduced-level (funcall outline-level)))
  22931. (when (and (<= level n)
  22932. (looking-at org-complex-heading-regexp)
  22933. (setq head0 (org-match-string-no-properties 4)))
  22934. (setq head (org-link-display-format head0)
  22935. m (org-imenu-new-marker))
  22936. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  22937. (if (>= level last-level)
  22938. (push (cons head m) (aref subs level))
  22939. (push (cons head (aref subs (1+ level))) (aref subs level))
  22940. (loop for i from (1+ level) to n do (aset subs i nil)))
  22941. (setq last-level level)))))
  22942. (aref subs 1)))
  22943. (eval-after-load "imenu"
  22944. '(progn
  22945. (add-hook 'imenu-after-jump-hook
  22946. (lambda ()
  22947. (when (derived-mode-p 'org-mode)
  22948. (org-show-context 'org-goto))))))
  22949. (defun org-link-display-format (link)
  22950. "Replace a link with its the description.
  22951. If there is no description, use the link target."
  22952. (save-match-data
  22953. (if (string-match org-bracket-link-analytic-regexp link)
  22954. (replace-match (if (match-end 5)
  22955. (match-string 5 link)
  22956. (concat (match-string 1 link)
  22957. (match-string 3 link)))
  22958. nil t link)
  22959. link)))
  22960. (defun org-toggle-link-display ()
  22961. "Toggle the literal or descriptive display of links."
  22962. (interactive)
  22963. (if org-descriptive-links
  22964. (progn (org-remove-from-invisibility-spec '(org-link))
  22965. (org-restart-font-lock)
  22966. (setq org-descriptive-links nil))
  22967. (progn (add-to-invisibility-spec '(org-link))
  22968. (org-restart-font-lock)
  22969. (setq org-descriptive-links t))))
  22970. ;; Speedbar support
  22971. (defvar org-speedbar-restriction-lock-overlay (make-overlay 1 1)
  22972. "Overlay marking the agenda restriction line in speedbar.")
  22973. (overlay-put org-speedbar-restriction-lock-overlay
  22974. 'face 'org-agenda-restriction-lock)
  22975. (overlay-put org-speedbar-restriction-lock-overlay
  22976. 'help-echo "Agendas are currently limited to this item.")
  22977. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  22978. (defun org-speedbar-set-agenda-restriction ()
  22979. "Restrict future agenda commands to the location at point in speedbar.
  22980. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  22981. (interactive)
  22982. (require 'org-agenda)
  22983. (let (p m tp np dir txt)
  22984. (cond
  22985. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  22986. 'org-imenu t))
  22987. (setq m (get-text-property p 'org-imenu-marker))
  22988. (with-current-buffer (marker-buffer m)
  22989. (goto-char m)
  22990. (org-agenda-set-restriction-lock 'subtree)))
  22991. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  22992. 'speedbar-function 'speedbar-find-file))
  22993. (setq tp (previous-single-property-change
  22994. (1+ p) 'speedbar-function)
  22995. np (next-single-property-change
  22996. tp 'speedbar-function)
  22997. dir (speedbar-line-directory)
  22998. txt (buffer-substring-no-properties (or tp (point-min))
  22999. (or np (point-max))))
  23000. (with-current-buffer (find-file-noselect
  23001. (let ((default-directory dir))
  23002. (expand-file-name txt)))
  23003. (unless (derived-mode-p 'org-mode)
  23004. (user-error "Cannot restrict to non-Org-mode file"))
  23005. (org-agenda-set-restriction-lock 'file)))
  23006. (t (user-error "Don't know how to restrict Org-mode's agenda")))
  23007. (move-overlay org-speedbar-restriction-lock-overlay
  23008. (point-at-bol) (point-at-eol))
  23009. (setq current-prefix-arg nil)
  23010. (org-agenda-maybe-redo)))
  23011. (defvar speedbar-file-key-map)
  23012. (declare-function speedbar-add-supported-extension "speedbar" (extension))
  23013. (eval-after-load "speedbar"
  23014. '(progn
  23015. (speedbar-add-supported-extension ".org")
  23016. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  23017. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  23018. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  23019. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  23020. (add-hook 'speedbar-visiting-tag-hook
  23021. (lambda () (and (derived-mode-p 'org-mode) (org-show-context 'org-goto))))))
  23022. ;;; Fixes and Hacks for problems with other packages
  23023. (defun org--flyspell-object-check-p (element)
  23024. "Non-nil when Flyspell can check object at point.
  23025. ELEMENT is the element at point."
  23026. (let ((object (save-excursion
  23027. (when (org-looking-at-p "\\>") (backward-char))
  23028. (org-element-context element))))
  23029. (case (org-element-type object)
  23030. ;; Prevent checks in links due to keybinding conflict with
  23031. ;; Flyspell.
  23032. ((code entity export-snippet inline-babel-call
  23033. inline-src-block line-break latex-fragment link macro
  23034. statistics-cookie target timestamp verbatim)
  23035. nil)
  23036. (footnote-reference
  23037. ;; Only in inline footnotes, within the definition.
  23038. (and (eq (org-element-property :type object) 'inline)
  23039. (< (save-excursion
  23040. (goto-char (org-element-property :begin object))
  23041. (search-forward ":" nil t 2))
  23042. (point))))
  23043. (otherwise t))))
  23044. (defun org-mode-flyspell-verify ()
  23045. "Function used for `flyspell-generic-check-word-predicate'."
  23046. (if (org-at-heading-p)
  23047. ;; At a headline or an inlinetask, check title only. This is
  23048. ;; faster than relying on `org-element-at-point'.
  23049. (and (save-excursion (beginning-of-line)
  23050. (and (let ((case-fold-search t))
  23051. (not (looking-at "\\*+ END[ \t]*$")))
  23052. (looking-at org-complex-heading-regexp)))
  23053. (match-beginning 4)
  23054. (>= (point) (match-beginning 4))
  23055. (or (not (match-beginning 5))
  23056. (< (point) (match-beginning 5))))
  23057. (let* ((element (org-element-at-point))
  23058. (post-affiliated (org-element-property :post-affiliated element)))
  23059. (cond
  23060. ;; Ignore checks in all affiliated keywords but captions.
  23061. ((< (point) post-affiliated)
  23062. (and (save-excursion
  23063. (beginning-of-line)
  23064. (let ((case-fold-search t)) (looking-at "[ \t]*#\\+CAPTION:")))
  23065. (> (point) (match-end 0))
  23066. (org--flyspell-object-check-p element)))
  23067. ;; Ignore checks in LOGBOOK (or equivalent) drawer.
  23068. ((let ((log (org-log-into-drawer)))
  23069. (and log
  23070. (let ((drawer (org-element-lineage element '(drawer))))
  23071. (and drawer
  23072. (eq (compare-strings
  23073. log nil nil
  23074. (org-element-property :drawer-name drawer) nil nil t)
  23075. t)))))
  23076. nil)
  23077. (t
  23078. (case (org-element-type element)
  23079. ((comment quote-section) t)
  23080. (comment-block
  23081. ;; Allow checks between block markers, not on them.
  23082. (and (> (line-beginning-position) post-affiliated)
  23083. (save-excursion
  23084. (end-of-line)
  23085. (skip-chars-forward " \r\t\n")
  23086. (< (point) (org-element-property :end element)))))
  23087. ;; Arbitrary list of keywords where checks are meaningful.
  23088. ;; Make sure point is on the value part of the element.
  23089. (keyword
  23090. (and (member (org-element-property :key element)
  23091. '("DESCRIPTION" "TITLE"))
  23092. (save-excursion
  23093. (search-backward ":" (line-beginning-position) t))))
  23094. ;; Check is globally allowed in paragraphs verse blocks and
  23095. ;; table rows (after affiliated keywords) but some objects
  23096. ;; must not be affected.
  23097. ((paragraph table-row verse-block)
  23098. (let ((cbeg (org-element-property :contents-begin element))
  23099. (cend (org-element-property :contents-end element)))
  23100. (and cbeg (>= (point) cbeg) (< (point) cend)
  23101. (org--flyspell-object-check-p element))))))))))
  23102. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  23103. (defun org-remove-flyspell-overlays-in (beg end)
  23104. "Remove flyspell overlays in region."
  23105. (and (org-bound-and-true-p flyspell-mode)
  23106. (fboundp 'flyspell-delete-region-overlays)
  23107. (flyspell-delete-region-overlays beg end)))
  23108. (defvar flyspell-delayed-commands)
  23109. (eval-after-load "flyspell"
  23110. '(add-to-list 'flyspell-delayed-commands 'org-self-insert-command))
  23111. ;; Make `bookmark-jump' shows the jump location if it was hidden.
  23112. (eval-after-load "bookmark"
  23113. '(if (boundp 'bookmark-after-jump-hook)
  23114. ;; We can use the hook
  23115. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  23116. ;; Hook not available, use advice
  23117. (defadvice bookmark-jump (after org-make-visible activate)
  23118. "Make the position visible."
  23119. (org-bookmark-jump-unhide))))
  23120. ;; Make sure saveplace shows the location if it was hidden
  23121. (eval-after-load "saveplace"
  23122. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  23123. "Make the position visible."
  23124. (org-bookmark-jump-unhide)))
  23125. ;; Make sure ecb shows the location if it was hidden
  23126. (eval-after-load "ecb"
  23127. '(defadvice ecb-method-clicked (after esf/org-show-context activate)
  23128. "Make hierarchy visible when jumping into location from ECB tree buffer."
  23129. (when (derived-mode-p 'org-mode)
  23130. (org-show-context))))
  23131. (defun org-bookmark-jump-unhide ()
  23132. "Unhide the current position, to show the bookmark location."
  23133. (and (derived-mode-p 'org-mode)
  23134. (or (outline-invisible-p)
  23135. (save-excursion (goto-char (max (point-min) (1- (point))))
  23136. (outline-invisible-p)))
  23137. (org-show-context 'bookmark-jump)))
  23138. (defun org-mark-jump-unhide ()
  23139. "Make the point visible with `org-show-context' after jumping to the mark."
  23140. (when (and (derived-mode-p 'org-mode)
  23141. (outline-invisible-p))
  23142. (org-show-context 'mark-goto)))
  23143. (eval-after-load "simple"
  23144. '(defadvice pop-to-mark-command (after org-make-visible activate)
  23145. "Make the point visible with `org-show-context'."
  23146. (org-mark-jump-unhide)))
  23147. (eval-after-load "simple"
  23148. '(defadvice exchange-point-and-mark (after org-make-visible activate)
  23149. "Make the point visible with `org-show-context'."
  23150. (org-mark-jump-unhide)))
  23151. (eval-after-load "simple"
  23152. '(defadvice pop-global-mark (after org-make-visible activate)
  23153. "Make the point visible with `org-show-context'."
  23154. (org-mark-jump-unhide)))
  23155. ;; Make session.el ignore our circular variable
  23156. (defvar session-globals-exclude)
  23157. (eval-after-load "session"
  23158. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  23159. ;;;; Finish up
  23160. (provide 'org)
  23161. (run-hooks 'org-load-hook)
  23162. ;;; org.el ends here