org.el 957 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545175461754717548175491755017551175521755317554175551755617557175581755917560175611756217563175641756517566175671756817569175701757117572175731757417575175761757717578175791758017581175821758317584175851758617587175881758917590175911759217593175941759517596175971759817599176001760117602176031760417605176061760717608176091761017611176121761317614176151761617617176181761917620176211762217623176241762517626176271762817629176301763117632176331763417635176361763717638176391764017641176421764317644176451764617647176481764917650176511765217653176541765517656176571765817659176601766117662176631766417665176661766717668176691767017671176721767317674176751767617677176781767917680176811768217683176841768517686176871768817689176901769117692176931769417695176961769717698176991770017701177021770317704177051770617707177081770917710177111771217713177141771517716177171771817719177201772117722177231772417725177261772717728177291773017731177321773317734177351773617737177381773917740177411774217743177441774517746177471774817749177501775117752177531775417755177561775717758177591776017761177621776317764177651776617767177681776917770177711777217773177741777517776177771777817779177801778117782177831778417785177861778717788177891779017791177921779317794177951779617797177981779917800178011780217803178041780517806178071780817809178101781117812178131781417815178161781717818178191782017821178221782317824178251782617827178281782917830178311783217833178341783517836178371783817839178401784117842178431784417845178461784717848178491785017851178521785317854178551785617857178581785917860178611786217863178641786517866178671786817869178701787117872178731787417875178761787717878178791788017881178821788317884178851788617887178881788917890178911789217893178941789517896178971789817899179001790117902179031790417905179061790717908179091791017911179121791317914179151791617917179181791917920179211792217923179241792517926179271792817929179301793117932179331793417935179361793717938179391794017941179421794317944179451794617947179481794917950179511795217953179541795517956179571795817959179601796117962179631796417965179661796717968179691797017971179721797317974179751797617977179781797917980179811798217983179841798517986179871798817989179901799117992179931799417995179961799717998179991800018001180021800318004180051800618007180081800918010180111801218013180141801518016180171801818019180201802118022180231802418025180261802718028180291803018031180321803318034180351803618037180381803918040180411804218043180441804518046180471804818049180501805118052180531805418055180561805718058180591806018061180621806318064180651806618067180681806918070180711807218073180741807518076180771807818079180801808118082180831808418085180861808718088180891809018091180921809318094180951809618097180981809918100181011810218103181041810518106181071810818109181101811118112181131811418115181161811718118181191812018121181221812318124181251812618127181281812918130181311813218133181341813518136181371813818139181401814118142181431814418145181461814718148181491815018151181521815318154181551815618157181581815918160181611816218163181641816518166181671816818169181701817118172181731817418175181761817718178181791818018181181821818318184181851818618187181881818918190181911819218193181941819518196181971819818199182001820118202182031820418205182061820718208182091821018211182121821318214182151821618217182181821918220182211822218223182241822518226182271822818229182301823118232182331823418235182361823718238182391824018241182421824318244182451824618247182481824918250182511825218253182541825518256182571825818259182601826118262182631826418265182661826718268182691827018271182721827318274182751827618277182781827918280182811828218283182841828518286182871828818289182901829118292182931829418295182961829718298182991830018301183021830318304183051830618307183081830918310183111831218313183141831518316183171831818319183201832118322183231832418325183261832718328183291833018331183321833318334183351833618337183381833918340183411834218343183441834518346183471834818349183501835118352183531835418355183561835718358183591836018361183621836318364183651836618367183681836918370183711837218373183741837518376183771837818379183801838118382183831838418385183861838718388183891839018391183921839318394183951839618397183981839918400184011840218403184041840518406184071840818409184101841118412184131841418415184161841718418184191842018421184221842318424184251842618427184281842918430184311843218433184341843518436184371843818439184401844118442184431844418445184461844718448184491845018451184521845318454184551845618457184581845918460184611846218463184641846518466184671846818469184701847118472184731847418475184761847718478184791848018481184821848318484184851848618487184881848918490184911849218493184941849518496184971849818499185001850118502185031850418505185061850718508185091851018511185121851318514185151851618517185181851918520185211852218523185241852518526185271852818529185301853118532185331853418535185361853718538185391854018541185421854318544185451854618547185481854918550185511855218553185541855518556185571855818559185601856118562185631856418565185661856718568185691857018571185721857318574185751857618577185781857918580185811858218583185841858518586185871858818589185901859118592185931859418595185961859718598185991860018601186021860318604186051860618607186081860918610186111861218613186141861518616186171861818619186201862118622186231862418625186261862718628186291863018631186321863318634186351863618637186381863918640186411864218643186441864518646186471864818649186501865118652186531865418655186561865718658186591866018661186621866318664186651866618667186681866918670186711867218673186741867518676186771867818679186801868118682186831868418685186861868718688186891869018691186921869318694186951869618697186981869918700187011870218703187041870518706187071870818709187101871118712187131871418715187161871718718187191872018721187221872318724187251872618727187281872918730187311873218733187341873518736187371873818739187401874118742187431874418745187461874718748187491875018751187521875318754187551875618757187581875918760187611876218763187641876518766187671876818769187701877118772187731877418775187761877718778187791878018781187821878318784187851878618787187881878918790187911879218793187941879518796187971879818799188001880118802188031880418805188061880718808188091881018811188121881318814188151881618817188181881918820188211882218823188241882518826188271882818829188301883118832188331883418835188361883718838188391884018841188421884318844188451884618847188481884918850188511885218853188541885518856188571885818859188601886118862188631886418865188661886718868188691887018871188721887318874188751887618877188781887918880188811888218883188841888518886188871888818889188901889118892188931889418895188961889718898188991890018901189021890318904189051890618907189081890918910189111891218913189141891518916189171891818919189201892118922189231892418925189261892718928189291893018931189321893318934189351893618937189381893918940189411894218943189441894518946189471894818949189501895118952189531895418955189561895718958189591896018961189621896318964189651896618967189681896918970189711897218973189741897518976189771897818979189801898118982189831898418985189861898718988189891899018991189921899318994189951899618997189981899919000190011900219003190041900519006190071900819009190101901119012190131901419015190161901719018190191902019021190221902319024190251902619027190281902919030190311903219033190341903519036190371903819039190401904119042190431904419045190461904719048190491905019051190521905319054190551905619057190581905919060190611906219063190641906519066190671906819069190701907119072190731907419075190761907719078190791908019081190821908319084190851908619087190881908919090190911909219093190941909519096190971909819099191001910119102191031910419105191061910719108191091911019111191121911319114191151911619117191181911919120191211912219123191241912519126191271912819129191301913119132191331913419135191361913719138191391914019141191421914319144191451914619147191481914919150191511915219153191541915519156191571915819159191601916119162191631916419165191661916719168191691917019171191721917319174191751917619177191781917919180191811918219183191841918519186191871918819189191901919119192191931919419195191961919719198191991920019201192021920319204192051920619207192081920919210192111921219213192141921519216192171921819219192201922119222192231922419225192261922719228192291923019231192321923319234192351923619237192381923919240192411924219243192441924519246192471924819249192501925119252192531925419255192561925719258192591926019261192621926319264192651926619267192681926919270192711927219273192741927519276192771927819279192801928119282192831928419285192861928719288192891929019291192921929319294192951929619297192981929919300193011930219303193041930519306193071930819309193101931119312193131931419315193161931719318193191932019321193221932319324193251932619327193281932919330193311933219333193341933519336193371933819339193401934119342193431934419345193461934719348193491935019351193521935319354193551935619357193581935919360193611936219363193641936519366193671936819369193701937119372193731937419375193761937719378193791938019381193821938319384193851938619387193881938919390193911939219393193941939519396193971939819399194001940119402194031940419405194061940719408194091941019411194121941319414194151941619417194181941919420194211942219423194241942519426194271942819429194301943119432194331943419435194361943719438194391944019441194421944319444194451944619447194481944919450194511945219453194541945519456194571945819459194601946119462194631946419465194661946719468194691947019471194721947319474194751947619477194781947919480194811948219483194841948519486194871948819489194901949119492194931949419495194961949719498194991950019501195021950319504195051950619507195081950919510195111951219513195141951519516195171951819519195201952119522195231952419525195261952719528195291953019531195321953319534195351953619537195381953919540195411954219543195441954519546195471954819549195501955119552195531955419555195561955719558195591956019561195621956319564195651956619567195681956919570195711957219573195741957519576195771957819579195801958119582195831958419585195861958719588195891959019591195921959319594195951959619597195981959919600196011960219603196041960519606196071960819609196101961119612196131961419615196161961719618196191962019621196221962319624196251962619627196281962919630196311963219633196341963519636196371963819639196401964119642196431964419645196461964719648196491965019651196521965319654196551965619657196581965919660196611966219663196641966519666196671966819669196701967119672196731967419675196761967719678196791968019681196821968319684196851968619687196881968919690196911969219693196941969519696196971969819699197001970119702197031970419705197061970719708197091971019711197121971319714197151971619717197181971919720197211972219723197241972519726197271972819729197301973119732197331973419735197361973719738197391974019741197421974319744197451974619747197481974919750197511975219753197541975519756197571975819759197601976119762197631976419765197661976719768197691977019771197721977319774197751977619777197781977919780197811978219783197841978519786197871978819789197901979119792197931979419795197961979719798197991980019801198021980319804198051980619807198081980919810198111981219813198141981519816198171981819819198201982119822198231982419825198261982719828198291983019831198321983319834198351983619837198381983919840198411984219843198441984519846198471984819849198501985119852198531985419855198561985719858198591986019861198621986319864198651986619867198681986919870198711987219873198741987519876198771987819879198801988119882198831988419885198861988719888198891989019891198921989319894198951989619897198981989919900199011990219903199041990519906199071990819909199101991119912199131991419915199161991719918199191992019921199221992319924199251992619927199281992919930199311993219933199341993519936199371993819939199401994119942199431994419945199461994719948199491995019951199521995319954199551995619957199581995919960199611996219963199641996519966199671996819969199701997119972199731997419975199761997719978199791998019981199821998319984199851998619987199881998919990199911999219993199941999519996199971999819999200002000120002200032000420005200062000720008200092001020011200122001320014200152001620017200182001920020200212002220023200242002520026200272002820029200302003120032200332003420035200362003720038200392004020041200422004320044200452004620047200482004920050200512005220053200542005520056200572005820059200602006120062200632006420065200662006720068200692007020071200722007320074200752007620077200782007920080200812008220083200842008520086200872008820089200902009120092200932009420095200962009720098200992010020101201022010320104201052010620107201082010920110201112011220113201142011520116201172011820119201202012120122201232012420125201262012720128201292013020131201322013320134201352013620137201382013920140201412014220143201442014520146201472014820149201502015120152201532015420155201562015720158201592016020161201622016320164201652016620167201682016920170201712017220173201742017520176201772017820179201802018120182201832018420185201862018720188201892019020191201922019320194201952019620197201982019920200202012020220203202042020520206202072020820209202102021120212202132021420215202162021720218202192022020221202222022320224202252022620227202282022920230202312023220233202342023520236202372023820239202402024120242202432024420245202462024720248202492025020251202522025320254202552025620257202582025920260202612026220263202642026520266202672026820269202702027120272202732027420275202762027720278202792028020281202822028320284202852028620287202882028920290202912029220293202942029520296202972029820299203002030120302203032030420305203062030720308203092031020311203122031320314203152031620317203182031920320203212032220323203242032520326203272032820329203302033120332203332033420335203362033720338203392034020341203422034320344203452034620347203482034920350203512035220353203542035520356203572035820359203602036120362203632036420365203662036720368203692037020371203722037320374203752037620377203782037920380203812038220383203842038520386203872038820389203902039120392203932039420395203962039720398203992040020401204022040320404204052040620407204082040920410204112041220413204142041520416204172041820419204202042120422204232042420425204262042720428204292043020431204322043320434204352043620437204382043920440204412044220443204442044520446204472044820449204502045120452204532045420455204562045720458204592046020461204622046320464204652046620467204682046920470204712047220473204742047520476204772047820479204802048120482204832048420485204862048720488204892049020491204922049320494204952049620497204982049920500205012050220503205042050520506205072050820509205102051120512205132051420515205162051720518205192052020521205222052320524205252052620527205282052920530205312053220533205342053520536205372053820539205402054120542205432054420545205462054720548205492055020551205522055320554205552055620557205582055920560205612056220563205642056520566205672056820569205702057120572205732057420575205762057720578205792058020581205822058320584205852058620587205882058920590205912059220593205942059520596205972059820599206002060120602206032060420605206062060720608206092061020611206122061320614206152061620617206182061920620206212062220623206242062520626206272062820629206302063120632206332063420635206362063720638206392064020641206422064320644206452064620647206482064920650206512065220653206542065520656206572065820659206602066120662206632066420665206662066720668206692067020671206722067320674206752067620677206782067920680206812068220683206842068520686206872068820689206902069120692206932069420695206962069720698206992070020701207022070320704207052070620707207082070920710207112071220713207142071520716207172071820719207202072120722207232072420725207262072720728207292073020731207322073320734207352073620737207382073920740207412074220743207442074520746207472074820749207502075120752207532075420755207562075720758207592076020761207622076320764207652076620767207682076920770207712077220773207742077520776207772077820779207802078120782207832078420785207862078720788207892079020791207922079320794207952079620797207982079920800208012080220803208042080520806208072080820809208102081120812208132081420815208162081720818208192082020821208222082320824208252082620827208282082920830208312083220833208342083520836208372083820839208402084120842208432084420845208462084720848208492085020851208522085320854208552085620857208582085920860208612086220863208642086520866208672086820869208702087120872208732087420875208762087720878208792088020881208822088320884208852088620887208882088920890208912089220893208942089520896208972089820899209002090120902209032090420905209062090720908209092091020911209122091320914209152091620917209182091920920209212092220923209242092520926209272092820929209302093120932209332093420935209362093720938209392094020941209422094320944209452094620947209482094920950209512095220953209542095520956209572095820959209602096120962209632096420965209662096720968209692097020971209722097320974209752097620977209782097920980209812098220983209842098520986209872098820989209902099120992209932099420995209962099720998209992100021001210022100321004210052100621007210082100921010210112101221013210142101521016210172101821019210202102121022210232102421025210262102721028210292103021031210322103321034210352103621037210382103921040210412104221043210442104521046210472104821049210502105121052210532105421055210562105721058210592106021061210622106321064210652106621067210682106921070210712107221073210742107521076210772107821079210802108121082210832108421085210862108721088210892109021091210922109321094210952109621097210982109921100211012110221103211042110521106211072110821109211102111121112211132111421115211162111721118211192112021121211222112321124211252112621127211282112921130211312113221133211342113521136211372113821139211402114121142211432114421145211462114721148211492115021151211522115321154211552115621157211582115921160211612116221163211642116521166211672116821169211702117121172211732117421175211762117721178211792118021181211822118321184211852118621187211882118921190211912119221193211942119521196211972119821199212002120121202212032120421205212062120721208212092121021211212122121321214212152121621217212182121921220212212122221223212242122521226212272122821229212302123121232212332123421235212362123721238212392124021241212422124321244212452124621247212482124921250212512125221253212542125521256212572125821259212602126121262212632126421265212662126721268212692127021271212722127321274212752127621277212782127921280212812128221283212842128521286212872128821289212902129121292212932129421295212962129721298212992130021301213022130321304213052130621307213082130921310213112131221313213142131521316213172131821319213202132121322213232132421325213262132721328213292133021331213322133321334213352133621337213382133921340213412134221343213442134521346213472134821349213502135121352213532135421355213562135721358213592136021361213622136321364213652136621367213682136921370213712137221373213742137521376213772137821379213802138121382213832138421385213862138721388213892139021391213922139321394213952139621397213982139921400214012140221403214042140521406214072140821409214102141121412214132141421415214162141721418214192142021421214222142321424214252142621427214282142921430214312143221433214342143521436214372143821439214402144121442214432144421445214462144721448214492145021451214522145321454214552145621457214582145921460214612146221463214642146521466214672146821469214702147121472214732147421475214762147721478214792148021481214822148321484214852148621487214882148921490214912149221493214942149521496214972149821499215002150121502215032150421505215062150721508215092151021511215122151321514215152151621517215182151921520215212152221523215242152521526215272152821529215302153121532215332153421535215362153721538215392154021541215422154321544215452154621547215482154921550215512155221553215542155521556215572155821559215602156121562215632156421565215662156721568215692157021571215722157321574215752157621577215782157921580215812158221583215842158521586215872158821589215902159121592215932159421595215962159721598215992160021601216022160321604216052160621607216082160921610216112161221613216142161521616216172161821619216202162121622216232162421625216262162721628216292163021631216322163321634216352163621637216382163921640216412164221643216442164521646216472164821649216502165121652216532165421655216562165721658216592166021661216622166321664216652166621667216682166921670216712167221673216742167521676216772167821679216802168121682216832168421685216862168721688216892169021691216922169321694216952169621697216982169921700217012170221703217042170521706217072170821709217102171121712217132171421715217162171721718217192172021721217222172321724217252172621727217282172921730217312173221733217342173521736217372173821739217402174121742217432174421745217462174721748217492175021751217522175321754217552175621757217582175921760217612176221763217642176521766217672176821769217702177121772217732177421775217762177721778217792178021781217822178321784217852178621787217882178921790217912179221793217942179521796217972179821799218002180121802218032180421805218062180721808218092181021811218122181321814218152181621817218182181921820218212182221823218242182521826218272182821829218302183121832218332183421835218362183721838218392184021841218422184321844218452184621847218482184921850218512185221853218542185521856218572185821859218602186121862218632186421865218662186721868218692187021871218722187321874218752187621877218782187921880218812188221883218842188521886218872188821889218902189121892218932189421895218962189721898218992190021901219022190321904219052190621907219082190921910219112191221913219142191521916219172191821919219202192121922219232192421925219262192721928219292193021931219322193321934219352193621937219382193921940219412194221943219442194521946219472194821949219502195121952219532195421955219562195721958219592196021961219622196321964219652196621967219682196921970219712197221973219742197521976219772197821979219802198121982219832198421985219862198721988219892199021991219922199321994219952199621997219982199922000220012200222003220042200522006220072200822009220102201122012220132201422015220162201722018220192202022021220222202322024220252202622027220282202922030220312203222033220342203522036220372203822039220402204122042220432204422045220462204722048220492205022051220522205322054220552205622057220582205922060220612206222063220642206522066220672206822069220702207122072220732207422075220762207722078220792208022081220822208322084220852208622087220882208922090220912209222093220942209522096220972209822099221002210122102221032210422105221062210722108221092211022111221122211322114221152211622117221182211922120221212212222123221242212522126221272212822129221302213122132221332213422135221362213722138221392214022141221422214322144221452214622147221482214922150221512215222153221542215522156221572215822159221602216122162221632216422165221662216722168221692217022171221722217322174221752217622177221782217922180221812218222183221842218522186221872218822189221902219122192221932219422195221962219722198221992220022201222022220322204222052220622207222082220922210222112221222213222142221522216222172221822219222202222122222222232222422225222262222722228222292223022231222322223322234222352223622237222382223922240222412224222243222442224522246222472224822249222502225122252222532225422255222562225722258222592226022261222622226322264222652226622267222682226922270222712227222273222742227522276222772227822279222802228122282222832228422285222862228722288222892229022291222922229322294222952229622297222982229922300223012230222303223042230522306223072230822309223102231122312223132231422315223162231722318223192232022321223222232322324223252232622327223282232922330223312233222333223342233522336223372233822339223402234122342223432234422345223462234722348223492235022351223522235322354223552235622357223582235922360223612236222363223642236522366223672236822369223702237122372223732237422375223762237722378223792238022381223822238322384223852238622387223882238922390223912239222393223942239522396223972239822399224002240122402224032240422405224062240722408224092241022411224122241322414224152241622417224182241922420224212242222423224242242522426224272242822429224302243122432224332243422435224362243722438224392244022441224422244322444224452244622447224482244922450224512245222453224542245522456224572245822459224602246122462224632246422465224662246722468224692247022471224722247322474224752247622477224782247922480224812248222483224842248522486224872248822489224902249122492224932249422495224962249722498224992250022501225022250322504225052250622507225082250922510225112251222513225142251522516225172251822519225202252122522225232252422525225262252722528225292253022531225322253322534225352253622537225382253922540225412254222543225442254522546225472254822549225502255122552225532255422555225562255722558225592256022561225622256322564225652256622567225682256922570225712257222573225742257522576225772257822579225802258122582225832258422585225862258722588225892259022591225922259322594225952259622597225982259922600226012260222603226042260522606226072260822609226102261122612226132261422615226162261722618226192262022621226222262322624226252262622627226282262922630226312263222633226342263522636226372263822639226402264122642226432264422645226462264722648226492265022651226522265322654226552265622657226582265922660226612266222663226642266522666226672266822669226702267122672226732267422675226762267722678226792268022681226822268322684226852268622687226882268922690226912269222693226942269522696226972269822699227002270122702227032270422705227062270722708227092271022711227122271322714227152271622717227182271922720227212272222723227242272522726227272272822729227302273122732227332273422735227362273722738227392274022741227422274322744227452274622747227482274922750227512275222753227542275522756227572275822759227602276122762227632276422765227662276722768227692277022771227722277322774227752277622777227782277922780227812278222783227842278522786227872278822789227902279122792227932279422795227962279722798227992280022801228022280322804228052280622807228082280922810228112281222813228142281522816228172281822819228202282122822228232282422825228262282722828228292283022831228322283322834228352283622837228382283922840228412284222843228442284522846228472284822849228502285122852228532285422855228562285722858228592286022861228622286322864228652286622867228682286922870228712287222873228742287522876228772287822879228802288122882228832288422885228862288722888228892289022891228922289322894228952289622897228982289922900229012290222903229042290522906229072290822909229102291122912229132291422915229162291722918229192292022921229222292322924229252292622927229282292922930229312293222933229342293522936229372293822939229402294122942229432294422945229462294722948229492295022951229522295322954229552295622957229582295922960229612296222963229642296522966229672296822969229702297122972229732297422975229762297722978229792298022981229822298322984229852298622987229882298922990229912299222993229942299522996229972299822999230002300123002230032300423005230062300723008230092301023011230122301323014230152301623017230182301923020230212302223023230242302523026230272302823029230302303123032230332303423035230362303723038230392304023041230422304323044230452304623047230482304923050230512305223053230542305523056230572305823059230602306123062230632306423065230662306723068230692307023071230722307323074230752307623077230782307923080230812308223083230842308523086230872308823089230902309123092230932309423095230962309723098230992310023101231022310323104231052310623107231082310923110231112311223113231142311523116231172311823119231202312123122231232312423125231262312723128231292313023131231322313323134231352313623137231382313923140231412314223143231442314523146231472314823149231502315123152231532315423155231562315723158231592316023161231622316323164231652316623167231682316923170231712317223173231742317523176231772317823179231802318123182231832318423185231862318723188231892319023191231922319323194231952319623197231982319923200232012320223203232042320523206232072320823209232102321123212232132321423215232162321723218232192322023221232222322323224232252322623227232282322923230232312323223233232342323523236232372323823239232402324123242232432324423245232462324723248232492325023251232522325323254232552325623257232582325923260232612326223263232642326523266232672326823269232702327123272232732327423275232762327723278232792328023281232822328323284232852328623287232882328923290232912329223293232942329523296232972329823299233002330123302233032330423305233062330723308233092331023311233122331323314233152331623317233182331923320233212332223323233242332523326233272332823329233302333123332233332333423335233362333723338233392334023341233422334323344233452334623347233482334923350233512335223353233542335523356233572335823359233602336123362233632336423365233662336723368233692337023371233722337323374233752337623377233782337923380233812338223383233842338523386233872338823389233902339123392233932339423395233962339723398233992340023401234022340323404234052340623407234082340923410234112341223413234142341523416234172341823419234202342123422234232342423425234262342723428234292343023431234322343323434234352343623437234382343923440234412344223443234442344523446234472344823449234502345123452234532345423455234562345723458234592346023461234622346323464234652346623467234682346923470234712347223473234742347523476234772347823479234802348123482234832348423485234862348723488234892349023491234922349323494234952349623497234982349923500235012350223503235042350523506235072350823509235102351123512235132351423515235162351723518235192352023521235222352323524235252352623527235282352923530235312353223533235342353523536235372353823539235402354123542235432354423545235462354723548235492355023551235522355323554235552355623557235582355923560235612356223563235642356523566235672356823569235702357123572235732357423575235762357723578235792358023581235822358323584235852358623587235882358923590235912359223593235942359523596235972359823599236002360123602236032360423605236062360723608236092361023611236122361323614236152361623617236182361923620236212362223623236242362523626236272362823629236302363123632236332363423635236362363723638236392364023641236422364323644236452364623647236482364923650236512365223653236542365523656236572365823659236602366123662236632366423665236662366723668236692367023671236722367323674236752367623677236782367923680236812368223683236842368523686236872368823689236902369123692236932369423695236962369723698236992370023701237022370323704237052370623707237082370923710237112371223713237142371523716237172371823719237202372123722237232372423725237262372723728237292373023731237322373323734237352373623737237382373923740237412374223743237442374523746237472374823749237502375123752237532375423755237562375723758237592376023761237622376323764237652376623767237682376923770237712377223773237742377523776237772377823779237802378123782237832378423785237862378723788237892379023791237922379323794237952379623797237982379923800238012380223803238042380523806238072380823809238102381123812238132381423815238162381723818238192382023821238222382323824238252382623827238282382923830238312383223833238342383523836238372383823839238402384123842238432384423845238462384723848238492385023851238522385323854238552385623857238582385923860238612386223863238642386523866238672386823869238702387123872238732387423875238762387723878238792388023881238822388323884238852388623887238882388923890238912389223893238942389523896238972389823899239002390123902239032390423905239062390723908239092391023911239122391323914239152391623917239182391923920239212392223923239242392523926239272392823929239302393123932239332393423935239362393723938239392394023941239422394323944239452394623947239482394923950239512395223953239542395523956239572395823959239602396123962239632396423965239662396723968239692397023971239722397323974239752397623977239782397923980239812398223983239842398523986239872398823989239902399123992239932399423995239962399723998239992400024001240022400324004240052400624007240082400924010240112401224013240142401524016240172401824019240202402124022240232402424025240262402724028240292403024031240322403324034240352403624037240382403924040240412404224043240442404524046240472404824049240502405124052240532405424055240562405724058240592406024061240622406324064240652406624067240682406924070240712407224073240742407524076240772407824079240802408124082240832408424085240862408724088240892409024091240922409324094240952409624097240982409924100241012410224103241042410524106241072410824109241102411124112241132411424115241162411724118241192412024121241222412324124241252412624127241282412924130241312413224133241342413524136241372413824139241402414124142241432414424145241462414724148241492415024151241522415324154241552415624157241582415924160241612416224163241642416524166241672416824169241702417124172241732417424175241762417724178241792418024181241822418324184241852418624187241882418924190241912419224193241942419524196241972419824199242002420124202242032420424205242062420724208242092421024211242122421324214242152421624217242182421924220242212422224223242242422524226242272422824229242302423124232242332423424235242362423724238242392424024241242422424324244242452424624247242482424924250242512425224253242542425524256242572425824259242602426124262242632426424265242662426724268242692427024271242722427324274242752427624277242782427924280242812428224283242842428524286242872428824289242902429124292242932429424295242962429724298242992430024301243022430324304243052430624307243082430924310243112431224313243142431524316243172431824319243202432124322243232432424325243262432724328243292433024331243322433324334243352433624337243382433924340243412434224343243442434524346243472434824349243502435124352243532435424355243562435724358243592436024361243622436324364243652436624367243682436924370243712437224373243742437524376243772437824379243802438124382243832438424385243862438724388243892439024391243922439324394243952439624397243982439924400244012440224403244042440524406244072440824409244102441124412244132441424415244162441724418244192442024421244222442324424244252442624427244282442924430244312443224433244342443524436244372443824439244402444124442244432444424445244462444724448244492445024451244522445324454244552445624457244582445924460244612446224463244642446524466244672446824469244702447124472244732447424475244762447724478244792448024481244822448324484244852448624487244882448924490244912449224493244942449524496244972449824499245002450124502245032450424505245062450724508245092451024511245122451324514245152451624517245182451924520245212452224523245242452524526245272452824529245302453124532245332453424535245362453724538245392454024541245422454324544245452454624547245482454924550245512455224553245542455524556245572455824559245602456124562245632456424565245662456724568245692457024571245722457324574245752457624577245782457924580245812458224583245842458524586245872458824589245902459124592245932459424595245962459724598245992460024601246022460324604246052460624607246082460924610246112461224613246142461524616246172461824619246202462124622246232462424625246262462724628246292463024631246322463324634246352463624637246382463924640246412464224643246442464524646246472464824649246502465124652246532465424655246562465724658246592466024661246622466324664246652466624667246682466924670246712467224673246742467524676246772467824679246802468124682246832468424685246862468724688246892469024691246922469324694246952469624697246982469924700247012470224703247042470524706247072470824709247102471124712247132471424715247162471724718247192472024721247222472324724247252472624727247282472924730247312473224733247342473524736247372473824739247402474124742247432474424745247462474724748247492475024751247522475324754247552475624757247582475924760247612476224763247642476524766247672476824769247702477124772247732477424775247762477724778247792478024781247822478324784247852478624787247882478924790247912479224793247942479524796247972479824799248002480124802248032480424805248062480724808248092481024811248122481324814248152481624817248182481924820248212482224823248242482524826248272482824829248302483124832248332483424835248362483724838248392484024841248422484324844248452484624847248482484924850248512485224853248542485524856248572485824859248602486124862248632486424865248662486724868248692487024871248722487324874248752487624877248782487924880248812488224883248842488524886248872488824889248902489124892248932489424895248962489724898248992490024901249022490324904249052490624907249082490924910249112491224913249142491524916249172491824919249202492124922249232492424925249262492724928249292493024931249322493324934249352493624937249382493924940249412494224943249442494524946249472494824949249502495124952249532495424955249562495724958249592496024961249622496324964249652496624967249682496924970249712497224973249742497524976249772497824979249802498124982249832498424985249862498724988249892499024991249922499324994249952499624997249982499925000250012500225003250042500525006250072500825009250102501125012250132501425015250162501725018250192502025021250222502325024250252502625027250282502925030250312503225033250342503525036250372503825039250402504125042250432504425045250462504725048250492505025051250522505325054250552505625057250582505925060250612506225063250642506525066250672506825069250702507125072250732507425075250762507725078250792508025081250822508325084250852508625087250882508925090250912509225093250942509525096250972509825099251002510125102251032510425105251062510725108251092511025111251122511325114251152511625117251182511925120251212512225123251242512525126251272512825129251302513125132251332513425135251362513725138251392514025141251422514325144251452514625147251482514925150251512515225153251542515525156251572515825159251602516125162251632516425165251662516725168251692517025171251722517325174251752517625177251782517925180251812518225183251842518525186251872518825189251902519125192251932519425195251962519725198251992520025201252022520325204252052520625207252082520925210252112521225213252142521525216252172521825219252202522125222252232522425225252262522725228252292523025231252322523325234252352523625237252382523925240252412524225243252442524525246252472524825249252502525125252252532525425255252562525725258252592526025261252622526325264252652526625267
  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: Bastien Guerry <bzg at gnu dot org>
  7. ;; URL: http://orgmode.org
  8. ;; Version: 8.4-git
  9. ;; Keywords: outlines, hypermedia, calendar, wp
  10. ;; Package-Requires: ((emacs "24.3"))
  11. ;;
  12. ;; This file is part of GNU Emacs.
  13. ;;
  14. ;; GNU Emacs is free software: you can redistribute it and/or modify
  15. ;; it under the terms of the GNU General Public License as published by
  16. ;; the Free Software Foundation, either version 3 of the License, or
  17. ;; (at your option) any later version.
  18. ;; GNU Emacs is distributed in the hope that it will be useful,
  19. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. ;; GNU General Public License for more details.
  22. ;; You should have received a copy of the GNU General Public License
  23. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  24. ;;
  25. ;;; Commentary:
  26. ;;
  27. ;; Org-mode is a mode for keeping notes, maintaining ToDo lists, and doing
  28. ;; project planning with a fast and effective plain-text system.
  29. ;;
  30. ;; Org-mode develops organizational tasks around NOTES files that contain
  31. ;; information about projects as plain text. Org-mode is implemented on
  32. ;; top of outline-mode, which makes it possible to keep the content of
  33. ;; large files well structured. Visibility cycling and structure editing
  34. ;; help to work with the tree. Tables are easily created with a built-in
  35. ;; table editor. Org-mode supports ToDo items, deadlines, time stamps,
  36. ;; and scheduling. It dynamically compiles entries into an agenda that
  37. ;; utilizes and smoothly integrates much of the Emacs calendar and diary.
  38. ;; Plain text URL-like links connect to websites, emails, Usenet
  39. ;; messages, BBDB entries, and any files related to the projects. For
  40. ;; printing and sharing of notes, an Org-mode file can be exported as a
  41. ;; structured ASCII file, as HTML, or (todo and agenda items only) as an
  42. ;; iCalendar file. It can also serve as a publishing tool for a set of
  43. ;; linked webpages.
  44. ;;
  45. ;; Installation and Activation
  46. ;; ---------------------------
  47. ;; See the corresponding sections in the manual at
  48. ;;
  49. ;; http://orgmode.org/org.html#Installation
  50. ;;
  51. ;; Documentation
  52. ;; -------------
  53. ;; The documentation of Org-mode can be found in the TeXInfo file. The
  54. ;; distribution also contains a PDF version of it. At the homepage of
  55. ;; Org-mode, you can read the same text online as HTML. There is also an
  56. ;; excellent reference card made by Philip Rooke. This card can be found
  57. ;; in the etc/ directory of Emacs 22.
  58. ;;
  59. ;; A list of recent changes can be found at
  60. ;; http://orgmode.org/Changes.html
  61. ;;
  62. ;;; Code:
  63. (defvar org-inhibit-highlight-removal nil) ; dynamically scoped param
  64. (defvar org-table-formula-constants-local nil
  65. "Local version of `org-table-formula-constants'.")
  66. (make-variable-buffer-local 'org-table-formula-constants-local)
  67. ;;;; Require other packages
  68. (eval-when-compile
  69. (require 'cl)
  70. (require 'gnus-sum))
  71. (require 'calendar)
  72. (require 'find-func)
  73. (require 'format-spec)
  74. (or (equal this-command 'eval-buffer)
  75. (condition-case nil
  76. (load (concat (file-name-directory load-file-name)
  77. "org-loaddefs.el")
  78. nil t t t)
  79. (error
  80. (message "WARNING: No org-loaddefs.el file could be found from where org.el is loaded.")
  81. (sit-for 3)
  82. (message "You need to run \"make\" or \"make autoloads\" from Org lisp directory")
  83. (sit-for 3))))
  84. (require 'org-macs)
  85. (require 'org-compat)
  86. ;; `org-outline-regexp' ought to be a defconst but is let-bound in
  87. ;; some places -- e.g. see the macro `org-with-limited-levels'.
  88. ;;
  89. ;; In Org buffers, the value of `outline-regexp' is that of
  90. ;; `org-outline-regexp'. The only function still directly relying on
  91. ;; `outline-regexp' is `org-overview' so that `org-cycle' can do its
  92. ;; job when `orgstruct-mode' is active.
  93. (defvar org-outline-regexp "\\*+ "
  94. "Regexp to match Org headlines.")
  95. (defvar org-outline-regexp-bol "^\\*+ "
  96. "Regexp to match Org headlines.
  97. This is similar to `org-outline-regexp' but additionally makes
  98. sure that we are at the beginning of the line.")
  99. (defvar org-heading-regexp "^\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  100. "Matches a headline, putting stars and text into groups.
  101. Stars are put in group 1 and the trimmed body in group 2.")
  102. ;; Emacs 22 calendar compatibility: Make sure the new variables are available
  103. (unless (boundp 'calendar-view-holidays-initially-flag)
  104. (org-defvaralias 'calendar-view-holidays-initially-flag
  105. 'view-calendar-holidays-initially))
  106. (unless (boundp 'calendar-view-diary-initially-flag)
  107. (org-defvaralias 'calendar-view-diary-initially-flag
  108. 'view-diary-entries-initially))
  109. (unless (boundp 'diary-fancy-buffer)
  110. (org-defvaralias 'diary-fancy-buffer 'fancy-diary-buffer))
  111. (declare-function cdlatex-environment "ext:cdlatex" (environment item))
  112. (declare-function org-add-archive-files "org-archive" (files))
  113. (declare-function org-agenda-entry-get-agenda-timestamp "org-agenda" (pom))
  114. (declare-function org-agenda-list "org-agenda" (&optional arg start-day span))
  115. (declare-function org-agenda-redo "org-agenda" (&optional all))
  116. (declare-function org-babel-do-in-edit-buffer "ob-core" (&rest body))
  117. (declare-function org-babel-tangle-file "ob-tangle" (file &optional target-file lang))
  118. (declare-function org-beamer-mode "ox-beamer" ())
  119. (declare-function org-clock-get-last-clock-out-time "org-clock" ())
  120. (declare-function org-clock-out "org-clock" (&optional switch-to-state fail-quietly at-time))
  121. (declare-function org-clock-remove-overlays "org-clock" (&optional beg end noremove))
  122. (declare-function org-clock-sum "org-clock" (&optional tstart tend headline-filter propname))
  123. (declare-function org-clock-sum-current-item "org-clock" (&optional tstart))
  124. (declare-function org-clock-timestamps-down "org-clock" (&optional n))
  125. (declare-function org-clock-timestamps-up "org-clock" (&optional n))
  126. (declare-function org-clock-update-time-maybe "org-clock" ())
  127. (declare-function org-clocktable-shift "org-clock" (dir n))
  128. (declare-function org-element-at-point "org-element" ())
  129. (declare-function org-element-cache-refresh "org-element" (pos))
  130. (declare-function org-element-cache-reset "org-element" (&optional all))
  131. (declare-function org-element-contents "org-element" (element))
  132. (declare-function org-element-context "org-element" (&optional element))
  133. (declare-function org-element-copy "org-element" (datum))
  134. (declare-function org-element-interpret-data "org-element" (data &optional parent))
  135. (declare-function org-element-lineage "org-element" (blob &optional types with-self))
  136. (declare-function org-element-nested-p "org-element" (elem-a elem-b))
  137. (declare-function org-element-parse-buffer "org-element" (&optional granularity visible-only))
  138. (declare-function org-element-property "org-element" (property element))
  139. (declare-function org-element-put-property "org-element" (element property value))
  140. (declare-function org-element-swap-A-B "org-element" (elem-a elem-b))
  141. (declare-function org-element-type "org-element" (element))
  142. (declare-function org-element-update-syntax "org-element" ())
  143. (declare-function org-id-find-id-file "org-id" (id))
  144. (declare-function org-id-get-create "org-id" (&optional force))
  145. (declare-function org-inlinetask-at-task-p "org-inlinetask" ())
  146. (declare-function org-inlinetask-outline-regexp "org-inlinetask" ())
  147. (declare-function org-inlinetask-toggle-visibility "org-inlinetask" ())
  148. (declare-function org-plot/gnuplot "org-plot" (&optional params))
  149. (declare-function org-pop-to-buffer-same-window "org-compat" (&optional buffer-or-name norecord label))
  150. (declare-function org-table-align "org-table" ())
  151. (declare-function org-table-begin "org-table" (&optional table-type))
  152. (declare-function org-table-beginning-of-field "org-table" (&optional n))
  153. (declare-function org-table-blank-field "org-table" ())
  154. (declare-function org-table-calc-current-TBLFM "org-table" (&optional arg))
  155. (declare-function org-table-edit-field "org-table" (arg))
  156. (declare-function org-table-end "org-table" (&optional table-type))
  157. (declare-function org-table-end-of-field "org-table" (&optional n))
  158. (declare-function org-table-insert-row "org-table" (&optional arg))
  159. (declare-function org-table-justify-field-maybe "org-table" (&optional new))
  160. (declare-function org-table-maybe-eval-formula "org-table" ())
  161. (declare-function org-table-maybe-recalculate-line "org-table" ())
  162. (declare-function org-table-next-row "org-table" ())
  163. (declare-function org-table-paste-rectangle "org-table" ())
  164. (declare-function org-table-wrap-region "org-table" (arg))
  165. (declare-function org-tags-view "org-agenda" (&optional todo-only match))
  166. (declare-function orgtbl-ascii-plot "org-table" (&optional ask))
  167. (declare-function orgtbl-mode "org-table" (&optional arg))
  168. (defsubst org-uniquify (list)
  169. "Non-destructively remove duplicate elements from LIST."
  170. (let ((res (copy-sequence list))) (delete-dups res)))
  171. (defsubst org-get-at-bol (property)
  172. "Get text property PROPERTY at the beginning of line."
  173. (get-text-property (point-at-bol) property))
  174. (defsubst org-trim (s)
  175. "Remove whitespace at the beginning and the end of string S."
  176. (replace-regexp-in-string
  177. "\\`[ \t\n\r]+" ""
  178. (replace-regexp-in-string "[ \t\n\r]+\\'" "" s)))
  179. ;; load languages based on value of `org-babel-load-languages'
  180. (defvar org-babel-load-languages)
  181. ;;;###autoload
  182. (defun org-babel-do-load-languages (sym value)
  183. "Load the languages defined in `org-babel-load-languages'."
  184. (set-default sym value)
  185. (mapc (lambda (pair)
  186. (let ((active (cdr pair)) (lang (symbol-name (car pair))))
  187. (if active
  188. (progn
  189. (require (intern (concat "ob-" lang))))
  190. (progn
  191. (funcall 'fmakunbound
  192. (intern (concat "org-babel-execute:" lang)))
  193. (funcall 'fmakunbound
  194. (intern (concat "org-babel-expand-body:" lang)))))))
  195. org-babel-load-languages))
  196. (declare-function org-babel-tangle-file "ob-tangle" (file &optional target-file lang))
  197. ;;;###autoload
  198. (defun org-babel-load-file (file &optional compile)
  199. "Load Emacs Lisp source code blocks in the Org-mode FILE.
  200. This function exports the source code using `org-babel-tangle'
  201. and then loads the resulting file using `load-file'. With prefix
  202. arg (noninteractively: 2nd arg) COMPILE the tangled Emacs Lisp
  203. file to byte-code before it is loaded."
  204. (interactive "fFile to load: \nP")
  205. (let* ((age (lambda (file)
  206. (float-time
  207. (time-subtract (current-time)
  208. (nth 5 (or (file-attributes (file-truename file))
  209. (file-attributes file)))))))
  210. (base-name (file-name-sans-extension file))
  211. (exported-file (concat base-name ".el")))
  212. ;; tangle if the org-mode file is newer than the elisp file
  213. (unless (and (file-exists-p exported-file)
  214. (> (funcall age file) (funcall age exported-file)))
  215. ;; Tangle-file traversal returns reversed list of tangled files
  216. ;; and we want to evaluate the first target.
  217. (setq exported-file
  218. (car (last (org-babel-tangle-file file exported-file "emacs-lisp")))))
  219. (message "%s %s"
  220. (if compile
  221. (progn (byte-compile-file exported-file 'load)
  222. "Compiled and loaded")
  223. (progn (load-file exported-file) "Loaded"))
  224. exported-file)))
  225. (defcustom org-babel-load-languages '((emacs-lisp . t))
  226. "Languages which can be evaluated in Org-mode buffers.
  227. This list can be used to load support for any of the languages
  228. below, note that each language will depend on a different set of
  229. system executables and/or Emacs modes. When a language is
  230. \"loaded\", then code blocks in that language can be evaluated
  231. with `org-babel-execute-src-block' bound by default to C-c
  232. C-c (note the `org-babel-no-eval-on-ctrl-c-ctrl-c' variable can
  233. be set to remove code block evaluation from the C-c C-c
  234. keybinding. By default only Emacs Lisp (which has no
  235. requirements) is loaded."
  236. :group 'org-babel
  237. :set 'org-babel-do-load-languages
  238. :version "24.1"
  239. :type '(alist :tag "Babel Languages"
  240. :key-type
  241. (choice
  242. (const :tag "Awk" awk)
  243. (const :tag "C" C)
  244. (const :tag "R" R)
  245. (const :tag "Asymptote" asymptote)
  246. (const :tag "Calc" calc)
  247. (const :tag "Clojure" clojure)
  248. (const :tag "CSS" css)
  249. (const :tag "Ditaa" ditaa)
  250. (const :tag "Dot" dot)
  251. (const :tag "Emacs Lisp" emacs-lisp)
  252. (const :tag "Forth" forth)
  253. (const :tag "Fortran" fortran)
  254. (const :tag "Gnuplot" gnuplot)
  255. (const :tag "Haskell" haskell)
  256. (const :tag "IO" io)
  257. (const :tag "J" J)
  258. (const :tag "Java" java)
  259. (const :tag "Javascript" js)
  260. (const :tag "LaTeX" latex)
  261. (const :tag "Ledger" ledger)
  262. (const :tag "Lilypond" lilypond)
  263. (const :tag "Lisp" lisp)
  264. (const :tag "Makefile" makefile)
  265. (const :tag "Maxima" maxima)
  266. (const :tag "Matlab" matlab)
  267. (const :tag "Mscgen" mscgen)
  268. (const :tag "Ocaml" ocaml)
  269. (const :tag "Octave" octave)
  270. (const :tag "Org" org)
  271. (const :tag "Perl" perl)
  272. (const :tag "Pico Lisp" picolisp)
  273. (const :tag "PlantUML" plantuml)
  274. (const :tag "Python" python)
  275. (const :tag "Ruby" ruby)
  276. (const :tag "Sass" sass)
  277. (const :tag "Scala" scala)
  278. (const :tag "Scheme" scheme)
  279. (const :tag "Screen" screen)
  280. (const :tag "Shell Script" shell)
  281. (const :tag "Shen" shen)
  282. (const :tag "Sql" sql)
  283. (const :tag "Sqlite" sqlite)
  284. (const :tag "ebnf2ps" ebnf2ps))
  285. :value-type (boolean :tag "Activate" :value t)))
  286. ;;;; Customization variables
  287. (defcustom org-clone-delete-id nil
  288. "Remove ID property of clones of a subtree.
  289. When non-nil, clones of a subtree don't inherit the ID property.
  290. Otherwise they inherit the ID property with a new unique
  291. identifier."
  292. :type 'boolean
  293. :version "24.1"
  294. :group 'org-id)
  295. ;;; Version
  296. (org-check-version)
  297. ;;;###autoload
  298. (defun org-version (&optional here full message)
  299. "Show the org-mode version.
  300. Interactively, or when MESSAGE is non-nil, show it in echo area.
  301. With prefix argument, or when HERE is non-nil, insert it at point.
  302. In non-interactive uses, a reduced version string is output unless
  303. FULL is given."
  304. (interactive (list current-prefix-arg t (not current-prefix-arg)))
  305. (let* ((org-dir (ignore-errors (org-find-library-dir "org")))
  306. (save-load-suffixes (when (boundp 'load-suffixes) load-suffixes))
  307. (load-suffixes (list ".el"))
  308. (org-install-dir (ignore-errors (org-find-library-dir "org-loaddefs")))
  309. (org-trash (or
  310. (and (fboundp 'org-release) (fboundp 'org-git-version))
  311. (org-load-noerror-mustsuffix (concat org-dir "org-version"))))
  312. (load-suffixes save-load-suffixes)
  313. (org-version (org-release))
  314. (git-version (org-git-version))
  315. (version (format "Org-mode version %s (%s @ %s)"
  316. org-version
  317. git-version
  318. (if org-install-dir
  319. (if (string= org-dir org-install-dir)
  320. org-install-dir
  321. (concat "mixed installation! " org-install-dir " and " org-dir))
  322. "org-loaddefs.el can not be found!")))
  323. (version1 (if full version org-version)))
  324. (when here (insert version1))
  325. (when message (message "%s" version1))
  326. version1))
  327. (defconst org-version (org-version))
  328. ;;; Syntax Constants
  329. ;;;; Block
  330. (defconst org-block-regexp
  331. "^[ \t]*#\\+begin_?\\([^ \n]+\\)\\(\\([^\n]+\\)\\)?\n\\([^\000]+?\\)#\\+end_?\\1[ \t]*$"
  332. "Regular expression for hiding blocks.")
  333. (defconst org-dblock-start-re
  334. "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  335. "Matches the start line of a dynamic block, with parameters.")
  336. (defconst org-dblock-end-re "^[ \t]*#\\+\\(?:END\\|end\\)\\([: \t\r\n]\\|$\\)"
  337. "Matches the end of a dynamic block.")
  338. ;;;; Clock and Planning
  339. (defconst org-clock-string "CLOCK:"
  340. "String used as prefix for timestamps clocking work hours on an item.")
  341. (defvar org-closed-string "CLOSED:"
  342. "String used as the prefix for timestamps logging closing a TODO entry.")
  343. (defvar org-deadline-string "DEADLINE:"
  344. "String to mark deadline entries.
  345. A deadline is this string, followed by a time stamp. Should be a word,
  346. terminated by a colon. You can insert a schedule keyword and
  347. a timestamp with \\[org-deadline].")
  348. (defvar org-scheduled-string "SCHEDULED:"
  349. "String to mark scheduled TODO entries.
  350. A schedule is this string, followed by a time stamp. Should be a word,
  351. terminated by a colon. You can insert a schedule keyword and
  352. a timestamp with \\[org-schedule].")
  353. (defconst org-ds-keyword-length
  354. (+ 2
  355. (apply #'max
  356. (mapcar #'length
  357. (list org-deadline-string org-scheduled-string
  358. org-clock-string org-closed-string))))
  359. "Maximum length of the DEADLINE and SCHEDULED keywords.")
  360. (defconst org-planning-line-re
  361. (concat "^[ \t]*"
  362. (regexp-opt
  363. (list org-closed-string org-deadline-string org-scheduled-string)
  364. t))
  365. "Matches a line with planning info.
  366. Matched keyword is in group 1.")
  367. (defconst org-clock-line-re
  368. (concat "^[ \t]*" org-clock-string)
  369. "Matches a line with clock info.")
  370. (defconst org-deadline-regexp (concat "\\<" org-deadline-string)
  371. "Matches the DEADLINE keyword.")
  372. (defconst org-deadline-time-regexp
  373. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  374. "Matches the DEADLINE keyword together with a time stamp.")
  375. (defconst org-deadline-time-hour-regexp
  376. (concat "\\<" org-deadline-string
  377. " *<\\([^>]+[0-9]\\{1,2\\}:[0-9]\\{2\\}[0-9-+:hdwmy \t.]*\\)>")
  378. "Matches the DEADLINE keyword together with a time-and-hour stamp.")
  379. (defconst org-deadline-line-regexp
  380. (concat "\\<\\(" org-deadline-string "\\).*")
  381. "Matches the DEADLINE keyword and the rest of the line.")
  382. (defconst org-scheduled-regexp (concat "\\<" org-scheduled-string)
  383. "Matches the SCHEDULED keyword.")
  384. (defconst org-scheduled-time-regexp
  385. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  386. "Matches the SCHEDULED keyword together with a time stamp.")
  387. (defconst org-scheduled-time-hour-regexp
  388. (concat "\\<" org-scheduled-string
  389. " *<\\([^>]+[0-9]\\{1,2\\}:[0-9]\\{2\\}[0-9-+:hdwmy \t.]*\\)>")
  390. "Matches the SCHEDULED keyword together with a time-and-hour stamp.")
  391. (defconst org-closed-time-regexp
  392. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  393. "Matches the CLOSED keyword together with a time stamp.")
  394. (defconst org-keyword-time-regexp
  395. (concat "\\<"
  396. (regexp-opt
  397. (list org-scheduled-string org-deadline-string org-closed-string
  398. org-clock-string)
  399. t)
  400. " *[[<]\\([^]>]+\\)[]>]")
  401. "Matches any of the 4 keywords, together with the time stamp.")
  402. (defconst org-keyword-time-not-clock-regexp
  403. (concat
  404. "\\<"
  405. (regexp-opt
  406. (list org-scheduled-string org-deadline-string org-closed-string) t)
  407. " *[[<]\\([^]>]+\\)[]>]")
  408. "Matches any of the 3 keywords, together with the time stamp.")
  409. (defconst org-maybe-keyword-time-regexp
  410. (concat "\\(\\<"
  411. (regexp-opt
  412. (list org-scheduled-string org-deadline-string org-closed-string
  413. org-clock-string)
  414. t)
  415. "\\)?"
  416. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?[]>]"
  417. "\\|"
  418. "<%%([^\r\n>]*>\\)")
  419. "Matches a timestamp, possibly preceded by a keyword.")
  420. (defconst org-all-time-keywords
  421. (mapcar (lambda (w) (substring w 0 -1))
  422. (list org-scheduled-string org-deadline-string
  423. org-clock-string org-closed-string))
  424. "List of time keywords.")
  425. ;;;; Drawer
  426. (defconst org-drawer-regexp "^[ \t]*:\\(\\(?:\\w\\|[-_]\\)+\\):[ \t]*$"
  427. "Matches first or last line of a hidden block.
  428. Group 1 contains drawer's name or \"END\".")
  429. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  430. "Regular expression matching the first line of a property drawer.")
  431. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  432. "Regular expression matching the last line of a property drawer.")
  433. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  434. "Regular expression matching the first line of a clock drawer.")
  435. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  436. "Regular expression matching the last line of a clock drawer.")
  437. (defconst org-property-drawer-re
  438. (concat "^[ \t]*:PROPERTIES:[ \t]*\n"
  439. "\\(?:[ \t]*:\\S-+:\\(?: .*\\)?[ \t]*\n\\)*"
  440. "[ \t]*:END:[ \t]*$")
  441. "Matches an entire property drawer.")
  442. (defconst org-clock-drawer-re
  443. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*?\\("
  444. org-clock-drawer-end-re "\\)\n?")
  445. "Matches an entire clock drawer.")
  446. ;;;; Headline
  447. (defconst org-heading-keyword-regexp-format
  448. "^\\(\\*+\\)\\(?: +%s\\)\\(?: +\\(.*?\\)\\)?[ \t]*$"
  449. "Printf format for a regexp matching a headline with some keyword.
  450. This regexp will match the headline of any node which has the
  451. exact keyword that is put into the format. The keyword isn't in
  452. any group by default, but the stars and the body are.")
  453. (defconst org-heading-keyword-maybe-regexp-format
  454. "^\\(\\*+\\)\\(?: +%s\\)?\\(?: +\\(.*?\\)\\)?[ \t]*$"
  455. "Printf format for a regexp matching a headline, possibly with some keyword.
  456. This regexp can match any headline with the specified keyword, or
  457. without a keyword. The keyword isn't in any group by default,
  458. but the stars and the body are.")
  459. (defconst org-archive-tag "ARCHIVE"
  460. "The tag that marks a subtree as archived.
  461. An archived subtree does not open during visibility cycling, and does
  462. not contribute to the agenda listings.")
  463. (defconst org-comment-string "COMMENT"
  464. "Entries starting with this keyword will never be exported.
  465. An entry can be toggled between COMMENT and normal with
  466. \\[org-toggle-comment].")
  467. ;;;; LaTeX Environments and Fragments
  468. (defconst org-latex-regexps
  469. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  470. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  471. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  472. ("$1" "\\([^$]\\|^\\)\\(\\$[^ \r\n,;.$]\\$\\)\\(\\s.\\|\\s-\\|\\s(\\|\\s)\\|\\s\"\\|\000\\|$\\)" 2 nil)
  473. ("$" "\\([^$]\\|^\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\(\\s.\\|\\s-\\|\\s(\\|\\s)\\|\\s\"\\|\000\\|$\\)" 2 nil)
  474. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  475. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 nil)
  476. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 nil))
  477. "Regular expressions for matching embedded LaTeX.")
  478. ;;;; Node Property
  479. (defconst org-effort-property "Effort"
  480. "The property that is being used to keep track of effort estimates.
  481. Effort estimates given in this property need to have the format H:MM.")
  482. ;;;; Table
  483. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  484. "Detect an org-type or table-type table.")
  485. (defconst org-table-line-regexp "^[ \t]*|"
  486. "Detect an org-type table line.")
  487. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  488. "Detect an org-type table line.")
  489. (defconst org-table-hline-regexp "^[ \t]*|-"
  490. "Detect an org-type table hline.")
  491. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  492. "Detect a table-type table hline.")
  493. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  494. "Detect the first line outside a table when searching from within it.
  495. This works for both table types.")
  496. (defconst org-TBLFM-regexp "^[ \t]*#\\+TBLFM: "
  497. "Detect a #+TBLFM line.")
  498. ;;;; Timestamp
  499. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^\r\n>]*?\\)>"
  500. "Regular expression for fast time stamp matching.")
  501. (defconst org-ts-regexp-inactive
  502. "\\[\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^\r\n>]*?\\)\\]"
  503. "Regular expression for fast inactive time stamp matching.")
  504. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} ?[^]\r\n>]*?\\)[]>]"
  505. "Regular expression for fast time stamp matching.")
  506. (defconst org-ts-regexp0
  507. "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\)\\( +[^]+0-9>\r\n -]+\\)?\\( +\\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  508. "Regular expression matching time strings for analysis.
  509. This one does not require the space after the date, so it can be used
  510. on a string that terminates immediately after the date.")
  511. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]+0-9>\r\n -]*\\)\\( \\([0-9]\\{1,2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  512. "Regular expression matching time strings for analysis.")
  513. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  514. "Regular expression matching time stamps, with groups.")
  515. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  516. "Regular expression matching time stamps (also [..]), with groups.")
  517. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  518. "Regular expression matching a time stamp range.")
  519. (defconst org-tr-regexp-both
  520. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  521. "Regular expression matching a time stamp range.")
  522. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  523. org-ts-regexp "\\)?")
  524. "Regular expression matching a time stamp or time stamp range.")
  525. (defconst org-tsr-regexp-both
  526. (concat org-ts-regexp-both "\\(--?-?"
  527. org-ts-regexp-both "\\)?")
  528. "Regular expression matching a time stamp or time stamp range.
  529. The time stamps may be either active or inactive.")
  530. (defconst org-repeat-re
  531. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*?\\([.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)"
  532. "Regular expression for specifying repeated events.
  533. After a match, group 1 contains the repeat expression.")
  534. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  535. "Formats for `format-time-string' which are used for time stamps.")
  536. ;;; The custom variables
  537. (defgroup org nil
  538. "Outline-based notes management and organizer."
  539. :tag "Org"
  540. :group 'outlines
  541. :group 'calendar)
  542. (defcustom org-mode-hook nil
  543. "Mode hook for Org-mode, run after the mode was turned on."
  544. :group 'org
  545. :type 'hook)
  546. (defcustom org-load-hook nil
  547. "Hook that is run after org.el has been loaded."
  548. :group 'org
  549. :type 'hook)
  550. (defcustom org-log-buffer-setup-hook nil
  551. "Hook that is run after an Org log buffer is created."
  552. :group 'org
  553. :version "24.1"
  554. :type 'hook)
  555. (defvar org-modules) ; defined below
  556. (defvar org-modules-loaded nil
  557. "Have the modules been loaded already?")
  558. (defun org-load-modules-maybe (&optional force)
  559. "Load all extensions listed in `org-modules'."
  560. (when (or force (not org-modules-loaded))
  561. (mapc (lambda (ext)
  562. (condition-case nil (require ext)
  563. (error (message "Problems while trying to load feature `%s'" ext))))
  564. org-modules)
  565. (setq org-modules-loaded t)))
  566. (defun org-set-modules (var value)
  567. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  568. (set var value)
  569. (when (featurep 'org)
  570. (org-load-modules-maybe 'force)
  571. (org-element-cache-reset 'all)))
  572. (defcustom org-modules '(org-w3m org-bbdb org-bibtex org-docview org-gnus org-info org-irc org-mhe org-rmail)
  573. "Modules that should always be loaded together with org.el.
  574. If a description starts with <C>, the file is not part of Emacs
  575. and loading it will require that you have downloaded and properly
  576. installed the Org mode distribution.
  577. You can also use this system to load external packages (i.e. neither Org
  578. core modules, nor modules from the CONTRIB directory). Just add symbols
  579. to the end of the list. If the package is called org-xyz.el, then you need
  580. to add the symbol `xyz', and the package must have a call to:
  581. \(provide 'org-xyz)
  582. For export specific modules, see also `org-export-backends'."
  583. :group 'org
  584. :set 'org-set-modules
  585. :version "24.4"
  586. :package-version '(Org . "8.0")
  587. :type
  588. '(set :greedy t
  589. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  590. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  591. (const :tag " crypt: Encryption of subtrees" org-crypt)
  592. (const :tag " ctags: Access to Emacs tags with links" org-ctags)
  593. (const :tag " docview: Links to doc-view buffers" org-docview)
  594. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  595. (const :tag " habit: Track your consistency with habits" org-habit)
  596. (const :tag " id: Global IDs for identifying entries" org-id)
  597. (const :tag " info: Links to Info nodes" org-info)
  598. (const :tag " inlinetask: Tasks independent of outline hierarchy" org-inlinetask)
  599. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  600. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  601. (const :tag " mouse: Additional mouse support" org-mouse)
  602. (const :tag " protocol: Intercept calls from emacsclient" org-protocol)
  603. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  604. (const :tag " w3m: Special cut/paste from w3m to Org-mode." org-w3m)
  605. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  606. (const :tag "C bookmark: Org-mode links to bookmarks" org-bookmark)
  607. (const :tag "C bullets: Add overlays to headlines stars" org-bullets)
  608. (const :tag "C checklist: Extra functions for checklists in repeated tasks" org-checklist)
  609. (const :tag "C choose: Use TODO keywords to mark decisions states" org-choose)
  610. (const :tag "C collector: Collect properties into tables" org-collector)
  611. (const :tag "C depend: TODO dependencies for Org-mode\n\t\t\t(PARTIALLY OBSOLETE, see built-in dependency support))" org-depend)
  612. (const :tag "C drill: Flashcards and spaced repetition for Org-mode" org-drill)
  613. (const :tag "C elisp-symbol: Org-mode links to emacs-lisp symbols" org-elisp-symbol)
  614. (const :tag "C eshell Support for links to working directories in eshell" org-eshell)
  615. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  616. (const :tag "C eval: Include command output as text" org-eval)
  617. (const :tag "C eww: Store link to url of eww" org-eww)
  618. (const :tag "C expiry: Expiry mechanism for Org-mode entries" org-expiry)
  619. (const :tag "C favtable: Lookup table of favorite references and links" org-favtable)
  620. (const :tag "C git-link: Provide org links to specific file version" org-git-link)
  621. (const :tag "C interactive-query: Interactive modification of tags query\n\t\t\t(PARTIALLY OBSOLETE, see secondary filtering)" org-interactive-query)
  622. (const :tag "C invoice: Help manage client invoices in Org-mode" org-invoice)
  623. (const :tag "C learn: SuperMemo's incremental learning algorithm" org-learn)
  624. (const :tag "C mac-iCal Imports events from iCal.app to the Emacs diary" org-mac-iCal)
  625. (const :tag "C mac-link: Grab links and url from various mac Applications" org-mac-link)
  626. (const :tag "C mairix: Hook mairix search into Org-mode for different MUAs" org-mairix)
  627. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  628. (const :tag "C mew: Links to Mew folders/messages" org-mew)
  629. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  630. (const :tag "C notmuch: Provide org links to notmuch searches or messages" org-notmuch)
  631. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  632. (const :tag "C registry: A registry for Org-mode links" org-registry)
  633. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  634. (const :tag "C secretary: Team management with org-mode" org-secretary)
  635. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  636. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  637. (const :tag "C track: Keep up with Org-mode development" org-track)
  638. (const :tag "C velocity Something like Notational Velocity for Org" org-velocity)
  639. (const :tag "C vm: Links to VM folders/messages" org-vm)
  640. (const :tag "C wikinodes: CamelCase wiki-like links" org-wikinodes)
  641. (const :tag "C wl: Links to Wanderlust folders/messages" org-wl)
  642. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  643. (defvar org-export-registered-backends) ; From ox.el.
  644. (declare-function org-export-derived-backend-p "ox" (backend &rest backends))
  645. (declare-function org-export-backend-name "ox" (backend))
  646. (defcustom org-export-backends '(ascii html icalendar latex)
  647. "List of export back-ends that should be always available.
  648. If a description starts with <C>, the file is not part of Emacs
  649. and loading it will require that you have downloaded and properly
  650. installed the Org mode distribution.
  651. Unlike to `org-modules', libraries in this list will not be
  652. loaded along with Org, but only once the export framework is
  653. needed.
  654. This variable needs to be set before org.el is loaded. If you
  655. need to make a change while Emacs is running, use the customize
  656. interface or run the following code, where VAL stands for the new
  657. value of the variable, after updating it:
  658. \(progn
  659. \(setq org-export-registered-backends
  660. \(org-remove-if-not
  661. \(lambda (backend)
  662. \(let ((name (org-export-backend-name backend)))
  663. \(or (memq name val)
  664. \(catch 'parentp
  665. \(dolist (b val)
  666. \(and (org-export-derived-backend-p b name)
  667. \(throw 'parentp t)))))))
  668. org-export-registered-backends))
  669. \(let ((new-list (mapcar #'org-export-backend-name
  670. org-export-registered-backends)))
  671. \(dolist (backend val)
  672. \(cond
  673. \((not (load (format \"ox-%s\" backend) t t))
  674. \(message \"Problems while trying to load export back-end `%s'\"
  675. backend))
  676. \((not (memq backend new-list)) (push backend new-list))))
  677. \(set-default 'org-export-backends new-list)))
  678. Adding a back-end to this list will also pull the back-end it
  679. depends on, if any."
  680. :group 'org
  681. :group 'org-export
  682. :version "24.4"
  683. :package-version '(Org . "8.0")
  684. :initialize 'custom-initialize-set
  685. :set (lambda (var val)
  686. (if (not (featurep 'ox)) (set-default var val)
  687. ;; Any back-end not required anymore (not present in VAL and not
  688. ;; a parent of any back-end in the new value) is removed from the
  689. ;; list of registered back-ends.
  690. (setq org-export-registered-backends
  691. (org-remove-if-not
  692. (lambda (backend)
  693. (let ((name (org-export-backend-name backend)))
  694. (or (memq name val)
  695. (catch 'parentp
  696. (dolist (b val)
  697. (and (org-export-derived-backend-p b name)
  698. (throw 'parentp t)))))))
  699. org-export-registered-backends))
  700. ;; Now build NEW-LIST of both new back-ends and required
  701. ;; parents.
  702. (let ((new-list (mapcar #'org-export-backend-name
  703. org-export-registered-backends)))
  704. (dolist (backend val)
  705. (cond
  706. ((not (load (format "ox-%s" backend) t t))
  707. (message "Problems while trying to load export back-end `%s'"
  708. backend))
  709. ((not (memq backend new-list)) (push backend new-list))))
  710. ;; Set VAR to that list with fixed dependencies.
  711. (set-default var new-list))))
  712. :type '(set :greedy t
  713. (const :tag " ascii Export buffer to ASCII format" ascii)
  714. (const :tag " beamer Export buffer to Beamer presentation" beamer)
  715. (const :tag " html Export buffer to HTML format" html)
  716. (const :tag " icalendar Export buffer to iCalendar format" icalendar)
  717. (const :tag " latex Export buffer to LaTeX format" latex)
  718. (const :tag " man Export buffer to MAN format" man)
  719. (const :tag " md Export buffer to Markdown format" md)
  720. (const :tag " odt Export buffer to ODT format" odt)
  721. (const :tag " org Export buffer to Org format" org)
  722. (const :tag " texinfo Export buffer to Texinfo format" texinfo)
  723. (const :tag "C confluence Export buffer to Confluence Wiki format" confluence)
  724. (const :tag "C deck Export buffer to deck.js presentations" deck)
  725. (const :tag "C freemind Export buffer to Freemind mindmap format" freemind)
  726. (const :tag "C groff Export buffer to Groff format" groff)
  727. (const :tag "C koma-letter Export buffer to KOMA Scrlttrl2 format" koma-letter)
  728. (const :tag "C RSS 2.0 Export buffer to RSS 2.0 format" rss)
  729. (const :tag "C s5 Export buffer to s5 presentations" s5)
  730. (const :tag "C taskjuggler Export buffer to TaskJuggler format" taskjuggler)))
  731. (eval-after-load 'ox
  732. '(mapc
  733. (lambda (backend)
  734. (condition-case nil (require (intern (format "ox-%s" backend)))
  735. (error (message "Problems while trying to load export back-end `%s'"
  736. backend))))
  737. org-export-backends))
  738. (defcustom org-support-shift-select nil
  739. "Non-nil means make shift-cursor commands select text when possible.
  740. In Emacs 23, when `shift-select-mode' is on, shifted cursor keys
  741. start selecting a region, or enlarge regions started in this way.
  742. In Org-mode, in special contexts, these same keys are used for
  743. other purposes, important enough to compete with shift selection.
  744. Org tries to balance these needs by supporting `shift-select-mode'
  745. outside these special contexts, under control of this variable.
  746. The default of this variable is nil, to avoid confusing behavior. Shifted
  747. cursor keys will then execute Org commands in the following contexts:
  748. - on a headline, changing TODO state (left/right) and priority (up/down)
  749. - on a time stamp, changing the time
  750. - in a plain list item, changing the bullet type
  751. - in a property definition line, switching between allowed values
  752. - in the BEGIN line of a clock table (changing the time block).
  753. Outside these contexts, the commands will throw an error.
  754. When this variable is t and the cursor is not in a special
  755. context, Org-mode will support shift-selection for making and
  756. enlarging regions. To make this more effective, the bullet
  757. cycling will no longer happen anywhere in an item line, but only
  758. if the cursor is exactly on the bullet.
  759. If you set this variable to the symbol `always', then the keys
  760. will not be special in headlines, property lines, and item lines,
  761. to make shift selection work there as well. If this is what you
  762. want, you can use the following alternative commands: `C-c C-t'
  763. and `C-c ,' to change TODO state and priority, `C-u C-u C-c C-t'
  764. can be used to switch TODO sets, `C-c -' to cycle item bullet
  765. types, and properties can be edited by hand or in column view.
  766. However, when the cursor is on a timestamp, shift-cursor commands
  767. will still edit the time stamp - this is just too good to give up.
  768. XEmacs user should have this variable set to nil, because
  769. `shift-select-mode' is in Emacs 23 or later only."
  770. :group 'org
  771. :type '(choice
  772. (const :tag "Never" nil)
  773. (const :tag "When outside special context" t)
  774. (const :tag "Everywhere except timestamps" always)))
  775. (defcustom org-loop-over-headlines-in-active-region nil
  776. "Shall some commands act upon headlines in the active region?
  777. When set to t, some commands will be performed in all headlines
  778. within the active region.
  779. When set to `start-level', some commands will be performed in all
  780. headlines within the active region, provided that these headlines
  781. are of the same level than the first one.
  782. When set to a string, those commands will be performed on the
  783. matching headlines within the active region. Such string must be
  784. a tags/property/todo match as it is used in the agenda tags view.
  785. The list of commands is: `org-schedule', `org-deadline',
  786. `org-todo', `org-archive-subtree', `org-archive-set-tag' and
  787. `org-archive-to-archive-sibling'. The archiving commands skip
  788. already archived entries."
  789. :type '(choice (const :tag "Don't loop" nil)
  790. (const :tag "All headlines in active region" t)
  791. (const :tag "In active region, headlines at the same level than the first one" start-level)
  792. (string :tag "Tags/Property/Todo matcher"))
  793. :version "24.1"
  794. :group 'org-todo
  795. :group 'org-archive)
  796. (defgroup org-startup nil
  797. "Options concerning startup of Org-mode."
  798. :tag "Org Startup"
  799. :group 'org)
  800. (defcustom org-startup-folded t
  801. "Non-nil means entering Org-mode will switch to OVERVIEW.
  802. This can also be configured on a per-file basis by adding one of
  803. the following lines anywhere in the buffer:
  804. #+STARTUP: fold (or `overview', this is equivalent)
  805. #+STARTUP: nofold (or `showall', this is equivalent)
  806. #+STARTUP: content
  807. #+STARTUP: showeverything
  808. By default, this option is ignored when Org opens agenda files
  809. for the first time. If you want the agenda to honor the startup
  810. option, set `org-agenda-inhibit-startup' to nil."
  811. :group 'org-startup
  812. :type '(choice
  813. (const :tag "nofold: show all" nil)
  814. (const :tag "fold: overview" t)
  815. (const :tag "content: all headlines" content)
  816. (const :tag "show everything, even drawers" showeverything)))
  817. (defcustom org-startup-truncated t
  818. "Non-nil means entering Org-mode will set `truncate-lines'.
  819. This is useful since some lines containing links can be very long and
  820. uninteresting. Also tables look terrible when wrapped."
  821. :group 'org-startup
  822. :type 'boolean)
  823. (defcustom org-startup-indented nil
  824. "Non-nil means turn on `org-indent-mode' on startup.
  825. This can also be configured on a per-file basis by adding one of
  826. the following lines anywhere in the buffer:
  827. #+STARTUP: indent
  828. #+STARTUP: noindent"
  829. :group 'org-structure
  830. :type '(choice
  831. (const :tag "Not" nil)
  832. (const :tag "Globally (slow on startup in large files)" t)))
  833. (defcustom org-use-sub-superscripts t
  834. "Non-nil means interpret \"_\" and \"^\" for display.
  835. If you want to control how Org exports those characters, see
  836. `org-export-with-sub-superscripts'. `org-use-sub-superscripts'
  837. used to be an alias for `org-export-with-sub-superscripts' in
  838. Org <8.0, it is not anymore.
  839. When this option is turned on, you can use TeX-like syntax for
  840. sub- and superscripts within the buffer. Several characters after
  841. \"_\" or \"^\" will be considered as a single item - so grouping
  842. with {} is normally not needed. For example, the following things
  843. will be parsed as single sub- or superscripts:
  844. 10^24 or 10^tau several digits will be considered 1 item.
  845. 10^-12 or 10^-tau a leading sign with digits or a word
  846. x^2-y^3 will be read as x^2 - y^3, because items are
  847. terminated by almost any nonword/nondigit char.
  848. x_{i^2} or x^(2-i) braces or parenthesis do grouping.
  849. Still, ambiguity is possible. So when in doubt, use {} to enclose
  850. the sub/superscript. If you set this variable to the symbol `{}',
  851. the braces are *required* in order to trigger interpretations as
  852. sub/superscript. This can be helpful in documents that need \"_\"
  853. frequently in plain text."
  854. :group 'org-startup
  855. :version "24.4"
  856. :package-version '(Org . "8.0")
  857. :type '(choice
  858. (const :tag "Always interpret" t)
  859. (const :tag "Only with braces" {})
  860. (const :tag "Never interpret" nil)))
  861. (defcustom org-startup-with-beamer-mode nil
  862. "Non-nil means turn on `org-beamer-mode' on startup.
  863. This can also be configured on a per-file basis by adding one of
  864. the following lines anywhere in the buffer:
  865. #+STARTUP: beamer"
  866. :group 'org-startup
  867. :version "24.1"
  868. :type 'boolean)
  869. (defcustom org-startup-align-all-tables nil
  870. "Non-nil means align all tables when visiting a file.
  871. This is useful when the column width in tables is forced with <N> cookies
  872. in table fields. Such tables will look correct only after the first re-align.
  873. This can also be configured on a per-file basis by adding one of
  874. the following lines anywhere in the buffer:
  875. #+STARTUP: align
  876. #+STARTUP: noalign"
  877. :group 'org-startup
  878. :type 'boolean)
  879. (defcustom org-startup-with-inline-images nil
  880. "Non-nil means show inline images when loading a new Org file.
  881. This can also be configured on a per-file basis by adding one of
  882. the following lines anywhere in the buffer:
  883. #+STARTUP: inlineimages
  884. #+STARTUP: noinlineimages"
  885. :group 'org-startup
  886. :version "24.1"
  887. :type 'boolean)
  888. (defcustom org-startup-with-latex-preview nil
  889. "Non-nil means preview LaTeX fragments when loading a new Org file.
  890. This can also be configured on a per-file basis by adding one of
  891. the following lines anywhere in the buffer:
  892. #+STARTUP: latexpreview
  893. #+STARTUP: nolatexpreview"
  894. :group 'org-startup
  895. :version "24.4"
  896. :package-version '(Org . "8.0")
  897. :type 'boolean)
  898. (defcustom org-insert-mode-line-in-empty-file nil
  899. "Non-nil means insert the first line setting Org-mode in empty files.
  900. When the function `org-mode' is called interactively in an empty file, this
  901. normally means that the file name does not automatically trigger Org-mode.
  902. To ensure that the file will always be in Org-mode in the future, a
  903. line enforcing Org-mode will be inserted into the buffer, if this option
  904. has been set."
  905. :group 'org-startup
  906. :type 'boolean)
  907. (defcustom org-replace-disputed-keys nil
  908. "Non-nil means use alternative key bindings for some keys.
  909. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  910. These keys are also used by other packages like shift-selection-mode'
  911. \(built into Emacs 23), `CUA-mode' or `windmove.el'.
  912. If you want to use Org-mode together with one of these other modes,
  913. or more generally if you would like to move some Org-mode commands to
  914. other keys, set this variable and configure the keys with the variable
  915. `org-disputed-keys'.
  916. This option is only relevant at load-time of Org-mode, and must be set
  917. *before* org.el is loaded. Changing it requires a restart of Emacs to
  918. become effective."
  919. :group 'org-startup
  920. :type 'boolean)
  921. (defcustom org-use-extra-keys nil
  922. "Non-nil means use extra key sequence definitions for certain commands.
  923. This happens automatically if you run XEmacs or if `window-system'
  924. is nil. This variable lets you do the same manually. You must
  925. set it before loading org.
  926. Example: on Carbon Emacs 22 running graphically, with an external
  927. keyboard on a Powerbook, the default way of setting M-left might
  928. not work for either Alt or ESC. Setting this variable will make
  929. it work for ESC."
  930. :group 'org-startup
  931. :type 'boolean)
  932. (org-defvaralias 'org-CUA-compatible 'org-replace-disputed-keys)
  933. (defcustom org-disputed-keys
  934. '(([(shift up)] . [(meta p)])
  935. ([(shift down)] . [(meta n)])
  936. ([(shift left)] . [(meta -)])
  937. ([(shift right)] . [(meta +)])
  938. ([(control shift right)] . [(meta shift +)])
  939. ([(control shift left)] . [(meta shift -)]))
  940. "Keys for which Org-mode and other modes compete.
  941. This is an alist, cars are the default keys, second element specifies
  942. the alternative to use when `org-replace-disputed-keys' is t.
  943. Keys can be specified in any syntax supported by `define-key'.
  944. The value of this option takes effect only at Org-mode's startup,
  945. therefore you'll have to restart Emacs to apply it after changing."
  946. :group 'org-startup
  947. :type 'alist)
  948. (defun org-key (key)
  949. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  950. Or return the original if not disputed.
  951. Also apply the translations defined in `org-xemacs-key-equivalents'."
  952. (when org-replace-disputed-keys
  953. (let* ((nkey (key-description key))
  954. (x (org-find-if (lambda (x)
  955. (equal (key-description (car x)) nkey))
  956. org-disputed-keys)))
  957. (setq key (if x (cdr x) key))))
  958. (when (featurep 'xemacs)
  959. (setq key (or (cdr (assoc key org-xemacs-key-equivalents)) key)))
  960. key)
  961. (defun org-find-if (predicate seq)
  962. (catch 'exit
  963. (while seq
  964. (if (funcall predicate (car seq))
  965. (throw 'exit (car seq))
  966. (pop seq)))))
  967. (defun org-defkey (keymap key def)
  968. "Define a key, possibly translated, as returned by `org-key'."
  969. (define-key keymap (org-key key) def))
  970. (defcustom org-ellipsis nil
  971. "The ellipsis to use in the Org-mode outline.
  972. When nil, just use the standard three dots.
  973. When a string, use that string instead.
  974. When a face, use the standard 3 dots, but with the specified face.
  975. The change affects only Org-mode (which will then use its own display table).
  976. Changing this requires executing \\[org-mode] in a buffer to become
  977. effective."
  978. :group 'org-startup
  979. :type '(choice (const :tag "Default" nil)
  980. (face :tag "Face" :value org-warning)
  981. (string :tag "String" :value "...#")))
  982. (defvar org-display-table nil
  983. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  984. (defgroup org-keywords nil
  985. "Keywords in Org-mode."
  986. :tag "Org Keywords"
  987. :group 'org)
  988. (defcustom org-closed-keep-when-no-todo nil
  989. "Remove CLOSED: time-stamp when switching back to a non-todo state?"
  990. :group 'org-todo
  991. :group 'org-keywords
  992. :version "24.4"
  993. :package-version '(Org . "8.0")
  994. :type 'boolean)
  995. (defgroup org-structure nil
  996. "Options concerning the general structure of Org-mode files."
  997. :tag "Org Structure"
  998. :group 'org)
  999. (defgroup org-reveal-location nil
  1000. "Options about how to make context of a location visible."
  1001. :tag "Org Reveal Location"
  1002. :group 'org-structure)
  1003. (defcustom org-show-context-detail '((isearch . lineage)
  1004. (bookmark-jump . lineage)
  1005. (default . ancestors))
  1006. "Alist between context and visibility span when revealing a location.
  1007. \\<org-mode-map>Some actions may move point into invisible
  1008. locations. As a consequence, Org always expose a neighborhood
  1009. around point. How much is shown depends on the initial action,
  1010. or context. Valid contexts are
  1011. agenda when exposing an entry from the agenda
  1012. org-goto when using the command `org-goto' (\\[org-goto])
  1013. occur-tree when using the command `org-occur' (\\[org-sparse-tree] /)
  1014. tags-tree when constructing a sparse tree based on tags matches
  1015. link-search when exposing search matches associated with a link
  1016. mark-goto when exposing the jump goal of a mark
  1017. bookmark-jump when exposing a bookmark location
  1018. isearch when exiting from an incremental search
  1019. default default for all contexts not set explicitly
  1020. Allowed visibility spans are
  1021. minimal show current headline; if point is not on headline,
  1022. also show entry
  1023. local show current headline, entry and next headline
  1024. ancestors show current headline and its direct ancestors; if
  1025. point is not on headline, also show entry
  1026. lineage show current headline, its direct ancestors and all
  1027. their children; if point is not on headline, also show
  1028. entry and first child
  1029. tree show current headline, its direct ancestors and all
  1030. their children; if point is not on headline, also show
  1031. entry and all children
  1032. canonical show current headline, its direct ancestors along with
  1033. their entries and children; if point is not located on
  1034. the headline, also show current entry and all children
  1035. As special cases, a nil or t value means show all contexts in
  1036. `minimal' or `canonical' view, respectively.
  1037. Some views can make displayed information very compact, but also
  1038. make it harder to edit the location of the match. In such
  1039. a case, use the command `org-reveal' (\\[org-reveal]) to show
  1040. more context."
  1041. :group 'org-reveal-location
  1042. :version "25.1"
  1043. :package-version '(Org . "8.3")
  1044. :type '(choice
  1045. (const :tag "Canonical" t)
  1046. (const :tag "Minimal" nil)
  1047. (repeat :greedy t :tag "Individual contexts"
  1048. (cons
  1049. (choice :tag "Context"
  1050. (const agenda)
  1051. (const org-goto)
  1052. (const occur-tree)
  1053. (const tags-tree)
  1054. (const link-search)
  1055. (const mark-goto)
  1056. (const bookmark-jump)
  1057. (const isearch)
  1058. (const default))
  1059. (choice :tag "Detail level"
  1060. (const minimal)
  1061. (const local)
  1062. (const ancestors)
  1063. (const lineage)
  1064. (const tree)
  1065. (const canonical))))))
  1066. (defcustom org-indirect-buffer-display 'other-window
  1067. "How should indirect tree buffers be displayed?
  1068. This applies to indirect buffers created with the commands
  1069. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  1070. Valid values are:
  1071. current-window Display in the current window
  1072. other-window Just display in another window.
  1073. dedicated-frame Create one new frame, and re-use it each time.
  1074. new-frame Make a new frame each time. Note that in this case
  1075. previously-made indirect buffers are kept, and you need to
  1076. kill these buffers yourself."
  1077. :group 'org-structure
  1078. :group 'org-agenda-windows
  1079. :type '(choice
  1080. (const :tag "In current window" current-window)
  1081. (const :tag "In current frame, other window" other-window)
  1082. (const :tag "Each time a new frame" new-frame)
  1083. (const :tag "One dedicated frame" dedicated-frame)))
  1084. (defcustom org-use-speed-commands nil
  1085. "Non-nil means activate single letter commands at beginning of a headline.
  1086. This may also be a function to test for appropriate locations where speed
  1087. commands should be active.
  1088. For example, to activate speed commands when the point is on any
  1089. star at the beginning of the headline, you can do this:
  1090. (setq org-use-speed-commands
  1091. (lambda () (and (looking-at org-outline-regexp) (looking-back \"^\\**\"))))"
  1092. :group 'org-structure
  1093. :type '(choice
  1094. (const :tag "Never" nil)
  1095. (const :tag "At beginning of headline stars" t)
  1096. (function)))
  1097. (defcustom org-speed-commands-user nil
  1098. "Alist of additional speed commands.
  1099. This list will be checked before `org-speed-commands-default'
  1100. when the variable `org-use-speed-commands' is non-nil
  1101. and when the cursor is at the beginning of a headline.
  1102. The car if each entry is a string with a single letter, which must
  1103. be assigned to `self-insert-command' in the global map.
  1104. The cdr is either a command to be called interactively, a function
  1105. to be called, or a form to be evaluated.
  1106. An entry that is just a list with a single string will be interpreted
  1107. as a descriptive headline that will be added when listing the speed
  1108. commands in the Help buffer using the `?' speed command."
  1109. :group 'org-structure
  1110. :type '(repeat :value ("k" . ignore)
  1111. (choice :value ("k" . ignore)
  1112. (list :tag "Descriptive Headline" (string :tag "Headline"))
  1113. (cons :tag "Letter and Command"
  1114. (string :tag "Command letter")
  1115. (choice
  1116. (function)
  1117. (sexp))))))
  1118. (defcustom org-bookmark-names-plist
  1119. '(:last-capture "org-capture-last-stored"
  1120. :last-refile "org-refile-last-stored"
  1121. :last-capture-marker "org-capture-last-stored-marker")
  1122. "Names for bookmarks automatically set by some Org commands.
  1123. This can provide strings as names for a number of bookmarks Org sets
  1124. automatically. The following keys are currently implemented:
  1125. :last-capture
  1126. :last-capture-marker
  1127. :last-refile
  1128. When a key does not show up in the property list, the corresponding bookmark
  1129. is not set."
  1130. :group 'org-structure
  1131. :type 'plist)
  1132. (defgroup org-cycle nil
  1133. "Options concerning visibility cycling in Org-mode."
  1134. :tag "Org Cycle"
  1135. :group 'org-structure)
  1136. (defcustom org-cycle-skip-children-state-if-no-children t
  1137. "Non-nil means skip CHILDREN state in entries that don't have any."
  1138. :group 'org-cycle
  1139. :type 'boolean)
  1140. (defcustom org-cycle-max-level nil
  1141. "Maximum level which should still be subject to visibility cycling.
  1142. Levels higher than this will, for cycling, be treated as text, not a headline.
  1143. When `org-odd-levels-only' is set, a value of N in this variable actually
  1144. means 2N-1 stars as the limiting headline.
  1145. When nil, cycle all levels.
  1146. Note that the limiting level of cycling is also influenced by
  1147. `org-inlinetask-min-level'. When `org-cycle-max-level' is not set but
  1148. `org-inlinetask-min-level' is, cycling will be limited to levels one less
  1149. than its value."
  1150. :group 'org-cycle
  1151. :type '(choice
  1152. (const :tag "No limit" nil)
  1153. (integer :tag "Maximum level")))
  1154. (defcustom org-hide-block-startup nil
  1155. "Non-nil means entering Org-mode will fold all blocks.
  1156. This can also be set in on a per-file basis with
  1157. #+STARTUP: hideblocks
  1158. #+STARTUP: showblocks"
  1159. :group 'org-startup
  1160. :group 'org-cycle
  1161. :type 'boolean)
  1162. (defcustom org-cycle-global-at-bob nil
  1163. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  1164. This makes it possible to do global cycling without having to use S-TAB or
  1165. \\[universal-argument] TAB. For this special case to work, the first line
  1166. of the buffer must not be a headline -- it may be empty or some other text.
  1167. When used in this way, `org-cycle-hook' is disabled temporarily to make
  1168. sure the cursor stays at the beginning of the buffer. When this option is
  1169. nil, don't do anything special at the beginning of the buffer."
  1170. :group 'org-cycle
  1171. :type 'boolean)
  1172. (defcustom org-cycle-level-after-item/entry-creation t
  1173. "Non-nil means cycle entry level or item indentation in new empty entries.
  1174. When the cursor is at the end of an empty headline, i.e., with only stars
  1175. and maybe a TODO keyword, TAB will then switch the entry to become a child,
  1176. and then all possible ancestor states, before returning to the original state.
  1177. This makes data entry extremely fast: M-RET to create a new headline,
  1178. on TAB to make it a child, two or more tabs to make it a (grand-)uncle.
  1179. When the cursor is at the end of an empty plain list item, one TAB will
  1180. make it a subitem, two or more tabs will back up to make this an item
  1181. higher up in the item hierarchy."
  1182. :group 'org-cycle
  1183. :type 'boolean)
  1184. (defcustom org-cycle-emulate-tab t
  1185. "Where should `org-cycle' emulate TAB.
  1186. nil Never
  1187. white Only in completely white lines
  1188. whitestart Only at the beginning of lines, before the first non-white char
  1189. t Everywhere except in headlines
  1190. exc-hl-bol Everywhere except at the start of a headline
  1191. If TAB is used in a place where it does not emulate TAB, the current subtree
  1192. visibility is cycled."
  1193. :group 'org-cycle
  1194. :type '(choice (const :tag "Never" nil)
  1195. (const :tag "Only in completely white lines" white)
  1196. (const :tag "Before first char in a line" whitestart)
  1197. (const :tag "Everywhere except in headlines" t)
  1198. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)))
  1199. (defcustom org-cycle-separator-lines 2
  1200. "Number of empty lines needed to keep an empty line between collapsed trees.
  1201. If you leave an empty line between the end of a subtree and the following
  1202. headline, this empty line is hidden when the subtree is folded.
  1203. Org-mode will leave (exactly) one empty line visible if the number of
  1204. empty lines is equal or larger to the number given in this variable.
  1205. So the default 2 means at least 2 empty lines after the end of a subtree
  1206. are needed to produce free space between a collapsed subtree and the
  1207. following headline.
  1208. If the number is negative, and the number of empty lines is at least -N,
  1209. all empty lines are shown.
  1210. Special case: when 0, never leave empty lines in collapsed view."
  1211. :group 'org-cycle
  1212. :type 'integer)
  1213. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  1214. (defcustom org-pre-cycle-hook nil
  1215. "Hook that is run before visibility cycling is happening.
  1216. The function(s) in this hook must accept a single argument which indicates
  1217. the new state that will be set right after running this hook. The
  1218. argument is a symbol. Before a global state change, it can have the values
  1219. `overview', `content', or `all'. Before a local state change, it can have
  1220. the values `folded', `children', or `subtree'."
  1221. :group 'org-cycle
  1222. :type 'hook)
  1223. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  1224. org-cycle-hide-drawers
  1225. org-cycle-show-empty-lines
  1226. org-optimize-window-after-visibility-change)
  1227. "Hook that is run after `org-cycle' has changed the buffer visibility.
  1228. The function(s) in this hook must accept a single argument which indicates
  1229. the new state that was set by the most recent `org-cycle' command. The
  1230. argument is a symbol. After a global state change, it can have the values
  1231. `overview', `contents', or `all'. After a local state change, it can have
  1232. the values `folded', `children', or `subtree'."
  1233. :group 'org-cycle
  1234. :type 'hook
  1235. :version "25.1"
  1236. :package-version '(Org . "8.3"))
  1237. (defgroup org-edit-structure nil
  1238. "Options concerning structure editing in Org-mode."
  1239. :tag "Org Edit Structure"
  1240. :group 'org-structure)
  1241. (defcustom org-odd-levels-only nil
  1242. "Non-nil means skip even levels and only use odd levels for the outline.
  1243. This has the effect that two stars are being added/taken away in
  1244. promotion/demotion commands. It also influences how levels are
  1245. handled by the exporters.
  1246. Changing it requires restart of `font-lock-mode' to become effective
  1247. for fontification also in regions already fontified.
  1248. You may also set this on a per-file basis by adding one of the following
  1249. lines to the buffer:
  1250. #+STARTUP: odd
  1251. #+STARTUP: oddeven"
  1252. :group 'org-edit-structure
  1253. :group 'org-appearance
  1254. :type 'boolean)
  1255. (defcustom org-adapt-indentation t
  1256. "Non-nil means adapt indentation to outline node level.
  1257. When this variable is set, Org assumes that you write outlines by
  1258. indenting text in each node to align with the headline (after the
  1259. stars). The following issues are influenced by this variable:
  1260. - The indentation is increased by one space in a demotion
  1261. command, and decreased by one in a promotion command. However,
  1262. in the latter case, if shifting some line in the entry body
  1263. would alter document structure (e.g., insert a new headline),
  1264. indentation is not changed at all.
  1265. - Property drawers and planning information is inserted indented
  1266. when this variable is set. When nil, they will not be indented.
  1267. - TAB indents a line relative to current level. The lines below
  1268. a headline will be indented when this variable is set.
  1269. Note that this is all about true indentation, by adding and
  1270. removing space characters. See also `org-indent.el' which does
  1271. level-dependent indentation in a virtual way, i.e. at display
  1272. time in Emacs."
  1273. :group 'org-edit-structure
  1274. :type 'boolean)
  1275. (defcustom org-special-ctrl-a/e nil
  1276. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  1277. When t, `C-a' will bring back the cursor to the beginning of the
  1278. headline text, i.e. after the stars and after a possible TODO
  1279. keyword. In an item, this will be the position after bullet and
  1280. check-box, if any. When the cursor is already at that position,
  1281. another `C-a' will bring it to the beginning of the line.
  1282. `C-e' will jump to the end of the headline, ignoring the presence
  1283. of tags in the headline. A second `C-e' will then jump to the
  1284. true end of the line, after any tags. This also means that, when
  1285. this variable is non-nil, `C-e' also will never jump beyond the
  1286. end of the heading of a folded section, i.e. not after the
  1287. ellipses.
  1288. When set to the symbol `reversed', the first `C-a' or `C-e' works
  1289. normally, going to the true line boundary first. Only a directly
  1290. following, identical keypress will bring the cursor to the
  1291. special positions.
  1292. This may also be a cons cell where the behavior for `C-a' and
  1293. `C-e' is set separately."
  1294. :group 'org-edit-structure
  1295. :type '(choice
  1296. (const :tag "off" nil)
  1297. (const :tag "on: after stars/bullet and before tags first" t)
  1298. (const :tag "reversed: true line boundary first" reversed)
  1299. (cons :tag "Set C-a and C-e separately"
  1300. (choice :tag "Special C-a"
  1301. (const :tag "off" nil)
  1302. (const :tag "on: after stars/bullet first" t)
  1303. (const :tag "reversed: before stars/bullet first" reversed))
  1304. (choice :tag "Special C-e"
  1305. (const :tag "off" nil)
  1306. (const :tag "on: before tags first" t)
  1307. (const :tag "reversed: after tags first" reversed)))))
  1308. (org-defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e)
  1309. (defcustom org-special-ctrl-k nil
  1310. "Non-nil means `C-k' will behave specially in headlines.
  1311. When nil, `C-k' will call the default `kill-line' command.
  1312. When t, the following will happen while the cursor is in the headline:
  1313. - When the cursor is at the beginning of a headline, kill the entire
  1314. line and possible the folded subtree below the line.
  1315. - When in the middle of the headline text, kill the headline up to the tags.
  1316. - When after the headline text, kill the tags."
  1317. :group 'org-edit-structure
  1318. :type 'boolean)
  1319. (defcustom org-ctrl-k-protect-subtree nil
  1320. "Non-nil means, do not delete a hidden subtree with C-k.
  1321. When set to the symbol `error', simply throw an error when C-k is
  1322. used to kill (part-of) a headline that has hidden text behind it.
  1323. Any other non-nil value will result in a query to the user, if it is
  1324. OK to kill that hidden subtree. When nil, kill without remorse."
  1325. :group 'org-edit-structure
  1326. :version "24.1"
  1327. :type '(choice
  1328. (const :tag "Do not protect hidden subtrees" nil)
  1329. (const :tag "Protect hidden subtrees with a security query" t)
  1330. (const :tag "Never kill a hidden subtree with C-k" error)))
  1331. (defcustom org-special-ctrl-o t
  1332. "Non-nil means, make `C-o' insert a row in tables."
  1333. :group 'org-edit-structure
  1334. :type 'boolean)
  1335. (defcustom org-catch-invisible-edits nil
  1336. "Check if in invisible region before inserting or deleting a character.
  1337. Valid values are:
  1338. nil Do not check, so just do invisible edits.
  1339. error Throw an error and do nothing.
  1340. show Make point visible, and do the requested edit.
  1341. show-and-error Make point visible, then throw an error and abort the edit.
  1342. smart Make point visible, and do insertion/deletion if it is
  1343. adjacent to visible text and the change feels predictable.
  1344. Never delete a previously invisible character or add in the
  1345. middle or right after an invisible region. Basically, this
  1346. allows insertion and backward-delete right before ellipses.
  1347. FIXME: maybe in this case we should not even show?"
  1348. :group 'org-edit-structure
  1349. :version "24.1"
  1350. :type '(choice
  1351. (const :tag "Do not check" nil)
  1352. (const :tag "Throw error when trying to edit" error)
  1353. (const :tag "Unhide, but do not do the edit" show-and-error)
  1354. (const :tag "Show invisible part and do the edit" show)
  1355. (const :tag "Be smart and do the right thing" smart)))
  1356. (defcustom org-yank-folded-subtrees t
  1357. "Non-nil means when yanking subtrees, fold them.
  1358. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  1359. it starts with a heading and all other headings in it are either children
  1360. or siblings, then fold all the subtrees. However, do this only if no
  1361. text after the yank would be swallowed into a folded tree by this action."
  1362. :group 'org-edit-structure
  1363. :type 'boolean)
  1364. (defcustom org-yank-adjusted-subtrees nil
  1365. "Non-nil means when yanking subtrees, adjust the level.
  1366. With this setting, `org-paste-subtree' is used to insert the subtree, see
  1367. this function for details."
  1368. :group 'org-edit-structure
  1369. :type 'boolean)
  1370. (defcustom org-M-RET-may-split-line '((default . t))
  1371. "Non-nil means M-RET will split the line at the cursor position.
  1372. When nil, it will go to the end of the line before making a
  1373. new line.
  1374. You may also set this option in a different way for different
  1375. contexts. Valid contexts are:
  1376. headline when creating a new headline
  1377. item when creating a new item
  1378. table in a table field
  1379. default the value to be used for all contexts not explicitly
  1380. customized"
  1381. :group 'org-structure
  1382. :group 'org-table
  1383. :type '(choice
  1384. (const :tag "Always" t)
  1385. (const :tag "Never" nil)
  1386. (repeat :greedy t :tag "Individual contexts"
  1387. (cons
  1388. (choice :tag "Context"
  1389. (const headline)
  1390. (const item)
  1391. (const table)
  1392. (const default))
  1393. (boolean)))))
  1394. (defcustom org-insert-heading-respect-content nil
  1395. "Non-nil means insert new headings after the current subtree.
  1396. When nil, the new heading is created directly after the current line.
  1397. The commands \\[org-insert-heading-respect-content] and \\[org-insert-todo-heading-respect-content] turn
  1398. this variable on for the duration of the command."
  1399. :group 'org-structure
  1400. :type 'boolean)
  1401. (defcustom org-blank-before-new-entry '((heading . auto)
  1402. (plain-list-item . auto))
  1403. "Should `org-insert-heading' leave a blank line before new heading/item?
  1404. The value is an alist, with `heading' and `plain-list-item' as CAR,
  1405. and a boolean flag as CDR. The cdr may also be the symbol `auto', in
  1406. which case Org will look at the surrounding headings/items and try to
  1407. make an intelligent decision whether to insert a blank line or not."
  1408. :group 'org-edit-structure
  1409. :type '(list
  1410. (cons (const heading)
  1411. (choice (const :tag "Never" nil)
  1412. (const :tag "Always" t)
  1413. (const :tag "Auto" auto)))
  1414. (cons (const plain-list-item)
  1415. (choice (const :tag "Never" nil)
  1416. (const :tag "Always" t)
  1417. (const :tag "Auto" auto)))))
  1418. (defcustom org-insert-heading-hook nil
  1419. "Hook being run after inserting a new heading."
  1420. :group 'org-edit-structure
  1421. :type 'hook)
  1422. (defcustom org-enable-fixed-width-editor t
  1423. "Non-nil means lines starting with \":\" are treated as fixed-width.
  1424. This currently only means they are never auto-wrapped.
  1425. When nil, such lines will be treated like ordinary lines."
  1426. :group 'org-edit-structure
  1427. :type 'boolean)
  1428. (defcustom org-goto-auto-isearch t
  1429. "Non-nil means typing characters in `org-goto' starts incremental search.
  1430. When nil, you can use these keybindings to navigate the buffer:
  1431. q Quit the org-goto interface
  1432. n Go to the next visible heading
  1433. p Go to the previous visible heading
  1434. f Go one heading forward on same level
  1435. b Go one heading backward on same level
  1436. u Go one heading up"
  1437. :group 'org-edit-structure
  1438. :type 'boolean)
  1439. (defgroup org-sparse-trees nil
  1440. "Options concerning sparse trees in Org-mode."
  1441. :tag "Org Sparse Trees"
  1442. :group 'org-structure)
  1443. (defcustom org-highlight-sparse-tree-matches t
  1444. "Non-nil means highlight all matches that define a sparse tree.
  1445. The highlights will automatically disappear the next time the buffer is
  1446. changed by an edit command."
  1447. :group 'org-sparse-trees
  1448. :type 'boolean)
  1449. (defcustom org-remove-highlights-with-change t
  1450. "Non-nil means any change to the buffer will remove temporary highlights.
  1451. Such highlights are created by `org-occur' and `org-clock-display'.
  1452. When nil, `C-c C-c' needs to be used to get rid of the highlights.
  1453. The highlights created by `org-toggle-latex-fragment' always need
  1454. `C-c C-x C-l' to be removed."
  1455. :group 'org-sparse-trees
  1456. :group 'org-time
  1457. :type 'boolean)
  1458. (defcustom org-occur-hook '(org-first-headline-recenter)
  1459. "Hook that is run after `org-occur' has constructed a sparse tree.
  1460. This can be used to recenter the window to show as much of the structure
  1461. as possible."
  1462. :group 'org-sparse-trees
  1463. :type 'hook)
  1464. (defgroup org-imenu-and-speedbar nil
  1465. "Options concerning imenu and speedbar in Org-mode."
  1466. :tag "Org Imenu and Speedbar"
  1467. :group 'org-structure)
  1468. (defcustom org-imenu-depth 2
  1469. "The maximum level for Imenu access to Org-mode headlines.
  1470. This also applied for speedbar access."
  1471. :group 'org-imenu-and-speedbar
  1472. :type 'integer)
  1473. (defgroup org-table nil
  1474. "Options concerning tables in Org-mode."
  1475. :tag "Org Table"
  1476. :group 'org)
  1477. (defcustom org-enable-table-editor 'optimized
  1478. "Non-nil means lines starting with \"|\" are handled by the table editor.
  1479. When nil, such lines will be treated like ordinary lines.
  1480. When equal to the symbol `optimized', the table editor will be optimized to
  1481. do the following:
  1482. - Automatic overwrite mode in front of whitespace in table fields.
  1483. This makes the structure of the table stay in tact as long as the edited
  1484. field does not exceed the column width.
  1485. - Minimize the number of realigns. Normally, the table is aligned each time
  1486. TAB or RET are pressed to move to another field. With optimization this
  1487. happens only if changes to a field might have changed the column width.
  1488. Optimization requires replacing the functions `self-insert-command',
  1489. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  1490. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  1491. very good at guessing when a re-align will be necessary, but you can always
  1492. force one with \\[org-ctrl-c-ctrl-c].
  1493. If you would like to use the optimized version in Org-mode, but the
  1494. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  1495. This variable can be used to turn on and off the table editor during a session,
  1496. but in order to toggle optimization, a restart is required.
  1497. See also the variable `org-table-auto-blank-field'."
  1498. :group 'org-table
  1499. :type '(choice
  1500. (const :tag "off" nil)
  1501. (const :tag "on" t)
  1502. (const :tag "on, optimized" optimized)))
  1503. (defcustom org-self-insert-cluster-for-undo (or (featurep 'xemacs)
  1504. (version<= emacs-version "24.1"))
  1505. "Non-nil means cluster self-insert commands for undo when possible.
  1506. If this is set, then, like in the Emacs command loop, 20 consecutive
  1507. characters will be undone together.
  1508. This is configurable, because there is some impact on typing performance."
  1509. :group 'org-table
  1510. :type 'boolean)
  1511. (defcustom org-table-tab-recognizes-table.el t
  1512. "Non-nil means TAB will automatically notice a table.el table.
  1513. When it sees such a table, it moves point into it and - if necessary -
  1514. calls `table-recognize-table'."
  1515. :group 'org-table-editing
  1516. :type 'boolean)
  1517. (defgroup org-link nil
  1518. "Options concerning links in Org-mode."
  1519. :tag "Org Link"
  1520. :group 'org)
  1521. (defvar org-link-abbrev-alist-local nil
  1522. "Buffer-local version of `org-link-abbrev-alist', which see.
  1523. The value of this is taken from the #+LINK lines.")
  1524. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  1525. (defcustom org-link-abbrev-alist nil
  1526. "Alist of link abbreviations.
  1527. The car of each element is a string, to be replaced at the start of a link.
  1528. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  1529. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  1530. [[linkkey:tag][description]]
  1531. The 'linkkey' must be a word word, starting with a letter, followed
  1532. by letters, numbers, '-' or '_'.
  1533. If REPLACE is a string, the tag will simply be appended to create the link.
  1534. If the string contains \"%s\", the tag will be inserted there. If the string
  1535. contains \"%h\", it will cause a url-encoded version of the tag to be inserted
  1536. at that point (see the function `url-hexify-string'). If the string contains
  1537. the specifier \"%(my-function)\", then the custom function `my-function' will
  1538. be invoked: this function takes the tag as its only argument and must return
  1539. a string.
  1540. REPLACE may also be a function that will be called with the tag as the
  1541. only argument to create the link, which should be returned as a string.
  1542. See the manual for examples."
  1543. :group 'org-link
  1544. :type '(repeat
  1545. (cons
  1546. (string :tag "Protocol")
  1547. (choice
  1548. (string :tag "Format")
  1549. (function)))))
  1550. (defcustom org-descriptive-links t
  1551. "Non-nil means Org will display descriptive links.
  1552. E.g. [[http://orgmode.org][Org website]] will be displayed as
  1553. \"Org Website\", hiding the link itself and just displaying its
  1554. description. When set to nil, Org will display the full links
  1555. literally.
  1556. You can interactively set the value of this variable by calling
  1557. `org-toggle-link-display' or from the menu Org>Hyperlinks menu."
  1558. :group 'org-link
  1559. :type 'boolean)
  1560. (defcustom org-link-file-path-type 'adaptive
  1561. "How the path name in file links should be stored.
  1562. Valid values are:
  1563. relative Relative to the current directory, i.e. the directory of the file
  1564. into which the link is being inserted.
  1565. absolute Absolute path, if possible with ~ for home directory.
  1566. noabbrev Absolute path, no abbreviation of home directory.
  1567. adaptive Use relative path for files in the current directory and sub-
  1568. directories of it. For other files, use an absolute path."
  1569. :group 'org-link
  1570. :type '(choice
  1571. (const relative)
  1572. (const absolute)
  1573. (const noabbrev)
  1574. (const adaptive)))
  1575. (defvaralias 'org-activate-links 'org-highlight-links)
  1576. (defcustom org-highlight-links '(bracket angle plain radio tag date footnote)
  1577. "Types of links that should be highlighted in Org-mode files.
  1578. This is a list of symbols, each one of them leading to the
  1579. highlighting of a certain link type.
  1580. You can still open links that are not highlighted.
  1581. In principle, it does not hurt to turn on highlighting for all
  1582. link types. There may be a small gain when turning off unused
  1583. link types. The types are:
  1584. bracket The recommended [[link][description]] or [[link]] links with hiding.
  1585. angle Links in angular brackets that may contain whitespace like
  1586. <bbdb:Carsten Dominik>.
  1587. plain Plain links in normal text, no whitespace, like http://google.com.
  1588. radio Text that is matched by a radio target, see manual for details.
  1589. tag Tag settings in a headline (link to tag search).
  1590. date Time stamps (link to calendar).
  1591. footnote Footnote labels.
  1592. If you set this variable during an Emacs session, use `org-mode-restart'
  1593. in the Org buffer so that the change takes effect."
  1594. :group 'org-link
  1595. :group 'org-appearance
  1596. :type '(set :greedy t
  1597. (const :tag "Double bracket links" bracket)
  1598. (const :tag "Angular bracket links" angle)
  1599. (const :tag "Plain text links" plain)
  1600. (const :tag "Radio target matches" radio)
  1601. (const :tag "Tags" tag)
  1602. (const :tag "Timestamps" date)
  1603. (const :tag "Footnotes" footnote)))
  1604. (defcustom org-make-link-description-function nil
  1605. "Function to use for generating link descriptions from links.
  1606. When nil, the link location will be used. This function must take
  1607. two parameters: the first one is the link, the second one is the
  1608. description generated by `org-insert-link'. The function should
  1609. return the description to use."
  1610. :group 'org-link
  1611. :type '(choice (const nil) (function)))
  1612. (defgroup org-link-store nil
  1613. "Options concerning storing links in Org-mode."
  1614. :tag "Org Store Link"
  1615. :group 'org-link)
  1616. (defcustom org-url-hexify-p t
  1617. "When non-nil, hexify URL when creating a link."
  1618. :type 'boolean
  1619. :version "24.3"
  1620. :group 'org-link-store)
  1621. (defcustom org-email-link-description-format "Email %c: %.30s"
  1622. "Format of the description part of a link to an email or usenet message.
  1623. The following %-escapes will be replaced by corresponding information:
  1624. %F full \"From\" field
  1625. %f name, taken from \"From\" field, address if no name
  1626. %T full \"To\" field
  1627. %t first name in \"To\" field, address if no name
  1628. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  1629. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  1630. %s subject
  1631. %d date
  1632. %m message-id.
  1633. You may use normal field width specification between the % and the letter.
  1634. This is for example useful to limit the length of the subject.
  1635. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  1636. :group 'org-link-store
  1637. :type 'string)
  1638. (defcustom org-from-is-user-regexp
  1639. (let (r1 r2)
  1640. (when (and user-mail-address (not (string= user-mail-address "")))
  1641. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  1642. (when (and user-full-name (not (string= user-full-name "")))
  1643. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  1644. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  1645. "Regexp matched against the \"From:\" header of an email or usenet message.
  1646. It should match if the message is from the user him/herself."
  1647. :group 'org-link-store
  1648. :type 'regexp)
  1649. (defcustom org-context-in-file-links t
  1650. "Non-nil means file links from `org-store-link' contain context.
  1651. A search string will be added to the file name with :: as separator and
  1652. used to find the context when the link is activated by the command
  1653. `org-open-at-point'. When this option is t, the entire active region
  1654. will be placed in the search string of the file link. If set to a
  1655. positive integer, only the first n lines of context will be stored.
  1656. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  1657. negates this setting for the duration of the command."
  1658. :group 'org-link-store
  1659. :type '(choice boolean integer))
  1660. (defcustom org-keep-stored-link-after-insertion nil
  1661. "Non-nil means keep link in list for entire session.
  1662. The command `org-store-link' adds a link pointing to the current
  1663. location to an internal list. These links accumulate during a session.
  1664. The command `org-insert-link' can be used to insert links into any
  1665. Org-mode file (offering completion for all stored links). When this
  1666. option is nil, every link which has been inserted once using \\[org-insert-link]
  1667. will be removed from the list, to make completing the unused links
  1668. more efficient."
  1669. :group 'org-link-store
  1670. :type 'boolean)
  1671. (defgroup org-link-follow nil
  1672. "Options concerning following links in Org-mode."
  1673. :tag "Org Follow Link"
  1674. :group 'org-link)
  1675. (defcustom org-link-translation-function nil
  1676. "Function to translate links with different syntax to Org syntax.
  1677. This can be used to translate links created for example by the Planner
  1678. or emacs-wiki packages to Org syntax.
  1679. The function must accept two parameters, a TYPE containing the link
  1680. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  1681. which is everything after the link protocol. It should return a cons
  1682. with possibly modified values of type and path.
  1683. Org contains a function for this, so if you set this variable to
  1684. `org-translate-link-from-planner', you should be able follow many
  1685. links created by planner."
  1686. :group 'org-link-follow
  1687. :type '(choice (const nil) (function)))
  1688. (defcustom org-follow-link-hook nil
  1689. "Hook that is run after a link has been followed."
  1690. :group 'org-link-follow
  1691. :type 'hook)
  1692. (defcustom org-tab-follows-link nil
  1693. "Non-nil means on links TAB will follow the link.
  1694. Needs to be set before org.el is loaded.
  1695. This really should not be used, it does not make sense, and the
  1696. implementation is bad."
  1697. :group 'org-link-follow
  1698. :type 'boolean)
  1699. (defcustom org-return-follows-link nil
  1700. "Non-nil means on links RET will follow the link.
  1701. In tables, the special behavior of RET has precedence."
  1702. :group 'org-link-follow
  1703. :type 'boolean)
  1704. (defcustom org-mouse-1-follows-link
  1705. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  1706. "Non-nil means mouse-1 on a link will follow the link.
  1707. A longer mouse click will still set point. Does not work on XEmacs.
  1708. Needs to be set before org.el is loaded."
  1709. :group 'org-link-follow
  1710. :version "24.4"
  1711. :package-version '(Org . "8.3")
  1712. :type '(choice
  1713. (const :tag "A double click follows the link" double)
  1714. (const :tag "Unconditionally follow the link with mouse-1" t)
  1715. (integer :tag "mouse-1 click does not follow the link if longer than N ms" 450)))
  1716. (defcustom org-mark-ring-length 4
  1717. "Number of different positions to be recorded in the ring.
  1718. Changing this requires a restart of Emacs to work correctly."
  1719. :group 'org-link-follow
  1720. :type 'integer)
  1721. (defcustom org-link-search-must-match-exact-headline 'query-to-create
  1722. "Non-nil means internal links in Org files must exactly match a headline.
  1723. When nil, the link search tries to match a phrase with all words
  1724. in the search text."
  1725. :group 'org-link-follow
  1726. :version "24.1"
  1727. :type '(choice
  1728. (const :tag "Use fuzzy text search" nil)
  1729. (const :tag "Match only exact headline" t)
  1730. (const :tag "Match exact headline or query to create it"
  1731. query-to-create)))
  1732. (defcustom org-link-frame-setup
  1733. '((vm . vm-visit-folder-other-frame)
  1734. (vm-imap . vm-visit-imap-folder-other-frame)
  1735. (gnus . org-gnus-no-new-news)
  1736. (file . find-file-other-window)
  1737. (wl . wl-other-frame))
  1738. "Setup the frame configuration for following links.
  1739. When following a link with Emacs, it may often be useful to display
  1740. this link in another window or frame. This variable can be used to
  1741. set this up for the different types of links.
  1742. For VM, use any of
  1743. `vm-visit-folder'
  1744. `vm-visit-folder-other-window'
  1745. `vm-visit-folder-other-frame'
  1746. For Gnus, use any of
  1747. `gnus'
  1748. `gnus-other-frame'
  1749. `org-gnus-no-new-news'
  1750. For FILE, use any of
  1751. `find-file'
  1752. `find-file-other-window'
  1753. `find-file-other-frame'
  1754. For Wanderlust use any of
  1755. `wl'
  1756. `wl-other-frame'
  1757. For the calendar, use the variable `calendar-setup'.
  1758. For BBDB, it is currently only possible to display the matches in
  1759. another window."
  1760. :group 'org-link-follow
  1761. :type '(list
  1762. (cons (const vm)
  1763. (choice
  1764. (const vm-visit-folder)
  1765. (const vm-visit-folder-other-window)
  1766. (const vm-visit-folder-other-frame)))
  1767. (cons (const vm-imap)
  1768. (choice
  1769. (const vm-visit-imap-folder)
  1770. (const vm-visit-imap-folder-other-window)
  1771. (const vm-visit-imap-folder-other-frame)))
  1772. (cons (const gnus)
  1773. (choice
  1774. (const gnus)
  1775. (const gnus-other-frame)
  1776. (const org-gnus-no-new-news)))
  1777. (cons (const file)
  1778. (choice
  1779. (const find-file)
  1780. (const find-file-other-window)
  1781. (const find-file-other-frame)))
  1782. (cons (const wl)
  1783. (choice
  1784. (const wl)
  1785. (const wl-other-frame)))))
  1786. (defcustom org-display-internal-link-with-indirect-buffer nil
  1787. "Non-nil means use indirect buffer to display infile links.
  1788. Activating internal links (from one location in a file to another location
  1789. in the same file) normally just jumps to the location. When the link is
  1790. activated with a \\[universal-argument] prefix (or with mouse-3), the link \
  1791. is displayed in
  1792. another window. When this option is set, the other window actually displays
  1793. an indirect buffer clone of the current buffer, to avoid any visibility
  1794. changes to the current buffer."
  1795. :group 'org-link-follow
  1796. :type 'boolean)
  1797. (defcustom org-open-non-existing-files nil
  1798. "Non-nil means `org-open-file' will open non-existing files.
  1799. When nil, an error will be generated.
  1800. This variable applies only to external applications because they
  1801. might choke on non-existing files. If the link is to a file that
  1802. will be opened in Emacs, the variable is ignored."
  1803. :group 'org-link-follow
  1804. :type 'boolean)
  1805. (defcustom org-open-directory-means-index-dot-org nil
  1806. "Non-nil means a link to a directory really means to index.org.
  1807. When nil, following a directory link will run dired or open a finder/explorer
  1808. window on that directory."
  1809. :group 'org-link-follow
  1810. :type 'boolean)
  1811. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1812. "Non-nil means ask for confirmation before executing shell links.
  1813. Shell links can be dangerous: just think about a link
  1814. [[shell:rm -rf ~/*][Google Search]]
  1815. This link would show up in your Org-mode document as \"Google Search\",
  1816. but really it would remove your entire home directory.
  1817. Therefore we advise against setting this variable to nil.
  1818. Just change it to `y-or-n-p' if you want to confirm with a
  1819. single keystroke rather than having to type \"yes\"."
  1820. :group 'org-link-follow
  1821. :type '(choice
  1822. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1823. (const :tag "with y-or-n (faster)" y-or-n-p)
  1824. (const :tag "no confirmation (dangerous)" nil)))
  1825. (put 'org-confirm-shell-link-function
  1826. 'safe-local-variable
  1827. (lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1828. (defcustom org-confirm-shell-link-not-regexp ""
  1829. "A regexp to skip confirmation for shell links."
  1830. :group 'org-link-follow
  1831. :version "24.1"
  1832. :type 'regexp)
  1833. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1834. "Non-nil means ask for confirmation before executing Emacs Lisp links.
  1835. Elisp links can be dangerous: just think about a link
  1836. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1837. This link would show up in your Org-mode document as \"Google Search\",
  1838. but really it would remove your entire home directory.
  1839. Therefore we advise against setting this variable to nil.
  1840. Just change it to `y-or-n-p' if you want to confirm with a
  1841. single keystroke rather than having to type \"yes\"."
  1842. :group 'org-link-follow
  1843. :type '(choice
  1844. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1845. (const :tag "with y-or-n (faster)" y-or-n-p)
  1846. (const :tag "no confirmation (dangerous)" nil)))
  1847. (put 'org-confirm-shell-link-function
  1848. 'safe-local-variable
  1849. (lambda (x) (member x '(yes-or-no-p y-or-n-p))))
  1850. (defcustom org-confirm-elisp-link-not-regexp ""
  1851. "A regexp to skip confirmation for Elisp links."
  1852. :group 'org-link-follow
  1853. :version "24.1"
  1854. :type 'regexp)
  1855. (defconst org-file-apps-defaults-gnu
  1856. '((remote . emacs)
  1857. (system . mailcap)
  1858. (t . mailcap))
  1859. "Default file applications on a UNIX or GNU/Linux system.
  1860. See `org-file-apps'.")
  1861. (defconst org-file-apps-defaults-macosx
  1862. '((remote . emacs)
  1863. (t . "open %s")
  1864. (system . "open %s")
  1865. ("ps.gz" . "gv %s")
  1866. ("eps.gz" . "gv %s")
  1867. ("dvi" . "xdvi %s")
  1868. ("fig" . "xfig %s"))
  1869. "Default file applications on a MacOS X system.
  1870. The system \"open\" is known as a default, but we use X11 applications
  1871. for some files for which the OS does not have a good default.
  1872. See `org-file-apps'.")
  1873. (defconst org-file-apps-defaults-windowsnt
  1874. (list
  1875. '(remote . emacs)
  1876. (cons t
  1877. (list (if (featurep 'xemacs)
  1878. 'mswindows-shell-execute
  1879. 'w32-shell-execute)
  1880. "open" 'file))
  1881. (cons 'system
  1882. (list (if (featurep 'xemacs)
  1883. 'mswindows-shell-execute
  1884. 'w32-shell-execute)
  1885. "open" 'file)))
  1886. "Default file applications on a Windows NT system.
  1887. The system \"open\" is used for most files.
  1888. See `org-file-apps'.")
  1889. (defcustom org-file-apps
  1890. '((auto-mode . emacs)
  1891. ("\\.mm\\'" . default)
  1892. ("\\.x?html?\\'" . default)
  1893. ("\\.pdf\\'" . default))
  1894. "External applications for opening `file:path' items in a document.
  1895. Org-mode uses system defaults for different file types, but
  1896. you can use this variable to set the application for a given file
  1897. extension. The entries in this list are cons cells where the car identifies
  1898. files and the cdr the corresponding command. Possible values for the
  1899. file identifier are
  1900. \"string\" A string as a file identifier can be interpreted in different
  1901. ways, depending on its contents:
  1902. - Alphanumeric characters only:
  1903. Match links with this file extension.
  1904. Example: (\"pdf\" . \"evince %s\")
  1905. to open PDFs with evince.
  1906. - Regular expression: Match links where the
  1907. filename matches the regexp. If you want to
  1908. use groups here, use shy groups.
  1909. Example: (\"\\.x?html\\'\" . \"firefox %s\")
  1910. (\"\\(?:xhtml\\|html\\)\" . \"firefox %s\")
  1911. to open *.html and *.xhtml with firefox.
  1912. - Regular expression which contains (non-shy) groups:
  1913. Match links where the whole link, including \"::\", and
  1914. anything after that, matches the regexp.
  1915. In a custom command string, %1, %2, etc. are replaced with
  1916. the parts of the link that were matched by the groups.
  1917. For backwards compatibility, if a command string is given
  1918. that does not use any of the group matches, this case is
  1919. handled identically to the second one (i.e. match against
  1920. file name only).
  1921. In a custom lisp form, you can access the group matches with
  1922. (match-string n link).
  1923. Example: (\"\\.pdf::\\(\\d+\\)\\'\" . \"evince -p %1 %s\")
  1924. to open [[file:document.pdf::5]] with evince at page 5.
  1925. `directory' Matches a directory
  1926. `remote' Matches a remote file, accessible through tramp or efs.
  1927. Remote files most likely should be visited through Emacs
  1928. because external applications cannot handle such paths.
  1929. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1930. so all files Emacs knows how to handle. Using this with
  1931. command `emacs' will open most files in Emacs. Beware that this
  1932. will also open html files inside Emacs, unless you add
  1933. (\"html\" . default) to the list as well.
  1934. t Default for files not matched by any of the other options.
  1935. `system' The system command to open files, like `open' on Windows
  1936. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1937. that will be selected if you call `C-c C-o' with a double
  1938. \\[universal-argument] \\[universal-argument] prefix.
  1939. Possible values for the command are:
  1940. `emacs' The file will be visited by the current Emacs process.
  1941. `default' Use the default application for this file type, which is the
  1942. association for t in the list, most likely in the system-specific
  1943. part.
  1944. This can be used to overrule an unwanted setting in the
  1945. system-specific variable.
  1946. `system' Use the system command for opening files, like \"open\".
  1947. This command is specified by the entry whose car is `system'.
  1948. Most likely, the system-specific version of this variable
  1949. does define this command, but you can overrule/replace it
  1950. here.
  1951. string A command to be executed by a shell; %s will be replaced
  1952. by the path to the file.
  1953. sexp A Lisp form which will be evaluated. The file path will
  1954. be available in the Lisp variable `file'.
  1955. For more examples, see the system specific constants
  1956. `org-file-apps-defaults-macosx'
  1957. `org-file-apps-defaults-windowsnt'
  1958. `org-file-apps-defaults-gnu'."
  1959. :group 'org-link-follow
  1960. :type '(repeat
  1961. (cons (choice :value ""
  1962. (string :tag "Extension")
  1963. (const :tag "System command to open files" system)
  1964. (const :tag "Default for unrecognized files" t)
  1965. (const :tag "Remote file" remote)
  1966. (const :tag "Links to a directory" directory)
  1967. (const :tag "Any files that have Emacs modes"
  1968. auto-mode))
  1969. (choice :value ""
  1970. (const :tag "Visit with Emacs" emacs)
  1971. (const :tag "Use default" default)
  1972. (const :tag "Use the system command" system)
  1973. (string :tag "Command")
  1974. (sexp :tag "Lisp form")))))
  1975. (defcustom org-doi-server-url "http://dx.doi.org/"
  1976. "The URL of the DOI server."
  1977. :type 'string
  1978. :version "24.3"
  1979. :group 'org-link-follow)
  1980. (defgroup org-refile nil
  1981. "Options concerning refiling entries in Org-mode."
  1982. :tag "Org Refile"
  1983. :group 'org)
  1984. (defcustom org-directory "~/org"
  1985. "Directory with org files.
  1986. This is just a default location to look for Org files. There is no need
  1987. at all to put your files into this directory. It is only used in the
  1988. following situations:
  1989. 1. When a capture template specifies a target file that is not an
  1990. absolute path. The path will then be interpreted relative to
  1991. `org-directory'
  1992. 2. When a capture note is filed away in an interactive way (when exiting the
  1993. note buffer with `C-1 C-c C-c'. The user is prompted for an org file,
  1994. with `org-directory' as the default path."
  1995. :group 'org-refile
  1996. :group 'org-capture
  1997. :type 'directory)
  1998. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1999. "Default target for storing notes.
  2000. Used as a fall back file for org-capture.el, for templates that
  2001. do not specify a target file."
  2002. :group 'org-refile
  2003. :group 'org-capture
  2004. :type 'file)
  2005. (defcustom org-goto-interface 'outline
  2006. "The default interface to be used for `org-goto'.
  2007. Allowed values are:
  2008. outline The interface shows an outline of the relevant file
  2009. and the correct heading is found by moving through
  2010. the outline or by searching with incremental search.
  2011. outline-path-completion Headlines in the current buffer are offered via
  2012. completion. This is the interface also used by
  2013. the refile command."
  2014. :group 'org-refile
  2015. :type '(choice
  2016. (const :tag "Outline" outline)
  2017. (const :tag "Outline-path-completion" outline-path-completion)))
  2018. (defcustom org-goto-max-level 5
  2019. "Maximum target level when running `org-goto' with refile interface."
  2020. :group 'org-refile
  2021. :type 'integer)
  2022. (defcustom org-reverse-note-order nil
  2023. "Non-nil means store new notes at the beginning of a file or entry.
  2024. When nil, new notes will be filed to the end of a file or entry.
  2025. This can also be a list with cons cells of regular expressions that
  2026. are matched against file names, and values."
  2027. :group 'org-capture
  2028. :group 'org-refile
  2029. :type '(choice
  2030. (const :tag "Reverse always" t)
  2031. (const :tag "Reverse never" nil)
  2032. (repeat :tag "By file name regexp"
  2033. (cons regexp boolean))))
  2034. (defcustom org-log-refile nil
  2035. "Information to record when a task is refiled.
  2036. Possible values are:
  2037. nil Don't add anything
  2038. time Add a time stamp to the task
  2039. note Prompt for a note and add it with template `org-log-note-headings'
  2040. This option can also be set with on a per-file-basis with
  2041. #+STARTUP: nologrefile
  2042. #+STARTUP: logrefile
  2043. #+STARTUP: lognoterefile
  2044. You can have local logging settings for a subtree by setting the LOGGING
  2045. property to one or more of these keywords.
  2046. When bulk-refiling from the agenda, the value `note' is forbidden and
  2047. will temporarily be changed to `time'."
  2048. :group 'org-refile
  2049. :group 'org-progress
  2050. :version "24.1"
  2051. :type '(choice
  2052. (const :tag "No logging" nil)
  2053. (const :tag "Record timestamp" time)
  2054. (const :tag "Record timestamp with note." note)))
  2055. (defcustom org-refile-targets nil
  2056. "Targets for refiling entries with \\[org-refile].
  2057. This is a list of cons cells. Each cell contains:
  2058. - a specification of the files to be considered, either a list of files,
  2059. or a symbol whose function or variable value will be used to retrieve
  2060. a file name or a list of file names. If you use `org-agenda-files' for
  2061. that, all agenda files will be scanned for targets. Nil means consider
  2062. headings in the current buffer.
  2063. - A specification of how to find candidate refile targets. This may be
  2064. any of:
  2065. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  2066. This tag has to be present in all target headlines, inheritance will
  2067. not be considered.
  2068. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  2069. todo keyword.
  2070. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  2071. headlines that are refiling targets.
  2072. - a cons cell (:level . N). Any headline of level N is considered a target.
  2073. Note that, when `org-odd-levels-only' is set, level corresponds to
  2074. order in hierarchy, not to the number of stars.
  2075. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  2076. Note that, when `org-odd-levels-only' is set, level corresponds to
  2077. order in hierarchy, not to the number of stars.
  2078. Each element of this list generates a set of possible targets.
  2079. The union of these sets is presented (with completion) to
  2080. the user by `org-refile'.
  2081. You can set the variable `org-refile-target-verify-function' to a function
  2082. to verify each headline found by the simple criteria above.
  2083. When this variable is nil, all top-level headlines in the current buffer
  2084. are used, equivalent to the value `((nil . (:level . 1))'."
  2085. :group 'org-refile
  2086. :type '(repeat
  2087. (cons
  2088. (choice :value org-agenda-files
  2089. (const :tag "All agenda files" org-agenda-files)
  2090. (const :tag "Current buffer" nil)
  2091. (function) (variable) (file))
  2092. (choice :tag "Identify target headline by"
  2093. (cons :tag "Specific tag" (const :value :tag) (string))
  2094. (cons :tag "TODO keyword" (const :value :todo) (string))
  2095. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  2096. (cons :tag "Level number" (const :value :level) (integer))
  2097. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  2098. (defcustom org-refile-target-verify-function nil
  2099. "Function to verify if the headline at point should be a refile target.
  2100. The function will be called without arguments, with point at the
  2101. beginning of the headline. It should return t and leave point
  2102. where it is if the headline is a valid target for refiling.
  2103. If the target should not be selected, the function must return nil.
  2104. In addition to this, it may move point to a place from where the search
  2105. should be continued. For example, the function may decide that the entire
  2106. subtree of the current entry should be excluded and move point to the end
  2107. of the subtree."
  2108. :group 'org-refile
  2109. :type '(choice
  2110. (const nil)
  2111. (function)))
  2112. (defcustom org-refile-use-cache nil
  2113. "Non-nil means cache refile targets to speed up the process.
  2114. The cache for a particular file will be updated automatically when
  2115. the buffer has been killed, or when any of the marker used for flagging
  2116. refile targets no longer points at a live buffer.
  2117. If you have added new entries to a buffer that might themselves be targets,
  2118. you need to clear the cache manually by pressing `C-0 C-c C-w' or, if you
  2119. find that easier, `C-u C-u C-u C-c C-w'."
  2120. :group 'org-refile
  2121. :version "24.1"
  2122. :type 'boolean)
  2123. (defcustom org-refile-use-outline-path nil
  2124. "Non-nil means provide refile targets as paths.
  2125. So a level 3 headline will be available as level1/level2/level3.
  2126. When the value is `file', also include the file name (without directory)
  2127. into the path. In this case, you can also stop the completion after
  2128. the file name, to get entries inserted as top level in the file.
  2129. When `full-file-path', include the full file path."
  2130. :group 'org-refile
  2131. :type '(choice
  2132. (const :tag "Not" nil)
  2133. (const :tag "Yes" t)
  2134. (const :tag "Start with file name" file)
  2135. (const :tag "Start with full file path" full-file-path)))
  2136. (defcustom org-outline-path-complete-in-steps t
  2137. "Non-nil means complete the outline path in hierarchical steps.
  2138. When Org-mode uses the refile interface to select an outline path
  2139. \(see variable `org-refile-use-outline-path'), the completion of
  2140. the path can be done in a single go, or it can be done in steps down
  2141. the headline hierarchy. Going in steps is probably the best if you
  2142. do not use a special completion package like `ido' or `icicles'.
  2143. However, when using these packages, going in one step can be very
  2144. fast, while still showing the whole path to the entry."
  2145. :group 'org-refile
  2146. :type 'boolean)
  2147. (defcustom org-refile-allow-creating-parent-nodes nil
  2148. "Non-nil means allow to create new nodes as refile targets.
  2149. New nodes are then created by adding \"/new node name\" to the completion
  2150. of an existing node. When the value of this variable is `confirm',
  2151. new node creation must be confirmed by the user (recommended).
  2152. When nil, the completion must match an existing entry.
  2153. Note that, if the new heading is not seen by the criteria
  2154. listed in `org-refile-targets', multiple instances of the same
  2155. heading would be created by trying again to file under the new
  2156. heading."
  2157. :group 'org-refile
  2158. :type '(choice
  2159. (const :tag "Never" nil)
  2160. (const :tag "Always" t)
  2161. (const :tag "Prompt for confirmation" confirm)))
  2162. (defcustom org-refile-active-region-within-subtree nil
  2163. "Non-nil means also refile active region within a subtree.
  2164. By default `org-refile' doesn't allow refiling regions if they
  2165. don't contain a set of subtrees, but it might be convenient to
  2166. do so sometimes: in that case, the first line of the region is
  2167. converted to a headline before refiling."
  2168. :group 'org-refile
  2169. :version "24.1"
  2170. :type 'boolean)
  2171. (defgroup org-todo nil
  2172. "Options concerning TODO items in Org-mode."
  2173. :tag "Org TODO"
  2174. :group 'org)
  2175. (defgroup org-progress nil
  2176. "Options concerning Progress logging in Org-mode."
  2177. :tag "Org Progress"
  2178. :group 'org-time)
  2179. (defvar org-todo-interpretation-widgets
  2180. '((:tag "Sequence (cycling hits every state)" sequence)
  2181. (:tag "Type (cycling directly to DONE)" type))
  2182. "The available interpretation symbols for customizing `org-todo-keywords'.
  2183. Interested libraries should add to this list.")
  2184. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  2185. "List of TODO entry keyword sequences and their interpretation.
  2186. \\<org-mode-map>This is a list of sequences.
  2187. Each sequence starts with a symbol, either `sequence' or `type',
  2188. indicating if the keywords should be interpreted as a sequence of
  2189. action steps, or as different types of TODO items. The first
  2190. keywords are states requiring action - these states will select a headline
  2191. for inclusion into the global TODO list Org-mode produces. If one of
  2192. the \"keywords\" is the vertical bar, \"|\", the remaining keywords
  2193. signify that no further action is necessary. If \"|\" is not found,
  2194. the last keyword is treated as the only DONE state of the sequence.
  2195. The command \\[org-todo] cycles an entry through these states, and one
  2196. additional state where no keyword is present. For details about this
  2197. cycling, see the manual.
  2198. TODO keywords and interpretation can also be set on a per-file basis with
  2199. the special #+SEQ_TODO and #+TYP_TODO lines.
  2200. Each keyword can optionally specify a character for fast state selection
  2201. \(in combination with the variable `org-use-fast-todo-selection')
  2202. and specifiers for state change logging, using the same syntax that
  2203. is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says that
  2204. the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  2205. indicates to record a time stamp each time this state is selected.
  2206. Each keyword may also specify if a timestamp or a note should be
  2207. recorded when entering or leaving the state, by adding additional
  2208. characters in the parenthesis after the keyword. This looks like this:
  2209. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  2210. record only the time of the state change. With X and Y being either
  2211. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  2212. Y when leaving the state if and only if the *target* state does not
  2213. define X. You may omit any of the fast-selection key or X or /Y,
  2214. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  2215. For backward compatibility, this variable may also be just a list
  2216. of keywords. In this case the interpretation (sequence or type) will be
  2217. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  2218. :group 'org-todo
  2219. :group 'org-keywords
  2220. :type '(choice
  2221. (repeat :tag "Old syntax, just keywords"
  2222. (string :tag "Keyword"))
  2223. (repeat :tag "New syntax"
  2224. (cons
  2225. (choice
  2226. :tag "Interpretation"
  2227. ;;Quick and dirty way to see
  2228. ;;`org-todo-interpretations'. This takes the
  2229. ;;place of item arguments
  2230. :convert-widget
  2231. (lambda (widget)
  2232. (widget-put widget
  2233. :args (mapcar
  2234. (lambda (x)
  2235. (widget-convert
  2236. (cons 'const x)))
  2237. org-todo-interpretation-widgets))
  2238. widget))
  2239. (repeat
  2240. (string :tag "Keyword"))))))
  2241. (defvar org-todo-keywords-1 nil
  2242. "All TODO and DONE keywords active in a buffer.")
  2243. (make-variable-buffer-local 'org-todo-keywords-1)
  2244. (defvar org-todo-keywords-for-agenda nil)
  2245. (defvar org-done-keywords-for-agenda nil)
  2246. (defvar org-todo-keyword-alist-for-agenda nil)
  2247. (defvar org-tag-alist-for-agenda nil
  2248. "Alist of all tags from all agenda files.")
  2249. (defvar org-tag-groups-alist-for-agenda nil
  2250. "Alist of all groups tags from all current agenda files.")
  2251. (defvar org-tag-groups-alist nil)
  2252. (make-variable-buffer-local 'org-tag-groups-alist)
  2253. (defvar org-agenda-contributing-files nil)
  2254. (defvar org-not-done-keywords nil)
  2255. (make-variable-buffer-local 'org-not-done-keywords)
  2256. (defvar org-done-keywords nil)
  2257. (make-variable-buffer-local 'org-done-keywords)
  2258. (defvar org-todo-heads nil)
  2259. (make-variable-buffer-local 'org-todo-heads)
  2260. (defvar org-todo-sets nil)
  2261. (make-variable-buffer-local 'org-todo-sets)
  2262. (defvar org-todo-log-states nil)
  2263. (make-variable-buffer-local 'org-todo-log-states)
  2264. (defvar org-todo-kwd-alist nil)
  2265. (make-variable-buffer-local 'org-todo-kwd-alist)
  2266. (defvar org-todo-key-alist nil)
  2267. (make-variable-buffer-local 'org-todo-key-alist)
  2268. (defvar org-todo-key-trigger nil)
  2269. (make-variable-buffer-local 'org-todo-key-trigger)
  2270. (defcustom org-todo-interpretation 'sequence
  2271. "Controls how TODO keywords are interpreted.
  2272. This variable is in principle obsolete and is only used for
  2273. backward compatibility, if the interpretation of todo keywords is
  2274. not given already in `org-todo-keywords'. See that variable for
  2275. more information."
  2276. :group 'org-todo
  2277. :group 'org-keywords
  2278. :type '(choice (const sequence)
  2279. (const type)))
  2280. (defcustom org-use-fast-todo-selection t
  2281. "Non-nil means use the fast todo selection scheme with C-c C-t.
  2282. This variable describes if and under what circumstances the cycling
  2283. mechanism for TODO keywords will be replaced by a single-key, direct
  2284. selection scheme.
  2285. When nil, fast selection is never used.
  2286. When the symbol `prefix', it will be used when `org-todo' is called
  2287. with a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and
  2288. `C-u t' in an agenda buffer.
  2289. When t, fast selection is used by default. In this case, the prefix
  2290. argument forces cycling instead.
  2291. In all cases, the special interface is only used if access keys have
  2292. actually been assigned by the user, i.e. if keywords in the configuration
  2293. are followed by a letter in parenthesis, like TODO(t)."
  2294. :group 'org-todo
  2295. :type '(choice
  2296. (const :tag "Never" nil)
  2297. (const :tag "By default" t)
  2298. (const :tag "Only with C-u C-c C-t" prefix)))
  2299. (defcustom org-provide-todo-statistics t
  2300. "Non-nil means update todo statistics after insert and toggle.
  2301. ALL-HEADLINES means update todo statistics by including headlines
  2302. with no TODO keyword as well, counting them as not done.
  2303. A list of TODO keywords means the same, but skip keywords that are
  2304. not in this list.
  2305. When set to a list of two lists, the first list contains keywords
  2306. to consider as TODO keywords, the second list contains keywords
  2307. to consider as DONE keywords.
  2308. When this is set, todo statistics is updated in the parent of the
  2309. current entry each time a todo state is changed."
  2310. :group 'org-todo
  2311. :type '(choice
  2312. (const :tag "Yes, only for TODO entries" t)
  2313. (const :tag "Yes, including all entries" all-headlines)
  2314. (repeat :tag "Yes, for TODOs in this list"
  2315. (string :tag "TODO keyword"))
  2316. (list :tag "Yes, for TODOs and DONEs in these lists"
  2317. (repeat (string :tag "TODO keyword"))
  2318. (repeat (string :tag "DONE keyword")))
  2319. (other :tag "No TODO statistics" nil)))
  2320. (defcustom org-hierarchical-todo-statistics t
  2321. "Non-nil means TODO statistics covers just direct children.
  2322. When nil, all entries in the subtree are considered.
  2323. This has only an effect if `org-provide-todo-statistics' is set.
  2324. To set this to nil for only a single subtree, use a COOKIE_DATA
  2325. property and include the word \"recursive\" into the value."
  2326. :group 'org-todo
  2327. :type 'boolean)
  2328. (defcustom org-after-todo-state-change-hook nil
  2329. "Hook which is run after the state of a TODO item was changed.
  2330. The new state (a string with a TODO keyword, or nil) is available in the
  2331. Lisp variable `org-state'."
  2332. :group 'org-todo
  2333. :type 'hook)
  2334. (defvar org-blocker-hook nil
  2335. "Hook for functions that are allowed to block a state change.
  2336. Functions in this hook should not modify the buffer.
  2337. Each function gets as its single argument a property list,
  2338. see `org-trigger-hook' for more information about this list.
  2339. If any of the functions in this hook returns nil, the state change
  2340. is blocked.")
  2341. (defvar org-trigger-hook nil
  2342. "Hook for functions that are triggered by a state change.
  2343. Each function gets as its single argument a property list with at
  2344. least the following elements:
  2345. (:type type-of-change :position pos-at-entry-start
  2346. :from old-state :to new-state)
  2347. Depending on the type, more properties may be present.
  2348. This mechanism is currently implemented for:
  2349. TODO state changes
  2350. ------------------
  2351. :type todo-state-change
  2352. :from previous state (keyword as a string), or nil, or a symbol
  2353. 'todo' or 'done', to indicate the general type of state.
  2354. :to new state, like in :from")
  2355. (defcustom org-enforce-todo-dependencies nil
  2356. "Non-nil means undone TODO entries will block switching the parent to DONE.
  2357. Also, if a parent has an :ORDERED: property, switching an entry to DONE will
  2358. be blocked if any prior sibling is not yet done.
  2359. Finally, if the parent is blocked because of ordered siblings of its own,
  2360. the child will also be blocked."
  2361. :set (lambda (var val)
  2362. (set var val)
  2363. (if val
  2364. (add-hook 'org-blocker-hook
  2365. 'org-block-todo-from-children-or-siblings-or-parent)
  2366. (remove-hook 'org-blocker-hook
  2367. 'org-block-todo-from-children-or-siblings-or-parent)))
  2368. :group 'org-todo
  2369. :type 'boolean)
  2370. (defcustom org-enforce-todo-checkbox-dependencies nil
  2371. "Non-nil means unchecked boxes will block switching the parent to DONE.
  2372. When this is nil, checkboxes have no influence on switching TODO states.
  2373. When non-nil, you first need to check off all check boxes before the TODO
  2374. entry can be switched to DONE.
  2375. This variable needs to be set before org.el is loaded, and you need to
  2376. restart Emacs after a change to make the change effective. The only way
  2377. to change is while Emacs is running is through the customize interface."
  2378. :set (lambda (var val)
  2379. (set var val)
  2380. (if val
  2381. (add-hook 'org-blocker-hook
  2382. 'org-block-todo-from-checkboxes)
  2383. (remove-hook 'org-blocker-hook
  2384. 'org-block-todo-from-checkboxes)))
  2385. :group 'org-todo
  2386. :type 'boolean)
  2387. (defcustom org-treat-insert-todo-heading-as-state-change nil
  2388. "Non-nil means inserting a TODO heading is treated as state change.
  2389. So when the command \\[org-insert-todo-heading] is used, state change
  2390. logging will apply if appropriate. When nil, the new TODO item will
  2391. be inserted directly, and no logging will take place."
  2392. :group 'org-todo
  2393. :type 'boolean)
  2394. (defcustom org-treat-S-cursor-todo-selection-as-state-change t
  2395. "Non-nil means switching TODO states with S-cursor counts as state change.
  2396. This is the default behavior. However, setting this to nil allows a
  2397. convenient way to select a TODO state and bypass any logging associated
  2398. with that."
  2399. :group 'org-todo
  2400. :type 'boolean)
  2401. (defcustom org-todo-state-tags-triggers nil
  2402. "Tag changes that should be triggered by TODO state changes.
  2403. This is a list. Each entry is
  2404. (state-change (tag . flag) .......)
  2405. State-change can be a string with a state, and empty string to indicate the
  2406. state that has no TODO keyword, or it can be one of the symbols `todo'
  2407. or `done', meaning any not-done or done state, respectively."
  2408. :group 'org-todo
  2409. :group 'org-tags
  2410. :type '(repeat
  2411. (cons (choice :tag "When changing to"
  2412. (const :tag "Not-done state" todo)
  2413. (const :tag "Done state" done)
  2414. (string :tag "State"))
  2415. (repeat
  2416. (cons :tag "Tag action"
  2417. (string :tag "Tag")
  2418. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  2419. (defcustom org-log-done nil
  2420. "Information to record when a task moves to the DONE state.
  2421. Possible values are:
  2422. nil Don't add anything, just change the keyword
  2423. time Add a time stamp to the task
  2424. note Prompt for a note and add it with template `org-log-note-headings'
  2425. This option can also be set with on a per-file-basis with
  2426. #+STARTUP: nologdone
  2427. #+STARTUP: logdone
  2428. #+STARTUP: lognotedone
  2429. You can have local logging settings for a subtree by setting the LOGGING
  2430. property to one or more of these keywords."
  2431. :group 'org-todo
  2432. :group 'org-progress
  2433. :type '(choice
  2434. (const :tag "No logging" nil)
  2435. (const :tag "Record CLOSED timestamp" time)
  2436. (const :tag "Record CLOSED timestamp with note." note)))
  2437. ;; Normalize old uses of org-log-done.
  2438. (cond
  2439. ((eq org-log-done t) (setq org-log-done 'time))
  2440. ((and (listp org-log-done) (memq 'done org-log-done))
  2441. (setq org-log-done 'note)))
  2442. (defcustom org-log-reschedule nil
  2443. "Information to record when the scheduling date of a tasks is modified.
  2444. Possible values are:
  2445. nil Don't add anything, just change the date
  2446. time Add a time stamp to the task
  2447. note Prompt for a note and add it with template `org-log-note-headings'
  2448. This option can also be set with on a per-file-basis with
  2449. #+STARTUP: nologreschedule
  2450. #+STARTUP: logreschedule
  2451. #+STARTUP: lognotereschedule"
  2452. :group 'org-todo
  2453. :group 'org-progress
  2454. :type '(choice
  2455. (const :tag "No logging" nil)
  2456. (const :tag "Record timestamp" time)
  2457. (const :tag "Record timestamp with note." note)))
  2458. (defcustom org-log-redeadline nil
  2459. "Information to record when the deadline date of a tasks is modified.
  2460. Possible values are:
  2461. nil Don't add anything, just change the date
  2462. time Add a time stamp to the task
  2463. note Prompt for a note and add it with template `org-log-note-headings'
  2464. This option can also be set with on a per-file-basis with
  2465. #+STARTUP: nologredeadline
  2466. #+STARTUP: logredeadline
  2467. #+STARTUP: lognoteredeadline
  2468. You can have local logging settings for a subtree by setting the LOGGING
  2469. property to one or more of these keywords."
  2470. :group 'org-todo
  2471. :group 'org-progress
  2472. :type '(choice
  2473. (const :tag "No logging" nil)
  2474. (const :tag "Record timestamp" time)
  2475. (const :tag "Record timestamp with note." note)))
  2476. (defcustom org-log-note-clock-out nil
  2477. "Non-nil means record a note when clocking out of an item.
  2478. This can also be configured on a per-file basis by adding one of
  2479. the following lines anywhere in the buffer:
  2480. #+STARTUP: lognoteclock-out
  2481. #+STARTUP: nolognoteclock-out"
  2482. :group 'org-todo
  2483. :group 'org-progress
  2484. :type 'boolean)
  2485. (defcustom org-log-done-with-time t
  2486. "Non-nil means the CLOSED time stamp will contain date and time.
  2487. When nil, only the date will be recorded."
  2488. :group 'org-progress
  2489. :type 'boolean)
  2490. (defcustom org-log-note-headings
  2491. '((done . "CLOSING NOTE %t")
  2492. (state . "State %-12s from %-12S %t")
  2493. (note . "Note taken on %t")
  2494. (reschedule . "Rescheduled from %S on %t")
  2495. (delschedule . "Not scheduled, was %S on %t")
  2496. (redeadline . "New deadline from %S on %t")
  2497. (deldeadline . "Removed deadline, was %S on %t")
  2498. (refile . "Refiled on %t")
  2499. (clock-out . ""))
  2500. "Headings for notes added to entries.
  2501. The value is an alist, with the car being a symbol indicating the
  2502. note context, and the cdr is the heading to be used. The heading
  2503. may also be the empty string. The following placeholders can be
  2504. used:
  2505. %t a time stamp.
  2506. %T an active time stamp instead the default inactive one
  2507. %d a short-format time stamp.
  2508. %D an active short-format time stamp.
  2509. %s the new TODO state or time stamp (inactive), in double quotes.
  2510. %S the old TODO state or time stamp (inactive), in double quotes.
  2511. %u the user name.
  2512. %U full user name.
  2513. In fact, it is not a good idea to change the `state' entry,
  2514. because Agenda Log mode depends on the format of these entries."
  2515. :group 'org-todo
  2516. :group 'org-progress
  2517. :type '(list :greedy t
  2518. (cons (const :tag "Heading when closing an item" done) string)
  2519. (cons (const :tag
  2520. "Heading when changing todo state (todo sequence only)"
  2521. state) string)
  2522. (cons (const :tag "Heading when just taking a note" note) string)
  2523. (cons (const :tag "Heading when rescheduling" reschedule) string)
  2524. (cons (const :tag "Heading when an item is no longer scheduled" delschedule) string)
  2525. (cons (const :tag "Heading when changing deadline" redeadline) string)
  2526. (cons (const :tag "Heading when deleting a deadline" deldeadline) string)
  2527. (cons (const :tag "Heading when refiling" refile) string)
  2528. (cons (const :tag "Heading when clocking out" clock-out) string)))
  2529. (unless (assq 'note org-log-note-headings)
  2530. (push '(note . "%t") org-log-note-headings))
  2531. (defcustom org-log-into-drawer nil
  2532. "Non-nil means insert state change notes and time stamps into a drawer.
  2533. When nil, state changes notes will be inserted after the headline and
  2534. any scheduling and clock lines, but not inside a drawer.
  2535. The value of this variable should be the name of the drawer to use.
  2536. LOGBOOK is proposed as the default drawer for this purpose, you can
  2537. also set this to a string to define the drawer of your choice.
  2538. A value of t is also allowed, representing \"LOGBOOK\".
  2539. A value of t or nil can also be set with on a per-file-basis with
  2540. #+STARTUP: logdrawer
  2541. #+STARTUP: nologdrawer
  2542. If this variable is set, `org-log-state-notes-insert-after-drawers'
  2543. will be ignored.
  2544. You can set the property LOG_INTO_DRAWER to overrule this setting for
  2545. a subtree.
  2546. Do not check directly this variable in a Lisp program. Call
  2547. function `org-log-into-drawer' instead."
  2548. :group 'org-todo
  2549. :group 'org-progress
  2550. :type '(choice
  2551. (const :tag "Not into a drawer" nil)
  2552. (const :tag "LOGBOOK" t)
  2553. (string :tag "Other")))
  2554. (org-defvaralias 'org-log-state-notes-into-drawer 'org-log-into-drawer)
  2555. (defun org-log-into-drawer ()
  2556. "Name of the log drawer, as a string, or nil.
  2557. This is the value of `org-log-into-drawer'. However, if the
  2558. current entry has or inherits a LOG_INTO_DRAWER property, it will
  2559. be used instead of the default value."
  2560. (let ((p (org-entry-get nil "LOG_INTO_DRAWER" 'inherit t)))
  2561. (cond ((equal p "nil") nil)
  2562. ((equal p "t") "LOGBOOK")
  2563. ((stringp p) p)
  2564. (p "LOGBOOK")
  2565. ((stringp org-log-into-drawer) org-log-into-drawer)
  2566. (org-log-into-drawer "LOGBOOK"))))
  2567. (defcustom org-log-state-notes-insert-after-drawers nil
  2568. "Non-nil means insert state change notes after any drawers in entry.
  2569. Only the drawers that *immediately* follow the headline and the
  2570. deadline/scheduled line are skipped.
  2571. When nil, insert notes right after the heading and perhaps the line
  2572. with deadline/scheduling if present.
  2573. This variable will have no effect if `org-log-into-drawer' is
  2574. set."
  2575. :group 'org-todo
  2576. :group 'org-progress
  2577. :type 'boolean)
  2578. (defcustom org-log-states-order-reversed t
  2579. "Non-nil means the latest state note will be directly after heading.
  2580. When nil, the state change notes will be ordered according to time.
  2581. This option can also be set with on a per-file-basis with
  2582. #+STARTUP: logstatesreversed
  2583. #+STARTUP: nologstatesreversed"
  2584. :group 'org-todo
  2585. :group 'org-progress
  2586. :type 'boolean)
  2587. (defcustom org-todo-repeat-to-state nil
  2588. "The TODO state to which a repeater should return the repeating task.
  2589. By default this is the first task in a TODO sequence, or the previous state
  2590. in a TODO_TYP set. But you can specify another task here.
  2591. alternatively, set the :REPEAT_TO_STATE: property of the entry."
  2592. :group 'org-todo
  2593. :version "24.1"
  2594. :type '(choice (const :tag "Head of sequence" nil)
  2595. (string :tag "Specific state")))
  2596. (defcustom org-log-repeat 'time
  2597. "Non-nil means record moving through the DONE state when triggering repeat.
  2598. An auto-repeating task is immediately switched back to TODO when
  2599. marked DONE. If you are not logging state changes (by adding \"@\"
  2600. or \"!\" to the TODO keyword definition), or set `org-log-done' to
  2601. record a closing note, there will be no record of the task moving
  2602. through DONE. This variable forces taking a note anyway.
  2603. nil Don't force a record
  2604. time Record a time stamp
  2605. note Prompt for a note and add it with template `org-log-note-headings'
  2606. This option can also be set with on a per-file-basis with
  2607. #+STARTUP: nologrepeat
  2608. #+STARTUP: logrepeat
  2609. #+STARTUP: lognoterepeat
  2610. You can have local logging settings for a subtree by setting the LOGGING
  2611. property to one or more of these keywords."
  2612. :group 'org-todo
  2613. :group 'org-progress
  2614. :type '(choice
  2615. (const :tag "Don't force a record" nil)
  2616. (const :tag "Force recording the DONE state" time)
  2617. (const :tag "Force recording a note with the DONE state" note)))
  2618. (defgroup org-priorities nil
  2619. "Priorities in Org-mode."
  2620. :tag "Org Priorities"
  2621. :group 'org-todo)
  2622. (defcustom org-enable-priority-commands t
  2623. "Non-nil means priority commands are active.
  2624. When nil, these commands will be disabled, so that you never accidentally
  2625. set a priority."
  2626. :group 'org-priorities
  2627. :type 'boolean)
  2628. (defcustom org-highest-priority ?A
  2629. "The highest priority of TODO items. A character like ?A, ?B etc.
  2630. Must have a smaller ASCII number than `org-lowest-priority'."
  2631. :group 'org-priorities
  2632. :type 'character)
  2633. (defcustom org-lowest-priority ?C
  2634. "The lowest priority of TODO items. A character like ?A, ?B etc.
  2635. Must have a larger ASCII number than `org-highest-priority'."
  2636. :group 'org-priorities
  2637. :type 'character)
  2638. (defcustom org-default-priority ?B
  2639. "The default priority of TODO items.
  2640. This is the priority an item gets if no explicit priority is given.
  2641. When starting to cycle on an empty priority the first step in the cycle
  2642. depends on `org-priority-start-cycle-with-default'. The resulting first
  2643. step priority must not exceed the range from `org-highest-priority' to
  2644. `org-lowest-priority' which means that `org-default-priority' has to be
  2645. in this range exclusive or inclusive the range boundaries. Else the
  2646. first step refuses to set the default and the second will fall back
  2647. to (depending on the command used) the highest or lowest priority."
  2648. :group 'org-priorities
  2649. :type 'character)
  2650. (defcustom org-priority-start-cycle-with-default t
  2651. "Non-nil means start with default priority when starting to cycle.
  2652. When this is nil, the first step in the cycle will be (depending on the
  2653. command used) one higher or lower than the default priority.
  2654. See also `org-default-priority'."
  2655. :group 'org-priorities
  2656. :type 'boolean)
  2657. (defcustom org-get-priority-function nil
  2658. "Function to extract the priority from a string.
  2659. The string is normally the headline. If this is nil Org computes the
  2660. priority from the priority cookie like [#A] in the headline. It returns
  2661. an integer, increasing by 1000 for each priority level.
  2662. The user can set a different function here, which should take a string
  2663. as an argument and return the numeric priority."
  2664. :group 'org-priorities
  2665. :version "24.1"
  2666. :type '(choice
  2667. (const nil)
  2668. (function)))
  2669. (defgroup org-time nil
  2670. "Options concerning time stamps and deadlines in Org-mode."
  2671. :tag "Org Time"
  2672. :group 'org)
  2673. (defcustom org-time-stamp-rounding-minutes '(0 5)
  2674. "Number of minutes to round time stamps to.
  2675. These are two values, the first applies when first creating a time stamp.
  2676. The second applies when changing it with the commands `S-up' and `S-down'.
  2677. When changing the time stamp, this means that it will change in steps
  2678. of N minutes, as given by the second value.
  2679. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  2680. numbers should be factors of 60, so for example 5, 10, 15.
  2681. When this is larger than 1, you can still force an exact time stamp by using
  2682. a double prefix argument to a time stamp command like `C-c .' or `C-c !',
  2683. and by using a prefix arg to `S-up/down' to specify the exact number
  2684. of minutes to shift."
  2685. :group 'org-time
  2686. :get (lambda (var) ; Make sure both elements are there
  2687. (if (integerp (default-value var))
  2688. (list (default-value var) 5)
  2689. (default-value var)))
  2690. :type '(list
  2691. (integer :tag "when inserting times")
  2692. (integer :tag "when modifying times")))
  2693. ;; Normalize old customizations of this variable.
  2694. (when (integerp org-time-stamp-rounding-minutes)
  2695. (setq org-time-stamp-rounding-minutes
  2696. (list org-time-stamp-rounding-minutes
  2697. org-time-stamp-rounding-minutes)))
  2698. (defcustom org-display-custom-times nil
  2699. "Non-nil means overlay custom formats over all time stamps.
  2700. The formats are defined through the variable `org-time-stamp-custom-formats'.
  2701. To turn this on on a per-file basis, insert anywhere in the file:
  2702. #+STARTUP: customtime"
  2703. :group 'org-time
  2704. :set 'set-default
  2705. :type 'sexp)
  2706. (make-variable-buffer-local 'org-display-custom-times)
  2707. (defcustom org-time-stamp-custom-formats
  2708. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  2709. "Custom formats for time stamps. See `format-time-string' for the syntax.
  2710. These are overlaid over the default ISO format if the variable
  2711. `org-display-custom-times' is set. Time like %H:%M should be at the
  2712. end of the second format. The custom formats are also honored by export
  2713. commands, if custom time display is turned on at the time of export."
  2714. :group 'org-time
  2715. :type 'sexp)
  2716. (defun org-time-stamp-format (&optional long inactive)
  2717. "Get the right format for a time string."
  2718. (let ((f (if long (cdr org-time-stamp-formats)
  2719. (car org-time-stamp-formats))))
  2720. (if inactive
  2721. (concat "[" (substring f 1 -1) "]")
  2722. f)))
  2723. (defcustom org-time-clocksum-format
  2724. '(:days "%dd " :hours "%d" :require-hours t :minutes ":%02d" :require-minutes t)
  2725. "The format string used when creating CLOCKSUM lines.
  2726. This is also used when Org mode generates a time duration.
  2727. The value can be a single format string containing two
  2728. %-sequences, which will be filled with the number of hours and
  2729. minutes in that order.
  2730. Alternatively, the value can be a plist associating any of the
  2731. keys :years, :months, :weeks, :days, :hours or :minutes with
  2732. format strings. The time duration is formatted using only the
  2733. time components that are needed and concatenating the results.
  2734. If a time unit in absent, it falls back to the next smallest
  2735. unit.
  2736. The keys :require-years, :require-months, :require-days,
  2737. :require-weeks, :require-hours, :require-minutes are also
  2738. meaningful. A non-nil value for these keys indicates that the
  2739. corresponding time component should always be included, even if
  2740. its value is 0.
  2741. For example,
  2742. \(:days \"%dd\" :hours \"%d\" :require-hours t :minutes \":%02d\"
  2743. :require-minutes t)
  2744. means durations longer than a day will be expressed in days,
  2745. hours and minutes, and durations less than a day will always be
  2746. expressed in hours and minutes (even for durations less than an
  2747. hour).
  2748. The value
  2749. \(:days \"%dd\" :minutes \"%dm\")
  2750. means durations longer than a day will be expressed in days and
  2751. minutes, and durations less than a day will be expressed entirely
  2752. in minutes (even for durations longer than an hour)."
  2753. :group 'org-time
  2754. :group 'org-clock
  2755. :version "24.4"
  2756. :package-version '(Org . "8.0")
  2757. :type '(choice (string :tag "Format string")
  2758. (set :tag "Plist"
  2759. (group :inline t (const :tag "Years" :years)
  2760. (string :tag "Format string"))
  2761. (group :inline t
  2762. (const :tag "Always show years" :require-years)
  2763. (const t))
  2764. (group :inline t (const :tag "Months" :months)
  2765. (string :tag "Format string"))
  2766. (group :inline t
  2767. (const :tag "Always show months" :require-months)
  2768. (const t))
  2769. (group :inline t (const :tag "Weeks" :weeks)
  2770. (string :tag "Format string"))
  2771. (group :inline t
  2772. (const :tag "Always show weeks" :require-weeks)
  2773. (const t))
  2774. (group :inline t (const :tag "Days" :days)
  2775. (string :tag "Format string"))
  2776. (group :inline t
  2777. (const :tag "Always show days" :require-days)
  2778. (const t))
  2779. (group :inline t (const :tag "Hours" :hours)
  2780. (string :tag "Format string"))
  2781. (group :inline t
  2782. (const :tag "Always show hours" :require-hours)
  2783. (const t))
  2784. (group :inline t (const :tag "Minutes" :minutes)
  2785. (string :tag "Format string"))
  2786. (group :inline t
  2787. (const :tag "Always show minutes" :require-minutes)
  2788. (const t)))))
  2789. (defcustom org-time-clocksum-use-fractional nil
  2790. "When non-nil, \\[org-clock-display] uses fractional times.
  2791. See `org-time-clocksum-format' for more on time clock formats."
  2792. :group 'org-time
  2793. :group 'org-clock
  2794. :version "24.3"
  2795. :type 'boolean)
  2796. (defcustom org-time-clocksum-use-effort-durations nil
  2797. "When non-nil, \\[org-clock-display] uses effort durations.
  2798. E.g. by default, one day is considered to be a 8 hours effort,
  2799. so a task that has been clocked for 16 hours will be displayed
  2800. as during 2 days in the clock display or in the clocktable.
  2801. See `org-effort-durations' on how to set effort durations
  2802. and `org-time-clocksum-format' for more on time clock formats."
  2803. :group 'org-time
  2804. :group 'org-clock
  2805. :version "24.4"
  2806. :package-version '(Org . "8.0")
  2807. :type 'boolean)
  2808. (defcustom org-time-clocksum-fractional-format "%.2f"
  2809. "The format string used when creating CLOCKSUM lines,
  2810. or when Org mode generates a time duration, if
  2811. `org-time-clocksum-use-fractional' is enabled.
  2812. The value can be a single format string containing one
  2813. %-sequence, which will be filled with the number of hours as
  2814. a float.
  2815. Alternatively, the value can be a plist associating any of the
  2816. keys :years, :months, :weeks, :days, :hours or :minutes with
  2817. a format string. The time duration is formatted using the
  2818. largest time unit which gives a non-zero integer part. If all
  2819. specified formats have zero integer part, the smallest time unit
  2820. is used."
  2821. :group 'org-time
  2822. :type '(choice (string :tag "Format string")
  2823. (set (group :inline t (const :tag "Years" :years)
  2824. (string :tag "Format string"))
  2825. (group :inline t (const :tag "Months" :months)
  2826. (string :tag "Format string"))
  2827. (group :inline t (const :tag "Weeks" :weeks)
  2828. (string :tag "Format string"))
  2829. (group :inline t (const :tag "Days" :days)
  2830. (string :tag "Format string"))
  2831. (group :inline t (const :tag "Hours" :hours)
  2832. (string :tag "Format string"))
  2833. (group :inline t (const :tag "Minutes" :minutes)
  2834. (string :tag "Format string")))))
  2835. (defcustom org-deadline-warning-days 14
  2836. "Number of days before expiration during which a deadline becomes active.
  2837. This variable governs the display in sparse trees and in the agenda.
  2838. When 0 or negative, it means use this number (the absolute value of it)
  2839. even if a deadline has a different individual lead time specified.
  2840. Custom commands can set this variable in the options section."
  2841. :group 'org-time
  2842. :group 'org-agenda-daily/weekly
  2843. :type 'integer)
  2844. (defcustom org-scheduled-delay-days 0
  2845. "Number of days before a scheduled item becomes active.
  2846. This variable governs the display in sparse trees and in the agenda.
  2847. The default value (i.e. 0) means: don't delay scheduled item.
  2848. When negative, it means use this number (the absolute value of it)
  2849. even if a scheduled item has a different individual delay time
  2850. specified.
  2851. Custom commands can set this variable in the options section."
  2852. :group 'org-time
  2853. :group 'org-agenda-daily/weekly
  2854. :version "24.4"
  2855. :package-version '(Org . "8.0")
  2856. :type 'integer)
  2857. (defcustom org-read-date-prefer-future t
  2858. "Non-nil means assume future for incomplete date input from user.
  2859. This affects the following situations:
  2860. 1. The user gives a month but not a year.
  2861. For example, if it is April and you enter \"feb 2\", this will be read
  2862. as Feb 2, *next* year. \"May 5\", however, will be this year.
  2863. 2. The user gives a day, but no month.
  2864. For example, if today is the 15th, and you enter \"3\", Org-mode will
  2865. read this as the third of *next* month. However, if you enter \"17\",
  2866. it will be considered as *this* month.
  2867. If you set this variable to the symbol `time', then also the following
  2868. will work:
  2869. 3. If the user gives a time.
  2870. If the time is before now, it will be interpreted as tomorrow.
  2871. Currently none of this works for ISO week specifications.
  2872. When this option is nil, the current day, month and year will always be
  2873. used as defaults.
  2874. See also `org-agenda-jump-prefer-future'."
  2875. :group 'org-time
  2876. :type '(choice
  2877. (const :tag "Never" nil)
  2878. (const :tag "Check month and day" t)
  2879. (const :tag "Check month, day, and time" time)))
  2880. (defcustom org-agenda-jump-prefer-future 'org-read-date-prefer-future
  2881. "Should the agenda jump command prefer the future for incomplete dates?
  2882. The default is to do the same as configured in `org-read-date-prefer-future'.
  2883. But you can also set a deviating value here.
  2884. This may t or nil, or the symbol `org-read-date-prefer-future'."
  2885. :group 'org-agenda
  2886. :group 'org-time
  2887. :version "24.1"
  2888. :type '(choice
  2889. (const :tag "Use org-read-date-prefer-future"
  2890. org-read-date-prefer-future)
  2891. (const :tag "Never" nil)
  2892. (const :tag "Always" t)))
  2893. (defcustom org-read-date-force-compatible-dates t
  2894. "Should date/time prompt force dates that are guaranteed to work in Emacs?
  2895. Depending on the system Emacs is running on, certain dates cannot
  2896. be represented with the type used internally to represent time.
  2897. Dates between 1970-1-1 and 2038-1-1 can always be represented
  2898. correctly. Some systems allow for earlier dates, some for later,
  2899. some for both. One way to find out it to insert any date into an
  2900. Org buffer, putting the cursor on the year and hitting S-up and
  2901. S-down to test the range.
  2902. When this variable is set to t, the date/time prompt will not let
  2903. you specify dates outside the 1970-2037 range, so it is certain that
  2904. these dates will work in whatever version of Emacs you are
  2905. running, and also that you can move a file from one Emacs implementation
  2906. to another. WHenever Org is forcing the year for you, it will display
  2907. a message and beep.
  2908. When this variable is nil, Org will check if the date is
  2909. representable in the specific Emacs implementation you are using.
  2910. If not, it will force a year, usually the current year, and beep
  2911. to remind you. Currently this setting is not recommended because
  2912. the likelihood that you will open your Org files in an Emacs that
  2913. has limited date range is not negligible.
  2914. A workaround for this problem is to use diary sexp dates for time
  2915. stamps outside of this range."
  2916. :group 'org-time
  2917. :version "24.1"
  2918. :type 'boolean)
  2919. (defcustom org-read-date-display-live t
  2920. "Non-nil means display current interpretation of date prompt live.
  2921. This display will be in an overlay, in the minibuffer."
  2922. :group 'org-time
  2923. :type 'boolean)
  2924. (defcustom org-read-date-popup-calendar t
  2925. "Non-nil means pop up a calendar when prompting for a date.
  2926. In the calendar, the date can be selected with mouse-1. However, the
  2927. minibuffer will also be active, and you can simply enter the date as well.
  2928. When nil, only the minibuffer will be available."
  2929. :group 'org-time
  2930. :type 'boolean)
  2931. (org-defvaralias 'org-popup-calendar-for-date-prompt
  2932. 'org-read-date-popup-calendar)
  2933. (make-obsolete-variable
  2934. 'org-read-date-minibuffer-setup-hook
  2935. "Set `org-read-date-minibuffer-local-map' instead." "24.4")
  2936. (defcustom org-read-date-minibuffer-setup-hook nil
  2937. "Hook to be used to set up keys for the date/time interface.
  2938. Add key definitions to `minibuffer-local-map', which will be a
  2939. temporary copy.
  2940. WARNING: This option is obsolete, you should use
  2941. `org-read-date-minibuffer-local-map' to set up keys."
  2942. :group 'org-time
  2943. :type 'hook)
  2944. (defcustom org-extend-today-until 0
  2945. "The hour when your day really ends. Must be an integer.
  2946. This has influence for the following applications:
  2947. - When switching the agenda to \"today\". It it is still earlier than
  2948. the time given here, the day recognized as TODAY is actually yesterday.
  2949. - When a date is read from the user and it is still before the time given
  2950. here, the current date and time will be assumed to be yesterday, 23:59.
  2951. Also, timestamps inserted in capture templates follow this rule.
  2952. IMPORTANT: This is a feature whose implementation is and likely will
  2953. remain incomplete. Really, it is only here because past midnight seems to
  2954. be the favorite working time of John Wiegley :-)"
  2955. :group 'org-time
  2956. :type 'integer)
  2957. (defcustom org-use-effective-time nil
  2958. "If non-nil, consider `org-extend-today-until' when creating timestamps.
  2959. For example, if `org-extend-today-until' is 8, and it's 4am, then the
  2960. \"effective time\" of any timestamps between midnight and 8am will be
  2961. 23:59 of the previous day."
  2962. :group 'org-time
  2963. :version "24.1"
  2964. :type 'boolean)
  2965. (defcustom org-use-last-clock-out-time-as-effective-time nil
  2966. "When non-nil, use the last clock out time for `org-todo'.
  2967. Note that this option has precedence over the combined use of
  2968. `org-use-effective-time' and `org-extend-today-until'."
  2969. :group 'org-time
  2970. :version "24.4"
  2971. :package-version '(Org . "8.0")
  2972. :type 'boolean)
  2973. (defcustom org-edit-timestamp-down-means-later nil
  2974. "Non-nil means S-down will increase the time in a time stamp.
  2975. When nil, S-up will increase."
  2976. :group 'org-time
  2977. :type 'boolean)
  2978. (defcustom org-calendar-follow-timestamp-change t
  2979. "Non-nil means make the calendar window follow timestamp changes.
  2980. When a timestamp is modified and the calendar window is visible, it will be
  2981. moved to the new date."
  2982. :group 'org-time
  2983. :type 'boolean)
  2984. (defgroup org-tags nil
  2985. "Options concerning tags in Org-mode."
  2986. :tag "Org Tags"
  2987. :group 'org)
  2988. (defcustom org-tag-alist nil
  2989. "List of tags allowed in Org-mode files.
  2990. When this list is nil, Org-mode will base TAG input on what is already in the
  2991. buffer.
  2992. The value of this variable is an alist, the car of each entry must be a
  2993. keyword as a string, the cdr may be a character that is used to select
  2994. that tag through the fast-tag-selection interface.
  2995. See the manual for details."
  2996. :group 'org-tags
  2997. :type '(repeat
  2998. (choice
  2999. (cons (string :tag "Tag name")
  3000. (character :tag "Access char"))
  3001. (list :tag "Start radio group"
  3002. (const :startgroup)
  3003. (option (string :tag "Group description")))
  3004. (list :tag "Start tag group, non distinct"
  3005. (const :startgrouptag)
  3006. (option (string :tag "Group description")))
  3007. (list :tag "Group tags delimiter"
  3008. (const :grouptags))
  3009. (list :tag "End radio group"
  3010. (const :endgroup)
  3011. (option (string :tag "Group description")))
  3012. (list :tag "End tag group, non distinct"
  3013. (const :endgrouptag)
  3014. (option (string :tag "Group description")))
  3015. (const :tag "New line" (:newline)))))
  3016. (defcustom org-tag-persistent-alist nil
  3017. "List of tags that will always appear in all Org-mode files.
  3018. This is in addition to any in buffer settings or customizations
  3019. of `org-tag-alist'.
  3020. When this list is nil, Org-mode will base TAG input on `org-tag-alist'.
  3021. The value of this variable is an alist, the car of each entry must be a
  3022. keyword as a string, the cdr may be a character that is used to select
  3023. that tag through the fast-tag-selection interface.
  3024. See the manual for details.
  3025. To disable these tags on a per-file basis, insert anywhere in the file:
  3026. #+STARTUP: noptag"
  3027. :group 'org-tags
  3028. :type '(repeat
  3029. (choice
  3030. (cons (string :tag "Tag name")
  3031. (character :tag "Access char"))
  3032. (const :tag "Start radio group" (:startgroup))
  3033. (const :tag "Group tags delimiter" (:grouptags))
  3034. (const :tag "End radio group" (:endgroup))
  3035. (const :tag "New line" (:newline)))))
  3036. (defcustom org-complete-tags-always-offer-all-agenda-tags nil
  3037. "If non-nil, always offer completion for all tags of all agenda files.
  3038. Instead of customizing this variable directly, you might want to
  3039. set it locally for capture buffers, because there no list of
  3040. tags in that file can be created dynamically (there are none).
  3041. (add-hook 'org-capture-mode-hook
  3042. (lambda ()
  3043. (set (make-local-variable
  3044. 'org-complete-tags-always-offer-all-agenda-tags)
  3045. t)))"
  3046. :group 'org-tags
  3047. :version "24.1"
  3048. :type 'boolean)
  3049. (defvar org-file-tags nil
  3050. "List of tags that can be inherited by all entries in the file.
  3051. The tags will be inherited if the variable `org-use-tag-inheritance'
  3052. says they should be.
  3053. This variable is populated from #+FILETAGS lines.")
  3054. (defcustom org-use-fast-tag-selection 'auto
  3055. "Non-nil means use fast tag selection scheme.
  3056. This is a special interface to select and deselect tags with single keys.
  3057. When nil, fast selection is never used.
  3058. When the symbol `auto', fast selection is used if and only if selection
  3059. characters for tags have been configured, either through the variable
  3060. `org-tag-alist' or through a #+TAGS line in the buffer.
  3061. When t, fast selection is always used and selection keys are assigned
  3062. automatically if necessary."
  3063. :group 'org-tags
  3064. :type '(choice
  3065. (const :tag "Always" t)
  3066. (const :tag "Never" nil)
  3067. (const :tag "When selection characters are configured" auto)))
  3068. (defcustom org-fast-tag-selection-single-key nil
  3069. "Non-nil means fast tag selection exits after first change.
  3070. When nil, you have to press RET to exit it.
  3071. During fast tag selection, you can toggle this flag with `C-c'.
  3072. This variable can also have the value `expert'. In this case, the window
  3073. displaying the tags menu is not even shown, until you press C-c again."
  3074. :group 'org-tags
  3075. :type '(choice
  3076. (const :tag "No" nil)
  3077. (const :tag "Yes" t)
  3078. (const :tag "Expert" expert)))
  3079. (defvar org-fast-tag-selection-include-todo nil
  3080. "Non-nil means fast tags selection interface will also offer TODO states.
  3081. This is an undocumented feature, you should not rely on it.")
  3082. (defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
  3083. "The column to which tags should be indented in a headline.
  3084. If this number is positive, it specifies the column. If it is negative,
  3085. it means that the tags should be flushright to that column. For example,
  3086. -80 works well for a normal 80 character screen.
  3087. When 0, place tags directly after headline text, with only one space in
  3088. between."
  3089. :group 'org-tags
  3090. :type 'integer)
  3091. (defcustom org-auto-align-tags t
  3092. "Non-nil keeps tags aligned when modifying headlines.
  3093. Some operations (i.e. demoting) change the length of a headline and
  3094. therefore shift the tags around. With this option turned on, after
  3095. each such operation the tags are again aligned to `org-tags-column'."
  3096. :group 'org-tags
  3097. :type 'boolean)
  3098. (defcustom org-use-tag-inheritance t
  3099. "Non-nil means tags in levels apply also for sublevels.
  3100. When nil, only the tags directly given in a specific line apply there.
  3101. This may also be a list of tags that should be inherited, or a regexp that
  3102. matches tags that should be inherited. Additional control is possible
  3103. with the variable `org-tags-exclude-from-inheritance' which gives an
  3104. explicit list of tags to be excluded from inheritance, even if the value of
  3105. `org-use-tag-inheritance' would select it for inheritance.
  3106. If this option is t, a match early-on in a tree can lead to a large
  3107. number of matches in the subtree when constructing the agenda or creating
  3108. a sparse tree. If you only want to see the first match in a tree during
  3109. a search, check out the variable `org-tags-match-list-sublevels'."
  3110. :group 'org-tags
  3111. :type '(choice
  3112. (const :tag "Not" nil)
  3113. (const :tag "Always" t)
  3114. (repeat :tag "Specific tags" (string :tag "Tag"))
  3115. (regexp :tag "Tags matched by regexp")))
  3116. (defcustom org-tags-exclude-from-inheritance nil
  3117. "List of tags that should never be inherited.
  3118. This is a way to exclude a few tags from inheritance. For way to do
  3119. the opposite, to actively allow inheritance for selected tags,
  3120. see the variable `org-use-tag-inheritance'."
  3121. :group 'org-tags
  3122. :type '(repeat (string :tag "Tag")))
  3123. (defun org-tag-inherit-p (tag)
  3124. "Check if TAG is one that should be inherited."
  3125. (cond
  3126. ((member tag org-tags-exclude-from-inheritance) nil)
  3127. ((eq org-use-tag-inheritance t) t)
  3128. ((not org-use-tag-inheritance) nil)
  3129. ((stringp org-use-tag-inheritance)
  3130. (string-match org-use-tag-inheritance tag))
  3131. ((listp org-use-tag-inheritance)
  3132. (member tag org-use-tag-inheritance))
  3133. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  3134. (defcustom org-tags-match-list-sublevels t
  3135. "Non-nil means list also sublevels of headlines matching a search.
  3136. This variable applies to tags/property searches, and also to stuck
  3137. projects because this search is based on a tags match as well.
  3138. When set to the symbol `indented', sublevels are indented with
  3139. leading dots.
  3140. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  3141. the sublevels of a headline matching a tag search often also match
  3142. the same search. Listing all of them can create very long lists.
  3143. Setting this variable to nil causes subtrees of a match to be skipped.
  3144. This variable is semi-obsolete and probably should always be true. It
  3145. is better to limit inheritance to certain tags using the variables
  3146. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  3147. :group 'org-tags
  3148. :type '(choice
  3149. (const :tag "No, don't list them" nil)
  3150. (const :tag "Yes, do list them" t)
  3151. (const :tag "List them, indented with leading dots" indented)))
  3152. (defcustom org-tags-sort-function nil
  3153. "When set, tags are sorted using this function as a comparator."
  3154. :group 'org-tags
  3155. :type '(choice
  3156. (const :tag "No sorting" nil)
  3157. (const :tag "Alphabetical" string<)
  3158. (const :tag "Reverse alphabetical" string>)
  3159. (function :tag "Custom function" nil)))
  3160. (defvar org-tags-history nil
  3161. "History of minibuffer reads for tags.")
  3162. (defvar org-last-tags-completion-table nil
  3163. "The last used completion table for tags.")
  3164. (defvar org-after-tags-change-hook nil
  3165. "Hook that is run after the tags in a line have changed.")
  3166. (defgroup org-properties nil
  3167. "Options concerning properties in Org-mode."
  3168. :tag "Org Properties"
  3169. :group 'org)
  3170. (defcustom org-property-format "%-10s %s"
  3171. "How property key/value pairs should be formatted by `indent-line'.
  3172. When `indent-line' hits a property definition, it will format the line
  3173. according to this format, mainly to make sure that the values are
  3174. lined-up with respect to each other."
  3175. :group 'org-properties
  3176. :type 'string)
  3177. (defcustom org-properties-postprocess-alist nil
  3178. "Alist of properties and functions to adjust inserted values.
  3179. Elements of this alist must be of the form
  3180. ([string] [function])
  3181. where [string] must be a property name and [function] must be a
  3182. lambda expression: this lambda expression must take one argument,
  3183. the value to adjust, and return the new value as a string.
  3184. For example, this element will allow the property \"Remaining\"
  3185. to be updated wrt the relation between the \"Effort\" property
  3186. and the clock summary:
  3187. ((\"Remaining\" (lambda(value)
  3188. (let ((clocksum (org-clock-sum-current-item))
  3189. (effort (org-duration-string-to-minutes
  3190. (org-entry-get (point) \"Effort\"))))
  3191. (org-minutes-to-clocksum-string (- effort clocksum))))))"
  3192. :group 'org-properties
  3193. :version "24.1"
  3194. :type '(alist :key-type (string :tag "Property")
  3195. :value-type (function :tag "Function")))
  3196. (defcustom org-use-property-inheritance nil
  3197. "Non-nil means properties apply also for sublevels.
  3198. This setting is chiefly used during property searches. Turning it on can
  3199. cause significant overhead when doing a search, which is why it is not
  3200. on by default.
  3201. When nil, only the properties directly given in the current entry count.
  3202. When t, every property is inherited. The value may also be a list of
  3203. properties that should have inheritance, or a regular expression matching
  3204. properties that should be inherited.
  3205. However, note that some special properties use inheritance under special
  3206. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  3207. and the properties ending in \"_ALL\" when they are used as descriptor
  3208. for valid values of a property.
  3209. Note for programmers:
  3210. When querying an entry with `org-entry-get', you can control if inheritance
  3211. should be used. By default, `org-entry-get' looks only at the local
  3212. properties. You can request inheritance by setting the inherit argument
  3213. to t (to force inheritance) or to `selective' (to respect the setting
  3214. in this variable)."
  3215. :group 'org-properties
  3216. :type '(choice
  3217. (const :tag "Not" nil)
  3218. (const :tag "Always" t)
  3219. (repeat :tag "Specific properties" (string :tag "Property"))
  3220. (regexp :tag "Properties matched by regexp")))
  3221. (defun org-property-inherit-p (property)
  3222. "Check if PROPERTY is one that should be inherited."
  3223. (cond
  3224. ((eq org-use-property-inheritance t) t)
  3225. ((not org-use-property-inheritance) nil)
  3226. ((stringp org-use-property-inheritance)
  3227. (string-match org-use-property-inheritance property))
  3228. ((listp org-use-property-inheritance)
  3229. (member property org-use-property-inheritance))
  3230. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  3231. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  3232. "The default column format, if no other format has been defined.
  3233. This variable can be set on the per-file basis by inserting a line
  3234. #+COLUMNS: %25ITEM ....."
  3235. :group 'org-properties
  3236. :type 'string)
  3237. (defcustom org-columns-ellipses ".."
  3238. "The ellipses to be used when a field in column view is truncated.
  3239. When this is the empty string, as many characters as possible are shown,
  3240. but then there will be no visual indication that the field has been truncated.
  3241. When this is a string of length N, the last N characters of a truncated
  3242. field are replaced by this string. If the column is narrower than the
  3243. ellipses string, only part of the ellipses string will be shown."
  3244. :group 'org-properties
  3245. :type 'string)
  3246. (defcustom org-columns-modify-value-for-display-function nil
  3247. "Function that modifies values for display in column view.
  3248. For example, it can be used to cut out a certain part from a time stamp.
  3249. The function must take 2 arguments:
  3250. column-title The title of the column (*not* the property name)
  3251. value The value that should be modified.
  3252. The function should return the value that should be displayed,
  3253. or nil if the normal value should be used."
  3254. :group 'org-properties
  3255. :type '(choice (const nil) (function)))
  3256. (defconst org-global-properties-fixed
  3257. '(("VISIBILITY_ALL" . "folded children content all")
  3258. ("CLOCK_MODELINE_TOTAL_ALL" . "current today repeat all auto"))
  3259. "List of property/value pairs that can be inherited by any entry.
  3260. These are fixed values, for the preset properties. The user variable
  3261. that can be used to add to this list is `org-global-properties'.
  3262. The entries in this list are cons cells where the car is a property
  3263. name and cdr is a string with the value. If the value represents
  3264. multiple items like an \"_ALL\" property, separate the items by
  3265. spaces.")
  3266. (defcustom org-global-properties nil
  3267. "List of property/value pairs that can be inherited by any entry.
  3268. This list will be combined with the constant `org-global-properties-fixed'.
  3269. The entries in this list are cons cells where the car is a property
  3270. name and cdr is a string with the value.
  3271. You can set buffer-local values for the same purpose in the variable
  3272. `org-file-properties' this by adding lines like
  3273. #+PROPERTY: NAME VALUE"
  3274. :group 'org-properties
  3275. :type '(repeat
  3276. (cons (string :tag "Property")
  3277. (string :tag "Value"))))
  3278. (defvar org-file-properties nil
  3279. "List of property/value pairs that can be inherited by any entry.
  3280. Valid for the current buffer.
  3281. This variable is populated from #+PROPERTY lines.")
  3282. (make-variable-buffer-local 'org-file-properties)
  3283. (defgroup org-agenda nil
  3284. "Options concerning agenda views in Org-mode."
  3285. :tag "Org Agenda"
  3286. :group 'org)
  3287. (defvar org-category nil
  3288. "Variable used by org files to set a category for agenda display.
  3289. Such files should use a file variable to set it, for example
  3290. # -*- mode: org; org-category: \"ELisp\"
  3291. or contain a special line
  3292. #+CATEGORY: ELisp
  3293. If the file does not specify a category, then file's base name
  3294. is used instead.")
  3295. (make-variable-buffer-local 'org-category)
  3296. (put 'org-category 'safe-local-variable (lambda (x) (or (symbolp x) (stringp x))))
  3297. (defcustom org-agenda-files nil
  3298. "The files to be used for agenda display.
  3299. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  3300. \\[org-remove-file]. You can also use customize to edit the list.
  3301. If an entry is a directory, all files in that directory that are matched by
  3302. `org-agenda-file-regexp' will be part of the file list.
  3303. If the value of the variable is not a list but a single file name, then
  3304. the list of agenda files is actually stored and maintained in that file, one
  3305. agenda file per line. In this file paths can be given relative to
  3306. `org-directory'. Tilde expansion and environment variable substitution
  3307. are also made."
  3308. :group 'org-agenda
  3309. :type '(choice
  3310. (repeat :tag "List of files and directories" file)
  3311. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  3312. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  3313. "Regular expression to match files for `org-agenda-files'.
  3314. If any element in the list in that variable contains a directory instead
  3315. of a normal file, all files in that directory that are matched by this
  3316. regular expression will be included."
  3317. :group 'org-agenda
  3318. :type 'regexp)
  3319. (defcustom org-agenda-text-search-extra-files nil
  3320. "List of extra files to be searched by text search commands.
  3321. These files will be searched in addition to the agenda files by the
  3322. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  3323. Note that these files will only be searched for text search commands,
  3324. not for the other agenda views like todo lists, tag searches or the weekly
  3325. agenda. This variable is intended to list notes and possibly archive files
  3326. that should also be searched by these two commands.
  3327. In fact, if the first element in the list is the symbol `agenda-archives',
  3328. then all archive files of all agenda files will be added to the search
  3329. scope."
  3330. :group 'org-agenda
  3331. :type '(set :greedy t
  3332. (const :tag "Agenda Archives" agenda-archives)
  3333. (repeat :inline t (file))))
  3334. (org-defvaralias 'org-agenda-multi-occur-extra-files
  3335. 'org-agenda-text-search-extra-files)
  3336. (defcustom org-agenda-skip-unavailable-files nil
  3337. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  3338. A nil value means to remove them, after a query, from the list."
  3339. :group 'org-agenda
  3340. :type 'boolean)
  3341. (defcustom org-calendar-to-agenda-key [?c]
  3342. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  3343. The command `org-calendar-goto-agenda' will be bound to this key. The
  3344. default is the character `c' because then `c' can be used to switch back and
  3345. forth between agenda and calendar."
  3346. :group 'org-agenda
  3347. :type 'sexp)
  3348. (defcustom org-calendar-insert-diary-entry-key [?i]
  3349. "The key to be installed in `calendar-mode-map' for adding diary entries.
  3350. This option is irrelevant until `org-agenda-diary-file' has been configured
  3351. to point to an Org-mode file. When that is the case, the command
  3352. `org-agenda-diary-entry' will be bound to the key given here, by default
  3353. `i'. In the calendar, `i' normally adds entries to `diary-file'. So
  3354. if you want to continue doing this, you need to change this to a different
  3355. key."
  3356. :group 'org-agenda
  3357. :type 'sexp)
  3358. (defcustom org-agenda-diary-file 'diary-file
  3359. "File to which to add new entries with the `i' key in agenda and calendar.
  3360. When this is the symbol `diary-file', the functionality in the Emacs
  3361. calendar will be used to add entries to the `diary-file'. But when this
  3362. points to a file, `org-agenda-diary-entry' will be used instead."
  3363. :group 'org-agenda
  3364. :type '(choice
  3365. (const :tag "The standard Emacs diary file" diary-file)
  3366. (file :tag "Special Org file diary entries")))
  3367. (eval-after-load "calendar"
  3368. '(progn
  3369. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  3370. 'org-calendar-goto-agenda)
  3371. (add-hook 'calendar-mode-hook
  3372. (lambda ()
  3373. (unless (eq org-agenda-diary-file 'diary-file)
  3374. (define-key calendar-mode-map
  3375. org-calendar-insert-diary-entry-key
  3376. 'org-agenda-diary-entry))))))
  3377. (defgroup org-latex nil
  3378. "Options for embedding LaTeX code into Org-mode."
  3379. :tag "Org LaTeX"
  3380. :group 'org)
  3381. (defcustom org-format-latex-options
  3382. '(:foreground default :background default :scale 1.0
  3383. :html-foreground "Black" :html-background "Transparent"
  3384. :html-scale 1.0 :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
  3385. "Options for creating images from LaTeX fragments.
  3386. This is a property list with the following properties:
  3387. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  3388. `default' means use the foreground of the default face.
  3389. `auto' means use the foreground from the text face.
  3390. :background the background color, or \"Transparent\".
  3391. `default' means use the background of the default face.
  3392. `auto' means use the background from the text face.
  3393. :scale a scaling factor for the size of the images, to get more pixels
  3394. :html-foreground, :html-background, :html-scale
  3395. the same numbers for HTML export.
  3396. :matchers a list indicating which matchers should be used to
  3397. find LaTeX fragments. Valid members of this list are:
  3398. \"begin\" find environments
  3399. \"$1\" find single characters surrounded by $.$
  3400. \"$\" find math expressions surrounded by $...$
  3401. \"$$\" find math expressions surrounded by $$....$$
  3402. \"\\(\" find math expressions surrounded by \\(...\\)
  3403. \"\\=\\[\" find math expressions surrounded by \\=\\[...\\]"
  3404. :group 'org-latex
  3405. :type 'plist)
  3406. (defcustom org-format-latex-signal-error t
  3407. "Non-nil means signal an error when image creation of LaTeX snippets fails.
  3408. When nil, just push out a message."
  3409. :group 'org-latex
  3410. :version "24.1"
  3411. :type 'boolean)
  3412. (defcustom org-latex-to-mathml-jar-file nil
  3413. "Value of\"%j\" in `org-latex-to-mathml-convert-command'.
  3414. Use this to specify additional executable file say a jar file.
  3415. When using MathToWeb as the converter, specify the full-path to
  3416. your mathtoweb.jar file."
  3417. :group 'org-latex
  3418. :version "24.1"
  3419. :type '(choice
  3420. (const :tag "None" nil)
  3421. (file :tag "JAR file" :must-match t)))
  3422. (defcustom org-latex-to-mathml-convert-command nil
  3423. "Command to convert LaTeX fragments to MathML.
  3424. Replace format-specifiers in the command as noted below and use
  3425. `shell-command' to convert LaTeX to MathML.
  3426. %j: Executable file in fully expanded form as specified by
  3427. `org-latex-to-mathml-jar-file'.
  3428. %I: Input LaTeX file in fully expanded form.
  3429. %i: The latex fragment to be converted.
  3430. %o: Output MathML file.
  3431. This command is used by `org-create-math-formula'.
  3432. When using MathToWeb as the converter, set this option to
  3433. \"java -jar %j -unicode -force -df %o %I\".
  3434. When using LaTeXML set this option to
  3435. \"latexmlmath \"%i\" --presentationmathml=%o\"."
  3436. :group 'org-latex
  3437. :version "24.1"
  3438. :type '(choice
  3439. (const :tag "None" nil)
  3440. (string :tag "\nShell command")))
  3441. (defcustom org-latex-create-formula-image-program 'dvipng
  3442. "Program to convert LaTeX fragments with.
  3443. dvipng Process the LaTeX fragments to dvi file, then convert
  3444. dvi files to png files using dvipng.
  3445. This will also include processing of non-math environments.
  3446. imagemagick Convert the LaTeX fragments to pdf files and use imagemagick
  3447. to convert pdf files to png files"
  3448. :group 'org-latex
  3449. :version "24.1"
  3450. :type '(choice
  3451. (const :tag "dvipng" dvipng)
  3452. (const :tag "imagemagick" imagemagick)))
  3453. (defcustom org-latex-preview-ltxpng-directory "ltxpng/"
  3454. "Path to store latex preview images.
  3455. A relative path here creates many directories relative to the
  3456. processed org files paths. An absolute path puts all preview
  3457. images at the same place."
  3458. :group 'org-latex
  3459. :version "24.3"
  3460. :type 'string)
  3461. (defun org-format-latex-mathml-available-p ()
  3462. "Return t if `org-latex-to-mathml-convert-command' is usable."
  3463. (save-match-data
  3464. (when (and (boundp 'org-latex-to-mathml-convert-command)
  3465. org-latex-to-mathml-convert-command)
  3466. (let ((executable (car (split-string
  3467. org-latex-to-mathml-convert-command))))
  3468. (when (executable-find executable)
  3469. (if (string-match
  3470. "%j" org-latex-to-mathml-convert-command)
  3471. (file-readable-p org-latex-to-mathml-jar-file)
  3472. t))))))
  3473. (defcustom org-format-latex-header "\\documentclass{article}
  3474. \\usepackage[usenames]{color}
  3475. \[PACKAGES]
  3476. \[DEFAULT-PACKAGES]
  3477. \\pagestyle{empty} % do not remove
  3478. % The settings below are copied from fullpage.sty
  3479. \\setlength{\\textwidth}{\\paperwidth}
  3480. \\addtolength{\\textwidth}{-3cm}
  3481. \\setlength{\\oddsidemargin}{1.5cm}
  3482. \\addtolength{\\oddsidemargin}{-2.54cm}
  3483. \\setlength{\\evensidemargin}{\\oddsidemargin}
  3484. \\setlength{\\textheight}{\\paperheight}
  3485. \\addtolength{\\textheight}{-\\headheight}
  3486. \\addtolength{\\textheight}{-\\headsep}
  3487. \\addtolength{\\textheight}{-\\footskip}
  3488. \\addtolength{\\textheight}{-3cm}
  3489. \\setlength{\\topmargin}{1.5cm}
  3490. \\addtolength{\\topmargin}{-2.54cm}"
  3491. "The document header used for processing LaTeX fragments.
  3492. It is imperative that this header make sure that no page number
  3493. appears on the page. The package defined in the variables
  3494. `org-latex-default-packages-alist' and `org-latex-packages-alist'
  3495. will either replace the placeholder \"[PACKAGES]\" in this
  3496. header, or they will be appended."
  3497. :group 'org-latex
  3498. :type 'string)
  3499. (defun org-set-packages-alist (var val)
  3500. "Set the packages alist and make sure it has 3 elements per entry."
  3501. (set var (mapcar (lambda (x)
  3502. (if (and (consp x) (= (length x) 2))
  3503. (list (car x) (nth 1 x) t)
  3504. x))
  3505. val)))
  3506. (defun org-get-packages-alist (var)
  3507. "Get the packages alist and make sure it has 3 elements per entry."
  3508. (mapcar (lambda (x)
  3509. (if (and (consp x) (= (length x) 2))
  3510. (list (car x) (nth 1 x) t)
  3511. x))
  3512. (default-value var)))
  3513. (defcustom org-latex-default-packages-alist
  3514. '(("AUTO" "inputenc" t)
  3515. ("T1" "fontenc" t)
  3516. ("" "fixltx2e" nil)
  3517. ("" "graphicx" t)
  3518. ("" "grffile" t)
  3519. ("" "longtable" nil)
  3520. ("" "wrapfig" nil)
  3521. ("" "rotating" nil)
  3522. ("normalem" "ulem" t)
  3523. ("" "amsmath" t)
  3524. ("" "textcomp" t)
  3525. ("" "amssymb" t)
  3526. ("" "capt-of" nil)
  3527. ("" "hyperref" nil))
  3528. "Alist of default packages to be inserted in the header.
  3529. Change this only if one of the packages here causes an
  3530. incompatibility with another package you are using.
  3531. The packages in this list are needed by one part or another of
  3532. Org mode to function properly:
  3533. - inputenc, fontenc: for basic font and character selection
  3534. - fixltx2e: Important patches of LaTeX itself
  3535. - graphicx: for including images
  3536. - grffile: allow periods and spaces in graphics file names
  3537. - longtable: For multipage tables
  3538. - wrapfig: for figure placement
  3539. - rotating: for sideways figures and tables
  3540. - ulem: for underline and strike-through
  3541. - amsmath: for subscript and superscript and math environments
  3542. - textcomp, amssymb: for various symbols used
  3543. for interpreting the entities in `org-entities'. You can skip
  3544. some of these packages if you don't use any of their symbols.
  3545. - capt-of: for captions outside of floats
  3546. - hyperref: for cross references
  3547. Therefore you should not modify this variable unless you know
  3548. what you are doing. The one reason to change it anyway is that
  3549. you might be loading some other package that conflicts with one
  3550. of the default packages. Each element is either a cell or
  3551. a string.
  3552. A cell is of the format
  3553. \(\"options\" \"package\" SNIPPET-FLAG)
  3554. If SNIPPET-FLAG is non-nil, the package also needs to be included
  3555. when compiling LaTeX snippets into images for inclusion into
  3556. non-LaTeX output.
  3557. A string will be inserted as-is in the header of the document."
  3558. :group 'org-latex
  3559. :group 'org-export-latex
  3560. :set 'org-set-packages-alist
  3561. :get 'org-get-packages-alist
  3562. :version "25.1"
  3563. :package-version '(Org . "8.3")
  3564. :type '(repeat
  3565. (choice
  3566. (list :tag "options/package pair"
  3567. (string :tag "options")
  3568. (string :tag "package")
  3569. (boolean :tag "Snippet"))
  3570. (string :tag "A line of LaTeX"))))
  3571. (defcustom org-latex-packages-alist nil
  3572. "Alist of packages to be inserted in every LaTeX header.
  3573. These will be inserted after `org-latex-default-packages-alist'.
  3574. Each element is either a cell or a string.
  3575. A cell is of the format:
  3576. \(\"options\" \"package\" SNIPPET-FLAG)
  3577. SNIPPET-FLAG, when non-nil, indicates that this package is also
  3578. needed when turning LaTeX snippets into images for inclusion into
  3579. non-LaTeX output.
  3580. A string will be inserted as-is in the header of the document.
  3581. Make sure that you only list packages here which:
  3582. - you want in every file;
  3583. - do not conflict with the setup in `org-format-latex-header';
  3584. - do not conflict with the default packages in
  3585. `org-latex-default-packages-alist'."
  3586. :group 'org-latex
  3587. :group 'org-export-latex
  3588. :set 'org-set-packages-alist
  3589. :get 'org-get-packages-alist
  3590. :type '(repeat
  3591. (choice
  3592. (list :tag "options/package pair"
  3593. (string :tag "options")
  3594. (string :tag "package")
  3595. (boolean :tag "Snippet"))
  3596. (string :tag "A line of LaTeX"))))
  3597. (defgroup org-appearance nil
  3598. "Settings for Org-mode appearance."
  3599. :tag "Org Appearance"
  3600. :group 'org)
  3601. (defcustom org-level-color-stars-only nil
  3602. "Non-nil means fontify only the stars in each headline.
  3603. When nil, the entire headline is fontified.
  3604. Changing it requires restart of `font-lock-mode' to become effective
  3605. also in regions already fontified."
  3606. :group 'org-appearance
  3607. :type 'boolean)
  3608. (defcustom org-hide-leading-stars nil
  3609. "Non-nil means hide the first N-1 stars in a headline.
  3610. This works by using the face `org-hide' for these stars. This
  3611. face is white for a light background, and black for a dark
  3612. background. You may have to customize the face `org-hide' to
  3613. make this work.
  3614. Changing it requires restart of `font-lock-mode' to become effective
  3615. also in regions already fontified.
  3616. You may also set this on a per-file basis by adding one of the following
  3617. lines to the buffer:
  3618. #+STARTUP: hidestars
  3619. #+STARTUP: showstars"
  3620. :group 'org-appearance
  3621. :type 'boolean)
  3622. (defcustom org-hidden-keywords nil
  3623. "List of symbols corresponding to keywords to be hidden the org buffer.
  3624. For example, a value '(title) for this list will make the document's title
  3625. appear in the buffer without the initial #+TITLE: keyword."
  3626. :group 'org-appearance
  3627. :version "24.1"
  3628. :type '(set (const :tag "#+AUTHOR" author)
  3629. (const :tag "#+DATE" date)
  3630. (const :tag "#+EMAIL" email)
  3631. (const :tag "#+TITLE" title)))
  3632. (defcustom org-custom-properties nil
  3633. "List of properties (as strings) with a special meaning.
  3634. The default use of these custom properties is to let the user
  3635. hide them with `org-toggle-custom-properties-visibility'."
  3636. :group 'org-properties
  3637. :group 'org-appearance
  3638. :version "24.3"
  3639. :type '(repeat (string :tag "Property Name")))
  3640. (defcustom org-fontify-done-headline nil
  3641. "Non-nil means change the face of a headline if it is marked DONE.
  3642. Normally, only the TODO/DONE keyword indicates the state of a headline.
  3643. When this is non-nil, the headline after the keyword is set to the
  3644. `org-headline-done' as an additional indication."
  3645. :group 'org-appearance
  3646. :type 'boolean)
  3647. (defcustom org-fontify-emphasized-text t
  3648. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  3649. Changing this variable requires a restart of Emacs to take effect."
  3650. :group 'org-appearance
  3651. :type 'boolean)
  3652. (defcustom org-fontify-whole-heading-line nil
  3653. "Non-nil means fontify the whole line for headings.
  3654. This is useful when setting a background color for the
  3655. org-level-* faces."
  3656. :group 'org-appearance
  3657. :type 'boolean)
  3658. (defcustom org-highlight-latex-and-related nil
  3659. "Non-nil means highlight LaTeX related syntax in the buffer.
  3660. When non nil, the value should be a list containing any of the
  3661. following symbols:
  3662. `latex' Highlight LaTeX snippets and environments.
  3663. `script' Highlight subscript and superscript.
  3664. `entities' Highlight entities."
  3665. :group 'org-appearance
  3666. :version "24.4"
  3667. :package-version '(Org . "8.0")
  3668. :type '(choice
  3669. (const :tag "No highlighting" nil)
  3670. (set :greedy t :tag "Highlight"
  3671. (const :tag "LaTeX snippets and environments" latex)
  3672. (const :tag "Subscript and superscript" script)
  3673. (const :tag "Entities" entities))))
  3674. (defcustom org-hide-emphasis-markers nil
  3675. "Non-nil mean font-lock should hide the emphasis marker characters."
  3676. :group 'org-appearance
  3677. :type 'boolean)
  3678. (defcustom org-hide-macro-markers nil
  3679. "Non-nil mean font-lock should hide the brackets marking macro calls."
  3680. :group 'org-appearance
  3681. :type 'boolean)
  3682. (defcustom org-pretty-entities nil
  3683. "Non-nil means show entities as UTF8 characters.
  3684. When nil, the \\name form remains in the buffer."
  3685. :group 'org-appearance
  3686. :version "24.1"
  3687. :type 'boolean)
  3688. (defcustom org-pretty-entities-include-sub-superscripts t
  3689. "Non-nil means, pretty entity display includes formatting sub/superscripts."
  3690. :group 'org-appearance
  3691. :version "24.1"
  3692. :type 'boolean)
  3693. (defvar org-emph-re nil
  3694. "Regular expression for matching emphasis.
  3695. After a match, the match groups contain these elements:
  3696. 0 The match of the full regular expression, including the characters
  3697. before and after the proper match
  3698. 1 The character before the proper match, or empty at beginning of line
  3699. 2 The proper match, including the leading and trailing markers
  3700. 3 The leading marker like * or /, indicating the type of highlighting
  3701. 4 The text between the emphasis markers, not including the markers
  3702. 5 The character after the match, empty at the end of a line")
  3703. (defvar org-verbatim-re nil
  3704. "Regular expression for matching verbatim text.")
  3705. (defvar org-emphasis-regexp-components) ; defined just below
  3706. (defvar org-emphasis-alist) ; defined just below
  3707. (defun org-set-emph-re (var val)
  3708. "Set variable and compute the emphasis regular expression."
  3709. (set var val)
  3710. (when (and (boundp 'org-emphasis-alist)
  3711. (boundp 'org-emphasis-regexp-components)
  3712. org-emphasis-alist org-emphasis-regexp-components)
  3713. (let* ((e org-emphasis-regexp-components)
  3714. (pre (car e))
  3715. (post (nth 1 e))
  3716. (border (nth 2 e))
  3717. (body (nth 3 e))
  3718. (nl (nth 4 e))
  3719. (body1 (concat body "*?"))
  3720. (markers (mapconcat 'car org-emphasis-alist ""))
  3721. (vmarkers (mapconcat
  3722. (lambda (x) (if (eq (nth 2 x) 'verbatim) (car x) ""))
  3723. org-emphasis-alist "")))
  3724. ;; make sure special characters appear at the right position in the class
  3725. (if (string-match "\\^" markers)
  3726. (setq markers (concat (replace-match "" t t markers) "^")))
  3727. (if (string-match "-" markers)
  3728. (setq markers (concat (replace-match "" t t markers) "-")))
  3729. (if (string-match "\\^" vmarkers)
  3730. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  3731. (if (string-match "-" vmarkers)
  3732. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  3733. (if (> nl 0)
  3734. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  3735. (int-to-string nl) "\\}")))
  3736. ;; Make the regexp
  3737. (setq org-emph-re
  3738. (concat "\\([" pre "]\\|^\\)"
  3739. "\\("
  3740. "\\([" markers "]\\)"
  3741. "\\("
  3742. "[^" border "]\\|"
  3743. "[^" border "]"
  3744. body1
  3745. "[^" border "]"
  3746. "\\)"
  3747. "\\3\\)"
  3748. "\\([" post "]\\|$\\)"))
  3749. (setq org-verbatim-re
  3750. (concat "\\([" pre "]\\|^\\)"
  3751. "\\("
  3752. "\\([" vmarkers "]\\)"
  3753. "\\("
  3754. "[^" border "]\\|"
  3755. "[^" border "]"
  3756. body1
  3757. "[^" border "]"
  3758. "\\)"
  3759. "\\3\\)"
  3760. "\\([" post "]\\|$\\)")))))
  3761. ;; This used to be a defcustom (Org <8.0) but allowing the users to
  3762. ;; set this option proved cumbersome. See this message/thread:
  3763. ;; http://article.gmane.org/gmane.emacs.orgmode/68681
  3764. (defvar org-emphasis-regexp-components
  3765. '(" \t('\"{" "- \t.,:!?;'\")}\\[" " \t\r\n,\"'" "." 1)
  3766. "Components used to build the regular expression for emphasis.
  3767. This is a list with five entries. Terminology: In an emphasis string
  3768. like \" *strong word* \", we call the initial space PREMATCH, the final
  3769. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  3770. and \"trong wor\" is the body. The different components in this variable
  3771. specify what is allowed/forbidden in each part:
  3772. pre Chars allowed as prematch. Beginning of line will be allowed too.
  3773. post Chars allowed as postmatch. End of line will be allowed too.
  3774. border The chars *forbidden* as border characters.
  3775. body-regexp A regexp like \".\" to match a body character. Don't use
  3776. non-shy groups here, and don't allow newline here.
  3777. newline The maximum number of newlines allowed in an emphasis exp.
  3778. You need to reload Org or to restart Emacs after customizing this.")
  3779. (defcustom org-emphasis-alist
  3780. `(("*" bold)
  3781. ("/" italic)
  3782. ("_" underline)
  3783. ("=" org-verbatim verbatim)
  3784. ("~" org-code verbatim)
  3785. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))))
  3786. "Alist of characters and faces to emphasize text.
  3787. Text starting and ending with a special character will be emphasized,
  3788. for example *bold*, _underlined_ and /italic/. This variable sets the
  3789. marker characters and the face to be used by font-lock for highlighting
  3790. in Org-mode Emacs buffers.
  3791. You need to reload Org or to restart Emacs after customizing this."
  3792. :group 'org-appearance
  3793. :set 'org-set-emph-re
  3794. :version "24.4"
  3795. :package-version '(Org . "8.0")
  3796. :type '(repeat
  3797. (list
  3798. (string :tag "Marker character")
  3799. (choice
  3800. (face :tag "Font-lock-face")
  3801. (plist :tag "Face property list"))
  3802. (option (const verbatim)))))
  3803. (defvar org-protecting-blocks
  3804. '("src" "example" "latex" "ascii" "html" "ditaa" "dot" "r" "R")
  3805. "Blocks that contain text that is quoted, i.e. not processed as Org syntax.
  3806. This is needed for font-lock setup.")
  3807. ;;; Miscellaneous options
  3808. (defgroup org-completion nil
  3809. "Completion in Org-mode."
  3810. :tag "Org Completion"
  3811. :group 'org)
  3812. (defcustom org-completion-use-ido nil
  3813. "Non-nil means use ido completion wherever possible.
  3814. Note that `ido-mode' must be active for this variable to be relevant.
  3815. If you decide to turn this variable on, you might well want to turn off
  3816. `org-outline-path-complete-in-steps'.
  3817. See also `org-completion-use-iswitchb'."
  3818. :group 'org-completion
  3819. :type 'boolean)
  3820. (defcustom org-completion-use-iswitchb nil
  3821. "Non-nil means use iswitchb completion wherever possible.
  3822. Note that `iswitchb-mode' must be active for this variable to be relevant.
  3823. If you decide to turn this variable on, you might well want to turn off
  3824. `org-outline-path-complete-in-steps'.
  3825. Note that this variable has only an effect if `org-completion-use-ido' is nil."
  3826. :group 'org-completion
  3827. :type 'boolean)
  3828. (defcustom org-completion-fallback-command 'hippie-expand
  3829. "The expansion command called by \\[pcomplete] in normal context.
  3830. Normal means, no org-mode-specific context."
  3831. :group 'org-completion
  3832. :type 'function)
  3833. ;;; Functions and variables from their packages
  3834. ;; Declared here to avoid compiler warnings
  3835. ;; XEmacs only
  3836. (defvar outline-mode-menu-heading)
  3837. (defvar outline-mode-menu-show)
  3838. (defvar outline-mode-menu-hide)
  3839. (defvar zmacs-regions) ; XEmacs regions
  3840. ;; Emacs only
  3841. (defvar mark-active)
  3842. ;; Various packages
  3843. (declare-function calendar-iso-to-absolute "cal-iso" (date))
  3844. (declare-function calendar-forward-day "cal-move" (arg))
  3845. (declare-function calendar-goto-date "cal-move" (date))
  3846. (declare-function calendar-goto-today "cal-move" ())
  3847. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  3848. (defvar calc-embedded-close-formula)
  3849. (defvar calc-embedded-open-formula)
  3850. (declare-function cdlatex-tab "ext:cdlatex" ())
  3851. (declare-function cdlatex-compute-tables "ext:cdlatex" ())
  3852. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  3853. (defvar font-lock-unfontify-region-function)
  3854. (declare-function iswitchb-read-buffer "iswitchb"
  3855. (prompt &optional default require-match start matches-set))
  3856. (defvar iswitchb-temp-buflist)
  3857. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  3858. (defvar org-agenda-tags-todo-honor-ignore-options)
  3859. (declare-function org-agenda-skip "org-agenda" ())
  3860. (declare-function
  3861. org-agenda-format-item "org-agenda"
  3862. (extra txt &optional level category tags dotime noprefix remove-re habitp))
  3863. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  3864. (declare-function org-agenda-change-all-lines "org-agenda"
  3865. (newhead hdmarker &optional fixface just-this))
  3866. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  3867. (declare-function org-agenda-maybe-redo "org-agenda" ())
  3868. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  3869. (beg end))
  3870. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  3871. (declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
  3872. "org-agenda" (&optional end))
  3873. (declare-function org-inlinetask-remove-END-maybe "org-inlinetask" ())
  3874. (declare-function org-inlinetask-in-task-p "org-inlinetask" ())
  3875. (declare-function org-inlinetask-goto-beginning "org-inlinetask" ())
  3876. (declare-function org-inlinetask-goto-end "org-inlinetask" ())
  3877. (declare-function org-indent-mode "org-indent" (&optional arg))
  3878. (declare-function parse-time-string "parse-time" (string))
  3879. (declare-function org-attach-reveal "org-attach" (&optional if-exists))
  3880. (declare-function orgtbl-send-table "org-table" (&optional maybe))
  3881. (defvar remember-data-file)
  3882. (defvar texmathp-why)
  3883. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  3884. (declare-function table--at-cell-p "table" (position &optional object at-column))
  3885. (declare-function calc-eval "calc" (str &optional separator &rest args))
  3886. ;;;###autoload
  3887. (defun turn-on-orgtbl ()
  3888. "Unconditionally turn on `orgtbl-mode'."
  3889. (require 'org-table)
  3890. (orgtbl-mode 1))
  3891. (defun org-at-table-p (&optional table-type)
  3892. "Non-nil if the cursor is inside an Org table.
  3893. If TABLE-TYPE is non-nil, also check for table.el-type tables.
  3894. If `org-enable-table-editor' is nil, return nil unconditionally."
  3895. (and
  3896. org-enable-table-editor
  3897. (save-excursion
  3898. (beginning-of-line)
  3899. (org-looking-at-p (if table-type "[ \t]*[|+]" "[ \t]*|")))
  3900. (or (not (derived-mode-p 'org-mode))
  3901. (let ((e (org-element-lineage (org-element-at-point) '(table) t)))
  3902. (and e (or table-type (eq (org-element-property :type e) 'org)))))))
  3903. (defsubst org-table-p () (org-at-table-p))
  3904. (defun org-at-table.el-p ()
  3905. "Non-nil when point is at a table.el table."
  3906. (and (save-excursion (beginning-of-line) (looking-at "[ \t]*[|+]"))
  3907. (let ((element (org-element-at-point)))
  3908. (and (eq (org-element-type element) 'table)
  3909. (eq (org-element-property :type element) 'table.el)))))
  3910. (defun org-table-recognize-table.el ()
  3911. "If there is a table.el table nearby, recognize it and move into it."
  3912. (when (and org-table-tab-recognizes-table.el (org-at-table.el-p))
  3913. (beginning-of-line)
  3914. (unless (or (looking-at org-table-dataline-regexp)
  3915. (not (looking-at org-table1-hline-regexp)))
  3916. (forward-line)
  3917. (when (looking-at org-table-any-border-regexp)
  3918. (forward-line -2)))
  3919. (if (re-search-forward "|" (org-table-end t) t)
  3920. (progn
  3921. (require 'table)
  3922. (if (table--at-cell-p (point)) t
  3923. (message "recognizing table.el table...")
  3924. (table-recognize-table)
  3925. (message "recognizing table.el table...done")))
  3926. (error "This should not happen"))))
  3927. (defun org-at-table-hline-p ()
  3928. "Non-nil when point is inside a hline in a table.
  3929. Assume point is already in a table. If `org-enable-table-editor'
  3930. is nil, return nil unconditionally."
  3931. (and org-enable-table-editor
  3932. (save-excursion
  3933. (beginning-of-line)
  3934. (looking-at org-table-hline-regexp))))
  3935. (defun org-table-map-tables (function &optional quietly)
  3936. "Apply FUNCTION to the start of all tables in the buffer."
  3937. (save-excursion
  3938. (save-restriction
  3939. (widen)
  3940. (goto-char (point-min))
  3941. (while (re-search-forward org-table-any-line-regexp nil t)
  3942. (unless quietly
  3943. (message "Mapping tables: %d%%"
  3944. (floor (* 100.0 (point)) (buffer-size))))
  3945. (beginning-of-line 1)
  3946. (when (and (looking-at org-table-line-regexp)
  3947. ;; Exclude tables in src/example/verbatim/clocktable blocks
  3948. (not (org-in-block-p '("src" "example" "verbatim" "clocktable"))))
  3949. (save-excursion (funcall function))
  3950. (or (looking-at org-table-line-regexp)
  3951. (forward-char 1)))
  3952. (re-search-forward org-table-any-border-regexp nil 1))))
  3953. (unless quietly (message "Mapping tables: done")))
  3954. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock" (beg end))
  3955. (declare-function org-clock-update-mode-line "org-clock" ())
  3956. (declare-function org-resolve-clocks "org-clock"
  3957. (&optional also-non-dangling-p prompt last-valid))
  3958. (defun org-at-TBLFM-p (&optional pos)
  3959. "Non-nil when point (or POS) is in #+TBLFM line."
  3960. (save-excursion
  3961. (goto-char (or pos (point)))
  3962. (beginning-of-line)
  3963. (and (let ((case-fold-search t)) (looking-at org-TBLFM-regexp))
  3964. (eq (org-element-type (org-element-at-point)) 'table))))
  3965. (defvar org-clock-start-time)
  3966. (defvar org-clock-marker (make-marker)
  3967. "Marker recording the last clock-in.")
  3968. (defvar org-clock-hd-marker (make-marker)
  3969. "Marker recording the last clock-in, but the headline position.")
  3970. (defvar org-clock-heading ""
  3971. "The heading of the current clock entry.")
  3972. (defun org-clock-is-active ()
  3973. "Return the buffer where the clock is currently running.
  3974. Return nil if no clock is running."
  3975. (marker-buffer org-clock-marker))
  3976. (defun org-check-running-clock ()
  3977. "Check if the current buffer contains the running clock.
  3978. If yes, offer to stop it and to save the buffer with the changes."
  3979. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  3980. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  3981. (buffer-name))))
  3982. (org-clock-out)
  3983. (when (y-or-n-p "Save changed buffer?")
  3984. (save-buffer))))
  3985. (defun org-clocktable-try-shift (dir n)
  3986. "Check if this line starts a clock table, if yes, shift the time block."
  3987. (when (org-match-line "^[ \t]*#\\+BEGIN:[ \t]+clocktable\\>")
  3988. (org-clocktable-shift dir n)))
  3989. ;;;###autoload
  3990. (defun org-clock-persistence-insinuate ()
  3991. "Set up hooks for clock persistence."
  3992. (require 'org-clock)
  3993. (add-hook 'org-mode-hook 'org-clock-load)
  3994. (add-hook 'kill-emacs-hook 'org-clock-save))
  3995. (defgroup org-archive nil
  3996. "Options concerning archiving in Org-mode."
  3997. :tag "Org Archive"
  3998. :group 'org-structure)
  3999. (defcustom org-archive-location "%s_archive::"
  4000. "The location where subtrees should be archived.
  4001. The value of this variable is a string, consisting of two parts,
  4002. separated by a double-colon. The first part is a filename and
  4003. the second part is a headline.
  4004. When the filename is omitted, archiving happens in the same file.
  4005. %s in the filename will be replaced by the current file
  4006. name (without the directory part). Archiving to a different file
  4007. is useful to keep archived entries from contributing to the
  4008. Org-mode Agenda.
  4009. The archived entries will be filed as subtrees of the specified
  4010. headline. When the headline is omitted, the subtrees are simply
  4011. filed away at the end of the file, as top-level entries. Also in
  4012. the heading you can use %s to represent the file name, this can be
  4013. useful when using the same archive for a number of different files.
  4014. Here are a few examples:
  4015. \"%s_archive::\"
  4016. If the current file is Projects.org, archive in file
  4017. Projects.org_archive, as top-level trees. This is the default.
  4018. \"::* Archived Tasks\"
  4019. Archive in the current file, under the top-level headline
  4020. \"* Archived Tasks\".
  4021. \"~/org/archive.org::\"
  4022. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  4023. \"~/org/archive.org::* From %s\"
  4024. Archive in file ~/org/archive.org (absolute path), under headlines
  4025. \"From FILENAME\" where file name is the current file name.
  4026. \"~/org/datetree.org::datetree/* Finished Tasks\"
  4027. The \"datetree/\" string is special, signifying to archive
  4028. items to the datetree. Items are placed in either the CLOSED
  4029. date of the item, or the current date if there is no CLOSED date.
  4030. The heading will be a subentry to the current date. There doesn't
  4031. need to be a heading, but there always needs to be a slash after
  4032. datetree. For example, to store archived items directly in the
  4033. datetree, use \"~/org/datetree.org::datetree/\".
  4034. \"basement::** Finished Tasks\"
  4035. Archive in file ./basement (relative path), as level 3 trees
  4036. below the level 2 heading \"** Finished Tasks\".
  4037. You may set this option on a per-file basis by adding to the buffer a
  4038. line like
  4039. #+ARCHIVE: basement::** Finished Tasks
  4040. You may also define it locally for a subtree by setting an ARCHIVE property
  4041. in the entry. If such a property is found in an entry, or anywhere up
  4042. the hierarchy, it will be used."
  4043. :group 'org-archive
  4044. :type 'string)
  4045. (defcustom org-agenda-skip-archived-trees t
  4046. "Non-nil means the agenda will skip any items located in archived trees.
  4047. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  4048. variable is no longer recommended, you should leave it at the value t.
  4049. Instead, use the key `v' to cycle the archives-mode in the agenda."
  4050. :group 'org-archive
  4051. :group 'org-agenda-skip
  4052. :type 'boolean)
  4053. (defcustom org-columns-skip-archived-trees t
  4054. "Non-nil means ignore archived trees when creating column view."
  4055. :group 'org-archive
  4056. :group 'org-properties
  4057. :type 'boolean)
  4058. (defcustom org-cycle-open-archived-trees nil
  4059. "Non-nil means `org-cycle' will open archived trees.
  4060. An archived tree is a tree marked with the tag ARCHIVE.
  4061. When nil, archived trees will stay folded. You can still open them with
  4062. normal outline commands like `show-all', but not with the cycling commands."
  4063. :group 'org-archive
  4064. :group 'org-cycle
  4065. :type 'boolean)
  4066. (defcustom org-sparse-tree-open-archived-trees nil
  4067. "Non-nil means sparse tree construction shows matches in archived trees.
  4068. When nil, matches in these trees are highlighted, but the trees are kept in
  4069. collapsed state."
  4070. :group 'org-archive
  4071. :group 'org-sparse-trees
  4072. :type 'boolean)
  4073. (defcustom org-sparse-tree-default-date-type nil
  4074. "The default date type when building a sparse tree.
  4075. When this is nil, a date is a scheduled or a deadline timestamp.
  4076. Otherwise, these types are allowed:
  4077. all: all timestamps
  4078. active: only active timestamps (<...>)
  4079. inactive: only inactive timestamps ([...])
  4080. scheduled: only scheduled timestamps
  4081. deadline: only deadline timestamps"
  4082. :type '(choice (const :tag "Scheduled or deadline" nil)
  4083. (const :tag "All timestamps" all)
  4084. (const :tag "Only active timestamps" active)
  4085. (const :tag "Only inactive timestamps" inactive)
  4086. (const :tag "Only scheduled timestamps" scheduled)
  4087. (const :tag "Only deadline timestamps" deadline)
  4088. (const :tag "Only closed timestamps" closed))
  4089. :version "25.1"
  4090. :package-version '(Org . "8.3")
  4091. :group 'org-sparse-trees)
  4092. (defun org-cycle-hide-archived-subtrees (state)
  4093. "Re-hide all archived subtrees after a visibility state change."
  4094. (when (and (not org-cycle-open-archived-trees)
  4095. (not (memq state '(overview folded))))
  4096. (save-excursion
  4097. (let* ((globalp (memq state '(contents all)))
  4098. (beg (if globalp (point-min) (point)))
  4099. (end (if globalp (point-max) (org-end-of-subtree t))))
  4100. (org-hide-archived-subtrees beg end)
  4101. (goto-char beg)
  4102. (if (looking-at (concat ".*:" org-archive-tag ":"))
  4103. (message "%s" (substitute-command-keys
  4104. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  4105. (defun org-force-cycle-archived ()
  4106. "Cycle subtree even if it is archived."
  4107. (interactive)
  4108. (setq this-command 'org-cycle)
  4109. (let ((org-cycle-open-archived-trees t))
  4110. (call-interactively 'org-cycle)))
  4111. (defun org-hide-archived-subtrees (beg end)
  4112. "Re-hide all archived subtrees after a visibility state change."
  4113. (org-with-wide-buffer
  4114. (let ((case-fold-search nil)
  4115. (re (concat org-outline-regexp-bol ".*:" org-archive-tag ":")))
  4116. (goto-char beg)
  4117. (while (and (< (point) end) (re-search-forward re end t))
  4118. (when (member org-archive-tag (org-get-tags))
  4119. (org-flag-subtree t)
  4120. (org-end-of-subtree t))))))
  4121. (declare-function outline-end-of-heading "outline" ())
  4122. (declare-function outline-flag-region "outline" (from to flag))
  4123. (defun org-flag-subtree (flag)
  4124. (save-excursion
  4125. (org-back-to-heading t)
  4126. (outline-end-of-heading)
  4127. (outline-flag-region (point)
  4128. (progn (org-end-of-subtree t) (point))
  4129. flag)))
  4130. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  4131. ;; Declare Column View Code
  4132. (declare-function org-columns-number-to-string "org-colview" (n fmt &optional printf))
  4133. (declare-function org-columns-get-format-and-top-level "org-colview" ())
  4134. (declare-function org-columns-compute "org-colview" (property))
  4135. ;; Declare ID code
  4136. (declare-function org-id-store-link "org-id")
  4137. (declare-function org-id-locations-load "org-id")
  4138. (declare-function org-id-locations-save "org-id")
  4139. (defvar org-id-track-globally)
  4140. ;;; Variables for pre-computed regular expressions, all buffer local
  4141. (defvar org-todo-regexp nil
  4142. "Matches any of the TODO state keywords.")
  4143. (make-variable-buffer-local 'org-todo-regexp)
  4144. (defvar org-not-done-regexp nil
  4145. "Matches any of the TODO state keywords except the last one.")
  4146. (make-variable-buffer-local 'org-not-done-regexp)
  4147. (defvar org-not-done-heading-regexp nil
  4148. "Matches a TODO headline that is not done.")
  4149. (make-variable-buffer-local 'org-not-done-heading-regexp)
  4150. (defvar org-todo-line-regexp nil
  4151. "Matches a headline and puts TODO state into group 2 if present.")
  4152. (make-variable-buffer-local 'org-todo-line-regexp)
  4153. (defvar org-complex-heading-regexp nil
  4154. "Matches a headline and puts everything into groups:
  4155. group 1: the stars
  4156. group 2: The todo keyword, maybe
  4157. group 3: Priority cookie
  4158. group 4: True headline
  4159. group 5: Tags")
  4160. (make-variable-buffer-local 'org-complex-heading-regexp)
  4161. (defvar org-complex-heading-regexp-format nil
  4162. "Printf format to make regexp to match an exact headline.
  4163. This regexp will match the headline of any node which has the
  4164. exact headline text that is put into the format, but may have any
  4165. TODO state, priority and tags.")
  4166. (make-variable-buffer-local 'org-complex-heading-regexp-format)
  4167. (defvar org-todo-line-tags-regexp nil
  4168. "Matches a headline and puts TODO state into group 2 if present.
  4169. Also put tags into group 4 if tags are present.")
  4170. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  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. (org-set-local 'org-file-properties (cdr (assq 'property alist)))
  4308. ;; Archive location.
  4309. (let ((archive (cdr (assq 'archive alist))))
  4310. (when archive (org-set-local 'org-archive-location archive)))
  4311. ;; Category.
  4312. (let ((cat (org-string-nw-p (cdr (assq 'category alist)))))
  4313. (when cat
  4314. (org-set-local 'org-category (intern cat))
  4315. (org-set-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 (org-set-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. (org-set-local 'org-highest-priority (nth 0 priorities))
  4330. (org-set-local 'org-lowest-priority (nth 1 priorities))
  4331. (org-set-local 'org-default-priority (nth 2 priorities))))
  4332. ;; Scripts.
  4333. (let ((scripts (assq 'scripts alist)))
  4334. (when scripts
  4335. (org-set-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)) (org-set-local var val)
  4344. (unless (listp (symbol-value var))
  4345. (org-set-local var nil))
  4346. (add-to-list var val)))))))
  4347. ;; TODO keywords.
  4348. (org-set-local 'org-todo-kwd-alist nil)
  4349. (org-set-local 'org-todo-key-alist nil)
  4350. (org-set-local 'org-todo-key-trigger nil)
  4351. (org-set-local 'org-todo-keywords-1 nil)
  4352. (org-set-local 'org-done-keywords nil)
  4353. (org-set-local 'org-todo-heads nil)
  4354. (org-set-local 'org-todo-sets nil)
  4355. (org-set-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. (if (not 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. (insert-file-contents f)
  4538. (setq alist
  4539. ;; Fake Org mode to benefit from cache
  4540. ;; without recurring needlessly.
  4541. (let ((major-mode 'org-mode))
  4542. (org--setup-collect-keywords
  4543. regexp (cons f files) alist)))))))))))))))
  4544. alist)
  4545. (defun org--setup-process-tags (tags filetags)
  4546. "Precompute variables used for tags.
  4547. TAGS is a list of tags and tag group symbols, as strings.
  4548. FILETAGS is a list of tags, as strings."
  4549. ;; Process the file tags.
  4550. (org-set-local 'org-file-tags
  4551. (mapcar #'org-add-prop-inherited filetags))
  4552. ;; Provide default tags if no local tags are found.
  4553. (when (and (not tags) org-tag-alist)
  4554. (setq tags
  4555. (mapcar (lambda (tag)
  4556. (case (car tag)
  4557. (:startgroup "{")
  4558. (:endgroup "}")
  4559. (:startgrouptag "[")
  4560. (:endgrouptag "]")
  4561. (:grouptags ":")
  4562. (:newline "\\n")
  4563. (otherwise (concat (car tag)
  4564. (and (characterp (cdr tag))
  4565. (format "(%c)" (cdr tag)))))))
  4566. org-tag-alist)))
  4567. ;; Process the tags.
  4568. (org-set-local 'org-tag-groups-alist nil)
  4569. (org-set-local 'org-tag-alist nil)
  4570. (let (group-flag)
  4571. (while tags
  4572. (let ((e (car tags)))
  4573. (setq tags (cdr tags))
  4574. (cond
  4575. ((equal e "{")
  4576. (push '(:startgroup) org-tag-alist)
  4577. (when (equal (nth 1 tags) ":") (setq group-flag t)))
  4578. ((equal e "}")
  4579. (push '(:endgroup) org-tag-alist)
  4580. (setq group-flag nil))
  4581. ((equal e "[")
  4582. (push '(:startgrouptag) org-tag-alist)
  4583. (when (equal (nth 1 tags) ":") (setq group-flag t)))
  4584. ((equal e "]")
  4585. (push '(:endgrouptag) org-tag-alist)
  4586. (setq group-flag nil))
  4587. ((equal e ":")
  4588. (push '(:grouptags) org-tag-alist)
  4589. (setq group-flag 'append))
  4590. ((equal e "\\n") (push '(:newline) org-tag-alist))
  4591. ((string-match
  4592. (org-re (concat "\\`\\([[:alnum:]_@#%]+"
  4593. "\\|{.+?}\\)" ; regular expression
  4594. "\\(?:(\\(.\\))\\)?\\'")) e)
  4595. (let ((tag (match-string 1 e))
  4596. (key (and (match-beginning 2)
  4597. (string-to-char (match-string 2 e)))))
  4598. (cond ((eq group-flag 'append)
  4599. (setcar org-tag-groups-alist
  4600. (append (car org-tag-groups-alist) (list tag))))
  4601. (group-flag (push (list tag) org-tag-groups-alist)))
  4602. ;; Push all tags in groups, no matter if they already exist.
  4603. (unless (and (not group-flag) (assoc tag org-tag-alist))
  4604. (push (cons tag key) org-tag-alist))))))))
  4605. (setq org-tag-alist (nreverse org-tag-alist)))
  4606. (defun org-file-contents (file &optional noerror)
  4607. "Return the contents of FILE, as a string."
  4608. (if (and file (file-readable-p file))
  4609. (with-temp-buffer
  4610. (insert-file-contents file)
  4611. (buffer-string))
  4612. (funcall (if noerror 'message 'error)
  4613. "Cannot read file \"%s\"%s"
  4614. file
  4615. (let ((from (buffer-file-name (buffer-base-buffer))))
  4616. (if from (concat " (referenced in file \"" from "\")") "")))))
  4617. (defun org-extract-log-state-settings (x)
  4618. "Extract the log state setting from a TODO keyword string.
  4619. This will extract info from a string like \"WAIT(w@/!)\"."
  4620. (let (kw key log1 log2)
  4621. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  4622. (setq kw (match-string 1 x)
  4623. key (and (match-end 2) (match-string 2 x))
  4624. log1 (and (match-end 3) (match-string 3 x))
  4625. log2 (and (match-end 4) (match-string 4 x)))
  4626. (and (or log1 log2)
  4627. (list kw
  4628. (and log1 (if (equal log1 "!") 'time 'note))
  4629. (and log2 (if (equal log2 "!") 'time 'note)))))))
  4630. (defun org-remove-keyword-keys (list)
  4631. "Remove a pair of parenthesis at the end of each string in LIST."
  4632. (mapcar (lambda (x)
  4633. (if (string-match "(.*)$" x)
  4634. (substring x 0 (match-beginning 0))
  4635. x))
  4636. list))
  4637. (defun org-assign-fast-keys (alist)
  4638. "Assign fast keys to a keyword-key alist.
  4639. Respect keys that are already there."
  4640. (let (new e (alt ?0))
  4641. (while (setq e (pop alist))
  4642. (if (or (memq (car e) '(:newline :grouptags :endgroup :startgroup))
  4643. (cdr e)) ;; Key already assigned.
  4644. (push e new)
  4645. (let ((clist (string-to-list (downcase (car e))))
  4646. (used (append new alist)))
  4647. (when (= (car clist) ?@)
  4648. (pop clist))
  4649. (while (and clist (rassoc (car clist) used))
  4650. (pop clist))
  4651. (unless clist
  4652. (while (rassoc alt used)
  4653. (incf alt)))
  4654. (push (cons (car e) (or (car clist) alt)) new))))
  4655. (nreverse new)))
  4656. ;;; Some variables used in various places
  4657. (defvar org-window-configuration nil
  4658. "Used in various places to store a window configuration.")
  4659. (defvar org-selected-window nil
  4660. "Used in various places to store a window configuration.")
  4661. (defvar org-finish-function nil
  4662. "Function to be called when `C-c C-c' is used.
  4663. This is for getting out of special buffers like capture.")
  4664. ;; FIXME: Occasionally check by commenting these, to make sure
  4665. ;; no other functions uses these, forgetting to let-bind them.
  4666. (org-no-warnings (defvar entry)) ;; unprefixed, from calendar.el
  4667. (defvar org-last-state)
  4668. (org-no-warnings (defvar date)) ;; unprefixed, from calendar.el
  4669. ;; Defined somewhere in this file, but used before definition.
  4670. (defvar org-entities) ;; defined in org-entities.el
  4671. (defvar org-struct-menu)
  4672. (defvar org-org-menu)
  4673. (defvar org-tbl-menu)
  4674. ;;;; Define the Org-mode
  4675. ;; We use a before-change function to check if a table might need
  4676. ;; an update.
  4677. (defvar org-table-may-need-update t
  4678. "Indicates that a table might need an update.
  4679. This variable is set by `org-before-change-function'.
  4680. `org-table-align' sets it back to nil.")
  4681. (defun org-before-change-function (beg end)
  4682. "Every change indicates that a table might need an update."
  4683. (setq org-table-may-need-update t))
  4684. (defvar org-mode-map)
  4685. (defvar org-inhibit-startup-visibility-stuff nil) ; Dynamically-scoped param.
  4686. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  4687. (defvar org-inhibit-logging nil) ; Dynamically-scoped param.
  4688. (defvar org-inhibit-blocking nil) ; Dynamically-scoped param.
  4689. (defvar org-table-buffer-is-an nil)
  4690. (defvar bidi-paragraph-direction)
  4691. (defvar buffer-face-mode-face)
  4692. (require 'outline)
  4693. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  4694. (error "Conflict with outdated version of allout.el. Load org.el before allout.el, or upgrade to newer allout, for example by switching to Emacs 22"))
  4695. (require 'noutline "noutline" 'noerror) ;; stock XEmacs does not have it
  4696. ;; Other stuff we need.
  4697. (require 'time-date)
  4698. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  4699. (require 'easymenu)
  4700. (autoload 'easy-menu-add "easymenu")
  4701. (require 'overlay)
  4702. ;; (require 'org-macs) moved higher up in the file before it is first used
  4703. (require 'org-entities)
  4704. ;; (require 'org-compat) moved higher up in the file before it is first used
  4705. (require 'org-faces)
  4706. (require 'org-list)
  4707. (require 'org-pcomplete)
  4708. (require 'org-src)
  4709. (require 'org-footnote)
  4710. (require 'org-macro)
  4711. ;; babel
  4712. (require 'ob)
  4713. ;;;###autoload
  4714. (define-derived-mode org-mode outline-mode "Org"
  4715. "Outline-based notes management and organizer, alias
  4716. \"Carsten's outline-mode for keeping track of everything.\"
  4717. Org-mode develops organizational tasks around a NOTES file which
  4718. contains information about projects as plain text. Org-mode is
  4719. implemented on top of outline-mode, which is ideal to keep the content
  4720. of large files well structured. It supports ToDo items, deadlines and
  4721. time stamps, which magically appear in the diary listing of the Emacs
  4722. calendar. Tables are easily created with a built-in table editor.
  4723. Plain text URL-like links connect to websites, emails (VM), Usenet
  4724. messages (Gnus), BBDB entries, and any files related to the project.
  4725. For printing and sharing of notes, an Org-mode file (or a part of it)
  4726. can be exported as a structured ASCII or HTML file.
  4727. The following commands are available:
  4728. \\{org-mode-map}"
  4729. ;; Get rid of Outline menus, they are not needed
  4730. ;; Need to do this here because define-derived-mode sets up
  4731. ;; the keymap so late. Still, it is a waste to call this each time
  4732. ;; we switch another buffer into org-mode.
  4733. (if (featurep 'xemacs)
  4734. (when (boundp 'outline-mode-menu-heading)
  4735. ;; Assume this is Greg's port, it uses easymenu
  4736. (easy-menu-remove outline-mode-menu-heading)
  4737. (easy-menu-remove outline-mode-menu-show)
  4738. (easy-menu-remove outline-mode-menu-hide))
  4739. (define-key org-mode-map [menu-bar headings] 'undefined)
  4740. (define-key org-mode-map [menu-bar hide] 'undefined)
  4741. (define-key org-mode-map [menu-bar show] 'undefined))
  4742. (org-load-modules-maybe)
  4743. (when (featurep 'xemacs)
  4744. (easy-menu-add org-org-menu)
  4745. (easy-menu-add org-tbl-menu))
  4746. (org-install-agenda-files-menu)
  4747. (if org-descriptive-links (add-to-invisibility-spec '(org-link)))
  4748. (add-to-invisibility-spec '(org-cwidth))
  4749. (add-to-invisibility-spec '(org-hide-block . t))
  4750. (when (featurep 'xemacs)
  4751. (org-set-local 'line-move-ignore-invisible t))
  4752. (org-set-local 'outline-regexp org-outline-regexp)
  4753. (org-set-local 'outline-level 'org-outline-level)
  4754. (setq bidi-paragraph-direction 'left-to-right)
  4755. (when (and org-ellipsis
  4756. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  4757. (fboundp 'make-glyph-code))
  4758. (unless org-display-table
  4759. (setq org-display-table (make-display-table)))
  4760. (set-display-table-slot
  4761. org-display-table 4
  4762. (vconcat (mapcar
  4763. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  4764. org-ellipsis)))
  4765. (if (stringp org-ellipsis) org-ellipsis "..."))))
  4766. (setq buffer-display-table org-display-table))
  4767. (org-set-regexps-and-options)
  4768. (org-set-font-lock-defaults)
  4769. (when (and org-tag-faces (not org-tags-special-faces-re))
  4770. ;; tag faces set outside customize.... force initialization.
  4771. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  4772. ;; Calc embedded
  4773. (org-set-local 'calc-embedded-open-mode "# ")
  4774. ;; Modify a few syntax entries
  4775. (modify-syntax-entry ?@ "w")
  4776. (modify-syntax-entry ?\" "\"")
  4777. (modify-syntax-entry ?\\ "_")
  4778. (modify-syntax-entry ?~ "_")
  4779. (if org-startup-truncated (setq truncate-lines t))
  4780. (when org-startup-indented (require 'org-indent) (org-indent-mode 1))
  4781. (org-set-local 'font-lock-unfontify-region-function
  4782. 'org-unfontify-region)
  4783. ;; Activate before-change-function
  4784. (org-set-local 'org-table-may-need-update t)
  4785. (org-add-hook 'before-change-functions 'org-before-change-function nil
  4786. 'local)
  4787. ;; Check for running clock before killing a buffer
  4788. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  4789. ;; Initialize macros templates.
  4790. (org-macro-initialize-templates)
  4791. ;; Initialize radio targets.
  4792. (org-update-radio-target-regexp)
  4793. ;; Indentation.
  4794. (org-set-local 'indent-line-function 'org-indent-line)
  4795. (org-set-local 'indent-region-function 'org-indent-region)
  4796. ;; Filling and auto-filling.
  4797. (org-setup-filling)
  4798. ;; Comments.
  4799. (org-setup-comments-handling)
  4800. ;; Initialize cache.
  4801. (org-element-cache-reset)
  4802. ;; Beginning/end of defun
  4803. (org-set-local 'beginning-of-defun-function 'org-backward-element)
  4804. (org-set-local 'end-of-defun-function
  4805. (lambda ()
  4806. (if (not (org-at-heading-p))
  4807. (org-forward-element)
  4808. (org-forward-element)
  4809. (forward-char -1))))
  4810. ;; Next error for sparse trees
  4811. (org-set-local 'next-error-function 'org-occur-next-match)
  4812. ;; Make sure dependence stuff works reliably, even for users who set it
  4813. ;; too late :-(
  4814. (if org-enforce-todo-dependencies
  4815. (add-hook 'org-blocker-hook
  4816. 'org-block-todo-from-children-or-siblings-or-parent)
  4817. (remove-hook 'org-blocker-hook
  4818. 'org-block-todo-from-children-or-siblings-or-parent))
  4819. (if org-enforce-todo-checkbox-dependencies
  4820. (add-hook 'org-blocker-hook
  4821. 'org-block-todo-from-checkboxes)
  4822. (remove-hook 'org-blocker-hook
  4823. 'org-block-todo-from-checkboxes))
  4824. ;; Align options lines
  4825. (org-set-local
  4826. 'align-mode-rules-list
  4827. '((org-in-buffer-settings
  4828. (regexp . "^[ \t]*#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  4829. (modes . '(org-mode)))))
  4830. ;; Imenu
  4831. (org-set-local 'imenu-create-index-function
  4832. 'org-imenu-get-tree)
  4833. ;; Make isearch reveal context
  4834. (if (or (featurep 'xemacs)
  4835. (not (boundp 'outline-isearch-open-invisible-function)))
  4836. ;; Emacs 21 and XEmacs make use of the hook
  4837. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  4838. ;; Emacs 22 deals with this through a special variable
  4839. (org-set-local 'outline-isearch-open-invisible-function
  4840. (lambda (&rest ignore) (org-show-context 'isearch))))
  4841. ;; Setup the pcomplete hooks
  4842. (set (make-local-variable 'pcomplete-command-completion-function)
  4843. 'org-pcomplete-initial)
  4844. (set (make-local-variable 'pcomplete-command-name-function)
  4845. 'org-command-at-point)
  4846. (set (make-local-variable 'pcomplete-default-completion-function)
  4847. 'ignore)
  4848. (set (make-local-variable 'pcomplete-parse-arguments-function)
  4849. 'org-parse-arguments)
  4850. (set (make-local-variable 'pcomplete-termination-string) "")
  4851. (when (>= emacs-major-version 23)
  4852. (set (make-local-variable 'buffer-face-mode-face) 'org-default))
  4853. ;; If empty file that did not turn on org-mode automatically, make it to.
  4854. (if (and org-insert-mode-line-in-empty-file
  4855. (org-called-interactively-p 'any)
  4856. (= (point-min) (point-max)))
  4857. (insert "# -*- mode: org -*-\n\n"))
  4858. (unless org-inhibit-startup
  4859. (org-unmodified
  4860. (and org-startup-with-beamer-mode (org-beamer-mode))
  4861. (when org-startup-align-all-tables
  4862. (org-table-map-tables 'org-table-align 'quietly))
  4863. (when org-startup-with-inline-images
  4864. (org-display-inline-images))
  4865. (when org-startup-with-latex-preview
  4866. (org-toggle-latex-fragment))
  4867. (unless org-inhibit-startup-visibility-stuff
  4868. (org-set-startup-visibility))
  4869. (org-refresh-effort-properties)))
  4870. ;; Try to set org-hide correctly
  4871. (let ((foreground (org-find-invisible-foreground)))
  4872. (if foreground
  4873. (set-face-foreground 'org-hide foreground))))
  4874. ;; Update `customize-package-emacs-version-alist'
  4875. (add-to-list 'customize-package-emacs-version-alist
  4876. '(Org ("6.21b" . "23.1") ("6.33x" . "23.2")
  4877. ("7.8.11" . "24.1") ("7.9.4" . "24.3")
  4878. ("8.2.6" . "24.4") ("8.3" . "25.1")))
  4879. (defvar org-mode-transpose-word-syntax-table
  4880. (let ((st (make-syntax-table text-mode-syntax-table)))
  4881. (mapc (lambda(c) (modify-syntax-entry
  4882. (string-to-char (car c)) "w p" st))
  4883. org-emphasis-alist)
  4884. st))
  4885. (when (fboundp 'abbrev-table-put)
  4886. (abbrev-table-put org-mode-abbrev-table
  4887. :parents (list text-mode-abbrev-table)))
  4888. (defun org-find-invisible-foreground ()
  4889. (let ((candidates (remove
  4890. "unspecified-bg"
  4891. (nconc
  4892. (list (face-background 'default)
  4893. (face-background 'org-default))
  4894. (mapcar
  4895. (lambda (alist)
  4896. (when (boundp alist)
  4897. (cdr (assoc 'background-color (symbol-value alist)))))
  4898. '(default-frame-alist initial-frame-alist window-system-default-frame-alist))
  4899. (list (face-foreground 'org-hide))))))
  4900. (car (remove nil candidates))))
  4901. (defun org-current-time (&optional rounding-minutes past)
  4902. "Current time, possibly rounded to ROUNDING-MINUTES.
  4903. When ROUNDING-MINUTES is not an integer, fall back on the car of
  4904. `org-time-stamp-rounding-minutes'. When PAST is non-nil, ensure
  4905. the rounding returns a past time."
  4906. (let ((r (or (and (integerp rounding-minutes) rounding-minutes)
  4907. (car org-time-stamp-rounding-minutes)))
  4908. (time (decode-time)) res)
  4909. (if (< r 1)
  4910. (current-time)
  4911. (setq res
  4912. (apply 'encode-time
  4913. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  4914. (nthcdr 2 time))))
  4915. (if (and past (< (org-float-time (time-subtract (current-time) res)) 0))
  4916. (seconds-to-time (- (org-float-time res) (* r 60)))
  4917. res))))
  4918. (defun org-today ()
  4919. "Return today date, considering `org-extend-today-until'."
  4920. (time-to-days
  4921. (time-subtract (current-time)
  4922. (list 0 (* 3600 org-extend-today-until) 0))))
  4923. ;;;; Font-Lock stuff, including the activators
  4924. (defvar org-mouse-map (make-sparse-keymap))
  4925. (org-defkey org-mouse-map [mouse-2] 'org-open-at-mouse)
  4926. (org-defkey org-mouse-map [mouse-3] 'org-find-file-at-mouse)
  4927. (when org-mouse-1-follows-link
  4928. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  4929. (when org-tab-follows-link
  4930. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  4931. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  4932. (require 'font-lock)
  4933. (defconst org-non-link-chars "]\t\n\r<>")
  4934. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "file+emacs"
  4935. "file+sys" "news" "shell" "elisp" "doi" "message"
  4936. "help"))
  4937. (defvar org-link-types-re nil
  4938. "Matches a link that has a url-like prefix like \"http:\"")
  4939. (defvar org-link-re-with-space nil
  4940. "Matches a link with spaces, optional angular brackets around it.")
  4941. (defvar org-link-re-with-space2 nil
  4942. "Matches a link with spaces, optional angular brackets around it.")
  4943. (defvar org-link-re-with-space3 nil
  4944. "Matches a link with spaces, only for internal part in bracket links.")
  4945. (defvar org-angle-link-re nil
  4946. "Matches link with angular brackets, spaces are allowed.")
  4947. (defvar org-plain-link-re nil
  4948. "Matches plain link, without spaces.")
  4949. (defvar org-bracket-link-regexp nil
  4950. "Matches a link in double brackets.")
  4951. (defvar org-bracket-link-analytic-regexp nil
  4952. "Regular expression used to analyze links.
  4953. Here is what the match groups contain after a match:
  4954. 1: http:
  4955. 2: http
  4956. 3: path
  4957. 4: [desc]
  4958. 5: desc")
  4959. (defvar org-bracket-link-analytic-regexp++ nil
  4960. "Like `org-bracket-link-analytic-regexp', but include coderef internal type.")
  4961. (defvar org-any-link-re nil
  4962. "Regular expression matching any link.")
  4963. (defconst org-match-sexp-depth 3
  4964. "Number of stacked braces for sub/superscript matching.")
  4965. (defun org-create-multibrace-regexp (left right n)
  4966. "Create a regular expression which will match a balanced sexp.
  4967. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  4968. as single character strings.
  4969. The regexp returned will match the entire expression including the
  4970. delimiters. It will also define a single group which contains the
  4971. match except for the outermost delimiters. The maximum depth of
  4972. stacked delimiters is N. Escaping delimiters is not possible."
  4973. (let* ((nothing (concat "[^" left right "]*?"))
  4974. (or "\\|")
  4975. (re nothing)
  4976. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  4977. (while (> n 1)
  4978. (setq n (1- n)
  4979. re (concat re or next)
  4980. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  4981. (concat left "\\(" re "\\)" right)))
  4982. (defconst org-match-substring-regexp
  4983. (concat
  4984. "\\(\\S-\\)\\([_^]\\)\\("
  4985. "\\(?:" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  4986. "\\|"
  4987. "\\(?:" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  4988. "\\|"
  4989. "\\(?:\\*\\|[+-]?[[:alnum:].,\\]*[[:alnum:]]\\)\\)")
  4990. "The regular expression matching a sub- or superscript.")
  4991. (defconst org-match-substring-with-braces-regexp
  4992. (concat
  4993. "\\(\\S-\\)\\([_^]\\)"
  4994. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)")
  4995. "The regular expression matching a sub- or superscript, forcing braces.")
  4996. (defun org-make-link-regexps ()
  4997. "Update the link regular expressions.
  4998. This should be called after the variable `org-link-types' has changed."
  4999. (let ((types-re (regexp-opt org-link-types t)))
  5000. (setq org-link-types-re
  5001. (concat "\\`" types-re ":")
  5002. org-link-re-with-space
  5003. (concat "<?" types-re ":"
  5004. "\\([^" org-non-link-chars " ]"
  5005. "[^" org-non-link-chars "]*"
  5006. "[^" org-non-link-chars " ]\\)>?")
  5007. org-link-re-with-space2
  5008. (concat "<?" types-re ":"
  5009. "\\([^" org-non-link-chars " ]"
  5010. "[^\t\n\r]*"
  5011. "[^" org-non-link-chars " ]\\)>?")
  5012. org-link-re-with-space3
  5013. (concat "<?" types-re ":"
  5014. "\\([^" org-non-link-chars " ]"
  5015. "[^\t\n\r]*\\)")
  5016. org-angle-link-re
  5017. (format "<%s:\\([^>\n]*\\(?:\n[ \t]*[^> \t\n][^>\n]*\\)*\\)>"
  5018. types-re)
  5019. org-plain-link-re
  5020. (concat
  5021. "\\<" types-re ":"
  5022. (org-re "\\([^ \t\n()<>]+\\(?:([[:word:]0-9_]+)\\|\\([^[:punct:] \t\n]\\|/\\)\\)\\)"))
  5023. ;; "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  5024. org-bracket-link-regexp
  5025. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  5026. org-bracket-link-analytic-regexp
  5027. (concat
  5028. "\\[\\["
  5029. "\\(" types-re ":\\)?"
  5030. "\\([^]]+\\)"
  5031. "\\]"
  5032. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  5033. "\\]")
  5034. org-bracket-link-analytic-regexp++
  5035. (concat
  5036. "\\[\\["
  5037. "\\(" (regexp-opt (cons "coderef" org-link-types) t) ":\\)?"
  5038. "\\([^]]+\\)"
  5039. "\\]"
  5040. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  5041. "\\]")
  5042. org-any-link-re
  5043. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  5044. org-angle-link-re "\\)\\|\\("
  5045. org-plain-link-re "\\)"))))
  5046. (org-make-link-regexps)
  5047. (defvar org-emph-face nil)
  5048. (defun org-do-emphasis-faces (limit)
  5049. "Run through the buffer and emphasize strings."
  5050. (let (rtn a)
  5051. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  5052. (let* ((border (char-after (match-beginning 3)))
  5053. (bre (regexp-quote (char-to-string border))))
  5054. (if (and (not (= border (char-after (match-beginning 4))))
  5055. (not (save-match-data
  5056. (string-match (concat bre ".*" bre)
  5057. (replace-regexp-in-string
  5058. "\n" " "
  5059. (substring (match-string 2) 1 -1))))))
  5060. (progn
  5061. (setq rtn t)
  5062. (setq a (assoc (match-string 3) org-emphasis-alist))
  5063. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  5064. 'face
  5065. (nth 1 a))
  5066. (and (nth 2 a)
  5067. (org-remove-flyspell-overlays-in
  5068. (match-beginning 0) (match-end 0)))
  5069. (add-text-properties (match-beginning 2) (match-end 2)
  5070. '(font-lock-multiline t org-emphasis t))
  5071. (when org-hide-emphasis-markers
  5072. (add-text-properties (match-end 4) (match-beginning 5)
  5073. '(invisible org-link))
  5074. (add-text-properties (match-beginning 3) (match-end 3)
  5075. '(invisible org-link))))))
  5076. (goto-char (1+ (match-beginning 0))))
  5077. rtn))
  5078. (defun org-emphasize (&optional char)
  5079. "Insert or change an emphasis, i.e. a font like bold or italic.
  5080. If there is an active region, change that region to a new emphasis.
  5081. If there is no region, just insert the marker characters and position
  5082. the cursor between them.
  5083. CHAR should be the marker character. If it is a space, it means to
  5084. remove the emphasis of the selected region.
  5085. If CHAR is not given (for example in an interactive call) it will be
  5086. prompted for."
  5087. (interactive)
  5088. (let ((erc org-emphasis-regexp-components)
  5089. (prompt "")
  5090. (string "") beg end move c s)
  5091. (if (org-region-active-p)
  5092. (setq beg (region-beginning) end (region-end)
  5093. string (buffer-substring beg end))
  5094. (setq move t))
  5095. (unless char
  5096. (message "Emphasis marker or tag: [%s]"
  5097. (mapconcat (lambda(e) (car e)) org-emphasis-alist ""))
  5098. (setq char (read-char-exclusive)))
  5099. (if (equal char ?\ )
  5100. (setq s "" move nil)
  5101. (unless (assoc (char-to-string char) org-emphasis-alist)
  5102. (user-error "No such emphasis marker: \"%c\"" char))
  5103. (setq s (char-to-string char)))
  5104. (while (and (> (length string) 1)
  5105. (equal (substring string 0 1) (substring string -1))
  5106. (assoc (substring string 0 1) org-emphasis-alist))
  5107. (setq string (substring string 1 -1)))
  5108. (setq string (concat s string s))
  5109. (if beg (delete-region beg end))
  5110. (unless (or (bolp)
  5111. (string-match (concat "[" (nth 0 erc) "\n]")
  5112. (char-to-string (char-before (point)))))
  5113. (insert " "))
  5114. (unless (or (eobp)
  5115. (string-match (concat "[" (nth 1 erc) "\n]")
  5116. (char-to-string (char-after (point)))))
  5117. (insert " ") (backward-char 1))
  5118. (insert string)
  5119. (and move (backward-char 1))))
  5120. (defconst org-nonsticky-props
  5121. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text htmlize-link))
  5122. (defsubst org-rear-nonsticky-at (pos)
  5123. (add-text-properties (1- pos) pos (list 'rear-nonsticky org-nonsticky-props)))
  5124. (defun org-activate-plain-links (limit)
  5125. "Add link properties for plain links."
  5126. (let (f hl)
  5127. (when (and (re-search-forward (concat org-plain-link-re) limit t)
  5128. (not (member 'org-tag
  5129. (get-text-property (1- (match-beginning 0)) 'face)))
  5130. (not (org-in-src-block-p)))
  5131. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5132. (setq f (get-text-property (match-beginning 0) 'face))
  5133. (setq hl (org-match-string-no-properties 0))
  5134. (if (or (eq f 'org-tag)
  5135. (and (listp f) (memq 'org-tag f)))
  5136. nil
  5137. (add-text-properties (match-beginning 0) (match-end 0)
  5138. (list 'mouse-face 'highlight
  5139. 'face 'org-link
  5140. 'htmlize-link `(:uri ,hl)
  5141. 'keymap org-mouse-map))
  5142. (org-rear-nonsticky-at (match-end 0)))
  5143. t)))
  5144. (defun org-activate-code (limit)
  5145. (if (re-search-forward "^[ \t]*\\(:\\(?: .*\\|$\\)\n?\\)" limit t)
  5146. (progn
  5147. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5148. (remove-text-properties (match-beginning 0) (match-end 0)
  5149. '(display t invisible t intangible t))
  5150. t)))
  5151. (defcustom org-src-fontify-natively t
  5152. "When non-nil, fontify code in code blocks."
  5153. :type 'boolean
  5154. :version "24.4"
  5155. :package-version '(Org . "8.3")
  5156. :group 'org-appearance
  5157. :group 'org-babel)
  5158. (defcustom org-allow-promoting-top-level-subtree nil
  5159. "When non-nil, allow promoting a top level subtree.
  5160. The leading star of the top level headline will be replaced
  5161. by a #."
  5162. :type 'boolean
  5163. :version "24.1"
  5164. :group 'org-appearance)
  5165. (defun org-fontify-meta-lines-and-blocks (limit)
  5166. (condition-case nil
  5167. (org-fontify-meta-lines-and-blocks-1 limit)
  5168. (error (message "org-mode fontification error"))))
  5169. (defun org-fontify-meta-lines-and-blocks-1 (limit)
  5170. "Fontify #+ lines and blocks."
  5171. (let ((case-fold-search t))
  5172. (if (re-search-forward
  5173. "^\\([ \t]*#\\(\\(\\+[a-zA-Z]+:?\\| \\|$\\)\\(_\\([a-zA-Z]+\\)\\)?\\)[ \t]*\\(\\([^ \t\n]*\\)[ \t]*\\(.*\\)\\)\\)"
  5174. limit t)
  5175. (let ((beg (match-beginning 0))
  5176. (block-start (match-end 0))
  5177. (block-end nil)
  5178. (lang (match-string 7))
  5179. (beg1 (line-beginning-position 2))
  5180. (dc1 (downcase (match-string 2)))
  5181. (dc3 (downcase (match-string 3)))
  5182. end end1 quoting block-type ovl)
  5183. (cond
  5184. ((and (match-end 4) (equal dc3 "+begin"))
  5185. ;; Truly a block
  5186. (setq block-type (downcase (match-string 5))
  5187. quoting (member block-type org-protecting-blocks))
  5188. (when (re-search-forward
  5189. (concat "^[ \t]*#\\+end" (match-string 4) "\\>.*")
  5190. nil t) ;; on purpose, we look further than LIMIT
  5191. (setq end (min (point-max) (match-end 0))
  5192. end1 (min (point-max) (1- (match-beginning 0))))
  5193. (setq block-end (match-beginning 0))
  5194. (when quoting
  5195. (org-remove-flyspell-overlays-in beg1 end1)
  5196. (remove-text-properties beg end
  5197. '(display t invisible t intangible t)))
  5198. (add-text-properties
  5199. beg end '(font-lock-fontified t font-lock-multiline t))
  5200. (add-text-properties beg beg1 '(face org-meta-line))
  5201. (org-remove-flyspell-overlays-in beg beg1)
  5202. (add-text-properties ; For end_src
  5203. end1 (min (point-max) (1+ end)) '(face org-meta-line))
  5204. (org-remove-flyspell-overlays-in end1 end)
  5205. (cond
  5206. ((and lang (not (string= lang "")) org-src-fontify-natively)
  5207. (org-src-font-lock-fontify-block lang block-start block-end)
  5208. (add-text-properties beg1 block-end '(src-block t)))
  5209. (quoting
  5210. (add-text-properties beg1 (min (point-max) (1+ end1))
  5211. '(face org-block))) ; end of source block
  5212. ((not org-fontify-quote-and-verse-blocks))
  5213. ((string= block-type "quote")
  5214. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-quote)))
  5215. ((string= block-type "verse")
  5216. (add-text-properties beg1 (min (point-max) (1+ end1)) '(face org-verse))))
  5217. (add-text-properties beg beg1 '(face org-block-begin-line))
  5218. (add-text-properties (min (point-max) (1+ end)) (min (point-max) (1+ end1))
  5219. '(face org-block-end-line))
  5220. t))
  5221. ((member dc1 '("+title:" "+author:" "+email:" "+date:"))
  5222. (org-remove-flyspell-overlays-in
  5223. (match-beginning 0)
  5224. (if (equal "+title:" dc1) (match-end 2) (match-end 0)))
  5225. (add-text-properties
  5226. beg (match-end 3)
  5227. (if (member (intern (substring dc1 1 -1)) org-hidden-keywords)
  5228. '(font-lock-fontified t invisible t)
  5229. '(font-lock-fontified t face org-document-info-keyword)))
  5230. (add-text-properties
  5231. (match-beginning 6) (min (point-max) (1+ (match-end 6)))
  5232. (if (string-equal dc1 "+title:")
  5233. '(font-lock-fontified t face org-document-title)
  5234. '(font-lock-fontified t face org-document-info))))
  5235. ((equal dc1 "+caption:")
  5236. (org-remove-flyspell-overlays-in (match-end 2) (match-end 0))
  5237. (remove-text-properties (match-beginning 0) (match-end 0)
  5238. '(display t invisible t intangible t))
  5239. (add-text-properties (match-beginning 1) (match-end 3)
  5240. '(font-lock-fontified t face org-meta-line))
  5241. (add-text-properties (match-beginning 6) (+ (match-end 6) 1)
  5242. '(font-lock-fontified t face org-block))
  5243. t)
  5244. ((member dc3 '(" " ""))
  5245. (org-remove-flyspell-overlays-in beg (match-end 0))
  5246. (add-text-properties
  5247. beg (match-end 0)
  5248. '(font-lock-fontified t face font-lock-comment-face)))
  5249. (t ;; just any other in-buffer setting, but not indented
  5250. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5251. (remove-text-properties (match-beginning 0) (match-end 0)
  5252. '(display t invisible t intangible t))
  5253. (add-text-properties beg (match-end 0)
  5254. '(font-lock-fontified t face org-meta-line))
  5255. t))))))
  5256. (defun org-fontify-drawers (limit)
  5257. "Fontify drawers."
  5258. (when (re-search-forward org-drawer-regexp limit t)
  5259. (add-text-properties
  5260. (match-beginning 0) (match-end 0)
  5261. '(font-lock-fontified t face org-special-keyword))
  5262. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5263. t))
  5264. (defun org-fontify-macros (limit)
  5265. "Fontify macros."
  5266. (when (re-search-forward "\\({{{\\).+?\\(}}}\\)" limit t)
  5267. (add-text-properties
  5268. (match-beginning 0) (match-end 0)
  5269. '(font-lock-fontified t face org-macro))
  5270. (when org-hide-macro-markers
  5271. (add-text-properties (match-end 2) (match-beginning 2)
  5272. '(invisible t))
  5273. (add-text-properties (match-beginning 1) (match-end 1)
  5274. '(invisible t)))
  5275. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5276. t))
  5277. (defun org-activate-angle-links (limit)
  5278. "Add text properties for angle links."
  5279. (if (and (re-search-forward org-angle-link-re limit t)
  5280. (not (org-in-src-block-p)))
  5281. (progn
  5282. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5283. (add-text-properties (match-beginning 0) (match-end 0)
  5284. (list 'mouse-face 'highlight
  5285. 'keymap org-mouse-map
  5286. 'font-lock-multiline t))
  5287. (org-rear-nonsticky-at (match-end 0))
  5288. t)))
  5289. (defun org-activate-footnote-links (limit)
  5290. "Add text properties for footnotes."
  5291. (let ((fn (org-footnote-next-reference-or-definition limit)))
  5292. (when fn
  5293. (let* ((beg (nth 1 fn))
  5294. (end (nth 2 fn))
  5295. (label (car fn))
  5296. (referencep (/= (line-beginning-position) beg)))
  5297. (when (and referencep (nth 3 fn))
  5298. (save-excursion
  5299. (goto-char beg)
  5300. (search-forward (or label "fn:"))
  5301. (org-remove-flyspell-overlays-in beg (match-end 0))))
  5302. (add-text-properties beg end
  5303. (list 'mouse-face 'highlight
  5304. 'keymap org-mouse-map
  5305. 'help-echo
  5306. (if referencep "Footnote reference"
  5307. "Footnote definition")
  5308. 'font-lock-fontified t
  5309. 'font-lock-multiline t
  5310. 'face 'org-footnote))))))
  5311. (defun org-activate-bracket-links (limit)
  5312. "Add text properties for bracketed links."
  5313. (if (and (re-search-forward org-bracket-link-regexp limit t)
  5314. (not (org-in-src-block-p)))
  5315. (let* ((hl (org-match-string-no-properties 1))
  5316. (help (concat "LINK: " (save-match-data (org-link-unescape hl))))
  5317. (ip (org-maybe-intangible
  5318. (list 'invisible 'org-link
  5319. 'keymap org-mouse-map 'mouse-face 'highlight
  5320. 'font-lock-multiline t 'help-echo help
  5321. 'htmlize-link `(:uri ,hl))))
  5322. (vp (list 'keymap org-mouse-map 'mouse-face 'highlight
  5323. 'font-lock-multiline t 'help-echo help
  5324. 'htmlize-link `(:uri ,hl))))
  5325. ;; We need to remove the invisible property here. Table narrowing
  5326. ;; may have made some of this invisible.
  5327. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5328. (remove-text-properties (match-beginning 0) (match-end 0)
  5329. '(invisible nil))
  5330. (if (match-end 3)
  5331. (progn
  5332. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  5333. (org-rear-nonsticky-at (match-beginning 3))
  5334. (add-text-properties (match-beginning 3) (match-end 3) vp)
  5335. (org-rear-nonsticky-at (match-end 3))
  5336. (add-text-properties (match-end 3) (match-end 0) ip)
  5337. (org-rear-nonsticky-at (match-end 0)))
  5338. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  5339. (org-rear-nonsticky-at (match-beginning 1))
  5340. (add-text-properties (match-beginning 1) (match-end 1) vp)
  5341. (org-rear-nonsticky-at (match-end 1))
  5342. (add-text-properties (match-end 1) (match-end 0) ip)
  5343. (org-rear-nonsticky-at (match-end 0)))
  5344. t)))
  5345. (defun org-activate-dates (limit)
  5346. "Add text properties for dates."
  5347. (if (and (re-search-forward org-tsr-regexp-both limit t)
  5348. (not (equal (char-before (match-beginning 0)) 91)))
  5349. (progn
  5350. (org-remove-flyspell-overlays-in (match-beginning 0) (match-end 0))
  5351. (add-text-properties (match-beginning 0) (match-end 0)
  5352. (list 'mouse-face 'highlight
  5353. 'keymap org-mouse-map))
  5354. (org-rear-nonsticky-at (match-end 0))
  5355. (when org-display-custom-times
  5356. (if (match-end 3)
  5357. (org-display-custom-time (match-beginning 3) (match-end 3)))
  5358. (org-display-custom-time (match-beginning 1) (match-end 1)))
  5359. t)))
  5360. (defvar org-target-link-regexp nil
  5361. "Regular expression matching radio targets in plain text.")
  5362. (make-variable-buffer-local 'org-target-link-regexp)
  5363. (defconst org-target-regexp (let ((border "[^<>\n\r \t]"))
  5364. (format "<<\\(%s\\|%s[^<>\n\r]*%s\\)>>"
  5365. border border border))
  5366. "Regular expression matching a link target.")
  5367. (defconst org-radio-target-regexp (format "<%s>" org-target-regexp)
  5368. "Regular expression matching a radio target.")
  5369. (defconst org-any-target-regexp
  5370. (format "%s\\|%s" org-radio-target-regexp org-target-regexp)
  5371. "Regular expression matching any target.")
  5372. (defun org-activate-target-links (limit)
  5373. "Add text properties for target matches."
  5374. (when org-target-link-regexp
  5375. (let ((case-fold-search t))
  5376. (if (re-search-forward org-target-link-regexp limit t)
  5377. (progn
  5378. (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
  5379. (add-text-properties (match-beginning 1) (match-end 1)
  5380. (list 'mouse-face 'highlight
  5381. 'keymap org-mouse-map
  5382. 'help-echo "Radio target link"
  5383. 'org-linked-text t))
  5384. (org-rear-nonsticky-at (match-end 1))
  5385. t)))))
  5386. (defun org-update-radio-target-regexp ()
  5387. "Find all radio targets in this file and update the regular expression.
  5388. Also refresh fontification if needed."
  5389. (interactive)
  5390. (let ((old-regexp org-target-link-regexp)
  5391. (before-re "\\(?:^\\|[^[:alnum:]]\\)\\(")
  5392. (after-re "\\)\\(?:$\\|[^[:alnum:]]\\)")
  5393. (targets
  5394. (org-with-wide-buffer
  5395. (goto-char (point-min))
  5396. (let (rtn)
  5397. (while (re-search-forward org-radio-target-regexp nil t)
  5398. ;; Make sure point is really within the object.
  5399. (backward-char)
  5400. (let ((obj (org-element-context)))
  5401. (when (eq (org-element-type obj) 'radio-target)
  5402. (add-to-list 'rtn (org-element-property :value obj)))))
  5403. rtn))))
  5404. (setq org-target-link-regexp
  5405. (and targets
  5406. (concat before-re
  5407. (mapconcat
  5408. (lambda (x)
  5409. (replace-regexp-in-string
  5410. " +" "\\s-+" (regexp-quote x) t t))
  5411. targets
  5412. "\\|")
  5413. after-re)))
  5414. (unless (equal old-regexp org-target-link-regexp)
  5415. ;; Clean-up cache.
  5416. (let ((regexp (cond ((not old-regexp) org-target-link-regexp)
  5417. ((not org-target-link-regexp) old-regexp)
  5418. (t
  5419. (concat before-re
  5420. (mapconcat
  5421. (lambda (re)
  5422. (substring re (length before-re)
  5423. (- (length after-re))))
  5424. (list old-regexp org-target-link-regexp)
  5425. "\\|")
  5426. after-re)))))
  5427. (org-with-wide-buffer
  5428. (goto-char (point-min))
  5429. (while (re-search-forward regexp nil t)
  5430. (org-element-cache-refresh (match-beginning 1)))))
  5431. ;; Re fontify buffer.
  5432. (when (memq 'radio org-highlight-links)
  5433. (org-restart-font-lock)))))
  5434. (defun org-hide-wide-columns (limit)
  5435. (let (s e)
  5436. (setq s (text-property-any (point) (or limit (point-max))
  5437. 'org-cwidth t))
  5438. (when s
  5439. (setq e (next-single-property-change s 'org-cwidth))
  5440. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  5441. (goto-char e)
  5442. t)))
  5443. (defvar org-latex-and-related-regexp nil
  5444. "Regular expression for highlighting LaTeX, entities and sub/superscript.")
  5445. (defun org-compute-latex-and-related-regexp ()
  5446. "Compute regular expression for LaTeX, entities and sub/superscript.
  5447. Result depends on variable `org-highlight-latex-and-related'."
  5448. (org-set-local
  5449. 'org-latex-and-related-regexp
  5450. (let* ((re-sub
  5451. (cond ((not (memq 'script org-highlight-latex-and-related)) nil)
  5452. ((eq org-use-sub-superscripts '{})
  5453. (list org-match-substring-with-braces-regexp))
  5454. (org-use-sub-superscripts (list org-match-substring-regexp))))
  5455. (re-latex
  5456. (when (memq 'latex org-highlight-latex-and-related)
  5457. (let ((matchers (plist-get org-format-latex-options :matchers)))
  5458. (delq nil
  5459. (mapcar (lambda (x)
  5460. (and (member (car x) matchers) (nth 1 x)))
  5461. org-latex-regexps)))))
  5462. (re-entities
  5463. (when (memq 'entities org-highlight-latex-and-related)
  5464. (list "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]]\\)"))))
  5465. (mapconcat 'identity (append re-latex re-entities re-sub) "\\|"))))
  5466. (defun org-do-latex-and-related (limit)
  5467. "Highlight LaTeX snippets and environments, entities and sub/superscript.
  5468. LIMIT bounds the search for syntax to highlight. Stop at first
  5469. highlighted object, if any. Return t if some highlighting was
  5470. done, nil otherwise."
  5471. (when (org-string-nw-p org-latex-and-related-regexp)
  5472. (catch 'found
  5473. (while (re-search-forward org-latex-and-related-regexp limit t)
  5474. (unless (memq (car-safe (get-text-property (1+ (match-beginning 0))
  5475. 'face))
  5476. '(org-code org-verbatim underline))
  5477. (let ((offset (if (memq (char-after (1+ (match-beginning 0)))
  5478. '(?_ ?^))
  5479. 1
  5480. 0)))
  5481. (font-lock-prepend-text-property
  5482. (+ offset (match-beginning 0)) (match-end 0)
  5483. 'face 'org-latex-and-related)
  5484. (add-text-properties (+ offset (match-beginning 0)) (match-end 0)
  5485. '(font-lock-multiline t)))
  5486. (throw 'found t)))
  5487. nil)))
  5488. (defun org-restart-font-lock ()
  5489. "Restart `font-lock-mode', to force refontification."
  5490. (when (and (boundp 'font-lock-mode) font-lock-mode)
  5491. (font-lock-mode -1)
  5492. (font-lock-mode 1)))
  5493. (defun org-activate-tags (limit)
  5494. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \r\n]") limit t)
  5495. (progn
  5496. (org-remove-flyspell-overlays-in (match-beginning 1) (match-end 1))
  5497. (add-text-properties (match-beginning 1) (match-end 1)
  5498. (list 'mouse-face 'highlight
  5499. 'keymap org-mouse-map))
  5500. (org-rear-nonsticky-at (match-end 1))
  5501. t)))
  5502. (defun org-outline-level ()
  5503. "Compute the outline level of the heading at point.
  5504. If this is called at a normal headline, the level is the number
  5505. of stars. Use `org-reduced-level' to remove the effect of
  5506. `org-odd-levels'. Unlike to `org-current-level', this function
  5507. takes into consideration inlinetasks."
  5508. (org-with-wide-buffer
  5509. (end-of-line)
  5510. (if (re-search-backward org-outline-regexp-bol nil t)
  5511. (1- (- (match-end 0) (match-beginning 0)))
  5512. 0)))
  5513. (defvar org-font-lock-keywords nil)
  5514. (defsubst org-re-property (property &optional literal allow-null value)
  5515. "Return a regexp matching a PROPERTY line.
  5516. When optional argument LITERAL is non-nil, do not quote PROPERTY.
  5517. This is useful when PROPERTY is a regexp. When ALLOW-NULL is
  5518. non-nil, match properties even without a value.
  5519. Match group 3 is set to the value when it exists. If there is no
  5520. value and ALLOW-NULL is non-nil, it is set to the empty string.
  5521. With optional argument VALUE, match only property lines with
  5522. that value; in this case, ALLOW-NULL is ignored. VALUE is quoted
  5523. unless LITERAL is non-nil."
  5524. (concat
  5525. "^\\(?4:[ \t]*\\)"
  5526. (format "\\(?1::\\(?2:%s\\):\\)"
  5527. (if literal property (regexp-quote property)))
  5528. (cond (value
  5529. (format "[ \t]+\\(?3:%s\\)\\(?5:[ \t]*\\)$"
  5530. (if literal value (regexp-quote value))))
  5531. (allow-null
  5532. "\\(?:\\(?3:$\\)\\|[ \t]+\\(?3:.*?\\)\\)\\(?5:[ \t]*\\)$")
  5533. (t
  5534. "[ \t]+\\(?3:[^ \r\t\n]+.*?\\)\\(?5:[ \t]*\\)$"))))
  5535. (defconst org-property-re
  5536. (org-re-property "\\S-+" 'literal t)
  5537. "Regular expression matching a property line.
  5538. There are four matching groups:
  5539. 1: :PROPKEY: including the leading and trailing colon,
  5540. 2: PROPKEY without the leading and trailing colon,
  5541. 3: PROPVAL without leading or trailing spaces,
  5542. 4: the indentation of the current line,
  5543. 5: trailing whitespace.")
  5544. (defvar org-font-lock-hook nil
  5545. "Functions to be called for special font lock stuff.")
  5546. (defvar org-font-lock-set-keywords-hook nil
  5547. "Functions that can manipulate `org-font-lock-extra-keywords'.
  5548. This is called after `org-font-lock-extra-keywords' is defined, but before
  5549. it is installed to be used by font lock. This can be useful if something
  5550. needs to be inserted at a specific position in the font-lock sequence.")
  5551. (defun org-font-lock-hook (limit)
  5552. "Run `org-font-lock-hook' within LIMIT."
  5553. (run-hook-with-args 'org-font-lock-hook limit))
  5554. (defun org-set-font-lock-defaults ()
  5555. "Set font lock defaults for the current buffer."
  5556. (let* ((em org-fontify-emphasized-text)
  5557. (lk org-highlight-links)
  5558. (org-font-lock-extra-keywords
  5559. (list
  5560. ;; Call the hook
  5561. '(org-font-lock-hook)
  5562. ;; Headlines
  5563. `(,(if org-fontify-whole-heading-line
  5564. "^\\(\\**\\)\\(\\* \\)\\(.*\n?\\)"
  5565. "^\\(\\**\\)\\(\\* \\)\\(.*\\)")
  5566. (1 (org-get-level-face 1))
  5567. (2 (org-get-level-face 2))
  5568. (3 (org-get-level-face 3)))
  5569. ;; Table lines
  5570. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  5571. (1 'org-table t))
  5572. ;; Table internals
  5573. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  5574. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  5575. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  5576. '("| *\\(<[lrc]?[0-9]*>\\)" (1 'org-formula t))
  5577. ;; Drawers
  5578. '(org-fontify-drawers)
  5579. ;; Properties
  5580. (list org-property-re
  5581. '(1 'org-special-keyword t)
  5582. '(3 'org-property-value t))
  5583. ;; Links
  5584. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  5585. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  5586. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  5587. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  5588. (if (memq 'radio lk) '(org-activate-target-links (1 'org-link t)))
  5589. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  5590. (if (memq 'footnote lk) '(org-activate-footnote-links))
  5591. ;; Targets.
  5592. (list org-any-target-regexp '(0 'org-target t))
  5593. ;; Diary sexps.
  5594. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  5595. ;; Macro
  5596. '(org-fontify-macros)
  5597. '(org-hide-wide-columns (0 nil append))
  5598. ;; TODO keyword
  5599. (list (format org-heading-keyword-regexp-format
  5600. org-todo-regexp)
  5601. '(2 (org-get-todo-face 2) t))
  5602. ;; DONE
  5603. (if org-fontify-done-headline
  5604. (list (format org-heading-keyword-regexp-format
  5605. (concat
  5606. "\\(?:"
  5607. (mapconcat 'regexp-quote org-done-keywords "\\|")
  5608. "\\)"))
  5609. '(2 'org-headline-done t))
  5610. nil)
  5611. ;; Priorities
  5612. '(org-font-lock-add-priority-faces)
  5613. ;; Tags
  5614. '(org-font-lock-add-tag-faces)
  5615. ;; Tags groups
  5616. (if (and org-group-tags org-tag-groups-alist)
  5617. (list (concat org-outline-regexp-bol ".+\\(:"
  5618. (regexp-opt (mapcar 'car org-tag-groups-alist))
  5619. ":\\).*$")
  5620. '(1 'org-tag-group prepend)))
  5621. ;; Special keywords
  5622. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  5623. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  5624. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  5625. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  5626. ;; Emphasis
  5627. (if em
  5628. (if (featurep 'xemacs)
  5629. '(org-do-emphasis-faces (0 nil append))
  5630. '(org-do-emphasis-faces)))
  5631. ;; Checkboxes
  5632. '("^[ \t]*\\(?:[-+*]\\|[0-9]+[.)]\\)[ \t]+\\(?:\\[@\\(?:start:\\)?[0-9]+\\][ \t]*\\)?\\(\\[[- X]\\]\\)"
  5633. 1 'org-checkbox prepend)
  5634. (if (cdr (assq 'checkbox org-list-automatic-rules))
  5635. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  5636. (0 (org-get-checkbox-statistics-face) t)))
  5637. ;; Description list items
  5638. '("^[ \t]*[-+*][ \t]+\\(.*?[ \t]+::\\)\\([ \t]+\\|$\\)"
  5639. 1 'org-list-dt prepend)
  5640. ;; ARCHIVEd headings
  5641. (list (concat
  5642. org-outline-regexp-bol
  5643. "\\(.*:" org-archive-tag ":.*\\)")
  5644. '(1 'org-archived prepend))
  5645. ;; Specials
  5646. '(org-do-latex-and-related)
  5647. '(org-fontify-entities)
  5648. '(org-raise-scripts)
  5649. ;; Code
  5650. '(org-activate-code (1 'org-code t))
  5651. ;; COMMENT
  5652. (list (format
  5653. "^\\*\\(?: +%s\\)?\\(?: +\\[#[A-Z0-9]\\]\\)? +\\(?9:%s\\)\\(?: \\|$\\)"
  5654. org-todo-regexp
  5655. org-comment-string)
  5656. '(9 'org-special-keyword t))
  5657. ;; Blocks and meta lines
  5658. '(org-fontify-meta-lines-and-blocks))))
  5659. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  5660. (run-hooks 'org-font-lock-set-keywords-hook)
  5661. ;; Now set the full font-lock-keywords
  5662. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  5663. (org-set-local 'font-lock-defaults
  5664. '(org-font-lock-keywords t nil nil backward-paragraph))
  5665. (kill-local-variable 'font-lock-keywords) nil))
  5666. (defun org-toggle-pretty-entities ()
  5667. "Toggle the composition display of entities as UTF8 characters."
  5668. (interactive)
  5669. (org-set-local 'org-pretty-entities (not org-pretty-entities))
  5670. (org-restart-font-lock)
  5671. (if org-pretty-entities
  5672. (message "Entities are now displayed as UTF8 characters")
  5673. (save-restriction
  5674. (widen)
  5675. (org-decompose-region (point-min) (point-max))
  5676. (message "Entities are now displayed as plain text"))))
  5677. (defvar org-custom-properties-overlays nil
  5678. "List of overlays used for custom properties.")
  5679. (make-variable-buffer-local 'org-custom-properties-overlays)
  5680. (defun org-toggle-custom-properties-visibility ()
  5681. "Display or hide properties in `org-custom-properties'."
  5682. (interactive)
  5683. (if org-custom-properties-overlays
  5684. (progn (mapc #'delete-overlay org-custom-properties-overlays)
  5685. (setq org-custom-properties-overlays nil))
  5686. (when org-custom-properties
  5687. (org-with-wide-buffer
  5688. (goto-char (point-min))
  5689. (let ((regexp (org-re-property (regexp-opt org-custom-properties) t t)))
  5690. (while (re-search-forward regexp nil t)
  5691. (let ((end (cdr (save-match-data (org-get-property-block)))))
  5692. (when (and end (< (point) end))
  5693. ;; Hide first custom property in current drawer.
  5694. (let ((o (make-overlay (match-beginning 0) (1+ (match-end 0)))))
  5695. (overlay-put o 'invisible t)
  5696. (overlay-put o 'org-custom-property t)
  5697. (push o org-custom-properties-overlays))
  5698. ;; Hide additional custom properties in the same drawer.
  5699. (while (re-search-forward regexp end t)
  5700. (let ((o (make-overlay (match-beginning 0) (1+ (match-end 0)))))
  5701. (overlay-put o 'invisible t)
  5702. (overlay-put o 'org-custom-property t)
  5703. (push o org-custom-properties-overlays)))))
  5704. ;; Each entry is limited to a single property drawer.
  5705. (outline-next-heading)))))))
  5706. (defun org-fontify-entities (limit)
  5707. "Find an entity to fontify."
  5708. (let (ee)
  5709. (when org-pretty-entities
  5710. (catch 'match
  5711. ;; "\_ "-family is left out on purpose. Only the first one,
  5712. ;; i.e., "\_ ", could be fontified anyway, and it would be
  5713. ;; confusing when adding a second white space character.
  5714. (while (re-search-forward
  5715. "\\\\\\(there4\\|sup[123]\\|frac[13][24]\\|[a-zA-Z]+\\)\\($\\|{}\\|[^[:alpha:]\n]\\)"
  5716. limit t)
  5717. (if (and (not (org-at-comment-p))
  5718. (setq ee (org-entity-get (match-string 1)))
  5719. (= (length (nth 6 ee)) 1))
  5720. (let*
  5721. ((end (if (equal (match-string 2) "{}")
  5722. (match-end 2)
  5723. (match-end 1))))
  5724. (add-text-properties
  5725. (match-beginning 0) end
  5726. (list 'font-lock-fontified t))
  5727. (compose-region (match-beginning 0) end
  5728. (nth 6 ee) nil)
  5729. (backward-char 1)
  5730. (throw 'match t))))
  5731. nil))))
  5732. (defun org-fontify-like-in-org-mode (s &optional odd-levels)
  5733. "Fontify string S like in Org-mode."
  5734. (with-temp-buffer
  5735. (insert s)
  5736. (let ((org-odd-levels-only odd-levels))
  5737. (org-mode)
  5738. (font-lock-ensure)
  5739. (buffer-string))))
  5740. (defvar org-m nil)
  5741. (defvar org-l nil)
  5742. (defvar org-f nil)
  5743. (defun org-get-level-face (n)
  5744. "Get the right face for match N in font-lock matching of headlines."
  5745. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  5746. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  5747. (if org-cycle-level-faces
  5748. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  5749. (setq org-f (nth (1- (min org-l org-n-level-faces)) org-level-faces)))
  5750. (cond
  5751. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  5752. ((eq n 2) org-f)
  5753. (t (if org-level-color-stars-only nil org-f))))
  5754. (defun org-face-from-face-or-color (context inherit face-or-color)
  5755. "Create a face list that inherits INHERIT, but sets the foreground color.
  5756. When FACE-OR-COLOR is not a string, just return it."
  5757. (if (stringp face-or-color)
  5758. (list :inherit inherit
  5759. (cdr (assoc context org-faces-easy-properties))
  5760. face-or-color)
  5761. face-or-color))
  5762. (defun org-get-todo-face (kwd)
  5763. "Get the right face for a TODO keyword KWD.
  5764. If KWD is a number, get the corresponding match group."
  5765. (if (numberp kwd) (setq kwd (match-string kwd)))
  5766. (or (org-face-from-face-or-color
  5767. 'todo 'org-todo (cdr (assoc kwd org-todo-keyword-faces)))
  5768. (and (member kwd org-done-keywords) 'org-done)
  5769. 'org-todo))
  5770. (defun org-get-priority-face (priority)
  5771. "Get the right face for PRIORITY.
  5772. PRIORITY is a character."
  5773. (or (org-face-from-face-or-color
  5774. 'priority 'org-priority (cdr (assq priority org-priority-faces)))
  5775. 'org-priority))
  5776. (defun org-get-tag-face (tag)
  5777. "Get the right face for TAG.
  5778. If TAG is a number, get the corresponding match group."
  5779. (let ((tag (if (wholenump tag) (match-string tag) tag)))
  5780. (or (org-face-from-face-or-color
  5781. 'tag 'org-tag (cdr (assoc tag org-tag-faces)))
  5782. 'org-tag)))
  5783. (defun org-font-lock-add-priority-faces (limit)
  5784. "Add the special priority faces."
  5785. (while (re-search-forward "^\\*+ .*?\\(\\[#\\(.\\)\\]\\)" limit t)
  5786. (add-text-properties
  5787. (match-beginning 1) (match-end 1)
  5788. (list 'face (org-get-priority-face (string-to-char (match-string 2)))
  5789. 'font-lock-fontified t))))
  5790. (defun org-font-lock-add-tag-faces (limit)
  5791. "Add the special tag faces."
  5792. (when (and org-tag-faces org-tags-special-faces-re)
  5793. (while (re-search-forward org-tags-special-faces-re limit t)
  5794. (add-text-properties (match-beginning 1) (match-end 1)
  5795. (list 'face (org-get-tag-face 1)
  5796. 'font-lock-fontified t))
  5797. (backward-char 1))))
  5798. (defun org-unfontify-region (beg end &optional maybe_loudly)
  5799. "Remove fontification and activation overlays from links."
  5800. (font-lock-default-unfontify-region beg end)
  5801. (let* ((buffer-undo-list t)
  5802. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  5803. (inhibit-modification-hooks t)
  5804. deactivate-mark buffer-file-name buffer-file-truename)
  5805. (org-decompose-region beg end)
  5806. (remove-text-properties beg end
  5807. '(mouse-face t keymap t org-linked-text t
  5808. invisible t intangible t
  5809. org-emphasis t))
  5810. (org-remove-font-lock-display-properties beg end)))
  5811. (defconst org-script-display '(((raise -0.3) (height 0.7))
  5812. ((raise 0.3) (height 0.7))
  5813. ((raise -0.5))
  5814. ((raise 0.5)))
  5815. "Display properties for showing superscripts and subscripts.")
  5816. (defun org-remove-font-lock-display-properties (beg end)
  5817. "Remove specific display properties that have been added by font lock.
  5818. The will remove the raise properties that are used to show superscripts
  5819. and subscripts."
  5820. (let (next prop)
  5821. (while (< beg end)
  5822. (setq next (next-single-property-change beg 'display nil end)
  5823. prop (get-text-property beg 'display))
  5824. (if (member prop org-script-display)
  5825. (put-text-property beg next 'display nil))
  5826. (setq beg next))))
  5827. (defun org-raise-scripts (limit)
  5828. "Add raise properties to sub/superscripts."
  5829. (when (and org-pretty-entities org-pretty-entities-include-sub-superscripts)
  5830. (if (re-search-forward
  5831. (if (eq org-use-sub-superscripts t)
  5832. org-match-substring-regexp
  5833. org-match-substring-with-braces-regexp)
  5834. limit t)
  5835. (let* ((pos (point)) table-p comment-p
  5836. (mpos (match-beginning 3))
  5837. (emph-p (get-text-property mpos 'org-emphasis))
  5838. (link-p (get-text-property mpos 'mouse-face))
  5839. (keyw-p (eq 'org-special-keyword (get-text-property mpos 'face))))
  5840. (goto-char (point-at-bol))
  5841. (setq table-p (org-looking-at-p org-table-dataline-regexp)
  5842. comment-p (org-looking-at-p "^[ \t]*#[ +]"))
  5843. (goto-char pos)
  5844. ;; Handle a_b^c
  5845. (if (member (char-after) '(?_ ?^)) (goto-char (1- pos)))
  5846. (if (or comment-p emph-p link-p keyw-p)
  5847. t
  5848. (put-text-property (match-beginning 3) (match-end 0)
  5849. 'display
  5850. (if (equal (char-after (match-beginning 2)) ?^)
  5851. (nth (if table-p 3 1) org-script-display)
  5852. (nth (if table-p 2 0) org-script-display)))
  5853. (add-text-properties (match-beginning 2) (match-end 2)
  5854. (list 'invisible t
  5855. 'org-dwidth t 'org-dwidth-n 1))
  5856. (if (and (eq (char-after (match-beginning 3)) ?{)
  5857. (eq (char-before (match-end 3)) ?}))
  5858. (progn
  5859. (add-text-properties
  5860. (match-beginning 3) (1+ (match-beginning 3))
  5861. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))
  5862. (add-text-properties
  5863. (1- (match-end 3)) (match-end 3)
  5864. (list 'invisible t 'org-dwidth t 'org-dwidth-n 1))))
  5865. t)))))
  5866. ;;;; Visibility cycling, including org-goto and indirect buffer
  5867. ;;; Cycling
  5868. (defvar org-cycle-global-status nil)
  5869. (make-variable-buffer-local 'org-cycle-global-status)
  5870. (put 'org-cycle-global-status 'org-state t)
  5871. (defvar org-cycle-subtree-status nil)
  5872. (make-variable-buffer-local 'org-cycle-subtree-status)
  5873. (put 'org-cycle-subtree-status 'org-state t)
  5874. (defvar org-inlinetask-min-level)
  5875. (defun org-unlogged-message (&rest args)
  5876. "Display a message, but avoid logging it in the *Messages* buffer."
  5877. (let ((message-log-max nil))
  5878. (apply 'message args)))
  5879. ;;;###autoload
  5880. (defun org-cycle (&optional arg)
  5881. "TAB-action and visibility cycling for Org-mode.
  5882. This is the command invoked in Org-mode by the TAB key. Its main purpose
  5883. is outline visibility cycling, but it also invokes other actions
  5884. in special contexts.
  5885. - When this function is called with a prefix argument, rotate the entire
  5886. buffer through 3 states (global cycling)
  5887. 1. OVERVIEW: Show only top-level headlines.
  5888. 2. CONTENTS: Show all headlines of all levels, but no body text.
  5889. 3. SHOW ALL: Show everything.
  5890. When called with two `C-u C-u' prefixes, switch to the startup visibility,
  5891. determined by the variable `org-startup-folded', and by any VISIBILITY
  5892. properties in the buffer.
  5893. When called with three `C-u C-u C-u' prefixed, show the entire buffer,
  5894. including any drawers.
  5895. - When inside a table, re-align the table and move to the next field.
  5896. - When point is at the beginning of a headline, rotate the subtree started
  5897. by this line through 3 different states (local cycling)
  5898. 1. FOLDED: Only the main headline is shown.
  5899. 2. CHILDREN: The main headline and the direct children are shown.
  5900. From this state, you can move to one of the children
  5901. and zoom in further.
  5902. 3. SUBTREE: Show the entire subtree, including body text.
  5903. If there is no subtree, switch directly from CHILDREN to FOLDED.
  5904. - When point is at the beginning of an empty headline and the variable
  5905. `org-cycle-level-after-item/entry-creation' is set, cycle the level
  5906. of the headline by demoting and promoting it to likely levels. This
  5907. speeds up creation document structure by pressing TAB once or several
  5908. times right after creating a new headline.
  5909. - When there is a numeric prefix, go up to a heading with level ARG, do
  5910. a `show-subtree' and return to the previous cursor position. If ARG
  5911. is negative, go up that many levels.
  5912. - When point is not at the beginning of a headline, execute the global
  5913. binding for TAB, which is re-indenting the line. See the option
  5914. `org-cycle-emulate-tab' for details.
  5915. - Special case: if point is at the beginning of the buffer and there is
  5916. no headline in line 1, this function will act as if called with prefix arg
  5917. (C-u TAB, same as S-TAB) also when called without prefix arg.
  5918. But only if also the variable `org-cycle-global-at-bob' is t."
  5919. (interactive "P")
  5920. (org-load-modules-maybe)
  5921. (unless (or (run-hook-with-args-until-success 'org-tab-first-hook)
  5922. (and org-cycle-level-after-item/entry-creation
  5923. (or (org-cycle-level)
  5924. (org-cycle-item-indentation))))
  5925. (let* ((limit-level
  5926. (or org-cycle-max-level
  5927. (and (boundp 'org-inlinetask-min-level)
  5928. org-inlinetask-min-level
  5929. (1- org-inlinetask-min-level))))
  5930. (nstars (and limit-level
  5931. (if org-odd-levels-only
  5932. (and limit-level (1- (* limit-level 2)))
  5933. limit-level)))
  5934. (org-outline-regexp
  5935. (if (not (derived-mode-p 'org-mode))
  5936. outline-regexp
  5937. (concat "\\*" (if nstars (format "\\{1,%d\\} " nstars) "+ "))))
  5938. (bob-special (and org-cycle-global-at-bob (not arg) (bobp)
  5939. (not (looking-at org-outline-regexp))))
  5940. (org-cycle-hook
  5941. (if bob-special
  5942. (delq 'org-optimize-window-after-visibility-change
  5943. (copy-sequence org-cycle-hook))
  5944. org-cycle-hook))
  5945. (pos (point)))
  5946. (if (or bob-special (equal arg '(4)))
  5947. ;; special case: use global cycling
  5948. (setq arg t))
  5949. (cond
  5950. ((equal arg '(16))
  5951. (setq last-command 'dummy)
  5952. (org-set-startup-visibility)
  5953. (org-unlogged-message "Startup visibility, plus VISIBILITY properties"))
  5954. ((equal arg '(64))
  5955. (show-all)
  5956. (org-unlogged-message "Entire buffer visible, including drawers"))
  5957. ;; Try cdlatex TAB completion
  5958. ((org-try-cdlatex-tab))
  5959. ;; Table: enter it or move to the next field.
  5960. ((org-at-table-p 'any)
  5961. (if (org-at-table.el-p)
  5962. (message "Use C-c ' to edit table.el tables")
  5963. (if arg (org-table-edit-field t)
  5964. (org-table-justify-field-maybe)
  5965. (call-interactively 'org-table-next-field))))
  5966. ((run-hook-with-args-until-success
  5967. 'org-tab-after-check-for-table-hook))
  5968. ;; Global cycling: delegate to `org-cycle-internal-global'.
  5969. ((eq arg t) (org-cycle-internal-global))
  5970. ;; Drawers: delegate to `org-flag-drawer'.
  5971. ((save-excursion
  5972. (beginning-of-line 1)
  5973. (looking-at org-drawer-regexp))
  5974. (org-flag-drawer ; toggle block visibility
  5975. (not (get-char-property (match-end 0) 'invisible))))
  5976. ;; Show-subtree, ARG levels up from here.
  5977. ((integerp arg)
  5978. (save-excursion
  5979. (org-back-to-heading)
  5980. (outline-up-heading (if (< arg 0) (- arg)
  5981. (- (funcall outline-level) arg)))
  5982. (org-show-subtree)))
  5983. ;; Inline task: delegate to `org-inlinetask-toggle-visibility'.
  5984. ((and (featurep 'org-inlinetask)
  5985. (org-inlinetask-at-task-p)
  5986. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5987. (org-inlinetask-toggle-visibility))
  5988. ;; At an item/headline: delegate to `org-cycle-internal-local'.
  5989. ((and (or (and org-cycle-include-plain-lists (org-at-item-p))
  5990. (save-excursion (move-beginning-of-line 1)
  5991. (looking-at org-outline-regexp)))
  5992. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  5993. (org-cycle-internal-local))
  5994. ;; From there: TAB emulation and template completion.
  5995. (buffer-read-only (org-back-to-heading))
  5996. ((run-hook-with-args-until-success
  5997. 'org-tab-after-check-for-cycling-hook))
  5998. ((org-try-structure-completion))
  5999. ((run-hook-with-args-until-success
  6000. 'org-tab-before-tab-emulation-hook))
  6001. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  6002. (or (not (bolp))
  6003. (not (looking-at org-outline-regexp))))
  6004. (call-interactively (global-key-binding "\t")))
  6005. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  6006. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  6007. (or (and (eq org-cycle-emulate-tab 'white)
  6008. (= (match-end 0) (point-at-eol)))
  6009. (and (eq org-cycle-emulate-tab 'whitestart)
  6010. (>= (match-end 0) pos))))
  6011. t
  6012. (eq org-cycle-emulate-tab t))
  6013. (call-interactively (global-key-binding "\t")))
  6014. (t (save-excursion
  6015. (org-back-to-heading)
  6016. (org-cycle)))))))
  6017. (defun org-cycle-internal-global ()
  6018. "Do the global cycling action."
  6019. ;; Hack to avoid display of messages for .org attachments in Gnus
  6020. (let ((ga (string-match "\\*fontification" (buffer-name))))
  6021. (cond
  6022. ((and (eq last-command this-command)
  6023. (eq org-cycle-global-status 'overview))
  6024. ;; We just created the overview - now do table of contents
  6025. ;; This can be slow in very large buffers, so indicate action
  6026. (run-hook-with-args 'org-pre-cycle-hook 'contents)
  6027. (unless ga (org-unlogged-message "CONTENTS..."))
  6028. (org-content)
  6029. (unless ga (org-unlogged-message "CONTENTS...done"))
  6030. (setq org-cycle-global-status 'contents)
  6031. (run-hook-with-args 'org-cycle-hook 'contents))
  6032. ((and (eq last-command this-command)
  6033. (eq org-cycle-global-status 'contents))
  6034. ;; We just showed the table of contents - now show everything
  6035. (run-hook-with-args 'org-pre-cycle-hook 'all)
  6036. (show-all)
  6037. (unless ga (org-unlogged-message "SHOW ALL"))
  6038. (setq org-cycle-global-status 'all)
  6039. (run-hook-with-args 'org-cycle-hook 'all))
  6040. (t
  6041. ;; Default action: go to overview
  6042. (run-hook-with-args 'org-pre-cycle-hook 'overview)
  6043. (org-overview)
  6044. (unless ga (org-unlogged-message "OVERVIEW"))
  6045. (setq org-cycle-global-status 'overview)
  6046. (run-hook-with-args 'org-cycle-hook 'overview)))))
  6047. (defvar org-called-with-limited-levels nil
  6048. "Non-nil when `org-with-limited-levels' is currently active.")
  6049. (defun org-cycle-internal-local ()
  6050. "Do the local cycling action."
  6051. (let ((goal-column 0) eoh eol eos has-children children-skipped struct)
  6052. ;; First, determine end of headline (EOH), end of subtree or item
  6053. ;; (EOS), and if item or heading has children (HAS-CHILDREN).
  6054. (save-excursion
  6055. (if (org-at-item-p)
  6056. (progn
  6057. (beginning-of-line)
  6058. (setq struct (org-list-struct))
  6059. (setq eoh (point-at-eol))
  6060. (setq eos (org-list-get-item-end-before-blank (point) struct))
  6061. (setq has-children (org-list-has-child-p (point) struct)))
  6062. (org-back-to-heading)
  6063. (setq eoh (save-excursion (outline-end-of-heading) (point)))
  6064. (setq eos (save-excursion (org-end-of-subtree t t)
  6065. (when (bolp) (backward-char)) (point)))
  6066. (setq has-children
  6067. (or (save-excursion
  6068. (let ((level (funcall outline-level)))
  6069. (outline-next-heading)
  6070. (and (org-at-heading-p t)
  6071. (> (funcall outline-level) level))))
  6072. (save-excursion
  6073. (org-list-search-forward (org-item-beginning-re) eos t)))))
  6074. ;; Determine end invisible part of buffer (EOL)
  6075. (beginning-of-line 2)
  6076. ;; XEmacs doesn't have `next-single-char-property-change'
  6077. (if (featurep 'xemacs)
  6078. (while (and (not (eobp)) ;; this is like `next-line'
  6079. (get-char-property (1- (point)) 'invisible))
  6080. (beginning-of-line 2))
  6081. (while (and (not (eobp)) ;; this is like `next-line'
  6082. (get-char-property (1- (point)) 'invisible))
  6083. (goto-char (next-single-char-property-change (point) 'invisible))
  6084. (and (eolp) (beginning-of-line 2))))
  6085. (setq eol (point)))
  6086. ;; Find out what to do next and set `this-command'
  6087. (cond
  6088. ((= eos eoh)
  6089. ;; Nothing is hidden behind this heading
  6090. (unless (org-before-first-heading-p)
  6091. (run-hook-with-args 'org-pre-cycle-hook 'empty))
  6092. (org-unlogged-message "EMPTY ENTRY")
  6093. (setq org-cycle-subtree-status nil)
  6094. (save-excursion
  6095. (goto-char eos)
  6096. (outline-next-heading)
  6097. (if (outline-invisible-p) (org-flag-heading nil))))
  6098. ((and (or (>= eol eos)
  6099. (not (string-match "\\S-" (buffer-substring eol eos))))
  6100. (or has-children
  6101. (not (setq children-skipped
  6102. org-cycle-skip-children-state-if-no-children))))
  6103. ;; Entire subtree is hidden in one line: children view
  6104. (unless (org-before-first-heading-p)
  6105. (run-hook-with-args 'org-pre-cycle-hook 'children))
  6106. (if (org-at-item-p)
  6107. (org-list-set-item-visibility (point-at-bol) struct 'children)
  6108. (org-show-entry)
  6109. (org-with-limited-levels (org-show-children))
  6110. ;; FIXME: This slows down the func way too much.
  6111. ;; How keep drawers hidden in subtree anyway?
  6112. ;; (when (memq 'org-cycle-hide-drawers org-cycle-hook)
  6113. ;; (org-cycle-hide-drawers 'subtree))
  6114. ;; Fold every list in subtree to top-level items.
  6115. (when (eq org-cycle-include-plain-lists 'integrate)
  6116. (save-excursion
  6117. (org-back-to-heading)
  6118. (while (org-list-search-forward (org-item-beginning-re) eos t)
  6119. (beginning-of-line 1)
  6120. (let* ((struct (org-list-struct))
  6121. (prevs (org-list-prevs-alist struct))
  6122. (end (org-list-get-bottom-point struct)))
  6123. (mapc (lambda (e) (org-list-set-item-visibility e struct 'folded))
  6124. (org-list-get-all-items (point) struct prevs))
  6125. (goto-char (if (< end eos) end eos)))))))
  6126. (org-unlogged-message "CHILDREN")
  6127. (save-excursion
  6128. (goto-char eos)
  6129. (outline-next-heading)
  6130. (if (outline-invisible-p) (org-flag-heading nil)))
  6131. (setq org-cycle-subtree-status 'children)
  6132. (unless (org-before-first-heading-p)
  6133. (run-hook-with-args 'org-cycle-hook 'children)))
  6134. ((or children-skipped
  6135. (and (eq last-command this-command)
  6136. (eq org-cycle-subtree-status 'children)))
  6137. ;; We just showed the children, or no children are there,
  6138. ;; now show everything.
  6139. (unless (org-before-first-heading-p)
  6140. (run-hook-with-args 'org-pre-cycle-hook 'subtree))
  6141. (outline-flag-region eoh eos nil)
  6142. (org-unlogged-message
  6143. (if children-skipped "SUBTREE (NO CHILDREN)" "SUBTREE"))
  6144. (setq org-cycle-subtree-status 'subtree)
  6145. (unless (org-before-first-heading-p)
  6146. (run-hook-with-args 'org-cycle-hook 'subtree)))
  6147. (t
  6148. ;; Default action: hide the subtree.
  6149. (run-hook-with-args 'org-pre-cycle-hook 'folded)
  6150. (outline-flag-region eoh eos t)
  6151. (org-unlogged-message "FOLDED")
  6152. (setq org-cycle-subtree-status 'folded)
  6153. (unless (org-before-first-heading-p)
  6154. (run-hook-with-args 'org-cycle-hook 'folded))))))
  6155. ;;;###autoload
  6156. (defun org-global-cycle (&optional arg)
  6157. "Cycle the global visibility. For details see `org-cycle'.
  6158. With \\[universal-argument] prefix arg, switch to startup visibility.
  6159. With a numeric prefix, show all headlines up to that level."
  6160. (interactive "P")
  6161. (let ((org-cycle-include-plain-lists
  6162. (if (derived-mode-p 'org-mode) org-cycle-include-plain-lists nil)))
  6163. (cond
  6164. ((integerp arg)
  6165. (show-all)
  6166. (hide-sublevels arg)
  6167. (setq org-cycle-global-status 'contents))
  6168. ((equal arg '(4))
  6169. (org-set-startup-visibility)
  6170. (org-unlogged-message "Startup visibility, plus VISIBILITY properties."))
  6171. (t
  6172. (org-cycle '(4))))))
  6173. (defun org-set-startup-visibility ()
  6174. "Set the visibility required by startup options and properties."
  6175. (cond
  6176. ((eq org-startup-folded t)
  6177. (org-overview))
  6178. ((eq org-startup-folded 'content)
  6179. (org-content))
  6180. ((or (eq org-startup-folded 'showeverything)
  6181. (eq org-startup-folded nil))
  6182. (show-all)))
  6183. (unless (eq org-startup-folded 'showeverything)
  6184. (if org-hide-block-startup (org-hide-block-all))
  6185. (org-set-visibility-according-to-property 'no-cleanup)
  6186. (org-cycle-hide-archived-subtrees 'all)
  6187. (org-cycle-hide-drawers 'all)
  6188. (org-cycle-show-empty-lines t)))
  6189. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  6190. "Switch subtree visibilities according to :VISIBILITY: property."
  6191. (interactive)
  6192. (let (org-show-entry-below)
  6193. (org-with-wide-buffer
  6194. (goto-char (point-min))
  6195. (while (re-search-forward "^[ \t]*:VISIBILITY:" nil t)
  6196. (if (not (org-at-property-p)) (outline-next-heading)
  6197. (let ((state (match-string 3)))
  6198. (save-excursion
  6199. (org-back-to-heading t)
  6200. (hide-subtree)
  6201. (org-reveal)
  6202. (cond
  6203. ((equal state "folded")
  6204. (hide-subtree))
  6205. ((equal state "children")
  6206. (org-show-hidden-entry)
  6207. (org-show-children))
  6208. ((equal state "content")
  6209. (save-excursion
  6210. (save-restriction
  6211. (org-narrow-to-subtree)
  6212. (org-content))))
  6213. ((member state '("all" "showall"))
  6214. (show-subtree)))))))
  6215. (unless no-cleanup
  6216. (org-cycle-hide-archived-subtrees 'all)
  6217. (org-cycle-hide-drawers 'all)
  6218. (org-cycle-show-empty-lines 'all)))))
  6219. ;; This function uses outline-regexp instead of the more fundamental
  6220. ;; org-outline-regexp so that org-cycle-global works outside of Org
  6221. ;; buffers, where outline-regexp is needed.
  6222. (defun org-overview ()
  6223. "Switch to overview mode, showing only top-level headlines.
  6224. This shows all headlines with a level equal or greater than the level
  6225. of the first headline in the buffer. This is important, because if the
  6226. first headline is not level one, then (hide-sublevels 1) gives confusing
  6227. results."
  6228. (interactive)
  6229. (save-excursion
  6230. (let ((level
  6231. (save-excursion
  6232. (goto-char (point-min))
  6233. (if (re-search-forward (concat "^" outline-regexp) nil t)
  6234. (progn
  6235. (goto-char (match-beginning 0))
  6236. (funcall outline-level))))))
  6237. (and level (hide-sublevels level)))))
  6238. (defun org-content (&optional arg)
  6239. "Show all headlines in the buffer, like a table of contents.
  6240. With numerical argument N, show content up to level N."
  6241. (interactive "P")
  6242. (org-overview)
  6243. (save-excursion
  6244. ;; Visit all headings and show their offspring
  6245. (and (integerp arg) (org-overview))
  6246. (goto-char (point-max))
  6247. (catch 'exit
  6248. (while (and (progn (condition-case nil
  6249. (outline-previous-visible-heading 1)
  6250. (error (goto-char (point-min))))
  6251. t)
  6252. (looking-at org-outline-regexp))
  6253. (if (integerp arg)
  6254. (org-show-children (1- arg))
  6255. (show-branches))
  6256. (if (bobp) (throw 'exit nil))))))
  6257. (defun org-optimize-window-after-visibility-change (state)
  6258. "Adjust the window after a change in outline visibility.
  6259. This function is the default value of the hook `org-cycle-hook'."
  6260. (when (get-buffer-window (current-buffer))
  6261. (cond
  6262. ((eq state 'content) nil)
  6263. ((eq state 'all) nil)
  6264. ((eq state 'folded) nil)
  6265. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  6266. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  6267. (defun org-remove-empty-overlays-at (pos)
  6268. "Remove outline overlays that do not contain non-white stuff."
  6269. (mapc
  6270. (lambda (o)
  6271. (and (eq 'outline (overlay-get o 'invisible))
  6272. (not (string-match "\\S-" (buffer-substring (overlay-start o)
  6273. (overlay-end o))))
  6274. (delete-overlay o)))
  6275. (overlays-at pos)))
  6276. (defun org-clean-visibility-after-subtree-move ()
  6277. "Fix visibility issues after moving a subtree."
  6278. ;; First, find a reasonable region to look at:
  6279. ;; Start two siblings above, end three below
  6280. (let* ((beg (save-excursion
  6281. (and (org-get-last-sibling)
  6282. (org-get-last-sibling))
  6283. (point)))
  6284. (end (save-excursion
  6285. (and (org-get-next-sibling)
  6286. (org-get-next-sibling)
  6287. (org-get-next-sibling))
  6288. (if (org-at-heading-p)
  6289. (point-at-eol)
  6290. (point))))
  6291. (level (looking-at "\\*+"))
  6292. (re (if level (concat "^" (regexp-quote (match-string 0)) " "))))
  6293. (save-excursion
  6294. (save-restriction
  6295. (narrow-to-region beg end)
  6296. (when re
  6297. ;; Properly fold already folded siblings
  6298. (goto-char (point-min))
  6299. (while (re-search-forward re nil t)
  6300. (if (and (not (outline-invisible-p))
  6301. (save-excursion
  6302. (goto-char (point-at-eol)) (outline-invisible-p)))
  6303. (hide-entry))))
  6304. (org-cycle-show-empty-lines 'overview)
  6305. (org-cycle-hide-drawers 'overview)))))
  6306. (defun org-cycle-show-empty-lines (state)
  6307. "Show empty lines above all visible headlines.
  6308. The region to be covered depends on STATE when called through
  6309. `org-cycle-hook'. Lisp program can use t for STATE to get the
  6310. entire buffer covered. Note that an empty line is only shown if there
  6311. are at least `org-cycle-separator-lines' empty lines before the headline."
  6312. (when (/= org-cycle-separator-lines 0)
  6313. (save-excursion
  6314. (let* ((n (abs org-cycle-separator-lines))
  6315. (re (cond
  6316. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  6317. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  6318. (t (let ((ns (number-to-string (- n 2))))
  6319. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  6320. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  6321. beg end)
  6322. (cond
  6323. ((memq state '(overview contents t))
  6324. (setq beg (point-min) end (point-max)))
  6325. ((memq state '(children folded))
  6326. (setq beg (point)
  6327. end (progn (org-end-of-subtree t t)
  6328. (line-beginning-position 2)))))
  6329. (when beg
  6330. (goto-char beg)
  6331. (while (re-search-forward re end t)
  6332. (unless (get-char-property (match-end 1) 'invisible)
  6333. (let ((e (match-end 1))
  6334. (b (if (>= org-cycle-separator-lines 0)
  6335. (match-beginning 1)
  6336. (save-excursion
  6337. (goto-char (match-beginning 0))
  6338. (skip-chars-backward " \t\n")
  6339. (line-end-position)))))
  6340. (outline-flag-region b e nil))))))))
  6341. ;; Never hide empty lines at the end of the file.
  6342. (save-excursion
  6343. (goto-char (point-max))
  6344. (outline-previous-heading)
  6345. (outline-end-of-heading)
  6346. (if (and (looking-at "[ \t\n]+")
  6347. (= (match-end 0) (point-max)))
  6348. (outline-flag-region (point) (match-end 0) nil))))
  6349. (defun org-show-empty-lines-in-parent ()
  6350. "Move to the parent and re-show empty lines before visible headlines."
  6351. (save-excursion
  6352. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  6353. (org-cycle-show-empty-lines context))))
  6354. (defun org-files-list ()
  6355. "Return `org-agenda-files' list, plus all open org-mode files.
  6356. This is useful for operations that need to scan all of a user's
  6357. open and agenda-wise Org files."
  6358. (let ((files (mapcar 'expand-file-name (org-agenda-files))))
  6359. (dolist (buf (buffer-list))
  6360. (with-current-buffer buf
  6361. (if (and (derived-mode-p 'org-mode) (buffer-file-name))
  6362. (let ((file (expand-file-name (buffer-file-name))))
  6363. (unless (member file files)
  6364. (push file files))))))
  6365. files))
  6366. (defsubst org-entry-beginning-position ()
  6367. "Return the beginning position of the current entry."
  6368. (save-excursion (outline-back-to-heading t) (point)))
  6369. (defsubst org-entry-end-position ()
  6370. "Return the end position of the current entry."
  6371. (save-excursion (outline-next-heading) (point)))
  6372. (defun org-cycle-hide-drawers (state &optional exceptions)
  6373. "Re-hide all drawers after a visibility state change.
  6374. When non-nil, optional argument EXCEPTIONS is a list of strings
  6375. specifying which drawers should not be hidden."
  6376. (when (and (derived-mode-p 'org-mode)
  6377. (not (memq state '(overview folded contents))))
  6378. (save-excursion
  6379. (let* ((globalp (memq state '(contents all)))
  6380. (beg (if globalp (point-min) (point)))
  6381. (end (if globalp (point-max)
  6382. (if (eq state 'children)
  6383. (save-excursion (outline-next-heading) (point))
  6384. (org-end-of-subtree t)))))
  6385. (goto-char beg)
  6386. (while (re-search-forward org-drawer-regexp (max end (point)) t)
  6387. (unless (member-ignore-case (match-string 1) exceptions)
  6388. (let ((drawer (org-element-at-point)))
  6389. (when (memq (org-element-type drawer) '(drawer property-drawer))
  6390. (org-flag-drawer t drawer)
  6391. ;; Make sure to skip drawer entirely or we might flag
  6392. ;; it another time when matching its ending line with
  6393. ;; `org-drawer-regexp'.
  6394. (goto-char (org-element-property :end drawer))))))))))
  6395. (defun org-flag-drawer (flag &optional element)
  6396. "When FLAG is non-nil, hide the drawer we are at.
  6397. Otherwise make it visible. When optional argument ELEMENT is
  6398. a parsed drawer, as returned by `org-element-at-point', hide or
  6399. show that drawer instead."
  6400. (when (save-excursion
  6401. (beginning-of-line)
  6402. (org-looking-at-p org-drawer-regexp))
  6403. (let ((drawer (or element (org-element-at-point))))
  6404. (when (memq (org-element-type drawer) '(drawer property-drawer))
  6405. (let ((post (org-element-property :post-affiliated drawer)))
  6406. (save-excursion
  6407. (outline-flag-region
  6408. (progn (goto-char post) (line-end-position))
  6409. (progn (goto-char (org-element-property :end drawer))
  6410. (skip-chars-backward " \r\t\n")
  6411. (line-end-position))
  6412. flag))
  6413. ;; When the drawer is hidden away, make sure point lies in
  6414. ;; a visible part of the buffer.
  6415. (when (and flag (> (line-beginning-position) post))
  6416. (goto-char post)))))))
  6417. (defun org-subtree-end-visible-p ()
  6418. "Is the end of the current subtree visible?"
  6419. (pos-visible-in-window-p
  6420. (save-excursion (org-end-of-subtree t) (point))))
  6421. (defun org-first-headline-recenter ()
  6422. "Move cursor to the first headline and recenter the headline."
  6423. (goto-char (point-min))
  6424. (when (re-search-forward (concat "^\\(" org-outline-regexp "\\)") nil t)
  6425. (set-window-start (selected-window) (point-at-bol))))
  6426. ;;; Saving and restoring visibility
  6427. (defun org-outline-overlay-data (&optional use-markers)
  6428. "Return a list of the locations of all outline overlays.
  6429. These are overlays with the `invisible' property value `outline'.
  6430. The return value is a list of cons cells, with start and stop
  6431. positions for each overlay.
  6432. If USE-MARKERS is set, return the positions as markers."
  6433. (let (beg end)
  6434. (save-excursion
  6435. (save-restriction
  6436. (widen)
  6437. (delq nil
  6438. (mapcar (lambda (o)
  6439. (when (eq (overlay-get o 'invisible) 'outline)
  6440. (setq beg (overlay-start o)
  6441. end (overlay-end o))
  6442. (and beg end (> end beg)
  6443. (if use-markers
  6444. (cons (copy-marker beg)
  6445. (copy-marker end t))
  6446. (cons beg end)))))
  6447. (overlays-in (point-min) (point-max))))))))
  6448. (defun org-set-outline-overlay-data (data)
  6449. "Create visibility overlays for all positions in DATA.
  6450. DATA should have been made by `org-outline-overlay-data'."
  6451. (let (o)
  6452. (save-excursion
  6453. (save-restriction
  6454. (widen)
  6455. (show-all)
  6456. (mapc (lambda (c)
  6457. (outline-flag-region (car c) (cdr c) t))
  6458. data)))))
  6459. ;;; Folding of blocks
  6460. (defvar org-hide-block-overlays nil
  6461. "Overlays hiding blocks.")
  6462. (make-variable-buffer-local 'org-hide-block-overlays)
  6463. (defun org-block-map (function &optional start end)
  6464. "Call FUNCTION at the head of all source blocks in the current buffer.
  6465. Optional arguments START and END can be used to limit the range."
  6466. (let ((start (or start (point-min)))
  6467. (end (or end (point-max))))
  6468. (save-excursion
  6469. (goto-char start)
  6470. (while (and (< (point) end) (re-search-forward org-block-regexp end t))
  6471. (save-excursion
  6472. (save-match-data
  6473. (goto-char (match-beginning 0))
  6474. (funcall function)))))))
  6475. (defun org-hide-block-toggle-all ()
  6476. "Toggle the visibility of all blocks in the current buffer."
  6477. (org-block-map 'org-hide-block-toggle))
  6478. (defun org-hide-block-all ()
  6479. "Fold all blocks in the current buffer."
  6480. (interactive)
  6481. (org-show-block-all)
  6482. (org-block-map 'org-hide-block-toggle-maybe))
  6483. (defun org-show-block-all ()
  6484. "Unfold all blocks in the current buffer."
  6485. (interactive)
  6486. (mapc 'delete-overlay org-hide-block-overlays)
  6487. (setq org-hide-block-overlays nil))
  6488. (defun org-hide-block-toggle-maybe ()
  6489. "Toggle visibility of block at point.
  6490. Unlike to `org-hide-block-toggle', this function does not throw
  6491. an error. Return a non-nil value when toggling is successful."
  6492. (interactive)
  6493. (ignore-errors (org-hide-block-toggle)))
  6494. (defun org-hide-block-toggle (&optional force)
  6495. "Toggle the visibility of the current block.
  6496. When optional argument FORCE is `off', make block visible. If it
  6497. is non-nil, hide it unconditionally. Throw an error when not at
  6498. a block. Return a non-nil value when toggling is successful."
  6499. (interactive)
  6500. (let ((element (org-element-at-point)))
  6501. (unless (memq (org-element-type element)
  6502. '(center-block comment-block dynamic-block example-block
  6503. export-block quote-block special-block
  6504. src-block verse-block))
  6505. (user-error "Not at a block"))
  6506. (let* ((start (save-excursion
  6507. (goto-char (org-element-property :post-affiliated element))
  6508. (line-end-position)))
  6509. (end (save-excursion
  6510. (goto-char (org-element-property :end element))
  6511. (skip-chars-backward " \r\t\n")
  6512. (line-end-position)))
  6513. (overlays (overlays-at start)))
  6514. (cond
  6515. ;; Do nothing when not before or at the block opening line or
  6516. ;; at the block closing line.
  6517. ((let ((eol (line-end-position))) (and (> eol start) (/= eol end))) nil)
  6518. ((and (not (eq force 'off))
  6519. (not (memq t (mapcar
  6520. (lambda (o)
  6521. (eq (overlay-get o 'invisible) 'org-hide-block))
  6522. overlays))))
  6523. (let ((ov (make-overlay start end)))
  6524. (overlay-put ov 'invisible 'org-hide-block)
  6525. ;; Make the block accessible to `isearch'.
  6526. (overlay-put
  6527. ov 'isearch-open-invisible
  6528. (lambda (ov)
  6529. (when (memq ov org-hide-block-overlays)
  6530. (setq org-hide-block-overlays (delq ov org-hide-block-overlays)))
  6531. (when (eq (overlay-get ov 'invisible) 'org-hide-block)
  6532. (delete-overlay ov))))
  6533. (push ov org-hide-block-overlays)
  6534. ;; When the block is hidden away, make sure point is left in
  6535. ;; a visible part of the buffer.
  6536. (when (> (line-beginning-position) start)
  6537. (goto-char start)
  6538. (beginning-of-line))
  6539. ;; Signal successful toggling.
  6540. t))
  6541. ((or (not force) (eq force 'off))
  6542. (dolist (ov overlays t)
  6543. (when (memq ov org-hide-block-overlays)
  6544. (setq org-hide-block-overlays (delq ov org-hide-block-overlays)))
  6545. (when (eq (overlay-get ov 'invisible) 'org-hide-block)
  6546. (delete-overlay ov))))))))
  6547. ;; org-tab-after-check-for-cycling-hook
  6548. (add-hook 'org-tab-first-hook 'org-hide-block-toggle-maybe)
  6549. ;; Remove overlays when changing major mode
  6550. (add-hook 'org-mode-hook
  6551. (lambda () (org-add-hook 'change-major-mode-hook
  6552. 'org-show-block-all 'append 'local)))
  6553. ;;; Org-goto
  6554. (defvar org-goto-window-configuration nil)
  6555. (defvar org-goto-marker nil)
  6556. (defvar org-goto-map)
  6557. (defun org-goto-map ()
  6558. "Set the keymap `org-goto'."
  6559. (setq org-goto-map
  6560. (let ((map (make-sparse-keymap)))
  6561. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command
  6562. mouse-drag-region universal-argument org-occur)))
  6563. (dolist (cmd cmds)
  6564. (substitute-key-definition cmd cmd map global-map)))
  6565. (suppress-keymap map)
  6566. (org-defkey map "\C-m" 'org-goto-ret)
  6567. (org-defkey map [(return)] 'org-goto-ret)
  6568. (org-defkey map [(left)] 'org-goto-left)
  6569. (org-defkey map [(right)] 'org-goto-right)
  6570. (org-defkey map [(control ?g)] 'org-goto-quit)
  6571. (org-defkey map "\C-i" 'org-cycle)
  6572. (org-defkey map [(tab)] 'org-cycle)
  6573. (org-defkey map [(down)] 'outline-next-visible-heading)
  6574. (org-defkey map [(up)] 'outline-previous-visible-heading)
  6575. (if org-goto-auto-isearch
  6576. (if (fboundp 'define-key-after)
  6577. (define-key-after map [t] 'org-goto-local-auto-isearch)
  6578. nil)
  6579. (org-defkey map "q" 'org-goto-quit)
  6580. (org-defkey map "n" 'outline-next-visible-heading)
  6581. (org-defkey map "p" 'outline-previous-visible-heading)
  6582. (org-defkey map "f" 'outline-forward-same-level)
  6583. (org-defkey map "b" 'outline-backward-same-level)
  6584. (org-defkey map "u" 'outline-up-heading))
  6585. (org-defkey map "/" 'org-occur)
  6586. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  6587. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  6588. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  6589. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  6590. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  6591. map)))
  6592. (defconst org-goto-help
  6593. "Browse buffer copy, to find location or copy text.%s
  6594. RET=jump to location C-g=quit and return to previous location
  6595. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  6596. (defvar org-goto-start-pos) ; dynamically scoped parameter
  6597. (defun org-goto (&optional alternative-interface)
  6598. "Look up a different location in the current file, keeping current visibility.
  6599. When you want look-up or go to a different location in a
  6600. document, the fastest way is often to fold the entire buffer and
  6601. then dive into the tree. This method has the disadvantage, that
  6602. the previous location will be folded, which may not be what you
  6603. want.
  6604. This command works around this by showing a copy of the current
  6605. buffer in an indirect buffer, in overview mode. You can dive
  6606. into the tree in that copy, use org-occur and incremental search
  6607. to find a location. When pressing RET or `Q', the command
  6608. returns to the original buffer in which the visibility is still
  6609. unchanged. After RET it will also jump to the location selected
  6610. in the indirect buffer and expose the headline hierarchy above.
  6611. With a prefix argument, use the alternative interface: e.g. if
  6612. `org-goto-interface' is 'outline use 'outline-path-completion."
  6613. (interactive "P")
  6614. (org-goto-map)
  6615. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  6616. (org-refile-use-outline-path t)
  6617. (org-refile-target-verify-function nil)
  6618. (interface
  6619. (if (not alternative-interface)
  6620. org-goto-interface
  6621. (if (eq org-goto-interface 'outline)
  6622. 'outline-path-completion
  6623. 'outline)))
  6624. (org-goto-start-pos (point))
  6625. (selected-point
  6626. (if (eq interface 'outline)
  6627. (car (org-get-location (current-buffer) org-goto-help))
  6628. (let ((pa (org-refile-get-location "Goto" nil nil t)))
  6629. (org-refile-check-position pa)
  6630. (nth 3 pa)))))
  6631. (if selected-point
  6632. (progn
  6633. (org-mark-ring-push org-goto-start-pos)
  6634. (goto-char selected-point)
  6635. (if (or (outline-invisible-p) (org-invisible-p2))
  6636. (org-show-context 'org-goto)))
  6637. (message "Quit"))))
  6638. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  6639. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  6640. (defvar org-goto-local-auto-isearch-map) ; defined below
  6641. (defun org-get-location (buf help)
  6642. "Let the user select a location in the Org-mode buffer BUF.
  6643. This function uses a recursive edit. It returns the selected position
  6644. or nil."
  6645. (org-no-popups
  6646. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  6647. (isearch-hide-immediately nil)
  6648. (isearch-search-fun-function
  6649. (lambda () 'org-goto-local-search-headings))
  6650. (org-goto-selected-point org-goto-exit-command))
  6651. (save-excursion
  6652. (save-window-excursion
  6653. (delete-other-windows)
  6654. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  6655. (org-pop-to-buffer-same-window
  6656. (condition-case nil
  6657. (make-indirect-buffer (current-buffer) "*org-goto*")
  6658. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  6659. (with-output-to-temp-buffer "*Org Help*"
  6660. (princ (format help (if org-goto-auto-isearch
  6661. " Just type for auto-isearch."
  6662. " n/p/f/b/u to navigate, q to quit."))))
  6663. (org-fit-window-to-buffer (get-buffer-window "*Org Help*"))
  6664. (setq buffer-read-only nil)
  6665. (let ((org-startup-truncated t)
  6666. (org-startup-folded nil)
  6667. (org-startup-align-all-tables nil))
  6668. (org-mode)
  6669. (org-overview))
  6670. (setq buffer-read-only t)
  6671. (if (and (boundp 'org-goto-start-pos)
  6672. (integer-or-marker-p org-goto-start-pos))
  6673. (progn (goto-char org-goto-start-pos)
  6674. (when (outline-invisible-p)
  6675. (org-show-set-visibility 'lineage)))
  6676. (goto-char (point-min)))
  6677. (let (org-special-ctrl-a/e) (org-beginning-of-line))
  6678. (message "Select location and press RET")
  6679. (use-local-map org-goto-map)
  6680. (recursive-edit)))
  6681. (kill-buffer "*org-goto*")
  6682. (cons org-goto-selected-point org-goto-exit-command))))
  6683. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  6684. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  6685. ;; `isearch-other-control-char' was removed in Emacs 24.4.
  6686. (if (fboundp 'isearch-other-control-char)
  6687. (progn
  6688. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  6689. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char))
  6690. (define-key org-goto-local-auto-isearch-map "\C-i" nil)
  6691. (define-key org-goto-local-auto-isearch-map "\C-m" nil)
  6692. (define-key org-goto-local-auto-isearch-map [return] nil))
  6693. (defun org-goto-local-search-headings (string bound noerror)
  6694. "Search and make sure that any matches are in headlines."
  6695. (catch 'return
  6696. (while (if isearch-forward
  6697. (search-forward string bound noerror)
  6698. (search-backward string bound noerror))
  6699. (when (save-match-data
  6700. (and (save-excursion
  6701. (beginning-of-line)
  6702. (looking-at org-complex-heading-regexp))
  6703. (or (not (match-beginning 5))
  6704. (< (point) (match-beginning 5)))))
  6705. (throw 'return (point))))))
  6706. (defun org-goto-local-auto-isearch ()
  6707. "Start isearch."
  6708. (interactive)
  6709. (goto-char (point-min))
  6710. (let ((keys (this-command-keys)))
  6711. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  6712. (isearch-mode t)
  6713. (isearch-process-search-char (string-to-char keys)))))
  6714. (defun org-goto-ret (&optional arg)
  6715. "Finish `org-goto' by going to the new location."
  6716. (interactive "P")
  6717. (setq org-goto-selected-point (point)
  6718. org-goto-exit-command 'return)
  6719. (throw 'exit nil))
  6720. (defun org-goto-left ()
  6721. "Finish `org-goto' by going to the new location."
  6722. (interactive)
  6723. (if (org-at-heading-p)
  6724. (progn
  6725. (beginning-of-line 1)
  6726. (setq org-goto-selected-point (point)
  6727. org-goto-exit-command 'left)
  6728. (throw 'exit nil))
  6729. (user-error "Not on a heading")))
  6730. (defun org-goto-right ()
  6731. "Finish `org-goto' by going to the new location."
  6732. (interactive)
  6733. (if (org-at-heading-p)
  6734. (progn
  6735. (setq org-goto-selected-point (point)
  6736. org-goto-exit-command 'right)
  6737. (throw 'exit nil))
  6738. (user-error "Not on a heading")))
  6739. (defun org-goto-quit ()
  6740. "Finish `org-goto' without cursor motion."
  6741. (interactive)
  6742. (setq org-goto-selected-point nil)
  6743. (setq org-goto-exit-command 'quit)
  6744. (throw 'exit nil))
  6745. ;;; Indirect buffer display of subtrees
  6746. (defvar org-indirect-dedicated-frame nil
  6747. "This is the frame being used for indirect tree display.")
  6748. (defvar org-last-indirect-buffer nil)
  6749. (defun org-tree-to-indirect-buffer (&optional arg)
  6750. "Create indirect buffer and narrow it to current subtree.
  6751. With a numerical prefix ARG, go up to this level and then take that tree.
  6752. If ARG is negative, go up that many levels.
  6753. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  6754. indirect buffer previously made with this command, to avoid proliferation of
  6755. indirect buffers. However, when you call the command with a \
  6756. \\[universal-argument] prefix, or
  6757. when `org-indirect-buffer-display' is `new-frame', the last buffer
  6758. is kept so that you can work with several indirect buffers at the same time.
  6759. If `org-indirect-buffer-display' is `dedicated-frame', the \
  6760. \\[universal-argument] prefix also
  6761. requests that a new frame be made for the new buffer, so that the dedicated
  6762. frame is not changed."
  6763. (interactive "P")
  6764. (let ((cbuf (current-buffer))
  6765. (cwin (selected-window))
  6766. (pos (point))
  6767. beg end level heading ibuf)
  6768. (save-excursion
  6769. (org-back-to-heading t)
  6770. (when (numberp arg)
  6771. (setq level (org-outline-level))
  6772. (if (< arg 0) (setq arg (+ level arg)))
  6773. (while (> (setq level (org-outline-level)) arg)
  6774. (org-up-heading-safe)))
  6775. (setq beg (point)
  6776. heading (org-get-heading))
  6777. (org-end-of-subtree t t)
  6778. (if (org-at-heading-p) (backward-char 1))
  6779. (setq end (point)))
  6780. (if (and (buffer-live-p org-last-indirect-buffer)
  6781. (not (eq org-indirect-buffer-display 'new-frame))
  6782. (not arg))
  6783. (kill-buffer org-last-indirect-buffer))
  6784. (setq ibuf (org-get-indirect-buffer cbuf heading)
  6785. org-last-indirect-buffer ibuf)
  6786. (cond
  6787. ((or (eq org-indirect-buffer-display 'new-frame)
  6788. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  6789. (select-frame (make-frame))
  6790. (delete-other-windows)
  6791. (org-pop-to-buffer-same-window ibuf)
  6792. (org-set-frame-title heading))
  6793. ((eq org-indirect-buffer-display 'dedicated-frame)
  6794. (raise-frame
  6795. (select-frame (or (and org-indirect-dedicated-frame
  6796. (frame-live-p org-indirect-dedicated-frame)
  6797. org-indirect-dedicated-frame)
  6798. (setq org-indirect-dedicated-frame (make-frame)))))
  6799. (delete-other-windows)
  6800. (org-pop-to-buffer-same-window ibuf)
  6801. (org-set-frame-title (concat "Indirect: " heading)))
  6802. ((eq org-indirect-buffer-display 'current-window)
  6803. (org-pop-to-buffer-same-window ibuf))
  6804. ((eq org-indirect-buffer-display 'other-window)
  6805. (pop-to-buffer ibuf))
  6806. (t (error "Invalid value")))
  6807. (if (featurep 'xemacs)
  6808. (save-excursion (org-mode) (turn-on-font-lock)))
  6809. (narrow-to-region beg end)
  6810. (show-all)
  6811. (goto-char pos)
  6812. (run-hook-with-args 'org-cycle-hook 'all)
  6813. (and (window-live-p cwin) (select-window cwin))))
  6814. (defun org-get-indirect-buffer (&optional buffer heading)
  6815. (setq buffer (or buffer (current-buffer)))
  6816. (let ((n 1) (base (buffer-name buffer)) bname)
  6817. (while (buffer-live-p
  6818. (get-buffer
  6819. (setq bname
  6820. (concat base "-"
  6821. (if heading (concat heading "-" (number-to-string n))
  6822. (number-to-string n))))))
  6823. (setq n (1+ n)))
  6824. (condition-case nil
  6825. (make-indirect-buffer buffer bname 'clone)
  6826. (error (make-indirect-buffer buffer bname)))))
  6827. (defun org-set-frame-title (title)
  6828. "Set the title of the current frame to the string TITLE."
  6829. ;; FIXME: how to name a single frame in XEmacs???
  6830. (unless (featurep 'xemacs)
  6831. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  6832. ;;;; Structure editing
  6833. ;;; Inserting headlines
  6834. (defun org-previous-line-empty-p (&optional next)
  6835. "Is the previous line a blank line?
  6836. When NEXT is non-nil, check the next line instead."
  6837. (save-excursion
  6838. (and (not (bobp))
  6839. (or (beginning-of-line (if next 2 0)) t)
  6840. (save-match-data
  6841. (looking-at "[ \t]*$")))))
  6842. (defun org-insert-heading (&optional arg invisible-ok top-level)
  6843. "Insert a new heading or an item with the same depth at point.
  6844. If point is at the beginning of a heading or a list item, insert
  6845. a new heading or a new item above the current one. If point is
  6846. at the beginning of a normal line, turn the line into a heading.
  6847. If point is in the middle of a headline or a list item, split the
  6848. headline or the item and create a new headline/item with the text
  6849. in the current line after point \(see `org-M-RET-may-split-line'
  6850. on how to modify this behavior).
  6851. With one universal prefix argument, set the user option
  6852. `org-insert-heading-respect-content' to t for the duration of
  6853. the command. This modifies the behavior described above in this
  6854. ways: on list items and at the beginning of normal lines, force
  6855. the insertion of a heading after the current subtree.
  6856. With two universal prefix arguments, insert the heading at the
  6857. end of the grandparent subtree. For example, if point is within
  6858. a 2nd-level heading, then it will insert a 2nd-level heading at
  6859. the end of the 1st-level parent heading.
  6860. If point is at the beginning of a headline, insert a sibling
  6861. before the current headline. If point is not at the beginning,
  6862. split the line and create a new headline with the text in the
  6863. current line after point \(see `org-M-RET-may-split-line' on how
  6864. to modify this behavior).
  6865. If point is at the beginning of a normal line, turn this line
  6866. into a heading.
  6867. When INVISIBLE-OK is set, stop at invisible headlines when going
  6868. back. This is important for non-interactive uses of the
  6869. command.
  6870. When optional argument TOP-LEVEL is non-nil, insert a level 1
  6871. heading, unconditionally."
  6872. (interactive "P")
  6873. (if (org-called-interactively-p 'any) (org-reveal))
  6874. (let ((itemp (and (not top-level) (org-in-item-p)))
  6875. (may-split (org-get-alist-option org-M-RET-may-split-line 'headline))
  6876. (respect-content (or org-insert-heading-respect-content
  6877. (equal arg '(4))))
  6878. (initial-content ""))
  6879. (cond
  6880. ((or (= (buffer-size) 0)
  6881. (and (not (save-excursion
  6882. (and (ignore-errors (org-back-to-heading invisible-ok))
  6883. (org-at-heading-p))))
  6884. (or arg (not itemp))))
  6885. ;; At beginning of buffer or so high up that only a heading
  6886. ;; makes sense.
  6887. (cond ((and (bolp) (not respect-content)) (insert "* "))
  6888. ((not respect-content)
  6889. (unless may-split (end-of-line))
  6890. (insert "\n* "))
  6891. ((re-search-forward org-outline-regexp-bol nil t)
  6892. (beginning-of-line)
  6893. (insert "* \n")
  6894. (backward-char))
  6895. (t (goto-char (point-max))
  6896. (insert "\n* ")))
  6897. (run-hooks 'org-insert-heading-hook))
  6898. ((and itemp (not (member arg '((4) (16)))) (org-insert-item)))
  6899. (t
  6900. ;; Maybe move at the end of the subtree
  6901. (when (equal arg '(16))
  6902. (org-up-heading-safe)
  6903. (org-end-of-subtree t))
  6904. ;; Insert a heading
  6905. (save-restriction
  6906. (widen)
  6907. (let* ((level nil)
  6908. (on-heading (org-at-heading-p))
  6909. (empty-line-p (if on-heading
  6910. (org-previous-line-empty-p)
  6911. ;; We will decide later
  6912. nil))
  6913. ;; Get a level string to fall back on.
  6914. (fix-level
  6915. (if (org-before-first-heading-p) "*"
  6916. (save-excursion
  6917. (org-back-to-heading t)
  6918. (if (org-previous-line-empty-p) (setq empty-line-p t))
  6919. (looking-at org-outline-regexp)
  6920. (make-string (1- (length (match-string 0))) ?*))))
  6921. (stars
  6922. (save-excursion
  6923. (condition-case nil
  6924. (if top-level "* "
  6925. (org-back-to-heading invisible-ok)
  6926. (when (and (not on-heading)
  6927. (featurep 'org-inlinetask)
  6928. (integerp org-inlinetask-min-level)
  6929. (>= (length (match-string 0))
  6930. org-inlinetask-min-level))
  6931. ;; Find a heading level before the inline
  6932. ;; task.
  6933. (while (and (setq level (org-up-heading-safe))
  6934. (>= level org-inlinetask-min-level)))
  6935. (if (org-at-heading-p)
  6936. (org-back-to-heading invisible-ok)
  6937. (error "This should not happen")))
  6938. (unless (and (save-excursion
  6939. (save-match-data
  6940. (org-backward-heading-same-level
  6941. 1 invisible-ok))
  6942. (= (point) (match-beginning 0)))
  6943. (not (org-previous-line-empty-p t)))
  6944. (setq empty-line-p (or empty-line-p
  6945. (org-previous-line-empty-p))))
  6946. (match-string 0))
  6947. (error (or fix-level "* ")))))
  6948. (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
  6949. (blank (if (eq blank-a 'auto) empty-line-p blank-a))
  6950. pos hide-previous previous-pos)
  6951. ;; If we insert after content, move there and clean up
  6952. ;; whitespace.
  6953. (when (and respect-content
  6954. (not (org-looking-at-p org-outline-regexp-bol)))
  6955. (if (not (org-before-first-heading-p))
  6956. (org-end-of-subtree nil t)
  6957. (re-search-forward org-outline-regexp-bol)
  6958. (beginning-of-line 0))
  6959. (skip-chars-backward " \r\t\n")
  6960. (and (not (looking-back "^\\*+" (line-beginning-position)))
  6961. (looking-at "[ \t]+") (replace-match ""))
  6962. (unless (eobp) (forward-char 1))
  6963. (when (looking-at "^\\*")
  6964. (unless (bobp) (backward-char 1))
  6965. (insert "\n")))
  6966. ;; If we are splitting, grab the text that should be moved
  6967. ;; to the new headline.
  6968. (when may-split
  6969. (if (org-on-heading-p)
  6970. ;; This is a heading: split intelligently (keeping
  6971. ;; tags).
  6972. (let ((pos (point)))
  6973. (beginning-of-line)
  6974. (unless (looking-at org-complex-heading-regexp)
  6975. (error "This should not happen"))
  6976. (when (and (match-beginning 4)
  6977. (> pos (match-beginning 4))
  6978. (< pos (match-end 4)))
  6979. (setq initial-content (buffer-substring pos (match-end 4)))
  6980. (goto-char pos)
  6981. (delete-region (point) (match-end 4))
  6982. (if (looking-at "[ \t]*$")
  6983. (replace-match "")
  6984. (insert (make-string (length initial-content) ?\s)))
  6985. (setq initial-content (org-trim initial-content)))
  6986. (goto-char pos))
  6987. ;; A normal line.
  6988. (setq initial-content
  6989. (org-trim
  6990. (delete-and-extract-region (point) (line-end-position))))))
  6991. ;; If we are at the beginning of the line, insert before it.
  6992. ;; Otherwise, after it.
  6993. (cond
  6994. ((and (bolp) (looking-at "[ \t]*$")))
  6995. ((bolp) (save-excursion (insert "\n")))
  6996. (t (end-of-line)
  6997. (insert "\n")))
  6998. ;; Insert the new heading
  6999. (insert stars)
  7000. (just-one-space)
  7001. (insert initial-content)
  7002. (unless (and blank (org-previous-line-empty-p))
  7003. (org-N-empty-lines-before-current (if blank 1 0)))
  7004. ;; Adjust visibility, which may be messed up if we removed
  7005. ;; blank lines while previous entry was hidden.
  7006. (let ((bol (line-beginning-position)))
  7007. (dolist (o (overlays-at (1- bol)))
  7008. (when (and (eq (overlay-get o 'invisible) 'outline)
  7009. (eq (overlay-end o) bol))
  7010. (move-overlay o (overlay-start o) (1- bol)))))
  7011. (run-hooks 'org-insert-heading-hook)))))))
  7012. (defun org-N-empty-lines-before-current (N)
  7013. "Make the number of empty lines before current exactly N.
  7014. So this will delete or add empty lines."
  7015. (save-excursion
  7016. (beginning-of-line)
  7017. (let ((p (point)))
  7018. (skip-chars-backward " \r\t\n")
  7019. (unless (bolp) (forward-line))
  7020. (delete-region (point) p))
  7021. (when (> N 0) (insert (make-string N ?\n)))))
  7022. (defun org-get-heading (&optional no-tags no-todo)
  7023. "Return the heading of the current entry, without the stars.
  7024. When NO-TAGS is non-nil, don't include tags.
  7025. When NO-TODO is non-nil, don't include TODO keywords."
  7026. (save-excursion
  7027. (org-back-to-heading t)
  7028. (cond
  7029. ((and no-tags no-todo)
  7030. (looking-at org-complex-heading-regexp)
  7031. (match-string 4))
  7032. (no-tags
  7033. (looking-at (concat org-outline-regexp
  7034. "\\(.*?\\)"
  7035. "\\(?:[ \t]+:[[:alnum:]:_@#%]+:\\)?[ \t]*$"))
  7036. (match-string 1))
  7037. (no-todo
  7038. (looking-at org-todo-line-regexp)
  7039. (match-string 3))
  7040. (t (looking-at org-heading-regexp)
  7041. (match-string 2)))))
  7042. (defvar orgstruct-mode) ; defined below
  7043. (defun org-heading-components ()
  7044. "Return the components of the current heading.
  7045. This is a list with the following elements:
  7046. - the level as an integer
  7047. - the reduced level, different if `org-odd-levels-only' is set.
  7048. - the TODO keyword, or nil
  7049. - the priority character, like ?A, or nil if no priority is given
  7050. - the headline text itself, or the tags string if no headline text
  7051. - the tags string, or nil."
  7052. (save-excursion
  7053. (org-back-to-heading t)
  7054. (if (let (case-fold-search)
  7055. (looking-at
  7056. (if orgstruct-mode
  7057. org-heading-regexp
  7058. org-complex-heading-regexp)))
  7059. (if orgstruct-mode
  7060. (list (length (match-string 1))
  7061. (org-reduced-level (length (match-string 1)))
  7062. nil
  7063. nil
  7064. (match-string 2)
  7065. nil)
  7066. (list (length (match-string 1))
  7067. (org-reduced-level (length (match-string 1)))
  7068. (org-match-string-no-properties 2)
  7069. (and (match-end 3) (aref (match-string 3) 2))
  7070. (org-match-string-no-properties 4)
  7071. (org-match-string-no-properties 5))))))
  7072. (defun org-get-entry ()
  7073. "Get the entry text, after heading, entire subtree."
  7074. (save-excursion
  7075. (org-back-to-heading t)
  7076. (buffer-substring (point-at-bol 2) (org-end-of-subtree t))))
  7077. (defun org-insert-heading-after-current ()
  7078. "Insert a new heading with same level as current, after current subtree."
  7079. (interactive)
  7080. (org-back-to-heading)
  7081. (org-insert-heading)
  7082. (org-move-subtree-down)
  7083. (end-of-line 1))
  7084. (defun org-insert-heading-respect-content (&optional invisible-ok)
  7085. "Insert heading with `org-insert-heading-respect-content' set to t."
  7086. (interactive)
  7087. (org-insert-heading '(4) invisible-ok))
  7088. (defun org-insert-todo-heading-respect-content (&optional force-state)
  7089. "Insert TODO heading with `org-insert-heading-respect-content' set to t."
  7090. (interactive)
  7091. (org-insert-todo-heading force-state '(4)))
  7092. (defun org-insert-todo-heading (arg &optional force-heading)
  7093. "Insert a new heading with the same level and TODO state as current heading.
  7094. If the heading has no TODO state, or if the state is DONE, use the first
  7095. state (TODO by default). Also with one prefix arg, force first state. With
  7096. two prefix args, force inserting at the end of the parent subtree."
  7097. (interactive "P")
  7098. (when (or force-heading (not (org-insert-item 'checkbox)))
  7099. (org-insert-heading (or (and (equal arg '(16)) '(16))
  7100. force-heading))
  7101. (save-excursion
  7102. (org-back-to-heading)
  7103. (outline-previous-heading)
  7104. (looking-at org-todo-line-regexp))
  7105. (let*
  7106. ((new-mark-x
  7107. (if (or (equal arg '(4))
  7108. (not (match-beginning 2))
  7109. (member (match-string 2) org-done-keywords))
  7110. (car org-todo-keywords-1)
  7111. (match-string 2)))
  7112. (new-mark
  7113. (or
  7114. (run-hook-with-args-until-success
  7115. 'org-todo-get-default-hook new-mark-x nil)
  7116. new-mark-x)))
  7117. (beginning-of-line 1)
  7118. (and (looking-at org-outline-regexp) (goto-char (match-end 0))
  7119. (if org-treat-insert-todo-heading-as-state-change
  7120. (org-todo new-mark)
  7121. (insert new-mark " "))))
  7122. (when org-provide-todo-statistics
  7123. (org-update-parent-todo-statistics))))
  7124. (defun org-insert-subheading (arg)
  7125. "Insert a new subheading and demote it.
  7126. Works for outline headings and for plain lists alike."
  7127. (interactive "P")
  7128. (org-insert-heading arg)
  7129. (cond
  7130. ((org-at-heading-p) (org-do-demote))
  7131. ((org-at-item-p) (org-indent-item))))
  7132. (defun org-insert-todo-subheading (arg)
  7133. "Insert a new subheading with TODO keyword or checkbox and demote it.
  7134. Works for outline headings and for plain lists alike."
  7135. (interactive "P")
  7136. (org-insert-todo-heading arg)
  7137. (cond
  7138. ((org-at-heading-p) (org-do-demote))
  7139. ((org-at-item-p) (org-indent-item))))
  7140. ;;; Promotion and Demotion
  7141. (defvar org-after-demote-entry-hook nil
  7142. "Hook run after an entry has been demoted.
  7143. The cursor will be at the beginning of the entry.
  7144. When a subtree is being demoted, the hook will be called for each node.")
  7145. (defvar org-after-promote-entry-hook nil
  7146. "Hook run after an entry has been promoted.
  7147. The cursor will be at the beginning of the entry.
  7148. When a subtree is being promoted, the hook will be called for each node.")
  7149. (defun org-promote-subtree ()
  7150. "Promote the entire subtree.
  7151. See also `org-promote'."
  7152. (interactive)
  7153. (save-excursion
  7154. (org-with-limited-levels (org-map-tree 'org-promote)))
  7155. (org-fix-position-after-promote))
  7156. (defun org-demote-subtree ()
  7157. "Demote the entire subtree. See `org-demote'.
  7158. See also `org-promote'."
  7159. (interactive)
  7160. (save-excursion
  7161. (org-with-limited-levels (org-map-tree 'org-demote)))
  7162. (org-fix-position-after-promote))
  7163. (defun org-do-promote ()
  7164. "Promote the current heading higher up the tree.
  7165. If the region is active in `transient-mark-mode', promote all headings
  7166. in the region."
  7167. (interactive)
  7168. (save-excursion
  7169. (if (org-region-active-p)
  7170. (org-map-region 'org-promote (region-beginning) (region-end))
  7171. (org-promote)))
  7172. (org-fix-position-after-promote))
  7173. (defun org-do-demote ()
  7174. "Demote the current heading lower down the tree.
  7175. If the region is active in `transient-mark-mode', demote all headings
  7176. in the region."
  7177. (interactive)
  7178. (save-excursion
  7179. (if (org-region-active-p)
  7180. (org-map-region 'org-demote (region-beginning) (region-end))
  7181. (org-demote)))
  7182. (org-fix-position-after-promote))
  7183. (defun org-fix-position-after-promote ()
  7184. "Make sure that after pro/demotion cursor position is right."
  7185. (let ((pos (point)))
  7186. (when (save-excursion
  7187. (beginning-of-line 1)
  7188. (looking-at org-todo-line-regexp)
  7189. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  7190. (cond ((eobp) (insert " "))
  7191. ((eolp) (insert " "))
  7192. ((equal (char-after) ?\ ) (forward-char 1))))))
  7193. (defun org-current-level ()
  7194. "Return the level of the current entry, or nil if before the first headline.
  7195. The level is the number of stars at the beginning of the
  7196. headline. Use `org-reduced-level' to remove the effect of
  7197. `org-odd-levels'. Unlike to `org-outline-level', this function
  7198. ignores inlinetasks."
  7199. (let ((level (org-with-limited-levels (org-outline-level))))
  7200. (and (> level 0) level)))
  7201. (defun org-get-previous-line-level ()
  7202. "Return the outline depth of the last headline before the current line.
  7203. Returns 0 for the first headline in the buffer, and nil if before the
  7204. first headline."
  7205. (and (org-current-level)
  7206. (or (and (/= (line-beginning-position) (point-min))
  7207. (save-excursion (beginning-of-line 0) (org-current-level)))
  7208. 0)))
  7209. (defun org-reduced-level (l)
  7210. "Compute the effective level of a heading.
  7211. This takes into account the setting of `org-odd-levels-only'."
  7212. (cond
  7213. ((zerop l) 0)
  7214. (org-odd-levels-only (1+ (floor (/ l 2))))
  7215. (t l)))
  7216. (defun org-level-increment ()
  7217. "Return the number of stars that will be added or removed at a
  7218. time to headlines when structure editing, based on the value of
  7219. `org-odd-levels-only'."
  7220. (if org-odd-levels-only 2 1))
  7221. (defun org-get-valid-level (level &optional change)
  7222. "Rectify a level change under the influence of `org-odd-levels-only'
  7223. LEVEL is a current level, CHANGE is by how much the level should be
  7224. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  7225. even level numbers will become the next higher odd number."
  7226. (if org-odd-levels-only
  7227. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  7228. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  7229. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  7230. (max 1 (+ level (or change 0)))))
  7231. (if (boundp 'define-obsolete-function-alias)
  7232. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  7233. (define-obsolete-function-alias 'org-get-legal-level
  7234. 'org-get-valid-level)
  7235. (define-obsolete-function-alias 'org-get-legal-level
  7236. 'org-get-valid-level "23.1")))
  7237. (defun org-promote ()
  7238. "Promote the current heading higher up the tree."
  7239. (org-with-wide-buffer
  7240. (org-back-to-heading t)
  7241. (let* ((after-change-functions (remq 'flyspell-after-change-function
  7242. after-change-functions))
  7243. (level (save-match-data (funcall outline-level)))
  7244. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  7245. (diff (abs (- level (length up-head) -1))))
  7246. (cond
  7247. ((and (= level 1) org-allow-promoting-top-level-subtree)
  7248. (replace-match "# " nil t))
  7249. ((= level 1)
  7250. (user-error "Cannot promote to level 0. UNDO to recover if necessary"))
  7251. (t (replace-match up-head nil t)))
  7252. (unless (= level 1)
  7253. (when org-auto-align-tags (org-set-tags nil 'ignore-column))
  7254. (when org-adapt-indentation (org-fixup-indentation (- diff))))
  7255. (run-hooks 'org-after-promote-entry-hook))))
  7256. (defun org-demote ()
  7257. "Demote the current heading lower down the tree."
  7258. (org-with-wide-buffer
  7259. (org-back-to-heading t)
  7260. (let* ((after-change-functions (remq 'flyspell-after-change-function
  7261. after-change-functions))
  7262. (level (save-match-data (funcall outline-level)))
  7263. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  7264. (diff (abs (- level (length down-head) -1))))
  7265. (replace-match down-head nil t)
  7266. (when org-auto-align-tags (org-set-tags nil 'ignore-column))
  7267. (when org-adapt-indentation (org-fixup-indentation diff))
  7268. (run-hooks 'org-after-demote-entry-hook))))
  7269. (defun org-cycle-level ()
  7270. "Cycle the level of an empty headline through possible states.
  7271. This goes first to child, then to parent, level, then up the hierarchy.
  7272. After top level, it switches back to sibling level."
  7273. (interactive)
  7274. (let ((org-adapt-indentation nil))
  7275. (when (org-point-at-end-of-empty-headline)
  7276. (setq this-command 'org-cycle-level) ; Only needed for caching
  7277. (let ((cur-level (org-current-level))
  7278. (prev-level (org-get-previous-line-level)))
  7279. (cond
  7280. ;; If first headline in file, promote to top-level.
  7281. ((= prev-level 0)
  7282. (loop repeat (/ (- cur-level 1) (org-level-increment))
  7283. do (org-do-promote)))
  7284. ;; If same level as prev, demote one.
  7285. ((= prev-level cur-level)
  7286. (org-do-demote))
  7287. ;; If parent is top-level, promote to top level if not already.
  7288. ((= prev-level 1)
  7289. (loop repeat (/ (- cur-level 1) (org-level-increment))
  7290. do (org-do-promote)))
  7291. ;; If top-level, return to prev-level.
  7292. ((= cur-level 1)
  7293. (loop repeat (/ (- prev-level 1) (org-level-increment))
  7294. do (org-do-demote)))
  7295. ;; If less than prev-level, promote one.
  7296. ((< cur-level prev-level)
  7297. (org-do-promote))
  7298. ;; If deeper than prev-level, promote until higher than
  7299. ;; prev-level.
  7300. ((> cur-level prev-level)
  7301. (loop repeat (+ 1 (/ (- cur-level prev-level) (org-level-increment)))
  7302. do (org-do-promote))))
  7303. t))))
  7304. (defun org-map-tree (fun)
  7305. "Call FUN for every heading underneath the current one."
  7306. (org-back-to-heading)
  7307. (let ((level (funcall outline-level)))
  7308. (save-excursion
  7309. (funcall fun)
  7310. (while (and (progn
  7311. (outline-next-heading)
  7312. (> (funcall outline-level) level))
  7313. (not (eobp)))
  7314. (funcall fun)))))
  7315. (defun org-map-region (fun beg end)
  7316. "Call FUN for every heading between BEG and END."
  7317. (let ((org-ignore-region t))
  7318. (save-excursion
  7319. (setq end (copy-marker end))
  7320. (goto-char beg)
  7321. (if (and (re-search-forward org-outline-regexp-bol nil t)
  7322. (< (point) end))
  7323. (funcall fun))
  7324. (while (and (progn
  7325. (outline-next-heading)
  7326. (< (point) end))
  7327. (not (eobp)))
  7328. (funcall fun)))))
  7329. (defun org-fixup-indentation (diff)
  7330. "Change the indentation in the current entry by DIFF.
  7331. DIFF is an integer. Indentation is done according to the
  7332. following rules:
  7333. - Planning information and property drawers are always indented
  7334. according to the new level of the headline;
  7335. - Footnote definitions and their contents are ignored;
  7336. - Inlinetasks' boundaries are not shifted;
  7337. - Empty lines are ignored;
  7338. - Other lines' indentation are shifted by DIFF columns, unless
  7339. it would introduce a structural change in the document, in
  7340. which case no shifting is done at all.
  7341. Assume point is at a heading or an inlinetask beginning."
  7342. (org-with-wide-buffer
  7343. (narrow-to-region (line-beginning-position)
  7344. (save-excursion
  7345. (if (org-with-limited-levels (org-at-heading-p))
  7346. (org-with-limited-levels (outline-next-heading))
  7347. (org-inlinetask-goto-end))
  7348. (point)))
  7349. (forward-line)
  7350. ;; Indent properly planning info and property drawer.
  7351. (when (org-looking-at-p org-planning-line-re)
  7352. (org-indent-line)
  7353. (forward-line))
  7354. (when (looking-at org-property-drawer-re)
  7355. (goto-char (match-end 0))
  7356. (forward-line)
  7357. (save-excursion (org-indent-region (match-beginning 0) (match-end 0))))
  7358. (catch 'no-shift
  7359. (when (zerop diff) (throw 'no-shift nil))
  7360. ;; If DIFF is negative, first check if a shift is possible at all
  7361. ;; (e.g., it doesn't break structure). This can only happen if
  7362. ;; some contents are not properly indented.
  7363. (let ((case-fold-search t))
  7364. (when (< diff 0)
  7365. (let ((diff (- diff))
  7366. (forbidden-re (concat org-outline-regexp
  7367. "\\|"
  7368. (substring org-footnote-definition-re 1))))
  7369. (save-excursion
  7370. (while (not (eobp))
  7371. (cond
  7372. ((org-looking-at-p "[ \t]*$") (forward-line))
  7373. ((and (org-looking-at-p org-footnote-definition-re)
  7374. (let ((e (org-element-at-point)))
  7375. (and (eq (org-element-type e) 'footnote-definition)
  7376. (goto-char (org-element-property :end e))))))
  7377. ((org-looking-at-p org-outline-regexp) (forward-line))
  7378. ;; Give up if shifting would move before column 0 or
  7379. ;; if it would introduce a headline or a footnote
  7380. ;; definition.
  7381. (t
  7382. (skip-chars-forward " \t")
  7383. (let ((ind (current-column)))
  7384. (when (or (< ind diff)
  7385. (and (= ind diff) (org-looking-at-p forbidden-re)))
  7386. (throw 'no-shift nil)))
  7387. ;; Ignore contents of example blocks and source
  7388. ;; blocks if their indentation is meant to be
  7389. ;; preserved. Jump to block's closing line.
  7390. (beginning-of-line)
  7391. (or (and (org-looking-at-p "[ \t]*#\\+BEGIN_\\(EXAMPLE\\|SRC\\)")
  7392. (let ((e (org-element-at-point)))
  7393. (and (memq (org-element-type e)
  7394. '(example-block src-block))
  7395. (or org-src-preserve-indentation
  7396. (org-element-property :preserve-indent e))
  7397. (goto-char (org-element-property :end e))
  7398. (progn (skip-chars-backward " \r\t\n")
  7399. (beginning-of-line)
  7400. t))))
  7401. (forward-line))))))))
  7402. ;; Shift lines but footnote definitions, inlinetasks boundaries
  7403. ;; by DIFF. Also skip contents of source or example blocks
  7404. ;; when indentation is meant to be preserved.
  7405. (while (not (eobp))
  7406. (cond
  7407. ((and (org-looking-at-p org-footnote-definition-re)
  7408. (let ((e (org-element-at-point)))
  7409. (and (eq (org-element-type e) 'footnote-definition)
  7410. (goto-char (org-element-property :end e))))))
  7411. ((org-looking-at-p org-outline-regexp) (forward-line))
  7412. ((org-looking-at-p "[ \t]*$") (forward-line))
  7413. (t
  7414. (org-indent-line-to (+ (org-get-indentation) diff))
  7415. (beginning-of-line)
  7416. (or (and (org-looking-at-p "[ \t]*#\\+BEGIN_\\(EXAMPLE\\|SRC\\)")
  7417. (let ((e (org-element-at-point)))
  7418. (and (memq (org-element-type e)
  7419. '(example-block src-block))
  7420. (or org-src-preserve-indentation
  7421. (org-element-property :preserve-indent e))
  7422. (goto-char (org-element-property :end e))
  7423. (progn (skip-chars-backward " \r\t\n")
  7424. (beginning-of-line)
  7425. t))))
  7426. (forward-line)))))))))
  7427. (defun org-convert-to-odd-levels ()
  7428. "Convert an org-mode file with all levels allowed to one with odd levels.
  7429. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  7430. level 5 etc."
  7431. (interactive)
  7432. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  7433. (let ((outline-level 'org-outline-level)
  7434. (org-odd-levels-only nil) n)
  7435. (save-excursion
  7436. (goto-char (point-min))
  7437. (while (re-search-forward "^\\*\\*+ " nil t)
  7438. (setq n (- (length (match-string 0)) 2))
  7439. (while (>= (setq n (1- n)) 0)
  7440. (org-demote))
  7441. (end-of-line 1))))))
  7442. (defun org-convert-to-oddeven-levels ()
  7443. "Convert an org-mode file with only odd levels to one with odd/even levels.
  7444. This promotes level 3 to level 2, level 5 to level 3 etc. If the
  7445. file contains a section with an even level, conversion would
  7446. destroy the structure of the file. An error is signaled in this
  7447. case."
  7448. (interactive)
  7449. (goto-char (point-min))
  7450. ;; First check if there are no even levels
  7451. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  7452. (org-show-set-visibility 'canonical)
  7453. (error "Not all levels are odd in this file. Conversion not possible"))
  7454. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  7455. (let ((outline-regexp org-outline-regexp)
  7456. (outline-level 'org-outline-level)
  7457. (org-odd-levels-only nil) n)
  7458. (save-excursion
  7459. (goto-char (point-min))
  7460. (while (re-search-forward "^\\*\\*+ " nil t)
  7461. (setq n (/ (1- (length (match-string 0))) 2))
  7462. (while (>= (setq n (1- n)) 0)
  7463. (org-promote))
  7464. (end-of-line 1))))))
  7465. (defun org-tr-level (n)
  7466. "Make N odd if required."
  7467. (if org-odd-levels-only (1+ (/ n 2)) n))
  7468. ;;; Vertical tree motion, cutting and pasting of subtrees
  7469. (defun org-move-subtree-up (&optional arg)
  7470. "Move the current subtree up past ARG headlines of the same level."
  7471. (interactive "p")
  7472. (org-move-subtree-down (- (prefix-numeric-value arg))))
  7473. (defun org-move-subtree-down (&optional arg)
  7474. "Move the current subtree down past ARG headlines of the same level."
  7475. (interactive "p")
  7476. (setq arg (prefix-numeric-value arg))
  7477. (let ((movfunc (if (> arg 0) 'org-get-next-sibling
  7478. 'org-get-last-sibling))
  7479. (ins-point (make-marker))
  7480. (cnt (abs arg))
  7481. (col (current-column))
  7482. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  7483. ;; Select the tree
  7484. (org-back-to-heading)
  7485. (setq beg0 (point))
  7486. (save-excursion
  7487. (setq ne-beg (org-back-over-empty-lines))
  7488. (setq beg (point)))
  7489. (save-match-data
  7490. (save-excursion (outline-end-of-heading)
  7491. (setq folded (outline-invisible-p)))
  7492. (progn (org-end-of-subtree nil t)
  7493. (unless (eobp) (backward-char))))
  7494. (outline-next-heading)
  7495. (setq ne-end (org-back-over-empty-lines))
  7496. (setq end (point))
  7497. (goto-char beg0)
  7498. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  7499. ;; include less whitespace
  7500. (save-excursion
  7501. (goto-char beg)
  7502. (forward-line (- ne-beg ne-end))
  7503. (setq beg (point))))
  7504. ;; Find insertion point, with error handling
  7505. (while (> cnt 0)
  7506. (or (and (funcall movfunc) (looking-at org-outline-regexp))
  7507. (progn (goto-char beg0)
  7508. (user-error "Cannot move past superior level or buffer limit")))
  7509. (setq cnt (1- cnt)))
  7510. (if (> arg 0)
  7511. ;; Moving forward - still need to move over subtree
  7512. (progn (org-end-of-subtree t t)
  7513. (save-excursion
  7514. (org-back-over-empty-lines)
  7515. (or (bolp) (newline)))))
  7516. (setq ne-ins (org-back-over-empty-lines))
  7517. (move-marker ins-point (point))
  7518. (setq txt (buffer-substring beg end))
  7519. (org-save-markers-in-region beg end)
  7520. (delete-region beg end)
  7521. (org-remove-empty-overlays-at beg)
  7522. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  7523. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  7524. (and (not (bolp)) (looking-at "\n") (forward-char 1))
  7525. (let ((bbb (point)))
  7526. (insert-before-markers txt)
  7527. (org-reinstall-markers-in-region bbb)
  7528. (move-marker ins-point bbb))
  7529. (or (bolp) (insert "\n"))
  7530. (setq ins-end (point))
  7531. (goto-char ins-point)
  7532. (org-skip-whitespace)
  7533. (when (and (< arg 0)
  7534. (org-first-sibling-p)
  7535. (> ne-ins ne-beg))
  7536. ;; Move whitespace back to beginning
  7537. (save-excursion
  7538. (goto-char ins-end)
  7539. (let ((kill-whole-line t))
  7540. (kill-line (- ne-ins ne-beg)) (point)))
  7541. (insert (make-string (- ne-ins ne-beg) ?\n)))
  7542. (move-marker ins-point nil)
  7543. (if folded
  7544. (hide-subtree)
  7545. (org-show-entry)
  7546. (org-show-children)
  7547. (org-cycle-hide-drawers 'children))
  7548. (org-clean-visibility-after-subtree-move)
  7549. ;; move back to the initial column we were at
  7550. (move-to-column col)))
  7551. (defvar org-subtree-clip ""
  7552. "Clipboard for cut and paste of subtrees.
  7553. This is actually only a copy of the kill, because we use the normal kill
  7554. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  7555. (defvar org-subtree-clip-folded nil
  7556. "Was the last copied subtree folded?
  7557. This is used to fold the tree back after pasting.")
  7558. (defun org-cut-subtree (&optional n)
  7559. "Cut the current subtree into the clipboard.
  7560. With prefix arg N, cut this many sequential subtrees.
  7561. This is a short-hand for marking the subtree and then cutting it."
  7562. (interactive "p")
  7563. (org-copy-subtree n 'cut))
  7564. (defun org-copy-subtree (&optional n cut force-store-markers nosubtrees)
  7565. "Copy the current subtree it in the clipboard.
  7566. With prefix arg N, copy this many sequential subtrees.
  7567. This is a short-hand for marking the subtree and then copying it.
  7568. If CUT is non-nil, actually cut the subtree.
  7569. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  7570. of some markers in the region, even if CUT is non-nil. This is
  7571. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  7572. (interactive "p")
  7573. (let (beg end folded (beg0 (point)))
  7574. (if (org-called-interactively-p 'any)
  7575. (org-back-to-heading nil) ; take what looks like a subtree
  7576. (org-back-to-heading t)) ; take what is really there
  7577. (setq beg (point))
  7578. (skip-chars-forward " \t\r\n")
  7579. (save-match-data
  7580. (if nosubtrees
  7581. (outline-next-heading)
  7582. (save-excursion (outline-end-of-heading)
  7583. (setq folded (outline-invisible-p)))
  7584. (ignore-errors (org-forward-heading-same-level (1- n) t))
  7585. (org-end-of-subtree t t)))
  7586. ;; Include the end of an inlinetask
  7587. (when (and (featurep 'org-inlinetask)
  7588. (looking-at-p (concat (org-inlinetask-outline-regexp)
  7589. "END[ \t]*$")))
  7590. (end-of-line))
  7591. (setq end (point))
  7592. (goto-char beg0)
  7593. (when (> end beg)
  7594. (setq org-subtree-clip-folded folded)
  7595. (when (or cut force-store-markers)
  7596. (org-save-markers-in-region beg end))
  7597. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  7598. (setq org-subtree-clip (current-kill 0))
  7599. (message "%s: Subtree(s) with %d characters"
  7600. (if cut "Cut" "Copied")
  7601. (length org-subtree-clip)))))
  7602. (defun org-paste-subtree (&optional level tree for-yank remove)
  7603. "Paste the clipboard as a subtree, with modification of headline level.
  7604. The entire subtree is promoted or demoted in order to match a new headline
  7605. level.
  7606. If the cursor is at the beginning of a headline, the same level as
  7607. that headline is used to paste the tree.
  7608. If not, the new level is derived from the *visible* headings
  7609. before and after the insertion point, and taken to be the inferior headline
  7610. level of the two. So if the previous visible heading is level 3 and the
  7611. next is level 4 (or vice versa), level 4 will be used for insertion.
  7612. This makes sure that the subtree remains an independent subtree and does
  7613. not swallow low level entries.
  7614. You can also force a different level, either by using a numeric prefix
  7615. argument, or by inserting the heading marker by hand. For example, if the
  7616. cursor is after \"*****\", then the tree will be shifted to level 5.
  7617. If optional TREE is given, use this text instead of the kill ring.
  7618. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  7619. move back over whitespace before inserting, and move point to the end of
  7620. the inserted text when done.
  7621. When REMOVE is non-nil, remove the subtree from the clipboard."
  7622. (interactive "P")
  7623. (setq tree (or tree (and kill-ring (current-kill 0))))
  7624. (unless (org-kill-is-subtree-p tree)
  7625. (user-error "%s"
  7626. (substitute-command-keys
  7627. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  7628. (org-with-limited-levels
  7629. (let* ((visp (not (outline-invisible-p)))
  7630. (txt tree)
  7631. (^re_ "\\(\\*+\\)[ \t]*")
  7632. (old-level (if (string-match org-outline-regexp-bol txt)
  7633. (- (match-end 0) (match-beginning 0) 1)
  7634. -1))
  7635. (force-level (cond (level (prefix-numeric-value level))
  7636. ((and (looking-at "[ \t]*$")
  7637. (string-match
  7638. "^\\*+$" (buffer-substring
  7639. (point-at-bol) (point))))
  7640. (- (match-end 0) (match-beginning 0)))
  7641. ((and (bolp)
  7642. (looking-at org-outline-regexp))
  7643. (- (match-end 0) (point) 1))))
  7644. (previous-level (save-excursion
  7645. (condition-case nil
  7646. (progn
  7647. (outline-previous-visible-heading 1)
  7648. (if (looking-at ^re_)
  7649. (- (match-end 0) (match-beginning 0) 1)
  7650. 1))
  7651. (error 1))))
  7652. (next-level (save-excursion
  7653. (condition-case nil
  7654. (progn
  7655. (or (looking-at org-outline-regexp)
  7656. (outline-next-visible-heading 1))
  7657. (if (looking-at ^re_)
  7658. (- (match-end 0) (match-beginning 0) 1)
  7659. 1))
  7660. (error 1))))
  7661. (new-level (or force-level (max previous-level next-level)))
  7662. (shift (if (or (= old-level -1)
  7663. (= new-level -1)
  7664. (= old-level new-level))
  7665. 0
  7666. (- new-level old-level)))
  7667. (delta (if (> shift 0) -1 1))
  7668. (func (if (> shift 0) 'org-demote 'org-promote))
  7669. (org-odd-levels-only nil)
  7670. beg end newend)
  7671. ;; Remove the forced level indicator
  7672. (if force-level
  7673. (delete-region (point-at-bol) (point)))
  7674. ;; Paste
  7675. (beginning-of-line (if (bolp) 1 2))
  7676. (setq beg (point))
  7677. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  7678. (insert-before-markers txt)
  7679. (unless (string-match "\n\\'" txt) (insert "\n"))
  7680. (setq newend (point))
  7681. (org-reinstall-markers-in-region beg)
  7682. (setq end (point))
  7683. (goto-char beg)
  7684. (skip-chars-forward " \t\n\r")
  7685. (setq beg (point))
  7686. (if (and (outline-invisible-p) visp)
  7687. (save-excursion (outline-show-heading)))
  7688. ;; Shift if necessary
  7689. (unless (= shift 0)
  7690. (save-restriction
  7691. (narrow-to-region beg end)
  7692. (while (not (= shift 0))
  7693. (org-map-region func (point-min) (point-max))
  7694. (setq shift (+ delta shift)))
  7695. (goto-char (point-min))
  7696. (setq newend (point-max))))
  7697. (when (or (org-called-interactively-p 'interactive) for-yank)
  7698. (message "Clipboard pasted as level %d subtree" new-level))
  7699. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  7700. kill-ring
  7701. (eq org-subtree-clip (current-kill 0))
  7702. org-subtree-clip-folded)
  7703. ;; The tree was folded before it was killed/copied
  7704. (hide-subtree))
  7705. (and for-yank (goto-char newend))
  7706. (and remove (setq kill-ring (cdr kill-ring))))))
  7707. (defun org-kill-is-subtree-p (&optional txt)
  7708. "Check if the current kill is an outline subtree, or a set of trees.
  7709. Returns nil if kill does not start with a headline, or if the first
  7710. headline level is not the largest headline level in the tree.
  7711. So this will actually accept several entries of equal levels as well,
  7712. which is OK for `org-paste-subtree'.
  7713. If optional TXT is given, check this string instead of the current kill."
  7714. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  7715. (re (org-get-limited-outline-regexp))
  7716. (^re (concat "^" re))
  7717. (start-level (and kill
  7718. (string-match
  7719. (concat "\\`\\([ \t\n\r]*?\n\\)?\\(" re "\\)")
  7720. kill)
  7721. (- (match-end 2) (match-beginning 2) 1)))
  7722. (start (1+ (or (match-beginning 2) -1))))
  7723. (if (not start-level)
  7724. (progn
  7725. nil) ;; does not even start with a heading
  7726. (catch 'exit
  7727. (while (setq start (string-match ^re kill (1+ start)))
  7728. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  7729. (throw 'exit nil)))
  7730. t))))
  7731. (defvar org-markers-to-move nil
  7732. "Markers that should be moved with a cut-and-paste operation.
  7733. Those markers are stored together with their positions relative to
  7734. the start of the region.")
  7735. (defun org-save-markers-in-region (beg end)
  7736. "Check markers in region.
  7737. If these markers are between BEG and END, record their position relative
  7738. to BEG, so that after moving the block of text, we can put the markers back
  7739. into place.
  7740. This function gets called just before an entry or tree gets cut from the
  7741. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  7742. called immediately, to move the markers with the entries."
  7743. (setq org-markers-to-move nil)
  7744. (when (featurep 'org-clock)
  7745. (org-clock-save-markers-for-cut-and-paste beg end))
  7746. (when (featurep 'org-agenda)
  7747. (org-agenda-save-markers-for-cut-and-paste beg end)))
  7748. (defun org-check-and-save-marker (marker beg end)
  7749. "Check if MARKER is between BEG and END.
  7750. If yes, remember the marker and the distance to BEG."
  7751. (when (and (marker-buffer marker)
  7752. (equal (marker-buffer marker) (current-buffer)))
  7753. (if (and (>= marker beg) (< marker end))
  7754. (push (cons marker (- marker beg)) org-markers-to-move))))
  7755. (defun org-reinstall-markers-in-region (beg)
  7756. "Move all remembered markers to their position relative to BEG."
  7757. (mapc (lambda (x)
  7758. (move-marker (car x) (+ beg (cdr x))))
  7759. org-markers-to-move)
  7760. (setq org-markers-to-move nil))
  7761. (defun org-narrow-to-subtree ()
  7762. "Narrow buffer to the current subtree."
  7763. (interactive)
  7764. (save-excursion
  7765. (save-match-data
  7766. (org-with-limited-levels
  7767. (narrow-to-region
  7768. (progn (org-back-to-heading t) (point))
  7769. (progn (org-end-of-subtree t t)
  7770. (if (and (org-at-heading-p) (not (eobp))) (backward-char 1))
  7771. (point)))))))
  7772. (defun org-narrow-to-block ()
  7773. "Narrow buffer to the current block."
  7774. (interactive)
  7775. (let* ((case-fold-search t)
  7776. (blockp (org-between-regexps-p "^[ \t]*#\\+begin_.*"
  7777. "^[ \t]*#\\+end_.*")))
  7778. (if blockp
  7779. (narrow-to-region (car blockp) (cdr blockp))
  7780. (user-error "Not in a block"))))
  7781. (defun org-clone-subtree-with-time-shift (n &optional shift)
  7782. "Clone the task (subtree) at point N times.
  7783. The clones will be inserted as siblings.
  7784. In interactive use, the user will be prompted for the number of
  7785. clones to be produced. If the entry has a timestamp, the user
  7786. will also be prompted for a time shift, which may be a repeater
  7787. as used in time stamps, for example `+3d'. To disable this,
  7788. you can call the function with a universal prefix argument.
  7789. When a valid repeater is given and the entry contains any time
  7790. stamps, the clones will become a sequence in time, with time
  7791. stamps in the subtree shifted for each clone produced. If SHIFT
  7792. is nil or the empty string, time stamps will be left alone. The
  7793. ID property of the original subtree is removed.
  7794. If the original subtree did contain time stamps with a repeater,
  7795. the following will happen:
  7796. - the repeater will be removed in each clone
  7797. - an additional clone will be produced, with the current, unshifted
  7798. date(s) in the entry.
  7799. - the original entry will be placed *after* all the clones, with
  7800. repeater intact.
  7801. - the start days in the repeater in the original entry will be shifted
  7802. to past the last clone.
  7803. In this way you can spell out a number of instances of a repeating task,
  7804. and still retain the repeater to cover future instances of the task.
  7805. As described above, N+1 clones are produced when the original
  7806. subtree has a repeater. Setting N to 0, then, can be used to
  7807. remove the repeater from a subtree and create a shifted clone
  7808. with the original repeater."
  7809. (interactive "nNumber of clones to produce: ")
  7810. (let ((shift
  7811. (or shift
  7812. (if (and (not (equal current-prefix-arg '(4)))
  7813. (save-excursion
  7814. (re-search-forward org-ts-regexp-both
  7815. (save-excursion
  7816. (org-end-of-subtree t)
  7817. (point)) t)))
  7818. (read-from-minibuffer
  7819. "Date shift per clone (e.g. +1w, empty to copy unchanged): ")
  7820. ""))) ;; No time shift
  7821. (n-no-remove -1)
  7822. (drawer-re org-drawer-regexp)
  7823. (org-clock-re (format "^[ \t]*%s.*$" org-clock-string))
  7824. beg end template task idprop
  7825. shift-n shift-what doshift nmin nmax)
  7826. (unless (wholenump n)
  7827. (user-error "Invalid number of replications %s" n))
  7828. (if (and (setq doshift (and (stringp shift) (string-match "\\S-" shift)))
  7829. (not (string-match "\\`[ \t]*\\+?\\([0-9]+\\)\\([hdwmy]\\)[ \t]*\\'"
  7830. shift)))
  7831. (user-error "Invalid shift specification %s" shift))
  7832. (when doshift
  7833. (setq shift-n (string-to-number (match-string 1 shift))
  7834. shift-what (cdr (assoc (match-string 2 shift)
  7835. '(("d" . day) ("w" . week)
  7836. ("m" . month) ("y" . year))))))
  7837. (if (eq shift-what 'week) (setq shift-n (* 7 shift-n) shift-what 'day))
  7838. (setq nmin 1 nmax n)
  7839. (org-back-to-heading t)
  7840. (setq beg (point))
  7841. (setq idprop (org-entry-get nil "ID"))
  7842. (org-end-of-subtree t t)
  7843. (or (bolp) (insert "\n"))
  7844. (setq end (point))
  7845. (setq template (buffer-substring beg end))
  7846. (when (and doshift
  7847. (string-match "<[^<>\n]+ [.+]?\\+[0-9]+[hdwmy][^<>\n]*>" template))
  7848. (delete-region beg end)
  7849. (setq end beg)
  7850. (setq nmin 0 nmax (1+ nmax) n-no-remove nmax))
  7851. (goto-char end)
  7852. (loop for n from nmin to nmax do
  7853. ;; prepare clone
  7854. (with-temp-buffer
  7855. (insert template)
  7856. (org-mode)
  7857. (goto-char (point-min))
  7858. (org-show-subtree)
  7859. (and idprop (if org-clone-delete-id
  7860. (org-entry-delete nil "ID")
  7861. (org-id-get-create t)))
  7862. (unless (= n 0)
  7863. (while (re-search-forward org-clock-re nil t)
  7864. (kill-whole-line))
  7865. (goto-char (point-min))
  7866. (while (re-search-forward drawer-re nil t)
  7867. (org-remove-empty-drawer-at (point))))
  7868. (goto-char (point-min))
  7869. (when doshift
  7870. (while (re-search-forward org-ts-regexp-both nil t)
  7871. (org-timestamp-change (* n shift-n) shift-what))
  7872. (unless (= n n-no-remove)
  7873. (goto-char (point-min))
  7874. (while (re-search-forward org-ts-regexp nil t)
  7875. (save-excursion
  7876. (goto-char (match-beginning 0))
  7877. (if (looking-at "<[^<>\n]+\\( +[.+]?\\+[0-9]+[hdwmy]\\)")
  7878. (delete-region (match-beginning 1) (match-end 1)))))))
  7879. (setq task (buffer-string)))
  7880. (insert task))
  7881. (goto-char beg)))
  7882. ;;; Outline Sorting
  7883. (defun org-sort (with-case)
  7884. "Call `org-sort-entries', `org-table-sort-lines' or `org-sort-list'.
  7885. Optional argument WITH-CASE means sort case-sensitively."
  7886. (interactive "P")
  7887. (cond
  7888. ((org-at-table-p) (org-call-with-arg 'org-table-sort-lines with-case))
  7889. ((org-at-item-p) (org-call-with-arg 'org-sort-list with-case))
  7890. (t
  7891. (org-call-with-arg 'org-sort-entries with-case))))
  7892. (defun org-sort-remove-invisible (s)
  7893. "Remove invisible links from string S."
  7894. (remove-text-properties 0 (length s) org-rm-props s)
  7895. (while (string-match org-bracket-link-regexp s)
  7896. (setq s (replace-match (if (match-end 2)
  7897. (match-string 3 s)
  7898. (match-string 1 s))
  7899. t t s)))
  7900. (let ((st (format " %s " s)))
  7901. (while (string-match org-emph-re st)
  7902. (setq st (replace-match (format " %s " (match-string 4 st)) t t st)))
  7903. (setq s (substring st 1 -1)))
  7904. s)
  7905. (defvar org-priority-regexp) ; defined later in the file
  7906. (defvar org-after-sorting-entries-or-items-hook nil
  7907. "Hook that is run after a bunch of entries or items have been sorted.
  7908. When children are sorted, the cursor is in the parent line when this
  7909. hook gets called. When a region or a plain list is sorted, the cursor
  7910. will be in the first entry of the sorted region/list.")
  7911. (defun org-sort-entries
  7912. (&optional with-case sorting-type getkey-func compare-func property)
  7913. "Sort entries on a certain level of an outline tree.
  7914. If there is an active region, the entries in the region are sorted.
  7915. Else, if the cursor is before the first entry, sort the top-level items.
  7916. Else, the children of the entry at point are sorted.
  7917. Sorting can be alphabetically, numerically, by date/time as given by
  7918. a time stamp, by a property, by priority order, or by a custom function.
  7919. The command prompts for the sorting type unless it has been given to the
  7920. function through the SORTING-TYPE argument, which needs to be a character,
  7921. \(?n ?N ?a ?A ?t ?T ?s ?S ?d ?D ?p ?P ?o ?O ?r ?R ?f ?F ?k ?K). Here is
  7922. the precise meaning of each character:
  7923. a Alphabetically, ignoring the TODO keyword and the priority, if any.
  7924. c By creation time, which is assumed to be the first inactive time stamp
  7925. at the beginning of a line.
  7926. d By deadline date/time.
  7927. k By clocking time.
  7928. n Numerically, by converting the beginning of the entry/item to a number.
  7929. o By order of TODO keywords.
  7930. p By priority according to the cookie.
  7931. r By the value of a property.
  7932. s By scheduled date/time.
  7933. t By date/time, either the first active time stamp in the entry, or, if
  7934. none exist, by the first inactive one.
  7935. Capital letters will reverse the sort order.
  7936. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  7937. called with point at the beginning of the record. It must return either
  7938. a string or a number that should serve as the sorting key for that record.
  7939. Comparing entries ignores case by default. However, with an optional argument
  7940. WITH-CASE, the sorting considers case as well.
  7941. Sorting is done against the visible part of the headlines, it ignores hidden
  7942. links.
  7943. When sorting is done, call `org-after-sorting-entries-or-items-hook'."
  7944. (interactive "P")
  7945. (let ((case-func (if with-case 'identity 'downcase))
  7946. (cmstr
  7947. ;; The clock marker is lost when using `sort-subr', let's
  7948. ;; store the clocking string.
  7949. (when (equal (marker-buffer org-clock-marker) (current-buffer))
  7950. (save-excursion
  7951. (goto-char org-clock-marker)
  7952. (buffer-substring-no-properties (line-beginning-position)
  7953. (point)))))
  7954. start beg end stars re re2
  7955. txt what tmp)
  7956. ;; Find beginning and end of region to sort
  7957. (cond
  7958. ((org-region-active-p)
  7959. ;; we will sort the region
  7960. (setq end (region-end)
  7961. what "region")
  7962. (goto-char (region-beginning))
  7963. (if (not (org-at-heading-p)) (outline-next-heading))
  7964. (setq start (point)))
  7965. ((or (org-at-heading-p)
  7966. (ignore-errors (progn (org-back-to-heading) t)))
  7967. ;; we will sort the children of the current headline
  7968. (org-back-to-heading)
  7969. (setq start (point)
  7970. end (progn (org-end-of-subtree t t)
  7971. (or (bolp) (insert "\n"))
  7972. (when (>= (org-back-over-empty-lines) 1)
  7973. (forward-line 1))
  7974. (point))
  7975. what "children")
  7976. (goto-char start)
  7977. (show-subtree)
  7978. (outline-next-heading))
  7979. (t
  7980. ;; we will sort the top-level entries in this file
  7981. (goto-char (point-min))
  7982. (or (org-at-heading-p) (outline-next-heading))
  7983. (setq start (point))
  7984. (goto-char (point-max))
  7985. (beginning-of-line 1)
  7986. (when (looking-at ".*?\\S-")
  7987. ;; File ends in a non-white line
  7988. (end-of-line 1)
  7989. (insert "\n"))
  7990. (setq end (point-max))
  7991. (setq what "top-level")
  7992. (goto-char start)
  7993. (show-all)))
  7994. (setq beg (point))
  7995. (when (>= beg end) (goto-char start) (user-error "Nothing to sort"))
  7996. (looking-at "\\(\\*+\\)")
  7997. (setq stars (match-string 1)
  7998. re (concat "^" (regexp-quote stars) " +")
  7999. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[ \t\n]")
  8000. txt (buffer-substring beg end))
  8001. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  8002. (if (and (not (equal stars "*")) (string-match re2 txt))
  8003. (user-error "Region to sort contains a level above the first entry"))
  8004. (unless sorting-type
  8005. (message
  8006. "Sort %s: [a]lpha [n]umeric [p]riority p[r]operty todo[o]rder [f]unc
  8007. [t]ime [s]cheduled [d]eadline [c]reated cloc[k]ing
  8008. A/N/P/R/O/F/T/S/D/C/K means reversed:"
  8009. what)
  8010. (setq sorting-type (read-char-exclusive))
  8011. (unless getkey-func
  8012. (and (= (downcase sorting-type) ?f)
  8013. (setq getkey-func
  8014. (org-icompleting-read "Sort using function: "
  8015. obarray 'fboundp t nil nil))
  8016. (setq getkey-func (intern getkey-func))))
  8017. (and (= (downcase sorting-type) ?r)
  8018. (not property)
  8019. (setq property
  8020. (org-icompleting-read "Property: "
  8021. (mapcar 'list (org-buffer-property-keys t))
  8022. nil t))))
  8023. (when (member sorting-type '(?k ?K)) (org-clock-sum))
  8024. (message "Sorting entries...")
  8025. (save-restriction
  8026. (narrow-to-region start end)
  8027. (let ((dcst (downcase sorting-type))
  8028. (case-fold-search nil)
  8029. (now (current-time)))
  8030. (sort-subr
  8031. (/= dcst sorting-type)
  8032. ;; This function moves to the beginning character of the "record" to
  8033. ;; be sorted.
  8034. (lambda nil
  8035. (if (re-search-forward re nil t)
  8036. (goto-char (match-beginning 0))
  8037. (goto-char (point-max))))
  8038. ;; This function moves to the last character of the "record" being
  8039. ;; sorted.
  8040. (lambda nil
  8041. (save-match-data
  8042. (condition-case nil
  8043. (outline-forward-same-level 1)
  8044. (error
  8045. (goto-char (point-max))))))
  8046. ;; This function returns the value that gets sorted against.
  8047. (lambda nil
  8048. (cond
  8049. ((= dcst ?n)
  8050. (if (looking-at org-complex-heading-regexp)
  8051. (string-to-number (org-sort-remove-invisible (match-string 4)))
  8052. nil))
  8053. ((= dcst ?a)
  8054. (if (looking-at org-complex-heading-regexp)
  8055. (funcall case-func (org-sort-remove-invisible (match-string 4)))
  8056. nil))
  8057. ((= dcst ?k)
  8058. (or (get-text-property (point) :org-clock-minutes) 0))
  8059. ((= dcst ?t)
  8060. (let ((end (save-excursion (outline-next-heading) (point))))
  8061. (if (or (re-search-forward org-ts-regexp end t)
  8062. (re-search-forward org-ts-regexp-both end t))
  8063. (org-time-string-to-seconds (match-string 0))
  8064. (org-float-time now))))
  8065. ((= dcst ?c)
  8066. (let ((end (save-excursion (outline-next-heading) (point))))
  8067. (if (re-search-forward
  8068. (concat "^[ \t]*\\[" org-ts-regexp1 "\\]")
  8069. end t)
  8070. (org-time-string-to-seconds (match-string 0))
  8071. (org-float-time now))))
  8072. ((= dcst ?s)
  8073. (let ((end (save-excursion (outline-next-heading) (point))))
  8074. (if (re-search-forward org-scheduled-time-regexp end t)
  8075. (org-time-string-to-seconds (match-string 1))
  8076. (org-float-time now))))
  8077. ((= dcst ?d)
  8078. (let ((end (save-excursion (outline-next-heading) (point))))
  8079. (if (re-search-forward org-deadline-time-regexp end t)
  8080. (org-time-string-to-seconds (match-string 1))
  8081. (org-float-time now))))
  8082. ((= dcst ?p)
  8083. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  8084. (string-to-char (match-string 2))
  8085. org-default-priority))
  8086. ((= dcst ?r)
  8087. (or (org-entry-get nil property) ""))
  8088. ((= dcst ?o)
  8089. (if (looking-at org-complex-heading-regexp)
  8090. (let* ((m (match-string 2))
  8091. (s (if (member m org-done-keywords) '- '+)))
  8092. (- 99 (funcall s (length (member m org-todo-keywords-1)))))))
  8093. ((= dcst ?f)
  8094. (if getkey-func
  8095. (progn
  8096. (setq tmp (funcall getkey-func))
  8097. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  8098. tmp)
  8099. (error "Invalid key function `%s'" getkey-func)))
  8100. (t (error "Invalid sorting type `%c'" sorting-type))))
  8101. nil
  8102. (cond
  8103. ((= dcst ?a) 'string<)
  8104. ((= dcst ?f) compare-func)
  8105. ((member dcst '(?p ?t ?s ?d ?c ?k)) '<)))))
  8106. (run-hooks 'org-after-sorting-entries-or-items-hook)
  8107. ;; Reset the clock marker if needed
  8108. (when cmstr
  8109. (save-excursion
  8110. (goto-char start)
  8111. (search-forward cmstr nil t)
  8112. (move-marker org-clock-marker (point))))
  8113. (message "Sorting entries...done")))
  8114. ;;; The orgstruct minor mode
  8115. ;; Define a minor mode which can be used in other modes in order to
  8116. ;; integrate the org-mode structure editing commands.
  8117. ;; This is really a hack, because the org-mode structure commands use
  8118. ;; keys which normally belong to the major mode. Here is how it
  8119. ;; works: The minor mode defines all the keys necessary to operate the
  8120. ;; structure commands, but wraps the commands into a function which
  8121. ;; tests if the cursor is currently at a headline or a plain list
  8122. ;; item. If that is the case, the structure command is used,
  8123. ;; temporarily setting many Org-mode variables like regular
  8124. ;; expressions for filling etc. However, when any of those keys is
  8125. ;; used at a different location, function uses `key-binding' to look
  8126. ;; up if the key has an associated command in another currently active
  8127. ;; keymap (minor modes, major mode, global), and executes that
  8128. ;; command. There might be problems if any of the keys is otherwise
  8129. ;; used as a prefix key.
  8130. (defcustom orgstruct-heading-prefix-regexp ""
  8131. "Regexp that matches the custom prefix of Org headlines in
  8132. orgstruct(++)-mode."
  8133. :group 'org
  8134. :version "24.4"
  8135. :package-version '(Org . "8.3")
  8136. :type 'regexp)
  8137. ;;;###autoload(put 'orgstruct-heading-prefix-regexp 'safe-local-variable 'stringp)
  8138. (defcustom orgstruct-setup-hook nil
  8139. "Hook run after orgstruct-mode-map is filled."
  8140. :group 'org
  8141. :version "24.4"
  8142. :package-version '(Org . "8.0")
  8143. :type 'hook)
  8144. (defvar orgstruct-initialized nil)
  8145. (defvar org-local-vars nil
  8146. "List of local variables, for use by `orgstruct-mode'.")
  8147. ;;;###autoload
  8148. (define-minor-mode orgstruct-mode
  8149. "Toggle the minor mode `orgstruct-mode'.
  8150. This mode is for using Org-mode structure commands in other
  8151. modes. The following keys behave as if Org-mode were active, if
  8152. the cursor is on a headline, or on a plain list item (both as
  8153. defined by Org-mode)."
  8154. nil " OrgStruct" (make-sparse-keymap)
  8155. (funcall (if orgstruct-mode
  8156. 'add-to-invisibility-spec
  8157. 'remove-from-invisibility-spec)
  8158. '(outline . t))
  8159. (when orgstruct-mode
  8160. (org-load-modules-maybe)
  8161. (unless orgstruct-initialized
  8162. (orgstruct-setup)
  8163. (setq orgstruct-initialized t))))
  8164. ;;;###autoload
  8165. (defun turn-on-orgstruct ()
  8166. "Unconditionally turn on `orgstruct-mode'."
  8167. (orgstruct-mode 1))
  8168. (defvar org-fb-vars nil)
  8169. (make-variable-buffer-local 'org-fb-vars)
  8170. (defun orgstruct++-mode (&optional arg)
  8171. "Toggle `orgstruct-mode', the enhanced version of it.
  8172. In addition to setting orgstruct-mode, this also exports all
  8173. indentation and autofilling variables from org-mode into the
  8174. buffer. It will also recognize item context in multiline items."
  8175. (interactive "P")
  8176. (setq arg (prefix-numeric-value (or arg (if orgstruct-mode -1 1))))
  8177. (if (< arg 1)
  8178. (progn (orgstruct-mode -1)
  8179. (mapc (lambda(v)
  8180. (org-set-local (car v)
  8181. (if (eq (car-safe (cadr v)) 'quote) (cadadr v) (cadr v))))
  8182. org-fb-vars))
  8183. (orgstruct-mode 1)
  8184. (setq org-fb-vars nil)
  8185. (unless org-local-vars
  8186. (setq org-local-vars (org-get-local-variables)))
  8187. (let (var val)
  8188. (mapc
  8189. (lambda (x)
  8190. (when (string-match
  8191. "^\\(paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|fill-prefix\\|indent-\\)"
  8192. (symbol-name (car x)))
  8193. (setq var (car x) val (nth 1 x))
  8194. (push (list var `(quote ,(eval var))) org-fb-vars)
  8195. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  8196. org-local-vars)
  8197. (org-set-local 'orgstruct-is-++ t))))
  8198. (defvar orgstruct-is-++ nil
  8199. "Is `orgstruct-mode' in ++ version in the current-buffer?")
  8200. (make-variable-buffer-local 'orgstruct-is-++)
  8201. ;;;###autoload
  8202. (defun turn-on-orgstruct++ ()
  8203. "Unconditionally turn on `orgstruct++-mode'."
  8204. (orgstruct++-mode 1))
  8205. (defun orgstruct-error ()
  8206. "Error when there is no default binding for a structure key."
  8207. (interactive)
  8208. (funcall (if (fboundp 'user-error)
  8209. 'user-error
  8210. 'error)
  8211. "This key has no function outside structure elements"))
  8212. (defun orgstruct-setup ()
  8213. "Setup orgstruct keymap."
  8214. (dolist (cell '((org-demote . t)
  8215. (org-metaleft . t)
  8216. (org-metaright . t)
  8217. (org-promote . t)
  8218. (org-shiftmetaleft . t)
  8219. (org-shiftmetaright . t)
  8220. org-backward-element
  8221. org-backward-heading-same-level
  8222. org-ctrl-c-ret
  8223. org-ctrl-c-minus
  8224. org-ctrl-c-star
  8225. org-cycle
  8226. org-forward-heading-same-level
  8227. org-insert-heading
  8228. org-insert-heading-respect-content
  8229. org-kill-note-or-show-branches
  8230. org-mark-subtree
  8231. org-meta-return
  8232. org-metadown
  8233. org-metaup
  8234. org-narrow-to-subtree
  8235. org-promote-subtree
  8236. org-reveal
  8237. org-shiftdown
  8238. org-shiftleft
  8239. org-shiftmetadown
  8240. org-shiftmetaup
  8241. org-shiftright
  8242. org-shifttab
  8243. org-shifttab
  8244. org-shiftup
  8245. org-show-children
  8246. org-show-subtree
  8247. org-sort
  8248. org-up-element
  8249. outline-demote
  8250. outline-next-visible-heading
  8251. outline-previous-visible-heading
  8252. outline-promote
  8253. outline-up-heading))
  8254. (let ((f (or (car-safe cell) cell))
  8255. (disable-when-heading-prefix (cdr-safe cell)))
  8256. (when (fboundp f)
  8257. (let ((new-bindings))
  8258. (dolist (binding (nconc (where-is-internal f org-mode-map)
  8259. (where-is-internal f outline-mode-map)))
  8260. (push binding new-bindings)
  8261. ;; TODO use local-function-key-map
  8262. (dolist (rep '(("<tab>" . "TAB")
  8263. ("<return>" . "RET")
  8264. ("<escape>" . "ESC")
  8265. ("<delete>" . "DEL")))
  8266. (setq binding (read-kbd-macro
  8267. (let ((case-fold-search))
  8268. (replace-regexp-in-string
  8269. (regexp-quote (cdr rep))
  8270. (car rep)
  8271. (key-description binding)))))
  8272. (pushnew binding new-bindings :test 'equal)))
  8273. (dolist (binding new-bindings)
  8274. (let ((key (lookup-key orgstruct-mode-map binding)))
  8275. (when (or (not key) (numberp key))
  8276. (ignore-errors
  8277. (org-defkey orgstruct-mode-map
  8278. binding
  8279. (orgstruct-make-binding
  8280. f binding disable-when-heading-prefix))))))))))
  8281. (run-hooks 'orgstruct-setup-hook))
  8282. (defun orgstruct-make-binding (fun key disable-when-heading-prefix)
  8283. "Create a function for binding in the structure minor mode.
  8284. FUN is the command to call inside a table. KEY is the key that
  8285. should be checked in for a command to execute outside of tables.
  8286. Non-nil `disable-when-heading-prefix' means to disable the command
  8287. if `orgstruct-heading-prefix-regexp' is not empty."
  8288. (let ((name (concat "orgstruct-hijacker-" (symbol-name fun))))
  8289. (let ((nname name)
  8290. (i 0))
  8291. (while (fboundp (intern nname))
  8292. (setq nname (format "%s-%d" name (setq i (1+ i)))))
  8293. (setq name (intern nname)))
  8294. (eval
  8295. (let ((bindings '((org-heading-regexp
  8296. (concat "^"
  8297. orgstruct-heading-prefix-regexp
  8298. "\\(\\*+\\)\\(?: +\\(.*?\\)\\)?[ ]*$"))
  8299. (org-outline-regexp
  8300. (concat orgstruct-heading-prefix-regexp "\\*+ "))
  8301. (org-outline-regexp-bol
  8302. (concat "^" org-outline-regexp))
  8303. (outline-regexp org-outline-regexp)
  8304. (outline-heading-end-regexp "\n")
  8305. (outline-level 'org-outline-level)
  8306. (outline-heading-alist))))
  8307. `(defun ,name (arg)
  8308. ,(concat "In Structure, run `" (symbol-name fun) "'.\n"
  8309. "Outside of structure, run the binding of `"
  8310. (key-description key) "'."
  8311. (when disable-when-heading-prefix
  8312. (concat
  8313. "\nIf `orgstruct-heading-prefix-regexp' is not empty, this command will always fall\n"
  8314. "back to the default binding due to limitations of Org's implementation of\n"
  8315. "`" (symbol-name fun) "'.")))
  8316. (interactive "p")
  8317. (let* ((disable
  8318. ,(and disable-when-heading-prefix
  8319. '(not (string= orgstruct-heading-prefix-regexp ""))))
  8320. (fallback
  8321. (or disable
  8322. (not
  8323. (let* ,bindings
  8324. (org-context-p 'headline 'item
  8325. ,(when (memq fun
  8326. '(org-insert-heading
  8327. org-insert-heading-respect-content
  8328. org-meta-return))
  8329. '(when orgstruct-is-++
  8330. 'item-body))))))))
  8331. (if fallback
  8332. (let* ((orgstruct-mode)
  8333. (binding
  8334. (let ((key ,key))
  8335. (catch 'exit
  8336. (dolist
  8337. (rep
  8338. '(nil
  8339. ("<\\([^>]*\\)tab>" . "\\1TAB")
  8340. ("<\\([^>]*\\)return>" . "\\1RET")
  8341. ("<\\([^>]*\\)escape>" . "\\1ESC")
  8342. ("<\\([^>]*\\)delete>" . "\\1DEL"))
  8343. nil)
  8344. (when rep
  8345. (setq key (read-kbd-macro
  8346. (let ((case-fold-search))
  8347. (replace-regexp-in-string
  8348. (car rep)
  8349. (cdr rep)
  8350. (key-description key))))))
  8351. (when (key-binding key)
  8352. (throw 'exit (key-binding key))))))))
  8353. (if (keymapp binding)
  8354. (org-set-transient-map binding)
  8355. (let ((func (or binding
  8356. (unless disable
  8357. 'orgstruct-error))))
  8358. (when func
  8359. (call-interactively func)))))
  8360. (org-run-like-in-org-mode
  8361. (lambda ()
  8362. (interactive)
  8363. (let* ,bindings
  8364. (call-interactively ',fun)))))))))
  8365. name))
  8366. (defun org-contextualize-keys (alist contexts)
  8367. "Return valid elements in ALIST depending on CONTEXTS.
  8368. `org-agenda-custom-commands' or `org-capture-templates' are the
  8369. values used for ALIST, and `org-agenda-custom-commands-contexts'
  8370. or `org-capture-templates-contexts' are the associated contexts
  8371. definitions."
  8372. (let ((contexts
  8373. ;; normalize contexts
  8374. (mapcar
  8375. (lambda(c) (cond ((listp (cadr c))
  8376. (list (car c) (car c) (cadr c)))
  8377. ((string= "" (cadr c))
  8378. (list (car c) (car c) (caddr c)))
  8379. (t c)))
  8380. contexts))
  8381. (a alist) r s)
  8382. ;; loop over all commands or templates
  8383. (dolist (c a)
  8384. (let (vrules repl)
  8385. (cond
  8386. ((not (assoc (car c) contexts))
  8387. (push c r))
  8388. ((and (assoc (car c) contexts)
  8389. (setq vrules (org-contextualize-validate-key
  8390. (car c) contexts)))
  8391. (mapc (lambda (vr)
  8392. (when (not (equal (car vr) (cadr vr)))
  8393. (setq repl vr)))
  8394. vrules)
  8395. (if (not repl) (push c r)
  8396. (push (cadr repl) s)
  8397. (push
  8398. (cons (car c)
  8399. (cdr (or (assoc (cadr repl) alist)
  8400. (error "Undefined key `%s' as contextual replacement for `%s'"
  8401. (cadr repl) (car c)))))
  8402. r))))))
  8403. ;; Return limited ALIST, possibly with keys modified, and deduplicated
  8404. (delq
  8405. nil
  8406. (delete-dups
  8407. (mapcar (lambda (x)
  8408. (let ((tpl (car x)))
  8409. (when (not (delq
  8410. nil
  8411. (mapcar (lambda (y)
  8412. (equal y tpl))
  8413. s)))
  8414. x)))
  8415. (reverse r))))))
  8416. (defun org-contextualize-validate-key (key contexts)
  8417. "Check CONTEXTS for agenda or capture KEY."
  8418. (let (rr res)
  8419. (dolist (r contexts)
  8420. (mapc
  8421. (lambda (rr)
  8422. (when
  8423. (and (equal key (car r))
  8424. (if (functionp rr) (funcall rr)
  8425. (or (and (eq (car rr) 'in-file)
  8426. (buffer-file-name)
  8427. (string-match (cdr rr) (buffer-file-name)))
  8428. (and (eq (car rr) 'in-mode)
  8429. (string-match (cdr rr) (symbol-name major-mode)))
  8430. (and (eq (car rr) 'in-buffer)
  8431. (string-match (cdr rr) (buffer-name)))
  8432. (when (and (eq (car rr) 'not-in-file)
  8433. (buffer-file-name))
  8434. (not (string-match (cdr rr) (buffer-file-name))))
  8435. (when (eq (car rr) 'not-in-mode)
  8436. (not (string-match (cdr rr) (symbol-name major-mode))))
  8437. (when (eq (car rr) 'not-in-buffer)
  8438. (not (string-match (cdr rr) (buffer-name)))))))
  8439. (push r res)))
  8440. (car (last r))))
  8441. (delete-dups (delq nil res))))
  8442. (defun org-context-p (&rest contexts)
  8443. "Check if local context is any of CONTEXTS.
  8444. Possible values in the list of contexts are `table', `headline', and `item'."
  8445. (let ((pos (point)))
  8446. (goto-char (point-at-bol))
  8447. (prog1 (or (and (memq 'table contexts)
  8448. (looking-at "[ \t]*|"))
  8449. (and (memq 'headline contexts)
  8450. (looking-at org-outline-regexp))
  8451. (and (memq 'item contexts)
  8452. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)"))
  8453. (and (memq 'item-body contexts)
  8454. (org-in-item-p)))
  8455. (goto-char pos))))
  8456. (defun org-get-local-variables ()
  8457. "Return a list of all local variables in an Org mode buffer."
  8458. (let (varlist)
  8459. (with-current-buffer (get-buffer-create "*Org tmp*")
  8460. (erase-buffer)
  8461. (org-mode)
  8462. (setq varlist (buffer-local-variables)))
  8463. (kill-buffer "*Org tmp*")
  8464. (delq nil
  8465. (mapcar
  8466. (lambda (x)
  8467. (setq x
  8468. (if (symbolp x)
  8469. (list x)
  8470. (list (car x) (cdr x))))
  8471. (if (and (not (get (car x) 'org-state))
  8472. (string-match
  8473. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|normal-auto-fill\\|fill-paragraph\\|indent-\\)"
  8474. (symbol-name (car x))))
  8475. x nil))
  8476. varlist))))
  8477. (defun org-clone-local-variables (from-buffer &optional regexp)
  8478. "Clone local variables from FROM-BUFFER.
  8479. Optional argument REGEXP selects variables to clone."
  8480. (mapc
  8481. (lambda (pair)
  8482. (and (symbolp (car pair))
  8483. (or (null regexp)
  8484. (string-match regexp (symbol-name (car pair))))
  8485. (set (make-local-variable (car pair))
  8486. (cdr pair))))
  8487. (buffer-local-variables from-buffer)))
  8488. ;;;###autoload
  8489. (defun org-run-like-in-org-mode (cmd)
  8490. "Run a command, pretending that the current buffer is in Org-mode.
  8491. This will temporarily bind local variables that are typically bound in
  8492. Org-mode to the values they have in Org-mode, and then interactively
  8493. call CMD."
  8494. (org-load-modules-maybe)
  8495. (unless org-local-vars
  8496. (setq org-local-vars (org-get-local-variables)))
  8497. (let (binds)
  8498. (dolist (var org-local-vars)
  8499. (when (or (not (boundp (car var)))
  8500. (eq (symbol-value (car var))
  8501. (default-value (car var))))
  8502. (push (list (car var) `(quote ,(cadr var))) binds)))
  8503. (eval `(let ,binds
  8504. (call-interactively (quote ,cmd))))))
  8505. (defun org-get-category (&optional pos force-refresh)
  8506. "Get the category applying to position POS."
  8507. (save-match-data
  8508. (if force-refresh (org-refresh-category-properties))
  8509. (let ((pos (or pos (point))))
  8510. (or (get-text-property pos 'org-category)
  8511. (progn (org-refresh-category-properties)
  8512. (get-text-property pos 'org-category))))))
  8513. ;;; Refresh properties
  8514. (defun org-refresh-properties (dprop tprop)
  8515. "Refresh buffer text properties.
  8516. DPROP is the drawer property and TPROP is either the
  8517. corresponding text property to set, or an alist with each element
  8518. being a text property (as a symbol) and a function to apply to
  8519. the value of the drawer property."
  8520. (let ((case-fold-search t)
  8521. (inhibit-read-only t))
  8522. (org-with-silent-modifications
  8523. (save-excursion
  8524. (save-restriction
  8525. (widen)
  8526. (goto-char (point-min))
  8527. (while (re-search-forward (concat "^[ \t]*:" dprop ": +\\(.*\\)[ \t]*$") nil t)
  8528. (org-refresh-property tprop (org-match-string-no-properties 1))))))))
  8529. (defun org-refresh-property (tprop p)
  8530. "Refresh the buffer text property TPROP from the drawer property P.
  8531. The refresh happens only for the current tree (not subtree)."
  8532. (unless (org-before-first-heading-p)
  8533. (save-excursion
  8534. (org-back-to-heading t)
  8535. (if (symbolp tprop)
  8536. ;; TPROP is a text property symbol
  8537. (put-text-property
  8538. (point) (or (outline-next-heading) (point-max)) tprop p)
  8539. ;; TPROP is an alist with (properties . function) elements
  8540. (dolist (al tprop)
  8541. (save-excursion
  8542. (put-text-property
  8543. (line-beginning-position) (or (outline-next-heading) (point-max))
  8544. (car al)
  8545. (funcall (cdr al) p))))))))
  8546. (defun org-refresh-category-properties ()
  8547. "Refresh category text properties in the buffer."
  8548. (let ((case-fold-search t)
  8549. (inhibit-read-only t)
  8550. (default-category
  8551. (cond ((null org-category)
  8552. (if buffer-file-name
  8553. (file-name-sans-extension
  8554. (file-name-nondirectory buffer-file-name))
  8555. "???"))
  8556. ((symbolp org-category) (symbol-name org-category))
  8557. (t org-category))))
  8558. (org-with-silent-modifications
  8559. (org-with-wide-buffer
  8560. ;; Set buffer-wide category. Search last #+CATEGORY keyword.
  8561. ;; This is the default category for the buffer. If none is
  8562. ;; found, fall-back to `org-category' or buffer file name.
  8563. (put-text-property
  8564. (point-min) (point-max)
  8565. 'org-category
  8566. (catch 'buffer-category
  8567. (goto-char (point-max))
  8568. (while (re-search-backward "^[ \t]*#\\+CATEGORY:" (point-min) t)
  8569. (let ((element (org-element-at-point)))
  8570. (when (eq (org-element-type element) 'keyword)
  8571. (throw 'buffer-category
  8572. (org-element-property :value element)))))
  8573. default-category))
  8574. ;; Set sub-tree specific categories.
  8575. (goto-char (point-min))
  8576. (let ((regexp (org-re-property "CATEGORY")))
  8577. (while (re-search-forward regexp nil t)
  8578. (let ((value (org-match-string-no-properties 3)))
  8579. (when (org-at-property-p)
  8580. (put-text-property
  8581. (save-excursion (org-back-to-heading t) (point))
  8582. (save-excursion (org-end-of-subtree t t) (point))
  8583. 'org-category
  8584. value)))))))))
  8585. (defun org-refresh-stats-properties ()
  8586. "Refresh stats text properties in the buffer."
  8587. (let (stats)
  8588. (org-with-silent-modifications
  8589. (save-excursion
  8590. (save-restriction
  8591. (widen)
  8592. (goto-char (point-min))
  8593. (while (re-search-forward
  8594. (concat org-outline-regexp-bol ".*"
  8595. "\\(?:\\[\\([0-9]+\\)%\\|\\([0-9]+\\)/\\([0-9]+\\)\\]\\)")
  8596. nil t)
  8597. (setq stats (cond ((equal (match-string 3) "0") 0)
  8598. ((match-string 2)
  8599. (/ (* (string-to-number (match-string 2)) 100)
  8600. (string-to-number (match-string 3))))
  8601. (t (string-to-number (match-string 1)))))
  8602. (org-back-to-heading t)
  8603. (put-text-property (point) (progn (org-end-of-subtree t t) (point))
  8604. 'org-stats stats)))))))
  8605. (defun org-refresh-effort-properties ()
  8606. "Refresh effort properties"
  8607. (org-refresh-properties
  8608. org-effort-property
  8609. '((effort . identity)
  8610. (effort-minutes . org-duration-string-to-minutes))))
  8611. ;;;; Link Stuff
  8612. ;;; Link abbreviations
  8613. (defun org-link-expand-abbrev (link)
  8614. "Apply replacements as defined in `org-link-abbrev-alist'."
  8615. (if (string-match "^\\([^:]*\\)\\(::?\\(.*\\)\\)?$" link)
  8616. (let* ((key (match-string 1 link))
  8617. (as (or (assoc key org-link-abbrev-alist-local)
  8618. (assoc key org-link-abbrev-alist)))
  8619. (tag (and (match-end 2) (match-string 3 link)))
  8620. rpl)
  8621. (if (not as)
  8622. link
  8623. (setq rpl (cdr as))
  8624. (cond
  8625. ((symbolp rpl) (funcall rpl tag))
  8626. ((string-match "%(\\([^)]+\\))" rpl)
  8627. (replace-match
  8628. (save-match-data
  8629. (funcall (intern-soft (match-string 1 rpl)) tag)) t t rpl))
  8630. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  8631. ((string-match "%h" rpl)
  8632. (replace-match (url-hexify-string (or tag "")) t t rpl))
  8633. (t (concat rpl tag)))))
  8634. link))
  8635. ;;; Storing and inserting links
  8636. (defvar org-insert-link-history nil
  8637. "Minibuffer history for links inserted with `org-insert-link'.")
  8638. (defvar org-stored-links nil
  8639. "Contains the links stored with `org-store-link'.")
  8640. (defvar org-store-link-plist nil
  8641. "Plist with info about the most recently link created with `org-store-link'.")
  8642. (defvar org-link-protocols nil
  8643. "Link protocols added to Org-mode using `org-add-link-type'.")
  8644. (defvar org-store-link-functions nil
  8645. "List of functions that are called to create and store a link.
  8646. Each function will be called in turn until one returns a non-nil
  8647. value. Each function should check if it is responsible for creating
  8648. this link (for example by looking at the major mode).
  8649. If not, it must exit and return nil.
  8650. If yes, it should return a non-nil value after a calling
  8651. `org-store-link-props' with a list of properties and values.
  8652. Special properties are:
  8653. :type The link prefix, like \"http\". This must be given.
  8654. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  8655. This is obligatory as well.
  8656. :description Optional default description for the second pair
  8657. of brackets in an Org-mode link. The user can still change
  8658. this when inserting this link into an Org-mode buffer.
  8659. In addition to these, any additional properties can be specified
  8660. and then used in capture templates.")
  8661. (defun org-add-link-type (type &optional follow export)
  8662. "Add TYPE to the list of `org-link-types'.
  8663. Re-compute all regular expressions depending on `org-link-types'
  8664. FOLLOW and EXPORT are two functions.
  8665. FOLLOW should take the link path as the single argument and do whatever
  8666. is necessary to follow the link, for example find a file or display
  8667. a mail message.
  8668. EXPORT should format the link path for export to one of the export formats.
  8669. It should be a function accepting three arguments:
  8670. path the path of the link, the text after the prefix (like \"http:\")
  8671. desc the description of the link, if any
  8672. format the export format, a symbol like `html' or `latex' or `ascii'.
  8673. The function may use the FORMAT information to return different values
  8674. depending on the format. The return value will be put literally into
  8675. the exported file. If the return value is nil, this means Org should
  8676. do what it normally does with links which do not have EXPORT defined.
  8677. Org mode has a built-in default for exporting links. If you are happy with
  8678. this default, there is no need to define an export function for the link
  8679. type. For a simple example of an export function, see `org-bbdb.el'."
  8680. (add-to-list 'org-link-types type t)
  8681. (org-make-link-regexps)
  8682. (org-element-update-syntax)
  8683. (if (assoc type org-link-protocols)
  8684. (setcdr (assoc type org-link-protocols) (list follow export))
  8685. (push (list type follow export) org-link-protocols)))
  8686. (defvar org-agenda-buffer-name) ; Defined in org-agenda.el
  8687. (defvar org-id-link-to-org-use-id) ; Defined in org-id.el
  8688. ;;;###autoload
  8689. (defun org-store-link (arg)
  8690. "\\<org-mode-map>Store an org-link to the current location.
  8691. This link is added to `org-stored-links' and can later be inserted
  8692. into an Org buffer with \\[org-insert-link].
  8693. For some link types, a prefix ARG is interpreted.
  8694. For links to Usenet articles, ARG negates `org-gnus-prefer-web-links'.
  8695. For file links, ARG negates `org-context-in-file-links'.
  8696. A double prefix ARG force skipping storing functions that are not
  8697. part of Org's core.
  8698. A triple prefix ARG force storing a link for each line in the
  8699. active region."
  8700. (interactive "P")
  8701. (org-load-modules-maybe)
  8702. (if (and (equal arg '(64)) (org-region-active-p))
  8703. (save-excursion
  8704. (let ((end (region-end)))
  8705. (goto-char (region-beginning))
  8706. (set-mark (point))
  8707. (while (< (point-at-eol) end)
  8708. (move-end-of-line 1) (activate-mark)
  8709. (let (current-prefix-arg)
  8710. (call-interactively 'org-store-link))
  8711. (move-beginning-of-line 2)
  8712. (set-mark (point)))))
  8713. (org-with-limited-levels
  8714. (setq org-store-link-plist nil)
  8715. (let (link cpltxt desc description search
  8716. txt custom-id agenda-link sfuns sfunsn)
  8717. (cond
  8718. ;; Store a link using an external link type
  8719. ((and (not (equal arg '(16)))
  8720. (setq sfuns
  8721. (delq
  8722. nil (mapcar (lambda (f)
  8723. (let (fs) (if (funcall f) (push f fs))))
  8724. org-store-link-functions))
  8725. sfunsn (mapcar (lambda (fu) (symbol-name (car fu))) sfuns))
  8726. (or (and (cdr sfuns)
  8727. (funcall (intern
  8728. (completing-read
  8729. "Which function for creating the link? "
  8730. sfunsn nil t (car sfunsn)))))
  8731. (funcall (caar sfuns)))
  8732. (setq link (plist-get org-store-link-plist :link)
  8733. desc (or (plist-get org-store-link-plist
  8734. :description)
  8735. link))))
  8736. ;; Store a link from a source code buffer.
  8737. ((org-src-edit-buffer-p)
  8738. (cond
  8739. ((save-excursion
  8740. (beginning-of-line)
  8741. (looking-at (concat (format org-coderef-label-format "\\(.*?\\)")
  8742. "[ \t]*$")))
  8743. (setq link (format "(%s)" (org-match-string-no-properties 1))))
  8744. ((org-called-interactively-p 'any)
  8745. (let (label)
  8746. (while (or (not label)
  8747. (org-with-wide-buffer
  8748. (goto-char (point-min))
  8749. (re-search-forward
  8750. (regexp-quote (format org-coderef-label-format label))
  8751. nil t)))
  8752. (when label (message "Label exists already") (sit-for 2))
  8753. (setq label (read-string "Code line label: " label)))
  8754. (end-of-line)
  8755. (setq link (format org-coderef-label-format label))
  8756. (let ((gc (- 79 (length link))))
  8757. (if (< (current-column) gc) (org-move-to-column gc t)
  8758. (insert " ")))
  8759. (insert link)
  8760. (setq link (concat "(" label ")") desc nil)))
  8761. (t (setq link nil))))
  8762. ;; We are in the agenda, link to referenced location
  8763. ((equal (org-bound-and-true-p org-agenda-buffer-name) (buffer-name))
  8764. (let ((m (or (get-text-property (point) 'org-hd-marker)
  8765. (get-text-property (point) 'org-marker))))
  8766. (when m
  8767. (org-with-point-at m
  8768. (setq agenda-link
  8769. (if (org-called-interactively-p 'any)
  8770. (call-interactively 'org-store-link)
  8771. (org-store-link nil)))))))
  8772. ((eq major-mode 'calendar-mode)
  8773. (let ((cd (calendar-cursor-to-date)))
  8774. (setq link
  8775. (format-time-string
  8776. (car org-time-stamp-formats)
  8777. (apply 'encode-time
  8778. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  8779. nil nil nil))))
  8780. (org-store-link-props :type "calendar" :date cd)))
  8781. ((eq major-mode 'help-mode)
  8782. (setq link (concat "help:" (save-excursion
  8783. (goto-char (point-min))
  8784. (looking-at "^[^ ]+")
  8785. (match-string 0))))
  8786. (org-store-link-props :type "help"))
  8787. ((eq major-mode 'w3-mode)
  8788. (setq cpltxt (if (and (buffer-name)
  8789. (not (string-match "Untitled" (buffer-name))))
  8790. (buffer-name)
  8791. (url-view-url t))
  8792. link (url-view-url t))
  8793. (org-store-link-props :type "w3" :url (url-view-url t)))
  8794. ((eq major-mode 'image-mode)
  8795. (setq cpltxt (concat "file:"
  8796. (abbreviate-file-name buffer-file-name))
  8797. link cpltxt)
  8798. (org-store-link-props :type "image" :file buffer-file-name))
  8799. ;; In dired, store a link to the file of the current line
  8800. ((derived-mode-p 'dired-mode)
  8801. (let ((file (dired-get-filename nil t)))
  8802. (setq file (if file
  8803. (abbreviate-file-name
  8804. (expand-file-name (dired-get-filename nil t)))
  8805. ;; otherwise, no file so use current directory.
  8806. default-directory))
  8807. (setq cpltxt (concat "file:" file)
  8808. link cpltxt)))
  8809. ((setq search (run-hook-with-args-until-success
  8810. 'org-create-file-search-functions))
  8811. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  8812. "::" search))
  8813. (setq cpltxt (or description link)))
  8814. ((and (buffer-file-name (buffer-base-buffer)) (derived-mode-p 'org-mode))
  8815. (setq custom-id (org-entry-get nil "CUSTOM_ID"))
  8816. (cond
  8817. ;; Store a link using the target at point
  8818. ((org-in-regexp "[^<]<<\\([^<>]+\\)>>[^>]" 1)
  8819. (setq cpltxt
  8820. (concat "file:"
  8821. (abbreviate-file-name
  8822. (buffer-file-name (buffer-base-buffer)))
  8823. "::" (match-string 1))
  8824. link cpltxt))
  8825. ((and (featurep 'org-id)
  8826. (or (eq org-id-link-to-org-use-id t)
  8827. (and (org-called-interactively-p 'any)
  8828. (or (eq org-id-link-to-org-use-id 'create-if-interactive)
  8829. (and (eq org-id-link-to-org-use-id
  8830. 'create-if-interactive-and-no-custom-id)
  8831. (not custom-id))))
  8832. (and org-id-link-to-org-use-id (org-entry-get nil "ID"))))
  8833. ;; Store a link using the ID at point
  8834. (setq link (condition-case nil
  8835. (prog1 (org-id-store-link)
  8836. (setq desc (or (plist-get org-store-link-plist
  8837. :description)
  8838. "")))
  8839. (error
  8840. ;; Probably before first headline, link only to file
  8841. (concat "file:"
  8842. (abbreviate-file-name
  8843. (buffer-file-name (buffer-base-buffer))))))))
  8844. (t
  8845. ;; Just link to current headline
  8846. (setq cpltxt (concat "file:"
  8847. (abbreviate-file-name
  8848. (buffer-file-name (buffer-base-buffer)))))
  8849. ;; Add a context search string
  8850. (when (org-xor org-context-in-file-links arg)
  8851. (let* ((ee (org-element-at-point))
  8852. (et (org-element-type ee))
  8853. (ev (plist-get (cadr ee) :value))
  8854. (ek (plist-get (cadr ee) :key))
  8855. (eok (and (stringp ek) (string-match "name" ek))))
  8856. (setq txt (cond
  8857. ((org-at-heading-p) nil)
  8858. ((and (eq et 'keyword) eok) ev)
  8859. ((org-region-active-p)
  8860. (buffer-substring (region-beginning) (region-end)))))
  8861. (when (or (null txt) (string-match "\\S-" txt))
  8862. (setq cpltxt
  8863. (concat cpltxt "::"
  8864. (condition-case nil
  8865. (org-make-org-heading-search-string txt)
  8866. (error "")))
  8867. desc (or (and (eq et 'keyword) eok ev)
  8868. (nth 4 (ignore-errors (org-heading-components)))
  8869. "NONE")))))
  8870. (if (string-match "::\\'" cpltxt)
  8871. (setq cpltxt (substring cpltxt 0 -2)))
  8872. (setq link cpltxt))))
  8873. ((buffer-file-name (buffer-base-buffer))
  8874. ;; Just link to this file here.
  8875. (setq cpltxt (concat "file:"
  8876. (abbreviate-file-name
  8877. (buffer-file-name (buffer-base-buffer)))))
  8878. ;; Add a context string.
  8879. (when (org-xor org-context-in-file-links arg)
  8880. (setq txt (if (org-region-active-p)
  8881. (buffer-substring (region-beginning) (region-end))
  8882. (buffer-substring (point-at-bol) (point-at-eol))))
  8883. ;; Only use search option if there is some text.
  8884. (when (string-match "\\S-" txt)
  8885. (setq cpltxt
  8886. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  8887. desc "NONE")))
  8888. (setq link cpltxt))
  8889. ((org-called-interactively-p 'interactive)
  8890. (user-error "No method for storing a link from this buffer"))
  8891. (t (setq link nil)))
  8892. ;; We're done setting link and desc, clean up
  8893. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  8894. (setq link (or link cpltxt)
  8895. desc (or desc cpltxt))
  8896. (cond ((equal desc "NONE") (setq desc nil))
  8897. ((and desc (string-match org-bracket-link-analytic-regexp desc))
  8898. (let ((d0 (match-string 3 desc))
  8899. (p0 (match-string 5 desc)))
  8900. (setq desc
  8901. (replace-regexp-in-string
  8902. org-bracket-link-regexp
  8903. (concat (or p0 d0)
  8904. (if (equal (length (match-string 0 desc))
  8905. (length desc)) "*" "")) desc)))))
  8906. ;; Return the link
  8907. (if (not (and (or (org-called-interactively-p 'any)
  8908. executing-kbd-macro)
  8909. link))
  8910. (or agenda-link (and link (org-make-link-string link desc)))
  8911. (push (list link desc) org-stored-links)
  8912. (message "Stored: %s" (or desc link))
  8913. (when custom-id
  8914. (setq link (concat "file:" (abbreviate-file-name
  8915. (buffer-file-name)) "::#" custom-id))
  8916. (push (list link desc) org-stored-links))
  8917. (car org-stored-links))))))
  8918. (defun org-store-link-props (&rest plist)
  8919. "Store link properties, extract names and addresses."
  8920. (let (x adr)
  8921. (when (setq x (plist-get plist :from))
  8922. (setq adr (mail-extract-address-components x))
  8923. (setq plist (plist-put plist :fromname (car adr)))
  8924. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  8925. (when (setq x (plist-get plist :to))
  8926. (setq adr (mail-extract-address-components x))
  8927. (setq plist (plist-put plist :toname (car adr)))
  8928. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  8929. (let ((from (plist-get plist :from))
  8930. (to (plist-get plist :to)))
  8931. (when (and from to org-from-is-user-regexp)
  8932. (setq plist
  8933. (plist-put plist :fromto
  8934. (if (string-match org-from-is-user-regexp from)
  8935. (concat "to %t")
  8936. (concat "from %f"))))))
  8937. (setq org-store-link-plist plist))
  8938. (defun org-add-link-props (&rest plist)
  8939. "Add these properties to the link property list."
  8940. (let (key value)
  8941. (while plist
  8942. (setq key (pop plist) value (pop plist))
  8943. (setq org-store-link-plist
  8944. (plist-put org-store-link-plist key value)))))
  8945. (defun org-email-link-description (&optional fmt)
  8946. "Return the description part of an email link.
  8947. This takes information from `org-store-link-plist' and formats it
  8948. according to FMT (default from `org-email-link-description-format')."
  8949. (setq fmt (or fmt org-email-link-description-format))
  8950. (let* ((p org-store-link-plist)
  8951. (to (plist-get p :toaddress))
  8952. (from (plist-get p :fromaddress))
  8953. (table
  8954. (list
  8955. (cons "%c" (plist-get p :fromto))
  8956. (cons "%F" (plist-get p :from))
  8957. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  8958. (cons "%T" (plist-get p :to))
  8959. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  8960. (cons "%s" (plist-get p :subject))
  8961. (cons "%d" (plist-get p :date))
  8962. (cons "%m" (plist-get p :message-id)))))
  8963. (when (string-match "%c" fmt)
  8964. ;; Check if the user wrote this message
  8965. (if (and org-from-is-user-regexp from to
  8966. (save-match-data (string-match org-from-is-user-regexp from)))
  8967. (setq fmt (replace-match "to %t" t t fmt))
  8968. (setq fmt (replace-match "from %f" t t fmt))))
  8969. (org-replace-escapes fmt table)))
  8970. (defun org-make-org-heading-search-string (&optional string)
  8971. "Make search string for the current headline or STRING."
  8972. (let ((s (or string
  8973. (and (derived-mode-p 'org-mode)
  8974. (save-excursion
  8975. (org-back-to-heading t)
  8976. (org-element-property :raw-value (org-element-at-point))))))
  8977. (lines org-context-in-file-links))
  8978. (or string (setq s (concat "*" s))) ; Add * for headlines
  8979. (setq s (replace-regexp-in-string "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" "" s))
  8980. (when (and string (integerp lines) (> lines 0))
  8981. (let ((slines (org-split-string s "\n")))
  8982. (when (< lines (length slines))
  8983. (setq s (mapconcat
  8984. 'identity
  8985. (reverse (nthcdr (- (length slines) lines)
  8986. (reverse slines))) "\n")))))
  8987. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  8988. (defun org-make-link-string (link &optional description)
  8989. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  8990. (unless (org-string-nw-p link) (error "Empty link"))
  8991. (let ((uri (cond ((string-match org-link-types-re link)
  8992. (concat (match-string 1 link)
  8993. (org-link-escape (substring link (match-end 1)))))
  8994. ;; For readability, url-encode internal links only
  8995. ;; when absolutely needed (i.e, when they contain
  8996. ;; square brackets). File links however, are
  8997. ;; encoded since, e.g., spaces are significant.
  8998. ((or (file-name-absolute-p link)
  8999. (org-string-match-p "\\`\\.\\.?/\\|[][]" link))
  9000. (org-link-escape link))
  9001. (t link)))
  9002. (description
  9003. (and (org-string-nw-p description)
  9004. ;; Remove brackets from description, as they are fatal.
  9005. (replace-regexp-in-string
  9006. "[][]" (lambda (m) (if (equal "[" m) "{" "}"))
  9007. (org-trim description)))))
  9008. (format "[[%s]%s]"
  9009. uri
  9010. (if description (format "[%s]" description) ""))))
  9011. (defconst org-link-escape-chars
  9012. ;;%20 %5B %5D %25
  9013. '(?\s ?\[ ?\] ?%)
  9014. "List of characters that should be escaped in a link when stored to Org.
  9015. This is the list that is used for internal purposes.")
  9016. (defconst org-link-escape-chars-browser
  9017. ;;%20 %22
  9018. '(?\s ?\")
  9019. "List of characters to be escaped before handing over to the browser.
  9020. If you consider using this constant then you probably want to use
  9021. the function `org-link-escape-browser' instead. See there why
  9022. this constant is a candidate to be removed once Org drops support
  9023. for Emacs 24.1 and 24.2.")
  9024. (defun org-link-escape (text &optional table merge)
  9025. "Return percent escaped representation of TEXT.
  9026. TEXT is a string with the text to escape.
  9027. Optional argument TABLE is a list with characters that should be
  9028. escaped. When nil, `org-link-escape-chars' is used.
  9029. If optional argument MERGE is set, merge TABLE into
  9030. `org-link-escape-chars'."
  9031. (let ((characters-to-encode
  9032. (cond ((null table) org-link-escape-chars)
  9033. (merge (append org-link-escape-chars table))
  9034. (t table))))
  9035. (mapconcat
  9036. (lambda (c)
  9037. (if (or (memq c characters-to-encode)
  9038. (and org-url-hexify-p (or (< c 32) (> c 126))))
  9039. (mapconcat (lambda (e) (format "%%%.2X" e))
  9040. (or (encode-coding-char c 'utf-8)
  9041. (error "Unable to percent escape character: %c" c))
  9042. "")
  9043. (char-to-string c)))
  9044. text "")))
  9045. (defun org-link-escape-browser (text)
  9046. "Escape some characters before handing over to the browser.
  9047. This function is a candidate to be removed together with the
  9048. constant `org-link-escape-chars-browser' once Org drops support
  9049. for Emacs 24.1 and 24.2. All calls to this function will have to
  9050. be replaced with `url-encode-url' which is available since Emacs
  9051. 24.3.1."
  9052. ;; Example with the Org link
  9053. ;; [[http://lists.gnu.org/archive/cgi-bin/namazu.cgi?idxname=emacs-orgmode&query=%252Bsubject:"Release+8.2"]]
  9054. ;; to open the browser with +subject:"Release 8.2" filled into the
  9055. ;; query field: In this case the variable TEXT contains the
  9056. ;; unescaped [...]=%2Bsubject:"Release+8.2". Then `url-encode-url'
  9057. ;; converts correctly to [...]=%2Bsubject:%22Release+8.2%22 or
  9058. ;; `org-link-escape' with `org-link-escape-chars-browser' converts
  9059. ;; wrongly to [...]=%252Bsubject:%22Release+8.2%22.
  9060. (if (fboundp 'url-encode-url)
  9061. (url-encode-url text)
  9062. (if (org-string-match-p
  9063. (concat "[[:nonascii:]" org-link-escape-chars-browser "]")
  9064. text)
  9065. (org-link-escape text org-link-escape-chars-browser)
  9066. text)))
  9067. (defun org-link-unescape (str)
  9068. "Unhex hexified Unicode parts in string STR.
  9069. E.g. `%C3%B6' becomes the german o-Umlaut. This is the
  9070. reciprocal of `org-link-escape', which see."
  9071. (if (org-string-nw-p str)
  9072. (replace-regexp-in-string
  9073. "\\(%[0-9A-Za-z]\\{2\\}\\)+" #'org-link-unescape-compound str t t)
  9074. str))
  9075. (defun org-link-unescape-compound (hex)
  9076. "Unhexify Unicode hex-chars. E.g. `%C3%B6' is the German o-Umlaut.
  9077. Note: this function also decodes single byte encodings like
  9078. `%E1' (a-acute) if not followed by another `%[A-F0-9]{2}' group."
  9079. (save-match-data
  9080. (let* ((bytes (cdr (split-string hex "%")))
  9081. (ret "")
  9082. (eat 0)
  9083. (sum 0))
  9084. (while bytes
  9085. (let* ((val (string-to-number (pop bytes) 16))
  9086. (shift-xor
  9087. (if (= 0 eat)
  9088. (cond
  9089. ((>= val 252) (cons 6 252))
  9090. ((>= val 248) (cons 5 248))
  9091. ((>= val 240) (cons 4 240))
  9092. ((>= val 224) (cons 3 224))
  9093. ((>= val 192) (cons 2 192))
  9094. (t (cons 0 0)))
  9095. (cons 6 128))))
  9096. (if (>= val 192) (setq eat (car shift-xor)))
  9097. (setq val (logxor val (cdr shift-xor)))
  9098. (setq sum (+ (lsh sum (car shift-xor)) val))
  9099. (if (> eat 0) (setq eat (- eat 1)))
  9100. (cond
  9101. ((= 0 eat) ;multi byte
  9102. (setq ret (concat ret (org-char-to-string sum)))
  9103. (setq sum 0))
  9104. ((not bytes) ; single byte(s)
  9105. (setq ret (org-link-unescape-single-byte-sequence hex))))))
  9106. ret)))
  9107. (defun org-link-unescape-single-byte-sequence (hex)
  9108. "Unhexify hex-encoded single byte character sequences."
  9109. (mapconcat (lambda (byte)
  9110. (char-to-string (string-to-number byte 16)))
  9111. (cdr (split-string hex "%")) ""))
  9112. (defun org-xor (a b)
  9113. "Exclusive or."
  9114. (if a (not b) b))
  9115. (defun org-fixup-message-id-for-http (s)
  9116. "Replace special characters in a message id, so it can be used in an http query."
  9117. (when (string-match "%" s)
  9118. (setq s (mapconcat (lambda (c)
  9119. (if (eq c ?%)
  9120. "%25"
  9121. (char-to-string c)))
  9122. s "")))
  9123. (while (string-match "<" s)
  9124. (setq s (replace-match "%3C" t t s)))
  9125. (while (string-match ">" s)
  9126. (setq s (replace-match "%3E" t t s)))
  9127. (while (string-match "@" s)
  9128. (setq s (replace-match "%40" t t s)))
  9129. s)
  9130. (defun org-link-prettify (link)
  9131. "Return a human-readable representation of LINK.
  9132. The car of LINK must be a raw link.
  9133. The cdr of LINK must be either a link description or nil."
  9134. (let ((desc (or (cadr link) "<no description>")))
  9135. (concat (format "%-45s" (substring desc 0 (min (length desc) 40)))
  9136. "<" (car link) ">")))
  9137. ;;;###autoload
  9138. (defun org-insert-link-global ()
  9139. "Insert a link like Org-mode does.
  9140. This command can be called in any mode to insert a link in Org-mode syntax."
  9141. (interactive)
  9142. (org-load-modules-maybe)
  9143. (org-run-like-in-org-mode 'org-insert-link))
  9144. (defun org-insert-all-links (arg &optional pre post)
  9145. "Insert all links in `org-stored-links'.
  9146. When a universal prefix, do not delete the links from `org-stored-links'.
  9147. When `ARG' is a number, insert the last N link(s).
  9148. `PRE' and `POST' are optional arguments to define a string to
  9149. prepend or to append."
  9150. (interactive "P")
  9151. (let ((org-keep-stored-link-after-insertion (equal arg '(4)))
  9152. (links (copy-seq org-stored-links))
  9153. (pr (or pre "- "))
  9154. (po (or post "\n"))
  9155. (cnt 1) l)
  9156. (if (null org-stored-links)
  9157. (message "No link to insert")
  9158. (while (and (or (listp arg) (>= arg cnt))
  9159. (setq l (if (listp arg)
  9160. (pop links)
  9161. (pop org-stored-links))))
  9162. (setq cnt (1+ cnt))
  9163. (insert pr)
  9164. (org-insert-link nil (car l) (or (cadr l) "<no description>"))
  9165. (insert po)))))
  9166. (defun org-insert-last-stored-link (arg)
  9167. "Insert the last link stored in `org-stored-links'."
  9168. (interactive "p")
  9169. (org-insert-all-links arg "" "\n"))
  9170. (defun org-link-fontify-links-to-this-file ()
  9171. "Fontify links to the current file in `org-stored-links'."
  9172. (let ((f (buffer-file-name)) a b)
  9173. (setq a (mapcar (lambda(l)
  9174. (let ((ll (car l)))
  9175. (when (and (string-match "^file:\\(.+\\)::" ll)
  9176. (equal f (expand-file-name (match-string 1 ll))))
  9177. ll)))
  9178. org-stored-links))
  9179. (when (featurep 'org-id)
  9180. (setq b (mapcar (lambda(l)
  9181. (let ((ll (car l)))
  9182. (when (and (string-match "^id:\\(.+\\)$" ll)
  9183. (equal f (expand-file-name
  9184. (or (org-id-find-id-file
  9185. (match-string 1 ll)) ""))))
  9186. ll)))
  9187. org-stored-links)))
  9188. (mapcar (lambda(l)
  9189. (put-text-property 0 (length l) 'face 'font-lock-comment-face l))
  9190. (delq nil (append a b)))))
  9191. (defvar org-link-links-in-this-file nil)
  9192. (defun org-insert-link (&optional complete-file link-location default-description)
  9193. "Insert a link. At the prompt, enter the link.
  9194. Completion can be used to insert any of the link protocol prefixes like
  9195. http or ftp in use.
  9196. The history can be used to select a link previously stored with
  9197. `org-store-link'. When the empty string is entered (i.e. if you just
  9198. press RET at the prompt), the link defaults to the most recently
  9199. stored link. As SPC triggers completion in the minibuffer, you need to
  9200. use M-SPC or C-q SPC to force the insertion of a space character.
  9201. You will also be prompted for a description, and if one is given, it will
  9202. be displayed in the buffer instead of the link.
  9203. If there is already a link at point, this command will allow you to edit link
  9204. and description parts.
  9205. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  9206. be selected using completion. The path to the file will be relative to the
  9207. current directory if the file is in the current directory or a subdirectory.
  9208. Otherwise, the link will be the absolute path as completed in the minibuffer
  9209. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  9210. option `org-link-file-path-type'.
  9211. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  9212. the current directory or below.
  9213. With three \\[universal-argument] prefixes, negate the meaning of
  9214. `org-keep-stored-link-after-insertion'.
  9215. If `org-make-link-description-function' is non-nil, this function will be
  9216. called with the link target, and the result will be the default
  9217. link description.
  9218. If the LINK-LOCATION parameter is non-nil, this value will be
  9219. used as the link location instead of reading one interactively.
  9220. If the DEFAULT-DESCRIPTION parameter is non-nil, this value will
  9221. be used as the default description."
  9222. (interactive "P")
  9223. (let* ((wcf (current-window-configuration))
  9224. (origbuf (current-buffer))
  9225. (region (if (org-region-active-p)
  9226. (buffer-substring (region-beginning) (region-end))))
  9227. (remove (and region (list (region-beginning) (region-end))))
  9228. (desc region)
  9229. tmphist ; byte-compile incorrectly complains about this
  9230. (link link-location)
  9231. (abbrevs org-link-abbrev-alist-local)
  9232. entry file all-prefixes auto-desc)
  9233. (cond
  9234. (link-location) ; specified by arg, just use it.
  9235. ((org-in-regexp org-bracket-link-regexp 1)
  9236. ;; We do have a link at point, and we are going to edit it.
  9237. (setq remove (list (match-beginning 0) (match-end 0)))
  9238. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  9239. (setq link (read-string "Link: "
  9240. (org-link-unescape
  9241. (org-match-string-no-properties 1)))))
  9242. ((or (org-in-regexp org-angle-link-re)
  9243. (org-in-regexp org-plain-link-re))
  9244. ;; Convert to bracket link
  9245. (setq remove (list (match-beginning 0) (match-end 0))
  9246. link (read-string "Link: "
  9247. (org-remove-angle-brackets (match-string 0)))))
  9248. ((member complete-file '((4) (16)))
  9249. ;; Completing read for file names.
  9250. (setq link (org-file-complete-link complete-file)))
  9251. (t
  9252. ;; Read link, with completion for stored links.
  9253. (org-link-fontify-links-to-this-file)
  9254. (org-switch-to-buffer-other-window "*Org Links*")
  9255. (with-current-buffer "*Org Links*"
  9256. (erase-buffer)
  9257. (insert "Insert a link.
  9258. Use TAB to complete link prefixes, then RET for type-specific completion support\n")
  9259. (when org-stored-links
  9260. (insert "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  9261. (insert (mapconcat 'org-link-prettify
  9262. (reverse org-stored-links) "\n")))
  9263. (goto-char (point-min)))
  9264. (let ((cw (selected-window)))
  9265. (select-window (get-buffer-window "*Org Links*" 'visible))
  9266. (with-current-buffer "*Org Links*" (setq truncate-lines t))
  9267. (unless (pos-visible-in-window-p (point-max))
  9268. (org-fit-window-to-buffer))
  9269. (and (window-live-p cw) (select-window cw)))
  9270. ;; Fake a link history, containing the stored links.
  9271. (setq tmphist (append (mapcar 'car org-stored-links)
  9272. org-insert-link-history))
  9273. (setq all-prefixes (append (mapcar 'car abbrevs)
  9274. (mapcar 'car org-link-abbrev-alist)
  9275. org-link-types))
  9276. (unwind-protect
  9277. (progn
  9278. (setq link
  9279. (org-completing-read
  9280. "Link: "
  9281. (append
  9282. (mapcar (lambda (x) (concat x ":"))
  9283. all-prefixes)
  9284. (mapcar 'car org-stored-links))
  9285. nil nil nil
  9286. 'tmphist
  9287. (caar org-stored-links)))
  9288. (if (not (string-match "\\S-" link))
  9289. (user-error "No link selected"))
  9290. (mapc (lambda(l)
  9291. (when (equal link (cadr l)) (setq link (car l) auto-desc t)))
  9292. org-stored-links)
  9293. (if (or (member link all-prefixes)
  9294. (and (equal ":" (substring link -1))
  9295. (member (substring link 0 -1) all-prefixes)
  9296. (setq link (substring link 0 -1))))
  9297. (setq link (with-current-buffer origbuf
  9298. (org-link-try-special-completion link)))))
  9299. (set-window-configuration wcf)
  9300. (kill-buffer "*Org Links*"))
  9301. (setq entry (assoc link org-stored-links))
  9302. (or entry (push link org-insert-link-history))
  9303. (setq desc (or desc (nth 1 entry)))))
  9304. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  9305. (not org-keep-stored-link-after-insertion))
  9306. (setq org-stored-links (delq (assoc link org-stored-links)
  9307. org-stored-links)))
  9308. (if (and (string-match org-plain-link-re link)
  9309. (not (string-match org-ts-regexp link)))
  9310. ;; URL-like link, normalize the use of angular brackets.
  9311. (setq link (org-remove-angle-brackets link)))
  9312. ;; Check if we are linking to the current file with a search
  9313. ;; option If yes, simplify the link by using only the search
  9314. ;; option.
  9315. (when (and buffer-file-name
  9316. (string-match "^file:\\(.+?\\)::\\(.+\\)" link))
  9317. (let* ((path (match-string 1 link))
  9318. (case-fold-search nil)
  9319. (search (match-string 2 link)))
  9320. (save-match-data
  9321. (if (equal (file-truename buffer-file-name) (file-truename path))
  9322. ;; We are linking to this same file, with a search option
  9323. (setq link search)))))
  9324. ;; Check if we can/should use a relative path. If yes, simplify the link
  9325. (when (string-match "^\\(file:\\|docview:\\)\\(.*\\)" link)
  9326. (let* ((type (match-string 1 link))
  9327. (path (match-string 2 link))
  9328. (origpath path)
  9329. (case-fold-search nil))
  9330. (cond
  9331. ((or (eq org-link-file-path-type 'absolute)
  9332. (equal complete-file '(16)))
  9333. (setq path (abbreviate-file-name (expand-file-name path))))
  9334. ((eq org-link-file-path-type 'noabbrev)
  9335. (setq path (expand-file-name path)))
  9336. ((eq org-link-file-path-type 'relative)
  9337. (setq path (file-relative-name path)))
  9338. (t
  9339. (save-match-data
  9340. (if (string-match (concat "^" (regexp-quote
  9341. (expand-file-name
  9342. (file-name-as-directory
  9343. default-directory))))
  9344. (expand-file-name path))
  9345. ;; We are linking a file with relative path name.
  9346. (setq path (substring (expand-file-name path)
  9347. (match-end 0)))
  9348. (setq path (abbreviate-file-name (expand-file-name path)))))))
  9349. (setq link (concat type path))
  9350. (if (equal desc origpath)
  9351. (setq desc path))))
  9352. (if org-make-link-description-function
  9353. (setq desc
  9354. (or (condition-case nil
  9355. (funcall org-make-link-description-function link desc)
  9356. (error (progn (message "Can't get link description from `%s'"
  9357. (symbol-name org-make-link-description-function))
  9358. (sit-for 2) nil)))
  9359. (read-string "Description: " default-description)))
  9360. (if default-description (setq desc default-description)
  9361. (setq desc (or (and auto-desc desc)
  9362. (read-string "Description: " desc)))))
  9363. (unless (string-match "\\S-" desc) (setq desc nil))
  9364. (if remove (apply 'delete-region remove))
  9365. (insert (org-make-link-string link desc))))
  9366. (defun org-link-try-special-completion (type)
  9367. "If there is completion support for link type TYPE, offer it."
  9368. (let ((fun (intern (concat "org-" type "-complete-link"))))
  9369. (if (functionp fun)
  9370. (funcall fun)
  9371. (read-string "Link (no completion support): " (concat type ":")))))
  9372. (defun org-file-complete-link (&optional arg)
  9373. "Create a file link using completion."
  9374. (let ((file (org-iread-file-name "File: "))
  9375. (pwd (file-name-as-directory (expand-file-name ".")))
  9376. (pwd1 (file-name-as-directory (abbreviate-file-name
  9377. (expand-file-name ".")))))
  9378. (cond ((equal arg '(16))
  9379. (concat "file:"
  9380. (abbreviate-file-name (expand-file-name file))))
  9381. ((string-match
  9382. (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  9383. (concat "file:" (match-string 1 file)))
  9384. ((string-match
  9385. (concat "^" (regexp-quote pwd) "\\(.+\\)")
  9386. (expand-file-name file))
  9387. (concat "file:"
  9388. (match-string 1 (expand-file-name file))))
  9389. (t (concat "file:" file)))))
  9390. (defun org-iread-file-name (&rest args)
  9391. "Read-file-name using `ido-mode' speedup if available.
  9392. ARGS are arguments that may be passed to `ido-read-file-name' or `read-file-name'.
  9393. See `read-file-name' for a description of parameters."
  9394. (org-without-partial-completion
  9395. (if (and org-completion-use-ido
  9396. (fboundp 'ido-read-file-name)
  9397. (org-bound-and-true-p ido-mode)
  9398. (listp (nth 1 args)))
  9399. (let ((ido-enter-matching-directory nil))
  9400. (apply #'ido-read-file-name args))
  9401. (apply #'read-file-name args))))
  9402. (defun org-completing-read (&rest args)
  9403. "Completing-read with SPACE being a normal character."
  9404. (let ((enable-recursive-minibuffers t)
  9405. (minibuffer-local-completion-map
  9406. (copy-keymap minibuffer-local-completion-map)))
  9407. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  9408. (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
  9409. (org-defkey minibuffer-local-completion-map (kbd "C-c !") 'org-time-stamp-inactive)
  9410. (apply 'org-icompleting-read args)))
  9411. (defun org-completing-read-no-i (&rest args)
  9412. (let (org-completion-use-ido org-completion-use-iswitchb)
  9413. (apply 'org-completing-read args)))
  9414. (defun org-iswitchb-completing-read (prompt choices &rest args)
  9415. "Use iswitch as a completing-read replacement to choose from choices.
  9416. PROMPT is a string to prompt with. CHOICES is a list of strings to choose
  9417. from."
  9418. (let* ((iswitchb-use-virtual-buffers nil)
  9419. (iswitchb-make-buflist-hook
  9420. (lambda ()
  9421. (setq iswitchb-temp-buflist choices))))
  9422. (iswitchb-read-buffer prompt)))
  9423. (defun org-icompleting-read (&rest args)
  9424. "Completing-read using `ido-mode' or `iswitchb' speedups if available.
  9425. Should be called like `completing-read'."
  9426. (org-without-partial-completion
  9427. (if (not (listp (nth 1 args)))
  9428. ;; Ido only supports lists as the COLLECTION argument. Use
  9429. ;; default completion function when second argument is not
  9430. ;; a list.
  9431. (apply #'completing-read args)
  9432. (let ((ido-enter-matching-directory nil))
  9433. (apply (cond ((and org-completion-use-ido
  9434. (fboundp 'ido-completing-read)
  9435. (org-bound-and-true-p ido-mode))
  9436. #'ido-completing-read)
  9437. ((and org-completion-use-iswitchb
  9438. (org-bound-and-true-p iswitchb-mode))
  9439. #'org-iswitchb-completing-read)
  9440. (t #'completing-read))
  9441. args)))))
  9442. ;;; Opening/following a link
  9443. (defvar org-link-search-failed nil)
  9444. (defvar org-open-link-functions nil
  9445. "Hook for functions finding a plain text link.
  9446. These functions must take a single argument, the link content.
  9447. They will be called for links that look like [[link text][description]]
  9448. when LINK TEXT does not have a protocol like \"http:\" and does not look
  9449. like a filename (e.g. \"./blue.png\").
  9450. These functions will be called *before* Org attempts to resolve the
  9451. link by doing text searches in the current buffer - so if you want a
  9452. link \"[[target]]\" to still find \"<<target>>\", your function should
  9453. handle this as a special case.
  9454. When the function does handle the link, it must return a non-nil value.
  9455. If it decides that it is not responsible for this link, it must return
  9456. nil to indicate that that Org-mode can continue with other options
  9457. like exact and fuzzy text search.")
  9458. (defun org-next-link (&optional search-backward)
  9459. "Move forward to the next link.
  9460. If the link is in hidden text, expose it."
  9461. (interactive "P")
  9462. (when (and org-link-search-failed (eq this-command last-command))
  9463. (goto-char (point-min))
  9464. (message "Link search wrapped back to beginning of buffer"))
  9465. (setq org-link-search-failed nil)
  9466. (let* ((pos (point))
  9467. (ct (org-context))
  9468. (a (assoc :link ct))
  9469. (srch-fun (if search-backward 're-search-backward 're-search-forward)))
  9470. (cond (a (goto-char (nth (if search-backward 1 2) a)))
  9471. ((looking-at org-any-link-re)
  9472. ;; Don't stay stuck at link without an org-link face
  9473. (forward-char (if search-backward -1 1))))
  9474. (if (funcall srch-fun org-any-link-re nil t)
  9475. (progn
  9476. (goto-char (match-beginning 0))
  9477. (if (outline-invisible-p) (org-show-context)))
  9478. (goto-char pos)
  9479. (setq org-link-search-failed t)
  9480. (message "No further link found"))))
  9481. (defun org-previous-link ()
  9482. "Move backward to the previous link.
  9483. If the link is in hidden text, expose it."
  9484. (interactive)
  9485. (funcall 'org-next-link t))
  9486. (defun org-translate-link (s)
  9487. "Translate a link string if a translation function has been defined."
  9488. (with-temp-buffer
  9489. (insert (org-trim s))
  9490. (org-trim (org-element-interpret-data (org-element-context)))))
  9491. (defun org-translate-link-from-planner (type path)
  9492. "Translate a link from Emacs Planner syntax so that Org can follow it.
  9493. This is still an experimental function, your mileage may vary."
  9494. (cond
  9495. ((member type '("http" "https" "news" "ftp"))
  9496. ;; standard Internet links are the same.
  9497. nil)
  9498. ((and (equal type "irc") (string-match "^//" path))
  9499. ;; Planner has two / at the beginning of an irc link, we have 1.
  9500. ;; We should have zero, actually....
  9501. (setq path (substring path 1)))
  9502. ((and (equal type "lisp") (string-match "^/" path))
  9503. ;; Planner has a slash, we do not.
  9504. (setq type "elisp" path (substring path 1)))
  9505. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  9506. ;; A typical message link. Planner has the id after the final slash,
  9507. ;; we separate it with a hash mark
  9508. (setq path (concat (match-string 1 path) "#"
  9509. (org-remove-angle-brackets (match-string 2 path))))))
  9510. (cons type path))
  9511. (defun org-find-file-at-mouse (ev)
  9512. "Open file link or URL at mouse."
  9513. (interactive "e")
  9514. (mouse-set-point ev)
  9515. (org-open-at-point 'in-emacs))
  9516. (defun org-open-at-mouse (ev)
  9517. "Open file link or URL at mouse.
  9518. See the docstring of `org-open-file' for details."
  9519. (interactive "e")
  9520. (mouse-set-point ev)
  9521. (if (eq major-mode 'org-agenda-mode)
  9522. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  9523. (org-open-at-point))
  9524. (defvar org-window-config-before-follow-link nil
  9525. "The window configuration before following a link.
  9526. This is saved in case the need arises to restore it.")
  9527. ;;;###autoload
  9528. (defun org-open-at-point-global ()
  9529. "Follow a link like Org-mode does.
  9530. This command can be called in any mode to follow a link that has
  9531. Org-mode syntax."
  9532. (interactive)
  9533. (org-run-like-in-org-mode 'org-open-at-point))
  9534. ;;;###autoload
  9535. (defun org-open-link-from-string (s &optional arg reference-buffer)
  9536. "Open a link in the string S, as if it was in Org-mode."
  9537. (interactive "sLink: \nP")
  9538. (let ((reference-buffer (or reference-buffer (current-buffer))))
  9539. (with-temp-buffer
  9540. (let ((org-inhibit-startup (not reference-buffer)))
  9541. (org-mode)
  9542. (insert s)
  9543. (goto-char (point-min))
  9544. (when reference-buffer
  9545. (setq org-link-abbrev-alist-local
  9546. (with-current-buffer reference-buffer
  9547. org-link-abbrev-alist-local)))
  9548. (org-open-at-point arg reference-buffer)))))
  9549. (defvar org-open-at-point-functions nil
  9550. "Hook that is run when following a link at point.
  9551. Functions in this hook must return t if they identify and follow
  9552. a link at point. If they don't find anything interesting at point,
  9553. they must return nil.")
  9554. (defvar org-link-search-inhibit-query nil) ;; dynamically scoped
  9555. (defvar clean-buffer-list-kill-buffer-names) ; Defined in midnight.el
  9556. (defun org-open-at-point (&optional arg reference-buffer)
  9557. "Open link, timestamp, footnote or tags at point.
  9558. When point is on a link, follow it. Normally, files will be
  9559. opened by an appropriate application. If the optional prefix
  9560. argument ARG is non-nil, Emacs will visit the file. With
  9561. a double prefix argument, try to open outside of Emacs, in the
  9562. application the system uses for this file type.
  9563. When point is on a timestamp, open the agenda at the day
  9564. specified.
  9565. When point is a footnote definition, move to the first reference
  9566. found. If it is on a reference, move to the associated
  9567. definition.
  9568. When point is on a headline, display a list of every link in the
  9569. entry, so it is possible to pick one, or all, of them. If point
  9570. is on a tag, call `org-tags-view' instead.
  9571. When optional argument REFERENCE-BUFFER is non-nil, it should
  9572. specify a buffer from where the link search should happen. This
  9573. is used internally by `org-open-link-from-string'.
  9574. On top of syntactically correct links, this function will open
  9575. the link at point in comments or comment blocks and the first
  9576. link in a property drawer line."
  9577. (interactive "P")
  9578. ;; On a code block, open block's results.
  9579. (unless (call-interactively 'org-babel-open-src-block-result)
  9580. (org-load-modules-maybe)
  9581. (setq org-window-config-before-follow-link (current-window-configuration))
  9582. (org-remove-occur-highlights nil nil t)
  9583. (unless (run-hook-with-args-until-success 'org-open-at-point-functions)
  9584. (let* ((context
  9585. ;; Only consider supported types, even if they are not
  9586. ;; the closest one.
  9587. (org-element-lineage
  9588. (org-element-context)
  9589. '(clock comment comment-block footnote-definition
  9590. footnote-reference headline inlinetask keyword link
  9591. node-property timestamp)
  9592. t))
  9593. (type (org-element-type context))
  9594. (value (org-element-property :value context)))
  9595. (cond
  9596. ((not context) (user-error "No link found"))
  9597. ;; Exception: open timestamps and links in properties
  9598. ;; drawers, keywords and comments.
  9599. ((memq type '(comment comment-block keyword node-property))
  9600. (cond ((org-in-regexp org-any-link-re)
  9601. (org-open-link-from-string (match-string-no-properties 0)))
  9602. ((or (org-at-timestamp-p t) (org-at-date-range-p t))
  9603. (org-follow-timestamp-link))
  9604. (t (user-error "No link found"))))
  9605. ;; On a headline or an inlinetask, but not on a timestamp,
  9606. ;; a link, a footnote reference or on tags.
  9607. ((and (memq type '(headline inlinetask))
  9608. ;; Not on tags.
  9609. (progn (save-excursion (beginning-of-line)
  9610. (looking-at org-complex-heading-regexp))
  9611. (or (not (match-beginning 5))
  9612. (< (point) (match-beginning 5)))))
  9613. (let* ((data (org-offer-links-in-entry (current-buffer) (point) arg))
  9614. (links (car data))
  9615. (links-end (cdr data)))
  9616. (if links
  9617. (dolist (link (if (stringp links) (list links) links))
  9618. (search-forward link nil links-end)
  9619. (goto-char (match-beginning 0))
  9620. (org-open-at-point))
  9621. (require 'org-attach)
  9622. (org-attach-reveal 'if-exists))))
  9623. ;; On a clock line, make sure point is on the timestamp
  9624. ;; before opening it.
  9625. ((and (eq type 'clock)
  9626. value
  9627. (>= (point) (org-element-property :begin value))
  9628. (<= (point) (org-element-property :end value)))
  9629. (org-follow-timestamp-link))
  9630. ;; Do nothing on white spaces after an object, unless point
  9631. ;; is right after it.
  9632. ((> (point)
  9633. (save-excursion
  9634. (goto-char (org-element-property :end context))
  9635. (skip-chars-backward " \t")
  9636. (point)))
  9637. (user-error "No link found"))
  9638. ((eq type 'timestamp) (org-follow-timestamp-link))
  9639. ;; On tags within a headline or an inlinetask.
  9640. ((and (memq type '(headline inlinetask))
  9641. (progn (save-excursion (beginning-of-line)
  9642. (looking-at org-complex-heading-regexp))
  9643. (and (match-beginning 5)
  9644. (>= (point) (match-beginning 5)))))
  9645. (org-tags-view arg (substring (match-string 5) 0 -1)))
  9646. ((eq type 'link)
  9647. ;; When link is located within the description of another
  9648. ;; link (e.g., an inline image), always open the parent
  9649. ;; link.
  9650. (let*((link (let ((up (org-element-property :parent context)))
  9651. (if (eq (org-element-type up) 'link) up context)))
  9652. (type (org-element-property :type link))
  9653. (path (org-link-unescape (org-element-property :path link))))
  9654. ;; Switch back to REFERENCE-BUFFER needed when called in
  9655. ;; a temporary buffer through `org-open-link-from-string'.
  9656. (with-current-buffer (or reference-buffer (current-buffer))
  9657. (cond
  9658. ((equal type "file")
  9659. (if (string-match "[*?{]" (file-name-nondirectory path))
  9660. (dired path)
  9661. ;; Look into `org-link-protocols' in order to find
  9662. ;; a DEDICATED-FUNCTION to open file. The function
  9663. ;; will be applied on raw link instead of parsed
  9664. ;; link due to the limitation in `org-add-link-type'
  9665. ;; ("open" function called with a single argument).
  9666. ;; If no such function is found, fallback to
  9667. ;; `org-open-file'.
  9668. ;;
  9669. ;; Note : "file+emacs" and "file+sys" types are
  9670. ;; hard-coded in order to escape the previous
  9671. ;; limitation.
  9672. (let* ((option (org-element-property :search-option link))
  9673. (app (org-element-property :application link))
  9674. (dedicated-function
  9675. (nth 1 (assoc app org-link-protocols))))
  9676. (if dedicated-function
  9677. (funcall dedicated-function
  9678. (concat path
  9679. (and option (concat "::" option))))
  9680. (apply #'org-open-file
  9681. path
  9682. (cond (arg)
  9683. ((equal app "emacs") 'emacs)
  9684. ((equal app "sys") 'system))
  9685. (cond ((not option) nil)
  9686. ((org-string-match-p "\\`[0-9]+\\'" option)
  9687. (list (string-to-number option)))
  9688. (t (list nil
  9689. (org-link-unescape option)))))))))
  9690. ((assoc type org-link-protocols)
  9691. (funcall (nth 1 (assoc type org-link-protocols)) path))
  9692. ((equal type "help")
  9693. (let ((f-or-v (intern path)))
  9694. (cond ((fboundp f-or-v) (describe-function f-or-v))
  9695. ((boundp f-or-v) (describe-variable f-or-v))
  9696. (t (error "Not a known function or variable")))))
  9697. ((member type '("http" "https" "ftp" "mailto" "news"))
  9698. (browse-url (org-link-escape-browser (concat type ":" path))))
  9699. ((equal type "doi")
  9700. (browse-url
  9701. (org-link-escape-browser (concat org-doi-server-url path))))
  9702. ((equal type "message") (browse-url (concat type ":" path)))
  9703. ((equal type "shell")
  9704. (let ((buf (generate-new-buffer "*Org Shell Output*"))
  9705. (cmd path))
  9706. (if (or (and (org-string-nw-p
  9707. org-confirm-shell-link-not-regexp)
  9708. (string-match
  9709. org-confirm-shell-link-not-regexp cmd))
  9710. (not org-confirm-shell-link-function)
  9711. (funcall org-confirm-shell-link-function
  9712. (format "Execute \"%s\" in shell? "
  9713. (org-add-props cmd nil
  9714. 'face 'org-warning))))
  9715. (progn
  9716. (message "Executing %s" cmd)
  9717. (shell-command cmd buf)
  9718. (when (featurep 'midnight)
  9719. (setq clean-buffer-list-kill-buffer-names
  9720. (cons (buffer-name buf)
  9721. clean-buffer-list-kill-buffer-names))))
  9722. (user-error "Abort"))))
  9723. ((equal type "elisp")
  9724. (let ((cmd path))
  9725. (if (or (and (org-string-nw-p
  9726. org-confirm-elisp-link-not-regexp)
  9727. (org-string-match-p
  9728. org-confirm-elisp-link-not-regexp cmd))
  9729. (not org-confirm-elisp-link-function)
  9730. (funcall org-confirm-elisp-link-function
  9731. (format "Execute \"%s\" as elisp? "
  9732. (org-add-props cmd nil
  9733. 'face 'org-warning))))
  9734. (message "%s => %s" cmd
  9735. (if (eq (string-to-char cmd) ?\()
  9736. (eval (read cmd))
  9737. (call-interactively (read cmd))))
  9738. (user-error "Abort"))))
  9739. ((equal type "id")
  9740. (require 'ord-id)
  9741. (funcall (nth 1 (assoc "id" org-link-protocols)) path))
  9742. ((member type '("coderef" "custom-id" "fuzzy" "radio"))
  9743. (unless (run-hook-with-args-until-success
  9744. 'org-open-link-functions path)
  9745. (if (not arg) (org-mark-ring-push)
  9746. (switch-to-buffer-other-window
  9747. (org-get-buffer-for-internal-link (current-buffer))))
  9748. (let ((destination
  9749. (org-with-wide-buffer
  9750. (if (equal type "radio")
  9751. (org-search-radio-target
  9752. (org-element-property :path link))
  9753. (org-link-search
  9754. (if (member type '("custom-id" "coderef"))
  9755. (org-element-property :raw-link link)
  9756. path)
  9757. ;; Prevent fuzzy links from matching
  9758. ;; themselves.
  9759. (and (equal type "fuzzy")
  9760. (+ 2 (org-element-property :begin link)))))
  9761. (point))))
  9762. (unless (and (<= (point-min) destination)
  9763. (>= (point-max) destination))
  9764. (widen))
  9765. (goto-char destination))))
  9766. (t (browse-url-at-point))))))
  9767. ;; On a footnote reference or at a footnote definition's label.
  9768. ((or (eq type 'footnote-reference)
  9769. (and (eq type 'footnote-definition)
  9770. (save-excursion
  9771. ;; Do not validate action when point is on the
  9772. ;; spaces right after the footnote label, in
  9773. ;; order to be on par with behaviour on links.
  9774. (skip-chars-forward " \t")
  9775. (let ((begin
  9776. (org-element-property :contents-begin context)))
  9777. (if begin (< (point) begin)
  9778. (= (org-element-property :post-affiliated context)
  9779. (line-beginning-position)))))))
  9780. (org-footnote-action))
  9781. (t (user-error "No link found")))))
  9782. (run-hook-with-args 'org-follow-link-hook)))
  9783. (defun org-offer-links-in-entry (buffer marker &optional nth zero)
  9784. "Offer links in the current entry and return the selected link.
  9785. If there is only one link, return it.
  9786. If NTH is an integer, return the NTH link found.
  9787. If ZERO is a string, check also this string for a link, and if
  9788. there is one, return it."
  9789. (with-current-buffer buffer
  9790. (save-excursion
  9791. (save-restriction
  9792. (widen)
  9793. (goto-char marker)
  9794. (let ((cnt ?0)
  9795. (in-emacs (if (integerp nth) nil nth))
  9796. have-zero end links link c)
  9797. (when (and (stringp zero) (string-match org-bracket-link-regexp zero))
  9798. (push (match-string 0 zero) links)
  9799. (setq cnt (1- cnt) have-zero t))
  9800. (save-excursion
  9801. (org-back-to-heading t)
  9802. (setq end (save-excursion (outline-next-heading) (point)))
  9803. (while (re-search-forward org-any-link-re end t)
  9804. (push (match-string 0) links))
  9805. (setq links (org-uniquify (reverse links))))
  9806. (cond
  9807. ((null links)
  9808. (message "No links"))
  9809. ((equal (length links) 1)
  9810. (setq link (car links)))
  9811. ((and (integerp nth) (>= (length links) (if have-zero (1+ nth) nth)))
  9812. (setq link (nth (if have-zero nth (1- nth)) links)))
  9813. (t ; we have to select a link
  9814. (save-excursion
  9815. (save-window-excursion
  9816. (delete-other-windows)
  9817. (with-output-to-temp-buffer "*Select Link*"
  9818. (mapc (lambda (l)
  9819. (if (not (string-match org-bracket-link-regexp l))
  9820. (princ (format "[%c] %s\n" (incf cnt)
  9821. (org-remove-angle-brackets l)))
  9822. (if (match-end 3)
  9823. (princ (format "[%c] %s (%s)\n" (incf cnt)
  9824. (match-string 3 l) (match-string 1 l)))
  9825. (princ (format "[%c] %s\n" (incf cnt)
  9826. (match-string 1 l))))))
  9827. links))
  9828. (org-fit-window-to-buffer (get-buffer-window "*Select Link*"))
  9829. (message "Select link to open, RET to open all:")
  9830. (setq c (read-char-exclusive))
  9831. (and (get-buffer "*Select Link*") (kill-buffer "*Select Link*"))))
  9832. (when (equal c ?q) (user-error "Abort"))
  9833. (if (equal c ?\C-m)
  9834. (setq link links)
  9835. (setq nth (- c ?0))
  9836. (if have-zero (setq nth (1+ nth)))
  9837. (unless (and (integerp nth) (>= (length links) nth))
  9838. (user-error "Invalid link selection"))
  9839. (setq link (nth (1- nth) links)))))
  9840. (cons link end))))))
  9841. ;; TODO: These functions are deprecated since `org-open-at-point'
  9842. ;; hard-codes behaviour for "file+emacs" and "file+sys" types.
  9843. (defun org-open-file-with-system (path)
  9844. "Open file at PATH using the system way of opening it."
  9845. (org-open-file path 'system))
  9846. (defun org-open-file-with-emacs (path)
  9847. "Open file at PATH in Emacs."
  9848. (org-open-file path 'emacs))
  9849. ;;; File search
  9850. (defvar org-create-file-search-functions nil
  9851. "List of functions to construct the right search string for a file link.
  9852. These functions are called in turn with point at the location to
  9853. which the link should point.
  9854. A function in the hook should first test if it would like to
  9855. handle this file type, for example by checking the `major-mode'
  9856. or the file extension. If it decides not to handle this file, it
  9857. should just return nil to give other functions a chance. If it
  9858. does handle the file, it must return the search string to be used
  9859. when following the link. The search string will be part of the
  9860. file link, given after a double colon, and `org-open-at-point'
  9861. will automatically search for it. If special measures must be
  9862. taken to make the search successful, another function should be
  9863. added to the companion hook `org-execute-file-search-functions',
  9864. which see.
  9865. A function in this hook may also use `setq' to set the variable
  9866. `description' to provide a suggestion for the descriptive text to
  9867. be used for this link when it gets inserted into an Org-mode
  9868. buffer with \\[org-insert-link].")
  9869. (defvar org-execute-file-search-functions nil
  9870. "List of functions to execute a file search triggered by a link.
  9871. Functions added to this hook must accept a single argument, the
  9872. search string that was part of the file link, the part after the
  9873. double colon. The function must first check if it would like to
  9874. handle this search, for example by checking the `major-mode' or
  9875. the file extension. If it decides not to handle this search, it
  9876. should just return nil to give other functions a chance. If it
  9877. does handle the search, it must return a non-nil value to keep
  9878. other functions from trying.
  9879. Each function can access the current prefix argument through the
  9880. variable `current-prefix-arg'. Note that a single prefix is used
  9881. to force opening a link in Emacs, so it may be good to only use a
  9882. numeric or double prefix to guide the search function.
  9883. In case this is needed, a function in this hook can also restore
  9884. the window configuration before `org-open-at-point' was called using:
  9885. (set-window-configuration org-window-config-before-follow-link)")
  9886. (defun org-search-radio-target (target)
  9887. "Search a radio target matching TARGET in current buffer.
  9888. White spaces are not significant."
  9889. (let ((re (format "<<<%s>>>"
  9890. (mapconcat #'regexp-quote
  9891. (org-split-string target "[ \t\n]+")
  9892. "[ \t]+\\(?:\n[ \t]*\\)?")))
  9893. (origin (point)))
  9894. (goto-char (point-min))
  9895. (catch :radio-match
  9896. (while (re-search-forward re nil t)
  9897. (backward-char)
  9898. (let ((object (org-element-context)))
  9899. (when (eq (org-element-type object) 'radio-target)
  9900. (goto-char (org-element-property :begin object))
  9901. (org-show-context 'link-search)
  9902. (throw :radio-match nil))))
  9903. (goto-char origin)
  9904. (user-error "No match for radio target: %s" target))))
  9905. (defun org-link-search (s &optional avoid-pos stealth)
  9906. "Search for a search string S.
  9907. If S starts with \"#\", it triggers a custom ID search.
  9908. If S is enclosed within parenthesis, it initiates a coderef
  9909. search.
  9910. If S is surrounded by forward slashes, it is interpreted as
  9911. a regular expression. In Org mode files, this will create an
  9912. `org-occur' sparse tree. In ordinary files, `occur' will be used
  9913. to list matches. If the current buffer is in `dired-mode', grep
  9914. will be used to search in all files.
  9915. When AVOID-POS is given, ignore matches near that position.
  9916. When optional argument STEALTH is non-nil, do not modify
  9917. visibility around point, thus ignoring `org-show-context-detail'
  9918. variable.
  9919. Search is case-insensitive and ignores white spaces. Return type
  9920. of matched result, with is either `dedicated' or `fuzzy'."
  9921. (unless (org-string-nw-p s) (error "Invalid search string \"%s\"" s))
  9922. (let* ((case-fold-search t)
  9923. (origin (point))
  9924. (normalized (replace-regexp-in-string "\n[ \t]*" " " s))
  9925. (words (org-split-string s "[ \t\n]+"))
  9926. (s-multi-re (mapconcat #'regexp-quote words "[ \t]+\\(?:\n[ \t]*\\)?"))
  9927. (s-single-re (mapconcat #'regexp-quote words "[ \t]+"))
  9928. type)
  9929. (cond
  9930. ;; Check if there are any special search functions.
  9931. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  9932. ((eq (string-to-char s) ?#)
  9933. ;; Look for a custom ID S if S starts with "#".
  9934. (let* ((id (substring normalized 1))
  9935. (match (org-find-property "CUSTOM_ID" id)))
  9936. (if match (progn (goto-char match) (setf type 'dedicated))
  9937. (error "No match for custom ID: %s" id))))
  9938. ((string-match "\\`(\\(.*\\))\\'" normalized)
  9939. ;; Look for coderef targets if S is enclosed within parenthesis.
  9940. (let ((coderef (match-string-no-properties 1 normalized))
  9941. (re (substring s-single-re 1 -1)))
  9942. (goto-char (point-min))
  9943. (catch :coderef-match
  9944. (while (re-search-forward re nil t)
  9945. (let ((element (org-element-at-point)))
  9946. (when (and (memq (org-element-type element)
  9947. '(example-block src-block))
  9948. ;; Build proper regexp according to current
  9949. ;; block's label format.
  9950. (let ((label-fmt
  9951. (regexp-quote
  9952. (or (org-element-property :label-fmt element)
  9953. org-coderef-label-format))))
  9954. (save-excursion
  9955. (beginning-of-line)
  9956. (looking-at (format ".*?\\(%s\\)[ \t]*$"
  9957. (format label-fmt coderef))))))
  9958. (setq type 'dedicated)
  9959. (goto-char (match-beginning 1))
  9960. (throw :coderef-match nil))))
  9961. (goto-char origin)
  9962. (error "No match for coderef: %s" coderef))))
  9963. ((string-match "\\`/\\(.*\\)/\\'" normalized)
  9964. ;; Look for a regular expression.
  9965. (funcall (if (derived-mode-p 'org-mode) #'org-occur #'org-do-occur)
  9966. (match-string 1 s)))
  9967. ;; Fuzzy links.
  9968. (t
  9969. (let* ((starred (eq (string-to-char normalized) ?*))
  9970. (headline-search (and (derived-mode-p 'org-mode)
  9971. (or org-link-search-must-match-exact-headline
  9972. starred))))
  9973. (cond
  9974. ;; Look for targets, only if not in a headline search.
  9975. ((and (not starred)
  9976. (let ((target (format "<<%s>>" s-multi-re)))
  9977. (catch :target-match
  9978. (goto-char (point-min))
  9979. (while (re-search-forward target nil t)
  9980. (backward-char)
  9981. (let ((context (org-element-context)))
  9982. (when (eq (org-element-type context) 'target)
  9983. (setq type 'dedicated)
  9984. (goto-char (org-element-property :begin context))
  9985. (throw :target-match t))))
  9986. nil))))
  9987. ;; Look for elements named after S, only if not in a headline
  9988. ;; search.
  9989. ((and (not starred)
  9990. (let ((name (format "^[ \t]*#\\+NAME: +%s[ \t]*$" s-single-re)))
  9991. (catch :name-match
  9992. (goto-char (point-min))
  9993. (while (re-search-forward name nil t)
  9994. (let ((element (org-element-at-point)))
  9995. (when (equal (org-split-string
  9996. (org-element-property :name element)
  9997. "[ \t]+")
  9998. words)
  9999. (setq type 'dedicated)
  10000. (beginning-of-line)
  10001. (throw :name-match t))))
  10002. nil))))
  10003. ;; Regular text search. Prefer headlines in Org mode
  10004. ;; buffers.
  10005. ((and (derived-mode-p 'org-mode)
  10006. (let* ((wspace "[ \t]")
  10007. (wspaceopt (concat wspace "*"))
  10008. (cookie (concat "\\(?:"
  10009. wspaceopt
  10010. "\\[[0-9]*\\(?:%\\|/[0-9]*\\)\\]"
  10011. wspaceopt
  10012. "\\)"))
  10013. (sep (concat "\\(?:\\(?:" wspace "\\|" cookie "\\)+\\)"))
  10014. (re (concat
  10015. org-outline-regexp-bol
  10016. "\\(?:" org-todo-regexp "[ \t]+\\)?"
  10017. "\\(?:\\[#.\\][ \t]+\\)?"
  10018. "\\(?:" org-comment-string "[ \t]+\\)?"
  10019. sep "?"
  10020. (let ((title (mapconcat #'regexp-quote
  10021. words
  10022. sep)))
  10023. (if starred (substring title 1) title))
  10024. sep "?"
  10025. (org-re "\\(?:[ \t]+:[[:alnum:]_@#%%:]+:\\)?")
  10026. "[ \t]*$")))
  10027. (goto-char (point-min))
  10028. (re-search-forward re nil t)))
  10029. (goto-char (match-beginning 0))
  10030. (setq type 'dedicated))
  10031. ;; Offer to create non-existent headline depending on
  10032. ;; `org-link-search-must-match-exact-headline'.
  10033. ((and (derived-mode-p 'org-mode)
  10034. (not org-link-search-inhibit-query)
  10035. (eq org-link-search-must-match-exact-headline 'query-to-create)
  10036. (yes-or-no-p "No match - create this as a new heading? "))
  10037. (goto-char (point-max))
  10038. (unless (bolp) (newline))
  10039. (org-insert-heading nil t t)
  10040. (insert s "\n")
  10041. (beginning-of-line 0))
  10042. ;; Only headlines are looked after. No need to process
  10043. ;; further: throw an error.
  10044. ((and (derived-mode-p 'org-mode)
  10045. (or starred org-link-search-must-match-exact-headline))
  10046. (goto-char origin)
  10047. (error "No match for fuzzy expression: %s" normalized))
  10048. ;; Regular text search.
  10049. ((catch :fuzzy-match
  10050. (goto-char (point-min))
  10051. (while (re-search-forward s-multi-re nil t)
  10052. ;; Skip match if it contains AVOID-POS or it is included
  10053. ;; in a link with a description but outside the
  10054. ;; description.
  10055. (unless (or (and avoid-pos
  10056. (<= (match-beginning 0) avoid-pos)
  10057. (> (match-end 0) avoid-pos))
  10058. (and (save-match-data
  10059. (org-in-regexp org-bracket-link-regexp))
  10060. (match-beginning 3)
  10061. (or (> (match-beginning 3) (point))
  10062. (<= (match-end 3) (point)))
  10063. (org-element-lineage
  10064. (save-match-data (org-element-context))
  10065. '(link) t)))
  10066. (goto-char (match-beginning 0))
  10067. (setq type 'fuzzy)
  10068. (throw :fuzzy-match t)))
  10069. nil))
  10070. ;; All failed. Throw an error.
  10071. (t (goto-char origin)
  10072. (error "No match for fuzzy expression: %s" normalized))))))
  10073. ;; Disclose surroundings of match, if appropriate.
  10074. (when (and (derived-mode-p 'org-mode) (not stealth))
  10075. (org-show-context 'link-search))
  10076. type))
  10077. (defun org-get-buffer-for-internal-link (buffer)
  10078. "Return a buffer to be used for displaying the link target of internal links."
  10079. (cond
  10080. ((not org-display-internal-link-with-indirect-buffer)
  10081. buffer)
  10082. ((string-match "(Clone)$" (buffer-name buffer))
  10083. (message "Buffer is already a clone, not making another one")
  10084. ;; we also do not modify visibility in this case
  10085. buffer)
  10086. (t ; make a new indirect buffer for displaying the link
  10087. (let* ((bn (buffer-name buffer))
  10088. (ibn (concat bn "(Clone)"))
  10089. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  10090. (with-current-buffer ib (org-overview))
  10091. ib))))
  10092. (defun org-do-occur (regexp &optional cleanup)
  10093. "Call the Emacs command `occur'.
  10094. If CLEANUP is non-nil, remove the printout of the regular expression
  10095. in the *Occur* buffer. This is useful if the regex is long and not useful
  10096. to read."
  10097. (occur regexp)
  10098. (when cleanup
  10099. (let ((cwin (selected-window)) win beg end)
  10100. (when (setq win (get-buffer-window "*Occur*"))
  10101. (select-window win))
  10102. (goto-char (point-min))
  10103. (when (re-search-forward "match[a-z]+" nil t)
  10104. (setq beg (match-end 0))
  10105. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  10106. (setq end (1- (match-beginning 0)))))
  10107. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  10108. (goto-char (point-min))
  10109. (select-window cwin))))
  10110. ;;; The mark ring for links jumps
  10111. (defvar org-mark-ring nil
  10112. "Mark ring for positions before jumps in Org-mode.")
  10113. (defvar org-mark-ring-last-goto nil
  10114. "Last position in the mark ring used to go back.")
  10115. ;; Fill and close the ring
  10116. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  10117. (loop for i from 1 to org-mark-ring-length do
  10118. (push (make-marker) org-mark-ring))
  10119. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  10120. org-mark-ring)
  10121. (defun org-mark-ring-push (&optional pos buffer)
  10122. "Put the current position or POS into the mark ring and rotate it."
  10123. (interactive)
  10124. (setq pos (or pos (point)))
  10125. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  10126. (move-marker (car org-mark-ring)
  10127. (or pos (point))
  10128. (or buffer (current-buffer)))
  10129. (message "%s"
  10130. (substitute-command-keys
  10131. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  10132. (defun org-mark-ring-goto (&optional n)
  10133. "Jump to the previous position in the mark ring.
  10134. With prefix arg N, jump back that many stored positions. When
  10135. called several times in succession, walk through the entire ring.
  10136. Org-mode commands jumping to a different position in the current file,
  10137. or to another Org-mode file, automatically push the old position
  10138. onto the ring."
  10139. (interactive "p")
  10140. (let (p m)
  10141. (if (eq last-command this-command)
  10142. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  10143. (setq p org-mark-ring))
  10144. (setq org-mark-ring-last-goto p)
  10145. (setq m (car p))
  10146. (org-pop-to-buffer-same-window (marker-buffer m))
  10147. (goto-char m)
  10148. (if (or (outline-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  10149. (defun org-remove-angle-brackets (s)
  10150. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  10151. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  10152. s)
  10153. (defun org-add-angle-brackets (s)
  10154. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  10155. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  10156. s)
  10157. (defun org-remove-double-quotes (s)
  10158. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  10159. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  10160. s)
  10161. ;;; Following specific links
  10162. (defun org-follow-timestamp-link ()
  10163. "Open an agenda view for the time-stamp date/range at point."
  10164. (cond
  10165. ((org-at-date-range-p t)
  10166. (let ((org-agenda-start-on-weekday)
  10167. (t1 (match-string 1))
  10168. (t2 (match-string 2)) tt1 tt2)
  10169. (setq tt1 (time-to-days (org-time-string-to-time t1))
  10170. tt2 (time-to-days (org-time-string-to-time t2)))
  10171. (let ((org-agenda-buffer-tmp-name
  10172. (format "*Org Agenda(a:%s)"
  10173. (concat (substring t1 0 10) "--" (substring t2 0 10)))))
  10174. (org-agenda-list nil tt1 (1+ (- tt2 tt1))))))
  10175. ((org-at-timestamp-p t)
  10176. (let ((org-agenda-buffer-tmp-name
  10177. (format "*Org Agenda(a:%s)" (substring (match-string 1) 0 10))))
  10178. (org-agenda-list nil (time-to-days (org-time-string-to-time
  10179. (substring (match-string 1) 0 10)))
  10180. 1)))
  10181. (t (error "This should not happen"))))
  10182. ;;; Following file links
  10183. (declare-function mailcap-parse-mailcaps "mailcap" (&optional path force))
  10184. (declare-function mailcap-extension-to-mime "mailcap" (extn))
  10185. (declare-function mailcap-mime-info
  10186. "mailcap" (string &optional request no-decode))
  10187. (defvar org-wait nil)
  10188. (defun org-open-file (path &optional in-emacs line search)
  10189. "Open the file at PATH.
  10190. First, this expands any special file name abbreviations. Then the
  10191. configuration variable `org-file-apps' is checked if it contains an
  10192. entry for this file type, and if yes, the corresponding command is launched.
  10193. If no application is found, Emacs simply visits the file.
  10194. With optional prefix argument IN-EMACS, Emacs will visit the file.
  10195. With a double \\[universal-argument] \\[universal-argument] \
  10196. prefix arg, Org tries to avoid opening in Emacs
  10197. and to use an external application to visit the file.
  10198. Optional LINE specifies a line to go to, optional SEARCH a string
  10199. to search for. If LINE or SEARCH is given, the file will be
  10200. opened in Emacs, unless an entry from org-file-apps that makes
  10201. use of groups in a regexp matches.
  10202. If you want to change the way frames are used when following a
  10203. link, please customize `org-link-frame-setup'.
  10204. If the file does not exist, an error is thrown."
  10205. (let* ((file (if (equal path "")
  10206. buffer-file-name
  10207. (substitute-in-file-name (expand-file-name path))))
  10208. (file-apps (append org-file-apps (org-default-apps)))
  10209. (apps (org-remove-if
  10210. 'org-file-apps-entry-match-against-dlink-p file-apps))
  10211. (apps-dlink (org-remove-if-not
  10212. 'org-file-apps-entry-match-against-dlink-p file-apps))
  10213. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  10214. (dirp (if remp nil (file-directory-p file)))
  10215. (file (if (and dirp org-open-directory-means-index-dot-org)
  10216. (concat (file-name-as-directory file) "index.org")
  10217. file))
  10218. (a-m-a-p (assq 'auto-mode apps))
  10219. (dfile (downcase file))
  10220. ;; reconstruct the original file: link from the PATH, LINE and SEARCH args
  10221. (link (cond ((and (eq line nil)
  10222. (eq search nil))
  10223. file)
  10224. (line
  10225. (concat file "::" (number-to-string line)))
  10226. (search
  10227. (concat file "::" search))))
  10228. (dlink (downcase link))
  10229. (old-buffer (current-buffer))
  10230. (old-pos (point))
  10231. (old-mode major-mode)
  10232. ext cmd link-match-data)
  10233. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  10234. (setq ext (match-string 1 dfile))
  10235. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  10236. (setq ext (match-string 1 dfile))))
  10237. (cond
  10238. ((member in-emacs '((16) system))
  10239. (setq cmd (cdr (assoc 'system apps))))
  10240. (in-emacs (setq cmd 'emacs))
  10241. (t
  10242. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  10243. (and dirp (cdr (assoc 'directory apps)))
  10244. ; first, try matching against apps-dlink
  10245. ; if we get a match here, store the match data for later
  10246. (let ((match (assoc-default dlink apps-dlink
  10247. 'string-match)))
  10248. (if match
  10249. (progn (setq link-match-data (match-data))
  10250. match)
  10251. (progn (setq in-emacs (or in-emacs line search))
  10252. nil))) ; if we have no match in apps-dlink,
  10253. ; always open the file in emacs if line or search
  10254. ; is given (for backwards compatibility)
  10255. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  10256. 'string-match)
  10257. (cdr (assoc ext apps))
  10258. (cdr (assoc t apps))))))
  10259. (when (eq cmd 'system)
  10260. (setq cmd (cdr (assoc 'system apps))))
  10261. (when (eq cmd 'default)
  10262. (setq cmd (cdr (assoc t apps))))
  10263. (when (eq cmd 'mailcap)
  10264. (require 'mailcap)
  10265. (mailcap-parse-mailcaps)
  10266. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  10267. (command (mailcap-mime-info mime-type)))
  10268. (if (stringp command)
  10269. (setq cmd command)
  10270. (setq cmd 'emacs))))
  10271. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  10272. (not (file-exists-p file))
  10273. (not org-open-non-existing-files))
  10274. (user-error "No such file: %s" file))
  10275. (cond
  10276. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  10277. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  10278. (while (string-match "['\"]%s['\"]" cmd)
  10279. (setq cmd (replace-match "%s" t t cmd)))
  10280. (while (string-match "%s" cmd)
  10281. (setq cmd (replace-match
  10282. (save-match-data
  10283. (shell-quote-argument
  10284. (convert-standard-filename file)))
  10285. t t cmd)))
  10286. ;; Replace "%1", "%2" etc. in command with group matches from regex
  10287. (save-match-data
  10288. (let ((match-index 1)
  10289. (number-of-groups (- (/ (length link-match-data) 2) 1)))
  10290. (set-match-data link-match-data)
  10291. (while (<= match-index number-of-groups)
  10292. (let ((regex (concat "%" (number-to-string match-index)))
  10293. (replace-with (match-string match-index dlink)))
  10294. (while (string-match regex cmd)
  10295. (setq cmd (replace-match replace-with t t cmd))))
  10296. (setq match-index (+ match-index 1)))))
  10297. (save-window-excursion
  10298. (message "Running %s...done" cmd)
  10299. (start-process-shell-command cmd nil cmd)
  10300. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))))
  10301. ((or (stringp cmd)
  10302. (eq cmd 'emacs))
  10303. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  10304. (widen)
  10305. (if line (progn (org-goto-line line)
  10306. (if (derived-mode-p 'org-mode)
  10307. (org-reveal)))
  10308. (if search (org-link-search search))))
  10309. ((consp cmd)
  10310. (let ((file (convert-standard-filename file)))
  10311. (save-match-data
  10312. (set-match-data link-match-data)
  10313. (eval cmd))))
  10314. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  10315. (and (derived-mode-p 'org-mode) (eq old-mode 'org-mode)
  10316. (or (not (equal old-buffer (current-buffer)))
  10317. (not (equal old-pos (point))))
  10318. (org-mark-ring-push old-pos old-buffer))))
  10319. (defun org-file-apps-entry-match-against-dlink-p (entry)
  10320. "This function returns non-nil if `entry' uses a regular
  10321. expression which should be matched against the whole link by
  10322. org-open-file.
  10323. It assumes that is the case when the entry uses a regular
  10324. expression which has at least one grouping construct and the
  10325. action is either a lisp form or a command string containing
  10326. '%1', i.e. using at least one subexpression match as a
  10327. parameter."
  10328. (let ((selector (car entry))
  10329. (action (cdr entry)))
  10330. (if (stringp selector)
  10331. (and (> (regexp-opt-depth selector) 0)
  10332. (or (and (stringp action)
  10333. (string-match "%[0-9]" action))
  10334. (consp action)))
  10335. nil)))
  10336. (defun org-default-apps ()
  10337. "Return the default applications for this operating system."
  10338. (cond
  10339. ((eq system-type 'darwin)
  10340. org-file-apps-defaults-macosx)
  10341. ((eq system-type 'windows-nt)
  10342. org-file-apps-defaults-windowsnt)
  10343. (t org-file-apps-defaults-gnu)))
  10344. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  10345. "Convert extensions to regular expressions in the cars of LIST.
  10346. Also, weed out any non-string entries, because the return value is used
  10347. only for regexp matching.
  10348. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  10349. point to the symbol `emacs', indicating that the file should
  10350. be opened in Emacs."
  10351. (append
  10352. (delq nil
  10353. (mapcar (lambda (x)
  10354. (if (not (stringp (car x)))
  10355. nil
  10356. (if (string-match "\\W" (car x))
  10357. x
  10358. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  10359. list))
  10360. (if add-auto-mode
  10361. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  10362. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  10363. (defun org-file-remote-p (file)
  10364. "Test whether FILE specifies a location on a remote system.
  10365. Return non-nil if the location is indeed remote.
  10366. For example, the filename \"/user@host:/foo\" specifies a location
  10367. on the system \"/user@host:\"."
  10368. (cond ((fboundp 'file-remote-p)
  10369. (file-remote-p file))
  10370. ((fboundp 'tramp-handle-file-remote-p)
  10371. (tramp-handle-file-remote-p file))
  10372. ((and (boundp 'ange-ftp-name-format)
  10373. (string-match (car ange-ftp-name-format) file))
  10374. t)))
  10375. ;;;; Refiling
  10376. (defun org-get-org-file ()
  10377. "Read a filename, with default directory `org-directory'."
  10378. (let ((default (or org-default-notes-file remember-data-file)))
  10379. (read-file-name (format "File name [%s]: " default)
  10380. (file-name-as-directory org-directory)
  10381. default)))
  10382. (defun org-notes-order-reversed-p ()
  10383. "Check if the current file should receive notes in reversed order."
  10384. (cond
  10385. ((not org-reverse-note-order) nil)
  10386. ((eq t org-reverse-note-order) t)
  10387. ((not (listp org-reverse-note-order)) nil)
  10388. (t (catch 'exit
  10389. (dolist (entry org-reverse-note-order)
  10390. (if (string-match (car entry) buffer-file-name)
  10391. (throw 'exit (cdr entry))))))))
  10392. (defvar org-refile-target-table nil
  10393. "The list of refile targets, created by `org-refile'.")
  10394. (defvar org-agenda-new-buffers nil
  10395. "Buffers created to visit agenda files.")
  10396. (defvar org-refile-cache nil
  10397. "Cache for refile targets.")
  10398. (defvar org-refile-markers nil
  10399. "All the markers used for caching refile locations.")
  10400. (defun org-refile-marker (pos)
  10401. "Get a new refile marker, but only if caching is in use."
  10402. (if (not org-refile-use-cache)
  10403. pos
  10404. (let ((m (make-marker)))
  10405. (move-marker m pos)
  10406. (push m org-refile-markers)
  10407. m)))
  10408. (defun org-refile-cache-clear ()
  10409. "Clear the refile cache and disable all the markers."
  10410. (mapc (lambda (m) (move-marker m nil)) org-refile-markers)
  10411. (setq org-refile-markers nil)
  10412. (setq org-refile-cache nil)
  10413. (message "Refile cache has been cleared"))
  10414. (defun org-refile-cache-check-set (set)
  10415. "Check if all the markers in the cache still have live buffers."
  10416. (let (marker)
  10417. (catch 'exit
  10418. (while (and set (setq marker (nth 3 (pop set))))
  10419. ;; If `org-refile-use-outline-path' is 'file, marker may be nil
  10420. (when (and marker (null (marker-buffer marker)))
  10421. (message "Please regenerate the refile cache with `C-0 C-c C-w'")
  10422. (sit-for 3)
  10423. (throw 'exit nil)))
  10424. t)))
  10425. (defun org-refile-cache-put (set &rest identifiers)
  10426. "Push the refile targets SET into the cache, under IDENTIFIERS."
  10427. (let* ((key (sha1 (prin1-to-string identifiers)))
  10428. (entry (assoc key org-refile-cache)))
  10429. (if entry
  10430. (setcdr entry set)
  10431. (push (cons key set) org-refile-cache))))
  10432. (defun org-refile-cache-get (&rest identifiers)
  10433. "Retrieve the cached value for refile targets given by IDENTIFIERS."
  10434. (cond
  10435. ((not org-refile-cache) nil)
  10436. ((not org-refile-use-cache) (org-refile-cache-clear) nil)
  10437. (t
  10438. (let ((set (cdr (assoc (sha1 (prin1-to-string identifiers))
  10439. org-refile-cache))))
  10440. (and set (org-refile-cache-check-set set) set)))))
  10441. (defun org-refile-get-targets (&optional default-buffer excluded-entries)
  10442. "Produce a table with refile targets."
  10443. (let ((case-fold-search nil)
  10444. ;; otherwise org confuses "TODO" as a kw and "Todo" as a word
  10445. (entries (or org-refile-targets '((nil . (:level . 1)))))
  10446. targets tgs txt re files desc descre fast-path-p level pos0)
  10447. (message "Getting targets...")
  10448. (with-current-buffer (or default-buffer (current-buffer))
  10449. (dolist (entry entries)
  10450. (setq files (car entry) desc (cdr entry))
  10451. (setq fast-path-p nil)
  10452. (cond
  10453. ((null files) (setq files (list (current-buffer))))
  10454. ((eq files 'org-agenda-files)
  10455. (setq files (org-agenda-files 'unrestricted)))
  10456. ((and (symbolp files) (fboundp files))
  10457. (setq files (funcall files)))
  10458. ((and (symbolp files) (boundp files))
  10459. (setq files (symbol-value files))))
  10460. (if (stringp files) (setq files (list files)))
  10461. (cond
  10462. ((eq (car desc) :tag)
  10463. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  10464. ((eq (car desc) :todo)
  10465. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  10466. ((eq (car desc) :regexp)
  10467. (setq descre (cdr desc)))
  10468. ((eq (car desc) :level)
  10469. (setq descre (concat "^\\*\\{" (number-to-string
  10470. (if org-odd-levels-only
  10471. (1- (* 2 (cdr desc)))
  10472. (cdr desc)))
  10473. "\\}[ \t]")))
  10474. ((eq (car desc) :maxlevel)
  10475. (setq fast-path-p t)
  10476. (setq descre (concat "^\\*\\{1," (number-to-string
  10477. (if org-odd-levels-only
  10478. (1- (* 2 (cdr desc)))
  10479. (cdr desc)))
  10480. "\\}[ \t]")))
  10481. (t (error "Bad refiling target description %s" desc)))
  10482. (dolist (f files)
  10483. (with-current-buffer
  10484. (if (bufferp f) f (org-get-agenda-file-buffer f))
  10485. (or
  10486. (setq tgs (org-refile-cache-get (buffer-file-name) descre))
  10487. (progn
  10488. (if (bufferp f) (setq f (buffer-file-name
  10489. (buffer-base-buffer f))))
  10490. (setq f (and f (expand-file-name f)))
  10491. (if (eq org-refile-use-outline-path 'file)
  10492. (push (list (file-name-nondirectory f) f nil nil) tgs))
  10493. (save-excursion
  10494. (save-restriction
  10495. (widen)
  10496. (goto-char (point-min))
  10497. (while (re-search-forward descre nil t)
  10498. (goto-char (setq pos0 (point-at-bol)))
  10499. (catch 'next
  10500. (when org-refile-target-verify-function
  10501. (save-match-data
  10502. (or (funcall org-refile-target-verify-function)
  10503. (throw 'next t))))
  10504. (when (and (looking-at org-complex-heading-regexp)
  10505. (not (member (match-string 4) excluded-entries))
  10506. (match-string 4))
  10507. (setq level (org-reduced-level
  10508. (- (match-end 1) (match-beginning 1)))
  10509. txt (org-link-display-format (match-string 4))
  10510. txt (replace-regexp-in-string "\\( *\[[0-9]+/?[0-9]*%?\]\\)+$" "" txt)
  10511. re (format org-complex-heading-regexp-format
  10512. (regexp-quote (match-string 4))))
  10513. (when org-refile-use-outline-path
  10514. (setq txt (mapconcat
  10515. 'org-protect-slash
  10516. (append
  10517. (if (eq org-refile-use-outline-path
  10518. 'file)
  10519. (list (file-name-nondirectory
  10520. (buffer-file-name
  10521. (buffer-base-buffer))))
  10522. (if (eq org-refile-use-outline-path
  10523. 'full-file-path)
  10524. (list (buffer-file-name
  10525. (buffer-base-buffer)))))
  10526. (org-get-outline-path fast-path-p
  10527. level txt)
  10528. (list txt))
  10529. "/")))
  10530. (push (list txt f re (org-refile-marker (point)))
  10531. tgs)))
  10532. (when (= (point) pos0)
  10533. ;; verification function has not moved point
  10534. (goto-char (point-at-eol))))))))
  10535. (when org-refile-use-cache
  10536. (org-refile-cache-put tgs (buffer-file-name) descre))
  10537. (setq targets (append tgs targets))))))
  10538. (message "Getting targets...done")
  10539. (nreverse targets)))
  10540. (defun org-protect-slash (s)
  10541. (while (string-match "/" s)
  10542. (setq s (replace-match "\\" t t s)))
  10543. s)
  10544. (defvar org-olpa (make-vector 20 nil))
  10545. (defun org-get-outline-path (&optional fastp level heading)
  10546. "Return the outline path to the current entry, as a list.
  10547. The parameters FASTP, LEVEL, and HEADING are for use by a scanner
  10548. routine which makes outline path derivations for an entire file,
  10549. avoiding backtracing. Refile target collection makes use of that."
  10550. (if fastp
  10551. (progn
  10552. (if (> level 19)
  10553. (error "Outline path failure, more than 19 levels"))
  10554. (loop for i from level upto 19 do
  10555. (aset org-olpa i nil))
  10556. (prog1
  10557. (delq nil (append org-olpa nil))
  10558. (aset org-olpa level heading)))
  10559. (let (rtn case-fold-search)
  10560. (save-excursion
  10561. (save-restriction
  10562. (widen)
  10563. (while (org-up-heading-safe)
  10564. (when (looking-at org-complex-heading-regexp)
  10565. (push (org-trim
  10566. (replace-regexp-in-string
  10567. ;; Remove statistical/checkboxes cookies
  10568. "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" ""
  10569. (org-match-string-no-properties 4)))
  10570. rtn)))
  10571. rtn)))))
  10572. (defun org-format-outline-path (path &optional width prefix separator)
  10573. "Format the outline path PATH for display.
  10574. WIDTH is the maximum number of characters that is available.
  10575. PREFIX is a prefix to be included in the returned string,
  10576. such as the file name.
  10577. SEPARATOR is inserted between the different parts of the path,
  10578. the default is \"/\"."
  10579. (setq width (or width 79))
  10580. (if prefix (setq width (- width (length prefix))))
  10581. (if (not path)
  10582. (or prefix "")
  10583. (let* ((nsteps (length path))
  10584. (total-width (+ nsteps (apply '+ (mapcar 'length path))))
  10585. (maxwidth (if (<= total-width width)
  10586. 10000 ;; everything fits
  10587. ;; we need to shorten the level headings
  10588. (/ (- width nsteps) nsteps)))
  10589. (org-odd-levels-only nil)
  10590. (n 0)
  10591. (total (1+ (length prefix))))
  10592. (setq maxwidth (max maxwidth 10))
  10593. (concat prefix
  10594. (if prefix (or separator "/"))
  10595. (mapconcat
  10596. (lambda (h)
  10597. (setq n (1+ n))
  10598. (if (and (= n nsteps) (< maxwidth 10000))
  10599. (setq maxwidth (- total-width total)))
  10600. (if (< (length h) maxwidth)
  10601. (progn (setq total (+ total (length h) 1)) h)
  10602. (setq h (substring h 0 (- maxwidth 2))
  10603. total (+ total maxwidth 1))
  10604. (if (string-match "[ \t]+\\'" h)
  10605. (setq h (substring h 0 (match-beginning 0))))
  10606. (setq h (concat h "..")))
  10607. (org-add-props h nil 'face
  10608. (nth (% (1- n) org-n-level-faces)
  10609. org-level-faces))
  10610. h)
  10611. path (or separator "/"))))))
  10612. (defun org-display-outline-path (&optional file current separator just-return-string)
  10613. "Display the current outline path in the echo area.
  10614. If FILE is non-nil, prepend the output with the file name.
  10615. If CURRENT is non-nil, append the current heading to the output.
  10616. SEPARATOR is passed through to `org-format-outline-path'. It separates
  10617. the different parts of the path and defaults to \"/\".
  10618. If JUST-RETURN-STRING is non-nil, return a string, don't display a message."
  10619. (interactive "P")
  10620. (let* (case-fold-search
  10621. (bfn (buffer-file-name (buffer-base-buffer)))
  10622. (path (and (derived-mode-p 'org-mode) (org-get-outline-path)))
  10623. res)
  10624. (if current (setq path (append path
  10625. (save-excursion
  10626. (org-back-to-heading t)
  10627. (if (looking-at org-complex-heading-regexp)
  10628. (list (match-string 4)))))))
  10629. (setq res
  10630. (org-format-outline-path
  10631. path
  10632. (1- (frame-width))
  10633. (and file bfn (concat (file-name-nondirectory bfn) separator))
  10634. separator))
  10635. (if just-return-string
  10636. (org-no-properties res)
  10637. (org-unlogged-message "%s" res))))
  10638. (defvar org-refile-history nil
  10639. "History for refiling operations.")
  10640. (defvar org-after-refile-insert-hook nil
  10641. "Hook run after `org-refile' has inserted its stuff at the new location.
  10642. Note that this is still *before* the stuff will be removed from
  10643. the *old* location.")
  10644. (defvar org-capture-last-stored-marker)
  10645. (defvar org-refile-keep nil
  10646. "Non-nil means `org-refile' will copy instead of refile.")
  10647. (defun org-copy ()
  10648. "Like `org-refile', but copy."
  10649. (interactive)
  10650. (let ((org-refile-keep t))
  10651. (funcall 'org-refile nil nil nil "Copy")))
  10652. (defun org-refile (&optional arg default-buffer rfloc msg)
  10653. "Move the entry or entries at point to another heading.
  10654. The list of target headings is compiled using the information in
  10655. `org-refile-targets', which see.
  10656. At the target location, the entry is filed as a subitem of the
  10657. target heading. Depending on `org-reverse-note-order', the new
  10658. subitem will either be the first or the last subitem.
  10659. If there is an active region, all entries in that region will be
  10660. refiled. However, the region must fulfill the requirement that
  10661. the first heading sets the top-level of the moved text.
  10662. With prefix arg ARG, the command will only visit the target
  10663. location and not actually move anything.
  10664. With a double prefix arg \\[universal-argument] \\[universal-argument], go to the location where the last
  10665. refiling operation has put the subtree.
  10666. With a numeric prefix argument of `2', refile to the running clock.
  10667. With a numeric prefix argument of `3', emulate `org-refile-keep'
  10668. being set to t and copy to the target location, don't move it.
  10669. Beware that keeping refiled entries may result in duplicated ID
  10670. properties.
  10671. RFLOC can be a refile location obtained in a different way.
  10672. MSG is a string to replace \"Refile\" in the default prompt with
  10673. another verb. E.g. `org-copy' sets this parameter to \"Copy\".
  10674. See also `org-refile-use-outline-path' and `org-completion-use-ido'.
  10675. If you are using target caching (see `org-refile-use-cache'), you
  10676. have to clear the target cache in order to find new targets.
  10677. This can be done with a 0 prefix (`C-0 C-c C-w') or a triple
  10678. prefix argument (`C-u C-u C-u C-c C-w')."
  10679. (interactive "P")
  10680. (if (member arg '(0 (64)))
  10681. (org-refile-cache-clear)
  10682. (let* ((actionmsg (cond (msg msg)
  10683. ((equal arg 3) "Refile (and keep)")
  10684. (t "Refile")))
  10685. (cbuf (current-buffer))
  10686. (regionp (org-region-active-p))
  10687. (region-start (and regionp (region-beginning)))
  10688. (region-end (and regionp (region-end)))
  10689. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  10690. (org-refile-keep (if (equal arg 3) t org-refile-keep))
  10691. pos it nbuf file re level reversed)
  10692. (setq last-command nil)
  10693. (when regionp
  10694. (goto-char region-start)
  10695. (or (bolp) (goto-char (point-at-bol)))
  10696. (setq region-start (point))
  10697. (unless (or (org-kill-is-subtree-p
  10698. (buffer-substring region-start region-end))
  10699. (prog1 org-refile-active-region-within-subtree
  10700. (let ((s (point-at-eol)))
  10701. (org-toggle-heading)
  10702. (setq region-end (+ (- (point-at-eol) s) region-end)))))
  10703. (user-error "The region is not a (sequence of) subtree(s)")))
  10704. (if (equal arg '(16))
  10705. (org-refile-goto-last-stored)
  10706. (when (or
  10707. (and (equal arg 2)
  10708. org-clock-hd-marker (marker-buffer org-clock-hd-marker)
  10709. (prog1
  10710. (setq it (list (or org-clock-heading "running clock")
  10711. (buffer-file-name
  10712. (marker-buffer org-clock-hd-marker))
  10713. ""
  10714. (marker-position org-clock-hd-marker)))
  10715. (setq arg nil)))
  10716. (setq it (or rfloc
  10717. (let (heading-text)
  10718. (save-excursion
  10719. (unless (and arg (listp arg))
  10720. (org-back-to-heading t)
  10721. (setq heading-text
  10722. (replace-regexp-in-string
  10723. org-bracket-link-regexp
  10724. "\\3"
  10725. (nth 4 (org-heading-components)))))
  10726. (org-refile-get-location
  10727. (cond ((and arg (listp arg)) "Goto")
  10728. (regionp (concat actionmsg " region to"))
  10729. (t (concat actionmsg " subtree \""
  10730. heading-text "\" to")))
  10731. default-buffer
  10732. (and (not (equal '(4) arg))
  10733. org-refile-allow-creating-parent-nodes)
  10734. arg))))))
  10735. (setq file (nth 1 it)
  10736. re (nth 2 it)
  10737. pos (nth 3 it))
  10738. (if (and (not arg)
  10739. pos
  10740. (equal (buffer-file-name) file)
  10741. (if regionp
  10742. (and (>= pos region-start)
  10743. (<= pos region-end))
  10744. (and (>= pos (point))
  10745. (< pos (save-excursion
  10746. (org-end-of-subtree t t))))))
  10747. (error "Cannot refile to position inside the tree or region"))
  10748. (setq nbuf (or (find-buffer-visiting file)
  10749. (find-file-noselect file)))
  10750. (if (and arg (not (equal arg 3)))
  10751. (progn
  10752. (org-pop-to-buffer-same-window nbuf)
  10753. (goto-char pos)
  10754. (org-show-context 'org-goto))
  10755. (if regionp
  10756. (progn
  10757. (org-kill-new (buffer-substring region-start region-end))
  10758. (org-save-markers-in-region region-start region-end))
  10759. (org-copy-subtree 1 nil t))
  10760. (with-current-buffer (setq nbuf (or (find-buffer-visiting file)
  10761. (find-file-noselect file)))
  10762. (setq reversed (org-notes-order-reversed-p))
  10763. (save-excursion
  10764. (save-restriction
  10765. (widen)
  10766. (if pos
  10767. (progn
  10768. (goto-char pos)
  10769. (looking-at org-outline-regexp)
  10770. (setq level (org-get-valid-level (funcall outline-level) 1))
  10771. (goto-char
  10772. (if reversed
  10773. (or (outline-next-heading) (point-max))
  10774. (or (save-excursion (org-get-next-sibling))
  10775. (org-end-of-subtree t t)
  10776. (point-max)))))
  10777. (setq level 1)
  10778. (if (not reversed)
  10779. (goto-char (point-max))
  10780. (goto-char (point-min))
  10781. (or (outline-next-heading) (goto-char (point-max)))))
  10782. (if (not (bolp)) (newline))
  10783. (org-paste-subtree level nil nil t)
  10784. (when org-log-refile
  10785. (org-add-log-setup 'refile nil nil 'findpos org-log-refile)
  10786. (unless (eq org-log-refile 'note)
  10787. (save-excursion (org-add-log-note))))
  10788. (and org-auto-align-tags
  10789. (let ((org-loop-over-headlines-in-active-region nil))
  10790. (org-set-tags nil t)))
  10791. (let ((bookmark-name (plist-get org-bookmark-names-plist
  10792. :last-refile)))
  10793. (when bookmark-name
  10794. (with-demoted-errors
  10795. (bookmark-set bookmark-name))))
  10796. ;; If we are refiling for capture, make sure that the
  10797. ;; last-capture pointers point here
  10798. (when (org-bound-and-true-p org-refile-for-capture)
  10799. (let ((bookmark-name (plist-get org-bookmark-names-plist
  10800. :last-capture-marker)))
  10801. (when bookmark-name
  10802. (with-demoted-errors
  10803. (bookmark-set bookmark-name))))
  10804. (move-marker org-capture-last-stored-marker (point)))
  10805. (if (fboundp 'deactivate-mark) (deactivate-mark))
  10806. (run-hooks 'org-after-refile-insert-hook))))
  10807. (unless org-refile-keep
  10808. (if regionp
  10809. (delete-region (point) (+ (point) (- region-end region-start)))
  10810. (delete-region
  10811. (and (org-back-to-heading t) (point))
  10812. (min (1+ (buffer-size)) (org-end-of-subtree t t) (point)))))
  10813. (when (featurep 'org-inlinetask)
  10814. (org-inlinetask-remove-END-maybe))
  10815. (setq org-markers-to-move nil)
  10816. (message (concat actionmsg " to \"%s\" in file %s: done") (car it) file)))))))
  10817. (defun org-refile-goto-last-stored ()
  10818. "Go to the location where the last refile was stored."
  10819. (interactive)
  10820. (bookmark-jump (plist-get org-bookmark-names-plist :last-refile))
  10821. (message "This is the location of the last refile"))
  10822. (defun org-refile--get-location (refloc tbl)
  10823. "When user refile to REFLOC, find the associated target in TBL.
  10824. Also check `org-refile-target-table'."
  10825. (car (delq
  10826. nil
  10827. (mapcar
  10828. (lambda (r) (or (assoc r tbl)
  10829. (assoc r org-refile-target-table)))
  10830. (list (replace-regexp-in-string "/$" "" refloc)
  10831. (replace-regexp-in-string "\\([^/]\\)$" "\\1/" refloc))))))
  10832. (defun org-refile-get-location (&optional prompt default-buffer new-nodes
  10833. no-exclude)
  10834. "Prompt the user for a refile location, using PROMPT.
  10835. PROMPT should not be suffixed with a colon and a space, because
  10836. this function appends the default value from
  10837. `org-refile-history' automatically, if that is not empty.
  10838. When NO-EXCLUDE is set, do not exclude headlines in the current subtree,
  10839. this is used for the GOTO interface."
  10840. (let ((org-refile-targets org-refile-targets)
  10841. (org-refile-use-outline-path org-refile-use-outline-path)
  10842. excluded-entries)
  10843. (when (and (derived-mode-p 'org-mode)
  10844. (not org-refile-use-cache)
  10845. (not no-exclude))
  10846. (org-map-tree
  10847. (lambda()
  10848. (setq excluded-entries
  10849. (append excluded-entries (list (org-get-heading t t)))))))
  10850. (setq org-refile-target-table
  10851. (org-refile-get-targets default-buffer excluded-entries)))
  10852. (unless org-refile-target-table
  10853. (user-error "No refile targets"))
  10854. (let* ((cbuf (current-buffer))
  10855. (partial-completion-mode nil)
  10856. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  10857. (cfunc (if (and org-refile-use-outline-path
  10858. org-outline-path-complete-in-steps)
  10859. 'org-olpath-completing-read
  10860. 'org-icompleting-read))
  10861. (extra (if org-refile-use-outline-path "/" ""))
  10862. (cbnex (concat (buffer-name) extra))
  10863. (filename (and cfn (expand-file-name cfn)))
  10864. (tbl (mapcar
  10865. (lambda (x)
  10866. (if (and (not (member org-refile-use-outline-path
  10867. '(file full-file-path)))
  10868. (not (equal filename (nth 1 x))))
  10869. (cons (concat (car x) extra " ("
  10870. (file-name-nondirectory (nth 1 x)) ")")
  10871. (cdr x))
  10872. (cons (concat (car x) extra) (cdr x))))
  10873. org-refile-target-table))
  10874. (completion-ignore-case t)
  10875. cdef
  10876. (prompt (concat prompt
  10877. (or (and (car org-refile-history)
  10878. (concat " (default " (car org-refile-history) ")"))
  10879. (and (assoc cbnex tbl) (setq cdef cbnex)
  10880. (concat " (default " cbnex ")"))) ": "))
  10881. pa answ parent-target child parent old-hist)
  10882. (setq old-hist org-refile-history)
  10883. (setq answ (funcall cfunc prompt tbl nil (not new-nodes)
  10884. nil 'org-refile-history (or cdef (car org-refile-history))))
  10885. (if (setq pa (org-refile--get-location answ tbl))
  10886. (progn
  10887. (org-refile-check-position pa)
  10888. (when (or (not org-refile-history)
  10889. (not (eq old-hist org-refile-history))
  10890. (not (equal (car pa) (car org-refile-history))))
  10891. (setq org-refile-history
  10892. (cons (car pa) (if (assoc (car org-refile-history) tbl)
  10893. org-refile-history
  10894. (cdr org-refile-history))))
  10895. (if (equal (car org-refile-history) (nth 1 org-refile-history))
  10896. (pop org-refile-history)))
  10897. pa)
  10898. (if (string-match "\\`\\(.*\\)/\\([^/]+\\)\\'" answ)
  10899. (progn
  10900. (setq parent (match-string 1 answ)
  10901. child (match-string 2 answ))
  10902. (setq parent-target (org-refile--get-location parent tbl))
  10903. (when (and parent-target
  10904. (or (eq new-nodes t)
  10905. (and (eq new-nodes 'confirm)
  10906. (y-or-n-p (format "Create new node \"%s\"? "
  10907. child)))))
  10908. (org-refile-new-child parent-target child)))
  10909. (user-error "Invalid target location")))))
  10910. (declare-function org-string-nw-p "org-macs" (s))
  10911. (defun org-refile-check-position (refile-pointer)
  10912. "Check if the refile pointer matches the headline to which it points."
  10913. (let* ((file (nth 1 refile-pointer))
  10914. (re (nth 2 refile-pointer))
  10915. (pos (nth 3 refile-pointer))
  10916. buffer)
  10917. (if (and (not (markerp pos)) (not file))
  10918. (user-error "Please indicate a target file in the refile path")
  10919. (when (org-string-nw-p re)
  10920. (setq buffer (if (markerp pos)
  10921. (marker-buffer pos)
  10922. (or (find-buffer-visiting file)
  10923. (find-file-noselect file))))
  10924. (with-current-buffer buffer
  10925. (save-excursion
  10926. (save-restriction
  10927. (widen)
  10928. (goto-char pos)
  10929. (beginning-of-line 1)
  10930. (unless (org-looking-at-p re)
  10931. (user-error "Invalid refile position, please clear the cache with `C-0 C-c C-w' before refiling")))))))))
  10932. (defun org-refile-new-child (parent-target child)
  10933. "Use refile target PARENT-TARGET to add new CHILD below it."
  10934. (unless parent-target
  10935. (error "Cannot find parent for new node"))
  10936. (let ((file (nth 1 parent-target))
  10937. (pos (nth 3 parent-target))
  10938. level)
  10939. (with-current-buffer (or (find-buffer-visiting file)
  10940. (find-file-noselect file))
  10941. (save-excursion
  10942. (save-restriction
  10943. (widen)
  10944. (if pos
  10945. (goto-char pos)
  10946. (goto-char (point-max))
  10947. (if (not (bolp)) (newline)))
  10948. (when (looking-at org-outline-regexp)
  10949. (setq level (funcall outline-level))
  10950. (org-end-of-subtree t t))
  10951. (org-back-over-empty-lines)
  10952. (insert "\n" (make-string
  10953. (if pos (org-get-valid-level level 1) 1) ?*)
  10954. " " child "\n")
  10955. (beginning-of-line 0)
  10956. (list (concat (car parent-target) "/" child) file "" (point)))))))
  10957. (defun org-olpath-completing-read (prompt collection &rest args)
  10958. "Read an outline path like a file name."
  10959. (let ((thetable collection)
  10960. (org-completion-use-ido nil) ; does not work with ido.
  10961. (org-completion-use-iswitchb nil)) ; or iswitchb
  10962. (apply #'org-icompleting-read
  10963. prompt
  10964. (lambda (string predicate &optional flag)
  10965. (cond
  10966. ((eq flag nil) (try-completion string thetable))
  10967. ((eq flag t)
  10968. (let ((l (length string)))
  10969. (mapcar (lambda (x)
  10970. (let ((r (substring x l))
  10971. (f (if (string-match " ([^)]*)$" x)
  10972. (match-string 0 x)
  10973. "")))
  10974. (if (string-match "/" r)
  10975. (concat string (substring r 0 (match-end 0)) f)
  10976. x)))
  10977. (all-completions string thetable predicate))))
  10978. ;; Exact match?
  10979. ((eq flag 'lambda) (assoc string thetable))))
  10980. args)))
  10981. ;;;; Dynamic blocks
  10982. (defun org-find-dblock (name)
  10983. "Find the first dynamic block with name NAME in the buffer.
  10984. If not found, stay at current position and return nil."
  10985. (let ((case-fold-search t) pos)
  10986. (save-excursion
  10987. (goto-char (point-min))
  10988. (setq pos (and (re-search-forward
  10989. (concat "^[ \t]*#\\+\\(?:BEGIN\\|begin\\):[ \t]+" name "\\>") nil t)
  10990. (match-beginning 0))))
  10991. (if pos (goto-char pos))
  10992. pos))
  10993. (defun org-create-dblock (plist)
  10994. "Create a dynamic block section, with parameters taken from PLIST.
  10995. PLIST must contain a :name entry which is used as the name of the block."
  10996. (when (string-match "\\S-" (buffer-substring (point-at-bol) (point-at-eol)))
  10997. (end-of-line 1)
  10998. (newline))
  10999. (let ((col (current-column))
  11000. (name (plist-get plist :name)))
  11001. (insert "#+BEGIN: " name)
  11002. (while plist
  11003. (if (eq (car plist) :name)
  11004. (setq plist (cddr plist))
  11005. (insert " " (prin1-to-string (pop plist)))))
  11006. (insert "\n\n" (make-string col ?\ ) "#+END:\n")
  11007. (beginning-of-line -2)))
  11008. (defun org-prepare-dblock ()
  11009. "Prepare dynamic block for refresh.
  11010. This empties the block, puts the cursor at the insert position and returns
  11011. the property list including an extra property :name with the block name."
  11012. (unless (looking-at org-dblock-start-re)
  11013. (user-error "Not at a dynamic block"))
  11014. (let* ((begdel (1+ (match-end 0)))
  11015. (name (org-no-properties (match-string 1)))
  11016. (params (append (list :name name)
  11017. (read (concat "(" (match-string 3) ")")))))
  11018. (save-excursion
  11019. (beginning-of-line 1)
  11020. (skip-chars-forward " \t")
  11021. (setq params (plist-put params :indentation-column (current-column))))
  11022. (unless (re-search-forward org-dblock-end-re nil t)
  11023. (error "Dynamic block not terminated"))
  11024. (setq params
  11025. (append params
  11026. (list :content (buffer-substring
  11027. begdel (match-beginning 0)))))
  11028. (delete-region begdel (match-beginning 0))
  11029. (goto-char begdel)
  11030. (open-line 1)
  11031. params))
  11032. (defun org-map-dblocks (&optional command)
  11033. "Apply COMMAND to all dynamic blocks in the current buffer.
  11034. If COMMAND is not given, use `org-update-dblock'."
  11035. (let ((cmd (or command 'org-update-dblock)))
  11036. (save-excursion
  11037. (goto-char (point-min))
  11038. (while (re-search-forward org-dblock-start-re nil t)
  11039. (goto-char (match-beginning 0))
  11040. (save-excursion
  11041. (condition-case nil
  11042. (funcall cmd)
  11043. (error (message "Error during update of dynamic block"))))
  11044. (unless (re-search-forward org-dblock-end-re nil t)
  11045. (error "Dynamic block not terminated"))))))
  11046. (defun org-dblock-update (&optional arg)
  11047. "User command for updating dynamic blocks.
  11048. Update the dynamic block at point. With prefix ARG, update all dynamic
  11049. blocks in the buffer."
  11050. (interactive "P")
  11051. (if arg
  11052. (org-update-all-dblocks)
  11053. (or (looking-at org-dblock-start-re)
  11054. (org-beginning-of-dblock))
  11055. (org-update-dblock)))
  11056. (defun org-update-dblock ()
  11057. "Update the dynamic block at point.
  11058. This means to empty the block, parse for parameters and then call
  11059. the correct writing function."
  11060. (interactive)
  11061. (save-excursion
  11062. (let* ((win (selected-window))
  11063. (pos (point))
  11064. (line (org-current-line))
  11065. (params (org-prepare-dblock))
  11066. (name (plist-get params :name))
  11067. (indent (plist-get params :indentation-column))
  11068. (cmd (intern (concat "org-dblock-write:" name))))
  11069. (message "Updating dynamic block `%s' at line %d..." name line)
  11070. (funcall cmd params)
  11071. (message "Updating dynamic block `%s' at line %d...done" name line)
  11072. (goto-char pos)
  11073. (when (and indent (> indent 0))
  11074. (setq indent (make-string indent ?\ ))
  11075. (save-excursion
  11076. (select-window win)
  11077. (org-beginning-of-dblock)
  11078. (forward-line 1)
  11079. (while (not (looking-at org-dblock-end-re))
  11080. (insert indent)
  11081. (beginning-of-line 2))
  11082. (when (looking-at org-dblock-end-re)
  11083. (and (looking-at "[ \t]+")
  11084. (replace-match ""))
  11085. (insert indent)))))))
  11086. (defun org-beginning-of-dblock ()
  11087. "Find the beginning of the dynamic block at point.
  11088. Error if there is no such block at point."
  11089. (let ((pos (point))
  11090. beg)
  11091. (end-of-line 1)
  11092. (if (and (re-search-backward org-dblock-start-re nil t)
  11093. (setq beg (match-beginning 0))
  11094. (re-search-forward org-dblock-end-re nil t)
  11095. (> (match-end 0) pos))
  11096. (goto-char beg)
  11097. (goto-char pos)
  11098. (error "Not in a dynamic block"))))
  11099. (defun org-update-all-dblocks ()
  11100. "Update all dynamic blocks in the buffer.
  11101. This function can be used in a hook."
  11102. (interactive)
  11103. (when (derived-mode-p 'org-mode)
  11104. (org-map-dblocks 'org-update-dblock)))
  11105. ;;;; Completion
  11106. (declare-function org-export-backend-name "org-export" (cl-x))
  11107. (declare-function org-export-backend-options "org-export" (cl-x))
  11108. (defun org-get-export-keywords ()
  11109. "Return a list of all currently understood export keywords.
  11110. Export keywords include options, block names, attributes and
  11111. keywords relative to each registered export back-end."
  11112. (let (keywords)
  11113. (dolist (backend
  11114. (org-bound-and-true-p org-export-registered-backends)
  11115. (delq nil keywords))
  11116. ;; Back-end name (for keywords, like #+LATEX:)
  11117. (push (upcase (symbol-name (org-export-backend-name backend))) keywords)
  11118. (dolist (option-entry (org-export-backend-options backend))
  11119. ;; Back-end options.
  11120. (push (nth 1 option-entry) keywords)))))
  11121. (defconst org-options-keywords
  11122. '("ARCHIVE:" "AUTHOR:" "BIND:" "CATEGORY:" "COLUMNS:" "CREATOR:" "DATE:"
  11123. "DESCRIPTION:" "DRAWERS:" "EMAIL:" "EXCLUDE_TAGS:" "FILETAGS:" "INCLUDE:"
  11124. "INDEX:" "KEYWORDS:" "LANGUAGE:" "MACRO:" "OPTIONS:" "PROPERTY:"
  11125. "PRIORITIES:" "SELECT_TAGS:" "SEQ_TODO:" "SETUPFILE:" "STARTUP:" "TAGS:"
  11126. "TITLE:" "TODO:" "TYP_TODO:" "SELECT_TAGS:" "EXCLUDE_TAGS:"))
  11127. (defcustom org-structure-template-alist
  11128. '(("s" "#+BEGIN_SRC ?\n\n#+END_SRC")
  11129. ("e" "#+BEGIN_EXAMPLE\n?\n#+END_EXAMPLE")
  11130. ("q" "#+BEGIN_QUOTE\n?\n#+END_QUOTE")
  11131. ("v" "#+BEGIN_VERSE\n?\n#+END_VERSE")
  11132. ("V" "#+BEGIN_VERBATIM\n?\n#+END_VERBATIM")
  11133. ("c" "#+BEGIN_CENTER\n?\n#+END_CENTER")
  11134. ("l" "#+BEGIN_LaTeX\n?\n#+END_LaTeX")
  11135. ("L" "#+LaTeX: ")
  11136. ("h" "#+BEGIN_HTML\n?\n#+END_HTML")
  11137. ("H" "#+HTML: ")
  11138. ("a" "#+BEGIN_ASCII\n?\n#+END_ASCII")
  11139. ("A" "#+ASCII: ")
  11140. ("i" "#+INDEX: ?")
  11141. ("I" "#+INCLUDE: %file ?"))
  11142. "Structure completion elements.
  11143. This is a list of abbreviation keys and values. The value gets inserted
  11144. if you type `<' followed by the key and then press the completion key,
  11145. usually `TAB'. %file will be replaced by a file name after prompting
  11146. for the file using completion. The cursor will be placed at the position
  11147. of the `?' in the template.
  11148. There are two templates for each key, the first uses the original Org syntax,
  11149. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  11150. the default when the /org-mtags.el/ module has been loaded. See also the
  11151. variable `org-mtags-prefer-muse-templates'."
  11152. :group 'org-completion
  11153. :type '(repeat
  11154. (list
  11155. (string :tag "Key")
  11156. (string :tag "Template")))
  11157. :version "25.1"
  11158. :package-version '(Org . "8.3"))
  11159. (defun org-try-structure-completion ()
  11160. "Try to complete a structure template before point.
  11161. This looks for strings like \"<e\" on an otherwise empty line and
  11162. expands them."
  11163. (let ((l (buffer-substring (point-at-bol) (point)))
  11164. a)
  11165. (when (and (looking-at "[ \t]*$")
  11166. (string-match "^[ \t]*<\\([a-zA-Z]+\\)$" l)
  11167. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  11168. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  11169. (match-beginning 1)) a)
  11170. t)))
  11171. (defun org-complete-expand-structure-template (start cell)
  11172. "Expand a structure template."
  11173. (let ((rpl (nth 1 cell))
  11174. (ind ""))
  11175. (delete-region start (point))
  11176. (when (string-match "\\`[ \t]*#\\+" rpl)
  11177. (cond
  11178. ((bolp))
  11179. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  11180. (setq ind (buffer-substring (point-at-bol) (point))))
  11181. (t (newline))))
  11182. (setq start (point))
  11183. (if (string-match "%file" rpl)
  11184. (setq rpl (replace-match
  11185. (concat
  11186. "\""
  11187. (save-match-data
  11188. (abbreviate-file-name (read-file-name "Include file: ")))
  11189. "\"")
  11190. t t rpl)))
  11191. (setq rpl (mapconcat 'identity (split-string rpl "\n")
  11192. (concat "\n" ind)))
  11193. (insert rpl)
  11194. (if (re-search-backward "\\?" start t) (delete-char 1))))
  11195. ;;;; TODO, DEADLINE, Comments
  11196. (defun org-toggle-comment ()
  11197. "Change the COMMENT state of an entry."
  11198. (interactive)
  11199. (save-excursion
  11200. (org-back-to-heading)
  11201. (looking-at org-complex-heading-regexp)
  11202. (goto-char (or (match-end 3) (match-end 2) (match-end 1)))
  11203. (skip-chars-forward " \t")
  11204. (unless (memq (char-before) '(?\s ?\t)) (insert " "))
  11205. (if (org-in-commented-heading-p t)
  11206. (delete-region (point)
  11207. (progn (search-forward " " (line-end-position) 'move)
  11208. (skip-chars-forward " \t")
  11209. (point)))
  11210. (insert org-comment-string)
  11211. (unless (eolp) (insert " ")))))
  11212. (defvar org-last-todo-state-is-todo nil
  11213. "This is non-nil when the last TODO state change led to a TODO state.
  11214. If the last change removed the TODO tag or switched to DONE, then
  11215. this is nil.")
  11216. (defvar org-setting-tags nil) ; dynamically skipped
  11217. (defvar org-todo-setup-filter-hook nil
  11218. "Hook for functions that pre-filter todo specs.
  11219. Each function takes a todo spec and returns either nil or the spec
  11220. transformed into canonical form." )
  11221. (defvar org-todo-get-default-hook nil
  11222. "Hook for functions that get a default item for todo.
  11223. Each function takes arguments (NEW-MARK OLD-MARK) and returns either
  11224. nil or a string to be used for the todo mark." )
  11225. (defvar org-agenda-headline-snapshot-before-repeat)
  11226. (defun org-current-effective-time ()
  11227. "Return current time adjusted for `org-extend-today-until' variable."
  11228. (let* ((ct (org-current-time))
  11229. (dct (decode-time ct))
  11230. (ct1
  11231. (cond
  11232. (org-use-last-clock-out-time-as-effective-time
  11233. (or (org-clock-get-last-clock-out-time) ct))
  11234. ((and org-use-effective-time (< (nth 2 dct) org-extend-today-until))
  11235. (encode-time 0 59 23 (1- (nth 3 dct)) (nth 4 dct) (nth 5 dct)))
  11236. (t ct))))
  11237. ct1))
  11238. (defun org-todo-yesterday (&optional arg)
  11239. "Like `org-todo' but the time of change will be 23:59 of yesterday."
  11240. (interactive "P")
  11241. (if (eq major-mode 'org-agenda-mode)
  11242. (apply 'org-agenda-todo-yesterday arg)
  11243. (let* ((org-use-effective-time t)
  11244. (hour (third (decode-time
  11245. (org-current-time))))
  11246. (org-extend-today-until (1+ hour)))
  11247. (org-todo arg))))
  11248. (defvar org-block-entry-blocking ""
  11249. "First entry preventing the TODO state change.")
  11250. (defun org-cancel-repeater ()
  11251. "Cancel a repeater by setting its numeric value to zero."
  11252. (interactive)
  11253. (save-excursion
  11254. (org-back-to-heading t)
  11255. (let ((bound1 (point))
  11256. (bound0 (save-excursion (outline-next-heading) (point))))
  11257. (when (re-search-forward
  11258. (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  11259. org-deadline-time-regexp "\\)\\|\\("
  11260. org-ts-regexp "\\)")
  11261. bound0 t)
  11262. (if (re-search-backward "[ \t]+\\(?:[.+]\\)?\\+\\([0-9]+\\)[hdwmy]" bound1 t)
  11263. (replace-match "0" t nil nil 1))))))
  11264. (defun org-todo (&optional arg)
  11265. "Change the TODO state of an item.
  11266. The state of an item is given by a keyword at the start of the heading,
  11267. like
  11268. *** TODO Write paper
  11269. *** DONE Call mom
  11270. The different keywords are specified in the variable `org-todo-keywords'.
  11271. By default the available states are \"TODO\" and \"DONE\".
  11272. So for this example: when the item starts with TODO, it is changed to DONE.
  11273. When it starts with DONE, the DONE is removed. And when neither TODO nor
  11274. DONE are present, add TODO at the beginning of the heading.
  11275. With \\[universal-argument] prefix arg, use completion to determine the new \
  11276. state.
  11277. With numeric prefix arg, switch to that state.
  11278. With a double \\[universal-argument] prefix, switch to the next set of TODO \
  11279. keywords (nextset).
  11280. With a triple \\[universal-argument] prefix, circumvent any state blocking.
  11281. With a numeric prefix arg of 0, inhibit note taking for the change.
  11282. With a numeric prefix arg of -1, cancel repeater to allow marking as DONE.
  11283. When called through ELisp, arg is also interpreted in the following way:
  11284. 'none -> empty state
  11285. \"\"(empty string) -> switch to empty state
  11286. 'done -> switch to DONE
  11287. 'nextset -> switch to the next set of keywords
  11288. 'previousset -> switch to the previous set of keywords
  11289. \"WAITING\" -> switch to the specified keyword, but only if it
  11290. really is a member of `org-todo-keywords'."
  11291. (interactive "P")
  11292. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  11293. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  11294. 'region-start-level 'region))
  11295. org-loop-over-headlines-in-active-region)
  11296. (org-map-entries
  11297. `(org-todo ,arg)
  11298. org-loop-over-headlines-in-active-region
  11299. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  11300. (if (equal arg '(16)) (setq arg 'nextset))
  11301. (when (equal arg -1) (org-cancel-repeater) (setq arg nil))
  11302. (let ((org-blocker-hook org-blocker-hook)
  11303. commentp
  11304. case-fold-search)
  11305. (when (equal arg '(64))
  11306. (setq arg nil org-blocker-hook nil))
  11307. (when (and org-blocker-hook
  11308. (or org-inhibit-blocking
  11309. (org-entry-get nil "NOBLOCKING")))
  11310. (setq org-blocker-hook nil))
  11311. (save-excursion
  11312. (catch 'exit
  11313. (org-back-to-heading t)
  11314. (when (org-in-commented-heading-p t)
  11315. (org-toggle-comment)
  11316. (setq commentp t))
  11317. (if (looking-at org-outline-regexp) (goto-char (1- (match-end 0))))
  11318. (or (looking-at (concat " +" org-todo-regexp "\\( +\\|[ \t]*$\\)"))
  11319. (looking-at "\\(?: *\\|[ \t]*$\\)"))
  11320. (let* ((match-data (match-data))
  11321. (startpos (point-at-bol))
  11322. (logging (save-match-data (org-entry-get nil "LOGGING" t t)))
  11323. (org-log-done org-log-done)
  11324. (org-log-repeat org-log-repeat)
  11325. (org-todo-log-states org-todo-log-states)
  11326. (org-inhibit-logging
  11327. (if (equal arg 0)
  11328. (progn (setq arg nil) 'note) org-inhibit-logging))
  11329. (this (match-string 1))
  11330. (hl-pos (match-beginning 0))
  11331. (head (org-get-todo-sequence-head this))
  11332. (ass (assoc head org-todo-kwd-alist))
  11333. (interpret (nth 1 ass))
  11334. (done-word (nth 3 ass))
  11335. (final-done-word (nth 4 ass))
  11336. (org-last-state (or this ""))
  11337. (completion-ignore-case t)
  11338. (member (member this org-todo-keywords-1))
  11339. (tail (cdr member))
  11340. (org-state (cond
  11341. ((and org-todo-key-trigger
  11342. (or (and (equal arg '(4))
  11343. (eq org-use-fast-todo-selection 'prefix))
  11344. (and (not arg) org-use-fast-todo-selection
  11345. (not (eq org-use-fast-todo-selection
  11346. 'prefix)))))
  11347. ;; Use fast selection
  11348. (org-fast-todo-selection))
  11349. ((and (equal arg '(4))
  11350. (or (not org-use-fast-todo-selection)
  11351. (not org-todo-key-trigger)))
  11352. ;; Read a state with completion
  11353. (org-icompleting-read
  11354. "State: " (mapcar 'list org-todo-keywords-1)
  11355. nil t))
  11356. ((eq arg 'right)
  11357. (if this
  11358. (if tail (car tail) nil)
  11359. (car org-todo-keywords-1)))
  11360. ((eq arg 'left)
  11361. (if (equal member org-todo-keywords-1)
  11362. nil
  11363. (if this
  11364. (nth (- (length org-todo-keywords-1)
  11365. (length tail) 2)
  11366. org-todo-keywords-1)
  11367. (org-last org-todo-keywords-1))))
  11368. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  11369. (setq arg nil))) ; hack to fall back to cycling
  11370. (arg
  11371. ;; user or caller requests a specific state
  11372. (cond
  11373. ((equal arg "") nil)
  11374. ((eq arg 'none) nil)
  11375. ((eq arg 'done) (or done-word (car org-done-keywords)))
  11376. ((eq arg 'nextset)
  11377. (or (car (cdr (member head org-todo-heads)))
  11378. (car org-todo-heads)))
  11379. ((eq arg 'previousset)
  11380. (let ((org-todo-heads (reverse org-todo-heads)))
  11381. (or (car (cdr (member head org-todo-heads)))
  11382. (car org-todo-heads))))
  11383. ((car (member arg org-todo-keywords-1)))
  11384. ((stringp arg)
  11385. (user-error "State `%s' not valid in this file" arg))
  11386. ((nth (1- (prefix-numeric-value arg))
  11387. org-todo-keywords-1))))
  11388. ((null member) (or head (car org-todo-keywords-1)))
  11389. ((equal this final-done-word) nil) ;; -> make empty
  11390. ((null tail) nil) ;; -> first entry
  11391. ((memq interpret '(type priority))
  11392. (if (eq this-command last-command)
  11393. (car tail)
  11394. (if (> (length tail) 0)
  11395. (or done-word (car org-done-keywords))
  11396. nil)))
  11397. (t
  11398. (car tail))))
  11399. (org-state (or
  11400. (run-hook-with-args-until-success
  11401. 'org-todo-get-default-hook org-state org-last-state)
  11402. org-state))
  11403. (next (if org-state (concat " " org-state " ") " "))
  11404. (change-plist (list :type 'todo-state-change :from this :to org-state
  11405. :position startpos))
  11406. dolog now-done-p)
  11407. (when org-blocker-hook
  11408. (setq org-last-todo-state-is-todo
  11409. (not (member this org-done-keywords)))
  11410. (unless (save-excursion
  11411. (save-match-data
  11412. (org-with-wide-buffer
  11413. (run-hook-with-args-until-failure
  11414. 'org-blocker-hook change-plist))))
  11415. (if (org-called-interactively-p 'interactive)
  11416. (user-error "TODO state change from %s to %s blocked (by \"%s\")"
  11417. this org-state org-block-entry-blocking)
  11418. ;; fail silently
  11419. (message "TODO state change from %s to %s blocked (by \"%s\")"
  11420. this org-state org-block-entry-blocking)
  11421. (throw 'exit nil))))
  11422. (store-match-data match-data)
  11423. (replace-match next t t)
  11424. (cond ((equal this org-state)
  11425. (message "TODO state was already %s" (org-trim next)))
  11426. ((pos-visible-in-window-p hl-pos)
  11427. (message "TODO state changed to %s" (org-trim next))))
  11428. (unless head
  11429. (setq head (org-get-todo-sequence-head org-state)
  11430. ass (assoc head org-todo-kwd-alist)
  11431. interpret (nth 1 ass)
  11432. done-word (nth 3 ass)
  11433. final-done-word (nth 4 ass)))
  11434. (when (memq arg '(nextset previousset))
  11435. (message "Keyword-Set %d/%d: %s"
  11436. (- (length org-todo-sets) -1
  11437. (length (memq (assoc org-state org-todo-sets) org-todo-sets)))
  11438. (length org-todo-sets)
  11439. (mapconcat 'identity (assoc org-state org-todo-sets) " ")))
  11440. (setq org-last-todo-state-is-todo
  11441. (not (member org-state org-done-keywords)))
  11442. (setq now-done-p (and (member org-state org-done-keywords)
  11443. (not (member this org-done-keywords))))
  11444. (and logging (org-local-logging logging))
  11445. (when (and (or org-todo-log-states org-log-done)
  11446. (not (eq org-inhibit-logging t))
  11447. (not (memq arg '(nextset previousset))))
  11448. ;; we need to look at recording a time and note
  11449. (setq dolog (or (nth 1 (assoc org-state org-todo-log-states))
  11450. (nth 2 (assoc this org-todo-log-states))))
  11451. (if (and (eq dolog 'note) (eq org-inhibit-logging 'note))
  11452. (setq dolog 'time))
  11453. (when (or (and (not org-state) (not org-closed-keep-when-no-todo))
  11454. (and org-state
  11455. (member org-state org-not-done-keywords)
  11456. (not (member this org-not-done-keywords))))
  11457. ;; This is now a todo state and was not one before
  11458. ;; If there was a CLOSED time stamp, get rid of it.
  11459. (org-add-planning-info nil nil 'closed))
  11460. (when (and now-done-p org-log-done)
  11461. ;; It is now done, and it was not done before
  11462. (org-add-planning-info 'closed (org-current-effective-time))
  11463. (if (and (not dolog) (eq 'note org-log-done))
  11464. (org-add-log-setup 'done org-state this 'findpos 'note)))
  11465. (when (and org-state dolog)
  11466. ;; This is a non-nil state, and we need to log it
  11467. (org-add-log-setup 'state org-state this 'findpos dolog)))
  11468. ;; Fixup tag positioning
  11469. (org-todo-trigger-tag-changes org-state)
  11470. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  11471. (when org-provide-todo-statistics
  11472. (org-update-parent-todo-statistics))
  11473. (run-hooks 'org-after-todo-state-change-hook)
  11474. (if (and arg (not (member org-state org-done-keywords)))
  11475. (setq head (org-get-todo-sequence-head org-state)))
  11476. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  11477. ;; Do we need to trigger a repeat?
  11478. (when now-done-p
  11479. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  11480. ;; This is for the agenda, take a snapshot of the headline.
  11481. (save-match-data
  11482. (setq org-agenda-headline-snapshot-before-repeat
  11483. (org-get-heading))))
  11484. (org-auto-repeat-maybe org-state))
  11485. ;; Fixup cursor location if close to the keyword
  11486. (if (and (outline-on-heading-p)
  11487. (not (bolp))
  11488. (save-excursion (beginning-of-line 1)
  11489. (looking-at org-todo-line-regexp))
  11490. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  11491. (progn
  11492. (goto-char (or (match-end 2) (match-end 1)))
  11493. (and (looking-at " ") (just-one-space))))
  11494. (when org-trigger-hook
  11495. (save-excursion
  11496. (run-hook-with-args 'org-trigger-hook change-plist)))
  11497. (when commentp (org-toggle-comment))))))))
  11498. (defun org-block-todo-from-children-or-siblings-or-parent (change-plist)
  11499. "Block turning an entry into a TODO, using the hierarchy.
  11500. This checks whether the current task should be blocked from state
  11501. changes. Such blocking occurs when:
  11502. 1. The task has children which are not all in a completed state.
  11503. 2. A task has a parent with the property :ORDERED:, and there
  11504. are siblings prior to the current task with incomplete
  11505. status.
  11506. 3. The parent of the task is blocked because it has siblings that should
  11507. be done first, or is child of a block grandparent TODO entry."
  11508. (if (not org-enforce-todo-dependencies)
  11509. t ; if locally turned off don't block
  11510. (catch 'dont-block
  11511. ;; If this is not a todo state change, or if this entry is already DONE,
  11512. ;; do not block
  11513. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  11514. (member (plist-get change-plist :from)
  11515. (cons 'done org-done-keywords))
  11516. (member (plist-get change-plist :to)
  11517. (cons 'todo org-not-done-keywords))
  11518. (not (plist-get change-plist :to)))
  11519. (throw 'dont-block t))
  11520. ;; If this task has children, and any are undone, it's blocked
  11521. (save-excursion
  11522. (org-back-to-heading t)
  11523. (let ((this-level (funcall outline-level)))
  11524. (outline-next-heading)
  11525. (let ((child-level (funcall outline-level)))
  11526. (while (and (not (eobp))
  11527. (> child-level this-level))
  11528. ;; this todo has children, check whether they are all
  11529. ;; completed
  11530. (if (and (not (org-entry-is-done-p))
  11531. (org-entry-is-todo-p))
  11532. (progn (setq org-block-entry-blocking (org-get-heading))
  11533. (throw 'dont-block nil)))
  11534. (outline-next-heading)
  11535. (setq child-level (funcall outline-level))))))
  11536. ;; Otherwise, if the task's parent has the :ORDERED: property, and
  11537. ;; any previous siblings are undone, it's blocked
  11538. (save-excursion
  11539. (org-back-to-heading t)
  11540. (let* ((pos (point))
  11541. (parent-pos (and (org-up-heading-safe) (point))))
  11542. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  11543. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  11544. (forward-line 1)
  11545. (re-search-forward org-not-done-heading-regexp pos t))
  11546. (setq org-block-entry-blocking (match-string 0))
  11547. (throw 'dont-block nil)) ; block, there is an older sibling not done.
  11548. ;; Search further up the hierarchy, to see if an ancestor is blocked
  11549. (while t
  11550. (goto-char parent-pos)
  11551. (if (not (looking-at org-not-done-heading-regexp))
  11552. (throw 'dont-block t)) ; do not block, parent is not a TODO
  11553. (setq pos (point))
  11554. (setq parent-pos (and (org-up-heading-safe) (point)))
  11555. (if (not parent-pos) (throw 'dont-block t)) ; no parent
  11556. (when (and (org-not-nil (org-entry-get (point) "ORDERED"))
  11557. (forward-line 1)
  11558. (re-search-forward org-not-done-heading-regexp pos t)
  11559. (setq org-block-entry-blocking (org-get-heading)))
  11560. (throw 'dont-block nil)))))))) ; block, older sibling not done.
  11561. (defcustom org-track-ordered-property-with-tag nil
  11562. "Should the ORDERED property also be shown as a tag?
  11563. The ORDERED property decides if an entry should require subtasks to be
  11564. completed in sequence. Since a property is not very visible, setting
  11565. this option means that toggling the ORDERED property with the command
  11566. `org-toggle-ordered-property' will also toggle a tag ORDERED. That tag is
  11567. not relevant for the behavior, but it makes things more visible.
  11568. Note that toggling the tag with tags commands will not change the property
  11569. and therefore not influence behavior!
  11570. This can be t, meaning the tag ORDERED should be used, It can also be a
  11571. string to select a different tag for this task."
  11572. :group 'org-todo
  11573. :type '(choice
  11574. (const :tag "No tracking" nil)
  11575. (const :tag "Track with ORDERED tag" t)
  11576. (string :tag "Use other tag")))
  11577. (defun org-toggle-ordered-property ()
  11578. "Toggle the ORDERED property of the current entry.
  11579. For better visibility, you can track the value of this property with a tag.
  11580. See variable `org-track-ordered-property-with-tag'."
  11581. (interactive)
  11582. (let* ((t1 org-track-ordered-property-with-tag)
  11583. (tag (and t1 (if (stringp t1) t1 "ORDERED"))))
  11584. (save-excursion
  11585. (org-back-to-heading)
  11586. (if (org-entry-get nil "ORDERED")
  11587. (progn
  11588. (org-delete-property "ORDERED")
  11589. (and tag (org-toggle-tag tag 'off))
  11590. (message "Subtasks can be completed in arbitrary order"))
  11591. (org-entry-put nil "ORDERED" "t")
  11592. (and tag (org-toggle-tag tag 'on))
  11593. (message "Subtasks must be completed in sequence")))))
  11594. (defvar org-blocked-by-checkboxes) ; dynamically scoped
  11595. (defun org-block-todo-from-checkboxes (change-plist)
  11596. "Block turning an entry into a TODO, using checkboxes.
  11597. This checks whether the current task should be blocked from state
  11598. changes because there are unchecked boxes in this entry."
  11599. (if (not org-enforce-todo-checkbox-dependencies)
  11600. t ; if locally turned off don't block
  11601. (catch 'dont-block
  11602. ;; If this is not a todo state change, or if this entry is already DONE,
  11603. ;; do not block
  11604. (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
  11605. (member (plist-get change-plist :from)
  11606. (cons 'done org-done-keywords))
  11607. (member (plist-get change-plist :to)
  11608. (cons 'todo org-not-done-keywords))
  11609. (not (plist-get change-plist :to)))
  11610. (throw 'dont-block t))
  11611. ;; If this task has checkboxes that are not checked, it's blocked
  11612. (save-excursion
  11613. (org-back-to-heading t)
  11614. (let ((beg (point)) end)
  11615. (outline-next-heading)
  11616. (setq end (point))
  11617. (goto-char beg)
  11618. (if (org-list-search-forward
  11619. (concat (org-item-beginning-re)
  11620. "\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?"
  11621. "\\[[- ]\\]")
  11622. end t)
  11623. (progn
  11624. (if (boundp 'org-blocked-by-checkboxes)
  11625. (setq org-blocked-by-checkboxes t))
  11626. (throw 'dont-block nil)))))
  11627. t))) ; do not block
  11628. (defun org-entry-blocked-p ()
  11629. "Non-nil if entry at point is blocked."
  11630. (and (not (org-entry-get nil "NOBLOCKING"))
  11631. (member (org-entry-get nil "TODO") org-not-done-keywords)
  11632. (not (run-hook-with-args-until-failure
  11633. 'org-blocker-hook
  11634. (list :type 'todo-state-change
  11635. :position (point)
  11636. :from 'todo
  11637. :to 'done)))))
  11638. (defun org-update-statistics-cookies (all)
  11639. "Update the statistics cookie, either from TODO or from checkboxes.
  11640. This should be called with the cursor in a line with a statistics cookie."
  11641. (interactive "P")
  11642. (if all
  11643. (progn
  11644. (org-update-checkbox-count 'all)
  11645. (org-map-entries 'org-update-parent-todo-statistics))
  11646. (if (not (org-at-heading-p))
  11647. (org-update-checkbox-count)
  11648. (let ((pos (point-marker))
  11649. end l1 l2)
  11650. (ignore-errors (org-back-to-heading t))
  11651. (if (not (org-at-heading-p))
  11652. (org-update-checkbox-count)
  11653. (setq l1 (org-outline-level))
  11654. (setq end (save-excursion
  11655. (outline-next-heading)
  11656. (if (org-at-heading-p) (setq l2 (org-outline-level)))
  11657. (point)))
  11658. (if (and (save-excursion
  11659. (re-search-forward
  11660. "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) \\[[- X]\\]" end t))
  11661. (not (save-excursion (re-search-forward
  11662. ":COOKIE_DATA:.*\\<todo\\>" end t))))
  11663. (org-update-checkbox-count)
  11664. (if (and l2 (> l2 l1))
  11665. (progn
  11666. (goto-char end)
  11667. (org-update-parent-todo-statistics))
  11668. (goto-char pos)
  11669. (beginning-of-line 1)
  11670. (while (re-search-forward
  11671. "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)"
  11672. (point-at-eol) t)
  11673. (replace-match (if (match-end 2) "[100%]" "[0/0]") t t)))))
  11674. (goto-char pos)
  11675. (move-marker pos nil)))))
  11676. (defvar org-entry-property-inherited-from) ;; defined below
  11677. (defun org-update-parent-todo-statistics ()
  11678. "Update any statistics cookie in the parent of the current headline.
  11679. When `org-hierarchical-todo-statistics' is nil, statistics will cover
  11680. the entire subtree and this will travel up the hierarchy and update
  11681. statistics everywhere."
  11682. (let* ((prop (save-excursion (org-up-heading-safe)
  11683. (org-entry-get nil "COOKIE_DATA" 'inherit)))
  11684. (recursive (or (not org-hierarchical-todo-statistics)
  11685. (and prop (string-match "\\<recursive\\>" prop))))
  11686. (lim (or (and prop (marker-position org-entry-property-inherited-from))
  11687. 0))
  11688. (first t)
  11689. (box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  11690. level ltoggle l1 new ndel
  11691. (cnt-all 0) (cnt-done 0) is-percent kwd
  11692. checkbox-beg ov ovs ove cookie-present)
  11693. (catch 'exit
  11694. (save-excursion
  11695. (beginning-of-line 1)
  11696. (setq ltoggle (funcall outline-level))
  11697. ;; Three situations are to consider:
  11698. ;; 1. if `org-hierarchical-todo-statistics' is nil, repeat up
  11699. ;; to the top-level ancestor on the headline;
  11700. ;; 2. If parent has "recursive" property, repeat up to the
  11701. ;; headline setting that property, taking inheritance into
  11702. ;; account;
  11703. ;; 3. Else, move up to direct parent and proceed only once.
  11704. (while (and (setq level (org-up-heading-safe))
  11705. (or recursive first)
  11706. (>= (point) lim))
  11707. (setq first nil cookie-present nil)
  11708. (unless (and level
  11709. (not (string-match
  11710. "\\<checkbox\\>"
  11711. (downcase (or (org-entry-get nil "COOKIE_DATA")
  11712. "")))))
  11713. (throw 'exit nil))
  11714. (while (re-search-forward box-re (point-at-eol) t)
  11715. (setq cnt-all 0 cnt-done 0 cookie-present t)
  11716. (setq is-percent (match-end 2) checkbox-beg (match-beginning 0))
  11717. (save-match-data
  11718. (unless (outline-next-heading) (throw 'exit nil))
  11719. (while (and (looking-at org-complex-heading-regexp)
  11720. (> (setq l1 (length (match-string 1))) level))
  11721. (setq kwd (and (or recursive (= l1 ltoggle))
  11722. (match-string 2)))
  11723. (if (or (eq org-provide-todo-statistics 'all-headlines)
  11724. (and (eq org-provide-todo-statistics t)
  11725. (or (member kwd org-done-keywords)))
  11726. (and (listp org-provide-todo-statistics)
  11727. (stringp (car org-provide-todo-statistics))
  11728. (or (member kwd org-provide-todo-statistics)
  11729. (member kwd org-done-keywords)))
  11730. (and (listp org-provide-todo-statistics)
  11731. (listp (car org-provide-todo-statistics))
  11732. (or (member kwd (car org-provide-todo-statistics))
  11733. (and (member kwd org-done-keywords)
  11734. (member kwd (cadr org-provide-todo-statistics))))))
  11735. (setq cnt-all (1+ cnt-all))
  11736. (if (eq org-provide-todo-statistics t)
  11737. (and kwd (setq cnt-all (1+ cnt-all)))))
  11738. (when (or (and (member org-provide-todo-statistics '(t all-headlines))
  11739. (member kwd org-done-keywords))
  11740. (and (listp org-provide-todo-statistics)
  11741. (listp (car org-provide-todo-statistics))
  11742. (member kwd org-done-keywords)
  11743. (member kwd (cadr org-provide-todo-statistics)))
  11744. (and (listp org-provide-todo-statistics)
  11745. (stringp (car org-provide-todo-statistics))
  11746. (member kwd org-done-keywords)))
  11747. (setq cnt-done (1+ cnt-done)))
  11748. (outline-next-heading)))
  11749. (setq new
  11750. (if is-percent
  11751. (format "[%d%%]" (floor (* 100.0 cnt-done)
  11752. (max 1 cnt-all)))
  11753. (format "[%d/%d]" cnt-done cnt-all))
  11754. ndel (- (match-end 0) checkbox-beg))
  11755. ;; handle overlays when updating cookie from column view
  11756. (when (setq ov (car (overlays-at checkbox-beg)))
  11757. (setq ovs (overlay-start ov) ove (overlay-end ov))
  11758. (delete-overlay ov))
  11759. (goto-char checkbox-beg)
  11760. (insert new)
  11761. (delete-region (point) (+ (point) ndel))
  11762. (when org-auto-align-tags (org-fix-tags-on-the-fly))
  11763. (when ov (move-overlay ov ovs ove)))
  11764. (when cookie-present
  11765. (run-hook-with-args 'org-after-todo-statistics-hook
  11766. cnt-done (- cnt-all cnt-done))))))
  11767. (run-hooks 'org-todo-statistics-hook)))
  11768. (defvar org-after-todo-statistics-hook nil
  11769. "Hook that is called after a TODO statistics cookie has been updated.
  11770. Each function is called with two arguments: the number of not-done entries
  11771. and the number of done entries.
  11772. For example, the following function, when added to this hook, will switch
  11773. an entry to DONE when all children are done, and back to TODO when new
  11774. entries are set to a TODO status. Note that this hook is only called
  11775. when there is a statistics cookie in the headline!
  11776. (defun org-summary-todo (n-done n-not-done)
  11777. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  11778. (let (org-log-done org-log-states) ; turn off logging
  11779. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  11780. ")
  11781. (defvar org-todo-statistics-hook nil
  11782. "Hook that is run whenever Org thinks TODO statistics should be updated.
  11783. This hook runs even if there is no statistics cookie present, in which case
  11784. `org-after-todo-statistics-hook' would not run.")
  11785. (defun org-todo-trigger-tag-changes (state)
  11786. "Apply the changes defined in `org-todo-state-tags-triggers'."
  11787. (let ((l org-todo-state-tags-triggers)
  11788. changes)
  11789. (when (or (not state) (equal state ""))
  11790. (setq changes (append changes (cdr (assoc "" l)))))
  11791. (when (and (stringp state) (> (length state) 0))
  11792. (setq changes (append changes (cdr (assoc state l)))))
  11793. (when (member state org-not-done-keywords)
  11794. (setq changes (append changes (cdr (assoc 'todo l)))))
  11795. (when (member state org-done-keywords)
  11796. (setq changes (append changes (cdr (assoc 'done l)))))
  11797. (dolist (c changes)
  11798. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  11799. (defun org-local-logging (value)
  11800. "Get logging settings from a property VALUE."
  11801. ;; Directly set the variables, they are already local.
  11802. (setq org-log-done nil
  11803. org-log-repeat nil
  11804. org-todo-log-states nil)
  11805. (dolist (w (org-split-string value))
  11806. (let (a)
  11807. (cond
  11808. ((setq a (assoc w org-startup-options))
  11809. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  11810. (set (nth 1 a) (nth 2 a))))
  11811. ((setq a (org-extract-log-state-settings w))
  11812. (and (member (car a) org-todo-keywords-1)
  11813. (push a org-todo-log-states)))))))
  11814. (defun org-get-todo-sequence-head (kwd)
  11815. "Return the head of the TODO sequence to which KWD belongs.
  11816. If KWD is not set, check if there is a text property remembering the
  11817. right sequence."
  11818. (let (p)
  11819. (cond
  11820. ((not kwd)
  11821. (or (get-text-property (point-at-bol) 'org-todo-head)
  11822. (progn
  11823. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  11824. nil (point-at-eol)))
  11825. (get-text-property p 'org-todo-head))))
  11826. ((not (member kwd org-todo-keywords-1))
  11827. (car org-todo-keywords-1))
  11828. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  11829. (defun org-fast-todo-selection ()
  11830. "Fast TODO keyword selection with single keys.
  11831. Returns the new TODO keyword, or nil if no state change should occur."
  11832. (let* ((fulltable org-todo-key-alist)
  11833. (done-keywords org-done-keywords) ;; needed for the faces.
  11834. (maxlen (apply 'max (mapcar
  11835. (lambda (x)
  11836. (if (stringp (car x)) (string-width (car x)) 0))
  11837. fulltable)))
  11838. (expert nil)
  11839. (fwidth (+ maxlen 3 1 3))
  11840. (ncol (/ (- (window-width) 4) fwidth))
  11841. tg cnt e c tbl
  11842. groups ingroup)
  11843. (save-excursion
  11844. (save-window-excursion
  11845. (if expert
  11846. (set-buffer (get-buffer-create " *Org todo*"))
  11847. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  11848. (erase-buffer)
  11849. (org-set-local 'org-done-keywords done-keywords)
  11850. (setq tbl fulltable cnt 0)
  11851. (while (setq e (pop tbl))
  11852. (cond
  11853. ((equal e '(:startgroup))
  11854. (push '() groups) (setq ingroup t)
  11855. (when (not (= cnt 0))
  11856. (setq cnt 0)
  11857. (insert "\n"))
  11858. (insert "{ "))
  11859. ((equal e '(:endgroup))
  11860. (setq ingroup nil cnt 0)
  11861. (insert "}\n"))
  11862. ((equal e '(:newline))
  11863. (when (not (= cnt 0))
  11864. (setq cnt 0)
  11865. (insert "\n")
  11866. (setq e (car tbl))
  11867. (while (equal (car tbl) '(:newline))
  11868. (insert "\n")
  11869. (setq tbl (cdr tbl)))))
  11870. (t
  11871. (setq tg (car e) c (cdr e))
  11872. (if ingroup (push tg (car groups)))
  11873. (setq tg (org-add-props tg nil 'face
  11874. (org-get-todo-face tg)))
  11875. (if (and (= cnt 0) (not ingroup)) (insert " "))
  11876. (insert "[" c "] " tg (make-string
  11877. (- fwidth 4 (length tg)) ?\ ))
  11878. (when (= (setq cnt (1+ cnt)) ncol)
  11879. (insert "\n")
  11880. (if ingroup (insert " "))
  11881. (setq cnt 0)))))
  11882. (insert "\n")
  11883. (goto-char (point-min))
  11884. (if (not expert) (org-fit-window-to-buffer))
  11885. (message "[a-z..]:Set [SPC]:clear")
  11886. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  11887. (cond
  11888. ((or (= c ?\C-g)
  11889. (and (= c ?q) (not (rassoc c fulltable))))
  11890. (setq quit-flag t))
  11891. ((= c ?\ ) nil)
  11892. ((setq e (rassoc c fulltable) tg (car e))
  11893. tg)
  11894. (t (setq quit-flag t)))))))
  11895. (defun org-entry-is-todo-p ()
  11896. (member (org-get-todo-state) org-not-done-keywords))
  11897. (defun org-entry-is-done-p ()
  11898. (member (org-get-todo-state) org-done-keywords))
  11899. (defun org-get-todo-state ()
  11900. "Return the TODO keyword of the current subtree."
  11901. (save-excursion
  11902. (org-back-to-heading t)
  11903. (and (looking-at org-todo-line-regexp)
  11904. (match-end 2)
  11905. (match-string 2))))
  11906. (defun org-at-date-range-p (&optional inactive-ok)
  11907. "Non-nil if point is inside a date range.
  11908. When optional argument INACTIVE-OK is non-nil, also consider
  11909. inactive time ranges.
  11910. When this function returns a non-nil value, match data is set
  11911. according to `org-tr-regexp-both' or `org-tr-regexp', depending
  11912. on INACTIVE-OK."
  11913. (interactive)
  11914. (save-excursion
  11915. (catch 'exit
  11916. (let ((pos (point)))
  11917. (skip-chars-backward "^[<\r\n")
  11918. (skip-chars-backward "<[")
  11919. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  11920. (>= (match-end 0) pos)
  11921. (throw 'exit t))
  11922. (skip-chars-backward "^<[\r\n")
  11923. (skip-chars-backward "<[")
  11924. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  11925. (>= (match-end 0) pos)
  11926. (throw 'exit t)))
  11927. nil)))
  11928. (defun org-get-repeat (&optional tagline)
  11929. "Check if there is a deadline/schedule with repeater in this entry."
  11930. (save-match-data
  11931. (save-excursion
  11932. (org-back-to-heading t)
  11933. (and (re-search-forward (if tagline
  11934. (concat tagline "\\s-*" org-repeat-re)
  11935. org-repeat-re)
  11936. (org-entry-end-position) t)
  11937. (match-string-no-properties 1)))))
  11938. (defvar org-last-changed-timestamp)
  11939. (defvar org-last-inserted-timestamp)
  11940. (defvar org-log-post-message)
  11941. (defvar org-log-note-purpose)
  11942. (defvar org-log-note-how nil)
  11943. (defvar org-log-note-extra)
  11944. (defun org-auto-repeat-maybe (done-word)
  11945. "Check if the current headline contains a repeated deadline/schedule.
  11946. If yes, set TODO state back to what it was and change the base date
  11947. of repeating deadline/scheduled time stamps to new date.
  11948. This function is run automatically after each state change to a DONE state."
  11949. ;; last-state is dynamically scoped into this function
  11950. (let* ((repeat (org-get-repeat))
  11951. (aa (assoc org-last-state org-todo-kwd-alist))
  11952. (interpret (nth 1 aa))
  11953. (head (nth 2 aa))
  11954. (whata '(("h" . hour) ("d" . day) ("m" . month) ("y" . year)))
  11955. (msg "Entry repeats: ")
  11956. (org-log-done nil)
  11957. (org-todo-log-states nil)
  11958. re type n what ts time to-state)
  11959. (when (and repeat (not (zerop (string-to-number (substring repeat 1)))))
  11960. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  11961. (setq to-state (or (org-entry-get nil "REPEAT_TO_STATE")
  11962. org-todo-repeat-to-state))
  11963. (unless (and to-state (member to-state org-todo-keywords-1))
  11964. (setq to-state (if (eq interpret 'type) org-last-state head)))
  11965. (org-todo to-state)
  11966. (when (or org-log-repeat (org-entry-get nil "CLOCK"))
  11967. (org-entry-put nil "LAST_REPEAT" (format-time-string
  11968. (org-time-stamp-format t t))))
  11969. (when org-log-repeat
  11970. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  11971. (memq 'org-add-log-note post-command-hook))
  11972. ;; OK, we are already setup for some record
  11973. (if (eq org-log-repeat 'note)
  11974. ;; make sure we take a note, not only a time stamp
  11975. (setq org-log-note-how 'note))
  11976. ;; Set up for taking a record
  11977. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  11978. org-last-state
  11979. 'findpos org-log-repeat)))
  11980. (org-back-to-heading t)
  11981. (org-add-planning-info nil nil 'closed)
  11982. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  11983. org-deadline-time-regexp "\\)\\|\\("
  11984. org-ts-regexp "\\)"))
  11985. (while (re-search-forward
  11986. re (save-excursion (outline-next-heading) (point)) t)
  11987. (setq type (if (match-end 1) org-scheduled-string
  11988. (if (match-end 3) org-deadline-string "Plain:"))
  11989. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
  11990. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts)
  11991. (setq n (string-to-number (match-string 2 ts))
  11992. what (match-string 3 ts))
  11993. (if (equal what "w") (setq n (* n 7) what "d"))
  11994. (if (and (equal what "h") (not (string-match "[0-9]\\{1,2\\}:[0-9]\\{2\\}" ts)))
  11995. (user-error "Cannot repeat in Repeat in %d hour(s) because no hour has been set" n))
  11996. ;; Preparation, see if we need to modify the start date for the change
  11997. (when (match-end 1)
  11998. (setq time (save-match-data (org-time-string-to-time ts)))
  11999. (cond
  12000. ((equal (match-string 1 ts) ".")
  12001. ;; Shift starting date to today
  12002. (org-timestamp-change
  12003. (- (org-today) (time-to-days time))
  12004. 'day))
  12005. ((equal (match-string 1 ts) "+")
  12006. (let ((nshiftmax 10) (nshift 0))
  12007. (while (or (= nshift 0)
  12008. (<= (time-to-days time)
  12009. (time-to-days (current-time))))
  12010. (when (= (incf nshift) nshiftmax)
  12011. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  12012. (user-error "Abort")))
  12013. (org-timestamp-change n (cdr (assoc what whata)))
  12014. (org-at-timestamp-p t)
  12015. (setq ts (match-string 1))
  12016. (setq time (save-match-data (org-time-string-to-time ts)))))
  12017. (org-timestamp-change (- n) (cdr (assoc what whata)))
  12018. ;; rematch, so that we have everything in place for the real shift
  12019. (org-at-timestamp-p t)
  12020. (setq ts (match-string 1))
  12021. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([hdwmy]\\)" ts))))
  12022. (save-excursion (org-timestamp-change n (cdr (assoc what whata)) nil t))
  12023. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  12024. (setq org-log-post-message msg)
  12025. (message "%s" msg))))
  12026. (defun org-show-todo-tree (arg)
  12027. "Make a compact tree which shows all headlines marked with TODO.
  12028. The tree will show the lines where the regexp matches, and all higher
  12029. headlines above the match.
  12030. With a \\[universal-argument] prefix, prompt for a regexp to match.
  12031. With a numeric prefix N, construct a sparse tree for the Nth element
  12032. of `org-todo-keywords-1'."
  12033. (interactive "P")
  12034. (let ((case-fold-search nil)
  12035. (kwd-re
  12036. (cond ((null arg) org-not-done-regexp)
  12037. ((equal arg '(4))
  12038. (let ((kwd (org-icompleting-read "Keyword (or KWD1|KWD2|...): "
  12039. (mapcar 'list org-todo-keywords-1))))
  12040. (concat "\\("
  12041. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  12042. "\\)\\>")))
  12043. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  12044. (regexp-quote (nth (1- (prefix-numeric-value arg))
  12045. org-todo-keywords-1)))
  12046. (t (user-error "Invalid prefix argument: %s" arg)))))
  12047. (message "%d TODO entries found"
  12048. (org-occur (concat "^" org-outline-regexp " *" kwd-re )))))
  12049. (defun org-deadline (arg &optional time)
  12050. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  12051. With one universal prefix argument, remove any deadline from the item.
  12052. With two universal prefix arguments, prompt for a warning delay.
  12053. With argument TIME, set the deadline at the corresponding date. TIME
  12054. can either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  12055. (interactive "P")
  12056. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  12057. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  12058. 'region-start-level 'region))
  12059. org-loop-over-headlines-in-active-region)
  12060. (org-map-entries
  12061. `(org-deadline ',arg ,time)
  12062. org-loop-over-headlines-in-active-region
  12063. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  12064. (let* ((old-date (org-entry-get nil "DEADLINE"))
  12065. (old-date-time (if old-date (org-time-string-to-time old-date)))
  12066. (repeater (and old-date
  12067. (string-match
  12068. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  12069. old-date)
  12070. (match-string 1 old-date))))
  12071. (cond
  12072. ((equal arg '(4))
  12073. (when (and old-date org-log-redeadline)
  12074. (org-add-log-setup 'deldeadline nil old-date 'findpos
  12075. org-log-redeadline))
  12076. (org-remove-timestamp-with-keyword org-deadline-string)
  12077. (message "Item no longer has a deadline."))
  12078. ((equal arg '(16))
  12079. (save-excursion
  12080. (org-back-to-heading t)
  12081. (if (re-search-forward
  12082. org-deadline-time-regexp
  12083. (save-excursion (outline-next-heading) (point)) t)
  12084. (let* ((rpl0 (match-string 1))
  12085. (rpl (replace-regexp-in-string " -[0-9]+[hdwmy]" "" rpl0)))
  12086. (replace-match
  12087. (concat org-deadline-string
  12088. " <" rpl
  12089. (format " -%dd"
  12090. (abs
  12091. (- (time-to-days
  12092. (save-match-data
  12093. (org-read-date nil t nil "Warn starting from" old-date-time)))
  12094. (time-to-days old-date-time))))
  12095. ">") t t))
  12096. (user-error "No deadline information to update"))))
  12097. (t
  12098. (org-add-planning-info 'deadline time 'closed)
  12099. (when (and old-date org-log-redeadline
  12100. (not (equal old-date
  12101. (substring org-last-inserted-timestamp 1 -1))))
  12102. (org-add-log-setup 'redeadline nil old-date 'findpos
  12103. org-log-redeadline))
  12104. (when repeater
  12105. (save-excursion
  12106. (org-back-to-heading t)
  12107. (when (re-search-forward (concat org-deadline-string " "
  12108. org-last-inserted-timestamp)
  12109. (save-excursion
  12110. (outline-next-heading) (point)) t)
  12111. (goto-char (1- (match-end 0)))
  12112. (insert " " repeater)
  12113. (setq org-last-inserted-timestamp
  12114. (concat (substring org-last-inserted-timestamp 0 -1)
  12115. " " repeater
  12116. (substring org-last-inserted-timestamp -1))))))
  12117. (message "Deadline on %s" org-last-inserted-timestamp))))))
  12118. (defun org-schedule (arg &optional time)
  12119. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  12120. With one universal prefix argument, remove any scheduling date from the item.
  12121. With two universal prefix arguments, prompt for a delay cookie.
  12122. With argument TIME, scheduled at the corresponding date. TIME can
  12123. either be an Org date like \"2011-07-24\" or a delta like \"+2d\"."
  12124. (interactive "P")
  12125. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  12126. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  12127. 'region-start-level 'region))
  12128. org-loop-over-headlines-in-active-region)
  12129. (org-map-entries
  12130. `(org-schedule ',arg ,time)
  12131. org-loop-over-headlines-in-active-region
  12132. cl (if (outline-invisible-p) (org-end-of-subtree nil t))))
  12133. (let* ((old-date (org-entry-get nil "SCHEDULED"))
  12134. (old-date-time (if old-date (org-time-string-to-time old-date)))
  12135. (repeater (and old-date
  12136. (string-match
  12137. "\\([.+-]+[0-9]+[hdwmy]\\(?:[/ ][-+]?[0-9]+[hdwmy]\\)?\\) ?"
  12138. old-date)
  12139. (match-string 1 old-date))))
  12140. (cond
  12141. ((equal arg '(4))
  12142. (progn
  12143. (when (and old-date org-log-reschedule)
  12144. (org-add-log-setup 'delschedule nil old-date 'findpos
  12145. org-log-reschedule))
  12146. (org-remove-timestamp-with-keyword org-scheduled-string)
  12147. (message "Item is no longer scheduled.")))
  12148. ((equal arg '(16))
  12149. (save-excursion
  12150. (org-back-to-heading t)
  12151. (if (re-search-forward
  12152. org-scheduled-time-regexp
  12153. (save-excursion (outline-next-heading) (point)) t)
  12154. (let* ((rpl0 (match-string 1))
  12155. (rpl (replace-regexp-in-string " -[0-9]+[hdwmy]" "" rpl0)))
  12156. (replace-match
  12157. (concat org-scheduled-string
  12158. " <" rpl
  12159. (format " -%dd"
  12160. (abs
  12161. (- (time-to-days
  12162. (save-match-data
  12163. (org-read-date nil t nil "Delay until" old-date-time)))
  12164. (time-to-days old-date-time))))
  12165. ">") t t))
  12166. (user-error "No scheduled information to update"))))
  12167. (t
  12168. (org-add-planning-info 'scheduled time 'closed)
  12169. (when (and old-date org-log-reschedule
  12170. (not (equal old-date
  12171. (substring org-last-inserted-timestamp 1 -1))))
  12172. (org-add-log-setup 'reschedule nil old-date 'findpos
  12173. org-log-reschedule))
  12174. (when repeater
  12175. (save-excursion
  12176. (org-back-to-heading t)
  12177. (when (re-search-forward (concat org-scheduled-string " "
  12178. org-last-inserted-timestamp)
  12179. (save-excursion
  12180. (outline-next-heading) (point)) t)
  12181. (goto-char (1- (match-end 0)))
  12182. (insert " " repeater)
  12183. (setq org-last-inserted-timestamp
  12184. (concat (substring org-last-inserted-timestamp 0 -1)
  12185. " " repeater
  12186. (substring org-last-inserted-timestamp -1))))))
  12187. (message "Scheduled to %s" org-last-inserted-timestamp))))))
  12188. (defun org-get-scheduled-time (pom &optional inherit)
  12189. "Get the scheduled time as a time tuple, of a format suitable
  12190. for calling org-schedule with, or if there is no scheduling,
  12191. returns nil."
  12192. (let ((time (org-entry-get pom "SCHEDULED" inherit)))
  12193. (when time
  12194. (apply 'encode-time (org-parse-time-string time)))))
  12195. (defun org-get-deadline-time (pom &optional inherit)
  12196. "Get the deadline as a time tuple, of a format suitable for
  12197. calling org-deadline with, or if there is no scheduling, returns
  12198. nil."
  12199. (let ((time (org-entry-get pom "DEADLINE" inherit)))
  12200. (when time
  12201. (apply 'encode-time (org-parse-time-string time)))))
  12202. (defun org-remove-timestamp-with-keyword (keyword)
  12203. "Remove all time stamps with KEYWORD in the current entry."
  12204. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  12205. beg)
  12206. (save-excursion
  12207. (org-back-to-heading t)
  12208. (setq beg (point))
  12209. (outline-next-heading)
  12210. (while (re-search-backward re beg t)
  12211. (replace-match "")
  12212. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  12213. (equal (char-before) ?\ ))
  12214. (backward-delete-char 1)
  12215. (if (string-match "^[ \t]*$" (buffer-substring
  12216. (point-at-bol) (point-at-eol)))
  12217. (delete-region (point-at-bol)
  12218. (min (point-max) (1+ (point-at-eol))))))))))
  12219. (defvar org-time-was-given) ; dynamically scoped parameter
  12220. (defvar org-end-time-was-given) ; dynamically scoped parameter
  12221. (defun org-at-planning-p ()
  12222. "Non-nil when point is on a planning info line."
  12223. ;; This is as accurate and faster than `org-element-at-point' since
  12224. ;; planning info location is fixed in the section.
  12225. (org-with-wide-buffer
  12226. (beginning-of-line)
  12227. (and (org-looking-at-p org-planning-line-re)
  12228. (eq (point)
  12229. (ignore-errors
  12230. (if (and (featurep 'org-inlinetask) (org-inlinetask-in-task-p))
  12231. (org-back-to-heading t)
  12232. (org-with-limited-levels (org-back-to-heading t)))
  12233. (line-beginning-position 2))))))
  12234. (defun org-add-planning-info (what &optional time &rest remove)
  12235. "Insert new timestamp with keyword in the planning line.
  12236. WHAT indicates what kind of time stamp to add. It is a symbol
  12237. among `closed', `deadline', `scheduled' and nil. TIME indicates
  12238. the time to use. If none is given, the user is prompted for
  12239. a date. REMOVE indicates what kind of entries to remove. An old
  12240. WHAT entry will also be removed."
  12241. (let (org-time-was-given org-end-time-was-given default-time default-input)
  12242. (catch 'exit
  12243. (when (and (memq what '(scheduled deadline))
  12244. (or (not time)
  12245. (and (stringp time)
  12246. (string-match "^[-+]+[0-9]" time))))
  12247. ;; Try to get a default date/time from existing timestamp
  12248. (save-excursion
  12249. (org-back-to-heading t)
  12250. (let ((end (save-excursion (outline-next-heading) (point))) ts)
  12251. (when (re-search-forward (if (eq what 'scheduled)
  12252. org-scheduled-time-regexp
  12253. org-deadline-time-regexp)
  12254. end t)
  12255. (setq ts (match-string 1)
  12256. default-time (apply 'encode-time (org-parse-time-string ts))
  12257. default-input (and ts (org-get-compact-tod ts)))))))
  12258. (when what
  12259. (setq time
  12260. (if (stringp time)
  12261. ;; This is a string (relative or absolute), set
  12262. ;; proper date.
  12263. (apply #'encode-time
  12264. (org-read-date-analyze
  12265. time default-time (decode-time default-time)))
  12266. ;; If necessary, get the time from the user
  12267. (or time (org-read-date nil 'to-time nil nil
  12268. default-time default-input)))))
  12269. (org-with-wide-buffer
  12270. (org-back-to-heading t)
  12271. (forward-line)
  12272. (unless (bolp) (insert "\n"))
  12273. (cond ((org-looking-at-p org-planning-line-re)
  12274. ;; Move to current indentation.
  12275. (skip-chars-forward " \t")
  12276. ;; Check if we have to remove something.
  12277. (dolist (type (if what (cons what remove) remove))
  12278. (save-excursion
  12279. (when (re-search-forward
  12280. (concat
  12281. " *"
  12282. (case type
  12283. (closed org-closed-time-regexp)
  12284. (deadline org-deadline-time-regexp)
  12285. (scheduled org-scheduled-time-regexp)
  12286. (otherwise
  12287. (error "Invalid planning type: %s" type))))
  12288. (line-end-position) t)
  12289. (replace-match "")
  12290. (when (looking-at "--+<[^>]+>") (replace-match ""))
  12291. (when (and (not what) (eq type 'closed))
  12292. (save-excursion
  12293. (beginning-of-line)
  12294. (when (looking-at "[ \t]*$")
  12295. (delete-region (point)
  12296. (line-beginning-position 2)))))))
  12297. ;; Remove leading white spaces.
  12298. (when (looking-at "[ \t]+") (replace-match ""))))
  12299. ((not what) (throw 'exit nil)) ; Nothing to do.
  12300. (t (insert-before-markers "\n")
  12301. (backward-char 1)
  12302. (when org-adapt-indentation
  12303. (org-indent-to-column (1+ (org-outline-level))))))
  12304. (when what
  12305. ;; Insert planning keyword.
  12306. (insert (case what
  12307. (closed org-closed-string)
  12308. (deadline org-deadline-string)
  12309. (scheduled org-scheduled-string)
  12310. (otherwise (error "Invalid planning type: %s" what)))
  12311. " ")
  12312. ;; Insert associated timestamp.
  12313. (let ((ts (org-insert-time-stamp
  12314. time
  12315. (or org-time-was-given
  12316. (and (eq what 'closed) org-log-done-with-time))
  12317. (eq what 'closed)
  12318. nil nil (list org-end-time-was-given))))
  12319. (unless (eolp) (insert " "))
  12320. ts))))))
  12321. (defvar org-log-note-marker (make-marker))
  12322. (defvar org-log-note-purpose nil)
  12323. (defvar org-log-note-state nil)
  12324. (defvar org-log-note-previous-state nil)
  12325. (defvar org-log-note-extra nil)
  12326. (defvar org-log-note-window-configuration nil)
  12327. (defvar org-log-note-return-to (make-marker))
  12328. (defvar org-log-note-effective-time nil
  12329. "Remembered current time so that dynamically scoped
  12330. `org-extend-today-until' affects tha timestamps in state change
  12331. log")
  12332. (defvar org-log-post-message nil
  12333. "Message to be displayed after a log note has been stored.
  12334. The auto-repeater uses this.")
  12335. (defun org-add-note ()
  12336. "Add a note to the current entry.
  12337. This is done in the same way as adding a state change note."
  12338. (interactive)
  12339. (org-add-log-setup 'note nil nil 'findpos nil))
  12340. (defun org-log-beginning (&optional create)
  12341. "Return expected start of log notes in current entry.
  12342. When optional argument CREATE is non-nil, the function creates
  12343. a drawer to store notes, if necessary. Returned position ignores
  12344. narrowing."
  12345. (org-with-wide-buffer
  12346. (org-end-of-meta-data)
  12347. (let ((end (if (org-at-heading-p) (point)
  12348. (save-excursion (outline-next-heading) (point))))
  12349. (drawer (org-log-into-drawer)))
  12350. (cond
  12351. (drawer
  12352. (let ((regexp (concat "^[ \t]*:" (regexp-quote drawer) ":[ \t]*$"))
  12353. (case-fold-search t))
  12354. (catch 'exit
  12355. ;; Try to find existing drawer.
  12356. (while (re-search-forward regexp end t)
  12357. (let ((element (org-element-at-point)))
  12358. (when (eq (org-element-type element) 'drawer)
  12359. (let ((cend (org-element-property :contents-end element)))
  12360. (when (and (not org-log-states-order-reversed) cend)
  12361. (goto-char cend)))
  12362. (throw 'exit nil))))
  12363. ;; No drawer found. Create one, if permitted.
  12364. (when create
  12365. (unless (bolp) (insert "\n"))
  12366. (let ((beg (point)))
  12367. (insert ":" drawer ":\n:END:\n")
  12368. (org-indent-region beg (point)))
  12369. (end-of-line -1)))))
  12370. (org-log-state-notes-insert-after-drawers
  12371. (while (and (looking-at org-drawer-regexp)
  12372. (progn (goto-char (match-end 0))
  12373. (re-search-forward org-property-end-re end t)))
  12374. (forward-line)))))
  12375. (if (bolp) (point) (line-beginning-position 2))))
  12376. (defun org-add-log-setup (&optional purpose state prev-state findpos how extra)
  12377. "Set up the post command hook to take a note.
  12378. If this is about to TODO state change, the new state is expected in STATE.
  12379. When FINDPOS is non-nil, find the correct position for the note in
  12380. the current entry. If not, assume that it can be inserted at point.
  12381. HOW is an indicator what kind of note should be created.
  12382. EXTRA is additional text that will be inserted into the notes buffer."
  12383. (org-with-wide-buffer
  12384. (when findpos
  12385. (goto-char (org-log-beginning t))
  12386. (unless org-log-states-order-reversed
  12387. (org-skip-over-state-notes)
  12388. (skip-chars-backward " \t\n\r")
  12389. (forward-line)))
  12390. (move-marker org-log-note-marker (point))
  12391. ;; Preserve position even if a property drawer is inserted in the
  12392. ;; process.
  12393. (set-marker-insertion-type org-log-note-marker t)
  12394. (setq org-log-note-purpose purpose
  12395. org-log-note-state state
  12396. org-log-note-previous-state prev-state
  12397. org-log-note-how how
  12398. org-log-note-extra extra
  12399. org-log-note-effective-time (org-current-effective-time))
  12400. (add-hook 'post-command-hook 'org-add-log-note 'append)))
  12401. (defun org-skip-over-state-notes ()
  12402. "Skip past the list of State notes in an entry."
  12403. (when (ignore-errors (goto-char (org-in-item-p)))
  12404. (let* ((struct (org-list-struct))
  12405. (prevs (org-list-prevs-alist struct))
  12406. (regexp
  12407. (concat "[ \t]*- +"
  12408. (replace-regexp-in-string
  12409. " +" " +"
  12410. (org-replace-escapes
  12411. (regexp-quote (cdr (assq 'state org-log-note-headings)))
  12412. `(("%d" . ,org-ts-regexp-inactive)
  12413. ("%D" . ,org-ts-regexp)
  12414. ("%s" . "\"\\S-+\"")
  12415. ("%S" . "\"\\S-+\"")
  12416. ("%t" . ,org-ts-regexp-inactive)
  12417. ("%T" . ,org-ts-regexp)
  12418. ("%u" . ".*?")
  12419. ("%U" . ".*?")))))))
  12420. (while (org-looking-at-p regexp)
  12421. (goto-char (or (org-list-get-next-item (point) struct prevs)
  12422. (org-list-get-item-end (point) struct)))))))
  12423. (defun org-add-log-note (&optional purpose)
  12424. "Pop up a window for taking a note, and add this note later at point."
  12425. (remove-hook 'post-command-hook 'org-add-log-note)
  12426. (setq org-log-note-window-configuration (current-window-configuration))
  12427. (delete-other-windows)
  12428. (move-marker org-log-note-return-to (point))
  12429. (org-pop-to-buffer-same-window (marker-buffer org-log-note-marker))
  12430. (goto-char org-log-note-marker)
  12431. (org-switch-to-buffer-other-window "*Org Note*")
  12432. (erase-buffer)
  12433. (if (memq org-log-note-how '(time state))
  12434. (let (current-prefix-arg) (org-store-log-note))
  12435. (let ((org-inhibit-startup t)) (org-mode))
  12436. (insert (format "# Insert note for %s.
  12437. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  12438. (cond
  12439. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  12440. ((eq org-log-note-purpose 'done) "closed todo item")
  12441. ((eq org-log-note-purpose 'state)
  12442. (format "state change from \"%s\" to \"%s\""
  12443. (or org-log-note-previous-state "")
  12444. (or org-log-note-state "")))
  12445. ((eq org-log-note-purpose 'reschedule)
  12446. "rescheduling")
  12447. ((eq org-log-note-purpose 'delschedule)
  12448. "no longer scheduled")
  12449. ((eq org-log-note-purpose 'redeadline)
  12450. "changing deadline")
  12451. ((eq org-log-note-purpose 'deldeadline)
  12452. "removing deadline")
  12453. ((eq org-log-note-purpose 'refile)
  12454. "refiling")
  12455. ((eq org-log-note-purpose 'note)
  12456. "this entry")
  12457. (t (error "This should not happen")))))
  12458. (if org-log-note-extra (insert org-log-note-extra))
  12459. (org-set-local 'org-finish-function 'org-store-log-note)
  12460. (run-hooks 'org-log-buffer-setup-hook)))
  12461. (defvar org-note-abort nil) ; dynamically scoped
  12462. (defun org-store-log-note ()
  12463. "Finish taking a log note, and insert it to where it belongs."
  12464. (let ((txt (buffer-string)))
  12465. (kill-buffer (current-buffer))
  12466. (let ((note (cdr (assq org-log-note-purpose org-log-note-headings))) lines)
  12467. (while (string-match "\\`# .*\n[ \t\n]*" txt)
  12468. (setq txt (replace-match "" t t txt)))
  12469. (if (string-match "\\s-+\\'" txt)
  12470. (setq txt (replace-match "" t t txt)))
  12471. (setq lines (org-split-string txt "\n"))
  12472. (when (and note (string-match "\\S-" note))
  12473. (setq note
  12474. (org-replace-escapes
  12475. note
  12476. (list (cons "%u" (user-login-name))
  12477. (cons "%U" user-full-name)
  12478. (cons "%t" (format-time-string
  12479. (org-time-stamp-format 'long 'inactive)
  12480. org-log-note-effective-time))
  12481. (cons "%T" (format-time-string
  12482. (org-time-stamp-format 'long nil)
  12483. org-log-note-effective-time))
  12484. (cons "%d" (format-time-string
  12485. (org-time-stamp-format nil 'inactive)
  12486. org-log-note-effective-time))
  12487. (cons "%D" (format-time-string
  12488. (org-time-stamp-format nil nil)
  12489. org-log-note-effective-time))
  12490. (cons "%s" (cond
  12491. ((not org-log-note-state) "")
  12492. ((org-string-match-p org-ts-regexp
  12493. org-log-note-state)
  12494. (format "\"[%s]\""
  12495. (substring org-log-note-state 1 -1)))
  12496. (t (format "\"%s\"" org-log-note-state))))
  12497. (cons "%S"
  12498. (cond
  12499. ((not org-log-note-previous-state) "")
  12500. ((org-string-match-p org-ts-regexp
  12501. org-log-note-previous-state)
  12502. (format "\"[%s]\""
  12503. (substring
  12504. org-log-note-previous-state 1 -1)))
  12505. (t (format "\"%s\""
  12506. org-log-note-previous-state)))))))
  12507. (when lines (setq note (concat note " \\\\")))
  12508. (push note lines))
  12509. (when (or current-prefix-arg org-note-abort)
  12510. (when (org-log-into-drawer)
  12511. (org-remove-empty-drawer-at org-log-note-marker))
  12512. (setq lines nil))
  12513. (when lines
  12514. (with-current-buffer (marker-buffer org-log-note-marker)
  12515. (org-with-wide-buffer
  12516. (goto-char org-log-note-marker)
  12517. (move-marker org-log-note-marker nil)
  12518. ;; Make sure point is at the beginning of an empty line.
  12519. (cond ((not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  12520. ((looking-at "[ \t]*\\S-") (save-excursion (insert "\n"))))
  12521. ;; In an existing list, add a new item at the top level.
  12522. ;; Otherwise, indent line like a regular one.
  12523. (let ((itemp (org-in-item-p)))
  12524. (if itemp
  12525. (org-indent-line-to
  12526. (let ((struct (save-excursion
  12527. (goto-char itemp) (org-list-struct))))
  12528. (org-list-get-ind (org-list-get-top-point struct) struct)))
  12529. (org-indent-line)))
  12530. (insert (org-list-bullet-string "-") (pop lines))
  12531. (let ((ind (org-list-item-body-column (line-beginning-position))))
  12532. (dolist (line lines)
  12533. (insert "\n")
  12534. (org-indent-line-to ind)
  12535. (insert line)))
  12536. (message "Note stored")
  12537. (org-back-to-heading t)
  12538. (org-cycle-hide-drawers 'children))
  12539. ;; Fix `buffer-undo-list' when `org-store-log-note' is called
  12540. ;; from within `org-add-log-note' because `buffer-undo-list'
  12541. ;; is then modified outside of `org-with-remote-undo'.
  12542. (when (eq this-command 'org-agenda-todo)
  12543. (setcdr buffer-undo-list (cddr buffer-undo-list)))))))
  12544. ;; Don't add undo information when called from `org-agenda-todo'
  12545. (let ((buffer-undo-list (eq this-command 'org-agenda-todo)))
  12546. (set-window-configuration org-log-note-window-configuration)
  12547. (with-current-buffer (marker-buffer org-log-note-return-to)
  12548. (goto-char org-log-note-return-to))
  12549. (move-marker org-log-note-return-to nil)
  12550. (and org-log-post-message (message "%s" org-log-post-message))))
  12551. (defun org-remove-empty-drawer-at (pos)
  12552. "Remove an empty drawer at position POS.
  12553. POS may also be a marker."
  12554. (with-current-buffer (if (markerp pos) (marker-buffer pos) (current-buffer))
  12555. (org-with-wide-buffer
  12556. (goto-char pos)
  12557. (let ((drawer (org-element-at-point)))
  12558. (when (and (memq (org-element-type drawer) '(drawer property-drawer))
  12559. (not (org-element-property :contents-begin drawer)))
  12560. (delete-region (org-element-property :begin drawer)
  12561. (progn (goto-char (org-element-property :end drawer))
  12562. (skip-chars-backward " \r\t\n")
  12563. (forward-line)
  12564. (point))))))))
  12565. (defvar org-ts-type nil)
  12566. (defun org-sparse-tree (&optional arg type)
  12567. "Create a sparse tree, prompt for the details.
  12568. This command can create sparse trees. You first need to select the type
  12569. of match used to create the tree:
  12570. t Show all TODO entries.
  12571. T Show entries with a specific TODO keyword.
  12572. m Show entries selected by a tags/property match.
  12573. p Enter a property name and its value (both with completion on existing
  12574. names/values) and show entries with that property.
  12575. r Show entries matching a regular expression (`/' can be used as well).
  12576. b Show deadlines and scheduled items before a date.
  12577. a Show deadlines and scheduled items after a date.
  12578. d Show deadlines due within `org-deadline-warning-days'.
  12579. D Show deadlines and scheduled items between a date range."
  12580. (interactive "P")
  12581. (setq type (or type org-sparse-tree-default-date-type))
  12582. (setq org-ts-type type)
  12583. (message "Sparse tree: [/]regexp [t]odo [T]odo-kwd [m]atch [p]roperty
  12584. [d]eadlines [b]efore-date [a]fter-date [D]ates range
  12585. [c]ycle through date types: %s"
  12586. (case type
  12587. (all "all timestamps")
  12588. (scheduled "only scheduled")
  12589. (deadline "only deadline")
  12590. (active "only active timestamps")
  12591. (inactive "only inactive timestamps")
  12592. (closed "with a closed time-stamp")
  12593. (otherwise "scheduled/deadline")))
  12594. (let ((answer (read-char-exclusive)))
  12595. (case answer
  12596. (?c
  12597. (org-sparse-tree
  12598. arg
  12599. (cadr
  12600. (memq type '(nil all scheduled deadline active inactive closed)))))
  12601. (?d (call-interactively 'org-check-deadlines))
  12602. (?b (call-interactively 'org-check-before-date))
  12603. (?a (call-interactively 'org-check-after-date))
  12604. (?D (call-interactively 'org-check-dates-range))
  12605. (?t (call-interactively 'org-show-todo-tree))
  12606. (?T (org-show-todo-tree '(4)))
  12607. (?m (call-interactively 'org-match-sparse-tree))
  12608. ((?p ?P)
  12609. (let* ((kwd (org-icompleting-read
  12610. "Property: " (mapcar 'list (org-buffer-property-keys))))
  12611. (value (org-icompleting-read
  12612. "Value: " (mapcar 'list (org-property-values kwd)))))
  12613. (unless (string-match "\\`{.*}\\'" value)
  12614. (setq value (concat "\"" value "\"")))
  12615. (org-match-sparse-tree arg (concat kwd "=" value))))
  12616. ((?r ?R ?/) (call-interactively 'org-occur))
  12617. (otherwise (user-error "No such sparse tree command \"%c\"" answer)))))
  12618. (defvar org-occur-highlights nil
  12619. "List of overlays used for occur matches.")
  12620. (make-variable-buffer-local 'org-occur-highlights)
  12621. (defvar org-occur-parameters nil
  12622. "Parameters of the active org-occur calls.
  12623. This is a list, each call to org-occur pushes as cons cell,
  12624. containing the regular expression and the callback, onto the list.
  12625. The list can contain several entries if `org-occur' has been called
  12626. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  12627. will only contain one set of parameters. When the highlights are
  12628. removed (for example with `C-c C-c', or with the next edit (depending
  12629. on `org-remove-highlights-with-change'), this variable is emptied
  12630. as well.")
  12631. (make-variable-buffer-local 'org-occur-parameters)
  12632. (defun org-occur (regexp &optional keep-previous callback)
  12633. "Make a compact tree which shows all matches of REGEXP.
  12634. The tree will show the lines where the regexp matches, and all higher
  12635. headlines above the match. It will also show the heading after the match,
  12636. to make sure editing the matching entry is easy.
  12637. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  12638. call to `org-occur' will be kept, to allow stacking of calls to this
  12639. command.
  12640. If CALLBACK is non-nil, it is a function which is called to confirm
  12641. that the match should indeed be shown."
  12642. (interactive "sRegexp: \nP")
  12643. (when (equal regexp "")
  12644. (user-error "Regexp cannot be empty"))
  12645. (unless keep-previous
  12646. (org-remove-occur-highlights nil nil t))
  12647. (push (cons regexp callback) org-occur-parameters)
  12648. (let ((cnt 0))
  12649. (save-excursion
  12650. (goto-char (point-min))
  12651. (if (or (not keep-previous) ; do not want to keep
  12652. (not org-occur-highlights)) ; no previous matches
  12653. ;; hide everything
  12654. (org-overview))
  12655. (while (re-search-forward regexp nil t)
  12656. (backward-char) ;; FIXME: Match timestamps at the end of a headline
  12657. (when (or (not callback)
  12658. (save-match-data (funcall callback)))
  12659. (setq cnt (1+ cnt))
  12660. (when org-highlight-sparse-tree-matches
  12661. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  12662. (org-show-context 'occur-tree))))
  12663. (when org-remove-highlights-with-change
  12664. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  12665. nil 'local))
  12666. (unless org-sparse-tree-open-archived-trees
  12667. (org-hide-archived-subtrees (point-min) (point-max)))
  12668. (run-hooks 'org-occur-hook)
  12669. (if (org-called-interactively-p 'interactive)
  12670. (message "%d match(es) for regexp %s" cnt regexp))
  12671. cnt))
  12672. (defun org-occur-next-match (&optional n reset)
  12673. "Function for `next-error-function' to find sparse tree matches.
  12674. N is the number of matches to move, when negative move backwards.
  12675. RESET is entirely ignored - this function always goes back to the
  12676. starting point when no match is found."
  12677. (let* ((limit (if (< n 0) (point-min) (point-max)))
  12678. (search-func (if (< n 0)
  12679. 'previous-single-char-property-change
  12680. 'next-single-char-property-change))
  12681. (n (abs n))
  12682. (pos (point))
  12683. p1)
  12684. (catch 'exit
  12685. (while (setq p1 (funcall search-func (point) 'org-type))
  12686. (when (equal p1 limit)
  12687. (goto-char pos)
  12688. (user-error "No more matches"))
  12689. (when (equal (get-char-property p1 'org-type) 'org-occur)
  12690. (setq n (1- n))
  12691. (when (= n 0)
  12692. (goto-char p1)
  12693. (throw 'exit (point))))
  12694. (goto-char p1))
  12695. (goto-char p1)
  12696. (user-error "No more matches"))))
  12697. (defun org-show-context (&optional key)
  12698. "Make sure point and context are visible.
  12699. Optional argument KEY, when non-nil, is a symbol. See
  12700. `org-show-context-detail' for allowed values and how much is to
  12701. be shown."
  12702. (org-show-set-visibility
  12703. (cond ((symbolp org-show-context-detail) org-show-context-detail)
  12704. ((cdr (assq key org-show-context-detail)))
  12705. (t (cdr (assq 'default org-show-context-detail))))))
  12706. (defun org-show-set-visibility (detail)
  12707. "Set visibility around point according to DETAIL.
  12708. DETAIL is either nil, `minimal', `local', `ancestors', `lineage',
  12709. `tree', `canonical' or t. See `org-show-context-detail' for more
  12710. information."
  12711. (unless (org-before-first-heading-p)
  12712. ;; Show current heading and possibly its entry, following headline
  12713. ;; or all children.
  12714. (if (and (org-at-heading-p) (not (eq detail 'local)))
  12715. (org-flag-heading nil)
  12716. (org-show-entry)
  12717. (org-with-limited-levels
  12718. (case detail
  12719. ((tree canonical t) (org-show-children))
  12720. ((nil minimal ancestors))
  12721. (t (save-excursion
  12722. (outline-next-heading)
  12723. (org-flag-heading nil))))))
  12724. ;; Show all siblings.
  12725. (when (eq detail 'lineage) (org-show-siblings))
  12726. ;; Show ancestors, possibly with their children.
  12727. (when (memq detail '(ancestors lineage tree canonical t))
  12728. (save-excursion
  12729. (while (org-up-heading-safe)
  12730. (org-flag-heading nil)
  12731. (when (memq detail '(canonical t)) (org-show-entry))
  12732. (when (memq detail '(tree canonical t)) (org-show-children)))))))
  12733. (defvar org-reveal-start-hook nil
  12734. "Hook run before revealing a location.")
  12735. (defun org-reveal (&optional siblings)
  12736. "Show current entry, hierarchy above it, and the following headline.
  12737. This can be used to show a consistent set of context around
  12738. locations exposed with `org-show-context'.
  12739. With optional argument SIBLINGS, on each level of the hierarchy all
  12740. siblings are shown. This repairs the tree structure to what it would
  12741. look like when opened with hierarchical calls to `org-cycle'.
  12742. With double optional argument \\[universal-argument] \\[universal-argument], \
  12743. go to the parent and show the
  12744. entire tree."
  12745. (interactive "P")
  12746. (run-hooks 'org-reveal-start-hook)
  12747. (cond ((equal siblings '(4)) (org-show-set-visibility 'canonical))
  12748. ((equal siblings '(16))
  12749. (save-excursion
  12750. (when (org-up-heading-safe)
  12751. (org-show-subtree)
  12752. (run-hook-with-args 'org-cycle-hook 'subtree))))
  12753. (t (org-show-set-visibility 'lineage))))
  12754. (defun org-highlight-new-match (beg end)
  12755. "Highlight from BEG to END and mark the highlight is an occur headline."
  12756. (let ((ov (make-overlay beg end)))
  12757. (overlay-put ov 'face 'secondary-selection)
  12758. (overlay-put ov 'org-type 'org-occur)
  12759. (push ov org-occur-highlights)))
  12760. (defun org-remove-occur-highlights (&optional beg end noremove)
  12761. "Remove the occur highlights from the buffer.
  12762. BEG and END are ignored. If NOREMOVE is nil, remove this function
  12763. from the `before-change-functions' in the current buffer."
  12764. (interactive)
  12765. (unless org-inhibit-highlight-removal
  12766. (mapc 'delete-overlay org-occur-highlights)
  12767. (setq org-occur-highlights nil)
  12768. (setq org-occur-parameters nil)
  12769. (unless noremove
  12770. (remove-hook 'before-change-functions
  12771. 'org-remove-occur-highlights 'local))))
  12772. ;;;; Priorities
  12773. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  12774. "Regular expression matching the priority indicator.")
  12775. (defvar org-remove-priority-next-time nil)
  12776. (defun org-priority-up ()
  12777. "Increase the priority of the current item."
  12778. (interactive)
  12779. (org-priority 'up))
  12780. (defun org-priority-down ()
  12781. "Decrease the priority of the current item."
  12782. (interactive)
  12783. (org-priority 'down))
  12784. (defun org-priority (&optional action show)
  12785. "Change the priority of an item.
  12786. ACTION can be `set', `up', `down', or a character."
  12787. (interactive "P")
  12788. (if (equal action '(4))
  12789. (org-show-priority)
  12790. (unless org-enable-priority-commands
  12791. (user-error "Priority commands are disabled"))
  12792. (setq action (or action 'set))
  12793. (let (current new news have remove)
  12794. (save-excursion
  12795. (org-back-to-heading t)
  12796. (if (looking-at org-priority-regexp)
  12797. (setq current (string-to-char (match-string 2))
  12798. have t))
  12799. (cond
  12800. ((eq action 'remove)
  12801. (setq remove t new ?\ ))
  12802. ((or (eq action 'set)
  12803. (if (featurep 'xemacs) (characterp action) (integerp action)))
  12804. (if (not (eq action 'set))
  12805. (setq new action)
  12806. (message "Priority %c-%c, SPC to remove: "
  12807. org-highest-priority org-lowest-priority)
  12808. (save-match-data
  12809. (setq new (read-char-exclusive))))
  12810. (if (and (= (upcase org-highest-priority) org-highest-priority)
  12811. (= (upcase org-lowest-priority) org-lowest-priority))
  12812. (setq new (upcase new)))
  12813. (cond ((equal new ?\ ) (setq remove t))
  12814. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  12815. (user-error "Priority must be between `%c' and `%c'"
  12816. org-highest-priority org-lowest-priority))))
  12817. ((eq action 'up)
  12818. (setq new (if have
  12819. (1- current) ; normal cycling
  12820. ;; last priority was empty
  12821. (if (eq last-command this-command)
  12822. org-lowest-priority ; wrap around empty to lowest
  12823. ;; default
  12824. (if org-priority-start-cycle-with-default
  12825. org-default-priority
  12826. (1- org-default-priority))))))
  12827. ((eq action 'down)
  12828. (setq new (if have
  12829. (1+ current) ; normal cycling
  12830. ;; last priority was empty
  12831. (if (eq last-command this-command)
  12832. org-highest-priority ; wrap around empty to highest
  12833. ;; default
  12834. (if org-priority-start-cycle-with-default
  12835. org-default-priority
  12836. (1+ org-default-priority))))))
  12837. (t (user-error "Invalid action")))
  12838. (if (or (< (upcase new) org-highest-priority)
  12839. (> (upcase new) org-lowest-priority))
  12840. (if (and (memq action '(up down))
  12841. (not have) (not (eq last-command this-command)))
  12842. ;; `new' is from default priority
  12843. (error
  12844. "The default can not be set, see `org-default-priority' why")
  12845. ;; normal cycling: `new' is beyond highest/lowest priority
  12846. ;; and is wrapped around to the empty priority
  12847. (setq remove t)))
  12848. (setq news (format "%c" new))
  12849. (if have
  12850. (if remove
  12851. (replace-match "" t t nil 1)
  12852. (replace-match news t t nil 2))
  12853. (if remove
  12854. (user-error "No priority cookie found in line")
  12855. (let ((case-fold-search nil))
  12856. (looking-at org-todo-line-regexp))
  12857. (if (match-end 2)
  12858. (progn
  12859. (goto-char (match-end 2))
  12860. (insert " [#" news "]"))
  12861. (goto-char (match-beginning 3))
  12862. (insert "[#" news "] "))))
  12863. (org-set-tags nil 'align))
  12864. (if remove
  12865. (message "Priority removed")
  12866. (message "Priority of current item set to %s" news)))))
  12867. (defun org-show-priority ()
  12868. "Show the priority of the current item.
  12869. This priority is composed of the main priority given with the [#A] cookies,
  12870. and by additional input from the age of a schedules or deadline entry."
  12871. (interactive)
  12872. (let ((pri (if (eq major-mode 'org-agenda-mode)
  12873. (org-get-at-bol 'priority)
  12874. (save-excursion
  12875. (save-match-data
  12876. (beginning-of-line)
  12877. (and (looking-at org-heading-regexp)
  12878. (org-get-priority (match-string 0))))))))
  12879. (message "Priority is %d" (if pri pri -1000))))
  12880. (defun org-get-priority (s)
  12881. "Find priority cookie and return priority."
  12882. (save-match-data
  12883. (if (functionp org-get-priority-function)
  12884. (funcall org-get-priority-function)
  12885. (if (not (string-match org-priority-regexp s))
  12886. (* 1000 (- org-lowest-priority org-default-priority))
  12887. (* 1000 (- org-lowest-priority
  12888. (string-to-char (match-string 2 s))))))))
  12889. ;;;; Tags
  12890. (defvar org-agenda-archives-mode)
  12891. (defvar org-map-continue-from nil
  12892. "Position from where mapping should continue.
  12893. Can be set by the action argument to `org-scan-tags' and `org-map-entries'.")
  12894. (defvar org-scanner-tags nil
  12895. "The current tag list while the tags scanner is running.")
  12896. (defvar org-trust-scanner-tags nil
  12897. "Should `org-get-tags-at' use the tags for the scanner.
  12898. This is for internal dynamical scoping only.
  12899. When this is non-nil, the function `org-get-tags-at' will return the value
  12900. of `org-scanner-tags' instead of building the list by itself. This
  12901. can lead to large speed-ups when the tags scanner is used in a file with
  12902. many entries, and when the list of tags is retrieved, for example to
  12903. obtain a list of properties. Building the tags list for each entry in such
  12904. a file becomes an N^2 operation - but with this variable set, it scales
  12905. as N.")
  12906. (defun org-scan-tags (action matcher todo-only &optional start-level)
  12907. "Scan headline tags with inheritance and produce output ACTION.
  12908. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  12909. or `agenda' to produce an entry list for an agenda view. It can also be
  12910. a Lisp form or a function that should be called at each matched headline, in
  12911. this case the return value is a list of all return values from these calls.
  12912. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  12913. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  12914. only lines with a not-done TODO keyword are included in the output.
  12915. This should be the same variable that was scoped into
  12916. and set by `org-make-tags-matcher' when it constructed MATCHER.
  12917. START-LEVEL can be a string with asterisks, reducing the scope to
  12918. headlines matching this string."
  12919. (require 'org-agenda)
  12920. (let* ((re (concat "^"
  12921. (if start-level
  12922. ;; Get the correct level to match
  12923. (concat "\\*\\{" (number-to-string start-level) "\\} ")
  12924. org-outline-regexp)
  12925. " *\\(\\<\\("
  12926. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  12927. (org-re "\\)\\>\\)? *\\(.*?\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*$")))
  12928. (props (list 'face 'default
  12929. 'done-face 'org-agenda-done
  12930. 'undone-face 'default
  12931. 'mouse-face 'highlight
  12932. 'org-not-done-regexp org-not-done-regexp
  12933. 'org-todo-regexp org-todo-regexp
  12934. 'org-complex-heading-regexp org-complex-heading-regexp
  12935. 'help-echo
  12936. (format "mouse-2 or RET jump to org file %s"
  12937. (abbreviate-file-name
  12938. (or (buffer-file-name (buffer-base-buffer))
  12939. (buffer-name (buffer-base-buffer)))))))
  12940. (org-map-continue-from nil)
  12941. lspos tags tags-list
  12942. (tags-alist (list (cons 0 org-file-tags)))
  12943. (llast 0) rtn rtn1 level category i txt
  12944. todo marker entry priority
  12945. ts-date ts-date-type ts-date-pair)
  12946. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  12947. (setq action (list 'lambda nil action)))
  12948. (save-excursion
  12949. (goto-char (point-min))
  12950. (when (eq action 'sparse-tree)
  12951. (org-overview)
  12952. (org-remove-occur-highlights))
  12953. (while (let (case-fold-search)
  12954. (re-search-forward re nil t))
  12955. (setq org-map-continue-from nil)
  12956. (catch :skip
  12957. (setq todo (if (match-end 1) (org-match-string-no-properties 2))
  12958. tags (if (match-end 4) (org-match-string-no-properties 4)))
  12959. (goto-char (setq lspos (match-beginning 0)))
  12960. (setq level (org-reduced-level (org-outline-level))
  12961. category (org-get-category))
  12962. (when (eq action 'agenda)
  12963. (setq ts-date-pair (org-agenda-entry-get-agenda-timestamp (point))
  12964. ts-date (car ts-date-pair)
  12965. ts-date-type (cdr ts-date-pair)))
  12966. (setq i llast llast level)
  12967. ;; remove tag lists from same and sublevels
  12968. (while (>= i level)
  12969. (when (setq entry (assoc i tags-alist))
  12970. (setq tags-alist (delete entry tags-alist)))
  12971. (setq i (1- i)))
  12972. ;; add the next tags
  12973. (when tags
  12974. (setq tags (org-split-string tags ":")
  12975. tags-alist
  12976. (cons (cons level tags) tags-alist)))
  12977. ;; compile tags for current headline
  12978. (setq tags-list
  12979. (if org-use-tag-inheritance
  12980. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  12981. tags)
  12982. org-scanner-tags tags-list)
  12983. (when org-use-tag-inheritance
  12984. (setcdr (car tags-alist)
  12985. (mapcar (lambda (x)
  12986. (setq x (copy-sequence x))
  12987. (org-add-prop-inherited x))
  12988. (cdar tags-alist))))
  12989. (when (and tags org-use-tag-inheritance
  12990. (or (not (eq t org-use-tag-inheritance))
  12991. org-tags-exclude-from-inheritance))
  12992. ;; selective inheritance, remove uninherited ones
  12993. (setcdr (car tags-alist)
  12994. (org-remove-uninherited-tags (cdar tags-alist))))
  12995. (when (and
  12996. ;; eval matcher only when the todo condition is OK
  12997. (and (or (not todo-only) (member todo org-not-done-keywords))
  12998. (let ((case-fold-search t) (org-trust-scanner-tags t))
  12999. (eval matcher)))
  13000. ;; Call the skipper, but return t if it does not skip,
  13001. ;; so that the `and' form continues evaluating
  13002. (progn
  13003. (unless (eq action 'sparse-tree) (org-agenda-skip))
  13004. t)
  13005. ;; Check if timestamps are deselecting this entry
  13006. (or (not todo-only)
  13007. (and (member todo org-not-done-keywords)
  13008. (or (not org-agenda-tags-todo-honor-ignore-options)
  13009. (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item))))))
  13010. ;; select this headline
  13011. (cond
  13012. ((eq action 'sparse-tree)
  13013. (and org-highlight-sparse-tree-matches
  13014. (org-get-heading) (match-end 0)
  13015. (org-highlight-new-match
  13016. (match-beginning 1) (match-end 1)))
  13017. (org-show-context 'tags-tree))
  13018. ((eq action 'agenda)
  13019. (setq txt (org-agenda-format-item
  13020. ""
  13021. (concat
  13022. (if (eq org-tags-match-list-sublevels 'indented)
  13023. (make-string (1- level) ?.) "")
  13024. (org-get-heading))
  13025. (make-string level ?\s)
  13026. category
  13027. tags-list)
  13028. priority (org-get-priority txt))
  13029. (goto-char lspos)
  13030. (setq marker (org-agenda-new-marker))
  13031. (org-add-props txt props
  13032. 'org-marker marker 'org-hd-marker marker 'org-category category
  13033. 'todo-state todo
  13034. 'ts-date ts-date
  13035. 'priority priority
  13036. 'type (concat "tagsmatch" ts-date-type))
  13037. (push txt rtn))
  13038. ((functionp action)
  13039. (setq org-map-continue-from nil)
  13040. (save-excursion
  13041. (setq rtn1 (funcall action))
  13042. (push rtn1 rtn)))
  13043. (t (user-error "Invalid action")))
  13044. ;; if we are to skip sublevels, jump to end of subtree
  13045. (unless org-tags-match-list-sublevels
  13046. (org-end-of-subtree t)
  13047. (backward-char 1))))
  13048. ;; Get the correct position from where to continue
  13049. (if org-map-continue-from
  13050. (goto-char org-map-continue-from)
  13051. (and (= (point) lspos) (end-of-line 1)))))
  13052. (when (and (eq action 'sparse-tree)
  13053. (not org-sparse-tree-open-archived-trees))
  13054. (org-hide-archived-subtrees (point-min) (point-max)))
  13055. (nreverse rtn)))
  13056. (defun org-remove-uninherited-tags (tags)
  13057. "Remove all tags that are not inherited from the list TAGS."
  13058. (cond
  13059. ((eq org-use-tag-inheritance t)
  13060. (if org-tags-exclude-from-inheritance
  13061. (org-delete-all org-tags-exclude-from-inheritance tags)
  13062. tags))
  13063. ((not org-use-tag-inheritance) nil)
  13064. ((stringp org-use-tag-inheritance)
  13065. (delq nil (mapcar
  13066. (lambda (x)
  13067. (if (and (string-match org-use-tag-inheritance x)
  13068. (not (member x org-tags-exclude-from-inheritance)))
  13069. x nil))
  13070. tags)))
  13071. ((listp org-use-tag-inheritance)
  13072. (delq nil (mapcar
  13073. (lambda (x)
  13074. (if (member x org-use-tag-inheritance) x nil))
  13075. tags)))))
  13076. (defun org-match-sparse-tree (&optional todo-only match)
  13077. "Create a sparse tree according to tags string MATCH.
  13078. MATCH can contain positive and negative selection of tags, like
  13079. \"+WORK+URGENT-WITHBOSS\".
  13080. If optional argument TODO-ONLY is non-nil, only select lines that are
  13081. also TODO lines."
  13082. (interactive "P")
  13083. (org-agenda-prepare-buffers (list (current-buffer)))
  13084. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  13085. (defalias 'org-tags-sparse-tree 'org-match-sparse-tree)
  13086. (defvar org-cached-props nil)
  13087. (defun org-cached-entry-get (pom property)
  13088. (if (or (eq t org-use-property-inheritance)
  13089. (and (stringp org-use-property-inheritance)
  13090. (let ((case-fold-search t))
  13091. (org-string-match-p org-use-property-inheritance property)))
  13092. (and (listp org-use-property-inheritance)
  13093. (member-ignore-case property org-use-property-inheritance)))
  13094. ;; Caching is not possible, check it directly.
  13095. (org-entry-get pom property 'inherit)
  13096. ;; Get all properties, so we can do complicated checks easily.
  13097. (cdr (assoc-string property
  13098. (or org-cached-props
  13099. (setq org-cached-props (org-entry-properties pom)))
  13100. t))))
  13101. (defun org-global-tags-completion-table (&optional files)
  13102. "Return the list of all tags in all agenda buffer/files.
  13103. Optional FILES argument is a list of files which can be used
  13104. instead of the agenda files."
  13105. (save-excursion
  13106. (org-uniquify
  13107. (delq nil
  13108. (apply 'append
  13109. (mapcar
  13110. (lambda (file)
  13111. (set-buffer (find-file-noselect file))
  13112. (append (org-get-buffer-tags)
  13113. (mapcar (lambda (x) (if (stringp (car-safe x))
  13114. (list (car-safe x)) nil))
  13115. org-tag-alist)))
  13116. (if (and files (car files))
  13117. files
  13118. (org-agenda-files))))))))
  13119. (defun org-make-tags-matcher (match)
  13120. "Create the TAGS/TODO matcher form for the selection string MATCH.
  13121. The variable `todo-only' is scoped dynamically into this function.
  13122. It will be set to t if the matcher restricts matching to TODO entries,
  13123. otherwise will not be touched.
  13124. Returns a cons of the selection string MATCH and the constructed
  13125. lisp form implementing the matcher. The matcher is to be evaluated
  13126. at an Org entry, with point on the headline, and returns t if the
  13127. entry matches the selection string MATCH. The returned lisp form
  13128. references two variables with information about the entry, which
  13129. must be bound around the form's evaluation: todo, the TODO keyword
  13130. at the entry (or nil of none); and tags-list, the list of all tags
  13131. at the entry including inherited ones. Additionally, the category
  13132. of the entry (if any) must be specified as the text property
  13133. 'org-category on the headline.
  13134. See also `org-scan-tags'.
  13135. "
  13136. (declare (special todo-only))
  13137. (unless (boundp 'todo-only)
  13138. (error "`org-make-tags-matcher' expects todo-only to be scoped in"))
  13139. (unless match
  13140. ;; Get a new match request, with completion against the global
  13141. ;; tags table and the local tags in current buffer
  13142. (let ((org-last-tags-completion-table
  13143. (org-uniquify
  13144. (delq nil (append (org-get-buffer-tags)
  13145. (org-global-tags-completion-table))))))
  13146. (setq match (org-completing-read-no-i
  13147. "Match: " 'org-tags-completion-function nil nil nil
  13148. 'org-tags-history))))
  13149. ;; Parse the string and create a lisp form
  13150. (let ((match0 match)
  13151. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\(\\(?:[[:alnum:]_]+\\(?:\\\\-\\)*\\)+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@#%]+\\)"))
  13152. minus tag mm
  13153. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  13154. orterms term orlist re-p str-p level-p level-op time-p
  13155. prop-p pn pv po gv rest (start 0) (ss 0))
  13156. ;; Expand group tags
  13157. (setq match (org-tags-expand match))
  13158. ;; Check if there is a TODO part of this match, which would be the
  13159. ;; part after a "/". TO make sure that this slash is not part of
  13160. ;; a property value to be matched against, we also check that there
  13161. ;; is no " after that slash.
  13162. ;; First, find the last slash
  13163. (while (string-match "/+" match ss)
  13164. (setq start (match-beginning 0) ss (match-end 0)))
  13165. (if (and (string-match "/+" match start)
  13166. (not (save-match-data (string-match "\"" match start))))
  13167. ;; match contains also a todo-matching request
  13168. (progn
  13169. (setq tagsmatch (substring match 0 (match-beginning 0))
  13170. todomatch (substring match (match-end 0)))
  13171. (if (string-match "^!" todomatch)
  13172. (setq todo-only t todomatch (substring todomatch 1)))
  13173. (if (string-match "^\\s-*$" todomatch)
  13174. (setq todomatch nil)))
  13175. ;; only matching tags
  13176. (setq tagsmatch match todomatch nil))
  13177. ;; Make the tags matcher
  13178. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  13179. (setq tagsmatcher t)
  13180. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  13181. (while (setq term (pop orterms))
  13182. (while (and (equal (substring term -1) "\\") orterms)
  13183. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  13184. (while (string-match re term)
  13185. (setq rest (substring term (match-end 0))
  13186. minus (and (match-end 1)
  13187. (equal (match-string 1 term) "-"))
  13188. tag (save-match-data (replace-regexp-in-string
  13189. "\\\\-" "-"
  13190. (match-string 2 term)))
  13191. re-p (equal (string-to-char tag) ?{)
  13192. level-p (match-end 4)
  13193. prop-p (match-end 5)
  13194. mm (cond
  13195. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  13196. (level-p
  13197. (setq level-op (org-op-to-function (match-string 3 term)))
  13198. `(,level-op level ,(string-to-number
  13199. (match-string 4 term))))
  13200. (prop-p
  13201. (setq pn (match-string 5 term)
  13202. po (match-string 6 term)
  13203. pv (match-string 7 term)
  13204. re-p (equal (string-to-char pv) ?{)
  13205. str-p (equal (string-to-char pv) ?\")
  13206. time-p (save-match-data
  13207. (string-match "^\"[[<].*[]>]\"$" pv))
  13208. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  13209. (if time-p (setq pv (org-matcher-time pv)))
  13210. (setq po (org-op-to-function po (if time-p 'time str-p)))
  13211. (cond
  13212. ((equal pn "CATEGORY")
  13213. (setq gv '(get-text-property (point) 'org-category)))
  13214. ((equal pn "TODO")
  13215. (setq gv 'todo))
  13216. (t
  13217. (setq gv `(org-cached-entry-get nil ,pn))))
  13218. (if re-p
  13219. (if (eq po 'org<>)
  13220. `(not (string-match ,pv (or ,gv "")))
  13221. `(string-match ,pv (or ,gv "")))
  13222. (if str-p
  13223. `(,po (or ,gv "") ,pv)
  13224. `(,po (string-to-number (or ,gv ""))
  13225. ,(string-to-number pv) ))))
  13226. (t `(member ,tag tags-list)))
  13227. mm (if minus (list 'not mm) mm)
  13228. term rest)
  13229. (push mm tagsmatcher))
  13230. (push (if (> (length tagsmatcher) 1)
  13231. (cons 'and tagsmatcher)
  13232. (car tagsmatcher))
  13233. orlist)
  13234. (setq tagsmatcher nil))
  13235. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  13236. (setq tagsmatcher
  13237. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  13238. ;; Make the todo matcher
  13239. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  13240. (setq todomatcher t)
  13241. (setq orterms (org-split-string todomatch "|") orlist nil)
  13242. (dolist (term orterms)
  13243. (while (string-match re term)
  13244. (setq minus (and (match-end 1)
  13245. (equal (match-string 1 term) "-"))
  13246. kwd (match-string 2 term)
  13247. re-p (equal (string-to-char kwd) ?{)
  13248. term (substring term (match-end 0))
  13249. mm (if re-p
  13250. `(string-match ,(substring kwd 1 -1) todo)
  13251. (list 'equal 'todo kwd))
  13252. mm (if minus (list 'not mm) mm))
  13253. (push mm todomatcher))
  13254. (push (if (> (length todomatcher) 1)
  13255. (cons 'and todomatcher)
  13256. (car todomatcher))
  13257. orlist)
  13258. (setq todomatcher nil))
  13259. (setq todomatcher (if (> (length orlist) 1)
  13260. (cons 'or orlist) (car orlist))))
  13261. ;; Return the string and lisp forms of the matcher
  13262. (setq matcher (if todomatcher
  13263. (list 'and tagsmatcher todomatcher)
  13264. tagsmatcher))
  13265. (when todo-only
  13266. (setq matcher (list 'and '(member todo org-not-done-keywords)
  13267. matcher)))
  13268. (cons match0 matcher)))
  13269. (defun org-tags-expand (match &optional single-as-list downcased tags-already-expanded)
  13270. "Expand group tags in MATCH.
  13271. This replaces every group tag in MATCH with a regexp tag search.
  13272. For example, a group tag \"Work\" defined as { Work : Lab Conf }
  13273. will be replaced like this:
  13274. Work => {\\<\\(?:Work\\|Lab\\|Conf\\)\\>}
  13275. +Work => +{\\<\\(?:Work\\|Lab\\|Conf\\)\\>}
  13276. -Work => -{\\<\\(?:Work\\|Lab\\|Conf\\)\\>}
  13277. Replacing by a regexp preserves the structure of the match.
  13278. E.g., this expansion
  13279. Work|Home => {\\(?:Work\\|Lab\\|Conf\\}|Home
  13280. will match anything tagged with \"Lab\" and \"Home\", or tagged
  13281. with \"Conf\" and \"Home\" or tagged with \"Work\" and \"home\".
  13282. A group tag in MATCH can contain regular expressions of its own.
  13283. For example, a group tag \"Proj\" defined as { Proj : {P@.+} }
  13284. will be replaced like this:
  13285. Proj => {\\<\\(?:Proj\\)\\>\\|P@.+}
  13286. When the optional argument SINGLE-AS-LIST is non-nil, MATCH is
  13287. assumed to be a single group tag, and the function will return
  13288. the list of tags in this group.
  13289. When DOWNCASE is non-nil, expand downcased TAGS."
  13290. (if org-group-tags
  13291. (let* ((case-fold-search t)
  13292. (stable org-mode-syntax-table)
  13293. (taggroups (or org-tag-groups-alist-for-agenda org-tag-groups-alist))
  13294. (taggroups (if downcased
  13295. (mapcar (lambda (tg) (mapcar #'downcase tg))
  13296. taggroups)
  13297. taggroups))
  13298. (taggroups-keys (mapcar #'car taggroups))
  13299. (return-match (if downcased (downcase match) match))
  13300. (count 0)
  13301. (work-already-expanded tags-already-expanded)
  13302. regexps-in-match tags-in-group regexp-in-group regexp-in-group-escaped)
  13303. ;; @ and _ are allowed as word-components in tags.
  13304. (modify-syntax-entry ?@ "w" stable)
  13305. (modify-syntax-entry ?_ "w" stable)
  13306. ;; Temporarily replace regexp-expressions in the match-expression.
  13307. (while (string-match "{.+?}" return-match)
  13308. (incf count)
  13309. (push (match-string 0 return-match) regexps-in-match)
  13310. (setq return-match (replace-match (format "<%d>" count) t nil return-match)))
  13311. (while (and taggroups-keys
  13312. (with-syntax-table stable
  13313. (string-match
  13314. (concat "\\(?1:[+-]?\\)\\(?2:\\<"
  13315. (regexp-opt taggroups-keys) "\\>\\)")
  13316. return-match)))
  13317. (let* ((dir (match-string 1 return-match))
  13318. (tag (match-string 2 return-match))
  13319. (tag (if downcased (downcase tag) tag)))
  13320. (unless (or (get-text-property 0 'grouptag (match-string 2 return-match))
  13321. (member tag work-already-expanded))
  13322. (setq tags-in-group (assoc tag taggroups))
  13323. (push tag work-already-expanded)
  13324. ;; Recursively expand each tag in the group, if the tag hasn't
  13325. ;; already been expanded. Restore the match-data after all recursive calls.
  13326. (save-match-data
  13327. (let (tags-expanded)
  13328. (dolist (x (cdr tags-in-group))
  13329. (if (and (member x taggroups-keys)
  13330. (not (member x work-already-expanded)))
  13331. (setq tags-expanded
  13332. (delete-dups
  13333. (append
  13334. (org-tags-expand x t downcased
  13335. work-already-expanded)
  13336. tags-expanded)))
  13337. (setq tags-expanded
  13338. (append (list x) tags-expanded)))
  13339. (setq work-already-expanded
  13340. (delete-dups
  13341. (append tags-expanded
  13342. work-already-expanded))))
  13343. (setq tags-in-group
  13344. (delete-dups (cons (car tags-in-group)
  13345. tags-expanded)))))
  13346. ;; Filter tag-regexps from tags.
  13347. (setq regexp-in-group-escaped
  13348. (delq nil (mapcar (lambda (x)
  13349. (if (stringp x)
  13350. (and (equal "{" (substring x 0 1))
  13351. (equal "}" (substring x -1))
  13352. x)
  13353. x))
  13354. tags-in-group))
  13355. regexp-in-group
  13356. (mapcar (lambda (x)
  13357. (substring x 1 -1))
  13358. regexp-in-group-escaped)
  13359. tags-in-group
  13360. (delq nil (mapcar (lambda (x)
  13361. (if (stringp x)
  13362. (and (not (equal "{" (substring x 0 1)))
  13363. (not (equal "}" (substring x -1)))
  13364. x)
  13365. x))
  13366. tags-in-group)))
  13367. ;; If single-as-list, do no more in the while-loop.
  13368. (if (not single-as-list)
  13369. (progn
  13370. (when regexp-in-group
  13371. (setq regexp-in-group
  13372. (concat "\\|"
  13373. (mapconcat 'identity regexp-in-group
  13374. "\\|"))))
  13375. (setq tags-in-group
  13376. (concat dir
  13377. "{\\<"
  13378. (regexp-opt tags-in-group)
  13379. "\\>"
  13380. regexp-in-group
  13381. "}"))
  13382. (when (stringp tags-in-group)
  13383. (org-add-props tags-in-group '(grouptag t)))
  13384. (setq return-match
  13385. (replace-match tags-in-group t t return-match)))
  13386. (setq tags-in-group
  13387. (append regexp-in-group-escaped tags-in-group))))
  13388. (setq taggroups-keys (delete tag taggroups-keys))))
  13389. ;; Add the regular expressions back into the match-expression again.
  13390. (while regexps-in-match
  13391. (setq return-match (replace-regexp-in-string (format "<%d>" count)
  13392. (pop regexps-in-match)
  13393. return-match t t))
  13394. (decf count))
  13395. (if single-as-list
  13396. (if tags-in-group tags-in-group (list return-match))
  13397. return-match))
  13398. (if single-as-list
  13399. (list (if downcased (downcase match) match))
  13400. match)))
  13401. (defun org-op-to-function (op &optional stringp)
  13402. "Turn an operator into the appropriate function."
  13403. (setq op
  13404. (cond
  13405. ((equal op "<" ) '(< string< org-time<))
  13406. ((equal op ">" ) '(> org-string> org-time>))
  13407. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  13408. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  13409. ((member op '("=" "==")) '(= string= org-time=))
  13410. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  13411. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  13412. (defun org<> (a b) (not (= a b)))
  13413. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  13414. (defun org-string>= (a b) (not (string< a b)))
  13415. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  13416. (defun org-string<> (a b) (not (string= a b)))
  13417. (defun org-time= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (= a b)))
  13418. (defun org-time< (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (< a b)))
  13419. (defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<= a b)))
  13420. (defun org-time> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (> a b)))
  13421. (defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>= a b)))
  13422. (defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
  13423. (defun org-2ft (s)
  13424. "Convert S to a floating point time.
  13425. If S is already a number, just return it. If it is a string, parse
  13426. it as a time string and apply `float-time' to it. If S is nil, just return 0."
  13427. (cond
  13428. ((numberp s) s)
  13429. ((stringp s)
  13430. (condition-case nil
  13431. (float-time (apply 'encode-time (org-parse-time-string s)))
  13432. (error 0.)))
  13433. (t 0.)))
  13434. (defun org-time-today ()
  13435. "Time in seconds today at 0:00.
  13436. Returns the float number of seconds since the beginning of the
  13437. epoch to the beginning of today (00:00)."
  13438. (float-time (apply 'encode-time
  13439. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  13440. (defun org-matcher-time (s)
  13441. "Interpret a time comparison value."
  13442. (save-match-data
  13443. (cond
  13444. ((string= s "<now>") (float-time))
  13445. ((string= s "<today>") (org-time-today))
  13446. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  13447. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  13448. ((string-match "^<\\([-+][0-9]+\\)\\([hdwmy]\\)>$" s)
  13449. (+ (org-time-today)
  13450. (* (string-to-number (match-string 1 s))
  13451. (cdr (assoc (match-string 2 s)
  13452. '(("d" . 86400.0) ("w" . 604800.0)
  13453. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  13454. (t (org-2ft s)))))
  13455. (defun org-match-any-p (re list)
  13456. "Does re match any element of list?"
  13457. (setq list (mapcar (lambda (x) (string-match re x)) list))
  13458. (delq nil list))
  13459. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  13460. (defvar org-tags-overlay (make-overlay 1 1))
  13461. (org-detach-overlay org-tags-overlay)
  13462. (defun org-get-local-tags-at (&optional pos)
  13463. "Get a list of tags defined in the current headline."
  13464. (org-get-tags-at pos 'local))
  13465. (defun org-get-local-tags ()
  13466. "Get a list of tags defined in the current headline."
  13467. (org-get-tags-at nil 'local))
  13468. (defun org-get-tags-at (&optional pos local)
  13469. "Get a list of all headline tags applicable at POS.
  13470. POS defaults to point. If tags are inherited, the list contains
  13471. the targets in the same sequence as the headlines appear, i.e.
  13472. the tags of the current headline come last.
  13473. When LOCAL is non-nil, only return tags from the current headline,
  13474. ignore inherited ones."
  13475. (interactive)
  13476. (if (and org-trust-scanner-tags
  13477. (or (not pos) (equal pos (point)))
  13478. (not local))
  13479. org-scanner-tags
  13480. (let (tags ltags lastpos parent)
  13481. (save-excursion
  13482. (save-restriction
  13483. (widen)
  13484. (goto-char (or pos (point)))
  13485. (save-match-data
  13486. (catch 'done
  13487. (condition-case nil
  13488. (progn
  13489. (org-back-to-heading t)
  13490. (while (not (equal lastpos (point)))
  13491. (setq lastpos (point))
  13492. (when (looking-at
  13493. (org-re "[^\r\n]+?:\\([[:alnum:]_@#%:]+\\):[ \t]*$"))
  13494. (setq ltags (org-split-string
  13495. (org-match-string-no-properties 1) ":"))
  13496. (when parent
  13497. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  13498. (setq tags (append
  13499. (if parent
  13500. (org-remove-uninherited-tags ltags)
  13501. ltags)
  13502. tags)))
  13503. (or org-use-tag-inheritance (throw 'done t))
  13504. (if local (throw 'done t))
  13505. (or (org-up-heading-safe) (error nil))
  13506. (setq parent t)))
  13507. (error nil)))))
  13508. (if local
  13509. tags
  13510. (reverse (delete-dups
  13511. (reverse (append
  13512. (org-remove-uninherited-tags
  13513. org-file-tags)
  13514. tags)))))))))
  13515. (defun org-add-prop-inherited (s)
  13516. (add-text-properties 0 (length s) '(inherited t) s)
  13517. s)
  13518. (defun org-toggle-tag (tag &optional onoff)
  13519. "Toggle the tag TAG for the current line.
  13520. If ONOFF is `on' or `off', don't toggle but set to this state."
  13521. (let (res current)
  13522. (save-excursion
  13523. (org-back-to-heading t)
  13524. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@#%:]+\\):[ \t]*$")
  13525. (point-at-eol) t)
  13526. (progn
  13527. (setq current (match-string 1))
  13528. (replace-match ""))
  13529. (setq current ""))
  13530. (setq current (nreverse (org-split-string current ":")))
  13531. (cond
  13532. ((eq onoff 'on)
  13533. (setq res t)
  13534. (or (member tag current) (push tag current)))
  13535. ((eq onoff 'off)
  13536. (or (not (member tag current)) (setq current (delete tag current))))
  13537. (t (if (member tag current)
  13538. (setq current (delete tag current))
  13539. (setq res t)
  13540. (push tag current))))
  13541. (end-of-line 1)
  13542. (if current
  13543. (progn
  13544. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  13545. (org-set-tags nil t))
  13546. (delete-horizontal-space))
  13547. (run-hooks 'org-after-tags-change-hook))
  13548. res))
  13549. (defun org-align-tags-here (to-col)
  13550. ;; Assumes that this is a headline
  13551. "Align tags on the current headline to TO-COL."
  13552. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  13553. (beginning-of-line 1)
  13554. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13555. (< pos (match-beginning 2)))
  13556. (progn
  13557. (setq tags-l (- (match-end 2) (match-beginning 2)))
  13558. (goto-char (match-beginning 1))
  13559. (insert " ")
  13560. (delete-region (point) (1+ (match-beginning 2)))
  13561. (setq ncol (max (current-column)
  13562. (1+ col)
  13563. (if (> to-col 0)
  13564. to-col
  13565. (- (abs to-col) tags-l))))
  13566. (setq p (point))
  13567. (insert (make-string (- ncol (current-column)) ?\ ))
  13568. (setq ncol (current-column))
  13569. (when indent-tabs-mode (tabify p (point-at-eol)))
  13570. (org-move-to-column (min ncol col)))
  13571. (goto-char pos))))
  13572. (defun org-set-tags-command (&optional arg just-align)
  13573. "Call the set-tags command for the current entry."
  13574. (interactive "P")
  13575. (if (or (org-at-heading-p) (and arg (org-before-first-heading-p)))
  13576. (org-set-tags arg just-align)
  13577. (save-excursion
  13578. (unless (and (org-region-active-p)
  13579. org-loop-over-headlines-in-active-region)
  13580. (org-back-to-heading t))
  13581. (org-set-tags arg just-align))))
  13582. (defun org-set-tags-to (data)
  13583. "Set the tags of the current entry to DATA, replacing the current tags.
  13584. DATA may be a tags string like :aa:bb:cc:, or a list of tags.
  13585. If DATA is nil or the empty string, any tags will be removed."
  13586. (interactive "sTags: ")
  13587. (setq data
  13588. (cond
  13589. ((eq data nil) "")
  13590. ((equal data "") "")
  13591. ((stringp data)
  13592. (concat ":" (mapconcat 'identity (org-split-string data ":+") ":")
  13593. ":"))
  13594. ((listp data)
  13595. (concat ":" (mapconcat 'identity data ":") ":"))))
  13596. (when data
  13597. (save-excursion
  13598. (org-back-to-heading t)
  13599. (when (looking-at org-complex-heading-regexp)
  13600. (if (match-end 5)
  13601. (progn
  13602. (goto-char (match-beginning 5))
  13603. (insert data)
  13604. (delete-region (point) (point-at-eol))
  13605. (org-set-tags nil 'align))
  13606. (goto-char (point-at-eol))
  13607. (insert " " data)
  13608. (org-set-tags nil 'align)))
  13609. (beginning-of-line 1)
  13610. (if (looking-at ".*?\\([ \t]+\\)$")
  13611. (delete-region (match-beginning 1) (match-end 1))))))
  13612. (defun org-align-all-tags ()
  13613. "Align the tags i all headings."
  13614. (interactive)
  13615. (save-excursion
  13616. (or (ignore-errors (org-back-to-heading t))
  13617. (outline-next-heading))
  13618. (if (org-at-heading-p)
  13619. (org-set-tags t)
  13620. (message "No headings"))))
  13621. (defvar org-indent-indentation-per-level)
  13622. (defun org-set-tags (&optional arg just-align)
  13623. "Set the tags for the current headline.
  13624. With prefix ARG, realign all tags in headings in the current buffer.
  13625. When JUST-ALIGN is non-nil, only align tags."
  13626. (interactive "P")
  13627. (if (and (org-region-active-p) org-loop-over-headlines-in-active-region)
  13628. (let ((cl (if (eq org-loop-over-headlines-in-active-region 'start-level)
  13629. 'region-start-level
  13630. 'region))
  13631. org-loop-over-headlines-in-active-region)
  13632. (org-map-entries
  13633. ;; We don't use ARG and JUST-ALIGN here because these args
  13634. ;; are not useful when looping over headlines.
  13635. #'org-set-tags
  13636. org-loop-over-headlines-in-active-region
  13637. cl
  13638. '(when (outline-invisible-p) (org-end-of-subtree nil t))))
  13639. (let ((org-setting-tags t))
  13640. (if arg
  13641. (save-excursion
  13642. (goto-char (point-min))
  13643. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  13644. (while (re-search-forward org-outline-regexp-bol nil t)
  13645. (org-set-tags nil t)
  13646. (end-of-line)))
  13647. (message "All tags realigned to column %d" org-tags-column))
  13648. (let* ((current (org-get-tags-string))
  13649. (col (current-column))
  13650. (tags
  13651. (if just-align current
  13652. ;; Get a new set of tags from the user.
  13653. (save-excursion
  13654. (let* ((table
  13655. (setq
  13656. org-last-tags-completion-table
  13657. (append
  13658. org-tag-persistent-alist
  13659. (or org-tag-alist (org-get-buffer-tags))
  13660. (and
  13661. org-complete-tags-always-offer-all-agenda-tags
  13662. (org-global-tags-completion-table
  13663. (org-agenda-files))))))
  13664. (current-tags (org-split-string current ":"))
  13665. (inherited-tags
  13666. (nreverse (nthcdr (length current-tags)
  13667. (nreverse (org-get-tags-at))))))
  13668. (replace-regexp-in-string
  13669. "\\([-+&]+\\|,\\)"
  13670. ":"
  13671. (if (or (eq t org-use-fast-tag-selection)
  13672. (and org-use-fast-tag-selection
  13673. (delq nil (mapcar #'cdr table))))
  13674. (org-fast-tag-selection
  13675. current-tags inherited-tags table
  13676. (and org-fast-tag-selection-include-todo
  13677. org-todo-key-alist))
  13678. (let ((org-add-colon-after-tag-completion
  13679. (< 1 (length table))))
  13680. (org-trim
  13681. (completing-read
  13682. "Tags: "
  13683. #'org-tags-completion-function
  13684. nil nil current 'org-tags-history))))))))))
  13685. (when org-tags-sort-function
  13686. (setq tags
  13687. (mapconcat
  13688. #'identity
  13689. (sort (org-split-string tags (org-re "[^[:alnum:]_@#%]+"))
  13690. org-tags-sort-function)
  13691. ":")))
  13692. (if (not (org-string-nw-p tags)) (setq tags "")
  13693. (unless (string-match ":\\'" tags) (setq tags (concat tags ":")))
  13694. (unless (string-match "\\`:" tags) (setq tags (concat ":" tags))))
  13695. ;; Insert new tags at the correct column
  13696. (beginning-of-line)
  13697. (let ((level (if (looking-at org-outline-regexp)
  13698. (- (match-end 0) (point) 1)
  13699. 1)))
  13700. (cond
  13701. ((and (equal current "") (equal tags "")))
  13702. ((re-search-forward
  13703. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  13704. (line-end-position)
  13705. t)
  13706. (if (equal tags "") (replace-match "" t t)
  13707. (goto-char (match-beginning 0))
  13708. (let* ((c0 (current-column))
  13709. ;; Compute offset for the case of org-indent-mode
  13710. ;; active.
  13711. (di (if (org-bound-and-true-p org-indent-mode)
  13712. (* (1- org-indent-indentation-per-level)
  13713. (1- level))
  13714. 0))
  13715. (p0 (if (eq (char-before) ?*) (1+ (point)) (point)))
  13716. (tc (+ org-tags-column
  13717. (if (> org-tags-column 0) (- di) di)))
  13718. (c1 (max (1+ c0)
  13719. (if (> tc 0) tc
  13720. (- (- tc) (string-width tags)))))
  13721. (rpl (concat (make-string (max 0 (- c1 c0)) ?\s) tags)))
  13722. (replace-match rpl t t)
  13723. (when (and (not (featurep 'xemacs)) indent-tabs-mode)
  13724. (tabify p0 (point))))))
  13725. (t (error "Tags alignment failed"))))
  13726. (org-move-to-column col))
  13727. (unless just-align (run-hooks 'org-after-tags-change-hook))))))
  13728. (defun org-change-tag-in-region (beg end tag off)
  13729. "Add or remove TAG for each entry in the region.
  13730. This works in the agenda, and also in an org-mode buffer."
  13731. (interactive
  13732. (list (region-beginning) (region-end)
  13733. (let ((org-last-tags-completion-table
  13734. (if (derived-mode-p 'org-mode)
  13735. (org-uniquify
  13736. (delq nil (append (org-get-buffer-tags)
  13737. (org-global-tags-completion-table))))
  13738. (org-global-tags-completion-table))))
  13739. (org-icompleting-read
  13740. "Tag: " 'org-tags-completion-function nil nil nil
  13741. 'org-tags-history))
  13742. (progn
  13743. (message "[s]et or [r]emove? ")
  13744. (equal (read-char-exclusive) ?r))))
  13745. (if (fboundp 'deactivate-mark) (deactivate-mark))
  13746. (let ((agendap (equal major-mode 'org-agenda-mode))
  13747. l1 l2 m buf pos newhead (cnt 0))
  13748. (goto-char end)
  13749. (setq l2 (1- (org-current-line)))
  13750. (goto-char beg)
  13751. (setq l1 (org-current-line))
  13752. (loop for l from l1 to l2 do
  13753. (org-goto-line l)
  13754. (setq m (get-text-property (point) 'org-hd-marker))
  13755. (when (or (and (derived-mode-p 'org-mode) (org-at-heading-p))
  13756. (and agendap m))
  13757. (setq buf (if agendap (marker-buffer m) (current-buffer))
  13758. pos (if agendap m (point)))
  13759. (with-current-buffer buf
  13760. (save-excursion
  13761. (save-restriction
  13762. (goto-char pos)
  13763. (setq cnt (1+ cnt))
  13764. (org-toggle-tag tag (if off 'off 'on))
  13765. (setq newhead (org-get-heading)))))
  13766. (and agendap (org-agenda-change-all-lines newhead m))))
  13767. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  13768. (defun org-tags-completion-function (string predicate &optional flag)
  13769. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  13770. (confirm (lambda (x) (stringp (car x)))))
  13771. (if (string-match "^\\(.*[-+:&,|]\\)\\([^-+:&,|]*\\)$" string)
  13772. (setq s1 (match-string 1 string)
  13773. s2 (match-string 2 string))
  13774. (setq s1 "" s2 string))
  13775. (cond
  13776. ((eq flag nil)
  13777. ;; try completion
  13778. (setq rtn (try-completion s2 ctable confirm))
  13779. (if (stringp rtn)
  13780. (setq rtn
  13781. (concat s1 s2 (substring rtn (length s2))
  13782. (if (and org-add-colon-after-tag-completion
  13783. (assoc rtn ctable))
  13784. ":" ""))))
  13785. rtn)
  13786. ((eq flag t)
  13787. ;; all-completions
  13788. (all-completions s2 ctable confirm))
  13789. ((eq flag 'lambda)
  13790. ;; exact match?
  13791. (assoc s2 ctable)))))
  13792. (defun org-fast-tag-insert (kwd tags face &optional end)
  13793. "Insert KDW, and the TAGS, the latter with face FACE.
  13794. Also insert END."
  13795. (insert (format "%-12s" (concat kwd ":"))
  13796. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  13797. (or end "")))
  13798. (defun org-fast-tag-show-exit (flag)
  13799. (save-excursion
  13800. (org-goto-line 3)
  13801. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  13802. (replace-match ""))
  13803. (when flag
  13804. (end-of-line 1)
  13805. (org-move-to-column (- (window-width) 19) t)
  13806. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  13807. (defun org-set-current-tags-overlay (current prefix)
  13808. "Add an overlay to CURRENT tag with PREFIX."
  13809. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  13810. (if (featurep 'xemacs)
  13811. (org-overlay-display org-tags-overlay (concat prefix s)
  13812. 'secondary-selection)
  13813. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  13814. (org-overlay-display org-tags-overlay (concat prefix s)))))
  13815. (defvar org-last-tag-selection-key nil)
  13816. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  13817. "Fast tag selection with single keys.
  13818. CURRENT is the current list of tags in the headline, INHERITED is the
  13819. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  13820. possibly with grouping information. TODO-TABLE is a similar table with
  13821. TODO keywords, should these have keys assigned to them.
  13822. If the keys are nil, a-z are automatically assigned.
  13823. Returns the new tags string, or nil to not change the current settings."
  13824. (let* ((fulltable (append table todo-table))
  13825. (maxlen (apply 'max (mapcar
  13826. (lambda (x)
  13827. (if (stringp (car x)) (string-width (car x)) 0))
  13828. fulltable)))
  13829. (buf (current-buffer))
  13830. (expert (eq org-fast-tag-selection-single-key 'expert))
  13831. (buffer-tags nil)
  13832. (fwidth (+ maxlen 3 1 3))
  13833. (ncol (/ (- (window-width) 4) fwidth))
  13834. (i-face 'org-done)
  13835. (c-face 'org-todo)
  13836. tg cnt e c char c1 c2 ntable tbl rtn
  13837. ov-start ov-end ov-prefix
  13838. (exit-after-next org-fast-tag-selection-single-key)
  13839. (done-keywords org-done-keywords)
  13840. groups ingroup intaggroup)
  13841. (save-excursion
  13842. (beginning-of-line 1)
  13843. (if (looking-at
  13844. (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  13845. (setq ov-start (match-beginning 1)
  13846. ov-end (match-end 1)
  13847. ov-prefix "")
  13848. (setq ov-start (1- (point-at-eol))
  13849. ov-end (1+ ov-start))
  13850. (skip-chars-forward "^\n\r")
  13851. (setq ov-prefix
  13852. (concat
  13853. (buffer-substring (1- (point)) (point))
  13854. (if (> (current-column) org-tags-column)
  13855. " "
  13856. (make-string (- org-tags-column (current-column)) ?\ ))))))
  13857. (move-overlay org-tags-overlay ov-start ov-end)
  13858. (save-window-excursion
  13859. (if expert
  13860. (set-buffer (get-buffer-create " *Org tags*"))
  13861. (delete-other-windows)
  13862. (set-window-buffer (split-window-vertically) (get-buffer-create " *Org tags*"))
  13863. (org-switch-to-buffer-other-window " *Org tags*"))
  13864. (erase-buffer)
  13865. (org-set-local 'org-done-keywords done-keywords)
  13866. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  13867. (org-fast-tag-insert "Current" current c-face "\n\n")
  13868. (org-fast-tag-show-exit exit-after-next)
  13869. (org-set-current-tags-overlay current ov-prefix)
  13870. (setq tbl fulltable char ?a cnt 0)
  13871. (while (setq e (pop tbl))
  13872. (cond
  13873. ((eq (car e) :startgroup)
  13874. (push '() groups) (setq ingroup t)
  13875. (unless (zerop cnt)
  13876. (setq cnt 0)
  13877. (insert "\n"))
  13878. (insert (if (cdr e) (format "%s: " (cdr e)) "") "{ "))
  13879. ((eq (car e) :endgroup)
  13880. (setq ingroup nil cnt 0)
  13881. (insert "}" (if (cdr e) (format " (%s) " (cdr e)) "") "\n"))
  13882. ((eq (car e) :startgrouptag)
  13883. (setq intaggroup t)
  13884. (unless (zerop cnt)
  13885. (setq cnt 0)
  13886. (insert "\n"))
  13887. (insert "[ "))
  13888. ((eq (car e) :endgrouptag)
  13889. (setq intaggroup nil cnt 0)
  13890. (insert "]\n"))
  13891. ((equal e '(:newline))
  13892. (unless (zerop cnt)
  13893. (setq cnt 0)
  13894. (insert "\n")
  13895. (setq e (car tbl))
  13896. (while (equal (car tbl) '(:newline))
  13897. (insert "\n")
  13898. (setq tbl (cdr tbl)))))
  13899. ((equal e '(:grouptags)) (insert " : "))
  13900. (t
  13901. (setq tg (copy-sequence (car e)) c2 nil)
  13902. (if (cdr e)
  13903. (setq c (cdr e))
  13904. ;; automatically assign a character.
  13905. (setq c1 (string-to-char
  13906. (downcase (substring
  13907. tg (if (= (string-to-char tg) ?@) 1 0)))))
  13908. (if (or (rassoc c1 ntable) (rassoc c1 table))
  13909. (while (or (rassoc char ntable) (rassoc char table))
  13910. (setq char (1+ char)))
  13911. (setq c2 c1))
  13912. (setq c (or c2 char)))
  13913. (when ingroup (push tg (car groups)))
  13914. (setq tg (org-add-props tg nil 'face
  13915. (cond
  13916. ((not (assoc tg table))
  13917. (org-get-todo-face tg))
  13918. ((member tg current) c-face)
  13919. ((member tg inherited) i-face))))
  13920. (when (equal (caar tbl) :grouptags)
  13921. (org-add-props tg nil 'face 'org-tag-group))
  13922. (when (and (zerop cnt) (not ingroup) (not intaggroup)) (insert " "))
  13923. (insert "[" c "] " tg (make-string
  13924. (- fwidth 4 (length tg)) ?\ ))
  13925. (push (cons tg c) ntable)
  13926. (when (= (incf cnt) ncol)
  13927. (insert "\n")
  13928. (when (or ingroup intaggroup) (insert " "))
  13929. (setq cnt 0)))))
  13930. (setq ntable (nreverse ntable))
  13931. (insert "\n")
  13932. (goto-char (point-min))
  13933. (unless expert (org-fit-window-to-buffer))
  13934. (setq rtn
  13935. (catch 'exit
  13936. (while t
  13937. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free [!] %sgroups%s"
  13938. (if (not groups) "no " "")
  13939. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  13940. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  13941. (setq org-last-tag-selection-key c)
  13942. (cond
  13943. ((= c ?\r) (throw 'exit t))
  13944. ((= c ?!)
  13945. (setq groups (not groups))
  13946. (goto-char (point-min))
  13947. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  13948. ((= c ?\C-c)
  13949. (if (not expert)
  13950. (org-fast-tag-show-exit
  13951. (setq exit-after-next (not exit-after-next)))
  13952. (setq expert nil)
  13953. (delete-other-windows)
  13954. (set-window-buffer (split-window-vertically) " *Org tags*")
  13955. (org-switch-to-buffer-other-window " *Org tags*")
  13956. (org-fit-window-to-buffer)))
  13957. ((or (= c ?\C-g)
  13958. (and (= c ?q) (not (rassoc c ntable))))
  13959. (org-detach-overlay org-tags-overlay)
  13960. (setq quit-flag t))
  13961. ((= c ?\ )
  13962. (setq current nil)
  13963. (when exit-after-next (setq exit-after-next 'now)))
  13964. ((= c ?\t)
  13965. (condition-case nil
  13966. (setq tg (org-icompleting-read
  13967. "Tag: "
  13968. (or buffer-tags
  13969. (with-current-buffer buf
  13970. (setq buffer-tags
  13971. (org-get-buffer-tags))))))
  13972. (quit (setq tg "")))
  13973. (when (string-match "\\S-" tg)
  13974. (add-to-list 'buffer-tags (list tg))
  13975. (if (member tg current)
  13976. (setq current (delete tg current))
  13977. (push tg current)))
  13978. (when exit-after-next (setq exit-after-next 'now)))
  13979. ((setq e (rassoc c todo-table) tg (car e))
  13980. (with-current-buffer buf
  13981. (save-excursion (org-todo tg)))
  13982. (when exit-after-next (setq exit-after-next 'now)))
  13983. ((setq e (rassoc c ntable) tg (car e))
  13984. (if (member tg current)
  13985. (setq current (delete tg current))
  13986. (loop for g in groups do
  13987. (when (member tg g)
  13988. (dolist (x g) (setq current (delete x current)))))
  13989. (push tg current))
  13990. (when exit-after-next (setq exit-after-next 'now))))
  13991. ;; Create a sorted list
  13992. (setq current
  13993. (sort current
  13994. (lambda (a b)
  13995. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  13996. (when (eq exit-after-next 'now) (throw 'exit t))
  13997. (goto-char (point-min))
  13998. (beginning-of-line 2)
  13999. (delete-region (point) (point-at-eol))
  14000. (org-fast-tag-insert "Current" current c-face)
  14001. (org-set-current-tags-overlay current ov-prefix)
  14002. (while (re-search-forward
  14003. (org-re "\\[.\\] \\([[:alnum:]_@#%]+\\)") nil t)
  14004. (setq tg (match-string 1))
  14005. (add-text-properties
  14006. (match-beginning 1) (match-end 1)
  14007. (list 'face
  14008. (cond
  14009. ((member tg current) c-face)
  14010. ((member tg inherited) i-face)
  14011. (t (get-text-property (match-beginning 1) 'face))))))
  14012. (goto-char (point-min)))))
  14013. (org-detach-overlay org-tags-overlay)
  14014. (if rtn
  14015. (mapconcat 'identity current ":")
  14016. nil))))
  14017. (defun org-get-tags-string ()
  14018. "Get the TAGS string in the current headline."
  14019. (unless (org-at-heading-p t)
  14020. (user-error "Not on a heading"))
  14021. (save-excursion
  14022. (beginning-of-line 1)
  14023. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$"))
  14024. (org-match-string-no-properties 1)
  14025. "")))
  14026. (defun org-get-tags ()
  14027. "Get the list of tags specified in the current headline."
  14028. (org-split-string (org-get-tags-string) ":"))
  14029. (defun org-get-buffer-tags ()
  14030. "Get a table of all tags used in the buffer, for completion."
  14031. (org-with-wide-buffer
  14032. (goto-char (point-min))
  14033. (let ((tag-re (concat org-outline-regexp-bol
  14034. "\\(?:.*?[ \t]\\)?"
  14035. (org-re ":\\([[:alnum:]_@#%:]+\\):[ \t]*$")))
  14036. tags)
  14037. (while (re-search-forward tag-re nil t)
  14038. (dolist (tag (org-split-string (org-match-string-no-properties 1) ":"))
  14039. (push tag tags)))
  14040. (mapcar #'list (append org-file-tags (org-uniquify tags))))))
  14041. ;;;; The mapping API
  14042. (defun org-map-entries (func &optional match scope &rest skip)
  14043. "Call FUNC at each headline selected by MATCH in SCOPE.
  14044. FUNC is a function or a lisp form. The function will be called without
  14045. arguments, with the cursor positioned at the beginning of the headline.
  14046. The return values of all calls to the function will be collected and
  14047. returned as a list.
  14048. The call to FUNC will be wrapped into a save-excursion form, so FUNC
  14049. does not need to preserve point. After evaluation, the cursor will be
  14050. moved to the end of the line (presumably of the headline of the
  14051. processed entry) and search continues from there. Under some
  14052. circumstances, this may not produce the wanted results. For example,
  14053. if you have removed (e.g. archived) the current (sub)tree it could
  14054. mean that the next entry will be skipped entirely. In such cases, you
  14055. can specify the position from where search should continue by making
  14056. FUNC set the variable `org-map-continue-from' to the desired buffer
  14057. position.
  14058. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  14059. Only headlines that are matched by this query will be considered during
  14060. the iteration. When MATCH is nil or t, all headlines will be
  14061. visited by the iteration.
  14062. SCOPE determines the scope of this command. It can be any of:
  14063. nil The current buffer, respecting the restriction if any
  14064. tree The subtree started with the entry at point
  14065. region The entries within the active region, if any
  14066. region-start-level
  14067. The entries within the active region, but only those at
  14068. the same level than the first one.
  14069. file The current buffer, without restriction
  14070. file-with-archives
  14071. The current buffer, and any archives associated with it
  14072. agenda All agenda files
  14073. agenda-with-archives
  14074. All agenda files with any archive files associated with them
  14075. \(file1 file2 ...)
  14076. If this is a list, all files in the list will be scanned
  14077. The remaining args are treated as settings for the skipping facilities of
  14078. the scanner. The following items can be given here:
  14079. archive skip trees with the archive tag
  14080. comment skip trees with the COMMENT keyword
  14081. function or Emacs Lisp form:
  14082. will be used as value for `org-agenda-skip-function', so
  14083. whenever the function returns a position, FUNC will not be
  14084. called for that entry and search will continue from the
  14085. position returned
  14086. If your function needs to retrieve the tags including inherited tags
  14087. at the *current* entry, you can use the value of the variable
  14088. `org-scanner-tags' which will be much faster than getting the value
  14089. with `org-get-tags-at'. If your function gets properties with
  14090. `org-entry-properties' at the *current* entry, bind `org-trust-scanner-tags'
  14091. to t around the call to `org-entry-properties' to get the same speedup.
  14092. Note that if your function moves around to retrieve tags and properties at
  14093. a *different* entry, you cannot use these techniques."
  14094. (unless (and (or (eq scope 'region) (eq scope 'region-start-level))
  14095. (not (org-region-active-p)))
  14096. (let* ((org-agenda-archives-mode nil) ; just to make sure
  14097. (org-agenda-skip-archived-trees (memq 'archive skip))
  14098. (org-agenda-skip-comment-trees (memq 'comment skip))
  14099. (org-agenda-skip-function
  14100. (car (org-delete-all '(comment archive) skip)))
  14101. (org-tags-match-list-sublevels t)
  14102. (start-level (eq scope 'region-start-level))
  14103. matcher file res
  14104. org-todo-keywords-for-agenda
  14105. org-done-keywords-for-agenda
  14106. org-todo-keyword-alist-for-agenda
  14107. org-tag-alist-for-agenda
  14108. todo-only)
  14109. (cond
  14110. ((eq match t) (setq matcher t))
  14111. ((eq match nil) (setq matcher t))
  14112. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  14113. (save-excursion
  14114. (save-restriction
  14115. (cond ((eq scope 'tree)
  14116. (org-back-to-heading t)
  14117. (org-narrow-to-subtree)
  14118. (setq scope nil))
  14119. ((and (or (eq scope 'region) (eq scope 'region-start-level))
  14120. (org-region-active-p))
  14121. ;; If needed, set start-level to a string like "2"
  14122. (when start-level
  14123. (save-excursion
  14124. (goto-char (region-beginning))
  14125. (unless (org-at-heading-p) (outline-next-heading))
  14126. (setq start-level (org-current-level))))
  14127. (narrow-to-region (region-beginning)
  14128. (save-excursion
  14129. (goto-char (region-end))
  14130. (unless (and (bolp) (org-at-heading-p))
  14131. (outline-next-heading))
  14132. (point)))
  14133. (setq scope nil)))
  14134. (if (not scope)
  14135. (progn
  14136. (org-agenda-prepare-buffers
  14137. (and buffer-file-name (list buffer-file-name)))
  14138. (setq res (org-scan-tags func matcher todo-only start-level)))
  14139. ;; Get the right scope
  14140. (cond
  14141. ((and scope (listp scope) (symbolp (car scope)))
  14142. (setq scope (eval scope)))
  14143. ((eq scope 'agenda)
  14144. (setq scope (org-agenda-files t)))
  14145. ((eq scope 'agenda-with-archives)
  14146. (setq scope (org-agenda-files t))
  14147. (setq scope (org-add-archive-files scope)))
  14148. ((eq scope 'file)
  14149. (setq scope (and buffer-file-name (list buffer-file-name))))
  14150. ((eq scope 'file-with-archives)
  14151. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  14152. (org-agenda-prepare-buffers scope)
  14153. (dolist (file scope)
  14154. (with-current-buffer (org-find-base-buffer-visiting file)
  14155. (save-excursion
  14156. (save-restriction
  14157. (widen)
  14158. (goto-char (point-min))
  14159. (setq res (append res (org-scan-tags func matcher todo-only))))))))))
  14160. res)))
  14161. ;;; Properties API
  14162. (defconst org-special-properties
  14163. '("ALLTAGS" "BLOCKED" "CLOCKSUM" "CLOCKSUM_T" "CLOSED" "DEADLINE" "FILE"
  14164. "ITEM" "PRIORITY" "SCHEDULED" "TAGS" "TIMESTAMP" "TIMESTAMP_IA" "TODO")
  14165. "The special properties valid in Org mode.
  14166. These are properties that are not defined in the property drawer,
  14167. but in some other way.")
  14168. (defconst org-default-properties
  14169. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION" "CUSTOM_ID"
  14170. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  14171. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  14172. "EXPORT_OPTIONS" "EXPORT_TEXT" "EXPORT_FILE_NAME"
  14173. "EXPORT_TITLE" "EXPORT_AUTHOR" "EXPORT_DATE" "UNNUMBERED"
  14174. "ORDERED" "NOBLOCKING" "COOKIE_DATA" "LOG_INTO_DRAWER" "REPEAT_TO_STATE"
  14175. "CLOCK_MODELINE_TOTAL" "STYLE" "HTML_CONTAINER_CLASS")
  14176. "Some properties that are used by Org mode for various purposes.
  14177. Being in this list makes sure that they are offered for completion.")
  14178. (defun org--update-property-plist (key val props)
  14179. "Associate KEY to VAL in alist PROPS.
  14180. Modifications are made by side-effect. Return new alist."
  14181. (let* ((appending (string= (substring key -1) "+"))
  14182. (key (if appending (substring key 0 -1) key))
  14183. (old (assoc-string key props t)))
  14184. (if (not old) (cons (cons key val) props)
  14185. (setcdr old (if appending (concat (cdr old) " " val) val))
  14186. props)))
  14187. (defun org-get-property-block (&optional beg force)
  14188. "Return the (beg . end) range of the body of the property drawer.
  14189. BEG is the beginning of the current subtree, or of the part
  14190. before the first headline. If it is not given, it will be found.
  14191. If the drawer does not exist, create it if FORCE is non-nil, or
  14192. return nil."
  14193. (org-with-wide-buffer
  14194. (when beg (goto-char beg))
  14195. (unless (org-before-first-heading-p)
  14196. (let ((beg (cond (beg)
  14197. ((or (not (featurep 'org-inlinetask))
  14198. (org-inlinetask-in-task-p))
  14199. (org-back-to-heading t))
  14200. (t (org-with-limited-levels (org-back-to-heading t))))))
  14201. (forward-line)
  14202. (when (org-looking-at-p org-planning-line-re) (forward-line))
  14203. (cond ((looking-at org-property-drawer-re)
  14204. (forward-line)
  14205. (cons (point) (progn (goto-char (match-end 0))
  14206. (line-beginning-position))))
  14207. (force
  14208. (goto-char beg)
  14209. (org-insert-property-drawer)
  14210. (let ((pos (save-excursion (search-forward ":END:")
  14211. (line-beginning-position))))
  14212. (cons pos pos))))))))
  14213. (defun org-at-property-p ()
  14214. "Non-nil when point is inside a property drawer.
  14215. See `org-property-re' for match data, if applicable."
  14216. (save-excursion
  14217. (beginning-of-line)
  14218. (and (looking-at org-property-re)
  14219. (let ((property-drawer (save-match-data (org-get-property-block))))
  14220. (and property-drawer
  14221. (>= (point) (car property-drawer))
  14222. (< (point) (cdr property-drawer)))))))
  14223. (defun org-property-action ()
  14224. "Do an action on properties."
  14225. (interactive)
  14226. (unless (org-at-property-p) (user-error "Not at a property"))
  14227. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  14228. (let ((c (read-char-exclusive)))
  14229. (case c
  14230. (?s (call-interactively #'org-set-property))
  14231. (?d (call-interactively #'org-delete-property))
  14232. (?D (call-interactively #'org-delete-property-globally))
  14233. (?c (call-interactively #'org-compute-property-at-point))
  14234. (otherwise (user-error "No such property action %c" c)))))
  14235. (defun org-inc-effort ()
  14236. "Increment the value of the effort property in the current entry."
  14237. (interactive)
  14238. (org-set-effort nil t))
  14239. (defvar org-clock-effort) ; Defined in org-clock.el.
  14240. (defvar org-clock-current-task) ; Defined in org-clock.el.
  14241. (defun org-set-effort (&optional value increment)
  14242. "Set the effort property of the current entry.
  14243. With numerical prefix arg, use the nth allowed value, 0 stands for the
  14244. 10th allowed value.
  14245. When INCREMENT is non-nil, set the property to the next allowed value."
  14246. (interactive "P")
  14247. (if (equal value 0) (setq value 10))
  14248. (let* ((completion-ignore-case t)
  14249. (prop org-effort-property)
  14250. (cur (org-entry-get nil prop))
  14251. (allowed (org-property-get-allowed-values nil prop 'table))
  14252. (existing (mapcar 'list (org-property-values prop)))
  14253. (heading (nth 4 (org-heading-components)))
  14254. rpl
  14255. (val (cond
  14256. ((stringp value) value)
  14257. ((and allowed (integerp value))
  14258. (or (car (nth (1- value) allowed))
  14259. (car (org-last allowed))))
  14260. ((and allowed increment)
  14261. (or (caadr (member (list cur) allowed))
  14262. (user-error "Allowed effort values are not set")))
  14263. (allowed
  14264. (message "Select 1-9,0, [RET%s]: %s"
  14265. (if cur (concat "=" cur) "")
  14266. (mapconcat 'car allowed " "))
  14267. (setq rpl (read-char-exclusive))
  14268. (if (equal rpl ?\r)
  14269. cur
  14270. (setq rpl (- rpl ?0))
  14271. (if (equal rpl 0) (setq rpl 10))
  14272. (if (and (> rpl 0) (<= rpl (length allowed)))
  14273. (car (nth (1- rpl) allowed))
  14274. (org-completing-read "Effort: " allowed nil))))
  14275. (t
  14276. (let (org-completion-use-ido org-completion-use-iswitchb)
  14277. (org-completing-read
  14278. (concat "Effort " (if (and cur (string-match "\\S-" cur))
  14279. (concat "[" cur "]") "")
  14280. ": ")
  14281. existing nil nil "" nil cur))))))
  14282. (unless (equal (org-entry-get nil prop) val)
  14283. (org-entry-put nil prop val))
  14284. (org-refresh-property
  14285. '((effort . identity)
  14286. (effort-minutes . org-duration-string-to-minutes))
  14287. val)
  14288. (when (string= heading org-clock-current-task)
  14289. (setq org-clock-effort (get-text-property (point-at-bol) 'effort))
  14290. (org-clock-update-mode-line))
  14291. (message "%s is now %s" prop val)))
  14292. (defun org-entry-properties (&optional pom which)
  14293. "Get all properties of the current entry.
  14294. When POM is a buffer position, get all properties from the entry
  14295. there instead.
  14296. This includes the TODO keyword, the tags, time strings for
  14297. deadline, scheduled, and clocking, and any additional properties
  14298. defined in the entry.
  14299. If WHICH is nil or `all', get all properties. If WHICH is
  14300. `special' or `standard', only get that subclass. If WHICH is
  14301. a string, only get that property.
  14302. Return value is an alist. Keys are properties, as upcased
  14303. strings."
  14304. (org-with-point-at pom
  14305. (when (and (derived-mode-p 'org-mode)
  14306. (ignore-errors (org-back-to-heading t)))
  14307. (catch 'exit
  14308. (let* ((beg (point))
  14309. (specific (and (stringp which) (upcase which)))
  14310. (which (cond ((not specific) which)
  14311. ((member specific org-special-properties) 'special)
  14312. (t 'standard)))
  14313. props)
  14314. ;; Get the special properties, like TODO and TAGS.
  14315. (when (memq which '(nil all special))
  14316. (when (or (not specific) (string= specific "CLOCKSUM"))
  14317. (let ((clocksum (get-text-property (point) :org-clock-minutes)))
  14318. (when clocksum
  14319. (push (cons "CLOCKSUM"
  14320. (org-columns-number-to-string
  14321. (/ (float clocksum) 60.) 'add_times))
  14322. props)))
  14323. (when specific (throw 'exit props)))
  14324. (when (or (not specific) (string= specific "CLOCKSUM_T"))
  14325. (let ((clocksumt (get-text-property (point)
  14326. :org-clock-minutes-today)))
  14327. (when clocksumt
  14328. (push (cons "CLOCKSUM_T"
  14329. (org-columns-number-to-string
  14330. (/ (float clocksumt) 60.) 'add_times))
  14331. props)))
  14332. (when specific (throw 'exit props)))
  14333. (when (or (not specific) (string= specific "ITEM"))
  14334. (when (looking-at org-complex-heading-regexp)
  14335. (push (cons "ITEM"
  14336. (concat
  14337. (org-match-string-no-properties 1)
  14338. (let ((title (org-match-string-no-properties 4)))
  14339. (when (org-string-nw-p title)
  14340. (concat " " (org-remove-tabs title))))))
  14341. props))
  14342. (when specific (throw 'exit props)))
  14343. (when (or (not specific) (string= specific "TODO"))
  14344. (let ((case-fold-search nil))
  14345. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  14346. (push (cons "TODO" (org-match-string-no-properties 2)) props)))
  14347. (when specific (throw 'exit props)))
  14348. (when (or (not specific) (string= specific "PRIORITY"))
  14349. (push (cons "PRIORITY"
  14350. (if (looking-at org-priority-regexp)
  14351. (org-match-string-no-properties 2)
  14352. (char-to-string org-default-priority)))
  14353. props)
  14354. (when specific (throw 'exit props)))
  14355. (when (or (not specific) (string= specific "FILE"))
  14356. (push (cons "FILE" (buffer-file-name (buffer-base-buffer)))
  14357. props)
  14358. (when specific (throw 'exit props)))
  14359. (when (or (not specific) (string= specific "TAGS"))
  14360. (let ((value (org-string-nw-p (org-get-tags-string))))
  14361. (when value (push (cons "TAGS" value) props)))
  14362. (when specific (throw 'exit props)))
  14363. (when (or (not specific) (string= specific "ALLTAGS"))
  14364. (let ((value (org-get-tags-at)))
  14365. (when value
  14366. (push (cons "ALLTAGS"
  14367. (format ":%s:" (mapconcat #'identity value ":")))
  14368. props)))
  14369. (when specific (throw 'exit props)))
  14370. (when (or (not specific) (string= specific "BLOCKED"))
  14371. (push (cons "BLOCKED" (if (org-entry-blocked-p) "t" "")) props)
  14372. (when specific (throw 'exit props)))
  14373. (when (or (not specific)
  14374. (member specific '("CLOSED" "DEADLINE" "SCHEDULED")))
  14375. (forward-line)
  14376. (when (org-looking-at-p org-planning-line-re)
  14377. (end-of-line)
  14378. (let ((bol (line-beginning-position))
  14379. ;; Backward compatibility: time keywords used to
  14380. ;; be configurable (before 8.3). Make sure we
  14381. ;; get the correct keyword.
  14382. (key-assoc `(("CLOSED" . ,org-closed-string)
  14383. ("DEADLINE" . ,org-deadline-string)
  14384. ("SCHEDULED" . ,org-scheduled-string))))
  14385. (dolist (pair (if specific (list (assoc specific key-assoc))
  14386. key-assoc))
  14387. (save-excursion
  14388. (when (search-backward (cdr pair) bol t)
  14389. (goto-char (match-end 0))
  14390. (skip-chars-forward " \t")
  14391. (and (looking-at org-ts-regexp-both)
  14392. (push (cons (car pair)
  14393. (org-match-string-no-properties 0))
  14394. props)))))))
  14395. (when specific (throw 'exit props)))
  14396. (when (or (not specific)
  14397. (member specific '("TIMESTAMP" "TIMESTAMP_IA")))
  14398. (let ((find-ts
  14399. (lambda (end ts)
  14400. (let ((regexp (cond
  14401. ((string= specific "TIMESTAMP")
  14402. org-ts-regexp)
  14403. ((string= specific "TIMESTAMP_IA")
  14404. org-ts-regexp-inactive)
  14405. ((assoc "TIMESTAMP_IA" ts)
  14406. org-ts-regexp)
  14407. ((assoc "TIMESTAMP" ts)
  14408. org-ts-regexp-inactive)
  14409. (t org-ts-regexp-both))))
  14410. (catch 'next
  14411. (while (re-search-forward regexp end t)
  14412. (backward-char)
  14413. (let ((object (org-element-context)))
  14414. ;; Accept to match timestamps in node
  14415. ;; properties, too.
  14416. (when (memq (org-element-type object)
  14417. '(node-property timestamp))
  14418. (let ((type
  14419. (org-element-property :type object)))
  14420. (cond
  14421. ((and (memq type '(active active-range))
  14422. (not (equal specific "TIMESTAMP_IA")))
  14423. (unless (assoc "TIMESTAMP" ts)
  14424. (push (cons "TIMESTAMP"
  14425. (org-element-property
  14426. :raw-value object))
  14427. ts)
  14428. (when specific (throw 'exit ts))))
  14429. ((and (memq type '(inactive inactive-range))
  14430. (not (string= specific "TIMESTAMP")))
  14431. (unless (assoc "TIMESTAMP_IA" ts)
  14432. (push (cons "TIMESTAMP_IA"
  14433. (org-element-property
  14434. :raw-value object))
  14435. ts)
  14436. (when specific (throw 'exit ts))))))
  14437. ;; Both timestamp types are found,
  14438. ;; move to next part.
  14439. (when (= (length ts) 2) (throw 'next ts)))))
  14440. ts)))))
  14441. (goto-char beg)
  14442. ;; First look for timestamps within headline.
  14443. (let ((ts (funcall find-ts (line-end-position) nil)))
  14444. (if (= (length ts) 2) (setq props (nconc ts props))
  14445. (forward-line)
  14446. ;; Then find timestamps in the section, skipping
  14447. ;; planning line.
  14448. (when (org-looking-at-p org-planning-line-re)
  14449. (forward-line))
  14450. (let ((end (save-excursion (outline-next-heading))))
  14451. (setq props (nconc (funcall find-ts end ts) props))))))))
  14452. ;; Get the standard properties, like :PROP:.
  14453. (when (memq which '(nil all standard))
  14454. ;; If we are looking after a specific property, delegate
  14455. ;; to `org-entry-get', which is faster. However, make an
  14456. ;; exception for "CATEGORY", since it can be also set
  14457. ;; through keywords (i.e. #+CATEGORY).
  14458. (if (and specific (not (equal specific "CATEGORY")))
  14459. (let ((value (org-entry-get beg specific nil t)))
  14460. (throw 'exit (and value (list (cons specific value)))))
  14461. (let ((range (org-get-property-block beg)))
  14462. (when range
  14463. (let ((end (cdr range)) seen-base)
  14464. (goto-char (car range))
  14465. ;; Unlike to `org--update-property-plist', we
  14466. ;; handle the case where base values is found
  14467. ;; after its extension. We also forbid standard
  14468. ;; properties to be named as special properties.
  14469. (while (re-search-forward org-property-re end t)
  14470. (let* ((key (upcase (org-match-string-no-properties 2)))
  14471. (extendp (org-string-match-p "\\+\\'" key))
  14472. (key-base (if extendp (substring key 0 -1) key))
  14473. (value (org-match-string-no-properties 3)))
  14474. (cond
  14475. ((member-ignore-case key-base org-special-properties))
  14476. (extendp
  14477. (setq props
  14478. (org--update-property-plist key value props)))
  14479. ((member key seen-base))
  14480. (t (push key seen-base)
  14481. (let ((p (assoc-string key props t)))
  14482. (if p (setcdr p (concat value " " (cdr p)))
  14483. (push (cons key value) props))))))))))))
  14484. (unless (assoc "CATEGORY" props)
  14485. (push (cons "CATEGORY" (org-get-category beg)) props)
  14486. (when (string= specific "CATEGORY") (throw 'exit props)))
  14487. ;; Return value.
  14488. props)))))
  14489. (defun org-property--local-values (property literal-nil)
  14490. "Return value for PROPERTY in current entry.
  14491. Value is a list whose care is the base value for PROPERTY and cdr
  14492. a list of accumulated values. Return nil if neither is found in
  14493. the entry. Also return nil when PROPERTY is set to \"nil\",
  14494. unless LITERAL-NIL is non-nil."
  14495. (let ((range (org-get-property-block)))
  14496. (when range
  14497. (goto-char (car range))
  14498. (let* ((case-fold-search t)
  14499. (end (cdr range))
  14500. (value
  14501. ;; Base value.
  14502. (save-excursion
  14503. (let ((v (and (re-search-forward
  14504. (org-re-property property nil t) end t)
  14505. (org-match-string-no-properties 3))))
  14506. (list (if literal-nil v (org-not-nil v)))))))
  14507. ;; Find additional values.
  14508. (let* ((property+ (org-re-property (concat property "+") nil t)))
  14509. (while (re-search-forward property+ end t)
  14510. (push (org-match-string-no-properties 3) value)))
  14511. ;; Return final values.
  14512. (and (not (equal value '(nil))) (nreverse value))))))
  14513. (defun org-entry-get (pom property &optional inherit literal-nil)
  14514. "Get value of PROPERTY for entry or content at point-or-marker POM.
  14515. If INHERIT is non-nil and the entry does not have the property,
  14516. then also check higher levels of the hierarchy. If INHERIT is
  14517. the symbol `selective', use inheritance only if the setting in
  14518. `org-use-property-inheritance' selects PROPERTY for inheritance.
  14519. If the property is present but empty, the return value is the
  14520. empty string. If the property is not present at all, nil is
  14521. returned. In any other case, return the value as a string.
  14522. Search is case-insensitive.
  14523. If LITERAL-NIL is set, return the string value \"nil\" as
  14524. a string, do not interpret it as the list atom nil. This is used
  14525. for inheritance when a \"nil\" value can supersede a non-nil
  14526. value higher up the hierarchy."
  14527. (org-with-point-at pom
  14528. (cond
  14529. ((member-ignore-case property (cons "CATEGORY" org-special-properties))
  14530. ;; We need a special property. Use `org-entry-properties' to
  14531. ;; retrieve it, but specify the wanted property.
  14532. (cdr (assoc-string property (org-entry-properties nil property))))
  14533. ((and inherit
  14534. (or (not (eq inherit 'selective)) (org-property-inherit-p property)))
  14535. (org-entry-get-with-inheritance property literal-nil))
  14536. (t
  14537. (let* ((local (org-property--local-values property literal-nil))
  14538. (value (and local (mapconcat #'identity (delq nil local) " "))))
  14539. (if literal-nil value (org-not-nil value)))))))
  14540. (defun org-property-or-variable-value (var &optional inherit)
  14541. "Check if there is a property fixing the value of VAR.
  14542. If yes, return this value. If not, return the current value of the variable."
  14543. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  14544. (if (and prop (stringp prop) (string-match "\\S-" prop))
  14545. (read prop)
  14546. (symbol-value var))))
  14547. (defun org-entry-delete (pom property)
  14548. "Delete PROPERTY from entry at point-or-marker POM.
  14549. Accumulated properties, i.e. PROPERTY+, are also removed. Return
  14550. non-nil when a property was removed."
  14551. (unless (member property org-special-properties)
  14552. (org-with-point-at pom
  14553. (let ((range (org-get-property-block)))
  14554. (when range
  14555. (let* ((begin (car range))
  14556. (origin (cdr range))
  14557. (end (copy-marker origin))
  14558. (re (org-re-property
  14559. (concat (regexp-quote property) "\\+?") t t)))
  14560. (goto-char begin)
  14561. (while (re-search-forward re end t)
  14562. (delete-region (match-beginning 0) (line-beginning-position 2)))
  14563. ;; If drawer is empty, remove it altogether.
  14564. (when (= begin end)
  14565. (delete-region (line-beginning-position 0)
  14566. (line-beginning-position 2)))
  14567. ;; Return non-nil if some property was removed.
  14568. (prog1 (/= end origin) (set-marker end nil))))))))
  14569. ;; Multi-values properties are properties that contain multiple values
  14570. ;; These values are assumed to be single words, separated by whitespace.
  14571. (defun org-entry-add-to-multivalued-property (pom property value)
  14572. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  14573. (let* ((old (org-entry-get pom property))
  14574. (values (and old (org-split-string old "[ \t]"))))
  14575. (setq value (org-entry-protect-space value))
  14576. (unless (member value values)
  14577. (setq values (append values (list value)))
  14578. (org-entry-put pom property
  14579. (mapconcat 'identity values " ")))))
  14580. (defun org-entry-remove-from-multivalued-property (pom property value)
  14581. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  14582. (let* ((old (org-entry-get pom property))
  14583. (values (and old (org-split-string old "[ \t]"))))
  14584. (setq value (org-entry-protect-space value))
  14585. (when (member value values)
  14586. (setq values (delete value values))
  14587. (org-entry-put pom property
  14588. (mapconcat 'identity values " ")))))
  14589. (defun org-entry-member-in-multivalued-property (pom property value)
  14590. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  14591. (let* ((old (org-entry-get pom property))
  14592. (values (and old (org-split-string old "[ \t]"))))
  14593. (setq value (org-entry-protect-space value))
  14594. (member value values)))
  14595. (defun org-entry-get-multivalued-property (pom property)
  14596. "Return a list of values in a multivalued property."
  14597. (let* ((value (org-entry-get pom property))
  14598. (values (and value (org-split-string value "[ \t]"))))
  14599. (mapcar 'org-entry-restore-space values)))
  14600. (defun org-entry-put-multivalued-property (pom property &rest values)
  14601. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  14602. VALUES should be a list of strings. Spaces will be protected."
  14603. (org-entry-put pom property
  14604. (mapconcat 'org-entry-protect-space values " "))
  14605. (let* ((value (org-entry-get pom property))
  14606. (values (and value (org-split-string value "[ \t]"))))
  14607. (mapcar 'org-entry-restore-space values)))
  14608. (defun org-entry-protect-space (s)
  14609. "Protect spaces and newline in string S."
  14610. (while (string-match " " s)
  14611. (setq s (replace-match "%20" t t s)))
  14612. (while (string-match "\n" s)
  14613. (setq s (replace-match "%0A" t t s)))
  14614. s)
  14615. (defun org-entry-restore-space (s)
  14616. "Restore spaces and newline in string S."
  14617. (while (string-match "%20" s)
  14618. (setq s (replace-match " " t t s)))
  14619. (while (string-match "%0A" s)
  14620. (setq s (replace-match "\n" t t s)))
  14621. s)
  14622. (defvar org-entry-property-inherited-from (make-marker)
  14623. "Marker pointing to the entry from where a property was inherited.
  14624. Each call to `org-entry-get-with-inheritance' will set this marker to the
  14625. location of the entry where the inheritance search matched. If there was
  14626. no match, the marker will point nowhere.
  14627. Note that also `org-entry-get' calls this function, if the INHERIT flag
  14628. is set.")
  14629. (defun org-entry-get-with-inheritance (property &optional literal-nil)
  14630. "Get PROPERTY of entry or content at point, search higher levels if needed.
  14631. The search will stop at the first ancestor which has the property defined.
  14632. If the value found is \"nil\", return nil to show that the property
  14633. should be considered as undefined (this is the meaning of nil here).
  14634. However, if LITERAL-NIL is set, return the string value \"nil\" instead."
  14635. (move-marker org-entry-property-inherited-from nil)
  14636. (org-with-wide-buffer
  14637. (let (value)
  14638. (catch 'exit
  14639. (while t
  14640. (let ((v (org-property--local-values property literal-nil)))
  14641. (when v
  14642. (setq value
  14643. (concat (mapconcat #'identity (delq nil v) " ")
  14644. (and value " ")
  14645. value)))
  14646. (cond
  14647. ((car v)
  14648. (org-back-to-heading t)
  14649. (move-marker org-entry-property-inherited-from (point))
  14650. (throw 'exit nil))
  14651. ((org-up-heading-safe))
  14652. (t
  14653. (let ((global
  14654. (cdr (or (assoc-string property org-file-properties t)
  14655. (assoc-string property org-global-properties t)
  14656. (assoc-string property org-global-properties-fixed t)))))
  14657. (cond ((not global))
  14658. (value (setq value (concat global " " value)))
  14659. (t (setq value global))))
  14660. (throw 'exit nil))))))
  14661. (if literal-nil value (org-not-nil value)))))
  14662. (defvar org-property-changed-functions nil
  14663. "Hook called when the value of a property has changed.
  14664. Each hook function should accept two arguments, the name of the property
  14665. and the new value.")
  14666. (defun org-entry-put (pom property value)
  14667. "Set PROPERTY to VALUE for entry at point-or-marker POM.
  14668. If the value is nil, it is converted to the empty string. If
  14669. it is not a string, an error is raised.
  14670. PROPERTY can be any regular property (see
  14671. `org-special-properties'). It can also be \"TODO\",
  14672. \"PRIORITY\", \"SCHEDULED\" and \"DEADLINE\".
  14673. For the last two properties, VALUE may have any of the special
  14674. values \"earlier\" and \"later\". The function then increases or
  14675. decreases scheduled or deadline date by one day."
  14676. (cond ((null value) (setq value ""))
  14677. ((not (stringp value)) (error "Properties values should be strings")))
  14678. (org-with-point-at pom
  14679. (if (or (not (featurep 'org-inlinetask)) (org-inlinetask-in-task-p))
  14680. (org-back-to-heading t)
  14681. (org-with-limited-levels (org-back-to-heading t)))
  14682. (let ((beg (point)))
  14683. (cond
  14684. ((equal property "TODO")
  14685. (cond ((not (org-string-nw-p value)) (setq value 'none))
  14686. ((not (member value org-todo-keywords-1))
  14687. (user-error "\"%s\" is not a valid TODO state" value)))
  14688. (org-todo value)
  14689. (org-set-tags nil 'align))
  14690. ((equal property "PRIORITY")
  14691. (org-priority (if (org-string-nw-p value) (string-to-char value) ?\s))
  14692. (org-set-tags nil 'align))
  14693. ((equal property "SCHEDULED")
  14694. (forward-line)
  14695. (if (and (org-looking-at-p org-planning-line-re)
  14696. (re-search-forward
  14697. org-scheduled-time-regexp (line-end-position) t))
  14698. (cond ((string= value "earlier") (org-timestamp-change -1 'day))
  14699. ((string= value "later") (org-timestamp-change 1 'day))
  14700. ((string= value "") (org-schedule '(4)))
  14701. (t (org-schedule nil value)))
  14702. (if (member value '("earlier" "later" ""))
  14703. (call-interactively #'org-schedule)
  14704. (org-schedule nil value))))
  14705. ((equal property "DEADLINE")
  14706. (forward-line)
  14707. (if (and (org-looking-at-p org-planning-line-re)
  14708. (re-search-forward
  14709. org-deadline-time-regexp (line-end-position) t))
  14710. (cond ((string= value "earlier") (org-timestamp-change -1 'day))
  14711. ((string= value "later") (org-timestamp-change 1 'day))
  14712. ((string= value "") (org-deadline '(4)))
  14713. (t (org-deadline nil value)))
  14714. (if (member value '("earlier" "later" ""))
  14715. (call-interactively #'org-deadline)
  14716. (org-deadline nil value))))
  14717. ((member property org-special-properties)
  14718. (error "The %s property cannot be set with `org-entry-put'" property))
  14719. (t
  14720. (let* ((range (org-get-property-block beg 'force))
  14721. (end (cdr range))
  14722. (case-fold-search t))
  14723. (goto-char (car range))
  14724. (if (re-search-forward (org-re-property property nil t) end t)
  14725. (progn (delete-region (match-beginning 0) (match-end 0))
  14726. (goto-char (match-beginning 0)))
  14727. (goto-char end)
  14728. (insert "\n")
  14729. (backward-char))
  14730. (insert ":" property ":")
  14731. (when value (insert " " value))
  14732. (org-indent-line)))))
  14733. (run-hook-with-args 'org-property-changed-functions property value)))
  14734. (defun org-buffer-property-keys (&optional specials defaults columns)
  14735. "Get all property keys in the current buffer.
  14736. When SPECIALS is non-nil, also list the special properties that
  14737. reflect things like tags and TODO state.
  14738. When DEFAULTS is non-nil, also include properties that has
  14739. special meaning internally: ARCHIVE, CATEGORY, SUMMARY,
  14740. DESCRIPTION, LOCATION, and LOGGING and others.
  14741. When COLUMNS in non-nil, also include property names given in
  14742. COLUMN formats in the current buffer."
  14743. (let ((case-fold-search t)
  14744. (props (append
  14745. (and specials org-special-properties)
  14746. (and defaults (cons org-effort-property org-default-properties))
  14747. nil)))
  14748. (org-with-wide-buffer
  14749. (goto-char (point-min))
  14750. (while (re-search-forward org-property-start-re nil t)
  14751. (let ((range (org-get-property-block)))
  14752. (catch 'skip
  14753. (unless range
  14754. (when (and (not (org-before-first-heading-p))
  14755. (y-or-n-p (format "Malformed drawer at %d, repair?"
  14756. (line-beginning-position))))
  14757. (org-get-property-block nil t))
  14758. (throw 'skip nil))
  14759. (goto-char (car range))
  14760. (let ((begin (car range))
  14761. (end (cdr range)))
  14762. ;; Make sure that found property block is not located
  14763. ;; before current point, as it would generate an infloop.
  14764. ;; It can happen, for example, in the following
  14765. ;; situation:
  14766. ;;
  14767. ;; * Headline
  14768. ;; :PROPERTIES:
  14769. ;; ...
  14770. ;; :END:
  14771. ;; *************** Inlinetask
  14772. ;; #+BEGIN_EXAMPLE
  14773. ;; :PROPERTIES:
  14774. ;; #+END_EXAMPLE
  14775. ;;
  14776. (if (< begin (point)) (throw 'skip nil) (goto-char begin))
  14777. (while (< (point) end)
  14778. (let ((p (progn (looking-at org-property-re)
  14779. (org-match-string-no-properties 2))))
  14780. ;; Only add true property name, not extension symbol.
  14781. (add-to-list 'props
  14782. (if (not (org-string-match-p "\\+\\'" p)) p
  14783. (substring p 0 -1))))
  14784. (forward-line))))
  14785. (outline-next-heading)))
  14786. (when columns
  14787. (goto-char (point-min))
  14788. (while (re-search-forward "^[ \t]*\\(?:#\\+\\|:\\)COLUMNS:" nil t)
  14789. (let ((element (org-element-at-point)))
  14790. (when (memq (org-element-type element) '(keyword node-property))
  14791. (let ((value (org-element-property :value element))
  14792. (start 0))
  14793. (while (string-match "%[0-9]*\\(\\S-+\\)" value start)
  14794. (setq start (match-end 0))
  14795. (let ((p (org-match-string-no-properties 1 value)))
  14796. (unless (member-ignore-case p org-special-properties)
  14797. (add-to-list 'props p))))))))))
  14798. (sort props (lambda (a b) (string< (upcase a) (upcase b))))))
  14799. (defun org-property-values (key)
  14800. "List all non-nil values of property KEY in current buffer."
  14801. (org-with-wide-buffer
  14802. (goto-char (point-min))
  14803. (let ((case-fold-search t)
  14804. (re (org-re-property key))
  14805. values)
  14806. (while (re-search-forward re nil t)
  14807. (add-to-list 'values (org-entry-get (point) key)))
  14808. values)))
  14809. (defun org-insert-property-drawer ()
  14810. "Insert a property drawer into the current entry."
  14811. (org-with-wide-buffer
  14812. (if (or (not (featurep 'org-inlinetask)) (org-inlinetask-in-task-p))
  14813. (org-back-to-heading t)
  14814. (org-with-limited-levels (org-back-to-heading t)))
  14815. (forward-line)
  14816. (when (org-looking-at-p org-planning-line-re) (forward-line))
  14817. (unless (org-looking-at-p org-property-drawer-re)
  14818. (let ((inhibit-read-only t))
  14819. (unless (bolp) (insert "\n"))
  14820. (let ((begin (point)))
  14821. (insert ":PROPERTIES:\n:END:\n")
  14822. (org-indent-region begin (point)))))))
  14823. (defun org-insert-drawer (&optional arg drawer)
  14824. "Insert a drawer at point.
  14825. When optional argument ARG is non-nil, insert a property drawer.
  14826. Optional argument DRAWER, when non-nil, is a string representing
  14827. drawer's name. Otherwise, the user is prompted for a name.
  14828. If a region is active, insert the drawer around that region
  14829. instead.
  14830. Point is left between drawer's boundaries."
  14831. (interactive "P")
  14832. (let* ((drawer (if arg "PROPERTIES"
  14833. (or drawer (read-from-minibuffer "Drawer: ")))))
  14834. (cond
  14835. ;; With C-u, fall back on `org-insert-property-drawer'
  14836. (arg (org-insert-property-drawer))
  14837. ;; Check validity of suggested drawer's name.
  14838. ((not (org-string-match-p org-drawer-regexp (format ":%s:" drawer)))
  14839. (user-error "Invalid drawer name"))
  14840. ;; With an active region, insert a drawer at point.
  14841. ((not (org-region-active-p))
  14842. (progn
  14843. (unless (bolp) (insert "\n"))
  14844. (insert (format ":%s:\n\n:END:\n" drawer))
  14845. (forward-line -2)))
  14846. ;; Otherwise, insert the drawer at point
  14847. (t
  14848. (let ((rbeg (region-beginning))
  14849. (rend (copy-marker (region-end))))
  14850. (unwind-protect
  14851. (progn
  14852. (goto-char rbeg)
  14853. (beginning-of-line)
  14854. (when (save-excursion
  14855. (re-search-forward org-outline-regexp-bol rend t))
  14856. (user-error "Drawers cannot contain headlines"))
  14857. ;; Position point at the beginning of the first
  14858. ;; non-blank line in region. Insert drawer's opening
  14859. ;; there, then indent it.
  14860. (org-skip-whitespace)
  14861. (beginning-of-line)
  14862. (insert ":" drawer ":\n")
  14863. (forward-line -1)
  14864. (indent-for-tab-command)
  14865. ;; Move point to the beginning of the first blank line
  14866. ;; after the last non-blank line in region. Insert
  14867. ;; drawer's closing, then indent it.
  14868. (goto-char rend)
  14869. (skip-chars-backward " \r\t\n")
  14870. (insert "\n:END:")
  14871. (deactivate-mark t)
  14872. (indent-for-tab-command)
  14873. (unless (eolp) (insert "\n")))
  14874. ;; Clear marker, whatever the outcome of insertion is.
  14875. (set-marker rend nil)))))))
  14876. (defvar org-property-set-functions-alist nil
  14877. "Property set function alist.
  14878. Each entry should have the following format:
  14879. (PROPERTY . READ-FUNCTION)
  14880. The read function will be called with the same argument as
  14881. `org-completing-read'.")
  14882. (defun org-set-property-function (property)
  14883. "Get the function that should be used to set PROPERTY.
  14884. This is computed according to `org-property-set-functions-alist'."
  14885. (or (cdr (assoc property org-property-set-functions-alist))
  14886. 'org-completing-read))
  14887. (defun org-read-property-value (property)
  14888. "Read PROPERTY value from user."
  14889. (let* ((completion-ignore-case t)
  14890. (allowed (org-property-get-allowed-values nil property 'table))
  14891. (cur (org-entry-get nil property))
  14892. (prompt (concat property " value"
  14893. (if (and cur (string-match "\\S-" cur))
  14894. (concat " [" cur "]") "") ": "))
  14895. (set-function (org-set-property-function property))
  14896. (val (if allowed
  14897. (funcall set-function prompt allowed nil
  14898. (not (get-text-property 0 'org-unrestricted
  14899. (caar allowed))))
  14900. (let (org-completion-use-ido org-completion-use-iswitchb)
  14901. (funcall set-function prompt
  14902. (mapcar 'list (org-property-values property))
  14903. nil nil "" nil cur)))))
  14904. (org-trim val)))
  14905. (defvar org-last-set-property nil)
  14906. (defvar org-last-set-property-value nil)
  14907. (defun org-read-property-name ()
  14908. "Read a property name."
  14909. (let ((completion-ignore-case t)
  14910. (default-prop (or (and (org-at-property-p)
  14911. (org-match-string-no-properties 2))
  14912. org-last-set-property)))
  14913. (org-completing-read
  14914. (concat "Property"
  14915. (if default-prop (concat " [" default-prop "]") "")
  14916. ": ")
  14917. (mapcar #'list (org-buffer-property-keys nil t t))
  14918. nil nil nil nil default-prop)))
  14919. (defun org-set-property-and-value (use-last)
  14920. "Allow to set [PROPERTY]: [value] direction from prompt.
  14921. When use-default, don't even ask, just use the last
  14922. \"[PROPERTY]: [value]\" string from the history."
  14923. (interactive "P")
  14924. (let* ((completion-ignore-case t)
  14925. (pv (or (and use-last org-last-set-property-value)
  14926. (org-completing-read
  14927. "Enter a \"[Property]: [value]\" pair: "
  14928. nil nil nil nil nil
  14929. org-last-set-property-value)))
  14930. prop val)
  14931. (when (string-match "^[ \t]*\\([^:]+\\):[ \t]*\\(.*\\)[ \t]*$" pv)
  14932. (setq prop (match-string 1 pv)
  14933. val (match-string 2 pv))
  14934. (org-set-property prop val))))
  14935. (defun org-set-property (property value)
  14936. "In the current entry, set PROPERTY to VALUE.
  14937. When called interactively, this will prompt for a property name, offering
  14938. completion on existing and default properties. And then it will prompt
  14939. for a value, offering completion either on allowed values (via an inherited
  14940. xxx_ALL property) or on existing values in other instances of this property
  14941. in the current file."
  14942. (interactive (list nil nil))
  14943. (let* ((property (or property (org-read-property-name)))
  14944. (value (or value (org-read-property-value property)))
  14945. (fn (cdr (assoc property org-properties-postprocess-alist))))
  14946. (setq org-last-set-property property)
  14947. (setq org-last-set-property-value (concat property ": " value))
  14948. ;; Possibly postprocess the inserted value:
  14949. (when fn (setq value (funcall fn value)))
  14950. (unless (equal (org-entry-get nil property) value)
  14951. (org-entry-put nil property value))))
  14952. (defun org-find-property (property &optional value)
  14953. "Find first entry in buffer that sets PROPERTY.
  14954. When optional argument VALUE is non-nil, only consider an entry
  14955. if it contains PROPERTY set to this value. If PROPERTY should be
  14956. explicitly set to nil, use string \"nil\" for VALUE.
  14957. Return position where the entry begins, or nil if there is no
  14958. such entry. If narrowing is in effect, only search the visible
  14959. part of the buffer."
  14960. (save-excursion
  14961. (goto-char (point-min))
  14962. (let ((case-fold-search t)
  14963. (re (org-re-property property nil (not value) value)))
  14964. (catch 'exit
  14965. (while (re-search-forward re nil t)
  14966. (when (if value (org-at-property-p)
  14967. (org-entry-get (point) property nil t))
  14968. (throw 'exit (progn (org-back-to-heading t) (point)))))))))
  14969. (defun org-delete-property (property)
  14970. "In the current entry, delete PROPERTY."
  14971. (interactive
  14972. (let* ((completion-ignore-case t)
  14973. (cat (org-entry-get (point) "CATEGORY"))
  14974. (props0 (org-entry-properties nil 'standard))
  14975. (props (if cat props0
  14976. (delete `("CATEGORY" . ,(org-get-category)) props0)))
  14977. (prop (if (< 1 (length props))
  14978. (org-icompleting-read "Property: " props nil t)
  14979. (caar props))))
  14980. (list prop)))
  14981. (if (not property)
  14982. (message "No property to delete in this entry")
  14983. (org-entry-delete nil property)
  14984. (message "Property \"%s\" deleted" property)))
  14985. (defun org-delete-property-globally (property)
  14986. "Remove PROPERTY globally, from all entries.
  14987. This function ignores narrowing, if any."
  14988. (interactive
  14989. (let* ((completion-ignore-case t)
  14990. (prop (org-icompleting-read
  14991. "Globally remove property: "
  14992. (mapcar #'list (org-buffer-property-keys)))))
  14993. (list prop)))
  14994. (org-with-wide-buffer
  14995. (goto-char (point-min))
  14996. (let ((count 0)
  14997. (re (org-re-property (concat (regexp-quote property) "\\+?") t t)))
  14998. (while (re-search-forward re nil t)
  14999. (when (org-entry-delete (point) property) (incf count)))
  15000. (message "Property \"%s\" removed from %d entries" property count))))
  15001. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  15002. (defun org-compute-property-at-point ()
  15003. "Compute the property at point.
  15004. This looks for an enclosing column format, extracts the operator and
  15005. then applies it to the property in the column format's scope."
  15006. (interactive)
  15007. (unless (org-at-property-p)
  15008. (user-error "Not at a property"))
  15009. (let ((prop (org-match-string-no-properties 2)))
  15010. (org-columns-get-format-and-top-level)
  15011. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  15012. (user-error "No operator defined for property %s" prop))
  15013. (org-columns-compute prop)))
  15014. (defvar org-property-allowed-value-functions nil
  15015. "Hook for functions supplying allowed values for a specific property.
  15016. The functions must take a single argument, the name of the property, and
  15017. return a flat list of allowed values. If \":ETC\" is one of
  15018. the values, this means that these values are intended as defaults for
  15019. completion, but that other values should be allowed too.
  15020. The functions must return nil if they are not responsible for this
  15021. property.")
  15022. (defun org-property-get-allowed-values (pom property &optional table)
  15023. "Get allowed values for the property PROPERTY.
  15024. When TABLE is non-nil, return an alist that can directly be used for
  15025. completion."
  15026. (let (vals)
  15027. (cond
  15028. ((equal property "TODO")
  15029. (setq vals (org-with-point-at pom
  15030. (append org-todo-keywords-1 '("")))))
  15031. ((equal property "PRIORITY")
  15032. (let ((n org-lowest-priority))
  15033. (while (>= n org-highest-priority)
  15034. (push (char-to-string n) vals)
  15035. (setq n (1- n)))))
  15036. ((equal property "CATEGORY"))
  15037. ((member property org-special-properties))
  15038. ((setq vals (run-hook-with-args-until-success
  15039. 'org-property-allowed-value-functions property)))
  15040. (t
  15041. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  15042. (when (and vals (string-match "\\S-" vals))
  15043. (setq vals (car (read-from-string (concat "(" vals ")"))))
  15044. (setq vals (mapcar (lambda (x)
  15045. (cond ((stringp x) x)
  15046. ((numberp x) (number-to-string x))
  15047. ((symbolp x) (symbol-name x))
  15048. (t "???")))
  15049. vals)))))
  15050. (when (member ":ETC" vals)
  15051. (setq vals (remove ":ETC" vals))
  15052. (org-add-props (car vals) '(org-unrestricted t)))
  15053. (if table (mapcar 'list vals) vals)))
  15054. (defun org-property-previous-allowed-value (&optional previous)
  15055. "Switch to the next allowed value for this property."
  15056. (interactive)
  15057. (org-property-next-allowed-value t))
  15058. (defun org-property-next-allowed-value (&optional previous)
  15059. "Switch to the next allowed value for this property."
  15060. (interactive)
  15061. (unless (org-at-property-p)
  15062. (user-error "Not at a property"))
  15063. (let* ((prop (car (save-match-data (org-split-string (match-string 1) ":"))))
  15064. (key (match-string 2))
  15065. (value (match-string 3))
  15066. (allowed (or (org-property-get-allowed-values (point) key)
  15067. (and (member value '("[ ]" "[-]" "[X]"))
  15068. '("[ ]" "[X]"))))
  15069. (heading (save-match-data (nth 4 (org-heading-components))))
  15070. nval)
  15071. (unless allowed
  15072. (user-error "Allowed values for this property have not been defined"))
  15073. (if previous (setq allowed (reverse allowed)))
  15074. (if (member value allowed)
  15075. (setq nval (car (cdr (member value allowed)))))
  15076. (setq nval (or nval (car allowed)))
  15077. (if (equal nval value)
  15078. (user-error "Only one allowed value for this property"))
  15079. (org-at-property-p)
  15080. (replace-match (concat " :" key ": " nval) t t)
  15081. (org-indent-line)
  15082. (beginning-of-line 1)
  15083. (skip-chars-forward " \t")
  15084. (when (equal prop org-effort-property)
  15085. (org-refresh-property
  15086. '((effort . identity)
  15087. (effort-minutes . org-duration-string-to-minutes))
  15088. nval)
  15089. (when (string= org-clock-current-task heading)
  15090. (setq org-clock-effort nval)
  15091. (org-clock-update-mode-line)))
  15092. (run-hook-with-args 'org-property-changed-functions key nval)))
  15093. (defun org-find-olp (path &optional this-buffer)
  15094. "Return a marker pointing to the entry at outline path OLP.
  15095. If anything goes wrong, throw an error.
  15096. You can wrap this call to catch the error like this:
  15097. (condition-case msg
  15098. (org-mobile-locate-entry (match-string 4))
  15099. (error (nth 1 msg)))
  15100. The return value will then be either a string with the error message,
  15101. or a marker if everything is OK.
  15102. If THIS-BUFFER is set, the outline path does not contain a file,
  15103. only headings."
  15104. (let* ((file (if this-buffer buffer-file-name (pop path)))
  15105. (buffer (if this-buffer (current-buffer) (find-file-noselect file)))
  15106. (level 1)
  15107. (lmin 1)
  15108. (lmax 1)
  15109. limit re end found pos heading cnt flevel)
  15110. (unless buffer (error "File not found :%s" file))
  15111. (with-current-buffer buffer
  15112. (save-excursion
  15113. (save-restriction
  15114. (widen)
  15115. (setq limit (point-max))
  15116. (goto-char (point-min))
  15117. (dolist (heading path)
  15118. (setq re (format org-complex-heading-regexp-format
  15119. (regexp-quote heading)))
  15120. (setq cnt 0 pos (point))
  15121. (while (re-search-forward re end t)
  15122. (setq level (- (match-end 1) (match-beginning 1)))
  15123. (if (and (>= level lmin) (<= level lmax))
  15124. (setq found (match-beginning 0) flevel level cnt (1+ cnt))))
  15125. (when (= cnt 0) (error "Heading not found on level %d: %s"
  15126. lmax heading))
  15127. (when (> cnt 1) (error "Heading not unique on level %d: %s"
  15128. lmax heading))
  15129. (goto-char found)
  15130. (setq lmin (1+ flevel) lmax (+ lmin (if org-odd-levels-only 1 0)))
  15131. (setq end (save-excursion (org-end-of-subtree t t))))
  15132. (when (org-at-heading-p)
  15133. (point-marker)))))))
  15134. (defun org-find-exact-headline-in-buffer (heading &optional buffer pos-only)
  15135. "Find node HEADING in BUFFER.
  15136. Return a marker to the heading if it was found, or nil if not.
  15137. If POS-ONLY is set, return just the position instead of a marker.
  15138. The heading text must match exact, but it may have a TODO keyword,
  15139. a priority cookie and tags in the standard locations."
  15140. (with-current-buffer (or buffer (current-buffer))
  15141. (save-excursion
  15142. (save-restriction
  15143. (widen)
  15144. (goto-char (point-min))
  15145. (let (case-fold-search)
  15146. (if (re-search-forward
  15147. (format org-complex-heading-regexp-format
  15148. (regexp-quote heading)) nil t)
  15149. (if pos-only
  15150. (match-beginning 0)
  15151. (move-marker (make-marker) (match-beginning 0)))))))))
  15152. (defun org-find-exact-heading-in-directory (heading &optional dir)
  15153. "Find Org node headline HEADING in all .org files in directory DIR.
  15154. When the target headline is found, return a marker to this location."
  15155. (let ((files (directory-files (or dir default-directory)
  15156. t "\\`[^.#].*\\.org\\'"))
  15157. visiting m buffer)
  15158. (catch 'found
  15159. (dolist (file files)
  15160. (message "trying %s" file)
  15161. (setq visiting (org-find-base-buffer-visiting file))
  15162. (setq buffer (or visiting (find-file-noselect file)))
  15163. (setq m (org-find-exact-headline-in-buffer
  15164. heading buffer))
  15165. (when (and (not m) (not visiting)) (kill-buffer buffer))
  15166. (and m (throw 'found m))))))
  15167. (defun org-find-entry-with-id (ident)
  15168. "Locate the entry that contains the ID property with exact value IDENT.
  15169. IDENT can be a string, a symbol or a number, this function will search for
  15170. the string representation of it.
  15171. Return the position where this entry starts, or nil if there is no such entry."
  15172. (interactive "sID: ")
  15173. (let ((id (cond
  15174. ((stringp ident) ident)
  15175. ((symbolp ident) (symbol-name ident))
  15176. ((numberp ident) (number-to-string ident))
  15177. (t (error "IDENT %s must be a string, symbol or number" ident)))))
  15178. (org-with-wide-buffer (org-find-property "ID" id))))
  15179. ;;;; Timestamps
  15180. (defvar org-last-changed-timestamp nil)
  15181. (defvar org-last-inserted-timestamp nil
  15182. "The last time stamp inserted with `org-insert-time-stamp'.")
  15183. (defvar org-ts-what) ; dynamically scoped parameter
  15184. (defun org-time-stamp (arg &optional inactive)
  15185. "Prompt for a date/time and insert a time stamp.
  15186. If the user specifies a time like HH:MM or if this command is
  15187. called with at least one prefix argument, the time stamp contains
  15188. the date and the time. Otherwise, only the date is included.
  15189. All parts of a date not specified by the user are filled in from
  15190. the timestamp at point, if any, or the current date/time
  15191. otherwise.
  15192. If there is already a timestamp at the cursor, it is replaced.
  15193. With two universal prefix arguments, insert an active timestamp
  15194. with the current time without prompting the user.
  15195. When called from lisp, the timestamp is inactive if INACTIVE is
  15196. non-nil."
  15197. (interactive "P")
  15198. (let* ((ts (cond
  15199. ((org-at-date-range-p t)
  15200. (match-string (if (< (point) (- (match-beginning 2) 2)) 1 2)))
  15201. ((org-at-timestamp-p t) (match-string 0))))
  15202. ;; Default time is either the timestamp at point or today.
  15203. ;; When entering a range, only the range start is considered.
  15204. (default-time (if (not ts) (current-time)
  15205. (apply #'encode-time (org-parse-time-string ts))))
  15206. (default-input (and ts (org-get-compact-tod ts)))
  15207. (repeater (and ts
  15208. (string-match "\\([.+-]+[0-9]+[hdwmy] ?\\)+" ts)
  15209. (match-string 0 ts)))
  15210. org-time-was-given
  15211. org-end-time-was-given
  15212. (time
  15213. (and (if (equal arg '(16)) (current-time)
  15214. ;; Preserve `this-command' and `last-command'.
  15215. (let ((this-command this-command)
  15216. (last-command last-command))
  15217. (org-read-date
  15218. arg 'totime nil nil default-time default-input
  15219. inactive))))))
  15220. (cond
  15221. ((and ts
  15222. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  15223. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  15224. (insert "--")
  15225. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  15226. (ts
  15227. ;; Make sure we're on a timestamp. When in the middle of a date
  15228. ;; range, move arbitrarily to range end.
  15229. (unless (org-at-timestamp-p t)
  15230. (skip-chars-forward "-")
  15231. (org-at-timestamp-p t))
  15232. (replace-match "")
  15233. (setq org-last-changed-timestamp
  15234. (org-insert-time-stamp
  15235. time (or org-time-was-given arg)
  15236. inactive nil nil (list org-end-time-was-given)))
  15237. (when repeater
  15238. (backward-char)
  15239. (insert " " repeater)
  15240. (setq org-last-changed-timestamp
  15241. (concat (substring org-last-inserted-timestamp 0 -1)
  15242. " " repeater ">")))
  15243. (message "Timestamp updated"))
  15244. ((equal arg '(16)) (org-insert-time-stamp time t inactive))
  15245. (t (org-insert-time-stamp
  15246. time (or org-time-was-given arg) inactive nil nil
  15247. (list org-end-time-was-given))))))
  15248. ;; FIXME: can we use this for something else, like computing time differences?
  15249. (defun org-get-compact-tod (s)
  15250. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  15251. (let* ((t1 (match-string 1 s))
  15252. (h1 (string-to-number (match-string 2 s)))
  15253. (m1 (string-to-number (match-string 3 s)))
  15254. (t2 (and (match-end 4) (match-string 5 s)))
  15255. (h2 (and t2 (string-to-number (match-string 6 s))))
  15256. (m2 (and t2 (string-to-number (match-string 7 s))))
  15257. dh dm)
  15258. (if (not t2)
  15259. t1
  15260. (setq dh (- h2 h1) dm (- m2 m1))
  15261. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  15262. (concat t1 "+" (number-to-string dh)
  15263. (and (/= 0 dm) (format ":%02d" dm)))))))
  15264. (defun org-time-stamp-inactive (&optional arg)
  15265. "Insert an inactive time stamp.
  15266. An inactive time stamp is enclosed in square brackets instead of angle
  15267. brackets. It is inactive in the sense that it does not trigger agenda entries,
  15268. does not link to the calendar and cannot be changed with the S-cursor keys.
  15269. So these are more for recording a certain time/date."
  15270. (interactive "P")
  15271. (org-time-stamp arg 'inactive))
  15272. (defvar org-date-ovl (make-overlay 1 1))
  15273. (overlay-put org-date-ovl 'face 'org-date-selected)
  15274. (org-detach-overlay org-date-ovl)
  15275. (defvar org-ans1) ; dynamically scoped parameter
  15276. (defvar org-ans2) ; dynamically scoped parameter
  15277. (defvar org-plain-time-of-day-regexp) ; defined below
  15278. (defvar org-overriding-default-time nil) ; dynamically scoped
  15279. (defvar org-read-date-overlay nil)
  15280. (defvar org-dcst nil) ; dynamically scoped
  15281. (defvar org-read-date-history nil)
  15282. (defvar org-read-date-final-answer nil)
  15283. (defvar org-read-date-analyze-futurep nil)
  15284. (defvar org-read-date-analyze-forced-year nil)
  15285. (defvar org-read-date-inactive)
  15286. (defvar org-read-date-minibuffer-local-map
  15287. (let* ((map (make-sparse-keymap)))
  15288. (set-keymap-parent map minibuffer-local-map)
  15289. (org-defkey map (kbd ".")
  15290. (lambda () (interactive)
  15291. ;; Are we at the beginning of the prompt?
  15292. (if (looking-back "^[^:]+: ")
  15293. (org-eval-in-calendar '(calendar-goto-today))
  15294. (insert "."))))
  15295. (org-defkey map (kbd "C-.")
  15296. (lambda () (interactive)
  15297. (org-eval-in-calendar '(calendar-goto-today))))
  15298. (org-defkey map [(meta shift left)]
  15299. (lambda () (interactive)
  15300. (org-eval-in-calendar '(calendar-backward-month 1))))
  15301. (org-defkey map [(meta shift right)]
  15302. (lambda () (interactive)
  15303. (org-eval-in-calendar '(calendar-forward-month 1))))
  15304. (org-defkey map [(meta shift up)]
  15305. (lambda () (interactive)
  15306. (org-eval-in-calendar '(calendar-backward-year 1))))
  15307. (org-defkey map [(meta shift down)]
  15308. (lambda () (interactive)
  15309. (org-eval-in-calendar '(calendar-forward-year 1))))
  15310. (org-defkey map [?\e (shift left)]
  15311. (lambda () (interactive)
  15312. (org-eval-in-calendar '(calendar-backward-month 1))))
  15313. (org-defkey map [?\e (shift right)]
  15314. (lambda () (interactive)
  15315. (org-eval-in-calendar '(calendar-forward-month 1))))
  15316. (org-defkey map [?\e (shift up)]
  15317. (lambda () (interactive)
  15318. (org-eval-in-calendar '(calendar-backward-year 1))))
  15319. (org-defkey map [?\e (shift down)]
  15320. (lambda () (interactive)
  15321. (org-eval-in-calendar '(calendar-forward-year 1))))
  15322. (org-defkey map [(shift up)]
  15323. (lambda () (interactive)
  15324. (org-eval-in-calendar '(calendar-backward-week 1))))
  15325. (org-defkey map [(shift down)]
  15326. (lambda () (interactive)
  15327. (org-eval-in-calendar '(calendar-forward-week 1))))
  15328. (org-defkey map [(shift left)]
  15329. (lambda () (interactive)
  15330. (org-eval-in-calendar '(calendar-backward-day 1))))
  15331. (org-defkey map [(shift right)]
  15332. (lambda () (interactive)
  15333. (org-eval-in-calendar '(calendar-forward-day 1))))
  15334. (org-defkey map "!"
  15335. (lambda () (interactive)
  15336. (org-eval-in-calendar '(diary-view-entries))
  15337. (message "")))
  15338. (org-defkey map ">"
  15339. (lambda () (interactive)
  15340. (org-eval-in-calendar '(calendar-scroll-left 1))))
  15341. (org-defkey map "<"
  15342. (lambda () (interactive)
  15343. (org-eval-in-calendar '(calendar-scroll-right 1))))
  15344. (org-defkey map "\C-v"
  15345. (lambda () (interactive)
  15346. (org-eval-in-calendar
  15347. '(calendar-scroll-left-three-months 1))))
  15348. (org-defkey map "\M-v"
  15349. (lambda () (interactive)
  15350. (org-eval-in-calendar
  15351. '(calendar-scroll-right-three-months 1))))
  15352. map)
  15353. "Keymap for minibuffer commands when using `org-read-date'.")
  15354. (defvar org-def)
  15355. (defvar org-defdecode)
  15356. (defvar org-with-time)
  15357. (defun org-read-date (&optional org-with-time to-time from-string prompt
  15358. default-time default-input inactive)
  15359. "Read a date, possibly a time, and make things smooth for the user.
  15360. The prompt will suggest to enter an ISO date, but you can also enter anything
  15361. which will at least partially be understood by `parse-time-string'.
  15362. Unrecognized parts of the date will default to the current day, month, year,
  15363. hour and minute. If this command is called to replace a timestamp at point,
  15364. or to enter the second timestamp of a range, the default time is taken
  15365. from the existing stamp. Furthermore, the command prefers the future,
  15366. so if you are giving a date where the year is not given, and the day-month
  15367. combination is already past in the current year, it will assume you
  15368. mean next year. For details, see the manual. A few examples:
  15369. 3-2-5 --> 2003-02-05
  15370. feb 15 --> currentyear-02-15
  15371. 2/15 --> currentyear-02-15
  15372. sep 12 9 --> 2009-09-12
  15373. 12:45 --> today 12:45
  15374. 22 sept 0:34 --> currentyear-09-22 0:34
  15375. 12 --> currentyear-currentmonth-12
  15376. Fri --> nearest Friday after today
  15377. -Tue --> last Tuesday
  15378. etc.
  15379. Furthermore you can specify a relative date by giving, as the *first* thing
  15380. in the input: a plus/minus sign, a number and a letter [hdwmy] to indicate
  15381. change in days weeks, months, years.
  15382. With a single plus or minus, the date is relative to today. With a double
  15383. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  15384. +4d --> four days from today
  15385. +4 --> same as above
  15386. +2w --> two weeks from today
  15387. ++5 --> five days from default date
  15388. The function understands only English month and weekday abbreviations.
  15389. While prompting, a calendar is popped up - you can also select the
  15390. date with the mouse (button 1). The calendar shows a period of three
  15391. months. To scroll it to other months, use the keys `>' and `<'.
  15392. If you don't like the calendar, turn it off with
  15393. \(setq org-read-date-popup-calendar nil)
  15394. With optional argument TO-TIME, the date will immediately be converted
  15395. to an internal time.
  15396. With an optional argument ORG-WITH-TIME, the prompt will suggest to
  15397. also insert a time. Note that when ORG-WITH-TIME is not set, you can
  15398. still enter a time, and this function will inform the calling routine
  15399. about this change. The calling routine may then choose to change the
  15400. format used to insert the time stamp into the buffer to include the time.
  15401. With optional argument FROM-STRING, read from this string instead from
  15402. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  15403. the time/date that is used for everything that is not specified by the
  15404. user."
  15405. (require 'parse-time)
  15406. (let* ((org-time-stamp-rounding-minutes
  15407. (if (equal org-with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  15408. (org-dcst org-display-custom-times)
  15409. (ct (org-current-time))
  15410. (org-def (or org-overriding-default-time default-time ct))
  15411. (org-defdecode (decode-time org-def))
  15412. (dummy (progn
  15413. (when (< (nth 2 org-defdecode) org-extend-today-until)
  15414. (setcar (nthcdr 2 org-defdecode) -1)
  15415. (setcar (nthcdr 1 org-defdecode) 59)
  15416. (setq org-def (apply 'encode-time org-defdecode)
  15417. org-defdecode (decode-time org-def)))))
  15418. (cur-frame (selected-frame))
  15419. (mouse-autoselect-window nil) ; Don't let the mouse jump
  15420. (calendar-frame-setup nil)
  15421. (calendar-setup (when (eq calendar-setup 'calendar-only) 'calendar-only))
  15422. (calendar-move-hook nil)
  15423. (calendar-view-diary-initially-flag nil)
  15424. (calendar-view-holidays-initially-flag nil)
  15425. (timestr (format-time-string
  15426. (if org-with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") org-def))
  15427. (prompt (concat (if prompt (concat prompt " ") "")
  15428. (format "Date+time [%s]: " timestr)))
  15429. ans (org-ans0 "") org-ans1 org-ans2 final cal-frame)
  15430. (cond
  15431. (from-string (setq ans from-string))
  15432. (org-read-date-popup-calendar
  15433. (save-excursion
  15434. (save-window-excursion
  15435. (calendar)
  15436. (when (eq calendar-setup 'calendar-only)
  15437. (setq cal-frame
  15438. (window-frame (get-buffer-window "*Calendar*" 'visible)))
  15439. (select-frame cal-frame))
  15440. (org-eval-in-calendar '(setq cursor-type nil) t)
  15441. (unwind-protect
  15442. (progn
  15443. (calendar-forward-day (- (time-to-days org-def)
  15444. (calendar-absolute-from-gregorian
  15445. (calendar-current-date))))
  15446. (org-eval-in-calendar nil t)
  15447. (let* ((old-map (current-local-map))
  15448. (map (copy-keymap calendar-mode-map))
  15449. (minibuffer-local-map
  15450. (copy-keymap org-read-date-minibuffer-local-map)))
  15451. (org-defkey map (kbd "RET") 'org-calendar-select)
  15452. (org-defkey map [mouse-1] 'org-calendar-select-mouse)
  15453. (org-defkey map [mouse-2] 'org-calendar-select-mouse)
  15454. (unwind-protect
  15455. (progn
  15456. (use-local-map map)
  15457. (setq org-read-date-inactive inactive)
  15458. (add-hook 'post-command-hook 'org-read-date-display)
  15459. (setq org-ans0 (read-string prompt default-input
  15460. 'org-read-date-history nil))
  15461. ;; org-ans0: from prompt
  15462. ;; org-ans1: from mouse click
  15463. ;; org-ans2: from calendar motion
  15464. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  15465. (remove-hook 'post-command-hook 'org-read-date-display)
  15466. (use-local-map old-map)
  15467. (when org-read-date-overlay
  15468. (delete-overlay org-read-date-overlay)
  15469. (setq org-read-date-overlay nil)))))
  15470. (bury-buffer "*Calendar*")
  15471. (when cal-frame
  15472. (delete-frame cal-frame)
  15473. (select-frame-set-input-focus cur-frame))))))
  15474. (t ; Naked prompt only
  15475. (unwind-protect
  15476. (setq ans (read-string prompt default-input
  15477. 'org-read-date-history timestr))
  15478. (when org-read-date-overlay
  15479. (delete-overlay org-read-date-overlay)
  15480. (setq org-read-date-overlay nil)))))
  15481. (setq final (org-read-date-analyze ans org-def org-defdecode))
  15482. (when org-read-date-analyze-forced-year
  15483. (message "Year was forced into %s"
  15484. (if org-read-date-force-compatible-dates
  15485. "compatible range (1970-2037)"
  15486. "range representable on this machine"))
  15487. (ding))
  15488. ;; One round trip to get rid of 34th of August and stuff like that....
  15489. (setq final (decode-time (apply 'encode-time final)))
  15490. (setq org-read-date-final-answer ans)
  15491. (if to-time
  15492. (apply 'encode-time final)
  15493. (if (and (boundp 'org-time-was-given) org-time-was-given)
  15494. (format "%04d-%02d-%02d %02d:%02d"
  15495. (nth 5 final) (nth 4 final) (nth 3 final)
  15496. (nth 2 final) (nth 1 final))
  15497. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  15498. (defun org-read-date-display ()
  15499. "Display the current date prompt interpretation in the minibuffer."
  15500. (when org-read-date-display-live
  15501. (when org-read-date-overlay
  15502. (delete-overlay org-read-date-overlay))
  15503. (when (minibufferp (current-buffer))
  15504. (save-excursion
  15505. (end-of-line 1)
  15506. (while (not (equal (buffer-substring
  15507. (max (point-min) (- (point) 4)) (point))
  15508. " "))
  15509. (insert " ")))
  15510. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  15511. " " (or org-ans1 org-ans2)))
  15512. (org-end-time-was-given nil)
  15513. (f (org-read-date-analyze ans org-def org-defdecode))
  15514. (fmts (if org-dcst
  15515. org-time-stamp-custom-formats
  15516. org-time-stamp-formats))
  15517. (fmt (if (or org-with-time
  15518. (and (boundp 'org-time-was-given) org-time-was-given))
  15519. (cdr fmts)
  15520. (car fmts)))
  15521. (txt (format-time-string fmt (apply 'encode-time f)))
  15522. (txt (if org-read-date-inactive (concat "[" (substring txt 1 -1) "]") txt))
  15523. (txt (concat "=> " txt)))
  15524. (when (and org-end-time-was-given
  15525. (string-match org-plain-time-of-day-regexp txt))
  15526. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  15527. org-end-time-was-given
  15528. (substring txt (match-end 0)))))
  15529. (when org-read-date-analyze-futurep
  15530. (setq txt (concat txt " (=>F)")))
  15531. (setq org-read-date-overlay
  15532. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  15533. (org-overlay-display org-read-date-overlay txt 'secondary-selection)))))
  15534. (defun org-read-date-analyze (ans org-def org-defdecode)
  15535. "Analyze the combined answer of the date prompt."
  15536. ;; FIXME: cleanup and comment
  15537. ;; Pass `current-time' result to `decode-time' (instead of calling
  15538. ;; without arguments) so that only `current-time' has to be
  15539. ;; overriden in tests.
  15540. (let ((nowdecode (decode-time (current-time)))
  15541. delta deltan deltaw deltadef year month day
  15542. hour minute second wday pm h2 m2 tl wday1
  15543. iso-year iso-weekday iso-week iso-year iso-date futurep kill-year)
  15544. (setq org-read-date-analyze-futurep nil
  15545. org-read-date-analyze-forced-year nil)
  15546. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  15547. (setq ans "+0"))
  15548. (when (setq delta (org-read-date-get-relative ans (current-time) org-def))
  15549. (setq ans (replace-match "" t t ans)
  15550. deltan (car delta)
  15551. deltaw (nth 1 delta)
  15552. deltadef (nth 2 delta)))
  15553. ;; Check if there is an iso week date in there. If yes, store the
  15554. ;; info and postpone interpreting it until the rest of the parsing
  15555. ;; is done.
  15556. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  15557. (setq iso-year (if (match-end 1)
  15558. (org-small-year-to-year
  15559. (string-to-number (match-string 1 ans))))
  15560. iso-weekday (if (match-end 3)
  15561. (string-to-number (match-string 3 ans)))
  15562. iso-week (string-to-number (match-string 2 ans)))
  15563. (setq ans (replace-match "" t t ans)))
  15564. ;; Help matching ISO dates with single digit month or day, like 2006-8-11.
  15565. (when (string-match
  15566. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  15567. (setq year (if (match-end 2)
  15568. (string-to-number (match-string 2 ans))
  15569. (progn (setq kill-year t)
  15570. (string-to-number (format-time-string "%Y"))))
  15571. month (string-to-number (match-string 3 ans))
  15572. day (string-to-number (match-string 4 ans)))
  15573. (if (< year 100) (setq year (+ 2000 year)))
  15574. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  15575. t nil ans)))
  15576. ;; Help matching dotted european dates
  15577. (when (string-match
  15578. "^ *\\(3[01]\\|0?[1-9]\\|[12][0-9]\\)\\. ?\\(0?[1-9]\\|1[012]\\)\\.\\( ?[1-9][0-9]\\{3\\}\\)?" ans)
  15579. (setq year (if (match-end 3) (string-to-number (match-string 3 ans))
  15580. (setq kill-year t)
  15581. (string-to-number (format-time-string "%Y")))
  15582. day (string-to-number (match-string 1 ans))
  15583. month (string-to-number (match-string 2 ans))
  15584. ans (replace-match (format "%04d-%02d-%02d" year month day)
  15585. t nil ans)))
  15586. ;; Help matching american dates, like 5/30 or 5/30/7
  15587. (when (string-match
  15588. "^ *\\(0?[1-9]\\|1[012]\\)/\\(0?[1-9]\\|[12][0-9]\\|3[01]\\)\\(/\\([0-9]+\\)\\)?\\([^/0-9]\\|$\\)" ans)
  15589. (setq year (if (match-end 4)
  15590. (string-to-number (match-string 4 ans))
  15591. (progn (setq kill-year t)
  15592. (string-to-number (format-time-string "%Y"))))
  15593. month (string-to-number (match-string 1 ans))
  15594. day (string-to-number (match-string 2 ans)))
  15595. (if (< year 100) (setq year (+ 2000 year)))
  15596. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  15597. t nil ans)))
  15598. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  15599. ;; If there is a time with am/pm, and *no* time without it, we convert
  15600. ;; so that matching will be successful.
  15601. (loop for i from 1 to 2 do ; twice, for end time as well
  15602. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  15603. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  15604. (setq hour (string-to-number (match-string 1 ans))
  15605. minute (if (match-end 3)
  15606. (string-to-number (match-string 3 ans))
  15607. 0)
  15608. pm (equal ?p
  15609. (string-to-char (downcase (match-string 4 ans)))))
  15610. (if (and (= hour 12) (not pm))
  15611. (setq hour 0)
  15612. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  15613. (setq ans (replace-match (format "%02d:%02d" hour minute)
  15614. t t ans))))
  15615. ;; Check if a time range is given as a duration
  15616. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  15617. (setq hour (string-to-number (match-string 1 ans))
  15618. h2 (+ hour (string-to-number (match-string 3 ans)))
  15619. minute (string-to-number (match-string 2 ans))
  15620. m2 (+ minute (if (match-end 5) (string-to-number
  15621. (match-string 5 ans))0)))
  15622. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  15623. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  15624. t t ans)))
  15625. ;; Check if there is a time range
  15626. (when (boundp 'org-end-time-was-given)
  15627. (setq org-time-was-given nil)
  15628. (when (and (string-match org-plain-time-of-day-regexp ans)
  15629. (match-end 8))
  15630. (setq org-end-time-was-given (match-string 8 ans))
  15631. (setq ans (concat (substring ans 0 (match-beginning 7))
  15632. (substring ans (match-end 7))))))
  15633. (setq tl (parse-time-string ans)
  15634. day (or (nth 3 tl) (nth 3 org-defdecode))
  15635. month
  15636. (cond ((nth 4 tl))
  15637. ((not org-read-date-prefer-future) (nth 4 org-defdecode))
  15638. ;; Day was specified. Make sure DAY+MONTH
  15639. ;; combination happens in the future.
  15640. ((nth 3 tl)
  15641. (setq futurep t)
  15642. (if (< day (nth 3 nowdecode)) (1+ (nth 4 nowdecode))
  15643. (nth 4 nowdecode)))
  15644. (t (nth 4 org-defdecode)))
  15645. year
  15646. (cond ((and (not kill-year) (nth 5 tl)))
  15647. ((not org-read-date-prefer-future) (nth 5 org-defdecode))
  15648. ;; Month was guessed in the future and is at least
  15649. ;; equal to NOWDECODE's. Fix year accordingly.
  15650. (futurep
  15651. (if (or (> month (nth 4 nowdecode))
  15652. (>= day (nth 3 nowdecode)))
  15653. (nth 5 nowdecode)
  15654. (1+ (nth 5 nowdecode))))
  15655. ;; Month was specified. Make sure MONTH+YEAR
  15656. ;; combination happens in the future.
  15657. ((nth 4 tl)
  15658. (setq futurep t)
  15659. (cond ((> month (nth 4 nowdecode)) (nth 5 nowdecode))
  15660. ((< month (nth 4 nowdecode)) (1+ (nth 5 nowdecode)))
  15661. ((< day (nth 3 nowdecode)) (1+ (nth 5 nowdecode)))
  15662. (t (nth 5 nowdecode))))
  15663. (t (nth 5 org-defdecode)))
  15664. hour (or (nth 2 tl) (nth 2 org-defdecode))
  15665. minute (or (nth 1 tl) (nth 1 org-defdecode))
  15666. second (or (nth 0 tl) 0)
  15667. wday (nth 6 tl))
  15668. (when (and (eq org-read-date-prefer-future 'time)
  15669. (not (nth 3 tl)) (not (nth 4 tl)) (not (nth 5 tl))
  15670. (equal day (nth 3 nowdecode))
  15671. (equal month (nth 4 nowdecode))
  15672. (equal year (nth 5 nowdecode))
  15673. (nth 2 tl)
  15674. (or (< (nth 2 tl) (nth 2 nowdecode))
  15675. (and (= (nth 2 tl) (nth 2 nowdecode))
  15676. (nth 1 tl)
  15677. (< (nth 1 tl) (nth 1 nowdecode)))))
  15678. (setq day (1+ day)
  15679. futurep t))
  15680. ;; Special date definitions below
  15681. (cond
  15682. (iso-week
  15683. ;; There was an iso week
  15684. (require 'cal-iso)
  15685. (setq futurep nil)
  15686. (setq year (or iso-year year)
  15687. day (or iso-weekday wday 1)
  15688. wday nil ; to make sure that the trigger below does not match
  15689. iso-date (calendar-gregorian-from-absolute
  15690. (calendar-iso-to-absolute
  15691. (list iso-week day year))))
  15692. ; FIXME: Should we also push ISO weeks into the future?
  15693. ; (when (and org-read-date-prefer-future
  15694. ; (not iso-year)
  15695. ; (< (calendar-absolute-from-gregorian iso-date)
  15696. ; (time-to-days (current-time))))
  15697. ; (setq year (1+ year)
  15698. ; iso-date (calendar-gregorian-from-absolute
  15699. ; (calendar-iso-to-absolute
  15700. ; (list iso-week day year)))))
  15701. (setq month (car iso-date)
  15702. year (nth 2 iso-date)
  15703. day (nth 1 iso-date)))
  15704. (deltan
  15705. (setq futurep nil)
  15706. (unless deltadef
  15707. ;; Pass `current-time' result to `decode-time' (instead of
  15708. ;; calling without arguments) so that only `current-time' has
  15709. ;; to be overriden in tests.
  15710. (let ((now (decode-time (current-time))))
  15711. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  15712. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  15713. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  15714. ((equal deltaw "m") (setq month (+ month deltan)))
  15715. ((equal deltaw "y") (setq year (+ year deltan)))))
  15716. ((and wday (not (nth 3 tl)))
  15717. ;; Weekday was given, but no day, so pick that day in the week
  15718. ;; on or after the derived date.
  15719. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  15720. (unless (equal wday wday1)
  15721. (setq day (+ day (% (- wday wday1 -7) 7))))))
  15722. (if (and (boundp 'org-time-was-given)
  15723. (nth 2 tl))
  15724. (setq org-time-was-given t))
  15725. (if (< year 100) (setq year (+ 2000 year)))
  15726. ;; Check of the date is representable
  15727. (if org-read-date-force-compatible-dates
  15728. (progn
  15729. (if (< year 1970)
  15730. (setq year 1970 org-read-date-analyze-forced-year t))
  15731. (if (> year 2037)
  15732. (setq year 2037 org-read-date-analyze-forced-year t)))
  15733. (condition-case nil
  15734. (ignore (encode-time second minute hour day month year))
  15735. (error
  15736. (setq year (nth 5 org-defdecode))
  15737. (setq org-read-date-analyze-forced-year t))))
  15738. (setq org-read-date-analyze-futurep futurep)
  15739. (list second minute hour day month year)))
  15740. (defvar parse-time-weekdays)
  15741. (defun org-read-date-get-relative (s today default)
  15742. "Check string S for special relative date string.
  15743. TODAY and DEFAULT are internal times, for today and for a default.
  15744. Return shift list (N what def-flag)
  15745. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  15746. N is the number of WHATs to shift.
  15747. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  15748. the DEFAULT date rather than TODAY."
  15749. (require 'parse-time)
  15750. (when (and
  15751. (string-match
  15752. (concat
  15753. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  15754. "\\([0-9]+\\)?"
  15755. "\\([hdwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  15756. "\\([ \t]\\|$\\)") s)
  15757. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  15758. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  15759. (string-to-char (substring (match-string 1 s) -1))
  15760. ?+))
  15761. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  15762. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  15763. (what (if (match-end 3) (match-string 3 s) "d"))
  15764. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  15765. (date (if rel default today))
  15766. (wday (nth 6 (decode-time date)))
  15767. delta)
  15768. (if wday1
  15769. (progn
  15770. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  15771. (if (= delta 0) (setq delta 7))
  15772. (if (= dir ?-)
  15773. (progn
  15774. (setq delta (- delta 7))
  15775. (if (= delta 0) (setq delta -7))))
  15776. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  15777. (list delta "d" rel))
  15778. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  15779. (defun org-order-calendar-date-args (arg1 arg2 arg3)
  15780. "Turn a user-specified date into the internal representation.
  15781. The internal representation needed by the calendar is (month day year).
  15782. This is a wrapper to handle the brain-dead convention in calendar that
  15783. user function argument order change dependent on argument order."
  15784. (if (boundp 'calendar-date-style)
  15785. (cond
  15786. ((eq calendar-date-style 'american)
  15787. (list arg1 arg2 arg3))
  15788. ((eq calendar-date-style 'european)
  15789. (list arg2 arg1 arg3))
  15790. ((eq calendar-date-style 'iso)
  15791. (list arg2 arg3 arg1)))
  15792. (org-no-warnings ;; european-calendar-style is obsolete as of version 23.1
  15793. (if (org-bound-and-true-p european-calendar-style)
  15794. (list arg2 arg1 arg3)
  15795. (list arg1 arg2 arg3)))))
  15796. (defun org-eval-in-calendar (form &optional keepdate)
  15797. "Eval FORM in the calendar window and return to current window.
  15798. When KEEPDATE is non-nil, update `org-ans2' from the cursor date,
  15799. otherwise stick to the current value of `org-ans2'."
  15800. (let ((sf (selected-frame))
  15801. (sw (selected-window)))
  15802. (select-window (get-buffer-window "*Calendar*" t))
  15803. (eval form)
  15804. (when (and (not keepdate) (calendar-cursor-to-date))
  15805. (let* ((date (calendar-cursor-to-date))
  15806. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15807. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  15808. (move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  15809. (select-window sw)
  15810. (org-select-frame-set-input-focus sf)))
  15811. (defun org-calendar-select ()
  15812. "Return to `org-read-date' with the date currently selected.
  15813. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  15814. (interactive)
  15815. (when (calendar-cursor-to-date)
  15816. (let* ((date (calendar-cursor-to-date))
  15817. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15818. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  15819. (if (active-minibuffer-window) (exit-minibuffer))))
  15820. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  15821. "Insert a date stamp for the date given by the internal TIME.
  15822. See `format-time-string' for the format of TIME.
  15823. WITH-HM means use the stamp format that includes the time of the day.
  15824. INACTIVE means use square brackets instead of angular ones, so that the
  15825. stamp will not contribute to the agenda.
  15826. PRE and POST are optional strings to be inserted before and after the
  15827. stamp.
  15828. The command returns the inserted time stamp."
  15829. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  15830. stamp)
  15831. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  15832. (insert-before-markers (or pre ""))
  15833. (when (listp extra)
  15834. (setq extra (car extra))
  15835. (if (and (stringp extra)
  15836. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  15837. (setq extra (format "-%02d:%02d"
  15838. (string-to-number (match-string 1 extra))
  15839. (string-to-number (match-string 2 extra))))
  15840. (setq extra nil)))
  15841. (when extra
  15842. (setq fmt (concat (substring fmt 0 -1) extra (substring fmt -1))))
  15843. (insert-before-markers (setq stamp (format-time-string fmt time)))
  15844. (insert-before-markers (or post ""))
  15845. (setq org-last-inserted-timestamp stamp)))
  15846. (defun org-toggle-time-stamp-overlays ()
  15847. "Toggle the use of custom time stamp formats."
  15848. (interactive)
  15849. (setq org-display-custom-times (not org-display-custom-times))
  15850. (unless org-display-custom-times
  15851. (let ((p (point-min)) (bmp (buffer-modified-p)))
  15852. (while (setq p (next-single-property-change p 'display))
  15853. (if (and (get-text-property p 'display)
  15854. (eq (get-text-property p 'face) 'org-date))
  15855. (remove-text-properties
  15856. p (setq p (next-single-property-change p 'display))
  15857. '(display t))))
  15858. (set-buffer-modified-p bmp)))
  15859. (if (featurep 'xemacs)
  15860. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  15861. (org-restart-font-lock)
  15862. (setq org-table-may-need-update t)
  15863. (if org-display-custom-times
  15864. (message "Time stamps are overlaid with custom format")
  15865. (message "Time stamp overlays removed")))
  15866. (defun org-display-custom-time (beg end)
  15867. "Overlay modified time stamp format over timestamp between BEG and END."
  15868. (let* ((ts (buffer-substring beg end))
  15869. t1 w1 with-hm tf time str w2 (off 0))
  15870. (save-match-data
  15871. (setq t1 (org-parse-time-string ts t))
  15872. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)?\\'" ts)
  15873. (setq off (- (match-end 0) (match-beginning 0)))))
  15874. (setq end (- end off))
  15875. (setq w1 (- end beg)
  15876. with-hm (and (nth 1 t1) (nth 2 t1))
  15877. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  15878. time (org-fix-decoded-time t1)
  15879. str (org-add-props
  15880. (format-time-string
  15881. (substring tf 1 -1) (apply 'encode-time time))
  15882. nil 'mouse-face 'highlight)
  15883. w2 (length str))
  15884. (if (not (= w2 w1))
  15885. (add-text-properties (1+ beg) (+ 2 beg)
  15886. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  15887. (if (featurep 'xemacs)
  15888. (progn
  15889. (put-text-property beg end 'invisible t)
  15890. (put-text-property beg end 'end-glyph (make-glyph str)))
  15891. (put-text-property beg end 'display str))))
  15892. (defun org-fix-decoded-time (time)
  15893. "Set 0 instead of nil for the first 6 elements of time.
  15894. Don't touch the rest."
  15895. (let ((n 0))
  15896. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  15897. (define-obsolete-function-alias 'org-days-to-time 'org-time-stamp-to-now "24.4")
  15898. (defun org-time-stamp-to-now (timestamp-string &optional seconds)
  15899. "Difference between TIMESTAMP-STRING and now in days.
  15900. If SECONDS is non-nil, return the difference in seconds."
  15901. (let ((fdiff (if seconds 'org-float-time 'time-to-days)))
  15902. (- (funcall fdiff (org-time-string-to-time timestamp-string))
  15903. (funcall fdiff (current-time)))))
  15904. (defun org-deadline-close (timestamp-string &optional ndays)
  15905. "Is the time in TIMESTAMP-STRING close to the current date?"
  15906. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  15907. (and (< (org-time-stamp-to-now timestamp-string) ndays)
  15908. (not (org-entry-is-done-p))))
  15909. (defun org-get-wdays (ts &optional delay zero-delay)
  15910. "Get the deadline lead time appropriate for timestring TS.
  15911. When DELAY is non-nil, get the delay time for scheduled items
  15912. instead of the deadline lead time. When ZERO-DELAY is non-nil
  15913. and `org-scheduled-delay-days' is 0, enforce 0 as the delay,
  15914. don't try to find the delay cookie in the scheduled timestamp."
  15915. (let ((tv (if delay org-scheduled-delay-days
  15916. org-deadline-warning-days)))
  15917. (cond
  15918. ((or (and delay (< tv 0))
  15919. (and delay zero-delay (<= tv 0))
  15920. (and (not delay) (<= tv 0)))
  15921. ;; Enforce this value no matter what
  15922. (- tv))
  15923. ((string-match "-\\([0-9]+\\)\\([hdwmy]\\)\\(\\'\\|>\\| \\)" ts)
  15924. ;; lead time is specified.
  15925. (floor (* (string-to-number (match-string 1 ts))
  15926. (cdr (assoc (match-string 2 ts)
  15927. '(("d" . 1) ("w" . 7)
  15928. ("m" . 30.4) ("y" . 365.25)
  15929. ("h" . 0.041667)))))))
  15930. ;; go for the default.
  15931. (t tv))))
  15932. (defun org-calendar-select-mouse (ev)
  15933. "Return to `org-read-date' with the date currently selected.
  15934. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  15935. (interactive "e")
  15936. (mouse-set-point ev)
  15937. (when (calendar-cursor-to-date)
  15938. (let* ((date (calendar-cursor-to-date))
  15939. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  15940. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  15941. (if (active-minibuffer-window) (exit-minibuffer))))
  15942. (defun org-check-deadlines (ndays)
  15943. "Check if there are any deadlines due or past due.
  15944. A deadline is considered due if it happens within `org-deadline-warning-days'
  15945. days from today's date. If the deadline appears in an entry marked DONE,
  15946. it is not shown. The prefix arg NDAYS can be used to test that many
  15947. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  15948. (interactive "P")
  15949. (let* ((org-warn-days
  15950. (cond
  15951. ((equal ndays '(4)) 100000)
  15952. (ndays (prefix-numeric-value ndays))
  15953. (t (abs org-deadline-warning-days))))
  15954. (case-fold-search nil)
  15955. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  15956. (callback
  15957. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  15958. (message "%d deadlines past-due or due within %d days"
  15959. (org-occur regexp nil callback)
  15960. org-warn-days)))
  15961. (defsubst org-re-timestamp (type)
  15962. "Return a regexp for timestamp TYPE.
  15963. Allowed values for TYPE are:
  15964. all: all timestamps
  15965. active: only active timestamps (<...>)
  15966. inactive: only inactive timestamps ([...])
  15967. scheduled: only scheduled timestamps
  15968. deadline: only deadline timestamps
  15969. closed: only closed time-stamps
  15970. When TYPE is nil, fall back on returning a regexp that matches
  15971. both scheduled and deadline timestamps."
  15972. (case type
  15973. (all org-ts-regexp-both)
  15974. (active org-ts-regexp)
  15975. (inactive org-ts-regexp-inactive)
  15976. (scheduled org-scheduled-time-regexp)
  15977. (deadline org-deadline-time-regexp)
  15978. (closed org-closed-time-regexp)
  15979. (otherwise
  15980. (concat "\\<"
  15981. (regexp-opt (list org-deadline-string org-scheduled-string))
  15982. " *<\\([^>]+\\)>"))))
  15983. (defun org-check-before-date (date)
  15984. "Check if there are deadlines or scheduled entries before DATE."
  15985. (interactive (list (org-read-date)))
  15986. (let ((case-fold-search nil)
  15987. (regexp (org-re-timestamp org-ts-type))
  15988. (callback
  15989. `(lambda ()
  15990. (let ((match (match-string 1)))
  15991. (and ,(if (memq org-ts-type '(active inactive all))
  15992. '(eq (org-element-type (org-element-context)) 'timestamp)
  15993. '(org-at-planning-p))
  15994. (time-less-p
  15995. (org-time-string-to-time match)
  15996. (org-time-string-to-time date)))))))
  15997. (message "%d entries before %s"
  15998. (org-occur regexp nil callback) date)))
  15999. (defun org-check-after-date (date)
  16000. "Check if there are deadlines or scheduled entries after DATE."
  16001. (interactive (list (org-read-date)))
  16002. (let ((case-fold-search nil)
  16003. (regexp (org-re-timestamp org-ts-type))
  16004. (callback
  16005. `(lambda ()
  16006. (let ((match (match-string 1)))
  16007. (and ,(if (memq org-ts-type '(active inactive all))
  16008. '(eq (org-element-type (org-element-context)) 'timestamp)
  16009. '(org-at-planning-p))
  16010. (not (time-less-p
  16011. (org-time-string-to-time match)
  16012. (org-time-string-to-time date))))))))
  16013. (message "%d entries after %s"
  16014. (org-occur regexp nil callback) date)))
  16015. (defun org-check-dates-range (start-date end-date)
  16016. "Check for deadlines/scheduled entries between START-DATE and END-DATE."
  16017. (interactive (list (org-read-date nil nil nil "Range starts")
  16018. (org-read-date nil nil nil "Range end")))
  16019. (let ((case-fold-search nil)
  16020. (regexp (org-re-timestamp org-ts-type))
  16021. (callback
  16022. `(lambda ()
  16023. (let ((match (match-string 1)))
  16024. (and
  16025. ,(if (memq org-ts-type '(active inactive all))
  16026. '(eq (org-element-type (org-element-context)) 'timestamp)
  16027. '(org-at-planning-p))
  16028. (not (time-less-p
  16029. (org-time-string-to-time match)
  16030. (org-time-string-to-time start-date)))
  16031. (time-less-p
  16032. (org-time-string-to-time match)
  16033. (org-time-string-to-time end-date)))))))
  16034. (message "%d entries between %s and %s"
  16035. (org-occur regexp nil callback) start-date end-date)))
  16036. (defun org-evaluate-time-range (&optional to-buffer)
  16037. "Evaluate a time range by computing the difference between start and end.
  16038. Normally the result is just printed in the echo area, but with prefix arg
  16039. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  16040. If the time range is actually in a table, the result is inserted into the
  16041. next column.
  16042. For time difference computation, a year is assumed to be exactly 365
  16043. days in order to avoid rounding problems."
  16044. (interactive "P")
  16045. (or
  16046. (org-clock-update-time-maybe)
  16047. (save-excursion
  16048. (unless (org-at-date-range-p t)
  16049. (goto-char (point-at-bol))
  16050. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  16051. (if (not (org-at-date-range-p t))
  16052. (user-error "Not at a time-stamp range, and none found in current line")))
  16053. (let* ((ts1 (match-string 1))
  16054. (ts2 (match-string 2))
  16055. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  16056. (match-end (match-end 0))
  16057. (time1 (org-time-string-to-time ts1))
  16058. (time2 (org-time-string-to-time ts2))
  16059. (t1 (org-float-time time1))
  16060. (t2 (org-float-time time2))
  16061. (diff (abs (- t2 t1)))
  16062. (negative (< (- t2 t1) 0))
  16063. ;; (ys (floor (* 365 24 60 60)))
  16064. (ds (* 24 60 60))
  16065. (hs (* 60 60))
  16066. (fy "%dy %dd %02d:%02d")
  16067. (fy1 "%dy %dd")
  16068. (fd "%dd %02d:%02d")
  16069. (fd1 "%dd")
  16070. (fh "%02d:%02d")
  16071. y d h m align)
  16072. (if havetime
  16073. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  16074. y 0
  16075. d (floor (/ diff ds)) diff (mod diff ds)
  16076. h (floor (/ diff hs)) diff (mod diff hs)
  16077. m (floor (/ diff 60)))
  16078. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  16079. y 0
  16080. d (floor (+ (/ diff ds) 0.5))
  16081. h 0 m 0))
  16082. (if (not to-buffer)
  16083. (message "%s" (org-make-tdiff-string y d h m))
  16084. (if (org-at-table-p)
  16085. (progn
  16086. (goto-char match-end)
  16087. (setq align t)
  16088. (and (looking-at " *|") (goto-char (match-end 0))))
  16089. (goto-char match-end))
  16090. (if (looking-at
  16091. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  16092. (replace-match ""))
  16093. (if negative (insert " -"))
  16094. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  16095. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  16096. (insert " " (format fh h m))))
  16097. (if align (org-table-align))
  16098. (message "Time difference inserted")))))
  16099. (defun org-make-tdiff-string (y d h m)
  16100. (let ((fmt "")
  16101. (l nil))
  16102. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  16103. l (push y l)))
  16104. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  16105. l (push d l)))
  16106. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  16107. l (push h l)))
  16108. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  16109. l (push m l)))
  16110. (apply 'format fmt (nreverse l))))
  16111. (defun org-time-string-to-time (s &optional buffer pos)
  16112. "Convert a timestamp string into internal time."
  16113. (condition-case errdata
  16114. (apply 'encode-time (org-parse-time-string s))
  16115. (error (error "Bad timestamp `%s'%s\nError was: %s"
  16116. s (if (not (and buffer pos))
  16117. ""
  16118. (format " at %d in buffer `%s'" pos buffer))
  16119. (cdr errdata)))))
  16120. (defun org-time-string-to-seconds (s)
  16121. "Convert a timestamp string to a number of seconds."
  16122. (org-float-time (org-time-string-to-time s)))
  16123. (defun org-time-string-to-absolute (s &optional daynr prefer show-all buffer pos)
  16124. "Convert a time stamp to an absolute day number.
  16125. If there is a specifier for a cyclic time stamp, get the closest
  16126. date to DAYNR.
  16127. PREFER and SHOW-ALL are passed through to `org-closest-date'.
  16128. The variable `date' is bound by the calendar when this is called."
  16129. (cond
  16130. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  16131. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  16132. daynr
  16133. (+ daynr 1000)))
  16134. ((and daynr (string-match "\\+\\([0-9]+\\)[hdwmy]" s)
  16135. (> (string-to-number (match-string 1 s)) 0))
  16136. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  16137. (time-to-days (current-time))) (match-string 0 s)
  16138. prefer show-all))
  16139. (t (time-to-days
  16140. (condition-case errdata
  16141. (apply 'encode-time (org-parse-time-string s))
  16142. (error (error "Bad timestamp `%s'%s\nError was: %s"
  16143. s (if (not (and buffer pos))
  16144. ""
  16145. (format " at %d in buffer `%s'" pos buffer))
  16146. (cdr errdata))))))))
  16147. (defun org-days-to-iso-week (days)
  16148. "Return the iso week number."
  16149. (require 'cal-iso)
  16150. (car (calendar-iso-from-absolute days)))
  16151. (defun org-small-year-to-year (year)
  16152. "Convert 2-digit years into 4-digit years.
  16153. YEAR is expanded into one of the 30 next years, if possible, or
  16154. into a past one. Any year larger than 99 is returned unchanged."
  16155. (if (>= year 100) year
  16156. (let* ((current (string-to-number (format-time-string "%Y" (current-time))))
  16157. (century (/ current 100))
  16158. (offset (- year (% current 100))))
  16159. (cond ((> offset 30) (+ (* (1- century) 100) year))
  16160. ((> offset -70) (+ (* century 100) year))
  16161. (t (+ (* (1+ century) 100) year))))))
  16162. (defun org-time-from-absolute (d)
  16163. "Return the time corresponding to date D.
  16164. D may be an absolute day number, or a calendar-type list (month day year)."
  16165. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  16166. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  16167. (defun org-calendar-holiday ()
  16168. "List of holidays, for Diary display in Org-mode."
  16169. (require 'holidays)
  16170. (let ((hl (funcall
  16171. (if (fboundp 'calendar-check-holidays)
  16172. 'calendar-check-holidays 'check-calendar-holidays) date)))
  16173. (if hl (mapconcat 'identity hl "; "))))
  16174. (defun org-diary-sexp-entry (sexp entry date)
  16175. "Process a SEXP diary ENTRY for DATE."
  16176. (require 'diary-lib)
  16177. (let ((result (if calendar-debug-sexp
  16178. (let ((stack-trace-on-error t))
  16179. (eval (car (read-from-string sexp))))
  16180. (condition-case nil
  16181. (eval (car (read-from-string sexp)))
  16182. (error
  16183. (beep)
  16184. (message "Bad sexp at line %d in %s: %s"
  16185. (org-current-line)
  16186. (buffer-file-name) sexp)
  16187. (sleep-for 2))))))
  16188. (cond ((stringp result) (split-string result "; "))
  16189. ((and (consp result)
  16190. (not (consp (cdr result)))
  16191. (stringp (cdr result))) (cdr result))
  16192. ((and (consp result)
  16193. (stringp (car result))) result)
  16194. (result entry))))
  16195. (defun org-diary-to-ical-string (frombuf)
  16196. "Get iCalendar entries from diary entries in buffer FROMBUF.
  16197. This uses the icalendar.el library."
  16198. (let* ((tmpdir (if (featurep 'xemacs)
  16199. (temp-directory)
  16200. temporary-file-directory))
  16201. (tmpfile (make-temp-name
  16202. (expand-file-name "orgics" tmpdir)))
  16203. buf rtn b e)
  16204. (with-current-buffer frombuf
  16205. (icalendar-export-region (point-min) (point-max) tmpfile)
  16206. (setq buf (find-buffer-visiting tmpfile))
  16207. (set-buffer buf)
  16208. (goto-char (point-min))
  16209. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  16210. (setq b (match-beginning 0)))
  16211. (goto-char (point-max))
  16212. (if (re-search-backward "^END:VEVENT" nil t)
  16213. (setq e (match-end 0)))
  16214. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  16215. (kill-buffer buf)
  16216. (delete-file tmpfile)
  16217. rtn))
  16218. (defun org-closest-date (start current change prefer show-all)
  16219. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  16220. When PREFER is `past', return a date that is either CURRENT or past.
  16221. When PREFER is `future', return a date that is either CURRENT or future.
  16222. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  16223. ;; Make the proper lists from the dates
  16224. (catch 'exit
  16225. (let ((a1 '(("h" . hour)
  16226. ("d" . day)
  16227. ("w" . week)
  16228. ("m" . month)
  16229. ("y" . year)))
  16230. (shour (nth 2 (org-parse-time-string start)))
  16231. dn dw sday cday n1 n2 n0
  16232. d m y y1 y2 date1 date2 nmonths nm ny m2)
  16233. (setq start (org-date-to-gregorian start)
  16234. current (org-date-to-gregorian
  16235. (if show-all
  16236. current
  16237. (time-to-days (current-time))))
  16238. sday (calendar-absolute-from-gregorian start)
  16239. cday (calendar-absolute-from-gregorian current))
  16240. (if (<= cday sday) (throw 'exit sday))
  16241. (when (string-match "\\(\\+[0-9]+\\)\\([hdwmy]\\)" change)
  16242. (setq dn (string-to-number (match-string 1 change))
  16243. dw (cdr (assoc (match-string 2 change) a1))))
  16244. (unless (and dn (> dn 0))
  16245. (user-error "Invalid change specifier: %s" change))
  16246. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  16247. (cond
  16248. ((eq dw 'hour)
  16249. (let ((missing-hours
  16250. (mod (+ (- (* 24 (- cday sday)) shour) org-extend-today-until)
  16251. dn)))
  16252. (setq n1 (if (zerop missing-hours) cday
  16253. (- cday (1+ (floor (/ missing-hours 24)))))
  16254. n2 (+ cday (floor (/ (- dn missing-hours) 24))))))
  16255. ((eq dw 'day)
  16256. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  16257. n2 (+ n1 dn)))
  16258. ((eq dw 'year)
  16259. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  16260. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  16261. (setq date1 (list m d y1)
  16262. n1 (calendar-absolute-from-gregorian date1)
  16263. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  16264. n2 (calendar-absolute-from-gregorian date2)))
  16265. ((eq dw 'month)
  16266. ;; approx number of month between the two dates
  16267. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  16268. ;; How often does dn fit in there?
  16269. (setq d (nth 1 start) m (car start) y (nth 2 start)
  16270. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  16271. m (+ m nm)
  16272. ny (floor (/ m 12))
  16273. y (+ y ny)
  16274. m (- m (* ny 12)))
  16275. (while (> m 12) (setq m (- m 12) y (1+ y)))
  16276. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  16277. (setq m2 (+ m dn) y2 y)
  16278. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  16279. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  16280. (while (<= n2 cday)
  16281. (setq n1 n2 m m2 y y2)
  16282. (setq m2 (+ m dn) y2 y)
  16283. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  16284. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  16285. ;; Make sure n1 is the earlier date
  16286. (setq n0 n1 n1 (min n1 n2) n2 (max n0 n2))
  16287. (if show-all
  16288. (cond
  16289. ((eq prefer 'past) (if (= cday n2) n2 n1))
  16290. ((eq prefer 'future) (if (= cday n1) n1 n2))
  16291. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  16292. (cond
  16293. ((eq prefer 'past) (if (= cday n2) n2 n1))
  16294. ((eq prefer 'future) (if (= cday n1) n1 n2))
  16295. (t (if (= cday n1) n1 n2)))))))
  16296. (defun org-date-to-gregorian (date)
  16297. "Turn any specification of DATE into a Gregorian date for the calendar."
  16298. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  16299. ((and (listp date) (= (length date) 3)) date)
  16300. ((stringp date)
  16301. (setq date (org-parse-time-string date))
  16302. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  16303. ((listp date)
  16304. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  16305. (defun org-parse-time-string (s &optional nodefault)
  16306. "Parse the standard Org-mode time string.
  16307. This should be a lot faster than the normal `parse-time-string'.
  16308. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  16309. hour and minute fields will be nil if not given."
  16310. (cond ((string-match org-ts-regexp0 s)
  16311. (list 0
  16312. (if (or (match-beginning 8) (not nodefault))
  16313. (string-to-number (or (match-string 8 s) "0")))
  16314. (if (or (match-beginning 7) (not nodefault))
  16315. (string-to-number (or (match-string 7 s) "0")))
  16316. (string-to-number (match-string 4 s))
  16317. (string-to-number (match-string 3 s))
  16318. (string-to-number (match-string 2 s))
  16319. nil nil nil))
  16320. ((string-match "^<[^>]+>$" s)
  16321. (decode-time (seconds-to-time (org-matcher-time s))))
  16322. (t (error "Not a standard Org-mode time string: %s" s))))
  16323. (defun org-timestamp-up (&optional arg)
  16324. "Increase the date item at the cursor by one.
  16325. If the cursor is on the year, change the year. If it is on the month,
  16326. the day or the time, change that.
  16327. With prefix ARG, change by that many units."
  16328. (interactive "p")
  16329. (org-timestamp-change (prefix-numeric-value arg) nil 'updown))
  16330. (defun org-timestamp-down (&optional arg)
  16331. "Decrease the date item at the cursor by one.
  16332. If the cursor is on the year, change the year. If it is on the month,
  16333. the day or the time, change that.
  16334. With prefix ARG, change by that many units."
  16335. (interactive "p")
  16336. (org-timestamp-change (- (prefix-numeric-value arg)) nil 'updown))
  16337. (defun org-timestamp-up-day (&optional arg)
  16338. "Increase the date in the time stamp by one day.
  16339. With prefix ARG, change that many days."
  16340. (interactive "p")
  16341. (if (and (not (org-at-timestamp-p t))
  16342. (org-at-heading-p))
  16343. (org-todo 'up)
  16344. (org-timestamp-change (prefix-numeric-value arg) 'day 'updown)))
  16345. (defun org-timestamp-down-day (&optional arg)
  16346. "Decrease the date in the time stamp by one day.
  16347. With prefix ARG, change that many days."
  16348. (interactive "p")
  16349. (if (and (not (org-at-timestamp-p t))
  16350. (org-at-heading-p))
  16351. (org-todo 'down)
  16352. (org-timestamp-change (- (prefix-numeric-value arg)) 'day) 'updown))
  16353. (defun org-at-timestamp-p (&optional inactive-ok)
  16354. "Non-nil if point is inside a timestamp.
  16355. When optional argument INACTIVE-OK is non-nil, also consider
  16356. inactive timestamps.
  16357. When this function returns a non-nil value, match data is set
  16358. according to `org-ts-regexp3' or `org-ts-regexp2', depending on
  16359. INACTIVE-OK."
  16360. (interactive)
  16361. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  16362. (pos (point))
  16363. (ans (or (looking-at tsr)
  16364. (save-excursion
  16365. (skip-chars-backward "^[<\n\r\t")
  16366. (if (> (point) (point-min)) (backward-char 1))
  16367. (and (looking-at tsr)
  16368. (> (- (match-end 0) pos) -1))))))
  16369. (and ans
  16370. (boundp 'org-ts-what)
  16371. (setq org-ts-what
  16372. (cond
  16373. ((= pos (match-beginning 0)) 'bracket)
  16374. ;; Point is considered to be "on the bracket" whether
  16375. ;; it's really on it or right after it.
  16376. ((= pos (1- (match-end 0))) 'bracket)
  16377. ((= pos (match-end 0)) 'after)
  16378. ((org-pos-in-match-range pos 2) 'year)
  16379. ((org-pos-in-match-range pos 3) 'month)
  16380. ((org-pos-in-match-range pos 7) 'hour)
  16381. ((org-pos-in-match-range pos 8) 'minute)
  16382. ((or (org-pos-in-match-range pos 4)
  16383. (org-pos-in-match-range pos 5)) 'day)
  16384. ((and (> pos (or (match-end 8) (match-end 5)))
  16385. (< pos (match-end 0)))
  16386. (- pos (or (match-end 8) (match-end 5))))
  16387. (t 'day))))
  16388. ans))
  16389. (defun org-toggle-timestamp-type ()
  16390. "Toggle the type (<active> or [inactive]) of a time stamp."
  16391. (interactive)
  16392. (when (org-at-timestamp-p t)
  16393. (let ((beg (match-beginning 0)) (end (match-end 0))
  16394. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  16395. (save-excursion
  16396. (goto-char beg)
  16397. (while (re-search-forward "[][<>]" end t)
  16398. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  16399. t t)))
  16400. (message "Timestamp is now %sactive"
  16401. (if (equal (char-after beg) ?<) "" "in")))))
  16402. (defun org-at-clock-log-p nil
  16403. "Is the cursor on the clock log line?"
  16404. (save-excursion
  16405. (move-beginning-of-line 1)
  16406. (looking-at org-clock-line-re)))
  16407. (defvar org-clock-history) ; defined in org-clock.el
  16408. (defvar org-clock-adjust-closest nil) ; defined in org-clock.el
  16409. (defun org-timestamp-change (n &optional what updown suppress-tmp-delay)
  16410. "Change the date in the time stamp at point.
  16411. The date will be changed by N times WHAT. WHAT can be `day', `month',
  16412. `year', `minute', `second'. If WHAT is not given, the cursor position
  16413. in the timestamp determines what will be changed.
  16414. When SUPPRESS-TMP-DELAY is non-nil, suppress delays like \"--2d\"."
  16415. (let ((origin (point)) origin-cat
  16416. with-hm inactive
  16417. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  16418. org-ts-what
  16419. extra rem
  16420. ts time time0 fixnext clrgx)
  16421. (if (not (org-at-timestamp-p t))
  16422. (user-error "Not at a timestamp"))
  16423. (if (and (not what) (eq org-ts-what 'bracket))
  16424. (org-toggle-timestamp-type)
  16425. ;; Point isn't on brackets. Remember the part of the time-stamp
  16426. ;; the point was in. Indeed, size of time-stamps may change,
  16427. ;; but point must be kept in the same category nonetheless.
  16428. (setq origin-cat org-ts-what)
  16429. (if (and (not what) (not (eq org-ts-what 'day))
  16430. org-display-custom-times
  16431. (get-text-property (point) 'display)
  16432. (not (get-text-property (1- (point)) 'display)))
  16433. (setq org-ts-what 'day))
  16434. (setq org-ts-what (or what org-ts-what)
  16435. inactive (= (char-after (match-beginning 0)) ?\[)
  16436. ts (match-string 0))
  16437. (replace-match "")
  16438. (when (string-match
  16439. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?-?[-+][0-9]+[hdwmy]\\(/[0-9]+[hdwmy]\\)?\\)*\\)[]>]"
  16440. ts)
  16441. (setq extra (match-string 1 ts))
  16442. (if suppress-tmp-delay
  16443. (setq extra (replace-regexp-in-string " --[0-9]+[hdwmy]" "" extra))))
  16444. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  16445. (setq with-hm t))
  16446. (setq time0 (org-parse-time-string ts))
  16447. (when (and updown
  16448. (eq org-ts-what 'minute)
  16449. (not current-prefix-arg))
  16450. ;; This looks like s-up and s-down. Change by one rounding step.
  16451. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  16452. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  16453. (setcar (cdr time0) (+ (nth 1 time0)
  16454. (if (> n 0) (- rem) (- dm rem))))))
  16455. (setq time
  16456. (encode-time (or (car time0) 0)
  16457. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  16458. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  16459. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  16460. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  16461. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  16462. (nthcdr 6 time0)))
  16463. (when (and (member org-ts-what '(hour minute))
  16464. extra
  16465. (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra))
  16466. (setq extra (org-modify-ts-extra
  16467. extra
  16468. (if (eq org-ts-what 'hour) 2 5)
  16469. n dm)))
  16470. (when (integerp org-ts-what)
  16471. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  16472. (if (eq what 'calendar)
  16473. (let ((cal-date (org-get-date-from-calendar)))
  16474. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  16475. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  16476. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  16477. (setcar time0 (or (car time0) 0))
  16478. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  16479. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  16480. (setq time (apply 'encode-time time0))))
  16481. ;; Insert the new time-stamp, and ensure point stays in the same
  16482. ;; category as before (i.e. not after the last position in that
  16483. ;; category).
  16484. (let ((pos (point)))
  16485. ;; Stay before inserted string. `save-excursion' is of no use.
  16486. (setq org-last-changed-timestamp
  16487. (org-insert-time-stamp time with-hm inactive nil nil extra))
  16488. (goto-char pos))
  16489. (save-match-data
  16490. (looking-at org-ts-regexp3)
  16491. (goto-char (cond
  16492. ;; `day' category ends before `hour' if any, or at
  16493. ;; the end of the day name.
  16494. ((eq origin-cat 'day)
  16495. (min (or (match-beginning 7) (1- (match-end 5))) origin))
  16496. ((eq origin-cat 'hour) (min (match-end 7) origin))
  16497. ((eq origin-cat 'minute) (min (1- (match-end 8)) origin))
  16498. ((integerp origin-cat) (min (1- (match-end 0)) origin))
  16499. ;; `year' and `month' have both fixed size: point
  16500. ;; couldn't have moved into another part.
  16501. (t origin))))
  16502. ;; Update clock if on a CLOCK line.
  16503. (org-clock-update-time-maybe)
  16504. ;; Maybe adjust the closest clock in `org-clock-history'
  16505. (when org-clock-adjust-closest
  16506. (if (not (and (org-at-clock-log-p)
  16507. (< 1 (length (delq nil (mapcar 'marker-position
  16508. org-clock-history))))))
  16509. (message "No clock to adjust")
  16510. (cond ((save-excursion ; fix previous clock?
  16511. (re-search-backward org-ts-regexp0 nil t)
  16512. (org-looking-back (concat org-clock-string " \\[")))
  16513. (setq fixnext 1 clrgx (concat org-ts-regexp0 "\\] =>.*$")))
  16514. ((save-excursion ; fix next clock?
  16515. (re-search-backward org-ts-regexp0 nil t)
  16516. (looking-at (concat org-ts-regexp0 "\\] =>")))
  16517. (setq fixnext -1 clrgx (concat org-clock-string " \\[" org-ts-regexp0))))
  16518. (save-window-excursion
  16519. ;; Find closest clock to point, adjust the previous/next one in history
  16520. (let* ((p (save-excursion (org-back-to-heading t)))
  16521. (cl (mapcar (lambda(c) (abs (- (marker-position c) p))) org-clock-history))
  16522. (clfixnth
  16523. (+ fixnext (- (length cl) (or (length (member (apply 'min cl) cl)) 100))))
  16524. (clfixpos (if (> 0 clfixnth) nil (nth clfixnth org-clock-history))))
  16525. (if (not clfixpos)
  16526. (message "No clock to adjust")
  16527. (save-excursion
  16528. (org-goto-marker-or-bmk clfixpos)
  16529. (org-show-subtree)
  16530. (when (re-search-forward clrgx nil t)
  16531. (goto-char (match-beginning 1))
  16532. (let (org-clock-adjust-closest)
  16533. (org-timestamp-change n org-ts-what updown))
  16534. (message "Clock adjusted in %s for heading: %s"
  16535. (file-name-nondirectory (buffer-file-name))
  16536. (org-get-heading t t)))))))))
  16537. ;; Try to recenter the calendar window, if any.
  16538. (if (and org-calendar-follow-timestamp-change
  16539. (get-buffer-window "*Calendar*" t)
  16540. (memq org-ts-what '(day month year)))
  16541. (org-recenter-calendar (time-to-days time))))))
  16542. (defun org-modify-ts-extra (s pos n dm)
  16543. "Change the different parts of the lead-time and repeat fields in timestamp."
  16544. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  16545. ng h m new rem)
  16546. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  16547. (cond
  16548. ((or (org-pos-in-match-range pos 2)
  16549. (org-pos-in-match-range pos 3))
  16550. (setq m (string-to-number (match-string 3 s))
  16551. h (string-to-number (match-string 2 s)))
  16552. (if (org-pos-in-match-range pos 2)
  16553. (setq h (+ h n))
  16554. (setq n (* dm (org-no-warnings (signum n))))
  16555. (when (not (= 0 (setq rem (% m dm))))
  16556. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  16557. (setq m (+ m n)))
  16558. (if (< m 0) (setq m (+ m 60) h (1- h)))
  16559. (if (> m 59) (setq m (- m 60) h (1+ h)))
  16560. (setq h (min 24 (max 0 h)))
  16561. (setq ng 1 new (format "-%02d:%02d" h m)))
  16562. ((org-pos-in-match-range pos 6)
  16563. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  16564. ((org-pos-in-match-range pos 5)
  16565. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  16566. ((org-pos-in-match-range pos 9)
  16567. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  16568. ((org-pos-in-match-range pos 8)
  16569. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  16570. (when ng
  16571. (setq s (concat
  16572. (substring s 0 (match-beginning ng))
  16573. new
  16574. (substring s (match-end ng))))))
  16575. s))
  16576. (defun org-recenter-calendar (date)
  16577. "If the calendar is visible, recenter it to DATE."
  16578. (let ((cwin (get-buffer-window "*Calendar*" t)))
  16579. (when cwin
  16580. (let ((calendar-move-hook nil))
  16581. (with-selected-window cwin
  16582. (calendar-goto-date (if (listp date) date
  16583. (calendar-gregorian-from-absolute date))))))))
  16584. (defun org-goto-calendar (&optional arg)
  16585. "Go to the Emacs calendar at the current date.
  16586. If there is a time stamp in the current line, go to that date.
  16587. A prefix ARG can be used to force the current date."
  16588. (interactive "P")
  16589. (let ((tsr org-ts-regexp) diff
  16590. (calendar-move-hook nil)
  16591. (calendar-view-holidays-initially-flag nil)
  16592. (calendar-view-diary-initially-flag nil))
  16593. (if (or (org-at-timestamp-p)
  16594. (save-excursion
  16595. (beginning-of-line 1)
  16596. (looking-at (concat ".*" tsr))))
  16597. (let ((d1 (time-to-days (current-time)))
  16598. (d2 (time-to-days
  16599. (org-time-string-to-time (match-string 1)))))
  16600. (setq diff (- d2 d1))))
  16601. (calendar)
  16602. (calendar-goto-today)
  16603. (if (and diff (not arg)) (calendar-forward-day diff))))
  16604. (defun org-get-date-from-calendar ()
  16605. "Return a list (month day year) of date at point in calendar."
  16606. (with-current-buffer "*Calendar*"
  16607. (save-match-data
  16608. (calendar-cursor-to-date))))
  16609. (defun org-date-from-calendar ()
  16610. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  16611. If there is already a time stamp at the cursor position, update it."
  16612. (interactive)
  16613. (if (org-at-timestamp-p t)
  16614. (org-timestamp-change 0 'calendar)
  16615. (let ((cal-date (org-get-date-from-calendar)))
  16616. (org-insert-time-stamp
  16617. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  16618. (defcustom org-effort-durations
  16619. `(("min" . 1)
  16620. ("h" . 60)
  16621. ("d" . ,(* 60 8))
  16622. ("w" . ,(* 60 8 5))
  16623. ("m" . ,(* 60 8 5 4))
  16624. ("y" . ,(* 60 8 5 40)))
  16625. "Conversion factor to minutes for an effort modifier.
  16626. Each entry has the form (MODIFIER . MINUTES).
  16627. In an effort string, a number followed by MODIFIER is multiplied
  16628. by the specified number of MINUTES to obtain an effort in
  16629. minutes.
  16630. For example, if the value of this variable is ((\"hours\" . 60)), then an
  16631. effort string \"2hours\" is equivalent to 120 minutes."
  16632. :group 'org-agenda
  16633. :version "25.1"
  16634. :package-version '(Org . "8.3")
  16635. :type '(alist :key-type (string :tag "Modifier")
  16636. :value-type (number :tag "Minutes")))
  16637. (defun org-minutes-to-clocksum-string (m)
  16638. "Format number of minutes as a clocksum string.
  16639. The format is determined by `org-time-clocksum-format',
  16640. `org-time-clocksum-use-fractional' and
  16641. `org-time-clocksum-fractional-format' and
  16642. `org-time-clocksum-use-effort-durations'."
  16643. (let ((clocksum "")
  16644. (m (round m)) ; Don't allow fractions of minutes
  16645. h d w mo y fmt n)
  16646. (setq h (if org-time-clocksum-use-effort-durations
  16647. (cdr (assoc "h" org-effort-durations)) 60)
  16648. d (if org-time-clocksum-use-effort-durations
  16649. (/ (cdr (assoc "d" org-effort-durations)) h) 24)
  16650. w (if org-time-clocksum-use-effort-durations
  16651. (/ (cdr (assoc "w" org-effort-durations)) (* d h)) 7)
  16652. mo (if org-time-clocksum-use-effort-durations
  16653. (/ (cdr (assoc "m" org-effort-durations)) (* d h)) 30)
  16654. y (if org-time-clocksum-use-effort-durations
  16655. (/ (cdr (assoc "y" org-effort-durations)) (* d h)) 365))
  16656. ;; fractional format
  16657. (if org-time-clocksum-use-fractional
  16658. (cond
  16659. ;; single format string
  16660. ((stringp org-time-clocksum-fractional-format)
  16661. (format org-time-clocksum-fractional-format (/ m (float h))))
  16662. ;; choice of fractional formats for different time units
  16663. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :years))
  16664. (> (/ (truncate m) (* y d h)) 0))
  16665. (format fmt (/ m (* y d (float h)))))
  16666. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :months))
  16667. (> (/ (truncate m) (* mo d h)) 0))
  16668. (format fmt (/ m (* mo d (float h)))))
  16669. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :weeks))
  16670. (> (/ (truncate m) (* w d h)) 0))
  16671. (format fmt (/ m (* w d (float h)))))
  16672. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :days))
  16673. (> (/ (truncate m) (* d h)) 0))
  16674. (format fmt (/ m (* d (float h)))))
  16675. ((and (setq fmt (plist-get org-time-clocksum-fractional-format :hours))
  16676. (> (/ (truncate m) h) 0))
  16677. (format fmt (/ m (float h))))
  16678. ((setq fmt (plist-get org-time-clocksum-fractional-format :minutes))
  16679. (format fmt m))
  16680. ;; fall back to smallest time unit with a format
  16681. ((setq fmt (plist-get org-time-clocksum-fractional-format :hours))
  16682. (format fmt (/ m (float h))))
  16683. ((setq fmt (plist-get org-time-clocksum-fractional-format :days))
  16684. (format fmt (/ m (* d (float h)))))
  16685. ((setq fmt (plist-get org-time-clocksum-fractional-format :weeks))
  16686. (format fmt (/ m (* w d (float h)))))
  16687. ((setq fmt (plist-get org-time-clocksum-fractional-format :months))
  16688. (format fmt (/ m (* mo d (float h)))))
  16689. ((setq fmt (plist-get org-time-clocksum-fractional-format :years))
  16690. (format fmt (/ m (* y d (float h))))))
  16691. ;; standard (non-fractional) format, with single format string
  16692. (if (stringp org-time-clocksum-format)
  16693. (format org-time-clocksum-format (setq n (/ m h)) (- m (* h n)))
  16694. ;; separate formats components
  16695. (and (setq fmt (plist-get org-time-clocksum-format :years))
  16696. (or (> (setq n (/ (truncate m) (* y d h))) 0)
  16697. (plist-get org-time-clocksum-format :require-years))
  16698. (setq clocksum (concat clocksum (format fmt n))
  16699. m (- m (* n y d h))))
  16700. (and (setq fmt (plist-get org-time-clocksum-format :months))
  16701. (or (> (setq n (/ (truncate m) (* mo d h))) 0)
  16702. (plist-get org-time-clocksum-format :require-months))
  16703. (setq clocksum (concat clocksum (format fmt n))
  16704. m (- m (* n mo d h))))
  16705. (and (setq fmt (plist-get org-time-clocksum-format :weeks))
  16706. (or (> (setq n (/ (truncate m) (* w d h))) 0)
  16707. (plist-get org-time-clocksum-format :require-weeks))
  16708. (setq clocksum (concat clocksum (format fmt n))
  16709. m (- m (* n w d h))))
  16710. (and (setq fmt (plist-get org-time-clocksum-format :days))
  16711. (or (> (setq n (/ (truncate m) (* d h))) 0)
  16712. (plist-get org-time-clocksum-format :require-days))
  16713. (setq clocksum (concat clocksum (format fmt n))
  16714. m (- m (* n d h))))
  16715. (and (setq fmt (plist-get org-time-clocksum-format :hours))
  16716. (or (> (setq n (/ (truncate m) h)) 0)
  16717. (plist-get org-time-clocksum-format :require-hours))
  16718. (setq clocksum (concat clocksum (format fmt n))
  16719. m (- m (* n h))))
  16720. (and (setq fmt (plist-get org-time-clocksum-format :minutes))
  16721. (or (> m 0) (plist-get org-time-clocksum-format :require-minutes))
  16722. (setq clocksum (concat clocksum (format fmt m))))
  16723. ;; return formatted time duration
  16724. clocksum))))
  16725. (defalias 'org-minutes-to-hh:mm-string 'org-minutes-to-clocksum-string)
  16726. (make-obsolete 'org-minutes-to-hh:mm-string 'org-minutes-to-clocksum-string
  16727. "Org mode version 8.0")
  16728. (defun org-hours-to-clocksum-string (n)
  16729. (org-minutes-to-clocksum-string (* n 60)))
  16730. (defun org-hh:mm-string-to-minutes (s)
  16731. "Convert a string H:MM to a number of minutes.
  16732. If the string is just a number, interpret it as minutes.
  16733. In fact, the first hh:mm or number in the string will be taken,
  16734. there can be extra stuff in the string.
  16735. If no number is found, the return value is 0."
  16736. (cond
  16737. ((integerp s) s)
  16738. ((string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  16739. (+ (* (string-to-number (match-string 1 s)) 60)
  16740. (string-to-number (match-string 2 s))))
  16741. ((string-match "\\([0-9]+\\)" s)
  16742. (string-to-number (match-string 1 s)))
  16743. (t 0)))
  16744. (defcustom org-image-actual-width t
  16745. "Should we use the actual width of images when inlining them?
  16746. When set to t, always use the image width.
  16747. When set to a number, use imagemagick (when available) to set
  16748. the image's width to this value.
  16749. When set to a number in a list, try to get the width from any
  16750. #+ATTR.* keyword if it matches a width specification like
  16751. #+ATTR_HTML: :width 300px
  16752. and fall back on that number if none is found.
  16753. When set to nil, try to get the width from an #+ATTR.* keyword
  16754. and fall back on the original width if none is found.
  16755. This requires Emacs >= 24.1, build with imagemagick support."
  16756. :group 'org-appearance
  16757. :version "24.4"
  16758. :package-version '(Org . "8.0")
  16759. :type '(choice
  16760. (const :tag "Use the image width" t)
  16761. (integer :tag "Use a number of pixels")
  16762. (list :tag "Use #+ATTR* or a number of pixels" (integer))
  16763. (const :tag "Use #+ATTR* or don't resize" nil)))
  16764. (defcustom org-agenda-inhibit-startup nil
  16765. "Inhibit startup when preparing agenda buffers.
  16766. When this variable is t, the initialization of the Org agenda
  16767. buffers is inhibited: e.g. the visibility state is not set, the
  16768. tables are not re-aligned, etc."
  16769. :type 'boolean
  16770. :version "24.3"
  16771. :group 'org-agenda)
  16772. (define-obsolete-variable-alias
  16773. 'org-agenda-ignore-drawer-properties
  16774. 'org-agenda-ignore-properties "25.1")
  16775. (defcustom org-agenda-ignore-properties nil
  16776. "Avoid updating text properties when building the agenda.
  16777. Properties are used to prepare buffers for effort estimates,
  16778. appointments, statistics and subtree-local categories.
  16779. If you don't use these in the agenda, you can add them to this
  16780. list and agenda building will be a bit faster.
  16781. The value is a list, with zero or more of the symbols `effort', `appt',
  16782. `stats' or `category'."
  16783. :type '(set :greedy t
  16784. (const effort)
  16785. (const appt)
  16786. (const stats)
  16787. (const category))
  16788. :version "25.1"
  16789. :package-version '(Org . "8.3")
  16790. :group 'org-agenda)
  16791. (defun org-duration-string-to-minutes (s &optional output-to-string)
  16792. "Convert a duration string S to minutes.
  16793. A bare number is interpreted as minutes, modifiers can be set by
  16794. customizing `org-effort-durations' (which see).
  16795. Entries containing a colon are interpreted as H:MM by
  16796. `org-hh:mm-string-to-minutes'."
  16797. (let ((result 0)
  16798. (re (concat "\\([0-9.]+\\) *\\("
  16799. (regexp-opt (mapcar 'car org-effort-durations))
  16800. "\\)")))
  16801. (while (string-match re s)
  16802. (incf result (* (cdr (assoc (match-string 2 s) org-effort-durations))
  16803. (string-to-number (match-string 1 s))))
  16804. (setq s (replace-match "" nil t s)))
  16805. (setq result (floor result))
  16806. (incf result (org-hh:mm-string-to-minutes s))
  16807. (if output-to-string (number-to-string result) result)))
  16808. ;;;; Files
  16809. (defun org-save-all-org-buffers ()
  16810. "Save all Org-mode buffers without user confirmation."
  16811. (interactive)
  16812. (message "Saving all Org-mode buffers...")
  16813. (save-some-buffers t (lambda () (derived-mode-p 'org-mode)))
  16814. (when (featurep 'org-id) (org-id-locations-save))
  16815. (message "Saving all Org-mode buffers... done"))
  16816. (defun org-revert-all-org-buffers ()
  16817. "Revert all Org-mode buffers.
  16818. Prompt for confirmation when there are unsaved changes.
  16819. Be sure you know what you are doing before letting this function
  16820. overwrite your changes.
  16821. This function is useful in a setup where one tracks org files
  16822. with a version control system, to revert on one machine after pulling
  16823. changes from another. I believe the procedure must be like this:
  16824. 1. M-x org-save-all-org-buffers
  16825. 2. Pull changes from the other machine, resolve conflicts
  16826. 3. M-x org-revert-all-org-buffers"
  16827. (interactive)
  16828. (unless (yes-or-no-p "Revert all Org buffers from their files? ")
  16829. (user-error "Abort"))
  16830. (save-excursion
  16831. (save-window-excursion
  16832. (mapc
  16833. (lambda (b)
  16834. (when (and (with-current-buffer b (derived-mode-p 'org-mode))
  16835. (with-current-buffer b buffer-file-name))
  16836. (org-pop-to-buffer-same-window b)
  16837. (revert-buffer t 'no-confirm)))
  16838. (buffer-list))
  16839. (when (and (featurep 'org-id) org-id-track-globally)
  16840. (org-id-locations-load)))))
  16841. ;;;; Agenda files
  16842. ;;;###autoload
  16843. (defun org-switchb (&optional arg)
  16844. "Switch between Org buffers.
  16845. With one prefix argument, restrict available buffers to files.
  16846. With two prefix arguments, restrict available buffers to agenda files.
  16847. Defaults to `iswitchb' for buffer name completion.
  16848. Set `org-completion-use-ido' to make it use ido instead."
  16849. (interactive "P")
  16850. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  16851. ((equal arg '(16)) (org-buffer-list 'agenda))
  16852. (t (org-buffer-list))))
  16853. (org-completion-use-iswitchb org-completion-use-iswitchb)
  16854. (org-completion-use-ido org-completion-use-ido))
  16855. (unless (or org-completion-use-ido org-completion-use-iswitchb)
  16856. (setq org-completion-use-iswitchb t))
  16857. (org-pop-to-buffer-same-window
  16858. (org-icompleting-read "Org buffer: "
  16859. (mapcar 'list (mapcar 'buffer-name blist))
  16860. nil t))))
  16861. ;;; Define some older names previously used for this functionality
  16862. ;;;###autoload
  16863. (defalias 'org-ido-switchb 'org-switchb)
  16864. ;;;###autoload
  16865. (defalias 'org-iswitchb 'org-switchb)
  16866. (defun org-buffer-list (&optional predicate exclude-tmp)
  16867. "Return a list of Org buffers.
  16868. PREDICATE can be `export', `files' or `agenda'.
  16869. export restrict the list to Export buffers.
  16870. files restrict the list to buffers visiting Org files.
  16871. agenda restrict the list to buffers visiting agenda files.
  16872. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  16873. (let* ((bfn nil)
  16874. (agenda-files (and (eq predicate 'agenda)
  16875. (mapcar 'file-truename (org-agenda-files t))))
  16876. (filter
  16877. (cond
  16878. ((eq predicate 'files)
  16879. (lambda (b) (with-current-buffer b (derived-mode-p 'org-mode))))
  16880. ((eq predicate 'export)
  16881. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  16882. ((eq predicate 'agenda)
  16883. (lambda (b)
  16884. (with-current-buffer b
  16885. (and (derived-mode-p 'org-mode)
  16886. (setq bfn (buffer-file-name b))
  16887. (member (file-truename bfn) agenda-files)))))
  16888. (t (lambda (b) (with-current-buffer b
  16889. (or (derived-mode-p 'org-mode)
  16890. (string-match "\*Org .*Export"
  16891. (buffer-name b)))))))))
  16892. (delq nil
  16893. (mapcar
  16894. (lambda(b)
  16895. (if (and (funcall filter b)
  16896. (or (not exclude-tmp)
  16897. (not (string-match "tmp" (buffer-name b)))))
  16898. b
  16899. nil))
  16900. (buffer-list)))))
  16901. (defun org-agenda-files (&optional unrestricted archives)
  16902. "Get the list of agenda files.
  16903. Optional UNRESTRICTED means return the full list even if a restriction
  16904. is currently in place.
  16905. When ARCHIVES is t, include all archive files that are really being
  16906. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  16907. `org-agenda-archives-mode' is t."
  16908. (let ((files
  16909. (cond
  16910. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  16911. ((stringp org-agenda-files) (org-read-agenda-file-list))
  16912. ((listp org-agenda-files) org-agenda-files)
  16913. (t (error "Invalid value of `org-agenda-files'")))))
  16914. (setq files (apply 'append
  16915. (mapcar (lambda (f)
  16916. (if (file-directory-p f)
  16917. (directory-files
  16918. f t org-agenda-file-regexp)
  16919. (list f)))
  16920. files)))
  16921. (when org-agenda-skip-unavailable-files
  16922. (setq files (delq nil
  16923. (mapcar (function
  16924. (lambda (file)
  16925. (and (file-readable-p file) file)))
  16926. files))))
  16927. (when (or (eq archives t)
  16928. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  16929. (setq files (org-add-archive-files files)))
  16930. files))
  16931. (defun org-agenda-file-p (&optional file)
  16932. "Return non-nil, if FILE is an agenda file.
  16933. If FILE is omitted, use the file associated with the current
  16934. buffer."
  16935. (let ((fname (or file (buffer-file-name))))
  16936. (and fname
  16937. (member (file-truename fname)
  16938. (mapcar #'file-truename (org-agenda-files t))))))
  16939. (defun org-edit-agenda-file-list ()
  16940. "Edit the list of agenda files.
  16941. Depending on setup, this either uses customize to edit the variable
  16942. `org-agenda-files', or it visits the file that is holding the list. In the
  16943. latter case, the buffer is set up in a way that saving it automatically kills
  16944. the buffer and restores the previous window configuration."
  16945. (interactive)
  16946. (if (stringp org-agenda-files)
  16947. (let ((cw (current-window-configuration)))
  16948. (find-file org-agenda-files)
  16949. (org-set-local 'org-window-configuration cw)
  16950. (org-add-hook 'after-save-hook
  16951. (lambda ()
  16952. (set-window-configuration
  16953. (prog1 org-window-configuration
  16954. (kill-buffer (current-buffer))))
  16955. (org-install-agenda-files-menu)
  16956. (message "New agenda file list installed"))
  16957. nil 'local)
  16958. (message "%s" (substitute-command-keys
  16959. "Edit list and finish with \\[save-buffer]")))
  16960. (customize-variable 'org-agenda-files)))
  16961. (defun org-store-new-agenda-file-list (list)
  16962. "Set new value for the agenda file list and save it correctly."
  16963. (if (stringp org-agenda-files)
  16964. (let ((fe (org-read-agenda-file-list t)) b u)
  16965. (while (setq b (find-buffer-visiting org-agenda-files))
  16966. (kill-buffer b))
  16967. (with-temp-file org-agenda-files
  16968. (insert
  16969. (mapconcat
  16970. (lambda (f) ;; Keep un-expanded entries.
  16971. (if (setq u (assoc f fe))
  16972. (cdr u)
  16973. f))
  16974. list "\n")
  16975. "\n")))
  16976. (let ((org-mode-hook nil) (org-inhibit-startup t)
  16977. (org-insert-mode-line-in-empty-file nil))
  16978. (setq org-agenda-files list)
  16979. (customize-save-variable 'org-agenda-files org-agenda-files))))
  16980. (defun org-read-agenda-file-list (&optional pair-with-expansion)
  16981. "Read the list of agenda files from a file.
  16982. If PAIR-WITH-EXPANSION is t return pairs with un-expanded
  16983. filenames, used by `org-store-new-agenda-file-list' to write back
  16984. un-expanded file names."
  16985. (when (file-directory-p org-agenda-files)
  16986. (error "`org-agenda-files' cannot be a single directory"))
  16987. (when (stringp org-agenda-files)
  16988. (with-temp-buffer
  16989. (insert-file-contents org-agenda-files)
  16990. (mapcar
  16991. (lambda (f)
  16992. (let ((e (expand-file-name (substitute-in-file-name f)
  16993. org-directory)))
  16994. (if pair-with-expansion
  16995. (cons e f)
  16996. e)))
  16997. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*")))))
  16998. ;;;###autoload
  16999. (defun org-cycle-agenda-files ()
  17000. "Cycle through the files in `org-agenda-files'.
  17001. If the current buffer visits an agenda file, find the next one in the list.
  17002. If the current buffer does not, find the first agenda file."
  17003. (interactive)
  17004. (let* ((fs (or (org-agenda-files t)
  17005. (user-error "No agenda files")))
  17006. (files (copy-sequence fs))
  17007. (tcf (and buffer-file-name (file-truename buffer-file-name)))
  17008. file)
  17009. (when tcf
  17010. (while (and (setq file (pop files))
  17011. (not (equal (file-truename file) tcf)))))
  17012. (find-file (car (or files fs)))
  17013. (if (buffer-base-buffer) (org-pop-to-buffer-same-window (buffer-base-buffer)))))
  17014. (defun org-agenda-file-to-front (&optional to-end)
  17015. "Move/add the current file to the top of the agenda file list.
  17016. If the file is not present in the list, it is added to the front. If it is
  17017. present, it is moved there. With optional argument TO-END, add/move to the
  17018. end of the list."
  17019. (interactive "P")
  17020. (let ((org-agenda-skip-unavailable-files nil)
  17021. (file-alist (mapcar (lambda (x)
  17022. (cons (file-truename x) x))
  17023. (org-agenda-files t)))
  17024. (ctf (file-truename
  17025. (or buffer-file-name
  17026. (user-error "Please save the current buffer to a file"))))
  17027. x had)
  17028. (setq x (assoc ctf file-alist) had x)
  17029. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  17030. (if to-end
  17031. (setq file-alist (append (delq x file-alist) (list x)))
  17032. (setq file-alist (cons x (delq x file-alist))))
  17033. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  17034. (org-install-agenda-files-menu)
  17035. (message "File %s to %s of agenda file list"
  17036. (if had "moved" "added") (if to-end "end" "front"))))
  17037. (defun org-remove-file (&optional file)
  17038. "Remove current file from the list of files in variable `org-agenda-files'.
  17039. These are the files which are being checked for agenda entries.
  17040. Optional argument FILE means use this file instead of the current."
  17041. (interactive)
  17042. (let* ((org-agenda-skip-unavailable-files nil)
  17043. (file (or file buffer-file-name
  17044. (user-error "Current buffer does not visit a file")))
  17045. (true-file (file-truename file))
  17046. (afile (abbreviate-file-name file))
  17047. (files (delq nil (mapcar
  17048. (lambda (x)
  17049. (if (equal true-file
  17050. (file-truename x))
  17051. nil x))
  17052. (org-agenda-files t)))))
  17053. (if (not (= (length files) (length (org-agenda-files t))))
  17054. (progn
  17055. (org-store-new-agenda-file-list files)
  17056. (org-install-agenda-files-menu)
  17057. (message "Removed from Org Agenda list: %s" afile))
  17058. (message "File was not in list: %s (not removed)" afile))))
  17059. (defun org-file-menu-entry (file)
  17060. (vector file (list 'find-file file) t))
  17061. (defun org-check-agenda-file (file)
  17062. "Make sure FILE exists. If not, ask user what to do."
  17063. (when (not (file-exists-p file))
  17064. (message "Non-existent agenda file %s. [R]emove from list or [A]bort?"
  17065. (abbreviate-file-name file))
  17066. (let ((r (downcase (read-char-exclusive))))
  17067. (cond
  17068. ((equal r ?r)
  17069. (org-remove-file file)
  17070. (throw 'nextfile t))
  17071. (t (user-error "Abort"))))))
  17072. (defun org-get-agenda-file-buffer (file)
  17073. "Get an agenda buffer visiting FILE.
  17074. If the buffer needs to be created, add it to the list of buffers
  17075. which might be released later."
  17076. (let ((buf (org-find-base-buffer-visiting file)))
  17077. (if buf
  17078. buf ; just return it
  17079. ;; Make a new buffer and remember it
  17080. (setq buf (find-file-noselect file))
  17081. (if buf (push buf org-agenda-new-buffers))
  17082. buf)))
  17083. (defun org-release-buffers (blist)
  17084. "Release all buffers in list, asking the user for confirmation when needed.
  17085. When a buffer is unmodified, it is just killed. When modified, it is saved
  17086. \(if the user agrees) and then killed."
  17087. (let (file)
  17088. (dolist (buf blist)
  17089. (setq file (buffer-file-name buf))
  17090. (when (and (buffer-modified-p buf)
  17091. file
  17092. (y-or-n-p (format "Save file %s? " file)))
  17093. (with-current-buffer buf (save-buffer)))
  17094. (kill-buffer buf))))
  17095. (defun org-agenda-prepare-buffers (files)
  17096. "Create buffers for all agenda files, protect archived trees and comments."
  17097. (interactive)
  17098. (let ((pa '(:org-archived t))
  17099. (pc '(:org-comment t))
  17100. (pall '(:org-archived t :org-comment t))
  17101. (inhibit-read-only t)
  17102. (org-inhibit-startup org-agenda-inhibit-startup)
  17103. (rea (concat ":" org-archive-tag ":"))
  17104. file re pos)
  17105. (setq org-tag-alist-for-agenda nil
  17106. org-tag-groups-alist-for-agenda nil)
  17107. (save-excursion
  17108. (save-restriction
  17109. (dolist (file files)
  17110. (catch 'nextfile
  17111. (if (bufferp file)
  17112. (set-buffer file)
  17113. (org-check-agenda-file file)
  17114. (set-buffer (org-get-agenda-file-buffer file)))
  17115. (widen)
  17116. (org-set-regexps-and-options 'tags-only)
  17117. (setq pos (point))
  17118. (or (memq 'category org-agenda-ignore-properties)
  17119. (org-refresh-category-properties))
  17120. (or (memq 'stats org-agenda-ignore-properties)
  17121. (org-refresh-stats-properties))
  17122. (or (memq 'effort org-agenda-ignore-properties)
  17123. (org-refresh-effort-properties))
  17124. (or (memq 'appt org-agenda-ignore-properties)
  17125. (org-refresh-properties "APPT_WARNTIME" 'org-appt-warntime))
  17126. (setq org-todo-keywords-for-agenda
  17127. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  17128. (setq org-done-keywords-for-agenda
  17129. (append org-done-keywords-for-agenda org-done-keywords))
  17130. (setq org-todo-keyword-alist-for-agenda
  17131. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  17132. (setq org-tag-alist-for-agenda
  17133. (org-uniquify
  17134. (append org-tag-alist-for-agenda
  17135. org-tag-alist
  17136. org-tag-persistent-alist)))
  17137. (if org-group-tags
  17138. (setq org-tag-groups-alist-for-agenda
  17139. (org-uniquify-alist
  17140. (append org-tag-groups-alist-for-agenda org-tag-groups-alist))))
  17141. (org-with-silent-modifications
  17142. (save-excursion
  17143. (remove-text-properties (point-min) (point-max) pall)
  17144. (when org-agenda-skip-archived-trees
  17145. (goto-char (point-min))
  17146. (while (re-search-forward rea nil t)
  17147. (if (org-at-heading-p t)
  17148. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  17149. (goto-char (point-min))
  17150. (setq re (format "^\\*+ .*\\<%s\\>" org-comment-string))
  17151. (while (re-search-forward re nil t)
  17152. (when (save-match-data (org-in-commented-heading-p t))
  17153. (add-text-properties
  17154. (match-beginning 0) (org-end-of-subtree t) pc)))))
  17155. (goto-char pos)))))
  17156. (setq org-todo-keywords-for-agenda
  17157. (org-uniquify org-todo-keywords-for-agenda))
  17158. (setq org-todo-keyword-alist-for-agenda
  17159. (org-uniquify org-todo-keyword-alist-for-agenda))))
  17160. ;;;; CDLaTeX minor mode
  17161. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  17162. "Keymap for the minor `org-cdlatex-mode'.")
  17163. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  17164. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  17165. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  17166. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  17167. (org-defkey org-cdlatex-mode-map "\C-c{" 'org-cdlatex-environment-indent)
  17168. (defvar org-cdlatex-texmathp-advice-is-done nil
  17169. "Flag remembering if we have applied the advice to texmathp already.")
  17170. (define-minor-mode org-cdlatex-mode
  17171. "Toggle the minor `org-cdlatex-mode'.
  17172. This mode supports entering LaTeX environment and math in LaTeX fragments
  17173. in Org-mode.
  17174. \\{org-cdlatex-mode-map}"
  17175. nil " OCDL" nil
  17176. (when org-cdlatex-mode
  17177. (require 'cdlatex)
  17178. (run-hooks 'cdlatex-mode-hook)
  17179. (cdlatex-compute-tables))
  17180. (unless org-cdlatex-texmathp-advice-is-done
  17181. (setq org-cdlatex-texmathp-advice-is-done t)
  17182. (defadvice texmathp (around org-math-always-on activate)
  17183. "Always return t in org-mode buffers.
  17184. This is because we want to insert math symbols without dollars even outside
  17185. the LaTeX math segments. If Orgmode thinks that point is actually inside
  17186. an embedded LaTeX fragment, let texmathp do its job.
  17187. \\[org-cdlatex-mode-map]"
  17188. (interactive)
  17189. (let (p)
  17190. (cond
  17191. ((not (derived-mode-p 'org-mode)) ad-do-it)
  17192. ((eq this-command 'cdlatex-math-symbol)
  17193. (setq ad-return-value t
  17194. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  17195. (t
  17196. (let ((p (org-inside-LaTeX-fragment-p)))
  17197. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  17198. (setq ad-return-value t
  17199. texmathp-why '("Org-mode embedded math" . 0))
  17200. (if p ad-do-it)))))))))
  17201. (defun turn-on-org-cdlatex ()
  17202. "Unconditionally turn on `org-cdlatex-mode'."
  17203. (org-cdlatex-mode 1))
  17204. (defun org-try-cdlatex-tab ()
  17205. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  17206. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  17207. - inside a LaTeX fragment, or
  17208. - after the first word in a line, where an abbreviation expansion could
  17209. insert a LaTeX environment."
  17210. (when org-cdlatex-mode
  17211. (cond
  17212. ;; Before any word on the line: No expansion possible.
  17213. ((save-excursion (skip-chars-backward " \t") (bolp)) nil)
  17214. ;; Just after first word on the line: Expand it. Make sure it
  17215. ;; cannot happen on headlines, though.
  17216. ((save-excursion
  17217. (skip-chars-backward "a-zA-Z0-9*")
  17218. (skip-chars-backward " \t")
  17219. (and (bolp) (not (org-at-heading-p))))
  17220. (cdlatex-tab) t)
  17221. ((org-inside-LaTeX-fragment-p) (cdlatex-tab) t))))
  17222. (defun org-cdlatex-underscore-caret (&optional arg)
  17223. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  17224. Revert to the normal definition outside of these fragments."
  17225. (interactive "P")
  17226. (if (org-inside-LaTeX-fragment-p)
  17227. (call-interactively 'cdlatex-sub-superscript)
  17228. (let (org-cdlatex-mode)
  17229. (call-interactively (key-binding (vector last-input-event))))))
  17230. (defun org-cdlatex-math-modify (&optional arg)
  17231. "Execute `cdlatex-math-modify' in LaTeX fragments.
  17232. Revert to the normal definition outside of these fragments."
  17233. (interactive "P")
  17234. (if (org-inside-LaTeX-fragment-p)
  17235. (call-interactively 'cdlatex-math-modify)
  17236. (let (org-cdlatex-mode)
  17237. (call-interactively (key-binding (vector last-input-event))))))
  17238. (defun org-cdlatex-environment-indent (&optional environment item)
  17239. "Execute `cdlatex-environment' and indent the inserted environment.
  17240. ENVIRONMENT and ITEM are passed to `cdlatex-environment'.
  17241. The inserted environment is indented to current indentation
  17242. unless point is at the beginning of the line, in which the
  17243. environment remains unintended."
  17244. (interactive)
  17245. ;; cdlatex-environment always return nil. Therefore, capture output
  17246. ;; first and determine if an environment was selected.
  17247. (let* ((beg (point-marker))
  17248. (end (copy-marker (point) t))
  17249. (inserted (progn
  17250. (ignore-errors (cdlatex-environment environment item))
  17251. (< beg end)))
  17252. ;; Figure out how many lines to move forward after the
  17253. ;; environment has been inserted.
  17254. (lines (when inserted
  17255. (save-excursion
  17256. (- (loop while (< beg (point))
  17257. with x = 0
  17258. do (forward-line -1)
  17259. (incf x)
  17260. finally return x)
  17261. (if (progn (goto-char beg)
  17262. (and (progn (skip-chars-forward " \t") (eolp))
  17263. (progn (skip-chars-backward " \t") (bolp))))
  17264. 1 0)))))
  17265. (env (org-trim (delete-and-extract-region beg end))))
  17266. (when inserted
  17267. ;; Get indentation of next line unless at column 0.
  17268. (let ((ind (if (bolp) 0
  17269. (save-excursion
  17270. (org-return-indent)
  17271. (prog1 (org-get-indentation)
  17272. (when (progn (skip-chars-forward " \t") (eolp))
  17273. (delete-region beg (point)))))))
  17274. (bol (progn (skip-chars-backward " \t") (bolp))))
  17275. ;; Insert a newline before environment unless at column zero
  17276. ;; to "escape" the current line. Insert a newline if
  17277. ;; something is one the same line as \end{ENVIRONMENT}.
  17278. (insert
  17279. (concat (unless bol "\n") env
  17280. (when (and (skip-chars-forward " \t") (not (eolp))) "\n")))
  17281. (unless (zerop ind)
  17282. (save-excursion
  17283. (goto-char beg)
  17284. (while (< (point) end)
  17285. (unless (eolp) (org-indent-line-to ind))
  17286. (forward-line))))
  17287. (goto-char beg)
  17288. (forward-line lines)
  17289. (org-indent-line-to ind)))
  17290. (set-marker beg nil)
  17291. (set-marker end nil)))
  17292. ;;;; LaTeX fragments
  17293. (defun org-inside-LaTeX-fragment-p ()
  17294. "Test if point is inside a LaTeX fragment.
  17295. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  17296. sequence appearing also before point.
  17297. Even though the matchers for math are configurable, this function assumes
  17298. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  17299. delimiters are skipped when they have been removed by customization.
  17300. The return value is nil, or a cons cell with the delimiter and the
  17301. position of this delimiter.
  17302. This function does a reasonably good job, but can locally be fooled by
  17303. for example currency specifications. For example it will assume being in
  17304. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  17305. fragments that are properly closed, but during editing, we have to live
  17306. with the uncertainty caused by missing closing delimiters. This function
  17307. looks only before point, not after."
  17308. (catch 'exit
  17309. (let ((pos (point))
  17310. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  17311. (lim (progn
  17312. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  17313. (point)))
  17314. dd-on str (start 0) m re)
  17315. (goto-char pos)
  17316. (when dodollar
  17317. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  17318. re (nth 1 (assoc "$" org-latex-regexps)))
  17319. (while (string-match re str start)
  17320. (cond
  17321. ((= (match-end 0) (length str))
  17322. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  17323. ((= (match-end 0) (- (length str) 5))
  17324. (throw 'exit nil))
  17325. (t (setq start (match-end 0))))))
  17326. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  17327. (goto-char pos)
  17328. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  17329. (and (match-beginning 2) (throw 'exit nil))
  17330. ;; count $$
  17331. (while (re-search-backward "\\$\\$" lim t)
  17332. (setq dd-on (not dd-on)))
  17333. (goto-char pos)
  17334. (if dd-on (cons "$$" m))))))
  17335. (defun org-inside-latex-macro-p ()
  17336. "Is point inside a LaTeX macro or its arguments?"
  17337. (save-match-data
  17338. (org-in-regexp
  17339. "\\\\[a-zA-Z]+\\*?\\(\\(\\[[^][\n{}]*\\]\\)\\|\\({[^{}\n]*}\\)\\)*")))
  17340. (defvar org-latex-fragment-image-overlays nil
  17341. "List of overlays carrying the images of latex fragments.")
  17342. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  17343. (defun org-remove-latex-fragment-image-overlays (&optional beg end)
  17344. "Remove all overlays with LaTeX fragment images in current buffer.
  17345. When optional arguments BEG and END are non-nil, remove all
  17346. overlays between them instead. Return t when some overlays were
  17347. removed, nil otherwise."
  17348. (let (removedp)
  17349. (setq org-latex-fragment-image-overlays
  17350. (let ((beg (or beg (point-min)))
  17351. (end (or end (point-max))))
  17352. (org-remove-if
  17353. (lambda (o)
  17354. (and (>= (overlay-start o) beg)
  17355. (<= (overlay-end o) end)
  17356. (progn (delete-overlay o)
  17357. (or removedp (setq removedp t)))))
  17358. org-latex-fragment-image-overlays)))
  17359. removedp))
  17360. (define-obsolete-function-alias
  17361. 'org-preview-latex-fragment 'org-toggle-latex-fragment "24.4")
  17362. (defun org-toggle-latex-fragment (&optional arg)
  17363. "Preview the LaTeX fragment at point, or all locally or globally.
  17364. If the cursor is on a LaTeX fragment, create the image and overlay
  17365. it over the source code, if there is none. Remove it otherwise.
  17366. If there is no fragment at point, display all fragments in the
  17367. current section.
  17368. With prefix ARG, preview or clear image for all fragments in the
  17369. current subtree or in the whole buffer when used before the first
  17370. headline. With a double prefix ARG \\[universal-argument] \
  17371. \\[universal-argument] preview or clear images
  17372. for all fragments in the buffer."
  17373. (interactive "P")
  17374. (unless (buffer-file-name (buffer-base-buffer))
  17375. (user-error "Can't preview LaTeX fragment in a non-file buffer"))
  17376. (when (display-graphic-p)
  17377. (catch 'exit
  17378. (save-excursion
  17379. (let ((window-start (window-start)) msg)
  17380. (save-restriction
  17381. (cond
  17382. ((or (equal arg '(16))
  17383. (and (equal arg '(4))
  17384. (org-with-limited-levels (org-before-first-heading-p))))
  17385. (if (org-remove-latex-fragment-image-overlays)
  17386. (progn (message "LaTeX fragments images removed from buffer")
  17387. (throw 'exit nil))
  17388. (setq msg "Creating images for buffer...")))
  17389. ((equal arg '(4))
  17390. (org-with-limited-levels (org-back-to-heading t))
  17391. (let ((beg (point))
  17392. (end (progn (org-end-of-subtree t) (point))))
  17393. (if (org-remove-latex-fragment-image-overlays beg end)
  17394. (progn
  17395. (message "LaTeX fragment images removed from subtree")
  17396. (throw 'exit nil))
  17397. (setq msg "Creating images for subtree...")
  17398. (narrow-to-region beg end))))
  17399. ((let ((datum (org-element-context)))
  17400. (when (memq (org-element-type datum)
  17401. '(latex-environment latex-fragment))
  17402. (let* ((beg (org-element-property :begin datum))
  17403. (end (org-element-property :end datum)))
  17404. (if (org-remove-latex-fragment-image-overlays beg end)
  17405. (progn (message "LaTeX fragment image removed")
  17406. (throw 'exit nil))
  17407. (narrow-to-region beg end)
  17408. (setq msg "Creating image..."))))))
  17409. (t
  17410. (org-with-limited-levels
  17411. (let ((beg (if (org-at-heading-p) (line-beginning-position)
  17412. (outline-previous-heading)
  17413. (point)))
  17414. (end (progn (outline-next-heading) (point))))
  17415. (if (org-remove-latex-fragment-image-overlays beg end)
  17416. (progn
  17417. (message "LaTeX fragment images removed from section")
  17418. (throw 'exit nil))
  17419. (setq msg "Creating images for section...")
  17420. (narrow-to-region beg end))))))
  17421. (let ((file (buffer-file-name (buffer-base-buffer))))
  17422. (org-format-latex
  17423. (concat org-latex-preview-ltxpng-directory
  17424. (file-name-sans-extension (file-name-nondirectory file)))
  17425. ;; Emacs cannot overlay images from remote hosts.
  17426. ;; Create it in `temporary-file-directory' instead.
  17427. (if (file-remote-p file) temporary-file-directory
  17428. default-directory)
  17429. 'overlays msg 'forbuffer
  17430. org-latex-create-formula-image-program)))
  17431. ;; Work around a bug that doesn't restore window's start
  17432. ;; when widening back the buffer.
  17433. (set-window-start nil window-start)
  17434. (message (concat msg "done")))))))
  17435. (defun org-format-latex
  17436. (prefix &optional dir overlays msg forbuffer processing-type)
  17437. "Replace LaTeX fragments with links to an image, and produce images.
  17438. When optional argument OVERLAYS is non-nil, display the image on
  17439. top of the fragment instead of replacing it.
  17440. PROCESSING-TYPE is the conversion method to use, as a symbol.
  17441. Some of the options can be changed using the variable
  17442. `org-format-latex-options', which see."
  17443. (when (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  17444. (unless (eq processing-type 'verbatim)
  17445. (let* ((math-regexp "\\$\\|\\\\[([]\\|^[ \t]*\\\\begin{[A-Za-z0-9*]+}")
  17446. (cnt 0)
  17447. checkdir-flag)
  17448. (goto-char (point-min))
  17449. ;; Optimize overlay creation: (info "(elisp) Managing Overlays").
  17450. (when (and overlays (memq processing-type '(dvipng imagemagick)))
  17451. (overlay-recenter (point-max)))
  17452. (while (re-search-forward math-regexp nil t)
  17453. (unless (and overlays
  17454. (eq (get-char-property (point) 'org-overlay-type)
  17455. 'org-latex-overlay))
  17456. (let* ((context (org-element-context))
  17457. (type (org-element-type context)))
  17458. (when (memq type '(latex-environment latex-fragment))
  17459. (let ((block-type (eq type 'latex-environment))
  17460. (value (org-element-property :value context))
  17461. (beg (org-element-property :begin context))
  17462. (end (save-excursion
  17463. (goto-char (org-element-property :end context))
  17464. (skip-chars-backward " \r\t\n")
  17465. (point))))
  17466. (case processing-type
  17467. (mathjax
  17468. ;; Prepare for MathJax processing.
  17469. (if (eq (char-after beg) ?$)
  17470. (save-excursion
  17471. (delete-region beg end)
  17472. (insert "\\(" (substring value 1 -1) "\\)"))
  17473. (goto-char end)))
  17474. ((dvipng imagemagick)
  17475. ;; Process to an image.
  17476. (incf cnt)
  17477. (goto-char beg)
  17478. (let* ((face (face-at-point))
  17479. ;; Get the colors from the face at point.
  17480. (fg
  17481. (let ((color (plist-get org-format-latex-options
  17482. :foreground)))
  17483. (if (and forbuffer (eq color 'auto))
  17484. (face-attribute face :foreground nil 'default)
  17485. color)))
  17486. (bg
  17487. (let ((color (plist-get org-format-latex-options
  17488. :background)))
  17489. (if (and forbuffer (eq color 'auto))
  17490. (face-attribute face :background nil 'default)
  17491. color)))
  17492. (hash (sha1 (prin1-to-string
  17493. (list org-format-latex-header
  17494. org-latex-default-packages-alist
  17495. org-latex-packages-alist
  17496. org-format-latex-options
  17497. forbuffer value fg bg))))
  17498. (absprefix (expand-file-name prefix dir))
  17499. (linkfile (format "%s_%s.png" prefix hash))
  17500. (movefile (format "%s_%s.png" absprefix hash))
  17501. (sep (and block-type "\n\n"))
  17502. (link (concat sep "[[file:" linkfile "]]" sep))
  17503. (options
  17504. (org-combine-plists
  17505. org-format-latex-options
  17506. `(:foreground ,fg :background ,bg))))
  17507. (when msg (message msg cnt))
  17508. (unless checkdir-flag ; Ensure the directory exists.
  17509. (setq checkdir-flag t)
  17510. (let ((todir (file-name-directory absprefix)))
  17511. (unless (file-directory-p todir)
  17512. (make-directory todir t))))
  17513. (unless (file-exists-p movefile)
  17514. (org-create-formula-image
  17515. value movefile options forbuffer processing-type))
  17516. (if overlays
  17517. (progn
  17518. (dolist (o (overlays-in beg end))
  17519. (when (eq (overlay-get o 'org-overlay-type)
  17520. 'org-latex-overlay)
  17521. (delete-overlay o)))
  17522. (let ((ov (make-overlay beg end)))
  17523. (overlay-put ov
  17524. 'org-overlay-type
  17525. 'org-latex-overlay)
  17526. (overlay-put ov 'evaporate t)
  17527. (if (featurep 'xemacs)
  17528. (progn
  17529. (overlay-put ov 'invisible t)
  17530. (overlay-put
  17531. ov 'end-glyph
  17532. (make-glyph
  17533. (vector 'png :file movefile))))
  17534. (overlay-put
  17535. ov 'display
  17536. (list 'image
  17537. :type 'png
  17538. :file movefile
  17539. :ascent 'center)))
  17540. (push ov org-latex-fragment-image-overlays))
  17541. (goto-char end))
  17542. (delete-region beg end)
  17543. (insert
  17544. (org-add-props link
  17545. (list 'org-latex-src
  17546. (replace-regexp-in-string "\"" "" value)
  17547. 'org-latex-src-embed-type
  17548. (if block-type 'paragraph 'character)))))))
  17549. (mathml
  17550. ;; Process to MathML.
  17551. (unless (org-format-latex-mathml-available-p)
  17552. (user-error "LaTeX to MathML converter not configured"))
  17553. (incf cnt)
  17554. (when msg (message msg cnt))
  17555. (goto-char beg)
  17556. (delete-region beg end)
  17557. (insert (org-format-latex-as-mathml
  17558. value block-type prefix dir)))
  17559. (otherwise
  17560. (error "Unknown conversion type %s for LaTeX fragments"
  17561. processing-type)))))))))))
  17562. (defun org-create-math-formula (latex-frag &optional mathml-file)
  17563. "Convert LATEX-FRAG to MathML and store it in MATHML-FILE.
  17564. Use `org-latex-to-mathml-convert-command'. If the conversion is
  17565. sucessful, return the portion between \"<math...> </math>\"
  17566. elements otherwise return nil. When MATHML-FILE is specified,
  17567. write the results in to that file. When invoked as an
  17568. interactive command, prompt for LATEX-FRAG, with initial value
  17569. set to the current active region and echo the results for user
  17570. inspection."
  17571. (interactive (list (let ((frag (when (org-region-active-p)
  17572. (buffer-substring-no-properties
  17573. (region-beginning) (region-end)))))
  17574. (read-string "LaTeX Fragment: " frag nil frag))))
  17575. (unless latex-frag (user-error "Invalid LaTeX fragment"))
  17576. (let* ((tmp-in-file (file-relative-name
  17577. (make-temp-name (expand-file-name "ltxmathml-in"))))
  17578. (ignore (write-region latex-frag nil tmp-in-file))
  17579. (tmp-out-file (file-relative-name
  17580. (make-temp-name (expand-file-name "ltxmathml-out"))))
  17581. (cmd (format-spec
  17582. org-latex-to-mathml-convert-command
  17583. `((?j . ,(and org-latex-to-mathml-jar-file
  17584. (shell-quote-argument
  17585. (expand-file-name
  17586. org-latex-to-mathml-jar-file))))
  17587. (?I . ,(shell-quote-argument tmp-in-file))
  17588. (?i . ,latex-frag)
  17589. (?o . ,(shell-quote-argument tmp-out-file)))))
  17590. mathml shell-command-output)
  17591. (when (org-called-interactively-p 'any)
  17592. (unless (org-format-latex-mathml-available-p)
  17593. (user-error "LaTeX to MathML converter not configured")))
  17594. (message "Running %s" cmd)
  17595. (setq shell-command-output (shell-command-to-string cmd))
  17596. (setq mathml
  17597. (when (file-readable-p tmp-out-file)
  17598. (with-current-buffer (find-file-noselect tmp-out-file t)
  17599. (goto-char (point-min))
  17600. (when (re-search-forward
  17601. (concat
  17602. (regexp-quote
  17603. "<math xmlns=\"http://www.w3.org/1998/Math/MathML\"")
  17604. "[^>]*?>"
  17605. "\\(.\\|\n\\)*"
  17606. "</math>")
  17607. nil t)
  17608. (prog1 (match-string 0) (kill-buffer))))))
  17609. (cond
  17610. (mathml
  17611. (setq mathml
  17612. (concat "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" mathml))
  17613. (when mathml-file
  17614. (write-region mathml nil mathml-file))
  17615. (when (org-called-interactively-p 'any)
  17616. (message mathml)))
  17617. ((message "LaTeX to MathML conversion failed")
  17618. (message shell-command-output)))
  17619. (delete-file tmp-in-file)
  17620. (when (file-exists-p tmp-out-file)
  17621. (delete-file tmp-out-file))
  17622. mathml))
  17623. (defun org-format-latex-as-mathml (latex-frag latex-frag-type
  17624. prefix &optional dir)
  17625. "Use `org-create-math-formula' but check local cache first."
  17626. (let* ((absprefix (expand-file-name prefix dir))
  17627. (print-length nil) (print-level nil)
  17628. (formula-id (concat
  17629. "formula-"
  17630. (sha1
  17631. (prin1-to-string
  17632. (list latex-frag
  17633. org-latex-to-mathml-convert-command)))))
  17634. (formula-cache (format "%s-%s.mathml" absprefix formula-id))
  17635. (formula-cache-dir (file-name-directory formula-cache)))
  17636. (unless (file-directory-p formula-cache-dir)
  17637. (make-directory formula-cache-dir t))
  17638. (unless (file-exists-p formula-cache)
  17639. (org-create-math-formula latex-frag formula-cache))
  17640. (if (file-exists-p formula-cache)
  17641. ;; Successful conversion. Return the link to MathML file.
  17642. (org-add-props
  17643. (format "[[file:%s]]" (file-relative-name formula-cache dir))
  17644. (list 'org-latex-src (replace-regexp-in-string "\"" "" latex-frag)
  17645. 'org-latex-src-embed-type (if latex-frag-type
  17646. 'paragraph 'character)))
  17647. ;; Failed conversion. Return the LaTeX fragment verbatim
  17648. latex-frag)))
  17649. (defun org-create-formula-image (string tofile options buffer &optional type)
  17650. "Create an image from LaTeX source using dvipng or convert.
  17651. This function calls either `org-create-formula-image-with-dvipng'
  17652. or `org-create-formula-image-with-imagemagick' depending on the
  17653. value of `org-latex-create-formula-image-program' or on the value
  17654. of the optional TYPE variable.
  17655. Note: ultimately these two function should be combined as they
  17656. share a good deal of logic."
  17657. (org-check-external-command
  17658. "latex" "needed to convert LaTeX fragments to images")
  17659. (funcall
  17660. (case (or type org-latex-create-formula-image-program)
  17661. (dvipng
  17662. (org-check-external-command
  17663. "dvipng" "needed to convert LaTeX fragments to images")
  17664. #'org-create-formula-image-with-dvipng)
  17665. (imagemagick
  17666. (org-check-external-command
  17667. "convert" "you need to install imagemagick")
  17668. #'org-create-formula-image-with-imagemagick)
  17669. (t (error
  17670. "Invalid value of `org-latex-create-formula-image-program'")))
  17671. string tofile options buffer))
  17672. (declare-function org-export-get-backend "ox" (name))
  17673. (declare-function org-export--get-global-options "ox" (&optional backend))
  17674. (declare-function org-export--get-inbuffer-options "ox" (&optional backend))
  17675. (declare-function org-latex-guess-inputenc "ox-latex" (header))
  17676. (declare-function org-latex-guess-babel-language "ox-latex" (header info))
  17677. (defun org-create-formula--latex-header ()
  17678. "Return LaTeX header appropriate for previewing a LaTeX snippet."
  17679. (let ((info (org-combine-plists (org-export--get-global-options
  17680. (org-export-get-backend 'latex))
  17681. (org-export--get-inbuffer-options
  17682. (org-export-get-backend 'latex)))))
  17683. (org-latex-guess-babel-language
  17684. (org-latex-guess-inputenc
  17685. (org-splice-latex-header
  17686. org-format-latex-header
  17687. org-latex-default-packages-alist
  17688. org-latex-packages-alist t
  17689. (plist-get info :latex-header)))
  17690. info)))
  17691. (defun org--get-display-dpi ()
  17692. "Get the DPI of the display.
  17693. Assumes that the display has the same pixel width in the
  17694. horizontal and vertical directions."
  17695. (if (display-graphic-p)
  17696. (round (/ (display-pixel-height)
  17697. (/ (display-mm-height) 25.4)))
  17698. (error "Attempt to calculate the dpi of a non-graphic display")))
  17699. ;; This function borrows from Ganesh Swami's latex2png.el
  17700. (defun org-create-formula-image-with-dvipng (string tofile options buffer)
  17701. "This calls dvipng."
  17702. (require 'ox-latex)
  17703. (let* ((tmpdir (if (featurep 'xemacs)
  17704. (temp-directory)
  17705. temporary-file-directory))
  17706. (texfilebase (make-temp-name
  17707. (expand-file-name "orgtex" tmpdir)))
  17708. (texfile (concat texfilebase ".tex"))
  17709. (dvifile (concat texfilebase ".dvi"))
  17710. (pngfile (concat texfilebase ".png"))
  17711. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  17712. ;; This assumes that the display has the same pixel width in
  17713. ;; the horizontal and vertical directions
  17714. (dpi (number-to-string (* scale (if buffer (org--get-display-dpi) 120))))
  17715. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  17716. "Black"))
  17717. (bg (or (plist-get options (if buffer :background :html-background))
  17718. "Transparent")))
  17719. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground))
  17720. (unless (string= fg "Transparent") (setq fg (org-dvipng-color-format fg))))
  17721. (if (eq bg 'default) (setq bg (org-dvipng-color :background))
  17722. (unless (string= bg "Transparent") (setq bg (org-dvipng-color-format bg))))
  17723. (let ((latex-header (org-create-formula--latex-header)))
  17724. (with-temp-file texfile
  17725. (insert latex-header)
  17726. (insert "\n\\begin{document}\n" string "\n\\end{document}\n")))
  17727. (let ((dir default-directory))
  17728. (ignore-errors
  17729. (cd tmpdir)
  17730. (call-process "latex" nil nil nil texfile))
  17731. (cd dir))
  17732. (if (not (file-exists-p dvifile))
  17733. (progn (message "Failed to create dvi file from %s" texfile) nil)
  17734. (ignore-errors
  17735. (if (featurep 'xemacs)
  17736. (call-process "dvipng" nil nil nil
  17737. "-fg" fg "-bg" bg
  17738. "-T" "tight"
  17739. "-o" pngfile
  17740. dvifile)
  17741. (call-process "dvipng" nil nil nil
  17742. "-fg" fg "-bg" bg
  17743. "-D" dpi
  17744. ;;"-x" scale "-y" scale
  17745. "-T" "tight"
  17746. "-o" pngfile
  17747. dvifile)))
  17748. (if (not (file-exists-p pngfile))
  17749. (if org-format-latex-signal-error
  17750. (error "Failed to create png file from %s" texfile)
  17751. (message "Failed to create png file from %s" texfile)
  17752. nil)
  17753. ;; Use the requested file name and clean up
  17754. (copy-file pngfile tofile 'replace)
  17755. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png" ".out") do
  17756. (if (file-exists-p (concat texfilebase e))
  17757. (delete-file (concat texfilebase e))))
  17758. pngfile))))
  17759. (declare-function org-latex-compile "ox-latex" (texfile &optional snippet))
  17760. (defun org-create-formula-image-with-imagemagick (string tofile options buffer)
  17761. "This calls convert, which is included into imagemagick."
  17762. (require 'ox-latex)
  17763. (let* ((tmpdir (if (featurep 'xemacs)
  17764. (temp-directory)
  17765. temporary-file-directory))
  17766. (texfilebase (make-temp-name
  17767. (expand-file-name "orgtex" tmpdir)))
  17768. (texfile (concat texfilebase ".tex"))
  17769. (pdffile (concat texfilebase ".pdf"))
  17770. (pngfile (concat texfilebase ".png"))
  17771. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  17772. (dpi (number-to-string (* scale (if buffer (org--get-display-dpi) 120))))
  17773. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  17774. "black"))
  17775. (bg (or (plist-get options (if buffer :background :html-background))
  17776. "white")))
  17777. (if (eq fg 'default) (setq fg (org-latex-color :foreground))
  17778. (setq fg (org-latex-color-format fg)))
  17779. (if (eq bg 'default) (setq bg (org-latex-color :background))
  17780. (setq bg (org-latex-color-format
  17781. (if (string= bg "Transparent") "white" bg))))
  17782. (let ((latex-header (org-create-formula--latex-header)))
  17783. (with-temp-file texfile
  17784. (insert latex-header)
  17785. (insert "\n\\begin{document}\n"
  17786. "\\definecolor{fg}{rgb}{" fg "}\n"
  17787. "\\definecolor{bg}{rgb}{" bg "}\n"
  17788. "\n\\pagecolor{bg}\n"
  17789. "\n{\\color{fg}\n"
  17790. string
  17791. "\n}\n"
  17792. "\n\\end{document}\n")))
  17793. (org-latex-compile texfile t)
  17794. (if (not (file-exists-p pdffile))
  17795. (progn (message "Failed to create pdf file from %s" texfile) nil)
  17796. (ignore-errors
  17797. (if (featurep 'xemacs)
  17798. (call-process "convert" nil nil nil
  17799. "-density" "96"
  17800. "-trim"
  17801. "-antialias"
  17802. pdffile
  17803. "-quality" "100"
  17804. ;; "-sharpen" "0x1.0"
  17805. pngfile)
  17806. (call-process "convert" nil nil nil
  17807. "-density" dpi
  17808. "-trim"
  17809. "-antialias"
  17810. pdffile
  17811. "-quality" "100"
  17812. ;; "-sharpen" "0x1.0"
  17813. pngfile)))
  17814. (if (not (file-exists-p pngfile))
  17815. (if org-format-latex-signal-error
  17816. (error "Failed to create png file from %s" texfile)
  17817. (message "Failed to create png file from %s" texfile)
  17818. nil)
  17819. ;; Use the requested file name and clean up
  17820. (copy-file pngfile tofile 'replace)
  17821. (loop for e in '(".pdf" ".tex" ".aux" ".log" ".png") do
  17822. (if (file-exists-p (concat texfilebase e))
  17823. (delete-file (concat texfilebase e))))
  17824. pngfile))))
  17825. (defun org-splice-latex-header (tpl def-pkg pkg snippets-p &optional extra)
  17826. "Fill a LaTeX header template TPL.
  17827. In the template, the following place holders will be recognized:
  17828. [DEFAULT-PACKAGES] \\usepackage statements for DEF-PKG
  17829. [NO-DEFAULT-PACKAGES] do not include DEF-PKG
  17830. [PACKAGES] \\usepackage statements for PKG
  17831. [NO-PACKAGES] do not include PKG
  17832. [EXTRA] the string EXTRA
  17833. [NO-EXTRA] do not include EXTRA
  17834. For backward compatibility, if both the positive and the negative place
  17835. holder is missing, the positive one (without the \"NO-\") will be
  17836. assumed to be present at the end of the template.
  17837. DEF-PKG and PKG are assumed to be alists of options/packagename lists.
  17838. EXTRA is a string.
  17839. SNIPPETS-P indicates if this is run to create snippet images for HTML."
  17840. (let (rpl (end ""))
  17841. (if (string-match "^[ \t]*\\[\\(NO-\\)?DEFAULT-PACKAGES\\][ \t]*\n?" tpl)
  17842. (setq rpl (if (or (match-end 1) (not def-pkg))
  17843. "" (org-latex-packages-to-string def-pkg snippets-p t))
  17844. tpl (replace-match rpl t t tpl))
  17845. (if def-pkg (setq end (org-latex-packages-to-string def-pkg snippets-p))))
  17846. (if (string-match "\\[\\(NO-\\)?PACKAGES\\][ \t]*\n?" tpl)
  17847. (setq rpl (if (or (match-end 1) (not pkg))
  17848. "" (org-latex-packages-to-string pkg snippets-p t))
  17849. tpl (replace-match rpl t t tpl))
  17850. (if pkg (setq end
  17851. (concat end "\n"
  17852. (org-latex-packages-to-string pkg snippets-p)))))
  17853. (if (string-match "\\[\\(NO-\\)?EXTRA\\][ \t]*\n?" tpl)
  17854. (setq rpl (if (or (match-end 1) (not extra))
  17855. "" (concat extra "\n"))
  17856. tpl (replace-match rpl t t tpl))
  17857. (if (and extra (string-match "\\S-" extra))
  17858. (setq end (concat end "\n" extra))))
  17859. (if (string-match "\\S-" end)
  17860. (concat tpl "\n" end)
  17861. tpl)))
  17862. (defun org-latex-packages-to-string (pkg &optional snippets-p newline)
  17863. "Turn an alist of packages into a string with the \\usepackage macros."
  17864. (setq pkg (mapconcat (lambda(p)
  17865. (cond
  17866. ((stringp p) p)
  17867. ((and snippets-p (>= (length p) 3) (not (nth 2 p)))
  17868. (format "%% Package %s omitted" (cadr p)))
  17869. ((equal "" (car p))
  17870. (format "\\usepackage{%s}" (cadr p)))
  17871. (t
  17872. (format "\\usepackage[%s]{%s}"
  17873. (car p) (cadr p)))))
  17874. pkg
  17875. "\n"))
  17876. (if newline (concat pkg "\n") pkg))
  17877. (defun org-dvipng-color (attr)
  17878. "Return a RGB color specification for dvipng."
  17879. (apply 'format "rgb %s %s %s"
  17880. (mapcar 'org-normalize-color
  17881. (if (featurep 'xemacs)
  17882. (color-rgb-components
  17883. (face-property 'default
  17884. (cond ((eq attr :foreground) 'foreground)
  17885. ((eq attr :background) 'background))))
  17886. (color-values (face-attribute 'default attr nil))))))
  17887. (defun org-dvipng-color-format (color-name)
  17888. "Convert COLOR-NAME to a RGB color value for dvipng."
  17889. (apply 'format "rgb %s %s %s"
  17890. (mapcar 'org-normalize-color
  17891. (color-values color-name))))
  17892. (defun org-latex-color (attr)
  17893. "Return a RGB color for the LaTeX color package."
  17894. (apply 'format "%s,%s,%s"
  17895. (mapcar 'org-normalize-color
  17896. (if (featurep 'xemacs)
  17897. (color-rgb-components
  17898. (face-property 'default
  17899. (cond ((eq attr :foreground) 'foreground)
  17900. ((eq attr :background) 'background))))
  17901. (color-values (face-attribute 'default attr nil))))))
  17902. (defun org-latex-color-format (color-name)
  17903. "Convert COLOR-NAME to a RGB color value."
  17904. (apply 'format "%s,%s,%s"
  17905. (mapcar 'org-normalize-color
  17906. (color-values color-name))))
  17907. (defun org-normalize-color (value)
  17908. "Return string to be used as color value for an RGB component."
  17909. (format "%g" (/ value 65535.0)))
  17910. ;; Image display
  17911. (defvar org-inline-image-overlays nil)
  17912. (make-variable-buffer-local 'org-inline-image-overlays)
  17913. (defun org-toggle-inline-images (&optional include-linked)
  17914. "Toggle the display of inline images.
  17915. INCLUDE-LINKED is passed to `org-display-inline-images'."
  17916. (interactive "P")
  17917. (if org-inline-image-overlays
  17918. (progn
  17919. (org-remove-inline-images)
  17920. (when (org-called-interactively-p 'interactive)
  17921. (message "Inline image display turned off")))
  17922. (org-display-inline-images include-linked)
  17923. (when (org-called-interactively-p 'interactive)
  17924. (message (if org-inline-image-overlays
  17925. (format "%d images displayed inline"
  17926. (length org-inline-image-overlays))
  17927. "No images to display inline")))))
  17928. (defun org-redisplay-inline-images ()
  17929. "Refresh the display of inline images."
  17930. (interactive)
  17931. (if (not org-inline-image-overlays)
  17932. (org-toggle-inline-images)
  17933. (org-toggle-inline-images)
  17934. (org-toggle-inline-images)))
  17935. (defun org-display-inline-images (&optional include-linked refresh beg end)
  17936. "Display inline images.
  17937. An inline image is a link which follows either of these
  17938. conventions:
  17939. 1. Its path is a file with an extension matching return value
  17940. from `image-file-name-regexp' and it has no contents.
  17941. 2. Its description consists in a single link of the previous
  17942. type.
  17943. When optional argument INCLUDE-LINKED is non-nil, also links with
  17944. a text description part will be inlined. This can be nice for
  17945. a quick look at those images, but it does not reflect what
  17946. exported files will look like.
  17947. When optional argument REFRESH is non-nil, refresh existing
  17948. images between BEG and END. This will create new image displays
  17949. only if necessary. BEG and END default to the buffer
  17950. boundaries."
  17951. (interactive "P")
  17952. (when (display-graphic-p)
  17953. (unless refresh
  17954. (org-remove-inline-images)
  17955. (when (fboundp 'clear-image-cache) (clear-image-cache)))
  17956. (org-with-wide-buffer
  17957. (goto-char (or beg (point-min)))
  17958. (let ((case-fold-search t)
  17959. (file-extension-re (org-image-file-name-regexp)))
  17960. (while (re-search-forward "[][]\\[\\(?:file\\|[./~]\\)" end t)
  17961. (let ((link (save-match-data (org-element-context))))
  17962. ;; Check if we're at an inline image.
  17963. (when (and (equal (org-element-property :type link) "file")
  17964. (or include-linked
  17965. (not (org-element-property :contents-begin link)))
  17966. (let ((parent (org-element-property :parent link)))
  17967. (or (not (eq (org-element-type parent) 'link))
  17968. (not (cdr (org-element-contents parent)))))
  17969. (org-string-match-p file-extension-re
  17970. (org-element-property :path link)))
  17971. (let ((file (expand-file-name
  17972. (org-link-unescape
  17973. (org-element-property :path link)))))
  17974. (when (file-exists-p file)
  17975. (let ((width
  17976. ;; Apply `org-image-actual-width' specifications.
  17977. (cond
  17978. ((not (image-type-available-p 'imagemagick)) nil)
  17979. ((eq org-image-actual-width t) nil)
  17980. ((listp org-image-actual-width)
  17981. (or
  17982. ;; First try to find a width among
  17983. ;; attributes associated to the paragraph
  17984. ;; containing link.
  17985. (let ((paragraph
  17986. (let ((e link))
  17987. (while (and (setq e (org-element-property
  17988. :parent e))
  17989. (not (eq (org-element-type e)
  17990. 'paragraph))))
  17991. e)))
  17992. (when paragraph
  17993. (save-excursion
  17994. (goto-char (org-element-property :begin paragraph))
  17995. (when
  17996. (re-search-forward
  17997. "^[ \t]*#\\+attr_.*?: +.*?:width +\\(\\S-+\\)"
  17998. (org-element-property
  17999. :post-affiliated paragraph)
  18000. t)
  18001. (string-to-number (match-string 1))))))
  18002. ;; Otherwise, fall-back to provided number.
  18003. (car org-image-actual-width)))
  18004. ((numberp org-image-actual-width)
  18005. org-image-actual-width)))
  18006. (old (get-char-property-and-overlay
  18007. (org-element-property :begin link)
  18008. 'org-image-overlay)))
  18009. (if (and (car-safe old) refresh)
  18010. (image-refresh (overlay-get (cdr old) 'display))
  18011. (let ((image (create-image file
  18012. (and width 'imagemagick)
  18013. nil
  18014. :width width)))
  18015. (when image
  18016. (let* ((link
  18017. ;; If inline image is the description
  18018. ;; of another link, be sure to
  18019. ;; consider the latter as the one to
  18020. ;; apply the overlay on.
  18021. (let ((parent
  18022. (org-element-property :parent link)))
  18023. (if (eq (org-element-type parent) 'link)
  18024. parent
  18025. link)))
  18026. (ov (make-overlay
  18027. (org-element-property :begin link)
  18028. (progn
  18029. (goto-char
  18030. (org-element-property :end link))
  18031. (skip-chars-backward " \t")
  18032. (point)))))
  18033. (overlay-put ov 'display image)
  18034. (overlay-put ov 'face 'default)
  18035. (overlay-put ov 'org-image-overlay t)
  18036. (overlay-put
  18037. ov 'modification-hooks
  18038. (list 'org-display-inline-remove-overlay))
  18039. (push ov org-inline-image-overlays)))))))))))))))
  18040. (define-obsolete-function-alias
  18041. 'org-display-inline-modification-hook 'org-display-inline-remove-overlay "24.3")
  18042. (defun org-display-inline-remove-overlay (ov after beg end &optional len)
  18043. "Remove inline-display overlay if a corresponding region is modified."
  18044. (let ((inhibit-modification-hooks t))
  18045. (when (and ov after)
  18046. (delete ov org-inline-image-overlays)
  18047. (delete-overlay ov))))
  18048. (defun org-remove-inline-images ()
  18049. "Remove inline display of images."
  18050. (interactive)
  18051. (mapc 'delete-overlay org-inline-image-overlays)
  18052. (setq org-inline-image-overlays nil))
  18053. ;;;; Key bindings
  18054. ;; Outline functions from `outline-mode-prefix-map'
  18055. ;; that can be remapped in Org:
  18056. (define-key org-mode-map [remap outline-mark-subtree] 'org-mark-subtree)
  18057. (define-key org-mode-map [remap show-subtree] 'org-show-subtree)
  18058. (define-key org-mode-map [remap outline-forward-same-level]
  18059. 'org-forward-heading-same-level)
  18060. (define-key org-mode-map [remap outline-backward-same-level]
  18061. 'org-backward-heading-same-level)
  18062. (define-key org-mode-map [remap show-branches]
  18063. 'org-kill-note-or-show-branches)
  18064. (define-key org-mode-map [remap outline-promote] 'org-promote-subtree)
  18065. (define-key org-mode-map [remap outline-demote] 'org-demote-subtree)
  18066. (define-key org-mode-map [remap outline-insert-heading] 'org-ctrl-c-ret)
  18067. (define-key org-mode-map [remap outline-next-visible-heading]
  18068. 'org-next-visible-heading)
  18069. (define-key org-mode-map [remap outline-previous-visible-heading]
  18070. 'org-previous-visible-heading)
  18071. (define-key org-mode-map [remap show-children] 'org-show-children)
  18072. ;; Outline functions from `outline-mode-prefix-map' that can not
  18073. ;; be remapped in Org:
  18074. ;; - the column "key binding" shows whether the Outline function is still
  18075. ;; available in Org mode on the same key that it has been bound to in
  18076. ;; Outline mode:
  18077. ;; - "overridden": key used for a different functionality in Org mode
  18078. ;; - else: key still bound to the same Outline function in Org mode
  18079. ;; | Outline function | key binding | Org replacement |
  18080. ;; |------------------------------------+-------------+--------------------------|
  18081. ;; | `outline-up-heading' | `C-c C-u' | still same function |
  18082. ;; | `outline-move-subtree-up' | overridden | better: org-shiftup |
  18083. ;; | `outline-move-subtree-down' | overridden | better: org-shiftdown |
  18084. ;; | `show-entry' | overridden | no replacement |
  18085. ;; | `show-branches' | `C-c C-k' | still same function |
  18086. ;; | `show-subtree' | overridden | visibility cycling |
  18087. ;; | `show-all' | overridden | no replacement |
  18088. ;; | `hide-subtree' | overridden | visibility cycling |
  18089. ;; | `hide-body' | overridden | no replacement |
  18090. ;; | `hide-entry' | overridden | visibility cycling |
  18091. ;; | `hide-leaves' | overridden | no replacement |
  18092. ;; | `hide-sublevels' | overridden | no replacement |
  18093. ;; | `hide-other' | overridden | no replacement |
  18094. ;; Make `C-c C-x' a prefix key
  18095. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  18096. ;; TAB key with modifiers
  18097. (org-defkey org-mode-map "\C-i" 'org-cycle)
  18098. (org-defkey org-mode-map [(tab)] 'org-cycle)
  18099. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  18100. (org-defkey org-mode-map "\M-\t" #'pcomplete)
  18101. ;; The following line is necessary under Suse GNU/Linux
  18102. (unless (featurep 'xemacs)
  18103. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  18104. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  18105. (define-key org-mode-map [backtab] 'org-shifttab)
  18106. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  18107. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  18108. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  18109. ;; Cursor keys with modifiers
  18110. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  18111. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  18112. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  18113. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  18114. (org-defkey org-mode-map [(control meta shift right)] 'org-increase-number-at-point)
  18115. (org-defkey org-mode-map [(control meta shift left)] 'org-decrease-number-at-point)
  18116. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  18117. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  18118. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  18119. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  18120. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  18121. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  18122. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  18123. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  18124. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  18125. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  18126. (org-defkey org-mode-map [(control shift up)] 'org-shiftcontrolup)
  18127. (org-defkey org-mode-map [(control shift down)] 'org-shiftcontroldown)
  18128. ;; Babel keys
  18129. (define-key org-mode-map org-babel-key-prefix org-babel-map)
  18130. (mapc (lambda (pair)
  18131. (define-key org-babel-map (car pair) (cdr pair)))
  18132. org-babel-key-bindings)
  18133. ;;; Extra keys for tty access.
  18134. ;; We only set them when really needed because otherwise the
  18135. ;; menus don't show the simple keys
  18136. (when (or org-use-extra-keys
  18137. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  18138. (not window-system))
  18139. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  18140. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  18141. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  18142. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  18143. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  18144. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  18145. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  18146. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  18147. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  18148. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  18149. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  18150. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  18151. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  18152. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  18153. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  18154. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  18155. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  18156. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  18157. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  18158. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  18159. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  18160. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft)
  18161. (org-defkey org-mode-map [?\e (tab)] #'pcomplete)
  18162. (org-defkey org-mode-map [?\e (shift return)] 'org-insert-todo-heading)
  18163. (org-defkey org-mode-map [?\e (shift left)] 'org-shiftmetaleft)
  18164. (org-defkey org-mode-map [?\e (shift right)] 'org-shiftmetaright)
  18165. (org-defkey org-mode-map [?\e (shift up)] 'org-shiftmetaup)
  18166. (org-defkey org-mode-map [?\e (shift down)] 'org-shiftmetadown))
  18167. ;; All the other keys
  18168. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  18169. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  18170. (if (boundp 'narrow-map)
  18171. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  18172. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  18173. (if (boundp 'narrow-map)
  18174. (org-defkey narrow-map "b" 'org-narrow-to-block)
  18175. (org-defkey org-mode-map "\C-xnb" 'org-narrow-to-block))
  18176. (if (boundp 'narrow-map)
  18177. (org-defkey narrow-map "e" 'org-narrow-to-element)
  18178. (org-defkey org-mode-map "\C-xne" 'org-narrow-to-element))
  18179. (org-defkey org-mode-map "\C-\M-t" 'org-transpose-element)
  18180. (org-defkey org-mode-map "\M-}" 'org-forward-element)
  18181. (org-defkey org-mode-map "\M-{" 'org-backward-element)
  18182. (org-defkey org-mode-map "\C-c\C-^" 'org-up-element)
  18183. (org-defkey org-mode-map "\C-c\C-_" 'org-down-element)
  18184. (org-defkey org-mode-map "\C-c\C-f" 'org-forward-heading-same-level)
  18185. (org-defkey org-mode-map "\C-c\C-b" 'org-backward-heading-same-level)
  18186. (org-defkey org-mode-map "\C-c\M-f" 'org-next-block)
  18187. (org-defkey org-mode-map "\C-c\M-b" 'org-previous-block)
  18188. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  18189. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-archive-subtree)
  18190. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-archive-subtree-default)
  18191. (org-defkey org-mode-map "\C-c\C-xd" 'org-insert-drawer)
  18192. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  18193. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  18194. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  18195. (org-defkey org-mode-map "\C-c\C-xq" 'org-toggle-tags-groups)
  18196. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  18197. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  18198. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  18199. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  18200. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  18201. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  18202. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  18203. (org-defkey org-mode-map "\C-c\M-w" 'org-copy)
  18204. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  18205. (org-defkey org-mode-map "\C-c\\" 'org-match-sparse-tree) ; Minor-mode res.
  18206. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  18207. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  18208. (org-defkey org-mode-map "\C-c\C-xc" 'org-clone-subtree-with-time-shift)
  18209. (org-defkey org-mode-map "\C-c\C-xv" 'org-copy-visible)
  18210. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  18211. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  18212. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  18213. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  18214. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  18215. (org-defkey org-mode-map "\C-c\M-l" 'org-insert-last-stored-link)
  18216. (org-defkey org-mode-map "\C-c\C-\M-l" 'org-insert-all-links)
  18217. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  18218. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  18219. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  18220. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  18221. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  18222. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  18223. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  18224. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  18225. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  18226. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  18227. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  18228. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  18229. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  18230. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  18231. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  18232. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  18233. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  18234. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  18235. (org-defkey org-mode-map "\C-c^" 'org-sort)
  18236. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  18237. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  18238. (org-defkey org-mode-map "\C-c#" 'org-update-statistics-cookies)
  18239. (org-defkey org-mode-map [remap open-line] 'org-open-line)
  18240. (org-defkey org-mode-map [remap comment-dwim] 'org-comment-dwim)
  18241. (org-defkey org-mode-map [remap forward-paragraph] 'org-forward-paragraph)
  18242. (org-defkey org-mode-map [remap backward-paragraph] 'org-backward-paragraph)
  18243. (org-defkey org-mode-map "\M-^" 'org-delete-indentation)
  18244. (org-defkey org-mode-map "\C-m" 'org-return)
  18245. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  18246. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  18247. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  18248. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  18249. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  18250. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  18251. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  18252. (org-defkey org-mode-map "\C-c\"a" 'orgtbl-ascii-plot)
  18253. (org-defkey org-mode-map "\C-c\"g" 'org-plot/gnuplot)
  18254. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  18255. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  18256. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  18257. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  18258. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  18259. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  18260. (org-defkey org-mode-map "\C-c\C-e" 'org-export-dispatch)
  18261. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width)
  18262. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  18263. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  18264. (org-defkey org-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
  18265. (org-defkey org-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
  18266. (org-defkey org-mode-map "\C-c@" 'org-mark-subtree)
  18267. (org-defkey org-mode-map "\M-h" 'org-mark-element)
  18268. (org-defkey org-mode-map [?\C-c (control ?*)] 'org-list-make-subtree)
  18269. ;;(org-defkey org-mode-map [?\C-c (control ?-)] 'org-list-make-list-from-subtree)
  18270. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  18271. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  18272. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  18273. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  18274. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  18275. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-in-last)
  18276. (org-defkey org-mode-map "\C-c\C-x\C-z" 'org-resolve-clocks)
  18277. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  18278. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  18279. (org-defkey org-mode-map "\C-c\C-x\C-q" 'org-clock-cancel)
  18280. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  18281. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  18282. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  18283. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-toggle-latex-fragment)
  18284. (org-defkey org-mode-map "\C-c\C-x\C-v" 'org-toggle-inline-images)
  18285. (org-defkey org-mode-map "\C-c\C-x\C-\M-v" 'org-redisplay-inline-images)
  18286. (org-defkey org-mode-map "\C-c\C-x\\" 'org-toggle-pretty-entities)
  18287. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  18288. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  18289. (org-defkey org-mode-map "\C-c\C-xP" 'org-set-property-and-value)
  18290. (org-defkey org-mode-map "\C-c\C-xe" 'org-set-effort)
  18291. (org-defkey org-mode-map "\C-c\C-xE" 'org-inc-effort)
  18292. (org-defkey org-mode-map "\C-c\C-xo" 'org-toggle-ordered-property)
  18293. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  18294. (org-defkey org-mode-map [(control ?c) (control ?x) ?\;] 'org-timer-set-timer)
  18295. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  18296. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  18297. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  18298. (org-defkey org-mode-map "\C-c\C-x_" 'org-timer-stop)
  18299. (org-defkey org-mode-map "\C-c\C-x," 'org-timer-pause-or-continue)
  18300. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  18301. (define-key org-mode-map "\C-c\C-x!" 'org-reload)
  18302. (define-key org-mode-map "\C-c\C-xg" 'org-feed-update-all)
  18303. (define-key org-mode-map "\C-c\C-xG" 'org-feed-goto-inbox)
  18304. (define-key org-mode-map "\C-c\C-x[" 'org-reftex-citation)
  18305. (when (featurep 'xemacs)
  18306. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  18307. (defconst org-speed-commands-default
  18308. '(
  18309. ("Outline Navigation")
  18310. ("n" . (org-speed-move-safe 'org-next-visible-heading))
  18311. ("p" . (org-speed-move-safe 'org-previous-visible-heading))
  18312. ("f" . (org-speed-move-safe 'org-forward-heading-same-level))
  18313. ("b" . (org-speed-move-safe 'org-backward-heading-same-level))
  18314. ("F" . org-next-block)
  18315. ("B" . org-previous-block)
  18316. ("u" . (org-speed-move-safe 'outline-up-heading))
  18317. ("j" . org-goto)
  18318. ("g" . (org-refile t))
  18319. ("Outline Visibility")
  18320. ("c" . org-cycle)
  18321. ("C" . org-shifttab)
  18322. (" " . org-display-outline-path)
  18323. ("s" . org-narrow-to-subtree)
  18324. ("=" . org-columns)
  18325. ("Outline Structure Editing")
  18326. ("U" . org-metaup)
  18327. ("D" . org-metadown)
  18328. ("r" . org-metaright)
  18329. ("l" . org-metaleft)
  18330. ("R" . org-shiftmetaright)
  18331. ("L" . org-shiftmetaleft)
  18332. ("i" . (progn (forward-char 1) (call-interactively
  18333. 'org-insert-heading-respect-content)))
  18334. ("^" . org-sort)
  18335. ("w" . org-refile)
  18336. ("a" . org-archive-subtree-default-with-confirmation)
  18337. ("@" . org-mark-subtree)
  18338. ("#" . org-toggle-comment)
  18339. ("Clock Commands")
  18340. ("I" . org-clock-in)
  18341. ("O" . org-clock-out)
  18342. ("Meta Data Editing")
  18343. ("t" . org-todo)
  18344. ("," . (org-priority))
  18345. ("0" . (org-priority ?\ ))
  18346. ("1" . (org-priority ?A))
  18347. ("2" . (org-priority ?B))
  18348. ("3" . (org-priority ?C))
  18349. (":" . org-set-tags-command)
  18350. ("e" . org-set-effort)
  18351. ("E" . org-inc-effort)
  18352. ("W" . (lambda(m) (interactive "sMinutes before warning: ")
  18353. (org-entry-put (point) "APPT_WARNTIME" m)))
  18354. ("Agenda Views etc")
  18355. ("v" . org-agenda)
  18356. ("/" . org-sparse-tree)
  18357. ("Misc")
  18358. ("o" . org-open-at-point)
  18359. ("?" . org-speed-command-help)
  18360. ("<" . (org-agenda-set-restriction-lock 'subtree))
  18361. (">" . (org-agenda-remove-restriction-lock))
  18362. )
  18363. "The default speed commands.")
  18364. (defun org-print-speed-command (e)
  18365. (if (> (length (car e)) 1)
  18366. (progn
  18367. (princ "\n")
  18368. (princ (car e))
  18369. (princ "\n")
  18370. (princ (make-string (length (car e)) ?-))
  18371. (princ "\n"))
  18372. (princ (car e))
  18373. (princ " ")
  18374. (if (symbolp (cdr e))
  18375. (princ (symbol-name (cdr e)))
  18376. (prin1 (cdr e)))
  18377. (princ "\n")))
  18378. (defun org-speed-command-help ()
  18379. "Show the available speed commands."
  18380. (interactive)
  18381. (if (not org-use-speed-commands)
  18382. (user-error "Speed commands are not activated, customize `org-use-speed-commands'")
  18383. (with-output-to-temp-buffer "*Help*"
  18384. (princ "User-defined Speed commands\n===========================\n")
  18385. (mapc 'org-print-speed-command org-speed-commands-user)
  18386. (princ "\n")
  18387. (princ "Built-in Speed commands\n=======================\n")
  18388. (mapc 'org-print-speed-command org-speed-commands-default))
  18389. (with-current-buffer "*Help*"
  18390. (setq truncate-lines t))))
  18391. (defun org-speed-move-safe (cmd)
  18392. "Execute CMD, but make sure that the cursor always ends up in a headline.
  18393. If not, return to the original position and throw an error."
  18394. (interactive)
  18395. (let ((pos (point)))
  18396. (call-interactively cmd)
  18397. (unless (and (bolp) (org-at-heading-p))
  18398. (goto-char pos)
  18399. (error "Boundary reached while executing %s" cmd))))
  18400. (defvar org-self-insert-command-undo-counter 0)
  18401. (defvar org-table-auto-blank-field) ; defined in org-table.el
  18402. (defvar org-speed-command nil)
  18403. (define-obsolete-function-alias
  18404. 'org-speed-command-default-hook 'org-speed-command-activate "24.3")
  18405. (defun org-speed-command-activate (keys)
  18406. "Hook for activating single-letter speed commands.
  18407. `org-speed-commands-default' specifies a minimal command set.
  18408. Use `org-speed-commands-user' for further customization."
  18409. (when (or (and (bolp) (looking-at org-outline-regexp))
  18410. (and (functionp org-use-speed-commands)
  18411. (funcall org-use-speed-commands)))
  18412. (cdr (assoc keys (append org-speed-commands-user
  18413. org-speed-commands-default)))))
  18414. (define-obsolete-function-alias
  18415. 'org-babel-speed-command-hook 'org-babel-speed-command-activate "24.3")
  18416. (defun org-babel-speed-command-activate (keys)
  18417. "Hook for activating single-letter code block commands."
  18418. (when (and (bolp) (looking-at org-babel-src-block-regexp))
  18419. (cdr (assoc keys org-babel-key-bindings))))
  18420. (defcustom org-speed-command-hook
  18421. '(org-speed-command-default-hook org-babel-speed-command-hook)
  18422. "Hook for activating speed commands at strategic locations.
  18423. Hook functions are called in sequence until a valid handler is
  18424. found.
  18425. Each hook takes a single argument, a user-pressed command key
  18426. which is also a `self-insert-command' from the global map.
  18427. Within the hook, examine the cursor position and the command key
  18428. and return nil or a valid handler as appropriate. Handler could
  18429. be one of an interactive command, a function, or a form.
  18430. Set `org-use-speed-commands' to non-nil value to enable this
  18431. hook. The default setting is `org-speed-command-activate'."
  18432. :group 'org-structure
  18433. :version "24.1"
  18434. :type 'hook)
  18435. (defun org-self-insert-command (N)
  18436. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  18437. If the cursor is in a table looking at whitespace, the whitespace is
  18438. overwritten, and the table is not marked as requiring realignment."
  18439. (interactive "p")
  18440. (org-check-before-invisible-edit 'insert)
  18441. (cond
  18442. ((and org-use-speed-commands
  18443. (let ((kv (this-command-keys-vector)))
  18444. (setq org-speed-command
  18445. (run-hook-with-args-until-success
  18446. 'org-speed-command-hook
  18447. (make-string 1 (aref kv (1- (length kv))))))))
  18448. (cond
  18449. ((commandp org-speed-command)
  18450. (setq this-command org-speed-command)
  18451. (call-interactively org-speed-command))
  18452. ((functionp org-speed-command)
  18453. (funcall org-speed-command))
  18454. ((and org-speed-command (listp org-speed-command))
  18455. (eval org-speed-command))
  18456. (t (let (org-use-speed-commands)
  18457. (call-interactively 'org-self-insert-command)))))
  18458. ((and
  18459. (org-table-p)
  18460. (progn
  18461. ;; check if we blank the field, and if that triggers align
  18462. (and (featurep 'org-table) org-table-auto-blank-field
  18463. (memq last-command
  18464. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
  18465. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  18466. ;; got extra space, this field does not determine column width
  18467. (let (org-table-may-need-update) (org-table-blank-field))
  18468. ;; no extra space, this field may determine column width
  18469. (org-table-blank-field)))
  18470. t)
  18471. (eq N 1)
  18472. (looking-at "[^|\n]* |"))
  18473. (let (org-table-may-need-update)
  18474. (goto-char (1- (match-end 0)))
  18475. (backward-delete-char 1)
  18476. (goto-char (match-beginning 0))
  18477. (self-insert-command N)))
  18478. (t
  18479. (setq org-table-may-need-update t)
  18480. (self-insert-command N)
  18481. (org-fix-tags-on-the-fly)
  18482. (if org-self-insert-cluster-for-undo
  18483. (if (not (eq last-command 'org-self-insert-command))
  18484. (setq org-self-insert-command-undo-counter 1)
  18485. (if (>= org-self-insert-command-undo-counter 20)
  18486. (setq org-self-insert-command-undo-counter 1)
  18487. (and (> org-self-insert-command-undo-counter 0)
  18488. buffer-undo-list (listp buffer-undo-list)
  18489. (not (cadr buffer-undo-list)) ; remove nil entry
  18490. (setcdr buffer-undo-list (cddr buffer-undo-list)))
  18491. (setq org-self-insert-command-undo-counter
  18492. (1+ org-self-insert-command-undo-counter))))))))
  18493. (defun org-check-before-invisible-edit (kind)
  18494. "Check is editing if kind KIND would be dangerous with invisible text around.
  18495. The detailed reaction depends on the user option `org-catch-invisible-edits'."
  18496. ;; First, try to get out of here as quickly as possible, to reduce overhead
  18497. (if (and org-catch-invisible-edits
  18498. (or (not (boundp 'visible-mode)) (not visible-mode))
  18499. (or (get-char-property (point) 'invisible)
  18500. (get-char-property (max (point-min) (1- (point))) 'invisible)))
  18501. ;; OK, we need to take a closer look
  18502. (let* ((invisible-at-point (get-char-property (point) 'invisible))
  18503. (invisible-before-point (if (bobp) nil (get-char-property
  18504. (1- (point)) 'invisible)))
  18505. (border-and-ok-direction
  18506. (or
  18507. ;; Check if we are acting predictably before invisible text
  18508. (and invisible-at-point (not invisible-before-point)
  18509. (memq kind '(insert delete-backward)))
  18510. ;; Check if we are acting predictably after invisible text
  18511. ;; This works not well, and I have turned it off. It seems
  18512. ;; better to always show and stop after invisible text.
  18513. ;; (and (not invisible-at-point) invisible-before-point
  18514. ;; (memq kind '(insert delete)))
  18515. )))
  18516. (when (or (memq invisible-at-point '(outline org-hide-block t))
  18517. (memq invisible-before-point '(outline org-hide-block t)))
  18518. (if (eq org-catch-invisible-edits 'error)
  18519. (user-error "Editing in invisible areas is prohibited, make them visible first"))
  18520. (if (and org-custom-properties-overlays
  18521. (y-or-n-p "Display invisible properties in this buffer? "))
  18522. (org-toggle-custom-properties-visibility)
  18523. ;; Make the area visible
  18524. (save-excursion
  18525. (if invisible-before-point
  18526. (goto-char (previous-single-char-property-change
  18527. (point) 'invisible)))
  18528. (show-subtree))
  18529. (cond
  18530. ((eq org-catch-invisible-edits 'show)
  18531. ;; That's it, we do the edit after showing
  18532. (message
  18533. "Unfolding invisible region around point before editing")
  18534. (sit-for 1))
  18535. ((and (eq org-catch-invisible-edits 'smart)
  18536. border-and-ok-direction)
  18537. (message "Unfolding invisible region around point before editing"))
  18538. (t
  18539. ;; Don't do the edit, make the user repeat it in full visibility
  18540. (user-error "Edit in invisible region aborted, repeat to confirm with text visible"))))))))
  18541. (defun org-fix-tags-on-the-fly ()
  18542. (when (and (equal (char-after (point-at-bol)) ?*)
  18543. (org-at-heading-p))
  18544. (org-align-tags-here org-tags-column)))
  18545. (defun org-delete-backward-char (N)
  18546. "Like `delete-backward-char', insert whitespace at field end in tables.
  18547. When deleting backwards, in tables this function will insert whitespace in
  18548. front of the next \"|\" separator, to keep the table aligned. The table will
  18549. still be marked for re-alignment if the field did fill the entire column,
  18550. because, in this case the deletion might narrow the column."
  18551. (interactive "p")
  18552. (save-match-data
  18553. (org-check-before-invisible-edit 'delete-backward)
  18554. (if (and (org-table-p)
  18555. (eq N 1)
  18556. (string-match "|" (buffer-substring (point-at-bol) (point)))
  18557. (looking-at ".*?|"))
  18558. (let ((pos (point))
  18559. (noalign (looking-at "[^|\n\r]* |"))
  18560. (c org-table-may-need-update))
  18561. (backward-delete-char N)
  18562. (if (not overwrite-mode)
  18563. (progn
  18564. (skip-chars-forward "^|")
  18565. (insert " ")
  18566. (goto-char (1- pos))))
  18567. ;; noalign: if there were two spaces at the end, this field
  18568. ;; does not determine the width of the column.
  18569. (if noalign (setq org-table-may-need-update c)))
  18570. (backward-delete-char N)
  18571. (org-fix-tags-on-the-fly))))
  18572. (defun org-delete-char (N)
  18573. "Like `delete-char', but insert whitespace at field end in tables.
  18574. When deleting characters, in tables this function will insert whitespace in
  18575. front of the next \"|\" separator, to keep the table aligned. The table will
  18576. still be marked for re-alignment if the field did fill the entire column,
  18577. because, in this case the deletion might narrow the column."
  18578. (interactive "p")
  18579. (save-match-data
  18580. (org-check-before-invisible-edit 'delete)
  18581. (if (and (org-table-p)
  18582. (not (bolp))
  18583. (not (= (char-after) ?|))
  18584. (eq N 1))
  18585. (if (looking-at ".*?|")
  18586. (let ((pos (point))
  18587. (noalign (looking-at "[^|\n\r]* |"))
  18588. (c org-table-may-need-update))
  18589. (replace-match
  18590. (concat (substring (match-string 0) 1 -1) " |") nil t)
  18591. (goto-char pos)
  18592. ;; noalign: if there were two spaces at the end, this field
  18593. ;; does not determine the width of the column.
  18594. (if noalign (setq org-table-may-need-update c)))
  18595. (delete-char N))
  18596. (delete-char N)
  18597. (org-fix-tags-on-the-fly))))
  18598. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  18599. (put 'org-self-insert-command 'delete-selection
  18600. (lambda ()
  18601. (not (run-hook-with-args-until-success
  18602. 'self-insert-uses-region-functions))))
  18603. (put 'orgtbl-self-insert-command 'delete-selection
  18604. (lambda ()
  18605. (not (run-hook-with-args-until-success
  18606. 'self-insert-uses-region-functions))))
  18607. (put 'org-delete-char 'delete-selection 'supersede)
  18608. (put 'org-delete-backward-char 'delete-selection 'supersede)
  18609. (put 'org-yank 'delete-selection 'yank)
  18610. ;; Make `flyspell-mode' delay after some commands
  18611. (put 'org-self-insert-command 'flyspell-delayed t)
  18612. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  18613. (put 'org-delete-char 'flyspell-delayed t)
  18614. (put 'org-delete-backward-char 'flyspell-delayed t)
  18615. ;; Make pabbrev-mode expand after org-mode commands
  18616. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  18617. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  18618. (defun org-remap (map &rest commands)
  18619. "In MAP, remap the functions given in COMMANDS.
  18620. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  18621. (let (new old)
  18622. (while commands
  18623. (setq old (pop commands) new (pop commands))
  18624. (if (fboundp 'command-remapping)
  18625. (org-defkey map (vector 'remap old) new)
  18626. (substitute-key-definition old new map global-map)))))
  18627. (defun org-transpose-words ()
  18628. "Transpose words for Org.
  18629. This uses the `org-mode-transpose-word-syntax-table' syntax
  18630. table, which interprets characters in `org-emphasis-alist' as
  18631. word constituents."
  18632. (interactive)
  18633. (with-syntax-table org-mode-transpose-word-syntax-table
  18634. (call-interactively 'transpose-words)))
  18635. (org-remap org-mode-map 'transpose-words 'org-transpose-words)
  18636. (when (eq org-enable-table-editor 'optimized)
  18637. ;; If the user wants maximum table support, we need to hijack
  18638. ;; some standard editing functions
  18639. (org-remap org-mode-map
  18640. 'self-insert-command 'org-self-insert-command
  18641. 'delete-char 'org-delete-char
  18642. 'delete-backward-char 'org-delete-backward-char)
  18643. (org-defkey org-mode-map "|" 'org-force-self-insert))
  18644. (defvar org-ctrl-c-ctrl-c-hook nil
  18645. "Hook for functions attaching themselves to `C-c C-c'.
  18646. This can be used to add additional functionality to the C-c C-c
  18647. key which executes context-dependent commands. This hook is run
  18648. before any other test, while `org-ctrl-c-ctrl-c-final-hook' is
  18649. run after the last test.
  18650. Each function will be called with no arguments. The function
  18651. must check if the context is appropriate for it to act. If yes,
  18652. it should do its thing and then return a non-nil value. If the
  18653. context is wrong, just do nothing and return nil.")
  18654. (defvar org-ctrl-c-ctrl-c-final-hook nil
  18655. "Hook for functions attaching themselves to `C-c C-c'.
  18656. This can be used to add additional functionality to the C-c C-c
  18657. key which executes context-dependent commands. This hook is run
  18658. after any other test, while `org-ctrl-c-ctrl-c-hook' is run
  18659. before the first test.
  18660. Each function will be called with no arguments. The function
  18661. must check if the context is appropriate for it to act. If yes,
  18662. it should do its thing and then return a non-nil value. If the
  18663. context is wrong, just do nothing and return nil.")
  18664. (defvar org-tab-first-hook nil
  18665. "Hook for functions to attach themselves to TAB.
  18666. See `org-ctrl-c-ctrl-c-hook' for more information.
  18667. This hook runs as the first action when TAB is pressed, even before
  18668. `org-cycle' messes around with the `outline-regexp' to cater for
  18669. inline tasks and plain list item folding.
  18670. If any function in this hook returns t, any other actions that
  18671. would have been caused by TAB (such as table field motion or visibility
  18672. cycling) will not occur.")
  18673. (defvar org-tab-after-check-for-table-hook nil
  18674. "Hook for functions to attach themselves to TAB.
  18675. See `org-ctrl-c-ctrl-c-hook' for more information.
  18676. This hook runs after it has been established that the cursor is not in a
  18677. table, but before checking if the cursor is in a headline or if global cycling
  18678. should be done.
  18679. If any function in this hook returns t, not other actions like visibility
  18680. cycling will be done.")
  18681. (defvar org-tab-after-check-for-cycling-hook nil
  18682. "Hook for functions to attach themselves to TAB.
  18683. See `org-ctrl-c-ctrl-c-hook' for more information.
  18684. This hook runs after it has been established that not table field motion and
  18685. not visibility should be done because of current context. This is probably
  18686. the place where a package like yasnippets can hook in.")
  18687. (defvar org-tab-before-tab-emulation-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 every other options for TAB have been exhausted, but
  18691. before indentation and \t insertion takes place.")
  18692. (defvar org-metaleft-hook nil
  18693. "Hook for functions attaching themselves to `M-left'.
  18694. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18695. (defvar org-metaright-hook nil
  18696. "Hook for functions attaching themselves to `M-right'.
  18697. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18698. (defvar org-metaup-hook nil
  18699. "Hook for functions attaching themselves to `M-up'.
  18700. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18701. (defvar org-metadown-hook nil
  18702. "Hook for functions attaching themselves to `M-down'.
  18703. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18704. (defvar org-shiftmetaleft-hook nil
  18705. "Hook for functions attaching themselves to `M-S-left'.
  18706. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18707. (defvar org-shiftmetaright-hook nil
  18708. "Hook for functions attaching themselves to `M-S-right'.
  18709. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18710. (defvar org-shiftmetaup-hook nil
  18711. "Hook for functions attaching themselves to `M-S-up'.
  18712. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18713. (defvar org-shiftmetadown-hook nil
  18714. "Hook for functions attaching themselves to `M-S-down'.
  18715. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18716. (defvar org-metareturn-hook nil
  18717. "Hook for functions attaching themselves to `M-RET'.
  18718. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18719. (defvar org-shiftup-hook nil
  18720. "Hook for functions attaching themselves to `S-up'.
  18721. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18722. (defvar org-shiftup-final-hook nil
  18723. "Hook for functions attaching themselves to `S-up'.
  18724. This one runs after all other options except shift-select have been excluded.
  18725. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18726. (defvar org-shiftdown-hook nil
  18727. "Hook for functions attaching themselves to `S-down'.
  18728. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18729. (defvar org-shiftdown-final-hook nil
  18730. "Hook for functions attaching themselves to `S-down'.
  18731. This one runs after all other options except shift-select have been excluded.
  18732. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18733. (defvar org-shiftleft-hook nil
  18734. "Hook for functions attaching themselves to `S-left'.
  18735. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18736. (defvar org-shiftleft-final-hook nil
  18737. "Hook for functions attaching themselves to `S-left'.
  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-shiftright-hook nil
  18741. "Hook for functions attaching themselves to `S-right'.
  18742. See `org-ctrl-c-ctrl-c-hook' for more information.")
  18743. (defvar org-shiftright-final-hook nil
  18744. "Hook for functions attaching themselves to `S-right'.
  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. (defun org-modifier-cursor-error ()
  18748. "Throw an error, a modified cursor command was applied in wrong context."
  18749. (user-error "This command is active in special context like tables, headlines or items"))
  18750. (defun org-shiftselect-error ()
  18751. "Throw an error because Shift-Cursor command was applied in wrong context."
  18752. (if (and (boundp 'shift-select-mode) shift-select-mode)
  18753. (user-error "To use shift-selection with Org-mode, customize `org-support-shift-select'")
  18754. (user-error "This command works only in special context like headlines or timestamps")))
  18755. (defun org-call-for-shift-select (cmd)
  18756. (let ((this-command-keys-shift-translated t))
  18757. (call-interactively cmd)))
  18758. (defun org-shifttab (&optional arg)
  18759. "Global visibility cycling or move to previous table field.
  18760. Call `org-table-previous-field' within a table.
  18761. When ARG is nil, cycle globally through visibility states.
  18762. When ARG is a numeric prefix, show contents of this level."
  18763. (interactive "P")
  18764. (cond
  18765. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  18766. ((integerp arg)
  18767. (let ((arg2 (if org-odd-levels-only (1- (* 2 arg)) arg)))
  18768. (message "Content view to level: %d" arg)
  18769. (org-content (prefix-numeric-value arg2))
  18770. (org-cycle-show-empty-lines t)
  18771. (setq org-cycle-global-status 'overview)))
  18772. (t (call-interactively 'org-global-cycle))))
  18773. (defun org-shiftmetaleft ()
  18774. "Promote subtree or delete table column.
  18775. Calls `org-promote-subtree', `org-outdent-item-tree', or
  18776. `org-table-delete-column', depending on context. See the
  18777. individual commands for more information."
  18778. (interactive)
  18779. (cond
  18780. ((run-hook-with-args-until-success 'org-shiftmetaleft-hook))
  18781. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  18782. ((org-at-heading-p) (call-interactively 'org-promote-subtree))
  18783. ((if (not (org-region-active-p)) (org-at-item-p)
  18784. (save-excursion (goto-char (region-beginning))
  18785. (org-at-item-p)))
  18786. (call-interactively 'org-outdent-item-tree))
  18787. (t (org-modifier-cursor-error))))
  18788. (defun org-shiftmetaright ()
  18789. "Demote subtree or insert table column.
  18790. Calls `org-demote-subtree', `org-indent-item-tree', or
  18791. `org-table-insert-column', depending on context. See the
  18792. individual commands for more information."
  18793. (interactive)
  18794. (cond
  18795. ((run-hook-with-args-until-success 'org-shiftmetaright-hook))
  18796. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  18797. ((org-at-heading-p) (call-interactively 'org-demote-subtree))
  18798. ((if (not (org-region-active-p)) (org-at-item-p)
  18799. (save-excursion (goto-char (region-beginning))
  18800. (org-at-item-p)))
  18801. (call-interactively 'org-indent-item-tree))
  18802. (t (org-modifier-cursor-error))))
  18803. (defun org-shiftmetaup (&optional arg)
  18804. "Drag the line at point up.
  18805. In a table, kill the current row.
  18806. On a clock timestamp, update the value of the timestamp like `S-<up>'
  18807. but also adjust the previous clocked item in the clock history.
  18808. Everywhere else, drag the line at point up."
  18809. (interactive "P")
  18810. (cond
  18811. ((run-hook-with-args-until-success 'org-shiftmetaup-hook))
  18812. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  18813. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  18814. (call-interactively 'org-timestamp-up)))
  18815. (t (call-interactively 'org-drag-line-backward))))
  18816. (defun org-shiftmetadown (&optional arg)
  18817. "Drag the line at point down.
  18818. In a table, insert an empty row at the current line.
  18819. On a clock timestamp, update the value of the timestamp like `S-<down>'
  18820. but also adjust the previous clocked item in the clock history.
  18821. Everywhere else, drag the line at point down."
  18822. (interactive "P")
  18823. (cond
  18824. ((run-hook-with-args-until-success 'org-shiftmetadown-hook))
  18825. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  18826. ((org-at-clock-log-p) (let ((org-clock-adjust-closest t))
  18827. (call-interactively 'org-timestamp-down)))
  18828. (t (call-interactively 'org-drag-line-forward))))
  18829. (defsubst org-hidden-tree-error ()
  18830. (user-error
  18831. "Hidden subtree, open with TAB or use subtree command M-S-<left>/<right>"))
  18832. (defun org-metaleft (&optional arg)
  18833. "Promote heading, list item at point or move table column left.
  18834. Calls `org-do-promote', `org-outdent-item' or `org-table-move-column',
  18835. depending on context. With no specific context, calls the Emacs
  18836. default `backward-word'. See the individual commands for more
  18837. information.
  18838. This function runs the hook `org-metaleft-hook' as a first step,
  18839. and returns at first non-nil value."
  18840. (interactive "P")
  18841. (cond
  18842. ((run-hook-with-args-until-success 'org-metaleft-hook))
  18843. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  18844. ((org-with-limited-levels
  18845. (or (org-at-heading-p)
  18846. (and (org-region-active-p)
  18847. (save-excursion
  18848. (goto-char (region-beginning))
  18849. (org-at-heading-p)))))
  18850. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  18851. (call-interactively 'org-do-promote))
  18852. ;; At an inline task.
  18853. ((org-at-heading-p)
  18854. (call-interactively 'org-inlinetask-promote))
  18855. ((or (org-at-item-p)
  18856. (and (org-region-active-p)
  18857. (save-excursion
  18858. (goto-char (region-beginning))
  18859. (org-at-item-p))))
  18860. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  18861. (call-interactively 'org-outdent-item))
  18862. (t (call-interactively 'backward-word))))
  18863. (defun org-metaright (&optional arg)
  18864. "Demote heading, list item at point or move table column right.
  18865. In front of a drawer or a block keyword, indent it correctly.
  18866. Calls `org-do-demote', `org-indent-item', `org-table-move-column',
  18867. `org-indnet-drawer' or `org-indent-block' depending on context.
  18868. With no specific context, calls the Emacs default `forward-word'.
  18869. See the individual commands for more information.
  18870. This function runs the hook `org-metaright-hook' as a first step,
  18871. and returns at first non-nil value."
  18872. (interactive "P")
  18873. (cond
  18874. ((run-hook-with-args-until-success 'org-metaright-hook))
  18875. ((org-at-table-p) (call-interactively 'org-table-move-column))
  18876. ((org-at-drawer-p) (call-interactively 'org-indent-drawer))
  18877. ((org-at-block-p) (call-interactively 'org-indent-block))
  18878. ((org-with-limited-levels
  18879. (or (org-at-heading-p)
  18880. (and (org-region-active-p)
  18881. (save-excursion
  18882. (goto-char (region-beginning))
  18883. (org-at-heading-p)))))
  18884. (when (org-check-for-hidden 'headlines) (org-hidden-tree-error))
  18885. (call-interactively 'org-do-demote))
  18886. ;; At an inline task.
  18887. ((org-at-heading-p)
  18888. (call-interactively 'org-inlinetask-demote))
  18889. ((or (org-at-item-p)
  18890. (and (org-region-active-p)
  18891. (save-excursion
  18892. (goto-char (region-beginning))
  18893. (org-at-item-p))))
  18894. (when (org-check-for-hidden 'items) (org-hidden-tree-error))
  18895. (call-interactively 'org-indent-item))
  18896. (t (call-interactively 'forward-word))))
  18897. (defun org-check-for-hidden (what)
  18898. "Check if there are hidden headlines/items in the current visual line.
  18899. WHAT can be either `headlines' or `items'. If the current line is
  18900. an outline or item heading and it has a folded subtree below it,
  18901. this function returns t, nil otherwise."
  18902. (let ((re (cond
  18903. ((eq what 'headlines) org-outline-regexp-bol)
  18904. ((eq what 'items) (org-item-beginning-re))
  18905. (t (error "This should not happen"))))
  18906. beg end)
  18907. (save-excursion
  18908. (catch 'exit
  18909. (unless (org-region-active-p)
  18910. (setq beg (point-at-bol))
  18911. (beginning-of-line 2)
  18912. (while (and (not (eobp)) ;; this is like `next-line'
  18913. (get-char-property (1- (point)) 'invisible))
  18914. (beginning-of-line 2))
  18915. (setq end (point))
  18916. (goto-char beg)
  18917. (goto-char (point-at-eol))
  18918. (setq end (max end (point)))
  18919. (while (re-search-forward re end t)
  18920. (if (get-char-property (match-beginning 0) 'invisible)
  18921. (throw 'exit t))))
  18922. nil))))
  18923. (defun org-metaup (&optional arg)
  18924. "Move subtree up or move table row up.
  18925. Calls `org-move-subtree-up' or `org-table-move-row' or
  18926. `org-move-item-up', depending on context. See the individual commands
  18927. for more information."
  18928. (interactive "P")
  18929. (cond
  18930. ((run-hook-with-args-until-success 'org-metaup-hook))
  18931. ((org-region-active-p)
  18932. (let* ((a (min (region-beginning) (region-end)))
  18933. (b (1- (max (region-beginning) (region-end))))
  18934. (c (save-excursion (goto-char a)
  18935. (move-beginning-of-line 0)))
  18936. (d (save-excursion (goto-char a)
  18937. (move-end-of-line 0) (point))))
  18938. (transpose-regions a b c d)
  18939. (goto-char c)))
  18940. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  18941. ((org-at-heading-p) (call-interactively 'org-move-subtree-up))
  18942. ((org-at-item-p) (call-interactively 'org-move-item-up))
  18943. (t (org-drag-element-backward))))
  18944. (defun org-metadown (&optional arg)
  18945. "Move subtree down or move table row down.
  18946. Calls `org-move-subtree-down' or `org-table-move-row' or
  18947. `org-move-item-down', depending on context. See the individual
  18948. commands for more information."
  18949. (interactive "P")
  18950. (cond
  18951. ((run-hook-with-args-until-success 'org-metadown-hook))
  18952. ((org-region-active-p)
  18953. (let* ((a (min (region-beginning) (region-end)))
  18954. (b (max (region-beginning) (region-end)))
  18955. (c (save-excursion (goto-char b)
  18956. (move-beginning-of-line 1)))
  18957. (d (save-excursion (goto-char b)
  18958. (move-end-of-line 1) (1+ (point)))))
  18959. (transpose-regions a b c d)
  18960. (goto-char d)))
  18961. ((org-at-table-p) (call-interactively 'org-table-move-row))
  18962. ((org-at-heading-p) (call-interactively 'org-move-subtree-down))
  18963. ((org-at-item-p) (call-interactively 'org-move-item-down))
  18964. (t (org-drag-element-forward))))
  18965. (defun org-shiftup (&optional arg)
  18966. "Increase item in timestamp or increase priority of current headline.
  18967. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  18968. depending on context. See the individual commands for more information."
  18969. (interactive "P")
  18970. (cond
  18971. ((run-hook-with-args-until-success 'org-shiftup-hook))
  18972. ((and org-support-shift-select (org-region-active-p))
  18973. (org-call-for-shift-select 'previous-line))
  18974. ((org-at-timestamp-p t)
  18975. (call-interactively (if org-edit-timestamp-down-means-later
  18976. 'org-timestamp-down 'org-timestamp-up)))
  18977. ((and (not (eq org-support-shift-select 'always))
  18978. org-enable-priority-commands
  18979. (org-at-heading-p))
  18980. (call-interactively 'org-priority-up))
  18981. ((and (not org-support-shift-select) (org-at-item-p))
  18982. (call-interactively 'org-previous-item))
  18983. ((org-clocktable-try-shift 'up arg))
  18984. ((run-hook-with-args-until-success 'org-shiftup-final-hook))
  18985. (org-support-shift-select
  18986. (org-call-for-shift-select 'previous-line))
  18987. (t (org-shiftselect-error))))
  18988. (defun org-shiftdown (&optional arg)
  18989. "Decrease item in timestamp or decrease priority of current headline.
  18990. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  18991. depending on context. See the individual commands for more information."
  18992. (interactive "P")
  18993. (cond
  18994. ((run-hook-with-args-until-success 'org-shiftdown-hook))
  18995. ((and org-support-shift-select (org-region-active-p))
  18996. (org-call-for-shift-select 'next-line))
  18997. ((org-at-timestamp-p t)
  18998. (call-interactively (if org-edit-timestamp-down-means-later
  18999. 'org-timestamp-up 'org-timestamp-down)))
  19000. ((and (not (eq org-support-shift-select 'always))
  19001. org-enable-priority-commands
  19002. (org-at-heading-p))
  19003. (call-interactively 'org-priority-down))
  19004. ((and (not org-support-shift-select) (org-at-item-p))
  19005. (call-interactively 'org-next-item))
  19006. ((org-clocktable-try-shift 'down arg))
  19007. ((run-hook-with-args-until-success 'org-shiftdown-final-hook))
  19008. (org-support-shift-select
  19009. (org-call-for-shift-select 'next-line))
  19010. (t (org-shiftselect-error))))
  19011. (defun org-shiftright (&optional arg)
  19012. "Cycle the thing at point or in the current line, depending on context.
  19013. Depending on context, this does one of the following:
  19014. - switch a timestamp at point one day into the future
  19015. - on a headline, switch to the next TODO keyword.
  19016. - on an item, switch entire list to the next bullet type
  19017. - on a property line, switch to the next allowed value
  19018. - on a clocktable definition line, move time block into the future"
  19019. (interactive "P")
  19020. (cond
  19021. ((run-hook-with-args-until-success 'org-shiftright-hook))
  19022. ((and org-support-shift-select (org-region-active-p))
  19023. (org-call-for-shift-select 'forward-char))
  19024. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  19025. ((and (not (eq org-support-shift-select 'always))
  19026. (org-at-heading-p))
  19027. (let ((org-inhibit-logging
  19028. (not org-treat-S-cursor-todo-selection-as-state-change))
  19029. (org-inhibit-blocking
  19030. (not org-treat-S-cursor-todo-selection-as-state-change)))
  19031. (org-call-with-arg 'org-todo 'right)))
  19032. ((or (and org-support-shift-select
  19033. (not (eq org-support-shift-select 'always))
  19034. (org-at-item-bullet-p))
  19035. (and (not org-support-shift-select) (org-at-item-p)))
  19036. (org-call-with-arg 'org-cycle-list-bullet nil))
  19037. ((and (not (eq org-support-shift-select 'always))
  19038. (org-at-property-p))
  19039. (call-interactively 'org-property-next-allowed-value))
  19040. ((org-clocktable-try-shift 'right arg))
  19041. ((run-hook-with-args-until-success 'org-shiftright-final-hook))
  19042. (org-support-shift-select
  19043. (org-call-for-shift-select 'forward-char))
  19044. (t (org-shiftselect-error))))
  19045. (defun org-shiftleft (&optional arg)
  19046. "Cycle the thing at point or in the current line, depending on context.
  19047. Depending on context, this does one of the following:
  19048. - switch a timestamp at point one day into the past
  19049. - on a headline, switch to the previous TODO keyword.
  19050. - on an item, switch entire list to the previous bullet type
  19051. - on a property line, switch to the previous allowed value
  19052. - on a clocktable definition line, move time block into the past"
  19053. (interactive "P")
  19054. (cond
  19055. ((run-hook-with-args-until-success 'org-shiftleft-hook))
  19056. ((and org-support-shift-select (org-region-active-p))
  19057. (org-call-for-shift-select 'backward-char))
  19058. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  19059. ((and (not (eq org-support-shift-select 'always))
  19060. (org-at-heading-p))
  19061. (let ((org-inhibit-logging
  19062. (not org-treat-S-cursor-todo-selection-as-state-change))
  19063. (org-inhibit-blocking
  19064. (not org-treat-S-cursor-todo-selection-as-state-change)))
  19065. (org-call-with-arg 'org-todo 'left)))
  19066. ((or (and org-support-shift-select
  19067. (not (eq org-support-shift-select 'always))
  19068. (org-at-item-bullet-p))
  19069. (and (not org-support-shift-select) (org-at-item-p)))
  19070. (org-call-with-arg 'org-cycle-list-bullet 'previous))
  19071. ((and (not (eq org-support-shift-select 'always))
  19072. (org-at-property-p))
  19073. (call-interactively 'org-property-previous-allowed-value))
  19074. ((org-clocktable-try-shift 'left arg))
  19075. ((run-hook-with-args-until-success 'org-shiftleft-final-hook))
  19076. (org-support-shift-select
  19077. (org-call-for-shift-select 'backward-char))
  19078. (t (org-shiftselect-error))))
  19079. (defun org-shiftcontrolright ()
  19080. "Switch to next TODO set."
  19081. (interactive)
  19082. (cond
  19083. ((and org-support-shift-select (org-region-active-p))
  19084. (org-call-for-shift-select 'forward-word))
  19085. ((and (not (eq org-support-shift-select 'always))
  19086. (org-at-heading-p))
  19087. (org-call-with-arg 'org-todo 'nextset))
  19088. (org-support-shift-select
  19089. (org-call-for-shift-select 'forward-word))
  19090. (t (org-shiftselect-error))))
  19091. (defun org-shiftcontrolleft ()
  19092. "Switch to previous TODO set."
  19093. (interactive)
  19094. (cond
  19095. ((and org-support-shift-select (org-region-active-p))
  19096. (org-call-for-shift-select 'backward-word))
  19097. ((and (not (eq org-support-shift-select 'always))
  19098. (org-at-heading-p))
  19099. (org-call-with-arg 'org-todo 'previousset))
  19100. (org-support-shift-select
  19101. (org-call-for-shift-select 'backward-word))
  19102. (t (org-shiftselect-error))))
  19103. (defun org-shiftcontrolup (&optional n)
  19104. "Change timestamps synchronously up in CLOCK log lines.
  19105. Optional argument N tells to change by that many units."
  19106. (interactive "P")
  19107. (if (and (org-at-clock-log-p) (org-at-timestamp-p t))
  19108. (let (org-support-shift-select)
  19109. (org-clock-timestamps-up n))
  19110. (user-error "Not at a clock log")))
  19111. (defun org-shiftcontroldown (&optional n)
  19112. "Change timestamps synchronously down in CLOCK log lines.
  19113. Optional argument N tells to change by that many units."
  19114. (interactive "P")
  19115. (if (and (org-at-clock-log-p) (org-at-timestamp-p t))
  19116. (let (org-support-shift-select)
  19117. (org-clock-timestamps-down n))
  19118. (user-error "Not at a clock log")))
  19119. (defun org-increase-number-at-point (&optional inc)
  19120. "Increment the number at point.
  19121. With an optional prefix numeric argument INC, increment using
  19122. this numeric value."
  19123. (interactive "p")
  19124. (if (not (number-at-point))
  19125. (user-error "Not on a number")
  19126. (unless inc (setq inc 1))
  19127. (let ((pos (point))
  19128. (beg (skip-chars-backward "-+^/*0-9eE."))
  19129. (end (skip-chars-forward "-+^/*0-9eE^.")) nap)
  19130. (setq nap (buffer-substring-no-properties
  19131. (+ pos beg) (+ pos beg end)))
  19132. (delete-region (+ pos beg) (+ pos beg end))
  19133. (insert (calc-eval (concat (number-to-string inc) "+" nap))))
  19134. (when (org-at-table-p)
  19135. (org-table-align)
  19136. (org-table-end-of-field 1))))
  19137. (defun org-decrease-number-at-point (&optional inc)
  19138. "Decrement the number at point.
  19139. With an optional prefix numeric argument INC, decrement using
  19140. this numeric value."
  19141. (interactive "p")
  19142. (org-increase-number-at-point (- (or inc 1))))
  19143. (defun org-ctrl-c-ret ()
  19144. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  19145. (interactive)
  19146. (cond
  19147. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  19148. (t (call-interactively 'org-insert-heading))))
  19149. (defun org-find-visible ()
  19150. (let ((s (point)))
  19151. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  19152. (get-char-property s 'invisible)))
  19153. s))
  19154. (defun org-find-invisible ()
  19155. (let ((s (point)))
  19156. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  19157. (not (get-char-property s 'invisible))))
  19158. s))
  19159. (defun org-copy-visible (beg end)
  19160. "Copy the visible parts of the region."
  19161. (interactive "r")
  19162. (let (snippets s)
  19163. (save-excursion
  19164. (save-restriction
  19165. (narrow-to-region beg end)
  19166. (setq s (goto-char (point-min)))
  19167. (while (not (= (point) (point-max)))
  19168. (goto-char (org-find-invisible))
  19169. (push (buffer-substring s (point)) snippets)
  19170. (setq s (goto-char (org-find-visible))))))
  19171. (kill-new (apply 'concat (nreverse snippets)))))
  19172. (defun org-copy-special ()
  19173. "Copy region in table or copy current subtree.
  19174. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  19175. See the individual commands for more information."
  19176. (interactive)
  19177. (call-interactively
  19178. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  19179. (defun org-cut-special ()
  19180. "Cut region in table or cut current subtree.
  19181. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  19182. See the individual commands for more information."
  19183. (interactive)
  19184. (call-interactively
  19185. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  19186. (defun org-paste-special (arg)
  19187. "Paste rectangular region into table, or past subtree relative to level.
  19188. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  19189. See the individual commands for more information."
  19190. (interactive "P")
  19191. (if (org-at-table-p)
  19192. (org-table-paste-rectangle)
  19193. (org-paste-subtree arg)))
  19194. (defsubst org-in-fixed-width-region-p ()
  19195. "Is point in a fixed-width region?"
  19196. (save-match-data
  19197. (eq 'fixed-width (org-element-type (org-element-at-point)))))
  19198. (defun org-edit-special (&optional arg)
  19199. "Call a special editor for the element at point.
  19200. When at a table, call the formula editor with `org-table-edit-formulas'.
  19201. When in a source code block, call `org-edit-src-code'.
  19202. When in a fixed-width region, call `org-edit-fixed-width-region'.
  19203. When in an export block, call `org-edit-export-block'.
  19204. When at an #+INCLUDE keyword, visit the included file.
  19205. When at a footnote reference, call `org-edit-footnote-reference'
  19206. On a link, call `ffap' to visit the link at point.
  19207. Otherwise, return a user error."
  19208. (interactive "P")
  19209. (let ((element (org-element-at-point)))
  19210. (assert (not buffer-read-only) nil
  19211. "Buffer is read-only: %s" (buffer-name))
  19212. (case (org-element-type element)
  19213. (src-block
  19214. (if (not arg) (org-edit-src-code)
  19215. (let* ((info (org-babel-get-src-block-info))
  19216. (lang (nth 0 info))
  19217. (params (nth 2 info))
  19218. (session (cdr (assq :session params))))
  19219. (if (not session) (org-edit-src-code)
  19220. ;; At a src-block with a session and function called with
  19221. ;; an ARG: switch to the buffer related to the inferior
  19222. ;; process.
  19223. (switch-to-buffer
  19224. (funcall (intern (concat "org-babel-prep-session:" lang))
  19225. session params))))))
  19226. (keyword
  19227. (if (member (org-element-property :key element) '("INCLUDE" "SETUPFILE"))
  19228. (org-open-link-from-string
  19229. (format "[[%s]]"
  19230. (expand-file-name
  19231. (let ((value (org-element-property :value element)))
  19232. (cond ((not (org-string-nw-p value))
  19233. (user-error "No file to edit"))
  19234. ((string-match "\\`\"\\(.*?\\)\"" value)
  19235. (match-string 1 value))
  19236. ((string-match "\\`[^ \t\"]\\S-*" value)
  19237. (match-string 0 value))
  19238. (t (user-error "No valid file specified")))))))
  19239. (user-error "No special environment to edit here")))
  19240. (table
  19241. (if (eq (org-element-property :type element) 'table.el)
  19242. (org-edit-table.el)
  19243. (call-interactively 'org-table-edit-formulas)))
  19244. ;; Only Org tables contain `table-row' type elements.
  19245. (table-row (call-interactively 'org-table-edit-formulas))
  19246. (example-block (org-edit-src-code))
  19247. (export-block (org-edit-export-block))
  19248. (fixed-width (org-edit-fixed-width-region))
  19249. (otherwise
  19250. ;; No notable element at point. Though, we may be at a link or
  19251. ;; a footnote reference, which are objects. Thus, scan deeper.
  19252. (let ((context (org-element-context element)))
  19253. (case (org-element-type context)
  19254. (link (call-interactively #'ffap))
  19255. (footnote-reference (org-edit-footnote-reference))
  19256. (t (user-error "No special environment to edit here"))))))))
  19257. (defvar org-table-coordinate-overlays) ; defined in org-table.el
  19258. (defun org-ctrl-c-ctrl-c (&optional arg)
  19259. "Set tags in headline, or update according to changed information at point.
  19260. This command does many different things, depending on context:
  19261. - If a function in `org-ctrl-c-ctrl-c-hook' recognizes this location,
  19262. this is what we do.
  19263. - If the cursor is on a statistics cookie, update it.
  19264. - If the cursor is in a headline, prompt for tags and insert them
  19265. into the current line, aligned to `org-tags-column'. When called
  19266. with prefix arg, realign all tags in the current buffer.
  19267. - If the cursor is in one of the special #+KEYWORD lines, this
  19268. triggers scanning the buffer for these lines and updating the
  19269. information.
  19270. - If the cursor is inside a table, realign the table. This command
  19271. works even if the automatic table editor has been turned off.
  19272. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  19273. the entire table.
  19274. - If the cursor is at a footnote reference or definition, jump to
  19275. the corresponding definition or references, respectively.
  19276. - If the cursor is a the beginning of a dynamic block, update it.
  19277. - If the current buffer is a capture buffer, close note and file it.
  19278. - If the cursor is on a <<<target>>>, update radio targets and
  19279. corresponding links in this buffer.
  19280. - If the cursor is on a numbered item in a plain list, renumber the
  19281. ordered list.
  19282. - If the cursor is on a checkbox, toggle it.
  19283. - If the cursor is on a code block, evaluate it. The variable
  19284. `org-confirm-babel-evaluate' can be used to control prompting
  19285. before code block evaluation, by default every code block
  19286. evaluation requires confirmation. Code block evaluation can be
  19287. inhibited by setting `org-babel-no-eval-on-ctrl-c-ctrl-c'."
  19288. (interactive "P")
  19289. (cond
  19290. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  19291. org-occur-highlights)
  19292. (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
  19293. (org-remove-occur-highlights)
  19294. (message "Temporary highlights/overlays removed from current buffer"))
  19295. ((and (local-variable-p 'org-finish-function (current-buffer))
  19296. (fboundp org-finish-function))
  19297. (funcall org-finish-function))
  19298. ((run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-hook))
  19299. (t
  19300. (if (save-excursion (beginning-of-line) (looking-at "[ \t]*$"))
  19301. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  19302. (user-error "C-c C-c can do nothing useful at this location"))
  19303. (let* ((context (org-element-context))
  19304. (type (org-element-type context)))
  19305. (case type
  19306. ;; When at a link, act according to the parent instead.
  19307. (link (setq context (org-element-property :parent context))
  19308. (setq type (org-element-type context)))
  19309. ;; Unsupported object types: refer to the first supported
  19310. ;; element or object containing it.
  19311. ((bold code entity export-snippet inline-babel-call inline-src-block
  19312. italic latex-fragment line-break macro strike-through subscript
  19313. superscript underline verbatim)
  19314. (setq context
  19315. (org-element-lineage
  19316. context '(radio-target paragraph verse-block table-cell)))))
  19317. ;; For convenience: at the first line of a paragraph on the
  19318. ;; same line as an item, apply function on that item instead.
  19319. (when (eq type 'paragraph)
  19320. (let ((parent (org-element-property :parent context)))
  19321. (when (and (eq (org-element-type parent) 'item)
  19322. (= (line-beginning-position)
  19323. (org-element-property :begin parent)))
  19324. (setq context parent type 'item))))
  19325. ;; Act according to type of element or object at point.
  19326. (case type
  19327. (clock (org-clock-update-time-maybe))
  19328. (dynamic-block
  19329. (save-excursion
  19330. (goto-char (org-element-property :post-affiliated context))
  19331. (org-update-dblock)))
  19332. (footnote-definition
  19333. (goto-char (org-element-property :post-affiliated context))
  19334. (call-interactively 'org-footnote-action))
  19335. (footnote-reference (call-interactively 'org-footnote-action))
  19336. ((headline inlinetask)
  19337. (save-excursion (goto-char (org-element-property :begin context))
  19338. (call-interactively 'org-set-tags)))
  19339. (item
  19340. ;; At an item: a double C-u set checkbox to "[-]"
  19341. ;; unconditionally, whereas a single one will toggle its
  19342. ;; presence. Without a universal argument, if the item
  19343. ;; has a checkbox, toggle it. Otherwise repair the list.
  19344. (let* ((box (org-element-property :checkbox context))
  19345. (struct (org-element-property :structure context))
  19346. (old-struct (copy-tree struct))
  19347. (parents (org-list-parents-alist struct))
  19348. (prevs (org-list-prevs-alist struct))
  19349. (orderedp (org-not-nil (org-entry-get nil "ORDERED"))))
  19350. (org-list-set-checkbox
  19351. (org-element-property :begin context) struct
  19352. (cond ((equal arg '(16)) "[-]")
  19353. ((and (not box) (equal arg '(4))) "[ ]")
  19354. ((or (not box) (equal arg '(4))) nil)
  19355. ((eq box 'on) "[ ]")
  19356. (t "[X]")))
  19357. ;; Mimic `org-list-write-struct' but with grabbing
  19358. ;; a return value from `org-list-struct-fix-box'.
  19359. (org-list-struct-fix-ind struct parents 2)
  19360. (org-list-struct-fix-item-end struct)
  19361. (org-list-struct-fix-bul struct prevs)
  19362. (org-list-struct-fix-ind struct parents)
  19363. (let ((block-item
  19364. (org-list-struct-fix-box struct parents prevs orderedp)))
  19365. (if (and box (equal struct old-struct))
  19366. (if (equal arg '(16))
  19367. (message "Checkboxes already reset")
  19368. (user-error "Cannot toggle this checkbox: %s"
  19369. (if (eq box 'on)
  19370. "all subitems checked"
  19371. "unchecked subitems")))
  19372. (org-list-struct-apply-struct struct old-struct)
  19373. (org-update-checkbox-count-maybe))
  19374. (when block-item
  19375. (message "Checkboxes were removed due to empty box at line %d"
  19376. (org-current-line block-item))))))
  19377. (keyword
  19378. (let ((org-inhibit-startup-visibility-stuff t)
  19379. (org-startup-align-all-tables nil))
  19380. (when (boundp 'org-table-coordinate-overlays)
  19381. (mapc 'delete-overlay org-table-coordinate-overlays)
  19382. (setq org-table-coordinate-overlays nil))
  19383. (org-save-outline-visibility 'use-markers (org-mode-restart)))
  19384. (message "Local setup has been refreshed"))
  19385. (plain-list
  19386. ;; At a plain list, with a double C-u argument, set
  19387. ;; checkboxes of each item to "[-]", whereas a single one
  19388. ;; will toggle their presence according to the state of the
  19389. ;; first item in the list. Without an argument, repair the
  19390. ;; list.
  19391. (let* ((begin (org-element-property :contents-begin context))
  19392. (beginm (move-marker (make-marker) begin))
  19393. (struct (org-element-property :structure context))
  19394. (old-struct (copy-tree struct))
  19395. (first-box (save-excursion
  19396. (goto-char begin)
  19397. (looking-at org-list-full-item-re)
  19398. (match-string-no-properties 3)))
  19399. (new-box (cond ((equal arg '(16)) "[-]")
  19400. ((equal arg '(4)) (unless first-box "[ ]"))
  19401. ((equal first-box "[X]") "[ ]")
  19402. (t "[X]"))))
  19403. (cond
  19404. (arg
  19405. (mapc (lambda (pos) (org-list-set-checkbox pos struct new-box))
  19406. (org-list-get-all-items
  19407. begin struct (org-list-prevs-alist struct))))
  19408. ((and first-box (eq (point) begin))
  19409. ;; For convenience, when point is at bol on the first
  19410. ;; item of the list and no argument is provided, simply
  19411. ;; toggle checkbox of that item, if any.
  19412. (org-list-set-checkbox begin struct new-box)))
  19413. (org-list-write-struct
  19414. struct (org-list-parents-alist struct) old-struct)
  19415. (org-update-checkbox-count-maybe)
  19416. (save-excursion (goto-char beginm) (org-list-send-list 'maybe))))
  19417. ((property-drawer node-property)
  19418. (call-interactively 'org-property-action))
  19419. ((radio-target target)
  19420. (call-interactively 'org-update-radio-target-regexp))
  19421. (statistics-cookie
  19422. (call-interactively 'org-update-statistics-cookies))
  19423. ((table table-cell table-row)
  19424. ;; At a table, recalculate every field and align it. Also
  19425. ;; send the table if necessary. If the table has
  19426. ;; a `table.el' type, just give up. At a table row or
  19427. ;; cell, maybe recalculate line but always align table.
  19428. (if (eq (org-element-property :type context) 'table.el)
  19429. (message "Use C-c ' to edit table.el tables")
  19430. (let ((org-enable-table-editor t))
  19431. (if (or (eq type 'table)
  19432. ;; Check if point is at a TBLFM line.
  19433. (and (eq type 'table-row)
  19434. (= (point) (org-element-property :end context))))
  19435. (save-excursion
  19436. (if (org-at-TBLFM-p)
  19437. (progn (require 'org-table)
  19438. (org-table-calc-current-TBLFM))
  19439. (goto-char (org-element-property :contents-begin context))
  19440. (org-call-with-arg 'org-table-recalculate (or arg t))
  19441. (orgtbl-send-table 'maybe)))
  19442. (org-table-maybe-eval-formula)
  19443. (cond (arg (call-interactively 'org-table-recalculate))
  19444. ((org-table-maybe-recalculate-line))
  19445. (t (org-table-align)))))))
  19446. (timestamp (org-timestamp-change 0 'day))
  19447. (otherwise
  19448. (or (run-hook-with-args-until-success 'org-ctrl-c-ctrl-c-final-hook)
  19449. (user-error
  19450. "C-c C-c can do nothing useful at this location")))))))))
  19451. (defun org-mode-restart ()
  19452. (interactive)
  19453. (let ((indent-status (org-bound-and-true-p org-indent-mode)))
  19454. (funcall major-mode)
  19455. (hack-local-variables)
  19456. (when (and indent-status (not (org-bound-and-true-p org-indent-mode)))
  19457. (org-indent-mode -1)))
  19458. (message "%s restarted" major-mode))
  19459. (defun org-kill-note-or-show-branches ()
  19460. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  19461. (interactive)
  19462. (if (not org-finish-function)
  19463. (progn
  19464. (hide-subtree)
  19465. (call-interactively 'show-branches))
  19466. (let ((org-note-abort t))
  19467. (funcall org-finish-function))))
  19468. (defun org-delete-indentation (&optional ARG)
  19469. "Join current line to previous and fix whitespace at join.
  19470. If previous line is a headline add to headline title. Otherwise
  19471. the function calls `delete-indentation'.
  19472. With argument, join this line to following line."
  19473. (interactive "*P")
  19474. (if (save-excursion
  19475. (if ARG (beginning-of-line)
  19476. (forward-line -1))
  19477. (looking-at org-complex-heading-regexp))
  19478. ;; At headline.
  19479. (let ((tags-column (when (match-beginning 5)
  19480. (save-excursion (goto-char (match-beginning 5))
  19481. (current-column))))
  19482. (string (concat " " (progn (when ARG (forward-line 1))
  19483. (org-trim (delete-and-extract-region
  19484. (line-beginning-position)
  19485. (line-end-position)))))))
  19486. (unless (bobp) (delete-region (point) (1- (point))))
  19487. (goto-char (or (match-end 4)
  19488. (match-beginning 5)
  19489. (match-end 0)))
  19490. (skip-chars-backward " \t")
  19491. (save-excursion (insert string))
  19492. ;; Adjust alignment of tags.
  19493. (when tags-column
  19494. (org-align-tags-here (if org-auto-align-tags
  19495. org-tags-column
  19496. tags-column))))
  19497. (delete-indentation ARG)))
  19498. (defun org-open-line (n)
  19499. "Insert a new row in tables, call `open-line' elsewhere.
  19500. If `org-special-ctrl-o' is nil, just call `open-line' everywhere."
  19501. (interactive "*p")
  19502. (cond
  19503. ((not org-special-ctrl-o)
  19504. (open-line n))
  19505. ((org-at-table-p)
  19506. (org-table-insert-row))
  19507. (t
  19508. (open-line n))))
  19509. (defun org-return (&optional indent)
  19510. "Goto next table row or insert a newline.
  19511. Calls `org-table-next-row' or `newline', depending on context.
  19512. When optional INDENT argument is non-nil, call
  19513. `newline-and-indent' instead of `newline'.
  19514. When `org-return-follows-link' is non-nil and point is on
  19515. a timestamp or a link, call `org-open-at-point'. However, it
  19516. will not happen if point is in a table or on a \"dead\"
  19517. object (e.g., within a comment). In these case, you need to use
  19518. `org-open-at-point' directly."
  19519. (interactive)
  19520. (let* ((context (if org-return-follows-link (org-element-context)
  19521. (org-element-at-point)))
  19522. (type (org-element-type context)))
  19523. (cond
  19524. ;; In a table, call `org-table-next-row'.
  19525. ((or (and (eq type 'table)
  19526. (>= (point) (org-element-property :contents-begin context))
  19527. (< (point) (org-element-property :contents-end context)))
  19528. (org-element-lineage context '(table-row table-cell) t))
  19529. (org-table-justify-field-maybe)
  19530. (call-interactively #'org-table-next-row))
  19531. ;; On a link or a timestamp but not on white spaces after it,
  19532. ;; call `org-open-line' if `org-return-follows-link' allows it.
  19533. ((and org-return-follows-link
  19534. (memq type '(link timestamp))
  19535. (< (point)
  19536. (save-excursion (goto-char (org-element-property :end context))
  19537. (skip-chars-backward " \t")
  19538. (point))))
  19539. (call-interactively #'org-open-at-point))
  19540. ;; Insert newline in heading, but preserve tags.
  19541. ((and (not (bolp))
  19542. (save-excursion (beginning-of-line)
  19543. (looking-at org-complex-heading-regexp)))
  19544. ;; At headline. Split line. However, if point is on keyword,
  19545. ;; priority cookie or tags, do not break any of them: add
  19546. ;; a newline after the headline instead.
  19547. (let ((tags-column (and (match-beginning 5)
  19548. (save-excursion (goto-char (match-beginning 5))
  19549. (current-column))))
  19550. (string
  19551. (when (and (match-end 4)
  19552. (>= (point)
  19553. (or (match-end 3) (match-end 2) (1+ (match-end 1))))
  19554. (<= (point) (match-end 4)))
  19555. (delete-and-extract-region (point) (match-end 4)))))
  19556. (when (and tags-column string) ; Adjust tag alignment.
  19557. (org-align-tags-here
  19558. (if org-auto-align-tags org-tags-column tags-column)))
  19559. (end-of-line)
  19560. (org-show-entry)
  19561. (if indent (newline-and-indent) (newline))
  19562. (when string (save-excursion (insert (org-trim string))))))
  19563. ;; In a list, make sure indenting keeps trailing text within.
  19564. ((and indent
  19565. (not (eolp))
  19566. (org-element-lineage context '(item)))
  19567. (let ((trailing-data
  19568. (delete-and-extract-region (point) (line-end-position))))
  19569. (newline-and-indent)
  19570. (save-excursion (insert trailing-data))))
  19571. (t (if indent (newline-and-indent) (newline))))))
  19572. (defun org-return-indent ()
  19573. "Goto next table row or insert a newline and indent.
  19574. Calls `org-table-next-row' or `newline-and-indent', depending on
  19575. context. See the individual commands for more information."
  19576. (interactive)
  19577. (org-return t))
  19578. (defun org-ctrl-c-star ()
  19579. "Compute table, or change heading status of lines.
  19580. Calls `org-table-recalculate' or `org-toggle-heading',
  19581. depending on context."
  19582. (interactive)
  19583. (cond
  19584. ((org-at-table-p)
  19585. (call-interactively 'org-table-recalculate))
  19586. (t
  19587. ;; Convert all lines in region to list items
  19588. (call-interactively 'org-toggle-heading))))
  19589. (defun org-ctrl-c-minus ()
  19590. "Insert separator line in table or modify bullet status of line.
  19591. Also turns a plain line or a region of lines into list items.
  19592. Calls `org-table-insert-hline', `org-toggle-item', or
  19593. `org-cycle-list-bullet', depending on context."
  19594. (interactive)
  19595. (cond
  19596. ((org-at-table-p)
  19597. (call-interactively 'org-table-insert-hline))
  19598. ((org-region-active-p)
  19599. (call-interactively 'org-toggle-item))
  19600. ((org-in-item-p)
  19601. (call-interactively 'org-cycle-list-bullet))
  19602. (t
  19603. (call-interactively 'org-toggle-item))))
  19604. (defun org-toggle-item (arg)
  19605. "Convert headings or normal lines to items, items to normal lines.
  19606. If there is no active region, only the current line is considered.
  19607. If the first non blank line in the region is a headline, convert
  19608. all headlines to items, shifting text accordingly.
  19609. If it is an item, convert all items to normal lines.
  19610. If it is normal text, change region into a list of items.
  19611. With a prefix argument ARG, change the region in a single item."
  19612. (interactive "P")
  19613. (let ((shift-text
  19614. (function
  19615. ;; Shift text in current section to IND, from point to END.
  19616. ;; The function leaves point to END line.
  19617. (lambda (ind end)
  19618. (let ((min-i 1000) (end (copy-marker end)))
  19619. ;; First determine the minimum indentation (MIN-I) of
  19620. ;; the text.
  19621. (save-excursion
  19622. (catch 'exit
  19623. (while (< (point) end)
  19624. (let ((i (org-get-indentation)))
  19625. (cond
  19626. ;; Skip blank lines and inline tasks.
  19627. ((looking-at "^[ \t]*$"))
  19628. ((looking-at org-outline-regexp-bol))
  19629. ;; We can't find less than 0 indentation.
  19630. ((zerop i) (throw 'exit (setq min-i 0)))
  19631. ((< i min-i) (setq min-i i))))
  19632. (forward-line))))
  19633. ;; Then indent each line so that a line indented to
  19634. ;; MIN-I becomes indented to IND. Ignore blank lines
  19635. ;; and inline tasks in the process.
  19636. (let ((delta (- ind min-i)))
  19637. (while (< (point) end)
  19638. (unless (or (looking-at "^[ \t]*$")
  19639. (looking-at org-outline-regexp-bol))
  19640. (org-indent-line-to (+ (org-get-indentation) delta)))
  19641. (forward-line)))))))
  19642. (skip-blanks
  19643. (function
  19644. ;; Return beginning of first non-blank line, starting from
  19645. ;; line at POS.
  19646. (lambda (pos)
  19647. (save-excursion
  19648. (goto-char pos)
  19649. (skip-chars-forward " \r\t\n")
  19650. (point-at-bol)))))
  19651. beg end)
  19652. ;; Determine boundaries of changes.
  19653. (if (org-region-active-p)
  19654. (setq beg (funcall skip-blanks (region-beginning))
  19655. end (copy-marker (region-end)))
  19656. (setq beg (funcall skip-blanks (point-at-bol))
  19657. end (copy-marker (point-at-eol))))
  19658. ;; Depending on the starting line, choose an action on the text
  19659. ;; between BEG and END.
  19660. (org-with-limited-levels
  19661. (save-excursion
  19662. (goto-char beg)
  19663. (cond
  19664. ;; Case 1. Start at an item: de-itemize. Note that it only
  19665. ;; happens when a region is active: `org-ctrl-c-minus'
  19666. ;; would call `org-cycle-list-bullet' otherwise.
  19667. ((org-at-item-p)
  19668. (while (< (point) end)
  19669. (when (org-at-item-p)
  19670. (skip-chars-forward " \t")
  19671. (delete-region (point) (match-end 0)))
  19672. (forward-line)))
  19673. ;; Case 2. Start at an heading: convert to items.
  19674. ((org-at-heading-p)
  19675. (let* ((bul (org-list-bullet-string "-"))
  19676. (bul-len (length bul))
  19677. (done (org-entry-is-done-p))
  19678. (todo (org-entry-is-todo-p))
  19679. ;; Indentation of the first heading. It should be
  19680. ;; relative to the indentation of its parent, if any.
  19681. (start-ind (save-excursion
  19682. (cond
  19683. ((not org-adapt-indentation) 0)
  19684. ((not (outline-previous-heading)) 0)
  19685. (t (length (match-string 0))))))
  19686. ;; Level of first heading. Further headings will be
  19687. ;; compared to it to determine hierarchy in the list.
  19688. (ref-level (org-reduced-level (org-outline-level))))
  19689. (when (or done todo) (org-todo ""))
  19690. (while (< (point) end)
  19691. (let* ((level (org-reduced-level (org-outline-level)))
  19692. (delta (max 0 (- level ref-level))))
  19693. ;; If current headline is less indented than the first
  19694. ;; one, set it as reference, in order to preserve
  19695. ;; subtrees.
  19696. (when (< level ref-level) (setq ref-level level))
  19697. (replace-match bul t t)
  19698. (org-indent-line-to (+ start-ind (* delta bul-len)))
  19699. (when (or done todo)
  19700. (let* ((struct (org-list-struct))
  19701. (old (copy-tree struct)))
  19702. (org-list-set-checkbox (line-beginning-position)
  19703. struct
  19704. (if done "[X]" "[ ]"))
  19705. (org-list-write-struct struct
  19706. (org-list-parents-alist struct)
  19707. old)))
  19708. ;; Ensure all text down to END (or SECTION-END) belongs
  19709. ;; to the newly created item.
  19710. (let ((section-end (save-excursion
  19711. (or (outline-next-heading) (point)))))
  19712. (forward-line)
  19713. (funcall shift-text
  19714. (+ start-ind (* (1+ delta) bul-len))
  19715. (min end section-end)))))))
  19716. ;; Case 3. Normal line with ARG: make the first line of region
  19717. ;; an item, and shift indentation of others lines to
  19718. ;; set them as item's body.
  19719. (arg (let* ((bul (org-list-bullet-string "-"))
  19720. (bul-len (length bul))
  19721. (ref-ind (org-get-indentation)))
  19722. (skip-chars-forward " \t")
  19723. (insert bul)
  19724. (forward-line)
  19725. (while (< (point) end)
  19726. ;; Ensure that lines less indented than first one
  19727. ;; still get included in item body.
  19728. (funcall shift-text
  19729. (+ ref-ind bul-len)
  19730. (min end (save-excursion (or (outline-next-heading)
  19731. (point)))))
  19732. (forward-line))))
  19733. ;; Case 4. Normal line without ARG: turn each non-item line
  19734. ;; into an item.
  19735. (t
  19736. (while (< (point) end)
  19737. (unless (or (org-at-heading-p) (org-at-item-p))
  19738. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  19739. (replace-match
  19740. (concat "\\1" (org-list-bullet-string "-") "\\2"))))
  19741. (forward-line))))))))
  19742. (defun org-toggle-heading (&optional nstars)
  19743. "Convert headings to normal text, or items or text to headings.
  19744. If there is no active region, only convert the current line.
  19745. With a \\[universal-argument] prefix, convert the whole list at
  19746. point into heading.
  19747. In a region:
  19748. - If the first non blank line is a headline, remove the stars
  19749. from all headlines in the region.
  19750. - If it is a normal line, turn each and every normal line (i.e.,
  19751. not an heading or an item) in the region into headings. If you
  19752. want to convert only the first line of this region, use one
  19753. universal prefix argument.
  19754. - If it is a plain list item, turn all plain list items into headings.
  19755. When converting a line into a heading, the number of stars is chosen
  19756. such that the lines become children of the current entry. However,
  19757. when a numeric prefix argument is given, its value determines the
  19758. number of stars to add."
  19759. (interactive "P")
  19760. (let ((skip-blanks
  19761. (function
  19762. ;; Return beginning of first non-blank line, starting from
  19763. ;; line at POS.
  19764. (lambda (pos)
  19765. (save-excursion
  19766. (goto-char pos)
  19767. (while (org-at-comment-p) (forward-line))
  19768. (skip-chars-forward " \r\t\n")
  19769. (point-at-bol)))))
  19770. beg end toggled)
  19771. ;; Determine boundaries of changes. If a universal prefix has
  19772. ;; been given, put the list in a region. If region ends at a bol,
  19773. ;; do not consider the last line to be in the region.
  19774. (when (and current-prefix-arg (org-at-item-p))
  19775. (if (listp current-prefix-arg) (setq current-prefix-arg 1))
  19776. (org-mark-element))
  19777. (if (org-region-active-p)
  19778. (setq beg (funcall skip-blanks (region-beginning))
  19779. end (copy-marker (save-excursion
  19780. (goto-char (region-end))
  19781. (if (bolp) (point) (point-at-eol)))))
  19782. (setq beg (funcall skip-blanks (point-at-bol))
  19783. end (copy-marker (point-at-eol))))
  19784. ;; Ensure inline tasks don't count as headings.
  19785. (org-with-limited-levels
  19786. (save-excursion
  19787. (goto-char beg)
  19788. (cond
  19789. ;; Case 1. Started at an heading: de-star headings.
  19790. ((org-at-heading-p)
  19791. (while (< (point) end)
  19792. (when (org-at-heading-p t)
  19793. (looking-at org-outline-regexp) (replace-match "")
  19794. (setq toggled t))
  19795. (forward-line)))
  19796. ;; Case 2. Started at an item: change items into headlines.
  19797. ;; One star will be added by `org-list-to-subtree'.
  19798. ((org-at-item-p)
  19799. (while (< (point) end)
  19800. (when (org-at-item-p)
  19801. ;; Pay attention to cases when region ends before list.
  19802. (let* ((struct (org-list-struct))
  19803. (list-end (min (org-list-get-bottom-point struct) (1+ end))))
  19804. (save-restriction
  19805. (narrow-to-region (point) list-end)
  19806. (insert (org-list-to-subtree (org-list-parse-list t)))))
  19807. (setq toggled t))
  19808. (forward-line)))
  19809. ;; Case 3. Started at normal text: make every line an heading,
  19810. ;; skipping headlines and items.
  19811. (t (let* ((stars
  19812. (make-string
  19813. (if (numberp nstars) nstars (or (org-current-level) 0)) ?*))
  19814. (add-stars
  19815. (cond (nstars "") ; stars from prefix only
  19816. ((equal stars "") "*") ; before first heading
  19817. (org-odd-levels-only "**") ; inside heading, odd
  19818. (t "*"))) ; inside heading, oddeven
  19819. (rpl (concat stars add-stars " "))
  19820. (lend (if (listp nstars) (save-excursion (end-of-line) (point)))))
  19821. (while (< (point) (if (equal nstars '(4)) lend end))
  19822. (when (and (not (or (org-at-heading-p) (org-at-item-p) (org-at-comment-p)))
  19823. (looking-at "\\([ \t]*\\)\\(\\S-\\)"))
  19824. (replace-match (concat rpl (match-string 2))) (setq toggled t))
  19825. (forward-line)))))))
  19826. (unless toggled (message "Cannot toggle heading from here"))))
  19827. (defun org-meta-return (&optional _arg)
  19828. "Insert a new heading or wrap a region in a table.
  19829. Calls `org-insert-heading' or `org-table-wrap-region', depending
  19830. on context. See the individual commands for more information."
  19831. (interactive)
  19832. (org-check-before-invisible-edit 'insert)
  19833. (or (run-hook-with-args-until-success 'org-metareturn-hook)
  19834. (call-interactively (if (org-at-table-p) #'org-table-wrap-region
  19835. #'org-insert-heading))))
  19836. ;;; Menu entries
  19837. (defsubst org-in-subtree-not-table-p ()
  19838. "Are we in a subtree and not in a table?"
  19839. (and (not (org-before-first-heading-p))
  19840. (not (org-at-table-p))))
  19841. ;; Define the Org-mode menus
  19842. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  19843. '("Tbl"
  19844. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  19845. ["Next Field" org-cycle (org-at-table-p)]
  19846. ["Previous Field" org-shifttab (org-at-table-p)]
  19847. ["Next Row" org-return (org-at-table-p)]
  19848. "--"
  19849. ["Blank Field" org-table-blank-field (org-at-table-p)]
  19850. ["Edit Field" org-table-edit-field (org-at-table-p)]
  19851. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  19852. "--"
  19853. ("Column"
  19854. ["Move Column Left" org-metaleft (org-at-table-p)]
  19855. ["Move Column Right" org-metaright (org-at-table-p)]
  19856. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  19857. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  19858. ("Row"
  19859. ["Move Row Up" org-metaup (org-at-table-p)]
  19860. ["Move Row Down" org-metadown (org-at-table-p)]
  19861. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  19862. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  19863. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  19864. "--"
  19865. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  19866. ("Rectangle"
  19867. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  19868. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  19869. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  19870. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  19871. "--"
  19872. ("Calculate"
  19873. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  19874. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  19875. ["Edit Formulas" org-edit-special (org-at-table-p)]
  19876. "--"
  19877. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  19878. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  19879. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  19880. "--"
  19881. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  19882. "--"
  19883. ["Sum Column/Rectangle" org-table-sum
  19884. (or (org-at-table-p) (org-region-active-p))]
  19885. ["Which Column?" org-table-current-column (org-at-table-p)])
  19886. ["Debug Formulas"
  19887. org-table-toggle-formula-debugger
  19888. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  19889. ["Show Col/Row Numbers"
  19890. org-table-toggle-coordinate-overlays
  19891. :style toggle
  19892. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  19893. "--"
  19894. ["Create" org-table-create (and (not (org-at-table-p))
  19895. org-enable-table-editor)]
  19896. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  19897. ["Import from File" org-table-import (not (org-at-table-p))]
  19898. ["Export to File" org-table-export (org-at-table-p)]
  19899. "--"
  19900. ["Create/Convert from/to table.el" org-table-create-with-table.el t]
  19901. "--"
  19902. ("Plot"
  19903. ["Ascii plot" orgtbl-ascii-plot :active (org-at-table-p) :keys "C-c \" a"]
  19904. ["Gnuplot" org-plot/gnuplot :active (org-at-table-p) :keys "C-c \" g"])))
  19905. (easy-menu-define org-org-menu org-mode-map "Org menu"
  19906. '("Org"
  19907. ("Show/Hide"
  19908. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  19909. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  19910. ["Sparse Tree..." org-sparse-tree t]
  19911. ["Reveal Context" org-reveal t]
  19912. ["Show All" show-all t]
  19913. "--"
  19914. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  19915. "--"
  19916. ["New Heading" org-insert-heading t]
  19917. ("Navigate Headings"
  19918. ["Up" outline-up-heading t]
  19919. ["Next" outline-next-visible-heading t]
  19920. ["Previous" outline-previous-visible-heading t]
  19921. ["Next Same Level" outline-forward-same-level t]
  19922. ["Previous Same Level" outline-backward-same-level t]
  19923. "--"
  19924. ["Jump" org-goto t])
  19925. ("Edit Structure"
  19926. ["Refile Subtree" org-refile (org-in-subtree-not-table-p)]
  19927. "--"
  19928. ["Move Subtree Up" org-metaup (org-at-heading-p)]
  19929. ["Move Subtree Down" org-metadown (org-at-heading-p)]
  19930. "--"
  19931. ["Copy Subtree" org-copy-special (org-in-subtree-not-table-p)]
  19932. ["Cut Subtree" org-cut-special (org-in-subtree-not-table-p)]
  19933. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  19934. "--"
  19935. ["Clone subtree, shift time" org-clone-subtree-with-time-shift t]
  19936. "--"
  19937. ["Copy visible text" org-copy-visible t]
  19938. "--"
  19939. ["Promote Heading" org-metaleft (org-in-subtree-not-table-p)]
  19940. ["Promote Subtree" org-shiftmetaleft (org-in-subtree-not-table-p)]
  19941. ["Demote Heading" org-metaright (org-in-subtree-not-table-p)]
  19942. ["Demote Subtree" org-shiftmetaright (org-in-subtree-not-table-p)]
  19943. "--"
  19944. ["Sort Region/Children" org-sort t]
  19945. "--"
  19946. ["Convert to odd levels" org-convert-to-odd-levels t]
  19947. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  19948. ("Editing"
  19949. ["Emphasis..." org-emphasize t]
  19950. ["Edit Source Example" org-edit-special t]
  19951. "--"
  19952. ["Footnote new/jump" org-footnote-action t]
  19953. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  19954. ("Archive"
  19955. ["Archive (default method)" org-archive-subtree-default (org-in-subtree-not-table-p)]
  19956. "--"
  19957. ["Move Subtree to Archive file" org-archive-subtree (org-in-subtree-not-table-p)]
  19958. ["Toggle ARCHIVE tag" org-toggle-archive-tag (org-in-subtree-not-table-p)]
  19959. ["Move subtree to Archive sibling" org-archive-to-archive-sibling (org-in-subtree-not-table-p)]
  19960. )
  19961. "--"
  19962. ("Hyperlinks"
  19963. ["Store Link (Global)" org-store-link t]
  19964. ["Find existing link to here" org-occur-link-in-agenda-files t]
  19965. ["Insert Link" org-insert-link t]
  19966. ["Follow Link" org-open-at-point t]
  19967. "--"
  19968. ["Next link" org-next-link t]
  19969. ["Previous link" org-previous-link t]
  19970. "--"
  19971. ["Descriptive Links"
  19972. org-toggle-link-display
  19973. :style radio
  19974. :selected org-descriptive-links
  19975. ]
  19976. ["Literal Links"
  19977. org-toggle-link-display
  19978. :style radio
  19979. :selected (not org-descriptive-links)])
  19980. "--"
  19981. ("TODO Lists"
  19982. ["TODO/DONE/-" org-todo t]
  19983. ("Select keyword"
  19984. ["Next keyword" org-shiftright (org-at-heading-p)]
  19985. ["Previous keyword" org-shiftleft (org-at-heading-p)]
  19986. ["Complete Keyword" pcomplete (assq :todo-keyword (org-context))]
  19987. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))]
  19988. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-at-heading-p))])
  19989. ["Show TODO Tree" org-show-todo-tree :active t :keys "C-c / t"]
  19990. ["Global TODO list" org-todo-list :active t :keys "C-c a t"]
  19991. "--"
  19992. ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
  19993. :selected org-enforce-todo-dependencies :style toggle :active t]
  19994. "Settings for tree at point"
  19995. ["Do Children sequentially" org-toggle-ordered-property :style radio
  19996. :selected (org-entry-get nil "ORDERED")
  19997. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  19998. ["Do Children parallel" org-toggle-ordered-property :style radio
  19999. :selected (not (org-entry-get nil "ORDERED"))
  20000. :active org-enforce-todo-dependencies :keys "C-c C-x o"]
  20001. "--"
  20002. ["Set Priority" org-priority t]
  20003. ["Priority Up" org-shiftup t]
  20004. ["Priority Down" org-shiftdown t]
  20005. "--"
  20006. ["Get news from all feeds" org-feed-update-all t]
  20007. ["Go to the inbox of a feed..." org-feed-goto-inbox t]
  20008. ["Customize feeds" (customize-variable 'org-feed-alist) t])
  20009. ("TAGS and Properties"
  20010. ["Set Tags" org-set-tags-command (not (org-before-first-heading-p))]
  20011. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  20012. "--"
  20013. ["Set property" org-set-property (not (org-before-first-heading-p))]
  20014. ["Column view of properties" org-columns t]
  20015. ["Insert Column View DBlock" org-insert-columns-dblock t])
  20016. ("Dates and Scheduling"
  20017. ["Timestamp" org-time-stamp (not (org-before-first-heading-p))]
  20018. ["Timestamp (inactive)" org-time-stamp-inactive (not (org-before-first-heading-p))]
  20019. ("Change Date"
  20020. ["1 Day Later" org-shiftright (org-at-timestamp-p)]
  20021. ["1 Day Earlier" org-shiftleft (org-at-timestamp-p)]
  20022. ["1 ... Later" org-shiftup (org-at-timestamp-p)]
  20023. ["1 ... Earlier" org-shiftdown (org-at-timestamp-p)])
  20024. ["Compute Time Range" org-evaluate-time-range t]
  20025. ["Schedule Item" org-schedule (not (org-before-first-heading-p))]
  20026. ["Deadline" org-deadline (not (org-before-first-heading-p))]
  20027. "--"
  20028. ["Custom time format" org-toggle-time-stamp-overlays
  20029. :style radio :selected org-display-custom-times]
  20030. "--"
  20031. ["Goto Calendar" org-goto-calendar t]
  20032. ["Date from Calendar" org-date-from-calendar t]
  20033. "--"
  20034. ["Start/Restart Timer" org-timer-start t]
  20035. ["Pause/Continue Timer" org-timer-pause-or-continue t]
  20036. ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
  20037. ["Insert Timer String" org-timer t]
  20038. ["Insert Timer Item" org-timer-item t])
  20039. ("Logging work"
  20040. ["Clock in" org-clock-in :active t :keys "C-c C-x C-i"]
  20041. ["Switch task" (lambda () (interactive) (org-clock-in '(4))) :active t :keys "C-u C-c C-x C-i"]
  20042. ["Clock out" org-clock-out t]
  20043. ["Clock cancel" org-clock-cancel t]
  20044. "--"
  20045. ["Mark as default task" org-clock-mark-default-task t]
  20046. ["Clock in, mark as default" (lambda () (interactive) (org-clock-in '(16))) :active t :keys "C-u C-u C-c C-x C-i"]
  20047. ["Goto running clock" org-clock-goto t]
  20048. "--"
  20049. ["Display times" org-clock-display t]
  20050. ["Create clock table" org-clock-report t]
  20051. "--"
  20052. ["Record DONE time"
  20053. (progn (setq org-log-done (not org-log-done))
  20054. (message "Switching to %s will %s record a timestamp"
  20055. (car org-done-keywords)
  20056. (if org-log-done "automatically" "not")))
  20057. :style toggle :selected org-log-done])
  20058. "--"
  20059. ["Agenda Command..." org-agenda t]
  20060. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  20061. ("File List for Agenda")
  20062. ("Special views current file"
  20063. ["TODO Tree" org-show-todo-tree t]
  20064. ["Check Deadlines" org-check-deadlines t]
  20065. ["Timeline" org-timeline t]
  20066. ["Tags/Property tree" org-match-sparse-tree t])
  20067. "--"
  20068. ["Export/Publish..." org-export-dispatch t]
  20069. ("LaTeX"
  20070. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  20071. :selected org-cdlatex-mode]
  20072. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  20073. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  20074. ["Modify math symbol" org-cdlatex-math-modify
  20075. (org-inside-LaTeX-fragment-p)]
  20076. ["Insert citation" org-reftex-citation t]
  20077. "--"
  20078. ["Template for BEAMER" (org-beamer-insert-options-template) t])
  20079. "--"
  20080. ("MobileOrg"
  20081. ["Push Files and Views" org-mobile-push t]
  20082. ["Get Captured and Flagged" org-mobile-pull t]
  20083. ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
  20084. "--"
  20085. ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
  20086. "--"
  20087. ("Documentation"
  20088. ["Show Version" org-version t]
  20089. ["Info Documentation" org-info t])
  20090. ("Customize"
  20091. ["Browse Org Group" org-customize t]
  20092. "--"
  20093. ["Expand This Menu" org-create-customize-menu
  20094. (fboundp 'customize-menu-create)])
  20095. ["Send bug report" org-submit-bug-report t]
  20096. "--"
  20097. ("Refresh/Reload"
  20098. ["Refresh setup current buffer" org-mode-restart t]
  20099. ["Reload Org (after update)" org-reload t]
  20100. ["Reload Org uncompiled" (org-reload t) :active t :keys "C-u C-c C-x !"])
  20101. ))
  20102. (defun org-info (&optional node)
  20103. "Read documentation for Org-mode in the info system.
  20104. With optional NODE, go directly to that node."
  20105. (interactive)
  20106. (info (format "(org)%s" (or node ""))))
  20107. ;;;###autoload
  20108. (defun org-submit-bug-report ()
  20109. "Submit a bug report on Org-mode via mail.
  20110. Don't hesitate to report any problems or inaccurate documentation.
  20111. If you don't have setup sending mail from (X)Emacs, please copy the
  20112. output buffer into your mail program, as it gives us important
  20113. information about your Org-mode version and configuration."
  20114. (interactive)
  20115. (require 'reporter)
  20116. (defvar reporter-prompt-for-summary-p)
  20117. (org-load-modules-maybe)
  20118. (org-require-autoloaded-modules)
  20119. (let ((reporter-prompt-for-summary-p "Bug report subject: "))
  20120. (reporter-submit-bug-report
  20121. "emacs-orgmode@gnu.org"
  20122. (org-version nil 'full)
  20123. (let (list)
  20124. (save-window-excursion
  20125. (org-pop-to-buffer-same-window (get-buffer-create "*Warn about privacy*"))
  20126. (delete-other-windows)
  20127. (erase-buffer)
  20128. (insert "You are about to submit a bug report to the Org-mode mailing list.
  20129. We would like to add your full Org-mode and Outline configuration to the
  20130. bug report. This greatly simplifies the work of the maintainer and
  20131. other experts on the mailing list.
  20132. HOWEVER, some variables you have customized may contain private
  20133. information. The names of customers, colleagues, or friends, might
  20134. appear in the form of file names, tags, todo states, or search strings.
  20135. If you answer yes to the prompt, you might want to check and remove
  20136. such private information before sending the email.")
  20137. (add-text-properties (point-min) (point-max) '(face org-warning))
  20138. (when (yes-or-no-p "Include your Org-mode configuration ")
  20139. (mapatoms
  20140. (lambda (v)
  20141. (and (boundp v)
  20142. (string-match "\\`\\(org-\\|outline-\\)" (symbol-name v))
  20143. (or (and (symbol-value v)
  20144. (string-match "\\(-hook\\|-function\\)\\'" (symbol-name v)))
  20145. (and
  20146. (get v 'custom-type) (get v 'standard-value)
  20147. (not (equal (symbol-value v) (eval (car (get v 'standard-value)))))))
  20148. (push v list)))))
  20149. (kill-buffer (get-buffer "*Warn about privacy*"))
  20150. list))
  20151. nil nil
  20152. "Remember to cover the basics, that is, what you expected to happen and
  20153. what in fact did happen. You don't know how to make a good report? See
  20154. http://orgmode.org/manual/Feedback.html#Feedback
  20155. Your bug report will be posted to the Org-mode mailing list.
  20156. ------------------------------------------------------------------------")
  20157. (save-excursion
  20158. (if (re-search-backward "^\\(Subject: \\)Org-mode version \\(.*?\\);[ \t]*\\(.*\\)" nil t)
  20159. (replace-match "\\1Bug: \\3 [\\2]")))))
  20160. (defun org-install-agenda-files-menu ()
  20161. (let ((bl (buffer-list)))
  20162. (save-excursion
  20163. (while bl
  20164. (set-buffer (pop bl))
  20165. (if (derived-mode-p 'org-mode) (setq bl nil)))
  20166. (when (derived-mode-p 'org-mode)
  20167. (easy-menu-change
  20168. '("Org") "File List for Agenda"
  20169. (append
  20170. (list
  20171. ["Edit File List" (org-edit-agenda-file-list) t]
  20172. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  20173. ["Remove Current File from List" org-remove-file t]
  20174. ["Cycle through agenda files" org-cycle-agenda-files t]
  20175. ["Occur in all agenda files" org-occur-in-agenda-files t]
  20176. "--")
  20177. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  20178. ;;;; Documentation
  20179. (defun org-require-autoloaded-modules ()
  20180. (interactive)
  20181. (mapc 'require
  20182. '(org-agenda org-archive org-attach org-clock org-colview org-id
  20183. org-table org-timer)))
  20184. ;;;###autoload
  20185. (defun org-reload (&optional uncompiled)
  20186. "Reload all org lisp files.
  20187. With prefix arg UNCOMPILED, load the uncompiled versions."
  20188. (interactive "P")
  20189. (require 'loadhist)
  20190. (let* ((org-dir (org-find-library-dir "org"))
  20191. (contrib-dir (or (org-find-library-dir "org-contribdir") org-dir))
  20192. (feature-re "^\\(org\\|ob\\|ox\\)\\(-.*\\)?")
  20193. (remove-re (mapconcat 'identity
  20194. (mapcar (lambda (f) (concat "^" f "$"))
  20195. (list (if (featurep 'xemacs)
  20196. "org-colview"
  20197. "org-colview-xemacs")
  20198. "org" "org-loaddefs" "org-version"))
  20199. "\\|"))
  20200. (feats (delete-dups
  20201. (mapcar 'file-name-sans-extension
  20202. (mapcar 'file-name-nondirectory
  20203. (delq nil
  20204. (mapcar 'feature-file
  20205. features))))))
  20206. (lfeat (append
  20207. (sort
  20208. (setq feats
  20209. (delq nil (mapcar
  20210. (lambda (f)
  20211. (if (and (string-match feature-re f)
  20212. (not (string-match remove-re f)))
  20213. f nil))
  20214. feats)))
  20215. 'string-lessp)
  20216. (list "org-version" "org")))
  20217. (load-suffixes (when (boundp 'load-suffixes) load-suffixes))
  20218. (load-suffixes (if uncompiled (reverse load-suffixes) load-suffixes))
  20219. load-uncore load-misses)
  20220. (setq load-misses
  20221. (delq 't
  20222. (mapcar (lambda (f)
  20223. (or (org-load-noerror-mustsuffix (concat org-dir f))
  20224. (and (string= org-dir contrib-dir)
  20225. (org-load-noerror-mustsuffix (concat contrib-dir f)))
  20226. (and (org-load-noerror-mustsuffix (concat (org-find-library-dir f) f))
  20227. (add-to-list 'load-uncore f 'append)
  20228. 't)
  20229. f))
  20230. lfeat)))
  20231. (if load-uncore
  20232. (message "The following feature%s found in load-path, please check if that's correct:\n%s"
  20233. (if (> (length load-uncore) 1) "s were" " was") load-uncore))
  20234. (if load-misses
  20235. (message "Some error occurred while reloading Org feature%s\n%s\nPlease check *Messages*!\n%s"
  20236. (if (> (length load-misses) 1) "s" "") load-misses (org-version nil 'full))
  20237. (message "Successfully reloaded Org\n%s" (org-version nil 'full)))))
  20238. ;;;###autoload
  20239. (defun org-customize ()
  20240. "Call the customize function with org as argument."
  20241. (interactive)
  20242. (org-load-modules-maybe)
  20243. (org-require-autoloaded-modules)
  20244. (customize-browse 'org))
  20245. (defun org-create-customize-menu ()
  20246. "Create a full customization menu for Org-mode, insert it into the menu."
  20247. (interactive)
  20248. (org-load-modules-maybe)
  20249. (org-require-autoloaded-modules)
  20250. (if (fboundp 'customize-menu-create)
  20251. (progn
  20252. (easy-menu-change
  20253. '("Org") "Customize"
  20254. `(["Browse Org group" org-customize t]
  20255. "--"
  20256. ,(customize-menu-create 'org)
  20257. ["Set" Custom-set t]
  20258. ["Save" Custom-save t]
  20259. ["Reset to Current" Custom-reset-current t]
  20260. ["Reset to Saved" Custom-reset-saved t]
  20261. ["Reset to Standard Settings" Custom-reset-standard t]))
  20262. (message "\"Org\"-menu now contains full customization menu"))
  20263. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  20264. ;;;; Miscellaneous stuff
  20265. ;;; Generally useful functions
  20266. (defsubst org-get-at-eol (property n)
  20267. "Get text property PROPERTY at the end of line less N characters."
  20268. (get-text-property (- (point-at-eol) n) property))
  20269. (defun org-find-text-property-in-string (prop s)
  20270. "Return the first non-nil value of property PROP in string S."
  20271. (or (get-text-property 0 prop s)
  20272. (get-text-property (or (next-single-property-change 0 prop s) 0)
  20273. prop s)))
  20274. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  20275. "Display the given MESSAGE as a warning."
  20276. (if (fboundp 'display-warning)
  20277. (display-warning 'org message
  20278. (if (featurep 'xemacs) 'warning :warning))
  20279. (let ((buf (get-buffer-create "*Org warnings*")))
  20280. (with-current-buffer buf
  20281. (goto-char (point-max))
  20282. (insert "Warning (Org): " message)
  20283. (unless (bolp)
  20284. (newline)))
  20285. (display-buffer buf)
  20286. (sit-for 0))))
  20287. (defun org-eval (form)
  20288. "Eval FORM and return result."
  20289. (condition-case error
  20290. (eval form)
  20291. (error (format "%%![Error: %s]" error))))
  20292. (defun org-in-clocktable-p ()
  20293. "Check if the cursor is in a clocktable."
  20294. (let ((pos (point)) start)
  20295. (save-excursion
  20296. (end-of-line 1)
  20297. (and (re-search-backward "^[ \t]*#\\+BEGIN:[ \t]+clocktable" nil t)
  20298. (setq start (match-beginning 0))
  20299. (re-search-forward "^[ \t]*#\\+END:.*" nil t)
  20300. (>= (match-end 0) pos)
  20301. start))))
  20302. (defun org-in-verbatim-emphasis ()
  20303. (save-match-data
  20304. (and (org-in-regexp org-emph-re 2)
  20305. (>= (point) (match-beginning 3))
  20306. (<= (point) (match-end 4))
  20307. (member (match-string 3) '("=" "~")))))
  20308. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  20309. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  20310. (if (and marker (marker-buffer marker)
  20311. (buffer-live-p (marker-buffer marker)))
  20312. (progn
  20313. (org-pop-to-buffer-same-window (marker-buffer marker))
  20314. (if (or (> marker (point-max)) (< marker (point-min)))
  20315. (widen))
  20316. (goto-char marker)
  20317. (org-show-context 'org-goto))
  20318. (if bookmark
  20319. (bookmark-jump bookmark)
  20320. (error "Cannot find location"))))
  20321. (defun org-quote-csv-field (s)
  20322. "Quote field for inclusion in CSV material."
  20323. (if (string-match "[\",]" s)
  20324. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  20325. s))
  20326. (defun org-force-self-insert (N)
  20327. "Needed to enforce self-insert under remapping."
  20328. (interactive "p")
  20329. (self-insert-command N))
  20330. (defun org-string-width (s)
  20331. "Compute width of string, ignoring invisible characters.
  20332. This ignores character with invisibility property `org-link', and also
  20333. characters with property `org-cwidth', because these will become invisible
  20334. upon the next fontification round."
  20335. (let (b l)
  20336. (when (or (eq t buffer-invisibility-spec)
  20337. (assq 'org-link buffer-invisibility-spec))
  20338. (while (setq b (text-property-any 0 (length s)
  20339. 'invisible 'org-link s))
  20340. (setq s (concat (substring s 0 b)
  20341. (substring s (or (next-single-property-change
  20342. b 'invisible s)
  20343. (length s)))))))
  20344. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  20345. (setq s (concat (substring s 0 b)
  20346. (substring s (or (next-single-property-change
  20347. b 'org-cwidth s)
  20348. (length s))))))
  20349. (setq l (string-width s) b -1)
  20350. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  20351. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  20352. l))
  20353. (defun org-shorten-string (s maxlength)
  20354. "Shorten string S so tht it is no longer than MAXLENGTH characters.
  20355. If the string is shorter or has length MAXLENGTH, just return the
  20356. original string. If it is longer, the functions finds a space in the
  20357. string, breaks this string off at that locations and adds three dots
  20358. as ellipsis. Including the ellipsis, the string will not be longer
  20359. than MAXLENGTH. If finding a good breaking point in the string does
  20360. not work, the string is just chopped off in the middle of a word
  20361. if necessary."
  20362. (if (<= (length s) maxlength)
  20363. s
  20364. (let* ((n (max (- maxlength 4) 1))
  20365. (re (concat "\\`\\(.\\{1," (int-to-string n) "\\}[^ ]\\)\\([ ]\\|\\'\\)")))
  20366. (if (string-match re s)
  20367. (concat (match-string 1 s) "...")
  20368. (concat (substring s 0 (max (- maxlength 3) 0)) "...")))))
  20369. (defun org-get-indentation (&optional line)
  20370. "Get the indentation of the current line, interpreting tabs.
  20371. When LINE is given, assume it represents a line and compute its indentation."
  20372. (if line
  20373. (if (string-match "^ *" (org-remove-tabs line))
  20374. (match-end 0))
  20375. (save-excursion
  20376. (beginning-of-line 1)
  20377. (skip-chars-forward " \t")
  20378. (current-column))))
  20379. (defun org-get-string-indentation (s)
  20380. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  20381. (let ((n -1) (i 0) (w tab-width) c)
  20382. (catch 'exit
  20383. (while (< (setq n (1+ n)) (length s))
  20384. (setq c (aref s n))
  20385. (cond ((= c ?\ ) (setq i (1+ i)))
  20386. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  20387. (t (throw 'exit t)))))
  20388. i))
  20389. (defun org-remove-tabs (s &optional width)
  20390. "Replace tabulators in S with spaces.
  20391. Assumes that s is a single line, starting in column 0."
  20392. (setq width (or width tab-width))
  20393. (while (string-match "\t" s)
  20394. (setq s (replace-match
  20395. (make-string
  20396. (- (* width (/ (+ (match-beginning 0) width) width))
  20397. (match-beginning 0)) ?\ )
  20398. t t s)))
  20399. s)
  20400. (defun org-fix-indentation (line ind)
  20401. "Fix indentation in LINE.
  20402. IND is a cons cell with target and minimum indentation.
  20403. If the current indentation in LINE is smaller than the minimum,
  20404. leave it alone. If it is larger than ind, set it to the target."
  20405. (let* ((l (org-remove-tabs line))
  20406. (i (org-get-indentation l))
  20407. (i1 (car ind)) (i2 (cdr ind)))
  20408. (if (>= i i2) (setq l (substring line i2)))
  20409. (if (> i1 0)
  20410. (concat (make-string i1 ?\ ) l)
  20411. l)))
  20412. (defun org-remove-indentation (code &optional n)
  20413. "Remove the maximum common indentation from the lines in CODE.
  20414. N may optionally be the number of spaces to remove."
  20415. (with-temp-buffer
  20416. (insert code)
  20417. (org-do-remove-indentation n)
  20418. (buffer-string)))
  20419. (defun org-do-remove-indentation (&optional n)
  20420. "Remove the maximum common indentation from the buffer."
  20421. (untabify (point-min) (point-max))
  20422. (let ((min 10000) re)
  20423. (if n
  20424. (setq min n)
  20425. (goto-char (point-min))
  20426. (while (re-search-forward "^ *[^ \n]" nil t)
  20427. (setq min (min min (1- (- (match-end 0) (match-beginning 0)))))))
  20428. (unless (or (= min 0) (= min 10000))
  20429. (setq re (format "^ \\{%d\\}" min))
  20430. (goto-char (point-min))
  20431. (while (re-search-forward re nil t)
  20432. (replace-match "")
  20433. (end-of-line 1))
  20434. min)))
  20435. (defun org-fill-template (template alist)
  20436. "Find each %key of ALIST in TEMPLATE and replace it."
  20437. (let ((case-fold-search nil))
  20438. (dolist (entry (sort (copy-sequence alist)
  20439. (lambda (a b) (< (length (car a)) (length (car b))))))
  20440. (setq template
  20441. (replace-regexp-in-string
  20442. (concat "%" (regexp-quote (car entry)))
  20443. (or (cdr entry) "") template t t)))
  20444. template))
  20445. (defun org-base-buffer (buffer)
  20446. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  20447. (if (not buffer)
  20448. buffer
  20449. (or (buffer-base-buffer buffer)
  20450. buffer)))
  20451. (defun org-wrap (string &optional width lines)
  20452. "Wrap string to either a number of lines, or a width in characters.
  20453. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  20454. that costs. If there is a word longer than WIDTH, the text is actually
  20455. wrapped to the length of that word.
  20456. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  20457. many lines, whatever width that takes.
  20458. The return value is a list of lines, without newlines at the end."
  20459. (let* ((words (org-split-string string "[ \t\n]+"))
  20460. (maxword (apply 'max (mapcar 'org-string-width words)))
  20461. w ll)
  20462. (cond (width
  20463. (org-do-wrap words (max maxword width)))
  20464. (lines
  20465. (setq w maxword)
  20466. (setq ll (org-do-wrap words maxword))
  20467. (if (<= (length ll) lines)
  20468. ll
  20469. (setq ll words)
  20470. (while (> (length ll) lines)
  20471. (setq w (1+ w))
  20472. (setq ll (org-do-wrap words w)))
  20473. ll))
  20474. (t (error "Cannot wrap this")))))
  20475. (defun org-do-wrap (words width)
  20476. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  20477. (let (lines line)
  20478. (while words
  20479. (setq line (pop words))
  20480. (while (and words (< (+ (length line) (length (car words))) width))
  20481. (setq line (concat line " " (pop words))))
  20482. (setq lines (push line lines)))
  20483. (nreverse lines)))
  20484. (defun org-split-string (string &optional separators)
  20485. "Splits STRING into substrings at SEPARATORS.
  20486. SEPARATORS is a regular expression.
  20487. No empty strings are returned if there are matches at the beginning
  20488. and end of string."
  20489. ;; FIXME: why not use (split-string STRING SEPARATORS t)?
  20490. (let ((start 0) notfirst list)
  20491. (while (and (string-match (or separators "[ \f\t\n\r\v]+") string
  20492. (if (and notfirst
  20493. (= start (match-beginning 0))
  20494. (< start (length string)))
  20495. (1+ start) start))
  20496. (< (match-beginning 0) (length string)))
  20497. (setq notfirst t)
  20498. (or (eq (match-beginning 0) 0)
  20499. (and (eq (match-beginning 0) (match-end 0))
  20500. (eq (match-beginning 0) start))
  20501. (push (substring string start (match-beginning 0)) list))
  20502. (setq start (match-end 0)))
  20503. (or (eq start (length string))
  20504. (push (substring string start) list))
  20505. (nreverse list)))
  20506. (defun org-quote-vert (s)
  20507. "Replace \"|\" with \"\\vert\"."
  20508. (while (string-match "|" s)
  20509. (setq s (replace-match "\\vert" t t s)))
  20510. s)
  20511. (defun org-uuidgen-p (s)
  20512. "Is S an ID created by UUIDGEN?"
  20513. (string-match "\\`[0-9a-f]\\{8\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{4\\}-[0-9a-f]\\{12\\}\\'" (downcase s)))
  20514. (defun org-in-src-block-p (&optional inside)
  20515. "Whether point is in a code source block.
  20516. When INSIDE is non-nil, don't consider we are within a src block
  20517. when point is at #+BEGIN_SRC or #+END_SRC."
  20518. (let ((case-fold-search t) ov)
  20519. (or (and (eq (get-char-property (point) 'src-block) t))
  20520. (and (not inside)
  20521. (save-match-data
  20522. (save-excursion
  20523. (beginning-of-line)
  20524. (looking-at ".*#\\+\\(begin\\|end\\)_src")))))))
  20525. (defun org-context ()
  20526. "Return a list of contexts of the current cursor position.
  20527. If several contexts apply, all are returned.
  20528. Each context entry is a list with a symbol naming the context, and
  20529. two positions indicating start and end of the context. Possible
  20530. contexts are:
  20531. :headline anywhere in a headline
  20532. :headline-stars on the leading stars in a headline
  20533. :todo-keyword on a TODO keyword (including DONE) in a headline
  20534. :tags on the TAGS in a headline
  20535. :priority on the priority cookie in a headline
  20536. :item on the first line of a plain list item
  20537. :item-bullet on the bullet/number of a plain list item
  20538. :checkbox on the checkbox in a plain list item
  20539. :table in an org-mode table
  20540. :table-special on a special filed in a table
  20541. :table-table in a table.el table
  20542. :clocktable in a clocktable
  20543. :src-block in a source block
  20544. :link on a hyperlink
  20545. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE, COMMENT.
  20546. :target on a <<target>>
  20547. :radio-target on a <<<radio-target>>>
  20548. :latex-fragment on a LaTeX fragment
  20549. :latex-preview on a LaTeX fragment with overlaid preview image
  20550. This function expects the position to be visible because it uses font-lock
  20551. faces as a help to recognize the following contexts: :table-special, :link,
  20552. and :keyword."
  20553. (let* ((f (get-text-property (point) 'face))
  20554. (faces (if (listp f) f (list f)))
  20555. (case-fold-search t)
  20556. (p (point)) clist o)
  20557. ;; First the large context
  20558. (cond
  20559. ((org-at-heading-p t)
  20560. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  20561. (when (progn
  20562. (beginning-of-line 1)
  20563. (looking-at org-todo-line-tags-regexp))
  20564. (push (org-point-in-group p 1 :headline-stars) clist)
  20565. (push (org-point-in-group p 2 :todo-keyword) clist)
  20566. (push (org-point-in-group p 4 :tags) clist))
  20567. (goto-char p)
  20568. (skip-chars-backward "^[\n\r \t") (or (bobp) (backward-char 1))
  20569. (if (looking-at "\\[#[A-Z0-9]\\]")
  20570. (push (org-point-in-group p 0 :priority) clist)))
  20571. ((org-at-item-p)
  20572. (push (org-point-in-group p 2 :item-bullet) clist)
  20573. (push (list :item (point-at-bol)
  20574. (save-excursion (org-end-of-item) (point)))
  20575. clist)
  20576. (and (org-at-item-checkbox-p)
  20577. (push (org-point-in-group p 0 :checkbox) clist)))
  20578. ((org-at-table-p)
  20579. (push (list :table (org-table-begin) (org-table-end)) clist)
  20580. (if (memq 'org-formula faces)
  20581. (push (list :table-special
  20582. (previous-single-property-change p 'face)
  20583. (next-single-property-change p 'face)) clist)))
  20584. ((org-at-table-p 'any)
  20585. (push (list :table-table) clist)))
  20586. (goto-char p)
  20587. (let ((case-fold-search t))
  20588. ;; New the "medium" contexts: clocktables, source blocks
  20589. (cond ((org-in-clocktable-p)
  20590. (push (list :clocktable
  20591. (and (or (looking-at "[ \t]*\\(#\\+BEGIN: clocktable\\)")
  20592. (re-search-backward "[ \t]*\\(#+BEGIN: clocktable\\)" nil t))
  20593. (match-beginning 1))
  20594. (and (re-search-forward "[ \t]*#\\+END:?" nil t)
  20595. (match-end 0))) clist))
  20596. ((org-in-src-block-p)
  20597. (push (list :src-block
  20598. (and (or (looking-at "[ \t]*\\(#\\+BEGIN_SRC\\)")
  20599. (re-search-backward "[ \t]*\\(#+BEGIN_SRC\\)" nil t))
  20600. (match-beginning 1))
  20601. (and (search-forward "#+END_SRC" nil t)
  20602. (match-beginning 0))) clist))))
  20603. (goto-char p)
  20604. ;; Now the small context
  20605. (cond
  20606. ((org-at-timestamp-p)
  20607. (push (org-point-in-group p 0 :timestamp) clist))
  20608. ((memq 'org-link faces)
  20609. (push (list :link
  20610. (previous-single-property-change p 'face)
  20611. (next-single-property-change p 'face)) clist))
  20612. ((memq 'org-special-keyword faces)
  20613. (push (list :keyword
  20614. (previous-single-property-change p 'face)
  20615. (next-single-property-change p 'face)) clist))
  20616. ((org-at-target-p)
  20617. (push (org-point-in-group p 0 :target) clist)
  20618. (goto-char (1- (match-beginning 0)))
  20619. (if (looking-at org-radio-target-regexp)
  20620. (push (org-point-in-group p 0 :radio-target) clist))
  20621. (goto-char p))
  20622. ((setq o (car (delq nil
  20623. (mapcar
  20624. (lambda (x)
  20625. (if (memq x org-latex-fragment-image-overlays) x))
  20626. (overlays-at (point))))))
  20627. (push (list :latex-fragment
  20628. (overlay-start o) (overlay-end o)) clist)
  20629. (push (list :latex-preview
  20630. (overlay-start o) (overlay-end o)) clist))
  20631. ((org-inside-LaTeX-fragment-p)
  20632. ;; FIXME: positions wrong.
  20633. (push (list :latex-fragment (point) (point)) clist)))
  20634. (setq clist (nreverse (delq nil clist)))
  20635. clist))
  20636. (defun org-in-regexp (re &optional nlines visually)
  20637. "Check if point is inside a match of RE.
  20638. Normally only the current line is checked, but you can include
  20639. NLINES extra lines after point into the search. If VISUALLY is
  20640. set, require that the cursor is not after the match but really
  20641. on, so that the block visually is on the match."
  20642. (catch 'exit
  20643. (let ((pos (point))
  20644. (eol (point-at-eol (+ 1 (or nlines 0))))
  20645. (inc (if visually 1 0)))
  20646. (save-excursion
  20647. (beginning-of-line (- 1 (or nlines 0)))
  20648. (while (re-search-forward re eol t)
  20649. (if (and (<= (match-beginning 0) pos)
  20650. (>= (+ inc (match-end 0)) pos))
  20651. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  20652. (define-obsolete-function-alias 'org-at-regexp-p 'org-in-regexp
  20653. "Org mode 8.3")
  20654. (defun org-between-regexps-p (start-re end-re &optional lim-up lim-down)
  20655. "Non-nil when point is between matches of START-RE and END-RE.
  20656. Also return a non-nil value when point is on one of the matches.
  20657. Optional arguments LIM-UP and LIM-DOWN bound the search; they are
  20658. buffer positions. Default values are the positions of headlines
  20659. surrounding the point.
  20660. The functions returns a cons cell whose car (resp. cdr) is the
  20661. position before START-RE (resp. after END-RE)."
  20662. (save-match-data
  20663. (let ((pos (point))
  20664. (limit-up (or lim-up (save-excursion (outline-previous-heading))))
  20665. (limit-down (or lim-down (save-excursion (outline-next-heading))))
  20666. beg end)
  20667. (save-excursion
  20668. ;; Point is on a block when on START-RE or if START-RE can be
  20669. ;; found before it...
  20670. (and (or (org-in-regexp start-re)
  20671. (re-search-backward start-re limit-up t))
  20672. (setq beg (match-beginning 0))
  20673. ;; ... and END-RE after it...
  20674. (goto-char (match-end 0))
  20675. (re-search-forward end-re limit-down t)
  20676. (> (setq end (match-end 0)) pos)
  20677. ;; ... without another START-RE in-between.
  20678. (goto-char (match-beginning 0))
  20679. (not (re-search-backward start-re (1+ beg) t))
  20680. ;; Return value.
  20681. (cons beg end))))))
  20682. (defun org-in-block-p (names)
  20683. "Non-nil when point belongs to a block whose name belongs to NAMES.
  20684. NAMES is a list of strings containing names of blocks.
  20685. Return first block name matched, or nil. Beware that in case of
  20686. nested blocks, the returned name may not belong to the closest
  20687. block from point."
  20688. (save-match-data
  20689. (catch 'exit
  20690. (let ((case-fold-search t)
  20691. (lim-up (save-excursion (outline-previous-heading)))
  20692. (lim-down (save-excursion (outline-next-heading))))
  20693. (mapc (lambda (name)
  20694. (let ((n (regexp-quote name)))
  20695. (when (org-between-regexps-p
  20696. (concat "^[ \t]*#\\+begin_" n)
  20697. (concat "^[ \t]*#\\+end_" n)
  20698. lim-up lim-down)
  20699. (throw 'exit n))))
  20700. names))
  20701. nil)))
  20702. (defun org-occur-in-agenda-files (regexp &optional _nlines)
  20703. "Call `multi-occur' with buffers for all agenda files."
  20704. (interactive "sOrg-files matching: ")
  20705. (let* ((files (org-agenda-files))
  20706. (tnames (mapcar #'file-truename files))
  20707. (extra org-agenda-text-search-extra-files))
  20708. (when (eq (car extra) 'agenda-archives)
  20709. (setq extra (cdr extra))
  20710. (setq files (org-add-archive-files files)))
  20711. (dolist (f extra)
  20712. (unless (member (file-truename f) tnames)
  20713. (unless (member f files) (setq files (append files (list f))))
  20714. (setq tnames (append tnames (list (file-truename f))))))
  20715. (multi-occur
  20716. (mapcar (lambda (x)
  20717. (with-current-buffer
  20718. ;; FIXME: Why not just (find-file-noselect x)?
  20719. ;; Is it to avoid the "revert buffer" prompt?
  20720. (or (get-file-buffer x) (find-file-noselect x))
  20721. (widen)
  20722. (current-buffer)))
  20723. files)
  20724. regexp)))
  20725. (if (boundp 'occur-mode-find-occurrence-hook)
  20726. ;; Emacs 23
  20727. (add-hook 'occur-mode-find-occurrence-hook
  20728. (lambda ()
  20729. (when (derived-mode-p 'org-mode)
  20730. (org-reveal))))
  20731. ;; Emacs 22
  20732. (defadvice occur-mode-goto-occurrence
  20733. (after org-occur-reveal activate)
  20734. (and (derived-mode-p 'org-mode) (org-reveal)))
  20735. (defadvice occur-mode-goto-occurrence-other-window
  20736. (after org-occur-reveal activate)
  20737. (and (derived-mode-p 'org-mode) (org-reveal)))
  20738. (defadvice occur-mode-display-occurrence
  20739. (after org-occur-reveal activate)
  20740. (when (derived-mode-p 'org-mode)
  20741. (let ((pos (occur-mode-find-occurrence)))
  20742. (with-current-buffer (marker-buffer pos)
  20743. (save-excursion
  20744. (goto-char pos)
  20745. (org-reveal)))))))
  20746. (defun org-occur-link-in-agenda-files ()
  20747. "Create a link and search for it in the agendas.
  20748. The link is not stored in `org-stored-links', it is just created
  20749. for the search purpose."
  20750. (interactive)
  20751. (let ((link (condition-case nil
  20752. (org-store-link nil)
  20753. (error "Unable to create a link to here"))))
  20754. (org-occur-in-agenda-files (regexp-quote link))))
  20755. (defun org-reverse-string (string)
  20756. "Return the reverse of STRING."
  20757. (apply 'string (reverse (string-to-list string))))
  20758. ;; defsubst org-uniquify must be defined before first use
  20759. (defun org-uniquify-alist (alist)
  20760. "Merge elements of ALIST with the same key.
  20761. For example, in this alist:
  20762. \(org-uniquify-alist '((a 1) (b 2) (a 3)))
  20763. => '((a 1 3) (b 2))
  20764. merge (a 1) and (a 3) into (a 1 3).
  20765. The function returns the new ALIST."
  20766. (let (rtn)
  20767. (mapc
  20768. (lambda (e)
  20769. (let (n)
  20770. (if (not (assoc (car e) rtn))
  20771. (push e rtn)
  20772. (setq n (cons (car e) (append (cdr (assoc (car e) rtn)) (cdr e))))
  20773. (setq rtn (assq-delete-all (car e) rtn))
  20774. (push n rtn))))
  20775. alist)
  20776. rtn))
  20777. (defun org-delete-all (elts list)
  20778. "Remove all elements in ELTS from LIST."
  20779. (while elts
  20780. (setq list (delete (pop elts) list)))
  20781. list)
  20782. (defun org-count (cl-item cl-seq)
  20783. "Count the number of occurrences of ITEM in SEQ.
  20784. Taken from `count' in cl-seq.el with all keyword arguments removed."
  20785. (let ((cl-end (length cl-seq)) (cl-start 0) (cl-count 0) cl-x)
  20786. (when (consp cl-seq) (setq cl-seq (nthcdr cl-start cl-seq)))
  20787. (while (< cl-start cl-end)
  20788. (setq cl-x (if (consp cl-seq) (pop cl-seq) (aref cl-seq cl-start)))
  20789. (if (equal cl-item cl-x) (setq cl-count (1+ cl-count)))
  20790. (setq cl-start (1+ cl-start)))
  20791. cl-count))
  20792. (defun org-remove-if (predicate seq)
  20793. "Remove everything from SEQ that fulfills PREDICATE."
  20794. (let (res e)
  20795. (while seq
  20796. (setq e (pop seq))
  20797. (if (not (funcall predicate e)) (push e res)))
  20798. (nreverse res)))
  20799. (defun org-remove-if-not (predicate seq)
  20800. "Remove everything from SEQ that does not fulfill PREDICATE."
  20801. (let (res e)
  20802. (while seq
  20803. (setq e (pop seq))
  20804. (if (funcall predicate e) (push e res)))
  20805. (nreverse res)))
  20806. (defun org-reduce (cl-func cl-seq &rest cl-keys)
  20807. "Reduce two-argument FUNCTION across SEQ.
  20808. Taken from `reduce' in cl-seq.el with all keyword arguments but
  20809. \":initial-value\" removed."
  20810. (let ((cl-accum (cond ((memq :initial-value cl-keys)
  20811. (cadr (memq :initial-value cl-keys)))
  20812. (cl-seq (pop cl-seq))
  20813. (t (funcall cl-func)))))
  20814. (while cl-seq
  20815. (setq cl-accum (funcall cl-func cl-accum (pop cl-seq))))
  20816. cl-accum))
  20817. (defun org-every (pred seq)
  20818. "Return true if PREDICATE is true of every element of SEQ.
  20819. Adapted from `every' in cl.el."
  20820. (catch 'org-every
  20821. (mapc (lambda (e) (unless (funcall pred e) (throw 'org-every nil))) seq)
  20822. t))
  20823. (defun org-some (pred seq)
  20824. "Return true if PREDICATE is true of any element of SEQ.
  20825. Adapted from `some' in cl.el."
  20826. (catch 'org-some
  20827. (mapc (lambda (e) (when (funcall pred e) (throw 'org-some t))) seq)
  20828. nil))
  20829. (defun org-back-over-empty-lines ()
  20830. "Move backwards over whitespace, to the beginning of the first empty line.
  20831. Returns the number of empty lines passed."
  20832. (let ((pos (point)))
  20833. (if (cdr (assoc 'heading org-blank-before-new-entry))
  20834. (skip-chars-backward " \t\n\r")
  20835. (unless (eobp)
  20836. (forward-line -1)))
  20837. (beginning-of-line 2)
  20838. (goto-char (min (point) pos))
  20839. (count-lines (point) pos)))
  20840. (defun org-skip-whitespace ()
  20841. (skip-chars-forward " \t\n\r"))
  20842. (defun org-point-in-group (point group &optional context)
  20843. "Check if POINT is in match-group GROUP.
  20844. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  20845. match. If the match group does not exist or point is not inside it,
  20846. return nil."
  20847. (and (match-beginning group)
  20848. (>= point (match-beginning group))
  20849. (<= point (match-end group))
  20850. (if context
  20851. (list context (match-beginning group) (match-end group))
  20852. t)))
  20853. (defun org-switch-to-buffer-other-window (&rest args)
  20854. "Switch to buffer in a second window on the current frame.
  20855. In particular, do not allow pop-up frames.
  20856. Returns the newly created buffer."
  20857. (org-no-popups
  20858. (apply 'switch-to-buffer-other-window args)))
  20859. (defun org-combine-plists (&rest plists)
  20860. "Create a single property list from all plists in PLISTS.
  20861. The process starts by copying the first list, and then setting properties
  20862. from the other lists. Settings in the last list are the most significant
  20863. ones and overrule settings in the other lists."
  20864. (let ((rtn (copy-sequence (pop plists)))
  20865. p v ls)
  20866. (while plists
  20867. (setq ls (pop plists))
  20868. (while ls
  20869. (setq p (pop ls) v (pop ls))
  20870. (setq rtn (plist-put rtn p v))))
  20871. rtn))
  20872. (defun org-replace-escapes (string table)
  20873. "Replace %-escapes in STRING with values in TABLE.
  20874. TABLE is an association list with keys like \"%a\" and string values.
  20875. The sequences in STRING may contain normal field width and padding information,
  20876. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  20877. so values can contain further %-escapes if they are define later in TABLE."
  20878. (let ((tbl (copy-alist table))
  20879. (case-fold-search nil)
  20880. (pchg 0)
  20881. e re rpl)
  20882. (dolist (e tbl)
  20883. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  20884. (when (and (cdr e) (string-match re (cdr e)))
  20885. (let ((sref (substring (cdr e) (match-beginning 0) (match-end 0)))
  20886. (safe "SREF"))
  20887. (add-text-properties 0 3 (list 'sref sref) safe)
  20888. (setcdr e (replace-match safe t t (cdr e)))))
  20889. (while (string-match re string)
  20890. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  20891. (cdr e)))
  20892. (setq string (replace-match rpl t t string))))
  20893. (while (setq pchg (next-property-change pchg string))
  20894. (let ((sref (get-text-property pchg 'sref string)))
  20895. (when (and sref (string-match "SREF" string pchg))
  20896. (setq string (replace-match sref t t string)))))
  20897. string))
  20898. (defun org-sublist (list start end)
  20899. "Return a section of LIST, from START to END.
  20900. Counting starts at 1."
  20901. (let (rtn (c start))
  20902. (setq list (nthcdr (1- start) list))
  20903. (while (and list (<= c end))
  20904. (push (pop list) rtn)
  20905. (setq c (1+ c)))
  20906. (nreverse rtn)))
  20907. (defun org-find-base-buffer-visiting (file)
  20908. "Like `find-buffer-visiting' but always return the base buffer and
  20909. not an indirect buffer."
  20910. (let ((buf (or (get-file-buffer file)
  20911. (find-buffer-visiting file))))
  20912. (if buf
  20913. (or (buffer-base-buffer buf) buf)
  20914. nil)))
  20915. (defun org-image-file-name-regexp (&optional extensions)
  20916. "Return regexp matching the file names of images.
  20917. If EXTENSIONS is given, only match these."
  20918. (if (and (not extensions) (fboundp 'image-file-name-regexp))
  20919. (image-file-name-regexp)
  20920. (let ((image-file-name-extensions
  20921. (or extensions
  20922. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  20923. "xbm" "xpm" "pbm" "pgm" "ppm"))))
  20924. (concat "\\."
  20925. (regexp-opt (nconc (mapcar 'upcase
  20926. image-file-name-extensions)
  20927. image-file-name-extensions)
  20928. t)
  20929. "\\'"))))
  20930. (defun org-file-image-p (file &optional extensions)
  20931. "Return non-nil if FILE is an image."
  20932. (save-match-data
  20933. (string-match (org-image-file-name-regexp extensions) file)))
  20934. (defun org-get-cursor-date (&optional with-time)
  20935. "Return the date at cursor in as a time.
  20936. This works in the calendar and in the agenda, anywhere else it just
  20937. returns the current time.
  20938. If WITH-TIME is non-nil, returns the time of the event at point (in
  20939. the agenda) or the current time of the day."
  20940. (let (date day defd tp hod mod)
  20941. (when with-time
  20942. (setq tp (get-text-property (point) 'time))
  20943. (when (and tp (string-match "\\([0-9][0-9]\\):\\([0-9][0-9]\\)" tp))
  20944. (setq hod (string-to-number (match-string 1 tp))
  20945. mod (string-to-number (match-string 2 tp))))
  20946. (or tp (let ((now (decode-time)))
  20947. (setq hod (nth 2 now)
  20948. mod (nth 1 now)))))
  20949. (cond
  20950. ((eq major-mode 'calendar-mode)
  20951. (setq date (calendar-cursor-to-date)
  20952. defd (encode-time 0 (or mod 0) (or hod 0)
  20953. (nth 1 date) (nth 0 date) (nth 2 date))))
  20954. ((eq major-mode 'org-agenda-mode)
  20955. (setq day (get-text-property (point) 'day))
  20956. (if day
  20957. (setq date (calendar-gregorian-from-absolute day)
  20958. defd (encode-time 0 (or mod 0) (or hod 0)
  20959. (nth 1 date) (nth 0 date) (nth 2 date))))))
  20960. (or defd (current-time))))
  20961. (defun org-mark-subtree (&optional up)
  20962. "Mark the current subtree.
  20963. This puts point at the start of the current subtree, and mark at
  20964. the end. If a numeric prefix UP is given, move up into the
  20965. hierarchy of headlines by UP levels before marking the subtree."
  20966. (interactive "P")
  20967. (org-with-limited-levels
  20968. (cond ((org-at-heading-p) (beginning-of-line))
  20969. ((org-before-first-heading-p) (user-error "Not in a subtree"))
  20970. (t (outline-previous-visible-heading 1))))
  20971. (when up (while (and (> up 0) (org-up-heading-safe)) (decf up)))
  20972. (if (org-called-interactively-p 'any)
  20973. (call-interactively 'org-mark-element)
  20974. (org-mark-element)))
  20975. ;;; Indentation
  20976. (defun org--get-expected-indentation (element contentsp)
  20977. "Expected indentation column for current line, according to ELEMENT.
  20978. ELEMENT is an element containing point. CONTENTSP is non-nil
  20979. when indentation is to be computed according to contents of
  20980. ELEMENT."
  20981. (let ((type (org-element-type element))
  20982. (start (org-element-property :begin element))
  20983. (post-affiliated (org-element-property :post-affiliated element)))
  20984. (org-with-wide-buffer
  20985. (cond
  20986. (contentsp
  20987. (case type
  20988. ((diary-sexp footnote-definition) 0)
  20989. ((headline inlinetask nil)
  20990. (if (not org-adapt-indentation) 0
  20991. (let ((level (org-current-level)))
  20992. (if level (1+ level) 0))))
  20993. ((item plain-list) (org-list-item-body-column post-affiliated))
  20994. (t
  20995. (goto-char start)
  20996. (org-get-indentation))))
  20997. ((memq type '(headline inlinetask nil))
  20998. (if (save-excursion (beginning-of-line) (looking-at "[ \t]*$"))
  20999. (org--get-expected-indentation element t)
  21000. 0))
  21001. ((memq type '(diary-sexp footnote-definition)) 0)
  21002. ;; First paragraph of a footnote definition or an item.
  21003. ;; Indent like parent.
  21004. ((< (line-beginning-position) start)
  21005. (org--get-expected-indentation
  21006. (org-element-property :parent element) t))
  21007. ;; At first line: indent according to previous sibling, if any,
  21008. ;; ignoring footnote definitions and inline tasks, or parent's
  21009. ;; contents.
  21010. ((= (line-beginning-position) start)
  21011. (catch 'exit
  21012. (while t
  21013. (if (= (point-min) start) (throw 'exit 0)
  21014. (goto-char (1- start))
  21015. (let* ((previous (org-element-at-point))
  21016. (parent previous))
  21017. (while (and parent (<= (org-element-property :end parent) start))
  21018. (setq previous parent
  21019. parent (org-element-property :parent parent)))
  21020. (cond
  21021. ((not previous) (throw 'exit 0))
  21022. ((> (org-element-property :end previous) start)
  21023. (throw 'exit (org--get-expected-indentation previous t)))
  21024. ((memq (org-element-type previous)
  21025. '(footnote-definition inlinetask))
  21026. (setq start (org-element-property :begin previous)))
  21027. (t (goto-char (org-element-property :begin previous))
  21028. (throw 'exit
  21029. (if (bolp) (org-get-indentation)
  21030. ;; At first paragraph in an item or
  21031. ;; a footnote definition.
  21032. (org--get-expected-indentation
  21033. (org-element-property :parent previous) t))))))))))
  21034. ;; Otherwise, move to the first non-blank line above.
  21035. (t
  21036. (beginning-of-line)
  21037. (let ((pos (point)))
  21038. (skip-chars-backward " \r\t\n")
  21039. (cond
  21040. ;; Two blank lines end a footnote definition or a plain
  21041. ;; list. When we indent an empty line after them, the
  21042. ;; containing list or footnote definition is over, so it
  21043. ;; qualifies as a previous sibling. Therefore, we indent
  21044. ;; like its first line.
  21045. ((and (memq type '(footnote-definition plain-list))
  21046. (> (count-lines (point) pos) 2))
  21047. (goto-char start)
  21048. (org-get-indentation))
  21049. ;; Line above is the first one of a paragraph at the
  21050. ;; beginning of an item or a footnote definition. Indent
  21051. ;; like parent.
  21052. ((< (line-beginning-position) start)
  21053. (org--get-expected-indentation
  21054. (org-element-property :parent element) t))
  21055. ;; Line above is the beginning of an element, i.e., point
  21056. ;; was originally on the blank lines between element's start
  21057. ;; and contents.
  21058. ((= (line-beginning-position) post-affiliated)
  21059. (org--get-expected-indentation element t))
  21060. ;; POS is after contents in a greater element. Indent like
  21061. ;; the beginning of the element.
  21062. ;;
  21063. ;; As a special case, if point is at the end of a footnote
  21064. ;; definition or an item, indent like the very last element
  21065. ;; within.
  21066. ((and (not (eq type 'paragraph))
  21067. (let ((cend (org-element-property :contents-end element)))
  21068. (and cend (<= cend pos))))
  21069. (if (memq type '(footnote-definition item plain-list))
  21070. (let ((last (org-element-at-point)))
  21071. (org--get-expected-indentation
  21072. last
  21073. (memq (org-element-type last)
  21074. '(footnote-definition item plain-list))))
  21075. (goto-char start)
  21076. (org-get-indentation)))
  21077. ;; In any other case, indent like the current line.
  21078. (t (org-get-indentation)))))))))
  21079. (defun org--align-node-property ()
  21080. "Align node property at point.
  21081. Alignment is done according to `org-property-format', which see."
  21082. (when (save-excursion
  21083. (beginning-of-line)
  21084. (looking-at org-property-re))
  21085. (replace-match
  21086. (concat (match-string 4)
  21087. (org-trim
  21088. (format org-property-format (match-string 1) (match-string 3))))
  21089. t t)))
  21090. (defun org-indent-line ()
  21091. "Indent line depending on context.
  21092. Indentation is done according to the following rules:
  21093. - Footnote definitions, diary sexps, headlines and inline tasks
  21094. have to start at column 0.
  21095. - On the very first line of an element, consider, in order, the
  21096. next rules until one matches:
  21097. 1. If there's a sibling element before, ignoring footnote
  21098. definitions and inline tasks, indent like its first line.
  21099. 2. If element has a parent, indent like its contents. More
  21100. precisely, if parent is an item, indent after the
  21101. description part, if any, or the bullet (see
  21102. `org-list-description-max-indent'). Else, indent like
  21103. parent's first line.
  21104. 3. Otherwise, indent relatively to current level, if
  21105. `org-adapt-indentation' is non-nil, or to left margin.
  21106. - On a blank line at the end of an element, indent according to
  21107. the type of the element. More precisely
  21108. 1. If element is a plain list, an item, or a footnote
  21109. definition, indent like the very last element within.
  21110. 2. If element is a paragraph, indent like its last non blank
  21111. line.
  21112. 3. Otherwise, indent like its very first line.
  21113. - In the code part of a source block, use language major mode
  21114. to indent current line if `org-src-tab-acts-natively' is
  21115. non-nil. If it is nil, do nothing.
  21116. - Otherwise, indent like the first non-blank line above.
  21117. The function doesn't indent an item as it could break the whole
  21118. list structure. Instead, use \\<org-mode-map>\\[org-shiftmetaleft] or \
  21119. \\[org-shiftmetaright].
  21120. Also align node properties according to `org-property-format'."
  21121. (interactive)
  21122. (cond
  21123. (orgstruct-is-++
  21124. (let ((indent-line-function
  21125. (cadadr (assq 'indent-line-function org-fb-vars))))
  21126. (indent-according-to-mode)))
  21127. ((org-at-heading-p) 'noindent)
  21128. (t
  21129. (let* ((element (save-excursion (beginning-of-line) (org-element-at-point)))
  21130. (type (org-element-type element)))
  21131. (cond ((and (memq type '(plain-list item))
  21132. (= (line-beginning-position)
  21133. (org-element-property :post-affiliated element)))
  21134. 'noindent)
  21135. ((and (eq type 'src-block)
  21136. org-src-tab-acts-natively
  21137. (> (line-beginning-position)
  21138. (org-element-property :post-affiliated element))
  21139. (< (line-beginning-position)
  21140. (org-with-wide-buffer
  21141. (goto-char (org-element-property :end element))
  21142. (skip-chars-backward " \r\t\n")
  21143. (line-beginning-position))))
  21144. (org-babel-do-key-sequence-in-edit-buffer (kbd "TAB")))
  21145. (t
  21146. (let ((column (org--get-expected-indentation element nil)))
  21147. ;; Preserve current column.
  21148. (if (<= (current-column) (current-indentation))
  21149. (org-indent-line-to column)
  21150. (save-excursion (org-indent-line-to column))))
  21151. ;; Align node property. Also preserve current column.
  21152. (when (eq type 'node-property)
  21153. (let ((column (current-column)))
  21154. (org--align-node-property)
  21155. (org-move-to-column column)))))))))
  21156. (defun org-indent-region (start end)
  21157. "Indent each non-blank line in the region.
  21158. Called from a program, START and END specify the region to
  21159. indent. The function will not indent contents of example blocks,
  21160. verse blocks and export blocks as leading white spaces are
  21161. assumed to be significant there."
  21162. (interactive "r")
  21163. (save-excursion
  21164. (goto-char start)
  21165. (skip-chars-forward " \r\t\n")
  21166. (unless (eobp) (beginning-of-line))
  21167. (let ((indent-to
  21168. (lambda (ind pos)
  21169. ;; Set IND as indentation for all lines between point and
  21170. ;; POS or END, whichever comes first. Blank lines are
  21171. ;; ignored. Leave point after POS once done.
  21172. (let ((limit (copy-marker (min end pos))))
  21173. (while (< (point) limit)
  21174. (unless (org-looking-at-p "[ \t]*$") (org-indent-line-to ind))
  21175. (forward-line))
  21176. (set-marker limit nil))))
  21177. (end (copy-marker end)))
  21178. (while (< (point) end)
  21179. (if (or (org-looking-at-p " \r\t\n") (org-at-heading-p)) (forward-line)
  21180. (let* ((element (org-element-at-point))
  21181. (type (org-element-type element))
  21182. (element-end (copy-marker (org-element-property :end element)))
  21183. (ind (org--get-expected-indentation element nil)))
  21184. (cond
  21185. ((or (memq type '(paragraph table table-row))
  21186. (not (or (org-element-property :contents-begin element)
  21187. (memq type
  21188. '(example-block export-block src-block)))))
  21189. ;; Elements here are indented as a single block. Also
  21190. ;; align node properties.
  21191. (when (eq type 'node-property)
  21192. (org--align-node-property)
  21193. (beginning-of-line))
  21194. (funcall indent-to ind element-end))
  21195. (t
  21196. ;; Elements in this category consist of three parts:
  21197. ;; before the contents, the contents, and after the
  21198. ;; contents. The contents are treated specially,
  21199. ;; according to the element type, or not indented at
  21200. ;; all. Other parts are indented as a single block.
  21201. (let* ((post (copy-marker
  21202. (org-element-property :post-affiliated element)))
  21203. (cbeg
  21204. (copy-marker
  21205. (cond
  21206. ((not (org-element-property :contents-begin element))
  21207. ;; Fake contents for source blocks.
  21208. (org-with-wide-buffer
  21209. (goto-char post)
  21210. (forward-line)
  21211. (point)))
  21212. ((memq type '(footnote-definition item plain-list))
  21213. ;; Contents in these elements could start on
  21214. ;; the same line as the beginning of the
  21215. ;; element. Make sure we start indenting
  21216. ;; from the second line.
  21217. (org-with-wide-buffer
  21218. (goto-char post)
  21219. (end-of-line)
  21220. (skip-chars-forward " \r\t\n")
  21221. (if (eobp) (point) (line-beginning-position))))
  21222. (t (org-element-property :contents-begin element)))))
  21223. (cend (copy-marker
  21224. (or (org-element-property :contents-end element)
  21225. ;; Fake contents for source blocks.
  21226. (org-with-wide-buffer
  21227. (goto-char element-end)
  21228. (skip-chars-backward " \r\t\n")
  21229. (line-beginning-position)))
  21230. t)))
  21231. ;; Do not change items indentation individually as it
  21232. ;; might break the list as a whole. On the other
  21233. ;; hand, when at a plain list, indent it as a whole.
  21234. (cond ((eq type 'plain-list)
  21235. (let ((offset (- ind (org-get-indentation))))
  21236. (unless (zerop offset)
  21237. (indent-rigidly (org-element-property :begin element)
  21238. (org-element-property :end element)
  21239. offset))
  21240. (goto-char cbeg)))
  21241. ((eq type 'item) (goto-char cbeg))
  21242. (t (funcall indent-to ind cbeg)))
  21243. (when (< (point) end)
  21244. (case type
  21245. ((example-block export-block verse-block))
  21246. (src-block
  21247. ;; In a source block, indent source code
  21248. ;; according to language major mode, but only if
  21249. ;; `org-src-tab-acts-natively' is non-nil.
  21250. (when (and (< (point) end) org-src-tab-acts-natively)
  21251. (ignore-errors
  21252. (org-babel-do-in-edit-buffer
  21253. (indent-region (point-min) (point-max))))))
  21254. (t (org-indent-region (point) (min cend end))))
  21255. (goto-char (min cend end))
  21256. (when (< (point) end) (funcall indent-to ind element-end)))
  21257. (set-marker post nil)
  21258. (set-marker cbeg nil)
  21259. (set-marker cend nil))))
  21260. (set-marker element-end nil))))
  21261. (set-marker end nil))))
  21262. (defun org-indent-drawer ()
  21263. "Indent the drawer at point."
  21264. (interactive)
  21265. (unless (save-excursion
  21266. (beginning-of-line)
  21267. (org-looking-at-p org-drawer-regexp))
  21268. (user-error "Not at a drawer"))
  21269. (let ((element (org-element-at-point)))
  21270. (unless (memq (org-element-type element) '(drawer property-drawer))
  21271. (user-error "Not at a drawer"))
  21272. (org-with-wide-buffer
  21273. (org-indent-region (org-element-property :begin element)
  21274. (org-element-property :end element))))
  21275. (message "Drawer at point indented"))
  21276. (defun org-indent-block ()
  21277. "Indent the block at point."
  21278. (interactive)
  21279. (unless (save-excursion
  21280. (beginning-of-line)
  21281. (let ((case-fold-search t))
  21282. (org-looking-at-p "[ \t]*#\\+\\(begin\\|end\\)_")))
  21283. (user-error "Not at a block"))
  21284. (let ((element (org-element-at-point)))
  21285. (unless (memq (org-element-type element)
  21286. '(comment-block center-block dynamic-block example-block
  21287. export-block quote-block special-block
  21288. src-block verse-block))
  21289. (user-error "Not at a block"))
  21290. (org-with-wide-buffer
  21291. (org-indent-region (org-element-property :begin element)
  21292. (org-element-property :end element))))
  21293. (message "Block at point indented"))
  21294. ;;; Filling
  21295. ;; We use our own fill-paragraph and auto-fill functions.
  21296. ;; `org-fill-paragraph' relies on adaptive filling and context
  21297. ;; checking. Appropriate `fill-prefix' is computed with
  21298. ;; `org-adaptive-fill-function'.
  21299. ;; `org-auto-fill-function' takes care of auto-filling. It calls
  21300. ;; `do-auto-fill' only on valid areas with `fill-prefix' shadowed with
  21301. ;; `org-adaptive-fill-function' value. Internally,
  21302. ;; `org-comment-line-break-function' breaks the line.
  21303. ;; `org-setup-filling' installs filling and auto-filling related
  21304. ;; variables during `org-mode' initialization.
  21305. (defvar org-element-paragraph-separate) ; org-element.el
  21306. (defun org-setup-filling ()
  21307. (require 'org-element)
  21308. ;; Prevent auto-fill from inserting unwanted new items.
  21309. (when (boundp 'fill-nobreak-predicate)
  21310. (org-set-local
  21311. 'fill-nobreak-predicate
  21312. (org-uniquify
  21313. (append fill-nobreak-predicate
  21314. '(org-fill-line-break-nobreak-p
  21315. org-fill-paragraph-with-timestamp-nobreak-p)))))
  21316. (let ((paragraph-ending (substring org-element-paragraph-separate 1)))
  21317. (org-set-local 'paragraph-start paragraph-ending)
  21318. (org-set-local 'paragraph-separate paragraph-ending))
  21319. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  21320. (org-set-local 'auto-fill-inhibit-regexp nil)
  21321. (org-set-local 'adaptive-fill-function 'org-adaptive-fill-function)
  21322. (org-set-local 'normal-auto-fill-function 'org-auto-fill-function)
  21323. (org-set-local 'comment-line-break-function 'org-comment-line-break-function))
  21324. (defun org-fill-line-break-nobreak-p ()
  21325. "Non-nil when a new line at point would create an Org line break."
  21326. (save-excursion
  21327. (skip-chars-backward "[ \t]")
  21328. (skip-chars-backward "\\\\")
  21329. (looking-at "\\\\\\\\\\($\\|[^\\\\]\\)")))
  21330. (defun org-fill-paragraph-with-timestamp-nobreak-p ()
  21331. "Non-nil when a new line at point would split a timestamp."
  21332. (and (org-at-timestamp-p t)
  21333. (not (looking-at org-ts-regexp-both))))
  21334. (declare-function message-in-body-p "message" ())
  21335. (defvar orgtbl-line-start-regexp) ; From org-table.el
  21336. (defun org-adaptive-fill-function ()
  21337. "Compute a fill prefix for the current line.
  21338. Return fill prefix, as a string, or nil if current line isn't
  21339. meant to be filled. For convenience, if `adaptive-fill-regexp'
  21340. matches in paragraphs or comments, use it."
  21341. (catch 'exit
  21342. (when (derived-mode-p 'message-mode)
  21343. (save-excursion
  21344. (beginning-of-line)
  21345. (cond ((not (message-in-body-p)) (throw 'exit nil))
  21346. ((org-looking-at-p org-table-line-regexp) (throw 'exit nil))
  21347. ((looking-at message-cite-prefix-regexp)
  21348. (throw 'exit (match-string-no-properties 0)))
  21349. ((looking-at org-outline-regexp)
  21350. (throw 'exit (make-string (length (match-string 0)) ?\s))))))
  21351. (org-with-wide-buffer
  21352. (unless (org-at-heading-p)
  21353. (let* ((p (line-beginning-position))
  21354. (element (save-excursion
  21355. (beginning-of-line)
  21356. (org-element-at-point)))
  21357. (type (org-element-type element))
  21358. (post-affiliated (org-element-property :post-affiliated element)))
  21359. (unless (< p post-affiliated)
  21360. (case type
  21361. (comment
  21362. (save-excursion
  21363. (beginning-of-line)
  21364. (looking-at "[ \t]*")
  21365. (concat (match-string 0) "# ")))
  21366. (footnote-definition "")
  21367. ((item plain-list)
  21368. (make-string (org-list-item-body-column post-affiliated) ?\s))
  21369. (paragraph
  21370. ;; Fill prefix is usually the same as the current line,
  21371. ;; unless the paragraph is at the beginning of an item.
  21372. (let ((parent (org-element-property :parent element)))
  21373. (save-excursion
  21374. (beginning-of-line)
  21375. (cond ((eq (org-element-type parent) 'item)
  21376. (make-string (org-list-item-body-column
  21377. (org-element-property :begin parent))
  21378. ?\s))
  21379. ((and adaptive-fill-regexp
  21380. ;; Locally disable
  21381. ;; `adaptive-fill-function' to let
  21382. ;; `fill-context-prefix' handle
  21383. ;; `adaptive-fill-regexp' variable.
  21384. (let (adaptive-fill-function)
  21385. (fill-context-prefix
  21386. post-affiliated
  21387. (org-element-property :end element)))))
  21388. ((looking-at "[ \t]+") (match-string 0))
  21389. (t "")))))
  21390. (comment-block
  21391. ;; Only fill contents if P is within block boundaries.
  21392. (let* ((cbeg (save-excursion (goto-char post-affiliated)
  21393. (forward-line)
  21394. (point)))
  21395. (cend (save-excursion
  21396. (goto-char (org-element-property :end element))
  21397. (skip-chars-backward " \r\t\n")
  21398. (line-beginning-position))))
  21399. (when (and (>= p cbeg) (< p cend))
  21400. (if (save-excursion (beginning-of-line) (looking-at "[ \t]+"))
  21401. (match-string 0)
  21402. "")))))))))))
  21403. (declare-function message-goto-body "message" ())
  21404. (defvar message-cite-prefix-regexp) ; From message.el
  21405. (defun org-fill-paragraph (&optional justify)
  21406. "Fill element at point, when applicable.
  21407. This function only applies to comment blocks, comments, example
  21408. blocks and paragraphs. Also, as a special case, re-align table
  21409. when point is at one.
  21410. If JUSTIFY is non-nil (interactively, with prefix argument),
  21411. justify as well. If `sentence-end-double-space' is non-nil, then
  21412. period followed by one space does not end a sentence, so don't
  21413. break a line there. The variable `fill-column' controls the
  21414. width for filling.
  21415. For convenience, when point is at a plain list, an item or
  21416. a footnote definition, try to fill the first paragraph within."
  21417. (interactive)
  21418. (if (and (derived-mode-p 'message-mode)
  21419. (or (not (message-in-body-p))
  21420. (save-excursion (move-beginning-of-line 1)
  21421. (looking-at message-cite-prefix-regexp))))
  21422. ;; First ensure filling is correct in message-mode.
  21423. (let ((fill-paragraph-function
  21424. (cadadr (assoc 'fill-paragraph-function org-fb-vars)))
  21425. (fill-prefix (cadadr (assoc 'fill-prefix org-fb-vars)))
  21426. (paragraph-start (cadadr (assoc 'paragraph-start org-fb-vars)))
  21427. (paragraph-separate
  21428. (cadadr (assoc 'paragraph-separate org-fb-vars))))
  21429. (fill-paragraph nil))
  21430. (with-syntax-table org-mode-transpose-word-syntax-table
  21431. ;; Move to end of line in order to get the first paragraph
  21432. ;; within a plain list or a footnote definition.
  21433. (let ((element (save-excursion
  21434. (end-of-line)
  21435. (or (ignore-errors (org-element-at-point))
  21436. (user-error "An element cannot be parsed line %d"
  21437. (line-number-at-pos (point)))))))
  21438. ;; First check if point is in a blank line at the beginning of
  21439. ;; the buffer. In that case, ignore filling.
  21440. (case (org-element-type element)
  21441. ;; Use major mode filling function is src blocks.
  21442. (src-block (org-babel-do-key-sequence-in-edit-buffer (kbd "M-q")))
  21443. ;; Align Org tables, leave table.el tables as-is.
  21444. (table-row (org-table-align) t)
  21445. (table
  21446. (when (eq (org-element-property :type element) 'org)
  21447. (save-excursion
  21448. (goto-char (org-element-property :post-affiliated element))
  21449. (org-table-align)))
  21450. t)
  21451. (paragraph
  21452. ;; Paragraphs may contain `line-break' type objects.
  21453. (let ((beg (max (point-min)
  21454. (org-element-property :contents-begin element)))
  21455. (end (min (point-max)
  21456. (org-element-property :contents-end element))))
  21457. ;; Do nothing if point is at an affiliated keyword.
  21458. (if (< (line-end-position) beg) t
  21459. (when (derived-mode-p 'message-mode)
  21460. ;; In `message-mode', do not fill following citation
  21461. ;; in current paragraph nor text before message body.
  21462. (let ((body-start (save-excursion (message-goto-body))))
  21463. (when body-start (setq beg (max body-start beg))))
  21464. (when (save-excursion
  21465. (re-search-forward
  21466. (concat "^" message-cite-prefix-regexp) end t))
  21467. (setq end (match-beginning 0))))
  21468. ;; Fill paragraph, taking line breaks into account.
  21469. (save-excursion
  21470. (goto-char beg)
  21471. (let ((cuts (list beg)))
  21472. (while (re-search-forward "\\\\\\\\[ \t]*\n" end t)
  21473. (when (eq 'line-break
  21474. (org-element-type
  21475. (save-excursion (backward-char)
  21476. (org-element-context))))
  21477. (push (point) cuts)))
  21478. (dolist (c (delq end cuts))
  21479. (fill-region-as-paragraph c end justify)
  21480. (setq end c))))
  21481. t)))
  21482. ;; Contents of `comment-block' type elements should be
  21483. ;; filled as plain text, but only if point is within block
  21484. ;; markers.
  21485. (comment-block
  21486. (let* ((case-fold-search t)
  21487. (beg (save-excursion
  21488. (goto-char (org-element-property :begin element))
  21489. (re-search-forward "^[ \t]*#\\+begin_comment" nil t)
  21490. (forward-line)
  21491. (point)))
  21492. (end (save-excursion
  21493. (goto-char (org-element-property :end element))
  21494. (re-search-backward "^[ \t]*#\\+end_comment" nil t)
  21495. (line-beginning-position))))
  21496. (if (or (< (point) beg) (> (point) end)) t
  21497. (fill-region-as-paragraph
  21498. (save-excursion (end-of-line)
  21499. (re-search-backward "^[ \t]*$" beg 'move)
  21500. (line-beginning-position))
  21501. (save-excursion (beginning-of-line)
  21502. (re-search-forward "^[ \t]*$" end 'move)
  21503. (line-beginning-position))
  21504. justify))))
  21505. ;; Fill comments.
  21506. (comment
  21507. (let ((begin (org-element-property :post-affiliated element))
  21508. (end (org-element-property :end element)))
  21509. (when (and (>= (point) begin) (<= (point) end))
  21510. (let ((begin (save-excursion
  21511. (end-of-line)
  21512. (if (re-search-backward "^[ \t]*#[ \t]*$" begin t)
  21513. (progn (forward-line) (point))
  21514. begin)))
  21515. (end (save-excursion
  21516. (end-of-line)
  21517. (if (re-search-forward "^[ \t]*#[ \t]*$" end 'move)
  21518. (1- (line-beginning-position))
  21519. (skip-chars-backward " \r\t\n")
  21520. (line-end-position)))))
  21521. ;; Do not fill comments when at a blank line.
  21522. (when (> end begin)
  21523. (let ((fill-prefix
  21524. (save-excursion
  21525. (beginning-of-line)
  21526. (looking-at "[ \t]*#")
  21527. (let ((comment-prefix (match-string 0)))
  21528. (goto-char (match-end 0))
  21529. (if (looking-at adaptive-fill-regexp)
  21530. (concat comment-prefix (match-string 0))
  21531. (concat comment-prefix " "))))))
  21532. (save-excursion
  21533. (fill-region-as-paragraph begin end justify))))))
  21534. t))
  21535. ;; Ignore every other element.
  21536. (otherwise t))))))
  21537. (defun org-auto-fill-function ()
  21538. "Auto-fill function."
  21539. ;; Check if auto-filling is meaningful.
  21540. (let ((fc (current-fill-column)))
  21541. (when (and fc (> (current-column) fc))
  21542. (let* ((fill-prefix (org-adaptive-fill-function))
  21543. ;; Enforce empty fill prefix, if required. Otherwise, it
  21544. ;; will be computed again.
  21545. (adaptive-fill-mode (not (equal fill-prefix ""))))
  21546. (when fill-prefix (do-auto-fill))))))
  21547. (defun org-comment-line-break-function (&optional soft)
  21548. "Break line at point and indent, continuing comment if within one.
  21549. The inserted newline is marked hard if variable
  21550. `use-hard-newlines' is true, unless optional argument SOFT is
  21551. non-nil."
  21552. (if soft (insert-and-inherit ?\n) (newline 1))
  21553. (save-excursion (forward-char -1) (delete-horizontal-space))
  21554. (delete-horizontal-space)
  21555. (indent-to-left-margin)
  21556. (insert-before-markers-and-inherit fill-prefix))
  21557. ;;; Fixed Width Areas
  21558. (defun org-toggle-fixed-width ()
  21559. "Toggle fixed-width markup.
  21560. Add or remove fixed-width markup on current line, whenever it
  21561. makes sense. Return an error otherwise.
  21562. If a region is active and if it contains only fixed-width areas
  21563. or blank lines, remove all fixed-width markup in it. If the
  21564. region contains anything else, convert all non-fixed-width lines
  21565. to fixed-width ones.
  21566. Blank lines at the end of the region are ignored unless the
  21567. region only contains such lines."
  21568. (interactive)
  21569. (if (not (org-region-active-p))
  21570. ;; No region:
  21571. ;;
  21572. ;; Remove fixed width marker only in a fixed-with element.
  21573. ;;
  21574. ;; Add fixed width maker in paragraphs, in blank lines after
  21575. ;; elements or at the beginning of a headline or an inlinetask,
  21576. ;; and before any one-line elements (e.g., a clock).
  21577. (progn
  21578. (beginning-of-line)
  21579. (let* ((element (org-element-at-point))
  21580. (type (org-element-type element)))
  21581. (cond
  21582. ((and (eq type 'fixed-width)
  21583. (looking-at "[ \t]*\\(:\\(?: \\|$\\)\\)"))
  21584. (replace-match
  21585. "" nil nil nil (if (= (line-end-position) (match-end 0)) 0 1)))
  21586. ((and (memq type '(babel-call clock comment diary-sexp headline
  21587. horizontal-rule keyword paragraph
  21588. planning))
  21589. (<= (org-element-property :post-affiliated element) (point)))
  21590. (skip-chars-forward " \t")
  21591. (insert ": "))
  21592. ((and (org-looking-at-p "[ \t]*$")
  21593. (or (eq type 'inlinetask)
  21594. (save-excursion
  21595. (skip-chars-forward " \r\t\n")
  21596. (<= (org-element-property :end element) (point)))))
  21597. (delete-region (point) (line-end-position))
  21598. (org-indent-line)
  21599. (insert ": "))
  21600. (t (user-error "Cannot insert a fixed-width line here")))))
  21601. ;; Region active.
  21602. (let* ((begin (save-excursion
  21603. (goto-char (region-beginning))
  21604. (line-beginning-position)))
  21605. (end (copy-marker
  21606. (save-excursion
  21607. (goto-char (region-end))
  21608. (unless (eolp) (beginning-of-line))
  21609. (if (save-excursion (re-search-backward "\\S-" begin t))
  21610. (progn (skip-chars-backward " \r\t\n") (point))
  21611. (point)))))
  21612. (all-fixed-width-p
  21613. (catch 'not-all-p
  21614. (save-excursion
  21615. (goto-char begin)
  21616. (skip-chars-forward " \r\t\n")
  21617. (when (eobp) (throw 'not-all-p nil))
  21618. (while (< (point) end)
  21619. (let ((element (org-element-at-point)))
  21620. (if (eq (org-element-type element) 'fixed-width)
  21621. (goto-char (org-element-property :end element))
  21622. (throw 'not-all-p nil))))
  21623. t))))
  21624. (if all-fixed-width-p
  21625. (save-excursion
  21626. (goto-char begin)
  21627. (while (< (point) end)
  21628. (when (looking-at "[ \t]*\\(:\\(?: \\|$\\)\\)")
  21629. (replace-match
  21630. "" nil nil nil
  21631. (if (= (line-end-position) (match-end 0)) 0 1)))
  21632. (forward-line)))
  21633. (let ((min-ind (point-max)))
  21634. ;; Find minimum indentation across all lines.
  21635. (save-excursion
  21636. (goto-char begin)
  21637. (if (not (save-excursion (re-search-forward "\\S-" end t)))
  21638. (setq min-ind 0)
  21639. (catch 'zerop
  21640. (while (< (point) end)
  21641. (unless (org-looking-at-p "[ \t]*$")
  21642. (let ((ind (org-get-indentation)))
  21643. (setq min-ind (min min-ind ind))
  21644. (when (zerop ind) (throw 'zerop t))))
  21645. (forward-line)))))
  21646. ;; Loop over all lines and add fixed-width markup everywhere
  21647. ;; but in fixed-width lines.
  21648. (save-excursion
  21649. (goto-char begin)
  21650. (while (< (point) end)
  21651. (cond
  21652. ((org-at-heading-p)
  21653. (insert ": ")
  21654. (forward-line)
  21655. (while (and (< (point) end) (org-looking-at-p "[ \t]*$"))
  21656. (insert ":")
  21657. (forward-line)))
  21658. ((org-looking-at-p "[ \t]*:\\( \\|$\\)")
  21659. (let* ((element (org-element-at-point))
  21660. (element-end (org-element-property :end element)))
  21661. (if (eq (org-element-type element) 'fixed-width)
  21662. (progn (goto-char element-end)
  21663. (skip-chars-backward " \r\t\n")
  21664. (forward-line))
  21665. (let ((limit (min end element-end)))
  21666. (while (< (point) limit)
  21667. (org-move-to-column min-ind t)
  21668. (insert ": ")
  21669. (forward-line))))))
  21670. (t
  21671. (org-move-to-column min-ind t)
  21672. (insert ": ")
  21673. (forward-line)))))))
  21674. (set-marker end nil))))
  21675. ;;; Comments
  21676. ;; Org comments syntax is quite complex. It requires the entire line
  21677. ;; to be just a comment. Also, even with the right syntax at the
  21678. ;; beginning of line, some some elements (i.e. verse-block or
  21679. ;; example-block) don't accept comments. Usual Emacs comment commands
  21680. ;; cannot cope with those requirements. Therefore, Org replaces them.
  21681. ;; Org still relies on `comment-dwim', but cannot trust
  21682. ;; `comment-only-p'. So, `comment-region-function' and
  21683. ;; `uncomment-region-function' both point
  21684. ;; to`org-comment-or-uncomment-region'. Eventually,
  21685. ;; `org-insert-comment' takes care of insertion of comments at the
  21686. ;; beginning of line.
  21687. ;; `org-setup-comments-handling' install comments related variables
  21688. ;; during `org-mode' initialization.
  21689. (defun org-setup-comments-handling ()
  21690. (interactive)
  21691. (org-set-local 'comment-use-syntax nil)
  21692. (org-set-local 'comment-start "# ")
  21693. (org-set-local 'comment-start-skip "^\\s-*#\\(?: \\|$\\)")
  21694. (org-set-local 'comment-insert-comment-function 'org-insert-comment)
  21695. (org-set-local 'comment-region-function 'org-comment-or-uncomment-region)
  21696. (org-set-local 'uncomment-region-function 'org-comment-or-uncomment-region))
  21697. (defun org-insert-comment ()
  21698. "Insert an empty comment above current line.
  21699. If the line is empty, insert comment at its beginning. When
  21700. point is within a source block, comment according to the related
  21701. major mode."
  21702. (if (let ((element (org-element-at-point)))
  21703. (and (eq (org-element-type element) 'src-block)
  21704. (< (save-excursion
  21705. (goto-char (org-element-property :post-affiliated element))
  21706. (line-end-position))
  21707. (point))
  21708. (> (save-excursion
  21709. (goto-char (org-element-property :end element))
  21710. (skip-chars-backward " \r\t\n")
  21711. (line-beginning-position))
  21712. (point))))
  21713. (org-babel-do-in-edit-buffer (call-interactively 'comment-dwim))
  21714. (beginning-of-line)
  21715. (if (looking-at "\\s-*$") (delete-region (point) (point-at-eol))
  21716. (open-line 1))
  21717. (org-indent-line)
  21718. (insert "# ")))
  21719. (defvar comment-empty-lines) ; From newcomment.el.
  21720. (defun org-comment-or-uncomment-region (beg end &rest _)
  21721. "Comment or uncomment each non-blank line in the region.
  21722. Uncomment each non-blank line between BEG and END if it only
  21723. contains commented lines. Otherwise, comment them. If region is
  21724. strictly within a source block, use appropriate comment syntax."
  21725. (if (let ((element (org-element-at-point)))
  21726. (and (eq (org-element-type element) 'src-block)
  21727. (< (save-excursion
  21728. (goto-char (org-element-property :post-affiliated element))
  21729. (line-end-position))
  21730. beg)
  21731. (>= (save-excursion
  21732. (goto-char (org-element-property :end element))
  21733. (skip-chars-backward " \r\t\n")
  21734. (line-beginning-position))
  21735. end)))
  21736. (org-babel-do-in-edit-buffer (call-interactively 'comment-dwim))
  21737. (save-restriction
  21738. ;; Restrict region
  21739. (narrow-to-region (save-excursion (goto-char beg)
  21740. (skip-chars-forward " \r\t\n" end)
  21741. (line-beginning-position))
  21742. (save-excursion (goto-char end)
  21743. (skip-chars-backward " \r\t\n" beg)
  21744. (line-end-position)))
  21745. (let ((uncommentp
  21746. ;; UNCOMMENTP is non-nil when every non blank line between
  21747. ;; BEG and END is a comment.
  21748. (save-excursion
  21749. (goto-char (point-min))
  21750. (while (and (not (eobp))
  21751. (let ((element (org-element-at-point)))
  21752. (and (eq (org-element-type element) 'comment)
  21753. (goto-char (min (point-max)
  21754. (org-element-property
  21755. :end element)))))))
  21756. (eobp))))
  21757. (if uncommentp
  21758. ;; Only blank lines and comments in region: uncomment it.
  21759. (save-excursion
  21760. (goto-char (point-min))
  21761. (while (not (eobp))
  21762. (when (looking-at "[ \t]*\\(#\\(?: \\|$\\)\\)")
  21763. (replace-match "" nil nil nil 1))
  21764. (forward-line)))
  21765. ;; Comment each line in region.
  21766. (let ((min-indent (point-max)))
  21767. ;; First find the minimum indentation across all lines.
  21768. (save-excursion
  21769. (goto-char (point-min))
  21770. (while (and (not (eobp)) (not (zerop min-indent)))
  21771. (unless (looking-at "[ \t]*$")
  21772. (setq min-indent (min min-indent (current-indentation))))
  21773. (forward-line)))
  21774. ;; Then loop over all lines.
  21775. (save-excursion
  21776. (goto-char (point-min))
  21777. (while (not (eobp))
  21778. (unless (and (not comment-empty-lines) (looking-at "[ \t]*$"))
  21779. ;; Don't get fooled by invisible text (e.g. link path)
  21780. ;; when moving to column MIN-INDENT.
  21781. (let ((buffer-invisibility-spec nil))
  21782. (org-move-to-column min-indent t))
  21783. (insert comment-start))
  21784. (forward-line)))))))))
  21785. (defun org-comment-dwim (arg)
  21786. "Call `comment-dwim' within a source edit buffer if needed."
  21787. (interactive "*P")
  21788. (if (org-in-src-block-p)
  21789. (org-babel-do-in-edit-buffer (call-interactively 'comment-dwim))
  21790. (call-interactively 'comment-dwim)))
  21791. ;;; Timestamps API
  21792. ;; This section contains tools to operate on timestamp objects, as
  21793. ;; returned by, e.g. `org-element-context'.
  21794. (defun org-timestamp--to-internal-time (timestamp &optional end)
  21795. "Encode TIMESTAMP object into Emacs internal time.
  21796. Use end of date range or time range when END is non-nil."
  21797. (apply #'encode-time
  21798. (cons 0
  21799. (mapcar
  21800. (lambda (prop) (or (org-element-property prop timestamp) 0))
  21801. (if end '(:minute-end :hour-end :day-end :month-end :year-end)
  21802. '(:minute-start :hour-start :day-start :month-start
  21803. :year-start))))))
  21804. (defun org-timestamp-has-time-p (timestamp)
  21805. "Non-nil when TIMESTAMP has a time specified."
  21806. (org-element-property :hour-start timestamp))
  21807. (defun org-timestamp-format (timestamp format &optional end utc)
  21808. "Format a TIMESTAMP object into a string.
  21809. FORMAT is a format specifier to be passed to
  21810. `format-time-string'.
  21811. When optional argument END is non-nil, use end of date-range or
  21812. time-range, if possible.
  21813. When optional argument UTC is non-nil, time will be expressed as
  21814. Universal Time."
  21815. (format-time-string
  21816. format (org-timestamp--to-internal-time timestamp end) utc))
  21817. (defun org-timestamp-split-range (timestamp &optional end)
  21818. "Extract a TIMESTAMP object from a date or time range.
  21819. END, when non-nil, means extract the end of the range.
  21820. Otherwise, extract its start.
  21821. Return a new timestamp object."
  21822. (let ((type (org-element-property :type timestamp)))
  21823. (if (memq type '(active inactive diary)) timestamp
  21824. (let ((split-ts (org-element-copy timestamp)))
  21825. ;; Set new type.
  21826. (org-element-put-property
  21827. split-ts :type (if (eq type 'active-range) 'active 'inactive))
  21828. ;; Copy start properties over end properties if END is
  21829. ;; non-nil. Otherwise, copy end properties over `start' ones.
  21830. (let ((p-alist '((:minute-start . :minute-end)
  21831. (:hour-start . :hour-end)
  21832. (:day-start . :day-end)
  21833. (:month-start . :month-end)
  21834. (:year-start . :year-end))))
  21835. (dolist (p-cell p-alist)
  21836. (org-element-put-property
  21837. split-ts
  21838. (funcall (if end #'car #'cdr) p-cell)
  21839. (org-element-property
  21840. (funcall (if end #'cdr #'car) p-cell) split-ts)))
  21841. ;; Eventually refresh `:raw-value'.
  21842. (org-element-put-property split-ts :raw-value nil)
  21843. (org-element-put-property
  21844. split-ts :raw-value (org-element-interpret-data split-ts)))))))
  21845. (defun org-timestamp-translate (timestamp &optional boundary)
  21846. "Translate TIMESTAMP object to custom format.
  21847. Format string is defined in `org-time-stamp-custom-formats',
  21848. which see.
  21849. When optional argument BOUNDARY is non-nil, it is either the
  21850. symbol `start' or `end'. In this case, only translate the
  21851. starting or ending part of TIMESTAMP if it is a date or time
  21852. range. Otherwise, translate both parts.
  21853. Return timestamp as-is if `org-display-custom-times' is nil or if
  21854. it has a `diary' type."
  21855. (let ((type (org-element-property :type timestamp)))
  21856. (if (or (not org-display-custom-times) (eq type 'diary))
  21857. (org-element-interpret-data timestamp)
  21858. (let ((fmt (funcall (if (org-timestamp-has-time-p timestamp) #'cdr #'car)
  21859. org-time-stamp-custom-formats)))
  21860. (if (and (not boundary) (memq type '(active-range inactive-range)))
  21861. (concat (org-timestamp-format timestamp fmt)
  21862. "--"
  21863. (org-timestamp-format timestamp fmt t))
  21864. (org-timestamp-format timestamp fmt (eq boundary 'end)))))))
  21865. ;;; Other stuff.
  21866. (defvar reftex-docstruct-symbol)
  21867. (defvar reftex-cite-format)
  21868. (defvar org--rds)
  21869. (defun org-reftex-citation ()
  21870. "Use reftex-citation to insert a citation into the buffer.
  21871. This looks for a line like
  21872. #+BIBLIOGRAPHY: foo plain option:-d
  21873. and derives from it that foo.bib is the bibliography file relevant
  21874. for this document. It then installs the necessary environment for RefTeX
  21875. to work in this buffer and calls `reftex-citation' to insert a citation
  21876. into the buffer.
  21877. Export of such citations to both LaTeX and HTML is handled by the contributed
  21878. package ox-bibtex by Taru Karttunen."
  21879. (interactive)
  21880. (let ((reftex-docstruct-symbol 'org--rds)
  21881. (reftex-cite-format "\\cite{%l}")
  21882. org--rds bib)
  21883. (save-excursion
  21884. (save-restriction
  21885. (widen)
  21886. (let ((case-fold-search t)
  21887. (re "^[ \t]*#\\+BIBLIOGRAPHY:[ \t]+\\([^ \t\n]+\\)"))
  21888. (if (not (save-excursion
  21889. (or (re-search-forward re nil t)
  21890. (re-search-backward re nil t))))
  21891. (user-error "No bibliography defined in file")
  21892. (setq bib (concat (match-string 1) ".bib")
  21893. org--rds (list (list 'bib bib)))))))
  21894. (call-interactively 'reftex-citation)))
  21895. ;;;; Functions extending outline functionality
  21896. (defun org-beginning-of-line (&optional arg)
  21897. "Go to the beginning of the current line. If that is invisible, continue
  21898. to a visible line beginning. This makes the function of C-a more intuitive.
  21899. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  21900. first attempt, and only move to after the tags when the cursor is already
  21901. beyond the end of the headline."
  21902. (interactive "P")
  21903. (let ((pos (point))
  21904. (special (if (consp org-special-ctrl-a/e)
  21905. (car org-special-ctrl-a/e)
  21906. org-special-ctrl-a/e))
  21907. deactivate-mark refpos)
  21908. (if (org-bound-and-true-p visual-line-mode)
  21909. (beginning-of-visual-line 1)
  21910. (beginning-of-line 1))
  21911. (if (and arg (fboundp 'move-beginning-of-line))
  21912. (call-interactively 'move-beginning-of-line)
  21913. (if (bobp)
  21914. nil
  21915. (backward-char 1)
  21916. (if (org-truely-invisible-p)
  21917. (while (and (not (bobp)) (org-truely-invisible-p))
  21918. (backward-char 1)
  21919. (beginning-of-line 1))
  21920. (forward-char 1))))
  21921. (when special
  21922. (cond
  21923. ((and (looking-at org-complex-heading-regexp)
  21924. (eq (char-after (match-end 1)) ?\s))
  21925. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  21926. (point-at-eol)))
  21927. (goto-char
  21928. (if (eq special t)
  21929. (cond ((> pos refpos) refpos)
  21930. ((= pos (point)) refpos)
  21931. (t (point)))
  21932. (cond ((> pos (point)) (point))
  21933. ((not (eq last-command this-command)) (point))
  21934. (t refpos)))))
  21935. ((org-at-item-p)
  21936. ;; Being at an item and not looking at an the item means point
  21937. ;; was previously moved to beginning of a visual line, which
  21938. ;; doesn't contain the item. Therefore, do nothing special,
  21939. ;; just stay here.
  21940. (when (looking-at org-list-full-item-re)
  21941. ;; Set special position at first white space character after
  21942. ;; bullet, and check-box, if any.
  21943. (let ((after-bullet
  21944. (let ((box (match-end 3)))
  21945. (if (not box) (match-end 1)
  21946. (let ((after (char-after box)))
  21947. (if (and after (= after ? )) (1+ box) box))))))
  21948. ;; Special case: Move point to special position when
  21949. ;; currently after it or at beginning of line.
  21950. (if (eq special t)
  21951. (when (or (> pos after-bullet) (= (point) pos))
  21952. (goto-char after-bullet))
  21953. ;; Reversed case: Move point to special position when
  21954. ;; point was already at beginning of line and command is
  21955. ;; repeated.
  21956. (when (and (= (point) pos) (eq last-command this-command))
  21957. (goto-char after-bullet))))))))
  21958. (org-no-warnings
  21959. (and (featurep 'xemacs) (setq zmacs-region-stays t))))
  21960. (setq disable-point-adjustment
  21961. (or (not (invisible-p (point)))
  21962. (not (invisible-p (max (point-min) (1- (point))))))))
  21963. (defun org-end-of-line (&optional arg)
  21964. "Go to the end of the line.
  21965. If this is a headline, and `org-special-ctrl-a/e' is set, ignore
  21966. tags on the first attempt, and only move to after the tags when
  21967. the cursor is already beyond the end of the headline."
  21968. (interactive "P")
  21969. (let ((special (if (consp org-special-ctrl-a/e) (cdr org-special-ctrl-a/e)
  21970. org-special-ctrl-a/e))
  21971. (move-fun (cond ((org-bound-and-true-p visual-line-mode)
  21972. 'end-of-visual-line)
  21973. ((fboundp 'move-end-of-line) 'move-end-of-line)
  21974. (t 'end-of-line)))
  21975. deactivate-mark)
  21976. (if (or (not special) arg) (call-interactively move-fun)
  21977. (let* ((element (save-excursion (beginning-of-line)
  21978. (org-element-at-point)))
  21979. (type (org-element-type element)))
  21980. (cond
  21981. ((memq type '(headline inlinetask))
  21982. (let ((pos (point)))
  21983. (beginning-of-line 1)
  21984. (if (looking-at (org-re ".*?\\(?:\\([ \t]*\\)\\(:[[:alnum:]_@#%:]+:\\)?[ \t]*\\)?$"))
  21985. (if (eq special t)
  21986. (if (or (< pos (match-beginning 1)) (= pos (match-end 0)))
  21987. (goto-char (match-beginning 1))
  21988. (goto-char (match-end 0)))
  21989. (if (or (< pos (match-end 0))
  21990. (not (eq this-command last-command)))
  21991. (goto-char (match-end 0))
  21992. (goto-char (match-beginning 1))))
  21993. (call-interactively move-fun))))
  21994. ((outline-invisible-p (line-end-position))
  21995. ;; If element is hidden, `move-end-of-line' would put point
  21996. ;; after it. Use `end-of-line' to stay on current line.
  21997. (call-interactively 'end-of-line))
  21998. (t (call-interactively move-fun)))))
  21999. (org-no-warnings (and (featurep 'xemacs) (setq zmacs-region-stays t))))
  22000. (setq disable-point-adjustment
  22001. (or (not (invisible-p (point)))
  22002. (not (invisible-p (max (point-min) (1- (point))))))))
  22003. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  22004. (define-key org-mode-map "\C-e" 'org-end-of-line)
  22005. (defun org-backward-sentence (&optional _arg)
  22006. "Go to beginning of sentence, or beginning of table field.
  22007. This will call `backward-sentence' or `org-table-beginning-of-field',
  22008. depending on context."
  22009. (interactive)
  22010. (let* ((element (org-element-at-point))
  22011. (contents-begin (org-element-property :contents-begin element))
  22012. (table (org-element-lineage element '(table) t)))
  22013. (if (and table
  22014. (> (point) contents-begin)
  22015. (<= (point) (org-element-property :contents-end table)))
  22016. (call-interactively #'org-table-beginning-of-field)
  22017. (save-restriction
  22018. (when (and contents-begin
  22019. (< (point-min) contents-begin)
  22020. (> (point) contents-begin))
  22021. (narrow-to-region contents-begin
  22022. (org-element-property :contents-end element)))
  22023. (call-interactively #'backward-sentence)))))
  22024. (defun org-forward-sentence (&optional _arg)
  22025. "Go to end of sentence, or end of table field.
  22026. This will call `forward-sentence' or `org-table-end-of-field',
  22027. depending on context."
  22028. (interactive)
  22029. (let* ((element (org-element-at-point))
  22030. (contents-end (org-element-property :contents-end element))
  22031. (table (org-element-lineage element '(table) t)))
  22032. (if (and table
  22033. (>= (point) (org-element-property :contents-begin table))
  22034. (< (point) contents-end))
  22035. (call-interactively #'org-table-end-of-field)
  22036. (save-restriction
  22037. (when (and contents-end
  22038. (> (point-max) contents-end)
  22039. ;; Skip blank lines between elements.
  22040. (< (org-element-property :end element)
  22041. (save-excursion (goto-char contents-end)
  22042. (skip-chars-forward " \r\t\n"))))
  22043. (narrow-to-region (org-element-property :contents-begin element)
  22044. contents-end))
  22045. (call-interactively #'forward-sentence)))))
  22046. (define-key org-mode-map "\M-a" 'org-backward-sentence)
  22047. (define-key org-mode-map "\M-e" 'org-forward-sentence)
  22048. (defun org-kill-line (&optional _arg)
  22049. "Kill line, to tags or end of line."
  22050. (interactive)
  22051. (cond
  22052. ((or (not org-special-ctrl-k)
  22053. (bolp)
  22054. (not (org-at-heading-p)))
  22055. (if (and (get-char-property (min (point-max) (point-at-eol)) 'invisible)
  22056. org-ctrl-k-protect-subtree)
  22057. (if (or (eq org-ctrl-k-protect-subtree 'error)
  22058. (not (y-or-n-p "Kill hidden subtree along with headline? ")))
  22059. (user-error "C-k aborted as it would kill a hidden subtree")))
  22060. (call-interactively
  22061. (if (org-bound-and-true-p visual-line-mode) 'kill-visual-line 'kill-line)))
  22062. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$"))
  22063. (kill-region (point) (match-beginning 1))
  22064. (org-set-tags nil t))
  22065. (t (kill-region (point) (point-at-eol)))))
  22066. (define-key org-mode-map "\C-k" 'org-kill-line)
  22067. (defun org-yank (&optional arg)
  22068. "Yank. If the kill is a subtree, treat it specially.
  22069. This command will look at the current kill and check if is a single
  22070. subtree, or a series of subtrees[1]. If it passes the test, and if the
  22071. cursor is at the beginning of a line or after the stars of a currently
  22072. empty headline, then the yank is handled specially. How exactly depends
  22073. on the value of the following variables, both set by default.
  22074. `org-yank-folded-subtrees'
  22075. When set, the subtree(s) will be folded after insertion, but only
  22076. if doing so would now swallow text after the yanked text.
  22077. `org-yank-adjusted-subtrees'
  22078. When set, the subtree will be promoted or demoted in order to
  22079. fit into the local outline tree structure, which means that the
  22080. level will be adjusted so that it becomes the smaller one of the
  22081. two *visible* surrounding headings.
  22082. Any prefix to this command will cause `yank' to be called directly with
  22083. no special treatment. In particular, a simple \\[universal-argument] prefix \
  22084. will just
  22085. plainly yank the text as it is.
  22086. \[1] The test checks if the first non-white line is a heading
  22087. and if there are no other headings with fewer stars."
  22088. (interactive "P")
  22089. (org-yank-generic 'yank arg))
  22090. (defun org-yank-generic (command arg)
  22091. "Perform some yank-like command.
  22092. This function implements the behavior described in the `org-yank'
  22093. documentation. However, it has been generalized to work for any
  22094. interactive command with similar behavior."
  22095. ;; pretend to be command COMMAND
  22096. (setq this-command command)
  22097. (if arg
  22098. (call-interactively command)
  22099. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  22100. (and (org-kill-is-subtree-p)
  22101. (or (bolp)
  22102. (and (looking-at "[ \t]*$")
  22103. (string-match
  22104. "\\`\\*+\\'"
  22105. (buffer-substring (point-at-bol) (point)))))))
  22106. swallowp)
  22107. (cond
  22108. ((and subtreep org-yank-folded-subtrees)
  22109. (let ((beg (point))
  22110. end)
  22111. (if (and subtreep org-yank-adjusted-subtrees)
  22112. (org-paste-subtree nil nil 'for-yank)
  22113. (call-interactively command))
  22114. (setq end (point))
  22115. (goto-char beg)
  22116. (when (and (bolp) subtreep
  22117. (not (setq swallowp
  22118. (org-yank-folding-would-swallow-text beg end))))
  22119. (org-with-limited-levels
  22120. (or (looking-at org-outline-regexp)
  22121. (re-search-forward org-outline-regexp-bol end t))
  22122. (while (and (< (point) end) (looking-at org-outline-regexp))
  22123. (hide-subtree)
  22124. (org-cycle-show-empty-lines 'folded)
  22125. (condition-case nil
  22126. (outline-forward-same-level 1)
  22127. (error (goto-char end))))))
  22128. (when swallowp
  22129. (message
  22130. "Inserted text not folded because that would swallow text"))
  22131. (goto-char end)
  22132. (skip-chars-forward " \t\n\r")
  22133. (beginning-of-line 1)
  22134. (push-mark beg 'nomsg)))
  22135. ((and subtreep org-yank-adjusted-subtrees)
  22136. (let ((beg (point-at-bol)))
  22137. (org-paste-subtree nil nil 'for-yank)
  22138. (push-mark beg 'nomsg)))
  22139. (t
  22140. (call-interactively command))))))
  22141. (defun org-yank-folding-would-swallow-text (beg end)
  22142. "Would hide-subtree at BEG swallow any text after END?"
  22143. (let (level)
  22144. (org-with-limited-levels
  22145. (save-excursion
  22146. (goto-char beg)
  22147. (when (or (looking-at org-outline-regexp)
  22148. (re-search-forward org-outline-regexp-bol end t))
  22149. (setq level (org-outline-level)))
  22150. (goto-char end)
  22151. (skip-chars-forward " \t\r\n\v\f")
  22152. (if (or (eobp)
  22153. (and (bolp) (looking-at org-outline-regexp)
  22154. (<= (org-outline-level) level)))
  22155. nil ; Nothing would be swallowed
  22156. t))))) ; something would swallow
  22157. (define-key org-mode-map "\C-y" 'org-yank)
  22158. (defun org-truely-invisible-p ()
  22159. "Check if point is at a character currently not visible.
  22160. This version does not only check the character property, but also
  22161. `visible-mode'."
  22162. ;; Early versions of noutline don't have `outline-invisible-p'.
  22163. (if (org-bound-and-true-p visible-mode)
  22164. nil
  22165. (outline-invisible-p)))
  22166. (defun org-invisible-p2 ()
  22167. "Check if point is at a character currently not visible."
  22168. (save-excursion
  22169. (if (and (eolp) (not (bobp))) (backward-char 1))
  22170. ;; Early versions of noutline don't have `outline-invisible-p'.
  22171. (outline-invisible-p)))
  22172. (defun org-back-to-heading (&optional invisible-ok)
  22173. "Call `outline-back-to-heading', but provide a better error message."
  22174. (condition-case nil
  22175. (outline-back-to-heading invisible-ok)
  22176. (error (error "Before first headline at position %d in buffer %s"
  22177. (point) (current-buffer)))))
  22178. (defun org-before-first-heading-p ()
  22179. "Before first heading?"
  22180. (save-excursion
  22181. (end-of-line)
  22182. (null (re-search-backward org-outline-regexp-bol nil t))))
  22183. (defun org-at-heading-p (&optional ignored)
  22184. (outline-on-heading-p t))
  22185. ;; Compatibility alias with Org versions < 7.8.03
  22186. (defalias 'org-on-heading-p 'org-at-heading-p)
  22187. (defun org-in-commented-heading-p (&optional no-inheritance)
  22188. "Non-nil if point is under a commented heading.
  22189. This function also checks ancestors of the current headline,
  22190. unless optional argument NO-INHERITANCE is non-nil."
  22191. (cond
  22192. ((org-before-first-heading-p) nil)
  22193. ((let ((headline (nth 4 (org-heading-components))))
  22194. (and headline
  22195. (let ((case-fold-search nil))
  22196. (org-string-match-p (concat "^" org-comment-string "\\(?: \\|$\\)")
  22197. headline)))))
  22198. (no-inheritance nil)
  22199. (t
  22200. (save-excursion (and (org-up-heading-safe) (org-in-commented-heading-p))))))
  22201. (defun org-at-comment-p nil
  22202. "Is cursor in a commented line?"
  22203. (save-excursion
  22204. (save-match-data
  22205. (beginning-of-line)
  22206. (looking-at "^[ \t]*# "))))
  22207. (defun org-at-drawer-p nil
  22208. "Is cursor at a drawer keyword?"
  22209. (save-excursion
  22210. (move-beginning-of-line 1)
  22211. (looking-at org-drawer-regexp)))
  22212. (defun org-at-block-p nil
  22213. "Is cursor at a block keyword?"
  22214. (save-excursion
  22215. (move-beginning-of-line 1)
  22216. (looking-at org-block-regexp)))
  22217. (defun org-point-at-end-of-empty-headline ()
  22218. "If point is at the end of an empty headline, return t, else nil.
  22219. If the heading only contains a TODO keyword, it is still still considered
  22220. empty."
  22221. (and (looking-at "[ \t]*$")
  22222. (when org-todo-line-regexp
  22223. (save-excursion
  22224. (beginning-of-line 1)
  22225. (let ((case-fold-search nil))
  22226. (looking-at org-todo-line-regexp)
  22227. (string= (match-string 3) ""))))))
  22228. (defun org-at-heading-or-item-p ()
  22229. (or (org-at-heading-p) (org-at-item-p)))
  22230. (defun org-at-target-p ()
  22231. (or (org-in-regexp org-radio-target-regexp)
  22232. (org-in-regexp org-target-regexp)))
  22233. ;; Compatibility alias with Org versions < 7.8.03
  22234. (defalias 'org-on-target-p 'org-at-target-p)
  22235. (defun org-up-heading-all (arg)
  22236. "Move to the heading line of which the present line is a subheading.
  22237. This function considers both visible and invisible heading lines.
  22238. With argument, move up ARG levels."
  22239. (if (fboundp 'outline-up-heading-all)
  22240. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  22241. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  22242. (defun org-up-heading-safe ()
  22243. "Move to the heading line of which the present line is a subheading.
  22244. This version will not throw an error. It will return the level of the
  22245. headline found, or nil if no higher level is found.
  22246. Also, this function will be a lot faster than `outline-up-heading',
  22247. because it relies on stars being the outline starters. This can really
  22248. make a significant difference in outlines with very many siblings."
  22249. (when (ignore-errors (org-back-to-heading t))
  22250. (let ((level-up (1- (funcall outline-level))))
  22251. (and (> level-up 0)
  22252. (re-search-backward (format "^\\*\\{1,%d\\} " level-up) nil t)
  22253. (funcall outline-level)))))
  22254. (defun org-first-sibling-p ()
  22255. "Is this heading the first child of its parents?"
  22256. (interactive)
  22257. (let ((re org-outline-regexp-bol)
  22258. level l)
  22259. (unless (org-at-heading-p t)
  22260. (user-error "Not at a heading"))
  22261. (setq level (funcall outline-level))
  22262. (save-excursion
  22263. (if (not (re-search-backward re nil t))
  22264. t
  22265. (setq l (funcall outline-level))
  22266. (< l level)))))
  22267. (defun org-goto-sibling (&optional previous)
  22268. "Goto the next sibling, even if it is invisible.
  22269. When PREVIOUS is set, go to the previous sibling instead. Returns t
  22270. when a sibling was found. When none is found, return nil and don't
  22271. move point."
  22272. (let ((fun (if previous 're-search-backward 're-search-forward))
  22273. (pos (point))
  22274. (re org-outline-regexp-bol)
  22275. level l)
  22276. (when (ignore-errors (org-back-to-heading t))
  22277. (setq level (funcall outline-level))
  22278. (catch 'exit
  22279. (or previous (forward-char 1))
  22280. (while (funcall fun re nil t)
  22281. (setq l (funcall outline-level))
  22282. (when (< l level) (goto-char pos) (throw 'exit nil))
  22283. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  22284. (goto-char pos)
  22285. nil))))
  22286. (defun org-show-siblings ()
  22287. "Show all siblings of the current headline."
  22288. (save-excursion
  22289. (while (org-goto-sibling) (org-flag-heading nil)))
  22290. (save-excursion
  22291. (while (org-goto-sibling 'previous)
  22292. (org-flag-heading nil))))
  22293. (defun org-goto-first-child ()
  22294. "Goto the first child, even if it is invisible.
  22295. Return t when a child was found. Otherwise don't move point and
  22296. return nil."
  22297. (let (level (pos (point)) (re org-outline-regexp-bol))
  22298. (when (ignore-errors (org-back-to-heading t))
  22299. (setq level (outline-level))
  22300. (forward-char 1)
  22301. (if (and (re-search-forward re nil t) (> (outline-level) level))
  22302. (progn (goto-char (match-beginning 0)) t)
  22303. (goto-char pos) nil))))
  22304. (defun org-show-hidden-entry ()
  22305. "Show an entry where even the heading is hidden."
  22306. (save-excursion
  22307. (org-show-entry)))
  22308. (defun org-flag-heading (flag &optional entry)
  22309. "Flag the current heading. FLAG non-nil means make invisible.
  22310. When ENTRY is non-nil, show the entire entry."
  22311. (save-excursion
  22312. (org-back-to-heading t)
  22313. ;; Check if we should show the entire entry
  22314. (if entry
  22315. (progn
  22316. (org-show-entry)
  22317. (save-excursion
  22318. (and (outline-next-heading)
  22319. (org-flag-heading nil))))
  22320. (outline-flag-region (max (point-min) (1- (point)))
  22321. (save-excursion (outline-end-of-heading) (point))
  22322. flag))))
  22323. (defun org-get-next-sibling ()
  22324. "Move to next heading of the same level, and return point.
  22325. If there is no such heading, return nil.
  22326. This is like outline-next-sibling, but invisible headings are ok."
  22327. (let ((level (funcall outline-level)))
  22328. (outline-next-heading)
  22329. (while (and (not (eobp)) (> (funcall outline-level) level))
  22330. (outline-next-heading))
  22331. (if (or (eobp) (< (funcall outline-level) level))
  22332. nil
  22333. (point))))
  22334. (defun org-get-last-sibling ()
  22335. "Move to previous heading of the same level, and return point.
  22336. If there is no such heading, return nil."
  22337. (let ((opoint (point))
  22338. (level (funcall outline-level)))
  22339. (outline-previous-heading)
  22340. (when (and (/= (point) opoint) (outline-on-heading-p t))
  22341. (while (and (> (funcall outline-level) level)
  22342. (not (bobp)))
  22343. (outline-previous-heading))
  22344. (if (< (funcall outline-level) level)
  22345. nil
  22346. (point)))))
  22347. (defun org-end-of-subtree (&optional invisible-ok to-heading)
  22348. "Goto to the end of a subtree."
  22349. ;; This contains an exact copy of the original function, but it uses
  22350. ;; `org-back-to-heading', to make it work also in invisible
  22351. ;; trees. And is uses an invisible-ok argument.
  22352. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  22353. ;; Furthermore, when used inside Org, finding the end of a large subtree
  22354. ;; with many children and grandchildren etc, this can be much faster
  22355. ;; than the outline version.
  22356. (org-back-to-heading invisible-ok)
  22357. (let ((first t)
  22358. (level (funcall outline-level)))
  22359. (if (and (derived-mode-p 'org-mode) (< level 1000))
  22360. ;; A true heading (not a plain list item), in Org-mode
  22361. ;; This means we can easily find the end by looking
  22362. ;; only for the right number of stars. Using a regexp to do
  22363. ;; this is so much faster than using a Lisp loop.
  22364. (let ((re (concat "^\\*\\{1," (int-to-string level) "\\} ")))
  22365. (forward-char 1)
  22366. (and (re-search-forward re nil 'move) (beginning-of-line 1)))
  22367. ;; something else, do it the slow way
  22368. (while (and (not (eobp))
  22369. (or first (> (funcall outline-level) level)))
  22370. (setq first nil)
  22371. (outline-next-heading)))
  22372. (unless to-heading
  22373. (if (memq (preceding-char) '(?\n ?\^M))
  22374. (progn
  22375. ;; Go to end of line before heading
  22376. (forward-char -1)
  22377. (if (memq (preceding-char) '(?\n ?\^M))
  22378. ;; leave blank line before heading
  22379. (forward-char -1))))))
  22380. (point))
  22381. (defun org-end-of-meta-data (&optional full)
  22382. "Skip planning line and properties drawer in current entry.
  22383. When optional argument FULL is non-nil, also skip empty lines,
  22384. clocking lines and regular drawers at the beginning of the
  22385. entry."
  22386. (org-back-to-heading t)
  22387. (forward-line)
  22388. (when (org-looking-at-p org-planning-line-re) (forward-line))
  22389. (when (looking-at org-property-drawer-re)
  22390. (goto-char (match-end 0))
  22391. (forward-line))
  22392. (when (and full (not (org-at-heading-p)))
  22393. (catch 'exit
  22394. (let ((end (save-excursion (outline-next-heading) (point)))
  22395. (re (concat "[ \t]*$" "\\|" org-clock-line-re)))
  22396. (while (not (eobp))
  22397. (cond ((org-looking-at-p org-drawer-regexp)
  22398. (if (re-search-forward "^[ \t]*:END:[ \t]*$" end t)
  22399. (forward-line)
  22400. (throw 'exit t)))
  22401. ((org-looking-at-p re) (forward-line))
  22402. (t (throw 'exit t))))))))
  22403. (defun org-forward-heading-same-level (arg &optional invisible-ok)
  22404. "Move forward to the ARG'th subheading at same level as this one.
  22405. Stop at the first and last subheadings of a superior heading.
  22406. Normally this only looks at visible headings, but when INVISIBLE-OK is
  22407. non-nil it will also look at invisible ones."
  22408. (interactive "p")
  22409. (if (not (ignore-errors (org-back-to-heading invisible-ok)))
  22410. (if (and arg (< arg 0))
  22411. (goto-char (point-min))
  22412. (outline-next-heading))
  22413. (org-at-heading-p)
  22414. (let ((level (- (match-end 0) (match-beginning 0) 1))
  22415. (f (if (and arg (< arg 0))
  22416. 're-search-backward
  22417. 're-search-forward))
  22418. (count (if arg (abs arg) 1))
  22419. (result (point)))
  22420. (while (and (prog1 (> count 0)
  22421. (forward-char (if (and arg (< arg 0)) -1 1)))
  22422. (funcall f org-outline-regexp-bol nil 'move))
  22423. (let ((l (- (match-end 0) (match-beginning 0) 1)))
  22424. (cond ((< l level) (setq count 0))
  22425. ((and (= l level)
  22426. (or invisible-ok
  22427. (progn
  22428. (goto-char (line-beginning-position))
  22429. (not (outline-invisible-p)))))
  22430. (setq count (1- count))
  22431. (when (eq l level)
  22432. (setq result (point)))))))
  22433. (goto-char result))
  22434. (beginning-of-line 1)))
  22435. (defun org-backward-heading-same-level (arg &optional invisible-ok)
  22436. "Move backward to the ARG'th subheading at same level as this one.
  22437. Stop at the first and last subheadings of a superior heading."
  22438. (interactive "p")
  22439. (org-forward-heading-same-level (if arg (- arg) -1) invisible-ok))
  22440. (defun org-next-visible-heading (arg)
  22441. "Move to the next visible heading.
  22442. This function wraps `outline-next-visible-heading' with
  22443. `org-with-limited-levels' in order to skip over inline tasks and
  22444. respect customization of `org-odd-levels-only'."
  22445. (interactive "p")
  22446. (org-with-limited-levels
  22447. (outline-next-visible-heading arg)))
  22448. (defun org-previous-visible-heading (arg)
  22449. "Move to the next visible heading.
  22450. This function wraps `outline-previous-visible-heading' with
  22451. `org-with-limited-levels' in order to skip over inline tasks and
  22452. respect customization of `org-odd-levels-only'."
  22453. (interactive "p")
  22454. (org-with-limited-levels
  22455. (outline-previous-visible-heading arg)))
  22456. (defun org-next-block (arg &optional backward block-regexp)
  22457. "Jump to the next block.
  22458. With a prefix argument ARG, jump forward ARG many blocks.
  22459. When BACKWARD is non-nil, jump to the previous block.
  22460. When BLOCK-REGEXP is non-nil, use this regexp to find blocks.
  22461. Match data is set according to this regexp when the function
  22462. returns.
  22463. Return point at beginning of the opening line of found block.
  22464. Throw an error if no block is found."
  22465. (interactive "p")
  22466. (let ((re (or block-regexp "^[ \t]*#\\+BEGIN"))
  22467. (case-fold-search t)
  22468. (search-fn (if backward #'re-search-backward #'re-search-forward))
  22469. (count (or arg 1))
  22470. (origin (point))
  22471. last-element)
  22472. (if backward (beginning-of-line) (end-of-line))
  22473. (while (and (> count 0) (funcall search-fn re nil t))
  22474. (let ((element (save-excursion
  22475. (goto-char (match-beginning 0))
  22476. (save-match-data (org-element-at-point)))))
  22477. (when (and (memq (org-element-type element)
  22478. '(center-block comment-block dynamic-block
  22479. example-block export-block quote-block
  22480. special-block src-block verse-block))
  22481. (<= (match-beginning 0)
  22482. (org-element-property :post-affiliated element)))
  22483. (setq last-element element)
  22484. (decf count))))
  22485. (if (= count 0)
  22486. (prog1 (goto-char (org-element-property :post-affiliated last-element))
  22487. (save-match-data (org-show-context)))
  22488. (goto-char origin)
  22489. (user-error "No %s code blocks" (if backward "previous" "further")))))
  22490. (defun org-previous-block (arg &optional block-regexp)
  22491. "Jump to the previous block.
  22492. With a prefix argument ARG, jump backward ARG many source blocks.
  22493. When BLOCK-REGEXP is non-nil, use this regexp to find blocks."
  22494. (interactive "p")
  22495. (org-next-block arg t block-regexp))
  22496. (defun org-forward-paragraph ()
  22497. "Move forward to beginning of next paragraph or equivalent.
  22498. The function moves point to the beginning of the next visible
  22499. structural element, which can be a paragraph, a table, a list
  22500. item, etc. It also provides some special moves for convenience:
  22501. - On an affiliated keyword, jump to the beginning of the
  22502. relative element.
  22503. - On an item or a footnote definition, move to the second
  22504. element inside, if any.
  22505. - On a table or a property drawer, jump after it.
  22506. - On a verse or source block, stop after blank lines."
  22507. (interactive)
  22508. (when (eobp) (user-error "Cannot move further down"))
  22509. (let* ((deactivate-mark nil)
  22510. (element (org-element-at-point))
  22511. (type (org-element-type element))
  22512. (post-affiliated (org-element-property :post-affiliated element))
  22513. (contents-begin (org-element-property :contents-begin element))
  22514. (contents-end (org-element-property :contents-end element))
  22515. (end (let ((end (org-element-property :end element)) (parent element))
  22516. (while (and (setq parent (org-element-property :parent parent))
  22517. (= (org-element-property :contents-end parent) end))
  22518. (setq end (org-element-property :end parent)))
  22519. end)))
  22520. (cond ((not element)
  22521. (skip-chars-forward " \r\t\n")
  22522. (or (eobp) (beginning-of-line)))
  22523. ;; On affiliated keywords, move to element's beginning.
  22524. ((< (point) post-affiliated)
  22525. (goto-char post-affiliated))
  22526. ;; At a table row, move to the end of the table. Similarly,
  22527. ;; at a node property, move to the end of the property
  22528. ;; drawer.
  22529. ((memq type '(node-property table-row))
  22530. (goto-char (org-element-property
  22531. :end (org-element-property :parent element))))
  22532. ((memq type '(property-drawer table)) (goto-char end))
  22533. ;; Consider blank lines as separators in verse and source
  22534. ;; blocks to ease editing.
  22535. ((memq type '(src-block verse-block))
  22536. (when (eq type 'src-block)
  22537. (setq contents-end
  22538. (save-excursion (goto-char end)
  22539. (skip-chars-backward " \r\t\n")
  22540. (line-beginning-position))))
  22541. (beginning-of-line)
  22542. (when (looking-at "[ \t]*$") (skip-chars-forward " \r\t\n"))
  22543. (if (not (re-search-forward "^[ \t]*$" contents-end t))
  22544. (goto-char end)
  22545. (skip-chars-forward " \r\t\n")
  22546. (if (= (point) contents-end) (goto-char end)
  22547. (beginning-of-line))))
  22548. ;; With no contents, just skip element.
  22549. ((not contents-begin) (goto-char end))
  22550. ;; If contents are invisible, skip the element altogether.
  22551. ((outline-invisible-p (line-end-position))
  22552. (case type
  22553. (headline
  22554. (org-with-limited-levels (outline-next-visible-heading 1)))
  22555. ;; At a plain list, make sure we move to the next item
  22556. ;; instead of skipping the whole list.
  22557. (plain-list (forward-char)
  22558. (org-forward-paragraph))
  22559. (otherwise (goto-char end))))
  22560. ((>= (point) contents-end) (goto-char end))
  22561. ((>= (point) contents-begin)
  22562. ;; This can only happen on paragraphs and plain lists.
  22563. (case type
  22564. (paragraph (goto-char end))
  22565. ;; At a plain list, try to move to second element in
  22566. ;; first item, if possible.
  22567. (plain-list (end-of-line)
  22568. (org-forward-paragraph))))
  22569. ;; When contents start on the middle of a line (e.g. in
  22570. ;; items and footnote definitions), try to reach first
  22571. ;; element starting after current line.
  22572. ((> (line-end-position) contents-begin)
  22573. (end-of-line)
  22574. (org-forward-paragraph))
  22575. (t (goto-char contents-begin)))))
  22576. (defun org-backward-paragraph ()
  22577. "Move backward to start of previous paragraph or equivalent.
  22578. The function moves point to the beginning of the current
  22579. structural element, which can be a paragraph, a table, a list
  22580. item, etc., or to the beginning of the previous visible one if
  22581. point is already there. It also provides some special moves for
  22582. convenience:
  22583. - On an affiliated keyword, jump to the first one.
  22584. - On a table or a property drawer, move to its beginning.
  22585. - On a verse or source block, stop before blank lines."
  22586. (interactive)
  22587. (when (bobp) (user-error "Cannot move further up"))
  22588. (let* ((deactivate-mark nil)
  22589. (element (org-element-at-point))
  22590. (type (org-element-type element))
  22591. (contents-begin (org-element-property :contents-begin element))
  22592. (contents-end (org-element-property :contents-end element))
  22593. (post-affiliated (org-element-property :post-affiliated element))
  22594. (begin (org-element-property :begin element)))
  22595. (cond
  22596. ((not element) (goto-char (point-min)))
  22597. ((= (point) begin)
  22598. (backward-char)
  22599. (org-backward-paragraph))
  22600. ((<= (point) post-affiliated) (goto-char begin))
  22601. ((memq type '(node-property table-row))
  22602. (goto-char (org-element-property
  22603. :post-affiliated (org-element-property :parent element))))
  22604. ((memq type '(property-drawer table)) (goto-char begin))
  22605. ((memq type '(src-block verse-block))
  22606. (when (eq type 'src-block)
  22607. (setq contents-begin
  22608. (save-excursion (goto-char begin) (forward-line) (point))))
  22609. (if (= (point) contents-begin) (goto-char post-affiliated)
  22610. ;; Inside a verse block, see blank lines as paragraph
  22611. ;; separators.
  22612. (let ((origin (point)))
  22613. (skip-chars-backward " \r\t\n" contents-begin)
  22614. (when (re-search-backward "^[ \t]*$" contents-begin 'move)
  22615. (skip-chars-forward " \r\t\n" origin)
  22616. (if (= (point) origin) (goto-char contents-begin)
  22617. (beginning-of-line))))))
  22618. ((not contents-begin) (goto-char (or post-affiliated begin)))
  22619. ((eq type 'paragraph)
  22620. (goto-char contents-begin)
  22621. ;; When at first paragraph in an item or a footnote definition,
  22622. ;; move directly to beginning of line.
  22623. (let ((parent-contents
  22624. (org-element-property
  22625. :contents-begin (org-element-property :parent element))))
  22626. (when (and parent-contents (= parent-contents contents-begin))
  22627. (beginning-of-line))))
  22628. ;; At the end of a greater element, move to the beginning of the
  22629. ;; last element within.
  22630. ((>= (point) contents-end)
  22631. (goto-char (1- contents-end))
  22632. (org-backward-paragraph))
  22633. (t (goto-char (or post-affiliated begin))))
  22634. ;; Ensure we never leave point invisible.
  22635. (when (outline-invisible-p (point)) (beginning-of-visual-line))))
  22636. (defun org-forward-element ()
  22637. "Move forward by one element.
  22638. Move to the next element at the same level, when possible."
  22639. (interactive)
  22640. (cond ((eobp) (user-error "Cannot move further down"))
  22641. ((org-with-limited-levels (org-at-heading-p))
  22642. (let ((origin (point)))
  22643. (goto-char (org-end-of-subtree nil t))
  22644. (unless (org-with-limited-levels (org-at-heading-p))
  22645. (goto-char origin)
  22646. (user-error "Cannot move further down"))))
  22647. (t
  22648. (let* ((elem (org-element-at-point))
  22649. (end (org-element-property :end elem))
  22650. (parent (org-element-property :parent elem)))
  22651. (cond ((and parent (= (org-element-property :contents-end parent) end))
  22652. (goto-char (org-element-property :end parent)))
  22653. ((integer-or-marker-p end) (goto-char end))
  22654. (t (message "No element at point")))))))
  22655. (defun org-backward-element ()
  22656. "Move backward by one element.
  22657. Move to the previous element at the same level, when possible."
  22658. (interactive)
  22659. (cond ((bobp) (user-error "Cannot move further up"))
  22660. ((org-with-limited-levels (org-at-heading-p))
  22661. ;; At a headline, move to the previous one, if any, or stay
  22662. ;; here.
  22663. (let ((origin (point)))
  22664. (org-with-limited-levels (org-backward-heading-same-level 1))
  22665. ;; When current headline has no sibling above, move to its
  22666. ;; parent.
  22667. (when (= (point) origin)
  22668. (or (org-with-limited-levels (org-up-heading-safe))
  22669. (progn (goto-char origin)
  22670. (user-error "Cannot move further up"))))))
  22671. (t
  22672. (let* ((elem (org-element-at-point))
  22673. (beg (org-element-property :begin elem)))
  22674. (cond
  22675. ;; Move to beginning of current element if point isn't
  22676. ;; there already.
  22677. ((null beg) (message "No element at point"))
  22678. ((/= (point) beg) (goto-char beg))
  22679. (t (goto-char beg)
  22680. (skip-chars-backward " \r\t\n")
  22681. (unless (bobp)
  22682. (let ((prev (org-element-at-point)))
  22683. (goto-char (org-element-property :begin prev))
  22684. (while (and (setq prev (org-element-property :parent prev))
  22685. (<= (org-element-property :end prev) beg))
  22686. (goto-char (org-element-property :begin prev)))))))))))
  22687. (defun org-up-element ()
  22688. "Move to upper element."
  22689. (interactive)
  22690. (if (org-with-limited-levels (org-at-heading-p))
  22691. (unless (org-up-heading-safe) (user-error "No surrounding element"))
  22692. (let* ((elem (org-element-at-point))
  22693. (parent (org-element-property :parent elem)))
  22694. (if parent (goto-char (org-element-property :begin parent))
  22695. (if (org-with-limited-levels (org-before-first-heading-p))
  22696. (user-error "No surrounding element")
  22697. (org-with-limited-levels (org-back-to-heading)))))))
  22698. (defvar org-element-greater-elements)
  22699. (defun org-down-element ()
  22700. "Move to inner element."
  22701. (interactive)
  22702. (let ((element (org-element-at-point)))
  22703. (cond
  22704. ((memq (org-element-type element) '(plain-list table))
  22705. (goto-char (org-element-property :contents-begin element))
  22706. (forward-char))
  22707. ((memq (org-element-type element) org-element-greater-elements)
  22708. ;; If contents are hidden, first disclose them.
  22709. (when (outline-invisible-p (line-end-position)) (org-cycle))
  22710. (goto-char (or (org-element-property :contents-begin element)
  22711. (user-error "No content for this element"))))
  22712. (t (user-error "No inner element")))))
  22713. (defun org-drag-element-backward ()
  22714. "Move backward element at point."
  22715. (interactive)
  22716. (if (org-with-limited-levels (org-at-heading-p)) (org-move-subtree-up)
  22717. (let* ((elem (org-element-at-point))
  22718. (prev-elem
  22719. (save-excursion
  22720. (goto-char (org-element-property :begin elem))
  22721. (skip-chars-backward " \r\t\n")
  22722. (unless (bobp)
  22723. (let* ((beg (org-element-property :begin elem))
  22724. (prev (org-element-at-point))
  22725. (up prev))
  22726. (while (and (setq up (org-element-property :parent up))
  22727. (<= (org-element-property :end up) beg))
  22728. (setq prev up))
  22729. prev)))))
  22730. ;; Error out if no previous element or previous element is
  22731. ;; a parent of the current one.
  22732. (if (or (not prev-elem) (org-element-nested-p elem prev-elem))
  22733. (user-error "Cannot drag element backward")
  22734. (let ((pos (point)))
  22735. (org-element-swap-A-B prev-elem elem)
  22736. (goto-char (+ (org-element-property :begin prev-elem)
  22737. (- pos (org-element-property :begin elem)))))))))
  22738. (defun org-drag-element-forward ()
  22739. "Move forward element at point."
  22740. (interactive)
  22741. (let* ((pos (point))
  22742. (elem (org-element-at-point)))
  22743. (when (= (point-max) (org-element-property :end elem))
  22744. (user-error "Cannot drag element forward"))
  22745. (goto-char (org-element-property :end elem))
  22746. (let ((next-elem (org-element-at-point)))
  22747. (when (or (org-element-nested-p elem next-elem)
  22748. (and (eq (org-element-type next-elem) 'headline)
  22749. (not (eq (org-element-type elem) 'headline))))
  22750. (goto-char pos)
  22751. (user-error "Cannot drag element forward"))
  22752. ;; Compute new position of point: it's shifted by NEXT-ELEM
  22753. ;; body's length (without final blanks) and by the length of
  22754. ;; blanks between ELEM and NEXT-ELEM.
  22755. (let ((size-next (- (save-excursion
  22756. (goto-char (org-element-property :end next-elem))
  22757. (skip-chars-backward " \r\t\n")
  22758. (forward-line)
  22759. ;; Small correction if buffer doesn't end
  22760. ;; with a newline character.
  22761. (if (and (eolp) (not (bolp))) (1+ (point)) (point)))
  22762. (org-element-property :begin next-elem)))
  22763. (size-blank (- (org-element-property :end elem)
  22764. (save-excursion
  22765. (goto-char (org-element-property :end elem))
  22766. (skip-chars-backward " \r\t\n")
  22767. (forward-line)
  22768. (point)))))
  22769. (org-element-swap-A-B elem next-elem)
  22770. (goto-char (+ pos size-next size-blank))))))
  22771. (defun org-drag-line-forward (arg)
  22772. "Drag the line at point ARG lines forward."
  22773. (interactive "p")
  22774. (dotimes (n (abs arg))
  22775. (let ((c (current-column)))
  22776. (if (< 0 arg)
  22777. (progn
  22778. (beginning-of-line 2)
  22779. (transpose-lines 1)
  22780. (beginning-of-line 0))
  22781. (transpose-lines 1)
  22782. (beginning-of-line -1))
  22783. (org-move-to-column c))))
  22784. (defun org-drag-line-backward (arg)
  22785. "Drag the line at point ARG lines backward."
  22786. (interactive "p")
  22787. (org-drag-line-forward (- arg)))
  22788. (defun org-mark-element ()
  22789. "Put point at beginning of this element, mark at end.
  22790. Interactively, if this command is repeated or (in Transient Mark
  22791. mode) if the mark is active, it marks the next element after the
  22792. ones already marked."
  22793. (interactive)
  22794. (let (deactivate-mark)
  22795. (if (and (org-called-interactively-p 'any)
  22796. (or (and (eq last-command this-command) (mark t))
  22797. (and transient-mark-mode mark-active)))
  22798. (set-mark
  22799. (save-excursion
  22800. (goto-char (mark))
  22801. (goto-char (org-element-property :end (org-element-at-point)))))
  22802. (let ((element (org-element-at-point)))
  22803. (end-of-line)
  22804. (push-mark (org-element-property :end element) t t)
  22805. (goto-char (org-element-property :begin element))))))
  22806. (defun org-narrow-to-element ()
  22807. "Narrow buffer to current element."
  22808. (interactive)
  22809. (let ((elem (org-element-at-point)))
  22810. (cond
  22811. ((eq (car elem) 'headline)
  22812. (narrow-to-region
  22813. (org-element-property :begin elem)
  22814. (org-element-property :end elem)))
  22815. ((memq (car elem) org-element-greater-elements)
  22816. (narrow-to-region
  22817. (org-element-property :contents-begin elem)
  22818. (org-element-property :contents-end elem)))
  22819. (t
  22820. (narrow-to-region
  22821. (org-element-property :begin elem)
  22822. (org-element-property :end elem))))))
  22823. (defun org-transpose-element ()
  22824. "Transpose current and previous elements, keeping blank lines between.
  22825. Point is moved after both elements."
  22826. (interactive)
  22827. (org-skip-whitespace)
  22828. (let ((end (org-element-property :end (org-element-at-point))))
  22829. (org-drag-element-backward)
  22830. (goto-char end)))
  22831. (defun org-unindent-buffer ()
  22832. "Un-indent the visible part of the buffer.
  22833. Relative indentation (between items, inside blocks, etc.) isn't
  22834. modified."
  22835. (interactive)
  22836. (unless (eq major-mode 'org-mode)
  22837. (user-error "Cannot un-indent a buffer not in Org mode"))
  22838. (let* ((parse-tree (org-element-parse-buffer 'greater-element))
  22839. unindent-tree ; For byte-compiler.
  22840. (unindent-tree
  22841. (function
  22842. (lambda (contents)
  22843. (mapc
  22844. (lambda (element)
  22845. (if (memq (org-element-type element) '(headline section))
  22846. (funcall unindent-tree (org-element-contents element))
  22847. (save-excursion
  22848. (save-restriction
  22849. (narrow-to-region
  22850. (org-element-property :begin element)
  22851. (org-element-property :end element))
  22852. (org-do-remove-indentation)))))
  22853. (reverse contents))))))
  22854. (funcall unindent-tree (org-element-contents parse-tree))))
  22855. (defun org-show-children (&optional level)
  22856. "Show all direct subheadings of this heading.
  22857. Prefix arg LEVEL is how many levels below the current level should be shown.
  22858. Default is enough to cause the following heading to appear."
  22859. (save-excursion
  22860. (org-back-to-heading t)
  22861. (let* ((current-level (funcall outline-level))
  22862. (max-level (org-get-valid-level
  22863. current-level (if level (prefix-numeric-value level) 1)))
  22864. (end (save-excursion (org-end-of-subtree t t)))
  22865. (regexp-fmt "^\\*\\{%d,%s\\}\\(?: \\|$\\)")
  22866. (past-first-child nil)
  22867. ;; Make sure to skip inlinetasks.
  22868. (re (format regexp-fmt
  22869. current-level
  22870. (cond
  22871. ((not (featurep 'org-inlinetask)) "")
  22872. (org-odd-levels-only (- (* 2 org-inlinetask-min-level)
  22873. 3))
  22874. (t (1- org-inlinetask-min-level))))))
  22875. ;; Display parent heading.
  22876. (outline-flag-region (line-end-position 0) (line-end-position) nil)
  22877. (forward-line)
  22878. ;; Display children. First child may be deeper than expected
  22879. ;; MAX-LEVEL. Since we want to display it anyway, adjust
  22880. ;; MAX-LEVEL accordingly.
  22881. (while (re-search-forward re end t)
  22882. (unless past-first-child
  22883. (setq re (format regexp-fmt
  22884. current-level
  22885. (max (funcall outline-level) max-level)))
  22886. (setq past-first-child t))
  22887. (outline-flag-region (line-end-position 0) (line-end-position) nil)))))
  22888. (defun org-show-subtree ()
  22889. "Show everything after this heading at deeper levels."
  22890. (interactive)
  22891. (outline-flag-region
  22892. (point)
  22893. (save-excursion
  22894. (org-end-of-subtree t t))
  22895. nil))
  22896. (defun org-show-entry ()
  22897. "Show the body directly following this heading.
  22898. Show the heading too, if it is currently invisible."
  22899. (interactive)
  22900. (save-excursion
  22901. (ignore-errors
  22902. (org-back-to-heading t)
  22903. (outline-flag-region
  22904. (max (point-min) (1- (point)))
  22905. (save-excursion
  22906. (if (re-search-forward
  22907. (concat "[\r\n]\\(" org-outline-regexp "\\)") nil t)
  22908. (match-beginning 1)
  22909. (point-max)))
  22910. nil)
  22911. (org-cycle-hide-drawers 'children))))
  22912. (defun org-make-options-regexp (kwds &optional extra)
  22913. "Make a regular expression for keyword lines.
  22914. KWDS is a list of keywords, as strings. Optional argument EXTRA,
  22915. when non-nil, is a regexp matching keywords names."
  22916. (concat "^[ \t]*#\\+\\("
  22917. (regexp-opt kwds)
  22918. (and extra (concat (and kwds "\\|") extra))
  22919. "\\):[ \t]*\\(.*\\)"))
  22920. ;; Make isearch reveal the necessary context
  22921. (defun org-isearch-end ()
  22922. "Reveal context after isearch exits."
  22923. (when isearch-success ; only if search was successful
  22924. (if (featurep 'xemacs)
  22925. ;; Under XEmacs, the hook is run in the correct place,
  22926. ;; we directly show the context.
  22927. (org-show-context 'isearch)
  22928. ;; In Emacs the hook runs *before* restoring the overlays.
  22929. ;; So we have to use a one-time post-command-hook to do this.
  22930. ;; (Emacs 22 has a special variable, see function `org-mode')
  22931. (unless (and (boundp 'isearch-mode-end-hook-quit)
  22932. isearch-mode-end-hook-quit)
  22933. ;; Only when the isearch was not quitted.
  22934. (org-add-hook 'post-command-hook 'org-isearch-post-command
  22935. 'append 'local)))))
  22936. (defun org-isearch-post-command ()
  22937. "Remove self from hook, and show context."
  22938. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  22939. (org-show-context 'isearch))
  22940. ;;;; Integration with and fixes for other packages
  22941. ;;; Imenu support
  22942. (defvar org-imenu-markers nil
  22943. "All markers currently used by Imenu.")
  22944. (make-variable-buffer-local 'org-imenu-markers)
  22945. (defun org-imenu-new-marker (&optional pos)
  22946. "Return a new marker for use by Imenu, and remember the marker."
  22947. (let ((m (make-marker)))
  22948. (move-marker m (or pos (point)))
  22949. (push m org-imenu-markers)
  22950. m))
  22951. (defun org-imenu-get-tree ()
  22952. "Produce the index for Imenu."
  22953. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  22954. (setq org-imenu-markers nil)
  22955. (let* ((n org-imenu-depth)
  22956. (re (concat "^" (org-get-limited-outline-regexp)))
  22957. (subs (make-vector (1+ n) nil))
  22958. (last-level 0)
  22959. m level head0 head)
  22960. (save-excursion
  22961. (save-restriction
  22962. (widen)
  22963. (goto-char (point-max))
  22964. (while (re-search-backward re nil t)
  22965. (setq level (org-reduced-level (funcall outline-level)))
  22966. (when (and (<= level n)
  22967. (looking-at org-complex-heading-regexp)
  22968. (setq head0 (org-match-string-no-properties 4)))
  22969. (setq head (org-link-display-format head0)
  22970. m (org-imenu-new-marker))
  22971. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  22972. (if (>= level last-level)
  22973. (push (cons head m) (aref subs level))
  22974. (push (cons head (aref subs (1+ level))) (aref subs level))
  22975. (loop for i from (1+ level) to n do (aset subs i nil)))
  22976. (setq last-level level)))))
  22977. (aref subs 1)))
  22978. (eval-after-load "imenu"
  22979. '(progn
  22980. (add-hook 'imenu-after-jump-hook
  22981. (lambda ()
  22982. (if (derived-mode-p 'org-mode)
  22983. (org-show-context 'org-goto))))))
  22984. (defun org-link-display-format (link)
  22985. "Replace a link with its the description.
  22986. If there is no description, use the link target."
  22987. (save-match-data
  22988. (if (string-match org-bracket-link-analytic-regexp link)
  22989. (replace-match (if (match-end 5)
  22990. (match-string 5 link)
  22991. (concat (match-string 1 link)
  22992. (match-string 3 link)))
  22993. nil t link)
  22994. link)))
  22995. (defun org-toggle-link-display ()
  22996. "Toggle the literal or descriptive display of links."
  22997. (interactive)
  22998. (if org-descriptive-links
  22999. (progn (org-remove-from-invisibility-spec '(org-link))
  23000. (org-restart-font-lock)
  23001. (setq org-descriptive-links nil))
  23002. (progn (add-to-invisibility-spec '(org-link))
  23003. (org-restart-font-lock)
  23004. (setq org-descriptive-links t))))
  23005. ;; Speedbar support
  23006. (defvar org-speedbar-restriction-lock-overlay (make-overlay 1 1)
  23007. "Overlay marking the agenda restriction line in speedbar.")
  23008. (overlay-put org-speedbar-restriction-lock-overlay
  23009. 'face 'org-agenda-restriction-lock)
  23010. (overlay-put org-speedbar-restriction-lock-overlay
  23011. 'help-echo "Agendas are currently limited to this item.")
  23012. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  23013. (defun org-speedbar-set-agenda-restriction ()
  23014. "Restrict future agenda commands to the location at point in speedbar.
  23015. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  23016. (interactive)
  23017. (require 'org-agenda)
  23018. (let (p m tp np dir txt)
  23019. (cond
  23020. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  23021. 'org-imenu t))
  23022. (setq m (get-text-property p 'org-imenu-marker))
  23023. (with-current-buffer (marker-buffer m)
  23024. (goto-char m)
  23025. (org-agenda-set-restriction-lock 'subtree)))
  23026. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  23027. 'speedbar-function 'speedbar-find-file))
  23028. (setq tp (previous-single-property-change
  23029. (1+ p) 'speedbar-function)
  23030. np (next-single-property-change
  23031. tp 'speedbar-function)
  23032. dir (speedbar-line-directory)
  23033. txt (buffer-substring-no-properties (or tp (point-min))
  23034. (or np (point-max))))
  23035. (with-current-buffer (find-file-noselect
  23036. (let ((default-directory dir))
  23037. (expand-file-name txt)))
  23038. (unless (derived-mode-p 'org-mode)
  23039. (user-error "Cannot restrict to non-Org-mode file"))
  23040. (org-agenda-set-restriction-lock 'file)))
  23041. (t (user-error "Don't know how to restrict Org-mode's agenda")))
  23042. (move-overlay org-speedbar-restriction-lock-overlay
  23043. (point-at-bol) (point-at-eol))
  23044. (setq current-prefix-arg nil)
  23045. (org-agenda-maybe-redo)))
  23046. (defvar speedbar-file-key-map)
  23047. (declare-function speedbar-add-supported-extension "speedbar" (extension))
  23048. (eval-after-load "speedbar"
  23049. '(progn
  23050. (speedbar-add-supported-extension ".org")
  23051. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  23052. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  23053. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  23054. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  23055. (add-hook 'speedbar-visiting-tag-hook
  23056. (lambda () (and (derived-mode-p 'org-mode) (org-show-context 'org-goto))))))
  23057. ;;; Fixes and Hacks for problems with other packages
  23058. (defun org--flyspell-object-check-p (element)
  23059. "Non-nil when Flyspell can check object at point.
  23060. ELEMENT is the element at point."
  23061. (let ((object (save-excursion
  23062. (when (org-looking-at-p "\\>") (backward-char))
  23063. (org-element-context element))))
  23064. (case (org-element-type object)
  23065. ;; Prevent checks in links due to keybinding conflict with
  23066. ;; Flyspell.
  23067. ((code entity export-snippet inline-babel-call
  23068. inline-src-block line-break latex-fragment link macro
  23069. statistics-cookie target timestamp verbatim)
  23070. nil)
  23071. (footnote-reference
  23072. ;; Only in inline footnotes, within the definition.
  23073. (and (eq (org-element-property :type object) 'inline)
  23074. (< (save-excursion
  23075. (goto-char (org-element-property :begin object))
  23076. (search-forward ":" nil t 2))
  23077. (point))))
  23078. (otherwise t))))
  23079. (defun org-mode-flyspell-verify ()
  23080. "Function used for `flyspell-generic-check-word-predicate'."
  23081. (if (org-at-heading-p)
  23082. ;; At a headline or an inlinetask, check title only. This is
  23083. ;; faster than relying on `org-element-at-point'.
  23084. (and (save-excursion (beginning-of-line)
  23085. (and (let ((case-fold-search t))
  23086. (not (looking-at "\\*+ END[ \t]*$")))
  23087. (looking-at org-complex-heading-regexp)))
  23088. (match-beginning 4)
  23089. (>= (point) (match-beginning 4))
  23090. (or (not (match-beginning 5))
  23091. (< (point) (match-beginning 5))))
  23092. (let* ((element (org-element-at-point))
  23093. (post-affiliated (org-element-property :post-affiliated element)))
  23094. (cond
  23095. ;; Ignore checks in all affiliated keywords but captions.
  23096. ((< (point) post-affiliated)
  23097. (and (save-excursion
  23098. (beginning-of-line)
  23099. (let ((case-fold-search t)) (looking-at "[ \t]*#\\+CAPTION:")))
  23100. (> (point) (match-end 0))
  23101. (org--flyspell-object-check-p element)))
  23102. ;; Ignore checks in LOGBOOK (or equivalent) drawer.
  23103. ((let ((log (org-log-into-drawer)))
  23104. (and log
  23105. (let ((drawer (org-element-lineage element '(drawer))))
  23106. (and drawer
  23107. (eq (compare-strings
  23108. log nil nil
  23109. (org-element-property :drawer-name drawer) nil nil t)
  23110. t)))))
  23111. nil)
  23112. (t
  23113. (case (org-element-type element)
  23114. ((comment quote-section) t)
  23115. (comment-block
  23116. ;; Allow checks between block markers, not on them.
  23117. (and (> (line-beginning-position) post-affiliated)
  23118. (save-excursion
  23119. (end-of-line)
  23120. (skip-chars-forward " \r\t\n")
  23121. (< (point) (org-element-property :end element)))))
  23122. ;; Arbitrary list of keywords where checks are meaningful.
  23123. ;; Make sure point is on the value part of the element.
  23124. (keyword
  23125. (and (member (org-element-property :key element)
  23126. '("DESCRIPTION" "TITLE"))
  23127. (save-excursion
  23128. (search-backward ":" (line-beginning-position) t))))
  23129. ;; Check is globally allowed in paragraphs verse blocks and
  23130. ;; table rows (after affiliated keywords) but some objects
  23131. ;; must not be affected.
  23132. ((paragraph table-row verse-block)
  23133. (let ((cbeg (org-element-property :contents-begin element))
  23134. (cend (org-element-property :contents-end element)))
  23135. (and cbeg (>= (point) cbeg) (< (point) cend)
  23136. (org--flyspell-object-check-p element))))))))))
  23137. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  23138. (defun org-remove-flyspell-overlays-in (beg end)
  23139. "Remove flyspell overlays in region."
  23140. (and (org-bound-and-true-p flyspell-mode)
  23141. (fboundp 'flyspell-delete-region-overlays)
  23142. (flyspell-delete-region-overlays beg end)))
  23143. (defvar flyspell-delayed-commands)
  23144. (eval-after-load "flyspell"
  23145. '(add-to-list 'flyspell-delayed-commands 'org-self-insert-command))
  23146. ;; Make `bookmark-jump' shows the jump location if it was hidden.
  23147. (eval-after-load "bookmark"
  23148. '(if (boundp 'bookmark-after-jump-hook)
  23149. ;; We can use the hook
  23150. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  23151. ;; Hook not available, use advice
  23152. (defadvice bookmark-jump (after org-make-visible activate)
  23153. "Make the position visible."
  23154. (org-bookmark-jump-unhide))))
  23155. ;; Make sure saveplace shows the location if it was hidden
  23156. (eval-after-load "saveplace"
  23157. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  23158. "Make the position visible."
  23159. (org-bookmark-jump-unhide)))
  23160. ;; Make sure ecb shows the location if it was hidden
  23161. (eval-after-load "ecb"
  23162. '(defadvice ecb-method-clicked (after esf/org-show-context activate)
  23163. "Make hierarchy visible when jumping into location from ECB tree buffer."
  23164. (if (derived-mode-p 'org-mode)
  23165. (org-show-context))))
  23166. (defun org-bookmark-jump-unhide ()
  23167. "Unhide the current position, to show the bookmark location."
  23168. (and (derived-mode-p 'org-mode)
  23169. (or (outline-invisible-p)
  23170. (save-excursion (goto-char (max (point-min) (1- (point))))
  23171. (outline-invisible-p)))
  23172. (org-show-context 'bookmark-jump)))
  23173. (defun org-mark-jump-unhide ()
  23174. "Make the point visible with `org-show-context' after jumping to the mark."
  23175. (when (and (derived-mode-p 'org-mode)
  23176. (outline-invisible-p))
  23177. (org-show-context 'mark-goto)))
  23178. (eval-after-load "simple"
  23179. '(defadvice pop-to-mark-command (after org-make-visible activate)
  23180. "Make the point visible with `org-show-context'."
  23181. (org-mark-jump-unhide)))
  23182. (eval-after-load "simple"
  23183. '(defadvice exchange-point-and-mark (after org-make-visible activate)
  23184. "Make the point visible with `org-show-context'."
  23185. (org-mark-jump-unhide)))
  23186. (eval-after-load "simple"
  23187. '(defadvice pop-global-mark (after org-make-visible activate)
  23188. "Make the point visible with `org-show-context'."
  23189. (org-mark-jump-unhide)))
  23190. ;; Make session.el ignore our circular variable
  23191. (defvar session-globals-exclude)
  23192. (eval-after-load "session"
  23193. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  23194. ;;;; Finish up
  23195. (provide 'org)
  23196. (run-hooks 'org-load-hook)
  23197. ;;; org.el ends here